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