1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Object hierarchy for describing the traces in memory.
30 #ifndef _TRACE_MODEL_HPP_
31 #define _TRACE_MODEL_HPP_
45 // Should match Call::no
46 typedef unsigned CallNo;
56 const char **arg_names;
64 const char **member_names;
70 signed long long value;
77 const EnumValue *values;
83 unsigned long long value;
90 const BitmaskFlag *flags;
104 virtual void visit(Visitor &visitor) = 0;
106 virtual bool toBool(void) const = 0;
107 virtual signed long long toSInt(void) const;
108 virtual unsigned long long toUInt(void) const;
109 virtual float toFloat(void) const;
110 virtual double toDouble(void) const;
112 virtual void *toPointer(void) const;
113 virtual void *toPointer(bool bind);
114 virtual unsigned long long toUIntPtr(void) const;
115 virtual const char *toString(void) const;
117 virtual const Null *toNull(void) const { return NULL; }
118 virtual Null *toNull(void) { return NULL; }
120 virtual const Array *toArray(void) const { return NULL; }
121 virtual Array *toArray(void) { return NULL; }
123 virtual const Struct *toStruct(void) const { return NULL; }
124 virtual Struct *toStruct(void) { return NULL; }
126 const Value & operator[](size_t index) const;
130 class Null : public Value
133 bool toBool(void) const;
134 signed long long toSInt(void) const;
135 unsigned long long toUInt(void) const;
136 virtual float toFloat(void) const;
137 virtual double toDouble(void) const;
138 void *toPointer(void) const;
139 void *toPointer(bool bind);
140 unsigned long long toUIntPtr(void) const;
141 const char *toString(void) const;
142 void visit(Visitor &visitor);
144 const Null *toNull(void) const { return this; }
145 Null *toNull(void) { return this; }
149 class Bool : public Value
152 Bool(bool _value) : value(_value) {}
154 bool toBool(void) const;
155 signed long long toSInt(void) const;
156 unsigned long long toUInt(void) const;
157 virtual float toFloat(void) const;
158 virtual double toDouble(void) const;
159 void visit(Visitor &visitor);
165 class SInt : public Value
168 SInt(signed long long _value) : value(_value) {}
170 bool toBool(void) const;
171 signed long long toSInt(void) const;
172 unsigned long long toUInt(void) const;
173 virtual float toFloat(void) const;
174 virtual double toDouble(void) const;
175 void visit(Visitor &visitor);
177 signed long long value;
181 class UInt : public Value
184 UInt(unsigned long long _value) : value(_value) {}
186 bool toBool(void) const;
187 signed long long toSInt(void) const;
188 unsigned long long toUInt(void) const;
189 virtual float toFloat(void) const;
190 virtual double toDouble(void) const;
191 void visit(Visitor &visitor);
193 unsigned long long value;
197 class Float : public Value
200 Float(float _value) : value(_value) {}
202 bool toBool(void) const;
203 signed long long toSInt(void) const;
204 unsigned long long toUInt(void) const;
205 virtual float toFloat(void) const;
206 virtual double toDouble(void) const;
207 void visit(Visitor &visitor);
213 class Double : public Value
216 Double(double _value) : value(_value) {}
218 bool toBool(void) const;
219 signed long long toSInt(void) const;
220 unsigned long long toUInt(void) const;
221 virtual float toFloat(void) const;
222 virtual double toDouble(void) const;
223 void visit(Visitor &visitor);
229 class String : public Value
232 String(const char * _value) : value(_value) {}
235 bool toBool(void) const;
236 const char *toString(void) const;
237 void visit(Visitor &visitor);
243 class Enum : public SInt
246 Enum(const EnumSig *_sig, signed long long _value) : SInt(_value), sig(_sig) {}
248 void visit(Visitor &visitor);
254 // TODO: use a std::map
255 for (const EnumValue *it = sig->values; it != sig->values + sig->num_values; ++it) {
256 if (it->value == value) {
265 class Bitmask : public UInt
268 Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
270 void visit(Visitor &visitor);
272 const BitmaskSig *sig;
276 class Struct : public Value
279 Struct(StructSig *_sig) : sig(_sig), members(_sig->num_members) { }
282 bool toBool(void) const;
283 void visit(Visitor &visitor);
285 const Struct *toStruct(void) const { return this; }
286 Struct *toStruct(void) { return this; }
288 const StructSig *sig;
289 std::vector<Value *> members;
293 class Array : public Value
296 Array(size_t len) : values(len) {}
299 bool toBool(void) const;
300 void visit(Visitor &visitor);
302 const Array *toArray(void) const { return this; }
303 Array *toArray(void) { return this; }
305 std::vector<Value *> values;
309 return values.size();
314 class Blob : public Value
319 buf = new char[_size];
325 bool toBool(void) const;
326 void *toPointer(void) const;
327 void *toPointer(bool bind);
328 void visit(Visitor &visitor);
336 class Pointer : public UInt
339 Pointer(unsigned long long value) : UInt(value) {}
341 bool toBool(void) const;
342 void *toPointer(void) const;
343 void *toPointer(bool bind);
344 unsigned long long toUIntPtr(void) const;
345 void visit(Visitor &visitor);
349 class Repr : public Value
352 Repr(Value *human, Value *machine) :
354 machineValue(machine)
357 /** Human-readible value */
360 /** Machine-readible value */
363 virtual bool toBool(void) const;
364 virtual signed long long toSInt(void) const;
365 virtual unsigned long long toUInt(void) const;
366 virtual float toFloat(void) const;
367 virtual double toDouble(void) const;
369 virtual void *toPointer(void) const;
370 virtual void *toPointer(bool bind);
371 virtual unsigned long long toUIntPtr(void) const;
372 virtual const char *toString(void) const;
374 void visit(Visitor &visitor);
377 struct RawStackFrame {
380 const char * function;
381 const char * filename;
393 void dump(std::ostream &os) {
394 os << (this->module ? this->module : "?");
395 if (this->function != NULL) {
396 os << ": " << this->function;
398 if (this->offset >= 0) {
399 os << "+0x" << std::hex << this->offset << std::dec;
401 if (this->filename != NULL) {
402 os << ": " << this->filename;
403 if (this->linenumber >= 0) {
404 os << ":" << this->linenumber;
410 class StackFrame : public RawStackFrame {
415 typedef std::vector<StackFrame *> Backtrace;
420 virtual void visit(Null *);
421 virtual void visit(Bool *);
422 virtual void visit(SInt *);
423 virtual void visit(UInt *);
424 virtual void visit(Float *);
425 virtual void visit(Double *);
426 virtual void visit(String *);
427 virtual void visit(Enum *);
428 virtual void visit(Bitmask *);
429 virtual void visit(Struct *);
430 virtual void visit(Array *);
431 virtual void visit(Blob *);
432 virtual void visit(Pointer *);
433 virtual void visit(Repr *);
434 virtual void visit(Backtrace *);
435 virtual void visit(StackFrame *);
437 inline void _visit(Value *value) {
445 typedef unsigned CallFlags;
450 * TODO: It might be better to to record some of these (but not all) into the
456 * Whether a call was really done by the application or not.
458 * This flag is set for fake calls -- calls not truly done by the application
459 * but emitted and recorded for completeness, to provide contextual information
460 * necessary for retracing, that would not be available through other ways.
462 * XXX: This one definetely needs to go into the trace file.
464 CALL_FLAG_FAKE = (1 << 0),
467 * Whether this call should be retraced or ignored.
469 * This flag is set for calls which can't be safely replayed (due to incomplete
470 * information) or that have no sideffects.
472 * Some incomplete calls are unreproduceable, but not all.
474 CALL_FLAG_NON_REPRODUCIBLE = (1 << 1),
477 * Whether this call has no side-effects, therefore don't need to be
480 * This flag is set for calls that merely query information which is not
481 * needed for posterior calls.
483 CALL_FLAG_NO_SIDE_EFFECTS = (1 << 2),
486 * Whether this call renders into the bound rendertargets.
488 CALL_FLAG_RENDER = (1 << 3),
491 * Whether this call causes render target to be swapped.
493 * This does not mark frame termination by itself -- that's solely the
494 * responsibility of `endOfFrame` bit.
496 * This mean that snapshots should be take prior to the call, and not
499 CALL_FLAG_SWAP_RENDERTARGET = (1 << 4),
502 * Whether this call terminates a frame.
504 * XXX: This can't always be determined by the function name, so it should also
505 * go into the trace file eventually.
507 CALL_FLAG_END_FRAME = (1 << 5),
510 * Whether this call is incomplete, i.e., it never returned.
512 CALL_FLAG_INCOMPLETE = (1 << 6),
515 * Whether this call is verbose (i.e., not usually interesting).
517 CALL_FLAG_VERBOSE = (1 << 7),
532 const FunctionSig *sig;
533 std::vector<Arg> args;
537 Backtrace* backtrace;
539 Call(const FunctionSig *_sig, const CallFlags &_flags, unsigned _thread_id) :
540 thread_id(_thread_id),
542 args(_sig->num_args),
550 inline const char * name(void) const {
554 inline Value & arg(unsigned index) {
555 assert(index < args.size());
556 return *(args[index].value);
561 } /* namespace trace */
563 #endif /* _TRACE_MODEL_HPP_ */