Mac 10.9 上的 gdb 在 32+64 arch 中失败,并显示“不可执行格式:无法识别文件格式”


$ file app
app: Mach-O universal binary with 2 architectures
app (for architecture i386):    Mach-O executable i386
app (for architecture x86_64):  Mach-O 64-bit executable x86_64
$ gdb app
GNU gdb (GDB) 7.6
Copyright (C) 2013 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.
This GDB was configured as "x86_64-apple-darwin13.0.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
"app": not in executable format: File format not recognized

$ file test
test: Mach-O 64-bit executable x86_64
$ gdb test
GNU gdb (GDB) 7.6
Copyright (C) 2013 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.
This GDB was configured as "x86_64-apple-darwin13.0.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Users/dmulder/test...Reading symbols from /Users/dmulder/test.dSYM/Contents/Resources/DWARF/test...done.
done.

为什么 64 位二进制会成功,而 64+32 二进制会失败?

不幸的是,非苹果版本的GNU gdb目前无法调试通用(或"胖"(二进制文件(同时包含32位和64位可执行文件的二进制文件(。

一种选择是使用 lipo 提取单个架构并在其上运行 gdb:

lipo -thin x86_64 -output app-x86_64 ./app

lipo -thin i386 -output app-i386 ./app

如果您希望调试组合的可执行文件,可以尝试使用 LLDB 或 Apple 版本的 gdb。

正如OP评论的那样,使用Apple的gdb将解决问题。

以下是在OS 10.9上从源代码构建Apple gdb 6.3.50.20050815-cvs的说明:

注意:您需要安装 Xcode 并设置构建环境。如果您安装了自制软件,请运行brew doctor以查看"您的系统是否已准备好酿造"。

  1. 从以下位置下载 gdb-1822 源码包: http://opensource.apple.com/tarballs/gdb/gdb-1822.tar.gz

  2. 将其解压缩到临时目录中。打开终端并cd gdb-1822/src

  3. 运行configure脚本:

    ./configure --prefix="$HOME/.local/stow/gdb-1822" --disable-debug --disable-dependency-tracking --with-system-readline

    (最后三个配置参数来自自制重复公式:https://github.com/Homebrew/homebrew-dupes/blob/master/gdb.rb

  4. (
  5. 运行制作:

    做进行安装
  6. 按照 https://sourceware.org/gdb/wiki/BuildingOnDarwin#Creating_a_certificate 中的说明创建 gdb 证书代码签名证书。

  7. cd $HOME/.local/stow/gdb-1822/bin并对gdb可执行文件进行签名:

    Codesign -s GDB-cert GDB
  8. cd $HOME/.local/stow并存放gdb-1822文件夹:

    存放 GDB-1822
  9. $HOME/.local/bin添加到您的PATH并重新启动终端或将 Bash 的缓存清除到gdb的位置:

    哈希 -d GDB

最新更新