vmware + kgdb + linux2.6.15内核调试配置
2008-11-10 20:45

from http://space.doit.com.cn/45811

===================================================
作者:ietf AT doit.com.cn
引用请注明出处。
===================================================
从早到晚,整整一天啊,总算成功了。
首先看看配置吧:
系统配置:
vmware workstation 5.5.3
FC-5,内核2.6.15
具体可以参考《在VmWare Workstation中编译 Linux 2.6.20内核》(以下简称《 2.6.20》)中“VmWare虚拟硬件环境:”和“VmWare原始系统内核环境”,完成系统的安装,命名系统为" FC-5-kgdb-client"。
Clone一个和预装环境一样的系统,选则"Create a full clone",命名为" FC-5-kgdb-server"。
分别为两个系统增加一个串口,以"use named pipe"方式,其中:
client端选择"this end is the client", "the other end is a virtual machine"
Server端选择"this end is the server", "the other end is a virtual machine"
文件下载:
进入client系统,从网上下载如下源文件。( 之所以算泽2.6.15版本内核,是因为kgdb的patch目前最高只能支持到这个版本
按照《2.6.20》所介绍方式,解压缩各个包。假设最终kernel和patch的路径如下:
/usr/src/linux
/usr/src/linux-2.6.15.5-kgdb-2.4
为内核打上kgdb的支持补丁(俺的过程如下,具体可以参考patch中README,选择需要得patch):
cd /usr/src/linux
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/core-lite.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/i386-lite.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/8250.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/eth.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/i386.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/core.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/module.patch
patch -p1 < ../linux-2.6.15.5-kgdb-2.4/sysrq_bugfix.patch
按正常编译内核流程,进入make menuxonfig阶段,除了《2.6.20》文中为了支持vmware中内核编译必须的选项,还需要加上如下内容:
http://kgdb.linsyssoft.com/p_w_picpaths/xconfig-selection.jpg
但是这张图片有点老,仅供参考。最关键的一条,上面没有,就是:
Serial port number for KGDB,默认选项是1,需要改为零。(正是这一点,折磨了我一个晚上)
接着该干吗就干吗,直到make bzImage完成。
下面要做的工作就相对简单一点:
将/usr/src/linux/arch/i386/boot/bzImage和/usr/src/linux/System.map复制到server上,进入这两个文件所在目录,
cp bzImage /boot/vmlinuz-2.6.15.5-kgdb
cp System.map /boot/System.map-2.6.15.5-kgdb
创建symbolic链接(为什么是symbolic,参考俺关于文件系统的文章吧)
ln -s /boot/vmlinuz-2.6.15.5-kgdb /boot/vmlinuz
ln -s /boot/System.map-2.6.15.5-kgdb /boot/System.map
下面就该修改启动项啦:
vim /boot/grub/grub.conf
我的给大家做个参考吧:
default=1
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora Core (2.6.15.5-kgdb)
root (hd0,0)
kernel /vmlinuz-2.6.15.5-kgdb ro root=/dev/VolGroup00/LogVol00 kgdb8250=0,115200 kgdbwait
title Fedora Core (2.6.15-1.2054_FC5)
root (hd0,0)
kernel /vmlinuz-2.6.15-1.2054_FC5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
initrd /initrd-2.6.15-1.2054_FC5.img
OK, reboot就等着重启吧。
系统引导到
"Uncompressing Linux... OK, booting the kernel."
所有的资料上都说看到
"Waiting for connection from remote gdb..."
才是等待调试状态,可我看到的只是前面一句。再加上默认得串口选项没有看到资料提醒需要修改,所以总是无法连接。这两个综合起来,就一直折腾了整整一天。好在最后耐心的看patch的README才有所启发。

就可以用client去连接了。在client上:
cd /usr/src/linux
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) 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)
下面就随心所欲,去折腾你的内核吧。


IETF好同学,补充一下Linux下的调试,
1)对于应用程序,主要是gdb;
1) kgdb主要是用于双机调试,特别是调试hardware driver;还有它支持ethernet的连接,此时注意关闭防火墙有时还需要处理selinux;
2) 而对于纯软件驱动,比如FS, VM等,kdb基本满足;
3) 对于作业系统上的死机,hang等,最好搞到core dump,然后用crash分析;