Windows下Debug Linux C/C++程序的一种方法
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调试器,其实很简单,步骤如下:
- 下载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能够解析什么平台的可执行文件
- www.bkjia.com@bkjia:$ ./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
由于我们的build是在windows下,host也是windows,所以这2个参数就不用指定了,直接指定target就可以了:
- Administrator@www.bkjia.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)
|
评论暂时关闭