]> git.ipfire.org Git - thirdparty/bird.git/blob - bird-gdb.py
Formalized our contribution policy which we're currently applying
[thirdparty/bird.git] / bird-gdb.py
1 class BIRDPrinter:
2 def __init__(self, val):
3 self.val = val
4
5 @classmethod
6 def lookup(cls, val):
7 if val.type.code != cls.typeCode:
8 return None
9 if val.type.tag != cls.typeTag:
10 return None
11
12 return cls(val)
13
14
15 class BIRDFValPrinter(BIRDPrinter):
16 "Print BIRD\s struct f_val"
17 typeCode = gdb.TYPE_CODE_STRUCT
18 typeTag = "f_val"
19
20 codemap = {
21 "T_INT": "i",
22 "T_BOOL": "i",
23 "T_PAIR": "i",
24 "T_QUAD": "i",
25 "T_ENUM_RTS": "i",
26 "T_ENUM_BGP_ORIGIN": "i",
27 "T_ENUM_SCOPE": "i",
28 "T_ENUM_RTC": "i",
29 "T_ENUM_RTD": "i",
30 "T_ENUM_ROA": "i",
31 "T_ENUM_NETTYPE": "i",
32 "T_ENUM_RA_PREFERENCE": "i",
33 "T_ENUM_AF": "i",
34 "T_IP": "ip",
35 "T_NET": "net",
36 "T_STRING": "s",
37 "T_BYTESTRING": "bs",
38 "T_PATH_MASK": "path_mask",
39 "T_PATH": "ad",
40 "T_CLIST": "ad",
41 "T_EC": "ec",
42 "T_ECLIST": "ad",
43 "T_LC": "lc",
44 "T_LCLIST": "ad",
45 "T_RD": "ec",
46 "T_PATH_MASK_ITEM": "pmi",
47 "T_SET": "t",
48 "T_PREFIX_SET": "ti",
49 }
50
51 def to_string(self):
52 code = self.val['type']
53 if code.type.code != gdb.TYPE_CODE_ENUM or code.type.tag != "f_type":
54 raise Exception("Strange 'type' element in f_val")
55
56 if str(code) == "T_VOID":
57 return "T_VOID"
58 else:
59 return "(%(c)s) %(v)s" % { "c": code, "v": self.val['val'][self.codemap[str(code)]] }
60
61 def display_hint(self):
62 return "map"
63
64 class BIRDFValStackPrinter(BIRDPrinter):
65 "Print BIRD's struct f_val_stack"
66 typeCode = gdb.TYPE_CODE_STRUCT
67 typeTag = "f_val_stack"
68
69 def to_string(self):
70 cnt = self.val['cnt']
71 return ("Value stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
72 "\n\t".join([ (".val[%(n) 3d] = " % { "n": n}) + str(self.val['val'][n]) for n in range(cnt-1, -1, -1) ])
73
74 def display_hint(self):
75 return "map"
76
77 class BIRDFInstPrinter(BIRDPrinter):
78 "Print BIRD's struct f_inst"
79 typeCode = gdb.TYPE_CODE_STRUCT
80 typeTag = "f_inst"
81
82 def to_string(self):
83 code = self.val['fi_code']
84 if str(code) == "FI_NOP":
85 return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
86 return "%(code)s:\t%(lineno) 6dL\t%(size)6dS\tnext = %(next)s: .i_%(code)s = %(union)s" % {
87 "code": str(code),
88 "lineno": self.val['lineno'],
89 "size": self.val['size'],
90 "next": str(self.val['next']),
91 "union": str(self.val['i_' + str(code)])
92 }
93
94 # def children(self): # children iterator
95 def display_hint(self):
96 return "map"
97
98 class BIRDFLineItemPrinter(BIRDPrinter):
99 "Print BIRD's struct f_line_item"
100 typeCode = gdb.TYPE_CODE_STRUCT
101 typeTag = "f_line_item"
102
103 def to_string(self):
104 code = self.val['fi_code']
105 if str(code) == "FI_NOP":
106 return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
107 return "%(code)s:\t%(lineno) 6dL\t%(flags)2dF: .i_%(code)s = %(union)s" % {
108 "code": str(code),
109 "lineno": self.val['lineno'],
110 "flags": self.val['flags'],
111 "union": str(self.val['i_' + str(code)])
112 }
113
114 class BIRDFLinePrinter(BIRDPrinter):
115 "Print BIRD's struct f_line"
116 typeCode = gdb.TYPE_CODE_STRUCT
117 typeTag = "f_line"
118
119 def to_string(self):
120 cnt = self.val['len']
121 return ("FLine (%(cnt)d, args=%(args)d): " % { "cnt": cnt, "args" : self.val['args'] } + \
122 ", ".join([
123 ".items[%(n) 3d] = %(code)s" % {
124 "n": n,
125 "code": str(self.val['items'][n]['fi_code']),
126 } if n % 8 == 0 else str(self.val['items'][n]['fi_code']) for n in range(cnt)]))
127
128
129 class BIRDFExecStackPrinter(BIRDPrinter):
130 "Print BIRD's struct f_exec_stack"
131 typeCode = gdb.TYPE_CODE_STRUCT
132 typeTag = "f_exec_stack"
133
134 def to_string(self):
135 cnt = self.val['cnt']
136 return ("Exec stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
137 "\n\t".join([ ".item[%(n) 3d] = %(retflag)d V%(ventry) 3d P%(pos) 4d %(line)s" % {
138 "retflag": self.val['item'][n]['emask'],
139 "ventry": self.val['item'][n]['ventry'],
140 "pos": self.val['item'][n]['pos'],
141 "line": str(self.val['item'][n]['line'].dereference()),
142 "n": n
143 } for n in range(cnt-1, -1, -1) ])
144
145 def register_printers(objfile):
146 objfile.pretty_printers.append(BIRDFInstPrinter.lookup)
147 objfile.pretty_printers.append(BIRDFValPrinter.lookup)
148 objfile.pretty_printers.append(BIRDFValStackPrinter.lookup)
149 objfile.pretty_printers.append(BIRDFLineItemPrinter.lookup)
150 objfile.pretty_printers.append(BIRDFLinePrinter.lookup)
151 objfile.pretty_printers.append(BIRDFExecStackPrinter.lookup)
152
153 register_printers(gdb.current_objfile())
154
155 print("BIRD pretty printers loaded OK.")