if (vm != null) { try { vm.detach(); } catch (IOException e) { tty.println(e.getMessage());
private void attachAgent(final String targetJvmPid, final String agentJarPath, final String cfg) throws Exception { VirtualMachine vmObj = null; try { vmObj = VirtualMachine.attach(targetJvmPid); if (vmObj != null) { vmObj.loadAgent(agentJarPath, cfg); } } finally { if (null != vmObj) { vmObj.detach(); } } }
@Override public int getTaskPort(BTraceTask task) { VirtualMachine vm = null; try { vm = VirtualMachine.attach(String.valueOf(task.getPid())); String portStr = vm.getSystemProperties().getProperty(PORT_PROPERTY); return portStr != null ? Integer.parseInt(portStr) : findFreePort(); } catch (AttachNotSupportedException ex) { Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { if (vm != null) { try { vm.detach(); } catch (IOException e) { LOGGER.log(Level.SEVERE, null, e); } } } return findFreePort(); }
public static LocalVirtualMachine getDelegateMachine(VirtualMachine vm) throws IOException { // privileges. boolean attachable = false; String address = null; String name = String.valueOf(vm.id()); // default display name to pid attachable = true; Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach(); return new LocalVirtualMachine(Integer.parseInt(vm.id()), name, attachable, address); }
public static JMXServiceURL extractJMXServiceURLFromPid(String pid) throws IOException { try { VirtualMachine vm = VirtualMachine.attach(pid); try { String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); if (connectorAddress == null) { String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar"; vm.loadAgent(agent); connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); } return new JMXServiceURL(connectorAddress); } finally { vm.detach(); } } catch(Exception e) { throw new IOException(e); } }
VirtualMachine vm = VirtualMachine.attach(pid); vm.loadAgent(agentJar.getAbsolutePath(), null); vm.detach();
VirtualMachine vm = VirtualMachine.attach(pid); vm.loadAgent(agentJar.getAbsolutePath(), null); vm.detach();
public static LocalVirtualMachine getLocalVirtualMachine(int vmid) throws Exception { Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines(); LocalVirtualMachine lvm = map.get(vmid); if (lvm == null) { // Check if the VM is attachable but not included in the list // if it's running with a different security context. // For example, Windows services running // local SYSTEM account are attachable if you have Adminstrator // privileges. boolean attachable = false; String address = null; String name = String.valueOf(vmid); // default display name to pid VirtualMachine vm = VirtualMachine.attach(name); attachable = true; Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach(); lvm = new LocalVirtualMachine(vmid, name, attachable, address); } return lvm; }
@Test public void attachToSelfIsOk() throws Exception { String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); int p = nameOfRunningVM.indexOf('@'); String pid = nameOfRunningVM.substring(0, p); VirtualMachine vm = VirtualMachine.attach(pid); vm.detach(); } }
Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach();
public void close() throws IOException { vm.detach(); } });
public static void loadAgent() { logger.info("dynamically loading javaagent"); String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); int p = nameOfRunningVM.indexOf('@'); String pid = nameOfRunningVM.substring(0, p); try { VirtualMachine vm = VirtualMachine.attach(pid); vm.loadAgent(jarFilePath, ""); vm.detach(); } catch (Exception e) { throw new RuntimeException(e); } }