Fix: lttng: add-trigger: leak of parser context on capture
[lttng-tools.git] / src / bin / lttng / commands / add_trigger.cpp
1 /*
2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdarg.h>
12
13 #include "../command.hpp"
14 #include "../loglevel.hpp"
15 #include "../uprobe.hpp"
16
17 #include "common/argpar/argpar.h"
18 #include "common/argpar-utils/argpar-utils.hpp"
19 #include "common/dynamic-array.hpp"
20 #include "common/mi-lttng.hpp"
21 #include "common/string-utils/string-utils.hpp"
22 #include "common/utils.hpp"
23 #include <lttng/domain-internal.hpp>
24 /* For lttng_event_rule_type_str(). */
25 #include <lttng/event-rule/event-rule-internal.hpp>
26 #include <lttng/lttng.h>
27 #include "common/filter/filter-ast.hpp"
28 #include "common/filter/filter-ir.hpp"
29 #include "common/dynamic-array.hpp"
30
31 #if (LTTNG_SYMBOL_NAME_LEN == 256)
32 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
33 #endif
34
35 #ifdef LTTNG_EMBED_HELP
36 static const char help_msg[] =
37 #include <lttng-add-trigger.1.h>
38 ;
39 #endif
40
41 enum {
42 OPT_HELP,
43 OPT_LIST_OPTIONS,
44
45 OPT_CONDITION,
46 OPT_ACTION,
47 OPT_ID,
48 OPT_OWNER_UID,
49 OPT_RATE_POLICY,
50
51 OPT_NAME,
52 OPT_FILTER,
53 OPT_EXCLUDE_NAME,
54 OPT_EVENT_NAME,
55 OPT_LOG_LEVEL,
56
57 OPT_TYPE,
58 OPT_LOCATION,
59
60 OPT_MAX_SIZE,
61 OPT_DATA_URL,
62 OPT_CTRL_URL,
63 OPT_URL,
64 OPT_PATH,
65
66 OPT_CAPTURE,
67 };
68
69 static const struct argpar_opt_descr event_rule_opt_descrs[] = {
70 { OPT_FILTER, 'f', "filter", true },
71 { OPT_NAME, 'n', "name", true },
72 { OPT_EXCLUDE_NAME, 'x', "exclude-name", true },
73 { OPT_LOG_LEVEL, 'l', "log-level", true },
74 { OPT_EVENT_NAME, 'E', "event-name", true },
75
76 { OPT_TYPE, 't', "type", true },
77 { OPT_LOCATION, 'L', "location", true },
78
79 /* Capture descriptor */
80 { OPT_CAPTURE, '\0', "capture", true },
81
82 ARGPAR_OPT_DESCR_SENTINEL
83 };
84
85 static
86 bool has_syscall_prefix(const char *arg)
87 {
88 bool matches = false;
89 const char kernel_syscall_type_opt_prefix[] = "kernel:syscall";
90 const size_t kernel_syscall_type_opt_prefix_len =
91 sizeof(kernel_syscall_type_opt_prefix) - 1;
92 const char syscall_type_opt_prefix[] = "syscall";
93 const size_t syscall_type_opt_prefix_len =
94 sizeof(syscall_type_opt_prefix) - 1;
95
96 if (strncmp(arg, syscall_type_opt_prefix,
97 syscall_type_opt_prefix_len) == 0) {
98 matches = true;
99 } else if (strncmp(arg, kernel_syscall_type_opt_prefix,
100 kernel_syscall_type_opt_prefix_len) == 0) {
101 matches = true;
102 } else {
103 matches = false;
104 }
105
106 return matches;
107 }
108
109 static
110 bool assign_event_rule_type(enum lttng_event_rule_type *dest, const char *arg)
111 {
112 bool ret;
113
114 if (*dest != LTTNG_EVENT_RULE_TYPE_UNKNOWN) {
115 ERR("More than one `--type` was specified.");
116 goto error;
117 }
118
119 if (strcmp(arg, "user") == 0 || strcmp(arg, "user:tracepoint") == 0) {
120 *dest = LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT;
121 } else if (strcmp(arg, "kernel") == 0 ||
122 strcmp(arg, "kernel:tracepoint") == 0) {
123 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT;
124 } else if (strcmp(arg, "jul") == 0 || strcmp(arg, "jul:logging") == 0) {
125 *dest = LTTNG_EVENT_RULE_TYPE_JUL_LOGGING;
126 } else if (strcmp(arg, "log4j") == 0 ||
127 strcmp(arg, "log4j:logging") == 0) {
128 *dest = LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING;
129 } else if (strcmp(arg, "python") == 0 ||
130 strcmp(arg, "python:logging") == 0) {
131 *dest = LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING;
132 } else if (strcmp(arg, "kprobe") == 0 ||
133 strcmp(arg, "kernel:kprobe") == 0) {
134 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE;
135 } else if (strcmp(arg, "kernel:uprobe") == 0) {
136 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE;
137 } else if (has_syscall_prefix(arg)) {
138 /*
139 * Matches the following:
140 * - syscall
141 * - syscall:entry
142 * - syscall:exit
143 * - syscall:entry+exit
144 * - syscall:*
145 * - kernel:syscall
146 * - kernel:syscall:entry
147 * - kernel:syscall:exit
148 * - kernel:syscall:entry+exit
149 * - kernel:syscall:*
150 *
151 * Validation for the right side is left to further usage sites.
152 */
153 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL;
154 } else {
155 ERR("Invalid `--type` value: %s", arg);
156 goto error;
157 }
158
159 ret = true;
160 goto end;
161
162 error:
163 ret = false;
164
165 end:
166 return ret;
167 }
168
169 static
170 bool assign_string(char **dest, const char *src, const char *opt_name)
171 {
172 bool ret;
173
174 if (*dest) {
175 ERR("Duplicate '%s' given.", opt_name);
176 goto error;
177 }
178
179 *dest = strdup(src);
180 if (!*dest) {
181 PERROR("Failed to allocate string '%s'.", opt_name);
182 goto error;
183 }
184
185 ret = true;
186 goto end;
187
188 error:
189 ret = false;
190
191 end:
192 return ret;
193 }
194
195 static bool parse_syscall_emission_site_from_type(const char *str,
196 enum lttng_event_rule_kernel_syscall_emission_site *type)
197 {
198 bool ret = false;
199 const char kernel_prefix[] = "kernel:";
200 const size_t kernel_prefix_len = sizeof(kernel_prefix) - 1;
201
202 /*
203 * If the passed string is of the form "kernel:syscall*", move the
204 * pointer passed "kernel:".
205 */
206 if (strncmp(str, kernel_prefix, kernel_prefix_len) == 0) {
207 str = &str[kernel_prefix_len];
208 }
209
210 if (strcmp(str, "syscall") == 0 ||
211 strcmp(str, "syscall:entry+exit") == 0) {
212 *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT;
213 } else if (strcmp(str, "syscall:entry") == 0) {
214 *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY;
215 } else if (strcmp(str, "syscall:exit") == 0) {
216 *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT;
217 } else {
218 goto error;
219 }
220
221 ret = true;
222
223 error:
224 return ret;
225 }
226
227 /*
228 * Parse `str` as a log level against the passed event rule type.
229 *
230 * Return the log level in `*log_level`. Return true in `*log_level_only` if
231 * the string specifies exactly this log level, false if it specifies at least
232 * this log level.
233 *
234 * Return true if the string was successfully parsed as a log level string.
235 */
236 static bool parse_log_level_string(const char *str,
237 enum lttng_event_rule_type event_rule_type,
238 int *log_level,
239 bool *log_level_only)
240 {
241 bool ret;
242
243 switch (event_rule_type) {
244 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
245 {
246 enum lttng_loglevel log_level_min, log_level_max;
247 if (!loglevel_parse_range_string(
248 str, &log_level_min, &log_level_max)) {
249 goto error;
250 }
251
252 /* Only support VAL and VAL.. for now. */
253 if (log_level_min != log_level_max &&
254 log_level_max != LTTNG_LOGLEVEL_EMERG) {
255 goto error;
256 }
257
258 *log_level = (int) log_level_min;
259 *log_level_only = log_level_min == log_level_max;
260 break;
261 }
262 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
263 {
264 enum lttng_loglevel_log4j log_level_min, log_level_max;
265 if (!loglevel_log4j_parse_range_string(
266 str, &log_level_min, &log_level_max)) {
267 goto error;
268 }
269
270 /* Only support VAL and VAL.. for now. */
271 if (log_level_min != log_level_max &&
272 log_level_max != LTTNG_LOGLEVEL_LOG4J_FATAL) {
273 goto error;
274 }
275
276 *log_level = (int) log_level_min;
277 *log_level_only = log_level_min == log_level_max;
278 break;
279 }
280 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
281 {
282 enum lttng_loglevel_jul log_level_min, log_level_max;
283 if (!loglevel_jul_parse_range_string(
284 str, &log_level_min, &log_level_max)) {
285 goto error;
286 }
287
288 /* Only support VAL and VAL.. for now. */
289 if (log_level_min != log_level_max &&
290 log_level_max != LTTNG_LOGLEVEL_JUL_SEVERE) {
291 goto error;
292 }
293
294 *log_level = (int) log_level_min;
295 *log_level_only = log_level_min == log_level_max;
296 break;
297 }
298 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
299 {
300 enum lttng_loglevel_python log_level_min, log_level_max;
301 if (!loglevel_python_parse_range_string(
302 str, &log_level_min, &log_level_max)) {
303 goto error;
304 }
305
306 /* Only support VAL and VAL.. for now. */
307 if (log_level_min != log_level_max &&
308 log_level_max !=
309 LTTNG_LOGLEVEL_PYTHON_CRITICAL) {
310 goto error;
311 }
312
313 *log_level = (int) log_level_min;
314 *log_level_only = log_level_min == log_level_max;
315 break;
316 }
317 default:
318 /* Invalid domain type. */
319 abort();
320 }
321
322 ret = true;
323 goto end;
324
325 error:
326 ret = false;
327
328 end:
329 return ret;
330 }
331
332 static int parse_kernel_probe_opts(const char *source,
333 struct lttng_kernel_probe_location **location)
334 {
335 int ret = 0;
336 int match;
337 char s_hex[19];
338 char name[LTTNG_SYMBOL_NAME_LEN];
339 char *symbol_name = NULL;
340 uint64_t offset;
341
342 /* Check for symbol+offset. */
343 match = sscanf(source,
344 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
345 "[^'+']+%18s",
346 name, s_hex);
347 if (match == 2) {
348 if (*s_hex == '\0') {
349 ERR("Kernel probe symbol offset is missing.");
350 goto error;
351 }
352
353 symbol_name = strndup(name, LTTNG_SYMBOL_NAME_LEN);
354 if (!symbol_name) {
355 PERROR("Failed to copy kernel probe location symbol name.");
356 goto error;
357 }
358 offset = strtoull(s_hex, NULL, 0);
359
360 *location = lttng_kernel_probe_location_symbol_create(
361 symbol_name, offset);
362 if (!*location) {
363 ERR("Failed to create symbol kernel probe location.");
364 goto error;
365 }
366
367 goto end;
368 }
369
370 /* Check for symbol. */
371 if (isalpha(name[0]) || name[0] == '_') {
372 match = sscanf(source,
373 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
374 "s",
375 name);
376 if (match == 1) {
377 symbol_name = strndup(name, LTTNG_SYMBOL_NAME_LEN);
378 if (!symbol_name) {
379 ERR("Failed to copy kernel probe location symbol name.");
380 goto error;
381 }
382
383 *location = lttng_kernel_probe_location_symbol_create(
384 symbol_name, 0);
385 if (!*location) {
386 ERR("Failed to create symbol kernel probe location.");
387 goto error;
388 }
389
390 goto end;
391 }
392 }
393
394 /* Check for address. */
395 match = sscanf(source, "%18s", s_hex);
396 if (match > 0) {
397 uint64_t address;
398
399 if (*s_hex == '\0') {
400 ERR("Invalid kernel probe location address.");
401 goto error;
402 }
403
404 address = strtoull(s_hex, NULL, 0);
405 *location = lttng_kernel_probe_location_address_create(address);
406 if (!*location) {
407 ERR("Failed to create symbol kernel probe location.");
408 goto error;
409 }
410
411 goto end;
412 }
413
414 error:
415 /* No match */
416 ret = -1;
417 *location = NULL;
418
419 end:
420 free(symbol_name);
421 return ret;
422 }
423
424 static
425 struct lttng_event_expr *ir_op_load_expr_to_event_expr(
426 const struct ir_load_expression *load_expr,
427 const char *capture_str)
428 {
429 char *provider_name = NULL;
430 struct lttng_event_expr *event_expr = NULL;
431 const struct ir_load_expression_op *load_expr_op = load_expr->child;
432 const enum ir_load_expression_type load_expr_child_type =
433 load_expr_op->type;
434
435 switch (load_expr_child_type) {
436 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT:
437 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
438 {
439 const char *field_name;
440
441 load_expr_op = load_expr_op->next;
442 LTTNG_ASSERT(load_expr_op);
443 LTTNG_ASSERT(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
444 field_name = load_expr_op->u.symbol;
445 LTTNG_ASSERT(field_name);
446
447 event_expr = load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ?
448 lttng_event_expr_event_payload_field_create(field_name) :
449 lttng_event_expr_channel_context_field_create(field_name);
450 if (!event_expr) {
451 ERR("Failed to create %s event expression: field name = `%s`.",
452 load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ?
453 "payload field" : "channel context",
454 field_name);
455 goto error;
456 }
457
458 break;
459 }
460 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
461 {
462 const char *colon;
463 const char *type_name;
464 const char *field_name;
465
466 load_expr_op = load_expr_op->next;
467 LTTNG_ASSERT(load_expr_op);
468 LTTNG_ASSERT(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
469 field_name = load_expr_op->u.symbol;
470 LTTNG_ASSERT(field_name);
471
472 /*
473 * The field name needs to be of the form PROVIDER:TYPE. We
474 * split it here.
475 */
476 colon = strchr(field_name, ':');
477 if (!colon) {
478 ERR("Invalid app-specific context field name: missing colon in `%s`.",
479 field_name);
480 goto error;
481 }
482
483 type_name = colon + 1;
484 if (*type_name == '\0') {
485 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
486 field_name);
487 goto error;
488 }
489
490 provider_name = strndup(field_name, colon - field_name);
491 if (!provider_name) {
492 PERROR("Failed to allocate field name string");
493 goto error;
494 }
495
496 event_expr = lttng_event_expr_app_specific_context_field_create(
497 provider_name, type_name);
498 if (!event_expr) {
499 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
500 provider_name, type_name);
501 goto error;
502 }
503
504 break;
505 }
506 default:
507 ERR("%s: unexpected load expr type %d.", __func__,
508 load_expr_op->type);
509 abort();
510 }
511
512 load_expr_op = load_expr_op->next;
513
514 /* There may be a single array index after that. */
515 if (load_expr_op->type == IR_LOAD_EXPRESSION_GET_INDEX) {
516 struct lttng_event_expr *index_event_expr;
517 const uint64_t index = load_expr_op->u.index;
518
519 index_event_expr = lttng_event_expr_array_field_element_create(event_expr, index);
520 if (!index_event_expr) {
521 ERR("Failed to create array field element event expression.");
522 goto error;
523 }
524
525 event_expr = index_event_expr;
526 load_expr_op = load_expr_op->next;
527 }
528
529 switch (load_expr_op->type) {
530 case IR_LOAD_EXPRESSION_LOAD_FIELD:
531 /*
532 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
533 * always found at the end of the chain.
534 */
535 break;
536 case IR_LOAD_EXPRESSION_GET_SYMBOL:
537 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
538 capture_str);
539 goto error;
540
541 default:
542 ERR("%s: unexpected load expression operator %s.", __func__,
543 ir_load_expression_type_str(load_expr_op->type));
544 abort();
545 }
546
547 goto end;
548
549 error:
550 lttng_event_expr_destroy(event_expr);
551 event_expr = NULL;
552
553 end:
554 free(provider_name);
555
556 return event_expr;
557 }
558
559 static
560 struct lttng_event_expr *ir_op_load_to_event_expr(
561 const struct ir_op *ir, const char *capture_str)
562 {
563 struct lttng_event_expr *event_expr = NULL;
564
565 LTTNG_ASSERT(ir->op == IR_OP_LOAD);
566
567 switch (ir->data_type) {
568 case IR_DATA_EXPRESSION:
569 {
570 const struct ir_load_expression *ir_load_expr =
571 ir->u.load.u.expression;
572
573 event_expr = ir_op_load_expr_to_event_expr(
574 ir_load_expr, capture_str);
575 break;
576 }
577 default:
578 ERR("%s: unexpected data type: %s.", __func__,
579 ir_data_type_str(ir->data_type));
580 abort();
581 }
582
583 return event_expr;
584 }
585
586 static
587 const char *ir_operator_type_human_str(enum ir_op_type op)
588 {
589 const char *name;
590
591 switch (op) {
592 case IR_OP_BINARY:
593 name = "Binary";
594 break;
595 case IR_OP_UNARY:
596 name = "Unary";
597 break;
598 case IR_OP_LOGICAL:
599 name = "Logical";
600 break;
601 default:
602 abort();
603 }
604
605 return name;
606 }
607
608 static
609 struct lttng_event_expr *ir_op_root_to_event_expr(const struct ir_op *ir,
610 const char *capture_str)
611 {
612 struct lttng_event_expr *event_expr = NULL;
613
614 LTTNG_ASSERT(ir->op == IR_OP_ROOT);
615 ir = ir->u.root.child;
616
617 switch (ir->op) {
618 case IR_OP_LOAD:
619 event_expr = ir_op_load_to_event_expr(ir, capture_str);
620 break;
621 case IR_OP_BINARY:
622 case IR_OP_UNARY:
623 case IR_OP_LOGICAL:
624 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
625 capture_str,
626 ir_operator_type_human_str(ir->op));
627 break;
628 default:
629 ERR("%s: unexpected IR op type: %s.", __func__,
630 ir_op_type_str(ir->op));
631 abort();
632 }
633
634 return event_expr;
635 }
636
637 static
638 void destroy_event_expr(void *ptr)
639 {
640 lttng_event_expr_destroy((lttng_event_expr *) ptr);
641 }
642
643 namespace {
644 struct parse_event_rule_res {
645 /* Owned by this. */
646 struct lttng_event_rule *er;
647
648 /* Array of `struct lttng_event_expr *` */
649 struct lttng_dynamic_pointer_array capture_descriptors;
650 };
651 } /* namespace */
652
653 static
654 struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv,
655 int argc_offset)
656 {
657 enum lttng_event_rule_type event_rule_type =
658 LTTNG_EVENT_RULE_TYPE_UNKNOWN;
659 struct argpar_iter *argpar_iter = NULL;
660 const struct argpar_item *argpar_item = NULL;
661 int consumed_args = -1;
662 struct lttng_kernel_probe_location *kernel_probe_location = NULL;
663 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
664 struct parse_event_rule_res res = {};
665 struct lttng_event_expr *event_expr = NULL;
666 struct filter_parser_ctx *parser_ctx = NULL;
667 struct lttng_log_level_rule *log_level_rule = NULL;
668
669 /* Event rule type option */
670 char *event_rule_type_str = NULL;
671
672 /* Tracepoint and syscall options. */
673 char *name = NULL;
674 /* Array of strings. */
675 struct lttng_dynamic_pointer_array exclude_names;
676
677 /* For userspace / kernel probe and function. */
678 char *location = NULL;
679 char *event_name = NULL;
680
681 /* Filter. */
682 char *filter = NULL;
683
684 /* Log level. */
685 char *log_level_str = NULL;
686
687 lttng_dynamic_pointer_array_init(&res.capture_descriptors,
688 destroy_event_expr);
689
690 lttng_dynamic_pointer_array_init(&exclude_names, free);
691
692 argpar_iter = argpar_iter_create(*argc, *argv, event_rule_opt_descrs);
693 if (!argpar_iter) {
694 ERR("Failed to allocate an argpar iter.");
695 goto error;
696 }
697
698 while (true) {
699 enum parse_next_item_status status;
700
701 status = parse_next_item(argpar_iter, &argpar_item,
702 argc_offset, *argv, false, NULL, NULL);
703 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
704 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
705 goto error;
706 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
707 break;
708 }
709
710 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
711
712 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
713 const struct argpar_opt_descr *descr =
714 argpar_item_opt_descr(argpar_item);
715 const char *arg = argpar_item_opt_arg(argpar_item);
716
717 switch (descr->id) {
718 case OPT_TYPE:
719 if (!assign_event_rule_type(&event_rule_type, arg)) {
720 goto error;
721 }
722
723 /* Save the string for later use. */
724 if (!assign_string(&event_rule_type_str, arg,
725 "--type/-t")) {
726 goto error;
727 }
728
729 break;
730 case OPT_LOCATION:
731 if (!assign_string(&location, arg,
732 "--location/-L")) {
733 goto error;
734 }
735
736 break;
737 case OPT_EVENT_NAME:
738 if (!assign_string(&event_name, arg,
739 "--event-name/-E")) {
740 goto error;
741 }
742
743 break;
744 case OPT_FILTER:
745 if (!assign_string(&filter, arg,
746 "--filter/-f")) {
747 goto error;
748 }
749
750 break;
751 case OPT_NAME:
752 if (!assign_string(&name, arg,
753 "--name/-n")) {
754 goto error;
755 }
756
757 break;
758 case OPT_EXCLUDE_NAME:
759 {
760 int ret;
761
762 ret = lttng_dynamic_pointer_array_add_pointer(
763 &exclude_names,
764 strdup(arg));
765 if (ret != 0) {
766 ERR("Failed to add pointer to dynamic pointer array.");
767 goto error;
768 }
769
770 break;
771 }
772 case OPT_LOG_LEVEL:
773 if (!assign_string(&log_level_str, arg,
774 "--log-level/-l")) {
775 goto error;
776 }
777
778 break;
779 case OPT_CAPTURE:
780 {
781 int ret;
782
783 ret = filter_parser_ctx_create_from_filter_expression(
784 arg, &parser_ctx);
785 if (ret) {
786 ERR("Failed to parse capture expression `%s`.", arg);
787 goto error;
788 }
789
790 event_expr = ir_op_root_to_event_expr(
791 parser_ctx->ir_root, arg);
792 filter_parser_ctx_free(parser_ctx);
793 parser_ctx = nullptr;
794 if (!event_expr) {
795 /*
796 * ir_op_root_to_event_expr has printed
797 * an error message.
798 */
799 goto error;
800 }
801
802 ret = lttng_dynamic_pointer_array_add_pointer(
803 &res.capture_descriptors,
804 event_expr);
805 if (ret) {
806 goto error;
807 }
808
809 /*
810 * The ownership of event expression was
811 * transferred to the dynamic array.
812 */
813 event_expr = NULL;
814
815 break;
816 }
817 default:
818 abort();
819 }
820 } else {
821 const char *arg = argpar_item_non_opt_arg(argpar_item);
822
823 /* Don't accept non-option arguments. */
824 ERR("Unexpected argument '%s'", arg);
825 goto error;
826 }
827 }
828
829 if (event_rule_type == LTTNG_EVENT_RULE_TYPE_UNKNOWN) {
830 ERR("Event rule requires a --type.");
831 goto error;
832 }
833
834 /*
835 * Option --name is applicable to event rules of type kernel, user, jul,
836 * log4j,python and syscall. If --name is omitted, it is implicitly
837 * "*".
838 */
839 switch (event_rule_type) {
840 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
841 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
842 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
843 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
844 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
845 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
846 if (!name) {
847 name = strdup("*");
848 }
849 break;
850
851 default:
852 if (name) {
853 ERR("Can't use --name with %s event rules.",
854 lttng_event_rule_type_str(
855 event_rule_type));
856 goto error;
857 }
858
859 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
860 ERR("Can't use --exclude-name/-x with %s event rules.",
861 lttng_event_rule_type_str(
862 event_rule_type));
863 goto error;
864 }
865 }
866
867 /*
868 * Option --location is only applicable to (and mandatory for) event
869 * rules of type {k,u}probe and function.
870 *
871 * Option --event-name is only applicable to event rules of type probe.
872 * If omitted, it defaults to the location.
873 */
874 switch (event_rule_type) {
875 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
876 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
877 if (!location) {
878 ERR("Event rule of type %s requires a --location.",
879 lttng_event_rule_type_str(event_rule_type));
880 goto error;
881 }
882
883 if (!event_name) {
884 event_name = strdup(location);
885 }
886
887 break;
888
889 default:
890 if (location) {
891 ERR("Can't use --location with %s event rules.",
892 lttng_event_rule_type_str(event_rule_type));
893 goto error;
894 }
895
896 if (event_name) {
897 ERR("Can't use --event-name with %s event rules.",
898 lttng_event_rule_type_str(
899 event_rule_type));
900 goto error;
901 }
902 }
903
904 /*
905 * Update *argc and *argv so our caller can keep parsing what follows.
906 */
907 consumed_args = argpar_iter_ingested_orig_args(argpar_iter);
908 LTTNG_ASSERT(consumed_args >= 0);
909 *argc -= consumed_args;
910 *argv += consumed_args;
911
912 /*
913 * Adding a filter to a probe, function or userspace-probe would be
914 * denied by the kernel tracer as it's not supported at the moment. We
915 * do an early check here to warn the user.
916 */
917 if (filter) {
918 switch (event_rule_type) {
919 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
920 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
921 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
922 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
923 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
924 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
925 break;
926 default:
927 ERR("Filter expressions are not supported for %s event rules.",
928 lttng_event_rule_type_str(event_rule_type));
929 goto error;
930 }
931 }
932
933 /*
934 * If --exclude-name/-x was passed, split it into an exclusion list.
935 * Exclusions are only supported by
936 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
937 */
938 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
939 if (event_rule_type != LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT) {
940 ERR("Event name exclusions are not yet implemented for %s event rules.",
941 lttng_event_rule_type_str(event_rule_type));
942 goto error;
943 }
944
945 if (validate_exclusion_list(name, &exclude_names) != 0) {
946 /*
947 * Assume validate_exclusion_list already prints an
948 * error message.
949 */
950 goto error;
951 }
952 }
953
954 if (log_level_str) {
955 switch (event_rule_type) {
956 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
957 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
958 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
959 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
960 {
961 int log_level;
962 bool log_level_only;
963
964 if (strcmp(log_level_str, "..") == 0) {
965 /*
966 * ".." is the same as passing no log level
967 * option and correspond to the "ANY" case.
968 */
969 break;
970 }
971
972 if (!parse_log_level_string(log_level_str, event_rule_type,
973 &log_level, &log_level_only)) {
974 ERR("Failed to parse log level string `%s`.",
975 log_level_str);
976 goto error;
977 }
978
979 if (log_level_only) {
980 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
981 } else {
982 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level);
983 }
984
985 if (log_level_rule == NULL) {
986 ERR("Failed to create log level rule object.");
987 goto error;
988 }
989 break;
990 }
991 default:
992 ERR("Log levels are not supported for %s event rules.",
993 lttng_event_rule_type_str(event_rule_type));
994 goto error;
995 }
996 }
997
998 /* Finally, create the event rule object. */
999 switch (event_rule_type) {
1000 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
1001 {
1002 enum lttng_event_rule_status event_rule_status;
1003
1004 res.er = lttng_event_rule_user_tracepoint_create();
1005 if (!res.er) {
1006 ERR("Failed to create user_tracepoint event rule.");
1007 goto error;
1008 }
1009
1010 /* Set pattern. */
1011 event_rule_status = lttng_event_rule_user_tracepoint_set_name_pattern(
1012 res.er, name);
1013 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1014 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1015 name);
1016 goto error;
1017 }
1018
1019 /* Set filter. */
1020 if (filter) {
1021 event_rule_status = lttng_event_rule_user_tracepoint_set_filter(
1022 res.er, filter);
1023 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1024 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1025 filter);
1026 goto error;
1027 }
1028 }
1029
1030 /* Set exclusion list. */
1031 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
1032 int n;
1033 int count = lttng_dynamic_pointer_array_get_count(
1034 &exclude_names);
1035
1036 for (n = 0; n < count; n++) {
1037 const char *exclude_name =
1038 (const char *) lttng_dynamic_pointer_array_get_pointer(
1039 &exclude_names,
1040 n);
1041
1042 event_rule_status =
1043 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1044 res.er,
1045 exclude_name);
1046 if (event_rule_status !=
1047 LTTNG_EVENT_RULE_STATUS_OK) {
1048 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1049 exclude_name);
1050 goto error;
1051 }
1052 }
1053 }
1054
1055 if (log_level_rule) {
1056 event_rule_status =
1057 lttng_event_rule_user_tracepoint_set_log_level_rule(
1058 res.er, log_level_rule);
1059
1060 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1061 ERR("Failed to set log level on event fule.");
1062 goto error;
1063 }
1064 }
1065
1066 break;
1067 }
1068 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
1069 {
1070 enum lttng_event_rule_status event_rule_status;
1071
1072 res.er = lttng_event_rule_kernel_tracepoint_create();
1073 if (!res.er) {
1074 ERR("Failed to create kernel_tracepoint event rule.");
1075 goto error;
1076 }
1077
1078 /* Set pattern. */
1079 event_rule_status = lttng_event_rule_kernel_tracepoint_set_name_pattern(
1080 res.er, name);
1081 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1082 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1083 name);
1084 goto error;
1085 }
1086
1087 /* Set filter. */
1088 if (filter) {
1089 event_rule_status = lttng_event_rule_kernel_tracepoint_set_filter(
1090 res.er, filter);
1091 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1092 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1093 filter);
1094 goto error;
1095 }
1096 }
1097 break;
1098 }
1099 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1100 {
1101 enum lttng_event_rule_status event_rule_status;
1102
1103 res.er = lttng_event_rule_jul_logging_create();
1104 if (!res.er) {
1105 ERR("Failed to create jul_logging event rule.");
1106 goto error;
1107 }
1108
1109 /* Set pattern. */
1110 event_rule_status = lttng_event_rule_jul_logging_set_name_pattern(
1111 res.er, name);
1112 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1113 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1114 name);
1115 goto error;
1116 }
1117
1118 /* Set filter. */
1119 if (filter) {
1120 event_rule_status = lttng_event_rule_jul_logging_set_filter(
1121 res.er, filter);
1122 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1123 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1124 filter);
1125 goto error;
1126 }
1127 }
1128
1129 if (log_level_rule) {
1130 event_rule_status =
1131 lttng_event_rule_jul_logging_set_log_level_rule(
1132 res.er, log_level_rule);
1133
1134 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1135 ERR("Failed to set log level on event fule.");
1136 goto error;
1137 }
1138 }
1139 break;
1140 }
1141 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1142 {
1143 enum lttng_event_rule_status event_rule_status;
1144
1145 res.er = lttng_event_rule_log4j_logging_create();
1146 if (!res.er) {
1147 ERR("Failed to create jul_logging event rule.");
1148 goto error;
1149 }
1150
1151 /* Set pattern. */
1152 event_rule_status = lttng_event_rule_log4j_logging_set_name_pattern(
1153 res.er, name);
1154 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1155 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1156 name);
1157 goto error;
1158 }
1159
1160 /* Set filter. */
1161 if (filter) {
1162 event_rule_status = lttng_event_rule_log4j_logging_set_filter(
1163 res.er, filter);
1164 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1165 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1166 filter);
1167 goto error;
1168 }
1169 }
1170
1171 if (log_level_rule) {
1172 event_rule_status =
1173 lttng_event_rule_log4j_logging_set_log_level_rule(
1174 res.er, log_level_rule);
1175
1176 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1177 ERR("Failed to set log level on event fule.");
1178 goto error;
1179 }
1180 }
1181 break;
1182 }
1183 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1184 {
1185 enum lttng_event_rule_status event_rule_status;
1186
1187 res.er = lttng_event_rule_python_logging_create();
1188 if (!res.er) {
1189 ERR("Failed to create jul_logging event rule.");
1190 goto error;
1191 }
1192
1193 /* Set pattern. */
1194 event_rule_status = lttng_event_rule_python_logging_set_name_pattern(
1195 res.er, name);
1196 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1197 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1198 name);
1199 goto error;
1200 }
1201
1202 /* Set filter. */
1203 if (filter) {
1204 event_rule_status = lttng_event_rule_python_logging_set_filter(
1205 res.er, filter);
1206 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1207 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1208 filter);
1209 goto error;
1210 }
1211 }
1212
1213 if (log_level_rule) {
1214 event_rule_status =
1215 lttng_event_rule_python_logging_set_log_level_rule(
1216 res.er, log_level_rule);
1217
1218 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1219 ERR("Failed to set log level on event fule.");
1220 goto error;
1221 }
1222 }
1223 break;
1224 }
1225 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
1226 {
1227 int ret;
1228 enum lttng_event_rule_status event_rule_status;
1229
1230 ret = parse_kernel_probe_opts(
1231 location, &kernel_probe_location);
1232 if (ret) {
1233 ERR("Failed to parse kernel probe location.");
1234 goto error;
1235 }
1236
1237 LTTNG_ASSERT(kernel_probe_location);
1238 res.er = lttng_event_rule_kernel_kprobe_create(kernel_probe_location);
1239 if (!res.er) {
1240 ERR("Failed to create kprobe event rule.");
1241 goto error;
1242 }
1243
1244 event_rule_status =
1245 lttng_event_rule_kernel_kprobe_set_event_name(
1246 res.er, event_name);
1247 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1248 ERR("Failed to set kprobe event rule's name to '%s'.",
1249 event_name);
1250 goto error;
1251 }
1252
1253 break;
1254 }
1255 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
1256 {
1257 int ret;
1258 enum lttng_event_rule_status event_rule_status;
1259
1260 ret = parse_userspace_probe_opts(
1261 location, &userspace_probe_location);
1262 if (ret) {
1263 ERR("Failed to parse user space probe location.");
1264 goto error;
1265 }
1266
1267 res.er = lttng_event_rule_kernel_uprobe_create(userspace_probe_location);
1268 if (!res.er) {
1269 ERR("Failed to create userspace probe event rule.");
1270 goto error;
1271 }
1272
1273 event_rule_status =
1274 lttng_event_rule_kernel_uprobe_set_event_name(
1275 res.er, event_name);
1276 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1277 ERR("Failed to set user space probe event rule's name to '%s'.",
1278 event_name);
1279 goto error;
1280 }
1281
1282 break;
1283 }
1284 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
1285 {
1286 enum lttng_event_rule_status event_rule_status;
1287 enum lttng_event_rule_kernel_syscall_emission_site emission_site;
1288
1289 if (!parse_syscall_emission_site_from_type(
1290 event_rule_type_str, &emission_site)) {
1291 ERR("Failed to parse syscall type '%s'.", event_rule_type_str);
1292 goto error;
1293 }
1294
1295 res.er = lttng_event_rule_kernel_syscall_create(emission_site);
1296 if (!res.er) {
1297 ERR("Failed to create syscall event rule.");
1298 goto error;
1299 }
1300
1301 event_rule_status = lttng_event_rule_kernel_syscall_set_name_pattern(
1302 res.er, name);
1303 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1304 ERR("Failed to set syscall event rule's pattern to '%s'.",
1305 name);
1306 goto error;
1307 }
1308
1309 if (filter) {
1310 event_rule_status = lttng_event_rule_kernel_syscall_set_filter(
1311 res.er, filter);
1312 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1313 ERR("Failed to set syscall event rule's filter to '%s'.",
1314 filter);
1315 goto error;
1316 }
1317 }
1318
1319 break;
1320 }
1321 default:
1322 abort();
1323 goto error;
1324 }
1325
1326 goto end;
1327
1328 error:
1329 lttng_event_rule_destroy(res.er);
1330 res.er = NULL;
1331 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1332
1333 end:
1334 if (parser_ctx) {
1335 filter_parser_ctx_free(parser_ctx);
1336 }
1337
1338 lttng_event_expr_destroy(event_expr);
1339 argpar_item_destroy(argpar_item);
1340 argpar_iter_destroy(argpar_iter);
1341 free(filter);
1342 free(name);
1343 lttng_dynamic_pointer_array_reset(&exclude_names);
1344 free(log_level_str);
1345 free(location);
1346 free(event_name);
1347 free(event_rule_type_str);
1348
1349 lttng_kernel_probe_location_destroy(kernel_probe_location);
1350 lttng_userspace_probe_location_destroy(userspace_probe_location);
1351 lttng_log_level_rule_destroy(log_level_rule);
1352 return res;
1353 }
1354
1355 static
1356 struct lttng_condition *handle_condition_event(int *argc, const char ***argv,
1357 int argc_offset)
1358 {
1359 struct parse_event_rule_res res;
1360 struct lttng_condition *c;
1361 size_t i;
1362
1363 res = parse_event_rule(argc, argv, argc_offset);
1364 if (!res.er) {
1365 c = NULL;
1366 goto error;
1367 }
1368
1369 c = lttng_condition_event_rule_matches_create(res.er);
1370 lttng_event_rule_destroy(res.er);
1371 res.er = NULL;
1372 if (!c) {
1373 goto error;
1374 }
1375
1376 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors);
1377 i++) {
1378 enum lttng_condition_status status;
1379 struct lttng_event_expr **expr =
1380 (lttng_event_expr **) lttng_dynamic_array_get_element(
1381 &res.capture_descriptors.array, i);
1382
1383 LTTNG_ASSERT(expr);
1384 LTTNG_ASSERT(*expr);
1385 status = lttng_condition_event_rule_matches_append_capture_descriptor(
1386 c, *expr);
1387 if (status != LTTNG_CONDITION_STATUS_OK) {
1388 if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) {
1389 ERR("The capture feature is unsupported by the event-rule condition type");
1390 }
1391
1392 goto error;
1393 }
1394
1395 /* Ownership of event expression moved to `c` */
1396 *expr = NULL;
1397 }
1398
1399 goto end;
1400
1401 error:
1402 lttng_condition_destroy(c);
1403 c = NULL;
1404
1405 end:
1406 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1407 lttng_event_rule_destroy(res.er);
1408 return c;
1409 }
1410
1411 namespace {
1412 struct condition_descr {
1413 const char *name;
1414 struct lttng_condition *(*handler) (int *argc, const char ***argv,
1415 int argc_offset);
1416 };
1417 } /* namespace */
1418
1419 static const
1420 struct condition_descr condition_descrs[] = {
1421 { "event-rule-matches", handle_condition_event },
1422 };
1423
1424 static
1425 void print_valid_condition_names(void)
1426 {
1427 unsigned int i;
1428
1429 ERR("Valid condition names are:");
1430
1431 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
1432 ERR(" %s", condition_descrs[i].name);
1433 }
1434 }
1435
1436 static
1437 struct lttng_condition *parse_condition(const char *condition_name, int *argc,
1438 const char ***argv, int argc_offset, int orig_arg_index,
1439 const char *orig_arg)
1440 {
1441 int i;
1442 struct lttng_condition *cond;
1443 const struct condition_descr *descr = NULL;
1444
1445 for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
1446 if (strcmp(condition_name, condition_descrs[i].name) == 0) {
1447 descr = &condition_descrs[i];
1448 break;
1449 }
1450 }
1451
1452 if (!descr) {
1453 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'",
1454 orig_arg_index + 1, orig_arg, condition_name);
1455 print_valid_condition_names();
1456 goto error;
1457 }
1458
1459 cond = descr->handler(argc, argv, argc_offset);
1460 if (!cond) {
1461 /* The handler has already printed an error message. */
1462 goto error;
1463 }
1464
1465 goto end;
1466 error:
1467 cond = NULL;
1468 end:
1469 return cond;
1470 }
1471
1472 static struct lttng_rate_policy *parse_rate_policy(const char *policy_str)
1473 {
1474 int ret;
1475 size_t num_token = 0;
1476 struct lttng_dynamic_pointer_array tokens;
1477 struct lttng_rate_policy *policy = NULL;
1478 enum lttng_rate_policy_type policy_type;
1479 unsigned long long value;
1480 char *policy_type_str;
1481 char *policy_value_str;
1482
1483 LTTNG_ASSERT(policy_str);
1484 lttng_dynamic_pointer_array_init(&tokens, NULL);
1485
1486 /* Rate policy fields are separated by ':'. */
1487 ret = strutils_split(policy_str, ':', 1, &tokens);
1488 if (ret == 0) {
1489 num_token = lttng_dynamic_pointer_array_get_count(&tokens);
1490 }
1491
1492 /*
1493 * Early sanity check that the number of parameter is exactly 2.
1494 * i.e : type:value
1495 */
1496 if (num_token != 2) {
1497 ERR("Rate policy format is invalid.");
1498 goto end;
1499 }
1500
1501 policy_type_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 0);
1502 policy_value_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 1);
1503
1504 /* Parse the type. */
1505 if (strcmp(policy_type_str, "once-after") == 0) {
1506 policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N;
1507 } else if (strcmp(policy_type_str, "every") == 0) {
1508 policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N;
1509 } else {
1510 ERR("Rate policy type `%s` unknown.", policy_type_str);
1511 goto end;
1512 }
1513
1514 /* Parse the value. */
1515 if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) {
1516 ERR("Failed to parse rate policy value `%s` as an integer.",
1517 policy_value_str);
1518 goto end;
1519 }
1520
1521 if (value == 0) {
1522 ERR("Rate policy value `%s` must be > 0.", policy_value_str);
1523 goto end;
1524 }
1525
1526 switch (policy_type) {
1527 case LTTNG_RATE_POLICY_TYPE_EVERY_N:
1528 policy = lttng_rate_policy_every_n_create(value);
1529 break;
1530 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N:
1531 policy = lttng_rate_policy_once_after_n_create(value);
1532 break;
1533 default:
1534 abort();
1535 }
1536
1537 if (policy == NULL) {
1538 ERR("Failed to create rate policy `%s`.", policy_str);
1539 }
1540
1541 end:
1542 lttng_dynamic_pointer_array_reset(&tokens);
1543 return policy;
1544 }
1545
1546 static const struct argpar_opt_descr notify_action_opt_descrs[] = {
1547 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1548 ARGPAR_OPT_DESCR_SENTINEL
1549 };
1550
1551 static
1552 struct lttng_action *handle_action_notify(int *argc, const char ***argv,
1553 int argc_offset)
1554 {
1555 struct lttng_action *action = NULL;
1556 struct argpar_iter *argpar_iter = NULL;
1557 const struct argpar_item *argpar_item = NULL;
1558 struct lttng_rate_policy *policy = NULL;
1559
1560 argpar_iter = argpar_iter_create(*argc, *argv, notify_action_opt_descrs);
1561 if (!argpar_iter) {
1562 ERR("Failed to allocate an argpar iter.");
1563 goto error;
1564 }
1565
1566 while (true) {
1567 enum parse_next_item_status status;
1568
1569 status = parse_next_item(argpar_iter, &argpar_item,
1570 argc_offset, *argv, false, NULL,
1571 "While parsing `notify` action:");
1572 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1573 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1574 goto error;
1575 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1576 break;
1577 }
1578
1579 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
1580
1581 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1582 const struct argpar_opt_descr *descr =
1583 argpar_item_opt_descr(argpar_item);
1584 const char *arg = argpar_item_opt_arg(argpar_item);
1585
1586 switch (descr->id) {
1587 case OPT_RATE_POLICY:
1588 {
1589 policy = parse_rate_policy(arg);
1590 if (!policy) {
1591 goto error;
1592 }
1593 break;
1594 }
1595 default:
1596 abort();
1597 }
1598 } else {
1599 const char *arg = argpar_item_non_opt_arg(argpar_item);
1600
1601 ERR("Unexpected argument `%s`.", arg);
1602 goto error;
1603 }
1604 }
1605
1606 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1607 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1608
1609 action = lttng_action_notify_create();
1610 if (!action) {
1611 ERR("Failed to create notify action");
1612 goto error;
1613 }
1614
1615 if (policy) {
1616 enum lttng_action_status status;
1617 status = lttng_action_notify_set_rate_policy(action, policy);
1618 if (status != LTTNG_ACTION_STATUS_OK) {
1619 ERR("Failed to set rate policy");
1620 goto error;
1621 }
1622 }
1623
1624 goto end;
1625
1626 error:
1627 lttng_action_destroy(action);
1628 action = NULL;
1629 end:
1630 lttng_rate_policy_destroy(policy);
1631 argpar_item_destroy(argpar_item);
1632 argpar_iter_destroy(argpar_iter);
1633 return action;
1634 }
1635
1636 /*
1637 * Generic handler for a kind of action that takes a session name and an
1638 * optional rate policy.
1639 */
1640
1641 static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
1642 const char ***argv,
1643 int argc_offset,
1644 struct lttng_action *(*create_action_cb)(void),
1645 enum lttng_action_status (*set_session_name_cb)(
1646 struct lttng_action *, const char *),
1647 enum lttng_action_status (*set_rate_policy_cb)(
1648 struct lttng_action *,
1649 const struct lttng_rate_policy *),
1650 const char *action_name)
1651 {
1652 struct lttng_action *action = NULL;
1653 struct argpar_iter *argpar_iter = NULL;
1654 const struct argpar_item *argpar_item = NULL;
1655 const char *session_name_arg = NULL;
1656 enum lttng_action_status action_status;
1657 struct lttng_rate_policy *policy = NULL;
1658
1659 LTTNG_ASSERT(set_session_name_cb);
1660 LTTNG_ASSERT(set_rate_policy_cb);
1661
1662 const struct argpar_opt_descr rate_policy_opt_descrs[] = {
1663 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1664 ARGPAR_OPT_DESCR_SENTINEL
1665 };
1666
1667 argpar_iter = argpar_iter_create(*argc, *argv, rate_policy_opt_descrs);
1668 if (!argpar_iter) {
1669 ERR("Failed to allocate an argpar iter.");
1670 goto error;
1671 }
1672
1673 while (true) {
1674 enum parse_next_item_status status;
1675
1676 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
1677 *argv, false, NULL,
1678 "While parsing `%s` action:", action_name);
1679 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1680 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1681 goto error;
1682 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1683 break;
1684 }
1685
1686 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
1687
1688 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1689 const struct argpar_opt_descr *descr =
1690 argpar_item_opt_descr(argpar_item);
1691 const char *arg = argpar_item_opt_arg(argpar_item);
1692
1693 switch (descr->id) {
1694 case OPT_RATE_POLICY:
1695 {
1696 policy = parse_rate_policy(arg);
1697 if (!policy) {
1698 goto error;
1699 }
1700 break;
1701 }
1702 default:
1703 abort();
1704 }
1705 } else {
1706 const char *arg = argpar_item_non_opt_arg(argpar_item);
1707 unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
1708
1709 switch (idx) {
1710 case 0:
1711 session_name_arg = arg;
1712 break;
1713 default:
1714 ERR("Unexpected argument `%s`.", arg);
1715 goto error;
1716 }
1717 }
1718 }
1719
1720 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1721 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1722
1723 if (!session_name_arg) {
1724 ERR("Missing session name.");
1725 goto error;
1726 }
1727
1728 action = create_action_cb();
1729 if (!action) {
1730 ERR("Failed to allocate %s session action.", action_name);
1731 goto error;
1732 }
1733
1734 action_status = set_session_name_cb(action, session_name_arg);
1735 if (action_status != LTTNG_ACTION_STATUS_OK) {
1736 ERR("Failed to set action %s session's session name to '%s'.",
1737 action_name, session_name_arg);
1738 goto error;
1739 }
1740
1741 if (policy) {
1742 action_status = set_rate_policy_cb(action, policy);
1743 if (action_status != LTTNG_ACTION_STATUS_OK) {
1744 ERR("Failed to set rate policy");
1745 goto error;
1746 }
1747 }
1748
1749 goto end;
1750
1751 error:
1752 lttng_action_destroy(action);
1753 action = NULL;
1754
1755 end:
1756 lttng_rate_policy_destroy(policy);
1757 argpar_item_destroy(argpar_item);
1758 argpar_iter_destroy(argpar_iter);
1759 return action;
1760 }
1761
1762 static
1763 struct lttng_action *handle_action_start_session(int *argc,
1764 const char ***argv, int argc_offset)
1765 {
1766 return handle_action_simple_session_with_policy(argc, argv,
1767 argc_offset,
1768 lttng_action_start_session_create,
1769 lttng_action_start_session_set_session_name,
1770 lttng_action_start_session_set_rate_policy, "start");
1771 }
1772
1773 static
1774 struct lttng_action *handle_action_stop_session(int *argc,
1775 const char ***argv, int argc_offset)
1776 {
1777 return handle_action_simple_session_with_policy(argc, argv,
1778 argc_offset,
1779 lttng_action_stop_session_create,
1780 lttng_action_stop_session_set_session_name,
1781 lttng_action_stop_session_set_rate_policy, "stop");
1782 }
1783
1784 static
1785 struct lttng_action *handle_action_rotate_session(int *argc,
1786 const char ***argv, int argc_offset)
1787 {
1788 return handle_action_simple_session_with_policy(argc, argv,
1789 argc_offset,
1790 lttng_action_rotate_session_create,
1791 lttng_action_rotate_session_set_session_name,
1792 lttng_action_rotate_session_set_rate_policy,
1793 "rotate");
1794 }
1795
1796 static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
1797 { OPT_NAME, 'n', "name", true },
1798 { OPT_MAX_SIZE, 'm', "max-size", true },
1799 { OPT_CTRL_URL, '\0', "ctrl-url", true },
1800 { OPT_DATA_URL, '\0', "data-url", true },
1801 { OPT_URL, '\0', "url", true },
1802 { OPT_PATH, '\0', "path", true },
1803 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1804 ARGPAR_OPT_DESCR_SENTINEL
1805 };
1806
1807 static
1808 struct lttng_action *handle_action_snapshot_session(int *argc,
1809 const char ***argv, int argc_offset)
1810 {
1811 struct lttng_action *action = NULL;
1812 struct argpar_iter *argpar_iter = NULL;
1813 const struct argpar_item *argpar_item = NULL;
1814 const char *session_name_arg = NULL;
1815 char *snapshot_name_arg = NULL;
1816 char *ctrl_url_arg = NULL;
1817 char *data_url_arg = NULL;
1818 char *max_size_arg = NULL;
1819 char *url_arg = NULL;
1820 char *path_arg = NULL;
1821 char *error = NULL;
1822 enum lttng_action_status action_status;
1823 struct lttng_snapshot_output *snapshot_output = NULL;
1824 struct lttng_rate_policy *policy = NULL;
1825 int ret;
1826 unsigned int locations_specified = 0;
1827
1828 argpar_iter = argpar_iter_create(*argc, *argv, snapshot_action_opt_descrs);
1829 if (!argpar_iter) {
1830 ERR("Failed to allocate an argpar iter.");
1831 goto error;
1832 }
1833
1834 while (true) {
1835 enum parse_next_item_status status;
1836
1837 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
1838 *argv, false, NULL, "While parsing `snapshot` action:");
1839 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1840 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1841 goto error;
1842 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1843 break;
1844 }
1845
1846 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
1847
1848 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1849 const struct argpar_opt_descr *descr =
1850 argpar_item_opt_descr(argpar_item);
1851 const char *arg = argpar_item_opt_arg(argpar_item);
1852
1853 switch (descr->id) {
1854 case OPT_NAME:
1855 if (!assign_string(&snapshot_name_arg, arg, "--name/-n")) {
1856 goto error;
1857 }
1858
1859 break;
1860 case OPT_MAX_SIZE:
1861 if (!assign_string(&max_size_arg, arg, "--max-size/-m")) {
1862 goto error;
1863 }
1864
1865 break;
1866 case OPT_CTRL_URL:
1867 if (!assign_string(&ctrl_url_arg, arg, "--ctrl-url")) {
1868 goto error;
1869 }
1870
1871 break;
1872 case OPT_DATA_URL:
1873 if (!assign_string(&data_url_arg, arg, "--data-url")) {
1874 goto error;
1875 }
1876
1877 break;
1878 case OPT_URL:
1879 if (!assign_string(&url_arg, arg, "--url")) {
1880 goto error;
1881 }
1882
1883 break;
1884 case OPT_PATH:
1885 if (!assign_string(&path_arg, arg, "--path")) {
1886 goto error;
1887 }
1888
1889 break;
1890 case OPT_RATE_POLICY:
1891 {
1892 policy = parse_rate_policy(arg);
1893 if (!policy) {
1894 goto error;
1895 }
1896 break;
1897 }
1898 default:
1899 abort();
1900 }
1901 } else {
1902 const char *arg = argpar_item_non_opt_arg(argpar_item);
1903 const unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
1904
1905 switch (idx) {
1906 case 0:
1907 session_name_arg = arg;
1908 break;
1909 default:
1910 ERR("Unexpected argument `%s`.", arg);
1911 goto error;
1912 }
1913 }
1914 }
1915
1916 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1917 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1918
1919 if (!session_name_arg) {
1920 ERR("Missing session name.");
1921 goto error;
1922 }
1923
1924 /* --ctrl-url and --data-url must come in pair. */
1925 if (ctrl_url_arg && !data_url_arg) {
1926 ERR("--ctrl-url is specified, but --data-url is missing.");
1927 goto error;
1928 }
1929
1930 if (!ctrl_url_arg && data_url_arg) {
1931 ERR("--data-url is specified, but --ctrl-url is missing.");
1932 goto error;
1933 }
1934
1935 locations_specified += !!(ctrl_url_arg || data_url_arg);
1936 locations_specified += !!url_arg;
1937 locations_specified += !!path_arg;
1938
1939 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1940 if (locations_specified > 1) {
1941 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1942 goto error;
1943 }
1944
1945 /*
1946 * Did the user specify an option that implies using a
1947 * custom/unregistered output?
1948 */
1949 if (url_arg || ctrl_url_arg || path_arg) {
1950 snapshot_output = lttng_snapshot_output_create();
1951 if (!snapshot_output) {
1952 ERR("Failed to allocate a snapshot output.");
1953 goto error;
1954 }
1955 }
1956
1957 action = lttng_action_snapshot_session_create();
1958 if (!action) {
1959 ERR("Failed to allocate snapshot session action.");
1960 goto error;
1961 }
1962
1963 action_status = lttng_action_snapshot_session_set_session_name(
1964 action, session_name_arg);
1965 if (action_status != LTTNG_ACTION_STATUS_OK) {
1966 ERR("Failed to set action snapshot session's session name to '%s'.",
1967 session_name_arg);
1968 goto error;
1969 }
1970
1971 if (snapshot_name_arg) {
1972 if (!snapshot_output) {
1973 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1974 goto error;
1975 }
1976
1977 ret = lttng_snapshot_output_set_name(
1978 snapshot_name_arg, snapshot_output);
1979 if (ret != 0) {
1980 ERR("Failed to set name of snapshot output.");
1981 goto error;
1982 }
1983 }
1984
1985 if (max_size_arg) {
1986 uint64_t max_size;
1987
1988 if (!snapshot_output) {
1989 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1990 goto error;
1991 }
1992
1993 ret = utils_parse_size_suffix(max_size_arg, &max_size);
1994 if (ret != 0) {
1995 ERR("Failed to parse `%s` as a size.", max_size_arg);
1996 goto error;
1997 }
1998
1999 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
2000 if (ret != 0) {
2001 ERR("Failed to set snapshot output's max size to %" PRIu64 " bytes.",
2002 max_size);
2003 goto error;
2004 }
2005 }
2006
2007 if (url_arg) {
2008 int num_uris;
2009 struct lttng_uri *uris;
2010
2011 if (!strstr(url_arg, "://")) {
2012 ERR("Failed to parse '%s' as an URL.", url_arg);
2013 goto error;
2014 }
2015
2016 num_uris = uri_parse_str_urls(url_arg, NULL, &uris);
2017 if (num_uris < 1) {
2018 ERR("Failed to parse '%s' as an URL.", url_arg);
2019 goto error;
2020 }
2021
2022 if (uris[0].dtype == LTTNG_DST_PATH) {
2023 ret = lttng_snapshot_output_set_local_path(
2024 uris[0].dst.path, snapshot_output);
2025 free(uris);
2026 if (ret != 0) {
2027 ERR("Failed to assign '%s' as a local destination.",
2028 url_arg);
2029 goto error;
2030 }
2031 } else {
2032 ret = lttng_snapshot_output_set_network_url(
2033 url_arg, snapshot_output);
2034 free(uris);
2035 if (ret != 0) {
2036 ERR("Failed to assign '%s' as a network URL.",
2037 url_arg);
2038 goto error;
2039 }
2040 }
2041 }
2042
2043 if (path_arg) {
2044 ret = lttng_snapshot_output_set_local_path(
2045 path_arg, snapshot_output);
2046 if (ret != 0) {
2047 ERR("Failed to parse '%s' as a local path.", path_arg);
2048 goto error;
2049 }
2050 }
2051
2052 if (ctrl_url_arg) {
2053 /*
2054 * Two argument form, network output with separate control and
2055 * data URLs.
2056 */
2057 ret = lttng_snapshot_output_set_network_urls(
2058 ctrl_url_arg, data_url_arg, snapshot_output);
2059 if (ret != 0) {
2060 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2061 ctrl_url_arg, data_url_arg);
2062 goto error;
2063 }
2064 }
2065
2066 if (snapshot_output) {
2067 action_status = lttng_action_snapshot_session_set_output(
2068 action, snapshot_output);
2069 if (action_status != LTTNG_ACTION_STATUS_OK) {
2070 ERR("Failed to set snapshot session action's output.");
2071 goto error;
2072 }
2073
2074 /* Ownership of `snapshot_output` has been transferred to the action. */
2075 snapshot_output = NULL;
2076 }
2077
2078 if (policy) {
2079 enum lttng_action_status status;
2080 status = lttng_action_snapshot_session_set_rate_policy(
2081 action, policy);
2082 if (status != LTTNG_ACTION_STATUS_OK) {
2083 ERR("Failed to set rate policy");
2084 goto error;
2085 }
2086 }
2087
2088 goto end;
2089
2090 error:
2091 lttng_action_destroy(action);
2092 action = NULL;
2093 free(error);
2094 end:
2095 free(snapshot_name_arg);
2096 free(path_arg);
2097 free(url_arg);
2098 free(ctrl_url_arg);
2099 free(data_url_arg);
2100 free(snapshot_output);
2101 free(max_size_arg);
2102 lttng_rate_policy_destroy(policy);
2103 argpar_item_destroy(argpar_item);
2104 argpar_iter_destroy(argpar_iter);
2105 return action;
2106 }
2107
2108 namespace {
2109 struct action_descr {
2110 const char *name;
2111 struct lttng_action *(*handler) (int *argc, const char ***argv,
2112 int argc_offset);
2113 };
2114 } /* namespace */
2115
2116 static const
2117 struct action_descr action_descrs[] = {
2118 { "notify", handle_action_notify },
2119 { "start-session", handle_action_start_session },
2120 { "stop-session", handle_action_stop_session },
2121 { "rotate-session", handle_action_rotate_session },
2122 { "snapshot-session", handle_action_snapshot_session },
2123 };
2124
2125 static
2126 void print_valid_action_names(void)
2127 {
2128 unsigned int i;
2129
2130 ERR("Valid action names are:");
2131
2132 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
2133 ERR(" %s", action_descrs[i].name);
2134 }
2135 }
2136
2137 static
2138 struct lttng_action *parse_action(const char *action_name, int *argc,
2139 const char ***argv, int argc_offset, int orig_arg_index,
2140 const char *orig_arg)
2141 {
2142 int i;
2143 struct lttng_action *action;
2144 const struct action_descr *descr = NULL;
2145
2146 for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
2147 if (strcmp(action_name, action_descrs[i].name) == 0) {
2148 descr = &action_descrs[i];
2149 break;
2150 }
2151 }
2152
2153 if (!descr) {
2154 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name '%s'",
2155 orig_arg_index + 1, orig_arg, action_name);
2156 print_valid_action_names();
2157 goto error;
2158 }
2159
2160 action = descr->handler(argc, argv, argc_offset);
2161 if (!action) {
2162 /* The handler has already printed an error message. */
2163 goto error;
2164 }
2165
2166 goto end;
2167 error:
2168 action = NULL;
2169 end:
2170 return action;
2171 }
2172
2173 static const
2174 struct argpar_opt_descr add_trigger_options[] = {
2175 { OPT_HELP, 'h', "help", false },
2176 { OPT_LIST_OPTIONS, '\0', "list-options", false },
2177 { OPT_CONDITION, '\0', "condition", true },
2178 { OPT_ACTION, '\0', "action", true },
2179 { OPT_NAME, '\0', "name", true },
2180 { OPT_OWNER_UID, '\0', "owner-uid", true },
2181 ARGPAR_OPT_DESCR_SENTINEL,
2182 };
2183
2184 static
2185 void lttng_actions_destructor(void *p)
2186 {
2187 struct lttng_action *action = (lttng_action *) p;
2188
2189 lttng_action_destroy(action);
2190 }
2191
2192 int cmd_add_trigger(int argc, const char **argv)
2193 {
2194 int ret;
2195 int my_argc = argc - 1;
2196 const char **my_argv = argv + 1;
2197 struct lttng_condition *condition = NULL;
2198 struct lttng_dynamic_pointer_array actions;
2199 struct argpar_iter *argpar_iter = NULL;
2200 const struct argpar_item *argpar_item = NULL;
2201 const struct argpar_error *argpar_error = NULL;
2202 struct lttng_action *action_list = NULL;
2203 struct lttng_action *action = NULL;
2204 struct lttng_trigger *trigger = NULL;
2205 char *name = NULL;
2206 int i;
2207 char *owner_uid = NULL;
2208 enum lttng_error_code ret_code;
2209 struct mi_writer *mi_writer = NULL;
2210
2211 lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor);
2212
2213 if (lttng_opt_mi) {
2214 mi_writer = mi_lttng_writer_create(
2215 fileno(stdout), lttng_opt_mi);
2216 if (!mi_writer) {
2217 ret = CMD_ERROR;
2218 goto error;
2219 }
2220
2221 /* Open command element. */
2222 ret = mi_lttng_writer_command_open(mi_writer,
2223 mi_lttng_element_command_add_trigger);
2224 if (ret) {
2225 ret = CMD_ERROR;
2226 goto error;
2227 }
2228
2229 /* Open output element. */
2230 ret = mi_lttng_writer_open_element(
2231 mi_writer, mi_lttng_element_command_output);
2232 if (ret) {
2233 ret = CMD_ERROR;
2234 goto error;
2235 }
2236 }
2237
2238 while (true) {
2239 enum parse_next_item_status status;
2240 int ingested_args;
2241 const struct argpar_opt_descr *descr;
2242 const char *arg;
2243
2244 argpar_iter_destroy(argpar_iter);
2245 argpar_iter = argpar_iter_create(my_argc, my_argv,
2246 add_trigger_options);
2247 if (!argpar_iter) {
2248 ERR("Failed to create argpar iter.");
2249 goto error;
2250 }
2251
2252 status = parse_next_item(argpar_iter, &argpar_item,
2253 argc - my_argc, my_argv, true, &argpar_error, NULL);
2254 if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
2255
2256 if (argpar_error_type(argpar_error) ==
2257 ARGPAR_ERROR_TYPE_MISSING_OPT_ARG) {
2258 int opt_id = argpar_error_opt_descr(argpar_error, NULL)->id;
2259
2260 if (opt_id == OPT_CONDITION) {
2261 print_valid_condition_names();
2262 } else if (opt_id == OPT_ACTION) {
2263 print_valid_action_names();
2264 }
2265 }
2266
2267 goto error;
2268 } else if (status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
2269 goto error;
2270 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
2271 break;
2272 }
2273
2274 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
2275
2276 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_NON_OPT) {
2277 ERR("Unexpected argument `%s`.",
2278 argpar_item_non_opt_arg(argpar_item));
2279 goto error;
2280 }
2281
2282 ingested_args = argpar_iter_ingested_orig_args(argpar_iter);
2283
2284 my_argc -= ingested_args;
2285 my_argv += ingested_args;
2286
2287 descr = argpar_item_opt_descr(argpar_item);
2288 arg = argpar_item_opt_arg(argpar_item);
2289
2290 switch (descr->id) {
2291 case OPT_HELP:
2292 SHOW_HELP();
2293 ret = 0;
2294 goto end;
2295 case OPT_LIST_OPTIONS:
2296 list_cmd_options_argpar(stdout, add_trigger_options);
2297 ret = 0;
2298 goto end;
2299 case OPT_CONDITION:
2300 {
2301 if (condition) {
2302 ERR("A --condition was already given.");
2303 goto error;
2304 }
2305
2306 condition = parse_condition(arg, &my_argc, &my_argv,
2307 argc - my_argc, argc - my_argc - ingested_args,
2308 my_argv[-ingested_args]);
2309 if (!condition) {
2310 /*
2311 * An error message was already printed by
2312 * parse_condition.
2313 */
2314 goto error;
2315 }
2316
2317 break;
2318 }
2319 case OPT_ACTION:
2320 {
2321 action = parse_action(arg, &my_argc, &my_argv,
2322 argc - my_argc, argc - my_argc - ingested_args,
2323 my_argv[-ingested_args]);
2324 if (!action) {
2325 /*
2326 * An error message was already printed by
2327 * parse_condition.
2328 */
2329 goto error;
2330 }
2331
2332 ret = lttng_dynamic_pointer_array_add_pointer(
2333 &actions, action);
2334 if (ret) {
2335 ERR("Failed to add pointer to pointer array.");
2336 goto error;
2337 }
2338
2339 /* Ownership of the action was transferred to the list. */
2340 action = NULL;
2341
2342 break;
2343 }
2344 case OPT_NAME:
2345 {
2346 if (!assign_string(&name, arg, "--name")) {
2347 goto error;
2348 }
2349
2350 break;
2351 }
2352 case OPT_OWNER_UID:
2353 {
2354 if (!assign_string(&owner_uid, arg,
2355 "--owner-uid")) {
2356 goto error;
2357 }
2358
2359 break;
2360 }
2361 default:
2362 abort();
2363 }
2364 }
2365
2366 if (!condition) {
2367 ERR("Missing --condition.");
2368 goto error;
2369 }
2370
2371 if (lttng_dynamic_pointer_array_get_count(&actions) == 0) {
2372 ERR("Need at least one --action.");
2373 goto error;
2374 }
2375
2376 action_list = lttng_action_list_create();
2377 if (!action_list) {
2378 goto error;
2379 }
2380
2381 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&actions); i++) {
2382 enum lttng_action_status status;
2383
2384 action = (lttng_action *) lttng_dynamic_pointer_array_steal_pointer(&actions, i);
2385
2386 status = lttng_action_list_add_action(action_list, action);
2387 if (status != LTTNG_ACTION_STATUS_OK) {
2388 goto error;
2389 }
2390
2391 /*
2392 * The `lttng_action_list_add_action()` takes a reference to
2393 * the action. We can destroy ours.
2394 */
2395 lttng_action_destroy(action);
2396 action = NULL;
2397 }
2398
2399 trigger = lttng_trigger_create(condition, action_list);
2400 if (!trigger) {
2401 goto error;
2402 }
2403
2404 if (owner_uid) {
2405 enum lttng_trigger_status trigger_status;
2406 char *end;
2407 long long uid;
2408
2409 errno = 0;
2410 uid = strtol(owner_uid, &end, 10);
2411 if (end == owner_uid || *end != '\0' || errno != 0) {
2412 ERR("Failed to parse `%s` as a user id.", owner_uid);
2413 goto error;
2414 }
2415
2416 trigger_status = lttng_trigger_set_owner_uid(trigger, uid);
2417 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2418 ERR("Failed to set trigger's user identity.");
2419 goto error;
2420 }
2421 }
2422
2423 if (name) {
2424 ret_code = lttng_register_trigger_with_name(trigger, name);
2425 } else {
2426 ret_code = lttng_register_trigger_with_automatic_name(trigger);
2427 }
2428
2429 if (ret_code != LTTNG_OK) {
2430 ERR("Failed to register trigger: %s.",
2431 lttng_strerror(-ret_code));
2432 goto error;
2433 }
2434
2435 if (lttng_opt_mi) {
2436 ret_code = lttng_trigger_mi_serialize(trigger, mi_writer, NULL);
2437 if (ret_code != LTTNG_OK) {
2438 goto error;
2439 }
2440 } else {
2441 const char *returned_trigger_name;
2442 const enum lttng_trigger_status trigger_status =
2443 lttng_trigger_get_name(trigger,
2444 &returned_trigger_name);
2445
2446 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2447 WARN("Failed to retrieve the added trigger's name.");
2448 } else {
2449 MSG("Added trigger `%s`.", returned_trigger_name);
2450 }
2451 }
2452
2453 ret = 0;
2454
2455 goto end;
2456
2457 error:
2458 ret = 1;
2459
2460 end:
2461 /* Mi closing. */
2462 if (lttng_opt_mi && mi_writer) {
2463 int mi_ret;
2464
2465 /* Close output element. */
2466 mi_ret = mi_lttng_writer_close_element(mi_writer);
2467 if (mi_ret) {
2468 ret = 1;
2469 goto cleanup;
2470 }
2471
2472 mi_ret = mi_lttng_writer_write_element_bool(mi_writer,
2473 mi_lttng_element_command_success, ret ? 0 : 1);
2474 if (mi_ret) {
2475 ret = 1;
2476 goto cleanup;
2477 }
2478
2479 /* Command element close. */
2480 mi_ret = mi_lttng_writer_command_close(mi_writer);
2481 if (mi_ret) {
2482 ret = 1;
2483 goto cleanup;
2484 }
2485 }
2486
2487 cleanup:
2488 argpar_error_destroy(argpar_error);
2489 argpar_iter_destroy(argpar_iter);
2490 argpar_item_destroy(argpar_item);
2491 lttng_dynamic_pointer_array_reset(&actions);
2492 lttng_condition_destroy(condition);
2493 lttng_action_destroy(action_list);
2494 lttng_action_destroy(action);
2495 lttng_trigger_destroy(trigger);
2496 free(name);
2497 free(owner_uid);
2498 if (mi_writer && mi_lttng_writer_destroy(mi_writer)) {
2499 /* Preserve original error code. */
2500 ret = ret ? ret : CMD_ERROR;
2501 }
2502
2503 return ret;
2504 }
This page took 0.082082 seconds and 4 git commands to generate.