Search in sources :

Example 26 with JavaClassLoader

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

the class JavaObject method getReferences.

/* (non-Javadoc)
	 * @see com.ibm.dtfj.java.JavaObject#getReferences()
	 */
public Iterator getReferences() {
    if (null == _references) {
        _references = new Vector();
        try {
            // find this object's class
            JavaClass jClass = getJavaClass();
            // add a reference to the object's class
            if (null != jClass) {
                JavaReference ref = new JavaReference(_javaVM, this, jClass, "Class", JavaReference.REFERENCE_CLASS, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
                _references.add(ref);
            }
            // walk through the super classes to this object.
            while (null != jClass) {
                List refs = null;
                if (jClass instanceof JavaArrayClass) {
                    JavaArrayClass arrayClass = (JavaArrayClass) jClass;
                    refs = getArrayReferences(arrayClass);
                } else if (jClass instanceof com.ibm.dtfj.java.j9.JavaClass) {
                    refs = getFieldReferences((com.ibm.dtfj.java.j9.JavaClass) jClass);
                }
                if (null != refs) {
                    _references.addAll(refs);
                }
                jClass = jClass.getSuperclass();
            }
        } catch (CorruptDataException e) {
            // Corrupt data, so add it to the container.
            _references.add(e.getCorruptData());
        }
        // Now add association-specific references
        if (isClassLoader()) {
            JavaClassLoader associatedClassLoader = getAssociatedClassLoader();
            for (Iterator classes = associatedClassLoader.getDefinedClasses(); classes.hasNext(); ) {
                Object potentialClass = classes.next();
                if (potentialClass instanceof JavaClass) {
                    JavaClass currentClass = (JavaClass) potentialClass;
                    JavaReference ref = new JavaReference(_javaVM, this, currentClass, "Loaded class", JavaReference.REFERENCE_LOADED_CLASS, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
                    _references.add(ref);
                }
            }
        }
        if (isMonitor()) {
        // need to figure out whether we need additional references here (for example to the owning thread)
        }
        if (isThread()) {
        // need to figure out whether we need additional references here
        }
        if (isClass()) {
            JavaClass associatedClass = getAssociatedClass();
            JavaReference ref = new JavaReference(_javaVM, this, associatedClass, "Associated class", JavaReference.REFERENCE_ASSOCIATED_CLASS, JavaReference.HEAP_ROOT_UNKNOWN, JavaReference.REACHABILITY_STRONG);
            _references.add(ref);
        }
    }
    return _references.iterator();
}
Also used : CorruptDataException(com.ibm.dtfj.image.CorruptDataException) JavaClass(com.ibm.dtfj.java.JavaClass) JavaClassLoader(com.ibm.dtfj.java.JavaClassLoader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector)

Example 27 with JavaClassLoader

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

the class PHDJavaClassLoader method processData.

private void processData(HeapdumpReader reader, final PHDImage parentImage, final ImageAddressSpace space, final PHDJavaRuntime runtime) throws IOException {
    final JavaClassLoader loader = this;
    final int adjustLen = reader.version() == 4 && reader.isJ9() ? 1 : 0;
    final int[] realClasses = new int[1];
    try {
        reader.parse(new PortableHeapDumpListener() {

            long prevAddress;

            PHDJavaClass prevObjClass;

            private void updateSizes(long address) {
                if (prevObjClass != null)
                    prevObjClass.updateSize(prevAddress, address);
                prevObjClass = null;
                prevAddress = address;
            }

            public void classDump(long address, long superAddress, String name, int size, int flags, int hashCode, LongEnumeration refs) throws Exception {
                updateSizes(address);
                classes.put(address, new PHDJavaClass.Builder(space, runtime, loader, address, superAddress, name).size(size).flags(flags).hashCode(hashCode).refs(refs).build());
                updateAddresses(address, size, name);
                ++realClasses[0];
            }

            public void objectArrayDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, int length, long instanceSize) throws Exception {
                int refsLen = refs.numberOfElements();
                int adjustLen2 = Math.min(adjustLen, refsLen);
                updateSizes(address);
                updateAddresses(address, classAddress, length - adjustLen2);
                long classAddress2 = adjustLen2 == 1 ? refs.nextLong() : 0;
                genArrayClasses(space, runtime, loader, adjustLen2, classAddress, classAddress2);
            }

            public void objectDump(long address, long classAddress, int flags, int hashCode, LongEnumeration refs, long instanceSize) throws Exception {
                updateSizes(address);
                updateAddresses(address, classAddress, -1);
                genObjectClass(space, runtime, loader, classAddress, hashCode);
            }

            public void primitiveArrayDump(long address, int type, int length, int flags, int hashCode, long instanceSize) throws Exception {
                updateSizes(address);
                updateAddresses(address, type, length);
            }

            private void genObjectClass(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, long classAddress, int hashCode) {
                if (!classes.containsKey(classAddress)) {
                    PHDJavaClass objClass = new PHDJavaClass.Builder(space, runtime, loader, classAddress, PHDJavaClass.UNKNOWN_SUPERCLASS, PHDJavaClass.UNKNOWN_NONARRAY).build();
                    classes.put(classAddress, objClass);
                    updateAddresses(classAddress, 100, null);
                }
                prevObjClass = (PHDJavaClass) findClass(classAddress);
            }

            private PHDJavaClass genArrayClass(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, long classAddress, long sup, String name) {
                PHDJavaClass elemCls;
                int size = 100;
                elemCls = new PHDJavaClass.Builder(space, runtime, loader, classAddress, sup, name).build();
                classes.put(classAddress, elemCls);
                updateAddresses(classAddress, size, name);
                return elemCls;
            }

            private void genArrayClasses(final ImageAddressSpace space, final PHDJavaRuntime runtime, final JavaClassLoader loader, final int adjustLen, long classAddress, long classAddress2) {
                if (adjustLen == 1) {
                    // Java 5.0 arrays with type = base component type e.g. String
                    // First reference = actual array type e.g. [[Ljava.lang.String;
                    JavaClass arrayCls;
                    if (!classes.containsKey(classAddress2)) {
                        String name = PHDJavaClass.UNKNOWN_ARRAY;
                        arrayCls = genArrayClass(space, runtime, loader, classAddress2, jlo, name);
                    } else {
                        arrayCls = findClass(classAddress2);
                    }
                    arrayClasses.put(classAddress, arrayCls);
                    if (!classes.containsKey(classAddress)) {
                        String name = PHDJavaClass.UNKNOWN_NONARRAY;
                        genArrayClass(space, runtime, loader, classAddress, PHDJavaClass.UNKNOWN_SUPERCLASS, name);
                    }
                } else {
                    arrayClasses.put(classAddress, null);
                }
                return;
            }
        });
    } catch (EOFException e) {
        classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Truncated dump found building class " + realClasses[0], null, e));
    } catch (IOException e) {
        classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Corrupted dump found building class " + realClasses[0], null, e));
    } catch (Exception e) {
        classes.put(runtime.nextDummyClassAddr(), new PHDCorruptJavaClass("Building class " + realClasses[0], null, e));
    } finally {
        reader.close();
        reader = null;
    }
    jlo = 0;
    // Use an uncached search as the index hasn't been built.
    JavaClass jco = findClassUncached("java/lang/Object");
    if (jco != null) {
        ImagePointer ip = jco.getID();
        if (ip != null)
            jlo = ip.getAddress();
    } else {
        // If the dump is very corrupt it won't have java.lang.Object - so create one
        jco = new PHDJavaClass.Builder(space, runtime, loader, 0, 0, "java/lang/Object").build();
        jlo = runtime.nextDummyClassAddr();
        classes.put(jlo, jco);
    }
    // If the dump is very corrupt it won't have java.lang.Class - so create one
    JavaClass jcl = findClassUncached("java/lang/Class");
    if (jcl == null) {
        jcl = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, "java/lang/Class").build();
        classes.put(runtime.nextDummyClassAddr(), jcl);
    }
    // nor will it have primitive array classes
    for (int i = 0; i < PHDJavaRuntime.arrayTypeName.length; ++i) {
        JavaClass jc = findClassUncached(PHDJavaRuntime.arrayTypeName[i]);
        if (jc == null) {
            jc = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, PHDJavaRuntime.arrayTypeName[i]).build();
            classes.put(runtime.nextDummyClassAddr(), jc);
        }
    }
    // Bug in PHD dumps - some Java 6 types have the type of an array being the whole array, not the elements
    // Is the type of the array the whole array or the type of an element?
    boolean arrayTypeIsArray = true;
    for (Long id : arrayClasses.keySet()) {
        JavaClass cl1 = findClass(id);
        // Corrupt dump might not have the class!
        if (cl1 == null)
            continue;
        try {
            if (!cl1.isArray()) {
                arrayTypeIsArray = false;
            }
        } catch (CorruptDataException e) {
            // If we don't know the type then presume they are not arrays
            arrayTypeIsArray = false;
        }
    }
    // Create a mapping from the phd id of an array object to the type of the whole array
    for (Long id : arrayClasses.keySet()) {
        JavaClass cl1 = findClass(id);
        if (cl1 == null) {
            String name = arrayTypeIsArray ? PHDJavaClass.UNKNOWN_ARRAY : null;
            long sup = arrayTypeIsArray ? jlo : PHDJavaClass.UNKNOWN_SUPERCLASS;
            cl1 = new PHDJavaClass.Builder(space, runtime, loader, id, sup, name).build();
            classes.put(id, cl1);
        }
        JavaClass ar = arrayClasses.get(id);
        if (ar != null) {
            ImagePointer ip = ar.getID();
            if (ip != null) {
                JavaClass ar2 = findClass(ip.getAddress());
                if (ar2 != null) {
                    ar = ar2;
                }
            }
        // cl1 is not a component type for multidimensional arrays, but is the scalar type
        // ((PHDJavaClass)ar).setComponentType(cl1);
        }
        JavaClass cl2;
        if (arrayTypeIsArray) {
            cl2 = cl1;
        } else if (ar != null) {
            cl2 = ar;
        } else {
            try {
                String name = cl1.getName();
                String arrayName = arrayName(name);
                Set<JavaClass> s = findClasses(arrayName);
                if (s.size() == 0) {
                    // Array class doesn't exist, so create a dummy one
                    cl2 = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, arrayName).componentType(cl1).build();
                } else if (s.size() == 1) {
                    // Only one, so use it
                    cl2 = s.iterator().next();
                } else {
                    // Multiple classes, so if possible choose one
                    cl2 = null;
                    for (JavaClass cl3 : s) {
                        // If so then it is the one.
                        if (PHDJavaClass.referencesClass(cl3, cl1)) {
                            cl2 = cl3;
                            break;
                        }
                    }
                }
            } catch (CorruptDataException e) {
                // Array class doesn't exist, so create a dummy one
                cl2 = new PHDJavaClass.Builder(space, runtime, loader, 0, jlo, PHDJavaClass.UNKNOWN_ARRAY).componentType(cl1).build();
            }
            if (cl2 instanceof PHDJavaClass) {
                // We know the component type, so remember it for later
                ((PHDJavaClass) cl2).setComponentType(cl1);
            }
        }
        // Even if the array class is null (we couldn't decide), mark it so we know for later
        arrayClasses.put(id, cl2);
    }
    initCache();
}
Also used : PortableHeapDumpListener(com.ibm.dtfj.phd.parser.PortableHeapDumpListener) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) IOException(java.io.IOException) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) IOException(java.io.IOException) EOFException(java.io.EOFException) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) ImageAddressSpace(com.ibm.dtfj.image.ImageAddressSpace) ImagePointer(com.ibm.dtfj.image.ImagePointer) LongEnumeration(com.ibm.dtfj.phd.util.LongEnumeration) JavaClassLoader(com.ibm.dtfj.java.JavaClassLoader) JavaClass(com.ibm.dtfj.java.JavaClass) EOFException(java.io.EOFException)

