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_NAME
, 'x', "exclude-name", 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
;
213 * Parse `str` as a log level in domain `domain_type`.
215 * Return the log level in `*log_level`. Return true in `*log_level_only` if
216 * the string specifies exactly this log level, false if it specifies at least
219 * Return true if the string was successfully parsed as a log level string.
221 static bool parse_log_level_string(const char *str
,
222 enum lttng_domain_type domain_type
,
224 bool *log_level_only
)
228 switch (domain_type
) {
229 case LTTNG_DOMAIN_UST
:
231 enum lttng_loglevel log_level_min
, log_level_max
;
232 if (!loglevel_parse_range_string(
233 str
, &log_level_min
, &log_level_max
)) {
237 /* Only support VAL and VAL.. for now. */
238 if (log_level_min
!= log_level_max
&&
239 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
243 *log_level
= (int) log_level_min
;
244 *log_level_only
= log_level_min
== log_level_max
;
247 case LTTNG_DOMAIN_LOG4J
:
249 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
250 if (!loglevel_log4j_parse_range_string(
251 str
, &log_level_min
, &log_level_max
)) {
255 /* Only support VAL and VAL.. for now. */
256 if (log_level_min
!= log_level_max
&&
257 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
261 *log_level
= (int) log_level_min
;
262 *log_level_only
= log_level_min
== log_level_max
;
265 case LTTNG_DOMAIN_JUL
:
267 enum lttng_loglevel_jul log_level_min
, log_level_max
;
268 if (!loglevel_jul_parse_range_string(
269 str
, &log_level_min
, &log_level_max
)) {
273 /* Only support VAL and VAL.. for now. */
274 if (log_level_min
!= log_level_max
&&
275 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
279 *log_level
= (int) log_level_min
;
280 *log_level_only
= log_level_min
== log_level_max
;
283 case LTTNG_DOMAIN_PYTHON
:
285 enum lttng_loglevel_python log_level_min
, log_level_max
;
286 if (!loglevel_python_parse_range_string(
287 str
, &log_level_min
, &log_level_max
)) {
291 /* Only support VAL and VAL.. for now. */
292 if (log_level_min
!= log_level_max
&&
294 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
298 *log_level
= (int) log_level_min
;
299 *log_level_only
= log_level_min
== log_level_max
;
303 /* Invalid domain type. */
317 static int parse_kernel_probe_opts(const char *source
,
318 struct lttng_kernel_probe_location
**location
)
323 char name
[LTTNG_SYMBOL_NAME_LEN
];
324 char *symbol_name
= NULL
;
327 /* Check for symbol+offset. */
328 match
= sscanf(source
,
329 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
333 if (*s_hex
== '\0') {
334 ERR("Kernel probe symbol offset is missing.");
338 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
340 PERROR("Failed to copy kernel probe location symbol name.");
343 offset
= strtoul(s_hex
, NULL
, 0);
345 *location
= lttng_kernel_probe_location_symbol_create(
346 symbol_name
, offset
);
348 ERR("Failed to create symbol kernel probe location.");
355 /* Check for symbol. */
356 if (isalpha(name
[0]) || name
[0] == '_') {
357 match
= sscanf(source
,
358 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
362 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
364 ERR("Failed to copy kernel probe location symbol name.");
368 *location
= lttng_kernel_probe_location_symbol_create(
371 ERR("Failed to create symbol kernel probe location.");
379 /* Check for address. */
380 match
= sscanf(source
, "%18s", s_hex
);
384 if (*s_hex
== '\0') {
385 ERR("Invalid kernel probe location address.");
389 address
= strtoul(s_hex
, NULL
, 0);
390 *location
= lttng_kernel_probe_location_address_create(address
);
392 ERR("Failed to create symbol kernel probe location.");
410 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
411 const struct ir_load_expression
*load_expr
,
412 const char *capture_str
)
414 char *provider_name
= NULL
;
415 struct lttng_event_expr
*event_expr
= NULL
;
416 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
417 const enum ir_load_expression_type load_expr_child_type
=
420 switch (load_expr_child_type
) {
421 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
422 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
424 const char *field_name
;
426 load_expr_op
= load_expr_op
->next
;
427 assert(load_expr_op
);
428 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
429 field_name
= load_expr_op
->u
.symbol
;
432 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
433 lttng_event_expr_event_payload_field_create(field_name
) :
434 lttng_event_expr_channel_context_field_create(field_name
);
436 ERR("Failed to create %s event expression: field name = `%s`.",
437 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
438 "payload field" : "channel context",
445 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
448 const char *type_name
;
449 const char *field_name
;
451 load_expr_op
= load_expr_op
->next
;
452 assert(load_expr_op
);
453 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
454 field_name
= load_expr_op
->u
.symbol
;
458 * The field name needs to be of the form PROVIDER:TYPE. We
461 colon
= strchr(field_name
, ':');
463 ERR("Invalid app-specific context field name: missing colon in `%s`.",
468 type_name
= colon
+ 1;
469 if (*type_name
== '\0') {
470 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
475 provider_name
= strndup(field_name
, colon
- field_name
);
476 if (!provider_name
) {
477 PERROR("Failed to allocate field name string");
481 event_expr
= lttng_event_expr_app_specific_context_field_create(
482 provider_name
, type_name
);
484 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
485 provider_name
, type_name
);
492 ERR("%s: unexpected load expr type %d.", __func__
,
497 load_expr_op
= load_expr_op
->next
;
499 /* There may be a single array index after that. */
500 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
501 struct lttng_event_expr
*index_event_expr
;
502 const uint64_t index
= load_expr_op
->u
.index
;
504 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
505 if (!index_event_expr
) {
506 ERR("Failed to create array field element event expression.");
510 event_expr
= index_event_expr
;
511 load_expr_op
= load_expr_op
->next
;
514 switch (load_expr_op
->type
) {
515 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
517 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
518 * always found at the end of the chain.
521 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
522 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
527 ERR("%s: unexpected load expression operator %s.", __func__
,
528 ir_load_expression_type_str(load_expr_op
->type
));
535 lttng_event_expr_destroy(event_expr
);
545 struct lttng_event_expr
*ir_op_load_to_event_expr(
546 const struct ir_op
*ir
, const char *capture_str
)
548 struct lttng_event_expr
*event_expr
= NULL
;
550 assert(ir
->op
== IR_OP_LOAD
);
552 switch (ir
->data_type
) {
553 case IR_DATA_EXPRESSION
:
555 const struct ir_load_expression
*ir_load_expr
=
556 ir
->u
.load
.u
.expression
;
558 event_expr
= ir_op_load_expr_to_event_expr(
559 ir_load_expr
, capture_str
);
563 ERR("%s: unexpected data type: %s.", __func__
,
564 ir_data_type_str(ir
->data_type
));
572 const char *ir_operator_type_human_str(enum ir_op_type op
)
594 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
595 const char *capture_str
)
597 struct lttng_event_expr
*event_expr
= NULL
;
599 assert(ir
->op
== IR_OP_ROOT
);
600 ir
= ir
->u
.root
.child
;
604 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
609 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
611 ir_operator_type_human_str(ir
->op
));
614 ERR("%s: unexpected IR op type: %s.", __func__
,
615 ir_op_type_str(ir
->op
));
623 void destroy_event_expr(void *ptr
)
625 lttng_event_expr_destroy(ptr
);
628 struct parse_event_rule_res
{
630 struct lttng_event_rule
*er
;
632 /* Array of `struct lttng_event_expr *` */
633 struct lttng_dynamic_pointer_array capture_descriptors
;
637 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
639 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
640 enum lttng_event_rule_type event_rule_type
=
641 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
642 struct argpar_state
*state
;
643 struct argpar_item
*item
= NULL
;
645 int consumed_args
= -1;
646 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
647 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
648 struct parse_event_rule_res res
= { 0 };
649 struct lttng_event_expr
*event_expr
= NULL
;
650 struct filter_parser_ctx
*parser_ctx
= NULL
;
651 struct lttng_log_level_rule
*log_level_rule
= NULL
;
653 /* Event rule type option */
654 char *event_rule_type_str
= NULL
;
656 /* Tracepoint and syscall options. */
658 /* Array of strings. */
659 struct lttng_dynamic_pointer_array exclude_names
;
661 /* For userspace / kernel probe and function. */
662 char *location
= NULL
;
663 char *event_name
= NULL
;
669 char *log_level_str
= NULL
;
671 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
674 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
676 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
678 ERR("Failed to allocate an argpar state.");
683 enum argpar_state_parse_next_status status
;
685 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
686 status
= argpar_state_parse_next(state
, &item
, &error
);
687 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
690 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
691 /* Just stop parsing here. */
693 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
697 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
699 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
700 const struct argpar_item_opt
*item_opt
=
701 (const struct argpar_item_opt
*) item
;
703 switch (item_opt
->descr
->id
) {
706 if (!assign_domain_type(&domain_type
,
713 if (!assign_event_rule_type(&event_rule_type
,
718 /* Save the string for later use. */
719 if (!assign_string(&event_rule_type_str
,
727 if (!assign_string(&location
,
735 if (!assign_string(&event_name
,
737 "--event-name/-E")) {
743 if (!assign_string(&filter
, item_opt
->arg
,
750 if (!assign_string(&name
, item_opt
->arg
,
756 case OPT_EXCLUDE_NAME
:
760 ret
= lttng_dynamic_pointer_array_add_pointer(
762 strdup(item_opt
->arg
));
764 ERR("Failed to add pointer to dynamic pointer array.");
771 if (!assign_string(&log_level_str
,
772 item_opt
->arg
, "--log-level/-l")) {
780 const char *capture_str
= item_opt
->arg
;
782 ret
= filter_parser_ctx_create_from_filter_expression(
783 capture_str
, &parser_ctx
);
785 ERR("Failed to parse capture expression `%s`.",
790 event_expr
= ir_op_root_to_event_expr(
795 * ir_op_root_to_event_expr has printed
801 ret
= lttng_dynamic_pointer_array_add_pointer(
802 &res
.capture_descriptors
,
809 * The ownership of event expression was
810 * transferred to the dynamic array.
820 const struct argpar_item_non_opt
*item_non_opt
=
821 (const struct argpar_item_non_opt
*)
824 /* Don't accept non-option arguments. */
825 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
830 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
831 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
835 * Option --name is applicable to event rules of type tracepoint
836 * and syscall. For tracepoint and syscall rules, if --name is
837 * omitted, it is implicitly "*".
839 switch (event_rule_type
) {
840 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
841 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
849 ERR("Can't use --name with %s event rules.",
850 lttng_event_rule_type_str(
855 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
856 ERR("Can't use --exclude-name/-x with %s event rules.",
857 lttng_event_rule_type_str(
864 * Option --location is only applicable to (and mandatory for) event
865 * rules of type {k,u}probe and function.
867 * Option --event-name is only applicable to event rules of type probe.
868 * If omitted, it defaults to the location.
870 switch (event_rule_type
) {
871 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
872 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
873 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
875 ERR("Event rule of type %s requires a --location.",
876 lttng_event_rule_type_str(event_rule_type
));
881 event_name
= strdup(location
);
888 ERR("Can't use --location with %s event rules.",
889 lttng_event_rule_type_str(event_rule_type
));
894 ERR("Can't use --event-name with %s event rules.",
895 lttng_event_rule_type_str(
902 * Update *argc and *argv so our caller can keep parsing what follows.
904 consumed_args
= argpar_state_get_ingested_orig_args(state
);
905 assert(consumed_args
>= 0);
906 *argc
-= consumed_args
;
907 *argv
+= consumed_args
;
909 /* Need to specify a domain. */
910 if (domain_type
== LTTNG_DOMAIN_NONE
) {
911 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
915 /* Validate event rule type against domain. */
916 switch (event_rule_type
) {
917 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
918 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
919 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
920 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
921 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
922 ERR("Event type not available for user-space tracing.");
927 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
935 * Adding a filter to a probe, function or userspace-probe would be
936 * denied by the kernel tracer as it's not supported at the moment. We
937 * do an early check here to warn the user.
939 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
940 switch (event_rule_type
) {
941 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
942 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
945 ERR("Filter expressions are not supported for %s event rules.",
946 lttng_event_rule_type_str(event_rule_type
));
951 /* If --exclude-name/-x was passed, split it into an exclusion list. */
952 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
953 if (domain_type
!= LTTNG_DOMAIN_UST
) {
954 ERR("Event name exclusions are not yet implemented for %s event rules.",
955 get_domain_str(domain_type
));
959 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
961 * Assume validate_exclusion_list already prints an
969 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
970 ERR("Log levels are only applicable to tracepoint event rules.");
974 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
975 ERR("Log levels are not supported by the kernel tracer.");
980 /* Finally, create the event rule object. */
981 switch (event_rule_type
) {
982 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
984 enum lttng_event_rule_status event_rule_status
;
986 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
988 ERR("Failed to create tracepoint event rule.");
993 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
995 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
996 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
1003 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1005 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1006 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1012 /* Set exclusion list. */
1013 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1015 int count
= lttng_dynamic_pointer_array_get_count(
1018 for (n
= 0; n
< count
; n
++) {
1019 const char *exclude_name
=
1020 lttng_dynamic_pointer_array_get_pointer(
1025 lttng_event_rule_tracepoint_add_exclusion(
1028 if (event_rule_status
!=
1029 LTTNG_EVENT_RULE_STATUS_OK
) {
1030 ERR("Failed to set tracepoint exclusion list element '%s'",
1038 * ".." is the same as passing no log level option and
1039 * correspond the the "ANY" case.
1041 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
1043 bool log_level_only
;
1045 if (!parse_log_level_string(log_level_str
, domain_type
,
1046 &log_level
, &log_level_only
)) {
1047 ERR("Failed to parse log level string `%s`.",
1052 if (log_level_only
) {
1053 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
1055 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1058 if (log_level_rule
== NULL
) {
1059 ERR("Failed to create log level rule object.");
1064 lttng_event_rule_tracepoint_set_log_level_rule(
1065 res
.er
, log_level_rule
);
1067 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1068 ERR("Failed to set log level on event fule.");
1075 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1078 enum lttng_event_rule_status event_rule_status
;
1080 ret
= parse_kernel_probe_opts(
1081 location
, &kernel_probe_location
);
1083 ERR("Failed to parse kernel probe location.");
1087 assert(kernel_probe_location
);
1088 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1090 ERR("Failed to create kprobe event rule.");
1095 lttng_event_rule_kernel_probe_set_event_name(
1096 res
.er
, event_name
);
1097 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1098 ERR("Failed to set kprobe event rule's name to '%s'.",
1105 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1108 enum lttng_event_rule_status event_rule_status
;
1110 ret
= parse_userspace_probe_opts(
1111 location
, &userspace_probe_location
);
1113 ERR("Failed to parse user space probe location.");
1117 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1119 ERR("Failed to create userspace probe event rule.");
1124 lttng_event_rule_userspace_probe_set_event_name(
1125 res
.er
, event_name
);
1126 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1127 ERR("Failed to set user space probe event rule's name to '%s'.",
1134 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1136 enum lttng_event_rule_status event_rule_status
;
1137 enum lttng_event_rule_syscall_emission_site_type emission_site_type
;
1139 if (!parse_syscall_emission_site_from_type(
1140 event_rule_type_str
, &emission_site_type
)) {
1141 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1145 res
.er
= lttng_event_rule_syscall_create(emission_site_type
);
1147 ERR("Failed to create syscall event rule.");
1151 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1153 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1154 ERR("Failed to set syscall event rule's pattern to '%s'.",
1160 event_rule_status
= lttng_event_rule_syscall_set_filter(
1162 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1163 ERR("Failed to set syscall event rule's filter to '%s'.",
1179 lttng_event_rule_destroy(res
.er
);
1181 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1185 filter_parser_ctx_free(parser_ctx
);
1188 lttng_event_expr_destroy(event_expr
);
1189 argpar_item_destroy(item
);
1191 argpar_state_destroy(state
);
1194 lttng_dynamic_pointer_array_reset(&exclude_names
);
1195 free(log_level_str
);
1198 free(event_rule_type_str
);
1200 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1201 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1202 lttng_log_level_rule_destroy(log_level_rule
);
1207 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1209 struct parse_event_rule_res res
;
1210 struct lttng_condition
*c
;
1213 res
= parse_event_rule(argc
, argv
);
1219 c
= lttng_condition_event_rule_matches_create(res
.er
);
1220 lttng_event_rule_destroy(res
.er
);
1226 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1228 enum lttng_condition_status status
;
1229 struct lttng_event_expr
**expr
=
1230 lttng_dynamic_array_get_element(
1231 &res
.capture_descriptors
.array
, i
);
1235 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1237 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1238 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1239 ERR("The capture feature is unsupported by the event-rule condition type");
1245 /* Ownership of event expression moved to `c` */
1252 lttng_condition_destroy(c
);
1256 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1257 lttng_event_rule_destroy(res
.er
);
1261 struct condition_descr
{
1263 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1267 struct condition_descr condition_descrs
[] = {
1268 { "event-rule-matches", handle_condition_event
},
1272 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1276 struct lttng_condition
*cond
;
1277 const struct condition_descr
*descr
= NULL
;
1279 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1280 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1281 descr
= &condition_descrs
[i
];
1287 ERR("Unknown condition name '%s'", condition_name
);
1291 cond
= descr
->handler(argc
, argv
);
1293 /* The handler has already printed an error message. */
1304 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1307 size_t num_token
= 0;
1308 struct lttng_dynamic_pointer_array tokens
;
1309 struct lttng_rate_policy
*policy
= NULL
;
1310 enum lttng_rate_policy_type policy_type
;
1311 unsigned long long value
;
1312 char *policy_type_str
;
1313 char *policy_value_str
;
1316 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1318 /* Rate policy fields are separated by ':'. */
1319 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1321 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1325 * Early sanity check that the number of parameter is exactly 2.
1328 if (num_token
!= 2) {
1329 ERR("Rate policy format is invalid.");
1333 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1334 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1336 /* Parse the type. */
1337 if (strcmp(policy_type_str
, "once-after") == 0) {
1338 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1339 } else if (strcmp(policy_type_str
, "every") == 0) {
1340 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1342 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1346 /* Parse the value. */
1347 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1348 ERR("Failed to parse rate policy value `%s` as an integer.",
1354 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1358 switch (policy_type
) {
1359 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1360 policy
= lttng_rate_policy_every_n_create(value
);
1362 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1363 policy
= lttng_rate_policy_once_after_n_create(value
);
1369 if (policy
== NULL
) {
1370 ERR("Failed to create rate policy `%s`.", policy_str
);
1374 lttng_dynamic_pointer_array_reset(&tokens
);
1378 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1379 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1380 ARGPAR_OPT_DESCR_SENTINEL
1384 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1386 struct lttng_action
*action
= NULL
;
1387 struct argpar_state
*state
= NULL
;
1388 struct argpar_item
*item
= NULL
;
1390 struct lttng_rate_policy
*policy
= NULL
;
1392 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1394 ERR("Failed to allocate an argpar state.");
1399 enum argpar_state_parse_next_status status
;
1401 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1402 status
= argpar_state_parse_next(state
, &item
, &error
);
1403 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1406 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1407 /* Just stop parsing here. */
1409 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1413 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1415 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1416 const struct argpar_item_opt
*item_opt
=
1417 (const struct argpar_item_opt
*) item
;
1419 switch (item_opt
->descr
->id
) {
1420 case OPT_RATE_POLICY
:
1422 policy
= parse_rate_policy(item_opt
->arg
);
1432 const struct argpar_item_non_opt
*item_non_opt
;
1434 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1436 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1438 switch (item_non_opt
->non_opt_index
) {
1440 ERR("Unexpected argument `%s`.",
1447 *argc
-= argpar_state_get_ingested_orig_args(state
);
1448 *argv
+= argpar_state_get_ingested_orig_args(state
);
1450 action
= lttng_action_notify_create();
1452 ERR("Failed to create notify action");
1457 enum lttng_action_status status
;
1458 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1459 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1460 ERR("Failed to set rate policy");
1468 lttng_action_destroy(action
);
1472 lttng_rate_policy_destroy(policy
);
1473 argpar_state_destroy(state
);
1474 argpar_item_destroy(item
);
1479 * Generic handler for a kind of action that takes a session name and an
1480 * optional rate policy.
1483 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1485 struct lttng_action
*(*create_action_cb
)(void),
1486 enum lttng_action_status (*set_session_name_cb
)(
1487 struct lttng_action
*, const char *),
1488 enum lttng_action_status (*set_rate_policy_cb
)(
1489 struct lttng_action
*,
1490 const struct lttng_rate_policy
*),
1491 const char *action_name
)
1493 struct lttng_action
*action
= NULL
;
1494 struct argpar_state
*state
= NULL
;
1495 struct argpar_item
*item
= NULL
;
1496 const char *session_name_arg
= NULL
;
1498 enum lttng_action_status action_status
;
1499 struct lttng_rate_policy
*policy
= NULL
;
1501 assert(set_session_name_cb
);
1502 assert(set_rate_policy_cb
);
1504 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1505 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1506 ARGPAR_OPT_DESCR_SENTINEL
1509 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1511 ERR("Failed to allocate an argpar state.");
1516 enum argpar_state_parse_next_status status
;
1518 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1519 status
= argpar_state_parse_next(state
, &item
, &error
);
1520 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1523 } else if (status
==
1524 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1525 /* Just stop parsing here. */
1527 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1531 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1532 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1533 const struct argpar_item_opt
*item_opt
=
1534 (const struct argpar_item_opt
*) item
;
1536 switch (item_opt
->descr
->id
) {
1537 case OPT_RATE_POLICY
:
1539 policy
= parse_rate_policy(item_opt
->arg
);
1549 const struct argpar_item_non_opt
*item_non_opt
;
1550 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1552 switch (item_non_opt
->non_opt_index
) {
1554 session_name_arg
= item_non_opt
->arg
;
1557 ERR("Unexpected argument `%s`.",
1564 *argc
-= argpar_state_get_ingested_orig_args(state
);
1565 *argv
+= argpar_state_get_ingested_orig_args(state
);
1567 if (!session_name_arg
) {
1568 ERR("Missing session name.");
1572 action
= create_action_cb();
1574 ERR("Failed to allocate %s session action.", action_name
);
1578 action_status
= set_session_name_cb(action
, session_name_arg
);
1579 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1580 ERR("Failed to set action %s session's session name to '%s'.",
1581 action_name
, session_name_arg
);
1586 action_status
= set_rate_policy_cb(action
, policy
);
1587 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1588 ERR("Failed to set rate policy");
1596 lttng_action_destroy(action
);
1598 argpar_item_destroy(item
);
1600 lttng_rate_policy_destroy(policy
);
1602 argpar_state_destroy(state
);
1607 struct lttng_action
*handle_action_start_session(int *argc
,
1610 return handle_action_simple_session_with_policy(argc
, argv
,
1611 lttng_action_start_session_create
,
1612 lttng_action_start_session_set_session_name
,
1613 lttng_action_start_session_set_rate_policy
, "start");
1617 struct lttng_action
*handle_action_stop_session(int *argc
,
1620 return handle_action_simple_session_with_policy(argc
, argv
,
1621 lttng_action_stop_session_create
,
1622 lttng_action_stop_session_set_session_name
,
1623 lttng_action_stop_session_set_rate_policy
, "stop");
1627 struct lttng_action
*handle_action_rotate_session(int *argc
,
1630 return handle_action_simple_session_with_policy(argc
, argv
,
1631 lttng_action_rotate_session_create
,
1632 lttng_action_rotate_session_set_session_name
,
1633 lttng_action_rotate_session_set_rate_policy
,
1637 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1638 { OPT_NAME
, 'n', "name", true },
1639 { OPT_MAX_SIZE
, 'm', "max-size", true },
1640 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1641 { OPT_DATA_URL
, '\0', "data-url", true },
1642 { OPT_URL
, '\0', "url", true },
1643 { OPT_PATH
, '\0', "path", true },
1644 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1645 ARGPAR_OPT_DESCR_SENTINEL
1649 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1652 struct lttng_action
*action
= NULL
;
1653 struct argpar_state
*state
= NULL
;
1654 struct argpar_item
*item
= NULL
;
1655 const char *session_name_arg
= NULL
;
1656 char *snapshot_name_arg
= NULL
;
1657 char *ctrl_url_arg
= NULL
;
1658 char *data_url_arg
= NULL
;
1659 char *max_size_arg
= NULL
;
1660 char *url_arg
= NULL
;
1661 char *path_arg
= NULL
;
1663 enum lttng_action_status action_status
;
1664 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1665 struct lttng_rate_policy
*policy
= NULL
;
1667 unsigned int locations_specified
= 0;
1669 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1671 ERR("Failed to allocate an argpar state.");
1676 enum argpar_state_parse_next_status status
;
1678 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1679 status
= argpar_state_parse_next(state
, &item
, &error
);
1680 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1683 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1684 /* Just stop parsing here. */
1686 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1690 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1692 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1693 const struct argpar_item_opt
*item_opt
=
1694 (const struct argpar_item_opt
*) item
;
1696 switch (item_opt
->descr
->id
) {
1698 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1704 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1710 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1716 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1722 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1728 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1733 case OPT_RATE_POLICY
:
1735 policy
= parse_rate_policy(item_opt
->arg
);
1745 const struct argpar_item_non_opt
*item_non_opt
;
1747 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1749 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1751 switch (item_non_opt
->non_opt_index
) {
1753 session_name_arg
= item_non_opt
->arg
;
1756 ERR("Unexpected argument `%s`.",
1763 *argc
-= argpar_state_get_ingested_orig_args(state
);
1764 *argv
+= argpar_state_get_ingested_orig_args(state
);
1766 if (!session_name_arg
) {
1767 ERR("Missing session name.");
1771 /* --ctrl-url and --data-url must come in pair. */
1772 if (ctrl_url_arg
&& !data_url_arg
) {
1773 ERR("--ctrl-url is specified, but --data-url is missing.");
1777 if (!ctrl_url_arg
&& data_url_arg
) {
1778 ERR("--data-url is specified, but --ctrl-url is missing.");
1782 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1783 locations_specified
+= !!url_arg
;
1784 locations_specified
+= !!path_arg
;
1786 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1787 if (locations_specified
> 1) {
1788 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1793 * Did the user specify an option that implies using a
1794 * custom/unregistered output?
1796 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1797 snapshot_output
= lttng_snapshot_output_create();
1798 if (!snapshot_output
) {
1799 ERR("Failed to allocate a snapshot output.");
1804 action
= lttng_action_snapshot_session_create();
1806 ERR("Failed to allocate snapshot session action.");
1810 action_status
= lttng_action_snapshot_session_set_session_name(
1811 action
, session_name_arg
);
1812 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1813 ERR("Failed to set action snapshot session's session name to '%s'.",
1818 if (snapshot_name_arg
) {
1819 if (!snapshot_output
) {
1820 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1824 ret
= lttng_snapshot_output_set_name(
1825 snapshot_name_arg
, snapshot_output
);
1827 ERR("Failed to set name of snapshot output.");
1835 if (!snapshot_output
) {
1836 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1840 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1842 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1846 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1848 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1856 struct lttng_uri
*uris
;
1858 if (!strstr(url_arg
, "://")) {
1859 ERR("Failed to parse '%s' as an URL.", url_arg
);
1863 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1865 ERR("Failed to parse '%s' as an URL.", url_arg
);
1869 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1870 ret
= lttng_snapshot_output_set_local_path(
1871 uris
[0].dst
.path
, snapshot_output
);
1874 ERR("Failed to assign '%s' as a local destination.",
1879 ret
= lttng_snapshot_output_set_network_url(
1880 url_arg
, snapshot_output
);
1883 ERR("Failed to assign '%s' as a network URL.",
1891 ret
= lttng_snapshot_output_set_local_path(
1892 path_arg
, snapshot_output
);
1894 ERR("Failed to parse '%s' as a local path.", path_arg
);
1901 * Two argument form, network output with separate control and
1904 ret
= lttng_snapshot_output_set_network_urls(
1905 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1907 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1908 ctrl_url_arg
, data_url_arg
);
1913 if (snapshot_output
) {
1914 action_status
= lttng_action_snapshot_session_set_output(
1915 action
, snapshot_output
);
1916 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1917 ERR("Failed to set snapshot session action's output.");
1921 /* Ownership of `snapshot_output` has been transferred to the action. */
1922 snapshot_output
= NULL
;
1926 enum lttng_action_status status
;
1927 status
= lttng_action_snapshot_session_set_rate_policy(
1929 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1930 ERR("Failed to set rate policy");
1938 lttng_action_destroy(action
);
1942 free(snapshot_name_arg
);
1947 free(snapshot_output
);
1949 lttng_rate_policy_destroy(policy
);
1950 argpar_state_destroy(state
);
1951 argpar_item_destroy(item
);
1955 struct action_descr
{
1957 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1961 struct action_descr action_descrs
[] = {
1962 { "notify", handle_action_notify
},
1963 { "start-session", handle_action_start_session
},
1964 { "stop-session", handle_action_stop_session
},
1965 { "rotate-session", handle_action_rotate_session
},
1966 { "snapshot-session", handle_action_snapshot_session
},
1970 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1973 struct lttng_action
*action
;
1974 const struct action_descr
*descr
= NULL
;
1976 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1977 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1978 descr
= &action_descrs
[i
];
1984 ERR("Unknown action name: %s", action_name
);
1988 action
= descr
->handler(argc
, argv
);
1990 /* The handler has already printed an error message. */
2002 struct argpar_opt_descr add_trigger_options
[] = {
2003 { OPT_HELP
, 'h', "help", false },
2004 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2005 { OPT_CONDITION
, '\0', "condition", true },
2006 { OPT_ACTION
, '\0', "action", true },
2007 { OPT_NAME
, '\0', "name", true },
2008 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2009 ARGPAR_OPT_DESCR_SENTINEL
,
2013 void lttng_actions_destructor(void *p
)
2015 struct lttng_action
*action
= p
;
2017 lttng_action_destroy(action
);
2020 int cmd_add_trigger(int argc
, const char **argv
)
2023 int my_argc
= argc
- 1;
2024 const char **my_argv
= argv
+ 1;
2025 struct lttng_condition
*condition
= NULL
;
2026 struct lttng_dynamic_pointer_array actions
;
2027 struct argpar_state
*argpar_state
= NULL
;
2028 struct argpar_item
*argpar_item
= NULL
;
2029 struct lttng_action
*action_list
= NULL
;
2030 struct lttng_action
*action
= NULL
;
2031 struct lttng_trigger
*trigger
= NULL
;
2035 char *owner_uid
= NULL
;
2036 enum lttng_error_code ret_code
;
2038 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2041 enum argpar_state_parse_next_status status
;
2042 const struct argpar_item_opt
*item_opt
;
2045 argpar_state_destroy(argpar_state
);
2046 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2047 add_trigger_options
);
2048 if (!argpar_state
) {
2049 ERR("Failed to create argpar state.");
2053 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2054 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2055 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2058 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2061 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2065 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2067 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2068 const struct argpar_item_non_opt
*item_non_opt
=
2069 (const struct argpar_item_non_opt
*)
2072 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2076 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2078 ingested_args
= argpar_state_get_ingested_orig_args(
2081 my_argc
-= ingested_args
;
2082 my_argv
+= ingested_args
;
2084 switch (item_opt
->descr
->id
) {
2089 case OPT_LIST_OPTIONS
:
2090 list_cmd_options_argpar(stdout
, add_trigger_options
);
2096 ERR("A --condition was already given.");
2100 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2103 * An error message was already printed by
2113 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2116 * An error message was already printed by
2122 ret
= lttng_dynamic_pointer_array_add_pointer(
2125 ERR("Failed to add pointer to pointer array.");
2129 /* Ownership of the action was transferred to the group. */
2136 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2144 if (!assign_string(&owner_uid
, item_opt
->arg
,
2157 ERR("Missing --condition.");
2161 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2162 ERR("Need at least one --action.");
2166 action_list
= lttng_action_list_create();
2171 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2172 enum lttng_action_status status
;
2174 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2176 status
= lttng_action_list_add_action(action_list
, action
);
2177 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2182 * The `lttng_action_list_add_action()` takes a reference to
2183 * the action. We can destroy ours.
2185 lttng_action_destroy(action
);
2189 trigger
= lttng_trigger_create(condition
, action_list
);
2195 enum lttng_trigger_status trigger_status
;
2200 uid
= strtol(owner_uid
, &end
, 10);
2201 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2202 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2206 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2207 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2208 ERR("Failed to set trigger's user identity.");
2214 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2216 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2219 if (ret_code
!= LTTNG_OK
) {
2220 ERR("Failed to register trigger: %s.",
2221 lttng_strerror(-ret_code
));
2225 MSG("Trigger registered successfully.");
2234 argpar_state_destroy(argpar_state
);
2235 argpar_item_destroy(argpar_item
);
2236 lttng_dynamic_pointer_array_reset(&actions
);
2237 lttng_condition_destroy(condition
);
2238 lttng_action_destroy(action_list
);
2239 lttng_action_destroy(action
);
2240 lttng_trigger_destroy(trigger
);