Follow
Publications: 35 | Followers: 0

gdbInEmacs_DebugWorkshop

Publish on Category: Birds 0

Gnu Debugger (gdb)
Debuggers are used to:Find semantic errorsLocatesegfaults and bus errors
Prepared by Dr. Spiegel
UsingGDB
When to use a debugger?Sometimes you can figure out errors just by usingcout(print statements)Incorrect outputUnexpected executionsDebuggers permit fine-tuned controlAn absolute must for finding subtle and more complex errorsDebuggers quickly provide the location of run-time errors
Using GDB
Basic Functions of a Debugger:Run Program & Enter/Exit Debug ModeIn Debug Mode:ControlExecutionWatchThingsThe bestoption is usually to rungdbinsideemacs
Using GDB
First step: Compile the program with flag for debuggingFlag: -gInstructs the compiler to retain user’s codeOtherwise, resulting machine code bears noresemblenceto original codeNote use of –g inmakefile(example in next slide)Inmakefile, -g employed easily via macro
Array Debug Example’sMakefile
DebugFlag=-gdebug:Array.oArrayDebug.og++ -o debugArray.oArrayDebug.o$(DebugFlag)ArrayDebug.o: ArrayDebug.cppArray.hg++ -c ArrayDebug.cpp $(DebugFlag)Array.o: Array.cppArray.hg++ -c Array.cpp $(DebugFlag)
Macro (constdeclaration)
If –g is removed from macro, $(DebugFlag) is replaced by nothing
Replaces
Starting GDB
RungdbinsideemacsProvides dual window environmentTop window: Command environmentBottom Window: Code Being DebuggedBuild UsingmakeStartemacsESC-x (Display at bottom: M-x)gdb<Enter> <Enter>You will be in the debugging environmentThere will be a single window at this time
Run Program & Enter/Exit Debug Mode
BreakpointsDesignate a location where execution is suspended and debug mode enteredCommand:break <argument>Three possibilities for <argument>line numberfunction namePC addressNote: Underlined character(s) in command are shortcuts
Run Program & Enter/Exit Debug Mode
Break Command Argumentsline numberUse<file name>:<line number> in other filesExample:bArray.cpp:121Can appear alone in application file(some versions ofgdbonly)function nameCan appear alone in application fileUse <class name>::<function name> in other filesExample:b Array::~ArrayPC addressPreface address with *More commonly used with assembler codeNote: Tab completion for setting breakpoints is available
Run Program & Enter/Exit Debug Mode
Set up breakpoints before starting the programRun the programCommand:run <cmdline argument(s)>program will run until it hits a breakpointResume execution:Command:continueYou can also userun to restart a currently running programifyou want to go back to the beginning
Run Program & Enter/Exit Debug Mode
When a breakpoint is encountered:Execution stopsThe screen will splitNew window opens showing current file with arrow (=>) to current linethis line hasn’t actually been executed yetProgram is in debug modeUse debugger commandsControlWatchRemoving BreakpointsOnce a breakpoint’s usefulness has ended it may be removedCommand:delete <breakpoint number>No argument willcause promptto delete all breakpointsBreakpoint number is by order breakpoints were establishedgiven when created or when reached during execution
Control Execution
Run one line at a timeCommands:stepnextThe difference between step and next iswhenthe current statementisa functioncallnextexecutes the functionIf function has breakpoint, it will stop there and re-enter debug modestepenters the function to debug itStops at first line to await nextcommand
Control Execution
Othercommands:finishResume execution until end of current function or a breakpoint is encounteredup<# frames>Go up the number of functions indicated in the stackI the argument is 1, goes to the line where the current function was calleddown<# frames>Opposite of up
Control Execution
Entering a functionWhen a function is entered,gdbdisplays information about this callName of functionParameters, including valuesPitfall: Entering a libraryfunctione.g. The stream insertionoperatorThe window footer gives file name and line numberDO NOT try to debug in hereUsefinto exit back to where you entered
Watching Stuff
View variable and test functionsCommands:printdisplay(no shortcut key)printdisplays value of its argumentargument can be quite intricatearray : shows address; you can supply subscriptobject: will try to provide value of all membersif item is address, * can be used to dereferenceargument can be function call!!function will be executedif function alters program data, alteration sticksdisplayis a persistent printshows argument value after each command when argument is in scope
Finding Causes of Crashes
Run-time Errors’ Location(s) are not Reported in UnixMust usegdbto find the location and examine program state at time of crashUsually, the state at the time of crash is preservedIf not, once location is determined, set breakpoint before line of crash to examine variables,etc;Procedure
Determine Location of Crash
Steps to find location:Start debuggerRun program using same inputNo breakpoints; just let it crashUsewherecommand to show run-time stackdisplays sequence of function calls to arrive at currentlocationEach function’s call in the stack is numberedFind the 1stfunction in the list that you wrote. Note the numberXThe first several functions may be library functionsIssue commandup<X>Screen will split and display line where crash occurred (=> denotes)Useprintordisplayto examine variables for irregularities.
Resources
Quick Primer by Dr. SpiegelComplete Manual - Delore.comGDB Cheat SheetYoLinuxCommand Cheat Sheet

0

Embed

Share

Upload

Make amazing presentation for free
gdbInEmacs_DebugWorkshop