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