aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/qtools/dmtrace.cpp
blob: a27193a71b5752e9d302466397c53cb2c836e08e (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
// Copyright 2006 The Android Open Source Project

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "dmtrace.h"

static const short kVersion = 2;

const DmTrace::Header DmTrace::header = {
    0x574f4c53, kVersion, sizeof(DmTrace::Header), 0LL
};

static char *keyHeader = "*version\n" "2\n" "clock=thread-cpu\n";
static char *keyThreadHeader = "*threads\n";
static char *keyFunctionHeader = "*methods\n";
static char *keyEnd = "*end\n";

DmTrace::DmTrace() {
    fData = NULL;
    fTrace = NULL;
    threads = new std::vector<ThreadRecord*>;
    functions = new std::vector<FunctionRecord*>;
}

DmTrace::~DmTrace() {
    delete threads;
    delete functions;
}

void DmTrace::open(const char *dmtrace_file, uint64_t start_time)
{
    fTrace = fopen(dmtrace_file, "w");
    if (fTrace == NULL) {
        perror(dmtrace_file);
        exit(1);
    }

    // Make a temporary file to write the data into.
    char tmpData[32];
    strcpy(tmpData, "/tmp/dmtrace-data-XXXXXX");
    int data_fd = mkstemp(tmpData);
    if (data_fd < 0) {
        perror("Cannot create temporary file");
        exit(1);
    }

    // Ensure it goes away on exit.
    unlink(tmpData);
    fData = fdopen(data_fd, "w+");
    if (fData == NULL) {
        perror("Can't make temp data file");
        exit(1);
    }

    writeHeader(fData, start_time);
}

void DmTrace::close()
{
    if (fTrace == NULL)
        return;
    writeKeyFile(fTrace);

    // Take down how much data we wrote to the temp data file.
    long size = ftell(fData);
    // Rewind the data file and append its contents to the trace file.
    rewind(fData);
    char *data = (char *)malloc(size);
    fread(data, size, 1, fData);
    fwrite(data, size, 1, fTrace);
    free(data);
    fclose(fData);
    fclose(fTrace);
}

/*
 * Write values to the binary data file.
 */
void DmTrace::write2LE(FILE* fstream, unsigned short val)
{
    putc(val & 0xff, fstream);
    putc(val >> 8, fstream);
}

void DmTrace::write4LE(FILE* fstream, unsigned int val)
{
    putc(val & 0xff, fstream);
    putc((val >> 8) & 0xff, fstream);
    putc((val >> 16) & 0xff, fstream);
    putc((val >> 24) & 0xff, fstream);
}

void DmTrace::write8LE(FILE* fstream, unsigned long long val)
{
    putc(val & 0xff, fstream);
    putc((val >> 8) & 0xff, fstream);
    putc((val >> 16) & 0xff, fstream);
    putc((val >> 24) & 0xff, fstream);
    putc((val >> 32) & 0xff, fstream);
    putc((val >> 40) & 0xff, fstream);
    putc((val >> 48) & 0xff, fstream);
    putc((val >> 56) & 0xff, fstream);
}

void DmTrace::writeHeader(FILE *fstream, uint64_t startTime)
{
    write4LE(fstream, header.magic);
    write2LE(fstream, header.version);
    write2LE(fstream, header.offset);
    write8LE(fstream, startTime);
}

void DmTrace::writeDataRecord(FILE *fstream, int threadId,
                             unsigned int methodVal,
                             unsigned int elapsedTime)
{
    write2LE(fstream, threadId);
    write4LE(fstream, methodVal);
    write4LE(fstream, elapsedTime);
}

void DmTrace::addFunctionEntry(int functionId, uint32_t cycle, uint32_t pid)
{
    writeDataRecord(fData, pid, functionId, cycle);
}

void DmTrace::addFunctionExit(int functionId, uint32_t cycle, uint32_t pid)
{
    writeDataRecord(fData, pid, functionId | 1, cycle);
}

void DmTrace::addFunction(int functionId, const char *name)
{
    FunctionRecord *rec = new FunctionRecord;
    rec->id = functionId;
    rec->name = name;
    functions->push_back(rec);
}

void DmTrace::addFunction(int functionId, const char *clazz,
                          const char *method, const char *sig)
{
    // Allocate space for all the strings, plus 2 tab separators plus null byte.
    // We currently don't reclaim this space.
    int len = strlen(clazz) + strlen(method) + strlen(sig) + 3;
    char *name = new char[len];
    sprintf(name, "%s\t%s\t%s", clazz, method, sig);

    addFunction(functionId, name);
}

void DmTrace::parseAndAddFunction(int functionId, const char *name)
{
    // Parse the "name" string into "class", "method" and "signature".
    // The "name" string should look something like this:
    //   name = "java.util.LinkedList.size()I"
    // and it will be parsed into this:
    //   clazz = "java.util.LinkedList"
    //   method = "size"
    //   sig = "()I"

    // Find the first parenthesis, the start of the signature.
    char *paren = strchr(name, '(');

    // If not found, then add the original name.
    if (paren == NULL) {
        addFunction(functionId, name);
        return;
    }

    // Copy the signature
    int len = strlen(paren) + 1;
    char *sig = new char[len];
    strcpy(sig, paren);

    // Zero the parenthesis so that we can search backwards from the signature
    *paren = 0;

    // Search for the last period, the start of the method name
    char *dot = strrchr(name, '.');

    // If not found, then add the original name.
    if (dot == NULL || dot == name) {
        delete[] sig;
        *paren = '(';
        addFunction(functionId, name);
        return;
    }

    // Copy the method, not including the dot
    len = strlen(dot + 1) + 1;
    char *method = new char[len];
    strcpy(method, dot + 1);

    // Zero the dot to delimit the class name
    *dot = 0;

    addFunction(functionId, name, method, sig);

    // Free the space we allocated.
    delete[] sig;
    delete[] method;
}

void DmTrace::addThread(int threadId, const char *name)
{
    ThreadRecord *rec = new ThreadRecord;
    rec->id = threadId;
    rec->name = name;
    threads->push_back(rec);
}

void DmTrace::updateName(int threadId, const char *name)
{
    std::vector<ThreadRecord*>::iterator iter;

    for (iter = threads->begin(); iter != threads->end(); ++iter) {
        if ((*iter)->id == threadId) {
            (*iter)->name = name;
            return;
        }
    }
}

void DmTrace::writeKeyFile(FILE *fstream)
{
    fwrite(keyHeader, strlen(keyHeader), 1, fstream);
    writeThreads(fstream);
    writeFunctions(fstream);
    fwrite(keyEnd, strlen(keyEnd), 1, fstream);
}

void DmTrace::writeThreads(FILE *fstream)
{
    std::vector<ThreadRecord*>::iterator iter;

    fwrite(keyThreadHeader, strlen(keyThreadHeader), 1, fstream);
    for (iter = threads->begin(); iter != threads->end(); ++iter) {
        fprintf(fstream, "%d\t%s\n", (*iter)->id, (*iter)->name);
    }
}

void DmTrace::writeFunctions(FILE *fstream)
{
    std::vector<FunctionRecord*>::iterator iter;

    fwrite(keyFunctionHeader, strlen(keyFunctionHeader), 1, fstream);
    for (iter = functions->begin(); iter != functions->end(); ++iter) {
        fprintf(fstream, "0x%x\t%s\n", (*iter)->id, (*iter)->name);
    }
}