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