ca18ccd4847edfea7407f02a425456c3149ce1ba
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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
7 * of the License.
8 *
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.
13 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <ctype.h>
29
30 #include "../command.h"
31
32 static char *opt_event_list;
33 static int opt_event_type;
34 static int opt_kernel;
35 static char *opt_session_name;
36 static int opt_userspace;
37 static char *opt_cmd_name;
38 static int opt_enable_all;
39 static pid_t opt_pid;
40 static char *opt_probe;
41 static char *opt_function;
42 static char *opt_function_entry_symbol;
43 static char *opt_channel_name;
44
45 enum {
46 OPT_HELP = 1,
47 OPT_TRACEPOINT,
48 OPT_PROBE,
49 OPT_FUNCTION,
50 OPT_FUNCTION_ENTRY,
51 OPT_SYSCALL,
52 OPT_USERSPACE,
53 OPT_TRACEPOINT_LOGLEVEL,
54 };
55
56 static struct lttng_handle *handle;
57
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", '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, &opt_cmd_name, OPT_USERSPACE, 0, 0},
66 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
67 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
68 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
69 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
70 #if 0
71 /*
72 * Currently removed from lttng kernel tracer. Removed from
73 * lttng UI to discourage its use.
74 */
75 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
76 #endif
77 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
78 {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0},
79 {0, 0, 0, 0, 0, 0, 0}
80 };
81
82 /*
83 * usage
84 */
85 static void usage(FILE *ofp)
86 {
87 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
88 fprintf(ofp, "\n");
89 fprintf(ofp, " -h, --help Show this help\n");
90 fprintf(ofp, " -s, --session Apply on session name\n");
91 fprintf(ofp, " -c, --channel Apply on this channel\n");
92 fprintf(ofp, " -a, --all Enable all tracepoints\n");
93 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
94 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
95 fprintf(ofp, " If no CMD, the domain used is UST global\n");
96 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
97 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
98 fprintf(ofp, "\n");
99 fprintf(ofp, "Event options:\n");
100 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
101 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
102 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
103 fprintf(ofp, " e.g.:\n");
104 fprintf(ofp, " \"*\"\n");
105 fprintf(ofp, " \"app_component:na*\"\n");
106 fprintf(ofp, " --loglevel Tracepoint loglevel\n");
107 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
108 fprintf(ofp, " Dynamic probe.\n");
109 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
110 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
111 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
112 fprintf(ofp, " Dynamic function entry/return probe.\n");
113 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
114 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
115 #if 0
116 fprintf(ofp, " --function:entry symbol\n");
117 fprintf(ofp, " Function tracer event\n");
118 #endif
119 fprintf(ofp, " --syscall System call event\n");
120 fprintf(ofp, "\n");
121 }
122
123 /*
124 * Parse probe options.
125 */
126 static int parse_probe_opts(struct lttng_event *ev, char *opt)
127 {
128 int ret;
129 char s_hex[19];
130 char name[LTTNG_SYMBOL_NAME_LEN];
131
132 if (opt == NULL) {
133 ret = -1;
134 goto end;
135 }
136
137 /* Check for symbol+offset */
138 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
139 if (ret == 2) {
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);
145 ret = -1;
146 goto end;
147 }
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;
151 goto end;
152 }
153
154 /* Check for symbol */
155 if (isalpha(name[0])) {
156 ret = sscanf(opt, "%s", name);
157 if (ret == 1) {
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;
164 goto end;
165 }
166 }
167
168 /* Check for address */
169 ret = sscanf(opt, "%s", s_hex);
170 if (ret > 0) {
171 if (strlen(s_hex) == 0) {
172 ERR("Invalid probe address %s", s_hex);
173 ret = -1;
174 goto end;
175 }
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);
180 goto end;
181 }
182
183 /* No match */
184 ret = -1;
185
186 end:
187 return ret;
188 }
189
190 /*
191 * Enabling event using the lttng API.
192 */
193 static int enable_events(char *session_name)
194 {
195 int err, ret = CMD_SUCCESS;
196 char *event_name, *channel_name = NULL;
197 struct lttng_event ev;
198 struct lttng_domain dom;
199
200 if (opt_channel_name == NULL) {
201 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
202 if (err < 0) {
203 ret = CMD_FATAL;
204 goto error;
205 }
206 } else {
207 channel_name = opt_channel_name;
208 }
209
210 if (opt_kernel && opt_userspace) {
211 ERR("Can't use -k/--kernel and -u/--userspace together");
212 ret = CMD_FATAL;
213 goto error;
214 }
215
216 /* Create lttng domain */
217 if (opt_kernel) {
218 dom.type = LTTNG_DOMAIN_KERNEL;
219 } else if (opt_pid != 0) {
220 dom.type = LTTNG_DOMAIN_UST_PID;
221 dom.attr.pid = opt_pid;
222 DBG("PID %d set to lttng handle", opt_pid);
223 } else if (opt_userspace && opt_cmd_name == NULL) {
224 dom.type = LTTNG_DOMAIN_UST;
225 } else if (opt_userspace && opt_cmd_name != NULL) {
226 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
227 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
228 } else {
229 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
230 ret = CMD_NOT_IMPLEMENTED;
231 goto error;
232 }
233
234 handle = lttng_create_handle(session_name, &dom);
235 if (handle == NULL) {
236 ret = -1;
237 goto error;
238 }
239
240 if (opt_enable_all) {
241 /* Default setup for enable all */
242
243 if (opt_kernel) {
244 ev.type = opt_event_type;
245 ev.name[0] = '\0';
246 } else {
247 ev.type = LTTNG_EVENT_TRACEPOINT;
248 strcpy(ev.name, "*");
249 }
250
251 ret = lttng_enable_event(handle, &ev, channel_name);
252 if (ret < 0) {
253 goto error;
254 }
255
256 switch (opt_event_type) {
257 case LTTNG_EVENT_TRACEPOINT:
258 MSG("All %s tracepoints are enabled in channel %s",
259 opt_kernel ? "kernel" : "UST", channel_name);
260 break;
261 case LTTNG_EVENT_SYSCALL:
262 if (opt_kernel) {
263 MSG("All kernel system calls are enabled in channel %s",
264 channel_name);
265 }
266 break;
267 case LTTNG_EVENT_ALL:
268 MSG("All %s events are enabled in channel %s",
269 opt_kernel ? "kernel" : "UST", channel_name);
270 break;
271 default:
272 /*
273 * We should not be here since lttng_enable_event should have
274 * failed on the event type.
275 */
276 goto error;
277 }
278 goto end;
279 }
280
281 /* Strip event list */
282 event_name = strtok(opt_event_list, ",");
283 while (event_name != NULL) {
284 /* Copy name and type of the event */
285 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
286 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
287 ev.type = opt_event_type;
288
289 /* Kernel tracer action */
290 if (opt_kernel) {
291 DBG("Enabling kernel event %s for channel %s",
292 event_name, channel_name);
293
294 switch (opt_event_type) {
295 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
296 ev.type = LTTNG_EVENT_TRACEPOINT;
297 /* Fall-through */
298 case LTTNG_EVENT_TRACEPOINT:
299 break;
300 case LTTNG_EVENT_PROBE:
301 ret = parse_probe_opts(&ev, opt_probe);
302 if (ret < 0) {
303 ERR("Unable to parse probe options");
304 ret = 0;
305 goto error;
306 }
307 break;
308 case LTTNG_EVENT_FUNCTION:
309 ret = parse_probe_opts(&ev, opt_function);
310 if (ret < 0) {
311 ERR("Unable to parse function probe options");
312 ret = 0;
313 goto error;
314 }
315 break;
316 case LTTNG_EVENT_FUNCTION_ENTRY:
317 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
318 LTTNG_SYMBOL_NAME_LEN);
319 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
320 break;
321 case LTTNG_EVENT_SYSCALL:
322 MSG("per-syscall selection not supported yet. Use \"-a\" "
323 "for all syscalls.");
324 default:
325 ret = CMD_NOT_IMPLEMENTED;
326 goto error;
327 }
328 } else if (opt_userspace) { /* User-space tracer action */
329 if (opt_cmd_name != NULL || opt_pid) {
330 MSG("Only supporting tracing all UST processes (-u) for now.");
331 ret = CMD_NOT_IMPLEMENTED;
332 goto error;
333 }
334
335 DBG("Enabling UST event %s for channel %s", event_name,
336 channel_name);
337
338 switch (opt_event_type) {
339 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
340 /* Fall-through */
341 case LTTNG_EVENT_TRACEPOINT:
342 /* Copy name and type of the event */
343 ev.type = LTTNG_EVENT_TRACEPOINT;
344 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
345 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
346 break;
347 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
348 /* Copy name and type of the event */
349 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
350 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
351 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
352 break;
353 case LTTNG_EVENT_PROBE:
354 case LTTNG_EVENT_FUNCTION:
355 case LTTNG_EVENT_FUNCTION_ENTRY:
356 case LTTNG_EVENT_SYSCALL:
357 default:
358 ret = CMD_NOT_IMPLEMENTED;
359 goto error;
360 }
361 } else {
362 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
363 goto error;
364 }
365
366 ret = lttng_enable_event(handle, &ev, channel_name);
367 if (ret == 0) {
368 MSG("%s event %s created in channel %s",
369 opt_kernel ? "kernel": "UST", event_name, channel_name);
370 }
371
372 /* Next event */
373 event_name = strtok(NULL, ",");
374 }
375
376 end:
377 error:
378 if (opt_channel_name == NULL) {
379 free(channel_name);
380 }
381 lttng_destroy_handle(handle);
382
383 return ret;
384 }
385
386 /*
387 * Add event to trace session
388 */
389 int cmd_enable_events(int argc, const char **argv)
390 {
391 int opt, ret;
392 static poptContext pc;
393 char *session_name = NULL;
394
395 pc = poptGetContext(NULL, argc, argv, long_options, 0);
396 poptReadDefaultConfig(pc, 0);
397
398 /* Default event type */
399 opt_event_type = LTTNG_EVENT_ALL;
400
401 while ((opt = poptGetNextOpt(pc)) != -1) {
402 switch (opt) {
403 case OPT_HELP:
404 usage(stderr);
405 ret = CMD_SUCCESS;
406 goto end;
407 case OPT_TRACEPOINT:
408 opt_event_type = LTTNG_EVENT_TRACEPOINT;
409 break;
410 case OPT_PROBE:
411 opt_event_type = LTTNG_EVENT_PROBE;
412 break;
413 case OPT_FUNCTION:
414 opt_event_type = LTTNG_EVENT_FUNCTION;
415 break;
416 case OPT_FUNCTION_ENTRY:
417 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
418 break;
419 case OPT_SYSCALL:
420 opt_event_type = LTTNG_EVENT_SYSCALL;
421 break;
422 case OPT_USERSPACE:
423 opt_userspace = 1;
424 break;
425 case OPT_TRACEPOINT_LOGLEVEL:
426 opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
427 break;
428 default:
429 usage(stderr);
430 ret = CMD_UNDEFINED;
431 goto end;
432 }
433 }
434
435 opt_event_list = (char*) poptGetArg(pc);
436 if (opt_event_list == NULL && opt_enable_all == 0) {
437 ERR("Missing event name(s).\n");
438 usage(stderr);
439 ret = CMD_SUCCESS;
440 goto end;
441 }
442
443 if (!opt_session_name) {
444 session_name = get_session_name();
445 if (session_name == NULL) {
446 ret = -1;
447 goto end;
448 }
449 } else {
450 session_name = opt_session_name;
451 }
452
453 ret = enable_events(session_name);
454
455 end:
456 if (opt_session_name == NULL) {
457 free(session_name);
458 }
459
460 return ret;
461 }
This page took 0.038688 seconds and 3 git commands to generate.