Search in sources :

Example 1 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project jdk8u_jdk by JetBrains.

the class VirtualMachine method attach.

/**
     * Attaches to a Java virtual machine.
     *
     * <p> This method obtains the list of attach providers by invoking the
     * {@link com.sun.tools.attach.spi.AttachProvider#providers()
     * AttachProvider.providers()} method. It then iterates overs the list
     * and invokes each provider's {@link
     * com.sun.tools.attach.spi.AttachProvider#attachVirtualMachine(java.lang.String)
     * attachVirtualMachine} method in turn. If a provider successfully
     * attaches then the iteration terminates, and the VirtualMachine created
     * by the provider that successfully attached is returned by this method.
     * If the <code>attachVirtualMachine</code> method of all providers throws
     * {@link com.sun.tools.attach.AttachNotSupportedException AttachNotSupportedException}
     * then this method also throws <code>AttachNotSupportedException</code>.
     * This means that <code>AttachNotSupportedException</code> is thrown when
     * the identifier provided to this method is invalid, or the identifier
     * corresponds to a Java virtual machine that does not exist, or none
     * of the providers can attach to it. This exception is also thrown if
     * {@link com.sun.tools.attach.spi.AttachProvider#providers()
     * AttachProvider.providers()} returns an empty list. </p>
     *
     * @param   id
     *          The abstract identifier that identifies the Java virtual machine.
     *
     * @return  A VirtualMachine representing the target VM.
     *
     * @throws  SecurityException
     *          If a security manager has been installed and it denies
     *          {@link com.sun.tools.attach.AttachPermission AttachPermission}
     *          <tt>("attachVirtualMachine")</tt>, or another permission
     *          required by the implementation.
     *
     * @throws  AttachNotSupportedException
     *          If the <code>attachVirtualmachine</code> method of all installed
     *          providers throws <code>AttachNotSupportedException</code>, or
     *          there aren't any providers installed.
     *
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @throws  NullPointerException
     *          If <code>id</code> is <code>null</code>.
     */
public static VirtualMachine attach(String id) throws AttachNotSupportedException, IOException {
    if (id == null) {
        throw new NullPointerException("id cannot be null");
    }
    List<AttachProvider> providers = AttachProvider.providers();
    if (providers.size() == 0) {
        throw new AttachNotSupportedException("no providers installed");
    }
    AttachNotSupportedException lastExc = null;
    for (AttachProvider provider : providers) {
        try {
            return provider.attachVirtualMachine(id);
        } catch (AttachNotSupportedException x) {
            lastExc = x;
        }
    }
    throw lastExc;
}
Also used : AttachProvider(com.sun.tools.attach.spi.AttachProvider)

Example 2 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class Props method listVms.

private static String[] listVms() {
    List<AttachProvider> providers = AttachProvider.providers();
    AttachProvider ap = providers.get(0);
    if (null == ap) {
        System.err.println("no attach providers available");
        return (null);
    }
    List<VirtualMachineDescriptor> vmds = ap.listVirtualMachines();
    ArrayList<String> vmidBuffer = new ArrayList<String>();
    for (VirtualMachineDescriptor vmd : vmds) {
        vmidBuffer.add(vmd.id());
    }
    return vmidBuffer.toArray(new String[vmidBuffer.size()]);
}
Also used : ArrayList(java.util.ArrayList) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachProvider(com.sun.tools.attach.spi.AttachProvider)

Example 3 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class TestAttachAPI method test_vmname04.

@Test
public void test_vmname04() {
    logger.debug("starting " + testName);
    TargetManager target = launchTarget();
    VirtualMachine vm = null;
    VirtualMachineDescriptor vmd = null;
    try {
        String tgtId = target.getTargetPid();
        List<AttachProvider> providers = AttachProvider.providers();
        AttachProvider myProvider = providers.get(0);
        vmd = findVmdForPid(myProvider, tgtId);
        vm = VirtualMachine.attach(vmd);
        String vmId = vm.id();
        String vmdId = vmd.id();
        AssertJUnit.assertEquals("VirtualMachineDescriptor.id() == VirtualMachine.id()", vmId, vmdId);
    } catch (AttachNotSupportedException | IOException e) {
        logger.error("Trying to attach " + vmd.id() + " " + vmd.displayName());
        logExceptionInfoAndFail(e);
    } finally {
        if (null != vm) {
            try {
                vm.detach();
            } catch (IOException e) {
                logExceptionInfoAndFail(e);
            }
        }
        target.terminateTarget();
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AttachProvider(com.sun.tools.attach.spi.AttachProvider) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.testng.annotations.Test)

Example 4 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class TestAttachAPI method test_attach05.

@Test
public void test_attach05() {
    TargetManager target = launchTarget();
    VirtualMachine vm = null;
    try {
        List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
        AttachProvider myProvider = providers.get(0);
        VirtualMachineDescriptor vmd;
        if (null == (vmd = findVmdForPid(myProvider, target.getTargetPid()))) {
            fail("ERROR " + testName + ": no target VMs found");
        }
        vm = VirtualMachine.attach(vmd);
        if (null == vm) {
            fail("vm is null");
            return;
        }
        if (null == vmd) {
            fail("vmd is null");
            return;
        }
        AttachProvider vmdProvider = vmd.provider();
        AssertJUnit.assertEquals("VirtualMachineDescriptor.provider() == main provider", vmdProvider, myProvider);
        AttachProvider vmProvider = vm.provider();
        AssertJUnit.assertEquals("VirtualMachine.provider() == main provider", vmProvider, myProvider);
    } catch (AttachNotSupportedException | IOException e) {
        listIpcDir();
        logExceptionInfoAndFail(e);
    } finally {
        if (null != vm) {
            try {
                vm.detach();
            } catch (IOException e) {
                logExceptionInfoAndFail(e);
            }
        }
        target.terminateTarget();
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AttachProvider(com.sun.tools.attach.spi.AttachProvider) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.testng.annotations.Test)

Example 5 with AttachProvider

use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.

the class TestAttachAPI method test_vmname06.

@Test
public void test_vmname06() {
    logger.debug("starting " + testName);
    TargetManager mainTarget = launchTarget();
    try {
        List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
        AttachProvider myProvider = providers.get(0);
        List<VirtualMachineDescriptor> vmdList = myProvider.listVirtualMachines();
        List<VirtualMachineDescriptor> vmdList2 = myProvider.listVirtualMachines();
        if (vmdList.size() < 2) {
            fail("ERROR " + testName + ": no target VMs found");
        }
        VirtualMachineDescriptor mainDesc = vmdList.get(0);
        boolean found = false;
        Iterator<VirtualMachineDescriptor> vmdi = vmdList2.iterator();
        while (vmdi.hasNext()) {
            VirtualMachineDescriptor vmd = vmdi.next();
            if (vmd.equals(mainDesc)) {
                found = true;
                break;
            }
        }
        assertTrue("VirtualMachineDescriptor.equals", found);
    } finally {
        mainTarget.terminateTarget();
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachProvider(com.sun.tools.attach.spi.AttachProvider) Test(org.testng.annotations.Test)

Aggregations

AttachProvider (com.sun.tools.attach.spi.AttachProvider)20 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)12 VirtualMachine (com.sun.tools.attach.VirtualMachine)9 Test (org.testng.annotations.Test)9 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)2 HashMap (java.util.HashMap)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ObjectName (javax.management.ObjectName)1 JMXConnector (javax.management.remote.JMXConnector)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1