How can I set the current line of execution in the eclipse java debugger? - eclipse

I want to force the current execution line to a specific line in the same function, possibly skipping intermediate lines. All my old school debuggers had this feature, but I can't find it in eclipse. Is there a way to do it without changing code?

The first two answers seem to miss the topic, unless it is me not understanding the question.
My understanding, a feature I searched myself, is that you want to skip a number of lines (when stepping in code) and set the program counter (to take assembly vocabulary) to the given line. It might be interesting to skip some costly object creation, see some error situations, etc. I used to do that in Visual Studio (C or C++ code).
I haven't found that in Eclipse, nor in NetBean. It might be a limitation of JVM, or an enforcement of some policy...
The Run to line command execute, of course, all lines between the current execution position and the designated one.

I too have long sought this feature, and "Run to line" is not the same thing.
This may well be a limitation of the JVM. Java does not implement goto, though it does have jump statements, like break and continue. Those are at the block level however. If this is a JVM limitation, my guess is that it is more likely due to the security architecture. Tight control of the program counter is debilitating for interlopers, like viruses -- and sadly debuggers.

I think that is not possible in Java. The only feature that allows you to "step back" is using "drop to frame", which takes you back to the first instruction of the current frame. At least I haven't seen any debugger with this specific functionality, but I haven't been able to find on the net why is it so...
I know the debugger in Visual C allows to change to pointer. I will keep on searching, maybe at least we will know why is like this, but it seems to be some kind of design limitation.

Feature request saying it is not possible
In https://bugs.eclipse.org/bugs/show_bug.cgi?id=287795 (credits to Amitd) Darin Wright says it is a limitation of the underlying Java debugger:
Currently, this is not possible with the Java debugger as the native debug interface does not provide the ability to move/set the program counter arbitrarily.
C / C++
CDT supports it however (tested on Neon, Ubuntu 14.04). Right click on the line you want to go to, and select either:
"Move to line": jump to line and break there
"Resume at line": jump to line and continue execution from there
This also serves as further evidence that there is an underlying Java limitation, as Java tends to be more feature rich in Eclipse, and those menu entries are not present in Java next to "Run to line" (which does not skip execution of lines).
This test program prints 0 if you jump the line i = 1:
#include <stdio.h>
int main(void) {
int i;
i = 0; /* Break here. */
i = 1;
printf("%d\n", i); /* Jump to here. */
}

"Run to line" appears to need the program to be running and in a paused state to use. The other option is to set a breakpoint for that line when running in debug-mode.
Double-click far-left vertical bar in the source pane on the same line to add a breakpoint; or,
Select the line and go to Run > Toggle Breakpoint.
At least, this is in Eclipse 3.3.2.

Related

How to step over method call in Eclipse Debugger?

