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/dynamic-array.hpp"
13 #include "common/mi-lttng.hpp"
14 #include "common/string-utils/string-utils.hpp"
15 #include "common/utils.hpp"
16 #include "vendor/argpar/argpar.h"
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
, "log4j2") == 0 || strcmp(arg
, "log4j2:logging") == 0) {
126 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
;
127 } else if (strcmp(arg
, "python") == 0 || strcmp(arg
, "python:logging") == 0) {
128 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
129 } else if (strcmp(arg
, "kprobe") == 0 || strcmp(arg
, "kernel:kprobe") == 0) {
130 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
131 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
133 } else if (has_syscall_prefix(arg
)) {
135 * Matches the following:
139 * - syscall:entry+exit
142 * - kernel:syscall:entry
143 * - kernel:syscall:exit
144 * - kernel:syscall:entry+exit
147 * Validation for the right side is left to further usage sites.
149 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
151 ERR("Invalid `--type` value: %s", arg
);
165 static bool assign_string(char **dest
, const char *src
, const char *opt_name
)
170 ERR("Duplicate '%s' given.", opt_name
);
176 PERROR("Failed to allocate string '%s'.", opt_name
);
191 parse_syscall_emission_site_from_type(const char *str
,
192 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
195 const char kernel_prefix
[] = "kernel:";
196 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
199 * If the passed string is of the form "kernel:syscall*", move the
200 * pointer passed "kernel:".
202 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
203 str
= &str
[kernel_prefix_len
];
206 if (strcmp(str
, "syscall") == 0 || strcmp(str
, "syscall:entry+exit") == 0) {
207 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
208 } else if (strcmp(str
, "syscall:entry") == 0) {
209 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
210 } else if (strcmp(str
, "syscall:exit") == 0) {
211 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
223 * Parse `str` as a log level against the passed event rule type.
225 * Return the log level in `*log_level`. Return true in `*log_level_only` if
226 * the string specifies exactly this log level, false if it specifies at least
229 * Return true if the string was successfully parsed as a log level string.
231 static bool parse_log_level_string(const char *str
,
232 enum lttng_event_rule_type event_rule_type
,
234 bool *log_level_only
)
238 switch (event_rule_type
) {
239 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
241 enum lttng_loglevel log_level_least_severe
, log_level_most_severe
;
242 if (!loglevel_parse_range_string(
243 str
, &log_level_least_severe
, &log_level_most_severe
)) {
247 /* Only support VAL and VAL.. for now. */
248 if (log_level_least_severe
!= log_level_most_severe
&&
249 log_level_most_severe
!= LTTNG_LOGLEVEL_EMERG
) {
253 *log_level
= (int) log_level_least_severe
;
254 *log_level_only
= log_level_least_severe
== log_level_most_severe
;
257 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
259 enum lttng_loglevel_log4j log_level_least_severe
, log_level_most_severe
;
260 if (!loglevel_log4j_parse_range_string(
261 str
, &log_level_least_severe
, &log_level_most_severe
)) {
265 /* Only support VAL and VAL.. for now. */
266 if (log_level_least_severe
!= log_level_most_severe
&&
267 log_level_most_severe
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
271 *log_level
= (int) log_level_least_severe
;
272 *log_level_only
= log_level_least_severe
== log_level_most_severe
;
275 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
277 enum lttng_loglevel_log4j2 log_level_least_severe
, log_level_most_severe
;
278 if (!loglevel_log4j2_parse_range_string(
279 str
, &log_level_least_severe
, &log_level_most_severe
)) {
283 /* Only support VAL and VAL.. for now. */
284 if (log_level_least_severe
!= log_level_most_severe
&&
285 log_level_most_severe
!= LTTNG_LOGLEVEL_LOG4J2_FATAL
) {
289 *log_level
= (int) log_level_least_severe
;
290 *log_level_only
= log_level_least_severe
== log_level_most_severe
;
293 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
295 enum lttng_loglevel_jul log_level_least_severe
, log_level_most_severe
;
296 if (!loglevel_jul_parse_range_string(
297 str
, &log_level_least_severe
, &log_level_most_severe
)) {
301 /* Only support VAL and VAL.. for now. */
302 if (log_level_least_severe
!= log_level_most_severe
&&
303 log_level_most_severe
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
307 *log_level
= (int) log_level_least_severe
;
308 *log_level_only
= log_level_least_severe
== log_level_most_severe
;
311 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
313 enum lttng_loglevel_python log_level_least_severe
, log_level_most_severe
;
314 if (!loglevel_python_parse_range_string(
315 str
, &log_level_least_severe
, &log_level_most_severe
)) {
319 /* Only support VAL and VAL.. for now. */
320 if (log_level_least_severe
!= log_level_most_severe
&&
321 log_level_most_severe
!= LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
325 *log_level
= (int) log_level_least_severe
;
326 *log_level_only
= log_level_least_severe
== log_level_most_severe
;
330 /* Invalid domain type. */
344 static int parse_kernel_probe_opts(const char *source
,
345 struct lttng_kernel_probe_location
**location
)
350 char name
[LTTNG_SYMBOL_NAME_LEN
];
351 char *symbol_name
= nullptr;
354 /* Check for symbol+offset. */
356 source
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"[^'+']+%18s", name
, s_hex
);
358 if (*s_hex
== '\0') {
359 ERR("Kernel probe symbol offset is missing.");
363 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
365 PERROR("Failed to copy kernel probe location symbol name.");
368 offset
= strtoull(s_hex
, nullptr, 0);
370 *location
= lttng_kernel_probe_location_symbol_create(symbol_name
, offset
);
372 ERR("Failed to create symbol kernel probe location.");
379 /* Check for symbol. */
380 if (isalpha(name
[0]) || name
[0] == '_') {
381 match
= sscanf(source
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s", name
);
383 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
385 ERR("Failed to copy kernel probe location symbol name.");
389 *location
= lttng_kernel_probe_location_symbol_create(symbol_name
, 0);
391 ERR("Failed to create symbol kernel probe location.");
399 /* Check for address. */
400 match
= sscanf(source
, "%18s", s_hex
);
404 if (*s_hex
== '\0') {
405 ERR("Invalid kernel probe location address.");
409 address
= strtoull(s_hex
, nullptr, 0);
410 *location
= lttng_kernel_probe_location_address_create(address
);
412 ERR("Failed to create symbol kernel probe location.");
429 static struct lttng_event_expr
*
430 ir_op_load_expr_to_event_expr(const struct ir_load_expression
*load_expr
, const char *capture_str
)
432 char *provider_name
= nullptr;
433 struct lttng_event_expr
*event_expr
= nullptr;
434 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
435 const enum ir_load_expression_type load_expr_child_type
= load_expr_op
->type
;
437 switch (load_expr_child_type
) {
438 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
439 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
441 const char *field_name
;
443 load_expr_op
= load_expr_op
->next
;
444 LTTNG_ASSERT(load_expr_op
);
445 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
446 field_name
= load_expr_op
->u
.symbol
;
447 LTTNG_ASSERT(field_name
);
449 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
450 lttng_event_expr_event_payload_field_create(field_name
) :
451 lttng_event_expr_channel_context_field_create(field_name
);
453 ERR("Failed to create %s event expression: field name = `%s`.",
454 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
463 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
466 const char *type_name
;
467 const char *field_name
;
469 load_expr_op
= load_expr_op
->next
;
470 LTTNG_ASSERT(load_expr_op
);
471 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
472 field_name
= load_expr_op
->u
.symbol
;
473 LTTNG_ASSERT(field_name
);
476 * The field name needs to be of the form PROVIDER:TYPE. We
479 colon
= strchr(field_name
, ':');
481 ERR("Invalid app-specific context field name: missing colon in `%s`.",
486 type_name
= colon
+ 1;
487 if (*type_name
== '\0') {
488 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
493 provider_name
= strndup(field_name
, colon
- field_name
);
494 if (!provider_name
) {
495 PERROR("Failed to allocate field name string");
499 event_expr
= lttng_event_expr_app_specific_context_field_create(provider_name
,
502 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
511 ERR("%s: unexpected load expr type %d.", __func__
, load_expr_op
->type
);
515 load_expr_op
= load_expr_op
->next
;
517 /* There may be a single array index after that. */
518 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
519 struct lttng_event_expr
*index_event_expr
;
520 const uint64_t index
= load_expr_op
->u
.index
;
522 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
523 if (!index_event_expr
) {
524 ERR("Failed to create array field element event expression.");
528 event_expr
= index_event_expr
;
529 load_expr_op
= load_expr_op
->next
;
532 switch (load_expr_op
->type
) {
533 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
535 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
536 * always found at the end of the chain.
539 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
540 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
545 ERR("%s: unexpected load expression operator %s.",
547 ir_load_expression_type_str(load_expr_op
->type
));
554 lttng_event_expr_destroy(event_expr
);
555 event_expr
= nullptr;
563 static struct lttng_event_expr
*ir_op_load_to_event_expr(const struct ir_op
*ir
,
564 const char *capture_str
)
566 struct lttng_event_expr
*event_expr
= nullptr;
568 LTTNG_ASSERT(ir
->op
== IR_OP_LOAD
);
570 switch (ir
->data_type
) {
571 case IR_DATA_EXPRESSION
:
573 const struct ir_load_expression
*ir_load_expr
= ir
->u
.load
.u
.expression
;
575 event_expr
= ir_op_load_expr_to_event_expr(ir_load_expr
, capture_str
);
579 ERR("%s: unexpected data type: %s.", __func__
, ir_data_type_str(ir
->data_type
));
586 static const char *ir_operator_type_human_str(enum ir_op_type op
)
607 static struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
608 const char *capture_str
)
610 struct lttng_event_expr
*event_expr
= nullptr;
612 LTTNG_ASSERT(ir
->op
== IR_OP_ROOT
);
613 ir
= ir
->u
.root
.child
;
617 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
622 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
624 ir_operator_type_human_str(ir
->op
));
627 ERR("%s: unexpected IR op type: %s.", __func__
, ir_op_type_str(ir
->op
));
634 static void destroy_event_expr(void *ptr
)
636 lttng_event_expr_destroy((lttng_event_expr
*) ptr
);
640 struct parse_event_rule_res
{
642 struct lttng_event_rule
*er
;
644 /* Array of `struct lttng_event_expr *` */
645 struct lttng_dynamic_pointer_array capture_descriptors
;
649 static struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
, int argc_offset
)
651 enum lttng_event_rule_type event_rule_type
= LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
652 struct argpar_iter
*argpar_iter
= nullptr;
653 const struct argpar_item
*argpar_item
= nullptr;
654 int consumed_args
= -1;
655 struct lttng_kernel_probe_location
*kernel_probe_location
= nullptr;
656 struct lttng_userspace_probe_location
*userspace_probe_location
= nullptr;
657 struct parse_event_rule_res res
= {};
658 struct lttng_event_expr
*event_expr
= nullptr;
659 struct filter_parser_ctx
*parser_ctx
= nullptr;
660 struct lttng_log_level_rule
*log_level_rule
= nullptr;
662 /* Event rule type option */
663 char *event_rule_type_str
= nullptr;
665 /* Tracepoint and syscall options. */
666 char *name
= nullptr;
667 /* Array of strings. */
668 struct lttng_dynamic_pointer_array exclude_names
;
670 /* For userspace / kernel probe and function. */
671 char *location
= nullptr;
672 char *event_name
= nullptr;
675 char *filter
= nullptr;
678 char *log_level_str
= nullptr;
680 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
, destroy_event_expr
);
682 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
684 argpar_iter
= argpar_iter_create(*argc
, *argv
, event_rule_opt_descrs
);
686 ERR("Failed to allocate an argpar iter.");
691 enum parse_next_item_status status
;
693 status
= parse_next_item(
694 argpar_iter
, &argpar_item
, argc_offset
, *argv
, false, nullptr, nullptr);
695 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
696 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
698 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
702 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
704 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
705 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
706 const char *arg
= argpar_item_opt_arg(argpar_item
);
710 if (!assign_event_rule_type(&event_rule_type
, arg
)) {
714 /* Save the string for later use. */
715 if (!assign_string(&event_rule_type_str
, arg
, "--type/-t")) {
721 if (!assign_string(&location
, arg
, "--location/-L")) {
727 if (!assign_string(&event_name
, arg
, "--event-name/-E")) {
733 if (!assign_string(&filter
, arg
, "--filter/-f")) {
739 if (!assign_string(&name
, arg
, "--name/-n")) {
744 case OPT_EXCLUDE_NAME
:
748 ret
= lttng_dynamic_pointer_array_add_pointer(&exclude_names
,
751 ERR("Failed to add pointer to dynamic pointer array.");
758 if (!assign_string(&log_level_str
, arg
, "--log-level/-l")) {
767 ret
= filter_parser_ctx_create_from_filter_expression(arg
,
770 ERR("Failed to parse capture expression `%s`.", arg
);
774 event_expr
= ir_op_root_to_event_expr(parser_ctx
->ir_root
, arg
);
775 filter_parser_ctx_free(parser_ctx
);
776 parser_ctx
= nullptr;
779 * ir_op_root_to_event_expr has printed
785 ret
= lttng_dynamic_pointer_array_add_pointer(
786 &res
.capture_descriptors
, event_expr
);
792 * The ownership of event expression was
793 * transferred to the dynamic array.
795 event_expr
= nullptr;
803 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
805 /* Don't accept non-option arguments. */
806 ERR("Unexpected argument '%s'", arg
);
811 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
812 ERR("Event rule requires a --type.");
817 * Option --name is applicable to event rules of type kernel, user, jul,
818 * log4j, log4j2, python and syscall. If --name is omitted, it is
821 switch (event_rule_type
) {
822 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
823 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
824 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
825 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
826 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
827 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
828 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
836 ERR("Can't use --name with %s event rules.",
837 lttng_event_rule_type_str(event_rule_type
));
841 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
842 ERR("Can't use --exclude-name/-x with %s event rules.",
843 lttng_event_rule_type_str(event_rule_type
));
849 * Option --location is only applicable to (and mandatory for) event
850 * rules of type {k,u}probe and function.
852 * Option --event-name is only applicable to event rules of type probe.
853 * If omitted, it defaults to the location.
855 switch (event_rule_type
) {
856 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
857 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
859 ERR("Event rule of type %s requires a --location.",
860 lttng_event_rule_type_str(event_rule_type
));
865 event_name
= strdup(location
);
872 ERR("Can't use --location with %s event rules.",
873 lttng_event_rule_type_str(event_rule_type
));
878 ERR("Can't use --event-name with %s event rules.",
879 lttng_event_rule_type_str(event_rule_type
));
885 * Update *argc and *argv so our caller can keep parsing what follows.
887 consumed_args
= argpar_iter_ingested_orig_args(argpar_iter
);
888 LTTNG_ASSERT(consumed_args
>= 0);
889 *argc
-= consumed_args
;
890 *argv
+= consumed_args
;
893 * Adding a filter to a probe, function or userspace-probe would be
894 * denied by the kernel tracer as it's not supported at the moment. We
895 * do an early check here to warn the user.
898 switch (event_rule_type
) {
899 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
900 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
901 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
902 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
903 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
904 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
905 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
908 ERR("Filter expressions are not supported for %s event rules.",
909 lttng_event_rule_type_str(event_rule_type
));
915 * If --exclude-name/-x was passed, split it into an exclusion list.
916 * Exclusions are only supported by
917 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
919 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
920 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
921 ERR("Event name exclusions are not yet implemented for %s event rules.",
922 lttng_event_rule_type_str(event_rule_type
));
926 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
928 * Assume validate_exclusion_list already prints an
936 switch (event_rule_type
) {
937 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
938 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
939 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
940 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
941 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
946 if (strcmp(log_level_str
, "..") == 0) {
948 * ".." is the same as passing no log level
949 * option and correspond to the "ANY" case.
954 if (!parse_log_level_string(
955 log_level_str
, event_rule_type
, &log_level
, &log_level_only
)) {
956 ERR("Failed to parse log level string `%s`.", log_level_str
);
960 if (log_level_only
) {
961 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
963 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(
967 if (log_level_rule
== nullptr) {
968 ERR("Failed to create log level rule object.");
974 ERR("Log levels are not supported for %s event rules.",
975 lttng_event_rule_type_str(event_rule_type
));
980 /* Finally, create the event rule object. */
981 switch (event_rule_type
) {
982 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
984 enum lttng_event_rule_status event_rule_status
;
986 res
.er
= lttng_event_rule_user_tracepoint_create();
988 ERR("Failed to create user_tracepoint event rule.");
993 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(res
.er
, name
);
994 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
995 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.", name
);
1002 lttng_event_rule_user_tracepoint_set_filter(res
.er
, filter
);
1003 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1004 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1010 /* Set exclusion list. */
1011 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1013 const int count
= lttng_dynamic_pointer_array_get_count(&exclude_names
);
1015 for (n
= 0; n
< count
; n
++) {
1016 const char *exclude_name
=
1017 (const char *) lttng_dynamic_pointer_array_get_pointer(
1021 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1022 res
.er
, exclude_name
);
1023 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1024 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1031 if (log_level_rule
) {
1032 event_rule_status
= lttng_event_rule_user_tracepoint_set_log_level_rule(
1033 res
.er
, log_level_rule
);
1035 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1036 ERR("Failed to set log level on event fule.");
1043 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1045 enum lttng_event_rule_status event_rule_status
;
1047 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1049 ERR("Failed to create kernel_tracepoint event rule.");
1055 lttng_event_rule_kernel_tracepoint_set_name_pattern(res
.er
, name
);
1056 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1057 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.", name
);
1064 lttng_event_rule_kernel_tracepoint_set_filter(res
.er
, filter
);
1065 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1066 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1073 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1075 enum lttng_event_rule_status event_rule_status
;
1077 res
.er
= lttng_event_rule_jul_logging_create();
1079 ERR("Failed to create jul_logging event rule.");
1084 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(res
.er
, name
);
1085 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1086 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1092 event_rule_status
= lttng_event_rule_jul_logging_set_filter(res
.er
, filter
);
1093 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1094 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1100 if (log_level_rule
) {
1101 event_rule_status
= lttng_event_rule_jul_logging_set_log_level_rule(
1102 res
.er
, log_level_rule
);
1104 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1105 ERR("Failed to set log level on event fule.");
1111 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1113 enum lttng_event_rule_status event_rule_status
;
1115 res
.er
= lttng_event_rule_log4j_logging_create();
1117 ERR("Failed to create jul_logging event rule.");
1122 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(res
.er
, name
);
1123 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1124 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1131 lttng_event_rule_log4j_logging_set_filter(res
.er
, filter
);
1132 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1133 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1139 if (log_level_rule
) {
1140 event_rule_status
= lttng_event_rule_log4j_logging_set_log_level_rule(
1141 res
.er
, log_level_rule
);
1143 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1144 ERR("Failed to set log level on event fule.");
1150 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
1152 enum lttng_event_rule_status event_rule_status
;
1154 res
.er
= lttng_event_rule_log4j2_logging_create();
1156 ERR("Failed to create log4j2_logging event rule.");
1161 event_rule_status
= lttng_event_rule_log4j2_logging_set_name_pattern(res
.er
, name
);
1162 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1163 ERR("Failed to set log4j2_logging event rule's pattern to '%s'.", name
);
1170 lttng_event_rule_log4j2_logging_set_filter(res
.er
, filter
);
1171 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1172 ERR("Failed to set log4j2_logging event rule's filter to '%s'.",
1178 if (log_level_rule
) {
1179 event_rule_status
= lttng_event_rule_log4j2_logging_set_log_level_rule(
1180 res
.er
, log_level_rule
);
1182 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1183 ERR("Failed to set log level on event fule.");
1189 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1191 enum lttng_event_rule_status event_rule_status
;
1193 res
.er
= lttng_event_rule_python_logging_create();
1195 ERR("Failed to create jul_logging event rule.");
1200 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(res
.er
, name
);
1201 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1202 ERR("Failed to set jul_logging event rule's pattern to '%s'.", name
);
1209 lttng_event_rule_python_logging_set_filter(res
.er
, filter
);
1210 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1211 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1217 if (log_level_rule
) {
1218 event_rule_status
= lttng_event_rule_python_logging_set_log_level_rule(
1219 res
.er
, log_level_rule
);
1221 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1222 ERR("Failed to set log level on event fule.");
1228 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1231 enum lttng_event_rule_status event_rule_status
;
1233 ret
= parse_kernel_probe_opts(location
, &kernel_probe_location
);
1235 ERR("Failed to parse kernel probe location.");
1239 LTTNG_ASSERT(kernel_probe_location
);
1240 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1242 ERR("Failed to create kprobe event rule.");
1247 lttng_event_rule_kernel_kprobe_set_event_name(res
.er
, event_name
);
1248 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1249 ERR("Failed to set kprobe event rule's name to '%s'.", event_name
);
1255 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1258 enum lttng_event_rule_status event_rule_status
;
1260 ret
= parse_userspace_probe_opts(location
, &userspace_probe_location
);
1262 ERR("Failed to parse user space probe location.");
1266 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1268 ERR("Failed to create userspace probe event rule.");
1273 lttng_event_rule_kernel_uprobe_set_event_name(res
.er
, event_name
);
1274 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1275 ERR("Failed to set user space probe event rule's name to '%s'.",
1282 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1284 enum lttng_event_rule_status event_rule_status
;
1285 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1287 if (!parse_syscall_emission_site_from_type(event_rule_type_str
, &emission_site
)) {
1288 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1292 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1294 ERR("Failed to create syscall event rule.");
1298 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(res
.er
, name
);
1299 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1300 ERR("Failed to set syscall event rule's pattern to '%s'.", name
);
1306 lttng_event_rule_kernel_syscall_set_filter(res
.er
, filter
);
1307 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1308 ERR("Failed to set syscall event rule's filter to '%s'.", filter
);
1323 lttng_event_rule_destroy(res
.er
);
1325 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1329 filter_parser_ctx_free(parser_ctx
);
1332 lttng_event_expr_destroy(event_expr
);
1333 argpar_item_destroy(argpar_item
);
1334 argpar_iter_destroy(argpar_iter
);
1337 lttng_dynamic_pointer_array_reset(&exclude_names
);
1338 free(log_level_str
);
1341 free(event_rule_type_str
);
1343 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1344 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1345 lttng_log_level_rule_destroy(log_level_rule
);
1349 static struct lttng_condition
*
1350 handle_condition_event(int *argc
, const char ***argv
, int argc_offset
)
1352 struct parse_event_rule_res res
;
1353 struct lttng_condition
*c
;
1356 res
= parse_event_rule(argc
, argv
, argc_offset
);
1362 c
= lttng_condition_event_rule_matches_create(res
.er
);
1363 lttng_event_rule_destroy(res
.er
);
1369 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
); i
++) {
1370 enum lttng_condition_status status
;
1371 struct lttng_event_expr
**expr
=
1372 (lttng_event_expr
**) lttng_dynamic_array_get_element(
1373 &res
.capture_descriptors
.array
, i
);
1376 LTTNG_ASSERT(*expr
);
1377 status
= lttng_condition_event_rule_matches_append_capture_descriptor(c
, *expr
);
1378 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1379 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1380 ERR("The capture feature is unsupported by the event-rule condition type");
1386 /* Ownership of event expression moved to `c` */
1393 lttng_condition_destroy(c
);
1397 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1398 lttng_event_rule_destroy(res
.er
);
1403 struct condition_descr
{
1405 struct lttng_condition
*(*handler
)(int *argc
, const char ***argv
, int argc_offset
);
1409 static const struct condition_descr condition_descrs
[] = {
1410 { "event-rule-matches", handle_condition_event
},
1413 static void print_valid_condition_names()
1417 ERR("Valid condition names are:");
1419 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
1420 ERR(" %s", condition_descrs
[i
].name
);
1424 static struct lttng_condition
*parse_condition(const char *condition_name
,
1429 const char *orig_arg
)
1432 struct lttng_condition
*cond
;
1433 const struct condition_descr
*descr
= nullptr;
1435 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1436 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1437 descr
= &condition_descrs
[i
];
1443 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown condition name '%s'",
1447 print_valid_condition_names();
1451 cond
= descr
->handler(argc
, argv
, argc_offset
);
1453 /* The handler has already printed an error message. */
1464 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1467 size_t num_token
= 0;
1468 struct lttng_dynamic_pointer_array tokens
;
1469 struct lttng_rate_policy
*policy
= nullptr;
1470 enum lttng_rate_policy_type policy_type
;
1471 unsigned long long value
;
1472 char *policy_type_str
;
1473 char *policy_value_str
;
1475 LTTNG_ASSERT(policy_str
);
1476 lttng_dynamic_pointer_array_init(&tokens
, nullptr);
1478 /* Rate policy fields are separated by ':'. */
1479 ret
= strutils_split(policy_str
, ':', true, &tokens
);
1481 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1485 * Early sanity check that the number of parameter is exactly 2.
1488 if (num_token
!= 2) {
1489 ERR("Rate policy format is invalid.");
1493 policy_type_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1494 policy_value_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1496 /* Parse the type. */
1497 if (strcmp(policy_type_str
, "once-after") == 0) {
1498 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1499 } else if (strcmp(policy_type_str
, "every") == 0) {
1500 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1502 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1506 /* Parse the value. */
1507 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1508 ERR("Failed to parse rate policy value `%s` as an integer.", policy_value_str
);
1513 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1517 switch (policy_type
) {
1518 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1519 policy
= lttng_rate_policy_every_n_create(value
);
1521 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1522 policy
= lttng_rate_policy_once_after_n_create(value
);
1528 if (policy
== nullptr) {
1529 ERR("Failed to create rate policy `%s`.", policy_str
);
1533 lttng_dynamic_pointer_array_reset(&tokens
);
1537 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1538 { OPT_RATE_POLICY
, '\0', "rate-policy", true }, ARGPAR_OPT_DESCR_SENTINEL
1541 static struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
, int argc_offset
)
1543 struct lttng_action
*action
= nullptr;
1544 struct argpar_iter
*argpar_iter
= nullptr;
1545 const struct argpar_item
*argpar_item
= nullptr;
1546 struct lttng_rate_policy
*policy
= nullptr;
1548 argpar_iter
= argpar_iter_create(*argc
, *argv
, notify_action_opt_descrs
);
1550 ERR("Failed to allocate an argpar iter.");
1555 enum parse_next_item_status status
;
1557 status
= parse_next_item(argpar_iter
,
1563 "While parsing `notify` action:");
1564 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1565 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1567 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1571 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1573 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1574 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1575 const char *arg
= argpar_item_opt_arg(argpar_item
);
1577 switch (descr
->id
) {
1578 case OPT_RATE_POLICY
:
1580 policy
= parse_rate_policy(arg
);
1590 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1592 ERR("Unexpected argument `%s`.", arg
);
1597 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1598 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1600 action
= lttng_action_notify_create();
1602 ERR("Failed to create notify action");
1607 enum lttng_action_status status
;
1608 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1609 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1610 ERR("Failed to set rate policy");
1618 lttng_action_destroy(action
);
1621 lttng_rate_policy_destroy(policy
);
1622 argpar_item_destroy(argpar_item
);
1623 argpar_iter_destroy(argpar_iter
);
1628 * Generic handler for a kind of action that takes a session name and an
1629 * optional rate policy.
1632 static struct lttng_action
*handle_action_simple_session_with_policy(
1636 struct lttng_action
*(*create_action_cb
)(),
1637 enum lttng_action_status (*set_session_name_cb
)(struct lttng_action
*, const char *),
1638 enum lttng_action_status (*set_rate_policy_cb
)(struct lttng_action
*,
1639 const struct lttng_rate_policy
*),
1640 const char *action_name
)
1642 struct lttng_action
*action
= nullptr;
1643 struct argpar_iter
*argpar_iter
= nullptr;
1644 const struct argpar_item
*argpar_item
= nullptr;
1645 const char *session_name_arg
= nullptr;
1646 enum lttng_action_status action_status
;
1647 struct lttng_rate_policy
*policy
= nullptr;
1649 LTTNG_ASSERT(set_session_name_cb
);
1650 LTTNG_ASSERT(set_rate_policy_cb
);
1652 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1653 { OPT_RATE_POLICY
, '\0', "rate-policy", true }, ARGPAR_OPT_DESCR_SENTINEL
1656 argpar_iter
= argpar_iter_create(*argc
, *argv
, rate_policy_opt_descrs
);
1658 ERR("Failed to allocate an argpar iter.");
1663 enum parse_next_item_status status
;
1665 status
= parse_next_item(argpar_iter
,
1671 "While parsing `%s` action:",
1673 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1674 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1676 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1680 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1682 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1683 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1684 const char *arg
= argpar_item_opt_arg(argpar_item
);
1686 switch (descr
->id
) {
1687 case OPT_RATE_POLICY
:
1689 policy
= parse_rate_policy(arg
);
1699 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1700 const unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1704 session_name_arg
= arg
;
1707 ERR("Unexpected argument `%s`.", arg
);
1713 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1714 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1716 if (!session_name_arg
) {
1717 ERR("Missing session name.");
1721 action
= create_action_cb();
1723 ERR("Failed to allocate %s session action.", action_name
);
1727 action_status
= set_session_name_cb(action
, session_name_arg
);
1728 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1729 ERR("Failed to set action %s session's session name to '%s'.",
1736 action_status
= set_rate_policy_cb(action
, policy
);
1737 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1738 ERR("Failed to set rate policy");
1746 lttng_action_destroy(action
);
1750 lttng_rate_policy_destroy(policy
);
1751 argpar_item_destroy(argpar_item
);
1752 argpar_iter_destroy(argpar_iter
);
1756 static struct lttng_action
*
1757 handle_action_start_session(int *argc
, const char ***argv
, int argc_offset
)
1759 return handle_action_simple_session_with_policy(argc
,
1762 lttng_action_start_session_create
,
1763 lttng_action_start_session_set_session_name
,
1764 lttng_action_start_session_set_rate_policy
,
1768 static struct lttng_action
*
1769 handle_action_stop_session(int *argc
, const char ***argv
, int argc_offset
)
1771 return handle_action_simple_session_with_policy(argc
,
1774 lttng_action_stop_session_create
,
1775 lttng_action_stop_session_set_session_name
,
1776 lttng_action_stop_session_set_rate_policy
,
1780 static struct lttng_action
*
1781 handle_action_rotate_session(int *argc
, const char ***argv
, int argc_offset
)
1783 return handle_action_simple_session_with_policy(
1787 lttng_action_rotate_session_create
,
1788 lttng_action_rotate_session_set_session_name
,
1789 lttng_action_rotate_session_set_rate_policy
,
1793 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1794 { OPT_NAME
, 'n', "name", true },
1795 { OPT_MAX_SIZE
, 'm', "max-size", true },
1796 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1797 { OPT_DATA_URL
, '\0', "data-url", true },
1798 { OPT_URL
, '\0', "url", true },
1799 { OPT_PATH
, '\0', "path", true },
1800 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1801 ARGPAR_OPT_DESCR_SENTINEL
1804 static struct lttng_action
*
1805 handle_action_snapshot_session(int *argc
, const char ***argv
, int argc_offset
)
1807 struct lttng_action
*action
= nullptr;
1808 struct argpar_iter
*argpar_iter
= nullptr;
1809 const struct argpar_item
*argpar_item
= nullptr;
1810 const char *session_name_arg
= nullptr;
1811 char *snapshot_name_arg
= nullptr;
1812 char *ctrl_url_arg
= nullptr;
1813 char *data_url_arg
= nullptr;
1814 char *max_size_arg
= nullptr;
1815 char *url_arg
= nullptr;
1816 char *path_arg
= nullptr;
1817 char *error
= nullptr;
1818 enum lttng_action_status action_status
;
1819 struct lttng_snapshot_output
*snapshot_output
= nullptr;
1820 struct lttng_rate_policy
*policy
= nullptr;
1822 unsigned int locations_specified
= 0;
1824 argpar_iter
= argpar_iter_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1826 ERR("Failed to allocate an argpar iter.");
1831 enum parse_next_item_status status
;
1833 status
= parse_next_item(argpar_iter
,
1839 "While parsing `snapshot` action:");
1840 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1841 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1843 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1847 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1849 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1850 const struct argpar_opt_descr
*descr
= argpar_item_opt_descr(argpar_item
);
1851 const char *arg
= argpar_item_opt_arg(argpar_item
);
1853 switch (descr
->id
) {
1855 if (!assign_string(&snapshot_name_arg
, arg
, "--name/-n")) {
1861 if (!assign_string(&max_size_arg
, arg
, "--max-size/-m")) {
1867 if (!assign_string(&ctrl_url_arg
, arg
, "--ctrl-url")) {
1873 if (!assign_string(&data_url_arg
, arg
, "--data-url")) {
1879 if (!assign_string(&url_arg
, arg
, "--url")) {
1885 if (!assign_string(&path_arg
, arg
, "--path")) {
1890 case OPT_RATE_POLICY
:
1892 policy
= parse_rate_policy(arg
);
1902 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1903 const unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1907 session_name_arg
= arg
;
1910 ERR("Unexpected argument `%s`.", arg
);
1916 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1917 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1919 if (!session_name_arg
) {
1920 ERR("Missing session name.");
1924 /* --ctrl-url and --data-url must come in pair. */
1925 if (ctrl_url_arg
&& !data_url_arg
) {
1926 ERR("--ctrl-url is specified, but --data-url is missing.");
1930 if (!ctrl_url_arg
&& data_url_arg
) {
1931 ERR("--data-url is specified, but --ctrl-url is missing.");
1935 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1936 locations_specified
+= !!url_arg
;
1937 locations_specified
+= !!path_arg
;
1939 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1940 if (locations_specified
> 1) {
1941 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1946 * Did the user specify an option that implies using a
1947 * custom/unregistered output?
1949 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1950 snapshot_output
= lttng_snapshot_output_create();
1951 if (!snapshot_output
) {
1952 ERR("Failed to allocate a snapshot output.");
1957 action
= lttng_action_snapshot_session_create();
1959 ERR("Failed to allocate snapshot session action.");
1963 action_status
= lttng_action_snapshot_session_set_session_name(action
, session_name_arg
);
1964 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1965 ERR("Failed to set action snapshot session's session name to '%s'.",
1970 if (snapshot_name_arg
) {
1971 if (!snapshot_output
) {
1972 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1976 ret
= lttng_snapshot_output_set_name(snapshot_name_arg
, snapshot_output
);
1978 ERR("Failed to set name of snapshot output.");
1986 if (!snapshot_output
) {
1987 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1991 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1993 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1997 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1999 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2007 struct lttng_uri
*uris
;
2009 if (!strstr(url_arg
, "://")) {
2010 ERR("Failed to parse '%s' as an URL.", url_arg
);
2014 num_uris
= uri_parse_str_urls(url_arg
, nullptr, &uris
);
2016 ERR("Failed to parse '%s' as an URL.", url_arg
);
2020 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2021 ret
= lttng_snapshot_output_set_local_path(uris
[0].dst
.path
,
2025 ERR("Failed to assign '%s' as a local destination.", url_arg
);
2029 ret
= lttng_snapshot_output_set_network_url(url_arg
, snapshot_output
);
2032 ERR("Failed to assign '%s' as a network URL.", url_arg
);
2039 ret
= lttng_snapshot_output_set_local_path(path_arg
, snapshot_output
);
2041 ERR("Failed to parse '%s' as a local path.", path_arg
);
2048 * Two argument form, network output with separate control and
2051 ret
= lttng_snapshot_output_set_network_urls(
2052 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2054 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2061 if (snapshot_output
) {
2062 action_status
= lttng_action_snapshot_session_set_output(action
, snapshot_output
);
2063 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2064 ERR("Failed to set snapshot session action's output.");
2068 /* Ownership of `snapshot_output` has been transferred to the action. */
2069 snapshot_output
= nullptr;
2073 enum lttng_action_status status
;
2074 status
= lttng_action_snapshot_session_set_rate_policy(action
, policy
);
2075 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2076 ERR("Failed to set rate policy");
2084 lttng_action_destroy(action
);
2088 free(snapshot_name_arg
);
2093 free(snapshot_output
);
2095 lttng_rate_policy_destroy(policy
);
2096 argpar_item_destroy(argpar_item
);
2097 argpar_iter_destroy(argpar_iter
);
2102 struct action_descr
{
2104 struct lttng_action
*(*handler
)(int *argc
, const char ***argv
, int argc_offset
);
2108 static const struct action_descr action_descrs
[] = {
2109 { "notify", handle_action_notify
},
2110 { "start-session", handle_action_start_session
},
2111 { "stop-session", handle_action_stop_session
},
2112 { "rotate-session", handle_action_rotate_session
},
2113 { "snapshot-session", handle_action_snapshot_session
},
2116 static void print_valid_action_names()
2120 ERR("Valid action names are:");
2122 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
2123 ERR(" %s", action_descrs
[i
].name
);
2127 static struct lttng_action
*parse_action(const char *action_name
,
2132 const char *orig_arg
)
2135 struct lttng_action
*action
;
2136 const struct action_descr
*descr
= nullptr;
2138 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2139 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2140 descr
= &action_descrs
[i
];
2146 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown action name '%s'",
2150 print_valid_action_names();
2154 action
= descr
->handler(argc
, argv
, argc_offset
);
2156 /* The handler has already printed an error message. */
2167 static const struct argpar_opt_descr add_trigger_options
[] = {
2168 { OPT_HELP
, 'h', "help", false },
2169 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2170 { OPT_CONDITION
, '\0', "condition", true },
2171 { OPT_ACTION
, '\0', "action", true },
2172 { OPT_NAME
, '\0', "name", true },
2173 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2174 ARGPAR_OPT_DESCR_SENTINEL
,
2177 static void lttng_actions_destructor(void *p
)
2179 struct lttng_action
*action
= (lttng_action
*) p
;
2181 lttng_action_destroy(action
);
2184 int cmd_add_trigger(int argc
, const char **argv
)
2187 int my_argc
= argc
- 1;
2188 const char **my_argv
= argv
+ 1;
2189 struct lttng_condition
*condition
= nullptr;
2190 struct lttng_dynamic_pointer_array actions
;
2191 struct argpar_iter
*argpar_iter
= nullptr;
2192 const struct argpar_item
*argpar_item
= nullptr;
2193 const struct argpar_error
*argpar_error
= nullptr;
2194 struct lttng_action
*action_list
= nullptr;
2195 struct lttng_action
*action
= nullptr;
2196 struct lttng_trigger
*trigger
= nullptr;
2197 char *name
= nullptr;
2199 char *owner_uid
= nullptr;
2200 enum lttng_error_code ret_code
;
2201 struct mi_writer
*mi_writer
= nullptr;
2203 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2206 mi_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
2212 /* Open command element. */
2213 ret
= mi_lttng_writer_command_open(mi_writer
, mi_lttng_element_command_add_trigger
);
2219 /* Open output element. */
2220 ret
= mi_lttng_writer_open_element(mi_writer
, mi_lttng_element_command_output
);
2228 enum parse_next_item_status status
;
2230 const struct argpar_opt_descr
*descr
;
2233 argpar_iter_destroy(argpar_iter
);
2234 argpar_iter
= argpar_iter_create(my_argc
, my_argv
, add_trigger_options
);
2236 ERR("Failed to create argpar iter.");
2240 status
= parse_next_item(argpar_iter
,
2247 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
2248 if (argpar_error_type(argpar_error
) == ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
) {
2250 argpar_error_opt_descr(argpar_error
, nullptr)->id
;
2252 if (opt_id
== OPT_CONDITION
) {
2253 print_valid_condition_names();
2254 } else if (opt_id
== OPT_ACTION
) {
2255 print_valid_action_names();
2260 } else if (status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
2262 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
2266 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
2268 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_NON_OPT
) {
2269 ERR("Unexpected argument `%s`.", argpar_item_non_opt_arg(argpar_item
));
2273 ingested_args
= argpar_iter_ingested_orig_args(argpar_iter
);
2275 my_argc
-= ingested_args
;
2276 my_argv
+= ingested_args
;
2278 descr
= argpar_item_opt_descr(argpar_item
);
2279 arg
= argpar_item_opt_arg(argpar_item
);
2281 switch (descr
->id
) {
2286 case OPT_LIST_OPTIONS
:
2287 list_cmd_options_argpar(stdout
, add_trigger_options
);
2293 ERR("A --condition was already given.");
2297 condition
= parse_condition(arg
,
2301 argc
- my_argc
- ingested_args
,
2302 my_argv
[-ingested_args
]);
2305 * An error message was already printed by
2315 action
= parse_action(arg
,
2319 argc
- my_argc
- ingested_args
,
2320 my_argv
[-ingested_args
]);
2323 * An error message was already printed by
2329 ret
= lttng_dynamic_pointer_array_add_pointer(&actions
, action
);
2331 ERR("Failed to add pointer to pointer array.");
2335 /* Ownership of the action was transferred to the list. */
2342 if (!assign_string(&name
, arg
, "--name")) {
2350 if (!assign_string(&owner_uid
, arg
, "--owner-uid")) {
2362 ERR("Missing --condition.");
2366 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2367 ERR("Need at least one --action.");
2371 action_list
= lttng_action_list_create();
2376 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2377 enum lttng_action_status status
;
2379 action
= (lttng_action
*) lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2381 status
= lttng_action_list_add_action(action_list
, action
);
2382 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2387 * The `lttng_action_list_add_action()` takes a reference to
2388 * the action. We can destroy ours.
2390 lttng_action_destroy(action
);
2394 trigger
= lttng_trigger_create(condition
, action_list
);
2400 enum lttng_trigger_status trigger_status
;
2405 uid
= strtol(owner_uid
, &end
, 10);
2406 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2407 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2411 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2412 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2413 ERR("Failed to set trigger's user identity.");
2419 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2421 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2424 if (ret_code
!= LTTNG_OK
) {
2425 ERR("Failed to register trigger: %s.", lttng_strerror(-ret_code
));
2430 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, nullptr);
2431 if (ret_code
!= LTTNG_OK
) {
2435 const char *returned_trigger_name
;
2436 const enum lttng_trigger_status trigger_status
=
2437 lttng_trigger_get_name(trigger
, &returned_trigger_name
);
2439 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2440 WARN("Failed to retrieve the added trigger's name.");
2442 MSG("Added trigger `%s`.", returned_trigger_name
);
2455 if (lttng_opt_mi
&& mi_writer
) {
2458 /* Close output element. */
2459 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2465 mi_ret
= mi_lttng_writer_write_element_bool(
2466 mi_writer
, mi_lttng_element_command_success
, ret
? 0 : 1);
2472 /* Command element close. */
2473 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2481 argpar_error_destroy(argpar_error
);
2482 argpar_iter_destroy(argpar_iter
);
2483 argpar_item_destroy(argpar_item
);
2484 lttng_dynamic_pointer_array_reset(&actions
);
2485 lttng_condition_destroy(condition
);
2486 lttng_action_destroy(action_list
);
2487 lttng_action_destroy(action
);
2488 lttng_trigger_destroy(trigger
);
2491 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2492 /* Preserve original error code. */
2493 ret
= ret
? ret
: CMD_ERROR
;