How to Setup GDB in Mac OS

The last time I wrote something in C++ was 10 years ago during the lab session in the university, and recently I just decided to write my thesis project in C++ because of many reasons. First time writing after 10 years wasn't so easy and I got this error message on the screen when running the code:

Segmentation fault: 11

Not so helpful, is it? Meaning some errors relating to pointer happened somewhere in the program.

In Java (and probably many other modern programming languages too) usually a complete stack trace will be printed at least on the stdout, even if it's the programmer's fault for not catching the exception. Apparently in C to debug something like this there is a tool called GDB that can be easily installed in linux. On Mac it can be installed from brew, followed by a simple step of adding keychain access for gdb.

So to install:

  1. brew tap homebrow/dupes
  2. brew install gdb
  3. Open /Applications/Utilities/Keychain Access and create a certificate in "System". Things to highlight here are (chronologically):
    • Identity type: Self signed root
    • Certificate type: code signing
    • Let me override defaults
    • Store in "System"
  4. Get info of the just created certificate and set everything to "Always trust"
  5. codesign -s [name of certificate] $(which gdb)
Start using GDB:
  1. gdb [name of executable]
  2. run
    • up to this point the program will run and print out line number where the error happened and if need more information use backtrace.
  3. backtrace 
  4. help catch exception followed by catch throw and re-run debug to help catch exceptions that were not caught in code.
  5. quit

0 comments: (+add yours?)