1 /* 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 _DALVIK_HPROF_HPROF 17 #define _DALVIK_HPROF_HPROF 18 19 #include "Dalvik.h" 20 21 #define HPROF_ID_SIZE (sizeof (u4)) 22 23 #define UNIQUE_ERROR() \ 24 -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff)) 25 26 #define HPROF_TIME 0 27 #define HPROF_NULL_STACK_TRACE 0 28 #define HPROF_NULL_THREAD 0 29 30 typedef u4 hprof_id; 31 typedef hprof_id hprof_string_id; 32 typedef hprof_id hprof_object_id; 33 typedef hprof_id hprof_class_object_id; 34 #if WITH_HPROF_STACK 35 typedef hprof_id hprof_stack_frame_id; 36 #endif 37 38 typedef enum hprof_basic_type { 39 hprof_basic_object = 2, 40 hprof_basic_boolean = 4, 41 hprof_basic_char = 5, 42 hprof_basic_float = 6, 43 hprof_basic_double = 7, 44 hprof_basic_byte = 8, 45 hprof_basic_short = 9, 46 hprof_basic_int = 10, 47 hprof_basic_long = 11, 48 } hprof_basic_type; 49 50 typedef enum hprof_tag_t { 51 HPROF_TAG_STRING = 0x01, 52 HPROF_TAG_LOAD_CLASS = 0x02, 53 HPROF_TAG_UNLOAD_CLASS = 0x03, 54 HPROF_TAG_STACK_FRAME = 0x04, 55 HPROF_TAG_STACK_TRACE = 0x05, 56 HPROF_TAG_ALLOC_SITES = 0x06, 57 HPROF_TAG_HEAP_SUMMARY = 0x07, 58 HPROF_TAG_START_THREAD = 0x0A, 59 HPROF_TAG_END_THREAD = 0x0B, 60 HPROF_TAG_HEAP_DUMP = 0x0C, 61 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C, 62 HPROF_TAG_HEAP_DUMP_END = 0x2C, 63 HPROF_TAG_CPU_SAMPLES = 0x0D, 64 HPROF_TAG_CONTROL_SETTINGS = 0x0E, 65 } hprof_tag_t; 66 67 /* Values for the first byte of 68 * HEAP_DUMP and HEAP_DUMP_SEGMENT 69 * records: 70 */ 71 typedef enum hprof_heap_tag_t { 72 /* standard */ 73 HPROF_ROOT_UNKNOWN = 0xFF, 74 HPROF_ROOT_JNI_GLOBAL = 0x01, 75 HPROF_ROOT_JNI_LOCAL = 0x02, 76 HPROF_ROOT_JAVA_FRAME = 0x03, 77 HPROF_ROOT_NATIVE_STACK = 0x04, 78 HPROF_ROOT_STICKY_CLASS = 0x05, 79 HPROF_ROOT_THREAD_BLOCK = 0x06, 80 HPROF_ROOT_MONITOR_USED = 0x07, 81 HPROF_ROOT_THREAD_OBJECT = 0x08, 82 HPROF_CLASS_DUMP = 0x20, 83 HPROF_INSTANCE_DUMP = 0x21, 84 HPROF_OBJECT_ARRAY_DUMP = 0x22, 85 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23, 86 87 /* Android */ 88 HPROF_HEAP_DUMP_INFO = 0xfe, 89 HPROF_ROOT_INTERNED_STRING = 0x89, 90 HPROF_ROOT_FINALIZING = 0x8a, 91 HPROF_ROOT_DEBUGGER = 0x8b, 92 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, 93 HPROF_ROOT_VM_INTERNAL = 0x8d, 94 HPROF_ROOT_JNI_MONITOR = 0x8e, 95 HPROF_UNREACHABLE = 0x90, /* deprecated */ 96 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, 97 } hprof_heap_tag_t; 98 99 /* Represents a top-level hprof record, whose serialized 100 * format is: 101 * 102 * u1 TAG: denoting the type of the record 103 * u4 TIME: number of microseconds since the time stamp in the header 104 * u4 LENGTH: number of bytes that follow this u4 field 105 * and belong to this record 106 * [u1]* BODY: as many bytes as specified in the above u4 field 107 */ 108 typedef struct hprof_record_t { 109 unsigned char *body; 110 u4 time; 111 u4 length; 112 size_t allocLen; 113 u1 tag; 114 bool dirty; 115 } hprof_record_t; 116 117 typedef enum { 118 HPROF_HEAP_DEFAULT = 0, 119 HPROF_HEAP_ZYGOTE = 'Z', 120 HPROF_HEAP_APP = 'A' 121 } HprofHeapId; 122 123 typedef struct hprof_context_t { 124 /* curRec *must* be first so that we 125 * can cast from a context to a record. 126 */ 127 hprof_record_t curRec; 128 129 u4 gcThreadSerialNumber; 130 u1 gcScanState; 131 HprofHeapId currentHeap; // which heap we're currently emitting 132 u4 stackTraceSerialNumber; 133 size_t objectsInSegment; 134 135 /* 136 * If directToDdms is set, "fileName" and "fd" will be ignored. 137 * Otherwise, "fileName" must be valid, though if "fd" >= 0 it will 138 * only be used for debug messages. 139 */ 140 bool directToDdms; 141 char *fileName; 142 char *fileDataPtr; // for open_memstream 143 size_t fileDataSize; // for open_memstream 144 FILE *memFp; 145 int fd; 146 } hprof_context_t; 147 148 149 /* 150 * HprofString.c functions 151 */ 152 153 hprof_string_id hprofLookupStringId(const char *str); 154 155 int hprofDumpStrings(hprof_context_t *ctx); 156 157 int hprofStartup_String(void); 158 int hprofShutdown_String(void); 159 160 161 /* 162 * HprofClass.c functions 163 */ 164 165 hprof_class_object_id hprofLookupClassId(const ClassObject *clazz); 166 167 int hprofDumpClasses(hprof_context_t *ctx); 168 169 int hprofStartup_Class(void); 170 int hprofShutdown_Class(void); 171 172 173 /* 174 * HprofHeap.c functions 175 */ 176 177 int hprofStartHeapDump(hprof_context_t *ctx); 178 int hprofFinishHeapDump(hprof_context_t *ctx); 179 180 int hprofSetGcScanState(hprof_context_t *ctx, 181 hprof_heap_tag_t state, u4 threadSerialNumber); 182 int hprofMarkRootObject(hprof_context_t *ctx, 183 const Object *obj, jobject jniObj); 184 185 int hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj); 186 187 /* 188 * HprofOutput.c functions 189 */ 190 191 void hprofContextInit(hprof_context_t *ctx, char *fileName, int fd, 192 bool writeHeader, bool directToDdms); 193 194 int hprofFlushRecord(hprof_record_t *rec, FILE *fp); 195 int hprofFlushCurrentRecord(hprof_context_t *ctx); 196 int hprofStartNewRecord(hprof_context_t *ctx, u1 tag, u4 time); 197 198 int hprofAddU1ToRecord(hprof_record_t *rec, u1 value); 199 int hprofAddU1ListToRecord(hprof_record_t *rec, 200 const u1 *values, size_t numValues); 201 202 int hprofAddUtf8StringToRecord(hprof_record_t *rec, const char *str); 203 204 int hprofAddU2ToRecord(hprof_record_t *rec, u2 value); 205 int hprofAddU2ListToRecord(hprof_record_t *rec, 206 const u2 *values, size_t numValues); 207 208 int hprofAddU4ToRecord(hprof_record_t *rec, u4 value); 209 int hprofAddU4ListToRecord(hprof_record_t *rec, 210 const u4 *values, size_t numValues); 211 212 int hprofAddU8ToRecord(hprof_record_t *rec, u8 value); 213 int hprofAddU8ListToRecord(hprof_record_t *rec, 214 const u8 *values, size_t numValues); 215 216 #define hprofAddIdToRecord(rec, id) hprofAddU4ToRecord((rec), (u4)(id)) 217 #define hprofAddIdListToRecord(rec, values, numValues) \ 218 hprofAddU4ListToRecord((rec), (const u4 *)(values), (numValues)) 219 220 #if WITH_HPROF_STACK 221 222 /* 223 * HprofStack.c functions 224 */ 225 226 void hprofFillInStackTrace(void *objectPtr); 227 228 int hprofDumpStacks(hprof_context_t *ctx); 229 230 int hprofStartup_Stack(void); 231 int hprofShutdown_Stack(void); 232 233 /* 234 * HprofStackFrame.c functions 235 */ 236 237 int hprofDumpStackFrames(hprof_context_t *ctx); 238 239 int hprofStartup_StackFrame(void); 240 int hprofShutdown_StackFrame(void); 241 242 #endif 243 244 /* 245 * Hprof.c functions 246 */ 247 248 hprof_context_t* hprofStartup(const char *outputFileName, int fd, 249 bool directToDdms); 250 bool hprofShutdown(hprof_context_t *ctx); 251 void hprofFreeContext(hprof_context_t *ctx); 252 253 /* 254 * Heap.c functions 255 * 256 * The contents of the hprof directory have no knowledge of 257 * the heap implementation; these functions require heap knowledge, 258 * so they are implemented in Heap.c. 259 */ 260 int hprofDumpHeap(const char* fileName, int fd, bool directToDdms); 261 void dvmHeapSetHprofGcScanState(hprof_heap_tag_t state, u4 threadSerialNumber); 262 263 #endif // _DALVIK_HPROF_HPROF 264