In Eclise debugger, the default step options are:
F5: Step into method
F6: Step over line
F7: Step out of method
Often I do have code like this:
obj.getMember().doStuff();
Or maybe even worse (please don't question the code design, just making this up):
obj.getInstance().createSomething().getMember().doStuff();
Usually I want to inspect doStuff() only, but F6 will not step over individual method calls, but it will step over the entire line. So I end up hitting F5 and F7 until I reach doStuff().
My question is: Is there any option to step over method calls (= do F5 and F7 in one shortcut)? Would be cool, if I had to tap X, X, X, F5 only to reach doStuff().
// Edit for clarification: I'm looking for a keyboard-only solution. I know there are several ways I can reach my method (e.g. breakpoints), but I'm asking for a combined shortcut for F5 + F7, so to say.
You have to select the method you want to step into and then with the right click of the mouse you will find the option Step into Selection :
Select the Step Into Selection command to step into the currently
selected statement in the active editor.
You must have program execution suspended for this command to be available.
As you can see from the image above you can just press OPTION and F5 on a mac once selected the method to step into ...
Shortcut :ALT + F5
You could also place a breakpoint into doStuff() and, when you reach obj.getInstance().createSomething().getMember().doStuff(); just resume execution (keybinding: F8:
All the intermediate frames will be executed and the debugger will pause directly in doStuff() again. This of course means that you cannot decide from run to run like you can do with aleroot's Step into Selection suggestion.
Alright, something completely different, since I think I understand the core of your question a bit better now.
There's no predefined skip-over-this-method command afaik, so you can build one yourself. This touches some advanced topics so I'll only post some pointers here:
Create an Eclipse plugin.
In that plugin, declare a command with a keybinding like Alt+F8 and implement the command's default handler
In the handler of your command, invoke the built-in commands which are bound to the Step into and Step out actions. You can find their IDs by looking into the Eclipse source or trying the Plugin-Spy feature.
Package up your plugin and start your Eclipse with it included.
This way you'll reuse the predefined functionality and automate your F5 plus F7 mashing into a single command with relatively small effort.

Goto in MATLAB?

I was looking through an error message that MATLAB gave me when I saw the command opentoline. After opening its help doc:
opentoline Open to specified line in function file in Editor.
Doesn't this serve the same purpose as goto as it can take you to the specific line number of any program?
opentoline will open the file in Matlab's editor. That's handy if a routine indicates a problem in a specific file/line. Then it can jump (or offer a link) to this location and the user sees the file/line.
goto is only for execution, it jumps to the position on execution and will continue executing from the goto target. The editor is not involved in this case. BTW, there is no built-in goto statement in Matlab, but functions called goto from third parties. I wouldn't use them anyway.

Disable auto-scrolling in command window

A lot of the code that I write in Matlab has a very verbose output. As the program runs, information is printed to the command window, and with each new line, the window automatically scrolls to the bottom. This becomes a problem when I want to read some of the output more closely or scroll up to look at older output. I can scroll up, but only until a new line is printed, which is often less than a second.
Does anyone know if it is possible to turn off this automatic scrolling in the Matlab window? I work in a number of different Matlab versions, depending on the machine, and this happens with all of them. The answer to this might be "No", but I swear I remember having this functionality at one point.
Use the more function: http://www.mathworks.com/help/matlab/ref/more.html
more on
Then run your program. Press spacebar when you wish to see more of the output.
more off will turn it off.
You may find this workaround useful.
First launch matlab using the command line matlab -logfile 'myLog.txt' (the doc says it "starts MATLAB and makes a copy of any output to the Command Window in filename. This includes all crash reports.")
Then open your .txt file using a text editor supporting automatic refresh of content (see picture). On OSX I use TextWrangler (freely available at www) but others have been reported to have this feature (see here or here).
Results: output displays (fprintf, disp, but not the commands per se) are printed both on the Matlab console and the text editor (file is refreshed with a little lag time, below half a second I would say with my configuration). And there is no automatic scrolling. Such procedure does not seem to impact the overall performance of the script (although it may deserve some testing).

Oddball issue arising with use of TextWrangler to edit Python 2.7 script which tests multithreading and Queue modules

I'm writing a script in Python 2.7 exploring the use of the multithreading and Queue modules. the script just defines two functions, one of which will be started in a thread instance to fill a Queue, and a second one which will be started in a second thread instance to pull something off the Queue. However, I cannot even get this script to execute, as it chokes on one of the function definitions even before it executes. The code snippet with the problem is:
def listenThread(counter):
while queue.empty() != True:
try:
outcome = queue.get()
print outcome
counter -=1
print counter
except:
return 'queue.get() command loop failing.'
The error message I am getting is:
$ python threading-and-queue-test.py
File "threading-and-queue-test.py", line 34
except:
^
IndentationError: unindent does not match any outer indentation level
$
I've searched SO and checked the docs for correct formation of the try-except construct, and it seems OK. I had earlier versions of the code (without the try-except ) working, so I am getting suspicious about the text editor, TextWrangler v4.0.1.
Can anyone suggest a debug approach or code correction?
Thanks!
Red
As you have already suspected, it is most likely an indentation error. Your text editor is probably mixing the use of tabs and spaces. Replace all tabs with spaces (or vice versa though the standard in python is 4 spaces) and you should see the error disappear.
For those who find this question later like I did it's easy to fix TextWrangler's settings and stop this issue altogether; Go to "Preferences" -> "Editor Defaults" -> "Auto-expand tabs" then set tabs to 4 spaces. Restart TextWrangler for changes to take affect. For existing documents that continue giving you a problem read this.

debug the last sentence of a program

I have used Eclipse and VS. When I insert a breakpoint and debug the program, it will stop before the breakpoint.
But what if I want to debug the effect of the last sentence of the program? Inserting a meaningless sentence(say print 'pause' in Python) is OK but seems awkward. Is there any alternatives?
In Visual Studio you can put break point on closing bracket in main (or any) method of Program (or any) class (default naming, may vary), then debugger stops just before closing application.
Is there any reason not to use a breakpoint on the last statement, like you said, then manually proceed one step? Depending on the debugger, this can also be automated. In GDB, one can use commands N, where N is the breakpoint number, to set a list of (debugger) commands to be executed immediately after a breakpoint is hit.