2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "../command.h"
12 #include "../loglevel.h"
13 #include "../uprobe.h"
15 #include "common/argpar/argpar.h"
16 #include "common/dynamic-array.h"
17 #include "common/string-utils/string-utils.h"
18 #include "common/utils.h"
19 /* For lttng_event_rule_type_str(). */
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/lttng.h>
22 #include "common/filter/filter-ast.h"
23 #include "common/filter/filter-ir.h"
24 #include "common/dynamic-array.h"
26 #if (LTTNG_SYMBOL_NAME_LEN == 256)
27 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg
[] =
32 #include <lttng-add-trigger.1.h>
66 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
67 { OPT_FILTER
, 'f', "filter", true },
68 { OPT_NAME
, 'n', "name", true },
69 { OPT_EXCLUDE_NAMES
, 'x', "exclude-names", true },
70 { OPT_LOGLEVEL
, '\0', "loglevel", true },
71 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
72 { OPT_EVENT_NAME
, 'E', "event-name", true },
74 { OPT_DOMAIN
, 'd', "domain", true },
75 { OPT_TYPE
, 't', "type", true },
76 { OPT_LOCATION
, 'L', "location", true },
78 /* Capture descriptor */
79 { OPT_CAPTURE
, '\0', "capture", true },
81 ARGPAR_OPT_DESCR_SENTINEL
85 bool assign_domain_type(enum lttng_domain_type
*dest
, const char *arg
)
89 if (*dest
!= LTTNG_DOMAIN_NONE
) {
90 ERR("More than one `--domain` was specified.");
94 if (strcmp(arg
, "kernel") == 0) {
95 *dest
= LTTNG_DOMAIN_KERNEL
;
96 } else if (strcmp(arg
, "user") == 0 || strcmp(arg
, "userspace") == 0) {
97 *dest
= LTTNG_DOMAIN_UST
;
98 } else if (strcmp(arg
, "jul") == 0) {
99 *dest
= LTTNG_DOMAIN_JUL
;
100 } else if (strcmp(arg
, "log4j") == 0) {
101 *dest
= LTTNG_DOMAIN_LOG4J
;
102 } else if (strcmp(arg
, "python") == 0) {
103 *dest
= LTTNG_DOMAIN_PYTHON
;
105 ERR("Invalid `--domain` value: %s", arg
);
120 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
124 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
125 ERR("More than one `--type` was specified.");
129 if (strcmp(arg
, "tracepoint") == 0 || strcmp(arg
, "logging") == 0) {
130 *dest
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
131 } else if (strcmp (arg
, "kprobe") == 0 || strcmp(arg
, "kernel-probe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
;
133 } else if (strcmp (arg
, "uprobe") == 0 || strcmp(arg
, "userspace-probe") == 0) {
134 *dest
= LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
;
135 } else if (strcmp (arg
, "function") == 0) {
136 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
;
137 } else if (strcmp (arg
, "syscall") == 0) {
138 *dest
= LTTNG_EVENT_RULE_TYPE_SYSCALL
;
140 ERR("Invalid `--type` value: %s", arg
);
155 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
160 ERR("Duplicate '%s' given.", opt_name
);
166 PERROR("Failed to allocate string '%s'.", opt_name
);
180 /* This is defined in enable_events.c. */
182 int create_exclusion_list_and_validate(const char *event_name
,
183 const char *exclusions_arg
,
184 char ***exclusion_list
);
187 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
188 * is not recognized as a valid log level.
191 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
193 switch (domain_type
) {
194 case LTTNG_DOMAIN_UST
:
196 enum lttng_loglevel loglevel
;
197 const int ret
= loglevel_name_to_value(str
, &loglevel
);
199 return ret
== -1 ? ret
: (int) loglevel
;
201 case LTTNG_DOMAIN_LOG4J
:
203 enum lttng_loglevel_log4j loglevel
;
204 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
206 return ret
== -1 ? ret
: (int) loglevel
;
208 case LTTNG_DOMAIN_JUL
:
210 enum lttng_loglevel_jul loglevel
;
211 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
213 return ret
== -1 ? ret
: (int) loglevel
;
215 case LTTNG_DOMAIN_PYTHON
:
217 enum lttng_loglevel_python loglevel
;
218 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
220 return ret
== -1 ? ret
: (int) loglevel
;
223 /* Invalid domain type. */
228 static int parse_kernel_probe_opts(const char *source
,
229 struct lttng_kernel_probe_location
**location
)
234 char name
[LTTNG_SYMBOL_NAME_LEN
];
235 char *symbol_name
= NULL
;
238 /* Check for symbol+offset. */
239 match
= sscanf(source
,
240 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
244 if (*s_hex
== '\0') {
245 ERR("Kernel probe symbol offset is missing.");
249 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
251 PERROR("Failed to copy kernel probe location symbol name.");
254 offset
= strtoul(s_hex
, NULL
, 0);
256 *location
= lttng_kernel_probe_location_symbol_create(
257 symbol_name
, offset
);
259 ERR("Failed to create symbol kernel probe location.");
266 /* Check for symbol. */
267 if (isalpha(name
[0]) || name
[0] == '_') {
268 match
= sscanf(source
,
269 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
273 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
275 ERR("Failed to copy kernel probe location symbol name.");
279 *location
= lttng_kernel_probe_location_symbol_create(
282 ERR("Failed to create symbol kernel probe location.");
290 /* Check for address. */
291 match
= sscanf(source
, "%18s", s_hex
);
295 if (*s_hex
== '\0') {
296 ERR("Invalid kernel probe location address.");
300 address
= strtoul(s_hex
, NULL
, 0);
301 *location
= lttng_kernel_probe_location_address_create(address
);
303 ERR("Failed to create symbol kernel probe location.");
321 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
322 const struct ir_load_expression
*load_expr
,
323 const char *capture_str
)
325 char *provider_name
= NULL
;
326 struct lttng_event_expr
*event_expr
= NULL
;
327 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
328 const enum ir_load_expression_type load_expr_child_type
=
331 switch (load_expr_child_type
) {
332 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
333 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
335 const char *field_name
;
337 load_expr_op
= load_expr_op
->next
;
338 assert(load_expr_op
);
339 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
340 field_name
= load_expr_op
->u
.symbol
;
343 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
344 lttng_event_expr_event_payload_field_create(field_name
) :
345 lttng_event_expr_channel_context_field_create(field_name
);
347 ERR("Failed to create %s event expression: field name = `%s`.",
348 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
349 "payload field" : "channel context",
356 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
359 const char *type_name
;
360 const char *field_name
;
362 load_expr_op
= load_expr_op
->next
;
363 assert(load_expr_op
);
364 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
365 field_name
= load_expr_op
->u
.symbol
;
369 * The field name needs to be of the form PROVIDER:TYPE. We
372 colon
= strchr(field_name
, ':');
374 ERR("Invalid app-specific context field name: missing colon in `%s`.",
379 type_name
= colon
+ 1;
380 if (*type_name
== '\0') {
381 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
386 provider_name
= strndup(field_name
, colon
- field_name
);
387 if (!provider_name
) {
388 PERROR("Failed to allocate field name string");
392 event_expr
= lttng_event_expr_app_specific_context_field_create(
393 provider_name
, type_name
);
395 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
396 provider_name
, type_name
);
403 ERR("%s: unexpected load expr type %d.", __func__
,
408 load_expr_op
= load_expr_op
->next
;
410 /* There may be a single array index after that. */
411 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
412 struct lttng_event_expr
*index_event_expr
;
413 const uint64_t index
= load_expr_op
->u
.index
;
415 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
416 if (!index_event_expr
) {
417 ERR("Failed to create array field element event expression.");
421 event_expr
= index_event_expr
;
422 load_expr_op
= load_expr_op
->next
;
425 switch (load_expr_op
->type
) {
426 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
428 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
429 * always found at the end of the chain.
432 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
433 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
438 ERR("%s: unexpected load expression operator %s.", __func__
,
439 ir_load_expression_type_str(load_expr_op
->type
));
446 lttng_event_expr_destroy(event_expr
);
456 struct lttng_event_expr
*ir_op_load_to_event_expr(
457 const struct ir_op
*ir
, const char *capture_str
)
459 struct lttng_event_expr
*event_expr
= NULL
;
461 assert(ir
->op
== IR_OP_LOAD
);
463 switch (ir
->data_type
) {
464 case IR_DATA_EXPRESSION
:
466 const struct ir_load_expression
*ir_load_expr
=
467 ir
->u
.load
.u
.expression
;
469 event_expr
= ir_op_load_expr_to_event_expr(
470 ir_load_expr
, capture_str
);
474 ERR("%s: unexpected data type: %s.", __func__
,
475 ir_data_type_str(ir
->data_type
));
483 const char *ir_operator_type_human_str(enum ir_op_type op
)
505 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
506 const char *capture_str
)
508 struct lttng_event_expr
*event_expr
= NULL
;
510 assert(ir
->op
== IR_OP_ROOT
);
511 ir
= ir
->u
.root
.child
;
515 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
520 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
522 ir_operator_type_human_str(ir
->op
));
525 ERR("%s: unexpected IR op type: %s.", __func__
,
526 ir_op_type_str(ir
->op
));
534 void destroy_event_expr(void *ptr
)
536 lttng_event_expr_destroy(ptr
);
539 struct parse_event_rule_res
{
541 struct lttng_event_rule
*er
;
543 /* Array of `struct lttng_event_expr *` */
544 struct lttng_dynamic_pointer_array capture_descriptors
;
548 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
550 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
551 enum lttng_event_rule_type event_rule_type
=
552 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
553 struct argpar_state
*state
;
554 struct argpar_item
*item
= NULL
;
556 int consumed_args
= -1;
557 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
558 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
559 struct parse_event_rule_res res
= { 0 };
560 struct lttng_event_expr
*event_expr
= NULL
;
561 struct filter_parser_ctx
*parser_ctx
= NULL
;
562 struct lttng_log_level_rule
*log_level_rule
= NULL
;
564 /* Tracepoint and syscall options. */
566 char *exclude_names
= NULL
;
567 char **exclusion_list
= NULL
;
569 /* For userspace / kernel probe and function. */
570 char *location
= NULL
;
571 char *event_name
= NULL
;
577 char *loglevel_str
= NULL
;
578 bool loglevel_only
= false;
580 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
582 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
584 ERR("Failed to allocate an argpar state.");
589 enum argpar_state_parse_next_status status
;
591 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
592 status
= argpar_state_parse_next(state
, &item
, &error
);
593 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
596 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
597 /* Just stop parsing here. */
599 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
603 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
605 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
606 const struct argpar_item_opt
*item_opt
=
607 (const struct argpar_item_opt
*) item
;
609 switch (item_opt
->descr
->id
) {
612 if (!assign_domain_type(&domain_type
,
619 if (!assign_event_rule_type(&event_rule_type
,
626 if (!assign_string(&location
,
634 if (!assign_string(&event_name
,
636 "--event-name/-E")) {
642 if (!assign_string(&filter
, item_opt
->arg
,
649 if (!assign_string(&name
, item_opt
->arg
,
655 case OPT_EXCLUDE_NAMES
:
656 if (!assign_string(&exclude_names
,
658 "--exclude-names/-x")) {
664 case OPT_LOGLEVEL_ONLY
:
665 if (!assign_string(&loglevel_str
, item_opt
->arg
,
666 "--loglevel/--loglevel-only")) {
670 loglevel_only
= item_opt
->descr
->id
==
676 const char *capture_str
= item_opt
->arg
;
678 ret
= filter_parser_ctx_create_from_filter_expression(
679 capture_str
, &parser_ctx
);
681 ERR("Failed to parse capture expression `%s`.",
686 event_expr
= ir_op_root_to_event_expr(
691 * ir_op_root_to_event_expr has printed
697 ret
= lttng_dynamic_pointer_array_add_pointer(
698 &res
.capture_descriptors
,
705 * The ownership of event expression was
706 * transferred to the dynamic array.
716 const struct argpar_item_non_opt
*item_non_opt
=
717 (const struct argpar_item_non_opt
*)
720 /* Don't accept non-option arguments. */
721 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
726 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
727 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
731 * Option --name is applicable to event rules of type tracepoint
732 * and syscall. For tracepoint and syscall rules, if --name is
733 * omitted, it is implicitly "*".
735 switch (event_rule_type
) {
736 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
737 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
745 ERR("Can't use --name with %s event rules.",
746 lttng_event_rule_type_str(
752 ERR("Can't use --exclude-names/-x with %s event rules.",
753 lttng_event_rule_type_str(
760 * Option --location is only applicable to (and mandatory for) event
761 * rules of type {k,u}probe and function.
763 * Option --event-name is only applicable to event rules of type probe.
764 * If omitted, it defaults to the location.
766 switch (event_rule_type
) {
767 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
768 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
769 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
771 ERR("Event rule of type %s requires a --location.",
772 lttng_event_rule_type_str(event_rule_type
));
777 event_name
= strdup(location
);
784 ERR("Can't use --location with %s event rules.",
785 lttng_event_rule_type_str(event_rule_type
));
790 ERR("Can't use --event-name with %s event rules.",
791 lttng_event_rule_type_str(
798 * Update *argc and *argv so our caller can keep parsing what follows.
800 consumed_args
= argpar_state_get_ingested_orig_args(state
);
801 assert(consumed_args
>= 0);
802 *argc
-= consumed_args
;
803 *argv
+= consumed_args
;
805 /* Need to specify a domain. */
806 if (domain_type
== LTTNG_DOMAIN_NONE
) {
807 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
811 /* Validate event rule type against domain. */
812 switch (event_rule_type
) {
813 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
814 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
815 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
816 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
817 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
818 ERR("Event type not available for user-space tracing.");
823 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
831 * Adding a filter to a probe, function or userspace-probe would be
832 * denied by the kernel tracer as it's not supported at the moment. We
833 * do an early check here to warn the user.
835 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
836 switch (event_rule_type
) {
837 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
838 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
841 ERR("Filter expressions are not supported for %s event rules.",
842 lttng_event_rule_type_str(event_rule_type
));
847 /* If --exclude/-x was passed, split it into an exclusion list. */
849 if (domain_type
!= LTTNG_DOMAIN_UST
) {
850 ERR("Event name exclusions are not yet implemented for %s event rules.",
851 get_domain_str(domain_type
));
855 if (create_exclusion_list_and_validate(name
,
856 exclude_names
, &exclusion_list
) != 0) {
857 ERR("Failed to create exclusion list.");
862 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
863 ERR("Log levels are only applicable to tracepoint event rules.");
867 /* Finally, create the event rule object. */
868 switch (event_rule_type
) {
869 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
871 enum lttng_event_rule_status event_rule_status
;
873 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
875 ERR("Failed to create tracepoint event rule.");
880 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
882 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
883 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
890 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
892 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
893 ERR("Failed to set tracepoint event rule's filter to '%s'.",
899 /* Set exclusion list. */
900 if (exclusion_list
) {
903 for (n
= 0; exclusion_list
[n
]; n
++) {
904 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
907 if (event_rule_status
!=
908 LTTNG_EVENT_RULE_STATUS_OK
) {
909 ERR("Failed to set tracepoint exclusion list element '%s'",
919 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
920 ERR("Log levels are not supported by the kernel tracer.");
924 loglevel
= parse_loglevel_string(
925 loglevel_str
, domain_type
);
927 ERR("Failed to parse `%s` as a log level.",
933 log_level_rule
= lttng_log_level_rule_exactly_create(loglevel
);
935 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(loglevel
);
938 if (log_level_rule
== NULL
) {
939 ERR("Failed to create log level rule object.");
944 lttng_event_rule_tracepoint_set_log_level_rule(
945 res
.er
, log_level_rule
);
947 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
948 ERR("Failed to set log level on event fule.");
955 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
958 enum lttng_event_rule_status event_rule_status
;
960 ret
= parse_kernel_probe_opts(
961 location
, &kernel_probe_location
);
963 ERR("Failed to parse kernel probe location.");
967 assert(kernel_probe_location
);
968 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
970 ERR("Failed to create kprobe event rule.");
975 lttng_event_rule_kernel_probe_set_event_name(
977 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
978 ERR("Failed to set kprobe event rule's name to '%s'.",
985 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
988 enum lttng_event_rule_status event_rule_status
;
990 ret
= parse_userspace_probe_opts(
991 location
, &userspace_probe_location
);
993 ERR("Failed to parse user space probe location.");
997 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
999 ERR("Failed to create userspace probe event rule.");
1004 lttng_event_rule_userspace_probe_set_event_name(
1005 res
.er
, event_name
);
1006 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1007 ERR("Failed to set user space probe event rule's name to '%s'.",
1014 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1016 enum lttng_event_rule_status event_rule_status
;
1018 res
.er
= lttng_event_rule_syscall_create();
1020 ERR("Failed to create syscall event rule.");
1024 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1026 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1027 ERR("Failed to set syscall event rule's pattern to '%s'.",
1033 event_rule_status
= lttng_event_rule_syscall_set_filter(
1035 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1036 ERR("Failed to set syscall event rule's filter to '%s'.",
1052 lttng_event_rule_destroy(res
.er
);
1054 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1058 filter_parser_ctx_free(parser_ctx
);
1061 lttng_event_expr_destroy(event_expr
);
1062 argpar_item_destroy(item
);
1064 argpar_state_destroy(state
);
1067 free(exclude_names
);
1072 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1073 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1074 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1075 lttng_log_level_rule_destroy(log_level_rule
);
1080 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1082 struct parse_event_rule_res res
;
1083 struct lttng_condition
*c
;
1086 res
= parse_event_rule(argc
, argv
);
1092 c
= lttng_condition_on_event_create(res
.er
);
1093 lttng_event_rule_destroy(res
.er
);
1099 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1101 enum lttng_condition_status status
;
1102 struct lttng_event_expr
**expr
=
1103 lttng_dynamic_array_get_element(
1104 &res
.capture_descriptors
.array
, i
);
1108 status
= lttng_condition_on_event_append_capture_descriptor(
1110 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1111 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1112 ERR("The capture feature is unsupported by the event-rule condition type");
1118 /* Ownership of event expression moved to `c` */
1125 lttng_condition_destroy(c
);
1129 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1130 lttng_event_rule_destroy(res
.er
);
1135 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1137 struct lttng_condition
*cond
= NULL
;
1138 struct argpar_state
*state
= NULL
;
1139 struct argpar_item
*item
= NULL
;
1140 const char *threshold_arg
= NULL
;
1141 const char *session_name_arg
= NULL
;
1144 enum lttng_condition_status condition_status
;
1146 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1148 ERR("Failed to allocate an argpar state.");
1153 enum argpar_state_parse_next_status status
;
1155 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1156 status
= argpar_state_parse_next(state
, &item
, &error
);
1157 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1160 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1161 /* Just stop parsing here. */
1163 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1167 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1169 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1170 const struct argpar_item_opt
*item_opt
=
1171 (const struct argpar_item_opt
*) item
;
1173 switch (item_opt
->descr
->id
) {
1178 const struct argpar_item_non_opt
*item_non_opt
;
1180 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1182 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1184 switch (item_non_opt
->non_opt_index
) {
1186 session_name_arg
= item_non_opt
->arg
;
1189 threshold_arg
= item_non_opt
->arg
;
1192 ERR("Unexpected argument `%s`.",
1199 *argc
-= argpar_state_get_ingested_orig_args(state
);
1200 *argv
+= argpar_state_get_ingested_orig_args(state
);
1202 if (!session_name_arg
) {
1203 ERR("Missing session name argument.");
1207 if (!threshold_arg
) {
1208 ERR("Missing threshold argument.");
1212 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1213 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1217 cond
= lttng_condition_session_consumed_size_create();
1219 ERR("Failed to allocate a session consumed size condition.");
1223 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1224 cond
, session_name_arg
);
1225 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1226 ERR("Failed to set session consumed size condition's session name to '%s'.",
1231 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1233 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1234 ERR("Failed to set session consumed size condition threshold.");
1241 lttng_condition_destroy(cond
);
1245 argpar_state_destroy(state
);
1246 argpar_item_destroy(item
);
1252 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1254 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1259 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1261 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1266 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1268 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1273 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1275 ERR("Session rotation completed conditions are unsupported for the moment.");
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
},
1287 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1288 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1289 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1290 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1291 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1295 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1299 struct lttng_condition
*cond
;
1300 const struct condition_descr
*descr
= NULL
;
1302 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1303 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1304 descr
= &condition_descrs
[i
];
1310 ERR("Unknown condition name '%s'", condition_name
);
1314 cond
= descr
->handler(argc
, argv
);
1316 /* The handler has already printed an error message. */
1327 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1330 char **tokens
= NULL
;
1331 struct lttng_rate_policy
*policy
= NULL
;
1332 enum lttng_rate_policy_type policy_type
;
1333 unsigned long long value
;
1334 char *policy_type_str
;
1335 char *policy_value_str
;
1340 * rate policy fields are separated by ':'.
1342 tokens
= strutils_split(policy_str
, ':', 1);
1343 num_token
= strutils_array_of_strings_len(tokens
);
1346 * Early sanity check that the number of parameter is exactly 2.
1349 if (num_token
!= 2) {
1350 ERR("Rate policy format is invalid.");
1354 policy_type_str
= tokens
[0];
1355 policy_value_str
= tokens
[1];
1357 /* Parse the type. */
1358 if (strcmp(policy_type_str
, "once-after") == 0) {
1359 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1360 } else if (strcmp(policy_type_str
, "every") == 0) {
1361 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1363 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1367 /* Parse the value. */
1368 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1369 ERR("Failed to parse rate policy value `%s` as an integer.",
1375 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1379 switch (policy_type
) {
1380 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1381 policy
= lttng_rate_policy_every_n_create(value
);
1383 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1384 policy
= lttng_rate_policy_once_after_n_create(value
);
1390 if (policy
== NULL
) {
1391 ERR("Failed to create rate policy `%s`.", policy_str
);
1395 strutils_free_null_terminated_array_of_strings(tokens
);
1399 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1400 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1401 ARGPAR_OPT_DESCR_SENTINEL
1405 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1407 struct lttng_action
*action
= NULL
;
1408 struct argpar_state
*state
= NULL
;
1409 struct argpar_item
*item
= NULL
;
1411 struct lttng_rate_policy
*policy
= NULL
;
1413 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1415 ERR("Failed to allocate an argpar state.");
1420 enum argpar_state_parse_next_status status
;
1422 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1423 status
= argpar_state_parse_next(state
, &item
, &error
);
1424 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1427 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1428 /* Just stop parsing here. */
1430 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1434 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1436 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1437 const struct argpar_item_opt
*item_opt
=
1438 (const struct argpar_item_opt
*) item
;
1440 switch (item_opt
->descr
->id
) {
1441 case OPT_RATE_POLICY
:
1443 policy
= parse_rate_policy(item_opt
->arg
);
1453 const struct argpar_item_non_opt
*item_non_opt
;
1455 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1457 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1459 switch (item_non_opt
->non_opt_index
) {
1461 ERR("Unexpected argument `%s`.",
1468 *argc
-= argpar_state_get_ingested_orig_args(state
);
1469 *argv
+= argpar_state_get_ingested_orig_args(state
);
1471 action
= lttng_action_notify_create();
1473 ERR("Failed to create notify action");
1478 enum lttng_action_status status
;
1479 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1480 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1481 ERR("Failed to set rate policy");
1489 lttng_action_destroy(action
);
1493 lttng_rate_policy_destroy(policy
);
1494 argpar_state_destroy(state
);
1495 argpar_item_destroy(item
);
1500 * Generic handler for a kind of action that takes a session name and an
1501 * optional rate policy.
1504 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1506 struct lttng_action
*(*create_action_cb
)(void),
1507 enum lttng_action_status (*set_session_name_cb
)(
1508 struct lttng_action
*, const char *),
1509 enum lttng_action_status (*set_rate_policy_cb
)(
1510 struct lttng_action
*,
1511 const struct lttng_rate_policy
*),
1512 const char *action_name
)
1514 struct lttng_action
*action
= NULL
;
1515 struct argpar_state
*state
= NULL
;
1516 struct argpar_item
*item
= NULL
;
1517 const char *session_name_arg
= NULL
;
1519 enum lttng_action_status action_status
;
1520 struct lttng_rate_policy
*policy
= NULL
;
1522 assert(set_session_name_cb
);
1523 assert(set_rate_policy_cb
);
1525 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1526 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1527 ARGPAR_OPT_DESCR_SENTINEL
1530 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1532 ERR("Failed to allocate an argpar state.");
1537 enum argpar_state_parse_next_status status
;
1539 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1540 status
= argpar_state_parse_next(state
, &item
, &error
);
1541 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1544 } else if (status
==
1545 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1546 /* Just stop parsing here. */
1548 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1552 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1553 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1554 const struct argpar_item_opt
*item_opt
=
1555 (const struct argpar_item_opt
*) item
;
1557 switch (item_opt
->descr
->id
) {
1558 case OPT_RATE_POLICY
:
1560 policy
= parse_rate_policy(item_opt
->arg
);
1570 const struct argpar_item_non_opt
*item_non_opt
;
1571 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1573 switch (item_non_opt
->non_opt_index
) {
1575 session_name_arg
= item_non_opt
->arg
;
1578 ERR("Unexpected argument `%s`.",
1585 *argc
-= argpar_state_get_ingested_orig_args(state
);
1586 *argv
+= argpar_state_get_ingested_orig_args(state
);
1588 if (!session_name_arg
) {
1589 ERR("Missing session name.");
1593 action
= create_action_cb();
1595 ERR("Failed to allocate %s session action.", action_name
);
1599 action_status
= set_session_name_cb(action
, session_name_arg
);
1600 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1601 ERR("Failed to set action %s session's session name to '%s'.",
1602 action_name
, session_name_arg
);
1607 action_status
= set_rate_policy_cb(action
, policy
);
1608 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1609 ERR("Failed to set rate policy");
1617 lttng_action_destroy(action
);
1619 argpar_item_destroy(item
);
1621 lttng_rate_policy_destroy(policy
);
1623 argpar_state_destroy(state
);
1628 struct lttng_action
*handle_action_start_session(int *argc
,
1631 return handle_action_simple_session_with_policy(argc
, argv
,
1632 lttng_action_start_session_create
,
1633 lttng_action_start_session_set_session_name
,
1634 lttng_action_start_session_set_rate_policy
, "start");
1638 struct lttng_action
*handle_action_stop_session(int *argc
,
1641 return handle_action_simple_session_with_policy(argc
, argv
,
1642 lttng_action_stop_session_create
,
1643 lttng_action_stop_session_set_session_name
,
1644 lttng_action_stop_session_set_rate_policy
, "stop");
1648 struct lttng_action
*handle_action_rotate_session(int *argc
,
1651 return handle_action_simple_session_with_policy(argc
, argv
,
1652 lttng_action_rotate_session_create
,
1653 lttng_action_rotate_session_set_session_name
,
1654 lttng_action_rotate_session_set_rate_policy
,
1658 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1659 { OPT_NAME
, 'n', "name", true },
1660 { OPT_MAX_SIZE
, 'm', "max-size", true },
1661 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1662 { OPT_DATA_URL
, '\0', "data-url", true },
1663 { OPT_URL
, '\0', "url", true },
1664 { OPT_PATH
, '\0', "path", true },
1665 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1666 ARGPAR_OPT_DESCR_SENTINEL
1670 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1673 struct lttng_action
*action
= NULL
;
1674 struct argpar_state
*state
= NULL
;
1675 struct argpar_item
*item
= NULL
;
1676 const char *session_name_arg
= NULL
;
1677 char *snapshot_name_arg
= NULL
;
1678 char *ctrl_url_arg
= NULL
;
1679 char *data_url_arg
= NULL
;
1680 char *max_size_arg
= NULL
;
1681 char *url_arg
= NULL
;
1682 char *path_arg
= NULL
;
1684 enum lttng_action_status action_status
;
1685 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1686 struct lttng_rate_policy
*policy
= NULL
;
1688 unsigned int locations_specified
= 0;
1690 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1692 ERR("Failed to allocate an argpar state.");
1697 enum argpar_state_parse_next_status status
;
1699 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1700 status
= argpar_state_parse_next(state
, &item
, &error
);
1701 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1704 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1705 /* Just stop parsing here. */
1707 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1711 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1713 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1714 const struct argpar_item_opt
*item_opt
=
1715 (const struct argpar_item_opt
*) item
;
1717 switch (item_opt
->descr
->id
) {
1719 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1725 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1731 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1737 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1743 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1749 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1754 case OPT_RATE_POLICY
:
1756 policy
= parse_rate_policy(item_opt
->arg
);
1766 const struct argpar_item_non_opt
*item_non_opt
;
1768 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1770 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1772 switch (item_non_opt
->non_opt_index
) {
1774 session_name_arg
= item_non_opt
->arg
;
1777 ERR("Unexpected argument `%s`.",
1784 *argc
-= argpar_state_get_ingested_orig_args(state
);
1785 *argv
+= argpar_state_get_ingested_orig_args(state
);
1787 if (!session_name_arg
) {
1788 ERR("Missing session name.");
1792 /* --ctrl-url and --data-url must come in pair. */
1793 if (ctrl_url_arg
&& !data_url_arg
) {
1794 ERR("--ctrl-url is specified, but --data-url is missing.");
1798 if (!ctrl_url_arg
&& data_url_arg
) {
1799 ERR("--data-url is specified, but --ctrl-url is missing.");
1803 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1804 locations_specified
+= !!url_arg
;
1805 locations_specified
+= !!path_arg
;
1807 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1808 if (locations_specified
> 1) {
1809 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1814 * Did the user specify an option that implies using a
1815 * custom/unregistered output?
1817 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1818 snapshot_output
= lttng_snapshot_output_create();
1819 if (!snapshot_output
) {
1820 ERR("Failed to allocate a snapshot output.");
1825 action
= lttng_action_snapshot_session_create();
1827 ERR("Failed to allocate snapshot session action.");
1831 action_status
= lttng_action_snapshot_session_set_session_name(
1832 action
, session_name_arg
);
1833 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1834 ERR("Failed to set action snapshot session's session name to '%s'.",
1839 if (snapshot_name_arg
) {
1840 if (!snapshot_output
) {
1841 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1845 ret
= lttng_snapshot_output_set_name(
1846 snapshot_name_arg
, snapshot_output
);
1848 ERR("Failed to set name of snapshot output.");
1856 if (!snapshot_output
) {
1857 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1861 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1863 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1867 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1869 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1877 struct lttng_uri
*uris
;
1879 if (!strstr(url_arg
, "://")) {
1880 ERR("Failed to parse '%s' as an URL.", url_arg
);
1884 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1886 ERR("Failed to parse '%s' as an URL.", url_arg
);
1890 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1891 ret
= lttng_snapshot_output_set_local_path(
1892 uris
[0].dst
.path
, snapshot_output
);
1895 ERR("Failed to assign '%s' as a local destination.",
1900 ret
= lttng_snapshot_output_set_network_url(
1901 url_arg
, snapshot_output
);
1904 ERR("Failed to assign '%s' as a network URL.",
1912 ret
= lttng_snapshot_output_set_local_path(
1913 path_arg
, snapshot_output
);
1915 ERR("Failed to parse '%s' as a local path.", path_arg
);
1922 * Two argument form, network output with separate control and
1925 ret
= lttng_snapshot_output_set_network_urls(
1926 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1928 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1929 ctrl_url_arg
, data_url_arg
);
1934 if (snapshot_output
) {
1935 action_status
= lttng_action_snapshot_session_set_output(
1936 action
, snapshot_output
);
1937 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1938 ERR("Failed to set snapshot session action's output.");
1942 /* Ownership of `snapshot_output` has been transferred to the action. */
1943 snapshot_output
= NULL
;
1947 enum lttng_action_status status
;
1948 status
= lttng_action_snapshot_session_set_rate_policy(
1950 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1951 ERR("Failed to set rate policy");
1959 lttng_action_destroy(action
);
1963 free(snapshot_name_arg
);
1968 free(snapshot_output
);
1970 lttng_rate_policy_destroy(policy
);
1971 argpar_state_destroy(state
);
1972 argpar_item_destroy(item
);
1976 struct action_descr
{
1978 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1982 struct action_descr action_descrs
[] = {
1983 { "notify", handle_action_notify
},
1984 { "start-session", handle_action_start_session
},
1985 { "stop-session", handle_action_stop_session
},
1986 { "rotate-session", handle_action_rotate_session
},
1987 { "snapshot-session", handle_action_snapshot_session
},
1991 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
1994 struct lttng_action
*action
;
1995 const struct action_descr
*descr
= NULL
;
1997 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1998 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1999 descr
= &action_descrs
[i
];
2005 ERR("Unknown action name: %s", action_name
);
2009 action
= descr
->handler(argc
, argv
);
2011 /* The handler has already printed an error message. */
2023 struct argpar_opt_descr add_trigger_options
[] = {
2024 { OPT_HELP
, 'h', "help", false },
2025 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2026 { OPT_CONDITION
, '\0', "condition", true },
2027 { OPT_ACTION
, '\0', "action", true },
2028 { OPT_NAME
, '\0', "name", true },
2029 { OPT_USER_ID
, '\0', "user-id", true },
2030 ARGPAR_OPT_DESCR_SENTINEL
,
2034 void lttng_actions_destructor(void *p
)
2036 struct lttng_action
*action
= p
;
2038 lttng_action_destroy(action
);
2041 int cmd_add_trigger(int argc
, const char **argv
)
2044 int my_argc
= argc
- 1;
2045 const char **my_argv
= argv
+ 1;
2046 struct lttng_condition
*condition
= NULL
;
2047 struct lttng_dynamic_pointer_array actions
;
2048 struct argpar_state
*argpar_state
= NULL
;
2049 struct argpar_item
*argpar_item
= NULL
;
2050 struct lttng_action
*action_group
= NULL
;
2051 struct lttng_action
*action
= NULL
;
2052 struct lttng_trigger
*trigger
= NULL
;
2056 char *user_id
= NULL
;
2058 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2061 enum argpar_state_parse_next_status status
;
2062 const struct argpar_item_opt
*item_opt
;
2065 argpar_state_destroy(argpar_state
);
2066 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2067 add_trigger_options
);
2068 if (!argpar_state
) {
2069 ERR("Failed to create argpar state.");
2073 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2074 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2075 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2078 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2081 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2085 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2087 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2088 const struct argpar_item_non_opt
*item_non_opt
=
2089 (const struct argpar_item_non_opt
*)
2092 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2096 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2098 ingested_args
= argpar_state_get_ingested_orig_args(
2101 my_argc
-= ingested_args
;
2102 my_argv
+= ingested_args
;
2104 switch (item_opt
->descr
->id
) {
2109 case OPT_LIST_OPTIONS
:
2110 list_cmd_options_argpar(stdout
, add_trigger_options
);
2116 ERR("A --condition was already given.");
2120 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2123 * An error message was already printed by
2133 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2136 * An error message was already printed by
2142 ret
= lttng_dynamic_pointer_array_add_pointer(
2145 ERR("Failed to add pointer to pointer array.");
2149 /* Ownership of the action was transferred to the group. */
2156 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2164 if (!assign_string(&user_id
, item_opt
->arg
,
2177 ERR("Missing --condition.");
2181 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2182 ERR("Need at least one --action.");
2186 action_group
= lttng_action_group_create();
2187 if (!action_group
) {
2191 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2192 enum lttng_action_status status
;
2194 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2196 status
= lttng_action_group_add_action(action_group
, action
);
2197 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2202 * The `lttng_action_group_add_action()` takes a reference to
2203 * the action. We can destroy ours.
2205 lttng_action_destroy(action
);
2209 trigger
= lttng_trigger_create(condition
, action_group
);
2215 enum lttng_trigger_status trigger_status
=
2216 lttng_trigger_set_name(trigger
, name
);
2218 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2219 ERR("Failed to set trigger name.");
2225 enum lttng_trigger_status trigger_status
;
2230 uid
= strtol(user_id
, &end
, 10);
2231 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
2232 ERR("Failed to parse `%s` as a user id.", user_id
);
2235 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2236 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2237 ERR("Failed to set trigger's user identity.");
2242 ret
= lttng_register_trigger(trigger
);
2244 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2248 MSG("Trigger registered successfully.");
2256 argpar_state_destroy(argpar_state
);
2257 argpar_item_destroy(argpar_item
);
2258 lttng_dynamic_pointer_array_reset(&actions
);
2259 lttng_condition_destroy(condition
);
2260 lttng_action_destroy(action_group
);
2261 lttng_action_destroy(action
);
2262 lttng_trigger_destroy(trigger
);