Eclipse - Execute Expression on at Point in Code? - java

Is it possible to have Eclipse execute an arbitrary expression at a particular point in code when debugging?
The Execute/Display functionality allows you to run arbitrary code in the context of debugging. A breakpoint stops flow.
What I'd like is something like a breakpoint that can be inserted at a particular point in a class, doesn't suspend application flow, but instead executes a snippet of code.
The background is I'm trying to debug some multithreaded code which I can't edit, and I want to do some naive System.out.printlns to see when various things happen. If I use breakpoints then the flow of the events will be disturbed.

Aha! There's an undocumented feature whereby conditional breakpoints can execute other code, dubbed 'Printpoints'.

You may use the "Breakpoint Properties" of a breakpoint to add some code. Hit the "Conditional" Checkbox in the dialog and place your code there.

Related

Eclipse Debugging Run Methods

it's probably really easy but I still don't know how to do this.
Is it possible to run a method while a program in Java is running in debug-mode in Eclipse?
While debugging, open the "Display view" to run any code you want at the position you're currently at.
See:
https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
In debug mode, it will open a new perspective and it's going to show the workflow of your Main method, including all the methods invocations during it's execution, which variable's values has changed, everything that happen to the beginning until the end of your Main class code.
May you check this link, got a lot of usefull information.

Drag operation on JavaSlider object using QTP/UFT works with breakpoint but fails without

I use Drag method on a Java Slider with some number as argument. It fails to work during execution. But always works with breakpoint. Any setting to make QTP simulate execution behavior of breakpoint.
Please note that application is thick client Java.
I've come across this problem in the past and I found it was because QTP/UFT was getting ahead of its self. This might seem primitive but I had a sync (to ensure that your application has displayed the expected page), put a couple of second wait, check that 'Slider' exists and then attempt an action on it.

Log message/error at design-time in IntelliJ Idea?

I have had a problem with a custom component I am writing. The issue seems to be a resource not loading properly (not sure why) at design-time.
To track it down I'd like to put in some debug-code that will execute at design-time, and give output in form of some messages.
Is there a log or something similar that can be written to at design-time?
Thanks
There's a really nice trick you can use to add debug statements in IntelliJ without actually changing the code. The answer is to use non-suspending breakpoints.
Add a breakpoint at the necessary point in the code, then right click on it. De-select "Suspend", and then put whatever you want in the "Log evaluated expression" - this will give you access to the fields of the instance.
Run your app in debug mode, and you'll get debugging info in the console without having to manually examine everything.

How do I invoke the debugger from code?

From Javascript, I can simply write
debugger;
and when that line executes, it stops the code as if I had put a breakpoint there.
Is there an equivalent in Java? I need it to work in Eclipse specifically.
EDIT: can we take it as read that I am not an idiot and if placing a breakpoint with the IDE itself were an option, I would have already done so?
FURTHER EDIT: I had not thought it necessary to point out that since placing a breakpoint with IDE is not an option, any answer that revolves around placing a breakpoint with IDE is not likely to be helpful. In case everybody is dying of curiosity, the original code is not written in Java -- it's processed down to Java byte-code. As a result, Eclipse is confused enough it doesn't want to set breakpoints.
The JVM debugger, which Eclipse uses (mostly) under the covers, can set breakpoint at a line number in a method IF compiled with certain optional debugging info OR at method entry (always).
If your classes were compiled without debugging "lines" so the debugger can't set a line breakpoint, and you don't want to or can't recompile them, you can still set a method-entry breakpoint. In Package Explorer -- NOT an edit window for the source -- right-click the method name/signature and Toggle Method Breakpoint to on.
This can be combined with the comment by #ajp: add a method e.g. void [static] debugger(){} that doesn't do anything when you call it, but provides a convenient target where you can set a method breakpoint.
Warning: although it is possible to compile with partial debugging info, like debugging "vars" but not debugging "lines", generally people just use "debug on" or "debug off". If your classes are compiled without debugging "vars", the debugger will be much less useful.
I am probably going to get a few downvotes, but so be it...
If you open a source file in Eclipse and right-click on the left edge of the document view, you will get the popup menu illustrated in the image below.
As you can see, you have the option to toggle a breakpoint and also to turn off and on the line numbers. So, I am not sure what you mean by "My Eclipse is being operated in an environment where it cannot find line numbers to the source code". Unless you have some modified version of Eclipse that does not show this menu, I don't know what you mean by that. The option is there.
You wrote:
From Javascript, I can simply write
debugger;
and when that line executes, it stops the code as if I had put a breakpoint there.
And also:
can we take it as read that I am not an idiot and if placing a
breakpoint with the IDE itself were an option, I would have already
done so?
Option 1: The simple, "incorrect" answer is that there is no instruction in the Java language to make the program pause in a breakpoint nor there is an option like in languages like C++ to
make a debug build. So, your "ONLY" option is to execute a breakpoint from the IDE.
Option 2: The complicated, correct answer is that you can do what you want following these instructions: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html
In your case, I don't believe that you don't have the option to place a breakpoint with the IDE to debug your program; no matter how complex your program is. BUT, I am not here to debate that point. According to your post, you have to do option 2 laid out here.

Debugging a continuas value Android Studio

At the moment I've been using logging to keep a track of my continuously changing values within an update loop. Problem is is that this can be time consuming.
I can use the debugger but it stops code executing and due to what I'm doing causes weird results and false values.
Does anyone know if it's possible to "watch" the variables while keeping the code running. Like having one watch within a scope.
Android Studio/IntelliJ have some really nice features in breakpoints that let the IDE take an action when it hits a breakpoint instead of just dropping your app into the debugger (which it can of course also do). On any given breakpoint, there's a checkbox in the options that controls whether it will actually suspend your app and drop it into the debugger; you can turn that on or off independently. You can have it log a message to the debugger console, or have it evaluate any expression and log the results to the console.
I find these options super-handy; it's the equivalent of adding debug logging to your code, but you can add and change it on the fly without modifying your code and doing a rebuild-restart cycle.
There's also some really powerful filtering that will selectively control whether or not the breakpoint is triggered based on certain criteria; you can even set up cascades of breakpoints where one doesn't fire until a previous one is hit.
Experiment with some of the options.

Categories