Example 28 with JavaClassLoader

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

the class HeapdumpCommand method getClassReferences.

/* Reference code reimplemented (rather than using the DTFJ getReferences() API)
	 * because we are trying to match the behaviour of the runtime heapdump rather than
	 * the GC spec. The set of references we're trying to create is different.
	 */
/**
 * Gets the references for the supplied class
 *
 * @param thisJavaClass Class being examined
 */
private ReferenceIterator getClassReferences(JavaClass thisJavaClass) {
    List references = new LinkedList();
    try {
        // Class object instance references
        addReferences(thisJavaClass.getObject(), references);
        // Statics
        addStaticReferences(thisJavaClass, references);
        addProtectionDomainReference(thisJavaClass, references);
        // Constant pool class references
        Iterator constantPoolIt = thisJavaClass.getConstantPoolReferences();
        while (constantPoolIt.hasNext()) {
            Object cpObject = constantPoolIt.next();
            if (cpObject instanceof JavaClass) {
                // Found a class reference, add it to the list
                JavaClass cpJavaClass = (JavaClass) cpObject;
                references.add(new Long(cpJavaClass.getObject().getID().getAddress()));
            }
        }
        // Superclass references
        JavaClass superClass = thisJavaClass.getSuperclass();
        while (null != superClass) {
            references.add(new Long(superClass.getObject().getID().getAddress()));
            superClass = superClass.getSuperclass();
        }
        // Classloader
        JavaClassLoader loader = thisJavaClass.getClassLoader();
        if (loader != null) {
            JavaObject loaderObject = loader.getObject();
            if (loaderObject != null) {
                references.add(new Long(loaderObject.getID().getAddress()));
            } else {
                reportError("Null loader object returned for class: " + thisJavaClass.getName() + "(" + thisJavaClass.getID() + ")", null);
                _numberOfErrors++;
            }
        } else {
            reportError("Null classloader returned for class: " + thisJavaClass.getName() + "(" + thisJavaClass.getID() + ")", null);
            _numberOfErrors++;
        }
    } catch (DTFJException ex) {
        reportError(null, ex);
        _numberOfErrors++;
    }
    return new LongListReferenceIterator(references);
}
Also used : JavaClass(com.ibm.dtfj.java.JavaClass) JavaClassLoader(com.ibm.dtfj.java.JavaClassLoader) JavaObject(com.ibm.dtfj.java.JavaObject) DTFJException(com.ibm.dtfj.image.DTFJException) ReferenceIterator(com.ibm.jvm.dtfjview.heapdump.ReferenceIterator) Iterator(java.util.Iterator) LongListReferenceIterator(com.ibm.jvm.dtfjview.heapdump.LongListReferenceIterator) LinkedList(java.util.LinkedList) List(java.util.List) JavaObject(com.ibm.dtfj.java.JavaObject) LongListReferenceIterator(com.ibm.jvm.dtfjview.heapdump.LongListReferenceIterator) LinkedList(java.util.LinkedList)

