June 02, 2016

Installing gdb on OSX - El Capitain

Hola, debugging with gdb is favourite of mine. I thing gdb is easy to use and covers everything you
need while debugging. On OS X it's kind of a pain to get installed. Sure, installing it is quite easy using either MacPorts or brew. I', a Macport's kinda person so I'll stick to this method.

$ sudo port install gdb

Will install gdb under:

$ which ggdb
/opt/local/bin/ggdb

Notice the filename is ggdb in macports. If you favor the old gdb you can either create an alias in your .bash_profile or a symbolic link using:

$ sudo ln -s /opt/local/bin/ggdb /opt/local/bin/gdb

That's the easy part. Gdb will run, but fail when you attempt to run a process. 

$ ggdb helloworld
GNU gdb (GDB) 7.9.1
Copyright (C) 2015 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-darwin15.3.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"...
Reading symbols from helloworld...Reading symbols from /Users/cannabissen/Documents/src/c/helloworld.dSYM/Contents/Resources/DWARF/helloworld...done.
done.
(gdb) run
Starting program: /Users/cannabissen/Documents/src/c/helloworld 
Unable to find Mach task port for process-id 5745: (os/kern) failure (0x5).
 (please check gdb is codesigned - see taskgated(8))
(gdb) 

This failure is due to apple's codesigning. To have gdb execute and attach to processes you'll have to create a certificate and associate this certificate with gdb. The certificate process is described here. To start gdb for debugging you'll have run it in superuser mode:

sudo ggdb helloworld
Password:
GNU gdb (GDB) 7.9.1
Copyright (C) 2015 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-darwin15.3.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"...
Reading symbols from helloworld...Reading symbols from /Users/cannabissen/Documents/src/c/helloworld.dSYM/Contents/Resources/DWARF/helloworld...done.
done.
(gdb) run
Starting program: /Users/cannabissen/Documents/src/c/helloworld 
Hello, World
[Inferior 1 (process 1097) exited normally]

(gdb)

Have fun

No comments: