2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
29 #include "../command.h"
31 static char *opt_event_list
;
32 static int opt_event_type
;
33 static const char *opt_loglevel
;
34 static int opt_loglevel_type
;
35 static int opt_kernel
;
36 static char *opt_session_name
;
37 static int opt_userspace
;
38 static int opt_enable_all
;
39 static char *opt_probe
;
40 static char *opt_function
;
41 static char *opt_function_entry_symbol
;
42 static char *opt_channel_name
;
44 /* Not implemented yet */
45 static char *opt_cmd_name
;
62 static struct lttng_handle
*handle
;
64 static struct poptOption long_options
[] = {
65 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
66 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
67 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
68 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
69 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
70 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
72 /* Not implemented yet */
73 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
74 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
76 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
78 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
79 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
80 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
83 * Currently removed from lttng kernel tracer. Removed from
84 * lttng UI to discourage its use.
86 {"function:entry", 0, POPT_ARG_STRING
, &opt_function_entry_symbol
, OPT_FUNCTION_ENTRY
, 0, 0},
88 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
89 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
90 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
91 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
98 static void usage(FILE *ofp
)
100 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
102 fprintf(ofp
, " -h, --help Show this help\n");
103 fprintf(ofp
, " --list-options Simple listing of options\n");
104 fprintf(ofp
, " -s, --session Apply to session name\n");
105 fprintf(ofp
, " -c, --channel Apply to this channel\n");
106 fprintf(ofp
, " -a, --all Enable all tracepoints and syscalls\n");
107 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
109 fprintf(ofp
, " -u, --userspace [CMD] Apply to the user-space tracer\n");
110 fprintf(ofp
, " If no CMD, the domain used is UST global\n");
111 fprintf(ofp
, " or else the domain is UST EXEC_NAME\n");
112 fprintf(ofp
, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
114 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
117 fprintf(ofp
, "Event options:\n");
118 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
119 fprintf(ofp
, " - userspace tracer supports wildcards at end of string.\n");
120 fprintf(ofp
, " Don't forget to quote to deal with bash expansion.\n");
121 fprintf(ofp
, " e.g.:\n");
122 fprintf(ofp
, " \"*\"\n");
123 fprintf(ofp
, " \"app_component:na*\"\n");
124 fprintf(ofp
, " --probe [addr | symbol | symbol+offset]\n");
125 fprintf(ofp
, " Dynamic probe.\n");
126 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
127 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
128 fprintf(ofp
, " --function [addr | symbol | symbol+offset]\n");
129 fprintf(ofp
, " Dynamic function entry/return probe.\n");
130 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
131 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
133 fprintf(ofp
, " --function:entry symbol\n");
134 fprintf(ofp
, " Function tracer event\n");
136 fprintf(ofp
, " --syscall System call event\n");
138 fprintf(ofp
, " --loglevel name\n");
139 fprintf(ofp
, " Tracepoint loglevel range from 0 to loglevel\n");
140 fprintf(ofp
, " --loglevel-only name\n");
141 fprintf(ofp
, " Tracepoint loglevel (only this loglevel)\n");
143 fprintf(ofp
, " The loglevel or loglevel-only options should be\n");
144 fprintf(ofp
, " combined with a tracepoint name or tracepoint\n");
145 fprintf(ofp
, " wildcard.\n");
146 fprintf(ofp
, " Available loglevels:\n");
147 fprintf(ofp
, " (higher value is more verbose)\n");
148 fprintf(ofp
, " TRACE_EMERG = 0\n");
149 fprintf(ofp
, " TRACE_ALERT = 1\n");
150 fprintf(ofp
, " TRACE_CRIT = 2\n");
151 fprintf(ofp
, " TRACE_ERR = 3\n");
152 fprintf(ofp
, " TRACE_WARNING = 4\n");
153 fprintf(ofp
, " TRACE_NOTICE = 5\n");
154 fprintf(ofp
, " TRACE_INFO = 6\n");
155 fprintf(ofp
, " TRACE_DEBUG_SYSTEM = 7\n");
156 fprintf(ofp
, " TRACE_DEBUG_PROGRAM = 8\n");
157 fprintf(ofp
, " TRACE_DEBUG_PROCESS = 9\n");
158 fprintf(ofp
, " TRACE_DEBUG_MODULE = 10\n");
159 fprintf(ofp
, " TRACE_DEBUG_UNIT = 11\n");
160 fprintf(ofp
, " TRACE_DEBUG_FUNCTION = 12\n");
161 fprintf(ofp
, " TRACE_DEBUG_LINE = 13\n");
162 fprintf(ofp
, " TRACE_DEBUG = 14\n");
163 fprintf(ofp
, " (shortcuts such as \"system\" are allowed)\n");
168 * Parse probe options.
170 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
174 char name
[LTTNG_SYMBOL_NAME_LEN
];
181 /* Check for symbol+offset */
182 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
184 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
185 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
186 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
187 if (strlen(s_hex
) == 0) {
188 ERR("Invalid probe offset %s", s_hex
);
192 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
193 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
194 ev
->attr
.probe
.addr
= 0;
198 /* Check for symbol */
199 if (isalpha(name
[0])) {
200 ret
= sscanf(opt
, "%s", name
);
202 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
203 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
204 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
205 ev
->attr
.probe
.offset
= 0;
206 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
207 ev
->attr
.probe
.addr
= 0;
212 /* Check for address */
213 ret
= sscanf(opt
, "%s", s_hex
);
215 if (strlen(s_hex
) == 0) {
216 ERR("Invalid probe address %s", s_hex
);
220 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
221 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
222 ev
->attr
.probe
.offset
= 0;
223 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
235 * Maps loglevel from string to value
238 int loglevel_str_to_value(const char *inputstr
)
241 char str
[LTTNG_SYMBOL_NAME_LEN
];
243 while (inputstr
[i
] != '\0' && i
< LTTNG_SYMBOL_NAME_LEN
) {
244 str
[i
] = toupper(inputstr
[i
]);
248 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
249 return LTTNG_LOGLEVEL_EMERG
;
250 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
251 return LTTNG_LOGLEVEL_ALERT
;
252 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
253 return LTTNG_LOGLEVEL_CRIT
;
254 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
255 return LTTNG_LOGLEVEL_ERR
;
256 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
257 return LTTNG_LOGLEVEL_WARNING
;
258 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
259 return LTTNG_LOGLEVEL_NOTICE
;
260 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
261 return LTTNG_LOGLEVEL_INFO
;
262 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
263 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
264 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
265 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
266 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
267 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
268 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
269 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
270 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
271 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
272 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
273 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
274 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
275 return LTTNG_LOGLEVEL_DEBUG_LINE
;
276 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
277 return LTTNG_LOGLEVEL_DEBUG
;
284 * Enabling event using the lttng API.
286 static int enable_events(char *session_name
)
288 int err
, ret
= CMD_SUCCESS
, warn
= 0;
289 char *event_name
, *channel_name
= NULL
;
290 struct lttng_event ev
;
291 struct lttng_domain dom
;
293 memset(&ev
, 0, sizeof(ev
));
294 memset(&dom
, 0, sizeof(dom
));
296 /* Create lttng domain */
298 dom
.type
= LTTNG_DOMAIN_KERNEL
;
299 } else if (opt_userspace
) {
300 dom
.type
= LTTNG_DOMAIN_UST
;
302 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
307 if (opt_channel_name
== NULL
) {
308 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
314 channel_name
= opt_channel_name
;
317 handle
= lttng_create_handle(session_name
, &dom
);
318 if (handle
== NULL
) {
323 if (opt_enable_all
) {
324 /* Default setup for enable all */
326 ev
.type
= opt_event_type
;
328 /* kernel loglevels not implemented */
329 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
331 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
332 strcpy(ev
.name
, "*");
333 ev
.loglevel_type
= opt_loglevel_type
;
335 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
336 if (ev
.loglevel
== -1) {
337 ERR("Unknown loglevel %s", opt_loglevel
);
346 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
351 switch (opt_event_type
) {
352 case LTTNG_EVENT_TRACEPOINT
:
354 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
355 opt_kernel
? "kernel" : "UST", channel_name
,
358 MSG("All %s tracepoints are enabled in channel %s",
359 opt_kernel
? "kernel" : "UST", channel_name
);
363 case LTTNG_EVENT_SYSCALL
:
365 MSG("All kernel system calls are enabled in channel %s",
369 case LTTNG_EVENT_ALL
:
371 MSG("All %s events are enabled in channel %s for loglevel %s",
372 opt_kernel
? "kernel" : "UST", channel_name
,
375 MSG("All %s events are enabled in channel %s",
376 opt_kernel
? "kernel" : "UST", channel_name
);
381 * We should not be here since lttng_enable_event should have
382 * failed on the event type.
389 /* Strip event list */
390 event_name
= strtok(opt_event_list
, ",");
391 while (event_name
!= NULL
) {
392 /* Copy name and type of the event */
393 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
394 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
395 ev
.type
= opt_event_type
;
397 /* Kernel tracer action */
399 DBG("Enabling kernel event %s for channel %s",
400 event_name
, channel_name
);
402 switch (opt_event_type
) {
403 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
404 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
406 case LTTNG_EVENT_TRACEPOINT
:
408 case LTTNG_EVENT_PROBE
:
409 ret
= parse_probe_opts(&ev
, opt_probe
);
411 ERR("Unable to parse probe options");
416 case LTTNG_EVENT_FUNCTION
:
417 ret
= parse_probe_opts(&ev
, opt_function
);
419 ERR("Unable to parse function probe options");
424 case LTTNG_EVENT_FUNCTION_ENTRY
:
425 strncpy(ev
.attr
.ftrace
.symbol_name
, opt_function_entry_symbol
,
426 LTTNG_SYMBOL_NAME_LEN
);
427 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
429 case LTTNG_EVENT_SYSCALL
:
430 MSG("per-syscall selection not supported yet. Use \"-a\" "
431 "for all syscalls.");
438 MSG("Kernel loglevels are not supported.");
443 /* kernel loglevels not implemented */
444 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
445 } else if (opt_userspace
) { /* User-space tracer action */
447 if (opt_cmd_name
!= NULL
|| opt_pid
) {
448 MSG("Only supporting tracing all UST processes (-u) for now.");
454 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
455 channel_name
, opt_loglevel
? : "<all>");
457 switch (opt_event_type
) {
458 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
460 case LTTNG_EVENT_TRACEPOINT
:
461 /* Copy name and type of the event */
462 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
463 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
464 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
466 case LTTNG_EVENT_PROBE
:
467 case LTTNG_EVENT_FUNCTION
:
468 case LTTNG_EVENT_FUNCTION_ENTRY
:
469 case LTTNG_EVENT_SYSCALL
:
471 ERR("Event type not available for user-space tracing");
476 ev
.loglevel_type
= opt_loglevel_type
;
478 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
479 if (ev
.loglevel
== -1) {
480 ERR("Unknown loglevel %s", opt_loglevel
);
488 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
493 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
495 ERR("Event %s: %s (channel %s, session %s)", event_name
,
496 lttng_strerror(ret
), channel_name
, session_name
);
499 MSG("%s event %s created in channel %s",
500 opt_kernel
? "kernel": "UST", event_name
, channel_name
);
504 event_name
= strtok(NULL
, ",");
512 if (opt_channel_name
== NULL
) {
515 lttng_destroy_handle(handle
);
521 * Add event to trace session
523 int cmd_enable_events(int argc
, const char **argv
)
525 int opt
, ret
= CMD_SUCCESS
;
526 static poptContext pc
;
527 char *session_name
= NULL
;
529 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
530 poptReadDefaultConfig(pc
, 0);
532 /* Default event type */
533 opt_event_type
= LTTNG_EVENT_ALL
;
535 while ((opt
= poptGetNextOpt(pc
)) != -1) {
541 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
544 opt_event_type
= LTTNG_EVENT_PROBE
;
547 opt_event_type
= LTTNG_EVENT_FUNCTION
;
549 case OPT_FUNCTION_ENTRY
:
550 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
553 opt_event_type
= LTTNG_EVENT_SYSCALL
;
559 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
560 opt_loglevel
= poptGetOptArg(pc
);
562 case OPT_LOGLEVEL_ONLY
:
563 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
564 opt_loglevel
= poptGetOptArg(pc
);
566 case OPT_LIST_OPTIONS
:
567 list_cmd_options(stdout
, long_options
);
576 opt_event_list
= (char*) poptGetArg(pc
);
577 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
578 ERR("Missing event name(s).\n");
584 if (!opt_session_name
) {
585 session_name
= get_session_name();
586 if (session_name
== NULL
) {
591 session_name
= opt_session_name
;
594 ret
= enable_events(session_name
);
597 if (opt_session_name
== NULL
) {