1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
29 #include "formatter.hpp"
30 #include "trace_dump.hpp"
36 class Dumper : public Visitor
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;
51 Dumper(std::ostream &_os, DumpFlags _flags) :
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);
78 os << literal << "NULL" << normal;
81 void visit(Bool *node) {
82 os << literal << (node->value ? "true" : "false") << normal;
85 void visit(SInt *node) {
86 os << literal << node->value << normal;
89 void visit(UInt *node) {
90 os << literal << node->value << normal;
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);
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);
105 void visit(String *node) {
106 os << literal << "\"";
107 for (const char *it = node->value; *it; ++it) {
108 unsigned char c = (unsigned char) *it;
113 else if (c >= 0x20 && c <= 0x7e)
115 else if (c == '\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;
123 unsigned octal0 = c & 0x7;
124 unsigned octal1 = (c >> 3) & 0x7;
125 unsigned octal2 = (c >> 3) & 0x7;
134 os << "\"" << normal;
137 void visit(Enum *node) {
138 const EnumValue *it = node->lookup();
140 os << literal << it->name << normal;
143 os << literal << node->value << normal;
146 void visit(Bitmask *bitmask) {
147 unsigned long long value = bitmask->value;
148 const BitmaskSig *sig = bitmask->sig;
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)) {
157 os << literal << it->name << normal;
165 if (value || first) {
169 os << literal << "0x" << std::hex << value << std::dec << normal;
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];
179 if (!memberName || !*memberName) {
180 // Anonymous structure
181 Struct *memberStruct = dynamic_cast<Struct *>(memberValue);
182 assert(memberStruct);
184 sep = visitMembers(memberStruct, sep);
189 os << sep << italic << memberName << normal << " = ",
196 void visit(Struct *s) {
202 void visit(Array *array) {
203 if (array->values.size() == 1) {
205 _visit(array->values[0]);
208 const char *sep = "";
210 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
219 void visit(Blob *blob) {
220 os << pointer << "blob(" << blob->size << ")" << normal;
223 void visit(Pointer *p) {
224 os << pointer << "0x" << std::hex << p->value << std::dec << normal;
227 void visit(Repr *r) {
228 _visit(r->humanValue);
231 void visit(Call *call) {
232 CallFlags callFlags = call->flags;
234 if (!(dumpFlags & DUMP_FLAG_NO_CALL_NO)) {
235 os << call->no << " ";
238 if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
240 } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
245 os << call->sig->name << normal;
248 const char *sep = "";
249 for (unsigned i = 0; i < call->args.size(); ++i) {
251 if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
252 os << italic << call->sig->arg_names[i] << normal << " = ";
254 if (call->args[i].value) {
255 _visit(call->args[i].value);
268 if (callFlags & CALL_FLAG_INCOMPLETE) {
269 os << " // " << red << "incomplete" << normal;
274 if (callFlags & CALL_FLAG_END_FRAME) {
281 void dump(Value *value, std::ostream &os, DumpFlags flags) {
287 void dump(Call &call, std::ostream &os, DumpFlags flags) {
293 } /* namespace trace */