2011-04-24 140 views
11

建設自己的探查,我使用JVMTI API來構建本地庫劑。此代理可以使用添加參數-agentlib與JVM一起啓動。此外,還有Attach API,它允許將代理注入正在運行的JVM。我想使用以下代碼將此功能部署到我的分析器中:AttachNotSupportedException由於缺少java_pid文件附加API

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 Changeset to LinuxVirtualMachine

回答

25

我經歷了同樣的問題。

異常線程「main」 com.sun.tools.attach.AttachNotSupportedException:無法打開套接字文件:目標進程不響應或HotSpot虛擬機未加載

該溶液在做一些重使然。

首先答案出來http://www.jvmmonitor.org/doc/index.html。似乎有一個bug:

If you see the additional message "Unable to open socket file: target process not responding or Hotspot VM not loaded", either your application didn't respond creating a socket file like /tmp/.java_pid1234 (e.g. due to hangup, file system permission), or JVM Monitor was not able to find the created socket file (e.g. due to the bug 7009828).

再經過一些更多的搜索,我發現另一個工具,它有同樣的症狀「無法打開套接字文件」在github上的對話(https://github.com/rhuss/椒/問題/ 34):

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

jgreen: right I have it working but only when lauching as the exact same user as activemq. root does not work

這最後一塊是該溶液中。這是唯一的方法。附上電話會成功是通過運行調用連接作爲相同用戶如誰擁有運行JVM進程的一個Java代碼。在我的情況下,它是activemq用戶。

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

HEAP: init = 27127296(26491K) used = 3974200(3881K) committed = 26345472(25728K) max = 675086336(659264K) 
+3

運行方式相同的用戶,贏了,謝謝你。 – 2013-05-06 18:11:29

+0

與同一個用戶一起工作!非常感謝! – Gonzalo 2013-09-09 18:07:22

+0

我使用的根,但得到以下錯誤。不支持附:: ERROR無法打開套接字文件:目標進程沒有響應或HotSpot虛擬機未裝載 com.sun.tools.attach.AttachNotSupportedException:無法打開套接字文件:目標進程沒有響應或HotSpot虛擬機未裝載 – 2016-07-03 13:25:33

3

嫌疑,你可能會爲你的運行JVM 這是Java 6的被指定-Djava.io.tmpdir更新23或24。如果是這樣的你只需要升級到更新25的運行實例。

唯一的參考這個問題,我所看到的是Jstack and Jstat stopped working with upgrade to JDK6u23。我已經看到更新23和jstack失敗的相同問題,它在23之前正常工作,並再次與25一起工作。我也試過對23的VirtualMachine.attach(pid),如果使用-Djava.io.tmpdir,它會失敗。它與25