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 "trace_model.hpp"
34 for (unsigned i = 0; i < args.size(); ++i) {
50 for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
57 for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
63 // Blobs are often bound and referred during many calls, so we can't delete
64 // them here in that case.
66 // Once bound there is no way to know when they were unbound, which
67 // effectively means we have to leak them. A better solution would be to
68 // keep a list of bound pointers, and defer the destruction to when the
69 // trace in question has been fully processed.
75 StackFrame::~StackFrame() {
79 if (function != NULL) {
82 if (filename != NULL) {
85 if (linenumber != NULL) {
93 Backtrace::~Backtrace() {
94 for (int i = 0; i < frames.size(); i++) {
100 bool Null ::toBool(void) const { return false; }
101 bool Bool ::toBool(void) const { return value; }
102 bool SInt ::toBool(void) const { return value != 0; }
103 bool UInt ::toBool(void) const { return value != 0; }
104 bool Float ::toBool(void) const { return value != 0; }
105 bool Double ::toBool(void) const { return value != 0; }
106 bool String ::toBool(void) const { return true; }
107 bool Struct ::toBool(void) const { return true; }
108 bool Array ::toBool(void) const { return true; }
109 bool Blob ::toBool(void) const { return true; }
110 bool Pointer::toBool(void) const { return value != 0; }
111 bool Repr ::toBool(void) const { return static_cast<bool>(machineValue); }
114 // signed integer cast
115 signed long long Value ::toSInt(void) const { assert(0); return 0; }
116 signed long long Null ::toSInt(void) const { return 0; }
117 signed long long Bool ::toSInt(void) const { return static_cast<signed long long>(value); }
118 signed long long SInt ::toSInt(void) const { return value; }
119 signed long long UInt ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
120 signed long long Float ::toSInt(void) const { return static_cast<signed long long>(value); }
121 signed long long Double ::toSInt(void) const { return static_cast<signed long long>(value); }
122 signed long long Repr ::toSInt(void) const { return machineValue->toSInt(); }
125 // unsigned integer cast
126 unsigned long long Value ::toUInt(void) const { assert(0); return 0; }
127 unsigned long long Null ::toUInt(void) const { return 0; }
128 unsigned long long Bool ::toUInt(void) const { return static_cast<unsigned long long>(value); }
129 unsigned long long SInt ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
130 unsigned long long UInt ::toUInt(void) const { return value; }
131 unsigned long long Float ::toUInt(void) const { return static_cast<unsigned long long>(value); }
132 unsigned long long Double ::toUInt(void) const { return static_cast<unsigned long long>(value); }
133 unsigned long long Repr ::toUInt(void) const { return machineValue->toUInt(); }
136 // floating point cast
137 float Value ::toFloat(void) const { assert(0); return 0; }
138 float Null ::toFloat(void) const { return 0; }
139 float Bool ::toFloat(void) const { return static_cast<float>(value); }
140 float SInt ::toFloat(void) const { return static_cast<float>(value); }
141 float UInt ::toFloat(void) const { return static_cast<float>(value); }
142 float Float ::toFloat(void) const { return value; }
143 float Double ::toFloat(void) const { return value; }
144 float Repr ::toFloat(void) const { return machineValue->toFloat(); }
147 // floating point cast
148 double Value ::toDouble(void) const { assert(0); return 0; }
149 double Null ::toDouble(void) const { return 0; }
150 double Bool ::toDouble(void) const { return static_cast<double>(value); }
151 double SInt ::toDouble(void) const { return static_cast<double>(value); }
152 double UInt ::toDouble(void) const { return static_cast<double>(value); }
153 double Float ::toDouble(void) const { return value; }
154 double Double ::toDouble(void) const { return value; }
155 double Repr ::toDouble(void) const { return machineValue->toDouble(); }
159 void * Value ::toPointer(void) const { assert(0); return NULL; }
160 void * Null ::toPointer(void) const { return NULL; }
161 void * Blob ::toPointer(void) const { return buf; }
162 void * Pointer::toPointer(void) const { return (void *)value; }
163 void * Repr ::toPointer(void) const { return machineValue->toPointer(); }
165 void * Value ::toPointer(bool bind) { assert(0); return NULL; }
166 void * Null ::toPointer(bool bind) { return NULL; }
167 void * Blob ::toPointer(bool bind) { if (bind) bound = true; return buf; }
168 void * Pointer::toPointer(bool bind) { return (void *)value; }
169 void * Repr ::toPointer(bool bind) { return machineValue->toPointer(bind); }
172 // unsigned int pointer cast
173 unsigned long long Value ::toUIntPtr(void) const { assert(0); return 0; }
174 unsigned long long Null ::toUIntPtr(void) const { return 0; }
175 unsigned long long Pointer::toUIntPtr(void) const { return value; }
176 unsigned long long Repr ::toUIntPtr(void) const { return machineValue->toUIntPtr(); }
180 const char * Value ::toString(void) const { assert(0); return NULL; }
181 const char * Null ::toString(void) const { return NULL; }
182 const char * String::toString(void) const { return value; }
183 const char * Repr ::toString(void) const { return machineValue->toString(); }
186 // virtual Value::visit()
187 void Null ::visit(Visitor &visitor) { visitor.visit(this); }
188 void Bool ::visit(Visitor &visitor) { visitor.visit(this); }
189 void SInt ::visit(Visitor &visitor) { visitor.visit(this); }
190 void UInt ::visit(Visitor &visitor) { visitor.visit(this); }
191 void Float ::visit(Visitor &visitor) { visitor.visit(this); }
192 void Double ::visit(Visitor &visitor) { visitor.visit(this); }
193 void String ::visit(Visitor &visitor) { visitor.visit(this); }
194 void Enum ::visit(Visitor &visitor) { visitor.visit(this); }
195 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
196 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
197 void Array ::visit(Visitor &visitor) { visitor.visit(this); }
198 void Blob ::visit(Visitor &visitor) { visitor.visit(this); }
199 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
200 void Repr ::visit(Visitor &visitor) { visitor.visit(this); }
202 void Backtrace::addFrame(StackFrame* frame) {
203 frames.push_back(frame);
206 void Visitor::visit(Null *) { assert(0); }
207 void Visitor::visit(Bool *) { assert(0); }
208 void Visitor::visit(SInt *) { assert(0); }
209 void Visitor::visit(UInt *) { assert(0); }
210 void Visitor::visit(Float *) { assert(0); }
211 void Visitor::visit(Double *) { assert(0); }
212 void Visitor::visit(String *) { assert(0); }
213 void Visitor::visit(Enum *node) { assert(0); }
214 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
215 void Visitor::visit(Struct *) { assert(0); }
216 void Visitor::visit(Array *) { assert(0); }
217 void Visitor::visit(Blob *) { assert(0); }
218 void Visitor::visit(Pointer *) { assert(0); }
219 void Visitor::visit(Repr *node) { node->machineValue->visit(*this); }
220 void Visitor::visit(Backtrace *) { assert(0); }
221 void Visitor::visit(StackFrame *) { assert(0); }
226 const Value & Value::operator[](size_t index) const {
227 const Array *array = dynamic_cast<const Array *>(this);
229 if (index < array->values.size()) {
230 return *array->values[index];
236 } /* namespace trace */