Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

Linux Kernel Development and Debugging Environment (ARM64)

A practical setup for building, running, and source-level debugging the ARM64 Linux kernel with QEMU, GDB, and VS Code.


Documents and Tools

  • scripts/mi2gdb: GDB translation script that intercepts VS Code Machine Interface (MI) requests and prints the equivalent GDB CLI commands in real time.

Quick Start

VS Code Dev Container

  1. Start docker Desktop & Open this repository folder in VS Code.
  2. When prompted, click "Reopen in Container" (or Cmd+Shift+P ➔ Dev Containers: Reopen in Container).

    VS Code automatically creates the linux-build-cache and linux-ccache named Docker volumes and mounts them at /build and ~/.cache/ccache.

  3. 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

Launch QEMU with GDB Stub Enabled (also launch through ./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 -S

The -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.


4. Attach the Debugger

Option A: VS Code (.vscode/launch.json)

{
  "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, miDebuggerPath is typically gdb-multiarch.
  • On macOS, use aarch64-linux-gnu-gdb or aarch64-elf-gdb.

Option B: mi2gdb Command Viewer

To see the exact CLI commands VS Code generates:

  1. In one terminal, run:
    python3 scripts/mi2gdb --view
  2. In your launch.json, point miDebuggerPath to scripts/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.

Why Not Just Use VS Code's Built-In engineLogging?

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: engineLogging is designed for C/C++ extension developers debugging the IDE wire protocol, not humans learning GDB CLI syntax.
  • What mi2gdb Solves: mi2gdb filters 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).

Verifying Breakpoints with Network Traffic

  1. Open net/core/dev.c in VS Code and set a breakpoint inside netif_receive_skb_core.
  2. Press F5 (Continue). QEMU will finish booting and drop you into the BusyBox shell.
  3. In the QEMU console, send a test packet:
    / # ping -c 1 10.0.2.2
  4. 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.

Key Concepts and Common Gotchas

  • Binary Roles: QEMU executes Image (raw machine code), while GDB reads vmlinux.unstripped (ELF with DWARF debug info). Both contain the same compiled instructions.
  • KASLR: If CONFIG_RANDOMIZE_BASE is 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 pass nokaslr on 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 an error -2 (ENOENT) during boot.
  • Console Output: ARM64 virt machines use the PL011 UART (console=ttyAMA0). Setting console=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 private 10.0.2.0/24 virtual subnet. 10.0.2.2 is the virtual host gateway, 10.0.2.3 is the virtual DNS server, and 10.0.2.15 is the fixed address assigned to the first guest machine. Statically assigning 10.0.2.15 in /init gives immediate zero-delay networking without waiting for a DHCP lease.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages