]> granicus.if.org Git - apache/blob - .gdbinit
Vote, promote.
[apache] / .gdbinit
1 # gdb macros which may be useful for folks using gdb to debug
2 # apache.  Delete it if it bothers you.
3
4 define dump_table
5     set $t = (apr_table_entry_t *)((apr_array_header_t *)$arg0)->elts
6     set $n = ((apr_array_header_t *)$arg0)->nelts
7     set $i = 0
8     while $i < $n
9         if $t[$i].val == (void *)0L
10            printf "[%u] '%s'=>NULL\n", $i, $t[$i].key
11         else
12            printf "[%u] '%s'='%s' [%p]\n", $i, $t[$i].key, $t[$i].val, $t[$i].val
13         end
14         set $i = $i + 1
15     end
16 end
17 document dump_table
18     Print the key/value pairs in a table.
19 end
20
21 define dump_skiplist
22     set $sl = (apr_skiplist *)$arg0
23     set $m = $sl->bottom
24     printf "skiplist@%p: size=%lu: height=%d\n", $sl, $sl->size, $sl->height
25     while ($m)
26         printf "(%p,%.12lx)", $m, $m->data
27         set $u = $m->up
28         while ($u)
29             printf " (%p,%.12lx)", $u, $u->data
30             set $u = $u->up
31         end
32         printf "\n"
33         set $m = $m->next
34     end
35 end
36 document dump_skiplist
37     Print the nodes/values in a skiplist
38 end
39
40 define dump_string_hash
41     set $h = $arg0->array
42     set $n = $arg0->max
43     set $i = 0
44     while $i < $n
45         set $ent = $h[$i]       
46         while $ent != (void *)0L
47             printf "'%s' => '%p'\n", $ent->key, $ent->val
48             set $ent = $ent->next
49         end
50         set $i = $i + 1
51     end
52 end
53 document dump_string_hash
54     Print the entries in a hash table indexed by strings
55 end
56
57 define dump_string_shash
58     set $h = $arg0->array
59     set $n = $arg0->max
60     set $i = 0
61     while $i < $n
62         set $ent = $h[$i]       
63         while $ent != (void *)0L
64             printf "'%s' => '%s'\n", $ent->key, $ent->val
65             set $ent = $ent->next
66         end
67         set $i = $i + 1
68     end
69 end
70 document dump_string_shash
71     Print the entries in a hash table indexed by strings with string values
72 end
73
74 define ro
75         run -DONE_PROCESS
76 end
77
78 define dump_string_array
79     set $a = (char **)((apr_array_header_t *)$arg0)->elts
80     set $n = (int)((apr_array_header_t *)$arg0)->nelts
81     set $i = 0
82     while $i < $n
83         printf "[%u] '%s'\n", $i, $a[$i]
84         set $i = $i + 1
85     end
86 end
87 document dump_string_array
88     Print all of the elements in an array of strings.
89 end
90
91 define printmemn
92     set $i = 0
93     while $i < $arg1
94         if $arg0[$i] < 0x20 || $arg0[$i] > 0x7e
95             printf "~"
96         else
97             printf "%c", $arg0[$i]
98         end
99         set $i = $i + 1
100     end
101 end
102
103 define print_bkt_datacol
104     # arg0 == column name
105     # arg1 == format
106     # arg2 == value
107     # arg3 == suppress header?
108     set $suppressheader = $arg3
109
110     if !$suppressheader
111         printf " "
112         printf $arg0
113         printf "="
114     else
115         printf " | "
116     end
117     printf $arg1, $arg2
118 end
119
120 define dump_bucket_ex
121     # arg0 == bucket
122     # arg1 == suppress header?
123     set $bucket = (struct apr_bucket *)$arg0
124     set $sh = $arg1
125     set $refcount = -1
126
127     print_bkt_datacol "bucket" "%-9s" $bucket->type->name $sh
128     printf "(0x%08lx)", (unsigned long)$bucket
129     print_bkt_datacol "length" "%-6ld" (long)($bucket->length) $sh
130     print_bkt_datacol "data" "0x%08lx" $bucket->data $sh
131
132     if !$sh
133         printf "\n    "
134     end
135
136     if (($bucket->type == &apr_bucket_type_eos)   || \
137         ($bucket->type == &apr_bucket_type_flush))
138
139         # metadata buckets, no content
140         print_bkt_datacol "contents" "%c" ' ' $sh
141         printf "                     "
142         print_bkt_datacol "rc" "n/%c" 'a' $sh
143
144     else
145     if ($bucket->type == &ap_bucket_type_error)
146
147         # metadata bucket, no content but it does have an error code in it
148         print_bkt_datacol "contents" "%c" ' ' $sh
149         set $status = ((ap_bucket_error *)$bucket->data)->status
150         printf " (status=%3d)        ", $status
151         print_bkt_datacol "rc" "n/%c" 'a' $sh
152
153     else
154     if (($bucket->type == &apr_bucket_type_file) || \
155         ($bucket->type == &apr_bucket_type_pipe) || \
156         ($bucket->type == &apr_bucket_type_socket))
157
158         # buckets that contain data not in memory (ie not printable)
159
160         print_bkt_datacol "contents" "[**unprintable**%c" ']' $sh
161         printf "     "
162         if $bucket->type == &apr_bucket_type_file
163             set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
164             print_bkt_datacol "rc" "%d" $refcount $sh
165         end
166
167     else
168     if (($bucket->type == &apr_bucket_type_heap)      || \
169         ($bucket->type == &apr_bucket_type_pool)      || \
170         ($bucket->type == &apr_bucket_type_mmap)      || \
171         ($bucket->type == &apr_bucket_type_transient) || \
172         ($bucket->type == &apr_bucket_type_immortal))
173
174         # in-memory buckets
175
176         if $bucket->type == &apr_bucket_type_heap
177             set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
178             set $p = (apr_bucket_heap *)$bucket->data
179             set $data = $p->base+$bucket->start
180
181         else
182         if $bucket->type == &apr_bucket_type_pool
183             set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
184             set $p = (apr_bucket_pool *)$bucket->data
185             if !$p->pool
186                 set $p = (apr_bucket_heap *)$bucket->data
187             end
188             set $data = $p->base+$bucket->start
189
190         else
191         if $bucket->type == &apr_bucket_type_mmap
192             # is this safe if not APR_HAS_MMAP?
193             set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
194             set $p = (apr_bucket_mmap *)$bucket->data
195             set $data = ((char *)$p->mmap->mm)+$bucket->start
196
197         else
198         if (($bucket->type == &apr_bucket_type_transient) || \
199             ($bucket->type == &apr_bucket_type_immortal))
200             set $data = ((char *)$bucket->data)+$bucket->start
201
202         end
203         end
204         end
205         end
206
207         if $sh
208             printf " | ["
209         else
210             printf " contents=["
211         end
212         set $datalen = $bucket->length
213         if $datalen > 17
214             printmem $data 17
215             printf "..."
216             set $datalen = 20
217         else
218             printmemn $data $datalen
219         end
220         printf "]"
221         while $datalen < 20
222             printf " "
223             set $datalen = $datalen + 1
224         end
225
226         if $refcount != -1
227             print_bkt_datacol "rc" "%d" $refcount $sh
228         else
229             print_bkt_datacol "rc" "n/%c" 'a' $sh
230         end
231
232     else
233         # 3rd-party bucket type
234         print_bkt_datacol "contents" "[**unknown**%c" ']' $sh
235         printf "         "
236         print_bkt_datacol "rc" "n/%c" 'a' $sh
237     end
238     end
239     end
240     end
241
242     printf "\n"
243
244 end
245
246 define dump_bucket
247     dump_bucket_ex $arg0 0
248 end
249 document dump_bucket
250     Print bucket info
251 end
252
253 define dump_brigade
254     set $bb = (apr_bucket_brigade *)$arg0
255     set $bucket = $bb->list.next
256     set $sentinel = ((char *)((&($bb->list)) \
257                                - ((size_t) &((struct apr_bucket *)0)->link)))
258     printf "dump of brigade 0x%lx\n", (unsigned long)$bb
259
260     printf "   | type     (address)    | length | "
261     printf "data addr  | contents               | rc\n"
262     printf "----------------------------------------"
263     printf "----------------------------------------\n"
264
265     if $bucket == $sentinel
266         printf "brigade is empty\n"
267     end
268
269     set $j = 0
270     while $bucket != $sentinel
271         printf "%2d", $j
272         dump_bucket_ex $bucket 1
273         set $j = $j + 1
274         set $bucket = $bucket->link.next
275     end
276     printf "end of brigade\n"
277 end
278 document dump_brigade
279     Print bucket brigade info
280 end
281
282 define dump_filters
283     set $f = $arg0
284     while $f
285         printf "%s(0x%lx): ctx=0x%lx, r=0x%lx, c=0x%lx\n", \
286         $f->frec->name, (unsigned long)$f, (unsigned long)$f->ctx, \
287         $f->r, $f->c
288         set $f = $f->next
289     end
290 end
291 document dump_filters
292     Print filter chain info
293 end
294
295 define dump_filter_chain
296     set $r = $arg0
297     set $f = $r->output_filters
298     while $f
299         if $f == $r->output_filters
300             printf "r->output_filters =>\n"
301         end
302         if $f == $r->proto_output_filters
303             printf "r->proto_output_filters =>\n"
304         end
305         if $f == $r->connection->output_filters
306             printf "r->connection->output_filters =>\n"
307         end
308         
309         printf "  %s(0x%lx): type=%d, ctx=0x%lx, r=%s(0x%lx), c=0x%lx\n", \
310           $f->frec->name, (unsigned long)$f, $f->frec->ftype, (unsigned long)$f->ctx, \
311           $f->r == $r ? "r" : ($f->r == 0L ? "null" : \
312           ($f->r == $r->main ? "r->main" :  \
313           ($r->main && $f->r == $r->main->main ? "r->main->main" : "????"))), \
314           $f->r, $f->c
315
316         set $f = $f->next
317     end
318 end
319 document dump_filter_chain
320     Print filter chain info given a request_rec pointer
321 end
322
323 define dump_process_rec
324     set $p = $arg0
325     printf "process_rec=0x%lx:\n", (unsigned long)$p
326     printf "   pool=0x%lx, pconf=0x%lx\n", \
327            (unsigned long)$p->pool, (unsigned long)$p->pconf
328 end
329 document dump_process_rec
330     Print process_rec info
331 end
332
333 define dump_server_rec
334     set $s = $arg0
335     printf "name=%s:%d\n", \
336             $s->server_hostname, $s->port
337     dump_process_rec($s->process)
338 end
339 document dump_server_rec
340     Print server_rec info
341 end
342
343 define dump_servers
344     set $s = $arg0
345     while $s
346         dump_server_rec($s)
347         printf "\n"
348         set $s = $s->next
349     end
350 end
351 document dump_servers
352     Print server_rec list info
353 end
354
355 define dump_request_tree
356     set $r = $arg0
357     set $i
358     while $r
359         printf "r=(0x%lx): uri=%s, handler=%s, r->main=0x%lx\n", \
360           $r, $r->unparsed_uri, $r->handler ? $r->handler : "(none)", $r->main
361         set $r = $r->main
362     end
363 end        
364
365 define dump_allocator
366     printf "Allocator current_free_index = %d, max_free_index = %d\n", \
367             ($arg0)->current_free_index, ($arg0)->max_free_index
368     printf "Allocator free list:\n"
369     set $i = 0
370     set $max =(sizeof $arg0->free)/(sizeof $arg0->free[0])
371     set $kb = 0
372     while $i < $max
373         set $node = $arg0->free[$i]
374         if $node != 0
375             printf " #%2d: ", $i
376             while $node != 0
377                 printf "%d, ", ($node->index + 1) << 12
378                 set $kb = $kb + (($node->index + 1) << 2)
379                 set $node = $node->next
380             end
381             printf "ends.\n"
382         end
383         set $i = $i + 1
384     end
385     printf "Sum of free blocks: %dkiB\n", $kb
386 end
387 document dump_allocator
388     Print status of an allocator and its freelists.
389 end
390
391 define dump_one_pool
392     set $p = $arg0
393     set $size = 0
394     set $free = 0
395     set $nodes = 0
396     set $node = $arg0->active
397     set $done = 0
398     while $done == 0
399         set $size = $size + (($node->index + 1) << 12)
400         set $free = $free + ($node->endp - $node->first_avail)
401         set $nodes = $nodes + 1
402         set $node = $node->next
403         if $node == $arg0->active
404             set $done = 1
405         end
406     end
407     printf "Pool '"
408     if $p->tag
409         printf "%s", $p->tag
410     else
411         printf "no tag"
412     end
413     printf "' [%p]: %d/%d free (%d blocks)\n", $p, $free, $size, $nodes
414 end
415
416 define dump_all_pools
417     set $root = $arg0
418     while $root->parent
419         set $root = $root->parent
420     end
421     dump_pool_and_children $root
422 end
423 document dump_all_pools
424     Dump the whole pool hierarchy starting from apr_global_pool. Requires an arbitrary pool as starting parameter.
425 end
426
427 python
428
429 from __future__ import print_function
430
431 class DumpPoolAndChilds (gdb.Command):
432   """Dump the whole pool hierarchy starting from the given pool."""
433
434   def __init__ (self):
435     super (DumpPoolAndChilds, self).__init__ ("dump_pool_and_children", gdb.COMMAND_USER)
436
437   def _allocator_free_blocks(self, alloc):
438     salloc = "%s" % (alloc)
439     if self.total_free_blocks.get(salloc) != None:
440       return self.total_free_blocks[salloc]
441     i = 0
442     dalloc = alloc.dereference()
443     max =(dalloc['free'].type.sizeof)/(dalloc['free'][0].type.sizeof)
444     kb = 0
445     while i < max:
446       node = dalloc['free'][i]
447       if node != 0:
448         while node != 0:
449           noded = node.dereference()
450           kb = kb + ((int(noded['index']) + 1) << 2)
451           node = noded['next']
452       i = i + 1
453     self.total_free_blocks[salloc] = kb
454     return kb
455
456
457   def _dump_one_pool(self, arg):
458     size = 0
459     free = 0
460     nodes = 0
461     darg = arg.dereference()
462     active = darg['active']
463     node = active
464     done = 0
465     while done == 0:
466       noded = node.dereference()
467       size = size + ((int(noded['index']) + 1) << 12)
468       free = free + (noded['endp'] - noded['first_avail'])
469       nodes = nodes + 1
470       node = noded['next']
471       if node == active:
472         done = 1
473     if darg['tag'] != 0:
474       tag = darg['tag'].string()
475     else:
476       tag = "No tag"
477     print("Pool '%s' [%s]: %d/%d free (%d blocks) allocator: %s free blocks in allocator: %i kiB" % (tag, arg, free, size, nodes, darg['allocator'], self._allocator_free_blocks(darg['allocator'])))
478     self.free = self.free + free
479     self.size = self.size + size
480     self.nodes = self.nodes + nodes
481
482   def _dump(self, arg, depth):
483     pool = arg
484     print("%*c" % (depth * 4 + 1, " "), end="")
485     self._dump_one_pool(pool)
486     if pool['child'] != 0:
487       self._dump(pool['child'], depth + 1)
488     s = pool['sibling']
489     if s != 0:
490       self._dump(s, depth)
491
492   def invoke (self, arg, from_tty):
493     pool = gdb.parse_and_eval(arg)
494     self.free = 0
495     self.size = 0
496     self.nodes = 0
497     self.total_free_blocks = {}
498     self._dump(pool, 0)
499     print("Total %d/%d free (%d blocks)" % (self.free, self.size, self.nodes))
500     sum = 0
501     for key in self.total_free_blocks:
502       sum = sum + self.total_free_blocks[key]
503     print("Total free allocator blocks: %i kiB" % (sum))
504
505 DumpPoolAndChilds ()
506 end
507 document dump_pool_and_children
508     Dump the whole pool hierarchy starting from the given pool.
509 end
510
511 # Set sane defaults for common signals:
512 handle SIGPIPE noprint pass nostop
513 handle SIGUSR1 print pass nostop