Fix all -Wdiscarded-qualifiers warning instances
[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
29#include "../command.h"
30
8ab7c0d9
MD
31#if (LTTNG_SYMBOL_NAME_LEN == 256)
32#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
33#endif
34
f3ed775e
DG
35static char *opt_event_list;
36static int opt_event_type;
0cda4f28
MD
37static const char *opt_loglevel;
38static int opt_loglevel_type;
6181537c 39static int opt_kernel;
5440dc42 40static char *opt_session_name;
f3ed775e 41static int opt_userspace;
b9dfb167 42static int opt_jul;
5cdb6027 43static int opt_log4j;
0e115563 44static int opt_python;
f3ed775e 45static int opt_enable_all;
cf0e5467 46static char *opt_probe;
dcabc190 47static char *opt_userspace_probe;
8f0d098b 48static char *opt_function;
f3ed775e 49static char *opt_channel_name;
53a80697 50static char *opt_filter;
fac3366c 51static char *opt_exclude;
f3ed775e 52
4fc83d94
PP
53#ifdef LTTNG_EMBED_HELP
54static const char help_msg[] =
55#include <lttng-enable-event.1.h>
56;
57#endif
58
f3ed775e
DG
59enum {
60 OPT_HELP = 1,
f3ed775e 61 OPT_TRACEPOINT,
cf0e5467 62 OPT_PROBE,
dcabc190 63 OPT_USERSPACE_PROBE,
f3ed775e 64 OPT_FUNCTION,
a54bd42d 65 OPT_SYSCALL,
eeac7d46 66 OPT_USERSPACE,
0cda4f28
MD
67 OPT_LOGLEVEL,
68 OPT_LOGLEVEL_ONLY,
679b4943 69 OPT_LIST_OPTIONS,
53a80697 70 OPT_FILTER,
fac3366c 71 OPT_EXCLUDE,
f3ed775e
DG
72};
73
cd80958d 74static struct lttng_handle *handle;
89476427 75static struct mi_writer *writer;
cd80958d 76
f3ed775e
DG
77static struct poptOption long_options[] = {
78 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
79 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 80 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 81 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
82 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
83 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 84 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
b9dfb167 85 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 86 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 87 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
f3ed775e 88 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 89 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
dcabc190 90 {"userspace-probe",0, POPT_ARG_STRING, &opt_userspace_probe, OPT_USERSPACE_PROBE, 0, 0},
40e9d5d3 91 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
7ebae521 92 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
93 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
94 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 95 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 96 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
fac3366c 97 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
f3ed775e
DG
98 {0, 0, 0, 0, 0, 0, 0}
99};
100
0d63dd19 101/*
6181537c 102 * Parse probe options.
0d63dd19 103 */
cf0e5467 104static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19 105{
49d4e302
JRJ
106 int ret = CMD_SUCCESS;
107 int match;
8ff0bbd0 108 char s_hex[19];
8ab7c0d9 109#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
0d63dd19
DG
110 char name[LTTNG_SYMBOL_NAME_LEN];
111
112 if (opt == NULL) {
49d4e302 113 ret = CMD_ERROR;
8f0d098b 114 goto end;
0d63dd19
DG
115 }
116
117 /* Check for symbol+offset */
49d4e302 118 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
8ab7c0d9 119 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
49d4e302 120 if (match == 2) {
7d29a247 121 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 122 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 123 DBG("probe symbol %s", ev->attr.probe.symbol_name);
9d035200 124 if (*s_hex == '\0') {
8ff0bbd0 125 ERR("Invalid probe offset %s", s_hex);
49d4e302 126 ret = CMD_ERROR;
8f0d098b 127 goto end;
0d63dd19 128 }
8ff0bbd0 129 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 130 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 131 ev->attr.probe.addr = 0;
8f0d098b
MD
132 goto end;
133 }
134
135 /* Check for symbol */
88c91427 136 if (isalpha(name[0]) || name[0] == '_') {
49d4e302 137 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
8ab7c0d9 138 name);
49d4e302 139 if (match == 1) {
8f0d098b 140 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 141 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
142 DBG("probe symbol %s", ev->attr.probe.symbol_name);
143 ev->attr.probe.offset = 0;
144 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
145 ev->attr.probe.addr = 0;
146 goto end;
147 }
0d63dd19
DG
148 }
149
150 /* Check for address */
49d4e302
JRJ
151 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
152 if (match > 0) {
531be721
FD
153 /*
154 * Return an error if the first character of the tentative
155 * address is NULL or not a digit. It can be "0" if the address
156 * is in hexadecimal and can be 1 to 9 if it's in decimal.
157 */
158 if (*s_hex == '\0' || !isdigit(*s_hex)) {
159 ERR("Invalid probe description %s", s_hex);
49d4e302 160 ret = CMD_ERROR;
8f0d098b 161 goto end;
0d63dd19 162 }
8ff0bbd0 163 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 164 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
165 ev->attr.probe.offset = 0;
166 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 167 goto end;
0d63dd19
DG
168 }
169
170 /* No match */
49d4e302 171 ret = CMD_ERROR;
0d63dd19 172
8f0d098b 173end:
0d63dd19
DG
174 return ret;
175}
176
dbc478f3
FD
177/*
178 * Walk the directories in the PATH environment variable to find the target
179 * binary passed as parameter.
180 *
181 * On success, the full path of the binary is copied in binary_full_path out
182 * parameter. This buffer is allocated by the caller and must be at least
183 * LTTNG_PATH_MAX bytes long.
184 * On failure, returns -1;
185 */
186static int walk_command_search_path(const char *binary, char *binary_full_path)
187{
188 char *tentative_binary_path = NULL;
189 char *command_search_path = NULL;
190 char *curr_search_dir_end = NULL;
191 char *curr_search_dir = NULL;
192 struct stat stat_output;
193 int ret = 0;
194
195 command_search_path = lttng_secure_getenv("PATH");
196 if (!command_search_path) {
197 ret = -1;
198 goto end;
199 }
200
201 /*
202 * Duplicate the $PATH string as the char pointer returned by getenv() should
203 * not be modified.
204 */
205 command_search_path = strdup(command_search_path);
206 if (!command_search_path) {
207 ret = -1;
208 goto end;
209 }
210
211 /*
212 * This char array is used to concatenate path to binary to look for
213 * the binary.
214 */
215 tentative_binary_path = zmalloc(LTTNG_PATH_MAX * sizeof(char));
216 if (!tentative_binary_path) {
217 ret = -1;
218 goto alloc_error;
219 }
220
221 curr_search_dir = command_search_path;
222 do {
223 /*
224 * Split on ':'. The return value of this call points to the
225 * matching character.
226 */
227 curr_search_dir_end = strchr(curr_search_dir, ':');
228 if (curr_search_dir_end != NULL) {
229 /*
230 * Add a NULL byte to the end of the first token so it
231 * can be used as a string.
232 */
233 curr_search_dir_end[0] = '\0';
234 }
235
236 /* Empty the tentative path */
237 memset(tentative_binary_path, 0, LTTNG_PATH_MAX * sizeof(char));
238
239 /*
240 * Build the tentative path to the binary using the current
241 * search directory and the name of the binary.
242 */
243 ret = snprintf(tentative_binary_path, LTTNG_PATH_MAX, "%s/%s",
244 curr_search_dir, binary);
245 if (ret < 0) {
246 goto free_binary_path;
247 }
248 if (ret < LTTNG_PATH_MAX) {
249 /*
250 * Use STAT(2) to see if the file exists.
251 */
252 ret = stat(tentative_binary_path, &stat_output);
253 if (ret == 0) {
254 /*
255 * Verify that it is a regular file or a
256 * symlink and not a special file (e.g.
257 * device).
258 */
259 if (S_ISREG(stat_output.st_mode)
260 || S_ISLNK(stat_output.st_mode)) {
261 /*
262 * Found a match, set the out parameter
263 * and return success.
264 */
265 ret = lttng_strncpy(binary_full_path,
266 tentative_binary_path,
267 LTTNG_PATH_MAX);
268 if (ret == -1) {
269 ERR("Source path does not fit "
270 "in destination buffer.");
271 }
272 goto free_binary_path;
273 }
274 }
275 }
276 /* Go to the next entry in the $PATH variable. */
277 curr_search_dir = curr_search_dir_end + 1;
278 } while (curr_search_dir_end != NULL);
279
280free_binary_path:
281 free(tentative_binary_path);
282alloc_error:
283 free(command_search_path);
284end:
285 return ret;
286}
287
36aa2f64
FD
288/*
289 * Check if the symbol field passed by the user is in fact an address or an
290 * offset from a symbol. Those two instrumentation types are not supported yet.
291 * It's expected to be a common mistake because of the existing --probe option
292 * that does support these formats.
293 *
294 * Here are examples of these unsupported formats for the --userspace-probe
295 * option:
296 * elf:/path/to/binary:0x400430
297 * elf:/path/to/binary:4194364
298 * elf:/path/to/binary:my_symbol+0x323
299 * elf:/path/to/binary:my_symbol+43
300 */
301static int warn_userspace_probe_syntax(const char *symbol)
302{
36aa2f64
FD
303 int ret;
304
305 /* Check if the symbol field is an hex address. */
639d2823 306 ret = sscanf(symbol, "0x%*x");
36aa2f64
FD
307 if (ret > 0) {
308 /* If there is a match, print a warning and return an error. */
309 ERR("Userspace probe on address not supported yet.");
310 ret = CMD_UNSUPPORTED;
311 goto error;
312 }
313
314 /* Check if the symbol field is an decimal address. */
639d2823 315 ret = sscanf(symbol, "%*u");
36aa2f64
FD
316 if (ret > 0) {
317 /* If there is a match, print a warning and return an error. */
318 ERR("Userspace probe on address not supported yet.");
319 ret = CMD_UNSUPPORTED;
320 goto error;
321 }
322
323 /* Check if the symbol field is symbol+hex_offset. */
639d2823 324 ret = sscanf(symbol, "%*[^+]+0x%*x");
36aa2f64
FD
325 if (ret > 0) {
326 /* If there is a match, print a warning and return an error. */
327 ERR("Userspace probe on symbol+offset not supported yet.");
328 ret = CMD_UNSUPPORTED;
329 goto error;
330 }
331
332 /* Check if the symbol field is symbol+decimal_offset. */
639d2823 333 ret = sscanf(symbol, "%*[^+]+%*u");
36aa2f64
FD
334 if (ret > 0) {
335 /* If there is a match, print a warning and return an error. */
336 ERR("Userspace probe on symbol+offset not supported yet.");
337 ret = CMD_UNSUPPORTED;
338 goto error;
339 }
340
341 ret = 0;
342
343error:
344 return ret;
345}
346
dcabc190
FD
347/*
348 * Parse userspace probe options
349 * Set the userspace probe fields in the lttng_event struct and set the
350 * target_path to the path to the binary.
351 */
352static int parse_userspace_probe_opts(struct lttng_event *ev, char *opt)
353{
354 int ret = CMD_SUCCESS;
355 int num_token;
356 char **tokens;
357 char *target_path = NULL;
358 char *unescaped_target_path = NULL;
359 char *real_target_path = NULL;
360 char *symbol_name = NULL, *probe_name = NULL, *provider_name = NULL;
361 struct lttng_userspace_probe_location *probe_location = NULL;
362 struct lttng_userspace_probe_location_lookup_method *lookup_method =
363 NULL;
364
365 if (opt == NULL) {
366 ret = CMD_ERROR;
367 goto end;
368 }
369
ebbcac93
FD
370 switch (ev->type) {
371 case LTTNG_EVENT_USERSPACE_PROBE:
372 break;
373 default:
374 assert(0);
375 }
376
dcabc190
FD
377 /*
378 * userspace probe fields are separated by ':'.
379 */
380 tokens = strutils_split(opt, ':', 1);
381 num_token = strutils_array_of_strings_len(tokens);
382
383 /*
384 * Early sanity check that the number of parameter is between 2 and 4
385 * inclusively.
386 * elf:PATH:SYMBOL
387 * std:PATH:PROVIDER_NAME:PROBE_NAME
388 * PATH:SYMBOL (same behavior as ELF)
389 */
390 if (num_token < 2 || num_token > 4) {
391 ret = CMD_ERROR;
392 goto end_string;
393 }
394
395 /*
396 * Looking up the first parameter will tell the technique to use to
397 * interpret the userspace probe/function description.
398 */
399 switch (num_token) {
400 case 2:
401 /* When the probe type is omitted we assume ELF for now. */
402 case 3:
403 if (num_token == 3 && strcmp(tokens[0], "elf") == 0) {
404 target_path = tokens[1];
405 symbol_name = tokens[2];
406 } else if (num_token == 2) {
407 target_path = tokens[0];
408 symbol_name = tokens[1];
409 } else {
410 ret = CMD_ERROR;
411 goto end_string;
412 }
413 lookup_method =
414 lttng_userspace_probe_location_lookup_method_function_elf_create();
415 if (!lookup_method) {
416 WARN("Failed to create ELF lookup method");
417 ret = CMD_ERROR;
418 goto end_string;
419 }
420 break;
421 case 4:
422 if (strcmp(tokens[0], "sdt") == 0) {
423 target_path = tokens[1];
424 provider_name = tokens[2];
425 probe_name = tokens[3];
426 } else {
427 ret = CMD_ERROR;
428 goto end_string;
429 }
430 lookup_method =
431 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
432 if (!lookup_method) {
b9e1aca7 433 WARN("Failed to create SDT lookup method");
dcabc190
FD
434 ret = CMD_ERROR;
435 goto end_string;
436 }
437 break;
438 default:
439 ret = CMD_ERROR;
440 goto end_string;
441 }
442
443 /* strutils_unescape_string allocates a new char *. */
444 unescaped_target_path = strutils_unescape_string(target_path, 0);
445 if (!unescaped_target_path) {
36aa2f64 446 ret = CMD_ERROR;
ebbcac93 447 goto end_destroy_lookup_method;
dcabc190
FD
448 }
449
450 /*
451 * If there is not forward slash in the path. Walk the $PATH else
452 * expand.
453 */
ebbcac93 454 if (strchr(unescaped_target_path, '/') == NULL) {
dcabc190
FD
455 /* Walk the $PATH variable to find the targeted binary. */
456 real_target_path = zmalloc(LTTNG_PATH_MAX * sizeof(char));
457 if (!real_target_path) {
458 PERROR("Error allocating path buffer");
459 ret = CMD_ERROR;
ebbcac93 460 goto end_destroy_lookup_method;
dcabc190 461 }
ebbcac93 462 ret = walk_command_search_path(unescaped_target_path, real_target_path);
dcabc190
FD
463 if (ret) {
464 ERR("Binary not found.");
465 ret = CMD_ERROR;
ebbcac93 466 goto end_destroy_lookup_method;
dcabc190
FD
467 }
468 } else {
469 /*
470 * Expand references to `/./` and `/../`. This function does not check
ebbcac93
FD
471 * if the file exists. This call returns an allocated buffer on
472 * success.
dcabc190 473 */
ebbcac93 474 real_target_path = utils_expand_path_keep_symlink(unescaped_target_path);
dcabc190
FD
475 if (!real_target_path) {
476 ERR("Error expanding the path to binary.");
477 ret = CMD_ERROR;
ebbcac93 478 goto end_destroy_lookup_method;
dcabc190
FD
479 }
480
481 /*
54185c70
FD
482 * Check if the file exists using access(2), If it does not,
483 * return an error.
dcabc190
FD
484 */
485 ret = access(real_target_path, F_OK);
486 if (ret) {
bb77204a 487 ERR("Cannot find binary at path: %s.", real_target_path);
dcabc190 488 ret = CMD_ERROR;
ebbcac93 489 goto end_destroy_lookup_method;
dcabc190
FD
490 }
491 }
492
493 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method)) {
494 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
36aa2f64
FD
495 /*
496 * Check for common mistakes in userspace probe description syntax.
497 */
498 ret = warn_userspace_probe_syntax(symbol_name);
499 if (ret) {
500 goto end_destroy_lookup_method;
501 }
502
dcabc190
FD
503 probe_location = lttng_userspace_probe_location_function_create(
504 real_target_path, symbol_name, lookup_method);
505 if (!probe_location) {
506 WARN("Failed to create function probe location");
507 ret = CMD_ERROR;
508 goto end_destroy_lookup_method;
509 }
510
511 /* Ownership transferred to probe_location. */
512 lookup_method = NULL;
513
514 ret = lttng_event_set_userspace_probe_location(ev, probe_location);
515 if (ret) {
516 WARN("Failed to set probe location on event");
517 ret = CMD_ERROR;
518 goto end_destroy_location;
519 }
520 break;
521 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
522 probe_location = lttng_userspace_probe_location_tracepoint_create(
523 real_target_path, provider_name, probe_name, lookup_method);
524 if (!probe_location) {
525 WARN("Failed to create function probe location");
526 ret = CMD_ERROR;
527 goto end_destroy_lookup_method;
528 }
529
530 /* Ownership transferred to probe_location. */
531 lookup_method = NULL;
532
533 ret = lttng_event_set_userspace_probe_location(ev, probe_location);
534 if (ret) {
535 WARN("Failed to set probe location on event");
536 ret = CMD_ERROR;
537 goto end_destroy_location;
538 }
539 break;
540 default:
541 ret = CMD_ERROR;
ebbcac93 542 goto end_destroy_lookup_method;
dcabc190
FD
543 }
544
ebbcac93
FD
545 /* Successful parsing, now clean up everything and return. */
546 goto end_string;
dcabc190
FD
547
548end_destroy_location:
549 lttng_userspace_probe_location_destroy(probe_location);
550end_destroy_lookup_method:
551 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
ebbcac93
FD
552end_string:
553 strutils_free_null_terminated_array_of_strings(tokens);
dcabc190 554 /*
ebbcac93
FD
555 * Freeing both char * here makes the error handling simplier. free()
556 * performs not action if the pointer is NULL.
dcabc190
FD
557 */
558 free(real_target_path);
dcabc190 559 free(unescaped_target_path);
dcabc190
FD
560end:
561 return ret;
562}
563
5cdb6027
DG
564/*
565 * Maps LOG4j loglevel from string to value
566 */
567static int loglevel_log4j_str_to_value(const char *inputstr)
568{
569 int i = 0;
570 char str[LTTNG_SYMBOL_NAME_LEN];
571
3712b110
JG
572 if (!inputstr || strlen(inputstr) == 0) {
573 return -1;
574 }
575
5cdb6027
DG
576 /*
577 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
578 * added at the end of the loop so a the upper bound we avoid the overflow.
579 */
580 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
581 str[i] = toupper(inputstr[i]);
582 i++;
583 }
584 str[i] = '\0';
585
586 if (!strcmp(str, "LOG4J_OFF") || !strcmp(str, "OFF")) {
587 return LTTNG_LOGLEVEL_LOG4J_OFF;
588 } else if (!strcmp(str, "LOG4J_FATAL") || !strcmp(str, "FATAL")) {
589 return LTTNG_LOGLEVEL_LOG4J_FATAL;
590 } else if (!strcmp(str, "LOG4J_ERROR") || !strcmp(str, "ERROR")) {
591 return LTTNG_LOGLEVEL_LOG4J_ERROR;
592 } else if (!strcmp(str, "LOG4J_WARN") || !strcmp(str, "WARN")) {
593 return LTTNG_LOGLEVEL_LOG4J_WARN;
594 } else if (!strcmp(str, "LOG4J_INFO") || !strcmp(str, "INFO")) {
595 return LTTNG_LOGLEVEL_LOG4J_INFO;
596 } else if (!strcmp(str, "LOG4J_DEBUG") || !strcmp(str, "DEBUG")) {
597 return LTTNG_LOGLEVEL_LOG4J_DEBUG;
598 } else if (!strcmp(str, "LOG4J_TRACE") || !strcmp(str, "TRACE")) {
599 return LTTNG_LOGLEVEL_LOG4J_TRACE;
600 } else if (!strcmp(str, "LOG4J_ALL") || !strcmp(str, "ALL")) {
601 return LTTNG_LOGLEVEL_LOG4J_ALL;
602 } else {
603 return -1;
604 }
605}
606
b2064f54
DG
607/*
608 * Maps JUL loglevel from string to value
609 */
610static int loglevel_jul_str_to_value(const char *inputstr)
611{
612 int i = 0;
613 char str[LTTNG_SYMBOL_NAME_LEN];
614
3712b110
JG
615 if (!inputstr || strlen(inputstr) == 0) {
616 return -1;
617 }
618
b2064f54
DG
619 /*
620 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
621 * added at the end of the loop so a the upper bound we avoid the overflow.
622 */
623 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
624 str[i] = toupper(inputstr[i]);
625 i++;
626 }
627 str[i] = '\0';
628
629 if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) {
630 return LTTNG_LOGLEVEL_JUL_OFF;
631 } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) {
632 return LTTNG_LOGLEVEL_JUL_SEVERE;
633 } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) {
634 return LTTNG_LOGLEVEL_JUL_WARNING;
635 } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) {
636 return LTTNG_LOGLEVEL_JUL_INFO;
637 } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) {
638 return LTTNG_LOGLEVEL_JUL_CONFIG;
639 } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) {
640 return LTTNG_LOGLEVEL_JUL_FINE;
641 } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) {
642 return LTTNG_LOGLEVEL_JUL_FINER;
643 } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) {
644 return LTTNG_LOGLEVEL_JUL_FINEST;
645 } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) {
646 return LTTNG_LOGLEVEL_JUL_ALL;
647 } else {
648 return -1;
649 }
650}
651
0e115563
DG
652/*
653 * Maps Python loglevel from string to value
654 */
655static int loglevel_python_str_to_value(const char *inputstr)
656{
657 int i = 0;
658 char str[LTTNG_SYMBOL_NAME_LEN];
659
3712b110
JG
660 if (!inputstr || strlen(inputstr) == 0) {
661 return -1;
662 }
663
0e115563
DG
664 /*
665 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
666 * added at the end of the loop so a the upper bound we avoid the overflow.
667 */
668 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
669 str[i] = toupper(inputstr[i]);
670 i++;
671 }
672 str[i] = '\0';
673
674 if (!strcmp(str, "PYTHON_CRITICAL") || !strcmp(str, "CRITICAL")) {
675 return LTTNG_LOGLEVEL_PYTHON_CRITICAL;
676 } else if (!strcmp(str, "PYTHON_ERROR") || !strcmp(str, "ERROR")) {
677 return LTTNG_LOGLEVEL_PYTHON_ERROR;
678 } else if (!strcmp(str, "PYTHON_WARNING") || !strcmp(str, "WARNING")) {
679 return LTTNG_LOGLEVEL_PYTHON_WARNING;
680 } else if (!strcmp(str, "PYTHON_INFO") || !strcmp(str, "INFO")) {
681 return LTTNG_LOGLEVEL_PYTHON_INFO;
682 } else if (!strcmp(str, "PYTNON_DEBUG") || !strcmp(str, "DEBUG")) {
683 return LTTNG_LOGLEVEL_PYTHON_DEBUG;
684 } else if (!strcmp(str, "PYTHON_NOTSET") || !strcmp(str, "NOTSET")) {
685 return LTTNG_LOGLEVEL_PYTHON_NOTSET;
686 } else {
687 return -1;
688 }
689}
690
8005f29a 691/*
0c8477c8 692 * Maps loglevel from string to value
8005f29a
MD
693 */
694static
46839cc2 695int loglevel_str_to_value(const char *inputstr)
8005f29a 696{
46839cc2
MD
697 int i = 0;
698 char str[LTTNG_SYMBOL_NAME_LEN];
699
3712b110
JG
700 if (!inputstr || strlen(inputstr) == 0) {
701 return -1;
702 }
703
e051e07c
DG
704 /*
705 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
706 * added at the end of the loop so a the upper bound we avoid the overflow.
707 */
708 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
46839cc2
MD
709 str[i] = toupper(inputstr[i]);
710 i++;
711 }
712 str[i] = '\0';
713 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
714 return LTTNG_LOGLEVEL_EMERG;
715 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
716 return LTTNG_LOGLEVEL_ALERT;
717 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
718 return LTTNG_LOGLEVEL_CRIT;
719 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
720 return LTTNG_LOGLEVEL_ERR;
721 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
722 return LTTNG_LOGLEVEL_WARNING;
723 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
724 return LTTNG_LOGLEVEL_NOTICE;
725 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
726 return LTTNG_LOGLEVEL_INFO;
727 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
728 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
729 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
730 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
731 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
732 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
733 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
734 return LTTNG_LOGLEVEL_DEBUG_MODULE;
735 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
736 return LTTNG_LOGLEVEL_DEBUG_UNIT;
737 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
738 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
739 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
740 return LTTNG_LOGLEVEL_DEBUG_LINE;
741 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
742 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
743 } else {
744 return -1;
745 }
746}
747
85076754
MD
748static
749const char *print_channel_name(const char *name)
750{
751 return name ? : DEFAULT_CHANNEL_NAME;
752}
753
754static
755const char *print_raw_channel_name(const char *name)
756{
757 return name ? : "<default>";
758}
759
89476427
JRJ
760/*
761 * Mi print exlcusion list
762 */
763static
9f449915 764int mi_print_exclusion(char **names)
89476427
JRJ
765{
766 int i, ret;
9f449915 767 int count = names ? strutils_array_of_strings_len(names) : 0;
89476427
JRJ
768
769 assert(writer);
770
771 if (count == 0) {
772 ret = 0;
773 goto end;
774 }
775 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
776 if (ret) {
777 goto end;
778 }
779
780 for (i = 0; i < count; i++) {
781 ret = mi_lttng_writer_write_element_string(writer,
782 config_element_exclusion, names[i]);
783 if (ret) {
784 goto end;
785 }
786 }
787
788 /* Close exclusions element */
789 ret = mi_lttng_writer_close_element(writer);
790
791end:
792 return ret;
793}
794
9c48cab3
JI
795/*
796 * Return allocated string for pretty-printing exclusion names.
797 */
798static
9f449915 799char *print_exclusions(char **names)
9c48cab3
JI
800{
801 int length = 0;
802 int i;
bfe36393 803 const char preamble[] = " excluding ";
9c48cab3 804 char *ret;
9f449915 805 int count = names ? strutils_array_of_strings_len(names) : 0;
9c48cab3
JI
806
807 if (count == 0) {
808 return strdup("");
809 }
810
811 /* calculate total required length */
812 for (i = 0; i < count; i++) {
9f449915 813 length += strlen(names[i]) + 4;
9c48cab3
JI
814 }
815
bfe36393
JG
816 length += sizeof(preamble);
817 ret = zmalloc(length);
fd591b18
MD
818 if (!ret) {
819 return NULL;
820 }
9c48cab3
JI
821 strncpy(ret, preamble, length);
822 for (i = 0; i < count; i++) {
9f449915 823 strcat(ret, "\"");
9c48cab3 824 strcat(ret, names[i]);
9f449915 825 strcat(ret, "\"");
9c48cab3 826 if (i != count - 1) {
9f449915 827 strcat(ret, ", ");
9c48cab3
JI
828 }
829 }
89476427 830
9c48cab3
JI
831 return ret;
832}
833
748bde76 834static
9f449915 835int check_exclusion_subsets(const char *event_name, const char *exclusion)
748bde76 836{
9f449915
PP
837 bool warn = false;
838 int ret = 0;
839 const char *e = event_name;
840 const char *x = exclusion;
841
842 /* Scan both the excluder and the event letter by letter */
843 while (true) {
844 if (*e == '\\') {
845 if (*x != *e) {
846 warn = true;
847 goto end;
848 }
748bde76 849
9f449915
PP
850 e++;
851 x++;
852 goto cmp_chars;
853 }
854
855 if (*x == '*') {
856 /* Event is a subset of the excluder */
857 ERR("Event %s: %s excludes all events from %s",
858 event_name, exclusion, event_name);
859 goto error;
860 }
861
862 if (*e == '*') {
863 /*
864 * Reached the end of the event name before the
865 * end of the exclusion: this is valid.
866 */
867 goto end;
868 }
869
870cmp_chars:
871 if (*x != *e) {
872 warn = true;
873 break;
874 }
875
876 x++;
877 e++;
748bde76
JI
878 }
879
9f449915 880 goto end;
748bde76 881
9f449915
PP
882error:
883 ret = -1;
748bde76 884
9f449915
PP
885end:
886 if (warn) {
887 WARN("Event %s: %s does not exclude any events from %s",
888 event_name, exclusion, event_name);
889 }
748bde76 890
9f449915
PP
891 return ret;
892}
893
9f449915
PP
894static
895int create_exclusion_list_and_validate(const char *event_name,
896 const char *exclusions_arg,
897 char ***exclusion_list)
898{
899 int ret = 0;
900 char **exclusions = NULL;
901
902 /* Event name must be a valid globbing pattern to allow exclusions. */
903 if (!strutils_is_star_glob_pattern(event_name)) {
904 ERR("Event %s: Exclusions can only be used with a globbing pattern",
905 event_name);
906 goto error;
907 }
908
909 /* Split exclusions. */
910 exclusions = strutils_split(exclusions_arg, ',', true);
911 if (!exclusions) {
912 goto error;
913 }
914
915 /*
916 * If the event name is a star-at-end only globbing pattern,
917 * then we can validate the individual exclusions. Otherwise
918 * all exclusions are passed to the session daemon.
919 */
920 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
921 char * const *exclusion;
922
923 for (exclusion = exclusions; *exclusion; exclusion++) {
924 if (!strutils_is_star_glob_pattern(*exclusion) ||
925 strutils_is_star_at_the_end_only_glob_pattern(*exclusion)) {
8aa579d4 926 ret = check_exclusion_subsets(event_name, *exclusion);
9f449915 927 if (ret) {
5ef79758
MD
928 goto error;
929 }
748bde76 930 }
748bde76
JI
931 }
932 }
9f449915
PP
933
934 *exclusion_list = exclusions;
935
748bde76 936 goto end;
9f449915 937
748bde76 938error:
9f449915
PP
939 ret = -1;
940 strutils_free_null_terminated_array_of_strings(exclusions);
941
748bde76 942end:
748bde76
JI
943 return ret;
944}
502bbe89 945
9f449915
PP
946static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
947 int *warn)
502bbe89 948{
9f449915 949 char * const *exclusion;
502bbe89 950
9f449915
PP
951 for (exclusion = exclusion_list; *exclusion; exclusion++) {
952 if (strlen(*exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
502bbe89 953 WARN("Event exclusion \"%s\" will be truncated",
9f449915 954 *exclusion);
502bbe89
PP
955 *warn = 1;
956 }
957 }
958}
959
f3ed775e 960/*
6181537c 961 * Enabling event using the lttng API.
89476427 962 * Note: in case of error only the last error code will be return.
f3ed775e 963 */
cd80958d 964static int enable_events(char *session_name)
f3ed775e 965{
89476427
JRJ
966 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
967 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
b73d0b29 968 char *event_name, *channel_name = NULL;
91744e14 969 struct lttng_event *ev;
7d29a247 970 struct lttng_domain dom;
7ed70bc9 971 char **exclusion_list = NULL;
f3ed775e 972
441c16a7
MD
973 memset(&dom, 0, sizeof(dom));
974
91744e14
FD
975 ev = lttng_event_create();
976 if (!ev) {
977 ret = CMD_ERROR;
978 goto error;
979 }
980
53a80697 981 if (opt_kernel) {
67b58630
JG
982 if (opt_loglevel) {
983 WARN("Kernel loglevels are not supported.");
984 }
53a80697
MD
985 }
986
7d29a247
DG
987 /* Create lttng domain */
988 if (opt_kernel) {
989 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 990 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 991 } else if (opt_userspace) {
2bdd86d4 992 dom.type = LTTNG_DOMAIN_UST;
7972aab2 993 /* Default. */
8692d4e5 994 dom.buf_type = LTTNG_BUFFER_PER_UID;
b9dfb167
DG
995 } else if (opt_jul) {
996 dom.type = LTTNG_DOMAIN_JUL;
997 /* Default. */
998 dom.buf_type = LTTNG_BUFFER_PER_UID;
5cdb6027
DG
999 } else if (opt_log4j) {
1000 dom.type = LTTNG_DOMAIN_LOG4J;
1001 /* Default. */
1002 dom.buf_type = LTTNG_BUFFER_PER_UID;
0e115563
DG
1003 } else if (opt_python) {
1004 dom.type = LTTNG_DOMAIN_PYTHON;
1005 /* Default. */
1006 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 1007 } else {
3ecec76a
PP
1008 /* Checked by the caller. */
1009 assert(0);
2bdd86d4 1010 }
7d29a247 1011
8b08c525
AB
1012 if (opt_exclude) {
1013 switch (dom.type) {
1014 case LTTNG_DOMAIN_KERNEL:
1015 case LTTNG_DOMAIN_JUL:
1016 case LTTNG_DOMAIN_LOG4J:
1017 case LTTNG_DOMAIN_PYTHON:
1018 ERR("Event name exclusions are not yet implemented for %s events",
1019 get_domain_str(dom.type));
1020 ret = CMD_ERROR;
1021 goto error;
1022 case LTTNG_DOMAIN_UST:
1023 /* Exclusions supported */
1024 break;
1025 default:
1026 assert(0);
1027 }
d5dd17fd
JI
1028 }
1029
4fd2697f
FD
1030 /*
1031 * Adding a filter to a probe, function or userspace-probe would be
1032 * denied by the kernel tracer as it's not supported at the moment. We
1033 * do an early check here to warn the user.
1034 */
1035 if (opt_filter && opt_kernel) {
1036 switch (opt_event_type) {
1037 case LTTNG_EVENT_ALL:
1038 case LTTNG_EVENT_TRACEPOINT:
1039 case LTTNG_EVENT_SYSCALL:
1040 break;
1041 case LTTNG_EVENT_PROBE:
1042 case LTTNG_EVENT_USERSPACE_PROBE:
1043 case LTTNG_EVENT_FUNCTION:
1044 ERR("Filter expressions are not supported for %s events",
1045 get_event_type_str(opt_event_type));
1046 ret = CMD_ERROR;
1047 goto error;
1048 default:
1049 ret = CMD_UNDEFINED;
1050 goto error;
1051 }
1052 }
1053
85076754 1054 channel_name = opt_channel_name;
ae856491 1055
cd80958d
DG
1056 handle = lttng_create_handle(session_name, &dom);
1057 if (handle == NULL) {
1058 ret = -1;
1059 goto error;
1060 }
1aef21b6 1061
89476427
JRJ
1062 /* Prepare Mi */
1063 if (lttng_opt_mi) {
1064 /* Open a events element */
1065 ret = mi_lttng_writer_open_element(writer, config_element_events);
1066 if (ret) {
1067 ret = CMD_ERROR;
1068 goto error;
1069 }
1070 }
1071
cd80958d 1072 if (opt_enable_all) {
8c9ae521 1073 /* Default setup for enable all */
75e8c5ab 1074 if (opt_kernel) {
91744e14
FD
1075 ev->type = opt_event_type;
1076 strcpy(ev->name, "*");
300b8fd5 1077 /* kernel loglevels not implemented */
91744e14 1078 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab 1079 } else {
91744e14
FD
1080 ev->type = LTTNG_EVENT_TRACEPOINT;
1081 strcpy(ev->name, "*");
1082 ev->loglevel_type = opt_loglevel_type;
300b8fd5 1083 if (opt_loglevel) {
0e115563 1084 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54 1085 if (opt_userspace) {
91744e14 1086 ev->loglevel = loglevel_str_to_value(opt_loglevel);
b2064f54 1087 } else if (opt_jul) {
91744e14 1088 ev->loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027 1089 } else if (opt_log4j) {
91744e14 1090 ev->loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563 1091 } else if (opt_python) {
91744e14 1092 ev->loglevel = loglevel_python_str_to_value(opt_loglevel);
b2064f54 1093 }
91744e14 1094 if (ev->loglevel == -1) {
300b8fd5 1095 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1096 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
1097 goto error;
1098 }
22e25b71 1099 } else {
0e115563 1100 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54 1101 if (opt_userspace) {
91744e14 1102 ev->loglevel = -1;
34aa3685 1103 } else if (opt_jul) {
91744e14 1104 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
34aa3685 1105 } else if (opt_log4j) {
91744e14 1106 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 1107 } else if (opt_python) {
91744e14 1108 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
b2064f54 1109 }
300b8fd5 1110 }
75e8c5ab 1111 }
8c9ae521 1112
7ed70bc9 1113 if (opt_exclude) {
9f449915
PP
1114 ret = create_exclusion_list_and_validate("*",
1115 opt_exclude, &exclusion_list);
1116 if (ret) {
1117 ret = CMD_ERROR;
7ed70bc9
JI
1118 goto error;
1119 }
502bbe89 1120
91744e14 1121 ev->exclusion = 1;
502bbe89 1122 warn_on_truncated_exclusion_names(exclusion_list,
9f449915 1123 &warn);
7ed70bc9 1124 }
025faf73 1125 if (!opt_filter) {
7ed70bc9 1126 ret = lttng_enable_event_with_exclusions(handle,
91744e14 1127 ev, channel_name,
7ed70bc9 1128 NULL,
9f449915
PP
1129 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1130 exclusion_list);
025faf73
DG
1131 if (ret < 0) {
1132 switch (-ret) {
1133 case LTTNG_ERR_KERN_EVENT_EXIST:
1134 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 1135 print_channel_name(channel_name), session_name);
89476427 1136 warn = 1;
025faf73 1137 break;
45d5d421
CB
1138 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1139 {
1140 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1141 ERR("Events: %s (channel %s, session %s)",
1142 msg,
1143 print_channel_name(channel_name),
1144 session_name);
1145 error = 1;
1146 break;
1147 }
025faf73
DG
1148 default:
1149 ERR("Events: %s (channel %s, session %s)",
85076754
MD
1150 lttng_strerror(ret),
1151 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1152 ? print_raw_channel_name(channel_name)
1153 : print_channel_name(channel_name),
1154 session_name);
89476427 1155 error = 1;
025faf73
DG
1156 break;
1157 }
1158 goto end;
42224349 1159 }
8c9ae521 1160
025faf73
DG
1161 switch (opt_event_type) {
1162 case LTTNG_EVENT_TRACEPOINT:
67b58630 1163 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9f449915 1164 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1165
1166 if (!exclusion_string) {
1167 PERROR("Cannot allocate exclusion_string");
1168 error = 1;
1169 goto end;
1170 }
9c48cab3 1171 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
b9dfb167 1172 get_domain_str(dom.type),
9c48cab3 1173 exclusion_string,
85076754 1174 print_channel_name(channel_name),
025faf73 1175 opt_loglevel);
9c48cab3 1176 free(exclusion_string);
025faf73 1177 } else {
9f449915 1178 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1179
1180 if (!exclusion_string) {
1181 PERROR("Cannot allocate exclusion_string");
1182 error = 1;
1183 goto end;
1184 }
9c48cab3 1185 MSG("All %s tracepoints%s are enabled in channel %s",
b9dfb167 1186 get_domain_str(dom.type),
9c48cab3 1187 exclusion_string,
85076754 1188 print_channel_name(channel_name));
9c48cab3 1189 free(exclusion_string);
025faf73
DG
1190 }
1191 break;
1192 case LTTNG_EVENT_SYSCALL:
1193 if (opt_kernel) {
6e911cad
MD
1194 MSG("All %s system calls are enabled in channel %s",
1195 get_domain_str(dom.type),
85076754 1196 print_channel_name(channel_name));
025faf73
DG
1197 }
1198 break;
1199 case LTTNG_EVENT_ALL:
67b58630 1200 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9f449915 1201 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1202
1203 if (!exclusion_string) {
1204 PERROR("Cannot allocate exclusion_string");
1205 error = 1;
1206 goto end;
1207 }
9c48cab3 1208 MSG("All %s events%s are enabled in channel %s for loglevel %s",
b9dfb167 1209 get_domain_str(dom.type),
9c48cab3 1210 exclusion_string,
85076754 1211 print_channel_name(channel_name),
025faf73 1212 opt_loglevel);
9c48cab3 1213 free(exclusion_string);
025faf73 1214 } else {
9f449915 1215 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1216
1217 if (!exclusion_string) {
1218 PERROR("Cannot allocate exclusion_string");
1219 error = 1;
1220 goto end;
1221 }
9c48cab3 1222 MSG("All %s events%s are enabled in channel %s",
b9dfb167 1223 get_domain_str(dom.type),
9c48cab3 1224 exclusion_string,
85076754 1225 print_channel_name(channel_name));
9c48cab3 1226 free(exclusion_string);
025faf73
DG
1227 }
1228 break;
1229 default:
1230 /*
1231 * We should not be here since lttng_enable_event should have
1232 * failed on the event type.
1233 */
1234 goto error;
57064ada 1235 }
f3ed775e 1236 }
89476427 1237
025faf73 1238 if (opt_filter) {
91744e14 1239 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
9f449915
PP
1240 opt_filter,
1241 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1242 exclusion_list);
89476427
JRJ
1243 if (command_ret < 0) {
1244 switch (-command_ret) {
16363652 1245 case LTTNG_ERR_FILTER_EXIST:
85076754 1246 WARN("Filter on all events is already enabled"
16363652 1247 " (channel %s, session %s)",
85076754 1248 print_channel_name(channel_name), session_name);
89476427 1249 warn = 1;
16363652 1250 break;
45d5d421
CB
1251 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1252 {
1253 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1254 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1255 msg,
1256 print_channel_name(channel_name),
1257 session_name, opt_filter);
1258 error = 1;
1259 break;
1260 }
16363652 1261 default:
85076754 1262 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
da3d7d0e
JRJ
1263 lttng_strerror(command_ret),
1264 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1265 ? print_raw_channel_name(channel_name)
1266 : print_channel_name(channel_name),
1267 session_name, opt_filter);
89476427 1268 error = 1;
16363652
DG
1269 break;
1270 }
89476427 1271 error_holder = command_ret;
16363652 1272 } else {
91744e14 1273 ev->filter = 1;
16363652
DG
1274 MSG("Filter '%s' successfully set", opt_filter);
1275 }
1276 }
89476427
JRJ
1277
1278 if (lttng_opt_mi) {
1279 /* The wildcard * is used for kernel and ust domain to
1280 * represent ALL. We copy * in event name to force the wildcard use
1281 * for kernel domain
1282 *
1283 * Note: this is strictly for semantic and printing while in
1284 * machine interface mode.
1285 */
91744e14 1286 strcpy(ev->name, "*");
89476427
JRJ
1287
1288 /* If we reach here the events are enabled */
1289 if (!error && !warn) {
91744e14 1290 ev->enabled = 1;
89476427 1291 } else {
91744e14 1292 ev->enabled = 0;
89476427
JRJ
1293 success = 0;
1294 }
91744e14 1295 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
1296 if (ret) {
1297 ret = CMD_ERROR;
1298 goto error;
1299 }
1300
1301 /* print exclusion */
9f449915 1302 ret = mi_print_exclusion(exclusion_list);
89476427
JRJ
1303 if (ret) {
1304 ret = CMD_ERROR;
1305 goto error;
1306 }
1307
1308 /* Success ? */
1309 ret = mi_lttng_writer_write_element_bool(writer,
1310 mi_lttng_element_command_success, success);
1311 if (ret) {
1312 ret = CMD_ERROR;
1313 goto error;
1314 }
1315
1316 /* Close event element */
1317 ret = mi_lttng_writer_close_element(writer);
1318 if (ret) {
1319 ret = CMD_ERROR;
1320 goto error;
1321 }
1322 }
1323
8c9ae521 1324 goto end;
f3ed775e
DG
1325 }
1326
1327 /* Strip event list */
1328 event_name = strtok(opt_event_list, ",");
1329 while (event_name != NULL) {
6181537c 1330 /* Copy name and type of the event */
91744e14
FD
1331 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1332 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1333 ev->type = opt_event_type;
6181537c 1334
f3ed775e
DG
1335 /* Kernel tracer action */
1336 if (opt_kernel) {
1337 DBG("Enabling kernel event %s for channel %s",
85076754
MD
1338 event_name,
1339 print_channel_name(channel_name));
f3ed775e
DG
1340
1341 switch (opt_event_type) {
29c62722
MD
1342 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
1343 /* If event name differs from *, select tracepoint. */
91744e14
FD
1344 if (strcmp(ev->name, "*")) {
1345 ev->type = LTTNG_EVENT_TRACEPOINT;
29c62722
MD
1346 }
1347 break;
e6ddca71 1348 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 1349 break;
7d29a247 1350 case LTTNG_EVENT_PROBE:
91744e14 1351 ret = parse_probe_opts(ev, opt_probe);
49d4e302 1352 if (ret) {
cf0e5467 1353 ERR("Unable to parse probe options");
91744e14 1354 ret = CMD_ERROR;
0d63dd19
DG
1355 goto error;
1356 }
f3ed775e 1357 break;
dcabc190
FD
1358 case LTTNG_EVENT_USERSPACE_PROBE:
1359 ret = parse_userspace_probe_opts(ev, opt_userspace_probe);
1360 if (ret) {
36aa2f64
FD
1361 switch (ret) {
1362 case CMD_UNSUPPORTED:
1363 /*
1364 * Error message describing
1365 * what is not supported was
1366 * printed in the function.
1367 */
1368 break;
1369 case CMD_ERROR:
1370 default:
1371 ERR("Unable to parse userspace probe options");
1372 break;
1373 }
dcabc190
FD
1374 goto error;
1375 }
1376 break;
f3ed775e 1377 case LTTNG_EVENT_FUNCTION:
91744e14 1378 ret = parse_probe_opts(ev, opt_function);
49d4e302 1379 if (ret) {
8f0d098b 1380 ERR("Unable to parse function probe options");
91744e14 1381 ret = CMD_ERROR;
8f0d098b
MD
1382 goto error;
1383 }
1384 break;
a54bd42d 1385 case LTTNG_EVENT_SYSCALL:
91744e14 1386 ev->type = LTTNG_EVENT_SYSCALL;
c6aa2d41 1387 break;
f3ed775e 1388 default:
1ab1ea0b 1389 ret = CMD_UNDEFINED;
f3ed775e
DG
1390 goto error;
1391 }
0cda4f28 1392
0cda4f28 1393 /* kernel loglevels not implemented */
91744e14 1394 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 1395 } else if (opt_userspace) { /* User-space tracer action */
300b8fd5 1396 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 1397 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
1398
1399 switch (opt_event_type) {
1400 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
1401 /* Fall-through */
1402 case LTTNG_EVENT_TRACEPOINT:
e4baff1e 1403 /* Copy name and type of the event */
91744e14
FD
1404 ev->type = LTTNG_EVENT_TRACEPOINT;
1405 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1406 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
1407 break;
1408 case LTTNG_EVENT_PROBE:
1409 case LTTNG_EVENT_FUNCTION:
2bdd86d4 1410 case LTTNG_EVENT_SYSCALL:
dcabc190 1411 case LTTNG_EVENT_USERSPACE_PROBE:
2bdd86d4 1412 default:
cc62c0c0 1413 ERR("Event type not available for user-space tracing");
4ce78777 1414 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
1415 goto error;
1416 }
0cda4f28 1417
7ed70bc9 1418 if (opt_exclude) {
91744e14 1419 ev->exclusion = 1;
d5dd17fd
JI
1420 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1421 ERR("Exclusion option can only be used with tracepoint events");
1422 ret = CMD_ERROR;
1423 goto error;
1424 }
7ed70bc9 1425 /* Free previously allocated items */
9f449915
PP
1426 strutils_free_null_terminated_array_of_strings(
1427 exclusion_list);
1428 exclusion_list = NULL;
1429 ret = create_exclusion_list_and_validate(
1430 event_name, opt_exclude,
1431 &exclusion_list);
1432 if (ret) {
1433 ret = CMD_ERROR;
7ed70bc9
JI
1434 goto error;
1435 }
502bbe89
PP
1436
1437 warn_on_truncated_exclusion_names(
9f449915 1438 exclusion_list, &warn);
7ed70bc9
JI
1439 }
1440
91744e14 1441 ev->loglevel_type = opt_loglevel_type;
ed7f4083 1442 if (opt_loglevel) {
91744e14
FD
1443 ev->loglevel = loglevel_str_to_value(opt_loglevel);
1444 if (ev->loglevel == -1) {
8005f29a 1445 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1446 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
1447 goto error;
1448 }
22e25b71 1449 } else {
91744e14 1450 ev->loglevel = -1;
ed7f4083 1451 }
0e115563 1452 } else if (opt_jul || opt_log4j || opt_python) {
b9dfb167
DG
1453 if (opt_event_type != LTTNG_EVENT_ALL &&
1454 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
5cdb6027 1455 ERR("Event type not supported for domain.");
b9dfb167
DG
1456 ret = CMD_UNSUPPORTED;
1457 goto error;
1458 }
b2064f54 1459
91744e14 1460 ev->loglevel_type = opt_loglevel_type;
b2064f54 1461 if (opt_loglevel) {
5cdb6027 1462 if (opt_jul) {
91744e14 1463 ev->loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027 1464 } else if (opt_log4j) {
91744e14 1465 ev->loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563 1466 } else if (opt_python) {
91744e14 1467 ev->loglevel = loglevel_python_str_to_value(opt_loglevel);
5cdb6027 1468 }
91744e14 1469 if (ev->loglevel == -1) {
b2064f54
DG
1470 ERR("Unknown loglevel %s", opt_loglevel);
1471 ret = -LTTNG_ERR_INVALID;
1472 goto error;
1473 }
1474 } else {
5cdb6027 1475 if (opt_jul) {
91744e14 1476 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
5cdb6027 1477 } else if (opt_log4j) {
91744e14 1478 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 1479 } else if (opt_python) {
91744e14 1480 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
5cdb6027 1481 }
b2064f54 1482 }
91744e14
FD
1483 ev->type = LTTNG_EVENT_TRACEPOINT;
1484 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1485 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1486 } else {
3ecec76a 1487 assert(0);
f3ed775e
DG
1488 }
1489
025faf73 1490 if (!opt_filter) {
9c48cab3
JI
1491 char *exclusion_string;
1492
89476427 1493 command_ret = lttng_enable_event_with_exclusions(handle,
91744e14 1494 ev, channel_name,
9f449915
PP
1495 NULL,
1496 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1497 exclusion_list);
1498 exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1499 if (!exclusion_string) {
1500 PERROR("Cannot allocate exclusion_string");
1501 error = 1;
1502 goto end;
1503 }
89476427 1504 if (command_ret < 0) {
025faf73 1505 /* Turn ret to positive value to handle the positive error code */
89476427 1506 switch (-command_ret) {
025faf73 1507 case LTTNG_ERR_KERN_EVENT_EXIST:
9c48cab3 1508 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
85076754 1509 event_name,
9c48cab3 1510 exclusion_string,
85076754 1511 print_channel_name(channel_name), session_name);
89476427 1512 warn = 1;
025faf73 1513 break;
45d5d421
CB
1514 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1515 {
1516 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1517 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1518 exclusion_string,
1519 msg,
1520 print_channel_name(channel_name),
1521 session_name);
1522 error = 1;
1523 break;
1524 }
dcabc190
FD
1525 case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
1526 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1527 event_name, print_channel_name(channel_name),
1528 session_name);
1529 error = 1;
1530 break;
025faf73 1531 default:
9c48cab3
JI
1532 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1533 exclusion_string,
da3d7d0e
JRJ
1534 lttng_strerror(command_ret),
1535 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1536 ? print_raw_channel_name(channel_name)
1537 : print_channel_name(channel_name),
1538 session_name);
89476427 1539 error = 1;
025faf73
DG
1540 break;
1541 }
89476427 1542 error_holder = command_ret;
025faf73 1543 } else {
8274eeba
AB
1544 switch (dom.type) {
1545 case LTTNG_DOMAIN_KERNEL:
1546 case LTTNG_DOMAIN_UST:
49ceaa70 1547 MSG("%s event %s%s created in channel %s",
8274eeba
AB
1548 get_domain_str(dom.type),
1549 event_name,
1550 exclusion_string,
1551 print_channel_name(channel_name));
1552 break;
1553 case LTTNG_DOMAIN_JUL:
1554 case LTTNG_DOMAIN_LOG4J:
1555 case LTTNG_DOMAIN_PYTHON:
1556 /*
1557 * Don't print the default channel
1558 * name for agent domains.
1559 */
895707da 1560 MSG("%s event %s%s enabled",
8274eeba
AB
1561 get_domain_str(dom.type),
1562 event_name,
1563 exclusion_string);
1564 break;
1565 default:
1566 assert(0);
49ceaa70 1567 }
42224349 1568 }
9c48cab3 1569 free(exclusion_string);
6181537c 1570 }
025faf73
DG
1571
1572 if (opt_filter) {
9c48cab3
JI
1573 char *exclusion_string;
1574
89476427 1575 /* Filter present */
91744e14 1576 ev->filter = 1;
89476427 1577
91744e14 1578 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
9f449915
PP
1579 opt_filter,
1580 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1581 exclusion_list);
1582 exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1583 if (!exclusion_string) {
1584 PERROR("Cannot allocate exclusion_string");
1585 error = 1;
1586 goto end;
1587 }
89476427
JRJ
1588 if (command_ret < 0) {
1589 switch (-command_ret) {
7671f53c 1590 case LTTNG_ERR_FILTER_EXIST:
9c48cab3 1591 WARN("Filter on event %s%s is already enabled"
7671f53c 1592 " (channel %s, session %s)",
85076754 1593 event_name,
9c48cab3 1594 exclusion_string,
85076754 1595 print_channel_name(channel_name), session_name);
89476427 1596 warn = 1;
7671f53c 1597 break;
45d5d421
CB
1598 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1599 {
1600 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
91744e14 1601 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
45d5d421
CB
1602 exclusion_string,
1603 msg,
1604 print_channel_name(channel_name),
1605 session_name, opt_filter);
1606 error = 1;
1607 break;
1608 }
7671f53c 1609 default:
91744e14 1610 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
9c48cab3 1611 exclusion_string,
da3d7d0e
JRJ
1612 lttng_strerror(command_ret),
1613 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1614 ? print_raw_channel_name(channel_name)
1615 : print_channel_name(channel_name),
1616 session_name, opt_filter);
89476427 1617 error = 1;
7671f53c
CB
1618 break;
1619 }
89476427
JRJ
1620 error_holder = command_ret;
1621
16363652 1622 } else {
9c48cab3
JI
1623 MSG("Event %s%s: Filter '%s' successfully set",
1624 event_name, exclusion_string,
1625 opt_filter);
53a80697 1626 }
9c48cab3 1627 free(exclusion_string);
53a80697 1628 }
6181537c 1629
89476427
JRJ
1630 if (lttng_opt_mi) {
1631 if (command_ret) {
1632 success = 0;
91744e14 1633 ev->enabled = 0;
89476427 1634 } else {
91744e14 1635 ev->enabled = 1;
89476427
JRJ
1636 }
1637
91744e14 1638 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
1639 if (ret) {
1640 ret = CMD_ERROR;
1641 goto error;
1642 }
1643
1644 /* print exclusion */
9f449915 1645 ret = mi_print_exclusion(exclusion_list);
89476427
JRJ
1646 if (ret) {
1647 ret = CMD_ERROR;
1648 goto error;
1649 }
1650
1651 /* Success ? */
1652 ret = mi_lttng_writer_write_element_bool(writer,
1653 mi_lttng_element_command_success, success);
1654 if (ret) {
1655 ret = CMD_ERROR;
1656 goto end;
1657 }
1658
1659 /* Close event element */
1660 ret = mi_lttng_writer_close_element(writer);
1661 if (ret) {
1662 ret = CMD_ERROR;
1663 goto end;
1664 }
1665 }
1666
f3ed775e
DG
1667 /* Next event */
1668 event_name = strtok(NULL, ",");
89476427
JRJ
1669 /* Reset warn, error and success */
1670 success = 1;
f3ed775e
DG
1671 }
1672
8c9ae521 1673end:
89476427
JRJ
1674 /* Close Mi */
1675 if (lttng_opt_mi) {
1676 /* Close events element */
1677 ret = mi_lttng_writer_close_element(writer);
1678 if (ret) {
1679 ret = CMD_ERROR;
1680 goto error;
1681 }
1682 }
f3ed775e 1683error:
ae856491
DG
1684 if (warn) {
1685 ret = CMD_WARNING;
1686 }
89476427
JRJ
1687 if (error) {
1688 ret = CMD_ERROR;
1689 }
cd80958d 1690 lttng_destroy_handle(handle);
9f449915 1691 strutils_free_null_terminated_array_of_strings(exclusion_list);
7ed70bc9 1692
89476427
JRJ
1693 /* Overwrite ret with error_holder if there was an actual error with
1694 * enabling an event.
1695 */
1696 ret = error_holder ? error_holder : ret;
1697
91744e14 1698 lttng_event_destroy(ev);
f3ed775e
DG
1699 return ret;
1700}
1701
1702/*
6181537c 1703 * Add event to trace session
f3ed775e
DG
1704 */
1705int cmd_enable_events(int argc, const char **argv)
1706{
89476427 1707 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 1708 static poptContext pc;
cd80958d 1709 char *session_name = NULL;
68c7f6e5 1710 const char *leftover = NULL;
de044b7a 1711 int event_type = -1;
f3ed775e
DG
1712
1713 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1714 poptReadDefaultConfig(pc, 0);
1715
1716 /* Default event type */
7a3d1328 1717 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
1718
1719 while ((opt = poptGetNextOpt(pc)) != -1) {
1720 switch (opt) {
1721 case OPT_HELP:
4ba92f18 1722 SHOW_HELP();
f3ed775e 1723 goto end;
f3ed775e 1724 case OPT_TRACEPOINT:
e6ddca71 1725 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 1726 break;
cf0e5467 1727 case OPT_PROBE:
7d29a247 1728 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e 1729 break;
dcabc190
FD
1730 case OPT_USERSPACE_PROBE:
1731 opt_event_type = LTTNG_EVENT_USERSPACE_PROBE;
1732 break;
f3ed775e
DG
1733 case OPT_FUNCTION:
1734 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b 1735 break;
a54bd42d
MD
1736 case OPT_SYSCALL:
1737 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 1738 break;
eeac7d46
MD
1739 case OPT_USERSPACE:
1740 opt_userspace = 1;
eeac7d46 1741 break;
0cda4f28 1742 case OPT_LOGLEVEL:
8005f29a 1743 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
1744 opt_loglevel = poptGetOptArg(pc);
1745 break;
1746 case OPT_LOGLEVEL_ONLY:
8005f29a 1747 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 1748 opt_loglevel = poptGetOptArg(pc);
13dce3b7 1749 break;
679b4943
SM
1750 case OPT_LIST_OPTIONS:
1751 list_cmd_options(stdout, long_options);
679b4943 1752 goto end;
53a80697
MD
1753 case OPT_FILTER:
1754 break;
fac3366c
JI
1755 case OPT_EXCLUDE:
1756 break;
f3ed775e 1757 default:
f3ed775e
DG
1758 ret = CMD_UNDEFINED;
1759 goto end;
1760 }
de044b7a
DG
1761
1762 /* Validate event type. Multiple event type are not supported. */
1763 if (event_type == -1) {
1764 event_type = opt_event_type;
1765 } else {
1766 if (event_type != opt_event_type) {
1767 ERR("Multiple event type not supported.");
1768 ret = CMD_ERROR;
1769 goto end;
1770 }
1771 }
f3ed775e
DG
1772 }
1773
3ecec76a
PP
1774 ret = print_missing_or_multiple_domains(
1775 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
1776 if (ret) {
1777 ret = CMD_ERROR;
1778 goto end;
1779 }
1780
89476427
JRJ
1781 /* Mi check */
1782 if (lttng_opt_mi) {
1783 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1784 if (!writer) {
1785 ret = -LTTNG_ERR_NOMEM;
1786 goto end;
1787 }
1788
1789 /* Open command element */
1790 ret = mi_lttng_writer_command_open(writer,
1791 mi_lttng_element_command_enable_event);
1792 if (ret) {
1793 ret = CMD_ERROR;
1794 goto end;
1795 }
1796
1797 /* Open output element */
1798 ret = mi_lttng_writer_open_element(writer,
1799 mi_lttng_element_command_output);
1800 if (ret) {
1801 ret = CMD_ERROR;
1802 goto end;
1803 }
1804 }
1805
f3ed775e
DG
1806 opt_event_list = (char*) poptGetArg(pc);
1807 if (opt_event_list == NULL && opt_enable_all == 0) {
1808 ERR("Missing event name(s).\n");
ca1c3607 1809 ret = CMD_ERROR;
f3ed775e
DG
1810 goto end;
1811 }
1812
68c7f6e5
JD
1813 leftover = poptGetArg(pc);
1814 if (leftover) {
1815 ERR("Unknown argument: %s", leftover);
1816 ret = CMD_ERROR;
1817 goto end;
1818 }
1819
cd80958d
DG
1820 if (!opt_session_name) {
1821 session_name = get_session_name();
1822 if (session_name == NULL) {
89476427
JRJ
1823 command_ret = CMD_ERROR;
1824 success = 0;
1825 goto mi_closing;
cd80958d
DG
1826 }
1827 } else {
1828 session_name = opt_session_name;
1829 }
1830
89476427
JRJ
1831 command_ret = enable_events(session_name);
1832 if (command_ret) {
1833 success = 0;
1834 goto mi_closing;
1835 }
1836
1837mi_closing:
1838 /* Mi closing */
1839 if (lttng_opt_mi) {
1840 /* Close output element */
1841 ret = mi_lttng_writer_close_element(writer);
1842 if (ret) {
1843 ret = CMD_ERROR;
1844 goto end;
1845 }
1846
1847 ret = mi_lttng_writer_write_element_bool(writer,
1848 mi_lttng_element_command_success, success);
1849 if (ret) {
1850 ret = CMD_ERROR;
1851 goto end;
1852 }
1853
1854 /* Command element close */
1855 ret = mi_lttng_writer_command_close(writer);
1856 if (ret) {
1857 ret = CMD_ERROR;
1858 goto end;
1859 }
1860 }
f3ed775e
DG
1861
1862end:
89476427
JRJ
1863 /* Mi clean-up */
1864 if (writer && mi_lttng_writer_destroy(writer)) {
1865 /* Preserve original error code */
1866 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1867 }
1868
cd80958d
DG
1869 if (opt_session_name == NULL) {
1870 free(session_name);
1871 }
1872
89476427
JRJ
1873 /* Overwrite ret if an error occurred in enable_events */
1874 ret = command_ret ? command_ret : ret;
1875
ca1c3607 1876 poptFreeContext(pc);
f3ed775e
DG
1877 return ret;
1878}
9f449915 1879
This page took 0.153429 seconds and 4 git commands to generate.