--- /dev/null
+#!/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 41
+
+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/userspace-probe-elf-binary")
+
+if [ "$(id -u)" == "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: T2
+ firing policy: once after 123 occurences
+ condition: event rule hit
+ rule: test-fire-once-after (type: tracepoint, domain: ust)
+ actions:
+ notify
+ - id: T3
+ firing policy: after every 124 occurences
+ condition: event rule hit
+ rule: test-fire-every (type: tracepoint, domain: ust)
+ actions:
+ notify
+ - id: hello
+ 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
+ condition: event rule hit
+ rule: aaa (type: tracepoint, domain: ust, filter: p == 2)
+ actions:
+ notify
+ - id: BCD
+ condition: event rule hit
+ rule: gerboise (type: tracepoint, domain: ust, log level <= TRACE_INFO)
+ actions:
+ notify
+ - id: T1
+ condition: event rule hit
+ rule: * (type: tracepoint, domain: ust)
+ actions:
+ notify
+ - id: T3
+ condition: event rule hit
+ rule: hello* (type: tracepoint, domain: ust, exclusions: hello2,hello3,hello4)
+ actions:
+ notify
+ - id: T5
+ 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 sys_open_addr
+
+ # shellcheck disable=SC2119
+ start_lttng_sessiond_notap
+
+ sys_open_addr=$(grep ' T do_sys_open$' /proc/kallsyms | cut -f 1 -d ' ')
+
+ add_trigger --condition on-event -k --probe=do_sys_open my_sys_open --action notify
+ add_trigger --condition on-event -k --probe=do_sys_open+10 my_sys_open --action notify
+ add_trigger --condition on-event -k --probe="${sys_open_addr}" my_sys_open --action notify
+ add_trigger --condition on-event -k --probe="${sys_open_addr}+10" my_sys_open --action notify
+
+ cat > "${tmp_expected_stdout}" <<- EOF
+ - id: T1
+ condition: event rule hit
+ rule: my_sys_open (type: probe, location: do_sys_open)
+ actions:
+ notify
+ - id: T2
+ condition: event rule hit
+ rule: my_sys_open (type: probe, location: do_sys_open+10)
+ actions:
+ notify
+ - id: T3
+ condition: event rule hit
+ rule: my_sys_open (type: probe, location: ${sys_open_addr})
+ actions:
+ notify
+ - id: T4
+ condition: event rule hit
+ rule: my_sys_open (type: probe, location: ${sys_open_addr}+10)
+ 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: T1
+ 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: T1
+ condition: event rule hit
+ - rule: open (type: syscall)
+ actions:
+ notify
+ - id: T2
+ 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: T1
+ condition: event rule hit
+ rule: some-event (type: tracepoint, domain: ust)
+ actions:
+ snapshot session \`ze-session\`
+ - id: T2
+ condition: event rule hit
+ rule: some-event (type: tracepoint, domain: ust)
+ actions:
+ snapshot session \`ze-session\`, path: /some/path
+ - id: T3
+ condition: event rule hit
+ rule: some-event (type: tracepoint, domain: ust)
+ actions:
+ snapshot session \`ze-session\`, path: /some/other/path
+ - id: T4
+ condition: event rule hit
+ rule: some-event (type: tracepoint, domain: ust)
+ actions:
+ snapshot session \`ze-session\`, url: net://1.2.3.4
+ - id: T5
+ 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: T6
+ 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: T7
+ condition: event rule hit
+ rule: some-event (type: tracepoint, domain: ust)
+ actions:
+ snapshot session \`ze-session\`, path: /some/path, max size: 1234
+ - id: T8
+ 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" 7 || 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}"