Windows下Debug Linux C/C++程序的一种方法


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

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

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

eclipse和cdt

一个gbdserver,跑在linux上

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

1 安装MinGW

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

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

如果需要手工安装,请参考:http://mingw.org/wiki/InstallationHOWTOforMinGW 的 Manual 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调试器,其实很简单,步骤如下:

  1. 下载gdb源代码: wget http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2
  2. 解压: tar xjf http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2
  3. cd gdb-7.2
  1.  ./configure --target=i686-pc-linux-gnu
    先解释一下configure脚本的3个平台,输入configure --help可以看到:System types:
    1. System types:  
    2.   --build=BUILD     configure for building on BUILD [guessed]  
    3.   --host=HOST       cross-compile to build programs to run on HOST [BUILD]  
    4.   --target=TARGET   configure for building compilers for TARGET [HOST]System types:  
    5.   
    6. build表示你用来编译该gdb的平台  
    7. host表示编译后用来运行gdb的平台  
    8. target表示gdb的解析器的ABI平台,即gdb能够解析什么平台的可执行文件  
    那么如何确定这3个平台呢,由于我们的target是linux平台,你可以在linux下先跑一下这个configure脚本,很快它会帮你guess出来的:
    1. www.bkjia.com@bkjia:$ ./configure   
    2. checking build system type... i686-pc-linux-gnu  
    3. checking host system type... i686-pc-linux-gnu  
    4. checking target system type... i686-pc-linux-gnu  
    由于这里是在linux下configure,又没有输入任何configure参数,因此我们看到configure脚本自动猜出我们的3个平台都为i686-pc-linux-gnu。
  • 由于我们的build是在windows下,host也是windows,所以这2个参数就不用指定了,直接指定target就可以了:

    1. Administrator@www.bkjia.com ~/gdb-7.2  
    2. $ ./configure --target=i686-pc-linux-gnu  
    3. checking build system type... i686-pc-mingw32  
    4. checking host system type... i686-pc-mingw32  
    5. checking target system type... i686-pc-linux-gnu  
    1. configure完后,如果没有什么错误的话,就可以make了,最后gdb-7.2/gdb目录下生成一个gdb.exe可执行文件,这就是我们构建的交叉调试器
      为了方便可以将它重命名为linux_gdb.exe
    1. 构建后,测试一下,输入linux_gdb,应该输出类似以下文本:
    1. $ linux_gdb  
    2. GNU gdb (GDB) 7.2  
    3. Copyright (C) 2010 Free Software Foundation, Inc.  
    4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
    5. This is free software: you are free to change and redistribute it.  
    6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
    7. and "show warranty" for details.  
    8. <span style="color:#FF0000;">This GDB was configured as "--host=i686-pc-mingw32 --target=i686-pc-linux-gnu".</span>  
    9. For bug reporting instructions, please see:  
    10. <http://www.gnu.org/software/gdb/bugs/>.  
    11. (gdb)  
    • 1
    • 2
    • 3
    • 下一页

    相关内容