2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "../command.h"
12 #include "../loglevel.h"
13 #include "../uprobe.h"
15 #include "common/argpar/argpar.h"
16 #include "common/dynamic-array.h"
17 #include "common/string-utils/string-utils.h"
18 #include "common/utils.h"
19 /* For lttng_event_rule_type_str(). */
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/lttng.h>
22 #include "common/filter/filter-ast.h"
23 #include "common/filter/filter-ir.h"
24 #include "common/dynamic-array.h"
26 #if (LTTNG_SYMBOL_NAME_LEN == 256)
27 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg
[] =
32 #include <lttng-add-trigger.1.h>
70 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
71 { OPT_FILTER
, 'f', "filter", true },
72 { OPT_NAME
, 'n', "name", true },
73 { OPT_EXCLUDE_NAMES
, 'x', "exclude-names", true },
74 { OPT_LOGLEVEL
, '\0', "loglevel", true },
75 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
76 { OPT_EVENT_NAME
, 'E', "event-name", true },
78 { OPT_DOMAIN
, 'd', "domain", true },
80 /* Event rule types */
81 { OPT_FUNCTION
, '\0', "function", true },
82 { OPT_PROBE
, '\0', "probe", true },
83 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
84 { OPT_SYSCALL
, '\0', "syscall" },
85 { OPT_TRACEPOINT
, '\0', "tracepoint" },
87 /* Capture descriptor */
88 { OPT_CAPTURE
, '\0', "capture", true },
90 ARGPAR_OPT_DESCR_SENTINEL
94 bool assign_domain_type(enum lttng_domain_type
*dest
, const char *arg
)
98 if (*dest
!= LTTNG_DOMAIN_NONE
) {
99 ERR("More than one `--domain` was specified.");
103 if (strcmp(arg
, "kernel") == 0) {
104 *dest
= LTTNG_DOMAIN_KERNEL
;
105 } else if (strcmp(arg
, "user") == 0 || strcmp(arg
, "userspace") == 0) {
106 *dest
= LTTNG_DOMAIN_UST
;
107 } else if (strcmp(arg
, "jul") == 0) {
108 *dest
= LTTNG_DOMAIN_JUL
;
109 } else if (strcmp(arg
, "log4j") == 0) {
110 *dest
= LTTNG_DOMAIN_LOG4J
;
111 } else if (strcmp(arg
, "python") == 0) {
112 *dest
= LTTNG_DOMAIN_PYTHON
;
114 ERR("Invalid `--domain` value: %s", arg
);
129 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
130 enum lttng_event_rule_type src
)
134 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
138 ERR("Multiple event types specified.");
146 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
151 ERR("Duplicate '%s' given.", opt_name
);
157 PERROR("Failed to allocate string '%s'.", opt_name
);
171 /* This is defined in enable_events.c. */
173 int create_exclusion_list_and_validate(const char *event_name
,
174 const char *exclusions_arg
,
175 char ***exclusion_list
);
178 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
179 * is not recognized as a valid log level.
182 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
184 switch (domain_type
) {
185 case LTTNG_DOMAIN_UST
:
187 enum lttng_loglevel loglevel
;
188 const int ret
= loglevel_name_to_value(str
, &loglevel
);
190 return ret
== -1 ? ret
: (int) loglevel
;
192 case LTTNG_DOMAIN_LOG4J
:
194 enum lttng_loglevel_log4j loglevel
;
195 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
197 return ret
== -1 ? ret
: (int) loglevel
;
199 case LTTNG_DOMAIN_JUL
:
201 enum lttng_loglevel_jul loglevel
;
202 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
204 return ret
== -1 ? ret
: (int) loglevel
;
206 case LTTNG_DOMAIN_PYTHON
:
208 enum lttng_loglevel_python loglevel
;
209 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
211 return ret
== -1 ? ret
: (int) loglevel
;
214 /* Invalid domain type. */
219 static int parse_kernel_probe_opts(const char *source
,
220 struct lttng_kernel_probe_location
**location
)
225 char name
[LTTNG_SYMBOL_NAME_LEN
];
226 char *symbol_name
= NULL
;
229 /* Check for symbol+offset. */
230 match
= sscanf(source
,
231 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
235 if (*s_hex
== '\0') {
236 ERR("Kernel probe symbol offset is missing.");
240 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
242 PERROR("Failed to copy kernel probe location symbol name.");
245 offset
= strtoul(s_hex
, NULL
, 0);
247 *location
= lttng_kernel_probe_location_symbol_create(
248 symbol_name
, offset
);
250 ERR("Failed to create symbol kernel probe location.");
257 /* Check for symbol. */
258 if (isalpha(name
[0]) || name
[0] == '_') {
259 match
= sscanf(source
,
260 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
264 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
266 ERR("Failed to copy kernel probe location symbol name.");
270 *location
= lttng_kernel_probe_location_symbol_create(
273 ERR("Failed to create symbol kernel probe location.");
281 /* Check for address. */
282 match
= sscanf(source
, "%18s", s_hex
);
286 if (*s_hex
== '\0') {
287 ERR("Invalid kernel probe location address.");
291 address
= strtoul(s_hex
, NULL
, 0);
292 *location
= lttng_kernel_probe_location_address_create(address
);
294 ERR("Failed to create symbol kernel probe location.");
312 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
313 const struct ir_load_expression
*load_expr
,
314 const char *capture_str
)
316 char *provider_name
= NULL
;
317 struct lttng_event_expr
*event_expr
= NULL
;
318 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
319 const enum ir_load_expression_type load_expr_child_type
=
322 switch (load_expr_child_type
) {
323 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
324 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
326 const char *field_name
;
328 load_expr_op
= load_expr_op
->next
;
329 assert(load_expr_op
);
330 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
331 field_name
= load_expr_op
->u
.symbol
;
334 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
335 lttng_event_expr_event_payload_field_create(field_name
) :
336 lttng_event_expr_channel_context_field_create(field_name
);
338 ERR("Failed to create %s event expression: field name = `%s`.",
339 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
340 "payload field" : "channel context",
347 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
350 const char *type_name
;
351 const char *field_name
;
353 load_expr_op
= load_expr_op
->next
;
354 assert(load_expr_op
);
355 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
356 field_name
= load_expr_op
->u
.symbol
;
360 * The field name needs to be of the form PROVIDER:TYPE. We
363 colon
= strchr(field_name
, ':');
365 ERR("Invalid app-specific context field name: missing colon in `%s`.",
370 type_name
= colon
+ 1;
371 if (*type_name
== '\0') {
372 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
377 provider_name
= strndup(field_name
, colon
- field_name
);
378 if (!provider_name
) {
379 PERROR("Failed to allocate field name string");
383 event_expr
= lttng_event_expr_app_specific_context_field_create(
384 provider_name
, type_name
);
386 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
387 provider_name
, type_name
);
394 ERR("%s: unexpected load expr type %d.", __func__
,
399 load_expr_op
= load_expr_op
->next
;
401 /* There may be a single array index after that. */
402 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
403 struct lttng_event_expr
*index_event_expr
;
404 const uint64_t index
= load_expr_op
->u
.index
;
406 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
407 if (!index_event_expr
) {
408 ERR("Failed to create array field element event expression.");
412 event_expr
= index_event_expr
;
413 load_expr_op
= load_expr_op
->next
;
416 switch (load_expr_op
->type
) {
417 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
419 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
420 * always found at the end of the chain.
423 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
424 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
429 ERR("%s: unexpected load expression operator %s.", __func__
,
430 ir_load_expression_type_str(load_expr_op
->type
));
437 lttng_event_expr_destroy(event_expr
);
447 struct lttng_event_expr
*ir_op_load_to_event_expr(
448 const struct ir_op
*ir
, const char *capture_str
)
450 struct lttng_event_expr
*event_expr
= NULL
;
452 assert(ir
->op
== IR_OP_LOAD
);
454 switch (ir
->data_type
) {
455 case IR_DATA_EXPRESSION
:
457 const struct ir_load_expression
*ir_load_expr
=
458 ir
->u
.load
.u
.expression
;
460 event_expr
= ir_op_load_expr_to_event_expr(
461 ir_load_expr
, capture_str
);
465 ERR("%s: unexpected data type: %s.", __func__
,
466 ir_data_type_str(ir
->data_type
));
474 const char *ir_operator_type_human_str(enum ir_op_type op
)
496 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
497 const char *capture_str
)
499 struct lttng_event_expr
*event_expr
= NULL
;
501 assert(ir
->op
== IR_OP_ROOT
);
502 ir
= ir
->u
.root
.child
;
506 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
511 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
513 ir_operator_type_human_str(ir
->op
));
516 ERR("%s: unexpected IR op type: %s.", __func__
,
517 ir_op_type_str(ir
->op
));
525 void destroy_event_expr(void *ptr
)
527 lttng_event_expr_destroy(ptr
);
530 struct parse_event_rule_res
{
532 struct lttng_event_rule
*er
;
534 /* Array of `struct lttng_event_expr *` */
535 struct lttng_dynamic_pointer_array capture_descriptors
;
539 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
541 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
542 enum lttng_event_rule_type event_rule_type
=
543 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
544 struct argpar_state
*state
;
545 struct argpar_item
*item
= NULL
;
547 int consumed_args
= -1;
548 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
549 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
550 struct parse_event_rule_res res
= { 0 };
551 struct lttng_event_expr
*event_expr
= NULL
;
552 struct filter_parser_ctx
*parser_ctx
= NULL
;
553 struct lttng_log_level_rule
*log_level_rule
= NULL
;
555 /* Tracepoint and syscall options. */
557 char *exclude_names
= NULL
;
558 char **exclusion_list
= NULL
;
560 /* Holds the argument of --probe / --userspace-probe. */
561 char *probe_source
= NULL
;
562 char *probe_event_name
= NULL
;
568 char *loglevel_str
= NULL
;
569 bool loglevel_only
= false;
571 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
573 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
575 ERR("Failed to allocate an argpar state.");
580 enum argpar_state_parse_next_status status
;
582 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
583 status
= argpar_state_parse_next(state
, &item
, &error
);
584 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
587 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
588 /* Just stop parsing here. */
590 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
594 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
596 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
597 const struct argpar_item_opt
*item_opt
=
598 (const struct argpar_item_opt
*) item
;
600 switch (item_opt
->descr
->id
) {
603 if (!assign_domain_type(&domain_type
, item_opt
->arg
)) {
609 /* Event rule types */
611 if (!assign_event_rule_type(&event_rule_type
,
612 LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
)) {
618 if (!assign_event_rule_type(&event_rule_type
,
619 LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
)) {
623 if (!assign_string(&probe_source
, item_opt
->arg
,
629 case OPT_USERSPACE_PROBE
:
630 if (!assign_event_rule_type(&event_rule_type
,
631 LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
)) {
635 if (!assign_string(&probe_source
, item_opt
->arg
,
636 "--userspace-probe")) {
642 if (!assign_string(&probe_event_name
,
644 "--event-name/-E")) {
650 if (!assign_event_rule_type(&event_rule_type
,
651 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
657 if (!assign_event_rule_type(&event_rule_type
,
658 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
664 if (!assign_string(&filter
, item_opt
->arg
,
671 if (!assign_string(&name
, item_opt
->arg
,
677 case OPT_EXCLUDE_NAMES
:
678 if (!assign_string(&exclude_names
,
680 "--exclude-names/-x")) {
686 case OPT_LOGLEVEL_ONLY
:
687 if (!assign_string(&loglevel_str
, item_opt
->arg
,
688 "--loglevel/--loglevel-only")) {
692 loglevel_only
= item_opt
->descr
->id
==
698 const char *capture_str
= item_opt
->arg
;
700 ret
= filter_parser_ctx_create_from_filter_expression(
701 capture_str
, &parser_ctx
);
703 ERR("Failed to parse capture expression `%s`.",
708 event_expr
= ir_op_root_to_event_expr(
713 * ir_op_root_to_event_expr has printed
719 ret
= lttng_dynamic_pointer_array_add_pointer(
720 &res
.capture_descriptors
,
727 * The ownership of event expression was
728 * transferred to the dynamic array.
738 const struct argpar_item_non_opt
*item_non_opt
=
739 (const struct argpar_item_non_opt
*)
742 /* Don't accept non-option arguments. */
743 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
748 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
749 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
753 * Option --name is applicable to event rules of type tracepoint
754 * and syscall. For tracepoint and syscall rules, if --name is
755 * omitted, it is implicitly "*".
757 switch (event_rule_type
) {
758 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
759 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
767 ERR("Can't use --name with %s event rules.",
768 lttng_event_rule_type_str(
774 ERR("Can't use --exclude-names/-x with %s event rules.",
775 lttng_event_rule_type_str(
782 * Option --event-name is only applicable to event rules of type probe.
783 * If omitted, it defaults to the probe location.
785 switch (event_rule_type
) {
786 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
787 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
788 if (!probe_event_name
) {
789 probe_event_name
= strdup(probe_source
);
795 if (probe_event_name
) {
796 ERR("Can't use --event-name with %s event rules.",
797 lttng_event_rule_type_str(
804 * Update *argc and *argv so our caller can keep parsing what follows.
806 consumed_args
= argpar_state_get_ingested_orig_args(state
);
807 assert(consumed_args
>= 0);
808 *argc
-= consumed_args
;
809 *argv
+= consumed_args
;
811 /* Need to specify a domain. */
812 if (domain_type
== LTTNG_DOMAIN_NONE
) {
813 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
817 /* Validate event rule type against domain. */
818 switch (event_rule_type
) {
819 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
820 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
821 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
822 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
823 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
824 ERR("Event type not available for user-space tracing.");
829 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
837 * Adding a filter to a probe, function or userspace-probe would be
838 * denied by the kernel tracer as it's not supported at the moment. We
839 * do an early check here to warn the user.
841 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
842 switch (event_rule_type
) {
843 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
844 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
847 ERR("Filter expressions are not supported for %s event rules.",
848 lttng_event_rule_type_str(event_rule_type
));
853 /* If --exclude/-x was passed, split it into an exclusion list. */
855 if (domain_type
!= LTTNG_DOMAIN_UST
) {
856 ERR("Event name exclusions are not yet implemented for %s event rules.",
857 get_domain_str(domain_type
));
861 if (create_exclusion_list_and_validate(name
,
862 exclude_names
, &exclusion_list
) != 0) {
863 ERR("Failed to create exclusion list.");
868 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
869 ERR("Log levels are only applicable to tracepoint event rules.");
873 /* Finally, create the event rule object. */
874 switch (event_rule_type
) {
875 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
877 enum lttng_event_rule_status event_rule_status
;
879 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
881 ERR("Failed to create tracepoint event rule.");
886 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
888 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
889 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
896 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
898 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
899 ERR("Failed to set tracepoint event rule's filter to '%s'.",
905 /* Set exclusion list. */
906 if (exclusion_list
) {
909 for (n
= 0; exclusion_list
[n
]; n
++) {
910 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
913 if (event_rule_status
!=
914 LTTNG_EVENT_RULE_STATUS_OK
) {
915 ERR("Failed to set tracepoint exclusion list element '%s'",
925 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
926 ERR("Log levels are not supported by the kernel tracer.");
930 loglevel
= parse_loglevel_string(
931 loglevel_str
, domain_type
);
933 ERR("Failed to parse `%s` as a log level.",
939 log_level_rule
= lttng_log_level_rule_exactly_create(loglevel
);
941 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(loglevel
);
944 if (log_level_rule
== NULL
) {
945 ERR("Failed to create log level rule object.");
950 lttng_event_rule_tracepoint_set_log_level_rule(
951 res
.er
, log_level_rule
);
953 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
954 ERR("Failed to set log level on event fule.");
961 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
964 enum lttng_event_rule_status event_rule_status
;
966 ret
= parse_kernel_probe_opts(
967 probe_source
, &kernel_probe_location
);
969 ERR("Failed to parse kernel probe location.");
973 assert(kernel_probe_location
);
974 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
976 ERR("Failed to create kprobe event rule.");
981 lttng_event_rule_kernel_probe_set_event_name(
982 res
.er
, probe_event_name
);
983 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
984 ERR("Failed to set kprobe event rule's name to '%s'.",
991 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
994 enum lttng_event_rule_status event_rule_status
;
996 ret
= parse_userspace_probe_opts(
997 probe_source
, &userspace_probe_location
);
999 ERR("Failed to parse user space probe location.");
1003 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1005 ERR("Failed to create userspace probe event rule.");
1010 lttng_event_rule_userspace_probe_set_event_name(
1011 res
.er
, probe_event_name
);
1012 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1013 ERR("Failed to set user space probe event rule's name to '%s'.",
1020 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1022 enum lttng_event_rule_status event_rule_status
;
1024 res
.er
= lttng_event_rule_syscall_create();
1026 ERR("Failed to create syscall event rule.");
1030 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1032 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1033 ERR("Failed to set syscall event rule's pattern to '%s'.",
1039 event_rule_status
= lttng_event_rule_syscall_set_filter(
1041 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1042 ERR("Failed to set syscall event rule's filter to '%s'.",
1058 lttng_event_rule_destroy(res
.er
);
1060 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1064 filter_parser_ctx_free(parser_ctx
);
1067 lttng_event_expr_destroy(event_expr
);
1068 argpar_item_destroy(item
);
1070 argpar_state_destroy(state
);
1073 free(exclude_names
);
1076 free(probe_event_name
);
1078 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1079 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1080 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1081 lttng_log_level_rule_destroy(log_level_rule
);
1086 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1088 struct parse_event_rule_res res
;
1089 struct lttng_condition
*c
;
1092 res
= parse_event_rule(argc
, argv
);
1098 c
= lttng_condition_on_event_create(res
.er
);
1099 lttng_event_rule_destroy(res
.er
);
1105 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1107 enum lttng_condition_status status
;
1108 struct lttng_event_expr
**expr
=
1109 lttng_dynamic_array_get_element(
1110 &res
.capture_descriptors
.array
, i
);
1114 status
= lttng_condition_on_event_append_capture_descriptor(
1116 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1117 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1118 ERR("The capture feature is unsupported by the event-rule condition type");
1124 /* Ownership of event expression moved to `c` */
1131 lttng_condition_destroy(c
);
1135 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1136 lttng_event_rule_destroy(res
.er
);
1141 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1143 struct lttng_condition
*cond
= NULL
;
1144 struct argpar_state
*state
= NULL
;
1145 struct argpar_item
*item
= NULL
;
1146 const char *threshold_arg
= NULL
;
1147 const char *session_name_arg
= NULL
;
1150 enum lttng_condition_status condition_status
;
1152 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1154 ERR("Failed to allocate an argpar state.");
1159 enum argpar_state_parse_next_status status
;
1161 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1162 status
= argpar_state_parse_next(state
, &item
, &error
);
1163 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1166 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1167 /* Just stop parsing here. */
1169 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1173 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1175 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1176 const struct argpar_item_opt
*item_opt
=
1177 (const struct argpar_item_opt
*) item
;
1179 switch (item_opt
->descr
->id
) {
1184 const struct argpar_item_non_opt
*item_non_opt
;
1186 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1188 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1190 switch (item_non_opt
->non_opt_index
) {
1192 session_name_arg
= item_non_opt
->arg
;
1195 threshold_arg
= item_non_opt
->arg
;
1198 ERR("Unexpected argument `%s`.",
1205 *argc
-= argpar_state_get_ingested_orig_args(state
);
1206 *argv
+= argpar_state_get_ingested_orig_args(state
);
1208 if (!session_name_arg
) {
1209 ERR("Missing session name argument.");
1213 if (!threshold_arg
) {
1214 ERR("Missing threshold argument.");
1218 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1219 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1223 cond
= lttng_condition_session_consumed_size_create();
1225 ERR("Failed to allocate a session consumed size condition.");
1229 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1230 cond
, session_name_arg
);
1231 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1232 ERR("Failed to set session consumed size condition's session name to '%s'.",
1237 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1239 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1240 ERR("Failed to set session consumed size condition threshold.");
1247 lttng_condition_destroy(cond
);
1251 argpar_state_destroy(state
);
1252 argpar_item_destroy(item
);
1258 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1260 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1265 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1267 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1272 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1274 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1279 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1281 ERR("Session rotation completed conditions are unsupported for the moment.");
1285 struct condition_descr
{
1287 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1291 struct condition_descr condition_descrs
[] = {
1292 { "event-rule-matches", handle_condition_event
},
1293 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1294 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1295 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1296 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1297 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1301 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1305 struct lttng_condition
*cond
;
1306 const struct condition_descr
*descr
= NULL
;
1308 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1309 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1310 descr
= &condition_descrs
[i
];
1316 ERR("Unknown condition name '%s'", condition_name
);
1320 cond
= descr
->handler(argc
, argv
);
1322 /* The handler has already printed an error message. */
1333 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1336 char **tokens
= NULL
;
1337 struct lttng_rate_policy
*policy
= NULL
;
1338 enum lttng_rate_policy_type policy_type
;
1339 unsigned long long value
;
1340 char *policy_type_str
;
1341 char *policy_value_str
;
1346 * rate policy fields are separated by ':'.
1348 tokens
= strutils_split(policy_str
, ':', 1);
1349 num_token
= strutils_array_of_strings_len(tokens
);
1352 * Early sanity check that the number of parameter is exactly 2.
1355 if (num_token
!= 2) {
1356 ERR("Rate policy format is invalid.");
1360 policy_type_str
= tokens
[0];
1361 policy_value_str
= tokens
[1];
1363 /* Parse the type. */
1364 if (strcmp(policy_type_str
, "once-after") == 0) {
1365 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1366 } else if (strcmp(policy_type_str
, "every") == 0) {
1367 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1369 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1373 /* Parse the value. */
1374 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1375 ERR("Failed to parse rate policy value `%s` as an integer.",
1381 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1385 switch (policy_type
) {
1386 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1387 policy
= lttng_rate_policy_every_n_create(value
);
1389 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1390 policy
= lttng_rate_policy_once_after_n_create(value
);
1396 if (policy
== NULL
) {
1397 ERR("Failed to create rate policy `%s`.", policy_str
);
1401 strutils_free_null_terminated_array_of_strings(tokens
);
1405 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1406 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1407 ARGPAR_OPT_DESCR_SENTINEL
1411 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1413 struct lttng_action
*action
= NULL
;
1414 struct argpar_state
*state
= NULL
;
1415 struct argpar_item
*item
= NULL
;
1417 struct lttng_rate_policy
*policy
= NULL
;
1419 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1421 ERR("Failed to allocate an argpar state.");
1426 enum argpar_state_parse_next_status status
;
1428 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1429 status
= argpar_state_parse_next(state
, &item
, &error
);
1430 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1433 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1434 /* Just stop parsing here. */
1436 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1440 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1442 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1443 const struct argpar_item_opt
*item_opt
=
1444 (const struct argpar_item_opt
*) item
;
1446 switch (item_opt
->descr
->id
) {
1447 case OPT_RATE_POLICY
:
1449 policy
= parse_rate_policy(item_opt
->arg
);
1459 const struct argpar_item_non_opt
*item_non_opt
;
1461 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1463 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1465 switch (item_non_opt
->non_opt_index
) {
1467 ERR("Unexpected argument `%s`.",
1474 *argc
-= argpar_state_get_ingested_orig_args(state
);
1475 *argv
+= argpar_state_get_ingested_orig_args(state
);
1477 action
= lttng_action_notify_create();
1479 ERR("Failed to create notify action");
1484 enum lttng_action_status status
;
1485 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1486 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1487 ERR("Failed to set rate policy");
1495 lttng_action_destroy(action
);
1499 lttng_rate_policy_destroy(policy
);
1500 argpar_state_destroy(state
);
1501 argpar_item_destroy(item
);
1506 * Generic handler for a kind of action that takes a session name and an
1507 * optional rate policy.
1510 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1512 struct lttng_action
*(*create_action_cb
)(void),
1513 enum lttng_action_status (*set_session_name_cb
)(
1514 struct lttng_action
*, const char *),
1515 enum lttng_action_status (*set_rate_policy_cb
)(
1516 struct lttng_action
*,
1517 const struct lttng_rate_policy
*),
1518 const char *action_name
)
1520 struct lttng_action
*action
= NULL
;
1521 struct argpar_state
*state
= NULL
;
1522 struct argpar_item
*item
= NULL
;
1523 const char *session_name_arg
= NULL
;
1525 enum lttng_action_status action_status
;
1526 struct lttng_rate_policy
*policy
= NULL
;
1528 assert(set_session_name_cb
);
1529 assert(set_rate_policy_cb
);
1531 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1532 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1533 ARGPAR_OPT_DESCR_SENTINEL
1536 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1538 ERR("Failed to allocate an argpar state.");
1543 enum argpar_state_parse_next_status status
;
1545 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1546 status
= argpar_state_parse_next(state
, &item
, &error
);
1547 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1550 } else if (status
==
1551 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1552 /* Just stop parsing here. */
1554 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1558 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1559 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1560 const struct argpar_item_opt
*item_opt
=
1561 (const struct argpar_item_opt
*) item
;
1563 switch (item_opt
->descr
->id
) {
1564 case OPT_RATE_POLICY
:
1566 policy
= parse_rate_policy(item_opt
->arg
);
1576 const struct argpar_item_non_opt
*item_non_opt
;
1577 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1579 switch (item_non_opt
->non_opt_index
) {
1581 session_name_arg
= item_non_opt
->arg
;
1584 ERR("Unexpected argument `%s`.",
1591 *argc
-= argpar_state_get_ingested_orig_args(state
);
1592 *argv
+= argpar_state_get_ingested_orig_args(state
);
1594 if (!session_name_arg
) {
1595 ERR("Missing session name.");
1599 action
= create_action_cb();
1601 ERR("Failed to allocate %s session action.", action_name
);
1605 action_status
= set_session_name_cb(action
, session_name_arg
);
1606 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1607 ERR("Failed to set action %s session's session name to '%s'.",
1608 action_name
, session_name_arg
);
1613 action_status
= set_rate_policy_cb(action
, policy
);
1614 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1615 ERR("Failed to set rate policy");
1623 lttng_action_destroy(action
);
1625 argpar_item_destroy(item
);
1627 lttng_rate_policy_destroy(policy
);
1629 argpar_state_destroy(state
);
1634 struct lttng_action
*handle_action_start_session(int *argc
,
1637 return handle_action_simple_session_with_policy(argc
, argv
,
1638 lttng_action_start_session_create
,
1639 lttng_action_start_session_set_session_name
,
1640 lttng_action_start_session_set_rate_policy
, "start");
1644 struct lttng_action
*handle_action_stop_session(int *argc
,
1647 return handle_action_simple_session_with_policy(argc
, argv
,
1648 lttng_action_stop_session_create
,
1649 lttng_action_stop_session_set_session_name
,
1650 lttng_action_stop_session_set_rate_policy
, "stop");
1654 struct lttng_action
*handle_action_rotate_session(int *argc
,
1657 return handle_action_simple_session_with_policy(argc
, argv
,
1658 lttng_action_rotate_session_create
,
1659 lttng_action_rotate_session_set_session_name
,
1660 lttng_action_rotate_session_set_rate_policy
,
1664 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1665 { OPT_NAME
, 'n', "name", true },
1666 { OPT_MAX_SIZE
, 'm', "max-size", true },
1667 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1668 { OPT_DATA_URL
, '\0', "data-url", true },
1669 { OPT_URL
, '\0', "url", true },
1670 { OPT_PATH
, '\0', "path", true },
1671 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1672 ARGPAR_OPT_DESCR_SENTINEL
1676 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1679 struct lttng_action
*action
= NULL
;
1680 struct argpar_state
*state
= NULL
;
1681 struct argpar_item
*item
= NULL
;
1682 const char *session_name_arg
= NULL
;
1683 char *snapshot_name_arg
= NULL
;
1684 char *ctrl_url_arg
= NULL
;
1685 char *data_url_arg
= NULL
;
1686 char *max_size_arg
= NULL
;
1687 char *url_arg
= NULL
;
1688 char *path_arg
= NULL
;
1690 enum lttng_action_status action_status
;
1691 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1692 struct lttng_rate_policy
*policy
= NULL
;
1694 unsigned int locations_specified
= 0;
1696 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1698 ERR("Failed to allocate an argpar state.");
1703 enum argpar_state_parse_next_status status
;
1705 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1706 status
= argpar_state_parse_next(state
, &item
, &error
);
1707 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1710 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1711 /* Just stop parsing here. */
1713 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1717 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1719 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1720 const struct argpar_item_opt
*item_opt
=
1721 (const struct argpar_item_opt
*) item
;
1723 switch (item_opt
->descr
->id
) {
1725 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1731 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1737 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1743 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1749 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1755 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1760 case OPT_RATE_POLICY
:
1762 policy
= parse_rate_policy(item_opt
->arg
);
1772 const struct argpar_item_non_opt
*item_non_opt
;
1774 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1776 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1778 switch (item_non_opt
->non_opt_index
) {
1780 session_name_arg
= item_non_opt
->arg
;
1783 ERR("Unexpected argument `%s`.",
1790 *argc
-= argpar_state_get_ingested_orig_args(state
);
1791 *argv
+= argpar_state_get_ingested_orig_args(state
);
1793 if (!session_name_arg
) {
1794 ERR("Missing session name.");
1798 /* --ctrl-url and --data-url must come in pair. */
1799 if (ctrl_url_arg
&& !data_url_arg
) {
1800 ERR("--ctrl-url is specified, but --data-url is missing.");
1804 if (!ctrl_url_arg
&& data_url_arg
) {
1805 ERR("--data-url is specified, but --ctrl-url is missing.");
1809 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1810 locations_specified
+= !!url_arg
;
1811 locations_specified
+= !!path_arg
;
1813 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1814 if (locations_specified
> 1) {
1815 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1820 * Did the user specify an option that implies using a
1821 * custom/unregistered output?
1823 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1824 snapshot_output
= lttng_snapshot_output_create();
1825 if (!snapshot_output
) {
1826 ERR("Failed to allocate a snapshot output.");
1831 action
= lttng_action_snapshot_session_create();
1833 ERR("Failed to allocate snapshot session action.");
1837 action_status
= lttng_action_snapshot_session_set_session_name(
1838 action
, session_name_arg
);
1839 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1840 ERR("Failed to set action snapshot session's session name to '%s'.",
1845 if (snapshot_name_arg
) {
1846 if (!snapshot_output
) {
1847 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1851 ret
= lttng_snapshot_output_set_name(
1852 snapshot_name_arg
, snapshot_output
);
1854 ERR("Failed to set name of snapshot output.");
1862 if (!snapshot_output
) {
1863 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1867 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1869 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1873 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1875 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1883 struct lttng_uri
*uris
;
1885 if (!strstr(url_arg
, "://")) {
1886 ERR("Failed to parse '%s' as an URL.", url_arg
);
1890 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1892 ERR("Failed to parse '%s' as an URL.", url_arg
);
1896 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1897 ret
= lttng_snapshot_output_set_local_path(
1898 uris
[0].dst
.path
, snapshot_output
);
1901 ERR("Failed to assign '%s' as a local destination.",
1906 ret
= lttng_snapshot_output_set_network_url(
1907 url_arg
, snapshot_output
);
1910 ERR("Failed to assign '%s' as a network URL.",
1918 ret
= lttng_snapshot_output_set_local_path(
1919 path_arg
, snapshot_output
);
1921 ERR("Failed to parse '%s' as a local path.", path_arg
);
1928 * Two argument form, network output with separate control and
1931 ret
= lttng_snapshot_output_set_network_urls(
1932 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1934 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1935 ctrl_url_arg
, data_url_arg
);
1940 if (snapshot_output
) {
1941 action_status
= lttng_action_snapshot_session_set_output(
1942 action
, snapshot_output
);
1943 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1944 ERR("Failed to set snapshot session action's output.");
1948 /* Ownership of `snapshot_output` has been transferred to the action. */
1949 snapshot_output
= NULL
;
1953 enum lttng_action_status status
;
1954 status
= lttng_action_snapshot_session_set_rate_policy(
1956 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1957 ERR("Failed to set rate policy");
1965 lttng_action_destroy(action
);
1969 free(snapshot_name_arg
);
1974 free(snapshot_output
);
1976 lttng_rate_policy_destroy(policy
);
1977 argpar_state_destroy(state
);
1978 argpar_item_destroy(item
);
1982 struct action_descr
{
1984 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1988 struct action_descr action_descrs
[] = {
1989 { "notify", handle_action_notify
},
1990 { "start-session", handle_action_start_session
},
1991 { "stop-session", handle_action_stop_session
},
1992 { "rotate-session", handle_action_rotate_session
},
1993 { "snapshot-session", handle_action_snapshot_session
},
1997 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2000 struct lttng_action
*action
;
2001 const struct action_descr
*descr
= NULL
;
2003 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2004 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2005 descr
= &action_descrs
[i
];
2011 ERR("Unknown action name: %s", action_name
);
2015 action
= descr
->handler(argc
, argv
);
2017 /* The handler has already printed an error message. */
2029 struct argpar_opt_descr add_trigger_options
[] = {
2030 { OPT_HELP
, 'h', "help", false },
2031 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2032 { OPT_CONDITION
, '\0', "condition", true },
2033 { OPT_ACTION
, '\0', "action", true },
2034 { OPT_NAME
, '\0', "name", true },
2035 { OPT_USER_ID
, '\0', "user-id", true },
2036 ARGPAR_OPT_DESCR_SENTINEL
,
2040 void lttng_actions_destructor(void *p
)
2042 struct lttng_action
*action
= p
;
2044 lttng_action_destroy(action
);
2047 int cmd_add_trigger(int argc
, const char **argv
)
2050 int my_argc
= argc
- 1;
2051 const char **my_argv
= argv
+ 1;
2052 struct lttng_condition
*condition
= NULL
;
2053 struct lttng_dynamic_pointer_array actions
;
2054 struct argpar_state
*argpar_state
= NULL
;
2055 struct argpar_item
*argpar_item
= NULL
;
2056 struct lttng_action
*action_group
= NULL
;
2057 struct lttng_action
*action
= NULL
;
2058 struct lttng_trigger
*trigger
= NULL
;
2062 char *user_id
= NULL
;
2064 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2067 enum argpar_state_parse_next_status status
;
2068 const struct argpar_item_opt
*item_opt
;
2071 argpar_state_destroy(argpar_state
);
2072 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2073 add_trigger_options
);
2074 if (!argpar_state
) {
2075 ERR("Failed to create argpar state.");
2079 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2080 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2081 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2084 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2087 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2091 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2093 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2094 const struct argpar_item_non_opt
*item_non_opt
=
2095 (const struct argpar_item_non_opt
*)
2098 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2102 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2104 ingested_args
= argpar_state_get_ingested_orig_args(
2107 my_argc
-= ingested_args
;
2108 my_argv
+= ingested_args
;
2110 switch (item_opt
->descr
->id
) {
2115 case OPT_LIST_OPTIONS
:
2116 list_cmd_options_argpar(stdout
, add_trigger_options
);
2122 ERR("A --condition was already given.");
2126 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2129 * An error message was already printed by
2139 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2142 * An error message was already printed by
2148 ret
= lttng_dynamic_pointer_array_add_pointer(
2151 ERR("Failed to add pointer to pointer array.");
2155 /* Ownership of the action was transferred to the group. */
2162 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2170 if (!assign_string(&user_id
, item_opt
->arg
,
2183 ERR("Missing --condition.");
2187 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2188 ERR("Need at least one --action.");
2192 action_group
= lttng_action_group_create();
2193 if (!action_group
) {
2197 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2198 enum lttng_action_status status
;
2200 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2202 status
= lttng_action_group_add_action(action_group
, action
);
2203 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2208 * The `lttng_action_group_add_action()` takes a reference to
2209 * the action. We can destroy ours.
2211 lttng_action_destroy(action
);
2215 trigger
= lttng_trigger_create(condition
, action_group
);
2221 enum lttng_trigger_status trigger_status
=
2222 lttng_trigger_set_name(trigger
, name
);
2224 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2225 ERR("Failed to set trigger name.");
2231 enum lttng_trigger_status trigger_status
;
2236 uid
= strtol(user_id
, &end
, 10);
2237 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
2238 ERR("Failed to parse `%s` as a user id.", user_id
);
2241 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2242 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2243 ERR("Failed to set trigger's user identity.");
2248 ret
= lttng_register_trigger(trigger
);
2250 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2254 MSG("Trigger registered successfully.");
2262 argpar_state_destroy(argpar_state
);
2263 argpar_item_destroy(argpar_item
);
2264 lttng_dynamic_pointer_array_reset(&actions
);
2265 lttng_condition_destroy(condition
);
2266 lttng_action_destroy(action_group
);
2267 lttng_action_destroy(action
);
2268 lttng_trigger_destroy(trigger
);