设为首页 收藏本站
查看: 790|回复: 0

[经验分享] VMware环境下用kgdb调试内核

[复制链接]
累计签到:5 天
连续签到:1 天
发表于 2015-10-8 14:02:26 | 显示全部楼层 |阅读模式
VMware环境下用kgdb调试内核2007-08-12  16:07  一、kgdb安装
  1. 系统配置:
VMware Workstation 5.5.4
Fedora Core 3(2.6.9-1.667)
kgdb-2.4(linux-2.6.15.5)

  2. 系统安装:
在虚拟机上安装FC3,然后参照后面的文章,将系统的内核升级到2.6.15.5,命名为“FC3-kgdb-client”。Clone一个和预装环境一样的系统,选择"Create a full clone",命名为“FC3-kgdb-server”。
分别为两个系统增加一个串口,以"Output to 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和server的区别:
client(development machine):即开发机,用于输入命令(例如设置断点)控制target machine的运行。
server(target machine):也就是被调试的机器了,其运行受到development machine命令的控制。
  3. 文件下载:
进入client系统,从网上下载如下源文件。(之所以选择2.6.15.5版本内核,是因为kgdb的patch目前最高只能支持到这个版本
linux kernel
http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.15.5.tar.gz
kgdb patch
http://kgdb.linsyssoft.com/downloads/kgdb-2/linux-2.6.15.5-kgdb-2.4.tar.bz2
  4. 安装过程:
解压缩各个包,假设最终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 menuconfig阶段,除了选择支持vmware中内核编译必须的选项,还需要加上如下内容:
  最关键的一条,就是:
Serial port number for KGDB,默认选项是1,需要改为0。
  接下来“make bzImage”,等着完成。
  下面要做的工作就相对简单一点:
将/usr/src/linux/arch/i386/boot/bzImage和/usr/src/linux/System.map复制到server上,这里使用scp命令复制,假设server的ip是192.168.6.13:
#scp arch/i386/boot/bzImage root@192.168.6.13:/boot/vmlinuz-2.6.15.5-kgdb
#scp System.map root@192.168.6.13:/boot/System.map-2.6.15.5-kgdb
  在server上创建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
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora Core (2.6.15.5-kgdb)
           root (hd0,0)
          kernel /boot/vmlinuz-2.6.15.5-kgdb ro root=/dev/hda1 kgdbwait kgdb8250=0,115200
title Fedora Core (2.6.15.5)
           root (hd0,0)
           kernel /boot/vmlinuz-2.6.15.5-secos ro root=LABEL=/ rhgb quiet
           initrd /boot/initrd-2.6.15.5-secos.img
title Fedora Core (2.6.9-1.667)
           root (hd0,0)
           kernel /boot/vmlinuz-2.6.9-1.667 ro root=LABEL=/ rhgb quiet
           initrd /boot/initrd-2.6.9-1.667.img
  OK, reboot重启吧。
  系统引导到
&quot;Uncompressing Linux... OK, booting the kernel.&quot;
所有的资料上都说看到
&quot;Waiting for connection from remote gdb...&quot;
才是等待调试状态,我的安装完之后可以看到这句话。当然也有可能只看到前面一句,我把这个版本的kgdb移植到2.6.20上就只显示前面一句话,不过可以用client正常连接。
  就可以用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 &quot;show copying&quot; to see the conditions.
There is absolutely no warranty for GDB.         Type &quot;show warranty&quot; for details.
This GDB was configured as &quot;i386-redhat-linux-gnu&quot;...Using host libthread_db library &quot;/lib/tls/libthread_db.so.1&quot;.
(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)
  上面“1212 atomic_set(&kgdb_setting_breakpoint, 0);”这一行表明已经连接上了。
  二、启动过程中的问题:
  1. VFS: Unable to mount root fs on unknown-block(3,1)
错误如下:
------------------------------------------------------------------------------
No filesystem could mount root, tried:    iso9660
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(3,1)
------------------------------------------------------------------------------
解决方法:将“Second extended fs support”和下面ext2和ext3相关选项都编入内核。
File systems->
<*> Second extended fs support

  • Ext2 extended attributes

  • Ext2 POSIX Access Control Lists

  • Ext2 Security Labels

  • Ext2 execute in place support
    <M> Ext3 journalling file system support

  • Ext2 extended attributes

  • Ext2 POSIX Access Control Lists

  • Ext2 Security Labels
      2.Warning: unable to open an initial console.
    解决方法:在2.6.13之前,将“dev file system support”编入内核
    File systems->Pseudo filesystems->

  • dev file system support
    2.6.13之后,需要生成“initrd”,在client端:
    #mkinitrd /boot/initrd-2.6.15.5-kgdb 2.6.15.5
    注意:上面命令最后2.6.15.5指的是“/lib/modules/2.6.15”这个目录,如果上面这个命令出错,你查看一下是否有这个目录。
    上面命令将在/boot下面生成initrd-2.6.15.5-kgdb这个文件,将其复制到server端:
    #scp /boot/initrd-2.6.15.5-kgdb root@192.168.6.13:/boot/ initrd-2.6.15.5-kgdb
      修改grub.conf,在“kernel /boot/vmlinuz-2.6.15.5-kgdb ro root=/dev/hda1 kgdbwait kgdb8250=0,115200”这行下面添加“initrd /boot/initrd-2.6.15.5-kgdb”
      三、模块调试
      1. gdbmod
    首先按上面过程配置好kgdb环境。从kgdb的网站下载gdbmod-2.4,将其改名为gdbmod,然后添加x属性,复制到/bin目录。
    #mv gdbmod-2.4 gdbmod
    #chmod +x gdbmod
    #cp gdbmod /bin
      2.编写内核模块和makefile
      在client机器上,写了个测试用的内核模块orig,如下:
      orig.c
    ------------------------------------------------
    void xcspy_func()
    {
           printk(&quot;<1>xcspy_func/n&quot;);
           printk(&quot;<1>aaaaaaaaaaa/n&quot;);
    }
      int xcspy_init()
    {
           printk(&quot;<1>xcspy_init_module/n&quot;);
             
           return 0;
    }
      void xcspy_exit()
    {
           printk(&quot;<1>xcspy_cleanup_module/n&quot;);
    }
      module_init(xcspy_init);
    module_exit(xcspy_exit);
    ------------------------------------------------
      Makefile
    ------------------------------------------------
    # Comment/uncomment the following line to disable/enable debugging
    DEBUG = y
      
    # Add your debugging flag (or not) to CFLAGS
    ifeq ($(DEBUG),y)
         DEBFLAGS = -O -g -DSCULL_DEBUG # &quot;-O&quot; is needed to expand inlines
    else
         DEBFLAGS = -O2
    endif
      CFLAGS += $(DEBFLAGS)
    CFLAGS += -I$(LDDINC)
      ifneq ($(KERNELRELEASE),)
    # call from kernel build system
      obj-m := orig.o
      else
      KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD         := $(shell pwd)
      modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules
      endif
      
    clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
      depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend
      
    ifeq (.depend,$(wildcard .depend))
    include .depend
    endif
    ------------------------------------------------
      #make
    #scp orig.ko root@192.168.6.13:/root
      3.开始调试
      在client机上:
    #cd /usr/src/linux
    # gdbmod vmlinux
    GNU gdb 6.0
    Copyright 2003 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 &quot;show copying&quot; to see the conditions.
    There is absolutely no warranty for GDB.    Type &quot;show warranty&quot; for details.
    This GDB was configured as &quot;i686-pc-linux-gnu&quot;...
    (gdb) set remotebaud 115200
    (gdb) target remote /dev/ttyS0
      设置符号文件的搜索路径(注意:orig后面不能有空格,有空格的话,会搜不到符号,大家都喜欢用tab键,而tab键会自动加空格,一定要记住把空格去掉)
    (gdb) set solib-search-path /root/orig
      这时在server机器上执行insmod orig.ko,如果正常,在client机上将显示共享库已经加载。
      接下来的过程就和用gdb调试应用程序差不多了,大家可以参照gdb user manual。
      vmware + kgdb + linux2.6.15内核调试配置
      
      参考文献:
    《vmware + kgdb + linux2.6.15内核调试配置》
    《Linux 系统内核的调试》
    《KGDB Quick Start》
    《使用kgdb调试linux内核及内核模块》
    《在VmWare Workstation中编译Linux 2.6.20内核》
    《RedHat9.0内核升级(从2.4.20-8到2.6.18)》
    《RHEL4内核升级(从2.6.9-42.EL到2.6.20.4)》

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-124381-1-1.html 上篇帖子: VMware+WinDbg联合调试Windows驱动环境搭建 下篇帖子: Vmware7.0上虚拟机 Redhat 5.0动在Starting udev 停几个小时
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表