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