How to Debug the Linux Kernel With Gdb and Qemu

How to debug the Linux kernel with GDB and QEMU?

I'd try:

(gdb) target remote localhost:1234
(gdb) continue

Using the '-s' option makes qemu listen on port tcp::1234, which you can connect to as localhost:1234 if you are on the same machine. Qemu's '-S' option makes Qemu stop execution until you give the continue command.

Best thing would probably be to have a look at a decent GDB tutorial to get along with what you are doing. This one looks quite nice.

debugging kernel with qemu and gdb, breakpoint not working?

This was solve not long after my posting the question and I forgot to put an answer.

It was because of the KASLR (kernel address space location randomization). You should disable it in the kernel configuration, or give option in the boot parameter. (without it, the kernel image is located in random location, causing mismach between debug symbol location and actual code location). This KASLR is turned on by default for aarch64.

In my case I did it with :

${QEMU_DIR}/qemu-system-aarch64 -M ${QMACHINE} -cpu cortex-a72 -kernel ${LINUX_DIR}/arch/arm64/boot/Image -initrd ${BUSYBOX_DIR}/initramfs.cpio.gz --append "root=/dev/ram init=/init nokaslr" -m 2048M -nographic

And I had to use 'hb'(or hbreak (hardware break)) instead of 'b'(or break).

How to debug Linux kernel modules with QEMU?

The easiest way in my opinion is to use buildroot
http://buildroot.uclibc.org/

clone it, configure it to use your custom kernel (default userspace is fine for a start, you might want to change it later).

it will build your kernel and root filesystem. the entire process takes about half an hour, twenty minutes of which is compiling the monster

my run line looks something:
qemu-system-i386
-hda rootfs.ext2
-kernel bzImage
-m 512M
-append "root=/dev/sda console=ttyS0"
-localtime
-serial stdio

and some more options regarding a tap device

Can't get gdb to stop at breakpoint in Linux kernel running under Qemu

The solution to the problem was to add nokaslr option and use hbreak. That means replace

-append "root=/dev/sda1"

with

`-append "root=/dev/sda1 nokaslr"

and

break start_kernel

with

hbreak start_kernel

then gdb properly catches kernel breakpoints.



Related Topics



Leave a reply



Submit