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