#!/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 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 lttng_add_trigger_ok "hello" --condition on-event -u test-id --action notify lttng_add_trigger_ok "T0" --fire-once-after 123 --condition on-event -u test-fire-once-after --action notify lttng_add_trigger_ok "T1" --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) tracer notifications discarded: 0 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) tracer notifications discarded: 0 actions: notify - id: hello user id: ${uid} condition: event rule hit rule: test-id (type: tracepoint, domain: ust) tracer notifications discarded: 0 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 lttng_add_trigger_ok "C" --condition on-event -u -a --action notify lttng_add_trigger_ok "A" --condition on-event aaa -u --filter 'p == 2' --action notify lttng_add_trigger_ok "D" --condition on-event 'hello*' -u -x 'hello2,hello3,hello4' --action notify lttng_add_trigger_ok "B" --condition on-event -u gerboise --loglevel INFO --action notify lttng_add_trigger_ok "E" --condition on-event -u lemming --loglevel-only WARNING --action notify lttng_add_trigger_ok "F" --condition on-event -u capture-payload-field --capture a --action notify lttng_add_trigger_ok "G" --condition on-event -u capture-array --capture 'a[2]' --capture '$ctx.tourlou[18]' --action notify lttng_add_trigger_ok "H" --condition on-event -u capture-chan-ctx --capture '$ctx.vpid' --action notify lttng_add_trigger_ok "I" --condition on-event -u capture-app-ctx --capture '$app.iga:active_clients' --action notify cat > "${tmp_expected_stdout}" <<- EOF - id: A user id: ${uid} condition: event rule hit rule: aaa (type: tracepoint, domain: ust, filter: p == 2) tracer notifications discarded: 0 actions: notify - id: B user id: ${uid} condition: event rule hit rule: gerboise (type: tracepoint, domain: ust, log level at least INFO) tracer notifications discarded: 0 actions: notify - id: C user id: ${uid} condition: event rule hit rule: * (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: notify - id: D user id: ${uid} condition: event rule hit rule: hello* (type: tracepoint, domain: ust, exclusions: hello2,hello3,hello4) tracer notifications discarded: 0 actions: notify - id: E user id: ${uid} condition: event rule hit rule: lemming (type: tracepoint, domain: ust, log level is WARNING) tracer notifications discarded: 0 actions: notify - id: F user id: ${uid} condition: event rule hit rule: capture-payload-field (type: tracepoint, domain: ust) tracer notifications discarded: 0 captures: - a actions: notify - id: G user id: ${uid} condition: event rule hit rule: capture-array (type: tracepoint, domain: ust) tracer notifications discarded: 0 captures: - a[2] - \$ctx.tourlou[18] actions: notify - id: H user id: ${uid} condition: event rule hit rule: capture-chan-ctx (type: tracepoint, domain: ust) tracer notifications discarded: 0 captures: - \$ctx.vpid actions: notify - id: I user id: ${uid} condition: event rule hit rule: capture-app-ctx (type: tracepoint, domain: ust) tracer notifications discarded: 0 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)" lttng_add_trigger_ok "T0" --condition on-event -k --probe=lttng_channel_enable my_channel_enable --action notify lttng_add_trigger_ok "T1" --condition on-event -k --probe="${base_symbol}+${offset_hex}" my_channel_enable --action notify lttng_add_trigger_ok "T2" --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) tracer notifications discarded: 0 actions: notify - id: T1 user id: ${uid} condition: event rule hit rule: my_channel_enable (type: probe, location: ${base_symbol}+${offset_hex}) tracer notifications discarded: 0 actions: notify - id: T2 user id: ${uid} condition: event rule hit rule: my_channel_enable (type: probe, location: 0x${channel_enable_addr}) tracer notifications discarded: 0 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 lttng_add_trigger_ok "T0" --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) tracer notifications discarded: 0 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 lttng_add_trigger_ok "T0" --condition on-event -k --syscall open --action notify lttng_add_trigger_ok "T1" --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) tracer notifications discarded: 0 actions: notify - id: T1 user id: ${uid} condition: event rule hit rule: ptrace (type: syscall, filter: a > 2) tracer notifications discarded: 0 actions: notify EOF list_triggers "on-event, syscall event rule" "${tmp_expected_stdout}" stop_lttng_sessiond_notap } test_snapshot_action () { start_lttng_sessiond_notap lttng_add_trigger_ok "T0" --condition on-event -u some-event --action snapshot-session ze-session lttng_add_trigger_ok "T1" --condition on-event -u some-event --action snapshot-session ze-session --path /some/path lttng_add_trigger_ok "T2" --condition on-event -u some-event --action snapshot-session ze-session --url file:///some/other/path lttng_add_trigger_ok "T3" --condition on-event -u some-event --action snapshot-session ze-session --url net://1.2.3.4 lttng_add_trigger_ok "T4" --condition on-event -u some-event --action snapshot-session ze-session --url net://1.2.3.4:1234:1235 lttng_add_trigger_ok "T5" --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 lttng_add_trigger_ok "T6" --condition on-event -u some-event --action snapshot-session ze-session --path /some/path --max-size=1234 lttng_add_trigger_ok "T7" --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) tracer notifications discarded: 0 actions: snapshot session \`ze-session\` - id: T1 user id: ${uid} condition: event rule hit rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: snapshot session \`ze-session\`, path: /some/path - id: T2 user id: ${uid} condition: event rule hit rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 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) tracer notifications discarded: 0 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) tracer notifications discarded: 0 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) tracer notifications discarded: 0 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) tracer notifications discarded: 0 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) tracer notifications discarded: 0 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}"