Search in sources :

Example 41 with JavaObject

use of com.ibm.dtfj.java.JavaObject in project openj9 by eclipse.

the class DTFJJavaObject method addFieldReferences.

private void addFieldReferences(JavaClass jClass, int referentReachabilityType) throws CorruptDataException, MemoryAccessException {
    Iterator<?> fieldIt = jClass.getDeclaredFields();
    while (fieldIt.hasNext()) {
        Object fieldObj = fieldIt.next();
        if (fieldObj instanceof JavaField) {
            JavaField thisField = (JavaField) fieldObj;
            if ((thisField.getModifiers() & Modifier.STATIC) == 0) {
                String signature = thisField.getSignature();
                // From a reference point of view, only objects are interesting
                if (signature.startsWith("L") || signature.startsWith("[")) {
                    Object targetObj = thisField.get(this);
                    if (targetObj == null) {
                        continue;
                    }
                    if (targetObj instanceof JavaObject) {
                        String fieldName = thisField.getName();
                        String declaringClassName = null;
                        try {
                            declaringClassName = thisField.getDeclaringClass().getName();
                        } catch (DataUnavailable e) {
                        // declaringClassName will be null, we will add this as a strong ref.
                        }
                        String description = "Object Reference [field name:" + fieldName + "]";
                        // (Not any referent field declared in subclasses.)
                        if (fieldName.equals("referent") && "java/lang/ref/Reference".equals(declaringClassName)) {
                            references.add(new DTFJJavaReference(this, targetObj, description, JavaReference.REFERENCE_FIELD, JavaReference.HEAP_ROOT_UNKNOWN, referentReachabilityType));
                        } else {
                            references.add(new DTFJJavaReference(this, targetObj, description, JavaReference.REFERENCE_FIELD, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG));
                        }
                    } else if (targetObj instanceof CorruptData) {
                        references.add(targetObj);
                    } else {
                        references.add(J9DDRDTFJUtils.newCorruptData(DTFJContext.getProcess(), "Unexpected type from field get: " + targetObj + ", class=" + targetObj.getClass().getName()));
                    }
                }
            }
        } else if (fieldObj instanceof CorruptData) {
            references.add(fieldObj);
        } else {
            references.add(J9DDRDTFJUtils.newCorruptData(DTFJContext.getProcess(), "Unexpected type from field iteration: " + fieldObj + ", class=" + fieldObj.getClass().getName()));
        }
    }
}
Also used : JavaField(com.ibm.dtfj.java.JavaField) JavaObject(com.ibm.dtfj.java.JavaObject) CorruptJavaObject(com.ibm.j9ddr.vm29.view.dtfj.java.corrupt.CorruptJavaObject) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) JavaObject(com.ibm.dtfj.java.JavaObject) J9Object(com.ibm.j9ddr.vm29.structure.J9Object) CorruptJavaObject(com.ibm.j9ddr.vm29.view.dtfj.java.corrupt.CorruptJavaObject) CorruptData(com.ibm.dtfj.image.CorruptData) J9DDRCorruptData(com.ibm.j9ddr.view.dtfj.image.J9DDRCorruptData)

Example 42 with JavaObject

use of com.ibm.dtfj.java.JavaObject 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 43 with JavaObject

use of com.ibm.dtfj.java.JavaObject in project openj9 by eclipse.

the class JavaObjectTest method fillObjectList.

/**
 * Fills each list with a limited number of objects from the respective iterator.
 * Does not add CorruptData objects from the Iterator
 * If one iterator returns a CorruptData insures that the other iterator returns a CorruptData
 *
 * Sorts the output if a sort order is provided.
 *
 * @param ddrList
 * @param ddrIterator
 * @param jextractList
 * @param jextractIterator
 * @param sortOrder
 * @param maxCount the maximum number of items to populate the lists with
 */
