网址: http://linux.chinaunix.net/techdoc/develop/2007/10/23/970501.shtml
Who may read
this document?
1. People who familiar with gdb but not never
use kgdb
2. People who like to debug kernel or driver
module using the gdb way.
Why to Using
KGDB
1. Want to debug kernel
2. Want to debug driver module
What’s
hardware you needed to use KGDB
1. Target system with have serial port
2. Host system with have serial port
3. Serial line
Tested
Target System: Fedora Core 3 of minimum installation
Tested Host
System: Fedora Core 3
How to Debug
Kernel using KGDB
1. Download kgdb patch for your kernel at
http://kgdb.linsyssoft.com/downloads
2. Host: cd /path/to/kernel/source/
3. Host: patch –p1 –i
/path/to/patch/file
4. for kernel version 2.5.0 to 2.6.7 download
patch:module section offsets in /sys/module. You can find it at
http://lwn.net/Articles/88052/
For kernel version 2.6.8 and later it is
already in kernel tree.
5. Host: make menuconfig, to confirm you choose
kgdb support in KERNEL_HACKING (before make menuconfig, you may copy the config file of target
system in /boot/confi….. to host)
6a. Host: make dep;make bzImage;make
modules;make modules_install (kernel version Host: make; make modules_install
(kernel version >2.5.0)
7. Copy built bzImage,
/path/to/kernel/source/System.map,to your target system under /boot named
bzImage-2.X.X-kgdb, System.map-2.X.X-kgdb.
Target: ln –s /boot/System.map-2.X.X-kgdb /boot/System.map.
Host: tar vzcf 2.X.X.tar.gz /lib/module/2.X.X. and copy to the target
system.
Target:
cd /lib/modules; tar vzxf 2.X.X.tar.gz
8. Target: mkinitrd initrd-2.X.X.img 2.X.X
(2.X.X is the kernel version of your download source)
9a. Target: edit /etc/grub.conf to boot system
using bzImage-2.X.X-kgdb,initrd-2.X.X.img with gdb gdbttyS=x gdbbaud=38400. Here ‘x’ is the index of your serial port used
to connect your host such as 0,1 or 2.(kernel version Target: edit /etc/grub.conf to boot
system using bzImage-2.X.X-kgdb,initrd-2.X.X.img with kgdbwait
kgdb8250=x,115200. Here ‘x’ is the index
of your serial port used to connect your host such as 0,1 or 2.(kernel version
> 2.5.0)
10. Host: cd /path/to/kernel/source
Host: gdb vmlinux
11a. Host gdb: set remotebaud 38400
(kernel version
11b. Host gdb: set remotebaud 115200 (kernel
version > 2.5.0)
12. Host gdb: target remote /dev/ttySx. Here ‘x’ is the index of your serial port
used to connect your target.
How to Debug
Driver Module using KGDB
1. Host: build your driver module with –g
option.
2a. Copy your driver.o to your target
system.(kernel version
2b. Copy your driver.ko to your target system.(kernel
version >2.5.0)
3. Reboot target with kernel build in kgdb.
4. Host gdb: Set breakpoint before kernel call
module’s initialization function so we can debug module’s initialization
function.
Kernel call module’s initialization in
kernel/module.c: sys_init_module():mod->init().
for kernel version >2.5.0 if you want to debug module’s
initialization function you may debug the sys_init_module()-àload_module()….to know the sections .text .data .bss address of the
module for using in 6b. you may find the value in add_sect_attrs()--àsattr->address
5. Target: load your driver module using insmod
and will stop at breakpoint set by 4.
6a. Host gdb: p/x module_list
Host gdb: add-symbol-file /path/to/your/driver/module/sourcecode address
where address is the value of
module_list+sizeof(module_list).
This command loads symbol file of
your module so you can debug it.
6b. Target: ls /sys/module/yourmodule/sections/
Host gdb: add-symbol-file /path/to/module 0xd081d000 \ # .text
-s .data 0xd08232c0 \ -s .bss 0xd0823e20
7. As 6 have add the symbol file of
your module, so you can set breakpoints to your source code and enjoy the
debugging.