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