X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_parser.hpp;h=49e89087601b3148cc06ec940f6310290d4349f1;hb=1d31b6c50225d78792de17a6b2fbc75f7ea406f1;hp=dc7b8d672abc1af80d79d2959863897cbc37f93b;hpb=6f51d3b71cd318f8337e7f5fe791867fc2713a5b;p=apitrace diff --git a/trace_parser.hpp b/trace_parser.hpp index dc7b8d6..49e8908 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -27,11 +27,9 @@ #define _TRACE_PARSER_HPP_ -#include - #include - -#include +#include +#include #include "trace_format.hpp" #include "trace_model.hpp" @@ -43,259 +41,76 @@ namespace Trace { class Parser { protected: - gzFile file; + void *file; + + typedef std::list CallList; + CallList calls; + + typedef std::vector FunctionMap; + FunctionMap functions; + + typedef std::vector StructMap; + StructMap structs; + + typedef std::vector EnumMap; + EnumMap enums; + + typedef std::vector BitmaskMap; + BitmaskMap bitmasks; + + unsigned next_call_no; + public: - Parser() { - file = NULL; - } - - ~Parser() { - close(); - } - - bool open(const char *filename) { - unsigned long long version; - - file = gzopen(filename, "rb"); - if (!file) { - return false; - } - - version = read_uint(); - if (version != TRACE_VERSION) { - std::cerr << "error: unsupported format version" << version << "\n"; - return false; - } - - return true; - } - - void close(void) { - if (file) { - gzclose(file); - file = NULL; - } - } - - Call *parse_call(void) { - Call *call = new Call; - call->name = read_string(); - do { - int c = read_byte(); - switch(c) { - case Trace::CALL_END: - return call; - case Trace::CALL_ARG: - call->args.push_back(parse_arg()); - break; - case Trace::CALL_RET: - call->ret = parse_value(); - break; - default: - std::cerr << "error: unknown call detail " << c << "\n"; - assert(0); - /* fallthrough */ - case -1: - delete call; - return NULL; - } - } while(true); - } - - Arg parse_arg(void) { - std::string name = read_string(); - Value *value = parse_value(); - return Arg(name, value); - } - - Value *parse_value(void) { - int c; - c = read_byte(); - switch(c) { - case Trace::TYPE_NULL: - return new Null; - case Trace::TYPE_FALSE: - return new Bool(false); - case Trace::TYPE_TRUE: - return new Bool(true); - case Trace::TYPE_SINT: - return parse_sint(); - case Trace::TYPE_UINT: - return parse_uint(); - case Trace::TYPE_FLOAT: - return parse_float(); - case Trace::TYPE_DOUBLE: - return parse_double(); - case Trace::TYPE_STRING: - return parse_string(); - case Trace::TYPE_CONST: - return parse_const(); - case Trace::TYPE_BITMASK: - return parse_bitmask(); - case Trace::TYPE_ARRAY: - return parse_array(); - case Trace::TYPE_STRUCT: - return parse_struct(); - case Trace::TYPE_BLOB: - return parse_blob(); - case Trace::TYPE_POINTER: - return parse_pointer(); - case Trace::TYPE_OPAQUE: - return parse_opaque(); - default: - std::cerr << "error: unknown type " << c << "\n"; - assert(0); - return NULL; - } - } - - Value *parse_sint() { - return new SInt(-read_uint()); - } - - Value *parse_uint() { - return new UInt(read_uint()); - } - - Value *parse_float() { - float value; - gzread(file, &value, sizeof value); - return new Float(value); - } - - Value *parse_double() { - double value; - gzread(file, &value, sizeof value); - return new Float(value); - } - - Value *parse_string() { - return new String(read_string()); - } - - Value *parse_const() { - std::string name = read_string(); - Value *value = parse_value(); - return new Const(name, value); - } - - Value *parse_bitmask() { - unsigned long long value = 0; - int c; - do { - c = read_byte(); - switch(c) { - case Trace::TYPE_SINT: - value |= -read_uint(); - break; - case Trace::TYPE_UINT: - value |= read_uint(); - break; - case Trace::TYPE_CONST: - read_string(); - break; - case Trace::TYPE_NULL: - goto done; - default: - std::cerr << "error: uexpected type " << c << "\n"; - assert(0); - return NULL; - } - } while(true); -done: - return new UInt(value); - } - - Value *parse_array(void) { - size_t len = read_uint(); - Array *array = new Array(len); - for (size_t i = 0; i < len; ++i) { - array->values[i] = parse_value(); - } - return array; - } - - Value *parse_blob(void) { - size_t size = read_uint(); - Blob *blob = new Blob(size); - if (size) { - gzread(file, blob->buf, size); - } - return blob; - } - - Value *parse_pointer() { - unsigned long long addr; - Value *value; - addr = read_uint(); - value = parse_value(); - if (!value) - value = new UInt(addr); - return value; - } - - Value *parse_struct() { - std::string name; - /* XXX */ - name = read_string(); - while(name.length()) { - Value *value = parse_value(); - std::cout << " " << name << " = " << value << "\n"; - name = read_string(); - } - return NULL; - } - - Value *parse_opaque() { - unsigned long long addr; - addr = read_uint(); - /* XXX */ - return new UInt(addr); - } - - std::string read_string(void) { - size_t len = read_uint(); - if (!len) { - return std::string(); - } - char * buf = new char[len]; - gzread(file, buf, len); - std::string value(buf, len); - delete [] buf; -#ifdef TRACE_VERBOSE - std::cerr << '"' << value << '"' << "\n"; -#endif - return value; - } - - unsigned long long read_uint(void) { - unsigned long long value = 0; - int c; - unsigned shift = 0; - do { - c = gzgetc(file); - if (c == -1) { - break; - } - value |= (unsigned long long)(c & 0x7f) << shift; - shift += 7; - } while(c & 0x80); -#ifdef TRACE_VERBOSE - std::cerr << value << "\n"; -#endif - return value; - } - - int read_byte(void) { - int c = gzgetc(file); -#ifdef TRACE_VERBOSE - if (c < 0) - std::cerr << "EOF" << "\n"; - else - std::cerr << "0x" << std::hex << c << "\n"; -#endif - return c; - } + unsigned long long version; + + Parser(); + + ~Parser(); + + bool open(const char *filename); + + void close(void); + + Call *parse_call(void); + +protected: + void parse_enter(void); + + Call *parse_leave(void); + + void parse_call_details(Call *call); + + void parse_arg(Call *call); + + Value *parse_value(void); + + Value *parse_sint(); + + Value *parse_uint(); + + Value *parse_float(); + + Value *parse_double(); + + Value *parse_string(); + + Value *parse_enum(); + + Value *parse_bitmask(); + + Value *parse_array(void); + + Value *parse_blob(void); + + Value *parse_struct(); + + Value *parse_opaque(); + + std::string read_string(void); + + unsigned long long read_uint(void); + + inline int read_byte(void); };