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