Search in sources :

Example 76 with CorruptDataException

use of com.ibm.dtfj.image.CorruptDataException in project openj9 by eclipse.

the class JavaClassTest method getComponentTypeTest.

@Test
public void getComponentTypeTest() {
    for (int i = 0; i < ddrTestObjects.size(); i++) {
        JavaClass ddrClass = (JavaClass) ddrTestObjects.get(i);
        JavaClass jextractClass = (JavaClass) jextractTestObjects.get(i);
        boolean isArray = false;
        try {
            assertEquals(jextractClass.isArray(), ddrClass.isArray());
            isArray = ddrClass.isArray();
        } catch (CorruptDataException e1) {
            fail("Problem with test setup");
        }
        if (isArray) {
            javaClassComparator.testComparatorEquals(ddrClass, jextractClass, "getComponentType");
        } else {
            JavaClass result = null;
            try {
                result = ddrClass.getComponentType();
            } catch (IllegalArgumentException e) {
            // Expected exception
            } catch (CorruptDataException e) {
                fail("getcComponentType threw unexpected CorruptDataException");
            }
            try {
                assertNull(String.format("%s is not an array and should have thrown an IllegalArgumentException", ddrClass.getName()), result);
            } catch (CorruptDataException e) {
                fail("getcComponentType threw unexpected CorruptDataException");
            }
        }
    }
}
Also used : JavaClass(com.ibm.dtfj.java.JavaClass) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) Test(org.junit.Test)

Example 77 with CorruptDataException

use of com.ibm.dtfj.image.CorruptDataException 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 78 with CorruptDataException

use of com.ibm.dtfj.image.CorruptDataException 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 79 with CorruptDataException

use of com.ibm.dtfj.image.CorruptDataException in project openj9 by eclipse.

the class JavaLocationTest method loadTestObjects.

protected void loadTestObjects(JavaRuntime ddrRuntime, List<Object> ddrObjects, JavaRuntime jextractRuntime, List<Object> jextractObjects) {
    List<Object> ddrThreads = new LinkedList<Object>();
    List<Object> jextractThreads = new LinkedList<Object>();
    fillLists(ddrThreads, ddrRuntime.getThreads(), jextractThreads, jextractRuntime.getThreads(), null);
    for (int i = 0; i < ddrThreads.size(); i++) {
        JavaThread ddrThread = (JavaThread) ddrThreads.get(i);
        JavaThread jextractThread = (JavaThread) jextractThreads.get(i);
        Iterator<?> ddrStackFrameIt = ddrThread.getStackFrames();
        Iterator<?> jextractStackFrameIt = jextractThread.getStackFrames();
        while (ddrStackFrameIt.hasNext()) {
            assertTrue(jextractStackFrameIt.hasNext());
            Object ddrStackFrameObj = ddrStackFrameIt.next();
            Object jextractStackFrameObj = jextractStackFrameIt.next();
            if ((ddrStackFrameObj instanceof CorruptData) ^ (jextractStackFrameObj instanceof CorruptData)) {
                fail("Different CorruptData from DDR & jextract. DDR returned : " + ddrStackFrameObj + ", jextract returned " + jextractStackFrameObj);
            }
            if (ddrStackFrameObj instanceof CorruptData) {
                continue;
            }
            JavaStackFrame ddrStackFrame = (JavaStackFrame) ddrStackFrameObj;
            JavaStackFrame jextractStackFrame = (JavaStackFrame) jextractStackFrameObj;
            boolean ddrFaulted = false;
            boolean jextractFaulted = false;
            try {
                ddrObjects.add(ddrStackFrame.getLocation());
            } catch (CorruptDataException e) {
                e.printStackTrace();
                ddrFaulted = true;
            }
            try {
                jextractObjects.add(jextractStackFrame.getLocation());
            } catch (CorruptDataException e) {
                e.printStackTrace();
                jextractFaulted = true;
            }
            if (ddrFaulted ^ jextractFaulted) {
                fail("Getting location from stackframe behaved differently on DDR/jextract. DDR faulted: " + ddrFaulted + ", DDR object: " + ddrStackFrame + ", jextract faulted: " + jextractFaulted + " jextract object: " + jextractStackFrame);
            }
        }
    }
}
Also used : JavaThread(com.ibm.dtfj.java.JavaThread) JavaStackFrame(com.ibm.dtfj.java.JavaStackFrame) CorruptData(com.ibm.dtfj.image.CorruptData) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) LinkedList(java.util.LinkedList)

Example 80 with CorruptDataException

use of com.ibm.dtfj.image.CorruptDataException 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

CorruptDataException (com.ibm.dtfj.image.CorruptDataException)124 JavaObject (com.ibm.dtfj.java.JavaObject)55 Iterator (java.util.Iterator)49 JavaClass (com.ibm.dtfj.java.JavaClass)41 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)39 CorruptData (com.ibm.dtfj.image.CorruptData)26 MemoryAccessException (com.ibm.dtfj.image.MemoryAccessException)25 ImagePointer (com.ibm.dtfj.image.ImagePointer)19 CorruptData (com.ibm.dtfj.image.j9.CorruptData)17 JavaClassLoader (com.ibm.dtfj.java.JavaClassLoader)14 ImageSection (com.ibm.dtfj.image.ImageSection)12 ImageThread (com.ibm.dtfj.image.ImageThread)12 JavaThread (com.ibm.dtfj.java.JavaThread)12 ArrayList (java.util.ArrayList)11 ImageProcess (com.ibm.dtfj.image.ImageProcess)9 JavaField (com.ibm.dtfj.java.JavaField)8 JavaReference (com.ibm.dtfj.java.JavaReference)8 LinkedList (java.util.LinkedList)8 ImageAddressSpace (com.ibm.dtfj.image.ImageAddressSpace)7 JavaMethod (com.ibm.dtfj.java.JavaMethod)7