blob: 34f645b54c9c37d2c996636a79705b7f57f76ad5 [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 <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>
Elliott Hughes622a6982012-06-08 17:58:54 -070034#include <unistd.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035
Elliott Hughes622a6982012-06-08 17:58:54 -070036#include <set>
37
Andreas Gampe57943812017-12-06 21:39:13 -080038#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040
Mathieu Chartierc7853442015-03-27 14:35:38 -070041#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080042#include "art_method-inl.h"
Alex Lightc7d2e782018-02-07 12:53:13 -080043#include "base/array_ref.h"
Andreas Gampedfcd82c2018-10-16 20:22:37 -070044#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080045#include "base/macros.h"
46#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080047#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080048#include "base/safe_map.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010049#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080050#include "base/unix_file/fd_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080051#include "class_linker.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010052#include "class_root.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080053#include "common_throws.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040054#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080055#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070056#include "gc/accounting/heap_bitmap.h"
Man Cao8c2ff642015-05-27 17:25:30 -070057#include "gc/allocation_record.h"
Andreas Gampe1c158a02017-07-13 17:26:19 -070058#include "gc/heap-visit-objects-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070059#include "gc/heap.h"
60#include "gc/scoped_gc_critical_section.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070061#include "gc/space/space.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070062#include "gc_root.h"
Mathieu Chartierad466ad2015-01-08 16:28:08 -080063#include "jdwp/jdwp.h"
64#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070066#include "mirror/class.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080067#include "mirror/object-refvisitor-inl.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080068#include "runtime_globals.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070069#include "scoped_thread_state_change-inl.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070070#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040071
72namespace art {
73
74namespace hprof {
75
Mathieu Chartierad466ad2015-01-08 16:28:08 -080076static constexpr bool kDirectStream = true;
Jesse Wilson0c54ac12011-11-09 15:14:05 -050077
Andreas Gampe3a913092015-01-10 00:26:17 -080078static constexpr uint32_t kHprofTime = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -080079static constexpr uint32_t kHprofNullThread = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -070080
Andreas Gampe3a913092015-01-10 00:26:17 -080081static constexpr size_t kMaxObjectsPerSegment = 128;
82static constexpr size_t kMaxBytesPerSegment = 4096;
Elliott Hughes622a6982012-06-08 17:58:54 -070083
Andreas Gampe3a913092015-01-10 00:26:17 -080084// The static field-name for the synthetic object generated to account for class static overhead.
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -070085static constexpr const char* kClassOverheadName = "$classOverhead";
Elliott Hughes622a6982012-06-08 17:58:54 -070086
87enum HprofTag {
88 HPROF_TAG_STRING = 0x01,
89 HPROF_TAG_LOAD_CLASS = 0x02,
90 HPROF_TAG_UNLOAD_CLASS = 0x03,
91 HPROF_TAG_STACK_FRAME = 0x04,
92 HPROF_TAG_STACK_TRACE = 0x05,
93 HPROF_TAG_ALLOC_SITES = 0x06,
94 HPROF_TAG_HEAP_SUMMARY = 0x07,
95 HPROF_TAG_START_THREAD = 0x0A,
96 HPROF_TAG_END_THREAD = 0x0B,
97 HPROF_TAG_HEAP_DUMP = 0x0C,
98 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
99 HPROF_TAG_HEAP_DUMP_END = 0x2C,
100 HPROF_TAG_CPU_SAMPLES = 0x0D,
101 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
102};
103
104// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
105enum HprofHeapTag {
106 // Traditional.
107 HPROF_ROOT_UNKNOWN = 0xFF,
108 HPROF_ROOT_JNI_GLOBAL = 0x01,
109 HPROF_ROOT_JNI_LOCAL = 0x02,
110 HPROF_ROOT_JAVA_FRAME = 0x03,
111 HPROF_ROOT_NATIVE_STACK = 0x04,
112 HPROF_ROOT_STICKY_CLASS = 0x05,
113 HPROF_ROOT_THREAD_BLOCK = 0x06,
114 HPROF_ROOT_MONITOR_USED = 0x07,
115 HPROF_ROOT_THREAD_OBJECT = 0x08,
116 HPROF_CLASS_DUMP = 0x20,
117 HPROF_INSTANCE_DUMP = 0x21,
118 HPROF_OBJECT_ARRAY_DUMP = 0x22,
119 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
120
121 // Android.
122 HPROF_HEAP_DUMP_INFO = 0xfe,
123 HPROF_ROOT_INTERNED_STRING = 0x89,
124 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
125 HPROF_ROOT_DEBUGGER = 0x8b,
126 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
127 HPROF_ROOT_VM_INTERNAL = 0x8d,
128 HPROF_ROOT_JNI_MONITOR = 0x8e,
129 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700130 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700131};
132
133enum HprofHeapId {
134 HPROF_HEAP_DEFAULT = 0,
135 HPROF_HEAP_ZYGOTE = 'Z',
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700136 HPROF_HEAP_APP = 'A',
137 HPROF_HEAP_IMAGE = 'I',
Elliott Hughes622a6982012-06-08 17:58:54 -0700138};
139
140enum HprofBasicType {
141 hprof_basic_object = 2,
142 hprof_basic_boolean = 4,
143 hprof_basic_char = 5,
144 hprof_basic_float = 6,
145 hprof_basic_double = 7,
146 hprof_basic_byte = 8,
147 hprof_basic_short = 9,
148 hprof_basic_int = 10,
149 hprof_basic_long = 11,
150};
151
Andreas Gampec55bb392018-09-21 00:02:02 +0000152using HprofStringId = uint32_t;
153using HprofClassObjectId = uint32_t;
154using HprofClassSerialNumber = uint32_t;
155using HprofStackTraceSerialNumber = uint32_t;
156using HprofStackFrameId = uint32_t;
Man Cao8c2ff642015-05-27 17:25:30 -0700157static constexpr HprofStackTraceSerialNumber kHprofNullStackTrace = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -0700158
Andreas Gampe3a913092015-01-10 00:26:17 -0800159class EndianOutput {
Elliott Hughes622a6982012-06-08 17:58:54 -0700160 public:
Andreas Gampe3a913092015-01-10 00:26:17 -0800161 EndianOutput() : length_(0), sum_length_(0), max_length_(0), started_(false) {}
162 virtual ~EndianOutput() {}
163
164 void StartNewRecord(uint8_t tag, uint32_t time) {
165 if (length_ > 0) {
166 EndRecord();
167 }
168 DCHECK_EQ(length_, 0U);
169 AddU1(tag);
170 AddU4(time);
171 AddU4(0xdeaddead); // Length, replaced on flush.
172 started_ = true;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700173 }
174
Andreas Gampe3a913092015-01-10 00:26:17 -0800175 void EndRecord() {
176 // Replace length in header.
177 if (started_) {
178 UpdateU4(sizeof(uint8_t) + sizeof(uint32_t),
179 length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
180 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700181
Andreas Gampe3a913092015-01-10 00:26:17 -0800182 HandleEndRecord();
183
184 sum_length_ += length_;
185 max_length_ = std::max(max_length_, length_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700186 length_ = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -0800187 started_ = false;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700188 }
189
Andreas Gampe3a913092015-01-10 00:26:17 -0800190 void AddU1(uint8_t value) {
191 AddU1List(&value, 1);
192 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800193 void AddU2(uint16_t value) {
194 AddU2List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700195 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800196 void AddU4(uint32_t value) {
197 AddU4List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700198 }
199
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800200 void AddU8(uint64_t value) {
201 AddU8List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700202 }
203
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800204 void AddObjectId(const mirror::Object* value) {
205 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206 }
207
Man Cao8c2ff642015-05-27 17:25:30 -0700208 void AddStackTraceSerialNumber(HprofStackTraceSerialNumber value) {
209 AddU4(value);
210 }
211
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 // The ID for the synthetic object generated to account for class static overhead.
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800213 void AddClassStaticsId(const mirror::Class* value) {
214 AddU4(1 | PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 }
216
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800217 void AddJniGlobalRefId(jobject value) {
218 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219 }
220
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800221 void AddClassId(HprofClassObjectId value) {
222 AddU4(value);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223 }
224
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800225 void AddStringId(HprofStringId value) {
226 AddU4(value);
Elliott Hughes622a6982012-06-08 17:58:54 -0700227 }
228
Andreas Gampe3a913092015-01-10 00:26:17 -0800229 void AddU1List(const uint8_t* values, size_t count) {
230 HandleU1List(values, count);
231 length_ += count;
232 }
233 void AddU2List(const uint16_t* values, size_t count) {
234 HandleU2List(values, count);
235 length_ += count * sizeof(uint16_t);
236 }
237 void AddU4List(const uint32_t* values, size_t count) {
238 HandleU4List(values, count);
239 length_ += count * sizeof(uint32_t);
240 }
Andreas Gampeca714582015-04-03 19:41:34 -0700241 virtual void UpdateU4(size_t offset, uint32_t new_value ATTRIBUTE_UNUSED) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800242 DCHECK_LE(offset, length_ - 4);
243 }
244 void AddU8List(const uint64_t* values, size_t count) {
245 HandleU8List(values, count);
246 length_ += count * sizeof(uint64_t);
247 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700248
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800249 void AddIdList(mirror::ObjectArray<mirror::Object>* values)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800251 const int32_t length = values->GetLength();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252 for (int32_t i = 0; i < length; ++i) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800253 AddObjectId(values->GetWithoutChecks(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700255 }
256
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800257 void AddUtf8String(const char* str) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700258 // The terminating NUL character is NOT written.
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800259 AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700260 }
261
Andreas Gampe3a913092015-01-10 00:26:17 -0800262 size_t Length() const {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700263 return length_;
264 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700265
Andreas Gampe3a913092015-01-10 00:26:17 -0800266 size_t SumLength() const {
267 return sum_length_;
Elliott Hughes622a6982012-06-08 17:58:54 -0700268 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700269
Andreas Gampe3a913092015-01-10 00:26:17 -0800270 size_t MaxLength() const {
271 return max_length_;
272 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700273
Andreas Gampe3a913092015-01-10 00:26:17 -0800274 protected:
275 virtual void HandleU1List(const uint8_t* values ATTRIBUTE_UNUSED,
276 size_t count ATTRIBUTE_UNUSED) {
277 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700278 virtual void HandleU1AsU2List(const uint8_t* values ATTRIBUTE_UNUSED,
279 size_t count ATTRIBUTE_UNUSED) {
280 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800281 virtual void HandleU2List(const uint16_t* values ATTRIBUTE_UNUSED,
282 size_t count ATTRIBUTE_UNUSED) {
283 }
284 virtual void HandleU4List(const uint32_t* values ATTRIBUTE_UNUSED,
285 size_t count ATTRIBUTE_UNUSED) {
286 }
287 virtual void HandleU8List(const uint64_t* values ATTRIBUTE_UNUSED,
288 size_t count ATTRIBUTE_UNUSED) {
289 }
290 virtual void HandleEndRecord() {
291 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700292
Andreas Gampe3a913092015-01-10 00:26:17 -0800293 size_t length_; // Current record size.
294 size_t sum_length_; // Size of all data.
295 size_t max_length_; // Maximum seen length.
296 bool started_; // Was StartRecord called?
Elliott Hughes622a6982012-06-08 17:58:54 -0700297};
298
Andreas Gampe3a913092015-01-10 00:26:17 -0800299// This keeps things buffered until flushed.
300class EndianOutputBuffered : public EndianOutput {
301 public:
302 explicit EndianOutputBuffered(size_t reserve_size) {
303 buffer_.reserve(reserve_size);
304 }
305 virtual ~EndianOutputBuffered() {}
306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100307 void UpdateU4(size_t offset, uint32_t new_value) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800308 DCHECK_LE(offset, length_ - 4);
309 buffer_[offset + 0] = static_cast<uint8_t>((new_value >> 24) & 0xFF);
310 buffer_[offset + 1] = static_cast<uint8_t>((new_value >> 16) & 0xFF);
311 buffer_[offset + 2] = static_cast<uint8_t>((new_value >> 8) & 0xFF);
312 buffer_[offset + 3] = static_cast<uint8_t>((new_value >> 0) & 0xFF);
313 }
314
315 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100316 void HandleU1List(const uint8_t* values, size_t count) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800317 DCHECK_EQ(length_, buffer_.size());
318 buffer_.insert(buffer_.end(), values, values + count);
319 }
320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100321 void HandleU1AsU2List(const uint8_t* values, size_t count) override {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700322 DCHECK_EQ(length_, buffer_.size());
323 // All 8-bits are grouped in 2 to make 16-bit block like Java Char
324 if (count & 1) {
325 buffer_.push_back(0);
326 }
327 for (size_t i = 0; i < count; ++i) {
328 uint8_t value = *values;
329 buffer_.push_back(value);
330 values++;
331 }
332 }
333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100334 void HandleU2List(const uint16_t* values, size_t count) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800335 DCHECK_EQ(length_, buffer_.size());
336 for (size_t i = 0; i < count; ++i) {
337 uint16_t value = *values;
338 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
339 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
340 values++;
341 }
342 }
343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100344 void HandleU4List(const uint32_t* values, size_t count) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800345 DCHECK_EQ(length_, buffer_.size());
346 for (size_t i = 0; i < count; ++i) {
347 uint32_t value = *values;
348 buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
349 buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
350 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
351 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
352 values++;
353 }
354 }
355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100356 void HandleU8List(const uint64_t* values, size_t count) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800357 DCHECK_EQ(length_, buffer_.size());
358 for (size_t i = 0; i < count; ++i) {
359 uint64_t value = *values;
360 buffer_.push_back(static_cast<uint8_t>((value >> 56) & 0xFF));
361 buffer_.push_back(static_cast<uint8_t>((value >> 48) & 0xFF));
362 buffer_.push_back(static_cast<uint8_t>((value >> 40) & 0xFF));
363 buffer_.push_back(static_cast<uint8_t>((value >> 32) & 0xFF));
364 buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
365 buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
366 buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
367 buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
368 values++;
369 }
370 }
371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100372 void HandleEndRecord() override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800373 DCHECK_EQ(buffer_.size(), length_);
374 if (kIsDebugBuild && started_) {
375 uint32_t stored_length =
376 static_cast<uint32_t>(buffer_[5]) << 24 |
377 static_cast<uint32_t>(buffer_[6]) << 16 |
378 static_cast<uint32_t>(buffer_[7]) << 8 |
379 static_cast<uint32_t>(buffer_[8]);
380 DCHECK_EQ(stored_length, length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
381 }
382 HandleFlush(buffer_.data(), length_);
383 buffer_.clear();
384 }
385
386 virtual void HandleFlush(const uint8_t* buffer ATTRIBUTE_UNUSED, size_t length ATTRIBUTE_UNUSED) {
387 }
388
389 std::vector<uint8_t> buffer_;
390};
391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100392class FileEndianOutput final : public EndianOutputBuffered {
Andreas Gampe3a913092015-01-10 00:26:17 -0800393 public:
394 FileEndianOutput(File* fp, size_t reserved_size)
395 : EndianOutputBuffered(reserved_size), fp_(fp), errors_(false) {
396 DCHECK(fp != nullptr);
397 }
398 ~FileEndianOutput() {
399 }
400
401 bool Errors() {
402 return errors_;
403 }
404
405 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100406 void HandleFlush(const uint8_t* buffer, size_t length) override {
Andreas Gampe3a913092015-01-10 00:26:17 -0800407 if (!errors_) {
408 errors_ = !fp_->WriteFully(buffer, length);
409 }
410 }
411
412 private:
413 File* fp_;
414 bool errors_;
415};
416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100417class VectorEndianOuputput final : public EndianOutputBuffered {
Andreas Gampe3a913092015-01-10 00:26:17 -0800418 public:
Alex Lightc7d2e782018-02-07 12:53:13 -0800419 VectorEndianOuputput(std::vector<uint8_t>& data, size_t reserved_size)
420 : EndianOutputBuffered(reserved_size), full_data_(data) {}
421 ~VectorEndianOuputput() {}
Andreas Gampe3a913092015-01-10 00:26:17 -0800422
423 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100424 void HandleFlush(const uint8_t* buf, size_t length) override {
Alex Lightc7d2e782018-02-07 12:53:13 -0800425 size_t old_size = full_data_.size();
426 full_data_.resize(old_size + length);
427 memcpy(full_data_.data() + old_size, buf, length);
Andreas Gampe3a913092015-01-10 00:26:17 -0800428 }
429
430 private:
Alex Lightc7d2e782018-02-07 12:53:13 -0800431 std::vector<uint8_t>& full_data_;
Andreas Gampe3a913092015-01-10 00:26:17 -0800432};
433
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700434#define __ output_->
Andreas Gampe3a913092015-01-10 00:26:17 -0800435
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700436class Hprof : public SingleRootVisitor {
Elliott Hughes622a6982012-06-08 17:58:54 -0700437 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700438 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
439 : filename_(output_filename),
440 fd_(fd),
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800441 direct_to_ddms_(direct_to_ddms) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700442 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500443 }
444
Andreas Gampe3a913092015-01-10 00:26:17 -0800445 void Dump()
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800446 REQUIRES(Locks::mutator_lock_)
447 REQUIRES(!Locks::heap_bitmap_lock_, !Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700448 {
449 MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
450 if (Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) {
451 PopulateAllocationTrackingTraces();
452 }
453 }
454
Andreas Gampe3a913092015-01-10 00:26:17 -0800455 // First pass to measure the size of the dump.
456 size_t overall_size;
457 size_t max_length;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800458 {
Andreas Gampe3a913092015-01-10 00:26:17 -0800459 EndianOutput count_output;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700460 output_ = &count_output;
461 ProcessHeap(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800462 overall_size = count_output.SumLength();
463 max_length = count_output.MaxLength();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700464 output_ = nullptr;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800465 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700466
Andreas Gampe3a913092015-01-10 00:26:17 -0800467 bool okay;
Mathieu Chartierecc82302017-02-16 10:20:12 -0800468 visited_objects_.clear();
Andreas Gampe3a913092015-01-10 00:26:17 -0800469 if (direct_to_ddms_) {
470 if (kDirectStream) {
471 okay = DumpToDdmsDirect(overall_size, max_length, CHUNK_TYPE("HPDS"));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700472 } else {
Andreas Gampe3a913092015-01-10 00:26:17 -0800473 okay = DumpToDdmsBuffered(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700474 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800475 } else {
476 okay = DumpToFile(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700477 }
478
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700479 if (okay) {
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800480 const uint64_t duration = NanoTime() - start_ns_;
481 LOG(INFO) << "hprof: heap dump completed (" << PrettySize(RoundUp(overall_size, KB))
482 << ") in " << PrettyDuration(duration)
483 << " objects " << total_objects_
484 << " objects with stack traces " << total_objects_with_stack_trace_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700485 }
486 }
487
488 private:
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700489 void DumpHeapObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700490 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700491
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700492 void DumpHeapClass(mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700493 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700494
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700495 void DumpHeapArray(mirror::Array* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700496 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800497
Mathieu Chartier996f75e2017-03-31 11:18:41 -0700498 void DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700499 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800500
Mathieu Chartier996f75e2017-03-31 11:18:41 -0700501 void DumpHeapInstanceObject(mirror::Object* obj,
502 mirror::Class* klass,
503 const std::set<mirror::Object*>& fake_roots)
504 REQUIRES_SHARED(Locks::mutator_lock_);
505
506 bool AddRuntimeInternalObjectsField(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
507
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700508 void ProcessHeap(bool header_first)
Mathieu Chartier90443472015-07-16 20:32:27 -0700509 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800510 // Reset current heap and object count.
511 current_heap_ = HPROF_HEAP_DEFAULT;
512 objects_in_segment_ = 0;
513
514 if (header_first) {
Man Cao8c2ff642015-05-27 17:25:30 -0700515 ProcessHeader(true);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700516 ProcessBody();
Andreas Gampe3a913092015-01-10 00:26:17 -0800517 } else {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700518 ProcessBody();
Man Cao8c2ff642015-05-27 17:25:30 -0700519 ProcessHeader(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800520 }
521 }
522
Mathieu Chartier90443472015-07-16 20:32:27 -0700523 void ProcessBody() REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700524 Runtime* const runtime = Runtime::Current();
Andreas Gampe3a913092015-01-10 00:26:17 -0800525 // Walk the roots and the heap.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700526 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
Andreas Gampe3a913092015-01-10 00:26:17 -0800527
Richard Uhler4b767552016-04-26 13:28:59 -0700528 simple_roots_.clear();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700529 runtime->VisitRoots(this);
530 runtime->VisitImageRoots(this);
Andreas Gampe1c158a02017-07-13 17:26:19 -0700531 auto dump_object = [this](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
532 DCHECK(obj != nullptr);
533 DumpHeapObject(obj);
534 };
535 runtime->GetHeap()->VisitObjectsPaused(dump_object);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700536 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_END, kHprofTime);
537 output_->EndRecord();
Andreas Gampe3a913092015-01-10 00:26:17 -0800538 }
539
Mathieu Chartier90443472015-07-16 20:32:27 -0700540 void ProcessHeader(bool string_first) REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800541 // Write the header.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700542 WriteFixedHeader();
Andreas Gampe3a913092015-01-10 00:26:17 -0800543 // Write the string and class tables, and any stack traces, to the header.
544 // (jhat requires that these appear before any of the data in the body that refers to them.)
Man Cao8c2ff642015-05-27 17:25:30 -0700545 // jhat also requires the string table appear before class table and stack traces.
546 // However, WriteStackTraces() can modify the string table, so it's necessary to call
547 // WriteStringTable() last in the first pass, to compute the correct length of the output.
548 if (string_first) {
549 WriteStringTable();
550 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700551 WriteClassTable();
552 WriteStackTraces();
Man Cao8c2ff642015-05-27 17:25:30 -0700553 if (!string_first) {
554 WriteStringTable();
555 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700556 output_->EndRecord();
Andreas Gampe3a913092015-01-10 00:26:17 -0800557 }
558
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700559 void WriteClassTable() REQUIRES_SHARED(Locks::mutator_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700560 for (const auto& p : classes_) {
561 mirror::Class* c = p.first;
562 HprofClassSerialNumber sn = p.second;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 CHECK(c != nullptr);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700564 output_->StartNewRecord(HPROF_TAG_LOAD_CLASS, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700565 // LOAD CLASS format:
566 // U4: class serial number (always > 0)
567 // ID: class object ID. We use the address of the class object structure as its ID.
568 // U4: stack trace serial number
569 // ID: class name string ID
Man Cao8c2ff642015-05-27 17:25:30 -0700570 __ AddU4(sn);
Andreas Gampe3a913092015-01-10 00:26:17 -0800571 __ AddObjectId(c);
Man Cao8c2ff642015-05-27 17:25:30 -0700572 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(c));
Andreas Gampe3a913092015-01-10 00:26:17 -0800573 __ AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700574 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700575 }
576
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700577 void WriteStringTable() {
Andreas Gampe1eeb00c2016-11-03 08:19:01 -0700578 for (const auto& p : strings_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800579 const std::string& string = p.first;
Andreas Gampe1eeb00c2016-11-03 08:19:01 -0700580 const HprofStringId id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700581
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700582 output_->StartNewRecord(HPROF_TAG_STRING, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700583
584 // STRING format:
585 // ID: ID for this string
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700586 // U1*: UTF8 characters for string (NOT null terminated)
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700587 // (the record format encodes the length)
Andreas Gampe3a913092015-01-10 00:26:17 -0800588 __ AddU4(id);
589 __ AddUtf8String(string.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700590 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700591 }
592
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700593 void StartNewHeapDumpSegment() {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700594 // This flushes the old segment and starts a new one.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700595 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700596 objects_in_segment_ = 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700597 // Starting a new HEAP_DUMP resets the heap to default.
598 current_heap_ = HPROF_HEAP_DEFAULT;
599 }
600
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700601 void CheckHeapSegmentConstraints() {
602 if (objects_in_segment_ >= kMaxObjectsPerSegment || output_->Length() >= kMaxBytesPerSegment) {
603 StartNewHeapDumpSegment();
Andreas Gampe3a913092015-01-10 00:26:17 -0800604 }
605 }
606
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700607 void VisitRoot(mirror::Object* obj, const RootInfo& root_info)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100608 override REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800609 void MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700610 uint32_t thread_serial);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700611
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700612 HprofClassObjectId LookupClassId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800613 if (c != nullptr) {
Man Cao8c2ff642015-05-27 17:25:30 -0700614 auto it = classes_.find(c);
615 if (it == classes_.end()) {
616 // first time to see this class
617 HprofClassSerialNumber sn = next_class_serial_number_++;
618 classes_.Put(c, sn);
619 // Make sure that we've assigned a string ID for this class' name
620 LookupClassNameId(c);
621 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800622 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800623 return PointerToLowMemUInt32(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700624 }
625
Man Cao8c2ff642015-05-27 17:25:30 -0700626 HprofStackTraceSerialNumber LookupStackTraceSerialNumber(const mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 REQUIRES_SHARED(Locks::mutator_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700628 auto r = allocation_records_.find(obj);
629 if (r == allocation_records_.end()) {
630 return kHprofNullStackTrace;
631 } else {
632 const gc::AllocRecordStackTrace* trace = r->second;
633 auto result = traces_.find(trace);
634 CHECK(result != traces_.end());
635 return result->second;
636 }
637 }
638
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700639 HprofStringId LookupStringId(mirror::String* string) REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700640 return LookupStringId(string->ToModifiedUtf8());
641 }
642
643 HprofStringId LookupStringId(const char* string) {
644 return LookupStringId(std::string(string));
645 }
646
647 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800648 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700649 if (it != strings_.end()) {
650 return it->second;
651 }
652 HprofStringId id = next_string_id_++;
653 strings_.Put(string, id);
654 return id;
655 }
656
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700657 HprofStringId LookupClassNameId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700658 return LookupStringId(c->PrettyDescriptor());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700659 }
660
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700661 void WriteFixedHeader() {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500662 // Write the file header.
663 // U1: NUL-terminated magic string.
Andreas Gampe3a913092015-01-10 00:26:17 -0800664 const char magic[] = "JAVA PROFILE 1.0.3";
665 __ AddU1List(reinterpret_cast<const uint8_t*>(magic), sizeof(magic));
666
Calin Juravle32805172014-07-04 16:24:03 +0100667 // U4: size of identifiers. We're using addresses as IDs and our heap references are stored
668 // as uint32_t.
669 // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800670 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t),
671 "Unexpected HeapReference size");
Andreas Gampe3a913092015-01-10 00:26:17 -0800672 __ AddU4(sizeof(uint32_t));
673
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500674 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700675 timeval now;
Andreas Gampe3a913092015-01-10 00:26:17 -0800676 const uint64_t nowMs = (gettimeofday(&now, nullptr) < 0) ? 0 :
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800677 (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
Andreas Gampe3a913092015-01-10 00:26:17 -0800678 // TODO: It seems it would be correct to use U8.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500679 // U4: high word of the 64-bit time.
Andreas Gampe3a913092015-01-10 00:26:17 -0800680 __ AddU4(static_cast<uint32_t>(nowMs >> 32));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500681 // U4: low word of the 64-bit time.
Andreas Gampe3a913092015-01-10 00:26:17 -0800682 __ AddU4(static_cast<uint32_t>(nowMs & 0xFFFFFFFF));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500683 }
684
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700685 void WriteStackTraces() REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700686 // Write a dummy stack trace record so the analysis tools don't freak out.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700687 output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
Man Cao8c2ff642015-05-27 17:25:30 -0700688 __ AddStackTraceSerialNumber(kHprofNullStackTrace);
Andreas Gampe3a913092015-01-10 00:26:17 -0800689 __ AddU4(kHprofNullThread);
690 __ AddU4(0); // no frames
Man Cao8c2ff642015-05-27 17:25:30 -0700691
692 // TODO: jhat complains "WARNING: Stack trace not found for serial # -1", but no trace should
693 // have -1 as its serial number (as long as HprofStackTraceSerialNumber doesn't overflow).
694 for (const auto& it : traces_) {
695 const gc::AllocRecordStackTrace* trace = it.first;
696 HprofStackTraceSerialNumber trace_sn = it.second;
697 size_t depth = trace->GetDepth();
698
699 // First write stack frames of the trace
700 for (size_t i = 0; i < depth; ++i) {
701 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
702 ArtMethod* method = frame->GetMethod();
703 CHECK(method != nullptr);
704 output_->StartNewRecord(HPROF_TAG_STACK_FRAME, kHprofTime);
705 // STACK FRAME format:
706 // ID: stack frame ID. We use the address of the AllocRecordStackTraceElement object as its ID.
707 // ID: method name string ID
708 // ID: method signature string ID
709 // ID: source file name string ID
710 // U4: class serial number
711 // U4: >0, line number; 0, no line information available; -1, unknown location
712 auto frame_result = frames_.find(frame);
713 CHECK(frame_result != frames_.end());
714 __ AddU4(frame_result->second);
715 __ AddStringId(LookupStringId(method->GetName()));
716 __ AddStringId(LookupStringId(method->GetSignature().ToString()));
717 const char* source_file = method->GetDeclaringClassSourceFile();
718 if (source_file == nullptr) {
719 source_file = "";
720 }
721 __ AddStringId(LookupStringId(source_file));
Vladimir Markod93e3742018-07-18 10:58:13 +0100722 auto class_result = classes_.find(method->GetDeclaringClass().Ptr());
Man Cao8c2ff642015-05-27 17:25:30 -0700723 CHECK(class_result != classes_.end());
724 __ AddU4(class_result->second);
725 __ AddU4(frame->ComputeLineNumber());
726 }
727
728 // Then write the trace itself
729 output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
730 // STACK TRACE format:
731 // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number.
732 // U4: thread serial number. We use Thread::GetTid().
733 // U4: number of frames
734 // [ID]*: series of stack frame ID's
735 __ AddStackTraceSerialNumber(trace_sn);
736 __ AddU4(trace->GetTid());
737 __ AddU4(depth);
738 for (size_t i = 0; i < depth; ++i) {
739 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
740 auto frame_result = frames_.find(frame);
741 CHECK(frame_result != frames_.end());
742 __ AddU4(frame_result->second);
743 }
744 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800745 }
746
747 bool DumpToDdmsBuffered(size_t overall_size ATTRIBUTE_UNUSED, size_t max_length ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700748 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800749 LOG(FATAL) << "Unimplemented";
750 UNREACHABLE();
751 // // Send the data off to DDMS.
752 // iovec iov[2];
753 // iov[0].iov_base = header_data_ptr_;
754 // iov[0].iov_len = header_data_size_;
755 // iov[1].iov_base = body_data_ptr_;
756 // iov[1].iov_len = body_data_size_;
757 // Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
758 }
759
760 bool DumpToFile(size_t overall_size, size_t max_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700761 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800762 // Where exactly are we writing to?
763 int out_fd;
764 if (fd_ >= 0) {
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700765 out_fd = DupCloexec(fd_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800766 if (out_fd < 0) {
767 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
768 return false;
769 }
770 } else {
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700771 out_fd = open(filename_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
Andreas Gampe3a913092015-01-10 00:26:17 -0800772 if (out_fd < 0) {
773 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
774 strerror(errno));
775 return false;
776 }
777 }
778
779 std::unique_ptr<File> file(new File(out_fd, filename_, true));
780 bool okay;
781 {
782 FileEndianOutput file_output(file.get(), max_length);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700783 output_ = &file_output;
784 ProcessHeap(true);
Andreas Gampe3a913092015-01-10 00:26:17 -0800785 okay = !file_output.Errors();
786
787 if (okay) {
Andreas Gampec515f212015-08-28 18:15:27 -0700788 // Check for expected size. Output is expected to be less-or-equal than first phase, see
789 // b/23521263.
790 DCHECK_LE(file_output.SumLength(), overall_size);
Andreas Gampe3a913092015-01-10 00:26:17 -0800791 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700792 output_ = nullptr;
Andreas Gampe3a913092015-01-10 00:26:17 -0800793 }
794
795 if (okay) {
796 okay = file->FlushCloseOrErase() == 0;
797 } else {
798 file->Erase();
799 }
800 if (!okay) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800801 std::string msg(android::base::StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
802 filename_.c_str(),
803 strerror(errno)));
Andreas Gampe3a913092015-01-10 00:26:17 -0800804 ThrowRuntimeException("%s", msg.c_str());
805 LOG(ERROR) << msg;
806 }
807
808 return okay;
809 }
810
811 bool DumpToDdmsDirect(size_t overall_size, size_t max_length, uint32_t chunk_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700812 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800813 CHECK(direct_to_ddms_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800814
Alex Lightc7d2e782018-02-07 12:53:13 -0800815 std::vector<uint8_t> out_data;
Andreas Gampe3a913092015-01-10 00:26:17 -0800816
Alex Lightc7d2e782018-02-07 12:53:13 -0800817 // TODO It would be really good to have some streaming thing again. b/73084059
818 VectorEndianOuputput output(out_data, max_length);
819 output_ = &output;
Andreas Gampe3a913092015-01-10 00:26:17 -0800820
821 // Write the dump.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700822 ProcessHeap(true);
Andreas Gampe3a913092015-01-10 00:26:17 -0800823
Alex Lightc7d2e782018-02-07 12:53:13 -0800824 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(
825 chunk_type, ArrayRef<const uint8_t>(out_data.data(), out_data.size()));
826
Andreas Gampec515f212015-08-28 18:15:27 -0700827 // Check for expected size. See DumpToFile for comment.
Alex Lightc7d2e782018-02-07 12:53:13 -0800828 DCHECK_LE(output.SumLength(), overall_size);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700829 output_ = nullptr;
Andreas Gampe3a913092015-01-10 00:26:17 -0800830
831 return true;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700832 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500833
Man Cao8c2ff642015-05-27 17:25:30 -0700834 void PopulateAllocationTrackingTraces()
Mathieu Chartier90443472015-07-16 20:32:27 -0700835 REQUIRES(Locks::mutator_lock_, Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700836 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
837 CHECK(records != nullptr);
838 HprofStackTraceSerialNumber next_trace_sn = kHprofNullStackTrace + 1;
839 HprofStackFrameId next_frame_id = 0;
Man Cao42c3c332015-06-23 16:38:25 -0700840 size_t count = 0;
Man Cao8c2ff642015-05-27 17:25:30 -0700841
842 for (auto it = records->Begin(), end = records->End(); it != end; ++it) {
843 const mirror::Object* obj = it->first.Read();
Man Cao42c3c332015-06-23 16:38:25 -0700844 if (obj == nullptr) {
845 continue;
846 }
847 ++count;
Mathieu Chartier458b1052016-03-29 14:02:55 -0700848 const gc::AllocRecordStackTrace* trace = it->second.GetStackTrace();
Man Cao8c2ff642015-05-27 17:25:30 -0700849
850 // Copy the pair into a real hash map to speed up look up.
851 auto records_result = allocation_records_.emplace(obj, trace);
852 // The insertion should always succeed, i.e. no duplicate object pointers in "records"
853 CHECK(records_result.second);
854
855 // Generate serial numbers for traces, and IDs for frames.
856 auto traces_result = traces_.find(trace);
857 if (traces_result == traces_.end()) {
858 traces_.emplace(trace, next_trace_sn++);
859 // only check frames if the trace is newly discovered
860 for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) {
861 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
862 auto frames_result = frames_.find(frame);
863 if (frames_result == frames_.end()) {
864 frames_.emplace(frame, next_frame_id++);
865 }
866 }
867 }
868 }
869 CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1);
870 CHECK_EQ(frames_.size(), next_frame_id);
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800871 total_objects_with_stack_trace_ = count;
Man Cao8c2ff642015-05-27 17:25:30 -0700872 }
873
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700874 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
875 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
876 // only be used for debug messages.
877 std::string filename_;
878 int fd_;
879 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500880
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800881 uint64_t start_ns_ = NanoTime();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700882
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800883 EndianOutput* output_ = nullptr;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700884
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800885 HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT; // Which heap we're currently dumping.
886 size_t objects_in_segment_ = 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700887
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800888 size_t total_objects_ = 0u;
889 size_t total_objects_with_stack_trace_ = 0u;
890
891 HprofStringId next_string_id_ = 0x400000;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800892 SafeMap<std::string, HprofStringId> strings_;
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800893 HprofClassSerialNumber next_class_serial_number_ = 1;
Man Cao8c2ff642015-05-27 17:25:30 -0700894 SafeMap<mirror::Class*, HprofClassSerialNumber> classes_;
895
896 std::unordered_map<const gc::AllocRecordStackTrace*, HprofStackTraceSerialNumber,
897 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTrace>,
898 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTrace>> traces_;
899 std::unordered_map<const gc::AllocRecordStackTraceElement*, HprofStackFrameId,
900 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>,
901 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>> frames_;
902 std::unordered_map<const mirror::Object*, const gc::AllocRecordStackTrace*> allocation_records_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700903
Richard Uhler4b767552016-04-26 13:28:59 -0700904 // Set used to keep track of what simple root records we have already
905 // emitted, to avoid emitting duplicate entries. The simple root records are
906 // those that contain no other information than the root type and the object
907 // id. A pair of root type and object id is packed into a uint64_t, with
908 // the root type in the upper 32 bits and the object id in the lower 32
909 // bits.
910 std::unordered_set<uint64_t> simple_roots_;
911
Mathieu Chartierecc82302017-02-16 10:20:12 -0800912 // To make sure we don't dump the same object multiple times. b/34967844
913 std::unordered_set<mirror::Object*> visited_objects_;
914
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700915 friend class GcRootVisitor;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700916 DISALLOW_COPY_AND_ASSIGN(Hprof);
917};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500918
Andreas Gampe3a913092015-01-10 00:26:17 -0800919static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* size_out) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500920 char c = sig[0];
921 HprofBasicType ret;
922 size_t size;
923
924 switch (c) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800925 case '[':
926 case 'L':
927 ret = hprof_basic_object;
928 size = 4;
929 break;
930 case 'Z':
931 ret = hprof_basic_boolean;
932 size = 1;
933 break;
934 case 'C':
935 ret = hprof_basic_char;
936 size = 2;
937 break;
938 case 'F':
939 ret = hprof_basic_float;
940 size = 4;
941 break;
942 case 'D':
943 ret = hprof_basic_double;
944 size = 8;
945 break;
946 case 'B':
947 ret = hprof_basic_byte;
948 size = 1;
949 break;
950 case 'S':
951 ret = hprof_basic_short;
952 size = 2;
953 break;
954 case 'I':
955 ret = hprof_basic_int;
956 size = 4;
957 break;
958 case 'J':
959 ret = hprof_basic_long;
960 size = 8;
961 break;
962 default:
963 LOG(FATAL) << "UNREACHABLE";
964 UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500965 }
966
Andreas Gampe3a913092015-01-10 00:26:17 -0800967 if (size_out != nullptr) {
968 *size_out = size;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500969 }
970
971 return ret;
972}
973
974// Always called when marking objects, but only does
975// something when ctx->gc_scan_state_ is non-zero, which is usually
976// only true when marking the root set or unreachable
977// objects. Used to add rootset references to obj.
Andreas Gampe3a913092015-01-10 00:26:17 -0800978void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700979 uint32_t thread_serial) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800980 if (heap_tag == 0) {
981 return;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500982 }
983
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700984 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500985
Andreas Gampe3a913092015-01-10 00:26:17 -0800986 switch (heap_tag) {
987 // ID: object ID
988 case HPROF_ROOT_UNKNOWN:
989 case HPROF_ROOT_STICKY_CLASS:
990 case HPROF_ROOT_MONITOR_USED:
991 case HPROF_ROOT_INTERNED_STRING:
992 case HPROF_ROOT_DEBUGGER:
Richard Uhler4b767552016-04-26 13:28:59 -0700993 case HPROF_ROOT_VM_INTERNAL: {
994 uint64_t key = (static_cast<uint64_t>(heap_tag) << 32) | PointerToLowMemUInt32(obj);
995 if (simple_roots_.insert(key).second) {
996 __ AddU1(heap_tag);
997 __ AddObjectId(obj);
998 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800999 break;
Richard Uhler4b767552016-04-26 13:28:59 -07001000 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001001
Andreas Gampe3a913092015-01-10 00:26:17 -08001002 // ID: object ID
1003 // ID: JNI global ref ID
1004 case HPROF_ROOT_JNI_GLOBAL:
1005 __ AddU1(heap_tag);
1006 __ AddObjectId(obj);
1007 __ AddJniGlobalRefId(jni_obj);
1008 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001009
Andreas Gampe3a913092015-01-10 00:26:17 -08001010 // ID: object ID
1011 // U4: thread serial number
1012 // U4: frame number in stack trace (-1 for empty)
1013 case HPROF_ROOT_JNI_LOCAL:
1014 case HPROF_ROOT_JNI_MONITOR:
1015 case HPROF_ROOT_JAVA_FRAME:
1016 __ AddU1(heap_tag);
1017 __ AddObjectId(obj);
1018 __ AddU4(thread_serial);
1019 __ AddU4((uint32_t)-1);
1020 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001021
Andreas Gampe3a913092015-01-10 00:26:17 -08001022 // ID: object ID
1023 // U4: thread serial number
1024 case HPROF_ROOT_NATIVE_STACK:
1025 case HPROF_ROOT_THREAD_BLOCK:
1026 __ AddU1(heap_tag);
1027 __ AddObjectId(obj);
1028 __ AddU4(thread_serial);
1029 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001030
Andreas Gampe3a913092015-01-10 00:26:17 -08001031 // ID: thread object ID
1032 // U4: thread serial number
1033 // U4: stack trace serial number
1034 case HPROF_ROOT_THREAD_OBJECT:
1035 __ AddU1(heap_tag);
1036 __ AddObjectId(obj);
1037 __ AddU4(thread_serial);
1038 __ AddU4((uint32_t)-1); // xxx
1039 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001040
Andreas Gampe3a913092015-01-10 00:26:17 -08001041 case HPROF_CLASS_DUMP:
1042 case HPROF_INSTANCE_DUMP:
1043 case HPROF_OBJECT_ARRAY_DUMP:
1044 case HPROF_PRIMITIVE_ARRAY_DUMP:
1045 case HPROF_HEAP_DUMP_INFO:
1046 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
1047 // Ignored.
1048 break;
Elliott Hughes73e66f72012-05-09 09:34:45 -07001049
Andreas Gampe3a913092015-01-10 00:26:17 -08001050 case HPROF_ROOT_FINALIZING:
1051 case HPROF_ROOT_REFERENCE_CLEANUP:
1052 case HPROF_UNREACHABLE:
1053 LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001054 UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001055 }
1056
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001057 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001058}
1059
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001060bool Hprof::AddRuntimeInternalObjectsField(mirror::Class* klass) {
1061 if (klass->IsDexCacheClass()) {
1062 return true;
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001063 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001064 // IsClassLoaderClass is true for subclasses of classloader but we only want to add the fake
1065 // field to the java.lang.ClassLoader class.
1066 if (klass->IsClassLoaderClass() && klass->GetSuperClass()->IsObjectClass()) {
1067 return true;
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001068 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001069 return false;
1070}
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001071
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001072void Hprof::DumpHeapObject(mirror::Object* obj) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001073 // Ignore classes that are retired.
1074 if (obj->IsClass() && obj->AsClass()->IsRetired()) {
1075 return;
1076 }
Andreas Gampedd473942018-10-09 08:36:21 -07001077 DCHECK(visited_objects_.insert(obj).second)
1078 << "Already visited " << obj << "(" << obj->PrettyTypeOf() << ")";
Andreas Gampe3a913092015-01-10 00:26:17 -08001079
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -08001080 ++total_objects_;
1081
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001082 class RootCollector {
1083 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001084 RootCollector() {}
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001085
1086 void operator()(mirror::Object*, MemberOffset, bool) const {}
1087
1088 // Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be
1089 // running during the hprof dumping process.
1090 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1091 REQUIRES_SHARED(Locks::mutator_lock_) {
1092 if (!root->IsNull()) {
1093 VisitRoot(root);
1094 }
1095 }
1096
1097 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1098 REQUIRES_SHARED(Locks::mutator_lock_) {
1099 roots_.insert(root->AsMirrorPtr());
1100 }
1101
1102 const std::set<mirror::Object*>& GetRoots() const {
1103 return roots_;
1104 }
1105
1106 private:
1107 // These roots are actually live from the object. Avoid marking them as roots in hprof to make
1108 // it easier to debug class unloading.
1109 mutable std::set<mirror::Object*> roots_;
1110 };
1111
1112 RootCollector visitor;
1113 // Collect all native roots.
1114 if (!obj->IsClass()) {
1115 obj->VisitReferences(visitor, VoidFunctor());
1116 }
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001117
Mathieu Chartiere7158112015-06-03 13:32:15 -07001118 gc::Heap* const heap = Runtime::Current()->GetHeap();
1119 const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001120 HprofHeapId heap_type = HPROF_HEAP_APP;
1121 if (space != nullptr) {
1122 if (space->IsZygoteSpace()) {
1123 heap_type = HPROF_HEAP_ZYGOTE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001124 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartier0b874522017-02-24 14:47:08 -08001125 } else if (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) {
1126 // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects as
1127 // HPROF_HEAP_APP. b/35762934
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001128 heap_type = HPROF_HEAP_IMAGE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001129 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001130 }
Mathieu Chartiere7158112015-06-03 13:32:15 -07001131 } else {
1132 const auto* los = heap->GetLargeObjectsSpace();
1133 if (los->Contains(obj) && los->IsZygoteLargeObject(Thread::Current(), obj)) {
1134 heap_type = HPROF_HEAP_ZYGOTE;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001135 VisitRoot(obj, RootInfo(kRootVMInternal));
Mathieu Chartiere7158112015-06-03 13:32:15 -07001136 }
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001137 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001138 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001139
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001140 if (heap_type != current_heap_) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001141 HprofStringId nameId;
1142
1143 // This object is in a different heap than the current one.
1144 // Emit a HEAP_DUMP_INFO tag to change heaps.
Andreas Gampe3a913092015-01-10 00:26:17 -08001145 __ AddU1(HPROF_HEAP_DUMP_INFO);
1146 __ AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001147 switch (heap_type) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001148 case HPROF_HEAP_APP:
1149 nameId = LookupStringId("app");
1150 break;
1151 case HPROF_HEAP_ZYGOTE:
1152 nameId = LookupStringId("zygote");
1153 break;
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001154 case HPROF_HEAP_IMAGE:
1155 nameId = LookupStringId("image");
1156 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001157 default:
1158 // Internal error
1159 LOG(ERROR) << "Unexpected desiredHeap";
1160 nameId = LookupStringId("<ILLEGAL>");
1161 break;
1162 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001163 __ AddStringId(nameId);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001164 current_heap_ = heap_type;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001165 }
1166
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001167 mirror::Class* c = obj->GetClass();
Andreas Gampe3a913092015-01-10 00:26:17 -08001168 if (c == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001169 // This object will bother HprofReader, because it has a null
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001170 // class, so just don't dump it. It could be
1171 // gDvm.unlinkedJavaLangClass or it could be an object just
1172 // allocated which hasn't been initialized yet.
1173 } else {
1174 if (obj->IsClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001175 DumpHeapClass(obj->AsClass());
Elliott Hughese84278b2012-03-22 10:06:53 -07001176 } else if (c->IsArrayClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001177 DumpHeapArray(obj->AsArray(), c);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001178 } else {
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001179 DumpHeapInstanceObject(obj, c, visitor.GetRoots());
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001180 }
1181 }
1182
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001183 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001184}
1185
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001186void Hprof::DumpHeapClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +00001187 if (!klass->IsResolved()) {
Mathieu Chartier12393f32016-10-20 17:11:23 -07001188 // Class is allocated but not yet resolved: we cannot access its fields or super class.
Sebastien Hertzca068b22015-04-07 10:28:53 +02001189 return;
1190 }
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001191
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001192 // Note: We will emit instance fields of Class as synthetic static fields with a prefix of
1193 // "$class$" so the class fields are visible in hprof dumps. For tools to account for that
1194 // correctly, we'll emit an instance size of zero for java.lang.Class, and also emit the
1195 // instance fields of java.lang.Object.
1196 //
1197 // For other overhead (currently only the embedded vtable), we will generate a synthetic
1198 // byte array (or field[s] in case the overhead size is of reference size or less).
1199
1200 const size_t num_static_fields = klass->NumStaticFields();
1201
1202 // Total class size:
1203 // * class instance fields (including Object instance fields)
1204 // * vtable
1205 // * class static fields
1206 const size_t total_class_size = klass->GetClassSize();
1207
1208 // Base class size (common parts of all Class instances):
1209 // * class instance fields (including Object instance fields)
1210 constexpr size_t base_class_size = sizeof(mirror::Class);
1211 CHECK_LE(base_class_size, total_class_size);
1212
1213 // Difference of Total and Base:
1214 // * vtable
1215 // * class static fields
1216 const size_t base_overhead_size = total_class_size - base_class_size;
1217
1218 // Tools (ahat/Studio) will count the static fields and account for them in the class size. We
1219 // must thus subtract them from base_overhead_size or they will be double-counted.
1220 size_t class_static_fields_size = 0;
1221 for (ArtField& class_static_field : klass->GetSFields()) {
1222 size_t size = 0;
1223 SignatureToBasicTypeAndSize(class_static_field.GetTypeDescriptor(), &size);
1224 class_static_fields_size += size;
1225 }
1226
1227 CHECK_GE(base_overhead_size, class_static_fields_size);
1228 // Now we have:
1229 // * vtable
1230 const size_t base_no_statics_overhead_size = base_overhead_size - class_static_fields_size;
1231
1232 // We may decide to display native overhead (the actual IMT, ArtFields and ArtMethods) in the
1233 // future.
1234 const size_t java_heap_overhead_size = base_no_statics_overhead_size;
1235
1236 // For overhead greater 4, we'll allocate a synthetic array.
1237 if (java_heap_overhead_size > 4) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001238 // Create a byte array to reflect the allocation of the
1239 // StaticField array at the end of this class.
1240 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1241 __ AddClassStaticsId(klass);
Man Cao8c2ff642015-05-27 17:25:30 -07001242 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001243 __ AddU4(java_heap_overhead_size - 4);
Andreas Gampe3a913092015-01-10 00:26:17 -08001244 __ AddU1(hprof_basic_byte);
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001245 for (size_t i = 0; i < java_heap_overhead_size - 4; ++i) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001246 __ AddU1(0);
1247 }
1248 }
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001249 const size_t java_heap_overhead_field_count = java_heap_overhead_size > 0
1250 ? (java_heap_overhead_size == 3 ? 2u : 1u)
1251 : 0;
Andreas Gampe3a913092015-01-10 00:26:17 -08001252
1253 __ AddU1(HPROF_CLASS_DUMP);
1254 __ AddClassId(LookupClassId(klass));
Man Cao8c2ff642015-05-27 17:25:30 -07001255 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Andreas Gampe98104992018-10-16 12:49:47 -07001256 __ AddClassId(LookupClassId(klass->GetSuperClass().Ptr()));
Andreas Gampe3a913092015-01-10 00:26:17 -08001257 __ AddObjectId(klass->GetClassLoader());
1258 __ AddObjectId(nullptr); // no signer
1259 __ AddObjectId(nullptr); // no prot domain
1260 __ AddObjectId(nullptr); // reserved
1261 __ AddObjectId(nullptr); // reserved
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001262 // Instance size.
Andreas Gampe3a913092015-01-10 00:26:17 -08001263 if (klass->IsClassClass()) {
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001264 // As mentioned above, we will emit instance fields as synthetic static fields. So the
1265 // base object is "empty."
1266 __ AddU4(0);
Jeff Hao0ce43532015-05-12 18:58:32 -07001267 } else if (klass->IsStringClass()) {
1268 // Strings are variable length with character data at the end like arrays.
1269 // This outputs the size of an empty string.
1270 __ AddU4(sizeof(mirror::String));
1271 } else if (klass->IsArrayClass() || klass->IsPrimitive()) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001272 __ AddU4(0);
1273 } else {
1274 __ AddU4(klass->GetObjectSize()); // instance size
1275 }
1276
1277 __ AddU2(0); // empty const pool
1278
1279 // Static fields
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001280 //
1281 // Note: we report Class' and Object's instance fields here, too. This is for visibility reasons.
1282 // (b/38167721)
1283 mirror::Class* class_class = klass->GetClass();
1284
1285 DCHECK(class_class->GetSuperClass()->IsObjectClass());
1286 const size_t static_fields_reported = class_class->NumInstanceFields()
1287 + class_class->GetSuperClass()->NumInstanceFields()
1288 + java_heap_overhead_field_count
1289 + num_static_fields;
1290 __ AddU2(dchecked_integral_cast<uint16_t>(static_fields_reported));
1291
1292 if (java_heap_overhead_size != 0) {
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001293 __ AddStringId(LookupStringId(kClassOverheadName));
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001294 size_t overhead_fields = 0;
1295 if (java_heap_overhead_size > 4) {
1296 __ AddU1(hprof_basic_object);
1297 __ AddClassStaticsId(klass);
1298 ++overhead_fields;
1299 } else {
1300 switch (java_heap_overhead_size) {
1301 case 4: {
1302 __ AddU1(hprof_basic_int);
1303 __ AddU4(0);
1304 ++overhead_fields;
1305 break;
1306 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001307
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001308 case 2: {
1309 __ AddU1(hprof_basic_short);
1310 __ AddU2(0);
1311 ++overhead_fields;
1312 break;
1313 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001314
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001315 case 3: {
1316 __ AddU1(hprof_basic_short);
1317 __ AddU2(0);
1318 __ AddStringId(LookupStringId(std::string(kClassOverheadName) + "2"));
1319 ++overhead_fields;
1320 }
1321 FALLTHROUGH_INTENDED;
1322
1323 case 1: {
1324 __ AddU1(hprof_basic_byte);
1325 __ AddU1(0);
1326 ++overhead_fields;
Andreas Gampe3a913092015-01-10 00:26:17 -08001327 break;
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001328 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001329 }
1330 }
Andreas Gampe3a2c55c2017-05-10 13:44:36 -07001331 DCHECK_EQ(java_heap_overhead_field_count, overhead_fields);
1332 }
1333
1334 // Helper lambda to emit the given static field. The second argument name_fn will be called to
1335 // generate the name to emit. This can be used to emit something else than the field's actual
1336 // name.
1337 auto static_field_writer = [&](ArtField& field, auto name_fn)
1338 REQUIRES_SHARED(Locks::mutator_lock_) {
1339 __ AddStringId(LookupStringId(name_fn(field)));
1340
1341 size_t size;
1342 HprofBasicType t = SignatureToBasicTypeAndSize(field.GetTypeDescriptor(), &size);
1343 __ AddU1(t);
1344 switch (t) {
1345 case hprof_basic_byte:
1346 __ AddU1(field.GetByte(klass));
1347 return;
1348 case hprof_basic_boolean:
1349 __ AddU1(field.GetBoolean(klass));
1350 return;
1351 case hprof_basic_char:
1352 __ AddU2(field.GetChar(klass));
1353 return;
1354 case hprof_basic_short:
1355 __ AddU2(field.GetShort(klass));
1356 return;
1357 case hprof_basic_float:
1358 case hprof_basic_int:
1359 case hprof_basic_object:
1360 __ AddU4(field.Get32(klass));
1361 return;
1362 case hprof_basic_double:
1363 case hprof_basic_long:
1364 __ AddU8(field.Get64(klass));
1365 return;
1366 }
1367 LOG(FATAL) << "Unexpected size " << size;
1368 UNREACHABLE();
1369 };
1370
1371 {
1372 auto class_instance_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
1373 return std::string("$class$") + field.GetName();
1374 };
1375 for (ArtField& class_instance_field : class_class->GetIFields()) {
1376 static_field_writer(class_instance_field, class_instance_field_name_fn);
1377 }
1378 for (ArtField& object_instance_field : class_class->GetSuperClass()->GetIFields()) {
1379 static_field_writer(object_instance_field, class_instance_field_name_fn);
1380 }
1381 }
1382
1383 {
1384 auto class_static_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) {
1385 return field.GetName();
1386 };
1387 for (ArtField& class_static_field : klass->GetSFields()) {
1388 static_field_writer(class_static_field, class_static_field_name_fn);
1389 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001390 }
1391
1392 // Instance fields for this class (no superclass fields)
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001393 int iFieldCount = klass->NumInstanceFields();
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001394 // add_internal_runtime_objects is only for classes that may retain objects live through means
1395 // other than fields. It is never the case for strings.
1396 const bool add_internal_runtime_objects = AddRuntimeInternalObjectsField(klass);
1397 if (klass->IsStringClass() || add_internal_runtime_objects) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001398 __ AddU2((uint16_t)iFieldCount + 1);
1399 } else {
1400 __ AddU2((uint16_t)iFieldCount);
1401 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001402 for (int i = 0; i < iFieldCount; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001403 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001404 __ AddStringId(LookupStringId(f->GetName()));
1405 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr);
1406 __ AddU1(t);
1407 }
Vladimir Marko595beb32017-02-06 14:11:54 +00001408 // Add native value character array for strings / byte array for compressed strings.
Jeff Hao848f70a2014-01-15 13:49:50 -08001409 if (klass->IsStringClass()) {
1410 __ AddStringId(LookupStringId("value"));
1411 __ AddU1(hprof_basic_object);
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001412 } else if (add_internal_runtime_objects) {
1413 __ AddStringId(LookupStringId("runtimeInternalObjects"));
1414 __ AddU1(hprof_basic_object);
1415 }
1416}
1417
1418void Hprof::DumpFakeObjectArray(mirror::Object* obj, const std::set<mirror::Object*>& elements) {
1419 __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
1420 __ AddObjectId(obj);
1421 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
1422 __ AddU4(elements.size());
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001423 __ AddClassId(LookupClassId(GetClassRoot<mirror::ObjectArray<mirror::Object>>().Ptr()));
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001424 for (mirror::Object* e : elements) {
1425 __ AddObjectId(e);
Jeff Hao848f70a2014-01-15 13:49:50 -08001426 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001427}
1428
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001429void Hprof::DumpHeapArray(mirror::Array* obj, mirror::Class* klass) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001430 uint32_t length = obj->GetLength();
1431
1432 if (obj->IsObjectArray()) {
1433 // obj is an object array.
1434 __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
1435
1436 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001437 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001438 __ AddU4(length);
1439 __ AddClassId(LookupClassId(klass));
1440
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001441 // Dump the elements, which are always objects or null.
Andreas Gampe3a913092015-01-10 00:26:17 -08001442 __ AddIdList(obj->AsObjectArray<mirror::Object>());
1443 } else {
1444 size_t size;
1445 HprofBasicType t = SignatureToBasicTypeAndSize(
1446 Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size);
1447
1448 // obj is a primitive array.
1449 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1450
1451 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001452 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001453 __ AddU4(length);
1454 __ AddU1(t);
1455
1456 // Dump the raw, packed element values.
1457 if (size == 1) {
1458 __ AddU1List(reinterpret_cast<const uint8_t*>(obj->GetRawData(sizeof(uint8_t), 0)), length);
1459 } else if (size == 2) {
1460 __ AddU2List(reinterpret_cast<const uint16_t*>(obj->GetRawData(sizeof(uint16_t), 0)), length);
1461 } else if (size == 4) {
1462 __ AddU4List(reinterpret_cast<const uint32_t*>(obj->GetRawData(sizeof(uint32_t), 0)), length);
1463 } else if (size == 8) {
1464 __ AddU8List(reinterpret_cast<const uint64_t*>(obj->GetRawData(sizeof(uint64_t), 0)), length);
1465 }
1466 }
1467}
1468
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001469void Hprof::DumpHeapInstanceObject(mirror::Object* obj,
1470 mirror::Class* klass,
1471 const std::set<mirror::Object*>& fake_roots) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001472 // obj is an instance object.
1473 __ AddU1(HPROF_INSTANCE_DUMP);
1474 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001475 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001476 __ AddClassId(LookupClassId(klass));
1477
1478 // Reserve some space for the length of the instance data, which we won't
1479 // know until we're done writing it.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001480 size_t size_patch_offset = output_->Length();
Andreas Gampe3a913092015-01-10 00:26:17 -08001481 __ AddU4(0x77777777);
1482
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001483 // What we will use for the string value if the object is a string.
1484 mirror::Object* string_value = nullptr;
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001485 mirror::Object* fake_object_array = nullptr;
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001486
1487 // Write the instance data; fields for this class, followed by super class fields, and so on.
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001488 do {
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001489 const size_t instance_fields = klass->NumInstanceFields();
1490 for (size_t i = 0; i < instance_fields; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001491 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001492 size_t size;
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001493 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001494 switch (t) {
1495 case hprof_basic_byte:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001496 __ AddU1(f->GetByte(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001497 break;
1498 case hprof_basic_boolean:
1499 __ AddU1(f->GetBoolean(obj));
1500 break;
1501 case hprof_basic_char:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001502 __ AddU2(f->GetChar(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001503 break;
1504 case hprof_basic_short:
1505 __ AddU2(f->GetShort(obj));
1506 break;
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001507 case hprof_basic_int:
Vladimir Marko595beb32017-02-06 14:11:54 +00001508 if (mirror::kUseStringCompression &&
1509 klass->IsStringClass() &&
1510 f->GetOffset().SizeValue() == mirror::String::CountOffset().SizeValue()) {
1511 // Store the string length instead of the raw count field with compression flag.
1512 __ AddU4(obj->AsString()->GetLength());
1513 break;
1514 }
1515 FALLTHROUGH_INTENDED;
1516 case hprof_basic_float:
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001517 case hprof_basic_object:
Andreas Gampe3a913092015-01-10 00:26:17 -08001518 __ AddU4(f->Get32(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001519 break;
1520 case hprof_basic_double:
1521 case hprof_basic_long:
Andreas Gampe3a913092015-01-10 00:26:17 -08001522 __ AddU8(f->Get64(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001523 break;
Andreas Gampe3a913092015-01-10 00:26:17 -08001524 }
1525 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001526 // Add value field for String if necessary.
1527 if (klass->IsStringClass()) {
1528 mirror::String* s = obj->AsString();
1529 if (s->GetLength() == 0) {
1530 // If string is empty, use an object-aligned address within the string for the value.
1531 string_value = reinterpret_cast<mirror::Object*>(
1532 reinterpret_cast<uintptr_t>(s) + kObjectAlignment);
1533 } else {
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001534 if (s->IsCompressed()) {
1535 string_value = reinterpret_cast<mirror::Object*>(s->GetValueCompressed());
1536 } else {
1537 string_value = reinterpret_cast<mirror::Object*>(s->GetValue());
1538 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001539 }
1540 __ AddObjectId(string_value);
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001541 } else if (AddRuntimeInternalObjectsField(klass)) {
1542 // We need an id that is guaranteed to not be used, use 1/2 of the object alignment.
1543 fake_object_array = reinterpret_cast<mirror::Object*>(
1544 reinterpret_cast<uintptr_t>(obj) + kObjectAlignment / 2);
1545 __ AddObjectId(fake_object_array);
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001546 }
Andreas Gampe98104992018-10-16 12:49:47 -07001547 klass = klass->GetSuperClass().Ptr();
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001548 } while (klass != nullptr);
Andreas Gampe3a913092015-01-10 00:26:17 -08001549
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001550 // Patch the instance field length.
1551 __ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4));
1552
Jeff Hao848f70a2014-01-15 13:49:50 -08001553 // Output native value character array for strings.
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001554 CHECK_EQ(obj->IsString(), string_value != nullptr);
1555 if (string_value != nullptr) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001556 mirror::String* s = obj->AsString();
Jeff Hao848f70a2014-01-15 13:49:50 -08001557 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001558 __ AddObjectId(string_value);
Man Cao8c2ff642015-05-27 17:25:30 -07001559 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Vladimir Marko595beb32017-02-06 14:11:54 +00001560 __ AddU4(s->GetLength());
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001561 if (s->IsCompressed()) {
Vladimir Marko595beb32017-02-06 14:11:54 +00001562 __ AddU1(hprof_basic_byte);
1563 __ AddU1List(s->GetValueCompressed(), s->GetLength());
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001564 } else {
Vladimir Marko595beb32017-02-06 14:11:54 +00001565 __ AddU1(hprof_basic_char);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001566 __ AddU2List(s->GetValue(), s->GetLength());
1567 }
Mathieu Chartier996f75e2017-03-31 11:18:41 -07001568 } else if (fake_object_array != nullptr) {
1569 DumpFakeObjectArray(fake_object_array, fake_roots);
Jeff Hao848f70a2014-01-15 13:49:50 -08001570 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001571}
1572
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001573void Hprof::VisitRoot(mirror::Object* obj, const RootInfo& info) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001574 static const HprofHeapTag xlate[] = {
1575 HPROF_ROOT_UNKNOWN,
1576 HPROF_ROOT_JNI_GLOBAL,
1577 HPROF_ROOT_JNI_LOCAL,
1578 HPROF_ROOT_JAVA_FRAME,
1579 HPROF_ROOT_NATIVE_STACK,
1580 HPROF_ROOT_STICKY_CLASS,
1581 HPROF_ROOT_THREAD_BLOCK,
1582 HPROF_ROOT_MONITOR_USED,
1583 HPROF_ROOT_THREAD_OBJECT,
1584 HPROF_ROOT_INTERNED_STRING,
1585 HPROF_ROOT_FINALIZING,
1586 HPROF_ROOT_DEBUGGER,
1587 HPROF_ROOT_REFERENCE_CLEANUP,
1588 HPROF_ROOT_VM_INTERNAL,
1589 HPROF_ROOT_JNI_MONITOR,
1590 };
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -08001591 CHECK_LT(info.GetType(), sizeof(xlate) / sizeof(HprofHeapTag));
Andreas Gampe3a913092015-01-10 00:26:17 -08001592 if (obj == nullptr) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001593 return;
1594 }
Yi Kong4b22b342018-08-02 14:43:21 -07001595 MarkRootObject(obj, nullptr, xlate[info.GetType()], info.GetThreadId());
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001596}
1597
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001598// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1599// sent directly to DDMS.
1600// If "fd" is >= 0, the output will be written to that file descriptor.
1601// Otherwise, "filename" is used to create an output file.
1602void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001603 CHECK(filename != nullptr);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001604 Thread* self = Thread::Current();
Mathieu Chartierecc82302017-02-16 10:20:12 -08001605 // Need to take a heap dump while GC isn't running. See the comment in Heap::VisitObjects().
1606 // Also we need the critical section to avoid visiting the same object twice. See b/34967844
1607 gc::ScopedGCCriticalSection gcs(self,
1608 gc::kGcCauseHprof,
1609 gc::kCollectorTypeHprof);
1610 ScopedSuspendAll ssa(__FUNCTION__, true /* long suspend */);
1611 Hprof hprof(filename, fd, direct_to_ddms);
1612 hprof.Dump();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001613}
1614
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001615} // namespace hprof
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001616} // namespace art