1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4# ftracetest - Ftrace test shell scripts
5#
6# Copyright (C) Hitachi Ltd., 2014
7#  Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8#
9
10usage() { # errno [message]
11[ ! -z "$2" ] && echo $2
12echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
13echo " Options:"
14echo "		-h|--help  Show help message"
15echo "		-k|--keep  Keep passed test logs"
16echo "		-K|--ktap  Output in KTAP format"
17echo "		-v|--verbose Increase verbosity of test messages"
18echo "		-vv        Alias of -v -v (Show all results in stdout)"
19echo "		-vvv       Alias of -v -v -v (Show all commands immediately)"
20echo "		--fail-unsupported Treat UNSUPPORTED as a failure"
21echo "		--fail-unresolved Treat UNRESOLVED as a failure"
22echo "		-d|--debug Debug mode (trace all shell commands)"
23echo "		-l|--logdir <dir> Save logs on the <dir>"
24echo "		            If <dir> is -, all logs output in console only"
25exit $1
26}
27
28# default error
29err_ret=1
30
31# kselftest skip code is 4
32err_skip=4
33
34# cgroup RT scheduling prevents chrt commands from succeeding, which
35# induces failures in test wakeup tests.  Disable for the duration of
36# the tests.
37
38readonly sched_rt_runtime=/proc/sys/kernel/sched_rt_runtime_us
39
40sched_rt_runtime_orig=$(cat $sched_rt_runtime)
41
42setup() {
43  echo -1 > $sched_rt_runtime
44}
45
46cleanup() {
47  echo $sched_rt_runtime_orig > $sched_rt_runtime
48}
49
50errexit() { # message
51  echo "Error: $1" 1>&2
52  cleanup
53  exit $err_ret
54}
55
56# Ensuring user privilege
57if [ `id -u` -ne 0 ]; then
58  errexit "this must be run by root user"
59fi
60
61setup
62
63# Utilities
64absdir() { # file_path
65  (cd `dirname $1`; pwd)
66}
67
68abspath() {
69  echo `absdir $1`/`basename $1`
70}
71
72find_testcases() { #directory
73  echo `find $1 -name \*.tc | sort`
74}
75
76parse_opts() { # opts
77  local OPT_TEST_CASES=
78  local OPT_TEST_DIR=
79
80  while [ ! -z "$1" ]; do
81    case "$1" in
82    --help|-h)
83      usage 0
84    ;;
85    --keep|-k)
86      KEEP_LOG=1
87      shift 1
88    ;;
89    --ktap|-K)
90      KTAP=1
91      shift 1
92    ;;
93    --verbose|-v|-vv|-vvv)
94      if [ $VERBOSE -eq -1 ]; then
95	usage "--console can not use with --verbose"
96      fi
97      VERBOSE=$((VERBOSE + 1))
98      [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
99      [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
100      shift 1
101    ;;
102    --console)
103      if [ $VERBOSE -ne 0 ]; then
104	usage "--console can not use with --verbose"
105      fi
106      VERBOSE=-1
107      shift 1
108    ;;
109    --debug|-d)
110      DEBUG=1
111      shift 1
112    ;;
113    --stop-fail)
114      STOP_FAILURE=1
115      shift 1
116    ;;
117    --fail-unsupported)
118      UNSUPPORTED_RESULT=1
119      shift 1
120    ;;
121    --fail-unresolved)
122      UNRESOLVED_RESULT=1
123      shift 1
124    ;;
125    --logdir|-l)
126      LOG_DIR=$2
127      LINK_PTR=
128      shift 2
129    ;;
130    *.tc)
131      if [ -f "$1" ]; then
132        OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
133        shift 1
134      else
135        usage 1 "$1 is not a testcase"
136      fi
137      ;;
138    *)
139      if [ -d "$1" ]; then
140        OPT_TEST_DIR=`abspath $1`
141        OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
142        shift 1
143      else
144        usage 1 "Invalid option ($1)"
145      fi
146    ;;
147    esac
148  done
149  if [ ! -z "$OPT_TEST_CASES" ]; then
150    TEST_CASES=$OPT_TEST_CASES
151  fi
152}
153
154# Parameters
155TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
156if [ -z "$TRACING_DIR" ]; then
157    DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
158    if [ -z "$DEBUGFS_DIR" ]; then
159	# If tracefs exists, then so does /sys/kernel/tracing
160	if [ -d "/sys/kernel/tracing" ]; then
161	    mount -t tracefs nodev /sys/kernel/tracing ||
162	      errexit "Failed to mount /sys/kernel/tracing"
163	    TRACING_DIR="/sys/kernel/tracing"
164	# If debugfs exists, then so does /sys/kernel/debug
165	elif [ -d "/sys/kernel/debug" ]; then
166	    mount -t debugfs nodev /sys/kernel/debug ||
167	      errexit "Failed to mount /sys/kernel/debug"
168	    TRACING_DIR="/sys/kernel/debug/tracing"
169	else
170	    err_ret=$err_skip
171	    errexit "debugfs and tracefs are not configured in this kernel"
172	fi
173    else
174	TRACING_DIR="$DEBUGFS_DIR/tracing"
175    fi
176fi
177if [ ! -d "$TRACING_DIR" ]; then
178    err_ret=$err_skip
179    errexit "ftrace is not configured in this kernel"
180fi
181
182TOP_DIR=`absdir $0`
183TEST_DIR=$TOP_DIR/test.d
184TEST_CASES=`find_testcases $TEST_DIR`
185LOG_TOP_DIR=$TOP_DIR/logs
186LOG_DATE=`date +%Y%m%d-%H%M%S`
187LOG_DIR=$LOG_TOP_DIR/$LOG_DATE/
188LINK_PTR=$LOG_TOP_DIR/latest
189KEEP_LOG=0
190KTAP=0
191DEBUG=0
192VERBOSE=0
193UNSUPPORTED_RESULT=0
194UNRESOLVED_RESULT=0
195STOP_FAILURE=0
196# Parse command-line options
197parse_opts $*
198
199[ $DEBUG -ne 0 ] && set -x
200
201# Verify parameters
202if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
203  errexit "No ftrace directory found"
204fi
205
206# Preparing logs
207if [ "x$LOG_DIR" = "x-" ]; then
208  LOG_FILE=
209  date
210else
211  LOG_FILE=$LOG_DIR/ftracetest.log
212  mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
213  date > $LOG_FILE
214  if [ "x-$LINK_PTR" != "x-" ]; then
215    unlink $LINK_PTR
216    ln -fs $LOG_DATE $LINK_PTR
217  fi
218fi
219
220# Define text colors
221# Check available colors on the terminal, if any
222ncolors=`tput colors 2>/dev/null || echo 0`
223color_reset=
224color_red=
225color_green=
226color_blue=
227# If stdout exists and number of colors is eight or more, use them
228if [ -t 1 -a "$ncolors" -ge 8 ]; then
229  color_reset="\033[0m"
230  color_red="\033[31m"
231  color_green="\033[32m"
232  color_blue="\033[34m"
233fi
234
235strip_esc() {
236  # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead.
237  sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
238}
239
240prlog() { # messages
241  newline="\n"
242  if [ "$1" = "-n" ] ; then
243    newline=
244    shift
245  fi
246  [ "$KTAP" != "1" ] && printf "$*$newline"
247  [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE
248}
249catlog() { #file
250  cat $1
251  [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
252}
253prlog "=== Ftrace unit tests ==="
254
255
256# Testcase management
257# Test result codes - Dejagnu extended code
258PASS=0	# The test succeeded.
259FAIL=1	# The test failed, but was expected to succeed.
260UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
261UNTESTED=3    # The test was not run, currently just a placeholder.
262UNSUPPORTED=4 # The test failed because of lack of feature.
263XFAIL=5	# The test failed, and was expected to fail.
264
265# Accumulations
266PASSED_CASES=
267FAILED_CASES=
268UNRESOLVED_CASES=
269UNTESTED_CASES=
270UNSUPPORTED_CASES=
271XFAILED_CASES=
272UNDEFINED_CASES=
273TOTAL_RESULT=0
274
275INSTANCE=
276CASENO=0
277CASENAME=
278
279testcase() { # testfile
280  CASENO=$((CASENO+1))
281  CASENAME=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
282}
283
284checkreq() { # testfile
285  requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
286  # Use eval to pass quoted-patterns correctly.
287  eval check_requires "$requires"
288}
289
290test_on_instance() { # testfile
291  grep -q "^#[ \t]*flags:.*instance" $1
292}
293
294ktaptest() { # result comment
295  if [ "$KTAP" != "1" ]; then
296    return
297  fi
298
299  local result=
300  if [ "$1" = "1" ]; then
301    result="ok"
302  else
303    result="not ok"
304  fi
305  shift
306
307  local comment=$*
308  if [ "$comment" != "" ]; then
309    comment="# $comment"
310  fi
311
312  echo $result $CASENO $INSTANCE$CASENAME $comment
313}
314
315eval_result() { # sigval
316  case $1 in
317    $PASS)
318      prlog "	[${color_green}PASS${color_reset}]"
319      ktaptest 1
320      PASSED_CASES="$PASSED_CASES $CASENO"
321      return 0
322    ;;
323    $FAIL)
324      prlog "	[${color_red}FAIL${color_reset}]"
325      ktaptest 0
326      FAILED_CASES="$FAILED_CASES $CASENO"
327      return 1 # this is a bug.
328    ;;
329    $UNRESOLVED)
330      prlog "	[${color_blue}UNRESOLVED${color_reset}]"
331      ktaptest 0 UNRESOLVED
332      UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
333      return $UNRESOLVED_RESULT # depends on use case
334    ;;
335    $UNTESTED)
336      prlog "	[${color_blue}UNTESTED${color_reset}]"
337      ktaptest 1 SKIP
338      UNTESTED_CASES="$UNTESTED_CASES $CASENO"
339      return 0
340    ;;
341    $UNSUPPORTED)
342      prlog "	[${color_blue}UNSUPPORTED${color_reset}]"
343      ktaptest 1 SKIP
344      UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
345      return $UNSUPPORTED_RESULT # depends on use case
346    ;;
347    $XFAIL)
348      prlog "	[${color_green}XFAIL${color_reset}]"
349      ktaptest 1 XFAIL
350      XFAILED_CASES="$XFAILED_CASES $CASENO"
351      return 0
352    ;;
353    *)
354      prlog "	[${color_blue}UNDEFINED${color_reset}]"
355      ktaptest 0 error
356      UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
357      return 1 # this must be a test bug
358    ;;
359  esac
360}
361
362# Signal handling for result codes
363SIG_RESULT=
364SIG_BASE=36	# Use realtime signals
365SIG_PID=$$
366
367exit_pass () {
368  exit 0
369}
370
371SIG_FAIL=$((SIG_BASE + FAIL))
372exit_fail () {
373  exit 1
374}
375trap 'SIG_RESULT=$FAIL' $SIG_FAIL
376
377SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
378exit_unresolved () {
379  kill -s $SIG_UNRESOLVED $SIG_PID
380  exit 0
381}
382trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
383
384SIG_UNTESTED=$((SIG_BASE + UNTESTED))
385exit_untested () {
386  kill -s $SIG_UNTESTED $SIG_PID
387  exit 0
388}
389trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
390
391SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
392exit_unsupported () {
393  kill -s $SIG_UNSUPPORTED $SIG_PID
394  exit 0
395}
396trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
397
398SIG_XFAIL=$((SIG_BASE + XFAIL))
399exit_xfail () {
400  kill -s $SIG_XFAIL $SIG_PID
401  exit 0
402}
403trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
404
405__run_test() { # testfile
406  # setup PID and PPID, $$ is not updated.
407  (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x;
408   checkreq $1; initialize_ftrace; . $1)
409  [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
410}
411
412# Run one test case
413run_test() { # testfile
414  local testname=`basename $1`
415  testcase $1
416  prlog -n "[$CASENO]$INSTANCE$CASENAME"
417  if [ ! -z "$LOG_FILE" ] ; then
418    local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
419  else
420    local testlog=/proc/self/fd/1
421  fi
422  export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
423  export FTRACETEST_ROOT=$TOP_DIR
424  echo "execute$INSTANCE: "$1 > $testlog
425  SIG_RESULT=0
426  if [ $VERBOSE -eq -1 ]; then
427    __run_test $1
428  elif [ -z "$LOG_FILE" ]; then
429    __run_test $1 2>&1
430  elif [ $VERBOSE -ge 3 ]; then
431    __run_test $1 | tee -a $testlog 2>&1
432  elif [ $VERBOSE -eq 2 ]; then
433    __run_test $1 2>> $testlog | tee -a $testlog
434  else
435    __run_test $1 >> $testlog 2>&1
436  fi
437  eval_result $SIG_RESULT
438  if [ $? -eq 0 ]; then
439    # Remove test log if the test was done as it was expected.
440    [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
441  else
442    [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
443    TOTAL_RESULT=1
444  fi
445  rm -rf $TMPDIR
446}
447
448# load in the helper functions
449. $TEST_DIR/functions
450
451if [ "$KTAP" = "1" ]; then
452  echo "TAP version 13"
453
454  casecount=`echo $TEST_CASES | wc -w`
455  for t in $TEST_CASES; do
456    test_on_instance $t || continue
457    casecount=$((casecount+1))
458  done
459  echo "1..${casecount}"
460fi
461
462# Main loop
463for t in $TEST_CASES; do
464  run_test $t
465  if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
466    echo "A failure detected. Stop test."
467    exit 1
468  fi
469done
470
471# Test on instance loop
472INSTANCE=" (instance) "
473for t in $TEST_CASES; do
474  test_on_instance $t || continue
475  SAVED_TRACING_DIR=$TRACING_DIR
476  export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
477  run_test $t
478  rmdir $TRACING_DIR
479  TRACING_DIR=$SAVED_TRACING_DIR
480  if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
481    echo "A failure detected. Stop test."
482    exit 1
483  fi
484done
485(cd $TRACING_DIR; finish_ftrace) # for cleanup
486
487prlog ""
488prlog "# of passed: " `echo $PASSED_CASES | wc -w`
489prlog "# of failed: " `echo $FAILED_CASES | wc -w`
490prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
491prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
492prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
493prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
494prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
495
496if [ "$KTAP" = "1" ]; then
497  echo -n "# Totals:"
498  echo -n " pass:"`echo $PASSED_CASES | wc -w`
499  echo -n " faii:"`echo $FAILED_CASES | wc -w`
500  echo -n " xfail:"`echo $XFAILED_CASES | wc -w`
501  echo -n " xpass:0"
502  echo -n " skip:"`echo $UNTESTED_CASES $UNSUPPORTED_CASES | wc -w`
503  echo -n " error:"`echo $UNRESOLVED_CASES $UNDEFINED_CASES | wc -w`
504  echo
505fi
506
507cleanup
508
509# if no error, return 0
510exit $TOTAL_RESULT
511