Friday, October 30, 2009

kgdb

The documentation you read is no more useful.
There is no need anymore to patch the kernel, just download the sources, for instance from kernel.org, go in the root directory of the sources and
then use either make xconfig or make menuconfig or something else to configure the kernel , ie set the options of the kernel .
You should go in Kernel Hacking sub-menu and select these options:
[*] Kernel debugging <-- not sure about this one [*] Compile the kernel with debug info[*] Compile the kernel with frame pointer[*] KGB: kernel debugging with remote gdb --->[*] KGDB: use kgdb over the serial console

and also that:
Device Drivers --->
Character Devices --->
Serial Drivers ---->
<*> 8250/16550 and compatible serial support[*] Console on 8250/16550 and compatible serial port


And I think that's it for the configuration step .

Then you have ti type make, the make modules_install and make install .
You'll have to check if your bootloader (grub, lilo or something else) has added an entry relative to the kernel you want to use,
and use correct kernel command line in this entry .
For instance in /boot/grub/menu.lst, I use this entry :

title linux 2.6.30 ( Kgdb )

root (hd0,4)

kernel /boot/vmlinuz-2.6.30 root=/dev/sda5 ro kgdb=ttyS0,38400 kgdboc=ttyS0,38400 kgdbwait

initrd /boot/initrd-2.6.30.img


You reboot, and if it should work nice, the kernel should wait for a serial connection from another machine with gdb.

Bye




====================================================

Contents

1. Hardware setup
2. Software setup
3. Applying the kgdb patch
4. Compiling the kernel on the development machine
5. Starting the debug session
6. Useful links and miscellaneous information

Hardware setup
Use a Null modem serial cable to connect across the Target and Development machines .

[ Development machine ]----------------------------------[ Target machine ]

1-Hardware pinout for the null modem serial cable

OR

2-Hardware pinout for the null modem serial cable

Testing the working of the Null-modem serial cable.

On the Development machine :
stty ispeed 115200 ospeed 115200 -F /dev/ttyS0
cat testfile.txt > /dev/ttyS0

On the Target machine :
stty ispeed 115200 ospeed 115200 -F /dev/ttyS0
cat /dev/ttyS0

Target machine.

The following settings are used for the Remote debugging session, automatically.

stty -F /dev/ttyS0
speed 115200 baud; line = 0;
min = 1; time = 0;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

Top
Software setup

1. Downloaded Linux kernel source : linux-2.6.6.tar.gz
2. Downloaded the 2.6.7 patch : patch-2.6.7.bz2
3. Downloaded the Kgdb patch : linux-2.6.7-kgdb-2.2.tar.bz2

4. Unzip the kernel sources & the kernel patch
cd ${BASE_DIR}
tar -zxvf linux-2.6.6.tar.gz
bunzip2 patch-2.6.7.bz2
ls ${BASE_DIR} --> linux-2.6.6 patch-2.6.7

5. mv ${BASE_DIR}/linux-2.6.6 ${BASE_DIR}/linux-2.6.7
6. cd ${BASE_DIR}/linux-2.6.7
7. patch -p1 < ${BASE_DIR}/patch-2.6.7

Applying the kgdb patch

1. Unzip the kgdb patch
mkdir ${BASE_DIR}/patch-kgdb
cd ${BASE_DIR}/patch-kgdb
tar -jxvf linux-2.6.7-kgdb-2.2.tar.bz2
2. patch -p1 < ${BASE_DIR}/patch-kgdb/core-lite.patch
3. patch -p1 < ${BASE_DIR}/patch-kgdb/i386-lite.patch
4. patch -p1 < ${BASE_DIR}/patch-kgdb/8250.patch
5. patch -p1 < ${BASE_DIR}/patch-kgdb/eth.patch
6. patch -p1 < ${BASE_DIR}/patch-kgdb/i386.patch
7. patch -p1 < ${BASE_DIR}/patch-kgdb/core.patch

