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
|
// Copyright 2006 The Android Open Source Project
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
#include "trace_reader.h"
#include "bitvector.h"
#include "parse_options.h"
#include "dmtrace.h"
#include "armdis.h"
struct symbol {
uint32_t id;
};
typedef TraceReader<symbol> TraceReaderType;
#include "parse_options-inl.h"
#include "callstack.h"
DmTrace *dmtrace;
class MyFrame : public StackFrame<symbol_type> {
public:
void push(int stackLevel, uint64_t time, CallStackBase *base);
void pop(int stackLevel, uint64_t time, CallStackBase *base);
};
typedef CallStack<MyFrame> CallStackType;
static const int kNumStackFrames = 500;
static const int kMaxThreads = (32 * 1024);
uint64_t thread_time[kMaxThreads];
class FunctionStack {
public:
FunctionStack() {
top = 0;
}
void push(symbol_type *sym) {
if (top >= kNumStackFrames)
return;
frames[top] = sym;
top += 1;
}
symbol_type* pop() {
if (top <= 0) {
return NULL;
}
top -= 1;
return frames[top];
}
void showStack() {
fprintf(stderr, "top %d\n", top);
for (int ii = 0; ii < top; ii++) {
fprintf(stderr, " %d: %s\n", ii, frames[ii]->name);
}
}
private:
int top;
symbol_type *frames[kNumStackFrames];
};
FunctionStack *dmtrace_stack[kMaxThreads];
void MyFrame::push(int stackLevel, uint64_t time, CallStackBase *base)
{
int pid = base->getId();
CallStackType *stack = (CallStackType *) base;
#if 0
fprintf(stderr, "native push t %llu p %d s %d fid %d 0x%x %s\n",
stack->getGlobalTime(time), pid, stackLevel,
function->id, function->addr, function->name);
#endif
FunctionStack *fstack = dmtrace_stack[pid];
if (fstack == NULL) {
fstack = new FunctionStack();
dmtrace_stack[pid] = fstack;
}
fstack->push(function);
thread_time[pid] = time;
dmtrace->addFunctionEntry(function->id, time, pid);
}
void MyFrame::pop(int stackLevel, uint64_t time, CallStackBase *base)
{
int pid = base->getId();
CallStackType *stack = (CallStackType *) base;
#if 0
fprintf(stderr, "native pop t %llu p %d s %d fid %d 0x%x %s\n",
stack->getGlobalTime(time), pid, stackLevel,
function->id, function->addr, function->name);
#endif
FunctionStack *fstack = dmtrace_stack[pid];
if (fstack == NULL) {
fstack = new FunctionStack();
dmtrace_stack[pid] = fstack;
}
symbol_type *sym = fstack->pop();
if (sym != NULL && sym != function) {
fprintf(stderr, "Error: q2dm function mismatch at time %llu pid %d sym %s\n",
stack->getGlobalTime(time), pid, sym->name);
fstack->showStack();
exit(1);
}
thread_time[pid] = time;
dmtrace->addFunctionExit(function->id, time, pid);
}
uint32_t nextFunctionId = 4;
CallStackType *stacks[kMaxThreads];
void Usage(const char *program)
{
fprintf(stderr, "Usage: %s [options] trace_name elf_file dmtrace_name\n",
program);
OptionsUsage();
}
int main(int argc, char **argv)
{
bool useKernelStack = true;
ParseOptions(argc, argv);
if (argc - optind != 3) {
Usage(argv[0]);
exit(1);
}
char *qemu_trace_file = argv[optind++];
char *elf_file = argv[optind++];
char *dmtrace_file = argv[optind++];
TraceReaderType *trace = new TraceReaderType;
trace->Open(qemu_trace_file);
trace->SetDemangle(demangle);
trace->ReadKernelSymbols(elf_file);
trace->SetRoot(root);
TraceHeader *qheader = trace->GetHeader();
uint64_t startTime = qheader->start_sec;
startTime = (startTime << 32) | qheader->start_usec;
int kernelPid = qheader->first_unused_pid;
dmtrace = new DmTrace;
dmtrace->open(dmtrace_file, startTime);
bool inKernel = false;
CallStackType *kernelStack = NULL;
if (useKernelStack) {
// Create a fake kernel thread stack where we will put all the kernel
// code.
kernelStack = new CallStackType(kernelPid, kNumStackFrames, trace);
dmtrace->addThread(kernelPid, "(kernel)");
}
CallStackType *prevStack = NULL;
BBEvent event;
while (1) {
BBEvent ignored;
symbol_type *function;
if (GetNextValidEvent(trace, &event, &ignored, &function))
break;
if (event.bb_num == 0)
break;
#if 0
fprintf(stderr, "event t %llu p %d %s\n",
event.time, event.pid, function->name);
#endif
CallStackType *pStack;
if (useKernelStack) {
uint32_t flags = function->region->flags;
uint32_t region_mask = region_type::kIsKernelRegion
| region_type::kIsUserMappedRegion;
if ((flags & region_mask) == region_type::kIsKernelRegion) {
// Use the kernel stack
pStack = kernelStack;
inKernel = true;
} else {
// If we were just in the kernel then pop off all of the
// stack frames for the kernel thread.
if (inKernel == true) {
inKernel = false;
kernelStack->popAll(event.time);
}
// Get the stack for the current thread
pStack = stacks[event.pid];
}
} else {
// Get the stack for the current thread
pStack = stacks[event.pid];
}
// If the stack does not exist, then allocate a new one.
if (pStack == NULL) {
pStack = new CallStackType(event.pid, kNumStackFrames, trace);
stacks[event.pid] = pStack;
char *name = trace->GetProcessName(event.pid);
dmtrace->addThread(event.pid, name);
}
if (prevStack != pStack) {
pStack->threadStart(event.time);
if (prevStack)
prevStack->threadStop(event.time);
}
prevStack = pStack;
// If we have never seen this function before, then add it to the
// list of known functions.
if (function->id == 0) {
function->id = nextFunctionId;
nextFunctionId += 4;
uint32_t flags = function->region->flags;
const char *name = function->name;
if (flags & region_type::kIsKernelRegion) {
// To distinguish kernel function names from user library
// names, add a marker to the name.
int len = strlen(name) + strlen(" [kernel]") + 1;
char *kernelName = new char[len];
strcpy(kernelName, name);
strcat(kernelName, " [kernel]");
name = kernelName;
}
dmtrace->parseAndAddFunction(function->id, name);
}
// Update the stack
pStack->updateStack(&event, function);
}
if (prevStack == NULL) {
fprintf(stderr, "Error: no events in trace.\n");
exit(1);
}
prevStack->threadStop(event.time);
for (int ii = 0; ii < kMaxThreads; ++ii) {
if (stacks[ii]) {
stacks[ii]->threadStart(event.time);
stacks[ii]->popAll(event.time);
}
}
if (useKernelStack) {
kernelStack->popAll(event.time);
}
// Read the pid events to find the names of the processes
while (1) {
PidEvent pid_event;
if (trace->ReadPidEvent(&pid_event))
break;
if (pid_event.rec_type == kPidName) {
dmtrace->updateName(pid_event.pid, pid_event.path);
}
}
dmtrace->close();
delete dmtrace;
delete trace;
return 0;
}
|