Setting a breakpoint in bytecode - java

As the title says: is it possible to set a breakpoint in classes with no source available? Does any debugger support this? Bonus points for being able to view the stack and local variables.
edit: Sorry for being unclear from start, but I'm not interested in method breakpoints. I want to set a breakpoint inside the method.

If you are using eclipse, you can set breakpoints on the methods in the class using the outline view.
You can set a method breakpoint using the outline view of the class in question. Then the debugger breaks at the first line of the method.(taken from How to put breakpoint in class without having it's source?)
Right click on the method in eclipse outline view and select Toggle Method Breakpoint
P.S. I have tried this and it works for me.

Related

Status Monitor in Eclipse?

I am pretty new at developing larger software and I am using Eclipse as IDE.
When my program is running, I want to have certain informations about the status in different classes (like values of certain objects). Up until now I was just printing it all out on the console with System.out.println(object.value);.
After a while the console became confusing with all the different values printed higgledy-piggledy. Now I am searching for a plugin or something, with which I can do something like
StatusMonitor monitorSize = new StatusMonitor();
StatusMonitor monitorHeight = new StatusMonitor();
monitorSize.print(object.size);
monitorHeight.print(object.height);
And then Eclipse has two different terminals/windows where the specific variables are printed.
Is there a possibility to achieve that?
YOU SHOULD DEBUG IT. FOR THIS ADD BREAKPOINTS IN THE CODE(To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively you can double-click on this position.) WHERE YOU WANT TO CHECK THE VALUES OF VARIABLES. WHEN YOU HAVE PUT THE BREAKPOINTS IN YOUR CODE THEN RIGHT CLICK ON THE CLASS WITH MAIN METHOD THEN SELECT --> DEBUG AS--> JAVA APPLICATION. THEN A DIALOG BOX WLL OPEN CLICK YES ON IT AND NOW YOUR CODE WILL BE OPEN IN DEBUG MODE. ON THE TOP PANEL .There will be options such as STEP INTO ETC. ALSO THERE WILL ARE FEW SHORTCUTS:
F5-->Executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code.
F6-->F6 steps over the call, i.e. it executes a method without stepping into it in the debugger.
F7-->F7 steps out to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.
F8-->F8 tells the Eclipse debugger to resume the execution of the program code until is reaches the next breakpoint or watchpoint.

How can I check if an object has been created/called upon in Java? (Eclipse IDE)

Suppose I was making a game and within the render method which cycles hundreds of times per second I have two lines of code e.g.
Sprite sprite = new Sprite (...);
screen.renderSprite(...);
Which creates an object and renders it onto the screen so it shows. But suppose it didn't show; it would be natural to assume that if there is no error then it just wasn't called.
So in Eclipse how I can check if these two lines of code have been called in line x? If you tell me to use breakpoints tell me step by step because I feel that would only obfuscate the render method. (?) I want to check if the object has been created and utilized.
If you need more information don't hesitate to ask.
11. Sprite sprite = new Sprite (...);
12. screen.renderSprite(...);
Right click on line 12 (in my example but will be different in your code.)
Click on Toggle breakpoint
Click Run > Debug As an then either selct Java Application or an existing launch configuration
Once the code starts and enters screen.renderSprite, the program will halt and you'll be able to check it.
First of all you will have to identify some lines in your code at which you know that your sprite wasn't rendered (maybe a state flag?).
From that you have two options:
If you maintain the source code of method #renderSprite(...) you should consider using a logger (like apache logging which should be already included as a plugin of your Eclipse IDE). Thus your application wouldn't obfuscate your rendering processes.
The other option would be debugging: Set up a breakpoint (double-click left of said lines) and run your application in debug mode. You can also set conditions for the breakpoint to stop only in certain cases. Therefor you'll have to right-click a breakpoint and choose "Breakpoint Properties..." and check the "conditional"-box. In the textfield below you can define your condition in Java (you'll have access to all variables just like in your Editor). For more information see the debug help page of Eclipse or Lars Vogel's tutorial on debugging. By debugging your rendering would be paused, but it should be easier for you to find the concrete source of your problem.
A line with a breakpoint set, whose bytecode has been executed, will have a little checkmark on the breakpoint image.

Is there any way to set breakpoints on all methods of a class?

Is there any way to set breakpoints on all methods of a given class?
I have a huge (2300 lines) legacy class and I need to set breakpoints on all method calls to understand how this mess works.
You can follow the steps below:
Run -> View breakpoints -> Add -> Java Method Breakpoints
Class pattern -> full reference of your class (e.g., mypackage.MyClass)
Method Name -> * (i.e., asterisk wild card)
I have discovered workaround :
1. I have set "Toggle Brakepoint" hotkey to Alt+Numpad 0.
2. After that you can click on first method
3. Use "Toggle Brakepoint"
4. Alt+Down - goto Next Method. ( Alt+Up - goto Previous Method. )
5. Repeat 3 step.
This is similar to Sergey Senkov's answer, but without hotkeys.
In the structure view, click on the first method. Repeat the following for each method:
Context Menu Key
M to toggle the method breakpoint.
Down
There is a plugin for idea:
Simple Toggle All Method Breakpoint.
It allows you to breakpoint all methods / clear all method's breakpoints in one click from context menu on class in Project view.
As Andrey Lavrukhin suggested, there is Simple Toggle All Method Breakpoint, install it through Settings -> Plugins. Works perfectly.
The only way you'll be able to do what you want is to set method breakpoints on each and every method with the class in question. You need to click on the left hand gutter next to the method - a little red circle with 4 dots will appear and you may get a warning saying method level breakpoints can impact performance. You can then further configure the breakpoint (by rightclicking on it, or select shift+F8) and set it so that it breaks on entry, exit or both
I'm afraid theres no way to do this in a single step/setting.

Why open Call Hierarchy on a method is not working

I am using Eclipse IDE 3.6 version .
Inside my code i have a method as
public APIRes execute(APIReq request) throws Exception {
// Some code
}
Could you please tell me , when i did open Call Hierarchy , nothing is shown .
Why is it so ??
The obvious solution is that there may be nothing calling that method - at least within the code that Eclipse knows about. Beyond that, we really can't say...
Maybe the problem is that Open Call Hierarchy was not searching the entire Workspace.
Click on the small down arrow (in the Call Hierarchy view window on the right; it is the "View Menu" arrow -- a triangle pointing down) in Call Hierarchy view, set the Search Scope > Workspace.
Do you mean Call Hierarchy or Class Referencing.
If your create a method and nobody calls it, what do you expect to see in the call hierarchy.
Your method is referencing the class in which the method is placed. This would be shown in the reference view.
first clear the project and refresh it and select the method press right click open call hierarchy you will get the class where method declaration is defined.

Watching variable to see if it changes value

I'm debugging my code to see if the variable is receiving new values during execution.
How can I watch it and make it stops when the value is changed by something?
I've got a CheckList and set the selectedValues with some values, and for some reason, the values selected are disappearing.
Use the outline view (Window -> Show View -> Outline) to select your variable. Right-click it and select Toggle Watchpoint. It will create a breakpoint for your variable. Right-click this breakpoint, select properties and leave only the checkbox "Field modification" enabled.
I think that this cannot be done for variables declared inside methods, although you can use it for class or instance variables.
You could use a breakpoint with a condition for the value.
You could set a breakpoint on the setter method for the value.
If the debugger does appear to help. esp if the problem doesn't appear when you debug it. You can add logging which can show you where a change is made. e.g.
log.info("Changed "+changeDescription, new Throwable("HERE"));

Categories