@SuppressWarnings("unchecked")
public static void fillObjectList(List<Object> ddrList, Iterator ddrIterator, List<Object> jextractList, Iterator jextractIterator, Comparator sortOrder, int maxCount, boolean arrayList) {
    List<Object> ddrObjects = new ArrayList<Object>();
    List<Object> jextractObjects = new ArrayList<Object>();
    while (ddrIterator.hasNext()) {
        assertTrue("DDR returned more elements than jextract", jextractIterator.hasNext());
        Object ddrObject = ddrIterator.next();
        Object jextractObject = jextractIterator.next();
        ddrObjects.add(ddrObject);
        jextractObjects.add(jextractObject);
    }
    // removed assertion below because when using a sample rather than an exhaustive list one list can be larger than the other so long
    // as the order is the same for the super set of list elements.
    // assertFalse("jextract returned more elements than ddr", jextractIterator.hasNext());
    Object[] ddrObjectsArray = ddrObjects.toArray();
    Object[] jextractObjectsArray = jextractObjects.toArray();
    if (sortOrder != null) {
        Arrays.sort(ddrObjectsArray, sortOrder);
        Arrays.sort(jextractObjectsArray, sortOrder);
    }
    int count = 0;
    for (int i = 0; (i < ddrObjectsArray.length) && (count < maxCount); i++) {
        Object ddrObject = ddrObjectsArray[i];
        Object jextractObject = jextractObjectsArray[i];
        if (!(ddrObject instanceof CorruptData) && !(jextractObject instanceof CorruptData)) {
            JavaObject ddrJavaObject = (JavaObject) ddrObject;
            JavaObject jextractJavaObject = (JavaObject) jextractObject;
            try {
                if ((ddrJavaObject.isArray() && jextractJavaObject.isArray()) == arrayList) {
                    ddrList.add(ddrObject);
                    jextractList.add(jextractObject);
                    count++;
                }
            } catch (CorruptDataException e) {
                fail("CDE when populating heap list : " + e.getMessage());
            }
            continue;
        }
        if (ddrObject instanceof CorruptData && jextractObject instanceof CorruptData) {
            continue;
        }
        fail("If DDR object is a CorruptData then jextract object must also be a CorruptData");
    }
    assertEquals("jextract and ddr returned iterators of different sizes", jextractList.size(), ddrList.size());
}
Also used : JavaObject(com.ibm.dtfj.java.JavaObject) ArrayList(java.util.ArrayList) JavaObject(com.ibm.dtfj.java.JavaObject) CorruptData(com.ibm.dtfj.image.CorruptData) CorruptDataException(com.ibm.dtfj.image.CorruptDataException)

Example 44 with JavaObject

use of com.ibm.dtfj.java.JavaObject in project openj9 by eclipse.

the class JavaRuntimeTest method getHeapsTest.

@Test
public void getHeapsTest() {
    Iterator<Object> iter = ((JavaRuntime) ddrTestObjects.get(0)).getHeaps();
    while (iter.hasNext()) {
        JavaHeap heap = (JavaHeap) iter.next();
        Iterator<Object> objectIter = heap.getObjects();
        int count = 0;
        while (objectIter.hasNext()) {
            // JavaObject javaObject = (JavaObject) objectIter.next();
            // System.out.println(javaObject.get);
            count++;
            objectIter.next();
        }
        System.out.println(heap.getName() + ": " + count);
    }
    javaHeapComparator.testComparatorIteratorEquals(ddrTestObjects, jextractTestObjects, "getHeaps", JavaHeap.class);
}
Also used : JavaRuntime(com.ibm.dtfj.java.JavaRuntime) JavaHeap(com.ibm.dtfj.java.JavaHeap) JavaObject(com.ibm.dtfj.java.JavaObject) Test(org.junit.Test)

Example 45 with JavaObject

use of com.ibm.dtfj.java.JavaObject in project openj9 by eclipse.

the class JavaClass method getReferences.

/* (non-Javadoc)
	 * @see com.ibm.dtfj.java.JavaClass#getReferences()
	 */
