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_model.hpp"
35 for (unsigned i = 0; i < args.size(); ++i) {
46 for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
53 for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
59 // TODO: Don't leak blobs. Blobs are often bound and accessed during many
60 // calls, so we can't delete them here.
66 Null ::operator bool(void) const { return false; }
67 Bool ::operator bool(void) const { return value; }
68 SInt ::operator bool(void) const { return value != 0; }
69 UInt ::operator bool(void) const { return value != 0; }
70 Float ::operator bool(void) const { return value != 0; }
71 String ::operator bool(void) const { return true; }
72 Enum ::operator bool(void) const { return static_cast<bool>(*sig->second); }
73 Struct ::operator bool(void) const { return true; }
74 Array ::operator bool(void) const { return true; }
75 Blob ::operator bool(void) const { return true; }
76 Pointer::operator bool(void) const { return value != 0; }
79 // signed integer cast
80 Value ::operator signed long long (void) const { assert(0); return 0; }
81 Null ::operator signed long long (void) const { return 0; }
82 Bool ::operator signed long long (void) const { return static_cast<signed long long>(value); }
83 SInt ::operator signed long long (void) const { return value; }
84 UInt ::operator signed long long (void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
85 Float ::operator signed long long (void) const { return static_cast<signed long long>(value); }
86 Enum ::operator signed long long (void) const { return static_cast<signed long long>(*sig->second); }
89 // unsigned integer cast
90 Value ::operator unsigned long long (void) const { assert(0); return 0; }
91 Null ::operator unsigned long long (void) const { return 0; }
92 Bool ::operator unsigned long long (void) const { return static_cast<unsigned long long>(value); }
93 SInt ::operator unsigned long long (void) const { assert(value >= 0); return static_cast<signed long long>(value); }
94 UInt ::operator unsigned long long (void) const { return value; }
95 Float ::operator unsigned long long (void) const { return static_cast<unsigned long long>(value); }
96 Enum ::operator unsigned long long (void) const { return static_cast<unsigned long long>(*sig->second); }
99 // floating point cast
100 Value ::operator double (void) const { assert(0); return 0; }
101 Null ::operator double (void) const { return 0; }
102 Bool ::operator double (void) const { return static_cast<double>(value); }
103 SInt ::operator double (void) const { return static_cast<double>(value); }
104 UInt ::operator double (void) const { return static_cast<double>(value); }
105 Float ::operator double (void) const { return value; }
106 Enum ::operator double (void) const { return static_cast<unsigned long long>(*sig->second); }
110 void * Value ::blob(void) const { assert(0); return NULL; }
111 void * Null ::blob(void) const { return NULL; }
112 void * Blob ::blob(void) const { return buf; }
113 void * Pointer::blob(void) const { assert(value < 0x100000ULL); return (void *)value; }
116 // virtual Value::visit()
117 void Null ::visit(Visitor &visitor) { visitor.visit(this); }
118 void Bool ::visit(Visitor &visitor) { visitor.visit(this); }
119 void SInt ::visit(Visitor &visitor) { visitor.visit(this); }
120 void UInt ::visit(Visitor &visitor) { visitor.visit(this); }
121 void Float ::visit(Visitor &visitor) { visitor.visit(this); }
122 void String ::visit(Visitor &visitor) { visitor.visit(this); }
123 void Enum ::visit(Visitor &visitor) { visitor.visit(this); }
124 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
125 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
126 void Array ::visit(Visitor &visitor) { visitor.visit(this); }
127 void Blob ::visit(Visitor &visitor) { visitor.visit(this); }
128 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
131 void Visitor::visit(Null *) { assert(0); }
132 void Visitor::visit(Bool *) { assert(0); }
133 void Visitor::visit(SInt *) { assert(0); }
134 void Visitor::visit(UInt *) { assert(0); }
135 void Visitor::visit(Float *) { assert(0); }
136 void Visitor::visit(String *) { assert(0); }
137 void Visitor::visit(Enum *node) { _visit(node->sig->second); }
138 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
139 void Visitor::visit(Struct *) { assert(0); }
140 void Visitor::visit(Array *) { assert(0); }
141 void Visitor::visit(Blob *) { assert(0); }
142 void Visitor::visit(Pointer *) { assert(0); }
145 class Dumper : public Visitor
149 Formatter::Formatter *formatter;
150 Formatter::Attribute *normal;
151 Formatter::Attribute *bold;
152 Formatter::Attribute *italic;
153 Formatter::Attribute *red;
154 Formatter::Attribute *pointer;
155 Formatter::Attribute *literal;
158 Dumper(std::ostream &_os) : os(_os) {
159 formatter = Formatter::defaultFormatter();
160 normal = formatter->normal();
161 bold = formatter->bold();
162 italic = formatter->italic();
163 red = formatter->color(Formatter::RED);
164 pointer = formatter->color(Formatter::GREEN);
165 literal = formatter->color(Formatter::BLUE);
182 void visit(Bool *node) {
183 os << literal << (node->value ? "true" : "false") << normal;
186 void visit(SInt *node) {
187 os << literal << node->value << normal;
190 void visit(UInt *node) {
191 os << literal << node->value << normal;
194 void visit(Float *node) {
195 os << literal << node->value << normal;
198 void visit(String *node) {
199 os << literal << "\"";
200 for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) {
201 unsigned char c = (unsigned char) *it;
206 else if (c >= 0x20 && c <= 0x7e)
208 else if (c == '\t') {
210 } else if (c == '\r') {
211 // Ignore carriage-return
212 } else if (c == '\n') {
213 // Reset formatting so that it looks correct with 'less -R'
214 os << normal << '\n' << literal;
216 unsigned octal0 = c & 0x7;
217 unsigned octal1 = (c >> 3) & 0x7;
218 unsigned octal2 = (c >> 3) & 0x7;
227 os << "\"" << normal;
230 void visit(Enum *node) {
231 os << literal << node->sig->first << normal;
234 void visit(Bitmask *bitmask) {
235 unsigned long long value = bitmask->value;
236 const Bitmask::Signature *sig = bitmask->sig;
238 for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
239 if ((it->second && (value & it->second) == it->second) ||
240 (!it->second && value == 0)) {
244 os << literal << it->first << normal;
245 value &= ~it->second;
249 if (value || first) {
253 os << literal << "0x" << std::hex << value << std::dec << normal;
257 void visit(Struct *s) {
258 const char *sep = "";
260 for (unsigned i = 0; i < s->members.size(); ++i) {
261 os << sep << italic << s->sig->member_names[i] << normal << " = ";
262 _visit(s->members[i]);
268 void visit(Array *array) {
269 if (array->values.size() == 1) {
271 _visit(array->values[0]);
274 const char *sep = "";
276 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
285 void visit(Blob *blob) {
286 os << pointer << "blob(" << blob->size << ")" << normal;
289 void visit(Pointer *p) {
290 os << pointer << "0x" << std::hex << p->value << std::dec << normal;
293 void visit(Call *call) {
294 const char *sep = "";
295 os << bold << call->sig->name << normal << "(";
296 for (unsigned i = 0; i < call->args.size(); ++i) {
297 os << sep << italic << call->sig->arg_names[i] << normal << " = ";
298 _visit(call->args[i]);
311 std::ostream & operator <<(std::ostream &os, Value *value) {
320 static inline const Value *unwrap(const Value *node) {
321 const Enum *c = dynamic_cast<const Enum *>(node);
323 return c->sig->second;
330 const Value & Value::operator[](size_t index) const {
331 const Array *array = dynamic_cast<const Array *>(unwrap(this));
333 if (index < array->values.size()) {
334 return *array->values[index];
340 const char * Value::string(void) const {
341 const String *string = dynamic_cast<const String *>(unwrap(this));
343 return string->value.c_str();
344 const Null *null = dynamic_cast<const Null *>(unwrap(this));
351 std::ostream & operator <<(std::ostream &os, Call &call) {
353 os << call.no << " ";
359 } /* namespace Trace */