blob: af9fbcd9b005e0ae3e9a12969555e74b5b4ae36c [file] [log] [blame]
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001/*
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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080026
Elliott Hughes622a6982012-06-08 17:58:54 -070027#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>
Elliott Hughes622a6982012-06-08 17:58:54 -070035#include <unistd.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036
Elliott Hughes622a6982012-06-08 17:58:54 -070037#include <set>
38
Andreas Gampe57943812017-12-06 21:39:13 -080039#include <android-base/logging.h>
40#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041
Mathieu Chartierc7853442015-03-27 14:35:38 -070042#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080043#include "art_method-inl.h"
Alex Lightc7d2e782018-02-07 12:53:13 -080044#include "base/array_ref.h"
Andreas Gampe57943812017-12-06 21:39:13 -080045#include "base/macros.h"
46#include "base/mutex.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010047#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080048#include "base/unix_file/fd_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080049#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080050#include "common_throws.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040051#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080052#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070053#include "gc/accounting/heap_bitmap.h"
Man Cao8c2ff642015-05-27 17:25:30 -070054#include "gc/allocation_record.h"
Andreas Gampe1c158a02017-07-13 17:26:19 -070055#include "gc/heap-visit-objects-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070056#include "gc/heap.h"
57#include "gc/scoped_gc_critical_section.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070058#include "gc/space/space.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070059#include "gc_root.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070060#include "globals.h"
Mathieu Chartierad466ad2015-01-08 16:28:08 -080061#include "jdwp/jdwp.h"
62#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070064#include "mirror/class.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080065#include "mirror/object-refvisitor-inl.h"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070066#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070067#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070068#include "scoped_thread_state_change-inl.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070069#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040070
71namespace art {
72
73namespace hprof {
74
Mathieu Chartierad466ad2015-01-08 16:28:08 -080075static constexpr bool kDirectStream = true;
Jesse Wilson0c54ac12011-11-09 15:14:05 -050076
Andreas Gampe3a913092015-01-10 00:26:17 -080077static constexpr uint32_t kHprofTime = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -080078static constexpr uint32_t kHprofNullThread = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -070079
Andreas Gampe3a913092015-01-10 00:26:17 -080080static constexpr size_t kMaxObjectsPerSegment = 128;
81static constexpr size_t kMaxBytesPerSegment = 4096;
Elliott Hughes622a6982012-06-08 17:58:54 -070082
Andreas Gampe3a913092015-01-10 00:26:17 -080083// The static field-name for the synthetic object generated to account for class static overhead.
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -070084static constexpr const char* kClassOverheadName = "$classOverhead";
Elliott Hughes622a6982012-06-08 17:58:54 -070085
86enum 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:
104enum 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.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700129 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700130};
131
132enum HprofHeapId {
133 HPROF_HEAP_DEFAULT = 0,
134 HPROF_HEAP_ZYGOTE = 'Z',
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700135 HPROF_HEAP_APP = 'A',
136 HPROF_HEAP_IMAGE = 'I',
Elliott Hughes622a6982012-06-08 17:58:54 -0700137};
138
139enum 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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151typedef uint32_t HprofStringId;
152typedef uint32_t HprofClassObjectId;
Man Cao8c2ff642015-05-27 17:25:30 -0700153typedef uint32_t HprofClassSerialNumber;
154typedef uint32_t HprofStackTraceSerialNumber;
155typedef uint32_t HprofStackFrameId;
156static constexpr HprofStackTraceSerialNumber kHprofNullStackTrace = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -0700157
Andreas Gampe3a913092015-01-10 00:26:17 -0800158class EndianOutput {
Elliott Hughes622a6982012-06-08 17:58:54 -0700159 public:
Andreas Gampe3a913092015-01-10 00:26:17 -0800160 EndianOutput() : length_(0), sum_length_(0), max_length_(0), started_(false) {}
161 virtual ~EndianOutput() {}
162
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;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700172 }
173
Andreas Gampe3a913092015-01-10 00:26:17 -0800174 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 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700180
Andreas Gampe3a913092015-01-10 00:26:17 -0800181 HandleEndRecord();
182
183 sum_length_ += length_;
184 max_length_ = std::max(max_length_, length_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700185 length_ = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -0800186 started_ = false;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700187 }
188
Andreas Gampe3a913092015-01-10 00:26:17 -0800189 void AddU1(uint8_t value) {
190 AddU1List(&value, 1);
191 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800192 void AddU2(uint16_t value) {
193 AddU2List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700194 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800195 void AddU4(uint32_t value) {
196 AddU4List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700197 }
198
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800199 void AddU8(uint64_t value) {
200 AddU8List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700201 }
202
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800203 void AddObjectId(const mirror::Object* value) {
204 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205 }
206
Man Cao8c2ff642015-05-27 17:25:30 -0700207 void AddStackTraceSerialNumber(HprofStackTraceSerialNumber value) {
208 AddU4(value);
209 }
210
Ian Rogersef7d42f2014-01-06 12:55:46 -0800211 // The ID for the synthetic object generated to account for class static overhead.
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800212 void AddClassStaticsId(const mirror::Class* value) {
213 AddU4(1 | PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214 }
215
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800216 void AddJniGlobalRefId(jobject value) {
217 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218 }
219
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800220 void AddClassId(HprofClassObjectId value) {
221 AddU4(value);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800222 }
223
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800224 void AddStringId(HprofStringId value) {
225 AddU4(value);
Elliott Hughes622a6982012-06-08 17:58:54 -0700226 }
227
Andreas Gampe3a913092015-01-10 00:26:17 -0800228 void AddU1List(const uint8_t* values, size_t count) {
229 HandleU1List(values, count);
230 length_ += count;
231 }
232 void AddU2List(const uint16_t* values, size_t count) {
233 HandleU2List(values, count);
234 length_ += count * sizeof(uint16_t);
235 }
236 void AddU4List(const uint32_t* values, size_t count) {
237 HandleU4List(values, count);
238 length_ += count * sizeof(uint32_t);
239 }
Andreas Gampeca714582015-04-03 19:41:34 -0700240 virtual void UpdateU4(size_t offset, uint32_t new_value ATTRIBUTE_UNUSED) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800241 DCHECK_LE(offset, length_ - 4);
242 }
243 void AddU8List(const uint64_t* values, size_t count) {
244 HandleU8List(values, count);
245 length_ += count * sizeof(uint64_t);
246 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700247
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800248 void AddIdList(mirror::ObjectArray<mirror::Object>* values)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800250 const int32_t length = values->GetLength();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251 for (int32_t i = 0; i < length; ++i) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800252 AddObjectId(values->GetWithoutChecks(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700254 }
255
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800256 void AddUtf8String(const char* str) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700257 // The terminating NUL character is NOT written.
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800258 AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700259 }
260
Andreas Gampe3a913092015-01-10 00:26:17 -0800261 size_t Length() const {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700262 return length_;
263 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700264
Andreas Gampe3a913092015-01-10 00:26:17 -0800265 size_t SumLength() const {
266 return sum_length_;
Elliott Hughes622a6982012-06-08 17:58:54 -0700267 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700268
Andreas Gampe3a913092015-01-10 00:26:17 -0800269 size_t MaxLength() const {
270 return max_length_;
271 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700272
Andreas Gampe3a913092015-01-10 00:26:17 -0800273 protected:
274 virtual void HandleU1List(const uint8_t* values ATTRIBUTE_UNUSED,
275 size_t count ATTRIBUTE_UNUSED) {
276 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700277 virtual void HandleU1AsU2List(const uint8_t* values ATTRIBUTE_UNUSED,
278 size_t count ATTRIBUTE_UNUSED) {
279 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800280 virtual void HandleU2List(const uint16_t* values ATTRIBUTE_UNUSED,
281 size_t count ATTRIBUTE_UNUSED) {
282 }
283 virtual void HandleU4List(const uint32_t* values ATTRIBUTE_UNUSED,
284 size_t count ATTRIBUTE_UNUSED) {
285 }
286 virtual void HandleU8List(const uint64_t* values ATTRIBUTE_UNUSED,
287 size_t count ATTRIBUTE_UNUSED) {
288 }
289 virtual void HandleEndRecord() {
290 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700291
Andreas Gampe3a913092015-01-10 00:26:17 -0800292 size_t length_; // Current record size.
293 size_t sum_length_; // Size of all data.
294 size_t max_length_; // Maximum seen length.
295 bool started_; // Was StartRecord called?
Elliott Hughes622a6982012-06-08 17:58:54 -0700296};
297
Andreas Gampe3a913092015-01-10 00:26:17 -0800298// This keeps things buffered until flushed.
299class EndianOutputBuffered : public EndianOutput {
300 public:
301 explicit EndianOutputBuffered(size_t reserve_size) {
302 buffer_.reserve(reserve_size);
303 }
304 virtual ~EndianOutputBuffered() {}
305
306 void UpdateU4(size_t offset, uint32_t new_value) OVERRIDE {
307 DCHECK_LE(offset, length_ - 4);
308 buffer_[offset + 0] = static_cast<uint8_t>((new_value >> 24) & 0xFF);
309 buffer_[offset + 1] = static_cast<uint8_t>((new_value >> 16) & 0xFF);
310 buffer_[offset + 2] = static_cast<uint8_t>((new_value >> 8) & 0xFF);
311 buffer_[offset + 3] = static_cast<uint8_t>((new_value >> 0) & 0xFF);
312 }
313
314 protected:
315 void HandleU1List(const uint8_t* values, size_t count) OVERRIDE {
316 DCHECK_EQ(length_, buffer_.size());
317 buffer_.insert(buffer_.end(), values, values + count);
318 }
319
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700320 void HandleU1AsU2List(const uint8_t* values, size_t count) OVERRIDE {
321 DCHECK_EQ(length_, buffer_.size());
322 // All 8-bits are grouped in 2 to make 16-bit block like Java Char
323 if (count & 1) {
324 buffer_.push_back(0);
325 }
326 for (size_t i = 0; i < count; ++i) {
327 uint8_t value = *values;
328 buffer_.push_back(value);
329 values++;
330 }
331 }
332
Andreas Gampe3a913092015-01-10 00:26:17 -0800333 void HandleU2List(const uint16_t* values, size_t count) OVERRIDE {
334 DCHECK_EQ(length_, buffer_.size());
335 for (size_t i = 0; i < count; ++i) {
336 uint16_t value = *values;
337 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
338 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
339 values++;
340 }
341 }
342
343 void HandleU4List(const uint32_t* values, size_t count) OVERRIDE {
344 DCHECK_EQ(length_, buffer_.size());
345 for (size_t i = 0; i < count; ++i) {
346 uint32_t value = *values;
347 buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
348 buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
349 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
350 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
351 values++;
352 }
353 }
354
355 void HandleU8List(const uint64_t* values, size_t count) OVERRIDE {
356 DCHECK_EQ(length_, buffer_.size());
357 for (size_t i = 0; i < count; ++i) {
358 uint64_t value = *values;
359 buffer_.push_back(static_cast<uint8_t>((value >> 56) & 0xFF));
360 buffer_.push_back(static_cast<uint8_t>((value >> 48) & 0xFF));
361 buffer_.push_back(static_cast<uint8_t>((value >> 40) & 0xFF));
362 buffer_.push_back(static_cast<uint8_t>((value >> 32) & 0xFF));
363 buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
364 buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
365 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
366 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
367 values++;
368 }
369 }
370
371 void HandleEndRecord() OVERRIDE {
372 DCHECK_EQ(buffer_.size(), length_);
373 if (kIsDebugBuild && started_) {
374 uint32_t stored_length =
375 static_cast<uint32_t>(buffer_[5]) << 24 |
376 static_cast<uint32_t>(buffer_[6]) << 16 |
377 static_cast<uint32_t>(buffer_[7]) << 8 |
378 static_cast<uint32_t>(buffer_[8]);
379 DCHECK_EQ(stored_length, length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
380 }
381 HandleFlush(buffer_.data(), length_);
382 buffer_.clear();
383 }
384
385 virtual void HandleFlush(const uint8_t* buffer ATTRIBUTE_UNUSED, size_t length ATTRIBUTE_UNUSED) {
386 }
387
388 std::vector<uint8_t> buffer_;
389};
390
391class FileEndianOutput FINAL : public EndianOutputBuffered {
392 public:
393 FileEndianOutput(File* fp, size_t reserved_size)
394 : EndianOutputBuffered(reserved_size), fp_(fp), errors_(false) {
395 DCHECK(fp != nullptr);
396 }
397 ~FileEndianOutput() {
398 }
399
400 bool Errors() {
401 return errors_;
402 }
403
404 protected:
405 void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE {
406 if (!errors_) {
407 errors_ = !fp_->WriteFully(buffer, length);
408 }
409 }
410
411 private:
412 File* fp_;
413 bool errors_;
414};
415
Alex Lightc7d2e782018-02-07 12:53:13 -0800416class VectorEndianOuputput FINAL : public EndianOutputBuffered {
Andreas Gampe3a913092015-01-10 00:26:17 -0800417 public:
Alex Lightc7d2e782018-02-07 12:53:13 -0800418 VectorEndianOuputput(std::vector<uint8_t>& data, size_t reserved_size)
419 : EndianOutputBuffered(reserved_size), full_data_(data) {}
420 ~VectorEndianOuputput() {}
Andreas Gampe3a913092015-01-10 00:26:17 -0800421
422 protected:
Alex Lightc7d2e782018-02-07 12:53:13 -0800423 void HandleFlush(const uint8_t* buf, size_t length) OVERRIDE {
424 size_t old_size = full_data_.size();
425 full_data_.resize(old_size + length);
426 memcpy(full_data_.data() + old_size, buf, length);
Andreas Gampe3a913092015-01-10 00:26:17 -0800427 }
428
429 private:
Alex Lightc7d2e782018-02-07 12:53:13 -0800430 std::vector<uint8_t>& full_data_;
Andreas Gampe3a913092015-01-10 00:26:17 -0800431};
432
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700433#define __ output_->
Andreas Gampe3a913092015-01-10 00:26:17 -0800434
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700435class Hprof : public SingleRootVisitor {
Elliott Hughes622a6982012-06-08 17:58:54 -0700436 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700437 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
438 : filename_(output_filename),
439 fd_(fd),
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800440 direct_to_ddms_(direct_to_ddms) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700441 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500442 }
443
Andreas Gampe3a913092015-01-10 00:26:17 -0800444 void Dump()
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800445 REQUIRES(Locks::mutator_lock_)
446 REQUIRES(!Locks::heap_bitmap_lock_, !Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700447 {
448 MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
449 if (Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) {
450 PopulateAllocationTrackingTraces();
451 }
452 }
453
Andreas Gampe3a913092015-01-10 00:26:17 -0800454 // First pass to measure the size of the dump.
455 size_t overall_size;
456 size_t max_length;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800457 {
Andreas Gampe3a913092015-01-10 00:26:17 -0800458 EndianOutput count_output;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700459 output_ = &count_output;
460 ProcessHeap(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800461 overall_size = count_output.SumLength();
462 max_length = count_output.MaxLength();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700463 output_ = nullptr;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800464 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700465
Andreas Gampe3a913092015-01-10 00:26:17 -0800466 bool okay;
Mathieu Chartierecc82302017-02-16 10:20:12 -0800467 visited_objects_.clear();
Andreas Gampe3a913092015-01-10 00:26:17 -0800468 if (direct_to_ddms_) {
469 if (kDirectStream) {
470 okay = DumpToDdmsDirect(overall_size, max_length, CHUNK_TYPE("HPDS"));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700471 } else {
Andreas Gampe3a913092015-01-10 00:26:17 -0800472 okay = DumpToDdmsBuffered(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700473 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800474 } else {
475 okay = DumpToFile(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700476 }
477
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700478 if (okay) {
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800479 const uint64_t duration = NanoTime() - start_ns_;
480 LOG(INFO) << "hprof: heap dump completed (" << PrettySize(RoundUp(overall_size, KB))
481 << ") in " << PrettyDuration(duration)
482 << " objects " << total_objects_
483 << " objects with stack traces " << total_objects_with_stack_trace_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700484 }
485 }
486
487 private:
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700488 void DumpHeapObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700489 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700490
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700491 void DumpHeapClass(mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700492 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700493
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700494 void DumpHeapArray(mirror::Array* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700495 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800496
Mathieu Chartier996f75e2017-03-31 11:18:41 -0700497 void DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700498 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800499
Mathieu Chartier996f75e2017-03-31 11:18:41 -0700500 void DumpHeapInstanceObject(mirror::Object* obj,
501 mirror::Class* klass,
502 const std::set<mirror::Object*>& fake_roots)
503 REQUIRES_SHARED(Locks::mutator_lock_);
504
505 bool AddRuntimeInternalObjectsField(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
506
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700507 void ProcessHeap(bool header_first)
Mathieu Chartier90443472015-07-16 20:32:27 -0700508 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800509 // Reset current heap and object count.
510 current_heap_ = HPROF_HEAP_DEFAULT;
511 objects_in_segment_ = 0;
512
513 if (header_first) {
Man Cao8c2ff642015-05-27 17:25:30 -0700514 ProcessHeader(true);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700515 ProcessBody();
Andreas Gampe3a913092015-01-10 00:26:17 -0800516 } else {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700517 ProcessBody();
Man Cao8c2ff642015-05-27 17:25:30 -0700518 ProcessHeader(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800519 }
520 }
521
Mathieu Chartier90443472015-07-16 20:32:27 -0700522 void ProcessBody() REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700523 Runtime* const runtime = Runtime::Current();
Andreas Gampe3a913092015-01-10 00:26:17 -0800524 // Walk the roots and the heap.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700525 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
Andreas Gampe3a913092015-01-10 00:26:17 -0800526
Richard Uhler4b767552016-04-26 13:28:59 -0700527 simple_roots_.clear();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700528 runtime->VisitRoots(this);
529 runtime->VisitImageRoots(this);
Andreas Gampe1c158a02017-07-13 17:26:19 -0700530 auto dump_object = [this](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
531 DCHECK(obj != nullptr);
532 DumpHeapObject(obj);
533 };
534 runtime->GetHeap()->VisitObjectsPaused(dump_object);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700535 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_END, kHprofTime);
536 output_->EndRecord();
Andreas Gampe3a913092015-01-10 00:26:17 -0800537 }
538
Mathieu Chartier90443472015-07-16 20:32:27 -0700539 void ProcessHeader(bool string_first) REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800540 // Write the header.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700541 WriteFixedHeader();
Andreas Gampe3a913092015-01-10 00:26:17 -0800542 // Write the string and class tables, and any stack traces, to the header.
543 // (jhat requires that these appear before any of the data in the body that refers to them.)
Man Cao8c2ff642015-05-27 17:25:30 -0700544 // jhat also requires the string table appear before class table and stack traces.
545 // However, WriteStackTraces() can modify the string table, so it's necessary to call
546 // WriteStringTable() last in the first pass, to compute the correct length of the output.
547 if (string_first) {
548 WriteStringTable();
549 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700550 WriteClassTable();
551 WriteStackTraces();
Man Cao8c2ff642015-05-27 17:25:30 -0700552 if (!string_first) {
553 WriteStringTable();
554 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700555 output_->EndRecord();
Andreas Gampe3a913092015-01-10 00:26:17 -0800556 }
557
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700558 void WriteClassTable() REQUIRES_SHARED(Locks::mutator_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700559 for (const auto& p : classes_) {
560 mirror::Class* c = p.first;
561 HprofClassSerialNumber sn = p.second;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 CHECK(c != nullptr);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700563 output_->StartNewRecord(HPROF_TAG_LOAD_CLASS, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700564 // LOAD CLASS format:
565 // U4: class serial number (always > 0)
566 // ID: class object ID. We use the address of the class object structure as its ID.
567 // U4: stack trace serial number
568 // ID: class name string ID
Man Cao8c2ff642015-05-27 17:25:30 -0700569 __ AddU4(sn);
Andreas Gampe3a913092015-01-10 00:26:17 -0800570 __ AddObjectId(c);
Man Cao8c2ff642015-05-27 17:25:30 -0700571 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(c));
Andreas Gampe3a913092015-01-10 00:26:17 -0800572 __ AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700573 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700574 }
575
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700576 void WriteStringTable() {
Andreas Gampe1eeb00c2016-11-03 08:19:01 -0700577 for (const auto& p : strings_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 const std::string& string = p.first;
Andreas Gampe1eeb00c2016-11-03 08:19:01 -0700579 const HprofStringId id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700580
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700581 output_->StartNewRecord(HPROF_TAG_STRING, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700582
583 // STRING format:
584 // ID: ID for this string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700585 // U1*: UTF8 characters for string (NOT null terminated)
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700586 // (the record format encodes the length)
Andreas Gampe3a913092015-01-10 00:26:17 -0800587 __ AddU4(id);
588 __ AddUtf8String(string.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700589 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700590 }
591
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700592 void StartNewHeapDumpSegment() {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700593 // This flushes the old segment and starts a new one.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700594 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700595 objects_in_segment_ = 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700596 // Starting a new HEAP_DUMP resets the heap to default.
597 current_heap_ = HPROF_HEAP_DEFAULT;
598 }
599
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700600 void CheckHeapSegmentConstraints() {
601 if (objects_in_segment_ >= kMaxObjectsPerSegment || output_->Length() >= kMaxBytesPerSegment) {
602 StartNewHeapDumpSegment();
Andreas Gampe3a913092015-01-10 00:26:17 -0800603 }
604 }
605
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700606 void VisitRoot(mirror::Object* obj, const RootInfo& root_info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800608 void MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700609 uint32_t thread_serial);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700610
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700611 HprofClassObjectId LookupClassId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800612 if (c != nullptr) {
Man Cao8c2ff642015-05-27 17:25:30 -0700613 auto it = classes_.find(c);
614 if (it == classes_.end()) {
615 // first time to see this class
616 HprofClassSerialNumber sn = next_class_serial_number_++;
617 classes_.Put(c, sn);
618 // Make sure that we've assigned a string ID for this class' name
619 LookupClassNameId(c);
620 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800621 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800622 return PointerToLowMemUInt32(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700623 }
624
Man Cao8c2ff642015-05-27 17:25:30 -0700625 HprofStackTraceSerialNumber LookupStackTraceSerialNumber(const mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700626 REQUIRES_SHARED(Locks::mutator_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700627 auto r = allocation_records_.find(obj);
628 if (r == allocation_records_.end()) {
629 return kHprofNullStackTrace;
630 } else {
631 const gc::AllocRecordStackTrace* trace = r->second;
632 auto result = traces_.find(trace);
633 CHECK(result != traces_.end());
634 return result->second;
635 }
636 }
637
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700638 HprofStringId LookupStringId(mirror::String* string) REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700639 return LookupStringId(string->ToModifiedUtf8());
640 }
641
642 HprofStringId LookupStringId(const char* string) {
643 return LookupStringId(std::string(string));
644 }
645
646 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800647 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700648 if (it != strings_.end()) {
649 return it->second;
650 }
651 HprofStringId id = next_string_id_++;
652 strings_.Put(string, id);
653 return id;
654 }
655
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700656 HprofStringId LookupClassNameId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700657 return LookupStringId(c->PrettyDescriptor());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700658 }
659
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700660 void WriteFixedHeader() {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500661 // Write the file header.
662 // U1: NUL-terminated magic string.
Andreas Gampe3a913092015-01-10 00:26:17 -0800663 const char magic[] = "JAVA PROFILE 1.0.3";
664 __ AddU1List(reinterpret_cast<const uint8_t*>(magic), sizeof(magic));
665
Calin Juravle32805172014-07-04 16:24:03 +0100666 // U4: size of identifiers. We're using addresses as IDs and our heap references are stored
667 // as uint32_t.
668 // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800669 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t),
670 "Unexpected HeapReference size");
Andreas Gampe3a913092015-01-10 00:26:17 -0800671 __ AddU4(sizeof(uint32_t));
672
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500673 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700674 timeval now;
Andreas Gampe3a913092015-01-10 00:26:17 -0800675 const uint64_t nowMs = (gettimeofday(&now, nullptr) < 0) ? 0 :
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800676 (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
Andreas Gampe3a913092015-01-10 00:26:17 -0800677 // TODO: It seems it would be correct to use U8.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500678 // U4: high word of the 64-bit time.
Andreas Gampe3a913092015-01-10 00:26:17 -0800679 __ AddU4(static_cast<uint32_t>(nowMs >> 32));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500680 // U4: low word of the 64-bit time.
Andreas Gampe3a913092015-01-10 00:26:17 -0800681 __ AddU4(static_cast<uint32_t>(nowMs & 0xFFFFFFFF));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500682 }
683
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700684 void WriteStackTraces() REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700685 // Write a dummy stack trace record so the analysis tools don't freak out.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700686 output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
Man Cao8c2ff642015-05-27 17:25:30 -0700687 __ AddStackTraceSerialNumber(kHprofNullStackTrace);
Andreas Gampe3a913092015-01-10 00:26:17 -0800688 __ AddU4(kHprofNullThread);
689 __ AddU4(0); // no frames
Man Cao8c2ff642015-05-27 17:25:30 -0700690
691 // TODO: jhat complains "WARNING: Stack trace not found for serial # -1", but no trace should
692 // have -1 as its serial number (as long as HprofStackTraceSerialNumber doesn't overflow).
693 for (const auto& it : traces_) {
694 const gc::AllocRecordStackTrace* trace = it.first;
695 HprofStackTraceSerialNumber trace_sn = it.second;
696 size_t depth = trace->GetDepth();
697
698 // First write stack frames of the trace
699 for (size_t i = 0; i < depth; ++i) {
700 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
701 ArtMethod* method = frame->GetMethod();
702 CHECK(method != nullptr);
703 output_->StartNewRecord(HPROF_TAG_STACK_FRAME, kHprofTime);
704 // STACK FRAME format:
705 // ID: stack frame ID. We use the address of the AllocRecordStackTraceElement object as its ID.
706 // ID: method name string ID
707 // ID: method signature string ID
708 // ID: source file name string ID
709 // U4: class serial number
710 // U4: >0, line number; 0, no line information available; -1, unknown location
711 auto frame_result = frames_.find(frame);
712 CHECK(frame_result != frames_.end());
713 __ AddU4(frame_result->second);
714 __ AddStringId(LookupStringId(method->GetName()));
715 __ AddStringId(LookupStringId(method->GetSignature().ToString()));
716 const char* source_file = method->GetDeclaringClassSourceFile();
717 if (source_file == nullptr) {
718 source_file = "";
719 }
720 __ AddStringId(LookupStringId(source_file));
721 auto class_result = classes_.find(method->GetDeclaringClass());
722 CHECK(class_result != classes_.end());
723 __ AddU4(class_result->second);
724 __ AddU4(frame->ComputeLineNumber());
725 }
726
727 // Then write the trace itself
728 output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
729 // STACK TRACE format:
730 // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number.
731 // U4: thread serial number. We use Thread::GetTid().
732 // U4: number of frames
733 // [ID]*: series of stack frame ID's
734 __ AddStackTraceSerialNumber(trace_sn);
735 __ AddU4(trace->GetTid());
736 __ AddU4(depth);
737 for (size_t i = 0; i < depth; ++i) {
738 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
739 auto frame_result = frames_.find(frame);
740 CHECK(frame_result != frames_.end());
741 __ AddU4(frame_result->second);
742 }
743 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800744 }
745
746 bool DumpToDdmsBuffered(size_t overall_size ATTRIBUTE_UNUSED, size_t max_length ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700747 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800748 LOG(FATAL) << "Unimplemented";
749 UNREACHABLE();
750 // // Send the data off to DDMS.
751 // iovec iov[2];
752 // iov[0].iov_base = header_data_ptr_;
753 // iov[0].iov_len = header_data_size_;
754 // iov[1].iov_base = body_data_ptr_;
755 // iov[1].iov_len = body_data_size_;
756 // Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
757 }
758
759 bool DumpToFile(size_t overall_size, size_t max_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700760 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800761 // Where exactly are we writing to?
762 int out_fd;
763 if (fd_ >= 0) {
764 out_fd = dup(fd_);
765 if (out_fd < 0) {
766 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
767 return false;
768 }
769 } else {
770 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
771 if (out_fd < 0) {
772 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
773 strerror(errno));
774 return false;
775 }
776 }
777
778 std::unique_ptr<File> file(new File(out_fd, filename_, true));
779 bool okay;
780 {
781 FileEndianOutput file_output(file.get(), max_length);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700782 output_ = &file_output;
783 ProcessHeap(true);
Andreas Gampe3a913092015-01-10 00:26:17 -0800784 okay = !file_output.Errors();
785
786 if (okay) {
Andreas Gampec515f212015-08-28 18:15:27 -0700787 // Check for expected size. Output is expected to be less-or-equal than first phase, see
788 // b/23521263.
789 DCHECK_LE(file_output.SumLength(), overall_size);
Andreas Gampe3a913092015-01-10 00:26:17 -0800790 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700791 output_ = nullptr;
Andreas Gampe3a913092015-01-10 00:26:17 -0800792 }
793
794 if (okay) {
795 okay = file->FlushCloseOrErase() == 0;
796 } else {
797 file->Erase();
798 }
799 if (!okay) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800800 std::string msg(android::base::StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
801 filename_.c_str(),
802 strerror(errno)));
Andreas Gampe3a913092015-01-10 00:26:17 -0800803 ThrowRuntimeException("%s", msg.c_str());
804 LOG(ERROR) << msg;
805 }
806
807 return okay;
808 }
809
810 bool DumpToDdmsDirect(size_t overall_size, size_t max_length, uint32_t chunk_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700811 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800812 CHECK(direct_to_ddms_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800813
Alex Lightc7d2e782018-02-07 12:53:13 -0800814 std::vector<uint8_t> out_data;
Andreas Gampe3a913092015-01-10 00:26:17 -0800815
Alex Lightc7d2e782018-02-07 12:53:13 -0800816 // TODO It would be really good to have some streaming thing again. b/73084059
817 VectorEndianOuputput output(out_data, max_length);
818 output_ = &output;
Andreas Gampe3a913092015-01-10 00:26:17 -0800819
820 // Write the dump.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700821 ProcessHeap(true);
Andreas Gampe3a913092015-01-10 00:26:17 -0800822
Alex Lightc7d2e782018-02-07 12:53:13 -0800823 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(
824 chunk_type, ArrayRef<const uint8_t>(out_data.data(), out_data.size()));
825
Andreas Gampec515f212015-08-28 18:15:27 -0700826 // Check for expected size. See DumpToFile for comment.
Alex Lightc7d2e782018-02-07 12:53:13 -0800827 DCHECK_LE(output.SumLength(), overall_size);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700828 output_ = nullptr;
Andreas Gampe3a913092015-01-10 00:26:17 -0800829
830 return true;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700831 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500832
Man Cao8c2ff642015-05-27 17:25:30 -0700833 void PopulateAllocationTrackingTraces()
Mathieu Chartier90443472015-07-16 20:32:27 -0700834 REQUIRES(Locks::mutator_lock_, Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700835 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
836 CHECK(records != nullptr);
837 HprofStackTraceSerialNumber next_trace_sn = kHprofNullStackTrace + 1;
838 HprofStackFrameId next_frame_id = 0;
Man Cao42c3c332015-06-23 16:38:25 -0700839 size_t count = 0;
Man Cao8c2ff642015-05-27 17:25:30 -0700840
841 for (auto it = records->Begin(), end = records->End(); it != end; ++it) {
842 const mirror::Object* obj = it->first.Read();
Man Cao42c3c332015-06-23 16:38:25 -0700843 if (obj == nullptr) {
844 continue;
845 }
846 ++count;
Mathieu Chartier458b1052016-03-29 14:02:55 -0700847 const gc::AllocRecordStackTrace* trace = it->second.GetStackTrace();
Man Cao8c2ff642015-05-27 17:25:30 -0700848
849 // Copy the pair into a real hash map to speed up look up.
850 auto records_result = allocation_records_.emplace(obj, trace);
851 // The insertion should always succeed, i.e. no duplicate object pointers in "records"
852 CHECK(records_result.second);
853
854 // Generate serial numbers for traces, and IDs for frames.
855 auto traces_result = traces_.find(trace);
856 if (traces_result == traces_.end()) {
857 traces_.emplace(trace, next_trace_sn++);
858 // only check frames if the trace is newly discovered
859 for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) {
860 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
861 auto frames_result = frames_.find(frame);
862 if (frames_result == frames_.end()) {
863 frames_.emplace(frame, next_frame_id++);
864 }
865 }
866 }
867 }
868 CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1);
869 CHECK_EQ(frames_.size(), next_frame_id);
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800870 total_objects_with_stack_trace_ = count;
Man Cao8c2ff642015-05-27 17:25:30 -0700871 }
872
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700873 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
874 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
875 // only be used for debug messages.
876 std::string filename_;
877 int fd_;
878 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500879
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800880 uint64_t start_ns_ = NanoTime();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700881
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800882 EndianOutput* output_ = nullptr;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700883
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800884 HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT; // Which heap we're currently dumping.
885 size_t objects_in_segment_ = 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700886
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800887 size_t total_objects_ = 0u;
888 size_t total_objects_with_stack_trace_ = 0u;
889
890 HprofStringId next_string_id_ = 0x400000;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800891 SafeMap<std::string, HprofStringId> strings_;
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800892 HprofClassSerialNumber next_class_serial_number_ = 1;
Man Cao8c2ff642015-05-27 17:25:30 -0700893 SafeMap<mirror::Class*, HprofClassSerialNumber> classes_;
894
895 std::unordered_map<const gc::AllocRecordStackTrace*, HprofStackTraceSerialNumber,
896 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTrace>,
897 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTrace>> traces_;
898 std::unordered_map<const gc::AllocRecordStackTraceElement*, HprofStackFrameId,
899 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>,
900 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>> frames_;
901 std::unordered_map<const mirror::Object*, const gc::AllocRecordStackTrace*> allocation_records_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700902
Richard Uhler4b767552016-04-26 13:28:59 -0700903 // Set used to keep track of what simple root records we have already
904 // emitted, to avoid emitting duplicate entries. The simple root records are
905 // those that contain no other information than the root type and the object
906 // id. A pair of root type and object id is packed into a uint64_t, with
907 // the root type in the upper 32 bits and the object id in the lower 32
908 // bits.
909 std::unordered_set<uint64_t> simple_roots_;
910
Mathieu Chartierecc82302017-02-16 10:20:12 -0800911 // To make sure we don't dump the same object multiple times. b/34967844
912 std::unordered_set<mirror::Object*> visited_objects_;
913
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700914 friend class GcRootVisitor;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700915 DISALLOW_COPY_AND_ASSIGN(Hprof);
916};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500917
Andreas Gampe3a913092015-01-10 00:26:17 -0800918static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* size_out) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500919 char c = sig[0];
920 HprofBasicType ret;
921 size_t size;
922
923 switch (c) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800924 case '[':
925 case 'L':
926 ret = hprof_basic_object;
927 size = 4;
928 break;
929 case 'Z':
930 ret = hprof_basic_boolean;
931 size = 1;
932 break;
933 case 'C':
934 ret = hprof_basic_char;
935 size = 2;
936 break;
937 case 'F':
938 ret = hprof_basic_float;
939 size = 4;
940 break;
941 case 'D':
942 ret = hprof_basic_double;
943 size = 8;
944 break;
945 case 'B':
946 ret = hprof_basic_byte;
947 size = 1;
948 break;
949 case 'S':
950 ret = hprof_basic_short;
951 size = 2;
952 break;
953 case 'I':
954 ret = hprof_basic_int;
955 size = 4;
956 break;
957 case 'J':
958 ret = hprof_basic_long;
959 size = 8;
960 break;
961 default:
962 LOG(FATAL) << "UNREACHABLE";
963 UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500964 }
965
Andreas Gampe3a913092015-01-10 00:26:17 -0800966 if (size_out != nullptr) {
967 *size_out = size;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500968 }
969
970 return ret;
971}
972
973// Always called when marking objects, but only does
974// something when ctx->gc_scan_state_ is non-zero, which is usually
975// only true when marking the root set or unreachable
976// objects. Used to add rootset references to obj.
Andreas Gampe3a913092015-01-10 00:26:17 -0800977void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700978 uint32_t thread_serial) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800979 if (heap_tag == 0) {
980 return;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500981 }
982
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700983 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500984
Andreas Gampe3a913092015-01-10 00:26:17 -0800985 switch (heap_tag) {
986 // ID: object ID
987 case HPROF_ROOT_UNKNOWN:
988 case HPROF_ROOT_STICKY_CLASS:
989 case HPROF_ROOT_MONITOR_USED:
990 case HPROF_ROOT_INTERNED_STRING:
991 case HPROF_ROOT_DEBUGGER:
Richard Uhler4b767552016-04-26 13:28:59 -0700992 case HPROF_ROOT_VM_INTERNAL: {
993 uint64_t key = (static_cast<uint64_t>(heap_tag) << 32) | PointerToLowMemUInt32(obj);
994 if (simple_roots_.insert(key).second) {
995 __ AddU1(heap_tag);
996 __ AddObjectId(obj);
997 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800998 break;
Richard Uhler4b767552016-04-26 13:28:59 -0700999 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001000
Andreas Gampe3a913092015-01-10 00:26:17 -08001001 // ID: object ID
1002 // ID: JNI global ref ID
1003 case HPROF_ROOT_JNI_GLOBAL:
1004 __ AddU1(heap_tag);
1005 __ AddObjectId(obj);
1006 __ AddJniGlobalRefId(jni_obj);
1007 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001008
Andreas Gampe3a913092015-01-10 00:26:17 -08001009 // ID: object ID
1010 // U4: thread serial number
1011 // U4: frame number in stack trace (-1 for empty)
1012 case HPROF_ROOT_JNI_LOCAL:
1013 case HPROF_ROOT_JNI_MONITOR:
1014 case HPROF_ROOT_JAVA_FRAME:
1015 __ AddU1(heap_tag);
1016 __ AddObjectId(obj);
1017 __ AddU4(thread_serial);
1018 __ AddU4((uint32_t)-1);
1019 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001020
Andreas Gampe3a913092015-01-10 00:26:17 -08001021 // ID: object ID
1022 // U4: thread serial number
1023 case HPROF_ROOT_NATIVE_STACK:
1024 case HPROF_ROOT_THREAD_BLOCK:
1025 __ AddU1(heap_tag);
1026 __ AddObjectId(obj);
1027 __ AddU4(thread_serial);
1028 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001029
Andreas Gampe3a913092015-01-10 00:26:17 -08001030 // ID: thread object ID
1031 // U4: thread serial number
1032 // U4: stack trace serial number
1033 case HPROF_ROOT_THREAD_OBJECT:
1034 __ AddU1(heap_tag);
1035 __ AddObjectId(obj);
1036 __ AddU4(thread_serial);
1037 __ AddU4((uint32_t)-1); // xxx
1038 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001039
Andreas Gampe3a913092015-01-10 00:26:17 -08001040 case HPROF_CLASS_DUMP:
1041 case HPROF_INSTANCE_DUMP:
1042 case HPROF_OBJECT_ARRAY_DUMP:
1043 case HPROF_PRIMITIVE_ARRAY_DUMP:
1044 case HPROF_HEAP_DUMP_INFO:
1045 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
1046 // Ignored.
1047 break;
Elliott Hughes73e66f72012-05-09 09:34:45 -07001048
Andreas Gampe3a913092015-01-10 00:26:17 -08001049 case HPROF_ROOT_FINALIZING:
1050 case HPROF_ROOT_REFERENCE_CLEANUP:
1051 case HPROF_UNREACHABLE:
1052 LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag);
1053 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001054 }
1055
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001056 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001057}
1058
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001059bool Hprof::AddRuntimeInternalObjectsField(mirror::Class* klass) {
1060 if (klass->IsDexCacheClass()) {
1061 return true;
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001062 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001063 // IsClassLoaderClass is true for subclasses of classloader but we only want to add the fake
1064 // field to the java.lang.ClassLoader class.
1065 if (klass->IsClassLoaderClass() && klass->GetSuperClass()->IsObjectClass()) {
1066 return true;
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001067 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001068 return false;
1069}
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001070
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001071void Hprof::DumpHeapObject(mirror::Object* obj) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001072 // Ignore classes that are retired.
1073 if (obj->IsClass() && obj->AsClass()->IsRetired()) {
1074 return;
1075 }
Mathieu Chartierecc82302017-02-16 10:20:12 -08001076 DCHECK(visited_objects_.insert(obj).second) << "Already visited " << obj;
Andreas Gampe3a913092015-01-10 00:26:17 -08001077
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -08001078 ++total_objects_;
1079
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001080 class RootCollector {
1081 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001082 RootCollector() {}
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001083
1084 void operator()(mirror::Object*, MemberOffset, bool) const {}
1085
1086 // Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be
1087 // running during the hprof dumping process.
1088 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1089 REQUIRES_SHARED(Locks::mutator_lock_) {
1090 if (!root->IsNull()) {
1091 VisitRoot(root);
1092 }
1093 }
1094
1095 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1096 REQUIRES_SHARED(Locks::mutator_lock_) {
1097 roots_.insert(root->AsMirrorPtr());
1098 }
1099
1100 const std::set<mirror::Object*>& GetRoots() const {
1101 return roots_;
1102 }
1103
1104 private:
1105 // These roots are actually live from the object. Avoid marking them as roots in hprof to make
1106 // it easier to debug class unloading.
1107 mutable std::set<mirror::Object*> roots_;
1108 };
1109
1110 RootCollector visitor;
1111 // Collect all native roots.
1112 if (!obj->IsClass()) {
1113 obj->VisitReferences(visitor, VoidFunctor());
1114 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001115
Mathieu Chartiere7158112015-06-03 13:32:15 -07001116 gc::Heap* const heap = Runtime::Current()->GetHeap();
1117 const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001118 HprofHeapId heap_type = HPROF_HEAP_APP;
1119 if (space != nullptr) {
1120 if (space->IsZygoteSpace()) {
1121 heap_type = HPROF_HEAP_ZYGOTE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001122 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartier0b874522017-02-24 14:47:08 -08001123 } else if (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) {
1124 // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects as
1125 // HPROF_HEAP_APP. b/35762934
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001126 heap_type = HPROF_HEAP_IMAGE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001127 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001128 }
Mathieu Chartiere7158112015-06-03 13:32:15 -07001129 } else {
1130 const auto* los = heap->GetLargeObjectsSpace();
1131 if (los->Contains(obj) && los->IsZygoteLargeObject(Thread::Current(), obj)) {
1132 heap_type = HPROF_HEAP_ZYGOTE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001133 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartiere7158112015-06-03 13:32:15 -07001134 }
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001135 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001136 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001137
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001138 if (heap_type != current_heap_) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001139 HprofStringId nameId;
1140
1141 // This object is in a different heap than the current one.
1142 // Emit a HEAP_DUMP_INFO tag to change heaps.
Andreas Gampe3a913092015-01-10 00:26:17 -08001143 __ AddU1(HPROF_HEAP_DUMP_INFO);
1144 __ AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001145 switch (heap_type) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001146 case HPROF_HEAP_APP:
1147 nameId = LookupStringId("app");
1148 break;
1149 case HPROF_HEAP_ZYGOTE:
1150 nameId = LookupStringId("zygote");
1151 break;
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001152 case HPROF_HEAP_IMAGE:
1153 nameId = LookupStringId("image");
1154 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001155 default:
1156 // Internal error
1157 LOG(ERROR) << "Unexpected desiredHeap";
1158 nameId = LookupStringId("<ILLEGAL>");
1159 break;
1160 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001161 __ AddStringId(nameId);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001162 current_heap_ = heap_type;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001163 }
1164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001165 mirror::Class* c = obj->GetClass();
Andreas Gampe3a913092015-01-10 00:26:17 -08001166 if (c == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001167 // This object will bother HprofReader, because it has a null
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001168 // class, so just don't dump it. It could be
1169 // gDvm.unlinkedJavaLangClass or it could be an object just
1170 // allocated which hasn't been initialized yet.
1171 } else {
1172 if (obj->IsClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001173 DumpHeapClass(obj->AsClass());
Elliott Hughese84278b2012-03-22 10:06:53 -07001174 } else if (c->IsArrayClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001175 DumpHeapArray(obj->AsArray(), c);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001176 } else {
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001177 DumpHeapInstanceObject(obj, c, visitor.GetRoots());
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001178 }
1179 }
1180
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001181 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001182}
1183
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001184void Hprof::DumpHeapClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +00001185 if (!klass->IsResolved()) {
Mathieu Chartier12393f32016-10-20 17:11:23 -07001186 // Class is allocated but not yet resolved: we cannot access its fields or super class.
Sebastien Hertzca068b22015-04-07 10:28:53 +02001187 return;
1188 }
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001189
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001190 // Note: We will emit instance fields of Class as synthetic static fields with a prefix of
1191 // "$class$" so the class fields are visible in hprof dumps. For tools to account for that
1192 // correctly, we'll emit an instance size of zero for java.lang.Class, and also emit the
1193 // instance fields of java.lang.Object.
1194 //
1195 // For other overhead (currently only the embedded vtable), we will generate a synthetic
1196 // byte array (or field[s] in case the overhead size is of reference size or less).
1197
1198 const size_t num_static_fields = klass->NumStaticFields();
1199
1200 // Total class size:
1201 // * class instance fields (including Object instance fields)
1202 // * vtable
1203 // * class static fields
1204 const size_t total_class_size = klass->GetClassSize();
1205
1206 // Base class size (common parts of all Class instances):
1207 // * class instance fields (including Object instance fields)
1208 constexpr size_t base_class_size = sizeof(mirror::Class);
1209 CHECK_LE(base_class_size, total_class_size);
1210
1211 // Difference of Total and Base:
1212 // * vtable
1213 // * class static fields
1214 const size_t base_overhead_size = total_class_size - base_class_size;
1215
1216 // Tools (ahat/Studio) will count the static fields and account for them in the class size. We
1217 // must thus subtract them from base_overhead_size or they will be double-counted.
1218 size_t class_static_fields_size = 0;
1219 for (ArtField& class_static_field : klass->GetSFields()) {
1220 size_t size = 0;
1221 SignatureToBasicTypeAndSize(class_static_field.GetTypeDescriptor(), &size);
1222 class_static_fields_size += size;
1223 }
1224
1225 CHECK_GE(base_overhead_size, class_static_fields_size);
1226 // Now we have:
1227 // * vtable
1228 const size_t base_no_statics_overhead_size = base_overhead_size - class_static_fields_size;
1229
1230 // We may decide to display native overhead (the actual IMT, ArtFields and ArtMethods) in the
1231 // future.
1232 const size_t java_heap_overhead_size = base_no_statics_overhead_size;
1233
1234 // For overhead greater 4, we'll allocate a synthetic array.
1235 if (java_heap_overhead_size > 4) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001236 // Create a byte array to reflect the allocation of the
1237 // StaticField array at the end of this class.
1238 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1239 __ AddClassStaticsId(klass);
Man Cao8c2ff642015-05-27 17:25:30 -07001240 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001241 __ AddU4(java_heap_overhead_size - 4);
Andreas Gampe3a913092015-01-10 00:26:17 -08001242 __ AddU1(hprof_basic_byte);
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001243 for (size_t i = 0; i < java_heap_overhead_size - 4; ++i) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001244 __ AddU1(0);
1245 }
1246 }
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001247 const size_t java_heap_overhead_field_count = java_heap_overhead_size > 0
1248 ? (java_heap_overhead_size == 3 ? 2u : 1u)
1249 : 0;
Andreas Gampe3a913092015-01-10 00:26:17 -08001250
1251 __ AddU1(HPROF_CLASS_DUMP);
1252 __ AddClassId(LookupClassId(klass));
Man Cao8c2ff642015-05-27 17:25:30 -07001253 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Andreas Gampe3a913092015-01-10 00:26:17 -08001254 __ AddClassId(LookupClassId(klass->GetSuperClass()));
1255 __ AddObjectId(klass->GetClassLoader());
1256 __ AddObjectId(nullptr); // no signer
1257 __ AddObjectId(nullptr); // no prot domain
1258 __ AddObjectId(nullptr); // reserved
1259 __ AddObjectId(nullptr); // reserved
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001260 // Instance size.
Andreas Gampe3a913092015-01-10 00:26:17 -08001261 if (klass->IsClassClass()) {
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001262 // As mentioned above, we will emit instance fields as synthetic static fields. So the
1263 // base object is "empty."
1264 __ AddU4(0);
Jeff Hao0ce43532015-05-12 18:58:32 -07001265 } else if (klass->IsStringClass()) {
1266 // Strings are variable length with character data at the end like arrays.
1267 // This outputs the size of an empty string.
1268 __ AddU4(sizeof(mirror::String));
1269 } else if (klass->IsArrayClass() || klass->IsPrimitive()) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001270 __ AddU4(0);
1271 } else {
1272 __ AddU4(klass->GetObjectSize()); // instance size
1273 }
1274
1275 __ AddU2(0); // empty const pool
1276
1277 // Static fields
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001278 //
1279 // Note: we report Class' and Object's instance fields here, too. This is for visibility reasons.
1280 // (b/38167721)
1281 mirror::Class* class_class = klass->GetClass();
1282
1283 DCHECK(class_class->GetSuperClass()->IsObjectClass());
1284 const size_t static_fields_reported = class_class->NumInstanceFields()
1285 + class_class->GetSuperClass()->NumInstanceFields()
1286 + java_heap_overhead_field_count
1287 + num_static_fields;
1288 __ AddU2(dchecked_integral_cast<uint16_t>(static_fields_reported));
1289
1290 if (java_heap_overhead_size != 0) {
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001291 __ AddStringId(LookupStringId(kClassOverheadName));
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001292 size_t overhead_fields = 0;
1293 if (java_heap_overhead_size > 4) {
1294 __ AddU1(hprof_basic_object);
1295 __ AddClassStaticsId(klass);
1296 ++overhead_fields;
1297 } else {
1298 switch (java_heap_overhead_size) {
1299 case 4: {
1300 __ AddU1(hprof_basic_int);
1301 __ AddU4(0);
1302 ++overhead_fields;
1303 break;
1304 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001305
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001306 case 2: {
1307 __ AddU1(hprof_basic_short);
1308 __ AddU2(0);
1309 ++overhead_fields;
1310 break;
1311 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001312
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001313 case 3: {
1314 __ AddU1(hprof_basic_short);
1315 __ AddU2(0);
1316 __ AddStringId(LookupStringId(std::string(kClassOverheadName) + "2"));
1317 ++overhead_fields;
1318 }
1319 FALLTHROUGH_INTENDED;
1320
1321 case 1: {
1322 __ AddU1(hprof_basic_byte);
1323 __ AddU1(0);
1324 ++overhead_fields;
Andreas Gampe3a913092015-01-10 00:26:17 -08001325 break;
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001326 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001327 }
1328 }
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001329 DCHECK_EQ(java_heap_overhead_field_count, overhead_fields);
1330 }
1331
1332 // Helper lambda to emit the given static field. The second argument name_fn will be called to
1333 // generate the name to emit. This can be used to emit something else than the field's actual
1334 // name.
1335 auto static_field_writer = [&](ArtField& field, auto name_fn)
1336 REQUIRES_SHARED(Locks::mutator_lock_) {
1337 __ AddStringId(LookupStringId(name_fn(field)));
1338
1339 size_t size;
1340 HprofBasicType t = SignatureToBasicTypeAndSize(field.GetTypeDescriptor(), &size);
1341 __ AddU1(t);
1342 switch (t) {
1343 case hprof_basic_byte:
1344 __ AddU1(field.GetByte(klass));
1345 return;
1346 case hprof_basic_boolean:
1347 __ AddU1(field.GetBoolean(klass));
1348 return;
1349 case hprof_basic_char:
1350 __ AddU2(field.GetChar(klass));
1351 return;
1352 case hprof_basic_short:
1353 __ AddU2(field.GetShort(klass));
1354 return;
1355 case hprof_basic_float:
1356 case hprof_basic_int:
1357 case hprof_basic_object:
1358 __ AddU4(field.Get32(klass));
1359 return;
1360 case hprof_basic_double:
1361 case hprof_basic_long:
1362 __ AddU8(field.Get64(klass));
1363 return;
1364 }
1365 LOG(FATAL) << "Unexpected size " << size;
1366 UNREACHABLE();
1367 };
1368
1369 {
1370 auto class_instance_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
1371 return std::string("$class$") + field.GetName();
1372 };
1373 for (ArtField& class_instance_field : class_class->GetIFields()) {
1374 static_field_writer(class_instance_field, class_instance_field_name_fn);
1375 }
1376 for (ArtField& object_instance_field : class_class->GetSuperClass()->GetIFields()) {
1377 static_field_writer(object_instance_field, class_instance_field_name_fn);
1378 }
1379 }
1380
1381 {
1382 auto class_static_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
1383 return field.GetName();
1384 };
1385 for (ArtField& class_static_field : klass->GetSFields()) {
1386 static_field_writer(class_static_field, class_static_field_name_fn);
1387 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001388 }
1389
1390 // Instance fields for this class (no superclass fields)
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001391 int iFieldCount = klass->NumInstanceFields();
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001392 // add_internal_runtime_objects is only for classes that may retain objects live through means
1393 // other than fields. It is never the case for strings.
1394 const bool add_internal_runtime_objects = AddRuntimeInternalObjectsField(klass);
1395 if (klass->IsStringClass() || add_internal_runtime_objects) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001396 __ AddU2((uint16_t)iFieldCount + 1);
1397 } else {
1398 __ AddU2((uint16_t)iFieldCount);
1399 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001400 for (int i = 0; i < iFieldCount; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001401 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001402 __ AddStringId(LookupStringId(f->GetName()));
1403 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr);
1404 __ AddU1(t);
1405 }
Vladimir Marko595beb32017-02-06 14:11:54 +00001406 // Add native value character array for strings / byte array for compressed strings.
Jeff Hao848f70a2014-01-15 13:49:50 -08001407 if (klass->IsStringClass()) {
1408 __ AddStringId(LookupStringId("value"));
1409 __ AddU1(hprof_basic_object);
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001410 } else if (add_internal_runtime_objects) {
1411 __ AddStringId(LookupStringId("runtimeInternalObjects"));
1412 __ AddU1(hprof_basic_object);
1413 }
1414}
1415
1416void Hprof::DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements) {
1417 __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
1418 __ AddObjectId(obj);
1419 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
1420 __ AddU4(elements.size());
1421 __ AddClassId(LookupClassId(
1422 Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kObjectArrayClass)));
1423 for (mirror::Object* e : elements) {
1424 __ AddObjectId(e);
Jeff Hao848f70a2014-01-15 13:49:50 -08001425 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001426}
1427
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001428void Hprof::DumpHeapArray(mirror::Array* obj, mirror::Class* klass) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001429 uint32_t length = obj->GetLength();
1430
1431 if (obj->IsObjectArray()) {
1432 // obj is an object array.
1433 __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
1434
1435 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001436 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001437 __ AddU4(length);
1438 __ AddClassId(LookupClassId(klass));
1439
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001440 // Dump the elements, which are always objects or null.
Andreas Gampe3a913092015-01-10 00:26:17 -08001441 __ AddIdList(obj->AsObjectArray<mirror::Object>());
1442 } else {
1443 size_t size;
1444 HprofBasicType t = SignatureToBasicTypeAndSize(
1445 Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size);
1446
1447 // obj is a primitive array.
1448 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1449
1450 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001451 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001452 __ AddU4(length);
1453 __ AddU1(t);
1454
1455 // Dump the raw, packed element values.
1456 if (size == 1) {
1457 __ AddU1List(reinterpret_cast<const uint8_t*>(obj->GetRawData(sizeof(uint8_t), 0)), length);
1458 } else if (size == 2) {
1459 __ AddU2List(reinterpret_cast<const uint16_t*>(obj->GetRawData(sizeof(uint16_t), 0)), length);
1460 } else if (size == 4) {
1461 __ AddU4List(reinterpret_cast<const uint32_t*>(obj->GetRawData(sizeof(uint32_t), 0)), length);
1462 } else if (size == 8) {
1463 __ AddU8List(reinterpret_cast<const uint64_t*>(obj->GetRawData(sizeof(uint64_t), 0)), length);
1464 }
1465 }
1466}
1467
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001468void Hprof::DumpHeapInstanceObject(mirror::Object* obj,
1469 mirror::Class* klass,
1470 const std::set<mirror::Object*>& fake_roots) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001471 // obj is an instance object.
1472 __ AddU1(HPROF_INSTANCE_DUMP);
1473 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001474 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001475 __ AddClassId(LookupClassId(klass));
1476
1477 // Reserve some space for the length of the instance data, which we won't
1478 // know until we're done writing it.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001479 size_t size_patch_offset = output_->Length();
Andreas Gampe3a913092015-01-10 00:26:17 -08001480 __ AddU4(0x77777777);
1481
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001482 // What we will use for the string value if the object is a string.
1483 mirror::Object* string_value = nullptr;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001484 mirror::Object* fake_object_array = nullptr;
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001485
1486 // Write the instance data; fields for this class, followed by super class fields, and so on.
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001487 do {
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001488 const size_t instance_fields = klass->NumInstanceFields();
1489 for (size_t i = 0; i < instance_fields; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001490 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001491 size_t size;
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001492 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001493 switch (t) {
1494 case hprof_basic_byte:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001495 __ AddU1(f->GetByte(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001496 break;
1497 case hprof_basic_boolean:
1498 __ AddU1(f->GetBoolean(obj));
1499 break;
1500 case hprof_basic_char:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001501 __ AddU2(f->GetChar(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001502 break;
1503 case hprof_basic_short:
1504 __ AddU2(f->GetShort(obj));
1505 break;
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001506 case hprof_basic_int:
Vladimir Marko595beb32017-02-06 14:11:54 +00001507 if (mirror::kUseStringCompression &&
1508 klass->IsStringClass() &&
1509 f->GetOffset().SizeValue() == mirror::String::CountOffset().SizeValue()) {
1510 // Store the string length instead of the raw count field with compression flag.
1511 __ AddU4(obj->AsString()->GetLength());
1512 break;
1513 }
1514 FALLTHROUGH_INTENDED;
1515 case hprof_basic_float:
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001516 case hprof_basic_object:
Andreas Gampe3a913092015-01-10 00:26:17 -08001517 __ AddU4(f->Get32(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001518 break;
1519 case hprof_basic_double:
1520 case hprof_basic_long:
Andreas Gampe3a913092015-01-10 00:26:17 -08001521 __ AddU8(f->Get64(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001522 break;
Andreas Gampe3a913092015-01-10 00:26:17 -08001523 }
1524 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001525 // Add value field for String if necessary.
1526 if (klass->IsStringClass()) {
1527 mirror::String* s = obj->AsString();
1528 if (s->GetLength() == 0) {
1529 // If string is empty, use an object-aligned address within the string for the value.
1530 string_value = reinterpret_cast<mirror::Object*>(
1531 reinterpret_cast<uintptr_t>(s) + kObjectAlignment);
1532 } else {
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001533 if (s->IsCompressed()) {
1534 string_value = reinterpret_cast<mirror::Object*>(s->GetValueCompressed());
1535 } else {
1536 string_value = reinterpret_cast<mirror::Object*>(s->GetValue());
1537 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001538 }
1539 __ AddObjectId(string_value);
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001540 } else if (AddRuntimeInternalObjectsField(klass)) {
1541 // We need an id that is guaranteed to not be used, use 1/2 of the object alignment.
1542 fake_object_array = reinterpret_cast<mirror::Object*>(
1543 reinterpret_cast<uintptr_t>(obj) + kObjectAlignment / 2);
1544 __ AddObjectId(fake_object_array);
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001545 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001546 klass = klass->GetSuperClass();
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001547 } while (klass != nullptr);
Andreas Gampe3a913092015-01-10 00:26:17 -08001548
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001549 // Patch the instance field length.
1550 __ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4));
1551
Jeff Hao848f70a2014-01-15 13:49:50 -08001552 // Output native value character array for strings.
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001553 CHECK_EQ(obj->IsString(), string_value != nullptr);
1554 if (string_value != nullptr) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001555 mirror::String* s = obj->AsString();
Jeff Hao848f70a2014-01-15 13:49:50 -08001556 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001557 __ AddObjectId(string_value);
Man Cao8c2ff642015-05-27 17:25:30 -07001558 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Vladimir Marko595beb32017-02-06 14:11:54 +00001559 __ AddU4(s->GetLength());
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001560 if (s->IsCompressed()) {
Vladimir Marko595beb32017-02-06 14:11:54 +00001561 __ AddU1(hprof_basic_byte);
1562 __ AddU1List(s->GetValueCompressed(), s->GetLength());
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001563 } else {
Vladimir Marko595beb32017-02-06 14:11:54 +00001564 __ AddU1(hprof_basic_char);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001565 __ AddU2List(s->GetValue(), s->GetLength());
1566 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001567 } else if (fake_object_array != nullptr) {
1568 DumpFakeObjectArray(fake_object_array, fake_roots);
Jeff Hao848f70a2014-01-15 13:49:50 -08001569 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001570}
1571
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001572void Hprof::VisitRoot(mirror::Object* obj, const RootInfo& info) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001573 static const HprofHeapTag xlate[] = {
1574 HPROF_ROOT_UNKNOWN,
1575 HPROF_ROOT_JNI_GLOBAL,
1576 HPROF_ROOT_JNI_LOCAL,
1577 HPROF_ROOT_JAVA_FRAME,
1578 HPROF_ROOT_NATIVE_STACK,
1579 HPROF_ROOT_STICKY_CLASS,
1580 HPROF_ROOT_THREAD_BLOCK,
1581 HPROF_ROOT_MONITOR_USED,
1582 HPROF_ROOT_THREAD_OBJECT,
1583 HPROF_ROOT_INTERNED_STRING,
1584 HPROF_ROOT_FINALIZING,
1585 HPROF_ROOT_DEBUGGER,
1586 HPROF_ROOT_REFERENCE_CLEANUP,
1587 HPROF_ROOT_VM_INTERNAL,
1588 HPROF_ROOT_JNI_MONITOR,
1589 };
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -08001590 CHECK_LT(info.GetType(), sizeof(xlate) / sizeof(HprofHeapTag));
Andreas Gampe3a913092015-01-10 00:26:17 -08001591 if (obj == nullptr) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001592 return;
1593 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001594 MarkRootObject(obj, 0, xlate[info.GetType()], info.GetThreadId());
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001595}
1596
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001597// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1598// sent directly to DDMS.
1599// If "fd" is >= 0, the output will be written to that file descriptor.
1600// Otherwise, "filename" is used to create an output file.
1601void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001602 CHECK(filename != nullptr);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001603 Thread* self = Thread::Current();
Mathieu Chartierecc82302017-02-16 10:20:12 -08001604 // Need to take a heap dump while GC isn't running. See the comment in Heap::VisitObjects().
1605 // Also we need the critical section to avoid visiting the same object twice. See b/34967844
1606 gc::ScopedGCCriticalSection gcs(self,
1607 gc::kGcCauseHprof,
1608 gc::kCollectorTypeHprof);
1609 ScopedSuspendAll ssa(__FUNCTION__, true /* long suspend */);
1610 Hprof hprof(filename, fd, direct_to_ddms);
1611 hprof.Dump();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001612}
1613
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001614} // namespace hprof
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001615} // namespace art