代码之家  ›  专栏  ›  技术社区  ›  Ramesh Subramanian

使用Open JDK 10.0.2时,VirtualMachine attach抛出com.sun.tools.attach.AgentLoadException:0

  •  0
  • Ramesh Subramanian  · 技术社区  · 5 年前

    com.sun.tools.attach.VirtualMachine Java API。我的目标应用程序(Tomcat)正在使用Oracle Hot Spot 1.7.0_80版运行。我正试图通过另一个使用Open JDK 10.0.2的Java程序(在同一台机器上)的动态连接来连接tomcat。我正在使用下面的代码

    ...............
    String agentPath = Agent.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    agentPath = convertFileSeparators(agentPath, File.separatorChar);
    String agentInstallDir = agentPath.substring(0, agentPath.lastIndexOf(File.separator));
    System.out.println("My Java agent installed on the location :"+agentPath);
    StringBuilder sb = new StringBuilder();
    sb.append("MY_RELIC_HOME").append("=").append(agentInstallDir);
    if (agentArgs != null) {
         sb.append(",").append(agentArgs);
    }
    com.sun.tools.attach.VirtualMachine virtualMachine =  com.sun.tools.attach.VirtualMachine.attach(vmID);
    virtualMachine.loadAgent(agentPath, sb.toString());
    .........
    

    我可以成功地附加到目标应用程序,但在附加后,在附加程序(运行open JDK 10.0.2)中遇到以下异常。

    com.sun.tools.attach.AgentLoadException: 0
        at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:104)
        at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:115)
        at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:139)
        at com.eg.agent.Agent.main(Agent.java:582)
    

    582号线上的代码是 virtualMachine.loadAgent(agentPath, sb.toString());

    如果我使用Java 7或8运行attach程序,没有异常,attach就是成功的。

    com.sun.tools.attach.AgentLoadException: 0 使用JDK 10时发生异常?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Ramesh Subramanian    5 年前

    在查找了 sun.tools.attach.HotSpotVirtualMachine

    以下代码来自1.8 这表明 result 为零表示VMAttach成功。

    private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
        throws AgentLoadException, AgentInitializationException, IOException
    {
        InputStream in = execute("load",
                                 agentLibrary,
                                 isAbsolute ? "true" : "false",
                                 options);
        try {
            int result = readInt(in);
            if (result != 0) {
                throw new AgentInitializationException("Agent_OnAttach failed", result);
            }
        } finally {
            in.close();
    
        }
    }
    

    下面的代码来自java 10 在VM Attach中,当 结果 "return code: 0" (但是在1.8中 0 ).

    private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) 
        throws AgentLoadException, AgentInitializationException, IOException 
    { 
        String msgPrefix = "return code: "; 
        InputStream in = execute("load", 
                                 agentLibrary, 
                                 isAbsolute ? "true" : "false", 
                                 options); 
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { 
            String result = reader.readLine(); 
            if (result == null) { 
                throw new AgentLoadException("Target VM did not respond"); 
            } else if (result.startsWith(msgPrefix)) { 
                int retCode = Integer.parseInt(result.substring(msgPrefix.length())); 
                if (retCode != 0) { 
                    throw new AgentInitializationException("Agent_OnAttach failed", retCode); 
                } 
            } else { 
                throw new AgentLoadException(result); 
            } 
        } 
    } 
    

    在我的例子中,Java7/8代码(目标应用程序)返回 作为 0个 (这意味着VM Attach是成功的)到Java 10代码(VM Attach code),期望结果应该是 “返回代码:0”

    推荐文章