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