Linux内核调试环境搭建(基于ubuntu12.04)


http://www.netfairy.net/?post=242


by Netfairy - 2016-05-29


一、测试环境

物理机:ubuntu16.04 LTS

target(被调试机)环境:VirtualBox 5.0.20+ubuntu 12.04 LTS + linux kernel 3.0.4

host(调试机)环境: VirtualBox 5.0.20+ubuntu 12.04 LTS + gdb 


二、搭建虚拟机环境

安装VirtualBox 然后

新建一个标准的虚拟机镜像就OK!然后加载ISO一步一步安装就行!(磁盘大小最好稍微大点,大约40G已经相当够用了) 我安装的是ubuntu 12.04 上述已经说明环境!下同

安装完毕后点击设置 如图进行串口设置

1.png

说明:这里的com1 即是 系统里面的/dev/ttyS0,然后启动系统。


三、编译以及安装内核

到内核官网下载你自己需要调试的内核版本!(https://www.kernel.org/pub/linux/kernel/)

这里我下的是3.0.4,解压到/opt目录下,然后转到linux-3.0.4目录,复制现有内核的config 到 当前目录为 .config

rroot@netfairy-VirtualBox:/opt/linux-3.0.4# cp /boot/config-3.2.0-29-generic-pae  .config

执行

make menuconfig

注意这里可能会出现 


*** Unable to find the ncurses libraries or the
 *** required header files.
 *** 'make menuconfig' requires the ncurses libraries.
 *** 
 *** Install ncurses (ncurses-devel) and try again.
 *** 
make[1]: *** [scripts/kconfig/dochecklxdialog] 错误 1
make: *** [menuconfig] 错误 2


那么就需要执行下面一条命令

sudo apt-get install libncurses5-dev 

然后开始配置内核

root@netfairy-VirtualBox:/opt/linux-3.0.4# make menuconfig

(下面配置为必须进行的)

位置:    


General setup--->
           -> Prompt for development and/or incomplete code/drivers     
Kernel hacking--->  
           -*- Magic SysRq key   //默认选上了
           [*] Kernel debugging  //默认选上了
           [*] Compile the kernel with debug info     //默认选上了
           [*] KGDB: kernel debugging with remote gdb --->     //默认选上了
                  <*> KGDB: use kgdb over the serial console     //默认选上了
                  [*]KGDB:allow debugging with traps in notifiers    //默认选上了,别选别的
            [] Write protect kernel read-only data struct ures


四、 编译内核

执行 

make -j4 

执行 

make modules_install

执行 

make install 

编译安装已经完毕 


五、 修改grub


 将下面内容修改,下面两个图盗别人的 番外_使用KGDB调试内核 (_调试真的很重要) - KeVen.w - Study Hard And Make


修改后为


番外_使用KGDB调试内核 (_调试真的很重要) - KeVen.w - Study Hard And Make
其实就是删除第131 行 和 155 行

然后在

 linux /boot/vmlinuz-2.6.32.61 root=UUID=f8c18f80-7723-470b-98a852566006 ro

后面 和 

quiet

前面加以下内容

kgdb=ttyS0,115200 kgdboc=ttyS0,115200  kgdbwait

然后保存退出即可!重启电脑,克隆一台一模一样的虚拟机作为host机,修改串口如下

然后启动target机器,如图,选择

3.png


选择要调试的内核,出现下面这样子表明成功(如果不出现的话重启试试)


4.png


然后启动host机器,选择原来的内核,正常登入系统


5.png

切换到要调试的内核目录

root@netfairy-VirtualBox:/home/netfairy# cd /opt/linux-3.0.4/

然后

root@netfairy-VirtualBox:/opt/linux-3.0.4# gdb vmlinux
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /opt/linux-3.0.4/vmlinux...done.


接着

set remotebaud 115200


最后

(gdb) target remote /dev/ttyS0
Remote debugging using /dev/ttyS0
kgdb_breakpoint () at kernel/debug/debug_core.c:960
960	wmb(); /* Sync point after breakpoint */


然后continue就可以让target跑起来了

如果需要断下来,在被调试机的终端执行

echo g > /proc/sysrq-trigger


7.png


六、下面是我遇到的一些问题和解决办法:

问题一:

gcc: error: elf_i386: No such file or directory
OBJCOPY arch/x86/vdso/vdso32-int80.so
objcopy: 'arch/x86/vdso/vdso32-int80.so.dbg': No such file
make[2]: *** [arch/x86/vdso/vdso32-int80.so] Error 1
make[1]: *** [arch/x86/vdso] Error 2
make: *** [arch/x86] Error 2
解决:

In arch/x86/vdso/Makefile :
replace "-m elf_x86_64" by "-m64" on the line starting with VDSO_LDFLAGS_vdso.lds
replace "-m elf_i386" by "-m32" on the line starting with VDSO_LDFLAGS_vdso32.lds

问题二:

In file included from drivers/net/igbvf/ethtool.c:36:0:
drivers/net/igbvf/igbvf.h: 在文件作用域:
drivers/net/igbvf/igbvf.h:128:15: 错误: 重复的成员‘page’
make[4]: *** [drivers/net/igbvf/ethtool.o] 错误 1
make[3]: *** [drivers/net/igbvf] 错误 2
make[2]: *** [drivers/net] 错误 2
make[1]: *** [drivers] 错误 2
make[1]:正在离开目录 `/usr/src/linux-2.6.32.2'
make: *** [debian/stamp/build/kernel] 错误 2
解决:

打开igbvf.h:,看128行,代码为:struct page *page;再往上看,第123行,也有struct page *page这行代码,这个结构定义在内部的一个结构体中。就是他的名字与128行的重复了,而4.6.3的编译器对不支持这种方式的定义,我们修改128行的代码为struct page *pagep;保存退出;


问题三:VirtualBox里Ubuntu的共享文件夹无法访问 之问题:You do not have the permissions necessary to view the contents of ‘shared_folder’

解决:sudo usermod -a -G vboxsf netfairy


问题四:

安装好新内核,但是开机直接进入系统,无内核选择菜单

解决:

编辑 /boot/grub/grub.cfg
找到 timeout 【注:第二个地方的那几个timeout】数字改为10,保存。


原文地址:https://www.cnblogs.com/ztguang/p/12645764.html