GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
Start your program, specifying anything that might affect its behavior.
Make your program stop on specified conditions.
Examine what has happened, when your program has stopped.
Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another
gdb is in the gnu package on CEC machines. If you don't have this package loaded then type pkgadd gnu at a shell prompt. If you can run g++, then you will be able to run gdb. gdb can only use debugging symbols that are generated by g++. For Sun CC users, there is the dbx debugger which is very similar to gdb. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With g++, this is accomplished using the -g command line argument. For even more information, the -ggdb switch can be used which includes debugging symbols which are specific to gdb. The makefile for this tutorial uses the -ggdb switch
Summary of the info Command
info ...
Information displayed
addresssym
Information about where symbol sym is stored. This is either a memory address or a register name.
all-registers
Information about all registers, including floating-point registers.
args
Information about the arguments to the current function (stack frame).
break [bpnum]
Information about breakpoint bpnum if given, or about all breakpoints if not.
breakpoints[bpnum]
Same information as the info break command.
catch
Information on exception handlers active in the current frame.
classes[regexp]
Information about Objective-C classes that matchregexp, or about all classes if regexp is not given.
display
Information about items in the automatic display list.
extensions
Information about the correspondence of filename extensions to source code programming languages.
f [address]
Same information as the info frame command.
files
Information about the current debugging target, including the current executable, core, and symbol files.
float
Information about the floating-point hardware.
frame[address]
With no argument, print information about the current frame. With an address, print information about the frame containing address, but do not make it the current frame.
functions[regexp]
With no argument, print the names and types of all functions. Otherwise, print information about functions whose names match regexp.
handle
The list of all signals and how GDB currently treats them.
Alias
Short for ...
Alias
Short for ...
bt
backtrace
i
info
c
continue
l
list
cont
continue
n
next
d
delete
ni
nexti
dir
directory
p
print
dis
disable
po
print-object
do
down
r
run
e
edit
s
step
f
frame
share
sharedlibrary
fo
forward-search
si
stepi
gcore
generate-core-file
u
until
h
help
where
backtrace
BreakPoints
awatch
Set an expression watchpoint.
break
Set a breakpoint at a line or function.
catch
Set a catchpoint to catch an event.
clear
Clear a given breakpoint.
commands
Specify commands to run when a breakpoint is reached.
condition
Supply a condition to a particular breakpoint.
delete
Delete one or more breakpoints or auto-display expressions.
disable
Disable one or more breakpoints.
enable
Enable one or more breakpoints.
hbreak
Set a hardware-assisted breakpoint.
ignore
Set the ignore-count of a particular breakpoint.
rbreak
Set a breakpoint for all functions matching a regular expression.
rwatch
Set a read watchpoint for an expression.
tbreak
Set a temporary breakpoint.
tcatch
Set a temporary catchpoint.
thbreak
Set a temporary hardware-assisted breakpoint.
watch
Set an expression watchpoint.
Data Analysis
call
Call a function in the program.
delete display
Cancel one or more expressions that have been set to display when the program stops.
delete mem
Delete a memory region.
disable display
Disable one or more expressions that have been set to display when the program stops.
disable mem
Disable a memory region.
disassemble
Disassemble a section of memory.
display
Print the value of an expression each time the program stops.
enable display
Enable one or more expressions that have been set to display when the program stops.
enable mem
Enable a memory region.
inspect
Same as print.
mem
Define attributes for a memory region.
output
Similar to print, but doesn't save the value in history and doesn't print a newline. For scripting.
print
Print the value of an expression.
print-object
Cause an Objective C object to print information about itself.
printf
Print values such as the printf command.
ptype
Print the definition of a given type.
set
Evaluate an expression and save the result in a program variable.
GDB Tutorials # 1
So you now have an executable file (in this case main) and you want to debug it. First you must launch the debugger. The debugger is called gdb and you can tell it which file to debug at the shell prompt. So to debug main we want to type gdb main. Here is what it looks like when I run it:
GDB Tutorials # 2
Inspecting crashes
So already we can see the that the program was at line 28 of main.cc, that this points to 0, and we can see the line of code that was executed. But we also want to know who called this method and we would like to be able to examine values in the calling methods. So at the gdb prompt, we type backtrace which gives me the following output:
No comments:
Post a Comment