private InputStream executeCommand(String cmd, Object ... args) throws IOException { try { return execute(cmd, args); } catch (AgentLoadException x) { throw new InternalError("Should not get here"); } }
public InputStream heapHisto(Object... args) throws IOException { return executeCommand("inspectheap", args); }
@Override public void loadAgentLibrary(String agentLibrary, String options) throws AgentLoadException, AgentInitializationException, IOException { loadAgentLibrary(agentLibrary, false, options); }
String selfName = ManagementFactory.getRuntimeMXBean().getName(); final int selfPid = Integer.valueOf(selfName.substring(0, selfName.indexOf('@'))); HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(Integer.toString(selfPid)); InputStream sDump = vm.remoteDataDump(new Object[]{"-l"}); // lowercase L for lock dump
public static byte[] getThreadDump(int pid) throws AttachNotSupportedException, IOException { HotSpotVirtualMachine virtualMachine = null; try { virtualMachine = (HotSpotVirtualMachine) VirtualMachine.attach(String.valueOf(pid)); try (InputStream in = virtualMachine.remoteDataDump()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int count; byte[] buf = new byte[256]; while ((count = in.read(buf)) != -1) { baos.write(buf, 0, count); } return baos.toByteArray(); } } finally { if (virtualMachine != null) { try { virtualMachine.detach(); } catch (IOException ignore) { } } } } public static String getThreadDumpAsString(int pid) throws AttachNotSupportedException, IOException {
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(); } }
@Override public String perform(VirtualMachine vm) throws Exception { StringWriter sw = new StringWriter(); InputStream is = ((HotSpotVirtualMachine)vm).dumpHeap(args); try { Reader r = new InputStreamReader(is); char[] buf = new char[16 << 10]; while(true) { int m = r.read(buf); if (m < 0) { break; } sw.write(buf, 0, m); } return sw.toString(); } finally { try { is.close(); } catch(IOException e) { // ignore } } } }
@Override public Void perform(VirtualMachine vm) throws Exception { InputStream is = ((HotSpotVirtualMachine)vm).remoteDataDump(args); try { Reader r = new InputStreamReader(is); CharBuffer cb = CharBuffer.allocate(16 << 10); while(true) { int m = r.read(cb.array()); if (m < 0) { break; } cb.limit(m); writer.append(cb); cb.clear(); } return null; } finally { try { is.close(); } catch(IOException e) { // ignore } } } }
/** * Load agent library. * If isAbsolute is true then the agent library is the absolute path to the library and thus * will not be expanded in the target VM. * If isAbsolute is false then the agent library is just a library name and it will be expended * in the target VM. */ 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(); } }
public InputStream printFlag(String name) throws IOException { return executeCommand("printflag", name); }
InputStream in = hsvm.remoteDataDump("-l");
/** * Load agent library - library name will be expanded in target VM. */ @Override public void loadAgentLibrary(String agentLibrary, String options) throws AgentLoadException, AgentInitializationException, IOException { loadAgentLibrary(agentLibrary, false, options); }
/** * Convenience method for simple commands. */ private InputStream executeCommand(String cmd, Object... args) throws IOException { try { return execute(cmd, args); } catch (AgentLoadException ignore) { throw new InternalError("Should not get here"); } }
public InputStream remoteDataDump(Object... args) throws IOException { return executeCommand("threaddump", args); }
InputStream in = ((HotSpotVirtualMachine) vm).remoteDataDump((Object[]) args);
/** * Load agent - absolute path of library provided to target VM. */ @Override public void loadAgentPath(String agentLibrary, String options) throws AgentLoadException, AgentInitializationException, IOException { loadAgentLibrary(agentLibrary, true, options); }
public InputStream dumpHeap(Object... args) throws IOException { return executeCommand("dumpheap", args); }
@Override public void loadAgentPath(String agentLibrary, String options) throws AgentLoadException, AgentInitializationException, IOException { loadAgentLibrary(agentLibrary, true, options); }
public InputStream setFlag(String name, String value) throws IOException { return executeCommand("setflag", name, value); }
loadAgentLibrary("instrument", args); } catch (AgentLoadException x) { throw new InternalError("instrument library is missing in target VM");