]> git.cworth.org Git - apitrace/blob - common/trace_model.cpp
2ceb81074a846ce494951b10918fb14f840aa034
[apitrace] / common / 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 String::~String() {
46     delete [] value;
47 }
48
49
50 Struct::~Struct() {
51     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
52         delete *it;
53     }
54 }
55
56
57 Array::~Array() {
58     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
59         delete *it;
60     }
61 }
62
63 Blob::~Blob() {
64     // Blobs are often bound and referred during many calls, so we can't delete
65     // them here in that case.
66     //
67     // Once bound there is no way to know when they were unbound, which
68     // effectively means we have to leak them.  A better solution would be to
69     // keep a list of bound pointers, and defer the destruction to when the
70     // trace in question has been fully processed.
71     if (!bound) {
72         delete [] buf;
73     }
74 }
75
76
77 // bool cast
78 bool Null   ::toBool(void) const { return false; }
79 bool Bool   ::toBool(void) const { return value; }
80 bool SInt   ::toBool(void) const { return value != 0; }
81 bool UInt   ::toBool(void) const { return value != 0; }
82 bool Float  ::toBool(void) const { return value != 0; }
83 bool Double ::toBool(void) const { return value != 0; }
84 bool String ::toBool(void) const { return true; }
85 bool Struct ::toBool(void) const { return true; }
86 bool Array  ::toBool(void) const { return true; }
87 bool Blob   ::toBool(void) const { return true; }
88 bool Pointer::toBool(void) const { return value != 0; }
89
90
91 // signed integer cast
92 signed long long Value  ::toSInt(void) const { assert(0); return 0; }
93 signed long long Null   ::toSInt(void) const { return 0; }
94 signed long long Bool   ::toSInt(void) const { return static_cast<signed long long>(value); }
95 signed long long SInt   ::toSInt(void) const { return value; }
96 signed long long UInt   ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
97 signed long long Float  ::toSInt(void) const { return static_cast<signed long long>(value); }
98 signed long long Double ::toSInt(void) const { return static_cast<signed long long>(value); }
99
100
101 // unsigned integer cast
102 unsigned long long Value  ::toUInt(void) const { assert(0); return 0; }
103 unsigned long long Null   ::toUInt(void) const { return 0; }
104 unsigned long long Bool   ::toUInt(void) const { return static_cast<unsigned long long>(value); }
105 unsigned long long SInt   ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
106 unsigned long long UInt   ::toUInt(void) const { return value; }
107 unsigned long long Float  ::toUInt(void) const { return static_cast<unsigned long long>(value); }
108 unsigned long long Double ::toUInt(void) const { return static_cast<unsigned long long>(value); }
109
110
111 // floating point cast
112 float Value  ::toFloat(void) const { assert(0); return 0; }
113 float Null   ::toFloat(void) const { return 0; }
114 float Bool   ::toFloat(void) const { return static_cast<float>(value); }
115 float SInt   ::toFloat(void) const { return static_cast<float>(value); }
116 float UInt   ::toFloat(void) const { return static_cast<float>(value); }
117 float Float  ::toFloat(void) const { return value; }
118 float Double ::toFloat(void) const { return value; }
119
120
121 // floating point cast
122 double Value  ::toDouble(void) const { assert(0); return 0; }
123 double Null   ::toDouble(void) const { return 0; }
124 double Bool   ::toDouble(void) const { return static_cast<double>(value); }
125 double SInt   ::toDouble(void) const { return static_cast<double>(value); }
126 double UInt   ::toDouble(void) const { return static_cast<double>(value); }
127 double Float  ::toDouble(void) const { return value; }
128 double Double ::toDouble(void) const { return value; }
129
130
131 // pointer cast
132 void * Value  ::toPointer(void) const { assert(0); return NULL; }
133 void * Null   ::toPointer(void) const { return NULL; }
134 void * Blob   ::toPointer(void) const { return buf; }
135 void * Pointer::toPointer(void) const { return (void *)value; }
136
137 void * Value  ::toPointer(bool bind) { assert(0); return NULL; }
138 void * Null   ::toPointer(bool bind) { return NULL; }
139 void * Blob   ::toPointer(bool bind) { if (bind) bound = true; return buf; }
140 void * Pointer::toPointer(bool bind) { return (void *)value; }
141
142
143 // pointer cast
144 unsigned long long Value  ::toUIntPtr(void) const { assert(0); return 0; }
145 unsigned long long Null   ::toUIntPtr(void) const { return 0; }
146 unsigned long long Pointer::toUIntPtr(void) const { return value; }
147
148
149 // string cast
150 const char * Value ::toString(void) const { assert(0); return NULL; }
151 const char * Null  ::toString(void) const { return NULL; }
152 const char * String::toString(void) const { return value; }
153
154
155 // virtual Value::visit()
156 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
157 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
158 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
159 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
160 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
161 void Double ::visit(Visitor &visitor) { visitor.visit(this); }
162 void String ::visit(Visitor &visitor) { visitor.visit(this); }
163 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
164 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
165 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
166 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
167 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
168 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
169
170
171 void Visitor::visit(Null *) { assert(0); }
172 void Visitor::visit(Bool *) { assert(0); }
173 void Visitor::visit(SInt *) { assert(0); }
174 void Visitor::visit(UInt *) { assert(0); }
175 void Visitor::visit(Float *) { assert(0); }
176 void Visitor::visit(Double *) { assert(0); }
177 void Visitor::visit(String *) { assert(0); }
178 void Visitor::visit(Enum *node) { assert(0); }
179 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
180 void Visitor::visit(Struct *) { assert(0); }
181 void Visitor::visit(Array *) { assert(0); }
182 void Visitor::visit(Blob *) { assert(0); }
183 void Visitor::visit(Pointer *) { assert(0); }
184
185
186 class Dumper : public Visitor
187 {
188 protected:
189     std::ostream &os;
190     formatter::Formatter *formatter;
191     formatter::Attribute *normal;
192     formatter::Attribute *bold;
193     formatter::Attribute *italic;
194     formatter::Attribute *strike;
195     formatter::Attribute *red;
196     formatter::Attribute *pointer;
197     formatter::Attribute *literal;
198
199 public:
200     Dumper(std::ostream &_os, bool color) : os(_os) {
201         formatter = formatter::defaultFormatter(color);
202         normal = formatter->normal();
203         bold = formatter->bold();
204         italic = formatter->italic();
205         strike = formatter->strike();
206         red = formatter->color(formatter::RED);
207         pointer = formatter->color(formatter::GREEN);
208         literal = formatter->color(formatter::BLUE);
209     }
210
211     ~Dumper() {
212         delete normal;
213         delete bold;
214         delete italic;
215         delete strike;
216         delete red;
217         delete pointer;
218         delete literal;
219         delete formatter;
220     }
221
222     void visit(Null *) {
223         os << "NULL";
224     }
225
226     void visit(Bool *node) {
227         os << literal << (node->value ? "true" : "false") << normal;
228     }
229
230     void visit(SInt *node) {
231         os << literal << node->value << normal;
232     }
233
234     void visit(UInt *node) {
235         os << literal << node->value << normal;
236     }
237
238     void visit(Float *node) {
239         os << literal << node->value << normal;
240     }
241
242     void visit(Double *node) {
243         os << literal << node->value << normal;
244     }
245
246     void visit(String *node) {
247         os << literal << "\"";
248         for (const char *it = node->value; *it; ++it) {
249             unsigned char c = (unsigned char) *it;
250             if (c == '\"')
251                 os << "\\\"";
252             else if (c == '\\')
253                 os << "\\\\";
254             else if (c >= 0x20 && c <= 0x7e)
255                 os << c;
256             else if (c == '\t') {
257                 os << "\t";
258             } else if (c == '\r') {
259                 // Ignore carriage-return
260             } else if (c == '\n') {
261                 // Reset formatting so that it looks correct with 'less -R'
262                 os << normal << '\n' << literal;
263             } else {
264                 unsigned octal0 = c & 0x7;
265                 unsigned octal1 = (c >> 3) & 0x7;
266                 unsigned octal2 = (c >> 3) & 0x7;
267                 os << "\\";
268                 if (octal2)
269                     os << octal2;
270                 if (octal1)
271                     os << octal1;
272                 os << octal0;
273             }
274         }
275         os << "\"" << normal;
276     }
277
278     void visit(Enum *node) {
279         const EnumSig *sig = node->sig;
280         for (const EnumValue *it = sig->values; it != sig->values + sig->num_values; ++it) {
281             if (it->value == node->value) {
282                 os << literal << it->name << normal;
283                 return;
284             }
285         }
286         os << literal << node->value << normal;
287     }
288
289     void visit(Bitmask *bitmask) {
290         unsigned long long value = bitmask->value;
291         const BitmaskSig *sig = bitmask->sig;
292         bool first = true;
293         for (const BitmaskFlag *it = sig->flags; value != 0 && it != sig->flags + sig->num_flags; ++it) {
294             if ((it->value && (value & it->value) == it->value) ||
295                 (!it->value && value == 0)) {
296                 if (!first) {
297                     os << " | ";
298                 }
299                 os << literal << it->name << normal;
300                 value &= ~it->value;
301                 first = false;
302             }
303         }
304         if (value || first) {
305             if (!first) {
306                 os << " | ";
307             }
308             os << literal << "0x" << std::hex << value << std::dec << normal;
309         }
310     }
311
312     void visit(Struct *s) {
313         const char *sep = "";
314         os << "{";
315         for (unsigned i = 0; i < s->members.size(); ++i) {
316             os << sep << italic << s->sig->member_names[i] << normal << " = ";
317             _visit(s->members[i]);
318             sep = ", ";
319         }
320         os << "}";
321     }
322
323     void visit(Array *array) {
324         if (array->values.size() == 1) {
325             os << "&";
326             _visit(array->values[0]);
327         }
328         else {
329             const char *sep = "";
330             os << "{";
331             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
332                 os << sep;
333                 _visit(*it);
334                 sep = ", ";
335             }
336             os << "}";
337         }
338     }
339
340     void visit(Blob *blob) {
341         os << pointer << "blob(" << blob->size << ")" << normal;
342     }
343
344     void visit(Pointer *p) {
345         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
346     }
347
348     void visit(Call *call) {
349         CallFlags flags = call->flags;
350
351         if (flags & CALL_FLAG_NON_REPRODUCIBLE) {
352             os << strike;
353         } else if (flags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
354             os << normal;
355         } else {
356             os << bold;
357         }
358         os << call->sig->name << normal;
359
360         os << "(";
361         const char *sep = "";
362         for (unsigned i = 0; i < call->args.size(); ++i) {
363             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
364             if (call->args[i]) {
365                 _visit(call->args[i]);
366             } else {
367                os << "?";
368             }
369             sep = ", ";
370         }
371         os << ")";
372
373         if (call->ret) {
374             os << " = ";
375             _visit(call->ret);
376         }
377         
378         if (flags & CALL_FLAG_INCOMPLETE) {
379             os << " // " << red << "incomplete" << normal;
380         }
381         
382         os << "\n";
383
384         if (flags & CALL_FLAG_END_FRAME) {
385             os << "\n";
386         }
387     }
388 };
389
390
391 void Value::dump(std::ostream &os, bool color) {
392     Dumper d(os, color);
393     visit(d);
394 }
395
396
397 static Null null;
398
399 const Value & Value::operator[](size_t index) const {
400     const Array *array = dynamic_cast<const Array *>(this);
401     if (array) {
402         if (index < array->values.size()) {
403             return *array->values[index];
404         }
405     }
406     return null;
407 }
408
409 void Call::dump(std::ostream &os, bool color) {
410     Dumper d(os, color);
411     os << no << " ";
412     d.visit(this);
413 }
414
415
416 } /* namespace trace */