3 #include "../command.h"
6 #include "common/argpar/argpar.h"
7 #include "common/dynamic-array.h"
8 #include "common/string-utils/string-utils.h"
9 #include "common/utils.h"
10 #include "lttng/condition/event-rule.h"
11 #include "lttng/event-internal.h"
12 #include "lttng/event-expr.h"
13 #include <lttng/event-rule/event-rule-internal.h>
14 #include "lttng/event-rule/kprobe.h"
15 #include "lttng/event-rule/syscall.h"
16 #include <lttng/event-rule/tracepoint.h>
17 #include "lttng/event-rule/uprobe.h"
18 #include "common/filter/filter-ast.h"
19 #include "common/filter/filter-ir.h"
20 #include "common/dynamic-array.h"
22 #ifdef LTTNG_EMBED_HELP
23 static const char help_msg
[] =
24 #include <lttng-add-trigger.1.h>
64 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
65 { OPT_ALL
, 'a', "all", false },
66 { OPT_FILTER
, 'f', "filter", true },
67 { OPT_EXCLUDE
, 'x', "exclude", true },
68 { OPT_LOGLEVEL
, '\0', "loglevel", true },
69 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
72 { OPT_USERSPACE
, 'u', "userspace", false },
73 { OPT_KERNEL
, 'k', "kernel", false },
74 { OPT_LOG4J
, 'l', "log4j", false },
75 { OPT_JUL
, 'j', "jul", false },
76 { OPT_PYTHON
, 'p', "python", false },
78 /* Event rule types */
79 { OPT_FUNCTION
, '\0', "function", true },
80 { OPT_PROBE
, '\0', "probe", true },
81 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
82 { OPT_SYSCALL
, '\0', "syscall" },
83 { OPT_TRACEPOINT
, '\0', "tracepoint" },
85 /* Capture descriptor */
86 { OPT_CAPTURE
, '\0', "capture", true },
88 ARGPAR_OPT_DESCR_SENTINEL
92 bool assign_domain_type(enum lttng_domain_type
*dest
,
93 enum lttng_domain_type src
)
97 if (*dest
== LTTNG_DOMAIN_NONE
|| *dest
== src
) {
101 ERR("Multiple domains specified.");
109 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
110 enum lttng_event_rule_type src
)
114 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
118 ERR("Multiple event type not supported.");
126 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
132 "Duplicate %s given.", opt_name
);
138 ERR("Failed to allocate %s string.", opt_name
);
152 /* This is defined in enable_events.c. */
154 int create_exclusion_list_and_validate(const char *event_name
,
155 const char *exclusions_arg
,
156 char ***exclusion_list
);
159 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
160 * is not recognized as a valid log level.
163 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
166 switch (domain_type
) {
167 case LTTNG_DOMAIN_UST
:
168 return loglevel_str_to_value(str
);
170 case LTTNG_DOMAIN_LOG4J
:
171 return loglevel_log4j_str_to_value(str
);
173 case LTTNG_DOMAIN_JUL
:
174 return loglevel_jul_str_to_value(str
);
176 case LTTNG_DOMAIN_PYTHON
:
177 return loglevel_python_str_to_value(str
);
180 /* Invalid domain type. */
186 struct lttng_event_expr
*ir_op_load_expr_to_event_expr(
187 struct ir_load_expression
*load_exp
, const char *capture_str
)
189 struct ir_load_expression_op
*load_expr_op
= load_exp
->child
;
190 struct lttng_event_expr
*event_expr
= NULL
;
191 char *provider_name
= NULL
;
193 switch (load_expr_op
->type
) {
194 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
196 const char *field_name
;
198 load_expr_op
= load_expr_op
->next
;
199 assert(load_expr_op
);
200 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
201 field_name
= load_expr_op
->u
.symbol
;
204 event_expr
= lttng_event_expr_event_payload_field_create(field_name
);
206 fprintf(stderr
, "Failed to create payload field event expression.\n");
213 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
215 const char *field_name
;
217 load_expr_op
= load_expr_op
->next
;
218 assert(load_expr_op
);
219 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
220 field_name
= load_expr_op
->u
.symbol
;
223 event_expr
= lttng_event_expr_channel_context_field_create(field_name
);
225 fprintf(stderr
, "Failed to create channel context field event expression.\n");
232 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
234 const char *field_name
;
236 const char *type_name
;
238 load_expr_op
= load_expr_op
->next
;
239 assert(load_expr_op
);
240 assert(load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_SYMBOL
);
241 field_name
= load_expr_op
->u
.symbol
;
245 * The field name needs to be of the form PROVIDER:TYPE. We
248 colon
= strchr(field_name
, ':');
250 fprintf(stderr
, "Invalid app-specific context field name: missing colon in `%s`.\n",
255 type_name
= colon
+ 1;
256 if (*type_name
== '\0') {
258 "Invalid app-specific context field name: missing type name after colon in `%s`.\n",
263 provider_name
= strndup(field_name
, colon
- field_name
);
264 if (!provider_name
) {
265 fprintf(stderr
, "Failed to allocate string.\n");
269 event_expr
= lttng_event_expr_app_specific_context_field_create(
270 provider_name
, type_name
);
273 "Failed to create app-specific context field event expression.\n");
281 fprintf(stderr
, "%s: unexpected load expr type %d.\n",
282 __func__
, load_expr_op
->type
);
286 load_expr_op
= load_expr_op
->next
;
288 /* There may be a single array index after that. */
289 if (load_expr_op
->type
== IR_LOAD_EXPRESSION_GET_INDEX
) {
290 uint64_t index
= load_expr_op
->u
.index
;
291 struct lttng_event_expr
*index_event_expr
;
293 index_event_expr
= lttng_event_expr_array_field_element_create(event_expr
, index
);
294 if (!index_event_expr
) {
295 fprintf(stderr
, "Failed to create array field element event expression.\n");
299 event_expr
= index_event_expr
;
300 load_expr_op
= load_expr_op
->next
;
303 switch (load_expr_op
->type
) {
304 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
306 * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
307 * always found at the end of the chain.
310 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
311 fprintf(stderr
, "Error: While parsing expression `%s`: Capturing subfields is not supported.\n",
316 fprintf(stderr
, "%s: unexpected load expression operator %s.\n",
317 __func__
, ir_load_expression_type_str(load_expr_op
->type
));
324 lttng_event_expr_destroy(event_expr
);
334 struct lttng_event_expr
*ir_op_load_to_event_expr(struct ir_op
*ir
,
335 const char *capture_str
)
337 struct lttng_event_expr
*event_expr
= NULL
;
339 assert(ir
->op
== IR_OP_LOAD
);
341 switch (ir
->data_type
) {
342 case IR_DATA_EXPRESSION
:
344 struct ir_load_expression
*ir_load_expr
= ir
->u
.load
.u
.expression
;
345 event_expr
= ir_op_load_expr_to_event_expr(ir_load_expr
, capture_str
);
350 fprintf(stderr
, "%s: unexpected data type: %s.\n", __func__
,
351 ir_data_type_str(ir
->data_type
));
359 struct lttng_event_expr
*ir_op_root_to_event_expr(struct ir_op
*ir
,
360 const char *capture_str
)
362 struct lttng_event_expr
*event_expr
= NULL
;
364 assert(ir
->op
== IR_OP_ROOT
);
365 ir
= ir
->u
.root
.child
;
369 event_expr
= ir_op_load_to_event_expr(ir
, capture_str
);
373 fprintf(stderr
, "Error: While parsing expression `%s`: Binary operators are not allowed in capture expressions.\n",
378 fprintf(stderr
, "Error: While parsing expression `%s`: Unary operators are not allowed in capture expressions.\n",
383 fprintf(stderr
, "Error: While parsing expression `%s`: Logical operators are not allowed in capture expressions.\n",
388 fprintf(stderr
, "%s: unexpected IR op type: %s.\n", __func__
,
389 ir_op_type_str(ir
->op
));
397 void destroy_event_expr(void *ptr
)
399 lttng_event_expr_destroy(ptr
);
402 struct parse_event_rule_res
{
404 struct lttng_event_rule
*er
;
406 /* Array of `struct lttng_event_expr *` */
407 struct lttng_dynamic_pointer_array capture_descriptors
;
411 struct parse_event_rule_res
parse_event_rule(int *argc
, const char ***argv
)
413 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
414 enum lttng_event_rule_type event_rule_type
= LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
415 struct argpar_state
*state
;
416 struct argpar_item
*item
= NULL
;
418 int consumed_args
= -1;
419 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
420 struct parse_event_rule_res res
= { 0 };
421 struct lttng_event_expr
*event_expr
= NULL
;
422 struct filter_parser_ctx
*parser_ctx
= NULL
;
424 /* Was the -a/--all flag provided? */
425 bool all_events
= false;
427 /* Tracepoint name (non-option argument) */
428 const char *tracepoint_name
= NULL
;
430 /* Holds the argument of --probe / --userspace-probe. */
437 char *exclude
= NULL
;
438 char **exclusion_list
= NULL
;
441 char *loglevel_str
= NULL
;
442 bool loglevel_only
= false;
444 lttng_dynamic_pointer_array_init(&res
.capture_descriptors
,
446 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
448 ERR("Failed to allocate an argpar state.");
453 enum argpar_state_parse_next_status status
;
455 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
456 status
= argpar_state_parse_next(state
, &item
, &error
);
457 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
460 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
461 /* Just stop parsing here. */
463 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
467 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
469 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
470 struct argpar_item_opt
*item_opt
=
471 (struct argpar_item_opt
*) item
;
473 switch (item_opt
->descr
->id
) {
476 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_UST
)) {
482 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_KERNEL
)) {
488 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_LOG4J
)) {
494 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_JUL
)) {
500 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_PYTHON
)) {
505 /* Event rule types */
507 if (!assign_event_rule_type(&event_rule_type
,
508 LTTNG_EVENT_RULE_TYPE_KRETPROBE
)) {
514 if (!assign_event_rule_type(&event_rule_type
,
515 LTTNG_EVENT_RULE_TYPE_KPROBE
)) {
519 if (!assign_string(&source
, item_opt
->arg
, "source")) {
525 case OPT_USERSPACE_PROBE
:
526 if (!assign_event_rule_type(&event_rule_type
,
527 LTTNG_EVENT_RULE_TYPE_UPROBE
)) {
531 if (!assign_string(&source
, item_opt
->arg
, "source")) {
537 if (!assign_event_rule_type(&event_rule_type
,
538 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
544 if (!assign_event_rule_type(&event_rule_type
,
545 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
555 if (!assign_string(&filter
, item_opt
->arg
, "--filter/-f")) {
561 if (!assign_string(&exclude
, item_opt
->arg
, "--exclude/-x")) {
567 case OPT_LOGLEVEL_ONLY
:
568 if (!assign_string(&loglevel_str
, item_opt
->arg
, "--loglevel/--loglevel-only")) {
572 loglevel_only
= item_opt
->descr
->id
== OPT_LOGLEVEL_ONLY
;
577 const char *capture_str
= item_opt
->arg
;
580 ret
= filter_parser_ctx_create_from_filter_expression(
581 capture_str
, &parser_ctx
);
583 fprintf(stderr
, "Failed to parse capture expression `%s`.\n", capture_str
);
587 event_expr
= ir_op_root_to_event_expr(parser_ctx
->ir_root
,
590 /* ir_op_root_to_event_expr has printed an error message. */
594 ret
= lttng_dynamic_pointer_array_add_pointer(
595 &res
.capture_descriptors
,
601 /* The ownership of event expression was transferred to the dynamic array. */
611 struct argpar_item_non_opt
*item_non_opt
=
612 (struct argpar_item_non_opt
*) item
;
615 * Don't accept two non-option arguments/tracepoint
618 if (tracepoint_name
) {
620 "Unexpected argument: %s",
625 tracepoint_name
= item_non_opt
->arg
;
629 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
630 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
634 * Option -a is applicable to event rules of type tracepoint and
635 * syscall, and it is equivalent to using "*" as the tracepoint name.
638 switch (event_rule_type
) {
639 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
640 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
643 ERR("Can't use -a/--all with event rule of type %s.",
644 lttng_event_rule_type_str(event_rule_type
));
648 if (tracepoint_name
) {
649 ERR("Can't provide a tracepoint name with -a/--all.");
653 /* In which case, it's equivalent to tracepoint name "*". */
654 tracepoint_name
= "*";
658 * A tracepoint name (or -a, for the event rule types that accept it)
661 if (!tracepoint_name
) {
662 ERR("Need to provide either a tracepoint name or -a/--all.");
667 * We don't support multiple tracepoint names for now.
669 if (strchr(tracepoint_name
, ',')) {
670 ERR("multiple tracepoint names are not supported at the moment.");
675 * Update *argc and *argv so our caller can keep parsing what follows.
677 consumed_args
= argpar_state_get_ingested_orig_args(state
);
678 assert(consumed_args
>= 0);
679 *argc
-= consumed_args
;
680 *argv
+= consumed_args
;
682 /* Need to specify a domain. */
683 if (domain_type
== LTTNG_DOMAIN_NONE
) {
684 ERR("Please specify a domain (-k/-u/-j).");
688 /* Validate event rule type against domain. */
689 switch (event_rule_type
) {
690 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
691 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
692 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
693 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
694 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
695 ERR("Event type not available for user-space tracing");
700 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
708 * Adding a filter to a probe, function or userspace-probe would be
709 * denied by the kernel tracer as it's not supported at the moment. We
710 * do an early check here to warn the user.
712 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
713 switch (event_rule_type
) {
714 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
715 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
718 ERR("Filter expressions are not supported for %s events",
719 lttng_event_rule_type_str(event_rule_type
));
724 /* If --exclude/-x was passed, split it into an exclusion list. */
726 if (domain_type
!= LTTNG_DOMAIN_UST
) {
727 ERR("Event name exclusions are not yet implemented for %s events",
728 get_domain_str(domain_type
));
733 if (create_exclusion_list_and_validate(tracepoint_name
, exclude
,
734 &exclusion_list
) != 0) {
735 ERR("Failed to create exclusion list.");
740 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
741 ERR("Log levels are only application to tracepoint event rules.");
745 /* Finally, create the event rule object. */
746 switch (event_rule_type
) {
747 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
749 enum lttng_event_rule_status event_rule_status
;
751 res
.er
= lttng_event_rule_tracepoint_create(domain_type
);
753 ERR("Failed to create tracepoint event rule.");
759 lttng_event_rule_tracepoint_set_pattern(res
.er
, tracepoint_name
);
760 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
761 ERR("Failed to set tracepoint pattern.");
768 lttng_event_rule_tracepoint_set_filter(
770 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
771 ERR("Failed to set tracepoint filter expression.");
776 /* Set exclusion list. */
777 if (exclusion_list
) {
780 /* Count number of items in exclusion list. */
781 for (n
= 0; exclusion_list
[n
]; n
++);
784 lttng_event_rule_tracepoint_set_exclusions(
786 n
, (const char **) exclusion_list
);
787 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
788 ERR("Failed to set tracepoint exclusion list.");
796 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
797 ERR("Log levels are not supported by the kernel tracer.");
801 loglevel
= parse_loglevel_string(
802 loglevel_str
, domain_type
);
804 ERR("Failed to parse `%s` as a log level.", loglevel_str
);
810 lttng_event_rule_tracepoint_set_loglevel(
814 lttng_event_rule_tracepoint_set_loglevel_range(
818 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
819 ERR("Failed to set log level.");
827 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
829 enum lttng_event_rule_status event_rule_status
;
831 res
.er
= lttng_event_rule_kprobe_create();
833 ERR("Failed to create kprobe event rule.");
837 event_rule_status
= lttng_event_rule_kprobe_set_name(res
.er
, tracepoint_name
);
838 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
839 ERR("Failed to set kprobe event rule's name.");
844 event_rule_status
= lttng_event_rule_kprobe_set_source(res
.er
, source
);
845 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
846 ERR("Failed to set kprobe event rule's source.");
853 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
856 enum lttng_event_rule_status event_rule_status
;
858 ret
= parse_userspace_probe_opts(source
, &userspace_probe_location
);
860 ERR("Failed to parse userspace probe location.");
864 res
.er
= lttng_event_rule_uprobe_create();
866 ERR("Failed to create userspace probe event rule.");
870 event_rule_status
= lttng_event_rule_uprobe_set_location(res
.er
, userspace_probe_location
);
871 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
872 ERR("Failed to set userspace probe event rule's location.");
876 event_rule_status
= lttng_event_rule_uprobe_set_name(res
.er
, tracepoint_name
);
877 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
878 ERR("Failed to set userspace probe event rule's name.");
885 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
887 enum lttng_event_rule_status event_rule_status
;
889 res
.er
= lttng_event_rule_syscall_create();
891 ERR("Failed to create syscall event rule.");
895 event_rule_status
= lttng_event_rule_syscall_set_pattern(res
.er
, tracepoint_name
);
896 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
897 ERR("Failed to set syscall event rule's pattern.");
902 event_rule_status
= lttng_event_rule_syscall_set_filter(
904 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
905 ERR("Failed to set syscall event rule's filter expression.");
914 ERR("%s: I don't support event rules of type `%s` at the moment.", __func__
,
915 lttng_event_rule_type_str(event_rule_type
));
922 lttng_event_rule_destroy(res
.er
);
924 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
928 filter_parser_ctx_free(parser_ctx
);
931 lttng_event_expr_destroy(event_expr
);
932 argpar_item_destroy(item
);
934 argpar_state_destroy(state
);
938 strutils_free_null_terminated_array_of_strings(exclusion_list
);
939 lttng_userspace_probe_location_destroy(userspace_probe_location
);
944 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
946 struct parse_event_rule_res res
;
947 struct lttng_condition
*c
;
950 res
= parse_event_rule(argc
, argv
);
956 c
= lttng_condition_event_rule_create(res
.er
);
961 /* Event rule ownership moved to `c` */
964 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&res
.capture_descriptors
);
966 enum lttng_condition_status status
;
967 struct lttng_event_expr
**expr
=
968 lttng_dynamic_array_get_element(
969 &res
.capture_descriptors
.array
, i
);
973 status
= lttng_condition_event_rule_append_capture_descriptor(
975 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
976 if (status
== LTTNG_CONDITION_STATUS_UNSUPPORTED
) {
977 ERR("The capture feature is unsupported by the event-rule type");
982 /* Ownership of event expression moved to `c` */
989 lttng_condition_destroy(c
);
993 lttng_dynamic_pointer_array_reset(&res
.capture_descriptors
);
994 lttng_event_rule_destroy(res
.er
);
999 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
1001 struct lttng_condition
*cond
= NULL
;
1002 struct argpar_state
*state
= NULL
;
1003 struct argpar_item
*item
= NULL
;
1004 const char *threshold_arg
= NULL
;
1005 const char *session_name_arg
= NULL
;
1008 enum lttng_condition_status condition_status
;
1010 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1012 ERR("Failed to allocate an argpar state.");
1017 enum argpar_state_parse_next_status status
;
1019 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1020 status
= argpar_state_parse_next(state
, &item
, &error
);
1021 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1024 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1025 /* Just stop parsing here. */
1027 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1031 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1033 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1034 struct argpar_item_opt
*item_opt
=
1035 (struct argpar_item_opt
*) item
;
1037 switch (item_opt
->descr
->id
) {
1042 struct argpar_item_non_opt
*item_non_opt
;
1044 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1046 item_non_opt
= (struct argpar_item_non_opt
*) item
;
1048 switch (item_non_opt
->non_opt_index
) {
1050 session_name_arg
= item_non_opt
->arg
;
1053 threshold_arg
= item_non_opt
->arg
;
1056 ERR("Unexpected argument `%s`.",
1063 *argc
-= argpar_state_get_ingested_orig_args(state
);
1064 *argv
+= argpar_state_get_ingested_orig_args(state
);
1066 if (!session_name_arg
) {
1067 ERR("Missing session name argument.");
1071 if (!threshold_arg
) {
1072 ERR("Missing threshold argument.");
1076 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1077 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1081 cond
= lttng_condition_session_consumed_size_create();
1083 ERR("Failed to allocate a session consumed size condition.");
1087 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1088 cond
, session_name_arg
);
1089 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1090 ERR("Failed to set session consumed size condition session name.");
1095 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1097 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1098 ERR("Failed to set session consumed size condition threshold.");
1105 lttng_condition_destroy(cond
);
1109 argpar_state_destroy(state
);
1110 argpar_item_destroy(item
);
1116 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
1118 struct lttng_condition
*cond
= NULL
;
1119 struct argpar_state
*state
= NULL
;
1120 struct argpar_item
*item
= NULL
;
1121 const char *threshold_arg
= NULL
;
1122 const char *session_name_arg
= NULL
;
1125 enum lttng_condition_status condition_status
;
1127 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
1129 ERR("Failed to allocate an argpar state.");
1134 enum argpar_state_parse_next_status status
;
1136 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1137 status
= argpar_state_parse_next(state
, &item
, &error
);
1138 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1141 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1142 /* Just stop parsing here. */
1144 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1148 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1150 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1151 struct argpar_item_opt
*item_opt
=
1152 (struct argpar_item_opt
*) item
;
1154 switch (item_opt
->descr
->id
) {
1159 struct argpar_item_non_opt
*item_non_opt
;
1161 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1163 item_non_opt
= (struct argpar_item_non_opt
*) item
;
1165 switch (item_non_opt
->non_opt_index
) {
1167 session_name_arg
= item_non_opt
->arg
;
1170 threshold_arg
= item_non_opt
->arg
;
1173 ERR("Unexpected argument `%s`.",
1180 *argc
-= argpar_state_get_ingested_orig_args(state
);
1181 *argv
+= argpar_state_get_ingested_orig_args(state
);
1183 if (!session_name_arg
) {
1184 ERR("Missing session name argument.");
1188 if (!threshold_arg
) {
1189 ERR("Missing threshold argument.");
1193 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
1194 ERR("Failed to parse `%s` as a size.", threshold_arg
);
1198 cond
= lttng_condition_session_consumed_size_create();
1200 ERR("Failed to allocate a session consumed size condition.");
1204 condition_status
= lttng_condition_session_consumed_size_set_session_name(
1205 cond
, session_name_arg
);
1206 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1207 ERR("Failed to set session consumed size condition session name.");
1211 condition_status
= lttng_condition_session_consumed_size_set_threshold(
1213 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1214 ERR("Failed to set session consumed size condition threshold.");
1221 lttng_condition_destroy(cond
);
1225 argpar_state_destroy(state
);
1226 argpar_item_destroy(item
);
1232 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
1238 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
1244 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
1249 struct condition_descr
{
1251 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
1255 struct condition_descr condition_descrs
[] = {
1256 { "on-event", handle_condition_event
},
1257 { "on-session-consumed-size", handle_condition_session_consumed_size
},
1258 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
1259 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
1260 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
1261 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
1265 struct lttng_condition
*parse_condition(int *argc
, const char ***argv
)
1268 struct lttng_condition
*cond
;
1269 const char *condition_name
;
1270 const struct condition_descr
*descr
= NULL
;
1273 ERR("Missing condition name.");
1277 condition_name
= (*argv
)[0];
1282 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1283 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1284 descr
= &condition_descrs
[i
];
1290 ERR("Unknown condition name: %s", condition_name
);
1294 cond
= descr
->handler(argc
, argv
);
1296 /* The handler has already printed an error message. */
1309 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1311 return lttng_action_notify_create();
1314 static const struct argpar_opt_descr no_opt_descrs
[] = {
1315 ARGPAR_OPT_DESCR_SENTINEL
1319 * Generic handler for a kind of action that takes a session name as its sole
1324 struct lttng_action
*handle_action_simple_session(
1325 int *argc
, const char ***argv
,
1326 struct lttng_action
*(*create_action_cb
)(void),
1327 enum lttng_action_status (*set_session_name_cb
)(struct lttng_action
*, const char *),
1328 const char *action_name
)
1330 struct lttng_action
*action
= NULL
;
1331 struct argpar_state
*state
= NULL
;
1332 struct argpar_item
*item
= NULL
;
1333 const char *session_name_arg
= NULL
;
1335 enum lttng_action_status action_status
;
1337 state
= argpar_state_create(*argc
, *argv
, no_opt_descrs
);
1339 ERR("Failed to allocate an argpar state.");
1344 enum argpar_state_parse_next_status status
;
1345 struct argpar_item_non_opt
*item_non_opt
;
1347 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1348 status
= argpar_state_parse_next(state
, &item
, &error
);
1349 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1352 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1353 /* Just stop parsing here. */
1355 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1359 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1360 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1362 item_non_opt
= (struct argpar_item_non_opt
*) item
;
1364 switch (item_non_opt
->non_opt_index
) {
1366 session_name_arg
= item_non_opt
->arg
;
1369 ERR("Unexpected argument `%s`.",
1375 *argc
-= argpar_state_get_ingested_orig_args(state
);
1376 *argv
+= argpar_state_get_ingested_orig_args(state
);
1378 if (!session_name_arg
) {
1379 ERR("Missing session name.");
1383 action
= create_action_cb();
1386 "Failed to allocate %s session action.", action_name
);
1390 action_status
= set_session_name_cb(action
, session_name_arg
);
1391 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1393 "Failed to set action %s session's session name.",
1401 lttng_action_destroy(action
);
1409 struct lttng_action
*handle_action_start_session(int *argc
,
1412 return handle_action_simple_session(argc
, argv
,
1413 lttng_action_start_session_create
,
1414 lttng_action_start_session_set_session_name
,
1419 struct lttng_action
*handle_action_stop_session(int *argc
,
1422 return handle_action_simple_session(argc
, argv
,
1423 lttng_action_stop_session_create
,
1424 lttng_action_stop_session_set_session_name
,
1429 struct lttng_action
*handle_action_rotate_session(int *argc
,
1432 return handle_action_simple_session(argc
, argv
,
1433 lttng_action_rotate_session_create
,
1434 lttng_action_rotate_session_set_session_name
,
1438 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1439 { OPT_NAME
, 'n', "name", true },
1440 { OPT_MAX_SIZE
, 'm', "max-size", true },
1441 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1442 { OPT_DATA_URL
, '\0', "data-url", true },
1443 ARGPAR_OPT_DESCR_SENTINEL
1447 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1450 struct lttng_action
*action
= NULL
;
1451 struct argpar_state
*state
= NULL
;
1452 struct argpar_item
*item
= NULL
;
1453 const char *session_name_arg
= NULL
;
1454 char *snapshot_name_arg
= NULL
;
1455 char *ctrl_url_arg
= NULL
;
1456 char *data_url_arg
= NULL
;
1457 char *max_size_arg
= NULL
;
1458 const char *url_arg
= NULL
;
1460 enum lttng_action_status action_status
;
1461 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1464 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1466 ERR("Failed to allocate an argpar state.");
1471 enum argpar_state_parse_next_status status
;
1473 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1474 status
= argpar_state_parse_next(state
, &item
, &error
);
1475 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1478 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1479 /* Just stop parsing here. */
1481 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1485 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1487 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1488 struct argpar_item_opt
*item_opt
=
1489 (struct argpar_item_opt
*) item
;
1491 switch (item_opt
->descr
->id
) {
1493 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1499 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1505 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1511 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1520 struct argpar_item_non_opt
*item_non_opt
;
1522 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1524 item_non_opt
= (struct argpar_item_non_opt
*) item
;
1526 switch (item_non_opt
->non_opt_index
) {
1528 session_name_arg
= item_non_opt
->arg
;
1531 // FIXME: the use of a non-option argument for this is to
1532 // follow the syntax of `lttng snapshot record`. But otherwise,
1533 // I think an option argument would be best.
1535 url_arg
= item_non_opt
->arg
;
1539 ERR("Unexpected argument `%s`.",
1546 *argc
-= argpar_state_get_ingested_orig_args(state
);
1547 *argv
+= argpar_state_get_ingested_orig_args(state
);
1549 if (!session_name_arg
) {
1550 ERR("Missing session name.");
1554 /* --ctrl-url and --data-url must come in pair. */
1555 if (ctrl_url_arg
&& !data_url_arg
) {
1556 ERR("--ctrl-url is specified, but --data-url is missing.");
1560 if (!ctrl_url_arg
&& data_url_arg
) {
1561 ERR("--data-url is specified, but --ctrl-url is missing.");
1565 /* --ctrl-url/--data-url and the non-option URL are mutually exclusive. */
1566 if (ctrl_url_arg
&& url_arg
) {
1567 ERR("Both --ctrl-url/--data-url and the non-option URL argument "
1568 "can't be used together.");
1573 * Did the user specify an option that implies using a
1574 * custom/unregistered output?
1576 if (url_arg
|| ctrl_url_arg
) {
1577 snapshot_output
= lttng_snapshot_output_create();
1578 if (!snapshot_output
) {
1579 ERR("Failed to allocate a snapshot output.");
1584 action
= lttng_action_snapshot_session_create();
1587 "Failed to allocate snapshot session action.");
1591 action_status
= lttng_action_snapshot_session_set_session_name(
1592 action
, session_name_arg
);
1593 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1595 "Failed to set action snapshot session's session name.");
1599 if (snapshot_name_arg
) {
1600 if (!snapshot_output
) {
1601 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1605 ret
= lttng_snapshot_output_set_name(snapshot_name_arg
, snapshot_output
);
1607 ERR("Failed to set name of snapshot output.");
1615 if (!snapshot_output
) {
1616 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1620 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1622 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1626 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1628 ERR("Failed to set snapshot output's max size.");
1634 /* One argument form, either net:// / net6:// or a local file path. */
1636 if (strncmp(url_arg
, "net://", strlen("net://")) == 0 ||
1637 strncmp(url_arg
, "net6://", strlen("net6://")) == 0) {
1638 ret
= lttng_snapshot_output_set_network_url(
1639 url_arg
, snapshot_output
);
1641 ERR("Failed to parse %s as a network URL.", url_arg
);
1645 ret
= lttng_snapshot_output_set_local_path(
1646 url_arg
, snapshot_output
);
1648 ERR("Failed to parse %s as a local path.", url_arg
);
1656 * Two argument form, network output with separate control and
1659 ret
= lttng_snapshot_output_set_network_urls(
1660 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1662 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1663 ctrl_url_arg
, data_url_arg
);
1668 if (snapshot_output
) {
1669 action_status
= lttng_action_snapshot_session_set_output(
1670 action
, snapshot_output
);
1671 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1672 ERR("Failed to set snapshot session action's output.");
1676 /* Ownership of `snapshot_output` has been transferred to the action. */
1677 snapshot_output
= NULL
;
1683 lttng_action_destroy(action
);
1687 free(snapshot_name_arg
);
1690 free(snapshot_output
);
1694 struct action_descr
{
1696 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1700 struct action_descr action_descrs
[] = {
1701 { "notify", handle_action_notify
},
1702 { "start-session", handle_action_start_session
},
1703 { "stop-session", handle_action_stop_session
},
1704 { "rotate-session", handle_action_rotate_session
},
1705 { "snapshot-session", handle_action_snapshot_session
},
1709 struct lttng_action
*parse_action(int *argc
, const char ***argv
)
1712 struct lttng_action
*action
;
1713 const char *action_name
;
1714 const struct action_descr
*descr
= NULL
;
1717 ERR("Missing action name.");
1721 action_name
= (*argv
)[0];
1726 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1727 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1728 descr
= &action_descrs
[i
];
1734 ERR("Unknown action name: %s", action_name
);
1738 action
= descr
->handler(argc
, argv
);
1740 /* The handler has already printed an error message. */
1752 struct argpar_opt_descr add_trigger_options
[] = {
1753 { OPT_HELP
, 'h', "help", false },
1754 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
1755 { OPT_CONDITION
, '\0', "condition", false },
1756 { OPT_ACTION
, '\0', "action", false },
1757 { OPT_ID
, '\0', "id", true },
1758 { OPT_FIRE_ONCE_AFTER
, '\0', "fire-once-after", true },
1759 { OPT_FIRE_EVERY
, '\0', "fire-every", true },
1760 ARGPAR_OPT_DESCR_SENTINEL
,
1764 void lttng_actions_destructor(void *p
)
1766 struct lttng_action
*action
= p
;
1768 lttng_action_destroy(action
);
1771 int cmd_add_trigger(int argc
, const char **argv
)
1774 int my_argc
= argc
- 1;
1775 const char **my_argv
= argv
+ 1;
1776 struct lttng_condition
*condition
= NULL
;
1777 struct lttng_dynamic_pointer_array actions
;
1778 struct argpar_state
*argpar_state
= NULL
;
1779 struct argpar_item
*argpar_item
= NULL
;
1780 struct lttng_action
*action_group
= NULL
;
1781 struct lttng_action
*action
= NULL
;
1782 struct lttng_trigger
*trigger
= NULL
;
1786 char *fire_once_after_str
= NULL
;
1787 char *fire_every_str
= NULL
;
1789 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
1792 enum argpar_state_parse_next_status status
;
1793 struct argpar_item_opt
*item_opt
;
1796 argpar_state_destroy(argpar_state
);
1797 argpar_state
= argpar_state_create(my_argc
, my_argv
,
1798 add_trigger_options
);
1799 if (!argpar_state
) {
1800 ERR("Failed to create argpar state.");
1804 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
1806 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1809 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1812 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1816 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1818 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
1819 struct argpar_item_non_opt
*item_non_opt
=
1820 (struct argpar_item_non_opt
*) argpar_item
;
1822 ERR("Unexpected argument `%s`.",
1827 item_opt
= (struct argpar_item_opt
*) argpar_item
;
1829 ingested_args
= argpar_state_get_ingested_orig_args(
1832 my_argc
-= ingested_args
;
1833 my_argv
+= ingested_args
;
1835 switch (item_opt
->descr
->id
) {
1841 case OPT_LIST_OPTIONS
:
1842 list_cmd_options_argpar(stdout
, add_trigger_options
);
1849 ERR("A --condition was already given.");
1853 condition
= parse_condition(&my_argc
, &my_argv
);
1856 * An error message was already printed by
1867 action
= parse_action(&my_argc
, &my_argv
);
1870 * An error message was already printed by
1876 ret
= lttng_dynamic_pointer_array_add_pointer(
1879 ERR("Failed to add pointer to pointer array.");
1883 /* Ownership of the action was transferred to the group. */
1891 if (!assign_string(&id
, item_opt
->arg
, "--id")) {
1898 case OPT_FIRE_ONCE_AFTER
:
1900 if (!assign_string(&fire_once_after_str
, item_opt
->arg
,
1901 "--fire-once-after")) {
1907 case OPT_FIRE_EVERY
:
1909 if (!assign_string(&fire_every_str
, item_opt
->arg
,
1922 ERR("Missing --condition.");
1926 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
1927 ERR("Need at least one --action.");
1931 if (fire_every_str
&& fire_once_after_str
) {
1932 ERR("Can't specify both --fire-once-after and --fire-every.");
1936 action_group
= lttng_action_group_create();
1937 if (!action_group
) {
1941 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
1942 enum lttng_action_status status
;
1944 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
1946 status
= lttng_action_group_add_action(
1947 action_group
, action
);
1948 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1952 /* Ownership of the action was transferred to the group. */
1957 trigger
= lttng_trigger_create(condition
, action_group
);
1963 enum lttng_trigger_status trigger_status
=
1964 lttng_trigger_set_name(trigger
, id
);
1965 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1966 ERR("Failed to set trigger id.");
1971 if (fire_once_after_str
) {
1972 unsigned long long threshold
;
1973 enum lttng_trigger_status trigger_status
;
1975 if (utils_parse_unsigned_long_long(fire_once_after_str
, &threshold
) != 0) {
1976 ERR("Failed to parse `%s` as an integer.", fire_once_after_str
);
1980 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
1981 LTTNG_TRIGGER_FIRE_ONCE_AFTER_N
, threshold
);
1982 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1983 ERR("Failed to set trigger's firing policy.");
1988 if (fire_every_str
) {
1989 unsigned long long threshold
;
1990 enum lttng_trigger_status trigger_status
;
1992 if (utils_parse_unsigned_long_long(fire_every_str
, &threshold
) != 0) {
1993 ERR("Failed to parse `%s` as an integer.", fire_every_str
);
1997 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
1998 LTTNG_TRIGGER_FIRE_EVERY_N
, threshold
);
1999 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2000 ERR("Failed to set trigger's firing policy.");
2005 ret
= lttng_register_trigger(trigger
);
2007 ERR("Failed to register trigger: %s.",
2008 lttng_strerror(ret
));
2012 MSG("Trigger registered successfully.");
2020 argpar_state_destroy(argpar_state
);
2021 lttng_dynamic_pointer_array_reset(&actions
);
2022 lttng_condition_destroy(condition
);
2023 lttng_action_destroy(action_group
);
2024 lttng_trigger_destroy(trigger
);
2026 free(fire_once_after_str
);
2027 free(fire_every_str
);
2028 // TODO: check what else to free