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