]> git.puffer.fish Git - matthieu/frr.git/commitdiff
python/xrelfo: cross-platform xrefstructs.json
authorDavid Lamparter <equinox@diac24.net>
Sun, 14 Feb 2021 00:36:09 +0000 (01:36 +0100)
committerDavid Lamparter <equinox@diac24.net>
Tue, 23 Feb 2021 15:56:58 +0000 (16:56 +0100)
Just get names, types and order from pahole; ditch offset & size since
they're different on 32/64 bit.  None of the structs has padding
currently;  if we really need that it can be implemented in the future.

(Padding will raise an exception, so it won't break silently.)

Signed-off-by: David Lamparter <equinox@diac24.net>
python/tiabwarfo.py
python/xrefstructs.json

index bddbeef26839ecd8811a622c38ea07bb6a4c4fc5..265173e314f9d8723af99335b4e5564e46c63241 100644 (file)
@@ -52,12 +52,14 @@ def extract(filename='lib/.libs/libfrr.so'):
     pastructs = struct_re.findall(pahole)
     out = {}
 
-    for name, data in pastructs:
-        this = out.setdefault(name, {})
+    for sname, data in pastructs:
+        this = out.setdefault(sname, {})
         fields = this.setdefault('fields', [])
 
         lines = data.strip().splitlines()
 
+        next_offs = 0
+
         for line in lines:
             if line.strip() == '':
                 continue
@@ -81,13 +83,16 @@ def extract(filename='lib/.libs/libfrr.so'):
                 data = {
                     'name': name,
                     'type': typ_,
-                    'offset': offs,
-                    'size': size,
+                #   'offset': offs,
+                #   'size': size,
                 }
                 if m.group('array'):
                     data['array'] = int(m.group('array'))
 
                 fields.append(data)
+                if offs != next_offs:
+                    raise ValueError('%d padding bytes before struct %s.%s' % (offs - next_offs, sname, name))
+                next_offs = offs + size
                 continue
 
             raise ValueError('cannot process line: %s' % line)
@@ -122,7 +127,7 @@ class FieldApplicator(object):
 
     def resolve(self, cls):
         out = []
-        offset = 0
+        #offset = 0
 
         fieldrename = getattr(cls, 'fieldrename', {})
         def mkname(n):
@@ -132,9 +137,12 @@ class FieldApplicator(object):
             typs = field['type'].split()
             typs = [i for i in typs if i not in ['const']]
 
-            if field['offset'] != offset:
-                assert offset < field['offset']
-                out.append(('_pad', '%ds' % (field['offset'] - offset,)))
+            # this will break reuse of xrefstructs.json across 32bit & 64bit
+            # platforms
+
+            #if field['offset'] != offset:
+            #    assert offset < field['offset']
+            #    out.append(('_pad', '%ds' % (field['offset'] - offset,)))
 
             # pretty hacky C types handling, but covers what we need
 
@@ -158,7 +166,7 @@ class FieldApplicator(object):
                 if typs[1] in self.clsmap:
                     packtype = (self.clsmap[typs[1]],)
                 else:
-                    packtype = ('%ds' % field['size'],)
+                    raise ValueError('embedded struct %s not in extracted data' % (typs[1],))
             else:
                 raise ValueError('cannot decode field %s in struct %s (%s)' % (
                         cls.struct, field['name'], field['type']))
@@ -172,7 +180,7 @@ class FieldApplicator(object):
             else:
                 out.append(mkname(field['name']) + packtype)
 
-            offset = field['offset'] + field['size']
+            #offset = field['offset'] + field['size']
 
         cls.fields = out
 
index e62c351bc9ff7bbd082d2fc54e98c95551d5cd0d..25c48c9d564c8e3943edde984e6ce0e3e1ab72eb 100644 (file)
@@ -3,44 +3,30 @@
     "fields": [
       {
         "name": "string",
-        "offset": 0,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "doc",
-        "offset": 8,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "daemon",
-        "offset": 16,
-        "size": 4,
         "type": "int"
       },
       {
         "name": "attr",
-        "offset": 20,
-        "size": 1,
-        "type": "uint8_t"
+        "type": "uint32_t"
       },
       {
         "name": "func",
-        "offset": 24,
-        "size": 8,
         "type": "int *"
       },
       {
         "name": "name",
-        "offset": 32,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "xref",
-        "offset": 40,
-        "size": 32,
         "type": "struct xref"
       }
     ]
     "fields": [
       {
         "name": "xrefdata",
-        "offset": 0,
-        "size": 8,
         "type": "struct xrefdata *"
       },
       {
         "name": "type",
-        "offset": 8,
-        "size": 4,
         "type": "enum xref_type"
       },
       {
         "name": "line",
-        "offset": 12,
-        "size": 4,
         "type": "int"
       },
       {
         "name": "file",
-        "offset": 16,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "func",
-        "offset": 24,
-        "size": 8,
         "type": "const char  *"
       }
     ]
     "fields": [
       {
         "name": "xref",
-        "offset": 0,
-        "size": 32,
         "type": "struct xref"
       },
       {
         "name": "cmd_element",
-        "offset": 32,
-        "size": 8,
         "type": "const struct cmd_element  *"
       },
       {
         "name": "node_type",
-        "offset": 40,
-        "size": 4,
         "type": "enum node_type"
       }
     ]
     "fields": [
       {
         "name": "xref",
-        "offset": 0,
-        "size": 32,
         "type": "struct xref"
       },
       {
         "name": "fmtstring",
-        "offset": 32,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "priority",
-        "offset": 40,
-        "size": 4,
         "type": "uint32_t"
       },
       {
         "name": "ec",
-        "offset": 44,
-        "size": 4,
         "type": "uint32_t"
       },
       {
         "name": "args",
-        "offset": 48,
-        "size": 8,
         "type": "const char  *"
       }
     ]
     "fields": [
       {
         "name": "xref",
-        "offset": 0,
-        "size": 32,
         "type": "struct xref"
       },
       {
         "name": "funcname",
-        "offset": 32,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "dest",
-        "offset": 40,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "name": "thread_type",
-        "offset": 48,
-        "size": 4,
         "type": "uint32_t"
       }
     ]
     "fields": [
       {
         "name": "xref",
-        "offset": 0,
-        "size": 8,
         "type": "const struct xref  *"
       },
       {
         "array": 16,
         "name": "uid",
-        "offset": 8,
-        "size": 16,
         "type": "char"
       },
       {
         "name": "hashstr",
-        "offset": 24,
-        "size": 8,
         "type": "const char  *"
       },
       {
         "array": 2,
         "name": "hashu32",
-        "offset": 32,
-        "size": 8,
         "type": "uint32_t"
       }
     ]