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>
23 #if (LTTNG_SYMBOL_NAME_LEN == 256)
24 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-add-trigger.1.h>
70 static const struct argpar_opt_descr event_rule_opt_descrs
[] = {
71 { OPT_ALL
, 'a', "all", false },
72 { OPT_FILTER
, 'f', "filter", true },
73 { OPT_EXCLUDE
, 'x', "exclude", true },
74 { OPT_LOGLEVEL
, '\0', "loglevel", true },
75 { OPT_LOGLEVEL_ONLY
, '\0', "loglevel-only", true },
78 { OPT_USERSPACE
, 'u', "userspace", false },
79 { OPT_KERNEL
, 'k', "kernel", false },
80 { OPT_LOG4J
, 'l', "log4j", false },
81 { OPT_JUL
, 'j', "jul", false },
82 { OPT_PYTHON
, 'p', "python", false },
84 /* Event rule types */
85 { OPT_FUNCTION
, '\0', "function", true },
86 { OPT_PROBE
, '\0', "probe", true },
87 { OPT_USERSPACE_PROBE
, '\0', "userspace-probe", true },
88 { OPT_SYSCALL
, '\0', "syscall" },
89 { OPT_TRACEPOINT
, '\0', "tracepoint" },
91 ARGPAR_OPT_DESCR_SENTINEL
95 bool assign_domain_type(enum lttng_domain_type
*dest
,
96 enum lttng_domain_type src
)
100 if (*dest
== LTTNG_DOMAIN_NONE
|| *dest
== src
) {
104 ERR("Multiple domains specified.");
112 bool assign_event_rule_type(enum lttng_event_rule_type
*dest
,
113 enum lttng_event_rule_type src
)
117 if (*dest
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
|| *dest
== src
) {
121 ERR("Multiple event types specified.");
129 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
134 ERR("Duplicate '%s' given.", opt_name
);
140 PERROR("Failed to allocate string '%s'.", opt_name
);
154 /* This is defined in enable_events.c. */
156 int create_exclusion_list_and_validate(const char *event_name
,
157 const char *exclusions_arg
,
158 char ***exclusion_list
);
161 * Parse `str` as a log level in domain `domain_type`. Return -1 if the string
162 * is not recognized as a valid log level.
165 int parse_loglevel_string(const char *str
, enum lttng_domain_type domain_type
)
167 switch (domain_type
) {
168 case LTTNG_DOMAIN_UST
:
170 enum lttng_loglevel loglevel
;
171 const int ret
= loglevel_name_to_value(str
, &loglevel
);
173 return ret
== -1 ? ret
: (int) loglevel
;
175 case LTTNG_DOMAIN_LOG4J
:
177 enum lttng_loglevel_log4j loglevel
;
178 const int ret
= loglevel_log4j_name_to_value(str
, &loglevel
);
180 return ret
== -1 ? ret
: (int) loglevel
;
182 case LTTNG_DOMAIN_JUL
:
184 enum lttng_loglevel_jul loglevel
;
185 const int ret
= loglevel_jul_name_to_value(str
, &loglevel
);
187 return ret
== -1 ? ret
: (int) loglevel
;
189 case LTTNG_DOMAIN_PYTHON
:
191 enum lttng_loglevel_python loglevel
;
192 const int ret
= loglevel_python_name_to_value(str
, &loglevel
);
194 return ret
== -1 ? ret
: (int) loglevel
;
197 /* Invalid domain type. */
202 static int parse_kernel_probe_opts(const char *source
,
203 struct lttng_kernel_probe_location
**location
)
208 char name
[LTTNG_SYMBOL_NAME_LEN
];
209 char *symbol_name
= NULL
;
212 /* Check for symbol+offset. */
213 match
= sscanf(source
,
214 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
218 if (*s_hex
== '\0') {
219 ERR("Kernel probe symbol offset is missing.");
223 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
225 PERROR("Failed to copy kernel probe location symbol name.");
228 offset
= strtoul(s_hex
, NULL
, 0);
230 *location
= lttng_kernel_probe_location_symbol_create(
231 symbol_name
, offset
);
233 ERR("Failed to create symbol kernel probe location.");
240 /* Check for symbol. */
241 if (isalpha(name
[0]) || name
[0] == '_') {
242 match
= sscanf(source
,
243 "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
247 symbol_name
= strndup(name
, LTTNG_SYMBOL_NAME_LEN
);
249 ERR("Failed to copy kernel probe location symbol name.");
253 *location
= lttng_kernel_probe_location_symbol_create(
256 ERR("Failed to create symbol kernel probe location.");
264 /* Check for address. */
265 match
= sscanf(source
, "%18s", s_hex
);
269 if (*s_hex
== '\0') {
270 ERR("Invalid kernel probe location address.");
274 address
= strtoul(s_hex
, NULL
, 0);
275 *location
= lttng_kernel_probe_location_address_create(address
);
277 ERR("Failed to create symbol kernel probe location.");
294 static struct lttng_event_rule
*parse_event_rule(int *argc
, const char ***argv
)
296 struct lttng_event_rule
*er
= NULL
;
297 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
298 enum lttng_event_rule_type event_rule_type
=
299 LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
300 struct argpar_state
*state
;
301 struct argpar_item
*item
= NULL
;
303 int consumed_args
= -1;
304 struct lttng_kernel_probe_location
*kernel_probe_location
= NULL
;
305 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
307 /* Was the -a/--all flag provided? */
308 bool all_events
= false;
310 /* Tracepoint name (non-option argument). */
311 const char *tracepoint_name
= NULL
;
313 /* Holds the argument of --probe / --userspace-probe. */
320 char *exclude
= NULL
;
321 char **exclusion_list
= NULL
;
324 char *loglevel_str
= NULL
;
325 bool loglevel_only
= false;
327 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
329 ERR("Failed to allocate an argpar state.");
334 enum argpar_state_parse_next_status status
;
336 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
337 status
= argpar_state_parse_next(state
, &item
, &error
);
338 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
341 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
342 /* Just stop parsing here. */
344 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
348 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
350 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
351 const struct argpar_item_opt
*item_opt
=
352 (const struct argpar_item_opt
*) item
;
354 switch (item_opt
->descr
->id
) {
357 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_UST
)) {
363 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_KERNEL
)) {
369 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_LOG4J
)) {
375 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_JUL
)) {
381 if (!assign_domain_type(&domain_type
, LTTNG_DOMAIN_PYTHON
)) {
387 /* Event rule types */
389 if (!assign_event_rule_type(&event_rule_type
,
390 LTTNG_EVENT_RULE_TYPE_KRETPROBE
)) {
396 if (!assign_event_rule_type(&event_rule_type
,
397 LTTNG_EVENT_RULE_TYPE_KPROBE
)) {
401 if (!assign_string(&source
, item_opt
->arg
, "source")) {
406 case OPT_USERSPACE_PROBE
:
407 if (!assign_event_rule_type(&event_rule_type
,
408 LTTNG_EVENT_RULE_TYPE_UPROBE
)) {
412 if (!assign_string(&source
, item_opt
->arg
, "source")) {
418 if (!assign_event_rule_type(&event_rule_type
,
419 LTTNG_EVENT_RULE_TYPE_SYSCALL
)) {
425 if (!assign_event_rule_type(&event_rule_type
,
426 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
)) {
435 if (!assign_string(&filter
, item_opt
->arg
,
442 if (!assign_string(&exclude
, item_opt
->arg
,
449 case OPT_LOGLEVEL_ONLY
:
450 if (!assign_string(&loglevel_str
, item_opt
->arg
,
451 "--loglevel/--loglevel-only")) {
455 loglevel_only
= item_opt
->descr
->id
==
462 const struct argpar_item_non_opt
*item_non_opt
=
463 (const struct argpar_item_non_opt
*)
467 * Don't accept two non-option arguments/tracepoint
470 if (tracepoint_name
) {
471 ERR("Unexpected argument '%s'",
476 tracepoint_name
= item_non_opt
->arg
;
480 if (event_rule_type
== LTTNG_EVENT_RULE_TYPE_UNKNOWN
) {
481 event_rule_type
= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
;
485 * Option -a is applicable to event rules of type tracepoint and
486 * syscall, and it is equivalent to using "*" as the tracepoint name.
489 switch (event_rule_type
) {
490 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
491 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
494 ERR("Can't use -a/--all with %s event rules.",
495 lttng_event_rule_type_str(event_rule_type
));
499 if (tracepoint_name
) {
500 ERR("Can't provide a tracepoint name with -a/--all.");
504 /* In which case, it's equivalent to tracepoint name "*". */
505 tracepoint_name
= "*";
509 * A tracepoint name (or -a, for the event rule types that accept it)
512 if (!tracepoint_name
) {
513 ERR("Need to provide either a tracepoint name or -a/--all.");
518 * We don't support multiple tracepoint names for now.
520 if (strchr(tracepoint_name
, ',')) {
521 ERR("Comma separated tracepoint names are not supported.");
526 * Update *argc and *argv so our caller can keep parsing what follows.
528 consumed_args
= argpar_state_get_ingested_orig_args(state
);
529 assert(consumed_args
>= 0);
530 *argc
-= consumed_args
;
531 *argv
+= consumed_args
;
533 /* Need to specify a domain. */
534 if (domain_type
== LTTNG_DOMAIN_NONE
) {
535 ERR("Please specify a domain (--kernel/--userspace/--jul/--log4j/--python).");
539 /* Validate event rule type against domain. */
540 switch (event_rule_type
) {
541 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
542 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
543 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
544 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
545 if (domain_type
!= LTTNG_DOMAIN_KERNEL
) {
546 ERR("Event type not available for user-space tracing.");
551 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
559 * Adding a filter to a probe, function or userspace-probe would be
560 * denied by the kernel tracer as it's not supported at the moment. We
561 * do an early check here to warn the user.
563 if (filter
&& domain_type
== LTTNG_DOMAIN_KERNEL
) {
564 switch (event_rule_type
) {
565 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
566 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
569 ERR("Filter expressions are not supported for %s event rules.",
570 lttng_event_rule_type_str(event_rule_type
));
575 /* If --exclude/-x was passed, split it into an exclusion list. */
577 if (domain_type
!= LTTNG_DOMAIN_UST
) {
578 ERR("Event name exclusions are not yet implemented for %s event rules.",
579 get_domain_str(domain_type
));
584 if (create_exclusion_list_and_validate(tracepoint_name
, exclude
,
585 &exclusion_list
) != 0) {
586 ERR("Failed to create exclusion list.");
591 if (loglevel_str
&& event_rule_type
!= LTTNG_EVENT_RULE_TYPE_TRACEPOINT
) {
592 ERR("Log levels are only applicable to tracepoint event rules.");
596 /* Finally, create the event rule object. */
597 switch (event_rule_type
) {
598 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
600 enum lttng_event_rule_status event_rule_status
;
602 er
= lttng_event_rule_tracepoint_create(domain_type
);
604 ERR("Failed to create tracepoint event rule.");
609 event_rule_status
= lttng_event_rule_tracepoint_set_pattern(
610 er
, tracepoint_name
);
611 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
612 ERR("Failed to set tracepoint event rule's pattern to '%s'.",
619 event_rule_status
= lttng_event_rule_tracepoint_set_filter(
621 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
622 ERR("Failed to set tracepoint event rule's filter to '%s'.",
628 /* Set exclusion list. */
629 if (exclusion_list
) {
632 for (n
= 0; exclusion_list
[n
]; n
++) {
633 event_rule_status
= lttng_event_rule_tracepoint_add_exclusion(
636 if (event_rule_status
!=
637 LTTNG_EVENT_RULE_STATUS_OK
) {
638 ERR("Failed to set tracepoint exclusion list element '%s'",
648 if (domain_type
== LTTNG_DOMAIN_KERNEL
) {
649 ERR("Log levels are not supported by the kernel tracer.");
653 loglevel
= parse_loglevel_string(
654 loglevel_str
, domain_type
);
656 ERR("Failed to parse `%s` as a log level.",
662 event_rule_status
= lttng_event_rule_tracepoint_set_log_level(
665 event_rule_status
= lttng_event_rule_tracepoint_set_log_level_range_lower_bound(
669 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
670 ERR("Failed to set log level on event fule.");
677 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
680 enum lttng_event_rule_status event_rule_status
;
682 er
= lttng_event_rule_kprobe_create();
684 ERR("Failed to create kprobe event rule.");
688 ret
= parse_kernel_probe_opts(source
, &kernel_probe_location
);
690 ERR("Failed to parse kernel probe location.");
694 event_rule_status
= lttng_event_rule_kprobe_set_name(er
, tracepoint_name
);
695 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
696 ERR("Failed to set kprobe event rule's name to '%s'.", tracepoint_name
);
700 assert(kernel_probe_location
);
701 event_rule_status
= lttng_event_rule_kprobe_set_location(er
, kernel_probe_location
);
702 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
703 ERR("Failed to set kprobe event rule's location.");
709 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
712 enum lttng_event_rule_status event_rule_status
;
714 ret
= parse_userspace_probe_opts(
715 source
, &userspace_probe_location
);
717 ERR("Failed to parse user space probe location.");
721 er
= lttng_event_rule_uprobe_create();
723 ERR("Failed to create user space probe event rule.");
727 event_rule_status
= lttng_event_rule_uprobe_set_location(
728 er
, userspace_probe_location
);
729 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
730 ERR("Failed to set user space probe event rule's location.");
734 event_rule_status
= lttng_event_rule_uprobe_set_name(
735 er
, tracepoint_name
);
736 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
737 ERR("Failed to set user space probe event rule's name to '%s'.",
744 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
746 enum lttng_event_rule_status event_rule_status
;
748 er
= lttng_event_rule_syscall_create();
750 ERR("Failed to create syscall event rule.");
754 event_rule_status
= lttng_event_rule_syscall_set_pattern(
755 er
, tracepoint_name
);
756 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
757 ERR("Failed to set syscall event rule's pattern to '%s'.",
763 event_rule_status
= lttng_event_rule_syscall_set_filter(
765 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
766 ERR("Failed to set syscall event rule's filter to '%s'.",
782 lttng_event_rule_destroy(er
);
786 argpar_item_destroy(item
);
788 argpar_state_destroy(state
);
793 strutils_free_null_terminated_array_of_strings(exclusion_list
);
794 lttng_kernel_probe_location_destroy(kernel_probe_location
);
795 lttng_userspace_probe_location_destroy(userspace_probe_location
);
800 struct lttng_condition
*handle_condition_event(int *argc
, const char ***argv
)
802 struct lttng_event_rule
*er
;
803 struct lttng_condition
*c
;
805 er
= parse_event_rule(argc
, argv
);
811 c
= lttng_condition_event_rule_create(er
);
812 lttng_event_rule_destroy(er
);
822 struct lttng_condition
*handle_condition_session_consumed_size(int *argc
, const char ***argv
)
824 struct lttng_condition
*cond
= NULL
;
825 struct argpar_state
*state
= NULL
;
826 struct argpar_item
*item
= NULL
;
827 const char *threshold_arg
= NULL
;
828 const char *session_name_arg
= NULL
;
831 enum lttng_condition_status condition_status
;
833 state
= argpar_state_create(*argc
, *argv
, event_rule_opt_descrs
);
835 ERR("Failed to allocate an argpar state.");
840 enum argpar_state_parse_next_status status
;
842 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
843 status
= argpar_state_parse_next(state
, &item
, &error
);
844 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
847 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
848 /* Just stop parsing here. */
850 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
854 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
856 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
857 const struct argpar_item_opt
*item_opt
=
858 (const struct argpar_item_opt
*) item
;
860 switch (item_opt
->descr
->id
) {
865 const struct argpar_item_non_opt
*item_non_opt
;
867 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
869 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
871 switch (item_non_opt
->non_opt_index
) {
873 session_name_arg
= item_non_opt
->arg
;
876 threshold_arg
= item_non_opt
->arg
;
879 ERR("Unexpected argument `%s`.",
886 *argc
-= argpar_state_get_ingested_orig_args(state
);
887 *argv
+= argpar_state_get_ingested_orig_args(state
);
889 if (!session_name_arg
) {
890 ERR("Missing session name argument.");
894 if (!threshold_arg
) {
895 ERR("Missing threshold argument.");
899 if (utils_parse_size_suffix(threshold_arg
, &threshold
) != 0) {
900 ERR("Failed to parse `%s` as a size.", threshold_arg
);
904 cond
= lttng_condition_session_consumed_size_create();
906 ERR("Failed to allocate a session consumed size condition.");
910 condition_status
= lttng_condition_session_consumed_size_set_session_name(
911 cond
, session_name_arg
);
912 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
913 ERR("Failed to set session consumed size condition's session name to '%s'.",
918 condition_status
= lttng_condition_session_consumed_size_set_threshold(
920 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
921 ERR("Failed to set session consumed size condition threshold.");
928 lttng_condition_destroy(cond
);
932 argpar_state_destroy(state
);
933 argpar_item_destroy(item
);
939 struct lttng_condition
*handle_condition_buffer_usage_high(int *argc
, const char ***argv
)
941 ERR("High buffer usage threshold conditions are unsupported for the moment.");
946 struct lttng_condition
*handle_condition_buffer_usage_low(int *argc
, const char ***argv
)
948 ERR("Low buffer usage threshold conditions are unsupported for the moment.");
953 struct lttng_condition
*handle_condition_session_rotation_ongoing(int *argc
, const char ***argv
)
955 ERR("Session rotation ongoing conditions are unsupported for the moment.");
960 struct lttng_condition
*handle_condition_session_rotation_completed(int *argc
, const char ***argv
)
962 ERR("Session rotation completed conditions are unsupported for the moment.");
966 struct condition_descr
{
968 struct lttng_condition
*(*handler
) (int *argc
, const char ***argv
);
972 struct condition_descr condition_descrs
[] = {
973 { "on-event", handle_condition_event
},
974 { "on-session-consumed-size", handle_condition_session_consumed_size
},
975 { "on-buffer-usage-high", handle_condition_buffer_usage_high
},
976 { "on-buffer-usage-low", handle_condition_buffer_usage_low
},
977 { "on-session-rotation-ongoing", handle_condition_session_rotation_ongoing
},
978 { "on-session-rotation-completed", handle_condition_session_rotation_completed
},
982 struct lttng_condition
*parse_condition(int *argc
, const char ***argv
)
985 struct lttng_condition
*cond
;
986 const char *condition_name
;
987 const struct condition_descr
*descr
= NULL
;
990 ERR("Missing condition name.");
994 condition_name
= (*argv
)[0];
999 for (i
= 0; i
< ARRAY_SIZE(condition_descrs
); i
++) {
1000 if (strcmp(condition_name
, condition_descrs
[i
].name
) == 0) {
1001 descr
= &condition_descrs
[i
];
1007 ERR("Unknown condition name '%s'", condition_name
);
1011 cond
= descr
->handler(argc
, argv
);
1013 /* The handler has already printed an error message. */
1026 struct lttng_action
*handle_action_notify(int *argc
, const char ***argv
)
1028 return lttng_action_notify_create();
1031 static const struct argpar_opt_descr no_opt_descrs
[] = {
1032 ARGPAR_OPT_DESCR_SENTINEL
1036 * Generic handler for a kind of action that takes a session name as its sole
1041 struct lttng_action
*handle_action_simple_session(
1042 int *argc
, const char ***argv
,
1043 struct lttng_action
*(*create_action_cb
)(void),
1044 enum lttng_action_status (*set_session_name_cb
)(struct lttng_action
*, const char *),
1045 const char *action_name
)
1047 struct lttng_action
*action
= NULL
;
1048 struct argpar_state
*state
= NULL
;
1049 struct argpar_item
*item
= NULL
;
1050 const char *session_name_arg
= NULL
;
1052 enum lttng_action_status action_status
;
1054 state
= argpar_state_create(*argc
, *argv
, no_opt_descrs
);
1056 ERR("Failed to allocate an argpar state.");
1061 enum argpar_state_parse_next_status status
;
1062 const struct argpar_item_non_opt
*item_non_opt
;
1064 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1065 status
= argpar_state_parse_next(state
, &item
, &error
);
1066 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1069 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1070 /* Just stop parsing here. */
1072 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1076 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1077 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1079 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1081 switch (item_non_opt
->non_opt_index
) {
1083 session_name_arg
= item_non_opt
->arg
;
1086 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
1091 *argc
-= argpar_state_get_ingested_orig_args(state
);
1092 *argv
+= argpar_state_get_ingested_orig_args(state
);
1094 if (!session_name_arg
) {
1095 ERR("Missing session name.");
1099 action
= create_action_cb();
1101 ERR("Failed to allocate %s session action.", action_name
);
1105 action_status
= set_session_name_cb(action
, session_name_arg
);
1106 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1107 ERR("Failed to set action %s session's session name to '%s'.",
1108 action_name
, session_name_arg
);
1115 lttng_action_destroy(action
);
1117 argpar_item_destroy(item
);
1120 argpar_state_destroy(state
);
1125 struct lttng_action
*handle_action_start_session(int *argc
,
1128 return handle_action_simple_session(argc
, argv
,
1129 lttng_action_start_session_create
,
1130 lttng_action_start_session_set_session_name
,
1135 struct lttng_action
*handle_action_stop_session(int *argc
,
1138 return handle_action_simple_session(argc
, argv
,
1139 lttng_action_stop_session_create
,
1140 lttng_action_stop_session_set_session_name
,
1145 struct lttng_action
*handle_action_rotate_session(int *argc
,
1148 return handle_action_simple_session(argc
, argv
,
1149 lttng_action_rotate_session_create
,
1150 lttng_action_rotate_session_set_session_name
,
1154 static const struct argpar_opt_descr snapshot_action_opt_descrs
[] = {
1155 { OPT_NAME
, 'n', "name", true },
1156 { OPT_MAX_SIZE
, 'm', "max-size", true },
1157 { OPT_CTRL_URL
, '\0', "ctrl-url", true },
1158 { OPT_DATA_URL
, '\0', "data-url", true },
1159 { OPT_URL
, '\0', "url", true },
1160 { OPT_PATH
, '\0', "path", true },
1161 ARGPAR_OPT_DESCR_SENTINEL
1165 struct lttng_action
*handle_action_snapshot_session(int *argc
,
1168 struct lttng_action
*action
= NULL
;
1169 struct argpar_state
*state
= NULL
;
1170 struct argpar_item
*item
= NULL
;
1171 const char *session_name_arg
= NULL
;
1172 char *snapshot_name_arg
= NULL
;
1173 char *ctrl_url_arg
= NULL
;
1174 char *data_url_arg
= NULL
;
1175 char *max_size_arg
= NULL
;
1176 char *url_arg
= NULL
;
1177 char *path_arg
= NULL
;
1179 enum lttng_action_status action_status
;
1180 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1182 unsigned int locations_specified
= 0;
1184 state
= argpar_state_create(*argc
, *argv
, snapshot_action_opt_descrs
);
1186 ERR("Failed to allocate an argpar state.");
1191 enum argpar_state_parse_next_status status
;
1193 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
1194 status
= argpar_state_parse_next(state
, &item
, &error
);
1195 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1198 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1199 /* Just stop parsing here. */
1201 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1205 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1207 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1208 const struct argpar_item_opt
*item_opt
=
1209 (const struct argpar_item_opt
*) item
;
1211 switch (item_opt
->descr
->id
) {
1213 if (!assign_string(&snapshot_name_arg
, item_opt
->arg
, "--name/-n")) {
1219 if (!assign_string(&max_size_arg
, item_opt
->arg
, "--max-size/-m")) {
1225 if (!assign_string(&ctrl_url_arg
, item_opt
->arg
, "--ctrl-url")) {
1231 if (!assign_string(&data_url_arg
, item_opt
->arg
, "--data-url")) {
1237 if (!assign_string(&url_arg
, item_opt
->arg
, "--url")) {
1243 if (!assign_string(&path_arg
, item_opt
->arg
, "--path")) {
1252 const struct argpar_item_non_opt
*item_non_opt
;
1254 assert(item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
);
1256 item_non_opt
= (const struct argpar_item_non_opt
*) item
;
1258 switch (item_non_opt
->non_opt_index
) {
1260 session_name_arg
= item_non_opt
->arg
;
1263 ERR("Unexpected argument `%s`.",
1270 *argc
-= argpar_state_get_ingested_orig_args(state
);
1271 *argv
+= argpar_state_get_ingested_orig_args(state
);
1273 if (!session_name_arg
) {
1274 ERR("Missing session name.");
1278 /* --ctrl-url and --data-url must come in pair. */
1279 if (ctrl_url_arg
&& !data_url_arg
) {
1280 ERR("--ctrl-url is specified, but --data-url is missing.");
1284 if (!ctrl_url_arg
&& data_url_arg
) {
1285 ERR("--data-url is specified, but --ctrl-url is missing.");
1289 locations_specified
+= !!(ctrl_url_arg
|| data_url_arg
);
1290 locations_specified
+= !!url_arg
;
1291 locations_specified
+= !!path_arg
;
1293 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1294 if (locations_specified
> 1) {
1295 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1300 * Did the user specify an option that implies using a
1301 * custom/unregistered output?
1303 if (url_arg
|| ctrl_url_arg
|| path_arg
) {
1304 snapshot_output
= lttng_snapshot_output_create();
1305 if (!snapshot_output
) {
1306 ERR("Failed to allocate a snapshot output.");
1311 action
= lttng_action_snapshot_session_create();
1313 ERR("Failed to allocate snapshot session action.");
1317 action_status
= lttng_action_snapshot_session_set_session_name(
1318 action
, session_name_arg
);
1319 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1320 ERR("Failed to set action snapshot session's session name to '%s'.",
1325 if (snapshot_name_arg
) {
1326 if (!snapshot_output
) {
1327 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1331 ret
= lttng_snapshot_output_set_name(
1332 snapshot_name_arg
, snapshot_output
);
1334 ERR("Failed to set name of snapshot output.");
1342 if (!snapshot_output
) {
1343 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1347 ret
= utils_parse_size_suffix(max_size_arg
, &max_size
);
1349 ERR("Failed to parse `%s` as a size.", max_size_arg
);
1353 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1355 ERR("Failed to set snapshot output's max size to %" PRIu64
" bytes.",
1363 struct lttng_uri
*uris
;
1365 if (!strstr(url_arg
, "://")) {
1366 ERR("Failed to parse '%s' as an URL.", url_arg
);
1370 num_uris
= uri_parse_str_urls(url_arg
, NULL
, &uris
);
1372 ERR("Failed to parse '%s' as an URL.", url_arg
);
1376 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1377 ret
= lttng_snapshot_output_set_local_path(
1378 uris
[0].dst
.path
, snapshot_output
);
1381 ERR("Failed to assign '%s' as a local destination.",
1386 ret
= lttng_snapshot_output_set_network_url(
1387 url_arg
, snapshot_output
);
1390 ERR("Failed to assign '%s' as a network URL.",
1398 ret
= lttng_snapshot_output_set_local_path(
1399 path_arg
, snapshot_output
);
1401 ERR("Failed to parse '%s' as a local path.", path_arg
);
1408 * Two argument form, network output with separate control and
1411 ret
= lttng_snapshot_output_set_network_urls(
1412 ctrl_url_arg
, data_url_arg
, snapshot_output
);
1414 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
1415 ctrl_url_arg
, data_url_arg
);
1420 if (snapshot_output
) {
1421 action_status
= lttng_action_snapshot_session_set_output(
1422 action
, snapshot_output
);
1423 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
1424 ERR("Failed to set snapshot session action's output.");
1428 /* Ownership of `snapshot_output` has been transferred to the action. */
1429 snapshot_output
= NULL
;
1435 lttng_action_destroy(action
);
1439 free(snapshot_name_arg
);
1444 free(snapshot_output
);
1446 argpar_state_destroy(state
);
1447 argpar_item_destroy(item
);
1451 struct action_descr
{
1453 struct lttng_action
*(*handler
) (int *argc
, const char ***argv
);
1457 struct action_descr action_descrs
[] = {
1458 { "notify", handle_action_notify
},
1459 { "start-session", handle_action_start_session
},
1460 { "stop-session", handle_action_stop_session
},
1461 { "rotate-session", handle_action_rotate_session
},
1462 { "snapshot-session", handle_action_snapshot_session
},
1466 struct lttng_action
*parse_action(int *argc
, const char ***argv
)
1469 struct lttng_action
*action
;
1470 const char *action_name
;
1471 const struct action_descr
*descr
= NULL
;
1474 ERR("Missing action name.");
1478 action_name
= (*argv
)[0];
1483 for (i
= 0; i
< ARRAY_SIZE(action_descrs
); i
++) {
1484 if (strcmp(action_name
, action_descrs
[i
].name
) == 0) {
1485 descr
= &action_descrs
[i
];
1491 ERR("Unknown action name: %s", action_name
);
1495 action
= descr
->handler(argc
, argv
);
1497 /* The handler has already printed an error message. */
1509 struct argpar_opt_descr add_trigger_options
[] = {
1510 { OPT_HELP
, 'h', "help", false },
1511 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
1512 { OPT_CONDITION
, '\0', "condition", false },
1513 { OPT_ACTION
, '\0', "action", false },
1514 { OPT_ID
, '\0', "id", true },
1515 { OPT_FIRE_ONCE_AFTER
, '\0', "fire-once-after", true },
1516 { OPT_FIRE_EVERY
, '\0', "fire-every", true },
1517 { OPT_USER_ID
, '\0', "user-id", true },
1518 ARGPAR_OPT_DESCR_SENTINEL
,
1522 void lttng_actions_destructor(void *p
)
1524 struct lttng_action
*action
= p
;
1526 lttng_action_destroy(action
);
1529 int cmd_add_trigger(int argc
, const char **argv
)
1532 int my_argc
= argc
- 1;
1533 const char **my_argv
= argv
+ 1;
1534 struct lttng_condition
*condition
= NULL
;
1535 struct lttng_dynamic_pointer_array actions
;
1536 struct argpar_state
*argpar_state
= NULL
;
1537 struct argpar_item
*argpar_item
= NULL
;
1538 struct lttng_action
*action_group
= NULL
;
1539 struct lttng_action
*action
= NULL
;
1540 struct lttng_trigger
*trigger
= NULL
;
1544 char *fire_once_after_str
= NULL
;
1545 char *fire_every_str
= NULL
;
1546 char *user_id
= NULL
;
1548 lttng_dynamic_pointer_array_init(&actions
, lttng_actions_destructor
);
1551 enum argpar_state_parse_next_status status
;
1552 const struct argpar_item_opt
*item_opt
;
1555 argpar_state_destroy(argpar_state
);
1556 argpar_state
= argpar_state_create(my_argc
, my_argv
,
1557 add_trigger_options
);
1558 if (!argpar_state
) {
1559 ERR("Failed to create argpar state.");
1563 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item
);
1564 status
= argpar_state_parse_next(argpar_state
, &argpar_item
, &error
);
1565 if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR
) {
1568 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT
) {
1571 } else if (status
== ARGPAR_STATE_PARSE_NEXT_STATUS_END
) {
1575 assert(status
== ARGPAR_STATE_PARSE_NEXT_STATUS_OK
);
1577 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
1578 const struct argpar_item_non_opt
*item_non_opt
=
1579 (const struct argpar_item_non_opt
*)
1582 ERR("Unexpected argument `%s`.", item_non_opt
->arg
);
1586 item_opt
= (const struct argpar_item_opt
*) argpar_item
;
1588 ingested_args
= argpar_state_get_ingested_orig_args(
1591 my_argc
-= ingested_args
;
1592 my_argv
+= ingested_args
;
1594 switch (item_opt
->descr
->id
) {
1599 case OPT_LIST_OPTIONS
:
1600 list_cmd_options_argpar(stdout
, add_trigger_options
);
1606 ERR("A --condition was already given.");
1610 condition
= parse_condition(&my_argc
, &my_argv
);
1613 * An error message was already printed by
1623 action
= parse_action(&my_argc
, &my_argv
);
1626 * An error message was already printed by
1632 ret
= lttng_dynamic_pointer_array_add_pointer(
1635 ERR("Failed to add pointer to pointer array.");
1639 /* Ownership of the action was transferred to the group. */
1646 if (!assign_string(&id
, item_opt
->arg
, "--id")) {
1652 case OPT_FIRE_ONCE_AFTER
:
1654 if (!assign_string(&fire_once_after_str
, item_opt
->arg
,
1655 "--fire-once-after")) {
1661 case OPT_FIRE_EVERY
:
1663 if (!assign_string(&fire_every_str
, item_opt
->arg
,
1672 if (!assign_string(&user_id
, item_opt
->arg
,
1685 ERR("Missing --condition.");
1689 if (lttng_dynamic_pointer_array_get_count(&actions
) == 0) {
1690 ERR("Need at least one --action.");
1694 if (fire_every_str
&& fire_once_after_str
) {
1695 ERR("Can't specify both --fire-once-after and --fire-every.");
1699 action_group
= lttng_action_group_create();
1700 if (!action_group
) {
1704 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&actions
); i
++) {
1705 enum lttng_action_status status
;
1707 action
= lttng_dynamic_pointer_array_steal_pointer(&actions
, i
);
1709 status
= lttng_action_group_add_action(action_group
, action
);
1710 if (status
!= LTTNG_ACTION_STATUS_OK
) {
1715 * The `lttng_action_group_add_action()` takes a reference to
1716 * the action. We can destroy ours.
1718 lttng_action_destroy(action
);
1722 trigger
= lttng_trigger_create(condition
, action_group
);
1728 enum lttng_trigger_status trigger_status
=
1729 lttng_trigger_set_name(trigger
, id
);
1731 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1732 ERR("Failed to set trigger id.");
1737 if (fire_once_after_str
) {
1738 unsigned long long threshold
;
1739 enum lttng_trigger_status trigger_status
;
1741 if (utils_parse_unsigned_long_long(fire_once_after_str
, &threshold
) != 0) {
1742 ERR("Failed to parse `%s` as an integer.", fire_once_after_str
);
1746 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
1747 LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
,
1749 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1750 ERR("Failed to set trigger's policy to `fire once after N`.");
1755 if (fire_every_str
) {
1756 unsigned long long threshold
;
1757 enum lttng_trigger_status trigger_status
;
1759 if (utils_parse_unsigned_long_long(fire_every_str
, &threshold
) != 0) {
1760 ERR("Failed to parse `%s` as an integer.", fire_every_str
);
1764 trigger_status
= lttng_trigger_set_firing_policy(trigger
,
1765 LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
, threshold
);
1766 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1767 ERR("Failed to set trigger's policy to `fire every N`.");
1773 enum lttng_trigger_status trigger_status
;
1778 uid
= strtol(user_id
, &end
, 10);
1779 if (end
== user_id
|| *end
!= '\0' || errno
!= 0) {
1780 ERR("Failed to parse `%s` as a user id.", user_id
);
1783 trigger_status
= lttng_trigger_set_owner_uid(trigger
, uid
);
1784 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
1785 ERR("Failed to set trigger's user identity.");
1790 ret
= lttng_register_trigger(trigger
);
1792 ERR("Failed to register trigger: %s.", lttng_strerror(ret
));
1796 MSG("Trigger registered successfully.");
1804 argpar_state_destroy(argpar_state
);
1805 argpar_item_destroy(argpar_item
);
1806 lttng_dynamic_pointer_array_reset(&actions
);
1807 lttng_condition_destroy(condition
);
1808 lttng_action_destroy(action_group
);
1809 lttng_action_destroy(action
);
1810 lttng_trigger_destroy(trigger
);
1813 free(fire_once_after_str
);
1814 free(fire_every_str
);