Allow to debug external process running from Gradle task

This commit is contained in:
Alexey Andreev 2023-07-31 13:15:36 +02:00
parent 4cfbca96b8
commit bf2cfa83f0
2 changed files with 12 additions and 4 deletions

View File

@ -50,7 +50,6 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi
private static final int MAX_PORT = 1 << 16; private static final int MAX_PORT = 1 << 16;
private static final String DAEMON_MESSAGE_PREFIX = "TeaVM daemon port: "; private static final String DAEMON_MESSAGE_PREFIX = "TeaVM daemon port: ";
private static final String INCREMENTAL_PROPERTY = "teavm.daemon.incremental"; private static final String INCREMENTAL_PROPERTY = "teavm.daemon.incremental";
private static final String DEBUG_PORT_PROPERTY = "teavm.daemon.debug.port";
private boolean incremental; private boolean incremental;
private int port; private int port;
private Registry registry; private Registry registry;
@ -269,6 +268,11 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi
public static DaemonInfo start(boolean incremental, int daemonMemory, DaemonLog log, public static DaemonInfo start(boolean incremental, int daemonMemory, DaemonLog log,
String... classPathEntries) throws IOException { String... classPathEntries) throws IOException {
return start(0, incremental, daemonMemory, log, classPathEntries);
}
public static DaemonInfo start(int debugPort, boolean incremental, int daemonMemory, DaemonLog log,
String... classPathEntries) throws IOException {
String javaHome = System.getProperty("java.home"); String javaHome = System.getProperty("java.home");
String javaCommand = javaHome + "/bin/java"; String javaCommand = javaHome + "/bin/java";
String classPath = String.join(File.pathSeparator, classPathEntries); String classPath = String.join(File.pathSeparator, classPathEntries);
@ -278,8 +282,7 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi
"-D" + INCREMENTAL_PROPERTY + "=" + incremental, "-D" + INCREMENTAL_PROPERTY + "=" + incremental,
"-Xmx" + daemonMemory + "m")); "-Xmx" + daemonMemory + "m"));
String debugPort = System.getProperty(DEBUG_PORT_PROPERTY); if (debugPort != 0) {
if (debugPort != null) {
arguments.add("-agentlib:jdwp=transport=dt_socket,quiet=y,server=y,address=" + debugPort + ",suspend=y"); arguments.add("-agentlib:jdwp=transport=dt_socket,quiet=y,server=y,address=" + debugPort + ",suspend=y");
} }

View File

@ -29,6 +29,7 @@ import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property; import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath; import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
@ -98,6 +99,9 @@ public abstract class TeaVMTask extends DefaultTask {
@Classpath @Classpath
public abstract ConfigurableFileCollection getDaemonClasspath(); public abstract ConfigurableFileCollection getDaemonClasspath();
@Internal
public abstract Property<Integer> getDaemonDebugPort();
@TaskAction @TaskAction
public void execute() throws BuildException, IOException, NotBoundException { public void execute() throws BuildException, IOException, NotBoundException {
if (getOutOfProcess().get()) { if (getOutOfProcess().get()) {
@ -108,7 +112,8 @@ public abstract class TeaVMTask extends DefaultTask {
} }
private void executeInSeparateProcess() throws BuildException, IOException, NotBoundException { private void executeInSeparateProcess() throws BuildException, IOException, NotBoundException {
var daemon = BuildDaemon.start(false, getProcessMemory().get(), new DaemonLogImpl(), var debugPort = getDaemonDebugPort().isPresent() ? getDaemonDebugPort().get() : 0;
var daemon = BuildDaemon.start(debugPort, false, getProcessMemory().get(), new DaemonLogImpl(),
createDaemonClassPath()); createDaemonClassPath());
try { try {