Add run_test.sh cmdline option for timeout

Tests implicitly run with a 3-minute timeout. Add an option for this
duration to be overridden. At the same time, fix a long-standing issue
of tests timing out under GDB by changing the default value to 0 if
invoked with -G.

Change-Id: I5ecf12d53bc9a9932c7a580893f0b6df93ca4ff4
diff --git a/kvm-unit-tests/run_test.sh b/kvm-unit-tests/run_test.sh
index b41481e..1e224f1 100755
--- a/kvm-unit-tests/run_test.sh
+++ b/kvm-unit-tests/run_test.sh
@@ -16,7 +16,6 @@
 
 source "$(dirname "${BASH_SOURCE[0]}")/../common.inc"
 
-default_var TIMEOUT		180s
 default_var VHE			0
 default_var VERBOSE		0
 default_var QUIET		0
@@ -26,6 +25,9 @@
 default_var TEST_PATH		""
 default_var DISPLAY_NAME	""
 default_var OUTPUT		""
+default_var TIMEOUT		""
+
+DEFAULT_TIMEOUT=180s
 
 # QEMU CPUs to use for VHE and nVHE runs
 CPU_VHE="max"
@@ -37,7 +39,7 @@
 	cat <<EOF
 
 Usage: $0 [-h] [-v] [-q] [-G] [-V]
-       [-k KERNEL] [-d DISPLAY_NAME] [-o OUTPUT]
+       [-k KERNEL] [-d DISPLAY_NAME] [-o OUTPUT] [-t TIMEOUT]
        PATH [-- RUN_QEMU_ARGS]
 
     PATH  Path to test binary. Basenames are resolved in the default output folder.
@@ -47,16 +49,18 @@
     -k    Kernel image
     -d    Override test name displayed in result
     -o    Redirect stdout/stderr output to given file (implies -q)
+    -t    kill test after given number of seconds
     -V    Enable VHE configuration
     -N    Disable protected KVM configuration
     -G    Enable debugging of emulated system with GDB
 EOF
 }
 
-while getopts ":k:d:o:vVNGqh" OPT; do
+while getopts ":k:d:o:t:vVNGqh" OPT; do
 	case "${OPT}" in
 	k)	KERNEL="${OPTARG}"		;;
 	d)	DISPLAY_NAME="${OPTARG}"	;;
+	t)	TIMEOUT="${OPTARG}"		;;
 	v)	VERBOSE=1			;;
 	V)	VHE=1				;;
 	N)	KVM_PROTECTED=0			;;
@@ -103,7 +107,7 @@
 	TEST_PATH="${KUT_TEST_DIR}/${TEST_PATH}"
 fi
 
-CMD=("${SCRIPT_RUN_QEMU}" -R "${TEST_PATH}" -t "${TIMEOUT}")
+CMD=("${SCRIPT_RUN_QEMU}" -R "${TEST_PATH}")
 
 if [ "${VHE}" -eq 1 ]; then
 	CMD+=(-c "${CPU_VHE}")
@@ -121,8 +125,18 @@
 
 if [ "${GDB}" -eq 1 ]; then
 	CMD+=(-G)
+	# Disable timeout unless overridden by user.
+	if [ -z "${TIMEOUT}" ]; then
+		TIMEOUT=0s
+	fi
 fi
 
+# If not otherwise specified, use default timeout value.
+if [ -z "${TIMEOUT}" ]; then
+	TIMEOUT="${DEFAULT_TIMEOUT}"
+fi
+CMD+=(-t "${TIMEOUT}")
+
 # If there are arguments after "--", pass them to the underlying run_qemu.sh.
 if [ $# -gt 0 ]; then
 	if [ $1 = "--" ]; then