Document filter (help and manpage)
[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");
919e300c
MD
171 fprintf(ofp, " Only specify on first activation of\n");
172 fprintf(ofp, " a given event within a session.\n");
173 fprintf(ofp, " Filter only allowed when enabling\n");
174 fprintf(ofp, " events within a session before tracing\n");
9bd578f5
MD
175 fprintf(ofp, " is started. If the filter fails to link\n");
176 fprintf(ofp, " with the event within the traced domain,\n");
177 fprintf(ofp, " the event will be discarded. Currently,\n");
178 fprintf(ofp, " filter is only implemented for the user-space\n");
179 fprintf(ofp, " tracer.\n");
180 fprintf(ofp, " Expression examples:.\n");
181 fprintf(ofp, " \n");
182 fprintf(ofp, " 'intfield > 500 && intfield < 503'\n");
183 fprintf(ofp, " '(stringfield == \"test\" || intfield != 10) && intfield > 33'\n");
184 fprintf(ofp, " 'doublefield > 1.1 && intfield < 5.3'\n");
185 fprintf(ofp, " \n");
186 fprintf(ofp, " Wildcards are allowed at the end of strings:\n");
187 fprintf(ofp, " 'seqfield1 == \"te*\"'\n");
188 fprintf(ofp, " In string literals, the escape character is '\\'.\n");
189 fprintf(ofp, " Use '\\*' for the '*' character, and '\\\\' for\n");
190 fprintf(ofp, " the '\\' character.\n");
300b8fd5 191 fprintf(ofp, "\n");
f3ed775e
DG
192}
193
0d63dd19 194/*
6181537c 195 * Parse probe options.
0d63dd19 196 */
cf0e5467 197static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
198{
199 int ret;
8ff0bbd0 200 char s_hex[19];
0d63dd19
DG
201 char name[LTTNG_SYMBOL_NAME_LEN];
202
203 if (opt == NULL) {
204 ret = -1;
8f0d098b 205 goto end;
0d63dd19
DG
206 }
207
208 /* Check for symbol+offset */
8ff0bbd0 209 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 210 if (ret == 2) {
7d29a247 211 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 212 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 213 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
214 if (strlen(s_hex) == 0) {
215 ERR("Invalid probe offset %s", s_hex);
0d63dd19 216 ret = -1;
8f0d098b 217 goto end;
0d63dd19 218 }
8ff0bbd0 219 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 220 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 221 ev->attr.probe.addr = 0;
8f0d098b
MD
222 goto end;
223 }
224
225 /* Check for symbol */
226 if (isalpha(name[0])) {
227 ret = sscanf(opt, "%s", name);
228 if (ret == 1) {
229 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 230 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
231 DBG("probe symbol %s", ev->attr.probe.symbol_name);
232 ev->attr.probe.offset = 0;
233 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
234 ev->attr.probe.addr = 0;
235 goto end;
236 }
0d63dd19
DG
237 }
238
239 /* Check for address */
8ff0bbd0 240 ret = sscanf(opt, "%s", s_hex);
0d63dd19 241 if (ret > 0) {
8ff0bbd0
DG
242 if (strlen(s_hex) == 0) {
243 ERR("Invalid probe address %s", s_hex);
0d63dd19 244 ret = -1;
8f0d098b 245 goto end;
0d63dd19 246 }
8ff0bbd0 247 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 248 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
249 ev->attr.probe.offset = 0;
250 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 251 goto end;
0d63dd19
DG
252 }
253
254 /* No match */
255 ret = -1;
256
8f0d098b 257end:
0d63dd19
DG
258 return ret;
259}
260
8005f29a 261/*
0c8477c8 262 * Maps loglevel from string to value
8005f29a
MD
263 */
264static
46839cc2 265int loglevel_str_to_value(const char *inputstr)
8005f29a 266{
46839cc2
MD
267 int i = 0;
268 char str[LTTNG_SYMBOL_NAME_LEN];
269
270 while (inputstr[i] != '\0' && i < LTTNG_SYMBOL_NAME_LEN) {
271 str[i] = toupper(inputstr[i]);
272 i++;
273 }
274 str[i] = '\0';
275 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
276 return LTTNG_LOGLEVEL_EMERG;
277 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
278 return LTTNG_LOGLEVEL_ALERT;
279 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
280 return LTTNG_LOGLEVEL_CRIT;
281 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
282 return LTTNG_LOGLEVEL_ERR;
283 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
284 return LTTNG_LOGLEVEL_WARNING;
285 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
286 return LTTNG_LOGLEVEL_NOTICE;
287 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
288 return LTTNG_LOGLEVEL_INFO;
289 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
290 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
291 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
292 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
293 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
294 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
295 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
296 return LTTNG_LOGLEVEL_DEBUG_MODULE;
297 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
298 return LTTNG_LOGLEVEL_DEBUG_UNIT;
299 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
300 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
301 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
302 return LTTNG_LOGLEVEL_DEBUG_LINE;
303 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
304 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
305 } else {
306 return -1;
307 }
308}
309
f3ed775e 310/*
6181537c 311 * Enabling event using the lttng API.
f3ed775e 312 */
cd80958d 313static int enable_events(char *session_name)
f3ed775e 314{
ae856491 315 int err, ret = CMD_SUCCESS, warn = 0;
b73d0b29 316 char *event_name, *channel_name = NULL;
f3ed775e 317 struct lttng_event ev;
7d29a247 318 struct lttng_domain dom;
f3ed775e 319
441c16a7
MD
320 memset(&ev, 0, sizeof(ev));
321 memset(&dom, 0, sizeof(dom));
322
53a80697
MD
323 if (opt_kernel) {
324 if (opt_filter) {
325 ERR("Filter not implement for kernel tracing yet");
326 ret = CMD_ERROR;
327 goto error;
328 }
329 }
330
7d29a247
DG
331 /* Create lttng domain */
332 if (opt_kernel) {
333 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 334 } else if (opt_userspace) {
2bdd86d4 335 dom.type = LTTNG_DOMAIN_UST;
6181537c 336 } else {
e14f64a8 337 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 338 ret = CMD_ERROR;
6181537c 339 goto error;
2bdd86d4 340 }
7d29a247 341
ae856491
DG
342 if (opt_channel_name == NULL) {
343 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
344 if (err < 0) {
345 ret = CMD_FATAL;
346 goto error;
347 }
348 } else {
349 channel_name = opt_channel_name;
350 }
351
cd80958d
DG
352 handle = lttng_create_handle(session_name, &dom);
353 if (handle == NULL) {
354 ret = -1;
355 goto error;
356 }
1aef21b6 357
cd80958d 358 if (opt_enable_all) {
8c9ae521 359 /* Default setup for enable all */
75e8c5ab
MD
360 if (opt_kernel) {
361 ev.type = opt_event_type;
362 ev.name[0] = '\0';
300b8fd5
MD
363 /* kernel loglevels not implemented */
364 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
365 } else {
366 ev.type = LTTNG_EVENT_TRACEPOINT;
367 strcpy(ev.name, "*");
300b8fd5
MD
368 ev.loglevel_type = opt_loglevel_type;
369 if (opt_loglevel) {
370 ev.loglevel = loglevel_str_to_value(opt_loglevel);
371 if (ev.loglevel == -1) {
372 ERR("Unknown loglevel %s", opt_loglevel);
373 ret = -1;
374 goto error;
375 }
22e25b71
MD
376 } else {
377 ev.loglevel = -1;
300b8fd5 378 }
75e8c5ab 379 }
8c9ae521
DG
380
381 ret = lttng_enable_event(handle, &ev, channel_name);
382 if (ret < 0) {
42224349
DG
383 switch (-ret) {
384 case LTTCOMM_KERN_EVENT_EXIST:
385 WARN("Kernel events already enabled (channel %s, session %s)",
386 channel_name, session_name);
6d805133 387 break;
42224349 388 default:
a6de644c 389 ERR("Events: %s (channel %s, session %s)",
42224349
DG
390 lttng_strerror(ret), channel_name, session_name);
391 break;
392 }
6d805133 393 goto end;
8c9ae521 394 }
53a80697
MD
395 if (opt_filter) {
396 ret = lttng_set_event_filter(handle, ev.name, channel_name,
397 opt_filter);
398 if (ret < 0) {
399 ERR("Error setting filter");
400 ret = -1;
401 goto error;
402 }
403 }
8c9ae521 404
0133c199
MD
405 switch (opt_event_type) {
406 case LTTNG_EVENT_TRACEPOINT:
57064ada
MD
407 if (opt_loglevel) {
408 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
409 opt_kernel ? "kernel" : "UST", channel_name,
410 opt_loglevel);
411 } else {
412 MSG("All %s tracepoints are enabled in channel %s",
413 opt_kernel ? "kernel" : "UST", channel_name);
414
415 }
0133c199 416 break;
a54bd42d 417 case LTTNG_EVENT_SYSCALL:
0133c199 418 if (opt_kernel) {
8c9ae521
DG
419 MSG("All kernel system calls are enabled in channel %s",
420 channel_name);
c93d9daf 421 }
0133c199 422 break;
7a3d1328 423 case LTTNG_EVENT_ALL:
57064ada
MD
424 if (opt_loglevel) {
425 MSG("All %s events are enabled in channel %s for loglevel %s",
426 opt_kernel ? "kernel" : "UST", channel_name,
427 opt_loglevel);
428 } else {
429 MSG("All %s events are enabled in channel %s",
430 opt_kernel ? "kernel" : "UST", channel_name);
431 }
7a3d1328 432 break;
0133c199 433 default:
8c9ae521 434 /*
6181537c
DG
435 * We should not be here since lttng_enable_event should have
436 * failed on the event type.
8c9ae521 437 */
f3ed775e
DG
438 goto error;
439 }
8c9ae521 440 goto end;
f3ed775e
DG
441 }
442
443 /* Strip event list */
444 event_name = strtok(opt_event_list, ",");
445 while (event_name != NULL) {
6181537c
DG
446 /* Copy name and type of the event */
447 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
448 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
449 ev.type = opt_event_type;
450
f3ed775e
DG
451 /* Kernel tracer action */
452 if (opt_kernel) {
453 DBG("Enabling kernel event %s for channel %s",
454 event_name, channel_name);
f3ed775e
DG
455
456 switch (opt_event_type) {
7a3d1328
MD
457 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
458 ev.type = LTTNG_EVENT_TRACEPOINT;
459 /* Fall-through */
e6ddca71 460 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 461 break;
7d29a247 462 case LTTNG_EVENT_PROBE:
cf0e5467 463 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 464 if (ret < 0) {
cf0e5467 465 ERR("Unable to parse probe options");
0d63dd19
DG
466 ret = 0;
467 goto error;
468 }
f3ed775e
DG
469 break;
470 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
471 ret = parse_probe_opts(&ev, opt_function);
472 if (ret < 0) {
473 ERR("Unable to parse function probe options");
474 ret = 0;
475 goto error;
476 }
477 break;
478 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
479 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
480 LTTNG_SYMBOL_NAME_LEN);
99497cd0 481 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 482 break;
a54bd42d 483 case LTTNG_EVENT_SYSCALL:
6181537c
DG
484 MSG("per-syscall selection not supported yet. Use \"-a\" "
485 "for all syscalls.");
f3ed775e 486 default:
1ab1ea0b 487 ret = CMD_UNDEFINED;
f3ed775e
DG
488 goto error;
489 }
0cda4f28 490
ed7f4083 491 if (opt_loglevel) {
0cda4f28
MD
492 MSG("Kernel loglevels are not supported.");
493 ret = CMD_UNDEFINED;
494 goto error;
495 }
496
497 /* kernel loglevels not implemented */
8005f29a 498 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 499 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 500#if 0
e14f64a8
DG
501 if (opt_cmd_name != NULL || opt_pid) {
502 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 503 ret = CMD_UNDEFINED;
2bdd86d4
MD
504 goto error;
505 }
d78d6610 506#endif
6181537c 507
300b8fd5
MD
508 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
509 channel_name, opt_loglevel ? : "<all>");
2bdd86d4
MD
510
511 switch (opt_event_type) {
512 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
513 /* Fall-through */
514 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
515 /* Copy name and type of the event */
516 ev.type = LTTNG_EVENT_TRACEPOINT;
517 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
518 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
519 break;
520 case LTTNG_EVENT_PROBE:
521 case LTTNG_EVENT_FUNCTION:
522 case LTTNG_EVENT_FUNCTION_ENTRY:
523 case LTTNG_EVENT_SYSCALL:
524 default:
cc62c0c0 525 ERR("Event type not available for user-space tracing");
1ab1ea0b 526 ret = CMD_UNDEFINED;
2bdd86d4
MD
527 goto error;
528 }
0cda4f28
MD
529
530 ev.loglevel_type = opt_loglevel_type;
ed7f4083 531 if (opt_loglevel) {
8005f29a
MD
532 ev.loglevel = loglevel_str_to_value(opt_loglevel);
533 if (ev.loglevel == -1) {
534 ERR("Unknown loglevel %s", opt_loglevel);
300b8fd5 535 ret = -1;
8005f29a
MD
536 goto error;
537 }
22e25b71
MD
538 } else {
539 ev.loglevel = -1;
ed7f4083 540 }
f3ed775e 541 } else {
e14f64a8 542 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
300b8fd5 543 ret = CMD_ERROR;
f3ed775e
DG
544 goto error;
545 }
546
6181537c 547 ret = lttng_enable_event(handle, &ev, channel_name);
ae856491 548 if (ret < 0) {
42224349
DG
549 /* Turn ret to positive value to handle the positive error code */
550 switch (-ret) {
551 case LTTCOMM_KERN_EVENT_EXIST:
552 WARN("Kernel event %s already enabled (channel %s, session %s)",
553 event_name, channel_name, session_name);
554 break;
555 default:
556 ERR("Event %s: %s (channel %s, session %s)", event_name,
557 lttng_strerror(ret), channel_name, session_name);
558 break;
559 }
ae856491
DG
560 warn = 1;
561 } else {
6181537c
DG
562 MSG("%s event %s created in channel %s",
563 opt_kernel ? "kernel": "UST", event_name, channel_name);
564 }
53a80697
MD
565 if (opt_filter) {
566 ret = lttng_set_event_filter(handle, ev.name,
567 channel_name, opt_filter);
568 if (ret < 0) {
569 ERR("Error setting filter");
570 ret = -1;
571 goto error;
572 }
573 }
6181537c 574
f3ed775e
DG
575 /* Next event */
576 event_name = strtok(NULL, ",");
577 }
578
8c9ae521 579end:
f3ed775e 580error:
ae856491
DG
581 if (warn) {
582 ret = CMD_WARNING;
583 }
b73d0b29
MD
584 if (opt_channel_name == NULL) {
585 free(channel_name);
586 }
cd80958d
DG
587 lttng_destroy_handle(handle);
588
f3ed775e
DG
589 return ret;
590}
591
592/*
6181537c 593 * Add event to trace session
f3ed775e
DG
594 */
595int cmd_enable_events(int argc, const char **argv)
596{
ca1c3607 597 int opt, ret = CMD_SUCCESS;
f3ed775e 598 static poptContext pc;
cd80958d 599 char *session_name = NULL;
f3ed775e
DG
600
601 pc = poptGetContext(NULL, argc, argv, long_options, 0);
602 poptReadDefaultConfig(pc, 0);
603
604 /* Default event type */
7a3d1328 605 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
606
607 while ((opt = poptGetNextOpt(pc)) != -1) {
608 switch (opt) {
609 case OPT_HELP:
ca1c3607 610 usage(stdout);
f3ed775e 611 goto end;
f3ed775e 612 case OPT_TRACEPOINT:
e6ddca71 613 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 614 break;
cf0e5467 615 case OPT_PROBE:
7d29a247 616 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
617 break;
618 case OPT_FUNCTION:
619 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
620 break;
621 case OPT_FUNCTION_ENTRY:
622 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 623 break;
a54bd42d
MD
624 case OPT_SYSCALL:
625 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 626 break;
eeac7d46
MD
627 case OPT_USERSPACE:
628 opt_userspace = 1;
eeac7d46 629 break;
0cda4f28 630 case OPT_LOGLEVEL:
8005f29a 631 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
632 opt_loglevel = poptGetOptArg(pc);
633 break;
634 case OPT_LOGLEVEL_ONLY:
8005f29a 635 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 636 opt_loglevel = poptGetOptArg(pc);
13dce3b7 637 break;
679b4943
SM
638 case OPT_LIST_OPTIONS:
639 list_cmd_options(stdout, long_options);
679b4943 640 goto end;
53a80697
MD
641 case OPT_FILTER:
642 break;
f3ed775e
DG
643 default:
644 usage(stderr);
645 ret = CMD_UNDEFINED;
646 goto end;
647 }
648 }
649
650 opt_event_list = (char*) poptGetArg(pc);
651 if (opt_event_list == NULL && opt_enable_all == 0) {
652 ERR("Missing event name(s).\n");
653 usage(stderr);
ca1c3607 654 ret = CMD_ERROR;
f3ed775e
DG
655 goto end;
656 }
657
cd80958d
DG
658 if (!opt_session_name) {
659 session_name = get_session_name();
660 if (session_name == NULL) {
ca1c3607 661 ret = CMD_ERROR;
cd80958d
DG
662 goto end;
663 }
664 } else {
665 session_name = opt_session_name;
666 }
667
668 ret = enable_events(session_name);
f3ed775e
DG
669
670end:
cd80958d
DG
671 if (opt_session_name == NULL) {
672 free(session_name);
673 }
674
ca1c3607 675 poptFreeContext(pc);
f3ed775e
DG
676 return ret;
677}
This page took 0.101499 seconds and 4 git commands to generate.