220 likes | 345 Views
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….
E N D
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
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
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
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
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
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
Breakpoints in Netbeans Breakpoint (Ctrl + F8, or click in left margin) SWC
Breakpoints in Netbeans Breakpoint Program is paused here! SWC
Breakpoints in Netbeans See current value of a variable by hovering mouse cursor over it SWC
Breakpoints in Netbeans See current values of all variables, instance fields, parameters, etc. in the ”Variables” debug window SWC
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
Finish Debugger Session • Finishes the debugging ”session” • In other words – stop the program, we are done for now! • Shortcut: Shift + F5 SWC
Pause • Pauses the program… • …how is that different than pausing at a breakpoint?? • Could e.g pause a long iteration • Shortcut: None… SWC
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
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
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
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
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
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
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