2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include "../command.h"
14 #include "../loglevel.h"
15 #include "../uprobe.h"
17 #include "common/argpar/argpar.h"
18 #include "common/argpar-utils/argpar-utils.h"
19 #include "common/dynamic-array.h"
20 #include "common/mi-lttng.h"
21 #include "common/string-utils/string-utils.h"
22 #include "common/utils.h"
23 #include <lttng/domain-internal.h>
24 /* For lttng_event_rule_type_str(). */
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/lttng.h>
27 #include "common/filter/filter-ast.h"
28 #include "common/filter/filter-ir.h"
29 #include "common/dynamic-array.h"
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
= strtoul(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
= strtoul(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 assert(load_expr_op
);
443 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
444 field_name
= load_expr_op
->u
.symbol
;
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 assert(load_expr_op
);
468 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
469 field_name
= load_expr_op
->u
.symbol
;
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 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 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(ptr
);
643 struct parse_event_rule_res
{
645 struct lttng_event_rule
*er
;
647 /* Array of `struct lttng_event_expr *` */
648 struct lttng_dynamic_pointer_array capture_descriptors
;
652 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
,
655 enum lttng_event_rule_type event_rule_type
=
656 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
657 struct argpar_iter
*argpar_iter
= NULL
;
658 const struct argpar_item
*argpar_item
= NULL
;
659 int consumed_args
= -1;
660 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
661 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
662 struct parse_event_rule_res res
= { 0 };
663 struct lttng_event_expr
*event_expr
= NULL
;
664 struct filter_parser_ctx
*parser_ctx
= NULL
;
665 struct lttng_log_level_rule
*log_level_rule
= NULL
;
667 /* Event rule type option */
668 char *event_rule_type_str
= NULL
;
670 /* Tracepoint and syscall options. */
672 /* Array of strings. */
673 struct lttng_dynamic_pointer_array exclude_names
;
675 /* For userspace / kernel probe and function. */
676 char *location
= NULL
;
677 char *event_name
= NULL
;
683 char *log_level_str
= NULL
;
685 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
688 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
690 argpar_iter
= argpar_iter_create(*argc
, *argv
, event_rule_opt_descrs
);
692 ERR("Failed to allocate an argpar iter.");
697 enum parse_next_item_status status
;
699 status
= parse_next_item(argpar_iter
, &argpar_item
,
700 argc_offset
, *argv
, false, NULL
);
701 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
703 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
707 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
709 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
710 const struct argpar_opt_descr
*descr
=
711 argpar_item_opt_descr(argpar_item
);
712 const char *arg
= argpar_item_opt_arg(argpar_item
);
716 if (!assign_event_rule_type(&event_rule_type
, arg
)) {
720 /* Save the string for later use. */
721 if (!assign_string(&event_rule_type_str
, arg
,
728 if (!assign_string(&location
, arg
,
735 if (!assign_string(&event_name
, arg
,
736 "--event-name/-E")) {
742 if (!assign_string(&filter
, arg
,
749 if (!assign_string(&name
, arg
,
755 case OPT_EXCLUDE_NAME
:
759 ret
= lttng_dynamic_pointer_array_add_pointer(
763 ERR("Failed to add pointer to dynamic pointer array.");
770 if (!assign_string(&log_level_str
, arg
,
780 ret
= filter_parser_ctx_create_from_filter_expression(
783 ERR("Failed to parse capture expression `%s`.", arg
);
787 event_expr
= ir_op_root_to_event_expr(
788 parser_ctx
->ir_root
, arg
);
791 * ir_op_root_to_event_expr has printed
797 ret
= lttng_dynamic_pointer_array_add_pointer(
798 &res
.capture_descriptors
,
805 * The ownership of event expression was
806 * transferred to the dynamic array.
816 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
818 /* Don't accept non-option arguments. */
819 ERR("Unexpected argument '%s'", arg
);
824 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
825 ERR("Event rule requires a --type.");
830 * Option --name is applicable to event rules of type kernel, user, jul,
831 * log4j,python and syscall. If --name is omitted, it is implicitly
834 switch (event_rule_type
) {
835 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
836 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
837 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
838 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
839 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
840 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
848 ERR("Can't use --name with %s event rules.",
849 lttng_event_rule_type_str(
854 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
855 ERR("Can't use --exclude-name/-x with %s event rules.",
856 lttng_event_rule_type_str(
863 * Option --location is only applicable to (and mandatory for) event
864 * rules of type {k,u}probe and function.
866 * Option --event-name is only applicable to event rules of type probe.
867 * If omitted, it defaults to the location.
869 switch (event_rule_type
) {
870 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
871 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
873 ERR("Event rule of type %s requires a --location.",
874 lttng_event_rule_type_str(event_rule_type
));
879 event_name
= strdup(location
);
886 ERR("Can't use --location with %s event rules.",
887 lttng_event_rule_type_str(event_rule_type
));
892 ERR("Can't use --event-name with %s event rules.",
893 lttng_event_rule_type_str(
900 * Update *argc and *argv so our caller can keep parsing what follows.
902 consumed_args
= argpar_iter_ingested_orig_args(argpar_iter
);
903 assert(consumed_args
>= 0);
904 *argc
-= consumed_args
;
905 *argv
+= consumed_args
;
908 * Adding a filter to a probe, function or userspace-probe would be
909 * denied by the kernel tracer as it's not supported at the moment. We
910 * do an early check here to warn the user.
913 switch (event_rule_type
) {
914 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
915 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
916 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
917 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
918 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
919 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
922 ERR("Filter expressions are not supported for %s event rules.",
923 lttng_event_rule_type_str(event_rule_type
));
929 * If --exclude-name/-x was passed, split it into an exclusion list.
930 * Exclusions are only supported by
931 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
933 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
934 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
935 ERR("Event name exclusions are not yet implemented for %s event rules.",
936 lttng_event_rule_type_str(event_rule_type
));
940 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
942 * Assume validate_exclusion_list already prints an
950 switch (event_rule_type
) {
951 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
952 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
953 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
954 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
959 if (strcmp(log_level_str
, "..") == 0) {
961 * ".." is the same as passing no log level
962 * option and correspond to the "ANY" case.
967 if (!parse_log_level_string(log_level_str
, event_rule_type
,
968 &log_level
, &log_level_only
)) {
969 ERR("Failed to parse log level string `%s`.",
974 if (log_level_only
) {
975 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
977 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
980 if (log_level_rule
== NULL
) {
981 ERR("Failed to create log level rule object.");
987 ERR("Log levels are not supported for %s event rules.",
988 lttng_event_rule_type_str(event_rule_type
));
993 /* Finally, create the event rule object. */
994 switch (event_rule_type
) {
995 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
997 enum lttng_event_rule_status event_rule_status
;
999 res
.er
= lttng_event_rule_user_tracepoint_create();
1001 ERR("Failed to create user_tracepoint event rule.");
1006 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1008 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1009 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1016 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1018 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1019 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1025 /* Set exclusion list. */
1026 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1028 int count
= lttng_dynamic_pointer_array_get_count(
1031 for (n
= 0; n
< count
; n
++) {
1032 const char *exclude_name
=
1033 lttng_dynamic_pointer_array_get_pointer(
1038 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1041 if (event_rule_status
!=
1042 LTTNG_EVENT_RULE_STATUS_OK
) {
1043 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1050 if (log_level_rule
) {
1052 lttng_event_rule_user_tracepoint_set_log_level_rule(
1053 res
.er
, log_level_rule
);
1055 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1056 ERR("Failed to set log level on event fule.");
1063 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1065 enum lttng_event_rule_status event_rule_status
;
1067 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1069 ERR("Failed to create kernel_tracepoint event rule.");
1074 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1076 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1077 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1084 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1086 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1087 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1094 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1096 enum lttng_event_rule_status event_rule_status
;
1098 res
.er
= lttng_event_rule_jul_logging_create();
1100 ERR("Failed to create jul_logging event rule.");
1105 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1107 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1108 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1115 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1117 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1118 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1124 if (log_level_rule
) {
1126 lttng_event_rule_jul_logging_set_log_level_rule(
1127 res
.er
, log_level_rule
);
1129 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1130 ERR("Failed to set log level on event fule.");
1136 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1138 enum lttng_event_rule_status event_rule_status
;
1140 res
.er
= lttng_event_rule_log4j_logging_create();
1142 ERR("Failed to create jul_logging event rule.");
1147 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1149 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1150 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1157 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1159 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1160 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1166 if (log_level_rule
) {
1168 lttng_event_rule_log4j_logging_set_log_level_rule(
1169 res
.er
, log_level_rule
);
1171 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1172 ERR("Failed to set log level on event fule.");
1178 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1180 enum lttng_event_rule_status event_rule_status
;
1182 res
.er
= lttng_event_rule_python_logging_create();
1184 ERR("Failed to create jul_logging event rule.");
1189 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1191 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1192 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1199 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1201 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1202 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1208 if (log_level_rule
) {
1210 lttng_event_rule_python_logging_set_log_level_rule(
1211 res
.er
, log_level_rule
);
1213 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1214 ERR("Failed to set log level on event fule.");
1220 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1223 enum lttng_event_rule_status event_rule_status
;
1225 ret
= parse_kernel_probe_opts(
1226 location
, &kernel_probe_location
);
1228 ERR("Failed to parse kernel probe location.");
1232 assert(kernel_probe_location
);
1233 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1235 ERR("Failed to create kprobe event rule.");
1240 lttng_event_rule_kernel_kprobe_set_event_name(
1241 res
.er
, event_name
);
1242 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1243 ERR("Failed to set kprobe event rule's name to '%s'.",
1250 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1253 enum lttng_event_rule_status event_rule_status
;
1255 ret
= parse_userspace_probe_opts(
1256 location
, &userspace_probe_location
);
1258 ERR("Failed to parse user space probe location.");
1262 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1264 ERR("Failed to create userspace probe event rule.");
1269 lttng_event_rule_kernel_uprobe_set_event_name(
1270 res
.er
, event_name
);
1271 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1272 ERR("Failed to set user space probe event rule's name to '%s'.",
1279 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1281 enum lttng_event_rule_status event_rule_status
;
1282 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1284 if (!parse_syscall_emission_site_from_type(
1285 event_rule_type_str
, &emission_site
)) {
1286 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1290 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1292 ERR("Failed to create syscall event rule.");
1296 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1298 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1299 ERR("Failed to set syscall event rule's pattern to '%s'.",
1305 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1307 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1308 ERR("Failed to set syscall event rule's filter to '%s'.",
1324 lttng_event_rule_destroy(res
.er
);
1326 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1330 filter_parser_ctx_free(parser_ctx
);
1333 lttng_event_expr_destroy(event_expr
);
1334 argpar_item_destroy(argpar_item
);
1335 argpar_iter_destroy(argpar_iter
);
1338 lttng_dynamic_pointer_array_reset(&exclude_names
);
1339 free(log_level_str
);
1342 free(event_rule_type_str
);
1344 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1345 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1346 lttng_log_level_rule_destroy(log_level_rule
);
1351 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
,
1354 struct parse_event_rule_res res
;
1355 struct lttng_condition
*c
;
1358 res
= parse_event_rule(argc
, argv
, argc_offset
);
1364 c
= lttng_condition_event_rule_matches_create(res
.er
);
1365 lttng_event_rule_destroy(res
.er
);
1371 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1373 enum lttng_condition_status status
;
1374 struct lttng_event_expr
**expr
=
1375 lttng_dynamic_array_get_element(
1376 &res
.capture_descriptors
.array
, i
);
1380 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1382 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1383 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1384 ERR("The capture feature is unsupported by the event-rule condition type");
1390 /* Ownership of event expression moved to `c` */
1397 lttng_condition_destroy(c
);
1401 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1402 lttng_event_rule_destroy(res
.er
);
1406 struct condition_descr
{
1408 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
,
1413 struct condition_descr condition_descrs
[] = {
1414 { "event-rule-matches", handle_condition_event
},
1418 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1419 const char ***argv
, int argc_offset
)
1422 struct lttng_condition
*cond
;
1423 const struct condition_descr
*descr
= NULL
;
1425 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1426 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1427 descr
= &condition_descrs
[i
];
1433 ERR("Unknown condition name '%s'", condition_name
);
1437 cond
= descr
->handler(argc
, argv
, argc_offset
);
1439 /* The handler has already printed an error message. */
1450 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1453 size_t num_token
= 0;
1454 struct lttng_dynamic_pointer_array tokens
;
1455 struct lttng_rate_policy
*policy
= NULL
;
1456 enum lttng_rate_policy_type policy_type
;
1457 unsigned long long value
;
1458 char *policy_type_str
;
1459 char *policy_value_str
;
1462 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1464 /* Rate policy fields are separated by ':'. */
1465 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1467 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1471 * Early sanity check that the number of parameter is exactly 2.
1474 if (num_token
!= 2) {
1475 ERR("Rate policy format is invalid.");
1479 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1480 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1482 /* Parse the type. */
1483 if (strcmp(policy_type_str
, "once-after") == 0) {
1484 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1485 } else if (strcmp(policy_type_str
, "every") == 0) {
1486 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1488 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1492 /* Parse the value. */
1493 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1494 ERR("Failed to parse rate policy value `%s` as an integer.",
1500 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1504 switch (policy_type
) {
1505 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1506 policy
= lttng_rate_policy_every_n_create(value
);
1508 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1509 policy
= lttng_rate_policy_once_after_n_create(value
);
1515 if (policy
== NULL
) {
1516 ERR("Failed to create rate policy `%s`.", policy_str
);
1520 lttng_dynamic_pointer_array_reset(&tokens
);
1524 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1525 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1526 ARGPAR_OPT_DESCR_SENTINEL
1530 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
,
1533 struct lttng_action
*action
= NULL
;
1534 struct argpar_iter
*argpar_iter
= NULL
;
1535 const struct argpar_item
*argpar_item
= NULL
;
1536 struct lttng_rate_policy
*policy
= NULL
;
1538 argpar_iter
= argpar_iter_create(*argc
, *argv
, notify_action_opt_descrs
);
1540 ERR("Failed to allocate an argpar iter.");
1545 enum parse_next_item_status status
;
1547 status
= parse_next_item(argpar_iter
, &argpar_item
,
1548 argc_offset
, *argv
, false,
1549 "While parsing `notify` action:");
1550 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1552 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1556 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1558 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1559 const struct argpar_opt_descr
*descr
=
1560 argpar_item_opt_descr(argpar_item
);
1561 const char *arg
= argpar_item_opt_arg(argpar_item
);
1563 switch (descr
->id
) {
1564 case OPT_RATE_POLICY
:
1566 policy
= parse_rate_policy(arg
);
1576 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1578 ERR("Unexpected argument `%s`.", arg
);
1583 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1584 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1586 action
= lttng_action_notify_create();
1588 ERR("Failed to create notify action");
1593 enum lttng_action_status status
;
1594 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1595 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1596 ERR("Failed to set rate policy");
1604 lttng_action_destroy(action
);
1607 lttng_rate_policy_destroy(policy
);
1608 argpar_item_destroy(argpar_item
);
1609 argpar_iter_destroy(argpar_iter
);
1614 * Generic handler for a kind of action that takes a session name and an
1615 * optional rate policy.
1618 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1621 struct lttng_action
*(*create_action_cb
)(void),
1622 enum lttng_action_status (*set_session_name_cb
)(
1623 struct lttng_action
*, const char *),
1624 enum lttng_action_status (*set_rate_policy_cb
)(
1625 struct lttng_action
*,
1626 const struct lttng_rate_policy
*),
1627 const char *action_name
)
1629 struct lttng_action
*action
= NULL
;
1630 struct argpar_iter
*argpar_iter
= NULL
;
1631 const struct argpar_item
*argpar_item
= NULL
;
1632 const char *session_name_arg
= NULL
;
1633 enum lttng_action_status action_status
;
1634 struct lttng_rate_policy
*policy
= NULL
;
1636 assert(set_session_name_cb
);
1637 assert(set_rate_policy_cb
);
1639 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1640 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1641 ARGPAR_OPT_DESCR_SENTINEL
1644 argpar_iter
= argpar_iter_create(*argc
, *argv
, rate_policy_opt_descrs
);
1646 ERR("Failed to allocate an argpar iter.");
1651 enum parse_next_item_status status
;
1653 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1655 "While parsing `%s` action:", action_name
);
1656 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1658 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1662 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1664 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1665 const struct argpar_opt_descr
*descr
=
1666 argpar_item_opt_descr(argpar_item
);
1667 const char *arg
= argpar_item_opt_arg(argpar_item
);
1669 switch (descr
->id
) {
1670 case OPT_RATE_POLICY
:
1672 policy
= parse_rate_policy(arg
);
1682 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1683 unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1687 session_name_arg
= arg
;
1690 ERR("Unexpected argument `%s`.", arg
);
1696 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1697 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1699 if (!session_name_arg
) {
1700 ERR("Missing session name.");
1704 action
= create_action_cb();
1706 ERR("Failed to allocate %s session action.", action_name
);
1710 action_status
= set_session_name_cb(action
, session_name_arg
);
1711 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1712 ERR("Failed to set action %s session's session name to '%s'.",
1713 action_name
, session_name_arg
);
1718 action_status
= set_rate_policy_cb(action
, policy
);
1719 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1720 ERR("Failed to set rate policy");
1728 lttng_action_destroy(action
);
1732 lttng_rate_policy_destroy(policy
);
1733 argpar_item_destroy(argpar_item
);
1734 argpar_iter_destroy(argpar_iter
);
1739 struct lttng_action
*handle_action_start_session(int *argc
,
1740 const char ***argv
, int argc_offset
)
1742 return handle_action_simple_session_with_policy(argc
, argv
,
1744 lttng_action_start_session_create
,
1745 lttng_action_start_session_set_session_name
,
1746 lttng_action_start_session_set_rate_policy
, "start");
1750 struct lttng_action
*handle_action_stop_session(int *argc
,
1751 const char ***argv
, int argc_offset
)
1753 return handle_action_simple_session_with_policy(argc
, argv
,
1755 lttng_action_stop_session_create
,
1756 lttng_action_stop_session_set_session_name
,
1757 lttng_action_stop_session_set_rate_policy
, "stop");
1761 struct lttng_action
*handle_action_rotate_session(int *argc
,
1762 const char ***argv
, int argc_offset
)
1764 return handle_action_simple_session_with_policy(argc
, argv
,
1766 lttng_action_rotate_session_create
,
1767 lttng_action_rotate_session_set_session_name
,
1768 lttng_action_rotate_session_set_rate_policy
,
1772 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1773 { OPT_NAME
, 'n', "name", true },
1774 { OPT_MAX_SIZE
, 'm', "max-size", true },
1775 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1776 { OPT_DATA_URL
, '\0', "data-url", true },
1777 { OPT_URL
, '\0', "url", true },
1778 { OPT_PATH
, '\0', "path", true },
1779 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1780 ARGPAR_OPT_DESCR_SENTINEL
1784 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1785 const char ***argv
, int argc_offset
)
1787 struct lttng_action
*action
= NULL
;
1788 struct argpar_iter
*argpar_iter
= NULL
;
1789 const struct argpar_item
*argpar_item
= NULL
;
1790 const char *session_name_arg
= NULL
;
1791 char *snapshot_name_arg
= NULL
;
1792 char *ctrl_url_arg
= NULL
;
1793 char *data_url_arg
= NULL
;
1794 char *max_size_arg
= NULL
;
1795 char *url_arg
= NULL
;
1796 char *path_arg
= NULL
;
1798 enum lttng_action_status action_status
;
1799 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1800 struct lttng_rate_policy
*policy
= NULL
;
1802 unsigned int locations_specified
= 0;
1804 argpar_iter
= argpar_iter_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1806 ERR("Failed to allocate an argpar iter.");
1811 enum parse_next_item_status status
;
1813 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1814 *argv
, false, "While parsing `snapshot` action:");
1815 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1817 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1821 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1823 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1824 const struct argpar_opt_descr
*descr
=
1825 argpar_item_opt_descr(argpar_item
);
1826 const char *arg
= argpar_item_opt_arg(argpar_item
);
1828 switch (descr
->id
) {
1830 if (!assign_string(&snapshot_name_arg
, arg
, "--name/-n")) {
1836 if (!assign_string(&max_size_arg
, arg
, "--max-size/-m")) {
1842 if (!assign_string(&ctrl_url_arg
, arg
, "--ctrl-url")) {
1848 if (!assign_string(&data_url_arg
, arg
, "--data-url")) {
1854 if (!assign_string(&url_arg
, arg
, "--url")) {
1860 if (!assign_string(&path_arg
, arg
, "--path")) {
1865 case OPT_RATE_POLICY
:
1867 policy
= parse_rate_policy(arg
);
1877 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1878 unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1882 session_name_arg
= arg
;
1885 ERR("Unexpected argument `%s`.", arg
);
1891 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1892 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1894 if (!session_name_arg
) {
1895 ERR("Missing session name.");
1899 /* --ctrl-url and --data-url must come in pair. */
1900 if (ctrl_url_arg
&& !data_url_arg
) {
1901 ERR("--ctrl-url is specified, but --data-url is missing.");
1905 if (!ctrl_url_arg
&& data_url_arg
) {
1906 ERR("--data-url is specified, but --ctrl-url is missing.");
1910 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1911 locations_specified
+= !!url_arg
;
1912 locations_specified
+= !!path_arg
;
1914 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1915 if (locations_specified
> 1) {
1916 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1921 * Did the user specify an option that implies using a
1922 * custom/unregistered output?
1924 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1925 snapshot_output
= lttng_snapshot_output_create();
1926 if (!snapshot_output
) {
1927 ERR("Failed to allocate a snapshot output.");
1932 action
= lttng_action_snapshot_session_create();
1934 ERR("Failed to allocate snapshot session action.");
1938 action_status
= lttng_action_snapshot_session_set_session_name(
1939 action
, session_name_arg
);
1940 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1941 ERR("Failed to set action snapshot session's session name to '%s'.",
1946 if (snapshot_name_arg
) {
1947 if (!snapshot_output
) {
1948 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1952 ret
= lttng_snapshot_output_set_name(
1953 snapshot_name_arg
, snapshot_output
);
1955 ERR("Failed to set name of snapshot output.");
1963 if (!snapshot_output
) {
1964 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1968 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1970 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1974 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1976 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1984 struct lttng_uri
*uris
;
1986 if (!strstr(url_arg
, "://")) {
1987 ERR("Failed to parse '%s' as an URL.", url_arg
);
1991 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1993 ERR("Failed to parse '%s' as an URL.", url_arg
);
1997 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1998 ret
= lttng_snapshot_output_set_local_path(
1999 uris
[0].dst
.path
, snapshot_output
);
2002 ERR("Failed to assign '%s' as a local destination.",
2007 ret
= lttng_snapshot_output_set_network_url(
2008 url_arg
, snapshot_output
);
2011 ERR("Failed to assign '%s' as a network URL.",
2019 ret
= lttng_snapshot_output_set_local_path(
2020 path_arg
, snapshot_output
);
2022 ERR("Failed to parse '%s' as a local path.", path_arg
);
2029 * Two argument form, network output with separate control and
2032 ret
= lttng_snapshot_output_set_network_urls(
2033 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2035 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2036 ctrl_url_arg
, data_url_arg
);
2041 if (snapshot_output
) {
2042 action_status
= lttng_action_snapshot_session_set_output(
2043 action
, snapshot_output
);
2044 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2045 ERR("Failed to set snapshot session action's output.");
2049 /* Ownership of `snapshot_output` has been transferred to the action. */
2050 snapshot_output
= NULL
;
2054 enum lttng_action_status status
;
2055 status
= lttng_action_snapshot_session_set_rate_policy(
2057 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2058 ERR("Failed to set rate policy");
2066 lttng_action_destroy(action
);
2070 free(snapshot_name_arg
);
2075 free(snapshot_output
);
2077 lttng_rate_policy_destroy(policy
);
2078 argpar_item_destroy(argpar_item
);
2079 argpar_iter_destroy(argpar_iter
);
2083 struct action_descr
{
2085 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
,
2090 struct action_descr action_descrs
[] = {
2091 { "notify", handle_action_notify
},
2092 { "start-session", handle_action_start_session
},
2093 { "stop-session", handle_action_stop_session
},
2094 { "rotate-session", handle_action_rotate_session
},
2095 { "snapshot-session", handle_action_snapshot_session
},
2099 struct lttng_action
*parse_action(const char *action_name
, int *argc
,
2100 const char ***argv
, int argc_offset
)
2103 struct lttng_action
*action
;
2104 const struct action_descr
*descr
= NULL
;
2106 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2107 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2108 descr
= &action_descrs
[i
];
2114 ERR("Unknown action name: %s", action_name
);
2118 action
= descr
->handler(argc
, argv
, argc_offset
);
2120 /* The handler has already printed an error message. */
2132 struct argpar_opt_descr add_trigger_options
[] = {
2133 { OPT_HELP
, 'h', "help", false },
2134 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2135 { OPT_CONDITION
, '\0', "condition", true },
2136 { OPT_ACTION
, '\0', "action", true },
2137 { OPT_NAME
, '\0', "name", true },
2138 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2139 ARGPAR_OPT_DESCR_SENTINEL
,
2143 void lttng_actions_destructor(void *p
)
2145 struct lttng_action
*action
= p
;
2147 lttng_action_destroy(action
);
2150 int cmd_add_trigger(int argc
, const char **argv
)
2153 int my_argc
= argc
- 1;
2154 const char **my_argv
= argv
+ 1;
2155 struct lttng_condition
*condition
= NULL
;
2156 struct lttng_dynamic_pointer_array actions
;
2157 struct argpar_iter
*argpar_iter
= NULL
;
2158 const struct argpar_item
*argpar_item
= NULL
;
2159 struct lttng_action
*action_list
= NULL
;
2160 struct lttng_action
*action
= NULL
;
2161 struct lttng_trigger
*trigger
= NULL
;
2164 char *owner_uid
= NULL
;
2165 enum lttng_error_code ret_code
;
2166 struct mi_writer
*mi_writer
= NULL
;
2168 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2171 mi_writer
= mi_lttng_writer_create(
2172 fileno(stdout
), lttng_opt_mi
);
2178 /* Open command element. */
2179 ret
= mi_lttng_writer_command_open(mi_writer
,
2180 mi_lttng_element_command_add_trigger
);
2186 /* Open output element. */
2187 ret
= mi_lttng_writer_open_element(
2188 mi_writer
, mi_lttng_element_command_output
);
2196 enum parse_next_item_status status
;
2198 const struct argpar_opt_descr
*descr
;
2201 argpar_iter_destroy(argpar_iter
);
2202 argpar_iter
= argpar_iter_create(my_argc
, my_argv
,
2203 add_trigger_options
);
2205 ERR("Failed to create argpar iter.");
2209 status
= parse_next_item(argpar_iter
, &argpar_item
,
2210 argc
- my_argc
, my_argv
, true, NULL
);
2211 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
2213 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
2217 assert(status
== PARSE_NEXT_ITEM_STATUS_OK
);
2219 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_NON_OPT
) {
2220 ERR("Unexpected argument `%s`.",
2221 argpar_item_non_opt_arg(argpar_item
));
2225 ingested_args
= argpar_iter_ingested_orig_args(argpar_iter
);
2227 my_argc
-= ingested_args
;
2228 my_argv
+= ingested_args
;
2230 descr
= argpar_item_opt_descr(argpar_item
);
2231 arg
= argpar_item_opt_arg(argpar_item
);
2233 switch (descr
->id
) {
2238 case OPT_LIST_OPTIONS
:
2239 list_cmd_options_argpar(stdout
, add_trigger_options
);
2245 ERR("A --condition was already given.");
2249 condition
= parse_condition(arg
, &my_argc
, &my_argv
,
2253 * An error message was already printed by
2263 action
= parse_action(arg
, &my_argc
, &my_argv
,
2267 * An error message was already printed by
2273 ret
= lttng_dynamic_pointer_array_add_pointer(
2276 ERR("Failed to add pointer to pointer array.");
2280 /* Ownership of the action was transferred to the list. */
2287 if (!assign_string(&name
, arg
, "--name")) {
2295 if (!assign_string(&owner_uid
, arg
,
2308 ERR("Missing --condition.");
2312 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2313 ERR("Need at least one --action.");
2317 action_list
= lttng_action_list_create();
2322 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2323 enum lttng_action_status status
;
2325 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2327 status
= lttng_action_list_add_action(action_list
, action
);
2328 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2333 * The `lttng_action_list_add_action()` takes a reference to
2334 * the action. We can destroy ours.
2336 lttng_action_destroy(action
);
2340 trigger
= lttng_trigger_create(condition
, action_list
);
2346 enum lttng_trigger_status trigger_status
;
2351 uid
= strtol(owner_uid
, &end
, 10);
2352 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2353 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2357 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2358 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2359 ERR("Failed to set trigger's user identity.");
2365 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2367 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2370 if (ret_code
!= LTTNG_OK
) {
2371 ERR("Failed to register trigger: %s.",
2372 lttng_strerror(-ret_code
));
2377 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2378 if (ret_code
!= LTTNG_OK
) {
2382 const char *returned_trigger_name
;
2383 const enum lttng_trigger_status trigger_status
=
2384 lttng_trigger_get_name(trigger
,
2385 &returned_trigger_name
);
2387 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2388 WARN("Failed to retrieve the added trigger's name.");
2390 MSG("Added trigger `%s`.", returned_trigger_name
);
2403 if (lttng_opt_mi
&& mi_writer
) {
2406 /* Close output element. */
2407 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2413 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2414 mi_lttng_element_command_success
, ret
? 0 : 1);
2420 /* Command element close. */
2421 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2429 argpar_iter_destroy(argpar_iter
);
2430 argpar_item_destroy(argpar_item
);
2431 lttng_dynamic_pointer_array_reset(&actions
);
2432 lttng_condition_destroy(condition
);
2433 lttng_action_destroy(action_list
);
2434 lttng_action_destroy(action
);
2435 lttng_trigger_destroy(trigger
);
2438 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2439 /* Preserve original error code. */
2440 ret
= ret
? ret
: CMD_ERROR
;