]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Make Value::blob a virtual method.
[apitrace] / trace_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
29
30
31 namespace Trace {
32
33
34 Call::~Call() {
35     for (unsigned i = 0; i < args.size(); ++i) {
36         delete args[i];
37     }
38
39     if (ret) {
40         delete ret;
41     }
42 }
43
44
45 Struct::~Struct() {
46     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
47         delete *it;
48     }
49 }
50
51
52 Array::~Array() {
53     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
54         delete *it;
55     }
56 }
57
58 Blob::~Blob() {
59     // TODO: Don't leak blobs.  Blobs are often bound and accessed during many
60     // calls, so we can't delete them here.
61     //delete [] buf;
62 }
63
64
65 // virtual Value::blob()
66 void * Value  ::blob(void) const { assert(0); return NULL; }
67 void * Null   ::blob(void) const { return NULL; }
68 void * Blob   ::blob(void) const { return buf; }
69 void * Pointer::blob(void) const { assert(value < 0x100000ULL); return (void *)value; }
70
71
72 // virtual Value::visit()
73 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
74 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
75 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
76 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
77 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
78 void String ::visit(Visitor &visitor) { visitor.visit(this); }
79 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
80 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
81 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
82 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
83 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
84 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
85
86
87 void Visitor::visit(Null *) { assert(0); }
88 void Visitor::visit(Bool *) { assert(0); }
89 void Visitor::visit(SInt *) { assert(0); }
90 void Visitor::visit(UInt *) { assert(0); }
91 void Visitor::visit(Float *) { assert(0); }
92 void Visitor::visit(String *) { assert(0); }
93 void Visitor::visit(Enum *) { assert(0); }
94 void Visitor::visit(Bitmask *bitmask) { visit(static_cast<UInt *>(bitmask)); }
95 void Visitor::visit(Struct *) { assert(0); }
96 void Visitor::visit(Array *) { assert(0); }
97 void Visitor::visit(Blob *) { assert(0); }
98 void Visitor::visit(Pointer *) { assert(0); }
99
100
101 class Dumper : public Visitor
102 {
103 protected:
104     std::ostream &os;
105     Formatter::Formatter *formatter;
106     Formatter::Attribute *normal;
107     Formatter::Attribute *bold;
108     Formatter::Attribute *italic;
109     Formatter::Attribute *red;
110     Formatter::Attribute *pointer;
111     Formatter::Attribute *literal;
112
113 public:
114     Dumper(std::ostream &_os) : os(_os) {
115         formatter = Formatter::defaultFormatter();
116         normal = formatter->normal();
117         bold = formatter->bold();
118         italic = formatter->italic();
119         red = formatter->color(Formatter::RED);
120         pointer = formatter->color(Formatter::GREEN);
121         literal = formatter->color(Formatter::BLUE);
122     }
123
124     ~Dumper() {
125         delete normal;
126         delete bold;
127         delete italic;
128         delete red;
129         delete pointer;
130         delete literal;
131         delete formatter;
132     }
133
134     void visit(Null *) {
135         os << "NULL";
136     }
137
138     void visit(Bool *node) {
139         os << literal << (node->value ? "true" : "false") << normal;
140     }
141
142     void visit(SInt *node) {
143         os << literal << node->value << normal;
144     }
145
146     void visit(UInt *node) {
147         os << literal << node->value << normal;
148     }
149
150     void visit(Float *node) {
151         os << literal << node->value << normal;
152     }
153
154     void visit(String *node) {
155         os << literal << '"' << node->value << '"' << normal;
156     }
157
158     void visit(Enum *node) {
159         os << literal << node->sig->first << normal;
160     }
161
162     void visit(Bitmask *bitmask) {
163         unsigned long long value = bitmask->value;
164         const Bitmask::Signature *sig = bitmask->sig;
165         bool first = true;
166         for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
167             assert(it->second);
168             if ((value & it->second) == it->second) {
169                 if (!first) {
170                     os << " | ";
171                 }
172                 os << literal << it->first << normal;
173                 value &= ~it->second;
174                 first = false;
175             }
176         }
177         if (value || first) {
178             if (!first) {
179                 os << " | ";
180             }
181             os << literal << "0x" << std::hex << value << std::dec << normal;
182         }
183     }
184
185     void visit(Struct *s) {
186         const char *sep = "";
187         os << "{";
188         for (unsigned i = 0; i < s->members.size(); ++i) {
189             os << sep << italic << s->sig->member_names[i] << normal << " = ";
190             _visit(s->members[i]);
191             sep = ", ";
192         }
193         os << "}";
194     }
195
196     void visit(Array *array) {
197         if (array->values.size() == 1) {
198             os << "&";
199             _visit(array->values[0]);
200         }
201         else {
202             const char *sep = "";
203             os << "{";
204             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
205                 os << sep;
206                 _visit(*it);
207                 sep = ", ";
208             }
209             os << "}";
210         }
211     }
212
213     void visit(Blob *blob) {
214         os << pointer << "blob(" << blob->size << ")" << normal;
215     }
216
217     void visit(Pointer *p) {
218         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
219     }
220
221     void visit(Call *call) {
222         const char *sep = "";
223         os << bold << call->sig->name << normal << "(";
224         for (unsigned i = 0; i < call->args.size(); ++i) {
225             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
226             _visit(call->args[i]);
227             sep = ", ";
228         }
229         os << ")";
230         if (call->ret) {
231             os << " = ";
232             _visit(call->ret);
233         }
234         os << "\n";
235     }
236 };
237
238
239 std::ostream & operator <<(std::ostream &os, Value *value) {
240     Dumper d(os);
241     if (value) {
242         value->visit(d);
243     }
244     return os;
245 }
246
247
248 static inline const Value *unwrap(const Value *node) {
249     const Enum *c = dynamic_cast<const Enum *>(node);
250     if (c)
251         return c->sig->second;
252     return node;
253 }
254
255
256 Value::operator bool(void) const {
257     const Bool *b = dynamic_cast<const Bool *>(unwrap(this));
258     if (b)
259         return b->value;
260     assert(0);
261     return false;
262 }
263
264 Value::operator signed long long(void) const {
265     const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
266     if (sint)
267         return sint->value;
268     const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
269     if (uint)
270         return uint->value;
271     assert(0);
272     return 0;
273 }
274
275 Value::operator unsigned long long(void) const {
276     const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
277     if (uint)
278         return uint->value;
279     assert(0);
280     return 0;
281 }
282
283
284 Value::operator double(void) const {
285     const Float *fl = dynamic_cast<const Float *>(unwrap(this));
286     assert(fl);
287     return fl->value;
288 }
289
290 static Null null;
291
292 const Value & Value::operator[](size_t index) const {
293     const Array *array = dynamic_cast<const Array *>(unwrap(this));
294     if (array) {
295         if (index < array->values.size()) {
296             return *array->values[index];
297         }
298     }
299     return null;
300 }
301
302 const char * Value::string(void) const {
303     const String *string = dynamic_cast<const String *>(unwrap(this));
304     if (string)
305         return string->value.c_str();
306     const Null *null = dynamic_cast<const Null *>(unwrap(this));
307     if (null)
308         return NULL;
309     assert(0);
310     return NULL;
311 }
312
313 std::ostream & operator <<(std::ostream &os, Call &call) {
314     Dumper d(os);
315     os << call.no << " ";
316     d.visit(&call);
317     return os;
318 }
319
320
321 } /* namespace Trace */