Skip to content

Instantly share code, notes, and snippets.

@junftnt
Forked from borrrden/instructions.sh
Created November 13, 2023 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junftnt/68a29566979bea20bd2b9b665818f351 to your computer and use it in GitHub Desktop.
Save junftnt/68a29566979bea20bd2b9b665818f351 to your computer and use it in GitHub Desktop.
Debugging ponchoOS kernel
# INSIDE WSL, install gdb (one-time instruction)
sudo apt install gdb
# Add the following to your kernel Makefile CFLAGS, so that gcc will generate
# debug symbols that the debugger can use
# Note: The thing being added is -g, so as of lesson 12 it should look like this.
# Delete all the contents of the lib folder to force a recompile
CFLAGS = -ffreestanding -fshort-wchar -g
# Add the following to your qemu invocation and start QEmu:
# -s (Starts a gdb debug server on port 1234)
# -S (Pauses the virtual machine after power on to wait for a debugger connection)
qemu-system-x86_64 -drive file=%BUILDDIR%/%OSNAME%.img -m 256M -cpu qemu64 \
-drive if=pflash,format=raw,unit=0,file=%OVMFDIR%/OVMF_CODE-pure-efi.fd,readonly=on \
-drive if=pflash,format=raw,unit=1,file=%OVMFDIR%/OVMF_VARS-pure-efi.fd -net none -s -S
# Start gdb using your built kernel file, this will read the kernel
# information into gdb. If your kernel name is different, then replace it,
# it's NOT the image, but the kernel binary
gdb bin/kernel.elf
# Your shell will look different now, as you are inside the gdb command prompt.
# Connect it to the kernel running in QEmu
target remote :1234
# You will see some output similar to '0x000000000000fff0 in ?? ()'.
# This is correct, it means that the machine is halted almost immediately after power on
# and is waiting for the signal to continue. First, add a hardware assisted breakpoint
# at your kernel start function
hbreak _start
# You should see output like
# 'Hardware assisted breakpoint 1 at 0x134: file src/kernel.cpp, line 3.'
# Now you can ask the program to continue, and it will stop at your start method
continue # or 'c' for short
# 'Continuing' is written, and qemu will continue into the uefi shell and eventually
# into your kernel and then pause again displaying something like
# 'Breakpoint 1, _start (bootInfo=0xff14f28) at src/kernel.cpp:3
# 3 extern "C" void _start(BootInfo* bootInfo){
# Common debugging commands include "step" (which will go forward one line and enter
# any function on that line, similar to step into in most IDE), "next"
# (analogous to "step over") and "finish" (step out). These can be abbreviated as "s", "n",
# and "fin". You can print out varibles using the "print" command (p for short):
p bootInfo
# $1 = (BootInfo *) 0xff14f28
# Tab autocomplete is available, as well as command history via the up key. Happy debugging!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment