blob: b703808b48e77c813e8dc7d5b53008f0c7484a10 [file] [log] [blame]
Paul Jakma5bcbc3f2014-10-29 10:33:17 +00001# GDB macros for use with Quagga.
2#
3# Macros in this file are not daemon specific. E.g., OS or Quagga library
4# APIs.
5#
6# The macro file can be loaded with 'source <filename>'. They can then be
7# called by the user. Macros that explore more complicated structs generally
8# take pointer arguments.
9#
10# E.g.:
11#
12# (gdb) source ~paul/code/quagga/gdb/lib.txt
13# (gdb) break bgp_packet.c:613
14# Breakpoint 3 at 0x7fa883033a32: file bgp_packet.c, line 613.
15# (gdb) cont
16# ...
17# (gdb) cont
18# Breakpoint 3, bgp_write_packet (peer=0x7fa885199080) at bgp_packet.c:614
19# 614 if (CHECK_FLAG (adv->binfo->peer->cap,PEER_CAP_RESTART_RCV)
20# (gdb) dump_prefix4 &adv->rn->p
21# IPv4:10.1.1.0/24
22# (gdb) dump_prefix &adv->rn->p
23# IPv4:10.1.1.0/24
24#
25
26
27define def_ntohs
28 set $data = (char *)$arg0
29 set $i = 0
30
31 set $_ = $data[$i++] << 8
32 set $_ += $data[$i++]
33end
34document def_ntohs
35Read a 2-byte short at the given pointed to area as big-endian and
36return it in $_
37
38Argument: Pointer to a 2-byte, big-endian short word.
39Returns: Integer value of that word in $_
40end
41
42define def_ntohl
43 set $data = (char *)$arg0
44 set $i = 0
45
46 set $_ = $data[$i++] << 24
47 set $_ += $data[$i++] << 16
48 set $_ += $data[$i++] << 8
49 set $_ += $data[$i++]
50end
51document def_ntohl
52Read a 4-byte integer at the given pointed to area as big-endian and
53return it in $_
54
55Argument: Pointer to a big-endian 4-byte word.
56Returns: Integer value of that word in $_
57end
58
59# NB: This is in more complicated iterative form, rather than more
60# conventional and simpler recursive form, because GDB has a recursion limit
61# on macro calls (I think).
62define walk_route_table_next
63 # callee saves
64 set $_top = $top
65 set $_node = $node
66 set $_prevl = $prevl
67
68 set $top = (struct route_node *)$arg0
69 set $node = (struct route_node *)$arg1
70 set $prevl = $node
71
72 # first try left
73 #echo try left\n
74 set $node = $prevl->link[0]
75
76 # otherwise try right
77 if ($node == 0)
78 #echo left null, try right\n
79 set $node = $prevl->link[1]
80 end
81
82 # otherwise go up, till we find the first right that
83 # we havn't been to yet
84 if ($node == 0)
85 set $node = $prevl
86 while ($node != $top)
87 #echo right null, try up and right\n
88
89 set $prevl = $node
90 set $parent = $node->parent
91 set $node = $parent->link[1]
92
93 if ($node != 0 && $node != $prevl)
94 #echo found node \n
95 loop_break
96 end
97
98 #echo go up\n
99 set $node = $parent
100 end
101 end
102
103 #printf "next node: 0x%x\n", $node
104
105 set $_ = $node
106
107 set $top = $_top
108 set $node = $_node
109 set $prevl = $_prevl
110end
111document walk_route_table_next
112Return the next node to visit in the given route_table (or subset of) and
113the given current node.
114
115Arguments:
1161st: (struct route_node *) to the top of the route_table to walk
1172nd: (struct route_node *) to the current node
118
119Returns: The (struct route_node *) for the next to visit in $_
120end
121
122define walk_route_table
123 set $_visited = $visited
124 set $_node = $node
125 set $top = $_top
126
127 set $node = (struct route_node *)$arg0
128 set $top = (struct route_node *)$arg0
129 set $visited = 0
130
131 while ($node != 0)
132 printf "Node: 0x%x", $node
133
134 if ($node->info != 0)
135 printf "\tinfo: 0x%x", $node->info
136 set $visited = $visited + 1
137 end
138
139 printf "\n"
140
141 walk_route_table_next $top $node
142 set $node = $_
143
144 # we've gotten back to the top, finish
145 if ($node == $top)
146 set $node = 0
147 end
148 end
149 printf "Visited: %u\n", $visited
150
151 set $top = $_top
152 set $visited = $_visited
153 set $node = $_node
154end
155
156document walk_route_table
157Walk through a routing table (or subset thereof) and dump all the non-null
158(struct route_node *)->info pointers.
159
160Argument: A lib/thread.h::(struct route_node *) pointing to the route_node
161under which all data should be dumped
162end
163
164define dump_timeval
165 set $tv = (struct timeval *)$arg0
166 set $day = 3600*24
167
168 if $tv->tv_sec > $day
169 printf "%d days, ", $tv->tv_sec / $day
170 end
171 if $tv->tv_sec > 3600
172 printf "%dh", $tv->tv_sec / 3600
173 end
174 if ($tv->tv_sec % 3600) > 60
175 printf "%dm", ($tv->tv_sec % 3600) / 60
176 end
177 printf "%d", $tv->tv_sec % 3600 % 60
178 if $tv->tv_usec != 0
179 printf ".%06d", $tv->tv_usec
180 end
181 printf "s"
182end
183document dump_timeval
184Human readable dump of a (struct timeval *) argument
185end
186
187define dump_s_addr
188 set $addr = (char *)$arg0
189
190 printf "%d.%d.%d.%d", $addr[0], $addr[1], $addr[2], $addr[3]
191end
192
193define dump_s6_addr
194 set $a6 = (char *)$arg0
195 set $field = 0
196
197 while ($field < 16)
198 set $i1 = $field++
199 set $i2 = $field++
200
201 printf "%x%x", $a6[$i1], $a6[$i2]
202
203 if ($field > 2 && ($field % 4 == 0))
204 printf ":"
205 end
206 end
207end
208document dump_s6_addr
209Interpret the memory starting at given address as an IPv6 s6_addr and
210print in human readable form.
211end
212
213define dump_prefix4
214 set $p = (struct prefix *) $arg0
215 echo IPv4:
216 dump_s_addr &($p->u.prefix4)
217 printf "/%d\n", $p->prefixlen
218end
219document dump_prefix4
220Textual dump of a (struct prefix4 *) argument.
221end
222
223define dump_prefix6
224 set $p = (struct prefix *) $arg0
225 echo IPv6:
226 dump_s6_addr &($p->u.prefix6)
227 printf "/%d\n", $p->prefixlen
228end
229document dump_prefix6
230Textual dump of a (struct prefix6 *) argument.
231end
232
233define dump_prefix
234 set $p = $arg0
235
236 if ($p->family == 2)
237 dump_prefix4 $p
238 end
239 if ($p->family == 10)
240 dump_prefix6 $p
241 end
242end
243document dump_prefix
244Human readable dump of a (struct prefix *) argument.
245end
246
247define rn_next_down
248 set $node = $arg0
249 while ($node != 0)
250 print/x $node
251 if ($node->link[0] != 0)
252 set $node = $node->link[0]
253 else
254 set $node = $node->link[1]
255 end
256 end
257end
258
259document rn_next_down
260Walk left-down a given route table, dumping locations of route_nodes
261
262Argument: A single (struct route_node *).
263end
264
265define rn_next_up
266 set $top = (struct route_node *)$arg0
267 set $node = (struct route_node *)$arg1
268
269 while ($node != $top)
270 echo walk up\n
271
272 set $prevl = $node
273 set $parent = $node->parent
274 set $node = $parent->link[1]
275
276 if ($node != 0 && $node != $prevl)
277 echo found a node\n
278 loop_break
279 end
280
281 echo going up\n
282 set $node = $parent
283 end
284 output/x $node
285 echo \n
286end
287
288document rn_next_up
289Walk up-and-right from the given route_node to the next valid route_node
290which is not the given "top" route_node
291
292Arguments:
2931st: A (struct route_node *) to the top of the route table.
2942nd: The (struct route_node *) to walk up from
295end