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