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 /* For lttng_event_rule_type_str(). */
22 #include <lttng/event-rule/event-rule-internal.h>
23 #include <lttng/lttng.h>
24 #include "common/filter/filter-ast.h"
25 #include "common/filter/filter-ir.h"
26 #include "common/dynamic-array.h"
28 #if (LTTNG_SYMBOL_NAME_LEN == 256)
29 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
32 #ifdef LTTNG_EMBED_HELP
33 static const char help_msg
[] =
34 #include <lttng-add-trigger.1.h>
66 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
67 { OPT_FILTER
, 'f', "filter", true },
68 { OPT_NAME
, 'n', "name", true },
69 { OPT_EXCLUDE_NAME
, 'x', "exclude-name", true },
70 { OPT_LOG_LEVEL
, 'l', "log-level", true },
71 { OPT_EVENT_NAME
, 'E', "event-name", true },
73 { OPT_TYPE
, 't', "type", true },
74 { OPT_LOCATION
, 'L', "location", true },
76 /* Capture descriptor */
77 { OPT_CAPTURE
, '\0', "capture", true },
79 ARGPAR_OPT_DESCR_SENTINEL
83 bool has_syscall_prefix(const char *arg
)
86 const char kernel_syscall_type_opt_prefix
[] = "kernel:syscall";
87 const size_t kernel_syscall_type_opt_prefix_len
=
88 sizeof(kernel_syscall_type_opt_prefix
) - 1;
89 const char syscall_type_opt_prefix
[] = "syscall";
90 const size_t syscall_type_opt_prefix_len
=
91 sizeof(syscall_type_opt_prefix
) - 1;
93 if (strncmp(arg
, syscall_type_opt_prefix
,
94 syscall_type_opt_prefix_len
) == 0) {
96 } else if (strncmp(arg
, kernel_syscall_type_opt_prefix
,
97 kernel_syscall_type_opt_prefix_len
) == 0) {
107 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
111 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
112 ERR("More than one `--type` was specified.");
116 if (strcmp(arg
, "user") == 0 || strcmp(arg
, "user:tracepoint") == 0) {
117 *dest
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
118 } else if (strcmp(arg
, "kernel") == 0 ||
119 strcmp(arg
, "kernel:tracepoint") == 0) {
120 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
;
121 } else if (strcmp(arg
, "jul") == 0 || strcmp(arg
, "jul:logging") == 0) {
122 *dest
= LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
;
123 } else if (strcmp(arg
, "log4j") == 0 ||
124 strcmp(arg
, "log4j:logging") == 0) {
125 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
;
126 } else if (strcmp(arg
, "python") == 0 ||
127 strcmp(arg
, "python:logging") == 0) {
128 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
129 } else if (strcmp(arg
, "kprobe") == 0 ||
130 strcmp(arg
, "kernel:kprobe") == 0) {
131 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
132 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
133 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
134 } else if (has_syscall_prefix(arg
)) {
136 * Matches the following:
140 * - syscall:entry+exit
143 * - kernel:syscall:entry
144 * - kernel:syscall:exit
145 * - kernel:syscall:entry+exit
148 * Validation for the right side is left to further usage sites.
150 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
152 ERR("Invalid `--type` value: %s", arg
);
167 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
172 ERR("Duplicate '%s' given.", opt_name
);
178 PERROR("Failed to allocate string '%s'.", opt_name
);
192 static bool parse_syscall_emission_site_from_type(const char *str
,
193 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
196 const char kernel_prefix
[] = "kernel:";
197 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
200 * If the passed string is of the form "kernel:syscall*", move the
201 * pointer passed "kernel:".
203 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
204 str
= &str
[kernel_prefix_len
];
207 if (strcmp(str
, "syscall") == 0 ||
208 strcmp(str
, "syscall:entry+exit") == 0) {
209 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
210 } else if (strcmp(str
, "syscall:entry") == 0) {
211 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
212 } else if (strcmp(str
, "syscall:exit") == 0) {
213 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
225 * Parse `str` as a log level against the passed event rule type.
227 * Return the log level in `*log_level`. Return true in `*log_level_only` if
228 * the string specifies exactly this log level, false if it specifies at least
231 * Return true if the string was successfully parsed as a log level string.
233 static bool parse_log_level_string(const char *str
,
234 enum lttng_event_rule_type event_rule_type
,
236 bool *log_level_only
)
240 switch (event_rule_type
) {
241 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
243 enum lttng_loglevel log_level_min
, log_level_max
;
244 if (!loglevel_parse_range_string(
245 str
, &log_level_min
, &log_level_max
)) {
249 /* Only support VAL and VAL.. for now. */
250 if (log_level_min
!= log_level_max
&&
251 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
255 *log_level
= (int) log_level_min
;
256 *log_level_only
= log_level_min
== log_level_max
;
259 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
261 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
262 if (!loglevel_log4j_parse_range_string(
263 str
, &log_level_min
, &log_level_max
)) {
267 /* Only support VAL and VAL.. for now. */
268 if (log_level_min
!= log_level_max
&&
269 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
273 *log_level
= (int) log_level_min
;
274 *log_level_only
= log_level_min
== log_level_max
;
277 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
279 enum lttng_loglevel_jul log_level_min
, log_level_max
;
280 if (!loglevel_jul_parse_range_string(
281 str
, &log_level_min
, &log_level_max
)) {
285 /* Only support VAL and VAL.. for now. */
286 if (log_level_min
!= log_level_max
&&
287 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
291 *log_level
= (int) log_level_min
;
292 *log_level_only
= log_level_min
== log_level_max
;
295 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
297 enum lttng_loglevel_python log_level_min
, log_level_max
;
298 if (!loglevel_python_parse_range_string(
299 str
, &log_level_min
, &log_level_max
)) {
303 /* Only support VAL and VAL.. for now. */
304 if (log_level_min
!= log_level_max
&&
306 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
310 *log_level
= (int) log_level_min
;
311 *log_level_only
= log_level_min
== log_level_max
;
315 /* Invalid domain type. */
329 static int parse_kernel_probe_opts(const char *source
,
330 struct lttng_kernel_probe_location
**location
)
335 char name
[LTTNG_SYMBOL_NAME_LEN
];
336 char *symbol_name
= NULL
;
339 /* Check for symbol+offset. */
340 match
= sscanf(source
,
341 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
345 if (*s_hex
== '\0') {
346 ERR("Kernel probe symbol offset is missing.");
350 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
352 PERROR("Failed to copy kernel probe location symbol name.");
355 offset
= strtoul(s_hex
, NULL
, 0);
357 *location
= lttng_kernel_probe_location_symbol_create(
358 symbol_name
, offset
);
360 ERR("Failed to create symbol kernel probe location.");
367 /* Check for symbol. */
368 if (isalpha(name
[0]) || name
[0] == '_') {
369 match
= sscanf(source
,
370 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
374 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
376 ERR("Failed to copy kernel probe location symbol name.");
380 *location
= lttng_kernel_probe_location_symbol_create(
383 ERR("Failed to create symbol kernel probe location.");
391 /* Check for address. */
392 match
= sscanf(source
, "%18s", s_hex
);
396 if (*s_hex
== '\0') {
397 ERR("Invalid kernel probe location address.");
401 address
= strtoul(s_hex
, NULL
, 0);
402 *location
= lttng_kernel_probe_location_address_create(address
);
404 ERR("Failed to create symbol kernel probe location.");
422 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
423 const struct ir_load_expression
*load_expr
,
424 const char *capture_str
)
426 char *provider_name
= NULL
;
427 struct lttng_event_expr
*event_expr
= NULL
;
428 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
429 const enum ir_load_expression_type load_expr_child_type
=
432 switch (load_expr_child_type
) {
433 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
434 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
436 const char *field_name
;
438 load_expr_op
= load_expr_op
->next
;
439 assert(load_expr_op
);
440 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
441 field_name
= load_expr_op
->u
.symbol
;
444 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
445 lttng_event_expr_event_payload_field_create(field_name
) :
446 lttng_event_expr_channel_context_field_create(field_name
);
448 ERR("Failed to create %s event expression: field name = `%s`.",
449 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
450 "payload field" : "channel context",
457 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
460 const char *type_name
;
461 const char *field_name
;
463 load_expr_op
= load_expr_op
->next
;
464 assert(load_expr_op
);
465 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
466 field_name
= load_expr_op
->u
.symbol
;
470 * The field name needs to be of the form PROVIDER:TYPE. We
473 colon
= strchr(field_name
, ':');
475 ERR("Invalid app-specific context field name: missing colon in `%s`.",
480 type_name
= colon
+ 1;
481 if (*type_name
== '\0') {
482 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
487 provider_name
= strndup(field_name
, colon
- field_name
);
488 if (!provider_name
) {
489 PERROR("Failed to allocate field name string");
493 event_expr
= lttng_event_expr_app_specific_context_field_create(
494 provider_name
, type_name
);
496 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
497 provider_name
, type_name
);
504 ERR("%s: unexpected load expr type %d.", __func__
,
509 load_expr_op
= load_expr_op
->next
;
511 /* There may be a single array index after that. */
512 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
513 struct lttng_event_expr
*index_event_expr
;
514 const uint64_t index
= load_expr_op
->u
.index
;
516 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
517 if (!index_event_expr
) {
518 ERR("Failed to create array field element event expression.");
522 event_expr
= index_event_expr
;
523 load_expr_op
= load_expr_op
->next
;
526 switch (load_expr_op
->type
) {
527 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
529 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
530 * always found at the end of the chain.
533 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
534 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
539 ERR("%s: unexpected load expression operator %s.", __func__
,
540 ir_load_expression_type_str(load_expr_op
->type
));
547 lttng_event_expr_destroy(event_expr
);
557 struct lttng_event_expr
*ir_op_load_to_event_expr(
558 const struct ir_op
*ir
, const char *capture_str
)
560 struct lttng_event_expr
*event_expr
= NULL
;
562 assert(ir
->op
== IR_OP_LOAD
);
564 switch (ir
->data_type
) {
565 case IR_DATA_EXPRESSION
:
567 const struct ir_load_expression
*ir_load_expr
=
568 ir
->u
.load
.u
.expression
;
570 event_expr
= ir_op_load_expr_to_event_expr(
571 ir_load_expr
, capture_str
);
575 ERR("%s: unexpected data type: %s.", __func__
,
576 ir_data_type_str(ir
->data_type
));
584 const char *ir_operator_type_human_str(enum ir_op_type op
)
606 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
607 const char *capture_str
)
609 struct lttng_event_expr
*event_expr
= NULL
;
611 assert(ir
->op
== IR_OP_ROOT
);
612 ir
= ir
->u
.root
.child
;
616 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
621 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
623 ir_operator_type_human_str(ir
->op
));
626 ERR("%s: unexpected IR op type: %s.", __func__
,
627 ir_op_type_str(ir
->op
));
635 void destroy_event_expr(void *ptr
)
637 lttng_event_expr_destroy(ptr
);
640 struct parse_event_rule_res
{
642 struct lttng_event_rule
*er
;
644 /* Array of `struct lttng_event_expr *` */
645 struct lttng_dynamic_pointer_array capture_descriptors
;
649 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
651 enum lttng_event_rule_type event_rule_type
=
652 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
653 struct argpar_state
*state
;
654 struct argpar_item
*item
= NULL
;
656 int consumed_args
= -1;
657 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
658 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
659 struct parse_event_rule_res res
= { 0 };
660 struct lttng_event_expr
*event_expr
= NULL
;
661 struct filter_parser_ctx
*parser_ctx
= NULL
;
662 struct lttng_log_level_rule
*log_level_rule
= NULL
;
664 /* Event rule type option */
665 char *event_rule_type_str
= NULL
;
667 /* Tracepoint and syscall options. */
669 /* Array of strings. */
670 struct lttng_dynamic_pointer_array exclude_names
;
672 /* For userspace / kernel probe and function. */
673 char *location
= NULL
;
674 char *event_name
= NULL
;
680 char *log_level_str
= NULL
;
682 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
685 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
687 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
689 ERR("Failed to allocate an argpar state.");
694 enum argpar_state_parse_next_status status
;
696 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
697 status
= argpar_state_parse_next(state
, &item
, &error
);
698 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
701 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
702 /* Just stop parsing here. */
704 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
708 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
710 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
711 const struct argpar_item_opt
*item_opt
=
712 (const struct argpar_item_opt
*) item
;
714 switch (item_opt
->descr
->id
) {
716 if (!assign_event_rule_type(&event_rule_type
,
721 /* Save the string for later use. */
722 if (!assign_string(&event_rule_type_str
,
730 if (!assign_string(&location
,
738 if (!assign_string(&event_name
,
740 "--event-name/-E")) {
746 if (!assign_string(&filter
, item_opt
->arg
,
753 if (!assign_string(&name
, item_opt
->arg
,
759 case OPT_EXCLUDE_NAME
:
763 ret
= lttng_dynamic_pointer_array_add_pointer(
765 strdup(item_opt
->arg
));
767 ERR("Failed to add pointer to dynamic pointer array.");
774 if (!assign_string(&log_level_str
,
775 item_opt
->arg
, "--log-level/-l")) {
783 const char *capture_str
= item_opt
->arg
;
785 ret
= filter_parser_ctx_create_from_filter_expression(
786 capture_str
, &parser_ctx
);
788 ERR("Failed to parse capture expression `%s`.",
793 event_expr
= ir_op_root_to_event_expr(
798 * ir_op_root_to_event_expr has printed
804 ret
= lttng_dynamic_pointer_array_add_pointer(
805 &res
.capture_descriptors
,
812 * The ownership of event expression was
813 * transferred to the dynamic array.
823 const struct argpar_item_non_opt
*item_non_opt
=
824 (const struct argpar_item_non_opt
*)
827 /* Don't accept non-option arguments. */
828 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
833 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
834 event_rule_type
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
838 * Option --name is applicable to event rules of type kernel, user, jul,
839 * log4j,python and syscall. If --name is omitted, it is implicitly
842 switch (event_rule_type
) {
843 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
844 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
845 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
846 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
847 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
848 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
856 ERR("Can't use --name with %s event rules.",
857 lttng_event_rule_type_str(
862 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
863 ERR("Can't use --exclude-name/-x with %s event rules.",
864 lttng_event_rule_type_str(
871 * Option --location is only applicable to (and mandatory for) event
872 * rules of type {k,u}probe and function.
874 * Option --event-name is only applicable to event rules of type probe.
875 * If omitted, it defaults to the location.
877 switch (event_rule_type
) {
878 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
879 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
881 ERR("Event rule of type %s requires a --location.",
882 lttng_event_rule_type_str(event_rule_type
));
887 event_name
= strdup(location
);
894 ERR("Can't use --location with %s event rules.",
895 lttng_event_rule_type_str(event_rule_type
));
900 ERR("Can't use --event-name with %s event rules.",
901 lttng_event_rule_type_str(
908 * Update *argc and *argv so our caller can keep parsing what follows.
910 consumed_args
= argpar_state_get_ingested_orig_args(state
);
911 assert(consumed_args
>= 0);
912 *argc
-= consumed_args
;
913 *argv
+= consumed_args
;
916 * Adding a filter to a probe, function or userspace-probe would be
917 * denied by the kernel tracer as it's not supported at the moment. We
918 * do an early check here to warn the user.
921 switch (event_rule_type
) {
922 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
923 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
924 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
925 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
926 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
927 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
930 ERR("Filter expressions are not supported for %s event rules.",
931 lttng_event_rule_type_str(event_rule_type
));
937 * If --exclude-name/-x was passed, split it into an exclusion list.
938 * Exclusions are only supported by
939 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
941 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
942 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
943 ERR("Event name exclusions are not yet implemented for %s event rules.",
944 lttng_event_rule_type_str(event_rule_type
));
948 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
950 * Assume validate_exclusion_list already prints an
958 switch (event_rule_type
) {
959 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
960 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
961 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
962 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
967 if (strcmp(log_level_str
, "..") == 0) {
969 * ".." is the same as passing no log level
970 * option and correspond to the "ANY" case.
975 if (!parse_log_level_string(log_level_str
, event_rule_type
,
976 &log_level
, &log_level_only
)) {
977 ERR("Failed to parse log level string `%s`.",
982 if (log_level_only
) {
983 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
985 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
988 if (log_level_rule
== NULL
) {
989 ERR("Failed to create log level rule object.");
995 ERR("Log levels are not supported for %s event rules.",
996 lttng_event_rule_type_str(event_rule_type
));
1001 /* Finally, create the event rule object. */
1002 switch (event_rule_type
) {
1003 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
1005 enum lttng_event_rule_status event_rule_status
;
1007 res
.er
= lttng_event_rule_user_tracepoint_create();
1009 ERR("Failed to create user_tracepoint event rule.");
1014 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1016 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1017 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1024 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1026 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1027 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1033 /* Set exclusion list. */
1034 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1036 int count
= lttng_dynamic_pointer_array_get_count(
1039 for (n
= 0; n
< count
; n
++) {
1040 const char *exclude_name
=
1041 lttng_dynamic_pointer_array_get_pointer(
1046 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1049 if (event_rule_status
!=
1050 LTTNG_EVENT_RULE_STATUS_OK
) {
1051 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1058 if (log_level_rule
) {
1060 lttng_event_rule_user_tracepoint_set_log_level_rule(
1061 res
.er
, log_level_rule
);
1063 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1064 ERR("Failed to set log level on event fule.");
1071 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1073 enum lttng_event_rule_status event_rule_status
;
1075 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1077 ERR("Failed to create kernel_tracepoint event rule.");
1082 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1084 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1085 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1092 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1094 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1095 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1102 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1104 enum lttng_event_rule_status event_rule_status
;
1106 res
.er
= lttng_event_rule_jul_logging_create();
1108 ERR("Failed to create jul_logging event rule.");
1113 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1115 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1116 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1123 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1125 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1126 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1132 if (log_level_rule
) {
1134 lttng_event_rule_jul_logging_set_log_level_rule(
1135 res
.er
, log_level_rule
);
1137 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1138 ERR("Failed to set log level on event fule.");
1144 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1146 enum lttng_event_rule_status event_rule_status
;
1148 res
.er
= lttng_event_rule_log4j_logging_create();
1150 ERR("Failed to create jul_logging event rule.");
1155 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1157 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1158 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1165 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1167 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1168 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1174 if (log_level_rule
) {
1176 lttng_event_rule_log4j_logging_set_log_level_rule(
1177 res
.er
, log_level_rule
);
1179 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1180 ERR("Failed to set log level on event fule.");
1186 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1188 enum lttng_event_rule_status event_rule_status
;
1190 res
.er
= lttng_event_rule_python_logging_create();
1192 ERR("Failed to create jul_logging event rule.");
1197 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1199 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1200 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1207 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1209 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1210 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1216 if (log_level_rule
) {
1218 lttng_event_rule_python_logging_set_log_level_rule(
1219 res
.er
, log_level_rule
);
1221 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1222 ERR("Failed to set log level on event fule.");
1228 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1231 enum lttng_event_rule_status event_rule_status
;
1233 ret
= parse_kernel_probe_opts(
1234 location
, &kernel_probe_location
);
1236 ERR("Failed to parse kernel probe location.");
1240 assert(kernel_probe_location
);
1241 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1243 ERR("Failed to create kprobe event rule.");
1248 lttng_event_rule_kernel_kprobe_set_event_name(
1249 res
.er
, event_name
);
1250 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1251 ERR("Failed to set kprobe event rule's name to '%s'.",
1258 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1261 enum lttng_event_rule_status event_rule_status
;
1263 ret
= parse_userspace_probe_opts(
1264 location
, &userspace_probe_location
);
1266 ERR("Failed to parse user space probe location.");
1270 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1272 ERR("Failed to create userspace probe event rule.");
1277 lttng_event_rule_kernel_uprobe_set_event_name(
1278 res
.er
, event_name
);
1279 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1280 ERR("Failed to set user space probe event rule's name to '%s'.",
1287 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1289 enum lttng_event_rule_status event_rule_status
;
1290 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1292 if (!parse_syscall_emission_site_from_type(
1293 event_rule_type_str
, &emission_site
)) {
1294 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1298 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1300 ERR("Failed to create syscall event rule.");
1304 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1306 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1307 ERR("Failed to set syscall event rule's pattern to '%s'.",
1313 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1315 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1316 ERR("Failed to set syscall event rule's filter to '%s'.",
1332 lttng_event_rule_destroy(res
.er
);
1334 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1338 filter_parser_ctx_free(parser_ctx
);
1341 lttng_event_expr_destroy(event_expr
);
1342 argpar_item_destroy(item
);
1344 argpar_state_destroy(state
);
1347 lttng_dynamic_pointer_array_reset(&exclude_names
);
1348 free(log_level_str
);
1351 free(event_rule_type_str
);
1353 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1354 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1355 lttng_log_level_rule_destroy(log_level_rule
);
1360 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1362 struct parse_event_rule_res res
;
1363 struct lttng_condition
*c
;
1366 res
= parse_event_rule(argc
, argv
);
1372 c
= lttng_condition_event_rule_matches_create(res
.er
);
1373 lttng_event_rule_destroy(res
.er
);
1379 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1381 enum lttng_condition_status status
;
1382 struct lttng_event_expr
**expr
=
1383 lttng_dynamic_array_get_element(
1384 &res
.capture_descriptors
.array
, i
);
1388 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1390 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1391 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1392 ERR("The capture feature is unsupported by the event-rule condition type");
1398 /* Ownership of event expression moved to `c` */
1405 lttng_condition_destroy(c
);
1409 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1410 lttng_event_rule_destroy(res
.er
);
1414 struct condition_descr
{
1416 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1420 struct condition_descr condition_descrs
[] = {
1421 { "event-rule-matches", handle_condition_event
},
1425 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1429 struct lttng_condition
*cond
;
1430 const struct condition_descr
*descr
= NULL
;
1432 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1433 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1434 descr
= &condition_descrs
[i
];
1440 ERR("Unknown condition name '%s'", condition_name
);
1444 cond
= descr
->handler(argc
, argv
);
1446 /* The handler has already printed an error message. */
1457 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1460 size_t num_token
= 0;
1461 struct lttng_dynamic_pointer_array tokens
;
1462 struct lttng_rate_policy
*policy
= NULL
;
1463 enum lttng_rate_policy_type policy_type
;
1464 unsigned long long value
;
1465 char *policy_type_str
;
1466 char *policy_value_str
;
1469 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1471 /* Rate policy fields are separated by ':'. */
1472 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1474 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1478 * Early sanity check that the number of parameter is exactly 2.
1481 if (num_token
!= 2) {
1482 ERR("Rate policy format is invalid.");
1486 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1487 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1489 /* Parse the type. */
1490 if (strcmp(policy_type_str
, "once-after") == 0) {
1491 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1492 } else if (strcmp(policy_type_str
, "every") == 0) {
1493 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1495 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1499 /* Parse the value. */
1500 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1501 ERR("Failed to parse rate policy value `%s` as an integer.",
1507 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1511 switch (policy_type
) {
1512 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1513 policy
= lttng_rate_policy_every_n_create(value
);
1515 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1516 policy
= lttng_rate_policy_once_after_n_create(value
);
1522 if (policy
== NULL
) {
1523 ERR("Failed to create rate policy `%s`.", policy_str
);
1527 lttng_dynamic_pointer_array_reset(&tokens
);
1531 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1532 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1533 ARGPAR_OPT_DESCR_SENTINEL
1537 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1539 struct lttng_action
*action
= NULL
;
1540 struct argpar_state
*state
= NULL
;
1541 struct argpar_item
*item
= NULL
;
1543 struct lttng_rate_policy
*policy
= NULL
;
1545 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1547 ERR("Failed to allocate an argpar state.");
1552 enum argpar_state_parse_next_status status
;
1554 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1555 status
= argpar_state_parse_next(state
, &item
, &error
);
1556 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1559 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1560 /* Just stop parsing here. */
1562 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1566 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1568 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1569 const struct argpar_item_opt
*item_opt
=
1570 (const struct argpar_item_opt
*) item
;
1572 switch (item_opt
->descr
->id
) {
1573 case OPT_RATE_POLICY
:
1575 policy
= parse_rate_policy(item_opt
->arg
);
1585 const struct argpar_item_non_opt
*item_non_opt
;
1587 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1589 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1591 switch (item_non_opt
->non_opt_index
) {
1593 ERR("Unexpected argument `%s`.",
1600 *argc
-= argpar_state_get_ingested_orig_args(state
);
1601 *argv
+= argpar_state_get_ingested_orig_args(state
);
1603 action
= lttng_action_notify_create();
1605 ERR("Failed to create notify action");
1610 enum lttng_action_status status
;
1611 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1612 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1613 ERR("Failed to set rate policy");
1621 lttng_action_destroy(action
);
1625 lttng_rate_policy_destroy(policy
);
1626 argpar_state_destroy(state
);
1627 argpar_item_destroy(item
);
1632 * Generic handler for a kind of action that takes a session name and an
1633 * optional rate policy.
1636 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1638 struct lttng_action
*(*create_action_cb
)(void),
1639 enum lttng_action_status (*set_session_name_cb
)(
1640 struct lttng_action
*, const char *),
1641 enum lttng_action_status (*set_rate_policy_cb
)(
1642 struct lttng_action
*,
1643 const struct lttng_rate_policy
*),
1644 const char *action_name
)
1646 struct lttng_action
*action
= NULL
;
1647 struct argpar_state
*state
= NULL
;
1648 struct argpar_item
*item
= NULL
;
1649 const char *session_name_arg
= NULL
;
1651 enum lttng_action_status action_status
;
1652 struct lttng_rate_policy
*policy
= NULL
;
1654 assert(set_session_name_cb
);
1655 assert(set_rate_policy_cb
);
1657 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1658 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1659 ARGPAR_OPT_DESCR_SENTINEL
1662 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1664 ERR("Failed to allocate an argpar state.");
1669 enum argpar_state_parse_next_status status
;
1671 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1672 status
= argpar_state_parse_next(state
, &item
, &error
);
1673 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1676 } else if (status
==
1677 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1678 /* Just stop parsing here. */
1680 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1684 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1685 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1686 const struct argpar_item_opt
*item_opt
=
1687 (const struct argpar_item_opt
*) item
;
1689 switch (item_opt
->descr
->id
) {
1690 case OPT_RATE_POLICY
:
1692 policy
= parse_rate_policy(item_opt
->arg
);
1702 const struct argpar_item_non_opt
*item_non_opt
;
1703 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1705 switch (item_non_opt
->non_opt_index
) {
1707 session_name_arg
= item_non_opt
->arg
;
1710 ERR("Unexpected argument `%s`.",
1717 *argc
-= argpar_state_get_ingested_orig_args(state
);
1718 *argv
+= argpar_state_get_ingested_orig_args(state
);
1720 if (!session_name_arg
) {
1721 ERR("Missing session name.");
1725 action
= create_action_cb();
1727 ERR("Failed to allocate %s session action.", action_name
);
1731 action_status
= set_session_name_cb(action
, session_name_arg
);
1732 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1733 ERR("Failed to set action %s session's session name to '%s'.",
1734 action_name
, session_name_arg
);
1739 action_status
= set_rate_policy_cb(action
, policy
);
1740 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1741 ERR("Failed to set rate policy");
1749 lttng_action_destroy(action
);
1751 argpar_item_destroy(item
);
1753 lttng_rate_policy_destroy(policy
);
1755 argpar_state_destroy(state
);
1760 struct lttng_action
*handle_action_start_session(int *argc
,
1763 return handle_action_simple_session_with_policy(argc
, argv
,
1764 lttng_action_start_session_create
,
1765 lttng_action_start_session_set_session_name
,
1766 lttng_action_start_session_set_rate_policy
, "start");
1770 struct lttng_action
*handle_action_stop_session(int *argc
,
1773 return handle_action_simple_session_with_policy(argc
, argv
,
1774 lttng_action_stop_session_create
,
1775 lttng_action_stop_session_set_session_name
,
1776 lttng_action_stop_session_set_rate_policy
, "stop");
1780 struct lttng_action
*handle_action_rotate_session(int *argc
,
1783 return handle_action_simple_session_with_policy(argc
, argv
,
1784 lttng_action_rotate_session_create
,
1785 lttng_action_rotate_session_set_session_name
,
1786 lttng_action_rotate_session_set_rate_policy
,
1790 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1791 { OPT_NAME
, 'n', "name", true },
1792 { OPT_MAX_SIZE
, 'm', "max-size", true },
1793 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1794 { OPT_DATA_URL
, '\0', "data-url", true },
1795 { OPT_URL
, '\0', "url", true },
1796 { OPT_PATH
, '\0', "path", true },
1797 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1798 ARGPAR_OPT_DESCR_SENTINEL
1802 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1805 struct lttng_action
*action
= NULL
;
1806 struct argpar_state
*state
= NULL
;
1807 struct argpar_item
*item
= NULL
;
1808 const char *session_name_arg
= NULL
;
1809 char *snapshot_name_arg
= NULL
;
1810 char *ctrl_url_arg
= NULL
;
1811 char *data_url_arg
= NULL
;
1812 char *max_size_arg
= NULL
;
1813 char *url_arg
= NULL
;
1814 char *path_arg
= NULL
;
1816 enum lttng_action_status action_status
;
1817 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1818 struct lttng_rate_policy
*policy
= NULL
;
1820 unsigned int locations_specified
= 0;
1822 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1824 ERR("Failed to allocate an argpar state.");
1829 enum argpar_state_parse_next_status status
;
1831 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1832 status
= argpar_state_parse_next(state
, &item
, &error
);
1833 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1836 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1837 /* Just stop parsing here. */
1839 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1843 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1845 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1846 const struct argpar_item_opt
*item_opt
=
1847 (const struct argpar_item_opt
*) item
;
1849 switch (item_opt
->descr
->id
) {
1851 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1857 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1863 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1869 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1875 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1881 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1886 case OPT_RATE_POLICY
:
1888 policy
= parse_rate_policy(item_opt
->arg
);
1898 const struct argpar_item_non_opt
*item_non_opt
;
1900 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1902 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1904 switch (item_non_opt
->non_opt_index
) {
1906 session_name_arg
= item_non_opt
->arg
;
1909 ERR("Unexpected argument `%s`.",
1916 *argc
-= argpar_state_get_ingested_orig_args(state
);
1917 *argv
+= argpar_state_get_ingested_orig_args(state
);
1919 if (!session_name_arg
) {
1920 ERR("Missing session name.");
1924 /* --ctrl-url and --data-url must come in pair. */
1925 if (ctrl_url_arg
&& !data_url_arg
) {
1926 ERR("--ctrl-url is specified, but --data-url is missing.");
1930 if (!ctrl_url_arg
&& data_url_arg
) {
1931 ERR("--data-url is specified, but --ctrl-url is missing.");
1935 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1936 locations_specified
+= !!url_arg
;
1937 locations_specified
+= !!path_arg
;
1939 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1940 if (locations_specified
> 1) {
1941 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1946 * Did the user specify an option that implies using a
1947 * custom/unregistered output?
1949 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1950 snapshot_output
= lttng_snapshot_output_create();
1951 if (!snapshot_output
) {
1952 ERR("Failed to allocate a snapshot output.");
1957 action
= lttng_action_snapshot_session_create();
1959 ERR("Failed to allocate snapshot session action.");
1963 action_status
= lttng_action_snapshot_session_set_session_name(
1964 action
, session_name_arg
);
1965 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1966 ERR("Failed to set action snapshot session's session name to '%s'.",
1971 if (snapshot_name_arg
) {
1972 if (!snapshot_output
) {
1973 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1977 ret
= lttng_snapshot_output_set_name(
1978 snapshot_name_arg
, snapshot_output
);
1980 ERR("Failed to set name of snapshot output.");
1988 if (!snapshot_output
) {
1989 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1993 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1995 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1999 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
2001 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2009 struct lttng_uri
*uris
;
2011 if (!strstr(url_arg
, "://")) {
2012 ERR("Failed to parse '%s' as an URL.", url_arg
);
2016 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
2018 ERR("Failed to parse '%s' as an URL.", url_arg
);
2022 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2023 ret
= lttng_snapshot_output_set_local_path(
2024 uris
[0].dst
.path
, snapshot_output
);
2027 ERR("Failed to assign '%s' as a local destination.",
2032 ret
= lttng_snapshot_output_set_network_url(
2033 url_arg
, snapshot_output
);
2036 ERR("Failed to assign '%s' as a network URL.",
2044 ret
= lttng_snapshot_output_set_local_path(
2045 path_arg
, snapshot_output
);
2047 ERR("Failed to parse '%s' as a local path.", path_arg
);
2054 * Two argument form, network output with separate control and
2057 ret
= lttng_snapshot_output_set_network_urls(
2058 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2060 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2061 ctrl_url_arg
, data_url_arg
);
2066 if (snapshot_output
) {
2067 action_status
= lttng_action_snapshot_session_set_output(
2068 action
, snapshot_output
);
2069 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2070 ERR("Failed to set snapshot session action's output.");
2074 /* Ownership of `snapshot_output` has been transferred to the action. */
2075 snapshot_output
= NULL
;
2079 enum lttng_action_status status
;
2080 status
= lttng_action_snapshot_session_set_rate_policy(
2082 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2083 ERR("Failed to set rate policy");
2091 lttng_action_destroy(action
);
2095 free(snapshot_name_arg
);
2100 free(snapshot_output
);
2102 lttng_rate_policy_destroy(policy
);
2103 argpar_state_destroy(state
);
2104 argpar_item_destroy(item
);
2108 struct action_descr
{
2110 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2114 struct action_descr action_descrs
[] = {
2115 { "notify", handle_action_notify
},
2116 { "start-session", handle_action_start_session
},
2117 { "stop-session", handle_action_stop_session
},
2118 { "rotate-session", handle_action_rotate_session
},
2119 { "snapshot-session", handle_action_snapshot_session
},
2123 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2126 struct lttng_action
*action
;
2127 const struct action_descr
*descr
= NULL
;
2129 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2130 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2131 descr
= &action_descrs
[i
];
2137 ERR("Unknown action name: %s", action_name
);
2141 action
= descr
->handler(argc
, argv
);
2143 /* The handler has already printed an error message. */
2155 struct argpar_opt_descr add_trigger_options
[] = {
2156 { OPT_HELP
, 'h', "help", false },
2157 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2158 { OPT_CONDITION
, '\0', "condition", true },
2159 { OPT_ACTION
, '\0', "action", true },
2160 { OPT_NAME
, '\0', "name", true },
2161 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2162 ARGPAR_OPT_DESCR_SENTINEL
,
2166 void lttng_actions_destructor(void *p
)
2168 struct lttng_action
*action
= p
;
2170 lttng_action_destroy(action
);
2173 int cmd_add_trigger(int argc
, const char **argv
)
2176 int my_argc
= argc
- 1;
2177 const char **my_argv
= argv
+ 1;
2178 struct lttng_condition
*condition
= NULL
;
2179 struct lttng_dynamic_pointer_array actions
;
2180 struct argpar_state
*argpar_state
= NULL
;
2181 struct argpar_item
*argpar_item
= NULL
;
2182 struct lttng_action
*action_list
= NULL
;
2183 struct lttng_action
*action
= NULL
;
2184 struct lttng_trigger
*trigger
= NULL
;
2188 char *owner_uid
= NULL
;
2189 enum lttng_error_code ret_code
;
2190 struct mi_writer
*mi_writer
= NULL
;
2192 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2195 mi_writer
= mi_lttng_writer_create(
2196 fileno(stdout
), lttng_opt_mi
);
2202 /* Open command element. */
2203 ret
= mi_lttng_writer_command_open(mi_writer
,
2204 mi_lttng_element_command_add_trigger
);
2210 /* Open output element. */
2211 ret
= mi_lttng_writer_open_element(
2212 mi_writer
, mi_lttng_element_command_output
);
2220 enum argpar_state_parse_next_status status
;
2221 const struct argpar_item_opt
*item_opt
;
2224 argpar_state_destroy(argpar_state
);
2225 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2226 add_trigger_options
);
2227 if (!argpar_state
) {
2228 ERR("Failed to create argpar state.");
2232 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2233 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2234 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2237 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2240 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2244 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2246 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2247 const struct argpar_item_non_opt
*item_non_opt
=
2248 (const struct argpar_item_non_opt
*)
2251 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2255 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2257 ingested_args
= argpar_state_get_ingested_orig_args(
2260 my_argc
-= ingested_args
;
2261 my_argv
+= ingested_args
;
2263 switch (item_opt
->descr
->id
) {
2268 case OPT_LIST_OPTIONS
:
2269 list_cmd_options_argpar(stdout
, add_trigger_options
);
2275 ERR("A --condition was already given.");
2279 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2282 * An error message was already printed by
2292 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2295 * An error message was already printed by
2301 ret
= lttng_dynamic_pointer_array_add_pointer(
2304 ERR("Failed to add pointer to pointer array.");
2308 /* Ownership of the action was transferred to the list. */
2315 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2323 if (!assign_string(&owner_uid
, item_opt
->arg
,
2336 ERR("Missing --condition.");
2340 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2341 ERR("Need at least one --action.");
2345 action_list
= lttng_action_list_create();
2350 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2351 enum lttng_action_status status
;
2353 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2355 status
= lttng_action_list_add_action(action_list
, action
);
2356 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2361 * The `lttng_action_list_add_action()` takes a reference to
2362 * the action. We can destroy ours.
2364 lttng_action_destroy(action
);
2368 trigger
= lttng_trigger_create(condition
, action_list
);
2374 enum lttng_trigger_status trigger_status
;
2379 uid
= strtol(owner_uid
, &end
, 10);
2380 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2381 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2385 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2386 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2387 ERR("Failed to set trigger's user identity.");
2393 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2395 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2398 if (ret_code
!= LTTNG_OK
) {
2399 ERR("Failed to register trigger: %s.",
2400 lttng_strerror(-ret_code
));
2405 ret_code
= lttng_trigger_mi_serialize(trigger
, mi_writer
, NULL
);
2406 if (ret_code
!= LTTNG_OK
) {
2410 const char *returned_trigger_name
;
2411 const enum lttng_trigger_status trigger_status
=
2412 lttng_trigger_get_name(trigger
,
2413 &returned_trigger_name
);
2415 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2416 WARN("Failed to retrieve the added trigger's name.");
2418 MSG("Added trigger `%s`.", returned_trigger_name
);
2431 if (lttng_opt_mi
&& mi_writer
) {
2434 /* Close output element. */
2435 mi_ret
= mi_lttng_writer_close_element(mi_writer
);
2441 mi_ret
= mi_lttng_writer_write_element_bool(mi_writer
,
2442 mi_lttng_element_command_success
, ret
? 0 : 1);
2448 /* Command element close. */
2449 mi_ret
= mi_lttng_writer_command_close(mi_writer
);
2457 argpar_state_destroy(argpar_state
);
2458 argpar_item_destroy(argpar_item
);
2459 lttng_dynamic_pointer_array_reset(&actions
);
2460 lttng_condition_destroy(condition
);
2461 lttng_action_destroy(action_list
);
2462 lttng_action_destroy(action
);
2463 lttng_trigger_destroy(trigger
);
2467 if (mi_writer
&& mi_lttng_writer_destroy(mi_writer
)) {
2468 /* Preserve original error code. */
2469 ret
= ret
? ret
: CMD_ERROR
;