blob: 8c0f3fb39d0b6b556a0a65e101045bc2a78ad49e [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',
154 HPROF_HEAP_APP = 'A'
155};
156
157enum HprofBasicType {
158 hprof_basic_object = 2,
159 hprof_basic_boolean = 4,
160 hprof_basic_char = 5,
161 hprof_basic_float = 6,
162 hprof_basic_double = 7,
163 hprof_basic_byte = 8,
164 hprof_basic_short = 9,
165 hprof_basic_int = 10,
166 hprof_basic_long = 11,
167};
168
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169typedef uint32_t HprofStringId;
170typedef uint32_t HprofClassObjectId;
Elliott Hughes622a6982012-06-08 17:58:54 -0700171
172// Represents a top-level hprof record, whose serialized format is:
173// U1 TAG: denoting the type of the record
174// U4 TIME: number of microseconds since the time stamp in the header
175// U4 LENGTH: number of bytes that follow this uint32_t field and belong to this record
176// U1* BODY: as many bytes as specified in the above uint32_t field
177class HprofRecord {
178 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 HprofRecord() : alloc_length_(128), fp_(nullptr), tag_(0), time_(0), length_(0), dirty_(false) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700180 body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700181 }
182
183 ~HprofRecord() {
184 free(body_);
185 }
186
187 int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) {
188 int rc = Flush();
189 if (rc != 0) {
190 return rc;
191 }
192
193 fp_ = fp;
194 tag_ = tag;
195 time_ = time;
196 length_ = 0;
197 dirty_ = true;
198 return 0;
199 }
200
201 int Flush() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700202 if (dirty_) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700203 unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)];
Elliott Hughes622a6982012-06-08 17:58:54 -0700204
205 headBuf[0] = tag_;
206 U4_TO_BUF_BE(headBuf, 1, time_);
207 U4_TO_BUF_BE(headBuf, 5, length_);
208
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700209 int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700210 if (nb != sizeof(headBuf)) {
211 return UNIQUE_ERROR;
212 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700213 nb = fwrite(body_, 1, length_, fp_);
Brian Carlstrom2d888622013-07-18 17:02:00 -0700214 if (nb != static_cast<int>(length_)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700215 return UNIQUE_ERROR;
216 }
217
218 dirty_ = false;
219 }
220 // TODO if we used less than half (or whatever) of allocLen, shrink the buffer.
221 return 0;
222 }
223
224 int AddU1(uint8_t value) {
225 int err = GuaranteeRecordAppend(1);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700227 return err;
228 }
229
230 body_[length_++] = value;
231 return 0;
232 }
233
234 int AddU2(uint16_t value) {
235 return AddU2List(&value, 1);
236 }
237
238 int AddU4(uint32_t value) {
239 return AddU4List(&value, 1);
240 }
241
242 int AddU8(uint64_t value) {
243 return AddU8List(&value, 1);
244 }
245
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 int AddObjectId(const mirror::Object* value) {
247 return AddU4(PointerToLowMemUInt32(value));
248 }
249
250 // The ID for the synthetic object generated to account for class static overhead.
251 int AddClassStaticsId(const mirror::Class* value) {
252 return AddU4(1 | PointerToLowMemUInt32(value));
253 }
254
255 int AddJniGlobalRefId(jobject value) {
256 return AddU4(PointerToLowMemUInt32(value));
257 }
258
259 int AddClassId(HprofClassObjectId value) {
260 return AddU4(value);
261 }
262
263 int AddStringId(HprofStringId value) {
264 return AddU4(value);
Elliott Hughes622a6982012-06-08 17:58:54 -0700265 }
266
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700267 int AddU1List(const uint8_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700268 int err = GuaranteeRecordAppend(numValues);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700270 return err;
271 }
272
273 memcpy(body_ + length_, values, numValues);
274 length_ += numValues;
275 return 0;
276 }
277
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700278 int AddU2List(const uint16_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700279 int err = GuaranteeRecordAppend(numValues * 2);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800280 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700281 return err;
282 }
283
284 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700285 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700286 U2_TO_BUF_BE(insert, 0, *values++);
287 insert += sizeof(*values);
288 }
289 length_ += numValues * 2;
290 return 0;
291 }
292
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700293 int AddU4List(const uint32_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700294 int err = GuaranteeRecordAppend(numValues * 4);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 if (UNLIKELY(err != 0)) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700296 return err;
297 }
298
299 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700300 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700301 U4_TO_BUF_BE(insert, 0, *values++);
302 insert += sizeof(*values);
303 }
304 length_ += numValues * 4;
305 return 0;
306 }
307
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700308 void UpdateU4(size_t offset, uint32_t new_value) {
309 U4_TO_BUF_BE(body_, offset, new_value);
310 }
311
312 int AddU8List(const uint64_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700313 int err = GuaranteeRecordAppend(numValues * 8);
314 if (err != 0) {
315 return err;
316 }
317
318 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700319 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700320 U8_TO_BUF_BE(insert, 0, *values++);
321 insert += sizeof(*values);
322 }
323 length_ += numValues * 8;
324 return 0;
325 }
326
Ian Rogersef7d42f2014-01-06 12:55:46 -0800327 int AddIdList(mirror::ObjectArray<mirror::Object>* values)
328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
329 int32_t length = values->GetLength();
330 for (int32_t i = 0; i < length; ++i) {
331 int err = AddObjectId(values->GetWithoutChecks(i));
332 if (UNLIKELY(err != 0)) {
333 return err;
334 }
335 }
336 return 0;
Elliott Hughes622a6982012-06-08 17:58:54 -0700337 }
338
339 int AddUtf8String(const char* str) {
340 // The terminating NUL character is NOT written.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700341 return AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700342 }
343
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700344 size_t Size() const {
345 return length_;
346 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700347
348 private:
349 int GuaranteeRecordAppend(size_t nmore) {
350 size_t minSize = length_ + nmore;
351 if (minSize > alloc_length_) {
352 size_t newAllocLen = alloc_length_ * 2;
353 if (newAllocLen < minSize) {
354 newAllocLen = alloc_length_ + nmore + nmore/2;
355 }
356 unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen);
357 if (newBody != NULL) {
358 body_ = newBody;
359 alloc_length_ = newAllocLen;
360 } else {
361 // TODO: set an error flag so future ops will fail
362 return UNIQUE_ERROR;
363 }
364 }
365
366 CHECK_LE(length_ + nmore, alloc_length_);
367 return 0;
368 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700369
370 size_t alloc_length_;
371 unsigned char* body_;
372
373 FILE* fp_;
374 uint8_t tag_;
375 uint32_t time_;
376 size_t length_;
377 bool dirty_;
378
379 DISALLOW_COPY_AND_ASSIGN(HprofRecord);
Elliott Hughes622a6982012-06-08 17:58:54 -0700380};
381
382class Hprof {
383 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700384 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
385 : filename_(output_filename),
386 fd_(fd),
387 direct_to_ddms_(direct_to_ddms),
388 start_ns_(NanoTime()),
389 current_record_(),
390 gc_thread_serial_number_(0),
391 gc_scan_state_(0),
392 current_heap_(HPROF_HEAP_DEFAULT),
393 objects_in_segment_(0),
394 header_fp_(NULL),
395 header_data_ptr_(NULL),
396 header_data_size_(0),
397 body_fp_(NULL),
398 body_data_ptr_(NULL),
399 body_data_size_(0),
400 next_string_id_(0x400000) {
401 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Elliott Hughes622a6982012-06-08 17:58:54 -0700402
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700403 header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_);
404 if (header_fp_ == NULL) {
405 PLOG(FATAL) << "header open_memstream failed";
406 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700407
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700408 body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_);
409 if (body_fp_ == NULL) {
410 PLOG(FATAL) << "body open_memstream failed";
411 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500412 }
413
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700414 ~Hprof() {
415 if (header_fp_ != NULL) {
416 fclose(header_fp_);
417 }
418 if (body_fp_ != NULL) {
419 fclose(body_fp_);
420 }
421 free(header_data_ptr_);
422 free(body_data_ptr_);
423 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500424
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700425 void Dump()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700426 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
427 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700428 // Walk the roots and the heap.
429 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800430 Runtime::Current()->VisitRoots(RootVisitor, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700431 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700432 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700433 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800434 Runtime::Current()->GetHeap()->VisitObjects(VisitObjectCallback, this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700435 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700436 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
437 current_record_.Flush();
438 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500439
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700440 // Write the header.
441 WriteFixedHeader();
442 // Write the string and class tables, and any stack traces, to the header.
443 // (jhat requires that these appear before any of the data in the body that refers to them.)
444 WriteStringTable();
445 WriteClassTable();
446 WriteStackTraces();
447 current_record_.Flush();
448 fflush(header_fp_);
449
450 bool okay = true;
451 if (direct_to_ddms_) {
452 // Send the data off to DDMS.
453 iovec iov[2];
454 iov[0].iov_base = header_data_ptr_;
455 iov[0].iov_len = header_data_size_;
456 iov[1].iov_base = body_data_ptr_;
457 iov[1].iov_len = body_data_size_;
458 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
459 } else {
460 // Where exactly are we writing to?
461 int out_fd;
462 if (fd_ >= 0) {
463 out_fd = dup(fd_);
464 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800465 ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700466 return;
467 }
468 } else {
469 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
470 if (out_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
472 strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700473 return;
474 }
475 }
476
Ian Rogers700a4022014-05-19 16:49:03 -0700477 std::unique_ptr<File> file(new File(out_fd, filename_));
Ian Rogers50b35e22012-10-04 10:09:15 -0700478 okay = file->WriteFully(header_data_ptr_, header_data_size_) &&
479 file->WriteFully(body_data_ptr_, body_data_size_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700480 if (!okay) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700481 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
482 filename_.c_str(), strerror(errno)));
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 ThrowRuntimeException("%s", msg.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700484 LOG(ERROR) << msg;
485 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700486 }
487
488 // Throw out a log message for the benefit of "runhat".
489 if (okay) {
490 uint64_t duration = NanoTime() - start_ns_;
Ian Rogers50b35e22012-10-04 10:09:15 -0700491 LOG(INFO) << "hprof: heap dump completed ("
492 << PrettySize(header_data_size_ + body_data_size_ + 1023)
493 << ") in " << PrettyDuration(duration);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700494 }
495 }
496
497 private:
Mathieu Chartier815873e2014-02-13 18:02:13 -0800498 static void RootVisitor(mirror::Object** obj, void* arg, uint32_t thread_id, RootType root_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800500 DCHECK(arg != nullptr);
501 DCHECK(obj != nullptr);
502 DCHECK(*obj != nullptr);
503 reinterpret_cast<Hprof*>(arg)->VisitRoot(*obj, thread_id, root_type);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700504 }
505
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800506 static void VisitObjectCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800508 DCHECK(obj != NULL);
509 DCHECK(arg != NULL);
510 reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700511 }
512
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800513 void VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type)
514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700515
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516 int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700517
518 void Finish() {
519 }
520
Ian Rogersb726dcb2012-09-05 08:57:23 -0700521 int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700522 HprofRecord* rec = &current_record_;
523 uint32_t nextSerialNumber = 1;
524
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525 for (mirror::Class* c : classes_) {
526 CHECK(c != nullptr);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700527
528 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529 if (UNLIKELY(err != 0)) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700530 return err;
531 }
532
533 // LOAD CLASS format:
534 // U4: class serial number (always > 0)
535 // ID: class object ID. We use the address of the class object structure as its ID.
536 // U4: stack trace serial number
537 // ID: class name string ID
538 rec->AddU4(nextSerialNumber++);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800539 rec->AddObjectId(c);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700540 rec->AddU4(HPROF_NULL_STACK_TRACE);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800541 rec->AddStringId(LookupClassNameId(c));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700542 }
543
544 return 0;
545 }
546
547 int WriteStringTable() {
548 HprofRecord* rec = &current_record_;
549
Ian Rogersef7d42f2014-01-06 12:55:46 -0800550 for (std::pair<std::string, HprofStringId> p : strings_) {
551 const std::string& string = p.first;
552 size_t id = p.second;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700553
554 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
555 if (err != 0) {
556 return err;
557 }
558
559 // STRING format:
560 // ID: ID for this string
561 // U1*: UTF8 characters for string (NOT NULL terminated)
562 // (the record format encodes the length)
563 err = rec->AddU4(id);
564 if (err != 0) {
565 return err;
566 }
567 err = rec->AddUtf8String(string.c_str());
568 if (err != 0) {
569 return err;
570 }
571 }
572
573 return 0;
574 }
575
576 void StartNewHeapDumpSegment() {
577 // This flushes the old segment and starts a new one.
578 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
579 objects_in_segment_ = 0;
580
581 // Starting a new HEAP_DUMP resets the heap to default.
582 current_heap_ = HPROF_HEAP_DEFAULT;
583 }
584
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585 int MarkRootObject(const mirror::Object* obj, jobject jniObj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700586
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
588 if (c == nullptr) {
589 // c is the superclass of java.lang.Object or a primitive.
590 return 0;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700591 }
592
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 {
594 auto result = classes_.insert(c);
595 const mirror::Class* present = *result.first;
596 CHECK_EQ(present, c);
597 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700598
599 // Make sure that we've assigned a string ID for this class' name
600 LookupClassNameId(c);
601
Ian Rogersef7d42f2014-01-06 12:55:46 -0800602 HprofClassObjectId result = PointerToLowMemUInt32(c);
603 return result;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700604 }
605
Ian Rogersef7d42f2014-01-06 12:55:46 -0800606 HprofStringId LookupStringId(mirror::String* string) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700607 return LookupStringId(string->ToModifiedUtf8());
608 }
609
610 HprofStringId LookupStringId(const char* string) {
611 return LookupStringId(std::string(string));
612 }
613
614 HprofStringId LookupStringId(const std::string& string) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800615 auto it = strings_.find(string);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700616 if (it != strings_.end()) {
617 return it->second;
618 }
619 HprofStringId id = next_string_id_++;
620 strings_.Put(string, id);
621 return id;
622 }
623
Ian Rogersef7d42f2014-01-06 12:55:46 -0800624 HprofStringId LookupClassNameId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700625 return LookupStringId(PrettyDescriptor(c));
626 }
627
628 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700629 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500630 unsigned char buf[4];
631
632 // Write the file header.
633 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700634 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500635
Calin Juravle32805172014-07-04 16:24:03 +0100636 // U4: size of identifiers. We're using addresses as IDs and our heap references are stored
637 // as uint32_t.
638 // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
639 COMPILE_ASSERT(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t),
640 UnexpectedHeapReferenceSize);
641 U4_TO_BUF_BE(buf, 0, sizeof(uint32_t));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700642 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500643
644 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700645 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500646 uint64_t nowMs;
647 if (gettimeofday(&now, NULL) < 0) {
648 nowMs = 0;
649 } else {
650 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
651 }
652
653 // U4: high word of the 64-bit time.
654 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700655 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500656
657 // U4: low word of the 64-bit time.
658 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700659 fwrite(buf, 1, sizeof(uint32_t), header_fp_); // xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500660 }
661
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700662 void WriteStackTraces() {
663 // Write a dummy stack trace record so the analysis tools don't freak out.
664 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
665 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
666 current_record_.AddU4(HPROF_NULL_THREAD);
667 current_record_.AddU4(0); // no frames
668 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500669
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700670 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
671 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
672 // only be used for debug messages.
673 std::string filename_;
674 int fd_;
675 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500676
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700677 uint64_t start_ns_;
678
679 HprofRecord current_record_;
680
681 uint32_t gc_thread_serial_number_;
682 uint8_t gc_scan_state_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700683 HprofHeapId current_heap_; // Which heap we're currently dumping.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700684 size_t objects_in_segment_;
685
686 FILE* header_fp_;
687 char* header_data_ptr_;
688 size_t header_data_size_;
689
690 FILE* body_fp_;
691 char* body_data_ptr_;
692 size_t body_data_size_;
693
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 std::set<mirror::Class*> classes_;
695 HprofStringId next_string_id_;
696 SafeMap<std::string, HprofStringId> strings_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700697
698 DISALLOW_COPY_AND_ASSIGN(Hprof);
699};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500700
701#define OBJECTS_PER_SEGMENT ((size_t)128)
702#define BYTES_PER_SEGMENT ((size_t)4096)
703
Ian Rogersef7d42f2014-01-06 12:55:46 -0800704// The static field-name for the synthetic object generated to account for class static overhead.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500705#define STATIC_OVERHEAD_NAME "$staticOverhead"
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500706
Elliott Hughes622a6982012-06-08 17:58:54 -0700707static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500708 char c = sig[0];
709 HprofBasicType ret;
710 size_t size;
711
712 switch (c) {
713 case '[':
714 case 'L': ret = hprof_basic_object; size = 4; break;
715 case 'Z': ret = hprof_basic_boolean; size = 1; break;
716 case 'C': ret = hprof_basic_char; size = 2; break;
717 case 'F': ret = hprof_basic_float; size = 4; break;
718 case 'D': ret = hprof_basic_double; size = 8; break;
719 case 'B': ret = hprof_basic_byte; size = 1; break;
720 case 'S': ret = hprof_basic_short; size = 2; break;
721 default: CHECK(false);
722 case 'I': ret = hprof_basic_int; size = 4; break;
723 case 'J': ret = hprof_basic_long; size = 8; break;
724 }
725
726 if (sizeOut != NULL) {
727 *sizeOut = size;
728 }
729
730 return ret;
731}
732
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700733static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500734 HprofBasicType ret;
735 size_t size;
736
737 switch (prim) {
738 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
739 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
740 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
741 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
742 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
743 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
744 default: CHECK(false);
745 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
746 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
747 }
748
749 if (sizeOut != NULL) {
750 *sizeOut = size;
751 }
752
753 return ret;
754}
755
756// Always called when marking objects, but only does
757// something when ctx->gc_scan_state_ is non-zero, which is usually
758// only true when marking the root set or unreachable
759// objects. Used to add rootset references to obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800760int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700761 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500762 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
763
764 if (heapTag == 0) {
765 return 0;
766 }
767
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700768 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
769 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500770 }
771
772 switch (heapTag) {
773 // ID: object ID
774 case HPROF_ROOT_UNKNOWN:
775 case HPROF_ROOT_STICKY_CLASS:
776 case HPROF_ROOT_MONITOR_USED:
777 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500778 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500779 case HPROF_ROOT_VM_INTERNAL:
780 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800781 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500782 break;
783
784 // ID: object ID
785 // ID: JNI global ref ID
786 case HPROF_ROOT_JNI_GLOBAL:
787 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800788 rec->AddObjectId(obj);
789 rec->AddJniGlobalRefId(jniObj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500790 break;
791
792 // ID: object ID
793 // U4: thread serial number
794 // U4: frame number in stack trace (-1 for empty)
795 case HPROF_ROOT_JNI_LOCAL:
796 case HPROF_ROOT_JNI_MONITOR:
797 case HPROF_ROOT_JAVA_FRAME:
798 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800799 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500800 rec->AddU4(gc_thread_serial_number_);
801 rec->AddU4((uint32_t)-1);
802 break;
803
804 // ID: object ID
805 // U4: thread serial number
806 case HPROF_ROOT_NATIVE_STACK:
807 case HPROF_ROOT_THREAD_BLOCK:
808 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500810 rec->AddU4(gc_thread_serial_number_);
811 break;
812
813 // ID: thread object ID
814 // U4: thread serial number
815 // U4: stack trace serial number
816 case HPROF_ROOT_THREAD_OBJECT:
817 rec->AddU1(heapTag);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800818 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500819 rec->AddU4(gc_thread_serial_number_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700820 rec->AddU4((uint32_t)-1); // xxx
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500821 break;
822
Elliott Hughes73e66f72012-05-09 09:34:45 -0700823 case HPROF_CLASS_DUMP:
824 case HPROF_INSTANCE_DUMP:
825 case HPROF_OBJECT_ARRAY_DUMP:
826 case HPROF_PRIMITIVE_ARRAY_DUMP:
827 case HPROF_HEAP_DUMP_INFO:
828 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
829 // Ignored.
830 break;
831
832 case HPROF_ROOT_FINALIZING:
833 case HPROF_ROOT_REFERENCE_CLEANUP:
834 case HPROF_UNREACHABLE:
835 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500836 break;
837 }
838
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700839 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700840 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500841}
842
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843static int StackTraceSerialNumber(const mirror::Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500844 return HPROF_NULL_STACK_TRACE;
845}
846
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800847int Hprof::DumpHeapObject(mirror::Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700848 HprofRecord* rec = &current_record_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700849 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500850
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700851 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
852 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500853 }
854
855 if (desiredHeap != current_heap_) {
856 HprofStringId nameId;
857
858 // This object is in a different heap than the current one.
859 // Emit a HEAP_DUMP_INFO tag to change heaps.
860 rec->AddU1(HPROF_HEAP_DUMP_INFO);
861 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
862 switch (desiredHeap) {
863 case HPROF_HEAP_APP:
864 nameId = LookupStringId("app");
865 break;
866 case HPROF_HEAP_ZYGOTE:
867 nameId = LookupStringId("zygote");
868 break;
869 default:
870 // Internal error
871 LOG(ERROR) << "Unexpected desiredHeap";
872 nameId = LookupStringId("<ILLEGAL>");
873 break;
874 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800875 rec->AddStringId(nameId);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500876 current_heap_ = desiredHeap;
877 }
878
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800879 mirror::Class* c = obj->GetClass();
Elliott Hughese84278b2012-03-22 10:06:53 -0700880 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500881 // This object will bother HprofReader, because it has a NULL
882 // class, so just don't dump it. It could be
883 // gDvm.unlinkedJavaLangClass or it could be an object just
884 // allocated which hasn't been initialized yet.
885 } else {
886 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 mirror::Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500888 // obj is a ClassObject.
889 size_t sFieldCount = thisClass->NumStaticFields();
890 if (sFieldCount != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800891 int byteLength = sFieldCount * sizeof(JValue); // TODO bogus; fields are packed
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500892 // Create a byte array to reflect the allocation of the
893 // StaticField array at the end of this class.
894 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800895 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500896 rec->AddU4(StackTraceSerialNumber(obj));
897 rec->AddU4(byteLength);
898 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700899 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500900 rec->AddU1(0);
901 }
902 }
903
904 rec->AddU1(HPROF_CLASS_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800905 rec->AddClassId(LookupClassId(thisClass));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500906 rec->AddU4(StackTraceSerialNumber(thisClass));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800907 rec->AddClassId(LookupClassId(thisClass->GetSuperClass()));
908 rec->AddObjectId(thisClass->GetClassLoader());
909 rec->AddObjectId(nullptr); // no signer
910 rec->AddObjectId(nullptr); // no prot domain
911 rec->AddObjectId(nullptr); // reserved
912 rec->AddObjectId(nullptr); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800913 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500914 // ClassObjects have their static fields appended, so aren't all the same size.
915 // But they're at least this size.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700916 rec->AddU4(sizeof(mirror::Class)); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500917 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
918 rec->AddU4(0);
919 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700920 rec->AddU4(thisClass->GetObjectSize()); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500921 }
922
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700923 rec->AddU2(0); // empty const pool
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500924
925 // Static fields
926 if (sFieldCount == 0) {
927 rec->AddU2((uint16_t)0);
928 } else {
929 rec->AddU2((uint16_t)(sFieldCount+1));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800930 rec->AddStringId(LookupStringId(STATIC_OVERHEAD_NAME));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500931 rec->AddU1(hprof_basic_object);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800932 rec->AddClassStaticsId(thisClass);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500933
934 for (size_t i = 0; i < sFieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700935 mirror::ArtField* f = thisClass->GetStaticField(i);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500936
937 size_t size;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700938 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
939 rec->AddStringId(LookupStringId(f->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);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700960 HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), NULL);
961 rec->AddStringId(LookupStringId(f->GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500962 rec->AddU1(t);
963 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700964 } else if (c->IsArrayClass()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800965 mirror::Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500966 uint32_t length = aobj->GetLength();
967
968 if (obj->IsObjectArray()) {
969 // obj is an object array.
970 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
971
Ian Rogersef7d42f2014-01-06 12:55:46 -0800972 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500973 rec->AddU4(StackTraceSerialNumber(obj));
974 rec->AddU4(length);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800975 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500976
977 // Dump the elements, which are always objects or NULL.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800978 rec->AddIdList(aobj->AsObjectArray<mirror::Object>());
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500979 } else {
980 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700981 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500982
983 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500984 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500985
Ian Rogersef7d42f2014-01-06 12:55:46 -0800986 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500987 rec->AddU4(StackTraceSerialNumber(obj));
988 rec->AddU4(length);
989 rec->AddU1(t);
990
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500991 // Dump the raw, packed element values.
992 if (size == 1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800993 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500994 } else if (size == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800995 rec->AddU2List((const uint16_t*)aobj->GetRawData(sizeof(uint16_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500996 } else if (size == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800997 rec->AddU4List((const uint32_t*)aobj->GetRawData(sizeof(uint32_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500998 } else if (size == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800999 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t), 0), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001000 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001001 }
1002 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001003 // obj is an instance object.
1004 rec->AddU1(HPROF_INSTANCE_DUMP);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001005 rec->AddObjectId(obj);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001006 rec->AddU4(StackTraceSerialNumber(obj));
Ian Rogersef7d42f2014-01-06 12:55:46 -08001007 rec->AddClassId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001008
1009 // Reserve some space for the length of the instance data, which we won't
1010 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001011 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001012 rec->AddU4(0x77777777);
1013
1014 // Write the instance data; fields for this class, followed by super class fields,
1015 // and so on. Don't write the klass or monitor fields of Object.class.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001016 mirror::Class* sclass = c;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001017 while (!sclass->IsObjectClass()) {
1018 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001019 for (int i = 0; i < ifieldCount; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001020 mirror::ArtField* f = sclass->GetInstanceField(i);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001021 size_t size;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001022 SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001023 if (size == 1) {
1024 rec->AddU1(f->Get32(obj));
1025 } else if (size == 2) {
1026 rec->AddU2(f->Get32(obj));
1027 } else if (size == 4) {
1028 rec->AddU4(f->Get32(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001029 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001030 CHECK_EQ(size, 8U);
1031 rec->AddU8(f->Get64(obj));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001032 }
1033 }
1034
1035 sclass = sclass->GetSuperClass();
1036 }
1037
1038 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001039 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001040 }
1041 }
1042
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001043 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001044 return 0;
1045}
1046
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001047void Hprof::VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001048 static const HprofHeapTag xlate[] = {
1049 HPROF_ROOT_UNKNOWN,
1050 HPROF_ROOT_JNI_GLOBAL,
1051 HPROF_ROOT_JNI_LOCAL,
1052 HPROF_ROOT_JAVA_FRAME,
1053 HPROF_ROOT_NATIVE_STACK,
1054 HPROF_ROOT_STICKY_CLASS,
1055 HPROF_ROOT_THREAD_BLOCK,
1056 HPROF_ROOT_MONITOR_USED,
1057 HPROF_ROOT_THREAD_OBJECT,
1058 HPROF_ROOT_INTERNED_STRING,
1059 HPROF_ROOT_FINALIZING,
1060 HPROF_ROOT_DEBUGGER,
1061 HPROF_ROOT_REFERENCE_CLEANUP,
1062 HPROF_ROOT_VM_INTERNAL,
1063 HPROF_ROOT_JNI_MONITOR,
1064 };
Jesse Wilson0b075f12011-11-09 10:57:41 -05001065 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1066 if (obj == NULL) {
1067 return;
1068 }
1069 gc_scan_state_ = xlate[type];
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001070 gc_thread_serial_number_ = thread_id;
Jesse Wilson0b075f12011-11-09 10:57:41 -05001071 MarkRootObject(obj, 0);
1072 gc_scan_state_ = 0;
1073 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001074}
1075
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001076// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1077// sent directly to DDMS.
1078// If "fd" is >= 0, the output will be written to that file descriptor.
1079// Otherwise, "filename" is used to create an output file.
1080void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1081 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001082
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001084 Hprof hprof(filename, fd, direct_to_ddms);
1085 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001087}
1088
1089} // namespace hprof
1090
1091} // namespace art