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
()
48 diag
"${FULL_LTTNG_BIN} add-trigger $*"
50 "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}"
51 ok $?
"${test_name}: exit code is 0"
54 diff -u "${tmp_stdout}" <(echo "Trigger registered successfully.")
55 ok $?
"${test_name}: expected stdout"
57 diff -u "${tmp_stderr}" /dev
/null
58 ok $?
"${test_name}: expected stderr"
61 function test_failure
()
68 diag
"${FULL_LTTNG_BIN} add-trigger $*"
69 "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}"
70 isnt $?
0 "${test_name}: exit code is not 0"
72 diff -u "${tmp_stdout}" /dev
/null
73 ok $?
"${test_name}: expected stdout"
75 diff -u "${tmp_stderr}" <(echo "${error_msg}")
76 ok $?
"${test_name}: expected stderr"
82 local tmp_expected_stdout
84 # Concretely the code used to serialize a trigger object is the same as
85 # the one used by the list command. Here we simply validate that a
86 # simple trigger is correctly generated.
88 tmp_stdout_raw
=$
(mktemp
--tmpdir -t "tmp.${FUNCNAME[0]}_stdout.XXXXXX")
89 tmp_expected_stdout
=$
(mktemp
--tmpdir -t "tmp.${FUNCNAME[0]}_expected_stdout.XXXXXX")
91 diag
"${FULL_LTTNG_BIN} --mi=xml add-trigger"
93 cat > "${tmp_expected_stdout}" <<- EOF
94 <?xml version="1.0" encoding="UTF-8"?>
95 <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/4/lttng-mi-4.0.xsd" schemaVersion="4.0">
96 <name>add-trigger</name>
99 <name>mi_hohoho</name>
100 <owner_uid>${UID}</owner_uid>
102 <condition_event_rule_matches>
104 <event_rule_user_tracepoint>
105 <name_pattern>some-event-id</name_pattern>
106 </event_rule_user_tracepoint>
108 <capture_descriptors/>
109 </condition_event_rule_matches>
116 <rate_policy_every_n>
117 <interval>1</interval>
118 </rate_policy_every_n>
126 <success>true</success>
130 "${FULL_LTTNG_BIN}" --mi xml add-trigger
--name mi_hohoho \
131 --condition event-rule-matches
--name=some-event-id
--type=user \
132 --action notify
> "${tmp_stdout_raw}" 2> "${tmp_stderr}"
134 ok $?
"add-trigger mi: exit code is 0"
136 # Pretty-fy xml before further test.
137 $XML_PRETTY_BIN < "${tmp_stdout_raw}" > "${tmp_stdout}"
139 $XML_VALIDATE_BIN "${MI_XSD_PATH}" "${tmp_stdout}"
140 ok $?
"add-trigger mi is valid"
142 diff -u "${tmp_expected_stdout}" "${tmp_stdout}"
143 ok $?
"mi: expected stdout"
145 diff -u "${tmp_stderr}" /dev
/null
146 ok $?
"mi: expected stderr"
148 rm -f "${tmp_stdout_raw}"
152 test_success
"explicit name" \
154 --condition event-rule-matches
--name=some-event-id
--type=user \
157 # `--condition event-rule-matches` successes
158 test_success
"--condition event-rule-matches some-event --type=user" \
159 --condition event-rule-matches
--name=some-event
--type=user \
162 test_success
"--condition event-rule-matches --type=user" \
163 --condition event-rule-matches
--type=user \
166 test_success
"notify action polices" \
167 --condition event-rule-matches
--type=user
--name=test-rate-policy \
169 --rate-policy=every
:55 \
171 --rate-policy=once-after
:55
173 test_success
"start session action polices" \
174 --condition event-rule-matches
--type=user
--name=test-rate-policy \
175 --action start-session my_session \
176 --rate-policy=every
:55 \
177 --action start-session my_session \
178 --rate-policy=once-after
:55
180 test_success
"stop session action polices" \
181 --condition event-rule-matches
--type=user
--name=test-rate-policy \
182 --action stop-session my_session \
183 --rate-policy=every
:55 \
184 --action stop-session my_session \
185 --rate-policy=once-after
:55
187 test_success
"snapshot session action polices" \
188 --condition event-rule-matches
--type=user
--name=test-rate-policy \
189 --action snapshot-session my_session \
190 --rate-policy=every
:55 \
191 --action snapshot-session my_session \
192 --rate-policy=once-after
:55
194 test_success
"rotate session action polices" \
195 --condition event-rule-matches
--type=user
--name=test-rate-policy \
196 --action rotate-session my_session \
197 --rate-policy=every
:55 \
198 --action rotate-session my_session \
199 --rate-policy=once-after
:55
201 test_success
"--log-level single level" \
202 --condition event-rule-matches
--type=user
--log-level=INFO \
205 test_success
"--log-level range open max" \
206 --condition event-rule-matches
--type=user
--log-level=INFO.. \
209 test_success
"--log-level range any" \
210 --condition event-rule-matches
--type=user
--log-level=.. \
213 test_success
"--exclude-name one" \
214 --condition event-rule-matches
--type=user
--name='bernard*' --exclude-name=bernard-lermite \
217 test_success
"--exclude-name two" \
218 --condition event-rule-matches
--type=user
--name='jean-*' --exclude-name jean-chretien
-x jean-charest \
221 skip
$ist_root "non-root user: skipping kprobe tests" 18 ||
{
222 for type in kprobe kernel
:kprobe
; do
223 test_success
"--condition event-rule-matches probe by symbol" \
224 --condition event-rule-matches
--type=$type --location=lttng_channel_enable
--event-name=my_channel_enable \
227 channel_enable_addr
=$
(grep ' t lttng_channel_enable\s\[lttng_tracer\]$' /proc
/kallsyms | cut
-f 1 -d ' ')
228 channel_disable_addr
=$
(grep ' t lttng_channel_disable\s\[lttng_tracer\]$' /proc
/kallsyms | cut
-f 1 -d ' ')
230 # We need to find a valid offset.
233 if [[ 0x
$channel_enable_addr -lt 0x
$channel_disable_addr ]]; then
234 base_symbol
="lttng_channel_enable"
235 offset
=$
(( 0x
$channel_disable_addr - 0x
$channel_enable_addr ))
237 base_symbol
="lttng_channel_disable"
238 offset
=$
(( 0x
$channel_enable_addr - 0x
$channel_disable_addr ))
241 offset_hex
="0x$(printf '%x' $offset)"
243 test_success
"--condition event-rule-matches probe by symbol with offset" \
244 --condition event-rule-matches
--type=$type --location="${base_symbol}+${offset_hex}" --event-name=my_
$base_symbol \
247 test_success
"--condition event-rule-matches probe by address" \
248 --condition event-rule-matches
--type=$type --location="0x${channel_enable_addr}" --event-name=my_channel_enable \
253 skip
$ist_root "non-root user: skipping uprobe tests" 6 ||
{
254 test_success
"--condition event-rule-matches uprobe" \
255 --condition event-rule-matches
--type=kernel
:uprobe
--location=${uprobe_elf_binary}:test_function
--event-name=ma-probe \
258 test_success
"--condition event-rule-matches uprobe with elf prefix" \
259 --condition event-rule-matches
--type=kernel
:uprobe
--location=elf
:${uprobe_elf_binary}:test_function
--event-name=ma-probe-2 \
263 skip
$ist_root "non-root user: skipping syscall tests" 30 ||
{
264 test_success
"--condition event-rule-matches one syscall" \
265 --condition event-rule-matches
--type=syscall
--name=open \
268 test_success
"--condition event-rule-matches all syscalls" \
269 --condition event-rule-matches
--type=syscall \
272 test_success
"--condition event-rule-matches one syscall with filter" \
273 --condition event-rule-matches
--type=syscall
--filter 'a > 2' --name=open \
275 test_success
"--condition event-rule-matches one syscall:entry" \
276 --condition event-rule-matches
--type=syscall
:entry
--name=open \
278 test_success
"--condition event-rule-matches one syscall:exit" \
279 --condition event-rule-matches
--type=syscall
:exit --name=open \
281 test_success
"--condition event-rule-matches one syscall:entry-exit" \
282 --condition event-rule-matches
--type=syscall
:entry
+exit --name=open \
285 # Same thing but with "kernel:syscall" type instead:
286 test_success
"--condition event-rule-matches one syscall" \
287 --condition event-rule-matches
--type=kernel
:syscall
--name=open \
290 test_success
"--condition event-rule-matches one kernel:syscall:entry" \
291 --condition event-rule-matches
--type=kernel
:syscall
:entry
--name=open \
293 test_success
"--condition event-rule-matches one kernel:syscall:exit" \
294 --condition event-rule-matches
--type=kernel
:syscall
:exit --name=open \
296 test_success
"--condition event-rule-matches one kernel:syscall:entry-exit" \
297 --condition event-rule-matches
--type=kernel
:syscall
:entry
+exit --name=open \
302 # `--action notify` successes
303 test_success
"--action notify" \
304 --condition event-rule-matches
--type=user \
307 test_success
"--action notify --capture foo" \
308 --condition event-rule-matches
--type=user \
309 --capture foo
--action notify
311 test_success
"--action notify --capture foo[2]" \
312 --condition event-rule-matches
--type=user \
313 --capture 'foo[2]' --action notify
315 test_success
'--action notify --capture $ctx.foo' \
316 --condition event-rule-matches
--type=user \
317 --capture '$ctx.foo' --action notify
319 test_success
'--action notify --capture $ctx.foo[2]' \
320 --condition event-rule-matches
--type=user \
321 --capture '$ctx.foo[2]' --action notify
323 test_success
'--action notify --capture $app.prov:type' \
324 --condition event-rule-matches
--type=user \
325 --capture '$app.prov:type' --action notify
327 test_success
'--action notify --capture $app.prov:type[2]' \
328 --condition event-rule-matches
--type=user \
329 --capture '$app.prov:type[2]' --action notify
331 test_success
'--action notify multiple captures' \
332 --condition event-rule-matches
--type=user \
333 --capture foo
--capture '$app.hello:world' --action notify
335 # `--action start-session` successes
336 test_success
"--action start-session" \
337 --condition event-rule-matches
--type=user \
338 --action start-session ze-session
340 # `--action stop-session` successes
341 test_success
"--action stop-session foo" \
342 --condition event-rule-matches
--type=user \
343 --action stop-session ze-session
345 # `--action rotate-session` successes
346 test_success
"--action rotate-session foo" \
347 --condition event-rule-matches
--type=user \
348 --action rotate-session ze-session
350 # `--action snapshot-session` successes
351 test_success
"--action snapshot-session foo" \
352 --condition event-rule-matches
--type=user \
353 --action snapshot-session ze-session
355 test_success
"--action snapshot-session with file URI" \
356 --condition event-rule-matches
--type=user \
357 --action snapshot-session ze-session
--path /hello
359 test_success
"--action snapshot-session with net URI" \
360 --condition event-rule-matches
--type=user \
361 --action snapshot-session ze-session
--url net
://1.2.3.4
363 test_success
"--action snapshot-session with ctrl/data URIs" \
364 --condition event-rule-matches
--type=user \
365 --action snapshot-session ze-session
--ctrl-url=tcp
://1.2.3.4:1234 --data-url=tcp
://1.2.3.4:1235
368 test_failure
"no args" "Error: Missing --condition."
370 test_failure
"unknown option" \
371 "Error: Unknown option \`--hello\`" \
374 test_failure
"missing --action" \
375 "Error: Need at least one --action." \
376 --condition event-rule-matches
--type=user
378 test_failure
"two --condition" \
379 "Error: A --condition was already given." \
380 --condition event-rule-matches
--name=aaa
--type=user \
381 --condition event-rule-matches
--name=bbb
--type=user \
384 test_failure
"missing argument to --name" \
385 "Error: While parsing argument #1 (\`--name\`): Missing required argument for option \`--name\`" \
388 for cmd
in rate-policy
=once-after rate-policy
=every
; do
389 test_failure
"missing argument to --${cmd}" \
390 "Error: Rate policy format is invalid." \
391 --condition event-rule-matches
--type=user
--action notify \
394 test_failure
"invalid argument to --${cmd}: non-digit character" \
395 "Error: Failed to parse rate policy value \`123bob\` as an integer." \
396 --condition event-rule-matches
--type=user
--action notify \
399 test_failure
"invalid argument to --${cmd}: empty string" \
400 "Error: Failed to parse rate policy value \`\` as an integer." \
401 --condition event-rule-matches
--type=user
--action notify \
405 test_failure
"invalid argument to --rate-policy: unknown policy type" \
406 "Error: Rate policy type \`bob\` unknown." \
407 --condition event-rule-matches
--type=user
--action notify \
408 --rate-policy=bob
:123
410 # `--condition` failures
411 test_failure
"missing args after --condition" \
412 "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \
414 test_failure
"unknown --condition" \
415 "Error: Unknown condition name 'zoofest'" \
418 # `--condition event-rule-matches` failures
419 test_failure
"missing args after --condition event-rule-matches" \
420 "Error: Need at least one --action." \
421 --condition event-rule-matches
423 test_failure
"extra args after --condition event-rule-matches" \
424 "Error: Unexpected argument 'bozo'" \
425 --condition event-rule-matches
--type=user bozo
427 test_failure
"--log-level unknown level" \
428 "Error: Failed to parse log level string \`FOO\`." \
429 --condition event-rule-matches
--type=user
--log-level=FOO
431 for type in kprobe kernel
:kprobe
; do
432 test_failure
"--condition event-rule-matches: --name with --type=$type" \
433 "Error: Can't use --name with kernel kprobe event rules." \
434 --condition event-rule-matches
--type=$type --location=do_sys_open
--name='hello'
437 test_failure
"--condition event-rule-matches: --location with user tracepoint event rule" \
438 "Error: Can't use --location with user tracepoint event rules." \
439 --condition event-rule-matches
--type=user
--location='hello'
441 test_failure
"--condition event-rule-matches: --event-name with user tracepoint event rule" \
442 "Error: Can't use --event-name with user tracepoint event rules." \
443 --condition event-rule-matches
--type=user
--event-name='hello'
445 test_failure
"--condition event-rule-matches: extra argument with --type=kernel:uprobe" \
446 "Error: Unexpected argument 'hello'" \
447 --condition event-rule-matches
--type=$type --location=${uprobe_elf_binary}:test_failure hello
449 test_failure
"--condition event-rule-matches: extra argument with --type=syscall" \
450 "Error: Unexpected argument 'open'" \
451 --condition event-rule-matches
--type=syscall open
453 test_failure
"--condition event-rule-matches: --type=syscall:nope" \
454 "Error: Failed to parse syscall type 'syscall:nope'." \
455 --condition event-rule-matches
--type=syscall
:nope \
458 test_failure
"--exclude-name with non-glob name" \
459 "Error: Event jean: Exclusions can only be used with a globbing pattern" \
460 --condition event-rule-matches
--type=user
--name='jean' --exclude-name jean-chretien \
463 test_failure
"--condition event-rule-matches --capture: missing argument (end of arg list)" \
464 'Error: While parsing argument #2 (`--capture`): Missing required argument for option `--capture`' \
466 --condition event-rule-matches
--type=user
--capture
468 test_failure
"--condition event-rule-matches --capture: missing argument (before another option)" \
469 'Error: While parsing expression `--action`: Unary operators are not allowed in capture expressions.' \
470 --condition event-rule-matches
--type=user
--capture \
473 test_failure
"--condition event-rule-matches --capture: binary operator" \
474 'Error: While parsing expression `foo == 2`: Binary operators are not allowed in capture expressions.' \
475 --condition event-rule-matches
--type=user \
476 --capture 'foo == 2' --action notify
478 test_failure
"--condition event-rule-matches --capture: unary operator" \
479 'Error: While parsing expression `!foo`: Unary operators are not allowed in capture expressions.' \
480 --condition event-rule-matches
--type=user \
481 --capture '!foo' --action notify
483 test_failure
"--condition event-rule-matches --capture: logical operator" \
484 'Error: While parsing expression `foo || bar`: Logical operators are not allowed in capture expressions.' \
485 --condition event-rule-matches
--type=user \
486 --capture 'foo || bar' --action notify
488 test_failure
"--condition event-rule-matches --capture: accessing a sub-field" \
489 'Error: While parsing expression `foo.bar`: Capturing subfields is not supported.' \
490 --condition event-rule-matches
--type=user \
491 --capture 'foo.bar' --action notify
493 test_failure
"--condition event-rule-matches --capture: accessing the sub-field of an array element" \
494 'Error: While parsing expression `foo[3].bar`: Capturing subfields is not supported.' \
495 --condition event-rule-matches
--type=user \
496 --capture 'foo[3].bar' --action notify
498 test_failure
"--condition event-rule-matches --capture: missing colon in app-specific context field" \
499 'Error: Invalid app-specific context field name: missing colon in `foo`.' \
500 --condition event-rule-matches
--type=user \
501 --capture '$app.foo' --action notify
503 test_failure
"--condition event-rule-matches --capture: missing colon in app-specific context field" \
504 'Error: Invalid app-specific context field name: missing type name after colon in `foo:`.' \
505 --condition event-rule-matches
--type=user \
506 --capture '$app.foo:' --action notify
508 # `--action` failures
509 test_failure
"missing args after --action" \
510 "Error: While parsing argument #1 (\`--action\`): Missing required argument for option \`--action\`" \
511 --condition event-rule-matches
--type=user \
514 # `--action notify` failures
515 test_failure
"extra arg after --action notify" \
516 "Error: Unexpected argument \`bob\`." \
517 --condition event-rule-matches
--type=user \
520 # `--action start-session` failures
521 test_failure
"missing arg after --action start-session" \
522 "Error: Missing session name." \
523 --condition event-rule-matches
--type=user \
524 --action start-session
525 test_failure
"extra arg after --action start-session" \
526 "Error: Unexpected argument \`bob\`." \
527 --condition event-rule-matches
--type=user \
528 --action start-session ze-session bob
530 # `--action stop-session` failures
531 test_failure
"missing arg after --action stop-session" \
532 "Error: Missing session name." \
533 --condition event-rule-matches
--type=user \
534 --action stop-session
535 test_failure
"extra arg after --action stop-session" \
536 "Error: Unexpected argument \`bob\`." \
537 --condition event-rule-matches
--type=user \
538 --action stop-session ze-session bob
540 # `--action rotate-session` failures
541 test_failure
"missing arg after --action rotate-session" \
542 "Error: Missing session name." \
543 --condition event-rule-matches
--type=user \
544 --action rotate-session
545 test_failure
"extra arg after --action rotate-session" \
546 "Error: Unexpected argument \`bob\`." \
547 --condition event-rule-matches
--type=user \
548 --action rotate-session ze-session bob
550 # `--action snapshot-session` failures
551 test_failure
"missing arg after --action snapshot-session" \
552 "Error: Missing session name." \
553 --condition event-rule-matches
--type=user \
554 --action snapshot-session
555 test_failure
"extra arg after --action snapshot-session" \
556 "Error: Unexpected argument \`bob\`." \
557 --condition event-rule-matches
--type=user \
558 --action snapshot-session ze-session bob
559 test_failure
"snapshot-session action, --max-size without destination" \
560 "Error: Can't provide a snapshot output max size without a snapshot output destination." \
561 --condition event-rule-matches
--type=user \
562 --action snapshot-session ze-session
--max-size 10M
563 test_failure
"snapshot-session action, --name without destination" \
564 "Error: Can't provide a snapshot output name without a snapshot output destination." \
565 --condition event-rule-matches
--type=user \
566 --action snapshot-session ze-session
--name hallo
567 test_failure
"snapshot-session action, --name with-local-path-instead-of-url" \
568 "Error: Failed to parse '/something/that/looks/like/a/path' as an URL." \
569 --condition event-rule-matches
--type=user \
570 --action snapshot-session ze-session
--name hallo
--url /something
/that
/looks
/like
/a
/path
571 test_failure
"snapshot-session action, --name with-net-url-instead-of-path" \
572 "Error: Failed to parse 'net://8.8.8.8/' as a local path." \
573 --condition event-rule-matches
--type=user \
574 --action snapshot-session ze-session
--name hallo
--path net
://8.8.8.8/
579 stop_lttng_sessiond_notap
580 rm -f "${tmp_stdout}"
581 rm -f "${tmp_stderr}"