Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM 9+ No Way to Enable Remote Debugging #20644

Closed
edeesis opened this issue May 3, 2022 · 1 comment · Fixed by #21008
Closed

JVM 9+ No Way to Enable Remote Debugging #20644

edeesis opened this issue May 3, 2022 · 1 comment · Fixed by #21008
Assignees
Milestone

Comments

@edeesis
Copy link

edeesis commented May 3, 2022

Expected Behavior

In JDK 9, the agentlib options for address changed from requiring simply a port number to allowing a full address (host and port) to be specified.

This change caused the following debug string:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

to only bind to 127.0.0.1 in JDK 9, compared to JDK 8 which would bind to 0.0.0.0.

The correct format to keep this behavior is

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

Sources (couldn't find an official source):

https://www.baeldung.com/java-application-remote-debugging#from-java9
https://www.jetbrains.com/help/idea/debug-a-java-application-using-a-dockerfile.html#create-remote-debug-config

While most people are debugging locally, this becomes a problem especially when using Docker - which needs 0.0.0.0 to be bound to allow the host to communicate over the internal docker network.

Current Behavior

Using the --jvm-debug parameter sets

-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005

and only the server, suspend, and port can be overriden, instead of the whole address. In addition, port is an Integer, so you can't specify *:5005

Context

Workaround:

tasks {
    run {
        jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"]
    }
}

Suggested solution:

Add a new property to https://docs.gradle.org/current/javadoc/org/gradle/process/JavaDebugOptions.html to specify the address.

args.add("-agentlib:jdwp=transport=dt_socket,server=" + (server ? 'y' : 'n') + ",suspend=" + (suspend ? 'y' : 'n') + ",address=" + port);

        if (debugOptions.getEnabled().get()) {
            boolean server = debugOptions.getServer().get();
            boolean suspend = debugOptions.getSuspend().get();
            int port = debugOptions.getPort().get();
            String address = debugOptions.getAddress().get()
            args.add("-agentlib:jdwp=transport=dt_socket,server=" + (server ? 'y' : 'n') + ",suspend=" + (suspend ? 'y' : 'n') + ",address=" + (address != null ? address + ":" : "") + port);
        }

Alternatively, the address could take priority over the port.

        if (debugOptions.getEnabled().get()) {
            boolean server = debugOptions.getServer().get();
            boolean suspend = debugOptions.getSuspend().get();
            int port = debugOptions.getPort().get();
            String address = debugOptions.getAddress().get()
            args.add("-agentlib:jdwp=transport=dt_socket,server=" + (server ? 'y' : 'n') + ",suspend=" + (suspend ? 'y' : 'n') + ",address=" + (address != null ? address : port));
        }
@h0tk3y
Copy link
Member

h0tk3y commented Jun 14, 2022

The suggested fix is:

  • Add a property to JavaDebugOptions:

    /**
     * The address of the debugger server.
     * If the value is not set, then only the port will be passed in the debugger options.
     *
     * @since 7.6
     */
    @Incubating
    @Optional
    @Input Property<String> getHost();

    It won't have any value by default, and the absence of the value will lead to the current behavior – no host name passed to JDWP → loopback only on Java 9+, and all interfaces on older versions.

    If present, the string will be passed in the JDWP arguments address= part, always followed by :$port (so the host name should not include the port). It will be possible to use the value "*" for host, meaning that the server should bind on all interfaces.

  • Similarly, support debugging the Gradle build remotely by introducing the property org.gradle.debug.host with the same semantics as the one described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants