Home | History | Annotate | Download | only in hprof
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  * Preparation and completion of hprof data generation.  The output is
     19  * written into two files and then combined.  This is necessary because
     20  * we generate some of the data (strings and classes) while we dump the
     21  * heap, and some analysis tools require that the class and string data
     22  * appear first.
     23  */
     24 
     25 #include "hprof.h"
     26 
     27 #include <cutils/open_memstream.h>
     28 #include <errno.h>
     29 #include <fcntl.h>
     30 #include <stdio.h>
     31 #include <string.h>
     32 #include <sys/time.h>
     33 #include <sys/uio.h>
     34 #include <time.h>
     35 #include <time.h>
     36 #include <unistd.h>
     37 #include <set>
     38 
     39 #include "android-base/stringprintf.h"
     40 
     41 #include "art_field-inl.h"
     42 #include "art_method-inl.h"
     43 #include "base/logging.h"
     44 #include "base/time_utils.h"
     45 #include "base/unix_file/fd_file.h"
     46 #include "class_linker.h"
     47 #include "common_throws.h"
     48 #include "debugger.h"
     49 #include "dex_file-inl.h"
     50 #include "gc_root.h"
     51 #include "gc/accounting/heap_bitmap.h"
     52 #include "gc/allocation_record.h"
     53 #include "gc/scoped_gc_critical_section.h"
     54 #include "gc/heap.h"
     55 #include "gc/heap-visit-objects-inl.h"
     56 #include "gc/space/space.h"
     57 #include "globals.h"
     58 #include "jdwp/jdwp.h"
     59 #include "jdwp/jdwp_priv.h"
     60 #include "mirror/class.h"
     61 #include "mirror/class-inl.h"
     62 #include "mirror/object-refvisitor-inl.h"
     63 #include "os.h"
     64 #include "safe_map.h"
     65 #include "scoped_thread_state_change-inl.h"
     66 #include "thread_list.h"
     67 
     68 namespace art {
     69 
     70 namespace hprof {
     71 
     72 static constexpr bool kDirectStream = true;
     73 
     74 static constexpr uint32_t kHprofTime = 0;
     75 static constexpr uint32_t kHprofNullThread = 0;
     76 
     77 static constexpr size_t kMaxObjectsPerSegment = 128;
     78 static constexpr size_t kMaxBytesPerSegment = 4096;
     79 
     80 // The static field-name for the synthetic object generated to account for class static overhead.
     81 static constexpr const char* kClassOverheadName = "$classOverhead";
     82 
     83 enum HprofTag {
     84   HPROF_TAG_STRING = 0x01,
     85   HPROF_TAG_LOAD_CLASS = 0x02,
     86   HPROF_TAG_UNLOAD_CLASS = 0x03,
     87   HPROF_TAG_STACK_FRAME = 0x04,
     88   HPROF_TAG_STACK_TRACE = 0x05,
     89   HPROF_TAG_ALLOC_SITES = 0x06,
     90   HPROF_TAG_HEAP_SUMMARY = 0x07,
     91   HPROF_TAG_START_THREAD = 0x0A,
     92   HPROF_TAG_END_THREAD = 0x0B,
     93   HPROF_TAG_HEAP_DUMP = 0x0C,
     94   HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
     95   HPROF_TAG_HEAP_DUMP_END = 0x2C,
     96   HPROF_TAG_CPU_SAMPLES = 0x0D,
     97   HPROF_TAG_CONTROL_SETTINGS = 0x0E,
     98 };
     99 
    100 // Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
    101 enum HprofHeapTag {
    102   // Traditional.
    103   HPROF_ROOT_UNKNOWN = 0xFF,
    104   HPROF_ROOT_JNI_GLOBAL = 0x01,
    105   HPROF_ROOT_JNI_LOCAL = 0x02,
    106   HPROF_ROOT_JAVA_FRAME = 0x03,
    107   HPROF_ROOT_NATIVE_STACK = 0x04,
    108   HPROF_ROOT_STICKY_CLASS = 0x05,
    109   HPROF_ROOT_THREAD_BLOCK = 0x06,
    110   HPROF_ROOT_MONITOR_USED = 0x07,
    111   HPROF_ROOT_THREAD_OBJECT = 0x08,
    112   HPROF_CLASS_DUMP = 0x20,
    113   HPROF_INSTANCE_DUMP = 0x21,
    114   HPROF_OBJECT_ARRAY_DUMP = 0x22,
    115   HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
    116 
    117   // Android.
    118   HPROF_HEAP_DUMP_INFO = 0xfe,
    119   HPROF_ROOT_INTERNED_STRING = 0x89,
    120   HPROF_ROOT_FINALIZING = 0x8a,  // Obsolete.
    121   HPROF_ROOT_DEBUGGER = 0x8b,
    122   HPROF_ROOT_REFERENCE_CLEANUP = 0x8c,  // Obsolete.
    123   HPROF_ROOT_VM_INTERNAL = 0x8d,
    124   HPROF_ROOT_JNI_MONITOR = 0x8e,
    125   HPROF_UNREACHABLE = 0x90,  // Obsolete.
    126   HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3,  // Obsolete.
    127 };
    128 
    129 enum HprofHeapId {
    130   HPROF_HEAP_DEFAULT = 0,
    131   HPROF_HEAP_ZYGOTE = 'Z',
    132   HPROF_HEAP_APP = 'A',
    133   HPROF_HEAP_IMAGE = 'I',
    134 };
    135 
    136 enum HprofBasicType {
    137   hprof_basic_object = 2,
    138   hprof_basic_boolean = 4,
    139   hprof_basic_char = 5,
    140   hprof_basic_float = 6,
    141   hprof_basic_double = 7,
    142   hprof_basic_byte = 8,
    143   hprof_basic_short = 9,
    144   hprof_basic_int = 10,
    145   hprof_basic_long = 11,
    146 };
    147 
    148 typedef uint32_t HprofStringId;
    149 typedef uint32_t HprofClassObjectId;
    150 typedef uint32_t HprofClassSerialNumber;
    151 typedef uint32_t HprofStackTraceSerialNumber;
    152 typedef uint32_t HprofStackFrameId;
    153 static constexpr HprofStackTraceSerialNumber kHprofNullStackTrace = 0;
    154 
    155 class EndianOutput {
    156  public:
    157   EndianOutput() : length_(0), sum_length_(0), max_length_(0), started_(false) {}
    158   virtual ~EndianOutput() {}
    159 
    160   void StartNewRecord(uint8_t tag, uint32_t time) {
    161     if (length_ > 0) {
    162       EndRecord();
    163     }
    164     DCHECK_EQ(length_, 0U);
    165     AddU1(tag);
    166     AddU4(time);
    167     AddU4(0xdeaddead);  // Length, replaced on flush.
    168     started_ = true;
    169   }
    170 
    171   void EndRecord() {
    172     // Replace length in header.
    173     if (started_) {
    174       UpdateU4(sizeof(uint8_t) + sizeof(uint32_t),
    175                length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
    176     }
    177 
    178     HandleEndRecord();
    179 
    180     sum_length_ += length_;
    181     max_length_ = std::max(max_length_, length_);
    182     length_ = 0;
    183     started_ = false;
    184   }
    185 
    186   void AddU1(uint8_t value) {
    187     AddU1List(&value, 1);
    188   }
    189   void AddU2(uint16_t value) {
    190     AddU2List(&value, 1);
    191   }
    192   void AddU4(uint32_t value) {
    193     AddU4List(&value, 1);
    194   }
    195 
    196   void AddU8(uint64_t value) {
    197     AddU8List(&value, 1);
    198   }
    199 
    200   void AddObjectId(const mirror::Object* value) {
    201     AddU4(PointerToLowMemUInt32(value));
    202   }
    203 
    204   void AddStackTraceSerialNumber(HprofStackTraceSerialNumber value) {
    205     AddU4(value);
    206   }
    207 
    208   // The ID for the synthetic object generated to account for class static overhead.
    209   void AddClassStaticsId(const mirror::Class* value) {
    210     AddU4(1 | PointerToLowMemUInt32(value));
    211   }
    212 
    213   void AddJniGlobalRefId(jobject value) {
    214     AddU4(PointerToLowMemUInt32(value));
    215   }
    216 
    217   void AddClassId(HprofClassObjectId value) {
    218     AddU4(value);
    219   }
    220 
    221   void AddStringId(HprofStringId value) {
    222     AddU4(value);
    223   }
    224 
    225   void AddU1List(const uint8_t* values, size_t count) {
    226     HandleU1List(values, count);
    227     length_ += count;
    228   }
    229   void AddU2List(const uint16_t* values, size_t count) {
    230     HandleU2List(values, count);
    231     length_ += count * sizeof(uint16_t);
    232   }
    233   void AddU4List(const uint32_t* values, size_t count) {
    234     HandleU4List(values, count);
    235     length_ += count * sizeof(uint32_t);
    236   }
    237   virtual void UpdateU4(size_t offset, uint32_t new_value ATTRIBUTE_UNUSED) {
    238     DCHECK_LE(offset, length_ - 4);
    239   }
    240   void AddU8List(const uint64_t* values, size_t count) {
    241     HandleU8List(values, count);
    242     length_ += count * sizeof(uint64_t);
    243   }
    244 
    245   void AddIdList(mirror::ObjectArray<mirror::Object>* values)
    246       REQUIRES_SHARED(Locks::mutator_lock_) {
    247     const int32_t length = values->GetLength();
    248     for (int32_t i = 0; i < length; ++i) {
    249       AddObjectId(values->GetWithoutChecks(i));
    250     }
    251   }
    252 
    253   void AddUtf8String(const char* str) {
    254     // The terminating NUL character is NOT written.
    255     AddU1List((const uint8_t*)str, strlen(str));
    256   }
    257 
    258   size_t Length() const {
    259     return length_;
    260   }
    261 
    262   size_t SumLength() const {
    263     return sum_length_;
    264   }
    265 
    266   size_t MaxLength() const {
    267     return max_length_;
    268   }
    269 
    270  protected:
    271   virtual void HandleU1List(const uint8_t* values ATTRIBUTE_UNUSED,
    272                             size_t count ATTRIBUTE_UNUSED) {
    273   }
    274   virtual void HandleU1AsU2List(const uint8_t* values ATTRIBUTE_UNUSED,
    275                                 size_t count ATTRIBUTE_UNUSED) {
    276   }
    277   virtual void HandleU2List(const uint16_t* values ATTRIBUTE_UNUSED,
    278                             size_t count ATTRIBUTE_UNUSED) {
    279   }
    280   virtual void HandleU4List(const uint32_t* values ATTRIBUTE_UNUSED,
    281                             size_t count ATTRIBUTE_UNUSED) {
    282   }
    283   virtual void HandleU8List(const uint64_t* values ATTRIBUTE_UNUSED,
    284                             size_t count ATTRIBUTE_UNUSED) {
    285   }
    286   virtual void HandleEndRecord() {
    287   }
    288 
    289   size_t length_;      // Current record size.
    290   size_t sum_length_;  // Size of all data.
    291   size_t max_length_;  // Maximum seen length.
    292   bool started_;       // Was StartRecord called?
    293 };
    294 
    295 // This keeps things buffered until flushed.
    296 class EndianOutputBuffered : public EndianOutput {
    297  public:
    298   explicit EndianOutputBuffered(size_t reserve_size) {
    299     buffer_.reserve(reserve_size);
    300   }
    301   virtual ~EndianOutputBuffered() {}
    302 
    303   void UpdateU4(size_t offset, uint32_t new_value) OVERRIDE {
    304     DCHECK_LE(offset, length_ - 4);
    305     buffer_[offset + 0] = static_cast<uint8_t>((new_value >> 24) & 0xFF);
    306     buffer_[offset + 1] = static_cast<uint8_t>((new_value >> 16) & 0xFF);
    307     buffer_[offset + 2] = static_cast<uint8_t>((new_value >> 8)  & 0xFF);
    308     buffer_[offset + 3] = static_cast<uint8_t>((new_value >> 0)  & 0xFF);
    309   }
    310 
    311  protected:
    312   void HandleU1List(const uint8_t* values, size_t count) OVERRIDE {
    313     DCHECK_EQ(length_, buffer_.size());
    314     buffer_.insert(buffer_.end(), values, values + count);
    315   }
    316 
    317   void HandleU1AsU2List(const uint8_t* values, size_t count) OVERRIDE {
    318     DCHECK_EQ(length_, buffer_.size());
    319     // All 8-bits are grouped in 2 to make 16-bit block like Java Char
    320     if (count & 1) {
    321       buffer_.push_back(0);
    322     }
    323     for (size_t i = 0; i < count; ++i) {
    324       uint8_t value = *values;
    325       buffer_.push_back(value);
    326       values++;
    327     }
    328   }
    329 
    330   void HandleU2List(const uint16_t* values, size_t count) OVERRIDE {
    331     DCHECK_EQ(length_, buffer_.size());
    332     for (size_t i = 0; i < count; ++i) {
    333       uint16_t value = *values;
    334       buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
    335       buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
    336       values++;
    337     }
    338   }
    339 
    340   void HandleU4List(const uint32_t* values, size_t count) OVERRIDE {
    341     DCHECK_EQ(length_, buffer_.size());
    342     for (size_t i = 0; i < count; ++i) {
    343       uint32_t value = *values;
    344       buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
    345       buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
    346       buffer_.push_back(static_cast<uint8_t>((value >> 8)  & 0xFF));
    347       buffer_.push_back(static_cast<uint8_t>((value >> 0)  & 0xFF));
    348       values++;
    349     }
    350   }
    351 
    352   void HandleU8List(const uint64_t* values, size_t count) OVERRIDE {
    353     DCHECK_EQ(length_, buffer_.size());
    354     for (size_t i = 0; i < count; ++i) {
    355       uint64_t value = *values;
    356       buffer_.push_back(static_cast<uint8_t>((value >> 56) & 0xFF));
    357       buffer_.push_back(static_cast<uint8_t>((value >> 48) & 0xFF));
    358       buffer_.push_back(static_cast<uint8_t>((value >> 40) & 0xFF));
    359       buffer_.push_back(static_cast<uint8_t>((value >> 32) & 0xFF));
    360       buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
    361       buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
    362       buffer_.push_back(static_cast<uint8_t>((value >> 8)  & 0xFF));
    363       buffer_.push_back(static_cast<uint8_t>((value >> 0)  & 0xFF));
    364       values++;
    365     }
    366   }
    367 
    368   void HandleEndRecord() OVERRIDE {
    369     DCHECK_EQ(buffer_.size(), length_);
    370     if (kIsDebugBuild && started_) {
    371       uint32_t stored_length =
    372           static_cast<uint32_t>(buffer_[5]) << 24 |
    373           static_cast<uint32_t>(buffer_[6]) << 16 |
    374           static_cast<uint32_t>(buffer_[7]) << 8 |
    375           static_cast<uint32_t>(buffer_[8]);
    376       DCHECK_EQ(stored_length, length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
    377     }
    378     HandleFlush(buffer_.data(), length_);
    379     buffer_.clear();
    380   }
    381 
    382   virtual void HandleFlush(const uint8_t* buffer ATTRIBUTE_UNUSED, size_t length ATTRIBUTE_UNUSED) {
    383   }
    384 
    385   std::vector<uint8_t> buffer_;
    386 };
    387 
    388 class FileEndianOutput FINAL : public EndianOutputBuffered {
    389  public:
    390   FileEndianOutput(File* fp, size_t reserved_size)
    391       : EndianOutputBuffered(reserved_size), fp_(fp), errors_(false) {
    392     DCHECK(fp != nullptr);
    393   }
    394   ~FileEndianOutput() {
    395   }
    396 
    397   bool Errors() {
    398     return errors_;
    399   }
    400 
    401  protected:
    402   void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE {
    403     if (!errors_) {
    404       errors_ = !fp_->WriteFully(buffer, length);
    405     }
    406   }
    407 
    408  private:
    409   File* fp_;
    410   bool errors_;
    411 };
    412 
    413 class NetStateEndianOutput FINAL : public EndianOutputBuffered {
    414  public:
    415   NetStateEndianOutput(JDWP::JdwpNetStateBase* net_state, size_t reserved_size)
    416       : EndianOutputBuffered(reserved_size), net_state_(net_state) {
    417     DCHECK(net_state != nullptr);
    418   }
    419   ~NetStateEndianOutput() {}
    420 
    421  protected:
    422   void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE {
    423     std::vector<iovec> iov;
    424     iov.push_back(iovec());
    425     iov[0].iov_base = const_cast<void*>(reinterpret_cast<const void*>(buffer));
    426     iov[0].iov_len = length;
    427     net_state_->WriteBufferedPacketLocked(iov);
    428   }
    429 
    430  private:
    431   JDWP::JdwpNetStateBase* net_state_;
    432 };
    433 
    434 #define __ output_->
    435 
    436 class Hprof : public SingleRootVisitor {
    437  public:
    438   Hprof(const char* output_filename, int fd, bool direct_to_ddms)
    439       : filename_(output_filename),
    440         fd_(fd),
    441         direct_to_ddms_(direct_to_ddms) {
    442     LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
    443   }
    444 
    445   void Dump()
    446     REQUIRES(Locks::mutator_lock_)
    447     REQUIRES(!Locks::heap_bitmap_lock_, !Locks::alloc_tracker_lock_) {
    448     {
    449       MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
    450       if (Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) {
    451         PopulateAllocationTrackingTraces();
    452       }
    453     }
    454 
    455     // First pass to measure the size of the dump.
    456     size_t overall_size;
    457     size_t max_length;
    458     {
    459       EndianOutput count_output;
    460       output_ = &count_output;
    461       ProcessHeap(false);
    462       overall_size = count_output.SumLength();
    463       max_length = count_output.MaxLength();
    464       output_ = nullptr;
    465     }
    466 
    467     bool okay;
    468     visited_objects_.clear();
    469     if (direct_to_ddms_) {
    470       if (kDirectStream) {
    471         okay = DumpToDdmsDirect(overall_size, max_length, CHUNK_TYPE("HPDS"));
    472       } else {
    473         okay = DumpToDdmsBuffered(overall_size, max_length);
    474       }
    475     } else {
    476       okay = DumpToFile(overall_size, max_length);
    477     }
    478 
    479     if (okay) {
    480       const uint64_t duration = NanoTime() - start_ns_;
    481       LOG(INFO) << "hprof: heap dump completed (" << PrettySize(RoundUp(overall_size, KB))
    482                 << ") in " << PrettyDuration(duration)
    483                 << " objects " << total_objects_
    484                 << " objects with stack traces " << total_objects_with_stack_trace_;
    485     }
    486   }
    487 
    488  private:
    489   void DumpHeapObject(mirror::Object* obj)
    490       REQUIRES_SHARED(Locks::mutator_lock_);
    491 
    492   void DumpHeapClass(mirror::Class* klass)
    493       REQUIRES_SHARED(Locks::mutator_lock_);
    494 
    495   void DumpHeapArray(mirror::Array* obj, mirror::Class* klass)
    496       REQUIRES_SHARED(Locks::mutator_lock_);
    497 
    498   void DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements)
    499       REQUIRES_SHARED(Locks::mutator_lock_);
    500 
    501   void DumpHeapInstanceObject(mirror::Object* obj,
    502                               mirror::Class* klass,
    503                               const std::set<mirror::Object*>& fake_roots)
    504       REQUIRES_SHARED(Locks::mutator_lock_);
    505 
    506   bool AddRuntimeInternalObjectsField(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
    507 
    508   void ProcessHeap(bool header_first)
    509       REQUIRES(Locks::mutator_lock_) {
    510     // Reset current heap and object count.
    511     current_heap_ = HPROF_HEAP_DEFAULT;
    512     objects_in_segment_ = 0;
    513 
    514     if (header_first) {
    515       ProcessHeader(true);
    516       ProcessBody();
    517     } else {
    518       ProcessBody();
    519       ProcessHeader(false);
    520     }
    521   }
    522 
    523   void ProcessBody() REQUIRES(Locks::mutator_lock_) {
    524     Runtime* const runtime = Runtime::Current();
    525     // Walk the roots and the heap.
    526     output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
    527 
    528     simple_roots_.clear();
    529     runtime->VisitRoots(this);
    530     runtime->VisitImageRoots(this);
    531     auto dump_object = [this](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
    532       DCHECK(obj != nullptr);
    533       DumpHeapObject(obj);
    534     };
    535     runtime->GetHeap()->VisitObjectsPaused(dump_object);
    536     output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_END, kHprofTime);
    537     output_->EndRecord();
    538   }
    539 
    540   void ProcessHeader(bool string_first) REQUIRES(Locks::mutator_lock_) {
    541     // Write the header.
    542     WriteFixedHeader();
    543     // Write the string and class tables, and any stack traces, to the header.
    544     // (jhat requires that these appear before any of the data in the body that refers to them.)
    545     // jhat also requires the string table appear before class table and stack traces.
    546     // However, WriteStackTraces() can modify the string table, so it's necessary to call
    547     // WriteStringTable() last in the first pass, to compute the correct length of the output.
    548     if (string_first) {
    549       WriteStringTable();
    550     }
    551     WriteClassTable();
    552     WriteStackTraces();
    553     if (!string_first) {
    554       WriteStringTable();
    555     }
    556     output_->EndRecord();
    557   }
    558 
    559   void WriteClassTable() REQUIRES_SHARED(Locks::mutator_lock_) {
    560     for (const auto& p : classes_) {
    561       mirror::Class* c = p.first;
    562       HprofClassSerialNumber sn = p.second;
    563       CHECK(c != nullptr);
    564       output_->StartNewRecord(HPROF_TAG_LOAD_CLASS, kHprofTime);
    565       // LOAD CLASS format:
    566       // U4: class serial number (always > 0)
    567       // ID: class object ID. We use the address of the class object structure as its ID.
    568       // U4: stack trace serial number
    569       // ID: class name string ID
    570       __ AddU4(sn);
    571       __ AddObjectId(c);
    572       __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(c));
    573       __ AddStringId(LookupClassNameId(c));
    574     }
    575   }
    576 
    577   void WriteStringTable() {
    578     for (const auto& p : strings_) {
    579       const std::string& string = p.first;
    580       const HprofStringId id = p.second;
    581 
    582       output_->StartNewRecord(HPROF_TAG_STRING, kHprofTime);
    583 
    584       // STRING format:
    585       // ID:  ID for this string
    586       // U1*: UTF8 characters for string (NOT null terminated)
    587       //      (the record format encodes the length)
    588       __ AddU4(id);
    589       __ AddUtf8String(string.c_str());
    590     }
    591   }
    592 
    593   void StartNewHeapDumpSegment() {
    594     // This flushes the old segment and starts a new one.
    595     output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
    596     objects_in_segment_ = 0;
    597     // Starting a new HEAP_DUMP resets the heap to default.
    598     current_heap_ = HPROF_HEAP_DEFAULT;
    599   }
    600 
    601   void CheckHeapSegmentConstraints() {
    602     if (objects_in_segment_ >= kMaxObjectsPerSegment || output_->Length() >= kMaxBytesPerSegment) {
    603       StartNewHeapDumpSegment();
    604     }
    605   }
    606 
    607   void VisitRoot(mirror::Object* obj, const RootInfo& root_info)
    608       OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
    609   void MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
    610                       uint32_t thread_serial);
    611 
    612   HprofClassObjectId LookupClassId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
    613     if (c != nullptr) {
    614       auto it = classes_.find(c);
    615       if (it == classes_.end()) {
    616         // first time to see this class
    617         HprofClassSerialNumber sn = next_class_serial_number_++;
    618         classes_.Put(c, sn);
    619         // Make sure that we've assigned a string ID for this class' name
    620         LookupClassNameId(c);
    621       }
    622     }
    623     return PointerToLowMemUInt32(c);
    624   }
    625 
    626   HprofStackTraceSerialNumber LookupStackTraceSerialNumber(const mirror::Object* obj)
    627       REQUIRES_SHARED(Locks::mutator_lock_) {
    628     auto r = allocation_records_.find(obj);
    629     if (r == allocation_records_.end()) {
    630       return kHprofNullStackTrace;
    631     } else {
    632       const gc::AllocRecordStackTrace* trace = r->second;
    633       auto result = traces_.find(trace);
    634       CHECK(result != traces_.end());
    635       return result->second;
    636     }
    637   }
    638 
    639   HprofStringId LookupStringId(mirror::String* string) REQUIRES_SHARED(Locks::mutator_lock_) {
    640     return LookupStringId(string->ToModifiedUtf8());
    641   }
    642 
    643   HprofStringId LookupStringId(const char* string) {
    644     return LookupStringId(std::string(string));
    645   }
    646 
    647   HprofStringId LookupStringId(const std::string& string) {
    648     auto it = strings_.find(string);
    649     if (it != strings_.end()) {
    650       return it->second;
    651     }
    652     HprofStringId id = next_string_id_++;
    653     strings_.Put(string, id);
    654     return id;
    655   }
    656 
    657   HprofStringId LookupClassNameId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
    658     return LookupStringId(c->PrettyDescriptor());
    659   }
    660 
    661   void WriteFixedHeader() {
    662     // Write the file header.
    663     // U1: NUL-terminated magic string.
    664     const char magic[] = "JAVA PROFILE 1.0.3";
    665     __ AddU1List(reinterpret_cast<const uint8_t*>(magic), sizeof(magic));
    666 
    667     // U4: size of identifiers.  We're using addresses as IDs and our heap references are stored
    668     // as uint32_t.
    669     // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
    670     static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t),
    671                   "Unexpected HeapReference size");
    672     __ AddU4(sizeof(uint32_t));
    673 
    674     // The current time, in milliseconds since 0:00 GMT, 1/1/70.
    675     timeval now;
    676     const uint64_t nowMs = (gettimeofday(&now, nullptr) < 0) ? 0 :
    677         (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
    678     // TODO: It seems it would be correct to use U8.
    679     // U4: high word of the 64-bit time.
    680     __ AddU4(static_cast<uint32_t>(nowMs >> 32));
    681     // U4: low word of the 64-bit time.
    682     __ AddU4(static_cast<uint32_t>(nowMs & 0xFFFFFFFF));
    683   }
    684 
    685   void WriteStackTraces() REQUIRES_SHARED(Locks::mutator_lock_) {
    686     // Write a dummy stack trace record so the analysis tools don't freak out.
    687     output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
    688     __ AddStackTraceSerialNumber(kHprofNullStackTrace);
    689     __ AddU4(kHprofNullThread);
    690     __ AddU4(0);    // no frames
    691 
    692     // TODO: jhat complains "WARNING: Stack trace not found for serial # -1", but no trace should
    693     // have -1 as its serial number (as long as HprofStackTraceSerialNumber doesn't overflow).
    694     for (const auto& it : traces_) {
    695       const gc::AllocRecordStackTrace* trace = it.first;
    696       HprofStackTraceSerialNumber trace_sn = it.second;
    697       size_t depth = trace->GetDepth();
    698 
    699       // First write stack frames of the trace
    700       for (size_t i = 0; i < depth; ++i) {
    701         const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
    702         ArtMethod* method = frame->GetMethod();
    703         CHECK(method != nullptr);
    704         output_->StartNewRecord(HPROF_TAG_STACK_FRAME, kHprofTime);
    705         // STACK FRAME format:
    706         // ID: stack frame ID. We use the address of the AllocRecordStackTraceElement object as its ID.
    707         // ID: method name string ID
    708         // ID: method signature string ID
    709         // ID: source file name string ID
    710         // U4: class serial number
    711         // U4: >0, line number; 0, no line information available; -1, unknown location
    712         auto frame_result = frames_.find(frame);
    713         CHECK(frame_result != frames_.end());
    714         __ AddU4(frame_result->second);
    715         __ AddStringId(LookupStringId(method->GetName()));
    716         __ AddStringId(LookupStringId(method->GetSignature().ToString()));
    717         const char* source_file = method->GetDeclaringClassSourceFile();
    718         if (source_file == nullptr) {
    719           source_file = "";
    720         }
    721         __ AddStringId(LookupStringId(source_file));
    722         auto class_result = classes_.find(method->GetDeclaringClass());
    723         CHECK(class_result != classes_.end());
    724         __ AddU4(class_result->second);
    725         __ AddU4(frame->ComputeLineNumber());
    726       }
    727 
    728       // Then write the trace itself
    729       output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
    730       // STACK TRACE format:
    731       // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number.
    732       // U4: thread serial number. We use Thread::GetTid().
    733       // U4: number of frames
    734       // [ID]*: series of stack frame ID's
    735       __ AddStackTraceSerialNumber(trace_sn);
    736       __ AddU4(trace->GetTid());
    737       __ AddU4(depth);
    738       for (size_t i = 0; i < depth; ++i) {
    739         const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
    740         auto frame_result = frames_.find(frame);
    741         CHECK(frame_result != frames_.end());
    742         __ AddU4(frame_result->second);
    743       }
    744     }
    745   }
    746 
    747   bool DumpToDdmsBuffered(size_t overall_size ATTRIBUTE_UNUSED, size_t max_length ATTRIBUTE_UNUSED)
    748       REQUIRES(Locks::mutator_lock_) {
    749     LOG(FATAL) << "Unimplemented";
    750     UNREACHABLE();
    751     //        // Send the data off to DDMS.
    752     //        iovec iov[2];
    753     //        iov[0].iov_base = header_data_ptr_;
    754     //        iov[0].iov_len = header_data_size_;
    755     //        iov[1].iov_base = body_data_ptr_;
    756     //        iov[1].iov_len = body_data_size_;
    757     //        Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
    758   }
    759 
    760   bool DumpToFile(size_t overall_size, size_t max_length)
    761       REQUIRES(Locks::mutator_lock_) {
    762     // Where exactly are we writing to?
    763     int out_fd;
    764     if (fd_ >= 0) {
    765       out_fd = dup(fd_);
    766       if (out_fd < 0) {
    767         ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
    768         return false;
    769       }
    770     } else {
    771       out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
    772       if (out_fd < 0) {
    773         ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
    774                               strerror(errno));
    775         return false;
    776       }
    777     }
    778 
    779     std::unique_ptr<File> file(new File(out_fd, filename_, true));
    780     bool okay;
    781     {
    782       FileEndianOutput file_output(file.get(), max_length);
    783       output_ = &file_output;
    784       ProcessHeap(true);
    785       okay = !file_output.Errors();
    786 
    787       if (okay) {
    788         // Check for expected size. Output is expected to be less-or-equal than first phase, see
    789         // b/23521263.
    790         DCHECK_LE(file_output.SumLength(), overall_size);
    791       }
    792       output_ = nullptr;
    793     }
    794 
    795     if (okay) {
    796       okay = file->FlushCloseOrErase() == 0;
    797     } else {
    798       file->Erase();
    799     }
    800     if (!okay) {
    801       std::string msg(android::base::StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
    802                                                   filename_.c_str(),
    803                                                   strerror(errno)));
    804       ThrowRuntimeException("%s", msg.c_str());
    805       LOG(ERROR) << msg;
    806     }
    807 
    808     return okay;
    809   }
    810 
    811   bool DumpToDdmsDirect(size_t overall_size, size_t max_length, uint32_t chunk_type)
    812       REQUIRES(Locks::mutator_lock_) {
    813     CHECK(direct_to_ddms_);
    814     JDWP::JdwpState* state = Dbg::GetJdwpState();
    815     CHECK(state != nullptr);
    816     JDWP::JdwpNetStateBase* net_state = state->netState;
    817     CHECK(net_state != nullptr);
    818 
    819     // Hold the socket lock for the whole time since we want this to be atomic.
    820     MutexLock mu(Thread::Current(), *net_state->GetSocketLock());
    821 
    822     // Prepare the Ddms chunk.
    823     constexpr size_t kChunkHeaderSize = kJDWPHeaderLen + 8;
    824     uint8_t chunk_header[kChunkHeaderSize] = { 0 };
    825     state->SetupChunkHeader(chunk_type, overall_size, kChunkHeaderSize, chunk_header);
    826 
    827     // Prepare the output and send the chunk header.
    828     NetStateEndianOutput net_output(net_state, max_length);
    829     output_ = &net_output;
    830     net_output.AddU1List(chunk_header, kChunkHeaderSize);
    831 
    832     // Write the dump.
    833     ProcessHeap(true);
    834 
    835     // Check for expected size. See DumpToFile for comment.
    836     DCHECK_LE(net_output.SumLength(), overall_size + kChunkHeaderSize);
    837     output_ = nullptr;
    838 
    839     return true;
    840   }
    841 
    842   void PopulateAllocationTrackingTraces()
    843       REQUIRES(Locks::mutator_lock_, Locks::alloc_tracker_lock_) {
    844     gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
    845     CHECK(records != nullptr);
    846     HprofStackTraceSerialNumber next_trace_sn = kHprofNullStackTrace + 1;
    847     HprofStackFrameId next_frame_id = 0;
    848     size_t count = 0;
    849 
    850     for (auto it = records->Begin(), end = records->End(); it != end; ++it) {
    851       const mirror::Object* obj = it->first.Read();
    852       if (obj == nullptr) {
    853         continue;
    854       }
    855       ++count;
    856       const gc::AllocRecordStackTrace* trace = it->second.GetStackTrace();
    857 
    858       // Copy the pair into a real hash map to speed up look up.
    859       auto records_result = allocation_records_.emplace(obj, trace);
    860       // The insertion should always succeed, i.e. no duplicate object pointers in "records"
    861       CHECK(records_result.second);
    862 
    863       // Generate serial numbers for traces, and IDs for frames.
    864       auto traces_result = traces_.find(trace);
    865       if (traces_result == traces_.end()) {
    866         traces_.emplace(trace, next_trace_sn++);
    867         // only check frames if the trace is newly discovered
    868         for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) {
    869           const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
    870           auto frames_result = frames_.find(frame);
    871           if (frames_result == frames_.end()) {
    872             frames_.emplace(frame, next_frame_id++);
    873           }
    874         }
    875       }
    876     }
    877     CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1);
    878     CHECK_EQ(frames_.size(), next_frame_id);
    879     total_objects_with_stack_trace_ = count;
    880   }
    881 
    882   // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
    883   // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
    884   // only be used for debug messages.
    885   std::string filename_;
    886   int fd_;
    887   bool direct_to_ddms_;
    888 
    889   uint64_t start_ns_ = NanoTime();
    890 
    891   EndianOutput* output_ = nullptr;
    892 
    893   HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT;  // Which heap we're currently dumping.
    894   size_t objects_in_segment_ = 0;
    895 
    896   size_t total_objects_ = 0u;
    897   size_t total_objects_with_stack_trace_ = 0u;
    898 
    899   HprofStringId next_string_id_ = 0x400000;
    900   SafeMap<std::string, HprofStringId> strings_;
    901   HprofClassSerialNumber next_class_serial_number_ = 1;
    902   SafeMap<mirror::Class*, HprofClassSerialNumber> classes_;
    903 
    904   std::unordered_map<const gc::AllocRecordStackTrace*, HprofStackTraceSerialNumber,
    905                      gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTrace>,
    906                      gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTrace>> traces_;
    907   std::unordered_map<const gc::AllocRecordStackTraceElement*, HprofStackFrameId,
    908                      gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>,
    909                      gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>> frames_;
    910   std::unordered_map<const mirror::Object*, const gc::AllocRecordStackTrace*> allocation_records_;
    911 
    912   // Set used to keep track of what simple root records we have already
    913   // emitted, to avoid emitting duplicate entries. The simple root records are
    914   // those that contain no other information than the root type and the object
    915   // id. A pair of root type and object id is packed into a uint64_t, with
    916   // the root type in the upper 32 bits and the object id in the lower 32
    917   // bits.
    918   std::unordered_set<uint64_t> simple_roots_;
    919 
    920   // To make sure we don't dump the same object multiple times. b/34967844
    921   std::unordered_set<mirror::Object*> visited_objects_;
    922 
    923   friend class GcRootVisitor;
    924   DISALLOW_COPY_AND_ASSIGN(Hprof);
    925 };
    926 
    927 static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* size_out) {
    928   char c = sig[0];
    929   HprofBasicType ret;
    930   size_t size;
    931 
    932   switch (c) {
    933     case '[':
    934     case 'L':
    935       ret = hprof_basic_object;
    936       size = 4;
    937       break;
    938     case 'Z':
    939       ret = hprof_basic_boolean;
    940       size = 1;
    941       break;
    942     case 'C':
    943       ret = hprof_basic_char;
    944       size = 2;
    945       break;
    946     case 'F':
    947       ret = hprof_basic_float;
    948       size = 4;
    949       break;
    950     case 'D':
    951       ret = hprof_basic_double;
    952       size = 8;
    953       break;
    954     case 'B':
    955       ret = hprof_basic_byte;
    956       size = 1;
    957       break;
    958     case 'S':
    959       ret = hprof_basic_short;
    960       size = 2;
    961       break;
    962     case 'I':
    963       ret = hprof_basic_int;
    964       size = 4;
    965       break;
    966     case 'J':
    967       ret = hprof_basic_long;
    968       size = 8;
    969       break;
    970     default:
    971       LOG(FATAL) << "UNREACHABLE";
    972       UNREACHABLE();
    973   }
    974 
    975   if (size_out != nullptr) {
    976     *size_out = size;
    977   }
    978 
    979   return ret;
    980 }
    981 
    982 // Always called when marking objects, but only does
    983 // something when ctx->gc_scan_state_ is non-zero, which is usually
    984 // only true when marking the root set or unreachable
    985 // objects.  Used to add rootset references to obj.
    986 void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
    987                            uint32_t thread_serial) {
    988   if (heap_tag == 0) {
    989     return;
    990   }
    991 
    992   CheckHeapSegmentConstraints();
    993 
    994   switch (heap_tag) {
    995     // ID: object ID
    996     case HPROF_ROOT_UNKNOWN:
    997     case HPROF_ROOT_STICKY_CLASS:
    998     case HPROF_ROOT_MONITOR_USED:
    999     case HPROF_ROOT_INTERNED_STRING:
   1000     case HPROF_ROOT_DEBUGGER:
   1001     case HPROF_ROOT_VM_INTERNAL: {
   1002       uint64_t key = (static_cast<uint64_t>(heap_tag) << 32) | PointerToLowMemUInt32(obj);
   1003       if (simple_roots_.insert(key).second) {
   1004         __ AddU1(heap_tag);
   1005         __ AddObjectId(obj);
   1006       }
   1007       break;
   1008     }
   1009 
   1010       // ID: object ID
   1011       // ID: JNI global ref ID
   1012     case HPROF_ROOT_JNI_GLOBAL:
   1013       __ AddU1(heap_tag);
   1014       __ AddObjectId(obj);
   1015       __ AddJniGlobalRefId(jni_obj);
   1016       break;
   1017 
   1018       // ID: object ID
   1019       // U4: thread serial number
   1020       // U4: frame number in stack trace (-1 for empty)
   1021     case HPROF_ROOT_JNI_LOCAL:
   1022     case HPROF_ROOT_JNI_MONITOR:
   1023     case HPROF_ROOT_JAVA_FRAME:
   1024       __ AddU1(heap_tag);
   1025       __ AddObjectId(obj);
   1026       __ AddU4(thread_serial);
   1027       __ AddU4((uint32_t)-1);
   1028       break;
   1029 
   1030       // ID: object ID
   1031       // U4: thread serial number
   1032     case HPROF_ROOT_NATIVE_STACK:
   1033     case HPROF_ROOT_THREAD_BLOCK:
   1034       __ AddU1(heap_tag);
   1035       __ AddObjectId(obj);
   1036       __ AddU4(thread_serial);
   1037       break;
   1038 
   1039       // ID: thread object ID
   1040       // U4: thread serial number
   1041       // U4: stack trace serial number
   1042     case HPROF_ROOT_THREAD_OBJECT:
   1043       __ AddU1(heap_tag);
   1044       __ AddObjectId(obj);
   1045       __ AddU4(thread_serial);
   1046       __ AddU4((uint32_t)-1);    // xxx
   1047       break;
   1048 
   1049     case HPROF_CLASS_DUMP:
   1050     case HPROF_INSTANCE_DUMP:
   1051     case HPROF_OBJECT_ARRAY_DUMP:
   1052     case HPROF_PRIMITIVE_ARRAY_DUMP:
   1053     case HPROF_HEAP_DUMP_INFO:
   1054     case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
   1055       // Ignored.
   1056       break;
   1057 
   1058     case HPROF_ROOT_FINALIZING:
   1059     case HPROF_ROOT_REFERENCE_CLEANUP:
   1060     case HPROF_UNREACHABLE:
   1061       LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag);
   1062       break;
   1063   }
   1064 
   1065   ++objects_in_segment_;
   1066 }
   1067 
   1068 bool Hprof::AddRuntimeInternalObjectsField(mirror::Class* klass) {
   1069   if (klass->IsDexCacheClass()) {
   1070     return true;
   1071   }
   1072   // IsClassLoaderClass is true for subclasses of classloader but we only want to add the fake
   1073   // field to the java.lang.ClassLoader class.
   1074   if (klass->IsClassLoaderClass() && klass->GetSuperClass()->IsObjectClass()) {
   1075     return true;
   1076   }
   1077   return false;
   1078 }
   1079 
   1080 void Hprof::DumpHeapObject(mirror::Object* obj) {
   1081   // Ignore classes that are retired.
   1082   if (obj->IsClass() && obj->AsClass()->IsRetired()) {
   1083     return;
   1084   }
   1085   DCHECK(visited_objects_.insert(obj).second) << "Already visited " << obj;
   1086 
   1087   ++total_objects_;
   1088 
   1089   class RootCollector {
   1090    public:
   1091     explicit RootCollector() {}
   1092 
   1093     void operator()(mirror::Object*, MemberOffset, bool) const {}
   1094 
   1095     // Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be
   1096     // running during the hprof dumping process.
   1097     void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
   1098         REQUIRES_SHARED(Locks::mutator_lock_) {
   1099       if (!root->IsNull()) {
   1100         VisitRoot(root);
   1101       }
   1102     }
   1103 
   1104     void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
   1105         REQUIRES_SHARED(Locks::mutator_lock_) {
   1106       roots_.insert(root->AsMirrorPtr());
   1107     }
   1108 
   1109     const std::set<mirror::Object*>& GetRoots() const {
   1110       return roots_;
   1111     }
   1112 
   1113    private:
   1114     // These roots are actually live from the object. Avoid marking them as roots in hprof to make
   1115     // it easier to debug class unloading.
   1116     mutable std::set<mirror::Object*> roots_;
   1117   };
   1118 
   1119   RootCollector visitor;
   1120   // Collect all native roots.
   1121   if (!obj->IsClass()) {
   1122     obj->VisitReferences(visitor, VoidFunctor());
   1123   }
   1124 
   1125   gc::Heap* const heap = Runtime::Current()->GetHeap();
   1126   const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true);
   1127   HprofHeapId heap_type = HPROF_HEAP_APP;
   1128   if (space != nullptr) {
   1129     if (space->IsZygoteSpace()) {
   1130       heap_type = HPROF_HEAP_ZYGOTE;
   1131       VisitRoot(obj, RootInfo(kRootVMInternal));
   1132     } else if (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) {
   1133       // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects as
   1134       // HPROF_HEAP_APP. b/35762934
   1135       heap_type = HPROF_HEAP_IMAGE;
   1136       VisitRoot(obj, RootInfo(kRootVMInternal));
   1137     }
   1138   } else {
   1139     const auto* los = heap->GetLargeObjectsSpace();
   1140     if (los->Contains(obj) && los->IsZygoteLargeObject(Thread::Current(), obj)) {
   1141       heap_type = HPROF_HEAP_ZYGOTE;
   1142       VisitRoot(obj, RootInfo(kRootVMInternal));
   1143     }
   1144   }
   1145   CheckHeapSegmentConstraints();
   1146 
   1147   if (heap_type != current_heap_) {
   1148     HprofStringId nameId;
   1149 
   1150     // This object is in a different heap than the current one.
   1151     // Emit a HEAP_DUMP_INFO tag to change heaps.
   1152     __ AddU1(HPROF_HEAP_DUMP_INFO);
   1153     __ AddU4(static_cast<uint32_t>(heap_type));   // uint32_t: heap type
   1154     switch (heap_type) {
   1155     case HPROF_HEAP_APP:
   1156       nameId = LookupStringId("app");
   1157       break;
   1158     case HPROF_HEAP_ZYGOTE:
   1159       nameId = LookupStringId("zygote");
   1160       break;
   1161     case HPROF_HEAP_IMAGE:
   1162       nameId = LookupStringId("image");
   1163       break;
   1164     default:
   1165       // Internal error
   1166       LOG(ERROR) << "Unexpected desiredHeap";
   1167       nameId = LookupStringId("<ILLEGAL>");
   1168       break;
   1169     }
   1170     __ AddStringId(nameId);
   1171     current_heap_ = heap_type;
   1172   }
   1173 
   1174   mirror::Class* c = obj->GetClass();
   1175   if (c == nullptr) {
   1176     // This object will bother HprofReader, because it has a null
   1177     // class, so just don't dump it. It could be
   1178     // gDvm.unlinkedJavaLangClass or it could be an object just
   1179     // allocated which hasn't been initialized yet.
   1180   } else {
   1181     if (obj->IsClass()) {
   1182       DumpHeapClass(obj->AsClass());
   1183     } else if (c->IsArrayClass()) {
   1184       DumpHeapArray(obj->AsArray(), c);
   1185     } else {
   1186       DumpHeapInstanceObject(obj, c, visitor.GetRoots());
   1187     }
   1188   }
   1189 
   1190   ++objects_in_segment_;
   1191 }
   1192 
   1193 void Hprof::DumpHeapClass(mirror::Class* klass) {
   1194   if (!klass->IsResolved()) {
   1195     // Class is allocated but not yet resolved: we cannot access its fields or super class.
   1196     return;
   1197   }
   1198 
   1199   // Note: We will emit instance fields of Class as synthetic static fields with a prefix of
   1200   //       "$class$" so the class fields are visible in hprof dumps. For tools to account for that
   1201   //       correctly, we'll emit an instance size of zero for java.lang.Class, and also emit the
   1202   //       instance fields of java.lang.Object.
   1203   //
   1204   //       For other overhead (currently only the embedded vtable), we will generate a synthetic
   1205   //       byte array (or field[s] in case the overhead size is of reference size or less).
   1206 
   1207   const size_t num_static_fields = klass->NumStaticFields();
   1208 
   1209   // Total class size:
   1210   //   * class instance fields (including Object instance fields)
   1211   //   * vtable
   1212   //   * class static fields
   1213   const size_t total_class_size = klass->GetClassSize();
   1214 
   1215   // Base class size (common parts of all Class instances):
   1216   //   * class instance fields (including Object instance fields)
   1217   constexpr size_t base_class_size = sizeof(mirror::Class);
   1218   CHECK_LE(base_class_size, total_class_size);
   1219 
   1220   // Difference of Total and Base:
   1221   //   * vtable
   1222   //   * class static fields
   1223   const size_t base_overhead_size = total_class_size - base_class_size;
   1224 
   1225   // Tools (ahat/Studio) will count the static fields and account for them in the class size. We
   1226   // must thus subtract them from base_overhead_size or they will be double-counted.
   1227   size_t class_static_fields_size = 0;
   1228   for (ArtField& class_static_field : klass->GetSFields()) {
   1229     size_t size = 0;
   1230     SignatureToBasicTypeAndSize(class_static_field.GetTypeDescriptor(), &size);
   1231     class_static_fields_size += size;
   1232   }
   1233 
   1234   CHECK_GE(base_overhead_size, class_static_fields_size);
   1235   // Now we have:
   1236   //   * vtable
   1237   const size_t base_no_statics_overhead_size = base_overhead_size - class_static_fields_size;
   1238 
   1239   // We may decide to display native overhead (the actual IMT, ArtFields and ArtMethods) in the
   1240   // future.
   1241   const size_t java_heap_overhead_size = base_no_statics_overhead_size;
   1242 
   1243   // For overhead greater 4, we'll allocate a synthetic array.
   1244   if (java_heap_overhead_size > 4) {
   1245     // Create a byte array to reflect the allocation of the
   1246     // StaticField array at the end of this class.
   1247     __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
   1248     __ AddClassStaticsId(klass);
   1249     __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
   1250     __ AddU4(java_heap_overhead_size - 4);
   1251     __ AddU1(hprof_basic_byte);
   1252     for (size_t i = 0; i < java_heap_overhead_size - 4; ++i) {
   1253       __ AddU1(0);
   1254     }
   1255   }
   1256   const size_t java_heap_overhead_field_count = java_heap_overhead_size > 0
   1257                                                     ? (java_heap_overhead_size == 3 ? 2u : 1u)
   1258                                                     : 0;
   1259 
   1260   __ AddU1(HPROF_CLASS_DUMP);
   1261   __ AddClassId(LookupClassId(klass));
   1262   __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
   1263   __ AddClassId(LookupClassId(klass->GetSuperClass()));
   1264   __ AddObjectId(klass->GetClassLoader());
   1265   __ AddObjectId(nullptr);    // no signer
   1266   __ AddObjectId(nullptr);    // no prot domain
   1267   __ AddObjectId(nullptr);    // reserved
   1268   __ AddObjectId(nullptr);    // reserved
   1269   // Instance size.
   1270   if (klass->IsClassClass()) {
   1271     // As mentioned above, we will emit instance fields as synthetic static fields. So the
   1272     // base object is "empty."
   1273     __ AddU4(0);
   1274   } else if (klass->IsStringClass()) {
   1275     // Strings are variable length with character data at the end like arrays.
   1276     // This outputs the size of an empty string.
   1277     __ AddU4(sizeof(mirror::String));
   1278   } else if (klass->IsArrayClass() || klass->IsPrimitive()) {
   1279     __ AddU4(0);
   1280   } else {
   1281     __ AddU4(klass->GetObjectSize());  // instance size
   1282   }
   1283 
   1284   __ AddU2(0);  // empty const pool
   1285 
   1286   // Static fields
   1287   //
   1288   // Note: we report Class' and Object's instance fields here, too. This is for visibility reasons.
   1289   //       (b/38167721)
   1290   mirror::Class* class_class = klass->GetClass();
   1291 
   1292   DCHECK(class_class->GetSuperClass()->IsObjectClass());
   1293   const size_t static_fields_reported = class_class->NumInstanceFields()
   1294                                         + class_class->GetSuperClass()->NumInstanceFields()
   1295                                         + java_heap_overhead_field_count
   1296                                         + num_static_fields;
   1297   __ AddU2(dchecked_integral_cast<uint16_t>(static_fields_reported));
   1298 
   1299   if (java_heap_overhead_size != 0) {
   1300     __ AddStringId(LookupStringId(kClassOverheadName));
   1301     size_t overhead_fields = 0;
   1302     if (java_heap_overhead_size > 4) {
   1303       __ AddU1(hprof_basic_object);
   1304       __ AddClassStaticsId(klass);
   1305       ++overhead_fields;
   1306     } else {
   1307       switch (java_heap_overhead_size) {
   1308         case 4: {
   1309           __ AddU1(hprof_basic_int);
   1310           __ AddU4(0);
   1311           ++overhead_fields;
   1312           break;
   1313         }
   1314 
   1315         case 2: {
   1316           __ AddU1(hprof_basic_short);
   1317           __ AddU2(0);
   1318           ++overhead_fields;
   1319           break;
   1320         }
   1321 
   1322         case 3: {
   1323           __ AddU1(hprof_basic_short);
   1324           __ AddU2(0);
   1325           __ AddStringId(LookupStringId(std::string(kClassOverheadName) + "2"));
   1326           ++overhead_fields;
   1327         }
   1328         FALLTHROUGH_INTENDED;
   1329 
   1330         case 1: {
   1331           __ AddU1(hprof_basic_byte);
   1332           __ AddU1(0);
   1333           ++overhead_fields;
   1334           break;
   1335         }
   1336       }
   1337     }
   1338     DCHECK_EQ(java_heap_overhead_field_count, overhead_fields);
   1339   }
   1340 
   1341   // Helper lambda to emit the given static field. The second argument name_fn will be called to
   1342   // generate the name to emit. This can be used to emit something else than the field's actual
   1343   // name.
   1344   auto static_field_writer = [&](ArtField& field, auto name_fn)
   1345       REQUIRES_SHARED(Locks::mutator_lock_) {
   1346     __ AddStringId(LookupStringId(name_fn(field)));
   1347 
   1348     size_t size;
   1349     HprofBasicType t = SignatureToBasicTypeAndSize(field.GetTypeDescriptor(), &size);
   1350     __ AddU1(t);
   1351     switch (t) {
   1352       case hprof_basic_byte:
   1353         __ AddU1(field.GetByte(klass));
   1354         return;
   1355       case hprof_basic_boolean:
   1356         __ AddU1(field.GetBoolean(klass));
   1357         return;
   1358       case hprof_basic_char:
   1359         __ AddU2(field.GetChar(klass));
   1360         return;
   1361       case hprof_basic_short:
   1362         __ AddU2(field.GetShort(klass));
   1363         return;
   1364       case hprof_basic_float:
   1365       case hprof_basic_int:
   1366       case hprof_basic_object:
   1367         __ AddU4(field.Get32(klass));
   1368         return;
   1369       case hprof_basic_double:
   1370       case hprof_basic_long:
   1371         __ AddU8(field.Get64(klass));
   1372         return;
   1373     }
   1374     LOG(FATAL) << "Unexpected size " << size;
   1375     UNREACHABLE();
   1376   };
   1377 
   1378   {
   1379     auto class_instance_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
   1380       return std::string("$class$") + field.GetName();
   1381     };
   1382     for (ArtField& class_instance_field : class_class->GetIFields()) {
   1383       static_field_writer(class_instance_field, class_instance_field_name_fn);
   1384     }
   1385     for (ArtField& object_instance_field : class_class->GetSuperClass()->GetIFields()) {
   1386       static_field_writer(object_instance_field, class_instance_field_name_fn);
   1387     }
   1388   }
   1389 
   1390   {
   1391     auto class_static_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
   1392       return field.GetName();
   1393     };
   1394     for (ArtField& class_static_field : klass->GetSFields()) {
   1395       static_field_writer(class_static_field, class_static_field_name_fn);
   1396     }
   1397   }
   1398 
   1399   // Instance fields for this class (no superclass fields)
   1400   int iFieldCount = klass->NumInstanceFields();
   1401   // add_internal_runtime_objects is only for classes that may retain objects live through means
   1402   // other than fields. It is never the case for strings.
   1403   const bool add_internal_runtime_objects = AddRuntimeInternalObjectsField(klass);
   1404   if (klass->IsStringClass() || add_internal_runtime_objects) {
   1405     __ AddU2((uint16_t)iFieldCount + 1);
   1406   } else {
   1407     __ AddU2((uint16_t)iFieldCount);
   1408   }
   1409   for (int i = 0; i < iFieldCount; ++i) {
   1410     ArtField* f = klass->GetInstanceField(i);
   1411     __ AddStringId(LookupStringId(f->GetName()));
   1412     HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr);
   1413     __ AddU1(t);
   1414   }
   1415   // Add native value character array for strings / byte array for compressed strings.
   1416   if (klass->IsStringClass()) {
   1417     __ AddStringId(LookupStringId("value"));
   1418     __ AddU1(hprof_basic_object);
   1419   } else if (add_internal_runtime_objects) {
   1420     __ AddStringId(LookupStringId("runtimeInternalObjects"));
   1421     __ AddU1(hprof_basic_object);
   1422   }
   1423 }
   1424 
   1425 void Hprof::DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements) {
   1426   __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
   1427   __ AddObjectId(obj);
   1428   __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
   1429   __ AddU4(elements.size());
   1430   __ AddClassId(LookupClassId(
   1431       Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kObjectArrayClass)));
   1432   for (mirror::Object* e : elements) {
   1433     __ AddObjectId(e);
   1434   }
   1435 }
   1436 
   1437 void Hprof::DumpHeapArray(mirror::Array* obj, mirror::Class* klass) {
   1438   uint32_t length = obj->GetLength();
   1439 
   1440   if (obj->IsObjectArray()) {
   1441     // obj is an object array.
   1442     __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
   1443 
   1444     __ AddObjectId(obj);
   1445     __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
   1446     __ AddU4(length);
   1447     __ AddClassId(LookupClassId(klass));
   1448 
   1449     // Dump the elements, which are always objects or null.
   1450     __ AddIdList(obj->AsObjectArray<mirror::Object>());
   1451   } else {
   1452     size_t size;
   1453     HprofBasicType t = SignatureToBasicTypeAndSize(
   1454         Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size);
   1455 
   1456     // obj is a primitive array.
   1457     __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
   1458 
   1459     __ AddObjectId(obj);
   1460     __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
   1461     __ AddU4(length);
   1462     __ AddU1(t);
   1463 
   1464     // Dump the raw, packed element values.
   1465     if (size == 1) {
   1466       __ AddU1List(reinterpret_cast<const uint8_t*>(obj->GetRawData(sizeof(uint8_t), 0)), length);
   1467     } else if (size == 2) {
   1468       __ AddU2List(reinterpret_cast<const uint16_t*>(obj->GetRawData(sizeof(uint16_t), 0)), length);
   1469     } else if (size == 4) {
   1470       __ AddU4List(reinterpret_cast<const uint32_t*>(obj->GetRawData(sizeof(uint32_t), 0)), length);
   1471     } else if (size == 8) {
   1472       __ AddU8List(reinterpret_cast<const uint64_t*>(obj->GetRawData(sizeof(uint64_t), 0)), length);
   1473     }
   1474   }
   1475 }
   1476 
   1477 void Hprof::DumpHeapInstanceObject(mirror::Object* obj,
   1478                                    mirror::Class* klass,
   1479                                    const std::set<mirror::Object*>& fake_roots) {
   1480   // obj is an instance object.
   1481   __ AddU1(HPROF_INSTANCE_DUMP);
   1482   __ AddObjectId(obj);
   1483   __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
   1484   __ AddClassId(LookupClassId(klass));
   1485 
   1486   // Reserve some space for the length of the instance data, which we won't
   1487   // know until we're done writing it.
   1488   size_t size_patch_offset = output_->Length();
   1489   __ AddU4(0x77777777);
   1490 
   1491   // What we will use for the string value if the object is a string.
   1492   mirror::Object* string_value = nullptr;
   1493   mirror::Object* fake_object_array = nullptr;
   1494 
   1495   // Write the instance data;  fields for this class, followed by super class fields, and so on.
   1496   do {
   1497     const size_t instance_fields = klass->NumInstanceFields();
   1498     for (size_t i = 0; i < instance_fields; ++i) {
   1499       ArtField* f = klass->GetInstanceField(i);
   1500       size_t size;
   1501       HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
   1502       switch (t) {
   1503       case hprof_basic_byte:
   1504         __ AddU1(f->GetByte(obj));
   1505         break;
   1506       case hprof_basic_boolean:
   1507         __ AddU1(f->GetBoolean(obj));
   1508         break;
   1509       case hprof_basic_char:
   1510         __ AddU2(f->GetChar(obj));
   1511         break;
   1512       case hprof_basic_short:
   1513         __ AddU2(f->GetShort(obj));
   1514         break;
   1515       case hprof_basic_int:
   1516         if (mirror::kUseStringCompression &&
   1517             klass->IsStringClass() &&
   1518             f->GetOffset().SizeValue() == mirror::String::CountOffset().SizeValue()) {
   1519           // Store the string length instead of the raw count field with compression flag.
   1520           __ AddU4(obj->AsString()->GetLength());
   1521           break;
   1522         }
   1523         FALLTHROUGH_INTENDED;
   1524       case hprof_basic_float:
   1525       case hprof_basic_object:
   1526         __ AddU4(f->Get32(obj));
   1527         break;
   1528       case hprof_basic_double:
   1529       case hprof_basic_long:
   1530         __ AddU8(f->Get64(obj));
   1531         break;
   1532       }
   1533     }
   1534     // Add value field for String if necessary.
   1535     if (klass->IsStringClass()) {
   1536       mirror::String* s = obj->AsString();
   1537       if (s->GetLength() == 0) {
   1538         // If string is empty, use an object-aligned address within the string for the value.
   1539         string_value = reinterpret_cast<mirror::Object*>(
   1540             reinterpret_cast<uintptr_t>(s) + kObjectAlignment);
   1541       } else {
   1542         if (s->IsCompressed()) {
   1543           string_value = reinterpret_cast<mirror::Object*>(s->GetValueCompressed());
   1544         } else {
   1545           string_value = reinterpret_cast<mirror::Object*>(s->GetValue());
   1546         }
   1547       }
   1548       __ AddObjectId(string_value);
   1549     } else if (AddRuntimeInternalObjectsField(klass)) {
   1550       // We need an id that is guaranteed to not be used, use 1/2 of the object alignment.
   1551       fake_object_array = reinterpret_cast<mirror::Object*>(
   1552           reinterpret_cast<uintptr_t>(obj) + kObjectAlignment / 2);
   1553       __ AddObjectId(fake_object_array);
   1554     }
   1555     klass = klass->GetSuperClass();
   1556   } while (klass != nullptr);
   1557 
   1558   // Patch the instance field length.
   1559   __ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4));
   1560 
   1561   // Output native value character array for strings.
   1562   CHECK_EQ(obj->IsString(), string_value != nullptr);
   1563   if (string_value != nullptr) {
   1564     mirror::String* s = obj->AsString();
   1565     __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
   1566     __ AddObjectId(string_value);
   1567     __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
   1568     __ AddU4(s->GetLength());
   1569     if (s->IsCompressed()) {
   1570       __ AddU1(hprof_basic_byte);
   1571       __ AddU1List(s->GetValueCompressed(), s->GetLength());
   1572     } else {
   1573       __ AddU1(hprof_basic_char);
   1574       __ AddU2List(s->GetValue(), s->GetLength());
   1575     }
   1576   } else if (fake_object_array != nullptr) {
   1577     DumpFakeObjectArray(fake_object_array, fake_roots);
   1578   }
   1579 }
   1580 
   1581 void Hprof::VisitRoot(mirror::Object* obj, const RootInfo& info) {
   1582   static const HprofHeapTag xlate[] = {
   1583     HPROF_ROOT_UNKNOWN,
   1584     HPROF_ROOT_JNI_GLOBAL,
   1585     HPROF_ROOT_JNI_LOCAL,
   1586     HPROF_ROOT_JAVA_FRAME,
   1587     HPROF_ROOT_NATIVE_STACK,
   1588     HPROF_ROOT_STICKY_CLASS,
   1589     HPROF_ROOT_THREAD_BLOCK,
   1590     HPROF_ROOT_MONITOR_USED,
   1591     HPROF_ROOT_THREAD_OBJECT,
   1592     HPROF_ROOT_INTERNED_STRING,
   1593     HPROF_ROOT_FINALIZING,
   1594     HPROF_ROOT_DEBUGGER,
   1595     HPROF_ROOT_REFERENCE_CLEANUP,
   1596     HPROF_ROOT_VM_INTERNAL,
   1597     HPROF_ROOT_JNI_MONITOR,
   1598   };
   1599   CHECK_LT(info.GetType(), sizeof(xlate) / sizeof(HprofHeapTag));
   1600   if (obj == nullptr) {
   1601     return;
   1602   }
   1603   MarkRootObject(obj, 0, xlate[info.GetType()], info.GetThreadId());
   1604 }
   1605 
   1606 // If "direct_to_ddms" is true, the other arguments are ignored, and data is
   1607 // sent directly to DDMS.
   1608 // If "fd" is >= 0, the output will be written to that file descriptor.
   1609 // Otherwise, "filename" is used to create an output file.
   1610 void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
   1611   CHECK(filename != nullptr);
   1612   Thread* self = Thread::Current();
   1613   // Need to take a heap dump while GC isn't running. See the comment in Heap::VisitObjects().
   1614   // Also we need the critical section to avoid visiting the same object twice. See b/34967844
   1615   gc::ScopedGCCriticalSection gcs(self,
   1616                                   gc::kGcCauseHprof,
   1617                                   gc::kCollectorTypeHprof);
   1618   ScopedSuspendAll ssa(__FUNCTION__, true /* long suspend */);
   1619   Hprof hprof(filename, fd, direct_to_ddms);
   1620   hprof.Dump();
   1621 }
   1622 
   1623 }  // namespace hprof
   1624 }  // namespace art
   1625