Cleanup: always use sysconf to get the page size
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#define _LGPL_SOURCE
9#include <popt.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15#include <inttypes.h>
16#include <ctype.h>
17
18#include <common/sessiond-comm/sessiond-comm.h>
19#include <common/compat/string.h>
20#include <common/compat/getenv.h>
21#include <common/string-utils/string-utils.h>
22#include <common/utils.h>
23
24/* Mi dependancy */
25#include <common/mi-lttng.h>
26
27#include <lttng/domain-internal.h>
28#include <lttng/event-internal.h>
29
30#include "../command.h"
31#include "../loglevel.h"
32#include "../uprobe.h"
33
34#if (LTTNG_SYMBOL_NAME_LEN == 256)
35#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
36#endif
37
38static char *opt_event_list;
39static int opt_event_type;
40static const char *opt_loglevel;
41static int opt_loglevel_type;
42static int opt_kernel;
43static char *opt_session_name;
44static int opt_userspace;
45static int opt_jul;
46static int opt_log4j;
47static int opt_python;
48static int opt_enable_all;
49static char *opt_probe;
50static char *opt_userspace_probe;
51static char *opt_function;
52static char *opt_channel_name;
53static char *opt_filter;
54static char *opt_exclude;
55
56#ifdef LTTNG_EMBED_HELP
57static const char help_msg[] =
58#include <lttng-enable-event.1.h>
59;
60#endif
61
62enum {
63 OPT_HELP = 1,
64 OPT_TRACEPOINT,
65 OPT_PROBE,
66 OPT_USERSPACE_PROBE,
67 OPT_FUNCTION,
68 OPT_SYSCALL,
69 OPT_USERSPACE,
70 OPT_LOGLEVEL,
71 OPT_LOGLEVEL_ONLY,
72 OPT_LIST_OPTIONS,
73 OPT_FILTER,
74 OPT_EXCLUDE,
75};
76
77static struct lttng_handle *handle;
78static struct mi_writer *writer;
79
80static struct poptOption long_options[] = {
81 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
82 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
83 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
84 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
85 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
86 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
87 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
88 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
89 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
90 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
91 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
92 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
93 {"userspace-probe",0, POPT_ARG_STRING, &opt_userspace_probe, OPT_USERSPACE_PROBE, 0, 0},
94 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
95 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
96 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
97 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
98 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
99 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
100 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
101 {0, 0, 0, 0, 0, 0, 0}
102};
103
104/*
105 * Parse probe options.
106 */
107static int parse_probe_opts(struct lttng_event *ev, char *opt)
108{
109 int ret = CMD_SUCCESS;
110 int match;
111 char s_hex[19];
112#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
113 char name[LTTNG_SYMBOL_NAME_LEN];
114
115 if (opt == NULL) {
116 ret = CMD_ERROR;
117 goto end;
118 }
119
120 /* Check for symbol+offset */
121 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
122 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
123 if (match == 2) {
124 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
125 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
126 DBG("probe symbol %s", ev->attr.probe.symbol_name);
127 if (*s_hex == '\0') {
128 ERR("Invalid probe offset %s", s_hex);
129 ret = CMD_ERROR;
130 goto end;
131 }
132 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
133 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
134 ev->attr.probe.addr = 0;
135 goto end;
136 }
137
138 /* Check for symbol */
139 if (isalpha(name[0]) || name[0] == '_') {
140 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
141 name);
142 if (match == 1) {
143 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
144 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
145 DBG("probe symbol %s", ev->attr.probe.symbol_name);
146 ev->attr.probe.offset = 0;
147 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
148 ev->attr.probe.addr = 0;
149 goto end;
150 }
151 }
152
153 /* Check for address */
154 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
155 if (match > 0) {
156 /*
157 * Return an error if the first character of the tentative
158 * address is NULL or not a digit. It can be "0" if the address
159 * is in hexadecimal and can be 1 to 9 if it's in decimal.
160 */
161 if (*s_hex == '\0' || !isdigit(*s_hex)) {
162 ERR("Invalid probe description %s", s_hex);
163 ret = CMD_ERROR;
164 goto end;
165 }
166 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
167 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
168 ev->attr.probe.offset = 0;
169 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
170 goto end;
171 }
172
173 /* No match */
174 ret = CMD_ERROR;
175
176end:
177 return ret;
178}
179
180static
181const char *print_channel_name(const char *name)
182{
183 return name ? : DEFAULT_CHANNEL_NAME;
184}
185
186static
187const char *print_raw_channel_name(const char *name)
188{
189 return name ? : "<default>";
190}
191
192/*
193 * Mi print exlcusion list
194 */
195static
196int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
197{
198 int ret;
199 size_t i;
200 const size_t count = lttng_dynamic_pointer_array_get_count(exclusions);
201
202 LTTNG_ASSERT(writer);
203
204 if (count == 0) {
205 ret = 0;
206 goto end;
207 }
208
209 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
210 if (ret) {
211 goto end;
212 }
213
214 for (i = 0; i < count; i++) {
215 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
216 exclusions, i);
217
218 ret = mi_lttng_writer_write_element_string(writer,
219 config_element_exclusion, exclusion);
220 if (ret) {
221 goto end;
222 }
223 }
224
225 /* Close exclusions element */
226 ret = mi_lttng_writer_close_element(writer);
227
228end:
229 return ret;
230}
231
232/*
233 * Return allocated string for pretty-printing exclusion names.
234 */
235static
236char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions)
237{
238 int length = 0;
239 size_t i;
240 const char preamble[] = " excluding ";
241 char *ret;
242 const size_t count = lttng_dynamic_pointer_array_get_count(exclusions);
243
244 if (count == 0) {
245 return strdup("");
246 }
247
248 /* Calculate total required length. */
249 for (i = 0; i < count; i++) {
250 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
251 exclusions, i);
252
253 length += strlen(exclusion) + 4;
254 }
255
256 length += sizeof(preamble);
257 ret = zmalloc(length);
258 if (!ret) {
259 return NULL;
260 }
261
262 strncpy(ret, preamble, length);
263 for (i = 0; i < count; i++) {
264 const char *exclusion = lttng_dynamic_pointer_array_get_pointer(
265 exclusions, i);
266
267 strcat(ret, "\"");
268 strcat(ret, exclusion);
269 strcat(ret, "\"");
270 if (i != count - 1) {
271 strcat(ret, ", ");
272 }
273 }
274
275 return ret;
276}
277
278static
279int check_exclusion_subsets(const char *event_name, const char *exclusion)
280{
281 bool warn = false;
282 int ret = 0;
283 const char *e = event_name;
284 const char *x = exclusion;
285
286 /* Scan both the excluder and the event letter by letter */
287 while (true) {
288 if (*e == '\\') {
289 if (*x != *e) {
290 warn = true;
291 goto end;
292 }
293
294 e++;
295 x++;
296 goto cmp_chars;
297 }
298
299 if (*x == '*') {
300 /* Event is a subset of the excluder */
301 ERR("Event %s: %s excludes all events from %s",
302 event_name, exclusion, event_name);
303 goto error;
304 }
305
306 if (*e == '*') {
307 /*
308 * Reached the end of the event name before the
309 * end of the exclusion: this is valid.
310 */
311 goto end;
312 }
313
314cmp_chars:
315 if (*x != *e) {
316 warn = true;
317 break;
318 }
319
320 x++;
321 e++;
322 }
323
324 goto end;
325
326error:
327 ret = -1;
328
329end:
330 if (warn) {
331 WARN("Event %s: %s does not exclude any events from %s",
332 event_name, exclusion, event_name);
333 }
334
335 return ret;
336}
337
338int validate_exclusion_list(const char *event_name,
339 const struct lttng_dynamic_pointer_array *exclusions)
340{
341 int ret;
342
343 /* Event name must be a valid globbing pattern to allow exclusions. */
344 if (!strutils_is_star_glob_pattern(event_name)) {
345 ERR("Event %s: Exclusions can only be used with a globbing pattern",
346 event_name);
347 goto error;
348 }
349
350 /*
351 * If the event name is a star-at-end only globbing pattern,
352 * then we can validate the individual exclusions. Otherwise
353 * all exclusions are passed to the session daemon.
354 */
355 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
356 size_t i, num_exclusions;
357
358 num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
359
360 for (i = 0; i < num_exclusions; i++) {
361 const char *exclusion =
362 lttng_dynamic_pointer_array_get_pointer(
363 exclusions, i);
364
365 if (!strutils_is_star_glob_pattern(exclusion) ||
366 strutils_is_star_at_the_end_only_glob_pattern(exclusion)) {
367 ret = check_exclusion_subsets(event_name, exclusion);
368 if (ret) {
369 goto error;
370 }
371 }
372 }
373 }
374
375 ret = 0;
376 goto end;
377
378error:
379 ret = -1;
380
381end:
382 return ret;
383}
384
385static int create_exclusion_list_and_validate(const char *event_name,
386 const char *exclusions_arg,
387 struct lttng_dynamic_pointer_array *exclusions)
388{
389 int ret = 0;
390
391 /* Split exclusions. */
392 ret = strutils_split(exclusions_arg, ',', true, exclusions);
393 if (ret < 0) {
394 goto error;
395 }
396
397 if (validate_exclusion_list(event_name, exclusions) !=
398 0) {
399 goto error;
400 }
401
402 goto end;
403
404error:
405 ret = -1;
406 lttng_dynamic_pointer_array_reset(exclusions);
407
408end:
409 return ret;
410}
411
412static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array *exclusions,
413 int *warn)
414{
415 size_t i;
416 const size_t num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
417
418 for (i = 0; i < num_exclusions; i++) {
419 const char * const exclusion = lttng_dynamic_pointer_array_get_pointer(exclusions, i);
420
421 if (strlen(exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
422 WARN("Event exclusion \"%s\" will be truncated",
423 exclusion);
424 *warn = 1;
425 }
426 }
427}
428
429/*
430 * Enabling event using the lttng API.
431 * Note: in case of error only the last error code will be return.
432 */
433static int enable_events(char *session_name)
434{
435 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
436 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
437 char *event_name, *channel_name = NULL;
438 struct lttng_event *ev;
439 struct lttng_domain dom = {};
440 struct lttng_dynamic_pointer_array exclusions;
441 struct lttng_userspace_probe_location *uprobe_loc = NULL;
442
443 lttng_dynamic_pointer_array_init(&exclusions, NULL);
444
445 ev = lttng_event_create();
446 if (!ev) {
447 ret = CMD_ERROR;
448 goto error;
449 }
450
451 if (opt_kernel) {
452 if (opt_loglevel) {
453 WARN("Kernel loglevels are not supported.");
454 }
455 }
456
457 /* Create lttng domain */
458 if (opt_kernel) {
459 dom.type = LTTNG_DOMAIN_KERNEL;
460 dom.buf_type = LTTNG_BUFFER_GLOBAL;
461 } else if (opt_userspace) {
462 dom.type = LTTNG_DOMAIN_UST;
463 /* Default. */
464 dom.buf_type = LTTNG_BUFFER_PER_UID;
465 } else if (opt_jul) {
466 dom.type = LTTNG_DOMAIN_JUL;
467 /* Default. */
468 dom.buf_type = LTTNG_BUFFER_PER_UID;
469 } else if (opt_log4j) {
470 dom.type = LTTNG_DOMAIN_LOG4J;
471 /* Default. */
472 dom.buf_type = LTTNG_BUFFER_PER_UID;
473 } else if (opt_python) {
474 dom.type = LTTNG_DOMAIN_PYTHON;
475 /* Default. */
476 dom.buf_type = LTTNG_BUFFER_PER_UID;
477 } else {
478 /* Checked by the caller. */
479 abort();
480 }
481
482 if (opt_exclude) {
483 switch (dom.type) {
484 case LTTNG_DOMAIN_KERNEL:
485 case LTTNG_DOMAIN_JUL:
486 case LTTNG_DOMAIN_LOG4J:
487 case LTTNG_DOMAIN_PYTHON:
488 ERR("Event name exclusions are not yet implemented for %s events",
489 lttng_domain_type_str(dom.type));
490 ret = CMD_ERROR;
491 goto error;
492 case LTTNG_DOMAIN_UST:
493 /* Exclusions supported */
494 break;
495 default:
496 abort();
497 }
498 }
499
500 /*
501 * Adding a filter to a probe, function or userspace-probe would be
502 * denied by the kernel tracer as it's not supported at the moment. We
503 * do an early check here to warn the user.
504 */
505 if (opt_filter && opt_kernel) {
506 switch (opt_event_type) {
507 case LTTNG_EVENT_ALL:
508 case LTTNG_EVENT_TRACEPOINT:
509 case LTTNG_EVENT_SYSCALL:
510 break;
511 case LTTNG_EVENT_PROBE:
512 case LTTNG_EVENT_USERSPACE_PROBE:
513 case LTTNG_EVENT_FUNCTION:
514 ERR("Filter expressions are not supported for %s events",
515 get_event_type_str(opt_event_type));
516 ret = CMD_ERROR;
517 goto error;
518 default:
519 ret = CMD_UNDEFINED;
520 goto error;
521 }
522 }
523
524 channel_name = opt_channel_name;
525
526 handle = lttng_create_handle(session_name, &dom);
527 if (handle == NULL) {
528 ret = -1;
529 goto error;
530 }
531
532 /* Prepare Mi */
533 if (lttng_opt_mi) {
534 /* Open a events element */
535 ret = mi_lttng_writer_open_element(writer, config_element_events);
536 if (ret) {
537 ret = CMD_ERROR;
538 goto error;
539 }
540 }
541
542 if (opt_enable_all) {
543 /* Default setup for enable all */
544 if (opt_kernel) {
545 ev->type = opt_event_type;
546 strcpy(ev->name, "*");
547 /* kernel loglevels not implemented */
548 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
549 } else {
550 ev->type = LTTNG_EVENT_TRACEPOINT;
551 strcpy(ev->name, "*");
552 ev->loglevel_type = opt_loglevel_type;
553 if (opt_loglevel) {
554 int name_search_ret;
555
556 LTTNG_ASSERT(opt_userspace || opt_jul || opt_log4j || opt_python);
557
558 if (opt_userspace) {
559 enum lttng_loglevel loglevel;
560
561 name_search_ret = loglevel_name_to_value(opt_loglevel, &loglevel);
562 ev->loglevel = (int) loglevel;
563 } else if (opt_jul) {
564 enum lttng_loglevel_jul loglevel;
565
566 name_search_ret = loglevel_jul_name_to_value(opt_loglevel, &loglevel);
567 ev->loglevel = (int) loglevel;
568 } else if (opt_log4j) {
569 enum lttng_loglevel_log4j loglevel;
570
571 name_search_ret = loglevel_log4j_name_to_value(opt_loglevel, &loglevel);
572 ev->loglevel = (int) loglevel;
573 } else {
574 /* python domain. */
575 enum lttng_loglevel_python loglevel;
576
577 name_search_ret = loglevel_python_name_to_value(opt_loglevel, &loglevel);
578 ev->loglevel = (int) loglevel;
579 }
580
581 if (name_search_ret == -1) {
582 ERR("Unknown loglevel %s", opt_loglevel);
583 ret = -LTTNG_ERR_INVALID;
584 goto error;
585 }
586 } else {
587 LTTNG_ASSERT(opt_userspace || opt_jul || opt_log4j || opt_python);
588 if (opt_userspace) {
589 ev->loglevel = -1;
590 } else if (opt_jul) {
591 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
592 } else if (opt_log4j) {
593 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
594 } else if (opt_python) {
595 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
596 }
597 }
598 }
599
600 if (opt_exclude) {
601 ret = create_exclusion_list_and_validate("*",
602 opt_exclude, &exclusions);
603 if (ret) {
604 ret = CMD_ERROR;
605 goto error;
606 }
607
608 ev->exclusion = 1;
609 warn_on_truncated_exclusion_names(&exclusions,
610 &warn);
611 }
612 if (!opt_filter) {
613 ret = lttng_enable_event_with_exclusions(handle,
614 ev, channel_name,
615 NULL,
616 lttng_dynamic_pointer_array_get_count(&exclusions),
617 (char **) exclusions.array.buffer.data);
618 if (ret < 0) {
619 switch (-ret) {
620 case LTTNG_ERR_KERN_EVENT_EXIST:
621 WARN("Kernel events already enabled (channel %s, session %s)",
622 print_channel_name(channel_name), session_name);
623 warn = 1;
624 break;
625 case LTTNG_ERR_TRACE_ALREADY_STARTED:
626 {
627 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
628 ERR("Events: %s (channel %s, session %s)",
629 msg,
630 print_channel_name(channel_name),
631 session_name);
632 error = 1;
633 break;
634 }
635 default:
636 ERR("Events: %s (channel %s, session %s)",
637 lttng_strerror(ret),
638 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
639 ? print_raw_channel_name(channel_name)
640 : print_channel_name(channel_name),
641 session_name);
642 error = 1;
643 break;
644 }
645 goto end;
646 }
647
648 switch (opt_event_type) {
649 case LTTNG_EVENT_TRACEPOINT:
650 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
651 char *exclusion_string = print_exclusions(&exclusions);
652
653 if (!exclusion_string) {
654 PERROR("Cannot allocate exclusion_string");
655 error = 1;
656 goto end;
657 }
658 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
659 lttng_domain_type_str(dom.type),
660 exclusion_string,
661 print_channel_name(channel_name),
662 opt_loglevel);
663 free(exclusion_string);
664 } else {
665 char *exclusion_string = print_exclusions(&exclusions);
666
667 if (!exclusion_string) {
668 PERROR("Cannot allocate exclusion_string");
669 error = 1;
670 goto end;
671 }
672 MSG("All %s tracepoints%s are enabled in channel %s",
673 lttng_domain_type_str(dom.type),
674 exclusion_string,
675 print_channel_name(channel_name));
676 free(exclusion_string);
677 }
678 break;
679 case LTTNG_EVENT_SYSCALL:
680 if (opt_kernel) {
681 MSG("All %s system calls are enabled in channel %s",
682 lttng_domain_type_str(dom.type),
683 print_channel_name(channel_name));
684 }
685 break;
686 case LTTNG_EVENT_ALL:
687 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
688 char *exclusion_string = print_exclusions(&exclusions);
689
690 if (!exclusion_string) {
691 PERROR("Cannot allocate exclusion_string");
692 error = 1;
693 goto end;
694 }
695 MSG("All %s events%s are enabled in channel %s for loglevel %s",
696 lttng_domain_type_str(dom.type),
697 exclusion_string,
698 print_channel_name(channel_name),
699 opt_loglevel);
700 free(exclusion_string);
701 } else {
702 char *exclusion_string = print_exclusions(&exclusions);
703
704 if (!exclusion_string) {
705 PERROR("Cannot allocate exclusion_string");
706 error = 1;
707 goto end;
708 }
709 MSG("All %s events%s are enabled in channel %s",
710 lttng_domain_type_str(dom.type),
711 exclusion_string,
712 print_channel_name(channel_name));
713 free(exclusion_string);
714 }
715 break;
716 default:
717 /*
718 * We should not be here since lttng_enable_event should have
719 * failed on the event type.
720 */
721 goto error;
722 }
723 }
724
725 if (opt_filter) {
726 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
727 opt_filter,
728 lttng_dynamic_pointer_array_get_count(&exclusions),
729 (char **) exclusions.array.buffer.data);
730 if (command_ret < 0) {
731 switch (-command_ret) {
732 case LTTNG_ERR_FILTER_EXIST:
733 WARN("Filter on all events is already enabled"
734 " (channel %s, session %s)",
735 print_channel_name(channel_name), session_name);
736 warn = 1;
737 break;
738 case LTTNG_ERR_TRACE_ALREADY_STARTED:
739 {
740 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
741 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
742 msg,
743 print_channel_name(channel_name),
744 session_name, opt_filter);
745 error = 1;
746 break;
747 }
748 default:
749 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
750 lttng_strerror(command_ret),
751 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
752 ? print_raw_channel_name(channel_name)
753 : print_channel_name(channel_name),
754 session_name, opt_filter);
755 error = 1;
756 break;
757 }
758 error_holder = command_ret;
759 } else {
760 ev->filter = 1;
761 MSG("Filter '%s' successfully set", opt_filter);
762 }
763 }
764
765 if (lttng_opt_mi) {
766 /* The wildcard * is used for kernel and ust domain to
767 * represent ALL. We copy * in event name to force the wildcard use
768 * for kernel domain
769 *
770 * Note: this is strictly for semantic and printing while in
771 * machine interface mode.
772 */
773 strcpy(ev->name, "*");
774
775 /* If we reach here the events are enabled */
776 if (!error && !warn) {
777 ev->enabled = 1;
778 } else {
779 ev->enabled = 0;
780 success = 0;
781 }
782 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
783 if (ret) {
784 ret = CMD_ERROR;
785 goto error;
786 }
787
788 /* print exclusion */
789 ret = mi_print_exclusion(&exclusions);
790 if (ret) {
791 ret = CMD_ERROR;
792 goto error;
793 }
794
795 /* Success ? */
796 ret = mi_lttng_writer_write_element_bool(writer,
797 mi_lttng_element_command_success, success);
798 if (ret) {
799 ret = CMD_ERROR;
800 goto error;
801 }
802
803 /* Close event element */
804 ret = mi_lttng_writer_close_element(writer);
805 if (ret) {
806 ret = CMD_ERROR;
807 goto error;
808 }
809 }
810
811 goto end;
812 }
813
814 /* Strip event list */
815 event_name = strtok(opt_event_list, ",");
816 while (event_name != NULL) {
817 /* Copy name and type of the event */
818 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
819 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
820 ev->type = opt_event_type;
821
822 /* Kernel tracer action */
823 if (opt_kernel) {
824 DBG("Enabling kernel event %s for channel %s",
825 event_name,
826 print_channel_name(channel_name));
827
828 switch (opt_event_type) {
829 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
830 /* If event name differs from *, select tracepoint. */
831 if (strcmp(ev->name, "*")) {
832 ev->type = LTTNG_EVENT_TRACEPOINT;
833 }
834 break;
835 case LTTNG_EVENT_TRACEPOINT:
836 break;
837 case LTTNG_EVENT_PROBE:
838 ret = parse_probe_opts(ev, opt_probe);
839 if (ret) {
840 ERR("Unable to parse probe options");
841 ret = CMD_ERROR;
842 goto error;
843 }
844 break;
845 case LTTNG_EVENT_USERSPACE_PROBE:
846 LTTNG_ASSERT(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
847
848 ret = parse_userspace_probe_opts(opt_userspace_probe, &uprobe_loc);
849 if (ret) {
850 switch (ret) {
851 case CMD_UNSUPPORTED:
852 /*
853 * Error message describing
854 * what is not supported was
855 * printed in the function.
856 */
857 break;
858 case CMD_ERROR:
859 default:
860 ERR("Unable to parse userspace probe options");
861 break;
862 }
863 goto error;
864 }
865
866 ret = lttng_event_set_userspace_probe_location(ev, uprobe_loc);
867 if (ret) {
868 WARN("Failed to set probe location on event");
869 ret = CMD_ERROR;
870 goto error;
871 }
872
873 /* Ownership of the uprobe location was transferred to the event. */
874 uprobe_loc = NULL;
875 break;
876 case LTTNG_EVENT_FUNCTION:
877 ret = parse_probe_opts(ev, opt_function);
878 if (ret) {
879 ERR("Unable to parse function probe options");
880 ret = CMD_ERROR;
881 goto error;
882 }
883 break;
884 case LTTNG_EVENT_SYSCALL:
885 ev->type = LTTNG_EVENT_SYSCALL;
886 break;
887 default:
888 ret = CMD_UNDEFINED;
889 goto error;
890 }
891
892 /* kernel loglevels not implemented */
893 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
894 } else if (opt_userspace) { /* User-space tracer action */
895 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
896 print_channel_name(channel_name), opt_loglevel ? : "<all>");
897
898 switch (opt_event_type) {
899 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
900 /* Fall-through */
901 case LTTNG_EVENT_TRACEPOINT:
902 /* Copy name and type of the event */
903 ev->type = LTTNG_EVENT_TRACEPOINT;
904 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
905 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
906 break;
907 case LTTNG_EVENT_PROBE:
908 case LTTNG_EVENT_FUNCTION:
909 case LTTNG_EVENT_SYSCALL:
910 case LTTNG_EVENT_USERSPACE_PROBE:
911 default:
912 ERR("Event type not available for user-space tracing");
913 ret = CMD_UNSUPPORTED;
914 goto error;
915 }
916
917 if (opt_exclude) {
918 ev->exclusion = 1;
919 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
920 ERR("Exclusion option can only be used with tracepoint events");
921 ret = CMD_ERROR;
922 goto error;
923 }
924 /* Free previously allocated items. */
925 lttng_dynamic_pointer_array_reset(&exclusions);
926 ret = create_exclusion_list_and_validate(
927 event_name, opt_exclude,
928 &exclusions);
929 if (ret) {
930 ret = CMD_ERROR;
931 goto error;
932 }
933
934 warn_on_truncated_exclusion_names(
935 &exclusions, &warn);
936 }
937
938 ev->loglevel_type = opt_loglevel_type;
939 if (opt_loglevel) {
940 enum lttng_loglevel loglevel;
941 const int name_search_ret = loglevel_name_to_value(opt_loglevel, &loglevel);
942
943 if (name_search_ret == -1) {
944 ERR("Unknown loglevel %s", opt_loglevel);
945 ret = -LTTNG_ERR_INVALID;
946 goto error;
947 }
948
949 ev->loglevel = (int) loglevel;
950 } else {
951 ev->loglevel = -1;
952 }
953 } else if (opt_jul || opt_log4j || opt_python) {
954 if (opt_event_type != LTTNG_EVENT_ALL &&
955 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
956 ERR("Event type not supported for domain.");
957 ret = CMD_UNSUPPORTED;
958 goto error;
959 }
960
961 ev->loglevel_type = opt_loglevel_type;
962 if (opt_loglevel) {
963 int name_search_ret;
964
965 if (opt_jul) {
966 enum lttng_loglevel_jul loglevel;
967
968 name_search_ret = loglevel_jul_name_to_value(opt_loglevel, &loglevel);
969 ev->loglevel = (int) loglevel;
970 } else if (opt_log4j) {
971 enum lttng_loglevel_log4j loglevel;
972
973 name_search_ret = loglevel_log4j_name_to_value(opt_loglevel, &loglevel);
974 ev->loglevel = (int) loglevel;
975 } else {
976 /* python domain. */
977 enum lttng_loglevel_python loglevel;
978
979 name_search_ret = loglevel_python_name_to_value(opt_loglevel, &loglevel);
980 ev->loglevel = (int) loglevel;
981 }
982
983 if (name_search_ret) {
984 ERR("Unknown loglevel %s", opt_loglevel);
985 ret = -LTTNG_ERR_INVALID;
986 goto error;
987 }
988 } else {
989 if (opt_jul) {
990 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
991 } else if (opt_log4j) {
992 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
993 } else if (opt_python) {
994 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
995 }
996 }
997 ev->type = LTTNG_EVENT_TRACEPOINT;
998 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
999 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1000 } else {
1001 abort();
1002 }
1003
1004 if (!opt_filter) {
1005 char *exclusion_string;
1006
1007 command_ret = lttng_enable_event_with_exclusions(handle,
1008 ev, channel_name,
1009 NULL,
1010 lttng_dynamic_pointer_array_get_count(&exclusions),
1011 (char **) exclusions.array.buffer.data);
1012 exclusion_string = print_exclusions(&exclusions);
1013 if (!exclusion_string) {
1014 PERROR("Cannot allocate exclusion_string");
1015 error = 1;
1016 goto end;
1017 }
1018 if (command_ret < 0) {
1019 /* Turn ret to positive value to handle the positive error code */
1020 switch (-command_ret) {
1021 case LTTNG_ERR_KERN_EVENT_EXIST:
1022 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1023 event_name,
1024 exclusion_string,
1025 print_channel_name(channel_name), session_name);
1026 warn = 1;
1027 break;
1028 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1029 {
1030 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1031 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1032 exclusion_string,
1033 msg,
1034 print_channel_name(channel_name),
1035 session_name);
1036 error = 1;
1037 break;
1038 }
1039 case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
1040 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1041 event_name, print_channel_name(channel_name),
1042 session_name);
1043 error = 1;
1044 break;
1045 default:
1046 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1047 exclusion_string,
1048 lttng_strerror(command_ret),
1049 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1050 ? print_raw_channel_name(channel_name)
1051 : print_channel_name(channel_name),
1052 session_name);
1053 error = 1;
1054 break;
1055 }
1056 error_holder = command_ret;
1057 } else {
1058 switch (dom.type) {
1059 case LTTNG_DOMAIN_KERNEL:
1060 case LTTNG_DOMAIN_UST:
1061 MSG("%s event %s%s created in channel %s",
1062 lttng_domain_type_str(dom.type),
1063 event_name,
1064 exclusion_string,
1065 print_channel_name(channel_name));
1066 break;
1067 case LTTNG_DOMAIN_JUL:
1068 case LTTNG_DOMAIN_LOG4J:
1069 case LTTNG_DOMAIN_PYTHON:
1070 /*
1071 * Don't print the default channel
1072 * name for agent domains.
1073 */
1074 MSG("%s event %s%s enabled",
1075 lttng_domain_type_str(dom.type),
1076 event_name,
1077 exclusion_string);
1078 break;
1079 default:
1080 abort();
1081 }
1082 }
1083 free(exclusion_string);
1084 }
1085
1086 if (opt_filter) {
1087 char *exclusion_string;
1088
1089 /* Filter present */
1090 ev->filter = 1;
1091
1092 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
1093 opt_filter,
1094 lttng_dynamic_pointer_array_get_count(&exclusions),
1095 (char **) exclusions.array.buffer.data);
1096 exclusion_string = print_exclusions(&exclusions);
1097 if (!exclusion_string) {
1098 PERROR("Cannot allocate exclusion_string");
1099 error = 1;
1100 goto end;
1101 }
1102 if (command_ret < 0) {
1103 switch (-command_ret) {
1104 case LTTNG_ERR_FILTER_EXIST:
1105 WARN("Filter on event %s%s is already enabled"
1106 " (channel %s, session %s)",
1107 event_name,
1108 exclusion_string,
1109 print_channel_name(channel_name), session_name);
1110 warn = 1;
1111 break;
1112 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1113 {
1114 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1115 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
1116 exclusion_string,
1117 msg,
1118 print_channel_name(channel_name),
1119 session_name, opt_filter);
1120 error = 1;
1121 break;
1122 }
1123 default:
1124 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
1125 exclusion_string,
1126 lttng_strerror(command_ret),
1127 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1128 ? print_raw_channel_name(channel_name)
1129 : print_channel_name(channel_name),
1130 session_name, opt_filter);
1131 error = 1;
1132 break;
1133 }
1134 error_holder = command_ret;
1135
1136 } else {
1137 MSG("Event %s%s: Filter '%s' successfully set",
1138 event_name, exclusion_string,
1139 opt_filter);
1140 }
1141 free(exclusion_string);
1142 }
1143
1144 if (lttng_opt_mi) {
1145 if (command_ret) {
1146 success = 0;
1147 ev->enabled = 0;
1148 } else {
1149 ev->enabled = 1;
1150 }
1151
1152 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
1153 if (ret) {
1154 ret = CMD_ERROR;
1155 goto error;
1156 }
1157
1158 /* print exclusion */
1159 ret = mi_print_exclusion(&exclusions);
1160 if (ret) {
1161 ret = CMD_ERROR;
1162 goto error;
1163 }
1164
1165 /* Success ? */
1166 ret = mi_lttng_writer_write_element_bool(writer,
1167 mi_lttng_element_command_success, success);
1168 if (ret) {
1169 ret = CMD_ERROR;
1170 goto end;
1171 }
1172
1173 /* Close event element */
1174 ret = mi_lttng_writer_close_element(writer);
1175 if (ret) {
1176 ret = CMD_ERROR;
1177 goto end;
1178 }
1179 }
1180
1181 /* Next event */
1182 event_name = strtok(NULL, ",");
1183 /* Reset warn, error and success */
1184 success = 1;
1185 }
1186
1187end:
1188 /* Close Mi */
1189 if (lttng_opt_mi) {
1190 /* Close events element */
1191 ret = mi_lttng_writer_close_element(writer);
1192 if (ret) {
1193 ret = CMD_ERROR;
1194 goto error;
1195 }
1196 }
1197error:
1198 if (warn) {
1199 ret = CMD_WARNING;
1200 }
1201 if (error) {
1202 ret = CMD_ERROR;
1203 }
1204 lttng_destroy_handle(handle);
1205 lttng_dynamic_pointer_array_reset(&exclusions);
1206 lttng_userspace_probe_location_destroy(uprobe_loc);
1207
1208 /* Overwrite ret with error_holder if there was an actual error with
1209 * enabling an event.
1210 */
1211 ret = error_holder ? error_holder : ret;
1212
1213 lttng_event_destroy(ev);
1214 return ret;
1215}
1216
1217/*
1218 * Add event to trace session
1219 */
1220int cmd_enable_events(int argc, const char **argv)
1221{
1222 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
1223 static poptContext pc;
1224 char *session_name = NULL;
1225 const char *leftover = NULL;
1226 int event_type = -1;
1227
1228 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1229 poptReadDefaultConfig(pc, 0);
1230
1231 /* Default event type */
1232 opt_event_type = LTTNG_EVENT_ALL;
1233
1234 while ((opt = poptGetNextOpt(pc)) != -1) {
1235 switch (opt) {
1236 case OPT_HELP:
1237 SHOW_HELP();
1238 goto end;
1239 case OPT_TRACEPOINT:
1240 opt_event_type = LTTNG_EVENT_TRACEPOINT;
1241 break;
1242 case OPT_PROBE:
1243 opt_event_type = LTTNG_EVENT_PROBE;
1244 break;
1245 case OPT_USERSPACE_PROBE:
1246 opt_event_type = LTTNG_EVENT_USERSPACE_PROBE;
1247 break;
1248 case OPT_FUNCTION:
1249 opt_event_type = LTTNG_EVENT_FUNCTION;
1250 break;
1251 case OPT_SYSCALL:
1252 opt_event_type = LTTNG_EVENT_SYSCALL;
1253 break;
1254 case OPT_USERSPACE:
1255 opt_userspace = 1;
1256 break;
1257 case OPT_LOGLEVEL:
1258 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
1259 opt_loglevel = poptGetOptArg(pc);
1260 break;
1261 case OPT_LOGLEVEL_ONLY:
1262 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
1263 opt_loglevel = poptGetOptArg(pc);
1264 break;
1265 case OPT_LIST_OPTIONS:
1266 list_cmd_options(stdout, long_options);
1267 goto end;
1268 case OPT_FILTER:
1269 break;
1270 case OPT_EXCLUDE:
1271 break;
1272 default:
1273 ret = CMD_UNDEFINED;
1274 goto end;
1275 }
1276
1277 /* Validate event type. Multiple event type are not supported. */
1278 if (event_type == -1) {
1279 event_type = opt_event_type;
1280 } else {
1281 if (event_type != opt_event_type) {
1282 ERR("Multiple event type not supported.");
1283 ret = CMD_ERROR;
1284 goto end;
1285 }
1286 }
1287 }
1288
1289 ret = print_missing_or_multiple_domains(
1290 opt_kernel + opt_userspace + opt_jul + opt_log4j +
1291 opt_python,
1292 true);
1293 if (ret) {
1294 ret = CMD_ERROR;
1295 goto end;
1296 }
1297
1298 /* Mi check */
1299 if (lttng_opt_mi) {
1300 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1301 if (!writer) {
1302 ret = -LTTNG_ERR_NOMEM;
1303 goto end;
1304 }
1305
1306 /* Open command element */
1307 ret = mi_lttng_writer_command_open(writer,
1308 mi_lttng_element_command_enable_event);
1309 if (ret) {
1310 ret = CMD_ERROR;
1311 goto end;
1312 }
1313
1314 /* Open output element */
1315 ret = mi_lttng_writer_open_element(writer,
1316 mi_lttng_element_command_output);
1317 if (ret) {
1318 ret = CMD_ERROR;
1319 goto end;
1320 }
1321 }
1322
1323 opt_event_list = (char*) poptGetArg(pc);
1324 if (opt_event_list == NULL && opt_enable_all == 0) {
1325 ERR("Missing event name(s).\n");
1326 ret = CMD_ERROR;
1327 goto end;
1328 }
1329
1330 leftover = poptGetArg(pc);
1331 if (leftover) {
1332 ERR("Unknown argument: %s", leftover);
1333 ret = CMD_ERROR;
1334 goto end;
1335 }
1336
1337 if (!opt_session_name) {
1338 session_name = get_session_name();
1339 if (session_name == NULL) {
1340 command_ret = CMD_ERROR;
1341 success = 0;
1342 goto mi_closing;
1343 }
1344 } else {
1345 session_name = opt_session_name;
1346 }
1347
1348 command_ret = enable_events(session_name);
1349 if (command_ret) {
1350 success = 0;
1351 goto mi_closing;
1352 }
1353
1354mi_closing:
1355 /* Mi closing */
1356 if (lttng_opt_mi) {
1357 /* Close output element */
1358 ret = mi_lttng_writer_close_element(writer);
1359 if (ret) {
1360 ret = CMD_ERROR;
1361 goto end;
1362 }
1363
1364 ret = mi_lttng_writer_write_element_bool(writer,
1365 mi_lttng_element_command_success, success);
1366 if (ret) {
1367 ret = CMD_ERROR;
1368 goto end;
1369 }
1370
1371 /* Command element close */
1372 ret = mi_lttng_writer_command_close(writer);
1373 if (ret) {
1374 ret = CMD_ERROR;
1375 goto end;
1376 }
1377 }
1378
1379end:
1380 /* Mi clean-up */
1381 if (writer && mi_lttng_writer_destroy(writer)) {
1382 /* Preserve original error code */
1383 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1384 }
1385
1386 if (opt_session_name == NULL) {
1387 free(session_name);
1388 }
1389
1390 /* Overwrite ret if an error occurred in enable_events */
1391 ret = command_ret ? command_ret : ret;
1392
1393 poptFreeContext(pc);
1394 return ret;
1395}
1396
This page took 0.031771 seconds and 4 git commands to generate.