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