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>
34 static char *opt_event_list
;
35 static int opt_event_type
;
36 static char *opt_kernel
;
37 static char *opt_session_name
;
38 static int opt_pid_all
;
39 static int opt_userspace
;
40 static char *opt_cmd_name
;
41 static int opt_enable_all
;
43 static char *opt_probe
;
44 static char *opt_function
;
45 static char *opt_function_entry_symbol
;
46 static char *opt_channel_name
;
59 static struct lttng_handle
*handle
;
61 static struct poptOption long_options
[] = {
62 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
63 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
64 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
65 {"all-events", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
66 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
67 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
68 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
69 {"all", 0, POPT_ARG_VAL
, &opt_pid_all
, 1, 0, 0},
70 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
71 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
72 {"marker", 0, POPT_ARG_NONE
, 0, OPT_MARKER
, 0, 0},
73 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
74 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
77 * Currently removed from lttng kernel tracer. Removed from
78 * lttng UI to discourage its use.
80 {"function:entry", 0, POPT_ARG_STRING
, &opt_function_entry_symbol
, OPT_FUNCTION_ENTRY
, 0, 0},
82 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
89 static void usage(FILE *ofp
)
91 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
93 fprintf(ofp
, " -h, --help Show this help\n");
94 fprintf(ofp
, " -s, --session Apply on session name\n");
95 fprintf(ofp
, " -c, --channel Apply on this channel\n");
96 fprintf(ofp
, " -a, --all-events Enable all tracepoints\n");
97 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
98 fprintf(ofp
, " -u, --userspace [CMD] Apply for the user-space tracer\n");
99 fprintf(ofp
, " --all If -u, apply on all traceable apps\n");
100 fprintf(ofp
, " -p, --pid PID If -u, apply on a specific PID\n");
102 fprintf(ofp
, "Event options:\n");
103 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
104 fprintf(ofp
, " --probe [addr | symbol | symbol+offset]\n");
105 fprintf(ofp
, " Dynamic probe.\n");
106 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
107 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
108 fprintf(ofp
, " --function [addr | symbol | symbol+offset]\n");
109 fprintf(ofp
, " Dynamic function entry/return probe.\n");
110 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
111 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
113 fprintf(ofp
, " --function:entry symbol\n");
114 fprintf(ofp
, " Function tracer event\n");
116 fprintf(ofp
, " --syscall System call event\n");
117 fprintf(ofp
, " --marker User-space marker (deprecated)\n");
124 * Parse probe options.
126 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
130 char name
[LTTNG_SYMBOL_NAME_LEN
];
137 /* Check for symbol+offset */
138 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
140 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
141 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
142 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
143 if (strlen(s_hex
) == 0) {
144 ERR("Invalid probe offset %s", s_hex
);
148 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
149 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
150 ev
->attr
.probe
.addr
= 0;
154 /* Check for symbol */
155 if (isalpha(name
[0])) {
156 ret
= sscanf(opt
, "%s", name
);
158 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
159 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
160 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
161 ev
->attr
.probe
.offset
= 0;
162 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
163 ev
->attr
.probe
.addr
= 0;
168 /* Check for address */
169 ret
= sscanf(opt
, "%s", s_hex
);
171 if (strlen(s_hex
) == 0) {
172 ERR("Invalid probe address %s", s_hex
);
176 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
177 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
178 ev
->attr
.probe
.offset
= 0;
179 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
193 * Enabling event using the lttng API.
195 static int enable_events(char *session_name
)
197 int err
, ret
= CMD_SUCCESS
;
198 char *event_name
, *channel_name
= NULL
;
199 struct lttng_event ev
;
200 struct lttng_domain dom
;
202 if (opt_channel_name
== NULL
) {
203 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
209 channel_name
= opt_channel_name
;
212 if (opt_kernel
&& opt_userspace
) {
213 MSG("Choose only one of --kernel or --userspace");
217 /* Create lttng domain */
219 dom
.type
= LTTNG_DOMAIN_KERNEL
;
223 * LTTNG_DOMAIN_UST_EXEC_NAME,
224 * LTTNG_DOMAIN_UST_PID,
225 * LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
227 dom
.type
= LTTNG_DOMAIN_UST
;
230 handle
= lttng_create_handle(session_name
, &dom
);
231 if (handle
== NULL
) {
236 if (opt_enable_all
) {
237 /* Default setup for enable all */
239 ev
.type
= opt_event_type
;
241 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
246 switch (opt_event_type
) {
247 case LTTNG_EVENT_TRACEPOINT
:
248 MSG("All %s tracepoints are enabled in channel %s",
249 opt_kernel
? "kernel" : "UST", channel_name
);
251 case LTTNG_EVENT_SYSCALL
:
253 MSG("All kernel system calls are enabled in channel %s",
257 case LTTNG_EVENT_ALL
:
258 MSG("All %s events are enabled in channel %s",
259 opt_kernel
? "kernel" : "UST", channel_name
);
263 * We should not be here since
264 * lttng_enable_event should have failed on the
272 /* Strip event list */
273 event_name
= strtok(opt_event_list
, ",");
274 while (event_name
!= NULL
) {
275 /* Kernel tracer action */
277 DBG("Enabling kernel event %s for channel %s",
278 event_name
, channel_name
);
279 /* Copy name and type of the event */
280 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
281 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
282 ev
.type
= opt_event_type
;
284 switch (opt_event_type
) {
285 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
286 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
288 case LTTNG_EVENT_TRACEPOINT
:
290 case LTTNG_EVENT_PROBE
:
291 ret
= parse_probe_opts(&ev
, opt_probe
);
293 ERR("Unable to parse probe options");
298 case LTTNG_EVENT_FUNCTION
:
299 ret
= parse_probe_opts(&ev
, opt_function
);
301 ERR("Unable to parse function probe options");
306 case LTTNG_EVENT_FUNCTION_ENTRY
:
307 strncpy(ev
.attr
.ftrace
.symbol_name
,
308 opt_function_entry_symbol
,
309 LTTNG_SYMBOL_NAME_LEN
);
310 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
312 case LTTNG_EVENT_SYSCALL
:
313 MSG("per-syscall selection not supported yet. Use \"-a\" for all syscalls.");
314 ret
= CMD_NOT_IMPLEMENTED
;
317 ret
= CMD_NOT_IMPLEMENTED
;
321 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
323 MSG("Kernel event %s created in channel %s", event_name
, channel_name
);
325 } else if (opt_userspace
) { /* User-space tracer action */
327 * TODO: only supporting pid_all tracing for
328 * now. Should have different domain based on
332 MSG("Only supporting tracing all UST processes (-u --all) for now.");
333 ret
= CMD_NOT_IMPLEMENTED
;
336 DBG("Enabling UST event %s for channel %s",
337 event_name
, channel_name
);
338 /* Copy name and type of the event */
339 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
340 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
341 ev
.type
= opt_event_type
;
343 switch (opt_event_type
) {
344 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
345 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
347 case LTTNG_EVENT_TRACEPOINT
:
349 case LTTNG_EVENT_PROBE
:
350 case LTTNG_EVENT_FUNCTION
:
351 case LTTNG_EVENT_FUNCTION_ENTRY
:
352 case LTTNG_EVENT_SYSCALL
:
354 ret
= CMD_NOT_IMPLEMENTED
;
358 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
360 MSG("UST event %s created in channel %s", event_name
, channel_name
);
363 ERR("Please specify a tracer (--kernel or --userspace)");
368 event_name
= strtok(NULL
, ",");
373 if (opt_channel_name
== NULL
) {
376 lttng_destroy_handle(handle
);
384 * Add event to trace session
386 int cmd_enable_events(int argc
, const char **argv
)
389 static poptContext pc
;
390 char *session_name
= NULL
;
392 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
393 poptReadDefaultConfig(pc
, 0);
395 /* Default event type */
396 opt_event_type
= LTTNG_EVENT_ALL
;
398 while ((opt
= poptGetNextOpt(pc
)) != -1) {
405 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
408 ret
= CMD_NOT_IMPLEMENTED
;
411 opt_event_type
= LTTNG_EVENT_PROBE
;
414 opt_event_type
= LTTNG_EVENT_FUNCTION
;
416 case OPT_FUNCTION_ENTRY
:
417 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
420 opt_event_type
= LTTNG_EVENT_SYSCALL
;
432 opt_event_list
= (char*) poptGetArg(pc
);
433 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
434 ERR("Missing event name(s).\n");
440 if (!opt_session_name
) {
441 session_name
= get_session_name();
442 if (session_name
== NULL
) {
447 session_name
= opt_session_name
;
450 ret
= enable_events(session_name
);
453 if (opt_session_name
== NULL
) {