Cleanup: rename `get_domain_str()` -> `lttng_domain_type_str()`
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
f3ed775e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
f3ed775e 5 *
f3ed775e
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
b2064f54 9#include <assert.h>
f3ed775e
DG
10#include <popt.h>
11#include <stdio.h>
12#include <stdlib.h>
f3ed775e
DG
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
5a0de755 16#include <inttypes.h>
8f0d098b 17#include <ctype.h>
f3ed775e 18
343af227 19#include <common/sessiond-comm/sessiond-comm.h>
f5436bfc 20#include <common/compat/string.h>
dbc478f3 21#include <common/compat/getenv.h>
9f449915 22#include <common/string-utils/string-utils.h>
dbc478f3 23#include <common/utils.h>
f3ed775e 24
89476427
JRJ
25/* Mi dependancy */
26#include <common/mi-lttng.h>
27
1004b719 28#include <lttng/domain-internal.h>
7e8f2e9c
JG
29#include <lttng/event-internal.h>
30
89476427 31#include "../command.h"
7e8f2e9c 32#include "../loglevel.h"
0c4e727b 33#include "../uprobe.h"
89476427 34
8ab7c0d9
MD
35#if (LTTNG_SYMBOL_NAME_LEN == 256)
36#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
37#endif
38
f3ed775e
DG
39static char *opt_event_list;
40static int opt_event_type;
0cda4f28
MD
41static const char *opt_loglevel;
42static int opt_loglevel_type;
6181537c 43static int opt_kernel;
5440dc42 44static char *opt_session_name;
f3ed775e 45static int opt_userspace;
b9dfb167 46static int opt_jul;
5cdb6027 47static int opt_log4j;
0e115563 48static int opt_python;
f3ed775e 49static int opt_enable_all;
cf0e5467 50static char *opt_probe;
dcabc190 51static char *opt_userspace_probe;
8f0d098b 52static char *opt_function;
f3ed775e 53static char *opt_channel_name;
53a80697 54static char *opt_filter;
fac3366c 55static char *opt_exclude;
f3ed775e 56
4fc83d94
PP
57#ifdef LTTNG_EMBED_HELP
58static const char help_msg[] =
59#include <lttng-enable-event.1.h>
60;
61#endif
62
f3ed775e
DG
63enum {
64 OPT_HELP = 1,
f3ed775e 65 OPT_TRACEPOINT,
cf0e5467 66 OPT_PROBE,
dcabc190 67 OPT_USERSPACE_PROBE,
f3ed775e 68 OPT_FUNCTION,
a54bd42d 69 OPT_SYSCALL,
eeac7d46 70 OPT_USERSPACE,
0cda4f28
MD
71 OPT_LOGLEVEL,
72 OPT_LOGLEVEL_ONLY,
679b4943 73 OPT_LIST_OPTIONS,
53a80697 74 OPT_FILTER,
fac3366c 75 OPT_EXCLUDE,
f3ed775e
DG
76};
77
cd80958d 78static struct lttng_handle *handle;
89476427 79static struct mi_writer *writer;
cd80958d 80
f3ed775e
DG
81static struct poptOption long_options[] = {
82 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
83 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 84 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 85 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
86 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
87 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 88 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
b9dfb167 89 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 90 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 91 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
f3ed775e 92 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 93 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
dcabc190 94 {"userspace-probe",0, POPT_ARG_STRING, &opt_userspace_probe, OPT_USERSPACE_PROBE, 0, 0},
40e9d5d3 95 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
7ebae521 96 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
97 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
98 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 99 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 100 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
fac3366c 101 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
f3ed775e
DG
102 {0, 0, 0, 0, 0, 0, 0}
103};
104
0d63dd19 105/*
6181537c 106 * Parse probe options.
0d63dd19 107 */
cf0e5467 108static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19 109{
49d4e302
JRJ
110 int ret = CMD_SUCCESS;
111 int match;
8ff0bbd0 112 char s_hex[19];
8ab7c0d9 113#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
0d63dd19
DG
114 char name[LTTNG_SYMBOL_NAME_LEN];
115
116 if (opt == NULL) {
49d4e302 117 ret = CMD_ERROR;
8f0d098b 118 goto end;
0d63dd19
DG
119 }
120
121 /* Check for symbol+offset */
49d4e302 122 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
8ab7c0d9 123 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
49d4e302 124 if (match == 2) {
7d29a247 125 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 126 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 127 DBG("probe symbol %s", ev->attr.probe.symbol_name);
9d035200 128 if (*s_hex == '\0') {
8ff0bbd0 129 ERR("Invalid probe offset %s", s_hex);
49d4e302 130 ret = CMD_ERROR;
8f0d098b 131 goto end;
0d63dd19 132 }
8ff0bbd0 133 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 134 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 135 ev->attr.probe.addr = 0;
8f0d098b
MD
136 goto end;
137 }
138
139 /* Check for symbol */
88c91427 140 if (isalpha(name[0]) || name[0] == '_') {
49d4e302 141 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
8ab7c0d9 142 name);
49d4e302 143 if (match == 1) {
8f0d098b 144 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 145 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
146 DBG("probe symbol %s", ev->attr.probe.symbol_name);
147 ev->attr.probe.offset = 0;
148 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
149 ev->attr.probe.addr = 0;
150 goto end;
151 }
0d63dd19
DG
152 }
153
154 /* Check for address */
49d4e302
JRJ
155 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
156 if (match > 0) {
531be721
FD
157 /*
158 * Return an error if the first character of the tentative
159 * address is NULL or not a digit. It can be "0" if the address
160 * is in hexadecimal and can be 1 to 9 if it's in decimal.
161 */
162 if (*s_hex == '\0' || !isdigit(*s_hex)) {
163 ERR("Invalid probe description %s", s_hex);
49d4e302 164 ret = CMD_ERROR;
8f0d098b 165 goto end;
0d63dd19 166 }
8ff0bbd0 167 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 168 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
169 ev->attr.probe.offset = 0;
170 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 171 goto end;
0d63dd19
DG
172 }
173
174 /* No match */
49d4e302 175 ret = CMD_ERROR;
0d63dd19 176
8f0d098b 177end:
0d63dd19
DG
178 return ret;
179}
180
85076754
MD
181static
182const char *print_channel_name(const char *name)
183{
184 return name ? : DEFAULT_CHANNEL_NAME;
185}
186
187static
188const char *print_raw_channel_name(const char *name)
189{
190 return name ? : "<default>";
191}
192
89476427
JRJ
193/*
194 * Mi print exlcusion list
195 */
196static
e358ddd5 197int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
89476427 198{
e358ddd5
JG
199 int ret;
200 size_t i;
201 const size_t count = lttng_dynamic_pointer_array_get_count(exclusions);
89476427
JRJ
202
203 assert(writer);
204
205 if (count == 0) {
206 ret = 0;
207 goto end;
208 }
e358ddd5 209
89476427
JRJ
210 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
211 if (ret) {
212 goto end;
213 }
214
215 for (i = 0; i < count; i++) {
e358ddd5
JG
216 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
217 exclusions, i);
218
89476427 219 ret = mi_lttng_writer_write_element_string(writer,
e358ddd5 220 config_element_exclusion, exclusion);
89476427
JRJ
221 if (ret) {
222 goto end;
223 }
224 }
225
226 /* Close exclusions element */
227 ret = mi_lttng_writer_close_element(writer);
228
229end:
230 return ret;
231}
232
9c48cab3
JI
233/*
234 * Return allocated string for pretty-printing exclusion names.
235 */
236static
e358ddd5 237char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions)
9c48cab3
JI
238{
239 int length = 0;
e358ddd5 240 size_t i;
bfe36393 241 const char preamble[] = " excluding ";
9c48cab3 242 char *ret;
e358ddd5 243 const size_t count = lttng_dynamic_pointer_array_get_count(exclusions);
9c48cab3
JI
244
245 if (count == 0) {
246 return strdup("");
247 }
248
e358ddd5 249 /* Calculate total required length. */
9c48cab3 250 for (i = 0; i < count; i++) {
e358ddd5
JG
251 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
252 exclusions, i);
253
254 length += strlen(exclusion) + 4;
9c48cab3
JI
255 }
256
bfe36393
JG
257 length += sizeof(preamble);
258 ret = zmalloc(length);
fd591b18
MD
259 if (!ret) {
260 return NULL;
261 }
e358ddd5 262
9c48cab3
JI
263 strncpy(ret, preamble, length);
264 for (i = 0; i < count; i++) {
e358ddd5
JG
265 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
266 exclusions, i);
267
9f449915 268 strcat(ret, "\"");
e358ddd5 269 strcat(ret, exclusion);
9f449915 270 strcat(ret, "\"");
9c48cab3 271 if (i != count - 1) {
9f449915 272 strcat(ret, ", ");
9c48cab3
JI
273 }
274 }
89476427 275
9c48cab3
JI
276 return ret;
277}
278
748bde76 279static
9f449915 280int check_exclusion_subsets(const char *event_name, const char *exclusion)
748bde76 281{
9f449915
PP
282 bool warn = false;
283 int ret = 0;
284 const char *e = event_name;
285 const char *x = exclusion;
286
287 /* Scan both the excluder and the event letter by letter */
288 while (true) {
289 if (*e == '\\') {
290 if (*x != *e) {
291 warn = true;
292 goto end;
293 }
748bde76 294
9f449915
PP
295 e++;
296 x++;
297 goto cmp_chars;
298 }
299
300 if (*x == '*') {
301 /* Event is a subset of the excluder */
302 ERR("Event %s: %s excludes all events from %s",
303 event_name, exclusion, event_name);
304 goto error;
305 }
306
307 if (*e == '*') {
308 /*
309 * Reached the end of the event name before the
310 * end of the exclusion: this is valid.
311 */
312 goto end;
313 }
314
315cmp_chars:
316 if (*x != *e) {
317 warn = true;
318 break;
319 }
320
321 x++;
322 e++;
748bde76
JI
323 }
324
9f449915 325 goto end;
748bde76 326
9f449915
PP
327error:
328 ret = -1;
748bde76 329
9f449915
PP
330end:
331 if (warn) {
332 WARN("Event %s: %s does not exclude any events from %s",
333 event_name, exclusion, event_name);
334 }
748bde76 335
9f449915
PP
336 return ret;
337}
338
e358ddd5
JG
339int validate_exclusion_list(const char *event_name,
340 const struct lttng_dynamic_pointer_array *exclusions)
9f449915 341{
b03a81fb 342 int ret;
9f449915
PP
343
344 /* Event name must be a valid globbing pattern to allow exclusions. */
345 if (!strutils_is_star_glob_pattern(event_name)) {
346 ERR("Event %s: Exclusions can only be used with a globbing pattern",
347 event_name);
348 goto error;
349 }
350
9f449915
PP
351 /*
352 * If the event name is a star-at-end only globbing pattern,
353 * then we can validate the individual exclusions. Otherwise
354 * all exclusions are passed to the session daemon.
355 */
356 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
e358ddd5
JG
357 size_t i, num_exclusions;
358
359 num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
360
361 for (i = 0; i < num_exclusions; i++) {
362 const char *exclusion =
363 lttng_dynamic_pointer_array_get_pointer(
364 exclusions, i);
9f449915 365
e358ddd5
JG
366 if (!strutils_is_star_glob_pattern(exclusion) ||
367 strutils_is_star_at_the_end_only_glob_pattern(exclusion)) {
368 ret = check_exclusion_subsets(event_name, exclusion);
9f449915 369 if (ret) {
5ef79758
MD
370 goto error;
371 }
748bde76 372 }
748bde76
JI
373 }
374 }
9f449915 375
b03a81fb
SM
376 ret = 0;
377 goto end;
378
379error:
380 ret = -1;
381
382end:
383 return ret;
384}
385
386static int create_exclusion_list_and_validate(const char *event_name,
387 const char *exclusions_arg,
e358ddd5 388 struct lttng_dynamic_pointer_array *exclusions)
b03a81fb
SM
389{
390 int ret = 0;
b03a81fb
SM
391
392 /* Split exclusions. */
e358ddd5
JG
393 ret = strutils_split(exclusions_arg, ',', true, exclusions);
394 if (ret < 0) {
b03a81fb
SM
395 goto error;
396 }
397
e358ddd5 398 if (validate_exclusion_list(event_name, exclusions) !=
b03a81fb
SM
399 0) {
400 goto error;
401 }
402
748bde76 403 goto end;
9f449915 404
748bde76 405error:
9f449915 406 ret = -1;
e358ddd5 407 lttng_dynamic_pointer_array_reset(exclusions);
9f449915 408
748bde76 409end:
748bde76
JI
410 return ret;
411}
502bbe89 412
e358ddd5 413static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array *exclusions,
9f449915 414 int *warn)
502bbe89 415{
e358ddd5
JG
416 size_t i;
417 const size_t num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
418
419 for (i = 0; i < num_exclusions; i++) {
420 const char * const exclusion = lttng_dynamic_pointer_array_get_pointer(exclusions, i);
502bbe89 421
e358ddd5 422 if (strlen(exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
502bbe89 423 WARN("Event exclusion \"%s\" will be truncated",
e358ddd5 424 exclusion);
502bbe89
PP
425 *warn = 1;
426 }
427 }
428}
429
f3ed775e 430/*
6181537c 431 * Enabling event using the lttng API.
89476427 432 * Note: in case of error only the last error code will be return.
f3ed775e 433 */
cd80958d 434static int enable_events(char *session_name)
f3ed775e 435{
89476427
JRJ
436 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
437 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
b73d0b29 438 char *event_name, *channel_name = NULL;
91744e14 439 struct lttng_event *ev;
e358ddd5
JG
440 struct lttng_domain dom = {};
441 struct lttng_dynamic_pointer_array exclusions;
0c4e727b 442 struct lttng_userspace_probe_location *uprobe_loc = NULL;
f3ed775e 443
e358ddd5 444 lttng_dynamic_pointer_array_init(&exclusions, NULL);
441c16a7 445
91744e14
FD
446 ev = lttng_event_create();
447 if (!ev) {
448 ret = CMD_ERROR;
449 goto error;
450 }
451
53a80697 452 if (opt_kernel) {
67b58630
JG
453 if (opt_loglevel) {
454 WARN("Kernel loglevels are not supported.");
455 }
53a80697
MD
456 }
457
7d29a247
DG
458 /* Create lttng domain */
459 if (opt_kernel) {
460 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 461 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 462 } else if (opt_userspace) {
2bdd86d4 463 dom.type = LTTNG_DOMAIN_UST;
7972aab2 464 /* Default. */
8692d4e5 465 dom.buf_type = LTTNG_BUFFER_PER_UID;
b9dfb167
DG
466 } else if (opt_jul) {
467 dom.type = LTTNG_DOMAIN_JUL;
468 /* Default. */
469 dom.buf_type = LTTNG_BUFFER_PER_UID;
5cdb6027
DG
470 } else if (opt_log4j) {
471 dom.type = LTTNG_DOMAIN_LOG4J;
472 /* Default. */
473 dom.buf_type = LTTNG_BUFFER_PER_UID;
0e115563
DG
474 } else if (opt_python) {
475 dom.type = LTTNG_DOMAIN_PYTHON;
476 /* Default. */
477 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 478 } else {
3ecec76a
PP
479 /* Checked by the caller. */
480 assert(0);
2bdd86d4 481 }
7d29a247 482
8b08c525
AB
483 if (opt_exclude) {
484 switch (dom.type) {
485 case LTTNG_DOMAIN_KERNEL:
486 case LTTNG_DOMAIN_JUL:
487 case LTTNG_DOMAIN_LOG4J:
488 case LTTNG_DOMAIN_PYTHON:
489 ERR("Event name exclusions are not yet implemented for %s events",
1004b719 490 lttng_domain_type_str(dom.type));
8b08c525
AB
491 ret = CMD_ERROR;
492 goto error;
493 case LTTNG_DOMAIN_UST:
494 /* Exclusions supported */
495 break;
496 default:
497 assert(0);
498 }
d5dd17fd
JI
499 }
500
4fd2697f
FD
501 /*
502 * Adding a filter to a probe, function or userspace-probe would be
503 * denied by the kernel tracer as it's not supported at the moment. We
504 * do an early check here to warn the user.
505 */
506 if (opt_filter && opt_kernel) {
507 switch (opt_event_type) {
508 case LTTNG_EVENT_ALL:
509 case LTTNG_EVENT_TRACEPOINT:
510 case LTTNG_EVENT_SYSCALL:
511 break;
512 case LTTNG_EVENT_PROBE:
513 case LTTNG_EVENT_USERSPACE_PROBE:
514 case LTTNG_EVENT_FUNCTION:
515 ERR("Filter expressions are not supported for %s events",
516 get_event_type_str(opt_event_type));
517 ret = CMD_ERROR;
518 goto error;
519 default:
520 ret = CMD_UNDEFINED;
521 goto error;
522 }
523 }
524
85076754 525 channel_name = opt_channel_name;
ae856491 526
cd80958d
DG
527 handle = lttng_create_handle(session_name, &dom);
528 if (handle == NULL) {
529 ret = -1;
530 goto error;
531 }
1aef21b6 532
89476427
JRJ
533 /* Prepare Mi */
534 if (lttng_opt_mi) {
535 /* Open a events element */
536 ret = mi_lttng_writer_open_element(writer, config_element_events);
537 if (ret) {
538 ret = CMD_ERROR;
539 goto error;
540 }
541 }
542
cd80958d 543 if (opt_enable_all) {
8c9ae521 544 /* Default setup for enable all */
75e8c5ab 545 if (opt_kernel) {
91744e14
FD
546 ev->type = opt_event_type;
547 strcpy(ev->name, "*");
300b8fd5 548 /* kernel loglevels not implemented */
91744e14 549 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab 550 } else {
91744e14
FD
551 ev->type = LTTNG_EVENT_TRACEPOINT;
552 strcpy(ev->name, "*");
553 ev->loglevel_type = opt_loglevel_type;
300b8fd5 554 if (opt_loglevel) {
7e8f2e9c
JG
555 int name_search_ret;
556
0e115563 557 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
7e8f2e9c 558
b2064f54 559 if (opt_userspace) {
7e8f2e9c
JG
560 enum lttng_loglevel loglevel;
561
562 name_search_ret = loglevel_name_to_value(opt_loglevel, &loglevel);
563 ev->loglevel = (int) loglevel;
b2064f54 564 } else if (opt_jul) {
7e8f2e9c
JG
565 enum lttng_loglevel_jul loglevel;
566
567 name_search_ret = loglevel_jul_name_to_value(opt_loglevel, &loglevel);
568 ev->loglevel = (int) loglevel;
5cdb6027 569 } else if (opt_log4j) {
7e8f2e9c
JG
570 enum lttng_loglevel_log4j loglevel;
571
572 name_search_ret = loglevel_log4j_name_to_value(opt_loglevel, &loglevel);
573 ev->loglevel = (int) loglevel;
7fefa907
JG
574 } else {
575 /* python domain. */
7e8f2e9c
JG
576 enum lttng_loglevel_python loglevel;
577
578 name_search_ret = loglevel_python_name_to_value(opt_loglevel, &loglevel);
579 ev->loglevel = (int) loglevel;
b2064f54 580 }
7e8f2e9c
JG
581
582 if (name_search_ret == -1) {
300b8fd5 583 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 584 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
585 goto error;
586 }
22e25b71 587 } else {
0e115563 588 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54 589 if (opt_userspace) {
91744e14 590 ev->loglevel = -1;
34aa3685 591 } else if (opt_jul) {
91744e14 592 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
34aa3685 593 } else if (opt_log4j) {
91744e14 594 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 595 } else if (opt_python) {
91744e14 596 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
b2064f54 597 }
300b8fd5 598 }
75e8c5ab 599 }
8c9ae521 600
7ed70bc9 601 if (opt_exclude) {
9f449915 602 ret = create_exclusion_list_and_validate("*",
e358ddd5 603 opt_exclude, &exclusions);
9f449915
PP
604 if (ret) {
605 ret = CMD_ERROR;
7ed70bc9
JI
606 goto error;
607 }
502bbe89 608
91744e14 609 ev->exclusion = 1;
e358ddd5 610 warn_on_truncated_exclusion_names(&exclusions,
9f449915 611 &warn);
7ed70bc9 612 }
025faf73 613 if (!opt_filter) {
7ed70bc9 614 ret = lttng_enable_event_with_exclusions(handle,
91744e14 615 ev, channel_name,
7ed70bc9 616 NULL,
e358ddd5
JG
617 lttng_dynamic_pointer_array_get_count(&exclusions),
618 (char **) exclusions.array.buffer.data);
025faf73
DG
619 if (ret < 0) {
620 switch (-ret) {
621 case LTTNG_ERR_KERN_EVENT_EXIST:
622 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 623 print_channel_name(channel_name), session_name);
89476427 624 warn = 1;
025faf73 625 break;
45d5d421
CB
626 case LTTNG_ERR_TRACE_ALREADY_STARTED:
627 {
628 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
629 ERR("Events: %s (channel %s, session %s)",
630 msg,
631 print_channel_name(channel_name),
632 session_name);
633 error = 1;
634 break;
635 }
025faf73
DG
636 default:
637 ERR("Events: %s (channel %s, session %s)",
85076754
MD
638 lttng_strerror(ret),
639 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
640 ? print_raw_channel_name(channel_name)
641 : print_channel_name(channel_name),
642 session_name);
89476427 643 error = 1;
025faf73
DG
644 break;
645 }
646 goto end;
42224349 647 }
8c9ae521 648
025faf73
DG
649 switch (opt_event_type) {
650 case LTTNG_EVENT_TRACEPOINT:
67b58630 651 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
e358ddd5 652 char *exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
653
654 if (!exclusion_string) {
655 PERROR("Cannot allocate exclusion_string");
656 error = 1;
657 goto end;
658 }
9c48cab3 659 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
1004b719 660 lttng_domain_type_str(dom.type),
9c48cab3 661 exclusion_string,
85076754 662 print_channel_name(channel_name),
025faf73 663 opt_loglevel);
9c48cab3 664 free(exclusion_string);
025faf73 665 } else {
e358ddd5 666 char *exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
667
668 if (!exclusion_string) {
669 PERROR("Cannot allocate exclusion_string");
670 error = 1;
671 goto end;
672 }
9c48cab3 673 MSG("All %s tracepoints%s are enabled in channel %s",
1004b719 674 lttng_domain_type_str(dom.type),
9c48cab3 675 exclusion_string,
85076754 676 print_channel_name(channel_name));
9c48cab3 677 free(exclusion_string);
025faf73
DG
678 }
679 break;
680 case LTTNG_EVENT_SYSCALL:
681 if (opt_kernel) {
6e911cad 682 MSG("All %s system calls are enabled in channel %s",
1004b719 683 lttng_domain_type_str(dom.type),
85076754 684 print_channel_name(channel_name));
025faf73
DG
685 }
686 break;
687 case LTTNG_EVENT_ALL:
67b58630 688 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
e358ddd5 689 char *exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
690
691 if (!exclusion_string) {
692 PERROR("Cannot allocate exclusion_string");
693 error = 1;
694 goto end;
695 }
9c48cab3 696 MSG("All %s events%s are enabled in channel %s for loglevel %s",
1004b719 697 lttng_domain_type_str(dom.type),
9c48cab3 698 exclusion_string,
85076754 699 print_channel_name(channel_name),
025faf73 700 opt_loglevel);
9c48cab3 701 free(exclusion_string);
025faf73 702 } else {
e358ddd5 703 char *exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
704
705 if (!exclusion_string) {
706 PERROR("Cannot allocate exclusion_string");
707 error = 1;
708 goto end;
709 }
9c48cab3 710 MSG("All %s events%s are enabled in channel %s",
1004b719 711 lttng_domain_type_str(dom.type),
9c48cab3 712 exclusion_string,
85076754 713 print_channel_name(channel_name));
9c48cab3 714 free(exclusion_string);
025faf73
DG
715 }
716 break;
717 default:
718 /*
719 * We should not be here since lttng_enable_event should have
720 * failed on the event type.
721 */
722 goto error;
57064ada 723 }
f3ed775e 724 }
89476427 725
025faf73 726 if (opt_filter) {
91744e14 727 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
e358ddd5
JG
728 opt_filter,
729 lttng_dynamic_pointer_array_get_count(&exclusions),
730 (char **) exclusions.array.buffer.data);
89476427
JRJ
731 if (command_ret < 0) {
732 switch (-command_ret) {
16363652 733 case LTTNG_ERR_FILTER_EXIST:
85076754 734 WARN("Filter on all events is already enabled"
16363652 735 " (channel %s, session %s)",
85076754 736 print_channel_name(channel_name), session_name);
89476427 737 warn = 1;
16363652 738 break;
45d5d421
CB
739 case LTTNG_ERR_TRACE_ALREADY_STARTED:
740 {
741 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
742 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
743 msg,
744 print_channel_name(channel_name),
745 session_name, opt_filter);
746 error = 1;
747 break;
748 }
16363652 749 default:
85076754 750 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
da3d7d0e
JRJ
751 lttng_strerror(command_ret),
752 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
753 ? print_raw_channel_name(channel_name)
754 : print_channel_name(channel_name),
755 session_name, opt_filter);
89476427 756 error = 1;
16363652
DG
757 break;
758 }
89476427 759 error_holder = command_ret;
16363652 760 } else {
91744e14 761 ev->filter = 1;
16363652
DG
762 MSG("Filter '%s' successfully set", opt_filter);
763 }
764 }
89476427
JRJ
765
766 if (lttng_opt_mi) {
767 /* The wildcard * is used for kernel and ust domain to
768 * represent ALL. We copy * in event name to force the wildcard use
769 * for kernel domain
770 *
771 * Note: this is strictly for semantic and printing while in
772 * machine interface mode.
773 */
91744e14 774 strcpy(ev->name, "*");
89476427
JRJ
775
776 /* If we reach here the events are enabled */
777 if (!error && !warn) {
91744e14 778 ev->enabled = 1;
89476427 779 } else {
91744e14 780 ev->enabled = 0;
89476427
JRJ
781 success = 0;
782 }
91744e14 783 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
784 if (ret) {
785 ret = CMD_ERROR;
786 goto error;
787 }
788
789 /* print exclusion */
e358ddd5 790 ret = mi_print_exclusion(&exclusions);
89476427
JRJ
791 if (ret) {
792 ret = CMD_ERROR;
793 goto error;
794 }
795
796 /* Success ? */
797 ret = mi_lttng_writer_write_element_bool(writer,
798 mi_lttng_element_command_success, success);
799 if (ret) {
800 ret = CMD_ERROR;
801 goto error;
802 }
803
804 /* Close event element */
805 ret = mi_lttng_writer_close_element(writer);
806 if (ret) {
807 ret = CMD_ERROR;
808 goto error;
809 }
810 }
811
8c9ae521 812 goto end;
f3ed775e
DG
813 }
814
815 /* Strip event list */
816 event_name = strtok(opt_event_list, ",");
817 while (event_name != NULL) {
6181537c 818 /* Copy name and type of the event */
91744e14
FD
819 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
820 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
821 ev->type = opt_event_type;
6181537c 822
f3ed775e
DG
823 /* Kernel tracer action */
824 if (opt_kernel) {
825 DBG("Enabling kernel event %s for channel %s",
85076754
MD
826 event_name,
827 print_channel_name(channel_name));
f3ed775e
DG
828
829 switch (opt_event_type) {
29c62722
MD
830 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
831 /* If event name differs from *, select tracepoint. */
91744e14
FD
832 if (strcmp(ev->name, "*")) {
833 ev->type = LTTNG_EVENT_TRACEPOINT;
29c62722
MD
834 }
835 break;
e6ddca71 836 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 837 break;
7d29a247 838 case LTTNG_EVENT_PROBE:
91744e14 839 ret = parse_probe_opts(ev, opt_probe);
49d4e302 840 if (ret) {
cf0e5467 841 ERR("Unable to parse probe options");
91744e14 842 ret = CMD_ERROR;
0d63dd19
DG
843 goto error;
844 }
f3ed775e 845 break;
dcabc190 846 case LTTNG_EVENT_USERSPACE_PROBE:
0c4e727b
JR
847 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
848
849 ret = parse_userspace_probe_opts(opt_userspace_probe, &uprobe_loc);
dcabc190 850 if (ret) {
36aa2f64
FD
851 switch (ret) {
852 case CMD_UNSUPPORTED:
853 /*
854 * Error message describing
855 * what is not supported was
856 * printed in the function.
857 */
858 break;
859 case CMD_ERROR:
860 default:
861 ERR("Unable to parse userspace probe options");
862 break;
863 }
dcabc190
FD
864 goto error;
865 }
0c4e727b
JR
866
867 ret = lttng_event_set_userspace_probe_location(ev, uprobe_loc);
868 if (ret) {
869 WARN("Failed to set probe location on event");
870 ret = CMD_ERROR;
871 goto error;
872 }
873
874 /* Ownership of the uprobe location was transferred to the event. */
875 uprobe_loc = NULL;
dcabc190 876 break;
f3ed775e 877 case LTTNG_EVENT_FUNCTION:
91744e14 878 ret = parse_probe_opts(ev, opt_function);
49d4e302 879 if (ret) {
8f0d098b 880 ERR("Unable to parse function probe options");
91744e14 881 ret = CMD_ERROR;
8f0d098b
MD
882 goto error;
883 }
884 break;
a54bd42d 885 case LTTNG_EVENT_SYSCALL:
91744e14 886 ev->type = LTTNG_EVENT_SYSCALL;
c6aa2d41 887 break;
f3ed775e 888 default:
1ab1ea0b 889 ret = CMD_UNDEFINED;
f3ed775e
DG
890 goto error;
891 }
0cda4f28 892
0cda4f28 893 /* kernel loglevels not implemented */
91744e14 894 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 895 } else if (opt_userspace) { /* User-space tracer action */
300b8fd5 896 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 897 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
898
899 switch (opt_event_type) {
900 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
901 /* Fall-through */
902 case LTTNG_EVENT_TRACEPOINT:
e4baff1e 903 /* Copy name and type of the event */
91744e14
FD
904 ev->type = LTTNG_EVENT_TRACEPOINT;
905 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
906 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
907 break;
908 case LTTNG_EVENT_PROBE:
909 case LTTNG_EVENT_FUNCTION:
2bdd86d4 910 case LTTNG_EVENT_SYSCALL:
dcabc190 911 case LTTNG_EVENT_USERSPACE_PROBE:
2bdd86d4 912 default:
cc62c0c0 913 ERR("Event type not available for user-space tracing");
4ce78777 914 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
915 goto error;
916 }
0cda4f28 917
7ed70bc9 918 if (opt_exclude) {
91744e14 919 ev->exclusion = 1;
d5dd17fd
JI
920 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
921 ERR("Exclusion option can only be used with tracepoint events");
922 ret = CMD_ERROR;
923 goto error;
924 }
e358ddd5
JG
925 /* Free previously allocated items. */
926 lttng_dynamic_pointer_array_reset(&exclusions);
9f449915
PP
927 ret = create_exclusion_list_and_validate(
928 event_name, opt_exclude,
e358ddd5 929 &exclusions);
9f449915
PP
930 if (ret) {
931 ret = CMD_ERROR;
7ed70bc9
JI
932 goto error;
933 }
502bbe89
PP
934
935 warn_on_truncated_exclusion_names(
e358ddd5 936 &exclusions, &warn);
7ed70bc9
JI
937 }
938
91744e14 939 ev->loglevel_type = opt_loglevel_type;
ed7f4083 940 if (opt_loglevel) {
7e8f2e9c
JG
941 enum lttng_loglevel loglevel;
942 const int name_search_ret = loglevel_name_to_value(opt_loglevel, &loglevel);
943
944 if (name_search_ret == -1) {
8005f29a 945 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 946 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
947 goto error;
948 }
7e8f2e9c
JG
949
950 ev->loglevel = (int) loglevel;
22e25b71 951 } else {
91744e14 952 ev->loglevel = -1;
ed7f4083 953 }
0e115563 954 } else if (opt_jul || opt_log4j || opt_python) {
b9dfb167
DG
955 if (opt_event_type != LTTNG_EVENT_ALL &&
956 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
5cdb6027 957 ERR("Event type not supported for domain.");
b9dfb167
DG
958 ret = CMD_UNSUPPORTED;
959 goto error;
960 }
b2064f54 961
91744e14 962 ev->loglevel_type = opt_loglevel_type;
b2064f54 963 if (opt_loglevel) {
7e8f2e9c
JG
964 int name_search_ret;
965
5cdb6027 966 if (opt_jul) {
7e8f2e9c
JG
967 enum lttng_loglevel_jul loglevel;
968
969 name_search_ret = loglevel_jul_name_to_value(opt_loglevel, &loglevel);
970 ev->loglevel = (int) loglevel;
5cdb6027 971 } else if (opt_log4j) {
7e8f2e9c
JG
972 enum lttng_loglevel_log4j loglevel;
973
974 name_search_ret = loglevel_log4j_name_to_value(opt_loglevel, &loglevel);
975 ev->loglevel = (int) loglevel;
7fefa907
JG
976 } else {
977 /* python domain. */
7e8f2e9c
JG
978 enum lttng_loglevel_python loglevel;
979
980 name_search_ret = loglevel_python_name_to_value(opt_loglevel, &loglevel);
981 ev->loglevel = (int) loglevel;
5cdb6027 982 }
7e8f2e9c
JG
983
984 if (name_search_ret) {
b2064f54
DG
985 ERR("Unknown loglevel %s", opt_loglevel);
986 ret = -LTTNG_ERR_INVALID;
987 goto error;
988 }
989 } else {
5cdb6027 990 if (opt_jul) {
91744e14 991 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
5cdb6027 992 } else if (opt_log4j) {
91744e14 993 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 994 } else if (opt_python) {
91744e14 995 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
5cdb6027 996 }
b2064f54 997 }
91744e14
FD
998 ev->type = LTTNG_EVENT_TRACEPOINT;
999 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1000 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1001 } else {
3ecec76a 1002 assert(0);
f3ed775e
DG
1003 }
1004
025faf73 1005 if (!opt_filter) {
9c48cab3
JI
1006 char *exclusion_string;
1007
89476427 1008 command_ret = lttng_enable_event_with_exclusions(handle,
91744e14 1009 ev, channel_name,
9f449915 1010 NULL,
e358ddd5
JG
1011 lttng_dynamic_pointer_array_get_count(&exclusions),
1012 (char **) exclusions.array.buffer.data);
1013 exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
1014 if (!exclusion_string) {
1015 PERROR("Cannot allocate exclusion_string");
1016 error = 1;
1017 goto end;
1018 }
89476427 1019 if (command_ret < 0) {
025faf73 1020 /* Turn ret to positive value to handle the positive error code */
89476427 1021 switch (-command_ret) {
025faf73 1022 case LTTNG_ERR_KERN_EVENT_EXIST:
9c48cab3 1023 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
85076754 1024 event_name,
9c48cab3 1025 exclusion_string,
85076754 1026 print_channel_name(channel_name), session_name);
89476427 1027 warn = 1;
025faf73 1028 break;
45d5d421
CB
1029 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1030 {
1031 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1032 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1033 exclusion_string,
1034 msg,
1035 print_channel_name(channel_name),
1036 session_name);
1037 error = 1;
1038 break;
1039 }
dcabc190
FD
1040 case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
1041 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1042 event_name, print_channel_name(channel_name),
1043 session_name);
1044 error = 1;
1045 break;
025faf73 1046 default:
9c48cab3
JI
1047 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1048 exclusion_string,
da3d7d0e
JRJ
1049 lttng_strerror(command_ret),
1050 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1051 ? print_raw_channel_name(channel_name)
1052 : print_channel_name(channel_name),
1053 session_name);
89476427 1054 error = 1;
025faf73
DG
1055 break;
1056 }
89476427 1057 error_holder = command_ret;
025faf73 1058 } else {
8274eeba
AB
1059 switch (dom.type) {
1060 case LTTNG_DOMAIN_KERNEL:
1061 case LTTNG_DOMAIN_UST:
49ceaa70 1062 MSG("%s event %s%s created in channel %s",
1004b719 1063 lttng_domain_type_str(dom.type),
8274eeba
AB
1064 event_name,
1065 exclusion_string,
1066 print_channel_name(channel_name));
1067 break;
1068 case LTTNG_DOMAIN_JUL:
1069 case LTTNG_DOMAIN_LOG4J:
1070 case LTTNG_DOMAIN_PYTHON:
1071 /*
1072 * Don't print the default channel
1073 * name for agent domains.
1074 */
895707da 1075 MSG("%s event %s%s enabled",
1004b719 1076 lttng_domain_type_str(dom.type),
8274eeba
AB
1077 event_name,
1078 exclusion_string);
1079 break;
1080 default:
1081 assert(0);
49ceaa70 1082 }
42224349 1083 }
9c48cab3 1084 free(exclusion_string);
6181537c 1085 }
025faf73
DG
1086
1087 if (opt_filter) {
9c48cab3
JI
1088 char *exclusion_string;
1089
89476427 1090 /* Filter present */
91744e14 1091 ev->filter = 1;
89476427 1092
91744e14 1093 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
9f449915 1094 opt_filter,
e358ddd5
JG
1095 lttng_dynamic_pointer_array_get_count(&exclusions),
1096 (char **) exclusions.array.buffer.data);
1097 exclusion_string = print_exclusions(&exclusions);
5ef79758
MD
1098 if (!exclusion_string) {
1099 PERROR("Cannot allocate exclusion_string");
1100 error = 1;
1101 goto end;
1102 }
89476427
JRJ
1103 if (command_ret < 0) {
1104 switch (-command_ret) {
7671f53c 1105 case LTTNG_ERR_FILTER_EXIST:
9c48cab3 1106 WARN("Filter on event %s%s is already enabled"
7671f53c 1107 " (channel %s, session %s)",
85076754 1108 event_name,
9c48cab3 1109 exclusion_string,
85076754 1110 print_channel_name(channel_name), session_name);
89476427 1111 warn = 1;
7671f53c 1112 break;
45d5d421
CB
1113 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1114 {
1115 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
91744e14 1116 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
45d5d421
CB
1117 exclusion_string,
1118 msg,
1119 print_channel_name(channel_name),
1120 session_name, opt_filter);
1121 error = 1;
1122 break;
1123 }
7671f53c 1124 default:
91744e14 1125 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
9c48cab3 1126 exclusion_string,
da3d7d0e
JRJ
1127 lttng_strerror(command_ret),
1128 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1129 ? print_raw_channel_name(channel_name)
1130 : print_channel_name(channel_name),
1131 session_name, opt_filter);
89476427 1132 error = 1;
7671f53c
CB
1133 break;
1134 }
89476427
JRJ
1135 error_holder = command_ret;
1136
16363652 1137 } else {
9c48cab3
JI
1138 MSG("Event %s%s: Filter '%s' successfully set",
1139 event_name, exclusion_string,
1140 opt_filter);
53a80697 1141 }
9c48cab3 1142 free(exclusion_string);
53a80697 1143 }
6181537c 1144
89476427
JRJ
1145 if (lttng_opt_mi) {
1146 if (command_ret) {
1147 success = 0;
91744e14 1148 ev->enabled = 0;
89476427 1149 } else {
91744e14 1150 ev->enabled = 1;
89476427
JRJ
1151 }
1152
91744e14 1153 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
1154 if (ret) {
1155 ret = CMD_ERROR;
1156 goto error;
1157 }
1158
1159 /* print exclusion */
e358ddd5 1160 ret = mi_print_exclusion(&exclusions);
89476427
JRJ
1161 if (ret) {
1162 ret = CMD_ERROR;
1163 goto error;
1164 }
1165
1166 /* Success ? */
1167 ret = mi_lttng_writer_write_element_bool(writer,
1168 mi_lttng_element_command_success, success);
1169 if (ret) {
1170 ret = CMD_ERROR;
1171 goto end;
1172 }
1173
1174 /* Close event element */
1175 ret = mi_lttng_writer_close_element(writer);
1176 if (ret) {
1177 ret = CMD_ERROR;
1178 goto end;
1179 }
1180 }
1181
f3ed775e
DG
1182 /* Next event */
1183 event_name = strtok(NULL, ",");
89476427
JRJ
1184 /* Reset warn, error and success */
1185 success = 1;
f3ed775e
DG
1186 }
1187
8c9ae521 1188end:
89476427
JRJ
1189 /* Close Mi */
1190 if (lttng_opt_mi) {
1191 /* Close events element */
1192 ret = mi_lttng_writer_close_element(writer);
1193 if (ret) {
1194 ret = CMD_ERROR;
1195 goto error;
1196 }
1197 }
f3ed775e 1198error:
ae856491
DG
1199 if (warn) {
1200 ret = CMD_WARNING;
1201 }
89476427
JRJ
1202 if (error) {
1203 ret = CMD_ERROR;
1204 }
cd80958d 1205 lttng_destroy_handle(handle);
e358ddd5 1206 lttng_dynamic_pointer_array_reset(&exclusions);
0c4e727b 1207 lttng_userspace_probe_location_destroy(uprobe_loc);
7ed70bc9 1208
89476427
JRJ
1209 /* Overwrite ret with error_holder if there was an actual error with
1210 * enabling an event.
1211 */
1212 ret = error_holder ? error_holder : ret;
1213
91744e14 1214 lttng_event_destroy(ev);
f3ed775e
DG
1215 return ret;
1216}
1217
1218/*
6181537c 1219 * Add event to trace session
f3ed775e
DG
1220 */
1221int cmd_enable_events(int argc, const char **argv)
1222{
89476427 1223 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 1224 static poptContext pc;
cd80958d 1225 char *session_name = NULL;
68c7f6e5 1226 const char *leftover = NULL;
de044b7a 1227 int event_type = -1;
f3ed775e
DG
1228
1229 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1230 poptReadDefaultConfig(pc, 0);
1231
1232 /* Default event type */
7a3d1328 1233 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
1234
1235 while ((opt = poptGetNextOpt(pc)) != -1) {
1236 switch (opt) {
1237 case OPT_HELP:
4ba92f18 1238 SHOW_HELP();
f3ed775e 1239 goto end;
f3ed775e 1240 case OPT_TRACEPOINT:
e6ddca71 1241 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 1242 break;
cf0e5467 1243 case OPT_PROBE:
7d29a247 1244 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e 1245 break;
dcabc190
FD
1246 case OPT_USERSPACE_PROBE:
1247 opt_event_type = LTTNG_EVENT_USERSPACE_PROBE;
1248 break;
f3ed775e
DG
1249 case OPT_FUNCTION:
1250 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b 1251 break;
a54bd42d
MD
1252 case OPT_SYSCALL:
1253 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 1254 break;
eeac7d46
MD
1255 case OPT_USERSPACE:
1256 opt_userspace = 1;
eeac7d46 1257 break;
0cda4f28 1258 case OPT_LOGLEVEL:
8005f29a 1259 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
1260 opt_loglevel = poptGetOptArg(pc);
1261 break;
1262 case OPT_LOGLEVEL_ONLY:
8005f29a 1263 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 1264 opt_loglevel = poptGetOptArg(pc);
13dce3b7 1265 break;
679b4943
SM
1266 case OPT_LIST_OPTIONS:
1267 list_cmd_options(stdout, long_options);
679b4943 1268 goto end;
53a80697
MD
1269 case OPT_FILTER:
1270 break;
fac3366c
JI
1271 case OPT_EXCLUDE:
1272 break;
f3ed775e 1273 default:
f3ed775e
DG
1274 ret = CMD_UNDEFINED;
1275 goto end;
1276 }
de044b7a
DG
1277
1278 /* Validate event type. Multiple event type are not supported. */
1279 if (event_type == -1) {
1280 event_type = opt_event_type;
1281 } else {
1282 if (event_type != opt_event_type) {
1283 ERR("Multiple event type not supported.");
1284 ret = CMD_ERROR;
1285 goto end;
1286 }
1287 }
f3ed775e
DG
1288 }
1289
3ecec76a 1290 ret = print_missing_or_multiple_domains(
3533d06b
JG
1291 opt_kernel + opt_userspace + opt_jul + opt_log4j +
1292 opt_python,
1293 true);
3ecec76a
PP
1294 if (ret) {
1295 ret = CMD_ERROR;
1296 goto end;
1297 }
1298
89476427
JRJ
1299 /* Mi check */
1300 if (lttng_opt_mi) {
1301 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1302 if (!writer) {
1303 ret = -LTTNG_ERR_NOMEM;
1304 goto end;
1305 }
1306
1307 /* Open command element */
1308 ret = mi_lttng_writer_command_open(writer,
1309 mi_lttng_element_command_enable_event);
1310 if (ret) {
1311 ret = CMD_ERROR;
1312 goto end;
1313 }
1314
1315 /* Open output element */
1316 ret = mi_lttng_writer_open_element(writer,
1317 mi_lttng_element_command_output);
1318 if (ret) {
1319 ret = CMD_ERROR;
1320 goto end;
1321 }
1322 }
1323
f3ed775e
DG
1324 opt_event_list = (char*) poptGetArg(pc);
1325 if (opt_event_list == NULL && opt_enable_all == 0) {
1326 ERR("Missing event name(s).\n");
ca1c3607 1327 ret = CMD_ERROR;
f3ed775e
DG
1328 goto end;
1329 }
1330
68c7f6e5
JD
1331 leftover = poptGetArg(pc);
1332 if (leftover) {
1333 ERR("Unknown argument: %s", leftover);
1334 ret = CMD_ERROR;
1335 goto end;
1336 }
1337
cd80958d
DG
1338 if (!opt_session_name) {
1339 session_name = get_session_name();
1340 if (session_name == NULL) {
89476427
JRJ
1341 command_ret = CMD_ERROR;
1342 success = 0;
1343 goto mi_closing;
cd80958d
DG
1344 }
1345 } else {
1346 session_name = opt_session_name;
1347 }
1348
89476427
JRJ
1349 command_ret = enable_events(session_name);
1350 if (command_ret) {
1351 success = 0;
1352 goto mi_closing;
1353 }
1354
1355mi_closing:
1356 /* Mi closing */
1357 if (lttng_opt_mi) {
1358 /* Close output element */
1359 ret = mi_lttng_writer_close_element(writer);
1360 if (ret) {
1361 ret = CMD_ERROR;
1362 goto end;
1363 }
1364
1365 ret = mi_lttng_writer_write_element_bool(writer,
1366 mi_lttng_element_command_success, success);
1367 if (ret) {
1368 ret = CMD_ERROR;
1369 goto end;
1370 }
1371
1372 /* Command element close */
1373 ret = mi_lttng_writer_command_close(writer);
1374 if (ret) {
1375 ret = CMD_ERROR;
1376 goto end;
1377 }
1378 }
f3ed775e
DG
1379
1380end:
89476427
JRJ
1381 /* Mi clean-up */
1382 if (writer && mi_lttng_writer_destroy(writer)) {
1383 /* Preserve original error code */
1384 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1385 }
1386
cd80958d
DG
1387 if (opt_session_name == NULL) {
1388 free(session_name);
1389 }
1390
89476427
JRJ
1391 /* Overwrite ret if an error occurred in enable_events */
1392 ret = command_ret ? command_ret : ret;
1393
ca1c3607 1394 poptFreeContext(pc);
f3ed775e
DG
1395 return ret;
1396}
9f449915 1397
This page took 0.234533 seconds and 4 git commands to generate.