For me, it can sometimes be the only or easiest way to find out why some code you have written is obstinately sticking to the wrong course of action, generally ending up with me fixing my stupid mistake! On Linux I never think twice about gdb’s availability; however, I was stumped to see that it was not available out of the box on my MacBook Pro, running OS X El Capitain, whilst gcc (the C compiler) was? This was the driving force behind this article, to provide you with helpful tips and some background information you might find useful when installing gdb.
gdb is a command line debugger. To operate it you use basic single letter commands, something I was always persuaded never to offer a user; as a coder though I kind of like it, it feels familiar and I know where I am with it, maybe that is just because I am a little long in the tooth! Although simple in operation, it has a comprehensive suite of capabilities. However, in comparison to today’s fantastic graphical IDEs, like Microsoft Visual Studio, IntelliJ IDEA, Netbeans or Eclipse (to name but a few) it seems very primitive, so why on earth would I want to use it? Well, sometimes I just enjoy doing things at the command line level and often it is easier to do so; for instance, if I am messing around on a Raspberry Pi or developing with code which only has a command line interface.
If gcc is already on the Mac then why is gdb not already there? Out of the box, you will find that even gcc is not available from the command line. Gcc is brought in as part of Xcode when it is installed; however, Xcode does not bring in gdb. It turns out that gcc and gdb are both “old hat” and have been replaced by the more modern clang and lldb respectively. In fact, if you type gcc without any parameters then you will see a clang error message. Clang and lldb are both parts of llvm, which underlies the Xcode development system as well as other platforms such as the Sony PS4, you can read more on that on the Wikipedia page.
I guess, what I should really be doing on my Mac is learning how to use clang and lldb. What I actually did was stick with what I knew (gcc and gdb) and worked out how to make them work from the console on my Mac. So if you have Xcode installed, can run gcc from the console and still want to use gdb, then read on.
To install gdb on the Mac your easiest path is to use Homebrew. Hombrew is “The missing package manager for OS X”, as is advertised on their website; which also explains how to install it and how to use it with very simple illustrations. Homebrew is great for installing and removing bits of software that are not common in the Apple store. The software is installed to a cellar maintained by Homebrew and then linked into the /usr/local folder from where it can easily be run on the command line. If you already have Hombrew installed then typing “brew list” on the command line will show you the list of packages you have installed; otherwise go ahead and install, following the instructions from the web site or by using the instructions from this web site, which includes a nice set of screenshots.
You are now ready to install gdb using Homebrew; others have made a good job of describing how to do that, so I suggest you check out their websites, such as this one or this one. One thing I noticed, between these sites, is that the first uses “brew install gdb” whilst the other uses “brew install homebrew/dupes/gdb”. You should use the first, “brew install gdb” as around last November, gdb as a recipe of homebrew was moved back into the core collection away from the dupes collection. Other than that, both cover the same operations, including signing gdb so that it can be run from the command line.
Once you have completed the install you can check by running “brew list” which should show the gdb package.
I hoped the above helped a little. Enjoy using gdb.
Mark Davison, Director, Terzo Digital. May 2016.