Add a '--list-options' option to each command.
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
5a0de755 27#include <inttypes.h>
8f0d098b 28#include <ctype.h>
f3ed775e 29
c399183f 30#include "../command.h"
f3ed775e
DG
31
32static char *opt_event_list;
33static int opt_event_type;
6181537c 34static int opt_kernel;
5440dc42 35static char *opt_session_name;
f3ed775e
DG
36static int opt_userspace;
37static int opt_enable_all;
cf0e5467 38static char *opt_probe;
8f0d098b
MD
39static char *opt_function;
40static char *opt_function_entry_symbol;
f3ed775e 41static char *opt_channel_name;
d78d6610
DG
42#if 0
43/* Not implemented yet */
44static char *opt_cmd_name;
45static pid_t opt_pid;
46#endif
f3ed775e
DG
47
48enum {
49 OPT_HELP = 1,
f3ed775e 50 OPT_TRACEPOINT,
cf0e5467 51 OPT_PROBE,
f3ed775e 52 OPT_FUNCTION,
8f0d098b 53 OPT_FUNCTION_ENTRY,
a54bd42d 54 OPT_SYSCALL,
eeac7d46 55 OPT_USERSPACE,
13dce3b7 56 OPT_TRACEPOINT_LOGLEVEL,
679b4943 57 OPT_LIST_OPTIONS,
f3ed775e
DG
58};
59
cd80958d
DG
60static struct lttng_handle *handle;
61
f3ed775e
DG
62static struct poptOption long_options[] = {
63 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
64 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 65 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 66 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
67 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
68 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
69#if 0
70 /* Not implemented yet */
40e9d5d3 71 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
f3ed775e 72 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
73#else
74 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
75#endif
f3ed775e 76 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 77 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 78 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
79#if 0
80 /*
81 * Currently removed from lttng kernel tracer. Removed from
82 * lttng UI to discourage its use.
83 */
40e9d5d3 84 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 85#endif
7ebae521 86 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
13dce3b7 87 {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0},
679b4943 88 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
f3ed775e
DG
89 {0, 0, 0, 0, 0, 0, 0}
90};
91
92/*
93 * usage
94 */
95static void usage(FILE *ofp)
96{
97 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
98 fprintf(ofp, "\n");
99 fprintf(ofp, " -h, --help Show this help\n");
679b4943 100 fprintf(ofp, " --list-options Simple listing of options\n");
5440dc42 101 fprintf(ofp, " -s, --session Apply on session name\n");
f3ed775e 102 fprintf(ofp, " -c, --channel Apply on this channel\n");
e14f64a8 103 fprintf(ofp, " -a, --all Enable all tracepoints\n");
f3ed775e 104 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
d78d6610 105#if 0
f3ed775e 106 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
e14f64a8
DG
107 fprintf(ofp, " If no CMD, the domain used is UST global\n");
108 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
109 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
d78d6610
DG
110#else
111 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
112#endif
f3ed775e
DG
113 fprintf(ofp, "\n");
114 fprintf(ofp, "Event options:\n");
115 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
be49af1e
MD
116 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
117 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
118 fprintf(ofp, " e.g.:\n");
119 fprintf(ofp, " \"*\"\n");
120 fprintf(ofp, " \"app_component:na*\"\n");
13dce3b7 121 fprintf(ofp, " --loglevel Tracepoint loglevel\n");
8f0d098b 122 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
123 fprintf(ofp, " Dynamic probe.\n");
124 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
125 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
126 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
127 fprintf(ofp, " Dynamic function entry/return probe.\n");
128 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
129 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 130#if 0
8f0d098b
MD
131 fprintf(ofp, " --function:entry symbol\n");
132 fprintf(ofp, " Function tracer event\n");
b213d387 133#endif
a54bd42d 134 fprintf(ofp, " --syscall System call event\n");
f3ed775e
DG
135 fprintf(ofp, "\n");
136}
137
0d63dd19 138/*
6181537c 139 * Parse probe options.
0d63dd19 140 */
cf0e5467 141static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
142{
143 int ret;
8ff0bbd0 144 char s_hex[19];
0d63dd19
DG
145 char name[LTTNG_SYMBOL_NAME_LEN];
146
147 if (opt == NULL) {
148 ret = -1;
8f0d098b 149 goto end;
0d63dd19
DG
150 }
151
152 /* Check for symbol+offset */
8ff0bbd0 153 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 154 if (ret == 2) {
7d29a247 155 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 156 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 157 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
158 if (strlen(s_hex) == 0) {
159 ERR("Invalid probe offset %s", s_hex);
0d63dd19 160 ret = -1;
8f0d098b 161 goto end;
0d63dd19 162 }
8ff0bbd0 163 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 164 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 165 ev->attr.probe.addr = 0;
8f0d098b
MD
166 goto end;
167 }
168
169 /* Check for symbol */
170 if (isalpha(name[0])) {
171 ret = sscanf(opt, "%s", name);
172 if (ret == 1) {
173 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 174 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
175 DBG("probe symbol %s", ev->attr.probe.symbol_name);
176 ev->attr.probe.offset = 0;
177 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
178 ev->attr.probe.addr = 0;
179 goto end;
180 }
0d63dd19
DG
181 }
182
183 /* Check for address */
8ff0bbd0 184 ret = sscanf(opt, "%s", s_hex);
0d63dd19 185 if (ret > 0) {
8ff0bbd0
DG
186 if (strlen(s_hex) == 0) {
187 ERR("Invalid probe address %s", s_hex);
0d63dd19 188 ret = -1;
8f0d098b 189 goto end;
0d63dd19 190 }
8ff0bbd0 191 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 192 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
193 ev->attr.probe.offset = 0;
194 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 195 goto end;
0d63dd19
DG
196 }
197
198 /* No match */
199 ret = -1;
200
8f0d098b 201end:
0d63dd19
DG
202 return ret;
203}
204
f3ed775e 205/*
6181537c 206 * Enabling event using the lttng API.
f3ed775e 207 */
cd80958d 208static int enable_events(char *session_name)
f3ed775e
DG
209{
210 int err, ret = CMD_SUCCESS;
b73d0b29 211 char *event_name, *channel_name = NULL;
f3ed775e 212 struct lttng_event ev;
7d29a247 213 struct lttng_domain dom;
f3ed775e 214
f3ed775e
DG
215 if (opt_channel_name == NULL) {
216 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
217 if (err < 0) {
218 ret = CMD_FATAL;
219 goto error;
220 }
221 } else {
222 channel_name = opt_channel_name;
223 }
224
2bdd86d4 225 if (opt_kernel && opt_userspace) {
e14f64a8 226 ERR("Can't use -k/--kernel and -u/--userspace together");
2bdd86d4
MD
227 ret = CMD_FATAL;
228 goto error;
229 }
6181537c 230
7d29a247
DG
231 /* Create lttng domain */
232 if (opt_kernel) {
233 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 234 } else if (opt_userspace) {
2bdd86d4 235 dom.type = LTTNG_DOMAIN_UST;
6181537c 236 } else {
e14f64a8 237 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
1ab1ea0b 238 ret = CMD_UNDEFINED;
6181537c 239 goto error;
2bdd86d4 240 }
7d29a247 241
cd80958d
DG
242 handle = lttng_create_handle(session_name, &dom);
243 if (handle == NULL) {
244 ret = -1;
245 goto error;
246 }
1aef21b6 247
cd80958d 248 if (opt_enable_all) {
8c9ae521 249 /* Default setup for enable all */
75e8c5ab
MD
250
251 if (opt_kernel) {
252 ev.type = opt_event_type;
253 ev.name[0] = '\0';
254 } else {
255 ev.type = LTTNG_EVENT_TRACEPOINT;
256 strcpy(ev.name, "*");
257 }
8c9ae521
DG
258
259 ret = lttng_enable_event(handle, &ev, channel_name);
260 if (ret < 0) {
261 goto error;
262 }
263
0133c199
MD
264 switch (opt_event_type) {
265 case LTTNG_EVENT_TRACEPOINT:
2bdd86d4
MD
266 MSG("All %s tracepoints are enabled in channel %s",
267 opt_kernel ? "kernel" : "UST", channel_name);
0133c199 268 break;
a54bd42d 269 case LTTNG_EVENT_SYSCALL:
0133c199 270 if (opt_kernel) {
8c9ae521
DG
271 MSG("All kernel system calls are enabled in channel %s",
272 channel_name);
c93d9daf 273 }
0133c199 274 break;
7a3d1328 275 case LTTNG_EVENT_ALL:
2bdd86d4
MD
276 MSG("All %s events are enabled in channel %s",
277 opt_kernel ? "kernel" : "UST", channel_name);
7a3d1328 278 break;
0133c199 279 default:
8c9ae521 280 /*
6181537c
DG
281 * We should not be here since lttng_enable_event should have
282 * failed on the event type.
8c9ae521 283 */
f3ed775e
DG
284 goto error;
285 }
8c9ae521 286 goto end;
f3ed775e
DG
287 }
288
289 /* Strip event list */
290 event_name = strtok(opt_event_list, ",");
291 while (event_name != NULL) {
6181537c
DG
292 /* Copy name and type of the event */
293 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
294 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
295 ev.type = opt_event_type;
296
f3ed775e
DG
297 /* Kernel tracer action */
298 if (opt_kernel) {
299 DBG("Enabling kernel event %s for channel %s",
300 event_name, channel_name);
f3ed775e
DG
301
302 switch (opt_event_type) {
7a3d1328
MD
303 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
304 ev.type = LTTNG_EVENT_TRACEPOINT;
305 /* Fall-through */
e6ddca71 306 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 307 break;
7d29a247 308 case LTTNG_EVENT_PROBE:
cf0e5467 309 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 310 if (ret < 0) {
cf0e5467 311 ERR("Unable to parse probe options");
0d63dd19
DG
312 ret = 0;
313 goto error;
314 }
f3ed775e
DG
315 break;
316 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
317 ret = parse_probe_opts(&ev, opt_function);
318 if (ret < 0) {
319 ERR("Unable to parse function probe options");
320 ret = 0;
321 goto error;
322 }
323 break;
324 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
325 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
326 LTTNG_SYMBOL_NAME_LEN);
99497cd0 327 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 328 break;
a54bd42d 329 case LTTNG_EVENT_SYSCALL:
6181537c
DG
330 MSG("per-syscall selection not supported yet. Use \"-a\" "
331 "for all syscalls.");
f3ed775e 332 default:
1ab1ea0b 333 ret = CMD_UNDEFINED;
f3ed775e
DG
334 goto error;
335 }
f3ed775e 336 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 337#if 0
e14f64a8
DG
338 if (opt_cmd_name != NULL || opt_pid) {
339 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 340 ret = CMD_UNDEFINED;
2bdd86d4
MD
341 goto error;
342 }
d78d6610 343#endif
6181537c
DG
344
345 DBG("Enabling UST event %s for channel %s", event_name,
346 channel_name);
2bdd86d4
MD
347
348 switch (opt_event_type) {
349 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
350 /* Fall-through */
351 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
352 /* Copy name and type of the event */
353 ev.type = LTTNG_EVENT_TRACEPOINT;
354 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
355 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4 356 break;
13dce3b7 357 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
e4baff1e 358 /* Copy name and type of the event */
13dce3b7 359 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
e4baff1e
MD
360 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
361 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
13dce3b7 362 break;
2bdd86d4
MD
363 case LTTNG_EVENT_PROBE:
364 case LTTNG_EVENT_FUNCTION:
365 case LTTNG_EVENT_FUNCTION_ENTRY:
366 case LTTNG_EVENT_SYSCALL:
367 default:
1ab1ea0b 368 ret = CMD_UNDEFINED;
2bdd86d4
MD
369 goto error;
370 }
f3ed775e 371 } else {
e14f64a8 372 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
f3ed775e
DG
373 goto error;
374 }
375
6181537c
DG
376 ret = lttng_enable_event(handle, &ev, channel_name);
377 if (ret == 0) {
378 MSG("%s event %s created in channel %s",
379 opt_kernel ? "kernel": "UST", event_name, channel_name);
380 }
381
f3ed775e
DG
382 /* Next event */
383 event_name = strtok(NULL, ",");
384 }
385
8c9ae521 386end:
f3ed775e 387error:
b73d0b29
MD
388 if (opt_channel_name == NULL) {
389 free(channel_name);
390 }
cd80958d
DG
391 lttng_destroy_handle(handle);
392
f3ed775e
DG
393 return ret;
394}
395
396/*
6181537c 397 * Add event to trace session
f3ed775e
DG
398 */
399int cmd_enable_events(int argc, const char **argv)
400{
401 int opt, ret;
402 static poptContext pc;
cd80958d 403 char *session_name = NULL;
f3ed775e
DG
404
405 pc = poptGetContext(NULL, argc, argv, long_options, 0);
406 poptReadDefaultConfig(pc, 0);
407
408 /* Default event type */
7a3d1328 409 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
410
411 while ((opt = poptGetNextOpt(pc)) != -1) {
412 switch (opt) {
413 case OPT_HELP:
414 usage(stderr);
415 ret = CMD_SUCCESS;
416 goto end;
f3ed775e 417 case OPT_TRACEPOINT:
e6ddca71 418 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 419 break;
cf0e5467 420 case OPT_PROBE:
7d29a247 421 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
422 break;
423 case OPT_FUNCTION:
424 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
425 break;
426 case OPT_FUNCTION_ENTRY:
427 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 428 break;
a54bd42d
MD
429 case OPT_SYSCALL:
430 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 431 break;
eeac7d46
MD
432 case OPT_USERSPACE:
433 opt_userspace = 1;
eeac7d46 434 break;
13dce3b7
MD
435 case OPT_TRACEPOINT_LOGLEVEL:
436 opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
437 break;
679b4943
SM
438 case OPT_LIST_OPTIONS:
439 list_cmd_options(stdout, long_options);
440 ret = CMD_SUCCESS;
441 goto end;
f3ed775e
DG
442 default:
443 usage(stderr);
444 ret = CMD_UNDEFINED;
445 goto end;
446 }
447 }
448
449 opt_event_list = (char*) poptGetArg(pc);
450 if (opt_event_list == NULL && opt_enable_all == 0) {
451 ERR("Missing event name(s).\n");
452 usage(stderr);
453 ret = CMD_SUCCESS;
454 goto end;
455 }
456
cd80958d
DG
457 if (!opt_session_name) {
458 session_name = get_session_name();
459 if (session_name == NULL) {
460 ret = -1;
461 goto end;
462 }
463 } else {
464 session_name = opt_session_name;
465 }
466
467 ret = enable_events(session_name);
f3ed775e
DG
468
469end:
cd80958d
DG
470 if (opt_session_name == NULL) {
471 free(session_name);
472 }
473
f3ed775e
DG
474 return ret;
475}
This page took 0.0818449999999999 seconds and 4 git commands to generate.