Java Debug Wire Protocol (JDWP) with Native Image

Overview

This document describes the Java Debug Wire Protocol (JDWP) debugging support for Native Image, a feature that enables debugging of native images using standard Java tooling.

Installation

The JDWP feature relies on a shared library, which is loaded only when the debugger is actively used. This library must be built once before building native images with JDWP enabled.

native-image --macro:svmjdwp-library

Note: This is a one-time setup step. The same library will be used for all native images built with JDWP enabled.

Note: This library is stored in the GraalVM installation by default. If that directory is not writable, provide an alternative destination path with -o <path/to/writable/directory>/libsvmjdwp, or on Windows, use -o <path\\to\\writable\\directory>\\svmjdwp.

Usage

Note: JDWP debugging for Native Image is experimental.

To include JDWP support in a native image, add the -H:+JDWP option to your native-image command:

native-image -H:+UnlockExperimentalVMOptions -H:+JDWP ... -cp <class/path> YourApplication ...

This command produces:

  1. The native executable
  2. An <image-name>.metadata file
  3. The lib:svmjdwp (libsvmjdwp.so, libsvmjdwp.dylib or svmjdwp.dll) shared library that will be necessary when debugging is also copied next to those files.

Launching in Debug Mode

To launch the native image in debug mode, use the -XX:JDWPOptions= option, similar to HotSpot’s -agentlib:jdwp=:

./your-application -XX:JDWPOptions=transport=dt_socket,server=y,address=8000

Note: Debugging requires the image-name.metadata file generated at build time and the svmjdwp shared library in the same directory as the native executable.

For a complete list of supported JDWP options on Native Image, run:

./your-application -XX:JDWPOptions=help

Additional JDWP Options

Native Image supports additional non-standard JDWP options:

Examples:

Note: If lib:svmjdwp cannot be found, the application will terminate with error code 1.

Goals and Constraints

The JDWP debugging support for Native Image aims to:

  1. Expose Native Image through JDWP as-is, maintaining its assumptions and constraints
  2. Incur minimal or no performance overhead when not in use
  3. Add minimal size overhead to the native binary
  4. Be available on all Graal-supported platforms, including Linux, macOS, and Windows, across x64 and AArch64 architectures
  5. Provide a debugging experience similar to HotSpot, without requiring additional steps (e.g., setting permissions, environment variables)

Architecture

The JDWP debugging support is implemented using a Java bytecode interpreter, adapted from Espresso to work with Native Image. Key components include:

  1. Interpreter: Derived from Espresso and adapted for SubstrateVM. It does not enable any dynamic features beyond what Native Image already supports.

  2. PLT/GOT Feature: Used to divert execution to the interpreter. This implementation detail may change for some platforms.

  3. Metadata File: An external .metadata file produced at build time, containing information required for runtime method interpretation.

  4. JDWP Server: Implemented as a native library (lib:svmjdwp), handling network connections and implementing JDWP commands.

  5. JDWP Resident: A component within the application providing access to locals, fields, stack traces, and other runtime information.

Limitations

The JDWP debugger for Native Image is designed to align with Native Image’s architecture and principles. While many limitations are a natural consequence of Native Image’s design, others may be due to the current implementation of the debugger itself. Here are the key limitations to be aware of:

These limitations reflect the current state of JDWP debugging support in Native Image. Some may be addressed in future iterations of the debugger, while others are fundamental to Native Image’s design.

Further Reading