blob: 356c1fb4f6d4fbd252656c37845353027bda084f [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
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "class_linker.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040041#include "debugger.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070042#include "file.h"
43#include "globals.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080044#include "heap.h"
45#include "logging.h"
Jesse Wilson0c54ac12011-11-09 15:14:05 -050046#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080047#include "object_utils.h"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070048#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070049#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050#include "scoped_thread_state_change.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070051#include "space.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040052#include "stringprintf.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070053#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040054
55namespace art {
56
57namespace hprof {
58
Elliott Hughes622a6982012-06-08 17:58:54 -070059#define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
Jesse Wilson0c54ac12011-11-09 15:14:05 -050060
Elliott Hughes622a6982012-06-08 17:58:54 -070061#define HPROF_TIME 0
62#define HPROF_NULL_STACK_TRACE 0
63#define HPROF_NULL_THREAD 0
64
65#define U2_TO_BUF_BE(buf, offset, value) \
66 do { \
67 unsigned char* buf_ = (unsigned char*)(buf); \
68 int offset_ = (int)(offset); \
69 uint16_t value_ = (uint16_t)(value); \
70 buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \
71 buf_[offset_ + 1] = (unsigned char)(value_ ); \
72 } while (0)
73
74#define U4_TO_BUF_BE(buf, offset, value) \
75 do { \
76 unsigned char* buf_ = (unsigned char*)(buf); \
77 int offset_ = (int)(offset); \
78 uint32_t value_ = (uint32_t)(value); \
79 buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \
80 buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \
81 buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \
82 buf_[offset_ + 3] = (unsigned char)(value_ ); \
83 } while (0)
84
85#define U8_TO_BUF_BE(buf, offset, value) \
86 do { \
87 unsigned char* buf_ = (unsigned char*)(buf); \
88 int offset_ = (int)(offset); \
89 uint64_t value_ = (uint64_t)(value); \
90 buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \
91 buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \
92 buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \
93 buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \
94 buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \
95 buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \
96 buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \
97 buf_[offset_ + 7] = (unsigned char)(value_ ); \
98 } while (0)
99
100enum HprofTag {
101 HPROF_TAG_STRING = 0x01,
102 HPROF_TAG_LOAD_CLASS = 0x02,
103 HPROF_TAG_UNLOAD_CLASS = 0x03,
104 HPROF_TAG_STACK_FRAME = 0x04,
105 HPROF_TAG_STACK_TRACE = 0x05,
106 HPROF_TAG_ALLOC_SITES = 0x06,
107 HPROF_TAG_HEAP_SUMMARY = 0x07,
108 HPROF_TAG_START_THREAD = 0x0A,
109 HPROF_TAG_END_THREAD = 0x0B,
110 HPROF_TAG_HEAP_DUMP = 0x0C,
111 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
112 HPROF_TAG_HEAP_DUMP_END = 0x2C,
113 HPROF_TAG_CPU_SAMPLES = 0x0D,
114 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
115};
116
117// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
118enum HprofHeapTag {
119 // Traditional.
120 HPROF_ROOT_UNKNOWN = 0xFF,
121 HPROF_ROOT_JNI_GLOBAL = 0x01,
122 HPROF_ROOT_JNI_LOCAL = 0x02,
123 HPROF_ROOT_JAVA_FRAME = 0x03,
124 HPROF_ROOT_NATIVE_STACK = 0x04,
125 HPROF_ROOT_STICKY_CLASS = 0x05,
126 HPROF_ROOT_THREAD_BLOCK = 0x06,
127 HPROF_ROOT_MONITOR_USED = 0x07,
128 HPROF_ROOT_THREAD_OBJECT = 0x08,
129 HPROF_CLASS_DUMP = 0x20,
130 HPROF_INSTANCE_DUMP = 0x21,
131 HPROF_OBJECT_ARRAY_DUMP = 0x22,
132 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
133
134 // Android.
135 HPROF_HEAP_DUMP_INFO = 0xfe,
136 HPROF_ROOT_INTERNED_STRING = 0x89,
137 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
138 HPROF_ROOT_DEBUGGER = 0x8b,
139 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
140 HPROF_ROOT_VM_INTERNAL = 0x8d,
141 HPROF_ROOT_JNI_MONITOR = 0x8e,
142 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700143 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700144};
145
146enum HprofHeapId {
147 HPROF_HEAP_DEFAULT = 0,
148 HPROF_HEAP_ZYGOTE = 'Z',
149 HPROF_HEAP_APP = 'A'
150};
151
152enum HprofBasicType {
153 hprof_basic_object = 2,
154 hprof_basic_boolean = 4,
155 hprof_basic_char = 5,
156 hprof_basic_float = 6,
157 hprof_basic_double = 7,
158 hprof_basic_byte = 8,
159 hprof_basic_short = 9,
160 hprof_basic_int = 10,
161 hprof_basic_long = 11,
162};
163
164typedef uint32_t HprofId;
165typedef HprofId HprofStringId;
166typedef HprofId HprofObjectId;
167typedef HprofId HprofClassObjectId;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168typedef std::set<Class*> ClassSet;
169typedef std::set<Class*>::iterator ClassSetIterator;
Elliott Hughes622a6982012-06-08 17:58:54 -0700170typedef SafeMap<std::string, size_t> StringMap;
171typedef SafeMap<std::string, size_t>::iterator StringMapIterator;
172
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:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700180 HprofRecord() {
181 dirty_ = false;
182 alloc_length_ = 128;
183 body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_));
184 fp_ = NULL;
185 }
186
187 ~HprofRecord() {
188 free(body_);
189 }
190
191 int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) {
192 int rc = Flush();
193 if (rc != 0) {
194 return rc;
195 }
196
197 fp_ = fp;
198 tag_ = tag;
199 time_ = time;
200 length_ = 0;
201 dirty_ = true;
202 return 0;
203 }
204
205 int Flush() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700206 if (dirty_) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700207 unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)];
Elliott Hughes622a6982012-06-08 17:58:54 -0700208
209 headBuf[0] = tag_;
210 U4_TO_BUF_BE(headBuf, 1, time_);
211 U4_TO_BUF_BE(headBuf, 5, length_);
212
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700213 int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700214 if (nb != sizeof(headBuf)) {
215 return UNIQUE_ERROR;
216 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700217 nb = fwrite(body_, 1, length_, fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700218 if (nb != (int)length_) {
219 return UNIQUE_ERROR;
220 }
221
222 dirty_ = false;
223 }
224 // TODO if we used less than half (or whatever) of allocLen, shrink the buffer.
225 return 0;
226 }
227
228 int AddU1(uint8_t value) {
229 int err = GuaranteeRecordAppend(1);
230 if (err != 0) {
231 return err;
232 }
233
234 body_[length_++] = value;
235 return 0;
236 }
237
238 int AddU2(uint16_t value) {
239 return AddU2List(&value, 1);
240 }
241
242 int AddU4(uint32_t value) {
243 return AddU4List(&value, 1);
244 }
245
246 int AddU8(uint64_t value) {
247 return AddU8List(&value, 1);
248 }
249
250 int AddId(HprofObjectId value) {
251 return AddU4((uint32_t) value);
252 }
253
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700254 int AddU1List(const uint8_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700255 int err = GuaranteeRecordAppend(numValues);
256 if (err != 0) {
257 return err;
258 }
259
260 memcpy(body_ + length_, values, numValues);
261 length_ += numValues;
262 return 0;
263 }
264
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700265 int AddU2List(const uint16_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700266 int err = GuaranteeRecordAppend(numValues * 2);
267 if (err != 0) {
268 return err;
269 }
270
271 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700272 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700273 U2_TO_BUF_BE(insert, 0, *values++);
274 insert += sizeof(*values);
275 }
276 length_ += numValues * 2;
277 return 0;
278 }
279
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700280 int AddU4List(const uint32_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700281 int err = GuaranteeRecordAppend(numValues * 4);
282 if (err != 0) {
283 return err;
284 }
285
286 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700287 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700288 U4_TO_BUF_BE(insert, 0, *values++);
289 insert += sizeof(*values);
290 }
291 length_ += numValues * 4;
292 return 0;
293 }
294
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700295 void UpdateU4(size_t offset, uint32_t new_value) {
296 U4_TO_BUF_BE(body_, offset, new_value);
297 }
298
299 int AddU8List(const uint64_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700300 int err = GuaranteeRecordAppend(numValues * 8);
301 if (err != 0) {
302 return err;
303 }
304
305 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700306 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700307 U8_TO_BUF_BE(insert, 0, *values++);
308 insert += sizeof(*values);
309 }
310 length_ += numValues * 8;
311 return 0;
312 }
313
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700314 int AddIdList(const HprofObjectId* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700315 return AddU4List((const uint32_t*) values, numValues);
316 }
317
318 int AddUtf8String(const char* str) {
319 // The terminating NUL character is NOT written.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700320 return AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700321 }
322
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700323 size_t Size() const {
324 return length_;
325 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700326
327 private:
328 int GuaranteeRecordAppend(size_t nmore) {
329 size_t minSize = length_ + nmore;
330 if (minSize > alloc_length_) {
331 size_t newAllocLen = alloc_length_ * 2;
332 if (newAllocLen < minSize) {
333 newAllocLen = alloc_length_ + nmore + nmore/2;
334 }
335 unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen);
336 if (newBody != NULL) {
337 body_ = newBody;
338 alloc_length_ = newAllocLen;
339 } else {
340 // TODO: set an error flag so future ops will fail
341 return UNIQUE_ERROR;
342 }
343 }
344
345 CHECK_LE(length_ + nmore, alloc_length_);
346 return 0;
347 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700348
349 size_t alloc_length_;
350 unsigned char* body_;
351
352 FILE* fp_;
353 uint8_t tag_;
354 uint32_t time_;
355 size_t length_;
356 bool dirty_;
357
358 DISALLOW_COPY_AND_ASSIGN(HprofRecord);
Elliott Hughes622a6982012-06-08 17:58:54 -0700359};
360
361class Hprof {
362 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700363 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
364 : filename_(output_filename),
365 fd_(fd),
366 direct_to_ddms_(direct_to_ddms),
367 start_ns_(NanoTime()),
368 current_record_(),
369 gc_thread_serial_number_(0),
370 gc_scan_state_(0),
371 current_heap_(HPROF_HEAP_DEFAULT),
372 objects_in_segment_(0),
373 header_fp_(NULL),
374 header_data_ptr_(NULL),
375 header_data_size_(0),
376 body_fp_(NULL),
377 body_data_ptr_(NULL),
378 body_data_size_(0),
379 next_string_id_(0x400000) {
380 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Elliott Hughes622a6982012-06-08 17:58:54 -0700381
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700382 header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_);
383 if (header_fp_ == NULL) {
384 PLOG(FATAL) << "header open_memstream failed";
385 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700386
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700387 body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_);
388 if (body_fp_ == NULL) {
389 PLOG(FATAL) << "body open_memstream failed";
390 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500391 }
392
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700393 ~Hprof() {
394 if (header_fp_ != NULL) {
395 fclose(header_fp_);
396 }
397 if (body_fp_ != NULL) {
398 fclose(body_fp_);
399 }
400 free(header_data_ptr_);
401 free(body_data_ptr_);
402 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 void Dump()
405 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
406 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700407 // Walk the roots and the heap.
408 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
409 Runtime::Current()->VisitRoots(RootVisitor, this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700411 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
412 Runtime::Current()->GetHeap()->FlushAllocStack();
413 }
414 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700415 ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
416 Runtime::Current()->GetHeap()->GetLiveBitmap()->Walk(HeapBitmapCallback, this);
417 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700418 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
419 current_record_.Flush();
420 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500421
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700422 // Write the header.
423 WriteFixedHeader();
424 // Write the string and class tables, and any stack traces, to the header.
425 // (jhat requires that these appear before any of the data in the body that refers to them.)
426 WriteStringTable();
427 WriteClassTable();
428 WriteStackTraces();
429 current_record_.Flush();
430 fflush(header_fp_);
431
432 bool okay = true;
433 if (direct_to_ddms_) {
434 // Send the data off to DDMS.
435 iovec iov[2];
436 iov[0].iov_base = header_data_ptr_;
437 iov[0].iov_len = header_data_size_;
438 iov[1].iov_base = body_data_ptr_;
439 iov[1].iov_len = body_data_size_;
440 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
441 } else {
442 // Where exactly are we writing to?
443 int out_fd;
444 if (fd_ >= 0) {
445 out_fd = dup(fd_);
446 if (out_fd < 0) {
447 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
448 return;
449 }
450 } else {
451 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
452 if (out_fd < 0) {
453 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(), strerror(errno));
454 return;
455 }
456 }
457
458 UniquePtr<File> file(OS::FileFromFd(filename_.c_str(), out_fd));
459 okay = file->WriteFully(header_data_ptr_, header_data_size_) && file->WriteFully(body_data_ptr_, body_data_size_);
460 if (!okay) {
461 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s", filename_.c_str(), strerror(errno)));
462 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", msg.c_str());
463 LOG(ERROR) << msg;
464 }
465 close(out_fd);
466 }
467
468 // Throw out a log message for the benefit of "runhat".
469 if (okay) {
470 uint64_t duration = NanoTime() - start_ns_;
471 LOG(INFO) << "hprof: heap dump completed (" << PrettySize(header_data_size_ + body_data_size_ + 1023) << ") in " << PrettyDuration(duration);
472 }
473 }
474
475 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476 static void RootVisitor(const Object* obj, void* arg)
477 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700478 CHECK(arg != NULL);
479 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
480 hprof->VisitRoot(obj);
481 }
482
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 static void HeapBitmapCallback(Object* obj, void* arg)
484 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700485 CHECK(obj != NULL);
486 CHECK(arg != NULL);
487 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
488 hprof->DumpHeapObject(obj);
489 }
490
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 void VisitRoot(const Object* obj) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700492
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 int DumpHeapObject(Object* obj) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700494
495 void Finish() {
496 }
497
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 int WriteClassTable() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700499 HprofRecord* rec = &current_record_;
500 uint32_t nextSerialNumber = 1;
501
502 for (ClassSetIterator it = classes_.begin(); it != classes_.end(); ++it) {
503 const Class* c = *it;
504 CHECK(c != NULL);
505
506 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
507 if (err != 0) {
508 return err;
509 }
510
511 // LOAD CLASS format:
512 // U4: class serial number (always > 0)
513 // ID: class object ID. We use the address of the class object structure as its ID.
514 // U4: stack trace serial number
515 // ID: class name string ID
516 rec->AddU4(nextSerialNumber++);
517 rec->AddId((HprofClassObjectId) c);
518 rec->AddU4(HPROF_NULL_STACK_TRACE);
519 rec->AddId(LookupClassNameId(c));
520 }
521
522 return 0;
523 }
524
525 int WriteStringTable() {
526 HprofRecord* rec = &current_record_;
527
528 for (StringMapIterator it = strings_.begin(); it != strings_.end(); ++it) {
529 std::string string((*it).first);
530 size_t id = (*it).second;
531
532 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
533 if (err != 0) {
534 return err;
535 }
536
537 // STRING format:
538 // ID: ID for this string
539 // U1*: UTF8 characters for string (NOT NULL terminated)
540 // (the record format encodes the length)
541 err = rec->AddU4(id);
542 if (err != 0) {
543 return err;
544 }
545 err = rec->AddUtf8String(string.c_str());
546 if (err != 0) {
547 return err;
548 }
549 }
550
551 return 0;
552 }
553
554 void StartNewHeapDumpSegment() {
555 // This flushes the old segment and starts a new one.
556 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
557 objects_in_segment_ = 0;
558
559 // Starting a new HEAP_DUMP resets the heap to default.
560 current_heap_ = HPROF_HEAP_DEFAULT;
561 }
562
563 int MarkRootObject(const Object* obj, jobject jniObj);
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 HprofClassObjectId LookupClassId(Class* c)
566 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700567 if (c == NULL) {
568 // c is the superclass of java.lang.Object or a primitive
569 return (HprofClassObjectId)0;
570 }
571
572 std::pair<ClassSetIterator, bool> result = classes_.insert(c);
573 const Class* present = *result.first;
574
575 // Make sure that we've assigned a string ID for this class' name
576 LookupClassNameId(c);
577
578 CHECK_EQ(present, c);
579 return (HprofStringId) present;
580 }
581
582 HprofStringId LookupStringId(String* string) {
583 return LookupStringId(string->ToModifiedUtf8());
584 }
585
586 HprofStringId LookupStringId(const char* string) {
587 return LookupStringId(std::string(string));
588 }
589
590 HprofStringId LookupStringId(const std::string& string) {
591 StringMapIterator it = strings_.find(string);
592 if (it != strings_.end()) {
593 return it->second;
594 }
595 HprofStringId id = next_string_id_++;
596 strings_.Put(string, id);
597 return id;
598 }
599
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 HprofStringId LookupClassNameId(const Class* c)
601 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700602 return LookupStringId(PrettyDescriptor(c));
603 }
604
605 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700606 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500607 unsigned char buf[4];
608
609 // Write the file header.
610 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700611 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500612
613 // U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700614 U4_TO_BUF_BE(buf, 0, sizeof(void*));
615 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500616
617 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700618 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500619 uint64_t nowMs;
620 if (gettimeofday(&now, NULL) < 0) {
621 nowMs = 0;
622 } else {
623 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
624 }
625
626 // U4: high word of the 64-bit time.
627 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700628 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500629
630 // U4: low word of the 64-bit time.
631 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700632 fwrite(buf, 1, sizeof(uint32_t), header_fp_); //xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500633 }
634
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700635 void WriteStackTraces() {
636 // Write a dummy stack trace record so the analysis tools don't freak out.
637 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
638 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
639 current_record_.AddU4(HPROF_NULL_THREAD);
640 current_record_.AddU4(0); // no frames
641 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500642
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700643 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
644 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
645 // only be used for debug messages.
646 std::string filename_;
647 int fd_;
648 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500649
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700650 uint64_t start_ns_;
651
652 HprofRecord current_record_;
653
654 uint32_t gc_thread_serial_number_;
655 uint8_t gc_scan_state_;
656 HprofHeapId current_heap_; // Which heap we're currently dumping.
657 size_t objects_in_segment_;
658
659 FILE* header_fp_;
660 char* header_data_ptr_;
661 size_t header_data_size_;
662
663 FILE* body_fp_;
664 char* body_data_ptr_;
665 size_t body_data_size_;
666
667 ClassSet classes_;
668 size_t next_string_id_;
669 StringMap strings_;
670
671 DISALLOW_COPY_AND_ASSIGN(Hprof);
672};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500673
674#define OBJECTS_PER_SEGMENT ((size_t)128)
675#define BYTES_PER_SEGMENT ((size_t)4096)
676
677// The static field-name for the synthetic object generated to account
678// for class static overhead.
679#define STATIC_OVERHEAD_NAME "$staticOverhead"
680// The ID for the synthetic object generated to account for class static overhead.
Elliott Hughese84278b2012-03-22 10:06:53 -0700681#define CLASS_STATICS_ID(c) ((HprofObjectId)(((uint32_t)(c)) | 1))
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500682
Elliott Hughes622a6982012-06-08 17:58:54 -0700683static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500684 char c = sig[0];
685 HprofBasicType ret;
686 size_t size;
687
688 switch (c) {
689 case '[':
690 case 'L': ret = hprof_basic_object; size = 4; break;
691 case 'Z': ret = hprof_basic_boolean; size = 1; break;
692 case 'C': ret = hprof_basic_char; size = 2; break;
693 case 'F': ret = hprof_basic_float; size = 4; break;
694 case 'D': ret = hprof_basic_double; size = 8; break;
695 case 'B': ret = hprof_basic_byte; size = 1; break;
696 case 'S': ret = hprof_basic_short; size = 2; break;
697 default: CHECK(false);
698 case 'I': ret = hprof_basic_int; size = 4; break;
699 case 'J': ret = hprof_basic_long; size = 8; break;
700 }
701
702 if (sizeOut != NULL) {
703 *sizeOut = size;
704 }
705
706 return ret;
707}
708
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700709static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500710 HprofBasicType ret;
711 size_t size;
712
713 switch (prim) {
714 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
715 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
716 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
717 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
718 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
719 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
720 default: CHECK(false);
721 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
722 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
723 }
724
725 if (sizeOut != NULL) {
726 *sizeOut = size;
727 }
728
729 return ret;
730}
731
732// Always called when marking objects, but only does
733// something when ctx->gc_scan_state_ is non-zero, which is usually
734// only true when marking the root set or unreachable
735// objects. Used to add rootset references to obj.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700736int Hprof::MarkRootObject(const Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700737 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500738 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
739
740 if (heapTag == 0) {
741 return 0;
742 }
743
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700744 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
745 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500746 }
747
748 switch (heapTag) {
749 // ID: object ID
750 case HPROF_ROOT_UNKNOWN:
751 case HPROF_ROOT_STICKY_CLASS:
752 case HPROF_ROOT_MONITOR_USED:
753 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500754 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500755 case HPROF_ROOT_VM_INTERNAL:
756 rec->AddU1(heapTag);
757 rec->AddId((HprofObjectId)obj);
758 break;
759
760 // ID: object ID
761 // ID: JNI global ref ID
762 case HPROF_ROOT_JNI_GLOBAL:
763 rec->AddU1(heapTag);
764 rec->AddId((HprofObjectId)obj);
765 rec->AddId((HprofId)jniObj);
766 break;
767
768 // ID: object ID
769 // U4: thread serial number
770 // U4: frame number in stack trace (-1 for empty)
771 case HPROF_ROOT_JNI_LOCAL:
772 case HPROF_ROOT_JNI_MONITOR:
773 case HPROF_ROOT_JAVA_FRAME:
774 rec->AddU1(heapTag);
775 rec->AddId((HprofObjectId)obj);
776 rec->AddU4(gc_thread_serial_number_);
777 rec->AddU4((uint32_t)-1);
778 break;
779
780 // ID: object ID
781 // U4: thread serial number
782 case HPROF_ROOT_NATIVE_STACK:
783 case HPROF_ROOT_THREAD_BLOCK:
784 rec->AddU1(heapTag);
785 rec->AddId((HprofObjectId)obj);
786 rec->AddU4(gc_thread_serial_number_);
787 break;
788
789 // ID: thread object ID
790 // U4: thread serial number
791 // U4: stack trace serial number
792 case HPROF_ROOT_THREAD_OBJECT:
793 rec->AddU1(heapTag);
794 rec->AddId((HprofObjectId)obj);
795 rec->AddU4(gc_thread_serial_number_);
796 rec->AddU4((uint32_t)-1); //xxx
797 break;
798
Elliott Hughes73e66f72012-05-09 09:34:45 -0700799 case HPROF_CLASS_DUMP:
800 case HPROF_INSTANCE_DUMP:
801 case HPROF_OBJECT_ARRAY_DUMP:
802 case HPROF_PRIMITIVE_ARRAY_DUMP:
803 case HPROF_HEAP_DUMP_INFO:
804 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
805 // Ignored.
806 break;
807
808 case HPROF_ROOT_FINALIZING:
809 case HPROF_ROOT_REFERENCE_CLEANUP:
810 case HPROF_UNREACHABLE:
811 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500812 break;
813 }
814
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700815 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700816 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500817}
818
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700819static int StackTraceSerialNumber(const Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500820 return HPROF_NULL_STACK_TRACE;
821}
822
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823int Hprof::DumpHeapObject(Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700824 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500825 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
826
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700827 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
828 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500829 }
830
831 if (desiredHeap != current_heap_) {
832 HprofStringId nameId;
833
834 // This object is in a different heap than the current one.
835 // Emit a HEAP_DUMP_INFO tag to change heaps.
836 rec->AddU1(HPROF_HEAP_DUMP_INFO);
837 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
838 switch (desiredHeap) {
839 case HPROF_HEAP_APP:
840 nameId = LookupStringId("app");
841 break;
842 case HPROF_HEAP_ZYGOTE:
843 nameId = LookupStringId("zygote");
844 break;
845 default:
846 // Internal error
847 LOG(ERROR) << "Unexpected desiredHeap";
848 nameId = LookupStringId("<ILLEGAL>");
849 break;
850 }
851 rec->AddId(nameId);
852 current_heap_ = desiredHeap;
853 }
854
Elliott Hughese84278b2012-03-22 10:06:53 -0700855 Class* c = obj->GetClass();
856 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500857 // This object will bother HprofReader, because it has a NULL
858 // class, so just don't dump it. It could be
859 // gDvm.unlinkedJavaLangClass or it could be an object just
860 // allocated which hasn't been initialized yet.
861 } else {
862 if (obj->IsClass()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500864 // obj is a ClassObject.
865 size_t sFieldCount = thisClass->NumStaticFields();
866 if (sFieldCount != 0) {
867 int byteLength = sFieldCount*sizeof(JValue); // TODO bogus; fields are packed
868 // Create a byte array to reflect the allocation of the
869 // StaticField array at the end of this class.
870 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
871 rec->AddId(CLASS_STATICS_ID(obj));
872 rec->AddU4(StackTraceSerialNumber(obj));
873 rec->AddU4(byteLength);
874 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700875 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500876 rec->AddU1(0);
877 }
878 }
879
880 rec->AddU1(HPROF_CLASS_DUMP);
881 rec->AddId(LookupClassId(thisClass));
882 rec->AddU4(StackTraceSerialNumber(thisClass));
883 rec->AddId(LookupClassId(thisClass->GetSuperClass()));
884 rec->AddId((HprofObjectId)thisClass->GetClassLoader());
885 rec->AddId((HprofObjectId)0); // no signer
886 rec->AddId((HprofObjectId)0); // no prot domain
887 rec->AddId((HprofId)0); // reserved
888 rec->AddId((HprofId)0); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800889 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500890 // ClassObjects have their static fields appended, so aren't all the same size.
891 // But they're at least this size.
892 rec->AddU4(sizeof(Class)); // instance size
893 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
894 rec->AddU4(0);
895 } else {
896 rec->AddU4(thisClass->GetObjectSize()); // instance size
897 }
898
899 rec->AddU2(0); // empty const pool
900
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800901 FieldHelper fh;
902
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500903 // Static fields
904 if (sFieldCount == 0) {
905 rec->AddU2((uint16_t)0);
906 } else {
907 rec->AddU2((uint16_t)(sFieldCount+1));
908 rec->AddId(LookupStringId(STATIC_OVERHEAD_NAME));
909 rec->AddU1(hprof_basic_object);
910 rec->AddId(CLASS_STATICS_ID(obj));
911
912 for (size_t i = 0; i < sFieldCount; ++i) {
913 Field* f = thisClass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500915
916 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800917 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
918 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500919 rec->AddU1(t);
920 if (size == 1) {
921 rec->AddU1(static_cast<uint8_t>(f->Get32(NULL)));
922 } else if (size == 2) {
923 rec->AddU2(static_cast<uint16_t>(f->Get32(NULL)));
924 } else if (size == 4) {
925 rec->AddU4(f->Get32(NULL));
926 } else if (size == 8) {
927 rec->AddU8(f->Get64(NULL));
928 } else {
929 CHECK(false);
930 }
931 }
932 }
933
934 // Instance fields for this class (no superclass fields)
935 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
936 rec->AddU2((uint16_t)iFieldCount);
937 for (int i = 0; i < iFieldCount; ++i) {
938 Field* f = thisClass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800939 fh.ChangeField(f);
940 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
941 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500942 rec->AddU1(t);
943 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700944 } else if (c->IsArrayClass()) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700945 const Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500946 uint32_t length = aobj->GetLength();
947
948 if (obj->IsObjectArray()) {
949 // obj is an object array.
950 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
951
952 rec->AddId((HprofObjectId)obj);
953 rec->AddU4(StackTraceSerialNumber(obj));
954 rec->AddU4(length);
Elliott Hughese84278b2012-03-22 10:06:53 -0700955 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500956
957 // Dump the elements, which are always objects or NULL.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700958 rec->AddIdList((const HprofObjectId*)aobj->GetRawData(sizeof(Object*)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500959 } else {
960 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700961 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500962
963 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500964 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500965
966 rec->AddId((HprofObjectId)obj);
967 rec->AddU4(StackTraceSerialNumber(obj));
968 rec->AddU4(length);
969 rec->AddU1(t);
970
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500971 // Dump the raw, packed element values.
972 if (size == 1) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700973 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500974 } else if (size == 2) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700975 rec->AddU2List((const uint16_t*)(void*)aobj->GetRawData(sizeof(uint16_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500976 } else if (size == 4) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700977 rec->AddU4List((const uint32_t*)(void*)aobj->GetRawData(sizeof(uint32_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500978 } else if (size == 8) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700979 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500980 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500981 }
982 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500983 // obj is an instance object.
984 rec->AddU1(HPROF_INSTANCE_DUMP);
985 rec->AddId((HprofObjectId)obj);
986 rec->AddU4(StackTraceSerialNumber(obj));
Elliott Hughese84278b2012-03-22 10:06:53 -0700987 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500988
989 // Reserve some space for the length of the instance data, which we won't
990 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700991 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500992 rec->AddU4(0x77777777);
993
994 // Write the instance data; fields for this class, followed by super class fields,
995 // and so on. Don't write the klass or monitor fields of Object.class.
Elliott Hughese84278b2012-03-22 10:06:53 -0700996 const Class* sclass = c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800997 FieldHelper fh;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500998 while (!sclass->IsObjectClass()) {
999 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001000 for (int i = 0; i < ifieldCount; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001001 Field* f = sclass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001002 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001003 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001004 SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001005 if (size == 1) {
1006 rec->AddU1(f->Get32(obj));
1007 } else if (size == 2) {
1008 rec->AddU2(f->Get32(obj));
1009 } else if (size == 4) {
1010 rec->AddU4(f->Get32(obj));
1011 } else if (size == 8) {
1012 rec->AddU8(f->Get64(obj));
1013 } else {
1014 CHECK(false);
1015 }
1016 }
1017
1018 sclass = sclass->GetSuperClass();
1019 }
1020
1021 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001022 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001023 }
1024 }
1025
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001026 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001027 return 0;
1028}
1029
Jesse Wilson3aa66fd2011-11-08 20:53:40 -05001030void Hprof::VisitRoot(const Object* obj) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001031 uint32_t threadId = 0; // TODO
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001032 /*RootType*/ size_t type = 0; // TODO
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001033
Jesse Wilson0b075f12011-11-09 10:57:41 -05001034 static const HprofHeapTag xlate[] = {
1035 HPROF_ROOT_UNKNOWN,
1036 HPROF_ROOT_JNI_GLOBAL,
1037 HPROF_ROOT_JNI_LOCAL,
1038 HPROF_ROOT_JAVA_FRAME,
1039 HPROF_ROOT_NATIVE_STACK,
1040 HPROF_ROOT_STICKY_CLASS,
1041 HPROF_ROOT_THREAD_BLOCK,
1042 HPROF_ROOT_MONITOR_USED,
1043 HPROF_ROOT_THREAD_OBJECT,
1044 HPROF_ROOT_INTERNED_STRING,
1045 HPROF_ROOT_FINALIZING,
1046 HPROF_ROOT_DEBUGGER,
1047 HPROF_ROOT_REFERENCE_CLEANUP,
1048 HPROF_ROOT_VM_INTERNAL,
1049 HPROF_ROOT_JNI_MONITOR,
1050 };
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001051
Jesse Wilson0b075f12011-11-09 10:57:41 -05001052 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1053 if (obj == NULL) {
1054 return;
1055 }
1056 gc_scan_state_ = xlate[type];
1057 gc_thread_serial_number_ = threadId;
1058 MarkRootObject(obj, 0);
1059 gc_scan_state_ = 0;
1060 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001061}
1062
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001063// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1064// sent directly to DDMS.
1065// If "fd" is >= 0, the output will be written to that file descriptor.
1066// Otherwise, "filename" is used to create an output file.
1067void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1068 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001069
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001071 Hprof hprof(filename, fd, direct_to_ddms);
1072 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001074}
1075
1076} // namespace hprof
1077
1078} // namespace art