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