Search in sources :

Example 1 with HeapdumpReader

use of com.ibm.dtfj.phd.parser.HeapdumpReader in project openj9 by eclipse.

the class PHDJavaHeap method getObjects.

/**
 * Return all the objects in the heap
 * This uses a modified version of the HeapdumpReader which allows abort and resume.
 */
public Iterator<JavaObject> getObjects() {
    final PHDJavaHeap heap = this;
    try {
        return new Iterator<JavaObject>() {

            HeapdumpReader reader = null;

            {
                if (stream == null) {
                    reader = new HeapdumpReader(file, image);
                } else {
                    reader = new HeapdumpReader(stream, image);
                }
            }

            final int adjustLen = reader.version() == 4 && reader.isJ9() ? 1 : 0;

            final long[] current = new long[1];

            static final boolean withRefs = true;

            JavaObject jo;

            int count = 0;

            PortableHeapDumpListener listen = new PortableHeapDumpListener() {

                public void classDump(long address, long superAddress, String name, int size, int flags, int hashCode, LongEnumeration refs) throws Exception {
                }

                public void objectArrayDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, int length, long instanceSize) throws Exception {
                    current[0] = address;
                    int refsLen = refs.numberOfElements();
                    int adjustLen2 = Math.min(adjustLen, refsLen);
                    // Use adjustLen for array class so for corrupt Java 5 with 0 refs we have no array class
                    PHDJavaObject.Builder b = new PHDJavaObject.Builder(heap, address, runtime.arrayOf(classAddress, refs, adjustLen), flags, hashCode).instanceSize(instanceSize);
                    jo = withRefs ? b.refs(refs, adjustLen2).length(length - adjustLen2).build() : b.length(length - adjustLen2).build();
                    current[0] = 0;
                    reader.exitParse();
                }

                public void objectDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, long instanceSize) throws Exception {
                    current[0] = address;
                    PHDJavaObject.Builder b = new PHDJavaObject.Builder(heap, address, runtime.findClass(classAddress), flags, hashCode).length(PHDJavaObject.SIMPLE_OBJECT).instanceSize(instanceSize);
                    jo = withRefs ? b.refs(refs, 0).build() : b.build();
                    current[0] = 0;
                    reader.exitParse();
                }

                public void primitiveArrayDump(long address, int type, int length, int flags, int hashCode, long instanceSize) throws Exception {
                    current[0] = address;
                    jo = new PHDJavaObject.Builder(heap, address, runtime.findArrayOfType(type), flags, hashCode).refsAsArray(NOREFS, 0).length(length).instanceSize(instanceSize).build();
                    current[0] = 0;
                    reader.exitParse();
                }
            };

            public boolean hasNext() {
                if (jo == null)
                    getNext();
                return jo != null;
            }

            public JavaObject next() {
                if (!hasNext())
                    throw new NoSuchElementException();
                JavaObject ret = jo;
                jo = null;
                ++count;
                return ret;
            }

            private void getNext() {
                try {
                    // but presumes the reader can restart parsing where it left off.
                    if (reader != null && !reader.parse(listen)) {
                        reader.close();
                        reader = null;
                    }
                } catch (EOFException e) {
                    jo = new PHDCorruptJavaObject("Truncated dump found while building object " + count + "/" + reader.totalObjects(), space.getPointer(0), e);
                    reader.close();
                    reader = null;
                } catch (IOException e) {
                    jo = new PHDCorruptJavaObject("Corrupted dump found while building object " + count + "/" + reader.totalObjects(), space.getPointer(0), e);
                    reader.close();
                    reader = null;
                } catch (Exception e) {
                    // Is this right?
                    if (current[0] != 0) {
                        // Only add an exception object the first time it happens
                        // Give up if exception occurs between objects
                        jo = new PHDCorruptJavaObject("Building object " + count + "/" + reader.totalObjects(), space.getPointer(current[0]), e);
                    } else {
                        jo = new PHDCorruptJavaObject("Building object " + count + "/" + reader.totalObjects(), space.getPointer(0), e);
                        reader.close();
                        reader = null;
                    }
                }
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }

            protected void finalize() throws Throwable {
                // but the client doesn't have to run the iterator to the end, so we're accounting for that here
                if (reader != null) {
                    reader.close();
                }
            }
        };
    } catch (IOException e) {
        return new ArrayList<JavaObject>().iterator();
    }
}
Also used : PortableHeapDumpListener(com.ibm.dtfj.phd.parser.PortableHeapDumpListener) HeapdumpReader(com.ibm.dtfj.phd.parser.HeapdumpReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) NoSuchElementException(java.util.NoSuchElementException) LongEnumeration(com.ibm.dtfj.phd.util.LongEnumeration) JavaObject(com.ibm.dtfj.java.JavaObject) Iterator(java.util.Iterator) EOFException(java.io.EOFException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

CorruptDataException (com.ibm.dtfj.image.CorruptDataException)1 JavaObject (com.ibm.dtfj.java.JavaObject)1 HeapdumpReader (com.ibm.dtfj.phd.parser.HeapdumpReader)1 PortableHeapDumpListener (com.ibm.dtfj.phd.parser.PortableHeapDumpListener)1 LongEnumeration (com.ibm.dtfj.phd.util.LongEnumeration)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1