< Day Day Up > |
Recipe 5.5 Stepping Through Your Code5.5.1 ProblemYour code is stopped at a breakpoint, and you want to execute it line by line, or until it encounters the next breakpoint. 5.5.2 SolutionUse the code-stepping options available via toolbar buttons, menu items, or keyboard shortcuts. 5.5.3 DiscussionThe most basic way to move through paused code is by single-stepping. Eclipse gives you four main options here, corresponding to the four arrow buttons in the Debug view toolbar, beginning with the double-headed arrow and moving to the right (these items also are accessible in the Run menu when you're paused in a debugging session):
In our example, clicking the Step Into button makes the debugger move to the next line of executable code. Because we were paused at the line if(loopIndex == 0) in the previous recipe, and because loopIndex is equal to 3, not 0, the body of the if statement is not executed. Instead, we move to the next iteration of the enclosing for loop, as shown in Figure 5-11.
Figure 5-11. Single-stepping through codeWhen you're stepping through your code, you also can use step filters, which enable you to specify code to avoid (or to filter out) while single-stepping. For example, filtering out the code in a class or package means you won't step through that code when you select Step With Filters. You can set step filters by selecting Window Preferences Java Debug Step Filtering, as shown in Figure 5-12. If you want to filter out code, use the checkboxes next to the predefined filters you see in the figure, or create a new filter by clicking the Add Filter button. Figure 5-12. Specifying step filtersStepping through code line by line is fine when you don't have a lot of code. But if you are debugging a few hundred lines of code, you need something faster. In that case, take a look at Recipe 5.6 on running until a breakpoint is encountered. 5.5.4 See AlsoRecipe 5.3 on starting a debugging session; Recipe 5.4 on setting a breakpoint; Recipe 5.6 on running until hitting a breakpoint; Chapter 3 of Eclipse (O'Reilly). |
< Day Day Up > |