在 Centos 5 的 64 位机器上使用带有 32 位二进制文件的 gdb 和 gdbserver 会抱怨内存访问或数据格式错误
我有两台相同的 64 位 Centos 5 机器,它们已联网,并共享其 /home 安装。我在一台机器上编译了一个简单的 Hello World 程序,然后我弄清楚了如何在一台机器上使用 gdb 来远程调试在另一台机器上运行的程序。当每个人都默认为 64 位时,这似乎工作得很好。
但是,如果我使用 -m32 编译 Hello World 以生成 32 位二进制文件(我们的完整系统的编译方式),那么我无法弄清楚如何让 gdb 和 gdbserver 正确连接。在我在完整的系统上尝试之前,我想我应该让它与 hello 一起工作。根据我尝试连接 gdb 和 gdbserver 的方式,我会收到有关格式错误的寄存器的消息、有关体系结构不匹配的警告或非法内存引用。
我似乎不太了解 -m32 在我的编译中的含义,也不知道如何启动 gdb 和 gdbserver 或指定体系结构或文件或其他内容的正确顺序。 :(
在 64 位 Linux 机器上的 32 位 (-m32) 可执行文件上使用 gdb 和 gdbserver 需要什么?
下面的示例,谢谢
Jerry
hello.cpp:
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello World." << std::endl;
return -1;
}
以下是三个运行:
- 在 gdb 中,设置架构 i386 / 然后连接到 gdbserver => 错误的架构
- 在 gdb 中,设置架构 i386 / file hello / 然后连接到 gdbserver => 错误的架构
- 在 gdb 中,设置架构(错误) i386:x86-64 / file hello / 然后连接到 gdbserver => 无法访问内存
或者更详细一点:
==============================
对于每次运行,远程 gdbserver说:
$ gdbserver --multi rdev6:2010 hello
Process hello created; pid = 32603
Listening on port 2010
Remote debugging from host 134.51.26.149
readchar: Got EOF
Remote side has terminated connection. GDBserver will reopen the connection.
Listening on port 2010
而在我们本地:
==============================
- 假设是i386 32位,设置archi为i386,那么连接 注意:在 gdb 端,尚未指定或加载可执行文件
$ gdb
GNU gdb Fedora (6.8-37.el5)
his GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386
The target architecture is assumed to be i386
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
Remote register badly formatted: T0506:0000000000000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
here: 0000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
Try to load the executable by `file' first,
you may also check `set/show architecture'.
(gdb)
================================
- 假设它是 i386 32位,将archi设置为i386,然后连接 注意:在 gdb 端,可执行文件已加载文件
$ gdb
GNU gdb Fedora (6.8-37.el5)
his GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386
The target architecture is assumed to be i386
(gdb) file hello
Reading symbols from /home/j/hello...done.
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
Remote register badly formatted: T0506:0000000000000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
here: 0000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
Try to load the executable by `file' first,
you may also check `set/show architecture'.
(gdb) sho archi
The target architecture is assumed to be i386
(gdb)
================================
- 假设(这应该是不正确的) )它是i386:x86-64,将archi设置为i386:x86-64,然后连接 注意:在 gdb 端,可执行文件已加载文件
$ gdb
GNU gdb Fedora (6.8-37.el5)
This GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386:x86-64
The target architecture is assumed to be i386:x86-64
(gdb) file hello
Reading symbols from /home/j/hello...done.
(gdb) show archi
The target architecture is assumed to be i386:x86-64
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
[New Thread 32667]
Cannot access memory at address 0x800000008
(gdb)
I have two identical 64 bit Centos 5 machines, that are networked, and share their /home mount. I compile a simple Hello World program on one, and then I have figured out how to use gdb on one machine to remotely debug it running on the other machine. That seems to work fine when everyone defaults to 64 bitness.
However, if I compile my Hello World with -m32 to generate a 32 bit binary, the way our full up system is being compiled, then I cannot figure out how to get gdb and gdbserver to correctly connect. Before I try it on our full up system, I figure I should get it working with hello. Depending on how I try connecting gdb and gdbserver, I either get messages about badly formatted registers, warnings about architecture mismatches, or illegal memory references.
I seem to have little understanding of what the implications of -m32 are in my compile and no idea of how to start gdb and gdbserver or the right order to specify architecture or files or something. :(
What does it take to use gdb and gdbserver on a 32 bit (-m32) executable on a 64 bit linux box?
Examples below, and thank you,
Jerry
hello.cpp:
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello World." << std::endl;
return -1;
}
Here are three runs:
- In gdb, set architecture i386 / then connect to gdbserver => bad architecture
- In gdb, set architecture i386 / file hello / then connect to gdbserver => bad architecture
- In gdb, set architecture (incorrectly) i386:x86-64 / file hello / then connect to gdbserver => Cannot access memory
Or in a bit more detail:
==============================
For each run, the remote gdbserver said:
$ gdbserver --multi rdev6:2010 hello
Process hello created; pid = 32603
Listening on port 2010
Remote debugging from host 134.51.26.149
readchar: Got EOF
Remote side has terminated connection. GDBserver will reopen the connection.
Listening on port 2010
And on our local:
==============================
- Assuming it is i386 32 bit, setting archi to i386, then connecting
note: on the gdb side, the executable hasn't been specified or loaded
$ gdb
GNU gdb Fedora (6.8-37.el5)
his GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386
The target architecture is assumed to be i386
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
Remote register badly formatted: T0506:0000000000000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
here: 0000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
Try to load the executable by `file' first,
you may also check `set/show architecture'.
(gdb)
==============================
- Assuming it is i386 32 bit, setting archi to i386, then connecting
note: on the gdb side, the executable has been loaded with file
$ gdb
GNU gdb Fedora (6.8-37.el5)
his GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386
The target architecture is assumed to be i386
(gdb) file hello
Reading symbols from /home/j/hello...done.
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
Remote register badly formatted: T0506:0000000000000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
here: 0000000;07:b0dcdfff00000000;10:1018620000000000;thread:7f5b;
Try to load the executable by `file' first,
you may also check `set/show architecture'.
(gdb) sho archi
The target architecture is assumed to be i386
(gdb)
==============================
- Assuming (which should be incorrect) that it is i386:x86-64, setting archi to i386:x86-64, then connecting
note: on the gdb side, the executable has been loaded with file
$ gdb
GNU gdb Fedora (6.8-37.el5)
This GDB was configured as "x86_64-redhat-linux-gnu".
(gdb) set archi i386:x86-64
The target architecture is assumed to be i386:x86-64
(gdb) file hello
Reading symbols from /home/j/hello...done.
(gdb) show archi
The target architecture is assumed to be i386:x86-64
(gdb) target extended-remote rdev6:2010
Remote debugging using rdev6:2010
[New Thread 32667]
Cannot access memory at address 0x800000008
(gdb)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要使用 64 位 gdb/gdbserver 调试 32 位进程,则需要更新版本的 GDB。特别是,您需要这个:
以 32 位模式从源代码构建已有的 gdb/gdbserver
或者,您可以通过运行并使用 gdb32/gdbserver32 来调试进程, 。不过,我没有看到这样做有任何好处——新版本的 GDB 有许多修复、加速,而且 STL 漂亮的打印机也很好。
If you want to debug 32-bit process using 64-bit gdb/gdbserver, you need a newer version of GDB. In particular, you need this:
Alternatively, you can build gdb/gdbserver you already have from source in 32-bit mode by running
and use gdb32/gdbserver32 to debug your processes. I don't see any advantage of doing this though -- newer versions of GDB have many fixes, speedups, and STL pretty printers are nice.