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