]> git.ipfire.org Git - thirdparty/bird.git/blame - bird-gdb.py
Filter: Some people can't pronounce "postfixify" correctly. Let's try "linearize...
[thirdparty/bird.git] / bird-gdb.py
CommitLineData
71365879
MM
1class 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
15class 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_IP": "ip",
34 "T_NET": "net",
35 "T_STRING": "s",
36 "T_PATH_MASK": "path_mask",
37 "T_PATH": "ad",
38 "T_CLIST": "ad",
39 "T_EC": "ec",
40 "T_ECLIST": "ad",
41 "T_LC": "lc",
42 "T_LCLIST": "ad",
43 "T_RD": "ec",
44 "T_PATH_MASK_ITEM": "pmi",
45 "T_SET": "t",
46 "T_PREFIX_SET": "ti",
47 }
48
49 def to_string(self):
50 code = self.val['type']
51 if code.type.code != gdb.TYPE_CODE_ENUM or code.type.tag != "f_type":
52 raise Exception("Strange 'type' element in f_val")
53
54 if str(code) == "T_VOID":
55 return "T_VOID"
56 else:
57 return "(%(c)s) %(v)s" % { "c": code, "v": self.val['val'][self.codemap[str(code)]] }
58
59 def display_hint(self):
60 return "map"
61
d4bf7481
MM
62class BIRDFValStackPrinter(BIRDPrinter):
63 "Print BIRD's struct f_val_stack"
64 typeCode = gdb.TYPE_CODE_STRUCT
65 typeTag = "f_val_stack"
66
67 def to_string(self):
68 cnt = self.val['cnt']
69 return ("Value stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
70 "\n\t".join([ (".val[%(n) 3d] = " % { "n": n}) + str(self.val['val'][n]) for n in range(cnt-1, -1, -1) ])
71
72 def display_hint(self):
73 return "map"
74
71365879
MM
75class BIRDFInstPrinter(BIRDPrinter):
76 "Print BIRD's struct f_inst"
77 typeCode = gdb.TYPE_CODE_STRUCT
78 typeTag = "f_inst"
79
80 def to_string(self):
81 code = self.val['fi_code']
82 if str(code) == "FI_NOP":
83 return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
d4bf7481
MM
84 return "%(code)s:\t%(lineno) 6dL\t%(size)6dS\tnext = %(next)s: .i_%(code)s = %(union)s" % {
85 "code": str(code),
86 "lineno": self.val['lineno'],
87 "size": self.val['size'],
88 "next": str(self.val['next']),
89 "union": str(self.val['i_' + str(code)])
90 }
71365879
MM
91
92# def children(self): # children iterator
93 def display_hint(self):
94 return "map"
95
d4bf7481
MM
96class BIRDFLineItemPrinter(BIRDPrinter):
97 "Print BIRD's struct f_line_item"
98 typeCode = gdb.TYPE_CODE_STRUCT
99 typeTag = "f_line_item"
100
101 def to_string(self):
102 code = self.val['fi_code']
103 if str(code) == "FI_NOP":
104 return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
105 return "%(code)s:\t%(lineno) 6dL\t%(flags)2dF: .i_%(code)s = %(union)s" % {
106 "code": str(code),
107 "lineno": self.val['lineno'],
108 "flags": self.val['flags'],
109 "union": str(self.val['i_' + str(code)])
110 }
111
112class BIRDFLinePrinter(BIRDPrinter):
113 "Print BIRD's struct f_line"
114 typeCode = gdb.TYPE_CODE_STRUCT
115 typeTag = "f_line"
116
117 def to_string(self):
118 cnt = self.val['len']
119 return ("FLine (%(cnt)d, args=%(args)d): " % { "cnt": cnt, "args" : self.val['args'] } + \
120 ", ".join([
121 ".items[%(n) 3d] = %(code)s" % {
122 "n": n,
123 "code": str(self.val['items'][n]['fi_code']),
124 } if n % 8 == 0 else str(self.val['items'][n]['fi_code']) for n in range(cnt)]))
125
126
127class BIRDFExecStackPrinter(BIRDPrinter):
128 "Print BIRD's struct f_exec_stack"
129 typeCode = gdb.TYPE_CODE_STRUCT
130 typeTag = "f_exec_stack"
131
132 def to_string(self):
133 cnt = self.val['cnt']
134 return ("Exec stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
135 "\n\t".join([ ".item[%(n) 3d] = %(retflag)d V%(ventry) 3d P%(pos) 4d %(line)s" % {
136 "retflag": self.val['item'][n]['emask'],
137 "ventry": self.val['item'][n]['ventry'],
138 "pos": self.val['item'][n]['pos'],
139 "line": str(self.val['item'][n]['line'].dereference()),
140 "n": n
141 } for n in range(cnt-1, -1, -1) ])
71365879
MM
142
143def register_printers(objfile):
144 objfile.pretty_printers.append(BIRDFInstPrinter.lookup)
145 objfile.pretty_printers.append(BIRDFValPrinter.lookup)
d4bf7481
MM
146 objfile.pretty_printers.append(BIRDFValStackPrinter.lookup)
147 objfile.pretty_printers.append(BIRDFLineItemPrinter.lookup)
148 objfile.pretty_printers.append(BIRDFLinePrinter.lookup)
149 objfile.pretty_printers.append(BIRDFExecStackPrinter.lookup)
71365879
MM
150
151register_printers(gdb.current_objfile())
152
153print("BIRD pretty printers loaded OK.")