blob: c5a8328a16372c95c931b22c47f7837a172e7b3b [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 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800435 Runtime::Current()->GetHeap()->VisitObjects(VisitObjectCallback, this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700436 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700437 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
438 current_record_.Flush();
439 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500440
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700441 // Write the header.
442 WriteFixedHeader();
443 // Write the string and class tables, and any stack traces, to the header.
444 // (jhat requires that these appear before any of the data in the body that refers to them.)
445 WriteStringTable();
446 WriteClassTable();
447 WriteStackTraces();
448 current_record_.Flush();
449 fflush(header_fp_);
450
451 bool okay = true;
452 if (direct_to_ddms_) {
453 // Send the data off to DDMS.
454 iovec iov[2];
455 iov[0].iov_base = header_data_ptr_;
456 iov[0].iov_len = header_data_size_;
457 iov[1].iov_base = body_data_ptr_;
458 iov[1].iov_len = body_data_size_;
459 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
460 } else {
461 // Where exactly are we writing to?
462 int out_fd;
463 if (fd_ >= 0) {
464 out_fd = dup(fd_);
465 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700467 return;
468 }
469 } else {
470 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
471 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800472 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
473 strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700474 return;
475 }
476 }
477
Elliott Hughes76160052012-12-12 16:31:20 -0800478 UniquePtr<File> file(new File(out_fd, filename_));
Ian Rogers50b35e22012-10-04 10:09:15 -0700479 okay = file->WriteFully(header_data_ptr_, header_data_size_) &&
480 file->WriteFully(body_data_ptr_, body_data_size_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700481 if (!okay) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700482 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
483 filename_.c_str(), strerror(errno)));
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 ThrowRuntimeException("%s", msg.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700485 LOG(ERROR) << msg;
486 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700487 }
488
489 // Throw out a log message for the benefit of "runhat".
490 if (okay) {
491 uint64_t duration = NanoTime() - start_ns_;
Ian Rogers50b35e22012-10-04 10:09:15 -0700492 LOG(INFO) << "hprof: heap dump completed ("
493 << PrettySize(header_data_size_ + body_data_size_ + 1023)
494 << ") in " << PrettyDuration(duration);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700495 }
496 }
497
498 private:
Mathieu Chartier815873e2014-02-13 18:02:13 -0800499 static void RootVisitor(mirror::Object** obj, void* arg, uint32_t thread_id, RootType root_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800501 DCHECK(arg != nullptr);
502 DCHECK(obj != nullptr);
503 DCHECK(*obj != nullptr);
504 reinterpret_cast<Hprof*>(arg)->VisitRoot(*obj, thread_id, root_type);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700505 }
506
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800507 static void VisitObjectCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800509 DCHECK(obj != NULL);
510 DCHECK(arg != NULL);
511 reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700512 }
513
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800514 void VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type)
515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700516
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517 int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700518
519 void Finish() {
520 }
521
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700523 HprofRecord* rec = &current_record_;
524 uint32_t nextSerialNumber = 1;
525
Ian Rogersef7d42f2014-01-06 12:55:46 -0800526 for (mirror::Class* c : classes_) {
527 CHECK(c != nullptr);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700528
529 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800530 if (UNLIKELY(err != 0)) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700531 return err;
532 }
533
534 // LOAD CLASS format:
535 // U4: class serial number (always > 0)
536 // ID: class object ID. We use the address of the class object structure as its ID.
537 // U4: stack trace serial number
538 // ID: class name string ID
539 rec->AddU4(nextSerialNumber++);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800540 rec->AddObjectId(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700541 rec->AddU4(HPROF_NULL_STACK_TRACE);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 rec->AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700543 }
544
545 return 0;
546 }
547
548 int WriteStringTable() {
549 HprofRecord* rec = &current_record_;
550
Ian Rogersef7d42f2014-01-06 12:55:46 -0800551 for (std::pair<std::string, HprofStringId> p : strings_) {
552 const std::string& string = p.first;
553 size_t id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700554
555 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
556 if (err != 0) {
557 return err;
558 }
559
560 // STRING format:
561 // ID: ID for this string
562 // U1*: UTF8 characters for string (NOT NULL terminated)
563 // (the record format encodes the length)
564 err = rec->AddU4(id);
565 if (err != 0) {
566 return err;
567 }
568 err = rec->AddUtf8String(string.c_str());
569 if (err != 0) {
570 return err;
571 }
572 }
573
574 return 0;
575 }
576
577 void StartNewHeapDumpSegment() {
578 // This flushes the old segment and starts a new one.
579 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
580 objects_in_segment_ = 0;
581
582 // Starting a new HEAP_DUMP resets the heap to default.
583 current_heap_ = HPROF_HEAP_DEFAULT;
584 }
585
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 int MarkRootObject(const mirror::Object* obj, jobject jniObj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700587
Ian Rogersef7d42f2014-01-06 12:55:46 -0800588 HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
589 if (c == nullptr) {
590 // c is the superclass of java.lang.Object or a primitive.
591 return 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700592 }
593
Ian Rogersef7d42f2014-01-06 12:55:46 -0800594 {
595 auto result = classes_.insert(c);
596 const mirror::Class* present = *result.first;
597 CHECK_EQ(present, c);
598 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700599
600 // Make sure that we've assigned a string ID for this class' name
601 LookupClassNameId(c);
602
Ian Rogersef7d42f2014-01-06 12:55:46 -0800603 HprofClassObjectId result = PointerToLowMemUInt32(c);
604 return result;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700605 }
606
Ian Rogersef7d42f2014-01-06 12:55:46 -0800607 HprofStringId LookupStringId(mirror::String* string) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700608 return LookupStringId(string->ToModifiedUtf8());
609 }
610
611 HprofStringId LookupStringId(const char* string) {
612 return LookupStringId(std::string(string));
613 }
614
615 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800616 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700617 if (it != strings_.end()) {
618 return it->second;
619 }
620 HprofStringId id = next_string_id_++;
621 strings_.Put(string, id);
622 return id;
623 }
624
Ian Rogersef7d42f2014-01-06 12:55:46 -0800625 HprofStringId LookupClassNameId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700626 return LookupStringId(PrettyDescriptor(c));
627 }
628
629 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700630 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500631 unsigned char buf[4];
632
633 // Write the file header.
634 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700635 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500636
637 // U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700638 U4_TO_BUF_BE(buf, 0, sizeof(void*));
639 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500640
641 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700642 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500643 uint64_t nowMs;
644 if (gettimeofday(&now, NULL) < 0) {
645 nowMs = 0;
646 } else {
647 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
648 }
649
650 // U4: high word of the 64-bit time.
651 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700652 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500653
654 // U4: low word of the 64-bit time.
655 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700656 fwrite(buf, 1, sizeof(uint32_t), header_fp_); // xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500657 }
658
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700659 void WriteStackTraces() {
660 // Write a dummy stack trace record so the analysis tools don't freak out.
661 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
662 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
663 current_record_.AddU4(HPROF_NULL_THREAD);
664 current_record_.AddU4(0); // no frames
665 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500666
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700667 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
668 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
669 // only be used for debug messages.
670 std::string filename_;
671 int fd_;
672 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500673
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700674 uint64_t start_ns_;
675
676 HprofRecord current_record_;
677
678 uint32_t gc_thread_serial_number_;
679 uint8_t gc_scan_state_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700680 HprofHeapId current_heap_; // Which heap we're currently dumping.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700681 size_t objects_in_segment_;
682
683 FILE* header_fp_;
684 char* header_data_ptr_;
685 size_t header_data_size_;
686
687 FILE* body_fp_;
688 char* body_data_ptr_;
689 size_t body_data_size_;
690
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 std::set<mirror::Class*> classes_;
692 HprofStringId next_string_id_;
693 SafeMap<std::string, HprofStringId> strings_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700694
695 DISALLOW_COPY_AND_ASSIGN(Hprof);
696};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500697
698#define OBJECTS_PER_SEGMENT ((size_t)128)
699#define BYTES_PER_SEGMENT ((size_t)4096)
700
Ian Rogersef7d42f2014-01-06 12:55:46 -0800701// The static field-name for the synthetic object generated to account for class static overhead.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500702#define STATIC_OVERHEAD_NAME "$staticOverhead"
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500703
Elliott Hughes622a6982012-06-08 17:58:54 -0700704static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500705 char c = sig[0];
706 HprofBasicType ret;
707 size_t size;
708
709 switch (c) {
710 case '[':
711 case 'L': ret = hprof_basic_object; size = 4; break;
712 case 'Z': ret = hprof_basic_boolean; size = 1; break;
713 case 'C': ret = hprof_basic_char; size = 2; break;
714 case 'F': ret = hprof_basic_float; size = 4; break;
715 case 'D': ret = hprof_basic_double; size = 8; break;
716 case 'B': ret = hprof_basic_byte; size = 1; break;
717 case 'S': ret = hprof_basic_short; size = 2; break;
718 default: CHECK(false);
719 case 'I': ret = hprof_basic_int; size = 4; break;
720 case 'J': ret = hprof_basic_long; size = 8; break;
721 }
722
723 if (sizeOut != NULL) {
724 *sizeOut = size;
725 }
726
727 return ret;
728}
729
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700730static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500731 HprofBasicType ret;
732 size_t size;
733
734 switch (prim) {
735 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
736 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
737 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
738 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
739 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
740 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
741 default: CHECK(false);
742 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
743 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
744 }
745
746 if (sizeOut != NULL) {
747 *sizeOut = size;
748 }
749
750 return ret;
751}
752
753// Always called when marking objects, but only does
754// something when ctx->gc_scan_state_ is non-zero, which is usually
755// only true when marking the root set or unreachable
756// objects. Used to add rootset references to obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800757int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700758 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500759 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
760
761 if (heapTag == 0) {
762 return 0;
763 }
764
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700765 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
766 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500767 }
768
769 switch (heapTag) {
770 // ID: object ID
771 case HPROF_ROOT_UNKNOWN:
772 case HPROF_ROOT_STICKY_CLASS:
773 case HPROF_ROOT_MONITOR_USED:
774 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500775 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500776 case HPROF_ROOT_VM_INTERNAL:
777 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800778 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500779 break;
780
781 // ID: object ID
782 // ID: JNI global ref ID
783 case HPROF_ROOT_JNI_GLOBAL:
784 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800785 rec->AddObjectId(obj);
786 rec->AddJniGlobalRefId(jniObj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500787 break;
788
789 // ID: object ID
790 // U4: thread serial number
791 // U4: frame number in stack trace (-1 for empty)
792 case HPROF_ROOT_JNI_LOCAL:
793 case HPROF_ROOT_JNI_MONITOR:
794 case HPROF_ROOT_JAVA_FRAME:
795 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800796 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500797 rec->AddU4(gc_thread_serial_number_);
798 rec->AddU4((uint32_t)-1);
799 break;
800
801 // ID: object ID
802 // U4: thread serial number
803 case HPROF_ROOT_NATIVE_STACK:
804 case HPROF_ROOT_THREAD_BLOCK:
805 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800806 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500807 rec->AddU4(gc_thread_serial_number_);
808 break;
809
810 // ID: thread object ID
811 // U4: thread serial number
812 // U4: stack trace serial number
813 case HPROF_ROOT_THREAD_OBJECT:
814 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800815 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500816 rec->AddU4(gc_thread_serial_number_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700817 rec->AddU4((uint32_t)-1); // xxx
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500818 break;
819
Elliott Hughes73e66f72012-05-09 09:34:45 -0700820 case HPROF_CLASS_DUMP:
821 case HPROF_INSTANCE_DUMP:
822 case HPROF_OBJECT_ARRAY_DUMP:
823 case HPROF_PRIMITIVE_ARRAY_DUMP:
824 case HPROF_HEAP_DUMP_INFO:
825 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
826 // Ignored.
827 break;
828
829 case HPROF_ROOT_FINALIZING:
830 case HPROF_ROOT_REFERENCE_CLEANUP:
831 case HPROF_UNREACHABLE:
832 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500833 break;
834 }
835
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700836 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700837 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500838}
839
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800840static int StackTraceSerialNumber(const mirror::Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500841 return HPROF_NULL_STACK_TRACE;
842}
843
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800844int Hprof::DumpHeapObject(mirror::Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700845 HprofRecord* rec = &current_record_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700846 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500847
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700848 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
849 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500850 }
851
852 if (desiredHeap != current_heap_) {
853 HprofStringId nameId;
854
855 // This object is in a different heap than the current one.
856 // Emit a HEAP_DUMP_INFO tag to change heaps.
857 rec->AddU1(HPROF_HEAP_DUMP_INFO);
858 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
859 switch (desiredHeap) {
860 case HPROF_HEAP_APP:
861 nameId = LookupStringId("app");
862 break;
863 case HPROF_HEAP_ZYGOTE:
864 nameId = LookupStringId("zygote");
865 break;
866 default:
867 // Internal error
868 LOG(ERROR) << "Unexpected desiredHeap";
869 nameId = LookupStringId("<ILLEGAL>");
870 break;
871 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800872 rec->AddStringId(nameId);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500873 current_heap_ = desiredHeap;
874 }
875
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800876 mirror::Class* c = obj->GetClass();
Elliott Hughese84278b2012-03-22 10:06:53 -0700877 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500878 // This object will bother HprofReader, because it has a NULL
879 // class, so just don't dump it. It could be
880 // gDvm.unlinkedJavaLangClass or it could be an object just
881 // allocated which hasn't been initialized yet.
882 } else {
883 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884 mirror::Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500885 // obj is a ClassObject.
886 size_t sFieldCount = thisClass->NumStaticFields();
887 if (sFieldCount != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800888 int byteLength = sFieldCount * sizeof(JValue); // TODO bogus; fields are packed
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500889 // Create a byte array to reflect the allocation of the
890 // StaticField array at the end of this class.
891 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800892 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500893 rec->AddU4(StackTraceSerialNumber(obj));
894 rec->AddU4(byteLength);
895 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700896 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500897 rec->AddU1(0);
898 }
899 }
900
901 rec->AddU1(HPROF_CLASS_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800902 rec->AddClassId(LookupClassId(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500903 rec->AddU4(StackTraceSerialNumber(thisClass));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800904 rec->AddClassId(LookupClassId(thisClass->GetSuperClass()));
905 rec->AddObjectId(thisClass->GetClassLoader());
906 rec->AddObjectId(nullptr); // no signer
907 rec->AddObjectId(nullptr); // no prot domain
908 rec->AddObjectId(nullptr); // reserved
909 rec->AddObjectId(nullptr); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800910 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500911 // ClassObjects have their static fields appended, so aren't all the same size.
912 // But they're at least this size.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700913 rec->AddU4(sizeof(mirror::Class)); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500914 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
915 rec->AddU4(0);
916 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700917 rec->AddU4(thisClass->GetObjectSize()); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500918 }
919
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700920 rec->AddU2(0); // empty const pool
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500921
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 FieldHelper fh;
923
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500924 // Static fields
925 if (sFieldCount == 0) {
926 rec->AddU2((uint16_t)0);
927 } else {
928 rec->AddU2((uint16_t)(sFieldCount+1));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800929 rec->AddStringId(LookupStringId(STATIC_OVERHEAD_NAME));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500930 rec->AddU1(hprof_basic_object);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800931 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500932
933 for (size_t i = 0; i < sFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700934 mirror::ArtField* f = thisClass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800935 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500936
937 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800938 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800939 rec->AddStringId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500940 rec->AddU1(t);
941 if (size == 1) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700942 rec->AddU1(static_cast<uint8_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500943 } else if (size == 2) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700944 rec->AddU2(static_cast<uint16_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500945 } else if (size == 4) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700946 rec->AddU4(f->Get32(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500947 } else if (size == 8) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700948 rec->AddU8(f->Get64(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500949 } else {
950 CHECK(false);
951 }
952 }
953 }
954
955 // Instance fields for this class (no superclass fields)
956 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
957 rec->AddU2((uint16_t)iFieldCount);
958 for (int i = 0; i < iFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700959 mirror::ArtField* f = thisClass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800960 fh.ChangeField(f);
961 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800962 rec->AddStringId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500963 rec->AddU1(t);
964 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700965 } else if (c->IsArrayClass()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800966 mirror::Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500967 uint32_t length = aobj->GetLength();
968
969 if (obj->IsObjectArray()) {
970 // obj is an object array.
971 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
972
Ian Rogersef7d42f2014-01-06 12:55:46 -0800973 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500974 rec->AddU4(StackTraceSerialNumber(obj));
975 rec->AddU4(length);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800976 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500977
978 // Dump the elements, which are always objects or NULL.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800979 rec->AddIdList(aobj->AsObjectArray<mirror::Object>());
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500980 } else {
981 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700982 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500983
984 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500985 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500986
Ian Rogersef7d42f2014-01-06 12:55:46 -0800987 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500988 rec->AddU4(StackTraceSerialNumber(obj));
989 rec->AddU4(length);
990 rec->AddU1(t);
991
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500992 // Dump the raw, packed element values.
993 if (size == 1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800994 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500995 } else if (size == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800996 rec->AddU2List((const uint16_t*)aobj->GetRawData(sizeof(uint16_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500997 } else if (size == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800998 rec->AddU4List((const uint32_t*)aobj->GetRawData(sizeof(uint32_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500999 } else if (size == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001000 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001001 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001002 }
1003 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001004 // obj is an instance object.
1005 rec->AddU1(HPROF_INSTANCE_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001006 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001007 rec->AddU4(StackTraceSerialNumber(obj));
Ian Rogersef7d42f2014-01-06 12:55:46 -08001008 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001009
1010 // Reserve some space for the length of the instance data, which we won't
1011 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001012 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001013 rec->AddU4(0x77777777);
1014
1015 // Write the instance data; fields for this class, followed by super class fields,
1016 // and so on. Don't write the klass or monitor fields of Object.class.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001017 mirror::Class* sclass = c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001018 FieldHelper fh;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001019 while (!sclass->IsObjectClass()) {
1020 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001021 for (int i = 0; i < ifieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001022 mirror::ArtField* f = sclass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001023 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001024 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001025 SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001026 if (size == 1) {
1027 rec->AddU1(f->Get32(obj));
1028 } else if (size == 2) {
1029 rec->AddU2(f->Get32(obj));
1030 } else if (size == 4) {
1031 rec->AddU4(f->Get32(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001032 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001033 CHECK_EQ(size, 8U);
1034 rec->AddU8(f->Get64(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001035 }
1036 }
1037
1038 sclass = sclass->GetSuperClass();
1039 }
1040
1041 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001042 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001043 }
1044 }
1045
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001046 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001047 return 0;
1048}
1049
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001050void Hprof::VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001051 static const HprofHeapTag xlate[] = {
1052 HPROF_ROOT_UNKNOWN,
1053 HPROF_ROOT_JNI_GLOBAL,
1054 HPROF_ROOT_JNI_LOCAL,
1055 HPROF_ROOT_JAVA_FRAME,
1056 HPROF_ROOT_NATIVE_STACK,
1057 HPROF_ROOT_STICKY_CLASS,
1058 HPROF_ROOT_THREAD_BLOCK,
1059 HPROF_ROOT_MONITOR_USED,
1060 HPROF_ROOT_THREAD_OBJECT,
1061 HPROF_ROOT_INTERNED_STRING,
1062 HPROF_ROOT_FINALIZING,
1063 HPROF_ROOT_DEBUGGER,
1064 HPROF_ROOT_REFERENCE_CLEANUP,
1065 HPROF_ROOT_VM_INTERNAL,
1066 HPROF_ROOT_JNI_MONITOR,
1067 };
Jesse Wilson0b075f12011-11-09 10:57:41 -05001068 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1069 if (obj == NULL) {
1070 return;
1071 }
1072 gc_scan_state_ = xlate[type];
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001073 gc_thread_serial_number_ = thread_id;
Jesse Wilson0b075f12011-11-09 10:57:41 -05001074 MarkRootObject(obj, 0);
1075 gc_scan_state_ = 0;
1076 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001077}
1078
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001079// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1080// sent directly to DDMS.
1081// If "fd" is >= 0, the output will be written to that file descriptor.
1082// Otherwise, "filename" is used to create an output file.
1083void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1084 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001085
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001087 Hprof hprof(filename, fd, direct_to_ddms);
1088 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001090}
1091
1092} // namespace hprof
1093
1094} // namespace art