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>
74 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
75 { OPT_ALL
, 'a', "all", false },
76 { OPT_FILTER
, 'f', "filter", true },
77 { OPT_EXCLUDE
, 'x', "exclude", true },
78 { OPT_LOGLEVEL
, '\0', "loglevel", true },
79 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
82 { OPT_USERSPACE
, 'u', "userspace", false },
83 { OPT_KERNEL
, 'k', "kernel", false },
84 { OPT_LOG4J
, 'l', "log4j", false },
85 { OPT_JUL
, 'j', "jul", false },
86 { OPT_PYTHON
, 'p', "python", false },
88 /* Event rule types */
89 { OPT_FUNCTION
, '\0', "function", true },
90 { OPT_PROBE
, '\0', "probe", true },
91 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
92 { OPT_SYSCALL
, '\0', "syscall" },
93 { OPT_TRACEPOINT
, '\0', "tracepoint" },
95 /* Capture descriptor */
96 { OPT_CAPTURE
, '\0', "capture", true },
98 ARGPAR_OPT_DESCR_SENTINEL
102 bool assign_domain_type(enum lttng_domain_type
*dest
,
103 enum lttng_domain_type src
)
107 if (*dest
== LTTNG_DOMAIN_NONE
|| *dest
== src
) {
111 ERR("Multiple domains specified.");
119 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
120 enum lttng_event_rule_type src
)
124 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
128 ERR("Multiple event types specified.");
136 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
141 ERR("Duplicate '%s' given.", opt_name
);
147 PERROR("Failed to allocate string '%s'.", opt_name
);
161 /* This is defined in enable_events.c. */
163 int create_exclusion_list_and_validate(const char *event_name
,
164 const char *exclusions_arg
,
165 char ***exclusion_list
);
168 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
169 * is not recognized as a valid log level.
172 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
174 switch (domain_type
) {
175 case LTTNG_DOMAIN_UST
:
177 enum lttng_loglevel loglevel
;
178 const int ret
= loglevel_name_to_value(str
, &loglevel
);
180 return ret
== -1 ? ret
: (int) loglevel
;
182 case LTTNG_DOMAIN_LOG4J
:
184 enum lttng_loglevel_log4j loglevel
;
185 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
187 return ret
== -1 ? ret
: (int) loglevel
;
189 case LTTNG_DOMAIN_JUL
:
191 enum lttng_loglevel_jul loglevel
;
192 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
194 return ret
== -1 ? ret
: (int) loglevel
;
196 case LTTNG_DOMAIN_PYTHON
:
198 enum lttng_loglevel_python loglevel
;
199 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
201 return ret
== -1 ? ret
: (int) loglevel
;
204 /* Invalid domain type. */
209 static int parse_kernel_probe_opts(const char *source
,
210 struct lttng_kernel_probe_location
**location
)
215 char name
[LTTNG_SYMBOL_NAME_LEN
];
216 char *symbol_name
= NULL
;
219 /* Check for symbol+offset. */
220 match
= sscanf(source
,
221 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
225 if (*s_hex
== '\0') {
226 ERR("Kernel probe symbol offset is missing.");
230 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
232 PERROR("Failed to copy kernel probe location symbol name.");
235 offset
= strtoul(s_hex
, NULL
, 0);
237 *location
= lttng_kernel_probe_location_symbol_create(
238 symbol_name
, offset
);
240 ERR("Failed to create symbol kernel probe location.");
247 /* Check for symbol. */
248 if (isalpha(name
[0]) || name
[0] == '_') {
249 match
= sscanf(source
,
250 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
254 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
256 ERR("Failed to copy kernel probe location symbol name.");
260 *location
= lttng_kernel_probe_location_symbol_create(
263 ERR("Failed to create symbol kernel probe location.");
271 /* Check for address. */
272 match
= sscanf(source
, "%18s", s_hex
);
276 if (*s_hex
== '\0') {
277 ERR("Invalid kernel probe location address.");
281 address
= strtoul(s_hex
, NULL
, 0);
282 *location
= lttng_kernel_probe_location_address_create(address
);
284 ERR("Failed to create symbol kernel probe location.");
302 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
303 const struct ir_load_expression
*load_expr
,
304 const char *capture_str
)
306 char *provider_name
= NULL
;
307 struct lttng_event_expr
*event_expr
= NULL
;
308 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
309 const enum ir_load_expression_type load_expr_child_type
=
312 switch (load_expr_child_type
) {
313 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
314 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
316 const char *field_name
;
318 load_expr_op
= load_expr_op
->next
;
319 assert(load_expr_op
);
320 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
321 field_name
= load_expr_op
->u
.symbol
;
324 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
325 lttng_event_expr_event_payload_field_create(field_name
) :
326 lttng_event_expr_channel_context_field_create(field_name
);
328 ERR("Failed to create %s event expression: field name = `%s`.",
329 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
330 "payload field" : "channel context",
337 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
340 const char *type_name
;
341 const char *field_name
;
343 load_expr_op
= load_expr_op
->next
;
344 assert(load_expr_op
);
345 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
346 field_name
= load_expr_op
->u
.symbol
;
350 * The field name needs to be of the form PROVIDER:TYPE. We
353 colon
= strchr(field_name
, ':');
355 ERR("Invalid app-specific context field name: missing colon in `%s`.",
360 type_name
= colon
+ 1;
361 if (*type_name
== '\0') {
362 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
367 provider_name
= strndup(field_name
, colon
- field_name
);
368 if (!provider_name
) {
369 PERROR("Failed to allocate field name string");
373 event_expr
= lttng_event_expr_app_specific_context_field_create(
374 provider_name
, type_name
);
376 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
377 provider_name
, type_name
);
384 ERR("%s: unexpected load expr type %d.", __func__
,
389 load_expr_op
= load_expr_op
->next
;
391 /* There may be a single array index after that. */
392 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
393 struct lttng_event_expr
*index_event_expr
;
394 const uint64_t index
= load_expr_op
->u
.index
;
396 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
397 if (!index_event_expr
) {
398 ERR("Failed to create array field element event expression.");
402 event_expr
= index_event_expr
;
403 load_expr_op
= load_expr_op
->next
;
406 switch (load_expr_op
->type
) {
407 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
409 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
410 * always found at the end of the chain.
413 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
414 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
419 ERR("%s: unexpected load expression operator %s.", __func__
,
420 ir_load_expression_type_str(load_expr_op
->type
));
427 lttng_event_expr_destroy(event_expr
);
437 struct lttng_event_expr
*ir_op_load_to_event_expr(
438 const struct ir_op
*ir
, const char *capture_str
)
440 struct lttng_event_expr
*event_expr
= NULL
;
442 assert(ir
->op
== IR_OP_LOAD
);
444 switch (ir
->data_type
) {
445 case IR_DATA_EXPRESSION
:
447 const struct ir_load_expression
*ir_load_expr
=
448 ir
->u
.load
.u
.expression
;
450 event_expr
= ir_op_load_expr_to_event_expr(
451 ir_load_expr
, capture_str
);
455 ERR("%s: unexpected data type: %s.", __func__
,
456 ir_data_type_str(ir
->data_type
));
464 const char *ir_operator_type_human_str(enum ir_op_type op
)
486 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
487 const char *capture_str
)
489 struct lttng_event_expr
*event_expr
= NULL
;
491 assert(ir
->op
== IR_OP_ROOT
);
492 ir
= ir
->u
.root
.child
;
496 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
501 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
503 ir_operator_type_human_str(ir
->op
));
506 ERR("%s: unexpected IR op type: %s.", __func__
,
507 ir_op_type_str(ir
->op
));
515 void destroy_event_expr(void *ptr
)
517 lttng_event_expr_destroy(ptr
);
520 struct parse_event_rule_res
{
522 struct lttng_event_rule
*er
;
524 /* Array of `struct lttng_event_expr *` */
525 struct lttng_dynamic_pointer_array capture_descriptors
;
529 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
531 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
532 enum lttng_event_rule_type event_rule_type
=
533 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
534 struct argpar_state
*state
;
535 struct argpar_item
*item
= NULL
;
537 int consumed_args
= -1;
538 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
539 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
540 struct parse_event_rule_res res
= { 0 };
541 struct lttng_event_expr
*event_expr
= NULL
;
542 struct filter_parser_ctx
*parser_ctx
= NULL
;
543 struct lttng_log_level_rule
*log_level_rule
= NULL
;
545 /* Was the -a/--all flag provided? */
546 bool all_events
= false;
548 /* Tracepoint name (non-option argument). */
549 const char *tracepoint_name
= NULL
;
551 /* Holds the argument of --probe / --userspace-probe. */
558 char *exclude
= NULL
;
559 char **exclusion_list
= NULL
;
562 char *loglevel_str
= NULL
;
563 bool loglevel_only
= false;
565 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
567 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
569 ERR("Failed to allocate an argpar state.");
574 enum argpar_state_parse_next_status status
;
576 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
577 status
= argpar_state_parse_next(state
, &item
, &error
);
578 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
581 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
582 /* Just stop parsing here. */
584 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
588 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
590 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
591 const struct argpar_item_opt
*item_opt
=
592 (const struct argpar_item_opt
*) item
;
594 switch (item_opt
->descr
->id
) {
597 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_UST
)) {
603 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_KERNEL
)) {
609 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_LOG4J
)) {
615 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_JUL
)) {
621 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_PYTHON
)) {
627 /* Event rule types */
629 if (!assign_event_rule_type(&event_rule_type
,
630 LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
)) {
636 if (!assign_event_rule_type(&event_rule_type
,
637 LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
)) {
641 if (!assign_string(&source
, item_opt
->arg
, "source")) {
646 case OPT_USERSPACE_PROBE
:
647 if (!assign_event_rule_type(&event_rule_type
,
648 LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
)) {
652 if (!assign_string(&source
, item_opt
->arg
, "source")) {
658 if (!assign_event_rule_type(&event_rule_type
,
659 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
665 if (!assign_event_rule_type(&event_rule_type
,
666 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
675 if (!assign_string(&filter
, item_opt
->arg
,
682 if (!assign_string(&exclude
, item_opt
->arg
,
689 case OPT_LOGLEVEL_ONLY
:
690 if (!assign_string(&loglevel_str
, item_opt
->arg
,
691 "--loglevel/--loglevel-only")) {
695 loglevel_only
= item_opt
->descr
->id
==
701 const char *capture_str
= item_opt
->arg
;
703 ret
= filter_parser_ctx_create_from_filter_expression(
704 capture_str
, &parser_ctx
);
706 ERR("Failed to parse capture expression `%s`.",
711 event_expr
= ir_op_root_to_event_expr(
716 * ir_op_root_to_event_expr has printed
722 ret
= lttng_dynamic_pointer_array_add_pointer(
723 &res
.capture_descriptors
,
730 * The ownership of event expression was
731 * transferred to the dynamic array.
741 const struct argpar_item_non_opt
*item_non_opt
=
742 (const struct argpar_item_non_opt
*)
746 * Don't accept two non-option arguments/tracepoint
749 if (tracepoint_name
) {
750 ERR("Unexpected argument '%s'",
755 tracepoint_name
= item_non_opt
->arg
;
759 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
760 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
764 * Option -a is applicable to event rules of type tracepoint and
765 * syscall, and it is equivalent to using "*" as the tracepoint name.
768 switch (event_rule_type
) {
769 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
770 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
773 ERR("Can't use -a/--all with %s event rules.",
774 lttng_event_rule_type_str(event_rule_type
));
778 if (tracepoint_name
) {
779 ERR("Can't provide a tracepoint name with -a/--all.");
783 /* In which case, it's equivalent to tracepoint name "*". */
784 tracepoint_name
= "*";
788 * A tracepoint name (or -a, for the event rule types that accept it)
791 if (!tracepoint_name
) {
792 ERR("Need to provide either a tracepoint name or -a/--all.");
797 * We don't support multiple tracepoint names for now.
799 if (strchr(tracepoint_name
, ',')) {
800 ERR("Comma separated tracepoint names are not supported.");
805 * Update *argc and *argv so our caller can keep parsing what follows.
807 consumed_args
= argpar_state_get_ingested_orig_args(state
);
808 assert(consumed_args
>= 0);
809 *argc
-= consumed_args
;
810 *argv
+= consumed_args
;
812 /* Need to specify a domain. */
813 if (domain_type
== LTTNG_DOMAIN_NONE
) {
814 ERR("Please specify a domain (--kernel/--userspace/--jul/--log4j/--python).");
818 /* Validate event rule type against domain. */
819 switch (event_rule_type
) {
820 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
821 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
822 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
823 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
824 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
825 ERR("Event type not available for user-space tracing.");
830 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
838 * Adding a filter to a probe, function or userspace-probe would be
839 * denied by the kernel tracer as it's not supported at the moment. We
840 * do an early check here to warn the user.
842 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
843 switch (event_rule_type
) {
844 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
845 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
848 ERR("Filter expressions are not supported for %s event rules.",
849 lttng_event_rule_type_str(event_rule_type
));
854 /* If --exclude/-x was passed, split it into an exclusion list. */
856 if (domain_type
!= LTTNG_DOMAIN_UST
) {
857 ERR("Event name exclusions are not yet implemented for %s event rules.",
858 get_domain_str(domain_type
));
863 if (create_exclusion_list_and_validate(tracepoint_name
, exclude
,
864 &exclusion_list
) != 0) {
865 ERR("Failed to create exclusion list.");
870 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
871 ERR("Log levels are only applicable to tracepoint event rules.");
875 /* Finally, create the event rule object. */
876 switch (event_rule_type
) {
877 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
879 enum lttng_event_rule_status event_rule_status
;
881 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
883 ERR("Failed to create tracepoint event rule.");
888 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
889 res
.er
, tracepoint_name
);
890 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
891 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
898 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
900 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
901 ERR("Failed to set tracepoint event rule's filter to '%s'.",
907 /* Set exclusion list. */
908 if (exclusion_list
) {
911 for (n
= 0; exclusion_list
[n
]; n
++) {
912 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
915 if (event_rule_status
!=
916 LTTNG_EVENT_RULE_STATUS_OK
) {
917 ERR("Failed to set tracepoint exclusion list element '%s'",
927 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
928 ERR("Log levels are not supported by the kernel tracer.");
932 loglevel
= parse_loglevel_string(
933 loglevel_str
, domain_type
);
935 ERR("Failed to parse `%s` as a log level.",
941 log_level_rule
= lttng_log_level_rule_exactly_create(loglevel
);
943 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(loglevel
);
946 if (log_level_rule
== NULL
) {
947 ERR("Failed to create log level rule object.");
952 lttng_event_rule_tracepoint_set_log_level_rule(
953 res
.er
, log_level_rule
);
955 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
956 ERR("Failed to set log level on event fule.");
963 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
966 enum lttng_event_rule_status event_rule_status
;
969 ret
= parse_kernel_probe_opts(source
, &kernel_probe_location
);
971 ERR("Failed to parse kernel probe location.");
975 assert(kernel_probe_location
);
976 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
978 ERR("Failed to create kprobe event rule.");
982 event_rule_status
= lttng_event_rule_kernel_probe_set_event_name(res
.er
, tracepoint_name
);
983 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
984 ERR("Failed to set kprobe event rule's name to '%s'.", tracepoint_name
);
990 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
993 enum lttng_event_rule_status event_rule_status
;
995 ret
= parse_userspace_probe_opts(
996 source
, &userspace_probe_location
);
998 ERR("Failed to parse user space probe location.");
1002 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1004 ERR("Failed to create userspace probe event rule.");
1008 event_rule_status
= lttng_event_rule_userspace_probe_set_event_name(
1009 res
.er
, tracepoint_name
);
1010 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1011 ERR("Failed to set user space probe event rule's name to '%s'.",
1018 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1020 enum lttng_event_rule_status event_rule_status
;
1022 res
.er
= lttng_event_rule_syscall_create();
1024 ERR("Failed to create syscall event rule.");
1028 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1029 res
.er
, tracepoint_name
);
1030 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1031 ERR("Failed to set syscall event rule's pattern to '%s'.",
1037 event_rule_status
= lttng_event_rule_syscall_set_filter(
1039 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1040 ERR("Failed to set syscall event rule's filter to '%s'.",
1056 lttng_event_rule_destroy(res
.er
);
1058 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1062 filter_parser_ctx_free(parser_ctx
);
1065 lttng_event_expr_destroy(event_expr
);
1066 argpar_item_destroy(item
);
1068 argpar_state_destroy(state
);
1073 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1074 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1075 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1076 lttng_log_level_rule_destroy(log_level_rule
);
1081 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1083 struct parse_event_rule_res res
;
1084 struct lttng_condition
*c
;
1087 res
= parse_event_rule(argc
, argv
);
1093 c
= lttng_condition_on_event_create(res
.er
);
1094 lttng_event_rule_destroy(res
.er
);
1100 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1102 enum lttng_condition_status status
;
1103 struct lttng_event_expr
**expr
=
1104 lttng_dynamic_array_get_element(
1105 &res
.capture_descriptors
.array
, i
);
1109 status
= lttng_condition_on_event_append_capture_descriptor(
1111 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1112 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1113 ERR("The capture feature is unsupported by the event-rule condition type");
1119 /* Ownership of event expression moved to `c` */
1126 lttng_condition_destroy(c
);
1130 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1131 lttng_event_rule_destroy(res
.er
);
1136 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1138 struct lttng_condition
*cond
= NULL
;
1139 struct argpar_state
*state
= NULL
;
1140 struct argpar_item
*item
= NULL
;
1141 const char *threshold_arg
= NULL
;
1142 const char *session_name_arg
= NULL
;
1145 enum lttng_condition_status condition_status
;
1147 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1149 ERR("Failed to allocate an argpar state.");
1154 enum argpar_state_parse_next_status status
;
1156 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1157 status
= argpar_state_parse_next(state
, &item
, &error
);
1158 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1161 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1162 /* Just stop parsing here. */
1164 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1168 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1170 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1171 const struct argpar_item_opt
*item_opt
=
1172 (const struct argpar_item_opt
*) item
;
1174 switch (item_opt
->descr
->id
) {
1179 const struct argpar_item_non_opt
*item_non_opt
;
1181 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1183 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1185 switch (item_non_opt
->non_opt_index
) {
1187 session_name_arg
= item_non_opt
->arg
;
1190 threshold_arg
= item_non_opt
->arg
;
1193 ERR("Unexpected argument `%s`.",
1200 *argc
-= argpar_state_get_ingested_orig_args(state
);
1201 *argv
+= argpar_state_get_ingested_orig_args(state
);
1203 if (!session_name_arg
) {
1204 ERR("Missing session name argument.");
1208 if (!threshold_arg
) {
1209 ERR("Missing threshold argument.");
1213 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1214 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1218 cond
= lttng_condition_session_consumed_size_create();
1220 ERR("Failed to allocate a session consumed size condition.");
1224 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1225 cond
, session_name_arg
);
1226 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1227 ERR("Failed to set session consumed size condition's session name to '%s'.",
1232 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1234 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1235 ERR("Failed to set session consumed size condition threshold.");
1242 lttng_condition_destroy(cond
);
1246 argpar_state_destroy(state
);
1247 argpar_item_destroy(item
);
1253 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1255 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1260 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1262 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1267 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1269 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1274 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1276 ERR("Session rotation completed conditions are unsupported for the moment.");
1280 struct condition_descr
{
1282 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1286 struct condition_descr condition_descrs
[] = {
1287 { "on-event", handle_condition_event
},
1288 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1289 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1290 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1291 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1292 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1296 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1300 struct lttng_condition
*cond
;
1301 const struct condition_descr
*descr
= NULL
;
1303 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1304 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1305 descr
= &condition_descrs
[i
];
1311 ERR("Unknown condition name '%s'", condition_name
);
1315 cond
= descr
->handler(argc
, argv
);
1317 /* The handler has already printed an error message. */
1328 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1331 char **tokens
= NULL
;
1332 struct lttng_rate_policy
*policy
= NULL
;
1333 enum lttng_rate_policy_type policy_type
;
1334 unsigned long long value
;
1335 char *policy_type_str
;
1336 char *policy_value_str
;
1341 * rate policy fields are separated by ':'.
1343 tokens
= strutils_split(policy_str
, ':', 1);
1344 num_token
= strutils_array_of_strings_len(tokens
);
1347 * Early sanity check that the number of parameter is exactly 2.
1350 if (num_token
!= 2) {
1351 ERR("Rate policy format is invalid.");
1355 policy_type_str
= tokens
[0];
1356 policy_value_str
= tokens
[1];
1358 /* Parse the type. */
1359 if (strcmp(policy_type_str
, "once-after") == 0) {
1360 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1361 } else if (strcmp(policy_type_str
, "every") == 0) {
1362 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1364 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1368 /* Parse the value. */
1369 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1370 ERR("Failed to parse rate policy value `%s` as an integer.",
1376 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1380 switch (policy_type
) {
1381 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1382 policy
= lttng_rate_policy_every_n_create(value
);
1384 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1385 policy
= lttng_rate_policy_once_after_n_create(value
);
1391 if (policy
== NULL
) {
1392 ERR("Failed to create rate policy `%s`.", policy_str
);
1396 strutils_free_null_terminated_array_of_strings(tokens
);
1400 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1401 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1402 ARGPAR_OPT_DESCR_SENTINEL
1406 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1408 struct lttng_action
*action
= NULL
;
1409 struct argpar_state
*state
= NULL
;
1410 struct argpar_item
*item
= NULL
;
1412 struct lttng_rate_policy
*policy
= NULL
;
1414 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1416 ERR("Failed to allocate an argpar state.");
1421 enum argpar_state_parse_next_status status
;
1423 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1424 status
= argpar_state_parse_next(state
, &item
, &error
);
1425 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1428 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1429 /* Just stop parsing here. */
1431 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1435 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1437 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1438 const struct argpar_item_opt
*item_opt
=
1439 (const struct argpar_item_opt
*) item
;
1441 switch (item_opt
->descr
->id
) {
1442 case OPT_RATE_POLICY
:
1444 policy
= parse_rate_policy(item_opt
->arg
);
1454 const struct argpar_item_non_opt
*item_non_opt
;
1456 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1458 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1460 switch (item_non_opt
->non_opt_index
) {
1462 ERR("Unexpected argument `%s`.",
1469 *argc
-= argpar_state_get_ingested_orig_args(state
);
1470 *argv
+= argpar_state_get_ingested_orig_args(state
);
1472 action
= lttng_action_notify_create();
1474 ERR("Failed to create notify action");
1479 enum lttng_action_status status
;
1480 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1481 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1482 ERR("Failed to set rate policy");
1490 lttng_action_destroy(action
);
1494 lttng_rate_policy_destroy(policy
);
1495 argpar_state_destroy(state
);
1496 argpar_item_destroy(item
);
1501 * Generic handler for a kind of action that takes a session name and an
1502 * optional rate policy.
1505 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1507 struct lttng_action
*(*create_action_cb
)(void),
1508 enum lttng_action_status (*set_session_name_cb
)(
1509 struct lttng_action
*, const char *),
1510 enum lttng_action_status (*set_rate_policy_cb
)(
1511 struct lttng_action
*,
1512 const struct lttng_rate_policy
*),
1513 const char *action_name
)
1515 struct lttng_action
*action
= NULL
;
1516 struct argpar_state
*state
= NULL
;
1517 struct argpar_item
*item
= NULL
;
1518 const char *session_name_arg
= NULL
;
1520 enum lttng_action_status action_status
;
1521 struct lttng_rate_policy
*policy
= NULL
;
1523 assert(set_session_name_cb
);
1524 assert(set_rate_policy_cb
);
1526 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1527 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1528 ARGPAR_OPT_DESCR_SENTINEL
1531 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1533 ERR("Failed to allocate an argpar state.");
1538 enum argpar_state_parse_next_status status
;
1540 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1541 status
= argpar_state_parse_next(state
, &item
, &error
);
1542 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1545 } else if (status
==
1546 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1547 /* Just stop parsing here. */
1549 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1553 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1554 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1555 const struct argpar_item_opt
*item_opt
=
1556 (const struct argpar_item_opt
*) item
;
1558 switch (item_opt
->descr
->id
) {
1559 case OPT_RATE_POLICY
:
1561 policy
= parse_rate_policy(item_opt
->arg
);
1571 const struct argpar_item_non_opt
*item_non_opt
;
1572 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1574 switch (item_non_opt
->non_opt_index
) {
1576 session_name_arg
= item_non_opt
->arg
;
1579 ERR("Unexpected argument `%s`.",
1586 *argc
-= argpar_state_get_ingested_orig_args(state
);
1587 *argv
+= argpar_state_get_ingested_orig_args(state
);
1589 if (!session_name_arg
) {
1590 ERR("Missing session name.");
1594 action
= create_action_cb();
1596 ERR("Failed to allocate %s session action.", action_name
);
1600 action_status
= set_session_name_cb(action
, session_name_arg
);
1601 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1602 ERR("Failed to set action %s session's session name to '%s'.",
1603 action_name
, session_name_arg
);
1608 action_status
= set_rate_policy_cb(action
, policy
);
1609 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1610 ERR("Failed to set rate policy");
1618 lttng_action_destroy(action
);
1620 argpar_item_destroy(item
);
1622 lttng_rate_policy_destroy(policy
);
1624 argpar_state_destroy(state
);
1629 struct lttng_action
*handle_action_start_session(int *argc
,
1632 return handle_action_simple_session_with_policy(argc
, argv
,
1633 lttng_action_start_session_create
,
1634 lttng_action_start_session_set_session_name
,
1635 lttng_action_start_session_set_rate_policy
, "start");
1639 struct lttng_action
*handle_action_stop_session(int *argc
,
1642 return handle_action_simple_session_with_policy(argc
, argv
,
1643 lttng_action_stop_session_create
,
1644 lttng_action_stop_session_set_session_name
,
1645 lttng_action_stop_session_set_rate_policy
, "stop");
1649 struct lttng_action
*handle_action_rotate_session(int *argc
,
1652 return handle_action_simple_session_with_policy(argc
, argv
,
1653 lttng_action_rotate_session_create
,
1654 lttng_action_rotate_session_set_session_name
,
1655 lttng_action_rotate_session_set_rate_policy
,
1659 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1660 { OPT_NAME
, 'n', "name", true },
1661 { OPT_MAX_SIZE
, 'm', "max-size", true },
1662 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1663 { OPT_DATA_URL
, '\0', "data-url", true },
1664 { OPT_URL
, '\0', "url", true },
1665 { OPT_PATH
, '\0', "path", true },
1666 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1667 ARGPAR_OPT_DESCR_SENTINEL
1671 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1674 struct lttng_action
*action
= NULL
;
1675 struct argpar_state
*state
= NULL
;
1676 struct argpar_item
*item
= NULL
;
1677 const char *session_name_arg
= NULL
;
1678 char *snapshot_name_arg
= NULL
;
1679 char *ctrl_url_arg
= NULL
;
1680 char *data_url_arg
= NULL
;
1681 char *max_size_arg
= NULL
;
1682 char *url_arg
= NULL
;
1683 char *path_arg
= NULL
;
1685 enum lttng_action_status action_status
;
1686 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1687 struct lttng_rate_policy
*policy
= NULL
;
1689 unsigned int locations_specified
= 0;
1691 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1693 ERR("Failed to allocate an argpar state.");
1698 enum argpar_state_parse_next_status status
;
1700 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1701 status
= argpar_state_parse_next(state
, &item
, &error
);
1702 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1705 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1706 /* Just stop parsing here. */
1708 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1712 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1714 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1715 const struct argpar_item_opt
*item_opt
=
1716 (const struct argpar_item_opt
*) item
;
1718 switch (item_opt
->descr
->id
) {
1720 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1726 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1732 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1738 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1744 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1750 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1755 case OPT_RATE_POLICY
:
1757 policy
= parse_rate_policy(item_opt
->arg
);
1767 const struct argpar_item_non_opt
*item_non_opt
;
1769 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1771 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1773 switch (item_non_opt
->non_opt_index
) {
1775 session_name_arg
= item_non_opt
->arg
;
1778 ERR("Unexpected argument `%s`.",
1785 *argc
-= argpar_state_get_ingested_orig_args(state
);
1786 *argv
+= argpar_state_get_ingested_orig_args(state
);
1788 if (!session_name_arg
) {
1789 ERR("Missing session name.");
1793 /* --ctrl-url and --data-url must come in pair. */
1794 if (ctrl_url_arg
&& !data_url_arg
) {
1795 ERR("--ctrl-url is specified, but --data-url is missing.");
1799 if (!ctrl_url_arg
&& data_url_arg
) {
1800 ERR("--data-url is specified, but --ctrl-url is missing.");
1804 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1805 locations_specified
+= !!url_arg
;
1806 locations_specified
+= !!path_arg
;
1808 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1809 if (locations_specified
> 1) {
1810 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1815 * Did the user specify an option that implies using a
1816 * custom/unregistered output?
1818 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1819 snapshot_output
= lttng_snapshot_output_create();
1820 if (!snapshot_output
) {
1821 ERR("Failed to allocate a snapshot output.");
1826 action
= lttng_action_snapshot_session_create();
1828 ERR("Failed to allocate snapshot session action.");
1832 action_status
= lttng_action_snapshot_session_set_session_name(
1833 action
, session_name_arg
);
1834 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1835 ERR("Failed to set action snapshot session's session name to '%s'.",
1840 if (snapshot_name_arg
) {
1841 if (!snapshot_output
) {
1842 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1846 ret
= lttng_snapshot_output_set_name(
1847 snapshot_name_arg
, snapshot_output
);
1849 ERR("Failed to set name of snapshot output.");
1857 if (!snapshot_output
) {
1858 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1862 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1864 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1868 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1870 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1878 struct lttng_uri
*uris
;
1880 if (!strstr(url_arg
, "://")) {
1881 ERR("Failed to parse '%s' as an URL.", url_arg
);
1885 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1887 ERR("Failed to parse '%s' as an URL.", url_arg
);
1891 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1892 ret
= lttng_snapshot_output_set_local_path(
1893 uris
[0].dst
.path
, snapshot_output
);
1896 ERR("Failed to assign '%s' as a local destination.",
1901 ret
= lttng_snapshot_output_set_network_url(
1902 url_arg
, snapshot_output
);
1905 ERR("Failed to assign '%s' as a network URL.",
1913 ret
= lttng_snapshot_output_set_local_path(
1914 path_arg
, snapshot_output
);
1916 ERR("Failed to parse '%s' as a local path.", path_arg
);
1923 * Two argument form, network output with separate control and
1926 ret
= lttng_snapshot_output_set_network_urls(
1927 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1929 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1930 ctrl_url_arg
, data_url_arg
);
1935 if (snapshot_output
) {
1936 action_status
= lttng_action_snapshot_session_set_output(
1937 action
, snapshot_output
);
1938 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1939 ERR("Failed to set snapshot session action's output.");
1943 /* Ownership of `snapshot_output` has been transferred to the action. */
1944 snapshot_output
= NULL
;
1948 enum lttng_action_status status
;
1949 status
= lttng_action_snapshot_session_set_rate_policy(
1951 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1952 ERR("Failed to set rate policy");
1960 lttng_action_destroy(action
);
1964 free(snapshot_name_arg
);
1969 free(snapshot_output
);
1971 lttng_rate_policy_destroy(policy
);
1972 argpar_state_destroy(state
);
1973 argpar_item_destroy(item
);
1977 struct action_descr
{
1979 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1983 struct action_descr action_descrs
[] = {
1984 { "notify", handle_action_notify
},
1985 { "start-session", handle_action_start_session
},
1986 { "stop-session", handle_action_stop_session
},
1987 { "rotate-session", handle_action_rotate_session
},
1988 { "snapshot-session", handle_action_snapshot_session
},
1992 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1995 struct lttng_action
*action
;
1996 const struct action_descr
*descr
= NULL
;
1998 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1999 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2000 descr
= &action_descrs
[i
];
2006 ERR("Unknown action name: %s", action_name
);
2010 action
= descr
->handler(argc
, argv
);
2012 /* The handler has already printed an error message. */
2024 struct argpar_opt_descr add_trigger_options
[] = {
2025 { OPT_HELP
, 'h', "help", false },
2026 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2027 { OPT_CONDITION
, '\0', "condition", true },
2028 { OPT_ACTION
, '\0', "action", true },
2029 { OPT_ID
, '\0', "id", true },
2030 { OPT_USER_ID
, '\0', "user-id", true },
2031 ARGPAR_OPT_DESCR_SENTINEL
,
2035 void lttng_actions_destructor(void *p
)
2037 struct lttng_action
*action
= p
;
2039 lttng_action_destroy(action
);
2042 int cmd_add_trigger(int argc
, const char **argv
)
2045 int my_argc
= argc
- 1;
2046 const char **my_argv
= argv
+ 1;
2047 struct lttng_condition
*condition
= NULL
;
2048 struct lttng_dynamic_pointer_array actions
;
2049 struct argpar_state
*argpar_state
= NULL
;
2050 struct argpar_item
*argpar_item
= NULL
;
2051 struct lttng_action
*action_group
= NULL
;
2052 struct lttng_action
*action
= NULL
;
2053 struct lttng_trigger
*trigger
= NULL
;
2057 char *user_id
= NULL
;
2059 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2062 enum argpar_state_parse_next_status status
;
2063 const struct argpar_item_opt
*item_opt
;
2066 argpar_state_destroy(argpar_state
);
2067 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2068 add_trigger_options
);
2069 if (!argpar_state
) {
2070 ERR("Failed to create argpar state.");
2074 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2075 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2076 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2079 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2082 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2086 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2088 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2089 const struct argpar_item_non_opt
*item_non_opt
=
2090 (const struct argpar_item_non_opt
*)
2093 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2097 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2099 ingested_args
= argpar_state_get_ingested_orig_args(
2102 my_argc
-= ingested_args
;
2103 my_argv
+= ingested_args
;
2105 switch (item_opt
->descr
->id
) {
2110 case OPT_LIST_OPTIONS
:
2111 list_cmd_options_argpar(stdout
, add_trigger_options
);
2117 ERR("A --condition was already given.");
2121 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2124 * An error message was already printed by
2134 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2137 * An error message was already printed by
2143 ret
= lttng_dynamic_pointer_array_add_pointer(
2146 ERR("Failed to add pointer to pointer array.");
2150 /* Ownership of the action was transferred to the group. */
2157 if (!assign_string(&id
, item_opt
->arg
, "--id")) {
2165 if (!assign_string(&user_id
, item_opt
->arg
,
2178 ERR("Missing --condition.");
2182 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2183 ERR("Need at least one --action.");
2187 action_group
= lttng_action_group_create();
2188 if (!action_group
) {
2192 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2193 enum lttng_action_status status
;
2195 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2197 status
= lttng_action_group_add_action(action_group
, action
);
2198 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2203 * The `lttng_action_group_add_action()` takes a reference to
2204 * the action. We can destroy ours.
2206 lttng_action_destroy(action
);
2210 trigger
= lttng_trigger_create(condition
, action_group
);
2216 enum lttng_trigger_status trigger_status
=
2217 lttng_trigger_set_name(trigger
, id
);
2219 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2220 ERR("Failed to set trigger id.");
2226 enum lttng_trigger_status trigger_status
;
2231 uid
= strtol(user_id
, &end
, 10);
2232 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
2233 ERR("Failed to parse `%s` as a user id.", user_id
);
2236 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2237 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2238 ERR("Failed to set trigger's user identity.");
2243 ret
= lttng_register_trigger(trigger
);
2245 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2249 MSG("Trigger registered successfully.");
2257 argpar_state_destroy(argpar_state
);
2258 argpar_item_destroy(argpar_item
);
2259 lttng_dynamic_pointer_array_reset(&actions
);
2260 lttng_condition_destroy(condition
);
2261 lttng_action_destroy(action_group
);
2262 lttng_action_destroy(action
);
2263 lttng_trigger_destroy(trigger
);