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