How to debug kernel with QEMU-KVM

来源:互联网 发布:台湾士林夜市 知乎 编辑:程序博客网 时间:2024/05/16 17:45

Step 1: Configure Guest kernel

Make sure your "CONFIG_DEBUG_INFO" and"CONFIG_FRAME_POINTER" is select as "Y"

 

Example:

Kernel hacking  --->

Compile-time checks and compiler options  --->

[*] Compile the kernel with debug info 

[*] Compile the kernel with frame pointers

 

Step 2: Compile andinstall the new kernel on guest

Example:

make -j 143 && make modules && makemodules_install install

 

Step3: copy guestvmlinx into HOST

The vmlinux should not be compressed.

 

Example:

scp $(kernel_src)/vmlinux  $(user)@$(host_ip)/tmp

 

Step 4: restart VMguest with "-s" option

 

Example:

qemu-system-x86_64 -machine accel=kvm -smp 8 -drivefile=./test.raw,format=raw -m 2048  -s

 

Step 5: use gdb attachto the VM. I assume you use the Intel Chip(the best in the world)

 

Example

[root@lmcesrv host-mnt]# gdb

(gdb)  set architecturei386:x86-64:intel

The target architecture is assumed to bei386:x86-64:intel

(gdb) target remote :1234

(gdb) file /tmp/vmlinux1

A program is being debugged already.

Are you sure you want to change the file? (y or n) y

Reading symbols from /tmp/vmlinux1...done.

(gdb) c

 

Step 6: Debug thekernel, feel good!

 

Example:

(gdb) b __schedule

Breakpoint 1 at 0xffffffff815ae7c6: filekernel/sched/core.c, line 3057.

(gdb) c

Continuing.

[New Thread 2]

[Switching to Thread 2]

 

Breakpoint 1, __schedule () at kernel/sched/core.c:3057

3057    kernel/sched/core.c: No such fileor directory.

(gdb) bt

#0  __schedule () at kernel/sched/core.c:3057

#1  0xffffffff815af0fe in schedule () atkernel/sched/core.c:3144

#2  0xffffffff815af2fe in schedule_preempt_disabled() at kernel/sched/core.c:3177

#3  0xffffffff81095d05 in cpu_idle_loop () atkernel/sched/idle.c:275

#4  0xffffffff81095de3 in cpu_startup_entry(state=<optimized out>) at kernel/sched/idle.c:297

#5  0xffffffff8103a603 in start_secondary(unused=<optimized out>) at arch/x86/kernel/smpboot.c:251

#6  0x0000000000000000 in ?? ()

(gdb) c

Continuing.

 

Breakpoint 1, __schedule () at kernel/sched/core.c:3057

3057    in kernel/sched/core.c

(gdb) bt

#0  __schedule () at kernel/sched/core.c:3057

#1  0xffffffff815af0fe in schedule () atkernel/sched/core.c:3144

#2  0xffffffff81071ee4 in worker_thread(__worker=<optimized out>) at kernel/workqueue.c:2183

#3  0xffffffff81076bec in kthread(_create=0xffff88007cab4ec0) at kernel/kthread.c:209

#4  0xffffffff815b29ef in ret_from_fork () atarch/x86/entry/entry_64.S:472

#5  0x0000000000000000 in ?? ()\

 

(gdb) detach

Detaching from program: /tmp/vmlinux1, Remote target

Ending remote debugging.

(gdb)q
0 0