Debugging in Eclipse

Shubham Dadasaheb Sawant
7 min readJan 27, 2022

Bug : When error is found in the set of instructions it is called Bug
Debug: Process of finding that bug is called debugging

How to do debugging in Eclipse

Consider the code written in java in Eclipse IDE

we generally write our code and press run then all lines of code get executed and at one time we get an output. But what we need to just execute first 7 lines of code, then the concept of debugger comes. so now if we need to execute only first 7 lines of code from screen then we need to add breakpoint. Initially we will be working with only one breakpoint.

So what is a breakpoint. Breakpoint is the point where your execution of code stops and till that point output is shown and it farther programmers says to continue to next line the execution is done to next line and so on

How to create a break point . Continuing with our example we need to execute first 7 lines of code so we will be creating breakpoint at line no 7. To create breakpoint just move your cursor at line no 7 on blue line at double click your mouse for eg

This created your breakpoint. Now earlier when we were running programs over eclipse we just used to click on run and entire program lines were executed and output for that lines was shown. But now after adding breakpoints we will run our program in different way

Just Go to Run menu at top -> Debug-> it will show a dialogue box where asking for entering into debugging view perspective. Earlier when we used to run programs we used to run in java view perspective so now we will be running program as we applied breakpoint in debug view. Enter Switch on dialogue box. And a new screen with multiple panels will appear as shown below.

1st column of screen: This gives info about threads running at different lines of code.
2nd column of screen: This contains your entire code.
3rd column of screen: in our case will execute all lines before 7 as breakpoint is at 7.
4th column of screen: Gives info about variables and their values at current execution.

So till now everything before 7 lines is executed at its output is shown at console screen and the values of variable is also shown in variables screen.

Now all lines before breakpoints are executed now programmer either can stop the debugging or if you want to execute next that is 7th line you will need to click on step over (F6/ fn+f6) at top toolbar menu. For eg

clicked on f6 you will see below:

again click on f6 and you will see below:

In the same way clicking on step over keeps on executing next line from current line.
Also note for info line no 9 contains some complex expression and if you want to know the value of that expression then you can select that entire expression -> right click ->inspect

Initially we applied breakpoint at line 7 then all lines before 7 got executed and we at 4th column we know the value of a = 27 but what if you need to execute line no 9 with a = 27 what will be its value?

Use of inspect tool while debugging

Note: The way you use inspect it will produce o/p the same way

Shortcuts till now:
Debug: f11
Next line: f6
finish execution: f8
ctrl+shift+I: Inspect
ctrl+shift+B: remove breakpoint(To remove breakpoint we also double click the point)

Working with one breakpoint for following code

Q1. If breakpoint is applied at line no 11 what would be in 3rd column that console screen and 4th column ie variable screen.
→ Here if breakpoint is applied at line no 11 first 10 lines get executed but also at line 10 there a call to a function show so this function is entirely executed i.e show() function also gets executed. so console screen will also contains show() function statements that are needed to print over screen.
and there is not variable x shown on variable screen with its content. Because it is out of scope as after execution control comes to breakout line.
for eg see below output.

How to do step by step debugging to get info about show() variables methods or statement.

You can achieve it by applying breakpoint at line no 10 then all lines upto 10 excluding 10 gets executed and control will be at line no 10 then at line 10 you will get show() method if here you perform step over operation(f6) then show() gets executed and you cant achieve line by line execution so when you are at line no 10 to perform step by step execution you should press (f5/fn+f5) this takes control at line no 24 and now to execute line no 24,25,26,27 step by step to watch variables and its content you can click f6(step over operation) and at last when you are line no 28 its time to come back to line 11 to do this click (f7/fn+f7) and control is at line no 11

For ref of above question images are:

  1. Breakpoint at line no 7 so all lines before 7 gets executed except 7 .After executing first 6 lines control is at line no 7

2. do one step over(f6): Line 7 gets executed control moves to next line

3. do one step over(f6):line 8 gets executed, look changes at console n variables screen. control moves to next line that is 10 as line 9 is has no instructions to execute

4. We are at line 10 now. if we perform step over entire show() is executed that means control goes to show() executes everything in it and returns back to line 11. But we need to perform step by step execution so ,we are at line 10 please note now we need to go from line 10 to line 24 as we will perform step by step operation so at line 10 click on f5 or fn+f5 to move control inside function show() at line 24. below image for ref.

5. Now to execute next line we will use f6 and then line 24 gets executed and control moves to next line. Look at console screen it is updated.

6. Also execute next line ie line 25 and look at variable screen it gets updated.

7.Execute last line in the same way by clicking f6 and now note to go back to line 11 click f7/fn+f7

Control back at line 11 as we used f7 at line 28.Also note a variable is visible now as we are at this scope now.Earlier b was visible as control was in that scope method

Shortcut till now:
Debug: f11
Next line: f6
finish execution: f8
Inspect: ctrl+shift+I
Remove breakpoint: ctrl+shift+B
Go inside methods first line: f5
Come back to next line of method call (for eg line 11) : f7

Working with multiple breakpoints:

In above screenshot you will start debugging by clicking f11 →
upto line 7 gets execute then if you want to directly execute till next breakpoint you will click f8 . so now your control is at line 7 breakpoint the next nearest breakpoint from this line is __ ? Not 13 , the nearest breakpoint is 27 as execution control comes to this breakpoint from show() method call and this is nearest breakpoint. After line 27 the nearest breakpoint is 13 then after 13 the nearest breakpoint is 17 and then 21.

Note: If you need to check how many breakpoints are their in your code go to
breakpoints screen on extreme right or if it is not visible search breakpoints from search option

You will see the breakpoints of program named debugging has 5 breakpoints.

Happy Coding :)

--

--