---
title: "qemu+emacs+gdb调试内核"
author: "haoningabc"
date: "2015-12-02T22:15:52Z"
tags: ["Emacs", "GCC", "GDB", "Linux"]
categories: ["开源软件"]
slug: "qemu-emacs-gdb"
draft: false
lastmod: "2021-07-19T16:50:13Z"
id: 2261233
url: "https://haoningabc.iteye.com/blog/2261233"
---

core生成的位置
echo “/data/coredump/core.%e.%p” >/proc/sys/kernel/core_pattern


emacs -nw


**1.debug方式编译内核
2.qemu -S启动
3.设置emacs参数 ，启用gdb模式远程调试**

**环境：**

```java

[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

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

```


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

```

make defconfig
生成.config文件
**<span style="color:red">
这里如果使用这种方式生成的.config文件内核可以编译成功，但是在可能无法进入linux操作系统，尝试使用fabrice bellard的jslinux的配置文件
linuxstart-20120111/config_linux-2.6.20 
</span>**
<http://bellard.org/jslinux/tech.html>
下载配置文件
<http://dl.iteye.com/topics/download/9973ef69-2386-36bb-82f5-1c01a6edabac>
把这个保存成.config放到和Makefile同级的目录下
make menuconfig 
选择debug模式
![](http://dl2.iteye.com/upload/attachment/0113/4982/dbfb744d-24b2-3380-afc1-d1d4f0df1135.png)
![](http://dl2.iteye.com/upload/attachment/0113/4978/1e2e82c7-1ee7-3e18-8d14-a8c9ba0aedd9.png)
![](http://dl2.iteye.com/upload/attachment/0113/4980/0255469f-1c26-31b4-a890-24ad67c6e5a4.png)

vim scripts/mod/sumversion.c
最上面新加一行

```java

#include <limits.h>

```

<span style="color:red"> vim arch/i386/kernel/Makefile</span>
gcc的版本问题
gcc的参数-m elf_i386改成-m32
ld后的参数不改

```java

 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 

```

改成

```java

 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 

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

```


开始make

```java

make ARCH=i386 -j16

```

用qemu启动内核

```java

 /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

```java

/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则会在当前命令行进入系统
![](http://dl2.iteye.com/upload/attachment/0113/5725/7556e63c-8335-3382-afcd-16ba870df730.png)


可以使用qemu官方的linux-0.2.img(不过这个img自带2.6.20的内核，不影响)
如果不适用我们编译的内核，而直接用他自己的内核

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

```

vnc连接后
可以去看一下内核的版本
![](http://dl2.iteye.com/upload/attachment/0113/5727/8e45f25a-746a-3ce3-be5a-8f33ff9957cb.png)

其中-S 是gdb需要的
远程使用vnc 端口3或者5903
![](http://dl2.iteye.com/upload/attachment/0113/4984/3348711e-96fd-3ade-b334-457548d2b054.png)
进入后应该是一片漆黑

ctl+alt+2
进入qemu调试
然后输入gdbserver回车
默认开启1234端口
指定端口用
(qemu) gdbserver tcp::1233
如果省去gdbserver这步骤
可以启动的时候直接加-s 注意小s是启动server，大S是暂停

```java

/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 

```

![](http://dl2.iteye.com/upload/attachment/0113/4986/a650d497-d197-3f64-b54b-f2c8be1e5d59.png)
可以netstat一下1234端口是否打开

```java

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
这里需要设置一下，否则会出现

```java
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
![](http://dl2.iteye.com/upload/attachment/0113/4988/1b8fb5cb-a000-365a-9c57-9607dfef985c.png)

<span style="color:red">Customize variable: gdb-non-stop-setting</span>
![](http://dl2.iteye.com/upload/attachment/0113/4990/8b69158f-66a7-3ba2-8bd8-57573dc6c806.png)
在state上回车
修改成
![](http://dl2.iteye.com/upload/attachment/0113/4992/fe2a70a2-6935-38ba-8ba4-5248d9a2e342.png)
可以看到在~/.emacs里面多了

```java

(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
![](http://dl2.iteye.com/upload/attachment/0113/4994/924a53d1-2d54-3f56-aec7-74bc453bd4ca.png)
在gdb下 info source 查看当前文件的名字
**开始enjoy吧**


另外，linux追代码的时候如果使用vim
编译完内核后到linux-2.6.20目录
make tags
生成ctags文件
这样比自己生成的ctags要准，自己生成的当函数重名的时候会跳错乱
配置~/.vimrc

```java

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/>