5740d3cbcf95547c72f8d20080da7d8f4318140d
[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 char *opt_kernel;
37 static char *opt_session_name;
38 static int opt_pid_all;
39 static int opt_userspace;
40 static char *opt_cmd_name;
41 static int opt_enable_all;
42 static pid_t opt_pid;
43 static char *opt_probe;
44 static char *opt_function;
45 static char *opt_function_entry_symbol;
46 static char *opt_channel_name;
47
48 enum {
49 OPT_HELP = 1,
50 OPT_TRACEPOINT,
51 OPT_PROBE,
52 OPT_FUNCTION,
53 OPT_FUNCTION_ENTRY,
54 OPT_SYSCALL,
55 OPT_USERSPACE,
56 OPT_TRACEPOINT_LOGLEVEL,
57 };
58
59 static struct lttng_handle *handle;
60
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, &opt_cmd_name, 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 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
73 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
74 #if 0
75 /*
76 * Currently removed from lttng kernel tracer. Removed from
77 * lttng UI to discourage its use.
78 */
79 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
80 #endif
81 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
82 {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0},
83 {0, 0, 0, 0, 0, 0, 0}
84 };
85
86 /*
87 * usage
88 */
89 static void usage(FILE *ofp)
90 {
91 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
92 fprintf(ofp, "\n");
93 fprintf(ofp, " -h, --help Show this help\n");
94 fprintf(ofp, " -s, --session Apply on session name\n");
95 fprintf(ofp, " -c, --channel Apply on this channel\n");
96 fprintf(ofp, " -a, --all-events Enable all tracepoints\n");
97 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
98 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
99 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
100 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
101 fprintf(ofp, "\n");
102 fprintf(ofp, "Event options:\n");
103 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
104 fprintf(ofp, " --loglevel Tracepoint loglevel\n");
105 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
106 fprintf(ofp, " Dynamic probe.\n");
107 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
108 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
109 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
110 fprintf(ofp, " Dynamic function entry/return probe.\n");
111 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
112 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
113 #if 0
114 fprintf(ofp, " --function:entry symbol\n");
115 fprintf(ofp, " Function tracer event\n");
116 #endif
117 fprintf(ofp, " --syscall System call event\n");
118 fprintf(ofp, "\n");
119 }
120
121 /*
122 * parse_probe_addr
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 * enable_events
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 MSG("Choose only one of --kernel or --userspace");
214 ret = CMD_FATAL;
215 goto error;
216 }
217 /* Create lttng domain */
218 if (opt_kernel) {
219 dom.type = LTTNG_DOMAIN_KERNEL;
220 }
221 if (opt_userspace) {
222 /* TODO
223 * LTTNG_DOMAIN_UST_EXEC_NAME,
224 * LTTNG_DOMAIN_UST_PID,
225 * LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
226 */
227 dom.type = LTTNG_DOMAIN_UST;
228 }
229
230 handle = lttng_create_handle(session_name, &dom);
231 if (handle == NULL) {
232 ret = -1;
233 goto error;
234 }
235
236 if (opt_enable_all) {
237 /* Default setup for enable all */
238 ev.name[0] = '\0';
239 ev.type = opt_event_type;
240
241 ret = lttng_enable_event(handle, &ev, channel_name);
242 if (ret < 0) {
243 goto error;
244 }
245
246 switch (opt_event_type) {
247 case LTTNG_EVENT_TRACEPOINT:
248 MSG("All %s tracepoints are enabled in channel %s",
249 opt_kernel ? "kernel" : "UST", channel_name);
250 break;
251 case LTTNG_EVENT_SYSCALL:
252 if (opt_kernel) {
253 MSG("All kernel system calls are enabled in channel %s",
254 channel_name);
255 }
256 break;
257 case LTTNG_EVENT_ALL:
258 MSG("All %s events are enabled in channel %s",
259 opt_kernel ? "kernel" : "UST", channel_name);
260 break;
261 default:
262 /*
263 * We should not be here since
264 * lttng_enable_event should have failed on the
265 * 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 /* Kernel tracer action */
276 if (opt_kernel) {
277 DBG("Enabling kernel event %s for channel %s",
278 event_name, channel_name);
279 /* Copy name and type of the event */
280 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
281 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
282 ev.type = opt_event_type;
283
284 switch (opt_event_type) {
285 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
286 ev.type = LTTNG_EVENT_TRACEPOINT;
287 /* Fall-through */
288 case LTTNG_EVENT_TRACEPOINT:
289 break;
290 case LTTNG_EVENT_PROBE:
291 ret = parse_probe_opts(&ev, opt_probe);
292 if (ret < 0) {
293 ERR("Unable to parse probe options");
294 ret = 0;
295 goto error;
296 }
297 break;
298 case LTTNG_EVENT_FUNCTION:
299 ret = parse_probe_opts(&ev, opt_function);
300 if (ret < 0) {
301 ERR("Unable to parse function probe options");
302 ret = 0;
303 goto error;
304 }
305 break;
306 case LTTNG_EVENT_FUNCTION_ENTRY:
307 strncpy(ev.attr.ftrace.symbol_name,
308 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\" for all syscalls.");
314 ret = CMD_NOT_IMPLEMENTED;
315 goto error;
316 default:
317 ret = CMD_NOT_IMPLEMENTED;
318 goto error;
319 }
320
321 ret = lttng_enable_event(handle, &ev, channel_name);
322 if (ret == 0) {
323 MSG("Kernel event %s created in channel %s", event_name, channel_name);
324 }
325 } else if (opt_userspace) { /* User-space tracer action */
326 /*
327 * TODO: only supporting pid_all tracing for
328 * now. Should have different domain based on
329 * opt_pid.
330 */
331 if (!opt_pid_all) {
332 MSG("Only supporting tracing all UST processes (-u --all) for now.");
333 ret = CMD_NOT_IMPLEMENTED;
334 goto error;
335 }
336 DBG("Enabling UST event %s for channel %s",
337 event_name, channel_name);
338 /* Copy name and type of the event */
339 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
340 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
341 ev.type = opt_event_type;
342
343 switch (opt_event_type) {
344 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
345 ev.type = LTTNG_EVENT_TRACEPOINT;
346 /* Fall-through */
347 case LTTNG_EVENT_TRACEPOINT:
348 break;
349 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
350 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
351 break;
352 case LTTNG_EVENT_PROBE:
353 case LTTNG_EVENT_FUNCTION:
354 case LTTNG_EVENT_FUNCTION_ENTRY:
355 case LTTNG_EVENT_SYSCALL:
356 default:
357 ret = CMD_NOT_IMPLEMENTED;
358 goto error;
359 }
360
361 ret = lttng_enable_event(handle, &ev, channel_name);
362 if (ret == 0) {
363 MSG("UST event %s created in channel %s", event_name, channel_name);
364 }
365 } else {
366 ERR("Please specify a tracer (--kernel or --userspace)");
367 goto error;
368 }
369
370 /* Next event */
371 event_name = strtok(NULL, ",");
372 }
373
374 end:
375 error:
376 if (opt_channel_name == NULL) {
377 free(channel_name);
378 }
379 lttng_destroy_handle(handle);
380
381 return ret;
382 }
383
384 /*
385 * cmd_enable_events
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.059129 seconds and 4 git commands to generate.