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>
65 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
66 { OPT_FILTER
, 'f', "filter", true },
67 { OPT_NAME
, 'n', "name", true },
68 { OPT_EXCLUDE_NAMES
, 'x', "exclude-names", true },
69 { OPT_LOG_LEVEL
, 'l', "log-level", true },
70 { OPT_EVENT_NAME
, 'E', "event-name", true },
72 { OPT_DOMAIN
, 'd', "domain", true },
73 { OPT_TYPE
, 't', "type", true },
74 { OPT_LOCATION
, 'L', "location", true },
76 /* Capture descriptor */
77 { OPT_CAPTURE
, '\0', "capture", true },
79 ARGPAR_OPT_DESCR_SENTINEL
83 bool assign_domain_type(enum lttng_domain_type
*dest
, const char *arg
)
87 if (*dest
!= LTTNG_DOMAIN_NONE
) {
88 ERR("More than one `--domain` was specified.");
92 if (strcmp(arg
, "kernel") == 0) {
93 *dest
= LTTNG_DOMAIN_KERNEL
;
94 } else if (strcmp(arg
, "user") == 0 || strcmp(arg
, "userspace") == 0) {
95 *dest
= LTTNG_DOMAIN_UST
;
96 } else if (strcmp(arg
, "jul") == 0) {
97 *dest
= LTTNG_DOMAIN_JUL
;
98 } else if (strcmp(arg
, "log4j") == 0) {
99 *dest
= LTTNG_DOMAIN_LOG4J
;
100 } else if (strcmp(arg
, "python") == 0) {
101 *dest
= LTTNG_DOMAIN_PYTHON
;
103 ERR("Invalid `--domain` value: %s", arg
);
118 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
122 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
123 ERR("More than one `--type` was specified.");
127 if (strcmp(arg
, "tracepoint") == 0 || strcmp(arg
, "logging") == 0) {
128 *dest
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
129 } else if (strcmp (arg
, "kprobe") == 0 || strcmp(arg
, "kernel-probe") == 0) {
130 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
;
131 } else if (strcmp (arg
, "uprobe") == 0 || strcmp(arg
, "userspace-probe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
;
133 } else if (strcmp (arg
, "function") == 0) {
134 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
;
135 } else if (strcmp (arg
, "syscall") == 0) {
136 *dest
= LTTNG_EVENT_RULE_TYPE_SYSCALL
;
138 ERR("Invalid `--type` value: %s", arg
);
153 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
158 ERR("Duplicate '%s' given.", opt_name
);
164 PERROR("Failed to allocate string '%s'.", opt_name
);
178 /* This is defined in enable_events.c. */
180 int create_exclusion_list_and_validate(const char *event_name
,
181 const char *exclusions_arg
,
182 char ***exclusion_list
);
185 * Parse `str` as a log level in domain `domain_type`.
187 * Return the log level in `*log_level`. Return true in `*log_level_only` if
188 * the string specifies exactly this log level, false if it specifies at least
191 * Return true if the string was successfully parsed as a log level string.
193 static bool parse_log_level_string(const char *str
,
194 enum lttng_domain_type domain_type
,
196 bool *log_level_only
)
200 switch (domain_type
) {
201 case LTTNG_DOMAIN_UST
:
203 enum lttng_loglevel log_level_min
, log_level_max
;
204 if (!loglevel_parse_range_string(
205 str
, &log_level_min
, &log_level_max
)) {
209 /* Only support VAL and VAL.. for now. */
210 if (log_level_min
!= log_level_max
&&
211 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
215 *log_level
= (int) log_level_min
;
216 *log_level_only
= log_level_min
== log_level_max
;
219 case LTTNG_DOMAIN_LOG4J
:
221 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
222 if (!loglevel_log4j_parse_range_string(
223 str
, &log_level_min
, &log_level_max
)) {
227 /* Only support VAL and VAL.. for now. */
228 if (log_level_min
!= log_level_max
&&
229 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
233 *log_level
= (int) log_level_min
;
234 *log_level_only
= log_level_min
== log_level_max
;
237 case LTTNG_DOMAIN_JUL
:
239 enum lttng_loglevel_jul log_level_min
, log_level_max
;
240 if (!loglevel_jul_parse_range_string(
241 str
, &log_level_min
, &log_level_max
)) {
245 /* Only support VAL and VAL.. for now. */
246 if (log_level_min
!= log_level_max
&&
247 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
251 *log_level
= (int) log_level_min
;
252 *log_level_only
= log_level_min
== log_level_max
;
255 case LTTNG_DOMAIN_PYTHON
:
257 enum lttng_loglevel_python log_level_min
, log_level_max
;
258 if (!loglevel_python_parse_range_string(
259 str
, &log_level_min
, &log_level_max
)) {
263 /* Only support VAL and VAL.. for now. */
264 if (log_level_min
!= log_level_max
&&
266 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
270 *log_level
= (int) log_level_min
;
271 *log_level_only
= log_level_min
== log_level_max
;
275 /* Invalid domain type. */
289 static int parse_kernel_probe_opts(const char *source
,
290 struct lttng_kernel_probe_location
**location
)
295 char name
[LTTNG_SYMBOL_NAME_LEN
];
296 char *symbol_name
= NULL
;
299 /* Check for symbol+offset. */
300 match
= sscanf(source
,
301 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
305 if (*s_hex
== '\0') {
306 ERR("Kernel probe symbol offset is missing.");
310 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
312 PERROR("Failed to copy kernel probe location symbol name.");
315 offset
= strtoul(s_hex
, NULL
, 0);
317 *location
= lttng_kernel_probe_location_symbol_create(
318 symbol_name
, offset
);
320 ERR("Failed to create symbol kernel probe location.");
327 /* Check for symbol. */
328 if (isalpha(name
[0]) || name
[0] == '_') {
329 match
= sscanf(source
,
330 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
334 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
336 ERR("Failed to copy kernel probe location symbol name.");
340 *location
= lttng_kernel_probe_location_symbol_create(
343 ERR("Failed to create symbol kernel probe location.");
351 /* Check for address. */
352 match
= sscanf(source
, "%18s", s_hex
);
356 if (*s_hex
== '\0') {
357 ERR("Invalid kernel probe location address.");
361 address
= strtoul(s_hex
, NULL
, 0);
362 *location
= lttng_kernel_probe_location_address_create(address
);
364 ERR("Failed to create symbol kernel probe location.");
382 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
383 const struct ir_load_expression
*load_expr
,
384 const char *capture_str
)
386 char *provider_name
= NULL
;
387 struct lttng_event_expr
*event_expr
= NULL
;
388 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
389 const enum ir_load_expression_type load_expr_child_type
=
392 switch (load_expr_child_type
) {
393 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
394 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
396 const char *field_name
;
398 load_expr_op
= load_expr_op
->next
;
399 assert(load_expr_op
);
400 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
401 field_name
= load_expr_op
->u
.symbol
;
404 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
405 lttng_event_expr_event_payload_field_create(field_name
) :
406 lttng_event_expr_channel_context_field_create(field_name
);
408 ERR("Failed to create %s event expression: field name = `%s`.",
409 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
410 "payload field" : "channel context",
417 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
420 const char *type_name
;
421 const char *field_name
;
423 load_expr_op
= load_expr_op
->next
;
424 assert(load_expr_op
);
425 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
426 field_name
= load_expr_op
->u
.symbol
;
430 * The field name needs to be of the form PROVIDER:TYPE. We
433 colon
= strchr(field_name
, ':');
435 ERR("Invalid app-specific context field name: missing colon in `%s`.",
440 type_name
= colon
+ 1;
441 if (*type_name
== '\0') {
442 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
447 provider_name
= strndup(field_name
, colon
- field_name
);
448 if (!provider_name
) {
449 PERROR("Failed to allocate field name string");
453 event_expr
= lttng_event_expr_app_specific_context_field_create(
454 provider_name
, type_name
);
456 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
457 provider_name
, type_name
);
464 ERR("%s: unexpected load expr type %d.", __func__
,
469 load_expr_op
= load_expr_op
->next
;
471 /* There may be a single array index after that. */
472 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
473 struct lttng_event_expr
*index_event_expr
;
474 const uint64_t index
= load_expr_op
->u
.index
;
476 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
477 if (!index_event_expr
) {
478 ERR("Failed to create array field element event expression.");
482 event_expr
= index_event_expr
;
483 load_expr_op
= load_expr_op
->next
;
486 switch (load_expr_op
->type
) {
487 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
489 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
490 * always found at the end of the chain.
493 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
494 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
499 ERR("%s: unexpected load expression operator %s.", __func__
,
500 ir_load_expression_type_str(load_expr_op
->type
));
507 lttng_event_expr_destroy(event_expr
);
517 struct lttng_event_expr
*ir_op_load_to_event_expr(
518 const struct ir_op
*ir
, const char *capture_str
)
520 struct lttng_event_expr
*event_expr
= NULL
;
522 assert(ir
->op
== IR_OP_LOAD
);
524 switch (ir
->data_type
) {
525 case IR_DATA_EXPRESSION
:
527 const struct ir_load_expression
*ir_load_expr
=
528 ir
->u
.load
.u
.expression
;
530 event_expr
= ir_op_load_expr_to_event_expr(
531 ir_load_expr
, capture_str
);
535 ERR("%s: unexpected data type: %s.", __func__
,
536 ir_data_type_str(ir
->data_type
));
544 const char *ir_operator_type_human_str(enum ir_op_type op
)
566 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
567 const char *capture_str
)
569 struct lttng_event_expr
*event_expr
= NULL
;
571 assert(ir
->op
== IR_OP_ROOT
);
572 ir
= ir
->u
.root
.child
;
576 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
581 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
583 ir_operator_type_human_str(ir
->op
));
586 ERR("%s: unexpected IR op type: %s.", __func__
,
587 ir_op_type_str(ir
->op
));
595 void destroy_event_expr(void *ptr
)
597 lttng_event_expr_destroy(ptr
);
600 struct parse_event_rule_res
{
602 struct lttng_event_rule
*er
;
604 /* Array of `struct lttng_event_expr *` */
605 struct lttng_dynamic_pointer_array capture_descriptors
;
609 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
611 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
612 enum lttng_event_rule_type event_rule_type
=
613 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
614 struct argpar_state
*state
;
615 struct argpar_item
*item
= NULL
;
617 int consumed_args
= -1;
618 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
619 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
620 struct parse_event_rule_res res
= { 0 };
621 struct lttng_event_expr
*event_expr
= NULL
;
622 struct filter_parser_ctx
*parser_ctx
= NULL
;
623 struct lttng_log_level_rule
*log_level_rule
= NULL
;
625 /* Tracepoint and syscall options. */
627 char *exclude_names
= NULL
;
628 char **exclusion_list
= NULL
;
630 /* For userspace / kernel probe and function. */
631 char *location
= NULL
;
632 char *event_name
= NULL
;
638 char *log_level_str
= NULL
;
640 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
642 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
644 ERR("Failed to allocate an argpar state.");
649 enum argpar_state_parse_next_status status
;
651 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
652 status
= argpar_state_parse_next(state
, &item
, &error
);
653 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
656 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
657 /* Just stop parsing here. */
659 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
663 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
665 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
666 const struct argpar_item_opt
*item_opt
=
667 (const struct argpar_item_opt
*) item
;
669 switch (item_opt
->descr
->id
) {
672 if (!assign_domain_type(&domain_type
,
679 if (!assign_event_rule_type(&event_rule_type
,
686 if (!assign_string(&location
,
694 if (!assign_string(&event_name
,
696 "--event-name/-E")) {
702 if (!assign_string(&filter
, item_opt
->arg
,
709 if (!assign_string(&name
, item_opt
->arg
,
715 case OPT_EXCLUDE_NAMES
:
716 if (!assign_string(&exclude_names
,
718 "--exclude-names/-x")) {
724 if (!assign_string(&log_level_str
,
725 item_opt
->arg
, "--log-level/-l")) {
733 const char *capture_str
= item_opt
->arg
;
735 ret
= filter_parser_ctx_create_from_filter_expression(
736 capture_str
, &parser_ctx
);
738 ERR("Failed to parse capture expression `%s`.",
743 event_expr
= ir_op_root_to_event_expr(
748 * ir_op_root_to_event_expr has printed
754 ret
= lttng_dynamic_pointer_array_add_pointer(
755 &res
.capture_descriptors
,
762 * The ownership of event expression was
763 * transferred to the dynamic array.
773 const struct argpar_item_non_opt
*item_non_opt
=
774 (const struct argpar_item_non_opt
*)
777 /* Don't accept non-option arguments. */
778 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
783 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
784 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
788 * Option --name is applicable to event rules of type tracepoint
789 * and syscall. For tracepoint and syscall rules, if --name is
790 * omitted, it is implicitly "*".
792 switch (event_rule_type
) {
793 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
794 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
802 ERR("Can't use --name with %s event rules.",
803 lttng_event_rule_type_str(
809 ERR("Can't use --exclude-names/-x with %s event rules.",
810 lttng_event_rule_type_str(
817 * Option --location is only applicable to (and mandatory for) event
818 * rules of type {k,u}probe and function.
820 * Option --event-name is only applicable to event rules of type probe.
821 * If omitted, it defaults to the location.
823 switch (event_rule_type
) {
824 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
825 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
826 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
828 ERR("Event rule of type %s requires a --location.",
829 lttng_event_rule_type_str(event_rule_type
));
834 event_name
= strdup(location
);
841 ERR("Can't use --location with %s event rules.",
842 lttng_event_rule_type_str(event_rule_type
));
847 ERR("Can't use --event-name with %s event rules.",
848 lttng_event_rule_type_str(
855 * Update *argc and *argv so our caller can keep parsing what follows.
857 consumed_args
= argpar_state_get_ingested_orig_args(state
);
858 assert(consumed_args
>= 0);
859 *argc
-= consumed_args
;
860 *argv
+= consumed_args
;
862 /* Need to specify a domain. */
863 if (domain_type
== LTTNG_DOMAIN_NONE
) {
864 ERR("Please specify a domain (--domain=(kernel,user,jul,log4j,python)).");
868 /* Validate event rule type against domain. */
869 switch (event_rule_type
) {
870 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
871 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
872 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
873 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
874 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
875 ERR("Event type not available for user-space tracing.");
880 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
888 * Adding a filter to a probe, function or userspace-probe would be
889 * denied by the kernel tracer as it's not supported at the moment. We
890 * do an early check here to warn the user.
892 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
893 switch (event_rule_type
) {
894 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
895 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
898 ERR("Filter expressions are not supported for %s event rules.",
899 lttng_event_rule_type_str(event_rule_type
));
904 /* If --exclude/-x was passed, split it into an exclusion list. */
906 if (domain_type
!= LTTNG_DOMAIN_UST
) {
907 ERR("Event name exclusions are not yet implemented for %s event rules.",
908 get_domain_str(domain_type
));
912 if (create_exclusion_list_and_validate(name
,
913 exclude_names
, &exclusion_list
) != 0) {
914 ERR("Failed to create exclusion list.");
920 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
921 ERR("Log levels are only applicable to tracepoint event rules.");
925 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
926 ERR("Log levels are not supported by the kernel tracer.");
931 /* Finally, create the event rule object. */
932 switch (event_rule_type
) {
933 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
935 enum lttng_event_rule_status event_rule_status
;
937 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
939 ERR("Failed to create tracepoint event rule.");
944 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
946 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
947 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
954 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
956 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
957 ERR("Failed to set tracepoint event rule's filter to '%s'.",
963 /* Set exclusion list. */
964 if (exclusion_list
) {
967 for (n
= 0; exclusion_list
[n
]; n
++) {
968 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
971 if (event_rule_status
!=
972 LTTNG_EVENT_RULE_STATUS_OK
) {
973 ERR("Failed to set tracepoint exclusion list element '%s'",
981 * ".." is the same as passing no log level option and
982 * correspond the the "ANY" case.
984 if (log_level_str
&& strcmp(log_level_str
, "..") != 0) {
988 if (!parse_log_level_string(log_level_str
, domain_type
,
989 &log_level
, &log_level_only
)) {
990 ERR("Failed to parse log level string `%s`.",
995 if (log_level_only
) {
996 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
998 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
1001 if (log_level_rule
== NULL
) {
1002 ERR("Failed to create log level rule object.");
1007 lttng_event_rule_tracepoint_set_log_level_rule(
1008 res
.er
, log_level_rule
);
1010 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1011 ERR("Failed to set log level on event fule.");
1018 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1021 enum lttng_event_rule_status event_rule_status
;
1023 ret
= parse_kernel_probe_opts(
1024 location
, &kernel_probe_location
);
1026 ERR("Failed to parse kernel probe location.");
1030 assert(kernel_probe_location
);
1031 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1033 ERR("Failed to create kprobe event rule.");
1038 lttng_event_rule_kernel_probe_set_event_name(
1039 res
.er
, event_name
);
1040 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1041 ERR("Failed to set kprobe event rule's name to '%s'.",
1048 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1051 enum lttng_event_rule_status event_rule_status
;
1053 ret
= parse_userspace_probe_opts(
1054 location
, &userspace_probe_location
);
1056 ERR("Failed to parse user space probe location.");
1060 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1062 ERR("Failed to create userspace probe event rule.");
1067 lttng_event_rule_userspace_probe_set_event_name(
1068 res
.er
, event_name
);
1069 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1070 ERR("Failed to set user space probe event rule's name to '%s'.",
1077 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1079 enum lttng_event_rule_status event_rule_status
;
1081 res
.er
= lttng_event_rule_syscall_create();
1083 ERR("Failed to create syscall event rule.");
1087 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1089 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1090 ERR("Failed to set syscall event rule's pattern to '%s'.",
1096 event_rule_status
= lttng_event_rule_syscall_set_filter(
1098 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1099 ERR("Failed to set syscall event rule's filter to '%s'.",
1115 lttng_event_rule_destroy(res
.er
);
1117 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1121 filter_parser_ctx_free(parser_ctx
);
1124 lttng_event_expr_destroy(event_expr
);
1125 argpar_item_destroy(item
);
1127 argpar_state_destroy(state
);
1130 free(exclude_names
);
1131 free(log_level_str
);
1135 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1136 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1137 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1138 lttng_log_level_rule_destroy(log_level_rule
);
1143 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1145 struct parse_event_rule_res res
;
1146 struct lttng_condition
*c
;
1149 res
= parse_event_rule(argc
, argv
);
1155 c
= lttng_condition_on_event_create(res
.er
);
1156 lttng_event_rule_destroy(res
.er
);
1162 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1164 enum lttng_condition_status status
;
1165 struct lttng_event_expr
**expr
=
1166 lttng_dynamic_array_get_element(
1167 &res
.capture_descriptors
.array
, i
);
1171 status
= lttng_condition_on_event_append_capture_descriptor(
1173 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1174 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1175 ERR("The capture feature is unsupported by the event-rule condition type");
1181 /* Ownership of event expression moved to `c` */
1188 lttng_condition_destroy(c
);
1192 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1193 lttng_event_rule_destroy(res
.er
);
1198 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1200 struct lttng_condition
*cond
= NULL
;
1201 struct argpar_state
*state
= NULL
;
1202 struct argpar_item
*item
= NULL
;
1203 const char *threshold_arg
= NULL
;
1204 const char *session_name_arg
= NULL
;
1207 enum lttng_condition_status condition_status
;
1209 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1211 ERR("Failed to allocate an argpar state.");
1216 enum argpar_state_parse_next_status status
;
1218 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1219 status
= argpar_state_parse_next(state
, &item
, &error
);
1220 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1223 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1224 /* Just stop parsing here. */
1226 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1230 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1232 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1233 const struct argpar_item_opt
*item_opt
=
1234 (const struct argpar_item_opt
*) item
;
1236 switch (item_opt
->descr
->id
) {
1241 const struct argpar_item_non_opt
*item_non_opt
;
1243 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1245 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1247 switch (item_non_opt
->non_opt_index
) {
1249 session_name_arg
= item_non_opt
->arg
;
1252 threshold_arg
= item_non_opt
->arg
;
1255 ERR("Unexpected argument `%s`.",
1262 *argc
-= argpar_state_get_ingested_orig_args(state
);
1263 *argv
+= argpar_state_get_ingested_orig_args(state
);
1265 if (!session_name_arg
) {
1266 ERR("Missing session name argument.");
1270 if (!threshold_arg
) {
1271 ERR("Missing threshold argument.");
1275 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1276 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1280 cond
= lttng_condition_session_consumed_size_create();
1282 ERR("Failed to allocate a session consumed size condition.");
1286 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1287 cond
, session_name_arg
);
1288 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1289 ERR("Failed to set session consumed size condition's session name to '%s'.",
1294 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1296 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1297 ERR("Failed to set session consumed size condition threshold.");
1304 lttng_condition_destroy(cond
);
1308 argpar_state_destroy(state
);
1309 argpar_item_destroy(item
);
1315 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1317 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1322 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1324 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1329 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1331 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1336 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1338 ERR("Session rotation completed conditions are unsupported for the moment.");
1342 struct condition_descr
{
1344 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1348 struct condition_descr condition_descrs
[] = {
1349 { "event-rule-matches", handle_condition_event
},
1350 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1351 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1352 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1353 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1354 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1358 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1362 struct lttng_condition
*cond
;
1363 const struct condition_descr
*descr
= NULL
;
1365 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1366 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1367 descr
= &condition_descrs
[i
];
1373 ERR("Unknown condition name '%s'", condition_name
);
1377 cond
= descr
->handler(argc
, argv
);
1379 /* The handler has already printed an error message. */
1390 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1393 char **tokens
= NULL
;
1394 struct lttng_rate_policy
*policy
= NULL
;
1395 enum lttng_rate_policy_type policy_type
;
1396 unsigned long long value
;
1397 char *policy_type_str
;
1398 char *policy_value_str
;
1403 * rate policy fields are separated by ':'.
1405 tokens
= strutils_split(policy_str
, ':', 1);
1406 num_token
= strutils_array_of_strings_len(tokens
);
1409 * Early sanity check that the number of parameter is exactly 2.
1412 if (num_token
!= 2) {
1413 ERR("Rate policy format is invalid.");
1417 policy_type_str
= tokens
[0];
1418 policy_value_str
= tokens
[1];
1420 /* Parse the type. */
1421 if (strcmp(policy_type_str
, "once-after") == 0) {
1422 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1423 } else if (strcmp(policy_type_str
, "every") == 0) {
1424 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1426 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1430 /* Parse the value. */
1431 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1432 ERR("Failed to parse rate policy value `%s` as an integer.",
1438 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1442 switch (policy_type
) {
1443 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1444 policy
= lttng_rate_policy_every_n_create(value
);
1446 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1447 policy
= lttng_rate_policy_once_after_n_create(value
);
1453 if (policy
== NULL
) {
1454 ERR("Failed to create rate policy `%s`.", policy_str
);
1458 strutils_free_null_terminated_array_of_strings(tokens
);
1462 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1463 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1464 ARGPAR_OPT_DESCR_SENTINEL
1468 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1470 struct lttng_action
*action
= NULL
;
1471 struct argpar_state
*state
= NULL
;
1472 struct argpar_item
*item
= NULL
;
1474 struct lttng_rate_policy
*policy
= NULL
;
1476 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1478 ERR("Failed to allocate an argpar state.");
1483 enum argpar_state_parse_next_status status
;
1485 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1486 status
= argpar_state_parse_next(state
, &item
, &error
);
1487 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1490 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1491 /* Just stop parsing here. */
1493 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1497 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1499 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1500 const struct argpar_item_opt
*item_opt
=
1501 (const struct argpar_item_opt
*) item
;
1503 switch (item_opt
->descr
->id
) {
1504 case OPT_RATE_POLICY
:
1506 policy
= parse_rate_policy(item_opt
->arg
);
1516 const struct argpar_item_non_opt
*item_non_opt
;
1518 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1520 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1522 switch (item_non_opt
->non_opt_index
) {
1524 ERR("Unexpected argument `%s`.",
1531 *argc
-= argpar_state_get_ingested_orig_args(state
);
1532 *argv
+= argpar_state_get_ingested_orig_args(state
);
1534 action
= lttng_action_notify_create();
1536 ERR("Failed to create notify action");
1541 enum lttng_action_status status
;
1542 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1543 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1544 ERR("Failed to set rate policy");
1552 lttng_action_destroy(action
);
1556 lttng_rate_policy_destroy(policy
);
1557 argpar_state_destroy(state
);
1558 argpar_item_destroy(item
);
1563 * Generic handler for a kind of action that takes a session name and an
1564 * optional rate policy.
1567 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1569 struct lttng_action
*(*create_action_cb
)(void),
1570 enum lttng_action_status (*set_session_name_cb
)(
1571 struct lttng_action
*, const char *),
1572 enum lttng_action_status (*set_rate_policy_cb
)(
1573 struct lttng_action
*,
1574 const struct lttng_rate_policy
*),
1575 const char *action_name
)
1577 struct lttng_action
*action
= NULL
;
1578 struct argpar_state
*state
= NULL
;
1579 struct argpar_item
*item
= NULL
;
1580 const char *session_name_arg
= NULL
;
1582 enum lttng_action_status action_status
;
1583 struct lttng_rate_policy
*policy
= NULL
;
1585 assert(set_session_name_cb
);
1586 assert(set_rate_policy_cb
);
1588 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1589 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1590 ARGPAR_OPT_DESCR_SENTINEL
1593 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1595 ERR("Failed to allocate an argpar state.");
1600 enum argpar_state_parse_next_status status
;
1602 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1603 status
= argpar_state_parse_next(state
, &item
, &error
);
1604 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1607 } else if (status
==
1608 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1609 /* Just stop parsing here. */
1611 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1615 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1616 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1617 const struct argpar_item_opt
*item_opt
=
1618 (const struct argpar_item_opt
*) item
;
1620 switch (item_opt
->descr
->id
) {
1621 case OPT_RATE_POLICY
:
1623 policy
= parse_rate_policy(item_opt
->arg
);
1633 const struct argpar_item_non_opt
*item_non_opt
;
1634 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1636 switch (item_non_opt
->non_opt_index
) {
1638 session_name_arg
= item_non_opt
->arg
;
1641 ERR("Unexpected argument `%s`.",
1648 *argc
-= argpar_state_get_ingested_orig_args(state
);
1649 *argv
+= argpar_state_get_ingested_orig_args(state
);
1651 if (!session_name_arg
) {
1652 ERR("Missing session name.");
1656 action
= create_action_cb();
1658 ERR("Failed to allocate %s session action.", action_name
);
1662 action_status
= set_session_name_cb(action
, session_name_arg
);
1663 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1664 ERR("Failed to set action %s session's session name to '%s'.",
1665 action_name
, session_name_arg
);
1670 action_status
= set_rate_policy_cb(action
, policy
);
1671 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1672 ERR("Failed to set rate policy");
1680 lttng_action_destroy(action
);
1682 argpar_item_destroy(item
);
1684 lttng_rate_policy_destroy(policy
);
1686 argpar_state_destroy(state
);
1691 struct lttng_action
*handle_action_start_session(int *argc
,
1694 return handle_action_simple_session_with_policy(argc
, argv
,
1695 lttng_action_start_session_create
,
1696 lttng_action_start_session_set_session_name
,
1697 lttng_action_start_session_set_rate_policy
, "start");
1701 struct lttng_action
*handle_action_stop_session(int *argc
,
1704 return handle_action_simple_session_with_policy(argc
, argv
,
1705 lttng_action_stop_session_create
,
1706 lttng_action_stop_session_set_session_name
,
1707 lttng_action_stop_session_set_rate_policy
, "stop");
1711 struct lttng_action
*handle_action_rotate_session(int *argc
,
1714 return handle_action_simple_session_with_policy(argc
, argv
,
1715 lttng_action_rotate_session_create
,
1716 lttng_action_rotate_session_set_session_name
,
1717 lttng_action_rotate_session_set_rate_policy
,
1721 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1722 { OPT_NAME
, 'n', "name", true },
1723 { OPT_MAX_SIZE
, 'm', "max-size", true },
1724 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1725 { OPT_DATA_URL
, '\0', "data-url", true },
1726 { OPT_URL
, '\0', "url", true },
1727 { OPT_PATH
, '\0', "path", true },
1728 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1729 ARGPAR_OPT_DESCR_SENTINEL
1733 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1736 struct lttng_action
*action
= NULL
;
1737 struct argpar_state
*state
= NULL
;
1738 struct argpar_item
*item
= NULL
;
1739 const char *session_name_arg
= NULL
;
1740 char *snapshot_name_arg
= NULL
;
1741 char *ctrl_url_arg
= NULL
;
1742 char *data_url_arg
= NULL
;
1743 char *max_size_arg
= NULL
;
1744 char *url_arg
= NULL
;
1745 char *path_arg
= NULL
;
1747 enum lttng_action_status action_status
;
1748 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1749 struct lttng_rate_policy
*policy
= NULL
;
1751 unsigned int locations_specified
= 0;
1753 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1755 ERR("Failed to allocate an argpar state.");
1760 enum argpar_state_parse_next_status status
;
1762 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1763 status
= argpar_state_parse_next(state
, &item
, &error
);
1764 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1767 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1768 /* Just stop parsing here. */
1770 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1774 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1776 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1777 const struct argpar_item_opt
*item_opt
=
1778 (const struct argpar_item_opt
*) item
;
1780 switch (item_opt
->descr
->id
) {
1782 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1788 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1794 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1800 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1806 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1812 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1817 case OPT_RATE_POLICY
:
1819 policy
= parse_rate_policy(item_opt
->arg
);
1829 const struct argpar_item_non_opt
*item_non_opt
;
1831 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1833 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1835 switch (item_non_opt
->non_opt_index
) {
1837 session_name_arg
= item_non_opt
->arg
;
1840 ERR("Unexpected argument `%s`.",
1847 *argc
-= argpar_state_get_ingested_orig_args(state
);
1848 *argv
+= argpar_state_get_ingested_orig_args(state
);
1850 if (!session_name_arg
) {
1851 ERR("Missing session name.");
1855 /* --ctrl-url and --data-url must come in pair. */
1856 if (ctrl_url_arg
&& !data_url_arg
) {
1857 ERR("--ctrl-url is specified, but --data-url is missing.");
1861 if (!ctrl_url_arg
&& data_url_arg
) {
1862 ERR("--data-url is specified, but --ctrl-url is missing.");
1866 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1867 locations_specified
+= !!url_arg
;
1868 locations_specified
+= !!path_arg
;
1870 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1871 if (locations_specified
> 1) {
1872 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1877 * Did the user specify an option that implies using a
1878 * custom/unregistered output?
1880 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1881 snapshot_output
= lttng_snapshot_output_create();
1882 if (!snapshot_output
) {
1883 ERR("Failed to allocate a snapshot output.");
1888 action
= lttng_action_snapshot_session_create();
1890 ERR("Failed to allocate snapshot session action.");
1894 action_status
= lttng_action_snapshot_session_set_session_name(
1895 action
, session_name_arg
);
1896 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1897 ERR("Failed to set action snapshot session's session name to '%s'.",
1902 if (snapshot_name_arg
) {
1903 if (!snapshot_output
) {
1904 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1908 ret
= lttng_snapshot_output_set_name(
1909 snapshot_name_arg
, snapshot_output
);
1911 ERR("Failed to set name of snapshot output.");
1919 if (!snapshot_output
) {
1920 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1924 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1926 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1930 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1932 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1940 struct lttng_uri
*uris
;
1942 if (!strstr(url_arg
, "://")) {
1943 ERR("Failed to parse '%s' as an URL.", url_arg
);
1947 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1949 ERR("Failed to parse '%s' as an URL.", url_arg
);
1953 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1954 ret
= lttng_snapshot_output_set_local_path(
1955 uris
[0].dst
.path
, snapshot_output
);
1958 ERR("Failed to assign '%s' as a local destination.",
1963 ret
= lttng_snapshot_output_set_network_url(
1964 url_arg
, snapshot_output
);
1967 ERR("Failed to assign '%s' as a network URL.",
1975 ret
= lttng_snapshot_output_set_local_path(
1976 path_arg
, snapshot_output
);
1978 ERR("Failed to parse '%s' as a local path.", path_arg
);
1985 * Two argument form, network output with separate control and
1988 ret
= lttng_snapshot_output_set_network_urls(
1989 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1991 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1992 ctrl_url_arg
, data_url_arg
);
1997 if (snapshot_output
) {
1998 action_status
= lttng_action_snapshot_session_set_output(
1999 action
, snapshot_output
);
2000 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2001 ERR("Failed to set snapshot session action's output.");
2005 /* Ownership of `snapshot_output` has been transferred to the action. */
2006 snapshot_output
= NULL
;
2010 enum lttng_action_status status
;
2011 status
= lttng_action_snapshot_session_set_rate_policy(
2013 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2014 ERR("Failed to set rate policy");
2022 lttng_action_destroy(action
);
2026 free(snapshot_name_arg
);
2031 free(snapshot_output
);
2033 lttng_rate_policy_destroy(policy
);
2034 argpar_state_destroy(state
);
2035 argpar_item_destroy(item
);
2039 struct action_descr
{
2041 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2045 struct action_descr action_descrs
[] = {
2046 { "notify", handle_action_notify
},
2047 { "start-session", handle_action_start_session
},
2048 { "stop-session", handle_action_stop_session
},
2049 { "rotate-session", handle_action_rotate_session
},
2050 { "snapshot-session", handle_action_snapshot_session
},
2054 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2057 struct lttng_action
*action
;
2058 const struct action_descr
*descr
= NULL
;
2060 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2061 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2062 descr
= &action_descrs
[i
];
2068 ERR("Unknown action name: %s", action_name
);
2072 action
= descr
->handler(argc
, argv
);
2074 /* The handler has already printed an error message. */
2086 struct argpar_opt_descr add_trigger_options
[] = {
2087 { OPT_HELP
, 'h', "help", false },
2088 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2089 { OPT_CONDITION
, '\0', "condition", true },
2090 { OPT_ACTION
, '\0', "action", true },
2091 { OPT_NAME
, '\0', "name", true },
2092 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2093 ARGPAR_OPT_DESCR_SENTINEL
,
2097 void lttng_actions_destructor(void *p
)
2099 struct lttng_action
*action
= p
;
2101 lttng_action_destroy(action
);
2104 int cmd_add_trigger(int argc
, const char **argv
)
2107 int my_argc
= argc
- 1;
2108 const char **my_argv
= argv
+ 1;
2109 struct lttng_condition
*condition
= NULL
;
2110 struct lttng_dynamic_pointer_array actions
;
2111 struct argpar_state
*argpar_state
= NULL
;
2112 struct argpar_item
*argpar_item
= NULL
;
2113 struct lttng_action
*action_group
= NULL
;
2114 struct lttng_action
*action
= NULL
;
2115 struct lttng_trigger
*trigger
= NULL
;
2119 char *owner_uid
= NULL
;
2121 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2124 enum argpar_state_parse_next_status status
;
2125 const struct argpar_item_opt
*item_opt
;
2128 argpar_state_destroy(argpar_state
);
2129 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2130 add_trigger_options
);
2131 if (!argpar_state
) {
2132 ERR("Failed to create argpar state.");
2136 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2137 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2138 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2141 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2144 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2148 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2150 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2151 const struct argpar_item_non_opt
*item_non_opt
=
2152 (const struct argpar_item_non_opt
*)
2155 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2159 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2161 ingested_args
= argpar_state_get_ingested_orig_args(
2164 my_argc
-= ingested_args
;
2165 my_argv
+= ingested_args
;
2167 switch (item_opt
->descr
->id
) {
2172 case OPT_LIST_OPTIONS
:
2173 list_cmd_options_argpar(stdout
, add_trigger_options
);
2179 ERR("A --condition was already given.");
2183 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2186 * An error message was already printed by
2196 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2199 * An error message was already printed by
2205 ret
= lttng_dynamic_pointer_array_add_pointer(
2208 ERR("Failed to add pointer to pointer array.");
2212 /* Ownership of the action was transferred to the group. */
2219 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2227 if (!assign_string(&owner_uid
, item_opt
->arg
,
2240 ERR("Missing --condition.");
2244 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2245 ERR("Need at least one --action.");
2249 action_group
= lttng_action_group_create();
2250 if (!action_group
) {
2254 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2255 enum lttng_action_status status
;
2257 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2259 status
= lttng_action_group_add_action(action_group
, action
);
2260 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2265 * The `lttng_action_group_add_action()` takes a reference to
2266 * the action. We can destroy ours.
2268 lttng_action_destroy(action
);
2272 trigger
= lttng_trigger_create(condition
, action_group
);
2278 enum lttng_trigger_status trigger_status
=
2279 lttng_trigger_set_name(trigger
, name
);
2281 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2282 ERR("Failed to set trigger name.");
2288 enum lttng_trigger_status trigger_status
;
2293 uid
= strtol(owner_uid
, &end
, 10);
2294 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2295 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2298 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2299 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2300 ERR("Failed to set trigger's user identity.");
2305 ret
= lttng_register_trigger(trigger
);
2307 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2311 MSG("Trigger registered successfully.");
2319 argpar_state_destroy(argpar_state
);
2320 argpar_item_destroy(argpar_item
);
2321 lttng_dynamic_pointer_array_reset(&actions
);
2322 lttng_condition_destroy(condition
);
2323 lttng_action_destroy(action_group
);
2324 lttng_action_destroy(action
);
2325 lttng_trigger_destroy(trigger
);