Download presentation
Presentation is loading. Please wait.
Published byAlejandro Ramsbottom Modified over 9 years ago
1
Debugging tutorial
2
Outline Brief refresher on debugging using Eclipse Logging with log4j – Logging levels – log4j.properties Debugging strategically
3
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
4
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.
5
Starting Debugger
6
Another way to start Debugging
7
Perspective Change Alert You may click yes to open Debugging perspective
8
Debugging Perspective Variable Values Call Stack (may have multiple threads!) Change values if needed
9
Debugging Perspective Control Break points
10
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.
11
Changing Back to Standard perspective
12
Useful Links http://www.vogella.com/tutorials/EclipseDeb ugging/article.html http://www.vogella.com/tutorials/EclipseDeb ugging/article.html http://agile.csc.ncsu.edu/SEMaterials/tutorial s/eclipse-debugger/ - For Beginners http://agile.csc.ncsu.edu/SEMaterials/tutorial s/eclipse-debugger/ You can find advanced ways of debugging by Exploring Eclipse and of course, Google Search
13
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!
14
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
15
log4j See: http://www.tutorialspoint.com/log4j/log4j_tutorial.pdf 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”);
16
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
17
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
18
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.
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.