]> git.cworth.org Git - apitrace/blob - common/trace_dump.cpp
Add simple CPU profiling support to glretrace.
[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 EnumSig *sig = node->sig;
133         for (const EnumValue *it = sig->values; it != sig->values + sig->num_values; ++it) {
134             if (it->value == node->value) {
135                 os << literal << it->name << normal;
136                 return;
137             }
138         }
139         os << literal << node->value << normal;
140     }
141
142     void visit(Bitmask *bitmask) {
143         unsigned long long value = bitmask->value;
144         const BitmaskSig *sig = bitmask->sig;
145         bool first = true;
146         for (const BitmaskFlag *it = sig->flags; value != 0 && it != sig->flags + sig->num_flags; ++it) {
147             if ((it->value && (value & it->value) == it->value) ||
148                 (!it->value && value == 0)) {
149                 if (!first) {
150                     os << " | ";
151                 }
152                 os << literal << it->name << normal;
153                 value &= ~it->value;
154                 first = false;
155             }
156         }
157         if (value || first) {
158             if (!first) {
159                 os << " | ";
160             }
161             os << literal << "0x" << std::hex << value << std::dec << normal;
162         }
163     }
164
165     void visit(Struct *s) {
166         const char *sep = "";
167         os << "{";
168         for (unsigned i = 0; i < s->members.size(); ++i) {
169             os << sep << italic << s->sig->member_names[i] << normal << " = ";
170             _visit(s->members[i]);
171             sep = ", ";
172         }
173         os << "}";
174     }
175
176     void visit(Array *array) {
177         if (array->values.size() == 1) {
178             os << "&";
179             _visit(array->values[0]);
180         }
181         else {
182             const char *sep = "";
183             os << "{";
184             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
185                 os << sep;
186                 _visit(*it);
187                 sep = ", ";
188             }
189             os << "}";
190         }
191     }
192
193     void visit(Blob *blob) {
194         os << pointer << "blob(" << blob->size << ")" << normal;
195     }
196
197     void visit(Pointer *p) {
198         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
199     }
200
201     void visit(Call *call) {
202         CallFlags callFlags = call->flags;
203         
204         if (!(dumpFlags & DUMP_FLAG_NO_CALL_NO)) {
205             os << call->no << " ";
206         }
207
208         if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
209             os << strike;
210         } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
211             os << normal;
212         } else {
213             os << bold;
214         }
215         os << call->sig->name << normal;
216
217         os << "(";
218         const char *sep = "";
219         for (unsigned i = 0; i < call->args.size(); ++i) {
220             os << sep;
221             if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
222                 os << italic << call->sig->arg_names[i] << normal << " = ";
223             }
224             if (call->args[i].value) {
225                 _visit(call->args[i].value);
226             } else {
227                os << "?";
228             }
229             sep = ", ";
230         }
231         os << ")";
232
233         if (call->ret) {
234             os << " = ";
235             _visit(call->ret);
236         }
237         
238         if (callFlags & CALL_FLAG_INCOMPLETE) {
239             os << " // " << red << "incomplete" << normal;
240         }
241         
242         os << "\n";
243
244         if (callFlags & CALL_FLAG_END_FRAME) {
245             os << "\n";
246         }
247     }
248 };
249
250
251 void dump(Value *value, std::ostream &os, DumpFlags flags) {
252     Dumper d(os, flags);
253     value->visit(d);
254 }
255
256
257 void dump(Call &call, std::ostream &os, DumpFlags flags) {
258     Dumper d(os, flags);
259     d.visit(&call);
260 }
261
262
263 } /* namespace trace */