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