cc873bef3aaae48f21b2c249819678fae574b404
[lttng-tools.git] / tests / regression / tools / trigger / test_add_trigger_cli
1 #!/bin/bash
2 #
3 # Copyright (C) - 2020 EfficiOS, inc
4 #
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.
8 #
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
12 # details.
13 #
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
17
18 # Test the `lttng add-trigger` command line interface.
19
20 CURDIR="$(dirname "$0")"
21 TESTDIR="$CURDIR/../../.."
22
23 # shellcheck source=../../../utils/utils.sh
24 source "$TESTDIR/utils/utils.sh"
25
26 plan_tests 286
27
28 FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}"
29
30 # shellcheck disable=SC2119
31 start_lttng_sessiond_notap
32
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"
36
37 if [ "$(id -u)" == "0" ]; then
38 ist_root=1
39 else
40 ist_root=0
41 fi
42
43 function test_success ()
44 {
45 local test_name="$1"
46 shift
47
48 diag "${FULL_LTTNG_BIN} add-trigger $*"
49 set -x
50 "${FULL_LTTNG_BIN}" add-trigger "$@" > "${tmp_stdout}" 2> "${tmp_stderr}"
51 ok $? "${test_name}: exit code is 0"
52 set +x
53
54 diff -u "${tmp_stdout}" <(echo "Trigger registered successfully.")
55 ok $? "${test_name}: expected stdout"
56
57 diff -u "${tmp_stderr}" /dev/null
58 ok $? "${test_name}: expected stderr"
59 }
60
61 function test_failure ()
62 {
63 local test_name="$1"
64 local error_msg="$2"
65
66 shift 2
67
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"
71
72 diff -u "${tmp_stdout}" /dev/null
73 ok $? "${test_name}: expected stdout"
74
75 diff -u "${tmp_stderr}" <(echo "${error_msg}")
76 ok $? "${test_name}: expected stderr"
77 }
78
79 function test_mi ()
80 {
81 local tmp_stdout_raw
82 local tmp_expected_stdout
83
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.
87
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")
90
91 diag "${FULL_LTTNG_BIN} --mi=xml add-trigger"
92
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>
97 <output>
98 <trigger>
99 <name>mi_hohoho</name>
100 <owner_uid>${UID}</owner_uid>
101 <condition>
102 <condition_event_rule_matches>
103 <event_rule>
104 <event_rule_user_tracepoint>
105 <name_pattern>some-event-id</name_pattern>
106 </event_rule_user_tracepoint>
107 </event_rule>
108 <capture_descriptors/>
109 </condition_event_rule_matches>
110 </condition>
111 <action>
112 <action_list>
113 <action>
114 <action_notify>
115 <rate_policy>
116 <rate_policy_every_n>
117 <interval>1</interval>
118 </rate_policy_every_n>
119 </rate_policy>
120 </action_notify>
121 </action>
122 </action_list>
123 </action>
124 </trigger>
125 </output>
126 <success>true</success>
127 </command>
128 EOF
129
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}"
133
134 ok $? "add-trigger mi: exit code is 0"
135
136 # Pretty-fy xml before further test.
137 $XML_PRETTY_BIN < "${tmp_stdout_raw}" > "${tmp_stdout}"
138
139 $XML_VALIDATE_BIN "${MI_XSD_PATH}" "${tmp_stdout}"
140 ok $? "add-trigger mi is valid"
141
142 diff -u "${tmp_expected_stdout}" "${tmp_stdout}"
143 ok $? "mi: expected stdout"
144
145 diff -u "${tmp_stderr}" /dev/null
146 ok $? "mi: expected stderr"
147
148 rm -f "${tmp_stdout_raw}"
149 }
150
151 # top-level options
152 test_success "explicit name" \
153 --name hohoho \
154 --condition event-rule-matches --name=some-event-id --type=user \
155 --action notify
156
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 \
160 --action notify
161
162 test_success "--condition event-rule-matches --type=user" \
163 --condition event-rule-matches --type=user \
164 --action notify
165
166 test_success "notify action polices" \
167 --condition event-rule-matches --type=user --name=test-rate-policy \
168 --action notify \
169 --rate-policy=every:55 \
170 --action notify \
171 --rate-policy=once-after:55
172
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
179
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
186
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
193
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
200
201 test_success "--log-level single level" \
202 --condition event-rule-matches --type=user --log-level=INFO \
203 --action notify
204
205 test_success "--log-level range open max" \
206 --condition event-rule-matches --type=user --log-level=INFO.. \
207 --action notify
208
209 test_success "--log-level range any" \
210 --condition event-rule-matches --type=user --log-level=.. \
211 --action notify
212
213 test_success "--exclude-name one" \
214 --condition event-rule-matches --type=user --name='bernard*' --exclude-name=bernard-lermite \
215 --action notify
216
217 test_success "--exclude-name two" \
218 --condition event-rule-matches --type=user --name='jean-*' --exclude-name jean-chretien -x jean-charest \
219 --action notify
220
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 \
225 --action notify
226
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 ' ')
229
230 # We need to find a valid offset.
231 base_symbol=""
232 offset=0
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 ))
236 else
237 base_symbol="lttng_channel_disable"
238 offset=$(( 0x$channel_enable_addr - 0x$channel_disable_addr ))
239 fi
240
241 offset_hex="0x$(printf '%x' $offset)"
242
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 \
245 --action notify
246
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 \
249 --action notify
250 done
251 }
252
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 \
256 --action notify
257
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 \
260 --action notify
261 }
262
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 \
266 --action notify
267
268 test_success "--condition event-rule-matches all syscalls" \
269 --condition event-rule-matches --type=syscall \
270 --action notify
271
272 test_success "--condition event-rule-matches one syscall with filter" \
273 --condition event-rule-matches --type=syscall --filter 'a > 2' --name=open \
274 --action notify
275 test_success "--condition event-rule-matches one syscall:entry" \
276 --condition event-rule-matches --type=syscall:entry --name=open \
277 --action notify
278 test_success "--condition event-rule-matches one syscall:exit" \
279 --condition event-rule-matches --type=syscall:exit --name=open \
280 --action notify
281 test_success "--condition event-rule-matches one syscall:entry-exit" \
282 --condition event-rule-matches --type=syscall:entry+exit --name=open \
283 --action notify
284
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 \
288 --action notify
289
290 test_success "--condition event-rule-matches one kernel:syscall:entry" \
291 --condition event-rule-matches --type=kernel:syscall:entry --name=open \
292 --action notify
293 test_success "--condition event-rule-matches one kernel:syscall:exit" \
294 --condition event-rule-matches --type=kernel:syscall:exit --name=open \
295 --action notify
296 test_success "--condition event-rule-matches one kernel:syscall:entry-exit" \
297 --condition event-rule-matches --type=kernel:syscall:entry+exit --name=open \
298 --action notify
299
300 }
301
302 # `--action notify` successes
303 test_success "--action notify" \
304 --condition event-rule-matches --type=user \
305 --action notify
306
307 test_success "--action notify --capture foo" \
308 --condition event-rule-matches --type=user \
309 --capture foo --action notify
310
311 test_success "--action notify --capture foo[2]" \
312 --condition event-rule-matches --type=user \
313 --capture 'foo[2]' --action notify
314
315 test_success '--action notify --capture $ctx.foo' \
316 --condition event-rule-matches --type=user \
317 --capture '$ctx.foo' --action notify
318
319 test_success '--action notify --capture $ctx.foo[2]' \
320 --condition event-rule-matches --type=user \
321 --capture '$ctx.foo[2]' --action notify
322
323 test_success '--action notify --capture $app.prov:type' \
324 --condition event-rule-matches --type=user \
325 --capture '$app.prov:type' --action notify
326
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
330
331 test_success '--action notify multiple captures' \
332 --condition event-rule-matches --type=user \
333 --capture foo --capture '$app.hello:world' --action notify
334
335 # `--action start-session` successes
336 test_success "--action start-session" \
337 --condition event-rule-matches --type=user \
338 --action start-session ze-session
339
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
344
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
349
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
354
355 test_success "--action snapshot-session with file URI" \
356 --condition event-rule-matches --type=user \
357 --action snapshot-session ze-session --path /hello
358
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
362
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
366
367 # top-level failures
368 test_failure "no args" "Error: Missing --condition."
369
370 test_failure "unknown option" \
371 "Error: Unknown option \`--hello\`" \
372 --hello
373
374 test_failure "missing --action" \
375 "Error: Need at least one --action." \
376 --condition event-rule-matches --type=user
377
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 \
382 --action notify
383
384 test_failure "missing argument to --name" \
385 "Error: While parsing argument #1 (\`--name\`): Missing required argument for option \`--name\`" \
386 --name
387
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 \
392 --${cmd}
393
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 \
397 --${cmd}:123bob
398
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 \
402 --${cmd}":"
403 done
404
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
409
410 # `--condition` failures
411 test_failure "missing args after --condition" \
412 "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \
413 --condition
414 test_failure "unknown --condition" \
415 "Error: Unknown condition name 'zoofest'" \
416 --condition zoofest
417
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
422
423 test_failure "extra args after --condition event-rule-matches" \
424 "Error: Unexpected argument 'bozo'" \
425 --condition event-rule-matches --type=user bozo
426
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
430
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'
435 done
436
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'
440
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'
444
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
448
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
452
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 \
456 --name=open
457
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 \
461 --action notify
462
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`' \
465 --action notify \
466 --condition event-rule-matches --type=user --capture
467
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 \
471 --action notify \
472
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
477
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
482
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
487
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
492
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
497
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
502
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
507
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 \
512 --action
513
514 # `--action notify` failures
515 test_failure "extra arg after --action notify" \
516 "Error: Unexpected argument \`bob\`." \
517 --condition event-rule-matches --type=user \
518 --action notify bob
519
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
529
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
539
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
549
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/
575
576 test_mi
577
578 # Cleanup
579 stop_lttng_sessiond_notap
580 rm -f "${tmp_stdout}"
581 rm -f "${tmp_stderr}"
This page took 0.041771 seconds and 3 git commands to generate.