Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame^] | 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 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 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace 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 | |
| 37 | typedef uint32_t hprof_id; |
| 38 | typedef hprof_id hprof_string_id; |
| 39 | typedef hprof_id hprof_object_id; |
| 40 | typedef hprof_id hprof_class_object_id; |
| 41 | |
| 42 | enum 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 | |
| 54 | enum 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 | */ |
| 75 | enum 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 | */ |
| 112 | struct 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 | |
| 121 | enum HprofHeapId { |
| 122 | HPROF_HEAP_DEFAULT = 0, |
| 123 | HPROF_HEAP_ZYGOTE = 'Z', |
| 124 | HPROF_HEAP_APP = 'A' |
| 125 | }; |
| 126 | |
| 127 | struct 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 | |
| 152 | |
| 153 | /* |
| 154 | * hprof_string.cc functions |
| 155 | */ |
| 156 | |
| 157 | hprof_string_id hprofLookupStringId(String* string); |
| 158 | hprof_string_id hprofLookupStringId(const char* string); |
| 159 | hprof_string_id hprofLookupStringId(std::string string); |
| 160 | |
| 161 | int hprofDumpStrings(hprof_context_t *ctx); |
| 162 | |
| 163 | int hprofStartup_String(void); |
| 164 | int hprofShutdown_String(void); |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * hprof_class.cc functions |
| 169 | */ |
| 170 | |
| 171 | hprof_class_object_id hprofLookupClassId(Class* clazz); |
| 172 | |
| 173 | int hprofDumpClasses(hprof_context_t *ctx); |
| 174 | |
| 175 | int hprofStartup_Class(void); |
| 176 | int hprofShutdown_Class(void); |
| 177 | |
| 178 | |
| 179 | /* |
| 180 | * hprof_heap.cc functions |
| 181 | */ |
| 182 | |
| 183 | int hprofStartHeapDump(hprof_context_t *ctx); |
| 184 | int hprofFinishHeapDump(hprof_context_t *ctx); |
| 185 | |
| 186 | int hprofSetGcScanState(hprof_context_t *ctx, |
| 187 | hprof_heap_tag_t state, uint32_t threadSerialNumber); |
| 188 | int hprofMarkRootObject(hprof_context_t *ctx, |
| 189 | const Object *obj, jobject jniObj); |
| 190 | |
| 191 | int hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj); |
| 192 | |
| 193 | /* |
| 194 | * hprof_output.cc functions |
| 195 | */ |
| 196 | |
| 197 | void hprofContextInit(hprof_context_t *ctx, char *fileName, int fd, |
| 198 | bool writeHeader, bool directToDdms); |
| 199 | |
| 200 | int hprofFlushRecord(hprof_record_t *rec, FILE *fp); |
| 201 | int hprofFlushCurrentRecord(hprof_context_t *ctx); |
| 202 | int hprofStartNewRecord(hprof_context_t *ctx, uint8_t tag, uint32_t time); |
| 203 | |
| 204 | int hprofAddU1ToRecord(hprof_record_t *rec, uint8_t value); |
| 205 | int hprofAddU1ListToRecord(hprof_record_t *rec, |
| 206 | const uint8_t *values, size_t numValues); |
| 207 | |
| 208 | int hprofAddUtf8StringToRecord(hprof_record_t *rec, const char *str); |
| 209 | |
| 210 | int hprofAddU2ToRecord(hprof_record_t *rec, uint16_t value); |
| 211 | int hprofAddU2ListToRecord(hprof_record_t *rec, |
| 212 | const uint16_t *values, size_t numValues); |
| 213 | |
| 214 | int hprofAddU4ToRecord(hprof_record_t *rec, uint32_t value); |
| 215 | int hprofAddU4ListToRecord(hprof_record_t *rec, |
| 216 | const uint32_t *values, size_t numValues); |
| 217 | |
| 218 | int hprofAddU8ToRecord(hprof_record_t *rec, uint64_t value); |
| 219 | int hprofAddU8ListToRecord(hprof_record_t *rec, |
| 220 | const uint64_t *values, size_t numValues); |
| 221 | |
| 222 | #define hprofAddIdToRecord(rec, id) hprofAddU4ToRecord((rec), (uint32_t)(id)) |
| 223 | #define hprofAddIdListToRecord(rec, values, numValues) \ |
| 224 | hprofAddU4ListToRecord((rec), (const uint32_t *)(values), (numValues)) |
| 225 | |
| 226 | /* |
| 227 | * hprof.cc functions |
| 228 | */ |
| 229 | |
| 230 | hprof_context_t* hprofStartup(const char *outputFileName, int fd, |
| 231 | bool directToDdms); |
| 232 | bool hprofShutdown(hprof_context_t *ctx); |
| 233 | void hprofFreeContext(hprof_context_t *ctx); |
| 234 | int hprofDumpHeap(const char* fileName, int fd, bool directToDdms); |
| 235 | |
| 236 | } // namespace hprof |
| 237 | |
| 238 | } // namespace art |
| 239 | |
| 240 | #endif // HPROF_HPROF_H_ |