Example 29 with JavaClassLoader

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

the class HeapdumpCommand method dumpClasses.

/**
 * Walks the runtime classes and passes them through the formatter interface
 */
private void dumpClasses(HeapDumpFormatter formatter, JavaRuntime runtime) throws IOException {
    Iterator classLoaderIt = runtime.getJavaClassLoaders();
    int numberOfClasses = 0;
    ITERATING_LOADERS: while (classLoaderIt.hasNext()) {
        Object potential = classLoaderIt.next();
        if (potential instanceof CorruptData) {
            _numberOfErrors++;
            reportError("CorruptData found in classloader list at address: " + ((CorruptData) potential).getAddress(), null);
            continue ITERATING_LOADERS;
        }
        JavaClassLoader thisClassLoader = (JavaClassLoader) potential;
        Iterator classesIt = thisClassLoader.getDefinedClasses();
        ITERATING_CLASSES: while (classesIt.hasNext()) {
            potential = classesIt.next();
            numberOfClasses++;
            try {
                if (potential instanceof CorruptData) {
                    _numberOfErrors++;
                    reportError("CorruptData found in class list for classloader " + Long.toHexString(thisClassLoader.getObject().getID().getAddress()) + " at address: " + ((CorruptData) potential).getAddress(), null);
                    continue ITERATING_CLASSES;
                }
                JavaClass thisJavaClass = (JavaClass) potential;
                JavaClass superClass = thisJavaClass.getSuperclass();
                JavaObject classObject = thisJavaClass.getObject();
                long instanceSize;
                if (thisJavaClass.isArray()) {
                    instanceSize = 0;
                } else {
                    instanceSize = thisJavaClass.getInstanceSize();
                }
                int hashcode = 0;
                if (_is32BitHash) {
                    // JVMs from 2.6 on, optional 32-bit hashcodes, if object was hashed
                    try {
                        hashcode = classObject != null ? (int) classObject.getPersistentHashcode() : 0;
                    } catch (DataUnavailable ex) {
                    // no persistent hashcode for this object, pass hashcode=0 to the heapdump formatter
                    }
                } else {
                    // JVMs prior to 2.6, all objects should have a 16-bit hashcode
                    hashcode = classObject != null ? (int) classObject.getHashcode() : 0;
                }
                formatter.addClass(classObject.getID().getAddress(), thisJavaClass.getName(), superClass != null ? superClass.getID().getAddress() : 0, classObject != null ? (int) classObject.getSize() : 0, instanceSize, hashcode, getClassReferences(thisJavaClass));
            } catch (DTFJException ex) {
                // Handle CorruptDataException and DataUnavailableException the same way
                _numberOfErrors++;
                reportError(null, ex);
                continue ITERATING_CLASSES;
            }
        }
    }
    _numberOfClasses = numberOfClasses;
    if ((pdSkipCount > 0) && _verbose) {
        out.println("Warning : The protection domain information was not available for " + pdSkipCount + " classes");
    }
}
Also used : JavaClassLoader(com.ibm.dtfj.java.JavaClassLoader) JavaClass(com.ibm.dtfj.java.JavaClass) JavaObject(com.ibm.dtfj.java.JavaObject) DTFJException(com.ibm.dtfj.image.DTFJException) 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)

