#!/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 add-trigger` command line interface. CURDIR="$(dirname "$0")" TESTDIR="$CURDIR/../../.." # shellcheck source=../../../utils/utils.sh source "$TESTDIR/utils/utils.sh" plan_tests 228 FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}" # shellcheck disable=SC2119 start_lttng_sessiond_notap tmp_stdout=$(mktemp -t test_parse_cli_trigger_stdout.XXXXXX) tmp_stderr=$(mktemp -t test_parse_cli_trigger_stderr.XXXXXX) uprobe_elf_binary="${TESTDIR}/utils/testapp/userspace-probe-elf-binary/.libs/userspace-probe-elf-binary" if [ "$(id -u)" == "0" ]; then ist_root=1 else ist_root=0 fi function test_success () { local test_name="$1" shift diag "${FULL_LTTNG_BIN} add-trigger $*" set -x "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}" set +x ok $? "${test_name}: exit code is 0" diff -u "${tmp_stdout}" <(echo "Trigger registered successfully.") ok $? "${test_name}: expected stdout" diff -u "${tmp_stderr}" /dev/null ok $? "${test_name}: expected stderr" } function test_failure () { local test_name="$1" local error_msg="$2" shift 2 diag "${FULL_LTTNG_BIN} add-trigger $*" "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}" isnt $? 0 "${test_name}: exit code is not 0" diff -u "${tmp_stdout}" /dev/null ok $? "${test_name}: expected stdout" diff -u "${tmp_stderr}" <(echo "${error_msg}") ok $? "${test_name}: expected stderr" } # top-level options test_success "explicit name" \ --name hohoho \ --condition event-rule-matches --name=some-event-id --domain=user \ --action notify # `--condition event-rule-matches` successes test_success "--condition event-rule-matches some-event --domain=user" \ --condition event-rule-matches --name=some-event --domain=user \ --action notify test_success "--condition event-rule-matches --domain=user" \ --condition event-rule-matches --domain=user \ --action notify test_success "notify action polices" \ --condition event-rule-matches --domain=user --name=test-rate-policy \ --action notify \ --rate-policy=every:55 \ --action notify \ --rate-policy=once-after:55 test_success "start session action polices" \ --condition event-rule-matches --domain=user --name=test-rate-policy \ --action start-session my_session \ --rate-policy=every:55 \ --action start-session my_session \ --rate-policy=once-after:55 test_success "stop session action polices" \ --condition event-rule-matches --domain=user --name=test-rate-policy \ --action stop-session my_session \ --rate-policy=every:55 \ --action stop-session my_session \ --rate-policy=once-after:55 test_success "snapshot session action polices" \ --condition event-rule-matches --domain=user --name=test-rate-policy \ --action snapshot-session my_session \ --rate-policy=every:55 \ --action snapshot-session my_session \ --rate-policy=once-after:55 test_success "rotate session action polices" \ --condition event-rule-matches --domain=user --name=test-rate-policy \ --action rotate-session my_session \ --rate-policy=every:55 \ --action rotate-session my_session \ --rate-policy=once-after:55 skip $ist_root "non-root user: skipping kprobe tests" 9 || { test_success "--condition event-rule-matches probe by symbol" \ --condition event-rule-matches --domain=kernel --probe=lttng_channel_enable --event-name=my_channel_enable \ --action notify 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)" test_success "--condition event-rule-matches probe by symbol with offset" \ --condition event-rule-matches --domain=kernel --probe="${base_symbol}+${offset_hex}" --event-name=my_$base_symbol \ --action notify test_success "--condition event-rule-matches probe by address" \ --condition event-rule-matches --domain=kernel "--probe=0x${channel_enable_addr}" --event-name=my_channel_enable \ --action notify } skip $ist_root "non-root user: skipping uprobe tests" 6 || { test_success "--condition event-rule-matches uprobe" \ --condition event-rule-matches --domain=kernel --userspace-probe=${uprobe_elf_binary}:test_function --event-name=ma-probe \ --action notify test_success "--condition event-rule-matches uprobe with elf prefix" \ --condition event-rule-matches --domain=kernel --userspace-probe=elf:${uprobe_elf_binary}:test_function --event-name=ma-probe-2 \ --action notify } skip $ist_root "non-root user: skipping syscall tests" 9 || { test_success "--condition event-rule-matches one syscall" \ --condition event-rule-matches --domain=kernel --syscall --name=open \ --action notify test_success "--condition event-rule-matches all syscalls" \ --condition event-rule-matches --domain=kernel --syscall \ --action notify test_success "--condition event-rule-matches one syscall with filter" \ --condition event-rule-matches --domain=kernel --syscall --filter 'a > 2' --name=open \ --action notify } # `--action notify` successes test_success "--action notify" \ --condition event-rule-matches --domain=user \ --action notify test_success "--action notify --capture foo" \ --condition event-rule-matches --domain=user \ --capture foo --action notify test_success "--action notify --capture foo[2]" \ --condition event-rule-matches --domain=user \ --capture 'foo[2]' --action notify test_success '--action notify --capture $ctx.foo' \ --condition event-rule-matches --domain=user \ --capture '$ctx.foo' --action notify test_success '--action notify --capture $ctx.foo[2]' \ --condition event-rule-matches --domain=user \ --capture '$ctx.foo[2]' --action notify test_success '--action notify --capture $app.prov:type' \ --condition event-rule-matches --domain=user \ --capture '$app.prov:type' --action notify test_success '--action notify --capture $app.prov:type[2]' \ --condition event-rule-matches --domain=user \ --capture '$app.prov:type[2]' --action notify test_success '--action notify multiple captures' \ --condition event-rule-matches --domain=user \ --capture foo --capture '$app.hello:world' --action notify # `--action start-session` successes test_success "--action start-session" \ --condition event-rule-matches --domain=user \ --action start-session ze-session # `--action stop-session` successes test_success "--action stop-session foo" \ --condition event-rule-matches --domain=user \ --action stop-session ze-session # `--action rotate-session` successes test_success "--action rotate-session foo" \ --condition event-rule-matches --domain=user \ --action rotate-session ze-session # `--action snapshot-session` successes test_success "--action snapshot-session foo" \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session test_success "--action snapshot-session with file URI" \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --path /hello test_success "--action snapshot-session with net URI" \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --url net://1.2.3.4 test_success "--action snapshot-session with ctrl/data URIs" \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --ctrl-url=tcp://1.2.3.4:1234 --data-url=tcp://1.2.3.4:1235 # top-level failures test_failure "no args" "Error: Missing --condition." test_failure "unknown option" \ "Error: Unknown option \`--hello\`" \ --hello test_failure "missing --action" \ "Error: Need at least one --action." \ --condition event-rule-matches --domain=user test_failure "two --condition" \ "Error: A --condition was already given." \ --condition event-rule-matches --name=aaa --domain=user \ --condition event-rule-matches --name=bbb --domain=user \ --action notify test_failure "missing argument to --name" \ "Error: While parsing argument #1 (\`--name\`): Missing required argument for option \`--name\`" \ --name for cmd in rate-policy=once-after rate-policy=every; do test_failure "missing argument to --${cmd}" \ "Error: Rate policy format is invalid." \ --condition event-rule-matches --domain=user --action notify \ --${cmd} test_failure "invalid argument to --${cmd}: non-digit character" \ "Error: Failed to parse rate policy value \`123bob\` as an integer." \ --condition event-rule-matches --domain=user --action notify \ --${cmd}:123bob test_failure "invalid argument to --${cmd}: empty string" \ "Error: Failed to parse rate policy value \`\` as an integer." \ --condition event-rule-matches --domain=user --action notify \ --${cmd}":" done test_failure "invalid argument to --rate-policy: unknown policy type" \ "Error: Rate policy type \`bob\` unknown." \ --condition event-rule-matches --domain=user --action notify \ --rate-policy=bob:123 # `--condition` failures test_failure "missing args after --condition" \ "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \ --condition test_failure "unknown --condition" \ "Error: Unknown condition name 'zoofest'" \ --condition zoofest # `--condition event-rule-matches` failures test_failure "missing args after --condition event-rule-matches" \ "Error: Please specify a domain (--domain=(kernel,user,jul,log4j,python))." \ --condition event-rule-matches test_failure "extra args after --condition event-rule-matches" \ "Error: Unexpected argument 'bozo'" \ --condition event-rule-matches --domain=user bozo test_failure "two same --domain" \ "Error: More than one \`--domain\` was specified." \ --condition event-rule-matches --domain=user --domain=user test_failure "two different --domain" \ "Error: More than one \`--domain\` was specified." \ --condition event-rule-matches --domain=user --domain=kernel test_failure "--condition event-rule-matches: --name with --probe" \ "Error: Can't use --name with probe event rules." \ --condition event-rule-matches --probe=do_sys_open --name='hello' test_failure "--condition event-rule-matches: --event-name with tracepoint" \ "Error: Can't use --event-name with tracepoint event rules." \ --condition event-rule-matches --domain=user --event-name='hello' test_failure "--condition event-rule-matches: extra argument with --userspace-probe" \ "Error: Unexpected argument 'hello'" \ --condition event-rule-matches --domain=kernel --userspace-probe=${uprobe_elf_binary}:test_failure hello test_failure "--condition event-rule-matches: extra argument with --syscall" \ "Error: Unexpected argument 'open'" \ --condition event-rule-matches --domain=kernel --syscall open test_failure "--condition event-rule-matches --capture: missing argument (end of arg list)" \ 'Error: While parsing argument #2 (`--capture`): Missing required argument for option `--capture`' \ --action notify \ --condition event-rule-matches --domain=user --capture test_failure "--condition event-rule-matches --capture: missing argument (before another option)" \ 'Error: While parsing expression `--action`: Unary operators are not allowed in capture expressions.' \ --condition event-rule-matches --domain=user --capture \ --action notify \ test_failure "--condition event-rule-matches --capture: binary operator" \ 'Error: While parsing expression `foo == 2`: Binary operators are not allowed in capture expressions.' \ --condition event-rule-matches --domain=user \ --capture 'foo == 2' --action notify test_failure "--condition event-rule-matches --capture: unary operator" \ 'Error: While parsing expression `!foo`: Unary operators are not allowed in capture expressions.' \ --condition event-rule-matches --domain=user \ --capture '!foo' --action notify test_failure "--condition event-rule-matches --capture: logical operator" \ 'Error: While parsing expression `foo || bar`: Logical operators are not allowed in capture expressions.' \ --condition event-rule-matches --domain=user \ --capture 'foo || bar' --action notify test_failure "--condition event-rule-matches --capture: accessing a sub-field" \ 'Error: While parsing expression `foo.bar`: Capturing subfields is not supported.' \ --condition event-rule-matches --domain=user \ --capture 'foo.bar' --action notify test_failure "--condition event-rule-matches --capture: accessing the sub-field of an array element" \ 'Error: While parsing expression `foo[3].bar`: Capturing subfields is not supported.' \ --condition event-rule-matches --domain=user \ --capture 'foo[3].bar' --action notify test_failure "--condition event-rule-matches --capture: missing colon in app-specific context field" \ 'Error: Invalid app-specific context field name: missing colon in `foo`.' \ --condition event-rule-matches --domain=user \ --capture '$app.foo' --action notify test_failure "--condition event-rule-matches --capture: missing colon in app-specific context field" \ 'Error: Invalid app-specific context field name: missing type name after colon in `foo:`.' \ --condition event-rule-matches --domain=user \ --capture '$app.foo:' --action notify # `--action` failures test_failure "missing args after --action" \ "Error: While parsing argument #1 (\`--action\`): Missing required argument for option \`--action\`" \ --condition event-rule-matches --domain=user \ --action # `--action notify` failures test_failure "extra arg after --action notify" \ "Error: Unexpected argument \`bob\`." \ --condition event-rule-matches --domain=user \ --action notify bob # `--action start-session` failures test_failure "missing arg after --action start-session" \ "Error: Missing session name." \ --condition event-rule-matches --domain=user \ --action start-session test_failure "extra arg after --action start-session" \ "Error: Unexpected argument \`bob\`." \ --condition event-rule-matches --domain=user \ --action start-session ze-session bob # `--action stop-session` failures test_failure "missing arg after --action stop-session" \ "Error: Missing session name." \ --condition event-rule-matches --domain=user \ --action stop-session test_failure "extra arg after --action stop-session" \ "Error: Unexpected argument \`bob\`." \ --condition event-rule-matches --domain=user \ --action stop-session ze-session bob # `--action rotate-session` failures test_failure "missing arg after --action rotate-session" \ "Error: Missing session name." \ --condition event-rule-matches --domain=user \ --action rotate-session test_failure "extra arg after --action rotate-session" \ "Error: Unexpected argument \`bob\`." \ --condition event-rule-matches --domain=user \ --action rotate-session ze-session bob # `--action snapshot-session` failures test_failure "missing arg after --action snapshot-session" \ "Error: Missing session name." \ --condition event-rule-matches --domain=user \ --action snapshot-session test_failure "extra arg after --action snapshot-session" \ "Error: Unexpected argument \`bob\`." \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session bob test_failure "snapshot-session action, --max-size without destination" \ "Error: Can't provide a snapshot output max size without a snapshot output destination." \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --max-size 10M test_failure "snapshot-session action, --name without destination" \ "Error: Can't provide a snapshot output name without a snapshot output destination." \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --name hallo test_failure "snapshot-session action, --name with-local-path-instead-of-url" \ "Error: Failed to parse '/something/that/looks/like/a/path' as an URL." \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --name hallo --url /something/that/looks/like/a/path test_failure "snapshot-session action, --name with-net-url-instead-of-path" \ "Error: Failed to parse 'net://8.8.8.8/' as a local path." \ --condition event-rule-matches --domain=user \ --action snapshot-session ze-session --name hallo --path net://8.8.8.8/ # Cleanup stop_lttng_sessiond_notap rm -f "${tmp_stdout}" rm -f "${tmp_stderr}"