What is debugger?
A special tool (gdb) used to find errors (bugs) in programs. A debugger allows a programmer to stop a program at any point and examine the values of variables, examine the call trace and much more. It allows you to examine what the program is doing at a certain point during execution.
In this tutorial I will cover only basic use of debugger. More advance concept I will cover when it require in subsequent tutorials. Tool gdb is an open source debugger.
Walkthrough with Example
[SIZE="3"]int main()
{
int j = printf(“Hello”);
return 0;
}[/SIZE]
How to create debug version of executable?
we have to provide “–g” option to compiler to create debug version of executable as shown below.
[SIZE="3"]$ gcc – g –o hello hello.c[/SIZE]How to start debugging the executable
[SIZE="3"]$ gdb hello[/SIZE]It displays information about gdb and gdb prompt is appeared to accept gdb commands.
How to set break point
[SIZE="3"](gdb) b hello.c:3[/SIZE]
Above command sets a breakpoint at line 3, of hello.c. Now, if the program ever reaches that location when running, the program will pause and prompt you for another command.
To execute the program type following command,
[SIZE="3"](gdb)run[/SIZE]
Program pauses at line 3 and you can examines type of variables and values by using following code,
[SIZE="3"](gdb) p j $1=5[/SIZE]This displays value of variable j.
[SIZE="3"]$ptype j Type = int[/SIZE]This displays type of variable.
To perform step by step execution type “s” at the debugger prompt.
When program reaches at the end of the program it displays message “Program exited normally”.
Assignment: Delete return 0 from program and debug this program again and send me your observations.


Sign In
Create Account

Back to top









