2 * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include "../command.h"
12 #include "common/argpar/argpar.h"
13 #include "common/dynamic-array.h"
14 #include "common/mi-lttng.h"
15 /* For lttng_condition_type_str(). */
16 #include "lttng/condition/condition-internal.h"
17 /* For lttng_domain_type_str(). */
18 #include "lttng/domain-internal.h"
19 #include "../loglevel.h"
21 #ifdef LTTNG_EMBED_HELP
22 static const char help_msg
[] =
23 #include <lttng-list-triggers.1.h>
33 struct argpar_opt_descr list_trigger_options
[] = {
34 { OPT_HELP
, 'h', "help", false },
35 { OPT_LIST_OPTIONS
, '\0', "list-options", false },
36 ARGPAR_OPT_DESCR_SENTINEL
,
40 * Returns the human-readable log level name associated with a numerical value
41 * if there is one. The Log4j and JUL domains have discontinuous log level
42 * values (a value can fall between two labels). In those cases, NULL is
45 static const char *get_pretty_loglevel_name(
46 enum lttng_domain_type domain
, int loglevel
)
48 const char *name
= NULL
;
51 case LTTNG_DOMAIN_UST
:
52 name
= loglevel_value_to_name(loglevel
);
54 case LTTNG_DOMAIN_LOG4J
:
55 name
= loglevel_log4j_value_to_name(loglevel
);
57 case LTTNG_DOMAIN_JUL
:
58 name
= loglevel_jul_value_to_name(loglevel
);
60 case LTTNG_DOMAIN_PYTHON
:
61 name
= loglevel_python_value_to_name(loglevel
);
71 void print_event_rule_tracepoint(const struct lttng_event_rule
*event_rule
)
73 enum lttng_event_rule_status event_rule_status
;
74 enum lttng_domain_type domain_type
;
78 const struct lttng_log_level_rule
*log_level_rule
= NULL
;
79 unsigned int exclusions_count
;
82 event_rule_status
= lttng_event_rule_tracepoint_get_pattern(
83 event_rule
, &pattern
);
84 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
86 event_rule_status
= lttng_event_rule_tracepoint_get_domain_type(
87 event_rule
, &domain_type
);
88 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
90 _MSG(" rule: %s (type: tracepoint, domain: %s", pattern
,
91 lttng_domain_type_str(domain_type
));
93 event_rule_status
= lttng_event_rule_tracepoint_get_filter(
95 if (event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
96 _MSG(", filter: %s", filter
);
98 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_UNSET
);
101 event_rule_status
= lttng_event_rule_tracepoint_get_log_level_rule(
102 event_rule
, &log_level_rule
);
103 if (event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
104 enum lttng_log_level_rule_status llr_status
;
105 const char *log_level_op
;
106 const char *pretty_loglevel_name
;
108 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
109 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
111 llr_status
= lttng_log_level_rule_exactly_get_level(
112 log_level_rule
, &log_level
);
114 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
115 log_level_op
= "at least";
116 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
117 log_level_rule
, &log_level
);
123 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
125 pretty_loglevel_name
= get_pretty_loglevel_name(
126 domain_type
, log_level
);
127 if (pretty_loglevel_name
) {
128 _MSG(", log level %s %s", log_level_op
,
129 pretty_loglevel_name
);
131 _MSG(", log level %s %d", log_level_op
, log_level
);
134 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_UNSET
);
137 event_rule_status
= lttng_event_rule_tracepoint_get_exclusions_count(
138 event_rule
, &exclusions_count
);
139 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
140 if (exclusions_count
> 0) {
141 _MSG(", exclusions: ");
142 for (i
= 0; i
< exclusions_count
; i
++) {
143 const char *exclusion
;
145 event_rule_status
= lttng_event_rule_tracepoint_get_exclusion_at_index(
146 event_rule
, i
, &exclusion
);
147 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
149 _MSG("%s%s", i
> 0 ? "," : "", exclusion
);
156 static void print_kernel_probe_location(
157 const struct lttng_kernel_probe_location
*location
)
159 enum lttng_kernel_probe_location_status status
;
160 switch (lttng_kernel_probe_location_get_type(location
)) {
161 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
165 status
= lttng_kernel_probe_location_address_get_address(
167 if (status
!= LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
) {
168 ERR("Getting kernel probe location address failed.");
172 _MSG("0x%" PRIx64
, address
);
176 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
179 const char *symbol_name
;
181 symbol_name
= lttng_kernel_probe_location_symbol_get_name(
184 ERR("Getting kernel probe location symbol name failed.");
188 status
= lttng_kernel_probe_location_symbol_get_offset(
190 if (status
!= LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
) {
191 ERR("Getting kernel probe location address failed.");
196 _MSG("%s", symbol_name
);
198 _MSG("%s+0x%" PRIx64
, symbol_name
, offset
);
211 void print_event_rule_kernel_probe(const struct lttng_event_rule
*event_rule
)
213 enum lttng_event_rule_status event_rule_status
;
215 const struct lttng_kernel_probe_location
*location
;
217 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
);
219 event_rule_status
= lttng_event_rule_kernel_probe_get_event_name(event_rule
, &name
);
220 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
221 ERR("Failed to get kprobe event rule's name.");
225 event_rule_status
= lttng_event_rule_kernel_probe_get_location(
226 event_rule
, &location
);
227 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
228 ERR("Failed to get kprobe event rule's location.");
232 _MSG(" rule: %s (type: probe, location: ", name
);
234 print_kernel_probe_location(location
);
243 void print_event_rule_userspace_probe(const struct lttng_event_rule
*event_rule
)
245 enum lttng_event_rule_status event_rule_status
;
247 const struct lttng_userspace_probe_location
*location
;
248 enum lttng_userspace_probe_location_type userspace_probe_location_type
;
250 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
);
252 event_rule_status
= lttng_event_rule_userspace_probe_get_event_name(
254 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
255 ERR("Failed to get uprobe event rule's name.");
259 event_rule_status
= lttng_event_rule_userspace_probe_get_location(
260 event_rule
, &location
);
261 if (event_rule_status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
262 ERR("Failed to get uprobe event rule's location.");
266 _MSG(" rule: %s (type: userspace probe, location: ", name
);
268 userspace_probe_location_type
=
269 lttng_userspace_probe_location_get_type(location
);
271 switch (userspace_probe_location_type
) {
272 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
274 const char *binary_path
, *function_name
;
276 binary_path
= lttng_userspace_probe_location_function_get_binary_path(
278 function_name
= lttng_userspace_probe_location_function_get_function_name(
281 _MSG("%s:%s", binary_path
, function_name
);
284 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
285 _MSG("SDT not implemented yet");
298 void print_event_rule_syscall(const struct lttng_event_rule
*event_rule
)
300 const char *pattern
, *filter
;
301 enum lttng_event_rule_status event_rule_status
;
303 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_SYSCALL
);
305 event_rule_status
= lttng_event_rule_syscall_get_pattern(
306 event_rule
, &pattern
);
307 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
);
309 _MSG(" rule: %s (type: syscall", pattern
);
311 event_rule_status
= lttng_event_rule_syscall_get_filter(
312 event_rule
, &filter
);
313 if (event_rule_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
314 _MSG(", filter: %s", filter
);
316 assert(event_rule_status
== LTTNG_EVENT_RULE_STATUS_UNSET
);
323 void print_event_rule(const struct lttng_event_rule
*event_rule
)
325 const enum lttng_event_rule_type event_rule_type
=
326 lttng_event_rule_get_type(event_rule
);
328 switch (event_rule_type
) {
329 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
330 print_event_rule_tracepoint(event_rule
);
332 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
333 print_event_rule_kernel_probe(event_rule
);
335 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
336 print_event_rule_userspace_probe(event_rule
);
338 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
339 print_event_rule_syscall(event_rule
);
347 void print_one_event_expr(const struct lttng_event_expr
*event_expr
)
349 enum lttng_event_expr_type type
;
351 type
= lttng_event_expr_get_type(event_expr
);
354 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
358 name
= lttng_event_expr_event_payload_field_get_name(
364 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
368 name
= lttng_event_expr_channel_context_field_get_name(
370 _MSG("$ctx.%s", name
);
374 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
376 const char *provider_name
;
377 const char *type_name
;
379 provider_name
= lttng_event_expr_app_specific_context_field_get_provider_name(
381 type_name
= lttng_event_expr_app_specific_context_field_get_type_name(
384 _MSG("$app.%s:%s", provider_name
, type_name
);
388 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
391 const struct lttng_event_expr
*parent_expr
;
392 enum lttng_event_expr_status status
;
394 parent_expr
= lttng_event_expr_array_field_element_get_parent_expr(
396 assert(parent_expr
!= NULL
);
398 print_one_event_expr(parent_expr
);
400 status
= lttng_event_expr_array_field_element_get_index(
402 assert(status
== LTTNG_EVENT_EXPR_STATUS_OK
);
414 void print_condition_on_event(const struct lttng_condition
*condition
)
416 const struct lttng_event_rule
*event_rule
;
417 enum lttng_condition_status condition_status
;
418 unsigned int cap_desc_count
, i
;
421 lttng_condition_on_event_get_rule(condition
, &event_rule
);
422 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
424 print_event_rule(event_rule
);
427 lttng_condition_on_event_get_capture_descriptor_count(
428 condition
, &cap_desc_count
);
429 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
431 if (cap_desc_count
> 0) {
434 for (i
= 0; i
< cap_desc_count
; i
++) {
435 const struct lttng_event_expr
*cap_desc
=
436 lttng_condition_on_event_get_capture_descriptor_at_index(
440 print_one_event_expr(cap_desc
);
447 void print_one_action(const struct lttng_action
*action
)
449 enum lttng_action_type action_type
;
450 enum lttng_action_status action_status
;
453 action_type
= lttng_action_get_type(action
);
454 assert(action_type
!= LTTNG_ACTION_TYPE_GROUP
);
456 switch (action_type
) {
457 case LTTNG_ACTION_TYPE_NOTIFY
:
460 case LTTNG_ACTION_TYPE_START_SESSION
:
461 action_status
= lttng_action_start_session_get_session_name(
463 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
464 MSG("start session `%s`", value
);
466 case LTTNG_ACTION_TYPE_STOP_SESSION
:
467 action_status
= lttng_action_stop_session_get_session_name(
469 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
470 MSG("stop session `%s`", value
);
472 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
473 action_status
= lttng_action_rotate_session_get_session_name(
475 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
476 MSG("rotate session `%s`", value
);
478 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
480 const struct lttng_snapshot_output
*output
;
482 action_status
= lttng_action_snapshot_session_get_session_name(
484 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
485 _MSG("snapshot session `%s`", value
);
487 action_status
= lttng_action_snapshot_session_get_output(
489 if (action_status
== LTTNG_ACTION_STATUS_OK
) {
492 const char *ctrl_url
, *data_url
;
493 bool starts_with_file
, starts_with_net
, starts_with_net6
;
495 ctrl_url
= lttng_snapshot_output_get_ctrl_url(output
);
496 assert(ctrl_url
&& strlen(ctrl_url
) > 0);
498 data_url
= lttng_snapshot_output_get_data_url(output
);
501 starts_with_file
= strncmp(ctrl_url
, "file://", strlen("file://")) == 0;
502 starts_with_net
= strncmp(ctrl_url
, "net://", strlen("net://")) == 0;
503 starts_with_net6
= strncmp(ctrl_url
, "net6://", strlen("net6://")) == 0;
505 if (ctrl_url
[0] == '/' || starts_with_file
) {
506 if (starts_with_file
) {
507 ctrl_url
+= strlen("file://");
510 _MSG(", path: %s", ctrl_url
);
511 } else if (starts_with_net
|| starts_with_net6
) {
512 _MSG(", url: %s", ctrl_url
);
514 assert(strlen(data_url
) > 0);
516 _MSG(", control url: %s, data url: %s", ctrl_url
, data_url
);
519 name
= lttng_snapshot_output_get_name(output
);
521 if (strlen(name
) > 0) {
522 _MSG(", name: %s", name
);
525 max_size
= lttng_snapshot_output_get_maxsize(output
);
526 if (max_size
!= -1ULL) {
527 _MSG(", max size: %" PRIu64
, max_size
);
541 void print_one_trigger(const struct lttng_trigger
*trigger
)
543 const struct lttng_condition
*condition
;
544 enum lttng_condition_type condition_type
;
545 const struct lttng_action
*action
;
546 enum lttng_action_type action_type
;
547 enum lttng_trigger_status trigger_status
;
549 enum lttng_trigger_firing_policy firing_policy_type
;
553 trigger_status
= lttng_trigger_get_name(trigger
, &name
);
554 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
556 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_uid
);
557 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
559 MSG("- id: %s", name
);
560 MSG(" user id: %d", trigger_uid
);
562 trigger_status
= lttng_trigger_get_firing_policy(
563 trigger
, &firing_policy_type
, &threshold
);
564 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
565 ERR("Failed to get trigger's policy.");
569 switch (firing_policy_type
) {
570 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N
:
572 MSG(" firing policy: after every %" PRIu64
" occurences", threshold
);
575 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N
:
576 MSG(" firing policy: once after %" PRIu64
" occurences", threshold
);
582 condition
= lttng_trigger_get_const_condition(trigger
);
583 condition_type
= lttng_condition_get_type(condition
);
584 MSG(" condition: %s", lttng_condition_type_str(condition_type
));
585 switch (condition_type
) {
586 case LTTNG_CONDITION_TYPE_ON_EVENT
:
587 print_condition_on_event(condition
);
590 MSG(" (condition type not handled in %s)", __func__
);
594 action
= lttng_trigger_get_const_action(trigger
);
595 action_type
= lttng_action_get_type(action
);
596 if (action_type
== LTTNG_ACTION_TYPE_GROUP
) {
597 unsigned int count
, i
;
598 enum lttng_action_status action_status
;
602 action_status
= lttng_action_group_get_count(action
, &count
);
603 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
605 for (i
= 0; i
< count
; i
++) {
606 const struct lttng_action
*subaction
=
607 lttng_action_group_get_at_index(
611 print_one_action(subaction
);
615 print_one_action(action
);
623 int compare_triggers_by_name(const void *a
, const void *b
)
625 const struct lttng_trigger
*trigger_a
= *((const struct lttng_trigger
**) a
);
626 const struct lttng_trigger
*trigger_b
= *((const struct lttng_trigger
**) b
);
627 const char *name_a
, *name_b
;
628 enum lttng_trigger_status trigger_status
;
630 trigger_status
= lttng_trigger_get_name(trigger_a
, &name_a
);
631 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
633 trigger_status
= lttng_trigger_get_name(trigger_b
, &name_b
);
634 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
636 return strcmp(name_a
, name_b
);
639 int cmd_list_triggers(int argc
, const char **argv
)
642 struct argpar_parse_ret argpar_parse_ret
= {};
643 struct lttng_triggers
*triggers
= NULL
;
645 struct lttng_dynamic_pointer_array sorted_triggers
;
646 enum lttng_trigger_status trigger_status
;
647 unsigned int num_triggers
;
649 lttng_dynamic_pointer_array_init(&sorted_triggers
, NULL
);
651 argpar_parse_ret
= argpar_parse(
652 argc
- 1, argv
+ 1, list_trigger_options
, true);
653 if (!argpar_parse_ret
.items
) {
654 ERR("%s", argpar_parse_ret
.error
);
658 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
659 const struct argpar_item
*item
=
660 argpar_parse_ret
.items
->items
[i
];
662 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
663 const struct argpar_item_opt
*item_opt
=
664 (const struct argpar_item_opt
*) item
;
666 switch (item_opt
->descr
->id
) {
672 case OPT_LIST_OPTIONS
:
673 list_cmd_options_argpar(stdout
,
674 list_trigger_options
);
683 const struct argpar_item_non_opt
*item_non_opt
=
684 (const struct argpar_item_non_opt
*) item
;
686 ERR("Unexpected argument: %s", item_non_opt
->arg
);
690 ret
= lttng_list_triggers(&triggers
);
691 if (ret
!= LTTNG_OK
) {
692 ERR("Error listing triggers: %s.", lttng_strerror(-ret
));
696 trigger_status
= lttng_triggers_get_count(triggers
, &num_triggers
);
697 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
698 ERR("Failed to get trigger count.");
702 for (i
= 0; i
< num_triggers
; i
++) {
703 const int add_ret
= lttng_dynamic_pointer_array_add_pointer(
705 (void *) lttng_triggers_get_at_index(triggers
, i
));
708 ERR("Failed to allocate array of struct lttng_trigger *.");
713 qsort(sorted_triggers
.array
.buffer
.data
, num_triggers
,
714 sizeof(struct lttng_trigger
*),
715 compare_triggers_by_name
);
717 for (i
= 0; i
< num_triggers
; i
++) {
718 const struct lttng_trigger
*trigger_to_print
=
719 (const struct lttng_trigger
*)
720 lttng_dynamic_pointer_array_get_pointer(
721 &sorted_triggers
, i
);
723 print_one_trigger(trigger_to_print
);
733 argpar_parse_ret_fini(&argpar_parse_ret
);
734 lttng_triggers_destroy(triggers
);
735 lttng_dynamic_pointer_array_reset(&sorted_triggers
);