3 # Copyright (C) - 2020 EfficiOS, inc
5 # This library is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 # Test the `lttng add-trigger` command line interface.
20 CURDIR
="$(dirname "$0")"
21 TESTDIR
="$CURDIR/../../.."
23 # shellcheck source=../../../utils/utils.sh
24 source "$TESTDIR/utils/utils.sh"
28 FULL_LTTNG_BIN
="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}"
30 # shellcheck disable=SC2119
31 start_lttng_sessiond_notap
33 tmp_stdout
=$
(mktemp
--tmpdir -t test_parse_cli_trigger_stdout.XXXXXX
)
34 tmp_stderr
=$
(mktemp
--tmpdir -t test_parse_cli_trigger_stderr.XXXXXX
)
35 uprobe_elf_binary
="${TESTDIR}/utils/testapp/userspace-probe-elf-binary/.libs/userspace-probe-elf-binary"
37 if [ "$(id -u)" == "0" ]; then
43 function test_success
()
46 local trigger_name
="$2"
49 diag
"${FULL_LTTNG_BIN} add-trigger $*"
51 "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}"
52 ok $?
"${test_name}: exit code is 0"
55 diff -u "${tmp_stdout}" <(echo "Added trigger \`$trigger_name\`.")
56 ok $?
"${test_name}: expected stdout"
58 diff -u "${tmp_stderr}" /dev
/null
59 ok $?
"${test_name}: expected stderr"
62 function test_failure
()
69 diag
"${FULL_LTTNG_BIN} add-trigger $*"
70 "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}"
71 isnt $?
0 "${test_name}: exit code is not 0"
73 diff -u "${tmp_stdout}" /dev
/null
74 ok $?
"${test_name}: expected stdout"
76 diff -u "${tmp_stderr}" <(echo "${error_msg}")
77 ok $?
"${test_name}: expected stderr"
83 local tmp_expected_stdout
85 # Concretely the code used to serialize a trigger object is the same as
86 # the one used by the list command. Here we simply validate that a
87 # simple trigger is correctly generated.
89 tmp_stdout_raw
=$
(mktemp
--tmpdir -t "tmp.${FUNCNAME[0]}_stdout.XXXXXX")
90 tmp_expected_stdout
=$
(mktemp
--tmpdir -t "tmp.${FUNCNAME[0]}_expected_stdout.XXXXXX")
92 diag
"${FULL_LTTNG_BIN} --mi=xml add-trigger"
94 cat > "${tmp_expected_stdout}" <<- EOF
95 <?xml version="1.0" encoding="UTF-8"?>
96 <command xmlns="https://lttng.org/xml/ns/lttng-mi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://lttng.org/xml/ns/lttng-mi https://lttng.org/xml/schemas/lttng-mi/${MI_XSD_MAJOR_VERSION}/lttng-mi-${MI_XSD_MAJOR_VERSION}.${MI_XSD_MINOR_VERSION}.xsd" schemaVersion="${MI_XSD_MAJOR_VERSION}.${MI_XSD_MINOR_VERSION}">
97 <name>add-trigger</name>
100 <name>mi_hohoho</name>
101 <owner_uid>${UID}</owner_uid>
103 <condition_event_rule_matches>
105 <event_rule_user_tracepoint>
106 <name_pattern>some-event-id</name_pattern>
107 </event_rule_user_tracepoint>
109 <capture_descriptors/>
110 </condition_event_rule_matches>
117 <rate_policy_every_n>
118 <interval>1</interval>
119 </rate_policy_every_n>
127 <success>true</success>
131 "${FULL_LTTNG_BIN}" --mi xml add-trigger
--name mi_hohoho \
132 --condition event-rule-matches
--name=some-event-id
--type=user \
133 --action notify
> "${tmp_stdout_raw}" 2> "${tmp_stderr}"
135 ok $?
"add-trigger mi: exit code is 0"
137 # Pretty-fy xml before further test.
138 $XML_PRETTY < "${tmp_stdout_raw}" > "${tmp_stdout}"
140 $MI_VALIDATE "${tmp_stdout}"
141 ok $?
"add-trigger mi is valid"
143 diff -u "${tmp_expected_stdout}" "${tmp_stdout}"
144 ok $?
"mi: expected stdout"
146 diff -u "${tmp_stderr}" /dev
/null
147 ok $?
"mi: expected stderr"
149 rm -f "${tmp_stdout_raw}"
153 test_success
"explicit name" "hohoho" \
155 --condition event-rule-matches
--type=user \
158 # `--condition event-rule-matches` successes
159 test_success
"--condition event-rule-matches some-event --type=user" \
161 --name=some-event
--condition event-rule-matches
--type=user \
164 test_success
"--condition event-rule-matches --type=user" "trigger0" \
165 --condition event-rule-matches
--type=user \
168 test_success
"notify action policies" "test-rate-policy-1" \
169 --name=test-rate-policy-1 \
170 --condition event-rule-matches
--type=user \
172 --rate-policy=every
:55 \
174 --rate-policy=once-after
:55
176 test_success
"start session action policies" "test-rate-policy-2" \
177 --name=test-rate-policy-2 \
178 --condition event-rule-matches
--type=user \
179 --action start-session my_session \
180 --rate-policy=every
:55 \
181 --action start-session my_session \
182 --rate-policy=once-after
:55
184 test_success
"stop session action policies" "test-rate-policy-3" \
185 --name=test-rate-policy-3 \
186 --condition event-rule-matches
--type=user \
187 --action stop-session my_session \
188 --rate-policy=every
:55 \
189 --action stop-session my_session \
190 --rate-policy=once-after
:55
192 test_success
"snapshot session action policies" "test-rate-policy-4" \
193 --name=test-rate-policy-4 \
194 --condition event-rule-matches
--type=user \
195 --action snapshot-session my_session \
196 --rate-policy=every
:55 \
197 --action snapshot-session my_session \
198 --rate-policy=once-after
:55
200 test_success
"rotate session action policies" "test-rate-policy-5" \
201 --name=test-rate-policy-5 \
202 --condition event-rule-matches
--type=user \
203 --action rotate-session my_session \
204 --rate-policy=every
:55 \
205 --action rotate-session my_session \
206 --rate-policy=once-after
:55
208 test_success
"--log-level single level" "trigger1" \
209 --condition event-rule-matches
--type=user
--log-level=INFO \
212 test_success
"--log-level range open max" "trigger2" \
213 --condition event-rule-matches
--type=user
--log-level=INFO.. \
216 test_success
"--log-level range any" "trigger3" \
217 --condition event-rule-matches
--type=user
--log-level=.. \
220 test_success
"--exclude-name one" "trigger4" \
221 --condition event-rule-matches
--type=user
--name='bernard*' --exclude-name=bernard-lermite \
224 test_success
"--exclude-name two" "trigger5" \
225 --condition event-rule-matches
--type=user
--name='jean-*' --exclude-name jean-chretien
-x jean-charest \
228 skip
$ist_root "non-root user: skipping kprobe tests" 18 ||
{
231 for type in kprobe kernel
:kprobe
; do
232 test_success
"--condition event-rule-matches probe by symbol" "kprobe-trigger-$i" \
233 --name="kprobe-trigger-$i" \
234 --condition event-rule-matches
--type=$type --location=lttng_channel_enable
--event-name=my_channel_enable \
237 channel_enable_addr
=$
(grep ' t lttng_channel_enable\s\[lttng_tracer\]$' /proc
/kallsyms | cut
-f 1 -d ' ')
238 channel_disable_addr
=$
(grep ' t lttng_channel_disable\s\[lttng_tracer\]$' /proc
/kallsyms | cut
-f 1 -d ' ')
240 # We need to find a valid offset.
243 if [[ 0x
$channel_enable_addr -lt 0x
$channel_disable_addr ]]; then
244 base_symbol
="lttng_channel_enable"
245 offset
=$
(( 0x
$channel_disable_addr - 0x
$channel_enable_addr ))
247 base_symbol
="lttng_channel_disable"
248 offset
=$
(( 0x
$channel_enable_addr - 0x
$channel_disable_addr ))
251 offset_hex
="0x$(printf '%x' $offset)"
254 test_success
"--condition event-rule-matches probe by symbol with offset" "kprobe-trigger-$((i+1))" \
255 --name="kprobe-trigger-$((i+1))" \
256 --condition event-rule-matches
--type=$type --location="${base_symbol}+${offset_hex}" --event-name=my_
$base_symbol \
259 test_success
"--condition event-rule-matches probe by address" "kprobe-trigger-$((i+2))" \
260 --name="kprobe-trigger-$((i+2))" \
261 --condition event-rule-matches
--type=$type --location="0x${channel_enable_addr}" --event-name=my_channel_enable \
267 skip
$ist_root "non-root user: skipping uprobe tests" 6 ||
{
268 test_success
"--condition event-rule-matches uprobe" "uprobe-trigger-0" \
269 --name="uprobe-trigger-0" \
270 --condition event-rule-matches
--type=kernel
:uprobe
--location=${uprobe_elf_binary}:test_function
--event-name=ma-probe \
273 test_success
"--condition event-rule-matches uprobe with elf prefix" "uprobe-trigger-1" \
274 --name="uprobe-trigger-1" \
275 --condition event-rule-matches
--type=kernel
:uprobe
--location=elf
:${uprobe_elf_binary}:test_function
--event-name=ma-probe-2 \
279 skip
$ist_root "non-root user: skipping syscall tests" 30 ||
{
280 test_success
"--condition event-rule-matches one syscall" "syscall-trigger-0" \
281 --name="syscall-trigger-0" \
282 --condition event-rule-matches
--type=syscall
--name=open \
285 test_success
"--condition event-rule-matches all syscalls" "syscall-trigger-1" \
286 --name="syscall-trigger-1" \
287 --condition event-rule-matches
--type=syscall \
290 test_success
"--condition event-rule-matches one syscall with filter" "syscall-trigger-2" \
291 --name="syscall-trigger-2" \
292 --condition event-rule-matches
--type=syscall
--filter 'a > 2' --name=open \
294 test_success
"--condition event-rule-matches one syscall:entry" "syscall-trigger-3" \
295 --name="syscall-trigger-3" \
296 --condition event-rule-matches
--type=syscall
:entry
--name=open \
298 test_success
"--condition event-rule-matches one syscall:exit" "syscall-trigger-4" \
299 --name="syscall-trigger-4" \
300 --condition event-rule-matches
--type=syscall
:exit --name=open \
302 test_success
"--condition event-rule-matches one syscall:entry-exit" "syscall-trigger-5" \
303 --name="syscall-trigger-5" \
304 --condition event-rule-matches
--type=syscall
:entry
+exit --name=open \
307 # Same thing but with "kernel:syscall" type instead:
308 test_success
"--condition event-rule-matches one syscall" "syscall-trigger-6" \
309 --name="syscall-trigger-6" \
310 --condition event-rule-matches
--type=kernel
:syscall
--name=open \
313 test_success
"--condition event-rule-matches one kernel:syscall:entry" "syscall-trigger-7" \
314 --name="syscall-trigger-7" \
315 --condition event-rule-matches
--type=kernel
:syscall
:entry
--name=open \
317 test_success
"--condition event-rule-matches one kernel:syscall:exit" "syscall-trigger-8" \
318 --name="syscall-trigger-8" \
319 --condition event-rule-matches
--type=kernel
:syscall
:exit --name=open \
321 test_success
"--condition event-rule-matches one kernel:syscall:entry-exit" "syscall-trigger-9" \
322 --name="syscall-trigger-9" \
323 --condition event-rule-matches
--type=kernel
:syscall
:entry
+exit --name=open \
328 # `--action notify` successes
329 test_success
"--action notify" "notify-1" \
331 --condition event-rule-matches
--type=user \
334 test_success
"--action notify --capture foo" "notify-2" \
336 --condition event-rule-matches
--type=user \
337 --capture foo
--action notify
339 test_success
"--action notify --capture foo[2]" "notify-3" \
341 --condition event-rule-matches
--type=user \
342 --capture 'foo[2]' --action notify
344 test_success
'--action notify --capture $ctx.foo' "notify-4" \
346 --condition event-rule-matches
--type=user \
347 --capture '$ctx.foo' --action notify
349 test_success
'--action notify --capture $ctx.foo[2]' "notify-5" \
351 --condition event-rule-matches
--type=user \
352 --capture '$ctx.foo[2]' --action notify
354 test_success
'--action notify --capture $app.prov:type' "notify-6" \
356 --condition event-rule-matches
--type=user \
357 --capture '$app.prov:type' --action notify
359 test_success
'--action notify --capture $app.prov:type[2]' "notify-7" \
361 --condition event-rule-matches
--type=user \
362 --capture '$app.prov:type[2]' --action notify
364 test_success
'--action notify multiple captures' "notify-8" \
366 --condition event-rule-matches
--type=user \
367 --capture foo
--capture '$app.hello:world' --action notify
369 # `--action start-session` successes
370 test_success
"--action start-session" "notify-9" \
372 --condition event-rule-matches
--type=user \
373 --action start-session ze-session
375 # `--action stop-session` successes
376 test_success
"--action stop-session foo" "notify-10"\
378 --condition event-rule-matches
--type=user \
379 --action stop-session ze-session
381 # `--action rotate-session` successes
382 test_success
"--action rotate-session foo" "notify-11"\
384 --condition event-rule-matches
--type=user \
385 --action rotate-session ze-session
387 # `--action snapshot-session` successes
388 test_success
"--action snapshot-session foo" "notify-12"\
390 --condition event-rule-matches
--type=user \
391 --action snapshot-session ze-session
393 test_success
"--action snapshot-session with file URI" "notify-13"\
395 --condition event-rule-matches
--type=user \
396 --action snapshot-session ze-session
--path /hello
398 test_success
"--action snapshot-session with net URI" "notify-14"\
400 --condition event-rule-matches
--type=user \
401 --action snapshot-session ze-session
--url net
://1.2.3.4
403 test_success
"--action snapshot-session with ctrl/data URIs" "notify-15"\
405 --condition event-rule-matches
--type=user \
406 --action snapshot-session ze-session
--ctrl-url=tcp
://1.2.3.4:1234 --data-url=tcp
://1.2.3.4:1235
409 test_failure
"no args" "Error: Missing --condition."
411 test_failure
"unknown option" \
412 "Error: Unknown option \`--hello\`" \
415 test_failure
"missing --action" \
416 "Error: Need at least one --action." \
417 --condition event-rule-matches
--type=user
419 test_failure
"two --condition" \
420 "Error: A --condition was already given." \
421 --condition event-rule-matches
--name=aaa
--type=user \
422 --condition event-rule-matches
--name=bbb
--type=user \
425 test_failure
"missing argument to --name" \
426 "Error: While parsing argument #1 (\`--name\`): Missing required argument for option \`--name\`" \
429 for cmd
in rate-policy
=once-after rate-policy
=every
; do
430 test_failure
"missing argument to --${cmd}" \
431 "Error: Rate policy format is invalid." \
432 --condition event-rule-matches
--type=user
--action notify \
435 test_failure
"invalid argument to --${cmd}: non-digit character" \
436 "Error: Failed to parse rate policy value \`123bob\` as an integer." \
437 --condition event-rule-matches
--type=user
--action notify \
440 test_failure
"invalid argument to --${cmd}: empty string" \
441 "Error: Failed to parse rate policy value \`\` as an integer." \
442 --condition event-rule-matches
--type=user
--action notify \
446 test_failure
"invalid argument to --rate-policy: unknown policy type" \
447 "Error: Rate policy type \`bob\` unknown." \
448 --condition event-rule-matches
--type=user
--action notify \
449 --rate-policy=bob
:123
451 # `--condition` failures
452 test_failure
"missing args after --condition" \
453 "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \
455 test_failure
"unknown --condition" \
456 "Error: Unknown condition name 'zoofest'" \
459 # `--condition event-rule-matches` failures
460 test_failure
"missing args after --condition event-rule-matches" \
461 "Error: Need at least one --action." \
462 --condition event-rule-matches
--type=user
464 test_failure
"extra args after --condition event-rule-matches" \
465 "Error: Unexpected argument 'bozo'" \
466 --condition event-rule-matches
--type=user bozo
468 test_failure
"--log-level unknown level" \
469 "Error: Failed to parse log level string \`FOO\`." \
470 --condition event-rule-matches
--type=user
--log-level=FOO
472 for type in kprobe kernel
:kprobe
; do
473 test_failure
"--condition event-rule-matches: --name with --type=$type" \
474 "Error: Can't use --name with kernel kprobe event rules." \
475 --condition event-rule-matches
--type=$type --location=do_sys_open
--name='hello'
478 test_failure
"--condition event-rule-matches: --location with user tracepoint event rule" \
479 "Error: Can't use --location with user tracepoint event rules." \
480 --condition event-rule-matches
--type=user
--location='hello'
482 test_failure
"--condition event-rule-matches: --event-name with user tracepoint event rule" \
483 "Error: Can't use --event-name with user tracepoint event rules." \
484 --condition event-rule-matches
--type=user
--event-name='hello'
486 test_failure
"--condition event-rule-matches: extra argument with --type=kernel:uprobe" \
487 "Error: Unexpected argument 'hello'" \
488 --condition event-rule-matches
--type=$type --location=${uprobe_elf_binary}:test_failure hello
490 test_failure
"--condition event-rule-matches: extra argument with --type=syscall" \
491 "Error: Unexpected argument 'open'" \
492 --condition event-rule-matches
--type=syscall open
494 test_failure
"--condition event-rule-matches: --type=syscall:nope" \
495 "Error: Failed to parse syscall type 'syscall:nope'." \
496 --condition event-rule-matches
--type=syscall
:nope \
499 test_failure
"--exclude-name with non-glob name" \
500 "Error: Event jean: Exclusions can only be used with a globbing pattern" \
501 --condition event-rule-matches
--type=user
--name='jean' --exclude-name jean-chretien \
504 test_failure
"--condition event-rule-matches --capture: missing argument (end of arg list)" \
505 'Error: While parsing argument #2 (`--capture`): Missing required argument for option `--capture`' \
507 --condition event-rule-matches
--type=user
--capture
509 test_failure
"--condition event-rule-matches --capture: missing argument (before another option)" \
510 'Error: While parsing expression `--action`: Unary operators are not allowed in capture expressions.' \
511 --condition event-rule-matches
--type=user
--capture \
514 test_failure
"--condition event-rule-matches --capture: binary operator" \
515 'Error: While parsing expression `foo == 2`: Binary operators are not allowed in capture expressions.' \
516 --condition event-rule-matches
--type=user \
517 --capture 'foo == 2' --action notify
519 test_failure
"--condition event-rule-matches --capture: unary operator" \
520 'Error: While parsing expression `!foo`: Unary operators are not allowed in capture expressions.' \
521 --condition event-rule-matches
--type=user \
522 --capture '!foo' --action notify
524 test_failure
"--condition event-rule-matches --capture: logical operator" \
525 'Error: While parsing expression `foo || bar`: Logical operators are not allowed in capture expressions.' \
526 --condition event-rule-matches
--type=user \
527 --capture 'foo || bar' --action notify
529 test_failure
"--condition event-rule-matches --capture: accessing a sub-field" \
530 'Error: While parsing expression `foo.bar`: Capturing subfields is not supported.' \
531 --condition event-rule-matches
--type=user \
532 --capture 'foo.bar' --action notify
534 test_failure
"--condition event-rule-matches --capture: accessing the sub-field of an array element" \
535 'Error: While parsing expression `foo[3].bar`: Capturing subfields is not supported.' \
536 --condition event-rule-matches
--type=user \
537 --capture 'foo[3].bar' --action notify
539 test_failure
"--condition event-rule-matches --capture: missing colon in app-specific context field" \
540 'Error: Invalid app-specific context field name: missing colon in `foo`.' \
541 --condition event-rule-matches
--type=user \
542 --capture '$app.foo' --action notify
544 test_failure
"--condition event-rule-matches --capture: missing colon in app-specific context field" \
545 'Error: Invalid app-specific context field name: missing type name after colon in `foo:`.' \
546 --condition event-rule-matches
--type=user \
547 --capture '$app.foo:' --action notify
549 # `--action` failures
550 test_failure
"missing args after --action" \
551 "Error: While parsing argument #1 (\`--action\`): Missing required argument for option \`--action\`" \
552 --condition event-rule-matches
--type=user \
555 # `--action notify` failures
556 test_failure
"extra arg after --action notify" \
557 "Error: Unexpected argument \`bob\`." \
558 --condition event-rule-matches
--type=user \
561 # `--action start-session` failures
562 test_failure
"missing arg after --action start-session" \
563 "Error: Missing session name." \
564 --condition event-rule-matches
--type=user \
565 --action start-session
566 test_failure
"extra arg after --action start-session" \
567 "Error: Unexpected argument \`bob\`." \
568 --condition event-rule-matches
--type=user \
569 --action start-session ze-session bob
571 # `--action stop-session` failures
572 test_failure
"missing arg after --action stop-session" \
573 "Error: Missing session name." \
574 --condition event-rule-matches
--type=user \
575 --action stop-session
576 test_failure
"extra arg after --action stop-session" \
577 "Error: Unexpected argument \`bob\`." \
578 --condition event-rule-matches
--type=user \
579 --action stop-session ze-session bob
581 # `--action rotate-session` failures
582 test_failure
"missing arg after --action rotate-session" \
583 "Error: Missing session name." \
584 --condition event-rule-matches
--type=user \
585 --action rotate-session
586 test_failure
"extra arg after --action rotate-session" \
587 "Error: Unexpected argument \`bob\`." \
588 --condition event-rule-matches
--type=user \
589 --action rotate-session ze-session bob
591 # `--action snapshot-session` failures
592 test_failure
"missing arg after --action snapshot-session" \
593 "Error: Missing session name." \
594 --condition event-rule-matches
--type=user \
595 --action snapshot-session
596 test_failure
"extra arg after --action snapshot-session" \
597 "Error: Unexpected argument \`bob\`." \
598 --condition event-rule-matches
--type=user \
599 --action snapshot-session ze-session bob
600 test_failure
"snapshot-session action, --max-size without destination" \
601 "Error: Can't provide a snapshot output max size without a snapshot output destination." \
602 --condition event-rule-matches
--type=user \
603 --action snapshot-session ze-session
--max-size 10M
604 test_failure
"snapshot-session action, --name without destination" \
605 "Error: Can't provide a snapshot output name without a snapshot output destination." \
606 --condition event-rule-matches
--type=user \
607 --action snapshot-session ze-session
--name hallo
608 test_failure
"snapshot-session action, --name with-local-path-instead-of-url" \
609 "Error: Failed to parse '/something/that/looks/like/a/path' as an URL." \
610 --condition event-rule-matches
--type=user \
611 --action snapshot-session ze-session
--name hallo
--url /something
/that
/looks
/like
/a
/path
612 test_failure
"snapshot-session action, --name with-net-url-instead-of-path" \
613 "Error: Failed to parse 'net://8.8.8.8/' as a local path." \
614 --condition event-rule-matches
--type=user \
615 --action snapshot-session ze-session
--name hallo
--path net
://8.8.8.8/
620 stop_lttng_sessiond_notap
621 rm -f "${tmp_stdout}"
622 rm -f "${tmp_stderr}"