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