a55406c1a80760c735c3b6208fbdca7214467b55
[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 282
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 set +x
52 ok $? "${test_name}: exit code is 0"
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 # top-level options
80 test_success "explicit name" \
81 --name hohoho \
82 --condition event-rule-matches --name=some-event-id --type=user \
83 --action notify
84
85 # `--condition event-rule-matches` successes
86 test_success "--condition event-rule-matches some-event --type=user" \
87 --condition event-rule-matches --name=some-event --type=user \
88 --action notify
89
90 test_success "--condition event-rule-matches --type=user" \
91 --condition event-rule-matches --type=user \
92 --action notify
93
94 test_success "notify action polices" \
95 --condition event-rule-matches --type=user --name=test-rate-policy \
96 --action notify \
97 --rate-policy=every:55 \
98 --action notify \
99 --rate-policy=once-after:55
100
101 test_success "start session action polices" \
102 --condition event-rule-matches --type=user --name=test-rate-policy \
103 --action start-session my_session \
104 --rate-policy=every:55 \
105 --action start-session my_session \
106 --rate-policy=once-after:55
107
108 test_success "stop session action polices" \
109 --condition event-rule-matches --type=user --name=test-rate-policy \
110 --action stop-session my_session \
111 --rate-policy=every:55 \
112 --action stop-session my_session \
113 --rate-policy=once-after:55
114
115 test_success "snapshot session action polices" \
116 --condition event-rule-matches --type=user --name=test-rate-policy \
117 --action snapshot-session my_session \
118 --rate-policy=every:55 \
119 --action snapshot-session my_session \
120 --rate-policy=once-after:55
121
122 test_success "rotate session action polices" \
123 --condition event-rule-matches --type=user --name=test-rate-policy \
124 --action rotate-session my_session \
125 --rate-policy=every:55 \
126 --action rotate-session my_session \
127 --rate-policy=once-after:55
128
129 test_success "--log-level single level" \
130 --condition event-rule-matches --type=user --log-level=INFO \
131 --action notify
132
133 test_success "--log-level range open max" \
134 --condition event-rule-matches --type=user --log-level=INFO.. \
135 --action notify
136
137 test_success "--log-level range any" \
138 --condition event-rule-matches --type=user --log-level=.. \
139 --action notify
140
141 test_success "--exclude-name one" \
142 --condition event-rule-matches --type=user --name='bernard*' --exclude-name=bernard-lermite \
143 --action notify
144
145 test_success "--exclude-name two" \
146 --condition event-rule-matches --type=user --name='jean-*' --exclude-name jean-chretien -x jean-charest \
147 --action notify
148
149 skip $ist_root "non-root user: skipping kprobe tests" 18 || {
150 for type in kprobe kernel:kprobe; do
151 test_success "--condition event-rule-matches probe by symbol" \
152 --condition event-rule-matches --type=$type --location=lttng_channel_enable --event-name=my_channel_enable \
153 --action notify
154
155 channel_enable_addr=$(grep ' t lttng_channel_enable\s\[lttng_tracer\]$' /proc/kallsyms | cut -f 1 -d ' ')
156 channel_disable_addr=$(grep ' t lttng_channel_disable\s\[lttng_tracer\]$' /proc/kallsyms | cut -f 1 -d ' ')
157
158 # We need to find a valid offset.
159 base_symbol=""
160 offset=0
161 if [[ 0x$channel_enable_addr -lt 0x$channel_disable_addr ]]; then
162 base_symbol="lttng_channel_enable"
163 offset=$(( 0x$channel_disable_addr - 0x$channel_enable_addr ))
164 else
165 base_symbol="lttng_channel_disable"
166 offset=$(( 0x$channel_enable_addr - 0x$channel_disable_addr ))
167 fi
168
169 offset_hex="0x$(printf '%x' $offset)"
170
171 test_success "--condition event-rule-matches probe by symbol with offset" \
172 --condition event-rule-matches --type=$type --location="${base_symbol}+${offset_hex}" --event-name=my_$base_symbol \
173 --action notify
174
175 test_success "--condition event-rule-matches probe by address" \
176 --condition event-rule-matches --type=$type --location="0x${channel_enable_addr}" --event-name=my_channel_enable \
177 --action notify
178 done
179 }
180
181 skip $ist_root "non-root user: skipping uprobe tests" 6 || {
182 test_success "--condition event-rule-matches uprobe" \
183 --condition event-rule-matches --type=kernel:uprobe --location=${uprobe_elf_binary}:test_function --event-name=ma-probe \
184 --action notify
185
186 test_success "--condition event-rule-matches uprobe with elf prefix" \
187 --condition event-rule-matches --type=kernel:uprobe --location=elf:${uprobe_elf_binary}:test_function --event-name=ma-probe-2 \
188 --action notify
189 }
190
191 skip $ist_root "non-root user: skipping syscall tests" 30 || {
192 test_success "--condition event-rule-matches one syscall" \
193 --condition event-rule-matches --type=syscall --name=open \
194 --action notify
195
196 test_success "--condition event-rule-matches all syscalls" \
197 --condition event-rule-matches --type=syscall \
198 --action notify
199
200 test_success "--condition event-rule-matches one syscall with filter" \
201 --condition event-rule-matches --type=syscall --filter 'a > 2' --name=open \
202 --action notify
203 test_success "--condition event-rule-matches one syscall:entry" \
204 --condition event-rule-matches --type=syscall:entry --name=open \
205 --action notify
206 test_success "--condition event-rule-matches one syscall:exit" \
207 --condition event-rule-matches --type=syscall:exit --name=open \
208 --action notify
209 test_success "--condition event-rule-matches one syscall:entry-exit" \
210 --condition event-rule-matches --type=syscall:entry+exit --name=open \
211 --action notify
212
213 # Same thing but with "kernel:syscall" type instead:
214 test_success "--condition event-rule-matches one syscall" \
215 --condition event-rule-matches --type=kernel:syscall --name=open \
216 --action notify
217
218 test_success "--condition event-rule-matches one kernel:syscall:entry" \
219 --condition event-rule-matches --type=kernel:syscall:entry --name=open \
220 --action notify
221 test_success "--condition event-rule-matches one kernel:syscall:exit" \
222 --condition event-rule-matches --type=kernel:syscall:exit --name=open \
223 --action notify
224 test_success "--condition event-rule-matches one kernel:syscall:entry-exit" \
225 --condition event-rule-matches --type=kernel:syscall:entry+exit --name=open \
226 --action notify
227
228 }
229
230 # `--action notify` successes
231 test_success "--action notify" \
232 --condition event-rule-matches --type=user \
233 --action notify
234
235 test_success "--action notify --capture foo" \
236 --condition event-rule-matches --type=user \
237 --capture foo --action notify
238
239 test_success "--action notify --capture foo[2]" \
240 --condition event-rule-matches --type=user \
241 --capture 'foo[2]' --action notify
242
243 test_success '--action notify --capture $ctx.foo' \
244 --condition event-rule-matches --type=user \
245 --capture '$ctx.foo' --action notify
246
247 test_success '--action notify --capture $ctx.foo[2]' \
248 --condition event-rule-matches --type=user \
249 --capture '$ctx.foo[2]' --action notify
250
251 test_success '--action notify --capture $app.prov:type' \
252 --condition event-rule-matches --type=user \
253 --capture '$app.prov:type' --action notify
254
255 test_success '--action notify --capture $app.prov:type[2]' \
256 --condition event-rule-matches --type=user \
257 --capture '$app.prov:type[2]' --action notify
258
259 test_success '--action notify multiple captures' \
260 --condition event-rule-matches --type=user \
261 --capture foo --capture '$app.hello:world' --action notify
262
263 # `--action start-session` successes
264 test_success "--action start-session" \
265 --condition event-rule-matches --type=user \
266 --action start-session ze-session
267
268 # `--action stop-session` successes
269 test_success "--action stop-session foo" \
270 --condition event-rule-matches --type=user \
271 --action stop-session ze-session
272
273 # `--action rotate-session` successes
274 test_success "--action rotate-session foo" \
275 --condition event-rule-matches --type=user \
276 --action rotate-session ze-session
277
278 # `--action snapshot-session` successes
279 test_success "--action snapshot-session foo" \
280 --condition event-rule-matches --type=user \
281 --action snapshot-session ze-session
282
283 test_success "--action snapshot-session with file URI" \
284 --condition event-rule-matches --type=user \
285 --action snapshot-session ze-session --path /hello
286
287 test_success "--action snapshot-session with net URI" \
288 --condition event-rule-matches --type=user \
289 --action snapshot-session ze-session --url net://1.2.3.4
290
291 test_success "--action snapshot-session with ctrl/data URIs" \
292 --condition event-rule-matches --type=user \
293 --action snapshot-session ze-session --ctrl-url=tcp://1.2.3.4:1234 --data-url=tcp://1.2.3.4:1235
294
295 # top-level failures
296 test_failure "no args" "Error: Missing --condition."
297
298 test_failure "unknown option" \
299 "Error: Unknown option \`--hello\`" \
300 --hello
301
302 test_failure "missing --action" \
303 "Error: Need at least one --action." \
304 --condition event-rule-matches --type=user
305
306 test_failure "two --condition" \
307 "Error: A --condition was already given." \
308 --condition event-rule-matches --name=aaa --type=user \
309 --condition event-rule-matches --name=bbb --type=user \
310 --action notify
311
312 test_failure "missing argument to --name" \
313 "Error: While parsing argument #1 (\`--name\`): Missing required argument for option \`--name\`" \
314 --name
315
316 for cmd in rate-policy=once-after rate-policy=every; do
317 test_failure "missing argument to --${cmd}" \
318 "Error: Rate policy format is invalid." \
319 --condition event-rule-matches --type=user --action notify \
320 --${cmd}
321
322 test_failure "invalid argument to --${cmd}: non-digit character" \
323 "Error: Failed to parse rate policy value \`123bob\` as an integer." \
324 --condition event-rule-matches --type=user --action notify \
325 --${cmd}:123bob
326
327 test_failure "invalid argument to --${cmd}: empty string" \
328 "Error: Failed to parse rate policy value \`\` as an integer." \
329 --condition event-rule-matches --type=user --action notify \
330 --${cmd}":"
331 done
332
333 test_failure "invalid argument to --rate-policy: unknown policy type" \
334 "Error: Rate policy type \`bob\` unknown." \
335 --condition event-rule-matches --type=user --action notify \
336 --rate-policy=bob:123
337
338 # `--condition` failures
339 test_failure "missing args after --condition" \
340 "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \
341 --condition
342 test_failure "unknown --condition" \
343 "Error: Unknown condition name 'zoofest'" \
344 --condition zoofest
345
346 # `--condition event-rule-matches` failures
347 test_failure "missing args after --condition event-rule-matches" \
348 "Error: Need at least one --action." \
349 --condition event-rule-matches
350
351 test_failure "extra args after --condition event-rule-matches" \
352 "Error: Unexpected argument 'bozo'" \
353 --condition event-rule-matches --type=user bozo
354
355 test_failure "--log-level unknown level" \
356 "Error: Failed to parse log level string \`FOO\`." \
357 --condition event-rule-matches --type=user --log-level=FOO
358
359 for type in kprobe kernel:kprobe; do
360 test_failure "--condition event-rule-matches: --name with --type=$type" \
361 "Error: Can't use --name with kernel kprobe event rules." \
362 --condition event-rule-matches --type=$type --location=do_sys_open --name='hello'
363 done
364
365 test_failure "--condition event-rule-matches: --location with user tracepoint event rule" \
366 "Error: Can't use --location with user tracepoint event rules." \
367 --condition event-rule-matches --type=user --location='hello'
368
369 test_failure "--condition event-rule-matches: --event-name with user tracepoint event rule" \
370 "Error: Can't use --event-name with user tracepoint event rules." \
371 --condition event-rule-matches --type=user --event-name='hello'
372
373 test_failure "--condition event-rule-matches: extra argument with --type=kernel:uprobe" \
374 "Error: Unexpected argument 'hello'" \
375 --condition event-rule-matches --type=$type --location=${uprobe_elf_binary}:test_failure hello
376
377 test_failure "--condition event-rule-matches: extra argument with --type=syscall" \
378 "Error: Unexpected argument 'open'" \
379 --condition event-rule-matches --type=syscall open
380
381 test_failure "--condition event-rule-matches: --type=syscall:nope" \
382 "Error: Failed to parse syscall type 'syscall:nope'." \
383 --condition event-rule-matches --type=syscall:nope \
384 --name=open
385
386 test_failure "--exclude-name with non-glob name" \
387 "Error: Event jean: Exclusions can only be used with a globbing pattern" \
388 --condition event-rule-matches --type=user --name='jean' --exclude-name jean-chretien \
389 --action notify
390
391 test_failure "--condition event-rule-matches --capture: missing argument (end of arg list)" \
392 'Error: While parsing argument #2 (`--capture`): Missing required argument for option `--capture`' \
393 --action notify \
394 --condition event-rule-matches --type=user --capture
395
396 test_failure "--condition event-rule-matches --capture: missing argument (before another option)" \
397 'Error: While parsing expression `--action`: Unary operators are not allowed in capture expressions.' \
398 --condition event-rule-matches --type=user --capture \
399 --action notify \
400
401 test_failure "--condition event-rule-matches --capture: binary operator" \
402 'Error: While parsing expression `foo == 2`: Binary operators are not allowed in capture expressions.' \
403 --condition event-rule-matches --type=user \
404 --capture 'foo == 2' --action notify
405
406 test_failure "--condition event-rule-matches --capture: unary operator" \
407 'Error: While parsing expression `!foo`: Unary operators are not allowed in capture expressions.' \
408 --condition event-rule-matches --type=user \
409 --capture '!foo' --action notify
410
411 test_failure "--condition event-rule-matches --capture: logical operator" \
412 'Error: While parsing expression `foo || bar`: Logical operators are not allowed in capture expressions.' \
413 --condition event-rule-matches --type=user \
414 --capture 'foo || bar' --action notify
415
416 test_failure "--condition event-rule-matches --capture: accessing a sub-field" \
417 'Error: While parsing expression `foo.bar`: Capturing subfields is not supported.' \
418 --condition event-rule-matches --type=user \
419 --capture 'foo.bar' --action notify
420
421 test_failure "--condition event-rule-matches --capture: accessing the sub-field of an array element" \
422 'Error: While parsing expression `foo[3].bar`: Capturing subfields is not supported.' \
423 --condition event-rule-matches --type=user \
424 --capture 'foo[3].bar' --action notify
425
426 test_failure "--condition event-rule-matches --capture: missing colon in app-specific context field" \
427 'Error: Invalid app-specific context field name: missing colon in `foo`.' \
428 --condition event-rule-matches --type=user \
429 --capture '$app.foo' --action notify
430
431 test_failure "--condition event-rule-matches --capture: missing colon in app-specific context field" \
432 'Error: Invalid app-specific context field name: missing type name after colon in `foo:`.' \
433 --condition event-rule-matches --type=user \
434 --capture '$app.foo:' --action notify
435
436 # `--action` failures
437 test_failure "missing args after --action" \
438 "Error: While parsing argument #1 (\`--action\`): Missing required argument for option \`--action\`" \
439 --condition event-rule-matches --type=user \
440 --action
441
442 # `--action notify` failures
443 test_failure "extra arg after --action notify" \
444 "Error: Unexpected argument \`bob\`." \
445 --condition event-rule-matches --type=user \
446 --action notify bob
447
448 # `--action start-session` failures
449 test_failure "missing arg after --action start-session" \
450 "Error: Missing session name." \
451 --condition event-rule-matches --type=user \
452 --action start-session
453 test_failure "extra arg after --action start-session" \
454 "Error: Unexpected argument \`bob\`." \
455 --condition event-rule-matches --type=user \
456 --action start-session ze-session bob
457
458 # `--action stop-session` failures
459 test_failure "missing arg after --action stop-session" \
460 "Error: Missing session name." \
461 --condition event-rule-matches --type=user \
462 --action stop-session
463 test_failure "extra arg after --action stop-session" \
464 "Error: Unexpected argument \`bob\`." \
465 --condition event-rule-matches --type=user \
466 --action stop-session ze-session bob
467
468 # `--action rotate-session` failures
469 test_failure "missing arg after --action rotate-session" \
470 "Error: Missing session name." \
471 --condition event-rule-matches --type=user \
472 --action rotate-session
473 test_failure "extra arg after --action rotate-session" \
474 "Error: Unexpected argument \`bob\`." \
475 --condition event-rule-matches --type=user \
476 --action rotate-session ze-session bob
477
478 # `--action snapshot-session` failures
479 test_failure "missing arg after --action snapshot-session" \
480 "Error: Missing session name." \
481 --condition event-rule-matches --type=user \
482 --action snapshot-session
483 test_failure "extra arg after --action snapshot-session" \
484 "Error: Unexpected argument \`bob\`." \
485 --condition event-rule-matches --type=user \
486 --action snapshot-session ze-session bob
487 test_failure "snapshot-session action, --max-size without destination" \
488 "Error: Can't provide a snapshot output max size without a snapshot output destination." \
489 --condition event-rule-matches --type=user \
490 --action snapshot-session ze-session --max-size 10M
491 test_failure "snapshot-session action, --name without destination" \
492 "Error: Can't provide a snapshot output name without a snapshot output destination." \
493 --condition event-rule-matches --type=user \
494 --action snapshot-session ze-session --name hallo
495 test_failure "snapshot-session action, --name with-local-path-instead-of-url" \
496 "Error: Failed to parse '/something/that/looks/like/a/path' as an URL." \
497 --condition event-rule-matches --type=user \
498 --action snapshot-session ze-session --name hallo --url /something/that/looks/like/a/path
499 test_failure "snapshot-session action, --name with-net-url-instead-of-path" \
500 "Error: Failed to parse 'net://8.8.8.8/' as a local path." \
501 --condition event-rule-matches --type=user \
502 --action snapshot-session ze-session --name hallo --path net://8.8.8.8/
503
504 # Cleanup
505 stop_lttng_sessiond_notap
506 rm -f "${tmp_stdout}"
507 rm -f "${tmp_stderr}"
This page took 0.039841 seconds and 3 git commands to generate.