使用Bytebuddy远程连接代理

时间:2018-02-12 11:40:22

标签: java performance byte-buddy

我正在尝试使用bytebuddy为进程附加代理。我发现我们可以使用ByteBuddyAgent.attach(file,"18467");。但是当我在发生错误时尝试这样做时。

这是我用过的代理

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

错误

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 

对此有何想法?当我尝试使用来自其他代理的附件时如下

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();
            }

    }

我收到此错误

错误

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

对此有任何建议!!

1 个答案:

答案 0 :(得分:0)

至于第一条错误消息:目标VM是否运行附件监听器?您可以尝试通过设置默认设置的<!-- Sidebar Header --> <div class="sidebar-header text-center"> <h1 id="notification"><%= title %></h1> </div> 来强制启动线程。你在运行什么JVM版本?

另一条错误消息表明附件库已由另一个类加载器加载。有关详细信息,请参阅此答案:java.lang.UnsatisfiedLinkError: Native Library XXX.so already loaded in another classloader