]> granicus.if.org Git - apache/blob - .gdbinit
Fix alignment in a <highlight> block.
[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_addr_recs
334     set $sa_ = $arg0
335     set $san_ = 0
336     while $sa_
337       ### need to call apr_sockaddr_info_getbuf to print ->host_addr properly
338       ### which is a PITA since we need a buffer :(
339       printf " addr#%d: vhost=%s -> :%d\n", $san_++, $sa_->virthost, $sa_->host_port
340       set $sa_ = $sa_->next
341     end
342 end
343 document dump_server_addr_recs
344     Print server_addr_rec info
345 end
346
347
348 define dump_server_rec
349     set $s = $arg0
350     printf "name=%s:%d (0x%lx)\n", \
351             $s->server_hostname, $s->port, $s
352     dump_server_addr_recs $s->addrs
353     dump_process_rec($s->process)
354 end
355 document dump_server_rec
356     Print server_rec info
357 end
358
359 define dump_servers
360     set $s = $arg0
361     while $s
362         dump_server_rec($s)
363         printf "\n"
364         set $s = $s->next
365     end
366 end
367 document dump_servers
368     Print server_rec list info
369 end
370
371 define dump_request_tree
372     set $r = $arg0
373     set $i
374     while $r
375         printf "r=(0x%lx): uri=%s, handler=%s, r->main=0x%lx\n", \
376           $r, $r->unparsed_uri, $r->handler ? $r->handler : "(none)", $r->main
377         set $r = $r->main
378     end
379 end        
380
381 define dump_scoreboard
382     # Need to reserve size of array first before string literals could be
383     # put in
384     set $status = {0, 1, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 ,10}
385     set $status = {"DEAD", "STARTING", "READY", "BUSY_READ", "BUSY_WRITE", "BUSY_KEEPALIVE", "BUSY_LOG", "BUSY_DNS", "CLOSING", "GRACEFUL", "IDLE_KILL"}
386     set $i = 0
387     while ($i < server_limit)
388         if ap_scoreboard_image->servers[$i][0].pid != 0
389             set $j = 0
390             while ($j < threads_per_child)
391                 set $ws = ap_scoreboard_image->servers[$i][$j]
392                 printf "pid: %d, tid: 0x%lx, status: %s\n", $ws.pid, $ws.tid, $status[$ws.status]
393                 set $j = $j +1
394             end
395         end
396         set $i = $i +1
397     end
398 end
399 document dump_scoreboard
400     Dump the scoreboard
401 end
402
403 define dump_allocator
404     printf "Allocator current_free_index = %d, max_free_index = %d\n", \
405             ($arg0)->current_free_index, ($arg0)->max_free_index
406     printf "Allocator free list:\n"
407     set $i = 0
408     set $max =(sizeof $arg0->free)/(sizeof $arg0->free[0])
409     set $kb = 0
410     while $i < $max
411         set $node = $arg0->free[$i]
412         if $node != 0
413             printf " #%2d: ", $i
414             while $node != 0
415                 printf "%d, ", ($node->index + 1) << 12
416                 set $kb = $kb + (($node->index + 1) << 2)
417                 set $node = $node->next
418             end
419             printf "ends.\n"
420         end
421         set $i = $i + 1
422     end
423     printf "Sum of free blocks: %dkiB\n", $kb
424 end
425 document dump_allocator
426     Print status of an allocator and its freelists.
427 end
428
429 define dump_one_pool
430     set $p = $arg0
431     set $size = 0
432     set $free = 0
433     set $nodes = 0
434     set $node = $arg0->active
435     set $done = 0
436     while $done == 0
437         set $size = $size + (($node->index + 1) << 12)
438         set $free = $free + ($node->endp - $node->first_avail)
439         set $nodes = $nodes + 1
440         set $node = $node->next
441         if $node == $arg0->active
442             set $done = 1
443         end
444     end
445     printf "Pool '"
446     if $p->tag
447         printf "%s", $p->tag
448     else
449         printf "no tag"
450     end
451     printf "' [%p]: %d/%d free (%d blocks)\n", $p, $free, $size, $nodes
452 end
453
454 define dump_all_pools
455     if $argc > 0
456         set $root = $arg0
457     else
458         set $root = ap_pglobal
459     end
460     while $root->parent
461         set $root = $root->parent
462     end
463     dump_pool_and_children $root
464 end
465 document dump_all_pools
466     Dump the whole pool hierarchy starting from apr_global_pool. Optionally takes an arbitrary pool as starting parameter.
467 end
468
469 python
470
471 from __future__ import print_function
472
473 class DumpPoolAndChilds (gdb.Command):
474   """Dump the whole pool hierarchy starting from the given pool."""
475
476   def __init__ (self):
477     super (DumpPoolAndChilds, self).__init__ ("dump_pool_and_children", gdb.COMMAND_USER)
478
479   def _allocator_free_blocks(self, alloc):
480     salloc = "%s" % (alloc)
481     if self.total_free_blocks.get(salloc) != None:
482       return self.total_free_blocks[salloc]
483     i = 0
484     dalloc = alloc.dereference()
485     max =(dalloc['free'].type.sizeof)/(dalloc['free'][0].type.sizeof)
486     kb = 0
487     while i < max:
488       node = dalloc['free'][i]
489       if node != 0:
490         while node != 0:
491           noded = node.dereference()
492           kb = kb + ((int(noded['index']) + 1) << 2)
493           node = noded['next']
494       i = i + 1
495     self.total_free_blocks[salloc] = kb
496     return kb
497
498
499   def _dump_one_pool(self, arg):
500     size = 0
501     free = 0
502     nodes = 0
503     darg = arg.dereference()
504     active = darg['active']
505     node = active
506     done = 0
507     while done == 0:
508       noded = node.dereference()
509       size = size + ((int(noded['index']) + 1) << 12)
510       free = free + (noded['endp'] - noded['first_avail'])
511       nodes = nodes + 1
512       node = noded['next']
513       if node == active:
514         done = 1
515     if darg['tag'] != 0:
516       tag = darg['tag'].string()
517     else:
518       tag = "No tag"
519     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'])))
520     self.free = self.free + free
521     self.size = self.size + size
522     self.nodes = self.nodes + nodes
523
524   def _dump(self, arg, depth):
525     pool = arg
526     print("%*c" % (depth * 4 + 1, " "), end="")
527     self._dump_one_pool(pool)
528     if pool['child'] != 0:
529       self._dump(pool['child'], depth + 1)
530     s = pool['sibling']
531     if s != 0:
532       self._dump(s, depth)
533
534   def invoke (self, arg, from_tty):
535     pool = gdb.parse_and_eval(arg)
536     self.free = 0
537     self.size = 0
538     self.nodes = 0
539     self.total_free_blocks = {}
540     self._dump(pool, 0)
541     print("Total %d/%d free (%d blocks)" % (self.free, self.size, self.nodes))
542     sum = 0
543     for key in self.total_free_blocks:
544       sum = sum + self.total_free_blocks[key]
545     print("Total free allocator blocks: %i kiB" % (sum))
546
547 DumpPoolAndChilds ()
548 end
549 document dump_pool_and_children
550     Dump the whole pool hierarchy starting from the given pool.
551 end
552
553 # Set sane defaults for common signals:
554 handle SIGPIPE noprint pass nostop
555 handle SIGUSR1 print pass nostop