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 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
);
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 LTTNG_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 LTTNG_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 (const char *) 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 LTTNG_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_event_expr
**) lttng_dynamic_array_get_element(
1376 &res
.capture_descriptors
.array
, i
);
1379 LTTNG_ASSERT(*expr
);
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
, int orig_arg_index
,
1420 const char *orig_arg
)
1423 struct lttng_condition
*cond
;
1424 const struct condition_descr
*descr
= NULL
;
1426 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1427 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1428 descr
= &condition_descrs
[i
];
1434 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown condition name '%s'",
1435 orig_arg_index
+ 1, orig_arg
, condition_name
);
1439 cond
= descr
->handler(argc
, argv
, argc_offset
);
1441 /* The handler has already printed an error message. */
1452 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1455 size_t num_token
= 0;
1456 struct lttng_dynamic_pointer_array tokens
;
1457 struct lttng_rate_policy
*policy
= NULL
;
1458 enum lttng_rate_policy_type policy_type
;
1459 unsigned long long value
;
1460 char *policy_type_str
;
1461 char *policy_value_str
;
1463 LTTNG_ASSERT(policy_str
);
1464 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1466 /* Rate policy fields are separated by ':'. */
1467 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1469 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1473 * Early sanity check that the number of parameter is exactly 2.
1476 if (num_token
!= 2) {
1477 ERR("Rate policy format is invalid.");
1481 policy_type_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1482 policy_value_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1484 /* Parse the type. */
1485 if (strcmp(policy_type_str
, "once-after") == 0) {
1486 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1487 } else if (strcmp(policy_type_str
, "every") == 0) {
1488 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1490 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1494 /* Parse the value. */
1495 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1496 ERR("Failed to parse rate policy value `%s` as an integer.",
1502 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1506 switch (policy_type
) {
1507 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1508 policy
= lttng_rate_policy_every_n_create(value
);
1510 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1511 policy
= lttng_rate_policy_once_after_n_create(value
);
1517 if (policy
== NULL
) {
1518 ERR("Failed to create rate policy `%s`.", policy_str
);
1522 lttng_dynamic_pointer_array_reset(&tokens
);
1526 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1527 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1528 ARGPAR_OPT_DESCR_SENTINEL
1532 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
,
1535 struct lttng_action
*action
= NULL
;
1536 struct argpar_iter
*argpar_iter
= NULL
;
1537 const struct argpar_item
*argpar_item
= NULL
;
1538 struct lttng_rate_policy
*policy
= NULL
;
1540 argpar_iter
= argpar_iter_create(*argc
, *argv
, notify_action_opt_descrs
);
1542 ERR("Failed to allocate an argpar iter.");
1547 enum parse_next_item_status status
;
1549 status
= parse_next_item(argpar_iter
, &argpar_item
,
1550 argc_offset
, *argv
, false,
1551 "While parsing `notify` action:");
1552 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1554 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1558 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1560 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1561 const struct argpar_opt_descr
*descr
=
1562 argpar_item_opt_descr(argpar_item
);
1563 const char *arg
= argpar_item_opt_arg(argpar_item
);
1565 switch (descr
->id
) {
1566 case OPT_RATE_POLICY
:
1568 policy
= parse_rate_policy(arg
);
1578 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1580 ERR("Unexpected argument `%s`.", arg
);
1585 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1586 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1588 action
= lttng_action_notify_create();
1590 ERR("Failed to create notify action");
1595 enum lttng_action_status status
;
1596 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1597 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1598 ERR("Failed to set rate policy");
1606 lttng_action_destroy(action
);
1609 lttng_rate_policy_destroy(policy
);
1610 argpar_item_destroy(argpar_item
);
1611 argpar_iter_destroy(argpar_iter
);
1616 * Generic handler for a kind of action that takes a session name and an
1617 * optional rate policy.
1620 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1623 struct lttng_action
*(*create_action_cb
)(void),
1624 enum lttng_action_status (*set_session_name_cb
)(
1625 struct lttng_action
*, const char *),
1626 enum lttng_action_status (*set_rate_policy_cb
)(
1627 struct lttng_action
*,
1628 const struct lttng_rate_policy
*),
1629 const char *action_name
)
1631 struct lttng_action
*action
= NULL
;
1632 struct argpar_iter
*argpar_iter
= NULL
;
1633 const struct argpar_item
*argpar_item
= NULL
;
1634 const char *session_name_arg
= NULL
;
1635 enum lttng_action_status action_status
;
1636 struct lttng_rate_policy
*policy
= NULL
;
1638 LTTNG_ASSERT(set_session_name_cb
);
1639 LTTNG_ASSERT(set_rate_policy_cb
);
1641 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1642 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1643 ARGPAR_OPT_DESCR_SENTINEL
1646 argpar_iter
= argpar_iter_create(*argc
, *argv
, rate_policy_opt_descrs
);
1648 ERR("Failed to allocate an argpar iter.");
1653 enum parse_next_item_status status
;
1655 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1657 "While parsing `%s` action:", action_name
);
1658 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1660 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1664 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1666 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1667 const struct argpar_opt_descr
*descr
=
1668 argpar_item_opt_descr(argpar_item
);
1669 const char *arg
= argpar_item_opt_arg(argpar_item
);
1671 switch (descr
->id
) {
1672 case OPT_RATE_POLICY
:
1674 policy
= parse_rate_policy(arg
);
1684 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1685 unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1689 session_name_arg
= arg
;
1692 ERR("Unexpected argument `%s`.", arg
);
1698 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1699 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1701 if (!session_name_arg
) {
1702 ERR("Missing session name.");
1706 action
= create_action_cb();
1708 ERR("Failed to allocate %s session action.", action_name
);
1712 action_status
= set_session_name_cb(action
, session_name_arg
);
1713 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1714 ERR("Failed to set action %s session's session name to '%s'.",
1715 action_name
, session_name_arg
);
1720 action_status
= set_rate_policy_cb(action
, policy
);
1721 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1722 ERR("Failed to set rate policy");
1730 lttng_action_destroy(action
);
1734 lttng_rate_policy_destroy(policy
);
1735 argpar_item_destroy(argpar_item
);
1736 argpar_iter_destroy(argpar_iter
);
1741 struct lttng_action
*handle_action_start_session(int *argc
,
1742 const char ***argv
, int argc_offset
)
1744 return handle_action_simple_session_with_policy(argc
, argv
,
1746 lttng_action_start_session_create
,
1747 lttng_action_start_session_set_session_name
,
1748 lttng_action_start_session_set_rate_policy
, "start");
1752 struct lttng_action
*handle_action_stop_session(int *argc
,
1753 const char ***argv
, int argc_offset
)
1755 return handle_action_simple_session_with_policy(argc
, argv
,
1757 lttng_action_stop_session_create
,
1758 lttng_action_stop_session_set_session_name
,
1759 lttng_action_stop_session_set_rate_policy
, "stop");
1763 struct lttng_action
*handle_action_rotate_session(int *argc
,
1764 const char ***argv
, int argc_offset
)
1766 return handle_action_simple_session_with_policy(argc
, argv
,
1768 lttng_action_rotate_session_create
,
1769 lttng_action_rotate_session_set_session_name
,
1770 lttng_action_rotate_session_set_rate_policy
,
1774 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1775 { OPT_NAME
, 'n', "name", true },
1776 { OPT_MAX_SIZE
, 'm', "max-size", true },
1777 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1778 { OPT_DATA_URL
, '\0', "data-url", true },
1779 { OPT_URL
, '\0', "url", true },
1780 { OPT_PATH
, '\0', "path", true },
1781 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1782 ARGPAR_OPT_DESCR_SENTINEL
1786 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1787 const char ***argv
, int argc_offset
)
1789 struct lttng_action
*action
= NULL
;
1790 struct argpar_iter
*argpar_iter
= NULL
;
1791 const struct argpar_item
*argpar_item
= NULL
;
1792 const char *session_name_arg
= NULL
;
1793 char *snapshot_name_arg
= NULL
;
1794 char *ctrl_url_arg
= NULL
;
1795 char *data_url_arg
= NULL
;
1796 char *max_size_arg
= NULL
;
1797 char *url_arg
= NULL
;
1798 char *path_arg
= NULL
;
1800 enum lttng_action_status action_status
;
1801 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1802 struct lttng_rate_policy
*policy
= NULL
;
1804 unsigned int locations_specified
= 0;
1806 argpar_iter
= argpar_iter_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1808 ERR("Failed to allocate an argpar iter.");
1813 enum parse_next_item_status status
;
1815 status
= parse_next_item(argpar_iter
, &argpar_item
, argc_offset
,
1816 *argv
, false, "While parsing `snapshot` action:");
1817 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
1819 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
1823 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
1825 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_OPT
) {
1826 const struct argpar_opt_descr
*descr
=
1827 argpar_item_opt_descr(argpar_item
);
1828 const char *arg
= argpar_item_opt_arg(argpar_item
);
1830 switch (descr
->id
) {
1832 if (!assign_string(&snapshot_name_arg
, arg
, "--name/-n")) {
1838 if (!assign_string(&max_size_arg
, arg
, "--max-size/-m")) {
1844 if (!assign_string(&ctrl_url_arg
, arg
, "--ctrl-url")) {
1850 if (!assign_string(&data_url_arg
, arg
, "--data-url")) {
1856 if (!assign_string(&url_arg
, arg
, "--url")) {
1862 if (!assign_string(&path_arg
, arg
, "--path")) {
1867 case OPT_RATE_POLICY
:
1869 policy
= parse_rate_policy(arg
);
1879 const char *arg
= argpar_item_non_opt_arg(argpar_item
);
1880 const unsigned int idx
= argpar_item_non_opt_non_opt_index(argpar_item
);
1884 session_name_arg
= arg
;
1887 ERR("Unexpected argument `%s`.", arg
);
1893 *argc
-= argpar_iter_ingested_orig_args(argpar_iter
);
1894 *argv
+= argpar_iter_ingested_orig_args(argpar_iter
);
1896 if (!session_name_arg
) {
1897 ERR("Missing session name.");
1901 /* --ctrl-url and --data-url must come in pair. */
1902 if (ctrl_url_arg
&& !data_url_arg
) {
1903 ERR("--ctrl-url is specified, but --data-url is missing.");
1907 if (!ctrl_url_arg
&& data_url_arg
) {
1908 ERR("--data-url is specified, but --ctrl-url is missing.");
1912 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1913 locations_specified
+= !!url_arg
;
1914 locations_specified
+= !!path_arg
;
1916 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1917 if (locations_specified
> 1) {
1918 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1923 * Did the user specify an option that implies using a
1924 * custom/unregistered output?
1926 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1927 snapshot_output
= lttng_snapshot_output_create();
1928 if (!snapshot_output
) {
1929 ERR("Failed to allocate a snapshot output.");
1934 action
= lttng_action_snapshot_session_create();
1936 ERR("Failed to allocate snapshot session action.");
1940 action_status
= lttng_action_snapshot_session_set_session_name(
1941 action
, session_name_arg
);
1942 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1943 ERR("Failed to set action snapshot session's session name to '%s'.",
1948 if (snapshot_name_arg
) {
1949 if (!snapshot_output
) {
1950 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1954 ret
= lttng_snapshot_output_set_name(
1955 snapshot_name_arg
, snapshot_output
);
1957 ERR("Failed to set name of snapshot output.");
1965 if (!snapshot_output
) {
1966 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1970 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1972 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1976 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1978 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1986 struct lttng_uri
*uris
;
1988 if (!strstr(url_arg
, "://")) {
1989 ERR("Failed to parse '%s' as an URL.", url_arg
);
1993 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1995 ERR("Failed to parse '%s' as an URL.", url_arg
);
1999 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2000 ret
= lttng_snapshot_output_set_local_path(
2001 uris
[0].dst
.path
, snapshot_output
);
2004 ERR("Failed to assign '%s' as a local destination.",
2009 ret
= lttng_snapshot_output_set_network_url(
2010 url_arg
, snapshot_output
);
2013 ERR("Failed to assign '%s' as a network URL.",
2021 ret
= lttng_snapshot_output_set_local_path(
2022 path_arg
, snapshot_output
);
2024 ERR("Failed to parse '%s' as a local path.", path_arg
);
2031 * Two argument form, network output with separate control and
2034 ret
= lttng_snapshot_output_set_network_urls(
2035 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2037 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2038 ctrl_url_arg
, data_url_arg
);
2043 if (snapshot_output
) {
2044 action_status
= lttng_action_snapshot_session_set_output(
2045 action
, snapshot_output
);
2046 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2047 ERR("Failed to set snapshot session action's output.");
2051 /* Ownership of `snapshot_output` has been transferred to the action. */
2052 snapshot_output
= NULL
;
2056 enum lttng_action_status status
;
2057 status
= lttng_action_snapshot_session_set_rate_policy(
2059 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2060 ERR("Failed to set rate policy");
2068 lttng_action_destroy(action
);
2072 free(snapshot_name_arg
);
2077 free(snapshot_output
);
2079 lttng_rate_policy_destroy(policy
);
2080 argpar_item_destroy(argpar_item
);
2081 argpar_iter_destroy(argpar_iter
);
2085 struct action_descr
{
2087 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
,
2092 struct action_descr action_descrs
[] = {
2093 { "notify", handle_action_notify
},
2094 { "start-session", handle_action_start_session
},
2095 { "stop-session", handle_action_stop_session
},
2096 { "rotate-session", handle_action_rotate_session
},
2097 { "snapshot-session", handle_action_snapshot_session
},
2101 struct lttng_action
*parse_action(const char *action_name
, int *argc
,
2102 const char ***argv
, int argc_offset
, int orig_arg_index
,
2103 const char *orig_arg
)
2106 struct lttng_action
*action
;
2107 const struct action_descr
*descr
= NULL
;
2109 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2110 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2111 descr
= &action_descrs
[i
];
2117 ERR(WHILE_PARSING_ARG_N_ARG_FMT
"Unknown action name: %s",
2118 orig_arg_index
+ 1, orig_arg
, action_name
);
2122 action
= descr
->handler(argc
, argv
, argc_offset
);
2124 /* The handler has already printed an error message. */
2136 struct argpar_opt_descr add_trigger_options
[] = {
2137 { OPT_HELP
, 'h', "help", false },
2138 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2139 { OPT_CONDITION
, '\0', "condition", true },
2140 { OPT_ACTION
, '\0', "action", true },
2141 { OPT_NAME
, '\0', "name", true },
2142 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2143 ARGPAR_OPT_DESCR_SENTINEL
,
2147 void lttng_actions_destructor(void *p
)
2149 struct lttng_action
*action
= (lttng_action
*) p
;
2151 lttng_action_destroy(action
);
2154 int cmd_add_trigger(int argc
, const char **argv
)
2157 int my_argc
= argc
- 1;
2158 const char **my_argv
= argv
+ 1;
2159 struct lttng_condition
*condition
= NULL
;
2160 struct lttng_dynamic_pointer_array actions
;
2161 struct argpar_iter
*argpar_iter
= NULL
;
2162 const struct argpar_item
*argpar_item
= NULL
;
2163 struct lttng_action
*action_list
= NULL
;
2164 struct lttng_action
*action
= NULL
;
2165 struct lttng_trigger
*trigger
= NULL
;
2168 char *owner_uid
= NULL
;
2169 enum lttng_error_code ret_code
;
2170 struct mi_writer
*mi_writer
= NULL
;
2172 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2175 mi_writer
= mi_lttng_writer_create(
2176 fileno(stdout
), lttng_opt_mi
);
2182 /* Open command element. */
2183 ret
= mi_lttng_writer_command_open(mi_writer
,
2184 mi_lttng_element_command_add_trigger
);
2190 /* Open output element. */
2191 ret
= mi_lttng_writer_open_element(
2192 mi_writer
, mi_lttng_element_command_output
);
2200 enum parse_next_item_status status
;
2202 const struct argpar_opt_descr
*descr
;
2205 argpar_iter_destroy(argpar_iter
);
2206 argpar_iter
= argpar_iter_create(my_argc
, my_argv
,
2207 add_trigger_options
);
2209 ERR("Failed to create argpar iter.");
2213 status
= parse_next_item(argpar_iter
, &argpar_item
,
2214 argc
- my_argc
, my_argv
, true, NULL
);
2215 if (status
== PARSE_NEXT_ITEM_STATUS_ERROR
) {
2217 } else if (status
== PARSE_NEXT_ITEM_STATUS_END
) {
2221 LTTNG_ASSERT(status
== PARSE_NEXT_ITEM_STATUS_OK
);
2223 if (argpar_item_type(argpar_item
) == ARGPAR_ITEM_TYPE_NON_OPT
) {
2224 ERR("Unexpected argument `%s`.",
2225 argpar_item_non_opt_arg(argpar_item
));
2229 ingested_args
= argpar_iter_ingested_orig_args(argpar_iter
);
2231 my_argc
-= ingested_args
;
2232 my_argv
+= ingested_args
;
2234 descr
= argpar_item_opt_descr(argpar_item
);
2235 arg
= argpar_item_opt_arg(argpar_item
);
2237 switch (descr
->id
) {
2242 case OPT_LIST_OPTIONS
:
2243 list_cmd_options_argpar(stdout
, add_trigger_options
);
2249 ERR("A --condition was already given.");
2253 condition
= parse_condition(arg
, &my_argc
, &my_argv
,
2254 argc
- my_argc
, argc
- my_argc
- ingested_args
,
2255 my_argv
[-ingested_args
]);
2258 * An error message was already printed by
2268 action
= parse_action(arg
, &my_argc
, &my_argv
,
2269 argc
- my_argc
, argc
- my_argc
- ingested_args
,
2270 my_argv
[-ingested_args
]);
2273 * An error message was already printed by
2279 ret
= lttng_dynamic_pointer_array_add_pointer(
2282 ERR("Failed to add pointer to pointer array.");
2286 /* Ownership of the action was transferred to the list. */
2293 if (!assign_string(&name
, arg
, "--name")) {
2301 if (!assign_string(&owner_uid
, arg
,
2314 ERR("Missing --condition.");
2318 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2319 ERR("Need at least one --action.");
2323 action_list
= lttng_action_list_create();
2328 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2329 enum lttng_action_status status
;
2331 action
= (lttng_action
*) lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2333 status
= lttng_action_list_add_action(action_list
, action
);
2334 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2339 * The `lttng_action_list_add_action()` takes a reference to
2340 * the action. We can destroy ours.
2342 lttng_action_destroy(action
);
2346 trigger
= lttng_trigger_create(condition
, action_list
);
2352 enum lttng_trigger_status trigger_status
;
2357 uid
= strtol(owner_uid
, &end
, 10);
2358 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2359 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2363 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2364 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2365 ERR("Failed to set trigger's user identity.");
2371 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2373 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2376 if (ret_code
!= LTTNG_OK
) {
2377 ERR("Failed to register trigger: %s.",
2378 lttng_strerror(-ret_code
));
2383 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2384 if (ret_code
!= LTTNG_OK
) {
2388 const char *returned_trigger_name
;
2389 const enum lttng_trigger_status trigger_status
=
2390 lttng_trigger_get_name(trigger
,
2391 &returned_trigger_name
);
2393 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2394 WARN("Failed to retrieve the added trigger's name.");
2396 MSG("Added trigger `%s`.", returned_trigger_name
);
2409 if (lttng_opt_mi
&& mi_writer
) {
2412 /* Close output element. */
2413 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2419 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2420 mi_lttng_element_command_success
, ret
? 0 : 1);
2426 /* Command element close. */
2427 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2435 argpar_iter_destroy(argpar_iter
);
2436 argpar_item_destroy(argpar_item
);
2437 lttng_dynamic_pointer_array_reset(&actions
);
2438 lttng_condition_destroy(condition
);
2439 lttng_action_destroy(action_list
);
2440 lttng_action_destroy(action
);
2441 lttng_trigger_destroy(trigger
);
2444 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2445 /* Preserve original error code. */
2446 ret
= ret
? ret
: CMD_ERROR
;