2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* The contents of this file are subject to the Sun Public License
|
|
28 |
* Version 1.0 (the "License"); you may not use this file except in
|
|
29 |
* compliance with the License. A copy of the License is available at
|
|
30 |
* http://www.sun.com/, and in the file LICENSE.html in the
|
|
31 |
* doc directory.
|
|
32 |
*
|
|
33 |
* The Original Code is HAT. The Initial Developer of the
|
|
34 |
* Original Code is Bill Foote, with contributions from others
|
|
35 |
* at JavaSoft/Sun. Portions created by Bill Foote and others
|
|
36 |
* at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
|
|
37 |
*
|
|
38 |
* In addition to the formal license, I ask that you don't
|
|
39 |
* change the history or donations files without permission.
|
|
40 |
*
|
|
41 |
*/
|
|
42 |
|
|
43 |
package com.sun.tools.hat.internal.parser;
|
|
44 |
|
|
45 |
import java.io.*;
|
|
46 |
import java.util.Date;
|
|
47 |
import java.util.Hashtable;
|
|
48 |
import com.sun.tools.hat.internal.model.ArrayTypeCodes;
|
|
49 |
import com.sun.tools.hat.internal.model.*;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Object that's used to read a hprof file.
|
|
53 |
*
|
|
54 |
* @author Bill Foote
|
|
55 |
*/
|
|
56 |
|
|
57 |
public class HprofReader extends Reader /* imports */ implements ArrayTypeCodes {
|
|
58 |
|
|
59 |
final static int MAGIC_NUMBER = 0x4a415641;
|
|
60 |
// That's "JAVA", the first part of "JAVA PROFILE ..."
|
|
61 |
private final static String[] VERSIONS = {
|
|
62 |
" PROFILE 1.0\0",
|
|
63 |
" PROFILE 1.0.1\0",
|
|
64 |
" PROFILE 1.0.2\0",
|
|
65 |
};
|
|
66 |
|
|
67 |
private final static int VERSION_JDK12BETA3 = 0;
|
|
68 |
private final static int VERSION_JDK12BETA4 = 1;
|
|
69 |
private final static int VERSION_JDK6 = 2;
|
|
70 |
// These version numbers are indices into VERSIONS. The instance data
|
|
71 |
// member version is set to one of these, and it drives decisions when
|
|
72 |
// reading the file.
|
|
73 |
//
|
|
74 |
// Version 1.0.1 added HPROF_GC_PRIM_ARRAY_DUMP, which requires no
|
|
75 |
// version-sensitive parsing.
|
|
76 |
//
|
|
77 |
// Version 1.0.1 changed the type of a constant pool entry from a signature
|
|
78 |
// to a typecode.
|
|
79 |
//
|
|
80 |
// Version 1.0.2 added HPROF_HEAP_DUMP_SEGMENT and HPROF_HEAP_DUMP_END
|
|
81 |
// to allow a large heap to be dumped as a sequence of heap dump segments.
|
|
82 |
//
|
|
83 |
// The HPROF agent in J2SE 1.2 through to 5.0 generate a version 1.0.1
|
|
84 |
// file. In Java SE 6.0 the version is either 1.0.1 or 1.0.2 depending on
|
|
85 |
// the size of the heap (normally it will be 1.0.1 but for multi-GB
|
|
86 |
// heaps the heap dump will not fit in a HPROF_HEAP_DUMP record so the
|
|
87 |
// dump is generated as version 1.0.2).
|
|
88 |
|
|
89 |
//
|
|
90 |
// Record types:
|
|
91 |
//
|
|
92 |
static final int HPROF_UTF8 = 0x01;
|
|
93 |
static final int HPROF_LOAD_CLASS = 0x02;
|
|
94 |
static final int HPROF_UNLOAD_CLASS = 0x03;
|
|
95 |
static final int HPROF_FRAME = 0x04;
|
|
96 |
static final int HPROF_TRACE = 0x05;
|
|
97 |
static final int HPROF_ALLOC_SITES = 0x06;
|
|
98 |
static final int HPROF_HEAP_SUMMARY = 0x07;
|
|
99 |
|
|
100 |
static final int HPROF_START_THREAD = 0x0a;
|
|
101 |
static final int HPROF_END_THREAD = 0x0b;
|
|
102 |
|
|
103 |
static final int HPROF_HEAP_DUMP = 0x0c;
|
|
104 |
|
|
105 |
static final int HPROF_CPU_SAMPLES = 0x0d;
|
|
106 |
static final int HPROF_CONTROL_SETTINGS = 0x0e;
|
|
107 |
static final int HPROF_LOCKSTATS_WAIT_TIME = 0x10;
|
|
108 |
static final int HPROF_LOCKSTATS_HOLD_TIME = 0x11;
|
|
109 |
|
|
110 |
static final int HPROF_GC_ROOT_UNKNOWN = 0xff;
|
|
111 |
static final int HPROF_GC_ROOT_JNI_GLOBAL = 0x01;
|
|
112 |
static final int HPROF_GC_ROOT_JNI_LOCAL = 0x02;
|
|
113 |
static final int HPROF_GC_ROOT_JAVA_FRAME = 0x03;
|
|
114 |
static final int HPROF_GC_ROOT_NATIVE_STACK = 0x04;
|
|
115 |
static final int HPROF_GC_ROOT_STICKY_CLASS = 0x05;
|
|
116 |
static final int HPROF_GC_ROOT_THREAD_BLOCK = 0x06;
|
|
117 |
static final int HPROF_GC_ROOT_MONITOR_USED = 0x07;
|
|
118 |
static final int HPROF_GC_ROOT_THREAD_OBJ = 0x08;
|
|
119 |
|
|
120 |
static final int HPROF_GC_CLASS_DUMP = 0x20;
|
|
121 |
static final int HPROF_GC_INSTANCE_DUMP = 0x21;
|
|
122 |
static final int HPROF_GC_OBJ_ARRAY_DUMP = 0x22;
|
|
123 |
static final int HPROF_GC_PRIM_ARRAY_DUMP = 0x23;
|
|
124 |
|
|
125 |
static final int HPROF_HEAP_DUMP_SEGMENT = 0x1c;
|
|
126 |
static final int HPROF_HEAP_DUMP_END = 0x2c;
|
|
127 |
|
|
128 |
private final static int T_CLASS = 2;
|
|
129 |
|
|
130 |
private int version; // The version of .hprof being read
|
|
131 |
|
|
132 |
private int debugLevel;
|
|
133 |
private int currPos; // Current position in the file
|
|
134 |
|
|
135 |
private int dumpsToSkip;
|
|
136 |
private boolean callStack; // If true, read the call stack of objects
|
|
137 |
|
|
138 |
private int identifierSize; // Size, in bytes, of identifiers.
|
|
139 |
private Hashtable<Long, String> names;
|
|
140 |
|
|
141 |
// Hashtable<Integer, ThreadObject>, used to map the thread sequence number
|
|
142 |
// (aka "serial number") to the thread object ID for
|
|
143 |
// HPROF_GC_ROOT_THREAD_OBJ. ThreadObject is a trivial inner class,
|
|
144 |
// at the end of this file.
|
|
145 |
private Hashtable<Integer, ThreadObject> threadObjects;
|
|
146 |
|
|
147 |
// Hashtable<Long, String>, maps class object ID to class name
|
|
148 |
// (with / converted to .)
|
|
149 |
private Hashtable<Long, String> classNameFromObjectID;
|
|
150 |
|
|
151 |
// Hashtable<Integer, Integer>, maps class serial # to class object ID
|
|
152 |
private Hashtable<Integer, String> classNameFromSerialNo;
|
|
153 |
|
|
154 |
// Hashtable<Long, StackFrame> maps stack frame ID to StackFrame.
|
|
155 |
// Null if we're not tracking them.
|
|
156 |
private Hashtable<Long, StackFrame> stackFrames;
|
|
157 |
|
|
158 |
// Hashtable<Integer, StackTrace> maps stack frame ID to StackTrace
|
|
159 |
// Null if we're not tracking them.
|
|
160 |
private Hashtable<Integer, StackTrace> stackTraces;
|
|
161 |
|
|
162 |
private Snapshot snapshot;
|
|
163 |
|
|
164 |
public HprofReader(String fileName, PositionDataInputStream in,
|
|
165 |
int dumpNumber, boolean callStack, int debugLevel)
|
|
166 |
throws IOException {
|
|
167 |
super(in);
|
|
168 |
RandomAccessFile file = new RandomAccessFile(fileName, "r");
|
|
169 |
this.snapshot = new Snapshot(MappedReadBuffer.create(file));
|
|
170 |
this.dumpsToSkip = dumpNumber - 1;
|
|
171 |
this.callStack = callStack;
|
|
172 |
this.debugLevel = debugLevel;
|
|
173 |
names = new Hashtable<Long, String>();
|
|
174 |
threadObjects = new Hashtable<Integer, ThreadObject>(43);
|
|
175 |
classNameFromObjectID = new Hashtable<Long, String>();
|
|
176 |
if (callStack) {
|
|
177 |
stackFrames = new Hashtable<Long, StackFrame>(43);
|
|
178 |
stackTraces = new Hashtable<Integer, StackTrace>(43);
|
|
179 |
classNameFromSerialNo = new Hashtable<Integer, String>();
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
public Snapshot read() throws IOException {
|
|
184 |
currPos = 4; // 4 because of the magic number
|
|
185 |
version = readVersionHeader();
|
|
186 |
identifierSize = in.readInt();
|
|
187 |
snapshot.setIdentifierSize(identifierSize);
|
|
188 |
if (version >= VERSION_JDK12BETA4) {
|
|
189 |
snapshot.setNewStyleArrayClass(true);
|
|
190 |
} else {
|
|
191 |
snapshot.setNewStyleArrayClass(false);
|
|
192 |
}
|
|
193 |
|
|
194 |
currPos += 4;
|
|
195 |
if (identifierSize != 4 && identifierSize != 8) {
|
|
196 |
throw new IOException("I'm sorry, but I can't deal with an identifier size of " + identifierSize + ". I can only deal with 4 or 8.");
|
|
197 |
}
|
|
198 |
System.out.println("Dump file created " + (new Date(in.readLong())));
|
|
199 |
currPos += 8;
|
|
200 |
|
|
201 |
for (;;) {
|
|
202 |
int type;
|
|
203 |
try {
|
|
204 |
type = in.readUnsignedByte();
|
|
205 |
} catch (EOFException ignored) {
|
|
206 |
break;
|
|
207 |
}
|
|
208 |
in.readInt(); // Timestamp of this record
|
|
209 |
int length = in.readInt();
|
|
210 |
if (debugLevel > 0) {
|
|
211 |
System.out.println("Read record type " + type
|
|
212 |
+ ", length " + length
|
|
213 |
+ " at position " + toHex(currPos));
|
|
214 |
}
|
|
215 |
if (length < 0) {
|
|
216 |
throw new IOException("Bad record length of " + length
|
|
217 |
+ " at byte " + toHex(currPos+5)
|
|
218 |
+ " of file.");
|
|
219 |
}
|
|
220 |
currPos += 9 + length;
|
|
221 |
switch (type) {
|
|
222 |
case HPROF_UTF8: {
|
|
223 |
long id = readID();
|
|
224 |
byte[] chars = new byte[length - identifierSize];
|
|
225 |
in.readFully(chars);
|
|
226 |
names.put(new Long(id), new String(chars));
|
|
227 |
break;
|
|
228 |
}
|
|
229 |
case HPROF_LOAD_CLASS: {
|
|
230 |
int serialNo = in.readInt(); // Not used
|
|
231 |
long classID = readID();
|
|
232 |
int stackTraceSerialNo = in.readInt();
|
|
233 |
long classNameID = readID();
|
|
234 |
Long classIdI = new Long(classID);
|
|
235 |
String nm = getNameFromID(classNameID).replace('/', '.');
|
|
236 |
classNameFromObjectID.put(classIdI, nm);
|
|
237 |
if (classNameFromSerialNo != null) {
|
|
238 |
classNameFromSerialNo.put(new Integer(serialNo), nm);
|
|
239 |
}
|
|
240 |
break;
|
|
241 |
}
|
|
242 |
|
|
243 |
case HPROF_HEAP_DUMP: {
|
|
244 |
if (dumpsToSkip <= 0) {
|
|
245 |
try {
|
|
246 |
readHeapDump(length, currPos);
|
|
247 |
} catch (EOFException exp) {
|
|
248 |
handleEOF(exp, snapshot);
|
|
249 |
}
|
|
250 |
if (debugLevel > 0) {
|
|
251 |
System.out.println(" Finished processing instances in heap dump.");
|
|
252 |
}
|
|
253 |
return snapshot;
|
|
254 |
} else {
|
|
255 |
dumpsToSkip--;
|
|
256 |
skipBytes(length);
|
|
257 |
}
|
|
258 |
break;
|
|
259 |
}
|
|
260 |
|
|
261 |
case HPROF_HEAP_DUMP_END: {
|
|
262 |
if (version >= VERSION_JDK6) {
|
|
263 |
if (dumpsToSkip <= 0) {
|
|
264 |
skipBytes(length); // should be no-op
|
|
265 |
return snapshot;
|
|
266 |
} else {
|
|
267 |
// skip this dump (of the end record for a sequence of dump segments)
|
|
268 |
dumpsToSkip--;
|
|
269 |
}
|
|
270 |
} else {
|
|
271 |
// HPROF_HEAP_DUMP_END only recognized in >= 1.0.2
|
|
272 |
warn("Ignoring unrecognized record type " + type);
|
|
273 |
}
|
|
274 |
skipBytes(length); // should be no-op
|
|
275 |
break;
|
|
276 |
}
|
|
277 |
|
|
278 |
case HPROF_HEAP_DUMP_SEGMENT: {
|
|
279 |
if (version >= VERSION_JDK6) {
|
|
280 |
if (dumpsToSkip <= 0) {
|
|
281 |
try {
|
|
282 |
// read the dump segment
|
|
283 |
readHeapDump(length, currPos);
|
|
284 |
} catch (EOFException exp) {
|
|
285 |
handleEOF(exp, snapshot);
|
|
286 |
}
|
|
287 |
} else {
|
|
288 |
// all segments comprising the heap dump will be skipped
|
|
289 |
skipBytes(length);
|
|
290 |
}
|
|
291 |
} else {
|
|
292 |
// HPROF_HEAP_DUMP_SEGMENT only recognized in >= 1.0.2
|
|
293 |
warn("Ignoring unrecognized record type " + type);
|
|
294 |
skipBytes(length);
|
|
295 |
}
|
|
296 |
break;
|
|
297 |
}
|
|
298 |
|
|
299 |
case HPROF_FRAME: {
|
|
300 |
if (stackFrames == null) {
|
|
301 |
skipBytes(length);
|
|
302 |
} else {
|
|
303 |
long id = readID();
|
|
304 |
String methodName = getNameFromID(readID());
|
|
305 |
String methodSig = getNameFromID(readID());
|
|
306 |
String sourceFile = getNameFromID(readID());
|
|
307 |
int classSer = in.readInt();
|
|
308 |
String className = classNameFromSerialNo.get(new Integer(classSer));
|
|
309 |
int lineNumber = in.readInt();
|
|
310 |
if (lineNumber < StackFrame.LINE_NUMBER_NATIVE) {
|
|
311 |
warn("Weird stack frame line number: " + lineNumber);
|
|
312 |
lineNumber = StackFrame.LINE_NUMBER_UNKNOWN;
|
|
313 |
}
|
|
314 |
stackFrames.put(new Long(id),
|
|
315 |
new StackFrame(methodName, methodSig,
|
|
316 |
className, sourceFile,
|
|
317 |
lineNumber));
|
|
318 |
}
|
|
319 |
break;
|
|
320 |
}
|
|
321 |
case HPROF_TRACE: {
|
|
322 |
if (stackTraces == null) {
|
|
323 |
skipBytes(length);
|
|
324 |
} else {
|
|
325 |
int serialNo = in.readInt();
|
|
326 |
int threadSeq = in.readInt(); // Not used
|
|
327 |
StackFrame[] frames = new StackFrame[in.readInt()];
|
|
328 |
for (int i = 0; i < frames.length; i++) {
|
|
329 |
long fid = readID();
|
|
330 |
frames[i] = stackFrames.get(new Long(fid));
|
|
331 |
if (frames[i] == null) {
|
|
332 |
throw new IOException("Stack frame " + toHex(fid) + " not found");
|
|
333 |
}
|
|
334 |
}
|
|
335 |
stackTraces.put(new Integer(serialNo),
|
|
336 |
new StackTrace(frames));
|
|
337 |
}
|
|
338 |
break;
|
|
339 |
}
|
|
340 |
case HPROF_UNLOAD_CLASS:
|
|
341 |
case HPROF_ALLOC_SITES:
|
|
342 |
case HPROF_START_THREAD:
|
|
343 |
case HPROF_END_THREAD:
|
|
344 |
case HPROF_HEAP_SUMMARY:
|
|
345 |
case HPROF_CPU_SAMPLES:
|
|
346 |
case HPROF_CONTROL_SETTINGS:
|
|
347 |
case HPROF_LOCKSTATS_WAIT_TIME:
|
|
348 |
case HPROF_LOCKSTATS_HOLD_TIME:
|
|
349 |
{
|
|
350 |
// Ignore these record types
|
|
351 |
skipBytes(length);
|
|
352 |
break;
|
|
353 |
}
|
|
354 |
default: {
|
|
355 |
skipBytes(length);
|
|
356 |
warn("Ignoring unrecognized record type " + type);
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
return snapshot;
|
|
362 |
}
|
|
363 |
|
|
364 |
private void skipBytes(int length) throws IOException {
|
|
365 |
in.skipBytes(length);
|
|
366 |
}
|
|
367 |
|
|
368 |
private int readVersionHeader() throws IOException {
|
|
369 |
int candidatesLeft = VERSIONS.length;
|
|
370 |
boolean[] matched = new boolean[VERSIONS.length];
|
|
371 |
for (int i = 0; i < candidatesLeft; i++) {
|
|
372 |
matched[i] = true;
|
|
373 |
}
|
|
374 |
|
|
375 |
int pos = 0;
|
|
376 |
while (candidatesLeft > 0) {
|
|
377 |
char c = (char) in.readByte();
|
|
378 |
currPos++;
|
|
379 |
for (int i = 0; i < VERSIONS.length; i++) {
|
|
380 |
if (matched[i]) {
|
|
381 |
if (c != VERSIONS[i].charAt(pos)) { // Not matched
|
|
382 |
matched[i] = false;
|
|
383 |
--candidatesLeft;
|
|
384 |
} else if (pos == VERSIONS[i].length() - 1) { // Full match
|
|
385 |
return i;
|
|
386 |
}
|
|
387 |
}
|
|
388 |
}
|
|
389 |
++pos;
|
|
390 |
}
|
|
391 |
throw new IOException("Version string not recognized at byte " + (pos+3));
|
|
392 |
}
|
|
393 |
|
|
394 |
private void readHeapDump(int bytesLeft, int posAtEnd) throws IOException {
|
|
395 |
while (bytesLeft > 0) {
|
|
396 |
int type = in.readUnsignedByte();
|
|
397 |
if (debugLevel > 0) {
|
|
398 |
System.out.println(" Read heap sub-record type " + type
|
|
399 |
+ " at position "
|
|
400 |
+ toHex(posAtEnd - bytesLeft));
|
|
401 |
}
|
|
402 |
bytesLeft--;
|
|
403 |
switch(type) {
|
|
404 |
case HPROF_GC_ROOT_UNKNOWN: {
|
|
405 |
long id = readID();
|
|
406 |
bytesLeft -= identifierSize;
|
|
407 |
snapshot.addRoot(new Root(id, 0, Root.UNKNOWN, ""));
|
|
408 |
break;
|
|
409 |
}
|
|
410 |
case HPROF_GC_ROOT_THREAD_OBJ: {
|
|
411 |
long id = readID();
|
|
412 |
int threadSeq = in.readInt();
|
|
413 |
int stackSeq = in.readInt();
|
|
414 |
bytesLeft -= identifierSize + 8;
|
|
415 |
threadObjects.put(new Integer(threadSeq),
|
|
416 |
new ThreadObject(id, stackSeq));
|
|
417 |
break;
|
|
418 |
}
|
|
419 |
case HPROF_GC_ROOT_JNI_GLOBAL: {
|
|
420 |
long id = readID();
|
|
421 |
long globalRefId = readID(); // Ignored, for now
|
|
422 |
bytesLeft -= 2*identifierSize;
|
|
423 |
snapshot.addRoot(new Root(id, 0, Root.NATIVE_STATIC, ""));
|
|
424 |
break;
|
|
425 |
}
|
|
426 |
case HPROF_GC_ROOT_JNI_LOCAL: {
|
|
427 |
long id = readID();
|
|
428 |
int threadSeq = in.readInt();
|
|
429 |
int depth = in.readInt();
|
|
430 |
bytesLeft -= identifierSize + 8;
|
|
431 |
ThreadObject to = getThreadObjectFromSequence(threadSeq);
|
|
432 |
StackTrace st = getStackTraceFromSerial(to.stackSeq);
|
|
433 |
if (st != null) {
|
|
434 |
st = st.traceForDepth(depth+1);
|
|
435 |
}
|
|
436 |
snapshot.addRoot(new Root(id, to.threadId,
|
|
437 |
Root.NATIVE_LOCAL, "", st));
|
|
438 |
break;
|
|
439 |
}
|
|
440 |
case HPROF_GC_ROOT_JAVA_FRAME: {
|
|
441 |
long id = readID();
|
|
442 |
int threadSeq = in.readInt();
|
|
443 |
int depth = in.readInt();
|
|
444 |
bytesLeft -= identifierSize + 8;
|
|
445 |
ThreadObject to = getThreadObjectFromSequence(threadSeq);
|
|
446 |
StackTrace st = getStackTraceFromSerial(to.stackSeq);
|
|
447 |
if (st != null) {
|
|
448 |
st = st.traceForDepth(depth+1);
|
|
449 |
}
|
|
450 |
snapshot.addRoot(new Root(id, to.threadId,
|
|
451 |
Root.JAVA_LOCAL, "", st));
|
|
452 |
break;
|
|
453 |
}
|
|
454 |
case HPROF_GC_ROOT_NATIVE_STACK: {
|
|
455 |
long id = readID();
|
|
456 |
int threadSeq = in.readInt();
|
|
457 |
bytesLeft -= identifierSize + 4;
|
|
458 |
ThreadObject to = getThreadObjectFromSequence(threadSeq);
|
|
459 |
StackTrace st = getStackTraceFromSerial(to.stackSeq);
|
|
460 |
snapshot.addRoot(new Root(id, to.threadId,
|
|
461 |
Root.NATIVE_STACK, "", st));
|
|
462 |
break;
|
|
463 |
}
|
|
464 |
case HPROF_GC_ROOT_STICKY_CLASS: {
|
|
465 |
long id = readID();
|
|
466 |
bytesLeft -= identifierSize;
|
|
467 |
snapshot.addRoot(new Root(id, 0, Root.SYSTEM_CLASS, ""));
|
|
468 |
break;
|
|
469 |
}
|
|
470 |
case HPROF_GC_ROOT_THREAD_BLOCK: {
|
|
471 |
long id = readID();
|
|
472 |
int threadSeq = in.readInt();
|
|
473 |
bytesLeft -= identifierSize + 4;
|
|
474 |
ThreadObject to = getThreadObjectFromSequence(threadSeq);
|
|
475 |
StackTrace st = getStackTraceFromSerial(to.stackSeq);
|
|
476 |
snapshot.addRoot(new Root(id, to.threadId,
|
|
477 |
Root.THREAD_BLOCK, "", st));
|
|
478 |
break;
|
|
479 |
}
|
|
480 |
case HPROF_GC_ROOT_MONITOR_USED: {
|
|
481 |
long id = readID();
|
|
482 |
bytesLeft -= identifierSize;
|
|
483 |
snapshot.addRoot(new Root(id, 0, Root.BUSY_MONITOR, ""));
|
|
484 |
break;
|
|
485 |
}
|
|
486 |
case HPROF_GC_CLASS_DUMP: {
|
|
487 |
int bytesRead = readClass();
|
|
488 |
bytesLeft -= bytesRead;
|
|
489 |
break;
|
|
490 |
}
|
|
491 |
case HPROF_GC_INSTANCE_DUMP: {
|
|
492 |
int bytesRead = readInstance();
|
|
493 |
bytesLeft -= bytesRead;
|
|
494 |
break;
|
|
495 |
}
|
|
496 |
case HPROF_GC_OBJ_ARRAY_DUMP: {
|
|
497 |
int bytesRead = readArray(false);
|
|
498 |
bytesLeft -= bytesRead;
|
|
499 |
break;
|
|
500 |
}
|
|
501 |
case HPROF_GC_PRIM_ARRAY_DUMP: {
|
|
502 |
int bytesRead = readArray(true);
|
|
503 |
bytesLeft -= bytesRead;
|
|
504 |
break;
|
|
505 |
}
|
|
506 |
default: {
|
|
507 |
throw new IOException("Unrecognized heap dump sub-record type: " + type);
|
|
508 |
}
|
|
509 |
}
|
|
510 |
}
|
|
511 |
if (bytesLeft != 0) {
|
|
512 |
warn("Error reading heap dump or heap dump segment: Byte count is " + bytesLeft + " instead of 0");
|
|
513 |
skipBytes(bytesLeft);
|
|
514 |
}
|
|
515 |
if (debugLevel > 0) {
|
|
516 |
System.out.println(" Finished heap sub-records.");
|
|
517 |
}
|
|
518 |
}
|
|
519 |
|
|
520 |
private long readID() throws IOException {
|
|
521 |
return (identifierSize == 4)?
|
|
522 |
(Snapshot.SMALL_ID_MASK & (long)in.readInt()) : in.readLong();
|
|
523 |
}
|
|
524 |
|
|
525 |
//
|
|
526 |
// Read a java value. If result is non-null, it's expected to be an
|
|
527 |
// array of one element. We use it to fake multiple return values.
|
|
528 |
// @returns the number of bytes read
|
|
529 |
//
|
|
530 |
private int readValue(JavaThing[] resultArr) throws IOException {
|
|
531 |
byte type = in.readByte();
|
|
532 |
return 1 + readValueForType(type, resultArr);
|
|
533 |
}
|
|
534 |
|
|
535 |
private int readValueForType(byte type, JavaThing[] resultArr)
|
|
536 |
throws IOException {
|
|
537 |
if (version >= VERSION_JDK12BETA4) {
|
|
538 |
type = signatureFromTypeId(type);
|
|
539 |
}
|
|
540 |
return readValueForTypeSignature(type, resultArr);
|
|
541 |
}
|
|
542 |
|
|
543 |
private int readValueForTypeSignature(byte type, JavaThing[] resultArr)
|
|
544 |
throws IOException {
|
|
545 |
switch (type) {
|
|
546 |
case '[':
|
|
547 |
case 'L': {
|
|
548 |
long id = readID();
|
|
549 |
if (resultArr != null) {
|
|
550 |
resultArr[0] = new JavaObjectRef(id);
|
|
551 |
}
|
|
552 |
return identifierSize;
|
|
553 |
}
|
|
554 |
case 'Z': {
|
|
555 |
int b = in.readByte();
|
|
556 |
if (b != 0 && b != 1) {
|
|
557 |
warn("Illegal boolean value read");
|
|
558 |
}
|
|
559 |
if (resultArr != null) {
|
|
560 |
resultArr[0] = new JavaBoolean(b != 0);
|
|
561 |
}
|
|
562 |
return 1;
|
|
563 |
}
|
|
564 |
case 'B': {
|
|
565 |
byte b = in.readByte();
|
|
566 |
if (resultArr != null) {
|
|
567 |
resultArr[0] = new JavaByte(b);
|
|
568 |
}
|
|
569 |
return 1;
|
|
570 |
}
|
|
571 |
case 'S': {
|
|
572 |
short s = in.readShort();
|
|
573 |
if (resultArr != null) {
|
|
574 |
resultArr[0] = new JavaShort(s);
|
|
575 |
}
|
|
576 |
return 2;
|
|
577 |
}
|
|
578 |
case 'C': {
|
|
579 |
char ch = in.readChar();
|
|
580 |
if (resultArr != null) {
|
|
581 |
resultArr[0] = new JavaChar(ch);
|
|
582 |
}
|
|
583 |
return 2;
|
|
584 |
}
|
|
585 |
case 'I': {
|
|
586 |
int val = in.readInt();
|
|
587 |
if (resultArr != null) {
|
|
588 |
resultArr[0] = new JavaInt(val);
|
|
589 |
}
|
|
590 |
return 4;
|
|
591 |
}
|
|
592 |
case 'J': {
|
|
593 |
long val = in.readLong();
|
|
594 |
if (resultArr != null) {
|
|
595 |
resultArr[0] = new JavaLong(val);
|
|
596 |
}
|
|
597 |
return 8;
|
|
598 |
}
|
|
599 |
case 'F': {
|
|
600 |
float val = in.readFloat();
|
|
601 |
if (resultArr != null) {
|
|
602 |
resultArr[0] = new JavaFloat(val);
|
|
603 |
}
|
|
604 |
return 4;
|
|
605 |
}
|
|
606 |
case 'D': {
|
|
607 |
double val = in.readDouble();
|
|
608 |
if (resultArr != null) {
|
|
609 |
resultArr[0] = new JavaDouble(val);
|
|
610 |
}
|
|
611 |
return 8;
|
|
612 |
}
|
|
613 |
default: {
|
|
614 |
throw new IOException("Bad value signature: " + type);
|
|
615 |
}
|
|
616 |
}
|
|
617 |
}
|
|
618 |
|
|
619 |
private ThreadObject getThreadObjectFromSequence(int threadSeq)
|
|
620 |
throws IOException {
|
|
621 |
ThreadObject to = threadObjects.get(new Integer(threadSeq));
|
|
622 |
if (to == null) {
|
|
623 |
throw new IOException("Thread " + threadSeq +
|
|
624 |
" not found for JNI local ref");
|
|
625 |
}
|
|
626 |
return to;
|
|
627 |
}
|
|
628 |
|
|
629 |
private String getNameFromID(long id) throws IOException {
|
|
630 |
return getNameFromID(new Long(id));
|
|
631 |
}
|
|
632 |
|
|
633 |
private String getNameFromID(Long id) throws IOException {
|
|
634 |
if (id.longValue() == 0L) {
|
|
635 |
return "";
|
|
636 |
}
|
|
637 |
String result = names.get(id);
|
|
638 |
if (result == null) {
|
|
639 |
warn("Name not found at " + toHex(id.longValue()));
|
|
640 |
return "unresolved name " + toHex(id.longValue());
|
|
641 |
}
|
|
642 |
return result;
|
|
643 |
}
|
|
644 |
|
|
645 |
private StackTrace getStackTraceFromSerial(int ser) throws IOException {
|
|
646 |
if (stackTraces == null) {
|
|
647 |
return null;
|
|
648 |
}
|
|
649 |
StackTrace result = stackTraces.get(new Integer(ser));
|
|
650 |
if (result == null) {
|
|
651 |
warn("Stack trace not found for serial # " + ser);
|
|
652 |
}
|
|
653 |
return result;
|
|
654 |
}
|
|
655 |
|
|
656 |
//
|
|
657 |
// Handle a HPROF_GC_CLASS_DUMP
|
|
658 |
// Return number of bytes read
|
|
659 |
//
|
|
660 |
private int readClass() throws IOException {
|
|
661 |
long id = readID();
|
|
662 |
StackTrace stackTrace = getStackTraceFromSerial(in.readInt());
|
|
663 |
long superId = readID();
|
|
664 |
long classLoaderId = readID();
|
|
665 |
long signersId = readID();
|
|
666 |
long protDomainId = readID();
|
|
667 |
long reserved1 = readID();
|
|
668 |
long reserved2 = readID();
|
|
669 |
int instanceSize = in.readInt();
|
|
670 |
int bytesRead = 7 * identifierSize + 8;
|
|
671 |
|
|
672 |
int numConstPoolEntries = in.readUnsignedShort();
|
|
673 |
bytesRead += 2;
|
|
674 |
for (int i = 0; i < numConstPoolEntries; i++) {
|
|
675 |
int index = in.readUnsignedShort(); // unused
|
|
676 |
bytesRead += 2;
|
|
677 |
bytesRead += readValue(null); // We ignore the values
|
|
678 |
}
|
|
679 |
|
|
680 |
int numStatics = in.readUnsignedShort();
|
|
681 |
bytesRead += 2;
|
|
682 |
JavaThing[] valueBin = new JavaThing[1];
|
|
683 |
JavaStatic[] statics = new JavaStatic[numStatics];
|
|
684 |
for (int i = 0; i < numStatics; i++) {
|
|
685 |
long nameId = readID();
|
|
686 |
bytesRead += identifierSize;
|
|
687 |
byte type = in.readByte();
|
|
688 |
bytesRead++;
|
|
689 |
bytesRead += readValueForType(type, valueBin);
|
|
690 |
String fieldName = getNameFromID(nameId);
|
|
691 |
if (version >= VERSION_JDK12BETA4) {
|
|
692 |
type = signatureFromTypeId(type);
|
|
693 |
}
|
|
694 |
String signature = "" + ((char) type);
|
|
695 |
JavaField f = new JavaField(fieldName, signature);
|
|
696 |
statics[i] = new JavaStatic(f, valueBin[0]);
|
|
697 |
}
|
|
698 |
|
|
699 |
int numFields = in.readUnsignedShort();
|
|
700 |
bytesRead += 2;
|
|
701 |
JavaField[] fields = new JavaField[numFields];
|
|
702 |
for (int i = 0; i < numFields; i++) {
|
|
703 |
long nameId = readID();
|
|
704 |
bytesRead += identifierSize;
|
|
705 |
byte type = in.readByte();
|
|
706 |
bytesRead++;
|
|
707 |
String fieldName = getNameFromID(nameId);
|
|
708 |
if (version >= VERSION_JDK12BETA4) {
|
|
709 |
type = signatureFromTypeId(type);
|
|
710 |
}
|
|
711 |
String signature = "" + ((char) type);
|
|
712 |
fields[i] = new JavaField(fieldName, signature);
|
|
713 |
}
|
|
714 |
String name = classNameFromObjectID.get(new Long(id));
|
|
715 |
if (name == null) {
|
|
716 |
warn("Class name not found for " + toHex(id));
|
|
717 |
name = "unknown-name@" + toHex(id);
|
|
718 |
}
|
|
719 |
JavaClass c = new JavaClass(id, name, superId, classLoaderId, signersId,
|
|
720 |
protDomainId, fields, statics,
|
|
721 |
instanceSize);
|
|
722 |
snapshot.addClass(id, c);
|
|
723 |
snapshot.setSiteTrace(c, stackTrace);
|
|
724 |
|
|
725 |
return bytesRead;
|
|
726 |
}
|
|
727 |
|
|
728 |
private String toHex(long addr) {
|
|
729 |
return com.sun.tools.hat.internal.util.Misc.toHex(addr);
|
|
730 |
}
|
|
731 |
|
|
732 |
//
|
|
733 |
// Handle a HPROF_GC_INSTANCE_DUMP
|
|
734 |
// Return number of bytes read
|
|
735 |
//
|
|
736 |
private int readInstance() throws IOException {
|
|
737 |
long start = in.position();
|
|
738 |
long id = readID();
|
|
739 |
StackTrace stackTrace = getStackTraceFromSerial(in.readInt());
|
|
740 |
long classID = readID();
|
|
741 |
int bytesFollowing = in.readInt();
|
|
742 |
int bytesRead = (2 * identifierSize) + 8 + bytesFollowing;
|
|
743 |
JavaObject jobj = new JavaObject(classID, start);
|
|
744 |
skipBytes(bytesFollowing);
|
|
745 |
snapshot.addHeapObject(id, jobj);
|
|
746 |
snapshot.setSiteTrace(jobj, stackTrace);
|
|
747 |
return bytesRead;
|
|
748 |
}
|
|
749 |
|
|
750 |
//
|
|
751 |
// Handle a HPROF_GC_OBJ_ARRAY_DUMP or HPROF_GC_PRIM_ARRAY_DUMP
|
|
752 |
// Return number of bytes read
|
|
753 |
//
|
|
754 |
private int readArray(boolean isPrimitive) throws IOException {
|
|
755 |
long start = in.position();
|
|
756 |
long id = readID();
|
|
757 |
StackTrace stackTrace = getStackTraceFromSerial(in.readInt());
|
|
758 |
int num = in.readInt();
|
|
759 |
int bytesRead = identifierSize + 8;
|
|
760 |
long elementClassID;
|
|
761 |
if (isPrimitive) {
|
|
762 |
elementClassID = in.readByte();
|
|
763 |
bytesRead++;
|
|
764 |
} else {
|
|
765 |
elementClassID = readID();
|
|
766 |
bytesRead += identifierSize;
|
|
767 |
}
|
|
768 |
|
|
769 |
// Check for primitive arrays:
|
|
770 |
byte primitiveSignature = 0x00;
|
|
771 |
int elSize = 0;
|
|
772 |
if (isPrimitive || version < VERSION_JDK12BETA4) {
|
|
773 |
switch ((int)elementClassID) {
|
|
774 |
case T_BOOLEAN: {
|
|
775 |
primitiveSignature = (byte) 'Z';
|
|
776 |
elSize = 1;
|
|
777 |
break;
|
|
778 |
}
|
|
779 |
case T_CHAR: {
|
|
780 |
primitiveSignature = (byte) 'C';
|
|
781 |
elSize = 2;
|
|
782 |
break;
|
|
783 |
}
|
|
784 |
case T_FLOAT: {
|
|
785 |
primitiveSignature = (byte) 'F';
|
|
786 |
elSize = 4;
|
|
787 |
break;
|
|
788 |
}
|
|
789 |
case T_DOUBLE: {
|
|
790 |
primitiveSignature = (byte) 'D';
|
|
791 |
elSize = 8;
|
|
792 |
break;
|
|
793 |
}
|
|
794 |
case T_BYTE: {
|
|
795 |
primitiveSignature = (byte) 'B';
|
|
796 |
elSize = 1;
|
|
797 |
break;
|
|
798 |
}
|
|
799 |
case T_SHORT: {
|
|
800 |
primitiveSignature = (byte) 'S';
|
|
801 |
elSize = 2;
|
|
802 |
break;
|
|
803 |
}
|
|
804 |
case T_INT: {
|
|
805 |
primitiveSignature = (byte) 'I';
|
|
806 |
elSize = 4;
|
|
807 |
break;
|
|
808 |
}
|
|
809 |
case T_LONG: {
|
|
810 |
primitiveSignature = (byte) 'J';
|
|
811 |
elSize = 8;
|
|
812 |
break;
|
|
813 |
}
|
|
814 |
}
|
|
815 |
if (version >= VERSION_JDK12BETA4 && primitiveSignature == 0x00) {
|
|
816 |
throw new IOException("Unrecognized typecode: "
|
|
817 |
+ elementClassID);
|
|
818 |
}
|
|
819 |
}
|
|
820 |
if (primitiveSignature != 0x00) {
|
|
821 |
int size = elSize * num;
|
|
822 |
bytesRead += size;
|
|
823 |
JavaValueArray va = new JavaValueArray(primitiveSignature, start);
|
|
824 |
skipBytes(size);
|
|
825 |
snapshot.addHeapObject(id, va);
|
|
826 |
snapshot.setSiteTrace(va, stackTrace);
|
|
827 |
} else {
|
|
828 |
int sz = num * identifierSize;
|
|
829 |
bytesRead += sz;
|
|
830 |
JavaObjectArray arr = new JavaObjectArray(elementClassID, start);
|
|
831 |
skipBytes(sz);
|
|
832 |
snapshot.addHeapObject(id, arr);
|
|
833 |
snapshot.setSiteTrace(arr, stackTrace);
|
|
834 |
}
|
|
835 |
return bytesRead;
|
|
836 |
}
|
|
837 |
|
|
838 |
private byte signatureFromTypeId(byte typeId) throws IOException {
|
|
839 |
switch (typeId) {
|
|
840 |
case T_CLASS: {
|
|
841 |
return (byte) 'L';
|
|
842 |
}
|
|
843 |
case T_BOOLEAN: {
|
|
844 |
return (byte) 'Z';
|
|
845 |
}
|
|
846 |
case T_CHAR: {
|
|
847 |
return (byte) 'C';
|
|
848 |
}
|
|
849 |
case T_FLOAT: {
|
|
850 |
return (byte) 'F';
|
|
851 |
}
|
|
852 |
case T_DOUBLE: {
|
|
853 |
return (byte) 'D';
|
|
854 |
}
|
|
855 |
case T_BYTE: {
|
|
856 |
return (byte) 'B';
|
|
857 |
}
|
|
858 |
case T_SHORT: {
|
|
859 |
return (byte) 'S';
|
|
860 |
}
|
|
861 |
case T_INT: {
|
|
862 |
return (byte) 'I';
|
|
863 |
}
|
|
864 |
case T_LONG: {
|
|
865 |
return (byte) 'J';
|
|
866 |
}
|
|
867 |
default: {
|
|
868 |
throw new IOException("Invalid type id of " + typeId);
|
|
869 |
}
|
|
870 |
}
|
|
871 |
}
|
|
872 |
|
|
873 |
private void handleEOF(EOFException exp, Snapshot snapshot) {
|
|
874 |
if (debugLevel > 0) {
|
|
875 |
exp.printStackTrace();
|
|
876 |
}
|
|
877 |
warn("Unexpected EOF. Will miss information...");
|
|
878 |
// we have EOF, we have to tolerate missing references
|
|
879 |
snapshot.setUnresolvedObjectsOK(true);
|
|
880 |
}
|
|
881 |
|
|
882 |
private void warn(String msg) {
|
|
883 |
System.out.println("WARNING: " + msg);
|
|
884 |
}
|
|
885 |
|
|
886 |
//
|
|
887 |
// A trivial data-holder class for HPROF_GC_ROOT_THREAD_OBJ.
|
|
888 |
//
|
|
889 |
private class ThreadObject {
|
|
890 |
|
|
891 |
long threadId;
|
|
892 |
int stackSeq;
|
|
893 |
|
|
894 |
ThreadObject(long threadId, int stackSeq) {
|
|
895 |
this.threadId = threadId;
|
|
896 |
this.stackSeq = stackSeq;
|
|
897 |
}
|
|
898 |
}
|
|
899 |
|
|
900 |
}
|