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