com.sun.tools.attach.VirtualMachine Java Examples

The following examples show how to use com.sun.tools.attach.VirtualMachine. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PermissionTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
        throw new RuntimeException("Test configuration error - no security manager set");
    }

    String pid = args[0];
    boolean shouldFail = Boolean.parseBoolean(args[1]);

    try {
        VirtualMachine.attach(pid).detach();
        if (shouldFail) {
            throw new RuntimeException("SecurityException should be thrown");
        }
        System.out.println(" - attached to target VM as expected.");
    } catch (Exception x) {
        // AttachNotSupportedException thrown when no providers can be loaded
        if (shouldFail && ((x instanceof AttachNotSupportedException) ||
            (x instanceof SecurityException))) {
            System.out.println(" - exception thrown as expected.");
        } else {
            throw x;
        }
    }
}
 
Example #2
Source File: LocalVirtualMachine.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #3
Source File: LocalVirtualMachine.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #4
Source File: PermissionTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
        throw new RuntimeException("Test configuration error - no security manager set");
    }

    String pid = args[0];
    boolean shouldFail = Boolean.parseBoolean(args[1]);

    try {
        VirtualMachine.attach(pid).detach();
        if (shouldFail) {
            throw new RuntimeException("SecurityException should be thrown");
        }
        System.out.println(" - attached to target VM as expected.");
    } catch (Exception x) {
        // AttachNotSupportedException thrown when no providers can be loaded
        if (shouldFail && ((x instanceof AttachNotSupportedException) ||
            (x instanceof SecurityException))) {
            System.out.println(" - exception thrown as expected.");
        } else {
            throw x;
        }
    }
}
 
Example #5
Source File: BsdAttachProvider.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new BsdVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #6
Source File: VirtualMachineMetrics.java    From spring-boot-graal-feature with Apache License 2.0 6 votes vote down vote up
public static Map<String, Long> fetch(String pid) {
    if (pid == null) {
        return Collections.emptyMap();
    }
    try {
        VirtualMachine vm = VirtualMachine.attach(pid);
        vm.startLocalManagementAgent();
        String connectorAddress = vm.getAgentProperties()
                .getProperty(CONNECTOR_ADDRESS);
        JMXServiceURL url = new JMXServiceURL(connectorAddress);
        JMXConnector connector = JMXConnectorFactory.connect(url);
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        gc(connection);
        Map<String, Long> metrics = new HashMap<>(
                new BufferPools(connection).getMetrics());
        metrics.putAll(new Threads(connection).getMetrics());
        metrics.putAll(new Classes(connection).getMetrics());
        vm.detach();
        return metrics;
    }
    catch (Exception e) {
        return Collections.emptyMap();
    }
}
 
Example #7
Source File: LinuxAttachProvider.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new LinuxVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #8
Source File: AixAttachProvider.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new AixVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #9
Source File: StartManagementAgent.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example #10
Source File: JMap.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void dump(String pid, String options) throws IOException {
    // parse the options to get the dump filename
    String filename = parseDumpOptions(options);
    if (filename == null) {
        usage(1);  // invalid options or no filename
    }

    // get the canonical path - important to avoid just passing
    // a "heap.bin" and having the dump created in the target VM
    // working directory rather than the directory where jmap
    // is executed.
    filename = new File(filename).getCanonicalPath();

    // dump live objects only or not
    boolean live = isDumpLiveObjects(options);

    VirtualMachine vm = attach(pid);
    System.out.println("Dumping heap to " + filename + " ...");
    InputStream in = ((HotSpotVirtualMachine)vm).
        dumpHeap((Object)filename,
                 (live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
    drain(vm, in);
}
 
Example #11
Source File: SolarisAttachProvider.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new SolarisVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #12
Source File: AttachSelf.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        String value = System.getProperty("jdk.attach.allowAttachSelf");
        boolean canAttachSelf = (value != null) && !value.equals("false");

        String vmid = "" + ProcessHandle.current().pid();

        VirtualMachine vm = null;
        try {
            vm = VirtualMachine.attach(vmid);
            if (!canAttachSelf)
                throw new RuntimeException("Attached to self not expected");
        } catch (IOException ioe) {
            if (canAttachSelf)
                throw ioe;
        } finally {
            if (vm != null) vm.detach();
        }

    }
 
Example #13
Source File: JMap.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static VirtualMachine attach(String pid) {
    try {
        return VirtualMachine.attach(pid);
    } catch (Exception x) {
        String msg = x.getMessage();
        if (msg != null) {
            System.err.println(pid + ": " + msg);
        } else {
            x.printStackTrace();
        }
        if ((x instanceof AttachNotSupportedException) && haveSA()) {
            System.err.println("The -F option can be used when the " +
              "target process is not responding");
        }
        System.exit(1);
        return null; // keep compiler happy
    }
}
 
Example #14
Source File: SolarisAttachProvider.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new SolarisVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #15
Source File: LocalVirtualMachine.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #16
Source File: LocalVirtualMachine.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #17
Source File: LinuxAttachProvider.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new LinuxVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #18
Source File: StartManagementAgent.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example #19
Source File: PermissionTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
        throw new RuntimeException("Test configuration error - no security manager set");
    }

    String pid = args[0];
    boolean shouldFail = Boolean.parseBoolean(args[1]);

    try {
        VirtualMachine.attach(pid).detach();
        if (shouldFail) {
            throw new RuntimeException("SecurityException should be thrown");
        }
        System.out.println(" - attached to target VM as expected.");
    } catch (Exception x) {
        // AttachNotSupportedException thrown when no providers can be loaded
        if (shouldFail && ((x instanceof AttachNotSupportedException) ||
            (x instanceof SecurityException))) {
            System.out.println(" - exception thrown as expected.");
        } else {
            throw x;
        }
    }
}
 
