【问题标题】:VirtualMachine attach throws com.sun.tools.attach.AgentLoadException: 0 when using Open JDK 10.0.2使用 Open JDK 10.0.2 时,VirtualMachine attach 抛出 com.sun.tools.attach.AgentLoadException: 0
【发布时间】:2019-01-24 06:19:27
【问题描述】:

我在使用com.sun.tools.attach.VirtualMachineJava 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());
.........

我能够成功附加到目标应用程序,但附加后在我的附加程序(运行打开的 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 运行我的附加程序,没有例外并且附加成功。

为什么在使用 JDK 10 时会出现com.sun.tools.attach.AgentLoadException: 0 异常?

【问题讨论】:

    标签: java javaagents java-attach-api


    【解决方案1】:

    发现问题,在java 10和8的两个版本上查看sun.tools.attach.HotSpotVirtualMachine的源代码后。方法loadAgentLibrary(...)在10和8之间完全不同。

    以下代码来自 1.8,表明result 的值为 0 表示 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,当result 的值为"return code: 0"(但在 1.8 中它只是0)时,VM Attach 成功。

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

    在我的情况下,Java 7/8 代码(目标应用程序)将 result 作为 0(这意味着 VM Attach 成功)返回到 Java 10 代码(VM Attach 代码),预期结果应该是 @987654330 @

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2023-03-17
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多