emacs -nw
[b]1.debug方式编译内核
2.qemu -S启动
3.设置emacs参数 ,启用gdb模式远程调试[/b]
[b]环境:[/b]
[code="java"]
# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
# 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.
[/code]
下载linux-2.6.20
[url]http://www.linux-mips.org/pub/linux/mips/kernel/v2.6/[/url]
修改Makefile
make defconfig
生成.config文件
[b][color=red]
这里如果使用这种方式生成的.config文件内核可以编译成功,但是在可能无法进入linux操作系统,尝试使用fabrice bellard的jslinux的配置文件
linuxstart-20120111/config_linux-2.6.20
[/color][/b]
[url]http://bellard.org/jslinux/tech.html[/url]
下载配置文件
[url]http://dl.iteye.com/topics/download/9973ef69-2386-36bb-82f5-1c01a6edabac[/url]
把这个保存成.config放到和Makefile同级的目录下
make menuconfig
选择debug模式
[img]http://dl2.iteye.com/upload/attachment/0113/4982/dbfb744d-24b2-3380-afc1-d1d4f0df1135.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0113/4978/1e2e82c7-1ee7-3e18-8d14-a8c9ba0aedd9.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0113/4980/0255469f-1c26-31b4-a890-24ad67c6e5a4.png[/img]
vim scripts/mod/sumversion.c
最上面新加一行
[color=red] vim arch/i386/kernel/Makefile[/color]
gcc的版本问题
gcc的参数-m elf_i386改成-m32
ld后的参数不改
改成
vim kernel/mutex.c
开始make
用qemu启动内核
或者使用qemu的官网的test的img:linux-0.2.img
注意:这里加了console=ttyS0 才有很多输出信息,不加没有
注意:这里的hda.bin是fabrice bellard的jslinux中的
注意,这里加了 -serial 才输出到屏幕上,在vnc连进去还是到"ok,booting the kernel"
如果不开-s和-S的调试模式,直接气得弄个qemu的话,append参数里面有console则会在当前命令行进入系统
[img]http://dl2.iteye.com/upload/attachment/0113/5725/7556e63c-8335-3382-afcd-16ba870df730.png[/img]
可以使用qemu官方的linux-0.2.img(不过这个img自带2.6.20的内核,不影响)
如果不适用我们编译的内核,而直接用他自己的内核
vnc连接后
可以去看一下内核的版本
[img]http://dl2.iteye.com/upload/attachment/0113/5727/8e45f25a-746a-3ce3-be5a-8f33ff9957cb.png[/img]
其中-S 是gdb需要的
远程使用vnc 端口3或者5903
[img]http://dl2.iteye.com/upload/attachment/0113/4984/3348711e-96fd-3ade-b334-457548d2b054.png[/img]
进入后应该是一片漆黑
ctl+alt+2
进入qemu调试
然后输入gdbserver回车
默认开启1234端口
指定端口用
(qemu) gdbserver tcp::1233
如果省去gdbserver这步骤
可以启动的时候直接加-s 注意小s是启动server,大S是暂停
[img]http://dl2.iteye.com/upload/attachment/0113/4986/a650d497-d197-3f64-b54b-f2c8be1e5d59.png[/img]
可以netstat一下1234端口是否打开
进入linux-2.6.20目录
下面有个vmlinux
[b]如果不使用emacs[/b]
直接gdb vmlinux
进入后
target remote localhost:1234
[b]如果使用emacs:[/b]
emacs
M-x gdb
Run gdb (like this): gdb -i=mi vmlinux
这里需要设置一下,否则会出现
的错误
设置方法如下
[url]http://stackoverflow.com/questions/15274798/valgrind-vgdb-with-gdb-in-emacs[/url]
M-x customize-option
[img]http://dl2.iteye.com/upload/attachment/0113/4988/1b8fb5cb-a000-365a-9c57-9607dfef985c.png[/img]
[color=red]Customize variable: gdb-non-stop-setting[/color]
[img]http://dl2.iteye.com/upload/attachment/0113/4990/8b69158f-66a7-3ba2-8bd8-57573dc6c806.png[/img]
在state上回车
修改成
[img]http://dl2.iteye.com/upload/attachment/0113/4992/fe2a70a2-6935-38ba-8ba4-5248d9a2e342.png[/img]
可以看到在~/.emacs里面多了
回到
M-x gdb
target remote localhost:1234
M-x gdb-many-windows
[img]http://dl2.iteye.com/upload/attachment/0113/4994/924a53d1-2d54-3f56-aec7-74bc453bd4ca.png[/img]
在gdb下 info source 查看当前文件的名字
[b]开始enjoy吧[/b]
另外,linux追代码的时候如果使用vim
编译完内核后到linux-2.6.20目录
make tags
生成ctags文件
这样比自己生成的ctags要准,自己生成的当函数重名的时候会跳错乱
配置~/.vimrc
备注:如果有linux启动不了的问题参考
[url]http://processors.wiki.ti.com/index.php/Kernel_-_Common_Problems_Booting_Linux[/url]
[url]http://serverfault.com/questions/471719/how-to-start-qemu-directly-in-the-console-not-in-curses-or-sdl[/url]
qemu:[url]http://wiki.qemu.org/Testing[/url]
参考:[url]http://blog.chinaunix.net/uid-20729583-id-1884617.html[/url]
[url]http://blog.chinaunix.net/uid-26872853-id-3428938.html[/url]
这里有版本相关的问题
参考极客范:[url]http://www.geekfan.net/8663/[/url]
[b]1.debug方式编译内核
2.qemu -S启动
3.设置emacs参数 ,启用gdb模式远程调试[/b]
[b]环境:[/b]
[code="java"]
# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
# 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.
[/code]
下载linux-2.6.20
[url]http://www.linux-mips.org/pub/linux/mips/kernel/v2.6/[/url]
修改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文件
[b][color=red]
这里如果使用这种方式生成的.config文件内核可以编译成功,但是在可能无法进入linux操作系统,尝试使用fabrice bellard的jslinux的配置文件
linuxstart-20120111/config_linux-2.6.20
[/color][/b]
[url]http://bellard.org/jslinux/tech.html[/url]
下载配置文件
[url]http://dl.iteye.com/topics/download/9973ef69-2386-36bb-82f5-1c01a6edabac[/url]
把这个保存成.config放到和Makefile同级的目录下
make menuconfig
选择debug模式
[img]http://dl2.iteye.com/upload/attachment/0113/4982/dbfb744d-24b2-3380-afc1-d1d4f0df1135.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0113/4978/1e2e82c7-1ee7-3e18-8d14-a8c9ba0aedd9.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0113/4980/0255469f-1c26-31b4-a890-24ad67c6e5a4.png[/img]
vim scripts/mod/sumversion.c
最上面新加一行
#include <limits.h>
[color=red] vim arch/i386/kernel/Makefile[/color]
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则会在当前命令行进入系统
[img]http://dl2.iteye.com/upload/attachment/0113/5725/7556e63c-8335-3382-afcd-16ba870df730.png[/img]
可以使用qemu官方的linux-0.2.img(不过这个img自带2.6.20的内核,不影响)
如果不适用我们编译的内核,而直接用他自己的内核
/usr/libexec/qemu-kvm linux-0.2.img -vnc 0.0.0.0:3
vnc连接后
可以去看一下内核的版本
[img]http://dl2.iteye.com/upload/attachment/0113/5727/8e45f25a-746a-3ce3-be5a-8f33ff9957cb.png[/img]
其中-S 是gdb需要的
远程使用vnc 端口3或者5903
[img]http://dl2.iteye.com/upload/attachment/0113/4984/3348711e-96fd-3ade-b334-457548d2b054.png[/img]
进入后应该是一片漆黑
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
[img]http://dl2.iteye.com/upload/attachment/0113/4986/a650d497-d197-3f64-b54b-f2c8be1e5d59.png[/img]
可以netstat一下1234端口是否打开
netstat -nltp |grep 1234
进入linux-2.6.20目录
下面有个vmlinux
[b]如果不使用emacs[/b]
直接gdb vmlinux
进入后
target remote localhost:1234
[b]如果使用emacs:[/b]
emacs
M-x gdb
Run gdb (like this): gdb -i=mi vmlinux
这里需要设置一下,否则会出现
get Non-stop mode requested, but remote does not support non-stop
的错误
设置方法如下
[url]http://stackoverflow.com/questions/15274798/valgrind-vgdb-with-gdb-in-emacs[/url]
M-x customize-option
[img]http://dl2.iteye.com/upload/attachment/0113/4988/1b8fb5cb-a000-365a-9c57-9607dfef985c.png[/img]
[color=red]Customize variable: gdb-non-stop-setting[/color]
[img]http://dl2.iteye.com/upload/attachment/0113/4990/8b69158f-66a7-3ba2-8bd8-57573dc6c806.png[/img]
在state上回车
修改成
[img]http://dl2.iteye.com/upload/attachment/0113/4992/fe2a70a2-6935-38ba-8ba4-5248d9a2e342.png[/img]
可以看到在~/.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
[img]http://dl2.iteye.com/upload/attachment/0113/4994/924a53d1-2d54-3f56-aec7-74bc453bd4ca.png[/img]
在gdb下 info source 查看当前文件的名字
[b]开始enjoy吧[/b]
另外,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启动不了的问题参考
[url]http://processors.wiki.ti.com/index.php/Kernel_-_Common_Problems_Booting_Linux[/url]
[url]http://serverfault.com/questions/471719/how-to-start-qemu-directly-in-the-console-not-in-curses-or-sdl[/url]
qemu:[url]http://wiki.qemu.org/Testing[/url]
参考:[url]http://blog.chinaunix.net/uid-20729583-id-1884617.html[/url]
[url]http://blog.chinaunix.net/uid-26872853-id-3428938.html[/url]
这里有版本相关的问题
参考极客范:[url]http://www.geekfan.net/8663/[/url]