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