]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Merge branch 'master' into multi-context
[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 // bool cast
66 Null   ::operator bool(void) const { return false; }
67 Bool   ::operator bool(void) const { return value; }
68 SInt   ::operator bool(void) const { return value != 0; }
69 UInt   ::operator bool(void) const { return value != 0; }
70 Float  ::operator bool(void) const { return value != 0; }
71 String ::operator bool(void) const { return true; }
72 Enum   ::operator bool(void) const { return static_cast<bool>(*sig->second); }
73 Struct ::operator bool(void) const { return true; }
74 Array  ::operator bool(void) const { return true; }
75 Blob   ::operator bool(void) const { return true; }
76 Pointer::operator bool(void) const { return value != 0; }
77
78
79 // signed integer cast
80 Value  ::operator signed long long (void) const { assert(0); return 0; }
81 Null   ::operator signed long long (void) const { return 0; }
82 Bool   ::operator signed long long (void) const { return static_cast<signed long long>(value); }
83 SInt   ::operator signed long long (void) const { return value; }
84 UInt   ::operator signed long long (void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
85 Float  ::operator signed long long (void) const { return static_cast<signed long long>(value); }
86 Enum   ::operator signed long long (void) const { return static_cast<signed long long>(*sig->second); }
87
88
89 // unsigned integer cast
90 Value  ::operator unsigned long long (void) const { assert(0); return 0; }
91 Null   ::operator unsigned long long (void) const { return 0; }
92 Bool   ::operator unsigned long long (void) const { return static_cast<unsigned long long>(value); }
93 SInt   ::operator unsigned long long (void) const { assert(value >= 0); return static_cast<signed long long>(value); }
94 UInt   ::operator unsigned long long (void) const { return value; }
95 Float  ::operator unsigned long long (void) const { return static_cast<unsigned long long>(value); }
96 Enum   ::operator unsigned long long (void) const { return static_cast<unsigned long long>(*sig->second); }
97
98
99 // floating point cast
100 Value  ::operator double (void) const { assert(0); return 0; }
101 Null   ::operator double (void) const { return 0; }
102 Bool   ::operator double (void) const { return static_cast<double>(value); }
103 SInt   ::operator double (void) const { return static_cast<double>(value); }
104 UInt   ::operator double (void) const { return static_cast<double>(value); }
105 Float  ::operator double (void) const { return value; }
106 Enum   ::operator double (void) const { return static_cast<unsigned long long>(*sig->second); }
107
108
109 // pointer cast
110 void * Value  ::toPointer(void) const { assert(0); return NULL; }
111 void * Null   ::toPointer(void) const { return NULL; }
112 void * Blob   ::toPointer(void) const { return buf; }
113 void * Pointer::toPointer(void) const { return (void *)value; }
114
115
116 // pointer cast
117 unsigned long long Value  ::toUIntPtr(void) const { assert(0); return 0; }
118 unsigned long long Null   ::toUIntPtr(void) const { return 0; }
119 unsigned long long Pointer::toUIntPtr(void) const { return value; }
120
121
122 // string cast
123 const char * Value ::toString(void) const { assert(0); return NULL; }
124 const char * Null  ::toString(void) const { return NULL; }
125 const char * String::toString(void) const { return value.c_str(); }
126
127
128 // virtual Value::visit()
129 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
130 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
131 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
132 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
133 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
134 void String ::visit(Visitor &visitor) { visitor.visit(this); }
135 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
136 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
137 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
138 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
139 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
140 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
141
142
143 void Visitor::visit(Null *) { assert(0); }
144 void Visitor::visit(Bool *) { assert(0); }
145 void Visitor::visit(SInt *) { assert(0); }
146 void Visitor::visit(UInt *) { assert(0); }
147 void Visitor::visit(Float *) { assert(0); }
148 void Visitor::visit(String *) { assert(0); }
149 void Visitor::visit(Enum *node) { _visit(node->sig->second); }
150 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
151 void Visitor::visit(Struct *) { assert(0); }
152 void Visitor::visit(Array *) { assert(0); }
153 void Visitor::visit(Blob *) { assert(0); }
154 void Visitor::visit(Pointer *) { assert(0); }
155
156
157 class Dumper : public Visitor
158 {
159 protected:
160     std::ostream &os;
161     Formatter::Formatter *formatter;
162     Formatter::Attribute *normal;
163     Formatter::Attribute *bold;
164     Formatter::Attribute *italic;
165     Formatter::Attribute *red;
166     Formatter::Attribute *pointer;
167     Formatter::Attribute *literal;
168
169 public:
170     Dumper(std::ostream &_os) : os(_os) {
171         formatter = Formatter::defaultFormatter();
172         normal = formatter->normal();
173         bold = formatter->bold();
174         italic = formatter->italic();
175         red = formatter->color(Formatter::RED);
176         pointer = formatter->color(Formatter::GREEN);
177         literal = formatter->color(Formatter::BLUE);
178     }
179
180     ~Dumper() {
181         delete normal;
182         delete bold;
183         delete italic;
184         delete red;
185         delete pointer;
186         delete literal;
187         delete formatter;
188     }
189
190     void visit(Null *) {
191         os << "NULL";
192     }
193
194     void visit(Bool *node) {
195         os << literal << (node->value ? "true" : "false") << normal;
196     }
197
198     void visit(SInt *node) {
199         os << literal << node->value << normal;
200     }
201
202     void visit(UInt *node) {
203         os << literal << node->value << normal;
204     }
205
206     void visit(Float *node) {
207         os << literal << node->value << normal;
208     }
209
210     void visit(String *node) {
211         os << literal << "\"";
212         for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) {
213             unsigned char c = (unsigned char) *it;
214             if (c == '\"')
215                 os << "\\\"";
216             else if (c == '\\')
217                 os << "\\\\";
218             else if (c >= 0x20 && c <= 0x7e)
219                 os << c;
220             else if (c == '\t') {
221                 os << "\t";
222             } else if (c == '\r') {
223                 // Ignore carriage-return
224             } else if (c == '\n') {
225                 // Reset formatting so that it looks correct with 'less -R'
226                 os << normal << '\n' << literal;
227             } else {
228                 unsigned octal0 = c & 0x7;
229                 unsigned octal1 = (c >> 3) & 0x7;
230                 unsigned octal2 = (c >> 3) & 0x7;
231                 os << "\\";
232                 if (octal2)
233                     os << octal2;
234                 if (octal1)
235                     os << octal1;
236                 os << octal0;
237             }
238         }
239         os << "\"" << normal;
240     }
241
242     void visit(Enum *node) {
243         os << literal << node->sig->first << normal;
244     }
245
246     void visit(Bitmask *bitmask) {
247         unsigned long long value = bitmask->value;
248         const Bitmask::Signature *sig = bitmask->sig;
249         bool first = true;
250         for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
251             if ((it->second && (value & it->second) == it->second) ||
252                 (!it->second && value == 0)) {
253                 if (!first) {
254                     os << " | ";
255                 }
256                 os << literal << it->first << normal;
257                 value &= ~it->second;
258                 first = false;
259             }
260         }
261         if (value || first) {
262             if (!first) {
263                 os << " | ";
264             }
265             os << literal << "0x" << std::hex << value << std::dec << normal;
266         }
267     }
268
269     void visit(Struct *s) {
270         const char *sep = "";
271         os << "{";
272         for (unsigned i = 0; i < s->members.size(); ++i) {
273             os << sep << italic << s->sig->member_names[i] << normal << " = ";
274             _visit(s->members[i]);
275             sep = ", ";
276         }
277         os << "}";
278     }
279
280     void visit(Array *array) {
281         if (array->values.size() == 1) {
282             os << "&";
283             _visit(array->values[0]);
284         }
285         else {
286             const char *sep = "";
287             os << "{";
288             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
289                 os << sep;
290                 _visit(*it);
291                 sep = ", ";
292             }
293             os << "}";
294         }
295     }
296
297     void visit(Blob *blob) {
298         os << pointer << "blob(" << blob->size << ")" << normal;
299     }
300
301     void visit(Pointer *p) {
302         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
303     }
304
305     void visit(Call *call) {
306         const char *sep = "";
307         os << bold << call->sig->name << normal << "(";
308         for (unsigned i = 0; i < call->args.size(); ++i) {
309             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
310             _visit(call->args[i]);
311             sep = ", ";
312         }
313         os << ")";
314         if (call->ret) {
315             os << " = ";
316             _visit(call->ret);
317         }
318         os << "\n";
319     }
320 };
321
322
323 std::ostream & operator <<(std::ostream &os, Value *value) {
324     Dumper d(os);
325     if (value) {
326         value->visit(d);
327     }
328     return os;
329 }
330
331
332 static inline const Value *unwrap(const Value *node) {
333     const Enum *c = dynamic_cast<const Enum *>(node);
334     if (c)
335         return c->sig->second;
336     return node;
337 }
338
339
340 static Null null;
341
342 const Value & Value::operator[](size_t index) const {
343     const Array *array = dynamic_cast<const Array *>(unwrap(this));
344     if (array) {
345         if (index < array->values.size()) {
346             return *array->values[index];
347         }
348     }
349     return null;
350 }
351
352 std::ostream & operator <<(std::ostream &os, Call &call) {
353     Dumper d(os);
354     os << call.no << " ";
355     d.visit(&call);
356     return os;
357 }
358
359
360 } /* namespace Trace */