summaryrefslogtreecommitdiffstats
path: root/gdb/lib.txt
blob: 5d22321b6291bf2c730fced71a98011b5fbc020c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# GDB macros for use with Quagga.
#
# Macros in this file are not daemon specific. E.g., OS or FRR library
# APIs.
#
# The macro file can be loaded with 'source <filename>'. They can then be
# called by the user. Macros that explore more complicated structs generally
# take pointer arguments.
#
# E.g.:
# 
# (gdb) source ~paul/code/frr/gdb/lib.txt
# (gdb) break bgp_packet.c:613
# Breakpoint 3 at 0x7fa883033a32: file bgp_packet.c, line 613.
# (gdb) cont
# ...
# (gdb) cont
# Breakpoint 3, bgp_write_packet (peer=0x7fa885199080) at bgp_packet.c:614
# 614                     if (CHECK_FLAG (adv->path->peer->cap,PEER_CAP_RESTART_RCV)
# (gdb) dump_prefix4  &adv->rn->p
# IPv4:10.1.1.0/24
# (gdb) dump_prefix  &adv->rn->p
# IPv4:10.1.1.0/24
#


define def_ntohs
 set $data = (char *)$arg0
 set $i = 0
 
 set $_  = $data[$i++] << 8
 set $_ += $data[$i++]
end
document def_ntohs
Read a 2-byte short at the given pointed to area as big-endian and 
return it in $_

Argument: Pointer to a 2-byte, big-endian short word.
Returns: Integer value of that word in $_
end

define def_ntohl
 set $data = (char *)$arg0
 set $i = 0
 
 set $_  = $data[$i++] << 24
 set $_ += $data[$i++] << 16
 set $_ += $data[$i++] << 8
 set $_ += $data[$i++]
end
document def_ntohl
Read a 4-byte integer at the given pointed to area as big-endian and 
return it in $_

Argument: Pointer to a big-endian 4-byte word.
Returns: Integer value of that word in $_
end

# NB: This is in more complicated iterative form, rather than more
# conventional and simpler recursive form, because GDB has a recursion limit
# on macro calls (I think).
define walk_route_table_next
  # callee saves
  set $_top = $top
  set $_node = $node
  set $_prevl = $prevl
  
  set $top = (struct route_node *)$arg0
  set $node = (struct route_node *)$arg1
  set $prevl = $node
  
  # first try left
  #echo try left\n
  set $node = $prevl->link[0]
  
  # otherwise try right
  if ($node == 0)
    #echo left null, try right\n
    set $node = $prevl->link[1]
  end
  
  # otherwise go up, till we find the first right that
  # we havn't been to yet
  if ($node == 0)
    set $node = $prevl
    while ($node != $top)
       #echo right null, try up and right\n
       
       set $prevl = $node
       set $parent = $node->parent
       set $node = $parent->link[1]
       
       if ($node != 0 && $node != $prevl)
         #echo found node \n
         loop_break
       end
       
       #echo go up\n
       set $node = $parent       
    end
  end
  
  #printf "next node: 0x%x\n", $node
  
  set $_ = $node
  
  set $top = $_top
  set $node = $_node
  set $prevl = $_prevl
end
document walk_route_table_next
Return the next node to visit in the given route_table (or subset of) and
the given current node.

Arguments:
1st: (struct route_node *) to the top of the route_table to walk
2nd: (struct route_node *) to the current node

Returns: The (struct route_node *) for the next to visit in $_
end

define walk_route_table
  set $_visited = $visited
  set $_node = $node
  set $top = $_top
  
  set $node = (struct route_node *)$arg0
  set $top = (struct route_node *)$arg0
  set $visited = 0
  
  while ($node != 0)
    printf "Node: 0x%x", $node

    if ($node->info != 0)
      printf "\tinfo: 0x%x", $node->info
      set $visited = $visited + 1
    end
    
    printf "\n"
    
    walk_route_table_next $top $node
    set $node = $_
    
    # we've gotten back to the top, finish
    if ($node == $top)
      set $node = 0
    end
  end
  printf "Visited: %u\n", $visited
  
  set $top = $_top
  set $visited = $_visited
  set $node = $_node
