]> git.cworth.org Git - apitrace/blobdiff - trace_parser.cpp
First working implementation of on-demand-loading of frames/calls.
[apitrace] / trace_parser.cpp
index 44d1786ac207fe2fa576a88c9cfae90339013fdc..abed4f51f37cab0543e0183b3fd97655ec9043a2 100644 (file)
@@ -146,22 +146,41 @@ void Parser::parse_enter(void) {
     size_t id = read_uint();
 
     FunctionSig *sig = lookup(functions, id);
-    if (!sig) {
-        sig = new FunctionSig;
-        sig->id = id;
-        sig->name = read_string();
-        sig->num_args = read_uint();
-        const char **arg_names = new const char *[sig->num_args];
-        for (unsigned i = 0; i < sig->num_args; ++i) {
-            arg_names[i] = read_string();
+    const File::Offset offset = file->currentOffset();
+    bool callWithSig = callWithSignature(offset);
+    if (!sig || callWithSig) {
+        if (!sig) {
+            sig = new FunctionSig;
+            sig->id = id;
+            sig->name = read_string();
+            sig->num_args = read_uint();
+            const char **arg_names = new const char *[sig->num_args];
+            for (unsigned i = 0; i < sig->num_args; ++i) {
+                arg_names[i] = read_string();
+            }
+            sig->arg_names = arg_names;
+            functions[id] = sig;
+            m_callSigOffsets.insert(offset);
+        } else {
+            /* skip over the signature */
+            read_string(); /* name */
+            int num_args = read_uint();
+            for (unsigned i = 0; i < num_args; ++i) {
+                 read_string(); /*arg_name*/
+            }
         }
-        sig->arg_names = arg_names;
-        functions[id] = sig;
     }
     assert(sig);
 
     Call *call = new Call(sig);
-    call->no = next_call_no++;
+
+    if (hasCallBeenParsed(offset)) {
+        call->no = callNumForOffset(offset);
+    } else {
+        call->no = next_call_no++;
+        m_callNumOffsets.insert(
+                    std::pair<File::Offset, unsigned>(offset, call->no));
+    }
 
     if (parse_call_details(call)) {
         calls.push_back(call);
@@ -322,14 +341,23 @@ Value *Parser::parse_string() {
 Value *Parser::parse_enum() {
     size_t id = read_uint();
     EnumSig *sig = lookup(enums, id);
-    if (!sig) {
-        sig = new EnumSig;
-        sig->id = id;
-        sig->name = read_string();
-        Value *value = parse_value();
-        sig->value = value->toSInt();
-        delete value;
-        enums[id] = sig;
+    const File::Offset offset = file->currentOffset();
+    bool enumWithSig = enumWithSignature(offset);
+    if (!sig || enumWithSig) {
+        if (!sig) {
+            sig = new EnumSig;
+            sig->id = id;
+            sig->name = read_string();
+            Value *value = parse_value();
+            sig->value = value->toSInt();
+            delete value;
+            enums[id] = sig;
+            m_enumSigOffsets.insert(offset);
+        } else {
+            read_string(); /*name*/
+            Value *value = parse_value();
+            delete value;
+        }
     }
     assert(sig);
     return new Enum(sig);
@@ -339,20 +367,31 @@ Value *Parser::parse_enum() {
 Value *Parser::parse_bitmask() {
     size_t id = read_uint();
     BitmaskSig *sig = lookup(bitmasks, id);
-    if (!sig) {
-        sig = new BitmaskSig;
-        sig->id = id;
-        sig->num_flags = read_uint();
-        BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
-        for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
-            it->name = read_string();
-            it->value = read_uint();
-            if (it->value == 0 && it != flags) {
-                std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
+    const File::Offset offset = file->currentOffset();
+    bool bitmaskWithSig = bitmaskWithSignature(offset);
+    if (!sig || bitmaskWithSig) {
+        if (!sig) {
+            sig = new BitmaskSig;
+            sig->id = id;
+            sig->num_flags = read_uint();
+            BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
+            for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
+                it->name = read_string();
+                it->value = read_uint();
+                if (it->value == 0 && it != flags) {
+                    std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
+                }
+            }
+            sig->flags = flags;
+            bitmasks[id] = sig;
+            m_bitmaskSigOffsets.insert(offset);
+        } else {
+            int num_flags = read_uint();
+            for (int i = 0; i < num_flags; ++i) {
+                read_string(); /*name */
+                read_uint(); /* value */
             }
         }
-        sig->flags = flags;
-        bitmasks[id] = sig;
     }
     assert(sig);
 
@@ -386,17 +425,28 @@ Value *Parser::parse_struct() {
     size_t id = read_uint();
 
     StructSig *sig = lookup(structs, id);
-    if (!sig) {
-        sig = new StructSig;
-        sig->id = id;
-        sig->name = read_string();
-        sig->num_members = read_uint();
-        const char **member_names = new const char *[sig->num_members];
-        for (unsigned i = 0; i < sig->num_members; ++i) {
-            member_names[i] = read_string();
+    const File::Offset offset = file->currentOffset();
+    bool structWithSig = structWithSignature(offset);
+    if (!sig || structWithSig) {
+        if (!sig) {
+            sig = new StructSig;
+            sig->id = id;
+            sig->name = read_string();
+            sig->num_members = read_uint();
+            const char **member_names = new const char *[sig->num_members];
+            for (unsigned i = 0; i < sig->num_members; ++i) {
+                member_names[i] = read_string();
+            }
+            sig->member_names = member_names;
+            structs[id] = sig;
+            m_structSigOffsets.insert(offset);
+        } else {
+            read_string(); /* name */
+            unsigned num_members = read_uint();
+            for (unsigned i = 0; i < num_members; ++i) {
+                read_string(); /* member_name */
+            }
         }
-        sig->member_names = member_names;
-        structs[id] = sig;
     }
     assert(sig);
 
@@ -462,4 +512,36 @@ inline int Parser::read_byte(void) {
 }
 
 
+inline bool Parser::callWithSignature(const File::Offset &offset) const
+{
+    return m_callSigOffsets.find(offset) != m_callSigOffsets.end();
+}
+
+inline bool Parser::structWithSignature(const File::Offset &offset) const
+{
+    return m_structSigOffsets.find(offset) != m_structSigOffsets.end();
+}
+
+inline bool Parser::enumWithSignature(const File::Offset &offset) const
+{
+    return m_enumSigOffsets.find(offset) != m_enumSigOffsets.end();
+}
+
+inline bool Parser::bitmaskWithSignature(const File::Offset &offset) const
+{
+    return m_bitmaskSigOffsets.find(offset) != m_bitmaskSigOffsets.end();
+}
+
+bool Parser::hasCallBeenParsed(const File::Offset &offset) const
+{
+    return m_callNumOffsets.find(offset) != m_callNumOffsets.end();
+}
+
+unsigned Parser::callNumForOffset(const File::Offset &offset) const
+{
+    CallNumOffsets::const_iterator itr = m_callNumOffsets.find(offset);
+    assert(itr != m_callNumOffsets.end());
+    return itr->second;
+}
+
 } /* namespace Trace */