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