use of com.ibm.dtfj.java.JavaReference in project openj9 by eclipse.
the class JavaRuntimeTest method getHeapRootsTest.
@SuppressWarnings("unchecked")
@Test
public void getHeapRootsTest() {
boolean result = true;
String version = ((J9DDRImageProcess) ddrProcess).getVersion();
IteratorFilter ddrFilter = null;
if (version.equals("26")) {
ddrFilter = new IteratorFilter() {
// vm26 has a StringTable Cache that the root scanner scans but jextract(HeapIteratorAPI) does not
public boolean accept(Object obj) {
JavaReference ref = (JavaReference) obj;
if (ref.getDescription().equals("StringCacheTable")) {
return false;
}
return true;
}
};
}
if (version.equals("23")) {
ddrFilter = new IteratorFilter() {
boolean foundFirstContrivedThread = false;
public boolean accept(Object obj) {
try {
JavaReference ref = (JavaReference) obj;
Object ddrHeapRoot = ref.getTarget();
if (ref.getRootType() == JavaReference.HEAP_ROOT_THREAD) {
if (ddrHeapRoot instanceof JavaObject) {
JavaObject javaObject = (JavaObject) ddrHeapRoot;
String name = javaObject.getJavaClass().getName();
if (name.endsWith("Error") || name.endsWith("Exception") || name.equals("com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors$MonitorClass") || name.equals("com/ibm/dtfj/tck/harness/Configure$DumpThread")) {
// Ignore these since JExtract can't find them.
return false;
}
if (name.equals("com/ibm/dtfj/tck/tests/javaruntime/TestJavaThread_getStackFrames$ContrivedThread")) {
if (foundFirstContrivedThread) {
return false;
} else {
foundFirstContrivedThread = true;
}
}
}
}
return true;
} catch (DataUnavailable e) {
fail("bail");
return false;
} catch (CorruptDataException e) {
fail("bail");
return false;
}
}
};
}
for (int i = 0; i < ddrTestObjects.size(); i++) {
JavaRuntime localDDRRuntime = (JavaRuntime) ddrTestObjects.get(i);
JavaRuntime localJextractRuntime = (JavaRuntime) jextractTestObjects.get(i);
Object[] ddrHeapRootsArray = null;
Object[] jextractHeapRootsArray = null;
int threadCount = 0;
Iterator titer = localDDRRuntime.getThreads();
while (titer.hasNext()) {
threadCount++;
titer.next();
}
System.out.println("Thread count: " + threadCount);
try {
ddrHeapRootsArray = DTFJComparator.getList(Object.class, localDDRRuntime.getHeapRoots(), ddrFilter).toArray();
jextractHeapRootsArray = DTFJComparator.getList(Object.class, localJextractRuntime.getHeapRoots(), null).toArray();
} catch (InvalidObjectException e1) {
fail("Test setup failure");
}
Map<Long, List<Object>> map = new HashMap<Long, List<Object>>();
for (int j = 0; j < ddrHeapRootsArray.length; j++) {
long key = 0;
JavaReference ref = (JavaReference) ddrHeapRootsArray[j];
Object ddrHeapRoot = null;
try {
ddrHeapRoot = ref.getTarget();
} catch (DataUnavailable e) {
fail("bail");
} catch (CorruptDataException e) {
fail("bail");
}
if (ddrHeapRoot instanceof JavaObject) {
key = ((JavaObject) ddrHeapRoot).getID().getAddress();
} else if (ddrHeapRoot instanceof JavaClass) {
key = ((JavaClass) ddrHeapRoot).getID().getAddress();
} else {
fail("bail");
}
List bucket = map.get(key);
if (bucket == null) {
bucket = new ArrayList<Object>();
map.put(key, bucket);
}
bucket.add(ddrHeapRoot);
}
for (int j = 0; j < jextractHeapRootsArray.length; j++) {
JavaReference ref = (JavaReference) jextractHeapRootsArray[j];
Object heapRoot = null;
String name = null;
long key = 0;
try {
heapRoot = ref.getTarget();
if (heapRoot instanceof JavaObject) {
key = ((JavaObject) heapRoot).getID().getAddress();
name = "Instance of " + ((JavaObject) heapRoot).getJavaClass().getName();
} else if (heapRoot instanceof JavaClass) {
key = ((JavaClass) heapRoot).getID().getAddress();
name = ((JavaClass) heapRoot).getName();
} else {
fail("bail");
}
} catch (DataUnavailable e) {
fail("bail");
} catch (CorruptDataException e) {
fail("bail");
}
List bucket = map.get(key);
if (bucket == null) {
System.out.println(String.format("Jextract found %s at address: %s. Element #: %s that ddr does not know about", name, Long.toHexString(key), j));
result = false;
continue;
}
if (bucket.isEmpty()) {
fail("bail");
}
bucket.remove(0);
if (bucket.isEmpty()) {
map.remove(key);
}
}
// Whats left.
int count = 0;
Collection<List<Object>> remains = map.values();
if (remains.size() > 0) {
System.out.println("");
System.out.println("DDR found the following that JExtract did not know about");
}
Iterator<List<Object>> bucketIter = remains.iterator();
while (bucketIter.hasNext()) {
List<Object> bucket = bucketIter.next();
Iterator<Object> contentsIter = bucket.iterator();
while (contentsIter.hasNext()) {
Object obj = contentsIter.next();
System.out.println(String.format("%s) %s", ++count, obj));
}
}
}
assertTrue(result);
}
use of com.ibm.dtfj.java.JavaReference in project openj9 by eclipse.
the class PHDJavaClass method referencesClass.
/**
* Is there a reference from one class to another?
* E.g. from an array class to the component type? This would be an indication that they
* are truly related.
* @param from
* @param to
* @return
*/
static boolean referencesClass(JavaClass from, JavaClass to) {
for (Iterator i1 = from.getReferences(); i1.hasNext(); ) {
Object o = i1.next();
if (o instanceof CorruptData)
continue;
JavaReference jr = (JavaReference) o;
try {
if (jr.isClassReference() && jr.getTarget().equals(to)) {
return true;
}
if (jr.isObjectReference() && jr.getTarget().equals(to.getObject())) {
return true;
}
} catch (DataUnavailable e) {
} catch (CorruptDataException e) {
}
}
return false;
}
use of com.ibm.dtfj.java.JavaReference in project openj9 by eclipse.
the class HeapdumpCommand method addReferences.
/**
* Extracts the instance references from an object
* @param object Object being walked
* @param references List<Long> to add references to
* @param thisClass Class of object
*/
private void addReferences(JavaObject object, List<Long> references) throws CorruptDataException, MemoryAccessException {
Iterator it = object.getReferences();
Object ref = null;
// discard the first reference which is always to the class
if (it.hasNext()) {
// test hasNext() to be on the safe side.
ref = it.next();
}
while (it.hasNext()) {
ref = it.next();
if (ref instanceof CorruptData) {
// can sometimes get a nasty surprise in the list - e.g. a J9DDRCorruptData
_numberOfErrors++;
reportError("Corrupt data found at address " + ((CorruptData) ref).getAddress() + " getting references from object at address: " + Long.toHexString(object.getID().getAddress()) + " of class " + object.getJavaClass().getName() + "(" + object.getJavaClass().getID() + ")", null);
continue;
}
if (!(ref instanceof JavaReference)) {
_numberOfErrors++;
reportError("Object of unexpected type " + ref.getClass() + " found within references from object at address: " + object.getID().getAddress() + " of class " + object.getJavaClass().getName() + "(" + object.getJavaClass().getID() + ")", null);
continue;
} else {
Object target;
try {
target = ((JavaReference) ref).getTarget();
} catch (DataUnavailable e) {
_numberOfErrors++;
reportError("DataUnavailable thrown from call to getTarget() on reference: " + ref, null);
continue;
}
// the following ugliness is necessary as JavaObject and JavaClass both support getID() but do not inherit from a common parent
if (target instanceof JavaObject) {
references.add(new Long(((JavaObject) target).getID().getAddress()));
} else if (target instanceof JavaClass) {
references.add(new Long(((JavaClass) target).getID().getAddress()));
} else {
_numberOfErrors++;
reportError("Object of unexpected type " + target.getClass() + " returned from call to getTarget() on reference " + ref, null);
}
}
}
}
Aggregations