1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright 2010 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
31 #include "trace_file.hpp"
32 #include "trace_parser.hpp"
35 #define TRACE_VERBOSE 0
53 bool Parser::open(const char *filename) {
55 if (File::isZLibCompressed(filename)) {
58 file = new SnappyFile;
61 if (!file->open(filename, File::Read)) {
65 version = read_uint();
66 if (version > TRACE_VERSION) {
67 std::cerr << "error: unsupported trace format version " << version << "\n";
74 template <typename Iter>
76 deleteAll(Iter begin, Iter end)
78 while (begin != end) {
84 template <typename Container>
86 deleteAll(const Container &c)
88 deleteAll(c.begin(), c.end());
91 void Parser::close(void) {
106 Call *Parser::parse_call(void) {
110 case Trace::EVENT_ENTER:
113 case Trace::EVENT_LEAVE:
114 return parse_leave();
116 std::cerr << "error: unknown event " << c << "\n";
119 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
120 std::cerr << "warning: incomplete call " << (*it)->name() << "\n";
121 std::cerr << **it << "\n";
130 * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit.
133 T *lookup(std::vector<T *> &map, size_t index) {
134 if (index >= map.size()) {
135 map.resize(index + 1);
143 void Parser::parse_enter(void) {
144 size_t id = read_uint();
146 FunctionSig *sig = lookup(functions, id);
148 sig = new FunctionSig;
150 sig->name = read_string();
151 sig->num_args = read_uint();
152 const char **arg_names = new const char *[sig->num_args];
153 for (unsigned i = 0; i < sig->num_args; ++i) {
154 arg_names[i] = read_string();
156 sig->arg_names = arg_names;
161 Call *call = new Call(sig);
162 call->no = next_call_no++;
164 if (parse_call_details(call)) {
165 calls.push_back(call);
172 Call *Parser::parse_leave(void) {
173 unsigned call_no = read_uint();
175 for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) {
176 if ((*it)->no == call_no) {
186 if (parse_call_details(call)) {
195 bool Parser::parse_call_details(Call *call) {
199 case Trace::CALL_END:
201 case Trace::CALL_ARG:
204 case Trace::CALL_RET:
205 call->ret = parse_value();
208 std::cerr << "error: ("<<call->name()<< ") unknown call detail "
218 void Parser::parse_arg(Call *call) {
219 unsigned index = read_uint();
220 Value *value = parse_value();
221 if (index >= call->args.size()) {
222 call->args.resize(index + 1);
224 call->args[index] = value;
228 Value *Parser::parse_value(void) {
233 case Trace::TYPE_NULL:
236 case Trace::TYPE_FALSE:
237 value = new Bool(false);
239 case Trace::TYPE_TRUE:
240 value = new Bool(true);
242 case Trace::TYPE_SINT:
243 value = parse_sint();
245 case Trace::TYPE_UINT:
246 value = parse_uint();
248 case Trace::TYPE_FLOAT:
249 value = parse_float();
251 case Trace::TYPE_DOUBLE:
252 value = parse_double();
254 case Trace::TYPE_STRING:
255 value = parse_string();
257 case Trace::TYPE_ENUM:
258 value = parse_enum();
260 case Trace::TYPE_BITMASK:
261 value = parse_bitmask();
263 case Trace::TYPE_ARRAY:
264 value = parse_array();
266 case Trace::TYPE_STRUCT:
267 value = parse_struct();
269 case Trace::TYPE_BLOB:
270 value = parse_blob();
272 case Trace::TYPE_OPAQUE:
273 value = parse_opaque();
276 std::cerr << "error: unknown type " << c << "\n";
284 std::cerr << "\tVALUE " << value << "\n";
291 Value *Parser::parse_sint() {
292 return new SInt(-(signed long long)read_uint());
296 Value *Parser::parse_uint() {
297 return new UInt(read_uint());
301 Value *Parser::parse_float() {
303 file->read(&value, sizeof value);
304 return new Float(value);
308 Value *Parser::parse_double() {
310 file->read(&value, sizeof value);
311 return new Float(value);
315 Value *Parser::parse_string() {
316 return new String(read_string());
320 Value *Parser::parse_enum() {
321 size_t id = read_uint();
322 EnumSig *sig = lookup(enums, id);
326 sig->name = read_string();
327 Value *value = parse_value();
328 sig->value = value->toSInt();
333 return new Enum(sig);
337 Value *Parser::parse_bitmask() {
338 size_t id = read_uint();
339 BitmaskSig *sig = lookup(bitmasks, id);
341 sig = new BitmaskSig;
343 sig->num_flags = read_uint();
344 BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
345 for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
346 it->name = read_string();
347 it->value = read_uint();
348 if (it->value == 0 && it != flags) {
349 std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
357 unsigned long long value = read_uint();
359 return new Bitmask(sig, value);
363 Value *Parser::parse_array(void) {
364 size_t len = read_uint();
365 Array *array = new Array(len);
366 for (size_t i = 0; i < len; ++i) {
367 array->values[i] = parse_value();
373 Value *Parser::parse_blob(void) {
374 size_t size = read_uint();
375 Blob *blob = new Blob(size);
377 file->read(blob->buf, (unsigned)size);
383 Value *Parser::parse_struct() {
384 size_t id = read_uint();
386 StructSig *sig = lookup(structs, id);
390 sig->name = read_string();
391 sig->num_members = read_uint();
392 const char **member_names = new const char *[sig->num_members];
393 for (unsigned i = 0; i < sig->num_members; ++i) {
394 member_names[i] = read_string();
396 sig->member_names = member_names;
401 Struct *value = new Struct(sig);
403 for (size_t i = 0; i < sig->num_members; ++i) {
404 value->members[i] = parse_value();
411 Value *Parser::parse_opaque() {
412 unsigned long long addr;
414 return new Pointer(addr);
418 const char * Parser::read_string(void) {
419 size_t len = read_uint();
420 char * value = new char[len + 1];
422 file->read(value, (unsigned)len);
426 std::cerr << "\tSTRING \"" << value << "\"\n";
432 unsigned long long Parser::read_uint(void) {
433 unsigned long long value = 0;
441 value |= (unsigned long long)(c & 0x7f) << shift;
445 std::cerr << "\tUINT " << value << "\n";
451 inline int Parser::read_byte(void) {
452 int c = file->getc();
455 std::cerr << "\tEOF" << "\n";
457 std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n";
463 } /* namespace Trace */