selftests/bpf: Add an option for a debug shell in vmtest.sh

The newly introduced -s command line option starts an interactive shell.
If a command is specified, the shell is started after the command
finishes executing. It's useful to have a shell especially when
debugging failing tests or developing new tests.

Since the user may terminate the VM forcefully, an extra "sync" is added
after the execution of the command to persist any logs from the command
into the log file.

Signed-off-by: KP Singh <kpsingh@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210323014752.3198283-1-kpsingh@kernel.org
This commit is contained in:
KP Singh 2021-03-23 01:47:52 +00:00 committed by Andrii Nakryiko
parent 235fc0e36d
commit 63f8af0fc3

View file

@ -24,15 +24,15 @@ EXIT_STATUS_FILE="${LOG_FILE_BASE}.exit_status"
usage() usage()
{ {
cat <<EOF cat <<EOF
Usage: $0 [-i] [-d <output_dir>] -- [<command>] Usage: $0 [-i] [-s] [-d <output_dir>] -- [<command>]
<command> is the command you would normally run when you are in <command> is the command you would normally run when you are in
tools/testing/selftests/bpf. e.g: tools/testing/selftests/bpf. e.g:
$0 -- ./test_progs -t test_lsm $0 -- ./test_progs -t test_lsm
If no command is specified, "${DEFAULT_COMMAND}" will be run by If no command is specified and a debug shell (-s) is not requested,
default. "${DEFAULT_COMMAND}" will be run by default.
If you build your kernel using KBUILD_OUTPUT= or O= options, these If you build your kernel using KBUILD_OUTPUT= or O= options, these
can be passed as environment variables to the script: can be passed as environment variables to the script:
@ -49,6 +49,9 @@ Options:
-d) Update the output directory (default: ${OUTPUT_DIR}) -d) Update the output directory (default: ${OUTPUT_DIR})
-j) Number of jobs for compilation, similar to -j in make -j) Number of jobs for compilation, similar to -j in make
(default: ${NUM_COMPILE_JOBS}) (default: ${NUM_COMPILE_JOBS})
-s) Instead of powering off the VM, start an interactive
shell. If <command> is specified, the shell runs after
the command finishes executing
EOF EOF
} }
@ -149,6 +152,7 @@ update_init_script()
local init_script_dir="${OUTPUT_DIR}/${MOUNT_DIR}/etc/rcS.d" local init_script_dir="${OUTPUT_DIR}/${MOUNT_DIR}/etc/rcS.d"
local init_script="${init_script_dir}/S50-startup" local init_script="${init_script_dir}/S50-startup"
local command="$1" local command="$1"
local exit_command="$2"
mount_image mount_image
@ -162,9 +166,10 @@ EOF
fi fi
sudo bash -c "cat >${init_script}" <<EOF sudo bash -c "echo '#!/bin/bash' > ${init_script}"
#!/bin/bash
if [[ "${command}" != "" ]]; then
sudo bash -c "cat >>${init_script}" <<EOF
# Have a default value in the exit status file # Have a default value in the exit status file
# incase the VM is forcefully stopped. # incase the VM is forcefully stopped.
echo "130" > "/root/${EXIT_STATUS_FILE}" echo "130" > "/root/${EXIT_STATUS_FILE}"
@ -175,9 +180,12 @@ echo "130" > "/root/${EXIT_STATUS_FILE}"
stdbuf -oL -eL ${command} stdbuf -oL -eL ${command}
echo "\$?" > "/root/${EXIT_STATUS_FILE}" echo "\$?" > "/root/${EXIT_STATUS_FILE}"
} 2>&1 | tee "/root/${LOG_FILE}" } 2>&1 | tee "/root/${LOG_FILE}"
poweroff -f # Ensure that the logs are written to disk
sync
EOF EOF
fi
sudo bash -c "echo ${exit_command} >> ${init_script}"
sudo chmod a+x "${init_script}" sudo chmod a+x "${init_script}"
unmount_image unmount_image
} }
@ -277,8 +285,10 @@ main()
local kernel_bzimage="${kernel_checkout}/${X86_BZIMAGE}" local kernel_bzimage="${kernel_checkout}/${X86_BZIMAGE}"
local command="${DEFAULT_COMMAND}" local command="${DEFAULT_COMMAND}"
local update_image="no" local update_image="no"
local exit_command="poweroff -f"
local debug_shell="no"
while getopts 'hkid:j:' opt; do while getopts 'hskid:j:' opt; do
case ${opt} in case ${opt} in
i) i)
update_image="yes" update_image="yes"
@ -289,6 +299,11 @@ main()
j) j)
NUM_COMPILE_JOBS="$OPTARG" NUM_COMPILE_JOBS="$OPTARG"
;; ;;
s)
command=""
debug_shell="yes"
exit_command="bash"
;;
h) h)
usage usage
exit 0 exit 0
@ -307,7 +322,7 @@ main()
done done
shift $((OPTIND -1)) shift $((OPTIND -1))
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then
echo "No command specified, will run ${DEFAULT_COMMAND} in the vm" echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
else else
command="$@" command="$@"
@ -355,10 +370,12 @@ main()
fi fi
update_selftests "${kernel_checkout}" "${make_command}" update_selftests "${kernel_checkout}" "${make_command}"
update_init_script "${command}" update_init_script "${command}" "${exit_command}"
run_vm "${kernel_bzimage}" run_vm "${kernel_bzimage}"
copy_logs if [[ "${command}" != "" ]]; then
echo "Logs saved in ${OUTPUT_DIR}/${LOG_FILE}" copy_logs
echo "Logs saved in ${OUTPUT_DIR}/${LOG_FILE}"
fi
} }
catch() catch()