< prev index next >

src/hotspot/share/services/heapDumper.cpp

Print this page

  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/javaClasses.inline.hpp"
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "gc/shared/gcVMOperations.hpp"
  35 #include "gc/shared/workgroup.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "memory/allocation.inline.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.hpp"
  40 #include "oops/klass.inline.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"


  43 #include "oops/oop.inline.hpp"
  44 #include "oops/typeArrayOop.inline.hpp"

  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/jniHandles.hpp"
  49 #include "runtime/os.hpp"
  50 #include "runtime/reflectionUtils.hpp"
  51 #include "runtime/thread.inline.hpp"
  52 #include "runtime/threadSMR.hpp"
  53 #include "runtime/vframe.hpp"
  54 #include "runtime/vmThread.hpp"
  55 #include "runtime/vmOperations.hpp"
  56 #include "services/heapDumper.hpp"
  57 #include "services/heapDumperCompression.hpp"
  58 #include "services/threadService.hpp"
  59 #include "utilities/macros.hpp"
  60 #include "utilities/ostream.hpp"
  61 
  62 /*
  63  * HPROF binary format - description copied from:
  64  *   src/share/demo/jvmti/hprof/hprof_io.c

 363   HPROF_GC_ROOT_JNI_GLOBAL      = 0x01,
 364   HPROF_GC_ROOT_JNI_LOCAL       = 0x02,
 365   HPROF_GC_ROOT_JAVA_FRAME      = 0x03,
 366   HPROF_GC_ROOT_NATIVE_STACK    = 0x04,
 367   HPROF_GC_ROOT_STICKY_CLASS    = 0x05,
 368   HPROF_GC_ROOT_THREAD_BLOCK    = 0x06,
 369   HPROF_GC_ROOT_MONITOR_USED    = 0x07,
 370   HPROF_GC_ROOT_THREAD_OBJ      = 0x08,
 371   HPROF_GC_CLASS_DUMP           = 0x20,
 372   HPROF_GC_INSTANCE_DUMP        = 0x21,
 373   HPROF_GC_OBJ_ARRAY_DUMP       = 0x22,
 374   HPROF_GC_PRIM_ARRAY_DUMP      = 0x23
 375 } hprofTag;
 376 
 377 // Default stack trace ID (used for dummy HPROF_TRACE record)
 378 enum {
 379   STACK_TRACE_ID = 1,
 380   INITIAL_CLASS_COUNT = 200
 381 };
 382 
















 383 // Supports I/O operations for a dump
 384 // Base class for dump and parallel dump
 385 class AbstractDumpWriter : public StackObj {
 386  protected:
 387   enum {
 388     io_buffer_max_size = 1*M,
 389     io_buffer_max_waste = 10*K,
 390     dump_segment_header_size = 9
 391   };
 392 
 393   char* _buffer;    // internal buffer
 394   size_t _size;
 395   size_t _pos;
 396 
 397   bool _in_dump_segment; // Are we currently in a dump segment?
 398   bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
 399   DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
 400   DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
 401 
 402   virtual void flush(bool force = false) = 0;
 403 
 404   char* buffer() const                          { return _buffer; }
 405   size_t buffer_size() const                    { return _size; }
 406   void set_position(size_t pos)                 { _pos = pos; }
 407 
 408   // Can be called if we have enough room in the buffer.
 409   void write_fast(void* s, size_t len);
 410 
 411   // Returns true if we have enough room in the buffer for 'len' bytes.
 412   bool can_write_fast(size_t len);



 413  public:
 414   AbstractDumpWriter() :
 415     _buffer(NULL),
 416     _size(io_buffer_max_size),
 417     _pos(0),
 418     _in_dump_segment(false) { }
 419 
 420   // total number of bytes written to the disk
 421   virtual julong bytes_written() const = 0;
 422   virtual char const* error() const = 0;
 423 


 424   size_t position() const                       { return _pos; }
 425   // writer functions
 426   virtual void write_raw(void* s, size_t len);
 427   void write_u1(u1 x);
 428   void write_u2(u2 x);
 429   void write_u4(u4 x);
 430   void write_u8(u8 x);
 431   void write_objectID(oop o);

 432   void write_symbolID(Symbol* o);
 433   void write_classID(Klass* k);
 434   void write_id(u4 x);
 435 
 436   // Start a new sub-record. Starts a new heap dump segment if needed.
 437   void start_sub_record(u1 tag, u4 len);
 438   // Ends the current sub-record.
 439   void end_sub_record();
 440   // Finishes the current dump segment if not already finished.
 441   void finish_dump_segment(bool force_flush = false);
 442   // Refresh to get new buffer
 443   void refresh() {
 444     assert (_in_dump_segment ==false, "Sanity check");
 445     _buffer = NULL;
 446     _size = io_buffer_max_size;
 447     _pos = 0;
 448     // Force flush to guarantee data from parallel dumper are written.
 449     flush(true);
 450   }
 451   // Called when finished to release the threads.

 503   u4 v;
 504   Bytes::put_Java_u4((address)&v, x);
 505   WRITE_KNOWN_TYPE((void*)&v, 4);
 506 }
 507 
 508 void AbstractDumpWriter::write_u8(u8 x) {
 509   u8 v;
 510   Bytes::put_Java_u8((address)&v, x);
 511   WRITE_KNOWN_TYPE((void*)&v, 8);
 512 }
 513 
 514 void AbstractDumpWriter::write_objectID(oop o) {
 515   address a = cast_from_oop<address>(o);
 516 #ifdef _LP64
 517   write_u8((u8)a);
 518 #else
 519   write_u4((u4)a);
 520 #endif
 521 }
 522 








 523 void AbstractDumpWriter::write_symbolID(Symbol* s) {
 524   address a = (address)((uintptr_t)s);
 525 #ifdef _LP64
 526   write_u8((u8)a);
 527 #else
 528   write_u4((u4)a);
 529 #endif
 530 }
 531 
 532 void AbstractDumpWriter::write_id(u4 x) {
 533 #ifdef _LP64
 534   write_u8((u8) x);
 535 #else
 536   write_u4(x);
 537 #endif
 538 }
 539 
 540 // We use java mirror as the class ID
 541 void AbstractDumpWriter::write_classID(Klass* k) {
 542   write_objectID(k->java_mirror());

 882 class DumperSupport : AllStatic {
 883  public:
 884 
 885   // write a header of the given type
 886   static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
 887 
 888   // returns hprof tag for the given type signature
 889   static hprofTag sig2tag(Symbol* sig);
 890   // returns hprof tag for the given basic type
 891   static hprofTag type2tag(BasicType type);
 892   // Returns the size of the data to write.
 893   static u4 sig2size(Symbol* sig);
 894 
 895   // returns the size of the instance of the given class
 896   static u4 instance_size(Klass* k);
 897 
 898   // dump a jfloat
 899   static void dump_float(AbstractDumpWriter* writer, jfloat f);
 900   // dump a jdouble
 901   static void dump_double(AbstractDumpWriter* writer, jdouble d);
 902   // dumps the raw value of the given field
 903   static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);

 904   // returns the size of the static fields; also counts the static fields
 905   static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
 906   // dumps static fields of the given class
 907   static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
 908   // dump the raw values of the instance fields of the given object
 909   static void dump_instance_fields(AbstractDumpWriter* writer, oop o);







 910   // get the count of the instance fields for a given class
 911   static u2 get_instance_fields_count(InstanceKlass* ik);
 912   // dumps the definition of the instance fields for a given class
 913   static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);


 914   // creates HPROF_GC_INSTANCE_DUMP record for the given object
 915   static void dump_instance(AbstractDumpWriter* writer, oop o);
 916   // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
 917   // array classes
 918   static void dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k);
 919   // creates HPROF_GC_CLASS_DUMP record for a given primitive array
 920   // class (and each multi-dimensional array class too)
 921   static void dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k);
 922 
 923   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
 924   static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
 925   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
 926   static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
 927   // create HPROF_FRAME record for the given method and bci
 928   static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
 929 
 930   // check if we need to truncate an array
 931   static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
 932 
 933   // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
 934   static void end_of_dump(AbstractDumpWriter* writer);
 935 
 936   static oop mask_dormant_archived_object(oop o) {
 937     if (o != NULL && o->klass()->java_mirror() == NULL) {
 938       // Ignore this object since the corresponding java mirror is not loaded.
 939       // Might be a dormant archive object.
 940       return NULL;
 941     } else {
 942       return o;
 943     }
 944   }
 945 };
 946 
 947 // write a header of the given type
 948 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
 949   writer->write_u1((u1)tag);
 950   writer->write_u4(0);                  // current ticks
 951   writer->write_u4(len);
 952 }
 953 
 954 // returns hprof tag for the given type signature
 955 hprofTag DumperSupport::sig2tag(Symbol* sig) {
 956   switch (sig->char_at(0)) {
 957     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;

 958     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
 959     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
 960     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
 961     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
 962     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
 963     case JVM_SIGNATURE_INT      : return HPROF_INT;
 964     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
 965     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
 966     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
 967     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 968   }
 969 }
 970 
 971 hprofTag DumperSupport::type2tag(BasicType type) {
 972   switch (type) {
 973     case T_BYTE     : return HPROF_BYTE;
 974     case T_CHAR     : return HPROF_CHAR;
 975     case T_FLOAT    : return HPROF_FLOAT;
 976     case T_DOUBLE   : return HPROF_DOUBLE;
 977     case T_INT      : return HPROF_INT;
 978     case T_LONG     : return HPROF_LONG;
 979     case T_SHORT    : return HPROF_SHORT;
 980     case T_BOOLEAN  : return HPROF_BOOLEAN;
 981     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
 982   }
 983 }
 984 
 985 u4 DumperSupport::sig2size(Symbol* sig) {
 986   switch (sig->char_at(0)) {
 987     case JVM_SIGNATURE_CLASS:

 988     case JVM_SIGNATURE_ARRAY: return sizeof(address);
 989     case JVM_SIGNATURE_BOOLEAN:
 990     case JVM_SIGNATURE_BYTE: return 1;
 991     case JVM_SIGNATURE_SHORT:
 992     case JVM_SIGNATURE_CHAR: return 2;
 993     case JVM_SIGNATURE_INT:
 994     case JVM_SIGNATURE_FLOAT: return 4;
 995     case JVM_SIGNATURE_LONG:
 996     case JVM_SIGNATURE_DOUBLE: return 8;
 997     default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
 998   }
 999 }
1000 
1001 // dump a jfloat
1002 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1003   if (g_isnan(f)) {
1004     writer->write_u4(0x7fc00000);    // collapsing NaNs
1005   } else {
1006     union {
1007       int i;

1010     u.f = (float)f;
1011     writer->write_u4((u4)u.i);
1012   }
1013 }
1014 
1015 // dump a jdouble
1016 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1017   union {
1018     jlong l;
1019     double d;
1020   } u;
1021   if (g_isnan(d)) {                 // collapsing NaNs
1022     u.l = (jlong)(0x7ff80000);
1023     u.l = (u.l << 32);
1024   } else {
1025     u.d = (double)d;
1026   }
1027   writer->write_u8((u8)u.l);
1028 }
1029 
1030 // dumps the raw value of the given field
1031 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {



1032   switch (type) {







1033     case JVM_SIGNATURE_CLASS :
1034     case JVM_SIGNATURE_ARRAY : {
1035       oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1036       if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1037         ResourceMark rm;
1038         log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1039                              p2i(o), o->klass()->external_name(),
1040                              p2i(obj), obj->klass()->external_name());
1041       }
1042       o = mask_dormant_archived_object(o);
1043       assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o));
1044       writer->write_objectID(o);
1045       break;
1046     }
1047     case JVM_SIGNATURE_BYTE : {
1048       jbyte b = obj->byte_field(offset);
1049       writer->write_u1((u1)b);
1050       break;
1051     }
1052     case JVM_SIGNATURE_CHAR : {

1140     field_count++;
1141     size += sizeof(address);
1142   }
1143 
1144   // We write the value itself plus a name and a one byte type tag per field.
1145   return size + field_count * (sizeof(address) + 1);
1146 }
1147 
1148 // dumps static fields of the given class
1149 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1150   InstanceKlass* ik = InstanceKlass::cast(k);
1151 
1152   // dump the field descriptors and raw values
1153   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1154     if (fld.access_flags().is_static()) {
1155       Symbol* sig = fld.signature();
1156 
1157       writer->write_symbolID(fld.name());   // name
1158       writer->write_u1(sig2tag(sig));       // type
1159 



1160       // value
1161       dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1162     }
1163   }
1164 
1165   // Add resolved_references for each class that has them
1166   oop resolved_references = ik->constants()->resolved_references_or_null();
1167   if (resolved_references != NULL) {
1168     writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1169     writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1170     writer->write_objectID(resolved_references);
1171 
1172     // Also write any previous versions
1173     InstanceKlass* prev = ik->previous_versions();
1174     while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
1175       writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1176       writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1177       writer->write_objectID(prev->constants()->resolved_references());
1178       prev = prev->previous_versions();
1179     }
1180   }
1181 
1182   // Add init lock to the end if the class is not yet initialized
1183   oop init_lock = ik->init_lock();
1184   if (init_lock != NULL) {
1185     writer->write_symbolID(vmSymbols::init_lock_name());         // name
1186     writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1187     writer->write_objectID(init_lock);
1188   }
1189 }
1190 

1191 // dump the raw values of the instance fields of the given object
1192 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o) {
1193   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1194 
1195   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1196     if (!fld.access_flags().is_static()) {
1197       Symbol* sig = fld.signature();
1198       dump_field_value(writer, sig->char_at(0), o, fld.offset());
1199     }
1200   }
1201 }
1202 
1203 // dumps the definition of the instance fields for a given class
















1204 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1205   u2 field_count = 0;
1206 
1207   for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1208     if (!fldc.access_flags().is_static()) field_count++;
1209   }
1210 
1211   return field_count;
1212 }
1213 
1214 // dumps the definition of the instance fields for a given class
1215 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1216   InstanceKlass* ik = InstanceKlass::cast(k);
1217 
1218   // dump the field descriptors
1219   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1220     if (!fld.access_flags().is_static()) {
1221       Symbol* sig = fld.signature();
1222 
1223       writer->write_symbolID(fld.name());   // name
1224       writer->write_u1(sig2tag(sig));       // type
1225     }
1226   }
1227 }
1228 































1229 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1230 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o) {
1231   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1232   u4 is = instance_size(ik);
1233   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1234 
1235   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1236   writer->write_objectID(o);
1237   writer->write_u4(STACK_TRACE_ID);
1238 
1239   // class ID
1240   writer->write_classID(ik);
1241 
1242   // number of bytes that follow
1243   writer->write_u4(is);
1244 


1245   // field values
1246   dump_instance_fields(writer, o);
1247 
1248   writer->end_sub_record();



1249 }
1250 
1251 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
1252 // its array classes
1253 void DumperSupport::dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k) {
1254   InstanceKlass* ik = InstanceKlass::cast(k);
1255 
1256   // We can safepoint and do a heap dump at a point where we have a Klass,
1257   // but no java mirror class has been setup for it. So we need to check
1258   // that the class is at least loaded, to avoid crash from a null mirror.
1259   if (!ik->is_loaded()) {
1260     return;
1261   }
1262 
1263   u2 static_fields_count = 0;
1264   u4 static_size = get_static_fields_size(ik, static_fields_count);
1265   u2 instance_fields_count = get_instance_fields_count(ik);
1266   u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1267   u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1268 

1288   writer->write_objectID(oop(NULL));
1289   writer->write_objectID(oop(NULL));
1290 
1291   // instance size
1292   writer->write_u4(DumperSupport::instance_size(ik));
1293 
1294   // size of constant pool - ignored by HAT 1.1
1295   writer->write_u2(0);
1296 
1297   // static fields
1298   writer->write_u2(static_fields_count);
1299   dump_static_fields(writer, ik);
1300 
1301   // description of instance fields
1302   writer->write_u2(instance_fields_count);
1303   dump_instance_field_descriptors(writer, ik);
1304 
1305   writer->end_sub_record();
1306 
1307   // array classes
1308   k = ik->array_klass_or_null();
1309   while (k != NULL) {
1310     assert(k->is_objArray_klass(), "not an ObjArrayKlass");
1311 
1312     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1313     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1314     writer->write_classID(k);
1315     writer->write_u4(STACK_TRACE_ID);
1316 
1317     // super class of array classes is java.lang.Object
1318     java_super = k->java_super();
1319     assert(java_super != NULL, "checking");
1320     writer->write_classID(java_super);
1321 
1322     writer->write_objectID(ik->class_loader());
1323     writer->write_objectID(ik->signers());
1324     writer->write_objectID(ik->protection_domain());
1325 
1326     writer->write_objectID(oop(NULL));    // reserved
1327     writer->write_objectID(oop(NULL));
1328     writer->write_u4(0);             // instance size
1329     writer->write_u2(0);             // constant pool
1330     writer->write_u2(0);             // static fields
1331     writer->write_u2(0);             // instance fields
1332 
1333     writer->end_sub_record();
1334 
1335     // get the array class for the next rank
1336     k = k->array_klass_or_null();
1337   }
1338 }
1339 
1340 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
1341 // class (and each multi-dimensional array class too)
1342 void DumperSupport::dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k) {
1343  // array classes
1344  while (k != NULL) {
1345     Klass* klass = k;
1346 
1347     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1348     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1349     writer->write_classID(klass);
1350     writer->write_u4(STACK_TRACE_ID);
1351 
1352     // super class of array classes is java.lang.Object
1353     InstanceKlass* java_super = klass->java_super();
1354     assert(java_super != NULL, "checking");
1355     writer->write_classID(java_super);
1356 
1357     writer->write_objectID(oop(NULL));    // loader
1358     writer->write_objectID(oop(NULL));    // signers
1359     writer->write_objectID(oop(NULL));    // protection domain
1360 
1361     writer->write_objectID(oop(NULL));    // reserved
1362     writer->write_objectID(oop(NULL));
1363     writer->write_u4(0);             // instance size
1364     writer->write_u2(0);             // constant pool
1365     writer->write_u2(0);             // static fields
1366     writer->write_u2(0);             // instance fields
1367 
1368     writer->end_sub_record();
1369 
1370     // get the array class for the next rank
1371     k = klass->array_klass_or_null();
1372   }
1373 }
1374 
1375 // Hprof uses an u4 as record length field,
1376 // which means we need to truncate arrays that are too long.
1377 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1378   BasicType type = ArrayKlass::cast(array->klass())->element_type();
1379   assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1380 
1381   int length = array->length();
1382 
1383   int type_size;
1384   if (type == T_OBJECT) {
1385     type_size = sizeof(address);
1386   } else {
1387     type_size = type2aelembytes(type);
1388   }
1389 
1390   size_t length_in_bytes = (size_t)length * type_size;
1391   uint max_bytes = max_juint - header_size;
1392 
1393   if (length_in_bytes > max_bytes) {
1394     length = max_bytes / type_size;
1395     length_in_bytes = (size_t)length * type_size;
1396 
1397     warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1398             type2name_tab[type], array->length(), length);
1399   }
1400   return length;
1401 }
1402 
1403 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1404 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1405   // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1406   short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1407   int length = calculate_array_max_length(writer, array, header_size);
1408   u4 size = header_size + length * sizeof(address);
1409 
1410   writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1411   writer->write_objectID(array);
1412   writer->write_u4(STACK_TRACE_ID);
1413   writer->write_u4(length);
1414 
1415   // array class ID
1416   writer->write_classID(array->klass());
1417 
1418   // [id]* elements
1419   for (int index = 0; index < length; index++) {
1420     oop o = array->obj_at(index);
1421     if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1422       ResourceMark rm;
1423       log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1424                            p2i(o), o->klass()->external_name(),
1425                            p2i(array), array->klass()->external_name());












1426     }
1427     o = mask_dormant_archived_object(o);
1428     writer->write_objectID(o);
1429   }
1430 
1431   writer->end_sub_record();













1432 }
1433 
1434 #define WRITE_ARRAY(Array, Type, Size, Length) \
1435   for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1436 
1437 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1438 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1439   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1440   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1441   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1442 
1443   int length = calculate_array_max_length(writer, array, header_size);
1444   int type_size = type2aelembytes(type);
1445   u4 length_in_bytes = (u4)length * type_size;
1446   u4 size = header_size + length_in_bytes;
1447 
1448   writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1449   writer->write_objectID(array);
1450   writer->write_u4(STACK_TRACE_ID);
1451   writer->write_u4(length);

1754     if (!java_lang_Class::is_primitive(o)) {
1755       return;
1756     }
1757   }
1758 
1759   if (DumperSupport::mask_dormant_archived_object(o) == NULL) {
1760     log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
1761     return;
1762   }
1763 
1764   // If large object list exists and it is large object/array,
1765   // add oop into the list and skip scan. VM thread will process it later.
1766   if (_list != NULL && is_large(o)) {
1767     _list->atomic_push(o);
1768     return;
1769   }
1770 
1771   if (o->is_instance()) {
1772     // create a HPROF_GC_INSTANCE record for each object
1773     DumperSupport::dump_instance(writer(), o);
1774   } else if (o->is_objArray()) {
1775     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1776     DumperSupport::dump_object_array(writer(), objArrayOop(o));
1777   } else if (o->is_typeArray()) {
1778     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1779     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1780   }
1781 }
1782 
1783 bool HeapObjectDumper::is_large(oop o) {
1784   size_t size = 0;
1785   if (o->is_instance()) {
1786     // Use o->size() * 8 as the upper limit of instance size to avoid iterating static fields
1787     size = o->size() * 8;
1788   } else if (o->is_objArray()) {
1789     objArrayOop array = objArrayOop(o);
1790     BasicType type = ArrayKlass::cast(array->klass())->element_type();
1791     assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1792     int length = array->length();
1793     int type_size = sizeof(address);
1794     size = (size_t)length * type_size;
1795   } else if (o->is_typeArray()) {
1796     typeArrayOop array = typeArrayOop(o);

  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/javaClasses.inline.hpp"
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "gc/shared/gcVMOperations.hpp"
  35 #include "gc/shared/workgroup.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "memory/allocation.inline.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.hpp"
  40 #include "oops/klass.inline.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/flatArrayKlass.hpp"
  44 #include "oops/flatArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/typeArrayOop.inline.hpp"
  47 #include "runtime/fieldDescriptor.inline.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/jniHandles.hpp"
  52 #include "runtime/os.hpp"
  53 #include "runtime/reflectionUtils.hpp"
  54 #include "runtime/thread.inline.hpp"
  55 #include "runtime/threadSMR.hpp"
  56 #include "runtime/vframe.hpp"
  57 #include "runtime/vmThread.hpp"
  58 #include "runtime/vmOperations.hpp"
  59 #include "services/heapDumper.hpp"
  60 #include "services/heapDumperCompression.hpp"
  61 #include "services/threadService.hpp"
  62 #include "utilities/macros.hpp"
  63 #include "utilities/ostream.hpp"
  64 
  65 /*
  66  * HPROF binary format - description copied from:
  67  *   src/share/demo/jvmti/hprof/hprof_io.c

 366   HPROF_GC_ROOT_JNI_GLOBAL      = 0x01,
 367   HPROF_GC_ROOT_JNI_LOCAL       = 0x02,
 368   HPROF_GC_ROOT_JAVA_FRAME      = 0x03,
 369   HPROF_GC_ROOT_NATIVE_STACK    = 0x04,
 370   HPROF_GC_ROOT_STICKY_CLASS    = 0x05,
 371   HPROF_GC_ROOT_THREAD_BLOCK    = 0x06,
 372   HPROF_GC_ROOT_MONITOR_USED    = 0x07,
 373   HPROF_GC_ROOT_THREAD_OBJ      = 0x08,
 374   HPROF_GC_CLASS_DUMP           = 0x20,
 375   HPROF_GC_INSTANCE_DUMP        = 0x21,
 376   HPROF_GC_OBJ_ARRAY_DUMP       = 0x22,
 377   HPROF_GC_PRIM_ARRAY_DUMP      = 0x23
 378 } hprofTag;
 379 
 380 // Default stack trace ID (used for dummy HPROF_TRACE record)
 381 enum {
 382   STACK_TRACE_ID = 1,
 383   INITIAL_CLASS_COUNT = 200
 384 };
 385 
 386 
 387 // Inlined fields of primitive classes are dumped as identity objects and require unique object ids.
 388 // We cannot use address of the object in container oop (as we do for identity objects)
 389 // because the address can be the same for inlined object which contains inlined field with offset 0.
 390 class InlinedObjectSupport : public StackObj {
 391     friend class AbstractDumpWriter;
 392     InlinedObjectSupport(int initial_value = 1) : _counter(initial_value) {}
 393 
 394     int _counter;
 395     int getId() { return _counter++; }
 396 public:
 397     InlinedObjectSupport save() const {
 398         return InlinedObjectSupport(_counter);
 399     }
 400 };
 401 
 402 // Supports I/O operations for a dump
 403 // Base class for dump and parallel dump
 404 class AbstractDumpWriter : public StackObj {
 405  protected:
 406   enum {
 407     io_buffer_max_size = 1*M,
 408     io_buffer_max_waste = 10*K,
 409     dump_segment_header_size = 9
 410   };
 411 
 412   char* _buffer;    // internal buffer
 413   size_t _size;
 414   size_t _pos;
 415 
 416   bool _in_dump_segment; // Are we currently in a dump segment?
 417   bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
 418   DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
 419   DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
 420 
 421   virtual void flush(bool force = false) = 0;
 422 
 423   char* buffer() const                          { return _buffer; }
 424   size_t buffer_size() const                    { return _size; }
 425   void set_position(size_t pos)                 { _pos = pos; }
 426 
 427   // Can be called if we have enough room in the buffer.
 428   void write_fast(void* s, size_t len);
 429 
 430   // Returns true if we have enough room in the buffer for 'len' bytes.
 431   bool can_write_fast(size_t len);
 432 
 433   InlinedObjectSupport _inlined_object_support;
 434 
 435  public:
 436   AbstractDumpWriter() :
 437     _buffer(NULL),
 438     _size(io_buffer_max_size),
 439     _pos(0),
 440     _in_dump_segment(false) { }
 441 
 442   // total number of bytes written to the disk
 443   virtual julong bytes_written() const = 0;
 444   virtual char const* error() const = 0;
 445 
 446   InlinedObjectSupport& inlined_object_support() { return _inlined_object_support; }
 447 
 448   size_t position() const                       { return _pos; }
 449   // writer functions
 450   virtual void write_raw(void* s, size_t len);
 451   void write_u1(u1 x);
 452   void write_u2(u2 x);
 453   void write_u4(u4 x);
 454   void write_u8(u8 x);
 455   void write_objectID(oop o);
 456   void write_inlinedObjectID(InlinedObjectSupport &inlinedObjectSupport);
 457   void write_symbolID(Symbol* o);
 458   void write_classID(Klass* k);
 459   void write_id(u4 x);
 460 
 461   // Start a new sub-record. Starts a new heap dump segment if needed.
 462   void start_sub_record(u1 tag, u4 len);
 463   // Ends the current sub-record.
 464   void end_sub_record();
 465   // Finishes the current dump segment if not already finished.
 466   void finish_dump_segment(bool force_flush = false);
 467   // Refresh to get new buffer
 468   void refresh() {
 469     assert (_in_dump_segment ==false, "Sanity check");
 470     _buffer = NULL;
 471     _size = io_buffer_max_size;
 472     _pos = 0;
 473     // Force flush to guarantee data from parallel dumper are written.
 474     flush(true);
 475   }
 476   // Called when finished to release the threads.

 528   u4 v;
 529   Bytes::put_Java_u4((address)&v, x);
 530   WRITE_KNOWN_TYPE((void*)&v, 4);
 531 }
 532 
 533 void AbstractDumpWriter::write_u8(u8 x) {
 534   u8 v;
 535   Bytes::put_Java_u8((address)&v, x);
 536   WRITE_KNOWN_TYPE((void*)&v, 8);
 537 }
 538 
 539 void AbstractDumpWriter::write_objectID(oop o) {
 540   address a = cast_from_oop<address>(o);
 541 #ifdef _LP64
 542   write_u8((u8)a);
 543 #else
 544   write_u4((u4)a);
 545 #endif
 546 }
 547 
 548 void AbstractDumpWriter::write_inlinedObjectID(InlinedObjectSupport& inlinedObjectSupport) {
 549 #ifdef _LP64
 550   write_u8(inlinedObjectSupport.getId());
 551 #else
 552   write_u4(inlinedObjectSupport.getId());
 553 #endif
 554 }
 555 
 556 void AbstractDumpWriter::write_symbolID(Symbol* s) {
 557   address a = (address)((uintptr_t)s);
 558 #ifdef _LP64
 559   write_u8((u8)a);
 560 #else
 561   write_u4((u4)a);
 562 #endif
 563 }
 564 
 565 void AbstractDumpWriter::write_id(u4 x) {
 566 #ifdef _LP64
 567   write_u8((u8) x);
 568 #else
 569   write_u4(x);
 570 #endif
 571 }
 572 
 573 // We use java mirror as the class ID
 574 void AbstractDumpWriter::write_classID(Klass* k) {
 575   write_objectID(k->java_mirror());

 915 class DumperSupport : AllStatic {
 916  public:
 917 
 918   // write a header of the given type
 919   static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
 920 
 921   // returns hprof tag for the given type signature
 922   static hprofTag sig2tag(Symbol* sig);
 923   // returns hprof tag for the given basic type
 924   static hprofTag type2tag(BasicType type);
 925   // Returns the size of the data to write.
 926   static u4 sig2size(Symbol* sig);
 927 
 928   // returns the size of the instance of the given class
 929   static u4 instance_size(Klass* k);
 930 
 931   // dump a jfloat
 932   static void dump_float(AbstractDumpWriter* writer, jfloat f);
 933   // dump a jdouble
 934   static void dump_double(AbstractDumpWriter* writer, jdouble d);
 935   // dumps the raw value of the given field; obj and offset specify the object (offset is 0 for identity objects)
 936   // for inlined fields writed object id generated by writer->inlined_object_support()
 937   static void dump_field_value(AbstractDumpWriter* writer, const FieldStream& fld, oop obj, int offset);
 938   // returns the size of the static fields; also counts the static fields
 939   static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
 940   // dumps static fields of the given class
 941   static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
 942   // dump the raw values of the instance fields of the given identity or inlined object;
 943   // for identity objects offset is 0 and 'klass' is o->klass(),
 944   // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class
 945   static void dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass* klass);
 946   // dump inlined instance fields of the given object (identity or inlined);
 947   // o is the holder object, offset and klass specify flattened field (or field of flattened field, etc.);
 948   // for identity object offset is 0 and klass is o->klass()
 949   static void dump_inlined_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass* klass, InlinedObjectSupport &ios);
 950 
 951   // get the count of the instance fields for a given class
 952   static u2 get_instance_fields_count(InstanceKlass* ik);
 953   // dumps the definition of the instance fields for a given class
 954   static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
 955   // creates HPROF_GC_INSTANCE_DUMP record for the given inlined object
 956   static void dump_inlined_object(AbstractDumpWriter* writer, oop holder, int offset, InlineKlass* klass, InlinedObjectSupport& ios);
 957   // creates HPROF_GC_INSTANCE_DUMP record for the given object
 958   static void dump_instance(AbstractDumpWriter* writer, oop o);
 959   // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
 960   // array classes
 961   static void dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k);
 962   // creates HPROF_GC_CLASS_DUMP record for a given primitive array
 963   // class (and each multi-dimensional array class too)
 964   static void dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k);
 965 
 966   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
 967   static void dump_object_array(AbstractDumpWriter* writer, arrayOop array);
 968   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
 969   static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
 970   // create HPROF_FRAME record for the given method and bci
 971   static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
 972 
 973   // check if we need to truncate an array
 974   static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
 975 
 976   // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
 977   static void end_of_dump(AbstractDumpWriter* writer);
 978 
 979   static oop mask_dormant_archived_object(oop o) {
 980     if (o != NULL && o->klass()->java_mirror() == NULL) {
 981       // Ignore this object since the corresponding java mirror is not loaded.
 982       // Might be a dormant archive object.
 983       return NULL;
 984     } else {
 985       return o;
 986     }
 987   }
 988 };
 989 
 990 // write a header of the given type
 991 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
 992   writer->write_u1((u1)tag);
 993   writer->write_u4(0);                  // current ticks
 994   writer->write_u4(len);
 995 }
 996 
 997 // returns hprof tag for the given type signature
 998 hprofTag DumperSupport::sig2tag(Symbol* sig) {
 999   switch (sig->char_at(0)) {
1000     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;
1001     case JVM_SIGNATURE_INLINE_TYPE: return HPROF_NORMAL_OBJECT;
1002     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
1003     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
1004     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
1005     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
1006     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
1007     case JVM_SIGNATURE_INT      : return HPROF_INT;
1008     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
1009     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
1010     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
1011     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1012   }
1013 }
1014 
1015 hprofTag DumperSupport::type2tag(BasicType type) {
1016   switch (type) {
1017     case T_BYTE     : return HPROF_BYTE;
1018     case T_CHAR     : return HPROF_CHAR;
1019     case T_FLOAT    : return HPROF_FLOAT;
1020     case T_DOUBLE   : return HPROF_DOUBLE;
1021     case T_INT      : return HPROF_INT;
1022     case T_LONG     : return HPROF_LONG;
1023     case T_SHORT    : return HPROF_SHORT;
1024     case T_BOOLEAN  : return HPROF_BOOLEAN;
1025     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1026   }
1027 }
1028 
1029 u4 DumperSupport::sig2size(Symbol* sig) {
1030   switch (sig->char_at(0)) {
1031     case JVM_SIGNATURE_CLASS:
1032     case JVM_SIGNATURE_INLINE_TYPE:
1033     case JVM_SIGNATURE_ARRAY: return sizeof(address);
1034     case JVM_SIGNATURE_BOOLEAN:
1035     case JVM_SIGNATURE_BYTE: return 1;
1036     case JVM_SIGNATURE_SHORT:
1037     case JVM_SIGNATURE_CHAR: return 2;
1038     case JVM_SIGNATURE_INT:
1039     case JVM_SIGNATURE_FLOAT: return 4;
1040     case JVM_SIGNATURE_LONG:
1041     case JVM_SIGNATURE_DOUBLE: return 8;
1042     default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
1043   }
1044 }
1045 
1046 // dump a jfloat
1047 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1048   if (g_isnan(f)) {
1049     writer->write_u4(0x7fc00000);    // collapsing NaNs
1050   } else {
1051     union {
1052       int i;

1055     u.f = (float)f;
1056     writer->write_u4((u4)u.i);
1057   }
1058 }
1059 
1060 // dump a jdouble
1061 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1062   union {
1063     jlong l;
1064     double d;
1065   } u;
1066   if (g_isnan(d)) {                 // collapsing NaNs
1067     u.l = (jlong)(0x7ff80000);
1068     u.l = (u.l << 32);
1069   } else {
1070     u.d = (double)d;
1071   }
1072   writer->write_u8((u8)u.l);
1073 }
1074 
1075 // dumps the raw value of the given field; obj and offset specify the object (offset is 0 for identity objects)
1076 // for inlined fields writed object id generated by writer->inlined_object_support()
1077 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, const FieldStream& fld, oop obj, int offset) {
1078   char type = fld.signature()->char_at(0);
1079   offset += fld.offset();
1080   switch (type) {
1081     case JVM_SIGNATURE_INLINE_TYPE: {
1082       if (fld.field_descriptor().is_inlined()) {
1083         writer->write_inlinedObjectID(writer->inlined_object_support());
1084         break;
1085       }
1086     }
1087     // pass through
1088     case JVM_SIGNATURE_CLASS :
1089     case JVM_SIGNATURE_ARRAY : {
1090       oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1091       if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1092         ResourceMark rm;
1093         log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1094                              p2i(o), o->klass()->external_name(),
1095                              p2i(obj), obj->klass()->external_name());
1096       }
1097       o = mask_dormant_archived_object(o);
1098       assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o));
1099       writer->write_objectID(o);
1100       break;
1101     }
1102     case JVM_SIGNATURE_BYTE : {
1103       jbyte b = obj->byte_field(offset);
1104       writer->write_u1((u1)b);
1105       break;
1106     }
1107     case JVM_SIGNATURE_CHAR : {

1195     field_count++;
1196     size += sizeof(address);
1197   }
1198 
1199   // We write the value itself plus a name and a one byte type tag per field.
1200   return size + field_count * (sizeof(address) + 1);
1201 }
1202 
1203 // dumps static fields of the given class
1204 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1205   InstanceKlass* ik = InstanceKlass::cast(k);
1206 
1207   // dump the field descriptors and raw values
1208   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1209     if (fld.access_flags().is_static()) {
1210       Symbol* sig = fld.signature();
1211 
1212       writer->write_symbolID(fld.name());   // name
1213       writer->write_u1(sig2tag(sig));       // type
1214 
1215       // if this changes, need to handle this properly (dump inlined objects after dump_static_fields)
1216       assert(!fld.field_descriptor().is_inlined(), "static fields cannot be inlined");
1217 
1218       // value
1219       dump_field_value(writer, fld, ik->java_mirror(), 0);
1220     }
1221   }
1222 
1223   // Add resolved_references for each class that has them
1224   oop resolved_references = ik->constants()->resolved_references_or_null();
1225   if (resolved_references != NULL) {
1226     writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1227     writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1228     writer->write_objectID(resolved_references);
1229 
1230     // Also write any previous versions
1231     InstanceKlass* prev = ik->previous_versions();
1232     while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
1233       writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1234       writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1235       writer->write_objectID(prev->constants()->resolved_references());
1236       prev = prev->previous_versions();
1237     }
1238   }
1239 
1240   // Add init lock to the end if the class is not yet initialized
1241   oop init_lock = ik->init_lock();
1242   if (init_lock != NULL) {
1243     writer->write_symbolID(vmSymbols::init_lock_name());         // name
1244     writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1245     writer->write_objectID(init_lock);
1246   }
1247 }
1248 
1249 
1250 // dump the raw values of the instance fields of the given object
1251 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass *klass) {
1252   InstanceKlass* ik = InstanceKlass::cast(klass);
1253 
1254   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1255     if (!fld.access_flags().is_static()) {
1256       dump_field_value(writer, fld, o, offset);

1257     }
1258   }
1259 }
1260 
1261 void DumperSupport::dump_inlined_instance_fields(AbstractDumpWriter *writer, oop o, int offset, Klass *klass, InlinedObjectSupport &ios) {
1262   InstanceKlass* ik = InstanceKlass::cast(klass);
1263 
1264   assert(&ios != &writer->inlined_object_support(), "must be saved copy");
1265 
1266   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1267     if (!fld.access_flags().is_static()) {
1268       if (fld.field_descriptor().is_inlined()) {
1269         InstanceKlass* holder_klass = fld.field_descriptor().field_holder();
1270         InlineKlass* field_klass = InlineKlass::cast(holder_klass->get_inline_type_field_klass(fld.index()));
1271         dump_inlined_object(writer, o, offset + fld.offset(), field_klass, ios);
1272       }
1273     }
1274   }
1275 }
1276 
1277 // gets the count of the instance fields for a given class
1278 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1279   u2 field_count = 0;
1280 
1281   for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1282     if (!fldc.access_flags().is_static()) field_count++;
1283   }
1284 
1285   return field_count;
1286 }
1287 
1288 // dumps the definition of the instance fields for a given class
1289 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1290   InstanceKlass* ik = InstanceKlass::cast(k);
1291 
1292   // dump the field descriptors
1293   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1294     if (!fld.access_flags().is_static()) {
1295       Symbol* sig = fld.signature();
1296 
1297       writer->write_symbolID(fld.name());   // name
1298       writer->write_u1(sig2tag(sig));       // type
1299     }
1300   }
1301 }
1302 
1303 // creates HPROF_GC_INSTANCE_DUMP record for the given inlined object
1304 void DumperSupport::dump_inlined_object(AbstractDumpWriter* writer, oop holder, int offset, InlineKlass* klass, InlinedObjectSupport& ios) {
1305   u4 is = instance_size(klass);
1306   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1307 
1308   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1309   writer->write_inlinedObjectID(ios);
1310 
1311   writer->write_u4(STACK_TRACE_ID);
1312 
1313   // class ID
1314   writer->write_classID(klass);
1315 
1316   // number of bytes that follow
1317   writer->write_u4(is);
1318 
1319   // the object if flattened, so all fields are stored without headers
1320   // update offset here instead of handling it in both dump_instance_fields and dump_inlined_instance_fields
1321   offset -= klass->first_field_offset();
1322 
1323   InlinedObjectSupport saved_ios = writer->inlined_object_support().save();
1324 
1325   // field values
1326   dump_instance_fields(writer, holder, offset, klass);
1327 
1328   writer->end_sub_record();
1329 
1330   // dump flattened fields
1331   dump_inlined_instance_fields(writer, holder, offset, klass, saved_ios);
1332 }
1333 
1334 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1335 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o) {
1336   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1337   u4 is = instance_size(ik);
1338   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1339 
1340   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1341   writer->write_objectID(o);
1342   writer->write_u4(STACK_TRACE_ID);
1343 
1344   // class ID
1345   writer->write_classID(ik);
1346 
1347   // number of bytes that follow
1348   writer->write_u4(is);
1349 
1350   InlinedObjectSupport saved_ios = writer->inlined_object_support().save();
1351 
1352   // field values
1353   dump_instance_fields(writer, o, 0, o->klass());
1354 
1355   writer->end_sub_record();
1356 
1357   // dump inlined fields
1358   dump_inlined_instance_fields(writer, o, 0, o->klass(), saved_ios);
1359 }
1360 
1361 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
1362 // its array classes
1363 void DumperSupport::dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k) {
1364   InstanceKlass* ik = InstanceKlass::cast(k);
1365 
1366   // We can safepoint and do a heap dump at a point where we have a Klass,
1367   // but no java mirror class has been setup for it. So we need to check
1368   // that the class is at least loaded, to avoid crash from a null mirror.
1369   if (!ik->is_loaded()) {
1370     return;
1371   }
1372 
1373   u2 static_fields_count = 0;
1374   u4 static_size = get_static_fields_size(ik, static_fields_count);
1375   u2 instance_fields_count = get_instance_fields_count(ik);
1376   u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1377   u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1378 

1398   writer->write_objectID(oop(NULL));
1399   writer->write_objectID(oop(NULL));
1400 
1401   // instance size
1402   writer->write_u4(DumperSupport::instance_size(ik));
1403 
1404   // size of constant pool - ignored by HAT 1.1
1405   writer->write_u2(0);
1406 
1407   // static fields
1408   writer->write_u2(static_fields_count);
1409   dump_static_fields(writer, ik);
1410 
1411   // description of instance fields
1412   writer->write_u2(instance_fields_count);
1413   dump_instance_field_descriptors(writer, ik);
1414 
1415   writer->end_sub_record();
1416 
1417   // array classes
1418   k = k->array_klass_or_null();
1419   while (k != NULL) {
1420     assert(k->is_objArray_klass(), "not an ObjArrayKlass");
1421 
1422     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1423     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1424     writer->write_classID(k);
1425     writer->write_u4(STACK_TRACE_ID);
1426 
1427     // super class of array classes is java.lang.Object
1428     java_super = k->java_super();
1429     assert(java_super != NULL, "checking");
1430     writer->write_classID(java_super);
1431 
1432     writer->write_objectID(ik->class_loader());
1433     writer->write_objectID(ik->signers());
1434     writer->write_objectID(ik->protection_domain());
1435 
1436     writer->write_objectID(oop(NULL));    // reserved
1437     writer->write_objectID(oop(NULL));
1438     writer->write_u4(0);             // instance size
1439     writer->write_u2(0);             // constant pool
1440     writer->write_u2(0);             // static fields
1441     writer->write_u2(0);             // instance fields
1442 
1443     writer->end_sub_record();
1444 
1445     // get the array class for the next rank
1446     k = k->array_klass_or_null();
1447   }
1448 }
1449 
1450 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
1451 // class (and each multi-dimensional array class too)
1452 void DumperSupport::dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k) {
1453   // array classes
1454   while (k != NULL) {
1455     Klass* klass = k;
1456 
1457     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1458     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1459     writer->write_classID(klass);
1460     writer->write_u4(STACK_TRACE_ID);
1461 
1462     // super class of array classes is java.lang.Object
1463     InstanceKlass* java_super = klass->java_super();
1464     assert(java_super != NULL, "checking");
1465     writer->write_classID(java_super);
1466 
1467     writer->write_objectID(oop(NULL));    // loader
1468     writer->write_objectID(oop(NULL));    // signers
1469     writer->write_objectID(oop(NULL));    // protection domain
1470 
1471     writer->write_objectID(oop(NULL));    // reserved
1472     writer->write_objectID(oop(NULL));
1473     writer->write_u4(0);             // instance size
1474     writer->write_u2(0);             // constant pool
1475     writer->write_u2(0);             // static fields
1476     writer->write_u2(0);             // instance fields
1477 
1478     writer->end_sub_record();
1479 
1480     // get the array class for the next rank
1481     k = klass->array_klass_or_null();
1482   }
1483 }
1484 
1485 // Hprof uses an u4 as record length field,
1486 // which means we need to truncate arrays that are too long.
1487 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1488   BasicType type = ArrayKlass::cast(array->klass())->element_type();
1489   assert((type >= T_BOOLEAN && type <= T_OBJECT) || type == T_INLINE_TYPE, "invalid array element type");
1490 
1491   int length = array->length();
1492 
1493   int type_size;
1494   if (type == T_OBJECT || type == T_INLINE_TYPE) {
1495     type_size = sizeof(address);
1496   } else {
1497     type_size = type2aelembytes(type);
1498   }
1499 
1500   size_t length_in_bytes = (size_t)length * type_size;
1501   uint max_bytes = max_juint - header_size;
1502 
1503   if (length_in_bytes > max_bytes) {
1504     length = max_bytes / type_size;
1505     length_in_bytes = (size_t)length * type_size;
1506 
1507     warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1508             type2name_tab[type], array->length(), length);
1509   }
1510   return length;
1511 }
1512 
1513 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1514 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, arrayOop array) {
1515   // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1516   short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1517   int length = calculate_array_max_length(writer, array, header_size);
1518   u4 size = header_size + length * sizeof(address);
1519 
1520   writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1521   writer->write_objectID(array);
1522   writer->write_u4(STACK_TRACE_ID);
1523   writer->write_u4(length);
1524 
1525   // array class ID
1526   writer->write_classID(array->klass());
1527 
1528   InlinedObjectSupport ios = writer->inlined_object_support().save();
1529   if (array->is_objArray()) {
1530     // [id]* elements
1531     objArrayOop objArray = objArrayOop(array);
1532     for (int index = 0; index < length; index++) {
1533       oop o = objArray->obj_at(index);
1534       if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1535         ResourceMark rm;
1536         log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1537                              p2i(o), o->klass()->external_name(),
1538                              p2i(array), array->klass()->external_name());
1539       }
1540       o = mask_dormant_archived_object(o);
1541       writer->write_objectID(o);
1542     }
1543   } else { // flatArray
1544     // [id]* elements
1545     flatArrayOop flatArray = flatArrayOop(array);
1546     for (int index = 0; index < length; index++) {
1547       writer->write_inlinedObjectID(writer->inlined_object_support());
1548     }


1549   }
1550 
1551   writer->end_sub_record();
1552 
1553   if (array->is_flatArray()) {
1554     flatArrayOop flatArray = flatArrayOop(array);
1555     FlatArrayKlass* vaklass = FlatArrayKlass::cast(flatArray->klass());
1556     InlineKlass* vklass = vaklass->element_klass();
1557     for (int index = 0; index < length; index++) {
1558       // need offset in the holder to read inlined object. calculate it from flatArrayOop::value_at_addr()
1559       int offset = (int)((address)flatArray->value_at_addr(index, vaklass->layout_helper())
1560                         - cast_from_oop<address>(flatArray));
1561 
1562       dump_inlined_object(writer, flatArray, offset, vklass, ios);
1563     }
1564   }
1565 }
1566 
1567 #define WRITE_ARRAY(Array, Type, Size, Length) \
1568   for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1569 
1570 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1571 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1572   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1573   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1574   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1575 
1576   int length = calculate_array_max_length(writer, array, header_size);
1577   int type_size = type2aelembytes(type);
1578   u4 length_in_bytes = (u4)length * type_size;
1579   u4 size = header_size + length_in_bytes;
1580 
1581   writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1582   writer->write_objectID(array);
1583   writer->write_u4(STACK_TRACE_ID);
1584   writer->write_u4(length);

1887     if (!java_lang_Class::is_primitive(o)) {
1888       return;
1889     }
1890   }
1891 
1892   if (DumperSupport::mask_dormant_archived_object(o) == NULL) {
1893     log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
1894     return;
1895   }
1896 
1897   // If large object list exists and it is large object/array,
1898   // add oop into the list and skip scan. VM thread will process it later.
1899   if (_list != NULL && is_large(o)) {
1900     _list->atomic_push(o);
1901     return;
1902   }
1903 
1904   if (o->is_instance()) {
1905     // create a HPROF_GC_INSTANCE record for each object
1906     DumperSupport::dump_instance(writer(), o);
1907   } else if (o->is_objArray() || o->is_flatArray()) {
1908     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1909     DumperSupport::dump_object_array(writer(), arrayOop(o));
1910   } else if (o->is_typeArray()) {
1911     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1912     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1913   }
1914 }
1915 
1916 bool HeapObjectDumper::is_large(oop o) {
1917   size_t size = 0;
1918   if (o->is_instance()) {
1919     // Use o->size() * 8 as the upper limit of instance size to avoid iterating static fields
1920     size = o->size() * 8;
1921   } else if (o->is_objArray()) {
1922     objArrayOop array = objArrayOop(o);
1923     BasicType type = ArrayKlass::cast(array->klass())->element_type();
1924     assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1925     int length = array->length();
1926     int type_size = sizeof(address);
1927     size = (size_t)length * type_size;
1928   } else if (o->is_typeArray()) {
1929     typeArrayOop array = typeArrayOop(o);
< prev index next >