Fix: check malloc return value
[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 74 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
f3ed775e 75 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 76 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 77 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
78#if 0
79 /*
80 * Currently removed from lttng kernel tracer. Removed from
81 * lttng UI to discourage its use.
82 */
40e9d5d3 83 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 84#endif
7ebae521 85 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
86 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
87 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 88 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 89 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
f3ed775e
DG
90 {0, 0, 0, 0, 0, 0, 0}
91};
92
93/*
94 * usage
95 */
96static void usage(FILE *ofp)
97{
32a6298d 98 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [-k|-u] [OPTIONS] \n");
f3ed775e 99 fprintf(ofp, "\n");
32a6298d 100 fprintf(ofp, "Options:\n");
f3ed775e 101 fprintf(ofp, " -h, --help Show this help\n");
679b4943 102 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d
DG
103 fprintf(ofp, " -s, --session NAME Apply to session name\n");
104 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
e08bff8d 105 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
f3ed775e 106 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
27221701 107 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
f3ed775e
DG
108 fprintf(ofp, "\n");
109 fprintf(ofp, "Event options:\n");
110 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
be49af1e
MD
111 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
112 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
113 fprintf(ofp, " e.g.:\n");
114 fprintf(ofp, " \"*\"\n");
115 fprintf(ofp, " \"app_component:na*\"\n");
6a240cd9 116 fprintf(ofp, " --probe (addr | symbol | symbol+offset)\n");
cf0e5467
MD
117 fprintf(ofp, " Dynamic probe.\n");
118 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
119 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
6a240cd9 120 fprintf(ofp, " --function (addr | symbol | symbol+offset)\n");
8f0d098b
MD
121 fprintf(ofp, " Dynamic function entry/return probe.\n");
122 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
123 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 124#if 0
8f0d098b
MD
125 fprintf(ofp, " --function:entry symbol\n");
126 fprintf(ofp, " Function tracer event\n");
b213d387 127#endif
a54bd42d 128 fprintf(ofp, " --syscall System call event\n");
f3ed775e 129 fprintf(ofp, "\n");
300b8fd5
MD
130 fprintf(ofp, " --loglevel name\n");
131 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n");
132 fprintf(ofp, " --loglevel-only name\n");
133 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
134 fprintf(ofp, "\n");
0c8477c8
MD
135 fprintf(ofp, " The loglevel or loglevel-only options should be\n");
136 fprintf(ofp, " combined with a tracepoint name or tracepoint\n");
137 fprintf(ofp, " wildcard.\n");
138 fprintf(ofp, " Available loglevels:\n");
139 fprintf(ofp, " (higher value is more verbose)\n");
46839cc2
MD
140 fprintf(ofp, " TRACE_EMERG = 0\n");
141 fprintf(ofp, " TRACE_ALERT = 1\n");
142 fprintf(ofp, " TRACE_CRIT = 2\n");
143 fprintf(ofp, " TRACE_ERR = 3\n");
144 fprintf(ofp, " TRACE_WARNING = 4\n");
145 fprintf(ofp, " TRACE_NOTICE = 5\n");
146 fprintf(ofp, " TRACE_INFO = 6\n");
147 fprintf(ofp, " TRACE_DEBUG_SYSTEM = 7\n");
148 fprintf(ofp, " TRACE_DEBUG_PROGRAM = 8\n");
149 fprintf(ofp, " TRACE_DEBUG_PROCESS = 9\n");
150 fprintf(ofp, " TRACE_DEBUG_MODULE = 10\n");
151 fprintf(ofp, " TRACE_DEBUG_UNIT = 11\n");
152 fprintf(ofp, " TRACE_DEBUG_FUNCTION = 12\n");
153 fprintf(ofp, " TRACE_DEBUG_LINE = 13\n");
154 fprintf(ofp, " TRACE_DEBUG = 14\n");
155 fprintf(ofp, " (shortcuts such as \"system\" are allowed)\n");
f2611ab0 156 fprintf(ofp, " -f, --filter \'expression\'\n");
ee8ccafa
MD
157 fprintf(ofp, " Filter expression on event fields and context.\n");
158 fprintf(ofp, " Event recording depends on evaluation.\n");
919e300c
MD
159 fprintf(ofp, " Only specify on first activation of\n");
160 fprintf(ofp, " a given event within a session.\n");
161 fprintf(ofp, " Filter only allowed when enabling\n");
162 fprintf(ofp, " events within a session before tracing\n");
9bd578f5
MD
163 fprintf(ofp, " is started. If the filter fails to link\n");
164 fprintf(ofp, " with the event within the traced domain,\n");
165 fprintf(ofp, " the event will be discarded. Currently,\n");
166 fprintf(ofp, " filter is only implemented for the user-space\n");
167 fprintf(ofp, " tracer.\n");
168 fprintf(ofp, " Expression examples:.\n");
169 fprintf(ofp, " \n");
170 fprintf(ofp, " 'intfield > 500 && intfield < 503'\n");
6a240cd9 171 fprintf(ofp, " '(strfield == \"test\" || intfield != 10) && intfield > 33'\n");
9bd578f5
MD
172 fprintf(ofp, " 'doublefield > 1.1 && intfield < 5.3'\n");
173 fprintf(ofp, " \n");
174 fprintf(ofp, " Wildcards are allowed at the end of strings:\n");
175 fprintf(ofp, " 'seqfield1 == \"te*\"'\n");
176 fprintf(ofp, " In string literals, the escape character is '\\'.\n");
177 fprintf(ofp, " Use '\\*' for the '*' character, and '\\\\' for\n");
ee8ccafa
MD
178 fprintf(ofp, " the '\\' character. Wildcard match any sequence of,\n");
179 fprintf(ofp, " characters including an empty sub-string (match 0 or\n");
180 fprintf(ofp, " more characters).\n");
181 fprintf(ofp, "\n");
182 fprintf(ofp, " Context information can be used for filtering. The\n");
183 fprintf(ofp, " examples below show usage of context filtering on\n");
184 fprintf(ofp, " process name (with a wildcard), process ID range, and\n");
185 fprintf(ofp, " unique thread ID for filtering. The process and\n");
186 fprintf(ofp, " thread ID of running applications can be found under\n");
187 fprintf(ofp, " columns \"PID\" and \"LWP\" of the \"ps -eLf\" command.\n");
188 fprintf(ofp, "\n");
189 fprintf(ofp, " '$ctx.procname == \"demo*\"'\n");
190 fprintf(ofp, " '$ctx.vpid >= 4433 && $ctx.vpid < 4455'\n");
191 fprintf(ofp, " '$ctx.vtid == 1234'\n");
300b8fd5 192 fprintf(ofp, "\n");
f3ed775e
DG
193}
194
0d63dd19 195/*
6181537c 196 * Parse probe options.
0d63dd19 197 */
cf0e5467 198static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
199{
200 int ret;
8ff0bbd0 201 char s_hex[19];
0d63dd19
DG
202 char name[LTTNG_SYMBOL_NAME_LEN];
203
204 if (opt == NULL) {
205 ret = -1;
8f0d098b 206 goto end;
0d63dd19
DG
207 }
208
209 /* Check for symbol+offset */
8ff0bbd0 210 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 211 if (ret == 2) {
7d29a247 212 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 213 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 214 DBG("probe symbol %s", ev->attr.probe.symbol_name);
9d035200 215 if (*s_hex == '\0') {
8ff0bbd0 216 ERR("Invalid probe offset %s", s_hex);
0d63dd19 217 ret = -1;
8f0d098b 218 goto end;
0d63dd19 219 }
8ff0bbd0 220 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 221 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 222 ev->attr.probe.addr = 0;
8f0d098b
MD
223 goto end;
224 }
225
226 /* Check for symbol */
227 if (isalpha(name[0])) {
228 ret = sscanf(opt, "%s", name);
229 if (ret == 1) {
230 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 231 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
232 DBG("probe symbol %s", ev->attr.probe.symbol_name);
233 ev->attr.probe.offset = 0;
234 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
235 ev->attr.probe.addr = 0;
236 goto end;
237 }
0d63dd19
DG
238 }
239
240 /* Check for address */
8ff0bbd0 241 ret = sscanf(opt, "%s", s_hex);
0d63dd19 242 if (ret > 0) {
9d035200 243 if (*s_hex == '\0') {
8ff0bbd0 244 ERR("Invalid probe address %s", s_hex);
0d63dd19 245 ret = -1;
8f0d098b 246 goto end;
0d63dd19 247 }
8ff0bbd0 248 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 249 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
250 ev->attr.probe.offset = 0;
251 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 252 goto end;
0d63dd19
DG
253 }
254
255 /* No match */
256 ret = -1;
257
8f0d098b 258end:
0d63dd19
DG
259 return ret;
260}
261
8005f29a 262/*
0c8477c8 263 * Maps loglevel from string to value
8005f29a
MD
264 */
265static
46839cc2 266int loglevel_str_to_value(const char *inputstr)
8005f29a 267{
46839cc2
MD
268 int i = 0;
269 char str[LTTNG_SYMBOL_NAME_LEN];
270
e051e07c
DG
271 /*
272 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
273 * added at the end of the loop so a the upper bound we avoid the overflow.
274 */
275 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
46839cc2
MD
276 str[i] = toupper(inputstr[i]);
277 i++;
278 }
279 str[i] = '\0';
280 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
281 return LTTNG_LOGLEVEL_EMERG;
282 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
283 return LTTNG_LOGLEVEL_ALERT;
284 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
285 return LTTNG_LOGLEVEL_CRIT;
286 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
287 return LTTNG_LOGLEVEL_ERR;
288 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
289 return LTTNG_LOGLEVEL_WARNING;
290 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
291 return LTTNG_LOGLEVEL_NOTICE;
292 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
293 return LTTNG_LOGLEVEL_INFO;
294 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
295 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
296 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
297 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
298 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
299 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
300 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
301 return LTTNG_LOGLEVEL_DEBUG_MODULE;
302 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
303 return LTTNG_LOGLEVEL_DEBUG_UNIT;
304 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
305 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
306 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
307 return LTTNG_LOGLEVEL_DEBUG_LINE;
308 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
309 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
310 } else {
311 return -1;
312 }
313}
314
85076754
MD
315static
316const char *print_channel_name(const char *name)
317{
318 return name ? : DEFAULT_CHANNEL_NAME;
319}
320
321static
322const char *print_raw_channel_name(const char *name)
323{
324 return name ? : "<default>";
325}
326
f3ed775e 327/*
6181537c 328 * Enabling event using the lttng API.
f3ed775e 329 */
cd80958d 330static int enable_events(char *session_name)
f3ed775e 331{
85076754 332 int ret = CMD_SUCCESS, warn = 0;
b73d0b29 333 char *event_name, *channel_name = NULL;
f3ed775e 334 struct lttng_event ev;
7d29a247 335 struct lttng_domain dom;
f3ed775e 336
441c16a7
MD
337 memset(&ev, 0, sizeof(ev));
338 memset(&dom, 0, sizeof(dom));
339
53a80697
MD
340 if (opt_kernel) {
341 if (opt_filter) {
342 ERR("Filter not implement for kernel tracing yet");
343 ret = CMD_ERROR;
344 goto error;
345 }
346 }
347
7d29a247
DG
348 /* Create lttng domain */
349 if (opt_kernel) {
350 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 351 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 352 } else if (opt_userspace) {
2bdd86d4 353 dom.type = LTTNG_DOMAIN_UST;
7972aab2 354 /* Default. */
8692d4e5 355 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 356 } else {
e14f64a8 357 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 358 ret = CMD_ERROR;
6181537c 359 goto error;
2bdd86d4 360 }
7d29a247 361
85076754 362 channel_name = opt_channel_name;
ae856491 363
cd80958d
DG
364 handle = lttng_create_handle(session_name, &dom);
365 if (handle == NULL) {
366 ret = -1;
367 goto error;
368 }
1aef21b6 369
cd80958d 370 if (opt_enable_all) {
8c9ae521 371 /* Default setup for enable all */
75e8c5ab
MD
372 if (opt_kernel) {
373 ev.type = opt_event_type;
374 ev.name[0] = '\0';
300b8fd5
MD
375 /* kernel loglevels not implemented */
376 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
377 } else {
378 ev.type = LTTNG_EVENT_TRACEPOINT;
379 strcpy(ev.name, "*");
300b8fd5
MD
380 ev.loglevel_type = opt_loglevel_type;
381 if (opt_loglevel) {
382 ev.loglevel = loglevel_str_to_value(opt_loglevel);
383 if (ev.loglevel == -1) {
384 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 385 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
386 goto error;
387 }
22e25b71
MD
388 } else {
389 ev.loglevel = -1;
300b8fd5 390 }
75e8c5ab 391 }
8c9ae521 392
025faf73
DG
393 if (!opt_filter) {
394 ret = lttng_enable_event(handle, &ev, channel_name);
395 if (ret < 0) {
396 switch (-ret) {
397 case LTTNG_ERR_KERN_EVENT_EXIST:
398 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 399 print_channel_name(channel_name), session_name);
025faf73
DG
400 break;
401 default:
402 ERR("Events: %s (channel %s, session %s)",
85076754
MD
403 lttng_strerror(ret),
404 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
405 ? print_raw_channel_name(channel_name)
406 : print_channel_name(channel_name),
407 session_name);
025faf73
DG
408 break;
409 }
410 goto end;
42224349 411 }
8c9ae521 412
025faf73
DG
413 switch (opt_event_type) {
414 case LTTNG_EVENT_TRACEPOINT:
415 if (opt_loglevel) {
416 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
85076754
MD
417 opt_kernel ? "kernel" : "UST",
418 print_channel_name(channel_name),
025faf73
DG
419 opt_loglevel);
420 } else {
421 MSG("All %s tracepoints are enabled in channel %s",
85076754
MD
422 opt_kernel ? "kernel" : "UST",
423 print_channel_name(channel_name));
57064ada 424
025faf73
DG
425 }
426 break;
427 case LTTNG_EVENT_SYSCALL:
428 if (opt_kernel) {
429 MSG("All kernel system calls are enabled in channel %s",
85076754 430 print_channel_name(channel_name));
025faf73
DG
431 }
432 break;
433 case LTTNG_EVENT_ALL:
434 if (opt_loglevel) {
435 MSG("All %s events are enabled in channel %s for loglevel %s",
85076754
MD
436 opt_kernel ? "kernel" : "UST",
437 print_channel_name(channel_name),
025faf73
DG
438 opt_loglevel);
439 } else {
440 MSG("All %s events are enabled in channel %s",
85076754
MD
441 opt_kernel ? "kernel" : "UST",
442 print_channel_name(channel_name));
025faf73
DG
443 }
444 break;
445 default:
446 /*
447 * We should not be here since lttng_enable_event should have
448 * failed on the event type.
449 */
450 goto error;
57064ada 451 }
f3ed775e 452 }
025faf73
DG
453 if (opt_filter) {
454 ret = lttng_enable_event_with_filter(handle, &ev, channel_name,
16363652
DG
455 opt_filter);
456 if (ret < 0) {
457 switch (-ret) {
458 case LTTNG_ERR_FILTER_EXIST:
85076754 459 WARN("Filter on all events is already enabled"
16363652 460 " (channel %s, session %s)",
85076754 461 print_channel_name(channel_name), session_name);
16363652
DG
462 break;
463 default:
85076754
MD
464 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
465 lttng_strerror(ret),
466 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
467 ? print_raw_channel_name(channel_name)
468 : print_channel_name(channel_name),
469 session_name, opt_filter);
16363652
DG
470 break;
471 }
472 goto error;
473 } else {
474 MSG("Filter '%s' successfully set", opt_filter);
475 }
476 }
8c9ae521 477 goto end;
f3ed775e
DG
478 }
479
480 /* Strip event list */
481 event_name = strtok(opt_event_list, ",");
482 while (event_name != NULL) {
6181537c
DG
483 /* Copy name and type of the event */
484 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
485 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
486 ev.type = opt_event_type;
487
f3ed775e
DG
488 /* Kernel tracer action */
489 if (opt_kernel) {
490 DBG("Enabling kernel event %s for channel %s",
85076754
MD
491 event_name,
492 print_channel_name(channel_name));
f3ed775e
DG
493
494 switch (opt_event_type) {
7a3d1328
MD
495 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
496 ev.type = LTTNG_EVENT_TRACEPOINT;
497 /* Fall-through */
e6ddca71 498 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 499 break;
7d29a247 500 case LTTNG_EVENT_PROBE:
cf0e5467 501 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 502 if (ret < 0) {
cf0e5467 503 ERR("Unable to parse probe options");
0d63dd19
DG
504 ret = 0;
505 goto error;
506 }
f3ed775e
DG
507 break;
508 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
509 ret = parse_probe_opts(&ev, opt_function);
510 if (ret < 0) {
511 ERR("Unable to parse function probe options");
512 ret = 0;
513 goto error;
514 }
515 break;
516 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
517 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
518 LTTNG_SYMBOL_NAME_LEN);
99497cd0 519 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 520 break;
a54bd42d 521 case LTTNG_EVENT_SYSCALL:
6181537c
DG
522 MSG("per-syscall selection not supported yet. Use \"-a\" "
523 "for all syscalls.");
f3ed775e 524 default:
1ab1ea0b 525 ret = CMD_UNDEFINED;
f3ed775e
DG
526 goto error;
527 }
0cda4f28 528
ed7f4083 529 if (opt_loglevel) {
0cda4f28 530 MSG("Kernel loglevels are not supported.");
4ce78777 531 ret = CMD_UNSUPPORTED;
0cda4f28
MD
532 goto error;
533 }
534
535 /* kernel loglevels not implemented */
8005f29a 536 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 537 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 538#if 0
e14f64a8
DG
539 if (opt_cmd_name != NULL || opt_pid) {
540 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 541 ret = CMD_UNDEFINED;
2bdd86d4
MD
542 goto error;
543 }
d78d6610 544#endif
6181537c 545
300b8fd5 546 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 547 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
548
549 switch (opt_event_type) {
550 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
551 /* Fall-through */
552 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
553 /* Copy name and type of the event */
554 ev.type = LTTNG_EVENT_TRACEPOINT;
555 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
556 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
557 break;
558 case LTTNG_EVENT_PROBE:
559 case LTTNG_EVENT_FUNCTION:
560 case LTTNG_EVENT_FUNCTION_ENTRY:
561 case LTTNG_EVENT_SYSCALL:
562 default:
cc62c0c0 563 ERR("Event type not available for user-space tracing");
4ce78777 564 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
565 goto error;
566 }
0cda4f28
MD
567
568 ev.loglevel_type = opt_loglevel_type;
ed7f4083 569 if (opt_loglevel) {
8005f29a
MD
570 ev.loglevel = loglevel_str_to_value(opt_loglevel);
571 if (ev.loglevel == -1) {
572 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 573 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
574 goto error;
575 }
22e25b71
MD
576 } else {
577 ev.loglevel = -1;
ed7f4083 578 }
f3ed775e 579 } else {
e14f64a8 580 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
300b8fd5 581 ret = CMD_ERROR;
f3ed775e
DG
582 goto error;
583 }
584
025faf73
DG
585 if (!opt_filter) {
586 ret = lttng_enable_event(handle, &ev, channel_name);
587 if (ret < 0) {
588 /* Turn ret to positive value to handle the positive error code */
589 switch (-ret) {
590 case LTTNG_ERR_KERN_EVENT_EXIST:
591 WARN("Kernel event %s already enabled (channel %s, session %s)",
85076754
MD
592 event_name,
593 print_channel_name(channel_name), session_name);
025faf73
DG
594 break;
595 default:
596 ERR("Event %s: %s (channel %s, session %s)", event_name,
85076754
MD
597 lttng_strerror(ret),
598 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
599 ? print_raw_channel_name(channel_name)
600 : print_channel_name(channel_name),
601 session_name);
025faf73
DG
602 break;
603 }
604 warn = 1;
605 } else {
606 MSG("%s event %s created in channel %s",
85076754
MD
607 opt_kernel ? "kernel": "UST", event_name,
608 print_channel_name(channel_name));
42224349 609 }
6181537c 610 }
025faf73
DG
611
612 if (opt_filter) {
613 ret = lttng_enable_event_with_filter(handle, &ev, channel_name,
16363652 614 opt_filter);
53a80697 615 if (ret < 0) {
7671f53c
CB
616 switch (-ret) {
617 case LTTNG_ERR_FILTER_EXIST:
ff7ef7b8 618 WARN("Filter on event %s is already enabled"
7671f53c 619 " (channel %s, session %s)",
85076754
MD
620 event_name,
621 print_channel_name(channel_name), session_name);
7671f53c
CB
622 break;
623 default:
85076754
MD
624 ERR("Event %s: %s (channel %s, session %s, filter \'%s\')", ev.name,
625 lttng_strerror(ret),
626 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
627 ? print_raw_channel_name(channel_name)
628 : print_channel_name(channel_name),
629 session_name, opt_filter);
7671f53c
CB
630 break;
631 }
53a80697 632 goto error;
16363652
DG
633 } else {
634 MSG("Filter '%s' successfully set", opt_filter);
53a80697
MD
635 }
636 }
6181537c 637
f3ed775e
DG
638 /* Next event */
639 event_name = strtok(NULL, ",");
640 }
641
8c9ae521 642end:
f3ed775e 643error:
ae856491
DG
644 if (warn) {
645 ret = CMD_WARNING;
646 }
cd80958d
DG
647 lttng_destroy_handle(handle);
648
f3ed775e
DG
649 return ret;
650}
651
652/*
6181537c 653 * Add event to trace session
f3ed775e
DG
654 */
655int cmd_enable_events(int argc, const char **argv)
656{
ca1c3607 657 int opt, ret = CMD_SUCCESS;
f3ed775e 658 static poptContext pc;
cd80958d 659 char *session_name = NULL;
de044b7a 660 int event_type = -1;
f3ed775e
DG
661
662 pc = poptGetContext(NULL, argc, argv, long_options, 0);
663 poptReadDefaultConfig(pc, 0);
664
665 /* Default event type */
7a3d1328 666 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
667
668 while ((opt = poptGetNextOpt(pc)) != -1) {
669 switch (opt) {
670 case OPT_HELP:
ca1c3607 671 usage(stdout);
f3ed775e 672 goto end;
f3ed775e 673 case OPT_TRACEPOINT:
e6ddca71 674 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 675 break;
cf0e5467 676 case OPT_PROBE:
7d29a247 677 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
678 break;
679 case OPT_FUNCTION:
680 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
681 break;
682 case OPT_FUNCTION_ENTRY:
683 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 684 break;
a54bd42d
MD
685 case OPT_SYSCALL:
686 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 687 break;
eeac7d46
MD
688 case OPT_USERSPACE:
689 opt_userspace = 1;
eeac7d46 690 break;
0cda4f28 691 case OPT_LOGLEVEL:
8005f29a 692 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
693 opt_loglevel = poptGetOptArg(pc);
694 break;
695 case OPT_LOGLEVEL_ONLY:
8005f29a 696 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 697 opt_loglevel = poptGetOptArg(pc);
13dce3b7 698 break;
679b4943
SM
699 case OPT_LIST_OPTIONS:
700 list_cmd_options(stdout, long_options);
679b4943 701 goto end;
53a80697
MD
702 case OPT_FILTER:
703 break;
f3ed775e
DG
704 default:
705 usage(stderr);
706 ret = CMD_UNDEFINED;
707 goto end;
708 }
de044b7a
DG
709
710 /* Validate event type. Multiple event type are not supported. */
711 if (event_type == -1) {
712 event_type = opt_event_type;
713 } else {
714 if (event_type != opt_event_type) {
715 ERR("Multiple event type not supported.");
716 ret = CMD_ERROR;
717 goto end;
718 }
719 }
f3ed775e
DG
720 }
721
722 opt_event_list = (char*) poptGetArg(pc);
723 if (opt_event_list == NULL && opt_enable_all == 0) {
724 ERR("Missing event name(s).\n");
725 usage(stderr);
ca1c3607 726 ret = CMD_ERROR;
f3ed775e
DG
727 goto end;
728 }
729
cd80958d
DG
730 if (!opt_session_name) {
731 session_name = get_session_name();
732 if (session_name == NULL) {
ca1c3607 733 ret = CMD_ERROR;
cd80958d
DG
734 goto end;
735 }
736 } else {
737 session_name = opt_session_name;
738 }
739
740 ret = enable_events(session_name);
f3ed775e
DG
741
742end:
cd80958d
DG
743 if (opt_session_name == NULL) {
744 free(session_name);
745 }
746
ca1c3607 747 poptFreeContext(pc);
f3ed775e
DG
748 return ret;
749}
This page took 0.079039 seconds and 4 git commands to generate.