2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "../command.h"
12 #include "../loglevel.h"
13 #include "../uprobe.h"
15 #include "common/argpar/argpar.h"
16 #include "common/dynamic-array.h"
17 #include "common/string-utils/string-utils.h"
18 #include "common/utils.h"
19 /* For lttng_event_rule_type_str(). */
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/lttng.h>
22 #include "lttng/event-rule/kernel-function.h"
23 #include "lttng/event-rule/kernel-probe.h"
24 #include "lttng/event-rule/syscall.h"
25 #include <lttng/event-rule/tracepoint.h>
26 #include "lttng/event-rule/userspace-probe.h"
27 #include "lttng/kernel-function.h"
28 #include "lttng/kernel-probe.h"
29 #include "lttng/log-level-rule.h"
30 #include "lttng/map-key-internal.h"
31 #include "common/filter/filter-ast.h"
32 #include "common/filter/filter-ir.h"
33 #include "common/dynamic-array.h"
35 #if (LTTNG_SYMBOL_NAME_LEN == 256)
36 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
39 #ifdef LTTNG_EMBED_HELP
40 static const char help_msg
[] =
41 #include <lttng-add-trigger.1.h>
88 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
89 { OPT_ALL
, 'a', "all", false },
90 { OPT_FILTER
, 'f', "filter", true },
91 { OPT_EXCLUDE
, 'x', "exclude", true },
92 { OPT_LOGLEVEL
, '\0', "loglevel", true },
93 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
96 { OPT_USERSPACE
, 'u', "userspace", false },
97 { OPT_KERNEL
, 'k', "kernel", false },
98 { OPT_LOG4J
, 'l', "log4j", false },
99 { OPT_JUL
, 'j', "jul", false },
100 { OPT_PYTHON
, 'p', "python", false },
102 /* Event rule types */
103 { OPT_FUNCTION
, '\0', "function", true },
104 { OPT_PROBE
, '\0', "probe", true },
105 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
106 { OPT_SYSCALL
, '\0', "syscall" },
107 { OPT_TRACEPOINT
, '\0', "tracepoint" },
109 /* Capture descriptor */
110 { OPT_CAPTURE
, '\0', "capture", true },
112 ARGPAR_OPT_DESCR_SENTINEL
116 bool assign_domain_type(enum lttng_domain_type
*dest
,
117 enum lttng_domain_type src
)
121 if (*dest
== LTTNG_DOMAIN_NONE
|| *dest
== src
) {
125 ERR("Multiple domains specified.");
133 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
134 enum lttng_event_rule_type src
)
138 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
142 ERR("Multiple event types specified.");
150 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
155 ERR("Duplicate '%s' given.", opt_name
);
161 PERROR("Failed to allocate string '%s'.", opt_name
);
175 /* This is defined in enable_events.c. */
177 int create_exclusion_list_and_validate(const char *event_name
,
178 const char *exclusions_arg
,
179 char ***exclusion_list
);
182 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
183 * is not recognized as a valid log level.
186 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
188 switch (domain_type
) {
189 case LTTNG_DOMAIN_UST
:
191 enum lttng_loglevel loglevel
;
192 const int ret
= loglevel_name_to_value(str
, &loglevel
);
194 return ret
== -1 ? ret
: (int) loglevel
;
196 case LTTNG_DOMAIN_LOG4J
:
198 enum lttng_loglevel_log4j loglevel
;
199 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
201 return ret
== -1 ? ret
: (int) loglevel
;
203 case LTTNG_DOMAIN_JUL
:
205 enum lttng_loglevel_jul loglevel
;
206 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
208 return ret
== -1 ? ret
: (int) loglevel
;
210 case LTTNG_DOMAIN_PYTHON
:
212 enum lttng_loglevel_python loglevel
;
213 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
215 return ret
== -1 ? ret
: (int) loglevel
;
218 /* Invalid domain type. */
223 static int parse_kernel_probe_opts(const char *source
,
224 struct lttng_kernel_probe_location
**location
)
229 char name
[LTTNG_SYMBOL_NAME_LEN
];
230 char *symbol_name
= NULL
;
233 /* Check for symbol+offset. */
234 match
= sscanf(source
,
235 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
239 if (*s_hex
== '\0') {
240 ERR("Kernel probe symbol offset is missing.");
244 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
246 PERROR("Failed to copy kernel probe location symbol name.");
249 offset
= strtoul(s_hex
, NULL
, 0);
251 *location
= lttng_kernel_probe_location_symbol_create(
252 symbol_name
, offset
);
254 ERR("Failed to create symbol kernel probe location.");
261 /* Check for symbol. */
262 if (isalpha(name
[0]) || name
[0] == '_') {
263 match
= sscanf(source
,
264 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
268 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
270 ERR("Failed to copy kernel probe location symbol name.");
274 *location
= lttng_kernel_probe_location_symbol_create(
277 ERR("Failed to create symbol kernel probe location.");
285 /* Check for address. */
286 match
= sscanf(source
, "%18s", s_hex
);
290 if (*s_hex
== '\0') {
291 ERR("Invalid kernel probe location address.");
295 address
= strtoul(s_hex
, NULL
, 0);
296 *location
= lttng_kernel_probe_location_address_create(address
);
298 ERR("Failed to create symbol kernel probe location.");
315 static int parse_kernel_function_opts(const char *source
,
316 struct lttng_kernel_function_location
**location
)
321 char name
[LTTNG_SYMBOL_NAME_LEN
];
322 char *symbol_name
= NULL
;
325 /* Check for symbol+offset. */
326 match
= sscanf(source
,
327 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
331 if (*s_hex
== '\0') {
332 ERR("Kernel function symbol offset is missing.");
336 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
338 PERROR("Failed to copy kernel function location symbol name.");
341 offset
= strtoul(s_hex
, NULL
, 0);
343 *location
= lttng_kernel_function_location_symbol_create(
344 symbol_name
, offset
);
346 ERR("Failed to create symbol kernel function location.");
353 /* Check for symbol. */
354 if (isalpha(name
[0]) || name
[0] == '_') {
355 match
= sscanf(source
,
356 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
360 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
362 ERR("Failed to copy kernel function location symbol name.");
366 *location
= lttng_kernel_function_location_symbol_create(
369 ERR("Failed to create symbol kernel function location.");
377 /* Check for address. */
378 match
= sscanf(source
, "%18s", s_hex
);
382 if (*s_hex
== '\0') {
383 ERR("Invalid kernel function location address.");
387 address
= strtoul(s_hex
, NULL
, 0);
388 *location
= lttng_kernel_function_location_address_create(address
);
390 ERR("Failed to create symbol kernel function location.");
408 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
409 const struct ir_load_expression
*load_expr
,
410 const char *capture_str
)
412 char *provider_name
= NULL
;
413 struct lttng_event_expr
*event_expr
= NULL
;
414 const struct ir_load_expression_op
*load_expr_op
= load_expr
->child
;
415 const enum ir_load_expression_type load_expr_child_type
=
418 switch (load_expr_child_type
) {
419 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
420 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
422 const char *field_name
;
424 load_expr_op
= load_expr_op
->next
;
425 assert(load_expr_op
);
426 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
427 field_name
= load_expr_op
->u
.symbol
;
430 event_expr
= load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
431 lttng_event_expr_event_payload_field_create(field_name
) :
432 lttng_event_expr_channel_context_field_create(field_name
);
434 ERR("Failed to create %s event expression: field name = `%s`.",
435 load_expr_child_type
== IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
?
436 "payload field" : "channel context",
443 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
446 const char *type_name
;
447 const char *field_name
;
449 load_expr_op
= load_expr_op
->next
;
450 assert(load_expr_op
);
451 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
452 field_name
= load_expr_op
->u
.symbol
;
456 * The field name needs to be of the form PROVIDER:TYPE. We
459 colon
= strchr(field_name
, ':');
461 ERR("Invalid app-specific context field name: missing colon in `%s`.",
466 type_name
= colon
+ 1;
467 if (*type_name
== '\0') {
468 ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
473 provider_name
= strndup(field_name
, colon
- field_name
);
474 if (!provider_name
) {
475 PERROR("Failed to allocate field name string");
479 event_expr
= lttng_event_expr_app_specific_context_field_create(
480 provider_name
, type_name
);
482 ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
483 provider_name
, type_name
);
490 ERR("%s: unexpected load expr type %d.", __func__
,
495 load_expr_op
= load_expr_op
->next
;
497 /* There may be a single array index after that. */
498 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
499 struct lttng_event_expr
*index_event_expr
;
500 const uint64_t index
= load_expr_op
->u
.index
;
502 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
503 if (!index_event_expr
) {
504 ERR("Failed to create array field element event expression.");
508 event_expr
= index_event_expr
;
509 load_expr_op
= load_expr_op
->next
;
512 switch (load_expr_op
->type
) {
513 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
515 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
516 * always found at the end of the chain.
519 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
520 ERR("While parsing expression `%s`: Capturing subfields is not supported.",
525 ERR("%s: unexpected load expression operator %s.", __func__
,
526 ir_load_expression_type_str(load_expr_op
->type
));
533 lttng_event_expr_destroy(event_expr
);
543 struct lttng_event_expr
*ir_op_load_to_event_expr(
544 const struct ir_op
*ir
, const char *capture_str
)
546 struct lttng_event_expr
*event_expr
= NULL
;
548 assert(ir
->op
== IR_OP_LOAD
);
550 switch (ir
->data_type
) {
551 case IR_DATA_EXPRESSION
:
553 const struct ir_load_expression
*ir_load_expr
=
554 ir
->u
.load
.u
.expression
;
556 event_expr
= ir_op_load_expr_to_event_expr(
557 ir_load_expr
, capture_str
);
561 ERR("%s: unexpected data type: %s.", __func__
,
562 ir_data_type_str(ir
->data_type
));
570 const char *ir_operator_type_human_str(enum ir_op_type op
)
592 struct lttng_event_expr
*ir_op_root_to_event_expr(const struct ir_op
*ir
,
593 const char *capture_str
)
595 struct lttng_event_expr
*event_expr
= NULL
;
597 assert(ir
->op
== IR_OP_ROOT
);
598 ir
= ir
->u
.root
.child
;
602 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
607 ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
609 ir_operator_type_human_str(ir
->op
));
612 ERR("%s: unexpected IR op type: %s.", __func__
,
613 ir_op_type_str(ir
->op
));
621 void destroy_event_expr(void *ptr
)
623 lttng_event_expr_destroy(ptr
);
626 struct parse_event_rule_res
{
628 struct lttng_event_rule
*er
;
630 /* Array of `struct lttng_event_expr *` */
631 struct lttng_dynamic_pointer_array capture_descriptors
;
635 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
637 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
638 enum lttng_event_rule_type event_rule_type
=
639 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
640 struct argpar_state
*state
;
641 struct argpar_item
*item
= NULL
;
643 int consumed_args
= -1;
644 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
645 struct lttng_kernel_function_location
*kernel_function_location
= NULL
;
646 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
647 struct parse_event_rule_res res
= { 0 };
648 struct lttng_event_expr
*event_expr
= NULL
;
649 struct filter_parser_ctx
*parser_ctx
= NULL
;
650 struct lttng_log_level_rule
*log_level_rule
= NULL
;
652 /* Was the -a/--all flag provided? */
653 bool all_events
= false;
655 /* Tracepoint name (non-option argument). */
656 const char *tracepoint_name
= NULL
;
658 /* Holds the argument of --probe / --userspace-probe. */
665 char *exclude
= NULL
;
666 char **exclusion_list
= NULL
;
669 char *loglevel_str
= NULL
;
670 bool loglevel_only
= false;
672 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
674 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
676 ERR("Failed to allocate an argpar state.");
681 enum argpar_state_parse_next_status status
;
683 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
684 status
= argpar_state_parse_next(state
, &item
, &error
);
685 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
688 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
689 /* Just stop parsing here. */
691 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
695 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
697 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
698 const struct argpar_item_opt
*item_opt
=
699 (const struct argpar_item_opt
*) item
;
701 switch (item_opt
->descr
->id
) {
704 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_UST
)) {
710 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_KERNEL
)) {
716 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_LOG4J
)) {
722 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_JUL
)) {
728 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_PYTHON
)) {
734 /* Event rule types */
736 if (!assign_event_rule_type(&event_rule_type
,
737 LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
)) {
741 if (!assign_string(&source
, item_opt
->arg
, "source")) {
747 if (!assign_event_rule_type(&event_rule_type
,
748 LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
)) {
752 if (!assign_string(&source
, item_opt
->arg
, "source")) {
757 case OPT_USERSPACE_PROBE
:
758 if (!assign_event_rule_type(&event_rule_type
,
759 LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
)) {
763 if (!assign_string(&source
, item_opt
->arg
, "source")) {
769 if (!assign_event_rule_type(&event_rule_type
,
770 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
776 if (!assign_event_rule_type(&event_rule_type
,
777 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
786 if (!assign_string(&filter
, item_opt
->arg
,
793 if (!assign_string(&exclude
, item_opt
->arg
,
800 case OPT_LOGLEVEL_ONLY
:
801 if (!assign_string(&loglevel_str
, item_opt
->arg
,
802 "--loglevel/--loglevel-only")) {
806 loglevel_only
= item_opt
->descr
->id
==
812 const char *capture_str
= item_opt
->arg
;
814 ret
= filter_parser_ctx_create_from_filter_expression(
815 capture_str
, &parser_ctx
);
817 ERR("Failed to parse capture expression `%s`.",
822 event_expr
= ir_op_root_to_event_expr(
827 * ir_op_root_to_event_expr has printed
833 ret
= lttng_dynamic_pointer_array_add_pointer(
834 &res
.capture_descriptors
,
841 * The ownership of event expression was
842 * transferred to the dynamic array.
852 const struct argpar_item_non_opt
*item_non_opt
=
853 (const struct argpar_item_non_opt
*)
857 * Don't accept two non-option arguments/tracepoint
860 if (tracepoint_name
) {
861 ERR("Unexpected argument '%s'",
866 tracepoint_name
= item_non_opt
->arg
;
870 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
871 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
875 * Option -a is applicable to event rules of type tracepoint and
876 * syscall, and it is equivalent to using "*" as the tracepoint name.
879 switch (event_rule_type
) {
880 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
881 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
884 ERR("Can't use -a/--all with %s event rules.",
885 lttng_event_rule_type_str(event_rule_type
));
889 if (tracepoint_name
) {
890 ERR("Can't provide a tracepoint name with -a/--all.");
894 /* In which case, it's equivalent to tracepoint name "*". */
895 tracepoint_name
= "*";
899 * A tracepoint name (or -a, for the event rule types that accept it)
902 if (!tracepoint_name
) {
903 ERR("Need to provide either a tracepoint name or -a/--all.");
908 * We don't support multiple tracepoint names for now.
910 if (strchr(tracepoint_name
, ',')) {
911 ERR("Comma separated tracepoint names are not supported.");
916 * Update *argc and *argv so our caller can keep parsing what follows.
918 consumed_args
= argpar_state_get_ingested_orig_args(state
);
919 assert(consumed_args
>= 0);
920 *argc
-= consumed_args
;
921 *argv
+= consumed_args
;
923 /* Need to specify a domain. */
924 if (domain_type
== LTTNG_DOMAIN_NONE
) {
925 ERR("Please specify a domain (--kernel/--userspace/--jul/--log4j/--python).");
929 /* Validate event rule type against domain. */
930 switch (event_rule_type
) {
931 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
932 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
933 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
934 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
935 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
936 ERR("Event type not available for user-space tracing.");
941 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
949 * Adding a filter to a probe, function or userspace-probe would be
950 * denied by the kernel tracer as it's not supported at the moment. We
951 * do an early check here to warn the user.
953 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
954 switch (event_rule_type
) {
955 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
956 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
959 ERR("Filter expressions are not supported for %s event rules.",
960 lttng_event_rule_type_str(event_rule_type
));
965 /* If --exclude/-x was passed, split it into an exclusion list. */
967 if (domain_type
!= LTTNG_DOMAIN_UST
) {
968 ERR("Event name exclusions are not yet implemented for %s event rules.",
969 get_domain_str(domain_type
));
974 if (create_exclusion_list_and_validate(tracepoint_name
, exclude
,
975 &exclusion_list
) != 0) {
976 ERR("Failed to create exclusion list.");
981 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
982 ERR("Log levels are only applicable to tracepoint event rules.");
986 /* Finally, create the event rule object. */
987 switch (event_rule_type
) {
988 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
990 enum lttng_event_rule_status event_rule_status
;
992 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
994 ERR("Failed to create tracepoint event rule.");
999 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
1000 res
.er
, tracepoint_name
);
1001 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1002 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
1009 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
1011 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1012 ERR("Failed to set tracepoint event rule's filter to '%s'.",
1018 /* Set exclusion list. */
1019 if (exclusion_list
) {
1022 for (n
= 0; exclusion_list
[n
]; n
++) {
1023 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
1026 if (event_rule_status
!=
1027 LTTNG_EVENT_RULE_STATUS_OK
) {
1028 ERR("Failed to set tracepoint exclusion list element '%s'",
1038 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
1039 ERR("Log levels are not supported by the kernel tracer.");
1043 loglevel
= parse_loglevel_string(
1044 loglevel_str
, domain_type
);
1046 ERR("Failed to parse `%s` as a log level.",
1051 if (loglevel_only
) {
1052 log_level_rule
= lttng_log_level_rule_exactly_create(loglevel
);
1054 log_level_rule
= lttng_log_level_rule_at_least_as_severe_as_create(loglevel
);
1057 if (log_level_rule
== NULL
) {
1058 ERR("Failed to create log level rule object.");
1063 lttng_event_rule_tracepoint_set_log_level_rule(
1064 res
.er
, log_level_rule
);
1066 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1067 ERR("Failed to set log level on event fule.");
1074 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
1077 enum lttng_event_rule_status event_rule_status
;
1080 ret
= parse_kernel_probe_opts(source
, &kernel_probe_location
);
1082 ERR("Failed to parse kernel probe location.");
1086 assert(kernel_probe_location
);
1087 res
.er
= lttng_event_rule_kernel_probe_create(kernel_probe_location
);
1089 ERR("Failed to create kprobe event rule.");
1093 event_rule_status
= lttng_event_rule_kernel_probe_set_event_name(res
.er
, tracepoint_name
);
1094 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1095 ERR("Failed to set kprobe event rule's name to '%s'.", tracepoint_name
);
1101 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
1104 enum lttng_event_rule_status event_rule_status
;
1107 ret
= parse_kernel_function_opts(source
, &kernel_function_location
);
1109 ERR("Failed to parse kernel function location.");
1113 assert(kernel_function_location
);
1114 res
.er
= lttng_event_rule_kernel_function_create(kernel_function_location
);
1116 ERR("Failed to create kfunction event rule.");
1120 event_rule_status
= lttng_event_rule_kernel_function_set_event_name(res
.er
, tracepoint_name
);
1121 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1122 ERR("Failed to set kfunction event rule's name to '%s'.", tracepoint_name
);
1128 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
1131 enum lttng_event_rule_status event_rule_status
;
1133 ret
= parse_userspace_probe_opts(
1134 source
, &userspace_probe_location
);
1136 ERR("Failed to parse user space probe location.");
1140 res
.er
= lttng_event_rule_userspace_probe_create(userspace_probe_location
);
1142 ERR("Failed to create userspace probe event rule.");
1146 event_rule_status
= lttng_event_rule_userspace_probe_set_event_name(
1147 res
.er
, tracepoint_name
);
1148 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1149 ERR("Failed to set user space probe event rule's name to '%s'.",
1156 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
1158 enum lttng_event_rule_status event_rule_status
;
1160 res
.er
= lttng_event_rule_syscall_create();
1162 ERR("Failed to create syscall event rule.");
1166 event_rule_status
= lttng_event_rule_syscall_set_pattern(
1167 res
.er
, tracepoint_name
);
1168 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1169 ERR("Failed to set syscall event rule's pattern to '%s'.",
1175 event_rule_status
= lttng_event_rule_syscall_set_filter(
1177 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1178 ERR("Failed to set syscall event rule's filter to '%s'.",
1194 lttng_event_rule_destroy(res
.er
);
1196 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1200 filter_parser_ctx_free(parser_ctx
);
1203 lttng_event_expr_destroy(event_expr
);
1204 argpar_item_destroy(item
);
1206 argpar_state_destroy(state
);
1211 strutils_free_null_terminated_array_of_strings(exclusion_list
);
1212 lttng_kernel_probe_location_destroy(kernel_probe_location
);
1213 lttng_userspace_probe_location_destroy(userspace_probe_location
);
1214 lttng_log_level_rule_destroy(log_level_rule
);
1219 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
1221 struct parse_event_rule_res res
;
1222 struct lttng_condition
*c
;
1225 res
= parse_event_rule(argc
, argv
);
1231 c
= lttng_condition_on_event_create(res
.er
);
1232 lttng_event_rule_destroy(res
.er
);
1238 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
1240 enum lttng_condition_status status
;
1241 struct lttng_event_expr
**expr
=
1242 lttng_dynamic_array_get_element(
1243 &res
.capture_descriptors
.array
, i
);
1247 status
= lttng_condition_on_event_append_capture_descriptor(
1249 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1250 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
1251 ERR("The capture feature is unsupported by the event-rule type");
1256 /* Ownership of event expression moved to `c` */
1263 lttng_condition_destroy(c
);
1267 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
1268 lttng_event_rule_destroy(res
.er
);
1273 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1275 struct lttng_condition
*cond
= NULL
;
1276 struct argpar_state
*state
= NULL
;
1277 struct argpar_item
*item
= NULL
;
1278 const char *threshold_arg
= NULL
;
1279 const char *session_name_arg
= NULL
;
1282 enum lttng_condition_status condition_status
;
1284 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1286 ERR("Failed to allocate an argpar state.");
1291 enum argpar_state_parse_next_status status
;
1293 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1294 status
= argpar_state_parse_next(state
, &item
, &error
);
1295 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1298 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1299 /* Just stop parsing here. */
1301 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1305 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1307 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1308 const struct argpar_item_opt
*item_opt
=
1309 (const struct argpar_item_opt
*) item
;
1311 switch (item_opt
->descr
->id
) {
1316 const struct argpar_item_non_opt
*item_non_opt
;
1318 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1320 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1322 switch (item_non_opt
->non_opt_index
) {
1324 session_name_arg
= item_non_opt
->arg
;
1327 threshold_arg
= item_non_opt
->arg
;
1330 ERR("Unexpected argument `%s`.",
1337 *argc
-= argpar_state_get_ingested_orig_args(state
);
1338 *argv
+= argpar_state_get_ingested_orig_args(state
);
1340 if (!session_name_arg
) {
1341 ERR("Missing session name argument.");
1345 if (!threshold_arg
) {
1346 ERR("Missing threshold argument.");
1350 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1351 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1355 cond
= lttng_condition_session_consumed_size_create();
1357 ERR("Failed to allocate a session consumed size condition.");
1361 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1362 cond
, session_name_arg
);
1363 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1364 ERR("Failed to set session consumed size condition's session name to '%s'.",
1369 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1371 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1372 ERR("Failed to set session consumed size condition threshold.");
1379 lttng_condition_destroy(cond
);
1383 argpar_state_destroy(state
);
1384 argpar_item_destroy(item
);
1390 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1392 ERR("High buffer usage threshold conditions are unsupported for the moment.");
1397 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1399 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
1404 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1406 ERR("Session rotation ongoing conditions are unsupported for the moment.");
1411 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1413 ERR("Session rotation completed conditions are unsupported for the moment.");
1417 struct condition_descr
{
1419 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1423 struct condition_descr condition_descrs
[] = {
1424 { "on-event", handle_condition_event
},
1425 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1426 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1427 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1428 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1429 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1433 struct lttng_condition
*parse_condition(int *argc
, const char ***argv
)
1436 struct lttng_condition
*cond
;
1437 const char *condition_name
;
1438 const struct condition_descr
*descr
= NULL
;
1441 ERR("Missing condition name.");
1445 condition_name
= (*argv
)[0];
1450 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1451 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1452 descr
= &condition_descrs
[i
];
1458 ERR("Unknown condition name '%s'", condition_name
);
1462 cond
= descr
->handler(argc
, argv
);
1464 /* The handler has already printed an error message. */
1477 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1479 return lttng_action_notify_create();
1482 static const struct argpar_opt_descr no_opt_descrs
[] = {
1483 ARGPAR_OPT_DESCR_SENTINEL
1487 * Generic handler for a kind of action that takes a session name as its sole
1492 struct lttng_action
*handle_action_simple_session(
1493 int *argc
, const char ***argv
,
1494 struct lttng_action
*(*create_action_cb
)(void),
1495 enum lttng_action_status (*set_session_name_cb
)(struct lttng_action
*, const char *),
1496 const char *action_name
)
1498 struct lttng_action
*action
= NULL
;
1499 struct argpar_state
*state
= NULL
;
1500 struct argpar_item
*item
= NULL
;
1501 const char *session_name_arg
= NULL
;
1503 enum lttng_action_status action_status
;
1505 state
= argpar_state_create(*argc
, *argv
, no_opt_descrs
);
1507 ERR("Failed to allocate an argpar state.");
1512 enum argpar_state_parse_next_status status
;
1513 const struct argpar_item_non_opt
*item_non_opt
;
1515 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1516 status
= argpar_state_parse_next(state
, &item
, &error
);
1517 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1520 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1521 /* Just stop parsing here. */
1523 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1527 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1528 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1530 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1532 switch (item_non_opt
->non_opt_index
) {
1534 session_name_arg
= item_non_opt
->arg
;
1537 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
1542 *argc
-= argpar_state_get_ingested_orig_args(state
);
1543 *argv
+= argpar_state_get_ingested_orig_args(state
);
1545 if (!session_name_arg
) {
1546 ERR("Missing session name.");
1550 action
= create_action_cb();
1552 ERR("Failed to allocate %s session action.", action_name
);
1556 action_status
= set_session_name_cb(action
, session_name_arg
);
1557 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1558 ERR("Failed to set action %s session's session name to '%s'.",
1559 action_name
, session_name_arg
);
1566 lttng_action_destroy(action
);
1568 argpar_item_destroy(item
);
1571 argpar_state_destroy(state
);
1576 struct lttng_action
*handle_action_start_session(int *argc
,
1579 return handle_action_simple_session(argc
, argv
,
1580 lttng_action_start_session_create
,
1581 lttng_action_start_session_set_session_name
,
1586 struct lttng_action
*handle_action_stop_session(int *argc
,
1589 return handle_action_simple_session(argc
, argv
,
1590 lttng_action_stop_session_create
,
1591 lttng_action_stop_session_set_session_name
,
1596 struct lttng_action
*handle_action_rotate_session(int *argc
,
1599 return handle_action_simple_session(argc
, argv
,
1600 lttng_action_rotate_session_create
,
1601 lttng_action_rotate_session_set_session_name
,
1605 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1606 { OPT_NAME
, 'n', "name", true },
1607 { OPT_MAX_SIZE
, 'm', "max-size", true },
1608 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1609 { OPT_DATA_URL
, '\0', "data-url", true },
1610 { OPT_URL
, '\0', "url", true },
1611 { OPT_PATH
, '\0', "path", true },
1612 ARGPAR_OPT_DESCR_SENTINEL
1616 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1619 struct lttng_action
*action
= NULL
;
1620 struct argpar_state
*state
= NULL
;
1621 struct argpar_item
*item
= NULL
;
1622 const char *session_name_arg
= NULL
;
1623 char *snapshot_name_arg
= NULL
;
1624 char *ctrl_url_arg
= NULL
;
1625 char *data_url_arg
= NULL
;
1626 char *max_size_arg
= NULL
;
1627 char *url_arg
= NULL
;
1628 char *path_arg
= NULL
;
1630 enum lttng_action_status action_status
;
1631 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1633 unsigned int locations_specified
= 0;
1635 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1637 ERR("Failed to allocate an argpar state.");
1642 enum argpar_state_parse_next_status status
;
1644 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1645 status
= argpar_state_parse_next(state
, &item
, &error
);
1646 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1649 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1650 /* Just stop parsing here. */
1652 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1656 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1658 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1659 const struct argpar_item_opt
*item_opt
=
1660 (const struct argpar_item_opt
*) item
;
1662 switch (item_opt
->descr
->id
) {
1664 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1670 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1676 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1682 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1688 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1694 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1703 const struct argpar_item_non_opt
*item_non_opt
;
1705 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1707 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1709 switch (item_non_opt
->non_opt_index
) {
1711 session_name_arg
= item_non_opt
->arg
;
1714 ERR("Unexpected argument `%s`.",
1721 *argc
-= argpar_state_get_ingested_orig_args(state
);
1722 *argv
+= argpar_state_get_ingested_orig_args(state
);
1724 if (!session_name_arg
) {
1725 ERR("Missing session name.");
1729 /* --ctrl-url and --data-url must come in pair. */
1730 if (ctrl_url_arg
&& !data_url_arg
) {
1731 ERR("--ctrl-url is specified, but --data-url is missing.");
1735 if (!ctrl_url_arg
&& data_url_arg
) {
1736 ERR("--data-url is specified, but --ctrl-url is missing.");
1740 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1741 locations_specified
+= !!url_arg
;
1742 locations_specified
+= !!path_arg
;
1744 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1745 if (locations_specified
> 1) {
1746 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1751 * Did the user specify an option that implies using a
1752 * custom/unregistered output?
1754 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1755 snapshot_output
= lttng_snapshot_output_create();
1756 if (!snapshot_output
) {
1757 ERR("Failed to allocate a snapshot output.");
1762 action
= lttng_action_snapshot_session_create();
1764 ERR("Failed to allocate snapshot session action.");
1768 action_status
= lttng_action_snapshot_session_set_session_name(
1769 action
, session_name_arg
);
1770 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1771 ERR("Failed to set action snapshot session's session name to '%s'.",
1776 if (snapshot_name_arg
) {
1777 if (!snapshot_output
) {
1778 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1782 ret
= lttng_snapshot_output_set_name(
1783 snapshot_name_arg
, snapshot_output
);
1785 ERR("Failed to set name of snapshot output.");
1793 if (!snapshot_output
) {
1794 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1798 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1800 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1804 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1806 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1814 struct lttng_uri
*uris
;
1816 if (!strstr(url_arg
, "://")) {
1817 ERR("Failed to parse '%s' as an URL.", url_arg
);
1821 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1823 ERR("Failed to parse '%s' as an URL.", url_arg
);
1827 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1828 ret
= lttng_snapshot_output_set_local_path(
1829 uris
[0].dst
.path
, snapshot_output
);
1832 ERR("Failed to assign '%s' as a local destination.",
1837 ret
= lttng_snapshot_output_set_network_url(
1838 url_arg
, snapshot_output
);
1841 ERR("Failed to assign '%s' as a network URL.",
1849 ret
= lttng_snapshot_output_set_local_path(
1850 path_arg
, snapshot_output
);
1852 ERR("Failed to parse '%s' as a local path.", path_arg
);
1859 * Two argument form, network output with separate control and
1862 ret
= lttng_snapshot_output_set_network_urls(
1863 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1865 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1866 ctrl_url_arg
, data_url_arg
);
1871 if (snapshot_output
) {
1872 action_status
= lttng_action_snapshot_session_set_output(
1873 action
, snapshot_output
);
1874 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1875 ERR("Failed to set snapshot session action's output.");
1879 /* Ownership of `snapshot_output` has been transferred to the action. */
1880 snapshot_output
= NULL
;
1886 lttng_action_destroy(action
);
1890 free(snapshot_name_arg
);
1895 free(snapshot_output
);
1897 argpar_state_destroy(state
);
1898 argpar_item_destroy(item
);
1902 static const struct argpar_opt_descr incr_value_action_opt_descrs
[] = {
1903 { OPT_SESSION_NAME
, 's', "session", true },
1904 { OPT_MAP_NAME
, 'm', "map", true },
1905 { OPT_KEY
, '\0', "key", true },
1906 ARGPAR_OPT_DESCR_SENTINEL
1910 struct lttng_action
*handle_action_incr_value(int *argc
,
1913 struct lttng_action
*action
= NULL
;
1914 struct argpar_state
*state
= NULL
;
1915 struct argpar_item
*item
= NULL
;
1916 struct lttng_map_key
*key
= NULL
;
1917 char *session_name_arg
= NULL
, *map_name_arg
= NULL
;
1918 char *key_arg
= NULL
;
1920 enum lttng_action_status action_status
;
1922 state
= argpar_state_create(*argc
, *argv
, incr_value_action_opt_descrs
);
1924 ERR("Failed to allocate an argpar state.");
1929 enum argpar_state_parse_next_status status
;
1931 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1932 status
= argpar_state_parse_next(state
, &item
, &error
);
1933 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1936 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1937 /* Just stop parsing here. */
1939 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1943 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1945 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1946 struct argpar_item_opt
*item_opt
=
1947 (struct argpar_item_opt
*) item
;
1949 switch (item_opt
->descr
->id
) {
1950 case OPT_SESSION_NAME
:
1951 if (!assign_string(&session_name_arg
, item_opt
->arg
, "--session/-s")) {
1956 if (!assign_string(&map_name_arg
, item_opt
->arg
, "--map/-m")) {
1961 if (!assign_string(&key_arg
, item_opt
->arg
, "--key")) {
1971 *argc
-= argpar_state_get_ingested_orig_args(state
);
1972 *argv
+= argpar_state_get_ingested_orig_args(state
);
1974 if (!session_name_arg
) {
1975 ERR("Missing session name.");
1979 if (!map_name_arg
) {
1980 ERR("Missing map name.");
1989 key
= lttng_map_key_parse_from_string(key_arg
);
1991 ERR("Error parsing key argument");
1995 action
= lttng_action_incr_value_create();
1997 ERR("Failed to allocate incr-value action.");
2001 action_status
= lttng_action_incr_value_set_session_name(action
,
2003 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2004 ERR("Failed to set action incr-value's session name.");
2008 action_status
= lttng_action_incr_value_set_map_name(action
,
2010 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2011 ERR("Failed to set action incr-value's map name.");
2015 action_status
= lttng_action_incr_value_set_key(action
, key
);
2016 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
2017 ERR("Failed to set action incr-value's key");
2024 lttng_action_destroy(action
);
2028 lttng_map_key_destroy(key
);
2029 free(session_name_arg
);
2035 struct action_descr
{
2037 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
2041 struct action_descr action_descrs
[] = {
2042 { "notify", handle_action_notify
},
2043 { "start-session", handle_action_start_session
},
2044 { "stop-session", handle_action_stop_session
},
2045 { "rotate-session", handle_action_rotate_session
},
2046 { "snapshot-session", handle_action_snapshot_session
},
2047 { "incr-value", handle_action_incr_value
},
2051 struct lttng_action
*parse_action(int *argc
, const char ***argv
)
2054 struct lttng_action
*action
;
2055 const char *action_name
;
2056 const struct action_descr
*descr
= NULL
;
2059 ERR("Missing action name.");
2063 action_name
= (*argv
)[0];
2068 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
2069 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
2070 descr
= &action_descrs
[i
];
2076 ERR("Unknown action name: %s", action_name
);
2080 action
= descr
->handler(argc
, argv
);
2082 /* The handler has already printed an error message. */
2094 struct argpar_opt_descr add_trigger_options
[] = {
2095 { OPT_HELP
, 'h', "help", false },
2096 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
2097 { OPT_CONDITION
, '\0', "condition", false },
2098 { OPT_ACTION
, '\0', "action", false },
2099 { OPT_ID
, '\0', "id", true },
2100 { OPT_FIRE_ONCE_AFTER
, '\0', "fire-once-after", true },
2101 { OPT_FIRE_EVERY
, '\0', "fire-every", true },
2102 { OPT_USER_ID
, '\0', "user-id", true },
2103 ARGPAR_OPT_DESCR_SENTINEL
,
2107 bool action_is_tracer_executed(const struct lttng_action
*action
)
2109 bool is_tracer_executed
;
2110 switch (lttng_action_get_type(action
)) {
2111 case LTTNG_ACTION_TYPE_NOTIFY
:
2112 case LTTNG_ACTION_TYPE_START_SESSION
:
2113 case LTTNG_ACTION_TYPE_STOP_SESSION
:
2114 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
2115 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
2116 is_tracer_executed
= false;
2118 case LTTNG_ACTION_TYPE_INCREMENT_VALUE
:
2119 is_tracer_executed
= true;
2121 case LTTNG_ACTION_TYPE_GROUP
:
2127 return is_tracer_executed
;
2131 void lttng_actions_destructor(void *p
)
2133 struct lttng_action
*action
= p
;
2135 lttng_action_destroy(action
);
2138 int cmd_add_trigger(int argc
, const char **argv
)
2141 int my_argc
= argc
- 1;
2142 const char **my_argv
= argv
+ 1;
2143 struct lttng_condition
*condition
= NULL
;
2144 struct lttng_dynamic_pointer_array actions
;
2145 struct argpar_state
*argpar_state
= NULL
;
2146 struct argpar_item
*argpar_item
= NULL
;
2147 struct lttng_action
*action_group
= NULL
;
2148 struct lttng_action
*action
= NULL
;
2149 struct lttng_trigger
*trigger
= NULL
;
2153 char *fire_once_after_str
= NULL
;
2154 char *fire_every_str
= NULL
;
2155 char *user_id
= NULL
;
2157 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
2160 enum argpar_state_parse_next_status status
;
2161 const struct argpar_item_opt
*item_opt
;
2164 argpar_state_destroy(argpar_state
);
2165 argpar_state
= argpar_state_create(my_argc
, my_argv
,
2166 add_trigger_options
);
2167 if (!argpar_state
) {
2168 ERR("Failed to create argpar state.");
2172 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
2173 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
2174 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
2177 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
2180 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
2184 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
2186 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
2187 const struct argpar_item_non_opt
*item_non_opt
=
2188 (const struct argpar_item_non_opt
*)
2191 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
2195 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
2197 ingested_args
= argpar_state_get_ingested_orig_args(
2200 my_argc
-= ingested_args
;
2201 my_argv
+= ingested_args
;
2203 switch (item_opt
->descr
->id
) {
2208 case OPT_LIST_OPTIONS
:
2209 list_cmd_options_argpar(stdout
, add_trigger_options
);
2215 ERR("A --condition was already given.");
2219 condition
= parse_condition(&my_argc
, &my_argv
);
2222 * An error message was already printed by
2232 action
= parse_action(&my_argc
, &my_argv
);
2235 * An error message was already printed by
2241 ret
= lttng_dynamic_pointer_array_add_pointer(
2244 ERR("Failed to add pointer to pointer array.");
2248 /* Ownership of the action was transferred to the group. */
2255 if (!assign_string(&id
, item_opt
->arg
, "--id")) {
2261 case OPT_FIRE_ONCE_AFTER
:
2263 if (!assign_string(&fire_once_after_str
, item_opt
->arg
,
2264 "--fire-once-after")) {
2270 case OPT_FIRE_EVERY
:
2272 if (!assign_string(&fire_every_str
, item_opt
->arg
,
2281 if (!assign_string(&user_id
, item_opt
->arg
,
2294 ERR("Missing --condition.");
2298 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
2299 ERR("Need at least one --action.");
2303 if (fire_every_str
&& fire_once_after_str
) {
2304 ERR("Can't specify both --fire-once-after and --fire-every.");
2308 action_group
= lttng_action_group_create();
2309 if (!action_group
) {
2313 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
2314 enum lttng_action_status status
;
2316 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
2317 if (action_is_tracer_executed(action
)) {
2318 if (fire_every_str
|| fire_once_after_str
) {
2320 * Firing policy with tracer-executed actions
2321 * (`incr-value`) is not supported at the
2322 * moment. It's not clear how the tracers will
2323 * handle the different policies efficiently.
2325 ERR("Can't use --fire-once-after or --fire-every with tracer executed action (incr-value)");
2330 status
= lttng_action_group_add_action(action_group
, action
);
2331 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2336 * The `lttng_action_group_add_action()` takes a reference to
2337 * the action. We can destroy ours.
2339 lttng_action_destroy(action
);
2343 trigger
= lttng_trigger_create(condition
, action_group
);
2349 enum lttng_trigger_status trigger_status
=
2350 lttng_trigger_set_name(trigger
, id
);
2352 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2353 ERR("Failed to set trigger id.");
2358 if (fire_once_after_str
) {
2359 unsigned long long threshold
;
2360 enum lttng_trigger_status trigger_status
;
2362 if (utils_parse_unsigned_long_long(fire_once_after_str
, &threshold
) != 0) {
2363 ERR("Failed to parse `%s` as an integer.", fire_once_after_str
);
2367 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
2368 LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
,
2370 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2371 ERR("Failed to set trigger's policy to `fire once after N`.");
2376 if (fire_every_str
) {
2377 unsigned long long threshold
;
2378 enum lttng_trigger_status trigger_status
;
2380 if (utils_parse_unsigned_long_long(fire_every_str
, &threshold
) != 0) {
2381 ERR("Failed to parse `%s` as an integer.", fire_every_str
);
2385 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
2386 LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
, threshold
);
2387 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2388 ERR("Failed to set trigger's policy to `fire every N`.");
2394 enum lttng_trigger_status trigger_status
;
2399 uid
= strtol(user_id
, &end
, 10);
2400 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
2401 ERR("Failed to parse `%s` as a user id.", user_id
);
2404 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
2405 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2406 ERR("Failed to set trigger's user identity.");
2411 ret
= lttng_register_trigger(trigger
);
2413 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
2417 MSG("Trigger registered successfully.");
2425 argpar_state_destroy(argpar_state
);
2426 argpar_item_destroy(argpar_item
);
2427 lttng_dynamic_pointer_array_reset(&actions
);
2428 lttng_condition_destroy(condition
);
2429 lttng_action_destroy(action_group
);
2430 lttng_action_destroy(action
);
2431 lttng_trigger_destroy(trigger
);
2434 free(fire_once_after_str
);
2435 free(fire_every_str
);