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