forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trace.cpp
380 lines (342 loc) · 9.05 KB
/
Trace.cpp
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "Trace.h"
#include "SystemState.h"
#include "Node.h"
#include "Core.h"
#include "Resource.h"
#include "Exceptions.h"
#include <iomanip>
#include <sstream>
#include <cstring>
using namespace Register;
const unsigned mnemonicColumn = 49;
const unsigned regWriteColumn = 87;
Tracer Tracer::instance;
Tracer::PushLineState::PushLineState() :
needRestore(false),
line(Tracer::get().line.pending)
{
if (Tracer::get().line.thread) {
std::swap(Tracer::get().line, line);
needRestore = true;
}
}
Tracer::PushLineState::~PushLineState()
{
if (needRestore) {
std::swap(Tracer::get().line, line);
}
}
void Tracer::setSymbolInfo(std::auto_ptr<SymbolInfo> &si)
{
symInfo = si;
}
void Tracer::setColour(bool enable)
{
colours = enable ? TerminalColours::ansi : TerminalColours::null;
}
void Tracer::escapeCode(const char *s)
{
*line.buf << s;
line.numEscapeChars += std::strlen(s);
}
void Tracer::printCommonEnd()
{
*line.buf << '\n';
if (line.out) {
*line.out << line.buf->str() << line.pending->str();
line.buf->str("");
}
line.thread = 0;
}
void Tracer::printCommonStart()
{
if (line.pending) {
line.pending->str("");
}
line.numEscapeChars = 0;
line.hadRegWrite = false;
line.thread = 0;
}
void Tracer::printThreadName()
{
*line.buf << line.thread->getParent().getCoreName();
*line.buf << ":t" << line.thread->getNum();
}
void Tracer::printCommonStart(const Thread &t)
{
printCommonStart();
line.thread = &t;
// TODO add option to show cycles?
//*line.buf << std::setw(6) << (uint64_t)line.thread->time;
green();
*line.buf << '<';
printThreadName();
*line.buf << '>';
reset();
}
void Tracer::printCommonStart(const Node &n)
{
printCommonStart();
green();
*line.buf << '<';
*line.buf << 'n' << n.getNodeID();
*line.buf << '>';
reset();
}
void Tracer::printThreadPC()
{
unsigned pc = line.thread->getParent().targetPc(line.thread->pc);
const Core *core = &line.thread->getParent();
const ElfSymbol *sym;
if (symInfo.get() && (sym = symInfo->getFunctionSymbol(core, pc))) {
*line.buf << sym->name;
if (sym->value != pc)
*line.buf << '+' << (pc - sym->value);
*line.buf << "(0x" << std::hex << pc << std::dec << ')';
} else {
*line.buf << "0x" << std::hex << pc << std::dec;
}
}
void Tracer::printInstructionStart(const Thread &t)
{
printCommonStart(t);
*line.buf << ' ';
printThreadPC();
*line.buf << ": ";
// Align
size_t pos = line.buf->str().size() - line.numEscapeChars;
if (pos < mnemonicColumn) {
*line.buf << std::setw(mnemonicColumn - pos) << "";
}
}
void Tracer::printOperand(SrcRegister op)
{
Register::Reg reg = op.getRegister();
*line.buf << reg << "(0x" << std::hex << line.thread->regs[reg] << ')'
<< std::dec;
}
void Tracer::printOperand(DestRegister op)
{
*line.buf << op.getRegister();
}
void Tracer::printOperand(SrcDestRegister op)
{
Register::Reg reg = op.getRegister();
*line.buf << reg << "(0x" << std::hex << line.thread->regs[reg] << ')'
<< std::dec;
}
void Tracer::printOperand(CPRelOffset op)
{
uint32_t cpValue = line.thread->regs[CP];
uint32_t address = cpValue + (op.getOffset() << 2);
const Core *core = &line.thread->getParent();
const ElfSymbol *sym, *cpSym;
if (symInfo.get() &&
(sym = symInfo->getDataSymbol(core, address)) &&
sym->value == address &&
(cpSym = symInfo->getGlobalSymbol(core, "_cp")) &&
cpSym->value == cpValue) {
*line.buf << sym->name;
*line.buf << "(0x" << std::hex << address << ')';
} else {
*line.buf << op.getOffset();
}
}
void Tracer::printOperand(DPRelOffset op)
{
uint32_t dpValue = line.thread->regs[DP];
uint32_t address = dpValue + (op.getOffset() << 2);
const Core *core = &line.thread->getParent();
const ElfSymbol *sym, *dpSym;
if (symInfo.get() &&
(sym = symInfo->getDataSymbol(core, address)) &&
sym->value == address &&
(dpSym = symInfo->getGlobalSymbol(core, "_dp")) &&
dpSym->value == dpValue) {
*line.buf << sym->name;
*line.buf << "(0x" << std::hex << address << std::dec << ')';
} else {
*line.buf << op.getOffset();
}
}
void Tracer::regWrite(Reg reg, uint32_t value)
{
if (!line.hadRegWrite) {
*line.buf << ' ';
// Align
size_t pos = line.buf->str().size() - line.numEscapeChars;
if (pos < regWriteColumn) {
*line.buf << std::setw(regWriteColumn - pos) << "";
}
*line.buf << "# ";
} else {
*line.buf << ", ";
}
*line.buf << reg << "=0x" << std::hex << value << std::dec;
line.hadRegWrite = true;
}
void Tracer::SSwitchRead(const Node &node, uint32_t retAddress, uint16_t regNum)
{
PushLineState save;
printCommonStart(node);
red();
*line.buf << " SSwitch read: ";
*line.buf << "register 0x" << std::hex << regNum;
*line.buf << ", reply address 0x" << retAddress << std::dec;
reset();
printCommonEnd();
}
void Tracer::
SSwitchWrite(const Node &node, uint32_t retAddress, uint16_t regNum,
uint32_t value)
{
PushLineState save;
printCommonStart(node);
red();
*line.buf << " SSwitch write: ";
*line.buf << "register 0x" << std::hex << regNum;
*line.buf << ", value 0x" << value;
*line.buf << ", reply address 0x" << retAddress << std::dec;
reset();
printCommonEnd();
}
void Tracer::SSwitchNack(const Node &node, uint32_t dest)
{
PushLineState save;
printCommonStart(node);
red();
*line.buf << " SSwitch reply: NACK";
*line.buf << ", destintion 0x" << std::hex << dest << std::dec;
reset();
printCommonEnd();
}
void Tracer::SSwitchAck(const Node &node, uint32_t dest)
{
PushLineState save;
printCommonStart(node);
red();
*line.buf << " SSwitch reply: ACK";
*line.buf << ", destintion 0x" << std::hex << dest << std::dec;
reset();
printCommonEnd();
}
void Tracer::SSwitchAck(const Node &node, uint32_t data, uint32_t dest)
{
PushLineState save;
printCommonStart(node);
red();
*line.buf << " SSwitch reply: ACK";
*line.buf << ", data 0x" << std::hex << data;
*line.buf << ", destintion 0x" << dest << std::dec;
reset();
printCommonEnd();
}
void Tracer::
event(const Thread &t, const EventableResource &res, uint32_t pc,
uint32_t ev)
{
PushLineState save;
printCommonStart(t);
red();
*line.buf << " Event caused by "
<< Resource::getResourceName(static_cast<const Resource&>(res).getType())
<< " 0x" << std::hex << (uint32_t)res.getID() << std::dec;
reset();
regWrite(ED, ev);
printCommonEnd();
}
void Tracer::
interrupt(const Thread &t, const EventableResource &res, uint32_t pc,
uint32_t ssr, uint32_t spc, uint32_t sed, uint32_t ed)
{
PushLineState save;
printCommonStart(t);
red();
*line.buf << " Interrupt caused by "
<< Resource::getResourceName(static_cast<const Resource&>(res).getType())
<< " 0x" << std::hex << (uint32_t)res.getID() << std::dec;
reset();
regWrite(ED, ed);
regWrite(SSR, ssr);
regWrite(SPC, spc);
regWrite(SED, sed);
printCommonEnd();
}
void Tracer::
exception(const Thread &t, uint32_t et, uint32_t ed,
uint32_t sed, uint32_t ssr, uint32_t spc)
{
PushLineState save;
printCommonStart(t);
red();
*line.buf << ' ' << Exceptions::getExceptionName(et) << " exception";
reset();
regWrite(ET, et);
regWrite(ED, ed);
regWrite(SSR, ssr);
regWrite(SPC, spc);
regWrite(SED, sed);
printCommonEnd();
}
void Tracer::
syscallBegin(const Thread &t)
{
printCommonStart(t);
red();
*line.buf << " Syscall ";
}
void Tracer::dumpThreadSummary(const Core &core)
{
for (unsigned i = 0; i < NUM_THREADS; i++) {
const Thread &t = core.getThread(i);
if (!t.isInUse())
continue;
const Thread &ts = t;
printCommonStart();
line.thread = &ts;
*line.buf << "Thread ";
printThreadName();
if (ts.waiting()) {
if (Resource *res = ts.pausedOn) {
*line.buf << " paused on ";
*line.buf << Resource::getResourceName(res->getType());
*line.buf << " 0x" << std::hex << res->getID();
} else if (ts.eeble()) {
*line.buf << " waiting for events";
if (ts.ieble())
*line.buf << " or interrupts";
} else if (ts.ieble()) {
*line.buf << " waiting for interrupts";
} else {
*line.buf << " paused";
}
}
*line.buf << " at ";
printThreadPC();
printCommonEnd();
}
}
void Tracer::dumpThreadSummary(const SystemState &system)
{
for (SystemState::const_node_iterator outerIt = system.node_begin(),
outerE = system.node_end(); outerIt != outerE; ++outerIt) {
const Node &node = **outerIt;
for (Node::const_core_iterator innerIt = node.core_begin(),
innerE = node.core_end(); innerIt != innerE; ++innerIt) {
dumpThreadSummary(**innerIt);
}
}
}
void Tracer::noRunnableThreads(const SystemState &system)
{
printCommonStart();
red();
*line.buf << "No more runnable threads";
reset();
printCommonEnd();
dumpThreadSummary(system);
}