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