Class ByteBuddyAgent


  • public class ByteBuddyAgent
    extends java.lang.Object

    The Byte Buddy agent provides a JVM Instrumentation in order to allow Byte Buddy the redefinition of already loaded classes. An agent must normally be specified via the command line via the javaagent parameter. As an argument to this parameter, one must specify the location of this agent's jar file such as for example in

    java -javaagent:byte-buddy-agent.jar -jar app.jar

    Note: The runtime installation of a Java agent is not possible on all JVMs. See the documentation for install() for details on JVMs that are supported out of the box.

    Important: This class's name is known to the Byte Buddy main application and must not be altered.

    Note: Byte Buddy does not execute code using an AccessController. If a security manager is present, the user of this class is responsible for assuring any required privileges.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static interface  ByteBuddyAgent.AgentProvider
      An agent provider is responsible for handling and providing the jar file of an agent that is being attached.
      static interface  ByteBuddyAgent.AttachmentProvider
      An attachment provider is responsible for making the Java attachment API available.
      protected static interface  ByteBuddyAgent.AttachmentTypeEvaluator
      An attachment evaluator is responsible for deciding if an agent can be attached from the current process.
      static interface  ByteBuddyAgent.ProcessProvider
      A process provider is responsible for providing the process id of the current VM.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String AGENT_CLASS_PROPERTY
      The manifest property specifying the agent class.
      private static java.lang.String ATTACHER_FILE_NAME
      The naming prefix of all artifacts for an attacher jar.
      private static ByteBuddyAgent.AttachmentTypeEvaluator ATTACHMENT_TYPE_EVALUATOR
      The attachment type evaluator to be used for determining if an attachment requires an external process.
      private static java.lang.ClassLoader BOOTSTRAP_CLASS_LOADER
      Representation of the bootstrap ClassLoader.
      private static int BUFFER_SIZE
      The size of the buffer for copying the agent installer file into another jar.
      private static java.lang.String CAN_REDEFINE_CLASSES_PROPERTY
      The manifest property specifying the can redefine property.
      private static java.lang.String CAN_RETRANSFORM_CLASSES_PROPERTY
      The manifest property specifying the can retransform property.
      private static java.lang.String CAN_SET_NATIVE_METHOD_PREFIX
      The manifest property specifying the can set native method prefix property.
      private static java.io.File CANNOT_SELF_RESOLVE
      Represents a failed attempt to self-resolve a jar file location.
      private static java.lang.String CLASS_FILE_EXTENSION
      The file extension for a class file.
      private static java.lang.String CLASS_PATH_ARGUMENT
      The class path argument to specify the class path elements.
      private static int END_OF_FILE
      Convenience indices for reading and writing to the buffer to make the code more readable.
      private static java.lang.String FILE_PROTOCOL
      Represents the file URL protocol.
      private static java.lang.String INSTRUMENTATION_METHOD
      The name of the method for reading the installer's instrumentation.
      private static java.lang.String JAR_FILE_EXTENSION
      The file extension for a jar file.
      private static java.lang.String JAVA_HOME
      The Java property denoting the Java home directory.
      static java.lang.String LATENT_RESOLVE
      Indicates that the agent should not resolve it's own code location for a self-attachment.
      private static java.lang.String MANIFEST_VERSION_VALUE
      The manifest property value for the manifest version.
      private static java.lang.String OS_NAME
      The Java property denoting the operating system name.
      private static int START_INDEX
      Convenience indices for reading and writing to the buffer to make the code more readable.
      private static java.lang.Object STATIC_MEMBER
      Base for access to a reflective member to make the code more readable.
      private static int SUCCESSFUL_ATTACH
      The status code expected as a result of a successful attachment.
      private static java.lang.instrument.Instrumentation UNAVAILABLE
      An indicator variable to express that no instrumentation is available.
      private static java.lang.String WITHOUT_ARGUMENT
      Represents a no-op argument for a dynamic agent attachment.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ByteBuddyAgent()
      The agent provides only static utility methods and should not be instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void attach​(java.io.File agentJar, java.lang.String processId)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, java.lang.String processId, java.lang.String argument)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, java.lang.String processId, java.lang.String argument, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, java.lang.String processId, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, ByteBuddyAgent.ProcessProvider processProvider)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, ByteBuddyAgent.ProcessProvider processProvider, java.lang.String argument)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, ByteBuddyAgent.ProcessProvider processProvider, java.lang.String argument, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attach​(java.io.File agentJar, ByteBuddyAgent.ProcessProvider processProvider, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent Jar on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, java.lang.String processId)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, java.lang.String processId, java.lang.String argument)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, java.lang.String processId, java.lang.String argument, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, java.lang.String processId, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, ByteBuddyAgent.ProcessProvider processProvider)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, ByteBuddyAgent.ProcessProvider processProvider, java.lang.String argument)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, ByteBuddyAgent.ProcessProvider processProvider, java.lang.String argument, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent library on the target process which must be a virtual machine process.
      static void attachNative​(java.io.File agentLibrary, ByteBuddyAgent.ProcessProvider processProvider, ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Attaches the given agent library on the target process which must be a virtual machine process.
      private static java.lang.instrument.Instrumentation doGetInstrumentation()
      Performs the actual lookup of the Instrumentation from an installed Byte Buddy agent.
      static java.lang.instrument.Instrumentation getInstrumentation()
      Looks up the Instrumentation instance of an installed Byte Buddy agent.
      static java.lang.instrument.Instrumentation install()
      Installs an agent on the currently running Java virtual machine.
      static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.AttachmentProvider attachmentProvider)
      Installs an agent on the currently running Java virtual machine using the supplied attachment provider.
      private static void install​(ByteBuddyAgent.AttachmentProvider attachmentProvider, java.lang.String processId, java.lang.String argument, ByteBuddyAgent.AgentProvider agentProvider, boolean isNative)
      Installs a Java agent on a target VM.
      static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.AttachmentProvider attachmentProvider, ByteBuddyAgent.ProcessProvider processProvider)
      Installs an agent on the currently running Java virtual machine using the supplied attachment provider and process provider.
      static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.ProcessProvider processProvider)
      Installs an agent on the Java virtual machine resolved by the process provider.
      private static void installExternal​(ByteBuddyAgent.AttachmentProvider.Accessor.ExternalAttachment externalAttachment, java.lang.String processId, java.io.File agent, boolean isNative, java.lang.String argument)
      Installs a Java agent to the current VM via an external process.
      private static java.lang.String quote​(java.lang.String value)
      Quotes a value if it contains a white space.
      private static java.io.File trySelfResolve()
      Attempts to resolve the location of the Attacher class for a self-attachment.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LATENT_RESOLVE

        public static final java.lang.String LATENT_RESOLVE
        Indicates that the agent should not resolve it's own code location for a self-attachment.
        See Also:
        Constant Field Values
      • AGENT_CLASS_PROPERTY

        private static final java.lang.String AGENT_CLASS_PROPERTY
        The manifest property specifying the agent class.
        See Also:
        Constant Field Values
      • CAN_REDEFINE_CLASSES_PROPERTY

        private static final java.lang.String CAN_REDEFINE_CLASSES_PROPERTY
        The manifest property specifying the can redefine property.
        See Also:
        Constant Field Values
      • CAN_RETRANSFORM_CLASSES_PROPERTY

        private static final java.lang.String CAN_RETRANSFORM_CLASSES_PROPERTY
        The manifest property specifying the can retransform property.
        See Also:
        Constant Field Values
      • CAN_SET_NATIVE_METHOD_PREFIX

        private static final java.lang.String CAN_SET_NATIVE_METHOD_PREFIX
        The manifest property specifying the can set native method prefix property.
        See Also:
        Constant Field Values
      • MANIFEST_VERSION_VALUE

        private static final java.lang.String MANIFEST_VERSION_VALUE
        The manifest property value for the manifest version.
        See Also:
        Constant Field Values
      • BUFFER_SIZE

        private static final int BUFFER_SIZE
        The size of the buffer for copying the agent installer file into another jar.
        See Also:
        Constant Field Values
      • START_INDEX

        private static final int START_INDEX
        Convenience indices for reading and writing to the buffer to make the code more readable.
        See Also:
        Constant Field Values
      • END_OF_FILE

        private static final int END_OF_FILE
        Convenience indices for reading and writing to the buffer to make the code more readable.
        See Also:
        Constant Field Values
      • SUCCESSFUL_ATTACH

        private static final int SUCCESSFUL_ATTACH
        The status code expected as a result of a successful attachment.
        See Also:
        Constant Field Values
      • STATIC_MEMBER

        private static final java.lang.Object STATIC_MEMBER
        Base for access to a reflective member to make the code more readable.
      • BOOTSTRAP_CLASS_LOADER

        private static final java.lang.ClassLoader BOOTSTRAP_CLASS_LOADER
        Representation of the bootstrap ClassLoader.
      • WITHOUT_ARGUMENT

        private static final java.lang.String WITHOUT_ARGUMENT
        Represents a no-op argument for a dynamic agent attachment.
      • ATTACHER_FILE_NAME

        private static final java.lang.String ATTACHER_FILE_NAME
        The naming prefix of all artifacts for an attacher jar.
        See Also:
        Constant Field Values
      • CLASS_FILE_EXTENSION

        private static final java.lang.String CLASS_FILE_EXTENSION
        The file extension for a class file.
        See Also:
        Constant Field Values
      • JAR_FILE_EXTENSION

        private static final java.lang.String JAR_FILE_EXTENSION
        The file extension for a jar file.
        See Also:
        Constant Field Values
      • CLASS_PATH_ARGUMENT

        private static final java.lang.String CLASS_PATH_ARGUMENT
        The class path argument to specify the class path elements.
        See Also:
        Constant Field Values
      • JAVA_HOME

        private static final java.lang.String JAVA_HOME
        The Java property denoting the Java home directory.
        See Also:
        Constant Field Values
      • OS_NAME

        private static final java.lang.String OS_NAME
        The Java property denoting the operating system name.
        See Also:
        Constant Field Values
      • INSTRUMENTATION_METHOD

        private static final java.lang.String INSTRUMENTATION_METHOD
        The name of the method for reading the installer's instrumentation.
        See Also:
        Constant Field Values
      • FILE_PROTOCOL

        private static final java.lang.String FILE_PROTOCOL
        Represents the file URL protocol.
        See Also:
        Constant Field Values
      • UNAVAILABLE

        private static final java.lang.instrument.Instrumentation UNAVAILABLE
        An indicator variable to express that no instrumentation is available.
      • CANNOT_SELF_RESOLVE

        private static final java.io.File CANNOT_SELF_RESOLVE
        Represents a failed attempt to self-resolve a jar file location.
      • ATTACHMENT_TYPE_EVALUATOR

        private static final ByteBuddyAgent.AttachmentTypeEvaluator ATTACHMENT_TYPE_EVALUATOR
        The attachment type evaluator to be used for determining if an attachment requires an external process.
    • Constructor Detail

      • ByteBuddyAgent

        private ByteBuddyAgent()
        The agent provides only static utility methods and should not be instantiated.
    • Method Detail

      • getInstrumentation

        public static java.lang.instrument.Instrumentation getInstrumentation()

        Looks up the Instrumentation instance of an installed Byte Buddy agent. Note that this method implies reflective lookup and reflective invocation such that the returned value should be cached rather than calling this method several times.

        Note: This method throws an IllegalStateException If the Byte Buddy agent is not properly installed.

        Returns:
        The Instrumentation instance which is provided by an installed Byte Buddy agent.
      • attach

        public static void attach​(java.io.File agentJar,
                                  java.lang.String processId)

        Attaches the given agent Jar on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processId - The target process id.
      • attach

        public static void attach​(java.io.File agentJar,
                                  java.lang.String processId,
                                  java.lang.String argument)

        Attaches the given agent Jar on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processId - The target process id.
        argument - The argument to provide to the agent.
      • attach

        public static void attach​(java.io.File agentJar,
                                  java.lang.String processId,
                                  ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent Jar on the target process which must be a virtual machine process. This operation blocks until the attachment is complete. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processId - The target process id.
        attachmentProvider - The attachment provider to use.
      • attach

        public static void attach​(java.io.File agentJar,
                                  java.lang.String processId,
                                  java.lang.String argument,
                                  ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent Jar on the target process which must be a virtual machine process. This operation blocks until the attachment is complete.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processId - The target process id.
        argument - The argument to provide to the agent.
        attachmentProvider - The attachment provider to use.
      • attach

        public static void attach​(java.io.File agentJar,
                                  ByteBuddyAgent.ProcessProvider processProvider)

        Attaches the given agent Jar on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processProvider - A provider of the target process id.
      • attach

        public static void attach​(java.io.File agentJar,
                                  ByteBuddyAgent.ProcessProvider processProvider,
                                  java.lang.String argument)

        Attaches the given agent Jar on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processProvider - A provider of the target process id.
        argument - The argument to provide to the agent.
      • attach

        public static void attach​(java.io.File agentJar,
                                  ByteBuddyAgent.ProcessProvider processProvider,
                                  ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent Jar on the target process which must be a virtual machine process. This operation blocks until the attachment is complete. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processProvider - A provider of the target process id.
        attachmentProvider - The attachment provider to use.
      • attach

        public static void attach​(java.io.File agentJar,
                                  ByteBuddyAgent.ProcessProvider processProvider,
                                  java.lang.String argument,
                                  ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent Jar on the target process which must be a virtual machine process. This operation blocks until the attachment is complete.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentJar - The agent jar file.
        processProvider - A provider of the target process id.
        argument - The argument to provide to the agent.
        attachmentProvider - The attachment provider to use.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        java.lang.String processId)

        Attaches the given agent library on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent jar file.
        processId - The target process id.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        java.lang.String processId,
                                        java.lang.String argument)

        Attaches the given agent library on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processId - The target process id.
        argument - The argument to provide to the agent.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        java.lang.String processId,
                                        ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent library on the target process which must be a virtual machine process. This operation blocks until the attachment is complete. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processId - The target process id.
        attachmentProvider - The attachment provider to use.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        java.lang.String processId,
                                        java.lang.String argument,
                                        ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent library on the target process which must be a virtual machine process. This operation blocks until the attachment is complete.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processId - The target process id.
        argument - The argument to provide to the agent.
        attachmentProvider - The attachment provider to use.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        ByteBuddyAgent.ProcessProvider processProvider)

        Attaches the given agent library on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processProvider - A provider of the target process id.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        ByteBuddyAgent.ProcessProvider processProvider,
                                        java.lang.String argument)

        Attaches the given agent library on the target process which must be a virtual machine process. The default attachment provider is used for applying the attachment. This operation blocks until the attachment is complete. If the current VM does not supply any known form of attachment to a remote VM, an IllegalStateException is thrown.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processProvider - A provider of the target process id.
        argument - The argument to provide to the agent.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        ByteBuddyAgent.ProcessProvider processProvider,
                                        ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent library on the target process which must be a virtual machine process. This operation blocks until the attachment is complete. The agent is not provided an argument.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processProvider - A provider of the target process id.
        attachmentProvider - The attachment provider to use.
      • attachNative

        public static void attachNative​(java.io.File agentLibrary,
                                        ByteBuddyAgent.ProcessProvider processProvider,
                                        java.lang.String argument,
                                        ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Attaches the given agent library on the target process which must be a virtual machine process. This operation blocks until the attachment is complete.

        Important: It is only possible to attach to processes that are executed by the same operating system user.

        Parameters:
        agentLibrary - The agent library.
        processProvider - A provider of the target process id.
        argument - The argument to provide to the agent.
        attachmentProvider - The attachment provider to use.
      • install

        public static java.lang.instrument.Instrumentation install()

        Installs an agent on the currently running Java virtual machine. Unfortunately, this does not always work. The runtime installation of a Java agent is supported for:

        • JVM version 9+: For Java VM of at least version 9, the attachment API was moved into a module and the runtime installation is possible if the jdk.attach module is available to Byte Buddy which is typically only available for VMs shipped with a JDK.
        • OpenJDK / Oracle JDK / IBM J9 versions 8-: The installation for HotSpot is only possible when bundled with a JDK and requires a tools.jar bundled with the VM which is typically only available for JDK-versions of the JVM.
        • When running Linux and including the optional junixsocket-native-common depedency, Byte Buddy emulates a Unix socket connection to attach to the target VM.

        If an agent cannot be installed, an IllegalStateException is thrown.

        Important: This is a rather computation-heavy operation. Therefore, this operation is not repeated after an agent was successfully installed for the first time. Instead, the previous instrumentation instance is returned. However, invoking this method requires synchronization such that subsequently to an installation, getInstrumentation() should be invoked instead.

        Returns:
        An instrumentation instance representing the currently running JVM.
      • install

        public static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.AttachmentProvider attachmentProvider)

        Installs an agent on the currently running Java virtual machine using the supplied attachment provider.

        If an agent cannot be installed, an IllegalStateException is thrown.

        Important: This is a rather computation-heavy operation. Therefore, this operation is not repeated after an agent was successfully installed for the first time. Instead, the previous instrumentation instance is returned. However, invoking this method requires synchronization such that subsequently to an installation, getInstrumentation() should be invoked instead.

        Parameters:
        attachmentProvider - The attachment provider to use for the installation.
        Returns:
        An instrumentation instance representing the currently running JVM.
      • install

        public static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.ProcessProvider processProvider)

        Installs an agent on the Java virtual machine resolved by the process provider. Unfortunately, this does not always work. The runtime installation of a Java agent is supported for:

        • JVM version 9+: For Java VM of at least version 9, the attachment API was moved into a module and the runtime installation is possible if the jdk.attach module is available to Byte Buddy which is typically only available for VMs shipped with a JDK.
        • OpenJDK / Oracle JDK / IBM J9 versions 8-: The installation for HotSpot is only possible when bundled with a JDK and requires a tools.jar bundled with the VM which is typically only available for JDK-versions of the JVM.
        • When running Linux and including the optional junixsocket-native-common depedency, Byte Buddy emulates a Unix socket connection to attach to the target VM.

        If an agent cannot be installed, an IllegalStateException is thrown.

        Parameters:
        processProvider - The provider for the current JVM's process id.
        Returns:
        An instrumentation instance representing the currently running JVM.
      • install

        public static java.lang.instrument.Instrumentation install​(ByteBuddyAgent.AttachmentProvider attachmentProvider,
                                                                   ByteBuddyAgent.ProcessProvider processProvider)

        Installs an agent on the currently running Java virtual machine using the supplied attachment provider and process provider.

        If an agent cannot be installed, an IllegalStateException is thrown.

        Parameters:
        attachmentProvider - The attachment provider to use for the installation.
        processProvider - The provider for the current JVM's process id.
        Returns:
        An instrumentation instance representing the currently running JVM.
      • install

        private static void install​(ByteBuddyAgent.AttachmentProvider attachmentProvider,
                                    java.lang.String processId,
                                    java.lang.String argument,
                                    ByteBuddyAgent.AgentProvider agentProvider,
                                    boolean isNative)
        Installs a Java agent on a target VM.
        Parameters:
        attachmentProvider - The attachment provider to use.
        processId - The process id of the target JVM process.
        argument - The argument to provide to the agent.
        agentProvider - The agent provider for the agent jar or library.
        isNative - true if the agent is native.
      • installExternal

        private static void installExternal​(ByteBuddyAgent.AttachmentProvider.Accessor.ExternalAttachment externalAttachment,
                                            java.lang.String processId,
                                            java.io.File agent,
                                            boolean isNative,
                                            java.lang.String argument)
                                     throws java.lang.Exception
        Installs a Java agent to the current VM via an external process. This is typically required starting with OpenJDK 9 when the jdk.attach.allowAttachSelf property is set to false what is the default setting.
        Parameters:
        externalAttachment - A description of the external attachment.
        processId - The process id of the current process.
        agent - The Java agent to install.
        isNative - true if the agent is native.
        argument - The argument to provide to the agent or null if no argument should be supplied.
        Throws:
        java.lang.Exception - If an exception occurs during the attachment or the external process fails the attachment.
      • trySelfResolve

        private static java.io.File trySelfResolve()
        Attempts to resolve the location of the Attacher class for a self-attachment. Doing so avoids the creation of a temporary jar file.
        Returns:
        The self-resolved jar file or null if the jar file cannot be located.
      • quote

        private static java.lang.String quote​(java.lang.String value)
        Quotes a value if it contains a white space.
        Parameters:
        value - The value to quote.
        Returns:
        The value being quoted if necessary.
      • doGetInstrumentation

        private static java.lang.instrument.Instrumentation doGetInstrumentation()
        Performs the actual lookup of the Instrumentation from an installed Byte Buddy agent.
        Returns:
        The Byte Buddy agent's Instrumentation instance.