How to create breakpoints programmatically?

Answered

Hi, we are trying to create a java breakpoint programmatically in our plugin. One complicating factor is that the java class does not (and cannot) exist in the IntelliJ project. To get round that we create a scratch file using ScratchRootType.getInstance().createScratchFile and then FileDocumentManager.getInstance().getDocument(scratchFile).

The problem we have is that the breakpoint request never makes it over the JDWP connection. We are creating the breakpoint using

ApplicationManager.getApplication().invokeAndWait(() -> debuggerManager.getBreakpointManager().addLineBreakpoint(scratchDoc, lineNumber));

and then set the breakpoint request using

 debugProcess.getManagerThread().schedule(new DebuggerCommandImpl() {
    protected void action() {
        List<Breakpoint> breakpoints = debuggerManager.getBreakpointManager().getBreakpoints();
        for (Breakpoint b : breakpoints) {
            if (BOOKMARK_CLASSNAME.equals(b.getClassName())) {
                b.setSuspendPolicy(DebuggerSettings.SUSPEND_ALL);
                b.createRequest(debugProcess);
            }
        }
    }
});

One strange thing is that the first time through b.getClassName() returns null although later it returns the class name.

Suspect the problem may be that async stuff is happening.

Is this the right way to go about creating breakpoints programmatically?

Many thanks,

David

0
4 comments

Mehmet Akcay figured it out!

        LineBreakpoint b = debuggerManager.getBreakpointManager().addLineBreakpoint(bookmarkDoc, lineNumber);
b.setSuspendPolicy(DebuggerSettings.SUSPEND_ALL);
RequestManagerImpl requestsManager = debugProcess.getRequestsManager();
BreakpointRequest request = requestsManager.createBreakpointRequest(b, location);
requestsManager.enableRequest(request);
0

Dgriffiths Thank you for your solution. May I ask you how can the location variable be created? I am working on something similar....

0

Hi plugin.coder,

If you dont have the class ReferenceType object first you need to get it. you can get the ReferenceType object using debugProcess.getVirtualMachineProxy().getVirtualMachine().classesByName(CLASSNAME) method.

Then you can get the location by calling ReferenceType.locationsOfLine. for a line number in a class you ll get multiple locations, since there might be possible more than one bytecode index. 

 

1

Hey Mehmetnakcay,

 

Wow, thanks for your reply. It works.

1

Please sign in to leave a comment.