Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install GDB on MacOS 10.13.3 (High Sierra)

Tags:

macos

gdb

There are tons and tons of post and blogs discussing this topic, but nothing seems to work...

From across the internet, here is the common consensus regarding the procedure required to install and run gdb.

  1. Install gdb

    brew install gdb
    

    Homebrew "successfully" installs gdb v8.1

  2. Codesign gdb

    Codesign gdb by following the codesigning procedure (as detailed in a separate SO post).

  3. Update ~/.gdbinit

    echo "set startup-with-shell off" >> ~/.gdbinit
    

After completing all the steps, when I attempt to debug a simple "hello world" program, I get the following startup error resulting from SIGTRAP being throw.

(gdb) start
Temporary breakpoint 1 at 0x100000e66: file test.cpp, line 5.
Starting program: /Users/zfields/Documents/Development/test/a.out
[New Thread 0x1703 of process 67971]
[New Thread 0x1903 of process 67971]
During startup program terminated with signal SIGTRAP, Trace/breakpoint trap.

What am I missing?

like image 218
Zak Avatar asked Mar 11 '18 17:03

Zak


People also ask

Is GDB available on Mac?

As with GCC, the easiest way to install GDB is through Homebrew. In a Terminal window, run the command brew install gdb , and wait for it to complete. (As usual, it may ask for your password.) Now, we need to code-sign the GDB executable, so it will be allowed to control other processes, as necessary for a debugger.

How do I set up GDB?

The simplest way to configure and build GDB is to run configure from the `gdb- version-number ' source directory, which in this example is the `gdb-5.1. 1' directory. First switch to the `gdb- version-number ' source directory if you are not already in it; then run configure .

Where is GDB path Mac?

If you have installed gdb as explained before (using Homebrew), the path should be: /usr/local/Cellar/gdb/version/bin/gdb (replace version with the actual version of your gdb installation, e.g. /usr/local/Cellar/gdb/8.3/bin/gdb).


1 Answers

After hours and hours of searching, I finally found an obscure gist identifying the issue and detailing the solution.

TL;DR The GNU Debugger requires a patch before it can work with MacOS. gdb v8.0.1 is the last known good version of GDB for MacOS.

  1. Uninstall the latest version of gdb (i.e. v8.1)

    brew uninstall --force gdb
    
  2. Force Homebrew to install a version of gdb with the patch for MacOS.

    brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c3128a5c335bd2fa75ffba9d721e9910134e4644/Formula/gdb.rb
    
  3. Use the existing certificate to codesign the new install of gdb

    codesign -f -s  "<GNU GDB Certificate>" $(which gdb)
    

Now, gdb works as expected!

Special thanks to https://github.com/marcoparente and https://github.com/lokoum for their gist comments!

like image 77
Zak Avatar answered Oct 10 '22 05:10

Zak