1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27 #include <string.h>
28 #include <deque>
29
30 #include "trace_model.hpp"
31
32
33 namespace trace {
34
35
36 static Null null;
37
38
~Call()39 Call::~Call() {
40 for (auto & arg : args) {
41 delete arg.value;
42 }
43
44 delete ret;
45 }
46
47 Value &
argByName(const char * argName)48 Call::argByName(const char *argName) {
49 for (unsigned i = 0; i < sig->num_args; ++i) {
50 if (strcmp(sig->arg_names[i], argName) == 0) {
51 return arg(i);
52 }
53 }
54 return null;
55 }
56
57
~String()58 String::~String() {
59 delete [] value;
60 }
61
62
~WString()63 WString::~WString() {
64 delete [] value;
65 }
66
67
~Struct()68 Struct::~Struct() {
69 for (auto & member : members) {
70 delete member;
71 }
72 }
73
74
~Array()75 Array::~Array() {
76 for (auto & value : values) {
77 delete value;
78 }
79 }
80
81
82 #define BLOB_MAX_BOUND_SIZE (1*1024*1024*1024)
83
84 class BoundBlob {
85 public:
86 static size_t totalSize;
87
88 private:
89 size_t size;
90 char *buf;
91
92 public:
93 inline
BoundBlob(size_t _size,char * _buf)94 BoundBlob(size_t _size, char *_buf) :
95 size(_size),
96 buf(_buf)
97 {
98 assert(totalSize + size >= totalSize);
99 totalSize += size;
100 }
101
102 inline
~BoundBlob()103 ~BoundBlob() {
104 assert(totalSize >= size);
105 totalSize -= size;
106 delete [] buf;
107 }
108
109 // Move constructor
BoundBlob(BoundBlob && other)110 BoundBlob(BoundBlob && other)
111 {
112 size = other.size;
113 buf = other.buf;
114 other.size = 0;
115 other.buf = nullptr;
116 }
117
118 // Disallow copy/assignment
119 BoundBlob(const BoundBlob & other) = delete;
120 BoundBlob & operator = (const BoundBlob &) = delete;
121 };
122
123 size_t BoundBlob::totalSize = 0;
124
125 typedef std::deque<BoundBlob> BoundBlobQueue;
126 static BoundBlobQueue boundBlobQueue;
127
128
~Blob()129 Blob::~Blob() {
130 // Blobs are often bound and referred during many calls, so we can't delete
131 // them here in that case.
132 //
133 // Once bound there is no way to know when they were unbound, which
134 // effectively means we have to leak them. But some applications
135 // (particularly OpenGL applications that use vertex arrays in user memory)
136 // we can easily exhaust all memory. So instead we maintain a queue of
137 // bound blobs and keep the total size bounded.
138
139 if (!bound) {
140 delete [] buf;
141 return;
142 }
143
144 while (!boundBlobQueue.empty() &&
145 BoundBlob::totalSize + size > BLOB_MAX_BOUND_SIZE) {
146 boundBlobQueue.pop_front();
147 }
148
149 BoundBlob bb(size, buf);
150 boundBlobQueue.push_back(std::move(bb));
151 }
152
~StackFrame()153 StackFrame::~StackFrame() {
154 delete [] module;
155 delete [] function;
156 delete [] filename;
157 }
158
159
160 // bool cast
toBool(void) const161 bool Null ::toBool(void) const { return false; }
toBool(void) const162 bool Bool ::toBool(void) const { return value; }
toBool(void) const163 bool SInt ::toBool(void) const { return value != 0; }
toBool(void) const164 bool UInt ::toBool(void) const { return value != 0; }
toBool(void) const165 bool Float ::toBool(void) const { return value != 0; }
toBool(void) const166 bool Double ::toBool(void) const { return value != 0; }
toBool(void) const167 bool String ::toBool(void) const { return true; }
toBool(void) const168 bool WString::toBool(void) const { return true; }
toBool(void) const169 bool Struct ::toBool(void) const { return true; }
toBool(void) const170 bool Array ::toBool(void) const { return true; }
toBool(void) const171 bool Blob ::toBool(void) const { return true; }
toBool(void) const172 bool Pointer::toBool(void) const { return value != 0; }
toBool(void) const173 bool Repr ::toBool(void) const { return static_cast<bool>(machineValue); }
174
175
176 // signed integer cast
toSInt(void) const177 signed long long Value ::toSInt(void) const { assert(0); return 0; }
toSInt(void) const178 signed long long Null ::toSInt(void) const { return 0; }
toSInt(void) const179 signed long long Bool ::toSInt(void) const { return static_cast<signed long long>(value); }
toSInt(void) const180 signed long long SInt ::toSInt(void) const { return value; }
toSInt(void) const181 signed long long UInt ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
toSInt(void) const182 signed long long Float ::toSInt(void) const { return static_cast<signed long long>(value); }
toSInt(void) const183 signed long long Double ::toSInt(void) const { return static_cast<signed long long>(value); }
toSInt(void) const184 signed long long Repr ::toSInt(void) const { return machineValue->toSInt(); }
185
186
187 // unsigned integer cast
toUInt(void) const188 unsigned long long Value ::toUInt(void) const { assert(0); return 0; }
toUInt(void) const189 unsigned long long Null ::toUInt(void) const { return 0; }
toUInt(void) const190 unsigned long long Bool ::toUInt(void) const { return static_cast<unsigned long long>(value); }
toUInt(void) const191 unsigned long long SInt ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
toUInt(void) const192 unsigned long long UInt ::toUInt(void) const { return value; }
toUInt(void) const193 unsigned long long Float ::toUInt(void) const { return static_cast<unsigned long long>(value); }
toUInt(void) const194 unsigned long long Double ::toUInt(void) const { return static_cast<unsigned long long>(value); }
toUInt(void) const195 unsigned long long Repr ::toUInt(void) const { return machineValue->toUInt(); }
196
197
198 // floating point cast
toFloat(void) const199 float Value ::toFloat(void) const { assert(0); return 0; }
toFloat(void) const200 float Null ::toFloat(void) const { return 0; }
toFloat(void) const201 float Bool ::toFloat(void) const { return static_cast<float>(value); }
toFloat(void) const202 float SInt ::toFloat(void) const { return static_cast<float>(value); }
toFloat(void) const203 float UInt ::toFloat(void) const { return static_cast<float>(value); }
toFloat(void) const204 float Float ::toFloat(void) const { return value; }
toFloat(void) const205 float Double ::toFloat(void) const { return value; }
toFloat(void) const206 float Repr ::toFloat(void) const { return machineValue->toFloat(); }
207
208
209 // floating point cast
toDouble(void) const210 double Value ::toDouble(void) const { assert(0); return 0; }
toDouble(void) const211 double Null ::toDouble(void) const { return 0; }
toDouble(void) const212 double Bool ::toDouble(void) const { return static_cast<double>(value); }
toDouble(void) const213 double SInt ::toDouble(void) const { return static_cast<double>(value); }
toDouble(void) const214 double UInt ::toDouble(void) const { return static_cast<double>(value); }
toDouble(void) const215 double Float ::toDouble(void) const { return value; }
toDouble(void) const216 double Double ::toDouble(void) const { return value; }
toDouble(void) const217 double Repr ::toDouble(void) const { return machineValue->toDouble(); }
218
219
220 // pointer cast
toPointer(void) const221 void * Value ::toPointer(void) const { assert(0); return NULL; }
toPointer(void) const222 void * Null ::toPointer(void) const { return NULL; }
toPointer(void) const223 void * Blob ::toPointer(void) const { return buf; }
toPointer(void) const224 void * Pointer::toPointer(void) const { return (void *)value; }
toPointer(void) const225 void * Repr ::toPointer(void) const { return machineValue->toPointer(); }
226
toPointer(bool bind)227 void * Value ::toPointer(bool bind) { assert(0); return NULL; }
toPointer(bool bind)228 void * Null ::toPointer(bool bind) { return NULL; }
toPointer(bool bind)229 void * Blob ::toPointer(bool bind) { if (bind) bound = true; return buf; }
toPointer(bool bind)230 void * Pointer::toPointer(bool bind) { return (void *)value; }
toPointer(bool bind)231 void * Repr ::toPointer(bool bind) { return machineValue->toPointer(bind); }
232
233
234 // unsigned int pointer cast
toUIntPtr(void) const235 unsigned long long Value ::toUIntPtr(void) const { assert(0); return 0; }
toUIntPtr(void) const236 unsigned long long Null ::toUIntPtr(void) const { return 0; }
toUIntPtr(void) const237 unsigned long long Pointer::toUIntPtr(void) const { return value; }
toUIntPtr(void) const238 unsigned long long Repr ::toUIntPtr(void) const { return machineValue->toUIntPtr(); }
239
240
241 // string cast
toString(void) const242 const char * Value ::toString(void) const { assert(0); return NULL; }
toString(void) const243 const char * Null ::toString(void) const { return NULL; }
toString(void) const244 const char * String::toString(void) const { return value; }
toString(void) const245 const char * Repr ::toString(void) const { return machineValue->toString(); }
246
247
248 // virtual Value::visit()
visit(Visitor & visitor)249 void Null ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)250 void Bool ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)251 void SInt ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)252 void UInt ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)253 void Float ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)254 void Double ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)255 void String ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)256 void WString::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)257 void Enum ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)258 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)259 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)260 void Array ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)261 void Blob ::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)262 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
visit(Visitor & visitor)263 void Repr ::visit(Visitor &visitor) { visitor.visit(this); }
264
265
visit(Null *)266 void Visitor::visit(Null *) { assert(0); }
visit(Bool *)267 void Visitor::visit(Bool *) { assert(0); }
visit(SInt *)268 void Visitor::visit(SInt *) { assert(0); }
visit(UInt *)269 void Visitor::visit(UInt *) { assert(0); }
visit(Float *)270 void Visitor::visit(Float *) { assert(0); }
visit(Double *)271 void Visitor::visit(Double *) { assert(0); }
visit(String *)272 void Visitor::visit(String *) { assert(0); }
visit(WString *)273 void Visitor::visit(WString *) { assert(0); }
visit(Enum * node)274 void Visitor::visit(Enum *node) { assert(0); }
visit(Bitmask * node)275 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
visit(Struct *)276 void Visitor::visit(Struct *) { assert(0); }
visit(Array *)277 void Visitor::visit(Array *) { assert(0); }
visit(Blob *)278 void Visitor::visit(Blob *) { assert(0); }
visit(Pointer *)279 void Visitor::visit(Pointer *) { assert(0); }
visit(Repr * node)280 void Visitor::visit(Repr *node) { node->machineValue->visit(*this); }
281
282
283 Value &
operator [](size_t index) const284 Value::operator[] (size_t index) const {
285 const Array *array = toArray();
286 if (array) {
287 if (index < array->values.size()) {
288 return *array->values[index];
289 }
290 }
291 return null;
292 }
293
294 } /* namespace trace */
295