summaryrefslogtreecommitdiffstats
path: root/.gdbinit
blob: f748b05e2f82073580a8895a89cd46a21470b65b (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# gdb macros which may be useful for folks using gdb to debug
# apache.  Delete it if it bothers you.

define dump_table
    set $t = (apr_table_entry_t *)((apr_array_header_t *)$arg0)->elts
    set $n = ((apr_array_header_t *)$arg0)->nelts
    set $i = 0
    while $i < $n
        if $t[$i].val == (void *)0L
            printf "[%u] '%s'=>NULL\n", $i, $t[$i].key
        else
            printf "[%u] '%s'='%s' [%p]\n", $i, $t[$i].key, $t[$i].val, $t[$i].val
        end
        set $i = $i + 1
    end
end
document dump_table
    Print the key/value pairs in a table.
end

define dump_skiplist
    set $sl = (apr_skiplist *)$arg0
    set $m = $sl->bottom
    printf "skiplist@%p: size=%lu: height=%d\n", $sl, $sl->size, $sl->height
    while ($m)
        printf "(%p,%.12lx)", $m, $m->data
        set $u = $m->up
        while ($u)
            printf " (%p,%.12lx)", $u, $u->data
            set $u = $u->up
        end
        printf "\n"
        set $m = $m->next
    end
end
document dump_skiplist
    Print the nodes/values in a skiplist
end

define dump_string_hash
    set $h = $arg0->array
    set $n = $arg0->max
    set $i = 0
    while $i < $n
        set $ent = $h[$i]       
        while $ent != (void *)0L
            printf "'%s' => '%p'\n", $ent->key, $ent->val
            set $ent = $ent->next
        end
        set $i = $i + 1
    end
end
document dump_string_hash
    Print the entries in a hash table indexed by strings
end

define dump_string_shash
    set $h = $arg0->array
    set $n = $arg0->max
    set $i = 0
    while $i < $n
        set $ent = $h[$i]       
        while $ent != (void *)0L
            printf "'%s' => '%s'\n", $ent->key, $ent->val
            set $ent = $ent->next
        end
        set $i = $i + 1
    end
end
document dump_string_shash
    Print the entries in a hash table indexed by strings with string values
end

define ro
    run -DONE_PROCESS
end

define dump_string_array
    set $a = (char **)((apr_array_header_t *)$arg0)->elts
    set $n = (int)((apr_array_header_t *)$arg0)->nelts
    set $i = 0
    while $i < $n
        printf "[%u] '%s'\n", $i, $a[$i]
        set $i = $i + 1
    end
end
document dump_string_array
    Print all of the elements in an array of strings.
end

define printmemn
    set $i = 0
    while $i < $arg1
        if $arg0[$i] < 0x20 || $arg0[$i] > 0x7e
            printf "~"
        else
            printf "%c", $arg0[$i]
        end
        set $i = $i + 1
    end
end

define print_bkt_datacol
    # arg0 == column name
    # arg1 == format
    # arg2 == value
    # arg3 == suppress header?
    set $suppressheader = $arg3

    if !$suppressheader
        printf " "
        printf $arg0
        printf "="
    else
        printf " | "
    end
    printf $arg1, $arg2
end

define dump_bucket_ex
    # arg0 == bucket
    # arg1 == suppress header?
    set $bucket = (struct apr_bucket *)$arg0
    set $sh = $arg1
    set $refcount = -1

    print_bkt_datacol "bucket" "%-9s" $bucket->type->name $sh
    printf "(%12lx)", (unsigned long)$bucket
    print_bkt_datacol "length" "%-6ld" (long)($bucket->length) $sh
    print_bkt_datacol "data" "%12lx" $bucket->data $sh

    if !$sh
        printf "\n    "
    end

    if (($bucket->type == &apr_bucket_type_eos)   || \
        ($bucket->type == &apr_bucket_type_flush))

        # metadata buckets, no content
        print_bkt_datacol "contents" "%c" ' ' $sh
        printf "                     "
        print_bkt_datacol "rc" "n/%c" 'a' $sh

    else
    if ($_streq($bucket->type->name, "ERROR"))

        # metadata bucket, no content but it does have an error code in it
        print_bkt_datacol "contents" "%c" ' ' $sh
        set $status = ((ap_bucket_error *)$bucket->data)->status
        printf " (status=%3d)        ", $status
        print_bkt_datacol "rc" "n/%c" 'a' $sh

    else
    if ($bucket->type == &apr_bucket_type_file)

        # file bucket, can show fd and refcount
        set $fd = ((apr_bucket_file*)$bucket->data)->fd->filedes
        print_bkt_datacol "contents" "[***file***] fd=%-6ld" (long)$fd $sh
        set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
        print_bkt_datacol "rc" "%-3d" $refcount $sh

    else
    if (($bucket->type == &apr_bucket_type_heap)      || \
        ($bucket->type == &apr_bucket_type_pool)      || \
        ($bucket->type == &apr_bucket_type_mmap)      || \
        ($bucket->type == &apr_bucket_type_transient) || \
        ($bucket->type == &apr_bucket_type_immortal))

        # in-memory buckets

        if $bucket->type == &apr_bucket_type_heap
            set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
            set $p = (apr_bucket_heap *)$bucket->data
            set $data = $p->base+$bucket->start

        else
        if $bucket->type == &apr_bucket_type_pool
            set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
            set $p = (apr_bucket_pool *)$bucket->data
            if !$p->pool
                set $p = (apr_bucket_heap *)$bucket->data
            end
            set $data = $p->base+$bucket->start

        else
        if $bucket->type == &apr_bucket_type_mmap
            # is this safe if not APR_HAS_MMAP?
            set $refcount = ((apr_bucket_refcount *)$bucket->data)->refcount
            set $p = (apr_bucket_mmap *)$bucket->data
            set $data = ((char *)$p->mmap->mm)+$bucket->start

        else
        if (($bucket->type == &apr_bucket_type_transient) || \
            ($bucket->type == &apr_bucket_type_immortal))
            set $data = ((char *)$bucket->data)+$bucket->start

        end
        end
        end
        end

        if $sh
            printf " | ["
        else
            printf " contents=["
        end
        set $datalen = $bucket->length
        if $isValidAddress($data) == 1
            if $datalen > 17
                printmem $data 17
                printf "..."
                set $datalen = 20
            else
                printmemn $data $datalen
            end
            printf "]"
            while $datalen < 20
                printf " "
                set $datalen = $datalen + 1
            end
        else
            printf "Iv addr %12lx]", $data
        end

        if $refcount != -1
            print_bkt_datacol "rc" "%-3d" $refcount $sh
        else
            print_bkt_datacol "rc" "n/%c" 'a' $sh
        end

    else
        if ($bucket->type == &apr_bucket_type_pipe)

            # pipe bucket, can show fd
            set $fd = ((apr_file_t*)$bucket->data)->filedes
            print_bkt_datacol "contents" "[***pipe***] fd=%-3ld" (long)$fd $sh

        else
        if ($bucket->type == &apr_bucket_type_socket)

            # file bucket, can show fd
            set $fd = ((apr_socket_t*)$bucket->data)->socketdes
            print_bkt_datacol "contents" "[**socket**] fd=%-3ld" (long)$fd $sh

        else

            # 3rd-party bucket type
            print_bkt_datacol "contents" "[**opaque**]%-7c" ' ' $sh
        end
        end

        # no refcount
        printf "   "
        print_bkt_datacol "rc" "n/%c" 'a' $sh
    end
    end
    end
    end

    printf "\n"

end

define dump_bucket
    dump_bucket_ex $arg0 0
end
document dump_bucket
    Print bucket info
end

define dump_brigade
    set $bb = (apr_bucket_brigade *)$arg0
    set $bucket = $bb->list.next
    set $sentinel = ((char *)((&($bb->list)) \
                               - ((size_t) &((struct apr_bucket *)0)->link)))
    printf "dump of brigade 0x%lx\n", (unsigned long)$bb

    printf "   | type     (address)      | length | "
    printf "data address | contents               | rc\n"
    printf "-------------------------------------------"
    printf "----------------------------------------\n"

    if $bucket == $sentinel
        printf "brigade is empty\n"
    end

    set $j = 0
    set $brigade_length = 0
    while $bucket != $sentinel
        printf "%2d", $j
        dump_bucket_ex $bucket 1
        set $j = $j + 1
        if $bucket->length > 0
            set $brigade_length = $brigade_length + $bucket->length
        end
        set $bucket = $bucket->link.next
    end
    printf "end of brigade\n"
    printf "Length of brigade (excluding buckets of unknown length): %u\n", $brigade_length
end
document dump_brigade
    Print bucket brigade info
end

define dump_filters
    set $f = $arg0
    while $f
        printf "%s(0x%lx): ctx=0x%lx, r=0x%lx, c=0x%lx\n", \
        $f->frec->name, (unsigned long)$f, (unsigned long)$f->ctx, \
        $f->r, $f->c
        set $f = $f->next
    end
end
document dump_filters
    Print filter chain info
end

define dump_filter_chain
    set $r = $arg0
    set $f = $r->output_filters
    while $f
        if $f == $r->output_filters
            printf "r->output_filters =>\n"
        end
        if $f == $r->proto_output_filters
            printf "r->proto_output_filters =>\n"
        end
        if $f == $r->connection->output_filters
            printf "r->connection->output_filters =>\n"
        end
        
        printf "  %s(0x%lx): type=%d, ctx=0x%lx, r=%s(0x%lx), c=0x%lx\n", \
          $f->frec->name, (unsigned long)$f, $f->frec->ftype, (unsigned long)$f->ctx, \
          $f->r == $r ? "r" : ($f->r == 0L ? "null" : \
          ($f->r == $r->main ? "r->main" :  \
          ($r->main && $f->r == $r->main->main ? "r->main->main" : "????"))), \
          $f->r, $f->c

        set $f = $f->next
    end
end
document dump_filter_chain
    Print filter chain info given a request_rec pointer
end

define dump_process_rec
    set $p = $arg0
    printf "process_rec=0x%lx:\n", (unsigned long)$p
    printf "   pool=0x%lx, pconf=0x%lx\n", \
           (unsigned long)$p->pool, (unsigned long)$p->pconf
end
document dump_process_rec
    Print process_rec info
end

define dump_server_addr_recs
    set $sa_ = $arg0
    set $san_ = 0
    while $sa_
      ### need to call apr_sockaddr_info_getbuf to print ->host_addr properly
      ### which is a PITA since we need a buffer :(
      printf " addr#%d: vhost=%s -> :%d\n", $san_++, $sa_->virthost, $sa_->host_port
      set $sa_ = $sa_->next
    end
end
document dump_server_addr_recs
    Print server_addr_rec info
end


define dump_server_rec
    set $s = $arg0
    printf "name=%s:%d (0x%lx)\n", \
            $s->server_hostname, $s->port, $s
    dump_server_addr_recs $s->addrs
    dump_process_rec($s->process)
end
document dump_server_rec
    Print server_rec info
end

define dump_servers
    set $s = $arg0
    while $s
        dump_server_rec($s)
        printf "\n"
        set $s = $s->next
    end
end
document dump_servers
    Print server_rec list info
end

define dump_request_tree
    set $r = $arg0
    set $i
    while $r
        printf "r=(0x%lx): uri=%s, handler=%s, r->main=0x%lx\n", \
          $r, $r->unparsed_uri, $r->handler ? $r->handler : "(none)", $r->main
        set $r = $r->main
    end
end        

define dump_scoreboard
    # Need to reserve size of array first before string literals could be
    # put in
    set $status = {0, 1, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 ,10}
    set $status = {"DEAD", "STARTING", "READY", "BUSY_READ", "BUSY_WRITE", "BUSY_KEEPALIVE", "BUSY_LOG", "BUSY_DNS", "CLOSING", "GRACEFUL", "IDLE_KILL"}
    set $i = 0
    while ($i < server_limit)
        if ap_scoreboard_image->servers[$i][0].pid != 0
            set $j = 0
            while ($j < threads_per_child)
                set $ws = ap_scoreboard_image->servers[$i][$j]
                printf "pid: %d, tid: 0x%lx, status: %s\n", $ws.pid, $ws.tid, $status[$ws.status]
                set $j = $j +1
            end
        end
        set $i = $i +1
    end
end
document dump_scoreboard
    Dump the scoreboard
end

define dump_allocator
    printf "Allocator current_free_index = %d, max_free_index = %d\n", \
            ($arg0)->current_free_index, ($arg0)->max_free_index
    printf "Allocator free list:\n"
    set $i = 0
    set $max =(sizeof $arg0->free)/(sizeof $arg0->free[0])
    set $kb = 0
    while $i < $max
        set $node = $arg0->free[$i]
        if $node != 0
            printf " #%2d: ", $i
            while $node != 0
                printf "%d, ", ($node->index + 1) << 12
                set $kb = $kb + (($node->index + 1) << 2)
                set $node = $node->next
            end
            printf "ends.\n"
        end
        set $i = $i + 1
    end
    printf "Sum of free blocks: %dkiB\n", $kb
end
document dump_allocator
    Print status of an allocator and its freelists.
end

define dump_one_pool
    set $p = $arg0
    set $size = 0
    set $free = 0
    set $nodes = 0
    set $node = $arg0->active
    set $done = 0
    while $done == 0
        set $size = $size + (($node->index + 1) << 12)
        set $free = $free + ($node->endp - $node->first_avail)
        set $nodes = $nodes + 1
        set $node = $node->next
        if $node == $arg0->active
            set $done = 1
        end
    end
    printf "Pool '"
    if $p->tag
        printf "%s", $p->tag
    else
        printf "no tag"
    end
    printf "' [%p]: %d/%d free (%d blocks)\n", $p, $free, $size, $nodes
end

define dump_all_pools
    if $argc > 0
        set $root = $arg0
    else
        set $root = ap_pglobal
    end
    while $root->parent
        set $root = $root->parent
    end
    dump_pool_and_children $root
end
document dump_all_pools
    Dump the whole pool hierarchy starting from apr_global_pool. Optionally takes an arbitrary pool as starting parameter.
end

python

from __future__ import print_function

class DumpPoolAndChilds (gdb.Command):
  """Dump the whole pool hierarchy starting from the given pool."""

  def __init__ (self):
    super (DumpPoolAndChilds, self).__init__ ("dump_pool_and_children", gdb.COMMAND_USER)

  def _allocator_free_blocks(self, alloc):
    salloc = "%s" % (alloc)
    if self.total_free_blocks.get(salloc) != None:
      return self.total_free_blocks[salloc]
    i = 0
    dalloc = alloc.dereference()
    max =(dalloc['free'].type.sizeof)/(dalloc['free'][0].type.sizeof)
    kb = 0
    while i < max:
      node = dalloc['free'][i]
      if node != 0:
        while node != 0:
          noded = node.dereference()
          kb = kb + ((int(noded['index']) + 1) << 2)
          node = noded['next']
      i = i + 1
    self.total_free_blocks[salloc] = kb
    return kb


  def _dump_one_pool(self, arg, depth):
    size = 0
    free = 0
    nodes = 0
    indent = depth * 4 + 1
    darg = arg.dereference()
    active = darg['active']
    node = active
    done = 0
    while done == 0:
      noded = node.dereference()
      size = size + ((int(noded['index']) + 1) << 12)
      free = free + (noded['endp'] - noded['first_avail'])
      nodes = nodes + 1
      node = noded['next']
      if node == active:
        done = 1
    if darg['tag'] != 0:
      tag = darg['tag'].string()
    else:
      tag = "No tag"
    print("%*cPool '%s' [%s]: %d/%d free (%d blocks) allocator: %s free blocks in allocator: %i kiB" % \
          (indent, ' ', tag, arg, free, size, nodes, darg['allocator'], \
           self._allocator_free_blocks(darg['allocator'])))
    self.free = self.free + free
    self.size = self.size + size
    self.nodes = self.nodes + nodes
    c_num = 0
    c = darg['pre_cleanups']
    if c:
        print("%*cCleanups (pre):" % (indent + 1, ' '))
    while c:
        c_num = c_num + 1
        dc = c.dereference()
        print("%*c%.3i: data = %s, plain_cleanup = %s" % \
              (indent + 2, ' ', c_num, \
               dc['data'], dc['plain_cleanup_fn'].dereference()))
        c = dc['next']
    c = darg['cleanups']
    if c:
        print("%*cCleanups (post):" % (indent + 1, ' '))
    while c:
        c_num = c_num + 1
        dc = c.dereference()
        print("%*c%.3i: data = %s, plain_cleanup = %s, child_cleanup = %s" % \
              (indent + 2, ' ', c_num, \
               dc['data'], dc['plain_cleanup_fn'].dereference(), \
               dc['child_cleanup_fn'].dereference()))
        c = dc['next']

  def _dump(self, arg, depth):
    pool = arg
    while pool:
        self._dump_one_pool(pool, depth)
        if pool['child'] != 0:
            self._dump(pool['child'], depth + 1)
        pool = pool['sibling']

  def invoke (self, arg, from_tty):
    pool = gdb.parse_and_eval(arg)
    self.free = 0
    self.size = 0
    self.nodes = 0
    self.total_free_blocks = {}
    self._dump(pool, 0)
    print("Total %d/%d free (%d blocks)" % (self.free, self.size, self.nodes))
    sum = 0
    for key in self.total_free_blocks:
      sum = sum + self.total_free_blocks[key]
    print("Total free allocator blocks: %i kiB" % (sum))

DumpPoolAndChilds ()
end
document dump_pool_and_children
    Dump the whole pool hierarchy starting from the given pool.
end

python

class isValidAddress (gdb.Function):
    """Determines if the argument is a valid address."""

    def __init__(self):
        super(isValidAddress, self).__init__("isValidAddress")

    def invoke(self, address):
        inf = gdb.inferiors()[0]
        result = 1
        try:
            inf.read_memory(address, 8)
        except:
            result = 0
        return result

isValidAddress()

end

# Set sane defaults for common signals:
handle SIGPIPE noprint pass nostop
handle SIGUSR1 print pass nostop