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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/**************************************************************************
*
* Copyright 2010 VMware, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
/*
* Object hierarchy for describing the traces in memory.
*/
#pragma once
#include <assert.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <ostream>
namespace trace {
// Should match Call::no
typedef unsigned CallNo;
typedef unsigned Id;
struct FunctionSig {
Id id;
const char *name;
unsigned num_args;
const char **arg_names;
};
struct StructSig {
Id id;
const char *name;
unsigned num_members;
const char **member_names;
};
struct EnumValue {
const char *name;
signed long long value;
};
struct EnumSig {
Id id;
unsigned num_values;
const EnumValue *values;
};
struct BitmaskFlag {
const char *name;
unsigned long long value;
};
struct BitmaskSig {
Id id;
unsigned num_flags;
const BitmaskFlag *flags;
};
class Visitor;
class Null;
class Struct;
class Array;
class Blob;
class Value
{
public:
virtual ~Value() {}
virtual void visit(Visitor &visitor) = 0;
virtual bool toBool(void) const = 0;
virtual signed long long toSInt(void) const;
virtual unsigned long long toUInt(void) const;
virtual float toFloat(void) const;
virtual double toDouble(void) const;
virtual void *toPointer(void) const;
virtual void *toPointer(bool bind);
virtual unsigned long long toUIntPtr(void) const;
virtual const char *toString(void) const;
virtual const Null *toNull(void) const { return NULL; }
virtual Null *toNull(void) { return NULL; }
virtual const Array *toArray(void) const { return NULL; }
virtual Array *toArray(void) { return NULL; }
virtual const Struct *toStruct(void) const { return NULL; }
virtual Struct *toStruct(void) { return NULL; }
virtual const Blob *toBlob(void) const { return NULL; }
virtual Blob *toBlob(void) { return NULL; }
Value & operator[](size_t index) const;
};
class Null : public Value
{
public:
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void *toPointer(void) const override;
void *toPointer(bool bind) override;
unsigned long long toUIntPtr(void) const override;
const char *toString(void) const override;
void visit(Visitor &visitor) override;
const Null *toNull(void) const override { return this; }
Null *toNull(void) override { return this; }
};
class Bool : public Value
{
public:
Bool(bool _value) : value(_value) {}
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void visit(Visitor &visitor) override;
bool value;
};
class SInt : public Value
{
public:
SInt(signed long long _value) : value(_value) {}
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void visit(Visitor &visitor) override;
signed long long value;
};
class UInt : public Value
{
public:
UInt(unsigned long long _value) : value(_value) {}
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void visit(Visitor &visitor) override;
unsigned long long value;
};
class Float : public Value
{
public:
Float(float _value) : value(_value) {}
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void visit(Visitor &visitor) override;
float value;
};
class Double : public Value
{
public:
Double(double _value) : value(_value) {}
bool toBool(void) const override;
signed long long toSInt(void) const override;
unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
void visit(Visitor &visitor) override;
double value;
};
class String : public Value
{
public:
String(const char * _value) : value(_value) {}
~String();
bool toBool(void) const override;
const char *toString(void) const override;
void visit(Visitor &visitor) override;
const char * value;
};
class WString : public Value
{
public:
WString(const wchar_t * _value) : value(_value) {}
~WString();
bool toBool(void) const override;
void visit(Visitor &visitor) override;
const wchar_t * value;
};
class Enum : public SInt
{
public:
Enum(const EnumSig *_sig, signed long long _value) : SInt(_value), sig(_sig) {}
void visit(Visitor &visitor) override;
const EnumSig *sig;
const EnumValue *
lookup() {
// TODO: use a std::map
for (const EnumValue *it = sig->values; it != sig->values + sig->num_values; ++it) {
if (it->value == value) {
return it;
}
}
return NULL;
}
};
class Bitmask : public UInt
{
public:
Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
void visit(Visitor &visitor) override;
const BitmaskSig *sig;
};
class Struct : public Value
{
public:
Struct(StructSig *_sig) : sig(_sig), members(_sig->num_members) { }
~Struct();
bool toBool(void) const override;
void visit(Visitor &visitor) override;
const Struct *toStruct(void) const override { return this; }
Struct *toStruct(void) override { return this; }
const StructSig *sig;
std::vector<Value *> members;
};
class Array : public Value
{
public:
Array(size_t len) : values(len) {}
~Array();
bool toBool(void) const override;
void visit(Visitor &visitor) override;
const Array *toArray(void) const override { return this; }
Array *toArray(void) override { return this; }
std::vector<Value *> values;
inline size_t
size(void) const {
return values.size();
}
};
class Blob : public Value
{
public:
Blob(size_t _size) {
size = _size;
buf = new char[_size];
bound = false;
}
~Blob();
bool toBool(void) const override;
void *toPointer(void) const override;
void *toPointer(bool bind) override;
void visit(Visitor &visitor) override;
const Blob *toBlob(void) const override { return this; }
Blob *toBlob(void) override { return this; }
size_t size;
char *buf;
bool bound;
};
class Pointer : public UInt
{
public:
Pointer(unsigned long long value) : UInt(value) {}
bool toBool(void) const override;
void *toPointer(void) const override;
void *toPointer(bool bind) override;
unsigned long long toUIntPtr(void) const override;
void visit(Visitor &visitor) override;
};
class Repr : public Value
{
public:
Repr(Value *human, Value *machine) :
humanValue(human),
machineValue(machine)
{}
/** Human-readible value */
Value *humanValue;
/** Machine-readible value */
Value *machineValue;
virtual bool toBool(void) const override;
virtual signed long long toSInt(void) const override;
virtual unsigned long long toUInt(void) const override;
virtual float toFloat(void) const override;
virtual double toDouble(void) const override;
virtual void *toPointer(void) const override;
virtual void *toPointer(bool bind) override;
virtual unsigned long long toUIntPtr(void) const override;
virtual const char *toString(void) const override;
void visit(Visitor &visitor) override;
};
struct RawStackFrame {
Id id;
const char * module;
const char * function;
const char * filename;
int linenumber;
long long offset;
RawStackFrame() :
module(0),
function(0),
filename(0),
linenumber(-1),
offset(-1)
{
}
void dump(std::ostream &os) {
os << (this->module ? this->module : "?");
if (this->function != NULL) {
os << ": " << this->function;
}
if (this->offset >= 0) {
os << "+0x" << std::hex << this->offset << std::dec;
}
if (this->filename != NULL) {
os << ": " << this->filename;
if (this->linenumber >= 0) {
os << ":" << this->linenumber;
}
}
}
};
class StackFrame : public RawStackFrame {
public:
~StackFrame();
};
typedef std::vector<StackFrame *> Backtrace;
class Visitor
{
public:
virtual void visit(Null *);
virtual void visit(Bool *);
virtual void visit(SInt *);
virtual void visit(UInt *);
virtual void visit(Float *);
virtual void visit(Double *);
virtual void visit(String *);
virtual void visit(WString *);
virtual void visit(Enum *);
virtual void visit(Bitmask *);
virtual void visit(Struct *);
virtual void visit(Array *);
virtual void visit(Blob *);
virtual void visit(Pointer *);
virtual void visit(Repr *);
protected:
inline void _visit(Value *value) {
if (value) {
value->visit(*this);
}
}
};
typedef unsigned CallFlags;
/**
* Call flags.
*
* TODO: It might be better to to record some of these (but not all) into the
* trace file.
*/
enum {
/**
* Whether a call was really done by the application or not.
*
* This flag is set for fake calls -- calls not truly done by the application
* but emitted and recorded for completeness, to provide contextual information
* necessary for retracing, that would not be available through other ways.
*
* XXX: This one definetely needs to go into the trace file.
*/
CALL_FLAG_FAKE = (1 << 0),
/**
* Whether this call should be retraced or ignored.
*
* This flag is set for calls which can't be safely replayed (due to incomplete
* information) or that have no sideffects.
*
* Some incomplete calls are unreproduceable, but not all.
*/
CALL_FLAG_NON_REPRODUCIBLE = (1 << 1),
/**
* Whether this call has no side-effects, therefore don't need to be
* retraced.
*
* This flag is set for calls that merely query information which is not
* needed for posterior calls.
*/
CALL_FLAG_NO_SIDE_EFFECTS = (1 << 2),
/**
* Whether this call renders into the bound rendertargets.
*/
CALL_FLAG_RENDER = (1 << 3),
/**
* Whether this call causes render target to be swapped.
*
* This does not mark frame termination by itself -- that's solely the
* responsibility of `endOfFrame` bit.
*
* This mean that snapshots should be take prior to the call, and not
* after.
*/
CALL_FLAG_SWAP_RENDERTARGET = (1 << 4),
/**
* Whether this call terminates a frame.
*
* XXX: This can't always be determined by the function name, so it should also
* go into the trace file eventually.
*/
CALL_FLAG_END_FRAME = (1 << 5),
/**
* Whether this call is incomplete, i.e., it never returned.
*/
CALL_FLAG_INCOMPLETE = (1 << 6),
/**
* Whether this call is verbose (i.e., not usually interesting).
*/
CALL_FLAG_VERBOSE = (1 << 7),
/**
* String markers.
*/
CALL_FLAG_MARKER = (1 << 8),
CALL_FLAG_MARKER_PUSH = (1 << 9),
CALL_FLAG_MARKER_POP = (1 << 10),
};
struct Arg
{
Value *value;
};
class Call
{
public:
unsigned thread_id;
unsigned no;
const FunctionSig *sig;
std::vector<Arg> args;
Value *ret;
CallFlags flags;
Backtrace* backtrace;
Call(const FunctionSig *_sig, const CallFlags &_flags, unsigned _thread_id) :
thread_id(_thread_id),
sig(_sig),
args(_sig->num_args),
ret(0),
flags(_flags),
backtrace(0) {
}
~Call();
inline const char *
name(void) const {
return sig->name;
}
inline Value &
arg(unsigned index) {
assert(index < args.size());
return *(args[index].value);
}
Value &
argByName(const char *argName);
};
} /* namespace trace */
|