jdk/src/share/classes/com/sun/tools/hat/internal/model/Snapshot.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 25186 63e1a2ec30f5
child 25798 0b2f54e47bc4
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    30
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package com.sun.tools.hat.internal.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.hat.internal.parser.ReadBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.hat.internal.util.Misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Represents a snapshot of the Java objects in the VM at one instant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This is the top-level "model" object read out of a single .hprof or .bod
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class Snapshot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static long SMALL_ID_MASK = 0x0FFFFFFFFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final JavaField[] EMPTY_FIELD_ARRAY = new JavaField[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final JavaStatic[] EMPTY_STATIC_ARRAY = new JavaStatic[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // all heap objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Hashtable<Number, JavaHeapObject> heapObjects =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                 new Hashtable<Number, JavaHeapObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Hashtable<Number, JavaClass> fakeClasses =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                 new Hashtable<Number, JavaClass>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // all Roots in this Snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Vector<Root> roots = new Vector<Root>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // name-to-class map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Map<String, JavaClass> classes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                 new TreeMap<String, JavaClass>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // new objects relative to a baseline - lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private volatile Map<JavaHeapObject, Boolean> newObjects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // allocation site traces for all objects - lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private volatile Map<JavaHeapObject, StackTrace> siteTraces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // object-to-Root map for all objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Map<JavaHeapObject, Root> rootsMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                 new HashMap<JavaHeapObject, Root>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // soft cache of finalizeable objects - lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private SoftReference<Vector> finalizablesCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // represents null reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private JavaThing nullThing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // java.lang.ref.Reference class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private JavaClass weakReferenceClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // index of 'referent' field in java.lang.ref.Reference class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private int referentFieldIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // java.lang.Class class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private JavaClass javaLangClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // java.lang.String class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private JavaClass javaLangString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // java.lang.ClassLoader class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private JavaClass javaLangClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // unknown "other" array class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private volatile JavaClass otherArrayType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Stuff to exclude from reachable query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private ReachableExcludes reachableExcludes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // the underlying heap dump buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private ReadBuffer readBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // True iff some heap objects have isNew set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private boolean hasNewSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private boolean unresolvedObjectsOK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // whether object array instances have new style class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // old style (element) class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private boolean newStyleArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // object id size in the heap dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int identifierSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // minimum object size - accounts for object header in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // most Java virtual machines - we assume 2 identifierSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // (which is true for Sun's hotspot JVM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private int minimumObjectSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Snapshot(ReadBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        nullThing = new HackJavaValue("<null>", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        readBuf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void setSiteTrace(JavaHeapObject obj, StackTrace trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (trace != null && trace.getFrames().length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            initSiteTraces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            siteTraces.put(obj, trace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public StackTrace getSiteTrace(JavaHeapObject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (siteTraces != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return siteTraces.get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void setNewStyleArrayClass(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        newStyleArrayClass = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean isNewStyleArrayClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return newStyleArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void setIdentifierSize(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        identifierSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        minimumObjectSize = 2 * size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public int getIdentifierSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return identifierSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int getMinimumObjectSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return minimumObjectSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void addHeapObject(long id, JavaHeapObject ho) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        heapObjects.put(makeId(id), ho);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void addRoot(Root r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        r.setIndex(roots.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        roots.addElement(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void addClass(long id, JavaClass c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        addHeapObject(id, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        putInClassesMap(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    JavaClass addFakeInstanceClass(long classID, int instSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Create a fake class name based on ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String name = "unknown-class<@" + Misc.toHex(classID) + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Create fake fields convering the given instance size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Create as many as int type fields and for the left over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // size create byte type fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int numInts = instSize / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int numBytes = instSize % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        JavaField[] fields = new JavaField[numInts + numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (i = 0; i < numInts; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            fields[i] = new JavaField("unknown-field-" + i, "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (i = 0; i < numBytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            fields[i + numInts] = new JavaField("unknown-field-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                                i + numInts, "B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Create fake instance class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        JavaClass c = new JavaClass(name, 0, 0, 0, 0, fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                 EMPTY_STATIC_ARRAY, instSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // Add the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        addFakeClass(makeId(classID), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return true iff it's possible that some JavaThing instances might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          isNew set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see JavaThing.isNew()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public boolean getHasNewSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return hasNewSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // Used in the body of resolve()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class MyVisitor extends AbstractJavaHeapObjectVisitor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        JavaHeapObject t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public void visit(JavaHeapObject other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            other.addReferenceFrom(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    // To show heap parsing progress, we print a '.' after this limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private static final int DOT_LIMIT = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Called after reading complete, to initialize the structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void resolve(boolean calculateRefs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        System.out.println("Resolving " + heapObjects.size() + " objects...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // First, resolve the classes.  All classes must be resolved before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // we try any objects, because the objects use classes in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        javaLangClass = findClass("java.lang.Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (javaLangClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            System.out.println("WARNING:  hprof file does not include java.lang.Class!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            javaLangClass = new JavaClass("java.lang.Class", 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                 EMPTY_FIELD_ARRAY, EMPTY_STATIC_ARRAY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            addFakeClass(javaLangClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        javaLangString = findClass("java.lang.String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (javaLangString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            System.out.println("WARNING:  hprof file does not include java.lang.String!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            javaLangString = new JavaClass("java.lang.String", 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                 EMPTY_FIELD_ARRAY, EMPTY_STATIC_ARRAY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            addFakeClass(javaLangString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        javaLangClassLoader = findClass("java.lang.ClassLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (javaLangClassLoader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.out.println("WARNING:  hprof file does not include java.lang.ClassLoader!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            javaLangClassLoader = new JavaClass("java.lang.ClassLoader", 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                 EMPTY_FIELD_ARRAY, EMPTY_STATIC_ARRAY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            addFakeClass(javaLangClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (JavaHeapObject t : heapObjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (t instanceof JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                t.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Now, resolve everything else.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        for (JavaHeapObject t : heapObjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (!(t instanceof JavaClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                t.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        heapObjects.putAll(fakeClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        fakeClasses.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        weakReferenceClass = findClass("java.lang.ref.Reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (weakReferenceClass == null)  {      // JDK 1.1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            weakReferenceClass = findClass("sun.misc.Ref");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            referentFieldIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            JavaField[] fields = weakReferenceClass.getFieldsForInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if ("referent".equals(fields[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    referentFieldIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (calculateRefs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            calculateReferencesToObjects();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            System.out.print("Eliminating duplicate references");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // This println refers to the *next* step
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        for (JavaHeapObject t : heapObjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            t.setupReferers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (calculateRefs && count % DOT_LIMIT == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                System.out.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (calculateRefs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // to ensure that Iterator.remove() on getClasses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // result will throw exception..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        classes = Collections.unmodifiableMap(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private void calculateReferencesToObjects() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        System.out.print("Chasing references, expect "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                         + (heapObjects.size() / DOT_LIMIT) + " dots");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        MyVisitor visitor = new MyVisitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        for (JavaHeapObject t : heapObjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            visitor.t = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // call addReferenceFrom(t) on all objects t references:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            t.visitReferencedObjects(visitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (count % DOT_LIMIT == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                System.out.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        for (Root r : roots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            r.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            JavaHeapObject t = findThing(r.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                t.addReferenceFromRoot(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void markNewRelativeTo(Snapshot baseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        hasNewSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        for (JavaHeapObject t : heapObjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            boolean isNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            long thingID = t.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (thingID == 0L || thingID == -1L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                isNew = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                JavaThing other = baseline.findThing(t.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    isNew = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    isNew = !t.isSameTypeAs(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            t.setNew(isNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public Enumeration<JavaHeapObject> getThings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return heapObjects.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public JavaHeapObject findThing(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Number idObj = makeId(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        JavaHeapObject jho = heapObjects.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return jho != null? jho : fakeClasses.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public JavaHeapObject findThing(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return findThing(Misc.parseHex(id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public JavaClass findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (name.startsWith("0x")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return (JavaClass) findThing(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return classes.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Return an Iterator of all of the classes in this snapshot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public Iterator getClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // note that because classes is a TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // classes are already sorted by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return classes.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public JavaClass[] getClassesArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        JavaClass[] res = new JavaClass[classes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        classes.values().toArray(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public synchronized Enumeration getFinalizerObjects() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        Vector obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (finalizablesCache != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            (obj = finalizablesCache.get()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return obj.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        JavaClass clazz = findClass("java.lang.ref.Finalizer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        JavaObject queue = (JavaObject) clazz.getStaticField("queue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        JavaThing tmp = queue.getField("head");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Vector<JavaHeapObject> finalizables = new Vector<JavaHeapObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (tmp != getNullThing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            JavaObject head = (JavaObject) tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                JavaHeapObject referent = (JavaHeapObject) head.getField("referent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                JavaThing next = head.getField("next");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (next == getNullThing() || next.equals(head)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                head = (JavaObject) next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                finalizables.add(referent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        finalizablesCache = new SoftReference<Vector>(finalizables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return finalizables.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public Enumeration<Root> getRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return roots.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public Root[] getRootsArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        Root[] res = new Root[roots.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        roots.toArray(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public Root getRootAt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return roots.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public ReferenceChain[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    rootsetReferencesTo(JavaHeapObject target, boolean includeWeak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        Vector<ReferenceChain> fifo = new Vector<ReferenceChain>();  // This is slow... A real fifo would help
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // Must be a fifo to go breadth-first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        Hashtable<JavaHeapObject, JavaHeapObject> visited = new Hashtable<JavaHeapObject, JavaHeapObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // Objects are added here right after being added to fifo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        Vector<ReferenceChain> result = new Vector<ReferenceChain>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        visited.put(target, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        fifo.addElement(new ReferenceChain(target, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        while (fifo.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            ReferenceChain chain = fifo.elementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            fifo.removeElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            JavaHeapObject curr = chain.getObj();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (curr.getRoot() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                result.addElement(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                // Even though curr is in the rootset, we want to explore its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // referers, because they might be more interesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            Enumeration referers = curr.getReferers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            while (referers.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                JavaHeapObject t = (JavaHeapObject) referers.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                if (t != null && !visited.containsKey(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    if (includeWeak || !t.refersOnlyWeaklyTo(this, curr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        visited.put(t, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        fifo.addElement(new ReferenceChain(t, chain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        ReferenceChain[] realResult = new ReferenceChain[result.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        for (int i = 0; i < result.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            realResult[i] =  result.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return realResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public boolean getUnresolvedObjectsOK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return unresolvedObjectsOK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public void setUnresolvedObjectsOK(boolean v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        unresolvedObjectsOK = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public JavaClass getWeakReferenceClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return weakReferenceClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public int getReferentFieldIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return referentFieldIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public JavaThing getNullThing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return nullThing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public void setReachableExcludes(ReachableExcludes e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        reachableExcludes = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public ReachableExcludes getReachableExcludes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return reachableExcludes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    // package privates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    void addReferenceFromRoot(Root r, JavaHeapObject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        Root root = rootsMap.get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            rootsMap.put(obj, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            rootsMap.put(obj, root.mostInteresting(r));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    Root getRoot(JavaHeapObject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return rootsMap.get(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    JavaClass getJavaLangClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return javaLangClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    JavaClass getJavaLangString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return javaLangString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    JavaClass getJavaLangClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return javaLangClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    JavaClass getOtherArrayType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (otherArrayType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                if (otherArrayType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    addFakeClass(new JavaClass("[<other>", 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                     EMPTY_FIELD_ARRAY, EMPTY_STATIC_ARRAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                     0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    otherArrayType = findClass("[<other>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return otherArrayType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    JavaClass getArrayClass(String elementSignature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        JavaClass clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        synchronized(classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            clazz = findClass("[" + elementSignature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                clazz = new JavaClass("[" + elementSignature, 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                   EMPTY_FIELD_ARRAY, EMPTY_STATIC_ARRAY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                addFakeClass(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                // This is needed because the JDK only creates Class structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                // for array element types, not the arrays themselves.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // analysis, though, we need to pretend that there's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // JavaClass for the array type, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    ReadBuffer getReadBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return readBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    void setNew(JavaHeapObject obj, boolean isNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        initNewObjects();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (isNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            newObjects.put(obj, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    boolean isNew(JavaHeapObject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (newObjects != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return newObjects.get(obj) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    // Internals only below this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private Number makeId(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (identifierSize == 4) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   584
            return (int)id;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        } else {
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 5506
diff changeset
   586
            return id;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private void putInClassesMap(JavaClass c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        String name = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (classes.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            // more than one class can have the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // if so, create a unique name by appending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // - and id string to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            name += "-" + c.getIdString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        classes.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    private void addFakeClass(JavaClass c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        putInClassesMap(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        c.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private void addFakeClass(Number id, JavaClass c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        fakeClasses.put(id, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        addFakeClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private synchronized void initNewObjects() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (newObjects == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                if (newObjects == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    newObjects = new HashMap<JavaHeapObject, Boolean>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    private synchronized void initSiteTraces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (siteTraces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (siteTraces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    siteTraces = new HashMap<JavaHeapObject, StackTrace>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
}