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_cmd_name
;
38 static char *opt_session_name
;
39 static int opt_pid_all
;
40 static int opt_userspace
;
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
, 0, 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
, 0, OPT_PROBE
, 0, 0},
74 {"function", 0, POPT_ARG_STRING
, 0, OPT_FUNCTION
, 0, 0},
75 {"function:entry", 0, POPT_ARG_STRING
, 0, OPT_FUNCTION_ENTRY
, 0, 0},
76 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
83 static void usage(FILE *ofp
)
85 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
87 fprintf(ofp
, " -h, --help Show this help\n");
88 fprintf(ofp
, " -s, --session Apply on session name\n");
89 fprintf(ofp
, " -c, --channel Apply on this channel\n");
90 fprintf(ofp
, " -a, --all-events Enable all tracepoints\n");
91 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
92 fprintf(ofp
, " -u, --userspace [CMD] Apply for the user-space tracer\n");
93 fprintf(ofp
, " --all If -u, apply on all traceable apps\n");
94 fprintf(ofp
, " -p, --pid PID If -u, apply on a specific PID\n");
96 fprintf(ofp
, "Event options:\n");
97 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
98 fprintf(ofp
, " --probe [addr | symbol | symbol+offset]\n");
99 fprintf(ofp
, " Dynamic probe.\n");
100 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
101 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
102 fprintf(ofp
, " --function [addr | symbol | symbol+offset]\n");
103 fprintf(ofp
, " Dynamic function entry/return probe.\n");
104 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
105 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
106 fprintf(ofp
, " --function:entry symbol\n");
107 fprintf(ofp
, " Function tracer event\n");
108 fprintf(ofp
, " --syscall System call event\n");
109 fprintf(ofp
, " --marker User-space marker (deprecated)\n");
116 * Parse probe options.
118 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
122 char name
[LTTNG_SYMBOL_NAME_LEN
];
129 /* Check for symbol+offset */
130 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
132 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
133 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
134 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
135 if (strlen(s_hex
) == 0) {
136 ERR("Invalid probe offset %s", s_hex
);
140 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
141 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
142 ev
->attr
.probe
.addr
= 0;
146 /* Check for symbol */
147 if (isalpha(name
[0])) {
148 ret
= sscanf(opt
, "%s", name
);
150 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
151 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
152 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
153 ev
->attr
.probe
.offset
= 0;
154 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
155 ev
->attr
.probe
.addr
= 0;
160 /* Check for address */
161 ret
= sscanf(opt
, "%s", s_hex
);
163 if (strlen(s_hex
) == 0) {
164 ERR("Invalid probe address %s", s_hex
);
168 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
169 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
170 ev
->attr
.probe
.offset
= 0;
171 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
185 * Enabling event using the lttng API.
187 static int enable_events(char *session_name
)
189 int err
, ret
= CMD_SUCCESS
;
190 char *event_name
, *channel_name
= NULL
;
191 struct lttng_event ev
;
192 struct lttng_domain dom
;
194 if (opt_channel_name
== NULL
) {
195 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
201 channel_name
= opt_channel_name
;
204 /* Create lttng domain */
206 dom
.type
= LTTNG_DOMAIN_KERNEL
;
209 handle
= lttng_create_handle(session_name
, &dom
);
210 if (handle
== NULL
) {
215 if (opt_enable_all
) {
216 /* Default setup for enable all */
218 ev
.type
= opt_event_type
;
220 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
225 switch (opt_event_type
) {
226 case LTTNG_EVENT_TRACEPOINT
:
228 MSG("All kernel tracepoints are enabled in channel %s",
232 case LTTNG_EVENT_SYSCALL
:
234 MSG("All kernel system calls are enabled in channel %s",
238 case LTTNG_EVENT_ALL
:
240 MSG("All kernel events are enabled in channel %s",
246 * We should not be here since lttng_enable_event should had failed
256 /* Strip event list */
257 event_name
= strtok(opt_event_list
, ",");
258 while (event_name
!= NULL
) {
259 /* Kernel tracer action */
261 DBG("Enabling kernel event %s for channel %s",
262 event_name
, channel_name
);
263 /* Copy name and type of the event */
264 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
265 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
266 ev
.type
= opt_event_type
;
268 switch (opt_event_type
) {
269 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
270 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
272 case LTTNG_EVENT_TRACEPOINT
:
274 case LTTNG_EVENT_PROBE
:
275 ret
= parse_probe_opts(&ev
, opt_probe
);
277 ERR("Unable to parse probe options");
282 case LTTNG_EVENT_FUNCTION
:
283 ret
= parse_probe_opts(&ev
, opt_function
);
285 ERR("Unable to parse function probe options");
290 case LTTNG_EVENT_FUNCTION_ENTRY
:
291 strncpy(ev
.attr
.ftrace
.symbol_name
,
292 opt_function_entry_symbol
,
293 LTTNG_SYMBOL_NAME_LEN
);
294 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
296 case LTTNG_EVENT_SYSCALL
:
297 MSG("per-syscall selection not supported yet. Use \"-a\" for all syscalls.");
298 ret
= CMD_NOT_IMPLEMENTED
;
301 ret
= CMD_NOT_IMPLEMENTED
;
305 ret
= lttng_enable_event(handle
, &ev
, channel_name
);
307 MSG("Kernel event %s created in channel %s", event_name
, channel_name
);
309 } else if (opt_userspace
) { /* User-space tracer action */
311 * TODO: Waiting on lttng UST 2.0
314 } else if (opt_pid
!= 0) {
316 ret
= CMD_NOT_IMPLEMENTED
;
319 ERR("Please specify a tracer (--kernel or --userspace)");
324 event_name
= strtok(NULL
, ",");
329 if (opt_channel_name
== NULL
) {
332 lttng_destroy_handle(handle
);
340 * Add event to trace session
342 int cmd_enable_events(int argc
, const char **argv
)
345 static poptContext pc
;
346 char *session_name
= NULL
;
348 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
349 poptReadDefaultConfig(pc
, 0);
351 /* Default event type */
352 opt_event_type
= LTTNG_EVENT_ALL
;
354 while ((opt
= poptGetNextOpt(pc
)) != -1) {
362 opt_cmd_name
= poptGetOptArg(pc
);
365 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
368 ret
= CMD_NOT_IMPLEMENTED
;
371 opt_event_type
= LTTNG_EVENT_PROBE
;
372 opt_probe
= poptGetOptArg(pc
);
375 opt_event_type
= LTTNG_EVENT_FUNCTION
;
376 opt_function
= poptGetOptArg(pc
);
378 case OPT_FUNCTION_ENTRY
:
379 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
380 opt_function_entry_symbol
= poptGetOptArg(pc
);
383 opt_event_type
= LTTNG_EVENT_SYSCALL
;
392 opt_event_list
= (char*) poptGetArg(pc
);
393 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
394 ERR("Missing event name(s).\n");
400 if (!opt_session_name
) {
401 session_name
= get_session_name();
402 if (session_name
== NULL
) {
407 session_name
= opt_session_name
;
410 ret
= enable_events(session_name
);
413 if (opt_session_name
== NULL
) {