1 /*
   2  * Copyright (c) 2004, 2018, Oracle and/or its affiliates. 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.utilities;
  26 
  27 import java.io.*;
  28 import java.nio.channels.*;
  29 import java.util.*;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.memory.*;
  32 import sun.jvm.hotspot.oops.*;
  33 import sun.jvm.hotspot.runtime.*;
  34 import sun.jvm.hotspot.classfile.*;
  35 import sun.jvm.hotspot.gc.z.ZCollectedHeap;
  36 
  37 /*
  38  * This class writes Java heap in hprof binary format. This format is
  39  * used by Heap Analysis Tool (HAT). The class is heavily influenced
  40  * by 'hprof_io.c' of 1.5 new hprof implementation.
  41  */
  42 
  43 /* hprof binary format: (result either written to a file or sent over
  44  * the network).
  45  *
  46  * WARNING: This format is still under development, and is subject to
  47  * change without notice.
  48  *
  49  * header     "JAVA PROFILE 1.0.2" (0-terminated)
  50  * u4         size of identifiers. Identifiers are used to represent
  51  *            UTF8 strings, objects, stack traces, etc. They usually
  52  *            have the same size as host pointers. For example, on
  53  *            Solaris and Win32, the size is 4.
  54  * u4         high word
  55  * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
  56  * [record]*  a sequence of records.
  57  *
  58  */
  59 
  60 /*
  61  *
  62  * Record format:
  63  *
  64  * u1         a TAG denoting the type of the record
  65  * u4         number of *microseconds* since the time stamp in the
  66  *            header. (wraps around in a little more than an hour)
  67  * u4         number of bytes *remaining* in the record. Note that
  68  *            this number excludes the tag and the length field itself.
  69  * [u1]*      BODY of the record (a sequence of bytes)
  70  */
  71 
  72 /*
  73  * The following TAGs are supported:
  74  *
  75  * TAG           BODY       notes
  76  *----------------------------------------------------------
  77  * HPROF_UTF8               a UTF8-encoded name
  78  *
  79  *               id         name ID
  80  *               [u1]*      UTF8 characters (no trailing zero)
  81  *
  82  * HPROF_LOAD_CLASS         a newly loaded class
  83  *
  84  *                u4        class serial number (> 0)
  85  *                id        class object ID
  86  *                u4        stack trace serial number
  87  *                id        class name ID
  88  *
  89  * HPROF_UNLOAD_CLASS       an unloading class
  90  *
  91  *                u4        class serial_number
  92  *
  93  * HPROF_FRAME              a Java stack frame
  94  *
  95  *                id        stack frame ID
  96  *                id        method name ID
  97  *                id        method signature ID
  98  *                id        source file name ID
  99  *                u4        class serial number
 100  *                i4        line number. >0: normal
 101  *                                       -1: unknown
 102  *                                       -2: compiled method
 103  *                                       -3: native method
 104  *
 105  * HPROF_TRACE              a Java stack trace
 106  *
 107  *               u4         stack trace serial number
 108  *               u4         thread serial number
 109  *               u4         number of frames
 110  *               [id]*      stack frame IDs
 111  *
 112  *
 113  * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
 114  *
 115  *               u2         flags 0x0001: incremental vs. complete
 116  *                                0x0002: sorted by allocation vs. live
 117  *                                0x0004: whether to force a GC
 118  *               u4         cutoff ratio
 119  *               u4         total live bytes
 120  *               u4         total live instances
 121  *               u8         total bytes allocated
 122  *               u8         total instances allocated
 123  *               u4         number of sites that follow
 124  *               [u1        is_array: 0:  normal object
 125  *                                    2:  object array
 126  *                                    4:  boolean array
 127  *                                    5:  char array
 128  *                                    6:  float array
 129  *                                    7:  double array
 130  *                                    8:  byte array
 131  *                                    9:  short array
 132  *                                    10: int array
 133  *                                    11: long array
 134  *                u4        class serial number (may be zero during startup)
 135  *                u4        stack trace serial number
 136  *                u4        number of bytes alive
 137  *                u4        number of instances alive
 138  *                u4        number of bytes allocated
 139  *                u4]*      number of instance allocated
 140  *
 141  * HPROF_START_THREAD       a newly started thread.
 142  *
 143  *               u4         thread serial number (> 0)
 144  *               id         thread object ID
 145  *               u4         stack trace serial number
 146  *               id         thread name ID
 147  *               id         thread group name ID
 148  *               id         thread group parent name ID
 149  *
 150  * HPROF_END_THREAD         a terminating thread.
 151  *
 152  *               u4         thread serial number
 153  *
 154  * HPROF_HEAP_SUMMARY       heap summary
 155  *
 156  *               u4         total live bytes
 157  *               u4         total live instances
 158  *               u8         total bytes allocated
 159  *               u8         total instances allocated
 160  *
 161  * HPROF_HEAP_DUMP          denote a heap dump
 162  *
 163  *               [heap dump sub-records]*
 164  *
 165  *                          There are four kinds of heap dump sub-records:
 166  *
 167  *               u1         sub-record type
 168  *
 169  *               HPROF_GC_ROOT_UNKNOWN         unknown root
 170  *
 171  *                          id         object ID
 172  *
 173  *               HPROF_GC_ROOT_THREAD_OBJ      thread object
 174  *
 175  *                          id         thread object ID  (may be 0 for a
 176  *                                     thread newly attached through JNI)
 177  *                          u4         thread sequence number
 178  *                          u4         stack trace sequence number
 179  *
 180  *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
 181  *
 182  *                          id         object ID
 183  *                          id         JNI global ref ID
 184  *
 185  *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
 186  *
 187  *                          id         object ID
 188  *                          u4         thread serial number
 189  *                          u4         frame # in stack trace (-1 for empty)
 190  *
 191  *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
 192  *
 193  *                          id         object ID
 194  *                          u4         thread serial number
 195  *                          u4         frame # in stack trace (-1 for empty)
 196  *
 197  *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
 198  *
 199  *                          id         object ID
 200  *                          u4         thread serial number
 201  *
 202  *               HPROF_GC_ROOT_STICKY_CLASS    System class
 203  *
 204  *                          id         object ID
 205  *
 206  *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
 207  *
 208  *                          id         object ID
 209  *                          u4         thread serial number
 210  *
 211  *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
 212  *
 213  *                          id         object ID
 214  *
 215  *               HPROF_GC_CLASS_DUMP           dump of a class object
 216  *
 217  *                          id         class object ID
 218  *                          u4         stack trace serial number
 219  *                          id         super class object ID
 220  *                          id         class loader object ID
 221  *                          id         signers object ID
 222  *                          id         protection domain object ID
 223  *                          id         reserved
 224  *                          id         reserved
 225  *
 226  *                          u4         instance size (in bytes)
 227  *
 228  *                          u2         size of constant pool
 229  *                          [u2,       constant pool index,
 230  *                           ty,       type
 231  *                                     2:  object
 232  *                                     4:  boolean
 233  *                                     5:  char
 234  *                                     6:  float
 235  *                                     7:  double
 236  *                                     8:  byte
 237  *                                     9:  short
 238  *                                     10: int
 239  *                                     11: long
 240  *                           vl]*      and value
 241  *
 242  *                          u2         number of static fields
 243  *                          [id,       static field name,
 244  *                           ty,       type,
 245  *                           vl]*      and value
 246  *
 247  *                          u2         number of inst. fields (not inc. super)
 248  *                          [id,       instance field name,
 249  *                           ty]*      type
 250  *
 251  *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
 252  *
 253  *                          id         object ID
 254  *                          u4         stack trace serial number
 255  *                          id         class object ID
 256  *                          u4         number of bytes that follow
 257  *                          [vl]*      instance field values (class, followed
 258  *                                     by super, super's super ...)
 259  *
 260  *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
 261  *
 262  *                          id         array object ID
 263  *                          u4         stack trace serial number
 264  *                          u4         number of elements
 265  *                          id         array class ID
 266  *                          [id]*      elements
 267  *
 268  *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
 269  *
 270  *                          id         array object ID
 271  *                          u4         stack trace serial number
 272  *                          u4         number of elements
 273  *                          u1         element type
 274  *                                     4:  boolean array
 275  *                                     5:  char array
 276  *                                     6:  float array
 277  *                                     7:  double array
 278  *                                     8:  byte array
 279  *                                     9:  short array
 280  *                                     10: int array
 281  *                                     11: long array
 282  *                          [u1]*      elements
 283  *
 284  * HPROF_CPU_SAMPLES        a set of sample traces of running threads
 285  *
 286  *                u4        total number of samples
 287  *                u4        # of traces
 288  *               [u4        # of samples
 289  *                u4]*      stack trace serial number
 290  *
 291  * HPROF_CONTROL_SETTINGS   the settings of on/off switches
 292  *
 293  *                u4        0x00000001: alloc traces on/off
 294  *                          0x00000002: cpu sampling on/off
 295  *                u2        stack trace depth
 296  *
 297  *
 298  * A heap dump can optionally be generated as a sequence of heap dump
 299  * segments. This sequence is terminated by an end record. The additional
 300  * tags allowed by format "JAVA PROFILE 1.0.2" are:
 301  *
 302  * HPROF_HEAP_DUMP_SEGMENT  denote a heap dump segment
 303  *
 304  *               [heap dump sub-records]*
 305  *               The same sub-record types allowed by HPROF_HEAP_DUMP
 306  *
 307  * HPROF_HEAP_DUMP_END      denotes the end of a heap dump
 308  *
 309  */
 310 
 311 public class HeapHprofBinWriter extends AbstractHeapGraphWriter {
 312 
 313     // Record which Symbol names have been dumped already.
 314     private HashSet<Symbol> names;
 315 
 316     private static final long HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD = 2L * 0x40000000;
 317 
 318     // The approximate size of a heap segment. Used to calculate when to create
 319     // a new segment.
 320     private static final long HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE = 1L * 0x40000000;
 321 
 322     // hprof binary file header
 323     private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";
 324 
 325     // constants in enum HprofTag
 326     private static final int HPROF_UTF8             = 0x01;
 327     private static final int HPROF_LOAD_CLASS       = 0x02;
 328     private static final int HPROF_UNLOAD_CLASS     = 0x03;
 329     private static final int HPROF_FRAME            = 0x04;
 330     private static final int HPROF_TRACE            = 0x05;
 331     private static final int HPROF_ALLOC_SITES      = 0x06;
 332     private static final int HPROF_HEAP_SUMMARY     = 0x07;
 333     private static final int HPROF_START_THREAD     = 0x0A;
 334     private static final int HPROF_END_THREAD       = 0x0B;
 335     private static final int HPROF_HEAP_DUMP        = 0x0C;
 336     private static final int HPROF_CPU_SAMPLES      = 0x0D;
 337     private static final int HPROF_CONTROL_SETTINGS = 0x0E;
 338 
 339     // 1.0.2 record types
 340     private static final int HPROF_HEAP_DUMP_SEGMENT = 0x1C;
 341     private static final int HPROF_HEAP_DUMP_END     = 0x2C;
 342 
 343     // Heap dump constants
 344     // constants in enum HprofGcTag
 345     private static final int HPROF_GC_ROOT_UNKNOWN       = 0xFF;
 346     private static final int HPROF_GC_ROOT_JNI_GLOBAL    = 0x01;
 347     private static final int HPROF_GC_ROOT_JNI_LOCAL     = 0x02;
 348     private static final int HPROF_GC_ROOT_JAVA_FRAME    = 0x03;
 349     private static final int HPROF_GC_ROOT_NATIVE_STACK  = 0x04;
 350     private static final int HPROF_GC_ROOT_STICKY_CLASS  = 0x05;
 351     private static final int HPROF_GC_ROOT_THREAD_BLOCK  = 0x06;
 352     private static final int HPROF_GC_ROOT_MONITOR_USED  = 0x07;
 353     private static final int HPROF_GC_ROOT_THREAD_OBJ    = 0x08;
 354     private static final int HPROF_GC_CLASS_DUMP         = 0x20;
 355     private static final int HPROF_GC_INSTANCE_DUMP      = 0x21;
 356     private static final int HPROF_GC_OBJ_ARRAY_DUMP     = 0x22;
 357     private static final int HPROF_GC_PRIM_ARRAY_DUMP    = 0x23;
 358 
 359     // constants in enum HprofType
 360     private static final int HPROF_ARRAY_OBJECT  = 1;
 361     private static final int HPROF_NORMAL_OBJECT = 2;
 362     private static final int HPROF_BOOLEAN       = 4;
 363     private static final int HPROF_CHAR          = 5;
 364     private static final int HPROF_FLOAT         = 6;
 365     private static final int HPROF_DOUBLE        = 7;
 366     private static final int HPROF_BYTE          = 8;
 367     private static final int HPROF_SHORT         = 9;
 368     private static final int HPROF_INT           = 10;
 369     private static final int HPROF_LONG          = 11;
 370 
 371     // Java type codes
 372     private static final int JVM_SIGNATURE_BOOLEAN = 'Z';
 373     private static final int JVM_SIGNATURE_CHAR    = 'C';
 374     private static final int JVM_SIGNATURE_BYTE    = 'B';
 375     private static final int JVM_SIGNATURE_SHORT   = 'S';
 376     private static final int JVM_SIGNATURE_INT     = 'I';
 377     private static final int JVM_SIGNATURE_LONG    = 'J';
 378     private static final int JVM_SIGNATURE_FLOAT   = 'F';
 379     private static final int JVM_SIGNATURE_DOUBLE  = 'D';
 380     private static final int JVM_SIGNATURE_ARRAY   = '[';
 381     private static final int JVM_SIGNATURE_CLASS   = 'L';
 382 
 383     private static final long MAX_U4_VALUE = 0xFFFFFFFFL;
 384     int serialNum = 1;
 385 
 386     public HeapHprofBinWriter() {
 387         this.KlassMap = new ArrayList<Klass>();
 388         this.names = new HashSet<Symbol>();
 389     }
 390 
 391     public synchronized void write(String fileName) throws IOException {
 392         // open file stream and create buffered data output stream
 393         fos = new FileOutputStream(fileName);
 394         out = new DataOutputStream(new BufferedOutputStream(fos));
 395 
 396         VM vm = VM.getVM();
 397         if (vm.getUniverse().heap() instanceof ZCollectedHeap) {
 398             throw new RuntimeException("This operation is not supported with ZGC.");
 399         }
 400 
 401         dbg = vm.getDebugger();
 402         objectHeap = vm.getObjectHeap();
 403 
 404         OBJ_ID_SIZE = (int) vm.getOopSize();
 405 
 406         BOOLEAN_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BOOLEAN);
 407         BYTE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BYTE);
 408         CHAR_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_CHAR);
 409         SHORT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_SHORT);
 410         INT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_INT);
 411         LONG_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_LONG);
 412         FLOAT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_FLOAT);
 413         DOUBLE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_DOUBLE);
 414         OBJECT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_OBJECT);
 415 
 416         BOOLEAN_SIZE = objectHeap.getBooleanSize();
 417         BYTE_SIZE = objectHeap.getByteSize();
 418         CHAR_SIZE = objectHeap.getCharSize();
 419         SHORT_SIZE = objectHeap.getShortSize();
 420         INT_SIZE = objectHeap.getIntSize();
 421         LONG_SIZE = objectHeap.getLongSize();
 422         FLOAT_SIZE = objectHeap.getFloatSize();
 423         DOUBLE_SIZE = objectHeap.getDoubleSize();
 424 
 425         // Check weather we should dump the heap as segments
 426         useSegmentedHeapDump = vm.getUniverse().heap().used() > HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD;
 427 
 428         // hprof bin format header
 429         writeFileHeader();
 430 
 431         // dummy stack trace without any frames so that
 432         // HAT can be run without -stack false option
 433         writeDummyTrace();
 434 
 435         // hprof UTF-8 symbols section
 436         writeSymbols();
 437 
 438         // HPROF_LOAD_CLASS records for all classes
 439         writeClasses();
 440 
 441         // write HPROF_FRAME and HPROF_TRACE records
 442         dumpStackTraces();
 443 
 444         // write CLASS_DUMP records
 445         writeClassDumpRecords();
 446 
 447         // this will write heap data into the buffer stream
 448         super.write();
 449 
 450         // flush buffer stream.
 451         out.flush();
 452 
 453         // Fill in final length
 454         fillInHeapRecordLength();
 455 
 456         if (useSegmentedHeapDump) {
 457             // Write heap segment-end record
 458             out.writeByte((byte) HPROF_HEAP_DUMP_END);
 459             out.writeInt(0);
 460             out.writeInt(0);
 461         }
 462 
 463         // flush buffer stream and throw it.
 464         out.flush();
 465         out = null;
 466 
 467         // close the file stream
 468         fos.close();
 469     }
 470 
 471     @Override
 472     protected void writeHeapRecordPrologue() throws IOException {
 473         if (currentSegmentStart == 0) {
 474             // write heap data header, depending on heap size use segmented heap
 475             // format
 476             out.writeByte((byte) (useSegmentedHeapDump ? HPROF_HEAP_DUMP_SEGMENT
 477                     : HPROF_HEAP_DUMP));
 478             out.writeInt(0);
 479 
 480             // remember position of dump length, we will fixup
 481             // length later - hprof format requires length.
 482             out.flush();
 483             currentSegmentStart = fos.getChannel().position();
 484             // write dummy length of 0 and we'll fix it later.
 485             out.writeInt(0);
 486         }
 487     }
 488 
 489     @Override
 490     protected void writeHeapRecordEpilogue() throws IOException {
 491         if (useSegmentedHeapDump) {
 492             out.flush();
 493             if ((fos.getChannel().position() - currentSegmentStart - 4L) >= HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE) {
 494                 fillInHeapRecordLength();
 495                 currentSegmentStart = 0;
 496             }
 497         }
 498     }
 499 
 500     private void fillInHeapRecordLength() throws IOException {
 501 
 502         // now get the current position to calculate length
 503         long dumpEnd = fos.getChannel().position();
 504 
 505         // calculate the length of heap data
 506         long dumpLenLong = (dumpEnd - currentSegmentStart - 4L);
 507 
 508         // Check length boundary, overflow could happen but is _very_ unlikely
 509         if (dumpLenLong >= (4L * 0x40000000)) {
 510             throw new RuntimeException("Heap segment size overflow.");
 511         }
 512 
 513         // Save the current position
 514         long currentPosition = fos.getChannel().position();
 515 
 516         // seek the position to write length
 517         fos.getChannel().position(currentSegmentStart);
 518 
 519         int dumpLen = (int) dumpLenLong;
 520 
 521         // write length as integer
 522         fos.write((dumpLen >>> 24) & 0xFF);
 523         fos.write((dumpLen >>> 16) & 0xFF);
 524         fos.write((dumpLen >>> 8) & 0xFF);
 525         fos.write((dumpLen >>> 0) & 0xFF);
 526 
 527         //Reset to previous current position
 528         fos.getChannel().position(currentPosition);
 529     }
 530 
 531     // get the size in bytes for the requested type
 532     private long getSizeForType(int type) throws IOException {
 533         switch (type) {
 534             case TypeArrayKlass.T_BOOLEAN:
 535                 return BOOLEAN_SIZE;
 536             case TypeArrayKlass.T_INT:
 537                 return INT_SIZE;
 538             case TypeArrayKlass.T_CHAR:
 539                 return CHAR_SIZE;
 540             case TypeArrayKlass.T_SHORT:
 541                 return SHORT_SIZE;
 542             case TypeArrayKlass.T_BYTE:
 543                 return BYTE_SIZE;
 544             case TypeArrayKlass.T_LONG:
 545                 return LONG_SIZE;
 546             case TypeArrayKlass.T_FLOAT:
 547                 return FLOAT_SIZE;
 548             case TypeArrayKlass.T_DOUBLE:
 549                 return DOUBLE_SIZE;
 550             default:
 551                 throw new RuntimeException(
 552                     "Should not reach here: Unknown type: " + type);
 553          }
 554     }
 555 
 556     private int getArrayHeaderSize(boolean isObjectAarray) {
 557         return isObjectAarray?
 558             ((int) BYTE_SIZE + 2 * (int) INT_SIZE + 2 * (int) OBJ_ID_SIZE):
 559             (2 * (int) BYTE_SIZE + 2 * (int) INT_SIZE + (int) OBJ_ID_SIZE);
 560     }
 561 
 562     // Check if we need to truncate an array
 563     private int calculateArrayMaxLength(long originalArrayLength,
 564                                         int headerSize,
 565                                         long typeSize,
 566                                         String typeName) throws IOException {
 567 
 568         long length = originalArrayLength;
 569 
 570         // now get the current position to calculate length
 571         long dumpEnd = fos.getChannel().position();
 572         long originalLengthInBytes = originalArrayLength * typeSize;
 573 
 574         // calculate the length of heap data
 575         long currentRecordLength = (dumpEnd - currentSegmentStart - 4L);
 576         if (currentRecordLength > 0 &&
 577             (currentRecordLength + headerSize + originalLengthInBytes) > MAX_U4_VALUE) {
 578             fillInHeapRecordLength();
 579             currentSegmentStart = 0;
 580             writeHeapRecordPrologue();
 581             currentRecordLength = 0;
 582         }
 583 
 584         // Calculate the max bytes we can use.
 585         long maxBytes = (MAX_U4_VALUE - (headerSize + currentRecordLength));
 586 
 587         if (originalLengthInBytes > maxBytes) {
 588             length = maxBytes/typeSize;
 589             System.err.println("WARNING: Cannot dump array of type " + typeName
 590                                + " with length " + originalArrayLength
 591                                + "; truncating to length " + length);
 592         }
 593         return (int) length;
 594     }
 595 
 596     private void writeClassDumpRecords() throws IOException {
 597         ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
 598         try {
 599              cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
 600                             public void visit(Klass k) {
 601                                 try {
 602                                     writeHeapRecordPrologue();
 603                                     writeClassDumpRecord(k);
 604                                     writeHeapRecordEpilogue();
 605                                 } catch (IOException e) {
 606                                     throw new RuntimeException(e);
 607                                 }
 608                             }
 609                         });
 610         } catch (RuntimeException re) {
 611             handleRuntimeException(re);
 612         }
 613     }
 614 
 615     protected void writeClass(Instance instance) throws IOException {
 616         Klass reflectedKlass = java_lang_Class.asKlass(instance);
 617         // dump instance record only for primitive type Class objects.
 618         // all other Class objects are covered by writeClassDumpRecords.
 619         if (reflectedKlass == null) {
 620             writeInstance(instance);
 621         }
 622     }
 623 
 624     private void writeClassDumpRecord(Klass k) throws IOException {
 625         out.writeByte((byte)HPROF_GC_CLASS_DUMP);
 626         writeObjectID(k.getJavaMirror());
 627         out.writeInt(DUMMY_STACK_TRACE_ID);
 628         Klass superKlass = k.getJavaSuper();
 629         if (superKlass != null) {
 630             writeObjectID(superKlass.getJavaMirror());
 631         } else {
 632             writeObjectID(null);
 633         }
 634 
 635         if (k instanceof InstanceKlass) {
 636             InstanceKlass ik = (InstanceKlass) k;
 637             writeObjectID(ik.getClassLoader());
 638             writeObjectID(null);  // ik.getJavaMirror().getSigners());
 639             writeObjectID(null);  // ik.getJavaMirror().getProtectionDomain());
 640             // two reserved id fields
 641             writeObjectID(null);
 642             writeObjectID(null);
 643             List fields = getInstanceFields(ik);
 644             int instSize = getSizeForFields(fields);
 645             classDataCache.put(ik, new ClassData(instSize, fields));
 646             out.writeInt(instSize);
 647 
 648             // For now, ignore constant pool - HAT ignores too!
 649             // output number of cp entries as zero.
 650             out.writeShort((short) 0);
 651 
 652             List declaredFields = ik.getImmediateFields();
 653             List staticFields = new ArrayList();
 654             List instanceFields = new ArrayList();
 655             Iterator itr = null;
 656             for (itr = declaredFields.iterator(); itr.hasNext();) {
 657                 Field field = (Field) itr.next();
 658                 if (field.isStatic()) {
 659                     staticFields.add(field);
 660                 } else {
 661                     instanceFields.add(field);
 662                 }
 663             }
 664 
 665             // dump static field descriptors
 666             writeFieldDescriptors(staticFields, ik);
 667 
 668             // dump instance field descriptors
 669             writeFieldDescriptors(instanceFields, null);
 670         } else {
 671             if (k instanceof ObjArrayKlass) {
 672                 ObjArrayKlass oak = (ObjArrayKlass) k;
 673                 Klass bottomKlass = oak.getBottomKlass();
 674                 if (bottomKlass instanceof InstanceKlass) {
 675                     InstanceKlass ik = (InstanceKlass) bottomKlass;
 676                     writeObjectID(ik.getClassLoader());
 677                     writeObjectID(null); // ik.getJavaMirror().getSigners());
 678                     writeObjectID(null); // ik.getJavaMirror().getProtectionDomain());
 679                 } else {
 680                     writeObjectID(null);
 681                     writeObjectID(null);
 682                     writeObjectID(null);
 683                 }
 684             } else {
 685                 writeObjectID(null);
 686                 writeObjectID(null);
 687                 writeObjectID(null);
 688             }
 689             // two reserved id fields
 690             writeObjectID(null);
 691             writeObjectID(null);
 692             // write zero instance size -- as instance size
 693             // is variable for arrays.
 694             out.writeInt(0);
 695             // no constant pool for array klasses
 696             out.writeShort((short) 0);
 697             // no static fields for array klasses
 698             out.writeShort((short) 0);
 699             // no instance fields for array klasses
 700             out.writeShort((short) 0);
 701         }
 702     }
 703 
 704     private void dumpStackTraces() throws IOException {
 705         // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
 706         writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE );
 707         out.writeInt(DUMMY_STACK_TRACE_ID);
 708         out.writeInt(0);                    // thread number
 709         out.writeInt(0);                    // frame count
 710 
 711         int frameSerialNum = 0;
 712         int numThreads = 0;
 713         Threads threads = VM.getVM().getThreads();
 714 
 715         for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
 716             Oop threadObj = thread.getThreadObj();
 717             if (threadObj != null && !thread.isExiting() && !thread.isHiddenFromExternalView()) {
 718 
 719                 // dump thread stack trace
 720                 ThreadStackTrace st = new ThreadStackTrace(thread);
 721                 st.dumpStack(-1);
 722                 numThreads++;
 723 
 724                 // write HPROF_FRAME records for this thread's stack trace
 725                 int depth = st.getStackDepth();
 726                 int threadFrameStart = frameSerialNum;
 727                 for (int j=0; j < depth; j++) {
 728                     StackFrameInfo frame = st.stackFrameAt(j);
 729                     Method m = frame.getMethod();
 730                     int classSerialNum = KlassMap.indexOf(m.getMethodHolder()) + 1;
 731                     // the class serial number starts from 1
 732                     assert classSerialNum > 0:"class not found";
 733                     dumpStackFrame(++frameSerialNum, classSerialNum, m, frame.getBCI());
 734                 }
 735 
 736                 // write HPROF_TRACE record for one thread
 737                 writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE + depth * (int)VM.getVM().getOopSize());
 738                 int stackSerialNum = numThreads + DUMMY_STACK_TRACE_ID;
 739                 out.writeInt(stackSerialNum);      // stack trace serial number
 740                 out.writeInt(numThreads);          // thread serial number
 741                 out.writeInt(depth);               // frame count
 742                 for (int j=1; j <= depth; j++) {
 743                     writeObjectID(threadFrameStart + j);
 744                 }
 745             }
 746         }
 747     }
 748 
 749     private void dumpStackFrame(int frameSN, int classSN, Method m, int bci) throws IOException {
 750         int lineNumber;
 751         if (m.isNative()) {
 752             lineNumber = -3; // native frame
 753         } else {
 754             lineNumber = m.getLineNumberFromBCI(bci);
 755         }
 756         // First dump UTF8 if needed
 757         writeSymbol(m.getName());                              // method's name
 758         writeSymbol(m.getSignature());                         // method's signature
 759         writeSymbol(m.getMethodHolder().getSourceFileName());  // source file name
 760         // Then write FRAME descriptor
 761         writeHeader(HPROF_FRAME, 4 * (int)VM.getVM().getOopSize() + 2 * (int)INT_SIZE);
 762         writeObjectID(frameSN);                                  // frame serial number
 763         writeSymbolID(m.getName());                              // method's name
 764         writeSymbolID(m.getSignature());                         // method's signature
 765         writeSymbolID(m.getMethodHolder().getSourceFileName());  // source file name
 766         out.writeInt(classSN);                                   // class serial number
 767         out.writeInt(lineNumber);                                // line number
 768     }
 769 
 770     protected void writeJavaThread(JavaThread jt, int index) throws IOException {
 771         out.writeByte((byte) HPROF_GC_ROOT_THREAD_OBJ);
 772         writeObjectID(jt.getThreadObj());
 773         out.writeInt(index);
 774         out.writeInt(DUMMY_STACK_TRACE_ID);
 775         writeLocalJNIHandles(jt, index);
 776     }
 777 
 778     protected void writeLocalJNIHandles(JavaThread jt, int index) throws IOException {
 779         final int threadIndex = index;
 780         JNIHandleBlock blk = jt.activeHandles();
 781         if (blk != null) {
 782             try {
 783                 blk.oopsDo(new AddressVisitor() {
 784                            public void visitAddress(Address handleAddr) {
 785                                try {
 786                                    if (handleAddr != null) {
 787                                        OopHandle oopHandle = handleAddr.getOopHandleAt(0);
 788                                        Oop oop = objectHeap.newOop(oopHandle);
 789                                        // exclude JNI handles hotspot internal objects
 790                                        if (oop != null && isJavaVisible(oop)) {
 791                                            out.writeByte((byte) HPROF_GC_ROOT_JNI_LOCAL);
 792                                            writeObjectID(oop);
 793                                            out.writeInt(threadIndex);
 794                                            out.writeInt(EMPTY_FRAME_DEPTH);
 795                                        }
 796                                    }
 797                                } catch (IOException exp) {
 798                                    throw new RuntimeException(exp);
 799                                }
 800                            }
 801                            public void visitCompOopAddress(Address handleAddr) {
 802                              throw new RuntimeException(
 803                                    " Should not reach here. JNIHandles are not compressed \n");
 804                            }
 805                        });
 806             } catch (RuntimeException re) {
 807                 handleRuntimeException(re);
 808             }
 809         }
 810     }
 811 
 812     protected void writeGlobalJNIHandle(Address handleAddr) throws IOException {
 813         OopHandle oopHandle = handleAddr.getOopHandleAt(0);
 814         Oop oop = objectHeap.newOop(oopHandle);
 815         // exclude JNI handles of hotspot internal objects
 816         if (oop != null && isJavaVisible(oop)) {
 817             out.writeByte((byte) HPROF_GC_ROOT_JNI_GLOBAL);
 818             writeObjectID(oop);
 819             // use JNIHandle address as ID
 820             writeObjectID(getAddressValue(handleAddr));
 821         }
 822     }
 823 
 824     protected void writeObjectArray(ObjArray array) throws IOException {
 825         int headerSize = getArrayHeaderSize(true);
 826         final int length = calculateArrayMaxLength(array.getLength(),
 827                                                    headerSize,
 828                                                    OBJ_ID_SIZE,
 829                                                    "Object");
 830         out.writeByte((byte) HPROF_GC_OBJ_ARRAY_DUMP);
 831         writeObjectID(array);
 832         out.writeInt(DUMMY_STACK_TRACE_ID);
 833         out.writeInt(length);
 834         writeObjectID(array.getKlass().getJavaMirror());
 835         for (int index = 0; index < length; index++) {
 836             OopHandle handle = array.getOopHandleAt(index);
 837             writeObjectID(getAddressValue(handle));
 838         }
 839     }
 840 
 841     protected void writePrimitiveArray(TypeArray array) throws IOException {
 842         int headerSize = getArrayHeaderSize(false);
 843         TypeArrayKlass tak = (TypeArrayKlass) array.getKlass();
 844         final int type = (int) tak.getElementType();
 845         final String typeName = tak.getElementTypeName();
 846         final long typeSize = getSizeForType(type);
 847         final int length = calculateArrayMaxLength(array.getLength(),
 848                                                    headerSize,
 849                                                    typeSize,
 850                                                    typeName);
 851         out.writeByte((byte) HPROF_GC_PRIM_ARRAY_DUMP);
 852         writeObjectID(array);
 853         out.writeInt(DUMMY_STACK_TRACE_ID);
 854         out.writeInt(length);
 855         out.writeByte((byte) type);
 856         switch (type) {
 857             case TypeArrayKlass.T_BOOLEAN:
 858                 writeBooleanArray(array, length);
 859                 break;
 860             case TypeArrayKlass.T_CHAR:
 861                 writeCharArray(array, length);
 862                 break;
 863             case TypeArrayKlass.T_FLOAT:
 864                 writeFloatArray(array, length);
 865                 break;
 866             case TypeArrayKlass.T_DOUBLE:
 867                 writeDoubleArray(array, length);
 868                 break;
 869             case TypeArrayKlass.T_BYTE:
 870                 writeByteArray(array, length);
 871                 break;
 872             case TypeArrayKlass.T_SHORT:
 873                 writeShortArray(array, length);
 874                 break;
 875             case TypeArrayKlass.T_INT:
 876                 writeIntArray(array, length);
 877                 break;
 878             case TypeArrayKlass.T_LONG:
 879                 writeLongArray(array, length);
 880                 break;
 881             default:
 882                 throw new RuntimeException(
 883                     "Should not reach here: Unknown type: " + type);
 884         }
 885     }
 886 
 887     private void writeBooleanArray(TypeArray array, int length) throws IOException {
 888         for (int index = 0; index < length; index++) {
 889              long offset = BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE;
 890              out.writeBoolean(array.getHandle().getJBooleanAt(offset));
 891         }
 892     }
 893 
 894     private void writeByteArray(TypeArray array, int length) throws IOException {
 895         for (int index = 0; index < length; index++) {
 896              long offset = BYTE_BASE_OFFSET + index * BYTE_SIZE;
 897              out.writeByte(array.getHandle().getJByteAt(offset));
 898         }
 899     }
 900 
 901     private void writeShortArray(TypeArray array, int length) throws IOException {
 902         for (int index = 0; index < length; index++) {
 903              long offset = SHORT_BASE_OFFSET + index * SHORT_SIZE;
 904              out.writeShort(array.getHandle().getJShortAt(offset));
 905         }
 906     }
 907 
 908     private void writeIntArray(TypeArray array, int length) throws IOException {
 909         for (int index = 0; index < length; index++) {
 910              long offset = INT_BASE_OFFSET + index * INT_SIZE;
 911              out.writeInt(array.getHandle().getJIntAt(offset));
 912         }
 913     }
 914 
 915     private void writeLongArray(TypeArray array, int length) throws IOException {
 916         for (int index = 0; index < length; index++) {
 917              long offset = LONG_BASE_OFFSET + index * LONG_SIZE;
 918              out.writeLong(array.getHandle().getJLongAt(offset));
 919         }
 920     }
 921 
 922     private void writeCharArray(TypeArray array, int length) throws IOException {
 923         for (int index = 0; index < length; index++) {
 924              long offset = CHAR_BASE_OFFSET + index * CHAR_SIZE;
 925              out.writeChar(array.getHandle().getJCharAt(offset));
 926         }
 927     }
 928 
 929     private void writeFloatArray(TypeArray array, int length) throws IOException {
 930         for (int index = 0; index < length; index++) {
 931              long offset = FLOAT_BASE_OFFSET + index * FLOAT_SIZE;
 932              out.writeFloat(array.getHandle().getJFloatAt(offset));
 933         }
 934     }
 935 
 936     private void writeDoubleArray(TypeArray array, int length) throws IOException {
 937         for (int index = 0; index < length; index++) {
 938              long offset = DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE;
 939              out.writeDouble(array.getHandle().getJDoubleAt(offset));
 940         }
 941     }
 942 
 943     protected void writeInstance(Instance instance) throws IOException {
 944         out.writeByte((byte) HPROF_GC_INSTANCE_DUMP);
 945         writeObjectID(instance);
 946         out.writeInt(DUMMY_STACK_TRACE_ID);
 947         Klass klass = instance.getKlass();
 948         writeObjectID(klass.getJavaMirror());
 949 
 950         ClassData cd = (ClassData) classDataCache.get(klass);
 951 
 952         if (Assert.ASSERTS_ENABLED) {
 953             Assert.that(cd != null, "can not get class data for " + klass.getName().asString() + klass.getAddress());
 954         }
 955         List fields = cd.fields;
 956         int size = cd.instSize;
 957         out.writeInt(size);
 958         for (Iterator itr = fields.iterator(); itr.hasNext();) {
 959             writeField((Field) itr.next(), instance);
 960         }
 961     }
 962 
 963     //-- Internals only below this point
 964 
 965     private void writeFieldDescriptors(List fields, InstanceKlass ik)
 966         throws IOException {
 967         // ik == null for instance fields.
 968         out.writeShort((short) fields.size());
 969         for (Iterator itr = fields.iterator(); itr.hasNext();) {
 970             Field field = (Field) itr.next();
 971             Symbol name = field.getName();
 972             writeSymbolID(name);
 973             char typeCode = (char) field.getSignature().getByteAt(0);
 974             int kind = signatureToHprofKind(typeCode);
 975             out.writeByte((byte)kind);
 976             if (ik != null) {
 977                 // static field
 978                 writeField(field, ik.getJavaMirror());
 979             }
 980         }
 981     }
 982 
 983     public static int signatureToHprofKind(char ch) {
 984         switch (ch) {
 985         case JVM_SIGNATURE_CLASS:
 986         case JVM_SIGNATURE_ARRAY:
 987             return HPROF_NORMAL_OBJECT;
 988         case JVM_SIGNATURE_BOOLEAN:
 989             return HPROF_BOOLEAN;
 990         case JVM_SIGNATURE_CHAR:
 991             return HPROF_CHAR;
 992         case JVM_SIGNATURE_FLOAT:
 993             return HPROF_FLOAT;
 994         case JVM_SIGNATURE_DOUBLE:
 995             return HPROF_DOUBLE;
 996         case JVM_SIGNATURE_BYTE:
 997             return HPROF_BYTE;
 998         case JVM_SIGNATURE_SHORT:
 999             return HPROF_SHORT;
1000         case JVM_SIGNATURE_INT:
1001             return HPROF_INT;
1002         case JVM_SIGNATURE_LONG:
1003             return HPROF_LONG;
1004         default:
1005             throw new RuntimeException("should not reach here");
1006         }
1007     }
1008 
1009     private void writeField(Field field, Oop oop) throws IOException {
1010         char typeCode = (char) field.getSignature().getByteAt(0);
1011         switch (typeCode) {
1012         case JVM_SIGNATURE_BOOLEAN:
1013             out.writeBoolean(((BooleanField)field).getValue(oop));
1014             break;
1015         case JVM_SIGNATURE_CHAR:
1016             out.writeChar(((CharField)field).getValue(oop));
1017             break;
1018         case JVM_SIGNATURE_BYTE:
1019             out.writeByte(((ByteField)field).getValue(oop));
1020             break;
1021         case JVM_SIGNATURE_SHORT:
1022             out.writeShort(((ShortField)field).getValue(oop));
1023             break;
1024         case JVM_SIGNATURE_INT:
1025             out.writeInt(((IntField)field).getValue(oop));
1026             break;
1027         case JVM_SIGNATURE_LONG:
1028             out.writeLong(((LongField)field).getValue(oop));
1029             break;
1030         case JVM_SIGNATURE_FLOAT:
1031             out.writeFloat(((FloatField)field).getValue(oop));
1032             break;
1033         case JVM_SIGNATURE_DOUBLE:
1034             out.writeDouble(((DoubleField)field).getValue(oop));
1035             break;
1036         case JVM_SIGNATURE_CLASS:
1037         case JVM_SIGNATURE_ARRAY: {
1038             if (VM.getVM().isCompressedOopsEnabled()) {
1039               OopHandle handle = ((NarrowOopField)field).getValueAsOopHandle(oop);
1040               writeObjectID(getAddressValue(handle));
1041             } else {
1042               OopHandle handle = ((OopField)field).getValueAsOopHandle(oop);
1043               writeObjectID(getAddressValue(handle));
1044             }
1045             break;
1046         }
1047         default:
1048             throw new RuntimeException("should not reach here");
1049         }
1050     }
1051 
1052     private void writeHeader(int tag, int len) throws IOException {
1053         out.writeByte((byte)tag);
1054         out.writeInt(0); // current ticks
1055         out.writeInt(len);
1056     }
1057 
1058     private void writeDummyTrace() throws IOException {
1059         writeHeader(HPROF_TRACE, 3 * 4);
1060         out.writeInt(DUMMY_STACK_TRACE_ID);
1061         out.writeInt(0);
1062         out.writeInt(0);
1063     }
1064 
1065     private void writeClassSymbols(Klass k) throws IOException {
1066         writeSymbol(k.getName());
1067         if (k instanceof InstanceKlass) {
1068             InstanceKlass ik = (InstanceKlass) k;
1069             List declaredFields = ik.getImmediateFields();
1070             for (Iterator itr = declaredFields.iterator(); itr.hasNext();) {
1071                 Field field = (Field) itr.next();
1072                 writeSymbol(field.getName());
1073             }
1074         }
1075     }
1076 
1077     private void writeSymbols() throws IOException {
1078         // Write all the symbols that are used by the classes
1079         ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
1080         try {
1081              cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
1082                             public void visit(Klass k) {
1083                                 try {
1084                                     writeClassSymbols(k);
1085                                 } catch (IOException e) {
1086                                     throw new RuntimeException(e);
1087                                 }
1088                             }
1089                         });
1090         } catch (RuntimeException re) {
1091             handleRuntimeException(re);
1092         }
1093     }
1094 
1095     private void writeSymbol(Symbol sym) throws IOException {
1096         // If name is already written don't write it again.
1097         if (names.add(sym)) {
1098             byte[] buf = sym.asString().getBytes("UTF-8");
1099             writeHeader(HPROF_UTF8, buf.length + OBJ_ID_SIZE);
1100             writeSymbolID(sym);
1101             out.write(buf);
1102         }
1103     }
1104 
1105     private void writeClasses() throws IOException {
1106         // write class list (id, name) association
1107         ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
1108         try {
1109             cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
1110                 public void visit(Klass k) {
1111                     try {
1112                         Instance clazz = k.getJavaMirror();
1113                         writeHeader(HPROF_LOAD_CLASS, 2 * (OBJ_ID_SIZE + 4));
1114                         out.writeInt(serialNum);
1115                         writeObjectID(clazz);
1116                         KlassMap.add(serialNum - 1, k);
1117                         out.writeInt(DUMMY_STACK_TRACE_ID);
1118                         writeSymbolID(k.getName());
1119                         serialNum++;
1120                     } catch (IOException exp) {
1121                         throw new RuntimeException(exp);
1122                     }
1123                 }
1124             });
1125         } catch (RuntimeException re) {
1126             handleRuntimeException(re);
1127         }
1128     }
1129 
1130     // writes hprof binary file header
1131     private void writeFileHeader() throws IOException {
1132         // version string
1133         out.writeBytes(HPROF_HEADER_1_0_2);
1134         out.writeByte((byte)'\0');
1135 
1136         // write identifier size. we use pointers as identifiers.
1137         out.writeInt(OBJ_ID_SIZE);
1138 
1139         // timestamp -- file creation time.
1140         out.writeLong(System.currentTimeMillis());
1141     }
1142 
1143     // writes unique ID for an object
1144     private void writeObjectID(Oop oop) throws IOException {
1145         OopHandle handle = (oop != null)? oop.getHandle() : null;
1146         long address = getAddressValue(handle);
1147         writeObjectID(address);
1148     }
1149 
1150     private void writeSymbolID(Symbol sym) throws IOException {
1151         assert names.contains(sym);
1152         writeObjectID(getAddressValue(sym.getAddress()));
1153     }
1154 
1155     private void writeObjectID(long address) throws IOException {
1156         if (OBJ_ID_SIZE == 4) {
1157             out.writeInt((int) address);
1158         } else {
1159             out.writeLong(address);
1160         }
1161     }
1162 
1163     private long getAddressValue(Address addr) {
1164         return (addr == null)? 0L : dbg.getAddressValue(addr);
1165     }
1166 
1167     // get all declared as well as inherited (directly/indirectly) fields
1168     private static List/*<Field>*/ getInstanceFields(InstanceKlass ik) {
1169         InstanceKlass klass = ik;
1170         List res = new ArrayList();
1171         while (klass != null) {
1172             List curFields = klass.getImmediateFields();
1173             for (Iterator itr = curFields.iterator(); itr.hasNext();) {
1174                 Field f = (Field) itr.next();
1175                 if (! f.isStatic()) {
1176                     res.add(f);
1177                 }
1178             }
1179             klass = (InstanceKlass) klass.getSuper();
1180         }
1181         return res;
1182     }
1183 
1184     // get size in bytes (in stream) required for given fields.  Note
1185     // that this is not the same as object size in heap. The size in
1186     // heap will include size of padding/alignment bytes as well.
1187     private int getSizeForFields(List fields) {
1188         int size = 0;
1189         for (Iterator itr = fields.iterator(); itr.hasNext();) {
1190             Field field = (Field) itr.next();
1191             char typeCode = (char) field.getSignature().getByteAt(0);
1192             switch (typeCode) {
1193             case JVM_SIGNATURE_BOOLEAN:
1194             case JVM_SIGNATURE_BYTE:
1195                 size++;
1196                 break;
1197             case JVM_SIGNATURE_CHAR:
1198             case JVM_SIGNATURE_SHORT:
1199                 size += 2;
1200                 break;
1201             case JVM_SIGNATURE_INT:
1202             case JVM_SIGNATURE_FLOAT:
1203                 size += 4;
1204                 break;
1205             case JVM_SIGNATURE_CLASS:
1206             case JVM_SIGNATURE_ARRAY:
1207                 size += OBJ_ID_SIZE;
1208                 break;
1209             case JVM_SIGNATURE_LONG:
1210             case JVM_SIGNATURE_DOUBLE:
1211                 size += 8;
1212                 break;
1213             default:
1214                 throw new RuntimeException("should not reach here");
1215             }
1216         }
1217         return size;
1218     }
1219 
1220     // We don't have allocation site info. We write a dummy
1221     // stack trace with this id.
1222     private static final int DUMMY_STACK_TRACE_ID = 1;
1223     private static final int EMPTY_FRAME_DEPTH = -1;
1224 
1225     private DataOutputStream out;
1226     private FileOutputStream fos;
1227     private Debugger dbg;
1228     private ObjectHeap objectHeap;
1229     private ArrayList<Klass> KlassMap;
1230 
1231     // oopSize of the debuggee
1232     private int OBJ_ID_SIZE;
1233 
1234     // Added for hprof file format 1.0.2 support
1235     private boolean useSegmentedHeapDump;
1236     private long currentSegmentStart;
1237 
1238     private long BOOLEAN_BASE_OFFSET;
1239     private long BYTE_BASE_OFFSET;
1240     private long CHAR_BASE_OFFSET;
1241     private long SHORT_BASE_OFFSET;
1242     private long INT_BASE_OFFSET;
1243     private long LONG_BASE_OFFSET;
1244     private long FLOAT_BASE_OFFSET;
1245     private long DOUBLE_BASE_OFFSET;
1246     private long OBJECT_BASE_OFFSET;
1247 
1248     private long BOOLEAN_SIZE;
1249     private long BYTE_SIZE;
1250     private long CHAR_SIZE;
1251     private long SHORT_SIZE;
1252     private long INT_SIZE;
1253     private long LONG_SIZE;
1254     private long FLOAT_SIZE;
1255     private long DOUBLE_SIZE;
1256 
1257     private static class ClassData {
1258         int instSize;
1259         List fields;
1260 
1261         ClassData(int instSize, List fields) {
1262             this.instSize = instSize;
1263             this.fields = fields;
1264         }
1265     }
1266 
1267     private Map classDataCache = new HashMap(); // <InstanceKlass, ClassData>
1268 }