e435e1f7858f0d15133a31d5ad8004fbd359925b
[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, " --loglevel Tracepoint loglevel\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");
112 #if 0
113 fprintf(ofp, " --function:entry symbol\n");
114 fprintf(ofp, " Function tracer event\n");
115 #endif
116 fprintf(ofp, " --syscall System call event\n");
117 fprintf(ofp, "\n");
118 }
119
120 /*
121 * Parse probe options.
122 */
123 static int parse_probe_opts(struct lttng_event *ev, char *opt)
124 {
125 int ret;
126 char s_hex[19];
127 char name[LTTNG_SYMBOL_NAME_LEN];
128
129 if (opt == NULL) {
130 ret = -1;
131 goto end;
132 }
133
134 /* Check for symbol+offset */
135 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
136 if (ret == 2) {
137 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
138 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
139 DBG("probe symbol %s", ev->attr.probe.symbol_name);
140 if (strlen(s_hex) == 0) {
141 ERR("Invalid probe offset %s", s_hex);
142 ret = -1;
143 goto end;
144 }
145 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
146 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
147 ev->attr.probe.addr = 0;
148 goto end;
149 }
150
151 /* Check for symbol */
152 if (isalpha(name[0])) {
153 ret = sscanf(opt, "%s", name);
154 if (ret == 1) {
155 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
156 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
157 DBG("probe symbol %s", ev->attr.probe.symbol_name);
158 ev->attr.probe.offset = 0;
159 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
160 ev->attr.probe.addr = 0;
161 goto end;
162 }
163 }
164
165 /* Check for address */
166 ret = sscanf(opt, "%s", s_hex);
167 if (ret > 0) {
168 if (strlen(s_hex) == 0) {
169 ERR("Invalid probe address %s", s_hex);
170 ret = -1;
171 goto end;
172 }
173 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
174 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
175 ev->attr.probe.offset = 0;
176 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
177 goto end;
178 }
179
180 /* No match */
181 ret = -1;
182
183 end:
184 return ret;
185 }
186
187 /*
188 * Enabling event using the lttng API.
189 */
190 static int enable_events(char *session_name)
191 {
192 int err, ret = CMD_SUCCESS;
193 char *event_name, *channel_name = NULL;
194 struct lttng_event ev;
195 struct lttng_domain dom;
196
197 if (opt_channel_name == NULL) {
198 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
199 if (err < 0) {
200 ret = CMD_FATAL;
201 goto error;
202 }
203 } else {
204 channel_name = opt_channel_name;
205 }
206
207 if (opt_kernel && opt_userspace) {
208 ERR("Can't use -k/--kernel and -u/--userspace together");
209 ret = CMD_FATAL;
210 goto error;
211 }
212
213 /* Create lttng domain */
214 if (opt_kernel) {
215 dom.type = LTTNG_DOMAIN_KERNEL;
216 } else if (opt_pid != 0) {
217 dom.type = LTTNG_DOMAIN_UST_PID;
218 dom.attr.pid = opt_pid;
219 DBG("PID %d set to lttng handle", opt_pid);
220 } else if (opt_userspace && opt_cmd_name == NULL) {
221 dom.type = LTTNG_DOMAIN_UST;
222 } else if (opt_userspace && opt_cmd_name != NULL) {
223 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
224 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
225 } else {
226 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
227 ret = CMD_NOT_IMPLEMENTED;
228 goto error;
229 }
230
231 handle = lttng_create_handle(session_name, &dom);
232 if (handle == NULL) {
233 ret = -1;
234 goto error;
235 }
236
237 if (opt_enable_all) {
238 /* Default setup for enable all */
239 ev.name[0] = '\0';
240 ev.type = opt_event_type;
241
242 ret = lttng_enable_event(handle, &ev, channel_name);
243 if (ret < 0) {
244 goto error;
245 }
246
247 switch (opt_event_type) {
248 case LTTNG_EVENT_TRACEPOINT:
249 MSG("All %s tracepoints are enabled in channel %s",
250 opt_kernel ? "kernel" : "UST", channel_name);
251 break;
252 case LTTNG_EVENT_SYSCALL:
253 if (opt_kernel) {
254 MSG("All kernel system calls are enabled in channel %s",
255 channel_name);
256 }
257 break;
258 case LTTNG_EVENT_ALL:
259 MSG("All %s events are enabled in channel %s",
260 opt_kernel ? "kernel" : "UST", channel_name);
261 break;
262 default:
263 /*
264 * We should not be here since lttng_enable_event should have
265 * failed on the event type.
266 */
267 goto error;
268 }
269 goto end;
270 }
271
272 /* Strip event list */
273 event_name = strtok(opt_event_list, ",");
274 while (event_name != NULL) {
275 /* Copy name and type of the event */
276 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
277 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
278 ev.type = opt_event_type;
279
280 /* Kernel tracer action */
281 if (opt_kernel) {
282 DBG("Enabling kernel event %s for channel %s",
283 event_name, channel_name);
284
285 switch (opt_event_type) {
286 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
287 ev.type = LTTNG_EVENT_TRACEPOINT;
288 /* Fall-through */
289 case LTTNG_EVENT_TRACEPOINT:
290 break;
291 case LTTNG_EVENT_PROBE:
292 ret = parse_probe_opts(&ev, opt_probe);
293 if (ret < 0) {
294 ERR("Unable to parse probe options");
295 ret = 0;
296 goto error;
297 }
298 break;
299 case LTTNG_EVENT_FUNCTION:
300 ret = parse_probe_opts(&ev, opt_function);
301 if (ret < 0) {
302 ERR("Unable to parse function probe options");
303 ret = 0;
304 goto error;
305 }
306 break;
307 case LTTNG_EVENT_FUNCTION_ENTRY:
308 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
309 LTTNG_SYMBOL_NAME_LEN);
310 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
311 break;
312 case LTTNG_EVENT_SYSCALL:
313 MSG("per-syscall selection not supported yet. Use \"-a\" "
314 "for all syscalls.");
315 default:
316 ret = CMD_NOT_IMPLEMENTED;
317 goto error;
318 }
319 } else if (opt_userspace) { /* User-space tracer action */
320 if (opt_cmd_name != NULL || opt_pid) {
321 MSG("Only supporting tracing all UST processes (-u) for now.");
322 ret = CMD_NOT_IMPLEMENTED;
323 goto error;
324 }
325
326 DBG("Enabling UST event %s for channel %s", event_name,
327 channel_name);
328
329 switch (opt_event_type) {
330 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
331 /* Fall-through */
332 case LTTNG_EVENT_TRACEPOINT:
333 /* Copy name and type of the event */
334 ev.type = LTTNG_EVENT_TRACEPOINT;
335 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
336 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
337 break;
338 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
339 /* Copy name and type of the event */
340 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
341 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
342 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
343 break;
344 case LTTNG_EVENT_PROBE:
345 case LTTNG_EVENT_FUNCTION:
346 case LTTNG_EVENT_FUNCTION_ENTRY:
347 case LTTNG_EVENT_SYSCALL:
348 default:
349 ret = CMD_NOT_IMPLEMENTED;
350 goto error;
351 }
352 } else {
353 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
354 goto error;
355 }
356
357 ret = lttng_enable_event(handle, &ev, channel_name);
358 if (ret == 0) {
359 MSG("%s event %s created in channel %s",
360 opt_kernel ? "kernel": "UST", event_name, channel_name);
361 }
362
363 /* Next event */
364 event_name = strtok(NULL, ",");
365 }
366
367 end:
368 error:
369 if (opt_channel_name == NULL) {
370 free(channel_name);
371 }
372 lttng_destroy_handle(handle);
373
374 return ret;
375 }
376
377 /*
378 * Add event to trace session
379 */
380 int cmd_enable_events(int argc, const char **argv)
381 {
382 int opt, ret;
383 static poptContext pc;
384 char *session_name = NULL;
385
386 pc = poptGetContext(NULL, argc, argv, long_options, 0);
387 poptReadDefaultConfig(pc, 0);
388
389 /* Default event type */
390 opt_event_type = LTTNG_EVENT_ALL;
391
392 while ((opt = poptGetNextOpt(pc)) != -1) {
393 switch (opt) {
394 case OPT_HELP:
395 usage(stderr);
396 ret = CMD_SUCCESS;
397 goto end;
398 case OPT_TRACEPOINT:
399 opt_event_type = LTTNG_EVENT_TRACEPOINT;
400 break;
401 case OPT_PROBE:
402 opt_event_type = LTTNG_EVENT_PROBE;
403 break;
404 case OPT_FUNCTION:
405 opt_event_type = LTTNG_EVENT_FUNCTION;
406 break;
407 case OPT_FUNCTION_ENTRY:
408 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
409 break;
410 case OPT_SYSCALL:
411 opt_event_type = LTTNG_EVENT_SYSCALL;
412 break;
413 case OPT_USERSPACE:
414 opt_userspace = 1;
415 break;
416 case OPT_TRACEPOINT_LOGLEVEL:
417 opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
418 break;
419 default:
420 usage(stderr);
421 ret = CMD_UNDEFINED;
422 goto end;
423 }
424 }
425
426 opt_event_list = (char*) poptGetArg(pc);
427 if (opt_event_list == NULL && opt_enable_all == 0) {
428 ERR("Missing event name(s).\n");
429 usage(stderr);
430 ret = CMD_SUCCESS;
431 goto end;
432 }
433
434 if (!opt_session_name) {
435 session_name = get_session_name();
436 if (session_name == NULL) {
437 ret = -1;
438 goto end;
439 }
440 } else {
441 session_name = opt_session_name;
442 }
443
444 ret = enable_events(session_name);
445
446 end:
447 if (opt_session_name == NULL) {
448 free(session_name);
449 }
450
451 return ret;
452 }
This page took 0.037742 seconds and 3 git commands to generate.