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 << "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();
93 std::cerr << "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_BOOL:
116 case Trace::TYPE_SINT:
118 case Trace::TYPE_UINT:
120 case Trace::TYPE_FLOAT:
121 return parse_float();
122 case Trace::TYPE_DOUBLE:
123 return parse_double();
124 case Trace::TYPE_STRING:
125 return parse_string();
126 case Trace::TYPE_CONST:
127 return parse_const();
128 case Trace::TYPE_BITMASK:
129 return parse_bitmask();
130 case Trace::TYPE_ARRAY:
131 return parse_array();
132 case Trace::TYPE_POINTER:
133 return parse_pointer();
134 case Trace::TYPE_VOID:
137 std::cerr << "Unknown type " << c << "\n";
143 Value *parse_bool() {
149 Value *parse_sint() {
150 return new SInt(-read_uint());
153 Value *parse_uint() {
154 return new UInt(read_uint());
157 Value *parse_float() {
159 gzread(file, &value, sizeof value);
160 return new Float(value);
163 Value *parse_double() {
165 gzread(file, &value, sizeof value);
166 return new Float(value);
169 Value *parse_string() {
170 return new String(read_string());
173 Value *parse_const() {
174 std::string name = read_string();
175 Value *value = parse_value();
176 return new Const(name, value);
179 Value *parse_bitmask() {
180 unsigned long long value = 0;
185 case Trace::TYPE_SINT:
186 value |= -read_uint();
188 case Trace::TYPE_UINT:
189 value |= read_uint();
191 case Trace::TYPE_CONST:
194 case Trace::TYPE_VOID:
197 std::cerr << "Unexpected type " << c << "\n";
203 return new UInt(value);
206 Value *parse_array() {
207 size_t len = read_uint();
208 Array *array = new Array(len);
209 for (size_t i = 0; i < len; ++i) {
210 array->values[i] = parse_value();
215 Value *parse_pointer() {
216 unsigned long long addr;
219 value = parse_value();
221 value = new UInt(addr);
225 std::string read_string(void) {
226 size_t len = read_uint();
227 char * buf = new char[len];
228 gzread(file, buf, len);
229 std::string value(buf, len);
234 unsigned long long read_uint(void) {
235 unsigned long long value = 0;
243 value |= (unsigned long long)(c & 0x7f) << shift;
251 } /* namespace Trace */
253 #endif /* _TRACE_PARSER_HPP_ */