1 / 21

Using a Debugger

Using a Debugger. What is ”debugging”?. An error in a computer program is often called a ”bug”… …so, to ”debug” is to find and get rid of errors in a program Applies to logical errors , syntax errors are handled by the compiler Logical errors can be extremely hard to find….

Download Presentation

Using a Debugger

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using a Debugger

  2. What is ”debugging”? • An error in a computer program is often called a ”bug”… • …so, to ”debug” is to find and get rid of errors in a program • Applies to logical errors, syntax errors are handled by the compiler • Logical errors can be extremely hard to find… SWC

  3. Strategies for debugging • Write error-free programs! Ideal, but practically impossible  • Write ”defensive” code. Do up-front checks for e.g. null references, illegal values, etc.. • Read the code. Many errors can actually be caught just by reading the code • Read the code (again). Many errors can actually be caught just by reading the code (really) • Explain the code to others. Trying to verbalise the code can often reveal errors SWC

  4. Strategies for debugging • Create proper tests. Will help you detect errors and pinpoint their location (if the test is detailed enough…) • Logging/printouts. This is often a beginners approach to debugging. Can work, but tedious • Use a debugger. Only realistic approach in larger programs SWC

  5. What is a ”debugger” • A special program which can ”manage” execution of another program • Pause program execution at a specific point • Start the program from that point again • Let a user run the program one line at a time • Inspect the value of variables • In modern integrated development environments – like NetBeans – the debugger is an integral part of the environment. SWC

  6. Keep it simple… • Modern debugges can do a lot of stuff – you only need to know a little bit to get started • Breakpoints • Variable inspection • Stepping over • Stepping into • Stepping out of SWC

  7. Breakpoints • A breakpoint is set at a line in a program • The breakpoint makes the debugger pause execution of the program at this line • This enables us to see the values of variables at this point in the program! • We run a program in ”debug mode” by pressing Ctrl + F5 SWC

  8. Breakpoints in Netbeans Breakpoint (Ctrl + F8, or click in left margin) SWC

  9. Breakpoints in Netbeans Breakpoint Program is paused here! SWC

  10. Breakpoints in Netbeans See current value of a variable by hovering mouse cursor over it SWC

  11. Breakpoints in Netbeans See current values of all variables, instance fields, parameters, etc. in the ”Variables” debug window SWC

  12. The debugging toolbar • This little toolbar actually contains all we need to know (and use) about the debugger at this point • Various actions to take, now that the program is paused at a breakpoint SWC

  13. Finish Debugger Session • Finishes the debugging ”session” • In other words – stop the program, we are done for now! • Shortcut: Shift + F5 SWC

  14. Pause • Pauses the program… • …how is that different than pausing at a breakpoint?? • Could e.g pause a long iteration • Shortcut: None… SWC

  15. Continue • Continue execution of the program, until the next breakpoint is met • If no more breakpoints, program simply runs to the end • Shortcut: F5 SWC

  16. Step Over • Move one line ahead, staying in the current method (what else?) • Often referred to as ”single-stepping” • Executes all method calls in the line of code • Shortcut: F8 SWC

  17. Step Over Expression • Almost like Step Over, but only executes one of the method calls in the line of code • Invoke again to execute next method call • Useful to analyse complex lines of code, without actually stepping into the called code • Shortcut: Shift + F8 SWC

  18. Step Into • If the line of code contains a method call, the debugger ”steps into” that code • Useful for following the details of a method call • Can not step into library methods… • Shortcut: F7 SWC

  19. Step Out • The debugger returns to the method which called the method we are currently inside • Note that remaining lines of code in the called method are executed • Shortcut: Ctrl + F7 SWC

  20. Run to Cursor • Runs the program to the current position of the mouse cursor • Like an ad-hoc breakpoint • Useful for e.g breaking out of a loop, while staying inside a method • Shortcut: F4 SWC

  21. Debugging in a nutshell • Set breakpoints where needed • Run program in debug mode (Ctrl + F5) • Inspect variable values at breakpoints (mouse hovering, the ”Variables” debug window) • Use the Step… facilities to continue execution from the breakpoint • Rewrite the code as appropriate • Repeat from the top… SWC

More Related