# Linux Kernel Module Cheat

### 3.6. GDB step debug userland processes

QEMU’s `-gdb` GDB breakpoints are set on virtual addresses, so you can in theory debug userland processes as well.

- https://stackoverflow.com/questions/26271901/is-it-possible-to-use-gdb-and-qemu-to-debug-linux-user-space-programs-and-kernel

- https://stackoverflow.com/questions/16273614/debug-init-on-qemu-using-gdb

You will generally want to use gdbserver for this as it is more reliable, but this method can overcome the following limitations of `gdbserver`:

- the emulator does not support host to guest networking. This seems to be the case for gem5 as explained at: Section 15.3.1.3, “gem5 host to guest networking”

- cannot see the start of the `init` process easily

- `gdbserver` alters the working of the kernel, and makes your run less representative

Known limitations of direct userland debugging:

- the kernel might switch context to another process or to the kernel itself e.g. on a system call, and then TODO confirm the PIC would go to weird places and source code would be missing.

  Solutions to this are being researched at: Section 3.10.1, “lx-ps”.

- TODO step into shared libraries. If I attempt to load them explicitly:

      (gdb) sharedlibrary ../../staging/lib/libc.so.0
      No loaded shared libraries match the pattern `../../staging/lib/libc.so.0'.

  since GDB does not know that libc is loaded.

- GDB step debug userland custom init

- GDB step debug userland BusyBox init

- GDB step debug userland non-init