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