sessiond: kernel triggers: add infrastructure to create event notifiers
[lttng-tools.git] / tests / utils / utils.sh
1 # Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
2 #
3 # SPDX-License-Identifier: LGPL-2.1-only
4 #
5
6 SESSIOND_BIN="lttng-sessiond"
7 SESSIOND_MATCH=".*lttng-sess.*"
8 RUNAS_BIN="lttng-runas"
9 RUNAS_MATCH=".*lttng-runas.*"
10 CONSUMERD_BIN="lttng-consumerd"
11 CONSUMERD_MATCH=".*lttng-consumerd.*"
12 RELAYD_BIN="lttng-relayd"
13 RELAYD_MATCH=".*lttng-relayd.*"
14 LTTNG_BIN="lttng"
15 BABELTRACE_BIN="babeltrace"
16 OUTPUT_DEST=/dev/null
17 ERROR_OUTPUT_DEST=/dev/null
18
19 # Minimal kernel version supported for session daemon tests
20 KERNEL_MAJOR_VERSION=2
21 KERNEL_MINOR_VERSION=6
22 KERNEL_PATCHLEVEL_VERSION=27
23
24 # We set the default UST register timeout and network and app socket timeout to
25 # "wait forever", so that basic tests don't have to worry about hitting
26 # timeouts on busy systems. Specialized tests should test those corner-cases.
27 export LTTNG_UST_REGISTER_TIMEOUT=-1
28 export LTTNG_NETWORK_SOCKET_TIMEOUT=-1
29 export LTTNG_APP_SOCKET_TIMEOUT=-1
30
31 # We set the default lttng-sessiond path to /bin/true to prevent the spawning
32 # of a daemonized sessiond. This is necessary since 'lttng create' will spawn
33 # its own sessiond if none is running. It also ensures that 'lttng create'
34 # fails when no sessiond is running.
35 export LTTNG_SESSIOND_PATH="/bin/true"
36
37 source $TESTDIR/utils/tap/tap.sh
38
39 if [ -z ${LTTNG_TEST_TEARDOWN_TIMEOUT+x} ]; then
40 LTTNG_TEST_TEARDOWN_TIMEOUT=60
41 fi
42
43 function full_cleanup ()
44 {
45 # Try to kill daemons gracefully
46 stop_lttng_relayd_notap SIGTERM $LTTNG_TEST_TEARDOWN_TIMEOUT
47 stop_lttng_sessiond_notap SIGTERM $LTTNG_TEST_TEARDOWN_TIMEOUT
48
49 # If daemons are still present, forcibly kill them
50 stop_lttng_relayd_notap SIGKILL $LTTNG_TEST_TEARDOWN_TIMEOUT
51 stop_lttng_sessiond_notap SIGKILL $LTTNG_TEST_TEARDOWN_TIMEOUT
52 stop_lttng_consumerd_notap SIGKILL $LTTNG_TEST_TEARDOWN_TIMEOUT
53
54 # Disable trap for SIGTERM since the following kill to the
55 # pidgroup will be SIGTERM. Otherwise it loops.
56 # The '-' before the pid number ($$) indicates 'kill' to signal the
57 # whole process group.
58 trap - SIGTERM && kill -- -$$
59 exit 1
60 }
61
62 function null_pipes ()
63 {
64 exec 0>/dev/null
65 exec 1>/dev/null
66 exec 2>/dev/null
67 }
68
69 trap full_cleanup SIGINT SIGTERM
70
71 # perl prove closes its child pipes before giving it a chance to run its
72 # signal trap handlers. Redirect pipes to /dev/null if SIGPIPE is caught
73 # to allow those trap handlers to proceed.
74
75 trap null_pipes SIGPIPE
76
77 # Check pgrep from env, default to pgrep if none
78 if [ -z "$PGREP" ]; then
79 PGREP=pgrep
80 fi
81
82 # Due to the renaming of threads we need to use the full command (pgrep -f) to
83 # identify the pids for multiple lttng related processes. The problem with "pgrep
84 # -f" is that it ends up also looking at the arguments. We use a two stage
85 # lookup. The first one is using "pgrep -f" yielding potential candidate.
86 # The second on perform grep on the basename of the first field of the
87 # /proc/pid/cmdline of the previously identified pids. The first field
88 # correspond to the actual command.
89 function lttng_pgrep ()
90 {
91 local pattern=$1
92 local possible_pids
93 local full_command_no_argument
94 local command_basename
95
96 possible_pids=$($PGREP -f "$pattern")
97 if [ -z "$possible_pids" ]; then
98 return 0
99 fi
100
101 while IFS= read -r pid ; do
102 # /proc/pid/cmdline is null separated.
103 if full_command_no_argument=$(cut -d '' -f 1 2>/dev/null < /proc/"$pid"/cmdline); then
104 command_basename=$(basename "$full_command_no_argument")
105 if grep -q "$pattern" <<< "$command_basename"; then
106 echo "$pid"
107 fi
108 fi
109 done <<< "$possible_pids"
110 return 0
111 }
112
113 function print_ok ()
114 {
115 # Check if we are a terminal
116 if [ -t 1 ]; then
117 echo -e "\e[1;32mOK\e[0m"
118 else
119 echo -e "OK"
120 fi
121 }
122
123 function print_fail ()
124 {
125 # Check if we are a terminal
126 if [ -t 1 ]; then
127 echo -e "\e[1;31mFAIL\e[0m"
128 else
129 echo -e "FAIL"
130 fi
131 }
132
133 function print_test_banner ()
134 {
135 local desc="$1"
136 diag "$desc"
137 }
138
139 function validate_kernel_version ()
140 {
141 local kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
142 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
143 return 0
144 fi
145 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
146 return 0
147 fi
148 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
149 return 0
150 fi
151 return 1
152 }
153
154 # Generate a random string
155 # $1 = number of characters; defaults to 16
156 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
157 function randstring()
158 {
159 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
160 cat /dev/urandom 2>/dev/null | tr -cd "$CHAR" 2>/dev/null | head -c ${1:-16} 2>/dev/null
161 echo
162 }
163
164 # Return the number of _configured_ CPUs.
165 function conf_proc_count()
166 {
167 getconf _NPROCESSORS_CONF
168 if [ $? -ne 0 ]; then
169 diag "Failed to get the number of configured CPUs"
170 fi
171 echo
172 }
173
174 # Check if base lttng-modules are present.
175 # Bail out on failure
176 function validate_lttng_modules_present ()
177 {
178 # Check for loadable modules.
179 modprobe -n lttng-tracer 2>/dev/null
180 if [ $? -eq 0 ]; then
181 return 0
182 fi
183
184 # Check for builtin modules.
185 ls /proc/lttng > /dev/null 2>&1
186 if [ $? -eq 0 ]; then
187 return 0
188 fi
189
190 BAIL_OUT "LTTng modules not detected."
191 }
192
193 function enable_kernel_lttng_event
194 {
195 local withtap="$1"
196 local expected_to_fail="$2"
197 local sess_name="$3"
198 local event_name="$4"
199 local channel_name="$5"
200
201 if [ -z "$event_name" ]; then
202 # Enable all event if no event name specified
203 event_name="-a"
204 fi
205
206 if [ -z "$channel_name" ]; then
207 # default channel if none specified
208 chan=""
209 else
210 chan="-c $channel_name"
211 fi
212
213 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
214 ret=$?
215 if [[ $expected_to_fail -eq "1" ]]; then
216 test $ret -ne "0"
217 ret=$?
218 if [ $withtap -eq "1" ]; then
219 ok $ret "Enable kernel event $event_name for session $session_name on channel $channel_name failed as expected"
220 fi
221 else
222 if [ $withtap -eq "1" ]; then
223 ok $ret "Enable kernel event $event_name for session $sess_name"
224 fi
225 fi
226 }
227
228 function enable_kernel_lttng_event_ok ()
229 {
230 enable_kernel_lttng_event 1 0 "$@"
231 }
232
233 function enable_kernel_lttng_event_fail ()
234 {
235 enable_kernel_lttng_event 1 1 "$@"
236 }
237
238 function enable_kernel_lttng_event_notap ()
239 {
240 enable_kernel_lttng_event 0 0 "$@"
241 }
242
243 # Old interface
244 function lttng_enable_kernel_event
245 {
246 enable_kernel_lttng_event_ok "$@"
247 }
248
249 function lttng_enable_kernel_syscall()
250 {
251 local expected_to_fail=$1
252 local sess_name=$2
253 local syscall_name=$3
254 local channel_name=$4
255
256 if [ -z $syscall_name ]; then
257 # Enable all event if no syscall name specified
258 syscall_name="-a"
259 fi
260
261 if [ -z $channel_name ]; then
262 # default channel if none specified
263 chan=""
264 else
265 chan="-c $channel_name"
266 fi
267
268 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --syscall "$syscall_name" $chan -s $sess_name -k 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
269 ret=$?
270 if [[ $expected_to_fail -eq "1" ]]; then
271 test $ret -ne "0"
272 ok $? "Enable kernel syscall $syscall_name for session $sess_name on channel $channel_name fail as expected"
273 else
274 ok $ret "Enable kernel syscall $syscall_name for session $sess_name on channel $channel_name"
275 fi
276 }
277
278 function lttng_enable_kernel_syscall_ok()
279 {
280 lttng_enable_kernel_syscall 0 "$@"
281 }
282
283 function lttng_enable_kernel_syscall_fail()
284 {
285 lttng_enable_kernel_syscall 1 "$@"
286 }
287
288 function lttng_disable_kernel_syscall()
289 {
290 local expected_to_fail=$1
291 local sess_name=$2
292 local syscall_name=$3
293 local channel_name=$4
294
295 if [ -z $syscall_name ]; then
296 # Enable all event if no syscall name specified
297 syscall_name="-a"
298 fi
299
300 if [ -z $channel_name ]; then
301 # default channel if none specified
302 chan=""
303 else
304 chan="-c $channel_name"
305 fi
306
307 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event --syscall "$syscall_name" $chan -s $sess_name -k 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
308
309 ret=$?
310 if [[ $expected_to_fail -eq "1" ]]; then
311 test $ret -ne "0"
312 ok $? "Disable kernel syscall $syscall_name for session $sess_name on channel $channel_name failed as expected"
313 else
314 ok $ret "Disable kernel syscall $syscall_name for session $sess_name on channel $channel_name"
315 fi
316 }
317
318 function lttng_disable_kernel_syscall_ok()
319 {
320 lttng_disable_kernel_syscall 0 "$@"
321 }
322
323 function lttng_disable_kernel_syscall_fail()
324 {
325 lttng_disable_kernel_syscall 1 "$@"
326 }
327
328 function lttng_enable_kernel_userspace_probe_event ()
329 {
330 local expected_to_fail="$1"
331 local sess_name="$2"
332 local target="$3"
333 local event_name="$4"
334
335 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event --kernel --userspace-probe="$target" "$event_name" -s "$sess_name" > "$OUTPUT_DEST" 2> "$ERROR_OUTPUT_DEST"
336 ret=$?
337 if [[ $expected_to_fail -eq "1" ]]; then
338 test $ret -ne "0"
339 ok $? "Enable kernel userspace probe event for session $sess_name failed as expected"
340 else
341 ok $ret "Enable kernel userspace probe event for session $sess_name"
342 fi
343 }
344
345 function lttng_enable_kernel_userspace_probe_event_fail ()
346 {
347 lttng_enable_kernel_userspace_probe_event 1 "$@"
348 }
349
350 function lttng_enable_kernel_userspace_probe_event_ok ()
351 {
352 lttng_enable_kernel_userspace_probe_event 0 "$@"
353 }
354
355 function disable_kernel_lttng_userspace_probe_event_ok ()
356 {
357 local sess_name="$1"
358 local event_name="$2"
359
360 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" disable-event --kernel "$event_name" -s "$sess_name" > "$OUTPUT_DEST" 2> "$ERROR_OUTPUT_DEST"
361 ok $? "Disable kernel event $target for session $sess_name"
362 }
363 function lttng_enable_kernel_channel()
364 {
365 local withtap=$1
366 local expected_to_fail=$2
367 local sess_name=$3
368 local channel_name=$4
369 local opts="${@:5}"
370
371 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -k $channel_name -s $sess_name $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
372 ret=$?
373 if [[ $expected_to_fail -eq "1" ]]; then
374 test "$ret" -ne "0"
375 ret=$?
376 if [ $withtap -eq "1" ]; then
377 ok $ret "Enable channel $channel_name for session $sess_name failed as expected"
378 fi
379 else
380 if [ $withtap -eq "1" ]; then
381 ok $ret "Enable channel $channel_name for session $sess_name"
382 fi
383 fi
384 }
385
386 function lttng_enable_kernel_channel_ok()
387 {
388 lttng_enable_kernel_channel 1 0 "$@"
389 }
390
391 function lttng_enable_kernel_channel_fail()
392 {
393 lttng_enable_kernel_channel 1 1 "$@"
394 }
395
396 function lttng_enable_kernel_channel_notap()
397 {
398 lttng_enable_kernel_channel 0 0 "$@"
399 }
400
401 function enable_kernel_lttng_channel_ok()
402 {
403 lttng_enable_kernel_channel 1 0 "$@"
404 }
405
406 function lttng_disable_kernel_channel()
407 {
408 local expected_to_fail=$1
409 local sess_name=$2
410 local channel_name=$3
411
412 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -k $channel_name -s $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
413 ret=$?
414 if [[ $expected_to_fail -eq "1" ]]; then
415 test "$ret" -ne "0"
416 ok $? "Disable channel $channel_name for session $sess_name failed as expected"
417 else
418 ok $ret "Disable channel $channel_name for session $sess_name"
419 fi
420 }
421
422 function lttng_disable_kernel_channel_ok()
423 {
424 lttng_disable_kernel_channel 0 "$@"
425 }
426
427 function lttng_disable_kernel_channel_fail()
428 {
429 lttng_disable_kernel_channel 1 "$@"
430 }
431
432 function start_lttng_relayd_opt()
433 {
434 local withtap=$1
435 local process_mode=$2
436 local opt=$3
437
438 DIR=$(readlink -f "$TESTDIR")
439
440 if [ -z $(lttng_pgrep "$RELAYD_MATCH") ]; then
441 # shellcheck disable=SC2086
442 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN $process_mode $opt 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
443 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
444 if [ $? -eq 1 ]; then
445 if [ $withtap -eq "1" ]; then
446 fail "Start lttng-relayd (process mode: $process_mode opt: $opt)"
447 fi
448 return 1
449 else
450 if [ $withtap -eq "1" ]; then
451 pass "Start lttng-relayd (process mode: $process_mode opt: $opt)"
452 fi
453 fi
454 else
455 pass "Start lttng-relayd (opt: $opt)"
456 fi
457 }
458
459 function start_lttng_relayd()
460 {
461 start_lttng_relayd_opt 1 "-b" "$@"
462 }
463
464 function start_lttng_relayd_notap()
465 {
466 start_lttng_relayd_opt 0 "-b" "$@"
467 }
468
469 function stop_lttng_relayd_opt()
470 {
471 local withtap=$1
472 local signal=$2
473
474 if [ -z "$signal" ]; then
475 signal="SIGTERM"
476 fi
477
478 local timeout_s=$3
479 local dtimeleft_s=
480
481 # Multiply time by 2 to simplify integer arithmetic
482 if [ -n "$timeout_s" ]; then
483 dtimeleft_s=$((timeout_s * 2))
484 fi
485
486 local retval=0
487 local pids=
488
489 pids=$(lttng_pgrep "$RELAYD_MATCH")
490 if [ -z "$pids" ]; then
491 if [ "$withtap" -eq "1" ]; then
492 pass "No relay daemon to kill"
493 fi
494 return 0
495 fi
496
497 diag "Killing (signal $signal) lttng-relayd (pid: $pids)"
498
499 # shellcheck disable=SC2086
500 if ! kill -s $signal $pids 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST; then
501 retval=1
502 if [ "$withtap" -eq "1" ]; then
503 fail "Kill relay daemon"
504 fi
505 else
506 out=1
507 while [ -n "$out" ]; do
508 out=$(lttng_pgrep "$RELAYD_MATCH")
509 if [ -n "$dtimeleft_s" ]; then
510 if [ $dtimeleft_s -lt 0 ]; then
511 out=
512 retval=1
513 fi
514 dtimeleft_s=$((dtimeleft_s - 1))
515 fi
516 sleep 0.5
517 done
518 if [ "$withtap" -eq "1" ]; then
519 if [ "$retval" -eq "0" ]; then
520 pass "Wait after kill relay daemon"
521 else
522 fail "Wait after kill relay daemon"
523 fi
524 fi
525 fi
526 return $retval
527 }
528
529 function stop_lttng_relayd()
530 {
531 stop_lttng_relayd_opt 1 "$@"
532 }
533
534 function stop_lttng_relayd_notap()
535 {
536 stop_lttng_relayd_opt 0 "$@"
537 }
538
539 #First arg: show tap output
540 #Second argument: load path for automatic loading
541 function start_lttng_sessiond_opt()
542 {
543 local withtap=$1
544 local load_path=$2
545
546 local env_vars=""
547 local consumerd=""
548
549 local long_bit_value=
550 long_bit_value=$(getconf LONG_BIT)
551
552 if [ -n "$TEST_NO_SESSIOND" ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
553 # Env variable requested no session daemon
554 return
555 fi
556
557 DIR=$(readlink -f "$TESTDIR")
558
559 # Get long_bit value for 32/64 consumerd
560 case "$long_bit_value" in
561 32)
562 consumerd="--consumerd32-path=$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
563 ;;
564 64)
565 consumerd="--consumerd64-path=$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
566 ;;
567 *)
568 return
569 ;;
570 esac
571
572 # Check for env. variable. Allow the use of LD_PRELOAD etc.
573 if [[ "x${LTTNG_SESSIOND_ENV_VARS}" != "x" ]]; then
574 env_vars="${LTTNG_SESSIOND_ENV_VARS} "
575 fi
576 env_vars="${env_vars}$DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN"
577
578 if ! validate_kernel_version; then
579 fail "Start session daemon"
580 BAIL_OUT "*** Kernel too old for session daemon tests ***"
581 fi
582
583 : "${LTTNG_SESSION_CONFIG_XSD_PATH="${DIR}/../src/common/config/"}"
584 export LTTNG_SESSION_CONFIG_XSD_PATH
585
586 if [ -z "$(lttng_pgrep "${SESSIOND_MATCH}")" ]; then
587 # Have a load path ?
588 if [ -n "$load_path" ]; then
589 # shellcheck disable=SC2086
590 env $env_vars --load "$load_path" --background "$consumerd"
591 else
592 # shellcheck disable=SC2086
593 env $env_vars --background "$consumerd"
594 fi
595 #$DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --background --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --verbose-consumer >>/tmp/sessiond.log 2>&1
596 status=$?
597 if [ "$withtap" -eq "1" ]; then
598 ok $status "Start session daemon"
599 fi
600 fi
601 }
602
603 function start_lttng_sessiond()
604 {
605 start_lttng_sessiond_opt 1 "$@"
606 }
607
608 function start_lttng_sessiond_notap()
609 {
610 start_lttng_sessiond_opt 0 "$@"
611 }
612
613 function stop_lttng_sessiond_opt()
614 {
615 local withtap=$1
616 local signal=$2
617
618 if [ -z "$signal" ]; then
619 signal=SIGTERM
620 fi
621
622 local timeout_s=$3
623 local dtimeleft_s=
624
625 # Multiply time by 2 to simplify integer arithmetic
626 if [ -n "$timeout_s" ]; then
627 dtimeleft_s=$((timeout_s * 2))
628 fi
629
630 if [ -n "$TEST_NO_SESSIOND" ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
631 # Env variable requested no session daemon
632 return 0
633 fi
634
635 local retval=0
636
637 local runas_pids=
638 runas_pids=$(lttng_pgrep "$RUNAS_MATCH")
639
640 local pids=
641 pids=$(lttng_pgrep "$SESSIOND_MATCH")
642
643 if [ -n "$runas_pids" ]; then
644 pids="$pids $runas_pids"
645 fi
646
647 if [ -z "$pids" ]; then
648 if [ "$withtap" -eq "1" ]; then
649 fail "No session daemon to kill"
650 else
651 BAIL_OUT "No session daemon to kill"
652 fi
653 return 0
654 fi
655
656 diag "Killing (signal $signal) $SESSIOND_BIN and lt-$SESSIOND_BIN pids: $(echo "$pids" | tr '\n' ' ')"
657
658 # shellcheck disable=SC2086
659 if ! kill -s $signal $pids 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST; then
660 retval=1
661 if [ "$withtap" -eq "1" ]; then
662 fail "Kill sessions daemon"
663 fi
664 else
665 out=1
666 while [ -n "$out" ]; do
667 out=$(lttng_pgrep "${SESSIOND_MATCH}")
668 if [ -n "$dtimeleft_s" ]; then
669 if [ $dtimeleft_s -lt 0 ]; then
670 out=
671 retval=1
672 fi
673 dtimeleft_s=$((dtimeleft_s - 1))
674 fi
675 sleep 0.5
676 done
677 out=1
678 while [ -n "$out" ]; do
679 out=$(lttng_pgrep "$CONSUMERD_MATCH")
680 if [ -n "$dtimeleft_s" ]; then
681 if [ $dtimeleft_s -lt 0 ]; then
682 out=
683 retval=1
684 fi
685 dtimeleft_s=$((dtimeleft_s - 1))
686 fi
687 sleep 0.5
688 done
689
690 if [ "$withtap" -eq "1" ]; then
691 if [ "$retval" -eq "0" ]; then
692 pass "Wait after kill session daemon"
693 else
694 fail "Wait after kill session daemon"
695 fi
696 fi
697 fi
698 if [ "$signal" = "SIGKILL" ]; then
699 if [ "$(id -u)" -eq "0" ]; then
700 local modules=
701 modules="$(lsmod | grep ^lttng | awk '{print $1}')"
702
703 if [ -n "$modules" ]; then
704 diag "Unloading all LTTng modules"
705 modprobe --remove "$modules"
706 fi
707 fi
708 fi
709
710 return $retval
711 }
712
713 function stop_lttng_sessiond()
714 {
715 stop_lttng_sessiond_opt 1 "$@"
716 }
717
718 function stop_lttng_sessiond_notap()
719 {
720 stop_lttng_sessiond_opt 0 "$@"
721 }
722
723 function sigstop_lttng_sessiond_opt()
724 {
725 local withtap=$1
726 local signal=SIGSTOP
727
728 if [ -n "$TEST_NO_SESSIOND" ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
729 # Env variable requested no session daemon
730 return
731 fi
732
733 PID_SESSIOND="$(lttng_pgrep "${SESSIOND_MATCH}") $(lttng_pgrep "$RUNAS_MATCH")"
734
735 if [ "$withtap" -eq "1" ]; then
736 diag "Sending SIGSTOP to lt-$SESSIOND_BIN and $SESSIOND_BIN pids: $(echo "$PID_SESSIOND" | tr '\n' ' ')"
737 fi
738
739 # shellcheck disable=SC2086
740 if ! kill -s $signal $PID_SESSIOND 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST; then
741 if [ "$withtap" -eq "1" ]; then
742 fail "Sending SIGSTOP to session daemon"
743 fi
744 else
745 out=1
746 while [ $out -ne 0 ]; do
747 pid="$(lttng_pgrep "$SESSIOND_MATCH")"
748
749 # Wait until state becomes stopped for session
750 # daemon(s).
751 out=0
752 for sessiond_pid in $pid; do
753 state="$(ps -p "$sessiond_pid" -o state= )"
754 if [[ -n "$state" && "$state" != "T" ]]; then
755 out=1
756 fi
757 done
758 sleep 0.5
759 done
760 if [ "$withtap" -eq "1" ]; then
761 pass "Sending SIGSTOP to session daemon"
762 fi
763 fi
764 }
765
766 function sigstop_lttng_sessiond()
767 {
768 sigstop_lttng_sessiond_opt 1 "$@"
769 }
770
771 function sigstop_lttng_sessiond_notap()
772 {
773 sigstop_lttng_sessiond_opt 0 "$@"
774 }
775
776 function stop_lttng_consumerd_opt()
777 {
778 local withtap=$1
779 local signal=$2
780
781 if [ -z "$signal" ]; then
782 signal=SIGTERM
783 fi
784
785 local timeout_s=$3
786 local dtimeleft_s=
787
788 # Multiply time by 2 to simplify integer arithmetic
789 if [ -n "$timeout_s" ]; then
790 dtimeleft_s=$((timeout_s * 2))
791 fi
792
793 local retval=0
794
795 PID_CONSUMERD="$(lttng_pgrep "$CONSUMERD_MATCH")"
796
797 if [ -z "$PID_CONSUMERD" ]; then
798 if [ "$withtap" -eq "1" ]; then
799 pass "No consumer daemon to kill"
800 fi
801 return 0
802 fi
803
804 diag "Killing (signal $signal) $CONSUMERD_BIN pids: $(echo "$PID_CONSUMERD" | tr '\n' ' ')"
805
806 # shellcheck disable=SC2086
807 if ! kill -s $signal $PID_CONSUMERD 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST; then
808 retval=1
809 if [ "$withtap" -eq "1" ]; then
810 fail "Kill consumer daemon"
811 fi
812 else
813 out=1
814 while [ $out -ne 0 ]; do
815 pid="$(lttng_pgrep "$CONSUMERD_MATCH")"
816
817 # If consumerds are still present check their status.
818 # A zombie status qualifies the consumerd as *killed*
819 out=0
820 for consumer_pid in $pid; do
821 state="$(ps -p "$consumer_pid" -o state= )"
822 if [[ -n "$state" && "$state" != "Z" ]]; then
823 out=1
824 fi
825 done
826 if [ -n "$dtimeleft_s" ]; then
827 if [ $dtimeleft_s -lt 0 ]; then
828 out=0
829 retval=1
830 fi
831 dtimeleft_s=$((dtimeleft_s - 1))
832 fi
833 sleep 0.5
834 done
835 if [ "$withtap" -eq "1" ]; then
836 if [ "$retval" -eq "0" ]; then
837 pass "Wait after kill consumer daemon"
838 else
839 fail "Wait after kill consumer daemon"
840 fi
841 fi
842 fi
843
844 return $retval
845 }
846
847 function stop_lttng_consumerd()
848 {
849 stop_lttng_consumerd_opt 1 "$@"
850 }
851
852 function stop_lttng_consumerd_notap()
853 {
854 stop_lttng_consumerd_opt 0 "$@"
855 }
856
857 function sigstop_lttng_consumerd_opt()
858 {
859 local withtap=$1
860 local signal=SIGSTOP
861
862 PID_CONSUMERD="$(lttng_pgrep "$CONSUMERD_MATCH")"
863
864 diag "Sending SIGSTOP to $CONSUMERD_BIN pids: $(echo "$PID_CONSUMERD" | tr '\n' ' ')"
865
866 # shellcheck disable=SC2086
867 kill -s $signal $PID_CONSUMERD 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
868 retval=$?
869
870 if [ $retval -eq 1 ]; then
871 if [ "$withtap" -eq "1" ]; then
872 fail "Sending SIGSTOP to consumer daemon"
873 fi
874 return 1
875 else
876 out=1
877 while [ $out -ne 0 ]; do
878 pid="$(lttng_pgrep "$CONSUMERD_MATCH")"
879
880 # Wait until state becomes stopped for all
881 # consumers.
882 out=0
883 for consumer_pid in $pid; do
884 state="$(ps -p "$consumer_pid" -o state= )"
885 if [[ -n "$state" && "$state" != "T" ]]; then
886 out=1
887 fi
888 done
889 sleep 0.5
890 done
891 if [ "$withtap" -eq "1" ]; then
892 pass "Sending SIGSTOP to consumer daemon"
893 fi
894 fi
895 return $retval
896 }
897
898 function sigstop_lttng_consumerd()
899 {
900 sigstop_lttng_consumerd_opt 1 "$@"
901 }
902
903 function sigstop_lttng_consumerd_notap()
904 {
905 sigstop_lttng_consumerd_opt 0 "$@"
906 }
907
908 function list_lttng_with_opts ()
909 {
910 local opts=$1
911 $TESTDIR/../src/bin/lttng/$LTTNG_BIN list $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
912 ok $? "Lttng-tool list command with option $opts"
913 }
914
915 function create_lttng_session_no_output ()
916 {
917 local sess_name=$1
918 local opts="${@:2}"
919
920 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
921 ok $? "Create session $sess_name in no-output mode"
922 }
923
924 function create_lttng_session_uri () {
925 local sess_name=$1
926 local uri=$2
927 local opts="${@:3}"
928
929 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -U $uri $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
930 ok $? "Create session $sess_name with uri:$uri and opts: $opts"
931 }
932
933 function create_lttng_session ()
934 {
935 local withtap=$1
936 local expected_to_fail=$2
937 local sess_name=$3
938 local trace_path=$4
939 local opt=$5
940
941 if [ -z "$trace_path" ]; then
942 # Use lttng-sessiond default output.
943 trace_path=""
944 else
945 trace_path="-o $trace_path"
946 fi
947
948 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create "$sess_name" $trace_path $opt 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
949 ret=$?
950 if [ $expected_to_fail -eq "1" ]; then
951 test "$ret" -ne "0"
952 ret=$?
953 if [ $withtap -eq "1" ]; then
954 ok $ret "Create session $sess_name in $trace_path failed as expected"
955 fi
956 else
957 if [ $withtap -eq "1" ]; then
958 ok $ret "Create session $sess_name in $trace_path"
959 fi
960 fi
961 return $ret
962 }
963
964 function create_lttng_session_ok ()
965 {
966 create_lttng_session 1 0 "$@"
967 }
968
969 function create_lttng_session_fail ()
970 {
971 create_lttng_session 1 1 "$@"
972 }
973
974 function create_lttng_session_notap ()
975 {
976 create_lttng_session 0 0 "$@"
977 }
978
979
980 function enable_ust_lttng_channel ()
981 {
982 local withtap=$1
983 local expected_to_fail=$2
984 local sess_name=$3
985 local channel_name=$4
986 local opts="${@:5}"
987
988 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
989 ret=$?
990 if [[ $expected_to_fail -eq "1" ]]; then
991 test "$ret" -ne "0"
992 ret=$?
993 if [ $withtap -eq "1" ]; then
994 ok $ret "Enable channel $channel_name for session $sess_name failed as expected"
995 fi
996 else
997 if [ $withtap -eq "1" ]; then
998 ok $ret "Enable channel $channel_name for session $sess_name"
999 fi
1000 fi
1001 return $ret
1002 }
1003
1004 function enable_ust_lttng_channel_ok ()
1005 {
1006 enable_ust_lttng_channel 1 0 "$@"
1007 }
1008
1009 function enable_ust_lttng_channel_fail ()
1010 {
1011 enable_ust_lttng_channel 1 1 "$@"
1012 }
1013
1014 function enable_ust_lttng_channel_notap ()
1015 {
1016 enable_ust_lttng_channel 0 0 "$@"
1017 }
1018
1019 function disable_ust_lttng_channel()
1020 {
1021 local sess_name=$1
1022 local channel_name=$2
1023
1024 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1025 ok $? "Disable channel $channel_name for session $sess_name"
1026 }
1027
1028 function enable_lttng_mmap_overwrite_kernel_channel()
1029 {
1030 local sess_name=$1
1031 local channel_name=$2
1032
1033 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1034 ok $? "Enable channel $channel_name for session $sess_name"
1035 }
1036
1037 function enable_lttng_mmap_discard_small_kernel_channel()
1038 {
1039 local sess_name=$1
1040 local channel_name=$2
1041
1042 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --discard --subbuf-size=$(getconf PAGE_SIZE) --num-subbuf=2 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1043 ok $? "Enable small discard channel $channel_name for session $sess_name"
1044 }
1045
1046 function enable_lttng_mmap_overwrite_small_kernel_channel()
1047 {
1048 local sess_name=$1
1049 local channel_name=$2
1050
1051 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite --subbuf-size=$(getconf PAGE_SIZE) --num-subbuf=2 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1052 ok $? "Enable small discard channel $channel_name for session $sess_name"
1053 }
1054
1055 function enable_lttng_mmap_overwrite_ust_channel()
1056 {
1057 local sess_name=$1
1058 local channel_name=$2
1059
1060 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1061 ok $? "Enable channel $channel_name for session $sess_name"
1062 }
1063
1064 function enable_ust_lttng_event ()
1065 {
1066 local withtap=$1
1067 local expected_to_fail=$2
1068 local sess_name=$3
1069 local event_name="$4"
1070 local channel_name=$5
1071
1072 if [ -z $channel_name ]; then
1073 # default channel if none specified
1074 chan=""
1075 else
1076 chan="-c $channel_name"
1077 fi
1078
1079 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -u 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1080 ret=$?
1081 if [[ $expected_to_fail -eq "1" ]]; then
1082 test $ret -ne "0"
1083 ret=$?
1084 if [[ $withtap -eq "1" ]]; then
1085 ok $ret "Enable ust event $event_name for session $session_name failed as expected"
1086 fi
1087 else
1088 if [[ $withtap -eq "1" ]]; then
1089 ok $ret "Enable ust event $event_name for session $sess_name"
1090 fi
1091 fi
1092 return $ret
1093 }
1094
1095 function enable_ust_lttng_event_ok ()
1096 {
1097 enable_ust_lttng_event 1 0 "$@"
1098 }
1099
1100 function enable_ust_lttng_event_fail ()
1101 {
1102 enable_ust_lttng_event 1 1 "$@"
1103 }
1104
1105 function enable_ust_lttng_event_notap ()
1106 {
1107 enable_ust_lttng_event 0 0 "$@"
1108 }
1109
1110 function enable_jul_lttng_event()
1111 {
1112 sess_name=$1
1113 event_name="$2"
1114 channel_name=$3
1115
1116 if [ -z $channel_name ]; then
1117 # default channel if none specified
1118 chan=""
1119 else
1120 chan="-c $channel_name"
1121 fi
1122
1123 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1124 ok $? "Enable JUL event $event_name for session $sess_name"
1125 }
1126
1127 function enable_jul_lttng_event_loglevel()
1128 {
1129 local sess_name=$1
1130 local event_name="$2"
1131 local loglevel=$3
1132 local channel_name=$4
1133
1134 if [ -z $channel_name ]; then
1135 # default channel if none specified
1136 chan=""
1137 else
1138 chan="-c $channel_name"
1139 fi
1140
1141 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -j 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1142 ok $? "Enable JUL event $event_name for session $sess_name with loglevel $loglevel"
1143 }
1144
1145 function enable_log4j_lttng_event()
1146 {
1147 sess_name=$1
1148 event_name="$2"
1149 channel_name=$3
1150
1151 if [ -z $channel_name ]; then
1152 # default channel if none specified
1153 chan=""
1154 else
1155 chan="-c $channel_name"
1156 fi
1157
1158 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -l 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1159 ok $? "Enable LOG4J event $event_name for session $sess_name"
1160 }
1161
1162 function enable_log4j_lttng_event_loglevel()
1163 {
1164 local sess_name=$1
1165 local event_name="$2"
1166 local loglevel=$3
1167 local channel_name=$4
1168
1169 if [ -z $channel_name ]; then
1170 # default channel if none specified
1171 chan=""
1172 else
1173 chan="-c $channel_name"
1174 fi
1175
1176 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -l 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1177 ok $? "Enable LOG4J event $event_name for session $sess_name with loglevel $loglevel"
1178 }
1179
1180 function enable_python_lttng_event()
1181 {
1182 sess_name=$1
1183 event_name="$2"
1184 channel_name=$3
1185
1186 if [ -z $channel_name ]; then
1187 # default channel if none specified
1188 chan=""
1189 else
1190 chan="-c $channel_name"
1191 fi
1192
1193 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -p 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1194 ok $? "Enable Python event $event_name for session $sess_name"
1195 }
1196
1197 function enable_python_lttng_event_loglevel()
1198 {
1199 local sess_name=$1
1200 local event_name="$2"
1201 local loglevel=$3
1202 local channel_name=$4
1203
1204 if [ -z $channel_name ]; then
1205 # default channel if none specified
1206 chan=""
1207 else
1208 chan="-c $channel_name"
1209 fi
1210
1211 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -p 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1212 ok $? "Enable Python event $event_name for session $sess_name with loglevel $loglevel"
1213 }
1214
1215 function enable_ust_lttng_event_filter()
1216 {
1217 local sess_name="$1"
1218 local event_name="$2"
1219 local filter="$3"
1220 local channel_name=$4
1221
1222 if [ -z $channel_name ]; then
1223 # default channel if none specified
1224 chan=""
1225 else
1226 chan="-c $channel_name"
1227 fi
1228
1229 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $chan "$event_name" -s $sess_name -u --filter "$filter" 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1230 ok $? "Enable event $event_name with filtering for session $sess_name"
1231 }
1232
1233 function enable_ust_lttng_event_loglevel()
1234 {
1235 local sess_name="$1"
1236 local event_name="$2"
1237 local loglevel="$3"
1238
1239 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1240 ok $? "Enable event $event_name with loglevel $loglevel"
1241 }
1242
1243 function enable_ust_lttng_event_loglevel_only()
1244 {
1245 local sess_name="$1"
1246 local event_name="$2"
1247 local loglevel="$3"
1248
1249 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1250 ok $? "Enable event $event_name with loglevel-only $loglevel"
1251 }
1252
1253 function disable_ust_lttng_event ()
1254 {
1255 local sess_name="$1"
1256 local event_name="$2"
1257 local channel_name="$3"
1258
1259 if [ -z $channel_name ]; then
1260 # default channel if none specified
1261 chan=""
1262 else
1263 chan="-c $channel_name"
1264 fi
1265
1266 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name $chan -u 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1267 ok $? "Disable event $event_name for session $sess_name"
1268 }
1269
1270 function disable_jul_lttng_event ()
1271 {
1272 local sess_name="$1"
1273 local event_name="$2"
1274
1275 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -j >/dev/null 2>&1
1276 ok $? "Disable JUL event $event_name for session $sess_name"
1277 }
1278
1279 function disable_log4j_lttng_event ()
1280 {
1281 local sess_name="$1"
1282 local event_name="$2"
1283
1284 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -l >/dev/null 2>&1
1285 ok $? "Disable LOG4J event $event_name for session $sess_name"
1286 }
1287
1288 function disable_python_lttng_event ()
1289 {
1290 local sess_name="$1"
1291 local event_name="$2"
1292
1293 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -p 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1294 ok $? "Disable Python event $event_name for session $sess_name"
1295 }
1296
1297 function start_lttng_tracing_opt ()
1298 {
1299 local withtap=$1
1300 local expected_to_fail=$2
1301 local sess_name=$3
1302
1303 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1304 ret=$?
1305 if [[ $expected_to_fail -eq "1" ]]; then
1306 test "$ret" -ne "0"
1307 ret=$?
1308 if [ $withtap -eq "1" ]; then
1309 ok $? "Start tracing for session $sess_name failed as expected"
1310 fi
1311 else
1312 if [ $withtap -eq "1" ]; then
1313 ok $ret "Start tracing for session $sess_name"
1314 fi
1315 fi
1316 }
1317
1318 function start_lttng_tracing_ok ()
1319 {
1320 start_lttng_tracing_opt 1 0 "$@"
1321 }
1322
1323 function start_lttng_tracing_fail ()
1324 {
1325 start_lttng_tracing_opt 1 1 "$@"
1326 }
1327
1328 function start_lttng_tracing_notap ()
1329 {
1330 start_lttng_tracing_opt 0 1 "$@"
1331 }
1332
1333 function stop_lttng_tracing_opt ()
1334 {
1335 local withtap=$1
1336 local expected_to_fail=$2
1337 local sess_name=$3
1338
1339 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1340 ret=$?
1341 if [[ $expected_to_fail -eq "1" ]]; then
1342 test "$ret" -ne "0"
1343 ret=$?
1344 if [ $withtap -eq "1" ]; then
1345 ok $? "Stop lttng tracing for session $sess_name failed as expected"
1346 fi
1347 else
1348 if [ $withtap -eq "1" ]; then
1349 ok $ret "Stop lttng tracing for session $sess_name"
1350 fi
1351 fi
1352 }
1353
1354 function stop_lttng_tracing_ok ()
1355 {
1356 stop_lttng_tracing_opt 1 0 "$@"
1357 }
1358
1359 function stop_lttng_tracing_fail ()
1360 {
1361 stop_lttng_tracing_opt 1 1 "$@"
1362 }
1363
1364 function stop_lttng_tracing_notap ()
1365 {
1366 stop_lttng_tracing_opt 0 0 "$@"
1367 }
1368
1369 function destroy_lttng_session ()
1370 {
1371 local withtap=$1
1372 local expected_to_fail=$2
1373 local sess_name=$3
1374
1375 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1376 ret=$?
1377 if [[ $expected_to_fail -eq "1" ]]; then
1378 test "$ret" -ne "0"
1379 ret=$?
1380 if [ $withtap -eq "1" ]; then
1381 ok $ret "Destroy session $sess_name failed as expected"
1382 fi
1383 else
1384 if [ $withtap -eq "1" ]; then
1385 ok $ret "Destroy session $sess_name"
1386 fi
1387 fi
1388 }
1389
1390 function destroy_lttng_session_ok ()
1391 {
1392 destroy_lttng_session 1 0 "$@"
1393
1394 }
1395
1396 function destroy_lttng_session_fail ()
1397 {
1398 destroy_lttng_session 1 1 "$@"
1399 }
1400
1401 function destroy_lttng_session_notap ()
1402 {
1403 destroy_lttng_session 0 0 "$@"
1404 }
1405
1406 function destroy_lttng_sessions ()
1407 {
1408 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy --all 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1409 ok $? "Destroy all lttng sessions"
1410 }
1411
1412 function lttng_snapshot_add_output ()
1413 {
1414 local expected_to_fail=$1
1415 local sess_name=$2
1416 local trace_path=$3
1417 local opts=$4
1418
1419 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name $trace_path $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1420 ret=$?
1421 if [[ $expected_to_fail -eq 1 ]]; then
1422 test "$ret" -ne "0"
1423 ok $? "Added snapshot output $trace_path failed as expected"
1424 else
1425 ok $ret "Added snapshot output $trace_path"
1426 fi
1427 }
1428
1429 function lttng_snapshot_add_output_ok ()
1430 {
1431 lttng_snapshot_add_output 0 "$@"
1432 }
1433
1434 function lttng_snapshot_add_output_fail ()
1435 {
1436 lttng_snapshot_add_output 1 "$@"
1437 }
1438
1439 function lttng_snapshot_del_output ()
1440 {
1441 local expected_to_fail=$1
1442 local sess_name=$2
1443 local id=$3
1444
1445 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1446 ret=$?
1447 if [[ $expected_to_fail -eq "1" ]]; then
1448 test "$ret" -ne "0"
1449 ok $? "Deleted snapshot output id $id failed as expected"
1450 else
1451 ok $ret "Deleted snapshot output id $id"
1452 fi
1453 }
1454
1455 function lttng_snapshot_del_output_ok ()
1456 {
1457 lttng_snapshot_del_output 0 "$@"
1458 }
1459
1460 function lttng_snapshot_del_output_fail ()
1461 {
1462 lttng_snapshot_del_output 1 "$@"
1463 }
1464
1465 function lttng_snapshot_record ()
1466 {
1467 local sess_name=$1
1468
1469 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name $trace_path 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1470 ok $? "Snapshot recorded"
1471 }
1472
1473 function lttng_snapshot_list ()
1474 {
1475 local sess_name=$1
1476 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot list-output -s $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1477 ok $? "Snapshot list"
1478 }
1479
1480 function lttng_save()
1481 {
1482 local sess_name=$1
1483 local opts=$2
1484
1485 $TESTDIR/../src/bin/lttng/$LTTNG_BIN save $sess_name $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1486 ok $? "Session saved"
1487 }
1488
1489 function lttng_load()
1490 {
1491 local expected_to_fail=$1
1492 local opts=$2
1493
1494 $TESTDIR/../src/bin/lttng/$LTTNG_BIN load $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1495 ret=$?
1496 if [[ $expected_to_fail -eq "1" ]]; then
1497 test $ret -ne "0"
1498 ok $? "Load command failed as expected with opts: $opts"
1499 else
1500 ok $ret "Load command with opts: $opts"
1501 fi
1502 }
1503
1504 function lttng_load_ok()
1505 {
1506 lttng_load 0 "$@"
1507 }
1508
1509 function lttng_load_fail()
1510 {
1511 lttng_load 1 "$@"
1512 }
1513
1514 function lttng_track()
1515 {
1516 local expected_to_fail="$1"
1517 shift 1
1518 local opts="$@"
1519 $TESTDIR/../src/bin/lttng/$LTTNG_BIN track $opts >$OUTPUT_DEST
1520 ret=$?
1521 if [[ $expected_to_fail -eq "1" ]]; then
1522 test $ret -ne "0"
1523 ok $? "Track command failed as expected with opts: $opts"
1524 else
1525 ok $ret "Track command with opts: $opts"
1526 fi
1527 }
1528
1529 function lttng_track_ok()
1530 {
1531 lttng_track 0 "$@"
1532 }
1533
1534 function lttng_track_fail()
1535 {
1536 lttng_track 1 "$@"
1537 }
1538
1539 function lttng_untrack()
1540 {
1541 local expected_to_fail="$1"
1542 shift 1
1543 local opts="$@"
1544 $TESTDIR/../src/bin/lttng/$LTTNG_BIN untrack $opts >$OUTPUT_DEST
1545 ret=$?
1546 if [[ $expected_to_fail -eq "1" ]]; then
1547 test $ret -ne "0"
1548 ok $? "Untrack command failed as expected with opts: $opts"
1549 else
1550 ok $ret "Untrack command with opts: $opts"
1551 fi
1552 }
1553
1554 function lttng_untrack_ok()
1555 {
1556 lttng_untrack 0 "$@"
1557 }
1558
1559 function lttng_untrack_fail()
1560 {
1561 lttng_untrack 1 "$@"
1562 }
1563
1564 function lttng_track_pid_ok()
1565 {
1566 PID=$1
1567 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" track --kernel --pid=$PID 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1568 ok $? "Lttng track pid on the kernel domain"
1569 }
1570
1571 function lttng_untrack_kernel_all_ok()
1572 {
1573 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" untrack --kernel --pid --all 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1574 ok $? "Lttng untrack all pid on the kernel domain"
1575 }
1576
1577 function lttng_track_ust_ok()
1578 {
1579 lttng_track_ok -u "$@"
1580 }
1581
1582 function lttng_track_ust_fail()
1583 {
1584 lttng_track_fail -u "$@"
1585 }
1586
1587 function lttng_track_kernel_ok()
1588 {
1589 lttng_track_ok -k "$@"
1590 }
1591
1592 function lttng_track_kernel_fail()
1593 {
1594 lttng_track_fail -k "$@"
1595 }
1596
1597 function lttng_untrack_ust_ok()
1598 {
1599 lttng_untrack_ok -u "$@"
1600 }
1601
1602 function lttng_untrack_ust_fail()
1603 {
1604 lttng_untrack_fail -u "$@"
1605 }
1606
1607 function lttng_untrack_kernel_ok()
1608 {
1609 lttng_untrack_ok -k "$@"
1610 }
1611
1612 function lttng_untrack_kernel_fail()
1613 {
1614 lttng_untrack_fail -k "$@"
1615 }
1616
1617 function lttng_add_context_list()
1618 {
1619 $TESTDIR/../src/bin/lttng/$LTTNG_BIN add-context --list 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1620 ret=$?
1621 ok $ret "Context listing"
1622 }
1623
1624 function add_context_lttng()
1625 {
1626 local expected_to_fail="$1"
1627 local domain="$2"
1628 local session_name="$3"
1629 local channel_name="$4"
1630 local type="$5"
1631
1632 $TESTDIR/../src/bin/lttng/$LTTNG_BIN add-context -s $session_name -c $channel_name -t $type $domain 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1633 ret=$?
1634 if [[ $expected_to_fail -eq "1" ]]; then
1635 test $ret -ne "0"
1636 ok $? "Add context command failed as expected for type: $type"
1637 else
1638 ok $ret "Add context command for type: $type"
1639 fi
1640 }
1641
1642 function add_context_ust_ok()
1643 {
1644 add_context_lttng 0 -u "$@"
1645 }
1646
1647 function add_context_ust_fail()
1648 {
1649 add_context_lttng 1 -u "$@"
1650 }
1651
1652 function add_context_kernel_ok()
1653 {
1654 add_context_lttng 0 -k "$@"
1655 }
1656
1657 function add_context_kernel_fail()
1658 {
1659 add_context_lttng 1 -k "$@"
1660 }
1661
1662 function wait_live_trace_ready ()
1663 {
1664 local url=$1
1665 local zero_client_match=0
1666
1667 diag "Waiting for live trace at url: $url"
1668 while [ $zero_client_match -eq 0 ]; do
1669 zero_client_match=$($BABELTRACE_BIN -i lttng-live $url | grep "0 client(s) connected" | wc -l)
1670 sleep 0.5
1671 done
1672 pass "Waiting for live trace at url: $url"
1673 }
1674
1675 function wait_live_viewer_connect ()
1676 {
1677 local url=$1
1678 local one_client_match=0
1679
1680 diag "Waiting for live viewers on url: $url"
1681 while [ $one_client_match -eq 0 ]; do
1682 one_client_match=$($BABELTRACE_BIN -i lttng-live $url | grep "1 client(s) connected" | wc -l)
1683 sleep 0.5
1684 done
1685 pass "Waiting for live viewers on url: $url"
1686 }
1687
1688 function validate_metadata_event ()
1689 {
1690 local event_name=$1
1691 local nr_event_id=$2
1692 local trace_path=$3
1693
1694 local metadata_file=$(find $trace_path | grep metadata)
1695 local metadata_path=$(dirname $metadata_file)
1696
1697 which $BABELTRACE_BIN >/dev/null
1698 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
1699
1700 local count=$($BABELTRACE_BIN --output-format=ctf-metadata $metadata_path | grep $event_name | wc -l)
1701
1702 if [ "$count" -ne "$nr_event_id" ]; then
1703 fail "Metadata match with the metadata of $count event(s) named $event_name"
1704 diag "$count matching event id found in metadata"
1705 else
1706 pass "Metadata match with the metadata of $count event(s) named $event_name"
1707 fi
1708
1709 }
1710
1711 function trace_matches ()
1712 {
1713 local event_name=$1
1714 local nr_iter=$2
1715 local trace_path=$3
1716
1717 which $BABELTRACE_BIN >/dev/null
1718 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
1719
1720 local count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
1721
1722 if [ "$count" -ne "$nr_iter" ]; then
1723 fail "Trace match"
1724 diag "$count matching events found in trace"
1725 else
1726 pass "Trace match"
1727 fi
1728 }
1729
1730 function trace_match_only()
1731 {
1732 local event_name=$1
1733 local nr_iter=$2
1734 local trace_path=$3
1735
1736 which $BABELTRACE_BIN >/dev/null
1737 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
1738
1739 local count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
1740 local total=$($BABELTRACE_BIN $trace_path | wc -l)
1741
1742 if [ "$nr_iter" -eq "$count" ] && [ "$total" -eq "$nr_iter" ]; then
1743 pass "Trace match with $total event $event_name"
1744 else
1745 fail "Trace match"
1746 diag "$total event(s) found, expecting $nr_iter of event $event_name and only found $count"
1747 fi
1748 }
1749
1750 function validate_trace
1751 {
1752 local event_name=$1
1753 local trace_path=$2
1754
1755 which $BABELTRACE_BIN >/dev/null
1756 if [ $? -ne 0 ]; then
1757 skip 0 "Babeltrace binary not found. Skipping trace validation"
1758 fi
1759
1760 OLDIFS=$IFS
1761 IFS=","
1762 for i in $event_name; do
1763 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
1764 if [ "$traced" -ne 0 ]; then
1765 pass "Validate trace for event $i, $traced events"
1766 else
1767 fail "Validate trace for event $i"
1768 diag "Found $traced occurences of $i"
1769 fi
1770 done
1771 ret=$?
1772 IFS=$OLDIFS
1773 return $ret
1774 }
1775
1776 function validate_trace_count
1777 {
1778 local event_name=$1
1779 local trace_path=$2
1780 local expected_count=$3
1781
1782 which $BABELTRACE_BIN >/dev/null
1783 if [ $? -ne 0 ]; then
1784 skip 0 "Babeltrace binary not found. Skipping trace validation"
1785 fi
1786
1787 cnt=0
1788 OLDIFS=$IFS
1789 IFS=","
1790 for i in $event_name; do
1791 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
1792 if [ "$traced" -ne 0 ]; then
1793 pass "Validate trace for event $i, $traced events"
1794 else
1795 fail "Validate trace for event $i"
1796 diag "Found $traced occurences of $i"
1797 fi
1798 cnt=$(($cnt + $traced))
1799 done
1800 IFS=$OLDIFS
1801 test $cnt -eq $expected_count
1802 ok $? "Read a total of $cnt events, expected $expected_count"
1803 }
1804
1805 function validate_trace_count_range_incl_min_excl_max
1806 {
1807 local event_name=$1
1808 local trace_path=$2
1809 local expected_min=$3
1810 local expected_max=$4
1811
1812 which $BABELTRACE_BIN >/dev/null
1813 if [ $? -ne 0 ]; then
1814 skip 0 "Babeltrace binary not found. Skipping trace validation"
1815 fi
1816
1817 cnt=0
1818 OLDIFS=$IFS
1819 IFS=","
1820 for i in $event_name; do
1821 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
1822 if [ "$traced" -ge $expected_min ]; then
1823 pass "Validate trace for event $i, $traced events"
1824 else
1825 fail "Validate trace for event $i"
1826 diag "Found $traced occurences of $i"
1827 fi
1828 cnt=$(($cnt + $traced))
1829 done
1830 IFS=$OLDIFS
1831 test $cnt -lt $expected_max
1832 ok $? "Read a total of $cnt events, expected between [$expected_min, $expected_max["
1833 }
1834
1835 function trace_first_line
1836 {
1837 local trace_path=$1
1838
1839 which $BABELTRACE_BIN >/dev/null
1840 if [ $? -ne 0 ]; then
1841 skip 0 "Babeltrace binary not found. Skipping trace validation"
1842 fi
1843
1844 $BABELTRACE_BIN $trace_path 2>/dev/null | head -n 1
1845 }
1846
1847 function validate_trace_exp()
1848 {
1849 local event_exp=$1
1850 local trace_path=$2
1851
1852 which $BABELTRACE_BIN >/dev/null
1853 skip $? -ne 0 "Babeltrace binary not found. Skipping trace validation"
1854
1855 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep --extended-regexp ${event_exp} | wc -l)
1856 if [ "$traced" -ne 0 ]; then
1857 pass "Validate trace for expression '${event_exp}', $traced events"
1858 else
1859 fail "Validate trace for expression '${event_exp}'"
1860 diag "Found $traced occurences of '${event_exp}'"
1861 fi
1862 ret=$?
1863 return $ret
1864 }
1865
1866 function validate_trace_only_exp()
1867 {
1868 local event_exp=$1
1869 local trace_path=$2
1870
1871 which $BABELTRACE_BIN >/dev/null
1872 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
1873
1874 local count=$($BABELTRACE_BIN $trace_path | grep --extended-regexp ${event_exp} | wc -l)
1875 local total=$($BABELTRACE_BIN $trace_path | wc -l)
1876
1877 if [ "$count" -ne 0 ] && [ "$total" -eq "$count" ]; then
1878 pass "Trace match with $total for expression '${event_exp}'"
1879 else
1880 fail "Trace match"
1881 diag "$total syscall event(s) found, only syscalls matching expression '${event_exp}' ($count occurrences) are expected"
1882 fi
1883 ret=$?
1884 return $ret
1885 }
1886
1887 function validate_trace_empty()
1888 {
1889 local trace_path=$1
1890
1891 which $BABELTRACE_BIN >/dev/null
1892 if [ $? -ne 0 ]; then
1893 skip 0 "Babeltrace binary not found. Skipping trace validation"
1894 fi
1895
1896 events=$($BABELTRACE_BIN $trace_path 2>/dev/null)
1897 ret=$?
1898 if [ $ret -ne 0 ]; then
1899 fail "Failed to parse trace"
1900 return $ret
1901 fi
1902
1903 traced=$(echo -n "$events" | wc -l)
1904 if [ "$traced" -eq 0 ]; then
1905 pass "Validate empty trace"
1906 else
1907 fail "Validate empty trace"
1908 diag "Found $traced events in trace"
1909 fi
1910 ret=$?
1911 return $ret
1912 }
1913
1914 function validate_directory_empty ()
1915 {
1916 local trace_path="$1"
1917
1918 # Do not double quote `$trace_path` below as we want wildcards to be
1919 # expanded.
1920 files="$(ls -A $trace_path)"
1921 ret=$?
1922 if [ $ret -ne 0 ]; then
1923 fail "Failed to list content of directory \"$trace_path\""
1924 return $ret
1925 fi
1926
1927 nb_files="$(echo -n "$files" | wc -l)"
1928 ok $nb_files "Directory \"$trace_path\" is empty"
1929 }
1930
1931 function validate_trace_session_ust_empty()
1932 {
1933 validate_directory_empty "$1"/ust
1934 }
1935
1936 function validate_trace_session_kernel_empty()
1937 {
1938 validate_trace_empty "$1"/kernel
1939 }
1940
1941 function regenerate_metadata ()
1942 {
1943 local expected_to_fail=$1
1944 local sess_name=$2
1945
1946 $TESTDIR/../src/bin/lttng/$LTTNG_BIN regenerate metadata -s $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1947 ret=$?
1948 if [[ $expected_to_fail -eq "1" ]]; then
1949 test "$ret" -ne "0"
1950 ok $? "Expected fail on regenerate metadata $sess_name"
1951 else
1952 ok $ret "Metadata regenerate $sess_name"
1953 fi
1954 }
1955
1956 function regenerate_metadata_ok ()
1957 {
1958 regenerate_metadata 0 "$@"
1959 }
1960
1961 function regenerate_metadata_fail ()
1962 {
1963 regenerate_metadata 1 "$@"
1964 }
1965
1966 function regenerate_statedump ()
1967 {
1968 local expected_to_fail=$1
1969 local sess_name=$2
1970
1971 $TESTDIR/../src/bin/lttng/$LTTNG_BIN regenerate statedump -s $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1972 ret=$?
1973 if [[ $expected_to_fail -eq "1" ]]; then
1974 test "$ret" -ne "0"
1975 ok $? "Expected fail on regenerate statedump $sess_name"
1976 else
1977 ok $ret "Statedump regenerate $sess_name"
1978 fi
1979 }
1980
1981 function regenerate_statedump_ok ()
1982 {
1983 regenerate_statedump 0 "$@"
1984 }
1985
1986 function regenerate_statedump_fail ()
1987 {
1988 regenerate_statedump 1 "$@"
1989 }
1990
1991 function rotate_session ()
1992 {
1993 local expected_to_fail=$1
1994 local sess_name=$2
1995
1996 $TESTDIR/../src/bin/lttng/$LTTNG_BIN rotate $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
1997 ret=$?
1998 if [[ $expected_to_fail -eq "1" ]]; then
1999 test "$ret" -ne "0"
2000 ok $? "Expected fail on rotate session $sess_name"
2001 else
2002 ok $ret "Rotate session $sess_name"
2003 fi
2004 }
2005
2006 function rotate_session_ok ()
2007 {
2008 rotate_session 0 "$@"
2009 }
2010
2011 function rotate_session_fail ()
2012 {
2013 rotate_session 1 "$@"
2014 }
2015
2016 function destructive_tests_enabled ()
2017 {
2018 if [ ${LTTNG_ENABLE_DESTRUCTIVE_TESTS} = "will-break-my-system" ]; then
2019 return 0
2020 else
2021 return 1
2022 fi
2023 }
2024
2025 function lttng_enable_rotation_timer ()
2026 {
2027 local expected_to_fail=$1
2028 local sess_name=$2
2029 local period=$3
2030
2031 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-rotation -s $sess_name --timer $period 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
2032 ret=$?
2033 if [[ $expected_to_fail -eq "1" ]]; then
2034 test "$ret" -ne "0"
2035 ok $? "Expected fail when setting periodic rotation ($period) of session $sess_name"
2036 else
2037 ok $ret "Set periodic rotation ($period) of session $sess_name"
2038 fi
2039 }
2040
2041 function lttng_enable_rotation_timer_ok ()
2042 {
2043 lttng_enable_rotation_timer 0 $@
2044 }
2045
2046 function lttng_enable_rotation_timer_fail ()
2047 {
2048 lttng_enable_rotation_timer 1 $@
2049 }
2050
2051 function lttng_enable_rotation_size ()
2052 {
2053 local expected_to_fail=$1
2054 local sess_name=$2
2055 local size=$3
2056
2057 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-rotation -s $sess_name --size $size 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
2058 ret=$?
2059 if [[ $expected_to_fail -eq "1" ]]; then
2060 test "$ret" -ne "0"
2061 ok $? "Expected fail on rotate session $sess_name"
2062 else
2063 ok $ret "Rotate session $sess_name"
2064 fi
2065 }
2066
2067 function lttng_enable_rotation_size_ok ()
2068 {
2069 lttng_enable_rotation_size 0 $@
2070 }
2071
2072 function lttng_enable_rotation_size_fail ()
2073 {
2074 lttng_enable_rotation_size 1 $@
2075 }
2076
2077 function lttng_clear_session ()
2078 {
2079 local expected_to_fail=$1
2080 local sess_name=$2
2081
2082 $TESTDIR/../src/bin/lttng/$LTTNG_BIN clear $sess_name 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
2083 ret=$?
2084 if [[ $expected_to_fail -eq "1" ]]; then
2085 test "$ret" -ne "0"
2086 ok $? "Expected fail on clear session $sess_name"
2087 else
2088 ok $ret "Clear session $sess_name"
2089 fi
2090 }
2091
2092 function lttng_clear_session_ok ()
2093 {
2094 lttng_clear_session 0 $@
2095 }
2096
2097 function lttng_clear_session_fail ()
2098 {
2099 lttng_clear_session 1 $@
2100 }
2101
2102 function lttng_clear_all ()
2103 {
2104 $TESTDIR/../src/bin/lttng/$LTTNG_BIN clear --all 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
2105 ok $? "Clear all lttng sessions"
2106 }
This page took 0.126949 seconds and 4 git commands to generate.