GNU Linux-libre 4.14.257-gnu1
[releases.git] / tools / testing / selftests / ftrace / ftracetest
1 #!/bin/sh
2
3 # ftracetest - Ftrace test shell scripts
4 #
5 # Copyright (C) Hitachi Ltd., 2014
6 #  Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7 #
8 # Released under the terms of the GPL v2.
9
10 usage() { # errno [message]
11 [ ! -z "$2" ] && echo $2
12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
13 echo " Options:"
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"
23 exit $1
24 }
25
26 errexit() { # message
27   echo "Error: $1" 1>&2
28   exit 1
29 }
30
31 # Ensuring user privilege
32 if [ `id -u` -ne 0 ]; then
33   errexit "this must be run by root user"
34 fi
35
36 # Utilities
37 absdir() { # file_path
38   (cd `dirname $1`; pwd)
39 }
40
41 abspath() {
42   echo `absdir $1`/`basename $1`
43 }
44
45 find_testcases() { #directory
46   echo `find $1 -name \*.tc | sort`
47 }
48
49 parse_opts() { # opts
50   local OPT_TEST_CASES=
51   local OPT_TEST_DIR=
52
53   while [ ! -z "$1" ]; do
54     case "$1" in
55     --help|-h)
56       usage 0
57     ;;
58     --keep|-k)
59       KEEP_LOG=1
60       shift 1
61     ;;
62     --verbose|-v|-vv|-vvv)
63       VERBOSE=$((VERBOSE + 1))
64       [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
65       [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
66       shift 1
67     ;;
68     --debug|-d)
69       DEBUG=1
70       shift 1
71     ;;
72     --fail-unsupported)
73       UNSUPPORTED_RESULT=1
74       shift 1
75     ;;
76     --logdir|-l)
77       LOG_DIR=$2
78       shift 2
79     ;;
80     *.tc)
81       if [ -f "$1" ]; then
82         OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
83         shift 1
84       else
85         usage 1 "$1 is not a testcase"
86       fi
87       ;;
88     *)
89       if [ -d "$1" ]; then
90         OPT_TEST_DIR=`abspath $1`
91         OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
92         shift 1
93       else
94         usage 1 "Invalid option ($1)"
95       fi
96     ;;
97     esac
98   done
99   if [ ! -z "$OPT_TEST_CASES" ]; then
100     TEST_CASES=$OPT_TEST_CASES
101   fi
102 }
103
104 # Parameters
105 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
106 if [ -z "$DEBUGFS_DIR" ]; then
107     TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
108 else
109     TRACING_DIR=$DEBUGFS_DIR/tracing
110 fi
111
112 TOP_DIR=`absdir $0`
113 TEST_DIR=$TOP_DIR/test.d
114 TEST_CASES=`find_testcases $TEST_DIR`
115 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
116 KEEP_LOG=0
117 DEBUG=0
118 VERBOSE=0
119 UNSUPPORTED_RESULT=0
120 # Parse command-line options
121 parse_opts $*
122
123 [ $DEBUG -ne 0 ] && set -x
124
125 # Verify parameters
126 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
127   errexit "No ftrace directory found"
128 fi
129
130 # Preparing logs
131 if [ "x$LOG_DIR" = "x-" ]; then
132   LOG_FILE=
133   date
134 else
135   LOG_FILE=$LOG_DIR/ftracetest.log
136   mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
137   date > $LOG_FILE
138 fi
139
140 prlog() { # messages
141   [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE
142 }
143 catlog() { #file
144   [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE
145 }
146 prlog "=== Ftrace unit tests ==="
147
148
149 # Testcase management
150 # Test result codes - Dejagnu extended code
151 PASS=0  # The test succeeded.
152 FAIL=1  # The test failed, but was expected to succeed.
153 UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
154 UNTESTED=3    # The test was not run, currently just a placeholder.
155 UNSUPPORTED=4 # The test failed because of lack of feature.
156 XFAIL=5 # The test failed, and was expected to fail.
157
158 # Accumulations
159 PASSED_CASES=
160 FAILED_CASES=
161 UNRESOLVED_CASES=
162 UNTESTED_CASES=
163 UNSUPPORTED_CASES=
164 XFAILED_CASES=
165 UNDEFINED_CASES=
166 TOTAL_RESULT=0
167
168 INSTANCE=
169 CASENO=0
170 testcase() { # testfile
171   CASENO=$((CASENO+1))
172   desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
173   prlog -n "[$CASENO]$INSTANCE$desc"
174 }
175
176 test_on_instance() { # testfile
177   grep -q "^#[ \t]*flags:.*instance" $1
178 }
179
180 eval_result() { # sigval
181   case $1 in
182     $PASS)
183       prlog "   [PASS]"
184       PASSED_CASES="$PASSED_CASES $CASENO"
185       return 0
186     ;;
187     $FAIL)
188       prlog "   [FAIL]"
189       FAILED_CASES="$FAILED_CASES $CASENO"
190       return 1 # this is a bug.
191     ;;
192     $UNRESOLVED)
193       prlog "   [UNRESOLVED]"
194       UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
195       return 1 # this is a kind of bug.. something happened.
196     ;;
197     $UNTESTED)
198       prlog "   [UNTESTED]"
199       UNTESTED_CASES="$UNTESTED_CASES $CASENO"
200       return 0
201     ;;
202     $UNSUPPORTED)
203       prlog "   [UNSUPPORTED]"
204       UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
205       return $UNSUPPORTED_RESULT # depends on use case
206     ;;
207     $XFAIL)
208       prlog "   [XFAIL]"
209       XFAILED_CASES="$XFAILED_CASES $CASENO"
210       return 0
211     ;;
212     *)
213       prlog "   [UNDEFINED]"
214       UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
215       return 1 # this must be a test bug
216     ;;
217   esac
218 }
219
220 # Signal handling for result codes
221 SIG_RESULT=
222 SIG_BASE=36     # Use realtime signals
223 SIG_PID=$$
224
225 SIG_FAIL=$((SIG_BASE + FAIL))
226 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
227
228 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
229 exit_unresolved () {
230   kill -s $SIG_UNRESOLVED $SIG_PID
231   exit 0
232 }
233 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
234
235 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
236 exit_untested () {
237   kill -s $SIG_UNTESTED $SIG_PID
238   exit 0
239 }
240 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
241
242 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
243 exit_unsupported () {
244   kill -s $SIG_UNSUPPORTED $SIG_PID
245   exit 0
246 }
247 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
248
249 SIG_XFAIL=$((SIG_BASE + XFAIL))
250 exit_xfail () {
251   kill -s $SIG_XFAIL $SIG_PID
252   exit 0
253 }
254 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
255
256 __run_test() { # testfile
257   # setup PID and PPID, $$ is not updated.
258   (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
259   [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
260 }
261
262 # Run one test case
263 run_test() { # testfile
264   local testname=`basename $1`
265   if [ ! -z "$LOG_FILE" ] ; then
266     local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
267   else
268     local testlog=/proc/self/fd/1
269   fi
270   export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
271   testcase $1
272   echo "execute$INSTANCE: "$1 > $testlog
273   SIG_RESULT=0
274   if [ -z "$LOG_FILE" ]; then
275     __run_test $1 2>&1
276   elif [ $VERBOSE -ge 3 ]; then
277     __run_test $1 | tee -a $testlog 2>&1
278   elif [ $VERBOSE -eq 2 ]; then
279     __run_test $1 2>> $testlog | tee -a $testlog
280   else
281     __run_test $1 >> $testlog 2>&1
282   fi
283   eval_result $SIG_RESULT
284   if [ $? -eq 0 ]; then
285     # Remove test log if the test was done as it was expected.
286     [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
287   else
288     [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
289     TOTAL_RESULT=1
290   fi
291   rm -rf $TMPDIR
292 }
293
294 # load in the helper functions
295 . $TEST_DIR/functions
296
297 # Main loop
298 for t in $TEST_CASES; do
299   run_test $t
300 done
301
302 # Test on instance loop
303 INSTANCE=" (instance) "
304 for t in $TEST_CASES; do
305   test_on_instance $t || continue
306   SAVED_TRACING_DIR=$TRACING_DIR
307   export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
308   run_test $t
309   rmdir $TRACING_DIR
310   TRACING_DIR=$SAVED_TRACING_DIR
311 done
312
313 prlog ""
314 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
315 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
316 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
317 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
318 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
319 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
320 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
321
322 # if no error, return 0
323 exit $TOTAL_RESULT