end

document walk_route_table
Walk through a routing table (or subset thereof) and dump all the non-null
(struct route_node *)->info pointers.

Argument: A lib/hread.h::(struct route_node *) pointing to the route_node
under which all data should be dumped
end

define dump_timeval 
  set $tv = (struct timeval *)$arg0
  set $day = 3600*24
  
  if $tv->tv_sec > $day
    printf "%d days, ", $tv->tv_sec / $day
  end
  if $tv->tv_sec > 3600
    printf "%dh", $tv->tv_sec / 3600
  end
  if ($tv->tv_sec % 3600) > 60
    printf "%dm", ($tv->tv_sec % 3600) / 60
  end
  printf "%d", $tv->tv_sec % 3600 % 60
  if $tv->tv_usec != 0
    printf ".%06d", $tv->tv_usec
  end
  printf "s"
end
document dump_timeval
Human readable dump of a (struct timeval *) argument
end

define dump_s_addr
  set $addr = (char *)$arg0
  
  printf "%d.%d.%d.%d", $addr[0], $addr[1], $addr[2], $addr[3]
end

define dump_s6_addr
  set $a6 = (char *)$arg0
  set $field = 0
  
  while ($field < 16)
    set $i1 = $field++
    set $i2 = $field++
    
    printf "%x%x", $a6[$i1], $a6[$i2]
    
    if ($field > 2 && ($field % 4 == 0))
      printf ":"
    end
  end
end
document dump_s6_addr
Interpret the memory starting at given address as an IPv6 s6_addr and
print in human readable form.
end

define dump_prefix4
  set $p = (struct prefix *) $arg0
  echo IPv4:
  dump_s_addr &($p->u.prefix4)
  printf "/%d\n", $p->prefixlen
end
document dump_prefix4
Textual dump of a (struct prefix4 *) argument.
end

define dump_prefix6
  set $p = (struct prefix *) $arg0
  echo IPv6:
  dump_s6_addr &($p->u.prefix6)
  printf "/%d\n", $p->prefixlen
end
document dump_prefix6
Textual dump of a (struct prefix6 *) argument.
end

define dump_prefix
  set $p = $arg0
  
  if ($p->family == 2)
    dump_prefix4 $p
  end
  if ($p->family == 10)
    dump_prefix6 $p
  end
end
document dump_prefix
Human readable dump of a (struct prefix *) argument.
end

define rn_next_down
  set $node = $arg0
  while ($node != 0)
    print/x $node
    if ($node->link[0] != 0)
      set $node = $node->link[0]
    else
      set $node = $node->link[1]
    end
  end
end

document rn_next_down
Walk left-down a given route table, dumping locations of route_nodes

Argument: A single (struct route_node *).
end

define rn_next_up
  set $top = (struct route_node *)$arg0
  set $node = (struct route_node *)$arg1
  
  while ($node != $top)
    echo walk up\n
    
    set $prevl = $node
    set $parent = $node->parent
    set $node = $parent->link[1]
    
    if ($node != 0 && $node != $prevl)
      echo found a node\n
      loop_break
    end
    
    echo going up\n
    set $node = $parent
  end
  output/x $node
  echo \n
end

document rn_next_up
Walk up-and-right from the given route_node to the next valid route_node
which is not the given "top" route_node

Arguments:
1st: A (struct route_node *) to the top of the route table.
2nd: The (struct route_node *) to walk up from
end

define mq_walk
  set $mg = (struct memgroup *)$arg0

  while ($mg)
     printf "showing active allocations in memory group %s\n", $mg->name
     set $mt = (struct memtype *)$mg->types
     while ($mt)
         printf "memstats: %s:%zu\n", $mt->name, $mt->n_alloc
         set $mt = $mt->next
     end
     set $mg = $mg->next
  end

document mg_walk
Walk the memory data structures to show what is holding memory.

Arguments:
1st: A (struct memgroup *) where to start the walk.  If you are not
     sure where to start pass it mg_first, which is a global DS for
     all memory allocated in FRR
end