2 # SPDX-License-Identifier: GPL-2.0-only
4 # ftracetest - Ftrace test shell scripts
6 # Copyright (C) Hitachi Ltd., 2014
7 # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
10 usage
() { # errno [message]
11 [ ! -z "$2" ] && echo $2
12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
14 echo " -h|--help Show help message"
15 echo " -k|--keep Keep passed test logs"
16 echo " -v|--verbose Increase verbosity of test messages"
17 echo " -vv Alias of -v -v (Show all results in stdout)"
18 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
19 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
20 echo " -d|--debug Debug mode (trace all shell commands)"
21 echo " -l|--logdir <dir> Save logs on the <dir>"
22 echo " If <dir> is -, all logs output in console only"
29 # kselftest skip code is 4
37 # Ensuring user privilege
38 if [ `id -u` -ne 0 ]; then
39 errexit
"this must be run by root user"
43 absdir
() { # file_path
44 (cd `dirname $1`; pwd)
48 echo `absdir $1`/`basename $1`
51 find_testcases
() { #directory
52 echo `find $1 -name \*.tc | sort`
59 while [ ! -z "$1" ]; do
68 --verbose|
-v|
-vv|
-vvv)
69 if [ $VERBOSE -eq -1 ]; then
70 usage
"--console can not use with --verbose"
72 VERBOSE
=$
((VERBOSE
+ 1))
73 [ $1 = '-vv' ] && VERBOSE
=$
((VERBOSE
+ 1))
74 [ $1 = '-vvv' ] && VERBOSE
=$
((VERBOSE
+ 2))
78 if [ $VERBOSE -ne 0 ]; then
79 usage
"--console can not use with --verbose"
102 OPT_TEST_CASES
="$OPT_TEST_CASES `abspath $1`"
105 usage
1 "$1 is not a testcase"
110 OPT_TEST_DIR
=`abspath $1`
111 OPT_TEST_CASES
="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
114 usage
1 "Invalid option ($1)"
119 if [ ! -z "$OPT_TEST_CASES" ]; then
120 TEST_CASES
=$OPT_TEST_CASES
125 TRACING_DIR
=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
126 if [ -z "$TRACING_DIR" ]; then
127 DEBUGFS_DIR
=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
128 if [ -z "$DEBUGFS_DIR" ]; then
129 # If tracefs exists, then so does /sys/kernel/tracing
130 if [ -d "/sys/kernel/tracing" ]; then
131 mount
-t tracefs nodev
/sys
/kernel
/tracing ||
132 errexit
"Failed to mount /sys/kernel/tracing"
133 TRACING_DIR
="/sys/kernel/tracing"
134 # If debugfs exists, then so does /sys/kernel/debug
135 elif [ -d "/sys/kernel/debug" ]; then
136 mount
-t debugfs nodev
/sys
/kernel
/debug ||
137 errexit
"Failed to mount /sys/kernel/debug"
138 TRACING_DIR
="/sys/kernel/debug/tracing"
141 errexit
"debugfs and tracefs are not configured in this kernel"
144 TRACING_DIR
="$DEBUGFS_DIR/tracing"
147 if [ ! -d "$TRACING_DIR" ]; then
149 errexit
"ftrace is not configured in this kernel"
153 TEST_DIR
=$TOP_DIR/test.d
154 TEST_CASES
=`find_testcases $TEST_DIR`
155 LOG_DIR
=$TOP_DIR/logs
/`date +%Y%m%d-%H%M%S`/
161 # Parse command-line options
164 [ $DEBUG -ne 0 ] && set -x
167 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
168 errexit
"No ftrace directory found"
172 if [ "x$LOG_DIR" = "x-" ]; then
176 LOG_FILE
=$LOG_DIR/ftracetest.log
177 mkdir
-p $LOG_DIR || errexit
"Failed to make a log directory: $LOG_DIR"
182 # Check available colors on the terminal, if any
183 ncolors
=`tput colors 2>/dev/null || echo 0`
188 # If stdout exists and number of colors is eight or more, use them
189 if [ -t 1 -a "$ncolors" -ge 8 ]; then
190 color_reset
="\033[0m"
192 color_green
="\033[32m"
193 color_blue
="\033[34m"
197 # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead.
198 sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
203 if [ "$1" = "-n" ] ; then
208 [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc
>> $LOG_FILE
212 [ "$LOG_FILE" ] && cat $1 | strip_esc
>> $LOG_FILE
214 prlog
"=== Ftrace unit tests ==="
217 # Testcase management
218 # Test result codes - Dejagnu extended code
219 PASS
=0 # The test succeeded.
220 FAIL
=1 # The test failed, but was expected to succeed.
221 UNRESOLVED
=2 # The test produced indeterminate results. (e.g. interrupted)
222 UNTESTED
=3 # The test was not run, currently just a placeholder.
223 UNSUPPORTED
=4 # The test failed because of lack of feature.
224 XFAIL
=5 # The test failed, and was expected to fail.
238 testcase
() { # testfile
240 desc
=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
241 prlog
-n "[$CASENO]$INSTANCE$desc"
244 test_on_instance
() { # testfile
245 grep -q "^#[ \t]*flags:.*instance" $1
248 eval_result
() { # sigval
251 prlog
" [${color_green}PASS${color_reset}]"
252 PASSED_CASES
="$PASSED_CASES $CASENO"
256 prlog
" [${color_red}FAIL${color_reset}]"
257 FAILED_CASES
="$FAILED_CASES $CASENO"
258 return 1 # this is a bug.
261 prlog
" [${color_blue}UNRESOLVED${color_reset}]"
262 UNRESOLVED_CASES
="$UNRESOLVED_CASES $CASENO"
263 return 1 # this is a kind of bug.. something happened.
266 prlog
" [${color_blue}UNTESTED${color_reset}]"
267 UNTESTED_CASES
="$UNTESTED_CASES $CASENO"
271 prlog
" [${color_blue}UNSUPPORTED${color_reset}]"
272 UNSUPPORTED_CASES
="$UNSUPPORTED_CASES $CASENO"
273 return $UNSUPPORTED_RESULT # depends on use case
276 prlog
" [${color_red}XFAIL${color_reset}]"
277 XFAILED_CASES
="$XFAILED_CASES $CASENO"
281 prlog
" [${color_blue}UNDEFINED${color_reset}]"
282 UNDEFINED_CASES
="$UNDEFINED_CASES $CASENO"
283 return 1 # this must be a test bug
288 # Signal handling for result codes
290 SIG_BASE
=36 # Use realtime signals
297 SIG_FAIL
=$
((SIG_BASE
+ FAIL
))
301 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
303 SIG_UNRESOLVED
=$
((SIG_BASE
+ UNRESOLVED
))
305 kill -s $SIG_UNRESOLVED $SIG_PID
308 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
310 SIG_UNTESTED
=$
((SIG_BASE
+ UNTESTED
))
312 kill -s $SIG_UNTESTED $SIG_PID
315 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
317 SIG_UNSUPPORTED
=$
((SIG_BASE
+ UNSUPPORTED
))
318 exit_unsupported
() {
319 kill -s $SIG_UNSUPPORTED $SIG_PID
322 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
324 SIG_XFAIL
=$
((SIG_BASE
+ XFAIL
))
326 kill -s $SIG_XFAIL $SIG_PID
329 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
331 __run_test
() { # testfile
332 # setup PID and PPID, $$ is not updated.
333 (cd $TRACING_DIR; read PID _
< /proc
/self
/stat
; set -e; set -x; initialize_ftrace
; .
$1)
334 [ $?
-ne 0 ] && kill -s $SIG_FAIL $SIG_PID
338 run_test
() { # testfile
339 local testname
=`basename $1`
341 if [ ! -z "$LOG_FILE" ] ; then
342 local testlog
=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
344 local testlog
=/proc
/self
/fd
/1
346 export TMPDIR
=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
347 export FTRACETEST_ROOT
=$TOP_DIR
348 echo "execute$INSTANCE: "$1 > $testlog
350 if [ $VERBOSE -eq -1 ]; then
352 elif [ -z "$LOG_FILE" ]; then
354 elif [ $VERBOSE -ge 3 ]; then
355 __run_test
$1 |
tee -a $testlog 2>&1
356 elif [ $VERBOSE -eq 2 ]; then
357 __run_test
$1 2>> $testlog |
tee -a $testlog
359 __run_test
$1 >> $testlog 2>&1
361 eval_result
$SIG_RESULT
362 if [ $?
-eq 0 ]; then
363 # Remove test log if the test was done as it was expected.
364 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
366 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog
$testlog
372 # load in the helper functions
373 .
$TEST_DIR/functions
376 for t
in $TEST_CASES; do
378 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
379 echo "A failure detected. Stop test."
384 # Test on instance loop
385 INSTANCE
=" (instance) "
386 for t
in $TEST_CASES; do
387 test_on_instance
$t ||
continue
388 SAVED_TRACING_DIR
=$TRACING_DIR
389 export TRACING_DIR
=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
392 TRACING_DIR
=$SAVED_TRACING_DIR
393 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
394 echo "A failure detected. Stop test."
398 (cd $TRACING_DIR; initialize_ftrace
) # for cleanup
401 prlog
"# of passed: " `echo $PASSED_CASES | wc -w`
402 prlog
"# of failed: " `echo $FAILED_CASES | wc -w`
403 prlog
"# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
404 prlog
"# of untested: " `echo $UNTESTED_CASES | wc -w`
405 prlog
"# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
406 prlog
"# of xfailed: " `echo $XFAILED_CASES | wc -w`
407 prlog
"# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
409 # if no error, return 0