13

构建我自己的分析器,我使用JVMTI API 来构建本机库代理。通过使用附加参数 -agentlib 可以与 JVM 一起启动此代理。此外还有Attach API,它允许将代理注入到正在运行的 JVM 中。我想使用以下代码在我的分析器中实现此功能:

try {
    String pid = VirtualMachine.list().get(0).id();
    VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgentLibrary("agent");
} catch (AgentLoadException e1) {
    e1.printStackTrace();
} catch (AgentInitializationException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
} catch (AttachNotSupportedException e) {
    e.printStackTrace();
}

它有什么作用?从所有可用的正在运行的虚拟机 ( VirtualMachine.list()) 中,我选择第一个,附加到它并尝试将我的代理加载到其中。可以找到名为 libagent.so 的 UNIX 系统上的代理,但在尝试加载代理时会引发以下异常:

com.sun.tools.attach.AttachNotSupportedException:
   Unable to open socket file:
      target process not responding or HotSpot VM not loaded.

查看源代码,抛出此异常,因为它找不到名为.java_pid<pid>. 我在文档中没有找到很多关于这种文件的信息。我经常听说这种文件不再使用了,但我正在运行 Java 1.6。

我还尝试附加到其他 JVM,实际上我保持这个附加过程是动态的,出于测试原因,我只是尝试附加到任何 JVM。


这是导致异常的代码,取自sun.tools.attach: LinuxVirtualMachine.java

    // Return the socket file for the given process.
    // Checks working directory of process for .java_pid<pid>. If not
    // found it looks in /tmp.
    private String findSocketFile(int pid) {
       // First check for a .java_pid<pid> file in the working directory
       // of the target process
       String fn = ".java_pid" + pid;
       String path = "/proc/" + pid + "/cwd/" + fn;
       File f = new File(path);
       if (!f.exists()) {
           // Not found, so try /tmp
           path = "/tmp/" + fn;
           f = new File(path);
           if (!f.exists()) {
               return null;            // not found
           }
       }
       return path;
   }

它说,它正在从根/proc/<pid>目录中查找。查看 JDK7 的变更集,他们似乎正在将代码JDK7 变更集更改为 LinuxVirtualMachine

4

2 回答 2

29

我遇到了同样的问题。

线程“主”com.sun.tools.attach.AttachNotSupportedException 中的异常:无法打开套接字文件:目标进程没有响应或 HotSpot VM 未加载

发现该解决方案进行了一些繁重的谷歌搜索。

第一个答案来自http://www.jvmmonitor.org/doc/index.html 。似乎有一个错误:

如果您看到附加消息“无法打开套接字文件:目标进程未响应或未加载热点虚拟机”,则您的应用程序没有响应创建像 /tmp/.java_pid1234 这样的套接字文件(例如,由于挂起,文件系统权限),或者 JVM Monitor 无法找到创建的套接字文件(例如,由于错误 7009828)。

然后经过更多搜索,我在 github 上找到了另一个工具的对话,该工具具有相同的症状“无法打开套接字文件”(https://github.com/rhuss/jolokia/issues/34):

jgreen:原因:com.sun.tools.attach.AttachNotSupportedException:无法打开套接字文件:目标进程没有响应或 HotSpot VM 未加载

jgreen:是的,我有它的工作,但只有在作为与 activemq 完全相同的用户启动时。根不工作

最后一块是解决方案。这个 .attach 调用成功的唯一方法是运行调用 attach 的 java 代码,该用户与拥有运行 jvm 的进程的用户相同。在我的情况下,它是 activemq 用户。

System.out.println("HEAP: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());

HEAP: init = 27127296(26491K) used = 3974200(3881K) committed = 26345472(25728K) max = 675086336(659264K)
于 2012-12-20T14:31:14.153 回答
3

怀疑您可能正在-Djava.io.tmpdir为正在运行的 JVM指定它它位于 Java 6 Update 23 或 24 上。如果是这种情况,您只需为正在运行的实例升级到 Update 25。

我见过的对这个问题的唯一参考是Jstack 和 Jstat 停止升级到 JDK6u23。我肯定已经看到更新 23 和 jstack 失败的相同问题,它在 23 之前运行良好,并再次与 25 一起工作。我也刚刚尝试过VirtualMachine.attach(pid)23,如果-Djava.io.tmpdir使用它会失败。它适用于 25。

于 2011-04-29T06:08:51.990 回答