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