]> git.cworth.org Git - apitrace/blob - common/trace_dump.cpp
Merge remote-tracking branch 'github/master' into d2d
[apitrace] / common / trace_dump.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 <limits>
28
29 #include "formatter.hpp"
30 #include "trace_dump.hpp"
31
32
33 namespace trace {
34
35
36 class Dumper : public Visitor
37 {
38 protected:
39     std::ostream &os;
40     DumpFlags dumpFlags;
41     formatter::Formatter *formatter;
42     formatter::Attribute *normal;
43     formatter::Attribute *bold;
44     formatter::Attribute *italic;
45     formatter::Attribute *strike;
46     formatter::Attribute *red;
47     formatter::Attribute *pointer;
48     formatter::Attribute *literal;
49
50 public:
51     Dumper(std::ostream &_os, DumpFlags _flags) : 
52         os(_os),
53         dumpFlags(_flags)
54     {
55         bool color = !(dumpFlags & DUMP_FLAG_NO_COLOR);
56         formatter = formatter::defaultFormatter(color);
57         normal = formatter->normal();
58         bold = formatter->bold();
59         italic = formatter->italic();
60         strike = formatter->strike();
61         red = formatter->color(formatter::RED);
62         pointer = formatter->color(formatter::GREEN);
63         literal = formatter->color(formatter::BLUE);
64     }
65
66     ~Dumper() {
67         delete normal;
68         delete bold;
69         delete italic;
70         delete strike;
71         delete red;
72         delete pointer;
73         delete literal;
74         delete formatter;
75     }
76
77     void visit(Null *) {
78         os << literal << "NULL" << normal;
79     }
80
81     void visit(Bool *node) {
82         os << literal << (node->value ? "true" : "false") << normal;
83     }
84
85     void visit(SInt *node) {
86         os << literal << node->value << normal;
87     }
88
89     void visit(UInt *node) {
90         os << literal << node->value << normal;
91     }
92
93     void visit(Float *node) {
94         std::streamsize oldPrecision = os.precision(std::numeric_limits<float>::digits10 + 1);
95         os << literal << node->value << normal;
96         os.precision(oldPrecision);
97     }
98
99     void visit(Double *node) {
100         std::streamsize oldPrecision = os.precision(std::numeric_limits<double>::digits10 + 1);
101         os << literal << node->value << normal;
102         os.precision(oldPrecision);
103     }
104
105     void visit(String *node) {
106         os << literal << "\"";
107         for (const char *it = node->value; *it; ++it) {
108             unsigned char c = (unsigned char) *it;
109             if (c == '\"')
110                 os << "\\\"";
111             else if (c == '\\')
112                 os << "\\\\";
113             else if (c >= 0x20 && c <= 0x7e)
114                 os << c;
115             else if (c == '\t') {
116                 os << "\t";
117             } else if (c == '\r') {
118                 // Ignore carriage-return
119             } else if (c == '\n') {
120                 // Reset formatting so that it looks correct with 'less -R'
121                 os << normal << '\n' << literal;
122             } else {
123                 unsigned octal0 = c & 0x7;
124                 unsigned octal1 = (c >> 3) & 0x7;
125                 unsigned octal2 = (c >> 3) & 0x7;
126                 os << "\\";
127                 if (octal2)
128                     os << octal2;
129                 if (octal1)
130                     os << octal1;
131                 os << octal0;
132             }
133         }
134         os << "\"" << normal;
135     }
136
137     void visit(Enum *node) {
138         const EnumValue *it = node->lookup();
139         if (it) {
140             os << literal << it->name << normal;
141             return;
142         }
143         os << literal << node->value << normal;
144     }
145
146     void visit(Bitmask *bitmask) {
147         unsigned long long value = bitmask->value;
148         const BitmaskSig *sig = bitmask->sig;
149         bool first = true;
150         for (const BitmaskFlag *it = sig->flags; it != sig->flags + sig->num_flags; ++it) {
151             assert(it->value || first);
152             if ((it->value && (value & it->value) == it->value) ||
153                 (!it->value && value == 0)) {
154                 if (!first) {
155                     os << " | ";
156                 }
157                 os << literal << it->name << normal;
158                 value &= ~it->value;
159                 first = false;
160             }
161             if (value == 0) {
162                 break;
163             }
164         }
165         if (value || first) {
166             if (!first) {
167                 os << " | ";
168             }
169             os << literal << "0x" << std::hex << value << std::dec << normal;
170         }
171     }
172
173     void visit(Struct *s) {
174         const char *sep = "";
175         os << "{";
176         for (unsigned i = 0; i < s->members.size(); ++i) {
177             os << sep << italic << s->sig->member_names[i] << normal << " = ";
178             _visit(s->members[i]);
179             sep = ", ";
180         }
181         os << "}";
182     }
183
184     void visit(Array *array) {
185         if (array->values.size() == 1) {
186             os << "&";
187             _visit(array->values[0]);
188         }
189         else {
190             const char *sep = "";
191             os << "{";
192             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
193                 os << sep;
194                 _visit(*it);
195                 sep = ", ";
196             }
197             os << "}";
198         }
199     }
200
201     void visit(Blob *blob) {
202         os << pointer << "blob(" << blob->size << ")" << normal;
203     }
204
205     void visit(Pointer *p) {
206         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
207     }
208
209     void visit(Repr *r) {
210         _visit(r->humanValue);
211     }
212
213     void visit(Call *call) {
214         CallFlags callFlags = call->flags;
215         
216         if (!(dumpFlags & DUMP_FLAG_NO_CALL_NO)) {
217             os << call->no << " ";
218         }
219
220         if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
221             os << strike;
222         } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
223             os << normal;
224         } else {
225             os << bold;
226         }
227         os << call->sig->name << normal;
228
229         os << "(";
230         const char *sep = "";
231         for (unsigned i = 0; i < call->args.size(); ++i) {
232             os << sep;
233             if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
234                 os << italic << call->sig->arg_names[i] << normal << " = ";
235             }
236             if (call->args[i].value) {
237                 _visit(call->args[i].value);
238             } else {
239                os << "?";
240             }
241             sep = ", ";
242         }
243         os << ")";
244
245         if (call->ret) {
246             os << " = ";
247             _visit(call->ret);
248         }
249         
250         if (callFlags & CALL_FLAG_INCOMPLETE) {
251             os << " // " << red << "incomplete" << normal;
252         }
253         
254         os << "\n";
255
256         if (callFlags & CALL_FLAG_END_FRAME) {
257             os << "\n";
258         }
259     }
260 };
261
262
263 void dump(Value *value, std::ostream &os, DumpFlags flags) {
264     Dumper d(os, flags);
265     value->visit(d);
266 }
267
268
269 void dump(Call &call, std::ostream &os, DumpFlags flags) {
270     Dumper d(os, flags);
271     d.visit(&call);
272 }
273
274
275 } /* namespace trace */