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