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