ITeye博客

qemu+emacs+gdb调试内核

haoningabc · 2015-12-02 22:15 · linux · gdb · emacs · 开源软件 · Emacs · GCC · Linux · GDB · Markdown

core生成的位置 echo “/data/coredump/core.%e.%p” >/proc/sys/kernel/core_pattern

emacs -nw

1.debug方式编译内核 2.qemu -S启动 3.设置emacs参数 ,启用gdb模式远程调试

环境:


[root@centos140_11 ~]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
[root@centos140_11 ~]# gcc --version
gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

下载linux-2.6.20 http://www.linux-mips.org/pub/linux/mips/kernel/v2.6/ 修改Makefile


1438 # Modules
1439 %/: prepare scripts FORCE
1440         $(Q)$(MAKE) KBUILD_MODULES=$(if $(CONFIG_MODULES),1) \
1441         $(build)=$(build-dir)


 412 %config: scripts_basic outputmakefile FORCE
 413         $(Q)mkdir -p include/linux include/config
 414         $(Q)$(MAKE) $(build)=scripts/kconfig $@

make defconfig 生成.config文件 这里如果使用这种方式生成的.config文件内核可以编译成功,但是在可能无法进入linux操作系统,尝试使用fabrice bellard的jslinux的配置文件 linuxstart-20120111/config_linux-2.6.20 http://bellard.org/jslinux/tech.html 下载配置文件 http://dl.iteye.com/topics/download/9973ef69-2386-36bb-82f5-1c01a6edabac 把这个保存成.config放到和Makefile同级的目录下 make menuconfig 选择debug模式

vim scripts/mod/sumversion.c 最上面新加一行


#include <limits.h>

vim arch/i386/kernel/Makefile gcc的版本问题 gcc的参数-m elf_i386改成-m32 ld后的参数不改


 57 # The DSO images are built using a special linker script.
 58 quiet_cmd_syscall = SYSCALL $@
 59       cmd_syscall = $(CC) -m elf_i386 -nostdlib $(SYSCFLAGS_$(@F)) \
 60                           -Wl,-T,$(filter-out FORCE,$^) -o $@
 61 

改成


 57 # The DSO images are built using a special linker script.
 58 quiet_cmd_syscall = SYSCALL $@
 59       cmd_syscall = $(CC) -m32 -nostdlib $(SYSCFLAGS_$(@F)) \
 60                           -Wl,-T,$(filter-out FORCE,$^) -o $@
 61 

vim kernel/mutex.c


__mutex_lock_slowpath 的两个方法的static去掉 
__mutex_unlock_slowpath的两个方法的static去掉 

开始make


make ARCH=i386 -j16

用qemu启动内核


 /usr/libexec/qemu-kvm  -kernel linux-2.6.20.jslinux/arch/i386/boot/bzImage -hda hda.bin -append "root=/dev/hda console=ttyS0" -serial stdio -vnc 0.0.0.0:3

或者使用qemu的官网的test的img:linux-0.2.img


/usr/libexec/qemu-kvm  -kernel linux-2.6.20.jslinux/arch/i386/boot/bzImage -hda linux-0.2.img -append "root=/dev/hda console=ttyS0" -serial stdio -vnc 0.0.0.0:3 

注意:这里加了console=ttyS0 才有很多输出信息,不加没有 注意:这里的hda.bin是fabrice bellard的jslinux中的 注意,这里加了 -serial 才输出到屏幕上,在vnc连进去还是到"ok,booting the kernel" 如果不开-s和-S的调试模式,直接气得弄个qemu的话,append参数里面有console则会在当前命令行进入系统

可以使用qemu官方的linux-0.2.img(不过这个img自带2.6.20的内核,不影响) 如果不适用我们编译的内核,而直接用他自己的内核


 /usr/libexec/qemu-kvm  linux-0.2.img -vnc 0.0.0.0:3 

vnc连接后 可以去看一下内核的版本

其中-S 是gdb需要的 远程使用vnc 端口3或者5903 进入后应该是一片漆黑

ctl+alt+2 进入qemu调试 然后输入gdbserver回车 默认开启1234端口 指定端口用 (qemu) gdbserver tcp::1233 如果省去gdbserver这步骤 可以启动的时候直接加-s 注意小s是启动server,大S是暂停


/usr/libexec/qemu-kvm  -kernel linux-2.6.20.debug/arch/i386/boot/bzImage -hda hda.bin -append "root=/dev/hda" -serial stdio -vnc 0.0.0.0:3 -S -s 

可以netstat一下1234端口是否打开


netstat -nltp |grep 1234

进入linux-2.6.20目录 下面有个vmlinux

如果不使用emacs 直接gdb vmlinux 进入后 target remote localhost:1234

如果使用emacs: emacs M-x gdb Run gdb (like this): gdb -i=mi vmlinux 这里需要设置一下,否则会出现

get Non-stop mode requested, but remote does not support non-stop

的错误 设置方法如下 http://stackoverflow.com/questions/15274798/valgrind-vgdb-with-gdb-in-emacs

M-x customize-option

Customize variable: gdb-non-stop-setting 在state上回车 修改成 可以看到在~/.emacs里面多了


(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(gdb-non-stop-setting nil))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
~  

回到 M-x gdb target remote localhost:1234 M-x gdb-many-windows 在gdb下 info source 查看当前文件的名字 开始enjoy吧

另外,linux追代码的时候如果使用vim 编译完内核后到linux-2.6.20目录 make tags 生成ctags文件 这样比自己生成的ctags要准,自己生成的当函数重名的时候会跳错乱 配置~/.vimrc


syntax on
set tabstop=4
set paste
set tags=/home/haoning/rtclinux/websocket/web/jslinux/tmp/linux-2.6.20.jslinux/tags

备注:如果有linux启动不了的问题参考 http://processors.wiki.ti.com/index.php/Kernel_-_Common_Problems_Booting_Linux http://serverfault.com/questions/471719/how-to-start-qemu-directly-in-the-console-not-in-curses-or-sdl qemu:http://wiki.qemu.org/Testing 参考:http://blog.chinaunix.net/uid-20729583-id-1884617.html http://blog.chinaunix.net/uid-26872853-id-3428938.html 这里有版本相关的问题 参考极客范:http://www.geekfan.net/8663/

评论(2)

  1. #2 楼 · haoningabc · 2015-12-17 10:53

    leebyte writes:

    太NB了,期待早日用上Killinux! 么么哒

  2. #1 楼 · leebyte · 2015-12-17 10:51

    太NB了,期待早日用上Killinux!

评论功能维护中,当前仅展示历史评论。