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
, "kernel:uprobe") == 0) {
134 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
135 } else if (strcmp(arg
, "function") == 0) {
136 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
;
137 } else if (strncmp(arg
, "syscall", strlen("syscall")) == 0 ||
138 strncmp(arg
, "kernel:syscall",
139 strlen("kernel:syscall")) == 0) {
141 * Matches the following:
145 * - syscall:entry+exit
148 * - kernel:syscall:entry
149 * - kernel:syscall:exit
150 * - kernel:syscall:entry+exit
153 * Validation for the right side is left to further usage sites.
155 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
157 ERR("Invalid `--type` value: %s", arg
);
172 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
177 ERR("Duplicate '%s' given.", opt_name
);
183 PERROR("Failed to allocate string '%s'.", opt_name
);
197 static bool parse_syscall_emission_site_from_type(const char *str
,
198 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
201 const char kernel_prefix
[] = "kernel:";
202 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
205 * If the passed string is of the form "kernel:syscall*", move the
206 * pointer passed "kernel:".
208 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
209 str
= &str
[kernel_prefix_len
];
212 if (strcmp(str
, "syscall") == 0 ||
213 strcmp(str
, "syscall:entry+exit") == 0) {
214 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
215 } else if (strcmp(str
, "syscall:entry") == 0) {
216 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
217 } else if (strcmp(str
, "syscall:exit") == 0) {
218 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
230 * Parse `str` as a log level in domain `domain_type`.
232 * Return the log level in `*log_level`. Return true in `*log_level_only` if
233 * the string specifies exactly this log level, false if it specifies at least
236 * Return true if the string was successfully parsed as a log level string.
238 static bool parse_log_level_string(const char *str
,
239 enum lttng_domain_type domain_type
,
241 bool *log_level_only
)
245 switch (domain_type
) {
246 case LTTNG_DOMAIN_UST
:
248 enum lttng_loglevel log_level_min
, log_level_max
;
249 if (!loglevel_parse_range_string(
250 str
, &log_level_min
, &log_level_max
)) {
254 /* Only support VAL and VAL.. for now. */
255 if (log_level_min
!= log_level_max
&&
256 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
260 *log_level
= (int) log_level_min
;
261 *log_level_only
= log_level_min
== log_level_max
;
264 case LTTNG_DOMAIN_LOG4J
:
266 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
267 if (!loglevel_log4j_parse_range_string(
268 str
, &log_level_min
, &log_level_max
)) {
272 /* Only support VAL and VAL.. for now. */
273 if (log_level_min
!= log_level_max
&&
274 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
278 *log_level
= (int) log_level_min
;
279 *log_level_only
= log_level_min
== log_level_max
;
282 case LTTNG_DOMAIN_JUL
:
284 enum lttng_loglevel_jul log_level_min
, log_level_max
;
285 if (!loglevel_jul_parse_range_string(
286 str
, &log_level_min
, &log_level_max
)) {
290 /* Only support VAL and VAL.. for now. */
291 if (log_level_min
!= log_level_max
&&
292 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
296 *log_level
= (int) log_level_min
;
297 *log_level_only
= log_level_min
== log_level_max
;
300 case LTTNG_DOMAIN_PYTHON
:
302 enum lttng_loglevel_python log_level_min
, log_level_max
;
303 if (!loglevel_python_parse_range_string(
304 str
, &log_level_min
, &log_level_max
)) {
308 /* Only support VAL and VAL.. for now. */
309 if (log_level_min
!= log_level_max
&&
311 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
315 *log_level
= (int) log_level_min
;
316 *log_level_only
= log_level_min
== log_level_max
;
320 /* Invalid domain type. */
334 static int parse_kernel_probe_opts(const char *source
,
335 struct lttng_kernel_probe_location
**location
)
340 char name
[LTTNG_SYMBOL_NAME_LEN
];
341 char *symbol_name
= NULL
;
344 /* Check for symbol+offset. */
345 match
= sscanf(source
,
346 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
350 if (*s_hex
== '\0') {
351 ERR("Kernel probe symbol offset is missing.");
355 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
357 PERROR("Failed to copy kernel probe location symbol name.");
360 offset
= strtoul(s_hex
, NULL
, 0);
362 *location
= lttng_kernel_probe_location_symbol_create(
363 symbol_name
, offset
);
365 ERR("Failed to create symbol kernel probe location.");
372 /* Check for symbol. */
373 if (isalpha(name
[0]) || name
[0] == '_') {
374 match
= sscanf(source
,
375 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
379 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
381 ERR("Failed to copy kernel probe location symbol name.");
385 *location
= lttng_kernel_probe_location_symbol_create(
388 ERR("Failed to create symbol kernel probe location.");
396 /* Check for address. */
397 match
= sscanf(source
, "%18s", s_hex
);
401 if (*s_hex
== '\0') {
402 ERR("Invalid kernel probe location address.");
406 address
= strtoul(s_hex
, NULL
, 0);
407 *location
= lttng_kernel_probe_location_address_create(address
);
409 ERR("Failed to create symbol kernel probe location.");
427 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
428 const struct ir_load_expression
*load_expr
,
429 const char *capture_str
)
431 char *provider_name
= NULL
;
432 struct lttng_event_expr
*event_expr
= NULL
;
433 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
434 const enum ir_load_expression_type load_expr_child_type
=
437 switch (load_expr_child_type
) {
438 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
439 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
441 const char *field_name
;
443 load_expr_op
= load_expr_op
->next
;
444 assert(load_expr_op
);
445 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
446 field_name
= load_expr_op
->u
.symbol
;
449 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
450 lttng_event_expr_event_payload_field_create(field_name
) :
451 lttng_event_expr_channel_context_field_create(field_name
);
453 ERR("Failed to create %s event expression: field name = `%s`.",
454 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
455 "payload field" : "channel context",
462 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
465 const char *type_name
;
466 const char *field_name
;
468 load_expr_op
= load_expr_op
->next
;
469 assert(load_expr_op
);
470 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
471 field_name
= load_expr_op
->u
.symbol
;
475 * The field name needs to be of the form PROVIDER:TYPE. We
478 colon
= strchr(field_name
, ':');
480 ERR("Invalid app-specific context field name: missing colon in `%s`.",
485 type_name
= colon
+ 1;
486 if (*type_name
== '\0') {
487 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
492 provider_name
= strndup(field_name
, colon
- field_name
);
493 if (!provider_name
) {
494 PERROR("Failed to allocate field name string");
498 event_expr
= lttng_event_expr_app_specific_context_field_create(
499 provider_name
, type_name
);
501 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
502 provider_name
, type_name
);
509 ERR("%s: unexpected load expr type %d.", __func__
,
514 load_expr_op
= load_expr_op
->next
;
516 /* There may be a single array index after that. */
517 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
518 struct lttng_event_expr
*index_event_expr
;
519 const uint64_t index
= load_expr_op
->u
.index
;
521 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
522 if (!index_event_expr
) {
523 ERR("Failed to create array field element event expression.");
527 event_expr
= index_event_expr
;
528 load_expr_op
= load_expr_op
->next
;
531 switch (load_expr_op
->type
) {
532 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
534 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
535 * always found at the end of the chain.
538 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
539 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
544 ERR("%s: unexpected load expression operator %s.", __func__
,
545 ir_load_expression_type_str(load_expr_op
->type
));
552 lttng_event_expr_destroy(event_expr
);
562 struct lttng_event_expr
*ir_op_load_to_event_expr(
563 const struct ir_op
*ir
, const char *capture_str
)
565 struct lttng_event_expr
*event_expr
= NULL
;
567 assert(ir
->op
== IR_OP_LOAD
);
569 switch (ir
->data_type
) {
570 case IR_DATA_EXPRESSION
:
572 const struct ir_load_expression
*ir_load_expr
=
573 ir
->u
.load
.u
.expression
;
575 event_expr
= ir_op_load_expr_to_event_expr(
576 ir_load_expr
, capture_str
);
580 ERR("%s: unexpected data type: %s.", __func__
,
581 ir_data_type_str(ir
->data_type
));
589 const char *ir_operator_type_human_str(enum ir_op_type op
)
611 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
612 const char *capture_str
)
614 struct lttng_event_expr
*event_expr
= NULL
;
616 assert(ir
->op
== IR_OP_ROOT
);
617 ir
= ir
->u
.root
.child
;
621 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
626 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
628 ir_operator_type_human_str(ir
->op
));
631 ERR("%s: unexpected IR op type: %s.", __func__
,
632 ir_op_type_str(ir
->op
));
640 void destroy_event_expr(void *ptr
)
642 lttng_event_expr_destroy(ptr
);
645 struct parse_event_rule_res
{
647 struct lttng_event_rule
*er
;
649 /* Array of `struct lttng_event_expr *` */
650 struct lttng_dynamic_pointer_array capture_descriptors
;
654 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
656 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
657 enum lttng_event_rule_type event_rule_type
=
658 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
659 struct argpar_state
*state
;
660 struct argpar_item
*item
= NULL
;
662 int consumed_args
= -1;
663 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
664 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
665 struct parse_event_rule_res res
= { 0 };
666 struct lttng_event_expr
*event_expr
= NULL
;
667 struct filter_parser_ctx
*parser_ctx
= NULL
;
668 struct lttng_log_level_rule
*log_level_rule
= NULL
;
670 /* Event rule type option */
671 char *event_rule_type_str
= NULL
;
673 /* Tracepoint and syscall options. */
675 /* Array of strings. */
676 struct lttng_dynamic_pointer_array exclude_names
;
678 /* For userspace / kernel probe and function. */
679 char *location
= NULL
;
680 char *event_name
= NULL
;
686 char *log_level_str
= NULL
;
688 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
691 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
693 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
695 ERR("Failed to allocate an argpar state.");
700 enum argpar_state_parse_next_status status
;
702 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
703 status
= argpar_state_parse_next(state
, &item
, &error
);
704 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
707 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
708 /* Just stop parsing here. */
710 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
714 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
716 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
717 const struct argpar_item_opt
*item_opt
=
718 (const struct argpar_item_opt
*) item
;
720 switch (item_opt
->descr
->id
) {
723 if (!assign_domain_type(&domain_type
,
730 if (!assign_event_rule_type(&event_rule_type
,
735 /* Save the string for later use. */
736 if (!assign_string(&event_rule_type_str
,
744 if (!assign_string(&location
,
752 if (!assign_string(&event_name
,
754 "--event-name/-E")) {
760 if (!assign_string(&filter
, item_opt
->arg
,
767 if (!assign_string(&name
, item_opt
->arg
,
773 case OPT_EXCLUDE_NAME
:
777 ret
= lttng_dynamic_pointer_array_add_pointer(
779 strdup(item_opt
->arg
));
781 ERR("Failed to add pointer to dynamic pointer array.");
788 if (!assign_string(&log_level_str
,
789 item_opt
->arg
, "--log-level/-l")) {
797 const char *capture_str
= item_opt
->arg
;
799 ret
= filter_parser_ctx_create_from_filter_expression(
800 capture_str
, &parser_ctx
);
802 ERR("Failed to parse capture expression `%s`.",
807 event_expr
= ir_op_root_to_event_expr(
812 * ir_op_root_to_event_expr has printed
818 ret
= lttng_dynamic_pointer_array_add_pointer(
819 &res
.capture_descriptors
,
826 * The ownership of event expression was
827 * transferred to the dynamic array.
837 const struct argpar_item_non_opt
*item_non_opt
=
838 (const struct argpar_item_non_opt
*)
841 /* Don't accept non-option arguments. */
842 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
847 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
848 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
852 * Option --name is applicable to event rules of type tracepoint
853 * and syscall. For tracepoint and syscall rules, if --name is
854 * omitted, it is implicitly "*".
856 switch (event_rule_type
) {
857 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
858 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
866 ERR("Can't use --name with %s event rules.",
867 lttng_event_rule_type_str(
872 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
873 ERR("Can't use --exclude-name/-x with %s event rules.",
874 lttng_event_rule_type_str(
881 * Option --location is only applicable to (and mandatory for) event
882 * rules of type {k,u}probe and function.
884 * Option --event-name is only applicable to event rules of type probe.
885 * If omitted, it defaults to the location.
887 switch (event_rule_type
) {
888 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
889 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
890 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
892 ERR("Event rule of type %s requires a --location.",
893 lttng_event_rule_type_str(event_rule_type
));
898 event_name
= strdup(location
);
905 ERR("Can't use --location with %s event rules.",
906 lttng_event_rule_type_str(event_rule_type
));
911 ERR("Can't use --event-name with %s event rules.",
912 lttng_event_rule_type_str(
919 * Update *argc and *argv so our caller can keep parsing what follows.
921 consumed_args
= argpar_state_get_ingested_orig_args(state
);
922 assert(consumed_args
>= 0);
923 *argc
-= consumed_args
;
924 *argv
+= consumed_args
;
926 /* Need to specify a domain. */
927 if (domain_type
== LTTNG_DOMAIN_NONE
) {
928 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
932 /* Validate event rule type against domain. */
933 switch (event_rule_type
) {
934 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
935 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
936 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
937 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
938 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
939 ERR("Event type not available for user-space tracing.");
944 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
952 * Adding a filter to a probe, function or userspace-probe would be
953 * denied by the kernel tracer as it's not supported at the moment. We
954 * do an early check here to warn the user.
956 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
957 switch (event_rule_type
) {
958 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
959 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
962 ERR("Filter expressions are not supported for %s event rules.",
963 lttng_event_rule_type_str(event_rule_type
));
968 /* If --exclude-name/-x was passed, split it into an exclusion list. */
969 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
970 if (domain_type
!= LTTNG_DOMAIN_UST
) {
971 ERR("Event name exclusions are not yet implemented for %s event rules.",
972 get_domain_str(domain_type
));
976 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
978 * Assume validate_exclusion_list already prints an
986 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
987 ERR("Log levels are only applicable to tracepoint event rules.");
991 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
992 ERR("Log levels are not supported by the kernel tracer.");
997 /* Finally, create the event rule object. */
998 switch (event_rule_type
) {
999 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
1001 enum lttng_event_rule_status event_rule_status
;
1003 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
1005 ERR("Failed to create tracepoint event rule.");
1010 event_rule_status
= lttng_event_rule_tracepoint_set_name_pattern(
1012 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1013 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
1020 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1022 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1023 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1029 /* Set exclusion list. */
1030 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1032 int count
= lttng_dynamic_pointer_array_get_count(
1035 for (n
= 0; n
< count
; n
++) {
1036 const char *exclude_name
=
1037 lttng_dynamic_pointer_array_get_pointer(
1042 lttng_event_rule_tracepoint_add_name_pattern_exclusion(
1045 if (event_rule_status
!=
1046 LTTNG_EVENT_RULE_STATUS_OK
) {
1047 ERR("Failed to set tracepoint exclusion list element '%s'",
1055 * ".." is the same as passing no log level option and
1056 * correspond the the "ANY" case.
1058 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
1060 bool log_level_only
;
1062 if (!parse_log_level_string(log_level_str
, domain_type
,
1063 &log_level
, &log_level_only
)) {
1064 ERR("Failed to parse log level string `%s`.",
1069 if (log_level_only
) {
1070 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
1072 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1075 if (log_level_rule
== NULL
) {
1076 ERR("Failed to create log level rule object.");
1081 lttng_event_rule_tracepoint_set_log_level_rule(
1082 res
.er
, log_level_rule
);
1084 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1085 ERR("Failed to set log level on event fule.");
1092 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1095 enum lttng_event_rule_status event_rule_status
;
1097 ret
= parse_kernel_probe_opts(
1098 location
, &kernel_probe_location
);
1100 ERR("Failed to parse kernel probe location.");
1104 assert(kernel_probe_location
);
1105 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1107 ERR("Failed to create kprobe event rule.");
1112 lttng_event_rule_kernel_probe_set_event_name(
1113 res
.er
, event_name
);
1114 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1115 ERR("Failed to set kprobe event rule's name to '%s'.",
1122 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1125 enum lttng_event_rule_status event_rule_status
;
1127 ret
= parse_userspace_probe_opts(
1128 location
, &userspace_probe_location
);
1130 ERR("Failed to parse user space probe location.");
1134 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1136 ERR("Failed to create userspace probe event rule.");
1141 lttng_event_rule_kernel_uprobe_set_event_name(
1142 res
.er
, event_name
);
1143 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1144 ERR("Failed to set user space probe event rule's name to '%s'.",
1151 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1153 enum lttng_event_rule_status event_rule_status
;
1154 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1156 if (!parse_syscall_emission_site_from_type(
1157 event_rule_type_str
, &emission_site
)) {
1158 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1162 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1164 ERR("Failed to create syscall event rule.");
1168 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1170 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1171 ERR("Failed to set syscall event rule's pattern to '%s'.",
1177 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1179 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1180 ERR("Failed to set syscall event rule's filter to '%s'.",
1196 lttng_event_rule_destroy(res
.er
);
1198 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1202 filter_parser_ctx_free(parser_ctx
);
1205 lttng_event_expr_destroy(event_expr
);
1206 argpar_item_destroy(item
);
1208 argpar_state_destroy(state
);
1211 lttng_dynamic_pointer_array_reset(&exclude_names
);
1212 free(log_level_str
);
1215 free(event_rule_type_str
);
1217 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1218 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1219 lttng_log_level_rule_destroy(log_level_rule
);
1224 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1226 struct parse_event_rule_res res
;
1227 struct lttng_condition
*c
;
1230 res
= parse_event_rule(argc
, argv
);
1236 c
= lttng_condition_event_rule_matches_create(res
.er
);
1237 lttng_event_rule_destroy(res
.er
);
1243 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1245 enum lttng_condition_status status
;
1246 struct lttng_event_expr
**expr
=
1247 lttng_dynamic_array_get_element(
1248 &res
.capture_descriptors
.array
, i
);
1252 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1254 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1255 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1256 ERR("The capture feature is unsupported by the event-rule condition type");
1262 /* Ownership of event expression moved to `c` */
1269 lttng_condition_destroy(c
);
1273 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1274 lttng_event_rule_destroy(res
.er
);
1278 struct condition_descr
{
1280 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1284 struct condition_descr condition_descrs
[] = {
1285 { "event-rule-matches", handle_condition_event
},
1289 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1293 struct lttng_condition
*cond
;
1294 const struct condition_descr
*descr
= NULL
;
1296 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1297 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1298 descr
= &condition_descrs
[i
];
1304 ERR("Unknown condition name '%s'", condition_name
);
1308 cond
= descr
->handler(argc
, argv
);
1310 /* The handler has already printed an error message. */
1321 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1324 size_t num_token
= 0;
1325 struct lttng_dynamic_pointer_array tokens
;
1326 struct lttng_rate_policy
*policy
= NULL
;
1327 enum lttng_rate_policy_type policy_type
;
1328 unsigned long long value
;
1329 char *policy_type_str
;
1330 char *policy_value_str
;
1333 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1335 /* Rate policy fields are separated by ':'. */
1336 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1338 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1342 * Early sanity check that the number of parameter is exactly 2.
1345 if (num_token
!= 2) {
1346 ERR("Rate policy format is invalid.");
1350 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1351 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1353 /* Parse the type. */
1354 if (strcmp(policy_type_str
, "once-after") == 0) {
1355 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1356 } else if (strcmp(policy_type_str
, "every") == 0) {
1357 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1359 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1363 /* Parse the value. */
1364 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1365 ERR("Failed to parse rate policy value `%s` as an integer.",
1371 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1375 switch (policy_type
) {
1376 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1377 policy
= lttng_rate_policy_every_n_create(value
);
1379 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1380 policy
= lttng_rate_policy_once_after_n_create(value
);
1386 if (policy
== NULL
) {
1387 ERR("Failed to create rate policy `%s`.", policy_str
);
1391 lttng_dynamic_pointer_array_reset(&tokens
);
1395 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1396 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1397 ARGPAR_OPT_DESCR_SENTINEL
1401 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1403 struct lttng_action
*action
= NULL
;
1404 struct argpar_state
*state
= NULL
;
1405 struct argpar_item
*item
= NULL
;
1407 struct lttng_rate_policy
*policy
= NULL
;
1409 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1411 ERR("Failed to allocate an argpar state.");
1416 enum argpar_state_parse_next_status status
;
1418 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1419 status
= argpar_state_parse_next(state
, &item
, &error
);
1420 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1423 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1424 /* Just stop parsing here. */
1426 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1430 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1432 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1433 const struct argpar_item_opt
*item_opt
=
1434 (const struct argpar_item_opt
*) item
;
1436 switch (item_opt
->descr
->id
) {
1437 case OPT_RATE_POLICY
:
1439 policy
= parse_rate_policy(item_opt
->arg
);
1449 const struct argpar_item_non_opt
*item_non_opt
;
1451 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1453 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1455 switch (item_non_opt
->non_opt_index
) {
1457 ERR("Unexpected argument `%s`.",
1464 *argc
-= argpar_state_get_ingested_orig_args(state
);
1465 *argv
+= argpar_state_get_ingested_orig_args(state
);
1467 action
= lttng_action_notify_create();
1469 ERR("Failed to create notify action");
1474 enum lttng_action_status status
;
1475 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1476 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1477 ERR("Failed to set rate policy");
1485 lttng_action_destroy(action
);
1489 lttng_rate_policy_destroy(policy
);
1490 argpar_state_destroy(state
);
1491 argpar_item_destroy(item
);
1496 * Generic handler for a kind of action that takes a session name and an
1497 * optional rate policy.
1500 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1502 struct lttng_action
*(*create_action_cb
)(void),
1503 enum lttng_action_status (*set_session_name_cb
)(
1504 struct lttng_action
*, const char *),
1505 enum lttng_action_status (*set_rate_policy_cb
)(
1506 struct lttng_action
*,
1507 const struct lttng_rate_policy
*),
1508 const char *action_name
)
1510 struct lttng_action
*action
= NULL
;
1511 struct argpar_state
*state
= NULL
;
1512 struct argpar_item
*item
= NULL
;
1513 const char *session_name_arg
= NULL
;
1515 enum lttng_action_status action_status
;
1516 struct lttng_rate_policy
*policy
= NULL
;
1518 assert(set_session_name_cb
);
1519 assert(set_rate_policy_cb
);
1521 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1522 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1523 ARGPAR_OPT_DESCR_SENTINEL
1526 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1528 ERR("Failed to allocate an argpar state.");
1533 enum argpar_state_parse_next_status status
;
1535 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1536 status
= argpar_state_parse_next(state
, &item
, &error
);
1537 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1540 } else if (status
==
1541 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1542 /* Just stop parsing here. */
1544 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1548 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1549 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1550 const struct argpar_item_opt
*item_opt
=
1551 (const struct argpar_item_opt
*) item
;
1553 switch (item_opt
->descr
->id
) {
1554 case OPT_RATE_POLICY
:
1556 policy
= parse_rate_policy(item_opt
->arg
);
1566 const struct argpar_item_non_opt
*item_non_opt
;
1567 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1569 switch (item_non_opt
->non_opt_index
) {
1571 session_name_arg
= item_non_opt
->arg
;
1574 ERR("Unexpected argument `%s`.",
1581 *argc
-= argpar_state_get_ingested_orig_args(state
);
1582 *argv
+= argpar_state_get_ingested_orig_args(state
);
1584 if (!session_name_arg
) {
1585 ERR("Missing session name.");
1589 action
= create_action_cb();
1591 ERR("Failed to allocate %s session action.", action_name
);
1595 action_status
= set_session_name_cb(action
, session_name_arg
);
1596 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1597 ERR("Failed to set action %s session's session name to '%s'.",
1598 action_name
, session_name_arg
);
1603 action_status
= set_rate_policy_cb(action
, policy
);
1604 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1605 ERR("Failed to set rate policy");
1613 lttng_action_destroy(action
);
1615 argpar_item_destroy(item
);
1617 lttng_rate_policy_destroy(policy
);
1619 argpar_state_destroy(state
);
1624 struct lttng_action
*handle_action_start_session(int *argc
,
1627 return handle_action_simple_session_with_policy(argc
, argv
,
1628 lttng_action_start_session_create
,
1629 lttng_action_start_session_set_session_name
,
1630 lttng_action_start_session_set_rate_policy
, "start");
1634 struct lttng_action
*handle_action_stop_session(int *argc
,
1637 return handle_action_simple_session_with_policy(argc
, argv
,
1638 lttng_action_stop_session_create
,
1639 lttng_action_stop_session_set_session_name
,
1640 lttng_action_stop_session_set_rate_policy
, "stop");
1644 struct lttng_action
*handle_action_rotate_session(int *argc
,
1647 return handle_action_simple_session_with_policy(argc
, argv
,
1648 lttng_action_rotate_session_create
,
1649 lttng_action_rotate_session_set_session_name
,
1650 lttng_action_rotate_session_set_rate_policy
,
1654 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1655 { OPT_NAME
, 'n', "name", true },
1656 { OPT_MAX_SIZE
, 'm', "max-size", true },
1657 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1658 { OPT_DATA_URL
, '\0', "data-url", true },
1659 { OPT_URL
, '\0', "url", true },
1660 { OPT_PATH
, '\0', "path", true },
1661 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1662 ARGPAR_OPT_DESCR_SENTINEL
1666 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1669 struct lttng_action
*action
= NULL
;
1670 struct argpar_state
*state
= NULL
;
1671 struct argpar_item
*item
= NULL
;
1672 const char *session_name_arg
= NULL
;
1673 char *snapshot_name_arg
= NULL
;
1674 char *ctrl_url_arg
= NULL
;
1675 char *data_url_arg
= NULL
;
1676 char *max_size_arg
= NULL
;
1677 char *url_arg
= NULL
;
1678 char *path_arg
= NULL
;
1680 enum lttng_action_status action_status
;
1681 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1682 struct lttng_rate_policy
*policy
= NULL
;
1684 unsigned int locations_specified
= 0;
1686 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1688 ERR("Failed to allocate an argpar state.");
1693 enum argpar_state_parse_next_status status
;
1695 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1696 status
= argpar_state_parse_next(state
, &item
, &error
);
1697 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1700 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1701 /* Just stop parsing here. */
1703 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1707 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1709 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1710 const struct argpar_item_opt
*item_opt
=
1711 (const struct argpar_item_opt
*) item
;
1713 switch (item_opt
->descr
->id
) {
1715 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1721 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1727 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1733 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1739 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1745 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1750 case OPT_RATE_POLICY
:
1752 policy
= parse_rate_policy(item_opt
->arg
);
1762 const struct argpar_item_non_opt
*item_non_opt
;
1764 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1766 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1768 switch (item_non_opt
->non_opt_index
) {
1770 session_name_arg
= item_non_opt
->arg
;
1773 ERR("Unexpected argument `%s`.",
1780 *argc
-= argpar_state_get_ingested_orig_args(state
);
1781 *argv
+= argpar_state_get_ingested_orig_args(state
);
1783 if (!session_name_arg
) {
1784 ERR("Missing session name.");
1788 /* --ctrl-url and --data-url must come in pair. */
1789 if (ctrl_url_arg
&& !data_url_arg
) {
1790 ERR("--ctrl-url is specified, but --data-url is missing.");
1794 if (!ctrl_url_arg
&& data_url_arg
) {
1795 ERR("--data-url is specified, but --ctrl-url is missing.");
1799 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1800 locations_specified
+= !!url_arg
;
1801 locations_specified
+= !!path_arg
;
1803 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1804 if (locations_specified
> 1) {
1805 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1810 * Did the user specify an option that implies using a
1811 * custom/unregistered output?
1813 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1814 snapshot_output
= lttng_snapshot_output_create();
1815 if (!snapshot_output
) {
1816 ERR("Failed to allocate a snapshot output.");
1821 action
= lttng_action_snapshot_session_create();
1823 ERR("Failed to allocate snapshot session action.");
1827 action_status
= lttng_action_snapshot_session_set_session_name(
1828 action
, session_name_arg
);
1829 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1830 ERR("Failed to set action snapshot session's session name to '%s'.",
1835 if (snapshot_name_arg
) {
1836 if (!snapshot_output
) {
1837 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1841 ret
= lttng_snapshot_output_set_name(
1842 snapshot_name_arg
, snapshot_output
);
1844 ERR("Failed to set name of snapshot output.");
1852 if (!snapshot_output
) {
1853 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1857 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1859 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1863 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1865 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1873 struct lttng_uri
*uris
;
1875 if (!strstr(url_arg
, "://")) {
1876 ERR("Failed to parse '%s' as an URL.", url_arg
);
1880 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1882 ERR("Failed to parse '%s' as an URL.", url_arg
);
1886 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1887 ret
= lttng_snapshot_output_set_local_path(
1888 uris
[0].dst
.path
, snapshot_output
);
1891 ERR("Failed to assign '%s' as a local destination.",
1896 ret
= lttng_snapshot_output_set_network_url(
1897 url_arg
, snapshot_output
);
1900 ERR("Failed to assign '%s' as a network URL.",
1908 ret
= lttng_snapshot_output_set_local_path(
1909 path_arg
, snapshot_output
);
1911 ERR("Failed to parse '%s' as a local path.", path_arg
);
1918 * Two argument form, network output with separate control and
1921 ret
= lttng_snapshot_output_set_network_urls(
1922 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1924 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1925 ctrl_url_arg
, data_url_arg
);
1930 if (snapshot_output
) {
1931 action_status
= lttng_action_snapshot_session_set_output(
1932 action
, snapshot_output
);
1933 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1934 ERR("Failed to set snapshot session action's output.");
1938 /* Ownership of `snapshot_output` has been transferred to the action. */
1939 snapshot_output
= NULL
;
1943 enum lttng_action_status status
;
1944 status
= lttng_action_snapshot_session_set_rate_policy(
1946 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1947 ERR("Failed to set rate policy");
1955 lttng_action_destroy(action
);
1959 free(snapshot_name_arg
);
1964 free(snapshot_output
);
1966 lttng_rate_policy_destroy(policy
);
1967 argpar_state_destroy(state
);
1968 argpar_item_destroy(item
);
1972 struct action_descr
{
1974 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1978 struct action_descr action_descrs
[] = {
1979 { "notify", handle_action_notify
},
1980 { "start-session", handle_action_start_session
},
1981 { "stop-session", handle_action_stop_session
},
1982 { "rotate-session", handle_action_rotate_session
},
1983 { "snapshot-session", handle_action_snapshot_session
},
1987 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1990 struct lttng_action
*action
;
1991 const struct action_descr
*descr
= NULL
;
1993 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1994 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1995 descr
= &action_descrs
[i
];
2001 ERR("Unknown action name: %s", action_name
);
2005 action
= descr
->handler(argc
, argv
);
2007 /* The handler has already printed an error message. */
2019 struct argpar_opt_descr add_trigger_options
[] = {
2020 { OPT_HELP
, 'h', "help", false },
2021 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2022 { OPT_CONDITION
, '\0', "condition", true },
2023 { OPT_ACTION
, '\0', "action", true },
2024 { OPT_NAME
, '\0', "name", true },
2025 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2026 ARGPAR_OPT_DESCR_SENTINEL
,
2030 void lttng_actions_destructor(void *p
)
2032 struct lttng_action
*action
= p
;
2034 lttng_action_destroy(action
);
2037 int cmd_add_trigger(int argc
, const char **argv
)
2040 int my_argc
= argc
- 1;
2041 const char **my_argv
= argv
+ 1;
2042 struct lttng_condition
*condition
= NULL
;
2043 struct lttng_dynamic_pointer_array actions
;
2044 struct argpar_state
*argpar_state
= NULL
;
2045 struct argpar_item
*argpar_item
= NULL
;
2046 struct lttng_action
*action_list
= NULL
;
2047 struct lttng_action
*action
= NULL
;
2048 struct lttng_trigger
*trigger
= NULL
;
2052 char *owner_uid
= NULL
;
2053 enum lttng_error_code ret_code
;
2055 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2058 enum argpar_state_parse_next_status status
;
2059 const struct argpar_item_opt
*item_opt
;
2062 argpar_state_destroy(argpar_state
);
2063 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2064 add_trigger_options
);
2065 if (!argpar_state
) {
2066 ERR("Failed to create argpar state.");
2070 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2071 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2072 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2075 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2078 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2082 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2084 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2085 const struct argpar_item_non_opt
*item_non_opt
=
2086 (const struct argpar_item_non_opt
*)
2089 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2093 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2095 ingested_args
= argpar_state_get_ingested_orig_args(
2098 my_argc
-= ingested_args
;
2099 my_argv
+= ingested_args
;
2101 switch (item_opt
->descr
->id
) {
2106 case OPT_LIST_OPTIONS
:
2107 list_cmd_options_argpar(stdout
, add_trigger_options
);
2113 ERR("A --condition was already given.");
2117 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2120 * An error message was already printed by
2130 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2133 * An error message was already printed by
2139 ret
= lttng_dynamic_pointer_array_add_pointer(
2142 ERR("Failed to add pointer to pointer array.");
2146 /* Ownership of the action was transferred to the group. */
2153 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2161 if (!assign_string(&owner_uid
, item_opt
->arg
,
2174 ERR("Missing --condition.");
2178 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2179 ERR("Need at least one --action.");
2183 action_list
= lttng_action_list_create();
2188 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2189 enum lttng_action_status status
;
2191 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2193 status
= lttng_action_list_add_action(action_list
, action
);
2194 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2199 * The `lttng_action_list_add_action()` takes a reference to
2200 * the action. We can destroy ours.
2202 lttng_action_destroy(action
);
2206 trigger
= lttng_trigger_create(condition
, action_list
);
2212 enum lttng_trigger_status trigger_status
;
2217 uid
= strtol(owner_uid
, &end
, 10);
2218 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2219 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2223 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2224 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2225 ERR("Failed to set trigger's user identity.");
2231 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2233 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2236 if (ret_code
!= LTTNG_OK
) {
2237 ERR("Failed to register trigger: %s.",
2238 lttng_strerror(-ret_code
));
2242 MSG("Trigger registered successfully.");
2251 argpar_state_destroy(argpar_state
);
2252 argpar_item_destroy(argpar_item
);
2253 lttng_dynamic_pointer_array_reset(&actions
);
2254 lttng_condition_destroy(condition
);
2255 lttng_action_destroy(action_list
);
2256 lttng_action_destroy(action
);
2257 lttng_trigger_destroy(trigger
);