The normal process for developing computer programs goes something like this: write some code, compile the code, run the program. If the program does not work as expected, then you go back to the code to look for errors (bugs) and repeat the cycle again.
Depending on the complexity of the program and the nature of the bugs, there are times when you can do with some additional help in tracking down the errors. This is what a “debugger” does. It allows you to examine a computer program while it is running. You can see the values of the different variables, you can examine the contents of memory and you can halt the program at a specified point and step through the code one line at a time.
The primary debugger on Linux is the GNU debugger (gdb). It might already be installed on your system (or a slimmed down version called gdb-minimal), but to be sure type the following command in a terminal:
To use the debugger, you need to tell the compiler to include debug information in the binary. Create a file called “hello10.c” using nano:
Copy and paste in the following code:
The "-g" flag tells the compiler to produce debug information, so to compile the program use:
To start debugging the program type:
At this point if you just start running the program (using the "run" command), then the program will execute and finish before you get a chance to do anything. To stop that, you need to create a "breakpoint" which will halt the program at a specified point. The easiest way to do this is to tell the debugger to stop in the function "main()":
Now start the program:
The debugger has stopped at the first executable line of code, e.g. the "for" loop. To step to the next line, type "next" or "n" for short. Keep using "next" to repeat around the loop a couple of times:
To inspect the value of a variable use the "print" command. In our example program, we can examine the contents of the variable "i":
Repeat around the loop and few more times and see how "i" changes:
In the example above "i" has reached the value of 4. The loop will continue while "i" is less than 10. You can change the value of a variable using "set var." Type the following in gdb to set "i" to 10.
You may need to do another "next" (depending on where the program was halted when you set "i" to 10), but when the "for" loop line is next executed, the loop will exit, because "i" is no longer less than 10.
The "next" command doesn't drill down into functions but rather the function is executed, and the debugger stops again at the next line after the function. If you want to step into a function, use the "step" command, or "s" for short.
Another way to debug your program is to set a watch on a variable. What this does is halt the program whenever the variable changes. Restart the program again by typing "run." Since the program is already running, the debugger will ask if you want to start it again from the beginning.
The program will stop in main (as we didn't remove the break point). Now set a watch on "i":
The "continue" command starts the program running again until the next break point condition. In this case it will run again until the variable "i" changes (due to the watch).
To stop debugging, just use the "quit" command.
If you want to learn some more about gdb, then a good place to start is the GDB Documentation. If you have any trouble with the examples given above, please use the comments section below and we will see if we can help.
Our latest tutorials delivered straight to your inbox