Debugger in Eclipse
The debugger of Eclipse is a very productive tool. It allows you to see the values of the variables in real time. Additionally, you will be able to see and control exactly what your app is doing while it is running.
If we want to start our app in debug mode, we have to run it using the debug button.
To set or delete breakpoints we can right-click on the space left to the source code and select "Toggle Breakpoint". You will notice a blue dot next to the line. When the app is running, it will stop executing the code at the position of the breakpoint. The line with the breakpoint will not be executed.
When you run the app now, it will show a dialog which says that it is waiting for the debugger to attach.
You will also see a pop-up asking you to launch the debug perspective. Hit "Yes".
The code of your app will be executed, but it will stop before executing the line with your breakpoint. The current line which is not executed yet will be marked green.
Now you can decide what should be done next. The proper controls can be found in the debug perspective we opened before.
The one on the left will run the code until the next breakpoint will be reached. This will be the next thing we see if we define another breakpoint and press the "Resume" button:
The red button will disconnect the debugger. If you do this, your app will go on running as if no debugger had ever been attached.
Let us talk about the other buttons. The left one will make your program execute the current line and if it is a method, it will step into this method and you will be able to debug the other method step by step, too. It is called "Step into". The debugger will not be able to step into methods which are not defined by you or any library you added to your project.
If you want the debugger to execute the next line but (if the line is a method) not to step into the method, use the second button called "Step over". It will directly go to the next line.
If you are in a method and want to step out of it, use the third button. That means that it will leave the current method. Note that it will still run all code of the current method. Its name is "Step return".
In the picture above the buttons would cause this:
- The "Step into" button will continue with the first line of the doSomething() method.
- The "Step over" button will run the doSomething() and pause afterwards.
- The "Step return" method will run the onCreate() method until its end and will pause then.
Now let us see how we can get the value of variables.
All variables used in the current method will be shown in the debugger view.
If there is an array, you can view its childs by clicking on the arrow next to it.
You can also view the fields of an object by clicking on the arrow.
If you hover of a variable in the code view, you will see its value, too.
I think that now you see why I call the debugger a productive tool.
And if I had shown you everything you can do with it, it would have been to long to post it on XDA. The ones I showed you are just the basic functions of the debugger.