DebuggingBugsSources of BugsDebugging in Software EngineeringWays NOT to DebugAn Approach to Debugging1. Stabilize the Error1. Stabilizing the Error2. Locate the SourceWhen it’s Tough to Find SourceFinding Error LocationsAlternative to Finding Specific Source3. Fix the DefectFixing the Code4. Check Your Fix5. Look for Similar ErrorsPreventing Bugs Or Finding Difficult OnesDebugging ToolsNon-traditional Debugging ToolsDebuggingCPSC 315 – Programming StudioSpring 2009BugsTerm has been around a long timeMark I – moth in machineMistake made by programmersAlso (and maybe better) called:ErrorsDefectsFaultsSources of BugsBad DesignWrong/incorrect solution to problemFrom system-level to statement-levelInsufficient IsolationChanges in one area affect anotherTyposEntered wrong text, chose wrong variableLater changes/fixes that aren’t completeA change in one area affects anotherDebugging in Software EngineeringProgrammer speed has high correlation to debugging speedBest debuggers can be more than to 10 times as fastFaster finding bugsFind more bugsIntroduce fewer new bugsWays NOT to DebugGuess at what’s causing itDon’t try to understand what’s causing itFix the symptom instead of the causeSpecial case codeBlame it on someone else’s codeOnly after extensive testing/proofBlame it on the compiler/computerYes, it happens, but almost never is this the real causeAn Approach to Debugging1. Stabilize the error2. Locate the source3. Fix the defect4. Test the fix5. Look for similar errorsGoal: Figure out why it occurs and fix it completely1. Stabilize the ErrorFind a simple test case to reliably produce the errorNarrow it to as simple a case as possibleSome errors resist thisFailure to initializePointer problemsTiming issues1. Stabilizing the ErrorConverge on the actual (limited) errorBad: “It crashes when I enter data”Better: “It crashes when I enter data in non-sorted order”Best: “It crashes when I enter something that needs to be first in sorted order”Create hypothesis for causeThen test hypothesis to see if it’s accurate2. Locate the SourceThis is where good code design helpsAgain, hypothesize where things are going wrong in code itselfThen, test to see if there are errors coming in thereSimple test cases make it easier to checkWhen it’s Tough to Find SourceCreate multiple test cases that cause same errorBut, from different “directions”Refine existing test cases to simpler onesTry to find source that encompasses all errorsCould be multiple ones, but less likelyBrainstorm for sources, and keep list to checkTalk to othersTake a breakFinding Error LocationsProcess of eliminationIdentify cases that work/failed hypothesesNarrow the regions of code you need to checkUse unit tests to verify smaller sectionsProcess of expansion:Be suspicious of:areas that previously had errorscode that changed recentlyExpand from suspicious areas of codeAlternative to Finding Specific SourceBrute Force Debugging“Guaranteed” to find bugExamples:Rewrite code from scratchAutomated test suiteFull design/code reviewFully output step-by-step statusDon’t spend more time trying to do a “quick” debug than it would take to brute-force it.3. Fix the DefectMake sure you understand the problemDon’t fix only the symptom (e.g. no magic “subtract one here” fixes)Understand what’s happening in the program, not just the place the error occurredUnderstand interactions and dependenciesSave the original codeBe able to “back out” of changeFixing the CodeChange only code that you have a good reason to changeDon’t just try things till they workMake one change at a time4. Check Your FixAfter making the change, check that it works on test cases that caused errorsThen, make sure it still works on other casesRegression testAdd the error case to the test suite5. Look for Similar ErrorsThere’s a good chance similar errors occurred in other parts of programBefore moving on, think about rest of programSimilar routines, functions, copied codeFix those areas immediatelyPreventing BugsOr Finding Difficult OnesGood DesignSelf-Checking codeOutput optionsPrint statements can be your friend…Debugging ToolsDebuggersOften integratedCan examine state in great detailDon’t use debuggers to do “blind probing” Can be far less productive than thinking harder and adding output statementsUse as “last resort” to identify sources, if you can’t understand another wayNon-traditional Debugging ToolsSource code comparators (diff)Compiler warning messagesExtended syntax/logic checkersProfilersTest
View Full Document