GDB(GNU Project Debugger) is, as the name implies, a debugger with a lot of features to make your life easy when programming in C. Using gdb helps to debug C programs in real execution time, i.e. you can put break points in the program to evaluate the value of variables, detect when something has gone wrong, and many other uses. Here I’ll cover the basics that I use to debug simple C programs.
NOTE: To install gdb in Debian or another Debian based distro, just use
$ sudo apt-get gdb
NOTE (2): If your program receives CLI args, the gdb have support for it using the –args tag, but for testing purposes it’s better to just automate the input inside the program (by automatically creating a test input, without reading from the CLI).
How to use
1.Compile it with
$ cc -g file.c
2.Open the generated file with
$ gdb a.out
3.The gdb should open it. Now if you want to, you can create a break point using
$ break file.c:xx
Replace ‘xx’ with the number of line to pause the execution;
4.Run the program with
$ run -v -da -Q
5.As the program executes, you can print the value of variables using
$ print x
where x is the name of the variable;
6.With gdb, a segmentation Fault will display more info, allowing you to debug your program more easily! To exit gdb and go back to your default terminal, just type
$ quit
This is just the very basic of gdb. For those who want something a little more sophisticated, I reccomend to study the frame, backtrack and navigation command in gdb (usually the up and continue commands). For more information and details about each argument used in the commands, I recommend the following pages: