Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging tutorial. Outline Brief refresher on debugging using Eclipse Logging with log4j – Logging levels – Debugging strategically.

Similar presentations

Presentation on theme: "Debugging tutorial. Outline Brief refresher on debugging using Eclipse Logging with log4j – Logging levels – Debugging strategically."— Presentation transcript:

1 Debugging tutorial

2 Outline Brief refresher on debugging using Eclipse Logging with log4j – Logging levels – 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 ugging/article.html ugging/article.html s/eclipse-debugger/ - For Beginners 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: – 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”);“Info message”);

16 Controlling the settings: 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

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

Download ppt "Debugging tutorial. Outline Brief refresher on debugging using Eclipse Logging with log4j – Logging levels – Debugging strategically."

Similar presentations

Ads by Google