Eclipse changes variable on its own during debugging - java

I've come across what seems to be a bug in Eclipse Kepler while debugging. One of my variables is incrementing itself randomly with every step of the debugger, even through steps that do not change the variable at all. Screenshots included below:
The method advanceLine() increments progress by one
The very next step, progress increments twice, before it even reaches the increment
There is another thread that accesses progress, but it does not modify it. These random changes don't seem to affect my program at all, just the debug view. Also, for some reason, the shortcut to Step-Into (F5) doesn't work despite being already bound. Does anybody know what's going on?

Variable progress is a class variable and if it is getting updated ambiguosly, it is possible that another thread is modifying your variable. Please make your variable local.
There is no way eclipse interferes your coding logic unless you explicitly right click on a variable and change its value.

Well.. I figured it out. I removed the variable watch from the Expressions view and added it back in. Lo and behold, it displays normally and my code has not altered at all. I think it was just the view that was acting wonky because like #Vineet says, Eclipse does not modify values in your code unless you tell it to.

Related

Eclipse - how to debug input parameter change

I have the following problem, we might even call it a classic one:
public void myMethod(Map<Object, Object> parameter){
someOtherObject.method(parameter);
.
.
.
someOtherThirdPartyObject.method(parameter);
}
And suddenly, in the end some method touched the input parameter Map, and I don't know where and how. Now, I know it would be desirable to make the parameter immutable, but it is not and that is the root of the problem. For instance, the methods inside myMethod are intended to perform some validations, but they do some more as well, which is wrong by design.
So, the question is how to create a breakpoint in this method where the execution pauses if an attribute of this parameter Map changes? It might be a good idea to put a conditional breakpoint after each method call, but if you have 20-odd methods, it's rather painful.
How can I debug when this input parameter is changing?
What you want appears to be called a "watchpoint". I actually didn't know this functionality existed and I used to work on the Eclipse Project!
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_watch.htm
It looks like you'll have to figure out what fields are being editted and then set a "Write" watchpoint using the help document above.
Additionally, Eclipse highlights variables which are modified, so if you step over your method calls one by one you will be able to see which one is modifying the value (and which field is being modified) because it will be highlighted (bright yellow, by default) in the "variables" tab in the "debug" perspective. Once you know which method if modifying the data you can run debug again, but this time debug the method that changes the value and just keep repeating until you find the problem.
This is a classic problem solving scenario where you start with a very large search space and systematically and methodologically narrow it down until the search space is small enough for you to locate the problem.
If you're trying to locate a spot where your map is being modified incorrectly, you might want to first start at the higher levels of the myMethod. Put breakpoints around the methods called inside the myMethod method. At each breakpoint, look at the contents of the Map. Eclipse has a variable watch panel where you can see the contents of every variable at a specific moment in time.
When you hit the breakpoint where you notice something is wrong. Stop. You now know to dig into someOtherObject.method(parameter); assuming the data was changed at it's breakpoint.
Now, someotherObject.method will likely have other methods inside it. Put your breakpoints inside this method around all of it's function calls and repeat the process. Continue repeating until there are no more methods left. Eventually, you will narrow down the problem and have the answer.
Unfortunately, there is no magic "fix my code" button for these types of problems. It just takes good, old fashioned Sherlock Holmes style investigative skills and reasoning to eliminate areas of the code that you know aren't the problem until you're left with a smaller section that allows you to get at the root cause.
If no code modification is allowed, you can
use the watchpoints method described by acattle to watch changes at this specific map instance or
have breakpoints in the Map methods modifying its state (if you want to do that for multiple instances). It does not matter that the Map code is binary only, you can still open it using Ctrl-Shift-T (Open Type), select the methods like put(...) or remove(...) in the outline view and add breakpoints using the context menu in the outline view.

Changing object fields on the fly while debugging in Eclipse [duplicate]

Using Eclipse, when debugging is it possible to change the value of variables during runtime of a project for testing purposes.
For example, say I have a method that returns the number 5 but for testing purposes i want to output 10 instead. This isn't the problem I'm facing its a little more complex but its just to get my idea across.
You should be able to set a break-point, go into debug mode, open the variables views and here change the content of the variables.
You can access variables through the Variables view. There you can right click on any variable and select "Change value ...".
Resources :
standford.edu - eclipse guide
help.eclipse.org - change var value
... and you can do much, much more:-) Just to give you and idea.
You may change the code during debug which is hot swapped and is effectively changed (recompiled) in given debug session.
You may run given method run (e.g. after catching breakpoint) few times without rerunning debug -> use drop to frame feature on method stack.
After you have changed the code you have to save it (cntrl-S) to make it effective.
You will see your running application respond to the code-change after the cntrl-S
I hope this works for you. it took me some time to figure this out.
Run your application in debug mode then go to variables window. select the parameter then change values according to your requirements. then save (ctrl+s). and go ahead with your changes. Hope this will help.
If variables window is missing. then goto eclipse window->show views->variables

Eclipse java breakpoints - What is the purpose?