Compiling the kernel on the development machine

1. In the ${BASE_DIR}/linux-2.6.7/Makefile, set the EXTRAVERSION = -kgdb
2. make mrproper V=1
3. make xconfig V=1 or make oldconfig V=1
Select the options appropriate for the target machine Hardware.
Select the options pertaining to kgdb under "Kernel hacking" .
Click to view the snapshot image

4. make clean V=1
5. make bzImage V=1
6. Transfer the built kernel to the Target machine from the Development machine .

Copy the Kernel image from ${BASE_DIR}/linux-2.6.7/arch/i386/boot/bzImage to the target machine as /boot/vmlinuz-2.6.7-kgdb
Copy the Map file from ${BASE_DIR}/linux-2.6.7/System.map to the target machine as /boot/System.map-2.6.7-kgdb
Also create links,
ln -s /boot/vmlinuz-2.6.7-kgdb /boot/vmlinuz
ln -s /boot/System.map-2.6.7-kgdb /boot/System.map

7. Edit the /boot/grub/grub.conf file in the target machine to have the kgdb enabled kernel entry.
Sample is shown below :-

# Sample grub.conf which will by default boot the kgdb enabled kernel
default=1
timeout=10
splashimage=(hd0,0)/boot/grub/splash.xpm.gz

title RH
root (hd0,0)
kernel /boot/vmlinuz ro root=/dev/sda1
initrd /boot/initrd

title Linux-2.6.7-kgdb
root (hd0,0)
kernel /boot/vmlinuz-2.6.7-kgdb ro root=/dev/sda1 rootfstype=ext3 kgdbwait kgdb8250=0,115200

Top
Starting the debug session

1. After booting the target machine will wait for the host development machine to connect, by displaying the message :-

Waiting for connection from remote gdb...

2. cd ${BASE_DIR}/linux-2.6.7
3. For setting a debug session with baud rate of 115200 on /dev/ttyS0 , run as "root" user:-

gdb ./vmlinux
GNU gdb Red Hat Linux (6.0post-0.20040223.17rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) shell echo -e "\003" > /dev/ttyS0
(gdb) set remotebaud 115200
(gdb) target remote /dev/ttyS0
Remote debugging using /dev/ttyS0
breakpoint () at kernel/kgdb.c:1212
1212 atomic_set(&kgdb_setting_breakpoint, 0);
warning: shared library handler failed to enable breakpoint
(gdb)

4. For further commands refer http://kgdb.linsyssoft.com/tockdebug.htm

Top
Useful links and Miscellaneous information

1. Useful Links :
http://kgdb.sourceforge.net/
http://kgdb.linsyssoft.com/
http://kgdb.linsyssoft.com/downloads/

2. Active Developer : Amit Kale --> amitkale@linsyssoft.com
3. Bug Report : kgdb-bugreport@lists.sourceforge.net

==============================
If you are using kgdboc, you need to have used kgdbwait as a boot argument, issued a sysrq-g, or the system you are going to debug has already taken an exception and is waiting for the debugger to attach before you can connect gdb.

If you are not using different kgdb I/O driver other than kgdboc, you should be able to connect and the target will automatically respond.

Example (using a serial port):

% gdb ./vmlinux
(gdb) set remotebaud 115200
(gdb) target remote /dev/ttyS0


Example (kgdb to a terminal server):

% gdb ./vmlinux
(gdb) target remote udp:192.168.2.2:6443


Example (kgdb over ethernet):

% gdb ./vmlinux
(gdb) target remote udp:192.168.2.2:6443


Once connected, you can debug a kernel the way you would debug an application program.

If you are having problems connecting or something is going seriously wrong while debugging, it will most often be the case that you want to enable gdb to be verbose about its target communications. You do this prior to issuing the target remote command by typing in: set remote debug 1


http://www.kernel.org/pub/linux/kernel/people/jwessel/kgdb/ch04.html

No comments:

Post a Comment