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