blob: 3d3ad593b326dd12f8605589ff5cd651a5e473ff [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>
35#include <time.h>
36#include <unistd.h>
37
38#include <set>
39
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040#include "android-base/stringprintf.h"
41
Mathieu Chartierc7853442015-03-27 14:35:38 -070042#include "art_field-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080043#include "base/logging.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010044#include "base/time_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080045#include "base/unix_file/fd_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080046#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080047#include "common_throws.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040048#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070049#include "dex_file-inl.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080050#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070051#include "gc/accounting/heap_bitmap.h"
Man Cao8c2ff642015-05-27 17:25:30 -070052#include "gc/allocation_record.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070053#include "gc/heap.h"
54#include "gc/space/space.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070055#include "globals.h"
Mathieu Chartierad466ad2015-01-08 16:28:08 -080056#include "jdwp/jdwp.h"
57#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058#include "mirror/class.h"
59#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060#include "mirror/object-inl.h"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070061#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070062#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070063#include "scoped_thread_state_change-inl.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070064#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040065
66namespace art {
67
68namespace hprof {
69
Mathieu Chartierad466ad2015-01-08 16:28:08 -080070static constexpr bool kDirectStream = true;
Jesse Wilson0c54ac12011-11-09 15:14:05 -050071
Andreas Gampe3a913092015-01-10 00:26:17 -080072static constexpr uint32_t kHprofTime = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -080073static constexpr uint32_t kHprofNullThread = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -070074
Andreas Gampe3a913092015-01-10 00:26:17 -080075static constexpr size_t kMaxObjectsPerSegment = 128;
76static constexpr size_t kMaxBytesPerSegment = 4096;
Elliott Hughes622a6982012-06-08 17:58:54 -070077
Andreas Gampe3a913092015-01-10 00:26:17 -080078// The static field-name for the synthetic object generated to account for class static overhead.
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -070079static constexpr const char* kClassOverheadName = "$classOverhead";
Elliott Hughes622a6982012-06-08 17:58:54 -070080
81enum HprofTag {
82 HPROF_TAG_STRING = 0x01,
83 HPROF_TAG_LOAD_CLASS = 0x02,
84 HPROF_TAG_UNLOAD_CLASS = 0x03,
85 HPROF_TAG_STACK_FRAME = 0x04,
86 HPROF_TAG_STACK_TRACE = 0x05,
87 HPROF_TAG_ALLOC_SITES = 0x06,
88 HPROF_TAG_HEAP_SUMMARY = 0x07,
89 HPROF_TAG_START_THREAD = 0x0A,
90 HPROF_TAG_END_THREAD = 0x0B,
91 HPROF_TAG_HEAP_DUMP = 0x0C,
92 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
93 HPROF_TAG_HEAP_DUMP_END = 0x2C,
94 HPROF_TAG_CPU_SAMPLES = 0x0D,
95 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
96};
97
98// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
99enum HprofHeapTag {
100 // Traditional.
101 HPROF_ROOT_UNKNOWN = 0xFF,
102 HPROF_ROOT_JNI_GLOBAL = 0x01,
103 HPROF_ROOT_JNI_LOCAL = 0x02,
104 HPROF_ROOT_JAVA_FRAME = 0x03,
105 HPROF_ROOT_NATIVE_STACK = 0x04,
106 HPROF_ROOT_STICKY_CLASS = 0x05,
107 HPROF_ROOT_THREAD_BLOCK = 0x06,
108 HPROF_ROOT_MONITOR_USED = 0x07,
109 HPROF_ROOT_THREAD_OBJECT = 0x08,
110 HPROF_CLASS_DUMP = 0x20,
111 HPROF_INSTANCE_DUMP = 0x21,
112 HPROF_OBJECT_ARRAY_DUMP = 0x22,
113 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
114
115 // Android.
116 HPROF_HEAP_DUMP_INFO = 0xfe,
117 HPROF_ROOT_INTERNED_STRING = 0x89,
118 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
119 HPROF_ROOT_DEBUGGER = 0x8b,
120 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
121 HPROF_ROOT_VM_INTERNAL = 0x8d,
122 HPROF_ROOT_JNI_MONITOR = 0x8e,
123 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700124 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700125};
126
127enum HprofHeapId {
128 HPROF_HEAP_DEFAULT = 0,
129 HPROF_HEAP_ZYGOTE = 'Z',
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700130 HPROF_HEAP_APP = 'A',
131 HPROF_HEAP_IMAGE = 'I',
Elliott Hughes622a6982012-06-08 17:58:54 -0700132};
133
134enum HprofBasicType {
135 hprof_basic_object = 2,
136 hprof_basic_boolean = 4,
137 hprof_basic_char = 5,
138 hprof_basic_float = 6,
139 hprof_basic_double = 7,
140 hprof_basic_byte = 8,
141 hprof_basic_short = 9,
142 hprof_basic_int = 10,
143 hprof_basic_long = 11,
144};
145
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146typedef uint32_t HprofStringId;
147typedef uint32_t HprofClassObjectId;
Man Cao8c2ff642015-05-27 17:25:30 -0700148typedef uint32_t HprofClassSerialNumber;
149typedef uint32_t HprofStackTraceSerialNumber;
150typedef uint32_t HprofStackFrameId;
151static constexpr HprofStackTraceSerialNumber kHprofNullStackTrace = 0;
Elliott Hughes622a6982012-06-08 17:58:54 -0700152
Andreas Gampe3a913092015-01-10 00:26:17 -0800153class EndianOutput {
Elliott Hughes622a6982012-06-08 17:58:54 -0700154 public:
Andreas Gampe3a913092015-01-10 00:26:17 -0800155 EndianOutput() : length_(0), sum_length_(0), max_length_(0), started_(false) {}
156 virtual ~EndianOutput() {}
157
158 void StartNewRecord(uint8_t tag, uint32_t time) {
159 if (length_ > 0) {
160 EndRecord();
161 }
162 DCHECK_EQ(length_, 0U);
163 AddU1(tag);
164 AddU4(time);
165 AddU4(0xdeaddead); // Length, replaced on flush.
166 started_ = true;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700167 }
168
Andreas Gampe3a913092015-01-10 00:26:17 -0800169 void EndRecord() {
170 // Replace length in header.
171 if (started_) {
172 UpdateU4(sizeof(uint8_t) + sizeof(uint32_t),
173 length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t));
174 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700175
Andreas Gampe3a913092015-01-10 00:26:17 -0800176 HandleEndRecord();
177
178 sum_length_ += length_;
179 max_length_ = std::max(max_length_, length_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700180 length_ = 0;
Andreas Gampe3a913092015-01-10 00:26:17 -0800181 started_ = false;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700182 }
183
Andreas Gampe3a913092015-01-10 00:26:17 -0800184 void AddU1(uint8_t value) {
185 AddU1List(&value, 1);
186 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800187 void AddU2(uint16_t value) {
188 AddU2List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700189 }
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800190 void AddU4(uint32_t value) {
191 AddU4List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700192 }
193
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800194 void AddU8(uint64_t value) {
195 AddU8List(&value, 1);
Elliott Hughes622a6982012-06-08 17:58:54 -0700196 }
197
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800198 void AddObjectId(const mirror::Object* value) {
199 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800200 }
201
Man Cao8c2ff642015-05-27 17:25:30 -0700202 void AddStackTraceSerialNumber(HprofStackTraceSerialNumber value) {
203 AddU4(value);
204 }
205
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206 // The ID for the synthetic object generated to account for class static overhead.
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800207 void AddClassStaticsId(const mirror::Class* value) {
208 AddU4(1 | PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800209 }
210
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800211 void AddJniGlobalRefId(jobject value) {
212 AddU4(PointerToLowMemUInt32(value));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 }
214
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800215 void AddClassId(HprofClassObjectId value) {
216 AddU4(value);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 }
218
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800219 void AddStringId(HprofStringId value) {
220 AddU4(value);
Elliott Hughes622a6982012-06-08 17:58:54 -0700221 }
222
Andreas Gampe3a913092015-01-10 00:26:17 -0800223 void AddU1List(const uint8_t* values, size_t count) {
224 HandleU1List(values, count);
225 length_ += count;
226 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700227 void AddU1AsU2List(const uint8_t* values, size_t count) {
228 HandleU1AsU2List(values, count);
229 // Array of char from compressed String (8-bit) is added as 16-bit blocks
230 int ceil_count_to_even = count + ((count & 1) ? 1 : 0);
231 length_ += ceil_count_to_even * sizeof(uint8_t);
232 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800233 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
307 void UpdateU4(size_t offset, uint32_t new_value) OVERRIDE {
308 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:
316 void HandleU1List(const uint8_t* values, size_t count) OVERRIDE {
317 DCHECK_EQ(length_, buffer_.size());
318 buffer_.insert(buffer_.end(), values, values + count);
319 }
320
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700321 void HandleU1AsU2List(const uint8_t* values, size_t count) OVERRIDE {
322 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
Andreas Gampe3a913092015-01-10 00:26:17 -0800334 void HandleU2List(const uint16_t* values, size_t count) OVERRIDE {
335 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
344 void HandleU4List(const uint32_t* values, size_t count) OVERRIDE {
345 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
356 void HandleU8List(const uint64_t* values, size_t count) OVERRIDE {
357 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
372 void HandleEndRecord() OVERRIDE {
373 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
392class FileEndianOutput FINAL : public EndianOutputBuffered {
393 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:
406 void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE {
407 if (!errors_) {
408 errors_ = !fp_->WriteFully(buffer, length);
409 }
410 }
411
412 private:
413 File* fp_;
414 bool errors_;
415};
416
417class NetStateEndianOutput FINAL : public EndianOutputBuffered {
418 public:
419 NetStateEndianOutput(JDWP::JdwpNetStateBase* net_state, size_t reserved_size)
420 : EndianOutputBuffered(reserved_size), net_state_(net_state) {
421 DCHECK(net_state != nullptr);
422 }
423 ~NetStateEndianOutput() {}
424
425 protected:
426 void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE {
427 std::vector<iovec> iov;
428 iov.push_back(iovec());
429 iov[0].iov_base = const_cast<void*>(reinterpret_cast<const void*>(buffer));
430 iov[0].iov_len = length;
431 net_state_->WriteBufferedPacketLocked(iov);
432 }
433
434 private:
435 JDWP::JdwpNetStateBase* net_state_;
436};
437
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700438#define __ output_->
Andreas Gampe3a913092015-01-10 00:26:17 -0800439
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700440class Hprof : public SingleRootVisitor {
Elliott Hughes622a6982012-06-08 17:58:54 -0700441 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700442 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
443 : filename_(output_filename),
444 fd_(fd),
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800445 direct_to_ddms_(direct_to_ddms) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700446 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500447 }
448
Andreas Gampe3a913092015-01-10 00:26:17 -0800449 void Dump()
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800450 REQUIRES(Locks::mutator_lock_)
451 REQUIRES(!Locks::heap_bitmap_lock_, !Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700452 {
453 MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_);
454 if (Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) {
455 PopulateAllocationTrackingTraces();
456 }
457 }
458
Andreas Gampe3a913092015-01-10 00:26:17 -0800459 // First pass to measure the size of the dump.
460 size_t overall_size;
461 size_t max_length;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800462 {
Andreas Gampe3a913092015-01-10 00:26:17 -0800463 EndianOutput count_output;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700464 output_ = &count_output;
465 ProcessHeap(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800466 overall_size = count_output.SumLength();
467 max_length = count_output.MaxLength();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700468 output_ = nullptr;
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800469 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700470
Andreas Gampe3a913092015-01-10 00:26:17 -0800471 bool okay;
472 if (direct_to_ddms_) {
473 if (kDirectStream) {
474 okay = DumpToDdmsDirect(overall_size, max_length, CHUNK_TYPE("HPDS"));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700475 } else {
Andreas Gampe3a913092015-01-10 00:26:17 -0800476 okay = DumpToDdmsBuffered(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700477 }
Andreas Gampe3a913092015-01-10 00:26:17 -0800478 } else {
479 okay = DumpToFile(overall_size, max_length);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700480 }
481
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700482 if (okay) {
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800483 const uint64_t duration = NanoTime() - start_ns_;
484 LOG(INFO) << "hprof: heap dump completed (" << PrettySize(RoundUp(overall_size, KB))
485 << ") in " << PrettyDuration(duration)
486 << " objects " << total_objects_
487 << " objects with stack traces " << total_objects_with_stack_trace_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700488 }
489 }
490
491 private:
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800492 static void VisitObjectCallback(mirror::Object* obj, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700493 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad466ad2015-01-08 16:28:08 -0800494 DCHECK(obj != nullptr);
495 DCHECK(arg != nullptr);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700496 reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700497 }
498
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700499 void DumpHeapObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700500 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700501
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700502 void DumpHeapClass(mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700503 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700504
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700505 void DumpHeapArray(mirror::Array* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700506 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800507
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700508 void DumpHeapInstanceObject(mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700509 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe3a913092015-01-10 00:26:17 -0800510
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700511 void ProcessHeap(bool header_first)
Mathieu Chartier90443472015-07-16 20:32:27 -0700512 REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800513 // Reset current heap and object count.
514 current_heap_ = HPROF_HEAP_DEFAULT;
515 objects_in_segment_ = 0;
516
517 if (header_first) {
Man Cao8c2ff642015-05-27 17:25:30 -0700518 ProcessHeader(true);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700519 ProcessBody();
Andreas Gampe3a913092015-01-10 00:26:17 -0800520 } else {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700521 ProcessBody();
Man Cao8c2ff642015-05-27 17:25:30 -0700522 ProcessHeader(false);
Andreas Gampe3a913092015-01-10 00:26:17 -0800523 }
524 }
525
Mathieu Chartier90443472015-07-16 20:32:27 -0700526 void ProcessBody() REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700527 Runtime* const runtime = Runtime::Current();
Andreas Gampe3a913092015-01-10 00:26:17 -0800528 // Walk the roots and the heap.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700529 output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
Andreas Gampe3a913092015-01-10 00:26:17 -0800530
Richard Uhler4b767552016-04-26 13:28:59 -0700531 simple_roots_.clear();
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700532 runtime->VisitRoots(this);
533 runtime->VisitImageRoots(this);
534 runtime->GetHeap()->VisitObjectsPaused(VisitObjectCallback, this);
Andreas Gampe3a913092015-01-10 00:26:17 -0800535
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)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700608 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));
722 auto class_result = classes_.find(method->GetDeclaringClass());
723 CHECK(class_result != classes_.end());
724 __ AddU4(class_result->second);
725 __ AddU4(frame->ComputeLineNumber());
726 }
727
728 // Then write the trace itself
729 output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
730 // STACK TRACE format:
731 // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number.
732 // U4: thread serial number. We use Thread::GetTid().
733 // U4: number of frames
734 // [ID]*: series of stack frame ID's
735 __ AddStackTraceSerialNumber(trace_sn);
736 __ AddU4(trace->GetTid());
737 __ AddU4(depth);
738 for (size_t i = 0; i < depth; ++i) {
739 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
740 auto frame_result = frames_.find(frame);
741 CHECK(frame_result != frames_.end());
742 __ AddU4(frame_result->second);
743 }
744 }
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) {
765 out_fd = dup(fd_);
766 if (out_fd < 0) {
767 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
768 return false;
769 }
770 } else {
771 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
772 if (out_fd < 0) {
773 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
774 strerror(errno));
775 return false;
776 }
777 }
778
779 std::unique_ptr<File> file(new File(out_fd, filename_, true));
780 bool okay;
781 {
782 FileEndianOutput file_output(file.get(), max_length);
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_);
814 JDWP::JdwpState* state = Dbg::GetJdwpState();
815 CHECK(state != nullptr);
816 JDWP::JdwpNetStateBase* net_state = state->netState;
817 CHECK(net_state != nullptr);
818
819 // Hold the socket lock for the whole time since we want this to be atomic.
820 MutexLock mu(Thread::Current(), *net_state->GetSocketLock());
821
822 // Prepare the Ddms chunk.
823 constexpr size_t kChunkHeaderSize = kJDWPHeaderLen + 8;
824 uint8_t chunk_header[kChunkHeaderSize] = { 0 };
825 state->SetupChunkHeader(chunk_type, overall_size, kChunkHeaderSize, chunk_header);
826
827 // Prepare the output and send the chunk header.
828 NetStateEndianOutput net_output(net_state, max_length);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700829 output_ = &net_output;
Andreas Gampe3a913092015-01-10 00:26:17 -0800830 net_output.AddU1List(chunk_header, kChunkHeaderSize);
831
832 // Write the dump.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700833 ProcessHeap(true);
Andreas Gampe3a913092015-01-10 00:26:17 -0800834
Andreas Gampec515f212015-08-28 18:15:27 -0700835 // Check for expected size. See DumpToFile for comment.
836 DCHECK_LE(net_output.SumLength(), overall_size + kChunkHeaderSize);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700837 output_ = nullptr;
Andreas Gampe3a913092015-01-10 00:26:17 -0800838
839 return true;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700840 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500841
Man Cao8c2ff642015-05-27 17:25:30 -0700842 void PopulateAllocationTrackingTraces()
Mathieu Chartier90443472015-07-16 20:32:27 -0700843 REQUIRES(Locks::mutator_lock_, Locks::alloc_tracker_lock_) {
Man Cao8c2ff642015-05-27 17:25:30 -0700844 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
845 CHECK(records != nullptr);
846 HprofStackTraceSerialNumber next_trace_sn = kHprofNullStackTrace + 1;
847 HprofStackFrameId next_frame_id = 0;
Man Cao42c3c332015-06-23 16:38:25 -0700848 size_t count = 0;
Man Cao8c2ff642015-05-27 17:25:30 -0700849
850 for (auto it = records->Begin(), end = records->End(); it != end; ++it) {
851 const mirror::Object* obj = it->first.Read();
Man Cao42c3c332015-06-23 16:38:25 -0700852 if (obj == nullptr) {
853 continue;
854 }
855 ++count;
Mathieu Chartier458b1052016-03-29 14:02:55 -0700856 const gc::AllocRecordStackTrace* trace = it->second.GetStackTrace();
Man Cao8c2ff642015-05-27 17:25:30 -0700857
858 // Copy the pair into a real hash map to speed up look up.
859 auto records_result = allocation_records_.emplace(obj, trace);
860 // The insertion should always succeed, i.e. no duplicate object pointers in "records"
861 CHECK(records_result.second);
862
863 // Generate serial numbers for traces, and IDs for frames.
864 auto traces_result = traces_.find(trace);
865 if (traces_result == traces_.end()) {
866 traces_.emplace(trace, next_trace_sn++);
867 // only check frames if the trace is newly discovered
868 for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) {
869 const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
870 auto frames_result = frames_.find(frame);
871 if (frames_result == frames_.end()) {
872 frames_.emplace(frame, next_frame_id++);
873 }
874 }
875 }
876 }
877 CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1);
878 CHECK_EQ(frames_.size(), next_frame_id);
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800879 total_objects_with_stack_trace_ = count;
Man Cao8c2ff642015-05-27 17:25:30 -0700880 }
881
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700882 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
883 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
884 // only be used for debug messages.
885 std::string filename_;
886 int fd_;
887 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500888
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800889 uint64_t start_ns_ = NanoTime();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700890
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800891 EndianOutput* output_ = nullptr;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700892
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800893 HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT; // Which heap we're currently dumping.
894 size_t objects_in_segment_ = 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700895
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800896 size_t total_objects_ = 0u;
897 size_t total_objects_with_stack_trace_ = 0u;
898
899 HprofStringId next_string_id_ = 0x400000;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800900 SafeMap<std::string, HprofStringId> strings_;
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -0800901 HprofClassSerialNumber next_class_serial_number_ = 1;
Man Cao8c2ff642015-05-27 17:25:30 -0700902 SafeMap<mirror::Class*, HprofClassSerialNumber> classes_;
903
904 std::unordered_map<const gc::AllocRecordStackTrace*, HprofStackTraceSerialNumber,
905 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTrace>,
906 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTrace>> traces_;
907 std::unordered_map<const gc::AllocRecordStackTraceElement*, HprofStackFrameId,
908 gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>,
909 gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>> frames_;
910 std::unordered_map<const mirror::Object*, const gc::AllocRecordStackTrace*> allocation_records_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700911
Richard Uhler4b767552016-04-26 13:28:59 -0700912 // Set used to keep track of what simple root records we have already
913 // emitted, to avoid emitting duplicate entries. The simple root records are
914 // those that contain no other information than the root type and the object
915 // id. A pair of root type and object id is packed into a uint64_t, with
916 // the root type in the upper 32 bits and the object id in the lower 32
917 // bits.
918 std::unordered_set<uint64_t> simple_roots_;
919
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700920 friend class GcRootVisitor;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700921 DISALLOW_COPY_AND_ASSIGN(Hprof);
922};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500923
Andreas Gampe3a913092015-01-10 00:26:17 -0800924static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* size_out) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500925 char c = sig[0];
926 HprofBasicType ret;
927 size_t size;
928
929 switch (c) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800930 case '[':
931 case 'L':
932 ret = hprof_basic_object;
933 size = 4;
934 break;
935 case 'Z':
936 ret = hprof_basic_boolean;
937 size = 1;
938 break;
939 case 'C':
940 ret = hprof_basic_char;
941 size = 2;
942 break;
943 case 'F':
944 ret = hprof_basic_float;
945 size = 4;
946 break;
947 case 'D':
948 ret = hprof_basic_double;
949 size = 8;
950 break;
951 case 'B':
952 ret = hprof_basic_byte;
953 size = 1;
954 break;
955 case 'S':
956 ret = hprof_basic_short;
957 size = 2;
958 break;
959 case 'I':
960 ret = hprof_basic_int;
961 size = 4;
962 break;
963 case 'J':
964 ret = hprof_basic_long;
965 size = 8;
966 break;
967 default:
968 LOG(FATAL) << "UNREACHABLE";
969 UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500970 }
971
Andreas Gampe3a913092015-01-10 00:26:17 -0800972 if (size_out != nullptr) {
973 *size_out = size;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500974 }
975
976 return ret;
977}
978
979// Always called when marking objects, but only does
980// something when ctx->gc_scan_state_ is non-zero, which is usually
981// only true when marking the root set or unreachable
982// objects. Used to add rootset references to obj.
Andreas Gampe3a913092015-01-10 00:26:17 -0800983void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700984 uint32_t thread_serial) {
Andreas Gampe3a913092015-01-10 00:26:17 -0800985 if (heap_tag == 0) {
986 return;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500987 }
988
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700989 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500990
Andreas Gampe3a913092015-01-10 00:26:17 -0800991 switch (heap_tag) {
992 // ID: object ID
993 case HPROF_ROOT_UNKNOWN:
994 case HPROF_ROOT_STICKY_CLASS:
995 case HPROF_ROOT_MONITOR_USED:
996 case HPROF_ROOT_INTERNED_STRING:
997 case HPROF_ROOT_DEBUGGER:
Richard Uhler4b767552016-04-26 13:28:59 -0700998 case HPROF_ROOT_VM_INTERNAL: {
999 uint64_t key = (static_cast<uint64_t>(heap_tag) << 32) | PointerToLowMemUInt32(obj);
1000 if (simple_roots_.insert(key).second) {
1001 __ AddU1(heap_tag);
1002 __ AddObjectId(obj);
1003 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001004 break;
Richard Uhler4b767552016-04-26 13:28:59 -07001005 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001006
Andreas Gampe3a913092015-01-10 00:26:17 -08001007 // ID: object ID
1008 // ID: JNI global ref ID
1009 case HPROF_ROOT_JNI_GLOBAL:
1010 __ AddU1(heap_tag);
1011 __ AddObjectId(obj);
1012 __ AddJniGlobalRefId(jni_obj);
1013 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001014
Andreas Gampe3a913092015-01-10 00:26:17 -08001015 // ID: object ID
1016 // U4: thread serial number
1017 // U4: frame number in stack trace (-1 for empty)
1018 case HPROF_ROOT_JNI_LOCAL:
1019 case HPROF_ROOT_JNI_MONITOR:
1020 case HPROF_ROOT_JAVA_FRAME:
1021 __ AddU1(heap_tag);
1022 __ AddObjectId(obj);
1023 __ AddU4(thread_serial);
1024 __ AddU4((uint32_t)-1);
1025 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001026
Andreas Gampe3a913092015-01-10 00:26:17 -08001027 // ID: object ID
1028 // U4: thread serial number
1029 case HPROF_ROOT_NATIVE_STACK:
1030 case HPROF_ROOT_THREAD_BLOCK:
1031 __ AddU1(heap_tag);
1032 __ AddObjectId(obj);
1033 __ AddU4(thread_serial);
1034 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001035
Andreas Gampe3a913092015-01-10 00:26:17 -08001036 // ID: thread object ID
1037 // U4: thread serial number
1038 // U4: stack trace serial number
1039 case HPROF_ROOT_THREAD_OBJECT:
1040 __ AddU1(heap_tag);
1041 __ AddObjectId(obj);
1042 __ AddU4(thread_serial);
1043 __ AddU4((uint32_t)-1); // xxx
1044 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001045
Andreas Gampe3a913092015-01-10 00:26:17 -08001046 case HPROF_CLASS_DUMP:
1047 case HPROF_INSTANCE_DUMP:
1048 case HPROF_OBJECT_ARRAY_DUMP:
1049 case HPROF_PRIMITIVE_ARRAY_DUMP:
1050 case HPROF_HEAP_DUMP_INFO:
1051 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
1052 // Ignored.
1053 break;
Elliott Hughes73e66f72012-05-09 09:34:45 -07001054
Andreas Gampe3a913092015-01-10 00:26:17 -08001055 case HPROF_ROOT_FINALIZING:
1056 case HPROF_ROOT_REFERENCE_CLEANUP:
1057 case HPROF_UNREACHABLE:
1058 LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag);
1059 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001060 }
1061
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001062 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001063}
1064
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001065// Use for visiting the GcRoots held live by ArtFields, ArtMethods, and ClassLoaders.
1066class GcRootVisitor {
1067 public:
1068 explicit GcRootVisitor(Hprof* hprof) : hprof_(hprof) {}
1069
1070 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
1071 MemberOffset offset ATTRIBUTE_UNUSED,
1072 bool is_static ATTRIBUTE_UNUSED) const {}
1073
1074 // Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be
1075 // running during the hprof dumping process.
1076 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001077 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001078 if (!root->IsNull()) {
1079 VisitRoot(root);
1080 }
1081 }
1082
1083 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001084 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001085 mirror::Object* obj = root->AsMirrorPtr();
1086 // The two cases are either classes or dex cache arrays. If it is a dex cache array, then use
1087 // VM internal. Otherwise the object is a declaring class of an ArtField or ArtMethod or a
1088 // class from a ClassLoader.
1089 hprof_->VisitRoot(obj, RootInfo(obj->IsClass() ? kRootStickyClass : kRootVMInternal));
1090 }
1091
1092
1093 private:
1094 Hprof* const hprof_;
1095};
1096
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001097void Hprof::DumpHeapObject(mirror::Object* obj) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001098 // Ignore classes that are retired.
1099 if (obj->IsClass() && obj->AsClass()->IsRetired()) {
1100 return;
1101 }
1102
Mathieu Chartierc9cd7ac2016-01-20 14:48:36 -08001103 ++total_objects_;
1104
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001105 GcRootVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001106 obj->VisitReferences(visitor, VoidFunctor());
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001107
Mathieu Chartiere7158112015-06-03 13:32:15 -07001108 gc::Heap* const heap = Runtime::Current()->GetHeap();
1109 const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001110 HprofHeapId heap_type = HPROF_HEAP_APP;
1111 if (space != nullptr) {
1112 if (space->IsZygoteSpace()) {
1113 heap_type = HPROF_HEAP_ZYGOTE;
1114 } else if (space->IsImageSpace()) {
1115 heap_type = HPROF_HEAP_IMAGE;
1116 }
Mathieu Chartiere7158112015-06-03 13:32:15 -07001117 } else {
1118 const auto* los = heap->GetLargeObjectsSpace();
1119 if (los->Contains(obj) && los->IsZygoteLargeObject(Thread::Current(), obj)) {
1120 heap_type = HPROF_HEAP_ZYGOTE;
1121 }
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001122 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001123 CheckHeapSegmentConstraints();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001124
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001125 if (heap_type != current_heap_) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001126 HprofStringId nameId;
1127
1128 // This object is in a different heap than the current one.
1129 // Emit a HEAP_DUMP_INFO tag to change heaps.
Andreas Gampe3a913092015-01-10 00:26:17 -08001130 __ AddU1(HPROF_HEAP_DUMP_INFO);
1131 __ AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001132 switch (heap_type) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001133 case HPROF_HEAP_APP:
1134 nameId = LookupStringId("app");
1135 break;
1136 case HPROF_HEAP_ZYGOTE:
1137 nameId = LookupStringId("zygote");
1138 break;
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001139 case HPROF_HEAP_IMAGE:
1140 nameId = LookupStringId("image");
1141 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001142 default:
1143 // Internal error
1144 LOG(ERROR) << "Unexpected desiredHeap";
1145 nameId = LookupStringId("<ILLEGAL>");
1146 break;
1147 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001148 __ AddStringId(nameId);
Mathieu Chartierae1ad002014-07-18 17:58:22 -07001149 current_heap_ = heap_type;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001150 }
1151
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001152 mirror::Class* c = obj->GetClass();
Andreas Gampe3a913092015-01-10 00:26:17 -08001153 if (c == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001154 // This object will bother HprofReader, because it has a null
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001155 // class, so just don't dump it. It could be
1156 // gDvm.unlinkedJavaLangClass or it could be an object just
1157 // allocated which hasn't been initialized yet.
1158 } else {
1159 if (obj->IsClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001160 DumpHeapClass(obj->AsClass());
Elliott Hughese84278b2012-03-22 10:06:53 -07001161 } else if (c->IsArrayClass()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001162 DumpHeapArray(obj->AsArray(), c);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001163 } else {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001164 DumpHeapInstanceObject(obj, c);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001165 }
1166 }
1167
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001168 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001169}
1170
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001171void Hprof::DumpHeapClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +00001172 if (!klass->IsResolved()) {
Mathieu Chartier12393f32016-10-20 17:11:23 -07001173 // Class is allocated but not yet resolved: we cannot access its fields or super class.
Sebastien Hertzca068b22015-04-07 10:28:53 +02001174 return;
1175 }
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001176 const size_t num_static_fields = klass->NumStaticFields();
1177 // Total class size including embedded IMT, embedded vtable, and static fields.
1178 const size_t class_size = klass->GetClassSize();
1179 // Class size excluding static fields (relies on reference fields being the first static fields).
1180 const size_t class_size_without_overhead = sizeof(mirror::Class);
1181 CHECK_LE(class_size_without_overhead, class_size);
1182 const size_t overhead_size = class_size - class_size_without_overhead;
1183
1184 if (overhead_size != 0) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001185 // Create a byte array to reflect the allocation of the
1186 // StaticField array at the end of this class.
1187 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1188 __ AddClassStaticsId(klass);
Man Cao8c2ff642015-05-27 17:25:30 -07001189 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001190 __ AddU4(overhead_size);
Andreas Gampe3a913092015-01-10 00:26:17 -08001191 __ AddU1(hprof_basic_byte);
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001192 for (size_t i = 0; i < overhead_size; ++i) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001193 __ AddU1(0);
1194 }
1195 }
1196
1197 __ AddU1(HPROF_CLASS_DUMP);
1198 __ AddClassId(LookupClassId(klass));
Man Cao8c2ff642015-05-27 17:25:30 -07001199 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
Andreas Gampe3a913092015-01-10 00:26:17 -08001200 __ AddClassId(LookupClassId(klass->GetSuperClass()));
1201 __ AddObjectId(klass->GetClassLoader());
1202 __ AddObjectId(nullptr); // no signer
1203 __ AddObjectId(nullptr); // no prot domain
1204 __ AddObjectId(nullptr); // reserved
1205 __ AddObjectId(nullptr); // reserved
1206 if (klass->IsClassClass()) {
1207 // ClassObjects have their static fields appended, so aren't all the same size.
1208 // But they're at least this size.
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001209 __ AddU4(class_size_without_overhead); // instance size
Jeff Hao0ce43532015-05-12 18:58:32 -07001210 } else if (klass->IsStringClass()) {
1211 // Strings are variable length with character data at the end like arrays.
1212 // This outputs the size of an empty string.
1213 __ AddU4(sizeof(mirror::String));
1214 } else if (klass->IsArrayClass() || klass->IsPrimitive()) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001215 __ AddU4(0);
1216 } else {
1217 __ AddU4(klass->GetObjectSize()); // instance size
1218 }
1219
1220 __ AddU2(0); // empty const pool
1221
1222 // Static fields
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001223 if (overhead_size == 0) {
1224 __ AddU2(static_cast<uint16_t>(0));
Andreas Gampe3a913092015-01-10 00:26:17 -08001225 } else {
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001226 __ AddU2(static_cast<uint16_t>(num_static_fields + 1));
1227 __ AddStringId(LookupStringId(kClassOverheadName));
Andreas Gampe3a913092015-01-10 00:26:17 -08001228 __ AddU1(hprof_basic_object);
1229 __ AddClassStaticsId(klass);
1230
Mathieu Chartiera6d3a7e2015-06-03 16:51:09 -07001231 for (size_t i = 0; i < num_static_fields; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001232 ArtField* f = klass->GetStaticField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001233
1234 size_t size;
1235 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
1236 __ AddStringId(LookupStringId(f->GetName()));
1237 __ AddU1(t);
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001238 switch (t) {
1239 case hprof_basic_byte:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001240 __ AddU1(f->GetByte(klass));
Andreas Gampe3a913092015-01-10 00:26:17 -08001241 break;
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001242 case hprof_basic_boolean:
1243 __ AddU1(f->GetBoolean(klass));
1244 break;
1245 case hprof_basic_char:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001246 __ AddU2(f->GetChar(klass));
Andreas Gampe3a913092015-01-10 00:26:17 -08001247 break;
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001248 case hprof_basic_short:
1249 __ AddU2(f->GetShort(klass));
1250 break;
1251 case hprof_basic_float:
1252 case hprof_basic_int:
1253 case hprof_basic_object:
Andreas Gampe3a913092015-01-10 00:26:17 -08001254 __ AddU4(f->Get32(klass));
1255 break;
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001256 case hprof_basic_double:
1257 case hprof_basic_long:
Andreas Gampe3a913092015-01-10 00:26:17 -08001258 __ AddU8(f->Get64(klass));
1259 break;
1260 default:
1261 LOG(FATAL) << "Unexpected size " << size;
1262 UNREACHABLE();
1263 }
1264 }
1265 }
1266
1267 // Instance fields for this class (no superclass fields)
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001268 int iFieldCount = klass->NumInstanceFields();
Jeff Hao848f70a2014-01-15 13:49:50 -08001269 if (klass->IsStringClass()) {
1270 __ AddU2((uint16_t)iFieldCount + 1);
1271 } else {
1272 __ AddU2((uint16_t)iFieldCount);
1273 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001274 for (int i = 0; i < iFieldCount; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001275 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001276 __ AddStringId(LookupStringId(f->GetName()));
1277 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr);
1278 __ AddU1(t);
1279 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001280 // Add native value character array for strings.
1281 if (klass->IsStringClass()) {
1282 __ AddStringId(LookupStringId("value"));
1283 __ AddU1(hprof_basic_object);
1284 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001285}
1286
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001287void Hprof::DumpHeapArray(mirror::Array* obj, mirror::Class* klass) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001288 uint32_t length = obj->GetLength();
1289
1290 if (obj->IsObjectArray()) {
1291 // obj is an object array.
1292 __ AddU1(HPROF_OBJECT_ARRAY_DUMP);
1293
1294 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001295 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001296 __ AddU4(length);
1297 __ AddClassId(LookupClassId(klass));
1298
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001299 // Dump the elements, which are always objects or null.
Andreas Gampe3a913092015-01-10 00:26:17 -08001300 __ AddIdList(obj->AsObjectArray<mirror::Object>());
1301 } else {
1302 size_t size;
1303 HprofBasicType t = SignatureToBasicTypeAndSize(
1304 Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size);
1305
1306 // obj is a primitive array.
1307 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
1308
1309 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001310 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001311 __ AddU4(length);
1312 __ AddU1(t);
1313
1314 // Dump the raw, packed element values.
1315 if (size == 1) {
1316 __ AddU1List(reinterpret_cast<const uint8_t*>(obj->GetRawData(sizeof(uint8_t), 0)), length);
1317 } else if (size == 2) {
1318 __ AddU2List(reinterpret_cast<const uint16_t*>(obj->GetRawData(sizeof(uint16_t), 0)), length);
1319 } else if (size == 4) {
1320 __ AddU4List(reinterpret_cast<const uint32_t*>(obj->GetRawData(sizeof(uint32_t), 0)), length);
1321 } else if (size == 8) {
1322 __ AddU8List(reinterpret_cast<const uint64_t*>(obj->GetRawData(sizeof(uint64_t), 0)), length);
1323 }
1324 }
1325}
1326
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001327void Hprof::DumpHeapInstanceObject(mirror::Object* obj, mirror::Class* klass) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001328 // obj is an instance object.
1329 __ AddU1(HPROF_INSTANCE_DUMP);
1330 __ AddObjectId(obj);
Man Cao8c2ff642015-05-27 17:25:30 -07001331 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
Andreas Gampe3a913092015-01-10 00:26:17 -08001332 __ AddClassId(LookupClassId(klass));
1333
1334 // Reserve some space for the length of the instance data, which we won't
1335 // know until we're done writing it.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001336 size_t size_patch_offset = output_->Length();
Andreas Gampe3a913092015-01-10 00:26:17 -08001337 __ AddU4(0x77777777);
1338
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001339 // What we will use for the string value if the object is a string.
1340 mirror::Object* string_value = nullptr;
1341
1342 // Write the instance data; fields for this class, followed by super class fields, and so on.
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001343 do {
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001344 const size_t instance_fields = klass->NumInstanceFields();
1345 for (size_t i = 0; i < instance_fields; ++i) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001346 ArtField* f = klass->GetInstanceField(i);
Andreas Gampe3a913092015-01-10 00:26:17 -08001347 size_t size;
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001348 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001349 switch (t) {
1350 case hprof_basic_byte:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001351 __ AddU1(f->GetByte(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001352 break;
1353 case hprof_basic_boolean:
1354 __ AddU1(f->GetBoolean(obj));
1355 break;
1356 case hprof_basic_char:
Mathieu Chartierff38c042015-03-06 11:33:36 -08001357 __ AddU2(f->GetChar(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001358 break;
1359 case hprof_basic_short:
1360 __ AddU2(f->GetShort(obj));
1361 break;
1362 case hprof_basic_float:
1363 case hprof_basic_int:
1364 case hprof_basic_object:
Andreas Gampe3a913092015-01-10 00:26:17 -08001365 __ AddU4(f->Get32(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001366 break;
1367 case hprof_basic_double:
1368 case hprof_basic_long:
Andreas Gampe3a913092015-01-10 00:26:17 -08001369 __ AddU8(f->Get64(obj));
Mathieu Chartier15f345c2015-03-06 12:45:44 -08001370 break;
Andreas Gampe3a913092015-01-10 00:26:17 -08001371 }
1372 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001373 // Add value field for String if necessary.
1374 if (klass->IsStringClass()) {
1375 mirror::String* s = obj->AsString();
1376 if (s->GetLength() == 0) {
1377 // If string is empty, use an object-aligned address within the string for the value.
1378 string_value = reinterpret_cast<mirror::Object*>(
1379 reinterpret_cast<uintptr_t>(s) + kObjectAlignment);
1380 } else {
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001381 if (s->IsCompressed()) {
1382 string_value = reinterpret_cast<mirror::Object*>(s->GetValueCompressed());
1383 } else {
1384 string_value = reinterpret_cast<mirror::Object*>(s->GetValue());
1385 }
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001386 }
1387 __ AddObjectId(string_value);
1388 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001389
1390 klass = klass->GetSuperClass();
Mathieu Chartier7c1f53e2015-06-03 10:51:13 -07001391 } while (klass != nullptr);
Andreas Gampe3a913092015-01-10 00:26:17 -08001392
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001393 // Patch the instance field length.
1394 __ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4));
1395
Jeff Hao848f70a2014-01-15 13:49:50 -08001396 // Output native value character array for strings.
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001397 CHECK_EQ(obj->IsString(), string_value != nullptr);
1398 if (string_value != nullptr) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001399 mirror::String* s = obj->AsString();
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001400 // Compressed string's (8-bit) length is ceil(length/2) in 16-bit blocks
1401 int length_in_16_bit = (s->IsCompressed()) ? ((s->GetLength() + 1) / 2) : s->GetLength();
Jeff Hao848f70a2014-01-15 13:49:50 -08001402 __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Mathieu Chartier07d7eab2015-06-23 15:45:15 -07001403 __ AddObjectId(string_value);
Man Cao8c2ff642015-05-27 17:25:30 -07001404 __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001405 __ AddU4(length_in_16_bit);
Jeff Hao848f70a2014-01-15 13:49:50 -08001406 __ AddU1(hprof_basic_char);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001407 if (s->IsCompressed()) {
1408 __ AddU1AsU2List(s->GetValueCompressed(), s->GetLength());
1409 } else {
1410 __ AddU2List(s->GetValue(), s->GetLength());
1411 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001412 }
Andreas Gampe3a913092015-01-10 00:26:17 -08001413}
1414
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001415void Hprof::VisitRoot(mirror::Object* obj, const RootInfo& info) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001416 static const HprofHeapTag xlate[] = {
1417 HPROF_ROOT_UNKNOWN,
1418 HPROF_ROOT_JNI_GLOBAL,
1419 HPROF_ROOT_JNI_LOCAL,
1420 HPROF_ROOT_JAVA_FRAME,
1421 HPROF_ROOT_NATIVE_STACK,
1422 HPROF_ROOT_STICKY_CLASS,
1423 HPROF_ROOT_THREAD_BLOCK,
1424 HPROF_ROOT_MONITOR_USED,
1425 HPROF_ROOT_THREAD_OBJECT,
1426 HPROF_ROOT_INTERNED_STRING,
1427 HPROF_ROOT_FINALIZING,
1428 HPROF_ROOT_DEBUGGER,
1429 HPROF_ROOT_REFERENCE_CLEANUP,
1430 HPROF_ROOT_VM_INTERNAL,
1431 HPROF_ROOT_JNI_MONITOR,
1432 };
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -08001433 CHECK_LT(info.GetType(), sizeof(xlate) / sizeof(HprofHeapTag));
Andreas Gampe3a913092015-01-10 00:26:17 -08001434 if (obj == nullptr) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001435 return;
1436 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001437 MarkRootObject(obj, 0, xlate[info.GetType()], info.GetThreadId());
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001438}
1439
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001440// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1441// sent directly to DDMS.
1442// If "fd" is >= 0, the output will be written to that file descriptor.
1443// Otherwise, "filename" is used to create an output file.
1444void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
Andreas Gampe3a913092015-01-10 00:26:17 -08001445 CHECK(filename != nullptr);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001446
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001447 Thread* self = Thread::Current();
1448 gc::Heap* heap = Runtime::Current()->GetHeap();
1449 if (heap->IsGcConcurrentAndMoving()) {
1450 // Need to take a heap dump while GC isn't running. See the
1451 // comment in Heap::VisitObjects().
1452 heap->IncrementDisableMovingGC(self);
1453 }
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001454 {
1455 ScopedSuspendAll ssa(__FUNCTION__, true /* long suspend */);
1456 Hprof hprof(filename, fd, direct_to_ddms);
1457 hprof.Dump();
1458 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08001459 if (heap->IsGcConcurrentAndMoving()) {
1460 heap->DecrementDisableMovingGC(self);
1461 }
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001462}
1463
Mathieu Chartierad466ad2015-01-08 16:28:08 -08001464} // namespace hprof
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001465} // namespace art