Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top