gdb 8.2无法在macOS Mojave 10.14上识别可执行文件

35

我通过 brew install gdb 安装了gdb。

源文件内容如下:

#include <cstdio>
int main(){
    int a = 10;
    for(int i = 0; i< 10; i++){
        a += i;
    }
    printf("%d\n",a);
    return 0;
}

这里是名为“demo”的可执行文件:

https://pan.baidu.com/s/1wg-ffGCYzPGDI77pRxhyaw

我是这样编译源文件的:

c++ -g -o demo demo.cpp

并运行gdb

gdb ./demo

但是,它无法工作。它无法识别可执行文件。

GNU gdb (GDB) 8.2
Copyright (C) 2018 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-darwin18.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
BFD: /Users/xxx/Codes/demo: unknown load command 0x32
BFD: /Users/xxx/Codes/demo: unknown load command 0x32
"/Users/xxx/Codes/demo": not in executable format: file format not recognized

我使用file demo命令,它的输出是demo: Mach-O 64位可执行文件 x86_64

我使用file ./demo命令,它的输出是./demo: Mach-O 64位可执行文件 x86_64

输入c++ -v,输出为:

Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

运行./demo,输出为55。在gdb中输入show configuration,它会显示:
 This GDB was configured as follows:
 configure --host=x86_64-apple-darwin18.0.0 --target=x86_64-apple-darwin18.0.0
         --with-auto-load-dir=:${prefix}/share/auto-load
         --with-auto-load-safe-path=:${prefix}/share/auto-load
         --with-expat
         --with-gdb-datadir=/usr/local/Cellar/gdb/8.2/share/gdb (relocatable)
         --with-jit-reader-dir=/usr/local/Cellar/gdb/8.2/lib/gdb (relocatable)
         --without-libunwind-ia64
         --without-lzma
         --without-babeltrace
         --without-intel-pt
         --disable-libmcheck
         --without-mpfr
         --with-python=/System/Library/Frameworks/Python.framework/Versions/2.7
         --without-guile
         --with-separate-debug-dir=/usr/local/Cellar/gdb/8.2/lib/debug (relocatable)

谁能帮助我?非常感谢!!!


你使用的是哪个 gdb 版本?你是如何获取它的?你是否从 https://sourceware.org/gdb/download/ 下载了它的源代码并编译了它?如果是,你是如何配置它的?如果不是,请在 gdb 中显示 show configuration 的输出。同样的,你的 c++ 是什么(是 GCCClang,还是其他)?请显示 c++ -v 的输出。你能在同一个终端中运行 ./demo 吗?file ./demo 的输出是什么? - Basile Starynkevitch
展示一下demo.cpp的源代码(或者将其变成一个微型[MCVE])。首先尝试使用一个类似于“hello-world”的例子。 - Basile Starynkevitch
此外,我认为还没有任何在gdb上工作的人尝试过Mojave。提交一个gdb错误报告会很好。更好的方法是附加一个“hello world”类型的可执行文件,以便出现故障时进行调试。 - Tom Tromey
尝试了gdb 8.0和8.2,问题相同。 - Felipe Lima
我在我的Mac上也遇到了同样的问题。在有人修复这个问题之前,您可以尝试使用lldb(它可以正常工作)。 - Xiaoyu Chen
显示剩余4条评论
8个回答

16
问题在于,Apple LLVM version 10.0.0 分发的 clang-1000.11.45.2 给 o-mach 可执行文件添加了一个名为 LC_BUILD_VERSION 的新的加载命令。
$ otool -l test.o
...
Load command 1
       cmd LC_BUILD_VERSION
   cmdsize 24
  platform macos
       sdk n/a
     minos 10.14
    ntools 0
...

来自苹果source

/*
 * The build_version_command contains the min OS version on which this
 * binary was built to run for its platform.  The list of known platforms and
 * tool values following it.
 */

目前,bfd(gdb用于操作可执行文件的程序)无法解释此命令并返回错误。

作为临时解决方案,您可以编辑与gdb提供的bfd源代码。

首先,从mirrors下载gdb-8.0.1源代码。然后在gdb-8.0.1/bfd/mach-o.c的第4649行添加以下代码:

case BFD_MACH_O_LC_BUILD_VERSION:
break;

最后,在gdb-8.0.1/include/mach-o/loader.h的第189行中:

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

不要忘记在第188行BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30后面添加一个,
然后按照README中的说明处理经典的gdb编译。
run the ``configure'' script here, e.g.:

    ./configure 
    make

To install them (by default in /usr/local/bin, /usr/local/lib, etc),
then do:
    make install

不要忘记在此处签署gdb的解释here。如果您仍然遇到(os/kern)失败(0x5)错误,请运行sudo gdb
这是一种临时解决方案,以等待GNU团队的修复。
编辑 Binutils-gdb已更新,这些更改现在已在提交fc7b364中实现。
希望它有所帮助。

