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