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 ||
139 strncmp(arg
, "kernel:syscall",
140 strlen("kernel:syscall")) == 0) {
142 * Matches the following:
146 * - syscall:entry+exit
149 * - kernel:syscall:entry
150 * - kernel:syscall:exit
151 * - kernel:syscall:entry+exit
154 * Validation for the right side is left to further usage sites.
156 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
158 ERR("Invalid `--type` value: %s", arg
);
173 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
178 ERR("Duplicate '%s' given.", opt_name
);
184 PERROR("Failed to allocate string '%s'.", opt_name
);
198 static bool parse_syscall_emission_site_from_type(const char *str
,
199 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
202 const char kernel_prefix
[] = "kernel:";
203 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
206 * If the passed string is of the form "kernel:syscall*", move the
207 * pointer passed "kernel:".
209 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
210 str
= &str
[kernel_prefix_len
];
213 if (strcmp(str
, "syscall") == 0 ||
214 strcmp(str
, "syscall:entry+exit") == 0) {
215 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
216 } else if (strcmp(str
, "syscall:entry") == 0) {
217 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
218 } else if (strcmp(str
, "syscall:exit") == 0) {
219 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
231 * Parse `str` as a log level in domain `domain_type`.
233 * Return the log level in `*log_level`. Return true in `*log_level_only` if
234 * the string specifies exactly this log level, false if it specifies at least
237 * Return true if the string was successfully parsed as a log level string.
239 static bool parse_log_level_string(const char *str
,
240 enum lttng_domain_type domain_type
,
242 bool *log_level_only
)
246 switch (domain_type
) {
247 case LTTNG_DOMAIN_UST
:
249 enum lttng_loglevel log_level_min
, log_level_max
;
250 if (!loglevel_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_EMERG
) {
261 *log_level
= (int) log_level_min
;
262 *log_level_only
= log_level_min
== log_level_max
;
265 case LTTNG_DOMAIN_LOG4J
:
267 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
268 if (!loglevel_log4j_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_LOG4J_FATAL
) {
279 *log_level
= (int) log_level_min
;
280 *log_level_only
= log_level_min
== log_level_max
;
283 case LTTNG_DOMAIN_JUL
:
285 enum lttng_loglevel_jul log_level_min
, log_level_max
;
286 if (!loglevel_jul_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
&&
293 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
297 *log_level
= (int) log_level_min
;
298 *log_level_only
= log_level_min
== log_level_max
;
301 case LTTNG_DOMAIN_PYTHON
:
303 enum lttng_loglevel_python log_level_min
, log_level_max
;
304 if (!loglevel_python_parse_range_string(
305 str
, &log_level_min
, &log_level_max
)) {
309 /* Only support VAL and VAL.. for now. */
310 if (log_level_min
!= log_level_max
&&
312 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
316 *log_level
= (int) log_level_min
;
317 *log_level_only
= log_level_min
== log_level_max
;
321 /* Invalid domain type. */
335 static int parse_kernel_probe_opts(const char *source
,
336 struct lttng_kernel_probe_location
**location
)
341 char name
[LTTNG_SYMBOL_NAME_LEN
];
342 char *symbol_name
= NULL
;
345 /* Check for symbol+offset. */
346 match
= sscanf(source
,
347 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
351 if (*s_hex
== '\0') {
352 ERR("Kernel probe symbol offset is missing.");
356 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
358 PERROR("Failed to copy kernel probe location symbol name.");
361 offset
= strtoul(s_hex
, NULL
, 0);
363 *location
= lttng_kernel_probe_location_symbol_create(
364 symbol_name
, offset
);
366 ERR("Failed to create symbol kernel probe location.");
373 /* Check for symbol. */
374 if (isalpha(name
[0]) || name
[0] == '_') {
375 match
= sscanf(source
,
376 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
380 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
382 ERR("Failed to copy kernel probe location symbol name.");
386 *location
= lttng_kernel_probe_location_symbol_create(
389 ERR("Failed to create symbol kernel probe location.");
397 /* Check for address. */
398 match
= sscanf(source
, "%18s", s_hex
);
402 if (*s_hex
== '\0') {
403 ERR("Invalid kernel probe location address.");
407 address
= strtoul(s_hex
, NULL
, 0);
408 *location
= lttng_kernel_probe_location_address_create(address
);
410 ERR("Failed to create symbol kernel probe location.");
428 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
429 const struct ir_load_expression
*load_expr
,
430 const char *capture_str
)
432 char *provider_name
= NULL
;
433 struct lttng_event_expr
*event_expr
= NULL
;
434 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
435 const enum ir_load_expression_type load_expr_child_type
=
438 switch (load_expr_child_type
) {
439 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
440 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
442 const char *field_name
;
444 load_expr_op
= load_expr_op
->next
;
445 assert(load_expr_op
);
446 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
447 field_name
= load_expr_op
->u
.symbol
;
450 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
451 lttng_event_expr_event_payload_field_create(field_name
) :
452 lttng_event_expr_channel_context_field_create(field_name
);
454 ERR("Failed to create %s event expression: field name = `%s`.",
455 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
456 "payload field" : "channel context",
463 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
466 const char *type_name
;
467 const char *field_name
;
469 load_expr_op
= load_expr_op
->next
;
470 assert(load_expr_op
);
471 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
472 field_name
= load_expr_op
->u
.symbol
;
476 * The field name needs to be of the form PROVIDER:TYPE. We
479 colon
= strchr(field_name
, ':');
481 ERR("Invalid app-specific context field name: missing colon in `%s`.",
486 type_name
= colon
+ 1;
487 if (*type_name
== '\0') {
488 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
493 provider_name
= strndup(field_name
, colon
- field_name
);
494 if (!provider_name
) {
495 PERROR("Failed to allocate field name string");
499 event_expr
= lttng_event_expr_app_specific_context_field_create(
500 provider_name
, type_name
);
502 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
503 provider_name
, type_name
);
510 ERR("%s: unexpected load expr type %d.", __func__
,
515 load_expr_op
= load_expr_op
->next
;
517 /* There may be a single array index after that. */
518 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
519 struct lttng_event_expr
*index_event_expr
;
520 const uint64_t index
= load_expr_op
->u
.index
;
522 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
523 if (!index_event_expr
) {
524 ERR("Failed to create array field element event expression.");
528 event_expr
= index_event_expr
;
529 load_expr_op
= load_expr_op
->next
;
532 switch (load_expr_op
->type
) {
533 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
535 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
536 * always found at the end of the chain.
539 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
540 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
545 ERR("%s: unexpected load expression operator %s.", __func__
,
546 ir_load_expression_type_str(load_expr_op
->type
));
553 lttng_event_expr_destroy(event_expr
);
563 struct lttng_event_expr
*ir_op_load_to_event_expr(
564 const struct ir_op
*ir
, const char *capture_str
)
566 struct lttng_event_expr
*event_expr
= NULL
;
568 assert(ir
->op
== IR_OP_LOAD
);
570 switch (ir
->data_type
) {
571 case IR_DATA_EXPRESSION
:
573 const struct ir_load_expression
*ir_load_expr
=
574 ir
->u
.load
.u
.expression
;
576 event_expr
= ir_op_load_expr_to_event_expr(
577 ir_load_expr
, capture_str
);
581 ERR("%s: unexpected data type: %s.", __func__
,
582 ir_data_type_str(ir
->data_type
));
590 const char *ir_operator_type_human_str(enum ir_op_type op
)
612 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
613 const char *capture_str
)
615 struct lttng_event_expr
*event_expr
= NULL
;
617 assert(ir
->op
== IR_OP_ROOT
);
618 ir
= ir
->u
.root
.child
;
622 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
627 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
629 ir_operator_type_human_str(ir
->op
));
632 ERR("%s: unexpected IR op type: %s.", __func__
,
633 ir_op_type_str(ir
->op
));
641 void destroy_event_expr(void *ptr
)
643 lttng_event_expr_destroy(ptr
);
646 struct parse_event_rule_res
{
648 struct lttng_event_rule
*er
;
650 /* Array of `struct lttng_event_expr *` */
651 struct lttng_dynamic_pointer_array capture_descriptors
;
655 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
657 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
658 enum lttng_event_rule_type event_rule_type
=
659 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
660 struct argpar_state
*state
;
661 struct argpar_item
*item
= NULL
;
663 int consumed_args
= -1;
664 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
665 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
666 struct parse_event_rule_res res
= { 0 };
667 struct lttng_event_expr
*event_expr
= NULL
;
668 struct filter_parser_ctx
*parser_ctx
= NULL
;
669 struct lttng_log_level_rule
*log_level_rule
= NULL
;
671 /* Event rule type option */
672 char *event_rule_type_str
= NULL
;
674 /* Tracepoint and syscall options. */
676 /* Array of strings. */
677 struct lttng_dynamic_pointer_array exclude_names
;
679 /* For userspace / kernel probe and function. */
680 char *location
= NULL
;
681 char *event_name
= NULL
;
687 char *log_level_str
= NULL
;
689 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
692 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
694 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
696 ERR("Failed to allocate an argpar state.");
701 enum argpar_state_parse_next_status status
;
703 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
704 status
= argpar_state_parse_next(state
, &item
, &error
);
705 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
708 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
709 /* Just stop parsing here. */
711 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
715 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
717 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
718 const struct argpar_item_opt
*item_opt
=
719 (const struct argpar_item_opt
*) item
;
721 switch (item_opt
->descr
->id
) {
724 if (!assign_domain_type(&domain_type
,
731 if (!assign_event_rule_type(&event_rule_type
,
736 /* Save the string for later use. */
737 if (!assign_string(&event_rule_type_str
,
745 if (!assign_string(&location
,
753 if (!assign_string(&event_name
,
755 "--event-name/-E")) {
761 if (!assign_string(&filter
, item_opt
->arg
,
768 if (!assign_string(&name
, item_opt
->arg
,
774 case OPT_EXCLUDE_NAME
:
778 ret
= lttng_dynamic_pointer_array_add_pointer(
780 strdup(item_opt
->arg
));
782 ERR("Failed to add pointer to dynamic pointer array.");
789 if (!assign_string(&log_level_str
,
790 item_opt
->arg
, "--log-level/-l")) {
798 const char *capture_str
= item_opt
->arg
;
800 ret
= filter_parser_ctx_create_from_filter_expression(
801 capture_str
, &parser_ctx
);
803 ERR("Failed to parse capture expression `%s`.",
808 event_expr
= ir_op_root_to_event_expr(
813 * ir_op_root_to_event_expr has printed
819 ret
= lttng_dynamic_pointer_array_add_pointer(
820 &res
.capture_descriptors
,
827 * The ownership of event expression was
828 * transferred to the dynamic array.
838 const struct argpar_item_non_opt
*item_non_opt
=
839 (const struct argpar_item_non_opt
*)
842 /* Don't accept non-option arguments. */
843 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
848 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
849 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
853 * Option --name is applicable to event rules of type tracepoint
854 * and syscall. For tracepoint and syscall rules, if --name is
855 * omitted, it is implicitly "*".
857 switch (event_rule_type
) {
858 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
859 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
867 ERR("Can't use --name with %s event rules.",
868 lttng_event_rule_type_str(
873 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
874 ERR("Can't use --exclude-name/-x with %s event rules.",
875 lttng_event_rule_type_str(
882 * Option --location is only applicable to (and mandatory for) event
883 * rules of type {k,u}probe and function.
885 * Option --event-name is only applicable to event rules of type probe.
886 * If omitted, it defaults to the location.
888 switch (event_rule_type
) {
889 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
890 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
891 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
893 ERR("Event rule of type %s requires a --location.",
894 lttng_event_rule_type_str(event_rule_type
));
899 event_name
= strdup(location
);
906 ERR("Can't use --location with %s event rules.",
907 lttng_event_rule_type_str(event_rule_type
));
912 ERR("Can't use --event-name with %s event rules.",
913 lttng_event_rule_type_str(
920 * Update *argc and *argv so our caller can keep parsing what follows.
922 consumed_args
= argpar_state_get_ingested_orig_args(state
);
923 assert(consumed_args
>= 0);
924 *argc
-= consumed_args
;
925 *argv
+= consumed_args
;
927 /* Need to specify a domain. */
928 if (domain_type
== LTTNG_DOMAIN_NONE
) {
929 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
933 /* Validate event rule type against domain. */
934 switch (event_rule_type
) {
935 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
936 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
937 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
938 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
939 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
940 ERR("Event type not available for user-space tracing.");
945 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
953 * Adding a filter to a probe, function or userspace-probe would be
954 * denied by the kernel tracer as it's not supported at the moment. We
955 * do an early check here to warn the user.
957 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
958 switch (event_rule_type
) {
959 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
960 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
963 ERR("Filter expressions are not supported for %s event rules.",
964 lttng_event_rule_type_str(event_rule_type
));
969 /* If --exclude-name/-x was passed, split it into an exclusion list. */
970 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
971 if (domain_type
!= LTTNG_DOMAIN_UST
) {
972 ERR("Event name exclusions are not yet implemented for %s event rules.",
973 get_domain_str(domain_type
));
977 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
979 * Assume validate_exclusion_list already prints an
987 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
988 ERR("Log levels are only applicable to tracepoint event rules.");
992 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
993 ERR("Log levels are not supported by the kernel tracer.");
998 /* Finally, create the event rule object. */
999 switch (event_rule_type
) {
1000 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
1002 enum lttng_event_rule_status event_rule_status
;
1004 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
1006 ERR("Failed to create tracepoint event rule.");
1011 event_rule_status
= lttng_event_rule_tracepoint_set_name_pattern(
1013 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1014 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
1021 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1023 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1024 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1030 /* Set exclusion list. */
1031 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1033 int count
= lttng_dynamic_pointer_array_get_count(
1036 for (n
= 0; n
< count
; n
++) {
1037 const char *exclude_name
=
1038 lttng_dynamic_pointer_array_get_pointer(
1043 lttng_event_rule_tracepoint_add_name_pattern_exclusion(
1046 if (event_rule_status
!=
1047 LTTNG_EVENT_RULE_STATUS_OK
) {
1048 ERR("Failed to set tracepoint exclusion list element '%s'",
1056 * ".." is the same as passing no log level option and
1057 * correspond the the "ANY" case.
1059 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
1061 bool log_level_only
;
1063 if (!parse_log_level_string(log_level_str
, domain_type
,
1064 &log_level
, &log_level_only
)) {
1065 ERR("Failed to parse log level string `%s`.",
1070 if (log_level_only
) {
1071 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
1073 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1076 if (log_level_rule
== NULL
) {
1077 ERR("Failed to create log level rule object.");
1082 lttng_event_rule_tracepoint_set_log_level_rule(
1083 res
.er
, log_level_rule
);
1085 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1086 ERR("Failed to set log level on event fule.");
1093 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1096 enum lttng_event_rule_status event_rule_status
;
1098 ret
= parse_kernel_probe_opts(
1099 location
, &kernel_probe_location
);
1101 ERR("Failed to parse kernel probe location.");
1105 assert(kernel_probe_location
);
1106 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1108 ERR("Failed to create kprobe event rule.");
1113 lttng_event_rule_kernel_probe_set_event_name(
1114 res
.er
, event_name
);
1115 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1116 ERR("Failed to set kprobe event rule's name to '%s'.",
1123 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1126 enum lttng_event_rule_status event_rule_status
;
1128 ret
= parse_userspace_probe_opts(
1129 location
, &userspace_probe_location
);
1131 ERR("Failed to parse user space probe location.");
1135 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1137 ERR("Failed to create userspace probe event rule.");
1142 lttng_event_rule_userspace_probe_set_event_name(
1143 res
.er
, event_name
);
1144 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1145 ERR("Failed to set user space probe event rule's name to '%s'.",
1152 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1154 enum lttng_event_rule_status event_rule_status
;
1155 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1157 if (!parse_syscall_emission_site_from_type(
1158 event_rule_type_str
, &emission_site
)) {
1159 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1163 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1165 ERR("Failed to create syscall event rule.");
1169 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1171 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1172 ERR("Failed to set syscall event rule's pattern to '%s'.",
1178 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1180 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1181 ERR("Failed to set syscall event rule's filter to '%s'.",
1197 lttng_event_rule_destroy(res
.er
);
1199 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1203 filter_parser_ctx_free(parser_ctx
);
1206 lttng_event_expr_destroy(event_expr
);
1207 argpar_item_destroy(item
);
1209 argpar_state_destroy(state
);
1212 lttng_dynamic_pointer_array_reset(&exclude_names
);
1213 free(log_level_str
);
1216 free(event_rule_type_str
);
1218 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1219 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1220 lttng_log_level_rule_destroy(log_level_rule
);
1225 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1227 struct parse_event_rule_res res
;
1228 struct lttng_condition
*c
;
1231 res
= parse_event_rule(argc
, argv
);
1237 c
= lttng_condition_event_rule_matches_create(res
.er
);
1238 lttng_event_rule_destroy(res
.er
);
1244 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1246 enum lttng_condition_status status
;
1247 struct lttng_event_expr
**expr
=
1248 lttng_dynamic_array_get_element(
1249 &res
.capture_descriptors
.array
, i
);
1253 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1255 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1256 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1257 ERR("The capture feature is unsupported by the event-rule condition type");
1263 /* Ownership of event expression moved to `c` */
1270 lttng_condition_destroy(c
);
1274 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1275 lttng_event_rule_destroy(res
.er
);
1279 struct condition_descr
{
1281 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1285 struct condition_descr condition_descrs
[] = {
1286 { "event-rule-matches", handle_condition_event
},
1290 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1294 struct lttng_condition
*cond
;
1295 const struct condition_descr
*descr
= NULL
;
1297 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1298 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1299 descr
= &condition_descrs
[i
];
1305 ERR("Unknown condition name '%s'", condition_name
);
1309 cond
= descr
->handler(argc
, argv
);
1311 /* The handler has already printed an error message. */
1322 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1325 size_t num_token
= 0;
1326 struct lttng_dynamic_pointer_array tokens
;
1327 struct lttng_rate_policy
*policy
= NULL
;
1328 enum lttng_rate_policy_type policy_type
;
1329 unsigned long long value
;
1330 char *policy_type_str
;
1331 char *policy_value_str
;
1334 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1336 /* Rate policy fields are separated by ':'. */
1337 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1339 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1343 * Early sanity check that the number of parameter is exactly 2.
1346 if (num_token
!= 2) {
1347 ERR("Rate policy format is invalid.");
1351 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1352 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1354 /* Parse the type. */
1355 if (strcmp(policy_type_str
, "once-after") == 0) {
1356 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1357 } else if (strcmp(policy_type_str
, "every") == 0) {
1358 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1360 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1364 /* Parse the value. */
1365 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1366 ERR("Failed to parse rate policy value `%s` as an integer.",
1372 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1376 switch (policy_type
) {
1377 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1378 policy
= lttng_rate_policy_every_n_create(value
);
1380 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1381 policy
= lttng_rate_policy_once_after_n_create(value
);
1387 if (policy
== NULL
) {
1388 ERR("Failed to create rate policy `%s`.", policy_str
);
1392 lttng_dynamic_pointer_array_reset(&tokens
);
1396 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1397 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1398 ARGPAR_OPT_DESCR_SENTINEL
1402 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1404 struct lttng_action
*action
= NULL
;
1405 struct argpar_state
*state
= NULL
;
1406 struct argpar_item
*item
= NULL
;
1408 struct lttng_rate_policy
*policy
= NULL
;
1410 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1412 ERR("Failed to allocate an argpar state.");
1417 enum argpar_state_parse_next_status status
;
1419 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1420 status
= argpar_state_parse_next(state
, &item
, &error
);
1421 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1424 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1425 /* Just stop parsing here. */
1427 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1431 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1433 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1434 const struct argpar_item_opt
*item_opt
=
1435 (const struct argpar_item_opt
*) item
;
1437 switch (item_opt
->descr
->id
) {
1438 case OPT_RATE_POLICY
:
1440 policy
= parse_rate_policy(item_opt
->arg
);
1450 const struct argpar_item_non_opt
*item_non_opt
;
1452 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1454 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1456 switch (item_non_opt
->non_opt_index
) {
1458 ERR("Unexpected argument `%s`.",
1465 *argc
-= argpar_state_get_ingested_orig_args(state
);
1466 *argv
+= argpar_state_get_ingested_orig_args(state
);
1468 action
= lttng_action_notify_create();
1470 ERR("Failed to create notify action");
1475 enum lttng_action_status status
;
1476 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1477 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1478 ERR("Failed to set rate policy");
1486 lttng_action_destroy(action
);
1490 lttng_rate_policy_destroy(policy
);
1491 argpar_state_destroy(state
);
1492 argpar_item_destroy(item
);
1497 * Generic handler for a kind of action that takes a session name and an
1498 * optional rate policy.
1501 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1503 struct lttng_action
*(*create_action_cb
)(void),
1504 enum lttng_action_status (*set_session_name_cb
)(
1505 struct lttng_action
*, const char *),
1506 enum lttng_action_status (*set_rate_policy_cb
)(
1507 struct lttng_action
*,
1508 const struct lttng_rate_policy
*),
1509 const char *action_name
)
1511 struct lttng_action
*action
= NULL
;
1512 struct argpar_state
*state
= NULL
;
1513 struct argpar_item
*item
= NULL
;
1514 const char *session_name_arg
= NULL
;
1516 enum lttng_action_status action_status
;
1517 struct lttng_rate_policy
*policy
= NULL
;
1519 assert(set_session_name_cb
);
1520 assert(set_rate_policy_cb
);
1522 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1523 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1524 ARGPAR_OPT_DESCR_SENTINEL
1527 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1529 ERR("Failed to allocate an argpar state.");
1534 enum argpar_state_parse_next_status status
;
1536 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1537 status
= argpar_state_parse_next(state
, &item
, &error
);
1538 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1541 } else if (status
==
1542 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1543 /* Just stop parsing here. */
1545 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1549 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1550 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1551 const struct argpar_item_opt
*item_opt
=
1552 (const struct argpar_item_opt
*) item
;
1554 switch (item_opt
->descr
->id
) {
1555 case OPT_RATE_POLICY
:
1557 policy
= parse_rate_policy(item_opt
->arg
);
1567 const struct argpar_item_non_opt
*item_non_opt
;
1568 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1570 switch (item_non_opt
->non_opt_index
) {
1572 session_name_arg
= item_non_opt
->arg
;
1575 ERR("Unexpected argument `%s`.",
1582 *argc
-= argpar_state_get_ingested_orig_args(state
);
1583 *argv
+= argpar_state_get_ingested_orig_args(state
);
1585 if (!session_name_arg
) {
1586 ERR("Missing session name.");
1590 action
= create_action_cb();
1592 ERR("Failed to allocate %s session action.", action_name
);
1596 action_status
= set_session_name_cb(action
, session_name_arg
);
1597 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1598 ERR("Failed to set action %s session's session name to '%s'.",
1599 action_name
, session_name_arg
);
1604 action_status
= set_rate_policy_cb(action
, policy
);
1605 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1606 ERR("Failed to set rate policy");
1614 lttng_action_destroy(action
);
1616 argpar_item_destroy(item
);
1618 lttng_rate_policy_destroy(policy
);
1620 argpar_state_destroy(state
);
1625 struct lttng_action
*handle_action_start_session(int *argc
,
1628 return handle_action_simple_session_with_policy(argc
, argv
,
1629 lttng_action_start_session_create
,
1630 lttng_action_start_session_set_session_name
,
1631 lttng_action_start_session_set_rate_policy
, "start");
1635 struct lttng_action
*handle_action_stop_session(int *argc
,
1638 return handle_action_simple_session_with_policy(argc
, argv
,
1639 lttng_action_stop_session_create
,
1640 lttng_action_stop_session_set_session_name
,
1641 lttng_action_stop_session_set_rate_policy
, "stop");
1645 struct lttng_action
*handle_action_rotate_session(int *argc
,
1648 return handle_action_simple_session_with_policy(argc
, argv
,
1649 lttng_action_rotate_session_create
,
1650 lttng_action_rotate_session_set_session_name
,
1651 lttng_action_rotate_session_set_rate_policy
,
1655 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1656 { OPT_NAME
, 'n', "name", true },
1657 { OPT_MAX_SIZE
, 'm', "max-size", true },
1658 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1659 { OPT_DATA_URL
, '\0', "data-url", true },
1660 { OPT_URL
, '\0', "url", true },
1661 { OPT_PATH
, '\0', "path", true },
1662 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1663 ARGPAR_OPT_DESCR_SENTINEL
1667 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1670 struct lttng_action
*action
= NULL
;
1671 struct argpar_state
*state
= NULL
;
1672 struct argpar_item
*item
= NULL
;
1673 const char *session_name_arg
= NULL
;
1674 char *snapshot_name_arg
= NULL
;
1675 char *ctrl_url_arg
= NULL
;
1676 char *data_url_arg
= NULL
;
1677 char *max_size_arg
= NULL
;
1678 char *url_arg
= NULL
;
1679 char *path_arg
= NULL
;
1681 enum lttng_action_status action_status
;
1682 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1683 struct lttng_rate_policy
*policy
= NULL
;
1685 unsigned int locations_specified
= 0;
1687 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1689 ERR("Failed to allocate an argpar state.");
1694 enum argpar_state_parse_next_status status
;
1696 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1697 status
= argpar_state_parse_next(state
, &item
, &error
);
1698 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1701 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1702 /* Just stop parsing here. */
1704 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1708 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1710 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1711 const struct argpar_item_opt
*item_opt
=
1712 (const struct argpar_item_opt
*) item
;
1714 switch (item_opt
->descr
->id
) {
1716 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1722 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1728 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1734 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1740 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1746 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1751 case OPT_RATE_POLICY
:
1753 policy
= parse_rate_policy(item_opt
->arg
);
1763 const struct argpar_item_non_opt
*item_non_opt
;
1765 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1767 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1769 switch (item_non_opt
->non_opt_index
) {
1771 session_name_arg
= item_non_opt
->arg
;
1774 ERR("Unexpected argument `%s`.",
1781 *argc
-= argpar_state_get_ingested_orig_args(state
);
1782 *argv
+= argpar_state_get_ingested_orig_args(state
);
1784 if (!session_name_arg
) {
1785 ERR("Missing session name.");
1789 /* --ctrl-url and --data-url must come in pair. */
1790 if (ctrl_url_arg
&& !data_url_arg
) {
1791 ERR("--ctrl-url is specified, but --data-url is missing.");
1795 if (!ctrl_url_arg
&& data_url_arg
) {
1796 ERR("--data-url is specified, but --ctrl-url is missing.");
1800 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1801 locations_specified
+= !!url_arg
;
1802 locations_specified
+= !!path_arg
;
1804 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1805 if (locations_specified
> 1) {
1806 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1811 * Did the user specify an option that implies using a
1812 * custom/unregistered output?
1814 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1815 snapshot_output
= lttng_snapshot_output_create();
1816 if (!snapshot_output
) {
1817 ERR("Failed to allocate a snapshot output.");
1822 action
= lttng_action_snapshot_session_create();
1824 ERR("Failed to allocate snapshot session action.");
1828 action_status
= lttng_action_snapshot_session_set_session_name(
1829 action
, session_name_arg
);
1830 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1831 ERR("Failed to set action snapshot session's session name to '%s'.",
1836 if (snapshot_name_arg
) {
1837 if (!snapshot_output
) {
1838 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1842 ret
= lttng_snapshot_output_set_name(
1843 snapshot_name_arg
, snapshot_output
);
1845 ERR("Failed to set name of snapshot output.");
1853 if (!snapshot_output
) {
1854 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1858 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1860 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1864 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1866 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1874 struct lttng_uri
*uris
;
1876 if (!strstr(url_arg
, "://")) {
1877 ERR("Failed to parse '%s' as an URL.", url_arg
);
1881 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1883 ERR("Failed to parse '%s' as an URL.", url_arg
);
1887 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1888 ret
= lttng_snapshot_output_set_local_path(
1889 uris
[0].dst
.path
, snapshot_output
);
1892 ERR("Failed to assign '%s' as a local destination.",
1897 ret
= lttng_snapshot_output_set_network_url(
1898 url_arg
, snapshot_output
);
1901 ERR("Failed to assign '%s' as a network URL.",
1909 ret
= lttng_snapshot_output_set_local_path(
1910 path_arg
, snapshot_output
);
1912 ERR("Failed to parse '%s' as a local path.", path_arg
);
1919 * Two argument form, network output with separate control and
1922 ret
= lttng_snapshot_output_set_network_urls(
1923 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1925 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1926 ctrl_url_arg
, data_url_arg
);
1931 if (snapshot_output
) {
1932 action_status
= lttng_action_snapshot_session_set_output(
1933 action
, snapshot_output
);
1934 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1935 ERR("Failed to set snapshot session action's output.");
1939 /* Ownership of `snapshot_output` has been transferred to the action. */
1940 snapshot_output
= NULL
;
1944 enum lttng_action_status status
;
1945 status
= lttng_action_snapshot_session_set_rate_policy(
1947 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1948 ERR("Failed to set rate policy");
1956 lttng_action_destroy(action
);
1960 free(snapshot_name_arg
);
1965 free(snapshot_output
);
1967 lttng_rate_policy_destroy(policy
);
1968 argpar_state_destroy(state
);
1969 argpar_item_destroy(item
);
1973 struct action_descr
{
1975 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1979 struct action_descr action_descrs
[] = {
1980 { "notify", handle_action_notify
},
1981 { "start-session", handle_action_start_session
},
1982 { "stop-session", handle_action_stop_session
},
1983 { "rotate-session", handle_action_rotate_session
},
1984 { "snapshot-session", handle_action_snapshot_session
},
1988 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1991 struct lttng_action
*action
;
1992 const struct action_descr
*descr
= NULL
;
1994 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1995 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1996 descr
= &action_descrs
[i
];
2002 ERR("Unknown action name: %s", action_name
);
2006 action
= descr
->handler(argc
, argv
);
2008 /* The handler has already printed an error message. */
2020 struct argpar_opt_descr add_trigger_options
[] = {
2021 { OPT_HELP
, 'h', "help", false },
2022 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2023 { OPT_CONDITION
, '\0', "condition", true },
2024 { OPT_ACTION
, '\0', "action", true },
2025 { OPT_NAME
, '\0', "name", true },
2026 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2027 ARGPAR_OPT_DESCR_SENTINEL
,
2031 void lttng_actions_destructor(void *p
)
2033 struct lttng_action
*action
= p
;
2035 lttng_action_destroy(action
);
2038 int cmd_add_trigger(int argc
, const char **argv
)
2041 int my_argc
= argc
- 1;
2042 const char **my_argv
= argv
+ 1;
2043 struct lttng_condition
*condition
= NULL
;
2044 struct lttng_dynamic_pointer_array actions
;
2045 struct argpar_state
*argpar_state
= NULL
;
2046 struct argpar_item
*argpar_item
= NULL
;
2047 struct lttng_action
*action_list
= NULL
;
2048 struct lttng_action
*action
= NULL
;
2049 struct lttng_trigger
*trigger
= NULL
;
2053 char *owner_uid
= NULL
;
2054 enum lttng_error_code ret_code
;
2056 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2059 enum argpar_state_parse_next_status status
;
2060 const struct argpar_item_opt
*item_opt
;
2063 argpar_state_destroy(argpar_state
);
2064 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2065 add_trigger_options
);
2066 if (!argpar_state
) {
2067 ERR("Failed to create argpar state.");
2071 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2072 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2073 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2076 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2079 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2083 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2085 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2086 const struct argpar_item_non_opt
*item_non_opt
=
2087 (const struct argpar_item_non_opt
*)
2090 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2094 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2096 ingested_args
= argpar_state_get_ingested_orig_args(
2099 my_argc
-= ingested_args
;
2100 my_argv
+= ingested_args
;
2102 switch (item_opt
->descr
->id
) {
2107 case OPT_LIST_OPTIONS
:
2108 list_cmd_options_argpar(stdout
, add_trigger_options
);
2114 ERR("A --condition was already given.");
2118 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2121 * An error message was already printed by
2131 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2134 * An error message was already printed by
2140 ret
= lttng_dynamic_pointer_array_add_pointer(
2143 ERR("Failed to add pointer to pointer array.");
2147 /* Ownership of the action was transferred to the group. */
2154 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2162 if (!assign_string(&owner_uid
, item_opt
->arg
,
2175 ERR("Missing --condition.");
2179 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2180 ERR("Need at least one --action.");
2184 action_list
= lttng_action_list_create();
2189 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2190 enum lttng_action_status status
;
2192 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2194 status
= lttng_action_list_add_action(action_list
, action
);
2195 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2200 * The `lttng_action_list_add_action()` takes a reference to
2201 * the action. We can destroy ours.
2203 lttng_action_destroy(action
);
2207 trigger
= lttng_trigger_create(condition
, action_list
);
2213 enum lttng_trigger_status trigger_status
;
2218 uid
= strtol(owner_uid
, &end
, 10);
2219 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2220 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2224 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2225 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2226 ERR("Failed to set trigger's user identity.");
2232 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2234 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2237 if (ret_code
!= LTTNG_OK
) {
2238 ERR("Failed to register trigger: %s.",
2239 lttng_strerror(-ret_code
));
2243 MSG("Trigger registered successfully.");
2252 argpar_state_destroy(argpar_state
);
2253 argpar_item_destroy(argpar_item
);
2254 lttng_dynamic_pointer_array_reset(&actions
);
2255 lttng_condition_destroy(condition
);
2256 lttng_action_destroy(action_list
);
2257 lttng_action_destroy(action
);
2258 lttng_trigger_destroy(trigger
);