1 /*
   2  * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2023, Alibaba Group Holding Limited. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/javaClasses.inline.hpp"
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "gc/shared/gcVMOperations.hpp"
  35 #include "gc/shared/workerThread.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "jvm.h"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "memory/universe.hpp"
  41 #include "oops/fieldStreams.inline.hpp"
  42 #include "oops/klass.inline.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/objArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/typeArrayOop.inline.hpp"
  47 #include "runtime/continuationWrapper.inline.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/javaThread.inline.hpp"
  52 #include "runtime/jniHandles.hpp"
  53 #include "runtime/os.hpp"
  54 #include "runtime/threads.hpp"
  55 #include "runtime/threadSMR.hpp"
  56 #include "runtime/vframe.hpp"
  57 #include "runtime/vmOperations.hpp"
  58 #include "runtime/vmThread.hpp"
  59 #include "runtime/timerTrace.hpp"
  60 #include "services/heapDumper.hpp"
  61 #include "services/heapDumperCompression.hpp"
  62 #include "services/threadService.hpp"
  63 #include "utilities/checkedCast.hpp"
  64 #include "utilities/macros.hpp"
  65 #include "utilities/ostream.hpp"
  66 #ifdef LINUX
  67 #include "os_linux.hpp"
  68 #endif
  69 
  70 /*
  71  * HPROF binary format - description copied from:
  72  *   src/share/demo/jvmti/hprof/hprof_io.c
  73  *
  74  *
  75  *  header    "JAVA PROFILE 1.0.2" (0-terminated)
  76  *
  77  *  u4        size of identifiers. Identifiers are used to represent
  78  *            UTF8 strings, objects, stack traces, etc. They usually
  79  *            have the same size as host pointers.
  80  * u4         high word
  81  * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
  82  * [record]*  a sequence of records.
  83  *
  84  *
  85  * Record format:
  86  *
  87  * u1         a TAG denoting the type of the record
  88  * u4         number of *microseconds* since the time stamp in the
  89  *            header. (wraps around in a little more than an hour)
  90  * u4         number of bytes *remaining* in the record. Note that
  91  *            this number excludes the tag and the length field itself.
  92  * [u1]*      BODY of the record (a sequence of bytes)
  93  *
  94  *
  95  * The following TAGs are supported:
  96  *
  97  * TAG           BODY       notes
  98  *----------------------------------------------------------
  99  * HPROF_UTF8               a UTF8-encoded name
 100  *
 101  *               id         name ID
 102  *               [u1]*      UTF8 characters (no trailing zero)
 103  *
 104  * HPROF_LOAD_CLASS         a newly loaded class
 105  *
 106  *                u4        class serial number (> 0)
 107  *                id        class object ID
 108  *                u4        stack trace serial number
 109  *                id        class name ID
 110  *
 111  * HPROF_UNLOAD_CLASS       an unloading class
 112  *
 113  *                u4        class serial_number
 114  *
 115  * HPROF_FRAME              a Java stack frame
 116  *
 117  *                id        stack frame ID
 118  *                id        method name ID
 119  *                id        method signature ID
 120  *                id        source file name ID
 121  *                u4        class serial number
 122  *                i4        line number. >0: normal
 123  *                                       -1: unknown
 124  *                                       -2: compiled method
 125  *                                       -3: native method
 126  *
 127  * HPROF_TRACE              a Java stack trace
 128  *
 129  *               u4         stack trace serial number
 130  *               u4         thread serial number
 131  *               u4         number of frames
 132  *               [id]*      stack frame IDs
 133  *
 134  *
 135  * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
 136  *
 137  *               u2         flags 0x0001: incremental vs. complete
 138  *                                0x0002: sorted by allocation vs. live
 139  *                                0x0004: whether to force a GC
 140  *               u4         cutoff ratio
 141  *               u4         total live bytes
 142  *               u4         total live instances
 143  *               u8         total bytes allocated
 144  *               u8         total instances allocated
 145  *               u4         number of sites that follow
 146  *               [u1        is_array: 0:  normal object
 147  *                                    2:  object array
 148  *                                    4:  boolean array
 149  *                                    5:  char array
 150  *                                    6:  float array
 151  *                                    7:  double array
 152  *                                    8:  byte array
 153  *                                    9:  short array
 154  *                                    10: int array
 155  *                                    11: long array
 156  *                u4        class serial number (may be zero during startup)
 157  *                u4        stack trace serial number
 158  *                u4        number of bytes alive
 159  *                u4        number of instances alive
 160  *                u4        number of bytes allocated
 161  *                u4]*      number of instance allocated
 162  *
 163  * HPROF_START_THREAD       a newly started thread.
 164  *
 165  *               u4         thread serial number (> 0)
 166  *               id         thread object ID
 167  *               u4         stack trace serial number
 168  *               id         thread name ID
 169  *               id         thread group name ID
 170  *               id         thread group parent name ID
 171  *
 172  * HPROF_END_THREAD         a terminating thread.
 173  *
 174  *               u4         thread serial number
 175  *
 176  * HPROF_HEAP_SUMMARY       heap summary
 177  *
 178  *               u4         total live bytes
 179  *               u4         total live instances
 180  *               u8         total bytes allocated
 181  *               u8         total instances allocated
 182  *
 183  * HPROF_HEAP_DUMP          denote a heap dump
 184  *
 185  *               [heap dump sub-records]*
 186  *
 187  *                          There are four kinds of heap dump sub-records:
 188  *
 189  *               u1         sub-record type
 190  *
 191  *               HPROF_GC_ROOT_UNKNOWN         unknown root
 192  *
 193  *                          id         object ID
 194  *
 195  *               HPROF_GC_ROOT_THREAD_OBJ      thread object
 196  *
 197  *                          id         thread object ID  (may be 0 for a
 198  *                                     thread newly attached through JNI)
 199  *                          u4         thread sequence number
 200  *                          u4         stack trace sequence number
 201  *
 202  *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
 203  *
 204  *                          id         object ID
 205  *                          id         JNI global ref ID
 206  *
 207  *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
 208  *
 209  *                          id         object ID
 210  *                          u4         thread serial number
 211  *                          u4         frame # in stack trace (-1 for empty)
 212  *
 213  *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
 214  *
 215  *                          id         object ID
 216  *                          u4         thread serial number
 217  *                          u4         frame # in stack trace (-1 for empty)
 218  *
 219  *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
 220  *
 221  *                          id         object ID
 222  *                          u4         thread serial number
 223  *
 224  *               HPROF_GC_ROOT_STICKY_CLASS    System class
 225  *
 226  *                          id         object ID
 227  *
 228  *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
 229  *
 230  *                          id         object ID
 231  *                          u4         thread serial number
 232  *
 233  *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
 234  *
 235  *                          id         object ID
 236  *
 237  *               HPROF_GC_CLASS_DUMP           dump of a class object
 238  *
 239  *                          id         class object ID
 240  *                          u4         stack trace serial number
 241  *                          id         super class object ID
 242  *                          id         class loader object ID
 243  *                          id         signers object ID
 244  *                          id         protection domain object ID
 245  *                          id         reserved
 246  *                          id         reserved
 247  *
 248  *                          u4         instance size (in bytes)
 249  *
 250  *                          u2         size of constant pool
 251  *                          [u2,       constant pool index,
 252  *                           ty,       type
 253  *                                     2:  object
 254  *                                     4:  boolean
 255  *                                     5:  char
 256  *                                     6:  float
 257  *                                     7:  double
 258  *                                     8:  byte
 259  *                                     9:  short
 260  *                                     10: int
 261  *                                     11: long
 262  *                           vl]*      and value
 263  *
 264  *                          u2         number of static fields
 265  *                          [id,       static field name,
 266  *                           ty,       type,
 267  *                           vl]*      and value
 268  *
 269  *                          u2         number of inst. fields (not inc. super)
 270  *                          [id,       instance field name,
 271  *                           ty]*      type
 272  *
 273  *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
 274  *
 275  *                          id         object ID
 276  *                          u4         stack trace serial number
 277  *                          id         class object ID
 278  *                          u4         number of bytes that follow
 279  *                          [vl]*      instance field values (class, followed
 280  *                                     by super, super's super ...)
 281  *
 282  *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
 283  *
 284  *                          id         array object ID
 285  *                          u4         stack trace serial number
 286  *                          u4         number of elements
 287  *                          id         array class ID
 288  *                          [id]*      elements
 289  *
 290  *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
 291  *
 292  *                          id         array object ID
 293  *                          u4         stack trace serial number
 294  *                          u4         number of elements
 295  *                          u1         element type
 296  *                                     4:  boolean array
 297  *                                     5:  char array
 298  *                                     6:  float array
 299  *                                     7:  double array
 300  *                                     8:  byte array
 301  *                                     9:  short array
 302  *                                     10: int array
 303  *                                     11: long array
 304  *                          [u1]*      elements
 305  *
 306  * HPROF_CPU_SAMPLES        a set of sample traces of running threads
 307  *
 308  *                u4        total number of samples
 309  *                u4        # of traces
 310  *               [u4        # of samples
 311  *                u4]*      stack trace serial number
 312  *
 313  * HPROF_CONTROL_SETTINGS   the settings of on/off switches
 314  *
 315  *                u4        0x00000001: alloc traces on/off
 316  *                          0x00000002: cpu sampling on/off
 317  *                u2        stack trace depth
 318  *
 319  *
 320  * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
 321  * be generated as a sequence of heap dump segments. This sequence is
 322  * terminated by an end record. The additional tags allowed by format
 323  * "JAVA PROFILE 1.0.2" are:
 324  *
 325  * HPROF_HEAP_DUMP_SEGMENT  denote a heap dump segment
 326  *
 327  *               [heap dump sub-records]*
 328  *               The same sub-record types allowed by HPROF_HEAP_DUMP
 329  *
 330  * HPROF_HEAP_DUMP_END      denotes the end of a heap dump
 331  *
 332  */
 333 
 334 
 335 // HPROF tags
 336 
 337 enum hprofTag : u1 {
 338   // top-level records
 339   HPROF_UTF8                    = 0x01,
 340   HPROF_LOAD_CLASS              = 0x02,
 341   HPROF_UNLOAD_CLASS            = 0x03,
 342   HPROF_FRAME                   = 0x04,
 343   HPROF_TRACE                   = 0x05,
 344   HPROF_ALLOC_SITES             = 0x06,
 345   HPROF_HEAP_SUMMARY            = 0x07,
 346   HPROF_START_THREAD            = 0x0A,
 347   HPROF_END_THREAD              = 0x0B,
 348   HPROF_HEAP_DUMP               = 0x0C,
 349   HPROF_CPU_SAMPLES             = 0x0D,
 350   HPROF_CONTROL_SETTINGS        = 0x0E,
 351 
 352   // 1.0.2 record types
 353   HPROF_HEAP_DUMP_SEGMENT       = 0x1C,
 354   HPROF_HEAP_DUMP_END           = 0x2C,
 355 
 356   // field types
 357   HPROF_ARRAY_OBJECT            = 0x01,
 358   HPROF_NORMAL_OBJECT           = 0x02,
 359   HPROF_BOOLEAN                 = 0x04,
 360   HPROF_CHAR                    = 0x05,
 361   HPROF_FLOAT                   = 0x06,
 362   HPROF_DOUBLE                  = 0x07,
 363   HPROF_BYTE                    = 0x08,
 364   HPROF_SHORT                   = 0x09,
 365   HPROF_INT                     = 0x0A,
 366   HPROF_LONG                    = 0x0B,
 367 
 368   // data-dump sub-records
 369   HPROF_GC_ROOT_UNKNOWN         = 0xFF,
 370   HPROF_GC_ROOT_JNI_GLOBAL      = 0x01,
 371   HPROF_GC_ROOT_JNI_LOCAL       = 0x02,
 372   HPROF_GC_ROOT_JAVA_FRAME      = 0x03,
 373   HPROF_GC_ROOT_NATIVE_STACK    = 0x04,
 374   HPROF_GC_ROOT_STICKY_CLASS    = 0x05,
 375   HPROF_GC_ROOT_THREAD_BLOCK    = 0x06,
 376   HPROF_GC_ROOT_MONITOR_USED    = 0x07,
 377   HPROF_GC_ROOT_THREAD_OBJ      = 0x08,
 378   HPROF_GC_CLASS_DUMP           = 0x20,
 379   HPROF_GC_INSTANCE_DUMP        = 0x21,
 380   HPROF_GC_OBJ_ARRAY_DUMP       = 0x22,
 381   HPROF_GC_PRIM_ARRAY_DUMP      = 0x23
 382 };
 383 
 384 // Default stack trace ID (used for dummy HPROF_TRACE record)
 385 enum {
 386   STACK_TRACE_ID = 1,
 387   INITIAL_CLASS_COUNT = 200
 388 };
 389 
 390 // Supports I/O operations for a dump
 391 // Base class for dump and parallel dump
 392 class AbstractDumpWriter : public CHeapObj<mtInternal> {
 393  protected:
 394   enum {
 395     io_buffer_max_size = 1*M,
 396     dump_segment_header_size = 9
 397   };
 398 
 399   char* _buffer;    // internal buffer
 400   size_t _size;
 401   size_t _pos;
 402 
 403   bool _in_dump_segment; // Are we currently in a dump segment?
 404   bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
 405   DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
 406   DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
 407 
 408   char* buffer() const                          { return _buffer; }
 409   size_t buffer_size() const                    { return _size; }
 410   void set_position(size_t pos)                 { _pos = pos; }
 411 
 412   // Can be called if we have enough room in the buffer.
 413   void write_fast(const void* s, size_t len);
 414 
 415   // Returns true if we have enough room in the buffer for 'len' bytes.
 416   bool can_write_fast(size_t len);
 417 
 418   void write_address(address a);
 419 
 420  public:
 421   AbstractDumpWriter() :
 422     _buffer(nullptr),
 423     _size(io_buffer_max_size),
 424     _pos(0),
 425     _in_dump_segment(false) { }
 426 
 427   // Total number of bytes written to the disk
 428   virtual julong bytes_written() const = 0;
 429   // Return non-null if error occurred
 430   virtual char const* error() const = 0;
 431 
 432   size_t position() const                       { return _pos; }
 433   // writer functions
 434   virtual void write_raw(const void* s, size_t len);
 435   void write_u1(u1 x);
 436   void write_u2(u2 x);
 437   void write_u4(u4 x);
 438   void write_u8(u8 x);
 439   void write_objectID(oop o);
 440   void write_rootID(oop* p);
 441   void write_symbolID(Symbol* o);
 442   void write_classID(Klass* k);
 443   void write_id(u4 x);
 444 
 445   // Start a new sub-record. Starts a new heap dump segment if needed.
 446   void start_sub_record(u1 tag, u4 len);
 447   // Ends the current sub-record.
 448   void end_sub_record();
 449   // Finishes the current dump segment if not already finished.
 450   void finish_dump_segment();
 451   // Flush internal buffer to persistent storage
 452   virtual void flush() = 0;
 453 };
 454 
 455 void AbstractDumpWriter::write_fast(const void* s, size_t len) {
 456   assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
 457   assert(buffer_size() - position() >= len, "Must fit");
 458   debug_only(_sub_record_left -= len);
 459   memcpy(buffer() + position(), s, len);
 460   set_position(position() + len);
 461 }
 462 
 463 bool AbstractDumpWriter::can_write_fast(size_t len) {
 464   return buffer_size() - position() >= len;
 465 }
 466 
 467 // write raw bytes
 468 void AbstractDumpWriter::write_raw(const void* s, size_t len) {
 469   assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
 470   debug_only(_sub_record_left -= len);
 471 
 472   // flush buffer to make room.
 473   while (len > buffer_size() - position()) {
 474     assert(!_in_dump_segment || _is_huge_sub_record,
 475            "Cannot overflow in non-huge sub-record.");
 476     size_t to_write = buffer_size() - position();
 477     memcpy(buffer() + position(), s, to_write);
 478     s = (void*) ((char*) s + to_write);
 479     len -= to_write;
 480     set_position(position() + to_write);
 481     flush();
 482   }
 483 
 484   memcpy(buffer() + position(), s, len);
 485   set_position(position() + len);
 486 }
 487 
 488 // Makes sure we inline the fast write into the write_u* functions. This is a big speedup.
 489 #define WRITE_KNOWN_TYPE(p, len) do { if (can_write_fast((len))) write_fast((p), (len)); \
 490                                       else write_raw((p), (len)); } while (0)
 491 
 492 void AbstractDumpWriter::write_u1(u1 x) {
 493   WRITE_KNOWN_TYPE(&x, 1);
 494 }
 495 
 496 void AbstractDumpWriter::write_u2(u2 x) {
 497   u2 v;
 498   Bytes::put_Java_u2((address)&v, x);
 499   WRITE_KNOWN_TYPE(&v, 2);
 500 }
 501 
 502 void AbstractDumpWriter::write_u4(u4 x) {
 503   u4 v;
 504   Bytes::put_Java_u4((address)&v, x);
 505   WRITE_KNOWN_TYPE(&v, 4);
 506 }
 507 
 508 void AbstractDumpWriter::write_u8(u8 x) {
 509   u8 v;
 510   Bytes::put_Java_u8((address)&v, x);
 511   WRITE_KNOWN_TYPE(&v, 8);
 512 }
 513 
 514 void AbstractDumpWriter::write_address(address a) {
 515 #ifdef _LP64
 516   write_u8((u8)a);
 517 #else
 518   write_u4((u4)a);
 519 #endif
 520 }
 521 
 522 void AbstractDumpWriter::write_objectID(oop o) {
 523   write_address(cast_from_oop<address>(o));
 524 }
 525 
 526 void AbstractDumpWriter::write_rootID(oop* p) {
 527   write_address((address)p);
 528 }
 529 
 530 void AbstractDumpWriter::write_symbolID(Symbol* s) {
 531   write_address((address)((uintptr_t)s));
 532 }
 533 
 534 void AbstractDumpWriter::write_id(u4 x) {
 535 #ifdef _LP64
 536   write_u8((u8) x);
 537 #else
 538   write_u4(x);
 539 #endif
 540 }
 541 
 542 // We use java mirror as the class ID
 543 void AbstractDumpWriter::write_classID(Klass* k) {
 544   write_objectID(k->java_mirror());
 545 }
 546 
 547 void AbstractDumpWriter::finish_dump_segment() {
 548   if (_in_dump_segment) {
 549     assert(_sub_record_left == 0, "Last sub-record not written completely");
 550     assert(_sub_record_ended, "sub-record must have ended");
 551 
 552     // Fix up the dump segment length if we haven't written a huge sub-record last
 553     // (in which case the segment length was already set to the correct value initially).
 554     if (!_is_huge_sub_record) {
 555       assert(position() > dump_segment_header_size, "Dump segment should have some content");
 556       Bytes::put_Java_u4((address) (buffer() + 5),
 557                          (u4) (position() - dump_segment_header_size));
 558     } else {
 559       // Finish process huge sub record
 560       // Set _is_huge_sub_record to false so the parallel dump writer can flush data to file.
 561       _is_huge_sub_record = false;
 562     }
 563 
 564     _in_dump_segment = false;
 565     flush();
 566   }
 567 }
 568 
 569 void AbstractDumpWriter::start_sub_record(u1 tag, u4 len) {
 570   if (!_in_dump_segment) {
 571     if (position() > 0) {
 572       flush();
 573     }
 574 
 575     assert(position() == 0 && buffer_size() > dump_segment_header_size, "Must be at the start");
 576 
 577     write_u1(HPROF_HEAP_DUMP_SEGMENT);
 578     write_u4(0); // timestamp
 579     // Will be fixed up later if we add more sub-records.  If this is a huge sub-record,
 580     // this is already the correct length, since we don't add more sub-records.
 581     write_u4(len);
 582     assert(Bytes::get_Java_u4((address)(buffer() + 5)) == len, "Inconsistent size!");
 583     _in_dump_segment = true;
 584     _is_huge_sub_record = len > buffer_size() - dump_segment_header_size;
 585   } else if (_is_huge_sub_record || (len > buffer_size() - position())) {
 586     // This object will not fit in completely or the last sub-record was huge.
 587     // Finish the current segment and try again.
 588     finish_dump_segment();
 589     start_sub_record(tag, len);
 590 
 591     return;
 592   }
 593 
 594   debug_only(_sub_record_left = len);
 595   debug_only(_sub_record_ended = false);
 596 
 597   write_u1(tag);
 598 }
 599 
 600 void AbstractDumpWriter::end_sub_record() {
 601   assert(_in_dump_segment, "must be in dump segment");
 602   assert(_sub_record_left == 0, "sub-record not written completely");
 603   assert(!_sub_record_ended, "Must not have ended yet");
 604   debug_only(_sub_record_ended = true);
 605 }
 606 
 607 // Supports I/O operations for a dump
 608 
 609 class DumpWriter : public AbstractDumpWriter {
 610 private:
 611   FileWriter* _writer;
 612   AbstractCompressor* _compressor;
 613   size_t _bytes_written;
 614   char* _error;
 615   // Compression support
 616   char* _out_buffer;
 617   size_t _out_size;
 618   size_t _out_pos;
 619   char* _tmp_buffer;
 620   size_t _tmp_size;
 621 
 622 private:
 623   void do_compress();
 624 
 625 public:
 626   DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor);
 627   ~DumpWriter();
 628   julong bytes_written() const override        { return (julong) _bytes_written; }
 629   char const* error() const override           { return _error; }
 630   void set_error(const char* error)            { _error = (char*)error; }
 631   bool has_error() const                       { return _error != nullptr; }
 632   const char* get_file_path() const            { return _writer->get_file_path(); }
 633   AbstractCompressor* compressor()             { return _compressor; }
 634   bool is_overwrite() const                    { return _writer->is_overwrite(); }
 635 
 636   void flush() override;
 637 
 638 private:
 639   // internals for DumpMerger
 640   friend class DumpMerger;
 641   void set_bytes_written(julong bytes_written) { _bytes_written = bytes_written; }
 642   int get_fd() const                           { return _writer->get_fd(); }
 643   void set_compressor(AbstractCompressor* p)   { _compressor = p; }
 644 };
 645 
 646 DumpWriter::DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor) :
 647   AbstractDumpWriter(),
 648   _writer(new (std::nothrow) FileWriter(path, overwrite)),
 649   _compressor(compressor),
 650   _bytes_written(0),
 651   _error(nullptr),
 652   _out_buffer(nullptr),
 653   _out_size(0),
 654   _out_pos(0),
 655   _tmp_buffer(nullptr),
 656   _tmp_size(0) {
 657   _error = (char*)_writer->open_writer();
 658   if (_error == nullptr) {
 659     _buffer = (char*)os::malloc(io_buffer_max_size, mtInternal);
 660     if (compressor != nullptr) {
 661       _error = (char*)_compressor->init(io_buffer_max_size, &_out_size, &_tmp_size);
 662       if (_error == nullptr) {
 663         if (_out_size > 0) {
 664           _out_buffer = (char*)os::malloc(_out_size, mtInternal);
 665         }
 666         if (_tmp_size > 0) {
 667           _tmp_buffer = (char*)os::malloc(_tmp_size, mtInternal);
 668         }
 669       }
 670     }
 671   }
 672   // initialize internal buffer
 673   _pos = 0;
 674   _size = io_buffer_max_size;
 675 }
 676 
 677 DumpWriter::~DumpWriter(){
 678   if (_buffer != nullptr) {
 679     os::free(_buffer);
 680   }
 681   if (_out_buffer != nullptr) {
 682     os::free(_out_buffer);
 683   }
 684   if (_tmp_buffer != nullptr) {
 685     os::free(_tmp_buffer);
 686   }
 687   if (_writer != nullptr) {
 688     delete _writer;
 689   }
 690   _bytes_written = -1;
 691 }
 692 
 693 // flush any buffered bytes to the file
 694 void DumpWriter::flush() {
 695   if (_pos <= 0) {
 696     return;
 697   }
 698   if (has_error()) {
 699     _pos = 0;
 700     return;
 701   }
 702   char* result = nullptr;
 703   if (_compressor == nullptr) {
 704     result = (char*)_writer->write_buf(_buffer, _pos);
 705     _bytes_written += _pos;
 706   } else {
 707     do_compress();
 708     if (!has_error()) {
 709       result = (char*)_writer->write_buf(_out_buffer, _out_pos);
 710       _bytes_written += _out_pos;
 711     }
 712   }
 713   _pos = 0; // reset pos to make internal buffer available
 714 
 715   if (result != nullptr) {
 716     set_error(result);
 717   }
 718 }
 719 
 720 void DumpWriter::do_compress() {
 721   const char* msg = _compressor->compress(_buffer, _pos, _out_buffer, _out_size,
 722                                           _tmp_buffer, _tmp_size, &_out_pos);
 723 
 724   if (msg != nullptr) {
 725     set_error(msg);
 726   }
 727 }
 728 
 729 class DumperClassCacheTable;
 730 class DumperClassCacheTableEntry;
 731 
 732 // Support class with a collection of functions used when dumping the heap
 733 class DumperSupport : AllStatic {
 734  public:
 735 
 736   // write a header of the given type
 737   static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
 738 
 739   // returns hprof tag for the given type signature
 740   static hprofTag sig2tag(Symbol* sig);
 741   // returns hprof tag for the given basic type
 742   static hprofTag type2tag(BasicType type);
 743   // Returns the size of the data to write.
 744   static u4 sig2size(Symbol* sig);
 745 
 746   // returns the size of the instance of the given class
 747   static u4 instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry = nullptr);
 748 
 749   // dump a jfloat
 750   static void dump_float(AbstractDumpWriter* writer, jfloat f);
 751   // dump a jdouble
 752   static void dump_double(AbstractDumpWriter* writer, jdouble d);
 753   // dumps the raw value of the given field
 754   static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
 755   // returns the size of the static fields; also counts the static fields
 756   static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
 757   // dumps static fields of the given class
 758   static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
 759   // dump the raw values of the instance fields of the given object
 760   static void dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry);
 761   // get the count of the instance fields for a given class
 762   static u2 get_instance_fields_count(InstanceKlass* ik);
 763   // dumps the definition of the instance fields for a given class
 764   static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
 765   // creates HPROF_GC_INSTANCE_DUMP record for the given object
 766   static void dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache);
 767   // creates HPROF_GC_CLASS_DUMP record for the given instance class
 768   static void dump_instance_class(AbstractDumpWriter* writer, Klass* k);
 769   // creates HPROF_GC_CLASS_DUMP record for a given array class
 770   static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
 771 
 772   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
 773   static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
 774   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
 775   static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
 776   // create HPROF_FRAME record for the given method and bci
 777   static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
 778 
 779   // check if we need to truncate an array
 780   static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
 781 
 782   // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
 783   static void end_of_dump(AbstractDumpWriter* writer);
 784 
 785   static oop mask_dormant_archived_object(oop o, oop ref_obj) {
 786     if (o != nullptr && o->klass()->java_mirror_no_keepalive() == nullptr) {
 787       // Ignore this object since the corresponding java mirror is not loaded.
 788       // Might be a dormant archive object.
 789       report_dormant_archived_object(o, ref_obj);
 790       return nullptr;
 791     } else {
 792       return o;
 793     }
 794   }
 795 
 796   static void report_dormant_archived_object(oop o, oop ref_obj) {
 797     if (log_is_enabled(Trace, cds, heap)) {
 798       ResourceMark rm;
 799       if (ref_obj != nullptr) {
 800         log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
 801                   p2i(o), o->klass()->external_name(),
 802                   p2i(ref_obj), ref_obj->klass()->external_name());
 803       } else {
 804         log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)",
 805                   p2i(o), o->klass()->external_name());
 806       }
 807     }
 808   }
 809 };
 810 
 811 // Hash table of klasses to the klass metadata. This should greatly improve the
 812 // hash dumping performance. This hash table is supposed to be used by a single
 813 // thread only.
 814 //
 815 class DumperClassCacheTableEntry : public CHeapObj<mtServiceability> {
 816   friend class DumperClassCacheTable;
 817 private:
 818   GrowableArray<char> _sigs_start;
 819   GrowableArray<int> _offsets;
 820   u4 _instance_size;
 821   int _entries;
 822 
 823 public:
 824   DumperClassCacheTableEntry() : _instance_size(0), _entries(0) {};
 825 
 826   int field_count()             { return _entries; }
 827   char sig_start(int field_idx) { return _sigs_start.at(field_idx); }
 828   int offset(int field_idx)     { return _offsets.at(field_idx); }
 829   u4 instance_size()            { return _instance_size; }
 830 };
 831 
 832 class DumperClassCacheTable {
 833 private:
 834   // ResourceHashtable SIZE is specified at compile time so we
 835   // use 1031 which is the first prime after 1024.
 836   static constexpr size_t TABLE_SIZE = 1031;
 837 
 838   // Maintain the cache for N classes. This limits memory footprint
 839   // impact, regardless of how many classes we have in the dump.
 840   // This also improves look up performance by keeping the statically
 841   // sized table from overloading.
 842   static constexpr int CACHE_TOP = 256;
 843 
 844   typedef ResourceHashtable<InstanceKlass*, DumperClassCacheTableEntry*,
 845                             TABLE_SIZE, AnyObj::C_HEAP, mtServiceability> PtrTable;
 846   PtrTable* _ptrs;
 847 
 848   // Single-slot cache to handle the major case of objects of the same
 849   // class back-to-back, e.g. from T[].
 850   InstanceKlass* _last_ik;
 851   DumperClassCacheTableEntry* _last_entry;
 852 
 853   void unlink_all(PtrTable* table) {
 854     class CleanupEntry: StackObj {
 855     public:
 856       bool do_entry(InstanceKlass*& key, DumperClassCacheTableEntry*& entry) {
 857         delete entry;
 858         return true;
 859       }
 860     } cleanup;
 861     table->unlink(&cleanup);
 862   }
 863 
 864 public:
 865   DumperClassCacheTableEntry* lookup_or_create(InstanceKlass* ik) {
 866     if (_last_ik == ik) {
 867       return _last_entry;
 868     }
 869 
 870     DumperClassCacheTableEntry* entry;
 871     DumperClassCacheTableEntry** from_cache = _ptrs->get(ik);
 872     if (from_cache == nullptr) {
 873       entry = new DumperClassCacheTableEntry();
 874       for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
 875         if (!fld.access_flags().is_static()) {
 876           Symbol* sig = fld.signature();
 877           entry->_sigs_start.push(sig->char_at(0));
 878           entry->_offsets.push(fld.offset());
 879           entry->_entries++;
 880           entry->_instance_size += DumperSupport::sig2size(sig);
 881         }
 882       }
 883 
 884       if (_ptrs->number_of_entries() >= CACHE_TOP) {
 885         // We do not track the individual hit rates for table entries.
 886         // Purge the entire table, and let the cache catch up with new
 887         // distribution.
 888         unlink_all(_ptrs);
 889       }
 890 
 891       _ptrs->put(ik, entry);
 892     } else {
 893       entry = *from_cache;
 894     }
 895 
 896     // Remember for single-slot cache.
 897     _last_ik = ik;
 898     _last_entry = entry;
 899 
 900     return entry;
 901   }
 902 
 903   DumperClassCacheTable() : _ptrs(new (mtServiceability) PtrTable), _last_ik(nullptr), _last_entry(nullptr) {}
 904 
 905   ~DumperClassCacheTable() {
 906     unlink_all(_ptrs);
 907     delete _ptrs;
 908   }
 909 };
 910 
 911 // write a header of the given type
 912 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
 913   writer->write_u1(tag);
 914   writer->write_u4(0);                  // current ticks
 915   writer->write_u4(len);
 916 }
 917 
 918 // returns hprof tag for the given type signature
 919 hprofTag DumperSupport::sig2tag(Symbol* sig) {
 920   switch (sig->char_at(0)) {
 921     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;
 922     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
 923     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
 924     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
 925     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
 926     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
 927     case JVM_SIGNATURE_INT      : return HPROF_INT;
 928     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
 929     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
 930     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
 931     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 932   }
 933 }
 934 
 935 hprofTag DumperSupport::type2tag(BasicType type) {
 936   switch (type) {
 937     case T_BYTE     : return HPROF_BYTE;
 938     case T_CHAR     : return HPROF_CHAR;
 939     case T_FLOAT    : return HPROF_FLOAT;
 940     case T_DOUBLE   : return HPROF_DOUBLE;
 941     case T_INT      : return HPROF_INT;
 942     case T_LONG     : return HPROF_LONG;
 943     case T_SHORT    : return HPROF_SHORT;
 944     case T_BOOLEAN  : return HPROF_BOOLEAN;
 945     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 946   }
 947 }
 948 
 949 u4 DumperSupport::sig2size(Symbol* sig) {
 950   switch (sig->char_at(0)) {
 951     case JVM_SIGNATURE_CLASS:
 952     case JVM_SIGNATURE_ARRAY: return sizeof(address);
 953     case JVM_SIGNATURE_BOOLEAN:
 954     case JVM_SIGNATURE_BYTE: return 1;
 955     case JVM_SIGNATURE_SHORT:
 956     case JVM_SIGNATURE_CHAR: return 2;
 957     case JVM_SIGNATURE_INT:
 958     case JVM_SIGNATURE_FLOAT: return 4;
 959     case JVM_SIGNATURE_LONG:
 960     case JVM_SIGNATURE_DOUBLE: return 8;
 961     default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
 962   }
 963 }
 964 
 965 template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
 966   T to;
 967   static_assert(sizeof(to) == sizeof(from), "must be of the same size");
 968   memcpy(&to, &from, sizeof(to));
 969   return to;
 970 }
 971 
 972 // dump a jfloat
 973 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
 974   if (g_isnan(f)) {
 975     writer->write_u4(0x7fc00000); // collapsing NaNs
 976   } else {
 977     writer->write_u4(bit_cast<u4>(f));
 978   }
 979 }
 980 
 981 // dump a jdouble
 982 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
 983   if (g_isnan(d)) {
 984     writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
 985   } else {
 986     writer->write_u8(bit_cast<u8>(d));
 987   }
 988 }
 989 
 990 // dumps the raw value of the given field
 991 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
 992   switch (type) {
 993     case JVM_SIGNATURE_CLASS :
 994     case JVM_SIGNATURE_ARRAY : {
 995       oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
 996       o = mask_dormant_archived_object(o, obj);
 997       assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
 998       writer->write_objectID(o);
 999       break;
1000     }
1001     case JVM_SIGNATURE_BYTE : {
1002       jbyte b = obj->byte_field(offset);
1003       writer->write_u1(b);
1004       break;
1005     }
1006     case JVM_SIGNATURE_CHAR : {
1007       jchar c = obj->char_field(offset);
1008       writer->write_u2(c);
1009       break;
1010     }
1011     case JVM_SIGNATURE_SHORT : {
1012       jshort s = obj->short_field(offset);
1013       writer->write_u2(s);
1014       break;
1015     }
1016     case JVM_SIGNATURE_FLOAT : {
1017       jfloat f = obj->float_field(offset);
1018       dump_float(writer, f);
1019       break;
1020     }
1021     case JVM_SIGNATURE_DOUBLE : {
1022       jdouble d = obj->double_field(offset);
1023       dump_double(writer, d);
1024       break;
1025     }
1026     case JVM_SIGNATURE_INT : {
1027       jint i = obj->int_field(offset);
1028       writer->write_u4(i);
1029       break;
1030     }
1031     case JVM_SIGNATURE_LONG : {
1032       jlong l = obj->long_field(offset);
1033       writer->write_u8(l);
1034       break;
1035     }
1036     case JVM_SIGNATURE_BOOLEAN : {
1037       jboolean b = obj->bool_field(offset);
1038       writer->write_u1(b);
1039       break;
1040     }
1041     default : {
1042       ShouldNotReachHere();
1043       break;
1044     }
1045   }
1046 }
1047 
1048 // returns the size of the instance of the given class
1049 u4 DumperSupport::instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry) {
1050   if (class_cache_entry != nullptr) {
1051     return class_cache_entry->instance_size();
1052   } else {
1053     u4 size = 0;
1054     for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1055       if (!fld.access_flags().is_static()) {
1056         size += sig2size(fld.signature());
1057       }
1058     }
1059     return size;
1060   }
1061 }
1062 
1063 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1064   field_count = 0;
1065   u4 size = 0;
1066 
1067   for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1068     if (fldc.access_flags().is_static()) {
1069       field_count++;
1070       size += sig2size(fldc.signature());
1071     }
1072   }
1073 
1074   // Add in resolved_references which is referenced by the cpCache
1075   // The resolved_references is an array per InstanceKlass holding the
1076   // strings and other oops resolved from the constant pool.
1077   oop resolved_references = ik->constants()->resolved_references_or_null();
1078   if (resolved_references != nullptr) {
1079     field_count++;
1080     size += sizeof(address);
1081 
1082     // Add in the resolved_references of the used previous versions of the class
1083     // in the case of RedefineClasses
1084     InstanceKlass* prev = ik->previous_versions();
1085     while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1086       field_count++;
1087       size += sizeof(address);
1088       prev = prev->previous_versions();
1089     }
1090   }
1091 
1092   // Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
1093   // arrays.
1094   oop init_lock = ik->init_lock();
1095   if (init_lock != nullptr) {
1096     field_count++;
1097     size += sizeof(address);
1098   }
1099 
1100   // We write the value itself plus a name and a one byte type tag per field.
1101   return checked_cast<u4>(size + field_count * (sizeof(address) + 1));
1102 }
1103 
1104 // dumps static fields of the given class
1105 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1106   InstanceKlass* ik = InstanceKlass::cast(k);
1107 
1108   // dump the field descriptors and raw values
1109   for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1110     if (fld.access_flags().is_static()) {
1111       Symbol* sig = fld.signature();
1112 
1113       writer->write_symbolID(fld.name());   // name
1114       writer->write_u1(sig2tag(sig));       // type
1115 
1116       // value
1117       dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1118     }
1119   }
1120 
1121   // Add resolved_references for each class that has them
1122   oop resolved_references = ik->constants()->resolved_references_or_null();
1123   if (resolved_references != nullptr) {
1124     writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1125     writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1126     writer->write_objectID(resolved_references);
1127 
1128     // Also write any previous versions
1129     InstanceKlass* prev = ik->previous_versions();
1130     while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1131       writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1132       writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1133       writer->write_objectID(prev->constants()->resolved_references());
1134       prev = prev->previous_versions();
1135     }
1136   }
1137 
1138   // Add init lock to the end if the class is not yet initialized
1139   oop init_lock = ik->init_lock();
1140   if (init_lock != nullptr) {
1141     writer->write_symbolID(vmSymbols::init_lock_name());         // name
1142     writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1143     writer->write_objectID(init_lock);
1144   }
1145 }
1146 
1147 // dump the raw values of the instance fields of the given object
1148 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry) {
1149   assert(class_cache_entry != nullptr, "Pre-condition: must be provided");
1150   for (int idx = 0; idx < class_cache_entry->field_count(); idx++) {
1151     dump_field_value(writer, class_cache_entry->sig_start(idx), o, class_cache_entry->offset(idx));
1152   }
1153 }
1154 
1155 // dumps the definition of the instance fields for a given class
1156 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1157   u2 field_count = 0;
1158 
1159   for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1160     if (!fldc.access_flags().is_static()) field_count++;
1161   }
1162 
1163   return field_count;
1164 }
1165 
1166 // dumps the definition of the instance fields for a given class
1167 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1168   InstanceKlass* ik = InstanceKlass::cast(k);
1169 
1170   // dump the field descriptors
1171   for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1172     if (!fld.access_flags().is_static()) {
1173       Symbol* sig = fld.signature();
1174 
1175       writer->write_symbolID(fld.name());   // name
1176       writer->write_u1(sig2tag(sig));       // type
1177     }
1178   }
1179 }
1180 
1181 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1182 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache) {
1183   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1184 
1185   DumperClassCacheTableEntry* cache_entry = class_cache->lookup_or_create(ik);
1186 
1187   u4 is = instance_size(ik, cache_entry);
1188   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1189 
1190   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1191   writer->write_objectID(o);
1192   writer->write_u4(STACK_TRACE_ID);
1193 
1194   // class ID
1195   writer->write_classID(ik);
1196 
1197   // number of bytes that follow
1198   writer->write_u4(is);
1199 
1200   // field values
1201   dump_instance_fields(writer, o, cache_entry);
1202 
1203   writer->end_sub_record();
1204 }
1205 
1206 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1207 void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, Klass* k) {
1208   InstanceKlass* ik = InstanceKlass::cast(k);
1209 
1210   // We can safepoint and do a heap dump at a point where we have a Klass,
1211   // but no java mirror class has been setup for it. So we need to check
1212   // that the class is at least loaded, to avoid crash from a null mirror.
1213   if (!ik->is_loaded()) {
1214     return;
1215   }
1216 
1217   u2 static_fields_count = 0;
1218   u4 static_size = get_static_fields_size(ik, static_fields_count);
1219   u2 instance_fields_count = get_instance_fields_count(ik);
1220   u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1221   u4 size = checked_cast<u4>(1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size);
1222 
1223   writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1224 
1225   // class ID
1226   writer->write_classID(ik);
1227   writer->write_u4(STACK_TRACE_ID);
1228 
1229   // super class ID
1230   InstanceKlass* java_super = ik->java_super();
1231   if (java_super == nullptr) {
1232     writer->write_objectID(oop(nullptr));
1233   } else {
1234     writer->write_classID(java_super);
1235   }
1236 
1237   writer->write_objectID(ik->class_loader());
1238   writer->write_objectID(ik->signers());
1239   writer->write_objectID(ik->protection_domain());
1240 
1241   // reserved
1242   writer->write_objectID(oop(nullptr));
1243   writer->write_objectID(oop(nullptr));
1244 
1245   // instance size
1246   writer->write_u4(DumperSupport::instance_size(ik));
1247 
1248   // size of constant pool - ignored by HAT 1.1
1249   writer->write_u2(0);
1250 
1251   // static fields
1252   writer->write_u2(static_fields_count);
1253   dump_static_fields(writer, ik);
1254 
1255   // description of instance fields
1256   writer->write_u2(instance_fields_count);
1257   dump_instance_field_descriptors(writer, ik);
1258 
1259   writer->end_sub_record();
1260 }
1261 
1262 // creates HPROF_GC_CLASS_DUMP record for the given array class
1263 void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1264   InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1265   if (k->is_objArray_klass()) {
1266     Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1267     assert(bk != nullptr, "checking");
1268     if (bk->is_instance_klass()) {
1269       ik = InstanceKlass::cast(bk);
1270     }
1271   }
1272 
1273   u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1274   writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1275   writer->write_classID(k);
1276   writer->write_u4(STACK_TRACE_ID);
1277 
1278   // super class of array classes is java.lang.Object
1279   InstanceKlass* java_super = k->java_super();
1280   assert(java_super != nullptr, "checking");
1281   writer->write_classID(java_super);
1282 
1283   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1284   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1285   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1286 
1287   writer->write_objectID(oop(nullptr));    // reserved
1288   writer->write_objectID(oop(nullptr));
1289   writer->write_u4(0);             // instance size
1290   writer->write_u2(0);             // constant pool
1291   writer->write_u2(0);             // static fields
1292   writer->write_u2(0);             // instance fields
1293 
1294   writer->end_sub_record();
1295 
1296 }
1297 
1298 // Hprof uses an u4 as record length field,
1299 // which means we need to truncate arrays that are too long.
1300 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1301   BasicType type = ArrayKlass::cast(array->klass())->element_type();
1302   assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1303 
1304   int length = array->length();
1305 
1306   int type_size;
1307   if (type == T_OBJECT) {
1308     type_size = sizeof(address);
1309   } else {
1310     type_size = type2aelembytes(type);
1311   }
1312 
1313   size_t length_in_bytes = (size_t)length * type_size;
1314   uint max_bytes = max_juint - header_size;
1315 
1316   if (length_in_bytes > max_bytes) {
1317     length = max_bytes / type_size;
1318     length_in_bytes = (size_t)length * type_size;
1319 
1320     warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1321             type2name_tab[type], array->length(), length);
1322   }
1323   return length;
1324 }
1325 
1326 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1327 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1328   // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1329   short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1330   int length = calculate_array_max_length(writer, array, header_size);
1331   u4 size = checked_cast<u4>(header_size + length * sizeof(address));
1332 
1333   writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1334   writer->write_objectID(array);
1335   writer->write_u4(STACK_TRACE_ID);
1336   writer->write_u4(length);
1337 
1338   // array class ID
1339   writer->write_classID(array->klass());
1340 
1341   // [id]* elements
1342   for (int index = 0; index < length; index++) {
1343     oop o = array->obj_at(index);
1344     o = mask_dormant_archived_object(o, array);
1345     writer->write_objectID(o);
1346   }
1347 
1348   writer->end_sub_record();
1349 }
1350 
1351 #define WRITE_ARRAY(Array, Type, Size, Length) \
1352   for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1353 
1354 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1355 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1356   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1357   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1358   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1359 
1360   int length = calculate_array_max_length(writer, array, header_size);
1361   int type_size = type2aelembytes(type);
1362   u4 length_in_bytes = (u4)length * type_size;
1363   u4 size = header_size + length_in_bytes;
1364 
1365   writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1366   writer->write_objectID(array);
1367   writer->write_u4(STACK_TRACE_ID);
1368   writer->write_u4(length);
1369   writer->write_u1(type2tag(type));
1370 
1371   // nothing to copy
1372   if (length == 0) {
1373     writer->end_sub_record();
1374     return;
1375   }
1376 
1377   // If the byte ordering is big endian then we can copy most types directly
1378 
1379   switch (type) {
1380     case T_INT : {
1381       if (Endian::is_Java_byte_ordering_different()) {
1382         WRITE_ARRAY(array, int, u4, length);
1383       } else {
1384         writer->write_raw(array->int_at_addr(0), length_in_bytes);
1385       }
1386       break;
1387     }
1388     case T_BYTE : {
1389       writer->write_raw(array->byte_at_addr(0), length_in_bytes);
1390       break;
1391     }
1392     case T_CHAR : {
1393       if (Endian::is_Java_byte_ordering_different()) {
1394         WRITE_ARRAY(array, char, u2, length);
1395       } else {
1396         writer->write_raw(array->char_at_addr(0), length_in_bytes);
1397       }
1398       break;
1399     }
1400     case T_SHORT : {
1401       if (Endian::is_Java_byte_ordering_different()) {
1402         WRITE_ARRAY(array, short, u2, length);
1403       } else {
1404         writer->write_raw(array->short_at_addr(0), length_in_bytes);
1405       }
1406       break;
1407     }
1408     case T_BOOLEAN : {
1409       if (Endian::is_Java_byte_ordering_different()) {
1410         WRITE_ARRAY(array, bool, u1, length);
1411       } else {
1412         writer->write_raw(array->bool_at_addr(0), length_in_bytes);
1413       }
1414       break;
1415     }
1416     case T_LONG : {
1417       if (Endian::is_Java_byte_ordering_different()) {
1418         WRITE_ARRAY(array, long, u8, length);
1419       } else {
1420         writer->write_raw(array->long_at_addr(0), length_in_bytes);
1421       }
1422       break;
1423     }
1424 
1425     // handle float/doubles in a special value to ensure than NaNs are
1426     // written correctly. TO DO: Check if we can avoid this on processors that
1427     // use IEEE 754.
1428 
1429     case T_FLOAT : {
1430       for (int i = 0; i < length; i++) {
1431         dump_float(writer, array->float_at(i));
1432       }
1433       break;
1434     }
1435     case T_DOUBLE : {
1436       for (int i = 0; i < length; i++) {
1437         dump_double(writer, array->double_at(i));
1438       }
1439       break;
1440     }
1441     default : ShouldNotReachHere();
1442   }
1443 
1444   writer->end_sub_record();
1445 }
1446 
1447 // create a HPROF_FRAME record of the given Method* and bci
1448 void DumperSupport::dump_stack_frame(AbstractDumpWriter* writer,
1449                                      int frame_serial_num,
1450                                      int class_serial_num,
1451                                      Method* m,
1452                                      int bci) {
1453   int line_number;
1454   if (m->is_native()) {
1455     line_number = -3;  // native frame
1456   } else {
1457     line_number = m->line_number_from_bci(bci);
1458   }
1459 
1460   write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1461   writer->write_id(frame_serial_num);               // frame serial number
1462   writer->write_symbolID(m->name());                // method's name
1463   writer->write_symbolID(m->signature());           // method's signature
1464 
1465   assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1466   writer->write_symbolID(m->method_holder()->source_file_name());  // source file name
1467   writer->write_u4(class_serial_num);               // class serial number
1468   writer->write_u4((u4) line_number);               // line number
1469 }
1470 
1471 
1472 // Support class used to generate HPROF_UTF8 records from the entries in the
1473 // SymbolTable.
1474 
1475 class SymbolTableDumper : public SymbolClosure {
1476  private:
1477   AbstractDumpWriter* _writer;
1478   AbstractDumpWriter* writer() const                { return _writer; }
1479  public:
1480   SymbolTableDumper(AbstractDumpWriter* writer)     { _writer = writer; }
1481   void do_symbol(Symbol** p);
1482 };
1483 
1484 void SymbolTableDumper::do_symbol(Symbol** p) {
1485   ResourceMark rm;
1486   Symbol* sym = *p;
1487   int len = sym->utf8_length();
1488   if (len > 0) {
1489     char* s = sym->as_utf8();
1490     DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1491     writer()->write_symbolID(sym);
1492     writer()->write_raw(s, len);
1493   }
1494 }
1495 
1496 // Support class used to generate HPROF_GC_CLASS_DUMP records
1497 
1498 class ClassDumper : public KlassClosure {
1499  private:
1500   AbstractDumpWriter* _writer;
1501   AbstractDumpWriter* writer() const { return _writer; }
1502 
1503  public:
1504   ClassDumper(AbstractDumpWriter* writer) : _writer(writer) {}
1505 
1506   void do_klass(Klass* k) {
1507     if (k->is_instance_klass()) {
1508       DumperSupport::dump_instance_class(writer(), k);
1509     } else {
1510       DumperSupport::dump_array_class(writer(), k);
1511     }
1512   }
1513 };
1514 
1515 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1516 
1517 class JNILocalsDumper : public OopClosure {
1518  private:
1519   AbstractDumpWriter* _writer;
1520   u4 _thread_serial_num;
1521   int _frame_num;
1522   AbstractDumpWriter* writer() const                { return _writer; }
1523  public:
1524   JNILocalsDumper(AbstractDumpWriter* writer, u4 thread_serial_num) {
1525     _writer = writer;
1526     _thread_serial_num = thread_serial_num;
1527     _frame_num = -1;  // default - empty stack
1528   }
1529   void set_frame_number(int n) { _frame_num = n; }
1530   void do_oop(oop* obj_p);
1531   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1532 };
1533 
1534 void JNILocalsDumper::do_oop(oop* obj_p) {
1535   // ignore null handles
1536   oop o = *obj_p;
1537   if (o != nullptr) {
1538     u4 size = 1 + sizeof(address) + 4 + 4;
1539     writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
1540     writer()->write_objectID(o);
1541     writer()->write_u4(_thread_serial_num);
1542     writer()->write_u4((u4)_frame_num);
1543     writer()->end_sub_record();
1544   }
1545 }
1546 
1547 
1548 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1549 
1550 class JNIGlobalsDumper : public OopClosure {
1551  private:
1552   AbstractDumpWriter* _writer;
1553   AbstractDumpWriter* writer() const                { return _writer; }
1554 
1555  public:
1556   JNIGlobalsDumper(AbstractDumpWriter* writer) {
1557     _writer = writer;
1558   }
1559   void do_oop(oop* obj_p);
1560   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1561 };
1562 
1563 void JNIGlobalsDumper::do_oop(oop* obj_p) {
1564   oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
1565 
1566   // ignore these
1567   if (o == nullptr) return;
1568   // we ignore global ref to symbols and other internal objects
1569   if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1570     u4 size = 1 + 2 * sizeof(address);
1571     writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
1572     writer()->write_objectID(o);
1573     writer()->write_rootID(obj_p);      // global ref ID
1574     writer()->end_sub_record();
1575   }
1576 };
1577 
1578 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1579 
1580 class StickyClassDumper : public KlassClosure {
1581  private:
1582   AbstractDumpWriter* _writer;
1583   AbstractDumpWriter* writer() const                { return _writer; }
1584  public:
1585   StickyClassDumper(AbstractDumpWriter* writer) {
1586     _writer = writer;
1587   }
1588   void do_klass(Klass* k) {
1589     if (k->is_instance_klass()) {
1590       InstanceKlass* ik = InstanceKlass::cast(k);
1591       u4 size = 1 + sizeof(address);
1592       writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
1593       writer()->write_classID(ik);
1594       writer()->end_sub_record();
1595     }
1596   }
1597 };
1598 
1599 // Support class used to generate HPROF_GC_ROOT_JAVA_FRAME records.
1600 
1601 class JavaStackRefDumper : public StackObj {
1602 private:
1603   AbstractDumpWriter* _writer;
1604   u4 _thread_serial_num;
1605   int _frame_num;
1606   AbstractDumpWriter* writer() const { return _writer; }
1607 public:
1608   JavaStackRefDumper(AbstractDumpWriter* writer, u4 thread_serial_num)
1609       : _writer(writer), _thread_serial_num(thread_serial_num), _frame_num(-1) // default - empty stack
1610   {
1611   }
1612 
1613   void set_frame_number(int n) { _frame_num = n; }
1614 
1615   void dump_java_stack_refs(StackValueCollection* values);
1616 };
1617 
1618 void JavaStackRefDumper::dump_java_stack_refs(StackValueCollection* values) {
1619   for (int index = 0; index < values->size(); index++) {
1620     if (values->at(index)->type() == T_OBJECT) {
1621       oop o = values->obj_at(index)();
1622       if (o != nullptr) {
1623         u4 size = 1 + sizeof(address) + 4 + 4;
1624         writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
1625         writer()->write_objectID(o);
1626         writer()->write_u4(_thread_serial_num);
1627         writer()->write_u4((u4)_frame_num);
1628         writer()->end_sub_record();
1629       }
1630     }
1631   }
1632 }
1633 
1634 // Class to collect, store and dump thread-related data:
1635 // - HPROF_TRACE and HPROF_FRAME records;
1636 // - HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecords.
1637 class ThreadDumper : public CHeapObj<mtInternal> {
1638 public:
1639   enum class ThreadType { Platform, MountedVirtual, UnmountedVirtual };
1640 
1641 private:
1642   ThreadType _thread_type;
1643   JavaThread* _java_thread;
1644   oop _thread_oop;
1645 
1646   GrowableArray<StackFrameInfo*>* _frames;
1647   // non-null if the thread is OOM thread
1648   Method* _oome_constructor;
1649   int _thread_serial_num;
1650   int _start_frame_serial_num;
1651 
1652   vframe* get_top_frame() const;
1653 
1654 public:
1655   static bool should_dump_pthread(JavaThread* thread) {
1656     return thread->threadObj() != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view();
1657   }
1658 
1659   static bool should_dump_vthread(oop vt) {
1660     return java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW
1661         && java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::TERMINATED;
1662   }
1663 
1664   static bool is_vthread_mounted(oop vt) {
1665     // The code should be consistent with the "mounted virtual thread" case
1666     // (VM_HeapDumper::dump_stack_traces(), ThreadDumper::get_top_frame()).
1667     // I.e. virtual thread is mounted if its carrierThread is not null
1668     // and is_vthread_mounted() for the carrier thread returns true.
1669     oop carrier_thread = java_lang_VirtualThread::carrier_thread(vt);
1670     if (carrier_thread == nullptr) {
1671       return false;
1672     }
1673     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
1674     return java_thread->is_vthread_mounted();
1675   }
1676 
1677   ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop);
1678 
1679   // affects frame_count
1680   void add_oom_frame(Method* oome_constructor) {
1681     assert(_start_frame_serial_num == 0, "add_oom_frame cannot be called after init_serial_nums");
1682     _oome_constructor = oome_constructor;
1683   }
1684 
1685   void init_serial_nums(volatile int* thread_counter, volatile int* frame_counter) {
1686     assert(_start_frame_serial_num == 0, "already initialized");
1687     _thread_serial_num = Atomic::fetch_then_add(thread_counter, 1);
1688     _start_frame_serial_num = Atomic::fetch_then_add(frame_counter, frame_count());
1689   }
1690 
1691   bool oom_thread() const {
1692     return _oome_constructor != nullptr;
1693   }
1694 
1695   int frame_count() const {
1696     return _frames->length() + (oom_thread() ? 1 : 0);
1697   }
1698 
1699   u4 thread_serial_num() const {
1700     return (u4)_thread_serial_num;
1701   }
1702 
1703   u4 stack_trace_serial_num() const {
1704     return (u4)(_thread_serial_num + STACK_TRACE_ID);
1705   }
1706 
1707   // writes HPROF_TRACE and HPROF_FRAME records
1708   // returns number of dumped frames
1709   void dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map);
1710 
1711   // writes HPROF_GC_ROOT_THREAD_OBJ subrecord
1712   void dump_thread_obj(AbstractDumpWriter* writer);
1713 
1714   // Walk the stack of the thread.
1715   // Dumps a HPROF_GC_ROOT_JAVA_FRAME subrecord for each local
1716   // Dumps a HPROF_GC_ROOT_JNI_LOCAL subrecord for each JNI local
1717   void dump_stack_refs(AbstractDumpWriter* writer);
1718 
1719 };
1720 
1721 ThreadDumper::ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop)
1722     : _thread_type(thread_type), _java_thread(java_thread), _thread_oop(thread_oop),
1723       _oome_constructor(nullptr),
1724       _thread_serial_num(0), _start_frame_serial_num(0)
1725 {
1726   // sanity checks
1727   if (_thread_type == ThreadType::UnmountedVirtual) {
1728     assert(_java_thread == nullptr, "sanity");
1729     assert(_thread_oop != nullptr, "sanity");
1730   } else {
1731     assert(_java_thread != nullptr, "sanity");
1732     assert(_thread_oop != nullptr, "sanity");
1733   }
1734 
1735   _frames = new (mtServiceability) GrowableArray<StackFrameInfo*>(10, mtServiceability);
1736   bool stop_at_vthread_entry = _thread_type == ThreadType::MountedVirtual;
1737 
1738   // vframes are resource allocated
1739   Thread* current_thread = Thread::current();
1740   ResourceMark rm(current_thread);
1741   HandleMark hm(current_thread);
1742 
1743   for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1744     if (stop_at_vthread_entry && vf->is_vthread_entry()) {
1745       break;
1746     }
1747     if (vf->is_java_frame()) {
1748       javaVFrame* jvf = javaVFrame::cast(vf);
1749       _frames->append(new StackFrameInfo(jvf, false));
1750     } else {
1751       // ignore non-Java frames
1752     }
1753   }
1754 }
1755 
1756 void ThreadDumper::dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map) {
1757   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_nums are not initialized");
1758 
1759   // write HPROF_FRAME records for this thread's stack trace
1760   int depth = _frames->length();
1761   int frame_serial_num = _start_frame_serial_num;
1762 
1763   if (oom_thread()) {
1764     // OOM thread
1765     // write fake frame that makes it look like the thread, which caused OOME,
1766     // is in the OutOfMemoryError zero-parameter constructor
1767     int oome_serial_num = klass_map->find(_oome_constructor->method_holder());
1768     // the class serial number starts from 1
1769     assert(oome_serial_num > 0, "OutOfMemoryError class not found");
1770     DumperSupport::dump_stack_frame(writer, ++frame_serial_num, oome_serial_num, _oome_constructor, 0);
1771     depth++;
1772   }
1773 
1774   for (int j = 0; j < _frames->length(); j++) {
1775     StackFrameInfo* frame = _frames->at(j);
1776     Method* m = frame->method();
1777     int class_serial_num = klass_map->find(m->method_holder());
1778     // the class serial number starts from 1
1779     assert(class_serial_num > 0, "class not found");
1780     DumperSupport::dump_stack_frame(writer, ++frame_serial_num, class_serial_num, m, frame->bci());
1781   }
1782 
1783   // write HPROF_TRACE record for the thread
1784   DumperSupport::write_header(writer, HPROF_TRACE, checked_cast<u4>(3 * sizeof(u4) + depth * oopSize));
1785   writer->write_u4(stack_trace_serial_num());   // stack trace serial number
1786   writer->write_u4(thread_serial_num());        // thread serial number
1787   writer->write_u4((u4)depth);                  // frame count (including oom frame)
1788   for (int j = 1; j <= depth; j++) {
1789     writer->write_id(_start_frame_serial_num + j);
1790   }
1791 }
1792 
1793 void ThreadDumper::dump_thread_obj(AbstractDumpWriter * writer) {
1794   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1795 
1796   u4 size = 1 + sizeof(address) + 4 + 4;
1797   writer->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
1798   writer->write_objectID(_thread_oop);
1799   writer->write_u4(thread_serial_num());      // thread serial number
1800   writer->write_u4(stack_trace_serial_num()); // stack trace serial number
1801   writer->end_sub_record();
1802 }
1803 
1804 void ThreadDumper::dump_stack_refs(AbstractDumpWriter * writer) {
1805   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1806 
1807   JNILocalsDumper blk(writer, thread_serial_num());
1808   if (_thread_type == ThreadType::Platform) {
1809     if (!_java_thread->has_last_Java_frame()) {
1810       // no last java frame but there may be JNI locals
1811       _java_thread->active_handles()->oops_do(&blk);
1812       return;
1813     }
1814   }
1815 
1816   JavaStackRefDumper java_ref_dumper(writer, thread_serial_num());
1817 
1818   // vframes are resource allocated
1819   Thread* current_thread = Thread::current();
1820   ResourceMark rm(current_thread);
1821   HandleMark hm(current_thread);
1822 
1823   bool stopAtVthreadEntry = _thread_type == ThreadType::MountedVirtual;
1824   frame* last_entry_frame = nullptr;
1825   bool is_top_frame = true;
1826   int depth = 0;
1827   if (oom_thread()) {
1828     depth++;
1829   }
1830 
1831   for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1832     if (stopAtVthreadEntry && vf->is_vthread_entry()) {
1833       break;
1834     }
1835 
1836     if (vf->is_java_frame()) {
1837       javaVFrame* jvf = javaVFrame::cast(vf);
1838       if (!(jvf->method()->is_native())) {
1839         java_ref_dumper.set_frame_number(depth);
1840         java_ref_dumper.dump_java_stack_refs(jvf->locals());
1841         java_ref_dumper.dump_java_stack_refs(jvf->expressions());
1842       } else {
1843         // native frame
1844         blk.set_frame_number(depth);
1845         if (is_top_frame) {
1846           // JNI locals for the top frame.
1847           assert(_java_thread != nullptr, "impossible for unmounted vthread");
1848           _java_thread->active_handles()->oops_do(&blk);
1849         } else {
1850           if (last_entry_frame != nullptr) {
1851             // JNI locals for the entry frame
1852             assert(last_entry_frame->is_entry_frame(), "checking");
1853             last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1854           }
1855         }
1856       }
1857       last_entry_frame = nullptr;
1858       // increment only for Java frames
1859       depth++;
1860     } else {
1861       // externalVFrame - for an entry frame then we report the JNI locals
1862       // when we find the corresponding javaVFrame
1863       frame* fr = vf->frame_pointer();
1864       assert(fr != nullptr, "sanity check");
1865       if (fr->is_entry_frame()) {
1866         last_entry_frame = fr;
1867       }
1868     }
1869   is_top_frame = false;
1870   }
1871   assert(depth == frame_count(), "total number of Java frames not matched");
1872 }
1873 
1874 vframe* ThreadDumper::get_top_frame() const {
1875   if (_thread_type == ThreadType::UnmountedVirtual) {
1876     ContinuationWrapper cont(java_lang_VirtualThread::continuation(_thread_oop));
1877     if (cont.is_empty()) {
1878       return nullptr;
1879     }
1880     assert(!cont.is_mounted(), "sanity check");
1881     stackChunkOop chunk = cont.last_nonempty_chunk();
1882     if (chunk == nullptr || chunk->is_empty()) {
1883       return nullptr;
1884     }
1885 
1886     RegisterMap reg_map(cont.continuation(), RegisterMap::UpdateMap::include);
1887     frame fr = chunk->top_frame(&reg_map);
1888     vframe* vf = vframe::new_vframe(&fr, &reg_map, nullptr); // don't need JavaThread
1889     return vf;
1890   }
1891 
1892   RegisterMap reg_map(_java_thread,
1893       RegisterMap::UpdateMap::include,
1894       RegisterMap::ProcessFrames::include,
1895       RegisterMap::WalkContinuation::skip);
1896   switch (_thread_type) {
1897   case ThreadType::Platform:
1898     if (!_java_thread->has_last_Java_frame()) {
1899       return nullptr;
1900     }
1901     return _java_thread->is_vthread_mounted()
1902         ? _java_thread->carrier_last_java_vframe(&reg_map)
1903         : _java_thread->platform_thread_last_java_vframe(&reg_map);
1904 
1905   case ThreadType::MountedVirtual:
1906     return _java_thread->last_java_vframe(&reg_map);
1907 
1908   default: // make compilers happy
1909       break;
1910   }
1911   ShouldNotReachHere();
1912   return nullptr;
1913 }
1914 
1915 // Callback to dump thread-related data for unmounted virtual threads;
1916 // implemented by VM_HeapDumper.
1917 class UnmountedVThreadDumper {
1918  public:
1919   virtual void dump_vthread(oop vt, AbstractDumpWriter* segment_writer) = 0;
1920 };
1921 
1922 // Support class used when iterating over the heap.
1923 class HeapObjectDumper : public ObjectClosure {
1924  private:
1925   AbstractDumpWriter* _writer;
1926   AbstractDumpWriter* writer()                  { return _writer; }
1927   UnmountedVThreadDumper* _vthread_dumper;
1928 
1929   DumperClassCacheTable _class_cache;
1930 
1931  public:
1932   HeapObjectDumper(AbstractDumpWriter* writer, UnmountedVThreadDumper* vthread_dumper)
1933     : _writer(writer), _vthread_dumper(vthread_dumper) {}
1934 
1935   // called for each object in the heap
1936   void do_object(oop o);
1937 };
1938 
1939 void HeapObjectDumper::do_object(oop o) {
1940   // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1941   if (o->klass() == vmClasses::Class_klass()) {
1942     if (!java_lang_Class::is_primitive(o)) {
1943       return;
1944     }
1945   }
1946 
1947   if (DumperSupport::mask_dormant_archived_object(o, nullptr) == nullptr) {
1948     return;
1949   }
1950 
1951   if (o->is_instance()) {
1952     // create a HPROF_GC_INSTANCE record for each object
1953     DumperSupport::dump_instance(writer(), o, &_class_cache);
1954     // If we encounter an unmounted virtual thread it needs to be dumped explicitly
1955     // (mounted virtual threads are dumped with their carriers).
1956     if (java_lang_VirtualThread::is_instance(o)
1957         && ThreadDumper::should_dump_vthread(o) && !ThreadDumper::is_vthread_mounted(o)) {
1958       _vthread_dumper->dump_vthread(o, writer());
1959     }
1960   } else if (o->is_objArray()) {
1961     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1962     DumperSupport::dump_object_array(writer(), objArrayOop(o));
1963   } else if (o->is_typeArray()) {
1964     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1965     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1966   }
1967 }
1968 
1969 // The dumper controller for parallel heap dump
1970 class DumperController : public CHeapObj<mtInternal> {
1971  private:
1972    Monitor* _lock;
1973    Mutex* _global_writer_lock;
1974 
1975    const uint   _dumper_number;
1976    uint   _complete_number;
1977 
1978    bool   _started; // VM dumper started and acquired global writer lock
1979 
1980  public:
1981    DumperController(uint number) :
1982      // _lock and _global_writer_lock are used for synchronization between GC worker threads inside safepoint,
1983      // so we lock with _no_safepoint_check_flag.
1984      // signal_start() acquires _lock when global writer is locked,
1985      // its rank must be less than _global_writer_lock rank.
1986      _lock(new (std::nothrow) PaddedMonitor(Mutex::nosafepoint - 1, "DumperController_lock")),
1987      _global_writer_lock(new (std::nothrow) Mutex(Mutex::nosafepoint, "DumpWriter_lock")),
1988      _dumper_number(number),
1989      _complete_number(0),
1990      _started(false)
1991    {}
1992 
1993    ~DumperController() {
1994      delete _lock;
1995      delete _global_writer_lock;
1996    }
1997 
1998    // parallel (non VM) dumpers must wait until VM dumper acquires global writer lock
1999    void wait_for_start_signal() {
2000      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2001      while (_started == false) {
2002        ml.wait();
2003      }
2004    }
2005 
2006    void signal_start() {
2007      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2008      _started = true;
2009      ml.notify_all();
2010    }
2011 
2012    void lock_global_writer() {
2013      _global_writer_lock->lock_without_safepoint_check();
2014    }
2015 
2016    void unlock_global_writer() {
2017      _global_writer_lock->unlock();
2018    }
2019 
2020    void dumper_complete(DumpWriter* local_writer, DumpWriter* global_writer) {
2021      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2022      _complete_number++;
2023      // propagate local error to global if any
2024      if (local_writer->has_error()) {
2025        global_writer->set_error(local_writer->error());
2026      }
2027      ml.notify();
2028    }
2029 
2030    void wait_all_dumpers_complete() {
2031      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2032      while (_complete_number != _dumper_number) {
2033         ml.wait();
2034      }
2035    }
2036 };
2037 
2038 // DumpMerger merges separate dump files into a complete one
2039 class DumpMerger : public StackObj {
2040 private:
2041   DumpWriter* _writer;
2042   const char* _path;
2043   bool _has_error;
2044   int _dump_seq;
2045 
2046 private:
2047   void merge_file(const char* path);
2048   void merge_done();
2049   void set_error(const char* msg);
2050 
2051 public:
2052   DumpMerger(const char* path, DumpWriter* writer, int dump_seq) :
2053     _writer(writer),
2054     _path(path),
2055     _has_error(_writer->has_error()),
2056     _dump_seq(dump_seq) {}
2057 
2058   void do_merge();
2059 
2060   // returns path for the parallel DumpWriter (resource allocated)
2061   static char* get_writer_path(const char* base_path, int seq);
2062 
2063 };
2064 
2065 char* DumpMerger::get_writer_path(const char* base_path, int seq) {
2066   // approximate required buffer size
2067   size_t buf_size = strlen(base_path)
2068                     + 2                 // ".p"
2069                     + 10                // number (that's enough for 2^32 parallel dumpers)
2070                     + 1;                // '\0'
2071 
2072   char* path = NEW_RESOURCE_ARRAY(char, buf_size);
2073   memset(path, 0, buf_size);
2074 
2075   os::snprintf(path, buf_size, "%s.p%d", base_path, seq);
2076 
2077   return path;
2078 }
2079 
2080 
2081 void DumpMerger::merge_done() {
2082   // Writes the HPROF_HEAP_DUMP_END record.
2083   if (!_has_error) {
2084     DumperSupport::end_of_dump(_writer);
2085     _writer->flush();
2086   }
2087   _dump_seq = 0; //reset
2088 }
2089 
2090 void DumpMerger::set_error(const char* msg) {
2091   assert(msg != nullptr, "sanity check");
2092   log_error(heapdump)("%s (file: %s)", msg, _path);
2093   _writer->set_error(msg);
2094   _has_error = true;
2095 }
2096 
2097 #ifdef LINUX
2098 // Merge segmented heap files via sendfile, it's more efficient than the
2099 // read+write combination, which would require transferring data to and from
2100 // user space.
2101 void DumpMerger::merge_file(const char* path) {
2102   TraceTime timer("Merge segmented heap file directly", TRACETIME_LOG(Info, heapdump));
2103 
2104   int segment_fd = os::open(path, O_RDONLY, 0);
2105   if (segment_fd == -1) {
2106     set_error("Can not open segmented heap file during merging");
2107     return;
2108   }
2109 
2110   struct stat st;
2111   if (os::stat(path, &st) != 0) {
2112     ::close(segment_fd);
2113     set_error("Can not get segmented heap file size during merging");
2114     return;
2115   }
2116 
2117   // A successful call to sendfile may write fewer bytes than requested; the
2118   // caller should be prepared to retry the call if there were unsent bytes.
2119   jlong offset = 0;
2120   while (offset < st.st_size) {
2121     int ret = os::Linux::sendfile(_writer->get_fd(), segment_fd, &offset, st.st_size);
2122     if (ret == -1) {
2123       ::close(segment_fd);
2124       set_error("Failed to merge segmented heap file");
2125       return;
2126     }
2127   }
2128 
2129   // As sendfile variant does not call the write method of the global writer,
2130   // bytes_written is also incorrect for this variant, we need to explicitly
2131   // accumulate bytes_written for the global writer in this case
2132   julong accum = _writer->bytes_written() + st.st_size;
2133   _writer->set_bytes_written(accum);
2134   ::close(segment_fd);
2135 }
2136 #else
2137 // Generic implementation using read+write
2138 void DumpMerger::merge_file(const char* path) {
2139   TraceTime timer("Merge segmented heap file", TRACETIME_LOG(Info, heapdump));
2140 
2141   fileStream segment_fs(path, "rb");
2142   if (!segment_fs.is_open()) {
2143     set_error("Can not open segmented heap file during merging");
2144     return;
2145   }
2146 
2147   jlong total = 0;
2148   size_t cnt = 0;
2149 
2150   // Use _writer buffer for reading.
2151   while ((cnt = segment_fs.read(_writer->buffer(), 1, _writer->buffer_size())) != 0) {
2152     _writer->set_position(cnt);
2153     _writer->flush();
2154     total += cnt;
2155   }
2156 
2157   if (segment_fs.fileSize() != total) {
2158     set_error("Merged heap dump is incomplete");
2159   }
2160 }
2161 #endif
2162 
2163 void DumpMerger::do_merge() {
2164   TraceTime timer("Merge heap files complete", TRACETIME_LOG(Info, heapdump));
2165 
2166   // Since contents in segmented heap file were already zipped, we don't need to zip
2167   // them again during merging.
2168   AbstractCompressor* saved_compressor = _writer->compressor();
2169   _writer->set_compressor(nullptr);
2170 
2171   // Merge the content of the remaining files into base file. Regardless of whether
2172   // the merge process is successful or not, these segmented files will be deleted.
2173   for (int i = 0; i < _dump_seq; i++) {
2174     ResourceMark rm;
2175     const char* path = get_writer_path(_path, i);
2176     if (!_has_error) {
2177       merge_file(path);
2178     }
2179     // Delete selected segmented heap file nevertheless
2180     if (remove(path) != 0) {
2181       log_info(heapdump)("Removal of segment file (%d) failed (%d)", i, errno);
2182     }
2183   }
2184 
2185   // restore compressor for further use
2186   _writer->set_compressor(saved_compressor);
2187   merge_done();
2188 }
2189 
2190 // The VM operation that performs the heap dump
2191 class VM_HeapDumper : public VM_GC_Operation, public WorkerTask, public UnmountedVThreadDumper {
2192  private:
2193   static VM_HeapDumper*   _global_dumper;
2194   static DumpWriter*      _global_writer;
2195   DumpWriter*             _local_writer;
2196   JavaThread*             _oome_thread;
2197   Method*                 _oome_constructor;
2198   bool                    _gc_before_heap_dump;
2199   GrowableArray<Klass*>*  _klass_map;
2200 
2201   ThreadDumper**          _thread_dumpers; // platform, carrier and mounted virtual threads
2202   int                     _thread_dumpers_count;
2203   volatile int            _thread_serial_num;
2204   volatile int            _frame_serial_num;
2205 
2206   volatile int            _dump_seq;
2207   // parallel heap dump support
2208   uint                    _num_dumper_threads;
2209   DumperController*       _dumper_controller;
2210   ParallelObjectIterator* _poi;
2211 
2212   // Dumper id of VMDumper thread.
2213   static const int VMDumperId = 0;
2214   // VM dumper dumps both heap and non-heap data, other dumpers dump heap-only data.
2215   static bool is_vm_dumper(int dumper_id) { return dumper_id == VMDumperId; }
2216   // the 1st dumper calling get_next_dumper_id becomes VM dumper
2217   int get_next_dumper_id() {
2218     return Atomic::fetch_then_add(&_dump_seq, 1);
2219   }
2220 
2221   // accessors and setters
2222   static VM_HeapDumper* dumper()         {  assert(_global_dumper != nullptr, "Error"); return _global_dumper; }
2223   static DumpWriter* writer()            {  assert(_global_writer != nullptr, "Error"); return _global_writer; }
2224 
2225   void set_global_dumper() {
2226     assert(_global_dumper == nullptr, "Error");
2227     _global_dumper = this;
2228   }
2229   void set_global_writer() {
2230     assert(_global_writer == nullptr, "Error");
2231     _global_writer = _local_writer;
2232   }
2233   void clear_global_dumper() { _global_dumper = nullptr; }
2234   void clear_global_writer() { _global_writer = nullptr; }
2235 
2236   bool skip_operation() const;
2237 
2238   // writes a HPROF_LOAD_CLASS record to global writer
2239   static void do_load_class(Klass* k);
2240 
2241   // HPROF_GC_ROOT_THREAD_OBJ records for platform and mounted virtual threads
2242   void dump_threads(AbstractDumpWriter* writer);
2243 
2244   void add_class_serial_number(Klass* k, int serial_num) {
2245     _klass_map->at_put_grow(serial_num, k);
2246   }
2247 
2248   bool is_oom_thread(JavaThread* thread) const {
2249     return thread == _oome_thread && _oome_constructor != nullptr;
2250   }
2251 
2252   // HPROF_TRACE and HPROF_FRAME records for platform and mounted virtual threads
2253   void dump_stack_traces(AbstractDumpWriter* writer);
2254 
2255  public:
2256   VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome, uint num_dump_threads) :
2257     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
2258                     GCCause::_heap_dump /* GC Cause */,
2259                     0 /* total full collections, dummy, ignored */,
2260                     gc_before_heap_dump),
2261     WorkerTask("dump heap") {
2262     _local_writer = writer;
2263     _gc_before_heap_dump = gc_before_heap_dump;
2264     _klass_map = new (mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
2265 
2266     _thread_dumpers = nullptr;
2267     _thread_dumpers_count = 0;
2268     _thread_serial_num = 1;
2269     _frame_serial_num = 1;
2270 
2271     _dump_seq = VMDumperId;
2272     _num_dumper_threads = num_dump_threads;
2273     _dumper_controller = nullptr;
2274     _poi = nullptr;
2275     if (oome) {
2276       assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
2277       // get OutOfMemoryError zero-parameter constructor
2278       InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
2279       _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
2280                                                           vmSymbols::void_method_signature());
2281       // get thread throwing OOME when generating the heap dump at OOME
2282       _oome_thread = JavaThread::current();
2283     } else {
2284       _oome_thread = nullptr;
2285       _oome_constructor = nullptr;
2286     }
2287   }
2288 
2289   ~VM_HeapDumper() {
2290     if (_thread_dumpers != nullptr) {
2291       for (int i = 0; i < _thread_dumpers_count; i++) {
2292         delete _thread_dumpers[i];
2293       }
2294       FREE_C_HEAP_ARRAY(ThreadDumper*, _thread_dumpers);
2295     }
2296 
2297     if (_dumper_controller != nullptr) {
2298       delete _dumper_controller;
2299       _dumper_controller = nullptr;
2300     }
2301     delete _klass_map;
2302   }
2303   int dump_seq()           { return _dump_seq; }
2304   bool is_parallel_dump()  { return _num_dumper_threads > 1; }
2305   void prepare_parallel_dump(WorkerThreads* workers);
2306 
2307   VMOp_Type type() const { return VMOp_HeapDumper; }
2308   virtual bool doit_prologue();
2309   void doit();
2310   void work(uint worker_id);
2311 
2312   // UnmountedVThreadDumper implementation
2313   void dump_vthread(oop vt, AbstractDumpWriter* segment_writer);
2314 };
2315 
2316 VM_HeapDumper* VM_HeapDumper::_global_dumper = nullptr;
2317 DumpWriter*    VM_HeapDumper::_global_writer = nullptr;
2318 
2319 bool VM_HeapDumper::skip_operation() const {
2320   return false;
2321 }
2322 
2323 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
2324 void DumperSupport::end_of_dump(AbstractDumpWriter* writer) {
2325   writer->finish_dump_segment();
2326 
2327   writer->write_u1(HPROF_HEAP_DUMP_END);
2328   writer->write_u4(0);
2329   writer->write_u4(0);
2330 }
2331 
2332 // writes a HPROF_LOAD_CLASS record for the class
2333 void VM_HeapDumper::do_load_class(Klass* k) {
2334   static u4 class_serial_num = 0;
2335 
2336   // len of HPROF_LOAD_CLASS record
2337   u4 remaining = 2*oopSize + 2*sizeof(u4);
2338 
2339   DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
2340 
2341   // class serial number is just a number
2342   writer()->write_u4(++class_serial_num);
2343 
2344   // class ID
2345   writer()->write_classID(k);
2346 
2347   // add the Klass* and class serial number pair
2348   dumper()->add_class_serial_number(k, class_serial_num);
2349 
2350   writer()->write_u4(STACK_TRACE_ID);
2351 
2352   // class name ID
2353   Symbol* name = k->name();
2354   writer()->write_symbolID(name);
2355 }
2356 
2357 // Write a HPROF_GC_ROOT_THREAD_OBJ record for platform/carrier and mounted virtual threads.
2358 // Then walk the stack so that locals and JNI locals are dumped.
2359 void VM_HeapDumper::dump_threads(AbstractDumpWriter* writer) {
2360   for (int i = 0; i < _thread_dumpers_count; i++) {
2361     _thread_dumpers[i]->dump_thread_obj(writer);
2362     _thread_dumpers[i]->dump_stack_refs(writer);
2363   }
2364 }
2365 
2366 bool VM_HeapDumper::doit_prologue() {
2367   if (_gc_before_heap_dump && UseZGC) {
2368     // ZGC cannot perform a synchronous GC cycle from within the VM thread.
2369     // So ZCollectedHeap::collect_as_vm_thread() is a noop. To respect the
2370     // _gc_before_heap_dump flag a synchronous GC cycle is performed from
2371     // the caller thread in the prologue.
2372     Universe::heap()->collect(GCCause::_heap_dump);
2373   }
2374   return VM_GC_Operation::doit_prologue();
2375 }
2376 
2377 void VM_HeapDumper::prepare_parallel_dump(WorkerThreads* workers) {
2378   uint num_active_workers = workers != nullptr ? workers->active_workers() : 0;
2379   uint num_requested_dump_threads = _num_dumper_threads;
2380   // check if we can dump in parallel based on requested and active threads
2381   if (num_active_workers <= 1 || num_requested_dump_threads <= 1) {
2382     _num_dumper_threads = 1;
2383   } else {
2384     _num_dumper_threads = clamp(num_requested_dump_threads, 2U, num_active_workers);
2385   }
2386   _dumper_controller = new (std::nothrow) DumperController(_num_dumper_threads);
2387   bool can_parallel = _num_dumper_threads > 1;
2388   log_info(heapdump)("Requested dump threads %u, active dump threads %u, "
2389                      "actual dump threads %u, parallelism %s",
2390                      num_requested_dump_threads, num_active_workers,
2391                      _num_dumper_threads, can_parallel ? "true" : "false");
2392 }
2393 
2394 // The VM operation that dumps the heap. The dump consists of the following
2395 // records:
2396 //
2397 //  HPROF_HEADER
2398 //  [HPROF_UTF8]*
2399 //  [HPROF_LOAD_CLASS]*
2400 //  [[HPROF_FRAME]*|HPROF_TRACE]*
2401 //  [HPROF_GC_CLASS_DUMP]*
2402 //  [HPROF_HEAP_DUMP_SEGMENT]*
2403 //  HPROF_HEAP_DUMP_END
2404 //
2405 // The HPROF_TRACE records represent the stack traces where the heap dump
2406 // is generated and a "dummy trace" record which does not include
2407 // any frames. The dummy trace record is used to be referenced as the
2408 // unknown object alloc site.
2409 //
2410 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
2411 // To allow the heap dump be generated in a single pass we remember the position
2412 // of the dump length and fix it up after all sub-records have been written.
2413 // To generate the sub-records we iterate over the heap, writing
2414 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
2415 // records as we go. Once that is done we write records for some of the GC
2416 // roots.
2417 
2418 void VM_HeapDumper::doit() {
2419 
2420   CollectedHeap* ch = Universe::heap();
2421 
2422   ch->ensure_parsability(false); // must happen, even if collection does
2423                                  // not happen (e.g. due to GCLocker)
2424 
2425   if (_gc_before_heap_dump) {
2426     if (GCLocker::is_active()) {
2427       warning("GC locker is held; pre-heapdump GC was skipped");
2428     } else {
2429       ch->collect_as_vm_thread(GCCause::_heap_dump);
2430     }
2431   }
2432 
2433   // At this point we should be the only dumper active, so
2434   // the following should be safe.
2435   set_global_dumper();
2436   set_global_writer();
2437 
2438   WorkerThreads* workers = ch->safepoint_workers();
2439   prepare_parallel_dump(workers);
2440 
2441   if (!is_parallel_dump()) {
2442     work(VMDumperId);
2443   } else {
2444     ParallelObjectIterator poi(_num_dumper_threads);
2445     _poi = &poi;
2446     workers->run_task(this, _num_dumper_threads);
2447     _poi = nullptr;
2448   }
2449 
2450   // Now we clear the global variables, so that a future dumper can run.
2451   clear_global_dumper();
2452   clear_global_writer();
2453 }
2454 
2455 void VM_HeapDumper::work(uint worker_id) {
2456   // VM Dumper works on all non-heap data dumping and part of heap iteration.
2457   int dumper_id = get_next_dumper_id();
2458 
2459   if (is_vm_dumper(dumper_id)) {
2460     // lock global writer, it will be unlocked after VM Dumper finishes with non-heap data
2461     _dumper_controller->lock_global_writer();
2462     _dumper_controller->signal_start();
2463   } else {
2464     _dumper_controller->wait_for_start_signal();
2465   }
2466 
2467   if (is_vm_dumper(dumper_id)) {
2468     TraceTime timer("Dump non-objects", TRACETIME_LOG(Info, heapdump));
2469     // Write the file header - we always use 1.0.2
2470     const char* header = "JAVA PROFILE 1.0.2";
2471 
2472     // header is few bytes long - no chance to overflow int
2473     writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2474     writer()->write_u4(oopSize);
2475     // timestamp is current time in ms
2476     writer()->write_u8(os::javaTimeMillis());
2477     // HPROF_UTF8 records
2478     SymbolTableDumper sym_dumper(writer());
2479     SymbolTable::symbols_do(&sym_dumper);
2480 
2481     // write HPROF_LOAD_CLASS records
2482     {
2483       LockedClassesDo locked_load_classes(&do_load_class);
2484       ClassLoaderDataGraph::classes_do(&locked_load_classes);
2485     }
2486 
2487     // write HPROF_FRAME and HPROF_TRACE records
2488     // this must be called after _klass_map is built when iterating the classes above.
2489     dump_stack_traces(writer());
2490 
2491     // unlock global writer, so parallel dumpers can dump stack traces of unmounted virtual threads
2492     _dumper_controller->unlock_global_writer();
2493   }
2494 
2495   // HPROF_HEAP_DUMP/HPROF_HEAP_DUMP_SEGMENT starts here
2496 
2497   ResourceMark rm;
2498   // share global compressor, local DumpWriter is not responsible for its life cycle
2499   DumpWriter segment_writer(DumpMerger::get_writer_path(writer()->get_file_path(), dumper_id),
2500                             writer()->is_overwrite(), writer()->compressor());
2501   if (!segment_writer.has_error()) {
2502     if (is_vm_dumper(dumper_id)) {
2503       // dump some non-heap subrecords to heap dump segment
2504       TraceTime timer("Dump non-objects (part 2)", TRACETIME_LOG(Info, heapdump));
2505       // Writes HPROF_GC_CLASS_DUMP records
2506       ClassDumper class_dumper(&segment_writer);
2507       ClassLoaderDataGraph::classes_do(&class_dumper);
2508 
2509       // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2510       dump_threads(&segment_writer);
2511 
2512       // HPROF_GC_ROOT_JNI_GLOBAL
2513       JNIGlobalsDumper jni_dumper(&segment_writer);
2514       JNIHandles::oops_do(&jni_dumper);
2515       // technically not jni roots, but global roots
2516       // for things like preallocated throwable backtraces
2517       Universe::vm_global()->oops_do(&jni_dumper);
2518       // HPROF_GC_ROOT_STICKY_CLASS
2519       // These should be classes in the null class loader data, and not all classes
2520       // if !ClassUnloading
2521       StickyClassDumper stiky_class_dumper(&segment_writer);
2522       ClassLoaderData::the_null_class_loader_data()->classes_do(&stiky_class_dumper);
2523     }
2524 
2525     // Heap iteration.
2526     // writes HPROF_GC_INSTANCE_DUMP records.
2527     // After each sub-record is written check_segment_length will be invoked
2528     // to check if the current segment exceeds a threshold. If so, a new
2529     // segment is started.
2530     // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
2531     // of the heap dump.
2532 
2533     TraceTime timer(is_parallel_dump() ? "Dump heap objects in parallel" : "Dump heap objects", TRACETIME_LOG(Info, heapdump));
2534     HeapObjectDumper obj_dumper(&segment_writer, this);
2535     if (!is_parallel_dump()) {
2536       Universe::heap()->object_iterate(&obj_dumper);
2537     } else {
2538       // == Parallel dump
2539       _poi->object_iterate(&obj_dumper, worker_id);
2540     }
2541 
2542     segment_writer.finish_dump_segment();
2543     segment_writer.flush();
2544   }
2545 
2546   _dumper_controller->dumper_complete(&segment_writer, writer());
2547 
2548   if (is_vm_dumper(dumper_id)) {
2549     _dumper_controller->wait_all_dumpers_complete();
2550 
2551     // flush global writer
2552     writer()->flush();
2553 
2554     // At this point, all fragments of the heapdump have been written to separate files.
2555     // We need to merge them into a complete heapdump and write HPROF_HEAP_DUMP_END at that time.
2556   }
2557 }
2558 
2559 void VM_HeapDumper::dump_stack_traces(AbstractDumpWriter* writer) {
2560   // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2561   DumperSupport::write_header(writer, HPROF_TRACE, 3 * sizeof(u4));
2562   writer->write_u4((u4)STACK_TRACE_ID);
2563   writer->write_u4(0);                    // thread number
2564   writer->write_u4(0);                    // frame count
2565 
2566   // max number if every platform thread is carrier with mounted virtual thread
2567   _thread_dumpers = NEW_C_HEAP_ARRAY(ThreadDumper*, Threads::number_of_threads() * 2, mtInternal);
2568 
2569   for (JavaThreadIteratorWithHandle jtiwh; JavaThread * thread = jtiwh.next(); ) {
2570     if (ThreadDumper::should_dump_pthread(thread)) {
2571       bool add_oom_frame = is_oom_thread(thread);
2572 
2573       oop mounted_vt = thread->is_vthread_mounted() ? thread->vthread() : nullptr;
2574       if (mounted_vt != nullptr && !ThreadDumper::should_dump_vthread(mounted_vt)) {
2575         mounted_vt = nullptr;
2576       }
2577 
2578       // mounted vthread (if any)
2579       if (mounted_vt != nullptr) {
2580         ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::MountedVirtual, thread, mounted_vt);
2581         _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2582         if (add_oom_frame) {
2583           thread_dumper->add_oom_frame(_oome_constructor);
2584           // we add oom frame to the VT stack, don't add it to the carrier thread stack
2585           add_oom_frame = false;
2586         }
2587         thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2588         thread_dumper->dump_stack_traces(writer, _klass_map);
2589       }
2590 
2591       // platform or carrier thread
2592       ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::Platform, thread, thread->threadObj());
2593       _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2594       if (add_oom_frame) {
2595         thread_dumper->add_oom_frame(_oome_constructor);
2596       }
2597       thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2598       thread_dumper->dump_stack_traces(writer, _klass_map);
2599     }
2600   }
2601 }
2602 
2603 void VM_HeapDumper::dump_vthread(oop vt, AbstractDumpWriter* segment_writer) {
2604   // unmounted vthread has no JavaThread
2605   ThreadDumper thread_dumper(ThreadDumper::ThreadType::UnmountedVirtual, nullptr, vt);
2606   thread_dumper.init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2607 
2608   // write HPROF_TRACE/HPROF_FRAME records to global writer
2609   _dumper_controller->lock_global_writer();
2610   thread_dumper.dump_stack_traces(writer(), _klass_map);
2611   _dumper_controller->unlock_global_writer();
2612 
2613   // write HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecord
2614   // to segment writer
2615   thread_dumper.dump_thread_obj(segment_writer);
2616   thread_dumper.dump_stack_refs(segment_writer);
2617 }
2618 
2619 // dump the heap to given path.
2620 int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite, uint num_dump_threads) {
2621   assert(path != nullptr && strlen(path) > 0, "path missing");
2622 
2623   // print message in interactive case
2624   if (out != nullptr) {
2625     out->print_cr("Dumping heap to %s ...", path);
2626     timer()->start();
2627   }
2628 
2629   if (_oome && num_dump_threads > 1) {
2630     // Each additional parallel writer requires several MB of internal memory
2631     // (DumpWriter buffer, DumperClassCacheTable, GZipCompressor buffers).
2632     // For the OOM handling we may already be limited in memory.
2633     // Lets ensure we have at least 20MB per thread.
2634     julong max_threads = os::free_memory() / (20 * M);
2635     if (num_dump_threads > max_threads) {
2636       num_dump_threads = MAX2<uint>(1, (uint)max_threads);
2637     }
2638   }
2639 
2640   // create JFR event
2641   EventHeapDump event;
2642 
2643   AbstractCompressor* compressor = nullptr;
2644 
2645   if (compression > 0) {
2646     compressor = new (std::nothrow) GZipCompressor(compression);
2647 
2648     if (compressor == nullptr) {
2649       set_error("Could not allocate gzip compressor");
2650       return -1;
2651     }
2652   }
2653 
2654   DumpWriter writer(path, overwrite, compressor);
2655 
2656   if (writer.error() != nullptr) {
2657     set_error(writer.error());
2658     if (out != nullptr) {
2659       out->print_cr("Unable to create %s: %s", path,
2660         (error() != nullptr) ? error() : "reason unknown");
2661     }
2662     return -1;
2663   }
2664 
2665   // generate the segmented heap dump into separate files
2666   VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome, num_dump_threads);
2667   VMThread::execute(&dumper);
2668 
2669   // record any error that the writer may have encountered
2670   set_error(writer.error());
2671 
2672   // Heap dump process is done in two phases
2673   //
2674   // Phase 1: Concurrent threads directly write heap data to multiple heap files.
2675   //          This is done by VM_HeapDumper, which is performed within safepoint.
2676   //
2677   // Phase 2: Merge multiple heap files into one complete heap dump file.
2678   //          This is done by DumpMerger, which is performed outside safepoint
2679 
2680   DumpMerger merger(path, &writer, dumper.dump_seq());
2681   // Perform heapdump file merge operation in the current thread prevents us
2682   // from occupying the VM Thread, which in turn affects the occurrence of
2683   // GC and other VM operations.
2684   merger.do_merge();
2685   if (writer.error() != nullptr) {
2686     set_error(writer.error());
2687   }
2688 
2689   // emit JFR event
2690   if (error() == nullptr) {
2691     event.set_destination(path);
2692     event.set_gcBeforeDump(_gc_before_heap_dump);
2693     event.set_size(writer.bytes_written());
2694     event.set_onOutOfMemoryError(_oome);
2695     event.set_overwrite(overwrite);
2696     event.set_compression(compression);
2697     event.commit();
2698   } else {
2699     log_debug(cds, heap)("Error %s while dumping heap", error());
2700   }
2701 
2702   // print message in interactive case
2703   if (out != nullptr) {
2704     timer()->stop();
2705     if (error() == nullptr) {
2706       out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
2707                     writer.bytes_written(), timer()->seconds());
2708     } else {
2709       out->print_cr("Dump file is incomplete: %s", writer.error());
2710     }
2711   }
2712 
2713   if (compressor != nullptr) {
2714     delete compressor;
2715   }
2716   return (writer.error() == nullptr) ? 0 : -1;
2717 }
2718 
2719 // stop timer (if still active), and free any error string we might be holding
2720 HeapDumper::~HeapDumper() {
2721   if (timer()->is_active()) {
2722     timer()->stop();
2723   }
2724   set_error(nullptr);
2725 }
2726 
2727 
2728 // returns the error string (resource allocated), or null
2729 char* HeapDumper::error_as_C_string() const {
2730   if (error() != nullptr) {
2731     char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
2732     strcpy(str, error());
2733     return str;
2734   } else {
2735     return nullptr;
2736   }
2737 }
2738 
2739 // set the error string
2740 void HeapDumper::set_error(char const* error) {
2741   if (_error != nullptr) {
2742     os::free(_error);
2743   }
2744   if (error == nullptr) {
2745     _error = nullptr;
2746   } else {
2747     _error = os::strdup(error);
2748     assert(_error != nullptr, "allocation failure");
2749   }
2750 }
2751 
2752 // Called by out-of-memory error reporting by a single Java thread
2753 // outside of a JVM safepoint
2754 void HeapDumper::dump_heap_from_oome() {
2755   HeapDumper::dump_heap(true);
2756 }
2757 
2758 // Called by error reporting by a single Java thread outside of a JVM safepoint,
2759 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
2760 // callers are strictly serialized and guaranteed not to interfere below. For more
2761 // general use, however, this method will need modification to prevent
2762 // inteference when updating the static variables base_path and dump_file_seq below.
2763 void HeapDumper::dump_heap() {
2764   HeapDumper::dump_heap(false);
2765 }
2766 
2767 void HeapDumper::dump_heap(bool oome) {
2768   static char base_path[JVM_MAXPATHLEN] = {'\0'};
2769   static uint dump_file_seq = 0;
2770   char* my_path;
2771   const int max_digit_chars = 20;
2772 
2773   const char* dump_file_name = "java_pid";
2774   const char* dump_file_ext  = HeapDumpGzipLevel > 0 ? ".hprof.gz" : ".hprof";
2775 
2776   // The dump file defaults to java_pid<pid>.hprof in the current working
2777   // directory. HeapDumpPath=<file> can be used to specify an alternative
2778   // dump file name or a directory where dump file is created.
2779   if (dump_file_seq == 0) { // first time in, we initialize base_path
2780     // Calculate potentially longest base path and check if we have enough
2781     // allocated statically.
2782     const size_t total_length =
2783                       (HeapDumpPath == nullptr ? 0 : strlen(HeapDumpPath)) +
2784                       strlen(os::file_separator()) + max_digit_chars +
2785                       strlen(dump_file_name) + strlen(dump_file_ext) + 1;
2786     if (total_length > sizeof(base_path)) {
2787       warning("Cannot create heap dump file.  HeapDumpPath is too long.");
2788       return;
2789     }
2790 
2791     bool use_default_filename = true;
2792     if (HeapDumpPath == nullptr || HeapDumpPath[0] == '\0') {
2793       // HeapDumpPath=<file> not specified
2794     } else {
2795       strcpy(base_path, HeapDumpPath);
2796       // check if the path is a directory (must exist)
2797       DIR* dir = os::opendir(base_path);
2798       if (dir == nullptr) {
2799         use_default_filename = false;
2800       } else {
2801         // HeapDumpPath specified a directory. We append a file separator
2802         // (if needed).
2803         os::closedir(dir);
2804         size_t fs_len = strlen(os::file_separator());
2805         if (strlen(base_path) >= fs_len) {
2806           char* end = base_path;
2807           end += (strlen(base_path) - fs_len);
2808           if (strcmp(end, os::file_separator()) != 0) {
2809             strcat(base_path, os::file_separator());
2810           }
2811         }
2812       }
2813     }
2814     // If HeapDumpPath wasn't a file name then we append the default name
2815     if (use_default_filename) {
2816       const size_t dlen = strlen(base_path);  // if heap dump dir specified
2817       jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s",
2818                    dump_file_name, os::current_process_id(), dump_file_ext);
2819     }
2820     const size_t len = strlen(base_path) + 1;
2821     my_path = (char*)os::malloc(len, mtInternal);
2822     if (my_path == nullptr) {
2823       warning("Cannot create heap dump file.  Out of system memory.");
2824       return;
2825     }
2826     strncpy(my_path, base_path, len);
2827   } else {
2828     // Append a sequence number id for dumps following the first
2829     const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
2830     my_path = (char*)os::malloc(len, mtInternal);
2831     if (my_path == nullptr) {
2832       warning("Cannot create heap dump file.  Out of system memory.");
2833       return;
2834     }
2835     jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
2836   }
2837   dump_file_seq++;   // increment seq number for next time we dump
2838 
2839   HeapDumper dumper(false /* no GC before heap dump */,
2840                     oome  /* pass along out-of-memory-error flag */);
2841   dumper.dump(my_path, tty, HeapDumpGzipLevel);
2842   os::free(my_path);
2843 }