1 /* 2 * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "jvm.h" 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/systemDictionary.hpp" 32 #include "classfile/vmSymbols.hpp" 33 #include "gc/shared/gcLocker.hpp" 34 #include "gc/shared/gcVMOperations.hpp" 35 #include "jfr/jfrEvents.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "memory/universe.hpp" 39 #include "oops/objArrayKlass.hpp" 40 #include "oops/objArrayOop.inline.hpp" 41 #include "oops/oop.inline.hpp" 42 #include "oops/typeArrayOop.inline.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/handles.inline.hpp" 45 #include "runtime/javaCalls.hpp" 46 #include "runtime/jniHandles.hpp" 47 #include "runtime/os.inline.hpp" 48 #include "runtime/reflectionUtils.hpp" 49 #include "runtime/thread.inline.hpp" 50 #include "runtime/threadSMR.hpp" 51 #include "runtime/vframe.hpp" 52 #include "runtime/vmThread.hpp" 53 #include "runtime/vmOperations.hpp" 54 #include "services/heapDumper.hpp" 55 #include "services/threadService.hpp" 56 #include "utilities/macros.hpp" 57 #include "utilities/ostream.hpp" 58 59 /* 60 * HPROF binary format - description copied from: 61 * src/share/demo/jvmti/hprof/hprof_io.c 62 * 63 * 64 * header "JAVA PROFILE 1.0.2" (0-terminated) 65 * 66 * u4 size of identifiers. Identifiers are used to represent 67 * UTF8 strings, objects, stack traces, etc. They usually 68 * have the same size as host pointers. For example, on 69 * Solaris and Win32, the size is 4. 70 * u4 high word 71 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70 72 * [record]* a sequence of records. 73 * 74 * 75 * Record format: 76 * 77 * u1 a TAG denoting the type of the record 78 * u4 number of *microseconds* since the time stamp in the 79 * header. (wraps around in a little more than an hour) 80 * u4 number of bytes *remaining* in the record. Note that 81 * this number excludes the tag and the length field itself. 82 * [u1]* BODY of the record (a sequence of bytes) 83 * 84 * 85 * The following TAGs are supported: 86 * 87 * TAG BODY notes 88 *---------------------------------------------------------- 89 * HPROF_UTF8 a UTF8-encoded name 90 * 91 * id name ID 92 * [u1]* UTF8 characters (no trailing zero) 93 * 94 * HPROF_LOAD_CLASS a newly loaded class 95 * 96 * u4 class serial number (> 0) 97 * id class object ID 98 * u4 stack trace serial number 99 * id class name ID 100 * 101 * HPROF_UNLOAD_CLASS an unloading class 102 * 103 * u4 class serial_number 104 * 105 * HPROF_FRAME a Java stack frame 106 * 107 * id stack frame ID 108 * id method name ID 109 * id method signature ID 110 * id source file name ID 111 * u4 class serial number 112 * i4 line number. >0: normal 113 * -1: unknown 114 * -2: compiled method 115 * -3: native method 116 * 117 * HPROF_TRACE a Java stack trace 118 * 119 * u4 stack trace serial number 120 * u4 thread serial number 121 * u4 number of frames 122 * [id]* stack frame IDs 123 * 124 * 125 * HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC 126 * 127 * u2 flags 0x0001: incremental vs. complete 128 * 0x0002: sorted by allocation vs. live 129 * 0x0004: whether to force a GC 130 * u4 cutoff ratio 131 * u4 total live bytes 132 * u4 total live instances 133 * u8 total bytes allocated 134 * u8 total instances allocated 135 * u4 number of sites that follow 136 * [u1 is_array: 0: normal object 137 * 2: object array 138 * 4: boolean array 139 * 5: char array 140 * 6: float array 141 * 7: double array 142 * 8: byte array 143 * 9: short array 144 * 10: int array 145 * 11: long array 146 * u4 class serial number (may be zero during startup) 147 * u4 stack trace serial number 148 * u4 number of bytes alive 149 * u4 number of instances alive 150 * u4 number of bytes allocated 151 * u4]* number of instance allocated 152 * 153 * HPROF_START_THREAD a newly started thread. 154 * 155 * u4 thread serial number (> 0) 156 * id thread object ID 157 * u4 stack trace serial number 158 * id thread name ID 159 * id thread group name ID 160 * id thread group parent name ID 161 * 162 * HPROF_END_THREAD a terminating thread. 163 * 164 * u4 thread serial number 165 * 166 * HPROF_HEAP_SUMMARY heap summary 167 * 168 * u4 total live bytes 169 * u4 total live instances 170 * u8 total bytes allocated 171 * u8 total instances allocated 172 * 173 * HPROF_HEAP_DUMP denote a heap dump 174 * 175 * [heap dump sub-records]* 176 * 177 * There are four kinds of heap dump sub-records: 178 * 179 * u1 sub-record type 180 * 181 * HPROF_GC_ROOT_UNKNOWN unknown root 182 * 183 * id object ID 184 * 185 * HPROF_GC_ROOT_THREAD_OBJ thread object 186 * 187 * id thread object ID (may be 0 for a 188 * thread newly attached through JNI) 189 * u4 thread sequence number 190 * u4 stack trace sequence number 191 * 192 * HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root 193 * 194 * id object ID 195 * id JNI global ref ID 196 * 197 * HPROF_GC_ROOT_JNI_LOCAL JNI local ref 198 * 199 * id object ID 200 * u4 thread serial number 201 * u4 frame # in stack trace (-1 for empty) 202 * 203 * HPROF_GC_ROOT_JAVA_FRAME Java stack frame 204 * 205 * id object ID 206 * u4 thread serial number 207 * u4 frame # in stack trace (-1 for empty) 208 * 209 * HPROF_GC_ROOT_NATIVE_STACK Native stack 210 * 211 * id object ID 212 * u4 thread serial number 213 * 214 * HPROF_GC_ROOT_STICKY_CLASS System class 215 * 216 * id object ID 217 * 218 * HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block 219 * 220 * id object ID 221 * u4 thread serial number 222 * 223 * HPROF_GC_ROOT_MONITOR_USED Busy monitor 224 * 225 * id object ID 226 * 227 * HPROF_GC_CLASS_DUMP dump of a class object 228 * 229 * id class object ID 230 * u4 stack trace serial number 231 * id super class object ID 232 * id class loader object ID 233 * id signers object ID 234 * id protection domain object ID 235 * id reserved 236 * id reserved 237 * 238 * u4 instance size (in bytes) 239 * 240 * u2 size of constant pool 241 * [u2, constant pool index, 242 * ty, type 243 * 2: object 244 * 4: boolean 245 * 5: char 246 * 6: float 247 * 7: double 248 * 8: byte 249 * 9: short 250 * 10: int 251 * 11: long 252 * vl]* and value 253 * 254 * u2 number of static fields 255 * [id, static field name, 256 * ty, type, 257 * vl]* and value 258 * 259 * u2 number of inst. fields (not inc. super) 260 * [id, instance field name, 261 * ty]* type 262 * 263 * HPROF_GC_INSTANCE_DUMP dump of a normal object 264 * 265 * id object ID 266 * u4 stack trace serial number 267 * id class object ID 268 * u4 number of bytes that follow 269 * [vl]* instance field values (class, followed 270 * by super, super's super ...) 271 * 272 * HPROF_GC_OBJ_ARRAY_DUMP dump of an object array 273 * 274 * id array object ID 275 * u4 stack trace serial number 276 * u4 number of elements 277 * id array class ID 278 * [id]* elements 279 * 280 * HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array 281 * 282 * id array object ID 283 * u4 stack trace serial number 284 * u4 number of elements 285 * u1 element type 286 * 4: boolean array 287 * 5: char array 288 * 6: float array 289 * 7: double array 290 * 8: byte array 291 * 9: short array 292 * 10: int array 293 * 11: long array 294 * [u1]* elements 295 * 296 * HPROF_CPU_SAMPLES a set of sample traces of running threads 297 * 298 * u4 total number of samples 299 * u4 # of traces 300 * [u4 # of samples 301 * u4]* stack trace serial number 302 * 303 * HPROF_CONTROL_SETTINGS the settings of on/off switches 304 * 305 * u4 0x00000001: alloc traces on/off 306 * 0x00000002: cpu sampling on/off 307 * u2 stack trace depth 308 * 309 * 310 * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally 311 * be generated as a sequence of heap dump segments. This sequence is 312 * terminated by an end record. The additional tags allowed by format 313 * "JAVA PROFILE 1.0.2" are: 314 * 315 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment 316 * 317 * [heap dump sub-records]* 318 * The same sub-record types allowed by HPROF_HEAP_DUMP 319 * 320 * HPROF_HEAP_DUMP_END denotes the end of a heap dump 321 * 322 */ 323 324 325 // HPROF tags 326 327 typedef enum { 328 // top-level records 329 HPROF_UTF8 = 0x01, 330 HPROF_LOAD_CLASS = 0x02, 331 HPROF_UNLOAD_CLASS = 0x03, 332 HPROF_FRAME = 0x04, 333 HPROF_TRACE = 0x05, 334 HPROF_ALLOC_SITES = 0x06, 335 HPROF_HEAP_SUMMARY = 0x07, 336 HPROF_START_THREAD = 0x0A, 337 HPROF_END_THREAD = 0x0B, 338 HPROF_HEAP_DUMP = 0x0C, 339 HPROF_CPU_SAMPLES = 0x0D, 340 HPROF_CONTROL_SETTINGS = 0x0E, 341 342 // 1.0.2 record types 343 HPROF_HEAP_DUMP_SEGMENT = 0x1C, 344 HPROF_HEAP_DUMP_END = 0x2C, 345 346 // field types 347 HPROF_ARRAY_OBJECT = 0x01, 348 HPROF_NORMAL_OBJECT = 0x02, 349 HPROF_BOOLEAN = 0x04, 350 HPROF_CHAR = 0x05, 351 HPROF_FLOAT = 0x06, 352 HPROF_DOUBLE = 0x07, 353 HPROF_BYTE = 0x08, 354 HPROF_SHORT = 0x09, 355 HPROF_INT = 0x0A, 356 HPROF_LONG = 0x0B, 357 358 // data-dump sub-records 359 HPROF_GC_ROOT_UNKNOWN = 0xFF, 360 HPROF_GC_ROOT_JNI_GLOBAL = 0x01, 361 HPROF_GC_ROOT_JNI_LOCAL = 0x02, 362 HPROF_GC_ROOT_JAVA_FRAME = 0x03, 363 HPROF_GC_ROOT_NATIVE_STACK = 0x04, 364 HPROF_GC_ROOT_STICKY_CLASS = 0x05, 365 HPROF_GC_ROOT_THREAD_BLOCK = 0x06, 366 HPROF_GC_ROOT_MONITOR_USED = 0x07, 367 HPROF_GC_ROOT_THREAD_OBJ = 0x08, 368 HPROF_GC_CLASS_DUMP = 0x20, 369 HPROF_GC_INSTANCE_DUMP = 0x21, 370 HPROF_GC_OBJ_ARRAY_DUMP = 0x22, 371 HPROF_GC_PRIM_ARRAY_DUMP = 0x23 372 } hprofTag; 373 374 // Default stack trace ID (used for dummy HPROF_TRACE record) 375 enum { 376 STACK_TRACE_ID = 1, 377 INITIAL_CLASS_COUNT = 200 378 }; 379 380 // Supports I/O operations on a dump file 381 382 class DumpWriter : public StackObj { 383 private: 384 enum { 385 io_buffer_max_size = 8*M, 386 io_buffer_min_size = 64*K, 387 dump_segment_header_size = 9 388 }; 389 390 int _fd; // file descriptor (-1 if dump file not open) 391 julong _bytes_written; // number of byte written to dump file 392 393 char* _buffer; // internal buffer 394 size_t _size; 395 size_t _pos; 396 397 bool _in_dump_segment; // Are we currently in a dump segment? 398 bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size? 399 DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record. 400 DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record(). 401 402 char* _error; // error message when I/O fails 403 404 void set_file_descriptor(int fd) { _fd = fd; } 405 int file_descriptor() const { return _fd; } 406 407 bool is_open() const { return file_descriptor() >= 0; } 408 void flush(); 409 410 char* buffer() const { return _buffer; } 411 size_t buffer_size() const { return _size; } 412 size_t position() const { return _pos; } 413 void set_position(size_t pos) { _pos = pos; } 414 415 void set_error(const char* error) { _error = (char*)os::strdup(error); } 416 417 // all I/O go through this function 418 void write_internal(void* s, size_t len); 419 420 public: 421 DumpWriter(const char* path); 422 ~DumpWriter(); 423 424 void close(); 425 426 // total number of bytes written to the disk 427 julong bytes_written() const { return _bytes_written; } 428 429 char* error() const { return _error; } 430 431 // writer functions 432 void write_raw(void* s, size_t len); 433 void write_u1(u1 x) { write_raw((void*)&x, 1); } 434 void write_u2(u2 x); 435 void write_u4(u4 x); 436 void write_u8(u8 x); 437 void write_objectID(oop o); 438 void write_symbolID(Symbol* o); 439 void write_classID(Klass* k); 440 void write_id(u4 x); 441 442 // Start a new sub-record. Starts a new heap dump segment if needed. 443 void start_sub_record(u1 tag, u4 len); 444 // Ends the current sub-record. 445 void end_sub_record(); 446 // Finishes the current dump segment if not already finished. 447 void finish_dump_segment(); 448 }; 449 450 DumpWriter::DumpWriter(const char* path) : _fd(-1), _bytes_written(0), _pos(0), 451 _in_dump_segment(false), _error(NULL) { 452 // try to allocate an I/O buffer of io_buffer_size. If there isn't 453 // sufficient memory then reduce size until we can allocate something. 454 _size = io_buffer_max_size; 455 do { 456 _buffer = (char*)os::malloc(_size, mtInternal); 457 if (_buffer == NULL) { 458 _size = _size >> 1; 459 } 460 } while (_buffer == NULL && _size >= io_buffer_min_size); 461 462 if (_buffer == NULL) { 463 set_error("Could not allocate buffer memory for heap dump"); 464 } else { 465 _fd = os::create_binary_file(path, false); // don't replace existing file 466 467 // if the open failed we record the error 468 if (_fd < 0) { 469 set_error(os::strerror(errno)); 470 } 471 } 472 } 473 474 DumpWriter::~DumpWriter() { 475 close(); 476 os::free(_buffer); 477 os::free(_error); 478 } 479 480 // closes dump file (if open) 481 void DumpWriter::close() { 482 // flush and close dump file 483 if (is_open()) { 484 flush(); 485 os::close(file_descriptor()); 486 set_file_descriptor(-1); 487 } 488 } 489 490 // write directly to the file 491 void DumpWriter::write_internal(void* s, size_t len) { 492 if (is_open()) { 493 const char* pos = (char*)s; 494 ssize_t n = 0; 495 while (len > 0) { 496 uint tmp = (uint)MIN2(len, (size_t)INT_MAX); 497 n = os::write(file_descriptor(), pos, tmp); 498 499 if (n < 0) { 500 // EINTR cannot happen here, os::write will take care of that 501 set_error(os::strerror(errno)); 502 os::close(file_descriptor()); 503 set_file_descriptor(-1); 504 return; 505 } 506 507 _bytes_written += n; 508 pos += n; 509 len -= n; 510 } 511 } 512 } 513 514 // write raw bytes 515 void DumpWriter::write_raw(void* s, size_t len) { 516 assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large"); 517 debug_only(_sub_record_left -= len); 518 519 // flush buffer to make room 520 if (len > buffer_size() - position()) { 521 assert(!_in_dump_segment || _is_huge_sub_record, "Cannot overflow in non-huge sub-record."); 522 flush(); 523 524 // If larger than the buffer, just write it directly. 525 if (len > buffer_size()) { 526 write_internal(s, len); 527 528 return; 529 } 530 } 531 532 memcpy(buffer() + position(), s, len); 533 set_position(position() + len); 534 } 535 536 // flush any buffered bytes to the file 537 void DumpWriter::flush() { 538 write_internal(buffer(), position()); 539 set_position(0); 540 } 541 542 void DumpWriter::write_u2(u2 x) { 543 u2 v; 544 Bytes::put_Java_u2((address)&v, x); 545 write_raw((void*)&v, 2); 546 } 547 548 void DumpWriter::write_u4(u4 x) { 549 u4 v; 550 Bytes::put_Java_u4((address)&v, x); 551 write_raw((void*)&v, 4); 552 } 553 554 void DumpWriter::write_u8(u8 x) { 555 u8 v; 556 Bytes::put_Java_u8((address)&v, x); 557 write_raw((void*)&v, 8); 558 } 559 560 void DumpWriter::write_objectID(oop o) { 561 address a = cast_from_oop<address>(o); 562 #ifdef _LP64 563 write_u8((u8)a); 564 #else 565 write_u4((u4)a); 566 #endif 567 } 568 569 void DumpWriter::write_symbolID(Symbol* s) { 570 address a = (address)((uintptr_t)s); 571 #ifdef _LP64 572 write_u8((u8)a); 573 #else 574 write_u4((u4)a); 575 #endif 576 } 577 578 void DumpWriter::write_id(u4 x) { 579 #ifdef _LP64 580 write_u8((u8) x); 581 #else 582 write_u4(x); 583 #endif 584 } 585 586 // We use java mirror as the class ID 587 void DumpWriter::write_classID(Klass* k) { 588 write_objectID(k->java_mirror()); 589 } 590 591 void DumpWriter::finish_dump_segment() { 592 if (_in_dump_segment) { 593 assert(_sub_record_left == 0, "Last sub-record not written completely"); 594 assert(_sub_record_ended, "sub-record must have ended"); 595 596 // Fix up the dump segment length if we haven't written a huge sub-record last 597 // (in which case the segment length was already set to the correct value initially). 598 if (!_is_huge_sub_record) { 599 assert(position() > dump_segment_header_size, "Dump segment should have some content"); 600 Bytes::put_Java_u4((address) (buffer() + 5), (u4) (position() - dump_segment_header_size)); 601 } 602 603 flush(); 604 _in_dump_segment = false; 605 } 606 } 607 608 void DumpWriter::start_sub_record(u1 tag, u4 len) { 609 if (!_in_dump_segment) { 610 if (position() > 0) { 611 flush(); 612 assert(position() == 0, "Must be at the start"); 613 } 614 615 write_u1(HPROF_HEAP_DUMP_SEGMENT); 616 write_u4(0); // timestamp 617 // Will be fixed up later if we add more sub-records. If this is a huge sub-record, 618 // this is already the correct length, since we don't add more sub-records. 619 write_u4(len); 620 _in_dump_segment = true; 621 _is_huge_sub_record = len > buffer_size() - dump_segment_header_size; 622 } else if (_is_huge_sub_record || (len > buffer_size() - position())) { 623 // This object will not fit in completely or the last sub-record was huge. 624 // Finish the current segement and try again. 625 finish_dump_segment(); 626 start_sub_record(tag, len); 627 628 return; 629 } 630 631 debug_only(_sub_record_left = len); 632 debug_only(_sub_record_ended = false); 633 634 write_u1(tag); 635 } 636 637 void DumpWriter::end_sub_record() { 638 assert(_in_dump_segment, "must be in dump segment"); 639 assert(_sub_record_left == 0, "sub-record not written completely"); 640 assert(!_sub_record_ended, "Must not have ended yet"); 641 debug_only(_sub_record_ended = true); 642 } 643 644 // Support class with a collection of functions used when dumping the heap 645 646 class DumperSupport : AllStatic { 647 public: 648 649 // write a header of the given type 650 static void write_header(DumpWriter* writer, hprofTag tag, u4 len); 651 652 // returns hprof tag for the given type signature 653 static hprofTag sig2tag(Symbol* sig); 654 // returns hprof tag for the given basic type 655 static hprofTag type2tag(BasicType type); 656 // Returns the size of the data to write. 657 static u4 sig2size(Symbol* sig); 658 659 // returns the size of the instance of the given class 660 static u4 instance_size(Klass* k); 661 662 // dump a jfloat 663 static void dump_float(DumpWriter* writer, jfloat f); 664 // dump a jdouble 665 static void dump_double(DumpWriter* writer, jdouble d); 666 // dumps the raw value of the given field 667 static void dump_field_value(DumpWriter* writer, char type, oop obj, int offset); 668 // returns the size of the static fields; also counts the static fields 669 static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count); 670 // dumps static fields of the given class 671 static void dump_static_fields(DumpWriter* writer, Klass* k); 672 // dump the raw values of the instance fields of the given object 673 static void dump_instance_fields(DumpWriter* writer, oop o); 674 // get the count of the instance fields for a given class 675 static u2 get_instance_fields_count(InstanceKlass* ik); 676 // dumps the definition of the instance fields for a given class 677 static void dump_instance_field_descriptors(DumpWriter* writer, Klass* k); 678 // creates HPROF_GC_INSTANCE_DUMP record for the given object 679 static void dump_instance(DumpWriter* writer, oop o); 680 // creates HPROF_GC_CLASS_DUMP record for the given class and each of its 681 // array classes 682 static void dump_class_and_array_classes(DumpWriter* writer, Klass* k); 683 // creates HPROF_GC_CLASS_DUMP record for a given primitive array 684 // class (and each multi-dimensional array class too) 685 static void dump_basic_type_array_class(DumpWriter* writer, Klass* k); 686 687 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array 688 static void dump_object_array(DumpWriter* writer, objArrayOop array); 689 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array 690 static void dump_prim_array(DumpWriter* writer, typeArrayOop array); 691 // create HPROF_FRAME record for the given method and bci 692 static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci); 693 694 // check if we need to truncate an array 695 static int calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size); 696 697 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record 698 static void end_of_dump(DumpWriter* writer); 699 700 static oop mask_dormant_archived_object(oop o) { 701 if (o != NULL && o->klass()->java_mirror() == NULL) { 702 // Ignore this object since the corresponding java mirror is not loaded. 703 // Might be a dormant archive object. 704 return NULL; 705 } else { 706 return o; 707 } 708 } 709 }; 710 711 // write a header of the given type 712 void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) { 713 writer->write_u1((u1)tag); 714 writer->write_u4(0); // current ticks 715 writer->write_u4(len); 716 } 717 718 // returns hprof tag for the given type signature 719 hprofTag DumperSupport::sig2tag(Symbol* sig) { 720 switch (sig->char_at(0)) { 721 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT; 722 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT; 723 case JVM_SIGNATURE_BYTE : return HPROF_BYTE; 724 case JVM_SIGNATURE_CHAR : return HPROF_CHAR; 725 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT; 726 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE; 727 case JVM_SIGNATURE_INT : return HPROF_INT; 728 case JVM_SIGNATURE_LONG : return HPROF_LONG; 729 case JVM_SIGNATURE_SHORT : return HPROF_SHORT; 730 case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN; 731 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE; 732 } 733 } 734 735 hprofTag DumperSupport::type2tag(BasicType type) { 736 switch (type) { 737 case T_BYTE : return HPROF_BYTE; 738 case T_CHAR : return HPROF_CHAR; 739 case T_FLOAT : return HPROF_FLOAT; 740 case T_DOUBLE : return HPROF_DOUBLE; 741 case T_INT : return HPROF_INT; 742 case T_LONG : return HPROF_LONG; 743 case T_SHORT : return HPROF_SHORT; 744 case T_BOOLEAN : return HPROF_BOOLEAN; 745 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE; 746 } 747 } 748 749 u4 DumperSupport::sig2size(Symbol* sig) { 750 switch (sig->char_at(0)) { 751 case JVM_SIGNATURE_CLASS: 752 case JVM_SIGNATURE_ARRAY: return sizeof(address); 753 case JVM_SIGNATURE_BOOLEAN: 754 case JVM_SIGNATURE_BYTE: return 1; 755 case JVM_SIGNATURE_SHORT: 756 case JVM_SIGNATURE_CHAR: return 2; 757 case JVM_SIGNATURE_INT: 758 case JVM_SIGNATURE_FLOAT: return 4; 759 case JVM_SIGNATURE_LONG: 760 case JVM_SIGNATURE_DOUBLE: return 8; 761 default: ShouldNotReachHere(); /* to shut up compiler */ return 0; 762 } 763 } 764 765 // dump a jfloat 766 void DumperSupport::dump_float(DumpWriter* writer, jfloat f) { 767 if (g_isnan(f)) { 768 writer->write_u4(0x7fc00000); // collapsing NaNs 769 } else { 770 union { 771 int i; 772 float f; 773 } u; 774 u.f = (float)f; 775 writer->write_u4((u4)u.i); 776 } 777 } 778 779 // dump a jdouble 780 void DumperSupport::dump_double(DumpWriter* writer, jdouble d) { 781 union { 782 jlong l; 783 double d; 784 } u; 785 if (g_isnan(d)) { // collapsing NaNs 786 u.l = (jlong)(0x7ff80000); 787 u.l = (u.l << 32); 788 } else { 789 u.d = (double)d; 790 } 791 writer->write_u8((u8)u.l); 792 } 793 794 // dumps the raw value of the given field 795 void DumperSupport::dump_field_value(DumpWriter* writer, char type, oop obj, int offset) { 796 switch (type) { 797 case JVM_SIGNATURE_CLASS : 798 case JVM_SIGNATURE_ARRAY : { 799 oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset); 800 if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) { 801 ResourceMark rm; 802 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)", 803 p2i(o), o->klass()->external_name(), 804 p2i(obj), obj->klass()->external_name()); 805 } 806 o = mask_dormant_archived_object(o); 807 assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o)); 808 writer->write_objectID(o); 809 break; 810 } 811 case JVM_SIGNATURE_BYTE : { 812 jbyte b = obj->byte_field(offset); 813 writer->write_u1((u1)b); 814 break; 815 } 816 case JVM_SIGNATURE_CHAR : { 817 jchar c = obj->char_field(offset); 818 writer->write_u2((u2)c); 819 break; 820 } 821 case JVM_SIGNATURE_SHORT : { 822 jshort s = obj->short_field(offset); 823 writer->write_u2((u2)s); 824 break; 825 } 826 case JVM_SIGNATURE_FLOAT : { 827 jfloat f = obj->float_field(offset); 828 dump_float(writer, f); 829 break; 830 } 831 case JVM_SIGNATURE_DOUBLE : { 832 jdouble d = obj->double_field(offset); 833 dump_double(writer, d); 834 break; 835 } 836 case JVM_SIGNATURE_INT : { 837 jint i = obj->int_field(offset); 838 writer->write_u4((u4)i); 839 break; 840 } 841 case JVM_SIGNATURE_LONG : { 842 jlong l = obj->long_field(offset); 843 writer->write_u8((u8)l); 844 break; 845 } 846 case JVM_SIGNATURE_BOOLEAN : { 847 jboolean b = obj->bool_field(offset); 848 writer->write_u1((u1)b); 849 break; 850 } 851 default : { 852 ShouldNotReachHere(); 853 break; 854 } 855 } 856 } 857 858 // returns the size of the instance of the given class 859 u4 DumperSupport::instance_size(Klass* k) { 860 HandleMark hm; 861 InstanceKlass* ik = InstanceKlass::cast(k); 862 u4 size = 0; 863 864 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) { 865 if (!fld.access_flags().is_static()) { 866 size += sig2size(fld.signature()); 867 } 868 } 869 return size; 870 } 871 872 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) { 873 HandleMark hm; 874 field_count = 0; 875 u4 size = 0; 876 877 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) { 878 if (fldc.access_flags().is_static()) { 879 field_count++; 880 size += sig2size(fldc.signature()); 881 } 882 } 883 884 // Add in resolved_references which is referenced by the cpCache 885 // The resolved_references is an array per InstanceKlass holding the 886 // strings and other oops resolved from the constant pool. 887 oop resolved_references = ik->constants()->resolved_references_or_null(); 888 if (resolved_references != NULL) { 889 field_count++; 890 size += sizeof(address); 891 892 // Add in the resolved_references of the used previous versions of the class 893 // in the case of RedefineClasses 894 InstanceKlass* prev = ik->previous_versions(); 895 while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) { 896 field_count++; 897 size += sizeof(address); 898 prev = prev->previous_versions(); 899 } 900 } 901 902 // Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0] 903 // arrays. 904 oop init_lock = ik->init_lock(); 905 if (init_lock != NULL) { 906 field_count++; 907 size += sizeof(address); 908 } 909 910 // We write the value itself plus a name and a one byte type tag per field. 911 return size + field_count * (sizeof(address) + 1); 912 } 913 914 // dumps static fields of the given class 915 void DumperSupport::dump_static_fields(DumpWriter* writer, Klass* k) { 916 HandleMark hm; 917 InstanceKlass* ik = InstanceKlass::cast(k); 918 919 // dump the field descriptors and raw values 920 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) { 921 if (fld.access_flags().is_static()) { 922 Symbol* sig = fld.signature(); 923 924 writer->write_symbolID(fld.name()); // name 925 writer->write_u1(sig2tag(sig)); // type 926 927 // value 928 dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset()); 929 } 930 } 931 932 // Add resolved_references for each class that has them 933 oop resolved_references = ik->constants()->resolved_references_or_null(); 934 if (resolved_references != NULL) { 935 writer->write_symbolID(vmSymbols::resolved_references_name()); // name 936 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type 937 writer->write_objectID(resolved_references); 938 939 // Also write any previous versions 940 InstanceKlass* prev = ik->previous_versions(); 941 while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) { 942 writer->write_symbolID(vmSymbols::resolved_references_name()); // name 943 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type 944 writer->write_objectID(prev->constants()->resolved_references()); 945 prev = prev->previous_versions(); 946 } 947 } 948 949 // Add init lock to the end if the class is not yet initialized 950 oop init_lock = ik->init_lock(); 951 if (init_lock != NULL) { 952 writer->write_symbolID(vmSymbols::init_lock_name()); // name 953 writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type 954 writer->write_objectID(init_lock); 955 } 956 } 957 958 // dump the raw values of the instance fields of the given object 959 void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) { 960 HandleMark hm; 961 InstanceKlass* ik = InstanceKlass::cast(o->klass()); 962 963 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) { 964 if (!fld.access_flags().is_static()) { 965 Symbol* sig = fld.signature(); 966 dump_field_value(writer, sig->char_at(0), o, fld.offset()); 967 } 968 } 969 } 970 971 // dumps the definition of the instance fields for a given class 972 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) { 973 HandleMark hm; 974 u2 field_count = 0; 975 976 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) { 977 if (!fldc.access_flags().is_static()) field_count++; 978 } 979 980 return field_count; 981 } 982 983 // dumps the definition of the instance fields for a given class 984 void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, Klass* k) { 985 HandleMark hm; 986 InstanceKlass* ik = InstanceKlass::cast(k); 987 988 // dump the field descriptors 989 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) { 990 if (!fld.access_flags().is_static()) { 991 Symbol* sig = fld.signature(); 992 993 writer->write_symbolID(fld.name()); // name 994 writer->write_u1(sig2tag(sig)); // type 995 } 996 } 997 } 998 999 // creates HPROF_GC_INSTANCE_DUMP record for the given object 1000 void DumperSupport::dump_instance(DumpWriter* writer, oop o) { 1001 InstanceKlass* ik = InstanceKlass::cast(o->klass()); 1002 u4 is = instance_size(ik); 1003 u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is; 1004 1005 writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size); 1006 writer->write_objectID(o); 1007 writer->write_u4(STACK_TRACE_ID); 1008 1009 // class ID 1010 writer->write_classID(ik); 1011 1012 // number of bytes that follow 1013 writer->write_u4(is); 1014 1015 // field values 1016 dump_instance_fields(writer, o); 1017 1018 writer->end_sub_record(); 1019 } 1020 1021 // creates HPROF_GC_CLASS_DUMP record for the given class and each of 1022 // its array classes 1023 void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, Klass* k) { 1024 InstanceKlass* ik = InstanceKlass::cast(k); 1025 1026 // We can safepoint and do a heap dump at a point where we have a Klass, 1027 // but no java mirror class has been setup for it. So we need to check 1028 // that the class is at least loaded, to avoid crash from a null mirror. 1029 if (!ik->is_loaded()) { 1030 return; 1031 } 1032 1033 u2 static_fields_count = 0; 1034 u4 static_size = get_static_fields_size(ik, static_fields_count); 1035 u2 instance_fields_count = get_instance_fields_count(ik); 1036 u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1); 1037 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size; 1038 1039 writer->start_sub_record(HPROF_GC_CLASS_DUMP, size); 1040 1041 // class ID 1042 writer->write_classID(ik); 1043 writer->write_u4(STACK_TRACE_ID); 1044 1045 // super class ID 1046 InstanceKlass* java_super = ik->java_super(); 1047 if (java_super == NULL) { 1048 writer->write_objectID(oop(NULL)); 1049 } else { 1050 writer->write_classID(java_super); 1051 } 1052 1053 writer->write_objectID(ik->class_loader()); 1054 writer->write_objectID(ik->signers()); 1055 writer->write_objectID(ik->protection_domain()); 1056 1057 // reserved 1058 writer->write_objectID(oop(NULL)); 1059 writer->write_objectID(oop(NULL)); 1060 1061 // instance size 1062 writer->write_u4(DumperSupport::instance_size(ik)); 1063 1064 // size of constant pool - ignored by HAT 1.1 1065 writer->write_u2(0); 1066 1067 // static fields 1068 writer->write_u2(static_fields_count); 1069 dump_static_fields(writer, ik); 1070 1071 // description of instance fields 1072 writer->write_u2(instance_fields_count); 1073 dump_instance_field_descriptors(writer, ik); 1074 1075 writer->end_sub_record(); 1076 1077 // array classes 1078 k = ik->array_klass_or_null(); 1079 while (k != NULL) { 1080 assert(k->is_objArray_klass(), "not an ObjArrayKlass"); 1081 1082 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2; 1083 writer->start_sub_record(HPROF_GC_CLASS_DUMP, size); 1084 writer->write_classID(k); 1085 writer->write_u4(STACK_TRACE_ID); 1086 1087 // super class of array classes is java.lang.Object 1088 java_super = k->java_super(); 1089 assert(java_super != NULL, "checking"); 1090 writer->write_classID(java_super); 1091 1092 writer->write_objectID(ik->class_loader()); 1093 writer->write_objectID(ik->signers()); 1094 writer->write_objectID(ik->protection_domain()); 1095 1096 writer->write_objectID(oop(NULL)); // reserved 1097 writer->write_objectID(oop(NULL)); 1098 writer->write_u4(0); // instance size 1099 writer->write_u2(0); // constant pool 1100 writer->write_u2(0); // static fields 1101 writer->write_u2(0); // instance fields 1102 1103 writer->end_sub_record(); 1104 1105 // get the array class for the next rank 1106 k = k->array_klass_or_null(); 1107 } 1108 } 1109 1110 // creates HPROF_GC_CLASS_DUMP record for a given primitive array 1111 // class (and each multi-dimensional array class too) 1112 void DumperSupport::dump_basic_type_array_class(DumpWriter* writer, Klass* k) { 1113 // array classes 1114 while (k != NULL) { 1115 Klass* klass = k; 1116 1117 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2; 1118 writer->start_sub_record(HPROF_GC_CLASS_DUMP, size); 1119 writer->write_classID(klass); 1120 writer->write_u4(STACK_TRACE_ID); 1121 1122 // super class of array classes is java.lang.Object 1123 InstanceKlass* java_super = klass->java_super(); 1124 assert(java_super != NULL, "checking"); 1125 writer->write_classID(java_super); 1126 1127 writer->write_objectID(oop(NULL)); // loader 1128 writer->write_objectID(oop(NULL)); // signers 1129 writer->write_objectID(oop(NULL)); // protection domain 1130 1131 writer->write_objectID(oop(NULL)); // reserved 1132 writer->write_objectID(oop(NULL)); 1133 writer->write_u4(0); // instance size 1134 writer->write_u2(0); // constant pool 1135 writer->write_u2(0); // static fields 1136 writer->write_u2(0); // instance fields 1137 1138 writer->end_sub_record(); 1139 1140 // get the array class for the next rank 1141 k = klass->array_klass_or_null(); 1142 } 1143 } 1144 1145 // Hprof uses an u4 as record length field, 1146 // which means we need to truncate arrays that are too long. 1147 int DumperSupport::calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size) { 1148 BasicType type = ArrayKlass::cast(array->klass())->element_type(); 1149 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type"); 1150 1151 int length = array->length(); 1152 1153 int type_size; 1154 if (type == T_OBJECT) { 1155 type_size = sizeof(address); 1156 } else { 1157 type_size = type2aelembytes(type); 1158 } 1159 1160 size_t length_in_bytes = (size_t)length * type_size; 1161 uint max_bytes = max_juint - header_size; 1162 1163 if (length_in_bytes > max_bytes) { 1164 length = max_bytes / type_size; 1165 length_in_bytes = (size_t)length * type_size; 1166 1167 warning("cannot dump array of type %s[] with length %d; truncating to length %d", 1168 type2name_tab[type], array->length(), length); 1169 } 1170 return length; 1171 } 1172 1173 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array 1174 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) { 1175 // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID) 1176 short header_size = 1 + 2 * 4 + 2 * sizeof(address); 1177 int length = calculate_array_max_length(writer, array, header_size); 1178 u4 size = header_size + length * sizeof(address); 1179 1180 writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size); 1181 writer->write_objectID(array); 1182 writer->write_u4(STACK_TRACE_ID); 1183 writer->write_u4(length); 1184 1185 // array class ID 1186 writer->write_classID(array->klass()); 1187 1188 // [id]* elements 1189 for (int index = 0; index < length; index++) { 1190 oop o = array->obj_at(index); 1191 if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) { 1192 ResourceMark rm; 1193 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)", 1194 p2i(o), o->klass()->external_name(), 1195 p2i(array), array->klass()->external_name()); 1196 } 1197 o = mask_dormant_archived_object(o); 1198 writer->write_objectID(o); 1199 } 1200 1201 writer->end_sub_record(); 1202 } 1203 1204 #define WRITE_ARRAY(Array, Type, Size, Length) \ 1205 for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); } 1206 1207 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array 1208 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) { 1209 BasicType type = TypeArrayKlass::cast(array->klass())->element_type(); 1210 1211 // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) 1212 short header_size = 2 * 1 + 2 * 4 + sizeof(address); 1213 1214 int length = calculate_array_max_length(writer, array, header_size); 1215 int type_size = type2aelembytes(type); 1216 u4 length_in_bytes = (u4)length * type_size; 1217 u4 size = header_size + length_in_bytes; 1218 1219 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size); 1220 writer->write_objectID(array); 1221 writer->write_u4(STACK_TRACE_ID); 1222 writer->write_u4(length); 1223 writer->write_u1(type2tag(type)); 1224 1225 // nothing to copy 1226 if (length == 0) { 1227 writer->end_sub_record(); 1228 return; 1229 } 1230 1231 // If the byte ordering is big endian then we can copy most types directly 1232 1233 switch (type) { 1234 case T_INT : { 1235 if (Endian::is_Java_byte_ordering_different()) { 1236 WRITE_ARRAY(array, int, u4, length); 1237 } else { 1238 writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes); 1239 } 1240 break; 1241 } 1242 case T_BYTE : { 1243 writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes); 1244 break; 1245 } 1246 case T_CHAR : { 1247 if (Endian::is_Java_byte_ordering_different()) { 1248 WRITE_ARRAY(array, char, u2, length); 1249 } else { 1250 writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes); 1251 } 1252 break; 1253 } 1254 case T_SHORT : { 1255 if (Endian::is_Java_byte_ordering_different()) { 1256 WRITE_ARRAY(array, short, u2, length); 1257 } else { 1258 writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes); 1259 } 1260 break; 1261 } 1262 case T_BOOLEAN : { 1263 if (Endian::is_Java_byte_ordering_different()) { 1264 WRITE_ARRAY(array, bool, u1, length); 1265 } else { 1266 writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes); 1267 } 1268 break; 1269 } 1270 case T_LONG : { 1271 if (Endian::is_Java_byte_ordering_different()) { 1272 WRITE_ARRAY(array, long, u8, length); 1273 } else { 1274 writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes); 1275 } 1276 break; 1277 } 1278 1279 // handle float/doubles in a special value to ensure than NaNs are 1280 // written correctly. TO DO: Check if we can avoid this on processors that 1281 // use IEEE 754. 1282 1283 case T_FLOAT : { 1284 for (int i = 0; i < length; i++) { 1285 dump_float(writer, array->float_at(i)); 1286 } 1287 break; 1288 } 1289 case T_DOUBLE : { 1290 for (int i = 0; i < length; i++) { 1291 dump_double(writer, array->double_at(i)); 1292 } 1293 break; 1294 } 1295 default : ShouldNotReachHere(); 1296 } 1297 1298 writer->end_sub_record(); 1299 } 1300 1301 // create a HPROF_FRAME record of the given Method* and bci 1302 void DumperSupport::dump_stack_frame(DumpWriter* writer, 1303 int frame_serial_num, 1304 int class_serial_num, 1305 Method* m, 1306 int bci) { 1307 int line_number; 1308 if (m->is_native()) { 1309 line_number = -3; // native frame 1310 } else { 1311 line_number = m->line_number_from_bci(bci); 1312 } 1313 1314 write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4)); 1315 writer->write_id(frame_serial_num); // frame serial number 1316 writer->write_symbolID(m->name()); // method's name 1317 writer->write_symbolID(m->signature()); // method's signature 1318 1319 assert(m->method_holder()->is_instance_klass(), "not InstanceKlass"); 1320 writer->write_symbolID(m->method_holder()->source_file_name()); // source file name 1321 writer->write_u4(class_serial_num); // class serial number 1322 writer->write_u4((u4) line_number); // line number 1323 } 1324 1325 1326 // Support class used to generate HPROF_UTF8 records from the entries in the 1327 // SymbolTable. 1328 1329 class SymbolTableDumper : public SymbolClosure { 1330 private: 1331 DumpWriter* _writer; 1332 DumpWriter* writer() const { return _writer; } 1333 public: 1334 SymbolTableDumper(DumpWriter* writer) { _writer = writer; } 1335 void do_symbol(Symbol** p); 1336 }; 1337 1338 void SymbolTableDumper::do_symbol(Symbol** p) { 1339 ResourceMark rm; 1340 Symbol* sym = load_symbol(p); 1341 int len = sym->utf8_length(); 1342 if (len > 0) { 1343 char* s = sym->as_utf8(); 1344 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len); 1345 writer()->write_symbolID(sym); 1346 writer()->write_raw(s, len); 1347 } 1348 } 1349 1350 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records 1351 1352 class JNILocalsDumper : public OopClosure { 1353 private: 1354 DumpWriter* _writer; 1355 u4 _thread_serial_num; 1356 int _frame_num; 1357 DumpWriter* writer() const { return _writer; } 1358 public: 1359 JNILocalsDumper(DumpWriter* writer, u4 thread_serial_num) { 1360 _writer = writer; 1361 _thread_serial_num = thread_serial_num; 1362 _frame_num = -1; // default - empty stack 1363 } 1364 void set_frame_number(int n) { _frame_num = n; } 1365 void do_oop(oop* obj_p); 1366 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } 1367 }; 1368 1369 1370 void JNILocalsDumper::do_oop(oop* obj_p) { 1371 // ignore null handles 1372 oop o = *obj_p; 1373 if (o != NULL) { 1374 u4 size = 1 + sizeof(address) + 4 + 4; 1375 writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size); 1376 writer()->write_objectID(o); 1377 writer()->write_u4(_thread_serial_num); 1378 writer()->write_u4((u4)_frame_num); 1379 writer()->end_sub_record(); 1380 } 1381 } 1382 1383 1384 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records 1385 1386 class JNIGlobalsDumper : public OopClosure { 1387 private: 1388 DumpWriter* _writer; 1389 DumpWriter* writer() const { return _writer; } 1390 1391 public: 1392 JNIGlobalsDumper(DumpWriter* writer) { 1393 _writer = writer; 1394 } 1395 void do_oop(oop* obj_p); 1396 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } 1397 }; 1398 1399 void JNIGlobalsDumper::do_oop(oop* obj_p) { 1400 oop o = *obj_p; 1401 1402 // ignore these 1403 if (o == NULL) return; 1404 1405 // we ignore global ref to symbols and other internal objects 1406 if (o->is_instance() || o->is_objArray() || o->is_typeArray()) { 1407 u4 size = 1 + 2 * sizeof(address); 1408 writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size); 1409 writer()->write_objectID(o); 1410 writer()->write_objectID((oopDesc*)obj_p); // global ref ID 1411 writer()->end_sub_record(); 1412 } 1413 }; 1414 1415 1416 // Support class used to generate HPROF_GC_ROOT_MONITOR_USED records 1417 1418 class MonitorUsedDumper : public OopClosure { 1419 private: 1420 DumpWriter* _writer; 1421 DumpWriter* writer() const { return _writer; } 1422 public: 1423 MonitorUsedDumper(DumpWriter* writer) { 1424 _writer = writer; 1425 } 1426 void do_oop(oop* obj_p) { 1427 u4 size = 1 + sizeof(address); 1428 writer()->start_sub_record(HPROF_GC_ROOT_MONITOR_USED, size); 1429 writer()->write_objectID(*obj_p); 1430 writer()->end_sub_record(); 1431 } 1432 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } 1433 }; 1434 1435 1436 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records 1437 1438 class StickyClassDumper : public KlassClosure { 1439 private: 1440 DumpWriter* _writer; 1441 DumpWriter* writer() const { return _writer; } 1442 public: 1443 StickyClassDumper(DumpWriter* writer) { 1444 _writer = writer; 1445 } 1446 void do_klass(Klass* k) { 1447 if (k->is_instance_klass()) { 1448 InstanceKlass* ik = InstanceKlass::cast(k); 1449 u4 size = 1 + sizeof(address); 1450 writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size); 1451 writer()->write_classID(ik); 1452 writer()->end_sub_record(); 1453 } 1454 } 1455 }; 1456 1457 1458 class VM_HeapDumper; 1459 1460 // Support class using when iterating over the heap. 1461 1462 class HeapObjectDumper : public ObjectClosure { 1463 private: 1464 VM_HeapDumper* _dumper; 1465 DumpWriter* _writer; 1466 1467 VM_HeapDumper* dumper() { return _dumper; } 1468 DumpWriter* writer() { return _writer; } 1469 1470 public: 1471 HeapObjectDumper(VM_HeapDumper* dumper, DumpWriter* writer) { 1472 _dumper = dumper; 1473 _writer = writer; 1474 } 1475 1476 // called for each object in the heap 1477 void do_object(oop o); 1478 }; 1479 1480 void HeapObjectDumper::do_object(oop o) { 1481 // skip classes as these emitted as HPROF_GC_CLASS_DUMP records 1482 if (o->klass() == SystemDictionary::Class_klass()) { 1483 if (!java_lang_Class::is_primitive(o)) { 1484 return; 1485 } 1486 } 1487 1488 if (DumperSupport::mask_dormant_archived_object(o) == NULL) { 1489 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name()); 1490 return; 1491 } 1492 1493 if (o->is_instance()) { 1494 // create a HPROF_GC_INSTANCE record for each object 1495 DumperSupport::dump_instance(writer(), o); 1496 } else if (o->is_objArray()) { 1497 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array 1498 DumperSupport::dump_object_array(writer(), objArrayOop(o)); 1499 } else if (o->is_typeArray()) { 1500 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array 1501 DumperSupport::dump_prim_array(writer(), typeArrayOop(o)); 1502 } 1503 } 1504 1505 // The VM operation that performs the heap dump 1506 class VM_HeapDumper : public VM_GC_Operation { 1507 private: 1508 static VM_HeapDumper* _global_dumper; 1509 static DumpWriter* _global_writer; 1510 DumpWriter* _local_writer; 1511 JavaThread* _oome_thread; 1512 Method* _oome_constructor; 1513 bool _gc_before_heap_dump; 1514 GrowableArray<Klass*>* _klass_map; 1515 ThreadStackTrace** _stack_traces; 1516 int _num_threads; 1517 1518 // accessors and setters 1519 static VM_HeapDumper* dumper() { assert(_global_dumper != NULL, "Error"); return _global_dumper; } 1520 static DumpWriter* writer() { assert(_global_writer != NULL, "Error"); return _global_writer; } 1521 void set_global_dumper() { 1522 assert(_global_dumper == NULL, "Error"); 1523 _global_dumper = this; 1524 } 1525 void set_global_writer() { 1526 assert(_global_writer == NULL, "Error"); 1527 _global_writer = _local_writer; 1528 } 1529 void clear_global_dumper() { _global_dumper = NULL; } 1530 void clear_global_writer() { _global_writer = NULL; } 1531 1532 bool skip_operation() const; 1533 1534 // writes a HPROF_LOAD_CLASS record 1535 class ClassesDo; 1536 static void do_load_class(Klass* k); 1537 1538 // writes a HPROF_GC_CLASS_DUMP record for the given class 1539 // (and each array class too) 1540 static void do_class_dump(Klass* k); 1541 1542 // writes a HPROF_GC_CLASS_DUMP records for a given basic type 1543 // array (and each multi-dimensional array too) 1544 static void do_basic_type_array_class_dump(Klass* k); 1545 1546 // HPROF_GC_ROOT_THREAD_OBJ records 1547 int do_thread(JavaThread* thread, u4 thread_serial_num); 1548 void do_threads(); 1549 1550 void add_class_serial_number(Klass* k, int serial_num) { 1551 _klass_map->at_put_grow(serial_num, k); 1552 } 1553 1554 // HPROF_TRACE and HPROF_FRAME records 1555 void dump_stack_traces(); 1556 1557 public: 1558 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) : 1559 VM_GC_Operation(0 /* total collections, dummy, ignored */, 1560 GCCause::_heap_dump /* GC Cause */, 1561 0 /* total full collections, dummy, ignored */, 1562 gc_before_heap_dump) { 1563 _local_writer = writer; 1564 _gc_before_heap_dump = gc_before_heap_dump; 1565 _klass_map = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, true); 1566 _stack_traces = NULL; 1567 _num_threads = 0; 1568 if (oome) { 1569 assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread"); 1570 // get OutOfMemoryError zero-parameter constructor 1571 InstanceKlass* oome_ik = SystemDictionary::OutOfMemoryError_klass(); 1572 _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(), 1573 vmSymbols::void_method_signature()); 1574 // get thread throwing OOME when generating the heap dump at OOME 1575 _oome_thread = JavaThread::current(); 1576 } else { 1577 _oome_thread = NULL; 1578 _oome_constructor = NULL; 1579 } 1580 } 1581 ~VM_HeapDumper() { 1582 if (_stack_traces != NULL) { 1583 for (int i=0; i < _num_threads; i++) { 1584 delete _stack_traces[i]; 1585 } 1586 FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces); 1587 } 1588 delete _klass_map; 1589 } 1590 1591 VMOp_Type type() const { return VMOp_HeapDumper; } 1592 void doit(); 1593 }; 1594 1595 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL; 1596 DumpWriter* VM_HeapDumper::_global_writer = NULL; 1597 1598 bool VM_HeapDumper::skip_operation() const { 1599 return false; 1600 } 1601 1602 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record 1603 void DumperSupport::end_of_dump(DumpWriter* writer) { 1604 writer->finish_dump_segment(); 1605 1606 writer->write_u1(HPROF_HEAP_DUMP_END); 1607 writer->write_u4(0); 1608 writer->write_u4(0); 1609 } 1610 1611 // writes a HPROF_LOAD_CLASS record for the class (and each of its 1612 // array classes) 1613 void VM_HeapDumper::do_load_class(Klass* k) { 1614 static u4 class_serial_num = 0; 1615 1616 // len of HPROF_LOAD_CLASS record 1617 u4 remaining = 2*oopSize + 2*sizeof(u4); 1618 1619 // write a HPROF_LOAD_CLASS for the class and each array class 1620 do { 1621 DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining); 1622 1623 // class serial number is just a number 1624 writer()->write_u4(++class_serial_num); 1625 1626 // class ID 1627 Klass* klass = k; 1628 writer()->write_classID(klass); 1629 1630 // add the Klass* and class serial number pair 1631 dumper()->add_class_serial_number(klass, class_serial_num); 1632 1633 writer()->write_u4(STACK_TRACE_ID); 1634 1635 // class name ID 1636 Symbol* name = klass->name(); 1637 writer()->write_symbolID(name); 1638 1639 // write a LOAD_CLASS record for the array type (if it exists) 1640 k = klass->array_klass_or_null(); 1641 } while (k != NULL); 1642 } 1643 1644 // writes a HPROF_GC_CLASS_DUMP record for the given class 1645 void VM_HeapDumper::do_class_dump(Klass* k) { 1646 if (k->is_instance_klass()) { 1647 DumperSupport::dump_class_and_array_classes(writer(), k); 1648 } 1649 } 1650 1651 // writes a HPROF_GC_CLASS_DUMP records for a given basic type 1652 // array (and each multi-dimensional array too) 1653 void VM_HeapDumper::do_basic_type_array_class_dump(Klass* k) { 1654 DumperSupport::dump_basic_type_array_class(writer(), k); 1655 } 1656 1657 // Walk the stack of the given thread. 1658 // Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local 1659 // Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local 1660 // 1661 // It returns the number of Java frames in this thread stack 1662 int VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) { 1663 JNILocalsDumper blk(writer(), thread_serial_num); 1664 1665 oop threadObj = java_thread->threadObj(); 1666 assert(threadObj != NULL, "sanity check"); 1667 1668 int stack_depth = 0; 1669 if (java_thread->has_last_Java_frame()) { 1670 1671 // vframes are resource allocated 1672 Thread* current_thread = Thread::current(); 1673 ResourceMark rm(current_thread); 1674 HandleMark hm(current_thread); 1675 1676 RegisterMap reg_map(java_thread); 1677 frame f = java_thread->last_frame(); 1678 vframe* vf = vframe::new_vframe(&f, ®_map, java_thread); 1679 frame* last_entry_frame = NULL; 1680 int extra_frames = 0; 1681 1682 if (java_thread == _oome_thread && _oome_constructor != NULL) { 1683 extra_frames++; 1684 } 1685 while (vf != NULL) { 1686 blk.set_frame_number(stack_depth); 1687 if (vf->is_java_frame()) { 1688 1689 // java frame (interpreted, compiled, ...) 1690 javaVFrame *jvf = javaVFrame::cast(vf); 1691 if (!(jvf->method()->is_native())) { 1692 StackValueCollection* locals = jvf->locals(); 1693 for (int slot=0; slot<locals->size(); slot++) { 1694 if (locals->at(slot)->type() == T_OBJECT) { 1695 oop o = locals->obj_at(slot)(); 1696 1697 if (o != NULL) { 1698 u4 size = 1 + sizeof(address) + 4 + 4; 1699 writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size); 1700 writer()->write_objectID(o); 1701 writer()->write_u4(thread_serial_num); 1702 writer()->write_u4((u4) (stack_depth + extra_frames)); 1703 writer()->end_sub_record(); 1704 } 1705 } 1706 } 1707 StackValueCollection *exprs = jvf->expressions(); 1708 for(int index = 0; index < exprs->size(); index++) { 1709 if (exprs->at(index)->type() == T_OBJECT) { 1710 oop o = exprs->obj_at(index)(); 1711 if (o != NULL) { 1712 u4 size = 1 + sizeof(address) + 4 + 4; 1713 writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size); 1714 writer()->write_objectID(o); 1715 writer()->write_u4(thread_serial_num); 1716 writer()->write_u4((u4) (stack_depth + extra_frames)); 1717 writer()->end_sub_record(); 1718 } 1719 } 1720 } 1721 } else { 1722 // native frame 1723 if (stack_depth == 0) { 1724 // JNI locals for the top frame. 1725 java_thread->active_handles()->oops_do(&blk); 1726 } else { 1727 if (last_entry_frame != NULL) { 1728 // JNI locals for the entry frame 1729 assert(last_entry_frame->is_entry_frame(), "checking"); 1730 last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk); 1731 } 1732 } 1733 } 1734 // increment only for Java frames 1735 stack_depth++; 1736 last_entry_frame = NULL; 1737 1738 } else { 1739 // externalVFrame - if it's an entry frame then report any JNI locals 1740 // as roots when we find the corresponding native javaVFrame 1741 frame* fr = vf->frame_pointer(); 1742 assert(fr != NULL, "sanity check"); 1743 if (fr->is_entry_frame()) { 1744 last_entry_frame = fr; 1745 } 1746 } 1747 vf = vf->sender(); 1748 } 1749 } else { 1750 // no last java frame but there may be JNI locals 1751 java_thread->active_handles()->oops_do(&blk); 1752 } 1753 return stack_depth; 1754 } 1755 1756 1757 // write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk 1758 // the stack so that locals and JNI locals are dumped. 1759 void VM_HeapDumper::do_threads() { 1760 for (int i=0; i < _num_threads; i++) { 1761 JavaThread* thread = _stack_traces[i]->thread(); 1762 oop threadObj = thread->threadObj(); 1763 u4 thread_serial_num = i+1; 1764 u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID; 1765 u4 size = 1 + sizeof(address) + 4 + 4; 1766 writer()->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size); 1767 writer()->write_objectID(threadObj); 1768 writer()->write_u4(thread_serial_num); // thread number 1769 writer()->write_u4(stack_serial_num); // stack trace serial number 1770 writer()->end_sub_record(); 1771 int num_frames = do_thread(thread, thread_serial_num); 1772 assert(num_frames == _stack_traces[i]->get_stack_depth(), 1773 "total number of Java frames not matched"); 1774 } 1775 } 1776 1777 1778 // The VM operation that dumps the heap. The dump consists of the following 1779 // records: 1780 // 1781 // HPROF_HEADER 1782 // [HPROF_UTF8]* 1783 // [HPROF_LOAD_CLASS]* 1784 // [[HPROF_FRAME]*|HPROF_TRACE]* 1785 // [HPROF_GC_CLASS_DUMP]* 1786 // [HPROF_HEAP_DUMP_SEGMENT]* 1787 // HPROF_HEAP_DUMP_END 1788 // 1789 // The HPROF_TRACE records represent the stack traces where the heap dump 1790 // is generated and a "dummy trace" record which does not include 1791 // any frames. The dummy trace record is used to be referenced as the 1792 // unknown object alloc site. 1793 // 1794 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records. 1795 // To allow the heap dump be generated in a single pass we remember the position 1796 // of the dump length and fix it up after all sub-records have been written. 1797 // To generate the sub-records we iterate over the heap, writing 1798 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP 1799 // records as we go. Once that is done we write records for some of the GC 1800 // roots. 1801 1802 void VM_HeapDumper::doit() { 1803 1804 HandleMark hm; 1805 CollectedHeap* ch = Universe::heap(); 1806 1807 ch->ensure_parsability(false); // must happen, even if collection does 1808 // not happen (e.g. due to GCLocker) 1809 1810 if (_gc_before_heap_dump) { 1811 if (GCLocker::is_active()) { 1812 warning("GC locker is held; pre-heapdump GC was skipped"); 1813 } else { 1814 ch->collect_as_vm_thread(GCCause::_heap_dump); 1815 } 1816 } 1817 1818 // At this point we should be the only dumper active, so 1819 // the following should be safe. 1820 set_global_dumper(); 1821 set_global_writer(); 1822 1823 // Write the file header - we always use 1.0.2 1824 size_t used = ch->used(); 1825 const char* header = "JAVA PROFILE 1.0.2"; 1826 1827 // header is few bytes long - no chance to overflow int 1828 writer()->write_raw((void*)header, (int)strlen(header)); 1829 writer()->write_u1(0); // terminator 1830 writer()->write_u4(oopSize); 1831 // timestamp is current time in ms 1832 writer()->write_u8(os::javaTimeMillis()); 1833 1834 // HPROF_UTF8 records 1835 SymbolTableDumper sym_dumper(writer()); 1836 SymbolTable::symbols_do(&sym_dumper); 1837 1838 // write HPROF_LOAD_CLASS records 1839 { 1840 LockedClassesDo locked_load_classes(&do_load_class); 1841 ClassLoaderDataGraph::classes_do(&locked_load_classes); 1842 } 1843 Universe::basic_type_classes_do(&do_load_class); 1844 1845 // write HPROF_FRAME and HPROF_TRACE records 1846 // this must be called after _klass_map is built when iterating the classes above. 1847 dump_stack_traces(); 1848 1849 // Writes HPROF_GC_CLASS_DUMP records 1850 { 1851 LockedClassesDo locked_dump_class(&do_class_dump); 1852 ClassLoaderDataGraph::classes_do(&locked_dump_class); 1853 } 1854 Universe::basic_type_classes_do(&do_basic_type_array_class_dump); 1855 1856 // writes HPROF_GC_INSTANCE_DUMP records. 1857 // After each sub-record is written check_segment_length will be invoked 1858 // to check if the current segment exceeds a threshold. If so, a new 1859 // segment is started. 1860 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk 1861 // of the heap dump. 1862 HeapObjectDumper obj_dumper(this, writer()); 1863 Universe::heap()->object_iterate(&obj_dumper); 1864 1865 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals 1866 do_threads(); 1867 1868 // HPROF_GC_ROOT_MONITOR_USED 1869 MonitorUsedDumper mon_dumper(writer()); 1870 ObjectSynchronizer::oops_do(&mon_dumper); 1871 1872 // HPROF_GC_ROOT_JNI_GLOBAL 1873 JNIGlobalsDumper jni_dumper(writer()); 1874 JNIHandles::oops_do(&jni_dumper); 1875 Universe::oops_do(&jni_dumper); // technically not jni roots, but global roots 1876 // for things like preallocated throwable backtraces 1877 1878 // HPROF_GC_ROOT_STICKY_CLASS 1879 // These should be classes in the NULL class loader data, and not all classes 1880 // if !ClassUnloading 1881 StickyClassDumper class_dumper(writer()); 1882 ClassLoaderData::the_null_class_loader_data()->classes_do(&class_dumper); 1883 1884 // Writes the HPROF_HEAP_DUMP_END record. 1885 DumperSupport::end_of_dump(writer()); 1886 1887 // Now we clear the global variables, so that a future dumper might run. 1888 clear_global_dumper(); 1889 clear_global_writer(); 1890 } 1891 1892 void VM_HeapDumper::dump_stack_traces() { 1893 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites 1894 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4)); 1895 writer()->write_u4((u4) STACK_TRACE_ID); 1896 writer()->write_u4(0); // thread number 1897 writer()->write_u4(0); // frame count 1898 1899 _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal); 1900 int frame_serial_num = 0; 1901 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { 1902 oop threadObj = thread->threadObj(); 1903 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { 1904 // dump thread stack trace 1905 ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false); 1906 stack_trace->dump_stack_at_safepoint(-1); 1907 _stack_traces[_num_threads++] = stack_trace; 1908 1909 // write HPROF_FRAME records for this thread's stack trace 1910 int depth = stack_trace->get_stack_depth(); 1911 int thread_frame_start = frame_serial_num; 1912 int extra_frames = 0; 1913 // write fake frame that makes it look like the thread, which caused OOME, 1914 // is in the OutOfMemoryError zero-parameter constructor 1915 if (thread == _oome_thread && _oome_constructor != NULL) { 1916 int oome_serial_num = _klass_map->find(_oome_constructor->method_holder()); 1917 // the class serial number starts from 1 1918 assert(oome_serial_num > 0, "OutOfMemoryError class not found"); 1919 DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, oome_serial_num, 1920 _oome_constructor, 0); 1921 extra_frames++; 1922 } 1923 for (int j=0; j < depth; j++) { 1924 StackFrameInfo* frame = stack_trace->stack_frame_at(j); 1925 Method* m = frame->method(); 1926 int class_serial_num = _klass_map->find(m->method_holder()); 1927 // the class serial number starts from 1 1928 assert(class_serial_num > 0, "class not found"); 1929 DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, class_serial_num, m, frame->bci()); 1930 } 1931 depth += extra_frames; 1932 1933 // write HPROF_TRACE record for one thread 1934 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4) + depth*oopSize); 1935 int stack_serial_num = _num_threads + STACK_TRACE_ID; 1936 writer()->write_u4(stack_serial_num); // stack trace serial number 1937 writer()->write_u4((u4) _num_threads); // thread serial number 1938 writer()->write_u4(depth); // frame count 1939 for (int j=1; j <= depth; j++) { 1940 writer()->write_id(thread_frame_start + j); 1941 } 1942 } 1943 } 1944 } 1945 1946 // dump the heap to given path. 1947 int HeapDumper::dump(const char* path, outputStream* out) { 1948 assert(path != NULL && strlen(path) > 0, "path missing"); 1949 1950 // print message in interactive case 1951 if (out != NULL) { 1952 out->print_cr("Dumping heap to %s ...", path); 1953 timer()->start(); 1954 } 1955 1956 // create JFR event 1957 EventHeapDump event; 1958 1959 // create the dump writer. If the file can be opened then bail 1960 DumpWriter writer(path); 1961 if (writer.error() != NULL) { 1962 set_error(writer.error()); 1963 if (out != NULL) { 1964 out->print_cr("Unable to create %s: %s", path, 1965 (error() != NULL) ? error() : "reason unknown"); 1966 } 1967 return -1; 1968 } 1969 1970 // generate the dump 1971 VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome); 1972 if (Thread::current()->is_VM_thread()) { 1973 assert(SafepointSynchronize::is_at_safepoint(), "Expected to be called at a safepoint"); 1974 dumper.doit(); 1975 } else { 1976 VMThread::execute(&dumper); 1977 } 1978 1979 // close dump file and record any error that the writer may have encountered 1980 writer.close(); 1981 set_error(writer.error()); 1982 1983 // emit JFR event 1984 if (error() == NULL) { 1985 event.set_destination(path); 1986 event.set_gcBeforeDump(_gc_before_heap_dump); 1987 event.set_size(writer.bytes_written()); 1988 event.set_onOutOfMemoryError(_oome); 1989 event.commit(); 1990 } 1991 1992 // print message in interactive case 1993 if (out != NULL) { 1994 timer()->stop(); 1995 if (error() == NULL) { 1996 out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]", 1997 writer.bytes_written(), timer()->seconds()); 1998 } else { 1999 out->print_cr("Dump file is incomplete: %s", writer.error()); 2000 } 2001 } 2002 2003 return (writer.error() == NULL) ? 0 : -1; 2004 } 2005 2006 // stop timer (if still active), and free any error string we might be holding 2007 HeapDumper::~HeapDumper() { 2008 if (timer()->is_active()) { 2009 timer()->stop(); 2010 } 2011 set_error(NULL); 2012 } 2013 2014 2015 // returns the error string (resource allocated), or NULL 2016 char* HeapDumper::error_as_C_string() const { 2017 if (error() != NULL) { 2018 char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1); 2019 strcpy(str, error()); 2020 return str; 2021 } else { 2022 return NULL; 2023 } 2024 } 2025 2026 // set the error string 2027 void HeapDumper::set_error(char* error) { 2028 if (_error != NULL) { 2029 os::free(_error); 2030 } 2031 if (error == NULL) { 2032 _error = NULL; 2033 } else { 2034 _error = os::strdup(error); 2035 assert(_error != NULL, "allocation failure"); 2036 } 2037 } 2038 2039 // Called by out-of-memory error reporting by a single Java thread 2040 // outside of a JVM safepoint 2041 void HeapDumper::dump_heap_from_oome() { 2042 HeapDumper::dump_heap(true); 2043 } 2044 2045 // Called by error reporting by a single Java thread outside of a JVM safepoint, 2046 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various 2047 // callers are strictly serialized and guaranteed not to interfere below. For more 2048 // general use, however, this method will need modification to prevent 2049 // inteference when updating the static variables base_path and dump_file_seq below. 2050 void HeapDumper::dump_heap() { 2051 HeapDumper::dump_heap(false); 2052 } 2053 2054 void HeapDumper::dump_heap(bool oome) { 2055 static char base_path[JVM_MAXPATHLEN] = {'\0'}; 2056 static uint dump_file_seq = 0; 2057 char* my_path; 2058 const int max_digit_chars = 20; 2059 2060 const char* dump_file_name = "java_pid"; 2061 const char* dump_file_ext = ".hprof"; 2062 2063 // The dump file defaults to java_pid<pid>.hprof in the current working 2064 // directory. HeapDumpPath=<file> can be used to specify an alternative 2065 // dump file name or a directory where dump file is created. 2066 if (dump_file_seq == 0) { // first time in, we initialize base_path 2067 // Calculate potentially longest base path and check if we have enough 2068 // allocated statically. 2069 const size_t total_length = 2070 (HeapDumpPath == NULL ? 0 : strlen(HeapDumpPath)) + 2071 strlen(os::file_separator()) + max_digit_chars + 2072 strlen(dump_file_name) + strlen(dump_file_ext) + 1; 2073 if (total_length > sizeof(base_path)) { 2074 warning("Cannot create heap dump file. HeapDumpPath is too long."); 2075 return; 2076 } 2077 2078 bool use_default_filename = true; 2079 if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') { 2080 // HeapDumpPath=<file> not specified 2081 } else { 2082 strcpy(base_path, HeapDumpPath); 2083 // check if the path is a directory (must exist) 2084 DIR* dir = os::opendir(base_path); 2085 if (dir == NULL) { 2086 use_default_filename = false; 2087 } else { 2088 // HeapDumpPath specified a directory. We append a file separator 2089 // (if needed). 2090 os::closedir(dir); 2091 size_t fs_len = strlen(os::file_separator()); 2092 if (strlen(base_path) >= fs_len) { 2093 char* end = base_path; 2094 end += (strlen(base_path) - fs_len); 2095 if (strcmp(end, os::file_separator()) != 0) { 2096 strcat(base_path, os::file_separator()); 2097 } 2098 } 2099 } 2100 } 2101 // If HeapDumpPath wasn't a file name then we append the default name 2102 if (use_default_filename) { 2103 const size_t dlen = strlen(base_path); // if heap dump dir specified 2104 jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s", 2105 dump_file_name, os::current_process_id(), dump_file_ext); 2106 } 2107 const size_t len = strlen(base_path) + 1; 2108 my_path = (char*)os::malloc(len, mtInternal); 2109 if (my_path == NULL) { 2110 warning("Cannot create heap dump file. Out of system memory."); 2111 return; 2112 } 2113 strncpy(my_path, base_path, len); 2114 } else { 2115 // Append a sequence number id for dumps following the first 2116 const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0 2117 my_path = (char*)os::malloc(len, mtInternal); 2118 if (my_path == NULL) { 2119 warning("Cannot create heap dump file. Out of system memory."); 2120 return; 2121 } 2122 jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq); 2123 } 2124 dump_file_seq++; // increment seq number for next time we dump 2125 2126 HeapDumper dumper(false /* no GC before heap dump */, 2127 oome /* pass along out-of-memory-error flag */); 2128 dumper.dump(my_path, tty); 2129 os::free(my_path); 2130 }