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