]> git.proxmox.com Git - mirror_frr.git/blob - python/tiabwarfo.py
tests: Check RIP allow-ecmp an arbitrary number of paths
[mirror_frr.git] / python / tiabwarfo.py
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 # FRR DWARF structure definition extractor
3 #
4 # Copyright (C) 2020 David Lamparter for NetDEF, Inc.
5
6 import sys
7 import os
8 import subprocess
9 import re
10 import argparse
11 import json
12
13 structs = [
14 "xref",
15 "xref_logmsg",
16 "xref_threadsched",
17 "xref_install_element",
18 "xrefdata",
19 "xrefdata_logmsg",
20 "cmd_element",
21 ]
22
23
24 def extract(filename="lib/.libs/libfrr.so"):
25 """
26 Convert output from "pahole" to JSON.
27
28 Example pahole output:
29 $ pahole -C xref lib/.libs/libfrr.so
30 struct xref {
31 struct xrefdata * xrefdata; /* 0 8 */
32 enum xref_type type; /* 8 4 */
33 int line; /* 12 4 */
34 const char * file; /* 16 8 */
35 const char * func; /* 24 8 */
36
37 /* size: 32, cachelines: 1, members: 5 */
38 /* last cacheline: 32 bytes */
39 };
40 """
41 pahole = subprocess.check_output(
42 ["pahole", "-C", ",".join(structs), filename]
43 ).decode("UTF-8")
44
45 struct_re = re.compile(r"^struct ([^ ]+) \{([^\}]+)};", flags=re.M | re.S)
46 field_re = re.compile(
47 r"^\s*(?P<type>[^;\(]+)\s+(?P<name>[^;\[\]]+)(?:\[(?P<array>\d+)\])?;\s*\/\*(?P<comment>.*)\*\/\s*$"
48 )
49 comment_re = re.compile(r"^\s*\/\*.*\*\/\s*$")
50
51 pastructs = struct_re.findall(pahole)
52 out = {}
53
54 for sname, data in pastructs:
55 this = out.setdefault(sname, {})
56 fields = this.setdefault("fields", [])
57
58 lines = data.strip().splitlines()
59
60 next_offs = 0
61
62 for line in lines:
63 if line.strip() == "":
64 continue
65 m = comment_re.match(line)
66 if m is not None:
67 continue
68
69 m = field_re.match(line)
70 if m is not None:
71 offs, size = m.group("comment").strip().split()
72 offs = int(offs)
73 size = int(size)
74 typ_ = m.group("type").strip()
75 name = m.group("name")
76
77 if name.startswith("(*"):
78 # function pointer
79 typ_ = typ_ + " *"
80 name = name[2:].split(")")[0]
81
82 data = {
83 "name": name,
84 "type": typ_,
85 # 'offset': offs,
86 # 'size': size,
87 }
88 if m.group("array"):
89 data["array"] = int(m.group("array"))
90
91 fields.append(data)
92 if offs != next_offs:
93 raise ValueError(
94 "%d padding bytes before struct %s.%s"
95 % (offs - next_offs, sname, name)
96 )
97 next_offs = offs + size
98 continue
99
100 raise ValueError("cannot process line: %s" % line)
101
102 return out
103
104
105 class FieldApplicator(object):
106 """
107 Fill ELFDissectStruct fields list from pahole/JSON
108
109 Uses the JSON file created by the above code to fill in the struct fields
110 in subclasses of ELFDissectStruct.
111 """
112
113 # only what we really need. add more as needed.
114 packtypes = {
115 "int": "i",
116 "uint8_t": "B",
117 "uint16_t": "H",
118 "uint32_t": "I",
119 "char": "s",
120 }
121
122 def __init__(self, data):
123 self.data = data
124 self.classes = []
125 self.clsmap = {}
126
127 def add(self, cls):
128 self.classes.append(cls)
129 self.clsmap[cls.struct] = cls
130
131 def resolve(self, cls):
132 out = []
133 # offset = 0
134
135 fieldrename = getattr(cls, "fieldrename", {})
136
137 def mkname(n):
138 return (fieldrename.get(n, n),)
139
140 for field in self.data[cls.struct]["fields"]:
141 typs = field["type"].split()
142 typs = [i for i in typs if i not in ["const"]]
143
144 # this will break reuse of xrefstructs.json across 32bit & 64bit
145 # platforms
146
147 # if field['offset'] != offset:
148 # assert offset < field['offset']
149 # out.append(('_pad', '%ds' % (field['offset'] - offset,)))
150
151 # pretty hacky C types handling, but covers what we need
152
153 ptrlevel = 0
154 while typs[-1] == "*":
155 typs.pop(-1)
156 ptrlevel += 1
157
158 if ptrlevel > 0:
159 packtype = ("P", None)
160 if ptrlevel == 1:
161 if typs[0] == "char":
162 packtype = ("P", str)
163 elif typs[0] == "struct" and typs[1] in self.clsmap:
164 packtype = ("P", self.clsmap[typs[1]])
165 elif typs[0] == "enum":
166 packtype = ("I",)
167 elif typs[0] in self.packtypes:
168 packtype = (self.packtypes[typs[0]],)
169 elif typs[0] == "struct":
170 if typs[1] in self.clsmap:
171 packtype = (self.clsmap[typs[1]],)
172 else:
173 raise ValueError(
174 "embedded struct %s not in extracted data" % (typs[1],)
175 )
176 else:
177 raise ValueError(
178 "cannot decode field %s in struct %s (%s)"
179 % (cls.struct, field["name"], field["type"])
180 )
181
182 if "array" in field and typs[0] == "char":
183 packtype = ("%ds" % field["array"],)
184 out.append(mkname(field["name"]) + packtype)
185 elif "array" in field:
186 for i in range(0, field["array"]):
187 out.append(mkname("%s_%d" % (field["name"], i)) + packtype)
188 else:
189 out.append(mkname(field["name"]) + packtype)
190
191 # offset = field['offset'] + field['size']
192
193 cls.fields = out
194
195 def __call__(self):
196 for cls in self.classes:
197 self.resolve(cls)
198
199
200 def main():
201 argp = argparse.ArgumentParser(description="FRR DWARF structure extractor")
202 argp.add_argument(
203 "-o",
204 dest="output",
205 type=str,
206 help="write JSON output",
207 default="python/xrefstructs.json",
208 )
209 argp.add_argument(
210 "-i",
211 dest="input",
212 type=str,
213 help="ELF file to read",
214 default="lib/.libs/libfrr.so",
215 )
216 args = argp.parse_args()
217
218 out = extract(args.input)
219 with open(args.output + ".tmp", "w") as fd:
220 json.dump(out, fd, indent=2, sort_keys=True)
221 os.rename(args.output + ".tmp", args.output)
222
223
224 if __name__ == "__main__":
225 main()