blob: 30695815227825e1dd5fbf089e8685e6c27a7907 [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"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070055#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070056#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057#include "scoped_thread_state_change.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070058#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040059
60namespace art {
61
62namespace hprof {
63
Elliott Hughes622a6982012-06-08 17:58:54 -070064#define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
Jesse Wilson0c54ac12011-11-09 15:14:05 -050065
Elliott Hughes622a6982012-06-08 17:58:54 -070066#define HPROF_TIME 0
67#define HPROF_NULL_STACK_TRACE 0
68#define HPROF_NULL_THREAD 0
69
70#define U2_TO_BUF_BE(buf, offset, value) \
71 do { \
72 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070073 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070074 uint16_t value_ = (uint16_t)(value); \
75 buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \
76 buf_[offset_ + 1] = (unsigned char)(value_ ); \
77 } while (0)
78
79#define U4_TO_BUF_BE(buf, offset, value) \
80 do { \
81 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070082 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070083 uint32_t value_ = (uint32_t)(value); \
84 buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \
85 buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \
86 buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \
87 buf_[offset_ + 3] = (unsigned char)(value_ ); \
88 } while (0)
89
90#define U8_TO_BUF_BE(buf, offset, value) \
91 do { \
92 unsigned char* buf_ = (unsigned char*)(buf); \
Brian Carlstrom2d888622013-07-18 17:02:00 -070093 int offset_ = static_cast<int>(offset); \
Elliott Hughes622a6982012-06-08 17:58:54 -070094 uint64_t value_ = (uint64_t)(value); \
95 buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \
96 buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \
97 buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \
98 buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \
99 buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \
100 buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \
101 buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \
102 buf_[offset_ + 7] = (unsigned char)(value_ ); \
103 } while (0)
104
105enum HprofTag {
106 HPROF_TAG_STRING = 0x01,
107 HPROF_TAG_LOAD_CLASS = 0x02,
108 HPROF_TAG_UNLOAD_CLASS = 0x03,
109 HPROF_TAG_STACK_FRAME = 0x04,
110 HPROF_TAG_STACK_TRACE = 0x05,
111 HPROF_TAG_ALLOC_SITES = 0x06,
112 HPROF_TAG_HEAP_SUMMARY = 0x07,
113 HPROF_TAG_START_THREAD = 0x0A,
114 HPROF_TAG_END_THREAD = 0x0B,
115 HPROF_TAG_HEAP_DUMP = 0x0C,
116 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
117 HPROF_TAG_HEAP_DUMP_END = 0x2C,
118 HPROF_TAG_CPU_SAMPLES = 0x0D,
119 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
120};
121
122// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
123enum HprofHeapTag {
124 // Traditional.
125 HPROF_ROOT_UNKNOWN = 0xFF,
126 HPROF_ROOT_JNI_GLOBAL = 0x01,
127 HPROF_ROOT_JNI_LOCAL = 0x02,
128 HPROF_ROOT_JAVA_FRAME = 0x03,
129 HPROF_ROOT_NATIVE_STACK = 0x04,
130 HPROF_ROOT_STICKY_CLASS = 0x05,
131 HPROF_ROOT_THREAD_BLOCK = 0x06,
132 HPROF_ROOT_MONITOR_USED = 0x07,
133 HPROF_ROOT_THREAD_OBJECT = 0x08,
134 HPROF_CLASS_DUMP = 0x20,
135 HPROF_INSTANCE_DUMP = 0x21,
136 HPROF_OBJECT_ARRAY_DUMP = 0x22,
137 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
138
139 // Android.
140 HPROF_HEAP_DUMP_INFO = 0xfe,
141 HPROF_ROOT_INTERNED_STRING = 0x89,
142 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
143 HPROF_ROOT_DEBUGGER = 0x8b,
144 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
145 HPROF_ROOT_VM_INTERNAL = 0x8d,
146 HPROF_ROOT_JNI_MONITOR = 0x8e,
147 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700148 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700149};
150
151enum HprofHeapId {
152 HPROF_HEAP_DEFAULT = 0,
153 HPROF_HEAP_ZYGOTE = 'Z',
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700154 HPROF_HEAP_APP = 'A',
155 HPROF_HEAP_IMAGE = 'I',
Elliott Hughes622a6982012-06-08 17:58:54 -0700156};
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);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800431 Runtime::Current()->VisitRoots(RootVisitor, this);
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
Andreas Gampe4303ba92014-11-06 01:00:46 -0800478 std::unique_ptr<File> file(new File(out_fd, filename_, true));
Ian Rogers50b35e22012-10-04 10:09:15 -0700479 okay = file->WriteFully(header_data_ptr_, header_data_size_) &&
Andreas Gampe4303ba92014-11-06 01:00:46 -0800480 file->WriteFully(body_data_ptr_, body_data_size_);
481 if (okay) {
482 okay = file->FlushCloseOrErase() == 0;
483 } else {
484 file->Erase();
485 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700486 if (!okay) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700487 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
488 filename_.c_str(), strerror(errno)));
Ian Rogers62d6c772013-02-27 08:32:07 -0800489 ThrowRuntimeException("%s", msg.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700490 LOG(ERROR) << msg;
491 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700492 }
493
494 // Throw out a log message for the benefit of "runhat".
495 if (okay) {
496 uint64_t duration = NanoTime() - start_ns_;
Ian Rogers50b35e22012-10-04 10:09:15 -0700497 LOG(INFO) << "hprof: heap dump completed ("
498 << PrettySize(header_data_size_ + body_data_size_ + 1023)
499 << ") in " << PrettyDuration(duration);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700500 }
501 }
502
503 private:
Mathieu Chartier815873e2014-02-13 18:02:13 -0800504 static void RootVisitor(mirror::Object** obj, void* arg, uint32_t thread_id, RootType root_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800506 DCHECK(arg != nullptr);
507 DCHECK(obj != nullptr);
508 DCHECK(*obj != nullptr);
509 reinterpret_cast<Hprof*>(arg)->VisitRoot(*obj, thread_id, root_type);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700510 }
511
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800512 static void VisitObjectCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800514 DCHECK(obj != NULL);
515 DCHECK(arg != NULL);
516 reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700517 }
518
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800519 void VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type)
520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700521
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700523
524 void Finish() {
525 }
526
Ian Rogersb726dcb2012-09-05 08:57:23 -0700527 int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700528 HprofRecord* rec = &current_record_;
529 uint32_t nextSerialNumber = 1;
530
Ian Rogersef7d42f2014-01-06 12:55:46 -0800531 for (mirror::Class* c : classes_) {
532 CHECK(c != nullptr);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700533
534 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800535 if (UNLIKELY(err != 0)) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700536 return err;
537 }
538
539 // LOAD CLASS format:
540 // U4: class serial number (always > 0)
541 // ID: class object ID. We use the address of the class object structure as its ID.
542 // U4: stack trace serial number
543 // ID: class name string ID
544 rec->AddU4(nextSerialNumber++);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800545 rec->AddObjectId(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700546 rec->AddU4(HPROF_NULL_STACK_TRACE);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800547 rec->AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700548 }
549
550 return 0;
551 }
552
553 int WriteStringTable() {
554 HprofRecord* rec = &current_record_;
555
Ian Rogersef7d42f2014-01-06 12:55:46 -0800556 for (std::pair<std::string, HprofStringId> p : strings_) {
557 const std::string& string = p.first;
558 size_t id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700559
560 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
561 if (err != 0) {
562 return err;
563 }
564
565 // STRING format:
566 // ID: ID for this string
567 // U1*: UTF8 characters for string (NOT NULL terminated)
568 // (the record format encodes the length)
569 err = rec->AddU4(id);
570 if (err != 0) {
571 return err;
572 }
573 err = rec->AddUtf8String(string.c_str());
574 if (err != 0) {
575 return err;
576 }
577 }
578
579 return 0;
580 }
581
582 void StartNewHeapDumpSegment() {
583 // This flushes the old segment and starts a new one.
584 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
585 objects_in_segment_ = 0;
586
587 // Starting a new HEAP_DUMP resets the heap to default.
588 current_heap_ = HPROF_HEAP_DEFAULT;
589 }
590
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 int MarkRootObject(const mirror::Object* obj, jobject jniObj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700592
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
594 if (c == nullptr) {
595 // c is the superclass of java.lang.Object or a primitive.
596 return 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700597 }
598
Ian Rogersef7d42f2014-01-06 12:55:46 -0800599 {
600 auto result = classes_.insert(c);
601 const mirror::Class* present = *result.first;
602 CHECK_EQ(present, c);
603 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700604
605 // Make sure that we've assigned a string ID for this class' name
606 LookupClassNameId(c);
607
Ian Rogersef7d42f2014-01-06 12:55:46 -0800608 HprofClassObjectId result = PointerToLowMemUInt32(c);
609 return result;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700610 }
611
Ian Rogersef7d42f2014-01-06 12:55:46 -0800612 HprofStringId LookupStringId(mirror::String* string) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700613 return LookupStringId(string->ToModifiedUtf8());
614 }
615
616 HprofStringId LookupStringId(const char* string) {
617 return LookupStringId(std::string(string));
618 }
619
620 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800621 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700622 if (it != strings_.end()) {
623 return it->second;
624 }
625 HprofStringId id = next_string_id_++;
626 strings_.Put(string, id);
627 return id;
628 }
629
Ian Rogersef7d42f2014-01-06 12:55:46 -0800630 HprofStringId LookupClassNameId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700631 return LookupStringId(PrettyDescriptor(c));
632 }
633
634 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700635 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500636 unsigned char buf[4];
637
638 // Write the file header.
639 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700640 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500641
Calin Juravle32805172014-07-04 16:24:03 +0100642 // U4: size of identifiers. We're using addresses as IDs and our heap references are stored
643 // as uint32_t.
644 // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800645 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t),
646 "Unexpected HeapReference size");
Calin Juravle32805172014-07-04 16:24:03 +0100647 U4_TO_BUF_BE(buf, 0, sizeof(uint32_t));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700648 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500649
650 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700651 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500652 uint64_t nowMs;
653 if (gettimeofday(&now, NULL) < 0) {
654 nowMs = 0;
655 } else {
656 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
657 }
658
659 // U4: high word of the 64-bit time.
660 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700661 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500662
663 // U4: low word of the 64-bit time.
664 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700665 fwrite(buf, 1, sizeof(uint32_t), header_fp_); // xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500666 }
667
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700668 void WriteStackTraces() {
669 // Write a dummy stack trace record so the analysis tools don't freak out.
670 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
671 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
672 current_record_.AddU4(HPROF_NULL_THREAD);
673 current_record_.AddU4(0); // no frames
674 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500675
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700676 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
677 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
678 // only be used for debug messages.
679 std::string filename_;
680 int fd_;
681 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500682
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700683 uint64_t start_ns_;
684
685 HprofRecord current_record_;
686
687 uint32_t gc_thread_serial_number_;
688 uint8_t gc_scan_state_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700689 HprofHeapId current_heap_; // Which heap we're currently dumping.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700690 size_t objects_in_segment_;
691
692 FILE* header_fp_;
693 char* header_data_ptr_;
694 size_t header_data_size_;
695
696 FILE* body_fp_;
697 char* body_data_ptr_;
698 size_t body_data_size_;
699
Ian Rogersef7d42f2014-01-06 12:55:46 -0800700 std::set<mirror::Class*> classes_;
701 HprofStringId next_string_id_;
702 SafeMap<std::string, HprofStringId> strings_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700703
704 DISALLOW_COPY_AND_ASSIGN(Hprof);
705};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500706
707#define OBJECTS_PER_SEGMENT ((size_t)128)
708#define BYTES_PER_SEGMENT ((size_t)4096)
709
Ian Rogersef7d42f2014-01-06 12:55:46 -0800710// The static field-name for the synthetic object generated to account for class static overhead.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500711#define STATIC_OVERHEAD_NAME "$staticOverhead"
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500712
Elliott Hughes622a6982012-06-08 17:58:54 -0700713static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500714 char c = sig[0];
715 HprofBasicType ret;
716 size_t size;
717
718 switch (c) {
719 case '[':
720 case 'L': ret = hprof_basic_object; size = 4; break;
721 case 'Z': ret = hprof_basic_boolean; size = 1; break;
722 case 'C': ret = hprof_basic_char; size = 2; break;
723 case 'F': ret = hprof_basic_float; size = 4; break;
724 case 'D': ret = hprof_basic_double; size = 8; break;
725 case 'B': ret = hprof_basic_byte; size = 1; break;
726 case 'S': ret = hprof_basic_short; size = 2; break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500727 case 'I': ret = hprof_basic_int; size = 4; break;
728 case 'J': ret = hprof_basic_long; size = 8; break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700729 default: LOG(FATAL) << "UNREACHABLE"; UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500730 }
731
732 if (sizeOut != NULL) {
733 *sizeOut = size;
734 }
735
736 return ret;
737}
738
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700739static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500740 HprofBasicType ret;
741 size_t size;
742
743 switch (prim) {
744 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
745 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
746 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
747 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
748 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
749 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500750 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
751 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700752 default: LOG(FATAL) << "UNREACHABLE"; UNREACHABLE();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500753 }
754
755 if (sizeOut != NULL) {
756 *sizeOut = size;
757 }
758
759 return ret;
760}
761
762// Always called when marking objects, but only does
763// something when ctx->gc_scan_state_ is non-zero, which is usually
764// only true when marking the root set or unreachable
765// objects. Used to add rootset references to obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800766int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700767 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500768 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
769
770 if (heapTag == 0) {
771 return 0;
772 }
773
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700774 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
775 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500776 }
777
778 switch (heapTag) {
779 // ID: object ID
780 case HPROF_ROOT_UNKNOWN:
781 case HPROF_ROOT_STICKY_CLASS:
782 case HPROF_ROOT_MONITOR_USED:
783 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500784 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500785 case HPROF_ROOT_VM_INTERNAL:
786 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800787 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500788 break;
789
790 // ID: object ID
791 // ID: JNI global ref ID
792 case HPROF_ROOT_JNI_GLOBAL:
793 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800794 rec->AddObjectId(obj);
795 rec->AddJniGlobalRefId(jniObj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500796 break;
797
798 // ID: object ID
799 // U4: thread serial number
800 // U4: frame number in stack trace (-1 for empty)
801 case HPROF_ROOT_JNI_LOCAL:
802 case HPROF_ROOT_JNI_MONITOR:
803 case HPROF_ROOT_JAVA_FRAME:
804 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800805 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500806 rec->AddU4(gc_thread_serial_number_);
807 rec->AddU4((uint32_t)-1);
808 break;
809
810 // ID: object ID
811 // U4: thread serial number
812 case HPROF_ROOT_NATIVE_STACK:
813 case HPROF_ROOT_THREAD_BLOCK:
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_);
817 break;
818
819 // ID: thread object ID
820 // U4: thread serial number
821 // U4: stack trace serial number
822 case HPROF_ROOT_THREAD_OBJECT:
823 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800824 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500825 rec->AddU4(gc_thread_serial_number_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700826 rec->AddU4((uint32_t)-1); // xxx
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500827 break;
828
Elliott Hughes73e66f72012-05-09 09:34:45 -0700829 case HPROF_CLASS_DUMP:
830 case HPROF_INSTANCE_DUMP:
831 case HPROF_OBJECT_ARRAY_DUMP:
832 case HPROF_PRIMITIVE_ARRAY_DUMP:
833 case HPROF_HEAP_DUMP_INFO:
834 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
835 // Ignored.
836 break;
837
838 case HPROF_ROOT_FINALIZING:
839 case HPROF_ROOT_REFERENCE_CLEANUP:
840 case HPROF_UNREACHABLE:
841 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500842 break;
843 }
844
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700845 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700846 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500847}
848
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800849static int StackTraceSerialNumber(const mirror::Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500850 return HPROF_NULL_STACK_TRACE;
851}
852
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853int Hprof::DumpHeapObject(mirror::Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700854 HprofRecord* rec = &current_record_;
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700855 gc::space::ContinuousSpace* space =
856 Runtime::Current()->GetHeap()->FindContinuousSpaceFromObject(obj, true);
857 HprofHeapId heap_type = HPROF_HEAP_APP;
858 if (space != nullptr) {
859 if (space->IsZygoteSpace()) {
860 heap_type = HPROF_HEAP_ZYGOTE;
861 } else if (space->IsImageSpace()) {
862 heap_type = HPROF_HEAP_IMAGE;
863 }
864 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700865 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
866 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500867 }
868
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700869 if (heap_type != current_heap_) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500870 HprofStringId nameId;
871
872 // This object is in a different heap than the current one.
873 // Emit a HEAP_DUMP_INFO tag to change heaps.
874 rec->AddU1(HPROF_HEAP_DUMP_INFO);
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700875 rec->AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type
876 switch (heap_type) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500877 case HPROF_HEAP_APP:
878 nameId = LookupStringId("app");
879 break;
880 case HPROF_HEAP_ZYGOTE:
881 nameId = LookupStringId("zygote");
882 break;
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700883 case HPROF_HEAP_IMAGE:
884 nameId = LookupStringId("image");
885 break;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500886 default:
887 // Internal error
888 LOG(ERROR) << "Unexpected desiredHeap";
889 nameId = LookupStringId("<ILLEGAL>");
890 break;
891 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800892 rec->AddStringId(nameId);
Mathieu Chartierae1ad002014-07-18 17:58:22 -0700893 current_heap_ = heap_type;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500894 }
895
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800896 mirror::Class* c = obj->GetClass();
Elliott Hughese84278b2012-03-22 10:06:53 -0700897 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500898 // This object will bother HprofReader, because it has a NULL
899 // class, so just don't dump it. It could be
900 // gDvm.unlinkedJavaLangClass or it could be an object just
901 // allocated which hasn't been initialized yet.
902 } else {
903 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800904 mirror::Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500905 // obj is a ClassObject.
906 size_t sFieldCount = thisClass->NumStaticFields();
907 if (sFieldCount != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800908 int byteLength = sFieldCount * sizeof(JValue); // TODO bogus; fields are packed
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500909 // Create a byte array to reflect the allocation of the
910 // StaticField array at the end of this class.
911 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800912 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500913 rec->AddU4(StackTraceSerialNumber(obj));
914 rec->AddU4(byteLength);
915 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700916 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500917 rec->AddU1(0);
918 }
919 }
920
921 rec->AddU1(HPROF_CLASS_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800922 rec->AddClassId(LookupClassId(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500923 rec->AddU4(StackTraceSerialNumber(thisClass));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800924 rec->AddClassId(LookupClassId(thisClass->GetSuperClass()));
925 rec->AddObjectId(thisClass->GetClassLoader());
926 rec->AddObjectId(nullptr); // no signer
927 rec->AddObjectId(nullptr); // no prot domain
928 rec->AddObjectId(nullptr); // reserved
929 rec->AddObjectId(nullptr); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800930 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500931 // ClassObjects have their static fields appended, so aren't all the same size.
932 // But they're at least this size.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700933 rec->AddU4(sizeof(mirror::Class)); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500934 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
935 rec->AddU4(0);
936 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700937 rec->AddU4(thisClass->GetObjectSize()); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500938 }
939
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700940 rec->AddU2(0); // empty const pool
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500941
942 // Static fields
943 if (sFieldCount == 0) {
944 rec->AddU2((uint16_t)0);
945 } else {
946 rec->AddU2((uint16_t)(sFieldCount+1));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800947 rec->AddStringId(LookupStringId(STATIC_OVERHEAD_NAME));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500948 rec->AddU1(hprof_basic_object);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800949 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500950
951 for (size_t i = 0; i < sFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700952 mirror::ArtField* f = thisClass->GetStaticField(i);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500953
954 size_t size;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700955 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
956 rec->AddStringId(LookupStringId(f->GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500957 rec->AddU1(t);
958 if (size == 1) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700959 rec->AddU1(static_cast<uint8_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500960 } else if (size == 2) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700961 rec->AddU2(static_cast<uint16_t>(f->Get32(thisClass)));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500962 } else if (size == 4) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700963 rec->AddU4(f->Get32(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500964 } else if (size == 8) {
Ian Rogersa3b59cb2013-03-19 19:50:20 -0700965 rec->AddU8(f->Get64(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500966 } else {
967 CHECK(false);
968 }
969 }
970 }
971
972 // Instance fields for this class (no superclass fields)
973 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
974 rec->AddU2((uint16_t)iFieldCount);
975 for (int i = 0; i < iFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700976 mirror::ArtField* f = thisClass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700977 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), NULL);
978 rec->AddStringId(LookupStringId(f->GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500979 rec->AddU1(t);
980 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700981 } else if (c->IsArrayClass()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800982 mirror::Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500983 uint32_t length = aobj->GetLength();
984
985 if (obj->IsObjectArray()) {
986 // obj is an object array.
987 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
988
Ian Rogersef7d42f2014-01-06 12:55:46 -0800989 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500990 rec->AddU4(StackTraceSerialNumber(obj));
991 rec->AddU4(length);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800992 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500993
994 // Dump the elements, which are always objects or NULL.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800995 rec->AddIdList(aobj->AsObjectArray<mirror::Object>());
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500996 } else {
997 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700998 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500999
1000 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001001 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001002
Ian Rogersef7d42f2014-01-06 12:55:46 -08001003 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001004 rec->AddU4(StackTraceSerialNumber(obj));
1005 rec->AddU4(length);
1006 rec->AddU1(t);
1007
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001008 // Dump the raw, packed element values.
1009 if (size == 1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001010 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001011 } else if (size == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001012 rec->AddU2List((const uint16_t*)aobj->GetRawData(sizeof(uint16_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001013 } else if (size == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001014 rec->AddU4List((const uint32_t*)aobj->GetRawData(sizeof(uint32_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001015 } else if (size == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001016 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001017 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001018 }
1019 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001020 // obj is an instance object.
1021 rec->AddU1(HPROF_INSTANCE_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001022 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001023 rec->AddU4(StackTraceSerialNumber(obj));
Ian Rogersef7d42f2014-01-06 12:55:46 -08001024 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001025
1026 // Reserve some space for the length of the instance data, which we won't
1027 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001028 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001029 rec->AddU4(0x77777777);
1030
1031 // Write the instance data; fields for this class, followed by super class fields,
1032 // and so on. Don't write the klass or monitor fields of Object.class.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001033 mirror::Class* sclass = c;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001034 while (!sclass->IsObjectClass()) {
1035 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001036 for (int i = 0; i < ifieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001037 mirror::ArtField* f = sclass->GetInstanceField(i);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001038 size_t size;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001039 SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001040 if (size == 1) {
1041 rec->AddU1(f->Get32(obj));
1042 } else if (size == 2) {
1043 rec->AddU2(f->Get32(obj));
1044 } else if (size == 4) {
1045 rec->AddU4(f->Get32(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001046 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001047 CHECK_EQ(size, 8U);
1048 rec->AddU8(f->Get64(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001049 }
1050 }
1051
1052 sclass = sclass->GetSuperClass();
1053 }
1054
1055 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001056 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001057 }
1058 }
1059
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001060 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001061 return 0;
1062}
1063
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001064void Hprof::VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001065 static const HprofHeapTag xlate[] = {
1066 HPROF_ROOT_UNKNOWN,
1067 HPROF_ROOT_JNI_GLOBAL,
1068 HPROF_ROOT_JNI_LOCAL,
1069 HPROF_ROOT_JAVA_FRAME,
1070 HPROF_ROOT_NATIVE_STACK,
1071 HPROF_ROOT_STICKY_CLASS,
1072 HPROF_ROOT_THREAD_BLOCK,
1073 HPROF_ROOT_MONITOR_USED,
1074 HPROF_ROOT_THREAD_OBJECT,
1075 HPROF_ROOT_INTERNED_STRING,
1076 HPROF_ROOT_FINALIZING,
1077 HPROF_ROOT_DEBUGGER,
1078 HPROF_ROOT_REFERENCE_CLEANUP,
1079 HPROF_ROOT_VM_INTERNAL,
1080 HPROF_ROOT_JNI_MONITOR,
1081 };
Jesse Wilson0b075f12011-11-09 10:57:41 -05001082 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1083 if (obj == NULL) {
1084 return;
1085 }
1086 gc_scan_state_ = xlate[type];
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001087 gc_thread_serial_number_ = thread_id;
Jesse Wilson0b075f12011-11-09 10:57:41 -05001088 MarkRootObject(obj, 0);
1089 gc_scan_state_ = 0;
1090 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001091}
1092
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001093// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1094// sent directly to DDMS.
1095// If "fd" is >= 0, the output will be written to that file descriptor.
1096// Otherwise, "filename" is used to create an output file.
1097void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1098 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001099
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001101 Hprof hprof(filename, fd, direct_to_ddms);
1102 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001104}
1105
1106} // namespace hprof
1107
1108} // namespace art