GDB (GNU Debugger)
Gdb is a debugger for C (and C++). It
allows you to do things like run the program up to a certain point then stop
and print out the values of certain variables at that point, or step through
the program one line at a time and print out the values of each variable after
executing each line. It uses a command line interface.
It allows you to inspect what the program is doing at a
certain point during execution. Errors like segmentation faults may be easier
to find with the help of gdb.
Steps:
1.
Compilation:
For
normal compilation without enabling gcc
#gcc
<flags> <source file > -o <output file>
e.g.
#gcc –Wall hello.c :q–o hello
For
compilation with gcc
#gcc
<flags> -g <source file > -o <output file>
e.g.
#gcc –Wall –g hello.c –o hello
NOTE:
Use g++ instead of gcc for c++ programs.
2. Start gdb
#gdb
After
this prompt will appear like this ->
(gdb)
3.
load program:
#
(gdb) file <executable_name>
e.g. # (gdb) file hello
OR
We
can start gdb and run a program in one command
#gdb
hello
4.
Run program
#
(gdb) run
This
will run complete program in one go.
If
program have any issue in running (like segmentation fault), then gdb will
throw some useful information like line number and parameters to function that
caused error.
If your
program has some bugs / issues then you don’t want to run program in one go,
instead you would like to step through program a bit at a time, until you
receive upon error.
Breakpoints:
These
can be used to stop a running program at a designated point. The command is
“break”
Breakpoint
using line number
#
(gdb) break hello.c:13
This
will set breakpoint at line 6 in hello.c , So when control of execution reach
at line 6, it will stop here and wait for another command.
Breakpoint
using function name
# (gdb) break my_function
Once
you have set break points, you can run your program using “run” command. This
time execution should stop at 1st break point (unless a fatal error
occurs before reaching that point)
Conditional
breakpoints:
Use to avoid unnecessary stepping in
program
# (gdb) hello.c:13 if len >=
MAX_LEN
Here, pausing of program will
trigger only if condition is satisfied.
# (gdb) delete <breakpoint-name> Use to delete breakpoint.
# (gdb) info breakpoints
show information about all declared breakpoints.
More useful commands:
#(gdb)
continue Use to proceed from one
break point to next break point.
#(gdb)
step It simply execute next line of
program and wait for further command
#(gdb)
next It work similar to step , but
it doesn’t execute each line of subroutine instead “next” treat it as one instruction
NOTE: Instead of typing “step” or “next”
again and again, you can just press ENTER, gdb will repeat last command fired
Checking value of variables in
program:
#(gdb) print my_variable
It prints value of variable named
“my_varaible” at that moment
You
can also print value of structure elements.
Using watch on
variables
Breakpoints
work on line number or function name, watch act on variable. It pause the
program whenever value of watched variable is modified. Gdb keep watch on
variable within variables’ scope
#
(gdb) watch var
Reference: http://www.gnu.org/software/gdb/
No comments:
Post a Comment