I'm new to Java and use Eclipse.
When using the debugger's step into button, it will sometimes open up a new class with loads of code and comments. It will then start stepping through a few lines of the new class it opened and then jump back to my class.
Sometimes it opens up more than one class and takes 20 steps to jump back to my code.
Can someone give me a simplified explanation why this happens and what the new class it opened is for?
Quoting from the article
Step into – An action to take in the debugger. If the line does not contain a function it behaves the same as “step over” but if it
does the debugger will enter the called function and continue
line-by-line debugging there.
Step over – An action to take in the debugger that will step over a given line. If the line contains a function the function will be
executed and the result returned without debugging each line.
So what is happening in your case is debugger is going through function's implementation from the framework or library that you used, which is invoked in your code.
As mentioned in the comments used step over instead of step into, so the debugger will not go through those framework or library source code.
Related
I have a code like this
String s="Test-Code-Data";
String[] splitedData = s.split("-");
I have a break point at second line. When my code reaches that point, can i start coding below that and immediately see the output. For example. I want to see the output from below code, and i type it when the execution reaches the second line only.
System.out.println(splitedData[1])
Is this possible in eclipse?
To answer your question, NO you cannot effectively alter code while the code is in execution without making Eclipse warn you that the code is 'out of sync'.
However, you can look at the actual values of your various variables in the various stages of execution of your code. And, once the execution is complete, then you can edit the code and Run the program in Debug again and keep going that way.
You can use the Inspect Variable feature to look at the value of a variable while in execution and paused at a breakpoint by selecting the variable you'd like to inspect and using the keyboard shortcut Ctrl+Shift+I to get the value of the variable.
I'm developing a plug-in for the Eclipse platform. This plug-in will be used to give information about the line of Java source code currently being debugged.
When debugging a Java program, as you hit a breakpoint, Eclipse switches to the standard Debug perspective. Inside this perspective, apart from the standard Console output, the stack trace and various other views, you can see source code of the Java program currently being debugged. Inside this 'source code view', you can see a highlighted line, which is the line of code currently being debugged/evaluated. This highlighted line of code is what I want to access.
Assuming I know when the debugger is running (I assess that through a DebugBreakpointListener class that implements IJavaBreakpointListener), I need to 'ask questions' to the debugger. What, I imagine, I will need, is to somehow ask the debugger directly either for the line of code it is currently highlighting/debugging/evaluating or for the line number of the said line of code.
I'm making a static access to the JDIDebugModel to add the Java Breakpoint Listener:
JDIDebugModel.addJavaBreakpointListener(new DebugBreakpointListener);
I thought I could access the debugger with static references to JDIDebugPlugin but I've yet to find what I'm looking for.
At Part 3 of this research paper, the authors suggested that:
The Eclipse Java debugger is built upon the API of Java Debug Interface (JDI), which is part of the Java Development Toolkit. This API enables adding requests to monitor JVM events such as BreakpointEvent. When an event occurs, the debugger gets a notification and the thread in which this event took place can be obtained. For each frame in the stack trace of this thread the following information can be obtained:
• The source Java file in which the execution at this frame has taken place (or null if the source is not available).
• The method and line number (if available).
• The this object or null if the method is static.
The Eclipse debugger uses this information when a breakpoint is hit. It shows the stack trace for the suspended thread in the ”Debug” view. For the selected frame in this trace, Eclipse highlights the corresponding line number in its source file, and displays the this variable in the ”Variables” view.
This bulletpoint-listed things are exactly what I'm looking for.
Unfortunately, I can't find detailed documentation on how to 'plug in' to the debugger.
If someone can give me information, point me to information or a sample code, or maybe provide me with contact information of someone from the Eclipse JDI project, it would be immensely appreciated.
Thanks in advance.
------Update & Answer:------
With the help of greg-449's answer, I did exactly what I wanted to do. Here's what I did:
The aformentioned breakpoint listener I wrote implements the interface method breakpointHit, which is as follows:
#Override
public int breakpointHit(IJavaThread thread, IJavaBreakpoint breakpoint) {
System.out.println("Just hit a breakpoint!");
// Save pointers to the thread & breakpoint for future use.
return 0;
}
With the pointers to the thread and breakpoint objects saved in one of my objects, I could query them to get up-to-date information on the state of the frame stack, the thread and about the particular breakpoint that I've hit. I can get the namea dn path of the class the debugger is currently debugging by calling:
IStackFrame topStackFrame = thread.getTopStackFrame();
int debuggedLineNumber = topStackFrame.getLineNumber();
String debuggedClassPath = topStackFrame.getLaunch().getSourceLocator().getSourceElement(thread.getTopStackFrame()).toString();
This was exactly what I was looking for. I imagine I will need to read the source code files manually, run them through a tokenizer by having the 'newline' character as a delimiter and get the corresponding token to read that specific line.
There is a huge amount of information available in the IJavaThread and IJavaBreakpoint arguments passed to the breakpointHit method of the IJavaBreakpointListener which should contain this information.
I think for breakpoints which have a line number (not all do) the IJavaBreakpoint argument also implements ILineBreakpoint containing the line information.
I am writing part of a PHP web application (which will be used in a high school bug finding contest) where the user must find bugs in a given Java program. As a part of this, when the Java program executes, we want to highlight the lines of the source of the Java program where the code has executed. To do this, all we need are the line numbers of the source that have been executed, that is, the code path (or is it called code coverage?). We will highlight the lines in the source file using the line numbers.
We will be using PHP's shell-exec() to execute the Java program and the tool to get the code path (whatever that will be). What is the easiest way of getting the line numbers of code path?
Thank you very much!
Here is a picture that describes what we would like
PHP interperts the code, which means it runs over the source each time you run the program. This has the benefit of blowing up as the code is read (which makes line number printouts trivial); however, it often is expensive in other ways, as you cannot optimize deeply (or do any pre-runtime error checking).
Java compiles its code into a JVM assembly language called "bytecode." This means that what is running doesn't generally have access to (or even use) the source code. That said, there are techniques. A compiled Java class has the ability to add "extra data" and one of those "extra data elements" is a line number table, which is an index allowing someone running the assembly to "look up" the line number as the compiler recorded it.
This generally works ok, with the considerations that: compilers often don't mark up every instruction, the source code may not be available, optimization might make certain inner chunks of code not function in ways that facilitate pointing to the input code text.
How code coverage tools "fix" this is that they generally insert into the code (at the assembly level) a large number of commands that effectively act as logging statements to a format that allows the tool to determine which path through the code was actually followed. This is then mapped back through the line number table as best as possible and then used to highlight lines within the original source file.
If you want something with finer resolution (something that can process which portion of a line was executed) then you need to dig deeper. Eventually you might even consider writing your own compiler (or compiler extension) which will store your own custom line number table that overcomes the shortcomings of the current solutions.
Tricks like throwing exceptions (as Shiven has mentioned) and parsing out the line number do work; however, they pollute your code with odd exception processing for items that really aren't exceptional, just to "get the line number". Due to the code clutter and the generally poorer runtime performance of exceptions, I tend to avoid such solutions (but they do work).
Anyway, hopefully this will give you a view as to why it doesn't always work exactly the same way as PHP.
You could get a linenumber if you compile the program with the -g option, do a printStackTrace(), capture the trace output and extract the linenumber from there.
Take a look at Cobertura. It computes coverage and stuff like that, and if it doesn't already do it, it should be relatively easy to add the line number collecting to it.
There's a very hackish attempt to do that, but that's so slow that you may not be able to use it in production https://bitbucket.org/jowu/myriapod/wiki/Home
I have never done or seen anything like this but it does seem like an interesting problem. My thought would be to use the java debugger (jdb) to run the code, rather than just the java command.
You can step through the code line by line (via the step command in jdb) and each time a line executes its line number is spit out. This would require a little help from the PHP side (it would have to parse the line number as well as execute the next step command) but the line numbers are there. Here is a sample output from a very basic java program.
Java (TestClass.java)
public class TestClass {
public static void main(String[] args) {
System.out.println("foo");
System.out.println("bar");
}
}
jdb (jdb TestClass after running javac TestClass.java)
Initializing jdb ...
> stop at TestClass:3
Deferring breakpoint TestClass:3.
It will be set after the class is loaded.
> run
run TestClass
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint TestClass:3
Breakpoint hit: "thread=main", TestClass.main(), line=3 bci=0
3 System.out.println("foo");
main[1] step
> foo
Step completed: "thread=main", TestClass.main(), line=4 bci=8
4 System.out.println("bar");
main[1] step
> bar
Step completed: "thread=main", TestClass.main(), line=5 bci=16
5 }
main[1] step
>
The application exited
Try referring to this link JVMDI
You can try accessing the values of the program counter and then map it onto the lineNumberTable .
OR
I think JVMDI has a method which can access the line number of the executing code.I'm not sure of the latter,refer the to the link above and hope it helps.
Is there a way to jump to a line of code in Eclipse for Java?
It would be useful for re-running a function to debug.
Something like Visual Studio's "Set Next Statement" or the draggable yellow arrow?
When in the debugger select a place in the stack, right click, and select "Drop to Frame". This will unwind the call stack. You can do this on the current method (top of the call stack) to unwind to the top of the method. This doesn't work all the time for various reasons but you can do it pretty often.
This feature does not exist even conceptually in the JVM Tools Interface, much less in the Java Debug Wire Protocol that IDEs tend to interface with. Implementing it in an IDE would require creating (and mantaining) a custom build of Hotspot/JRockit/etc itself.
I also have not found it, and don't think it is supported. It's availability in Visual Studio (for C++ development) really spoiled me. It is very useful on occasion.
I'm unaware of any means to do so in Eclipse - and all obvious checks turn up nothing. I suspect there's an issue with how java works, as it's been available an awfully long time in Visual Studio and for Eclipse to have not matched it means it must not be a heavy task but an epic one.
I have found one rather silly way to rerun a method without going back to the previous frame.
e.g. you are in this function:
'public int compareTo(EVAL evalOther) {
int jRet = compareId(this.id, evalOther.id);
if (jRet == 0) {
jRet = compareXYZ(this.XYZ, evalOther.XYZ);
}
return jRet;
}'
let us say, after executing the first line, i.e.
int jRet = compareId(this.id, evalOther.id);
I want to rerun this line, I just make a minor change to it which will force a quick recompile of the same method . So it starts from the first line of the method again.
e.g. I change the line
'int jRet = compareEVALClass(this, evalOther);'
to
'int jRet = 0 + compareEVALClass(this, evalOther);'
Press Ctrl-S (or whatever your shortcut key is to Save file)
And then the function recompile and will restart from the first line again.
Agreed, this is not as great as Visual Studio's "Set next statement"
I asked about some non-working code I had at Code works extremely slowly and does not print output.
As you can see in the answer to that question, deks debugged my code and also provided the output of the fib() function. How could I do that by myself? I really know very little about debugging.
To debug, get yourself and IDE like NetBeans or Eclipse. Then instead of clicking run, click on line number then click debug. When the execution gets to this point, it stops, and you can examine the variables that are currently visible. To make sure they have values they should have. For example, if n should be between 1 and 10, and it is 100, then you know something is wrong. Fine where you set n, and add more breakpoints. These are the points were the execution will stop. You can also click step over, to execute the next line, then stop, or step into, which will execute the next line, except it will step into any functions that you used.