建設自己的探查,我使用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
運行方式相同的用戶,贏了,謝謝你。 – 2013-05-06 18:11:29
與同一個用戶一起工作!非常感謝! – Gonzalo 2013-09-09 18:07:22
我使用的根,但得到以下錯誤。不支持附:: ERROR無法打開套接字文件:目標進程沒有響應或HotSpot虛擬機未裝載 com.sun.tools.attach.AttachNotSupportedException:無法打開套接字文件:目標進程沒有響應或HotSpot虛擬機未裝載 – 2016-07-03 13:25:33