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