35

I get gdb by brew install gdb.

The source file content is:

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

Here is the executable file named 'demo': https://pan.baidu.com/s/1wg-ffGCYzPGDI77pRxhyaw

I compile the source file like this:

c++ -g -o demo demo.cpp

And run gdb

gdb ./demo

But, it can't work. It can't recognized the executable file.

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

I use file demo,its ouput is demo: Mach-O 64-bit executable x86_64

I use file ./demo,its output is ./demo: Mach-O 64-bit executable x86_64

Type c++ -v, output is :

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

run ./demo,its output is 55 type show configuration in gdb,it shows:

 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)

Who can help me ? Thank you very much !!!

food far
  • 353
  • 1
  • 4
  • 7
  • What `gdb` are you using? How did you get it? Have you downloaded its source code from https://sourceware.org/gdb/download/ and compiled it? If yes, how have you configured it? If no, show the output of `show configuration` in `gdb`. Likewise for your `c++` (is it [GCC](http://gcc.gnu.org/), [Clang](http://clang.llvm.org/), ....)? Show the output of `c++ -v`. Are you able to run `./demo` in the same terminal ? What is the output of `file ./demo` ? – Basile Starynkevitch Sep 27 '18 at 05:18
  • Show perhaps the source of `demo.cpp` (or make it a tiny [MCVE]). Try first with a *hello-world* like example – Basile Starynkevitch Sep 27 '18 at 05:24
  • This sounds a lot like https://sourceware.org/bugzilla/show_bug.cgi?id=13157, except that was fixed in 8.2. Also note that there are some macOS fixes that are only on git master -- and they are needed starting at least with High Sierra. – Tom Tromey Sep 27 '18 at 11:56
  • Also, I don't think anybody working on gdb has tried Mojave yet. Filing a gdb bug would be great. Even better would be attaching a "hello world"-type executable where it fails. – Tom Tromey Sep 27 '18 at 11:58
  • Tried both gdb 8.0 and 8.2, same problem – Felipe Lima Sep 29 '18 at 04:15
  • I have met the same problem on my mac, too. You could try `lldb` (which works fine) until somebody fix this problem. – Xiaoyu Chen Oct 02 '18 at 07:03
  • I tried to uninstall gdb using homebrew and reinstall it from sources following [BuildingOnDarwin](https://sourceware.org/gdb/wiki/BuildingOnDarwin) guide but same problem – panic Oct 03 '18 at 21:48
  • I've reported this bug [#23746](https://sourceware.org/bugzilla/show_bug.cgi?id=23746) to sourceware. – panic Oct 09 '18 at 07:21
  • @ChrisF - I moved to close the other question because I believed it was a duplicate. But as you can see, the community has not closed the other question as a duplicate. Hence, different they are questions at this moment in time. – jww Dec 03 '18 at 09:22

8 Answers8

16

The problem is that clang-1000.11.45.2 distributed with Apple LLVM version 10.0.0 adds a new load command to o-mach executables named 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
...

From the apple 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.
 */

So currently bfd (the program used by gdb to manipulate executables) is not able to interpret this command and returns the error.

As a temporary solution, you can edit the bfd sources code provides with gdb.

First, download gdb-8.0.1 sources from mirrors. Then add to gdb-8.0.1/bfd/mach-o.c the following code at line 4649 :

case BFD_MACH_O_LC_BUILD_VERSION:
break;

Finally inside gdb-8.0.1/include/mach-o/loader.h at line 189:

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

Don't forget to add a , at the end of the line 188 after BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30).

Then process a classic gdb compilation following instructions from the README :

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

Don't forget to sign gdb as explain here. If you still get the (os/kern) failure (0x5) error, just run sudo gdb.

This is a temporary solution in order to wait for a fix from GNU team.

EDIT

Binutils-gdb has been updated, these changes are now implemented in commit fc7b364.

Hope It will be helpful.

panic
  • 2,004
  • 2
  • 17
  • 33
  • How to use the commit where you edited? I have downloaded gdb-8.2 source files and tried to modify as committed. but there are some files that I cannot find. – Q123 Nov 11 '18 at 04:15
  • 1
    The problem comes from `bfd` not directly `gdb`, `bfd` is present inside the `binutils` package. You can obtain `binutils` via git `git clone git://sourceware.org/git/binutils-gdb.git`. Follow the readme to compile and install `binutils`, I'm using gdb 8.0.1 to avoid compatibility problems. – panic Nov 12 '18 at 22:02
  • 3
    Has anyone informed `brew` of this update? UPDATE: Yes, https://discourse.brew.sh/t/mojave-and-gdb-8-2-or-lower-executable-not-found/3418 – John Greene Nov 26 '18 at 15:18
  • 1
    We probably want to make an issue on GitHub: https://github.com/Homebrew/brew/issues – Brian Nov 27 '18 at 16:40
  • 2
    Homebrew patched it, there’s no need for extra steps anymore. – MCCCS Dec 03 '18 at 17:40
  • True, the issue is fixed. Works for Mojave 10.14.1 GDB 8.2 as of Dec. 04 '18. – j3141592653589793238 Dec 04 '18 at 17:19
2

I published a temporary brew formula that seems to work, while awaiting for official brew formula to be updated:

brew install https://raw.githubusercontent.com/timotheecour/homebrew-timutil/master/gdb_tim.rb

timotheecour
  • 3,104
  • 3
  • 26
  • 29
1

Upgrade to GDB version 8.3. Also see Issue 23728, binutils fail on macOS 10.14 (Mojave) due to unimpl in the Binutils bug tracker.

From the bug report:

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.

jww
  • 97,681
  • 90
  • 411
  • 885
0

I have got a good solution for me from stack overflow and I do not know why it works. Here is the link.

I am new to macOS, and I do the following:

  1. Codesign gdb 8.0.1 in high Sierra
  2. Update to Mojave
  3. gdb 8.0.1 died with BFD: /Users/xxx/Codes/demo: unknown load command 0x32
  4. Change to gdb 8.2.1 and come across Keychain error Unknown Error -2,147,414,007.

    Solve this by getting the certificate in Login then export it and import it into System(Delete it from Login if unable to import).

  5. Finally, because of some kind of errors it still does not work and it comes with 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). (please check gdb is codesigned - see taskgated(8)), according to how to undo codesign, something wrong still exist and the answer tells me to brew reinstall gdb, but it still does not work, I called it a day yesterday.
  6. Finally I come across that link, I AM HAPPYY, now I am able to debug!

