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 **************************************************************************/
26 #ifndef _TRACE_PARSER_HPP_
27 #define _TRACE_PARSER_HPP_
36 #include "trace_format.hpp"
37 #include "trace_model.hpp"
52 bool parse(const char *filename) {
53 unsigned long long version;
55 file = gzopen(filename, "rb");
60 version = read_uint();
61 if (version != TRACE_VERSION) {
62 std::cerr << "error: unsupported format version" << version << "\n";
66 while (!gzeof(file)) {
73 void parse_call(void) {
75 call.name = read_string();
79 if (c == Trace::CALL_END || c == -1) {
86 call.args.push_back(parse_arg());
89 call.ret = parse_value();
92 std::cerr << "error: unknown call detail " << c << "\n";
100 virtual void handle_call(Call &call) {
104 Arg parse_arg(void) {
105 std::string name = read_string();
106 Value *value = parse_value();
107 return Arg(name, value);
110 Value *parse_value(void) {
114 case Trace::TYPE_FALSE:
115 return new Bool(false);
116 case Trace::TYPE_TRUE:
117 return new Bool(true);
118 case Trace::TYPE_SINT:
120 case Trace::TYPE_UINT:
122 case Trace::TYPE_FLOAT:
123 return parse_float();
124 case Trace::TYPE_DOUBLE:
125 return parse_double();
126 case Trace::TYPE_STRING:
127 return parse_string();
128 case Trace::TYPE_CONST:
129 return parse_const();
130 case Trace::TYPE_BITMASK:
131 return parse_bitmask();
132 case Trace::TYPE_ARRAY:
133 return parse_array();
134 case Trace::TYPE_BLOB:
136 case Trace::TYPE_POINTER:
137 return parse_pointer();
138 case Trace::TYPE_NULL:
141 std::cerr << "error: unknown type " << c << "\n";
147 Value *parse_bool() {
153 Value *parse_sint() {
154 return new SInt(-read_uint());
157 Value *parse_uint() {
158 return new UInt(read_uint());
161 Value *parse_float() {
163 gzread(file, &value, sizeof value);
164 return new Float(value);
167 Value *parse_double() {
169 gzread(file, &value, sizeof value);
170 return new Float(value);
173 Value *parse_string() {
174 return new String(read_string());
177 Value *parse_const() {
178 std::string name = read_string();
179 Value *value = parse_value();
180 return new Const(name, value);
183 Value *parse_bitmask() {
184 unsigned long long value = 0;
189 case Trace::TYPE_SINT:
190 value |= -read_uint();
192 case Trace::TYPE_UINT:
193 value |= read_uint();
195 case Trace::TYPE_CONST:
198 case Trace::TYPE_NULL:
201 std::cerr << "error: uexpected type " << c << "\n";
207 return new UInt(value);
210 Value *parse_array(void) {
211 size_t len = read_uint();
212 Array *array = new Array(len);
213 for (size_t i = 0; i < len; ++i) {
214 array->values[i] = parse_value();
219 Value *parse_blob(void) {
220 size_t size = read_uint();
221 Blob *blob = new Blob(size);
223 gzread(file, blob->buf, size);
228 Value *parse_pointer() {
229 unsigned long long addr;
232 value = parse_value();
234 value = new UInt(addr);
238 std::string read_string(void) {
239 size_t len = read_uint();
240 char * buf = new char[len];
241 gzread(file, buf, len);
242 std::string value(buf, len);
247 unsigned long long read_uint(void) {
248 unsigned long long value = 0;
256 value |= (unsigned long long)(c & 0x7f) << shift;
264 } /* namespace Trace */
266 #endif /* _TRACE_PARSER_HPP_ */