Example 30 with JavaClassLoader

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

the class ClassOutput method printRuntimeClassAndLoader.

public static String printRuntimeClassAndLoader(final JavaClass jc, final PrintStream out) {
    String classLoaderInfo;
    String classLoaderClass;
    String cdeInfo = "N/A (CorruptDataException occurred)";
    try {
        JavaClassLoader jClassLoader = jc.getClassLoader();
        classLoaderInfo = "0x" + Long.toHexString(jClassLoader.getObject().getID().getAddress());
        classLoaderClass = jClassLoader.getObject().getJavaClass().getName();
    } catch (CorruptDataException cde) {
        classLoaderInfo = null;
        classLoaderClass = cdeInfo;
    }
    String spaces = "    ";
    out.print(spaces);
    out.print("Class ID = 0x" + Long.toHexString(jc.getID().getAddress()));
    out.print(spaces);
    out.print("Class Loader " + classLoaderClass + "(" + classLoaderInfo + ")");
    out.print("\n");
    return classLoaderInfo;
}
Also used : JavaClassLoader(com.ibm.dtfj.java.JavaClassLoader) CorruptDataException(com.ibm.dtfj.image.CorruptDataException)

Aggregations

JavaClassLoader (com.ibm.dtfj.java.JavaClassLoader)32 JavaClass (com.ibm.dtfj.java.JavaClass)24 Iterator (java.util.Iterator)16 JavaObject (com.ibm.dtfj.java.JavaObject)15 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)14 ArrayList (java.util.ArrayList)7 CorruptData (com.ibm.dtfj.image.CorruptData)6 JavaReference (com.ibm.dtfj.java.JavaReference)3 J9DDRCorruptData (com.ibm.j9ddr.view.dtfj.image.J9DDRCorruptData)3 List (java.util.List)3 DTFJException (com.ibm.dtfj.image.DTFJException)2 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)2 ImagePointer (com.ibm.dtfj.image.ImagePointer)2 JavaMethod (com.ibm.dtfj.java.JavaMethod)2 JavaRuntime (com.ibm.dtfj.java.JavaRuntime)2 PortableHeapDumpListener (com.ibm.dtfj.phd.parser.PortableHeapDumpListener)2 LongEnumeration (com.ibm.dtfj.phd.util.LongEnumeration)2 J9DDRDTFJUtils.corruptIterator (com.ibm.j9ddr.view.dtfj.J9DDRDTFJUtils.corruptIterator)2 J9Object (com.ibm.j9ddr.vm29.structure.J9Object)2 CorruptJavaObject (com.ibm.j9ddr.vm29.view.dtfj.java.corrupt.CorruptJavaObject)2