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