aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/utilities/AbstractHeapGraphWriter.java
blob: 31880e8eab7406b5708bfc6c9c215f2c140100b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.utilities;

import java.io.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;

/**
 * This is abstract base class for heap graph writers. This class does
 * not assume any file format for the heap graph. It hides heap
 * iteration, object (fields) iteration mechanism from derived
 * classes. This class does not even accept OutputStream etc. so that
 * derived class can construct specific writer/filter from input
 * stream.
 */

public abstract class AbstractHeapGraphWriter implements HeapGraphWriter {
    // the function iterates heap and calls Oop type specific writers
    protected void write() throws IOException {
        SymbolTable symTbl = VM.getVM().getSymbolTable();
        javaLangClass = symTbl.probe("java/lang/Class");
        javaLangString = symTbl.probe("java/lang/String");
        javaLangThread = symTbl.probe("java/lang/Thread");
        ObjectHeap heap = VM.getVM().getObjectHeap();
        try {
            heap.iterate(new DefaultHeapVisitor() {
                    public void prologue(long usedSize) {
                        try {
                            writeHeapHeader();
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public boolean doObj(Oop oop) {
                        try {
                            if (oop instanceof TypeArray) {
                                writePrimitiveArray((TypeArray)oop);
                            } else if (oop instanceof ObjArray) {
                                Klass klass = oop.getKlass();
                                ObjArrayKlass oak = (ObjArrayKlass) klass;
                                Klass bottomType = oak.getBottomKlass();
                                if (bottomType instanceof InstanceKlass ||
                                    bottomType instanceof TypeArrayKlass) {
                                    writeObjectArray((ObjArray)oop);
                                } else {
                                    writeInternalObject(oop);
                                }
                            } else if (oop instanceof Instance) {
                                Instance instance = (Instance) oop;
                                Klass klass = instance.getKlass();
                                Symbol name = klass.getName();
                                if (name.equals(javaLangString)) {
                                    writeString(instance);
                                } else if (name.equals(javaLangClass)) {
                                    writeClass(instance);
                                } else if (name.equals(javaLangThread)) {
                                    writeThread(instance);
                                } else {
                                    klass = klass.getSuper();
                                    while (klass != null) {
                                        name = klass.getName();
                                        if (name.equals(javaLangThread)) {
                                            writeThread(instance);
                                            return false;
                                        }
                                        klass = klass.getSuper();
                                    }
                                    writeInstance(instance);
                                }
                            } else {
                                // not-a-Java-visible oop
                                writeInternalObject(oop);
                            }
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                        return false;
                    }

                    public void epilogue() {
                        try {
                            writeHeapFooter();
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }
                });

                // write JavaThreads
                writeJavaThreads();

                // write JNI global handles
                writeGlobalJNIHandles();

        } catch (RuntimeException re) {
            handleRuntimeException(re);
        }
    }

    protected void writeJavaThreads() throws IOException {
        Threads threads = VM.getVM().getThreads();
        JavaThread jt = threads.first();
        int index = 1;
        while (jt != null) {
            if (jt.getThreadObj() != null) {
                // Note that the thread serial number range is 1-to-N
                writeJavaThread(jt, index);
                index++;
            }
            jt = jt.next();
        }
    }

    protected void writeJavaThread(JavaThread jt, int index)
                            throws IOException {
    }

    protected void writeGlobalJNIHandles() throws IOException {
        JNIHandles handles = VM.getVM().getJNIHandles();
        JNIHandleBlock blk = handles.globalHandles();
        if (blk != null) {
            try {
                blk.oopsDo(new AddressVisitor() {
                          public void visitAddress(Address handleAddr) {
                              try {
                                  if (handleAddr != null) {
                                      writeGlobalJNIHandle(handleAddr);
                                  }
                              } catch (IOException exp) {
                                  throw new RuntimeException(exp);
                              }
                          }
                              public void visitCompOopAddress(Address handleAddr) {
                             throw new RuntimeException("Should not reach here. JNIHandles are not compressed");
                          }
                       });
            } catch (RuntimeException re) {
                handleRuntimeException(re);
            }
        }
    }

    protected void writeGlobalJNIHandle(Address handleAddr) throws IOException {
    }

    protected void writeHeapHeader() throws IOException {
    }

    // write non-Java-visible (hotspot internal) object
    protected void writeInternalObject(Oop oop) throws IOException {
    }

    // write Java primitive array
    protected void writePrimitiveArray(TypeArray array) throws IOException {
        writeObject(array);
    }

    // write Java object array
    protected void writeObjectArray(ObjArray array) throws IOException {
        writeObject(array);
    }

    protected void writeInstance(Instance instance) throws IOException {
        writeObject(instance);
    }

    protected void writeString(Instance instance) throws IOException {
        writeInstance(instance);
    }

    protected void writeClass(Instance instance) throws IOException {
        writeInstance(instance);
    }

    protected void writeThread(Instance instance) throws IOException {
        writeInstance(instance);
    }

    protected void writeObject(Oop oop) throws IOException {
        writeObjectHeader(oop);
        writeObjectFields(oop);
        writeObjectFooter(oop);
    }

    protected void writeObjectHeader(Oop oop) throws IOException {
    }

    // write instance fields of given object
    protected void writeObjectFields(final Oop oop) throws IOException {
        try {
            oop.iterate(new DefaultOopVisitor() {
                    public void doOop(OopField field, boolean isVMField) {
                        try {
                            Oop ref = field.getValue(oop);
                            if (ref instanceof TypeArray ||
                                ref instanceof ObjArray  ||
                                ref instanceof Instance) {
                                writeReferenceField(oop, field);
                            } else {
                                writeInternalReferenceField(oop, field);
                            }
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doByte(ByteField field, boolean isVMField) {
                        try {
                            writeByteField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doChar(CharField field, boolean isVMField) {
                        try {
                            writeCharField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doBoolean(BooleanField field, boolean vField) {
                        try {
                            writeBooleanField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doShort(ShortField field, boolean isVMField) {
                        try {
                            writeShortField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doInt(IntField field, boolean isVMField) {
                        try {
                            writeIntField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doLong(LongField field, boolean isVMField) {
                        try {
                            writeLongField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doFloat(FloatField field, boolean isVMField) {
                        try {
                            writeFloatField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }

                    public void doDouble(DoubleField field, boolean vField) {
                        try {
                            writeDoubleField(oop, field);
                        } catch (IOException exp) {
                            throw new RuntimeException(exp);
                        }
                    }
                }, false);
        } catch (RuntimeException re) {
            handleRuntimeException(re);
        }
    }

    // object field writers
    protected void writeInternalReferenceField(Oop oop, OopField field)
        throws IOException {
    }

    protected void writeReferenceField(Oop oop, OopField field)
        throws IOException {
    }

    protected void writeByteField(Oop oop, ByteField field)
        throws IOException {
    }

    protected void writeCharField(Oop oop, CharField field)
        throws IOException {
    }

    protected void writeBooleanField(Oop oop, BooleanField field)
        throws IOException {
    }

    protected void writeShortField(Oop oop, ShortField field)
        throws IOException {
    }

    protected void writeIntField(Oop oop, IntField field)
        throws IOException {
    }

    protected void writeLongField(Oop oop, LongField field)
        throws IOException {
    }

    protected void writeFloatField(Oop oop, FloatField field)
        throws IOException {
    }

    protected void writeDoubleField(Oop oop, DoubleField field)
        throws IOException {
    }

    protected void writeObjectFooter(Oop oop) throws IOException {
    }

    protected void writeHeapFooter() throws IOException {
    }

    // HeapVisitor, OopVisitor methods can't throw any non-runtime
    // exception. But, derived class write methods (which are called
    // from visitor callbacks) may throw IOException. Hence, we throw
    // RuntimeException with origianal IOException as cause from the
    // visitor methods. This method gets back the original IOException
    // (if any) and re-throws the same.
    protected void handleRuntimeException(RuntimeException re)
        throws IOException {
        Throwable cause = re.getCause();
        if (cause != null && cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            // some other RuntimeException, just re-throw
            throw re;
        }
    }

    // whether a given oop is Java visible or hotspot internal?
    protected boolean isJavaVisible(Oop oop) {
        if (oop instanceof Instance || oop instanceof TypeArray) {
            return true;
        } else if (oop instanceof ObjArray) {
            ObjArrayKlass oak = (ObjArrayKlass) oop.getKlass();
            Klass bottomKlass = oak.getBottomKlass();
            return bottomKlass instanceof InstanceKlass ||
                   bottomKlass instanceof TypeArrayKlass;
        } else {
            return false;
        }
    }

    protected Symbol javaLangClass;
    protected Symbol javaLangString;
    protected Symbol javaLangThread;
}