简体   繁体   中英

Attach an agent remotely using Bytebuddy

I am trying to attach an agent for a process using bytebuddy .I found that we can use ByteBuddyAgent.attach(file,"18467"); for this. But when i am trying to do this following errors occurring.

This is agent i used

File file = (new File("Agent.jar"));

Error

java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@e26db604
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:378)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:227)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:202)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:189)
    at common.netty.echo.EchoHttpServer.main(EchoHttpServer.java:95)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.bytebuddy.agent.Attacher.install(Attacher.java:77)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:373)
    ... 4 more
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
    at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:106)
    at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63)
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
    ... 10 more 

Any idea for this ? and When i try to use this attachment from another agent as follows

public class Agent {

    public static void premain(String args, Instrumentation instrumentation) {

        System.out.println("Premain");
        File file ;
        try {

                file = (new File("Agent.jar"));
                ByteBuddyAgent.attach(file,"18467");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

    }

I am getting this error

Error

java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@423f0955
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:378)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:227)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:202)
    at net.bytebuddy.agent.ByteBuddyAgent.attach(ByteBuddyAgent.java:189)
    at common.netty.echo.Agent.premain(Agent.java:111)
    at common.netty.echo.EchoHttpServer.<clinit>(EchoHttpServer.java:56)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.bytebuddy.agent.Attacher.install(Attacher.java:77)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:373)
    ... 5 more
Caused by: java.lang.UnsatisfiedLinkError: Native Library /usr/lib/jvm/java-8-oracle/jre/lib/amd64/libattach.so already loaded in another classloader
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1845)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at sun.tools.attach.LinuxVirtualMachine.<clinit>(LinuxVirtualMachine.java:342)
    at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63)
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
    ... 11 more

Any suggestion on this!!

As for the first error message: does the target VM run the attachment listener? You can try to force starting the thread by setting -XX:+StartAttachListener which should be set by default. What JVM version are you running?

The other error message indicates that the attachment library was already loaded by another class loader. See this answer for further details: java.lang.UnsatisfiedLinkError: Native Library XXX.so already loaded in another classloader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM