use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.
the class TestAttachAPI method test_vmname07.
@Test
public void test_vmname07() {
logger.debug("starting " + testName);
TargetManager target = null;
VirtualMachine vm = null;
try {
target = new TargetManager(TestConstants.TARGET_VM_CLASS, testName, testName, null, null);
target.syncWithTarget();
assertTrue(vmIdExists(testName));
List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
AttachProvider myProvider = providers.get(0);
vm = myProvider.attachVirtualMachine(testName);
Properties props = vm.getSystemProperties();
String vendor = props.getProperty(TestConstants.VENDOR_NAME_PROP);
String myVendor = System.getProperty(TestConstants.VENDOR_NAME_PROP);
AssertJUnit.assertEquals(myVendor, vendor);
} catch (AttachNotSupportedException | IOException e) {
logExceptionInfoAndFail(e);
} finally {
if (null != target) {
target.terminateTarget();
}
}
}
use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.
the class TestAttachAPI method checkHashes.
private boolean checkHashes(boolean detach, TargetManager[] tgts) {
final int iterations = 4;
List<VirtualMachineDescriptor> vmdList;
int[] vmHashes, vmdHashes;
VirtualMachine[] vms;
boolean failed = false;
List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
AttachProvider myProvider = providers.get(0);
vmdList = myProvider.listVirtualMachines();
if (vmdList.size() < tgts.length) {
fail("checkHashes ERROR: no target VMs found");
}
vmHashes = new int[vmdList.size()];
vmdHashes = new int[vmdList.size()];
vms = new VirtualMachine[vmdList.size()];
HashMap<String, Integer> id2hash = new HashMap<String, Integer>(vmdList.size());
Iterator<VirtualMachineDescriptor> vmdI = vmdList.iterator();
while (vmdI.hasNext()) {
VirtualMachineDescriptor vmd = vmdI.next();
id2hash.put(vmd.id(), vmd.hashCode());
}
ArrayList<VirtualMachine> attachments = new ArrayList<VirtualMachine>();
for (int iter = 0; iter < iterations; ++iter) {
int vmNum = 0;
for (TargetManager tm : tgts) {
String vmId = tm.getTargetPid();
logger.debug("checkHashes attaching to " + vmId);
try {
VirtualMachine vm = VirtualMachine.attach(vmId);
int vhc = vm.hashCode();
int dhc = id2hash.get(vmId);
if (0 == iter) {
vms[vmNum] = vm;
vmHashes[vmNum] = vhc;
vmdHashes[vmNum] = dhc;
logger.debug("hashcodes for " + vmId + ": vm = " + vhc + " descriptor = " + dhc);
} else {
if (!vm.equals(vms[vmNum])) {
logger.error("checkHashes ERROR: equals failed " + vmId);
failed = true;
}
if (dhc != vmdHashes[vmNum]) {
logger.error("checkHashes ERROR: " + vmId + " old descriptor hash " + vmdHashes[vmNum] + " new hash " + dhc);
failed = true;
}
if (vhc != vmHashes[vmNum]) {
logger.error("checkHashes ERROR: " + vmId + " old vm hash " + vmHashes[vmNum] + " new hash " + vhc);
failed = true;
}
}
if (detach) {
vm.detach();
} else {
attachments.add(vm);
}
} catch (AttachNotSupportedException | IOException e) {
logger.error(e.getMessage() + "\nfailure at " + vmId);
listIpcDir();
logExceptionInfoAndFail(e);
}
++vmNum;
}
}
for (VirtualMachine vm : attachments) {
try {
logger.debug("teardown detach " + vm.hashCode());
vm.detach();
} catch (IOException e) {
logStackTrace(e);
logger.error("IOException when detaching (permissible)");
}
}
logger.debug("checkHashes verify that hashes are unique");
for (int i = 0; i < vmHashes.length; ++i) {
if (vms[i] == null) {
continue;
}
for (int j = i + 1; j < i; ++j) {
if (vms[i].equals(vms[j])) {
logger.error("checkHashes ERROR: vm " + vmdList.get(i).id() + " equals " + vmdList.get(j).id());
failed = true;
}
if (vmHashes[i] == vmHashes[j]) {
logger.error("checkHashes ERROR: vm hashcode " + vmHashes[i] + " for " + vmdList.get(i).id() + " same as " + vmdList.get(j).id());
failed = true;
}
if (vmdHashes[i] == vmdHashes[j]) {
logger.error("checkHashes ERROR: descriptor hashcode " + vmdHashes[i] + " for " + vmdList.get(i).id() + " same as " + vmdList.get(j).id());
failed = true;
}
}
}
return !failed;
}
use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.
the class TestAttachErrorHandling method listVmsAndCheckStaleDeleted.
private void listVmsAndCheckStaleDeleted(File staleDirectory) {
AssertJUnit.assertTrue("stale directory missing", staleDirectory.exists());
List<AttachProvider> providers = com.sun.tools.attach.spi.AttachProvider.providers();
AttachProvider myProvider = providers.get(0);
myProvider.listVirtualMachines();
long staleDirectoryPid = getPidFromDirectory(staleDirectory);
if (!IPC.processExists(staleDirectoryPid)) {
AssertJUnit.assertFalse("stale directory not removed", staleDirectory.exists());
}
}
use of com.sun.tools.attach.spi.AttachProvider in project openj9 by eclipse.
the class TargetManager method vmIdExists.
public boolean vmIdExists() {
List<AttachProvider> providers = AttachProvider.providers();
AttachProvider ap = providers.get(0);
if (null == ap) {
logger.error("no attach providers available");
return false;
}
List<VirtualMachineDescriptor> vmds = ap.listVirtualMachines();
Iterator<VirtualMachineDescriptor> vmi = vmds.iterator();
while (vmi.hasNext()) {
VirtualMachineDescriptor vm = vmi.next();
if (targetId.equals(vm.id()) && ((null == displayName) || displayName.equals(vm.displayName()))) {
return true;
}
}
return false;
}
use of com.sun.tools.attach.spi.AttachProvider in project powermock by powermock.
the class VirtualMachine method attach.
/**
* <p>
* Attaches to a Java virtual machine.
* </p>
* <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} method of all providers throws
* {@link com.sun.tools.attach.AttachNotSupportedException AttachNotSupportedException}
* then this method also throws {@code AttachNotSupportedException}.
* This means that {@code AttachNotSupportedException} 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 IOException If an I/O error occurs
*/
public static VirtualMachine attach(String id) throws AttachNotSupportedException, IOException {
List<AttachProvider> providers = AttachProvider.providers();
AttachNotSupportedException lastExc = null;
for (AttachProvider provider : providers) {
try {
return provider.attachVirtualMachine(id);
} catch (AttachNotSupportedException x) {
lastExc = x;
}
}
throw lastExc;
}
Aggregations