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 LTTNG_ASSERT(load_expr_op
);
441 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
442 field_name
= load_expr_op
->u
.symbol
;
443 LTTNG_ASSERT(field_name
);
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 LTTNG_ASSERT(load_expr_op
);
466 LTTNG_ASSERT(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
467 field_name
= load_expr_op
->u
.symbol
;
468 LTTNG_ASSERT(field_name
);
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 LTTNG_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 LTTNG_ASSERT(ir
->op
== IR_OP_ROOT
);
613 ir
= ir
->u
.root
.child
;
617 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
622 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
624 ir_operator_type_human_str(ir
->op
));
627 ERR("%s: unexpected IR op type: %s.", __func__
,
628 ir_op_type_str(ir
->op
));
636 void destroy_event_expr(void *ptr
)
638 lttng_event_expr_destroy((lttng_event_expr
*) 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 LTTNG_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 ERR("Event rule requires a --type.");
840 * Option --name is applicable to event rules of type kernel, user, jul,
841 * log4j,python and syscall. If --name is omitted, it is implicitly
844 switch (event_rule_type
) {
845 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
846 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
847 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
848 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
849 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
850 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
858 ERR("Can't use --name with %s event rules.",
859 lttng_event_rule_type_str(
864 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
865 ERR("Can't use --exclude-name/-x with %s event rules.",
866 lttng_event_rule_type_str(
873 * Option --location is only applicable to (and mandatory for) event
874 * rules of type {k,u}probe and function.
876 * Option --event-name is only applicable to event rules of type probe.
877 * If omitted, it defaults to the location.
879 switch (event_rule_type
) {
880 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
881 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
883 ERR("Event rule of type %s requires a --location.",
884 lttng_event_rule_type_str(event_rule_type
));
889 event_name
= strdup(location
);
896 ERR("Can't use --location with %s event rules.",
897 lttng_event_rule_type_str(event_rule_type
));
902 ERR("Can't use --event-name with %s event rules.",
903 lttng_event_rule_type_str(
910 * Update *argc and *argv so our caller can keep parsing what follows.
912 consumed_args
= argpar_state_get_ingested_orig_args(state
);
913 LTTNG_ASSERT(consumed_args
>= 0);
914 *argc
-= consumed_args
;
915 *argv
+= consumed_args
;
918 * Adding a filter to a probe, function or userspace-probe would be
919 * denied by the kernel tracer as it's not supported at the moment. We
920 * do an early check here to warn the user.
923 switch (event_rule_type
) {
924 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
925 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
926 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
927 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
928 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
929 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
932 ERR("Filter expressions are not supported for %s event rules.",
933 lttng_event_rule_type_str(event_rule_type
));
939 * If --exclude-name/-x was passed, split it into an exclusion list.
940 * Exclusions are only supported by
941 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
943 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
944 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
945 ERR("Event name exclusions are not yet implemented for %s event rules.",
946 lttng_event_rule_type_str(event_rule_type
));
950 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
952 * Assume validate_exclusion_list already prints an
960 switch (event_rule_type
) {
961 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
962 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
963 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
964 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
969 if (strcmp(log_level_str
, "..") == 0) {
971 * ".." is the same as passing no log level
972 * option and correspond to the "ANY" case.
977 if (!parse_log_level_string(log_level_str
, event_rule_type
,
978 &log_level
, &log_level_only
)) {
979 ERR("Failed to parse log level string `%s`.",
984 if (log_level_only
) {
985 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
987 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
990 if (log_level_rule
== NULL
) {
991 ERR("Failed to create log level rule object.");
997 ERR("Log levels are not supported for %s event rules.",
998 lttng_event_rule_type_str(event_rule_type
));
1003 /* Finally, create the event rule object. */
1004 switch (event_rule_type
) {
1005 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
1007 enum lttng_event_rule_status event_rule_status
;
1009 res
.er
= lttng_event_rule_user_tracepoint_create();
1011 ERR("Failed to create user_tracepoint event rule.");
1016 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1018 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1019 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1026 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1028 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1029 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1035 /* Set exclusion list. */
1036 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1038 int count
= lttng_dynamic_pointer_array_get_count(
1041 for (n
= 0; n
< count
; n
++) {
1042 const char *exclude_name
=
1043 (const char *) lttng_dynamic_pointer_array_get_pointer(
1048 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1051 if (event_rule_status
!=
1052 LTTNG_EVENT_RULE_STATUS_OK
) {
1053 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1060 if (log_level_rule
) {
1062 lttng_event_rule_user_tracepoint_set_log_level_rule(
1063 res
.er
, log_level_rule
);
1065 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1066 ERR("Failed to set log level on event fule.");
1073 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1075 enum lttng_event_rule_status event_rule_status
;
1077 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1079 ERR("Failed to create kernel_tracepoint event rule.");
1084 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1086 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1087 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1094 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1096 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1097 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1104 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1106 enum lttng_event_rule_status event_rule_status
;
1108 res
.er
= lttng_event_rule_jul_logging_create();
1110 ERR("Failed to create jul_logging event rule.");
1115 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1117 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1118 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1125 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1127 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1128 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1134 if (log_level_rule
) {
1136 lttng_event_rule_jul_logging_set_log_level_rule(
1137 res
.er
, log_level_rule
);
1139 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1140 ERR("Failed to set log level on event fule.");
1146 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1148 enum lttng_event_rule_status event_rule_status
;
1150 res
.er
= lttng_event_rule_log4j_logging_create();
1152 ERR("Failed to create jul_logging event rule.");
1157 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1159 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1160 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1167 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1169 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1170 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1176 if (log_level_rule
) {
1178 lttng_event_rule_log4j_logging_set_log_level_rule(
1179 res
.er
, log_level_rule
);
1181 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1182 ERR("Failed to set log level on event fule.");
1188 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1190 enum lttng_event_rule_status event_rule_status
;
1192 res
.er
= lttng_event_rule_python_logging_create();
1194 ERR("Failed to create jul_logging event rule.");
1199 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1201 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1202 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1209 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1211 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1212 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1218 if (log_level_rule
) {
1220 lttng_event_rule_python_logging_set_log_level_rule(
1221 res
.er
, log_level_rule
);
1223 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1224 ERR("Failed to set log level on event fule.");
1230 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1233 enum lttng_event_rule_status event_rule_status
;
1235 ret
= parse_kernel_probe_opts(
1236 location
, &kernel_probe_location
);
1238 ERR("Failed to parse kernel probe location.");
1242 LTTNG_ASSERT(kernel_probe_location
);
1243 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1245 ERR("Failed to create kprobe event rule.");
1250 lttng_event_rule_kernel_kprobe_set_event_name(
1251 res
.er
, event_name
);
1252 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1253 ERR("Failed to set kprobe event rule's name to '%s'.",
1260 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1263 enum lttng_event_rule_status event_rule_status
;
1265 ret
= parse_userspace_probe_opts(
1266 location
, &userspace_probe_location
);
1268 ERR("Failed to parse user space probe location.");
1272 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1274 ERR("Failed to create userspace probe event rule.");
1279 lttng_event_rule_kernel_uprobe_set_event_name(
1280 res
.er
, event_name
);
1281 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1282 ERR("Failed to set user space probe event rule's name to '%s'.",
1289 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1291 enum lttng_event_rule_status event_rule_status
;
1292 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1294 if (!parse_syscall_emission_site_from_type(
1295 event_rule_type_str
, &emission_site
)) {
1296 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1300 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1302 ERR("Failed to create syscall event rule.");
1306 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1308 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1309 ERR("Failed to set syscall event rule's pattern to '%s'.",
1315 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1317 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1318 ERR("Failed to set syscall event rule's filter to '%s'.",
1334 lttng_event_rule_destroy(res
.er
);
1336 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1340 filter_parser_ctx_free(parser_ctx
);
1343 lttng_event_expr_destroy(event_expr
);
1344 argpar_item_destroy(item
);
1346 argpar_state_destroy(state
);
1349 lttng_dynamic_pointer_array_reset(&exclude_names
);
1350 free(log_level_str
);
1353 free(event_rule_type_str
);
1355 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1356 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1357 lttng_log_level_rule_destroy(log_level_rule
);
1362 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1364 struct parse_event_rule_res res
;
1365 struct lttng_condition
*c
;
1368 res
= parse_event_rule(argc
, argv
);
1374 c
= lttng_condition_event_rule_matches_create(res
.er
);
1375 lttng_event_rule_destroy(res
.er
);
1381 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1383 enum lttng_condition_status status
;
1384 struct lttng_event_expr
**expr
=
1385 (lttng_event_expr
**) lttng_dynamic_array_get_element(
1386 &res
.capture_descriptors
.array
, i
);
1389 LTTNG_ASSERT(*expr
);
1390 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1392 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1393 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1394 ERR("The capture feature is unsupported by the event-rule condition type");
1400 /* Ownership of event expression moved to `c` */
1407 lttng_condition_destroy(c
);
1411 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1412 lttng_event_rule_destroy(res
.er
);
1416 struct condition_descr
{
1418 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1422 struct condition_descr condition_descrs
[] = {
1423 { "event-rule-matches", handle_condition_event
},
1427 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1431 struct lttng_condition
*cond
;
1432 const struct condition_descr
*descr
= NULL
;
1434 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1435 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1436 descr
= &condition_descrs
[i
];
1442 ERR("Unknown condition name '%s'", condition_name
);
1446 cond
= descr
->handler(argc
, argv
);
1448 /* The handler has already printed an error message. */
1459 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1462 size_t num_token
= 0;
1463 struct lttng_dynamic_pointer_array tokens
;
1464 struct lttng_rate_policy
*policy
= NULL
;
1465 enum lttng_rate_policy_type policy_type
;
1466 unsigned long long value
;
1467 char *policy_type_str
;
1468 char *policy_value_str
;
1470 LTTNG_ASSERT(policy_str
);
1471 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1473 /* Rate policy fields are separated by ':'. */
1474 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1476 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1480 * Early sanity check that the number of parameter is exactly 2.
1483 if (num_token
!= 2) {
1484 ERR("Rate policy format is invalid.");
1488 policy_type_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1489 policy_value_str
= (char *) lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1491 /* Parse the type. */
1492 if (strcmp(policy_type_str
, "once-after") == 0) {
1493 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1494 } else if (strcmp(policy_type_str
, "every") == 0) {
1495 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1497 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1501 /* Parse the value. */
1502 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1503 ERR("Failed to parse rate policy value `%s` as an integer.",
1509 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1513 switch (policy_type
) {
1514 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1515 policy
= lttng_rate_policy_every_n_create(value
);
1517 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1518 policy
= lttng_rate_policy_once_after_n_create(value
);
1524 if (policy
== NULL
) {
1525 ERR("Failed to create rate policy `%s`.", policy_str
);
1529 lttng_dynamic_pointer_array_reset(&tokens
);
1533 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1534 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1535 ARGPAR_OPT_DESCR_SENTINEL
1539 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1541 struct lttng_action
*action
= NULL
;
1542 struct argpar_state
*state
= NULL
;
1543 struct argpar_item
*item
= NULL
;
1545 struct lttng_rate_policy
*policy
= NULL
;
1547 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1549 ERR("Failed to allocate an argpar state.");
1554 enum argpar_state_parse_next_status status
;
1556 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1557 status
= argpar_state_parse_next(state
, &item
, &error
);
1558 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1561 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1562 /* Just stop parsing here. */
1564 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1568 LTTNG_ASSERT(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1570 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1571 const struct argpar_item_opt
*item_opt
=
1572 (const struct argpar_item_opt
*) item
;
1574 switch (item_opt
->descr
->id
) {
1575 case OPT_RATE_POLICY
:
1577 policy
= parse_rate_policy(item_opt
->arg
);
1587 const struct argpar_item_non_opt
*item_non_opt
;
1589 LTTNG_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1591 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1593 switch (item_non_opt
->non_opt_index
) {
1595 ERR("Unexpected argument `%s`.",
1602 *argc
-= argpar_state_get_ingested_orig_args(state
);
1603 *argv
+= argpar_state_get_ingested_orig_args(state
);
1605 action
= lttng_action_notify_create();
1607 ERR("Failed to create notify action");
1612 enum lttng_action_status status
;
1613 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1614 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1615 ERR("Failed to set rate policy");
1623 lttng_action_destroy(action
);
1627 lttng_rate_policy_destroy(policy
);
1628 argpar_state_destroy(state
);
1629 argpar_item_destroy(item
);
1634 * Generic handler for a kind of action that takes a session name and an
1635 * optional rate policy.
1638 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1640 struct lttng_action
*(*create_action_cb
)(void),
1641 enum lttng_action_status (*set_session_name_cb
)(
1642 struct lttng_action
*, const char *),
1643 enum lttng_action_status (*set_rate_policy_cb
)(
1644 struct lttng_action
*,
1645 const struct lttng_rate_policy
*),
1646 const char *action_name
)
1648 struct lttng_action
*action
= NULL
;
1649 struct argpar_state
*state
= NULL
;
1650 struct argpar_item
*item
= NULL
;
1651 const char *session_name_arg
= NULL
;
1653 enum lttng_action_status action_status
;
1654 struct lttng_rate_policy
*policy
= NULL
;
1656 LTTNG_ASSERT(set_session_name_cb
);
1657 LTTNG_ASSERT(set_rate_policy_cb
);
1659 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1660 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1661 ARGPAR_OPT_DESCR_SENTINEL
1664 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1666 ERR("Failed to allocate an argpar state.");
1671 enum argpar_state_parse_next_status status
;
1673 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1674 status
= argpar_state_parse_next(state
, &item
, &error
);
1675 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1678 } else if (status
==
1679 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1680 /* Just stop parsing here. */
1682 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1686 LTTNG_ASSERT(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1687 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1688 const struct argpar_item_opt
*item_opt
=
1689 (const struct argpar_item_opt
*) item
;
1691 switch (item_opt
->descr
->id
) {
1692 case OPT_RATE_POLICY
:
1694 policy
= parse_rate_policy(item_opt
->arg
);
1704 const struct argpar_item_non_opt
*item_non_opt
;
1705 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1707 switch (item_non_opt
->non_opt_index
) {
1709 session_name_arg
= item_non_opt
->arg
;
1712 ERR("Unexpected argument `%s`.",
1719 *argc
-= argpar_state_get_ingested_orig_args(state
);
1720 *argv
+= argpar_state_get_ingested_orig_args(state
);
1722 if (!session_name_arg
) {
1723 ERR("Missing session name.");
1727 action
= create_action_cb();
1729 ERR("Failed to allocate %s session action.", action_name
);
1733 action_status
= set_session_name_cb(action
, session_name_arg
);
1734 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1735 ERR("Failed to set action %s session's session name to '%s'.",
1736 action_name
, session_name_arg
);
1741 action_status
= set_rate_policy_cb(action
, policy
);
1742 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1743 ERR("Failed to set rate policy");
1751 lttng_action_destroy(action
);
1753 argpar_item_destroy(item
);
1755 lttng_rate_policy_destroy(policy
);
1757 argpar_state_destroy(state
);
1762 struct lttng_action
*handle_action_start_session(int *argc
,
1765 return handle_action_simple_session_with_policy(argc
, argv
,
1766 lttng_action_start_session_create
,
1767 lttng_action_start_session_set_session_name
,
1768 lttng_action_start_session_set_rate_policy
, "start");
1772 struct lttng_action
*handle_action_stop_session(int *argc
,
1775 return handle_action_simple_session_with_policy(argc
, argv
,
1776 lttng_action_stop_session_create
,
1777 lttng_action_stop_session_set_session_name
,
1778 lttng_action_stop_session_set_rate_policy
, "stop");
1782 struct lttng_action
*handle_action_rotate_session(int *argc
,
1785 return handle_action_simple_session_with_policy(argc
, argv
,
1786 lttng_action_rotate_session_create
,
1787 lttng_action_rotate_session_set_session_name
,
1788 lttng_action_rotate_session_set_rate_policy
,
1792 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1793 { OPT_NAME
, 'n', "name", true },
1794 { OPT_MAX_SIZE
, 'm', "max-size", true },
1795 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1796 { OPT_DATA_URL
, '\0', "data-url", true },
1797 { OPT_URL
, '\0', "url", true },
1798 { OPT_PATH
, '\0', "path", true },
1799 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1800 ARGPAR_OPT_DESCR_SENTINEL
1804 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1807 struct lttng_action
*action
= NULL
;
1808 struct argpar_state
*state
= NULL
;
1809 struct argpar_item
*item
= NULL
;
1810 const char *session_name_arg
= NULL
;
1811 char *snapshot_name_arg
= NULL
;
1812 char *ctrl_url_arg
= NULL
;
1813 char *data_url_arg
= NULL
;
1814 char *max_size_arg
= NULL
;
1815 char *url_arg
= NULL
;
1816 char *path_arg
= NULL
;
1818 enum lttng_action_status action_status
;
1819 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1820 struct lttng_rate_policy
*policy
= NULL
;
1822 unsigned int locations_specified
= 0;
1824 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1826 ERR("Failed to allocate an argpar state.");
1831 enum argpar_state_parse_next_status status
;
1833 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1834 status
= argpar_state_parse_next(state
, &item
, &error
);
1835 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1838 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1839 /* Just stop parsing here. */
1841 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1845 LTTNG_ASSERT(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1847 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1848 const struct argpar_item_opt
*item_opt
=
1849 (const struct argpar_item_opt
*) item
;
1851 switch (item_opt
->descr
->id
) {
1853 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1859 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1865 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1871 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1877 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1883 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1888 case OPT_RATE_POLICY
:
1890 policy
= parse_rate_policy(item_opt
->arg
);
1900 const struct argpar_item_non_opt
*item_non_opt
;
1902 LTTNG_ASSERT(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1904 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1906 switch (item_non_opt
->non_opt_index
) {
1908 session_name_arg
= item_non_opt
->arg
;
1911 ERR("Unexpected argument `%s`.",
1918 *argc
-= argpar_state_get_ingested_orig_args(state
);
1919 *argv
+= argpar_state_get_ingested_orig_args(state
);
1921 if (!session_name_arg
) {
1922 ERR("Missing session name.");
1926 /* --ctrl-url and --data-url must come in pair. */
1927 if (ctrl_url_arg
&& !data_url_arg
) {
1928 ERR("--ctrl-url is specified, but --data-url is missing.");
1932 if (!ctrl_url_arg
&& data_url_arg
) {
1933 ERR("--data-url is specified, but --ctrl-url is missing.");
1937 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1938 locations_specified
+= !!url_arg
;
1939 locations_specified
+= !!path_arg
;
1941 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1942 if (locations_specified
> 1) {
1943 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1948 * Did the user specify an option that implies using a
1949 * custom/unregistered output?
1951 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1952 snapshot_output
= lttng_snapshot_output_create();
1953 if (!snapshot_output
) {
1954 ERR("Failed to allocate a snapshot output.");
1959 action
= lttng_action_snapshot_session_create();
1961 ERR("Failed to allocate snapshot session action.");
1965 action_status
= lttng_action_snapshot_session_set_session_name(
1966 action
, session_name_arg
);
1967 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1968 ERR("Failed to set action snapshot session's session name to '%s'.",
1973 if (snapshot_name_arg
) {
1974 if (!snapshot_output
) {
1975 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1979 ret
= lttng_snapshot_output_set_name(
1980 snapshot_name_arg
, snapshot_output
);
1982 ERR("Failed to set name of snapshot output.");
1990 if (!snapshot_output
) {
1991 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1995 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1997 ERR("Failed to parse `%s` as a size.", max_size_arg
);
2001 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
2003 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2011 struct lttng_uri
*uris
;
2013 if (!strstr(url_arg
, "://")) {
2014 ERR("Failed to parse '%s' as an URL.", url_arg
);
2018 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
2020 ERR("Failed to parse '%s' as an URL.", url_arg
);
2024 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2025 ret
= lttng_snapshot_output_set_local_path(
2026 uris
[0].dst
.path
, snapshot_output
);
2029 ERR("Failed to assign '%s' as a local destination.",
2034 ret
= lttng_snapshot_output_set_network_url(
2035 url_arg
, snapshot_output
);
2038 ERR("Failed to assign '%s' as a network URL.",
2046 ret
= lttng_snapshot_output_set_local_path(
2047 path_arg
, snapshot_output
);
2049 ERR("Failed to parse '%s' as a local path.", path_arg
);
2056 * Two argument form, network output with separate control and
2059 ret
= lttng_snapshot_output_set_network_urls(
2060 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2062 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2063 ctrl_url_arg
, data_url_arg
);
2068 if (snapshot_output
) {
2069 action_status
= lttng_action_snapshot_session_set_output(
2070 action
, snapshot_output
);
2071 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2072 ERR("Failed to set snapshot session action's output.");
2076 /* Ownership of `snapshot_output` has been transferred to the action. */
2077 snapshot_output
= NULL
;
2081 enum lttng_action_status status
;
2082 status
= lttng_action_snapshot_session_set_rate_policy(
2084 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2085 ERR("Failed to set rate policy");
2093 lttng_action_destroy(action
);
2097 free(snapshot_name_arg
);
2102 free(snapshot_output
);
2104 lttng_rate_policy_destroy(policy
);
2105 argpar_state_destroy(state
);
2106 argpar_item_destroy(item
);
2110 struct action_descr
{
2112 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2116 struct action_descr action_descrs
[] = {
2117 { "notify", handle_action_notify
},
2118 { "start-session", handle_action_start_session
},
2119 { "stop-session", handle_action_stop_session
},
2120 { "rotate-session", handle_action_rotate_session
},
2121 { "snapshot-session", handle_action_snapshot_session
},
2125 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2128 struct lttng_action
*action
;
2129 const struct action_descr
*descr
= NULL
;
2131 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2132 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2133 descr
= &action_descrs
[i
];
2139 ERR("Unknown action name: %s", action_name
);
2143 action
= descr
->handler(argc
, argv
);
2145 /* The handler has already printed an error message. */
2157 struct argpar_opt_descr add_trigger_options
[] = {
2158 { OPT_HELP
, 'h', "help", false },
2159 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2160 { OPT_CONDITION
, '\0', "condition", true },
2161 { OPT_ACTION
, '\0', "action", true },
2162 { OPT_NAME
, '\0', "name", true },
2163 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2164 ARGPAR_OPT_DESCR_SENTINEL
,
2168 void lttng_actions_destructor(void *p
)
2170 struct lttng_action
*action
= (lttng_action
*) p
;
2172 lttng_action_destroy(action
);
2175 int cmd_add_trigger(int argc
, const char **argv
)
2178 int my_argc
= argc
- 1;
2179 const char **my_argv
= argv
+ 1;
2180 struct lttng_condition
*condition
= NULL
;
2181 struct lttng_dynamic_pointer_array actions
;
2182 struct argpar_state
*argpar_state
= NULL
;
2183 struct argpar_item
*argpar_item
= NULL
;
2184 struct lttng_action
*action_list
= NULL
;
2185 struct lttng_action
*action
= NULL
;
2186 struct lttng_trigger
*trigger
= NULL
;
2190 char *owner_uid
= NULL
;
2191 enum lttng_error_code ret_code
;
2192 struct mi_writer
*mi_writer
= NULL
;
2194 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2197 mi_writer
= mi_lttng_writer_create(
2198 fileno(stdout
), lttng_opt_mi
);
2204 /* Open command element. */
2205 ret
= mi_lttng_writer_command_open(mi_writer
,
2206 mi_lttng_element_command_add_trigger
);
2212 /* Open output element. */
2213 ret
= mi_lttng_writer_open_element(
2214 mi_writer
, mi_lttng_element_command_output
);
2222 enum argpar_state_parse_next_status status
;
2223 const struct argpar_item_opt
*item_opt
;
2226 argpar_state_destroy(argpar_state
);
2227 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2228 add_trigger_options
);
2229 if (!argpar_state
) {
2230 ERR("Failed to create argpar state.");
2234 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2235 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2236 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2239 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2242 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2246 LTTNG_ASSERT(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2248 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2249 const struct argpar_item_non_opt
*item_non_opt
=
2250 (const struct argpar_item_non_opt
*)
2253 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2257 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2259 ingested_args
= argpar_state_get_ingested_orig_args(
2262 my_argc
-= ingested_args
;
2263 my_argv
+= ingested_args
;
2265 switch (item_opt
->descr
->id
) {
2270 case OPT_LIST_OPTIONS
:
2271 list_cmd_options_argpar(stdout
, add_trigger_options
);
2277 ERR("A --condition was already given.");
2281 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2284 * An error message was already printed by
2294 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2297 * An error message was already printed by
2303 ret
= lttng_dynamic_pointer_array_add_pointer(
2306 ERR("Failed to add pointer to pointer array.");
2310 /* Ownership of the action was transferred to the list. */
2317 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2325 if (!assign_string(&owner_uid
, item_opt
->arg
,
2338 ERR("Missing --condition.");
2342 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2343 ERR("Need at least one --action.");
2347 action_list
= lttng_action_list_create();
2352 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2353 enum lttng_action_status status
;
2355 action
= (lttng_action
*) lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2357 status
= lttng_action_list_add_action(action_list
, action
);
2358 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2363 * The `lttng_action_list_add_action()` takes a reference to
2364 * the action. We can destroy ours.
2366 lttng_action_destroy(action
);
2370 trigger
= lttng_trigger_create(condition
, action_list
);
2376 enum lttng_trigger_status trigger_status
;
2381 uid
= strtol(owner_uid
, &end
, 10);
2382 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2383 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2387 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2388 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2389 ERR("Failed to set trigger's user identity.");
2395 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2397 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2400 if (ret_code
!= LTTNG_OK
) {
2401 ERR("Failed to register trigger: %s.",
2402 lttng_strerror(-ret_code
));
2407 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2408 if (ret_code
!= LTTNG_OK
) {
2412 const char *returned_trigger_name
;
2413 const enum lttng_trigger_status trigger_status
=
2414 lttng_trigger_get_name(trigger
,
2415 &returned_trigger_name
);
2417 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2418 WARN("Failed to retrieve the added trigger's name.");
2420 MSG("Added trigger `%s`.", returned_trigger_name
);
2433 if (lttng_opt_mi
&& mi_writer
) {
2436 /* Close output element. */
2437 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2443 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2444 mi_lttng_element_command_success
, ret
? 0 : 1);
2450 /* Command element close. */
2451 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2459 argpar_state_destroy(argpar_state
);
2460 argpar_item_destroy(argpar_item
);
2461 lttng_dynamic_pointer_array_reset(&actions
);
2462 lttng_condition_destroy(condition
);
2463 lttng_action_destroy(action_list
);
2464 lttng_action_destroy(action
);
2465 lttng_trigger_destroy(trigger
);
2469 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2470 /* Preserve original error code. */
2471 ret
= ret
? ret
: CMD_ERROR
;