Fix: lttng: add-trigger: leak of parser context on capture
[lttng-tools.git] / src / bin / lttng / commands / add_trigger.c
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.h"
14 #include "../loglevel.h"
15 #include "../uprobe.h"
16
17 #include "common/argpar/argpar.h"
18 #include "common/argpar-utils/argpar-utils.h"
19 #include "common/dynamic-array.h"
20 #include "common/mi-lttng.h"
21 #include "common/string-utils/string-utils.h"
22 #include "common/utils.h"
23 #include <lttng/domain-internal.h>
24 /* For lttng_event_rule_type_str(). */
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/lttng.h>
27 #include "common/filter/filter-ast.h"
28 #include "common/filter/filter-ir.h"
29 #include "common/dynamic-array.h"
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 assert(load_expr_op);
443 assert(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
444 field_name = load_expr_op->u.symbol;
445 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 assert(load_expr_op);
468 assert(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
469 field_name = load_expr_op->u.symbol;
470 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 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 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(ptr);
641 }
642
643 struct parse_event_rule_res {
644 /* Owned by this. */
645 struct lttng_event_rule *er;
646
647 /* Array of `struct lttng_event_expr *` */
648 struct lttng_dynamic_pointer_array capture_descriptors;
649 };
650
651 static
652 struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv,
653 int argc_offset)
654 {
655 enum lttng_event_rule_type event_rule_type =
656 LTTNG_EVENT_RULE_TYPE_UNKNOWN;
657 struct argpar_iter *argpar_iter = NULL;
658 const struct argpar_item *argpar_item = NULL;
659 int consumed_args = -1;
660 struct lttng_kernel_probe_location *kernel_probe_location = NULL;
661 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
662 struct parse_event_rule_res res = { 0 };
663 struct lttng_event_expr *event_expr = NULL;
664 struct filter_parser_ctx *parser_ctx = NULL;
665 struct lttng_log_level_rule *log_level_rule = NULL;
666
667 /* Event rule type option */
668 char *event_rule_type_str = NULL;
669
670 /* Tracepoint and syscall options. */
671 char *name = NULL;
672 /* Array of strings. */
673 struct lttng_dynamic_pointer_array exclude_names;
674
675 /* For userspace / kernel probe and function. */
676 char *location = NULL;
677 char *event_name = NULL;
678
679 /* Filter. */
680 char *filter = NULL;
681
682 /* Log level. */
683 char *log_level_str = NULL;
684
685 lttng_dynamic_pointer_array_init(&res.capture_descriptors,
686 destroy_event_expr);
687
688 lttng_dynamic_pointer_array_init(&exclude_names, free);
689
690 argpar_iter = argpar_iter_create(*argc, *argv, event_rule_opt_descrs);
691 if (!argpar_iter) {
692 ERR("Failed to allocate an argpar iter.");
693 goto error;
694 }
695
696 while (true) {
697 enum parse_next_item_status status;
698
699 status = parse_next_item(argpar_iter, &argpar_item,
700 argc_offset, *argv, false, NULL, NULL);
701 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
702 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
703 goto error;
704 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
705 break;
706 }
707
708 assert(status == PARSE_NEXT_ITEM_STATUS_OK);
709
710 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
711 const struct argpar_opt_descr *descr =
712 argpar_item_opt_descr(argpar_item);
713 const char *arg = argpar_item_opt_arg(argpar_item);
714
715 switch (descr->id) {
716 case OPT_TYPE:
717 if (!assign_event_rule_type(&event_rule_type, arg)) {
718 goto error;
719 }
720
721 /* Save the string for later use. */
722 if (!assign_string(&event_rule_type_str, arg,
723 "--type/-t")) {
724 goto error;
725 }
726
727 break;
728 case OPT_LOCATION:
729 if (!assign_string(&location, arg,
730 "--location/-L")) {
731 goto error;
732 }
733
734 break;
735 case OPT_EVENT_NAME:
736 if (!assign_string(&event_name, arg,
737 "--event-name/-E")) {
738 goto error;
739 }
740
741 break;
742 case OPT_FILTER:
743 if (!assign_string(&filter, arg,
744 "--filter/-f")) {
745 goto error;
746 }
747
748 break;
749 case OPT_NAME:
750 if (!assign_string(&name, arg,
751 "--name/-n")) {
752 goto error;
753 }
754
755 break;
756 case OPT_EXCLUDE_NAME:
757 {
758 int ret;
759
760 ret = lttng_dynamic_pointer_array_add_pointer(
761 &exclude_names,
762 strdup(arg));
763 if (ret != 0) {
764 ERR("Failed to add pointer to dynamic pointer array.");
765 goto error;
766 }
767
768 break;
769 }
770 case OPT_LOG_LEVEL:
771 if (!assign_string(&log_level_str, arg,
772 "--log-level/-l")) {
773 goto error;
774 }
775
776 break;
777 case OPT_CAPTURE:
778 {
779 int ret;
780
781 ret = filter_parser_ctx_create_from_filter_expression(
782 arg, &parser_ctx);
783 if (ret) {
784 ERR("Failed to parse capture expression `%s`.", arg);
785 goto error;
786 }
787
788 event_expr = ir_op_root_to_event_expr(
789 parser_ctx->ir_root, arg);
790 filter_parser_ctx_free(parser_ctx);
791 parser_ctx = NULL;
792 if (!event_expr) {
793 /*
794 * ir_op_root_to_event_expr has printed
795 * an error message.
796 */
797 goto error;
798 }
799
800 ret = lttng_dynamic_pointer_array_add_pointer(
801 &res.capture_descriptors,
802 event_expr);
803 if (ret) {
804 goto error;
805 }
806
807 /*
808 * The ownership of event expression was
809 * transferred to the dynamic array.
810 */
811 event_expr = NULL;
812
813 break;
814 }
815 default:
816 abort();
817 }
818 } else {
819 const char *arg = argpar_item_non_opt_arg(argpar_item);
820
821 /* Don't accept non-option arguments. */
822 ERR("Unexpected argument '%s'", arg);
823 goto error;
824 }
825 }
826
827 if (event_rule_type == LTTNG_EVENT_RULE_TYPE_UNKNOWN) {
828 ERR("Event rule requires a --type.");
829 goto error;
830 }
831
832 /*
833 * Option --name is applicable to event rules of type kernel, user, jul,
834 * log4j,python and syscall. If --name is omitted, it is implicitly
835 * "*".
836 */
837 switch (event_rule_type) {
838 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
839 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
840 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
841 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
842 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
843 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
844 if (!name) {
845 name = strdup("*");
846 }
847 break;
848
849 default:
850 if (name) {
851 ERR("Can't use --name with %s event rules.",
852 lttng_event_rule_type_str(
853 event_rule_type));
854 goto error;
855 }
856
857 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
858 ERR("Can't use --exclude-name/-x with %s event rules.",
859 lttng_event_rule_type_str(
860 event_rule_type));
861 goto error;
862 }
863 }
864
865 /*
866 * Option --location is only applicable to (and mandatory for) event
867 * rules of type {k,u}probe and function.
868 *
869 * Option --event-name is only applicable to event rules of type probe.
870 * If omitted, it defaults to the location.
871 */
872 switch (event_rule_type) {
873 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
874 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
875 if (!location) {
876 ERR("Event rule of type %s requires a --location.",
877 lttng_event_rule_type_str(event_rule_type));
878 goto error;
879 }
880
881 if (!event_name) {
882 event_name = strdup(location);
883 }
884
885 break;
886
887 default:
888 if (location) {
889 ERR("Can't use --location with %s event rules.",
890 lttng_event_rule_type_str(event_rule_type));
891 goto error;
892 }
893
894 if (event_name) {
895 ERR("Can't use --event-name with %s event rules.",
896 lttng_event_rule_type_str(
897 event_rule_type));
898 goto error;
899 }
900 }
901
902 /*
903 * Update *argc and *argv so our caller can keep parsing what follows.
904 */
905 consumed_args = argpar_iter_ingested_orig_args(argpar_iter);
906 assert(consumed_args >= 0);
907 *argc -= consumed_args;
908 *argv += consumed_args;
909
910 /*
911 * Adding a filter to a probe, function or userspace-probe would be
912 * denied by the kernel tracer as it's not supported at the moment. We
913 * do an early check here to warn the user.
914 */
915 if (filter) {
916 switch (event_rule_type) {
917 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
918 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
919 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
920 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
921 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
922 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
923 break;
924 default:
925 ERR("Filter expressions are not supported for %s event rules.",
926 lttng_event_rule_type_str(event_rule_type));
927 goto error;
928 }
929 }
930
931 /*
932 * If --exclude-name/-x was passed, split it into an exclusion list.
933 * Exclusions are only supported by
934 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
935 */
936 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
937 if (event_rule_type != LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT) {
938 ERR("Event name exclusions are not yet implemented for %s event rules.",
939 lttng_event_rule_type_str(event_rule_type));
940 goto error;
941 }
942
943 if (validate_exclusion_list(name, &exclude_names) != 0) {
944 /*
945 * Assume validate_exclusion_list already prints an
946 * error message.
947 */
948 goto error;
949 }
950 }
951
952 if (log_level_str) {
953 switch (event_rule_type) {
954 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
955 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
956 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
957 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
958 {
959 int log_level;
960 bool log_level_only;
961
962 if (strcmp(log_level_str, "..") == 0) {
963 /*
964 * ".." is the same as passing no log level
965 * option and correspond to the "ANY" case.
966 */
967 break;
968 }
969
970 if (!parse_log_level_string(log_level_str, event_rule_type,
971 &log_level, &log_level_only)) {
972 ERR("Failed to parse log level string `%s`.",
973 log_level_str);
974 goto error;
975 }
976
977 if (log_level_only) {
978 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
979 } else {
980 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level);
981 }
982
983 if (log_level_rule == NULL) {
984 ERR("Failed to create log level rule object.");
985 goto error;
986 }
987 break;
988 }
989 default:
990 ERR("Log levels are not supported for %s event rules.",
991 lttng_event_rule_type_str(event_rule_type));
992 goto error;
993 }
994 }
995
996 /* Finally, create the event rule object. */
997 switch (event_rule_type) {
998 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
999 {
1000 enum lttng_event_rule_status event_rule_status;
1001
1002 res.er = lttng_event_rule_user_tracepoint_create();
1003 if (!res.er) {
1004 ERR("Failed to create user_tracepoint event rule.");
1005 goto error;
1006 }
1007
1008 /* Set pattern. */
1009 event_rule_status = lttng_event_rule_user_tracepoint_set_name_pattern(
1010 res.er, name);
1011 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1012 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1013 name);
1014 goto error;
1015 }
1016
1017 /* Set filter. */
1018 if (filter) {
1019 event_rule_status = lttng_event_rule_user_tracepoint_set_filter(
1020 res.er, filter);
1021 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1022 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1023 filter);
1024 goto error;
1025 }
1026 }
1027
1028 /* Set exclusion list. */
1029 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
1030 int n;
1031 int count = lttng_dynamic_pointer_array_get_count(
1032 &exclude_names);
1033
1034 for (n = 0; n < count; n++) {
1035 const char *exclude_name =
1036 lttng_dynamic_pointer_array_get_pointer(
1037 &exclude_names,
1038 n);
1039
1040 event_rule_status =
1041 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1042 res.er,
1043 exclude_name);
1044 if (event_rule_status !=
1045 LTTNG_EVENT_RULE_STATUS_OK) {
1046 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1047 exclude_name);
1048 goto error;
1049 }
1050 }
1051 }
1052
1053 if (log_level_rule) {
1054 event_rule_status =
1055 lttng_event_rule_user_tracepoint_set_log_level_rule(
1056 res.er, log_level_rule);
1057
1058 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1059 ERR("Failed to set log level on event fule.");
1060 goto error;
1061 }
1062 }
1063
1064 break;
1065 }
1066 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
1067 {
1068 enum lttng_event_rule_status event_rule_status;
1069
1070 res.er = lttng_event_rule_kernel_tracepoint_create();
1071 if (!res.er) {
1072 ERR("Failed to create kernel_tracepoint event rule.");
1073 goto error;
1074 }
1075
1076 /* Set pattern. */
1077 event_rule_status = lttng_event_rule_kernel_tracepoint_set_name_pattern(
1078 res.er, name);
1079 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1080 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1081 name);
1082 goto error;
1083 }
1084
1085 /* Set filter. */
1086 if (filter) {
1087 event_rule_status = lttng_event_rule_kernel_tracepoint_set_filter(
1088 res.er, filter);
1089 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1090 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1091 filter);
1092 goto error;
1093 }
1094 }
1095 break;
1096 }
1097 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1098 {
1099 enum lttng_event_rule_status event_rule_status;
1100
1101 res.er = lttng_event_rule_jul_logging_create();
1102 if (!res.er) {
1103 ERR("Failed to create jul_logging event rule.");
1104 goto error;
1105 }
1106
1107 /* Set pattern. */
1108 event_rule_status = lttng_event_rule_jul_logging_set_name_pattern(
1109 res.er, name);
1110 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1111 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1112 name);
1113 goto error;
1114 }
1115
1116 /* Set filter. */
1117 if (filter) {
1118 event_rule_status = lttng_event_rule_jul_logging_set_filter(
1119 res.er, filter);
1120 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1121 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1122 filter);
1123 goto error;
1124 }
1125 }
1126
1127 if (log_level_rule) {
1128 event_rule_status =
1129 lttng_event_rule_jul_logging_set_log_level_rule(
1130 res.er, log_level_rule);
1131
1132 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1133 ERR("Failed to set log level on event fule.");
1134 goto error;
1135 }
1136 }
1137 break;
1138 }
1139 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1140 {
1141 enum lttng_event_rule_status event_rule_status;
1142
1143 res.er = lttng_event_rule_log4j_logging_create();
1144 if (!res.er) {
1145 ERR("Failed to create jul_logging event rule.");
1146 goto error;
1147 }
1148
1149 /* Set pattern. */
1150 event_rule_status = lttng_event_rule_log4j_logging_set_name_pattern(
1151 res.er, name);
1152 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1153 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1154 name);
1155 goto error;
1156 }
1157
1158 /* Set filter. */
1159 if (filter) {
1160 event_rule_status = lttng_event_rule_log4j_logging_set_filter(
1161 res.er, filter);
1162 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1163 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1164 filter);
1165 goto error;
1166 }
1167 }
1168
1169 if (log_level_rule) {
1170 event_rule_status =
1171 lttng_event_rule_log4j_logging_set_log_level_rule(
1172 res.er, log_level_rule);
1173
1174 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1175 ERR("Failed to set log level on event fule.");
1176 goto error;
1177 }
1178 }
1179 break;
1180 }
1181 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1182 {
1183 enum lttng_event_rule_status event_rule_status;
1184
1185 res.er = lttng_event_rule_python_logging_create();
1186 if (!res.er) {
1187 ERR("Failed to create jul_logging event rule.");
1188 goto error;
1189 }
1190
1191 /* Set pattern. */
1192 event_rule_status = lttng_event_rule_python_logging_set_name_pattern(
1193 res.er, name);
1194 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1195 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1196 name);
1197 goto error;
1198 }
1199
1200 /* Set filter. */
1201 if (filter) {
1202 event_rule_status = lttng_event_rule_python_logging_set_filter(
1203 res.er, filter);
1204 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1205 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1206 filter);
1207 goto error;
1208 }
1209 }
1210
1211 if (log_level_rule) {
1212 event_rule_status =
1213 lttng_event_rule_python_logging_set_log_level_rule(
1214 res.er, log_level_rule);
1215
1216 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1217 ERR("Failed to set log level on event fule.");
1218 goto error;
1219 }
1220 }
1221 break;
1222 }
1223 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
1224 {
1225 int ret;
1226 enum lttng_event_rule_status event_rule_status;
1227
1228 ret = parse_kernel_probe_opts(
1229 location, &kernel_probe_location);
1230 if (ret) {
1231 ERR("Failed to parse kernel probe location.");
1232 goto error;
1233 }
1234
1235 assert(kernel_probe_location);
1236 res.er = lttng_event_rule_kernel_kprobe_create(kernel_probe_location);
1237 if (!res.er) {
1238 ERR("Failed to create kprobe event rule.");
1239 goto error;
1240 }
1241
1242 event_rule_status =
1243 lttng_event_rule_kernel_kprobe_set_event_name(
1244 res.er, event_name);
1245 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1246 ERR("Failed to set kprobe event rule's name to '%s'.",
1247 event_name);
1248 goto error;
1249 }
1250
1251 break;
1252 }
1253 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
1254 {
1255 int ret;
1256 enum lttng_event_rule_status event_rule_status;
1257
1258 ret = parse_userspace_probe_opts(
1259 location, &userspace_probe_location);
1260 if (ret) {
1261 ERR("Failed to parse user space probe location.");
1262 goto error;
1263 }
1264
1265 res.er = lttng_event_rule_kernel_uprobe_create(userspace_probe_location);
1266 if (!res.er) {
1267 ERR("Failed to create userspace probe event rule.");
1268 goto error;
1269 }
1270
1271 event_rule_status =
1272 lttng_event_rule_kernel_uprobe_set_event_name(
1273 res.er, event_name);
1274 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1275 ERR("Failed to set user space probe event rule's name to '%s'.",
1276 event_name);
1277 goto error;
1278 }
1279
1280 break;
1281 }
1282 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
1283 {
1284 enum lttng_event_rule_status event_rule_status;
1285 enum lttng_event_rule_kernel_syscall_emission_site emission_site;
1286
1287 if (!parse_syscall_emission_site_from_type(
1288 event_rule_type_str, &emission_site)) {
1289 ERR("Failed to parse syscall type '%s'.", event_rule_type_str);
1290 goto error;
1291 }
1292
1293 res.er = lttng_event_rule_kernel_syscall_create(emission_site);
1294 if (!res.er) {
1295 ERR("Failed to create syscall event rule.");
1296 goto error;
1297 }
1298
1299 event_rule_status = lttng_event_rule_kernel_syscall_set_name_pattern(
1300 res.er, name);
1301 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1302 ERR("Failed to set syscall event rule's pattern to '%s'.",
1303 name);
1304 goto error;
1305 }
1306
1307 if (filter) {
1308 event_rule_status = lttng_event_rule_kernel_syscall_set_filter(
1309 res.er, filter);
1310 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1311 ERR("Failed to set syscall event rule's filter to '%s'.",
1312 filter);
1313 goto error;
1314 }
1315 }
1316
1317 break;
1318 }
1319 default:
1320 abort();
1321 goto error;
1322 }
1323
1324 goto end;
1325
1326 error:
1327 lttng_event_rule_destroy(res.er);
1328 res.er = NULL;
1329 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1330
1331 end:
1332 if (parser_ctx) {
1333 filter_parser_ctx_free(parser_ctx);
1334 }
1335
1336 lttng_event_expr_destroy(event_expr);
1337 argpar_item_destroy(argpar_item);
1338 argpar_iter_destroy(argpar_iter);
1339 free(filter);
1340 free(name);
1341 lttng_dynamic_pointer_array_reset(&exclude_names);
1342 free(log_level_str);
1343 free(location);
1344 free(event_name);
1345 free(event_rule_type_str);
1346
1347 lttng_kernel_probe_location_destroy(kernel_probe_location);
1348 lttng_userspace_probe_location_destroy(userspace_probe_location);
1349 lttng_log_level_rule_destroy(log_level_rule);
1350 return res;
1351 }
1352
1353 static
1354 struct lttng_condition *handle_condition_event(int *argc, const char ***argv,
1355 int argc_offset)
1356 {
1357 struct parse_event_rule_res res;
1358 struct lttng_condition *c;
1359 size_t i;
1360
1361 res = parse_event_rule(argc, argv, argc_offset);
1362 if (!res.er) {
1363 c = NULL;
1364 goto error;
1365 }
1366
1367 c = lttng_condition_event_rule_matches_create(res.er);
1368 lttng_event_rule_destroy(res.er);
1369 res.er = NULL;
1370 if (!c) {
1371 goto error;
1372 }
1373
1374 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors);
1375 i++) {
1376 enum lttng_condition_status status;
1377 struct lttng_event_expr **expr =
1378 lttng_dynamic_array_get_element(
1379 &res.capture_descriptors.array, i);
1380
1381 assert(expr);
1382 assert(*expr);
1383 status = lttng_condition_event_rule_matches_append_capture_descriptor(
1384 c, *expr);
1385 if (status != LTTNG_CONDITION_STATUS_OK) {
1386 if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) {
1387 ERR("The capture feature is unsupported by the event-rule condition type");
1388 }
1389
1390 goto error;
1391 }
1392
1393 /* Ownership of event expression moved to `c` */
1394 *expr = NULL;
1395 }
1396
1397 goto end;
1398
1399 error:
1400 lttng_condition_destroy(c);
1401 c = NULL;
1402
1403 end:
1404 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1405 lttng_event_rule_destroy(res.er);
1406 return c;
1407 }
1408
1409 struct condition_descr {
1410 const char *name;
1411 struct lttng_condition *(*handler) (int *argc, const char ***argv,
1412 int argc_offset);
1413 };
1414
1415 static const
1416 struct condition_descr condition_descrs[] = {
1417 { "event-rule-matches", handle_condition_event },
1418 };
1419
1420 static
1421 void print_valid_condition_names(void)
1422 {
1423 unsigned int i;
1424
1425 ERR("Valid condition names are:");
1426
1427 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
1428 ERR(" %s", condition_descrs[i].name);
1429 }
1430 }
1431
1432 static
1433 struct lttng_condition *parse_condition(const char *condition_name, int *argc,
1434 const char ***argv, int argc_offset, int orig_arg_index,
1435 const char *orig_arg)
1436 {
1437 int i;
1438 struct lttng_condition *cond;
1439 const struct condition_descr *descr = NULL;
1440
1441 for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
1442 if (strcmp(condition_name, condition_descrs[i].name) == 0) {
1443 descr = &condition_descrs[i];
1444 break;
1445 }
1446 }
1447
1448 if (!descr) {
1449 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'",
1450 orig_arg_index + 1, orig_arg, condition_name);
1451 print_valid_condition_names();
1452 goto error;
1453 }
1454
1455 cond = descr->handler(argc, argv, argc_offset);
1456 if (!cond) {
1457 /* The handler has already printed an error message. */
1458 goto error;
1459 }
1460
1461 goto end;
1462 error:
1463 cond = NULL;
1464 end:
1465 return cond;
1466 }
1467
1468 static struct lttng_rate_policy *parse_rate_policy(const char *policy_str)
1469 {
1470 int ret;
1471 size_t num_token = 0;
1472 struct lttng_dynamic_pointer_array tokens;
1473 struct lttng_rate_policy *policy = NULL;
1474 enum lttng_rate_policy_type policy_type;
1475 unsigned long long value;
1476 char *policy_type_str;
1477 char *policy_value_str;
1478
1479 assert(policy_str);
1480 lttng_dynamic_pointer_array_init(&tokens, NULL);
1481
1482 /* Rate policy fields are separated by ':'. */
1483 ret = strutils_split(policy_str, ':', 1, &tokens);
1484 if (ret == 0) {
1485 num_token = lttng_dynamic_pointer_array_get_count(&tokens);
1486 }
1487
1488 /*
1489 * Early sanity check that the number of parameter is exactly 2.
1490 * i.e : type:value
1491 */
1492 if (num_token != 2) {
1493 ERR("Rate policy format is invalid.");
1494 goto end;
1495 }
1496
1497 policy_type_str = lttng_dynamic_pointer_array_get_pointer(&tokens, 0);
1498 policy_value_str = lttng_dynamic_pointer_array_get_pointer(&tokens, 1);
1499
1500 /* Parse the type. */
1501 if (strcmp(policy_type_str, "once-after") == 0) {
1502 policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N;
1503 } else if (strcmp(policy_type_str, "every") == 0) {
1504 policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N;
1505 } else {
1506 ERR("Rate policy type `%s` unknown.", policy_type_str);
1507 goto end;
1508 }
1509
1510 /* Parse the value. */
1511 if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) {
1512 ERR("Failed to parse rate policy value `%s` as an integer.",
1513 policy_value_str);
1514 goto end;
1515 }
1516
1517 if (value == 0) {
1518 ERR("Rate policy value `%s` must be > 0.", policy_value_str);
1519 goto end;
1520 }
1521
1522 switch (policy_type) {
1523 case LTTNG_RATE_POLICY_TYPE_EVERY_N:
1524 policy = lttng_rate_policy_every_n_create(value);
1525 break;
1526 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N:
1527 policy = lttng_rate_policy_once_after_n_create(value);
1528 break;
1529 default:
1530 abort();
1531 }
1532
1533 if (policy == NULL) {
1534 ERR("Failed to create rate policy `%s`.", policy_str);
1535 }
1536
1537 end:
1538 lttng_dynamic_pointer_array_reset(&tokens);
1539 return policy;
1540 }
1541
1542 static const struct argpar_opt_descr notify_action_opt_descrs[] = {
1543 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1544 ARGPAR_OPT_DESCR_SENTINEL
1545 };
1546
1547 static
1548 struct lttng_action *handle_action_notify(int *argc, const char ***argv,
1549 int argc_offset)
1550 {
1551 struct lttng_action *action = NULL;
1552 struct argpar_iter *argpar_iter = NULL;
1553 const struct argpar_item *argpar_item = NULL;
1554 struct lttng_rate_policy *policy = NULL;
1555
1556 argpar_iter = argpar_iter_create(*argc, *argv, notify_action_opt_descrs);
1557 if (!argpar_iter) {
1558 ERR("Failed to allocate an argpar iter.");
1559 goto error;
1560 }
1561
1562 while (true) {
1563 enum parse_next_item_status status;
1564
1565 status = parse_next_item(argpar_iter, &argpar_item,
1566 argc_offset, *argv, false, NULL,
1567 "While parsing `notify` action:");
1568 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1569 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1570 goto error;
1571 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1572 break;
1573 }
1574
1575 assert(status == PARSE_NEXT_ITEM_STATUS_OK);
1576
1577 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1578 const struct argpar_opt_descr *descr =
1579 argpar_item_opt_descr(argpar_item);
1580 const char *arg = argpar_item_opt_arg(argpar_item);
1581
1582 switch (descr->id) {
1583 case OPT_RATE_POLICY:
1584 {
1585 policy = parse_rate_policy(arg);
1586 if (!policy) {
1587 goto error;
1588 }
1589 break;
1590 }
1591 default:
1592 abort();
1593 }
1594 } else {
1595 const char *arg = argpar_item_non_opt_arg(argpar_item);
1596
1597 ERR("Unexpected argument `%s`.", arg);
1598 goto error;
1599 }
1600 }
1601
1602 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1603 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1604
1605 action = lttng_action_notify_create();
1606 if (!action) {
1607 ERR("Failed to create notify action");
1608 goto error;
1609 }
1610
1611 if (policy) {
1612 enum lttng_action_status status;
1613 status = lttng_action_notify_set_rate_policy(action, policy);
1614 if (status != LTTNG_ACTION_STATUS_OK) {
1615 ERR("Failed to set rate policy");
1616 goto error;
1617 }
1618 }
1619
1620 goto end;
1621
1622 error:
1623 lttng_action_destroy(action);
1624 action = NULL;
1625 end:
1626 lttng_rate_policy_destroy(policy);
1627 argpar_item_destroy(argpar_item);
1628 argpar_iter_destroy(argpar_iter);
1629 return action;
1630 }
1631
1632 /*
1633 * Generic handler for a kind of action that takes a session name and an
1634 * optional rate policy.
1635 */
1636
1637 static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
1638 const char ***argv,
1639 int argc_offset,
1640 struct lttng_action *(*create_action_cb)(void),
1641 enum lttng_action_status (*set_session_name_cb)(
1642 struct lttng_action *, const char *),
1643 enum lttng_action_status (*set_rate_policy_cb)(
1644 struct lttng_action *,
1645 const struct lttng_rate_policy *),
1646 const char *action_name)
1647 {
1648 struct lttng_action *action = NULL;
1649 struct argpar_iter *argpar_iter = NULL;
1650 const struct argpar_item *argpar_item = NULL;
1651 const char *session_name_arg = NULL;
1652 enum lttng_action_status action_status;
1653 struct lttng_rate_policy *policy = NULL;
1654
1655 assert(set_session_name_cb);
1656 assert(set_rate_policy_cb);
1657
1658 const struct argpar_opt_descr rate_policy_opt_descrs[] = {
1659 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1660 ARGPAR_OPT_DESCR_SENTINEL
1661 };
1662
1663 argpar_iter = argpar_iter_create(*argc, *argv, rate_policy_opt_descrs);
1664 if (!argpar_iter) {
1665 ERR("Failed to allocate an argpar iter.");
1666 goto error;
1667 }
1668
1669 while (true) {
1670 enum parse_next_item_status status;
1671
1672 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
1673 *argv, false, NULL,
1674 "While parsing `%s` action:", action_name);
1675 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1676 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1677 goto error;
1678 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1679 break;
1680 }
1681
1682 assert(status == PARSE_NEXT_ITEM_STATUS_OK);
1683
1684 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1685 const struct argpar_opt_descr *descr =
1686 argpar_item_opt_descr(argpar_item);
1687 const char *arg = argpar_item_opt_arg(argpar_item);
1688
1689 switch (descr->id) {
1690 case OPT_RATE_POLICY:
1691 {
1692 policy = parse_rate_policy(arg);
1693 if (!policy) {
1694 goto error;
1695 }
1696 break;
1697 }
1698 default:
1699 abort();
1700 }
1701 } else {
1702 const char *arg = argpar_item_non_opt_arg(argpar_item);
1703 unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
1704
1705 switch (idx) {
1706 case 0:
1707 session_name_arg = arg;
1708 break;
1709 default:
1710 ERR("Unexpected argument `%s`.", arg);
1711 goto error;
1712 }
1713 }
1714 }
1715
1716 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1717 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1718
1719 if (!session_name_arg) {
1720 ERR("Missing session name.");
1721 goto error;
1722 }
1723
1724 action = create_action_cb();
1725 if (!action) {
1726 ERR("Failed to allocate %s session action.", action_name);
1727 goto error;
1728 }
1729
1730 action_status = set_session_name_cb(action, session_name_arg);
1731 if (action_status != LTTNG_ACTION_STATUS_OK) {
1732 ERR("Failed to set action %s session's session name to '%s'.",
1733 action_name, session_name_arg);
1734 goto error;
1735 }
1736
1737 if (policy) {
1738 action_status = set_rate_policy_cb(action, policy);
1739 if (action_status != LTTNG_ACTION_STATUS_OK) {
1740 ERR("Failed to set rate policy");
1741 goto error;
1742 }
1743 }
1744
1745 goto end;
1746
1747 error:
1748 lttng_action_destroy(action);
1749 action = NULL;
1750
1751 end:
1752 lttng_rate_policy_destroy(policy);
1753 argpar_item_destroy(argpar_item);
1754 argpar_iter_destroy(argpar_iter);
1755 return action;
1756 }
1757
1758 static
1759 struct lttng_action *handle_action_start_session(int *argc,
1760 const char ***argv, int argc_offset)
1761 {
1762 return handle_action_simple_session_with_policy(argc, argv,
1763 argc_offset,
1764 lttng_action_start_session_create,
1765 lttng_action_start_session_set_session_name,
1766 lttng_action_start_session_set_rate_policy, "start");
1767 }
1768
1769 static
1770 struct lttng_action *handle_action_stop_session(int *argc,
1771 const char ***argv, int argc_offset)
1772 {
1773 return handle_action_simple_session_with_policy(argc, argv,
1774 argc_offset,
1775 lttng_action_stop_session_create,
1776 lttng_action_stop_session_set_session_name,
1777 lttng_action_stop_session_set_rate_policy, "stop");
1778 }
1779
1780 static
1781 struct lttng_action *handle_action_rotate_session(int *argc,
1782 const char ***argv, int argc_offset)
1783 {
1784 return handle_action_simple_session_with_policy(argc, argv,
1785 argc_offset,
1786 lttng_action_rotate_session_create,
1787 lttng_action_rotate_session_set_session_name,
1788 lttng_action_rotate_session_set_rate_policy,
1789 "rotate");
1790 }
1791
1792 static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
1793 { OPT_NAME, 'n', "name", true },
1794 { OPT_MAX_SIZE, 'm', "max-size", true },
1795 { OPT_CTRL_URL, '\0', "ctrl-url", true },
1796 { OPT_DATA_URL, '\0', "data-url", true },
1797 { OPT_URL, '\0', "url", true },
1798 { OPT_PATH, '\0', "path", true },
1799 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1800 ARGPAR_OPT_DESCR_SENTINEL
1801 };
1802
1803 static
1804 struct lttng_action *handle_action_snapshot_session(int *argc,
1805 const char ***argv, int argc_offset)
1806 {
1807 struct lttng_action *action = NULL;
1808 struct argpar_iter *argpar_iter = NULL;
1809 const struct argpar_item *argpar_item = NULL;
1810 const char *session_name_arg = NULL;
1811 char *snapshot_name_arg = NULL;
1812 char *ctrl_url_arg = NULL;
1813 char *data_url_arg = NULL;
1814 char *max_size_arg = NULL;
1815 char *url_arg = NULL;
1816 char *path_arg = NULL;
1817 char *error = NULL;
1818 enum lttng_action_status action_status;
1819 struct lttng_snapshot_output *snapshot_output = NULL;
1820 struct lttng_rate_policy *policy = NULL;
1821 int ret;
1822 unsigned int locations_specified = 0;
1823
1824 argpar_iter = argpar_iter_create(*argc, *argv, snapshot_action_opt_descrs);
1825 if (!argpar_iter) {
1826 ERR("Failed to allocate an argpar iter.");
1827 goto error;
1828 }
1829
1830 while (true) {
1831 enum parse_next_item_status status;
1832
1833 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
1834 *argv, false, NULL, "While parsing `snapshot` action:");
1835 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1836 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
1837 goto error;
1838 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
1839 break;
1840 }
1841
1842 assert(status == PARSE_NEXT_ITEM_STATUS_OK);
1843
1844 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1845 const struct argpar_opt_descr *descr =
1846 argpar_item_opt_descr(argpar_item);
1847 const char *arg = argpar_item_opt_arg(argpar_item);
1848
1849 switch (descr->id) {
1850 case OPT_NAME:
1851 if (!assign_string(&snapshot_name_arg, arg, "--name/-n")) {
1852 goto error;
1853 }
1854
1855 break;
1856 case OPT_MAX_SIZE:
1857 if (!assign_string(&max_size_arg, arg, "--max-size/-m")) {
1858 goto error;
1859 }
1860
1861 break;
1862 case OPT_CTRL_URL:
1863 if (!assign_string(&ctrl_url_arg, arg, "--ctrl-url")) {
1864 goto error;
1865 }
1866
1867 break;
1868 case OPT_DATA_URL:
1869 if (!assign_string(&data_url_arg, arg, "--data-url")) {
1870 goto error;
1871 }
1872
1873 break;
1874 case OPT_URL:
1875 if (!assign_string(&url_arg, arg, "--url")) {
1876 goto error;
1877 }
1878
1879 break;
1880 case OPT_PATH:
1881 if (!assign_string(&path_arg, arg, "--path")) {
1882 goto error;
1883 }
1884
1885 break;
1886 case OPT_RATE_POLICY:
1887 {
1888 policy = parse_rate_policy(arg);
1889 if (!policy) {
1890 goto error;
1891 }
1892 break;
1893 }
1894 default:
1895 abort();
1896 }
1897 } else {
1898 const char *arg = argpar_item_non_opt_arg(argpar_item);
1899 unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
1900
1901 switch (idx) {
1902 case 0:
1903 session_name_arg = arg;
1904 break;
1905 default:
1906 ERR("Unexpected argument `%s`.", arg);
1907 goto error;
1908 }
1909 }
1910 }
1911
1912 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1913 *argv += argpar_iter_ingested_orig_args(argpar_iter);
1914
1915 if (!session_name_arg) {
1916 ERR("Missing session name.");
1917 goto error;
1918 }
1919
1920 /* --ctrl-url and --data-url must come in pair. */
1921 if (ctrl_url_arg && !data_url_arg) {
1922 ERR("--ctrl-url is specified, but --data-url is missing.");
1923 goto error;
1924 }
1925
1926 if (!ctrl_url_arg && data_url_arg) {
1927 ERR("--data-url is specified, but --ctrl-url is missing.");
1928 goto error;
1929 }
1930
1931 locations_specified += !!(ctrl_url_arg || data_url_arg);
1932 locations_specified += !!url_arg;
1933 locations_specified += !!path_arg;
1934
1935 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1936 if (locations_specified > 1) {
1937 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1938 goto error;
1939 }
1940
1941 /*
1942 * Did the user specify an option that implies using a
1943 * custom/unregistered output?
1944 */
1945 if (url_arg || ctrl_url_arg || path_arg) {
1946 snapshot_output = lttng_snapshot_output_create();
1947 if (!snapshot_output) {
1948 ERR("Failed to allocate a snapshot output.");
1949 goto error;
1950 }
1951 }
1952
1953 action = lttng_action_snapshot_session_create();
1954 if (!action) {
1955 ERR("Failed to allocate snapshot session action.");
1956 goto error;
1957 }
1958
1959 action_status = lttng_action_snapshot_session_set_session_name(
1960 action, session_name_arg);
1961 if (action_status != LTTNG_ACTION_STATUS_OK) {
1962 ERR("Failed to set action snapshot session's session name to '%s'.",
1963 session_name_arg);
1964 goto error;
1965 }
1966
1967 if (snapshot_name_arg) {
1968 if (!snapshot_output) {
1969 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1970 goto error;
1971 }
1972
1973 ret = lttng_snapshot_output_set_name(
1974 snapshot_name_arg, snapshot_output);
1975 if (ret != 0) {
1976 ERR("Failed to set name of snapshot output.");
1977 goto error;
1978 }
1979 }
1980
1981 if (max_size_arg) {
1982 uint64_t max_size;
1983
1984 if (!snapshot_output) {
1985 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1986 goto error;
1987 }
1988
1989 ret = utils_parse_size_suffix(max_size_arg, &max_size);
1990 if (ret != 0) {
1991 ERR("Failed to parse `%s` as a size.", max_size_arg);
1992 goto error;
1993 }
1994
1995 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1996 if (ret != 0) {
1997 ERR("Failed to set snapshot output's max size to %" PRIu64 " bytes.",
1998 max_size);
1999 goto error;
2000 }
2001 }
2002
2003 if (url_arg) {
2004 int num_uris;
2005 struct lttng_uri *uris;
2006
2007 if (!strstr(url_arg, "://")) {
2008 ERR("Failed to parse '%s' as an URL.", url_arg);
2009 goto error;
2010 }
2011
2012 num_uris = uri_parse_str_urls(url_arg, NULL, &uris);
2013 if (num_uris < 1) {
2014 ERR("Failed to parse '%s' as an URL.", url_arg);
2015 goto error;
2016 }
2017
2018 if (uris[0].dtype == LTTNG_DST_PATH) {
2019 ret = lttng_snapshot_output_set_local_path(
2020 uris[0].dst.path, snapshot_output);
2021 free(uris);
2022 if (ret != 0) {
2023 ERR("Failed to assign '%s' as a local destination.",
2024 url_arg);
2025 goto error;
2026 }
2027 } else {
2028 ret = lttng_snapshot_output_set_network_url(
2029 url_arg, snapshot_output);
2030 free(uris);
2031 if (ret != 0) {
2032 ERR("Failed to assign '%s' as a network URL.",
2033 url_arg);
2034 goto error;
2035 }
2036 }
2037 }
2038
2039 if (path_arg) {
2040 ret = lttng_snapshot_output_set_local_path(
2041 path_arg, snapshot_output);
2042 if (ret != 0) {
2043 ERR("Failed to parse '%s' as a local path.", path_arg);
2044 goto error;
2045 }
2046 }
2047
2048 if (ctrl_url_arg) {
2049 /*
2050 * Two argument form, network output with separate control and
2051 * data URLs.
2052 */
2053 ret = lttng_snapshot_output_set_network_urls(
2054 ctrl_url_arg, data_url_arg, snapshot_output);
2055 if (ret != 0) {
2056 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2057 ctrl_url_arg, data_url_arg);
2058 goto error;
2059 }
2060 }
2061
2062 if (snapshot_output) {
2063 action_status = lttng_action_snapshot_session_set_output(
2064 action, snapshot_output);
2065 if (action_status != LTTNG_ACTION_STATUS_OK) {
2066 ERR("Failed to set snapshot session action's output.");
2067 goto error;
2068 }
2069
2070 /* Ownership of `snapshot_output` has been transferred to the action. */
2071 snapshot_output = NULL;
2072 }
2073
2074 if (policy) {
2075 enum lttng_action_status status;
2076 status = lttng_action_snapshot_session_set_rate_policy(
2077 action, policy);
2078 if (status != LTTNG_ACTION_STATUS_OK) {
2079 ERR("Failed to set rate policy");
2080 goto error;
2081 }
2082 }
2083
2084 goto end;
2085
2086 error:
2087 lttng_action_destroy(action);
2088 action = NULL;
2089 free(error);
2090 end:
2091 free(snapshot_name_arg);
2092 free(path_arg);
2093 free(url_arg);
2094 free(ctrl_url_arg);
2095 free(data_url_arg);
2096 free(snapshot_output);
2097 free(max_size_arg);
2098 lttng_rate_policy_destroy(policy);
2099 argpar_item_destroy(argpar_item);
2100 argpar_iter_destroy(argpar_iter);
2101 return action;
2102 }
2103
2104 struct action_descr {
2105 const char *name;
2106 struct lttng_action *(*handler) (int *argc, const char ***argv,
2107 int argc_offset);
2108 };
2109
2110 static const
2111 struct action_descr action_descrs[] = {
2112 { "notify", handle_action_notify },
2113 { "start-session", handle_action_start_session },
2114 { "stop-session", handle_action_stop_session },
2115 { "rotate-session", handle_action_rotate_session },
2116 { "snapshot-session", handle_action_snapshot_session },
2117 };
2118
2119 static
2120 void print_valid_action_names(void)
2121 {
2122 unsigned int i;
2123
2124 ERR("Valid action names are:");
2125
2126 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
2127 ERR(" %s", action_descrs[i].name);
2128 }
2129 }
2130
2131 static
2132 struct lttng_action *parse_action(const char *action_name, int *argc,
2133 const char ***argv, int argc_offset, int orig_arg_index,
2134 const char *orig_arg)
2135 {
2136 int i;
2137 struct lttng_action *action;
2138 const struct action_descr *descr = NULL;
2139
2140 for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
2141 if (strcmp(action_name, action_descrs[i].name) == 0) {
2142 descr = &action_descrs[i];
2143 break;
2144 }
2145 }
2146
2147 if (!descr) {
2148 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name '%s'",
2149 orig_arg_index + 1, orig_arg, action_name);
2150 print_valid_action_names();
2151 goto error;
2152 }
2153
2154 action = descr->handler(argc, argv, argc_offset);
2155 if (!action) {
2156 /* The handler has already printed an error message. */
2157 goto error;
2158 }
2159
2160 goto end;
2161 error:
2162 action = NULL;
2163 end:
2164 return action;
2165 }
2166
2167 static const
2168 struct argpar_opt_descr add_trigger_options[] = {
2169 { OPT_HELP, 'h', "help", false },
2170 { OPT_LIST_OPTIONS, '\0', "list-options", false },
2171 { OPT_CONDITION, '\0', "condition", true },
2172 { OPT_ACTION, '\0', "action", true },
2173 { OPT_NAME, '\0', "name", true },
2174 { OPT_OWNER_UID, '\0', "owner-uid", true },
2175 ARGPAR_OPT_DESCR_SENTINEL,
2176 };
2177
2178 static
2179 void lttng_actions_destructor(void *p)
2180 {
2181 struct lttng_action *action = p;
2182
2183 lttng_action_destroy(action);
2184 }
2185
2186 int cmd_add_trigger(int argc, const char **argv)
2187 {
2188 int ret;
2189 int my_argc = argc - 1;
2190 const char **my_argv = argv + 1;
2191 struct lttng_condition *condition = NULL;
2192 struct lttng_dynamic_pointer_array actions;
2193 struct argpar_iter *argpar_iter = NULL;
2194 const struct argpar_item *argpar_item = NULL;
2195 const struct argpar_error *argpar_error = NULL;
2196 struct lttng_action *action_list = NULL;
2197 struct lttng_action *action = NULL;
2198 struct lttng_trigger *trigger = NULL;
2199 char *name = NULL;
2200 int i;
2201 char *owner_uid = NULL;
2202 enum lttng_error_code ret_code;
2203 struct mi_writer *mi_writer = NULL;
2204
2205 lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor);
2206
2207 if (lttng_opt_mi) {
2208 mi_writer = mi_lttng_writer_create(
2209 fileno(stdout), lttng_opt_mi);
2210 if (!mi_writer) {
2211 ret = CMD_ERROR;
2212 goto error;
2213 }
2214
2215 /* Open command element. */
2216 ret = mi_lttng_writer_command_open(mi_writer,
2217 mi_lttng_element_command_add_trigger);
2218 if (ret) {
2219 ret = CMD_ERROR;
2220 goto error;
2221 }
2222
2223 /* Open output element. */
2224 ret = mi_lttng_writer_open_element(
2225 mi_writer, mi_lttng_element_command_output);
2226 if (ret) {
2227 ret = CMD_ERROR;
2228 goto error;
2229 }
2230 }
2231
2232 while (true) {
2233 enum parse_next_item_status status;
2234 int ingested_args;
2235 const struct argpar_opt_descr *descr;
2236 const char *arg;
2237
2238 argpar_iter_destroy(argpar_iter);
2239 argpar_iter = argpar_iter_create(my_argc, my_argv,
2240 add_trigger_options);
2241 if (!argpar_iter) {
2242 ERR("Failed to create argpar iter.");
2243 goto error;
2244 }
2245
2246 status = parse_next_item(argpar_iter, &argpar_item,
2247 argc - my_argc, my_argv, true, &argpar_error, NULL);
2248 if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
2249
2250 if (argpar_error_type(argpar_error) ==
2251 ARGPAR_ERROR_TYPE_MISSING_OPT_ARG) {
2252 int opt_id = argpar_error_opt_descr(argpar_error, NULL)->id;
2253
2254 if (opt_id == OPT_CONDITION) {
2255 print_valid_condition_names();
2256 } else if (opt_id == OPT_ACTION) {
2257 print_valid_action_names();
2258 }
2259 }
2260
2261 goto error;
2262 } else if (status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
2263 goto error;
2264 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
2265 break;
2266 }
2267
2268 assert(status == PARSE_NEXT_ITEM_STATUS_OK);
2269
2270 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_NON_OPT) {
2271 ERR("Unexpected argument `%s`.",
2272 argpar_item_non_opt_arg(argpar_item));
2273 goto error;
2274 }
2275
2276 ingested_args = argpar_iter_ingested_orig_args(argpar_iter);
2277
2278 my_argc -= ingested_args;
2279 my_argv += ingested_args;
2280
2281 descr = argpar_item_opt_descr(argpar_item);
2282 arg = argpar_item_opt_arg(argpar_item);
2283
2284 switch (descr->id) {
2285 case OPT_HELP:
2286 SHOW_HELP();
2287 ret = 0;
2288 goto end;
2289 case OPT_LIST_OPTIONS:
2290 list_cmd_options_argpar(stdout, add_trigger_options);
2291 ret = 0;
2292 goto end;
2293 case OPT_CONDITION:
2294 {
2295 if (condition) {
2296 ERR("A --condition was already given.");
2297 goto error;
2298 }
2299
2300 condition = parse_condition(arg, &my_argc, &my_argv,
2301 argc - my_argc, argc - my_argc - ingested_args,
2302 my_argv[-ingested_args]);
2303 if (!condition) {
2304 /*
2305 * An error message was already printed by
2306 * parse_condition.
2307 */
2308 goto error;
2309 }
2310
2311 break;
2312 }
2313 case OPT_ACTION:
2314 {
2315 action = parse_action(arg, &my_argc, &my_argv,
2316 argc - my_argc, argc - my_argc - ingested_args,
2317 my_argv[-ingested_args]);
2318 if (!action) {
2319 /*
2320 * An error message was already printed by
2321 * parse_condition.
2322 */
2323 goto error;
2324 }
2325
2326 ret = lttng_dynamic_pointer_array_add_pointer(
2327 &actions, action);
2328 if (ret) {
2329 ERR("Failed to add pointer to pointer array.");
2330 goto error;
2331 }
2332
2333 /* Ownership of the action was transferred to the list. */
2334 action = NULL;
2335
2336 break;
2337 }
2338 case OPT_NAME:
2339 {
2340 if (!assign_string(&name, arg, "--name")) {
2341 goto error;
2342 }
2343
2344 break;
2345 }
2346 case OPT_OWNER_UID:
2347 {
2348 if (!assign_string(&owner_uid, arg,
2349 "--owner-uid")) {
2350 goto error;
2351 }
2352
2353 break;
2354 }
2355 default:
2356 abort();
2357 }
2358 }
2359
2360 if (!condition) {
2361 ERR("Missing --condition.");
2362 goto error;
2363 }
2364
2365 if (lttng_dynamic_pointer_array_get_count(&actions) == 0) {
2366 ERR("Need at least one --action.");
2367 goto error;
2368 }
2369
2370 action_list = lttng_action_list_create();
2371 if (!action_list) {
2372 goto error;
2373 }
2374
2375 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&actions); i++) {
2376 enum lttng_action_status status;
2377
2378 action = lttng_dynamic_pointer_array_steal_pointer(&actions, i);
2379
2380 status = lttng_action_list_add_action(action_list, action);
2381 if (status != LTTNG_ACTION_STATUS_OK) {
2382 goto error;
2383 }
2384
2385 /*
2386 * The `lttng_action_list_add_action()` takes a reference to
2387 * the action. We can destroy ours.
2388 */
2389 lttng_action_destroy(action);
2390 action = NULL;
2391 }
2392
2393 trigger = lttng_trigger_create(condition, action_list);
2394 if (!trigger) {
2395 goto error;
2396 }
2397
2398 if (owner_uid) {
2399 enum lttng_trigger_status trigger_status;
2400 char *end;
2401 long long uid;
2402
2403 errno = 0;
2404 uid = strtol(owner_uid, &end, 10);
2405 if (end == owner_uid || *end != '\0' || errno != 0) {
2406 ERR("Failed to parse `%s` as a user id.", owner_uid);
2407 goto error;
2408 }
2409
2410 trigger_status = lttng_trigger_set_owner_uid(trigger, uid);
2411 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2412 ERR("Failed to set trigger's user identity.");
2413 goto error;
2414 }
2415 }
2416
2417 if (name) {
2418 ret_code = lttng_register_trigger_with_name(trigger, name);
2419 } else {
2420 ret_code = lttng_register_trigger_with_automatic_name(trigger);
2421 }
2422
2423 if (ret_code != LTTNG_OK) {
2424 ERR("Failed to register trigger: %s.",
2425 lttng_strerror(-ret_code));
2426 goto error;
2427 }
2428
2429 if (lttng_opt_mi) {
2430 ret_code = lttng_trigger_mi_serialize(trigger, mi_writer, NULL);
2431 if (ret_code != LTTNG_OK) {
2432 goto error;
2433 }
2434 } else {
2435 const char *returned_trigger_name;
2436 const enum lttng_trigger_status trigger_status =
2437 lttng_trigger_get_name(trigger,
2438 &returned_trigger_name);
2439
2440 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2441 WARN("Failed to retrieve the added trigger's name.");
2442 } else {
2443 MSG("Added trigger `%s`.", returned_trigger_name);
2444 }
2445 }
2446
2447 ret = 0;
2448
2449 goto end;
2450
2451 error:
2452 ret = 1;
2453
2454 end:
2455 /* Mi closing. */
2456 if (lttng_opt_mi && mi_writer) {
2457 int mi_ret;
2458
2459 /* Close output element. */
2460 mi_ret = mi_lttng_writer_close_element(mi_writer);
2461 if (mi_ret) {
2462 ret = 1;
2463 goto cleanup;
2464 }
2465
2466 mi_ret = mi_lttng_writer_write_element_bool(mi_writer,
2467 mi_lttng_element_command_success, ret ? 0 : 1);
2468 if (mi_ret) {
2469 ret = 1;
2470 goto cleanup;
2471 }
2472
2473 /* Command element close. */
2474 mi_ret = mi_lttng_writer_command_close(mi_writer);
2475 if (mi_ret) {
2476 ret = 1;
2477 goto cleanup;
2478 }
2479 }
2480
2481 cleanup:
2482 argpar_error_destroy(argpar_error);
2483 argpar_iter_destroy(argpar_iter);
2484 argpar_item_destroy(argpar_item);
2485 lttng_dynamic_pointer_array_reset(&actions);
2486 lttng_condition_destroy(condition);
2487 lttng_action_destroy(action_list);
2488 lttng_action_destroy(action);
2489 lttng_trigger_destroy(trigger);
2490 free(name);
2491 free(owner_uid);
2492 if (mi_writer && mi_lttng_writer_destroy(mi_writer)) {
2493 /* Preserve original error code. */
2494 ret = ret ? ret : CMD_ERROR;
2495 }
2496
2497 return ret;
2498 }
This page took 0.13177 seconds and 4 git commands to generate.