I'm working with the Android tutorial and I just got to the debugging section and I'm wondering what the purpose of a Breakpoint is. I can't tell just yet... is it actually stopping the app so I can be sure it runs up until that point, or can I set multiple breakpoints and use them as markers to "stop and go" from breakpoint to breakpoint checking my code?
A breakpoint is a place where the execution stops, and you can start inspecting the current situation in your debugger. This includes:
the point has actually been reached
the current values of all variables
the ability to change manually all variables
the current stacktrace - i.e. which methods were executed before the current one
the ability to add and execute arbitrary code
the ability to inspect the results of a method invocation, while not actually proceeding with the execution
In addition to that, you can manually step forward, line by line in your application. There are three options:
step into - enters a method which is invoked in the current line
step over - goes to the next line
step return - returns from the current method (to the method that invoked it)
You can set multiple breakpoints if you have multiple places where you want to do any of the above.
Generally speaking, a debugger is a very upgraded version of using System.out.println(..) or log.debug(..) all over the place in order to make sure certain conditions are present. (thanks to BalusC for this point)
You can definitely set multiple breakpoints. The questions answered by the breakpoint (alongside all of Eclipse's other debug tooling) include not only "did it get here" but also "how did it get here" (the stack trace) and "with what values" (you can observe the variables while the code is paused).

Moving the instruction pointer while debugging Java in Eclipse

Can I move the instruction pointer directly to a line of my choice (within the current method) while debugging a Java program in Eclipse (Galileo)?
It's straightforward to drag the instruction pointer to the desired line within a method in Visual Studio, but I don't see a way to do that in Eclipse (and don't find anything about it in the docs or on google).
This is possible...
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/tips/jdt_tips.html
Drop to frame - When stepping through your code, you might occasionally step too far, or step over a line you meant to step into.
Rather than restarting your debug session, you can use the Drop to
Frame action to quickly go back to the beginning of a method. Select
the stack frame corresponding to the Java method you wish to restart,
and select Drop to Frame from Debug view toolbar or the stack frame's
context menu. The current instruction pointer will be reset to the
first executable statement in the method. This works for non-top stack
frames as well.
Note that Drop to frame is only available when debugging with a 1.4 or
higher VM, or the J9 VM. There are some situations where a JVM may be
unable to pop the desired frames from the stack. For example, it is
generally impossible to drop to the bottom frame of the stack or to
any frame below a native method.
This is not possible.
If you simply want to execute some code at the current place you can use the Expressions view and enter your code as an expression. The methods called by the expression evaluation will run in the current debugging context.
Moving the pointer like in Visual Studio is not possible, however workarounds are:
Going backwards to the beginning of the currently executed method:
Select the method from the debug call stack, right click -> "Drop to frame"
et voila you're back at the beginning of the method.
Now to reach your desired line select the line by clicking in it and hit ctrl+r or right click the line and select "Run to line".
These techniques are hugely helpful and reduce debugging efforts massively, enjoy!
A trick I use is to type a space in your class, somewhere safe such as in the comment line; immediately delete it and save the class. This forces the execution point to jump to the beginning of your current method. Not ideal, I admit, but it can sometimes be used as a workaround to achieve what you want.
Although in the default installation of eclipse it is not possible to do directly move the execution point like in Visual Studio, there may exist an eclipse plugin which provides that functionality somewhere. Have a search around.
I like ankon's answer best, but another option (that will only work for your specific instance -- if that) is to stop at a breakpoint on your if and modify the variable(s) evaluated in the conditional such that it returns false (from the "Variables" view, right click on a variable and click "Change Value...")
I thought that this was totally possible in older versions of eclipse, I thought I had the memory of doing it, but I guess I just implanted that memory when I worked in Visual Studio. From what I'm reading it might come to the jvm and not eclipse itself, there are pages where it's suggested that the jvm cannot handle that.
In my opinion Eclipse is many many times better than VS, I worked extensively in both and since I discovered Eclipse I was always in pain when I had to work in VS. But not having this feature is definitely hurting right now hehe.
You can jump directly to any other method call inside of the currently debugged method. Select some method call below your current instruction pointer and use "Step into selection" from the context menu.
unfortunately not possible to step forward with instruction pointer (program counter), so what you need to do instead is to introduce your own "debugging" variables that you can test on - lets say you want to step around a loop that takes too long, then add a variable and test on its increased value and then encapsulate the loop in an if with that variable. I know this is ugly, but it gets it done - or you could just develop in C++ :-)
Just right click on desired line and choose run to line.That's it...
Put the cursor on the line of your choice and either hit ctrl-R ("Run to line") or right-click and select "Run to line" from the context menu.

Is there a possibility to break on every object reference in eclipse debugger?

Suppose I have a class
public class MyClass {
private Set<String> set = new HashSet<String>();
//and many methods here
}
is there a possibility to make an eclipse debugger stop at every line where set member is used?
I haven't used Eclipse for a while, but from what I remember this was possible in the Callisto release at least. If you set a breakpoint on the line containing the declaration, and then go into the advanced properties for that breakpoint, I believe you can set options for modification and access of that variable.
Edit: I just checked with Eclipse Europa. It does work broadly as I thought; the breakpoint is called a watchpoint when you set it on a variable; and in the "Breakpoint Properties" page (accessible by right-clicking on the breakpoint's bauble in the margin, and possibly in other ways) you can determine whether the debugger should stop on "Field access" and "Field Modification". In your case, you want the first one selected.
This has been part of the eclipse debugger from very start. You just have to set a breakpoint at the line where the variable is declared. For more control you can right click on the breakpoint and select breakpoint properties where you can set if you want to stop only on Access or Modification.
Please keep in mind that modification is actually the change in the value for value types and change in reference for reference types. For example if you set a modification breakpoint (watchpoint) on a HashMap then the debugger wont stop if you add items into this hashmap since adding items doesn't change the address/reference of the variable.
Yes. You can put a breakpoint at the expression
private String propString;
The breakpoint gets another symbol and shows the tool tip "Watchpoint [Acess and modification] "
Whith Shift+Ctrl+I you can watch the value of a selected variable name when the debugger is in step mode.
You also can change variable values at runtime when the debugger is in step mode.
The eclipse debugger is a very useful and powerful tool.

Categories