Skip to content

Commit

Permalink
feat(scripts: debug): add a script to start a debug session
Browse files Browse the repository at this point in the history
Since switching to a higher-half kernel, placing the "target remote ..."
inside the gdbinit script seems to make GDB crash. Having it executed
as a CLI argument seems to be a working alternative.

This script lets us chose which symbol we want to set a breakpoint on,
instead of hardcoding it to kernel_main inside the gdbinit script.
  • Loading branch information
d4ilyrun committed Feb 19, 2024
1 parent 9086aa7 commit 978dc95
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
10 changes: 1 addition & 9 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
# Connect to qemu's gdbserver
target remote :1234

# Change layout
layout reg

# Continue execution until entering the kernel
break kernel_main
continue
layout src
39 changes: 39 additions & 0 deletions scripts/debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

# Start a GDB session connected to our kernel.
# You can also pass in symbols to set breakpoints on as arguments.
#
# example: ./debug.sh kernel_main mmu_init

# Absolute path to this script
SCRIPT=$(readlink -f "$0")
SCRIPTDIR=$(dirname "$SCRIPT")
GITROOT="$SCRIPTDIR/.."

if ! command -v gdb > /dev/null; then
echo "[ERROR] GDB not found in path" >&2
exit 127
fi

cd "$GITROOT" || exit

BREAKPOINTS=()
for symbol in "$@"; do
BREAKPOINTS+=("-ex" "break $symbol")
done

if [ ! -d "build" ]; then
echo "[INFO] build dir not found, setting up meson project"
meson setup build --cross-file "scripts/meson_cross.ini" --reconfigure -Dbuildtype=debug
fi

echo "[INFO] Starting a debugging session"
ninja -C build qemu-server || exit
gdb -x .gdbinit --symbol ./build/kernel/kernel.sym \
-iex "set pagination of" \
-iex "target remote localhost:1234" \
"${BREAKPOINTS[@]}" \
-ex "continue"

echo "[INFO] Terminating debugging session"
pkill qemu

0 comments on commit 978dc95

Please sign in to comment.