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_FILTER
, 'f', "filter", true },
76 { OPT_NAME
, 'n', "name", true },
77 { OPT_EXCLUDE_NAMES
, 'x', "exclude-names", true },
78 { OPT_LOGLEVEL
, '\0', "loglevel", true },
79 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
80 { OPT_EVENT_NAME
, 'E', "event-name", true },
83 { OPT_USERSPACE
, 'u', "userspace", false },
84 { OPT_KERNEL
, 'k', "kernel", false },
85 { OPT_LOG4J
, 'l', "log4j", false },
86 { OPT_JUL
, 'j', "jul", false },
87 { OPT_PYTHON
, 'p', "python", false },
89 /* Event rule types */
90 { OPT_FUNCTION
, '\0', "function", true },
91 { OPT_PROBE
, '\0', "probe", true },
92 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
93 { OPT_SYSCALL
, '\0', "syscall" },
94 { OPT_TRACEPOINT
, '\0', "tracepoint" },
96 /* Capture descriptor */
97 { OPT_CAPTURE
, '\0', "capture", true },
99 ARGPAR_OPT_DESCR_SENTINEL
103 bool assign_domain_type(enum lttng_domain_type
*dest
,
104 enum lttng_domain_type src
)
108 if (*dest
== LTTNG_DOMAIN_NONE
|| *dest
== src
) {
112 ERR("Multiple domains specified.");
120 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
121 enum lttng_event_rule_type src
)
125 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
129 ERR("Multiple event types specified.");
137 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
142 ERR("Duplicate '%s' given.", opt_name
);
148 PERROR("Failed to allocate string '%s'.", opt_name
);
162 /* This is defined in enable_events.c. */
164 int create_exclusion_list_and_validate(const char *event_name
,
165 const char *exclusions_arg
,
166 char ***exclusion_list
);
169 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
170 * is not recognized as a valid log level.
173 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
175 switch (domain_type
) {
176 case LTTNG_DOMAIN_UST
:
178 enum lttng_loglevel loglevel
;
179 const int ret
= loglevel_name_to_value(str
, &loglevel
);
181 return ret
== -1 ? ret
: (int) loglevel
;
183 case LTTNG_DOMAIN_LOG4J
:
185 enum lttng_loglevel_log4j loglevel
;
186 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
188 return ret
== -1 ? ret
: (int) loglevel
;
190 case LTTNG_DOMAIN_JUL
:
192 enum lttng_loglevel_jul loglevel
;
193 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
195 return ret
== -1 ? ret
: (int) loglevel
;
197 case LTTNG_DOMAIN_PYTHON
:
199 enum lttng_loglevel_python loglevel
;
200 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
202 return ret
== -1 ? ret
: (int) loglevel
;
205 /* Invalid domain type. */
210 static int parse_kernel_probe_opts(const char *source
,
211 struct lttng_kernel_probe_location
**location
)
216 char name
[LTTNG_SYMBOL_NAME_LEN
];
217 char *symbol_name
= NULL
;
220 /* Check for symbol+offset. */
221 match
= sscanf(source
,
222 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
226 if (*s_hex
== '\0') {
227 ERR("Kernel probe symbol offset is missing.");
231 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
233 PERROR("Failed to copy kernel probe location symbol name.");
236 offset
= strtoul(s_hex
, NULL
, 0);
238 *location
= lttng_kernel_probe_location_symbol_create(
239 symbol_name
, offset
);
241 ERR("Failed to create symbol kernel probe location.");
248 /* Check for symbol. */
249 if (isalpha(name
[0]) || name
[0] == '_') {
250 match
= sscanf(source
,
251 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
255 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
257 ERR("Failed to copy kernel probe location symbol name.");
261 *location
= lttng_kernel_probe_location_symbol_create(
264 ERR("Failed to create symbol kernel probe location.");
272 /* Check for address. */
273 match
= sscanf(source
, "%18s", s_hex
);
277 if (*s_hex
== '\0') {
278 ERR("Invalid kernel probe location address.");
282 address
= strtoul(s_hex
, NULL
, 0);
283 *location
= lttng_kernel_probe_location_address_create(address
);
285 ERR("Failed to create symbol kernel probe location.");
303 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
304 const struct ir_load_expression
*load_expr
,
305 const char *capture_str
)
307 char *provider_name
= NULL
;
308 struct lttng_event_expr
*event_expr
= NULL
;
309 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
310 const enum ir_load_expression_type load_expr_child_type
=
313 switch (load_expr_child_type
) {
314 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
315 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
317 const char *field_name
;
319 load_expr_op
= load_expr_op
->next
;
320 assert(load_expr_op
);
321 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
322 field_name
= load_expr_op
->u
.symbol
;
325 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
326 lttng_event_expr_event_payload_field_create(field_name
) :
327 lttng_event_expr_channel_context_field_create(field_name
);
329 ERR("Failed to create %s event expression: field name = `%s`.",
330 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
331 "payload field" : "channel context",
338 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
341 const char *type_name
;
342 const char *field_name
;
344 load_expr_op
= load_expr_op
->next
;
345 assert(load_expr_op
);
346 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
347 field_name
= load_expr_op
->u
.symbol
;
351 * The field name needs to be of the form PROVIDER:TYPE. We
354 colon
= strchr(field_name
, ':');
356 ERR("Invalid app-specific context field name: missing colon in `%s`.",
361 type_name
= colon
+ 1;
362 if (*type_name
== '\0') {
363 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
368 provider_name
= strndup(field_name
, colon
- field_name
);
369 if (!provider_name
) {
370 PERROR("Failed to allocate field name string");
374 event_expr
= lttng_event_expr_app_specific_context_field_create(
375 provider_name
, type_name
);
377 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
378 provider_name
, type_name
);
385 ERR("%s: unexpected load expr type %d.", __func__
,
390 load_expr_op
= load_expr_op
->next
;
392 /* There may be a single array index after that. */
393 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
394 struct lttng_event_expr
*index_event_expr
;
395 const uint64_t index
= load_expr_op
->u
.index
;
397 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
398 if (!index_event_expr
) {
399 ERR("Failed to create array field element event expression.");
403 event_expr
= index_event_expr
;
404 load_expr_op
= load_expr_op
->next
;
407 switch (load_expr_op
->type
) {
408 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
410 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
411 * always found at the end of the chain.
414 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
415 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
420 ERR("%s: unexpected load expression operator %s.", __func__
,
421 ir_load_expression_type_str(load_expr_op
->type
));
428 lttng_event_expr_destroy(event_expr
);
438 struct lttng_event_expr
*ir_op_load_to_event_expr(
439 const struct ir_op
*ir
, const char *capture_str
)
441 struct lttng_event_expr
*event_expr
= NULL
;
443 assert(ir
->op
== IR_OP_LOAD
);
445 switch (ir
->data_type
) {
446 case IR_DATA_EXPRESSION
:
448 const struct ir_load_expression
*ir_load_expr
=
449 ir
->u
.load
.u
.expression
;
451 event_expr
= ir_op_load_expr_to_event_expr(
452 ir_load_expr
, capture_str
);
456 ERR("%s: unexpected data type: %s.", __func__
,
457 ir_data_type_str(ir
->data_type
));
465 const char *ir_operator_type_human_str(enum ir_op_type op
)
487 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
488 const char *capture_str
)
490 struct lttng_event_expr
*event_expr
= NULL
;
492 assert(ir
->op
== IR_OP_ROOT
);
493 ir
= ir
->u
.root
.child
;
497 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
502 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
504 ir_operator_type_human_str(ir
->op
));
507 ERR("%s: unexpected IR op type: %s.", __func__
,
508 ir_op_type_str(ir
->op
));
516 void destroy_event_expr(void *ptr
)
518 lttng_event_expr_destroy(ptr
);
521 struct parse_event_rule_res
{
523 struct lttng_event_rule
*er
;
525 /* Array of `struct lttng_event_expr *` */
526 struct lttng_dynamic_pointer_array capture_descriptors
;
530 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
532 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
533 enum lttng_event_rule_type event_rule_type
=
534 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
535 struct argpar_state
*state
;
536 struct argpar_item
*item
= NULL
;
538 int consumed_args
= -1;
539 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
540 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
541 struct parse_event_rule_res res
= { 0 };
542 struct lttng_event_expr
*event_expr
= NULL
;
543 struct filter_parser_ctx
*parser_ctx
= NULL
;
544 struct lttng_log_level_rule
*log_level_rule
= NULL
;
546 /* Tracepoint and syscall options. */
548 char *exclude_names
= NULL
;
549 char **exclusion_list
= NULL
;
551 /* Holds the argument of --probe / --userspace-probe. */
552 char *probe_source
= NULL
;
553 char *probe_event_name
= NULL
;
559 char *loglevel_str
= NULL
;
560 bool loglevel_only
= false;
562 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
564 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
566 ERR("Failed to allocate an argpar state.");
571 enum argpar_state_parse_next_status status
;
573 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
574 status
= argpar_state_parse_next(state
, &item
, &error
);
575 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
578 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
579 /* Just stop parsing here. */
581 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
585 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
587 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
588 const struct argpar_item_opt
*item_opt
=
589 (const struct argpar_item_opt
*) item
;
591 switch (item_opt
->descr
->id
) {
594 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_UST
)) {
600 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_KERNEL
)) {
606 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_LOG4J
)) {
612 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_JUL
)) {
618 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_PYTHON
)) {
624 /* Event rule types */
626 if (!assign_event_rule_type(&event_rule_type
,
627 LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
)) {
633 if (!assign_event_rule_type(&event_rule_type
,
634 LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
)) {
638 if (!assign_string(&probe_source
, item_opt
->arg
,
644 case OPT_USERSPACE_PROBE
:
645 if (!assign_event_rule_type(&event_rule_type
,
646 LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
)) {
650 if (!assign_string(&probe_source
, item_opt
->arg
,
651 "--userspace-probe")) {
657 if (!assign_string(&probe_event_name
,
659 "--event-name/-E")) {
665 if (!assign_event_rule_type(&event_rule_type
,
666 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
672 if (!assign_event_rule_type(&event_rule_type
,
673 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
679 if (!assign_string(&filter
, item_opt
->arg
,
686 if (!assign_string(&name
, item_opt
->arg
,
692 case OPT_EXCLUDE_NAMES
:
693 if (!assign_string(&exclude_names
,
695 "--exclude-names/-x")) {
701 case OPT_LOGLEVEL_ONLY
:
702 if (!assign_string(&loglevel_str
, item_opt
->arg
,
703 "--loglevel/--loglevel-only")) {
707 loglevel_only
= item_opt
->descr
->id
==
713 const char *capture_str
= item_opt
->arg
;
715 ret
= filter_parser_ctx_create_from_filter_expression(
716 capture_str
, &parser_ctx
);
718 ERR("Failed to parse capture expression `%s`.",
723 event_expr
= ir_op_root_to_event_expr(
728 * ir_op_root_to_event_expr has printed
734 ret
= lttng_dynamic_pointer_array_add_pointer(
735 &res
.capture_descriptors
,
742 * The ownership of event expression was
743 * transferred to the dynamic array.
753 const struct argpar_item_non_opt
*item_non_opt
=
754 (const struct argpar_item_non_opt
*)
757 /* Don't accept non-option arguments. */
758 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
763 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
764 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
768 * Option --name is applicable to event rules of type tracepoint
769 * and syscall. For tracepoint and syscall rules, if --name is
770 * omitted, it is implicitly "*".
772 switch (event_rule_type
) {
773 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
774 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
782 ERR("Can't use --name with %s event rules.",
783 lttng_event_rule_type_str(
789 ERR("Can't use --exclude-names/-x with %s event rules.",
790 lttng_event_rule_type_str(
797 * Option --event-name is only applicable to event rules of type probe.
798 * If omitted, it defaults to the probe location.
800 switch (event_rule_type
) {
801 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
802 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
803 if (!probe_event_name
) {
804 probe_event_name
= strdup(probe_source
);
810 if (probe_event_name
) {
811 ERR("Can't use --event-name with %s event rules.",
812 lttng_event_rule_type_str(
819 * Update *argc and *argv so our caller can keep parsing what follows.
821 consumed_args
= argpar_state_get_ingested_orig_args(state
);
822 assert(consumed_args
>= 0);
823 *argc
-= consumed_args
;
824 *argv
+= consumed_args
;
826 /* Need to specify a domain. */
827 if (domain_type
== LTTNG_DOMAIN_NONE
) {
828 ERR("Please specify a domain (--kernel/--userspace/--jul/--log4j/--python).");
832 /* Validate event rule type against domain. */
833 switch (event_rule_type
) {
834 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
835 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
836 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
837 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
838 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
839 ERR("Event type not available for user-space tracing.");
844 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
852 * Adding a filter to a probe, function or userspace-probe would be
853 * denied by the kernel tracer as it's not supported at the moment. We
854 * do an early check here to warn the user.
856 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
857 switch (event_rule_type
) {
858 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
859 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
862 ERR("Filter expressions are not supported for %s event rules.",
863 lttng_event_rule_type_str(event_rule_type
));
868 /* If --exclude/-x was passed, split it into an exclusion list. */
870 if (domain_type
!= LTTNG_DOMAIN_UST
) {
871 ERR("Event name exclusions are not yet implemented for %s event rules.",
872 get_domain_str(domain_type
));
876 if (create_exclusion_list_and_validate(name
,
877 exclude_names
, &exclusion_list
) != 0) {
878 ERR("Failed to create exclusion list.");
883 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
884 ERR("Log levels are only applicable to tracepoint event rules.");
888 /* Finally, create the event rule object. */
889 switch (event_rule_type
) {
890 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
892 enum lttng_event_rule_status event_rule_status
;
894 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
896 ERR("Failed to create tracepoint event rule.");
901 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
903 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
904 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
911 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
913 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
914 ERR("Failed to set tracepoint event rule's filter to '%s'.",
920 /* Set exclusion list. */
921 if (exclusion_list
) {
924 for (n
= 0; exclusion_list
[n
]; n
++) {
925 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
928 if (event_rule_status
!=
929 LTTNG_EVENT_RULE_STATUS_OK
) {
930 ERR("Failed to set tracepoint exclusion list element '%s'",
940 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
941 ERR("Log levels are not supported by the kernel tracer.");
945 loglevel
= parse_loglevel_string(
946 loglevel_str
, domain_type
);
948 ERR("Failed to parse `%s` as a log level.",
954 log_level_rule
= lttng_log_level_rule_exactly_create(loglevel
);
956 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(loglevel
);
959 if (log_level_rule
== NULL
) {
960 ERR("Failed to create log level rule object.");
965 lttng_event_rule_tracepoint_set_log_level_rule(
966 res
.er
, log_level_rule
);
968 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
969 ERR("Failed to set log level on event fule.");
976 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
979 enum lttng_event_rule_status event_rule_status
;
981 ret
= parse_kernel_probe_opts(
982 probe_source
, &kernel_probe_location
);
984 ERR("Failed to parse kernel probe location.");
988 assert(kernel_probe_location
);
989 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
991 ERR("Failed to create kprobe event rule.");
996 lttng_event_rule_kernel_probe_set_event_name(
997 res
.er
, probe_event_name
);
998 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
999 ERR("Failed to set kprobe event rule's name to '%s'.",
1006 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1009 enum lttng_event_rule_status event_rule_status
;
1011 ret
= parse_userspace_probe_opts(
1012 probe_source
, &userspace_probe_location
);
1014 ERR("Failed to parse user space probe location.");
1018 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1020 ERR("Failed to create userspace probe event rule.");
1025 lttng_event_rule_userspace_probe_set_event_name(
1026 res
.er
, probe_event_name
);
1027 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1028 ERR("Failed to set user space probe event rule's name to '%s'.",
1035 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1037 enum lttng_event_rule_status event_rule_status
;
1039 res
.er
= lttng_event_rule_syscall_create();
1041 ERR("Failed to create syscall event rule.");
1045 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1047 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1048 ERR("Failed to set syscall event rule's pattern to '%s'.",
1054 event_rule_status
= lttng_event_rule_syscall_set_filter(
1056 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1057 ERR("Failed to set syscall event rule's filter to '%s'.",
1073 lttng_event_rule_destroy(res
.er
);
1075 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1079 filter_parser_ctx_free(parser_ctx
);
1082 lttng_event_expr_destroy(event_expr
);
1083 argpar_item_destroy(item
);
1085 argpar_state_destroy(state
);
1088 free(exclude_names
);
1091 free(probe_event_name
);
1093 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1094 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1095 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1096 lttng_log_level_rule_destroy(log_level_rule
);
1101 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1103 struct parse_event_rule_res res
;
1104 struct lttng_condition
*c
;
1107 res
= parse_event_rule(argc
, argv
);
1113 c
= lttng_condition_on_event_create(res
.er
);
1114 lttng_event_rule_destroy(res
.er
);
1120 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1122 enum lttng_condition_status status
;
1123 struct lttng_event_expr
**expr
=
1124 lttng_dynamic_array_get_element(
1125 &res
.capture_descriptors
.array
, i
);
1129 status
= lttng_condition_on_event_append_capture_descriptor(
1131 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1132 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1133 ERR("The capture feature is unsupported by the event-rule condition type");
1139 /* Ownership of event expression moved to `c` */
1146 lttng_condition_destroy(c
);
1150 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1151 lttng_event_rule_destroy(res
.er
);
1156 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1158 struct lttng_condition
*cond
= NULL
;
1159 struct argpar_state
*state
= NULL
;
1160 struct argpar_item
*item
= NULL
;
1161 const char *threshold_arg
= NULL
;
1162 const char *session_name_arg
= NULL
;
1165 enum lttng_condition_status condition_status
;
1167 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1169 ERR("Failed to allocate an argpar state.");
1174 enum argpar_state_parse_next_status status
;
1176 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1177 status
= argpar_state_parse_next(state
, &item
, &error
);
1178 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1181 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1182 /* Just stop parsing here. */
1184 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1188 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1190 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1191 const struct argpar_item_opt
*item_opt
=
1192 (const struct argpar_item_opt
*) item
;
1194 switch (item_opt
->descr
->id
) {
1199 const struct argpar_item_non_opt
*item_non_opt
;
1201 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1203 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1205 switch (item_non_opt
->non_opt_index
) {
1207 session_name_arg
= item_non_opt
->arg
;
1210 threshold_arg
= item_non_opt
->arg
;
1213 ERR("Unexpected argument `%s`.",
1220 *argc
-= argpar_state_get_ingested_orig_args(state
);
1221 *argv
+= argpar_state_get_ingested_orig_args(state
);
1223 if (!session_name_arg
) {
1224 ERR("Missing session name argument.");
1228 if (!threshold_arg
) {
1229 ERR("Missing threshold argument.");
1233 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1234 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1238 cond
= lttng_condition_session_consumed_size_create();
1240 ERR("Failed to allocate a session consumed size condition.");
1244 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1245 cond
, session_name_arg
);
1246 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1247 ERR("Failed to set session consumed size condition's session name to '%s'.",
1252 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1254 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1255 ERR("Failed to set session consumed size condition threshold.");
1262 lttng_condition_destroy(cond
);
1266 argpar_state_destroy(state
);
1267 argpar_item_destroy(item
);
1273 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1275 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1280 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1282 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1287 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1289 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1294 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1296 ERR("Session rotation completed conditions are unsupported for the moment.");
1300 struct condition_descr
{
1302 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1306 struct condition_descr condition_descrs
[] = {
1307 { "event-rule-matches", handle_condition_event
},
1308 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1309 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1310 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1311 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1312 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1316 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1320 struct lttng_condition
*cond
;
1321 const struct condition_descr
*descr
= NULL
;
1323 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1324 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1325 descr
= &condition_descrs
[i
];
1331 ERR("Unknown condition name '%s'", condition_name
);
1335 cond
= descr
->handler(argc
, argv
);
1337 /* The handler has already printed an error message. */
1348 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1351 char **tokens
= NULL
;
1352 struct lttng_rate_policy
*policy
= NULL
;
1353 enum lttng_rate_policy_type policy_type
;
1354 unsigned long long value
;
1355 char *policy_type_str
;
1356 char *policy_value_str
;
1361 * rate policy fields are separated by ':'.
1363 tokens
= strutils_split(policy_str
, ':', 1);
1364 num_token
= strutils_array_of_strings_len(tokens
);
1367 * Early sanity check that the number of parameter is exactly 2.
1370 if (num_token
!= 2) {
1371 ERR("Rate policy format is invalid.");
1375 policy_type_str
= tokens
[0];
1376 policy_value_str
= tokens
[1];
1378 /* Parse the type. */
1379 if (strcmp(policy_type_str
, "once-after") == 0) {
1380 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1381 } else if (strcmp(policy_type_str
, "every") == 0) {
1382 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1384 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1388 /* Parse the value. */
1389 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1390 ERR("Failed to parse rate policy value `%s` as an integer.",
1396 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1400 switch (policy_type
) {
1401 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1402 policy
= lttng_rate_policy_every_n_create(value
);
1404 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1405 policy
= lttng_rate_policy_once_after_n_create(value
);
1411 if (policy
== NULL
) {
1412 ERR("Failed to create rate policy `%s`.", policy_str
);
1416 strutils_free_null_terminated_array_of_strings(tokens
);
1420 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1421 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1422 ARGPAR_OPT_DESCR_SENTINEL
1426 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1428 struct lttng_action
*action
= NULL
;
1429 struct argpar_state
*state
= NULL
;
1430 struct argpar_item
*item
= NULL
;
1432 struct lttng_rate_policy
*policy
= NULL
;
1434 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1436 ERR("Failed to allocate an argpar state.");
1441 enum argpar_state_parse_next_status status
;
1443 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1444 status
= argpar_state_parse_next(state
, &item
, &error
);
1445 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1448 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1449 /* Just stop parsing here. */
1451 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1455 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1457 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1458 const struct argpar_item_opt
*item_opt
=
1459 (const struct argpar_item_opt
*) item
;
1461 switch (item_opt
->descr
->id
) {
1462 case OPT_RATE_POLICY
:
1464 policy
= parse_rate_policy(item_opt
->arg
);
1474 const struct argpar_item_non_opt
*item_non_opt
;
1476 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1478 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1480 switch (item_non_opt
->non_opt_index
) {
1482 ERR("Unexpected argument `%s`.",
1489 *argc
-= argpar_state_get_ingested_orig_args(state
);
1490 *argv
+= argpar_state_get_ingested_orig_args(state
);
1492 action
= lttng_action_notify_create();
1494 ERR("Failed to create notify action");
1499 enum lttng_action_status status
;
1500 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1501 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1502 ERR("Failed to set rate policy");
1510 lttng_action_destroy(action
);
1514 lttng_rate_policy_destroy(policy
);
1515 argpar_state_destroy(state
);
1516 argpar_item_destroy(item
);
1521 * Generic handler for a kind of action that takes a session name and an
1522 * optional rate policy.
1525 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1527 struct lttng_action
*(*create_action_cb
)(void),
1528 enum lttng_action_status (*set_session_name_cb
)(
1529 struct lttng_action
*, const char *),
1530 enum lttng_action_status (*set_rate_policy_cb
)(
1531 struct lttng_action
*,
1532 const struct lttng_rate_policy
*),
1533 const char *action_name
)
1535 struct lttng_action
*action
= NULL
;
1536 struct argpar_state
*state
= NULL
;
1537 struct argpar_item
*item
= NULL
;
1538 const char *session_name_arg
= NULL
;
1540 enum lttng_action_status action_status
;
1541 struct lttng_rate_policy
*policy
= NULL
;
1543 assert(set_session_name_cb
);
1544 assert(set_rate_policy_cb
);
1546 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1547 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1548 ARGPAR_OPT_DESCR_SENTINEL
1551 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1553 ERR("Failed to allocate an argpar state.");
1558 enum argpar_state_parse_next_status status
;
1560 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1561 status
= argpar_state_parse_next(state
, &item
, &error
);
1562 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1565 } else if (status
==
1566 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1567 /* Just stop parsing here. */
1569 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1573 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1574 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1575 const struct argpar_item_opt
*item_opt
=
1576 (const struct argpar_item_opt
*) item
;
1578 switch (item_opt
->descr
->id
) {
1579 case OPT_RATE_POLICY
:
1581 policy
= parse_rate_policy(item_opt
->arg
);
1591 const struct argpar_item_non_opt
*item_non_opt
;
1592 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1594 switch (item_non_opt
->non_opt_index
) {
1596 session_name_arg
= item_non_opt
->arg
;
1599 ERR("Unexpected argument `%s`.",
1606 *argc
-= argpar_state_get_ingested_orig_args(state
);
1607 *argv
+= argpar_state_get_ingested_orig_args(state
);
1609 if (!session_name_arg
) {
1610 ERR("Missing session name.");
1614 action
= create_action_cb();
1616 ERR("Failed to allocate %s session action.", action_name
);
1620 action_status
= set_session_name_cb(action
, session_name_arg
);
1621 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1622 ERR("Failed to set action %s session's session name to '%s'.",
1623 action_name
, session_name_arg
);
1628 action_status
= set_rate_policy_cb(action
, policy
);
1629 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1630 ERR("Failed to set rate policy");
1638 lttng_action_destroy(action
);
1640 argpar_item_destroy(item
);
1642 lttng_rate_policy_destroy(policy
);
1644 argpar_state_destroy(state
);
1649 struct lttng_action
*handle_action_start_session(int *argc
,
1652 return handle_action_simple_session_with_policy(argc
, argv
,
1653 lttng_action_start_session_create
,
1654 lttng_action_start_session_set_session_name
,
1655 lttng_action_start_session_set_rate_policy
, "start");
1659 struct lttng_action
*handle_action_stop_session(int *argc
,
1662 return handle_action_simple_session_with_policy(argc
, argv
,
1663 lttng_action_stop_session_create
,
1664 lttng_action_stop_session_set_session_name
,
1665 lttng_action_stop_session_set_rate_policy
, "stop");
1669 struct lttng_action
*handle_action_rotate_session(int *argc
,
1672 return handle_action_simple_session_with_policy(argc
, argv
,
1673 lttng_action_rotate_session_create
,
1674 lttng_action_rotate_session_set_session_name
,
1675 lttng_action_rotate_session_set_rate_policy
,
1679 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1680 { OPT_NAME
, 'n', "name", true },
1681 { OPT_MAX_SIZE
, 'm', "max-size", true },
1682 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1683 { OPT_DATA_URL
, '\0', "data-url", true },
1684 { OPT_URL
, '\0', "url", true },
1685 { OPT_PATH
, '\0', "path", true },
1686 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1687 ARGPAR_OPT_DESCR_SENTINEL
1691 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1694 struct lttng_action
*action
= NULL
;
1695 struct argpar_state
*state
= NULL
;
1696 struct argpar_item
*item
= NULL
;
1697 const char *session_name_arg
= NULL
;
1698 char *snapshot_name_arg
= NULL
;
1699 char *ctrl_url_arg
= NULL
;
1700 char *data_url_arg
= NULL
;
1701 char *max_size_arg
= NULL
;
1702 char *url_arg
= NULL
;
1703 char *path_arg
= NULL
;
1705 enum lttng_action_status action_status
;
1706 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1707 struct lttng_rate_policy
*policy
= NULL
;
1709 unsigned int locations_specified
= 0;
1711 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1713 ERR("Failed to allocate an argpar state.");
1718 enum argpar_state_parse_next_status status
;
1720 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1721 status
= argpar_state_parse_next(state
, &item
, &error
);
1722 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1725 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1726 /* Just stop parsing here. */
1728 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1732 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1734 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1735 const struct argpar_item_opt
*item_opt
=
1736 (const struct argpar_item_opt
*) item
;
1738 switch (item_opt
->descr
->id
) {
1740 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1746 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1752 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1758 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1764 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1770 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1775 case OPT_RATE_POLICY
:
1777 policy
= parse_rate_policy(item_opt
->arg
);
1787 const struct argpar_item_non_opt
*item_non_opt
;
1789 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1791 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1793 switch (item_non_opt
->non_opt_index
) {
1795 session_name_arg
= item_non_opt
->arg
;
1798 ERR("Unexpected argument `%s`.",
1805 *argc
-= argpar_state_get_ingested_orig_args(state
);
1806 *argv
+= argpar_state_get_ingested_orig_args(state
);
1808 if (!session_name_arg
) {
1809 ERR("Missing session name.");
1813 /* --ctrl-url and --data-url must come in pair. */
1814 if (ctrl_url_arg
&& !data_url_arg
) {
1815 ERR("--ctrl-url is specified, but --data-url is missing.");
1819 if (!ctrl_url_arg
&& data_url_arg
) {
1820 ERR("--data-url is specified, but --ctrl-url is missing.");
1824 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1825 locations_specified
+= !!url_arg
;
1826 locations_specified
+= !!path_arg
;
1828 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1829 if (locations_specified
> 1) {
1830 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1835 * Did the user specify an option that implies using a
1836 * custom/unregistered output?
1838 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1839 snapshot_output
= lttng_snapshot_output_create();
1840 if (!snapshot_output
) {
1841 ERR("Failed to allocate a snapshot output.");
1846 action
= lttng_action_snapshot_session_create();
1848 ERR("Failed to allocate snapshot session action.");
1852 action_status
= lttng_action_snapshot_session_set_session_name(
1853 action
, session_name_arg
);
1854 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1855 ERR("Failed to set action snapshot session's session name to '%s'.",
1860 if (snapshot_name_arg
) {
1861 if (!snapshot_output
) {
1862 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1866 ret
= lttng_snapshot_output_set_name(
1867 snapshot_name_arg
, snapshot_output
);
1869 ERR("Failed to set name of snapshot output.");
1877 if (!snapshot_output
) {
1878 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1882 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1884 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1888 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1890 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1898 struct lttng_uri
*uris
;
1900 if (!strstr(url_arg
, "://")) {
1901 ERR("Failed to parse '%s' as an URL.", url_arg
);
1905 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1907 ERR("Failed to parse '%s' as an URL.", url_arg
);
1911 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1912 ret
= lttng_snapshot_output_set_local_path(
1913 uris
[0].dst
.path
, snapshot_output
);
1916 ERR("Failed to assign '%s' as a local destination.",
1921 ret
= lttng_snapshot_output_set_network_url(
1922 url_arg
, snapshot_output
);
1925 ERR("Failed to assign '%s' as a network URL.",
1933 ret
= lttng_snapshot_output_set_local_path(
1934 path_arg
, snapshot_output
);
1936 ERR("Failed to parse '%s' as a local path.", path_arg
);
1943 * Two argument form, network output with separate control and
1946 ret
= lttng_snapshot_output_set_network_urls(
1947 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1949 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1950 ctrl_url_arg
, data_url_arg
);
1955 if (snapshot_output
) {
1956 action_status
= lttng_action_snapshot_session_set_output(
1957 action
, snapshot_output
);
1958 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1959 ERR("Failed to set snapshot session action's output.");
1963 /* Ownership of `snapshot_output` has been transferred to the action. */
1964 snapshot_output
= NULL
;
1968 enum lttng_action_status status
;
1969 status
= lttng_action_snapshot_session_set_rate_policy(
1971 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1972 ERR("Failed to set rate policy");
1980 lttng_action_destroy(action
);
1984 free(snapshot_name_arg
);
1989 free(snapshot_output
);
1991 lttng_rate_policy_destroy(policy
);
1992 argpar_state_destroy(state
);
1993 argpar_item_destroy(item
);
1997 struct action_descr
{
1999 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2003 struct action_descr action_descrs
[] = {
2004 { "notify", handle_action_notify
},
2005 { "start-session", handle_action_start_session
},
2006 { "stop-session", handle_action_stop_session
},
2007 { "rotate-session", handle_action_rotate_session
},
2008 { "snapshot-session", handle_action_snapshot_session
},
2012 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2015 struct lttng_action
*action
;
2016 const struct action_descr
*descr
= NULL
;
2018 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2019 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2020 descr
= &action_descrs
[i
];
2026 ERR("Unknown action name: %s", action_name
);
2030 action
= descr
->handler(argc
, argv
);
2032 /* The handler has already printed an error message. */
2044 struct argpar_opt_descr add_trigger_options
[] = {
2045 { OPT_HELP
, 'h', "help", false },
2046 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2047 { OPT_CONDITION
, '\0', "condition", true },
2048 { OPT_ACTION
, '\0', "action", true },
2049 { OPT_NAME
, '\0', "name", true },
2050 { OPT_USER_ID
, '\0', "user-id", true },
2051 ARGPAR_OPT_DESCR_SENTINEL
,
2055 void lttng_actions_destructor(void *p
)
2057 struct lttng_action
*action
= p
;
2059 lttng_action_destroy(action
);
2062 int cmd_add_trigger(int argc
, const char **argv
)
2065 int my_argc
= argc
- 1;
2066 const char **my_argv
= argv
+ 1;
2067 struct lttng_condition
*condition
= NULL
;
2068 struct lttng_dynamic_pointer_array actions
;
2069 struct argpar_state
*argpar_state
= NULL
;
2070 struct argpar_item
*argpar_item
= NULL
;
2071 struct lttng_action
*action_group
= NULL
;
2072 struct lttng_action
*action
= NULL
;
2073 struct lttng_trigger
*trigger
= NULL
;
2077 char *user_id
= NULL
;
2079 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2082 enum argpar_state_parse_next_status status
;
2083 const struct argpar_item_opt
*item_opt
;
2086 argpar_state_destroy(argpar_state
);
2087 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2088 add_trigger_options
);
2089 if (!argpar_state
) {
2090 ERR("Failed to create argpar state.");
2094 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2095 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2096 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2099 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2102 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2106 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2108 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2109 const struct argpar_item_non_opt
*item_non_opt
=
2110 (const struct argpar_item_non_opt
*)
2113 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2117 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2119 ingested_args
= argpar_state_get_ingested_orig_args(
2122 my_argc
-= ingested_args
;
2123 my_argv
+= ingested_args
;
2125 switch (item_opt
->descr
->id
) {
2130 case OPT_LIST_OPTIONS
:
2131 list_cmd_options_argpar(stdout
, add_trigger_options
);
2137 ERR("A --condition was already given.");
2141 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2144 * An error message was already printed by
2154 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2157 * An error message was already printed by
2163 ret
= lttng_dynamic_pointer_array_add_pointer(
2166 ERR("Failed to add pointer to pointer array.");
2170 /* Ownership of the action was transferred to the group. */
2177 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2185 if (!assign_string(&user_id
, item_opt
->arg
,
2198 ERR("Missing --condition.");
2202 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2203 ERR("Need at least one --action.");
2207 action_group
= lttng_action_group_create();
2208 if (!action_group
) {
2212 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2213 enum lttng_action_status status
;
2215 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2217 status
= lttng_action_group_add_action(action_group
, action
);
2218 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2223 * The `lttng_action_group_add_action()` takes a reference to
2224 * the action. We can destroy ours.
2226 lttng_action_destroy(action
);
2230 trigger
= lttng_trigger_create(condition
, action_group
);
2236 enum lttng_trigger_status trigger_status
=
2237 lttng_trigger_set_name(trigger
, name
);
2239 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2240 ERR("Failed to set trigger name.");
2246 enum lttng_trigger_status trigger_status
;
2251 uid
= strtol(user_id
, &end
, 10);
2252 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
2253 ERR("Failed to parse `%s` as a user id.", user_id
);
2256 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2257 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2258 ERR("Failed to set trigger's user identity.");
2263 ret
= lttng_register_trigger(trigger
);
2265 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2269 MSG("Trigger registered successfully.");
2277 argpar_state_destroy(argpar_state
);
2278 argpar_item_destroy(argpar_item
);
2279 lttng_dynamic_pointer_array_reset(&actions
);
2280 lttng_condition_destroy(condition
);
2281 lttng_action_destroy(action_group
);
2282 lttng_action_destroy(action
);
2283 lttng_trigger_destroy(trigger
);