src/share/vm/services/heapDumper.cpp

Sat, 11 Sep 2010 08:18:31 +0200

author
thurka
date
Sat, 11 Sep 2010 08:18:31 +0200
changeset 2130
30f67acf635d
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

6765718: Indicate which thread throwing OOME when generating the heap dump at OOME
Summary: Emit a fake frame that makes it look like the thread is in the OutOfMemoryError zero-parameter constructor
Reviewed-by: dcubed

     1 /*
     2  * Copyright (c) 2005, 2010, 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  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_heapDumper.cpp.incl"
    28 /*
    29  * HPROF binary format - description copied from:
    30  *   src/share/demo/jvmti/hprof/hprof_io.c
    31  *
    32  *
    33  *  header    "JAVA PROFILE 1.0.1" or "JAVA PROFILE 1.0.2"
    34  *            (0-terminated)
    35  *
    36  *  u4        size of identifiers. Identifiers are used to represent
    37  *            UTF8 strings, objects, stack traces, etc. They usually
    38  *            have the same size as host pointers. For example, on
    39  *            Solaris and Win32, the size is 4.
    40  * u4         high word
    41  * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
    42  * [record]*  a sequence of records.
    43  *
    44  *
    45  * Record format:
    46  *
    47  * u1         a TAG denoting the type of the record
    48  * u4         number of *microseconds* since the time stamp in the
    49  *            header. (wraps around in a little more than an hour)
    50  * u4         number of bytes *remaining* in the record. Note that
    51  *            this number excludes the tag and the length field itself.
    52  * [u1]*      BODY of the record (a sequence of bytes)
    53  *
    54  *
    55  * The following TAGs are supported:
    56  *
    57  * TAG           BODY       notes
    58  *----------------------------------------------------------
    59  * HPROF_UTF8               a UTF8-encoded name
    60  *
    61  *               id         name ID
    62  *               [u1]*      UTF8 characters (no trailing zero)
    63  *
    64  * HPROF_LOAD_CLASS         a newly loaded class
    65  *
    66  *                u4        class serial number (> 0)
    67  *                id        class object ID
    68  *                u4        stack trace serial number
    69  *                id        class name ID
    70  *
    71  * HPROF_UNLOAD_CLASS       an unloading class
    72  *
    73  *                u4        class serial_number
    74  *
    75  * HPROF_FRAME              a Java stack frame
    76  *
    77  *                id        stack frame ID
    78  *                id        method name ID
    79  *                id        method signature ID
    80  *                id        source file name ID
    81  *                u4        class serial number
    82  *                i4        line number. >0: normal
    83  *                                       -1: unknown
    84  *                                       -2: compiled method
    85  *                                       -3: native method
    86  *
    87  * HPROF_TRACE              a Java stack trace
    88  *
    89  *               u4         stack trace serial number
    90  *               u4         thread serial number
    91  *               u4         number of frames
    92  *               [id]*      stack frame IDs
    93  *
    94  *
    95  * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
    96  *
    97  *               u2         flags 0x0001: incremental vs. complete
    98  *                                0x0002: sorted by allocation vs. live
    99  *                                0x0004: whether to force a GC
   100  *               u4         cutoff ratio
   101  *               u4         total live bytes
   102  *               u4         total live instances
   103  *               u8         total bytes allocated
   104  *               u8         total instances allocated
   105  *               u4         number of sites that follow
   106  *               [u1        is_array: 0:  normal object
   107  *                                    2:  object array
   108  *                                    4:  boolean array
   109  *                                    5:  char array
   110  *                                    6:  float array
   111  *                                    7:  double array
   112  *                                    8:  byte array
   113  *                                    9:  short array
   114  *                                    10: int array
   115  *                                    11: long array
   116  *                u4        class serial number (may be zero during startup)
   117  *                u4        stack trace serial number
   118  *                u4        number of bytes alive
   119  *                u4        number of instances alive
   120  *                u4        number of bytes allocated
   121  *                u4]*      number of instance allocated
   122  *
   123  * HPROF_START_THREAD       a newly started thread.
   124  *
   125  *               u4         thread serial number (> 0)
   126  *               id         thread object ID
   127  *               u4         stack trace serial number
   128  *               id         thread name ID
   129  *               id         thread group name ID
   130  *               id         thread group parent name ID
   131  *
   132  * HPROF_END_THREAD         a terminating thread.
   133  *
   134  *               u4         thread serial number
   135  *
   136  * HPROF_HEAP_SUMMARY       heap summary
   137  *
   138  *               u4         total live bytes
   139  *               u4         total live instances
   140  *               u8         total bytes allocated
   141  *               u8         total instances allocated
   142  *
   143  * HPROF_HEAP_DUMP          denote a heap dump
   144  *
   145  *               [heap dump sub-records]*
   146  *
   147  *                          There are four kinds of heap dump sub-records:
   148  *
   149  *               u1         sub-record type
   150  *
   151  *               HPROF_GC_ROOT_UNKNOWN         unknown root
   152  *
   153  *                          id         object ID
   154  *
   155  *               HPROF_GC_ROOT_THREAD_OBJ      thread object
   156  *
   157  *                          id         thread object ID  (may be 0 for a
   158  *                                     thread newly attached through JNI)
   159  *                          u4         thread sequence number
   160  *                          u4         stack trace sequence number
   161  *
   162  *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
   163  *
   164  *                          id         object ID
   165  *                          id         JNI global ref ID
   166  *
   167  *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
   168  *
   169  *                          id         object ID
   170  *                          u4         thread serial number
   171  *                          u4         frame # in stack trace (-1 for empty)
   172  *
   173  *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
   174  *
   175  *                          id         object ID
   176  *                          u4         thread serial number
   177  *                          u4         frame # in stack trace (-1 for empty)
   178  *
   179  *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
   180  *
   181  *                          id         object ID
   182  *                          u4         thread serial number
   183  *
   184  *               HPROF_GC_ROOT_STICKY_CLASS    System class
   185  *
   186  *                          id         object ID
   187  *
   188  *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
   189  *
   190  *                          id         object ID
   191  *                          u4         thread serial number
   192  *
   193  *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
   194  *
   195  *                          id         object ID
   196  *
   197  *               HPROF_GC_CLASS_DUMP           dump of a class object
   198  *
   199  *                          id         class object ID
   200  *                          u4         stack trace serial number
   201  *                          id         super class object ID
   202  *                          id         class loader object ID
   203  *                          id         signers object ID
   204  *                          id         protection domain object ID
   205  *                          id         reserved
   206  *                          id         reserved
   207  *
   208  *                          u4         instance size (in bytes)
   209  *
   210  *                          u2         size of constant pool
   211  *                          [u2,       constant pool index,
   212  *                           ty,       type
   213  *                                     2:  object
   214  *                                     4:  boolean
   215  *                                     5:  char
   216  *                                     6:  float
   217  *                                     7:  double
   218  *                                     8:  byte
   219  *                                     9:  short
   220  *                                     10: int
   221  *                                     11: long
   222  *                           vl]*      and value
   223  *
   224  *                          u2         number of static fields
   225  *                          [id,       static field name,
   226  *                           ty,       type,
   227  *                           vl]*      and value
   228  *
   229  *                          u2         number of inst. fields (not inc. super)
   230  *                          [id,       instance field name,
   231  *                           ty]*      type
   232  *
   233  *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
   234  *
   235  *                          id         object ID
   236  *                          u4         stack trace serial number
   237  *                          id         class object ID
   238  *                          u4         number of bytes that follow
   239  *                          [vl]*      instance field values (class, followed
   240  *                                     by super, super's super ...)
   241  *
   242  *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
   243  *
   244  *                          id         array object ID
   245  *                          u4         stack trace serial number
   246  *                          u4         number of elements
   247  *                          id         array class ID
   248  *                          [id]*      elements
   249  *
   250  *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
   251  *
   252  *                          id         array object ID
   253  *                          u4         stack trace serial number
   254  *                          u4         number of elements
   255  *                          u1         element type
   256  *                                     4:  boolean array
   257  *                                     5:  char array
   258  *                                     6:  float array
   259  *                                     7:  double array
   260  *                                     8:  byte array
   261  *                                     9:  short array
   262  *                                     10: int array
   263  *                                     11: long array
   264  *                          [u1]*      elements
   265  *
   266  * HPROF_CPU_SAMPLES        a set of sample traces of running threads
   267  *
   268  *                u4        total number of samples
   269  *                u4        # of traces
   270  *               [u4        # of samples
   271  *                u4]*      stack trace serial number
   272  *
   273  * HPROF_CONTROL_SETTINGS   the settings of on/off switches
   274  *
   275  *                u4        0x00000001: alloc traces on/off
   276  *                          0x00000002: cpu sampling on/off
   277  *                u2        stack trace depth
   278  *
   279  *
   280  * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
   281  * be generated as a sequence of heap dump segments. This sequence is
   282  * terminated by an end record. The additional tags allowed by format
   283  * "JAVA PROFILE 1.0.2" are:
   284  *
   285  * HPROF_HEAP_DUMP_SEGMENT  denote a heap dump segment
   286  *
   287  *               [heap dump sub-records]*
   288  *               The same sub-record types allowed by HPROF_HEAP_DUMP
   289  *
   290  * HPROF_HEAP_DUMP_END      denotes the end of a heap dump
   291  *
   292  */
   295 // HPROF tags
   297 typedef enum {
   298   // top-level records
   299   HPROF_UTF8                    = 0x01,
   300   HPROF_LOAD_CLASS              = 0x02,
   301   HPROF_UNLOAD_CLASS            = 0x03,
   302   HPROF_FRAME                   = 0x04,
   303   HPROF_TRACE                   = 0x05,
   304   HPROF_ALLOC_SITES             = 0x06,
   305   HPROF_HEAP_SUMMARY            = 0x07,
   306   HPROF_START_THREAD            = 0x0A,
   307   HPROF_END_THREAD              = 0x0B,
   308   HPROF_HEAP_DUMP               = 0x0C,
   309   HPROF_CPU_SAMPLES             = 0x0D,
   310   HPROF_CONTROL_SETTINGS        = 0x0E,
   312   // 1.0.2 record types
   313   HPROF_HEAP_DUMP_SEGMENT       = 0x1C,
   314   HPROF_HEAP_DUMP_END           = 0x2C,
   316   // field types
   317   HPROF_ARRAY_OBJECT            = 0x01,
   318   HPROF_NORMAL_OBJECT           = 0x02,
   319   HPROF_BOOLEAN                 = 0x04,
   320   HPROF_CHAR                    = 0x05,
   321   HPROF_FLOAT                   = 0x06,
   322   HPROF_DOUBLE                  = 0x07,
   323   HPROF_BYTE                    = 0x08,
   324   HPROF_SHORT                   = 0x09,
   325   HPROF_INT                     = 0x0A,
   326   HPROF_LONG                    = 0x0B,
   328   // data-dump sub-records
   329   HPROF_GC_ROOT_UNKNOWN         = 0xFF,
   330   HPROF_GC_ROOT_JNI_GLOBAL      = 0x01,
   331   HPROF_GC_ROOT_JNI_LOCAL       = 0x02,
   332   HPROF_GC_ROOT_JAVA_FRAME      = 0x03,
   333   HPROF_GC_ROOT_NATIVE_STACK    = 0x04,
   334   HPROF_GC_ROOT_STICKY_CLASS    = 0x05,
   335   HPROF_GC_ROOT_THREAD_BLOCK    = 0x06,
   336   HPROF_GC_ROOT_MONITOR_USED    = 0x07,
   337   HPROF_GC_ROOT_THREAD_OBJ      = 0x08,
   338   HPROF_GC_CLASS_DUMP           = 0x20,
   339   HPROF_GC_INSTANCE_DUMP        = 0x21,
   340   HPROF_GC_OBJ_ARRAY_DUMP       = 0x22,
   341   HPROF_GC_PRIM_ARRAY_DUMP      = 0x23
   342 } hprofTag;
   344 // Default stack trace ID (used for dummy HPROF_TRACE record)
   345 enum {
   346   STACK_TRACE_ID = 1,
   347   INITIAL_CLASS_COUNT = 200
   348 };
   350 // Supports I/O operations on a dump file
   352 class DumpWriter : public StackObj {
   353  private:
   354   enum {
   355     io_buffer_size  = 8*M
   356   };
   358   int _fd;              // file descriptor (-1 if dump file not open)
   359   jlong _bytes_written; // number of byte written to dump file
   361   char* _buffer;    // internal buffer
   362   int _size;
   363   int _pos;
   365   char* _error;   // error message when I/O fails
   367   void set_file_descriptor(int fd)              { _fd = fd; }
   368   int file_descriptor() const                   { return _fd; }
   370   char* buffer() const                          { return _buffer; }
   371   int buffer_size() const                       { return _size; }
   372   int position() const                          { return _pos; }
   373   void set_position(int pos)                    { _pos = pos; }
   375   void set_error(const char* error)             { _error = (char*)os::strdup(error); }
   377   // all I/O go through this function
   378   void write_internal(void* s, int len);
   380  public:
   381   DumpWriter(const char* path);
   382   ~DumpWriter();
   384   void close();
   385   bool is_open() const                  { return file_descriptor() >= 0; }
   386   void flush();
   388   // total number of bytes written to the disk
   389   jlong bytes_written() const           { return _bytes_written; }
   391   // adjust the number of bytes written to disk (used to keep the count
   392   // of the number of bytes written in case of rewrites)
   393   void adjust_bytes_written(jlong n)     { _bytes_written += n; }
   395   // number of (buffered) bytes as yet unwritten to the dump file
   396   jlong bytes_unwritten() const          { return (jlong)position(); }
   398   char* error() const                   { return _error; }
   400   jlong current_offset();
   401   void seek_to_offset(jlong pos);
   403   // writer functions
   404   void write_raw(void* s, int len);
   405   void write_u1(u1 x)                   { write_raw((void*)&x, 1); }
   406   void write_u2(u2 x);
   407   void write_u4(u4 x);
   408   void write_u8(u8 x);
   409   void write_objectID(oop o);
   410   void write_classID(Klass* k);
   411   void write_id(u4 x);
   412 };
   414 DumpWriter::DumpWriter(const char* path) {
   415   // try to allocate an I/O buffer of io_buffer_size. If there isn't
   416   // sufficient memory then reduce size until we can allocate something.
   417   _size = io_buffer_size;
   418   do {
   419     _buffer = (char*)os::malloc(_size);
   420     if (_buffer == NULL) {
   421       _size = _size >> 1;
   422     }
   423   } while (_buffer == NULL && _size > 0);
   424   assert((_size > 0 && _buffer != NULL) || (_size == 0 && _buffer == NULL), "sanity check");
   425   _pos = 0;
   426   _error = NULL;
   427   _bytes_written = 0L;
   428   _fd = os::create_binary_file(path, false);    // don't replace existing file
   430   // if the open failed we record the error
   431   if (_fd < 0) {
   432     _error = (char*)os::strdup(strerror(errno));
   433   }
   434 }
   436 DumpWriter::~DumpWriter() {
   437   // flush and close dump file
   438   if (file_descriptor() >= 0) {
   439     close();
   440   }
   441   if (_buffer != NULL) os::free(_buffer);
   442   if (_error != NULL) os::free(_error);
   443 }
   445 // closes dump file (if open)
   446 void DumpWriter::close() {
   447   // flush and close dump file
   448   if (file_descriptor() >= 0) {
   449     flush();
   450     ::close(file_descriptor());
   451   }
   452 }
   454 // write directly to the file
   455 void DumpWriter::write_internal(void* s, int len) {
   456   if (is_open()) {
   457     int n = ::write(file_descriptor(), s, len);
   458     if (n > 0) {
   459       _bytes_written += n;
   460     }
   461     if (n != len) {
   462       if (n < 0) {
   463         set_error(strerror(errno));
   464       } else {
   465         set_error("file size limit");
   466       }
   467       ::close(file_descriptor());
   468       set_file_descriptor(-1);
   469     }
   470   }
   471 }
   473 // write raw bytes
   474 void DumpWriter::write_raw(void* s, int len) {
   475   if (is_open()) {
   476     // flush buffer to make toom
   477     if ((position()+ len) >= buffer_size()) {
   478       flush();
   479     }
   481     // buffer not available or too big to buffer it
   482     if ((buffer() == NULL) || (len >= buffer_size())) {
   483       write_internal(s, len);
   484     } else {
   485       // Should optimize this for u1/u2/u4/u8 sizes.
   486       memcpy(buffer() + position(), s, len);
   487       set_position(position() + len);
   488     }
   489   }
   490 }
   492 // flush any buffered bytes to the file
   493 void DumpWriter::flush() {
   494   if (is_open() && position() > 0) {
   495     write_internal(buffer(), position());
   496     set_position(0);
   497   }
   498 }
   501 jlong DumpWriter::current_offset() {
   502   if (is_open()) {
   503     // the offset is the file offset plus whatever we have buffered
   504     jlong offset = os::current_file_offset(file_descriptor());
   505     assert(offset >= 0, "lseek failed");
   506     return offset + (jlong)position();
   507   } else {
   508     return (jlong)-1;
   509   }
   510 }
   512 void DumpWriter::seek_to_offset(jlong off) {
   513   assert(off >= 0, "bad offset");
   515   // need to flush before seeking
   516   flush();
   518   // may be closed due to I/O error
   519   if (is_open()) {
   520     jlong n = os::seek_to_file_offset(file_descriptor(), off);
   521     assert(n >= 0, "lseek failed");
   522   }
   523 }
   525 void DumpWriter::write_u2(u2 x) {
   526   u2 v;
   527   Bytes::put_Java_u2((address)&v, x);
   528   write_raw((void*)&v, 2);
   529 }
   531 void DumpWriter::write_u4(u4 x) {
   532   u4 v;
   533   Bytes::put_Java_u4((address)&v, x);
   534   write_raw((void*)&v, 4);
   535 }
   537 void DumpWriter::write_u8(u8 x) {
   538   u8 v;
   539   Bytes::put_Java_u8((address)&v, x);
   540   write_raw((void*)&v, 8);
   541 }
   543 void DumpWriter::write_objectID(oop o) {
   544   address a = (address)((uintptr_t)o);
   545 #ifdef _LP64
   546   write_u8((u8)a);
   547 #else
   548   write_u4((u4)a);
   549 #endif
   550 }
   552 void DumpWriter::write_id(u4 x) {
   553 #ifdef _LP64
   554   write_u8((u8) x);
   555 #else
   556   write_u4(x);
   557 #endif
   558 }
   560 // We use java mirror as the class ID
   561 void DumpWriter::write_classID(Klass* k) {
   562   write_objectID(k->java_mirror());
   563 }
   567 // Support class with a collection of functions used when dumping the heap
   569 class DumperSupport : AllStatic {
   570  public:
   572   // write a header of the given type
   573   static void write_header(DumpWriter* writer, hprofTag tag, u4 len);
   575   // returns hprof tag for the given type signature
   576   static hprofTag sig2tag(symbolOop sig);
   577   // returns hprof tag for the given basic type
   578   static hprofTag type2tag(BasicType type);
   580   // returns the size of the instance of the given class
   581   static u4 instance_size(klassOop k);
   583   // dump a jfloat
   584   static void dump_float(DumpWriter* writer, jfloat f);
   585   // dump a jdouble
   586   static void dump_double(DumpWriter* writer, jdouble d);
   587   // dumps the raw value of the given field
   588   static void dump_field_value(DumpWriter* writer, char type, address addr);
   589   // dumps static fields of the given class
   590   static void dump_static_fields(DumpWriter* writer, klassOop k);
   591   // dump the raw values of the instance fields of the given object
   592   static void dump_instance_fields(DumpWriter* writer, oop o);
   593   // dumps the definition of the instance fields for a given class
   594   static void dump_instance_field_descriptors(DumpWriter* writer, klassOop k);
   595   // creates HPROF_GC_INSTANCE_DUMP record for the given object
   596   static void dump_instance(DumpWriter* writer, oop o);
   597   // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
   598   // array classes
   599   static void dump_class_and_array_classes(DumpWriter* writer, klassOop k);
   600   // creates HPROF_GC_CLASS_DUMP record for a given primitive array
   601   // class (and each multi-dimensional array class too)
   602   static void dump_basic_type_array_class(DumpWriter* writer, klassOop k);
   604   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
   605   static void dump_object_array(DumpWriter* writer, objArrayOop array);
   606   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
   607   static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
   608   // create HPROF_FRAME record for the given method and bci
   609   static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, methodOop m, int bci);
   610 };
   612 // write a header of the given type
   613 void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
   614   writer->write_u1((u1)tag);
   615   writer->write_u4(0);                  // current ticks
   616   writer->write_u4(len);
   617 }
   619 // returns hprof tag for the given type signature
   620 hprofTag DumperSupport::sig2tag(symbolOop sig) {
   621   switch (sig->byte_at(0)) {
   622     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;
   623     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
   624     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
   625     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
   626     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
   627     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
   628     case JVM_SIGNATURE_INT      : return HPROF_INT;
   629     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
   630     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
   631     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
   632     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
   633   }
   634 }
   636 hprofTag DumperSupport::type2tag(BasicType type) {
   637   switch (type) {
   638     case T_BYTE     : return HPROF_BYTE;
   639     case T_CHAR     : return HPROF_CHAR;
   640     case T_FLOAT    : return HPROF_FLOAT;
   641     case T_DOUBLE   : return HPROF_DOUBLE;
   642     case T_INT      : return HPROF_INT;
   643     case T_LONG     : return HPROF_LONG;
   644     case T_SHORT    : return HPROF_SHORT;
   645     case T_BOOLEAN  : return HPROF_BOOLEAN;
   646     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
   647   }
   648 }
   650 // dump a jfloat
   651 void DumperSupport::dump_float(DumpWriter* writer, jfloat f) {
   652   if (g_isnan(f)) {
   653     writer->write_u4(0x7fc00000);    // collapsing NaNs
   654   } else {
   655     union {
   656       int i;
   657       float f;
   658     } u;
   659     u.f = (float)f;
   660     writer->write_u4((u4)u.i);
   661   }
   662 }
   664 // dump a jdouble
   665 void DumperSupport::dump_double(DumpWriter* writer, jdouble d) {
   666   union {
   667     jlong l;
   668     double d;
   669   } u;
   670   if (g_isnan(d)) {                 // collapsing NaNs
   671     u.l = (jlong)(0x7ff80000);
   672     u.l = (u.l << 32);
   673   } else {
   674     u.d = (double)d;
   675   }
   676   writer->write_u8((u8)u.l);
   677 }
   679 // dumps the raw value of the given field
   680 void DumperSupport::dump_field_value(DumpWriter* writer, char type, address addr) {
   681   switch (type) {
   682     case JVM_SIGNATURE_CLASS :
   683     case JVM_SIGNATURE_ARRAY : {
   684       oop o;
   685       if (UseCompressedOops) {
   686         o = oopDesc::load_decode_heap_oop((narrowOop*)addr);
   687       } else {
   688         o = oopDesc::load_decode_heap_oop((oop*)addr);
   689       }
   691       // reflection and sun.misc.Unsafe classes may have a reference to a
   692       // klassOop so filter it out.
   693       if (o != NULL && o->is_klass()) {
   694         o = NULL;
   695       }
   697       // FIXME: When sharing is enabled we don't emit field references to objects
   698       // in shared spaces. We can remove this once we write records for the classes
   699       // and strings that are shared.
   700       if (o != NULL && o->is_shared()) {
   701         o = NULL;
   702       }
   703       writer->write_objectID(o);
   704       break;
   705     }
   706     case JVM_SIGNATURE_BYTE     : {
   707       jbyte* b = (jbyte*)addr;
   708       writer->write_u1((u1)*b);
   709       break;
   710     }
   711     case JVM_SIGNATURE_CHAR     : {
   712       jchar* c = (jchar*)addr;
   713       writer->write_u2((u2)*c);
   714       break;
   715     }
   716     case JVM_SIGNATURE_SHORT : {
   717       jshort* s = (jshort*)addr;
   718       writer->write_u2((u2)*s);
   719       break;
   720     }
   721     case JVM_SIGNATURE_FLOAT : {
   722       jfloat* f = (jfloat*)addr;
   723       dump_float(writer, *f);
   724       break;
   725     }
   726     case JVM_SIGNATURE_DOUBLE : {
   727       jdouble* f = (jdouble*)addr;
   728       dump_double(writer, *f);
   729       break;
   730     }
   731     case JVM_SIGNATURE_INT : {
   732       jint* i = (jint*)addr;
   733       writer->write_u4((u4)*i);
   734       break;
   735     }
   736     case JVM_SIGNATURE_LONG     : {
   737       jlong* l = (jlong*)addr;
   738       writer->write_u8((u8)*l);
   739       break;
   740     }
   741     case JVM_SIGNATURE_BOOLEAN : {
   742       jboolean* b = (jboolean*)addr;
   743       writer->write_u1((u1)*b);
   744       break;
   745     }
   746     default : ShouldNotReachHere();
   747   }
   748 }
   750 // returns the size of the instance of the given class
   751 u4 DumperSupport::instance_size(klassOop k) {
   752   HandleMark hm;
   753   instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
   755   int size = 0;
   757   for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
   758     if (!fld.access_flags().is_static()) {
   759       symbolOop sig = fld.signature();
   760       switch (sig->byte_at(0)) {
   761         case JVM_SIGNATURE_CLASS   :
   762         case JVM_SIGNATURE_ARRAY   : size += oopSize; break;
   764         case JVM_SIGNATURE_BYTE    :
   765         case JVM_SIGNATURE_BOOLEAN : size += 1; break;
   767         case JVM_SIGNATURE_CHAR    :
   768         case JVM_SIGNATURE_SHORT   : size += 2; break;
   770         case JVM_SIGNATURE_INT     :
   771         case JVM_SIGNATURE_FLOAT   : size += 4; break;
   773         case JVM_SIGNATURE_LONG    :
   774         case JVM_SIGNATURE_DOUBLE  : size += 8; break;
   776         default : ShouldNotReachHere();
   777       }
   778     }
   779   }
   780   return (u4)size;
   781 }
   783 // dumps static fields of the given class
   784 void DumperSupport::dump_static_fields(DumpWriter* writer, klassOop k) {
   785   HandleMark hm;
   786   instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
   788   // pass 1 - count the static fields
   789   u2 field_count = 0;
   790   for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
   791     if (fldc.access_flags().is_static()) field_count++;
   792   }
   794   writer->write_u2(field_count);
   796   // pass 2 - dump the field descriptors and raw values
   797   for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
   798     if (fld.access_flags().is_static()) {
   799       symbolOop sig = fld.signature();
   801       writer->write_objectID(fld.name());   // name
   802       writer->write_u1(sig2tag(sig));       // type
   804       // value
   805       int offset = fld.offset();
   806       address addr = (address)k + offset;
   808       dump_field_value(writer, sig->byte_at(0), addr);
   809     }
   810   }
   811 }
   813 // dump the raw values of the instance fields of the given object
   814 void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) {
   815   HandleMark hm;
   816   instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), o->klass());
   818   for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
   819     if (!fld.access_flags().is_static()) {
   820       symbolOop sig = fld.signature();
   821       address addr = (address)o + fld.offset();
   823       dump_field_value(writer, sig->byte_at(0), addr);
   824     }
   825   }
   826 }
   828 // dumps the definition of the instance fields for a given class
   829 void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, klassOop k) {
   830   HandleMark hm;
   831   instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
   833   // pass 1 - count the instance fields
   834   u2 field_count = 0;
   835   for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
   836     if (!fldc.access_flags().is_static()) field_count++;
   837   }
   839   writer->write_u2(field_count);
   841   // pass 2 - dump the field descriptors
   842   for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
   843     if (!fld.access_flags().is_static()) {
   844       symbolOop sig = fld.signature();
   846       writer->write_objectID(fld.name());                   // name
   847       writer->write_u1(sig2tag(sig));       // type
   848     }
   849   }
   850 }
   852 // creates HPROF_GC_INSTANCE_DUMP record for the given object
   853 void DumperSupport::dump_instance(DumpWriter* writer, oop o) {
   854   klassOop k = o->klass();
   856   writer->write_u1(HPROF_GC_INSTANCE_DUMP);
   857   writer->write_objectID(o);
   858   writer->write_u4(STACK_TRACE_ID);
   860   // class ID
   861   writer->write_classID(Klass::cast(k));
   863   // number of bytes that follow
   864   writer->write_u4(instance_size(k) );
   866   // field values
   867   dump_instance_fields(writer, o);
   868 }
   870 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
   871 // its array classes
   872 void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, klassOop k) {
   873   Klass* klass = Klass::cast(k);
   874   assert(klass->oop_is_instance(), "not an instanceKlass");
   875   instanceKlass* ik = (instanceKlass*)klass;
   877   writer->write_u1(HPROF_GC_CLASS_DUMP);
   879   // class ID
   880   writer->write_classID(ik);
   881   writer->write_u4(STACK_TRACE_ID);
   883   // super class ID
   884   klassOop java_super = ik->java_super();
   885   if (java_super == NULL) {
   886     writer->write_objectID(NULL);
   887   } else {
   888     writer->write_classID(Klass::cast(java_super));
   889   }
   891   writer->write_objectID(ik->class_loader());
   892   writer->write_objectID(ik->signers());
   893   writer->write_objectID(ik->protection_domain());
   895   // reserved
   896   writer->write_objectID(NULL);
   897   writer->write_objectID(NULL);
   899   // instance size
   900   writer->write_u4(DumperSupport::instance_size(k));
   902   // size of constant pool - ignored by HAT 1.1
   903   writer->write_u2(0);
   905   // number of static fields
   906   dump_static_fields(writer, k);
   908   // description of instance fields
   909   dump_instance_field_descriptors(writer, k);
   911   // array classes
   912   k = klass->array_klass_or_null();
   913   while (k != NULL) {
   914     Klass* klass = Klass::cast(k);
   915     assert(klass->oop_is_objArray(), "not an objArrayKlass");
   917     writer->write_u1(HPROF_GC_CLASS_DUMP);
   918     writer->write_classID(klass);
   919     writer->write_u4(STACK_TRACE_ID);
   921     // super class of array classes is java.lang.Object
   922     java_super = klass->java_super();
   923     assert(java_super != NULL, "checking");
   924     writer->write_classID(Klass::cast(java_super));
   926     writer->write_objectID(ik->class_loader());
   927     writer->write_objectID(ik->signers());
   928     writer->write_objectID(ik->protection_domain());
   930     writer->write_objectID(NULL);    // reserved
   931     writer->write_objectID(NULL);
   932     writer->write_u4(0);             // instance size
   933     writer->write_u2(0);             // constant pool
   934     writer->write_u2(0);             // static fields
   935     writer->write_u2(0);             // instance fields
   937     // get the array class for the next rank
   938     k = klass->array_klass_or_null();
   939   }
   940 }
   942 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
   943 // class (and each multi-dimensional array class too)
   944 void DumperSupport::dump_basic_type_array_class(DumpWriter* writer, klassOop k) {
   945  // array classes
   946  while (k != NULL) {
   947     Klass* klass = Klass::cast(k);
   949     writer->write_u1(HPROF_GC_CLASS_DUMP);
   950     writer->write_classID(klass);
   951     writer->write_u4(STACK_TRACE_ID);
   953     // super class of array classes is java.lang.Object
   954     klassOop java_super = klass->java_super();
   955     assert(java_super != NULL, "checking");
   956     writer->write_classID(Klass::cast(java_super));
   958     writer->write_objectID(NULL);    // loader
   959     writer->write_objectID(NULL);    // signers
   960     writer->write_objectID(NULL);    // protection domain
   962     writer->write_objectID(NULL);    // reserved
   963     writer->write_objectID(NULL);
   964     writer->write_u4(0);             // instance size
   965     writer->write_u2(0);             // constant pool
   966     writer->write_u2(0);             // static fields
   967     writer->write_u2(0);             // instance fields
   969     // get the array class for the next rank
   970     k = klass->array_klass_or_null();
   971   }
   972 }
   974 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
   975 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
   977   // filter this
   978   if (array->klass() == Universe::systemObjArrayKlassObj()) return;
   980   writer->write_u1(HPROF_GC_OBJ_ARRAY_DUMP);
   981   writer->write_objectID(array);
   982   writer->write_u4(STACK_TRACE_ID);
   983   writer->write_u4((u4)array->length());
   985   // array class ID
   986   writer->write_classID(Klass::cast(array->klass()));
   988   // [id]* elements
   989   for (int index=0; index<array->length(); index++) {
   990     oop o = array->obj_at(index);
   991     writer->write_objectID(o);
   992   }
   993 }
   995 #define WRITE_ARRAY(Array, Type, Size) \
   996   for (int i=0; i<Array->length(); i++) { writer->write_##Size((Size)array->Type##_at(i)); }
   999 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
  1000 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
  1001   BasicType type = typeArrayKlass::cast(array->klass())->element_type();
  1003   writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
  1004   writer->write_objectID(array);
  1005   writer->write_u4(STACK_TRACE_ID);
  1006   writer->write_u4((u4)array->length());
  1007   writer->write_u1(type2tag(type));
  1009   // nothing to copy
  1010   if (array->length() == 0) {
  1011     return;
  1014   // If the byte ordering is big endian then we can copy most types directly
  1015   int length_in_bytes = array->length() * type2aelembytes(type);
  1016   assert(length_in_bytes > 0, "nothing to copy");
  1018   switch (type) {
  1019     case T_INT : {
  1020       if (Bytes::is_Java_byte_ordering_different()) {
  1021         WRITE_ARRAY(array, int, u4);
  1022       } else {
  1023         writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
  1025       break;
  1027     case T_BYTE : {
  1028       writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
  1029       break;
  1031     case T_CHAR : {
  1032       if (Bytes::is_Java_byte_ordering_different()) {
  1033         WRITE_ARRAY(array, char, u2);
  1034       } else {
  1035         writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
  1037       break;
  1039     case T_SHORT : {
  1040       if (Bytes::is_Java_byte_ordering_different()) {
  1041         WRITE_ARRAY(array, short, u2);
  1042       } else {
  1043         writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
  1045       break;
  1047     case T_BOOLEAN : {
  1048       if (Bytes::is_Java_byte_ordering_different()) {
  1049         WRITE_ARRAY(array, bool, u1);
  1050       } else {
  1051         writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
  1053       break;
  1055     case T_LONG : {
  1056       if (Bytes::is_Java_byte_ordering_different()) {
  1057         WRITE_ARRAY(array, long, u8);
  1058       } else {
  1059         writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
  1061       break;
  1064     // handle float/doubles in a special value to ensure than NaNs are
  1065     // written correctly. TO DO: Check if we can avoid this on processors that
  1066     // use IEEE 754.
  1068     case T_FLOAT : {
  1069       for (int i=0; i<array->length(); i++) {
  1070         dump_float( writer, array->float_at(i) );
  1072       break;
  1074     case T_DOUBLE : {
  1075       for (int i=0; i<array->length(); i++) {
  1076         dump_double( writer, array->double_at(i) );
  1078       break;
  1080     default : ShouldNotReachHere();
  1084 // create a HPROF_FRAME record of the given methodOop and bci
  1085 void DumperSupport::dump_stack_frame(DumpWriter* writer,
  1086                                      int frame_serial_num,
  1087                                      int class_serial_num,
  1088                                      methodOop m,
  1089                                      int bci) {
  1090   int line_number;
  1091   if (m->is_native()) {
  1092     line_number = -3;  // native frame
  1093   } else {
  1094     line_number = m->line_number_from_bci(bci);
  1097   write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
  1098   writer->write_id(frame_serial_num);               // frame serial number
  1099   writer->write_objectID(m->name());                // method's name
  1100   writer->write_objectID(m->signature());           // method's signature
  1102   assert(Klass::cast(m->method_holder())->oop_is_instance(), "not instanceKlass");
  1103   writer->write_objectID(instanceKlass::cast(m->method_holder())->source_file_name());  // source file name
  1104   writer->write_u4(class_serial_num);               // class serial number
  1105   writer->write_u4((u4) line_number);               // line number
  1108 // Support class used to generate HPROF_UTF8 records from the entries in the
  1109 // SymbolTable.
  1111 class SymbolTableDumper : public OopClosure {
  1112  private:
  1113   DumpWriter* _writer;
  1114   DumpWriter* writer() const                { return _writer; }
  1115  public:
  1116   SymbolTableDumper(DumpWriter* writer)     { _writer = writer; }
  1117   void do_oop(oop* obj_p);
  1118   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
  1119 };
  1121 void SymbolTableDumper::do_oop(oop* obj_p) {
  1122   ResourceMark rm;
  1123   symbolOop sym = (symbolOop)*obj_p;
  1125   int len = sym->utf8_length();
  1126   if (len > 0) {
  1127     char* s = sym->as_utf8();
  1128     DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
  1129     writer()->write_objectID(sym);
  1130     writer()->write_raw(s, len);
  1135 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
  1137 class JNILocalsDumper : public OopClosure {
  1138  private:
  1139   DumpWriter* _writer;
  1140   u4 _thread_serial_num;
  1141   int _frame_num;
  1142   DumpWriter* writer() const                { return _writer; }
  1143  public:
  1144   JNILocalsDumper(DumpWriter* writer, u4 thread_serial_num) {
  1145     _writer = writer;
  1146     _thread_serial_num = thread_serial_num;
  1147     _frame_num = -1;  // default - empty stack
  1149   void set_frame_number(int n) { _frame_num = n; }
  1150   void do_oop(oop* obj_p);
  1151   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
  1152 };
  1155 void JNILocalsDumper::do_oop(oop* obj_p) {
  1156   // ignore null or deleted handles
  1157   oop o = *obj_p;
  1158   if (o != NULL && o != JNIHandles::deleted_handle()) {
  1159     writer()->write_u1(HPROF_GC_ROOT_JNI_LOCAL);
  1160     writer()->write_objectID(o);
  1161     writer()->write_u4(_thread_serial_num);
  1162     writer()->write_u4((u4)_frame_num);
  1167 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
  1169 class JNIGlobalsDumper : public OopClosure {
  1170  private:
  1171   DumpWriter* _writer;
  1172   DumpWriter* writer() const                { return _writer; }
  1174  public:
  1175   JNIGlobalsDumper(DumpWriter* writer) {
  1176     _writer = writer;
  1178   void do_oop(oop* obj_p);
  1179   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
  1180 };
  1182 void JNIGlobalsDumper::do_oop(oop* obj_p) {
  1183   oop o = *obj_p;
  1185   // ignore these
  1186   if (o == NULL || o == JNIHandles::deleted_handle()) return;
  1188   // we ignore global ref to symbols and other internal objects
  1189   if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
  1190     writer()->write_u1(HPROF_GC_ROOT_JNI_GLOBAL);
  1191     writer()->write_objectID(o);
  1192     writer()->write_objectID((oopDesc*)obj_p);      // global ref ID
  1194 };
  1197 // Support class used to generate HPROF_GC_ROOT_MONITOR_USED records
  1199 class MonitorUsedDumper : public OopClosure {
  1200  private:
  1201   DumpWriter* _writer;
  1202   DumpWriter* writer() const                { return _writer; }
  1203  public:
  1204   MonitorUsedDumper(DumpWriter* writer) {
  1205     _writer = writer;
  1207   void do_oop(oop* obj_p) {
  1208     writer()->write_u1(HPROF_GC_ROOT_MONITOR_USED);
  1209     writer()->write_objectID(*obj_p);
  1211   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
  1212 };
  1215 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
  1217 class StickyClassDumper : public OopClosure {
  1218  private:
  1219   DumpWriter* _writer;
  1220   DumpWriter* writer() const                { return _writer; }
  1221  public:
  1222   StickyClassDumper(DumpWriter* writer) {
  1223     _writer = writer;
  1225   void do_oop(oop* obj_p);
  1226   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
  1227 };
  1229 void StickyClassDumper::do_oop(oop* obj_p) {
  1230   if (*obj_p != NULL) {
  1231     oop o = *obj_p;
  1232     if (o->is_klass()) {
  1233       klassOop k = klassOop(o);
  1234       if (Klass::cast(k)->oop_is_instance()) {
  1235         instanceKlass* ik = instanceKlass::cast(k);
  1236         writer()->write_u1(HPROF_GC_ROOT_STICKY_CLASS);
  1237         writer()->write_classID(ik);
  1244 class VM_HeapDumper;
  1246 // Support class using when iterating over the heap.
  1248 class HeapObjectDumper : public ObjectClosure {
  1249  private:
  1250   VM_HeapDumper* _dumper;
  1251   DumpWriter* _writer;
  1253   VM_HeapDumper* dumper()               { return _dumper; }
  1254   DumpWriter* writer()                  { return _writer; }
  1256   // used to indicate that a record has been writen
  1257   void mark_end_of_record();
  1259  public:
  1260   HeapObjectDumper(VM_HeapDumper* dumper, DumpWriter* writer) {
  1261     _dumper = dumper;
  1262     _writer = writer;
  1265   // called for each object in the heap
  1266   void do_object(oop o);
  1267 };
  1269 void HeapObjectDumper::do_object(oop o) {
  1270   // hide the sentinel for deleted handles
  1271   if (o == JNIHandles::deleted_handle()) return;
  1273   // ignore KlassKlass objects
  1274   if (o->is_klass()) return;
  1276   // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
  1277   if (o->klass() == SystemDictionary::Class_klass()) {
  1278     if (!java_lang_Class::is_primitive(o)) {
  1279       return;
  1283   // create a HPROF_GC_INSTANCE record for each object
  1284   if (o->is_instance()) {
  1285     DumperSupport::dump_instance(writer(), o);
  1286     mark_end_of_record();
  1287   } else {
  1288     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
  1289     if (o->is_objArray()) {
  1290       DumperSupport::dump_object_array(writer(), objArrayOop(o));
  1291       mark_end_of_record();
  1292     } else {
  1293       // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
  1294       if (o->is_typeArray()) {
  1295         DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
  1296         mark_end_of_record();
  1302 // The VM operation that performs the heap dump
  1303 class VM_HeapDumper : public VM_GC_Operation {
  1304  private:
  1305   static VM_HeapDumper* _global_dumper;
  1306   static DumpWriter*    _global_writer;
  1307   DumpWriter*           _local_writer;
  1308   JavaThread*           _oome_thread;
  1309   methodOop             _oome_constructor;
  1310   bool _gc_before_heap_dump;
  1311   bool _is_segmented_dump;
  1312   jlong _dump_start;
  1313   GrowableArray<Klass*>* _klass_map;
  1314   ThreadStackTrace** _stack_traces;
  1315   int _num_threads;
  1317   // accessors and setters
  1318   static VM_HeapDumper* dumper()         {  assert(_global_dumper != NULL, "Error"); return _global_dumper; }
  1319   static DumpWriter* writer()            {  assert(_global_writer != NULL, "Error"); return _global_writer; }
  1320   void set_global_dumper() {
  1321     assert(_global_dumper == NULL, "Error");
  1322     _global_dumper = this;
  1324   void set_global_writer() {
  1325     assert(_global_writer == NULL, "Error");
  1326     _global_writer = _local_writer;
  1328   void clear_global_dumper() { _global_dumper = NULL; }
  1329   void clear_global_writer() { _global_writer = NULL; }
  1331   bool is_segmented_dump() const                { return _is_segmented_dump; }
  1332   void set_segmented_dump()                     { _is_segmented_dump = true; }
  1333   jlong dump_start() const                      { return _dump_start; }
  1334   void set_dump_start(jlong pos);
  1336   bool skip_operation() const;
  1338   // writes a HPROF_LOAD_CLASS record
  1339   static void do_load_class(klassOop k);
  1341   // writes a HPROF_GC_CLASS_DUMP record for the given class
  1342   // (and each array class too)
  1343   static void do_class_dump(klassOop k);
  1345   // writes a HPROF_GC_CLASS_DUMP records for a given basic type
  1346   // array (and each multi-dimensional array too)
  1347   static void do_basic_type_array_class_dump(klassOop k);
  1349   // HPROF_GC_ROOT_THREAD_OBJ records
  1350   int do_thread(JavaThread* thread, u4 thread_serial_num);
  1351   void do_threads();
  1353   void add_class_serial_number(Klass* k, int serial_num) {
  1354     _klass_map->at_put_grow(serial_num, k);
  1357   // HPROF_TRACE and HPROF_FRAME records
  1358   void dump_stack_traces();
  1360   // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
  1361   void write_dump_header();
  1363   // fixes up the length of the current dump record
  1364   void write_current_dump_record_length();
  1366   // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
  1367   // record in the case of a segmented heap dump)
  1368   void end_of_dump();
  1370  public:
  1371   VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) :
  1372     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
  1373                     0 /* total full collections, dummy, ignored */,
  1374                     gc_before_heap_dump) {
  1375     _local_writer = writer;
  1376     _gc_before_heap_dump = gc_before_heap_dump;
  1377     _is_segmented_dump = false;
  1378     _dump_start = (jlong)-1;
  1379     _klass_map = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, true);
  1380     _stack_traces = NULL;
  1381     _num_threads = 0;
  1382     if (oome) {
  1383       assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
  1384       // get OutOfMemoryError zero-parameter constructor
  1385       instanceKlass* oome_ik = instanceKlass::cast(SystemDictionary::OutOfMemoryError_klass());
  1386       _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
  1387                                                           vmSymbols::void_method_signature());
  1388       // get thread throwing OOME when generating the heap dump at OOME
  1389       _oome_thread = JavaThread::current();
  1390     } else {
  1391       _oome_thread = NULL;
  1392       _oome_constructor = NULL;
  1395   ~VM_HeapDumper() {
  1396     if (_stack_traces != NULL) {
  1397       for (int i=0; i < _num_threads; i++) {
  1398         delete _stack_traces[i];
  1400       FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces);
  1402     delete _klass_map;
  1405   VMOp_Type type() const { return VMOp_HeapDumper; }
  1406   // used to mark sub-record boundary
  1407   void check_segment_length();
  1408   void doit();
  1409 };
  1411 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
  1412 DumpWriter*    VM_HeapDumper::_global_writer = NULL;
  1414 bool VM_HeapDumper::skip_operation() const {
  1415   return false;
  1418 // sets the dump starting position
  1419 void VM_HeapDumper::set_dump_start(jlong pos) {
  1420   _dump_start = pos;
  1423  // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
  1424 void VM_HeapDumper::write_dump_header() {
  1425   if (writer()->is_open()) {
  1426     if (is_segmented_dump()) {
  1427       writer()->write_u1(HPROF_HEAP_DUMP_SEGMENT);
  1428     } else {
  1429       writer()->write_u1(HPROF_HEAP_DUMP);
  1431     writer()->write_u4(0); // current ticks
  1433     // record the starting position for the dump (its length will be fixed up later)
  1434     set_dump_start(writer()->current_offset());
  1435     writer()->write_u4(0);
  1439 // fixes up the length of the current dump record
  1440 void VM_HeapDumper::write_current_dump_record_length() {
  1441   if (writer()->is_open()) {
  1442     assert(dump_start() >= 0, "no dump start recorded");
  1444     // calculate the size of the dump record
  1445     jlong dump_end = writer()->current_offset();
  1446     jlong dump_len = (dump_end - dump_start() - 4);
  1448     // record length must fit in a u4
  1449     if (dump_len > (jlong)(4L*(jlong)G)) {
  1450       warning("record is too large");
  1453     // seek to the dump start and fix-up the length
  1454     writer()->seek_to_offset(dump_start());
  1455     writer()->write_u4((u4)dump_len);
  1457     // adjust the total size written to keep the bytes written correct.
  1458     writer()->adjust_bytes_written(-((long) sizeof(u4)));
  1460     // seek to dump end so we can continue
  1461     writer()->seek_to_offset(dump_end);
  1463     // no current dump record
  1464     set_dump_start((jlong)-1);
  1468 // used on a sub-record boundary to check if we need to start a
  1469 // new segment.
  1470 void VM_HeapDumper::check_segment_length() {
  1471   if (writer()->is_open()) {
  1472     if (is_segmented_dump()) {
  1473       // don't use current_offset that would be too expensive on a per record basis
  1474       jlong dump_end = writer()->bytes_written() + writer()->bytes_unwritten();
  1475       assert(dump_end == writer()->current_offset(), "checking");
  1476       jlong dump_len = (dump_end - dump_start() - 4);
  1477       assert(dump_len >= 0 && dump_len <= max_juint, "bad dump length");
  1479       if (dump_len > (jlong)HeapDumpSegmentSize) {
  1480         write_current_dump_record_length();
  1481         write_dump_header();
  1487 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
  1488 // record in the case of a segmented heap dump)
  1489 void VM_HeapDumper::end_of_dump() {
  1490   if (writer()->is_open()) {
  1491     write_current_dump_record_length();
  1493     // for segmented dump we write the end record
  1494     if (is_segmented_dump()) {
  1495       writer()->write_u1(HPROF_HEAP_DUMP_END);
  1496       writer()->write_u4(0);
  1497       writer()->write_u4(0);
  1502 // marks sub-record boundary
  1503 void HeapObjectDumper::mark_end_of_record() {
  1504   dumper()->check_segment_length();
  1507 // writes a HPROF_LOAD_CLASS record for the class (and each of its
  1508 // array classes)
  1509 void VM_HeapDumper::do_load_class(klassOop k) {
  1510   static u4 class_serial_num = 0;
  1512   // len of HPROF_LOAD_CLASS record
  1513   u4 remaining = 2*oopSize + 2*sizeof(u4);
  1515   // write a HPROF_LOAD_CLASS for the class and each array class
  1516   do {
  1517     DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
  1519     // class serial number is just a number
  1520     writer()->write_u4(++class_serial_num);
  1522     // class ID
  1523     Klass* klass = Klass::cast(k);
  1524     writer()->write_classID(klass);
  1526     // add the klassOop and class serial number pair
  1527     dumper()->add_class_serial_number(klass, class_serial_num);
  1529     writer()->write_u4(STACK_TRACE_ID);
  1531     // class name ID
  1532     symbolOop name = klass->name();
  1533     writer()->write_objectID(name);
  1535     // write a LOAD_CLASS record for the array type (if it exists)
  1536     k = klass->array_klass_or_null();
  1537   } while (k != NULL);
  1540 // writes a HPROF_GC_CLASS_DUMP record for the given class
  1541 void VM_HeapDumper::do_class_dump(klassOop k) {
  1542   DumperSupport::dump_class_and_array_classes(writer(), k);
  1545 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
  1546 // array (and each multi-dimensional array too)
  1547 void VM_HeapDumper::do_basic_type_array_class_dump(klassOop k) {
  1548   DumperSupport::dump_basic_type_array_class(writer(), k);
  1551 // Walk the stack of the given thread.
  1552 // Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local
  1553 // Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local
  1554 //
  1555 // It returns the number of Java frames in this thread stack
  1556 int VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) {
  1557   JNILocalsDumper blk(writer(), thread_serial_num);
  1559   oop threadObj = java_thread->threadObj();
  1560   assert(threadObj != NULL, "sanity check");
  1562   int stack_depth = 0;
  1563   if (java_thread->has_last_Java_frame()) {
  1565     // vframes are resource allocated
  1566     Thread* current_thread = Thread::current();
  1567     ResourceMark rm(current_thread);
  1568     HandleMark hm(current_thread);
  1570     RegisterMap reg_map(java_thread);
  1571     frame f = java_thread->last_frame();
  1572     vframe* vf = vframe::new_vframe(&f, &reg_map, java_thread);
  1573     frame* last_entry_frame = NULL;
  1574     int extra_frames = 0;
  1576     if (java_thread == _oome_thread && _oome_constructor != NULL) {
  1577       extra_frames++;
  1579     while (vf != NULL) {
  1580       blk.set_frame_number(stack_depth);
  1581       if (vf->is_java_frame()) {
  1583         // java frame (interpreted, compiled, ...)
  1584         javaVFrame *jvf = javaVFrame::cast(vf);
  1585         if (!(jvf->method()->is_native())) {
  1586           StackValueCollection* locals = jvf->locals();
  1587           for (int slot=0; slot<locals->size(); slot++) {
  1588             if (locals->at(slot)->type() == T_OBJECT) {
  1589               oop o = locals->obj_at(slot)();
  1591               if (o != NULL) {
  1592                 writer()->write_u1(HPROF_GC_ROOT_JAVA_FRAME);
  1593                 writer()->write_objectID(o);
  1594                 writer()->write_u4(thread_serial_num);
  1595                 writer()->write_u4((u4) (stack_depth + extra_frames));
  1599         } else {
  1600           // native frame
  1601           if (stack_depth == 0) {
  1602             // JNI locals for the top frame.
  1603             java_thread->active_handles()->oops_do(&blk);
  1604           } else {
  1605             if (last_entry_frame != NULL) {
  1606               // JNI locals for the entry frame
  1607               assert(last_entry_frame->is_entry_frame(), "checking");
  1608               last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
  1612         // increment only for Java frames
  1613         stack_depth++;
  1614         last_entry_frame = NULL;
  1616       } else {
  1617         // externalVFrame - if it's an entry frame then report any JNI locals
  1618         // as roots when we find the corresponding native javaVFrame
  1619         frame* fr = vf->frame_pointer();
  1620         assert(fr != NULL, "sanity check");
  1621         if (fr->is_entry_frame()) {
  1622           last_entry_frame = fr;
  1625       vf = vf->sender();
  1627   } else {
  1628     // no last java frame but there may be JNI locals
  1629     java_thread->active_handles()->oops_do(&blk);
  1631   return stack_depth;
  1635 // write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk
  1636 // the stack so that locals and JNI locals are dumped.
  1637 void VM_HeapDumper::do_threads() {
  1638   for (int i=0; i < _num_threads; i++) {
  1639     JavaThread* thread = _stack_traces[i]->thread();
  1640     oop threadObj = thread->threadObj();
  1641     u4 thread_serial_num = i+1;
  1642     u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
  1643     writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
  1644     writer()->write_objectID(threadObj);
  1645     writer()->write_u4(thread_serial_num);  // thread number
  1646     writer()->write_u4(stack_serial_num);   // stack trace serial number
  1647     int num_frames = do_thread(thread, thread_serial_num);
  1648     assert(num_frames == _stack_traces[i]->get_stack_depth(),
  1649            "total number of Java frames not matched");
  1654 // The VM operation that dumps the heap. The dump consists of the following
  1655 // records:
  1656 //
  1657 //  HPROF_HEADER
  1658 //  [HPROF_UTF8]*
  1659 //  [HPROF_LOAD_CLASS]*
  1660 //  [[HPROF_FRAME]*|HPROF_TRACE]*
  1661 //  [HPROF_GC_CLASS_DUMP]*
  1662 //  HPROF_HEAP_DUMP
  1663 //
  1664 // The HPROF_TRACE records represent the stack traces where the heap dump
  1665 // is generated and a "dummy trace" record which does not include
  1666 // any frames. The dummy trace record is used to be referenced as the
  1667 // unknown object alloc site.
  1668 //
  1669 // The HPROF_HEAP_DUMP record has a length following by sub-records. To allow
  1670 // the heap dump be generated in a single pass we remember the position of
  1671 // the dump length and fix it up after all sub-records have been written.
  1672 // To generate the sub-records we iterate over the heap, writing
  1673 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
  1674 // records as we go. Once that is done we write records for some of the GC
  1675 // roots.
  1677 void VM_HeapDumper::doit() {
  1679   HandleMark hm;
  1680   CollectedHeap* ch = Universe::heap();
  1681   if (_gc_before_heap_dump) {
  1682     ch->collect_as_vm_thread(GCCause::_heap_dump);
  1683   } else {
  1684     // make the heap parsable (no need to retire TLABs)
  1685     ch->ensure_parsability(false);
  1688   // At this point we should be the only dumper active, so
  1689   // the following should be safe.
  1690   set_global_dumper();
  1691   set_global_writer();
  1693   // Write the file header - use 1.0.2 for large heaps, otherwise 1.0.1
  1694   size_t used = ch->used();
  1695   const char* header;
  1696   if (used > (size_t)SegmentedHeapDumpThreshold) {
  1697     set_segmented_dump();
  1698     header = "JAVA PROFILE 1.0.2";
  1699   } else {
  1700     header = "JAVA PROFILE 1.0.1";
  1703   // header is few bytes long - no chance to overflow int
  1704   writer()->write_raw((void*)header, (int)strlen(header));
  1705   writer()->write_u1(0); // terminator
  1706   writer()->write_u4(oopSize);
  1707   writer()->write_u8(os::javaTimeMillis());
  1709   // HPROF_UTF8 records
  1710   SymbolTableDumper sym_dumper(writer());
  1711   SymbolTable::oops_do(&sym_dumper);
  1713   // write HPROF_LOAD_CLASS records
  1714   SystemDictionary::classes_do(&do_load_class);
  1715   Universe::basic_type_classes_do(&do_load_class);
  1717   // write HPROF_FRAME and HPROF_TRACE records
  1718   // this must be called after _klass_map is built when iterating the classes above.
  1719   dump_stack_traces();
  1721   // write HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT
  1722   write_dump_header();
  1724   // Writes HPROF_GC_CLASS_DUMP records
  1725   SystemDictionary::classes_do(&do_class_dump);
  1726   Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
  1727   check_segment_length();
  1729   // writes HPROF_GC_INSTANCE_DUMP records.
  1730   // After each sub-record is written check_segment_length will be invoked. When
  1731   // generated a segmented heap dump this allows us to check if the current
  1732   // segment exceeds a threshold and if so, then a new segment is started.
  1733   // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
  1734   // of the heap dump.
  1735   HeapObjectDumper obj_dumper(this, writer());
  1736   Universe::heap()->safe_object_iterate(&obj_dumper);
  1738   // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
  1739   do_threads();
  1740   check_segment_length();
  1742   // HPROF_GC_ROOT_MONITOR_USED
  1743   MonitorUsedDumper mon_dumper(writer());
  1744   ObjectSynchronizer::oops_do(&mon_dumper);
  1745   check_segment_length();
  1747   // HPROF_GC_ROOT_JNI_GLOBAL
  1748   JNIGlobalsDumper jni_dumper(writer());
  1749   JNIHandles::oops_do(&jni_dumper);
  1750   check_segment_length();
  1752   // HPROF_GC_ROOT_STICKY_CLASS
  1753   StickyClassDumper class_dumper(writer());
  1754   SystemDictionary::always_strong_oops_do(&class_dumper);
  1756   // fixes up the length of the dump record. In the case of a segmented
  1757   // heap then the HPROF_HEAP_DUMP_END record is also written.
  1758   end_of_dump();
  1760   // Now we clear the global variables, so that a future dumper might run.
  1761   clear_global_dumper();
  1762   clear_global_writer();
  1765 void VM_HeapDumper::dump_stack_traces() {
  1766   // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
  1767   DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
  1768   writer()->write_u4((u4) STACK_TRACE_ID);
  1769   writer()->write_u4(0);                    // thread number
  1770   writer()->write_u4(0);                    // frame count
  1772   _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads());
  1773   int frame_serial_num = 0;
  1774   for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
  1775     oop threadObj = thread->threadObj();
  1776     if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
  1777       // dump thread stack trace
  1778       ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
  1779       stack_trace->dump_stack_at_safepoint(-1);
  1780       _stack_traces[_num_threads++] = stack_trace;
  1782       // write HPROF_FRAME records for this thread's stack trace
  1783       int depth = stack_trace->get_stack_depth();
  1784       int thread_frame_start = frame_serial_num;
  1785       int extra_frames = 0;
  1786       // write fake frame that makes it look like the thread, which caused OOME,
  1787       // is in the OutOfMemoryError zero-parameter constructor
  1788       if (thread == _oome_thread && _oome_constructor != NULL) {
  1789         int oome_serial_num = _klass_map->find(Klass::cast(_oome_constructor->method_holder()));
  1790         // the class serial number starts from 1
  1791         assert(oome_serial_num > 0, "OutOfMemoryError class not found");
  1792         DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, oome_serial_num,
  1793                                         _oome_constructor, 0);
  1794         extra_frames++;
  1796       for (int j=0; j < depth; j++) {
  1797         StackFrameInfo* frame = stack_trace->stack_frame_at(j);
  1798         methodOop m = frame->method();
  1799         int class_serial_num = _klass_map->find(Klass::cast(m->method_holder()));
  1800         // the class serial number starts from 1
  1801         assert(class_serial_num > 0, "class not found");
  1802         DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, class_serial_num, m, frame->bci());
  1804       depth += extra_frames;
  1806       // write HPROF_TRACE record for one thread
  1807       DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4) + depth*oopSize);
  1808       int stack_serial_num = _num_threads + STACK_TRACE_ID;
  1809       writer()->write_u4(stack_serial_num);      // stack trace serial number
  1810       writer()->write_u4((u4) _num_threads);     // thread serial number
  1811       writer()->write_u4(depth);                 // frame count
  1812       for (int j=1; j <= depth; j++) {
  1813         writer()->write_id(thread_frame_start + j);
  1819 // dump the heap to given path.
  1820 int HeapDumper::dump(const char* path) {
  1821   assert(path != NULL && strlen(path) > 0, "path missing");
  1823   // print message in interactive case
  1824   if (print_to_tty()) {
  1825     tty->print_cr("Dumping heap to %s ...", path);
  1826     timer()->start();
  1829   // create the dump writer. If the file can be opened then bail
  1830   DumpWriter writer(path);
  1831   if (!writer.is_open()) {
  1832     set_error(writer.error());
  1833     if (print_to_tty()) {
  1834       tty->print_cr("Unable to create %s: %s", path,
  1835         (error() != NULL) ? error() : "reason unknown");
  1837     return -1;
  1840   // generate the dump
  1841   VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome);
  1842   if (Thread::current()->is_VM_thread()) {
  1843     assert(SafepointSynchronize::is_at_safepoint(), "Expected to be called at a safepoint");
  1844     dumper.doit();
  1845   } else {
  1846     VMThread::execute(&dumper);
  1849   // close dump file and record any error that the writer may have encountered
  1850   writer.close();
  1851   set_error(writer.error());
  1853   // print message in interactive case
  1854   if (print_to_tty()) {
  1855     timer()->stop();
  1856     if (error() == NULL) {
  1857       char msg[256];
  1858       sprintf(msg, "Heap dump file created [%s bytes in %3.3f secs]",
  1859         os::jlong_format_specifier(), timer()->seconds());
  1860       tty->print_cr(msg, writer.bytes_written());
  1861     } else {
  1862       tty->print_cr("Dump file is incomplete: %s", writer.error());
  1866   return (writer.error() == NULL) ? 0 : -1;
  1869 // stop timer (if still active), and free any error string we might be holding
  1870 HeapDumper::~HeapDumper() {
  1871   if (timer()->is_active()) {
  1872     timer()->stop();
  1874   set_error(NULL);
  1878 // returns the error string (resource allocated), or NULL
  1879 char* HeapDumper::error_as_C_string() const {
  1880   if (error() != NULL) {
  1881     char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
  1882     strcpy(str, error());
  1883     return str;
  1884   } else {
  1885     return NULL;
  1889 // set the error string
  1890 void HeapDumper::set_error(char* error) {
  1891   if (_error != NULL) {
  1892     os::free(_error);
  1894   if (error == NULL) {
  1895     _error = NULL;
  1896   } else {
  1897     _error = os::strdup(error);
  1898     assert(_error != NULL, "allocation failure");
  1902 // Called by out-of-memory error reporting by a single Java thread
  1903 // outside of a JVM safepoint
  1904 void HeapDumper::dump_heap_from_oome() {
  1905   HeapDumper::dump_heap(true);
  1908 // Called by error reporting by a single Java thread outside of a JVM safepoint,
  1909 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
  1910 // callers are strictly serialized and guaranteed not to interfere below. For more
  1911 // general use, however, this method will need modification to prevent
  1912 // inteference when updating the static variables base_path and dump_file_seq below.
  1913 void HeapDumper::dump_heap() {
  1914   HeapDumper::dump_heap(false);
  1917 void HeapDumper::dump_heap(bool oome) {
  1918   static char base_path[JVM_MAXPATHLEN] = {'\0'};
  1919   static uint dump_file_seq = 0;
  1920   char   my_path[JVM_MAXPATHLEN] = {'\0'};
  1922   // The dump file defaults to java_pid<pid>.hprof in the current working
  1923   // directory. HeapDumpPath=<file> can be used to specify an alternative
  1924   // dump file name or a directory where dump file is created.
  1925   if (dump_file_seq == 0) { // first time in, we initialize base_path
  1926     bool use_default_filename = true;
  1927     if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') {
  1928       // HeapDumpPath=<file> not specified
  1929     } else {
  1930       assert(strlen(HeapDumpPath) < sizeof(base_path), "HeapDumpPath too long");
  1931       strcpy(base_path, HeapDumpPath);
  1932       // check if the path is a directory (must exist)
  1933       DIR* dir = os::opendir(base_path);
  1934       if (dir == NULL) {
  1935         use_default_filename = false;
  1936       } else {
  1937         // HeapDumpPath specified a directory. We append a file separator
  1938         // (if needed).
  1939         os::closedir(dir);
  1940         size_t fs_len = strlen(os::file_separator());
  1941         if (strlen(base_path) >= fs_len) {
  1942           char* end = base_path;
  1943           end += (strlen(base_path) - fs_len);
  1944           if (strcmp(end, os::file_separator()) != 0) {
  1945             assert(strlen(base_path) + strlen(os::file_separator()) < sizeof(base_path),
  1946               "HeapDumpPath too long");
  1947             strcat(base_path, os::file_separator());
  1952     // If HeapDumpPath wasn't a file name then we append the default name
  1953     if (use_default_filename) {
  1954       char fn[32];
  1955       sprintf(fn, "java_pid%d", os::current_process_id());
  1956       assert(strlen(base_path) + strlen(fn) + strlen(".hprof") < sizeof(base_path), "HeapDumpPath too long");
  1957       strcat(base_path, fn);
  1958       strcat(base_path, ".hprof");
  1960     assert(strlen(base_path) < sizeof(my_path), "Buffer too small");
  1961     strcpy(my_path, base_path);
  1962   } else {
  1963     // Append a sequence number id for dumps following the first
  1964     char fn[33];
  1965     sprintf(fn, ".%d", dump_file_seq);
  1966     assert(strlen(base_path) + strlen(fn) < sizeof(my_path), "HeapDumpPath too long");
  1967     strcpy(my_path, base_path);
  1968     strcat(my_path, fn);
  1970   dump_file_seq++;   // increment seq number for next time we dump
  1972   HeapDumper dumper(false /* no GC before heap dump */,
  1973                     true  /* send to tty */,
  1974                     oome  /* pass along out-of-memory-error flag */);
  1975   dumper.dump(my_path);

mercurial