aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/qtools/trace_reader_base.h
blob: 281d085e9863e7ec93614f7f1616876c0ff70d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2006 The Android Open Source Project

#ifndef TRACE_READER_BASE_H
#define TRACE_READER_BASE_H

#include <inttypes.h>
#include "trace_common.h"
#include "hash_table.h"

class BBReader;
class InsnReader;
class AddrReader;
class ExcReader;
class PidReader;
class MethodReader;

struct StaticRec {
    uint64_t    bb_num;
    uint32_t    bb_addr;
    uint32_t    num_insns;
};

struct StaticBlock {
    StaticRec   rec;
    uint32_t    *insns;
};

struct BBEvent {
    uint64_t    time;
    uint64_t    bb_num;
    uint32_t    bb_addr;
    uint32_t    *insns;
    int         num_insns;
    int         pid;
    int         is_thumb;
};

struct PidEvent {
    uint64_t    time;
    int         rec_type;       // record type: fork, context switch, exit ...
    int         tgid;           // thread group id
    int         pid;            // for fork: child pid; for switch: next pid;
                                //   for exit: exit value
    uint32_t    vstart;         // virtual start address (only used with mmap)
    uint32_t    vend;           // virtual end address (only used with mmap)
    uint32_t    offset;         // virtual file offset (only used with mmap)

    // Dynamically allocated path to executable (or lib). In the case of
    // an mmapped dex file, the path is modified to be more useful for
    // comparing against the output of dexlist.  For example, instead of this:
    //   /data/dalvik-cache/system@app@TestHarness.apk@classes.dex
    // We convert to this:
    //   /system/app/TestHarness.apk
    char        *path;
    char        *mmap_path;     // unmodified mmap path
    int         argc;           // number of args
    char        **argv;         // dynamically allocated array of args
};

struct MethodRec {
    uint64_t    time;
    uint32_t    addr;
    int         pid;
    int         flags;
};

struct DexSym {
    uint32_t    addr;
    int         len;
    char        *name;
};

struct DexFileList {
    char        *path;
    int         nsymbols;
    DexSym      *symbols;
};

class TraceReaderBase {
  public:
    TraceReaderBase();
    virtual ~TraceReaderBase();

    friend class BBReader;

    void                Open(char *filename);
    void                Close();
    void                WriteHeader(TraceHeader *header);
    inline bool         ReadBB(BBEvent *event);
    int                 ReadStatic(StaticRec *rec);
    int                 ReadStaticInsns(int num, uint32_t *insns);
    TraceHeader         *GetHeader()                { return header_; }
    inline uint64_t     ReadInsnTime(uint64_t min_time);
    void                TruncateLastBlock(uint32_t num_insns);
    inline bool         ReadAddr(uint64_t *time, uint32_t *addr, int *flags);
    inline bool         ReadExc(uint64_t *time, uint32_t *current_pc,
                                uint64_t *recnum, uint32_t *target_pc,
                                uint64_t *bb_num, uint64_t *bb_start_time,
                                int *num_insns);
    inline bool         ReadPidEvent(PidEvent *event);
    inline bool         ReadMethod(MethodRec *method_record);
    StaticBlock         *GetStaticBlock(uint64_t bb_num) { return &blocks_[bb_num]; }
    uint32_t            *GetInsns(uint64_t bb_num) { return blocks_[bb_num].insns; }
    uint32_t            GetBBAddr(uint64_t bb_num) {
        return blocks_[bb_num].rec.bb_addr & ~1;
    }
    int                 GetIsThumb(uint64_t bb_num) {
        return blocks_[bb_num].rec.bb_addr & 1;
    }
    void                SetPostProcessing(bool val) { post_processing_ = val; }

  protected:
    virtual int         FindCurrentPid(uint64_t time);
    int                 current_pid_;
    int                 next_pid_;
    uint64_t            next_pid_switch_time_;
    PidReader           *internal_pid_reader_;
    MethodReader        *internal_method_reader_;
    HashTable<DexFileList*> *dex_hash_;

  private:
    int          FindNumInsns(uint64_t bb_num, uint64_t bb_start_time);
    void         ReadTraceHeader(FILE *fstream, char *filename,
                                char *tracename, TraceHeader *header);
    PidEvent     *FindMmapDexFileEvent();
    void         ParseDexList(char *filename);

    char         *static_filename_;
    FILE         *static_fstream_;
    TraceHeader  *header_;
    BBReader     *bb_reader_;
    InsnReader   *insn_reader_;
    AddrReader   *load_addr_reader_;
    AddrReader   *store_addr_reader_;
    ExcReader    *exc_reader_;
    PidReader    *pid_reader_;
    MethodReader *method_reader_;
    ExcReader    *internal_exc_reader_;
    StaticBlock  *blocks_;
    bool         exc_end_;
    uint64_t     bb_recnum_;
    uint64_t     exc_recnum_;
    uint64_t     exc_bb_num_;
    uint64_t     exc_time_;
    int          exc_num_insns_;
    bool         post_processing_;

