blob: e9f8c428e8e8d0c2706fe041c9a3fac874b59681 [file] [log] [blame]
José Fonseca7e329022010-11-19 17:05:18 +00001/**************************************************************************
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#ifndef _TRACE_PARSER_HPP_
27#define _TRACE_PARSER_HPP_
28
29
30#include <cassert>
31
32#include <iostream>
José Fonsecafa922142010-11-25 09:36:04 +000033#include <map>
34#include <string>
José Fonseca7e329022010-11-19 17:05:18 +000035
36#include <zlib.h>
37
38#include "trace_format.hpp"
39#include "trace_model.hpp"
40
41
José Fonsecaf4b071e2010-11-25 15:48:46 +000042#define TRACE_VERBOSE 0
43
44
José Fonseca7e329022010-11-19 17:05:18 +000045namespace Trace {
46
47
48class Parser
49{
50protected:
51 gzFile file;
José Fonsecafa922142010-11-25 09:36:04 +000052
53 typedef std::map<size_t, std::string> namemap;
54 namemap names;
55
José Fonseca34957132010-11-25 16:14:45 +000056 typedef std::map<unsigned, Call *> callmap;
57 callmap calls;
58
José Fonseca2250a0e2010-11-26 15:01:29 +000059 typedef std::map<size_t, Call::Signature *> FunctionMap;
60 FunctionMap functions;
61
José Fonseca7eef8ce2010-11-26 15:46:36 +000062 typedef std::map<size_t, Struct::Signature *> StructMap;
63 StructMap structs;
64
José Fonsecaf84c70e2010-11-26 15:26:14 +000065 typedef std::map<size_t, Enum *> EnumMap;
66 EnumMap enums;
67
José Fonsecad35973c2010-11-26 14:14:45 +000068 typedef std::map<size_t, Bitmask::Signature *> BitmaskMap;
69 BitmaskMap bitmasks;
70
José Fonseca34957132010-11-25 16:14:45 +000071 unsigned next_call_no;
72
José Fonseca7e329022010-11-19 17:05:18 +000073public:
74 Parser() {
75 file = NULL;
José Fonseca34957132010-11-25 16:14:45 +000076 next_call_no = 0;
José Fonseca7e329022010-11-19 17:05:18 +000077 }
78
José Fonseca6f51d3b2010-11-22 19:56:19 +000079 ~Parser() {
80 close();
81 }
82
83 bool open(const char *filename) {
José Fonseca7e329022010-11-19 17:05:18 +000084 unsigned long long version;
85
86 file = gzopen(filename, "rb");
87 if (!file) {
88 return false;
89 }
90
91 version = read_uint();
92 if (version != TRACE_VERSION) {
José Fonseca501f2862010-11-19 20:41:18 +000093 std::cerr << "error: unsupported format version" << version << "\n";
José Fonseca7e329022010-11-19 17:05:18 +000094 return false;
95 }
96
José Fonseca7e329022010-11-19 17:05:18 +000097 return true;
98 }
99
José Fonseca6f51d3b2010-11-22 19:56:19 +0000100 void close(void) {
101 if (file) {
102 gzclose(file);
103 file = NULL;
104 }
105 }
106
107 Call *parse_call(void) {
José Fonseca34957132010-11-25 16:14:45 +0000108 do {
109 int c = read_byte();
110 switch(c) {
111 case Trace::EVENT_ENTER:
112 parse_enter();
113 break;
114 case Trace::EVENT_LEAVE:
115 return parse_leave();
116 case Trace::EVENT_MESSAGE:
117 std::cerr << "message: " << read_string() << "\n";
118 break;
119 default:
120 std::cerr << "error: unknown call detail " << c << "\n";
121 assert(0);
122 /* fallthrough */
123 case -1:
124 return NULL;
125 }
126 } while(true);
127 }
128
129 void parse_enter(void) {
José Fonseca2250a0e2010-11-26 15:01:29 +0000130 size_t id = read_uint();
131
132 Call::Signature *sig;
133 FunctionMap::const_iterator it = functions.find(id);
134 if (it == functions.end()) {
135 sig = new Call::Signature;
136 sig->name = read_string();
137 unsigned size = read_uint();
138 for (unsigned i = 0; i < size; ++i) {
139 sig->arg_names.push_back(read_string());
140 }
141 functions[id] = sig;
142 } else {
143 sig = it->second;
144 }
145 assert(sig);
146
147 Call *call = new Call(sig);
José Fonseca34957132010-11-25 16:14:45 +0000148 call->no = next_call_no++;
José Fonseca2250a0e2010-11-26 15:01:29 +0000149
José Fonseca34957132010-11-25 16:14:45 +0000150 parse_call_details(call);
151 calls[call->no] = call;
152 }
153
154 Call *parse_leave(void) {
155 unsigned call_no = read_uint();
156 Call *call = calls[call_no];
157 assert(call);
158 if (!call) {
159 return NULL;
160 }
161 parse_call_details(call);
162 return call;
163 }
164
165 void parse_call_details(Call *call) {
José Fonseca7e329022010-11-19 17:05:18 +0000166 do {
José Fonseca6f51d3b2010-11-22 19:56:19 +0000167 int c = read_byte();
José Fonseca7e329022010-11-19 17:05:18 +0000168 switch(c) {
169 case Trace::CALL_END:
José Fonseca34957132010-11-25 16:14:45 +0000170 return;
José Fonseca7e329022010-11-19 17:05:18 +0000171 case Trace::CALL_ARG:
José Fonsecadce84c42010-11-24 16:19:49 +0000172 parse_arg(call);
José Fonseca7e329022010-11-19 17:05:18 +0000173 break;
174 case Trace::CALL_RET:
José Fonseca6f51d3b2010-11-22 19:56:19 +0000175 call->ret = parse_value();
José Fonseca7e329022010-11-19 17:05:18 +0000176 break;
177 default:
José Fonseca501f2862010-11-19 20:41:18 +0000178 std::cerr << "error: unknown call detail " << c << "\n";
José Fonseca885f2652010-11-20 11:22:25 +0000179 assert(0);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000180 /* fallthrough */
181 case -1:
José Fonseca34957132010-11-25 16:14:45 +0000182 return;
José Fonseca7e329022010-11-19 17:05:18 +0000183 }
184 } while(true);
José Fonseca7e329022010-11-19 17:05:18 +0000185 }
186
José Fonsecadce84c42010-11-24 16:19:49 +0000187 void parse_arg(Call *call) {
188 unsigned index = read_uint();
José Fonseca7e329022010-11-19 17:05:18 +0000189 Value *value = parse_value();
José Fonsecadce84c42010-11-24 16:19:49 +0000190 if (index >= call->args.size()) {
191 call->args.resize(index + 1);
192 }
José Fonseca2250a0e2010-11-26 15:01:29 +0000193 call->args[index] = value;
José Fonseca7e329022010-11-19 17:05:18 +0000194 }
195
196 Value *parse_value(void) {
197 int c;
José Fonseca5cb3e172010-11-22 16:59:34 +0000198 c = read_byte();
José Fonseca7e329022010-11-19 17:05:18 +0000199 switch(c) {
José Fonsecaf6592d72010-11-21 12:44:41 +0000200 case Trace::TYPE_NULL:
201 return new Null;
José Fonsecab1887f92010-11-21 02:33:38 +0000202 case Trace::TYPE_FALSE:
203 return new Bool(false);
204 case Trace::TYPE_TRUE:
205 return new Bool(true);
José Fonseca7e329022010-11-19 17:05:18 +0000206 case Trace::TYPE_SINT:
207 return parse_sint();
208 case Trace::TYPE_UINT:
209 return parse_uint();
210 case Trace::TYPE_FLOAT:
211 return parse_float();
212 case Trace::TYPE_DOUBLE:
213 return parse_double();
214 case Trace::TYPE_STRING:
215 return parse_string();
José Fonsecaf84c70e2010-11-26 15:26:14 +0000216 case Trace::TYPE_ENUM:
217 return parse_enum();
José Fonseca7e329022010-11-19 17:05:18 +0000218 case Trace::TYPE_BITMASK:
219 return parse_bitmask();
220 case Trace::TYPE_ARRAY:
221 return parse_array();
José Fonseca5cb3e172010-11-22 16:59:34 +0000222 case Trace::TYPE_STRUCT:
223 return parse_struct();
José Fonseca885f2652010-11-20 11:22:25 +0000224 case Trace::TYPE_BLOB:
225 return parse_blob();
José Fonsecaf6592d72010-11-21 12:44:41 +0000226 case Trace::TYPE_OPAQUE:
227 return parse_opaque();
José Fonseca7e329022010-11-19 17:05:18 +0000228 default:
José Fonseca501f2862010-11-19 20:41:18 +0000229 std::cerr << "error: unknown type " << c << "\n";
José Fonseca7e329022010-11-19 17:05:18 +0000230 assert(0);
231 return NULL;
232 }
233 }
234
José Fonseca7e329022010-11-19 17:05:18 +0000235 Value *parse_sint() {
José Fonseca0633bdf2010-11-25 14:54:14 +0000236 return new SInt(-(signed long long)read_uint());
José Fonseca7e329022010-11-19 17:05:18 +0000237 }
238
239 Value *parse_uint() {
240 return new UInt(read_uint());
241 }
242
243 Value *parse_float() {
244 float value;
245 gzread(file, &value, sizeof value);
246 return new Float(value);
247 }
248
249 Value *parse_double() {
250 double value;
251 gzread(file, &value, sizeof value);
252 return new Float(value);
253 }
254
255 Value *parse_string() {
256 return new String(read_string());
257 }
258
José Fonsecaf84c70e2010-11-26 15:26:14 +0000259 Value *parse_enum() {
260 size_t id = read_uint();
261 Enum *sig;
262 EnumMap::const_iterator it = enums.find(id);
263 if (it == enums.end()) {
264 std::string name = read_string();
265 Value *value = parse_value();
266 sig = new Enum(name, value);
267 enums[id] = sig;
268 } else {
269 sig = it->second;
270 }
271 assert(sig);
272 return sig;
José Fonseca7e329022010-11-19 17:05:18 +0000273 }
274
275 Value *parse_bitmask() {
José Fonsecad35973c2010-11-26 14:14:45 +0000276 size_t id = read_uint();
277 Bitmask::Signature *sig;
278 BitmaskMap::const_iterator it = bitmasks.find(id);
279 if (it == bitmasks.end()) {
280 size_t size = read_uint();
281 sig = new Bitmask::Signature(size);
282 for (Bitmask::Signature::iterator it = sig->begin(); it != sig->end(); ++it) {
283 it->first = read_string();
284 it->second = read_uint();
285 assert(it->second);
286 }
287 bitmasks[id] = sig;
288 } else {
289 sig = it->second;
290 }
291 assert(sig);
292
293 unsigned long long value = read_uint();
294
295 return new Bitmask(sig, value);
José Fonseca7e329022010-11-19 17:05:18 +0000296 }
297
José Fonseca885f2652010-11-20 11:22:25 +0000298 Value *parse_array(void) {
José Fonseca7e329022010-11-19 17:05:18 +0000299 size_t len = read_uint();
300 Array *array = new Array(len);
301 for (size_t i = 0; i < len; ++i) {
302 array->values[i] = parse_value();
303 }
304 return array;
305 }
306
José Fonseca885f2652010-11-20 11:22:25 +0000307 Value *parse_blob(void) {
308 size_t size = read_uint();
309 Blob *blob = new Blob(size);
310 if (size) {
311 gzread(file, blob->buf, size);
312 }
313 return blob;
314 }
315
José Fonseca5cb3e172010-11-22 16:59:34 +0000316 Value *parse_struct() {
José Fonseca7eef8ce2010-11-26 15:46:36 +0000317 size_t id = read_uint();
318
319 Struct::Signature *sig;
320 StructMap::const_iterator it = structs.find(id);
321 if (it == structs.end()) {
322 sig = new Struct::Signature;
323 sig->name = read_string();
324 unsigned size = read_uint();
325 for (unsigned i = 0; i < size; ++i) {
326 sig->member_names.push_back(read_string());
327 }
328 structs[id] = sig;
329 } else {
330 sig = it->second;
José Fonseca5cb3e172010-11-22 16:59:34 +0000331 }
José Fonseca7eef8ce2010-11-26 15:46:36 +0000332 assert(sig);
333
334 Struct *value = new Struct(sig);
335
336 for (size_t i = 0; i < sig->member_names.size(); ++i) {
337 value->members[i] = parse_value();
338 }
339
340 return value;
José Fonseca5cb3e172010-11-22 16:59:34 +0000341 }
342
José Fonsecaf6592d72010-11-21 12:44:41 +0000343 Value *parse_opaque() {
344 unsigned long long addr;
345 addr = read_uint();
346 /* XXX */
347 return new UInt(addr);
348 }
José Fonsecafa922142010-11-25 09:36:04 +0000349
350 std::string read_name(void) {
351 std::string name;
352 size_t id = read_uint();
353 if (id >= names.size()) {
José Fonsecaf4b071e2010-11-25 15:48:46 +0000354 assert(id == names.size());
José Fonsecafa922142010-11-25 09:36:04 +0000355 name = read_string();
356 names[id] = name;
357 return name;
358 } else {
José Fonsecaf4b071e2010-11-25 15:48:46 +0000359 name = names[id];
José Fonsecafa922142010-11-25 09:36:04 +0000360 }
José Fonsecaf4b071e2010-11-25 15:48:46 +0000361#if TRACE_VERBOSE
362 std::cerr << "\tNAME " << id << " " << name << "\n";
363#endif
364 return name;
José Fonsecafa922142010-11-25 09:36:04 +0000365 }
José Fonsecaf6592d72010-11-21 12:44:41 +0000366
José Fonseca7e329022010-11-19 17:05:18 +0000367 std::string read_string(void) {
368 size_t len = read_uint();
José Fonseca5cb3e172010-11-22 16:59:34 +0000369 if (!len) {
370 return std::string();
371 }
José Fonseca7e329022010-11-19 17:05:18 +0000372 char * buf = new char[len];
373 gzread(file, buf, len);
374 std::string value(buf, len);
375 delete [] buf;
José Fonsecaf4b071e2010-11-25 15:48:46 +0000376#if TRACE_VERBOSE
377 std::cerr << "\tSTRING \"" << value << "\"\n";
José Fonseca5cb3e172010-11-22 16:59:34 +0000378#endif
José Fonseca7e329022010-11-19 17:05:18 +0000379 return value;
380 }
381
382 unsigned long long read_uint(void) {
383 unsigned long long value = 0;
384 int c;
385 unsigned shift = 0;
386 do {
387 c = gzgetc(file);
388 if (c == -1) {
389 break;
390 }
391 value |= (unsigned long long)(c & 0x7f) << shift;
392 shift += 7;
393 } while(c & 0x80);
José Fonsecaf4b071e2010-11-25 15:48:46 +0000394#if TRACE_VERBOSE
395 std::cerr << "\tUINT " << value << "\n";
José Fonseca5cb3e172010-11-22 16:59:34 +0000396#endif
José Fonseca7e329022010-11-19 17:05:18 +0000397 return value;
398 }
José Fonseca5cb3e172010-11-22 16:59:34 +0000399
400 int read_byte(void) {
401 int c = gzgetc(file);
José Fonsecaf4b071e2010-11-25 15:48:46 +0000402#if TRACE_VERBOSE
José Fonseca5cb3e172010-11-22 16:59:34 +0000403 if (c < 0)
José Fonsecaf4b071e2010-11-25 15:48:46 +0000404 std::cerr << "\tEOF" << "\n";
José Fonseca5cb3e172010-11-22 16:59:34 +0000405 else
José Fonsecaf4b071e2010-11-25 15:48:46 +0000406 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
José Fonseca5cb3e172010-11-22 16:59:34 +0000407#endif
408 return c;
409 }
José Fonseca7e329022010-11-19 17:05:18 +0000410};
411
412
413} /* namespace Trace */
414
415#endif /* _TRACE_PARSER_HPP_ */