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