Search in sources :

Example 11 with JavaReference

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);
}
Also used : JavaRuntime(com.ibm.dtfj.java.JavaRuntime) HashMap(java.util.HashMap) J9DDRImageProcess(com.ibm.j9ddr.view.dtfj.image.J9DDRImageProcess) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) JavaReference(com.ibm.dtfj.java.JavaReference) Iterator(java.util.Iterator) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) ArrayList(java.util.ArrayList) List(java.util.List) IteratorFilter(com.ibm.j9ddr.util.IteratorHelpers.IteratorFilter) JavaObject(com.ibm.dtfj.java.JavaObject) JavaClass(com.ibm.dtfj.java.JavaClass) JavaObject(com.ibm.dtfj.java.JavaObject) Test(org.junit.Test)

Example 12 with JavaReference

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;
}
Also used : JavaReference(com.ibm.dtfj.java.JavaReference) Iterator(java.util.Iterator) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) JavaObject(com.ibm.dtfj.java.JavaObject) CorruptData(com.ibm.dtfj.image.CorruptData) CorruptDataException(com.ibm.dtfj.image.CorruptDataException)

Example 13 with JavaReference

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);
            }
        }
    }
}
Also used : JavaReference(com.ibm.dtfj.java.JavaReference) JavaObject(com.ibm.dtfj.java.JavaObject) JavaClass(com.ibm.dtfj.java.JavaClass) ReferenceIterator(com.ibm.jvm.dtfjview.heapdump.ReferenceIterator) Iterator(java.util.Iterator) LongListReferenceIterator(com.ibm.jvm.dtfjview.heapdump.LongListReferenceIterator) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) JavaObject(com.ibm.dtfj.java.JavaObject) CorruptData(com.ibm.dtfj.image.CorruptData)

Aggregations

JavaReference (com.ibm.dtfj.java.JavaReference)13 JavaObject (com.ibm.dtfj.java.JavaObject)11 CorruptData (com.ibm.dtfj.image.CorruptData)10 Iterator (java.util.Iterator)8 JavaClass (com.ibm.dtfj.java.JavaClass)7 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)6 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)6 J9DDRCorruptData (com.ibm.j9ddr.view.dtfj.image.J9DDRCorruptData)6 JavaClassLoader (com.ibm.dtfj.java.JavaClassLoader)3 J9DDRDTFJUtils.corruptIterator (com.ibm.j9ddr.view.dtfj.J9DDRDTFJUtils.corruptIterator)3 J9Object (com.ibm.j9ddr.vm29.structure.J9Object)3 J9ObjectFieldOffsetIterator (com.ibm.j9ddr.vm29.j9.J9ObjectFieldOffsetIterator)2 DTFJConstantPoolIterator (com.ibm.j9ddr.vm29.view.dtfj.java.j9.DTFJConstantPoolIterator)2 HashMap (java.util.HashMap)2 ImagePointer (com.ibm.dtfj.image.ImagePointer)1 MemoryAccessException (com.ibm.dtfj.image.MemoryAccessException)1 JavaLocation (com.ibm.dtfj.java.JavaLocation)1 JavaMethod (com.ibm.dtfj.java.JavaMethod)1 JavaRuntime (com.ibm.dtfj.java.JavaRuntime)1 JavaStackFrame (com.ibm.dtfj.java.JavaStackFrame)1