Example #20
Source File: LocalVirtualMachine.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #21
Source File: LocalVirtualMachine.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #22
Source File: LocalVirtualMachine.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #23
Source File: JMap.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static VirtualMachine attach(String pid) {
    try {
        return VirtualMachine.attach(pid);
    } catch (Exception x) {
        String msg = x.getMessage();
        if (msg != null) {
            System.err.println(pid + ": " + msg);
        } else {
            x.printStackTrace();
        }
        if ((x instanceof AttachNotSupportedException) && haveSA()) {
            System.err.println("The -F option can be used when the " +
              "target process is not responding");
        }
        System.exit(1);
        return null; // keep compiler happy
    }
}
 
Example #24
Source File: JMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static VirtualMachine attach(String pid) {
    try {
        return VirtualMachine.attach(pid);
    } catch (Exception x) {
        String msg = x.getMessage();
        if (msg != null) {
            System.err.println(pid + ": " + msg);
        } else {
            x.printStackTrace();
        }
        if ((x instanceof AttachNotSupportedException) && haveSA()) {
            System.err.println("The -F option can be used when the " +
              "target process is not responding");
        }
        System.exit(1);
        return null; // keep compiler happy
    }
}
 
Example #25
Source File: JMap.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void dump(String pid, String options) throws IOException {
    // parse the options to get the dump filename
    String filename = parseDumpOptions(options);
    if (filename == null) {
        usage(1);  // invalid options or no filename
    }

    // get the canonical path - important to avoid just passing
    // a "heap.bin" and having the dump created in the target VM
    // working directory rather than the directory where jmap
    // is executed.
    filename = new File(filename).getCanonicalPath();

    // dump live objects only or not
    boolean live = isDumpLiveObjects(options);

    VirtualMachine vm = attach(pid);
    System.out.println("Dumping heap to " + filename + " ...");
    InputStream in = ((HotSpotVirtualMachine)vm).
        dumpHeap((Object)filename,
                 (live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
    drain(vm, in);
}
 
Example #26
Source File: AixAttachProvider.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)
    throws AttachNotSupportedException, IOException
{
    if (vmd.provider() != this) {
        throw new AttachNotSupportedException("provider mismatch");
    }
    // To avoid re-checking if the VM if attachable, we check if the descriptor
    // is for a hotspot VM - these descriptors are created by the listVirtualMachines
    // implementation which only returns a list of attachable VMs.
    if (vmd instanceof HotSpotVirtualMachineDescriptor) {
        assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();
        checkAttachPermission();
        return new AixVirtualMachine(this, vmd.id());
    } else {
        return attachVirtualMachine(vmd.id());
    }
}
 
Example #27
Source File: LocalVirtualMachine.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example #28
Source File: VirtualMachineMetrics.java    From spring-init with Apache License 2.0 6 votes vote down vote up
public static Map<String, Long> fetch(String pid) {
	if (pid == null) {
		return Collections.emptyMap();
	}
	try {
		VirtualMachine vm = VirtualMachine.attach(pid);
		vm.startLocalManagementAgent();
		String connectorAddress = vm.getAgentProperties()
				.getProperty(CONNECTOR_ADDRESS);
		JMXServiceURL url = new JMXServiceURL(connectorAddress);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		gc(connection);
		Map<String, Long> metrics = new HashMap<>(
				new BufferPools(connection).getMetrics());
		metrics.putAll(new Threads(connection).getMetrics());
		metrics.putAll(new Classes(connection).getMetrics());
		vm.detach();
		return metrics;
	}
	catch (Exception e) {
		return Collections.emptyMap();
	}
}
 
Example #29
Source File: LocalVirtualMachine.java    From jvmtop with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #30
Source File: JStack.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runThreadDump(String pid, String args[]) throws Exception {
    VirtualMachine vm = null;
    try {
        vm = VirtualMachine.attach(pid);
    } catch (Exception x) {
        String msg = x.getMessage();
        if (msg != null) {
            System.err.println(pid + ": " + msg);
        } else {
            x.printStackTrace();
        }
        if ((x instanceof AttachNotSupportedException) &&
            (loadSAClass() != null)) {
            System.err.println("The -F option can be used when the target " +
                "process is not responding");
        }
        System.exit(1);
    }

    // Cast to HotSpotVirtualMachine as this is implementation specific
    // method.
    InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);

    // read to EOF and just print output
    byte b[] = new byte[256];
    int n;
    do {
        n = in.read(b);
        if (n > 0) {
            String s = new String(b, 0, n, "UTF-8");
            System.out.print(s);
        }
    } while (n > 0);
    in.close();
    vm.detach();
}