Skip to content
Newer
Older
100644 276 lines (238 sloc) 7.83 KB
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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
5f2245e @jrfonseca Use appropriate number of digits when dumping floating point numbers.
jrfonseca authored May 14, 2012
27 #include <limits>
28
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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;
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
40 DumpFlags dumpFlags;
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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),
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
53 dumpFlags(_flags)
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
54 {
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
55 bool color = !(dumpFlags & DUMP_FLAG_NO_COLOR);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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 *) {
2fadd9f @jrfonseca Highlight NULL as a literal.
jrfonseca authored May 2, 2012
78 os << literal << "NULL" << normal;
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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) {
5f2245e @jrfonseca Use appropriate number of digits when dumping floating point numbers.
jrfonseca authored May 14, 2012
94 std::streamsize oldPrecision = os.precision(std::numeric_limits<float>::digits10 + 1);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
95 os << literal << node->value << normal;
5f2245e @jrfonseca Use appropriate number of digits when dumping floating point numbers.
jrfonseca authored May 14, 2012
96 os.precision(oldPrecision);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
97 }
98
99 void visit(Double *node) {
5f2245e @jrfonseca Use appropriate number of digits when dumping floating point numbers.
jrfonseca authored May 14, 2012
100 std::streamsize oldPrecision = os.precision(std::numeric_limits<double>::digits10 + 1);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
101 os << literal << node->value << normal;
5f2245e @jrfonseca Use appropriate number of digits when dumping floating point numbers.
jrfonseca authored May 14, 2012
102 os.precision(oldPrecision);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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) {
b16a4a8 @jrfonseca Diff with symbolic names.
jrfonseca authored Mar 16, 2012
138 const EnumValue *it = node->lookup();
139 if (it) {
140 os << literal << it->name << normal;
141 return;
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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;
b16a4a8 @jrfonseca Diff with symbolic names.
jrfonseca authored Mar 16, 2012
150 for (const BitmaskFlag *it = sig->flags; it != sig->flags + sig->num_flags; ++it) {
fc9939f @jrfonseca Handle zero valued bitmask flags.
jrfonseca authored Apr 23, 2012
151 assert(it->value || first);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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 }
b16a4a8 @jrfonseca Diff with symbolic names.
jrfonseca authored Mar 16, 2012
161 if (value == 0) {
162 break;
163 }
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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
112a132 @jrfonseca Preserve both D3D9 shader byte code, and disassembly.
jrfonseca authored Apr 27, 2012
209 void visit(Repr *r) {
210 _visit(r->humanValue);
211 }
212
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
213 void visit(Call *call) {
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
214 CallFlags callFlags = call->flags;
2dad4b4 @rcgordon Add simple CPU profiling support to glretrace.
rcgordon authored Jan 8, 2012
215
216 if (!(dumpFlags & DUMP_FLAG_NO_CALL_NO)) {
217 os << call->no << " ";
218 }
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
219
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
220 if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
221 os << strike;
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
222 } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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;
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
233 if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
234 os << italic << call->sig->arg_names[i] << normal << " = ";
235 }
a5b476e @cworth-gh Correctly copy "out" arguments to the "leave" portion of the trace
cworth-gh authored Nov 16, 2011
236 if (call->args[i].value) {
237 _visit(call->args[i].value);
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
238 } else {
239 os << "?";
240 }
241 sep = ", ";
242 }
243 os << ")";
244
245 if (call->ret) {
246 os << " = ";
247 _visit(call->ret);
248 }
249
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
250 if (callFlags & CALL_FLAG_INCOMPLETE) {
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
251 os << " // " << red << "incomplete" << normal;
252 }
253
254 os << "\n";
255
e84e3b8 @jrfonseca Fix name clash & typo on dump flags.
jrfonseca authored Dec 12, 2011
256 if (callFlags & CALL_FLAG_END_FRAME) {
946da14 @jrfonseca Move trace dumping to a separate module. Add option to not dump arg n…
jrfonseca authored Dec 11, 2011
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 */
Something went wrong with that request. Please try again.