2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "../command.hpp"
9 #include "../loglevel.hpp"
10 #include "../uprobe.hpp"
11 #include "common/argpar-utils/argpar-utils.hpp"
12 #include "common/argpar/argpar.h"
13 #include "common/dynamic-array.hpp"
14 #include "common/mi-lttng.hpp"
15 #include "common/string-utils/string-utils.hpp"
16 #include "common/utils.hpp"
18 #include <lttng/domain-internal.hpp>
24 /* For lttng_event_rule_type_str(). */
25 #include "common/dynamic-array.hpp"
26 #include "common/filter/filter-ast.hpp"
27 #include "common/filter/filter-ir.hpp"
29 #include <lttng/event-rule/event-rule-internal.hpp>
30 #include <lttng/lttng.h>
32 #if (LTTNG_SYMBOL_NAME_LEN == 256)
33 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
36 #ifdef LTTNG_EMBED_HELP
37 static const char help_msg
[] =
38 #include <lttng-add-trigger.1.h>
70 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
71 { OPT_FILTER
, 'f', "filter", true },
72 { OPT_NAME
, 'n', "name", true },
73 { OPT_EXCLUDE_NAME
, 'x', "exclude-name", true },
74 { OPT_LOG_LEVEL
, 'l', "log-level", true },
75 { OPT_EVENT_NAME
, 'E', "event-name", true },
77 { OPT_TYPE
, 't', "type", true },
78 { OPT_LOCATION
, 'L', "location", true },
80 /* Capture descriptor */
81 { OPT_CAPTURE
, '\0', "capture", true },
83 ARGPAR_OPT_DESCR_SENTINEL
86 static bool has_syscall_prefix(const char *arg
)
89 const char kernel_syscall_type_opt_prefix
[] = "kernel:syscall";
90 const size_t kernel_syscall_type_opt_prefix_len
=
91 sizeof(kernel_syscall_type_opt_prefix
) - 1;
92 const char syscall_type_opt_prefix
[] = "syscall";
93 const size_t syscall_type_opt_prefix_len
= sizeof(syscall_type_opt_prefix
) - 1;
95 if (strncmp(arg
, syscall_type_opt_prefix
, syscall_type_opt_prefix_len
) == 0) {
97 } else if (strncmp(arg
,
98 kernel_syscall_type_opt_prefix
,
99 kernel_syscall_type_opt_prefix_len
) == 0) {
108 static bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
112 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
113 ERR("More than one `--type` was specified.");
117 if (strcmp(arg
, "user") == 0 || strcmp(arg
, "user:tracepoint") == 0) {
118 *dest
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
119 } else if (strcmp(arg
, "kernel") == 0 || strcmp(arg
, "kernel:tracepoint") == 0) {
120 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
;
121 } else if (strcmp(arg
, "jul") == 0 || strcmp(arg
, "jul:logging") == 0) {
122 *dest
= LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
;
123 } else if (strcmp(arg
, "log4j") == 0 || strcmp(arg
, "log4j:logging") == 0) {
124 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
;
125 } else if (strcmp(arg
, "python") == 0 || strcmp(arg
, "python:logging") == 0) {
126 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
127 } else if (strcmp(arg
, "kprobe") == 0 || strcmp(arg
, "kernel:kprobe") == 0) {
128 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
129 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
130 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
131 } else if (has_syscall_prefix(arg
)) {
133 * Matches the following:
137 * - syscall:entry+exit
140 * - kernel:syscall:entry
141 * - kernel:syscall:exit
142 * - kernel:syscall:entry+exit
145 * Validation for the right side is left to further usage sites.
147 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
149 ERR("Invalid `--type` value: %s", arg
);
163 static bool assign_string(char **dest
, const char *src
, const char *opt_name
)
168 ERR("Duplicate '%s' given.", opt_name
);
174 PERROR("Failed to allocate string '%s'.", opt_name
);
189 parse_syscall_emission_site_from_type(const char *str
,
190 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
193 const char kernel_prefix
[] = "kernel:";
194 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
197 * If the passed string is of the form "kernel:syscall*", move the
198 * pointer passed "kernel:".
200 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
201 str
= &str
[kernel_prefix_len
];
204 if (strcmp(str
, "syscall") == 0 || strcmp(str
, "syscall:entry+exit") == 0) {
205 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
206 } else if (strcmp(str
, "syscall:entry") == 0) {
207 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
208 } else if (strcmp(str
, "syscall:exit") == 0) {
209 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
221 * Parse `str` as a log level against the passed event rule type.
223 * Return the log level in `*log_level`. Return true in `*log_level_only` if
224 * the string specifies exactly this log level, false if it specifies at least
227 * Return true if the string was successfully parsed as a log level string.
229 static bool parse_log_level_string(const char *str
,
230 enum lttng_event_rule_type event_rule_type
,
232 bool *log_level_only
)
236 switch (event_rule_type
) {
237 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
239 enum lttng_loglevel log_level_min
, log_level_max
;
240 if (!loglevel_parse_range_string(str
, &log_level_min
, &log_level_max
)) {
244 /* Only support VAL and VAL.. for now. */
245 if (log_level_min
!= log_level_max
&& log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
249 *log_level
= (int) log_level_min
;
250 *log_level_only
= log_level_min
== log_level_max
;
253 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
255 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
256 if (!loglevel_log4j_parse_range_string(str
, &log_level_min
, &log_level_max
)) {
260 /* Only support VAL and VAL.. for now. */
261 if (log_level_min
!= log_level_max
&& log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
265 *log_level
= (int) log_level_min
;
266 *log_level_only
= log_level_min
== log_level_max
;
269 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
271 enum lttng_loglevel_jul log_level_min
, log_level_max
;
272 if (!loglevel_jul_parse_range_string(str
, &log_level_min
, &log_level_max
)) {
276 /* Only support VAL and VAL.. for now. */
277 if (log_level_min
!= log_level_max
&& log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
281 *log_level
= (int) log_level_min
;
282 *log_level_only
= log_level_min
== log_level_max
;
285 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
287 enum lttng_loglevel_python log_level_min
, log_level_max
;
288 if (!loglevel_python_parse_range_string(str
, &log_level_min
, &log_level_max
)) {
292 /* Only support VAL and VAL.. for now. */
293 if (log_level_min
!= log_level_max
&&
294 log_level_max
!= LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
298 *log_level
= (int) log_level_min
;
299 *log_level_only
= log_level_min
== log_level_max
;
303 /* Invalid domain type. */
317 static int parse_kernel_probe_opts(const char *source
,
318 struct lttng_kernel_probe_location
**location
)
323 char name
[LTTNG_SYMBOL_NAME_LEN
];
324 char *symbol_name
= nullptr;
327 /* Check for symbol+offset. */
329 source
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"[^'+']+%18s", name
, s_hex
);
331 if (*s_hex
== '\0') {
332 ERR("Kernel probe symbol offset is missing.");
336 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
338 PERROR("Failed to copy kernel probe location symbol name.");
341 offset
= strtoull(s_hex
, nullptr, 0);
343 *location
= lttng_kernel_probe_location_symbol_create(symbol_name
, offset
);
345 ERR("Failed to create symbol kernel probe location.");
352 /* Check for symbol. */
353 if (isalpha(name
[0]) || name
[0] == '_') {
354 match
= sscanf(source
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s", name
);
356 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
358 ERR("Failed to copy kernel probe location symbol name.");
362 *location
= lttng_kernel_probe_location_symbol_create(symbol_name
, 0);
364 ERR("Failed to create symbol kernel probe location.");
372 /* Check for address. */
373 match
= sscanf(source
, "%18s", s_hex
);
377 if (*s_hex
== '\0') {
378 ERR("Invalid kernel probe location address.");
382 address
= strtoull(s_hex
, nullptr, 0);
383 *location
= lttng_kernel_probe_location_address_create(address
);
385 ERR("Failed to create symbol kernel probe location.");
402 static struct lttng_event_expr
*
403 ir_op_load_expr_to_event_expr(const struct ir_load_expression
*load_expr
, const char *capture_str
)
405 char *provider_name
= nullptr;
406 struct lttng_event_expr
*event_expr
= nullptr;
407 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
408 const enum ir_load_expression_type load_expr_child_type
= load_expr_op
->type
;
410 switch (load_expr_child_type
) {
411 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
412 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
414 const char *field_name
;
416 load_expr_op
= load_expr_op
->next
;
417 LTTNG_ASSERT(load_expr_op
);
418 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
419 field_name
= load_expr_op
->u
.symbol
;
420 LTTNG_ASSERT(field_name
);
422 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
423 lttng_event_expr_event_payload_field_create(field_name
) :
424 lttng_event_expr_channel_context_field_create(field_name
);
426 ERR("Failed to create %s event expression: field name = `%s`.",
427 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
436 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
439 const char *type_name
;
440 const char *field_name
;
442 load_expr_op
= load_expr_op
->next
;
443 LTTNG_ASSERT(load_expr_op
);
444 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
445 field_name
= load_expr_op
->u
.symbol
;
446 LTTNG_ASSERT(field_name
);
449 * The field name needs to be of the form PROVIDER:TYPE. We
452 colon
= strchr(field_name
, ':');
454 ERR("Invalid app-specific context field name: missing colon in `%s`.",
459 type_name
= colon
+ 1;
460 if (*type_name
== '\0') {
461 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
466 provider_name
= strndup(field_name
, colon
- field_name
);
467 if (!provider_name
) {
468 PERROR("Failed to allocate field name string");
472 event_expr
= lttng_event_expr_app_specific_context_field_create(provider_name
,
475 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
484 ERR("%s: unexpected load expr type %d.", __func__
, load_expr_op
->type
);
488 load_expr_op
= load_expr_op
->next
;
490 /* There may be a single array index after that. */
491 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
492 struct lttng_event_expr
*index_event_expr
;
493 const uint64_t index
= load_expr_op
->u
.index
;
495 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
496 if (!index_event_expr
) {
497 ERR("Failed to create array field element event expression.");
501 event_expr
= index_event_expr
;
502 load_expr_op
= load_expr_op
->next
;
505 switch (load_expr_op
->type
) {
506 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
508 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
509 * always found at the end of the chain.
512 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
513 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
518 ERR("%s: unexpected load expression operator %s.",
520 ir_load_expression_type_str(load_expr_op
->type
));
527 lttng_event_expr_destroy(event_expr
);
528 event_expr
= nullptr;
536 static struct lttng_event_expr
*ir_op_load_to_event_expr(const struct ir_op
*ir
,
537 const char *capture_str
)
539 struct lttng_event_expr
*event_expr
= nullptr;
541 LTTNG_ASSERT(ir
->op
== IR_OP_LOAD
);
543 switch (ir
->data_type
) {
544 case IR_DATA_EXPRESSION
:
546 const struct ir_load_expression
*ir_load_expr
= ir
->u
.load
.u
.expression
;
548 event_expr
= ir_op_load_expr_to_event_expr(ir_load_expr
, capture_str
);
552 ERR("%s: unexpected data type: %s.", __func__
, ir_data_type_str(ir
->data_type
));
559 static const char *ir_operator_type_human_str(enum ir_op_type op
)
580 static struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
581 const char *capture_str
)
583 struct lttng_event_expr
*event_expr
= nullptr;
585 LTTNG_ASSERT(ir
->op
== IR_OP_ROOT
);
586 ir
= ir
->u
.root
.child
;
590 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
595 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
597 ir_operator_type_human_str(ir
->op
));
600 ERR("%s: unexpected IR op type: %s.", __func__
, ir_op_type_str(ir
->op
));
607 static void destroy_event_expr(void *ptr
)
609 lttng_event_expr_destroy((lttng_event_expr
*) ptr
);
613 struct parse_event_rule_res
{
615 struct lttng_event_rule
*er
;
617 /* Array of `struct lttng_event_expr *` */
618 struct lttng_dynamic_pointer_array capture_descriptors
;
622 static struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
, int argc_offset
)
624 enum lttng_event_rule_type event_rule_type
= LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
625 struct argpar_iter
*argpar_iter
= nullptr;
626 const struct argpar_item
*argpar_item
= nullptr;
627 int consumed_args
= -1;
628 struct lttng_kernel_probe_location
*kernel_probe_location
= nullptr;
629 struct lttng_userspace_probe_location
*userspace_probe_location
= nullptr;
630 struct parse_event_rule_res res
= {};
631 struct lttng_event_expr
*event_expr
= nullptr;
632 struct filter_parser_ctx
*parser_ctx
= nullptr;
633 struct lttng_log_level_rule
*log_level_rule
= nullptr;
635 /* Event rule type option */
636 char *event_rule_type_str
= nullptr;
638 /* Tracepoint and syscall options. */
639 char *name
= nullptr;
640 /* Array of strings. */
641 struct lttng_dynamic_pointer_array exclude_names
;
643 /* For userspace / kernel probe and function. */
644 char *location
= nullptr;
645 char *event_name
= nullptr;
648 char *filter
= nullptr;
651 char *log_level_str
= nullptr;
653 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
, destroy_event_expr
);
655 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
657 argpar_iter
= argpar_iter_create(*argc
, *argv
, event_rule_opt_descrs
);
659 ERR("Failed to allocate an argpar iter.");
664 enum parse_next_item_status status
;
666 status
= parse_next_item(
667 argpar_iter
, &argpar_item
, argc_offset
, *argv
, false, nullptr, nullptr);
668 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
669 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
671 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
675 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
677 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
678 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
679 const char *arg
= argpar_item_opt_arg(argpar_item
);
683 if (!assign_event_rule_type(&event_rule_type
, arg
)) {
687 /* Save the string for later use. */
688 if (!assign_string(&event_rule_type_str
, arg
, "--type/-t")) {
694 if (!assign_string(&location
, arg
, "--location/-L")) {
700 if (!assign_string(&event_name
, arg
, "--event-name/-E")) {
706 if (!assign_string(&filter
, arg
, "--filter/-f")) {
712 if (!assign_string(&name
, arg
, "--name/-n")) {
717 case OPT_EXCLUDE_NAME
:
721 ret
= lttng_dynamic_pointer_array_add_pointer(&exclude_names
,
724 ERR("Failed to add pointer to dynamic pointer array.");
731 if (!assign_string(&log_level_str
, arg
, "--log-level/-l")) {
740 ret
= filter_parser_ctx_create_from_filter_expression(arg
,
743 ERR("Failed to parse capture expression `%s`.", arg
);
747 event_expr
= ir_op_root_to_event_expr(parser_ctx
->ir_root
, arg
);
748 filter_parser_ctx_free(parser_ctx
);
749 parser_ctx
= nullptr;
752 * ir_op_root_to_event_expr has printed
758 ret
= lttng_dynamic_pointer_array_add_pointer(
759 &res
.capture_descriptors
, event_expr
);
765 * The ownership of event expression was
766 * transferred to the dynamic array.
768 event_expr
= nullptr;
776 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
778 /* Don't accept non-option arguments. */
779 ERR("Unexpected argument '%s'", arg
);
784 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
785 ERR("Event rule requires a --type.");
790 * Option --name is applicable to event rules of type kernel, user, jul,
791 * log4j,python and syscall. If --name is omitted, it is implicitly
794 switch (event_rule_type
) {
795 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
796 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
797 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
798 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
799 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
800 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
808 ERR("Can't use --name with %s event rules.",
809 lttng_event_rule_type_str(event_rule_type
));
813 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
814 ERR("Can't use --exclude-name/-x with %s event rules.",
815 lttng_event_rule_type_str(event_rule_type
));
821 * Option --location is only applicable to (and mandatory for) event
822 * rules of type {k,u}probe and function.
824 * Option --event-name is only applicable to event rules of type probe.
825 * If omitted, it defaults to the location.
827 switch (event_rule_type
) {
828 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
829 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
831 ERR("Event rule of type %s requires a --location.",
832 lttng_event_rule_type_str(event_rule_type
));
837 event_name
= strdup(location
);
844 ERR("Can't use --location with %s event rules.",
845 lttng_event_rule_type_str(event_rule_type
));
850 ERR("Can't use --event-name with %s event rules.",
851 lttng_event_rule_type_str(event_rule_type
));
857 * Update *argc and *argv so our caller can keep parsing what follows.
859 consumed_args
= argpar_iter_ingested_orig_args(argpar_iter
);
860 LTTNG_ASSERT(consumed_args
>= 0);
861 *argc
-= consumed_args
;
862 *argv
+= consumed_args
;
865 * Adding a filter to a probe, function or userspace-probe would be
866 * denied by the kernel tracer as it's not supported at the moment. We
867 * do an early check here to warn the user.
870 switch (event_rule_type
) {
871 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
872 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
873 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
874 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
875 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
876 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
879 ERR("Filter expressions are not supported for %s event rules.",
880 lttng_event_rule_type_str(event_rule_type
));
886 * If --exclude-name/-x was passed, split it into an exclusion list.
887 * Exclusions are only supported by
888 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
890 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
891 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
892 ERR("Event name exclusions are not yet implemented for %s event rules.",
893 lttng_event_rule_type_str(event_rule_type
));
897 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
899 * Assume validate_exclusion_list already prints an
907 switch (event_rule_type
) {
908 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
909 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
910 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
911 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
916 if (strcmp(log_level_str
, "..") == 0) {
918 * ".." is the same as passing no log level
919 * option and correspond to the "ANY" case.
924 if (!parse_log_level_string(
925 log_level_str
, event_rule_type
, &log_level
, &log_level_only
)) {
926 ERR("Failed to parse log level string `%s`.", log_level_str
);
930 if (log_level_only
) {
931 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
933 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(
937 if (log_level_rule
== nullptr) {
938 ERR("Failed to create log level rule object.");
944 ERR("Log levels are not supported for %s event rules.",
945 lttng_event_rule_type_str(event_rule_type
));
950 /* Finally, create the event rule object. */
951 switch (event_rule_type
) {
952 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
954 enum lttng_event_rule_status event_rule_status
;
956 res
.er
= lttng_event_rule_user_tracepoint_create();
958 ERR("Failed to create user_tracepoint event rule.");
963 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(res
.er
, name
);
964 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
965 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.", name
);
972 lttng_event_rule_user_tracepoint_set_filter(res
.er
, filter
);
973 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
974 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
980 /* Set exclusion list. */
981 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
983 int count
= lttng_dynamic_pointer_array_get_count(&exclude_names
);
985 for (n
= 0; n
< count
; n
++) {
986 const char *exclude_name
=
987 (const char *) lttng_dynamic_pointer_array_get_pointer(
991 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
992 res
.er
, exclude_name
);
993 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
994 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1001 if (log_level_rule
) {
1002 event_rule_status
= lttng_event_rule_user_tracepoint_set_log_level_rule(
1003 res
.er
, log_level_rule
);
1005 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1006 ERR("Failed to set log level on event fule.");
1013 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1015 enum lttng_event_rule_status event_rule_status
;
1017 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1019 ERR("Failed to create kernel_tracepoint event rule.");
1025 lttng_event_rule_kernel_tracepoint_set_name_pattern(res
.er
, name
);
1026 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1027 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.", name
);
1034 lttng_event_rule_kernel_tracepoint_set_filter(res
.er
, filter
);
1035 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1036 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1043 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1045 enum lttng_event_rule_status event_rule_status
;
1047 res
.er
= lttng_event_rule_jul_logging_create();
1049 ERR("Failed to create jul_logging event rule.");
1054 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(res
.er
, name
);
1055 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1056 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1062 event_rule_status
= lttng_event_rule_jul_logging_set_filter(res
.er
, filter
);
1063 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1064 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1070 if (log_level_rule
) {
1071 event_rule_status
= lttng_event_rule_jul_logging_set_log_level_rule(
1072 res
.er
, log_level_rule
);
1074 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1075 ERR("Failed to set log level on event fule.");
1081 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1083 enum lttng_event_rule_status event_rule_status
;
1085 res
.er
= lttng_event_rule_log4j_logging_create();
1087 ERR("Failed to create jul_logging event rule.");
1092 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(res
.er
, name
);
1093 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1094 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1101 lttng_event_rule_log4j_logging_set_filter(res
.er
, filter
);
1102 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1103 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1109 if (log_level_rule
) {
1110 event_rule_status
= lttng_event_rule_log4j_logging_set_log_level_rule(
1111 res
.er
, log_level_rule
);
1113 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1114 ERR("Failed to set log level on event fule.");
1120 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1122 enum lttng_event_rule_status event_rule_status
;
1124 res
.er
= lttng_event_rule_python_logging_create();
1126 ERR("Failed to create jul_logging event rule.");
1131 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(res
.er
, name
);
1132 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1133 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1140 lttng_event_rule_python_logging_set_filter(res
.er
, filter
);
1141 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1142 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1148 if (log_level_rule
) {
1149 event_rule_status
= lttng_event_rule_python_logging_set_log_level_rule(
1150 res
.er
, log_level_rule
);
1152 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1153 ERR("Failed to set log level on event fule.");
1159 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1162 enum lttng_event_rule_status event_rule_status
;
1164 ret
= parse_kernel_probe_opts(location
, &kernel_probe_location
);
1166 ERR("Failed to parse kernel probe location.");
1170 LTTNG_ASSERT(kernel_probe_location
);
1171 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1173 ERR("Failed to create kprobe event rule.");
1178 lttng_event_rule_kernel_kprobe_set_event_name(res
.er
, event_name
);
1179 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1180 ERR("Failed to set kprobe event rule's name to '%s'.", event_name
);
1186 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1189 enum lttng_event_rule_status event_rule_status
;
1191 ret
= parse_userspace_probe_opts(location
, &userspace_probe_location
);
1193 ERR("Failed to parse user space probe location.");
1197 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1199 ERR("Failed to create userspace probe event rule.");
1204 lttng_event_rule_kernel_uprobe_set_event_name(res
.er
, event_name
);
1205 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1206 ERR("Failed to set user space probe event rule's name to '%s'.",
1213 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1215 enum lttng_event_rule_status event_rule_status
;
1216 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1218 if (!parse_syscall_emission_site_from_type(event_rule_type_str
, &emission_site
)) {
1219 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1223 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1225 ERR("Failed to create syscall event rule.");
1229 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(res
.er
, name
);
1230 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1231 ERR("Failed to set syscall event rule's pattern to '%s'.", name
);
1237 lttng_event_rule_kernel_syscall_set_filter(res
.er
, filter
);
1238 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1239 ERR("Failed to set syscall event rule's filter to '%s'.", filter
);
1254 lttng_event_rule_destroy(res
.er
);
1256 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1260 filter_parser_ctx_free(parser_ctx
);
1263 lttng_event_expr_destroy(event_expr
);
1264 argpar_item_destroy(argpar_item
);
1265 argpar_iter_destroy(argpar_iter
);
1268 lttng_dynamic_pointer_array_reset(&exclude_names
);
1269 free(log_level_str
);
1272 free(event_rule_type_str
);
1274 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1275 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1276 lttng_log_level_rule_destroy(log_level_rule
);
1280 static struct lttng_condition
*
1281 handle_condition_event(int *argc
, const char ***argv
, int argc_offset
)
1283 struct parse_event_rule_res res
;
1284 struct lttng_condition
*c
;
1287 res
= parse_event_rule(argc
, argv
, argc_offset
);
1293 c
= lttng_condition_event_rule_matches_create(res
.er
);
1294 lttng_event_rule_destroy(res
.er
);
1300 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
); i
++) {
1301 enum lttng_condition_status status
;
1302 struct lttng_event_expr
**expr
=
1303 (lttng_event_expr
**) lttng_dynamic_array_get_element(
1304 &res
.capture_descriptors
.array
, i
);
1307 LTTNG_ASSERT(*expr
);
1308 status
= lttng_condition_event_rule_matches_append_capture_descriptor(c
, *expr
);
1309 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1310 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1311 ERR("The capture feature is unsupported by the event-rule condition type");
1317 /* Ownership of event expression moved to `c` */
1324 lttng_condition_destroy(c
);
1328 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1329 lttng_event_rule_destroy(res
.er
);
1334 struct condition_descr
{
1336 struct lttng_condition
*(*handler
)(int *argc
, const char ***argv
, int argc_offset
);
1340 static const struct condition_descr condition_descrs
[] = {
1341 { "event-rule-matches", handle_condition_event
},
1344 static void print_valid_condition_names()
1348 ERR("Valid condition names are:");
1350 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
1351 ERR(" %s", condition_descrs
[i
].name
);
1355 static struct lttng_condition
*parse_condition(const char *condition_name
,
1360 const char *orig_arg
)
1363 struct lttng_condition
*cond
;
1364 const struct condition_descr
*descr
= nullptr;
1366 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1367 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1368 descr
= &condition_descrs
[i
];
1374 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown condition name '%s'",
1378 print_valid_condition_names();
1382 cond
= descr
->handler(argc
, argv
, argc_offset
);
1384 /* The handler has already printed an error message. */
1395 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1398 size_t num_token
= 0;
1399 struct lttng_dynamic_pointer_array tokens
;
1400 struct lttng_rate_policy
*policy
= nullptr;
1401 enum lttng_rate_policy_type policy_type
;
1402 unsigned long long value
;
1403 char *policy_type_str
;
1404 char *policy_value_str
;
1406 LTTNG_ASSERT(policy_str
);
1407 lttng_dynamic_pointer_array_init(&tokens
, nullptr);
1409 /* Rate policy fields are separated by ':'. */
1410 ret
= strutils_split(policy_str
, ':', true, &tokens
);
1412 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1416 * Early sanity check that the number of parameter is exactly 2.
1419 if (num_token
!= 2) {
1420 ERR("Rate policy format is invalid.");
1424 policy_type_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1425 policy_value_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1427 /* Parse the type. */
1428 if (strcmp(policy_type_str
, "once-after") == 0) {
1429 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1430 } else if (strcmp(policy_type_str
, "every") == 0) {
1431 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1433 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1437 /* Parse the value. */
1438 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1439 ERR("Failed to parse rate policy value `%s` as an integer.", policy_value_str
);
1444 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1448 switch (policy_type
) {
1449 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1450 policy
= lttng_rate_policy_every_n_create(value
);
1452 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1453 policy
= lttng_rate_policy_once_after_n_create(value
);
1459 if (policy
== nullptr) {
1460 ERR("Failed to create rate policy `%s`.", policy_str
);
1464 lttng_dynamic_pointer_array_reset(&tokens
);
1468 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1469 { OPT_RATE_POLICY
, '\0', "rate-policy", true }, ARGPAR_OPT_DESCR_SENTINEL
1472 static struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
, int argc_offset
)
1474 struct lttng_action
*action
= nullptr;
1475 struct argpar_iter
*argpar_iter
= nullptr;
1476 const struct argpar_item
*argpar_item
= nullptr;
1477 struct lttng_rate_policy
*policy
= nullptr;
1479 argpar_iter
= argpar_iter_create(*argc
, *argv
, notify_action_opt_descrs
);
1481 ERR("Failed to allocate an argpar iter.");
1486 enum parse_next_item_status status
;
1488 status
= parse_next_item(argpar_iter
,
1494 "While parsing `notify` action:");
1495 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1496 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1498 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1502 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1504 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1505 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1506 const char *arg
= argpar_item_opt_arg(argpar_item
);
1508 switch (descr
->id
) {
1509 case OPT_RATE_POLICY
:
1511 policy
= parse_rate_policy(arg
);
1521 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1523 ERR("Unexpected argument `%s`.", arg
);
1528 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1529 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1531 action
= lttng_action_notify_create();
1533 ERR("Failed to create notify action");
1538 enum lttng_action_status status
;
1539 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1540 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1541 ERR("Failed to set rate policy");
1549 lttng_action_destroy(action
);
1552 lttng_rate_policy_destroy(policy
);
1553 argpar_item_destroy(argpar_item
);
1554 argpar_iter_destroy(argpar_iter
);
1559 * Generic handler for a kind of action that takes a session name and an
1560 * optional rate policy.
1563 static struct lttng_action
*handle_action_simple_session_with_policy(
1567 struct lttng_action
*(*create_action_cb
)(),
1568 enum lttng_action_status (*set_session_name_cb
)(struct lttng_action
*, const char *),
1569 enum lttng_action_status (*set_rate_policy_cb
)(struct lttng_action
*,
1570 const struct lttng_rate_policy
*),
1571 const char *action_name
)
1573 struct lttng_action
*action
= nullptr;
1574 struct argpar_iter
*argpar_iter
= nullptr;
1575 const struct argpar_item
*argpar_item
= nullptr;
1576 const char *session_name_arg
= nullptr;
1577 enum lttng_action_status action_status
;
1578 struct lttng_rate_policy
*policy
= nullptr;
1580 LTTNG_ASSERT(set_session_name_cb
);
1581 LTTNG_ASSERT(set_rate_policy_cb
);
1583 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1584 { OPT_RATE_POLICY
, '\0', "rate-policy", true }, ARGPAR_OPT_DESCR_SENTINEL
1587 argpar_iter
= argpar_iter_create(*argc
, *argv
, rate_policy_opt_descrs
);
1589 ERR("Failed to allocate an argpar iter.");
1594 enum parse_next_item_status status
;
1596 status
= parse_next_item(argpar_iter
,
1602 "While parsing `%s` action:",
1604 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1605 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1607 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1611 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1613 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1614 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1615 const char *arg
= argpar_item_opt_arg(argpar_item
);
1617 switch (descr
->id
) {
1618 case OPT_RATE_POLICY
:
1620 policy
= parse_rate_policy(arg
);
1630 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1631 unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1635 session_name_arg
= arg
;
1638 ERR("Unexpected argument `%s`.", arg
);
1644 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1645 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1647 if (!session_name_arg
) {
1648 ERR("Missing session name.");
1652 action
= create_action_cb();
1654 ERR("Failed to allocate %s session action.", action_name
);
1658 action_status
= set_session_name_cb(action
, session_name_arg
);
1659 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1660 ERR("Failed to set action %s session's session name to '%s'.",
1667 action_status
= set_rate_policy_cb(action
, policy
);
1668 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1669 ERR("Failed to set rate policy");
1677 lttng_action_destroy(action
);
1681 lttng_rate_policy_destroy(policy
);
1682 argpar_item_destroy(argpar_item
);
1683 argpar_iter_destroy(argpar_iter
);
1687 static struct lttng_action
*
1688 handle_action_start_session(int *argc
, const char ***argv
, int argc_offset
)
1690 return handle_action_simple_session_with_policy(argc
,
1693 lttng_action_start_session_create
,
1694 lttng_action_start_session_set_session_name
,
1695 lttng_action_start_session_set_rate_policy
,
1699 static struct lttng_action
*
1700 handle_action_stop_session(int *argc
, const char ***argv
, int argc_offset
)
1702 return handle_action_simple_session_with_policy(argc
,
1705 lttng_action_stop_session_create
,
1706 lttng_action_stop_session_set_session_name
,
1707 lttng_action_stop_session_set_rate_policy
,
1711 static struct lttng_action
*
1712 handle_action_rotate_session(int *argc
, const char ***argv
, int argc_offset
)
1714 return handle_action_simple_session_with_policy(
1718 lttng_action_rotate_session_create
,
1719 lttng_action_rotate_session_set_session_name
,
1720 lttng_action_rotate_session_set_rate_policy
,
1724 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1725 { OPT_NAME
, 'n', "name", true },
1726 { OPT_MAX_SIZE
, 'm', "max-size", true },
1727 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1728 { OPT_DATA_URL
, '\0', "data-url", true },
1729 { OPT_URL
, '\0', "url", true },
1730 { OPT_PATH
, '\0', "path", true },
1731 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1732 ARGPAR_OPT_DESCR_SENTINEL
1735 static struct lttng_action
*
1736 handle_action_snapshot_session(int *argc
, const char ***argv
, int argc_offset
)
1738 struct lttng_action
*action
= nullptr;
1739 struct argpar_iter
*argpar_iter
= nullptr;
1740 const struct argpar_item
*argpar_item
= nullptr;
1741 const char *session_name_arg
= nullptr;
1742 char *snapshot_name_arg
= nullptr;
1743 char *ctrl_url_arg
= nullptr;
1744 char *data_url_arg
= nullptr;
1745 char *max_size_arg
= nullptr;
1746 char *url_arg
= nullptr;
1747 char *path_arg
= nullptr;
1748 char *error
= nullptr;
1749 enum lttng_action_status action_status
;
1750 struct lttng_snapshot_output
*snapshot_output
= nullptr;
1751 struct lttng_rate_policy
*policy
= nullptr;
1753 unsigned int locations_specified
= 0;
1755 argpar_iter
= argpar_iter_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1757 ERR("Failed to allocate an argpar iter.");
1762 enum parse_next_item_status status
;
1764 status
= parse_next_item(argpar_iter
,
1770 "While parsing `snapshot` action:");
1771 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1772 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1774 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1778 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1780 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1781 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1782 const char *arg
= argpar_item_opt_arg(argpar_item
);
1784 switch (descr
->id
) {
1786 if (!assign_string(&snapshot_name_arg
, arg
, "--name/-n")) {
1792 if (!assign_string(&max_size_arg
, arg
, "--max-size/-m")) {
1798 if (!assign_string(&ctrl_url_arg
, arg
, "--ctrl-url")) {
1804 if (!assign_string(&data_url_arg
, arg
, "--data-url")) {
1810 if (!assign_string(&url_arg
, arg
, "--url")) {
1816 if (!assign_string(&path_arg
, arg
, "--path")) {
1821 case OPT_RATE_POLICY
:
1823 policy
= parse_rate_policy(arg
);
1833 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1834 const unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1838 session_name_arg
= arg
;
1841 ERR("Unexpected argument `%s`.", arg
);
1847 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1848 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
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(action
, session_name_arg
);
1895 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1896 ERR("Failed to set action snapshot session's session name to '%s'.",
1901 if (snapshot_name_arg
) {
1902 if (!snapshot_output
) {
1903 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1907 ret
= lttng_snapshot_output_set_name(snapshot_name_arg
, snapshot_output
);
1909 ERR("Failed to set name of snapshot output.");
1917 if (!snapshot_output
) {
1918 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1922 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1924 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1928 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1930 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1938 struct lttng_uri
*uris
;
1940 if (!strstr(url_arg
, "://")) {
1941 ERR("Failed to parse '%s' as an URL.", url_arg
);
1945 num_uris
= uri_parse_str_urls(url_arg
, nullptr, &uris
);
1947 ERR("Failed to parse '%s' as an URL.", url_arg
);
1951 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1952 ret
= lttng_snapshot_output_set_local_path(uris
[0].dst
.path
,
1956 ERR("Failed to assign '%s' as a local destination.", url_arg
);
1960 ret
= lttng_snapshot_output_set_network_url(url_arg
, snapshot_output
);
1963 ERR("Failed to assign '%s' as a network URL.", url_arg
);
1970 ret
= lttng_snapshot_output_set_local_path(path_arg
, snapshot_output
);
1972 ERR("Failed to parse '%s' as a local path.", path_arg
);
1979 * Two argument form, network output with separate control and
1982 ret
= lttng_snapshot_output_set_network_urls(
1983 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1985 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1992 if (snapshot_output
) {
1993 action_status
= lttng_action_snapshot_session_set_output(action
, snapshot_output
);
1994 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1995 ERR("Failed to set snapshot session action's output.");
1999 /* Ownership of `snapshot_output` has been transferred to the action. */
2000 snapshot_output
= nullptr;
2004 enum lttng_action_status status
;
2005 status
= lttng_action_snapshot_session_set_rate_policy(action
, policy
);
2006 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2007 ERR("Failed to set rate policy");
2015 lttng_action_destroy(action
);
2019 free(snapshot_name_arg
);
2024 free(snapshot_output
);
2026 lttng_rate_policy_destroy(policy
);
2027 argpar_item_destroy(argpar_item
);
2028 argpar_iter_destroy(argpar_iter
);
2033 struct action_descr
{
2035 struct lttng_action
*(*handler
)(int *argc
, const char ***argv
, int argc_offset
);
2039 static const struct action_descr action_descrs
[] = {
2040 { "notify", handle_action_notify
},
2041 { "start-session", handle_action_start_session
},
2042 { "stop-session", handle_action_stop_session
},
2043 { "rotate-session", handle_action_rotate_session
},
2044 { "snapshot-session", handle_action_snapshot_session
},
2047 static void print_valid_action_names()
2051 ERR("Valid action names are:");
2053 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
2054 ERR(" %s", action_descrs
[i
].name
);
2058 static struct lttng_action
*parse_action(const char *action_name
,
2063 const char *orig_arg
)
2066 struct lttng_action
*action
;
2067 const struct action_descr
*descr
= nullptr;
2069 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2070 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2071 descr
= &action_descrs
[i
];
2077 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown action name '%s'",
2081 print_valid_action_names();
2085 action
= descr
->handler(argc
, argv
, argc_offset
);
2087 /* The handler has already printed an error message. */
2098 static const struct argpar_opt_descr add_trigger_options
[] = {
2099 { OPT_HELP
, 'h', "help", false },
2100 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2101 { OPT_CONDITION
, '\0', "condition", true },
2102 { OPT_ACTION
, '\0', "action", true },
2103 { OPT_NAME
, '\0', "name", true },
2104 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2105 ARGPAR_OPT_DESCR_SENTINEL
,
2108 static void lttng_actions_destructor(void *p
)
2110 struct lttng_action
*action
= (lttng_action
*) p
;
2112 lttng_action_destroy(action
);
2115 int cmd_add_trigger(int argc
, const char **argv
)
2118 int my_argc
= argc
- 1;
2119 const char **my_argv
= argv
+ 1;
2120 struct lttng_condition
*condition
= nullptr;
2121 struct lttng_dynamic_pointer_array actions
;
2122 struct argpar_iter
*argpar_iter
= nullptr;
2123 const struct argpar_item
*argpar_item
= nullptr;
2124 const struct argpar_error
*argpar_error
= nullptr;
2125 struct lttng_action
*action_list
= nullptr;
2126 struct lttng_action
*action
= nullptr;
2127 struct lttng_trigger
*trigger
= nullptr;
2128 char *name
= nullptr;
2130 char *owner_uid
= nullptr;
2131 enum lttng_error_code ret_code
;
2132 struct mi_writer
*mi_writer
= nullptr;
2134 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2137 mi_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
2143 /* Open command element. */
2144 ret
= mi_lttng_writer_command_open(mi_writer
, mi_lttng_element_command_add_trigger
);
2150 /* Open output element. */
2151 ret
= mi_lttng_writer_open_element(mi_writer
, mi_lttng_element_command_output
);
2159 enum parse_next_item_status status
;
2161 const struct argpar_opt_descr
*descr
;
2164 argpar_iter_destroy(argpar_iter
);
2165 argpar_iter
= argpar_iter_create(my_argc
, my_argv
, add_trigger_options
);
2167 ERR("Failed to create argpar iter.");
2171 status
= parse_next_item(argpar_iter
,
2178 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
2179 if (argpar_error_type(argpar_error
) == ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
) {
2180 int opt_id
= argpar_error_opt_descr(argpar_error
, nullptr)->id
;
2182 if (opt_id
== OPT_CONDITION
) {
2183 print_valid_condition_names();
2184 } else if (opt_id
== OPT_ACTION
) {
2185 print_valid_action_names();
2190 } else if (status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
2192 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
2196 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
2198 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_NON_OPT
) {
2199 ERR("Unexpected argument `%s`.", argpar_item_non_opt_arg(argpar_item
));
2203 ingested_args
= argpar_iter_ingested_orig_args(argpar_iter
);
2205 my_argc
-= ingested_args
;
2206 my_argv
+= ingested_args
;
2208 descr
= argpar_item_opt_descr(argpar_item
);
2209 arg
= argpar_item_opt_arg(argpar_item
);
2211 switch (descr
->id
) {
2216 case OPT_LIST_OPTIONS
:
2217 list_cmd_options_argpar(stdout
, add_trigger_options
);
2223 ERR("A --condition was already given.");
2227 condition
= parse_condition(arg
,
2231 argc
- my_argc
- ingested_args
,
2232 my_argv
[-ingested_args
]);
2235 * An error message was already printed by
2245 action
= parse_action(arg
,
2249 argc
- my_argc
- ingested_args
,
2250 my_argv
[-ingested_args
]);
2253 * An error message was already printed by
2259 ret
= lttng_dynamic_pointer_array_add_pointer(&actions
, action
);
2261 ERR("Failed to add pointer to pointer array.");
2265 /* Ownership of the action was transferred to the list. */
2272 if (!assign_string(&name
, arg
, "--name")) {
2280 if (!assign_string(&owner_uid
, arg
, "--owner-uid")) {
2292 ERR("Missing --condition.");
2296 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2297 ERR("Need at least one --action.");
2301 action_list
= lttng_action_list_create();
2306 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2307 enum lttng_action_status status
;
2309 action
= (lttng_action
*) lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2311 status
= lttng_action_list_add_action(action_list
, action
);
2312 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2317 * The `lttng_action_list_add_action()` takes a reference to
2318 * the action. We can destroy ours.
2320 lttng_action_destroy(action
);
2324 trigger
= lttng_trigger_create(condition
, action_list
);
2330 enum lttng_trigger_status trigger_status
;
2335 uid
= strtol(owner_uid
, &end
, 10);
2336 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2337 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2341 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2342 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2343 ERR("Failed to set trigger's user identity.");
2349 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2351 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2354 if (ret_code
!= LTTNG_OK
) {
2355 ERR("Failed to register trigger: %s.", lttng_strerror(-ret_code
));
2360 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, nullptr);
2361 if (ret_code
!= LTTNG_OK
) {
2365 const char *returned_trigger_name
;
2366 const enum lttng_trigger_status trigger_status
=
2367 lttng_trigger_get_name(trigger
, &returned_trigger_name
);
2369 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2370 WARN("Failed to retrieve the added trigger's name.");
2372 MSG("Added trigger `%s`.", returned_trigger_name
);
2385 if (lttng_opt_mi
&& mi_writer
) {
2388 /* Close output element. */
2389 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2395 mi_ret
= mi_lttng_writer_write_element_bool(
2396 mi_writer
, mi_lttng_element_command_success
, ret
? 0 : 1);
2402 /* Command element close. */
2403 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2411 argpar_error_destroy(argpar_error
);
2412 argpar_iter_destroy(argpar_iter
);
2413 argpar_item_destroy(argpar_item
);
2414 lttng_dynamic_pointer_array_reset(&actions
);
2415 lttng_condition_destroy(condition
);
2416 lttng_action_destroy(action_list
);
2417 lttng_action_destroy(action
);
2418 lttng_trigger_destroy(trigger
);
2421 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2422 /* Preserve original error code. */
2423 ret
= ret
? ret
: CMD_ERROR
;