    bool         load_eof_;
    uint64_t     load_time_;
    uint32_t     load_addr_;
    bool         store_eof_;
    uint64_t     store_time_;
    uint32_t     store_addr_;
};

class Decoder;

class BBReader {
  public:
    explicit BBReader(TraceReaderBase *trace);
    ~BBReader();
    void     Open(char *filename);
    void     Close();
    bool     ReadBB(BBEvent *event);

  private:
    struct TimeRec {
        BBRec       bb_rec;
        uint64_t    next_time;
    };

    struct Future {
        Future      *next;
        TimeRec     bb;
    };

    inline Future   *AllocFuture();
    inline void     FreeFuture(Future *future);
    inline void     InsertFuture(Future *future);
    inline int      DecodeNextRec();

    TimeRec         nextrec_;
    Future          futures_[kMaxNumBasicBlocks];
    Future          *head_;
    Future          *free_;
    Decoder         *decoder_;
    bool            is_eof_;
    TraceReaderBase *trace_;
};

class InsnReader {
  public:
    InsnReader();
    ~InsnReader();

    void        Open(char *filename);
    void        Close();
    uint64_t    ReadInsnTime(uint64_t min_time);

  private:
    Decoder     *decoder_;
    uint64_t    prev_time_;
    uint64_t    time_diff_;
    int         repeat_;
};

class AddrReader {
  public:
    AddrReader();
    ~AddrReader();

    bool        Open(char *filename, char *suffix);
    void        Close();
    bool        ReadAddr(uint64_t *time, uint32_t *addr);

  private:
    Decoder     *decoder_;
    uint32_t    prev_addr_;
    uint64_t    prev_time_;
    bool        opened_;        // true after file is opened
};

class ExcReader {
  public:
    ExcReader();
    ~ExcReader();

    void        Open(char *filename);
    void        Close();
    bool        ReadExc(uint64_t *time, uint32_t *current_pc,
                        uint64_t *recnum, uint32_t *target_pc,
                        uint64_t *bb_num, uint64_t *bb_start_time,
                        int *num_insns);

  private:
    Decoder     *decoder_;
    uint64_t    prev_time_;
    uint64_t    prev_recnum_;
};

class PidReader {
  public:
    PidReader();
    ~PidReader();

    void        Open(char *filename);
    void        Close();
    bool        ReadPidEvent(struct PidEvent *event);
    void        Dispose(struct PidEvent *event);

  private:
    Decoder     *decoder_;
    uint64_t    prev_time_;
};

class MethodReader {
  public:
    MethodReader();
    ~MethodReader();

    bool        Open(char *filename);
    void        Close();
    bool        ReadMethod(MethodRec *method_record);

  private:
    Decoder     *decoder_;
    uint64_t    prev_time_;
    uint32_t    prev_addr_;
    int32_t     prev_pid_;
    bool        opened_;        // true after file is opened
};

// Reads the next dynamic basic block from the trace.
// Returns true on end-of-file.
inline bool TraceReaderBase::ReadBB(BBEvent *event)
{
    bb_recnum_ += 1;
    return bb_reader_->ReadBB(event);
}

inline uint64_t TraceReaderBase::ReadInsnTime(uint64_t min_time)
{
    return insn_reader_->ReadInsnTime(min_time);
}

inline bool TraceReaderBase::ReadAddr(uint64_t *time, uint32_t *addr, int *flags)
{
    if (load_eof_ && store_eof_)
        return true;

    if (store_eof_ || (!load_eof_ && load_time_ <= store_time_)) {
        *time = load_time_;
        *addr = load_addr_;
        *flags = 0;
        load_eof_ = load_addr_reader_->ReadAddr(&load_time_, &load_addr_);
    } else {
        *time = store_time_;
        *addr = store_addr_;
        *flags = 1;
        store_eof_ = store_addr_reader_->ReadAddr(&store_time_, &store_addr_);
    }
    return false;
}

inline bool TraceReaderBase::ReadExc(uint64_t *time, uint32_t *current_pc,
                                     uint64_t *recnum, uint32_t *target_pc,
                                     uint64_t *bb_num, uint64_t *bb_start_time,
                                     int *num_insns)
{
    return exc_reader_->ReadExc(time, current_pc, recnum, target_pc, bb_num,
                                bb_start_time, num_insns);
}

inline bool TraceReaderBase::ReadPidEvent(PidEvent *event)
{
    return pid_reader_->ReadPidEvent(event);
}

inline bool TraceReaderBase::ReadMethod(MethodRec *method_record)
{
    return method_reader_->ReadMethod(method_record);
}

// Duplicates a string, allocating space using new[].
inline char * Strdup(const char *src) {
    int len = strlen(src);
    char *copy = new char[len + 1];
    strcpy(copy, src);
    return copy;
}

#endif /* TRACE_READER_BASE_H */