blob: 24d403d63401e5c233305511a710216da6296b48 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080040#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080041#include "base/stringprintf.h"
Elliott Hughes76160052012-12-12 16:31:20 -080042#include "base/unix_file/fd_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080044#include "common_throws.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040045#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070046#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070047#include "gc/accounting/heap_bitmap.h"
48#include "gc/heap.h"
49#include "gc/space/space.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070050#include "globals.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070051#include "mirror/art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "mirror/class.h"
53#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080055#include "object_utils.h"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070056#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070059#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040060
61namespace art {
62
63namespace hprof {
64
Elliott Hughes622a6982012-06-08 17:58:54 -070065#define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
Jesse Wilson0c54ac12011-11-09 15:14:05 -050066
Elliott Hughes622a6982012-06-08 17:58:54 -070067#define HPROF_TIME 0
68#define HPROF_NULL_STACK_TRACE 0
69#define HPROF_NULL_THREAD 0
70
71#define U2_TO_BUF_BE(buf, offset, value) \
72 do { \
73 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070074 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070075 uint16_t value_ = (uint16_t)(value); \
76 buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \
77 buf_[offset_ + 1] = (unsigned char)(value_ ); \
78 } while (0)
79
80#define U4_TO_BUF_BE(buf, offset, value) \
81 do { \
82 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070083 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070084 uint32_t value_ = (uint32_t)(value); \
85 buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \
86 buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \
87 buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \
88 buf_[offset_ + 3] = (unsigned char)(value_ ); \
89 } while (0)
90
91#define U8_TO_BUF_BE(buf, offset, value) \
92 do { \
93 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070094 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070095 uint64_t value_ = (uint64_t)(value); \
96 buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \
97 buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \
98 buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \
99 buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \
100 buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \
101 buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \
102 buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \
103 buf_[offset_ + 7] = (unsigned char)(value_ ); \
104 } while (0)
105
106enum HprofTag {
107 HPROF_TAG_STRING = 0x01,
108 HPROF_TAG_LOAD_CLASS = 0x02,
109 HPROF_TAG_UNLOAD_CLASS = 0x03,
110 HPROF_TAG_STACK_FRAME = 0x04,
111 HPROF_TAG_STACK_TRACE = 0x05,
112 HPROF_TAG_ALLOC_SITES = 0x06,
113 HPROF_TAG_HEAP_SUMMARY = 0x07,
114 HPROF_TAG_START_THREAD = 0x0A,
115 HPROF_TAG_END_THREAD = 0x0B,
116 HPROF_TAG_HEAP_DUMP = 0x0C,
117 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
118 HPROF_TAG_HEAP_DUMP_END = 0x2C,
119 HPROF_TAG_CPU_SAMPLES = 0x0D,
120 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
121};
122
123// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
124enum HprofHeapTag {
125 // Traditional.
126 HPROF_ROOT_UNKNOWN = 0xFF,
127 HPROF_ROOT_JNI_GLOBAL = 0x01,
128 HPROF_ROOT_JNI_LOCAL = 0x02,
129 HPROF_ROOT_JAVA_FRAME = 0x03,
130 HPROF_ROOT_NATIVE_STACK = 0x04,
131 HPROF_ROOT_STICKY_CLASS = 0x05,
132 HPROF_ROOT_THREAD_BLOCK = 0x06,
133 HPROF_ROOT_MONITOR_USED = 0x07,
134 HPROF_ROOT_THREAD_OBJECT = 0x08,
135 HPROF_CLASS_DUMP = 0x20,
136 HPROF_INSTANCE_DUMP = 0x21,
137 HPROF_OBJECT_ARRAY_DUMP = 0x22,
138 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
139
140 // Android.
141 HPROF_HEAP_DUMP_INFO = 0xfe,
142 HPROF_ROOT_INTERNED_STRING = 0x89,
143 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
144 HPROF_ROOT_DEBUGGER = 0x8b,
145 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
146 HPROF_ROOT_VM_INTERNAL = 0x8d,
147 HPROF_ROOT_JNI_MONITOR = 0x8e,
148 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700149 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700150};
151
152enum HprofHeapId {
153 HPROF_HEAP_DEFAULT = 0,
154 HPROF_HEAP_ZYGOTE = 'Z',
155 HPROF_HEAP_APP = 'A'
156};
157
158enum HprofBasicType {
159 hprof_basic_object = 2,
160 hprof_basic_boolean = 4,
161 hprof_basic_char = 5,
162 hprof_basic_float = 6,
163 hprof_basic_double = 7,
164 hprof_basic_byte = 8,
165 hprof_basic_short = 9,
166 hprof_basic_int = 10,
167 hprof_basic_long = 11,
168};
169
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170typedef uint32_t HprofStringId;
171typedef uint32_t HprofClassObjectId;
Elliott Hughes622a6982012-06-08 17:58:54 -0700172
173// Represents a top-level hprof record, whose serialized format is:
174// U1 TAG: denoting the type of the record
175// U4 TIME: number of microseconds since the time stamp in the header
176// U4 LENGTH: number of bytes that follow this uint32_t field and belong to this record
177// U1* BODY: as many bytes as specified in the above uint32_t field
178class HprofRecord {
179 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800180 HprofRecord() : alloc_length_(128), fp_(nullptr), tag_(0), time_(0), length_(0), dirty_(false) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700181 body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700182 }
183
184 ~HprofRecord() {
185 free(body_);
186 }
187
188 int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) {
189 int rc = Flush();
190 if (rc != 0) {
191 return rc;
192 }
193
194 fp_ = fp;
195 tag_ = tag;
196 time_ = time;
197 length_ = 0;
198 dirty_ = true;
199 return 0;
200 }
201
202 int Flush() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700203 if (dirty_) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700204 unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)];
Elliott Hughes622a6982012-06-08 17:58:54 -0700205
206 headBuf[0] = tag_;
207 U4_TO_BUF_BE(headBuf, 1, time_);
208 U4_TO_BUF_BE(headBuf, 5, length_);
209
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700210 int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700211 if (nb != sizeof(headBuf)) {
212 return UNIQUE_ERROR;
213 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700214 nb = fwrite(body_, 1, length_, fp_);
Brian Carlstrom2d888622013-07-18 17:02:00 -0700215 if (nb != static_cast<int>(length_)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700216 return UNIQUE_ERROR;
217 }
218
219 dirty_ = false;
220 }
221 // TODO if we used less than half (or whatever) of allocLen, shrink the buffer.
222 return 0;
223 }
224
225 int AddU1(uint8_t value) {
226 int err = GuaranteeRecordAppend(1);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800227 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700228 return err;
229 }
230
231 body_[length_++] = value;
232 return 0;
233 }
234
235 int AddU2(uint16_t value) {
236 return AddU2List(&value, 1);
237 }
238
239 int AddU4(uint32_t value) {
240 return AddU4List(&value, 1);
241 }
242
243 int AddU8(uint64_t value) {
244 return AddU8List(&value, 1);
245 }
246
Ian Rogersef7d42f2014-01-06 12:55:46 -0800247 int AddObjectId(const mirror::Object* value) {
248 return AddU4(PointerToLowMemUInt32(value));
249 }
250
251 // The ID for the synthetic object generated to account for class static overhead.
252 int AddClassStaticsId(const mirror::Class* value) {
253 return AddU4(1 | PointerToLowMemUInt32(value));
254 }
255
256 int AddJniGlobalRefId(jobject value) {
257 return AddU4(PointerToLowMemUInt32(value));
258 }
259
260 int AddClassId(HprofClassObjectId value) {
261 return AddU4(value);
262 }
263
264 int AddStringId(HprofStringId value) {
265 return AddU4(value);
Elliott Hughes622a6982012-06-08 17:58:54 -0700266 }
267
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700268 int AddU1List(const uint8_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700269 int err = GuaranteeRecordAppend(numValues);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800270 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700271 return err;
272 }
273
274 memcpy(body_ + length_, values, numValues);
275 length_ += numValues;
276 return 0;
277 }
278
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700279 int AddU2List(const uint16_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700280 int err = GuaranteeRecordAppend(numValues * 2);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700282 return err;
283 }
284
285 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700286 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700287 U2_TO_BUF_BE(insert, 0, *values++);
288 insert += sizeof(*values);
289 }
290 length_ += numValues * 2;
291 return 0;
292 }
293
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700294 int AddU4List(const uint32_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700295 int err = GuaranteeRecordAppend(numValues * 4);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800296 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700297 return err;
298 }
299
300 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700301 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700302 U4_TO_BUF_BE(insert, 0, *values++);
303 insert += sizeof(*values);
304 }
305 length_ += numValues * 4;
306 return 0;
307 }
308
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700309 void UpdateU4(size_t offset, uint32_t new_value) {
310 U4_TO_BUF_BE(body_, offset, new_value);
311 }
312
313 int AddU8List(const uint64_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700314 int err = GuaranteeRecordAppend(numValues * 8);
315 if (err != 0) {
316 return err;
317 }
318
319 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700320 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700321 U8_TO_BUF_BE(insert, 0, *values++);
322 insert += sizeof(*values);
323 }
324 length_ += numValues * 8;
325 return 0;
326 }
327
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 int AddIdList(mirror::ObjectArray<mirror::Object>* values)
329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
330 int32_t length = values->GetLength();
331 for (int32_t i = 0; i < length; ++i) {
332 int err = AddObjectId(values->GetWithoutChecks(i));
333 if (UNLIKELY(err != 0)) {
334 return err;
335 }
336 }
337 return 0;
Elliott Hughes622a6982012-06-08 17:58:54 -0700338 }
339
340 int AddUtf8String(const char* str) {
341 // The terminating NUL character is NOT written.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700342 return AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700343 }
344
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700345 size_t Size() const {
346 return length_;
347 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700348
349 private:
350 int GuaranteeRecordAppend(size_t nmore) {
351 size_t minSize = length_ + nmore;
352 if (minSize > alloc_length_) {
353 size_t newAllocLen = alloc_length_ * 2;
354 if (newAllocLen < minSize) {
355 newAllocLen = alloc_length_ + nmore + nmore/2;
356 }
357 unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen);
358 if (newBody != NULL) {
359 body_ = newBody;
360 alloc_length_ = newAllocLen;
361 } else {
362 // TODO: set an error flag so future ops will fail
363 return UNIQUE_ERROR;
364 }
365 }
366
367 CHECK_LE(length_ + nmore, alloc_length_);
368 return 0;
369 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700370
371 size_t alloc_length_;
372 unsigned char* body_;
373
374 FILE* fp_;
375 uint8_t tag_;
376 uint32_t time_;
377 size_t length_;
378 bool dirty_;
379
380 DISALLOW_COPY_AND_ASSIGN(HprofRecord);
Elliott Hughes622a6982012-06-08 17:58:54 -0700381};
382
383class Hprof {
384 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700385 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
386 : filename_(output_filename),
387 fd_(fd),
388 direct_to_ddms_(direct_to_ddms),
389 start_ns_(NanoTime()),
390 current_record_(),
391 gc_thread_serial_number_(0),
392 gc_scan_state_(0),
393 current_heap_(HPROF_HEAP_DEFAULT),
394 objects_in_segment_(0),
395 header_fp_(NULL),
396 header_data_ptr_(NULL),
397 header_data_size_(0),
398 body_fp_(NULL),
399 body_data_ptr_(NULL),
400 body_data_size_(0),
401 next_string_id_(0x400000) {
402 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Elliott Hughes622a6982012-06-08 17:58:54 -0700403
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700404 header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_);
405 if (header_fp_ == NULL) {
406 PLOG(FATAL) << "header open_memstream failed";
407 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700408
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700409 body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_);
410 if (body_fp_ == NULL) {
411 PLOG(FATAL) << "body open_memstream failed";
412 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500413 }
414
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700415 ~Hprof() {
416 if (header_fp_ != NULL) {
417 fclose(header_fp_);
418 }
419 if (body_fp_ != NULL) {
420 fclose(body_fp_);
421 }
422 free(header_data_ptr_);
423 free(body_data_ptr_);
424 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500425
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 void Dump()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700427 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
428 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700429 // Walk the roots and the heap.
430 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
Ian Rogers1d54e732013-05-02 21:10:01 -0700431 Runtime::Current()->VisitRoots(RootVisitor, this, false, false);
Ian Rogers50b35e22012-10-04 10:09:15 -0700432 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700433 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700434 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700435 Runtime::Current()->GetHeap()->FlushAllocStack();
436 }
437 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700438 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 Runtime::Current()->GetHeap()->GetLiveBitmap()->Walk(HeapBitmapCallback, this);
440 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700441 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
442 current_record_.Flush();
443 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500444
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700445 // Write the header.
446 WriteFixedHeader();
447 // Write the string and class tables, and any stack traces, to the header.
448 // (jhat requires that these appear before any of the data in the body that refers to them.)
449 WriteStringTable();
450 WriteClassTable();
451 WriteStackTraces();
452 current_record_.Flush();
453 fflush(header_fp_);
454
455 bool okay = true;
456 if (direct_to_ddms_) {
457 // Send the data off to DDMS.
458 iovec iov[2];
459 iov[0].iov_base = header_data_ptr_;
460 iov[0].iov_len = header_data_size_;
461 iov[1].iov_base = body_data_ptr_;
462 iov[1].iov_len = body_data_size_;
463 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
464 } else {
465 // Where exactly are we writing to?
466 int out_fd;
467 if (fd_ >= 0) {
468 out_fd = dup(fd_);
469 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700471 return;
472 }
473 } else {
474 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
475 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800476 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
477 strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700478 return;
479 }
480 }
481
Elliott Hughes76160052012-12-12 16:31:20 -0800482 UniquePtr<File> file(new File(out_fd, filename_));
Ian Rogers50b35e22012-10-04 10:09:15 -0700483 okay = file->WriteFully(header_data_ptr_, header_data_size_) &&
484 file->WriteFully(body_data_ptr_, body_data_size_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700485 if (!okay) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700486 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
487 filename_.c_str(), strerror(errno)));
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 ThrowRuntimeException("%s", msg.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700489 LOG(ERROR) << msg;
490 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700491 }
492
493 // Throw out a log message for the benefit of "runhat".
494 if (okay) {
495 uint64_t duration = NanoTime() - start_ns_;
Ian Rogers50b35e22012-10-04 10:09:15 -0700496 LOG(INFO) << "hprof: heap dump completed ("
497 << PrettySize(header_data_size_ + body_data_size_ + 1023)
498 << ") in " << PrettyDuration(duration);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700499 }
500 }
501
502 private:
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700503 static mirror::Object* RootVisitor(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700505 DCHECK(arg != NULL);
506 reinterpret_cast<Hprof*>(arg)->VisitRoot(obj);
507 return obj;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700508 }
509
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 static void HeapBitmapCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700512 CHECK(obj != NULL);
513 CHECK(arg != NULL);
514 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
515 hprof->DumpHeapObject(obj);
516 }
517
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 void VisitRoot(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700519
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700521
522 void Finish() {
523 }
524
Ian Rogersb726dcb2012-09-05 08:57:23 -0700525 int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700526 HprofRecord* rec = &current_record_;
527 uint32_t nextSerialNumber = 1;
528
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529 for (mirror::Class* c : classes_) {
530 CHECK(c != nullptr);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700531
532 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800533 if (UNLIKELY(err != 0)) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700534 return err;
535 }
536
537 // LOAD CLASS format:
538 // U4: class serial number (always > 0)
539 // ID: class object ID. We use the address of the class object structure as its ID.
540 // U4: stack trace serial number
541 // ID: class name string ID
542 rec->AddU4(nextSerialNumber++);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800543 rec->AddObjectId(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700544 rec->AddU4(HPROF_NULL_STACK_TRACE);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800545 rec->AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700546 }
547
548 return 0;
549 }
550
551 int WriteStringTable() {
552 HprofRecord* rec = &current_record_;
553
Ian Rogersef7d42f2014-01-06 12:55:46 -0800554 for (std::pair<std::string, HprofStringId> p : strings_) {
555 const std::string& string = p.first;
556 size_t id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700557
558 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
559 if (err != 0) {
560 return err;
561 }
562
563 // STRING format:
564 // ID: ID for this string
565 // U1*: UTF8 characters for string (NOT NULL terminated)
566 // (the record format encodes the length)
567 err = rec->AddU4(id);
568 if (err != 0) {
569 return err;
570 }
571 err = rec->AddUtf8String(string.c_str());
572 if (err != 0) {
573 return err;
574 }
575 }
576
577 return 0;
578 }
579
580 void StartNewHeapDumpSegment() {
581 // This flushes the old segment and starts a new one.
582 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
583 objects_in_segment_ = 0;
584
585 // Starting a new HEAP_DUMP resets the heap to default.
586 current_heap_ = HPROF_HEAP_DEFAULT;
587 }
588
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 int MarkRootObject(const mirror::Object* obj, jobject jniObj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700590
Ian Rogersef7d42f2014-01-06 12:55:46 -0800591 HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
592 if (c == nullptr) {
593 // c is the superclass of java.lang.Object or a primitive.
594 return 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700595 }
596
Ian Rogersef7d42f2014-01-06 12:55:46 -0800597 {
598 auto result = classes_.insert(c);
599 const mirror::Class* present = *result.first;
600 CHECK_EQ(present, c);
601 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700602
603 // Make sure that we've assigned a string ID for this class' name
604 LookupClassNameId(c);
605
Ian Rogersef7d42f2014-01-06 12:55:46 -0800606 HprofClassObjectId result = PointerToLowMemUInt32(c);
607 return result;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700608 }
609
Ian Rogersef7d42f2014-01-06 12:55:46 -0800610 HprofStringId LookupStringId(mirror::String* string) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700611 return LookupStringId(string->ToModifiedUtf8());
612 }
613
614 HprofStringId LookupStringId(const char* string) {
615 return LookupStringId(std::string(string));
616 }
617
618 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800619 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700620 if (it != strings_.end()) {
621 return it->second;
622 }
623 HprofStringId id = next_string_id_++;
624 strings_.Put(string, id);
625 return id;
626 }
627
Ian Rogersef7d42f2014-01-06 12:55:46 -0800628 HprofStringId LookupClassNameId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700629 return LookupStringId(PrettyDescriptor(c));
630 }
631
632 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700633 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500634 unsigned char buf[4];
635
636 // Write the file header.
637 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700638 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500639
640 // U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700641 U4_TO_BUF_BE(buf, 0, sizeof(void*));
642 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500643
644 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700645 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500646 uint64_t nowMs;
647 if (gettimeofday(&now, NULL) < 0) {
648 nowMs = 0;
649 } else {
650 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
651 }
652
653 // U4: high word of the 64-bit time.
654 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700655 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500656
657 // U4: low word of the 64-bit time.
658 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700659 fwrite(buf, 1, sizeof(uint32_t), header_fp_); // xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500660 }
661
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700662 void WriteStackTraces() {
663 // Write a dummy stack trace record so the analysis tools don't freak out.
664 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
665 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
666 current_record_.AddU4(HPROF_NULL_THREAD);
667 current_record_.AddU4(0); // no frames
668 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500669
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700670 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
671 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
672 // only be used for debug messages.
673 std::string filename_;
674 int fd_;
675 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500676
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700677 uint64_t start_ns_;
678
679 HprofRecord current_record_;
680
681 uint32_t gc_thread_serial_number_;
682 uint8_t gc_scan_state_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700683 HprofHeapId current_heap_; // Which heap we're currently dumping.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700684 size_t objects_in_segment_;
685
686 FILE* header_fp_;
687 char* header_data_ptr_;
688 size_t header_data_size_;
689
690 FILE* body_fp_;
691 char* body_data_ptr_;
692 size_t body_data_size_;
693
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 std::set<mirror::Class*> classes_;
695 HprofStringId next_string_id_;
696 SafeMap<std::string, HprofStringId> strings_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700697
698 DISALLOW_COPY_AND_ASSIGN(Hprof);
699};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500700
701#define OBJECTS_PER_SEGMENT ((size_t)128)
702#define BYTES_PER_SEGMENT ((size_t)4096)
703
Ian Rogersef7d42f2014-01-06 12:55:46 -0800704// The static field-name for the synthetic object generated to account for class static overhead.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500705#define STATIC_OVERHEAD_NAME "$staticOverhead"
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500706
Elliott Hughes622a6982012-06-08 17:58:54 -0700707static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500708 char c = sig[0];
709 HprofBasicType ret;
710 size_t size;
711
712 switch (c) {
713 case '[':
714 case 'L': ret = hprof_basic_object; size = 4; break;
715 case 'Z': ret = hprof_basic_boolean; size = 1; break;
716 case 'C': ret = hprof_basic_char; size = 2; break;
717 case 'F': ret = hprof_basic_float; size = 4; break;
718 case 'D': ret = hprof_basic_double; size = 8; break;
719 case 'B': ret = hprof_basic_byte; size = 1; break;
720 case 'S': ret = hprof_basic_short; size = 2; break;
721 default: CHECK(false);
722 case 'I': ret = hprof_basic_int; size = 4; break;
723 case 'J': ret = hprof_basic_long; size = 8; break;
724 }
725
726 if (sizeOut != NULL) {
727 *sizeOut = size;
728 }
729
730 return ret;
731}
732
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700733static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500734 HprofBasicType ret;
735 size_t size;
736
737 switch (prim) {
738 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
739 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
740 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
741 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
742 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
743 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
744 default: CHECK(false);
745 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
746 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
747 }
748
749 if (sizeOut != NULL) {
750 *sizeOut = size;
751 }
752
753 return ret;
754}
755
756// Always called when marking objects, but only does
757// something when ctx->gc_scan_state_ is non-zero, which is usually
758// only true when marking the root set or unreachable
759// objects. Used to add rootset references to obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800760int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700761 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500762 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
763
764 if (heapTag == 0) {
765 return 0;
766 }
767
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700768 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
769 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500770 }
771
772 switch (heapTag) {
773 // ID: object ID
774 case HPROF_ROOT_UNKNOWN:
775 case HPROF_ROOT_STICKY_CLASS:
776 case HPROF_ROOT_MONITOR_USED:
777 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500778 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500779 case HPROF_ROOT_VM_INTERNAL:
780 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800781 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500782 break;
783
784 // ID: object ID
785 // ID: JNI global ref ID
786 case HPROF_ROOT_JNI_GLOBAL:
787 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800788 rec->AddObjectId(obj);
789 rec->AddJniGlobalRefId(jniObj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500790 break;
791
792 // ID: object ID
793 // U4: thread serial number
794 // U4: frame number in stack trace (-1 for empty)
795 case HPROF_ROOT_JNI_LOCAL:
796 case HPROF_ROOT_JNI_MONITOR:
797 case HPROF_ROOT_JAVA_FRAME:
798 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800799 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500800 rec->AddU4(gc_thread_serial_number_);
801 rec->AddU4((uint32_t)-1);
802 break;
803
804 // ID: object ID
805 // U4: thread serial number
806 case HPROF_ROOT_NATIVE_STACK:
807 case HPROF_ROOT_THREAD_BLOCK:
808 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500810 rec->AddU4(gc_thread_serial_number_);
811 break;
812
813 // ID: thread object ID
814 // U4: thread serial number
815 // U4: stack trace serial number
816 case HPROF_ROOT_THREAD_OBJECT:
817 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800818 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500819 rec->AddU4(gc_thread_serial_number_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700820 rec->AddU4((uint32_t)-1); // xxx
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500821 break;
822
Elliott Hughes73e66f72012-05-09 09:34:45 -0700823 case HPROF_CLASS_DUMP:
824 case HPROF_INSTANCE_DUMP:
825 case HPROF_OBJECT_ARRAY_DUMP:
826 case HPROF_PRIMITIVE_ARRAY_DUMP:
827 case HPROF_HEAP_DUMP_INFO:
828 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
829 // Ignored.
830 break;
831
832 case HPROF_ROOT_FINALIZING:
833 case HPROF_ROOT_REFERENCE_CLEANUP:
834 case HPROF_UNREACHABLE:
835 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500836 break;
837 }
838
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700839 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700840 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500841}
842
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843static int StackTraceSerialNumber(const mirror::Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500844 return HPROF_NULL_STACK_TRACE;
845}
846
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800847int Hprof::DumpHeapObject(mirror::Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700848 HprofRecord* rec = &current_record_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700849 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500850
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700851 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
852 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500853 }
854
855 if (desiredHeap != current_heap_) {
856 HprofStringId nameId;
857
858 // This object is in a different heap than the current one.
859 // Emit a HEAP_DUMP_INFO tag to change heaps.
860 rec->AddU1(HPROF_HEAP_DUMP_INFO);
861 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
862 switch (desiredHeap) {
863 case HPROF_HEAP_APP:
864 nameId = LookupStringId("app");
865 break;
866 case HPROF_HEAP_ZYGOTE:
867 nameId = LookupStringId("zygote");
868 break;
869 default:
870 // Internal error
871 LOG(ERROR) << "Unexpected desiredHeap";
872 nameId = LookupStringId("<ILLEGAL>");
873 break;
874 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800875 rec->AddStringId(nameId);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500876 current_heap_ = desiredHeap;
877 }
878
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800879 mirror::Class* c = obj->GetClass();
Elliott Hughese84278b2012-03-22 10:06:53 -0700880 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500881 // This object will bother HprofReader, because it has a NULL
882 // class, so just don't dump it. It could be
883 // gDvm.unlinkedJavaLangClass or it could be an object just
884 // allocated which hasn't been initialized yet.
885 } else {
886 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 mirror::Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500888 // obj is a ClassObject.
889 size_t sFieldCount = thisClass->NumStaticFields();
890 if (sFieldCount != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800891 int byteLength = sFieldCount * sizeof(JValue); // TODO bogus; fields are packed
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500892 // Create a byte array to reflect the allocation of the
893 // StaticField array at the end of this class.
894 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800895 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500896 rec->AddU4(StackTraceSerialNumber(obj));
897 rec->AddU4(byteLength);
898 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700899 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500900 rec->AddU1(0);
901 }
902 }
903
904 rec->AddU1(HPROF_CLASS_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800905 rec->AddClassId(LookupClassId(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500906 rec->AddU4(StackTraceSerialNumber(thisClass));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800907 rec->AddClassId(LookupClassId(thisClass->GetSuperClass()));
908 rec->AddObjectId(thisClass->GetClassLoader());
909 rec->AddObjectId(nullptr); // no signer
910 rec->AddObjectId(nullptr); // no prot domain
911 rec->AddObjectId(nullptr); // reserved
912 rec->AddObjectId(nullptr); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800913 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500914 // ClassObjects have their static fields appended, so aren't all the same size.
915 // But they're at least this size.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700916 rec->AddU4(sizeof(mirror::Class)); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500917 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
918 rec->AddU4(0);
919 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700920 rec->AddU4(thisClass->GetObjectSize()); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500921 }
922
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700923 rec->AddU2(0); // empty const pool
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500924
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800925 FieldHelper fh;
926
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500927 // Static fields
928 if (sFieldCount == 0) {
929 rec->AddU2((uint16_t)0);
930 } else {
931 rec->AddU2((uint16_t)(sFieldCount+1));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800932 rec->AddStringId(LookupStringId(STATIC_OVERHEAD_NAME));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500933 rec->AddU1(hprof_basic_object);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800934 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500935
936 for (size_t i = 0; i < sFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700937 mirror::ArtField* f = thisClass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800938 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500939
940 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800941 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800942 rec->AddStringId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500943 rec->AddU1(t);
944 if (size == 1) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700945 rec->AddU1(static_cast<uint8_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500946 } else if (size == 2) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700947 rec->AddU2(static_cast<uint16_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500948 } else if (size == 4) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700949 rec->AddU4(f->Get32(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500950 } else if (size == 8) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700951 rec->AddU8(f->Get64(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500952 } else {
953 CHECK(false);
954 }
955 }
956 }
957
958 // Instance fields for this class (no superclass fields)
959 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
960 rec->AddU2((uint16_t)iFieldCount);
961 for (int i = 0; i < iFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700962 mirror::ArtField* f = thisClass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800963 fh.ChangeField(f);
964 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800965 rec->AddStringId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500966 rec->AddU1(t);
967 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700968 } else if (c->IsArrayClass()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800969 mirror::Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500970 uint32_t length = aobj->GetLength();
971
972 if (obj->IsObjectArray()) {
973 // obj is an object array.
974 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
975
Ian Rogersef7d42f2014-01-06 12:55:46 -0800976 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500977 rec->AddU4(StackTraceSerialNumber(obj));
978 rec->AddU4(length);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800979 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500980
981 // Dump the elements, which are always objects or NULL.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800982 rec->AddIdList(aobj->AsObjectArray<mirror::Object>());
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500983 } else {
984 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700985 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500986
987 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500988 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500989
Ian Rogersef7d42f2014-01-06 12:55:46 -0800990 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500991 rec->AddU4(StackTraceSerialNumber(obj));
992 rec->AddU4(length);
993 rec->AddU1(t);
994
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500995 // Dump the raw, packed element values.
996 if (size == 1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800997 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500998 } else if (size == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800999 rec->AddU2List((const uint16_t*)aobj->GetRawData(sizeof(uint16_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001000 } else if (size == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001001 rec->AddU4List((const uint32_t*)aobj->GetRawData(sizeof(uint32_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001002 } else if (size == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001003 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001004 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001005 }
1006 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001007 // obj is an instance object.
1008 rec->AddU1(HPROF_INSTANCE_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001009 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001010 rec->AddU4(StackTraceSerialNumber(obj));
Ian Rogersef7d42f2014-01-06 12:55:46 -08001011 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001012
1013 // Reserve some space for the length of the instance data, which we won't
1014 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001015 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001016 rec->AddU4(0x77777777);
1017
1018 // Write the instance data; fields for this class, followed by super class fields,
1019 // and so on. Don't write the klass or monitor fields of Object.class.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001020 mirror::Class* sclass = c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001021 FieldHelper fh;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001022 while (!sclass->IsObjectClass()) {
1023 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001024 for (int i = 0; i < ifieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001025 mirror::ArtField* f = sclass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001026 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001027 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001028 SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001029 if (size == 1) {
1030 rec->AddU1(f->Get32(obj));
1031 } else if (size == 2) {
1032 rec->AddU2(f->Get32(obj));
1033 } else if (size == 4) {
1034 rec->AddU4(f->Get32(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001035 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001036 CHECK_EQ(size, 8U);
1037 rec->AddU8(f->Get64(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001038 }
1039 }
1040
1041 sclass = sclass->GetSuperClass();
1042 }
1043
1044 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001045 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001046 }
1047 }
1048
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001049 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001050 return 0;
1051}
1052
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001053void Hprof::VisitRoot(const mirror::Object* obj) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001054 uint32_t threadId = 0; // TODO
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001055 /*RootType*/ size_t type = 0; // TODO
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001056
Jesse Wilson0b075f12011-11-09 10:57:41 -05001057 static const HprofHeapTag xlate[] = {
1058 HPROF_ROOT_UNKNOWN,
1059 HPROF_ROOT_JNI_GLOBAL,
1060 HPROF_ROOT_JNI_LOCAL,
1061 HPROF_ROOT_JAVA_FRAME,
1062 HPROF_ROOT_NATIVE_STACK,
1063 HPROF_ROOT_STICKY_CLASS,
1064 HPROF_ROOT_THREAD_BLOCK,
1065 HPROF_ROOT_MONITOR_USED,
1066 HPROF_ROOT_THREAD_OBJECT,
1067 HPROF_ROOT_INTERNED_STRING,
1068 HPROF_ROOT_FINALIZING,
1069 HPROF_ROOT_DEBUGGER,
1070 HPROF_ROOT_REFERENCE_CLEANUP,
1071 HPROF_ROOT_VM_INTERNAL,
1072 HPROF_ROOT_JNI_MONITOR,
1073 };
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001074
Jesse Wilson0b075f12011-11-09 10:57:41 -05001075 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1076 if (obj == NULL) {
1077 return;
1078 }
1079 gc_scan_state_ = xlate[type];
1080 gc_thread_serial_number_ = threadId;
1081 MarkRootObject(obj, 0);
1082 gc_scan_state_ = 0;
1083 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001084}
1085
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001086// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1087// sent directly to DDMS.
1088// If "fd" is >= 0, the output will be written to that file descriptor.
1089// Otherwise, "filename" is used to create an output file.
1090void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1091 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001092
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001094 Hprof hprof(filename, fd, direct_to_ddms);
1095 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001097}
1098
1099} // namespace hprof
1100
1101} // namespace art