2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include "../command.hpp"
14 #include "../loglevel.hpp"
15 #include "../uprobe.hpp"
17 #include "common/argpar/argpar.h"
18 #include "common/argpar-utils/argpar-utils.hpp"
19 #include "common/dynamic-array.hpp"
20 #include "common/mi-lttng.hpp"
21 #include "common/string-utils/string-utils.hpp"
22 #include "common/utils.hpp"
23 #include <lttng/domain-internal.hpp>
24 /* For lttng_event_rule_type_str(). */
25 #include <lttng/event-rule/event-rule-internal.hpp>
26 #include <lttng/lttng.h>
27 #include "common/filter/filter-ast.hpp"
28 #include "common/filter/filter-ir.hpp"
29 #include "common/dynamic-array.hpp"
31 #if (LTTNG_SYMBOL_NAME_LEN == 256)
32 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
35 #ifdef LTTNG_EMBED_HELP
36 static const char help_msg
[] =
37 #include <lttng-add-trigger.1.h>
69 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
70 { OPT_FILTER
, 'f', "filter", true },
71 { OPT_NAME
, 'n', "name", true },
72 { OPT_EXCLUDE_NAME
, 'x', "exclude-name", true },
73 { OPT_LOG_LEVEL
, 'l', "log-level", true },
74 { OPT_EVENT_NAME
, 'E', "event-name", true },
76 { OPT_TYPE
, 't', "type", true },
77 { OPT_LOCATION
, 'L', "location", true },
79 /* Capture descriptor */
80 { OPT_CAPTURE
, '\0', "capture", true },
82 ARGPAR_OPT_DESCR_SENTINEL
86 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
=
94 sizeof(syscall_type_opt_prefix
) - 1;
96 if (strncmp(arg
, syscall_type_opt_prefix
,
97 syscall_type_opt_prefix_len
) == 0) {
99 } else if (strncmp(arg
, kernel_syscall_type_opt_prefix
,
100 kernel_syscall_type_opt_prefix_len
) == 0) {
110 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
114 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
115 ERR("More than one `--type` was specified.");
119 if (strcmp(arg
, "user") == 0 || strcmp(arg
, "user:tracepoint") == 0) {
120 *dest
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
121 } else if (strcmp(arg
, "kernel") == 0 ||
122 strcmp(arg
, "kernel:tracepoint") == 0) {
123 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
;
124 } else if (strcmp(arg
, "jul") == 0 || strcmp(arg
, "jul:logging") == 0) {
125 *dest
= LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
;
126 } else if (strcmp(arg
, "log4j") == 0 ||
127 strcmp(arg
, "log4j:logging") == 0) {
128 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
;
129 } else if (strcmp(arg
, "python") == 0 ||
130 strcmp(arg
, "python:logging") == 0) {
131 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
132 } else if (strcmp(arg
, "kprobe") == 0 ||
133 strcmp(arg
, "kernel:kprobe") == 0) {
134 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
135 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
136 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
137 } else if (has_syscall_prefix(arg
)) {
139 * Matches the following:
143 * - syscall:entry+exit
146 * - kernel:syscall:entry
147 * - kernel:syscall:exit
148 * - kernel:syscall:entry+exit
151 * Validation for the right side is left to further usage sites.
153 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
155 ERR("Invalid `--type` value: %s", arg
);
170 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
175 ERR("Duplicate '%s' given.", opt_name
);
181 PERROR("Failed to allocate string '%s'.", opt_name
);
195 static bool parse_syscall_emission_site_from_type(const char *str
,
196 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
199 const char kernel_prefix
[] = "kernel:";
200 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
203 * If the passed string is of the form "kernel:syscall*", move the
204 * pointer passed "kernel:".
206 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
207 str
= &str
[kernel_prefix_len
];
210 if (strcmp(str
, "syscall") == 0 ||
211 strcmp(str
, "syscall:entry+exit") == 0) {
212 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
213 } else if (strcmp(str
, "syscall:entry") == 0) {
214 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
215 } else if (strcmp(str
, "syscall:exit") == 0) {
216 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
228 * Parse `str` as a log level against the passed event rule type.
230 * Return the log level in `*log_level`. Return true in `*log_level_only` if
231 * the string specifies exactly this log level, false if it specifies at least
234 * Return true if the string was successfully parsed as a log level string.
236 static bool parse_log_level_string(const char *str
,
237 enum lttng_event_rule_type event_rule_type
,
239 bool *log_level_only
)
243 switch (event_rule_type
) {
244 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
246 enum lttng_loglevel log_level_min
, log_level_max
;
247 if (!loglevel_parse_range_string(
248 str
, &log_level_min
, &log_level_max
)) {
252 /* Only support VAL and VAL.. for now. */
253 if (log_level_min
!= log_level_max
&&
254 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
258 *log_level
= (int) log_level_min
;
259 *log_level_only
= log_level_min
== log_level_max
;
262 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
264 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
265 if (!loglevel_log4j_parse_range_string(
266 str
, &log_level_min
, &log_level_max
)) {
270 /* Only support VAL and VAL.. for now. */
271 if (log_level_min
!= log_level_max
&&
272 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
276 *log_level
= (int) log_level_min
;
277 *log_level_only
= log_level_min
== log_level_max
;
280 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
282 enum lttng_loglevel_jul log_level_min
, log_level_max
;
283 if (!loglevel_jul_parse_range_string(
284 str
, &log_level_min
, &log_level_max
)) {
288 /* Only support VAL and VAL.. for now. */
289 if (log_level_min
!= log_level_max
&&
290 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
294 *log_level
= (int) log_level_min
;
295 *log_level_only
= log_level_min
== log_level_max
;
298 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
300 enum lttng_loglevel_python log_level_min
, log_level_max
;
301 if (!loglevel_python_parse_range_string(
302 str
, &log_level_min
, &log_level_max
)) {
306 /* Only support VAL and VAL.. for now. */
307 if (log_level_min
!= log_level_max
&&
309 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
313 *log_level
= (int) log_level_min
;
314 *log_level_only
= log_level_min
== log_level_max
;
318 /* Invalid domain type. */
332 static int parse_kernel_probe_opts(const char *source
,
333 struct lttng_kernel_probe_location
**location
)
338 char name
[LTTNG_SYMBOL_NAME_LEN
];
339 char *symbol_name
= NULL
;
342 /* Check for symbol+offset. */
343 match
= sscanf(source
,
344 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
348 if (*s_hex
== '\0') {
349 ERR("Kernel probe symbol offset is missing.");
353 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
355 PERROR("Failed to copy kernel probe location symbol name.");
358 offset
= strtoull(s_hex
, NULL
, 0);
360 *location
= lttng_kernel_probe_location_symbol_create(
361 symbol_name
, offset
);
363 ERR("Failed to create symbol kernel probe location.");
370 /* Check for symbol. */
371 if (isalpha(name
[0]) || name
[0] == '_') {
372 match
= sscanf(source
,
373 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
377 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
379 ERR("Failed to copy kernel probe location symbol name.");
383 *location
= lttng_kernel_probe_location_symbol_create(
386 ERR("Failed to create symbol kernel probe location.");
394 /* Check for address. */
395 match
= sscanf(source
, "%18s", s_hex
);
399 if (*s_hex
== '\0') {
400 ERR("Invalid kernel probe location address.");
404 address
= strtoull(s_hex
, NULL
, 0);
405 *location
= lttng_kernel_probe_location_address_create(address
);
407 ERR("Failed to create symbol kernel probe location.");
425 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
426 const struct ir_load_expression
*load_expr
,
427 const char *capture_str
)
429 char *provider_name
= NULL
;
430 struct lttng_event_expr
*event_expr
= NULL
;
431 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
432 const enum ir_load_expression_type load_expr_child_type
=
435 switch (load_expr_child_type
) {
436 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
437 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
439 const char *field_name
;
441 load_expr_op
= load_expr_op
->next
;
442 LTTNG_ASSERT(load_expr_op
);
443 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
444 field_name
= load_expr_op
->u
.symbol
;
445 LTTNG_ASSERT(field_name
);
447 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
448 lttng_event_expr_event_payload_field_create(field_name
) :
449 lttng_event_expr_channel_context_field_create(field_name
);
451 ERR("Failed to create %s event expression: field name = `%s`.",
452 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
453 "payload field" : "channel context",
460 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
463 const char *type_name
;
464 const char *field_name
;
466 load_expr_op
= load_expr_op
->next
;
467 LTTNG_ASSERT(load_expr_op
);
468 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
469 field_name
= load_expr_op
->u
.symbol
;
470 LTTNG_ASSERT(field_name
);
473 * The field name needs to be of the form PROVIDER:TYPE. We
476 colon
= strchr(field_name
, ':');
478 ERR("Invalid app-specific context field name: missing colon in `%s`.",
483 type_name
= colon
+ 1;
484 if (*type_name
== '\0') {
485 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
490 provider_name
= strndup(field_name
, colon
- field_name
);
491 if (!provider_name
) {
492 PERROR("Failed to allocate field name string");
496 event_expr
= lttng_event_expr_app_specific_context_field_create(
497 provider_name
, type_name
);
499 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
500 provider_name
, type_name
);
507 ERR("%s: unexpected load expr type %d.", __func__
,
512 load_expr_op
= load_expr_op
->next
;
514 /* There may be a single array index after that. */
515 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
516 struct lttng_event_expr
*index_event_expr
;
517 const uint64_t index
= load_expr_op
->u
.index
;
519 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
520 if (!index_event_expr
) {
521 ERR("Failed to create array field element event expression.");
525 event_expr
= index_event_expr
;
526 load_expr_op
= load_expr_op
->next
;
529 switch (load_expr_op
->type
) {
530 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
532 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
533 * always found at the end of the chain.
536 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
537 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
542 ERR("%s: unexpected load expression operator %s.", __func__
,
543 ir_load_expression_type_str(load_expr_op
->type
));
550 lttng_event_expr_destroy(event_expr
);
560 struct lttng_event_expr
*ir_op_load_to_event_expr(
561 const struct ir_op
*ir
, const char *capture_str
)
563 struct lttng_event_expr
*event_expr
= NULL
;
565 LTTNG_ASSERT(ir
->op
== IR_OP_LOAD
);
567 switch (ir
->data_type
) {
568 case IR_DATA_EXPRESSION
:
570 const struct ir_load_expression
*ir_load_expr
=
571 ir
->u
.load
.u
.expression
;
573 event_expr
= ir_op_load_expr_to_event_expr(
574 ir_load_expr
, capture_str
);
578 ERR("%s: unexpected data type: %s.", __func__
,
579 ir_data_type_str(ir
->data_type
));
587 const char *ir_operator_type_human_str(enum ir_op_type op
)
609 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
610 const char *capture_str
)
612 struct lttng_event_expr
*event_expr
= NULL
;
614 LTTNG_ASSERT(ir
->op
== IR_OP_ROOT
);
615 ir
= ir
->u
.root
.child
;
619 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
624 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
626 ir_operator_type_human_str(ir
->op
));
629 ERR("%s: unexpected IR op type: %s.", __func__
,
630 ir_op_type_str(ir
->op
));
638 void destroy_event_expr(void *ptr
)
640 lttng_event_expr_destroy((lttng_event_expr
*) ptr
);
644 struct parse_event_rule_res
{
646 struct lttng_event_rule
*er
;
648 /* Array of `struct lttng_event_expr *` */
649 struct lttng_dynamic_pointer_array capture_descriptors
;
654 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
,
657 enum lttng_event_rule_type event_rule_type
=
658 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
659 struct argpar_iter
*argpar_iter
= NULL
;
660 const struct argpar_item
*argpar_item
= NULL
;
661 int consumed_args
= -1;
662 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
663 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
664 struct parse_event_rule_res res
= {};
665 struct lttng_event_expr
*event_expr
= NULL
;
666 struct filter_parser_ctx
*parser_ctx
= NULL
;
667 struct lttng_log_level_rule
*log_level_rule
= NULL
;
669 /* Event rule type option */
670 char *event_rule_type_str
= NULL
;
672 /* Tracepoint and syscall options. */
674 /* Array of strings. */
675 struct lttng_dynamic_pointer_array exclude_names
;
677 /* For userspace / kernel probe and function. */
678 char *location
= NULL
;
679 char *event_name
= NULL
;
685 char *log_level_str
= NULL
;
687 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
690 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
692 argpar_iter
= argpar_iter_create(*argc
, *argv
, event_rule_opt_descrs
);
694 ERR("Failed to allocate an argpar iter.");
699 enum parse_next_item_status status
;
701 status
= parse_next_item(argpar_iter
, &argpar_item
,
702 argc_offset
, *argv
, false, NULL
, NULL
);
703 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
704 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
706 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
710 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
712 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
713 const struct argpar_opt_descr
*descr
=
714 argpar_item_opt_descr(argpar_item
);
715 const char *arg
= argpar_item_opt_arg(argpar_item
);
719 if (!assign_event_rule_type(&event_rule_type
, arg
)) {
723 /* Save the string for later use. */
724 if (!assign_string(&event_rule_type_str
, arg
,
731 if (!assign_string(&location
, arg
,
738 if (!assign_string(&event_name
, arg
,
739 "--event-name/-E")) {
745 if (!assign_string(&filter
, arg
,
752 if (!assign_string(&name
, arg
,
758 case OPT_EXCLUDE_NAME
:
762 ret
= lttng_dynamic_pointer_array_add_pointer(
766 ERR("Failed to add pointer to dynamic pointer array.");
773 if (!assign_string(&log_level_str
, arg
,
783 ret
= filter_parser_ctx_create_from_filter_expression(
786 ERR("Failed to parse capture expression `%s`.", arg
);
790 event_expr
= ir_op_root_to_event_expr(
791 parser_ctx
->ir_root
, arg
);
792 filter_parser_ctx_free(parser_ctx
);
793 parser_ctx
= nullptr;
796 * ir_op_root_to_event_expr has printed
802 ret
= lttng_dynamic_pointer_array_add_pointer(
803 &res
.capture_descriptors
,
810 * The ownership of event expression was
811 * transferred to the dynamic array.
821 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
823 /* Don't accept non-option arguments. */
824 ERR("Unexpected argument '%s'", arg
);
829 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
830 ERR("Event rule requires a --type.");
835 * Option --name is applicable to event rules of type kernel, user, jul,
836 * log4j,python and syscall. If --name is omitted, it is implicitly
839 switch (event_rule_type
) {
840 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
841 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
842 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
843 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
844 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
845 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
853 ERR("Can't use --name with %s event rules.",
854 lttng_event_rule_type_str(
859 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
860 ERR("Can't use --exclude-name/-x with %s event rules.",
861 lttng_event_rule_type_str(
868 * Option --location is only applicable to (and mandatory for) event
869 * rules of type {k,u}probe and function.
871 * Option --event-name is only applicable to event rules of type probe.
872 * If omitted, it defaults to the location.
874 switch (event_rule_type
) {
875 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
876 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
878 ERR("Event rule of type %s requires a --location.",
879 lttng_event_rule_type_str(event_rule_type
));
884 event_name
= strdup(location
);
891 ERR("Can't use --location with %s event rules.",
892 lttng_event_rule_type_str(event_rule_type
));
897 ERR("Can't use --event-name with %s event rules.",
898 lttng_event_rule_type_str(
905 * Update *argc and *argv so our caller can keep parsing what follows.
907 consumed_args
= argpar_iter_ingested_orig_args(argpar_iter
);
908 LTTNG_ASSERT(consumed_args
>= 0);
909 *argc
-= consumed_args
;
910 *argv
+= consumed_args
;
913 * Adding a filter to a probe, function or userspace-probe would be
914 * denied by the kernel tracer as it's not supported at the moment. We
915 * do an early check here to warn the user.
918 switch (event_rule_type
) {
919 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
920 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
921 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
922 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
923 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
924 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
927 ERR("Filter expressions are not supported for %s event rules.",
928 lttng_event_rule_type_str(event_rule_type
));
934 * If --exclude-name/-x was passed, split it into an exclusion list.
935 * Exclusions are only supported by
936 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
938 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
939 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
940 ERR("Event name exclusions are not yet implemented for %s event rules.",
941 lttng_event_rule_type_str(event_rule_type
));
945 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
947 * Assume validate_exclusion_list already prints an
955 switch (event_rule_type
) {
956 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
957 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
958 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
959 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
964 if (strcmp(log_level_str
, "..") == 0) {
966 * ".." is the same as passing no log level
967 * option and correspond to the "ANY" case.
972 if (!parse_log_level_string(log_level_str
, event_rule_type
,
973 &log_level
, &log_level_only
)) {
974 ERR("Failed to parse log level string `%s`.",
979 if (log_level_only
) {
980 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
982 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
985 if (log_level_rule
== NULL
) {
986 ERR("Failed to create log level rule object.");
992 ERR("Log levels are not supported for %s event rules.",
993 lttng_event_rule_type_str(event_rule_type
));
998 /* Finally, create the event rule object. */
999 switch (event_rule_type
) {
1000 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
1002 enum lttng_event_rule_status event_rule_status
;
1004 res
.er
= lttng_event_rule_user_tracepoint_create();
1006 ERR("Failed to create user_tracepoint event rule.");
1011 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1013 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1014 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1021 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1023 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1024 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1030 /* Set exclusion list. */
1031 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1033 int count
= lttng_dynamic_pointer_array_get_count(
1036 for (n
= 0; n
< count
; n
++) {
1037 const char *exclude_name
=
1038 (const char *) lttng_dynamic_pointer_array_get_pointer(
1043 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1046 if (event_rule_status
!=
1047 LTTNG_EVENT_RULE_STATUS_OK
) {
1048 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1055 if (log_level_rule
) {
1057 lttng_event_rule_user_tracepoint_set_log_level_rule(
1058 res
.er
, log_level_rule
);
1060 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1061 ERR("Failed to set log level on event fule.");
1068 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1070 enum lttng_event_rule_status event_rule_status
;
1072 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1074 ERR("Failed to create kernel_tracepoint event rule.");
1079 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1081 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1082 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1089 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1091 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1092 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1099 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1101 enum lttng_event_rule_status event_rule_status
;
1103 res
.er
= lttng_event_rule_jul_logging_create();
1105 ERR("Failed to create jul_logging event rule.");
1110 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1112 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1113 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1120 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1122 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1123 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1129 if (log_level_rule
) {
1131 lttng_event_rule_jul_logging_set_log_level_rule(
1132 res
.er
, log_level_rule
);
1134 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1135 ERR("Failed to set log level on event fule.");
1141 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1143 enum lttng_event_rule_status event_rule_status
;
1145 res
.er
= lttng_event_rule_log4j_logging_create();
1147 ERR("Failed to create jul_logging event rule.");
1152 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1154 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1155 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1162 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1164 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1165 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1171 if (log_level_rule
) {
1173 lttng_event_rule_log4j_logging_set_log_level_rule(
1174 res
.er
, log_level_rule
);
1176 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1177 ERR("Failed to set log level on event fule.");
1183 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1185 enum lttng_event_rule_status event_rule_status
;
1187 res
.er
= lttng_event_rule_python_logging_create();
1189 ERR("Failed to create jul_logging event rule.");
1194 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1196 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1197 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1204 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1206 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1207 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1213 if (log_level_rule
) {
1215 lttng_event_rule_python_logging_set_log_level_rule(
1216 res
.er
, log_level_rule
);
1218 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1219 ERR("Failed to set log level on event fule.");
1225 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1228 enum lttng_event_rule_status event_rule_status
;
1230 ret
= parse_kernel_probe_opts(
1231 location
, &kernel_probe_location
);
1233 ERR("Failed to parse kernel probe location.");
1237 LTTNG_ASSERT(kernel_probe_location
);
1238 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1240 ERR("Failed to create kprobe event rule.");
1245 lttng_event_rule_kernel_kprobe_set_event_name(
1246 res
.er
, event_name
);
1247 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1248 ERR("Failed to set kprobe event rule's name to '%s'.",
1255 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1258 enum lttng_event_rule_status event_rule_status
;
1260 ret
= parse_userspace_probe_opts(
1261 location
, &userspace_probe_location
);
1263 ERR("Failed to parse user space probe location.");
1267 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1269 ERR("Failed to create userspace probe event rule.");
1274 lttng_event_rule_kernel_uprobe_set_event_name(
1275 res
.er
, event_name
);
1276 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1277 ERR("Failed to set user space probe event rule's name to '%s'.",
1284 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1286 enum lttng_event_rule_status event_rule_status
;
1287 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1289 if (!parse_syscall_emission_site_from_type(
1290 event_rule_type_str
, &emission_site
)) {
1291 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1295 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1297 ERR("Failed to create syscall event rule.");
1301 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1303 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1304 ERR("Failed to set syscall event rule's pattern to '%s'.",
1310 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1312 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1313 ERR("Failed to set syscall event rule's filter to '%s'.",
1329 lttng_event_rule_destroy(res
.er
);
1331 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1335 filter_parser_ctx_free(parser_ctx
);
1338 lttng_event_expr_destroy(event_expr
);
1339 argpar_item_destroy(argpar_item
);
1340 argpar_iter_destroy(argpar_iter
);
1343 lttng_dynamic_pointer_array_reset(&exclude_names
);
1344 free(log_level_str
);
1347 free(event_rule_type_str
);
1349 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1350 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1351 lttng_log_level_rule_destroy(log_level_rule
);
1356 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
,
1359 struct parse_event_rule_res res
;
1360 struct lttng_condition
*c
;
1363 res
= parse_event_rule(argc
, argv
, argc_offset
);
1369 c
= lttng_condition_event_rule_matches_create(res
.er
);
1370 lttng_event_rule_destroy(res
.er
);
1376 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1378 enum lttng_condition_status status
;
1379 struct lttng_event_expr
**expr
=
1380 (lttng_event_expr
**) lttng_dynamic_array_get_element(
1381 &res
.capture_descriptors
.array
, i
);
1384 LTTNG_ASSERT(*expr
);
1385 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1387 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1388 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1389 ERR("The capture feature is unsupported by the event-rule condition type");
1395 /* Ownership of event expression moved to `c` */
1402 lttng_condition_destroy(c
);
1406 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1407 lttng_event_rule_destroy(res
.er
);
1412 struct condition_descr
{
1414 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
,
1420 struct condition_descr condition_descrs
[] = {
1421 { "event-rule-matches", handle_condition_event
},
1425 void print_valid_condition_names(void)
1429 ERR("Valid condition names are:");
1431 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
1432 ERR(" %s", condition_descrs
[i
].name
);
1437 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1438 const char ***argv
, int argc_offset
, int orig_arg_index
,
1439 const char *orig_arg
)
1442 struct lttng_condition
*cond
;
1443 const struct condition_descr
*descr
= NULL
;
1445 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1446 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1447 descr
= &condition_descrs
[i
];
1453 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown condition name '%s'",
1454 orig_arg_index
+ 1, orig_arg
, condition_name
);
1455 print_valid_condition_names();
1459 cond
= descr
->handler(argc
, argv
, argc_offset
);
1461 /* The handler has already printed an error message. */
1472 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1475 size_t num_token
= 0;
1476 struct lttng_dynamic_pointer_array tokens
;
1477 struct lttng_rate_policy
*policy
= NULL
;
1478 enum lttng_rate_policy_type policy_type
;
1479 unsigned long long value
;
1480 char *policy_type_str
;
1481 char *policy_value_str
;
1483 LTTNG_ASSERT(policy_str
);
1484 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1486 /* Rate policy fields are separated by ':'. */
1487 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1489 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1493 * Early sanity check that the number of parameter is exactly 2.
1496 if (num_token
!= 2) {
1497 ERR("Rate policy format is invalid.");
1501 policy_type_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1502 policy_value_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1504 /* Parse the type. */
1505 if (strcmp(policy_type_str
, "once-after") == 0) {
1506 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1507 } else if (strcmp(policy_type_str
, "every") == 0) {
1508 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1510 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1514 /* Parse the value. */
1515 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1516 ERR("Failed to parse rate policy value `%s` as an integer.",
1522 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1526 switch (policy_type
) {
1527 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1528 policy
= lttng_rate_policy_every_n_create(value
);
1530 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1531 policy
= lttng_rate_policy_once_after_n_create(value
);
1537 if (policy
== NULL
) {
1538 ERR("Failed to create rate policy `%s`.", policy_str
);
1542 lttng_dynamic_pointer_array_reset(&tokens
);
1546 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1547 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1548 ARGPAR_OPT_DESCR_SENTINEL
1552 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
,
1555 struct lttng_action
*action
= NULL
;
1556 struct argpar_iter
*argpar_iter
= NULL
;
1557 const struct argpar_item
*argpar_item
= NULL
;
1558 struct lttng_rate_policy
*policy
= NULL
;
1560 argpar_iter
= argpar_iter_create(*argc
, *argv
, notify_action_opt_descrs
);
1562 ERR("Failed to allocate an argpar iter.");
1567 enum parse_next_item_status status
;
1569 status
= parse_next_item(argpar_iter
, &argpar_item
,
1570 argc_offset
, *argv
, false, NULL
,
1571 "While parsing `notify` action:");
1572 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1573 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1575 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1579 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1581 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1582 const struct argpar_opt_descr
*descr
=
1583 argpar_item_opt_descr(argpar_item
);
1584 const char *arg
= argpar_item_opt_arg(argpar_item
);
1586 switch (descr
->id
) {
1587 case OPT_RATE_POLICY
:
1589 policy
= parse_rate_policy(arg
);
1599 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1601 ERR("Unexpected argument `%s`.", arg
);
1606 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1607 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1609 action
= lttng_action_notify_create();
1611 ERR("Failed to create notify action");
1616 enum lttng_action_status status
;
1617 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1618 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1619 ERR("Failed to set rate policy");
1627 lttng_action_destroy(action
);
1630 lttng_rate_policy_destroy(policy
);
1631 argpar_item_destroy(argpar_item
);
1632 argpar_iter_destroy(argpar_iter
);
1637 * Generic handler for a kind of action that takes a session name and an
1638 * optional rate policy.
1641 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1644 struct lttng_action
*(*create_action_cb
)(void),
1645 enum lttng_action_status (*set_session_name_cb
)(
1646 struct lttng_action
*, const char *),
1647 enum lttng_action_status (*set_rate_policy_cb
)(
1648 struct lttng_action
*,
1649 const struct lttng_rate_policy
*),
1650 const char *action_name
)
1652 struct lttng_action
*action
= NULL
;
1653 struct argpar_iter
*argpar_iter
= NULL
;
1654 const struct argpar_item
*argpar_item
= NULL
;
1655 const char *session_name_arg
= NULL
;
1656 enum lttng_action_status action_status
;
1657 struct lttng_rate_policy
*policy
= NULL
;
1659 LTTNG_ASSERT(set_session_name_cb
);
1660 LTTNG_ASSERT(set_rate_policy_cb
);
1662 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1663 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1664 ARGPAR_OPT_DESCR_SENTINEL
1667 argpar_iter
= argpar_iter_create(*argc
, *argv
, rate_policy_opt_descrs
);
1669 ERR("Failed to allocate an argpar iter.");
1674 enum parse_next_item_status status
;
1676 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1678 "While parsing `%s` action:", action_name
);
1679 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1680 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1682 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1686 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1688 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1689 const struct argpar_opt_descr
*descr
=
1690 argpar_item_opt_descr(argpar_item
);
1691 const char *arg
= argpar_item_opt_arg(argpar_item
);
1693 switch (descr
->id
) {
1694 case OPT_RATE_POLICY
:
1696 policy
= parse_rate_policy(arg
);
1706 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1707 unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1711 session_name_arg
= arg
;
1714 ERR("Unexpected argument `%s`.", arg
);
1720 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1721 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1723 if (!session_name_arg
) {
1724 ERR("Missing session name.");
1728 action
= create_action_cb();
1730 ERR("Failed to allocate %s session action.", action_name
);
1734 action_status
= set_session_name_cb(action
, session_name_arg
);
1735 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1736 ERR("Failed to set action %s session's session name to '%s'.",
1737 action_name
, session_name_arg
);
1742 action_status
= set_rate_policy_cb(action
, policy
);
1743 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1744 ERR("Failed to set rate policy");
1752 lttng_action_destroy(action
);
1756 lttng_rate_policy_destroy(policy
);
1757 argpar_item_destroy(argpar_item
);
1758 argpar_iter_destroy(argpar_iter
);
1763 struct lttng_action
*handle_action_start_session(int *argc
,
1764 const char ***argv
, int argc_offset
)
1766 return handle_action_simple_session_with_policy(argc
, argv
,
1768 lttng_action_start_session_create
,
1769 lttng_action_start_session_set_session_name
,
1770 lttng_action_start_session_set_rate_policy
, "start");
1774 struct lttng_action
*handle_action_stop_session(int *argc
,
1775 const char ***argv
, int argc_offset
)
1777 return handle_action_simple_session_with_policy(argc
, argv
,
1779 lttng_action_stop_session_create
,
1780 lttng_action_stop_session_set_session_name
,
1781 lttng_action_stop_session_set_rate_policy
, "stop");
1785 struct lttng_action
*handle_action_rotate_session(int *argc
,
1786 const char ***argv
, int argc_offset
)
1788 return handle_action_simple_session_with_policy(argc
, argv
,
1790 lttng_action_rotate_session_create
,
1791 lttng_action_rotate_session_set_session_name
,
1792 lttng_action_rotate_session_set_rate_policy
,
1796 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1797 { OPT_NAME
, 'n', "name", true },
1798 { OPT_MAX_SIZE
, 'm', "max-size", true },
1799 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1800 { OPT_DATA_URL
, '\0', "data-url", true },
1801 { OPT_URL
, '\0', "url", true },
1802 { OPT_PATH
, '\0', "path", true },
1803 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1804 ARGPAR_OPT_DESCR_SENTINEL
1808 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1809 const char ***argv
, int argc_offset
)
1811 struct lttng_action
*action
= NULL
;
1812 struct argpar_iter
*argpar_iter
= NULL
;
1813 const struct argpar_item
*argpar_item
= NULL
;
1814 const char *session_name_arg
= NULL
;
1815 char *snapshot_name_arg
= NULL
;
1816 char *ctrl_url_arg
= NULL
;
1817 char *data_url_arg
= NULL
;
1818 char *max_size_arg
= NULL
;
1819 char *url_arg
= NULL
;
1820 char *path_arg
= NULL
;
1822 enum lttng_action_status action_status
;
1823 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1824 struct lttng_rate_policy
*policy
= NULL
;
1826 unsigned int locations_specified
= 0;
1828 argpar_iter
= argpar_iter_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1830 ERR("Failed to allocate an argpar iter.");
1835 enum parse_next_item_status status
;
1837 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1838 *argv
, false, NULL
, "While parsing `snapshot` action:");
1839 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
||
1840 status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
1842 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1846 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1848 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1849 const struct argpar_opt_descr
*descr
=
1850 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(
1964 action
, session_name_arg
);
1965 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1966 ERR("Failed to set action snapshot session's session name to '%s'.",
1971 if (snapshot_name_arg
) {
1972 if (!snapshot_output
) {
1973 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1977 ret
= lttng_snapshot_output_set_name(
1978 snapshot_name_arg
, snapshot_output
);
1980 ERR("Failed to set name of snapshot output.");
1988 if (!snapshot_output
) {
1989 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1993 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1995 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1999 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
2001 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2009 struct lttng_uri
*uris
;
2011 if (!strstr(url_arg
, "://")) {
2012 ERR("Failed to parse '%s' as an URL.", url_arg
);
2016 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
2018 ERR("Failed to parse '%s' as an URL.", url_arg
);
2022 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2023 ret
= lttng_snapshot_output_set_local_path(
2024 uris
[0].dst
.path
, snapshot_output
);
2027 ERR("Failed to assign '%s' as a local destination.",
2032 ret
= lttng_snapshot_output_set_network_url(
2033 url_arg
, snapshot_output
);
2036 ERR("Failed to assign '%s' as a network URL.",
2044 ret
= lttng_snapshot_output_set_local_path(
2045 path_arg
, snapshot_output
);
2047 ERR("Failed to parse '%s' as a local path.", path_arg
);
2054 * Two argument form, network output with separate control and
2057 ret
= lttng_snapshot_output_set_network_urls(
2058 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2060 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2061 ctrl_url_arg
, data_url_arg
);
2066 if (snapshot_output
) {
2067 action_status
= lttng_action_snapshot_session_set_output(
2068 action
, snapshot_output
);
2069 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2070 ERR("Failed to set snapshot session action's output.");
2074 /* Ownership of `snapshot_output` has been transferred to the action. */
2075 snapshot_output
= NULL
;
2079 enum lttng_action_status status
;
2080 status
= lttng_action_snapshot_session_set_rate_policy(
2082 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2083 ERR("Failed to set rate policy");
2091 lttng_action_destroy(action
);
2095 free(snapshot_name_arg
);
2100 free(snapshot_output
);
2102 lttng_rate_policy_destroy(policy
);
2103 argpar_item_destroy(argpar_item
);
2104 argpar_iter_destroy(argpar_iter
);
2109 struct action_descr
{
2111 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
,
2117 struct action_descr action_descrs
[] = {
2118 { "notify", handle_action_notify
},
2119 { "start-session", handle_action_start_session
},
2120 { "stop-session", handle_action_stop_session
},
2121 { "rotate-session", handle_action_rotate_session
},
2122 { "snapshot-session", handle_action_snapshot_session
},
2126 void print_valid_action_names(void)
2130 ERR("Valid action names are:");
2132 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); ++i
) {
2133 ERR(" %s", action_descrs
[i
].name
);
2138 struct lttng_action
*parse_action(const char *action_name
, int *argc
,
2139 const char ***argv
, int argc_offset
, int orig_arg_index
,
2140 const char *orig_arg
)
2143 struct lttng_action
*action
;
2144 const struct action_descr
*descr
= NULL
;
2146 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2147 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2148 descr
= &action_descrs
[i
];
2154 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown action name '%s'",
2155 orig_arg_index
+ 1, orig_arg
, action_name
);
2156 print_valid_action_names();
2160 action
= descr
->handler(argc
, argv
, argc_offset
);
2162 /* The handler has already printed an error message. */
2174 struct argpar_opt_descr add_trigger_options
[] = {
2175 { OPT_HELP
, 'h', "help", false },
2176 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2177 { OPT_CONDITION
, '\0', "condition", true },
2178 { OPT_ACTION
, '\0', "action", true },
2179 { OPT_NAME
, '\0', "name", true },
2180 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2181 ARGPAR_OPT_DESCR_SENTINEL
,
2185 void lttng_actions_destructor(void *p
)
2187 struct lttng_action
*action
= (lttng_action
*) p
;
2189 lttng_action_destroy(action
);
2192 int cmd_add_trigger(int argc
, const char **argv
)
2195 int my_argc
= argc
- 1;
2196 const char **my_argv
= argv
+ 1;
2197 struct lttng_condition
*condition
= NULL
;
2198 struct lttng_dynamic_pointer_array actions
;
2199 struct argpar_iter
*argpar_iter
= NULL
;
2200 const struct argpar_item
*argpar_item
= NULL
;
2201 const struct argpar_error
*argpar_error
= NULL
;
2202 struct lttng_action
*action_list
= NULL
;
2203 struct lttng_action
*action
= NULL
;
2204 struct lttng_trigger
*trigger
= NULL
;
2207 char *owner_uid
= NULL
;
2208 enum lttng_error_code ret_code
;
2209 struct mi_writer
*mi_writer
= NULL
;
2211 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2214 mi_writer
= mi_lttng_writer_create(
2215 fileno(stdout
), lttng_opt_mi
);
2221 /* Open command element. */
2222 ret
= mi_lttng_writer_command_open(mi_writer
,
2223 mi_lttng_element_command_add_trigger
);
2229 /* Open output element. */
2230 ret
= mi_lttng_writer_open_element(
2231 mi_writer
, mi_lttng_element_command_output
);
2239 enum parse_next_item_status status
;
2241 const struct argpar_opt_descr
*descr
;
2244 argpar_iter_destroy(argpar_iter
);
2245 argpar_iter
= argpar_iter_create(my_argc
, my_argv
,
2246 add_trigger_options
);
2248 ERR("Failed to create argpar iter.");
2252 status
= parse_next_item(argpar_iter
, &argpar_item
,
2253 argc
- my_argc
, my_argv
, true, &argpar_error
, NULL
);
2254 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
2256 if (argpar_error_type(argpar_error
) ==
2257 ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
) {
2258 int opt_id
= argpar_error_opt_descr(argpar_error
, NULL
)->id
;
2260 if (opt_id
== OPT_CONDITION
) {
2261 print_valid_condition_names();
2262 } else if (opt_id
== OPT_ACTION
) {
2263 print_valid_action_names();
2268 } else if (status
== PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY
) {
2270 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
2274 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
2276 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_NON_OPT
) {
2277 ERR("Unexpected argument `%s`.",
2278 argpar_item_non_opt_arg(argpar_item
));
2282 ingested_args
= argpar_iter_ingested_orig_args(argpar_iter
);
2284 my_argc
-= ingested_args
;
2285 my_argv
+= ingested_args
;
2287 descr
= argpar_item_opt_descr(argpar_item
);
2288 arg
= argpar_item_opt_arg(argpar_item
);
2290 switch (descr
->id
) {
2295 case OPT_LIST_OPTIONS
:
2296 list_cmd_options_argpar(stdout
, add_trigger_options
);
2302 ERR("A --condition was already given.");
2306 condition
= parse_condition(arg
, &my_argc
, &my_argv
,
2307 argc
- my_argc
, argc
- my_argc
- ingested_args
,
2308 my_argv
[-ingested_args
]);
2311 * An error message was already printed by
2321 action
= parse_action(arg
, &my_argc
, &my_argv
,
2322 argc
- my_argc
, argc
- my_argc
- ingested_args
,
2323 my_argv
[-ingested_args
]);
2326 * An error message was already printed by
2332 ret
= lttng_dynamic_pointer_array_add_pointer(
2335 ERR("Failed to add pointer to pointer array.");
2339 /* Ownership of the action was transferred to the list. */
2346 if (!assign_string(&name
, arg
, "--name")) {
2354 if (!assign_string(&owner_uid
, arg
,
2367 ERR("Missing --condition.");
2371 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2372 ERR("Need at least one --action.");
2376 action_list
= lttng_action_list_create();
2381 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2382 enum lttng_action_status status
;
2384 action
= (lttng_action
*) lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2386 status
= lttng_action_list_add_action(action_list
, action
);
2387 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2392 * The `lttng_action_list_add_action()` takes a reference to
2393 * the action. We can destroy ours.
2395 lttng_action_destroy(action
);
2399 trigger
= lttng_trigger_create(condition
, action_list
);
2405 enum lttng_trigger_status trigger_status
;
2410 uid
= strtol(owner_uid
, &end
, 10);
2411 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2412 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2416 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2417 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2418 ERR("Failed to set trigger's user identity.");
2424 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2426 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2429 if (ret_code
!= LTTNG_OK
) {
2430 ERR("Failed to register trigger: %s.",
2431 lttng_strerror(-ret_code
));
2436 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2437 if (ret_code
!= LTTNG_OK
) {
2441 const char *returned_trigger_name
;
2442 const enum lttng_trigger_status trigger_status
=
2443 lttng_trigger_get_name(trigger
,
2444 &returned_trigger_name
);
2446 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2447 WARN("Failed to retrieve the added trigger's name.");
2449 MSG("Added trigger `%s`.", returned_trigger_name
);
2462 if (lttng_opt_mi
&& mi_writer
) {
2465 /* Close output element. */
2466 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2472 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2473 mi_lttng_element_command_success
, ret
? 0 : 1);
2479 /* Command element close. */
2480 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2488 argpar_error_destroy(argpar_error
);
2489 argpar_iter_destroy(argpar_iter
);
2490 argpar_item_destroy(argpar_item
);
2491 lttng_dynamic_pointer_array_reset(&actions
);
2492 lttng_condition_destroy(condition
);
2493 lttng_action_destroy(action_list
);
2494 lttng_action_destroy(action
);
2495 lttng_trigger_destroy(trigger
);
2498 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2499 /* Preserve original error code. */
2500 ret
= ret
? ret
: CMD_ERROR
;