Skip to content

Commit 63f8af0

Browse files
sinkapanakryiko
authored andcommitted
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 235fc0e commit 63f8af0

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

tools/testing/selftests/bpf/vmtest.sh

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ EXIT_STATUS_FILE="${LOG_FILE_BASE}.exit_status"
2424
usage()
2525
{
2626
cat <<EOF
27-
Usage: $0 [-i] [-d <output_dir>] -- [<command>]
27+
Usage: $0 [-i] [-s] [-d <output_dir>] -- [<command>]
2828
2929
<command> is the command you would normally run when you are in
3030
tools/testing/selftests/bpf. e.g:
3131
3232
$0 -- ./test_progs -t test_lsm
3333
34-
If no command is specified, "${DEFAULT_COMMAND}" will be run by
35-
default.
34+
If no command is specified and a debug shell (-s) is not requested,
35+
"${DEFAULT_COMMAND}" will be run by default.
3636
3737
If you build your kernel using KBUILD_OUTPUT= or O= options, these
3838
can be passed as environment variables to the script:
@@ -49,6 +49,9 @@ Options:
4949
-d) Update the output directory (default: ${OUTPUT_DIR})
5050
-j) Number of jobs for compilation, similar to -j in make
5151
(default: ${NUM_COMPILE_JOBS})
52+
-s) Instead of powering off the VM, start an interactive
53+
shell. If <command> is specified, the shell runs after
54+
the command finishes executing
5255
EOF
5356
}
5457

@@ -149,6 +152,7 @@ update_init_script()
149152
local init_script_dir="${OUTPUT_DIR}/${MOUNT_DIR}/etc/rcS.d"
150153
local init_script="${init_script_dir}/S50-startup"
151154
local command="$1"
155+
local exit_command="$2"
152156

153157
mount_image
154158

@@ -162,9 +166,10 @@ EOF
162166

163167
fi
164168

165-
sudo bash -c "cat >${init_script}" <<EOF
166-
#!/bin/bash
169+
sudo bash -c "echo '#!/bin/bash' > ${init_script}"
167170

171+
if [[ "${command}" != "" ]]; then
172+
sudo bash -c "cat >>${init_script}" <<EOF
168173
# Have a default value in the exit status file
169174
# incase the VM is forcefully stopped.
170175
echo "130" > "/root/${EXIT_STATUS_FILE}"
@@ -175,9 +180,12 @@ echo "130" > "/root/${EXIT_STATUS_FILE}"
175180
stdbuf -oL -eL ${command}
176181
echo "\$?" > "/root/${EXIT_STATUS_FILE}"
177182
} 2>&1 | tee "/root/${LOG_FILE}"
178-
poweroff -f
183+
# Ensure that the logs are written to disk
184+
sync
179185
EOF
186+
fi
180187

188+
sudo bash -c "echo ${exit_command} >> ${init_script}"
181189
sudo chmod a+x "${init_script}"
182190
unmount_image
183191
}
@@ -277,8 +285,10 @@ main()
277285
local kernel_bzimage="${kernel_checkout}/${X86_BZIMAGE}"
278286
local command="${DEFAULT_COMMAND}"
279287
local update_image="no"
288+
local exit_command="poweroff -f"
289+
local debug_shell="no"
280290

281-
while getopts 'hkid:j:' opt; do
291+
while getopts 'hskid:j:' opt; do
282292
case ${opt} in
283293
i)
284294
update_image="yes"
@@ -289,6 +299,11 @@ main()
289299
j)
290300
NUM_COMPILE_JOBS="$OPTARG"
291301
;;
302+
s)
303+
command=""
304+
debug_shell="yes"
305+
exit_command="bash"
306+
;;
292307
h)
293308
usage
294309
exit 0
@@ -307,7 +322,7 @@ main()
307322
done
308323
shift $((OPTIND -1))
309324

310-
if [[ $# -eq 0 ]]; then
325+
if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then
311326
echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
312327
else
313328
command="$@"
@@ -355,10 +370,12 @@ main()
355370
fi
356371

357372
update_selftests "${kernel_checkout}" "${make_command}"
358-
update_init_script "${command}"
373+
update_init_script "${command}" "${exit_command}"
359374
run_vm "${kernel_bzimage}"
360-
copy_logs
361-
echo "Logs saved in ${OUTPUT_DIR}/${LOG_FILE}"
375+
if [[ "${command}" != "" ]]; then
376+
copy_logs
377+
echo "Logs saved in ${OUTPUT_DIR}/${LOG_FILE}"
378+
fi
362379
}
363380

364381
catch()

0 commit comments

Comments
 (0)