]> git.cworth.org Git - apitrace/blob - common/trace_dump.cpp
Use skiplist-based FastCallSet within trace::CallSet
[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     const char *
174     visitMembers(Struct *s, const char *sep = "") {
175         for (unsigned i = 0; i < s->members.size(); ++i) {
176             const char *memberName = s->sig->member_names[i];
177             Value *memberValue = s->members[i];
178
179             if (!memberName || !*memberName) {
180                 // Anonymous structure
181                 Struct *memberStruct = dynamic_cast<Struct *>(memberValue);
182                 assert(memberStruct);
183                 if (memberStruct) {
184                     sep = visitMembers(memberStruct, sep);
185                     continue;
186                 }
187             }
188
189             os << sep << italic << memberName << normal << " = ",
190             _visit(memberValue);
191             sep = ", ";
192         }
193         return sep;
194     }
195
196     void visit(Struct *s) {
197         os << "{";
198         visitMembers(s);
199         os << "}";
200     }
201
202     void visit(Array *array) {
203         if (array->values.size() == 1) {
204             os << "&";
205             _visit(array->values[0]);
206         }
207         else {
208             const char *sep = "";
209             os << "{";
210             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
211                 os << sep;
212                 _visit(*it);
213                 sep = ", ";
214             }
215             os << "}";
216         }
217     }
218
219     void visit(Blob *blob) {
220         os << pointer << "blob(" << blob->size << ")" << normal;
221     }
222
223     void visit(Pointer *p) {
224         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
225     }
226
227     void visit(Repr *r) {
228         _visit(r->humanValue);
229     }
230
231     void visit(Call *call) {
232         CallFlags callFlags = call->flags;
233         
234         if (!(dumpFlags & DUMP_FLAG_NO_CALL_NO)) {
235             os << call->no << " ";
236         }
237
238         if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
239             os << strike;
240         } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
241             os << normal;
242         } else {
243             os << bold;
244         }
245         os << call->sig->name << normal;
246
247         os << "(";
248         const char *sep = "";
249         for (unsigned i = 0; i < call->args.size(); ++i) {
250             os << sep;
251             if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
252                 os << italic << call->sig->arg_names[i] << normal << " = ";
253             }
254             if (call->args[i].value) {
255                 _visit(call->args[i].value);
256             } else {
257                os << "?";
258             }
259             sep = ", ";
260         }
261         os << ")";
262
263         if (call->ret) {
264             os << " = ";
265             _visit(call->ret);
266         }
267         
268         if (callFlags & CALL_FLAG_INCOMPLETE) {
269             os << " // " << red << "incomplete" << normal;
270         }
271         
272         os << "\n";
273
274         if (callFlags & CALL_FLAG_END_FRAME) {
275             os << "\n";
276         }
277     }
278 };
279
280
281 void dump(Value *value, std::ostream &os, DumpFlags flags) {
282     Dumper d(os, flags);
283     value->visit(d);
284 }
285
286
287 void dump(Call &call, std::ostream &os, DumpFlags flags) {
288     Dumper d(os, flags);
289     d.visit(&call);
290 }
291
292
293 } /* namespace trace */