Debugging

advertisement
Debugging
“Why you were up till 2AM”
Dan Fleck
Fall 2007
Debugging
Definition: The process of finding
problems (bugs) in programs and
removing them
Why do we call it debugging?
1947, one of the first computers the Harvard
University Mark II Aiken Relay Calculator
On the 9th of September, 1947, when the
machine was experiencing problems, an
investigation showed that there was a moth
trapped between the points of Relay #70, in
Panel F.
The operators removed the moth and affixed it
to the log. The entry reads: "First actual case
of bug being found." - http://www.jamesshuggins.com
General Steps
Recognize that a bug exists
Isolate the source of the bug
Identify the cause of the bug
Determine a fix for the bug
Apply the fix and test it
Isolating source of the bug
Once found, and understood, most bugs
are easy to fix. We will concentrate only
on finding the bug
Confirm your beliefs
Finding your bug is a process of confirming the many
things you believe are true, until you find one which is
not true
 You believe that at a certain point in your source file, a
certain variable has a certain value
 You believe that in a given if-then-else statement, the
“else” part is the one that is executed
 You believe that when you call a certain function, the
function receives its parameters correctly
Confirm your beliefs
So the processing of finding the location of a bug
consists of confirming all these things!
Check everything!
Okay… How?
One way
Put lots of print statements in at each
point that says “I am in method X with
parameters set to Y,Z”
Note: If you do this, be able to turn them
on/off easily!
 Boolean DEBUG = true;
 If (DEBUG) System.out.println…..
Often a better way
Use the debugger
 Set a breakpoint at a point in the code
 Step through the code
 Look at the variables (local and class
variables) to see ones that are not as you
expected
The debugger is usually better/faster… but print statements
are also easy to turn on/off as a whole to make a DEBUG
mode for your program. Which is often useful.
The Most Important Thing
To do ANY debugging you must
understand what the program should do
What each method should do
What each if statement should do
If your beliefs are wrong, confirming
your beliefs will not help!
The Most Important Thing
Add beliefs into your code… beliefs are
otherwise known as COMMENTS!
Lets work some examples
See Netbeans debugging project
Fibonacci - Compile time problems
DebugMe - Use the debugger to find the
problems
WordAnalyzer  Test 1 - Use print statements
 Test 2 - Use print statements
 Test 3 - Use the debugger
My Presidents aren’t sorted by party
What are your beliefs? Or more
specifically, what are some hypothesis
that you should confirm?
 You’re not calling the sort routine at all
My Presidents aren’t sorted by party
What are your beliefs? Or more
specifically, what are some hypothesis
that you should confirm?





You’re not calling the sort routine at all
The sort is not “seeing” every president
The sort is sorting by something other than party
The array is not getting updated by the sort
The array is getting sorted, then resorted by
something else
My Presidents aren’t sorted by party
Confirming your beliefs
 You’re not calling the sort routine at all:
 Confirm: put in a print statement inside the sort routine
saying “DEBUG: sorting begun”
 The sort is not “seeing” every president
 Confirm: put in a print statement to print out every
president the sort loop “sees”
 The sort is sorting by something other than party
 Write code that says “sorted president (Reagan,
Rebulican) after (Clinton, Democrats)”
When I print I see duplicate Presidents
During the sort I don’t add any new data
 Confirm: Check the length of the data
before/after the sort
During the print, I only print each value
once
 Confirm: compare the length of the list to
the number of times you issue a print
statement
 assert (length == countPrints) : “Too many ”+length+”:”+countPrints;
Suduko cannot find a solution
 Confirm there is a solution
 Confirm you’re reading the values in correctly from the
grid
 Confirm you’re able to access the correct value using
index -> row, col conversion
 Confirm your List can move both forward and
backward correctly
 Confirm you are setting / clearing BitSets
appropriately (at various points print out the BitSets for
a row/col/box and the grid and compare
Suduko cannot find a solution
 Confirm you’re able to find the “next” value correctly
 Confirm you’re updating the SudokuList appropriately
….
COMMENT YOUR CODE!
COMMENT!
COMMENT!COMMENT!
COMMENT!
COMMENT!COMMENT! COMMENT! COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT! COMMENT!
COMMENT!COMMENT! COMMENT! COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT! COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!COMMENT!COMMENT!
COMMENT!
COMMENT! COMMENT!
Whoa there… I can’t even compile!
 First, try never to get here. Compile early,
compile often. If you can’t compile at any point
STOP… fix the code before adding any new
code.
 Review the compiler error, read it carefully
 Check your book and online resources for
examples and see what may be wrong
 Ask questions on WebCT, to your TA or
Professor.
The Most Important Thing (again)
Add beliefs into your code… beliefs are
otherwise known as COMMENTS!
Or if you can… java assertions!
Java Assertions
assert Expression1 : Expression2 ;
 If Expression 1 is not true, throw an
error with String in Expression2
 assert x<4 : “Invalid x value ”+x ;
 Assertions can be enabled during
debugging and disabled for production
use (increasing performance)
 java -ea edu.gmu.MyClass // enable
 java -da edu.gmu.MyClass // disable
More info: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
References
 http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html#tth_sEc
2
 http://en.wikipedia.org/wiki/Debugging
Download