如何使用你编辑的提交?我已经下载了gdb-8.2源文件并尝试进行修改,但有一些文件我找不到。 - Q123
1
问题来自于 bfd 而不是直接来自 gdbbfd 存在于 binutils 包中。您可以通过 git git clone git://sourceware.org/git/binutils-gdb.git 获取 binutils。按照自述文件编译和安装 binutils,我正在使用 gdb 8.0.1 以避免兼容性问题。 - panic
3
有人已经向 brew 通报了这个更新吗? 更新:是的,https://discourse.brew.sh/t/mojave-and-gdb-8-2-or-lower-executable-not-found/3418。 - John Greene
1
我们可能需要在GitHub上发布一个问题:https://github.com/Homebrew/brew/issues - Brian
2
Homebrew已经打了补丁,不再需要额外的步骤了。 - MCCCS
是的,问题已经解决。适用于Mojave 10.14.1和GDB 8.2,截至2018年12月04日。 - j3141592653589793238

2

1

升级至 GDB 版本 8.3。此外,请参见 Binutils 错误跟踪器中的 问题 23728,在 macOS 10.14(Mojave)上由于未实现而导致 binutils 失败

来自错误报告

I've found the root of the issue. binutils does not handle load command 0x32 LC_BUILD_VERSION (nor 0x31 LC_NOTE, actually). They are defined in recent LLVM versions: see https://github.com/llvm-mirror/llvm/blob/master/include/llvm/BinaryFormat/MachO.def#L77

Looking at the output of objdump -private-headers there is one clear difference:

@@ -56,16 +56,18 @@ attributes NO_TOC STRIP_STATIC_SYMS LIVE
  reserved1 0
  reserved2 0
 Load command 1
-      cmd LC_VERSION_MIN_MACOSX
-  cmdsize 16
-  version 10.13
-      sdk n/a
+       cmd LC_BUILD_VERSION
+   cmdsize 24
+  platform macos
+       sdk n/a
+     minos 10.14
+    ntools 0
 Load command 2
      cmd LC_SYMTAB
  cmdsize 24

LC_VERSION_MIN_MACOSX is implemented in binutils, while LC_BUILD_VERSION is not. It is apparently new in Mojave.


GDB 8.3尚未发布。您所说的升级是指从最新源代码编译吗? - kevinAlbs


0

从Homebrew安装的gdb 8.2与Mac mojave不兼容。 我已经升级到8.2.1。问题应该得到解决。


0

我通过以下步骤在Mojave上使gdb工作:

a) 获取最新的gdb源代码存档(在撰写本文时,ftp://sourceware.org/pub/gdb/snapshots/current/gdb-weekly-8.2.50.20190212.tar.xz) - 其中,它添加了识别Mac上可执行文件的处理程序。

b) 构建gdb。 我在darwin-nat.c中遇到变量阴影错误,因此我编辑了该文件并重新构建。

c) 按照https://forward-in-code.blogspot.com/2018/11/mojave-vs-gdb.html中的步骤进行操作。

完成。

(来源:GDB on Mac/Mojave: During startup program terminated with signal ?, Unknown signal


0

我从Stack Overflow得到了一个对我有用的好方法,但我不知道它为什么能够工作。 这是链接

我是macOS的新手,我正在进行以下操作:

  1. 在 High Sierra 中对 gdb 8.0.1 进行 Codesign。
  2. 升级到 Mojave。
  3. gdb 8.0.1 出现错误:BFD:/Users/xxx/Codes/demo:未知的加载命令 0x32
  4. 更改为 gdb 8.2.1,遇到 Keychain 错误 Unknown Error -2,147,414,007

    通过在Login中获取证书,然后将其导出并导入到System(如果无法导入,则从Login中删除)来解决此问题。

  5. 最终,由于某些错误,它仍然无法正常工作,并出现以下错误:ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Unable to find Mach task port for process-id 1510: (os/kern) failure (0x5). (请检查 gdb 是否已签名 - 请参见 taskgated(8)),根据how to undo codesign,仍存在问题,答案告诉我要brew reinstall gdb,但仍然无法正常工作,昨天我就放弃了。
  6. 最后我找到了that link,我很高兴,现在我可以进行调试了!

希望我的解决方案能够帮到你。


-4

我在Mojave上通过减小应用程序的大小来解决了这个问题。GDB不理解通用二进制文件。因此,如果file myapp告诉你myapp是一个通用二进制文件,请尝试以下操作:

lipo -thin x86_64 -output myapp-x86_64 myapp

然后

gdb myapp-x86_64

1
我也是。更具体地说,我收到了这条消息:致命错误:/Library/Developer/CommandLineTools/usr/bin/lipo:当指定-thin选项时,输入文件(a.out)必须是一个fat文件。 - Enno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接