The following will give you a basic introduction on how to get started with gdb. For more detailed usage reading the gdb documentation is recommended.
The gdb is very convenient in rectification of errors resulting from faulty memory allocations or memory overflow, though its equally convenient for detection of any other errors.
Here I am assuming that the compiler used is gcc. If you have any other compiler you can use that instead of gcc without any change in the commands.
Before we can use gdb we need to compile the source code to be debugged with “-g” option. It passes all the important information regarding the source code to gdb.
To compile :
>gcc -g filename
Now call gdb:
>gdb a.out (this will open the gdb terminal; Note that the program to be debugged is still not running)
In gdb terminal :
To quit gdb terminal:
To run the program:
or >r arg1 arg2 ar3 … (if args need to be given)
or >r < file (If values are to be taken from a file)
Breakpoints are an important feature of gdb. If a breakpoint is set somewhere in the code then the execution stops there until instructed to proceed.
Note: breakpoints need to be set before running the program
To set breakpoint:
>break line_no (set breakpoint at line no given by line_no)
or >break myfunct_name (set breakpoint at myfunct_name)
To continue execution after a breakpoint :
>continue (to continue execution after a breakpoint)
To remove breakpoint:
>delete N (delete breakpoint numbered N)
>delete (delete all breakpoints)
To view code:
>l (it will print the next 10 lines)
To step through code:
[Note: “s” and “step” will result in same thing.]
[Note: “n” and “next” will result in same thing.]
[Note:There is a subtle difference between the “step” and the “next” commands. While “step” will simply take you to the next line, “next” will take you to next statement to be executed. For e.g. If on a “printf” function you give “step” command then it will take you to the next line inside the “printf” function, whereas by doing “next” command the “printf” function (or any other function) is completely executed and it takes you to the next statement to be executed in the source code.]
To finish the execution of current function (important if you step
inside functions like printf, scanf, and so on and want to quickly get out of it):
Setting and monitoring variables:
>print x (print current value of variable x)
>set var x=3 (set value of var x to 3)
>set var x=y (make x=y)
>display x (always show value of x )
>undisplay var_number (its purpose is to not display a variable)(when display command is given a natural number is allotted to the variable and it is visible on the left of variable displayed, var_number refers to that)
To debug memory related failures:
when the program stops at memory/segmentation fault:
>bt (back trace .i.e print the current function stack to show where you are
in the current program. By looking at the function stack the origin of the problem can be found out.)
>up (move to the next frame up)
>down (move to the next frame down)
>return (return from current function)
By- Sumit Kumar
Computer Science student at IIT Kanpur (INDIA).