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