使用 gdb 和 gdbserver 从 Windows 远程调试 Linux 进程:Windows 端到底需要什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3502509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remotely debugging a Linux process from Windows with gdb and gdbserver: what exactly is needed on the Windows side?
提问by rewbs
I am running Eclipse CDT on Windows to develop C code that is built & tested on remote Linux systems. Currently, the code is never compiled on Windows.
我在 Windows 上运行 Eclipse CDT 来开发在远程 Linux 系统上构建和测试的 C 代码。目前,该代码从未在 Windows 上编译。
I am able to use CDT to begin the remote process on the Linux target under gdbserver, and then attach gdb from the Windows host. However, gdb immediately fails with errors like:
我可以使用 CDT 在 gdbserver 下的 Linux 目标上开始远程进程,然后从 Windows 主机附加 gdb。但是,gdb 立即失败并出现如下错误:
warning: A handler for the OS ABI "GNU/Linux" is not built into this configuration
of GDB. Attempting to continue with the default i386 settings.
[...]
Remote 'g' packet reply is too long: 74afe9bff0aee9bf02000000f4af4a00a0aee9bf[...]
Debugging between two Linux systems works fine, so it's clear that I'm doing something wrong on the Windows host side. My specific questions are:
在两个 Linux 系统之间调试工作正常,所以很明显我在 Windows 主机端做错了。我的具体问题是:
Is the Cygwin version of gdb sufficient to debug remote Linux processes, or do I need a special cross-gdb in order to run it on Windows andwork with Linux processes? If so, is there anywhere can I get hold of such a gdb?
Remote debugging with gdb requires that symbols are available on the host system. What is the easiest way to achieve this? Can I just copy the symbols produced by the build on the Linux target to the Windows host, or do have to get a full build going on Windows? Is there a way to avoid this requirement, such that I can supply symbols only on the target?
Cygwin 版本的 gdb 是否足以调试远程 Linux 进程,或者我是否需要一个特殊的跨 gdb 才能在 Windows 上运行它并使用 Linux 进程?如果是这样,有什么地方可以得到这样的 gdb?
使用 gdb 进行远程调试要求符号在主机系统上可用。实现这一目标的最简单方法是什么?我可以将 Linux 目标上的构建生成的符号复制到 Windows 主机,还是必须在 Windows 上进行完整构建?有没有办法避免这个要求,这样我只能在目标上提供符号?
Thanks,
谢谢,
-R
-R
More info:The RSE FAQprovides some pointers, but unfortunately I'm still blocked. The FAQ describes two approaches:
更多信息:将RSE常见问题提供了一些三分球,但不幸的是,我仍然受阻。常见问题解答描述了两种方法:
- Launch the gdb client on the remote system over ssh. Problem here is that certain fields in the CDT debug launcher are tied to the local system (project path, executable path etc...).
- Build/obtain a cross-debugging version of gdb that supports debugging Linux processes from Windows. Problem here is that there's little info on how to achieve this.
- 通过 ssh 在远程系统上启动 gdb 客户端。这里的问题是 CDT 调试启动器中的某些字段与本地系统(项目路径、可执行路径等)相关联。
- 构建/获取支持从 Windows 调试 Linux 进程的 gdb 的交叉调试版本。这里的问题是关于如何实现这一点的信息很少。
I have also raised this issue on the CDT forum.
采纳答案by bmargulies
Setting up a cross-compile or cross-debug environment with gcc/gdb is a very difficult problem, and it's almost never the most efficient solution. Putting a linux VM on your windows box, and debugging there, will be much less work. If you need, really, to debug 'over there', I'd suggest just ssh-ing and using command-line gdb. If you can't have source over there, remote-debugging from a linux VM under your control would be practical.
使用 gcc/gdb 设置交叉编译或交叉调试环境是一个非常困难的问题,它几乎从来都不是最有效的解决方案。将 linux VM 放在您的 Windows 机器上并在那里进行调试,工作量会少得多。如果您真的需要在“那边”进行调试,我建议您只使用 ssh-ing 并使用命令行 gdb。如果您在那里无法获得源代码,那么从您控制的 linux VM 进行远程调试将是实用的。
回答by Eugene Weiss
Just rebuild gdb with target platform supporting. You can use Cygwin for this. Example for RHEL target platform:
只需在目标平台支持下重建 gdb。为此,您可以使用 Cygwin。RHEL 目标平台示例:
> wget http://ftp.gnu.org/gnu/gdb/gdb-<ver>.tar.xz
> tar -xJvf gdb-<ver>.tar.xz
> mkdir -p gdb-<ver>/build/x86_64-redhat-linux-gnu
> cd gdb-<ver>/build/x86_64-redhat-linux-gnu
> ../../configure --target=x86_64-redhat-linux-gnu
> make && make install
> x86_64-redhat-linux-gnu-gdb.exe --version
Don't forget to reconfigure your toolchain after this. To obtain the target configuration name, you can use:
在此之后不要忘记重新配置您的工具链。要获取目标配置名称,您可以使用:
> echo ${BASH_VERSINFO[5]}
回答by Mohamed Abdelazim
Now there is a plugin http://marketplace.eclipse.org/content/direct-remote-c-debugging
现在有一个插件 http://marketplace.eclipse.org/content/direct-remote-c-debugging
Which allows you to launch gdb on the server remotely over ssh. It takes care about path mapping and others things.
这允许您通过 ssh 在服务器上远程启动 gdb。它负责路径映射和其他事情。
You don't need gdb server to be running remotely
您不需要远程运行 gdb 服务器
回答by TheRealOrange
Visual Studio Community Edition 2017 features GDB cross-compiling and debugging tools. Couple this with a Linux server or the Windows Linux sub-system and you can reliably develop C code for Linux systems. check out this guide.
Visual Studio Community Edition 2017 具有 GDB 交叉编译和调试工具。将其与 Linux 服务器或 Windows Linux 子系统结合使用,您就可以可靠地为 Linux 系统开发 C 代码。查看本指南。
回答by Martin
Software development is generally much easier on linux than it is on windows. But that's off topic.
Linux 上的软件开发通常比 Windows 上容易得多。但那是题外话。
What is important when you debug cross compiled binaries is that you use gdb from cross compiler - not host gdb. You can not for example use gdb built for windows to debug a linux process (well you can but it is not recommended). You need to use the cross compiler gdb to debug the remote process (which is part of the same toolchain that was used to build the binary). As you are saying that you never compile on windows, I'm pretty certain this could be your problem. The easiest way for you to get it working is to just use the gdb from command line on the remote machine, where you compile, and do your debugging over ssh.
调试交叉编译的二进制文件时,重要的是使用来自交叉编译器的 gdb - 而不是宿主 gdb。例如,您不能使用为 Windows 构建的 gdb 来调试 linux 进程(您可以,但不建议这样做)。您需要使用交叉编译器 gdb 来调试远程进程(它是用于构建二进制文件的同一工具链的一部分)。正如您所说,您从不在 Windows 上编译,我很确定这可能是您的问题。让它工作的最简单方法是从远程机器上的命令行使用 gdb,在那里编译,并通过 ssh 进行调试。
回答by tutejszy
I failed to build on Windows, but found quite easy building it under Linux. To summarize and complete @Eugene response: First, prepare sources:
我未能在 Windows 上构建,但发现在 Linux 下构建它很容易。总结并完成@Eugene 的回复:首先,准备资源:
wget http://ftp.gnu.org/gnu/gdb/gdb-<ver>.tar.xz
tar -xJvf gdb-<ver>.tar.xz
mkdir -p gdb-<ver>/build/x86_64-redhat-linux-gnu
cd gdb-<ver>/build/x86_64-redhat-linux-gnu
Download Windows compiler:
下载 Windows 编译器:
sudo apt-get install mingw-w64
Check out the target configuration platform you want to debug your binaries (what to put in --target parameter):
查看您要调试二进制文件的目标配置平台(要放入 --target 参数的内容):
echo ${BASH_VERSINFO[5]}
Prepare makefiles targeted for your desired platform but running on different host. We compiling it static so that it doesn't depend on any DLLs or other libraries. Also we disabling building other binaries as gdb wikisuggests:
准备针对您所需平台但在不同主机上运行的 makefile。我们编译它是静态的,因此它不依赖于任何 DLL 或其他库。我们也禁用构建其他二进制文件,因为gdb wiki建议:
../../configure --host=x86_64-w64-mingw32 --target=x86_64-pc-linux-gnu --enable-static=yes --disable-interprocess-agent --disable-binutils --disable-ld --disable-gold --disable-gas --disable-sim --disable-gprof
finally, build (takes like 30-60 min):
最后,构建(大约需要 30-60 分钟):
make LDFLAGS=-static
You can find your debugger in gdb folder. It is also good to strip it of debugging symbols as after building executable is huge:
您可以在 gdb 文件夹中找到您的调试器。在构建可执行文件之后,剥离它的调试符号也很好:
strip -s gdb/gdb.exe
Voila! gdb.exe ready to run in Windows and remotely debug Linux executables!
瞧!gdb.exe 准备好在 Windows 中运行并远程调试 Linux 可执行文件!