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