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
;
212 /* This is defined in enable_events.c. */
214 int validate_exclusion_list(
215 const char *event_name
, const char *const *exclusions
);
218 * Parse `str` as a log level in domain `domain_type`.
220 * Return the log level in `*log_level`. Return true in `*log_level_only` if
221 * the string specifies exactly this log level, false if it specifies at least
224 * Return true if the string was successfully parsed as a log level string.
226 static bool parse_log_level_string(const char *str
,
227 enum lttng_domain_type domain_type
,
229 bool *log_level_only
)
233 switch (domain_type
) {
234 case LTTNG_DOMAIN_UST
:
236 enum lttng_loglevel log_level_min
, log_level_max
;
237 if (!loglevel_parse_range_string(
238 str
, &log_level_min
, &log_level_max
)) {
242 /* Only support VAL and VAL.. for now. */
243 if (log_level_min
!= log_level_max
&&
244 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
248 *log_level
= (int) log_level_min
;
249 *log_level_only
= log_level_min
== log_level_max
;
252 case LTTNG_DOMAIN_LOG4J
:
254 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
255 if (!loglevel_log4j_parse_range_string(
256 str
, &log_level_min
, &log_level_max
)) {
260 /* Only support VAL and VAL.. for now. */
261 if (log_level_min
!= log_level_max
&&
262 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
266 *log_level
= (int) log_level_min
;
267 *log_level_only
= log_level_min
== log_level_max
;
270 case LTTNG_DOMAIN_JUL
:
272 enum lttng_loglevel_jul log_level_min
, log_level_max
;
273 if (!loglevel_jul_parse_range_string(
274 str
, &log_level_min
, &log_level_max
)) {
278 /* Only support VAL and VAL.. for now. */
279 if (log_level_min
!= log_level_max
&&
280 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
284 *log_level
= (int) log_level_min
;
285 *log_level_only
= log_level_min
== log_level_max
;
288 case LTTNG_DOMAIN_PYTHON
:
290 enum lttng_loglevel_python log_level_min
, log_level_max
;
291 if (!loglevel_python_parse_range_string(
292 str
, &log_level_min
, &log_level_max
)) {
296 /* Only support VAL and VAL.. for now. */
297 if (log_level_min
!= log_level_max
&&
299 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
303 *log_level
= (int) log_level_min
;
304 *log_level_only
= log_level_min
== log_level_max
;
308 /* Invalid domain type. */
322 static int parse_kernel_probe_opts(const char *source
,
323 struct lttng_kernel_probe_location
**location
)
328 char name
[LTTNG_SYMBOL_NAME_LEN
];
329 char *symbol_name
= NULL
;
332 /* Check for symbol+offset. */
333 match
= sscanf(source
,
334 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
338 if (*s_hex
== '\0') {
339 ERR("Kernel probe symbol offset is missing.");
343 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
345 PERROR("Failed to copy kernel probe location symbol name.");
348 offset
= strtoul(s_hex
, NULL
, 0);
350 *location
= lttng_kernel_probe_location_symbol_create(
351 symbol_name
, offset
);
353 ERR("Failed to create symbol kernel probe location.");
360 /* Check for symbol. */
361 if (isalpha(name
[0]) || name
[0] == '_') {
362 match
= sscanf(source
,
363 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
367 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
369 ERR("Failed to copy kernel probe location symbol name.");
373 *location
= lttng_kernel_probe_location_symbol_create(
376 ERR("Failed to create symbol kernel probe location.");
384 /* Check for address. */
385 match
= sscanf(source
, "%18s", s_hex
);
389 if (*s_hex
== '\0') {
390 ERR("Invalid kernel probe location address.");
394 address
= strtoul(s_hex
, NULL
, 0);
395 *location
= lttng_kernel_probe_location_address_create(address
);
397 ERR("Failed to create symbol kernel probe location.");
415 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
416 const struct ir_load_expression
*load_expr
,
417 const char *capture_str
)
419 char *provider_name
= NULL
;
420 struct lttng_event_expr
*event_expr
= NULL
;
421 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
422 const enum ir_load_expression_type load_expr_child_type
=
425 switch (load_expr_child_type
) {
426 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
427 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
429 const char *field_name
;
431 load_expr_op
= load_expr_op
->next
;
432 assert(load_expr_op
);
433 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
434 field_name
= load_expr_op
->u
.symbol
;
437 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
438 lttng_event_expr_event_payload_field_create(field_name
) :
439 lttng_event_expr_channel_context_field_create(field_name
);
441 ERR("Failed to create %s event expression: field name = `%s`.",
442 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
443 "payload field" : "channel context",
450 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
453 const char *type_name
;
454 const char *field_name
;
456 load_expr_op
= load_expr_op
->next
;
457 assert(load_expr_op
);
458 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
459 field_name
= load_expr_op
->u
.symbol
;
463 * The field name needs to be of the form PROVIDER:TYPE. We
466 colon
= strchr(field_name
, ':');
468 ERR("Invalid app-specific context field name: missing colon in `%s`.",
473 type_name
= colon
+ 1;
474 if (*type_name
== '\0') {
475 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
480 provider_name
= strndup(field_name
, colon
- field_name
);
481 if (!provider_name
) {
482 PERROR("Failed to allocate field name string");
486 event_expr
= lttng_event_expr_app_specific_context_field_create(
487 provider_name
, type_name
);
489 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
490 provider_name
, type_name
);
497 ERR("%s: unexpected load expr type %d.", __func__
,
502 load_expr_op
= load_expr_op
->next
;
504 /* There may be a single array index after that. */
505 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
506 struct lttng_event_expr
*index_event_expr
;
507 const uint64_t index
= load_expr_op
->u
.index
;
509 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
510 if (!index_event_expr
) {
511 ERR("Failed to create array field element event expression.");
515 event_expr
= index_event_expr
;
516 load_expr_op
= load_expr_op
->next
;
519 switch (load_expr_op
->type
) {
520 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
522 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
523 * always found at the end of the chain.
526 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
527 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
532 ERR("%s: unexpected load expression operator %s.", __func__
,
533 ir_load_expression_type_str(load_expr_op
->type
));
540 lttng_event_expr_destroy(event_expr
);
550 struct lttng_event_expr
*ir_op_load_to_event_expr(
551 const struct ir_op
*ir
, const char *capture_str
)
553 struct lttng_event_expr
*event_expr
= NULL
;
555 assert(ir
->op
== IR_OP_LOAD
);
557 switch (ir
->data_type
) {
558 case IR_DATA_EXPRESSION
:
560 const struct ir_load_expression
*ir_load_expr
=
561 ir
->u
.load
.u
.expression
;
563 event_expr
= ir_op_load_expr_to_event_expr(
564 ir_load_expr
, capture_str
);
568 ERR("%s: unexpected data type: %s.", __func__
,
569 ir_data_type_str(ir
->data_type
));
577 const char *ir_operator_type_human_str(enum ir_op_type op
)
599 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
600 const char *capture_str
)
602 struct lttng_event_expr
*event_expr
= NULL
;
604 assert(ir
->op
== IR_OP_ROOT
);
605 ir
= ir
->u
.root
.child
;
609 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
614 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
616 ir_operator_type_human_str(ir
->op
));
619 ERR("%s: unexpected IR op type: %s.", __func__
,
620 ir_op_type_str(ir
->op
));
628 void destroy_event_expr(void *ptr
)
630 lttng_event_expr_destroy(ptr
);
633 struct parse_event_rule_res
{
635 struct lttng_event_rule
*er
;
637 /* Array of `struct lttng_event_expr *` */
638 struct lttng_dynamic_pointer_array capture_descriptors
;
642 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
644 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
645 enum lttng_event_rule_type event_rule_type
=
646 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
647 struct argpar_state
*state
;
648 struct argpar_item
*item
= NULL
;
650 int consumed_args
= -1;
651 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
652 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
653 struct parse_event_rule_res res
= { 0 };
654 struct lttng_event_expr
*event_expr
= NULL
;
655 struct filter_parser_ctx
*parser_ctx
= NULL
;
656 struct lttng_log_level_rule
*log_level_rule
= NULL
;
658 /* Event rule type option */
659 char *event_rule_type_str
= NULL
;
661 /* Tracepoint and syscall options. */
663 /* Array of strings. */
664 struct lttng_dynamic_pointer_array exclude_names
;
666 /* For userspace / kernel probe and function. */
667 char *location
= NULL
;
668 char *event_name
= NULL
;
674 char *log_level_str
= NULL
;
676 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
679 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
681 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
683 ERR("Failed to allocate an argpar state.");
688 enum argpar_state_parse_next_status status
;
690 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
691 status
= argpar_state_parse_next(state
, &item
, &error
);
692 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
695 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
696 /* Just stop parsing here. */
698 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
702 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
704 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
705 const struct argpar_item_opt
*item_opt
=
706 (const struct argpar_item_opt
*) item
;
708 switch (item_opt
->descr
->id
) {
711 if (!assign_domain_type(&domain_type
,
718 if (!assign_event_rule_type(&event_rule_type
,
723 /* Save the string for later use. */
724 if (!assign_string(&event_rule_type_str
,
732 if (!assign_string(&location
,
740 if (!assign_string(&event_name
,
742 "--event-name/-E")) {
748 if (!assign_string(&filter
, item_opt
->arg
,
755 if (!assign_string(&name
, item_opt
->arg
,
761 case OPT_EXCLUDE_NAME
:
765 ret
= lttng_dynamic_pointer_array_add_pointer(
767 strdup(item_opt
->arg
));
769 ERR("Failed to add pointer to dynamic pointer array.");
776 if (!assign_string(&log_level_str
,
777 item_opt
->arg
, "--log-level/-l")) {
785 const char *capture_str
= item_opt
->arg
;
787 ret
= filter_parser_ctx_create_from_filter_expression(
788 capture_str
, &parser_ctx
);
790 ERR("Failed to parse capture expression `%s`.",
795 event_expr
= ir_op_root_to_event_expr(
800 * ir_op_root_to_event_expr has printed
806 ret
= lttng_dynamic_pointer_array_add_pointer(
807 &res
.capture_descriptors
,
814 * The ownership of event expression was
815 * transferred to the dynamic array.
825 const struct argpar_item_non_opt
*item_non_opt
=
826 (const struct argpar_item_non_opt
*)
829 /* Don't accept non-option arguments. */
830 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
835 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
836 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
840 * Option --name is applicable to event rules of type tracepoint
841 * and syscall. For tracepoint and syscall rules, if --name is
842 * omitted, it is implicitly "*".
844 switch (event_rule_type
) {
845 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
846 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
854 ERR("Can't use --name with %s event rules.",
855 lttng_event_rule_type_str(
860 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
861 ERR("Can't use --exclude-name/-x with %s event rules.",
862 lttng_event_rule_type_str(
869 * Option --location is only applicable to (and mandatory for) event
870 * rules of type {k,u}probe and function.
872 * Option --event-name is only applicable to event rules of type probe.
873 * If omitted, it defaults to the location.
875 switch (event_rule_type
) {
876 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
877 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
878 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
880 ERR("Event rule of type %s requires a --location.",
881 lttng_event_rule_type_str(event_rule_type
));
886 event_name
= strdup(location
);
893 ERR("Can't use --location with %s event rules.",
894 lttng_event_rule_type_str(event_rule_type
));
899 ERR("Can't use --event-name with %s event rules.",
900 lttng_event_rule_type_str(
907 * Update *argc and *argv so our caller can keep parsing what follows.
909 consumed_args
= argpar_state_get_ingested_orig_args(state
);
910 assert(consumed_args
>= 0);
911 *argc
-= consumed_args
;
912 *argv
+= consumed_args
;
914 /* Need to specify a domain. */
915 if (domain_type
== LTTNG_DOMAIN_NONE
) {
916 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
920 /* Validate event rule type against domain. */
921 switch (event_rule_type
) {
922 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
923 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
924 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
925 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
926 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
927 ERR("Event type not available for user-space tracing.");
932 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
940 * Adding a filter to a probe, function or userspace-probe would be
941 * denied by the kernel tracer as it's not supported at the moment. We
942 * do an early check here to warn the user.
944 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
945 switch (event_rule_type
) {
946 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
947 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
950 ERR("Filter expressions are not supported for %s event rules.",
951 lttng_event_rule_type_str(event_rule_type
));
956 /* If --exclude-name/-x was passed, split it into an exclusion list. */
957 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
958 if (domain_type
!= LTTNG_DOMAIN_UST
) {
959 ERR("Event name exclusions are not yet implemented for %s event rules.",
960 get_domain_str(domain_type
));
964 if (validate_exclusion_list(name
,
965 (const char **) exclude_names
.array
.buffer
970 * Assume validate_exclusion_list already prints an
978 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
979 ERR("Log levels are only applicable to tracepoint event rules.");
983 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
984 ERR("Log levels are not supported by the kernel tracer.");
989 /* Finally, create the event rule object. */
990 switch (event_rule_type
) {
991 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
993 enum lttng_event_rule_status event_rule_status
;
995 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
997 ERR("Failed to create tracepoint event rule.");
1002 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
1004 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1005 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
1012 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1014 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1015 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1021 /* Set exclusion list. */
1022 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1024 int count
= lttng_dynamic_pointer_array_get_count(
1027 for (n
= 0; n
< count
; n
++) {
1028 const char *exclude_name
=
1029 lttng_dynamic_pointer_array_get_pointer(
1034 lttng_event_rule_tracepoint_add_exclusion(
1037 if (event_rule_status
!=
1038 LTTNG_EVENT_RULE_STATUS_OK
) {
1039 ERR("Failed to set tracepoint exclusion list element '%s'",
1047 * ".." is the same as passing no log level option and
1048 * correspond the the "ANY" case.
1050 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
1052 bool log_level_only
;
1054 if (!parse_log_level_string(log_level_str
, domain_type
,
1055 &log_level
, &log_level_only
)) {
1056 ERR("Failed to parse log level string `%s`.",
1061 if (log_level_only
) {
1062 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
1064 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1067 if (log_level_rule
== NULL
) {
1068 ERR("Failed to create log level rule object.");
1073 lttng_event_rule_tracepoint_set_log_level_rule(
1074 res
.er
, log_level_rule
);
1076 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1077 ERR("Failed to set log level on event fule.");
1084 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1087 enum lttng_event_rule_status event_rule_status
;
1089 ret
= parse_kernel_probe_opts(
1090 location
, &kernel_probe_location
);
1092 ERR("Failed to parse kernel probe location.");
1096 assert(kernel_probe_location
);
1097 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1099 ERR("Failed to create kprobe event rule.");
1104 lttng_event_rule_kernel_probe_set_event_name(
1105 res
.er
, event_name
);
1106 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1107 ERR("Failed to set kprobe event rule's name to '%s'.",
1114 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1117 enum lttng_event_rule_status event_rule_status
;
1119 ret
= parse_userspace_probe_opts(
1120 location
, &userspace_probe_location
);
1122 ERR("Failed to parse user space probe location.");
1126 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1128 ERR("Failed to create userspace probe event rule.");
1133 lttng_event_rule_userspace_probe_set_event_name(
1134 res
.er
, event_name
);
1135 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1136 ERR("Failed to set user space probe event rule's name to '%s'.",
1143 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1145 enum lttng_event_rule_status event_rule_status
;
1146 enum lttng_event_rule_syscall_emission_site_type emission_site_type
;
1148 if (!parse_syscall_emission_site_from_type(
1149 event_rule_type_str
, &emission_site_type
)) {
1150 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1154 res
.er
= lttng_event_rule_syscall_create(emission_site_type
);
1156 ERR("Failed to create syscall event rule.");
1160 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1162 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1163 ERR("Failed to set syscall event rule's pattern to '%s'.",
1169 event_rule_status
= lttng_event_rule_syscall_set_filter(
1171 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1172 ERR("Failed to set syscall event rule's filter to '%s'.",
1188 lttng_event_rule_destroy(res
.er
);
1190 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1194 filter_parser_ctx_free(parser_ctx
);
1197 lttng_event_expr_destroy(event_expr
);
1198 argpar_item_destroy(item
);
1200 argpar_state_destroy(state
);
1203 lttng_dynamic_pointer_array_reset(&exclude_names
);
1204 free(log_level_str
);
1207 free(event_rule_type_str
);
1209 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1210 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1211 lttng_log_level_rule_destroy(log_level_rule
);
1216 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1218 struct parse_event_rule_res res
;
1219 struct lttng_condition
*c
;
1222 res
= parse_event_rule(argc
, argv
);
1228 c
= lttng_condition_event_rule_matches_create(res
.er
);
1229 lttng_event_rule_destroy(res
.er
);
1235 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1237 enum lttng_condition_status status
;
1238 struct lttng_event_expr
**expr
=
1239 lttng_dynamic_array_get_element(
1240 &res
.capture_descriptors
.array
, i
);
1244 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1246 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1247 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1248 ERR("The capture feature is unsupported by the event-rule condition type");
1254 /* Ownership of event expression moved to `c` */
1261 lttng_condition_destroy(c
);
1265 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1266 lttng_event_rule_destroy(res
.er
);
1270 struct condition_descr
{
1272 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1276 struct condition_descr condition_descrs
[] = {
1277 { "event-rule-matches", handle_condition_event
},
1281 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1285 struct lttng_condition
*cond
;
1286 const struct condition_descr
*descr
= NULL
;
1288 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1289 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1290 descr
= &condition_descrs
[i
];
1296 ERR("Unknown condition name '%s'", condition_name
);
1300 cond
= descr
->handler(argc
, argv
);
1302 /* The handler has already printed an error message. */
1313 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1316 char **tokens
= NULL
;
1317 struct lttng_rate_policy
*policy
= NULL
;
1318 enum lttng_rate_policy_type policy_type
;
1319 unsigned long long value
;
1320 char *policy_type_str
;
1321 char *policy_value_str
;
1326 * rate policy fields are separated by ':'.
1328 tokens
= strutils_split(policy_str
, ':', 1);
1329 num_token
= strutils_array_of_strings_len(tokens
);
1332 * Early sanity check that the number of parameter is exactly 2.
1335 if (num_token
!= 2) {
1336 ERR("Rate policy format is invalid.");
1340 policy_type_str
= tokens
[0];
1341 policy_value_str
= tokens
[1];
1343 /* Parse the type. */
1344 if (strcmp(policy_type_str
, "once-after") == 0) {
1345 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1346 } else if (strcmp(policy_type_str
, "every") == 0) {
1347 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1349 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1353 /* Parse the value. */
1354 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1355 ERR("Failed to parse rate policy value `%s` as an integer.",
1361 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1365 switch (policy_type
) {
1366 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1367 policy
= lttng_rate_policy_every_n_create(value
);
1369 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1370 policy
= lttng_rate_policy_once_after_n_create(value
);
1376 if (policy
== NULL
) {
1377 ERR("Failed to create rate policy `%s`.", policy_str
);
1381 strutils_free_null_terminated_array_of_strings(tokens
);
1385 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1386 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1387 ARGPAR_OPT_DESCR_SENTINEL
1391 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1393 struct lttng_action
*action
= NULL
;
1394 struct argpar_state
*state
= NULL
;
1395 struct argpar_item
*item
= NULL
;
1397 struct lttng_rate_policy
*policy
= NULL
;
1399 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1401 ERR("Failed to allocate an argpar state.");
1406 enum argpar_state_parse_next_status status
;
1408 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1409 status
= argpar_state_parse_next(state
, &item
, &error
);
1410 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1413 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1414 /* Just stop parsing here. */
1416 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1420 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1422 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1423 const struct argpar_item_opt
*item_opt
=
1424 (const struct argpar_item_opt
*) item
;
1426 switch (item_opt
->descr
->id
) {
1427 case OPT_RATE_POLICY
:
1429 policy
= parse_rate_policy(item_opt
->arg
);
1439 const struct argpar_item_non_opt
*item_non_opt
;
1441 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1443 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1445 switch (item_non_opt
->non_opt_index
) {
1447 ERR("Unexpected argument `%s`.",
1454 *argc
-= argpar_state_get_ingested_orig_args(state
);
1455 *argv
+= argpar_state_get_ingested_orig_args(state
);
1457 action
= lttng_action_notify_create();
1459 ERR("Failed to create notify action");
1464 enum lttng_action_status status
;
1465 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1466 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1467 ERR("Failed to set rate policy");
1475 lttng_action_destroy(action
);
1479 lttng_rate_policy_destroy(policy
);
1480 argpar_state_destroy(state
);
1481 argpar_item_destroy(item
);
1486 * Generic handler for a kind of action that takes a session name and an
1487 * optional rate policy.
1490 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1492 struct lttng_action
*(*create_action_cb
)(void),
1493 enum lttng_action_status (*set_session_name_cb
)(
1494 struct lttng_action
*, const char *),
1495 enum lttng_action_status (*set_rate_policy_cb
)(
1496 struct lttng_action
*,
1497 const struct lttng_rate_policy
*),
1498 const char *action_name
)
1500 struct lttng_action
*action
= NULL
;
1501 struct argpar_state
*state
= NULL
;
1502 struct argpar_item
*item
= NULL
;
1503 const char *session_name_arg
= NULL
;
1505 enum lttng_action_status action_status
;
1506 struct lttng_rate_policy
*policy
= NULL
;
1508 assert(set_session_name_cb
);
1509 assert(set_rate_policy_cb
);
1511 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1512 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1513 ARGPAR_OPT_DESCR_SENTINEL
1516 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1518 ERR("Failed to allocate an argpar state.");
1523 enum argpar_state_parse_next_status status
;
1525 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1526 status
= argpar_state_parse_next(state
, &item
, &error
);
1527 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1530 } else if (status
==
1531 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1532 /* Just stop parsing here. */
1534 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1538 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1539 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1540 const struct argpar_item_opt
*item_opt
=
1541 (const struct argpar_item_opt
*) item
;
1543 switch (item_opt
->descr
->id
) {
1544 case OPT_RATE_POLICY
:
1546 policy
= parse_rate_policy(item_opt
->arg
);
1556 const struct argpar_item_non_opt
*item_non_opt
;
1557 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1559 switch (item_non_opt
->non_opt_index
) {
1561 session_name_arg
= item_non_opt
->arg
;
1564 ERR("Unexpected argument `%s`.",
1571 *argc
-= argpar_state_get_ingested_orig_args(state
);
1572 *argv
+= argpar_state_get_ingested_orig_args(state
);
1574 if (!session_name_arg
) {
1575 ERR("Missing session name.");
1579 action
= create_action_cb();
1581 ERR("Failed to allocate %s session action.", action_name
);
1585 action_status
= set_session_name_cb(action
, session_name_arg
);
1586 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1587 ERR("Failed to set action %s session's session name to '%s'.",
1588 action_name
, session_name_arg
);
1593 action_status
= set_rate_policy_cb(action
, policy
);
1594 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1595 ERR("Failed to set rate policy");
1603 lttng_action_destroy(action
);
1605 argpar_item_destroy(item
);
1607 lttng_rate_policy_destroy(policy
);
1609 argpar_state_destroy(state
);
1614 struct lttng_action
*handle_action_start_session(int *argc
,
1617 return handle_action_simple_session_with_policy(argc
, argv
,
1618 lttng_action_start_session_create
,
1619 lttng_action_start_session_set_session_name
,
1620 lttng_action_start_session_set_rate_policy
, "start");
1624 struct lttng_action
*handle_action_stop_session(int *argc
,
1627 return handle_action_simple_session_with_policy(argc
, argv
,
1628 lttng_action_stop_session_create
,
1629 lttng_action_stop_session_set_session_name
,
1630 lttng_action_stop_session_set_rate_policy
, "stop");
1634 struct lttng_action
*handle_action_rotate_session(int *argc
,
1637 return handle_action_simple_session_with_policy(argc
, argv
,
1638 lttng_action_rotate_session_create
,
1639 lttng_action_rotate_session_set_session_name
,
1640 lttng_action_rotate_session_set_rate_policy
,
1644 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1645 { OPT_NAME
, 'n', "name", true },
1646 { OPT_MAX_SIZE
, 'm', "max-size", true },
1647 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1648 { OPT_DATA_URL
, '\0', "data-url", true },
1649 { OPT_URL
, '\0', "url", true },
1650 { OPT_PATH
, '\0', "path", true },
1651 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1652 ARGPAR_OPT_DESCR_SENTINEL
1656 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1659 struct lttng_action
*action
= NULL
;
1660 struct argpar_state
*state
= NULL
;
1661 struct argpar_item
*item
= NULL
;
1662 const char *session_name_arg
= NULL
;
1663 char *snapshot_name_arg
= NULL
;
1664 char *ctrl_url_arg
= NULL
;
1665 char *data_url_arg
= NULL
;
1666 char *max_size_arg
= NULL
;
1667 char *url_arg
= NULL
;
1668 char *path_arg
= NULL
;
1670 enum lttng_action_status action_status
;
1671 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1672 struct lttng_rate_policy
*policy
= NULL
;
1674 unsigned int locations_specified
= 0;
1676 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1678 ERR("Failed to allocate an argpar state.");
1683 enum argpar_state_parse_next_status status
;
1685 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1686 status
= argpar_state_parse_next(state
, &item
, &error
);
1687 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1690 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1691 /* Just stop parsing here. */
1693 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1697 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1699 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1700 const struct argpar_item_opt
*item_opt
=
1701 (const struct argpar_item_opt
*) item
;
1703 switch (item_opt
->descr
->id
) {
1705 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1711 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1717 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1723 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1729 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1735 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1740 case OPT_RATE_POLICY
:
1742 policy
= parse_rate_policy(item_opt
->arg
);
1752 const struct argpar_item_non_opt
*item_non_opt
;
1754 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1756 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1758 switch (item_non_opt
->non_opt_index
) {
1760 session_name_arg
= item_non_opt
->arg
;
1763 ERR("Unexpected argument `%s`.",
1770 *argc
-= argpar_state_get_ingested_orig_args(state
);
1771 *argv
+= argpar_state_get_ingested_orig_args(state
);
1773 if (!session_name_arg
) {
1774 ERR("Missing session name.");
1778 /* --ctrl-url and --data-url must come in pair. */
1779 if (ctrl_url_arg
&& !data_url_arg
) {
1780 ERR("--ctrl-url is specified, but --data-url is missing.");
1784 if (!ctrl_url_arg
&& data_url_arg
) {
1785 ERR("--data-url is specified, but --ctrl-url is missing.");
1789 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1790 locations_specified
+= !!url_arg
;
1791 locations_specified
+= !!path_arg
;
1793 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1794 if (locations_specified
> 1) {
1795 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1800 * Did the user specify an option that implies using a
1801 * custom/unregistered output?
1803 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1804 snapshot_output
= lttng_snapshot_output_create();
1805 if (!snapshot_output
) {
1806 ERR("Failed to allocate a snapshot output.");
1811 action
= lttng_action_snapshot_session_create();
1813 ERR("Failed to allocate snapshot session action.");
1817 action_status
= lttng_action_snapshot_session_set_session_name(
1818 action
, session_name_arg
);
1819 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1820 ERR("Failed to set action snapshot session's session name to '%s'.",
1825 if (snapshot_name_arg
) {
1826 if (!snapshot_output
) {
1827 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1831 ret
= lttng_snapshot_output_set_name(
1832 snapshot_name_arg
, snapshot_output
);
1834 ERR("Failed to set name of snapshot output.");
1842 if (!snapshot_output
) {
1843 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1847 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1849 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1853 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1855 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1863 struct lttng_uri
*uris
;
1865 if (!strstr(url_arg
, "://")) {
1866 ERR("Failed to parse '%s' as an URL.", url_arg
);
1870 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1872 ERR("Failed to parse '%s' as an URL.", url_arg
);
1876 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1877 ret
= lttng_snapshot_output_set_local_path(
1878 uris
[0].dst
.path
, snapshot_output
);
1881 ERR("Failed to assign '%s' as a local destination.",
1886 ret
= lttng_snapshot_output_set_network_url(
1887 url_arg
, snapshot_output
);
1890 ERR("Failed to assign '%s' as a network URL.",
1898 ret
= lttng_snapshot_output_set_local_path(
1899 path_arg
, snapshot_output
);
1901 ERR("Failed to parse '%s' as a local path.", path_arg
);
1908 * Two argument form, network output with separate control and
1911 ret
= lttng_snapshot_output_set_network_urls(
1912 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1914 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1915 ctrl_url_arg
, data_url_arg
);
1920 if (snapshot_output
) {
1921 action_status
= lttng_action_snapshot_session_set_output(
1922 action
, snapshot_output
);
1923 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1924 ERR("Failed to set snapshot session action's output.");
1928 /* Ownership of `snapshot_output` has been transferred to the action. */
1929 snapshot_output
= NULL
;
1933 enum lttng_action_status status
;
1934 status
= lttng_action_snapshot_session_set_rate_policy(
1936 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1937 ERR("Failed to set rate policy");
1945 lttng_action_destroy(action
);
1949 free(snapshot_name_arg
);
1954 free(snapshot_output
);
1956 lttng_rate_policy_destroy(policy
);
1957 argpar_state_destroy(state
);
1958 argpar_item_destroy(item
);
1962 struct action_descr
{
1964 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1968 struct action_descr action_descrs
[] = {
1969 { "notify", handle_action_notify
},
1970 { "start-session", handle_action_start_session
},
1971 { "stop-session", handle_action_stop_session
},
1972 { "rotate-session", handle_action_rotate_session
},
1973 { "snapshot-session", handle_action_snapshot_session
},
1977 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1980 struct lttng_action
*action
;
1981 const struct action_descr
*descr
= NULL
;
1983 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1984 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1985 descr
= &action_descrs
[i
];
1991 ERR("Unknown action name: %s", action_name
);
1995 action
= descr
->handler(argc
, argv
);
1997 /* The handler has already printed an error message. */
2009 struct argpar_opt_descr add_trigger_options
[] = {
2010 { OPT_HELP
, 'h', "help", false },
2011 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2012 { OPT_CONDITION
, '\0', "condition", true },
2013 { OPT_ACTION
, '\0', "action", true },
2014 { OPT_NAME
, '\0', "name", true },
2015 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2016 ARGPAR_OPT_DESCR_SENTINEL
,
2020 void lttng_actions_destructor(void *p
)
2022 struct lttng_action
*action
= p
;
2024 lttng_action_destroy(action
);
2027 int cmd_add_trigger(int argc
, const char **argv
)
2030 int my_argc
= argc
- 1;
2031 const char **my_argv
= argv
+ 1;
2032 struct lttng_condition
*condition
= NULL
;
2033 struct lttng_dynamic_pointer_array actions
;
2034 struct argpar_state
*argpar_state
= NULL
;
2035 struct argpar_item
*argpar_item
= NULL
;
2036 struct lttng_action
*action_list
= NULL
;
2037 struct lttng_action
*action
= NULL
;
2038 struct lttng_trigger
*trigger
= NULL
;
2042 char *owner_uid
= NULL
;
2043 enum lttng_error_code ret_code
;
2045 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2048 enum argpar_state_parse_next_status status
;
2049 const struct argpar_item_opt
*item_opt
;
2052 argpar_state_destroy(argpar_state
);
2053 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2054 add_trigger_options
);
2055 if (!argpar_state
) {
2056 ERR("Failed to create argpar state.");
2060 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2061 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2062 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2065 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2068 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2072 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2074 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2075 const struct argpar_item_non_opt
*item_non_opt
=
2076 (const struct argpar_item_non_opt
*)
2079 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2083 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2085 ingested_args
= argpar_state_get_ingested_orig_args(
2088 my_argc
-= ingested_args
;
2089 my_argv
+= ingested_args
;
2091 switch (item_opt
->descr
->id
) {
2096 case OPT_LIST_OPTIONS
:
2097 list_cmd_options_argpar(stdout
, add_trigger_options
);
2103 ERR("A --condition was already given.");
2107 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2110 * An error message was already printed by
2120 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2123 * An error message was already printed by
2129 ret
= lttng_dynamic_pointer_array_add_pointer(
2132 ERR("Failed to add pointer to pointer array.");
2136 /* Ownership of the action was transferred to the group. */
2143 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2151 if (!assign_string(&owner_uid
, item_opt
->arg
,
2164 ERR("Missing --condition.");
2168 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2169 ERR("Need at least one --action.");
2173 action_list
= lttng_action_list_create();
2178 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2179 enum lttng_action_status status
;
2181 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2183 status
= lttng_action_list_add_action(action_list
, action
);
2184 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2189 * The `lttng_action_list_add_action()` takes a reference to
2190 * the action. We can destroy ours.
2192 lttng_action_destroy(action
);
2196 trigger
= lttng_trigger_create(condition
, action_list
);
2202 enum lttng_trigger_status trigger_status
;
2207 uid
= strtol(owner_uid
, &end
, 10);
2208 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2209 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2212 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2213 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2214 ERR("Failed to set trigger's user identity.");
2220 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2222 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2225 if (ret_code
!= LTTNG_OK
) {
2226 ERR("Failed to register trigger: %s.",
2227 lttng_strerror(-ret_code
));
2231 MSG("Trigger registered successfully.");
2240 argpar_state_destroy(argpar_state
);
2241 argpar_item_destroy(argpar_item
);
2242 lttng_dynamic_pointer_array_reset(&actions
);
2243 lttng_condition_destroy(condition
);
2244 lttng_action_destroy(action_list
);
2245 lttng_action_destroy(action
);
2246 lttng_trigger_destroy(trigger
);