From bf2cfa83f00ff2291a834ec95f2cdd63fa4a1b61 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 31 Jul 2023 13:15:36 +0200 Subject: [PATCH] Allow to debug external process running from Gradle task --- .../main/java/org/teavm/tooling/daemon/BuildDaemon.java | 9 ++++++--- .../src/main/java/org/teavm/gradle/tasks/TeaVMTask.java | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java b/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java index 524769b00..5d73c2711 100644 --- a/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java +++ b/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java @@ -50,7 +50,6 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi private static final int MAX_PORT = 1 << 16; private static final String DAEMON_MESSAGE_PREFIX = "TeaVM daemon port: "; private static final String INCREMENTAL_PROPERTY = "teavm.daemon.incremental"; - private static final String DEBUG_PORT_PROPERTY = "teavm.daemon.debug.port"; private boolean incremental; private int port; private Registry registry; @@ -269,6 +268,11 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi public static DaemonInfo start(boolean incremental, int daemonMemory, DaemonLog log, 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 javaCommand = javaHome + "/bin/java"; String classPath = String.join(File.pathSeparator, classPathEntries); @@ -278,8 +282,7 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi "-D" + INCREMENTAL_PROPERTY + "=" + incremental, "-Xmx" + daemonMemory + "m")); - String debugPort = System.getProperty(DEBUG_PORT_PROPERTY); - if (debugPort != null) { + if (debugPort != 0) { arguments.add("-agentlib:jdwp=transport=dt_socket,quiet=y,server=y,address=" + debugPort + ",suspend=y"); } diff --git a/tools/gradle/src/main/java/org/teavm/gradle/tasks/TeaVMTask.java b/tools/gradle/src/main/java/org/teavm/gradle/tasks/TeaVMTask.java index 8596b105e..7ec5e2fd3 100644 --- a/tools/gradle/src/main/java/org/teavm/gradle/tasks/TeaVMTask.java +++ b/tools/gradle/src/main/java/org/teavm/gradle/tasks/TeaVMTask.java @@ -29,6 +29,7 @@ import org.gradle.api.provider.MapProperty; import org.gradle.api.provider.Property; import org.gradle.api.tasks.Classpath; import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.TaskAction; @@ -97,6 +98,9 @@ public abstract class TeaVMTask extends DefaultTask { @Classpath public abstract ConfigurableFileCollection getDaemonClasspath(); + + @Internal + public abstract Property getDaemonDebugPort(); @TaskAction public void execute() throws BuildException, IOException, NotBoundException { @@ -108,7 +112,8 @@ public abstract class TeaVMTask extends DefaultTask { } 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()); try {