1 /*
   2  * Copyright (c) 2005, 2023, 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   void set_bytes_written(julong bytes_written) { _bytes_written = bytes_written; }
 630   char const* error() const override           { return _error; }
 631   void set_error(const char* error)            { _error = (char*)error; }
 632   bool has_error() const                       { return _error != nullptr; }
 633   const char* get_file_path() const            { return _writer->get_file_path(); }
 634   AbstractCompressor* compressor()             { return _compressor; }
 635   void set_compressor(AbstractCompressor* p)   { _compressor = p; }
 636   bool is_overwrite() const                    { return _writer->is_overwrite(); }
 637   int get_fd() const                           { return _writer->get_fd(); }
 638 
 639   void flush() override;
 640 };
 641 
 642 DumpWriter::DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor) :
 643   AbstractDumpWriter(),
 644   _writer(new (std::nothrow) FileWriter(path, overwrite)),
 645   _compressor(compressor),
 646   _bytes_written(0),
 647   _error(nullptr),
 648   _out_buffer(nullptr),
 649   _out_size(0),
 650   _out_pos(0),
 651   _tmp_buffer(nullptr),
 652   _tmp_size(0) {
 653   _error = (char*)_writer->open_writer();
 654   if (_error == nullptr) {
 655     _buffer = (char*)os::malloc(io_buffer_max_size, mtInternal);
 656     if (compressor != nullptr) {
 657       _error = (char*)_compressor->init(io_buffer_max_size, &_out_size, &_tmp_size);
 658       if (_error == nullptr) {
 659         if (_out_size > 0) {
 660           _out_buffer = (char*)os::malloc(_out_size, mtInternal);
 661         }
 662         if (_tmp_size > 0) {
 663           _tmp_buffer = (char*)os::malloc(_tmp_size, mtInternal);
 664         }
 665       }
 666     }
 667   }
 668   // initialize internal buffer
 669   _pos = 0;
 670   _size = io_buffer_max_size;
 671 }
 672 
 673 DumpWriter::~DumpWriter(){
 674   if (_buffer != nullptr) {
 675     os::free(_buffer);
 676   }
 677   if (_out_buffer != nullptr) {
 678     os::free(_out_buffer);
 679   }
 680   if (_tmp_buffer != nullptr) {
 681     os::free(_tmp_buffer);
 682   }
 683   if (_writer != NULL) {
 684     delete _writer;
 685   }
 686   _bytes_written = -1;
 687 }
 688 
 689 // flush any buffered bytes to the file
 690 void DumpWriter::flush() {
 691   if (_pos <= 0) {
 692     return;
 693   }
 694   if (has_error()) {
 695     _pos = 0;
 696     return;
 697   }
 698   char* result = nullptr;
 699   if (_compressor == nullptr) {
 700     result = (char*)_writer->write_buf(_buffer, _pos);
 701     _bytes_written += _pos;
 702   } else {
 703     do_compress();
 704     if (!has_error()) {
 705       result = (char*)_writer->write_buf(_out_buffer, _out_pos);
 706       _bytes_written += _out_pos;
 707     }
 708   }
 709   _pos = 0; // reset pos to make internal buffer available
 710 
 711   if (result != nullptr) {
 712     set_error(result);
 713   }
 714 }
 715 
 716 void DumpWriter::do_compress() {
 717   const char* msg = _compressor->compress(_buffer, _pos, _out_buffer, _out_size,
 718                                           _tmp_buffer, _tmp_size, &_out_pos);
 719 
 720   if (msg != nullptr) {
 721     set_error(msg);
 722   }
 723 }
 724 
 725 class DumperClassCacheTable;
 726 class DumperClassCacheTableEntry;
 727 
 728 // Support class with a collection of functions used when dumping the heap
 729 class DumperSupport : AllStatic {
 730  public:
 731 
 732   // write a header of the given type
 733   static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
 734 
 735   // returns hprof tag for the given type signature
 736   static hprofTag sig2tag(Symbol* sig);
 737   // returns hprof tag for the given basic type
 738   static hprofTag type2tag(BasicType type);
 739   // Returns the size of the data to write.
 740   static u4 sig2size(Symbol* sig);
 741 
 742   // returns the size of the instance of the given class
 743   static u4 instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry = nullptr);
 744 
 745   // dump a jfloat
 746   static void dump_float(AbstractDumpWriter* writer, jfloat f);
 747   // dump a jdouble
 748   static void dump_double(AbstractDumpWriter* writer, jdouble d);
 749   // dumps the raw value of the given field
 750   static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
 751   // returns the size of the static fields; also counts the static fields
 752   static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
 753   // dumps static fields of the given class
 754   static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
 755   // dump the raw values of the instance fields of the given object
 756   static void dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry);
 757   // get the count of the instance fields for a given class
 758   static u2 get_instance_fields_count(InstanceKlass* ik);
 759   // dumps the definition of the instance fields for a given class
 760   static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
 761   // creates HPROF_GC_INSTANCE_DUMP record for the given object
 762   static void dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache);
 763   // creates HPROF_GC_CLASS_DUMP record for the given instance class
 764   static void dump_instance_class(AbstractDumpWriter* writer, Klass* k);
 765   // creates HPROF_GC_CLASS_DUMP record for a given array class
 766   static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
 767 
 768   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
 769   static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
 770   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
 771   static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
 772   // create HPROF_FRAME record for the given method and bci
 773   static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
 774 
 775   // check if we need to truncate an array
 776   static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
 777 
 778   // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
 779   static void end_of_dump(AbstractDumpWriter* writer);
 780 
 781   static oop mask_dormant_archived_object(oop o, oop ref_obj) {
 782     if (o != nullptr && o->klass()->java_mirror_no_keepalive() == nullptr) {
 783       // Ignore this object since the corresponding java mirror is not loaded.
 784       // Might be a dormant archive object.
 785       report_dormant_archived_object(o, ref_obj);
 786       return nullptr;
 787     } else {
 788       return o;
 789     }
 790   }
 791 
 792   static void report_dormant_archived_object(oop o, oop ref_obj) {
 793     if (log_is_enabled(Trace, cds, heap)) {
 794       ResourceMark rm;
 795       if (ref_obj != nullptr) {
 796         log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
 797                   p2i(o), o->klass()->external_name(),
 798                   p2i(ref_obj), ref_obj->klass()->external_name());
 799       } else {
 800         log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)",
 801                   p2i(o), o->klass()->external_name());
 802       }
 803     }
 804   }
 805 };
 806 
 807 // Hash table of klasses to the klass metadata. This should greatly improve the
 808 // hash dumping performance. This hash table is supposed to be used by a single
 809 // thread only.
 810 //
 811 class DumperClassCacheTableEntry : public CHeapObj<mtServiceability> {
 812   friend class DumperClassCacheTable;
 813 private:
 814   GrowableArray<char> _sigs_start;
 815   GrowableArray<int> _offsets;
 816   u4 _instance_size;
 817   int _entries;
 818 
 819 public:
 820   DumperClassCacheTableEntry() : _instance_size(0), _entries(0) {};
 821 
 822   int field_count()             { return _entries; }
 823   char sig_start(int field_idx) { return _sigs_start.at(field_idx); }
 824   int offset(int field_idx)     { return _offsets.at(field_idx); }
 825   u4 instance_size()            { return _instance_size; }
 826 };
 827 
 828 class DumperClassCacheTable {
 829 private:
 830   // ResourceHashtable SIZE is specified at compile time so we
 831   // use 1031 which is the first prime after 1024.
 832   static constexpr size_t TABLE_SIZE = 1031;
 833 
 834   // Maintain the cache for N classes. This limits memory footprint
 835   // impact, regardless of how many classes we have in the dump.
 836   // This also improves look up performance by keeping the statically
 837   // sized table from overloading.
 838   static constexpr int CACHE_TOP = 256;
 839 
 840   typedef ResourceHashtable<InstanceKlass*, DumperClassCacheTableEntry*,
 841                             TABLE_SIZE, AnyObj::C_HEAP, mtServiceability> PtrTable;
 842   PtrTable* _ptrs;
 843 
 844   // Single-slot cache to handle the major case of objects of the same
 845   // class back-to-back, e.g. from T[].
 846   InstanceKlass* _last_ik;
 847   DumperClassCacheTableEntry* _last_entry;
 848 
 849   void unlink_all(PtrTable* table) {
 850     class CleanupEntry: StackObj {
 851     public:
 852       bool do_entry(InstanceKlass*& key, DumperClassCacheTableEntry*& entry) {
 853         delete entry;
 854         return true;
 855       }
 856     } cleanup;
 857     table->unlink(&cleanup);
 858   }
 859 
 860 public:
 861   DumperClassCacheTableEntry* lookup_or_create(InstanceKlass* ik) {
 862     if (_last_ik == ik) {
 863       return _last_entry;
 864     }
 865 
 866     DumperClassCacheTableEntry* entry;
 867     DumperClassCacheTableEntry** from_cache = _ptrs->get(ik);
 868     if (from_cache == nullptr) {
 869       entry = new DumperClassCacheTableEntry();
 870       for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
 871         if (!fld.access_flags().is_static()) {
 872           Symbol* sig = fld.signature();
 873           entry->_sigs_start.push(sig->char_at(0));
 874           entry->_offsets.push(fld.offset());
 875           entry->_entries++;
 876           entry->_instance_size += DumperSupport::sig2size(sig);
 877         }
 878       }
 879 
 880       if (_ptrs->number_of_entries() >= CACHE_TOP) {
 881         // We do not track the individual hit rates for table entries.
 882         // Purge the entire table, and let the cache catch up with new
 883         // distribution.
 884         unlink_all(_ptrs);
 885       }
 886 
 887       _ptrs->put(ik, entry);
 888     } else {
 889       entry = *from_cache;
 890     }
 891 
 892     // Remember for single-slot cache.
 893     _last_ik = ik;
 894     _last_entry = entry;
 895 
 896     return entry;
 897   }
 898 
 899   DumperClassCacheTable() : _ptrs(new (mtServiceability) PtrTable), _last_ik(nullptr), _last_entry(nullptr) {}
 900 
 901   ~DumperClassCacheTable() {
 902     unlink_all(_ptrs);
 903     delete _ptrs;
 904   }
 905 };
 906 
 907 // write a header of the given type
 908 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
 909   writer->write_u1(tag);
 910   writer->write_u4(0);                  // current ticks
 911   writer->write_u4(len);
 912 }
 913 
 914 // returns hprof tag for the given type signature
 915 hprofTag DumperSupport::sig2tag(Symbol* sig) {
 916   switch (sig->char_at(0)) {
 917     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;
 918     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
 919     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
 920     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
 921     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
 922     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
 923     case JVM_SIGNATURE_INT      : return HPROF_INT;
 924     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
 925     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
 926     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
 927     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 928   }
 929 }
 930 
 931 hprofTag DumperSupport::type2tag(BasicType type) {
 932   switch (type) {
 933     case T_BYTE     : return HPROF_BYTE;
 934     case T_CHAR     : return HPROF_CHAR;
 935     case T_FLOAT    : return HPROF_FLOAT;
 936     case T_DOUBLE   : return HPROF_DOUBLE;
 937     case T_INT      : return HPROF_INT;
 938     case T_LONG     : return HPROF_LONG;
 939     case T_SHORT    : return HPROF_SHORT;
 940     case T_BOOLEAN  : return HPROF_BOOLEAN;
 941     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 942   }
 943 }
 944 
 945 u4 DumperSupport::sig2size(Symbol* sig) {
 946   switch (sig->char_at(0)) {
 947     case JVM_SIGNATURE_CLASS:
 948     case JVM_SIGNATURE_ARRAY: return sizeof(address);
 949     case JVM_SIGNATURE_BOOLEAN:
 950     case JVM_SIGNATURE_BYTE: return 1;
 951     case JVM_SIGNATURE_SHORT:
 952     case JVM_SIGNATURE_CHAR: return 2;
 953     case JVM_SIGNATURE_INT:
 954     case JVM_SIGNATURE_FLOAT: return 4;
 955     case JVM_SIGNATURE_LONG:
 956     case JVM_SIGNATURE_DOUBLE: return 8;
 957     default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
 958   }
 959 }
 960 
 961 template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
 962   T to;
 963   static_assert(sizeof(to) == sizeof(from), "must be of the same size");
 964   memcpy(&to, &from, sizeof(to));
 965   return to;
 966 }
 967 
 968 // dump a jfloat
 969 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
 970   if (g_isnan(f)) {
 971     writer->write_u4(0x7fc00000); // collapsing NaNs
 972   } else {
 973     writer->write_u4(bit_cast<u4>(f));
 974   }
 975 }
 976 
 977 // dump a jdouble
 978 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
 979   if (g_isnan(d)) {
 980     writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
 981   } else {
 982     writer->write_u8(bit_cast<u8>(d));
 983   }
 984 }
 985 
 986 // dumps the raw value of the given field
 987 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
 988   switch (type) {
 989     case JVM_SIGNATURE_CLASS :
 990     case JVM_SIGNATURE_ARRAY : {
 991       oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
 992       o = mask_dormant_archived_object(o, obj);
 993       assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
 994       writer->write_objectID(o);
 995       break;
 996     }
 997     case JVM_SIGNATURE_BYTE : {
 998       jbyte b = obj->byte_field(offset);
 999       writer->write_u1(b);
1000       break;
1001     }
1002     case JVM_SIGNATURE_CHAR : {
1003       jchar c = obj->char_field(offset);
1004       writer->write_u2(c);
1005       break;
1006     }
1007     case JVM_SIGNATURE_SHORT : {
1008       jshort s = obj->short_field(offset);
1009       writer->write_u2(s);
1010       break;
1011     }
1012     case JVM_SIGNATURE_FLOAT : {
1013       jfloat f = obj->float_field(offset);
1014       dump_float(writer, f);
1015       break;
1016     }
1017     case JVM_SIGNATURE_DOUBLE : {
1018       jdouble d = obj->double_field(offset);
1019       dump_double(writer, d);
1020       break;
1021     }
1022     case JVM_SIGNATURE_INT : {
1023       jint i = obj->int_field(offset);
1024       writer->write_u4(i);
1025       break;
1026     }
1027     case JVM_SIGNATURE_LONG : {
1028       jlong l = obj->long_field(offset);
1029       writer->write_u8(l);
1030       break;
1031     }
1032     case JVM_SIGNATURE_BOOLEAN : {
1033       jboolean b = obj->bool_field(offset);
1034       writer->write_u1(b);
1035       break;
1036     }
1037     default : {
1038       ShouldNotReachHere();
1039       break;
1040     }
1041   }
1042 }
1043 
1044 // returns the size of the instance of the given class
1045 u4 DumperSupport::instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry) {
1046   if (class_cache_entry != nullptr) {
1047     return class_cache_entry->instance_size();
1048   } else {
1049     u4 size = 0;
1050     for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1051       if (!fld.access_flags().is_static()) {
1052         size += sig2size(fld.signature());
1053       }
1054     }
1055     return size;
1056   }
1057 }
1058 
1059 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1060   field_count = 0;
1061   u4 size = 0;
1062 
1063   for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1064     if (fldc.access_flags().is_static()) {
1065       field_count++;
1066       size += sig2size(fldc.signature());
1067     }
1068   }
1069 
1070   // Add in resolved_references which is referenced by the cpCache
1071   // The resolved_references is an array per InstanceKlass holding the
1072   // strings and other oops resolved from the constant pool.
1073   oop resolved_references = ik->constants()->resolved_references_or_null();
1074   if (resolved_references != nullptr) {
1075     field_count++;
1076     size += sizeof(address);
1077 
1078     // Add in the resolved_references of the used previous versions of the class
1079     // in the case of RedefineClasses
1080     InstanceKlass* prev = ik->previous_versions();
1081     while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1082       field_count++;
1083       size += sizeof(address);
1084       prev = prev->previous_versions();
1085     }
1086   }
1087 
1088   // We write the value itself plus a name and a one byte type tag per field.
1089   return checked_cast<u4>(size + field_count * (sizeof(address) + 1));
1090 }
1091 
1092 // dumps static fields of the given class
1093 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1094   InstanceKlass* ik = InstanceKlass::cast(k);
1095 
1096   // dump the field descriptors and raw values
1097   for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1098     if (fld.access_flags().is_static()) {
1099       Symbol* sig = fld.signature();
1100 
1101       writer->write_symbolID(fld.name());   // name
1102       writer->write_u1(sig2tag(sig));       // type
1103 
1104       // value
1105       dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1106     }
1107   }
1108 
1109   // Add resolved_references for each class that has them
1110   oop resolved_references = ik->constants()->resolved_references_or_null();
1111   if (resolved_references != nullptr) {
1112     writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1113     writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1114     writer->write_objectID(resolved_references);
1115 
1116     // Also write any previous versions
1117     InstanceKlass* prev = ik->previous_versions();
1118     while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1119       writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1120       writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1121       writer->write_objectID(prev->constants()->resolved_references());
1122       prev = prev->previous_versions();
1123     }
1124   }
1125 }
1126 
1127 // dump the raw values of the instance fields of the given object
1128 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry) {
1129   assert(class_cache_entry != nullptr, "Pre-condition: must be provided");
1130   for (int idx = 0; idx < class_cache_entry->field_count(); idx++) {
1131     dump_field_value(writer, class_cache_entry->sig_start(idx), o, class_cache_entry->offset(idx));
1132   }
1133 }
1134 
1135 // dumps the definition of the instance fields for a given class
1136 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1137   u2 field_count = 0;
1138 
1139   for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1140     if (!fldc.access_flags().is_static()) field_count++;
1141   }
1142 
1143   return field_count;
1144 }
1145 
1146 // dumps the definition of the instance fields for a given class
1147 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1148   InstanceKlass* ik = InstanceKlass::cast(k);
1149 
1150   // dump the field descriptors
1151   for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1152     if (!fld.access_flags().is_static()) {
1153       Symbol* sig = fld.signature();
1154 
1155       writer->write_symbolID(fld.name());   // name
1156       writer->write_u1(sig2tag(sig));       // type
1157     }
1158   }
1159 }
1160 
1161 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1162 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache) {
1163   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1164 
1165   DumperClassCacheTableEntry* cache_entry = class_cache->lookup_or_create(ik);
1166 
1167   u4 is = instance_size(ik, cache_entry);
1168   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1169 
1170   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1171   writer->write_objectID(o);
1172   writer->write_u4(STACK_TRACE_ID);
1173 
1174   // class ID
1175   writer->write_classID(ik);
1176 
1177   // number of bytes that follow
1178   writer->write_u4(is);
1179 
1180   // field values
1181   dump_instance_fields(writer, o, cache_entry);
1182 
1183   writer->end_sub_record();
1184 }
1185 
1186 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1187 void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, Klass* k) {
1188   InstanceKlass* ik = InstanceKlass::cast(k);
1189 
1190   // We can safepoint and do a heap dump at a point where we have a Klass,
1191   // but no java mirror class has been setup for it. So we need to check
1192   // that the class is at least loaded, to avoid crash from a null mirror.
1193   if (!ik->is_loaded()) {
1194     return;
1195   }
1196 
1197   u2 static_fields_count = 0;
1198   u4 static_size = get_static_fields_size(ik, static_fields_count);
1199   u2 instance_fields_count = get_instance_fields_count(ik);
1200   u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1201   u4 size = checked_cast<u4>(1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size);
1202 
1203   writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1204 
1205   // class ID
1206   writer->write_classID(ik);
1207   writer->write_u4(STACK_TRACE_ID);
1208 
1209   // super class ID
1210   InstanceKlass* java_super = ik->java_super();
1211   if (java_super == nullptr) {
1212     writer->write_objectID(oop(nullptr));
1213   } else {
1214     writer->write_classID(java_super);
1215   }
1216 
1217   writer->write_objectID(ik->class_loader());
1218   writer->write_objectID(ik->signers());
1219   writer->write_objectID(ik->protection_domain());
1220 
1221   // reserved
1222   writer->write_objectID(oop(nullptr));
1223   writer->write_objectID(oop(nullptr));
1224 
1225   // instance size
1226   writer->write_u4(DumperSupport::instance_size(ik));
1227 
1228   // size of constant pool - ignored by HAT 1.1
1229   writer->write_u2(0);
1230 
1231   // static fields
1232   writer->write_u2(static_fields_count);
1233   dump_static_fields(writer, ik);
1234 
1235   // description of instance fields
1236   writer->write_u2(instance_fields_count);
1237   dump_instance_field_descriptors(writer, ik);
1238 
1239   writer->end_sub_record();
1240 }
1241 
1242 // creates HPROF_GC_CLASS_DUMP record for the given array class
1243 void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1244   InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1245   if (k->is_objArray_klass()) {
1246     Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1247     assert(bk != nullptr, "checking");
1248     if (bk->is_instance_klass()) {
1249       ik = InstanceKlass::cast(bk);
1250     }
1251   }
1252 
1253   u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1254   writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1255   writer->write_classID(k);
1256   writer->write_u4(STACK_TRACE_ID);
1257 
1258   // super class of array classes is java.lang.Object
1259   InstanceKlass* java_super = k->java_super();
1260   assert(java_super != nullptr, "checking");
1261   writer->write_classID(java_super);
1262 
1263   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1264   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1265   writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1266 
1267   writer->write_objectID(oop(nullptr));    // reserved
1268   writer->write_objectID(oop(nullptr));
1269   writer->write_u4(0);             // instance size
1270   writer->write_u2(0);             // constant pool
1271   writer->write_u2(0);             // static fields
1272   writer->write_u2(0);             // instance fields
1273 
1274   writer->end_sub_record();
1275 
1276 }
1277 
1278 // Hprof uses an u4 as record length field,
1279 // which means we need to truncate arrays that are too long.
1280 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1281   BasicType type = ArrayKlass::cast(array->klass())->element_type();
1282   assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1283 
1284   int length = array->length();
1285 
1286   int type_size;
1287   if (type == T_OBJECT) {
1288     type_size = sizeof(address);
1289   } else {
1290     type_size = type2aelembytes(type);
1291   }
1292 
1293   size_t length_in_bytes = (size_t)length * type_size;
1294   uint max_bytes = max_juint - header_size;
1295 
1296   if (length_in_bytes > max_bytes) {
1297     length = max_bytes / type_size;
1298     length_in_bytes = (size_t)length * type_size;
1299 
1300     warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1301             type2name_tab[type], array->length(), length);
1302   }
1303   return length;
1304 }
1305 
1306 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1307 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1308   // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1309   short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1310   int length = calculate_array_max_length(writer, array, header_size);
1311   u4 size = checked_cast<u4>(header_size + length * sizeof(address));
1312 
1313   writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1314   writer->write_objectID(array);
1315   writer->write_u4(STACK_TRACE_ID);
1316   writer->write_u4(length);
1317 
1318   // array class ID
1319   writer->write_classID(array->klass());
1320 
1321   // [id]* elements
1322   for (int index = 0; index < length; index++) {
1323     oop o = array->obj_at(index);
1324     o = mask_dormant_archived_object(o, array);
1325     writer->write_objectID(o);
1326   }
1327 
1328   writer->end_sub_record();
1329 }
1330 
1331 #define WRITE_ARRAY(Array, Type, Size, Length) \
1332   for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1333 
1334 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1335 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1336   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1337   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1338   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1339 
1340   int length = calculate_array_max_length(writer, array, header_size);
1341   int type_size = type2aelembytes(type);
1342   u4 length_in_bytes = (u4)length * type_size;
1343   u4 size = header_size + length_in_bytes;
1344 
1345   writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1346   writer->write_objectID(array);
1347   writer->write_u4(STACK_TRACE_ID);
1348   writer->write_u4(length);
1349   writer->write_u1(type2tag(type));
1350 
1351   // nothing to copy
1352   if (length == 0) {
1353     writer->end_sub_record();
1354     return;
1355   }
1356 
1357   // If the byte ordering is big endian then we can copy most types directly
1358 
1359   switch (type) {
1360     case T_INT : {
1361       if (Endian::is_Java_byte_ordering_different()) {
1362         WRITE_ARRAY(array, int, u4, length);
1363       } else {
1364         writer->write_raw(array->int_at_addr(0), length_in_bytes);
1365       }
1366       break;
1367     }
1368     case T_BYTE : {
1369       writer->write_raw(array->byte_at_addr(0), length_in_bytes);
1370       break;
1371     }
1372     case T_CHAR : {
1373       if (Endian::is_Java_byte_ordering_different()) {
1374         WRITE_ARRAY(array, char, u2, length);
1375       } else {
1376         writer->write_raw(array->char_at_addr(0), length_in_bytes);
1377       }
1378       break;
1379     }
1380     case T_SHORT : {
1381       if (Endian::is_Java_byte_ordering_different()) {
1382         WRITE_ARRAY(array, short, u2, length);
1383       } else {
1384         writer->write_raw(array->short_at_addr(0), length_in_bytes);
1385       }
1386       break;
1387     }
1388     case T_BOOLEAN : {
1389       if (Endian::is_Java_byte_ordering_different()) {
1390         WRITE_ARRAY(array, bool, u1, length);
1391       } else {
1392         writer->write_raw(array->bool_at_addr(0), length_in_bytes);
1393       }
1394       break;
1395     }
1396     case T_LONG : {
1397       if (Endian::is_Java_byte_ordering_different()) {
1398         WRITE_ARRAY(array, long, u8, length);
1399       } else {
1400         writer->write_raw(array->long_at_addr(0), length_in_bytes);
1401       }
1402       break;
1403     }
1404 
1405     // handle float/doubles in a special value to ensure than NaNs are
1406     // written correctly. TO DO: Check if we can avoid this on processors that
1407     // use IEEE 754.
1408 
1409     case T_FLOAT : {
1410       for (int i = 0; i < length; i++) {
1411         dump_float(writer, array->float_at(i));
1412       }
1413       break;
1414     }
1415     case T_DOUBLE : {
1416       for (int i = 0; i < length; i++) {
1417         dump_double(writer, array->double_at(i));
1418       }
1419       break;
1420     }
1421     default : ShouldNotReachHere();
1422   }
1423 
1424   writer->end_sub_record();
1425 }
1426 
1427 // create a HPROF_FRAME record of the given Method* and bci
1428 void DumperSupport::dump_stack_frame(AbstractDumpWriter* writer,
1429                                      int frame_serial_num,
1430                                      int class_serial_num,
1431                                      Method* m,
1432                                      int bci) {
1433   int line_number;
1434   if (m->is_native()) {
1435     line_number = -3;  // native frame
1436   } else {
1437     line_number = m->line_number_from_bci(bci);
1438   }
1439 
1440   write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1441   writer->write_id(frame_serial_num);               // frame serial number
1442   writer->write_symbolID(m->name());                // method's name
1443   writer->write_symbolID(m->signature());           // method's signature
1444 
1445   assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1446   writer->write_symbolID(m->method_holder()->source_file_name());  // source file name
1447   writer->write_u4(class_serial_num);               // class serial number
1448   writer->write_u4((u4) line_number);               // line number
1449 }
1450 
1451 
1452 // Support class used to generate HPROF_UTF8 records from the entries in the
1453 // SymbolTable.
1454 
1455 class SymbolTableDumper : public SymbolClosure {
1456  private:
1457   AbstractDumpWriter* _writer;
1458   AbstractDumpWriter* writer() const                { return _writer; }
1459  public:
1460   SymbolTableDumper(AbstractDumpWriter* writer)     { _writer = writer; }
1461   void do_symbol(Symbol** p);
1462 };
1463 
1464 void SymbolTableDumper::do_symbol(Symbol** p) {
1465   ResourceMark rm;
1466   Symbol* sym = *p;
1467   int len = sym->utf8_length();
1468   if (len > 0) {
1469     char* s = sym->as_utf8();
1470     DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1471     writer()->write_symbolID(sym);
1472     writer()->write_raw(s, len);
1473   }
1474 }
1475 
1476 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1477 
1478 class JNILocalsDumper : public OopClosure {
1479  private:
1480   AbstractDumpWriter* _writer;
1481   u4 _thread_serial_num;
1482   int _frame_num;
1483   AbstractDumpWriter* writer() const                { return _writer; }
1484  public:
1485   JNILocalsDumper(AbstractDumpWriter* writer, u4 thread_serial_num) {
1486     _writer = writer;
1487     _thread_serial_num = thread_serial_num;
1488     _frame_num = -1;  // default - empty stack
1489   }
1490   void set_frame_number(int n) { _frame_num = n; }
1491   void do_oop(oop* obj_p);
1492   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1493 };
1494 
1495 void JNILocalsDumper::do_oop(oop* obj_p) {
1496   // ignore null handles
1497   oop o = *obj_p;
1498   if (o != nullptr) {
1499     u4 size = 1 + sizeof(address) + 4 + 4;
1500     writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
1501     writer()->write_objectID(o);
1502     writer()->write_u4(_thread_serial_num);
1503     writer()->write_u4((u4)_frame_num);
1504     writer()->end_sub_record();
1505   }
1506 }
1507 
1508 
1509 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1510 
1511 class JNIGlobalsDumper : public OopClosure {
1512  private:
1513   AbstractDumpWriter* _writer;
1514   AbstractDumpWriter* writer() const                { return _writer; }
1515 
1516  public:
1517   JNIGlobalsDumper(AbstractDumpWriter* writer) {
1518     _writer = writer;
1519   }
1520   void do_oop(oop* obj_p);
1521   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1522 };
1523 
1524 void JNIGlobalsDumper::do_oop(oop* obj_p) {
1525   oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
1526 
1527   // ignore these
1528   if (o == nullptr) return;
1529   // we ignore global ref to symbols and other internal objects
1530   if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1531     u4 size = 1 + 2 * sizeof(address);
1532     writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
1533     writer()->write_objectID(o);
1534     writer()->write_rootID(obj_p);      // global ref ID
1535     writer()->end_sub_record();
1536   }
1537 };
1538 
1539 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1540 
1541 class StickyClassDumper : public KlassClosure {
1542  private:
1543   AbstractDumpWriter* _writer;
1544   AbstractDumpWriter* writer() const                { return _writer; }
1545  public:
1546   StickyClassDumper(AbstractDumpWriter* writer) {
1547     _writer = writer;
1548   }
1549   void do_klass(Klass* k) {
1550     if (k->is_instance_klass()) {
1551       InstanceKlass* ik = InstanceKlass::cast(k);
1552       u4 size = 1 + sizeof(address);
1553       writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
1554       writer()->write_classID(ik);
1555       writer()->end_sub_record();
1556     }
1557   }
1558 };
1559 
1560 // Support class used to generate HPROF_GC_ROOT_JAVA_FRAME records.
1561 
1562 class JavaStackRefDumper : public StackObj {
1563 private:
1564   AbstractDumpWriter* _writer;
1565   u4 _thread_serial_num;
1566   int _frame_num;
1567   AbstractDumpWriter* writer() const { return _writer; }
1568 public:
1569   JavaStackRefDumper(AbstractDumpWriter* writer, u4 thread_serial_num)
1570       : _writer(writer), _thread_serial_num(thread_serial_num), _frame_num(-1) // default - empty stack
1571   {
1572   }
1573 
1574   void set_frame_number(int n) { _frame_num = n; }
1575 
1576   void dump_java_stack_refs(StackValueCollection* values);
1577 };
1578 
1579 void JavaStackRefDumper::dump_java_stack_refs(StackValueCollection* values) {
1580   for (int index = 0; index < values->size(); index++) {
1581     if (values->at(index)->type() == T_OBJECT) {
1582       oop o = values->obj_at(index)();
1583       if (o != nullptr) {
1584         u4 size = 1 + sizeof(address) + 4 + 4;
1585         writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
1586         writer()->write_objectID(o);
1587         writer()->write_u4(_thread_serial_num);
1588         writer()->write_u4((u4)_frame_num);
1589         writer()->end_sub_record();
1590       }
1591     }
1592   }
1593 }
1594 
1595 // Class to collect, store and dump thread-related data:
1596 // - HPROF_TRACE and HPROF_FRAME records;
1597 // - HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecords.
1598 class ThreadDumper : public CHeapObj<mtInternal> {
1599 public:
1600   enum class ThreadType { Platform, MountedVirtual, UnmountedVirtual };
1601 
1602 private:
1603   ThreadType _thread_type;
1604   JavaThread* _java_thread;
1605   oop _thread_oop;
1606 
1607   GrowableArray<StackFrameInfo*>* _frames;
1608   // non-null if the thread is OOM thread
1609   Method* _oome_constructor;
1610   int _thread_serial_num;
1611   int _start_frame_serial_num;
1612 
1613   vframe* get_top_frame() const;
1614 
1615 public:
1616   static bool should_dump_pthread(JavaThread* thread) {
1617     return thread->threadObj() != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view();
1618   }
1619 
1620   static bool should_dump_vthread(oop vt) {
1621     return java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW
1622         && java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::TERMINATED;
1623   }
1624 
1625   ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop);
1626 
1627   // affects frame_count
1628   void add_oom_frame(Method* oome_constructor) {
1629     assert(_start_frame_serial_num == 0, "add_oom_frame cannot be called after init_serial_nums");
1630     _oome_constructor = oome_constructor;
1631   }
1632 
1633   void init_serial_nums(volatile int* thread_counter, volatile int* frame_counter) {
1634     assert(_start_frame_serial_num == 0, "already initialized");
1635     _thread_serial_num = Atomic::fetch_then_add(thread_counter, 1);
1636     _start_frame_serial_num = Atomic::fetch_then_add(frame_counter, frame_count());
1637   }
1638 
1639   bool oom_thread() const {
1640     return _oome_constructor != nullptr;
1641   }
1642 
1643   int frame_count() const {
1644     return _frames->length() + (oom_thread() ? 1 : 0);
1645   }
1646 
1647   u4 thread_serial_num() const {
1648     return (u4)_thread_serial_num;
1649   }
1650 
1651   u4 stack_trace_serial_num() const {
1652     return (u4)(_thread_serial_num + STACK_TRACE_ID);
1653   }
1654 
1655   // writes HPROF_TRACE and HPROF_FRAME records
1656   // returns number of dumped frames
1657   void dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map);
1658 
1659   // writes HPROF_GC_ROOT_THREAD_OBJ subrecord
1660   void dump_thread_obj(AbstractDumpWriter* writer);
1661 
1662   // Walk the stack of the thread.
1663   // Dumps a HPROF_GC_ROOT_JAVA_FRAME subrecord for each local
1664   // Dumps a HPROF_GC_ROOT_JNI_LOCAL subrecord for each JNI local
1665   void dump_stack_refs(AbstractDumpWriter* writer);
1666 
1667 };
1668 
1669 ThreadDumper::ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop)
1670     : _thread_type(thread_type), _java_thread(java_thread), _thread_oop(thread_oop),
1671       _oome_constructor(nullptr),
1672       _thread_serial_num(0), _start_frame_serial_num(0)
1673 {
1674   // sanity checks
1675   if (_thread_type == ThreadType::UnmountedVirtual) {
1676     assert(_java_thread == nullptr, "sanity");
1677     assert(_thread_oop != nullptr, "sanity");
1678   } else {
1679     assert(_java_thread != nullptr, "sanity");
1680     assert(_thread_oop != nullptr, "sanity");
1681   }
1682 
1683   _frames = new (mtServiceability) GrowableArray<StackFrameInfo*>(10, mtServiceability);
1684   bool stop_at_vthread_entry = _thread_type == ThreadType::MountedVirtual;
1685 
1686   // vframes are resource allocated
1687   Thread* current_thread = Thread::current();
1688   ResourceMark rm(current_thread);
1689   HandleMark hm(current_thread);
1690 
1691   for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1692     if (stop_at_vthread_entry && vf->is_vthread_entry()) {
1693       break;
1694     }
1695     if (vf->is_java_frame()) {
1696       javaVFrame* jvf = javaVFrame::cast(vf);
1697       _frames->append(new StackFrameInfo(jvf, false));
1698     } else {
1699       // ignore non-Java frames
1700     }
1701   }
1702 }
1703 
1704 void ThreadDumper::dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map) {
1705   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_nums are not initialized");
1706 
1707   // write HPROF_FRAME records for this thread's stack trace
1708   int depth = _frames->length();
1709   int frame_serial_num = _start_frame_serial_num;
1710 
1711   if (oom_thread()) {
1712     // OOM thread
1713     // write fake frame that makes it look like the thread, which caused OOME,
1714     // is in the OutOfMemoryError zero-parameter constructor
1715     int oome_serial_num = klass_map->find(_oome_constructor->method_holder());
1716     // the class serial number starts from 1
1717     assert(oome_serial_num > 0, "OutOfMemoryError class not found");
1718     DumperSupport::dump_stack_frame(writer, ++frame_serial_num, oome_serial_num, _oome_constructor, 0);
1719     depth++;
1720   }
1721 
1722   for (int j = 0; j < _frames->length(); j++) {
1723     StackFrameInfo* frame = _frames->at(j);
1724     Method* m = frame->method();
1725     int class_serial_num = klass_map->find(m->method_holder());
1726     // the class serial number starts from 1
1727     assert(class_serial_num > 0, "class not found");
1728     DumperSupport::dump_stack_frame(writer, ++frame_serial_num, class_serial_num, m, frame->bci());
1729   }
1730 
1731   // write HPROF_TRACE record for the thread
1732   DumperSupport::write_header(writer, HPROF_TRACE, checked_cast<u4>(3 * sizeof(u4) + depth * oopSize));
1733   writer->write_u4(stack_trace_serial_num());   // stack trace serial number
1734   writer->write_u4(thread_serial_num());        // thread serial number
1735   writer->write_u4((u4)depth);                  // frame count (including oom frame)
1736   for (int j = 1; j <= depth; j++) {
1737     writer->write_id(_start_frame_serial_num + j);
1738   }
1739 }
1740 
1741 void ThreadDumper::dump_thread_obj(AbstractDumpWriter * writer) {
1742   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1743 
1744   u4 size = 1 + sizeof(address) + 4 + 4;
1745   writer->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
1746   writer->write_objectID(_thread_oop);
1747   writer->write_u4(thread_serial_num());      // thread serial number
1748   writer->write_u4(stack_trace_serial_num()); // stack trace serial number
1749   writer->end_sub_record();
1750 }
1751 
1752 void ThreadDumper::dump_stack_refs(AbstractDumpWriter * writer) {
1753   assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1754 
1755   JNILocalsDumper blk(writer, thread_serial_num());
1756   if (_thread_type == ThreadType::Platform) {
1757     if (!_java_thread->has_last_Java_frame()) {
1758       // no last java frame but there may be JNI locals
1759       _java_thread->active_handles()->oops_do(&blk);
1760       return;
1761     }
1762   }
1763 
1764   JavaStackRefDumper java_ref_dumper(writer, thread_serial_num());
1765 
1766   // vframes are resource allocated
1767   Thread* current_thread = Thread::current();
1768   ResourceMark rm(current_thread);
1769   HandleMark hm(current_thread);
1770 
1771   bool stopAtVthreadEntry = _thread_type == ThreadType::MountedVirtual;
1772   frame* last_entry_frame = nullptr;
1773   bool is_top_frame = true;
1774   int depth = 0;
1775   if (oom_thread()) {
1776     depth++;
1777   }
1778 
1779   for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1780     if (stopAtVthreadEntry && vf->is_vthread_entry()) {
1781       break;
1782     }
1783 
1784     if (vf->is_java_frame()) {
1785       javaVFrame* jvf = javaVFrame::cast(vf);
1786       if (!(jvf->method()->is_native())) {
1787         java_ref_dumper.set_frame_number(depth);
1788         java_ref_dumper.dump_java_stack_refs(jvf->locals());
1789         java_ref_dumper.dump_java_stack_refs(jvf->expressions());
1790       } else {
1791         // native frame
1792         blk.set_frame_number(depth);
1793         if (is_top_frame) {
1794           // JNI locals for the top frame.
1795           assert(_java_thread != nullptr, "impossible for unmounted vthread");
1796           _java_thread->active_handles()->oops_do(&blk);
1797         } else {
1798           if (last_entry_frame != nullptr) {
1799             // JNI locals for the entry frame
1800             assert(last_entry_frame->is_entry_frame(), "checking");
1801             last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1802           }
1803         }
1804       }
1805       last_entry_frame = nullptr;
1806       // increment only for Java frames
1807       depth++;
1808     } else {
1809       // externalVFrame - for an entry frame then we report the JNI locals
1810       // when we find the corresponding javaVFrame
1811       frame* fr = vf->frame_pointer();
1812       assert(fr != nullptr, "sanity check");
1813       if (fr->is_entry_frame()) {
1814         last_entry_frame = fr;
1815       }
1816     }
1817   is_top_frame = false;
1818   }
1819   assert(depth == frame_count(), "total number of Java frames not matched");
1820 }
1821 
1822 vframe* ThreadDumper::get_top_frame() const {
1823   if (_thread_type == ThreadType::UnmountedVirtual) {
1824     ContinuationWrapper cont(java_lang_VirtualThread::continuation(_thread_oop));
1825     if (cont.is_empty()) {
1826       return nullptr;
1827     }
1828     assert(!cont.is_mounted(), "sanity check");
1829     stackChunkOop chunk = cont.last_nonempty_chunk();
1830     if (chunk == nullptr || chunk->is_empty()) {
1831       return nullptr;
1832     }
1833 
1834     RegisterMap reg_map(cont.continuation(), RegisterMap::UpdateMap::include);
1835     frame fr = chunk->top_frame(&reg_map);
1836     vframe* vf = vframe::new_vframe(&fr, &reg_map, nullptr); // don't need JavaThread
1837     return vf;
1838   }
1839 
1840   RegisterMap reg_map(_java_thread,
1841       RegisterMap::UpdateMap::include,
1842       RegisterMap::ProcessFrames::include,
1843       RegisterMap::WalkContinuation::skip);
1844   switch (_thread_type) {
1845   case ThreadType::Platform:
1846     if (!_java_thread->has_last_Java_frame()) {
1847       return nullptr;
1848     }
1849     return _java_thread->is_vthread_mounted()
1850         ? _java_thread->carrier_last_java_vframe(&reg_map)
1851         : _java_thread->platform_thread_last_java_vframe(&reg_map);
1852 
1853   case ThreadType::MountedVirtual:
1854     return _java_thread->last_java_vframe(&reg_map);
1855 
1856   default: // make compilers happy
1857       break;
1858   }
1859   ShouldNotReachHere();
1860   return nullptr;
1861 }
1862 
1863 
1864 class VM_HeapDumper;
1865 
1866 // Support class using when iterating over the heap.
1867 class HeapObjectDumper : public ObjectClosure {
1868  private:
1869   AbstractDumpWriter* _writer;
1870   AbstractDumpWriter* writer()                  { return _writer; }
1871 
1872   DumperClassCacheTable _class_cache;
1873 
1874  public:
1875   HeapObjectDumper(AbstractDumpWriter* writer) {
1876     _writer = writer;
1877   }
1878 
1879   // called for each object in the heap
1880   void do_object(oop o);
1881 };
1882 
1883 void HeapObjectDumper::do_object(oop o) {
1884   // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1885   if (o->klass() == vmClasses::Class_klass()) {
1886     if (!java_lang_Class::is_primitive(o)) {
1887       return;
1888     }
1889   }
1890 
1891   if (DumperSupport::mask_dormant_archived_object(o, nullptr) == nullptr) {
1892     return;
1893   }
1894 
1895   if (o->is_instance()) {
1896     // create a HPROF_GC_INSTANCE record for each object
1897     DumperSupport::dump_instance(writer(), o, &_class_cache);
1898   } else if (o->is_objArray()) {
1899     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1900     DumperSupport::dump_object_array(writer(), objArrayOop(o));
1901   } else if (o->is_typeArray()) {
1902     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1903     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1904   }
1905 }
1906 
1907 // The dumper controller for parallel heap dump
1908 class DumperController : public CHeapObj<mtInternal> {
1909  private:
1910    Monitor* _lock;
1911    const uint   _dumper_number;
1912    uint   _complete_number;
1913 
1914  public:
1915    DumperController(uint number) :
1916      _lock(new (std::nothrow) PaddedMonitor(Mutex::safepoint, "DumperController_lock")),
1917      _dumper_number(number),
1918      _complete_number(0) { }
1919 
1920    ~DumperController() { delete _lock; }
1921 
1922    void dumper_complete(DumpWriter* local_writer, DumpWriter* global_writer) {
1923      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1924      _complete_number++;
1925      // propagate local error to global if any
1926      if (local_writer->has_error()) {
1927        global_writer->set_error(local_writer->error());
1928      }
1929      ml.notify();
1930    }
1931 
1932    void wait_all_dumpers_complete() {
1933      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1934      while (_complete_number != _dumper_number) {
1935         ml.wait();
1936      }
1937    }
1938 };
1939 
1940 // DumpMerger merges separate dump files into a complete one
1941 class DumpMerger : public StackObj {
1942 private:
1943   DumpWriter* _writer;
1944   const char* _path;
1945   bool _has_error;
1946   int _dump_seq;
1947 
1948 private:
1949   void merge_file(char* path);
1950   void merge_done();
1951   void set_error(const char* msg);
1952 
1953 public:
1954   DumpMerger(const char* path, DumpWriter* writer, int dump_seq) :
1955     _writer(writer),
1956     _path(path),
1957     _has_error(_writer->has_error()),
1958     _dump_seq(dump_seq) {}
1959 
1960   void do_merge();
1961 };
1962 
1963 void DumpMerger::merge_done() {
1964   // Writes the HPROF_HEAP_DUMP_END record.
1965   if (!_has_error) {
1966     DumperSupport::end_of_dump(_writer);
1967     _writer->flush();
1968   }
1969   _dump_seq = 0; //reset
1970 }
1971 
1972 void DumpMerger::set_error(const char* msg) {
1973   assert(msg != nullptr, "sanity check");
1974   log_error(heapdump)("%s (file: %s)", msg, _path);
1975   _writer->set_error(msg);
1976   _has_error = true;
1977 }
1978 
1979 #ifdef LINUX
1980 // Merge segmented heap files via sendfile, it's more efficient than the
1981 // read+write combination, which would require transferring data to and from
1982 // user space.
1983 void DumpMerger::merge_file(char* path) {
1984   assert(!SafepointSynchronize::is_at_safepoint(), "merging happens outside safepoint");
1985   TraceTime timer("Merge segmented heap file directly", TRACETIME_LOG(Info, heapdump));
1986 
1987   int segment_fd = os::open(path, O_RDONLY, 0);
1988   if (segment_fd == -1) {
1989     set_error("Can not open segmented heap file during merging");
1990     return;
1991   }
1992 
1993   struct stat st;
1994   if (os::stat(path, &st) != 0) {
1995     ::close(segment_fd);
1996     set_error("Can not get segmented heap file size during merging");
1997     return;
1998   }
1999 
2000   // A successful call to sendfile may write fewer bytes than requested; the
2001   // caller should be prepared to retry the call if there were unsent bytes.
2002   jlong offset = 0;
2003   while (offset < st.st_size) {
2004     int ret = os::Linux::sendfile(_writer->get_fd(), segment_fd, &offset, st.st_size);
2005     if (ret == -1) {
2006       ::close(segment_fd);
2007       set_error("Failed to merge segmented heap file");
2008       return;
2009     }
2010   }
2011 
2012   // As sendfile variant does not call the write method of the global writer,
2013   // bytes_written is also incorrect for this variant, we need to explicitly
2014   // accumulate bytes_written for the global writer in this case
2015   julong accum = _writer->bytes_written() + st.st_size;
2016   _writer->set_bytes_written(accum);
2017   ::close(segment_fd);
2018 }
2019 #else
2020 // Generic implementation using read+write
2021 void DumpMerger::merge_file(char* path) {
2022   assert(!SafepointSynchronize::is_at_safepoint(), "merging happens outside safepoint");
2023   TraceTime timer("Merge segmented heap file", TRACETIME_LOG(Info, heapdump));
2024 
2025   fileStream segment_fs(path, "rb");
2026   if (!segment_fs.is_open()) {
2027     set_error("Can not open segmented heap file during merging");
2028     return;
2029   }
2030 
2031   jlong total = 0;
2032   size_t cnt = 0;
2033   char read_buf[4096];
2034   while ((cnt = segment_fs.read(read_buf, 1, 4096)) != 0) {
2035     _writer->write_raw(read_buf, cnt);
2036     total += cnt;
2037   }
2038 
2039   _writer->flush();
2040   if (segment_fs.fileSize() != total) {
2041     set_error("Merged heap dump is incomplete");
2042   }
2043 }
2044 #endif
2045 
2046 void DumpMerger::do_merge() {
2047   assert(!SafepointSynchronize::is_at_safepoint(), "merging happens outside safepoint");
2048   TraceTime timer("Merge heap files complete", TRACETIME_LOG(Info, heapdump));
2049 
2050   // Since contents in segmented heap file were already zipped, we don't need to zip
2051   // them again during merging.
2052   AbstractCompressor* saved_compressor = _writer->compressor();
2053   _writer->set_compressor(nullptr);
2054 
2055   // Merge the content of the remaining files into base file. Regardless of whether
2056   // the merge process is successful or not, these segmented files will be deleted.
2057   char path[JVM_MAXPATHLEN];
2058   for (int i = 0; i < _dump_seq; i++) {
2059     memset(path, 0, JVM_MAXPATHLEN);
2060     os::snprintf(path, JVM_MAXPATHLEN, "%s.p%d", _path, i);
2061     if (!_has_error) {
2062       merge_file(path);
2063     }
2064     // Delete selected segmented heap file nevertheless
2065     if (remove(path) != 0) {
2066       log_info(heapdump)("Removal of segment file (%d) failed (%d)", i, errno);
2067     }
2068   }
2069 
2070   // restore compressor for further use
2071   _writer->set_compressor(saved_compressor);
2072   merge_done();
2073 }
2074 
2075 // The VM operation wraps DumpMerger so that it could be performed by VM thread
2076 class VM_HeapDumpMerge : public VM_Operation {
2077 private:
2078   DumpMerger* _merger;
2079 public:
2080   VM_HeapDumpMerge(DumpMerger* merger) : _merger(merger) {}
2081   VMOp_Type type() const { return VMOp_HeapDumpMerge; }
2082   // heap dump merge could happen outside safepoint
2083   virtual bool evaluate_at_safepoint() const { return false; }
2084   void doit() {
2085     _merger->do_merge();
2086   }
2087 };
2088 
2089 // The VM operation that performs the heap dump
2090 class VM_HeapDumper : public VM_GC_Operation, public WorkerTask {
2091  private:
2092   static VM_HeapDumper*   _global_dumper;
2093   static DumpWriter*      _global_writer;
2094   DumpWriter*             _local_writer;
2095   JavaThread*             _oome_thread;
2096   Method*                 _oome_constructor;
2097   bool                    _gc_before_heap_dump;
2098   GrowableArray<Klass*>*  _klass_map;
2099 
2100   ThreadDumper**          _thread_dumpers; // platform, carrier and mounted virtual threads
2101   int                     _thread_dumpers_count;
2102   volatile int            _thread_serial_num;
2103   volatile int            _frame_serial_num;
2104 
2105   volatile int            _dump_seq;
2106   // parallel heap dump support
2107   uint                    _num_dumper_threads;
2108   DumperController*       _dumper_controller;
2109   ParallelObjectIterator* _poi;
2110   // worker id of VMDumper thread.
2111   static const size_t VMDumperWorkerId = 0;
2112   // VM dumper dumps both heap and non-heap data, other dumpers dump heap-only data.
2113   static bool is_vm_dumper(uint worker_id) { return worker_id == VMDumperWorkerId; }
2114 
2115   // accessors and setters
2116   static VM_HeapDumper* dumper()         {  assert(_global_dumper != nullptr, "Error"); return _global_dumper; }
2117   static DumpWriter* writer()            {  assert(_global_writer != nullptr, "Error"); return _global_writer; }
2118 
2119   void set_global_dumper() {
2120     assert(_global_dumper == nullptr, "Error");
2121     _global_dumper = this;
2122   }
2123   void set_global_writer() {
2124     assert(_global_writer == nullptr, "Error");
2125     _global_writer = _local_writer;
2126   }
2127   void clear_global_dumper() { _global_dumper = nullptr; }
2128   void clear_global_writer() { _global_writer = nullptr; }
2129 
2130   bool skip_operation() const;
2131 
2132   // create dump writer for every parallel dump thread
2133   DumpWriter* create_local_writer();
2134 
2135   // writes a HPROF_LOAD_CLASS record
2136   static void do_load_class(Klass* k);
2137 
2138   // writes a HPROF_GC_CLASS_DUMP record for the given class
2139   static void do_class_dump(Klass* k);
2140 
2141   // HPROF_GC_ROOT_THREAD_OBJ records for platform and mounted virtual threads
2142   void dump_threads();
2143 
2144   void add_class_serial_number(Klass* k, int serial_num) {
2145     _klass_map->at_put_grow(serial_num, k);
2146   }
2147 
2148   bool is_oom_thread(JavaThread* thread) const {
2149     return thread == _oome_thread && _oome_constructor != nullptr;
2150   }
2151 
2152   // HPROF_TRACE and HPROF_FRAME records for platform and mounted virtual threads
2153   void dump_stack_traces();
2154 
2155  public:
2156   VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome, uint num_dump_threads) :
2157     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
2158                     GCCause::_heap_dump /* GC Cause */,
2159                     0 /* total full collections, dummy, ignored */,
2160                     gc_before_heap_dump),
2161     WorkerTask("dump heap") {
2162     _local_writer = writer;
2163     _gc_before_heap_dump = gc_before_heap_dump;
2164     _klass_map = new (mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
2165 
2166     _thread_dumpers = nullptr;
2167     _thread_dumpers_count = 0;
2168     _thread_serial_num = 1;
2169     _frame_serial_num = 1;
2170 
2171     _dump_seq = 0;
2172     _num_dumper_threads = num_dump_threads;
2173     _dumper_controller = nullptr;
2174     _poi = nullptr;
2175     if (oome) {
2176       assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
2177       // get OutOfMemoryError zero-parameter constructor
2178       InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
2179       _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
2180                                                           vmSymbols::void_method_signature());
2181       // get thread throwing OOME when generating the heap dump at OOME
2182       _oome_thread = JavaThread::current();
2183     } else {
2184       _oome_thread = nullptr;
2185       _oome_constructor = nullptr;
2186     }
2187   }
2188 
2189   ~VM_HeapDumper() {
2190     if (_thread_dumpers != nullptr) {
2191       for (int i = 0; i < _thread_dumpers_count; i++) {
2192         delete _thread_dumpers[i];
2193       }
2194       FREE_C_HEAP_ARRAY(ThreadDumper*, _thread_dumpers);
2195     }
2196 
2197     if (_dumper_controller != nullptr) {
2198       delete _dumper_controller;
2199       _dumper_controller = nullptr;
2200     }
2201     delete _klass_map;
2202   }
2203   int dump_seq()           { return _dump_seq; }
2204   bool is_parallel_dump()  { return _num_dumper_threads > 1; }
2205   bool can_parallel_dump(WorkerThreads* workers);
2206 
2207   VMOp_Type type() const { return VMOp_HeapDumper; }
2208   virtual bool doit_prologue();
2209   void doit();
2210   void work(uint worker_id);
2211 };
2212 
2213 VM_HeapDumper* VM_HeapDumper::_global_dumper = nullptr;
2214 DumpWriter*    VM_HeapDumper::_global_writer = nullptr;
2215 
2216 bool VM_HeapDumper::skip_operation() const {
2217   return false;
2218 }
2219 
2220 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
2221 void DumperSupport::end_of_dump(AbstractDumpWriter* writer) {
2222   writer->finish_dump_segment();
2223 
2224   writer->write_u1(HPROF_HEAP_DUMP_END);
2225   writer->write_u4(0);
2226   writer->write_u4(0);
2227 }
2228 
2229 // writes a HPROF_LOAD_CLASS record for the class
2230 void VM_HeapDumper::do_load_class(Klass* k) {
2231   static u4 class_serial_num = 0;
2232 
2233   // len of HPROF_LOAD_CLASS record
2234   u4 remaining = 2*oopSize + 2*sizeof(u4);
2235 
2236   DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
2237 
2238   // class serial number is just a number
2239   writer()->write_u4(++class_serial_num);
2240 
2241   // class ID
2242   writer()->write_classID(k);
2243 
2244   // add the Klass* and class serial number pair
2245   dumper()->add_class_serial_number(k, class_serial_num);
2246 
2247   writer()->write_u4(STACK_TRACE_ID);
2248 
2249   // class name ID
2250   Symbol* name = k->name();
2251   writer()->write_symbolID(name);
2252 }
2253 
2254 // writes a HPROF_GC_CLASS_DUMP record for the given class
2255 void VM_HeapDumper::do_class_dump(Klass* k) {
2256   if (k->is_instance_klass()) {
2257     DumperSupport::dump_instance_class(writer(), k);
2258   } else {
2259     DumperSupport::dump_array_class(writer(), k);
2260   }
2261 }
2262 
2263 // Write a HPROF_GC_ROOT_THREAD_OBJ record for platform/carrier and mounted virtual threads.
2264 // Then walk the stack so that locals and JNI locals are dumped.
2265 void VM_HeapDumper::dump_threads() {
2266     for (int i = 0; i < _thread_dumpers_count; i++) {
2267         _thread_dumpers[i]->dump_thread_obj(writer());
2268         _thread_dumpers[i]->dump_stack_refs(writer());
2269     }
2270 }
2271 
2272 bool VM_HeapDumper::doit_prologue() {
2273   if (_gc_before_heap_dump && UseZGC) {
2274     // ZGC cannot perform a synchronous GC cycle from within the VM thread.
2275     // So ZCollectedHeap::collect_as_vm_thread() is a noop. To respect the
2276     // _gc_before_heap_dump flag a synchronous GC cycle is performed from
2277     // the caller thread in the prologue.
2278     Universe::heap()->collect(GCCause::_heap_dump);
2279   }
2280   return VM_GC_Operation::doit_prologue();
2281 }
2282 
2283 bool VM_HeapDumper::can_parallel_dump(WorkerThreads* workers) {
2284   bool can_parallel = true;
2285   uint num_active_workers = workers != nullptr ? workers->active_workers() : 0;
2286   uint num_requested_dump_threads = _num_dumper_threads;
2287   // check if we can dump in parallel based on requested and active threads
2288   if (num_active_workers <= 1 || num_requested_dump_threads <= 1) {
2289     _num_dumper_threads = 1;
2290     can_parallel = false;
2291   } else {
2292     // check if we have extra path room to accommodate segmented heap files
2293     const char* base_path = writer()->get_file_path();
2294     assert(base_path != nullptr, "sanity check");
2295     if ((strlen(base_path) + 7/*.p\d\d\d\d\0*/) >= JVM_MAXPATHLEN) {
2296       _num_dumper_threads = 1;
2297       can_parallel = false;
2298     } else {
2299       _num_dumper_threads = clamp(num_requested_dump_threads, 2U, num_active_workers);
2300     }
2301   }
2302 
2303   log_info(heapdump)("Requested dump threads %u, active dump threads %u, "
2304                      "actual dump threads %u, parallelism %s",
2305                      num_requested_dump_threads, num_active_workers,
2306                      _num_dumper_threads, can_parallel ? "true" : "false");
2307   return can_parallel;
2308 }
2309 
2310 // The VM operation that dumps the heap. The dump consists of the following
2311 // records:
2312 //
2313 //  HPROF_HEADER
2314 //  [HPROF_UTF8]*
2315 //  [HPROF_LOAD_CLASS]*
2316 //  [[HPROF_FRAME]*|HPROF_TRACE]*
2317 //  [HPROF_GC_CLASS_DUMP]*
2318 //  [HPROF_HEAP_DUMP_SEGMENT]*
2319 //  HPROF_HEAP_DUMP_END
2320 //
2321 // The HPROF_TRACE records represent the stack traces where the heap dump
2322 // is generated and a "dummy trace" record which does not include
2323 // any frames. The dummy trace record is used to be referenced as the
2324 // unknown object alloc site.
2325 //
2326 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
2327 // To allow the heap dump be generated in a single pass we remember the position
2328 // of the dump length and fix it up after all sub-records have been written.
2329 // To generate the sub-records we iterate over the heap, writing
2330 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
2331 // records as we go. Once that is done we write records for some of the GC
2332 // roots.
2333 
2334 void VM_HeapDumper::doit() {
2335 
2336   CollectedHeap* ch = Universe::heap();
2337 
2338   ch->ensure_parsability(false); // must happen, even if collection does
2339                                  // not happen (e.g. due to GCLocker)
2340 
2341   if (_gc_before_heap_dump) {
2342     if (GCLocker::is_active()) {
2343       warning("GC locker is held; pre-heapdump GC was skipped");
2344     } else {
2345       ch->collect_as_vm_thread(GCCause::_heap_dump);
2346     }
2347   }
2348 
2349   // At this point we should be the only dumper active, so
2350   // the following should be safe.
2351   set_global_dumper();
2352   set_global_writer();
2353 
2354   WorkerThreads* workers = ch->safepoint_workers();
2355   if (!can_parallel_dump(workers)) {
2356     work(VMDumperWorkerId);
2357   } else {
2358     uint heap_only_dumper_threads = _num_dumper_threads - 1 /* VMDumper thread */;
2359     _dumper_controller = new (std::nothrow) DumperController(heap_only_dumper_threads);
2360     ParallelObjectIterator poi(_num_dumper_threads);
2361     _poi = &poi;
2362     workers->run_task(this, _num_dumper_threads);
2363     _poi = nullptr;
2364   }
2365 
2366   // Now we clear the global variables, so that a future dumper can run.
2367   clear_global_dumper();
2368   clear_global_writer();
2369 }
2370 
2371 // prepare DumpWriter for every parallel dump thread
2372 DumpWriter* VM_HeapDumper::create_local_writer() {
2373   char* path = NEW_RESOURCE_ARRAY(char, JVM_MAXPATHLEN);
2374   memset(path, 0, JVM_MAXPATHLEN);
2375 
2376   // generate segmented heap file path
2377   const char* base_path = writer()->get_file_path();
2378   // share global compressor, local DumpWriter is not responsible for its life cycle
2379   AbstractCompressor* compressor = writer()->compressor();
2380   int seq = Atomic::fetch_then_add(&_dump_seq, 1);
2381   os::snprintf(path, JVM_MAXPATHLEN, "%s.p%d", base_path, seq);
2382 
2383   // create corresponding writer for that
2384   DumpWriter* local_writer = new DumpWriter(path, writer()->is_overwrite(), compressor);
2385   return local_writer;
2386 }
2387 
2388 void VM_HeapDumper::work(uint worker_id) {
2389   // VM Dumper works on all non-heap data dumping and part of heap iteration.
2390   if (is_vm_dumper(worker_id)) {
2391     TraceTime timer("Dump non-objects", TRACETIME_LOG(Info, heapdump));
2392     // Write the file header - we always use 1.0.2
2393     const char* header = "JAVA PROFILE 1.0.2";
2394 
2395     // header is few bytes long - no chance to overflow int
2396     writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2397     writer()->write_u4(oopSize);
2398     // timestamp is current time in ms
2399     writer()->write_u8(os::javaTimeMillis());
2400     // HPROF_UTF8 records
2401     SymbolTableDumper sym_dumper(writer());
2402     SymbolTable::symbols_do(&sym_dumper);
2403 
2404     // write HPROF_LOAD_CLASS records
2405     {
2406       LockedClassesDo locked_load_classes(&do_load_class);
2407       ClassLoaderDataGraph::classes_do(&locked_load_classes);
2408     }
2409 
2410     // write HPROF_FRAME and HPROF_TRACE records
2411     // this must be called after _klass_map is built when iterating the classes above.
2412     dump_stack_traces();
2413 
2414     // HPROF_HEAP_DUMP/HPROF_HEAP_DUMP_SEGMENT starts here
2415 
2416     // Writes HPROF_GC_CLASS_DUMP records
2417     {
2418       LockedClassesDo locked_dump_class(&do_class_dump);
2419       ClassLoaderDataGraph::classes_do(&locked_dump_class);
2420     }
2421 
2422     // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2423     dump_threads();
2424 
2425     // HPROF_GC_ROOT_JNI_GLOBAL
2426     JNIGlobalsDumper jni_dumper(writer());
2427     JNIHandles::oops_do(&jni_dumper);
2428     // technically not jni roots, but global roots
2429     // for things like preallocated throwable backtraces
2430     Universe::vm_global()->oops_do(&jni_dumper);
2431     // HPROF_GC_ROOT_STICKY_CLASS
2432     // These should be classes in the null class loader data, and not all classes
2433     // if !ClassUnloading
2434     StickyClassDumper class_dumper(writer());
2435     ClassLoaderData::the_null_class_loader_data()->classes_do(&class_dumper);
2436   }
2437 
2438   // Heap iteration.
2439   // writes HPROF_GC_INSTANCE_DUMP records.
2440   // After each sub-record is written check_segment_length will be invoked
2441   // to check if the current segment exceeds a threshold. If so, a new
2442   // segment is started.
2443   // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
2444   // of the heap dump.
2445   if (!is_parallel_dump()) {
2446     assert(is_vm_dumper(worker_id), "must be");
2447     // == Serial dump
2448     ResourceMark rm;
2449     TraceTime timer("Dump heap objects", TRACETIME_LOG(Info, heapdump));
2450     HeapObjectDumper obj_dumper(writer());
2451     Universe::heap()->object_iterate(&obj_dumper);
2452     writer()->finish_dump_segment();
2453     // Writes the HPROF_HEAP_DUMP_END record because merge does not happen in serial dump
2454     DumperSupport::end_of_dump(writer());
2455     writer()->flush();
2456   } else {
2457     // == Parallel dump
2458     ResourceMark rm;
2459     TraceTime timer("Dump heap objects in parallel", TRACETIME_LOG(Info, heapdump));
2460     DumpWriter* local_writer = is_vm_dumper(worker_id) ? writer() : create_local_writer();
2461     if (!local_writer->has_error()) {
2462       HeapObjectDumper obj_dumper(local_writer);
2463       _poi->object_iterate(&obj_dumper, worker_id);
2464       local_writer->finish_dump_segment();
2465       local_writer->flush();
2466     }
2467     if (is_vm_dumper(worker_id)) {
2468       _dumper_controller->wait_all_dumpers_complete();
2469     } else {
2470       _dumper_controller->dumper_complete(local_writer, writer());
2471       delete local_writer;
2472       return;
2473     }
2474   }
2475   // At this point, all fragments of the heapdump have been written to separate files.
2476   // We need to merge them into a complete heapdump and write HPROF_HEAP_DUMP_END at that time.
2477 }
2478 
2479 void VM_HeapDumper::dump_stack_traces() {
2480   // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2481   DumperSupport::write_header(writer(), HPROF_TRACE, 3 * sizeof(u4));
2482   writer()->write_u4((u4)STACK_TRACE_ID);
2483   writer()->write_u4(0);                    // thread number
2484   writer()->write_u4(0);                    // frame count
2485 
2486   // max number if every platform thread is carrier with mounted virtual thread
2487   _thread_dumpers = NEW_C_HEAP_ARRAY(ThreadDumper*, Threads::number_of_threads() * 2, mtInternal);
2488 
2489   for (JavaThreadIteratorWithHandle jtiwh; JavaThread * thread = jtiwh.next(); ) {
2490     if (ThreadDumper::should_dump_pthread(thread)) {
2491       bool add_oom_frame = is_oom_thread(thread);
2492 
2493       oop mounted_vt = thread->is_vthread_mounted() ? thread->vthread() : nullptr;
2494       if (mounted_vt != nullptr && !ThreadDumper::should_dump_vthread(mounted_vt)) {
2495         mounted_vt = nullptr;
2496       }
2497 
2498       // mounted vthread (if any)
2499       if (mounted_vt != nullptr) {
2500         ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::MountedVirtual, thread, mounted_vt);
2501         _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2502         if (add_oom_frame) {
2503           thread_dumper->add_oom_frame(_oome_constructor);
2504           // we add oom frame to the VT stack, don't add it to the carrier thread stack
2505           add_oom_frame = false;
2506         }
2507         thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2508         thread_dumper->dump_stack_traces(writer(), _klass_map);
2509       }
2510 
2511       // platform or carrier thread
2512       ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::Platform, thread, thread->threadObj());
2513       _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2514       if (add_oom_frame) {
2515         thread_dumper->add_oom_frame(_oome_constructor);
2516       }
2517       thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2518       thread_dumper->dump_stack_traces(writer(), _klass_map);
2519     }
2520   }
2521 }
2522 
2523 // dump the heap to given path.
2524 int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite, uint num_dump_threads) {
2525   assert(path != nullptr && strlen(path) > 0, "path missing");
2526 
2527   // print message in interactive case
2528   if (out != nullptr) {
2529     out->print_cr("Dumping heap to %s ...", path);
2530     timer()->start();
2531   }
2532   // create JFR event
2533   EventHeapDump event;
2534 
2535   AbstractCompressor* compressor = nullptr;
2536 
2537   if (compression > 0) {
2538     compressor = new (std::nothrow) GZipCompressor(compression);
2539 
2540     if (compressor == nullptr) {
2541       set_error("Could not allocate gzip compressor");
2542       return -1;
2543     }
2544   }
2545 
2546   DumpWriter writer(path, overwrite, compressor);
2547 
2548   if (writer.error() != nullptr) {
2549     set_error(writer.error());
2550     if (out != nullptr) {
2551       out->print_cr("Unable to create %s: %s", path,
2552         (error() != nullptr) ? error() : "reason unknown");
2553     }
2554     return -1;
2555   }
2556 
2557   // generate the segmented heap dump into separate files
2558   VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome, num_dump_threads);
2559   VMThread::execute(&dumper);
2560 
2561   // record any error that the writer may have encountered
2562   set_error(writer.error());
2563 
2564   // For serial dump, once VM_HeapDumper completes, the whole heap dump process
2565   // is done, no further phases needed. For parallel dump, the whole heap dump
2566   // process is done in two phases
2567   //
2568   // Phase 1: Concurrent threads directly write heap data to multiple heap files.
2569   //          This is done by VM_HeapDumper, which is performed within safepoint.
2570   //
2571   // Phase 2: Merge multiple heap files into one complete heap dump file.
2572   //          This is done by DumpMerger, which is performed outside safepoint
2573   if (dumper.is_parallel_dump()) {
2574     DumpMerger merger(path, &writer, dumper.dump_seq());
2575     Thread* current_thread = Thread::current();
2576     if (current_thread->is_AttachListener_thread()) {
2577       // perform heapdump file merge operation in the current thread prevents us
2578       // from occupying the VM Thread, which in turn affects the occurrence of
2579       // GC and other VM operations.
2580       merger.do_merge();
2581     } else {
2582       // otherwise, performs it by VM thread
2583       VM_HeapDumpMerge op(&merger);
2584       VMThread::execute(&op);
2585     }
2586     set_error(writer.error());
2587   }
2588 
2589   // emit JFR event
2590   if (error() == nullptr) {
2591     event.set_destination(path);
2592     event.set_gcBeforeDump(_gc_before_heap_dump);
2593     event.set_size(writer.bytes_written());
2594     event.set_onOutOfMemoryError(_oome);
2595     event.set_overwrite(overwrite);
2596     event.set_compression(compression);
2597     event.commit();
2598   } else {
2599     log_debug(cds, heap)("Error %s while dumping heap", error());
2600   }
2601 
2602   // print message in interactive case
2603   if (out != nullptr) {
2604     timer()->stop();
2605     if (error() == nullptr) {
2606       out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
2607                     writer.bytes_written(), timer()->seconds());
2608     } else {
2609       out->print_cr("Dump file is incomplete: %s", writer.error());
2610     }
2611   }
2612 
2613   if (compressor != nullptr) {
2614     delete compressor;
2615   }
2616   return (writer.error() == nullptr) ? 0 : -1;
2617 }
2618 
2619 // stop timer (if still active), and free any error string we might be holding
2620 HeapDumper::~HeapDumper() {
2621   if (timer()->is_active()) {
2622     timer()->stop();
2623   }
2624   set_error(nullptr);
2625 }
2626 
2627 
2628 // returns the error string (resource allocated), or null
2629 char* HeapDumper::error_as_C_string() const {
2630   if (error() != nullptr) {
2631     char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
2632     strcpy(str, error());
2633     return str;
2634   } else {
2635     return nullptr;
2636   }
2637 }
2638 
2639 // set the error string
2640 void HeapDumper::set_error(char const* error) {
2641   if (_error != nullptr) {
2642     os::free(_error);
2643   }
2644   if (error == nullptr) {
2645     _error = nullptr;
2646   } else {
2647     _error = os::strdup(error);
2648     assert(_error != nullptr, "allocation failure");
2649   }
2650 }
2651 
2652 // Called by out-of-memory error reporting by a single Java thread
2653 // outside of a JVM safepoint
2654 void HeapDumper::dump_heap_from_oome() {
2655   HeapDumper::dump_heap(true);
2656 }
2657 
2658 // Called by error reporting by a single Java thread outside of a JVM safepoint,
2659 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
2660 // callers are strictly serialized and guaranteed not to interfere below. For more
2661 // general use, however, this method will need modification to prevent
2662 // inteference when updating the static variables base_path and dump_file_seq below.
2663 void HeapDumper::dump_heap() {
2664   HeapDumper::dump_heap(false);
2665 }
2666 
2667 void HeapDumper::dump_heap(bool oome) {
2668   static char base_path[JVM_MAXPATHLEN] = {'\0'};
2669   static uint dump_file_seq = 0;
2670   char* my_path;
2671   const int max_digit_chars = 20;
2672 
2673   const char* dump_file_name = "java_pid";
2674   const char* dump_file_ext  = HeapDumpGzipLevel > 0 ? ".hprof.gz" : ".hprof";
2675 
2676   // The dump file defaults to java_pid<pid>.hprof in the current working
2677   // directory. HeapDumpPath=<file> can be used to specify an alternative
2678   // dump file name or a directory where dump file is created.
2679   if (dump_file_seq == 0) { // first time in, we initialize base_path
2680     // Calculate potentially longest base path and check if we have enough
2681     // allocated statically.
2682     const size_t total_length =
2683                       (HeapDumpPath == nullptr ? 0 : strlen(HeapDumpPath)) +
2684                       strlen(os::file_separator()) + max_digit_chars +
2685                       strlen(dump_file_name) + strlen(dump_file_ext) + 1;
2686     if (total_length > sizeof(base_path)) {
2687       warning("Cannot create heap dump file.  HeapDumpPath is too long.");
2688       return;
2689     }
2690 
2691     bool use_default_filename = true;
2692     if (HeapDumpPath == nullptr || HeapDumpPath[0] == '\0') {
2693       // HeapDumpPath=<file> not specified
2694     } else {
2695       strcpy(base_path, HeapDumpPath);
2696       // check if the path is a directory (must exist)
2697       DIR* dir = os::opendir(base_path);
2698       if (dir == nullptr) {
2699         use_default_filename = false;
2700       } else {
2701         // HeapDumpPath specified a directory. We append a file separator
2702         // (if needed).
2703         os::closedir(dir);
2704         size_t fs_len = strlen(os::file_separator());
2705         if (strlen(base_path) >= fs_len) {
2706           char* end = base_path;
2707           end += (strlen(base_path) - fs_len);
2708           if (strcmp(end, os::file_separator()) != 0) {
2709             strcat(base_path, os::file_separator());
2710           }
2711         }
2712       }
2713     }
2714     // If HeapDumpPath wasn't a file name then we append the default name
2715     if (use_default_filename) {
2716       const size_t dlen = strlen(base_path);  // if heap dump dir specified
2717       jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s",
2718                    dump_file_name, os::current_process_id(), dump_file_ext);
2719     }
2720     const size_t len = strlen(base_path) + 1;
2721     my_path = (char*)os::malloc(len, mtInternal);
2722     if (my_path == nullptr) {
2723       warning("Cannot create heap dump file.  Out of system memory.");
2724       return;
2725     }
2726     strncpy(my_path, base_path, len);
2727   } else {
2728     // Append a sequence number id for dumps following the first
2729     const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
2730     my_path = (char*)os::malloc(len, mtInternal);
2731     if (my_path == nullptr) {
2732       warning("Cannot create heap dump file.  Out of system memory.");
2733       return;
2734     }
2735     jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
2736   }
2737   dump_file_seq++;   // increment seq number for next time we dump
2738 
2739   HeapDumper dumper(false /* no GC before heap dump */,
2740                     oome  /* pass along out-of-memory-error flag */);
2741   dumper.dump(my_path, tty, HeapDumpGzipLevel);
2742   os::free(my_path);
2743 }