Hope my solution can help.

Karl Han
  • 1
  • 1
0

I got gdb working on Mojave by:

a) getting the latest gdb source archive (at time of writing, ftp://sourceware.org/pub/gdb/snapshots/current/gdb-weekly-8.2.50.20190212.tar.xz) - amongst other things, it adds handling for recognizing executables on Mac.

b) build gdb. I got errors for variable shadowing in darwin-nat.c so I edited the file and rebuilt.

c) follow steps in https://forward-in-code.blogspot.com/2018/11/mojave-vs-gdb.html

Voila.

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

Joubert Nel
  • 3,154
  • 3
  • 26
  • 24
0

gdb 8.2 installed from Homebrew is not compatible with Mac mojave. I have upgrade to 8.2.1. The issue should be resolved.

Pin
  • 23
  • 4
0

The answer by timotheecour above did work for me:

brew install https://raw.githubusercontent.com/timotheecour/homebrew-timutil/master/gdb_tim.rb

Then I had to generate a generate a self-signed certificate as in https://www.thomasvitale.com/how-to-setup-gdb-and-eclipse-to-debug-c-files-on-macos-sierra/

-4

I got past this issue on Mojave by thinning the app. GDB does not understand universal binaries. So if file myapp tells you myapp is a universal binary, try this:

lipo -thin x86_64 -output myapp-x86_64 myapp

And then

gdb myapp-x86_64
  • 1
    Me neither. More specifically, I get this message: fatal error: /Library/Developer/CommandLineTools/usr/bin/lipo: input file (a.out) must be a fat file when the -thin option is specified – Enno Nov 23 '18 at 12:24