The GNU Project Debugger (GDB) is a debugger built by the open source GNU Project, and it lets us debug the following languages:
GDB comes with most of the Unix distributions that include Linux and OS X. In the latest OS X named Maverick, GDB isn't installed by default and can be installed using brew. For Windows, we will have to download and install Minimalist GNU for Windows (MinGW) from http://sourceforge.net/projects/mingw/files/. This will let us compile C code and use GDB to debug it. Sublime has an awesome plugin called SublimeGDB, which is used for debugging with GDB, and is written by Fredrik Ehnbom (@quarnster
).
We'll start by installing Sublime GDB using the Package Control. Let's open the command palette by pressing Ctrl + Shift + P in Windows or Linux, and Command + Shift + P in OS X. Then choose Install Package and install the SublimeGDB package. After installing, we'll need to configure SublimeGDB to make it work. Let's create a new Hello World C file, hello.c
:
#include <stdio.h> int main(void) { printf("Hello World! "); return 0; }
Make sure this is the only file in our current project, and save the project by going to Project | Save Project As... in the Sublime menu. After the project has been saved, let's edit it by going to Project | Edit Project. A new empty JSON project file will be opened. We'll need to add the following to make SublimeGDB work:
{ "folders": [ { "path": "C:\Users\Danpe\Desktop\src", }, ], "settings": { "sublimegdb_workingdir": "${folder:${project_path:hello.exe}}", // NOTE: You MUST provide --interpreter=mi for the plugin to work "sublimegdb_commandline": "gdb --interpreter=mi C:\Users\Danpe\Desktop\src\hello.exe" } }
This JSON sets our project's folders and settings for SublimeGDB to work. Before trying out GDB, let's compile our C code first by executing the following code:
gcc –g hello.c –o hello.exe
This will compile our C code in the debug mode, and the output file will be named hello.exe
. After compiling, we can open the code and start setting breakpoints by going to the desired line and pressing F9.
After toggling a breakpoint, we'll press F5 to run our executable using SublimeGDB:
In the preceding screenshot, we can see our breakpoint on line 5. After pressing F11 to Step Into, we are currently on line 6. At the bottom, we can see the current variables, callstack, threads, and more.
The following table shows a quick summary of all the required shortcuts for debugging with SublimeGDB:
SublimeGDB command |
Windows/Linux |
OS X |
---|---|---|
Launch |
F5 |
F5 |
Exit |
Ctrl + F5 |
Control + F5 |
Add/remove Breakpoint |
F9 |
F9 |
Step Over |
F10 |
F10 |
Step Into |
F11 |
F11 |
Step Out |
Shift + F11 |
Shift + F11 |
Continue |
F5 |
F5 |