blob: e26e2cf9f5093e3447cc6bbf97386abfc59d2bd2 [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#ifndef HPROF_HPROF_H_
17#define HPROF_HPROF_H_
18
19#include <stdio.h>
20#include "globals.h"
21#include "file.h"
22#include "object.h"
23
24namespace art {
25
26namespace hprof {
27
28#define HPROF_ID_SIZE (sizeof (uint32_t))
29
30#define UNIQUE_ERROR() \
31 -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
32
33#define HPROF_TIME 0
34#define HPROF_NULL_STACK_TRACE 0
35#define HPROF_NULL_THREAD 0
36
37typedef uint32_t hprof_id;
38typedef hprof_id hprof_string_id;
39typedef hprof_id hprof_object_id;
40typedef hprof_id hprof_class_object_id;
41
42enum hprof_basic_type {
43 hprof_basic_object = 2,
44 hprof_basic_boolean = 4,
45 hprof_basic_char = 5,
46 hprof_basic_float = 6,
47 hprof_basic_double = 7,
48 hprof_basic_byte = 8,
49 hprof_basic_short = 9,
50 hprof_basic_int = 10,
51 hprof_basic_long = 11,
52};
53
54enum hprof_tag_t {
55 HPROF_TAG_STRING = 0x01,
56 HPROF_TAG_LOAD_CLASS = 0x02,
57 HPROF_TAG_UNLOAD_CLASS = 0x03,
58 HPROF_TAG_STACK_FRAME = 0x04,
59 HPROF_TAG_STACK_TRACE = 0x05,
60 HPROF_TAG_ALLOC_SITES = 0x06,
61 HPROF_TAG_HEAP_SUMMARY = 0x07,
62 HPROF_TAG_START_THREAD = 0x0A,
63 HPROF_TAG_END_THREAD = 0x0B,
64 HPROF_TAG_HEAP_DUMP = 0x0C,
65 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
66 HPROF_TAG_HEAP_DUMP_END = 0x2C,
67 HPROF_TAG_CPU_SAMPLES = 0x0D,
68 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
69};
70
71/* Values for the first byte of
72 * HEAP_DUMP and HEAP_DUMP_SEGMENT
73 * records:
74 */
75enum hprof_heap_tag_t {
76 /* standard */
77 HPROF_ROOT_UNKNOWN = 0xFF,
78 HPROF_ROOT_JNI_GLOBAL = 0x01,
79 HPROF_ROOT_JNI_LOCAL = 0x02,
80 HPROF_ROOT_JAVA_FRAME = 0x03,
81 HPROF_ROOT_NATIVE_STACK = 0x04,
82 HPROF_ROOT_STICKY_CLASS = 0x05,
83 HPROF_ROOT_THREAD_BLOCK = 0x06,
84 HPROF_ROOT_MONITOR_USED = 0x07,
85 HPROF_ROOT_THREAD_OBJECT = 0x08,
86 HPROF_CLASS_DUMP = 0x20,
87 HPROF_INSTANCE_DUMP = 0x21,
88 HPROF_OBJECT_ARRAY_DUMP = 0x22,
89 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
90
91 /* Android */
92 HPROF_HEAP_DUMP_INFO = 0xfe,
93 HPROF_ROOT_INTERNED_STRING = 0x89,
94 HPROF_ROOT_FINALIZING = 0x8a, /* obsolete */
95 HPROF_ROOT_DEBUGGER = 0x8b,
96 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, /* obsolete */
97 HPROF_ROOT_VM_INTERNAL = 0x8d,
98 HPROF_ROOT_JNI_MONITOR = 0x8e,
99 HPROF_UNREACHABLE = 0x90, /* obsolete */
100 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3,
101};
102
103/* Represents a top-level hprof record, whose serialized
104 * format is:
105 *
106 * uint8_t TAG: denoting the type of the record
107 * uint32_t TIME: number of microseconds since the time stamp in the header
108 * uint32_t LENGTH: number of bytes that follow this uint32_t field
109 * and belong to this record
110 * [uint8_t]* BODY: as many bytes as specified in the above uint32_t field
111 */
112struct hprof_record_t {
113 unsigned char *body;
114 uint32_t time;
115 uint32_t length;
116 size_t allocLen;
117 uint8_t tag;
118 bool dirty;
119};
120
121enum HprofHeapId {
122 HPROF_HEAP_DEFAULT = 0,
123 HPROF_HEAP_ZYGOTE = 'Z',
124 HPROF_HEAP_APP = 'A'
125};
126
127struct hprof_context_t {
128 /* curRec *must* be first so that we
129 * can cast from a context to a record.
130 */
131 hprof_record_t curRec;
132
133 uint32_t gcThreadSerialNumber;
134 uint8_t gcScanState;
135 HprofHeapId currentHeap; // which heap we're currently emitting
136 uint32_t stackTraceSerialNumber;
137 size_t objectsInSegment;
138
139 /*
140 * If directToDdms is set, "fileName" and "fd" will be ignored.
141 * Otherwise, "fileName" must be valid, though if "fd" >= 0 it will
142 * only be used for debug messages.
143 */
144 bool directToDdms;
145 char *fileName;
146 char *fileDataPtr; // for open_memstream
147 size_t fileDataSize; // for open_memstream
148 FILE *memFp;
149 int fd;
150};
151
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400152hprof_string_id hprofLookupStringId(String* string);
153hprof_string_id hprofLookupStringId(const char* string);
154hprof_string_id hprofLookupStringId(std::string string);
155
156int hprofDumpStrings(hprof_context_t *ctx);
157
158int hprofStartup_String(void);
159int hprofShutdown_String(void);
160
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400161hprof_class_object_id hprofLookupClassId(Class* clazz);
162
163int hprofDumpClasses(hprof_context_t *ctx);
164
165int hprofStartup_Class(void);
166int hprofShutdown_Class(void);
167
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400168int hprofStartHeapDump(hprof_context_t *ctx);
169int hprofFinishHeapDump(hprof_context_t *ctx);
170
171int hprofSetGcScanState(hprof_context_t *ctx,
172 hprof_heap_tag_t state, uint32_t threadSerialNumber);
173int hprofMarkRootObject(hprof_context_t *ctx,
174 const Object *obj, jobject jniObj);
175
Elliott Hughesfbd84562011-11-07 18:56:13 -0800176int DumpHeapObject(hprof_context_t *ctx, const Object *obj);
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400177
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400178void hprofContextInit(hprof_context_t *ctx, char *fileName, int fd,
179 bool writeHeader, bool directToDdms);
180
181int hprofFlushRecord(hprof_record_t *rec, FILE *fp);
182int hprofFlushCurrentRecord(hprof_context_t *ctx);
183int hprofStartNewRecord(hprof_context_t *ctx, uint8_t tag, uint32_t time);
184
185int hprofAddU1ToRecord(hprof_record_t *rec, uint8_t value);
186int hprofAddU1ListToRecord(hprof_record_t *rec,
187 const uint8_t *values, size_t numValues);
188
189int hprofAddUtf8StringToRecord(hprof_record_t *rec, const char *str);
190
191int hprofAddU2ToRecord(hprof_record_t *rec, uint16_t value);
192int hprofAddU2ListToRecord(hprof_record_t *rec,
193 const uint16_t *values, size_t numValues);
194
195int hprofAddU4ToRecord(hprof_record_t *rec, uint32_t value);
196int hprofAddU4ListToRecord(hprof_record_t *rec,
197 const uint32_t *values, size_t numValues);
198
199int hprofAddU8ToRecord(hprof_record_t *rec, uint64_t value);
200int hprofAddU8ListToRecord(hprof_record_t *rec,
201 const uint64_t *values, size_t numValues);
202
203#define hprofAddIdToRecord(rec, id) hprofAddU4ToRecord((rec), (uint32_t)(id))
204#define hprofAddIdListToRecord(rec, values, numValues) \
205 hprofAddU4ListToRecord((rec), (const uint32_t *)(values), (numValues))
206
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400207hprof_context_t* hprofStartup(const char *outputFileName, int fd,
208 bool directToDdms);
209bool hprofShutdown(hprof_context_t *ctx);
210void hprofFreeContext(hprof_context_t *ctx);
Elliott Hughesfbd84562011-11-07 18:56:13 -0800211int DumpHeap(const char* fileName, int fd, bool directToDdms);
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400212
213} // namespace hprof
214
215} // namespace art
216
217#endif // HPROF_HPROF_H_