Use gdb for debugging your program

For a not so friendly os like unix, gdb is quite user friendly debugger. You can use gdb to locate bugs in your program, trace variables, find out how your recursive functions work etc.

First step would be compilation of your program with -g flag. -g flag will enable gdb to used with the program
gcc -g myfile.c -o myfile
Next you run gdb on the executable program, in this case myfile.
gdb myfile

Some of the important commands to be used within gdb are
commandUsageDescription
runrun
run par
r
execute the program
till end or
till first break point 
breakbreak 3
break myfunction
Set a break point at the given
line or function
stepstep
step 2
Execute one statement
or n statements if arg is given
listlist
list n
list function
list file:function
List the source program
List around the given line num
List the function (of the given file)
printprint a
print num+3
print/x num
Print value of expression or var
with format if specified (last one prints
num in hex)
display display num
display a+b
Print the value of expression each time
program stops
backtracebacktrace
bt
Prints backtrace of all the stack frames (and thus function
calls)
frame frame 1
frame 0
Display content of nth frame
quitquit
q
Quit gdb
helphelp
help list
help running
help stack
Print help of a command or a category
continuecontinueContinue till next break point or end
finishfinishExecute till the current function ends
clearclear
clear 8
Clear the break point at current line or line specified

Now look at this screen shots to understand the usage

Comments

Popular Posts