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 bool Null ::toBool(void) const { return false; }
67 bool Bool ::toBool(void) const { return value; }
68 bool SInt ::toBool(void) const { return value != 0; }
69 bool UInt ::toBool(void) const { return value != 0; }
70 bool Float ::toBool(void) const { return value != 0; }
71 bool String ::toBool(void) const { return true; }
72 bool Enum ::toBool(void) const { return sig->second->toBool(); }
73 bool Struct ::toBool(void) const { return true; }
74 bool Array ::toBool(void) const { return true; }
75 bool Blob ::toBool(void) const { return true; }
76 bool Pointer::toBool(void) const { return value != 0; }
79 // signed integer cast
80 signed long long Value ::toSInt(void) const { assert(0); return 0; }
81 signed long long Null ::toSInt(void) const { return 0; }
82 signed long long Bool ::toSInt(void) const { return static_cast<signed long long>(value); }
83 signed long long SInt ::toSInt(void) const { return value; }
84 signed long long UInt ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
85 signed long long Float ::toSInt(void) const { return static_cast<signed long long>(value); }
86 signed long long Enum ::toSInt(void) const { return sig->second->toSInt(); }
89 // unsigned integer cast
90 unsigned long long Value ::toUInt(void) const { assert(0); return 0; }
91 unsigned long long Null ::toUInt(void) const { return 0; }
92 unsigned long long Bool ::toUInt(void) const { return static_cast<unsigned long long>(value); }
93 unsigned long long SInt ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
94 unsigned long long UInt ::toUInt(void) const { return value; }
95 unsigned long long Float ::toUInt(void) const { return static_cast<unsigned long long>(value); }
96 unsigned long long Enum ::toUInt(void) const { return sig->second->toUInt(); }
99 // floating point cast
100 float Value ::toFloat(void) const { assert(0); return 0; }
101 float Null ::toFloat(void) const { return 0; }
102 float Bool ::toFloat(void) const { return static_cast<float>(value); }
103 float SInt ::toFloat(void) const { return static_cast<float>(value); }
104 float UInt ::toFloat(void) const { return static_cast<float>(value); }
105 float Float ::toFloat(void) const { return value; }
106 float Enum ::toFloat(void) const { return sig->second->toFloat(); }
109 // floating point cast
110 double Value ::toDouble(void) const { assert(0); return 0; }
111 double Null ::toDouble(void) const { return 0; }
112 double Bool ::toDouble(void) const { return static_cast<double>(value); }
113 double SInt ::toDouble(void) const { return static_cast<double>(value); }
114 double UInt ::toDouble(void) const { return static_cast<double>(value); }
115 double Float ::toDouble(void) const { return value; }
116 double Enum ::toDouble(void) const { return sig->second->toDouble(); }
120 void * Value ::toPointer(void) const { assert(0); return NULL; }
121 void * Null ::toPointer(void) const { return NULL; }
122 void * Blob ::toPointer(void) const { return buf; }
123 void * Pointer::toPointer(void) const { return (void *)value; }
127 unsigned long long Value ::toUIntPtr(void) const { assert(0); return 0; }
128 unsigned long long Null ::toUIntPtr(void) const { return 0; }
129 unsigned long long Pointer::toUIntPtr(void) const { return value; }
133 const char * Value ::toString(void) const { assert(0); return NULL; }
134 const char * Null ::toString(void) const { return NULL; }
135 const char * String::toString(void) const { return value.c_str(); }
138 // virtual Value::visit()
139 void Null ::visit(Visitor &visitor) { visitor.visit(this); }
140 void Bool ::visit(Visitor &visitor) { visitor.visit(this); }
141 void SInt ::visit(Visitor &visitor) { visitor.visit(this); }
142 void UInt ::visit(Visitor &visitor) { visitor.visit(this); }
143 void Float ::visit(Visitor &visitor) { visitor.visit(this); }
144 void String ::visit(Visitor &visitor) { visitor.visit(this); }
145 void Enum ::visit(Visitor &visitor) { visitor.visit(this); }
146 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
147 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
148 void Array ::visit(Visitor &visitor) { visitor.visit(this); }
149 void Blob ::visit(Visitor &visitor) { visitor.visit(this); }
150 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
153 void Visitor::visit(Null *) { assert(0); }
154 void Visitor::visit(Bool *) { assert(0); }
155 void Visitor::visit(SInt *) { assert(0); }
156 void Visitor::visit(UInt *) { assert(0); }
157 void Visitor::visit(Float *) { assert(0); }
158 void Visitor::visit(String *) { assert(0); }
159 void Visitor::visit(Enum *node) { _visit(node->sig->second); }
160 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
161 void Visitor::visit(Struct *) { assert(0); }
162 void Visitor::visit(Array *) { assert(0); }
163 void Visitor::visit(Blob *) { assert(0); }
164 void Visitor::visit(Pointer *) { assert(0); }
167 class Dumper : public Visitor
171 Formatter::Formatter *formatter;
172 Formatter::Attribute *normal;
173 Formatter::Attribute *bold;
174 Formatter::Attribute *italic;
175 Formatter::Attribute *red;
176 Formatter::Attribute *pointer;
177 Formatter::Attribute *literal;
180 Dumper(std::ostream &_os) : os(_os) {
181 formatter = Formatter::defaultFormatter();
182 normal = formatter->normal();
183 bold = formatter->bold();
184 italic = formatter->italic();
185 red = formatter->color(Formatter::RED);
186 pointer = formatter->color(Formatter::GREEN);
187 literal = formatter->color(Formatter::BLUE);
204 void visit(Bool *node) {
205 os << literal << (node->value ? "true" : "false") << normal;
208 void visit(SInt *node) {
209 os << literal << node->value << normal;
212 void visit(UInt *node) {
213 os << literal << node->value << normal;
216 void visit(Float *node) {
217 os << literal << node->value << normal;
220 void visit(String *node) {
221 os << literal << "\"";
222 for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) {
223 unsigned char c = (unsigned char) *it;
228 else if (c >= 0x20 && c <= 0x7e)
230 else if (c == '\t') {
232 } else if (c == '\r') {
233 // Ignore carriage-return
234 } else if (c == '\n') {
235 // Reset formatting so that it looks correct with 'less -R'
236 os << normal << '\n' << literal;
238 unsigned octal0 = c & 0x7;
239 unsigned octal1 = (c >> 3) & 0x7;
240 unsigned octal2 = (c >> 3) & 0x7;
249 os << "\"" << normal;
252 void visit(Enum *node) {
253 os << literal << node->sig->first << normal;
256 void visit(Bitmask *bitmask) {
257 unsigned long long value = bitmask->value;
258 const Bitmask::Signature *sig = bitmask->sig;
260 for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
261 if ((it->second && (value & it->second) == it->second) ||
262 (!it->second && value == 0)) {
266 os << literal << it->first << normal;
267 value &= ~it->second;
271 if (value || first) {
275 os << literal << "0x" << std::hex << value << std::dec << normal;
279 void visit(Struct *s) {
280 const char *sep = "";
282 for (unsigned i = 0; i < s->members.size(); ++i) {
283 os << sep << italic << s->sig->member_names[i] << normal << " = ";
284 _visit(s->members[i]);
290 void visit(Array *array) {
291 if (array->values.size() == 1) {
293 _visit(array->values[0]);
296 const char *sep = "";
298 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
307 void visit(Blob *blob) {
308 os << pointer << "blob(" << blob->size << ")" << normal;
311 void visit(Pointer *p) {
312 os << pointer << "0x" << std::hex << p->value << std::dec << normal;
315 void visit(Call *call) {
316 const char *sep = "";
317 os << bold << call->sig->name << normal << "(";
318 for (unsigned i = 0; i < call->args.size(); ++i) {
319 os << sep << italic << call->sig->arg_names[i] << normal << " = ";
320 _visit(call->args[i]);
333 std::ostream & operator <<(std::ostream &os, Value *value) {
342 static inline const Value *unwrap(const Value *node) {
343 const Enum *c = dynamic_cast<const Enum *>(node);
345 return c->sig->second;
352 const Value & Value::operator[](size_t index) const {
353 const Array *array = dynamic_cast<const Array *>(unwrap(this));
355 if (index < array->values.size()) {
356 return *array->values[index];
362 std::ostream & operator <<(std::ostream &os, Call &call) {
364 os << call.no << " ";
370 } /* namespace Trace */