Trace code execution? - java

One of my programs is stopping at some position (no exception, also seems not to be crashed), but I don't know that position. Instead of setting up a breakpoint (which means you already need to know the position where something happens), is it possible to get an information which code line was executed as last (=before now)? I am using Eclipse, is this maybe somehow possible in the debug view?
Think of something like step-by-step code execution, except that I don't want to click step-by-step a million times, instead Eclipse or some other tool shall do that for me.
Thanks for any hint on this!

I faced the same problem. There is a "suspend" button (two vertical yellow bars - see top-right of image) on the right of the green arrow/triangle which will stop you in the debugger. You may need to look carefully at the debug stack until you recognize part of your code.
It was very useful for me - I was in a catastrophic backtrack in a regex (which could take years to terminate!) and I would never have guessed this.
[If you don't have any breakpoints you will need to start in debugger perspective to see the button.]

In Eclipse, if you click the "suspend" button in the debug UI you can pause a thread or the entire VM. You are probably hung behind a wait of some kind and may have to walk down the stack trace to find the line in your code that invokes the service that is waiting.

As others have said, Eclipse has the Suspend button.
But the good old fashioned way is to add a load of System.out.println("No problem so far at line 'x' " ) statements and track the bug down by hand.

Related

Prevent IntelliJ from removing newlines around single statement methods?

Sometimes IntelliJ will take single statement methods and put them on the same line, as seen here:
If I click either bracket it will turn back into:
This question isn't a debate for which of the two is better - I want to know how to turn this off. I combed through IntelliJ preferences and couldn't seem to find anything, and searching both here and Google didn't bring up what I wanted.
I think these options are responsible for this:
If you're talking about code reformatting putting the extra newlines into your code, then it's the "Simple methods in one line" option you want to check in Editor/Code Style/Scala/Wrapping and Braces:

Configure eclipse debugger to filter out library methods I didn't write when navigating the stack?

I'm using a number of frameworks, boot, hibernate, etc, in our code, including enhancing and generating code. When I call what looks like a simple method of code it will go through CglibAoPProxy and similar methods before to call the method I want. This means if I want to look into the next piece of my code I need to either walk through 5 lays of stack trace for code I presume is functional (and thus don't care to trace it's logic) to get to the next method of code I personally wrote, or add breakpoints and where I want to break, hit run, and then remove the breakpoint after.
What would be nice is if there was an easy way to tell the debugger that I only want to look at my code. If I step into a method implemented by some library just keep running until it hits the next line of code that is part of a library I wrote. Is there an easy way to configure the debugger to do this? to only care about code I personally wrote when stepping into something?
Likewise, when I want to move back up the stack trace, to look at an earlier phase state, it's very difficult. With so many levels of methods from libraries it's hard to find the ones that contain code I personally wrote. Is there a way to highlight only your methods (say methods from the current working set) or something similar in the stack trace?
Step filters may help.
Open Eclipse preferences. For example, on Windows, use the menu item Windows>Preferences.
Navigate to Java>Debug>Step Filtering.
Turn on the checkbox Use Step Filtering.
In the checkbox list titled Defined step filters, turn on checkboxes of package hierarchies you'd like to skip. Use the buttons alongside to add additional filters.
All that said, I hadn't used step filtering until your question led me to look into it. Not yet sure how I personally feel about skipping code while debugging. But that last item in the default filter list -- java.lang.ClassLoader -- looks very helpful.

Eclipse: How to force execute a particular java statement while debugging?

I have searched and found that indeed Eclipse does not support this 'direct' feature. But, Did I stil miss something? and Is it present in other IDEs?
Let me elaborate my question more -
if a statement falls under execution flow based on an expression evaluation, then why can't we force execute it? (without the execution of the expression).
For example consider this -
... if(bool returnsABoolean) {
<execute some statement>;
}
...
Can the execution of 'if' be skipped and the statement be executed as a 'next statement'? (I obviously can control the value of 'returnAsBoolean' in the Variables view; but can I not skip (in a controlloed manner) all the statements until a particular statement in the execution?)
Highlight the code you want to run and right-click/Execute or press Ctrl+U.
Alternatively to "Execute", use "Display" (Ctrl+Shift+D) or "Inspect" (Ctrl+Shift+I) to see the result.
Looks like you want the 'Display' view - in the Debug perspective, do :
Window -> ShowView -> Display.
You can enter Java statements to execute there (you have to select the bit of text each time that you want to execute)
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
Debugging allows you to run a program interactively while watching the source code and the variables during the execution.
So debugging is nothing but executing program but inspecting the elements during execution but you can not jump into something which is not there in execution.
You can use keyboard buttons F5,F6 ,F8 etc. (For Eclipse) and other Shortcuts during debugging for your convinience but you can't jump to something directly which is not in the execution sequence.
Debugging Shortcuts:
F5 Step into
F6 Step over
F8 Resume and will take you to the break point
Ctrl+Shift+B Toggle breakpoint
Ctrl+Shift+D Display Info of current statement
Ctrl+Shift+I Inspect the selected element
You can Skip some code by the use of breakpoint you can directly jump to specific point and avoid debugging of code which you believe works fine.Or you can jump out code snippet if you want to.
The question really was to set the Instruction pointer at will. This has been discussed and through url's i pasted on the comments above - this is not an eclipse feature (yet).

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.

Categories