1 /* 2 * Copyright (c) 2004, 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 package sun.jvm.hotspot.utilities; 26 27 import java.io.*; 28 import java.nio.channels.*; 29 import java.util.*; 30 import sun.jvm.hotspot.debugger.*; 31 import sun.jvm.hotspot.memory.*; 32 import sun.jvm.hotspot.oops.*; 33 import sun.jvm.hotspot.runtime.*; 34 import sun.jvm.hotspot.classfile.*; 35 36 /* 37 * This class writes Java heap in hprof binary format. This format is 38 * used by Heap Analysis Tool (HAT). The class is heavily influenced 39 * by 'hprof_io.c' of 1.5 new hprof implementation. 40 */ 41 42 /* hprof binary format: (result either written to a file or sent over 43 * the network). 44 * 45 * WARNING: This format is still under development, and is subject to 46 * change without notice. 47 * 48 * header "JAVA PROFILE 1.0.2" (0-terminated) 49 * u4 size of identifiers. Identifiers are used to represent 50 * UTF8 strings, objects, stack traces, etc. They usually 51 * have the same size as host pointers. For example, on 52 * Solaris and Win32, the size is 4. 53 * u4 high word 54 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70 55 * [record]* a sequence of records. 56 * 57 */ 58 59 /* 60 * 61 * Record format: 62 * 63 * u1 a TAG denoting the type of the record 64 * u4 number of *microseconds* since the time stamp in the 65 * header. (wraps around in a little more than an hour) 66 * u4 number of bytes *remaining* in the record. Note that 67 * this number excludes the tag and the length field itself. 68 * [u1]* BODY of the record (a sequence of bytes) 69 */ 70 71 /* 72 * The following TAGs are supported: 73 * 74 * TAG BODY notes 75 *---------------------------------------------------------- 76 * HPROF_UTF8 a UTF8-encoded name 77 * 78 * id name ID 79 * [u1]* UTF8 characters (no trailing zero) 80 * 81 * HPROF_LOAD_CLASS a newly loaded class 82 * 83 * u4 class serial number (> 0) 84 * id class object ID 85 * u4 stack trace serial number 86 * id class name ID 87 * 88 * HPROF_UNLOAD_CLASS an unloading class 89 * 90 * u4 class serial_number 91 * 92 * HPROF_FRAME a Java stack frame 93 * 94 * id stack frame ID 95 * id method name ID 96 * id method signature ID 97 * id source file name ID 98 * u4 class serial number 99 * i4 line number. >0: normal 100 * -1: unknown 101 * -2: compiled method 102 * -3: native method 103 * 104 * HPROF_TRACE a Java stack trace 105 * 106 * u4 stack trace serial number 107 * u4 thread serial number 108 * u4 number of frames 109 * [id]* stack frame IDs 110 * 111 * 112 * HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC 113 * 114 * u2 flags 0x0001: incremental vs. complete 115 * 0x0002: sorted by allocation vs. live 116 * 0x0004: whether to force a GC 117 * u4 cutoff ratio 118 * u4 total live bytes 119 * u4 total live instances 120 * u8 total bytes allocated 121 * u8 total instances allocated 122 * u4 number of sites that follow 123 * [u1 is_array: 0: normal object 124 * 2: object array 125 * 4: boolean array 126 * 5: char array 127 * 6: float array 128 * 7: double array 129 * 8: byte array 130 * 9: short array 131 * 10: int array 132 * 11: long array 133 * u4 class serial number (may be zero during startup) 134 * u4 stack trace serial number 135 * u4 number of bytes alive 136 * u4 number of instances alive 137 * u4 number of bytes allocated 138 * u4]* number of instance allocated 139 * 140 * HPROF_START_THREAD a newly started thread. 141 * 142 * u4 thread serial number (> 0) 143 * id thread object ID 144 * u4 stack trace serial number 145 * id thread name ID 146 * id thread group name ID 147 * id thread group parent name ID 148 * 149 * HPROF_END_THREAD a terminating thread. 150 * 151 * u4 thread serial number 152 * 153 * HPROF_HEAP_SUMMARY heap summary 154 * 155 * u4 total live bytes 156 * u4 total live instances 157 * u8 total bytes allocated 158 * u8 total instances allocated 159 * 160 * HPROF_HEAP_DUMP denote a heap dump 161 * 162 * [heap dump sub-records]* 163 * 164 * There are four kinds of heap dump sub-records: 165 * 166 * u1 sub-record type 167 * 168 * HPROF_GC_ROOT_UNKNOWN unknown root 169 * 170 * id object ID 171 * 172 * HPROF_GC_ROOT_THREAD_OBJ thread object 173 * 174 * id thread object ID (may be 0 for a 175 * thread newly attached through JNI) 176 * u4 thread sequence number 177 * u4 stack trace sequence number 178 * 179 * HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root 180 * 181 * id object ID 182 * id JNI global ref ID 183 * 184 * HPROF_GC_ROOT_JNI_LOCAL JNI local ref 185 * 186 * id object ID 187 * u4 thread serial number 188 * u4 frame # in stack trace (-1 for empty) 189 * 190 * HPROF_GC_ROOT_JAVA_FRAME Java stack frame 191 * 192 * id object ID 193 * u4 thread serial number 194 * u4 frame # in stack trace (-1 for empty) 195 * 196 * HPROF_GC_ROOT_NATIVE_STACK Native stack 197 * 198 * id object ID 199 * u4 thread serial number 200 * 201 * HPROF_GC_ROOT_STICKY_CLASS System class 202 * 203 * id object ID 204 * 205 * HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block 206 * 207 * id object ID 208 * u4 thread serial number 209 * 210 * HPROF_GC_ROOT_MONITOR_USED Busy monitor 211 * 212 * id object ID 213 * 214 * HPROF_GC_CLASS_DUMP dump of a class object 215 * 216 * id class object ID 217 * u4 stack trace serial number 218 * id super class object ID 219 * id class loader object ID 220 * id signers object ID 221 * id protection domain object ID 222 * id reserved 223 * id reserved 224 * 225 * u4 instance size (in bytes) 226 * 227 * u2 size of constant pool 228 * [u2, constant pool index, 229 * ty, type 230 * 2: object 231 * 4: boolean 232 * 5: char 233 * 6: float 234 * 7: double 235 * 8: byte 236 * 9: short 237 * 10: int 238 * 11: long 239 * vl]* and value 240 * 241 * u2 number of static fields 242 * [id, static field name, 243 * ty, type, 244 * vl]* and value 245 * 246 * u2 number of inst. fields (not inc. super) 247 * [id, instance field name, 248 * ty]* type 249 * 250 * HPROF_GC_INSTANCE_DUMP dump of a normal object 251 * 252 * id object ID 253 * u4 stack trace serial number 254 * id class object ID 255 * u4 number of bytes that follow 256 * [vl]* instance field values (class, followed 257 * by super, super's super ...) 258 * 259 * HPROF_GC_OBJ_ARRAY_DUMP dump of an object array 260 * 261 * id array object ID 262 * u4 stack trace serial number 263 * u4 number of elements 264 * id array class ID 265 * [id]* elements 266 * 267 * HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array 268 * 269 * id array object ID 270 * u4 stack trace serial number 271 * u4 number of elements 272 * u1 element type 273 * 4: boolean array 274 * 5: char array 275 * 6: float array 276 * 7: double array 277 * 8: byte array 278 * 9: short array 279 * 10: int array 280 * 11: long array 281 * [u1]* elements 282 * 283 * HPROF_CPU_SAMPLES a set of sample traces of running threads 284 * 285 * u4 total number of samples 286 * u4 # of traces 287 * [u4 # of samples 288 * u4]* stack trace serial number 289 * 290 * HPROF_CONTROL_SETTINGS the settings of on/off switches 291 * 292 * u4 0x00000001: alloc traces on/off 293 * 0x00000002: cpu sampling on/off 294 * u2 stack trace depth 295 * 296 * 297 * A heap dump can optionally be generated as a sequence of heap dump 298 * segments. This sequence is terminated by an end record. The additional 299 * tags allowed by format "JAVA PROFILE 1.0.2" are: 300 * 301 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment 302 * 303 * [heap dump sub-records]* 304 * The same sub-record types allowed by HPROF_HEAP_DUMP 305 * 306 * HPROF_HEAP_DUMP_END denotes the end of a heap dump 307 * 308 */ 309 310 public class HeapHprofBinWriter extends AbstractHeapGraphWriter { 311 312 private static final long HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD = 2L * 0x40000000; 313 314 // The approximate size of a heap segment. Used to calculate when to create 315 // a new segment. 316 private static final long HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE = 1L * 0x40000000; 317 318 // hprof binary file header 319 private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2"; 320 321 // constants in enum HprofTag 322 private static final int HPROF_UTF8 = 0x01; 323 private static final int HPROF_LOAD_CLASS = 0x02; 324 private static final int HPROF_UNLOAD_CLASS = 0x03; 325 private static final int HPROF_FRAME = 0x04; 326 private static final int HPROF_TRACE = 0x05; 327 private static final int HPROF_ALLOC_SITES = 0x06; 328 private static final int HPROF_HEAP_SUMMARY = 0x07; 329 private static final int HPROF_START_THREAD = 0x0A; 330 private static final int HPROF_END_THREAD = 0x0B; 331 private static final int HPROF_HEAP_DUMP = 0x0C; 332 private static final int HPROF_CPU_SAMPLES = 0x0D; 333 private static final int HPROF_CONTROL_SETTINGS = 0x0E; 334 335 // 1.0.2 record types 336 private static final int HPROF_HEAP_DUMP_SEGMENT = 0x1C; 337 private static final int HPROF_HEAP_DUMP_END = 0x2C; 338 339 // Heap dump constants 340 // constants in enum HprofGcTag 341 private static final int HPROF_GC_ROOT_UNKNOWN = 0xFF; 342 private static final int HPROF_GC_ROOT_JNI_GLOBAL = 0x01; 343 private static final int HPROF_GC_ROOT_JNI_LOCAL = 0x02; 344 private static final int HPROF_GC_ROOT_JAVA_FRAME = 0x03; 345 private static final int HPROF_GC_ROOT_NATIVE_STACK = 0x04; 346 private static final int HPROF_GC_ROOT_STICKY_CLASS = 0x05; 347 private static final int HPROF_GC_ROOT_THREAD_BLOCK = 0x06; 348 private static final int HPROF_GC_ROOT_MONITOR_USED = 0x07; 349 private static final int HPROF_GC_ROOT_THREAD_OBJ = 0x08; 350 private static final int HPROF_GC_CLASS_DUMP = 0x20; 351 private static final int HPROF_GC_INSTANCE_DUMP = 0x21; 352 private static final int HPROF_GC_OBJ_ARRAY_DUMP = 0x22; 353 private static final int HPROF_GC_PRIM_ARRAY_DUMP = 0x23; 354 355 // constants in enum HprofType 356 private static final int HPROF_ARRAY_OBJECT = 1; 357 private static final int HPROF_NORMAL_OBJECT = 2; 358 private static final int HPROF_BOOLEAN = 4; 359 private static final int HPROF_CHAR = 5; 360 private static final int HPROF_FLOAT = 6; 361 private static final int HPROF_DOUBLE = 7; 362 private static final int HPROF_BYTE = 8; 363 private static final int HPROF_SHORT = 9; 364 private static final int HPROF_INT = 10; 365 private static final int HPROF_LONG = 11; 366 367 // Java type codes 368 private static final int JVM_SIGNATURE_BOOLEAN = 'Z'; 369 private static final int JVM_SIGNATURE_CHAR = 'C'; 370 private static final int JVM_SIGNATURE_BYTE = 'B'; 371 private static final int JVM_SIGNATURE_SHORT = 'S'; 372 private static final int JVM_SIGNATURE_INT = 'I'; 373 private static final int JVM_SIGNATURE_LONG = 'J'; 374 private static final int JVM_SIGNATURE_FLOAT = 'F'; 375 private static final int JVM_SIGNATURE_DOUBLE = 'D'; 376 private static final int JVM_SIGNATURE_ARRAY = '['; 377 private static final int JVM_SIGNATURE_CLASS = 'L'; 378 379 private static final long MAX_U4_VALUE = 0xFFFFFFFFL; 380 int serialNum = 1; 381 382 public HeapHprofBinWriter() { 383 this.KlassMap = new ArrayList<Klass>(); 384 } 385 386 public synchronized void write(String fileName) throws IOException { 387 // open file stream and create buffered data output stream 388 fos = new FileOutputStream(fileName); 389 out = new DataOutputStream(new BufferedOutputStream(fos)); 390 391 VM vm = VM.getVM(); 392 dbg = vm.getDebugger(); 393 objectHeap = vm.getObjectHeap(); 394 symTbl = vm.getSymbolTable(); 395 396 OBJ_ID_SIZE = (int) vm.getOopSize(); 397 398 BOOLEAN_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BOOLEAN); 399 BYTE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BYTE); 400 CHAR_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_CHAR); 401 SHORT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_SHORT); 402 INT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_INT); 403 LONG_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_LONG); 404 FLOAT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_FLOAT); 405 DOUBLE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_DOUBLE); 406 OBJECT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_OBJECT); 407 408 BOOLEAN_SIZE = objectHeap.getBooleanSize(); 409 BYTE_SIZE = objectHeap.getByteSize(); 410 CHAR_SIZE = objectHeap.getCharSize(); 411 SHORT_SIZE = objectHeap.getShortSize(); 412 INT_SIZE = objectHeap.getIntSize(); 413 LONG_SIZE = objectHeap.getLongSize(); 414 FLOAT_SIZE = objectHeap.getFloatSize(); 415 DOUBLE_SIZE = objectHeap.getDoubleSize(); 416 417 // Check weather we should dump the heap as segments 418 useSegmentedHeapDump = vm.getUniverse().heap().used() > HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD; 419 420 // hprof bin format header 421 writeFileHeader(); 422 423 // dummy stack trace without any frames so that 424 // HAT can be run without -stack false option 425 writeDummyTrace(); 426 427 // hprof UTF-8 symbols section 428 writeSymbols(); 429 430 // HPROF_LOAD_CLASS records for all classes 431 writeClasses(); 432 433 // write HPROF_FRAME and HPROF_TRACE records 434 dumpStackTraces(); 435 436 // write CLASS_DUMP records 437 writeClassDumpRecords(); 438 439 // this will write heap data into the buffer stream 440 super.write(); 441 442 // flush buffer stream. 443 out.flush(); 444 445 // Fill in final length 446 fillInHeapRecordLength(); 447 448 if (useSegmentedHeapDump) { 449 // Write heap segment-end record 450 out.writeByte((byte) HPROF_HEAP_DUMP_END); 451 out.writeInt(0); 452 out.writeInt(0); 453 } 454 455 // flush buffer stream and throw it. 456 out.flush(); 457 out = null; 458 459 // close the file stream 460 fos.close(); 461 } 462 463 @Override 464 protected void writeHeapRecordPrologue() throws IOException { 465 if (currentSegmentStart == 0) { 466 // write heap data header, depending on heap size use segmented heap 467 // format 468 out.writeByte((byte) (useSegmentedHeapDump ? HPROF_HEAP_DUMP_SEGMENT 469 : HPROF_HEAP_DUMP)); 470 out.writeInt(0); 471 472 // remember position of dump length, we will fixup 473 // length later - hprof format requires length. 474 out.flush(); 475 currentSegmentStart = fos.getChannel().position(); 476 // write dummy length of 0 and we'll fix it later. 477 out.writeInt(0); 478 } 479 } 480 481 @Override 482 protected void writeHeapRecordEpilogue() throws IOException { 483 if (useSegmentedHeapDump) { 484 out.flush(); 485 if ((fos.getChannel().position() - currentSegmentStart - 4L) >= HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE) { 486 fillInHeapRecordLength(); 487 currentSegmentStart = 0; 488 } 489 } 490 } 491 492 private void fillInHeapRecordLength() throws IOException { 493 494 // now get the current position to calculate length 495 long dumpEnd = fos.getChannel().position(); 496 497 // calculate the length of heap data 498 long dumpLenLong = (dumpEnd - currentSegmentStart - 4L); 499 500 // Check length boundary, overflow could happen but is _very_ unlikely 501 if (dumpLenLong >= (4L * 0x40000000)) { 502 throw new RuntimeException("Heap segment size overflow."); 503 } 504 505 // Save the current position 506 long currentPosition = fos.getChannel().position(); 507 508 // seek the position to write length 509 fos.getChannel().position(currentSegmentStart); 510 511 int dumpLen = (int) dumpLenLong; 512 513 // write length as integer 514 fos.write((dumpLen >>> 24) & 0xFF); 515 fos.write((dumpLen >>> 16) & 0xFF); 516 fos.write((dumpLen >>> 8) & 0xFF); 517 fos.write((dumpLen >>> 0) & 0xFF); 518 519 //Reset to previous current position 520 fos.getChannel().position(currentPosition); 521 } 522 523 // get the size in bytes for the requested type 524 private long getSizeForType(int type) throws IOException { 525 switch (type) { 526 case TypeArrayKlass.T_BOOLEAN: 527 return BOOLEAN_SIZE; 528 case TypeArrayKlass.T_INT: 529 return INT_SIZE; 530 case TypeArrayKlass.T_CHAR: 531 return CHAR_SIZE; 532 case TypeArrayKlass.T_SHORT: 533 return SHORT_SIZE; 534 case TypeArrayKlass.T_BYTE: 535 return BYTE_SIZE; 536 case TypeArrayKlass.T_LONG: 537 return LONG_SIZE; 538 case TypeArrayKlass.T_FLOAT: 539 return FLOAT_SIZE; 540 case TypeArrayKlass.T_DOUBLE: 541 return DOUBLE_SIZE; 542 default: 543 throw new RuntimeException( 544 "Should not reach here: Unknown type: " + type); 545 } 546 } 547 548 private int getArrayHeaderSize(boolean isObjectAarray) { 549 return isObjectAarray? 550 ((int) BYTE_SIZE + 2 * (int) INT_SIZE + 2 * (int) OBJ_ID_SIZE): 551 (2 * (int) BYTE_SIZE + 2 * (int) INT_SIZE + (int) OBJ_ID_SIZE); 552 } 553 554 // Check if we need to truncate an array 555 private int calculateArrayMaxLength(long originalArrayLength, 556 int headerSize, 557 long typeSize, 558 String typeName) throws IOException { 559 560 long length = originalArrayLength; 561 562 // now get the current position to calculate length 563 long dumpEnd = fos.getChannel().position(); 564 long originalLengthInBytes = originalArrayLength * typeSize; 565 566 // calculate the length of heap data 567 long currentRecordLength = (dumpEnd - currentSegmentStart - 4L); 568 if (currentRecordLength > 0 && 569 (currentRecordLength + headerSize + originalLengthInBytes) > MAX_U4_VALUE) { 570 fillInHeapRecordLength(); 571 currentSegmentStart = 0; 572 writeHeapRecordPrologue(); 573 currentRecordLength = 0; 574 } 575 576 // Calculate the max bytes we can use. 577 long maxBytes = (MAX_U4_VALUE - (headerSize + currentRecordLength)); 578 579 if (originalLengthInBytes > maxBytes) { 580 length = maxBytes/typeSize; 581 System.err.println("WARNING: Cannot dump array of type " + typeName 582 + " with length " + originalArrayLength 583 + "; truncating to length " + length); 584 } 585 return (int) length; 586 } 587 588 private void writeClassDumpRecords() throws IOException { 589 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 590 ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph(); 591 try { 592 sysDict.allClassesDo(new SystemDictionary.ClassVisitor() { 593 public void visit(Klass k) { 594 try { 595 writeHeapRecordPrologue(); 596 writeClassDumpRecord(k); 597 writeHeapRecordEpilogue(); 598 } catch (IOException e) { 599 throw new RuntimeException(e); 600 } 601 } 602 }); 603 // Add the anonymous classes also which are not present in the 604 // System Dictionary 605 cldGraph.allAnonymousKlassesDo(new ClassLoaderDataGraph.KlassVisitor() { 606 public void visit(Klass k) { 607 try { 608 writeHeapRecordPrologue(); 609 writeClassDumpRecord(k); 610 writeHeapRecordEpilogue(); 611 } catch (IOException e) { 612 throw new RuntimeException(e); 613 } 614 } 615 }); 616 } catch (RuntimeException re) { 617 handleRuntimeException(re); 618 } 619 } 620 621 protected void writeClass(Instance instance) throws IOException { 622 Klass reflectedKlass = java_lang_Class.asKlass(instance); 623 // dump instance record only for primitive type Class objects. 624 // all other Class objects are covered by writeClassDumpRecords. 625 if (reflectedKlass == null) { 626 writeInstance(instance); 627 } 628 } 629 630 private void writeClassDumpRecord(Klass k) throws IOException { 631 out.writeByte((byte)HPROF_GC_CLASS_DUMP); 632 writeObjectID(k.getJavaMirror()); 633 out.writeInt(DUMMY_STACK_TRACE_ID); 634 Klass superKlass = k.getJavaSuper(); 635 if (superKlass != null) { 636 writeObjectID(superKlass.getJavaMirror()); 637 } else { 638 writeObjectID(null); 639 } 640 641 if (k instanceof InstanceKlass) { 642 InstanceKlass ik = (InstanceKlass) k; 643 writeObjectID(ik.getClassLoader()); 644 writeObjectID(null); // ik.getJavaMirror().getSigners()); 645 writeObjectID(null); // ik.getJavaMirror().getProtectionDomain()); 646 // two reserved id fields 647 writeObjectID(null); 648 writeObjectID(null); 649 List fields = getInstanceFields(ik); 650 int instSize = getSizeForFields(fields); 651 classDataCache.put(ik, new ClassData(instSize, fields)); 652 out.writeInt(instSize); 653 654 // For now, ignore constant pool - HAT ignores too! 655 // output number of cp entries as zero. 656 out.writeShort((short) 0); 657 658 List declaredFields = ik.getImmediateFields(); 659 List staticFields = new ArrayList(); 660 List instanceFields = new ArrayList(); 661 Iterator itr = null; 662 for (itr = declaredFields.iterator(); itr.hasNext();) { 663 Field field = (Field) itr.next(); 664 if (field.isStatic()) { 665 staticFields.add(field); 666 } else { 667 instanceFields.add(field); 668 } 669 } 670 671 // dump static field descriptors 672 writeFieldDescriptors(staticFields, ik); 673 674 // dump instance field descriptors 675 writeFieldDescriptors(instanceFields, null); 676 } else { 677 if (k instanceof ObjArrayKlass) { 678 ObjArrayKlass oak = (ObjArrayKlass) k; 679 Klass bottomKlass = oak.getBottomKlass(); 680 if (bottomKlass instanceof InstanceKlass) { 681 InstanceKlass ik = (InstanceKlass) bottomKlass; 682 writeObjectID(ik.getClassLoader()); 683 writeObjectID(null); // ik.getJavaMirror().getSigners()); 684 writeObjectID(null); // ik.getJavaMirror().getProtectionDomain()); 685 } else { 686 writeObjectID(null); 687 writeObjectID(null); 688 writeObjectID(null); 689 } 690 } else { 691 writeObjectID(null); 692 writeObjectID(null); 693 writeObjectID(null); 694 } 695 // two reserved id fields 696 writeObjectID(null); 697 writeObjectID(null); 698 // write zero instance size -- as instance size 699 // is variable for arrays. 700 out.writeInt(0); 701 // no constant pool for array klasses 702 out.writeShort((short) 0); 703 // no static fields for array klasses 704 out.writeShort((short) 0); 705 // no instance fields for array klasses 706 out.writeShort((short) 0); 707 } 708 } 709 710 private void dumpStackTraces() throws IOException { 711 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites 712 writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE ); 713 out.writeInt(DUMMY_STACK_TRACE_ID); 714 out.writeInt(0); // thread number 715 out.writeInt(0); // frame count 716 717 int frameSerialNum = 0; 718 int numThreads = 0; 719 Threads threads = VM.getVM().getThreads(); 720 721 for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) { 722 Oop threadObj = thread.getThreadObj(); 723 if (threadObj != null && !thread.isExiting() && !thread.isHiddenFromExternalView()) { 724 725 // dump thread stack trace 726 ThreadStackTrace st = new ThreadStackTrace(thread); 727 st.dumpStack(-1); 728 numThreads++; 729 730 // write HPROF_FRAME records for this thread's stack trace 731 int depth = st.getStackDepth(); 732 int threadFrameStart = frameSerialNum; 733 for (int j=0; j < depth; j++) { 734 StackFrameInfo frame = st.stackFrameAt(j); 735 Method m = frame.getMethod(); 736 int classSerialNum = KlassMap.indexOf(m.getMethodHolder()) + 1; 737 // the class serial number starts from 1 738 assert classSerialNum > 0:"class not found"; 739 dumpStackFrame(++frameSerialNum, classSerialNum, m, frame.getBCI()); 740 } 741 742 // write HPROF_TRACE record for one thread 743 writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE + depth * (int)VM.getVM().getOopSize()); 744 int stackSerialNum = numThreads + DUMMY_STACK_TRACE_ID; 745 out.writeInt(stackSerialNum); // stack trace serial number 746 out.writeInt(numThreads); // thread serial number 747 out.writeInt(depth); // frame count 748 for (int j=1; j <= depth; j++) { 749 writeObjectID(threadFrameStart + j); 750 } 751 } 752 } 753 } 754 755 private void dumpStackFrame(int frameSN, int classSN, Method m, int bci) throws IOException { 756 int lineNumber; 757 if (m.isNative()) { 758 lineNumber = -3; // native frame 759 } else { 760 lineNumber = m.getLineNumberFromBCI(bci); 761 } 762 writeHeader(HPROF_FRAME, 4 * (int)VM.getVM().getOopSize() + 2 * (int)INT_SIZE); 763 writeObjectID(frameSN); // frame serial number 764 writeSymbolID(m.getName()); // method's name 765 writeSymbolID(m.getSignature()); // method's signature 766 writeSymbolID(m.getMethodHolder().getSourceFileName()); // source file name 767 out.writeInt(classSN); // class serial number 768 out.writeInt(lineNumber); // line number 769 } 770 771 protected void writeJavaThread(JavaThread jt, int index) throws IOException { 772 out.writeByte((byte) HPROF_GC_ROOT_THREAD_OBJ); 773 writeObjectID(jt.getThreadObj()); 774 out.writeInt(index); 775 out.writeInt(DUMMY_STACK_TRACE_ID); 776 writeLocalJNIHandles(jt, index); 777 } 778 779 protected void writeLocalJNIHandles(JavaThread jt, int index) throws IOException { 780 final int threadIndex = index; 781 JNIHandleBlock blk = jt.activeHandles(); 782 if (blk != null) { 783 try { 784 blk.oopsDo(new AddressVisitor() { 785 public void visitAddress(Address handleAddr) { 786 try { 787 if (handleAddr != null) { 788 OopHandle oopHandle = handleAddr.getOopHandleAt(0); 789 Oop oop = objectHeap.newOop(oopHandle); 790 // exclude JNI handles hotspot internal objects 791 if (oop != null && isJavaVisible(oop)) { 792 out.writeByte((byte) HPROF_GC_ROOT_JNI_LOCAL); 793 writeObjectID(oop); 794 out.writeInt(threadIndex); 795 out.writeInt(EMPTY_FRAME_DEPTH); 796 } 797 } 798 } catch (IOException exp) { 799 throw new RuntimeException(exp); 800 } 801 } 802 public void visitCompOopAddress(Address handleAddr) { 803 throw new RuntimeException( 804 " Should not reach here. JNIHandles are not compressed \n"); 805 } 806 }); 807 } catch (RuntimeException re) { 808 handleRuntimeException(re); 809 } 810 } 811 } 812 813 protected void writeGlobalJNIHandle(Address handleAddr) throws IOException { 814 OopHandle oopHandle = handleAddr.getOopHandleAt(0); 815 Oop oop = objectHeap.newOop(oopHandle); 816 // exclude JNI handles of hotspot internal objects 817 if (oop != null && isJavaVisible(oop)) { 818 out.writeByte((byte) HPROF_GC_ROOT_JNI_GLOBAL); 819 writeObjectID(oop); 820 // use JNIHandle address as ID 821 writeObjectID(getAddressValue(handleAddr)); 822 } 823 } 824 825 protected void writeObjectArray(ObjArray array) throws IOException { 826 int headerSize = getArrayHeaderSize(true); 827 final int length = calculateArrayMaxLength(array.getLength(), 828 headerSize, 829 OBJ_ID_SIZE, 830 "Object"); 831 out.writeByte((byte) HPROF_GC_OBJ_ARRAY_DUMP); 832 writeObjectID(array); 833 out.writeInt(DUMMY_STACK_TRACE_ID); 834 out.writeInt(length); 835 writeObjectID(array.getKlass().getJavaMirror()); 836 for (int index = 0; index < length; index++) { 837 OopHandle handle = array.getOopHandleAt(index); 838 writeObjectID(getAddressValue(handle)); 839 } 840 } 841 842 protected void writePrimitiveArray(TypeArray array) throws IOException { 843 int headerSize = getArrayHeaderSize(false); 844 TypeArrayKlass tak = (TypeArrayKlass) array.getKlass(); 845 final int type = (int) tak.getElementType(); 846 final String typeName = tak.getElementTypeName(); 847 final long typeSize = getSizeForType(type); 848 final int length = calculateArrayMaxLength(array.getLength(), 849 headerSize, 850 typeSize, 851 typeName); 852 out.writeByte((byte) HPROF_GC_PRIM_ARRAY_DUMP); 853 writeObjectID(array); 854 out.writeInt(DUMMY_STACK_TRACE_ID); 855 out.writeInt(length); 856 out.writeByte((byte) type); 857 switch (type) { 858 case TypeArrayKlass.T_BOOLEAN: 859 writeBooleanArray(array, length); 860 break; 861 case TypeArrayKlass.T_CHAR: 862 writeCharArray(array, length); 863 break; 864 case TypeArrayKlass.T_FLOAT: 865 writeFloatArray(array, length); 866 break; 867 case TypeArrayKlass.T_DOUBLE: 868 writeDoubleArray(array, length); 869 break; 870 case TypeArrayKlass.T_BYTE: 871 writeByteArray(array, length); 872 break; 873 case TypeArrayKlass.T_SHORT: 874 writeShortArray(array, length); 875 break; 876 case TypeArrayKlass.T_INT: 877 writeIntArray(array, length); 878 break; 879 case TypeArrayKlass.T_LONG: 880 writeLongArray(array, length); 881 break; 882 default: 883 throw new RuntimeException( 884 "Should not reach here: Unknown type: " + type); 885 } 886 } 887 888 private void writeBooleanArray(TypeArray array, int length) throws IOException { 889 for (int index = 0; index < length; index++) { 890 long offset = BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE; 891 out.writeBoolean(array.getHandle().getJBooleanAt(offset)); 892 } 893 } 894 895 private void writeByteArray(TypeArray array, int length) throws IOException { 896 for (int index = 0; index < length; index++) { 897 long offset = BYTE_BASE_OFFSET + index * BYTE_SIZE; 898 out.writeByte(array.getHandle().getJByteAt(offset)); 899 } 900 } 901 902 private void writeShortArray(TypeArray array, int length) throws IOException { 903 for (int index = 0; index < length; index++) { 904 long offset = SHORT_BASE_OFFSET + index * SHORT_SIZE; 905 out.writeShort(array.getHandle().getJShortAt(offset)); 906 } 907 } 908 909 private void writeIntArray(TypeArray array, int length) throws IOException { 910 for (int index = 0; index < length; index++) { 911 long offset = INT_BASE_OFFSET + index * INT_SIZE; 912 out.writeInt(array.getHandle().getJIntAt(offset)); 913 } 914 } 915 916 private void writeLongArray(TypeArray array, int length) throws IOException { 917 for (int index = 0; index < length; index++) { 918 long offset = LONG_BASE_OFFSET + index * LONG_SIZE; 919 out.writeLong(array.getHandle().getJLongAt(offset)); 920 } 921 } 922 923 private void writeCharArray(TypeArray array, int length) throws IOException { 924 for (int index = 0; index < length; index++) { 925 long offset = CHAR_BASE_OFFSET + index * CHAR_SIZE; 926 out.writeChar(array.getHandle().getJCharAt(offset)); 927 } 928 } 929 930 private void writeFloatArray(TypeArray array, int length) throws IOException { 931 for (int index = 0; index < length; index++) { 932 long offset = FLOAT_BASE_OFFSET + index * FLOAT_SIZE; 933 out.writeFloat(array.getHandle().getJFloatAt(offset)); 934 } 935 } 936 937 private void writeDoubleArray(TypeArray array, int length) throws IOException { 938 for (int index = 0; index < length; index++) { 939 long offset = DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE; 940 out.writeDouble(array.getHandle().getJDoubleAt(offset)); 941 } 942 } 943 944 protected void writeInstance(Instance instance) throws IOException { 945 out.writeByte((byte) HPROF_GC_INSTANCE_DUMP); 946 writeObjectID(instance); 947 out.writeInt(DUMMY_STACK_TRACE_ID); 948 Klass klass = instance.getKlass(); 949 writeObjectID(klass.getJavaMirror()); 950 951 ClassData cd = (ClassData) classDataCache.get(klass); 952 953 if (Assert.ASSERTS_ENABLED) { 954 Assert.that(cd != null, "can not get class data for " + klass.getName().asString() + klass.getAddress()); 955 } 956 List fields = cd.fields; 957 int size = cd.instSize; 958 out.writeInt(size); 959 for (Iterator itr = fields.iterator(); itr.hasNext();) { 960 writeField((Field) itr.next(), instance); 961 } 962 } 963 964 //-- Internals only below this point 965 966 private void writeFieldDescriptors(List fields, InstanceKlass ik) 967 throws IOException { 968 // ik == null for instance fields. 969 out.writeShort((short) fields.size()); 970 for (Iterator itr = fields.iterator(); itr.hasNext();) { 971 Field field = (Field) itr.next(); 972 Symbol name = symTbl.probe(field.getID().getName()); 973 writeSymbolID(name); 974 char typeCode = (char) field.getSignature().getByteAt(0); 975 int kind = signatureToHprofKind(typeCode); 976 out.writeByte((byte)kind); 977 if (ik != null) { 978 // static field 979 writeField(field, ik.getJavaMirror()); 980 } 981 } 982 } 983 984 public static int signatureToHprofKind(char ch) { 985 switch (ch) { 986 case JVM_SIGNATURE_CLASS: 987 case JVM_SIGNATURE_ARRAY: 988 return HPROF_NORMAL_OBJECT; 989 case JVM_SIGNATURE_BOOLEAN: 990 return HPROF_BOOLEAN; 991 case JVM_SIGNATURE_CHAR: 992 return HPROF_CHAR; 993 case JVM_SIGNATURE_FLOAT: 994 return HPROF_FLOAT; 995 case JVM_SIGNATURE_DOUBLE: 996 return HPROF_DOUBLE; 997 case JVM_SIGNATURE_BYTE: 998 return HPROF_BYTE; 999 case JVM_SIGNATURE_SHORT: 1000 return HPROF_SHORT; 1001 case JVM_SIGNATURE_INT: 1002 return HPROF_INT; 1003 case JVM_SIGNATURE_LONG: 1004 return HPROF_LONG; 1005 default: 1006 throw new RuntimeException("should not reach here"); 1007 } 1008 } 1009 1010 private void writeField(Field field, Oop oop) throws IOException { 1011 char typeCode = (char) field.getSignature().getByteAt(0); 1012 switch (typeCode) { 1013 case JVM_SIGNATURE_BOOLEAN: 1014 out.writeBoolean(((BooleanField)field).getValue(oop)); 1015 break; 1016 case JVM_SIGNATURE_CHAR: 1017 out.writeChar(((CharField)field).getValue(oop)); 1018 break; 1019 case JVM_SIGNATURE_BYTE: 1020 out.writeByte(((ByteField)field).getValue(oop)); 1021 break; 1022 case JVM_SIGNATURE_SHORT: 1023 out.writeShort(((ShortField)field).getValue(oop)); 1024 break; 1025 case JVM_SIGNATURE_INT: 1026 out.writeInt(((IntField)field).getValue(oop)); 1027 break; 1028 case JVM_SIGNATURE_LONG: 1029 out.writeLong(((LongField)field).getValue(oop)); 1030 break; 1031 case JVM_SIGNATURE_FLOAT: 1032 out.writeFloat(((FloatField)field).getValue(oop)); 1033 break; 1034 case JVM_SIGNATURE_DOUBLE: 1035 out.writeDouble(((DoubleField)field).getValue(oop)); 1036 break; 1037 case JVM_SIGNATURE_CLASS: 1038 case JVM_SIGNATURE_ARRAY: { 1039 if (VM.getVM().isCompressedOopsEnabled()) { 1040 OopHandle handle = ((NarrowOopField)field).getValueAsOopHandle(oop); 1041 writeObjectID(getAddressValue(handle)); 1042 } else { 1043 OopHandle handle = ((OopField)field).getValueAsOopHandle(oop); 1044 writeObjectID(getAddressValue(handle)); 1045 } 1046 break; 1047 } 1048 default: 1049 throw new RuntimeException("should not reach here"); 1050 } 1051 } 1052 1053 private void writeHeader(int tag, int len) throws IOException { 1054 out.writeByte((byte)tag); 1055 out.writeInt(0); // current ticks 1056 out.writeInt(len); 1057 } 1058 1059 private void writeDummyTrace() throws IOException { 1060 writeHeader(HPROF_TRACE, 3 * 4); 1061 out.writeInt(DUMMY_STACK_TRACE_ID); 1062 out.writeInt(0); 1063 out.writeInt(0); 1064 } 1065 1066 private void writeSymbols() throws IOException { 1067 try { 1068 symTbl.symbolsDo(new SymbolTable.SymbolVisitor() { 1069 public void visit(Symbol sym) { 1070 try { 1071 writeSymbol(sym); 1072 } catch (IOException exp) { 1073 throw new RuntimeException(exp); 1074 } 1075 } 1076 }); 1077 } catch (RuntimeException re) { 1078 handleRuntimeException(re); 1079 } 1080 } 1081 1082 private void writeSymbol(Symbol sym) throws IOException { 1083 byte[] buf = sym.asString().getBytes("UTF-8"); 1084 writeHeader(HPROF_UTF8, buf.length + OBJ_ID_SIZE); 1085 writeSymbolID(sym); 1086 out.write(buf); 1087 } 1088 1089 private void writeClasses() throws IOException { 1090 // write class list (id, name) association 1091 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 1092 ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph(); 1093 try { 1094 sysDict.allClassesDo(new SystemDictionary.ClassVisitor() { 1095 public void visit(Klass k) { 1096 try { 1097 Instance clazz = k.getJavaMirror(); 1098 writeHeader(HPROF_LOAD_CLASS, 2 * (OBJ_ID_SIZE + 4)); 1099 out.writeInt(serialNum); 1100 writeObjectID(clazz); 1101 KlassMap.add(serialNum - 1, k); 1102 out.writeInt(DUMMY_STACK_TRACE_ID); 1103 writeSymbolID(k.getName()); 1104 serialNum++; 1105 } catch (IOException exp) { 1106 throw new RuntimeException(exp); 1107 } 1108 } 1109 }); 1110 cldGraph.allAnonymousKlassesDo(new ClassLoaderDataGraph.KlassVisitor() { 1111 public void visit(Klass k) { 1112 try { 1113 Instance clazz = k.getJavaMirror(); 1114 writeHeader(HPROF_LOAD_CLASS, 2 * (OBJ_ID_SIZE + 4)); 1115 out.writeInt(serialNum); 1116 writeObjectID(clazz); 1117 KlassMap.add(serialNum - 1, k); 1118 out.writeInt(DUMMY_STACK_TRACE_ID); 1119 writeSymbolID(k.getName()); 1120 serialNum++; 1121 } catch (IOException exp) { 1122 throw new RuntimeException(exp); 1123 } 1124 } 1125 }); 1126 } catch (RuntimeException re) { 1127 handleRuntimeException(re); 1128 } 1129 } 1130 1131 // writes hprof binary file header 1132 private void writeFileHeader() throws IOException { 1133 // version string 1134 out.writeBytes(HPROF_HEADER_1_0_2); 1135 out.writeByte((byte)'\0'); 1136 1137 // write identifier size. we use pointers as identifiers. 1138 out.writeInt(OBJ_ID_SIZE); 1139 1140 // timestamp -- file creation time. 1141 out.writeLong(System.currentTimeMillis()); 1142 } 1143 1144 // writes unique ID for an object 1145 private void writeObjectID(Oop oop) throws IOException { 1146 OopHandle handle = (oop != null)? oop.getHandle() : null; 1147 long address = getAddressValue(handle); 1148 writeObjectID(address); 1149 } 1150 1151 private void writeSymbolID(Symbol sym) throws IOException { 1152 writeObjectID(getAddressValue(sym.getAddress())); 1153 } 1154 1155 private void writeObjectID(long address) throws IOException { 1156 if (OBJ_ID_SIZE == 4) { 1157 out.writeInt((int) address); 1158 } else { 1159 out.writeLong(address); 1160 } 1161 } 1162 1163 private long getAddressValue(Address addr) { 1164 return (addr == null)? 0L : dbg.getAddressValue(addr); 1165 } 1166 1167 // get all declared as well as inherited (directly/indirectly) fields 1168 private static List/*<Field>*/ getInstanceFields(InstanceKlass ik) { 1169 InstanceKlass klass = ik; 1170 List res = new ArrayList(); 1171 while (klass != null) { 1172 List curFields = klass.getImmediateFields(); 1173 for (Iterator itr = curFields.iterator(); itr.hasNext();) { 1174 Field f = (Field) itr.next(); 1175 if (! f.isStatic()) { 1176 res.add(f); 1177 } 1178 } 1179 klass = (InstanceKlass) klass.getSuper(); 1180 } 1181 return res; 1182 } 1183 1184 // get size in bytes (in stream) required for given fields. Note 1185 // that this is not the same as object size in heap. The size in 1186 // heap will include size of padding/alignment bytes as well. 1187 private int getSizeForFields(List fields) { 1188 int size = 0; 1189 for (Iterator itr = fields.iterator(); itr.hasNext();) { 1190 Field field = (Field) itr.next(); 1191 char typeCode = (char) field.getSignature().getByteAt(0); 1192 switch (typeCode) { 1193 case JVM_SIGNATURE_BOOLEAN: 1194 case JVM_SIGNATURE_BYTE: 1195 size++; 1196 break; 1197 case JVM_SIGNATURE_CHAR: 1198 case JVM_SIGNATURE_SHORT: 1199 size += 2; 1200 break; 1201 case JVM_SIGNATURE_INT: 1202 case JVM_SIGNATURE_FLOAT: 1203 size += 4; 1204 break; 1205 case JVM_SIGNATURE_CLASS: 1206 case JVM_SIGNATURE_ARRAY: 1207 size += OBJ_ID_SIZE; 1208 break; 1209 case JVM_SIGNATURE_LONG: 1210 case JVM_SIGNATURE_DOUBLE: 1211 size += 8; 1212 break; 1213 default: 1214 throw new RuntimeException("should not reach here"); 1215 } 1216 } 1217 return size; 1218 } 1219 1220 // We don't have allocation site info. We write a dummy 1221 // stack trace with this id. 1222 private static final int DUMMY_STACK_TRACE_ID = 1; 1223 private static final int EMPTY_FRAME_DEPTH = -1; 1224 1225 private DataOutputStream out; 1226 private FileOutputStream fos; 1227 private Debugger dbg; 1228 private ObjectHeap objectHeap; 1229 private SymbolTable symTbl; 1230 private ArrayList<Klass> KlassMap; 1231 1232 // oopSize of the debuggee 1233 private int OBJ_ID_SIZE; 1234 1235 // Added for hprof file format 1.0.2 support 1236 private boolean useSegmentedHeapDump; 1237 private long currentSegmentStart; 1238 1239 private long BOOLEAN_BASE_OFFSET; 1240 private long BYTE_BASE_OFFSET; 1241 private long CHAR_BASE_OFFSET; 1242 private long SHORT_BASE_OFFSET; 1243 private long INT_BASE_OFFSET; 1244 private long LONG_BASE_OFFSET; 1245 private long FLOAT_BASE_OFFSET; 1246 private long DOUBLE_BASE_OFFSET; 1247 private long OBJECT_BASE_OFFSET; 1248 1249 private long BOOLEAN_SIZE; 1250 private long BYTE_SIZE; 1251 private long CHAR_SIZE; 1252 private long SHORT_SIZE; 1253 private long INT_SIZE; 1254 private long LONG_SIZE; 1255 private long FLOAT_SIZE; 1256 private long DOUBLE_SIZE; 1257 1258 private static class ClassData { 1259 int instSize; 1260 List fields; 1261 1262 ClassData(int instSize, List fields) { 1263 this.instSize = instSize; 1264 this.fields = fields; 1265 } 1266 } 1267 1268 private Map classDataCache = new HashMap(); // <InstanceKlass, ClassData> 1269 }