X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_parser.hpp;h=3aaa6d3a93f16cb71c85116185cee0d4d9cba14e;hb=9d50fbb84f9b3086aa8e985e32534961336563b2;hp=565ccaa8f830d7cce0bd806755ef1cec50ffc3f9;hpb=b1887f9c55f7017b795bfc4b2d9a70e45e5f1e77;p=apitrace diff --git a/trace_parser.hpp b/trace_parser.hpp index 565ccaa..3aaa6d3 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -27,12 +27,10 @@ #define _TRACE_PARSER_HPP_ -#include - #include +#include -#include - +#include "trace_file.hpp" #include "trace_format.hpp" #include "trace_model.hpp" @@ -40,224 +38,157 @@ namespace Trace { +struct ParseBookmark +{ + File::Offset offset; + unsigned next_call_no; +}; + + class Parser { protected: - gzFile file; + File *file; + + enum Mode { + FULL = 0, + SCAN, + SKIP + }; + + typedef std::list CallList; + CallList calls; + + // Helper template that extends a base signature structure, with additional + // parsing information. + template< class T > + struct SigState : public T { + // Offset in the file of where signature was defined. It is used when + // reparsing to determine whether the signature definition is to be + // expected next or not. + File::Offset offset; + }; + + typedef SigState FunctionSigState; + typedef SigState StructSigState; + typedef SigState EnumSigState; + typedef SigState BitmaskSigState; + + typedef std::vector FunctionMap; + typedef std::vector StructMap; + typedef std::vector EnumMap; + typedef std::vector BitmaskMap; + + FunctionMap functions; + StructMap structs; + EnumMap enums; + BitmaskMap bitmasks; + + unsigned next_call_no; + public: - Parser() { - file = NULL; - } - - bool parse(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; - } - - while (!gzeof(file)) { - parse_call(); - } - - return true; - } - - void parse_call(void) { - Call call; - call.name = read_string(); - int c; - do { - c = gzgetc(file); - if (c == Trace::CALL_END || c == -1) { - break; - } - switch(c) { - case Trace::CALL_END: - return; - 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); - break; - } - } while(true); - handle_call(call); - } - - virtual void handle_call(Call &call) { - std::cout << call; - } - - Arg parse_arg(void) { - std::string name = read_string(); - Value *value = parse_value(); - return Arg(name, value); - } - - Value *parse_value(void) { - int c; - c = gzgetc(file); - switch(c) { - 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_BLOB: - return parse_blob(); - case Trace::TYPE_POINTER: - return parse_pointer(); - case Trace::TYPE_NULL: - return new Null; - default: - std::cerr << "error: unknown type " << c << "\n"; - assert(0); - return NULL; - } - } - - Value *parse_bool() { - int c; - c = gzgetc(file); - return new Bool(c); - } - - 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 = gzgetc(file); - 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); + unsigned long long version; + + Parser(); + + ~Parser(); + + bool open(const char *filename); + + void close(void); + + Call *parse_call(void) { + return parse_call(FULL); + } + + bool supportsOffsets() const + { + return file->supportsOffsets(); + } + + void getBookmark(ParseBookmark &bookmark); + + void setBookmark(const ParseBookmark &bookmark); + + int percentRead() + { + return file->percentRead(); + } + + Call *scan_call() { + return parse_call(SCAN); + } + +protected: + Call *parse_call(Mode mode); + + FunctionSig *parse_function_sig(void); + StructSig *parse_struct_sig(); + EnumSig *parse_enum_sig(); + BitmaskSig *parse_bitmask_sig(); + + Call *parse_Call(Mode mode); + + void parse_enter(Mode mode); + + Call *parse_leave(Mode mode); + + bool parse_call_details(Call *call, Mode mode); + + void parse_arg(Call *call, Mode mode); + + Value *parse_value(void); + void scan_value(void); + inline Value *parse_value(Mode mode) { + if (mode == FULL) { + return parse_value(); + } else { + scan_value(); 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; - } - - std::string read_string(void) { - size_t len = read_uint(); - char * buf = new char[len]; - gzread(file, buf, len); - std::string value(buf, len); - delete [] buf; - 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); - return value; - } + } + } + + Value *parse_sint(); + void scan_sint(); + + Value *parse_uint(); + void scan_uint(); + + Value *parse_float(); + void scan_float(); + + Value *parse_double(); + void scan_double(); + + Value *parse_string(); + void scan_string(); + + Value *parse_enum(); + void scan_enum(); + + Value *parse_bitmask(); + void scan_bitmask(); + + Value *parse_array(void); + void scan_array(void); + + Value *parse_blob(void); + void scan_blob(void); + + Value *parse_struct(); + void scan_struct(); + + Value *parse_opaque(); + void scan_opaque(); + + const char * read_string(void); + void skip_string(void); + + unsigned long long read_uint(void); + void skip_uint(void); + + inline int read_byte(void); + inline void skip_byte(void); };