SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / tests / regression / tools / trigger / test_list_triggers_cli
diff --git a/tests/regression/tools/trigger/test_list_triggers_cli b/tests/regression/tools/trigger/test_list_triggers_cli
new file mode 100755 (executable)
index 0000000..78b89a1
--- /dev/null
@@ -0,0 +1,399 @@
+#!/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 44
+
+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
+         tracer notifications discarded: 0
+         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
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: test-fire-every (type: tracepoint, domain: ust)
+         actions:
+           notify
+       - id: hello
+         user id: ${uid}
+         tracer notifications discarded: 0
+         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
+       add_trigger --condition on-event -u capture-payload-field --capture a --action notify
+       add_trigger --condition on-event -u capture-array --capture 'a[2]' --capture '$ctx.tourlou[18]' --action notify
+       add_trigger --condition on-event -u capture-chan-ctx --capture '$ctx.vpid' --action notify
+       add_trigger --condition on-event -u capture-app-ctx --capture '$app.iga:active_clients' --action notify
+
+
+       cat > "${tmp_expected_stdout}" <<- EOF
+       - id: ABC
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: aaa (type: tracepoint, domain: ust, filter: p == 2)
+         actions:
+           notify
+       - id: BCD
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: gerboise (type: tracepoint, domain: ust, log level <= TRACE_INFO)
+         actions:
+           notify
+       - id: T0
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: * (type: tracepoint, domain: ust)
+         actions:
+           notify
+       - id: T1
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: hello* (type: tracepoint, domain: ust, exclusions: hello2,hello3,hello4)
+         actions:
+           notify
+       - id: T2
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: lemming (type: tracepoint, domain: ust, log level == TRACE_WARNING)
+         actions:
+           notify
+       - id: T3
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: capture-payload-field (type: tracepoint, domain: ust)
+           captures:
+             - a
+         actions:
+           notify
+       - id: T4
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: capture-array (type: tracepoint, domain: ust)
+           captures:
+             - a[2]
+             - \$ctx.tourlou[18]
+         actions:
+           notify
+       - id: T5
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: capture-chan-ctx (type: tracepoint, domain: ust)
+           captures:
+             - \$ctx.vpid
+         actions:
+           notify
+       - id: T6
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: capture-app-ctx (type: tracepoint, domain: ust)
+           captures:
+             - \$app.iga:active_clients
+         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}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: my_channel_enable (type: probe, location: lttng_channel_enable)
+         actions:
+           notify
+       - id: T1
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: my_channel_enable (type: probe, location: ${base_symbol}+${offset_hex})
+         actions:
+           notify
+       - id: T2
+         user id: ${uid}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         condition: event rule hit
+         - rule: open (type: syscall)
+         actions:
+           notify
+       - id: T1
+         user id: ${uid}
+         tracer notifications discarded: 0
+         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 /some/path
+       add_trigger --condition on-event -u some-event --action snapshot-session ze-session file:///some/other/path
+       add_trigger --condition on-event -u some-event --action snapshot-session ze-session net://1.2.3.4
+       add_trigger --condition on-event -u some-event --action snapshot-session ze-session 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 /some/path --max-size=1234
+       add_trigger --condition on-event -u some-event --action snapshot-session ze-session /some/path --name=meh
+
+
+       cat > "${tmp_expected_stdout}" <<- EOF
+       - id: T0
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: some-event (type: tracepoint, domain: ust)
+         actions:
+           snapshot session \`ze-session\`
+       - id: T1
+         user id: ${uid}
+         tracer notifications discarded: 0
+         condition: event rule hit
+           rule: some-event (type: tracepoint, domain: ust)
+         actions:
+           snapshot session \`ze-session\`, path: /some/path
+       - id: T2
+         user id: ${uid}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}
+         tracer notifications discarded: 0
+         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}"
This page took 0.028017 seconds and 5 git commands to generate.