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