2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include "../command.h"
13 #include "../loglevel.h"
14 #include "../uprobe.h"
16 #include "common/argpar/argpar.h"
17 #include "common/dynamic-array.h"
18 #include "common/string-utils/string-utils.h"
19 #include "common/utils.h"
20 /* For lttng_event_rule_type_str(). */
21 #include <lttng/event-rule/event-rule-internal.h>
22 #include <lttng/lttng.h>
23 #include "common/filter/filter-ast.h"
24 #include "common/filter/filter-ir.h"
25 #include "common/dynamic-array.h"
27 #if (LTTNG_SYMBOL_NAME_LEN == 256)
28 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
31 #ifdef LTTNG_EMBED_HELP
32 static const char help_msg
[] =
33 #include <lttng-add-trigger.1.h>
66 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
67 { OPT_FILTER
, 'f', "filter", true },
68 { OPT_NAME
, 'n', "name", true },
69 { OPT_EXCLUDE_NAMES
, 'x', "exclude-names", true },
70 { OPT_LOG_LEVEL
, 'l', "log-level", true },
71 { OPT_EVENT_NAME
, 'E', "event-name", true },
73 { OPT_DOMAIN
, 'd', "domain", true },
74 { OPT_TYPE
, 't', "type", true },
75 { OPT_LOCATION
, 'L', "location", true },
77 /* Capture descriptor */
78 { OPT_CAPTURE
, '\0', "capture", true },
80 ARGPAR_OPT_DESCR_SENTINEL
84 bool assign_domain_type(enum lttng_domain_type
*dest
, const char *arg
)
88 if (*dest
!= LTTNG_DOMAIN_NONE
) {
89 ERR("More than one `--domain` was specified.");
93 if (strcmp(arg
, "kernel") == 0) {
94 *dest
= LTTNG_DOMAIN_KERNEL
;
95 } else if (strcmp(arg
, "user") == 0 || strcmp(arg
, "userspace") == 0) {
96 *dest
= LTTNG_DOMAIN_UST
;
97 } else if (strcmp(arg
, "jul") == 0) {
98 *dest
= LTTNG_DOMAIN_JUL
;
99 } else if (strcmp(arg
, "log4j") == 0) {
100 *dest
= LTTNG_DOMAIN_LOG4J
;
101 } else if (strcmp(arg
, "python") == 0) {
102 *dest
= LTTNG_DOMAIN_PYTHON
;
104 ERR("Invalid `--domain` value: %s", arg
);
119 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
123 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
124 ERR("More than one `--type` was specified.");
128 if (strcmp(arg
, "tracepoint") == 0 || strcmp(arg
, "logging") == 0) {
129 *dest
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
130 } else if (strcmp(arg
, "kprobe") == 0 ||
131 strcmp(arg
, "kernel-probe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
;
133 } else if (strcmp(arg
, "uprobe") == 0 ||
134 strcmp(arg
, "userspace-probe") == 0) {
135 *dest
= LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
;
136 } else if (strcmp(arg
, "function") == 0) {
137 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
;
138 } else if (strncmp(arg
, "syscall", strlen("syscall")) == 0) {
140 * Matches the following:
144 * - syscall:entry+exit
147 * Validation for the right side is left to further usage sites.
149 *dest
= LTTNG_EVENT_RULE_TYPE_SYSCALL
;
151 ERR("Invalid `--type` value: %s", arg
);
166 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
171 ERR("Duplicate '%s' given.", opt_name
);
177 PERROR("Failed to allocate string '%s'.", opt_name
);
191 static bool parse_syscall_emission_site_from_type(const char *str
,
192 enum lttng_event_rule_syscall_emission_site_type
*type
)
195 if (strcmp(str
, "syscall") == 0 ||
196 strcmp(str
, "syscall:entry+exit") == 0) {
197 *type
= LTTNG_EVENT_RULE_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
198 } else if (strcmp(str
, "syscall:entry") == 0) {
199 *type
= LTTNG_EVENT_RULE_SYSCALL_EMISSION_SITE_ENTRY
;
200 } else if (strcmp(str
, "syscall:exit") == 0) {
201 *type
= LTTNG_EVENT_RULE_SYSCALL_EMISSION_SITE_EXIT
;
212 /* This is defined in enable_events.c. */
214 int create_exclusion_list_and_validate(const char *event_name
,
215 const char *exclusions_arg
,
216 char ***exclusion_list
);
219 * Parse `str` as a log level in domain `domain_type`.
221 * Return the log level in `*log_level`. Return true in `*log_level_only` if
222 * the string specifies exactly this log level, false if it specifies at least
225 * Return true if the string was successfully parsed as a log level string.
227 static bool parse_log_level_string(const char *str
,
228 enum lttng_domain_type domain_type
,
230 bool *log_level_only
)
234 switch (domain_type
) {
235 case LTTNG_DOMAIN_UST
:
237 enum lttng_loglevel log_level_min
, log_level_max
;
238 if (!loglevel_parse_range_string(
239 str
, &log_level_min
, &log_level_max
)) {
243 /* Only support VAL and VAL.. for now. */
244 if (log_level_min
!= log_level_max
&&
245 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
249 *log_level
= (int) log_level_min
;
250 *log_level_only
= log_level_min
== log_level_max
;
253 case LTTNG_DOMAIN_LOG4J
:
255 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
256 if (!loglevel_log4j_parse_range_string(
257 str
, &log_level_min
, &log_level_max
)) {
261 /* Only support VAL and VAL.. for now. */
262 if (log_level_min
!= log_level_max
&&
263 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
267 *log_level
= (int) log_level_min
;
268 *log_level_only
= log_level_min
== log_level_max
;
271 case LTTNG_DOMAIN_JUL
:
273 enum lttng_loglevel_jul log_level_min
, log_level_max
;
274 if (!loglevel_jul_parse_range_string(
275 str
, &log_level_min
, &log_level_max
)) {
279 /* Only support VAL and VAL.. for now. */
280 if (log_level_min
!= log_level_max
&&
281 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
285 *log_level
= (int) log_level_min
;
286 *log_level_only
= log_level_min
== log_level_max
;
289 case LTTNG_DOMAIN_PYTHON
:
291 enum lttng_loglevel_python log_level_min
, log_level_max
;
292 if (!loglevel_python_parse_range_string(
293 str
, &log_level_min
, &log_level_max
)) {
297 /* Only support VAL and VAL.. for now. */
298 if (log_level_min
!= log_level_max
&&
300 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
304 *log_level
= (int) log_level_min
;
305 *log_level_only
= log_level_min
== log_level_max
;
309 /* Invalid domain type. */
323 static int parse_kernel_probe_opts(const char *source
,
324 struct lttng_kernel_probe_location
**location
)
329 char name
[LTTNG_SYMBOL_NAME_LEN
];
330 char *symbol_name
= NULL
;
333 /* Check for symbol+offset. */
334 match
= sscanf(source
,
335 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
339 if (*s_hex
== '\0') {
340 ERR("Kernel probe symbol offset is missing.");
344 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
346 PERROR("Failed to copy kernel probe location symbol name.");
349 offset
= strtoul(s_hex
, NULL
, 0);
351 *location
= lttng_kernel_probe_location_symbol_create(
352 symbol_name
, offset
);
354 ERR("Failed to create symbol kernel probe location.");
361 /* Check for symbol. */
362 if (isalpha(name
[0]) || name
[0] == '_') {
363 match
= sscanf(source
,
364 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
368 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
370 ERR("Failed to copy kernel probe location symbol name.");
374 *location
= lttng_kernel_probe_location_symbol_create(
377 ERR("Failed to create symbol kernel probe location.");
385 /* Check for address. */
386 match
= sscanf(source
, "%18s", s_hex
);
390 if (*s_hex
== '\0') {
391 ERR("Invalid kernel probe location address.");
395 address
= strtoul(s_hex
, NULL
, 0);
396 *location
= lttng_kernel_probe_location_address_create(address
);
398 ERR("Failed to create symbol kernel probe location.");
416 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
417 const struct ir_load_expression
*load_expr
,
418 const char *capture_str
)
420 char *provider_name
= NULL
;
421 struct lttng_event_expr
*event_expr
= NULL
;
422 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
423 const enum ir_load_expression_type load_expr_child_type
=
426 switch (load_expr_child_type
) {
427 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
428 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
430 const char *field_name
;
432 load_expr_op
= load_expr_op
->next
;
433 assert(load_expr_op
);
434 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
435 field_name
= load_expr_op
->u
.symbol
;
438 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
439 lttng_event_expr_event_payload_field_create(field_name
) :
440 lttng_event_expr_channel_context_field_create(field_name
);
442 ERR("Failed to create %s event expression: field name = `%s`.",
443 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
444 "payload field" : "channel context",
451 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
454 const char *type_name
;
455 const char *field_name
;
457 load_expr_op
= load_expr_op
->next
;
458 assert(load_expr_op
);
459 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
460 field_name
= load_expr_op
->u
.symbol
;
464 * The field name needs to be of the form PROVIDER:TYPE. We
467 colon
= strchr(field_name
, ':');
469 ERR("Invalid app-specific context field name: missing colon in `%s`.",
474 type_name
= colon
+ 1;
475 if (*type_name
== '\0') {
476 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
481 provider_name
= strndup(field_name
, colon
- field_name
);
482 if (!provider_name
) {
483 PERROR("Failed to allocate field name string");
487 event_expr
= lttng_event_expr_app_specific_context_field_create(
488 provider_name
, type_name
);
490 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
491 provider_name
, type_name
);
498 ERR("%s: unexpected load expr type %d.", __func__
,
503 load_expr_op
= load_expr_op
->next
;
505 /* There may be a single array index after that. */
506 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
507 struct lttng_event_expr
*index_event_expr
;
508 const uint64_t index
= load_expr_op
->u
.index
;
510 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
511 if (!index_event_expr
) {
512 ERR("Failed to create array field element event expression.");
516 event_expr
= index_event_expr
;
517 load_expr_op
= load_expr_op
->next
;
520 switch (load_expr_op
->type
) {
521 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
523 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
524 * always found at the end of the chain.
527 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
528 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
533 ERR("%s: unexpected load expression operator %s.", __func__
,
534 ir_load_expression_type_str(load_expr_op
->type
));
541 lttng_event_expr_destroy(event_expr
);
551 struct lttng_event_expr
*ir_op_load_to_event_expr(
552 const struct ir_op
*ir
, const char *capture_str
)
554 struct lttng_event_expr
*event_expr
= NULL
;
556 assert(ir
->op
== IR_OP_LOAD
);
558 switch (ir
->data_type
) {
559 case IR_DATA_EXPRESSION
:
561 const struct ir_load_expression
*ir_load_expr
=
562 ir
->u
.load
.u
.expression
;
564 event_expr
= ir_op_load_expr_to_event_expr(
565 ir_load_expr
, capture_str
);
569 ERR("%s: unexpected data type: %s.", __func__
,
570 ir_data_type_str(ir
->data_type
));
578 const char *ir_operator_type_human_str(enum ir_op_type op
)
600 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
601 const char *capture_str
)
603 struct lttng_event_expr
*event_expr
= NULL
;
605 assert(ir
->op
== IR_OP_ROOT
);
606 ir
= ir
->u
.root
.child
;
610 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
615 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
617 ir_operator_type_human_str(ir
->op
));
620 ERR("%s: unexpected IR op type: %s.", __func__
,
621 ir_op_type_str(ir
->op
));
629 void destroy_event_expr(void *ptr
)
631 lttng_event_expr_destroy(ptr
);
634 struct parse_event_rule_res
{
636 struct lttng_event_rule
*er
;
638 /* Array of `struct lttng_event_expr *` */
639 struct lttng_dynamic_pointer_array capture_descriptors
;
643 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
645 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
646 enum lttng_event_rule_type event_rule_type
=
647 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
648 struct argpar_state
*state
;
649 struct argpar_item
*item
= NULL
;
651 int consumed_args
= -1;
652 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
653 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
654 struct parse_event_rule_res res
= { 0 };
655 struct lttng_event_expr
*event_expr
= NULL
;
656 struct filter_parser_ctx
*parser_ctx
= NULL
;
657 struct lttng_log_level_rule
*log_level_rule
= NULL
;
659 /* Event rule type option */
660 char *event_rule_type_str
= NULL
;
662 /* Tracepoint and syscall options. */
664 char *exclude_names
= NULL
;
665 char **exclusion_list
= NULL
;
667 /* For userspace / kernel probe and function. */
668 char *location
= NULL
;
669 char *event_name
= NULL
;
675 char *log_level_str
= NULL
;
677 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
679 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
681 ERR("Failed to allocate an argpar state.");
686 enum argpar_state_parse_next_status status
;
688 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
689 status
= argpar_state_parse_next(state
, &item
, &error
);
690 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
693 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
694 /* Just stop parsing here. */
696 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
700 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
702 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
703 const struct argpar_item_opt
*item_opt
=
704 (const struct argpar_item_opt
*) item
;
706 switch (item_opt
->descr
->id
) {
709 if (!assign_domain_type(&domain_type
,
716 if (!assign_event_rule_type(&event_rule_type
,
721 /* Save the string for later use. */
722 if (!assign_string(&event_rule_type_str
,
730 if (!assign_string(&location
,
738 if (!assign_string(&event_name
,
740 "--event-name/-E")) {
746 if (!assign_string(&filter
, item_opt
->arg
,
753 if (!assign_string(&name
, item_opt
->arg
,
759 case OPT_EXCLUDE_NAMES
:
760 if (!assign_string(&exclude_names
,
762 "--exclude-names/-x")) {
768 if (!assign_string(&log_level_str
,
769 item_opt
->arg
, "--log-level/-l")) {
777 const char *capture_str
= item_opt
->arg
;
779 ret
= filter_parser_ctx_create_from_filter_expression(
780 capture_str
, &parser_ctx
);
782 ERR("Failed to parse capture expression `%s`.",
787 event_expr
= ir_op_root_to_event_expr(
792 * ir_op_root_to_event_expr has printed
798 ret
= lttng_dynamic_pointer_array_add_pointer(
799 &res
.capture_descriptors
,
806 * The ownership of event expression was
807 * transferred to the dynamic array.
817 const struct argpar_item_non_opt
*item_non_opt
=
818 (const struct argpar_item_non_opt
*)
821 /* Don't accept non-option arguments. */
822 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
827 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
828 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
832 * Option --name is applicable to event rules of type tracepoint
833 * and syscall. For tracepoint and syscall rules, if --name is
834 * omitted, it is implicitly "*".
836 switch (event_rule_type
) {
837 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
838 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
846 ERR("Can't use --name with %s event rules.",
847 lttng_event_rule_type_str(
853 ERR("Can't use --exclude-names/-x with %s event rules.",
854 lttng_event_rule_type_str(
861 * Option --location is only applicable to (and mandatory for) event
862 * rules of type {k,u}probe and function.
864 * Option --event-name is only applicable to event rules of type probe.
865 * If omitted, it defaults to the location.
867 switch (event_rule_type
) {
868 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
869 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
870 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
872 ERR("Event rule of type %s requires a --location.",
873 lttng_event_rule_type_str(event_rule_type
));
878 event_name
= strdup(location
);
885 ERR("Can't use --location with %s event rules.",
886 lttng_event_rule_type_str(event_rule_type
));
891 ERR("Can't use --event-name with %s event rules.",
892 lttng_event_rule_type_str(
899 * Update *argc and *argv so our caller can keep parsing what follows.
901 consumed_args
= argpar_state_get_ingested_orig_args(state
);
902 assert(consumed_args
>= 0);
903 *argc
-= consumed_args
;
904 *argv
+= consumed_args
;
906 /* Need to specify a domain. */
907 if (domain_type
== LTTNG_DOMAIN_NONE
) {
908 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
912 /* Validate event rule type against domain. */
913 switch (event_rule_type
) {
914 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
915 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
916 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
917 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
918 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
919 ERR("Event type not available for user-space tracing.");
924 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
932 * Adding a filter to a probe, function or userspace-probe would be
933 * denied by the kernel tracer as it's not supported at the moment. We
934 * do an early check here to warn the user.
936 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
937 switch (event_rule_type
) {
938 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
939 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
942 ERR("Filter expressions are not supported for %s event rules.",
943 lttng_event_rule_type_str(event_rule_type
));
948 /* If --exclude/-x was passed, split it into an exclusion list. */
950 if (domain_type
!= LTTNG_DOMAIN_UST
) {
951 ERR("Event name exclusions are not yet implemented for %s event rules.",
952 get_domain_str(domain_type
));
956 if (create_exclusion_list_and_validate(name
,
957 exclude_names
, &exclusion_list
) != 0) {
958 ERR("Failed to create exclusion list.");
964 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
965 ERR("Log levels are only applicable to tracepoint event rules.");
969 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
970 ERR("Log levels are not supported by the kernel tracer.");
975 /* Finally, create the event rule object. */
976 switch (event_rule_type
) {
977 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
979 enum lttng_event_rule_status event_rule_status
;
981 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
983 ERR("Failed to create tracepoint event rule.");
988 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
990 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
991 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
998 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1000 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1001 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1007 /* Set exclusion list. */
1008 if (exclusion_list
) {
1011 for (n
= 0; exclusion_list
[n
]; n
++) {
1012 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
1015 if (event_rule_status
!=
1016 LTTNG_EVENT_RULE_STATUS_OK
) {
1017 ERR("Failed to set tracepoint exclusion list element '%s'",
1025 * ".." is the same as passing no log level option and
1026 * correspond the the "ANY" case.
1028 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
1030 bool log_level_only
;
1032 if (!parse_log_level_string(log_level_str
, domain_type
,
1033 &log_level
, &log_level_only
)) {
1034 ERR("Failed to parse log level string `%s`.",
1039 if (log_level_only
) {
1040 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
1042 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1045 if (log_level_rule
== NULL
) {
1046 ERR("Failed to create log level rule object.");
1051 lttng_event_rule_tracepoint_set_log_level_rule(
1052 res
.er
, log_level_rule
);
1054 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1055 ERR("Failed to set log level on event fule.");
1062 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1065 enum lttng_event_rule_status event_rule_status
;
1067 ret
= parse_kernel_probe_opts(
1068 location
, &kernel_probe_location
);
1070 ERR("Failed to parse kernel probe location.");
1074 assert(kernel_probe_location
);
1075 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1077 ERR("Failed to create kprobe event rule.");
1082 lttng_event_rule_kernel_probe_set_event_name(
1083 res
.er
, event_name
);
1084 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1085 ERR("Failed to set kprobe event rule's name to '%s'.",
1092 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1095 enum lttng_event_rule_status event_rule_status
;
1097 ret
= parse_userspace_probe_opts(
1098 location
, &userspace_probe_location
);
1100 ERR("Failed to parse user space probe location.");
1104 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1106 ERR("Failed to create userspace probe event rule.");
1111 lttng_event_rule_userspace_probe_set_event_name(
1112 res
.er
, event_name
);
1113 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1114 ERR("Failed to set user space probe event rule's name to '%s'.",
1121 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1123 enum lttng_event_rule_status event_rule_status
;
1124 enum lttng_event_rule_syscall_emission_site_type emission_site_type
;
1126 if (!parse_syscall_emission_site_from_type(
1127 event_rule_type_str
, &emission_site_type
)) {
1128 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1132 res
.er
= lttng_event_rule_syscall_create(emission_site_type
);
1134 ERR("Failed to create syscall event rule.");
1138 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1140 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1141 ERR("Failed to set syscall event rule's pattern to '%s'.",
1147 event_rule_status
= lttng_event_rule_syscall_set_filter(
1149 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1150 ERR("Failed to set syscall event rule's filter to '%s'.",
1166 lttng_event_rule_destroy(res
.er
);
1168 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1172 filter_parser_ctx_free(parser_ctx
);
1175 lttng_event_expr_destroy(event_expr
);
1176 argpar_item_destroy(item
);
1178 argpar_state_destroy(state
);
1181 free(exclude_names
);
1182 free(log_level_str
);
1185 free(event_rule_type_str
);
1187 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1188 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1189 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1190 lttng_log_level_rule_destroy(log_level_rule
);
1195 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1197 struct parse_event_rule_res res
;
1198 struct lttng_condition
*c
;
1201 res
= parse_event_rule(argc
, argv
);
1207 c
= lttng_condition_event_rule_matches_create(res
.er
);
1208 lttng_event_rule_destroy(res
.er
);
1214 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1216 enum lttng_condition_status status
;
1217 struct lttng_event_expr
**expr
=
1218 lttng_dynamic_array_get_element(
1219 &res
.capture_descriptors
.array
, i
);
1223 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1225 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1226 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1227 ERR("The capture feature is unsupported by the event-rule condition type");
1233 /* Ownership of event expression moved to `c` */
1240 lttng_condition_destroy(c
);
1244 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1245 lttng_event_rule_destroy(res
.er
);
1249 struct condition_descr
{
1251 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1255 struct condition_descr condition_descrs
[] = {
1256 { "event-rule-matches", handle_condition_event
},
1260 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1264 struct lttng_condition
*cond
;
1265 const struct condition_descr
*descr
= NULL
;
1267 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1268 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1269 descr
= &condition_descrs
[i
];
1275 ERR("Unknown condition name '%s'", condition_name
);
1279 cond
= descr
->handler(argc
, argv
);
1281 /* The handler has already printed an error message. */
1292 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1295 char **tokens
= NULL
;
1296 struct lttng_rate_policy
*policy
= NULL
;
1297 enum lttng_rate_policy_type policy_type
;
1298 unsigned long long value
;
1299 char *policy_type_str
;
1300 char *policy_value_str
;
1305 * rate policy fields are separated by ':'.
1307 tokens
= strutils_split(policy_str
, ':', 1);
1308 num_token
= strutils_array_of_strings_len(tokens
);
1311 * Early sanity check that the number of parameter is exactly 2.
1314 if (num_token
!= 2) {
1315 ERR("Rate policy format is invalid.");
1319 policy_type_str
= tokens
[0];
1320 policy_value_str
= tokens
[1];
1322 /* Parse the type. */
1323 if (strcmp(policy_type_str
, "once-after") == 0) {
1324 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1325 } else if (strcmp(policy_type_str
, "every") == 0) {
1326 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1328 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1332 /* Parse the value. */
1333 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1334 ERR("Failed to parse rate policy value `%s` as an integer.",
1340 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1344 switch (policy_type
) {
1345 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1346 policy
= lttng_rate_policy_every_n_create(value
);
1348 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1349 policy
= lttng_rate_policy_once_after_n_create(value
);
1355 if (policy
== NULL
) {
1356 ERR("Failed to create rate policy `%s`.", policy_str
);
1360 strutils_free_null_terminated_array_of_strings(tokens
);
1364 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1365 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1366 ARGPAR_OPT_DESCR_SENTINEL
1370 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1372 struct lttng_action
*action
= NULL
;
1373 struct argpar_state
*state
= NULL
;
1374 struct argpar_item
*item
= NULL
;
1376 struct lttng_rate_policy
*policy
= NULL
;
1378 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1380 ERR("Failed to allocate an argpar state.");
1385 enum argpar_state_parse_next_status status
;
1387 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1388 status
= argpar_state_parse_next(state
, &item
, &error
);
1389 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1392 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1393 /* Just stop parsing here. */
1395 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1399 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1401 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1402 const struct argpar_item_opt
*item_opt
=
1403 (const struct argpar_item_opt
*) item
;
1405 switch (item_opt
->descr
->id
) {
1406 case OPT_RATE_POLICY
:
1408 policy
= parse_rate_policy(item_opt
->arg
);
1418 const struct argpar_item_non_opt
*item_non_opt
;
1420 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1422 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1424 switch (item_non_opt
->non_opt_index
) {
1426 ERR("Unexpected argument `%s`.",
1433 *argc
-= argpar_state_get_ingested_orig_args(state
);
1434 *argv
+= argpar_state_get_ingested_orig_args(state
);
1436 action
= lttng_action_notify_create();
1438 ERR("Failed to create notify action");
1443 enum lttng_action_status status
;
1444 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1445 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1446 ERR("Failed to set rate policy");
1454 lttng_action_destroy(action
);
1458 lttng_rate_policy_destroy(policy
);
1459 argpar_state_destroy(state
);
1460 argpar_item_destroy(item
);
1465 * Generic handler for a kind of action that takes a session name and an
1466 * optional rate policy.
1469 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1471 struct lttng_action
*(*create_action_cb
)(void),
1472 enum lttng_action_status (*set_session_name_cb
)(
1473 struct lttng_action
*, const char *),
1474 enum lttng_action_status (*set_rate_policy_cb
)(
1475 struct lttng_action
*,
1476 const struct lttng_rate_policy
*),
1477 const char *action_name
)
1479 struct lttng_action
*action
= NULL
;
1480 struct argpar_state
*state
= NULL
;
1481 struct argpar_item
*item
= NULL
;
1482 const char *session_name_arg
= NULL
;
1484 enum lttng_action_status action_status
;
1485 struct lttng_rate_policy
*policy
= NULL
;
1487 assert(set_session_name_cb
);
1488 assert(set_rate_policy_cb
);
1490 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1491 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1492 ARGPAR_OPT_DESCR_SENTINEL
1495 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1497 ERR("Failed to allocate an argpar state.");
1502 enum argpar_state_parse_next_status status
;
1504 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1505 status
= argpar_state_parse_next(state
, &item
, &error
);
1506 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1509 } else if (status
==
1510 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1511 /* Just stop parsing here. */
1513 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1517 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1518 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1519 const struct argpar_item_opt
*item_opt
=
1520 (const struct argpar_item_opt
*) item
;
1522 switch (item_opt
->descr
->id
) {
1523 case OPT_RATE_POLICY
:
1525 policy
= parse_rate_policy(item_opt
->arg
);
1535 const struct argpar_item_non_opt
*item_non_opt
;
1536 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1538 switch (item_non_opt
->non_opt_index
) {
1540 session_name_arg
= item_non_opt
->arg
;
1543 ERR("Unexpected argument `%s`.",
1550 *argc
-= argpar_state_get_ingested_orig_args(state
);
1551 *argv
+= argpar_state_get_ingested_orig_args(state
);
1553 if (!session_name_arg
) {
1554 ERR("Missing session name.");
1558 action
= create_action_cb();
1560 ERR("Failed to allocate %s session action.", action_name
);
1564 action_status
= set_session_name_cb(action
, session_name_arg
);
1565 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1566 ERR("Failed to set action %s session's session name to '%s'.",
1567 action_name
, session_name_arg
);
1572 action_status
= set_rate_policy_cb(action
, policy
);
1573 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1574 ERR("Failed to set rate policy");
1582 lttng_action_destroy(action
);
1584 argpar_item_destroy(item
);
1586 lttng_rate_policy_destroy(policy
);
1588 argpar_state_destroy(state
);
1593 struct lttng_action
*handle_action_start_session(int *argc
,
1596 return handle_action_simple_session_with_policy(argc
, argv
,
1597 lttng_action_start_session_create
,
1598 lttng_action_start_session_set_session_name
,
1599 lttng_action_start_session_set_rate_policy
, "start");
1603 struct lttng_action
*handle_action_stop_session(int *argc
,
1606 return handle_action_simple_session_with_policy(argc
, argv
,
1607 lttng_action_stop_session_create
,
1608 lttng_action_stop_session_set_session_name
,
1609 lttng_action_stop_session_set_rate_policy
, "stop");
1613 struct lttng_action
*handle_action_rotate_session(int *argc
,
1616 return handle_action_simple_session_with_policy(argc
, argv
,
1617 lttng_action_rotate_session_create
,
1618 lttng_action_rotate_session_set_session_name
,
1619 lttng_action_rotate_session_set_rate_policy
,
1623 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1624 { OPT_NAME
, 'n', "name", true },
1625 { OPT_MAX_SIZE
, 'm', "max-size", true },
1626 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1627 { OPT_DATA_URL
, '\0', "data-url", true },
1628 { OPT_URL
, '\0', "url", true },
1629 { OPT_PATH
, '\0', "path", true },
1630 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1631 ARGPAR_OPT_DESCR_SENTINEL
1635 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1638 struct lttng_action
*action
= NULL
;
1639 struct argpar_state
*state
= NULL
;
1640 struct argpar_item
*item
= NULL
;
1641 const char *session_name_arg
= NULL
;
1642 char *snapshot_name_arg
= NULL
;
1643 char *ctrl_url_arg
= NULL
;
1644 char *data_url_arg
= NULL
;
1645 char *max_size_arg
= NULL
;
1646 char *url_arg
= NULL
;
1647 char *path_arg
= NULL
;
1649 enum lttng_action_status action_status
;
1650 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1651 struct lttng_rate_policy
*policy
= NULL
;
1653 unsigned int locations_specified
= 0;
1655 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1657 ERR("Failed to allocate an argpar state.");
1662 enum argpar_state_parse_next_status status
;
1664 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1665 status
= argpar_state_parse_next(state
, &item
, &error
);
1666 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1669 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1670 /* Just stop parsing here. */
1672 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1676 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1678 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1679 const struct argpar_item_opt
*item_opt
=
1680 (const struct argpar_item_opt
*) item
;
1682 switch (item_opt
->descr
->id
) {
1684 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1690 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1696 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1702 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1708 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1714 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1719 case OPT_RATE_POLICY
:
1721 policy
= parse_rate_policy(item_opt
->arg
);
1731 const struct argpar_item_non_opt
*item_non_opt
;
1733 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1735 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1737 switch (item_non_opt
->non_opt_index
) {
1739 session_name_arg
= item_non_opt
->arg
;
1742 ERR("Unexpected argument `%s`.",
1749 *argc
-= argpar_state_get_ingested_orig_args(state
);
1750 *argv
+= argpar_state_get_ingested_orig_args(state
);
1752 if (!session_name_arg
) {
1753 ERR("Missing session name.");
1757 /* --ctrl-url and --data-url must come in pair. */
1758 if (ctrl_url_arg
&& !data_url_arg
) {
1759 ERR("--ctrl-url is specified, but --data-url is missing.");
1763 if (!ctrl_url_arg
&& data_url_arg
) {
1764 ERR("--data-url is specified, but --ctrl-url is missing.");
1768 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1769 locations_specified
+= !!url_arg
;
1770 locations_specified
+= !!path_arg
;
1772 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1773 if (locations_specified
> 1) {
1774 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1779 * Did the user specify an option that implies using a
1780 * custom/unregistered output?
1782 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1783 snapshot_output
= lttng_snapshot_output_create();
1784 if (!snapshot_output
) {
1785 ERR("Failed to allocate a snapshot output.");
1790 action
= lttng_action_snapshot_session_create();
1792 ERR("Failed to allocate snapshot session action.");
1796 action_status
= lttng_action_snapshot_session_set_session_name(
1797 action
, session_name_arg
);
1798 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1799 ERR("Failed to set action snapshot session's session name to '%s'.",
1804 if (snapshot_name_arg
) {
1805 if (!snapshot_output
) {
1806 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1810 ret
= lttng_snapshot_output_set_name(
1811 snapshot_name_arg
, snapshot_output
);
1813 ERR("Failed to set name of snapshot output.");
1821 if (!snapshot_output
) {
1822 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1826 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1828 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1832 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1834 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1842 struct lttng_uri
*uris
;
1844 if (!strstr(url_arg
, "://")) {
1845 ERR("Failed to parse '%s' as an URL.", url_arg
);
1849 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1851 ERR("Failed to parse '%s' as an URL.", url_arg
);
1855 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1856 ret
= lttng_snapshot_output_set_local_path(
1857 uris
[0].dst
.path
, snapshot_output
);
1860 ERR("Failed to assign '%s' as a local destination.",
1865 ret
= lttng_snapshot_output_set_network_url(
1866 url_arg
, snapshot_output
);
1869 ERR("Failed to assign '%s' as a network URL.",
1877 ret
= lttng_snapshot_output_set_local_path(
1878 path_arg
, snapshot_output
);
1880 ERR("Failed to parse '%s' as a local path.", path_arg
);
1887 * Two argument form, network output with separate control and
1890 ret
= lttng_snapshot_output_set_network_urls(
1891 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1893 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1894 ctrl_url_arg
, data_url_arg
);
1899 if (snapshot_output
) {
1900 action_status
= lttng_action_snapshot_session_set_output(
1901 action
, snapshot_output
);
1902 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1903 ERR("Failed to set snapshot session action's output.");
1907 /* Ownership of `snapshot_output` has been transferred to the action. */
1908 snapshot_output
= NULL
;
1912 enum lttng_action_status status
;
1913 status
= lttng_action_snapshot_session_set_rate_policy(
1915 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1916 ERR("Failed to set rate policy");
1924 lttng_action_destroy(action
);
1928 free(snapshot_name_arg
);
1933 free(snapshot_output
);
1935 lttng_rate_policy_destroy(policy
);
1936 argpar_state_destroy(state
);
1937 argpar_item_destroy(item
);
1941 struct action_descr
{
1943 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1947 struct action_descr action_descrs
[] = {
1948 { "notify", handle_action_notify
},
1949 { "start-session", handle_action_start_session
},
1950 { "stop-session", handle_action_stop_session
},
1951 { "rotate-session", handle_action_rotate_session
},
1952 { "snapshot-session", handle_action_snapshot_session
},
1956 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1959 struct lttng_action
*action
;
1960 const struct action_descr
*descr
= NULL
;
1962 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1963 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1964 descr
= &action_descrs
[i
];
1970 ERR("Unknown action name: %s", action_name
);
1974 action
= descr
->handler(argc
, argv
);
1976 /* The handler has already printed an error message. */
1988 struct argpar_opt_descr add_trigger_options
[] = {
1989 { OPT_HELP
, 'h', "help", false },
1990 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
1991 { OPT_CONDITION
, '\0', "condition", true },
1992 { OPT_ACTION
, '\0', "action", true },
1993 { OPT_NAME
, '\0', "name", true },
1994 { OPT_OWNER_UID
, '\0', "owner-uid", true },
1995 ARGPAR_OPT_DESCR_SENTINEL
,
1999 void lttng_actions_destructor(void *p
)
2001 struct lttng_action
*action
= p
;
2003 lttng_action_destroy(action
);
2006 int cmd_add_trigger(int argc
, const char **argv
)
2009 int my_argc
= argc
- 1;
2010 const char **my_argv
= argv
+ 1;
2011 struct lttng_condition
*condition
= NULL
;
2012 struct lttng_dynamic_pointer_array actions
;
2013 struct argpar_state
*argpar_state
= NULL
;
2014 struct argpar_item
*argpar_item
= NULL
;
2015 struct lttng_action
*action_group
= NULL
;
2016 struct lttng_action
*action
= NULL
;
2017 struct lttng_trigger
*trigger
= NULL
;
2021 char *owner_uid
= NULL
;
2023 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2026 enum argpar_state_parse_next_status status
;
2027 const struct argpar_item_opt
*item_opt
;
2030 argpar_state_destroy(argpar_state
);
2031 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2032 add_trigger_options
);
2033 if (!argpar_state
) {
2034 ERR("Failed to create argpar state.");
2038 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2039 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2040 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2043 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2046 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2050 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2052 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2053 const struct argpar_item_non_opt
*item_non_opt
=
2054 (const struct argpar_item_non_opt
*)
2057 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2061 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2063 ingested_args
= argpar_state_get_ingested_orig_args(
2066 my_argc
-= ingested_args
;
2067 my_argv
+= ingested_args
;
2069 switch (item_opt
->descr
->id
) {
2074 case OPT_LIST_OPTIONS
:
2075 list_cmd_options_argpar(stdout
, add_trigger_options
);
2081 ERR("A --condition was already given.");
2085 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2088 * An error message was already printed by
2098 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2101 * An error message was already printed by
2107 ret
= lttng_dynamic_pointer_array_add_pointer(
2110 ERR("Failed to add pointer to pointer array.");
2114 /* Ownership of the action was transferred to the group. */
2121 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2129 if (!assign_string(&owner_uid
, item_opt
->arg
,
2142 ERR("Missing --condition.");
2146 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2147 ERR("Need at least one --action.");
2151 action_group
= lttng_action_group_create();
2152 if (!action_group
) {
2156 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2157 enum lttng_action_status status
;
2159 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2161 status
= lttng_action_group_add_action(action_group
, action
);
2162 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2167 * The `lttng_action_group_add_action()` takes a reference to
2168 * the action. We can destroy ours.
2170 lttng_action_destroy(action
);
2174 trigger
= lttng_trigger_create(condition
, action_group
);
2180 enum lttng_trigger_status trigger_status
=
2181 lttng_trigger_set_name(trigger
, name
);
2183 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2184 ERR("Failed to set trigger name.");
2190 enum lttng_trigger_status trigger_status
;
2195 uid
= strtol(owner_uid
, &end
, 10);
2196 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2197 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2200 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2201 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2202 ERR("Failed to set trigger's user identity.");
2207 ret
= lttng_register_trigger(trigger
);
2209 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2213 MSG("Trigger registered successfully.");
2221 argpar_state_destroy(argpar_state
);
2222 argpar_item_destroy(argpar_item
);
2223 lttng_dynamic_pointer_array_reset(&actions
);
2224 lttng_condition_destroy(condition
);
2225 lttng_action_destroy(action_group
);
2226 lttng_action_destroy(action
);
2227 lttng_trigger_destroy(trigger
);