Debugging tutorial

Download Report

Transcript Debugging tutorial

Debugging tutorial
Outline
• Brief refresher on debugging using Eclipse
• Logging with log4j
– Logging levels
– log4j.properties
• Debugging strategically
How to Debug in Eclipse
• Breakpoints: By using breakpoints in the
source code you specify where the execution
of the program should stop
• Watchpoints: To stop the execution only if a
field is read or modified, you can specify
watchpoints
Setting Breakpoints
• Right-click in the small left margin in your source
code editor and select Toggle Breakpoint.
• Or you can double-click on this position.
Starting Debugger
Another way to start Debugging
Perspective Change Alert
You may click yes to open Debugging perspective
Debugging Perspective
Call Stack
(may have multiple threads!)
Variable Values
Change values if needed
Debugging Perspective
Control Break points
Controlling the program Flow
•
Program flow can be controlled using
•
F5 - Executes the currently selected line and goes to the next line in your program.
If the selected line is a method call the debugger steps into the associated code.
•
F6 - F6 steps over the call, i.e. it executes a method without stepping into it in the
debugger.
•
F7 - F7 steps out to the caller of the currently executed method. This finishes the
execution of the current method and returns to the caller of this method.
•
F8 - F8 tells the Eclipse debugger to resume the execution of the program code
until is reaches the next breakpoint or watchpoint.
Changing Back to Standard perspective
Useful Links
• http://www.vogella.com/tutorials/EclipseDeb
ugging/article.html
• http://agile.csc.ncsu.edu/SEMaterials/tutorial
s/eclipse-debugger/ - For Beginners
• You can find advanced ways of debugging by
Exploring Eclipse and of course, Google Search
Debugging by More than Println
• The “old school” method of debugging – lots of
System.out.printlns
– When in doubt about the state of variables, print them out!
• This approach even works when you have multiple threads
– println is thread-safe
• Variant: for HTTP servers you often can include debug
messages in the Response!
• But: turning the printlns on and off is a real chore!
– Sometimes you get too much detail, sometimes too little
– What a pain to comment them out!
Java Logging
• Basic ideas of the logging infrastructure:
– Have one or more central logs
– Logging statements originate at a class, and have a
detail level (in increasing order of importance:
TRACE, DEBUG, INFO, WARN, ERROR, FATAL)
• We can adjust the detail levels we want to see
log4j
• See:
http://www.tutorialspoint.com/log4j/log4j_tutorial.pdf
– esp. Chapter 5
– Also see ProducerConsumer sample code we gave!
• Basic aspects in your source code:
import org.apache.log4j.Logger;
static Logger log =
Logger.getLogger(MyClass.class.getName());
log.debug(“Debug message”);
log.info(“Info message”);
Controlling the settings:
log4j.properties in CLASSPATH
# Where to put the log
log = /usr/home/log4j
# Default logging mode
log4j.rootLogger = DEBUG, FILE
# The file appender and the destination logfile
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Layout for file appender (message, newline)
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# More detail for MyClass
log4j.logger.edu.upenn.cis.cis455.hw1.MyClass=INFO
Using log4j Effectively
• Generally a good idea to instrument your
important classes + functions with detailed
logs
• Then adjust the detail level, both for the
rootLogger and for the classes as needed
More on Debugging Strategy
1. Be strategic. Use binary search on bugs:
•
•
See if things worked at the midpoint of execution
Then look at the midpoint of the half where things went wrong
2. Question assumptions. Don’t assume your data is good,
that you don’t have corner cases, etc. Verify, verify!
3. Get 2 threads working before scaling up.
But 1 thread may not be enough (except for correctness
of the core algorithm).
4. For sequencing across threads and in a distributed
setting: sometimes it helps to have a global counter that
gets atomically (synchronized) incremented in each step,
and to append that to everything.
5. Make use of the debugging and logging tools on both the
client (Web browsers have great tools!) and server side.
Happy Debugging!!
Everyone knows that debugging is twice as hard
as writing a program in the first place. So if
you're as clever as you can be when you write it,
how will you ever debug it?
― Brian W. Kernighan