2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
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.
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.
25 #include <sys/types.h>
30 #include "../command.h"
32 static char *opt_event_list
;
33 static int opt_event_type
;
34 static const char *opt_loglevel
;
35 static int opt_loglevel_type
;
36 static int opt_kernel
;
37 static char *opt_session_name
;
38 static int opt_userspace
;
39 static int opt_enable_all
;
40 static char *opt_probe
;
41 static char *opt_function
;
42 static char *opt_function_entry_symbol
;
43 static char *opt_channel_name
;
45 /* Not implemented yet */
46 static char *opt_cmd_name
;
63 static struct lttng_handle
*handle
;
65 static struct poptOption long_options
[] = {
66 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
67 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
68 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
69 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
70 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
71 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
73 /* Not implemented yet */
74 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
75 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
77 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
79 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
80 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
81 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
84 * Currently removed from lttng kernel tracer. Removed from
85 * lttng UI to discourage its use.
87 {"function:entry", 0, POPT_ARG_STRING
, &opt_function_entry_symbol
, OPT_FUNCTION_ENTRY
, 0, 0},
89 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
90 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
91 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
92 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
99 static void usage(FILE *ofp
)
101 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
103 fprintf(ofp
, " -h, --help Show this help\n");
104 fprintf(ofp
, " --list-options Simple listing of options\n");
105 fprintf(ofp
, " -s, --session Apply on session name\n");
106 fprintf(ofp
, " -c, --channel Apply on this channel\n");
107 fprintf(ofp
, " -a, --all Enable all tracepoints\n");
108 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
110 fprintf(ofp
, " -u, --userspace [CMD] Apply for the user-space tracer\n");
111 fprintf(ofp
, " If no CMD, the domain used is UST global\n");
112 fprintf(ofp
, " or else the domain is UST EXEC_NAME\n");
113 fprintf(ofp
, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
115 fprintf(ofp
, " -u, --userspace Apply for the user-space tracer\n");
118 fprintf(ofp
, "Event options:\n");
119 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
120 fprintf(ofp
, " - userspace tracer supports wildcards at end of string.\n");
121 fprintf(ofp
, " Don't forget to quote to deal with bash expansion.\n");
122 fprintf(ofp
, " e.g.:\n");
123 fprintf(ofp
, " \"*\"\n");
124 fprintf(ofp
, " \"app_component:na*\"\n");
125 fprintf(ofp
, " --loglevel name\n");
126 fprintf(ofp
, " Tracepoint loglevel (range: 0 to loglevel)\n");
127 fprintf(ofp
, " --loglevel-only name\n");
128 fprintf(ofp
, " Tracepoint loglevel (only this loglevel)\n");
129 fprintf(ofp
, " --probe [addr | symbol | symbol+offset]\n");
130 fprintf(ofp
, " Dynamic probe.\n");
131 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
132 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
133 fprintf(ofp
, " --function [addr | symbol | symbol+offset]\n");
134 fprintf(ofp
, " Dynamic function entry/return probe.\n");
135 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
136 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
138 fprintf(ofp
, " --function:entry symbol\n");
139 fprintf(ofp
, " Function tracer event\n");
141 fprintf(ofp
, " --syscall System call event\n");
146 * Parse probe options.
148 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
152 char name
[LTTNG_SYMBOL_NAME_LEN
];
159 /* Check for symbol+offset */
160 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
162 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
163 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
164 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
165 if (strlen(s_hex
) == 0) {
166 ERR("Invalid probe offset %s", s_hex
);
170 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
171 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
172 ev
->attr
.probe
.addr
= 0;
176 /* Check for symbol */
177 if (isalpha(name
[0])) {
178 ret
= sscanf(opt
, "%s", name
);
180 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
181 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
182 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
183 ev
->attr
.probe
.offset
= 0;
184 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
185 ev
->attr
.probe
.addr
= 0;
190 /* Check for address */
191 ret
= sscanf(opt
, "%s", s_hex
);
193 if (strlen(s_hex
) == 0) {
194 ERR("Invalid probe address %s", s_hex
);
198 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
199 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
200 ev
->attr
.probe
.offset
= 0;
201 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
213 * Enabling event using the lttng API.
215 static int enable_events(char *session_name
)
217 int err
, ret
= CMD_SUCCESS
, warn
= 0;
218 char *event_name
, *channel_name
= NULL
;
219 struct lttng_event ev
;
220 struct lttng_domain dom
;
222 /* Create lttng domain */
224 dom
.type
= LTTNG_DOMAIN_KERNEL
;
225 } else if (opt_userspace
) {
226 dom
.type
= LTTNG_DOMAIN_UST
;
228 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
233 if (opt_channel_name
== NULL
) {
234 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
240 channel_name
= opt_channel_name
;
243 handle
= lttng_create_handle(session_name
, &dom
);
244 if (handle
== NULL
) {
249 if (opt_enable_all
) {
250 /* Default setup for enable all */
252 ev
.type
= opt_event_type
;
255 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
256 strcpy(ev
.name
, "*");
259 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
264 switch (opt_event_type
) {
265 case LTTNG_EVENT_TRACEPOINT
:
266 MSG("All %s tracepoints are enabled in channel %s",
267 opt_kernel
? "kernel" : "UST", channel_name
);
269 case LTTNG_EVENT_SYSCALL
:
271 MSG("All kernel system calls are enabled in channel %s",
275 case LTTNG_EVENT_ALL
:
276 MSG("All %s events are enabled in channel %s",
277 opt_kernel
? "kernel" : "UST", channel_name
);
281 * We should not be here since lttng_enable_event should have
282 * failed on the event type.
289 /* Strip event list */
290 event_name
= strtok(opt_event_list
, ",");
291 while (event_name
!= NULL
) {
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
;
297 /* Kernel tracer action */
299 DBG("Enabling kernel event %s for channel %s",
300 event_name
, channel_name
);
302 switch (opt_event_type
) {
303 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
304 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
306 case LTTNG_EVENT_TRACEPOINT
:
308 case LTTNG_EVENT_PROBE
:
309 ret
= parse_probe_opts(&ev
, opt_probe
);
311 ERR("Unable to parse probe options");
316 case LTTNG_EVENT_FUNCTION
:
317 ret
= parse_probe_opts(&ev
, opt_function
);
319 ERR("Unable to parse function probe options");
324 case LTTNG_EVENT_FUNCTION_ENTRY
:
325 strncpy(ev
.attr
.ftrace
.symbol_name
, opt_function_entry_symbol
,
326 LTTNG_SYMBOL_NAME_LEN
);
327 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
329 case LTTNG_EVENT_SYSCALL
:
330 MSG("per-syscall selection not supported yet. Use \"-a\" "
331 "for all syscalls.");
338 MSG("Kernel loglevels are not supported.");
343 /* kernel loglevels not implemented */
344 ev
.loglevel_type
= opt_loglevel_type
;
345 ev
.loglevel
[0] = '\0';
346 } else if (opt_userspace
) { /* User-space tracer action */
348 if (opt_cmd_name
!= NULL
|| opt_pid
) {
349 MSG("Only supporting tracing all UST processes (-u) for now.");
355 DBG("Enabling UST event %s for channel %s", event_name
,
358 switch (opt_event_type
) {
359 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
361 case LTTNG_EVENT_TRACEPOINT
:
362 /* Copy name and type of the event */
363 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
364 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
365 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
367 case LTTNG_EVENT_PROBE
:
368 case LTTNG_EVENT_FUNCTION
:
369 case LTTNG_EVENT_FUNCTION_ENTRY
:
370 case LTTNG_EVENT_SYSCALL
:
372 ERR("Event type not available for user-space tracing");
377 ev
.loglevel_type
= opt_loglevel_type
;
379 strncpy(ev
.loglevel
, opt_loglevel
, LTTNG_SYMBOL_NAME_LEN
);
380 ev
.loglevel
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
382 ev
.loglevel
[0] = '\0';
385 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
389 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
391 ERR("Event %s: %s (channel %s, session %s)", event_name
,
392 lttng_strerror(ret
), channel_name
, session_name
);
395 MSG("%s event %s created in channel %s",
396 opt_kernel
? "kernel": "UST", event_name
, channel_name
);
400 event_name
= strtok(NULL
, ",");
408 if (opt_channel_name
== NULL
) {
411 lttng_destroy_handle(handle
);
417 * Add event to trace session
419 int cmd_enable_events(int argc
, const char **argv
)
421 int opt
, ret
= CMD_SUCCESS
;
422 static poptContext pc
;
423 char *session_name
= NULL
;
425 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
426 poptReadDefaultConfig(pc
, 0);
428 /* Default event type */
429 opt_event_type
= LTTNG_EVENT_ALL
;
431 while ((opt
= poptGetNextOpt(pc
)) != -1) {
437 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
440 opt_event_type
= LTTNG_EVENT_PROBE
;
443 opt_event_type
= LTTNG_EVENT_FUNCTION
;
445 case OPT_FUNCTION_ENTRY
:
446 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
449 opt_event_type
= LTTNG_EVENT_SYSCALL
;
455 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL
;
456 opt_loglevel
= poptGetOptArg(pc
);
458 case OPT_LOGLEVEL_ONLY
:
459 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_ONLY
;
460 opt_loglevel
= poptGetOptArg(pc
);
462 case OPT_LIST_OPTIONS
:
463 list_cmd_options(stdout
, long_options
);
472 opt_event_list
= (char*) poptGetArg(pc
);
473 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
474 ERR("Missing event name(s).\n");
480 if (!opt_session_name
) {
481 session_name
= get_session_name();
482 if (session_name
== NULL
) {
487 session_name
= opt_session_name
;
490 ret
= enable_events(session_name
);
493 if (opt_session_name
== NULL
) {