win_debug_linux

来源:互联网 发布:德沃夏克第九交响.知乎 编辑:程序博客网 时间:2024/06/05 14:16

win_debug_linux  

2011-12-16 10:50:09|  分类:默认分类 |字号 订阅

debug linux C/C++程序的方法有很多,下面介绍一种在windowsdebug linux程序的方法,道理很简单,就是通过gdb提供的clientserverremote debug功能来进行远程调试,windowsgdb客户端,linuxgbd服务器。需要准备的软件如下:

MinGW windows
开发环境,主要用它的gcc来编译交叉gdb调试器

一个交叉gdb调试器,跑在windows上,但是targetlinux

eclipse
cdt

一个gbdserver,跑在linux

可选,最好配置smb,这样方便

1
安装MinGW

MinGW
是一个很小的GNU windows平台开发环境,包括linux下常见的开发工具,如gccgdbmakeautotools等等,具体介绍详见:http://mingw.org/

MinGW
的自动安装比较简单,从以下地址下载mingw-get-inst文件,按照提示安装就可以了:http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/mingw-get-inst/

如果需要手工安装,请参考:http://mingw.org/wiki/InstallationHOWTOforMinGWManual Installation小节。

最后,MinGW只是GNU windows开发环境的一种,您还可以选择像CYGwin等其他环境。

2
构建交叉gdb调试器

由于我们debug的是linux c应用程序,因此我们需要一个linux C ABI兼容的调试器,也就是说该gdb必须能够解析在linux C应用程序文件(通常是elf文件),

另外,该gdb调试器作为gdb客户端跑在windows机器上,因此运行平台(host)为windows

安装完MinGW后,我们通过其提供的gcc来构建我们的交叉gdb调试器,其实很简单,步骤如下:

下载gdb源代码: wget http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2
解压: tar xjf http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2
cd gdb-7.2

./configure --target=i686-pc-linux-gnu
先解释一下configure脚本的3个平台,输入configure --help可以看到:System types:

System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
--target=TARGET configure for building compilers for TARGET [HOST]System types:

build
表示你用来编译该gdb的平台
host
表示编译后用来运行gdb的平台
target
表示gdb的解析器的ABI平台,即gdb能够解析什么平台的可执行文件

那么如何确定这3个平台呢,由于我们的targetlinux平台,你可以在linux下先跑一下这个configure脚本,很快它会帮你guess出来的:

www.linuxidc.com@linuxidc:$ ./configure
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu

由于这里是在linuxconfigure,又没有输入任何configure参数,因此我们看到configure脚本自动猜出我们的3个平台都为i686-pc-linux-gnu

由于我们的build是在windows下,host也是windows,所以这2个参数就不用指定了,直接指定target就可以了:

Administrator@www.linuxidc.com ~/gdb-7.2
$ ./configure --target=i686-pc-linux-gnu
checking build system type... i686-pc-mingw32
checking host system type... i686-pc-mingw32
checking target system type... i686-pc-linux-gnu

configure
完后,如果没有什么错误的话,就可以make了,最后gdb-7.2/gdb目录下生成一个gdb.exe可执行文件,这就是我们构建的交叉调试器
为了方便可以将它重命名为linux_gdb.exe

构建后,测试一下,输入linux_gdb,应该输出类似以下文本:

$ linux_gdb
GNU gdb (GDB) 7.2
Copyright (C) 2010 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.
<span style="color:#FF0000;">This GDB was configured as "--host=i686-pc-mingw32 --target=i686-pc-linux-gnu".</span>
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb)


3 配置eclipse环境

假设你已经安装完windows下的eclipsecdt开发环境,那么就可以配置debug环境了,步骤如下:

  1. 假设你已经有一个C工程,例如nginx代码,首先,你需要在linux下编译代码,得到可执行文件,然后将代码导入到eclipse(我是通过smblinux共享代码的)

  2. 点击debug图标下的debug configurations,新建一个c/c++ Application,配置如下:

     

    win_debug_linux - 风逸尘 - 星殒

    win_debug_linux - 风逸尘 - 星殒

4 启动一个gdbserver

win_debug_linux - 风逸尘 - 星殒
这个gdbserver对应你在cdt中设置的的ip、端口。启动后,可以看到gdbserver开始监听来自远端的请求。



5开始debug

点击cdtdebug图标下相应的debug项:
win_debug_linux - 风逸尘 - 星殒
eclipse
就会连接这个gdbserver了,这是可以看到gdbserver端显示如下:
win_debug_linux - 风逸尘 - 星殒

 


说明gdbserver已经和eclipse建立debug连接了,eclipsecdtdebug视图如下:
win_debug_linux - 风逸尘 - 星殒
至此,就可以对linux C/C++程序在windows下进行debug了,一样可以查看变量、设置断点等等,具体就不讲了。

6 一些尚未测试或者不能确定的地方

这种debug方法有一些性质我还没有试过,目前我所了解到的可以说明如下:
1 debug
共享库或者静态库一般是比较难的,我还没去找如何加载so.a文件的方法
2
多线程调试和多进程调试支持不好,例如多进程不能set follow-fork-mode,这个是试过了的,但是多线程没试过