Implement filter bytecode support in lttng-session, and parse filter string
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f3ed775e
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
5a0de755 26#include <inttypes.h>
8f0d098b 27#include <ctype.h>
f3ed775e 28
c399183f 29#include "../command.h"
42224349 30#include <src/common/sessiond-comm/sessiond-comm.h>
f3ed775e
DG
31
32static char *opt_event_list;
33static int opt_event_type;
0cda4f28
MD
34static const char *opt_loglevel;
35static int opt_loglevel_type;
6181537c 36static int opt_kernel;
5440dc42 37static char *opt_session_name;
f3ed775e
DG
38static int opt_userspace;
39static int opt_enable_all;
cf0e5467 40static char *opt_probe;
8f0d098b
MD
41static char *opt_function;
42static char *opt_function_entry_symbol;
f3ed775e 43static char *opt_channel_name;
53a80697 44static char *opt_filter;
d78d6610
DG
45#if 0
46/* Not implemented yet */
47static char *opt_cmd_name;
48static pid_t opt_pid;
49#endif
f3ed775e
DG
50
51enum {
52 OPT_HELP = 1,
f3ed775e 53 OPT_TRACEPOINT,
cf0e5467 54 OPT_PROBE,
f3ed775e 55 OPT_FUNCTION,
8f0d098b 56 OPT_FUNCTION_ENTRY,
a54bd42d 57 OPT_SYSCALL,
eeac7d46 58 OPT_USERSPACE,
0cda4f28
MD
59 OPT_LOGLEVEL,
60 OPT_LOGLEVEL_ONLY,
679b4943 61 OPT_LIST_OPTIONS,
53a80697 62 OPT_FILTER,
f3ed775e
DG
63};
64
cd80958d
DG
65static struct lttng_handle *handle;
66
f3ed775e
DG
67static struct poptOption long_options[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 70 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 71 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
72 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
73 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
74#if 0
75 /* Not implemented yet */
40e9d5d3 76 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
f3ed775e 77 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
78#else
79 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
80#endif
f3ed775e 81 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 82 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 83 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
84#if 0
85 /*
86 * Currently removed from lttng kernel tracer. Removed from
87 * lttng UI to discourage its use.
88 */
40e9d5d3 89 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 90#endif
7ebae521 91 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
92 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
93 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 94 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 95 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
f3ed775e
DG
96 {0, 0, 0, 0, 0, 0, 0}
97};
98
99/*
100 * usage
101 */
102static void usage(FILE *ofp)
103{
104 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
105 fprintf(ofp, "\n");
106 fprintf(ofp, " -h, --help Show this help\n");
679b4943 107 fprintf(ofp, " --list-options Simple listing of options\n");
27221701
DG
108 fprintf(ofp, " -s, --session Apply to session name\n");
109 fprintf(ofp, " -c, --channel Apply to this channel\n");
e08bff8d 110 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
f3ed775e 111 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
d78d6610 112#if 0
27221701 113 fprintf(ofp, " -u, --userspace [CMD] Apply to the user-space tracer\n");
e14f64a8
DG
114 fprintf(ofp, " If no CMD, the domain used is UST global\n");
115 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
116 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
d78d6610 117#else
27221701 118 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
d78d6610 119#endif
f3ed775e
DG
120 fprintf(ofp, "\n");
121 fprintf(ofp, "Event options:\n");
122 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
be49af1e
MD
123 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
124 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
125 fprintf(ofp, " e.g.:\n");
126 fprintf(ofp, " \"*\"\n");
127 fprintf(ofp, " \"app_component:na*\"\n");
8f0d098b 128 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
129 fprintf(ofp, " Dynamic probe.\n");
130 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
131 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
132 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
133 fprintf(ofp, " Dynamic function entry/return probe.\n");
134 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
135 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 136#if 0
8f0d098b
MD
137 fprintf(ofp, " --function:entry symbol\n");
138 fprintf(ofp, " Function tracer event\n");
b213d387 139#endif
a54bd42d 140 fprintf(ofp, " --syscall System call event\n");
f3ed775e 141 fprintf(ofp, "\n");
300b8fd5
MD
142 fprintf(ofp, " --loglevel name\n");
143 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n");
144 fprintf(ofp, " --loglevel-only name\n");
145 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
146 fprintf(ofp, "\n");
0c8477c8
MD
147 fprintf(ofp, " The loglevel or loglevel-only options should be\n");
148 fprintf(ofp, " combined with a tracepoint name or tracepoint\n");
149 fprintf(ofp, " wildcard.\n");
150 fprintf(ofp, " Available loglevels:\n");
151 fprintf(ofp, " (higher value is more verbose)\n");
46839cc2
MD
152 fprintf(ofp, " TRACE_EMERG = 0\n");
153 fprintf(ofp, " TRACE_ALERT = 1\n");
154 fprintf(ofp, " TRACE_CRIT = 2\n");
155 fprintf(ofp, " TRACE_ERR = 3\n");
156 fprintf(ofp, " TRACE_WARNING = 4\n");
157 fprintf(ofp, " TRACE_NOTICE = 5\n");
158 fprintf(ofp, " TRACE_INFO = 6\n");
159 fprintf(ofp, " TRACE_DEBUG_SYSTEM = 7\n");
160 fprintf(ofp, " TRACE_DEBUG_PROGRAM = 8\n");
161 fprintf(ofp, " TRACE_DEBUG_PROCESS = 9\n");
162 fprintf(ofp, " TRACE_DEBUG_MODULE = 10\n");
163 fprintf(ofp, " TRACE_DEBUG_UNIT = 11\n");
164 fprintf(ofp, " TRACE_DEBUG_FUNCTION = 12\n");
165 fprintf(ofp, " TRACE_DEBUG_LINE = 13\n");
166 fprintf(ofp, " TRACE_DEBUG = 14\n");
167 fprintf(ofp, " (shortcuts such as \"system\" are allowed)\n");
53a80697
MD
168 fprintf(ofp, " --filter \'expression\'\n");
169 fprintf(ofp, " Filter expression on event fields,\n");
170 fprintf(ofp, " event recording depends on evaluation.\n");
300b8fd5 171 fprintf(ofp, "\n");
f3ed775e
DG
172}
173
0d63dd19 174/*
6181537c 175 * Parse probe options.
0d63dd19 176 */
cf0e5467 177static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
178{
179 int ret;
8ff0bbd0 180 char s_hex[19];
0d63dd19
DG
181 char name[LTTNG_SYMBOL_NAME_LEN];
182
183 if (opt == NULL) {
184 ret = -1;
8f0d098b 185 goto end;
0d63dd19
DG
186 }
187
188 /* Check for symbol+offset */
8ff0bbd0 189 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 190 if (ret == 2) {
7d29a247 191 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 192 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 193 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
194 if (strlen(s_hex) == 0) {
195 ERR("Invalid probe offset %s", s_hex);
0d63dd19 196 ret = -1;
8f0d098b 197 goto end;
0d63dd19 198 }
8ff0bbd0 199 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 200 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 201 ev->attr.probe.addr = 0;
8f0d098b
MD
202 goto end;
203 }
204
205 /* Check for symbol */
206 if (isalpha(name[0])) {
207 ret = sscanf(opt, "%s", name);
208 if (ret == 1) {
209 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 210 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
211 DBG("probe symbol %s", ev->attr.probe.symbol_name);
212 ev->attr.probe.offset = 0;
213 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
214 ev->attr.probe.addr = 0;
215 goto end;
216 }
0d63dd19
DG
217 }
218
219 /* Check for address */
8ff0bbd0 220 ret = sscanf(opt, "%s", s_hex);
0d63dd19 221 if (ret > 0) {
8ff0bbd0
DG
222 if (strlen(s_hex) == 0) {
223 ERR("Invalid probe address %s", s_hex);
0d63dd19 224 ret = -1;
8f0d098b 225 goto end;
0d63dd19 226 }
8ff0bbd0 227 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 228 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
229 ev->attr.probe.offset = 0;
230 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 231 goto end;
0d63dd19
DG
232 }
233
234 /* No match */
235 ret = -1;
236
8f0d098b 237end:
0d63dd19
DG
238 return ret;
239}
240
8005f29a 241/*
0c8477c8 242 * Maps loglevel from string to value
8005f29a
MD
243 */
244static
46839cc2 245int loglevel_str_to_value(const char *inputstr)
8005f29a 246{
46839cc2
MD
247 int i = 0;
248 char str[LTTNG_SYMBOL_NAME_LEN];
249
250 while (inputstr[i] != '\0' && i < LTTNG_SYMBOL_NAME_LEN) {
251 str[i] = toupper(inputstr[i]);
252 i++;
253 }
254 str[i] = '\0';
255 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
256 return LTTNG_LOGLEVEL_EMERG;
257 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
258 return LTTNG_LOGLEVEL_ALERT;
259 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
260 return LTTNG_LOGLEVEL_CRIT;
261 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
262 return LTTNG_LOGLEVEL_ERR;
263 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
264 return LTTNG_LOGLEVEL_WARNING;
265 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
266 return LTTNG_LOGLEVEL_NOTICE;
267 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
268 return LTTNG_LOGLEVEL_INFO;
269 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
270 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
271 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
272 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
273 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
274 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
275 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
276 return LTTNG_LOGLEVEL_DEBUG_MODULE;
277 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
278 return LTTNG_LOGLEVEL_DEBUG_UNIT;
279 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
280 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
281 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
282 return LTTNG_LOGLEVEL_DEBUG_LINE;
283 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
284 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
285 } else {
286 return -1;
287 }
288}
289
f3ed775e 290/*
6181537c 291 * Enabling event using the lttng API.
f3ed775e 292 */
cd80958d 293static int enable_events(char *session_name)
f3ed775e 294{
ae856491 295 int err, ret = CMD_SUCCESS, warn = 0;
b73d0b29 296 char *event_name, *channel_name = NULL;
f3ed775e 297 struct lttng_event ev;
7d29a247 298 struct lttng_domain dom;
f3ed775e 299
441c16a7
MD
300 memset(&ev, 0, sizeof(ev));
301 memset(&dom, 0, sizeof(dom));
302
53a80697
MD
303 if (opt_kernel) {
304 if (opt_filter) {
305 ERR("Filter not implement for kernel tracing yet");
306 ret = CMD_ERROR;
307 goto error;
308 }
309 }
310
7d29a247
DG
311 /* Create lttng domain */
312 if (opt_kernel) {
313 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 314 } else if (opt_userspace) {
2bdd86d4 315 dom.type = LTTNG_DOMAIN_UST;
6181537c 316 } else {
e14f64a8 317 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 318 ret = CMD_ERROR;
6181537c 319 goto error;
2bdd86d4 320 }
7d29a247 321
ae856491
DG
322 if (opt_channel_name == NULL) {
323 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
324 if (err < 0) {
325 ret = CMD_FATAL;
326 goto error;
327 }
328 } else {
329 channel_name = opt_channel_name;
330 }
331
cd80958d
DG
332 handle = lttng_create_handle(session_name, &dom);
333 if (handle == NULL) {
334 ret = -1;
335 goto error;
336 }
1aef21b6 337
cd80958d 338 if (opt_enable_all) {
8c9ae521 339 /* Default setup for enable all */
75e8c5ab
MD
340 if (opt_kernel) {
341 ev.type = opt_event_type;
342 ev.name[0] = '\0';
300b8fd5
MD
343 /* kernel loglevels not implemented */
344 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
345 } else {
346 ev.type = LTTNG_EVENT_TRACEPOINT;
347 strcpy(ev.name, "*");
300b8fd5
MD
348 ev.loglevel_type = opt_loglevel_type;
349 if (opt_loglevel) {
350 ev.loglevel = loglevel_str_to_value(opt_loglevel);
351 if (ev.loglevel == -1) {
352 ERR("Unknown loglevel %s", opt_loglevel);
353 ret = -1;
354 goto error;
355 }
22e25b71
MD
356 } else {
357 ev.loglevel = -1;
300b8fd5 358 }
75e8c5ab 359 }
8c9ae521
DG
360
361 ret = lttng_enable_event(handle, &ev, channel_name);
362 if (ret < 0) {
42224349
DG
363 switch (-ret) {
364 case LTTCOMM_KERN_EVENT_EXIST:
365 WARN("Kernel events already enabled (channel %s, session %s)",
366 channel_name, session_name);
6d805133 367 break;
42224349 368 default:
a6de644c 369 ERR("Events: %s (channel %s, session %s)",
42224349
DG
370 lttng_strerror(ret), channel_name, session_name);
371 break;
372 }
6d805133 373 goto end;
8c9ae521 374 }
53a80697
MD
375 if (opt_filter) {
376 ret = lttng_set_event_filter(handle, ev.name, channel_name,
377 opt_filter);
378 if (ret < 0) {
379 ERR("Error setting filter");
380 ret = -1;
381 goto error;
382 }
383 }
8c9ae521 384
0133c199
MD
385 switch (opt_event_type) {
386 case LTTNG_EVENT_TRACEPOINT:
57064ada
MD
387 if (opt_loglevel) {
388 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
389 opt_kernel ? "kernel" : "UST", channel_name,
390 opt_loglevel);
391 } else {
392 MSG("All %s tracepoints are enabled in channel %s",
393 opt_kernel ? "kernel" : "UST", channel_name);
394
395 }
0133c199 396 break;
a54bd42d 397 case LTTNG_EVENT_SYSCALL:
0133c199 398 if (opt_kernel) {
8c9ae521
DG
399 MSG("All kernel system calls are enabled in channel %s",
400 channel_name);
c93d9daf 401 }
0133c199 402 break;
7a3d1328 403 case LTTNG_EVENT_ALL:
57064ada
MD
404 if (opt_loglevel) {
405 MSG("All %s events are enabled in channel %s for loglevel %s",
406 opt_kernel ? "kernel" : "UST", channel_name,
407 opt_loglevel);
408 } else {
409 MSG("All %s events are enabled in channel %s",
410 opt_kernel ? "kernel" : "UST", channel_name);
411 }
7a3d1328 412 break;
0133c199 413 default:
8c9ae521 414 /*
6181537c
DG
415 * We should not be here since lttng_enable_event should have
416 * failed on the event type.
8c9ae521 417 */
f3ed775e
DG
418 goto error;
419 }
8c9ae521 420 goto end;
f3ed775e
DG
421 }
422
423 /* Strip event list */
424 event_name = strtok(opt_event_list, ",");
425 while (event_name != NULL) {
6181537c
DG
426 /* Copy name and type of the event */
427 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
428 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
429 ev.type = opt_event_type;
430
f3ed775e
DG
431 /* Kernel tracer action */
432 if (opt_kernel) {
433 DBG("Enabling kernel event %s for channel %s",
434 event_name, channel_name);
f3ed775e
DG
435
436 switch (opt_event_type) {
7a3d1328
MD
437 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
438 ev.type = LTTNG_EVENT_TRACEPOINT;
439 /* Fall-through */
e6ddca71 440 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 441 break;
7d29a247 442 case LTTNG_EVENT_PROBE:
cf0e5467 443 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 444 if (ret < 0) {
cf0e5467 445 ERR("Unable to parse probe options");
0d63dd19
DG
446 ret = 0;
447 goto error;
448 }
f3ed775e
DG
449 break;
450 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
451 ret = parse_probe_opts(&ev, opt_function);
452 if (ret < 0) {
453 ERR("Unable to parse function probe options");
454 ret = 0;
455 goto error;
456 }
457 break;
458 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
459 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
460 LTTNG_SYMBOL_NAME_LEN);
99497cd0 461 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 462 break;
a54bd42d 463 case LTTNG_EVENT_SYSCALL:
6181537c
DG
464 MSG("per-syscall selection not supported yet. Use \"-a\" "
465 "for all syscalls.");
f3ed775e 466 default:
1ab1ea0b 467 ret = CMD_UNDEFINED;
f3ed775e
DG
468 goto error;
469 }
0cda4f28 470
ed7f4083 471 if (opt_loglevel) {
0cda4f28
MD
472 MSG("Kernel loglevels are not supported.");
473 ret = CMD_UNDEFINED;
474 goto error;
475 }
476
477 /* kernel loglevels not implemented */
8005f29a 478 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 479 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 480#if 0
e14f64a8
DG
481 if (opt_cmd_name != NULL || opt_pid) {
482 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 483 ret = CMD_UNDEFINED;
2bdd86d4
MD
484 goto error;
485 }
d78d6610 486#endif
6181537c 487
300b8fd5
MD
488 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
489 channel_name, opt_loglevel ? : "<all>");
2bdd86d4
MD
490
491 switch (opt_event_type) {
492 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
493 /* Fall-through */
494 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
495 /* Copy name and type of the event */
496 ev.type = LTTNG_EVENT_TRACEPOINT;
497 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
498 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
499 break;
500 case LTTNG_EVENT_PROBE:
501 case LTTNG_EVENT_FUNCTION:
502 case LTTNG_EVENT_FUNCTION_ENTRY:
503 case LTTNG_EVENT_SYSCALL:
504 default:
cc62c0c0 505 ERR("Event type not available for user-space tracing");
1ab1ea0b 506 ret = CMD_UNDEFINED;
2bdd86d4
MD
507 goto error;
508 }
0cda4f28
MD
509
510 ev.loglevel_type = opt_loglevel_type;
ed7f4083 511 if (opt_loglevel) {
8005f29a
MD
512 ev.loglevel = loglevel_str_to_value(opt_loglevel);
513 if (ev.loglevel == -1) {
514 ERR("Unknown loglevel %s", opt_loglevel);
300b8fd5 515 ret = -1;
8005f29a
MD
516 goto error;
517 }
22e25b71
MD
518 } else {
519 ev.loglevel = -1;
ed7f4083 520 }
f3ed775e 521 } else {
e14f64a8 522 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
300b8fd5 523 ret = CMD_ERROR;
f3ed775e
DG
524 goto error;
525 }
526
6181537c 527 ret = lttng_enable_event(handle, &ev, channel_name);
ae856491 528 if (ret < 0) {
42224349
DG
529 /* Turn ret to positive value to handle the positive error code */
530 switch (-ret) {
531 case LTTCOMM_KERN_EVENT_EXIST:
532 WARN("Kernel event %s already enabled (channel %s, session %s)",
533 event_name, channel_name, session_name);
534 break;
535 default:
536 ERR("Event %s: %s (channel %s, session %s)", event_name,
537 lttng_strerror(ret), channel_name, session_name);
538 break;
539 }
ae856491
DG
540 warn = 1;
541 } else {
6181537c
DG
542 MSG("%s event %s created in channel %s",
543 opt_kernel ? "kernel": "UST", event_name, channel_name);
544 }
53a80697
MD
545 if (opt_filter) {
546 ret = lttng_set_event_filter(handle, ev.name,
547 channel_name, opt_filter);
548 if (ret < 0) {
549 ERR("Error setting filter");
550 ret = -1;
551 goto error;
552 }
553 }
6181537c 554
f3ed775e
DG
555 /* Next event */
556 event_name = strtok(NULL, ",");
557 }
558
8c9ae521 559end:
f3ed775e 560error:
ae856491
DG
561 if (warn) {
562 ret = CMD_WARNING;
563 }
b73d0b29
MD
564 if (opt_channel_name == NULL) {
565 free(channel_name);
566 }
cd80958d
DG
567 lttng_destroy_handle(handle);
568
f3ed775e
DG
569 return ret;
570}
571
572/*
6181537c 573 * Add event to trace session
f3ed775e
DG
574 */
575int cmd_enable_events(int argc, const char **argv)
576{
ca1c3607 577 int opt, ret = CMD_SUCCESS;
f3ed775e 578 static poptContext pc;
cd80958d 579 char *session_name = NULL;
f3ed775e
DG
580
581 pc = poptGetContext(NULL, argc, argv, long_options, 0);
582 poptReadDefaultConfig(pc, 0);
583
584 /* Default event type */
7a3d1328 585 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
586
587 while ((opt = poptGetNextOpt(pc)) != -1) {
588 switch (opt) {
589 case OPT_HELP:
ca1c3607 590 usage(stdout);
f3ed775e 591 goto end;
f3ed775e 592 case OPT_TRACEPOINT:
e6ddca71 593 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 594 break;
cf0e5467 595 case OPT_PROBE:
7d29a247 596 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
597 break;
598 case OPT_FUNCTION:
599 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
600 break;
601 case OPT_FUNCTION_ENTRY:
602 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 603 break;
a54bd42d
MD
604 case OPT_SYSCALL:
605 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 606 break;
eeac7d46
MD
607 case OPT_USERSPACE:
608 opt_userspace = 1;
eeac7d46 609 break;
0cda4f28 610 case OPT_LOGLEVEL:
8005f29a 611 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
612 opt_loglevel = poptGetOptArg(pc);
613 break;
614 case OPT_LOGLEVEL_ONLY:
8005f29a 615 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 616 opt_loglevel = poptGetOptArg(pc);
13dce3b7 617 break;
679b4943
SM
618 case OPT_LIST_OPTIONS:
619 list_cmd_options(stdout, long_options);
679b4943 620 goto end;
53a80697
MD
621 case OPT_FILTER:
622 break;
f3ed775e
DG
623 default:
624 usage(stderr);
625 ret = CMD_UNDEFINED;
626 goto end;
627 }
628 }
629
630 opt_event_list = (char*) poptGetArg(pc);
631 if (opt_event_list == NULL && opt_enable_all == 0) {
632 ERR("Missing event name(s).\n");
633 usage(stderr);
ca1c3607 634 ret = CMD_ERROR;
f3ed775e
DG
635 goto end;
636 }
637
cd80958d
DG
638 if (!opt_session_name) {
639 session_name = get_session_name();
640 if (session_name == NULL) {
ca1c3607 641 ret = CMD_ERROR;
cd80958d
DG
642 goto end;
643 }
644 } else {
645 session_name = opt_session_name;
646 }
647
648 ret = enable_events(session_name);
f3ed775e
DG
649
650end:
cd80958d
DG
651 if (opt_session_name == NULL) {
652 free(session_name);
653 }
654
ca1c3607 655 poptFreeContext(pc);
f3ed775e
DG
656 return ret;
657}
This page took 0.099419 seconds and 4 git commands to generate.