Thursday, April 5, 2012

Debugging in Eclipse

Debugging can be an arduous task. Sometimes it takes an incredible amount of patience and care to ensure that you step over the right stuff, step into the right stuff, and not accidentally miss the whole block of code you were trying to debug. Sure, there is Eclipse's Drop to Frame functionality - which if you accidentally skip over something, can really get you out of a bind. However, if the code manipulated some object state, 'drop to frame' will not be able to reverse those changes, and you will be stuck re-debugging with potentially invalid data/state, or having to go back and re-set up your entire debugging session (which may take a good bit of prep time).
One case I often run into when debugging that makes me careless and often causes me to step-over when I mean to step-in, or step-return when I mean to step-over is hitting a loop inside a method that I don't particularly care about watching execute. If the loop will be iterated over a significant amount of times (10+) it isn't worth my time to keep hitting the step-over button until it's done.
So, what's my first instinct? Well, it used to be to put a breakpoint after the loop somewhere, and hit the resume button.


This causes the debugger to resume execution until it hits the next breakpoint, at which time it will break again. This works, but it is cumbersome, and leaves breakpoints hanging around unnecessarily. Eclipse has an alternative. The run-to-line feature allows you to select a line for it to execute up to.
When in the Debug perspective in Eclipse (and debugging) simply highlight the line you want to run to with your caret in the editor:


and then hit the keystroke Ctrl+R/
Hands at home row. That's what an old typing teacher of mine used to constantly say. Try to keep your hands at home row, and over time you'll be the most efficient typist with a standard QWERTY keyboard that you can be. Over the years of software development and writing articles (like this one), I have learned to type fairly quickly. So much so that typing feels like the most effective form of operation for me with a computer. By keeping my hands on the keyboard (and avoiding a mouse), I am able to easily navigate, get some typing done, and then navigate some more, and I do so much faster than I would if I had to transition back and forth between a mouse and the keyboard. I use the windows key, the arrow keys, and the tab/alt-tab navigation patterns to get around most programs, and I try to keep my mouse usage to a minimum unless I'm using a web-browser, or fiddling with images in a graphics program. Along that line, I often find myself learning and using hot-key commands (or mnemonics) in programs whenever possible. I have discussed some of Eclipse's hot-key commands (or mnemonics) in the past, such as the quick outline ( Ctrl+O ) and quick type hierarchy ( Ctrl+T ) . Covering all hot-keys would seem tedious and would probably bore my readers. After all, Eclipse is a big program. It has a *lot* of key commands.
The management (and perusal) of all hot-keys in Eclipse can always be done in the old fashioned way, which is to say you go to Window->Preferences->General[+]->Keys , where you can see as well as change all hot-keys. However, Eclipse 3.0+ has a hot-key to rule all hot-keys. Assuming you didn't change it, pressing Ctrl+Shift+L (don't ask me why they chose L) will open up another quick-like dialog in the lower right-hand corner of the workbench. This dialog has all of the hot-key commands, and their current key-mapping in Eclipse.
You may also notice from the above screenshot that pressing Ctrl+Shift+L again will take you directly to the hot-key preferences page. Next time you need to find a quick-key, try to remember the only one that is truly important to remember - Ctrl+Shift+L .

Stepping through the execution of a Java program

When a thread is suspended, the step controls can be used to step through the execution of the program line-by-line. If a breakpoint is encountered while performing a step operation,  the execution will suspend at the breakpoint and the step operation is ended.

Step over

  1. Select a stack frame in the Debug View. The current line of execution in that stack frame is highlighted in the editor in the Debug Perspective.
  2. Click the Step Over button [ Step Over ] in the view toolbar, or press the F6 key. The currently-selected line is executed and suspends on the next executable line.

Step into

  1. Select a stack frame in the Debug View. The current line of execution in the selected frame is highlighted in the editor in the Debug Perspective.
  2. Click the Step Into button [ Step Into ] in the view toolbar, or press the F5 key. The next expression on the currently-selected line to be executed is invoked, and execution suspends at the next executable line in the method that is invoked.

Step into Selection

  1. Select a stack frame in the Debug View. The current line of execution in the selected frame is highlighted in the editor in the Debug Perspective.
  2. In the Java Editor, within the current line of execution, place the cursor on the name of a method that you would like to step into.
  3. Click the Step into Selection action in the Run menu or Java editor context menu, or press the Ctrl-F5 key. Execution resumes until the selected method is invoked.

Step with filters

  1. Toggle the Use Step Filters button [ Use Step Filters in the Debug view toolbar, or use Shift+F5. When the action is toggled on, each of the step actions (over, into, return) will apply the set of step filters which are defined in the Opens the Step Filtering preference page Java > Debug > Step Filtering preference page. When a step action is invoked, stepping will continue until an unfiltered location is reached or a breakpoint is encountered.

Step Return

  1. Select a stack frame in the Debug View. The current line of execution in the selected frame is highlighted in the editor in the Debug Perspective.
  2. Click the Step Return button [ Step Return ] in the view toolbar or press the F7 key. Execution resumes until the next return statement in the current method is executed, and execution suspends on the next executable line.

Run to line

When a thread is suspended, it is possible to resume execution until a specified line is executed. This is a convenient way to suspend execution at a line without setting a breakpoint.
  1. Place your cursor on the line at which you want the program to run.
  2. Select the Run to Line command [ Run To Line ] from the pop-up menu or use Ctrl+R. Program execution is resumed and suspends just before the specified line is to be executed.
It is possible that the line will never be hit and that the program will not suspend. 
Breakpoints and exceptions can cause the thread to suspend before reaching the specified line.

No comments:

Post a Comment