2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include "../command.h"
13 #include "../loglevel.h"
14 #include "../uprobe.h"
16 #include "common/argpar/argpar.h"
17 #include "common/dynamic-array.h"
18 #include "common/mi-lttng.h"
19 #include "common/string-utils/string-utils.h"
20 #include "common/utils.h"
21 #include <lttng/domain-internal.h>
22 /* For lttng_event_rule_type_str(). */
23 #include <lttng/event-rule/event-rule-internal.h>
24 #include <lttng/lttng.h>
25 #include "common/filter/filter-ast.h"
26 #include "common/filter/filter-ir.h"
27 #include "common/dynamic-array.h"
29 #if (LTTNG_SYMBOL_NAME_LEN == 256)
30 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
33 #ifdef LTTNG_EMBED_HELP
34 static const char help_msg
[] =
35 #include <lttng-add-trigger.1.h>
67 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
68 { OPT_FILTER
, 'f', "filter", true },
69 { OPT_NAME
, 'n', "name", true },
70 { OPT_EXCLUDE_NAME
, 'x', "exclude-name", true },
71 { OPT_LOG_LEVEL
, 'l', "log-level", true },
72 { OPT_EVENT_NAME
, 'E', "event-name", true },
74 { OPT_TYPE
, 't', "type", true },
75 { OPT_LOCATION
, 'L', "location", true },
77 /* Capture descriptor */
78 { OPT_CAPTURE
, '\0', "capture", true },
80 ARGPAR_OPT_DESCR_SENTINEL
84 bool has_syscall_prefix(const char *arg
)
87 const char kernel_syscall_type_opt_prefix
[] = "kernel:syscall";
88 const size_t kernel_syscall_type_opt_prefix_len
=
89 sizeof(kernel_syscall_type_opt_prefix
) - 1;
90 const char syscall_type_opt_prefix
[] = "syscall";
91 const size_t syscall_type_opt_prefix_len
=
92 sizeof(syscall_type_opt_prefix
) - 1;
94 if (strncmp(arg
, syscall_type_opt_prefix
,
95 syscall_type_opt_prefix_len
) == 0) {
97 } else if (strncmp(arg
, kernel_syscall_type_opt_prefix
,
98 kernel_syscall_type_opt_prefix_len
) == 0) {
108 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
112 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
113 ERR("More than one `--type` was specified.");
117 if (strcmp(arg
, "user") == 0 || strcmp(arg
, "user:tracepoint") == 0) {
118 *dest
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
119 } else if (strcmp(arg
, "kernel") == 0 ||
120 strcmp(arg
, "kernel:tracepoint") == 0) {
121 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
;
122 } else if (strcmp(arg
, "jul") == 0 || strcmp(arg
, "jul:logging") == 0) {
123 *dest
= LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
;
124 } else if (strcmp(arg
, "log4j") == 0 ||
125 strcmp(arg
, "log4j:logging") == 0) {
126 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
;
127 } else if (strcmp(arg
, "python") == 0 ||
128 strcmp(arg
, "python:logging") == 0) {
129 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
130 } else if (strcmp(arg
, "kprobe") == 0 ||
131 strcmp(arg
, "kernel:kprobe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
133 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
134 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
135 } else if (has_syscall_prefix(arg
)) {
137 * Matches the following:
141 * - syscall:entry+exit
144 * - kernel:syscall:entry
145 * - kernel:syscall:exit
146 * - kernel:syscall:entry+exit
149 * Validation for the right side is left to further usage sites.
151 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
153 ERR("Invalid `--type` value: %s", arg
);
168 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
173 ERR("Duplicate '%s' given.", opt_name
);
179 PERROR("Failed to allocate string '%s'.", opt_name
);
193 static bool parse_syscall_emission_site_from_type(const char *str
,
194 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
197 const char kernel_prefix
[] = "kernel:";
198 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
201 * If the passed string is of the form "kernel:syscall*", move the
202 * pointer passed "kernel:".
204 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
205 str
= &str
[kernel_prefix_len
];
208 if (strcmp(str
, "syscall") == 0 ||
209 strcmp(str
, "syscall:entry+exit") == 0) {
210 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
211 } else if (strcmp(str
, "syscall:entry") == 0) {
212 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
213 } else if (strcmp(str
, "syscall:exit") == 0) {
214 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
226 * Parse `str` as a log level against the passed event rule type.
228 * Return the log level in `*log_level`. Return true in `*log_level_only` if
229 * the string specifies exactly this log level, false if it specifies at least
232 * Return true if the string was successfully parsed as a log level string.
234 static bool parse_log_level_string(const char *str
,
235 enum lttng_event_rule_type event_rule_type
,
237 bool *log_level_only
)
241 switch (event_rule_type
) {
242 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
244 enum lttng_loglevel log_level_min
, log_level_max
;
245 if (!loglevel_parse_range_string(
246 str
, &log_level_min
, &log_level_max
)) {
250 /* Only support VAL and VAL.. for now. */
251 if (log_level_min
!= log_level_max
&&
252 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
256 *log_level
= (int) log_level_min
;
257 *log_level_only
= log_level_min
== log_level_max
;
260 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
262 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
263 if (!loglevel_log4j_parse_range_string(
264 str
, &log_level_min
, &log_level_max
)) {
268 /* Only support VAL and VAL.. for now. */
269 if (log_level_min
!= log_level_max
&&
270 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
274 *log_level
= (int) log_level_min
;
275 *log_level_only
= log_level_min
== log_level_max
;
278 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
280 enum lttng_loglevel_jul log_level_min
, log_level_max
;
281 if (!loglevel_jul_parse_range_string(
282 str
, &log_level_min
, &log_level_max
)) {
286 /* Only support VAL and VAL.. for now. */
287 if (log_level_min
!= log_level_max
&&
288 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
292 *log_level
= (int) log_level_min
;
293 *log_level_only
= log_level_min
== log_level_max
;
296 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
298 enum lttng_loglevel_python log_level_min
, log_level_max
;
299 if (!loglevel_python_parse_range_string(
300 str
, &log_level_min
, &log_level_max
)) {
304 /* Only support VAL and VAL.. for now. */
305 if (log_level_min
!= log_level_max
&&
307 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
311 *log_level
= (int) log_level_min
;
312 *log_level_only
= log_level_min
== log_level_max
;
316 /* Invalid domain type. */
330 static int parse_kernel_probe_opts(const char *source
,
331 struct lttng_kernel_probe_location
**location
)
336 char name
[LTTNG_SYMBOL_NAME_LEN
];
337 char *symbol_name
= NULL
;
340 /* Check for symbol+offset. */
341 match
= sscanf(source
,
342 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
346 if (*s_hex
== '\0') {
347 ERR("Kernel probe symbol offset is missing.");
351 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
353 PERROR("Failed to copy kernel probe location symbol name.");
356 offset
= strtoul(s_hex
, NULL
, 0);
358 *location
= lttng_kernel_probe_location_symbol_create(
359 symbol_name
, offset
);
361 ERR("Failed to create symbol kernel probe location.");
368 /* Check for symbol. */
369 if (isalpha(name
[0]) || name
[0] == '_') {
370 match
= sscanf(source
,
371 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
375 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
377 ERR("Failed to copy kernel probe location symbol name.");
381 *location
= lttng_kernel_probe_location_symbol_create(
384 ERR("Failed to create symbol kernel probe location.");
392 /* Check for address. */
393 match
= sscanf(source
, "%18s", s_hex
);
397 if (*s_hex
== '\0') {
398 ERR("Invalid kernel probe location address.");
402 address
= strtoul(s_hex
, NULL
, 0);
403 *location
= lttng_kernel_probe_location_address_create(address
);
405 ERR("Failed to create symbol kernel probe location.");
423 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
424 const struct ir_load_expression
*load_expr
,
425 const char *capture_str
)
427 char *provider_name
= NULL
;
428 struct lttng_event_expr
*event_expr
= NULL
;
429 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
430 const enum ir_load_expression_type load_expr_child_type
=
433 switch (load_expr_child_type
) {
434 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
435 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
437 const char *field_name
;
439 load_expr_op
= load_expr_op
->next
;
440 assert(load_expr_op
);
441 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
442 field_name
= load_expr_op
->u
.symbol
;
445 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
446 lttng_event_expr_event_payload_field_create(field_name
) :
447 lttng_event_expr_channel_context_field_create(field_name
);
449 ERR("Failed to create %s event expression: field name = `%s`.",
450 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
451 "payload field" : "channel context",
458 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
461 const char *type_name
;
462 const char *field_name
;
464 load_expr_op
= load_expr_op
->next
;
465 assert(load_expr_op
);
466 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
467 field_name
= load_expr_op
->u
.symbol
;
471 * The field name needs to be of the form PROVIDER:TYPE. We
474 colon
= strchr(field_name
, ':');
476 ERR("Invalid app-specific context field name: missing colon in `%s`.",
481 type_name
= colon
+ 1;
482 if (*type_name
== '\0') {
483 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
488 provider_name
= strndup(field_name
, colon
- field_name
);
489 if (!provider_name
) {
490 PERROR("Failed to allocate field name string");
494 event_expr
= lttng_event_expr_app_specific_context_field_create(
495 provider_name
, type_name
);
497 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
498 provider_name
, type_name
);
505 ERR("%s: unexpected load expr type %d.", __func__
,
510 load_expr_op
= load_expr_op
->next
;
512 /* There may be a single array index after that. */
513 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
514 struct lttng_event_expr
*index_event_expr
;
515 const uint64_t index
= load_expr_op
->u
.index
;
517 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
518 if (!index_event_expr
) {
519 ERR("Failed to create array field element event expression.");
523 event_expr
= index_event_expr
;
524 load_expr_op
= load_expr_op
->next
;
527 switch (load_expr_op
->type
) {
528 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
530 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
531 * always found at the end of the chain.
534 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
535 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
540 ERR("%s: unexpected load expression operator %s.", __func__
,
541 ir_load_expression_type_str(load_expr_op
->type
));
548 lttng_event_expr_destroy(event_expr
);
558 struct lttng_event_expr
*ir_op_load_to_event_expr(
559 const struct ir_op
*ir
, const char *capture_str
)
561 struct lttng_event_expr
*event_expr
= NULL
;
563 assert(ir
->op
== IR_OP_LOAD
);
565 switch (ir
->data_type
) {
566 case IR_DATA_EXPRESSION
:
568 const struct ir_load_expression
*ir_load_expr
=
569 ir
->u
.load
.u
.expression
;
571 event_expr
= ir_op_load_expr_to_event_expr(
572 ir_load_expr
, capture_str
);
576 ERR("%s: unexpected data type: %s.", __func__
,
577 ir_data_type_str(ir
->data_type
));
585 const char *ir_operator_type_human_str(enum ir_op_type op
)
607 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
608 const char *capture_str
)
610 struct lttng_event_expr
*event_expr
= NULL
;
612 assert(ir
->op
== IR_OP_ROOT
);
613 ir
= ir
->u
.root
.child
;
617 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
622 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
624 ir_operator_type_human_str(ir
->op
));
627 ERR("%s: unexpected IR op type: %s.", __func__
,
628 ir_op_type_str(ir
->op
));
636 void destroy_event_expr(void *ptr
)
638 lttng_event_expr_destroy(ptr
);
641 struct parse_event_rule_res
{
643 struct lttng_event_rule
*er
;
645 /* Array of `struct lttng_event_expr *` */
646 struct lttng_dynamic_pointer_array capture_descriptors
;
650 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
652 enum lttng_event_rule_type event_rule_type
=
653 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
654 struct argpar_state
*state
;
655 struct argpar_item
*item
= NULL
;
657 int consumed_args
= -1;
658 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
659 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
660 struct parse_event_rule_res res
= { 0 };
661 struct lttng_event_expr
*event_expr
= NULL
;
662 struct filter_parser_ctx
*parser_ctx
= NULL
;
663 struct lttng_log_level_rule
*log_level_rule
= NULL
;
665 /* Event rule type option */
666 char *event_rule_type_str
= NULL
;
668 /* Tracepoint and syscall options. */
670 /* Array of strings. */
671 struct lttng_dynamic_pointer_array exclude_names
;
673 /* For userspace / kernel probe and function. */
674 char *location
= NULL
;
675 char *event_name
= NULL
;
681 char *log_level_str
= NULL
;
683 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
686 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
688 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
690 ERR("Failed to allocate an argpar state.");
695 enum argpar_state_parse_next_status status
;
697 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
698 status
= argpar_state_parse_next(state
, &item
, &error
);
699 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
702 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
703 /* Just stop parsing here. */
705 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
709 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
711 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
712 const struct argpar_item_opt
*item_opt
=
713 (const struct argpar_item_opt
*) item
;
715 switch (item_opt
->descr
->id
) {
717 if (!assign_event_rule_type(&event_rule_type
,
722 /* Save the string for later use. */
723 if (!assign_string(&event_rule_type_str
,
731 if (!assign_string(&location
,
739 if (!assign_string(&event_name
,
741 "--event-name/-E")) {
747 if (!assign_string(&filter
, item_opt
->arg
,
754 if (!assign_string(&name
, item_opt
->arg
,
760 case OPT_EXCLUDE_NAME
:
764 ret
= lttng_dynamic_pointer_array_add_pointer(
766 strdup(item_opt
->arg
));
768 ERR("Failed to add pointer to dynamic pointer array.");
775 if (!assign_string(&log_level_str
,
776 item_opt
->arg
, "--log-level/-l")) {
784 const char *capture_str
= item_opt
->arg
;
786 ret
= filter_parser_ctx_create_from_filter_expression(
787 capture_str
, &parser_ctx
);
789 ERR("Failed to parse capture expression `%s`.",
794 event_expr
= ir_op_root_to_event_expr(
799 * ir_op_root_to_event_expr has printed
805 ret
= lttng_dynamic_pointer_array_add_pointer(
806 &res
.capture_descriptors
,
813 * The ownership of event expression was
814 * transferred to the dynamic array.
824 const struct argpar_item_non_opt
*item_non_opt
=
825 (const struct argpar_item_non_opt
*)
828 /* Don't accept non-option arguments. */
829 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
834 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
835 event_rule_type
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
839 * Option --name is applicable to event rules of type kernel, user, jul,
840 * log4j,python and syscall. If --name is omitted, it is implicitly
843 switch (event_rule_type
) {
844 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
845 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
846 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
847 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
848 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
849 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
857 ERR("Can't use --name with %s event rules.",
858 lttng_event_rule_type_str(
863 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
864 ERR("Can't use --exclude-name/-x with %s event rules.",
865 lttng_event_rule_type_str(
872 * Option --location is only applicable to (and mandatory for) event
873 * rules of type {k,u}probe and function.
875 * Option --event-name is only applicable to event rules of type probe.
876 * If omitted, it defaults to the location.
878 switch (event_rule_type
) {
879 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
880 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
882 ERR("Event rule of type %s requires a --location.",
883 lttng_event_rule_type_str(event_rule_type
));
888 event_name
= strdup(location
);
895 ERR("Can't use --location with %s event rules.",
896 lttng_event_rule_type_str(event_rule_type
));
901 ERR("Can't use --event-name with %s event rules.",
902 lttng_event_rule_type_str(
909 * Update *argc and *argv so our caller can keep parsing what follows.
911 consumed_args
= argpar_state_get_ingested_orig_args(state
);
912 assert(consumed_args
>= 0);
913 *argc
-= consumed_args
;
914 *argv
+= consumed_args
;
917 * Adding a filter to a probe, function or userspace-probe would be
918 * denied by the kernel tracer as it's not supported at the moment. We
919 * do an early check here to warn the user.
922 switch (event_rule_type
) {
923 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
924 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
925 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
926 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
927 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
928 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
931 ERR("Filter expressions are not supported for %s event rules.",
932 lttng_event_rule_type_str(event_rule_type
));
938 * If --exclude-name/-x was passed, split it into an exclusion list.
939 * Exclusions are only supported by
940 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
942 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
943 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
944 ERR("Event name exclusions are not yet implemented for %s event rules.",
945 lttng_event_rule_type_str(event_rule_type
));
949 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
951 * Assume validate_exclusion_list already prints an
959 switch (event_rule_type
) {
960 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
961 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
962 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
963 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
968 if (strcmp(log_level_str
, "..") == 0) {
970 * ".." is the same as passing no log level
971 * option and correspond to the "ANY" case.
976 if (!parse_log_level_string(log_level_str
, event_rule_type
,
977 &log_level
, &log_level_only
)) {
978 ERR("Failed to parse log level string `%s`.",
983 if (log_level_only
) {
984 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
986 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
989 if (log_level_rule
== NULL
) {
990 ERR("Failed to create log level rule object.");
996 ERR("Log levels are not supported for %s event rules.",
997 lttng_event_rule_type_str(event_rule_type
));
1002 /* Finally, create the event rule object. */
1003 switch (event_rule_type
) {
1004 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
1006 enum lttng_event_rule_status event_rule_status
;
1008 res
.er
= lttng_event_rule_user_tracepoint_create();
1010 ERR("Failed to create user_tracepoint event rule.");
1015 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1017 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1018 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1025 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1027 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1028 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1034 /* Set exclusion list. */
1035 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1037 int count
= lttng_dynamic_pointer_array_get_count(
1040 for (n
= 0; n
< count
; n
++) {
1041 const char *exclude_name
=
1042 lttng_dynamic_pointer_array_get_pointer(
1047 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1050 if (event_rule_status
!=
1051 LTTNG_EVENT_RULE_STATUS_OK
) {
1052 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1059 if (log_level_rule
) {
1061 lttng_event_rule_user_tracepoint_set_log_level_rule(
1062 res
.er
, log_level_rule
);
1064 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1065 ERR("Failed to set log level on event fule.");
1072 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1074 enum lttng_event_rule_status event_rule_status
;
1076 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1078 ERR("Failed to create kernel_tracepoint event rule.");
1083 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1085 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1086 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1093 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1095 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1096 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1103 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1105 enum lttng_event_rule_status event_rule_status
;
1107 res
.er
= lttng_event_rule_jul_logging_create();
1109 ERR("Failed to create jul_logging event rule.");
1114 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1116 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1117 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1124 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1126 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1127 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1133 if (log_level_rule
) {
1135 lttng_event_rule_jul_logging_set_log_level_rule(
1136 res
.er
, log_level_rule
);
1138 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1139 ERR("Failed to set log level on event fule.");
1145 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1147 enum lttng_event_rule_status event_rule_status
;
1149 res
.er
= lttng_event_rule_log4j_logging_create();
1151 ERR("Failed to create jul_logging event rule.");
1156 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1158 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1159 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1166 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1168 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1169 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1175 if (log_level_rule
) {
1177 lttng_event_rule_log4j_logging_set_log_level_rule(
1178 res
.er
, log_level_rule
);
1180 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1181 ERR("Failed to set log level on event fule.");
1187 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1189 enum lttng_event_rule_status event_rule_status
;
1191 res
.er
= lttng_event_rule_python_logging_create();
1193 ERR("Failed to create jul_logging event rule.");
1198 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1200 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1201 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1208 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1210 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1211 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1217 if (log_level_rule
) {
1219 lttng_event_rule_python_logging_set_log_level_rule(
1220 res
.er
, log_level_rule
);
1222 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1223 ERR("Failed to set log level on event fule.");
1229 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1232 enum lttng_event_rule_status event_rule_status
;
1234 ret
= parse_kernel_probe_opts(
1235 location
, &kernel_probe_location
);
1237 ERR("Failed to parse kernel probe location.");
1241 assert(kernel_probe_location
);
1242 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1244 ERR("Failed to create kprobe event rule.");
1249 lttng_event_rule_kernel_kprobe_set_event_name(
1250 res
.er
, event_name
);
1251 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1252 ERR("Failed to set kprobe event rule's name to '%s'.",
1259 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1262 enum lttng_event_rule_status event_rule_status
;
1264 ret
= parse_userspace_probe_opts(
1265 location
, &userspace_probe_location
);
1267 ERR("Failed to parse user space probe location.");
1271 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1273 ERR("Failed to create userspace probe event rule.");
1278 lttng_event_rule_kernel_uprobe_set_event_name(
1279 res
.er
, event_name
);
1280 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1281 ERR("Failed to set user space probe event rule's name to '%s'.",
1288 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1290 enum lttng_event_rule_status event_rule_status
;
1291 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1293 if (!parse_syscall_emission_site_from_type(
1294 event_rule_type_str
, &emission_site
)) {
1295 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1299 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1301 ERR("Failed to create syscall event rule.");
1305 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1307 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1308 ERR("Failed to set syscall event rule's pattern to '%s'.",
1314 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1316 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1317 ERR("Failed to set syscall event rule's filter to '%s'.",
1333 lttng_event_rule_destroy(res
.er
);
1335 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1339 filter_parser_ctx_free(parser_ctx
);
1342 lttng_event_expr_destroy(event_expr
);
1343 argpar_item_destroy(item
);
1345 argpar_state_destroy(state
);
1348 lttng_dynamic_pointer_array_reset(&exclude_names
);
1349 free(log_level_str
);
1352 free(event_rule_type_str
);
1354 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1355 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1356 lttng_log_level_rule_destroy(log_level_rule
);
1361 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1363 struct parse_event_rule_res res
;
1364 struct lttng_condition
*c
;
1367 res
= parse_event_rule(argc
, argv
);
1373 c
= lttng_condition_event_rule_matches_create(res
.er
);
1374 lttng_event_rule_destroy(res
.er
);
1380 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1382 enum lttng_condition_status status
;
1383 struct lttng_event_expr
**expr
=
1384 lttng_dynamic_array_get_element(
1385 &res
.capture_descriptors
.array
, i
);
1389 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1391 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1392 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1393 ERR("The capture feature is unsupported by the event-rule condition type");
1399 /* Ownership of event expression moved to `c` */
1406 lttng_condition_destroy(c
);
1410 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1411 lttng_event_rule_destroy(res
.er
);
1415 struct condition_descr
{
1417 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1421 struct condition_descr condition_descrs
[] = {
1422 { "event-rule-matches", handle_condition_event
},
1426 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1430 struct lttng_condition
*cond
;
1431 const struct condition_descr
*descr
= NULL
;
1433 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1434 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1435 descr
= &condition_descrs
[i
];
1441 ERR("Unknown condition name '%s'", condition_name
);
1445 cond
= descr
->handler(argc
, argv
);
1447 /* The handler has already printed an error message. */
1458 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1461 size_t num_token
= 0;
1462 struct lttng_dynamic_pointer_array tokens
;
1463 struct lttng_rate_policy
*policy
= NULL
;
1464 enum lttng_rate_policy_type policy_type
;
1465 unsigned long long value
;
1466 char *policy_type_str
;
1467 char *policy_value_str
;
1470 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1472 /* Rate policy fields are separated by ':'. */
1473 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1475 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1479 * Early sanity check that the number of parameter is exactly 2.
1482 if (num_token
!= 2) {
1483 ERR("Rate policy format is invalid.");
1487 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1488 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1490 /* Parse the type. */
1491 if (strcmp(policy_type_str
, "once-after") == 0) {
1492 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1493 } else if (strcmp(policy_type_str
, "every") == 0) {
1494 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1496 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1500 /* Parse the value. */
1501 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1502 ERR("Failed to parse rate policy value `%s` as an integer.",
1508 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1512 switch (policy_type
) {
1513 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1514 policy
= lttng_rate_policy_every_n_create(value
);
1516 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1517 policy
= lttng_rate_policy_once_after_n_create(value
);
1523 if (policy
== NULL
) {
1524 ERR("Failed to create rate policy `%s`.", policy_str
);
1528 lttng_dynamic_pointer_array_reset(&tokens
);
1532 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1533 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1534 ARGPAR_OPT_DESCR_SENTINEL
1538 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1540 struct lttng_action
*action
= NULL
;
1541 struct argpar_state
*state
= NULL
;
1542 struct argpar_item
*item
= NULL
;
1544 struct lttng_rate_policy
*policy
= NULL
;
1546 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1548 ERR("Failed to allocate an argpar state.");
1553 enum argpar_state_parse_next_status status
;
1555 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1556 status
= argpar_state_parse_next(state
, &item
, &error
);
1557 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1560 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1561 /* Just stop parsing here. */
1563 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1567 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1569 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1570 const struct argpar_item_opt
*item_opt
=
1571 (const struct argpar_item_opt
*) item
;
1573 switch (item_opt
->descr
->id
) {
1574 case OPT_RATE_POLICY
:
1576 policy
= parse_rate_policy(item_opt
->arg
);
1586 const struct argpar_item_non_opt
*item_non_opt
;
1588 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1590 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1592 switch (item_non_opt
->non_opt_index
) {
1594 ERR("Unexpected argument `%s`.",
1601 *argc
-= argpar_state_get_ingested_orig_args(state
);
1602 *argv
+= argpar_state_get_ingested_orig_args(state
);
1604 action
= lttng_action_notify_create();
1606 ERR("Failed to create notify action");
1611 enum lttng_action_status status
;
1612 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1613 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1614 ERR("Failed to set rate policy");
1622 lttng_action_destroy(action
);
1626 lttng_rate_policy_destroy(policy
);
1627 argpar_state_destroy(state
);
1628 argpar_item_destroy(item
);
1633 * Generic handler for a kind of action that takes a session name and an
1634 * optional rate policy.
1637 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1639 struct lttng_action
*(*create_action_cb
)(void),
1640 enum lttng_action_status (*set_session_name_cb
)(
1641 struct lttng_action
*, const char *),
1642 enum lttng_action_status (*set_rate_policy_cb
)(
1643 struct lttng_action
*,
1644 const struct lttng_rate_policy
*),
1645 const char *action_name
)
1647 struct lttng_action
*action
= NULL
;
1648 struct argpar_state
*state
= NULL
;
1649 struct argpar_item
*item
= NULL
;
1650 const char *session_name_arg
= NULL
;
1652 enum lttng_action_status action_status
;
1653 struct lttng_rate_policy
*policy
= NULL
;
1655 assert(set_session_name_cb
);
1656 assert(set_rate_policy_cb
);
1658 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1659 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1660 ARGPAR_OPT_DESCR_SENTINEL
1663 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1665 ERR("Failed to allocate an argpar state.");
1670 enum argpar_state_parse_next_status status
;
1672 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1673 status
= argpar_state_parse_next(state
, &item
, &error
);
1674 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1677 } else if (status
==
1678 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1679 /* Just stop parsing here. */
1681 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1685 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1686 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1687 const struct argpar_item_opt
*item_opt
=
1688 (const struct argpar_item_opt
*) item
;
1690 switch (item_opt
->descr
->id
) {
1691 case OPT_RATE_POLICY
:
1693 policy
= parse_rate_policy(item_opt
->arg
);
1703 const struct argpar_item_non_opt
*item_non_opt
;
1704 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1706 switch (item_non_opt
->non_opt_index
) {
1708 session_name_arg
= item_non_opt
->arg
;
1711 ERR("Unexpected argument `%s`.",
1718 *argc
-= argpar_state_get_ingested_orig_args(state
);
1719 *argv
+= argpar_state_get_ingested_orig_args(state
);
1721 if (!session_name_arg
) {
1722 ERR("Missing session name.");
1726 action
= create_action_cb();
1728 ERR("Failed to allocate %s session action.", action_name
);
1732 action_status
= set_session_name_cb(action
, session_name_arg
);
1733 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1734 ERR("Failed to set action %s session's session name to '%s'.",
1735 action_name
, session_name_arg
);
1740 action_status
= set_rate_policy_cb(action
, policy
);
1741 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1742 ERR("Failed to set rate policy");
1750 lttng_action_destroy(action
);
1752 argpar_item_destroy(item
);
1754 lttng_rate_policy_destroy(policy
);
1756 argpar_state_destroy(state
);
1761 struct lttng_action
*handle_action_start_session(int *argc
,
1764 return handle_action_simple_session_with_policy(argc
, argv
,
1765 lttng_action_start_session_create
,
1766 lttng_action_start_session_set_session_name
,
1767 lttng_action_start_session_set_rate_policy
, "start");
1771 struct lttng_action
*handle_action_stop_session(int *argc
,
1774 return handle_action_simple_session_with_policy(argc
, argv
,
1775 lttng_action_stop_session_create
,
1776 lttng_action_stop_session_set_session_name
,
1777 lttng_action_stop_session_set_rate_policy
, "stop");
1781 struct lttng_action
*handle_action_rotate_session(int *argc
,
1784 return handle_action_simple_session_with_policy(argc
, argv
,
1785 lttng_action_rotate_session_create
,
1786 lttng_action_rotate_session_set_session_name
,
1787 lttng_action_rotate_session_set_rate_policy
,
1791 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1792 { OPT_NAME
, 'n', "name", true },
1793 { OPT_MAX_SIZE
, 'm', "max-size", true },
1794 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1795 { OPT_DATA_URL
, '\0', "data-url", true },
1796 { OPT_URL
, '\0', "url", true },
1797 { OPT_PATH
, '\0', "path", true },
1798 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1799 ARGPAR_OPT_DESCR_SENTINEL
1803 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1806 struct lttng_action
*action
= NULL
;
1807 struct argpar_state
*state
= NULL
;
1808 struct argpar_item
*item
= NULL
;
1809 const char *session_name_arg
= NULL
;
1810 char *snapshot_name_arg
= NULL
;
1811 char *ctrl_url_arg
= NULL
;
1812 char *data_url_arg
= NULL
;
1813 char *max_size_arg
= NULL
;
1814 char *url_arg
= NULL
;
1815 char *path_arg
= NULL
;
1817 enum lttng_action_status action_status
;
1818 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1819 struct lttng_rate_policy
*policy
= NULL
;
1821 unsigned int locations_specified
= 0;
1823 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1825 ERR("Failed to allocate an argpar state.");
1830 enum argpar_state_parse_next_status status
;
1832 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1833 status
= argpar_state_parse_next(state
, &item
, &error
);
1834 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1837 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1838 /* Just stop parsing here. */
1840 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1844 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1846 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1847 const struct argpar_item_opt
*item_opt
=
1848 (const struct argpar_item_opt
*) item
;
1850 switch (item_opt
->descr
->id
) {
1852 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1858 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1864 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1870 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1876 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1882 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1887 case OPT_RATE_POLICY
:
1889 policy
= parse_rate_policy(item_opt
->arg
);
1899 const struct argpar_item_non_opt
*item_non_opt
;
1901 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1903 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1905 switch (item_non_opt
->non_opt_index
) {
1907 session_name_arg
= item_non_opt
->arg
;
1910 ERR("Unexpected argument `%s`.",
1917 *argc
-= argpar_state_get_ingested_orig_args(state
);
1918 *argv
+= argpar_state_get_ingested_orig_args(state
);
1920 if (!session_name_arg
) {
1921 ERR("Missing session name.");
1925 /* --ctrl-url and --data-url must come in pair. */
1926 if (ctrl_url_arg
&& !data_url_arg
) {
1927 ERR("--ctrl-url is specified, but --data-url is missing.");
1931 if (!ctrl_url_arg
&& data_url_arg
) {
1932 ERR("--data-url is specified, but --ctrl-url is missing.");
1936 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1937 locations_specified
+= !!url_arg
;
1938 locations_specified
+= !!path_arg
;
1940 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1941 if (locations_specified
> 1) {
1942 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1947 * Did the user specify an option that implies using a
1948 * custom/unregistered output?
1950 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1951 snapshot_output
= lttng_snapshot_output_create();
1952 if (!snapshot_output
) {
1953 ERR("Failed to allocate a snapshot output.");
1958 action
= lttng_action_snapshot_session_create();
1960 ERR("Failed to allocate snapshot session action.");
1964 action_status
= lttng_action_snapshot_session_set_session_name(
1965 action
, session_name_arg
);
1966 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1967 ERR("Failed to set action snapshot session's session name to '%s'.",
1972 if (snapshot_name_arg
) {
1973 if (!snapshot_output
) {
1974 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1978 ret
= lttng_snapshot_output_set_name(
1979 snapshot_name_arg
, snapshot_output
);
1981 ERR("Failed to set name of snapshot output.");
1989 if (!snapshot_output
) {
1990 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1994 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1996 ERR("Failed to parse `%s` as a size.", max_size_arg
);
2000 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
2002 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2010 struct lttng_uri
*uris
;
2012 if (!strstr(url_arg
, "://")) {
2013 ERR("Failed to parse '%s' as an URL.", url_arg
);
2017 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
2019 ERR("Failed to parse '%s' as an URL.", url_arg
);
2023 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2024 ret
= lttng_snapshot_output_set_local_path(
2025 uris
[0].dst
.path
, snapshot_output
);
2028 ERR("Failed to assign '%s' as a local destination.",
2033 ret
= lttng_snapshot_output_set_network_url(
2034 url_arg
, snapshot_output
);
2037 ERR("Failed to assign '%s' as a network URL.",
2045 ret
= lttng_snapshot_output_set_local_path(
2046 path_arg
, snapshot_output
);
2048 ERR("Failed to parse '%s' as a local path.", path_arg
);
2055 * Two argument form, network output with separate control and
2058 ret
= lttng_snapshot_output_set_network_urls(
2059 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2061 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2062 ctrl_url_arg
, data_url_arg
);
2067 if (snapshot_output
) {
2068 action_status
= lttng_action_snapshot_session_set_output(
2069 action
, snapshot_output
);
2070 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2071 ERR("Failed to set snapshot session action's output.");
2075 /* Ownership of `snapshot_output` has been transferred to the action. */
2076 snapshot_output
= NULL
;
2080 enum lttng_action_status status
;
2081 status
= lttng_action_snapshot_session_set_rate_policy(
2083 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2084 ERR("Failed to set rate policy");
2092 lttng_action_destroy(action
);
2096 free(snapshot_name_arg
);
2101 free(snapshot_output
);
2103 lttng_rate_policy_destroy(policy
);
2104 argpar_state_destroy(state
);
2105 argpar_item_destroy(item
);
2109 struct action_descr
{
2111 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2115 struct action_descr action_descrs
[] = {
2116 { "notify", handle_action_notify
},
2117 { "start-session", handle_action_start_session
},
2118 { "stop-session", handle_action_stop_session
},
2119 { "rotate-session", handle_action_rotate_session
},
2120 { "snapshot-session", handle_action_snapshot_session
},
2124 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2127 struct lttng_action
*action
;
2128 const struct action_descr
*descr
= NULL
;
2130 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2131 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2132 descr
= &action_descrs
[i
];
2138 ERR("Unknown action name: %s", action_name
);
2142 action
= descr
->handler(argc
, argv
);
2144 /* The handler has already printed an error message. */
2156 struct argpar_opt_descr add_trigger_options
[] = {
2157 { OPT_HELP
, 'h', "help", false },
2158 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2159 { OPT_CONDITION
, '\0', "condition", true },
2160 { OPT_ACTION
, '\0', "action", true },
2161 { OPT_NAME
, '\0', "name", true },
2162 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2163 ARGPAR_OPT_DESCR_SENTINEL
,
2167 void lttng_actions_destructor(void *p
)
2169 struct lttng_action
*action
= p
;
2171 lttng_action_destroy(action
);
2174 int cmd_add_trigger(int argc
, const char **argv
)
2177 int my_argc
= argc
- 1;
2178 const char **my_argv
= argv
+ 1;
2179 struct lttng_condition
*condition
= NULL
;
2180 struct lttng_dynamic_pointer_array actions
;
2181 struct argpar_state
*argpar_state
= NULL
;
2182 struct argpar_item
*argpar_item
= NULL
;
2183 struct lttng_action
*action_list
= NULL
;
2184 struct lttng_action
*action
= NULL
;
2185 struct lttng_trigger
*trigger
= NULL
;
2189 char *owner_uid
= NULL
;
2190 enum lttng_error_code ret_code
;
2191 struct mi_writer
*mi_writer
= NULL
;
2193 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2196 mi_writer
= mi_lttng_writer_create(
2197 fileno(stdout
), lttng_opt_mi
);
2203 /* Open command element. */
2204 ret
= mi_lttng_writer_command_open(mi_writer
,
2205 mi_lttng_element_command_add_trigger
);
2211 /* Open output element. */
2212 ret
= mi_lttng_writer_open_element(
2213 mi_writer
, mi_lttng_element_command_output
);
2221 enum argpar_state_parse_next_status status
;
2222 const struct argpar_item_opt
*item_opt
;
2225 argpar_state_destroy(argpar_state
);
2226 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2227 add_trigger_options
);
2228 if (!argpar_state
) {
2229 ERR("Failed to create argpar state.");
2233 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2234 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2235 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2238 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2241 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2245 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2247 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2248 const struct argpar_item_non_opt
*item_non_opt
=
2249 (const struct argpar_item_non_opt
*)
2252 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2256 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2258 ingested_args
= argpar_state_get_ingested_orig_args(
2261 my_argc
-= ingested_args
;
2262 my_argv
+= ingested_args
;
2264 switch (item_opt
->descr
->id
) {
2269 case OPT_LIST_OPTIONS
:
2270 list_cmd_options_argpar(stdout
, add_trigger_options
);
2276 ERR("A --condition was already given.");
2280 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2283 * An error message was already printed by
2293 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2296 * An error message was already printed by
2302 ret
= lttng_dynamic_pointer_array_add_pointer(
2305 ERR("Failed to add pointer to pointer array.");
2309 /* Ownership of the action was transferred to the list. */
2316 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2324 if (!assign_string(&owner_uid
, item_opt
->arg
,
2337 ERR("Missing --condition.");
2341 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2342 ERR("Need at least one --action.");
2346 action_list
= lttng_action_list_create();
2351 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2352 enum lttng_action_status status
;
2354 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2356 status
= lttng_action_list_add_action(action_list
, action
);
2357 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2362 * The `lttng_action_list_add_action()` takes a reference to
2363 * the action. We can destroy ours.
2365 lttng_action_destroy(action
);
2369 trigger
= lttng_trigger_create(condition
, action_list
);
2375 enum lttng_trigger_status trigger_status
;
2380 uid
= strtol(owner_uid
, &end
, 10);
2381 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2382 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2386 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2387 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2388 ERR("Failed to set trigger's user identity.");
2394 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2396 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2399 if (ret_code
!= LTTNG_OK
) {
2400 ERR("Failed to register trigger: %s.",
2401 lttng_strerror(-ret_code
));
2406 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2407 if (ret_code
!= LTTNG_OK
) {
2411 const char *returned_trigger_name
;
2412 const enum lttng_trigger_status trigger_status
=
2413 lttng_trigger_get_name(trigger
,
2414 &returned_trigger_name
);
2416 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2417 WARN("Failed to retrieve the added trigger's name.");
2419 MSG("Added trigger `%s`.", returned_trigger_name
);
2432 if (lttng_opt_mi
&& mi_writer
) {
2435 /* Close output element. */
2436 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2442 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2443 mi_lttng_element_command_success
, ret
? 0 : 1);
2449 /* Command element close. */
2450 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2458 argpar_state_destroy(argpar_state
);
2459 argpar_item_destroy(argpar_item
);
2460 lttng_dynamic_pointer_array_reset(&actions
);
2461 lttng_condition_destroy(condition
);
2462 lttng_action_destroy(action_list
);
2463 lttng_action_destroy(action
);
2464 lttng_trigger_destroy(trigger
);
2468 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2469 /* Preserve original error code. */
2470 ret
= ret
? ret
: CMD_ERROR
;