Learn

Saturday, May 18, 2013

GDB Tutorials


Zariga.com  Present


What is GDB?

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
address symInformation about where symbol sym is stored. This is either a memory address or a register name.
all-registersInformation about all registers, including floating-point registers.
argsInformation 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.
catchInformation 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.
displayInformation about items in the automatic display list.
extensionsInformation about the correspondence of filename extensions to source code programming languages.
f [address]Same information as the info frame command.
filesInformation about the current debugging target, including the current executable, core, and symbol files.
floatInformation 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.
handleThe list of all signals and how GDB currently treats them.

AliasShort for ...AliasShort for ...
btbacktraceiinfo
ccontinuellist
contcontinuennext
ddeleteninexti
dirdirectorypprint
disdisablepoprint-object
dodownrrun
eeditsstep
fframesharesharedlibrary
foforward-searchsistepi
gcoregenerate-core-fileuuntil
hhelpwherebacktrace

BreakPoints
awatchSet an expression watchpoint.
breakSet a breakpoint at a line or function.
catchSet a catchpoint to catch an event.
clearClear a given breakpoint.
commandsSpecify commands to run when a breakpoint is reached.
conditionSupply a condition to a particular breakpoint.
deleteDelete one or more breakpoints or auto-display expressions.
disableDisable one or more breakpoints.
enableEnable one or more breakpoints.
hbreakSet a hardware-assisted breakpoint.
ignoreSet the ignore-count of a particular breakpoint.
rbreakSet a breakpoint for all functions matching a regular expression.
rwatchSet a read watchpoint for an expression.
tbreakSet a temporary breakpoint.
tcatchSet a temporary catchpoint.
thbreakSet a temporary hardware-assisted breakpoint.
watchSet an expression watchpoint.

Data Analysis

callCall a function in the program.
delete displayCancel one or more expressions that have been set to display when the program stops.
delete memDelete a memory region.
disable displayDisable one or more expressions that have been set to display when the program stops.
disable memDisable a memory region.
disassembleDisassemble a section of memory.
displayPrint the value of an expression each time the program stops.
enable displayEnable one or more expressions that have been set to display when the program stops.
enable memEnable a memory region.
inspectSame as print.
memDefine attributes for a memory region.
outputSimilar to print, but doesn't save the value in history and doesn't print a newline. For scripting.
printPrint the value of an expression.
print-objectCause an Objective C object to print information about itself.
printfPrint values such as the printf command.
ptypePrint the definition of a given type.
setEvaluate 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