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 **************************************************************************/
27 #include "formatter.hpp"
28 #include "trace_dump.hpp"
34 class Dumper : public Visitor
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;
49 Dumper(std::ostream &_os, DumpFlags _flags) :
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);
79 void visit(Bool *node) {
80 os << literal << (node->value ? "true" : "false") << normal;
83 void visit(SInt *node) {
84 os << literal << node->value << normal;
87 void visit(UInt *node) {
88 os << literal << node->value << normal;
91 void visit(Float *node) {
92 os << literal << node->value << normal;
95 void visit(Double *node) {
96 os << literal << node->value << normal;
99 void visit(String *node) {
100 os << literal << "\"";
101 for (const char *it = node->value; *it; ++it) {
102 unsigned char c = (unsigned char) *it;
107 else if (c >= 0x20 && c <= 0x7e)
109 else if (c == '\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;
117 unsigned octal0 = c & 0x7;
118 unsigned octal1 = (c >> 3) & 0x7;
119 unsigned octal2 = (c >> 3) & 0x7;
128 os << "\"" << normal;
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;
139 os << literal << node->value << normal;
142 void visit(Bitmask *bitmask) {
143 unsigned long long value = bitmask->value;
144 const BitmaskSig *sig = bitmask->sig;
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)) {
152 os << literal << it->name << normal;
157 if (value || first) {
161 os << literal << "0x" << std::hex << value << std::dec << normal;
165 void visit(Struct *s) {
166 const char *sep = "";
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]);
176 void visit(Array *array) {
177 if (array->values.size() == 1) {
179 _visit(array->values[0]);
182 const char *sep = "";
184 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
193 void visit(Blob *blob) {
194 os << pointer << "blob(" << blob->size << ")" << normal;
197 void visit(Pointer *p) {
198 os << pointer << "0x" << std::hex << p->value << std::dec << normal;
201 void visit(Call *call) {
202 CallFlags callFlags = call->flags;
204 if (callFlags & CALL_FLAG_NON_REPRODUCIBLE) {
206 } else if (callFlags & (CALL_FLAG_FAKE | CALL_FLAG_NO_SIDE_EFFECTS)) {
211 os << call->sig->name << normal;
214 const char *sep = "";
215 for (unsigned i = 0; i < call->args.size(); ++i) {
217 if (!(dumpFlags & DUMP_FLAG_NO_ARG_NAMES)) {
218 os << italic << call->sig->arg_names[i] << normal << " = ";
221 _visit(call->args[i]);
234 if (callFlags & CALL_FLAG_INCOMPLETE) {
235 os << " // " << red << "incomplete" << normal;
240 if (callFlags & CALL_FLAG_END_FRAME) {
247 void dump(Value *value, std::ostream &os, DumpFlags flags) {
253 void dump(Call &call, std::ostream &os, DumpFlags flags) {
255 os << call.no << " ";
260 } /* namespace trace */