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/string-utils/string-utils.h"
19 #include "common/utils.h"
20 /* For lttng_event_rule_type_str(). */
21 #include <lttng/event-rule/event-rule-internal.h>
22 #include <lttng/lttng.h>
23 #include "common/filter/filter-ast.h"
24 #include "common/filter/filter-ir.h"
25 #include "common/dynamic-array.h"
27 #if (LTTNG_SYMBOL_NAME_LEN == 256)
28 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
31 #ifdef LTTNG_EMBED_HELP
32 static const char help_msg
[] =
33 #include <lttng-add-trigger.1.h>
65 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
66 { OPT_FILTER
, 'f', "filter", true },
67 { OPT_NAME
, 'n', "name", true },
68 { OPT_EXCLUDE_NAME
, 'x', "exclude-name", true },
69 { OPT_LOG_LEVEL
, 'l', "log-level", true },
70 { OPT_EVENT_NAME
, 'E', "event-name", true },
72 { OPT_TYPE
, 't', "type", true },
73 { OPT_LOCATION
, 'L', "location", true },
75 /* Capture descriptor */
76 { OPT_CAPTURE
, '\0', "capture", true },
78 ARGPAR_OPT_DESCR_SENTINEL
82 bool has_syscall_prefix(const char *arg
)
85 const char kernel_syscall_type_opt_prefix
[] = "kernel:syscall";
86 const size_t kernel_syscall_type_opt_prefix_len
=
87 sizeof(kernel_syscall_type_opt_prefix
) - 1;
88 const char syscall_type_opt_prefix
[] = "syscall";
89 const size_t syscall_type_opt_prefix_len
=
90 sizeof(syscall_type_opt_prefix
) - 1;
92 if (strncmp(arg
, syscall_type_opt_prefix
,
93 syscall_type_opt_prefix_len
) == 0) {
95 } else if (strncmp(arg
, kernel_syscall_type_opt_prefix
,
96 kernel_syscall_type_opt_prefix_len
) == 0) {
106 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
, const char *arg
)
110 if (*dest
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
111 ERR("More than one `--type` was specified.");
115 if (strcmp(arg
, "user") == 0 || strcmp(arg
, "user:tracepoint") == 0) {
116 *dest
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
117 } else if (strcmp(arg
, "kernel") == 0 ||
118 strcmp(arg
, "kernel:tracepoint") == 0) {
119 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
;
120 } else if (strcmp(arg
, "jul") == 0 || strcmp(arg
, "jul:logging") == 0) {
121 *dest
= LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
;
122 } else if (strcmp(arg
, "log4j") == 0 ||
123 strcmp(arg
, "log4j:logging") == 0) {
124 *dest
= LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
;
125 } else if (strcmp(arg
, "python") == 0 ||
126 strcmp(arg
, "python:logging") == 0) {
127 *dest
= LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
;
128 } else if (strcmp(arg
, "kprobe") == 0 ||
129 strcmp(arg
, "kernel:kprobe") == 0) {
130 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
;
131 } else if (strcmp(arg
, "kernel:uprobe") == 0) {
132 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
;
133 } else if (has_syscall_prefix(arg
)) {
135 * Matches the following:
139 * - syscall:entry+exit
142 * - kernel:syscall:entry
143 * - kernel:syscall:exit
144 * - kernel:syscall:entry+exit
147 * Validation for the right side is left to further usage sites.
149 *dest
= LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
;
151 ERR("Invalid `--type` value: %s", arg
);
166 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
171 ERR("Duplicate '%s' given.", opt_name
);
177 PERROR("Failed to allocate string '%s'.", opt_name
);
191 static bool parse_syscall_emission_site_from_type(const char *str
,
192 enum lttng_event_rule_kernel_syscall_emission_site
*type
)
195 const char kernel_prefix
[] = "kernel:";
196 const size_t kernel_prefix_len
= sizeof(kernel_prefix
) - 1;
199 * If the passed string is of the form "kernel:syscall*", move the
200 * pointer passed "kernel:".
202 if (strncmp(str
, kernel_prefix
, kernel_prefix_len
) == 0) {
203 str
= &str
[kernel_prefix_len
];
206 if (strcmp(str
, "syscall") == 0 ||
207 strcmp(str
, "syscall:entry+exit") == 0) {
208 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
;
209 } else if (strcmp(str
, "syscall:entry") == 0) {
210 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
;
211 } else if (strcmp(str
, "syscall:exit") == 0) {
212 *type
= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
;
224 * Parse `str` as a log level against the passed event rule type.
226 * Return the log level in `*log_level`. Return true in `*log_level_only` if
227 * the string specifies exactly this log level, false if it specifies at least
230 * Return true if the string was successfully parsed as a log level string.
232 static bool parse_log_level_string(const char *str
,
233 enum lttng_event_rule_type event_rule_type
,
235 bool *log_level_only
)
239 switch (event_rule_type
) {
240 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
242 enum lttng_loglevel log_level_min
, log_level_max
;
243 if (!loglevel_parse_range_string(
244 str
, &log_level_min
, &log_level_max
)) {
248 /* Only support VAL and VAL.. for now. */
249 if (log_level_min
!= log_level_max
&&
250 log_level_max
!= LTTNG_LOGLEVEL_EMERG
) {
254 *log_level
= (int) log_level_min
;
255 *log_level_only
= log_level_min
== log_level_max
;
258 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
260 enum lttng_loglevel_log4j log_level_min
, log_level_max
;
261 if (!loglevel_log4j_parse_range_string(
262 str
, &log_level_min
, &log_level_max
)) {
266 /* Only support VAL and VAL.. for now. */
267 if (log_level_min
!= log_level_max
&&
268 log_level_max
!= LTTNG_LOGLEVEL_LOG4J_FATAL
) {
272 *log_level
= (int) log_level_min
;
273 *log_level_only
= log_level_min
== log_level_max
;
276 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
278 enum lttng_loglevel_jul log_level_min
, log_level_max
;
279 if (!loglevel_jul_parse_range_string(
280 str
, &log_level_min
, &log_level_max
)) {
284 /* Only support VAL and VAL.. for now. */
285 if (log_level_min
!= log_level_max
&&
286 log_level_max
!= LTTNG_LOGLEVEL_JUL_SEVERE
) {
290 *log_level
= (int) log_level_min
;
291 *log_level_only
= log_level_min
== log_level_max
;
294 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
296 enum lttng_loglevel_python log_level_min
, log_level_max
;
297 if (!loglevel_python_parse_range_string(
298 str
, &log_level_min
, &log_level_max
)) {
302 /* Only support VAL and VAL.. for now. */
303 if (log_level_min
!= log_level_max
&&
305 LTTNG_LOGLEVEL_PYTHON_CRITICAL
) {
309 *log_level
= (int) log_level_min
;
310 *log_level_only
= log_level_min
== log_level_max
;
314 /* Invalid domain type. */
328 static int parse_kernel_probe_opts(const char *source
,
329 struct lttng_kernel_probe_location
**location
)
334 char name
[LTTNG_SYMBOL_NAME_LEN
];
335 char *symbol_name
= NULL
;
338 /* Check for symbol+offset. */
339 match
= sscanf(source
,
340 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
344 if (*s_hex
== '\0') {
345 ERR("Kernel probe symbol offset is missing.");
349 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
351 PERROR("Failed to copy kernel probe location symbol name.");
354 offset
= strtoul(s_hex
, NULL
, 0);
356 *location
= lttng_kernel_probe_location_symbol_create(
357 symbol_name
, offset
);
359 ERR("Failed to create symbol kernel probe location.");
366 /* Check for symbol. */
367 if (isalpha(name
[0]) || name
[0] == '_') {
368 match
= sscanf(source
,
369 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
373 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
375 ERR("Failed to copy kernel probe location symbol name.");
379 *location
= lttng_kernel_probe_location_symbol_create(
382 ERR("Failed to create symbol kernel probe location.");
390 /* Check for address. */
391 match
= sscanf(source
, "%18s", s_hex
);
395 if (*s_hex
== '\0') {
396 ERR("Invalid kernel probe location address.");
400 address
= strtoul(s_hex
, NULL
, 0);
401 *location
= lttng_kernel_probe_location_address_create(address
);
403 ERR("Failed to create symbol kernel probe location.");
421 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
422 const struct ir_load_expression
*load_expr
,
423 const char *capture_str
)
425 char *provider_name
= NULL
;
426 struct lttng_event_expr
*event_expr
= NULL
;
427 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
428 const enum ir_load_expression_type load_expr_child_type
=
431 switch (load_expr_child_type
) {
432 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
433 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
435 const char *field_name
;
437 load_expr_op
= load_expr_op
->next
;
438 assert(load_expr_op
);
439 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
440 field_name
= load_expr_op
->u
.symbol
;
443 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
444 lttng_event_expr_event_payload_field_create(field_name
) :
445 lttng_event_expr_channel_context_field_create(field_name
);
447 ERR("Failed to create %s event expression: field name = `%s`.",
448 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
449 "payload field" : "channel context",
456 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
459 const char *type_name
;
460 const char *field_name
;
462 load_expr_op
= load_expr_op
->next
;
463 assert(load_expr_op
);
464 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
465 field_name
= load_expr_op
->u
.symbol
;
469 * The field name needs to be of the form PROVIDER:TYPE. We
472 colon
= strchr(field_name
, ':');
474 ERR("Invalid app-specific context field name: missing colon in `%s`.",
479 type_name
= colon
+ 1;
480 if (*type_name
== '\0') {
481 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
486 provider_name
= strndup(field_name
, colon
- field_name
);
487 if (!provider_name
) {
488 PERROR("Failed to allocate field name string");
492 event_expr
= lttng_event_expr_app_specific_context_field_create(
493 provider_name
, type_name
);
495 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
496 provider_name
, type_name
);
503 ERR("%s: unexpected load expr type %d.", __func__
,
508 load_expr_op
= load_expr_op
->next
;
510 /* There may be a single array index after that. */
511 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
512 struct lttng_event_expr
*index_event_expr
;
513 const uint64_t index
= load_expr_op
->u
.index
;
515 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
516 if (!index_event_expr
) {
517 ERR("Failed to create array field element event expression.");
521 event_expr
= index_event_expr
;
522 load_expr_op
= load_expr_op
->next
;
525 switch (load_expr_op
->type
) {
526 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
528 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
529 * always found at the end of the chain.
532 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
533 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
538 ERR("%s: unexpected load expression operator %s.", __func__
,
539 ir_load_expression_type_str(load_expr_op
->type
));
546 lttng_event_expr_destroy(event_expr
);
556 struct lttng_event_expr
*ir_op_load_to_event_expr(
557 const struct ir_op
*ir
, const char *capture_str
)
559 struct lttng_event_expr
*event_expr
= NULL
;
561 assert(ir
->op
== IR_OP_LOAD
);
563 switch (ir
->data_type
) {
564 case IR_DATA_EXPRESSION
:
566 const struct ir_load_expression
*ir_load_expr
=
567 ir
->u
.load
.u
.expression
;
569 event_expr
= ir_op_load_expr_to_event_expr(
570 ir_load_expr
, capture_str
);
574 ERR("%s: unexpected data type: %s.", __func__
,
575 ir_data_type_str(ir
->data_type
));
583 const char *ir_operator_type_human_str(enum ir_op_type op
)
605 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
606 const char *capture_str
)
608 struct lttng_event_expr
*event_expr
= NULL
;
610 assert(ir
->op
== IR_OP_ROOT
);
611 ir
= ir
->u
.root
.child
;
615 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
620 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
622 ir_operator_type_human_str(ir
->op
));
625 ERR("%s: unexpected IR op type: %s.", __func__
,
626 ir_op_type_str(ir
->op
));
634 void destroy_event_expr(void *ptr
)
636 lttng_event_expr_destroy(ptr
);
639 struct parse_event_rule_res
{
641 struct lttng_event_rule
*er
;
643 /* Array of `struct lttng_event_expr *` */
644 struct lttng_dynamic_pointer_array capture_descriptors
;
648 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
650 enum lttng_event_rule_type event_rule_type
=
651 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
652 struct argpar_state
*state
;
653 struct argpar_item
*item
= NULL
;
655 int consumed_args
= -1;
656 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
657 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
658 struct parse_event_rule_res res
= { 0 };
659 struct lttng_event_expr
*event_expr
= NULL
;
660 struct filter_parser_ctx
*parser_ctx
= NULL
;
661 struct lttng_log_level_rule
*log_level_rule
= NULL
;
663 /* Event rule type option */
664 char *event_rule_type_str
= NULL
;
666 /* Tracepoint and syscall options. */
668 /* Array of strings. */
669 struct lttng_dynamic_pointer_array exclude_names
;
671 /* For userspace / kernel probe and function. */
672 char *location
= NULL
;
673 char *event_name
= NULL
;
679 char *log_level_str
= NULL
;
681 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
684 lttng_dynamic_pointer_array_init(&exclude_names
, free
);
686 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
688 ERR("Failed to allocate an argpar state.");
693 enum argpar_state_parse_next_status status
;
695 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
696 status
= argpar_state_parse_next(state
, &item
, &error
);
697 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
700 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
701 /* Just stop parsing here. */
703 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
707 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
709 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
710 const struct argpar_item_opt
*item_opt
=
711 (const struct argpar_item_opt
*) item
;
713 switch (item_opt
->descr
->id
) {
715 if (!assign_event_rule_type(&event_rule_type
,
720 /* Save the string for later use. */
721 if (!assign_string(&event_rule_type_str
,
729 if (!assign_string(&location
,
737 if (!assign_string(&event_name
,
739 "--event-name/-E")) {
745 if (!assign_string(&filter
, item_opt
->arg
,
752 if (!assign_string(&name
, item_opt
->arg
,
758 case OPT_EXCLUDE_NAME
:
762 ret
= lttng_dynamic_pointer_array_add_pointer(
764 strdup(item_opt
->arg
));
766 ERR("Failed to add pointer to dynamic pointer array.");
773 if (!assign_string(&log_level_str
,
774 item_opt
->arg
, "--log-level/-l")) {
782 const char *capture_str
= item_opt
->arg
;
784 ret
= filter_parser_ctx_create_from_filter_expression(
785 capture_str
, &parser_ctx
);
787 ERR("Failed to parse capture expression `%s`.",
792 event_expr
= ir_op_root_to_event_expr(
797 * ir_op_root_to_event_expr has printed
803 ret
= lttng_dynamic_pointer_array_add_pointer(
804 &res
.capture_descriptors
,
811 * The ownership of event expression was
812 * transferred to the dynamic array.
822 const struct argpar_item_non_opt
*item_non_opt
=
823 (const struct argpar_item_non_opt
*)
826 /* Don't accept non-option arguments. */
827 ERR("Unexpected argument '%s'", item_non_opt
->arg
);
832 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
833 event_rule_type
= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
;
837 * Option --name is applicable to event rules of type kernel, user, jul,
838 * log4j,python and syscall. If --name is omitted, it is implicitly
841 switch (event_rule_type
) {
842 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
843 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
844 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
845 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
846 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
847 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
855 ERR("Can't use --name with %s event rules.",
856 lttng_event_rule_type_str(
861 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
862 ERR("Can't use --exclude-name/-x with %s event rules.",
863 lttng_event_rule_type_str(
870 * Option --location is only applicable to (and mandatory for) event
871 * rules of type {k,u}probe and function.
873 * Option --event-name is only applicable to event rules of type probe.
874 * If omitted, it defaults to the location.
876 switch (event_rule_type
) {
877 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
878 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
880 ERR("Event rule of type %s requires a --location.",
881 lttng_event_rule_type_str(event_rule_type
));
886 event_name
= strdup(location
);
893 ERR("Can't use --location with %s event rules.",
894 lttng_event_rule_type_str(event_rule_type
));
899 ERR("Can't use --event-name with %s event rules.",
900 lttng_event_rule_type_str(
907 * Update *argc and *argv so our caller can keep parsing what follows.
909 consumed_args
= argpar_state_get_ingested_orig_args(state
);
910 assert(consumed_args
>= 0);
911 *argc
-= consumed_args
;
912 *argv
+= consumed_args
;
915 * Adding a filter to a probe, function or userspace-probe would be
916 * denied by the kernel tracer as it's not supported at the moment. We
917 * do an early check here to warn the user.
920 switch (event_rule_type
) {
921 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
922 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
923 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
924 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
925 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
926 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
929 ERR("Filter expressions are not supported for %s event rules.",
930 lttng_event_rule_type_str(event_rule_type
));
936 * If --exclude-name/-x was passed, split it into an exclusion list.
937 * Exclusions are only supported by
938 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
940 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
941 if (event_rule_type
!= LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
) {
942 ERR("Event name exclusions are not yet implemented for %s event rules.",
943 lttng_event_rule_type_str(event_rule_type
));
947 if (validate_exclusion_list(name
, &exclude_names
) != 0) {
949 * Assume validate_exclusion_list already prints an
957 switch (event_rule_type
) {
958 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
959 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
960 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
961 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
966 if (strcmp(log_level_str
, "..") == 0) {
968 * ".." is the same as passing no log level
969 * option and correspond to the "ANY" case.
974 if (!parse_log_level_string(log_level_str
, event_rule_type
,
975 &log_level
, &log_level_only
)) {
976 ERR("Failed to parse log level string `%s`.",
981 if (log_level_only
) {
982 log_level_rule
= lttng_log_level_rule_exactly_create(log_level
);
984 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(log_level
);
987 if (log_level_rule
== NULL
) {
988 ERR("Failed to create log level rule object.");
994 ERR("Log levels are not supported for %s event rules.",
995 lttng_event_rule_type_str(event_rule_type
));
1000 /* Finally, create the event rule object. */
1001 switch (event_rule_type
) {
1002 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
1004 enum lttng_event_rule_status event_rule_status
;
1006 res
.er
= lttng_event_rule_user_tracepoint_create();
1008 ERR("Failed to create user_tracepoint event rule.");
1013 event_rule_status
= lttng_event_rule_user_tracepoint_set_name_pattern(
1015 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1016 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1023 event_rule_status
= lttng_event_rule_user_tracepoint_set_filter(
1025 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1026 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1032 /* Set exclusion list. */
1033 if (lttng_dynamic_pointer_array_get_count(&exclude_names
) > 0) {
1035 int count
= lttng_dynamic_pointer_array_get_count(
1038 for (n
= 0; n
< count
; n
++) {
1039 const char *exclude_name
=
1040 lttng_dynamic_pointer_array_get_pointer(
1045 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1048 if (event_rule_status
!=
1049 LTTNG_EVENT_RULE_STATUS_OK
) {
1050 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1057 if (log_level_rule
) {
1059 lttng_event_rule_user_tracepoint_set_log_level_rule(
1060 res
.er
, log_level_rule
);
1062 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1063 ERR("Failed to set log level on event fule.");
1070 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
1072 enum lttng_event_rule_status event_rule_status
;
1074 res
.er
= lttng_event_rule_kernel_tracepoint_create();
1076 ERR("Failed to create kernel_tracepoint event rule.");
1081 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_name_pattern(
1083 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1084 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1091 event_rule_status
= lttng_event_rule_kernel_tracepoint_set_filter(
1093 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1094 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1101 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1103 enum lttng_event_rule_status event_rule_status
;
1105 res
.er
= lttng_event_rule_jul_logging_create();
1107 ERR("Failed to create jul_logging event rule.");
1112 event_rule_status
= lttng_event_rule_jul_logging_set_name_pattern(
1114 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1115 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1122 event_rule_status
= lttng_event_rule_jul_logging_set_filter(
1124 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1125 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1131 if (log_level_rule
) {
1133 lttng_event_rule_jul_logging_set_log_level_rule(
1134 res
.er
, log_level_rule
);
1136 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1137 ERR("Failed to set log level on event fule.");
1143 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1145 enum lttng_event_rule_status event_rule_status
;
1147 res
.er
= lttng_event_rule_log4j_logging_create();
1149 ERR("Failed to create jul_logging event rule.");
1154 event_rule_status
= lttng_event_rule_log4j_logging_set_name_pattern(
1156 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1157 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1164 event_rule_status
= lttng_event_rule_log4j_logging_set_filter(
1166 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1167 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1173 if (log_level_rule
) {
1175 lttng_event_rule_log4j_logging_set_log_level_rule(
1176 res
.er
, log_level_rule
);
1178 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1179 ERR("Failed to set log level on event fule.");
1185 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1187 enum lttng_event_rule_status event_rule_status
;
1189 res
.er
= lttng_event_rule_python_logging_create();
1191 ERR("Failed to create jul_logging event rule.");
1196 event_rule_status
= lttng_event_rule_python_logging_set_name_pattern(
1198 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1199 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1206 event_rule_status
= lttng_event_rule_python_logging_set_filter(
1208 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1209 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1215 if (log_level_rule
) {
1217 lttng_event_rule_python_logging_set_log_level_rule(
1218 res
.er
, log_level_rule
);
1220 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1221 ERR("Failed to set log level on event fule.");
1227 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
1230 enum lttng_event_rule_status event_rule_status
;
1232 ret
= parse_kernel_probe_opts(
1233 location
, &kernel_probe_location
);
1235 ERR("Failed to parse kernel probe location.");
1239 assert(kernel_probe_location
);
1240 res
.er
= lttng_event_rule_kernel_kprobe_create(kernel_probe_location
);
1242 ERR("Failed to create kprobe event rule.");
1247 lttng_event_rule_kernel_kprobe_set_event_name(
1248 res
.er
, event_name
);
1249 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1250 ERR("Failed to set kprobe event rule's name to '%s'.",
1257 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
1260 enum lttng_event_rule_status event_rule_status
;
1262 ret
= parse_userspace_probe_opts(
1263 location
, &userspace_probe_location
);
1265 ERR("Failed to parse user space probe location.");
1269 res
.er
= lttng_event_rule_kernel_uprobe_create(userspace_probe_location
);
1271 ERR("Failed to create userspace probe event rule.");
1276 lttng_event_rule_kernel_uprobe_set_event_name(
1277 res
.er
, event_name
);
1278 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1279 ERR("Failed to set user space probe event rule's name to '%s'.",
1286 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
1288 enum lttng_event_rule_status event_rule_status
;
1289 enum lttng_event_rule_kernel_syscall_emission_site emission_site
;
1291 if (!parse_syscall_emission_site_from_type(
1292 event_rule_type_str
, &emission_site
)) {
1293 ERR("Failed to parse syscall type '%s'.", event_rule_type_str
);
1297 res
.er
= lttng_event_rule_kernel_syscall_create(emission_site
);
1299 ERR("Failed to create syscall event rule.");
1303 event_rule_status
= lttng_event_rule_kernel_syscall_set_name_pattern(
1305 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1306 ERR("Failed to set syscall event rule's pattern to '%s'.",
1312 event_rule_status
= lttng_event_rule_kernel_syscall_set_filter(
1314 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1315 ERR("Failed to set syscall event rule's filter to '%s'.",
1331 lttng_event_rule_destroy(res
.er
);
1333 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1337 filter_parser_ctx_free(parser_ctx
);
1340 lttng_event_expr_destroy(event_expr
);
1341 argpar_item_destroy(item
);
1343 argpar_state_destroy(state
);
1346 lttng_dynamic_pointer_array_reset(&exclude_names
);
1347 free(log_level_str
);
1350 free(event_rule_type_str
);
1352 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1353 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1354 lttng_log_level_rule_destroy(log_level_rule
);
1359 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1361 struct parse_event_rule_res res
;
1362 struct lttng_condition
*c
;
1365 res
= parse_event_rule(argc
, argv
);
1371 c
= lttng_condition_event_rule_matches_create(res
.er
);
1372 lttng_event_rule_destroy(res
.er
);
1378 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1380 enum lttng_condition_status status
;
1381 struct lttng_event_expr
**expr
=
1382 lttng_dynamic_array_get_element(
1383 &res
.capture_descriptors
.array
, i
);
1387 status
= lttng_condition_event_rule_matches_append_capture_descriptor(
1389 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1390 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1391 ERR("The capture feature is unsupported by the event-rule condition type");
1397 /* Ownership of event expression moved to `c` */
1404 lttng_condition_destroy(c
);
1408 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1409 lttng_event_rule_destroy(res
.er
);
1413 struct condition_descr
{
1415 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1419 struct condition_descr condition_descrs
[] = {
1420 { "event-rule-matches", handle_condition_event
},
1424 struct lttng_condition
*parse_condition(const char *condition_name
, int *argc
,
1428 struct lttng_condition
*cond
;
1429 const struct condition_descr
*descr
= NULL
;
1431 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1432 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1433 descr
= &condition_descrs
[i
];
1439 ERR("Unknown condition name '%s'", condition_name
);
1443 cond
= descr
->handler(argc
, argv
);
1445 /* The handler has already printed an error message. */
1456 static struct lttng_rate_policy
*parse_rate_policy(const char *policy_str
)
1459 size_t num_token
= 0;
1460 struct lttng_dynamic_pointer_array tokens
;
1461 struct lttng_rate_policy
*policy
= NULL
;
1462 enum lttng_rate_policy_type policy_type
;
1463 unsigned long long value
;
1464 char *policy_type_str
;
1465 char *policy_value_str
;
1468 lttng_dynamic_pointer_array_init(&tokens
, NULL
);
1470 /* Rate policy fields are separated by ':'. */
1471 ret
= strutils_split(policy_str
, ':', 1, &tokens
);
1473 num_token
= lttng_dynamic_pointer_array_get_count(&tokens
);
1477 * Early sanity check that the number of parameter is exactly 2.
1480 if (num_token
!= 2) {
1481 ERR("Rate policy format is invalid.");
1485 policy_type_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 0);
1486 policy_value_str
= lttng_dynamic_pointer_array_get_pointer(&tokens
, 1);
1488 /* Parse the type. */
1489 if (strcmp(policy_type_str
, "once-after") == 0) {
1490 policy_type
= LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
;
1491 } else if (strcmp(policy_type_str
, "every") == 0) {
1492 policy_type
= LTTNG_RATE_POLICY_TYPE_EVERY_N
;
1494 ERR("Rate policy type `%s` unknown.", policy_type_str
);
1498 /* Parse the value. */
1499 if (utils_parse_unsigned_long_long(policy_value_str
, &value
) != 0) {
1500 ERR("Failed to parse rate policy value `%s` as an integer.",
1506 ERR("Rate policy value `%s` must be > 0.", policy_value_str
);
1510 switch (policy_type
) {
1511 case LTTNG_RATE_POLICY_TYPE_EVERY_N
:
1512 policy
= lttng_rate_policy_every_n_create(value
);
1514 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N
:
1515 policy
= lttng_rate_policy_once_after_n_create(value
);
1521 if (policy
== NULL
) {
1522 ERR("Failed to create rate policy `%s`.", policy_str
);
1526 lttng_dynamic_pointer_array_reset(&tokens
);
1530 static const struct argpar_opt_descr notify_action_opt_descrs
[] = {
1531 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1532 ARGPAR_OPT_DESCR_SENTINEL
1536 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1538 struct lttng_action
*action
= NULL
;
1539 struct argpar_state
*state
= NULL
;
1540 struct argpar_item
*item
= NULL
;
1542 struct lttng_rate_policy
*policy
= NULL
;
1544 state
= argpar_state_create(*argc
, *argv
, notify_action_opt_descrs
);
1546 ERR("Failed to allocate an argpar state.");
1551 enum argpar_state_parse_next_status status
;
1553 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1554 status
= argpar_state_parse_next(state
, &item
, &error
);
1555 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1558 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1559 /* Just stop parsing here. */
1561 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1565 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1567 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1568 const struct argpar_item_opt
*item_opt
=
1569 (const struct argpar_item_opt
*) item
;
1571 switch (item_opt
->descr
->id
) {
1572 case OPT_RATE_POLICY
:
1574 policy
= parse_rate_policy(item_opt
->arg
);
1584 const struct argpar_item_non_opt
*item_non_opt
;
1586 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1588 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1590 switch (item_non_opt
->non_opt_index
) {
1592 ERR("Unexpected argument `%s`.",
1599 *argc
-= argpar_state_get_ingested_orig_args(state
);
1600 *argv
+= argpar_state_get_ingested_orig_args(state
);
1602 action
= lttng_action_notify_create();
1604 ERR("Failed to create notify action");
1609 enum lttng_action_status status
;
1610 status
= lttng_action_notify_set_rate_policy(action
, policy
);
1611 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1612 ERR("Failed to set rate policy");
1620 lttng_action_destroy(action
);
1624 lttng_rate_policy_destroy(policy
);
1625 argpar_state_destroy(state
);
1626 argpar_item_destroy(item
);
1631 * Generic handler for a kind of action that takes a session name and an
1632 * optional rate policy.
1635 static struct lttng_action
*handle_action_simple_session_with_policy(int *argc
,
1637 struct lttng_action
*(*create_action_cb
)(void),
1638 enum lttng_action_status (*set_session_name_cb
)(
1639 struct lttng_action
*, const char *),
1640 enum lttng_action_status (*set_rate_policy_cb
)(
1641 struct lttng_action
*,
1642 const struct lttng_rate_policy
*),
1643 const char *action_name
)
1645 struct lttng_action
*action
= NULL
;
1646 struct argpar_state
*state
= NULL
;
1647 struct argpar_item
*item
= NULL
;
1648 const char *session_name_arg
= NULL
;
1650 enum lttng_action_status action_status
;
1651 struct lttng_rate_policy
*policy
= NULL
;
1653 assert(set_session_name_cb
);
1654 assert(set_rate_policy_cb
);
1656 const struct argpar_opt_descr rate_policy_opt_descrs
[] = {
1657 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1658 ARGPAR_OPT_DESCR_SENTINEL
1661 state
= argpar_state_create(*argc
, *argv
, rate_policy_opt_descrs
);
1663 ERR("Failed to allocate an argpar state.");
1668 enum argpar_state_parse_next_status status
;
1670 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1671 status
= argpar_state_parse_next(state
, &item
, &error
);
1672 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1675 } else if (status
==
1676 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1677 /* Just stop parsing here. */
1679 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1683 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1684 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1685 const struct argpar_item_opt
*item_opt
=
1686 (const struct argpar_item_opt
*) item
;
1688 switch (item_opt
->descr
->id
) {
1689 case OPT_RATE_POLICY
:
1691 policy
= parse_rate_policy(item_opt
->arg
);
1701 const struct argpar_item_non_opt
*item_non_opt
;
1702 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1704 switch (item_non_opt
->non_opt_index
) {
1706 session_name_arg
= item_non_opt
->arg
;
1709 ERR("Unexpected argument `%s`.",
1716 *argc
-= argpar_state_get_ingested_orig_args(state
);
1717 *argv
+= argpar_state_get_ingested_orig_args(state
);
1719 if (!session_name_arg
) {
1720 ERR("Missing session name.");
1724 action
= create_action_cb();
1726 ERR("Failed to allocate %s session action.", action_name
);
1730 action_status
= set_session_name_cb(action
, session_name_arg
);
1731 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1732 ERR("Failed to set action %s session's session name to '%s'.",
1733 action_name
, session_name_arg
);
1738 action_status
= set_rate_policy_cb(action
, policy
);
1739 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1740 ERR("Failed to set rate policy");
1748 lttng_action_destroy(action
);
1750 argpar_item_destroy(item
);
1752 lttng_rate_policy_destroy(policy
);
1754 argpar_state_destroy(state
);
1759 struct lttng_action
*handle_action_start_session(int *argc
,
1762 return handle_action_simple_session_with_policy(argc
, argv
,
1763 lttng_action_start_session_create
,
1764 lttng_action_start_session_set_session_name
,
1765 lttng_action_start_session_set_rate_policy
, "start");
1769 struct lttng_action
*handle_action_stop_session(int *argc
,
1772 return handle_action_simple_session_with_policy(argc
, argv
,
1773 lttng_action_stop_session_create
,
1774 lttng_action_stop_session_set_session_name
,
1775 lttng_action_stop_session_set_rate_policy
, "stop");
1779 struct lttng_action
*handle_action_rotate_session(int *argc
,
1782 return handle_action_simple_session_with_policy(argc
, argv
,
1783 lttng_action_rotate_session_create
,
1784 lttng_action_rotate_session_set_session_name
,
1785 lttng_action_rotate_session_set_rate_policy
,
1789 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1790 { OPT_NAME
, 'n', "name", true },
1791 { OPT_MAX_SIZE
, 'm', "max-size", true },
1792 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1793 { OPT_DATA_URL
, '\0', "data-url", true },
1794 { OPT_URL
, '\0', "url", true },
1795 { OPT_PATH
, '\0', "path", true },
1796 { OPT_RATE_POLICY
, '\0', "rate-policy", true },
1797 ARGPAR_OPT_DESCR_SENTINEL
1801 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1804 struct lttng_action
*action
= NULL
;
1805 struct argpar_state
*state
= NULL
;
1806 struct argpar_item
*item
= NULL
;
1807 const char *session_name_arg
= NULL
;
1808 char *snapshot_name_arg
= NULL
;
1809 char *ctrl_url_arg
= NULL
;
1810 char *data_url_arg
= NULL
;
1811 char *max_size_arg
= NULL
;
1812 char *url_arg
= NULL
;
1813 char *path_arg
= NULL
;
1815 enum lttng_action_status action_status
;
1816 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1817 struct lttng_rate_policy
*policy
= NULL
;
1819 unsigned int locations_specified
= 0;
1821 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1823 ERR("Failed to allocate an argpar state.");
1828 enum argpar_state_parse_next_status status
;
1830 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1831 status
= argpar_state_parse_next(state
, &item
, &error
);
1832 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1835 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1836 /* Just stop parsing here. */
1838 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1842 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1844 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1845 const struct argpar_item_opt
*item_opt
=
1846 (const struct argpar_item_opt
*) item
;
1848 switch (item_opt
->descr
->id
) {
1850 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1856 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1862 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1868 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1874 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1880 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1885 case OPT_RATE_POLICY
:
1887 policy
= parse_rate_policy(item_opt
->arg
);
1897 const struct argpar_item_non_opt
*item_non_opt
;
1899 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1901 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1903 switch (item_non_opt
->non_opt_index
) {
1905 session_name_arg
= item_non_opt
->arg
;
1908 ERR("Unexpected argument `%s`.",
1915 *argc
-= argpar_state_get_ingested_orig_args(state
);
1916 *argv
+= argpar_state_get_ingested_orig_args(state
);
1918 if (!session_name_arg
) {
1919 ERR("Missing session name.");
1923 /* --ctrl-url and --data-url must come in pair. */
1924 if (ctrl_url_arg
&& !data_url_arg
) {
1925 ERR("--ctrl-url is specified, but --data-url is missing.");
1929 if (!ctrl_url_arg
&& data_url_arg
) {
1930 ERR("--data-url is specified, but --ctrl-url is missing.");
1934 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1935 locations_specified
+= !!url_arg
;
1936 locations_specified
+= !!path_arg
;
1938 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1939 if (locations_specified
> 1) {
1940 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1945 * Did the user specify an option that implies using a
1946 * custom/unregistered output?
1948 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1949 snapshot_output
= lttng_snapshot_output_create();
1950 if (!snapshot_output
) {
1951 ERR("Failed to allocate a snapshot output.");
1956 action
= lttng_action_snapshot_session_create();
1958 ERR("Failed to allocate snapshot session action.");
1962 action_status
= lttng_action_snapshot_session_set_session_name(
1963 action
, session_name_arg
);
1964 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1965 ERR("Failed to set action snapshot session's session name to '%s'.",
1970 if (snapshot_name_arg
) {
1971 if (!snapshot_output
) {
1972 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1976 ret
= lttng_snapshot_output_set_name(
1977 snapshot_name_arg
, snapshot_output
);
1979 ERR("Failed to set name of snapshot output.");
1987 if (!snapshot_output
) {
1988 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1992 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1994 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1998 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
2000 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
2008 struct lttng_uri
*uris
;
2010 if (!strstr(url_arg
, "://")) {
2011 ERR("Failed to parse '%s' as an URL.", url_arg
);
2015 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
2017 ERR("Failed to parse '%s' as an URL.", url_arg
);
2021 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2022 ret
= lttng_snapshot_output_set_local_path(
2023 uris
[0].dst
.path
, snapshot_output
);
2026 ERR("Failed to assign '%s' as a local destination.",
2031 ret
= lttng_snapshot_output_set_network_url(
2032 url_arg
, snapshot_output
);
2035 ERR("Failed to assign '%s' as a network URL.",
2043 ret
= lttng_snapshot_output_set_local_path(
2044 path_arg
, snapshot_output
);
2046 ERR("Failed to parse '%s' as a local path.", path_arg
);
2053 * Two argument form, network output with separate control and
2056 ret
= lttng_snapshot_output_set_network_urls(
2057 ctrl_url_arg
, data_url_arg
, snapshot_output
);
2059 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2060 ctrl_url_arg
, data_url_arg
);
2065 if (snapshot_output
) {
2066 action_status
= lttng_action_snapshot_session_set_output(
2067 action
, snapshot_output
);
2068 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2069 ERR("Failed to set snapshot session action's output.");
2073 /* Ownership of `snapshot_output` has been transferred to the action. */
2074 snapshot_output
= NULL
;
2078 enum lttng_action_status status
;
2079 status
= lttng_action_snapshot_session_set_rate_policy(
2081 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2082 ERR("Failed to set rate policy");
2090 lttng_action_destroy(action
);
2094 free(snapshot_name_arg
);
2099 free(snapshot_output
);
2101 lttng_rate_policy_destroy(policy
);
2102 argpar_state_destroy(state
);
2103 argpar_item_destroy(item
);
2107 struct action_descr
{
2109 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2113 struct action_descr action_descrs
[] = {
2114 { "notify", handle_action_notify
},
2115 { "start-session", handle_action_start_session
},
2116 { "stop-session", handle_action_stop_session
},
2117 { "rotate-session", handle_action_rotate_session
},
2118 { "snapshot-session", handle_action_snapshot_session
},
2122 struct lttng_action
*parse_action(const char *action_name
, int *argc
, const char ***argv
)
2125 struct lttng_action
*action
;
2126 const struct action_descr
*descr
= NULL
;
2128 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2129 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2130 descr
= &action_descrs
[i
];
2136 ERR("Unknown action name: %s", action_name
);
2140 action
= descr
->handler(argc
, argv
);
2142 /* The handler has already printed an error message. */
2154 struct argpar_opt_descr add_trigger_options
[] = {
2155 { OPT_HELP
, 'h', "help", false },
2156 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2157 { OPT_CONDITION
, '\0', "condition", true },
2158 { OPT_ACTION
, '\0', "action", true },
2159 { OPT_NAME
, '\0', "name", true },
2160 { OPT_OWNER_UID
, '\0', "owner-uid", true },
2161 ARGPAR_OPT_DESCR_SENTINEL
,
2165 void lttng_actions_destructor(void *p
)
2167 struct lttng_action
*action
= p
;
2169 lttng_action_destroy(action
);
2172 int cmd_add_trigger(int argc
, const char **argv
)
2175 int my_argc
= argc
- 1;
2176 const char **my_argv
= argv
+ 1;
2177 struct lttng_condition
*condition
= NULL
;
2178 struct lttng_dynamic_pointer_array actions
;
2179 struct argpar_state
*argpar_state
= NULL
;
2180 struct argpar_item
*argpar_item
= NULL
;
2181 struct lttng_action
*action_list
= NULL
;
2182 struct lttng_action
*action
= NULL
;
2183 struct lttng_trigger
*trigger
= NULL
;
2187 char *owner_uid
= NULL
;
2188 enum lttng_error_code ret_code
;
2190 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2193 enum argpar_state_parse_next_status status
;
2194 const struct argpar_item_opt
*item_opt
;
2197 argpar_state_destroy(argpar_state
);
2198 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2199 add_trigger_options
);
2200 if (!argpar_state
) {
2201 ERR("Failed to create argpar state.");
2205 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2206 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2207 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2210 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2213 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2217 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2219 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2220 const struct argpar_item_non_opt
*item_non_opt
=
2221 (const struct argpar_item_non_opt
*)
2224 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2228 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2230 ingested_args
= argpar_state_get_ingested_orig_args(
2233 my_argc
-= ingested_args
;
2234 my_argv
+= ingested_args
;
2236 switch (item_opt
->descr
->id
) {
2241 case OPT_LIST_OPTIONS
:
2242 list_cmd_options_argpar(stdout
, add_trigger_options
);
2248 ERR("A --condition was already given.");
2252 condition
= parse_condition(item_opt
->arg
, &my_argc
, &my_argv
);
2255 * An error message was already printed by
2265 action
= parse_action(item_opt
->arg
, &my_argc
, &my_argv
);
2268 * An error message was already printed by
2274 ret
= lttng_dynamic_pointer_array_add_pointer(
2277 ERR("Failed to add pointer to pointer array.");
2281 /* Ownership of the action was transferred to the list. */
2288 if (!assign_string(&name
, item_opt
->arg
, "--name")) {
2296 if (!assign_string(&owner_uid
, item_opt
->arg
,
2309 ERR("Missing --condition.");
2313 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2314 ERR("Need at least one --action.");
2318 action_list
= lttng_action_list_create();
2323 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2324 enum lttng_action_status status
;
2326 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2328 status
= lttng_action_list_add_action(action_list
, action
);
2329 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2334 * The `lttng_action_list_add_action()` takes a reference to
2335 * the action. We can destroy ours.
2337 lttng_action_destroy(action
);
2341 trigger
= lttng_trigger_create(condition
, action_list
);
2347 enum lttng_trigger_status trigger_status
;
2352 uid
= strtol(owner_uid
, &end
, 10);
2353 if (end
== owner_uid
|| *end
!= '\0' || errno
!= 0) {
2354 ERR("Failed to parse `%s` as a user id.", owner_uid
);
2358 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2359 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2360 ERR("Failed to set trigger's user identity.");
2366 ret_code
= lttng_register_trigger_with_name(trigger
, name
);
2368 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
2371 if (ret_code
!= LTTNG_OK
) {
2372 ERR("Failed to register trigger: %s.",
2373 lttng_strerror(-ret_code
));
2377 MSG("Trigger registered successfully.");
2386 argpar_state_destroy(argpar_state
);
2387 argpar_item_destroy(argpar_item
);
2388 lttng_dynamic_pointer_array_reset(&actions
);
2389 lttng_condition_destroy(condition
);
2390 lttng_action_destroy(action_list
);
2391 lttng_action_destroy(action
);
2392 lttng_trigger_destroy(trigger
);