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