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