Attaching a debugger using a custom run configuration

I'm launching a external process through my RunConfiguration, that external process calls java(which I can configure to run it as a remote jvm so that someone can attach to debugging). So I was just wondering if there is a way to attach intellij's debugger to that remote java process, so that I can step through my code. Currenly the 'Remote run configuration' can do this, but I want to extend that feature to my run configuration. Any help?

Thanks

7 comments
Comment actions Permalink

Hi, try using "Before launch" action in the configuration, this way you can for example run any other configuration before launch

0
Comment actions Permalink

Hi Egor, could you tell me how I can do this programatically?

0
Comment actions Permalink

I'm not sure public API is available for that, try having a look at com.intellij.execution.BeforeRunTask

0
Comment actions Permalink

Check com.intellij.debugger.impl.GenericDebuggerRunner.

U need override two methods

public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile)

- check for yr configuration instance

and


protected RunContentDescriptor createContentDescriptor(Project project, RunProfileState state, RunContentDescriptor contentToReuse, ExecutionEnvironment env) throws ExecutionException


attaching debugger to vm, with method attachVirtualMachine

0
Comment actions Permalink

Thanks for the reply Valeriy, But that would mean I'm just attaching the debugger to a port without launching the java process. But I want to launch a java process before that and redirect the ouput to the intellij console view.

public class MyTestRunner extends GenericDebuggerRunner {
    @NotNull
    public String getRunnerId() {
        return "MyApplicationRunner";
    }

    public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
        return profile instanceof MyRunConfiguration;
    }

    @Nullable
    @Override
    protected RunContentDescriptor createContentDescriptor(Project project, RunProfileState runProfileState, RunContentDescriptor runContentDescriptor, ExecutionEnvironment executionEnvironment) throws ExecutionException {
        RemoteConnection connection = new RemoteConnection(true, "127.0.0.1", "50005", true);
        return attachVirtualMachine(project, runProfileState, runContentDescriptor, executionEnvironment, connection, true);
    }

}


I previously overriden the doExecute to execute my commandLineState and redirect the output to console.

    protected RunContentDescriptor doExecute(Project project, RunProfileState state, RunContentDescriptor contentToReuse, ExecutionEnvironment env) throws ExecutionException {
        FileDocumentManager.getInstance().saveAllDocuments();

        ExecutionResult executionResult = state.execute(env.getExecutor(), this);
        if (executionResult == null) {
            return null;
        }

        final RunContentBuilder contentBuilder = new RunContentBuilder(this, executionResult, env);
        contentBuilder.setEnvironment(env);
        return contentBuilder.showRunContent(contentToReuse);
    }

This is how I was invoking the java process and now I can't do this since createContentDescriptor() returns RunContentDescriptor from the GenericDebuggerRunner's doExecute().


I bascially have to chain two run configurations. But not able to figure out how

0
Comment actions Permalink

In RunConfiguration u need implement method

com.intellij.execution.configurations.RunProfile#getState

and return for example com.intellij.execution.configurations.CommandLineState

0
Comment actions Permalink

I have overriden my getState in the Runconfiguration to return a new CommandLineState which returns a ColoredProcessHandler.
I think I didn't quite clearly explain my situation.

When I press the debug button, MyTestRunner get's invoked
I now have to launch a java process with redirected output to intellij console, which I have done by overriding doExecute
but I also want to attach a debugger just after launching the java process.

So basically I want to do two things, one that runs the command line and one that attaches debugger to vm

0

Please sign in to leave a comment.