I am researching Linux kernel source code on Android. So I built a debugging environment on Android emulator (the emulator based on qemu). But, when I debug the kernel using gdb, the next command always jump to error code. Here is the example:
(gdb) b vfs_write
Breakpoint 3 at 0xffffffff80383ec8: file fs/read_write.c, line 527.
(gdb) c
Continuing.
[Switching to Thread 3]
Thread 3 hit Breakpoint 3, vfs_write (file=0xffff88003aa7a600, buf=0x72f8f6490f9f "*\032_\f\220\250\364\275\345\310\023\320\022", count=1, pos=0xffff88000b7c7f18)
at fs/read_write.c:527
527 if (!(file->f_mode & FMODE_WRITE))
(gdb) list
522
523 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
524 {
525 ssize_t ret;
526
527 if (!(file->f_mode & FMODE_WRITE))
528 return -EBADF;
529 if (!(file->f_mode & FMODE_CAN_WRITE))
530 return -EINVAL;
531 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
(gdb) n
ioread8 (addr=0xffffc90000040000) at lib/iomap.c:73
73 IO_COND(addr, return inb(port), return readb(addr));
(gdb)
The breakpoint at fs/read_write.c:527. When next (n), the debugger should stop at fs/read_write.c:528 or fs/read_write.c:529. But unexpected, it jumped to lib/iomap.c:73.
Why does this problem happen?
I get the Android kernel source from
https://android.googlesource.com/kernel/goldfish
And, I checkout branch android-goldfish-4.4-dev
I built the kernel with some configs:
CONFIG_DEBUG_KERNEL=y
CONFIG_DEBUG_INFO=y
CONFIG_KGDB=y
CONFIG_FRAME_POINTER=y
\# CONFIG_DEBUG_RODATA
is not set\# CONFIG_RANDOMIZE_BASE
is not set
I launched the emulator like this:
emulator -avd Pixel2XL-x86_64 -show-kernel -verbose -wipe-data -netfast -kernel arch/x86/boot/bzImage -qemu -s
The avd Pixel2XL-x86_64 is x86_64 arch, and I built the kernel as x86_64.
Someone said this is caused by GCC O2 optimize. I referenced kernel hacking: GCC optimization for better debug experience (-Og)
But, it was useless.