public Iterator getReferences() {
    // need to build a list of references from this class.
    Vector references = new Vector();
    JavaReference jRef = null;
    // get the Constant Pool references from this class.
    Iterator constantPoolIt = getConstantPoolReferences();
    while (constantPoolIt.hasNext()) {
        // get each reference in turn, note that the iterator can return JavaClass
        // JavaObject and CorruptData. The CorruptData objects are ignored.
        Object cpObject = constantPoolIt.next();
        if (cpObject instanceof JavaObject) {
            // add the reference to the container.
            jRef = new JavaReference(_javaVM, this, cpObject, "Constant Pool Object", JavaReference.REFERENCE_CONSTANT_POOL, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
        } else if (cpObject instanceof JavaClass) {
            // got a JavaClass
            JavaClass jClass = (JavaClass) cpObject;
            // add the reference to the container.
            jRef = new JavaReference(_javaVM, this, jClass, "Constant Pool Class", JavaReference.REFERENCE_CONSTANT_POOL, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
        }
        if (null != jRef) {
            references.add(jRef);
        }
    }
    // get the static field references from this class.
    Iterator declaredFieldIt = getDeclaredFields();
    while (declaredFieldIt.hasNext()) {
        JavaField jField = (JavaField) declaredFieldIt.next();
        // got a field, now test it to see if it is a static reference.
        if (jField instanceof JavaStaticField) {
            JavaStaticField sField = (JavaStaticField) jField;
            try {
                Object obj = sField.getReferenceType(null);
                if (null != obj) {
                    if (obj instanceof JavaObject) {
                        // build a JavaReference type and add the reference to the container.
                        String fieldName = sField.getName();
                        String description = "Static field";
                        if (null != fieldName) {
                            description = description + " [field name:" + fieldName + "]";
                        }
                        JavaObject jObject = (JavaObject) obj;
                        jRef = new JavaReference(_javaVM, this, jObject, description, JavaReference.REFERENCE_STATIC_FIELD, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
                        references.add(jRef);
                    }
                }
            } catch (CorruptDataException e) {
                // Corrupt data, so add it to the container.
                references.add(e.getCorruptData());
            } catch (MemoryAccessException e) {
                // Memory access problems, so create a CorruptData object
                // to describe the problem and add it to the container.
                ImagePointer ptrInError = e.getPointer();
                String message = e.getMessage();
                references.add(new CorruptData(message, ptrInError));
            } catch (IllegalArgumentException e) {
            // No static data, so ignore.
            }
        }
    }
    addSuperclassReference(references);
    addClassLoaderReference(references);
    addClassObjectReference(references);
    return references.iterator();
}
Also used : CorruptDataException(com.ibm.dtfj.image.CorruptDataException) ImagePointer(com.ibm.dtfj.image.ImagePointer) JavaObject(com.ibm.dtfj.java.JavaObject) Iterator(java.util.Iterator) JavaObject(com.ibm.dtfj.java.JavaObject) CorruptData(com.ibm.dtfj.image.j9.CorruptData) Vector(java.util.Vector) MemoryAccessException(com.ibm.dtfj.image.MemoryAccessException)

Aggregations

JavaObject (com.ibm.dtfj.java.JavaObject)70 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)47 Iterator (java.util.Iterator)35 JavaClass (com.ibm.dtfj.java.JavaClass)31 CorruptData (com.ibm.dtfj.image.CorruptData)19 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)17 MemoryAccessException (com.ibm.dtfj.image.MemoryAccessException)14 JavaReference (com.ibm.dtfj.java.JavaReference)9 JavaThread (com.ibm.dtfj.java.JavaThread)9 JavaClassLoader (com.ibm.dtfj.java.JavaClassLoader)8 JavaField (com.ibm.dtfj.java.JavaField)7 ImagePointer (com.ibm.dtfj.image.ImagePointer)6 JavaRuntime (com.ibm.dtfj.java.JavaRuntime)6 J9Object (com.ibm.j9ddr.vm29.structure.J9Object)6 CorruptJavaObject (com.ibm.j9ddr.vm29.view.dtfj.java.corrupt.CorruptJavaObject)6 CorruptData (com.ibm.dtfj.image.j9.CorruptData)5 LongEnumeration (com.ibm.dtfj.phd.util.LongEnumeration)5 J9DDRCorruptData (com.ibm.j9ddr.view.dtfj.image.J9DDRCorruptData)5 LongListReferenceIterator (com.ibm.jvm.dtfjview.heapdump.LongListReferenceIterator)5 ReferenceIterator (com.ibm.jvm.dtfjview.heapdump.ReferenceIterator)5