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
;
58 static struct poptOption long_options
[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
61 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
62 {"all-events", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
63 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
64 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
65 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_USERSPACE
, 0, 0},
66 {"all", 0, POPT_ARG_VAL
, &opt_pid_all
, 1, 0, 0},
67 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
68 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
69 {"marker", 0, POPT_ARG_NONE
, 0, OPT_MARKER
, 0, 0},
70 {"probe", 0, POPT_ARG_STRING
, 0, OPT_PROBE
, 0, 0},
71 {"function", 0, POPT_ARG_STRING
, 0, OPT_FUNCTION
, 0, 0},
72 {"function:entry", 0, POPT_ARG_STRING
, 0, OPT_FUNCTION_ENTRY
, 0, 0},
79 static void usage(FILE *ofp
)
81 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
83 fprintf(ofp
, " -h, --help Show this help\n");
84 fprintf(ofp
, " -s, --session Apply on session name\n");
85 fprintf(ofp
, " -c, --channel Apply on this channel\n");
86 fprintf(ofp
, " -a, --all-events Enable all tracepoints\n");
87 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
88 fprintf(ofp
, " -u, --userspace [CMD] Apply for the user-space tracer\n");
89 fprintf(ofp
, " --all If -u, apply on all traceable apps\n");
90 fprintf(ofp
, " -p, --pid PID If -u, apply on a specific PID\n");
92 fprintf(ofp
, "Event options:\n");
93 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
94 fprintf(ofp
, " --probe [addr | symbol | symbol+offset]\n");
95 fprintf(ofp
, " Dynamic probe.\n");
96 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
97 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
98 fprintf(ofp
, " --function [addr | symbol | symbol+offset]\n");
99 fprintf(ofp
, " Dynamic function entry/return 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:entry symbol\n");
103 fprintf(ofp
, " Function tracer event\n");
104 fprintf(ofp
, " --marker User-space marker (deprecated)\n");
111 * Parse probe options.
113 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
117 char name
[LTTNG_SYMBOL_NAME_LEN
];
124 /* Check for symbol+offset */
125 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
127 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
128 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
129 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
130 if (strlen(s_hex
) == 0) {
131 ERR("Invalid probe offset %s", s_hex
);
135 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
136 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
137 ev
->attr
.probe
.addr
= 0;
141 /* Check for symbol */
142 if (isalpha(name
[0])) {
143 ret
= sscanf(opt
, "%s", name
);
145 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
146 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
147 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
148 ev
->attr
.probe
.offset
= 0;
149 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
150 ev
->attr
.probe
.addr
= 0;
155 /* Check for address */
156 ret
= sscanf(opt
, "%s", s_hex
);
158 if (strlen(s_hex
) == 0) {
159 ERR("Invalid probe address %s", s_hex
);
163 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
164 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
165 ev
->attr
.probe
.offset
= 0;
166 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
180 * Enabling event using the lttng API.
182 static int enable_events(void)
184 int err
, ret
= CMD_SUCCESS
;
185 char *event_name
, *channel_name
= NULL
;
186 struct lttng_event ev
;
187 struct lttng_domain dom
;
189 if (opt_channel_name
== NULL
) {
190 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
196 channel_name
= opt_channel_name
;
199 /* Create lttng domain */
201 dom
.type
= LTTNG_DOMAIN_KERNEL
;
204 if (opt_enable_all
) {
205 if (set_session_name(opt_session_name
) < 0) {
211 ret
= lttng_enable_event(&dom
, NULL
, channel_name
);
213 MSG("All kernel events are enabled in channel %s", channel_name
);
218 /* TODO: User-space tracer */
221 /* Strip event list */
222 event_name
= strtok(opt_event_list
, ",");
223 while (event_name
!= NULL
) {
224 if (set_session_name(opt_session_name
) < 0) {
229 /* Kernel tracer action */
231 DBG("Enabling kernel event %s for channel %s",
232 event_name
, channel_name
);
233 /* Copy name and type of the event */
234 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
235 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
236 ev
.type
= opt_event_type
;
238 switch (opt_event_type
) {
239 case LTTNG_EVENT_TRACEPOINT
:
241 case LTTNG_EVENT_PROBE
:
242 ret
= parse_probe_opts(&ev
, opt_probe
);
244 ERR("Unable to parse probe options");
249 case LTTNG_EVENT_FUNCTION
:
250 ret
= parse_probe_opts(&ev
, opt_function
);
252 ERR("Unable to parse function probe options");
257 case LTTNG_EVENT_FUNCTION_ENTRY
:
258 strncpy(ev
.attr
.ftrace
.symbol_name
,
259 opt_function_entry_symbol
,
260 LTTNG_SYMBOL_NAME_LEN
);
261 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
264 ret
= CMD_NOT_IMPLEMENTED
;
268 ret
= lttng_enable_event(&dom
, &ev
, channel_name
);
270 MSG("Kernel event %s created in channel %s", event_name
, channel_name
);
272 } else if (opt_userspace
) { /* User-space tracer action */
274 * TODO: Waiting on lttng UST 2.0
277 } else if (opt_pid
!= 0) {
279 ret
= CMD_NOT_IMPLEMENTED
;
282 ERR("Please specify a tracer (--kernel or --userspace)");
287 event_name
= strtok(NULL
, ",");
291 if (opt_channel_name
== NULL
) {
300 * Add event to trace session
302 int cmd_enable_events(int argc
, const char **argv
)
305 static poptContext pc
;
307 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
308 poptReadDefaultConfig(pc
, 0);
310 /* Default event type */
311 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
313 while ((opt
= poptGetNextOpt(pc
)) != -1) {
321 opt_cmd_name
= poptGetOptArg(pc
);
324 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
327 ret
= CMD_NOT_IMPLEMENTED
;
330 opt_event_type
= LTTNG_EVENT_PROBE
;
331 opt_probe
= poptGetOptArg(pc
);
334 opt_event_type
= LTTNG_EVENT_FUNCTION
;
335 opt_function
= poptGetOptArg(pc
);
337 case OPT_FUNCTION_ENTRY
:
338 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
339 opt_function_entry_symbol
= poptGetOptArg(pc
);
348 opt_event_list
= (char*) poptGetArg(pc
);
349 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
350 ERR("Missing event name(s).\n");
356 ret
= enable_events();