blob: 3e976ad8f254f932aa67c807952de221cd44b8a0 [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"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080050#include "scoped_heap_lock.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040051#include "stringprintf.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070052#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040053
54namespace art {
55
56namespace hprof {
57
Elliott Hughes622a6982012-06-08 17:58:54 -070058#define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
Jesse Wilson0c54ac12011-11-09 15:14:05 -050059
Elliott Hughes622a6982012-06-08 17:58:54 -070060#define HPROF_TIME 0
61#define HPROF_NULL_STACK_TRACE 0
62#define HPROF_NULL_THREAD 0
63
64#define U2_TO_BUF_BE(buf, offset, value) \
65 do { \
66 unsigned char* buf_ = (unsigned char*)(buf); \
67 int offset_ = (int)(offset); \
68 uint16_t value_ = (uint16_t)(value); \
69 buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \
70 buf_[offset_ + 1] = (unsigned char)(value_ ); \
71 } while (0)
72
73#define U4_TO_BUF_BE(buf, offset, value) \
74 do { \
75 unsigned char* buf_ = (unsigned char*)(buf); \
76 int offset_ = (int)(offset); \
77 uint32_t value_ = (uint32_t)(value); \
78 buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \
79 buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \
80 buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \
81 buf_[offset_ + 3] = (unsigned char)(value_ ); \
82 } while (0)
83
84#define U8_TO_BUF_BE(buf, offset, value) \
85 do { \
86 unsigned char* buf_ = (unsigned char*)(buf); \
87 int offset_ = (int)(offset); \
88 uint64_t value_ = (uint64_t)(value); \
89 buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \
90 buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \
91 buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \
92 buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \
93 buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \
94 buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \
95 buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \
96 buf_[offset_ + 7] = (unsigned char)(value_ ); \
97 } while (0)
98
99enum HprofTag {
100 HPROF_TAG_STRING = 0x01,
101 HPROF_TAG_LOAD_CLASS = 0x02,
102 HPROF_TAG_UNLOAD_CLASS = 0x03,
103 HPROF_TAG_STACK_FRAME = 0x04,
104 HPROF_TAG_STACK_TRACE = 0x05,
105 HPROF_TAG_ALLOC_SITES = 0x06,
106 HPROF_TAG_HEAP_SUMMARY = 0x07,
107 HPROF_TAG_START_THREAD = 0x0A,
108 HPROF_TAG_END_THREAD = 0x0B,
109 HPROF_TAG_HEAP_DUMP = 0x0C,
110 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
111 HPROF_TAG_HEAP_DUMP_END = 0x2C,
112 HPROF_TAG_CPU_SAMPLES = 0x0D,
113 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
114};
115
116// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
117enum HprofHeapTag {
118 // Traditional.
119 HPROF_ROOT_UNKNOWN = 0xFF,
120 HPROF_ROOT_JNI_GLOBAL = 0x01,
121 HPROF_ROOT_JNI_LOCAL = 0x02,
122 HPROF_ROOT_JAVA_FRAME = 0x03,
123 HPROF_ROOT_NATIVE_STACK = 0x04,
124 HPROF_ROOT_STICKY_CLASS = 0x05,
125 HPROF_ROOT_THREAD_BLOCK = 0x06,
126 HPROF_ROOT_MONITOR_USED = 0x07,
127 HPROF_ROOT_THREAD_OBJECT = 0x08,
128 HPROF_CLASS_DUMP = 0x20,
129 HPROF_INSTANCE_DUMP = 0x21,
130 HPROF_OBJECT_ARRAY_DUMP = 0x22,
131 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
132
133 // Android.
134 HPROF_HEAP_DUMP_INFO = 0xfe,
135 HPROF_ROOT_INTERNED_STRING = 0x89,
136 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
137 HPROF_ROOT_DEBUGGER = 0x8b,
138 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
139 HPROF_ROOT_VM_INTERNAL = 0x8d,
140 HPROF_ROOT_JNI_MONITOR = 0x8e,
141 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700142 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700143};
144
145enum HprofHeapId {
146 HPROF_HEAP_DEFAULT = 0,
147 HPROF_HEAP_ZYGOTE = 'Z',
148 HPROF_HEAP_APP = 'A'
149};
150
151enum HprofBasicType {
152 hprof_basic_object = 2,
153 hprof_basic_boolean = 4,
154 hprof_basic_char = 5,
155 hprof_basic_float = 6,
156 hprof_basic_double = 7,
157 hprof_basic_byte = 8,
158 hprof_basic_short = 9,
159 hprof_basic_int = 10,
160 hprof_basic_long = 11,
161};
162
163typedef uint32_t HprofId;
164typedef HprofId HprofStringId;
165typedef HprofId HprofObjectId;
166typedef HprofId HprofClassObjectId;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700167typedef std::set<const Class*> ClassSet;
168typedef std::set<const Class*>::iterator ClassSetIterator;
Elliott Hughes622a6982012-06-08 17:58:54 -0700169typedef SafeMap<std::string, size_t> StringMap;
170typedef SafeMap<std::string, size_t>::iterator StringMapIterator;
171
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:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700179 HprofRecord() {
180 dirty_ = false;
181 alloc_length_ = 128;
182 body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_));
183 fp_ = NULL;
184 }
185
186 ~HprofRecord() {
187 free(body_);
188 }
189
190 int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) {
191 int rc = Flush();
192 if (rc != 0) {
193 return rc;
194 }
195
196 fp_ = fp;
197 tag_ = tag;
198 time_ = time;
199 length_ = 0;
200 dirty_ = true;
201 return 0;
202 }
203
204 int Flush() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700205 if (dirty_) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700206 unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)];
Elliott Hughes622a6982012-06-08 17:58:54 -0700207
208 headBuf[0] = tag_;
209 U4_TO_BUF_BE(headBuf, 1, time_);
210 U4_TO_BUF_BE(headBuf, 5, length_);
211
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700212 int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700213 if (nb != sizeof(headBuf)) {
214 return UNIQUE_ERROR;
215 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700216 nb = fwrite(body_, 1, length_, fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700217 if (nb != (int)length_) {
218 return UNIQUE_ERROR;
219 }
220
221 dirty_ = false;
222 }
223 // TODO if we used less than half (or whatever) of allocLen, shrink the buffer.
224 return 0;
225 }
226
227 int AddU1(uint8_t value) {
228 int err = GuaranteeRecordAppend(1);
229 if (err != 0) {
230 return err;
231 }
232
233 body_[length_++] = value;
234 return 0;
235 }
236
237 int AddU2(uint16_t value) {
238 return AddU2List(&value, 1);
239 }
240
241 int AddU4(uint32_t value) {
242 return AddU4List(&value, 1);
243 }
244
245 int AddU8(uint64_t value) {
246 return AddU8List(&value, 1);
247 }
248
249 int AddId(HprofObjectId value) {
250 return AddU4((uint32_t) value);
251 }
252
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700253 int AddU1List(const uint8_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700254 int err = GuaranteeRecordAppend(numValues);
255 if (err != 0) {
256 return err;
257 }
258
259 memcpy(body_ + length_, values, numValues);
260 length_ += numValues;
261 return 0;
262 }
263
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700264 int AddU2List(const uint16_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700265 int err = GuaranteeRecordAppend(numValues * 2);
266 if (err != 0) {
267 return err;
268 }
269
270 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700271 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700272 U2_TO_BUF_BE(insert, 0, *values++);
273 insert += sizeof(*values);
274 }
275 length_ += numValues * 2;
276 return 0;
277 }
278
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700279 int AddU4List(const uint32_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700280 int err = GuaranteeRecordAppend(numValues * 4);
281 if (err != 0) {
282 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 U4_TO_BUF_BE(insert, 0, *values++);
288 insert += sizeof(*values);
289 }
290 length_ += numValues * 4;
291 return 0;
292 }
293
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700294 void UpdateU4(size_t offset, uint32_t new_value) {
295 U4_TO_BUF_BE(body_, offset, new_value);
296 }
297
298 int AddU8List(const uint64_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700299 int err = GuaranteeRecordAppend(numValues * 8);
300 if (err != 0) {
301 return err;
302 }
303
304 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700305 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700306 U8_TO_BUF_BE(insert, 0, *values++);
307 insert += sizeof(*values);
308 }
309 length_ += numValues * 8;
310 return 0;
311 }
312
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700313 int AddIdList(const HprofObjectId* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700314 return AddU4List((const uint32_t*) values, numValues);
315 }
316
317 int AddUtf8String(const char* str) {
318 // The terminating NUL character is NOT written.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700319 return AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700320 }
321
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700322 size_t Size() const {
323 return length_;
324 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700325
326 private:
327 int GuaranteeRecordAppend(size_t nmore) {
328 size_t minSize = length_ + nmore;
329 if (minSize > alloc_length_) {
330 size_t newAllocLen = alloc_length_ * 2;
331 if (newAllocLen < minSize) {
332 newAllocLen = alloc_length_ + nmore + nmore/2;
333 }
334 unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen);
335 if (newBody != NULL) {
336 body_ = newBody;
337 alloc_length_ = newAllocLen;
338 } else {
339 // TODO: set an error flag so future ops will fail
340 return UNIQUE_ERROR;
341 }
342 }
343
344 CHECK_LE(length_ + nmore, alloc_length_);
345 return 0;
346 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700347
348 size_t alloc_length_;
349 unsigned char* body_;
350
351 FILE* fp_;
352 uint8_t tag_;
353 uint32_t time_;
354 size_t length_;
355 bool dirty_;
356
357 DISALLOW_COPY_AND_ASSIGN(HprofRecord);
Elliott Hughes622a6982012-06-08 17:58:54 -0700358};
359
360class Hprof {
361 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700362 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
363 : filename_(output_filename),
364 fd_(fd),
365 direct_to_ddms_(direct_to_ddms),
366 start_ns_(NanoTime()),
367 current_record_(),
368 gc_thread_serial_number_(0),
369 gc_scan_state_(0),
370 current_heap_(HPROF_HEAP_DEFAULT),
371 objects_in_segment_(0),
372 header_fp_(NULL),
373 header_data_ptr_(NULL),
374 header_data_size_(0),
375 body_fp_(NULL),
376 body_data_ptr_(NULL),
377 body_data_size_(0),
378 next_string_id_(0x400000) {
379 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Elliott Hughes622a6982012-06-08 17:58:54 -0700380
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700381 header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_);
382 if (header_fp_ == NULL) {
383 PLOG(FATAL) << "header open_memstream failed";
384 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700385
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700386 body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_);
387 if (body_fp_ == NULL) {
388 PLOG(FATAL) << "body open_memstream failed";
389 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500390 }
391
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700392 ~Hprof() {
393 if (header_fp_ != NULL) {
394 fclose(header_fp_);
395 }
396 if (body_fp_ != NULL) {
397 fclose(body_fp_);
398 }
399 free(header_data_ptr_);
400 free(body_data_ptr_);
401 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500402
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700403 void Dump() {
404 // Walk the roots and the heap.
405 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
406 Runtime::Current()->VisitRoots(RootVisitor, this);
407 Runtime::Current()->GetHeap()->GetLiveBits()->Walk(HeapBitmapCallback, this);
408 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
409 current_record_.Flush();
410 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500411
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700412 // Write the header.
413 WriteFixedHeader();
414 // Write the string and class tables, and any stack traces, to the header.
415 // (jhat requires that these appear before any of the data in the body that refers to them.)
416 WriteStringTable();
417 WriteClassTable();
418 WriteStackTraces();
419 current_record_.Flush();
420 fflush(header_fp_);
421
422 bool okay = true;
423 if (direct_to_ddms_) {
424 // Send the data off to DDMS.
425 iovec iov[2];
426 iov[0].iov_base = header_data_ptr_;
427 iov[0].iov_len = header_data_size_;
428 iov[1].iov_base = body_data_ptr_;
429 iov[1].iov_len = body_data_size_;
430 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
431 } else {
432 // Where exactly are we writing to?
433 int out_fd;
434 if (fd_ >= 0) {
435 out_fd = dup(fd_);
436 if (out_fd < 0) {
437 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
438 return;
439 }
440 } else {
441 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
442 if (out_fd < 0) {
443 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(), strerror(errno));
444 return;
445 }
446 }
447
448 UniquePtr<File> file(OS::FileFromFd(filename_.c_str(), out_fd));
449 okay = file->WriteFully(header_data_ptr_, header_data_size_) && file->WriteFully(body_data_ptr_, body_data_size_);
450 if (!okay) {
451 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s", filename_.c_str(), strerror(errno)));
452 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", msg.c_str());
453 LOG(ERROR) << msg;
454 }
455 close(out_fd);
456 }
457
458 // Throw out a log message for the benefit of "runhat".
459 if (okay) {
460 uint64_t duration = NanoTime() - start_ns_;
461 LOG(INFO) << "hprof: heap dump completed (" << PrettySize(header_data_size_ + body_data_size_ + 1023) << ") in " << PrettyDuration(duration);
462 }
463 }
464
465 private:
466 static void RootVisitor(const Object* obj, void* arg) {
467 CHECK(arg != NULL);
468 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
469 hprof->VisitRoot(obj);
470 }
471
472 static void HeapBitmapCallback(Object* obj, void* arg) {
473 CHECK(obj != NULL);
474 CHECK(arg != NULL);
475 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
476 hprof->DumpHeapObject(obj);
477 }
478
479 void VisitRoot(const Object* obj);
480
481 int DumpHeapObject(const Object* obj);
482
483 void Finish() {
484 }
485
486 int WriteClassTable() {
487 HprofRecord* rec = &current_record_;
488 uint32_t nextSerialNumber = 1;
489
490 for (ClassSetIterator it = classes_.begin(); it != classes_.end(); ++it) {
491 const Class* c = *it;
492 CHECK(c != NULL);
493
494 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
495 if (err != 0) {
496 return err;
497 }
498
499 // LOAD CLASS format:
500 // U4: class serial number (always > 0)
501 // ID: class object ID. We use the address of the class object structure as its ID.
502 // U4: stack trace serial number
503 // ID: class name string ID
504 rec->AddU4(nextSerialNumber++);
505 rec->AddId((HprofClassObjectId) c);
506 rec->AddU4(HPROF_NULL_STACK_TRACE);
507 rec->AddId(LookupClassNameId(c));
508 }
509
510 return 0;
511 }
512
513 int WriteStringTable() {
514 HprofRecord* rec = &current_record_;
515
516 for (StringMapIterator it = strings_.begin(); it != strings_.end(); ++it) {
517 std::string string((*it).first);
518 size_t id = (*it).second;
519
520 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
521 if (err != 0) {
522 return err;
523 }
524
525 // STRING format:
526 // ID: ID for this string
527 // U1*: UTF8 characters for string (NOT NULL terminated)
528 // (the record format encodes the length)
529 err = rec->AddU4(id);
530 if (err != 0) {
531 return err;
532 }
533 err = rec->AddUtf8String(string.c_str());
534 if (err != 0) {
535 return err;
536 }
537 }
538
539 return 0;
540 }
541
542 void StartNewHeapDumpSegment() {
543 // This flushes the old segment and starts a new one.
544 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
545 objects_in_segment_ = 0;
546
547 // Starting a new HEAP_DUMP resets the heap to default.
548 current_heap_ = HPROF_HEAP_DEFAULT;
549 }
550
551 int MarkRootObject(const Object* obj, jobject jniObj);
552
553 HprofClassObjectId LookupClassId(const Class* c) {
554 if (c == NULL) {
555 // c is the superclass of java.lang.Object or a primitive
556 return (HprofClassObjectId)0;
557 }
558
559 std::pair<ClassSetIterator, bool> result = classes_.insert(c);
560 const Class* present = *result.first;
561
562 // Make sure that we've assigned a string ID for this class' name
563 LookupClassNameId(c);
564
565 CHECK_EQ(present, c);
566 return (HprofStringId) present;
567 }
568
569 HprofStringId LookupStringId(String* string) {
570 return LookupStringId(string->ToModifiedUtf8());
571 }
572
573 HprofStringId LookupStringId(const char* string) {
574 return LookupStringId(std::string(string));
575 }
576
577 HprofStringId LookupStringId(const std::string& string) {
578 StringMapIterator it = strings_.find(string);
579 if (it != strings_.end()) {
580 return it->second;
581 }
582 HprofStringId id = next_string_id_++;
583 strings_.Put(string, id);
584 return id;
585 }
586
587 HprofStringId LookupClassNameId(const Class* c) {
588 return LookupStringId(PrettyDescriptor(c));
589 }
590
591 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700592 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500593 unsigned char buf[4];
594
595 // Write the file header.
596 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700597 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500598
599 // U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700600 U4_TO_BUF_BE(buf, 0, sizeof(void*));
601 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500602
603 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700604 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500605 uint64_t nowMs;
606 if (gettimeofday(&now, NULL) < 0) {
607 nowMs = 0;
608 } else {
609 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
610 }
611
612 // U4: high word of the 64-bit time.
613 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700614 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500615
616 // U4: low word of the 64-bit time.
617 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700618 fwrite(buf, 1, sizeof(uint32_t), header_fp_); //xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500619 }
620
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700621 void WriteStackTraces() {
622 // Write a dummy stack trace record so the analysis tools don't freak out.
623 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
624 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
625 current_record_.AddU4(HPROF_NULL_THREAD);
626 current_record_.AddU4(0); // no frames
627 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500628
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700629 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
630 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
631 // only be used for debug messages.
632 std::string filename_;
633 int fd_;
634 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500635
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700636 uint64_t start_ns_;
637
638 HprofRecord current_record_;
639
640 uint32_t gc_thread_serial_number_;
641 uint8_t gc_scan_state_;
642 HprofHeapId current_heap_; // Which heap we're currently dumping.
643 size_t objects_in_segment_;
644
645 FILE* header_fp_;
646 char* header_data_ptr_;
647 size_t header_data_size_;
648
649 FILE* body_fp_;
650 char* body_data_ptr_;
651 size_t body_data_size_;
652
653 ClassSet classes_;
654 size_t next_string_id_;
655 StringMap strings_;
656
657 DISALLOW_COPY_AND_ASSIGN(Hprof);
658};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500659
660#define OBJECTS_PER_SEGMENT ((size_t)128)
661#define BYTES_PER_SEGMENT ((size_t)4096)
662
663// The static field-name for the synthetic object generated to account
664// for class static overhead.
665#define STATIC_OVERHEAD_NAME "$staticOverhead"
666// The ID for the synthetic object generated to account for class static overhead.
Elliott Hughese84278b2012-03-22 10:06:53 -0700667#define CLASS_STATICS_ID(c) ((HprofObjectId)(((uint32_t)(c)) | 1))
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500668
Elliott Hughes622a6982012-06-08 17:58:54 -0700669static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500670 char c = sig[0];
671 HprofBasicType ret;
672 size_t size;
673
674 switch (c) {
675 case '[':
676 case 'L': ret = hprof_basic_object; size = 4; break;
677 case 'Z': ret = hprof_basic_boolean; size = 1; break;
678 case 'C': ret = hprof_basic_char; size = 2; break;
679 case 'F': ret = hprof_basic_float; size = 4; break;
680 case 'D': ret = hprof_basic_double; size = 8; break;
681 case 'B': ret = hprof_basic_byte; size = 1; break;
682 case 'S': ret = hprof_basic_short; size = 2; break;
683 default: CHECK(false);
684 case 'I': ret = hprof_basic_int; size = 4; break;
685 case 'J': ret = hprof_basic_long; size = 8; break;
686 }
687
688 if (sizeOut != NULL) {
689 *sizeOut = size;
690 }
691
692 return ret;
693}
694
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700695static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500696 HprofBasicType ret;
697 size_t size;
698
699 switch (prim) {
700 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
701 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
702 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
703 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
704 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
705 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
706 default: CHECK(false);
707 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
708 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
709 }
710
711 if (sizeOut != NULL) {
712 *sizeOut = size;
713 }
714
715 return ret;
716}
717
718// Always called when marking objects, but only does
719// something when ctx->gc_scan_state_ is non-zero, which is usually
720// only true when marking the root set or unreachable
721// objects. Used to add rootset references to obj.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700722int Hprof::MarkRootObject(const Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700723 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500724 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
725
726 if (heapTag == 0) {
727 return 0;
728 }
729
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700730 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
731 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500732 }
733
734 switch (heapTag) {
735 // ID: object ID
736 case HPROF_ROOT_UNKNOWN:
737 case HPROF_ROOT_STICKY_CLASS:
738 case HPROF_ROOT_MONITOR_USED:
739 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500740 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500741 case HPROF_ROOT_VM_INTERNAL:
742 rec->AddU1(heapTag);
743 rec->AddId((HprofObjectId)obj);
744 break;
745
746 // ID: object ID
747 // ID: JNI global ref ID
748 case HPROF_ROOT_JNI_GLOBAL:
749 rec->AddU1(heapTag);
750 rec->AddId((HprofObjectId)obj);
751 rec->AddId((HprofId)jniObj);
752 break;
753
754 // ID: object ID
755 // U4: thread serial number
756 // U4: frame number in stack trace (-1 for empty)
757 case HPROF_ROOT_JNI_LOCAL:
758 case HPROF_ROOT_JNI_MONITOR:
759 case HPROF_ROOT_JAVA_FRAME:
760 rec->AddU1(heapTag);
761 rec->AddId((HprofObjectId)obj);
762 rec->AddU4(gc_thread_serial_number_);
763 rec->AddU4((uint32_t)-1);
764 break;
765
766 // ID: object ID
767 // U4: thread serial number
768 case HPROF_ROOT_NATIVE_STACK:
769 case HPROF_ROOT_THREAD_BLOCK:
770 rec->AddU1(heapTag);
771 rec->AddId((HprofObjectId)obj);
772 rec->AddU4(gc_thread_serial_number_);
773 break;
774
775 // ID: thread object ID
776 // U4: thread serial number
777 // U4: stack trace serial number
778 case HPROF_ROOT_THREAD_OBJECT:
779 rec->AddU1(heapTag);
780 rec->AddId((HprofObjectId)obj);
781 rec->AddU4(gc_thread_serial_number_);
782 rec->AddU4((uint32_t)-1); //xxx
783 break;
784
Elliott Hughes73e66f72012-05-09 09:34:45 -0700785 case HPROF_CLASS_DUMP:
786 case HPROF_INSTANCE_DUMP:
787 case HPROF_OBJECT_ARRAY_DUMP:
788 case HPROF_PRIMITIVE_ARRAY_DUMP:
789 case HPROF_HEAP_DUMP_INFO:
790 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
791 // Ignored.
792 break;
793
794 case HPROF_ROOT_FINALIZING:
795 case HPROF_ROOT_REFERENCE_CLEANUP:
796 case HPROF_UNREACHABLE:
797 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500798 break;
799 }
800
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700801 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700802 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500803}
804
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700805static int StackTraceSerialNumber(const Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500806 return HPROF_NULL_STACK_TRACE;
807}
808
809int Hprof::DumpHeapObject(const Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700810 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500811 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
812
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700813 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
814 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500815 }
816
817 if (desiredHeap != current_heap_) {
818 HprofStringId nameId;
819
820 // This object is in a different heap than the current one.
821 // Emit a HEAP_DUMP_INFO tag to change heaps.
822 rec->AddU1(HPROF_HEAP_DUMP_INFO);
823 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
824 switch (desiredHeap) {
825 case HPROF_HEAP_APP:
826 nameId = LookupStringId("app");
827 break;
828 case HPROF_HEAP_ZYGOTE:
829 nameId = LookupStringId("zygote");
830 break;
831 default:
832 // Internal error
833 LOG(ERROR) << "Unexpected desiredHeap";
834 nameId = LookupStringId("<ILLEGAL>");
835 break;
836 }
837 rec->AddId(nameId);
838 current_heap_ = desiredHeap;
839 }
840
Elliott Hughese84278b2012-03-22 10:06:53 -0700841 Class* c = obj->GetClass();
842 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500843 // This object will bother HprofReader, because it has a NULL
844 // class, so just don't dump it. It could be
845 // gDvm.unlinkedJavaLangClass or it could be an object just
846 // allocated which hasn't been initialized yet.
847 } else {
848 if (obj->IsClass()) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700849 const Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500850 // obj is a ClassObject.
851 size_t sFieldCount = thisClass->NumStaticFields();
852 if (sFieldCount != 0) {
853 int byteLength = sFieldCount*sizeof(JValue); // TODO bogus; fields are packed
854 // Create a byte array to reflect the allocation of the
855 // StaticField array at the end of this class.
856 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
857 rec->AddId(CLASS_STATICS_ID(obj));
858 rec->AddU4(StackTraceSerialNumber(obj));
859 rec->AddU4(byteLength);
860 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700861 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500862 rec->AddU1(0);
863 }
864 }
865
866 rec->AddU1(HPROF_CLASS_DUMP);
867 rec->AddId(LookupClassId(thisClass));
868 rec->AddU4(StackTraceSerialNumber(thisClass));
869 rec->AddId(LookupClassId(thisClass->GetSuperClass()));
870 rec->AddId((HprofObjectId)thisClass->GetClassLoader());
871 rec->AddId((HprofObjectId)0); // no signer
872 rec->AddId((HprofObjectId)0); // no prot domain
873 rec->AddId((HprofId)0); // reserved
874 rec->AddId((HprofId)0); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800875 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500876 // ClassObjects have their static fields appended, so aren't all the same size.
877 // But they're at least this size.
878 rec->AddU4(sizeof(Class)); // instance size
879 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
880 rec->AddU4(0);
881 } else {
882 rec->AddU4(thisClass->GetObjectSize()); // instance size
883 }
884
885 rec->AddU2(0); // empty const pool
886
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800887 FieldHelper fh;
888
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500889 // Static fields
890 if (sFieldCount == 0) {
891 rec->AddU2((uint16_t)0);
892 } else {
893 rec->AddU2((uint16_t)(sFieldCount+1));
894 rec->AddId(LookupStringId(STATIC_OVERHEAD_NAME));
895 rec->AddU1(hprof_basic_object);
896 rec->AddId(CLASS_STATICS_ID(obj));
897
898 for (size_t i = 0; i < sFieldCount; ++i) {
899 Field* f = thisClass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500901
902 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800903 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
904 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500905 rec->AddU1(t);
906 if (size == 1) {
907 rec->AddU1(static_cast<uint8_t>(f->Get32(NULL)));
908 } else if (size == 2) {
909 rec->AddU2(static_cast<uint16_t>(f->Get32(NULL)));
910 } else if (size == 4) {
911 rec->AddU4(f->Get32(NULL));
912 } else if (size == 8) {
913 rec->AddU8(f->Get64(NULL));
914 } else {
915 CHECK(false);
916 }
917 }
918 }
919
920 // Instance fields for this class (no superclass fields)
921 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
922 rec->AddU2((uint16_t)iFieldCount);
923 for (int i = 0; i < iFieldCount; ++i) {
924 Field* f = thisClass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800925 fh.ChangeField(f);
926 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
927 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500928 rec->AddU1(t);
929 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700930 } else if (c->IsArrayClass()) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700931 const Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500932 uint32_t length = aobj->GetLength();
933
934 if (obj->IsObjectArray()) {
935 // obj is an object array.
936 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
937
938 rec->AddId((HprofObjectId)obj);
939 rec->AddU4(StackTraceSerialNumber(obj));
940 rec->AddU4(length);
Elliott Hughese84278b2012-03-22 10:06:53 -0700941 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500942
943 // Dump the elements, which are always objects or NULL.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700944 rec->AddIdList((const HprofObjectId*)aobj->GetRawData(sizeof(Object*)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500945 } else {
946 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700947 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500948
949 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500950 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500951
952 rec->AddId((HprofObjectId)obj);
953 rec->AddU4(StackTraceSerialNumber(obj));
954 rec->AddU4(length);
955 rec->AddU1(t);
956
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500957 // Dump the raw, packed element values.
958 if (size == 1) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700959 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500960 } else if (size == 2) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700961 rec->AddU2List((const uint16_t*)(void*)aobj->GetRawData(sizeof(uint16_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500962 } else if (size == 4) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700963 rec->AddU4List((const uint32_t*)(void*)aobj->GetRawData(sizeof(uint32_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500964 } else if (size == 8) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700965 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500966 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500967 }
968 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500969 // obj is an instance object.
970 rec->AddU1(HPROF_INSTANCE_DUMP);
971 rec->AddId((HprofObjectId)obj);
972 rec->AddU4(StackTraceSerialNumber(obj));
Elliott Hughese84278b2012-03-22 10:06:53 -0700973 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500974
975 // Reserve some space for the length of the instance data, which we won't
976 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700977 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500978 rec->AddU4(0x77777777);
979
980 // Write the instance data; fields for this class, followed by super class fields,
981 // and so on. Don't write the klass or monitor fields of Object.class.
Elliott Hughese84278b2012-03-22 10:06:53 -0700982 const Class* sclass = c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800983 FieldHelper fh;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500984 while (!sclass->IsObjectClass()) {
985 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700986 for (int i = 0; i < ifieldCount; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500987 Field* f = sclass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800988 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500989 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800990 SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500991 if (size == 1) {
992 rec->AddU1(f->Get32(obj));
993 } else if (size == 2) {
994 rec->AddU2(f->Get32(obj));
995 } else if (size == 4) {
996 rec->AddU4(f->Get32(obj));
997 } else if (size == 8) {
998 rec->AddU8(f->Get64(obj));
999 } else {
1000 CHECK(false);
1001 }
1002 }
1003
1004 sclass = sclass->GetSuperClass();
1005 }
1006
1007 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001008 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001009 }
1010 }
1011
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001012 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001013 return 0;
1014}
1015
Jesse Wilson3aa66fd2011-11-08 20:53:40 -05001016void Hprof::VisitRoot(const Object* obj) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001017 uint32_t threadId = 0; // TODO
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001018 /*RootType*/ size_t type = 0; // TODO
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001019
Jesse Wilson0b075f12011-11-09 10:57:41 -05001020 static const HprofHeapTag xlate[] = {
1021 HPROF_ROOT_UNKNOWN,
1022 HPROF_ROOT_JNI_GLOBAL,
1023 HPROF_ROOT_JNI_LOCAL,
1024 HPROF_ROOT_JAVA_FRAME,
1025 HPROF_ROOT_NATIVE_STACK,
1026 HPROF_ROOT_STICKY_CLASS,
1027 HPROF_ROOT_THREAD_BLOCK,
1028 HPROF_ROOT_MONITOR_USED,
1029 HPROF_ROOT_THREAD_OBJECT,
1030 HPROF_ROOT_INTERNED_STRING,
1031 HPROF_ROOT_FINALIZING,
1032 HPROF_ROOT_DEBUGGER,
1033 HPROF_ROOT_REFERENCE_CLEANUP,
1034 HPROF_ROOT_VM_INTERNAL,
1035 HPROF_ROOT_JNI_MONITOR,
1036 };
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001037
Jesse Wilson0b075f12011-11-09 10:57:41 -05001038 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1039 if (obj == NULL) {
1040 return;
1041 }
1042 gc_scan_state_ = xlate[type];
1043 gc_thread_serial_number_ = threadId;
1044 MarkRootObject(obj, 0);
1045 gc_scan_state_ = 0;
1046 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001047}
1048
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001049// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1050// sent directly to DDMS.
1051// If "fd" is >= 0, the output will be written to that file descriptor.
1052// Otherwise, "filename" is used to create an output file.
1053void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1054 CHECK(filename != NULL);
Elliott Hughesffb465f2012-03-01 18:46:05 -08001055 ScopedHeapLock heap_lock;
Elliott Hughes34e06962012-04-09 13:55:55 -07001056 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001057
Jesse Wilson0b075f12011-11-09 10:57:41 -05001058 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1059 thread_list->SuspendAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001060
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001061 Hprof hprof(filename, fd, direct_to_ddms);
1062 hprof.Dump();
Jesse Wilson0b075f12011-11-09 10:57:41 -05001063 thread_list->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001064}
1065
1066} // namespace hprof
1067
1068} // namespace art