A practical setup for building, running, and source-level debugging the ARM64 Linux kernel with QEMU, GDB, and VS Code.
- scripts/mi2gdb: GDB translation script that intercepts VS Code Machine Interface (MI) requests and prints the equivalent GDB CLI commands in real time.
- Start docker Desktop & Open this repository folder in VS Code.
- When prompted, click "Reopen in Container" (or
Cmd+Shift+P➔Dev Containers: Reopen in Container).VS Code automatically creates the
linux-build-cacheandlinux-ccachenamed Docker volumes and mounts them at/buildand~/.cache/ccache. - Open the integrated terminal and run:
docker volume create linux-build-cache docker volume create linux-ccache ./scripts/build_kernel.sh ./scripts/setup_initramfs.sh ./scripts/run_qemu.sh
qemu-system-aarch64 \
-machine virt \
-cpu cortex-a57 \
-m 1024 \
-nographic \
-kernel /build/arch/arm64/boot/Image \
-initrd /build/initramfs.cpio.gz \
-append "console=ttyAMA0 nokaslr panic=0" \
-netdev user,id=net0 \
-device virtio-net-device,netdev=net0 \
-object filter-dump,id=dump0,netdev=net0,file=traffic.pcap \
-s -SThe -s flag opens a GDB server on TCP port 1234. The -S flag freezes the virtual CPU at boot so you can attach before kernel execution begins. The -object filter-dump flag automatically records all guest NIC network traffic to traffic.pcap in real time, which can be inspected with tcpdump -r traffic.pcap or Wireshark.
{
"version": "0.2.0",
"configurations": [
{
"name": "Kernel Debug",
"type": "cppdbg",
"request": "launch",
"program": "/build/vmlinux.unstripped",
"cwd": "${workspaceFolder}",
"miDebuggerServerAddress": "localhost:1234",
"miDebuggerPath": "gdb-multiarch",
"stopAtEntry": false,
"setupCommands": [
{ "text": "set architecture aarch64", "ignoreFailures": false },
{ "text": "set pagination off", "ignoreFailures": true },
{ "text": "directory ${workspaceFolder}/net/core:${workspaceFolder}/net/ipv4", "ignoreFailures": true }
]
}
]
}- On Linux,
miDebuggerPathis typicallygdb-multiarch. - On macOS, use
aarch64-linux-gnu-gdboraarch64-elf-gdb.
To see the exact CLI commands VS Code generates:
- In one terminal, run:
python3 scripts/mi2gdb --view
- In your
launch.json, pointmiDebuggerPathtoscripts/mi2gdb.
As you step, set breakpoints, and inspect variables in the VS Code GUI, mi2gdb displays the equivalent human-typed GDB commands in the terminal.
VS Code has a built-in "logging": { "engineLogging": true } setting in launch.json, but using it to learn GDB is overwhelming:
- Protocol Noise: VS Code generates hundreds of lines of internal Machine Interface (MI) plumbing (
-var-create,-symbol-list-lines,-stack-info-depth, variable tracking IDs, 300KB+ symbol tables) just to render UI panels. - Intended Audience:
engineLoggingis designed for C/C++ extension developers debugging the IDE wire protocol, not humans learning GDB CLI syntax. - What
mi2gdbSolves:mi2gdbfilters out 99% of internal IDE noise and translates only the meaningful user actions into clean, color-coded human GDB commands (break,next,step,continue,backtrace,print).
- Open
net/core/dev.cin VS Code and set a breakpoint insidenetif_receive_skb_core. - Press F5 (Continue). QEMU will finish booting and drop you into the BusyBox shell.
- In the QEMU console, send a test packet:
/ # ping -c 1 10.0.2.2 - VS Code will immediately hit the breakpoint on packet ingress. You can inspect variables like
skb, step through parsing logic, and view the call stack.
- Binary Roles: QEMU executes
Image(raw machine code), while GDB readsvmlinux.unstripped(ELF with DWARF debug info). Both contain the same compiled instructions. - KASLR: If
CONFIG_RANDOMIZE_BASEis left enabled, kernel code is placed at a randomized address at boot and breakpoints will silently fail to hit. Always disable it in your config and passnokaslron the command line. - Relative Symlinks: When creating the initramfs, applet symlinks to BusyBox must be relative (for example,
sh -> busybox). Absolute symlinks point to paths that do not exist inside the target root filesystem, resulting in anerror -2(ENOENT) during boot. - Console Output: ARM64 virt machines use the PL011 UART (
console=ttyAMA0). Settingconsole=ttyS0(which is standard for x86) produces a black screen with no boot messages. - QEMU User Networking IP Map: With
-netdev user(SLIRP), QEMU sets up a private10.0.2.0/24virtual subnet.10.0.2.2is the virtual host gateway,10.0.2.3is the virtual DNS server, and10.0.2.15is the fixed address assigned to the first guest machine. Statically assigning10.0.2.15in/initgives immediate zero-delay networking without waiting for a DHCP lease.