From 0de2479d5f5a287d31c69e34ea5202a69019f224 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 20 Jan 2020 17:25:19 -0500 Subject: [PATCH] lttng: Add list-triggers command MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Simon Marchi Signed-off-by: Jérémie Galarneau Change-Id: I901264798baa81626d8e212a40774570100dc4f9 Depends-on: lttng-ust: I5a800fc92e588c2a6a0e26282b0ad5f31c044479 --- doc/man/Makefile.am | 3 +- doc/man/lttng-list-triggers.1.txt | 37 ++ include/lttng/condition/condition-internal.h | 3 + include/lttng/snapshot.h | 10 +- include/lttng/trigger/trigger-internal.h | 8 - include/lttng/trigger/trigger.h | 5 + src/bin/lttng/Makefile.am | 1 + src/bin/lttng/command.h | 1 + src/bin/lttng/commands/list_triggers.c | 602 ++++++++++++++++++ src/bin/lttng/lttng.c | 1 + src/common/conditions/condition.c | 30 + src/common/trigger.c | 6 +- src/lib/lttng-ctl/snapshot.c | 10 +- tests/regression/Makefile.am | 3 +- tests/regression/tools/trigger/Makefile.am | 6 +- .../tools/trigger/test_list_triggers_cli | 335 ++++++++++ 16 files changed, 1035 insertions(+), 26 deletions(-) create mode 100644 doc/man/lttng-list-triggers.1.txt create mode 100644 src/bin/lttng/commands/list_triggers.c create mode 100755 tests/regression/tools/trigger/test_list_triggers_cli diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am index 59c0ca083..769dca470 100644 --- a/doc/man/Makefile.am +++ b/doc/man/Makefile.am @@ -37,7 +37,8 @@ MAN1_NAMES = \ lttng-enable-rotation \ lttng-disable-rotation \ lttng-clear \ - lttng-add-trigger + lttng-add-trigger \ + lttng-list-triggers MAN3_NAMES = MAN8_NAMES = lttng-sessiond lttng-relayd diff --git a/doc/man/lttng-list-triggers.1.txt b/doc/man/lttng-list-triggers.1.txt new file mode 100644 index 000000000..2ab3085cd --- /dev/null +++ b/doc/man/lttng-list-triggers.1.txt @@ -0,0 +1,37 @@ +lttng-list-triggers(1) +====================== +:revdate: 20 January 2020 + + +NAME +---- +lttng-list-triggers - List LTTng triggers + + +SYNOPSIS +-------- + +[verse] +*lttng* ['linkgenoptions:(GENERAL OPTIONS)'] *list-triggers* + + +DESCRIPTION +----------- + +The `lttng list-triggers` command list the existing triggers. + + +OPTIONS +------- + +include::common-cmd-help-options.txt[] + + +include::common-cmd-footer.txt[] + + +SEE ALSO +-------- +man:lttng-add-trigger(1), +man:lttng-remove-trigger(1), +man:lttng(1) diff --git a/include/lttng/condition/condition-internal.h b/include/lttng/condition/condition-internal.h index 405199b42..92bb4de71 100644 --- a/include/lttng/condition/condition-internal.h +++ b/include/lttng/condition/condition-internal.h @@ -71,4 +71,7 @@ LTTNG_HIDDEN bool lttng_condition_is_equal(const struct lttng_condition *a, const struct lttng_condition *b); +LTTNG_HIDDEN +const char *lttng_condition_type_str(enum lttng_condition_type type); + #endif /* LTTNG_CONDITION_INTERNAL_H */ diff --git a/include/lttng/snapshot.h b/include/lttng/snapshot.h index 24f165287..e674c0c7b 100644 --- a/include/lttng/snapshot.h +++ b/include/lttng/snapshot.h @@ -39,15 +39,15 @@ void lttng_snapshot_output_destroy(struct lttng_snapshot_output *output); */ /* Return snapshot ID. */ -uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output *output); +uint32_t lttng_snapshot_output_get_id(const struct lttng_snapshot_output *output); /* Return maximum size of a snapshot. */ -uint64_t lttng_snapshot_output_get_maxsize(struct lttng_snapshot_output *output); +uint64_t lttng_snapshot_output_get_maxsize(const struct lttng_snapshot_output *output); /* Return snapshot name. */ -const char *lttng_snapshot_output_get_name(struct lttng_snapshot_output *output); +const char *lttng_snapshot_output_get_name(const struct lttng_snapshot_output *output); /* Return snapshot control URL in a text format. */ -const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output *output); +const char *lttng_snapshot_output_get_ctrl_url(const struct lttng_snapshot_output *output); /* Return snapshot data URL in a text format. */ -const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output *output); +const char *lttng_snapshot_output_get_data_url(const struct lttng_snapshot_output *output); /* * Snapshot output setter family functions. diff --git a/include/lttng/trigger/trigger-internal.h b/include/lttng/trigger/trigger-internal.h index 3b760e167..20989bd99 100644 --- a/include/lttng/trigger/trigger-internal.h +++ b/include/lttng/trigger/trigger-internal.h @@ -83,14 +83,6 @@ LTTNG_HIDDEN int lttng_trigger_serialize(const struct lttng_trigger *trigger, struct lttng_payload *payload); -LTTNG_HIDDEN -const struct lttng_condition *lttng_trigger_get_const_condition( - const struct lttng_trigger *trigger); - -LTTNG_HIDDEN -const struct lttng_action *lttng_trigger_get_const_action( - const struct lttng_trigger *trigger); - LTTNG_HIDDEN bool lttng_trigger_validate(struct lttng_trigger *trigger); diff --git a/include/lttng/trigger/trigger.h b/include/lttng/trigger/trigger.h index b36ee13a2..2a7d4e36b 100644 --- a/include/lttng/trigger/trigger.h +++ b/include/lttng/trigger/trigger.h @@ -94,6 +94,9 @@ extern enum lttng_trigger_status lttng_trigger_get_owner_uid( extern struct lttng_condition *lttng_trigger_get_condition( struct lttng_trigger *trigger); +const struct lttng_condition *lttng_trigger_get_const_condition( + const struct lttng_trigger *trigger); + /* * Get the action of a trigger. * @@ -104,6 +107,8 @@ extern struct lttng_condition *lttng_trigger_get_condition( extern struct lttng_action *lttng_trigger_get_action( struct lttng_trigger *trigger); +const struct lttng_action *lttng_trigger_get_const_action( + const struct lttng_trigger *trigger); /* * Get the name of a trigger. diff --git a/src/bin/lttng/Makefile.am b/src/bin/lttng/Makefile.am index 83d078b22..7a305d49a 100644 --- a/src/bin/lttng/Makefile.am +++ b/src/bin/lttng/Makefile.am @@ -31,6 +31,7 @@ lttng_SOURCES = command.h conf.c conf.h commands/start.c \ commands/clear.c \ loglevel.c loglevel.h \ commands/add_trigger.c \ + commands/list_triggers.c \ utils.c utils.h lttng.c \ uprobe.c uprobe.h diff --git a/src/bin/lttng/command.h b/src/bin/lttng/command.h index 8961bad01..8a318c6f7 100644 --- a/src/bin/lttng/command.h +++ b/src/bin/lttng/command.h @@ -78,6 +78,7 @@ DECL_COMMAND(enable_rotation); DECL_COMMAND(disable_rotation); DECL_COMMAND(clear); DECL_COMMAND(add_trigger); +DECL_COMMAND(list_triggers); extern int cmd_help(int argc, const char **argv, const struct cmd_struct commands[]); diff --git a/src/bin/lttng/commands/list_triggers.c b/src/bin/lttng/commands/list_triggers.c new file mode 100644 index 000000000..ac6e1ba35 --- /dev/null +++ b/src/bin/lttng/commands/list_triggers.c @@ -0,0 +1,602 @@ +/* + * Copyright (C) 2021 Simon Marchi + * + * SPDX-License-Identifier: GPL-2.0-only + * + */ + +#include + +#include "../command.h" + +#include "common/argpar/argpar.h" +#include "common/dynamic-array.h" +#include "common/mi-lttng.h" +/* For lttng_condition_type_str(). */ +#include "lttng/condition/condition-internal.h" +/* For lttng_domain_type_str(). */ +#include "lttng/domain-internal.h" + +#ifdef LTTNG_EMBED_HELP +static const char help_msg[] = +#include +; +#endif + +enum { + OPT_HELP, + OPT_LIST_OPTIONS, +}; + +static const +struct argpar_opt_descr list_trigger_options[] = { + { OPT_HELP, 'h', "help", false }, + { OPT_LIST_OPTIONS, '\0', "list-options", false }, + ARGPAR_OPT_DESCR_SENTINEL, +}; + +static +void print_event_rule_tracepoint(const struct lttng_event_rule *event_rule) +{ + enum lttng_event_rule_status event_rule_status; + enum lttng_domain_type domain_type; + const char *pattern; + const char *filter; + int log_level; + unsigned int exclusions_count; + int i; + + event_rule_status = lttng_event_rule_tracepoint_get_pattern( + event_rule, &pattern); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + + event_rule_status = lttng_event_rule_tracepoint_get_domain_type( + event_rule, &domain_type); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + + _MSG(" rule: %s (type: tracepoint, domain: %s", pattern, + lttng_domain_type_str(domain_type)); + + event_rule_status = lttng_event_rule_tracepoint_get_filter( + event_rule, &filter); + if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) { + _MSG(", filter: %s", filter); + } else { + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET); + } + + event_rule_status = lttng_event_rule_tracepoint_get_log_level( + event_rule, &log_level); + if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) { + enum lttng_loglevel_type log_level_type; + const char *log_level_op; + + event_rule_status = lttng_event_rule_tracepoint_get_log_level_type( + event_rule, &log_level_type); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + assert(log_level_type == LTTNG_EVENT_LOGLEVEL_RANGE || + log_level_type == LTTNG_EVENT_LOGLEVEL_SINGLE); + + log_level_op = (log_level_type == LTTNG_EVENT_LOGLEVEL_RANGE ? "<=" : "=="); + + _MSG(", log level %s %s", log_level_op, + mi_lttng_loglevel_string( + log_level, domain_type)); + } else { + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET); + } + + event_rule_status = lttng_event_rule_tracepoint_get_exclusions_count( + event_rule, &exclusions_count); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + if (exclusions_count > 0) { + _MSG(", exclusions: "); + for (i = 0; i < exclusions_count; i++) { + const char *exclusion; + + event_rule_status = lttng_event_rule_tracepoint_get_exclusion_at_index( + event_rule, i, &exclusion); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + + _MSG("%s%s", i > 0 ? "," : "", exclusion); + } + } + + MSG(")"); +} + +static void print_kernel_probe_location( + const struct lttng_kernel_probe_location *location) +{ + enum lttng_kernel_probe_location_status status; + switch (lttng_kernel_probe_location_get_type(location)) { + case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS: + { + uint64_t address; + + status = lttng_kernel_probe_location_address_get_address( + location, &address); + if (status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) { + ERR("Getting kernel probe location address failed."); + goto end; + } + + _MSG("0x%" PRIx64, address); + + break; + } + case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET: + { + uint64_t offset; + const char *symbol_name; + + symbol_name = lttng_kernel_probe_location_symbol_get_name( + location); + if (!symbol_name) { + ERR("Getting kernel probe location symbol name failed."); + goto end; + } + + status = lttng_kernel_probe_location_symbol_get_offset( + location, &offset); + if (status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) { + ERR("Getting kernel probe location address failed."); + goto end; + } + + if (offset == 0) { + _MSG("%s", symbol_name); + } else { + _MSG("%s+0x%" PRIx64, symbol_name, offset); + } + + break; + } + default: + abort(); + }; +end: + return; +} + +static +void print_event_rule_kprobe(const struct lttng_event_rule *event_rule) +{ + enum lttng_event_rule_status event_rule_status; + const char *name; + const struct lttng_kernel_probe_location *location; + + assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_KPROBE); + + event_rule_status = lttng_event_rule_kprobe_get_name(event_rule, &name); + if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { + ERR("Failed to get kprobe event rule's name."); + goto end; + } + + event_rule_status = lttng_event_rule_kprobe_get_location( + event_rule, &location); + if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { + ERR("Failed to get kprobe event rule's location."); + goto end; + } + + _MSG(" rule: %s (type: probe, location: ", name); + + print_kernel_probe_location(location); + + MSG(")"); + +end: + return; +} + +static +void print_event_rule_uprobe(const struct lttng_event_rule *event_rule) +{ + enum lttng_event_rule_status event_rule_status; + const char *name; + const struct lttng_userspace_probe_location *location; + enum lttng_userspace_probe_location_type userspace_probe_location_type; + + assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_UPROBE); + + event_rule_status = lttng_event_rule_uprobe_get_name(event_rule, &name); + if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { + ERR("Failed to get uprobe event rule's name."); + goto end; + } + + event_rule_status = lttng_event_rule_uprobe_get_location( + event_rule, &location); + if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { + ERR("Failed to get uprobe event rule's location."); + goto end; + } + + _MSG(" rule: %s (type: userspace probe, location: ", name); + + userspace_probe_location_type = + lttng_userspace_probe_location_get_type(location); + + switch (userspace_probe_location_type) { + case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION: + { + const char *binary_path, *function_name; + + binary_path = lttng_userspace_probe_location_function_get_binary_path( + location); + function_name = lttng_userspace_probe_location_function_get_function_name( + location); + + _MSG("%s:%s", binary_path, function_name); + break; + } + case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT: + _MSG("SDT not implemented yet"); + break; + default: + abort(); + } + + MSG(")"); + +end: + return; +} + +static +void print_event_rule_syscall(const struct lttng_event_rule *event_rule) +{ + const char *pattern, *filter; + enum lttng_event_rule_status event_rule_status; + + assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_SYSCALL); + + event_rule_status = lttng_event_rule_syscall_get_pattern( + event_rule, &pattern); + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); + + _MSG(" rule: %s (type: syscall", pattern); + + event_rule_status = lttng_event_rule_syscall_get_filter( + event_rule, &filter); + if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) { + _MSG(", filter: %s", filter); + } else { + assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET); + } + + MSG(")"); +} + +static +void print_event_rule(const struct lttng_event_rule *event_rule) +{ + const enum lttng_event_rule_type event_rule_type = + lttng_event_rule_get_type(event_rule); + + switch (event_rule_type) { + case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: + print_event_rule_tracepoint(event_rule); + break; + case LTTNG_EVENT_RULE_TYPE_KPROBE: + print_event_rule_kprobe(event_rule); + break; + case LTTNG_EVENT_RULE_TYPE_UPROBE: + print_event_rule_uprobe(event_rule); + break; + case LTTNG_EVENT_RULE_TYPE_SYSCALL: + print_event_rule_syscall(event_rule); + break; + default: + abort(); + } +} + +static +void print_condition_event_rule_hit(const struct lttng_condition *condition) +{ + const struct lttng_event_rule *event_rule; + enum lttng_condition_status condition_status; + + condition_status = + lttng_condition_event_rule_get_rule(condition, &event_rule); + assert(condition_status == LTTNG_CONDITION_STATUS_OK); + + print_event_rule(event_rule); +} + +static +void print_one_action(const struct lttng_action *action) +{ + enum lttng_action_type action_type; + enum lttng_action_status action_status; + const char *value; + + action_type = lttng_action_get_type(action); + assert(action_type != LTTNG_ACTION_TYPE_GROUP); + + switch (action_type) { + case LTTNG_ACTION_TYPE_NOTIFY: + MSG("notify"); + break; + case LTTNG_ACTION_TYPE_START_SESSION: + action_status = lttng_action_start_session_get_session_name( + action, &value); + assert(action_status == LTTNG_ACTION_STATUS_OK); + MSG("start session `%s`", value); + break; + case LTTNG_ACTION_TYPE_STOP_SESSION: + action_status = lttng_action_stop_session_get_session_name( + action, &value); + assert(action_status == LTTNG_ACTION_STATUS_OK); + MSG("stop session `%s`", value); + break; + case LTTNG_ACTION_TYPE_ROTATE_SESSION: + action_status = lttng_action_rotate_session_get_session_name( + action, &value); + assert(action_status == LTTNG_ACTION_STATUS_OK); + MSG("rotate session `%s`", value); + break; + case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: + { + const struct lttng_snapshot_output *output; + + action_status = lttng_action_snapshot_session_get_session_name( + action, &value); + assert(action_status == LTTNG_ACTION_STATUS_OK); + _MSG("snapshot session `%s`", value); + + action_status = lttng_action_snapshot_session_get_output( + action, &output); + if (action_status == LTTNG_ACTION_STATUS_OK) { + const char *name; + uint64_t max_size; + const char *ctrl_url, *data_url; + bool starts_with_file, starts_with_net, starts_with_net6; + + ctrl_url = lttng_snapshot_output_get_ctrl_url(output); + assert(ctrl_url && strlen(ctrl_url) > 0); + + data_url = lttng_snapshot_output_get_data_url(output); + assert(data_url); + + starts_with_file = strncmp(ctrl_url, "file://", strlen("file://")) == 0; + starts_with_net = strncmp(ctrl_url, "net://", strlen("net://")) == 0; + starts_with_net6 = strncmp(ctrl_url, "net6://", strlen("net6://")) == 0; + + if (ctrl_url[0] == '/' || starts_with_file) { + if (starts_with_file) { + ctrl_url += strlen("file://"); + } + + _MSG(", path: %s", ctrl_url); + } else if (starts_with_net || starts_with_net6) { + _MSG(", url: %s", ctrl_url); + } else { + assert(strlen(data_url) > 0); + + _MSG(", control url: %s, data url: %s", ctrl_url, data_url); + } + + name = lttng_snapshot_output_get_name(output); + assert(name); + if (strlen(name) > 0) { + _MSG(", name: %s", name); + } + + max_size = lttng_snapshot_output_get_maxsize(output); + if (max_size != -1ULL) { + _MSG(", max size: %" PRIu64, max_size); + } + } + + MSG(""); + break; + } + + default: + abort(); + } +} + +static +void print_one_trigger(const struct lttng_trigger *trigger) +{ + const struct lttng_condition *condition; + enum lttng_condition_type condition_type; + const struct lttng_action *action; + enum lttng_action_type action_type; + enum lttng_trigger_status trigger_status; + const char *name; + enum lttng_trigger_firing_policy firing_policy_type; + uint64_t threshold; + uid_t trigger_uid; + + trigger_status = lttng_trigger_get_name(trigger, &name); + assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); + + trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_uid); + assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); + + MSG("- id: %s", name); + MSG(" user id: %d", trigger_uid); + + trigger_status = lttng_trigger_get_firing_policy( + trigger, &firing_policy_type, &threshold); + if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { + ERR("Failed to get trigger's policy."); + goto end; + } + + switch (firing_policy_type) { + case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N: + if (threshold > 1) { + MSG(" firing policy: after every %" PRIu64 " occurences", threshold); + } + break; + case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N: + MSG(" firing policy: once after %" PRIu64 " occurences", threshold); + break; + default: + abort(); + } + + condition = lttng_trigger_get_const_condition(trigger); + condition_type = lttng_condition_get_type(condition); + MSG(" condition: %s", lttng_condition_type_str(condition_type)); + switch (condition_type) { + case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: + print_condition_event_rule_hit(condition); + break; + default: + MSG(" (condition type not handled in %s)", __func__); + break; + } + + action = lttng_trigger_get_const_action(trigger); + action_type = lttng_action_get_type(action); + if (action_type == LTTNG_ACTION_TYPE_GROUP) { + unsigned int count, i; + enum lttng_action_status action_status; + + MSG(" actions:"); + + action_status = lttng_action_group_get_count(action, &count); + assert(action_status == LTTNG_ACTION_STATUS_OK); + + for (i = 0; i < count; i++) { + const struct lttng_action *subaction = + lttng_action_group_get_at_index( + action, i); + + _MSG(" "); + print_one_action(subaction); + } + } else { + _MSG(" action:"); + print_one_action(action); + } + +end: + return; +} + +static +int compare_triggers_by_name(const void *a, const void *b) +{ + const struct lttng_trigger *trigger_a = *((const struct lttng_trigger **) a); + const struct lttng_trigger *trigger_b = *((const struct lttng_trigger **) b); + const char *name_a, *name_b; + enum lttng_trigger_status trigger_status; + + trigger_status = lttng_trigger_get_name(trigger_a, &name_a); + assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); + + trigger_status = lttng_trigger_get_name(trigger_b, &name_b); + assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); + + return strcmp(name_a, name_b); +} + +int cmd_list_triggers(int argc, const char **argv) +{ + int ret; + struct argpar_parse_ret argpar_parse_ret = {}; + struct lttng_triggers *triggers = NULL; + int i; + struct lttng_dynamic_pointer_array sorted_triggers; + enum lttng_trigger_status trigger_status; + unsigned int num_triggers; + + lttng_dynamic_pointer_array_init(&sorted_triggers, NULL); + + argpar_parse_ret = argpar_parse( + argc - 1, argv + 1, list_trigger_options, true); + if (!argpar_parse_ret.items) { + ERR("%s", argpar_parse_ret.error); + goto error; + } + + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + const struct argpar_item *item = + argpar_parse_ret.items->items[i]; + + if (item->type == ARGPAR_ITEM_TYPE_OPT) { + const struct argpar_item_opt *item_opt = + (const struct argpar_item_opt *) item; + + switch (item_opt->descr->id) { + case OPT_HELP: + SHOW_HELP(); + ret = 0; + goto end; + + case OPT_LIST_OPTIONS: + list_cmd_options_argpar(stdout, + list_trigger_options); + ret = 0; + goto end; + + default: + abort(); + } + + } else { + const struct argpar_item_non_opt *item_non_opt = + (const struct argpar_item_non_opt *) item; + + ERR("Unexpected argument: %s", item_non_opt->arg); + } + } + + ret = lttng_list_triggers(&triggers); + if (ret != LTTNG_OK) { + ERR("Error listing triggers: %s.", lttng_strerror(-ret)); + goto error; + } + + trigger_status = lttng_triggers_get_count(triggers, &num_triggers); + if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { + ERR("Failed to get trigger count."); + goto error; + } + + for (i = 0; i < num_triggers; i++) { + const int add_ret = lttng_dynamic_pointer_array_add_pointer( + &sorted_triggers, + (void *) lttng_triggers_get_at_index(triggers, i)); + + if (add_ret) { + ERR("Failed to allocate array of struct lttng_trigger *."); + goto error; + } + } + + qsort(sorted_triggers.array.buffer.data, num_triggers, + sizeof(struct lttng_trigger *), + compare_triggers_by_name); + + for (i = 0; i < num_triggers; i++) { + const struct lttng_trigger *trigger_to_print = + (const struct lttng_trigger *) + lttng_dynamic_pointer_array_get_pointer( + &sorted_triggers, i); + + print_one_trigger(trigger_to_print); + } + + ret = 0; + goto end; + +error: + ret = 1; + +end: + argpar_parse_ret_fini(&argpar_parse_ret); + lttng_triggers_destroy(triggers); + lttng_dynamic_pointer_array_reset(&sorted_triggers); + + return ret; +} diff --git a/src/bin/lttng/lttng.c b/src/bin/lttng/lttng.c index e760bdbe1..e5642e24c 100644 --- a/src/bin/lttng/lttng.c +++ b/src/bin/lttng/lttng.c @@ -75,6 +75,7 @@ static struct cmd_struct commands[] = { { "enable-event", cmd_enable_events}, { "help", NULL}, { "list", cmd_list}, + { "list-triggers", cmd_list_triggers}, { "load", cmd_load}, { "metadata", cmd_metadata}, { "regenerate", cmd_regenerate}, diff --git a/src/common/conditions/condition.c b/src/common/conditions/condition.c index d1990414c..e0b85e429 100644 --- a/src/common/conditions/condition.c +++ b/src/common/conditions/condition.c @@ -207,3 +207,33 @@ void lttng_condition_init(struct lttng_condition *condition, condition->type = type; urcu_ref_init(&condition->ref); } + +LTTNG_HIDDEN +const char *lttng_condition_type_str(enum lttng_condition_type type) +{ + switch (type) { + case LTTNG_CONDITION_TYPE_UNKNOWN: + return "unknown"; + + case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: + return "session consumed size"; + + case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: + return "buffer usage high"; + + case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: + return "buffer usage low"; + + case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: + return "session rotation ongoing"; + + case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: + return "session rotation completed"; + + case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: + return "event rule hit"; + + default: + return "???"; + } +} diff --git a/src/common/trigger.c b/src/common/trigger.c index f47f76d55..55ab99729 100644 --- a/src/common/trigger.c +++ b/src/common/trigger.c @@ -84,11 +84,10 @@ struct lttng_condition *lttng_trigger_get_condition( return trigger ? trigger->condition : NULL; } -LTTNG_HIDDEN const struct lttng_condition *lttng_trigger_get_const_condition( const struct lttng_trigger *trigger) { - return trigger->condition; + return trigger ? trigger->condition : NULL; } @@ -103,11 +102,10 @@ struct lttng_action *lttng_trigger_get_action( return trigger ? trigger->action : NULL; } -LTTNG_HIDDEN const struct lttng_action *lttng_trigger_get_const_action( const struct lttng_trigger *trigger) { - return trigger->action; + return trigger ? trigger->action : NULL; } static void trigger_destroy_ref(struct urcu_ref *ref) diff --git a/src/lib/lttng-ctl/snapshot.c b/src/lib/lttng-ctl/snapshot.c index f7b4741ca..93f393fc2 100644 --- a/src/lib/lttng-ctl/snapshot.c +++ b/src/lib/lttng-ctl/snapshot.c @@ -265,29 +265,29 @@ void lttng_snapshot_output_destroy(struct lttng_snapshot_output *obj) * Getter family functions of snapshot output. */ -uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output *output) +uint32_t lttng_snapshot_output_get_id(const struct lttng_snapshot_output *output) { return output->id; } const char *lttng_snapshot_output_get_name( - struct lttng_snapshot_output *output) + const struct lttng_snapshot_output *output) { return output->name; } -const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output *output) +const char *lttng_snapshot_output_get_data_url(const struct lttng_snapshot_output *output) { return output->data_url; } -const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output *output) +const char *lttng_snapshot_output_get_ctrl_url(const struct lttng_snapshot_output *output) { return output->ctrl_url; } uint64_t lttng_snapshot_output_get_maxsize( - struct lttng_snapshot_output *output) + const struct lttng_snapshot_output *output) { return output->max_size; } diff --git a/tests/regression/Makefile.am b/tests/regression/Makefile.am index 193f730d6..cc5541956 100644 --- a/tests/regression/Makefile.am +++ b/tests/regression/Makefile.am @@ -40,7 +40,8 @@ TESTS = tools/filtering/test_invalid_filter \ tools/clear/test_ust \ tools/clear/test_kernel \ tools/tracker/test_event_tracker \ - tools/trigger/test_add_trigger_cli + tools/trigger/test_add_trigger_cli \ + tools/trigger/test_list_triggers_cli if HAVE_LIBLTTNG_UST_CTL SUBDIRS += ust diff --git a/tests/regression/tools/trigger/Makefile.am b/tests/regression/tools/trigger/Makefile.am index 8d911eae0..dcd15b4d1 100644 --- a/tests/regression/tools/trigger/Makefile.am +++ b/tests/regression/tools/trigger/Makefile.am @@ -1,5 +1,7 @@ -noinst_SCRIPTS = test_add_trigger_cli -EXTRA_DIST = test_add_trigger_cli +noinst_SCRIPTS = test_add_trigger_cli \ + test_list_triggers_cli +EXTRA_DIST = test_add_trigger_cli \ + test_list_triggers_cli all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ diff --git a/tests/regression/tools/trigger/test_list_triggers_cli b/tests/regression/tools/trigger/test_list_triggers_cli new file mode 100755 index 000000000..e09d7cdc6 --- /dev/null +++ b/tests/regression/tools/trigger/test_list_triggers_cli @@ -0,0 +1,335 @@ +#!/bin/bash +# +# Copyright (C) - 2020 EfficiOS, inc +# +# This library is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +# Test the `lttng list-trigger` command line interface. + +CURDIR="$(dirname "$0")" +TESTDIR="$CURDIR/../../.." + +# shellcheck source=../../../utils/utils.sh +source "$TESTDIR/utils/utils.sh" + +plan_tests 40 + +FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}" + +tmp_stdout=$(mktemp -t test_list_triggers_cli_stdout.XXXXXX) +tmp_stderr=$(mktemp -t test_list_triggers_cli_stderr.XXXXXX) +tmp_expected_stdout=$(mktemp -t test_list_triggers_cli_expected_stdout.XXXXXX) +uprobe_elf_binary=$(realpath "${TESTDIR}/utils/testapp/userspace-probe-elf-binary/.libs/userspace-probe-elf-binary") + +uid=$(id --user) +gid=$(id --group) + +if [ "$uid" == "0" ]; then + ist_root=1 +else + ist_root=0 +fi + +function add_trigger () +{ + "${FULL_LTTNG_BIN}" add-trigger "$@" + ok $? "add trigger \`$*\`: exit code is 0" +} + +function list_triggers () +{ + local test_name="$1" + local expected_stdout_file="$2" + + "${FULL_LTTNG_BIN}" list-triggers > "${tmp_stdout}" 2> "${tmp_stderr}" + ok $? "${test_name}: exit code is 0" + + diff -u "${expected_stdout_file}" "${tmp_stdout}" + ok $? "${test_name}: expected stdout" + + diff -u /dev/null "${tmp_stderr}" + ok $? "${test_name}: expected stderr" +} + +test_top_level_options () +{ + # shellcheck disable=SC2119 + start_lttng_sessiond_notap + + + add_trigger --id hello --condition on-event -u test-id --action notify + add_trigger --fire-once-after 123 --condition on-event -u test-fire-once-after --action notify + add_trigger --fire-every 124 --condition on-event -u test-fire-every --action notify + + cat > "${tmp_expected_stdout}" <<- EOF + - id: T0 + user id: ${uid} + firing policy: once after 123 occurences + condition: event rule hit + rule: test-fire-once-after (type: tracepoint, domain: ust) + actions: + notify + - id: T1 + user id: ${uid} + firing policy: after every 124 occurences + condition: event rule hit + rule: test-fire-every (type: tracepoint, domain: ust) + actions: + notify + - id: hello + user id: ${uid} + condition: event rule hit + rule: test-id (type: tracepoint, domain: ust) + actions: + notify + EOF + + list_triggers "top level options" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_on_event_tracepoint () +{ + # shellcheck disable=SC2119 + start_lttng_sessiond_notap + + add_trigger --condition on-event -u -a --action notify + add_trigger --id ABC --condition on-event aaa -u --filter 'p == 2' --action notify + add_trigger --condition on-event 'hello*' -u -x 'hello2,hello3,hello4' --action notify + add_trigger --id BCD --condition on-event -u gerboise --loglevel INFO --action notify + add_trigger --condition on-event -u lemming --loglevel-only WARNING --action notify + + cat > "${tmp_expected_stdout}" <<- EOF + - id: ABC + user id: ${uid} + condition: event rule hit + rule: aaa (type: tracepoint, domain: ust, filter: p == 2) + actions: + notify + - id: BCD + user id: ${uid} + condition: event rule hit + rule: gerboise (type: tracepoint, domain: ust, log level <= TRACE_INFO) + actions: + notify + - id: T0 + user id: ${uid} + condition: event rule hit + rule: * (type: tracepoint, domain: ust) + actions: + notify + - id: T1 + user id: ${uid} + condition: event rule hit + rule: hello* (type: tracepoint, domain: ust, exclusions: hello2,hello3,hello4) + actions: + notify + - id: T2 + user id: ${uid} + condition: event rule hit + rule: lemming (type: tracepoint, domain: ust, log level == TRACE_WARNING) + actions: + notify + EOF + + list_triggers "on-event, tracepoint event rule" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_on_event_probe () +{ + local channel_enable_addr + local channel_disable_addr + + # shellcheck disable=SC2119 + start_lttng_sessiond_notap + + channel_enable_addr=$(grep ' t lttng_channel_enable\s\[lttng_tracer\]$' /proc/kallsyms | cut -f 1 -d ' ') + channel_disable_addr=$(grep ' t lttng_channel_disable\s\[lttng_tracer\]$' /proc/kallsyms | cut -f 1 -d ' ') + + # We need to find a valid offset. + base_symbol="" + offset=0 + if [[ 0x$channel_enable_addr -lt 0x$channel_disable_addr ]]; then + base_symbol="lttng_channel_enable" + offset=$(( 0x$channel_disable_addr - 0x$channel_enable_addr )) + else + base_symbol="lttng_channel_disable" + offset=$(( 0x$channel_enable_addr - 0x$channel_disable_addr )) + fi + + offset_hex="0x$(printf '%x' $offset)" + + add_trigger --condition on-event -k --probe=lttng_channel_enable my_channel_enable --action notify + add_trigger --condition on-event -k --probe="${base_symbol}+${offset_hex}" my_channel_enable --action notify + add_trigger --condition on-event -k --probe="0x${channel_enable_addr}" my_channel_enable --action notify + + cat > "${tmp_expected_stdout}" <<- EOF + - id: T0 + user id: ${uid} + condition: event rule hit + rule: my_channel_enable (type: probe, location: lttng_channel_enable) + actions: + notify + - id: T1 + user id: ${uid} + condition: event rule hit + rule: my_channel_enable (type: probe, location: ${base_symbol}+${offset_hex}) + actions: + notify + - id: T2 + user id: ${uid} + condition: event rule hit + rule: my_channel_enable (type: probe, location: 0x${channel_enable_addr}) + actions: + notify + EOF + + list_triggers "on-event, probe event rule" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_on_event_userspace_probe () +{ + # shellcheck disable=SC2119 + start_lttng_sessiond_notap + + add_trigger --condition on-event -k --userspace-probe=${uprobe_elf_binary}:test_function ma-probe --action notify + + cat > "${tmp_expected_stdout}" <<- EOF + - id: T0 + user id: ${uid} + condition: event rule hit + rule: ma-probe (type: userspace probe, location: ${uprobe_elf_binary}:test_function) + actions: + notify + EOF + + list_triggers "on-event, userspace-probe event rule" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_on_event_syscall () +{ + # shellcheck disable=SC2119 + start_lttng_sessiond_notap + + add_trigger --condition on-event -k --syscall open --action notify + add_trigger --condition on-event -k --syscall ptrace --filter 'a > 2' --action notify + + cat > "${tmp_expected_stdout}" <<- EOF + - id: T0 + user id: ${uid} + condition: event rule hit + rule: open (type: syscall) + actions: + notify + - id: T1 + user id: ${uid} + condition: event rule hit + rule: ptrace (type: syscall, filter: a > 2) + actions: + notify + EOF + + list_triggers "on-event, syscall event rule" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_snapshot_action () +{ + start_lttng_sessiond_notap + + add_trigger --condition on-event -u some-event --action snapshot-session ze-session + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --path /some/path + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --url file:///some/other/path + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --url net://1.2.3.4 + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --url net://1.2.3.4:1234:1235 + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --ctrl-url=tcp://1.2.3.4:1111 --data-url=tcp://1.2.3.4:1112 + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --path /some/path --max-size=1234 + add_trigger --condition on-event -u some-event --action snapshot-session ze-session --path /some/path --name=meh + + + cat > "${tmp_expected_stdout}" <<- EOF + - id: T0 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\` + - id: T1 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, path: /some/path + - id: T2 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, path: /some/other/path + - id: T3 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, url: net://1.2.3.4 + - id: T4 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, url: net://1.2.3.4:1234:1235 + - id: T5 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, control url: tcp://1.2.3.4:1111, data url: tcp://1.2.3.4:1112 + - id: T6 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, path: /some/path, max size: 1234 + - id: T7 + user id: ${uid} + condition: event rule hit + rule: some-event (type: tracepoint, domain: ust) + actions: + snapshot session \`ze-session\`, path: /some/path, name: meh + EOF + + list_triggers "snapshot action" "${tmp_expected_stdout}" + + stop_lttng_sessiond_notap +} + +test_top_level_options +test_on_event_tracepoint +skip $ist_root "non-root user: skipping kprobe tests" 6 || test_on_event_probe +skip $ist_root "non-root user: skipping uprobe tests" 4 || test_on_event_userspace_probe +skip $ist_root "non-root user: skipping syscall tests" 5 || test_on_event_syscall +test_snapshot_action + +# Cleanup +rm -f "${tmp_stdout}" +rm -f "${tmp_stderr}" +rm -f "${tmp_expected_stdout}" -- 2.34.1