summaryrefslogtreecommitdiff
path: root/module/system/vm/debug.scm
blob: d53048dc2366f1309f0ea4ba793768fcaaf2ddbd (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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
;;; Guile runtime debug information

;;; Copyright (C) 2013, 2014, 2015, 2018 Free Software Foundation, Inc.
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;; Commentary:
;;;
;;; Guile's bytecode compiler and linker serialize debugging information
;;; into separate sections of the ELF image.  This module reads those
;;; sections.
;;;
;;; Code:

(define-module (system vm debug)
  #:use-module (system vm elf)
  #:use-module (system vm dwarf)
  #:use-module (system vm loader)
  #:use-module (system foreign)
  #:use-module (rnrs bytevectors)
  #:use-module (ice-9 match)
  #:use-module ((srfi srfi-1) #:select (fold split-at))
  #:use-module (srfi srfi-9)
  #:export (debug-context-image
            debug-context-base
            debug-context-length
            debug-context-text-base

            program-debug-info-name
            program-debug-info-context
            program-debug-info-image
            program-debug-info-offset
            program-debug-info-size
            program-debug-info-addr
            program-debug-info-u32-offset
            program-debug-info-u32-offset-end

            arity?
            arity-low-pc
            arity-high-pc
            arity-has-closure?
            arity-nreq
            arity-nopt
            arity-nlocals
            arity-has-rest?
            arity-allow-other-keys?
            arity-has-keyword-args?
            arity-keyword-args
            arity-is-case-lambda?
            arity-definitions
            arity-code

            debug-context-from-image
            fold-all-debug-contexts
            for-each-elf-symbol
            find-debug-context
            find-program-debug-info
            arity-arguments-alist
            find-program-arities
            find-program-arity
            find-program-minimum-arity

            find-program-docstring

            find-program-properties

            source?
            source-pre-pc
            source-post-pc
            source-file
            source-line
            source-line-for-user
            source-column
            find-source-for-addr
            find-program-sources
            fold-source-locations))

;;; A compiled procedure comes from a specific loaded ELF image.  A
;;; debug context identifies that image.
;;;
(define-record-type <debug-context>
  (make-debug-context elf base text-base)
  debug-context?
  (elf debug-context-elf)
  ;; Address at which this image is loaded in memory, in bytes.
  (base debug-context-base)
  ;; Offset of the text section relative to the image start, in bytes.
  (text-base debug-context-text-base))

(define (debug-context-image context)
  "Return the bytevector aliasing the mapped ELF image corresponding to
@var{context}."
  (elf-bytes (debug-context-elf context)))

(define (debug-context-length context)
  "Return the size of the mapped ELF image corresponding to
@var{context}, in bytes."
  (bytevector-length (debug-context-image context)))

(define (for-each-elf-symbol context proc)
  "Call @var{proc} on each symbol in the symbol table of @var{context}."
  (let ((elf (debug-context-elf context)))
    (cond
     ((elf-section-by-name elf ".symtab")
      => (lambda (symtab)
           (let ((len (elf-symbol-table-len symtab))
                 (strtab (elf-section elf (elf-section-link symtab))))
             (let lp ((n 0))
               (when (< n len)
                 (proc (elf-symbol-table-ref elf symtab n strtab))
                 (lp (1+ n))))))))))

;;; A program debug info (PDI) is a handle on debugging meta-data for a
;;; particular program.
;;;
(define-record-type <program-debug-info>
  (make-program-debug-info context name offset size)
  program-debug-info?
  (context program-debug-info-context)
  (name program-debug-info-name)
  ;; Offset of the procedure in the text section, in bytes.
  (offset program-debug-info-offset)
  (size program-debug-info-size))

(define (program-debug-info-addr pdi)
  "Return the address in memory of the entry of the program represented
by the debugging info @var{pdi}."
  (+ (program-debug-info-offset pdi)
     (debug-context-text-base (program-debug-info-context pdi))
     (debug-context-base (program-debug-info-context pdi))))

(define (program-debug-info-image pdi)
  "Return the ELF image containing @var{pdi}, as a bytevector."
  (debug-context-image (program-debug-info-context pdi)))

(define (program-debug-info-u32-offset pdi)
  "Return the start address of the program represented by @var{pdi}, as
an offset from the beginning of the ELF image in 32-bit units."
  (/ (+ (program-debug-info-offset pdi)
        (debug-context-text-base (program-debug-info-context pdi)))
     4))

(define (program-debug-info-u32-offset-end pdi)
  "Return the end address of the program represented by @var{pdi}, as an
offset from the beginning of the ELF image in 32-bit units."
  (/ (+ (program-debug-info-size pdi)
        (program-debug-info-offset pdi)
        (debug-context-text-base (program-debug-info-context pdi)))
     4))

(define (debug-context-from-image bv)
  "Build a debugging context corresponding to a given ELF image."
  (let* ((elf (parse-elf bv))
         (base (pointer-address (bytevector->pointer (elf-bytes elf))))
         (text-base (elf-section-offset
                     (or (elf-section-by-name elf ".rtl-text")
                         (error "ELF object has no text section")))))
    (make-debug-context elf base text-base)))

(define (fold-all-debug-contexts proc seed)
  "Fold @var{proc} over debug contexts corresponding to all images that
are mapped at the time this procedure is called.  Any images mapped
during the fold are omitted."
  (fold (lambda (image seed)
          (proc (debug-context-from-image image) seed))
        seed
        (all-mapped-elf-images)))

(define (find-debug-context addr)
  "Find and return the debugging context corresponding to the ELF image
containing the address @var{addr}.  @var{addr} is an integer.  If no ELF
image is found, return @code{#f}.  It's possible for an bytecode program
not to have an ELF image if the program was defined in as a stub in C."
  (and=> (find-mapped-elf-image addr)
         debug-context-from-image))

(define-inlinable (binary-search start end inc try failure)
  (let lp ((start start) (end end))
    (if (eqv? start end)
        (failure)
        (let ((mid (+ start (* inc (floor/ (- end start) (* 2 inc))))))
          (try mid
               (lambda ()
                 (lp start mid))
               (lambda ()
                 (lp (+ mid inc) end)))))))

(define (find-elf-symbol elf text-offset)
  "Search the symbol table of @var{elf} for the ELF symbol containing
@var{text-offset}.  @var{text-offset} is a byte offset in the text
section of the ELF image.  Returns an ELF symbol, or @code{#f}."
  (and=>
   (elf-section-by-name elf ".symtab")
   (lambda (symtab)
     (let ((strtab (elf-section elf (elf-section-link symtab))))
       (binary-search
        0 (elf-symbol-table-len symtab) 1
        (lambda (n continue-before continue-after)
          (let* ((sym (elf-symbol-table-ref elf symtab n strtab))
                 (val (elf-symbol-value sym))
                 (size (elf-symbol-size sym)))
            (cond
             ((< text-offset val) (continue-before))
             ((<= (+ val size) text-offset) (continue-after))
             (else sym))))
        (lambda ()
          #f))))))

(define* (find-program-debug-info addr #:optional
                                  (context (find-debug-context addr)))
  "Find and return the @code{<program-debug-info>} containing
@var{addr}, or @code{#f}."
  (cond
   ((and context
         (find-elf-symbol (debug-context-elf context)
                          (- addr
                             (debug-context-base context)
                             (debug-context-text-base context))))
    => (lambda (sym)
         (make-program-debug-info context
                                  (and=> (elf-symbol-name sym)
                                         ;; The name might be #f if
                                         ;; the string table was
                                         ;; stripped somehow.
                                         (lambda (x)
                                           (and (string? x)
                                                (not (string-null? x))
                                                (string->symbol x))))
                                  (elf-symbol-value sym)
                                  (elf-symbol-size sym))))
   (else #f)))

(define-record-type <arity>
  (make-arity context base header-offset)
  arity?
  (context arity-context)
  (base arity-base)
  (header-offset arity-header-offset))

(define arities-prefix-len 4)
(define arity-header-len (* 7 4))

;;;   struct arity_header {
;;;     uint32_t low_pc;
;;;     uint32_t high_pc;
;;;     uint32_t offset;
;;;     uint32_t flags;
;;;     uint32_t nreq;
;;;     uint32_t nopt;
;;;     uint32_t nlocals;
;;;   }

(define (arity-low-pc* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 0 4))))
(define (arity-high-pc* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 1 4))))
(define (arity-offset* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 2 4))))
(define (arity-flags* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 3 4))))
(define (arity-nreq* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 4 4))))
(define (arity-nopt* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 5 4))))
(define (arity-nlocals* bv header-pos)
  (bytevector-u32-native-ref bv (+ header-pos (* 6 4))))

;;;    #x1: has-rest?
;;;    #x2: allow-other-keys?
;;;    #x4: has-keyword-args?
;;;    #x8: is-case-lambda?
;;;   #x10: is-in-case-lambda?
;;;   #x20: elided-closure?

(define (has-rest? flags)         (not (zero? (logand flags (ash 1 0)))))
(define (allow-other-keys? flags) (not (zero? (logand flags (ash 1 1)))))
(define (has-keyword-args? flags) (not (zero? (logand flags (ash 1 2)))))
(define (is-case-lambda? flags)   (not (zero? (logand flags (ash 1 3)))))
(define (is-in-case-lambda? flags) (not (zero? (logand flags (ash 1 4)))))
(define (elided-closure? flags)   (not (zero? (logand flags (ash 1 5)))))

(define (arity-low-pc arity)
  (let ((ctx (arity-context arity)))
    (+ (debug-context-base ctx)
       (debug-context-text-base ctx)
       (arity-low-pc* (elf-bytes (debug-context-elf ctx))
                      (arity-header-offset arity)))))

(define (arity-high-pc arity)
  (let ((ctx (arity-context arity)))
    (+ (debug-context-base ctx)
       (debug-context-text-base ctx)
       (arity-high-pc* (elf-bytes (debug-context-elf ctx))
                       (arity-header-offset arity)))))

(define (arity-nreq arity)
  (arity-nreq* (elf-bytes (debug-context-elf (arity-context arity)))
               (arity-header-offset arity)))

(define (arity-nopt arity)
  (arity-nopt* (elf-bytes (debug-context-elf (arity-context arity)))
               (arity-header-offset arity)))

(define (arity-nlocals arity)
  (arity-nlocals* (elf-bytes (debug-context-elf (arity-context arity)))
                  (arity-header-offset arity)))

(define (arity-flags arity)
  (arity-flags* (elf-bytes (debug-context-elf (arity-context arity)))
                (arity-header-offset arity)))

(define (arity-has-closure? arity) (not (elided-closure? (arity-flags arity))))
(define (arity-has-rest? arity) (has-rest? (arity-flags arity)))
(define (arity-allow-other-keys? arity) (allow-other-keys? (arity-flags arity)))
(define (arity-has-keyword-args? arity) (has-keyword-args? (arity-flags arity)))
(define (arity-is-case-lambda? arity) (is-case-lambda? (arity-flags arity)))
(define (arity-is-in-case-lambda? arity) (is-in-case-lambda? (arity-flags arity)))

(define (arity-keyword-args arity)
  (define (unpack-scm n)
    (pointer->scm (make-pointer n)))
  (if (arity-has-keyword-args? arity)
      (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
             (header (arity-header-offset arity))
             (link-offset (arity-offset* bv header))
             (link (+ (arity-base arity) link-offset))
             (offset (bytevector-u32-native-ref bv link)))
        (unpack-scm (+ (debug-context-base (arity-context arity)) offset)))
      '()))

(define (arity-load-symbol arity)
  (let ((elf (debug-context-elf (arity-context arity))))
    (cond
     ((elf-section-by-name elf ".guile.arities")
      =>
      (lambda (sec)
        (let* ((strtab (elf-section elf (elf-section-link sec)))
               (bv (elf-bytes elf))
               (strtab-offset (elf-section-offset strtab)))
          (lambda (n)
            (string->symbol (string-table-ref bv (+ strtab-offset n)))))))
     (else (error "couldn't find arities section")))))

(define* (arity-definitions arity)
  (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
         (load-symbol (arity-load-symbol arity))
         (header (arity-header-offset arity))
         (nlocals (arity-nlocals* bv header))
         (flags (arity-flags* bv header))
         (link-offset (arity-offset* bv header))
         (link (+ (arity-base arity)
                  link-offset
                  (if (has-keyword-args? flags) 4 0))))
    (define (read-uleb128 bv pos)
      ;; Unrolled by one.
      (let ((b (bytevector-u8-ref bv pos)))
        (if (zero? (logand b #x80))
            (values b
                    (1+ pos))
            (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
              (let ((b (bytevector-u8-ref bv pos)))
                (if (zero? (logand b #x80))
                    (values (logior (ash b shift) n)
                            (1+ pos))
                    (lp (logior (ash (logxor #x80 b) shift) n)
                        (1+ pos)
                        (+ shift 7))))))))
    (define (load-definitions pos names)
      (let lp ((pos pos) (names names))
        (match names
          (() '())
          ((name . names)
           (call-with-values (lambda () (read-uleb128 bv pos))
             (lambda (def-offset pos)
               (call-with-values (lambda () (read-uleb128 bv pos))
                 (lambda (slot+representation pos)
                   (let ((slot (ash slot+representation -3))
                         (representation (case (logand slot+representation #x7)
                                           ((0) 'scm)
                                           ((1) 'f64)
                                           ((2) 'u64)
                                           ((3) 's64)
                                           ((4) 'ptr)
                                           (else 'unknown))))
                     (cons (vector name def-offset slot representation)
                           (lp pos names)))))))))))
    (define (load-symbols pos)
      (let lp ((pos pos) (n nlocals) (out '()))
        (if (zero? n)
            (load-definitions pos (reverse out))
            (call-with-values (lambda () (read-uleb128 bv pos))
              (lambda (strtab-offset pos)
                strtab-offset
                (lp pos
                    (1- n)
                    (cons (if (zero? strtab-offset)
                              #f
                              (load-symbol strtab-offset))
                          out)))))))
    (when (is-case-lambda? flags)
      (error "invalid request for definitions of case-lambda wrapper arity"))
    (load-symbols link)))

(define (arity-code arity)
  (let* ((ctx (arity-context arity))
         (bv (elf-bytes (debug-context-elf ctx)))
         (header (arity-header-offset arity))
         (base-addr (+ (debug-context-base ctx) (debug-context-text-base ctx)))
         (low-pc (+ base-addr (arity-low-pc* bv header)))
         (high-pc (+ base-addr (arity-high-pc* bv header))))
    ;; FIXME: We should be able to use a sub-bytevector operation here;
    ;; it would be safer.
    (pointer->bytevector (make-pointer low-pc) (- high-pc low-pc))))

(define* (arity-locals arity #:optional nlocals)
  (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
         (load-symbol (arity-load-symbol arity))
         (header (arity-header-offset arity))
         (nlocals (if nlocals
                      (if (<= 0 nlocals (arity-nlocals* bv header))
                          nlocals
                          (error "request for too many locals"))
                      (arity-nlocals* bv header)))
         (flags (arity-flags* bv header))
         (link-offset (arity-offset* bv header))
         (link (+ (arity-base arity)
                  link-offset
                  (if (has-keyword-args? flags) 4 0))))
    (define (read-uleb128 bv pos)
      ;; Unrolled by one.
      (let ((b (bytevector-u8-ref bv pos)))
        (if (zero? (logand b #x80))
            (values b
                    (1+ pos))
            (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
              (let ((b (bytevector-u8-ref bv pos)))
                (if (zero? (logand b #x80))
                    (values (logior (ash b shift) n)
                            (1+ pos))
                    (lp (logior (ash (logxor #x80 b) shift) n)
                        (1+ pos)
                        (+ shift 7))))))))
    (define (load-symbols pos n)
      (let lp ((pos pos) (n n) (out '()))
        (if (zero? n)
            (reverse out)
            (call-with-values (lambda () (read-uleb128 bv pos))
              (lambda (strtab-offset pos)
                strtab-offset
                (lp pos
                    (1- n)
                    (cons (if (zero? strtab-offset)
                              #f
                              (load-symbol strtab-offset))
                          out)))))))
    (when (is-case-lambda? flags)
      (error "invalid request for locals of case-lambda wrapper arity"))
    (load-symbols link nlocals)))

(define (arity-arguments-alist arity)
  (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
         (header (arity-header-offset arity))
         (flags (arity-flags* bv header))
         (nreq (arity-nreq* bv header))
         (nopt (arity-nopt* bv header))
         (nargs (+ nreq nopt (if (has-rest? flags) 1 0)))
         (nargs+closure (1+ nargs)))
    (when (is-case-lambda? flags)
      (error "invalid request for locals of case-lambda wrapper arity"))
    (match (arity-locals arity nargs+closure)
      ((closure . args)
       (call-with-values (lambda () (split-at args nreq))
         (lambda (req args)
           (call-with-values (lambda () (split-at args nopt))
             (lambda (opt args)
               `((required . ,req)
                 (optional . ,opt)
                 (keyword . ,(arity-keyword-args arity))
                 (allow-other-keys? . ,(allow-other-keys? flags))
                 (rest . ,(and (has-rest? flags) (car args))))))))))))

(define (find-first-arity context base addr)
  (let* ((bv (elf-bytes (debug-context-elf context)))
         (text-offset (- addr
                         (debug-context-text-base context)
                         (debug-context-base context))))
    (binary-search
     (+ base arities-prefix-len)
     (+ base (bytevector-u32-native-ref bv base))
     arity-header-len
     (lambda (pos continue-before continue-after)
       (let lp ((pos pos))
         (cond
          ((is-in-case-lambda? (arity-flags* bv pos))
           (lp (- pos arity-header-len)))
          ((< text-offset (arity-low-pc* bv pos))
           (continue-before))
          ((<= (arity-high-pc* bv pos) text-offset)
           (continue-after))
          (else
           (make-arity context base pos)))))
     (lambda ()
       #f))))

(define (read-sub-arities context base outer-header-offset)
  (let* ((bv (elf-bytes (debug-context-elf context)))
         (headers-end (+ base (bytevector-u32-native-ref bv base)))
         (low-pc (arity-low-pc* bv outer-header-offset))
         (high-pc (arity-high-pc* bv outer-header-offset)))
    (let lp ((pos (+ outer-header-offset arity-header-len)) (out '()))
      (if (and (< pos headers-end) (<= (arity-high-pc* bv pos) high-pc))
          (lp (+ pos arity-header-len)
              (cons (make-arity context base pos) out))
          (reverse out)))))

(define* (find-program-arities addr #:optional
                               (context (find-debug-context addr)))
  (and=>
   (and context
        (elf-section-by-name (debug-context-elf context) ".guile.arities"))
   (lambda (sec)
     (let* ((base (elf-section-offset sec))
            (first (find-first-arity context base addr)))
       (cond
        ((not first) '())
        ((arity-is-case-lambda? first)
         (read-sub-arities context base (arity-header-offset first)))
        (else (list first)))))))

(define* (find-program-arity addr #:optional
                             (context (find-debug-context addr)))
  (let lp ((arities (or (find-program-arities addr context) '())))
    (match arities
      (() #f)
      ((arity . arities)
       (if (and (<= (arity-low-pc arity) addr)
                (< addr (arity-high-pc arity)))
           arity
           (lp arities))))))

(define* (find-program-minimum-arity addr #:optional
                                     (context (find-debug-context addr)))
  (and=>
   (and context
        (elf-section-by-name (debug-context-elf context) ".guile.arities"))
   (lambda (sec)
     (let* ((base (elf-section-offset sec))
            (first (find-first-arity context base addr)))
       (if (arity-is-case-lambda? first)
           (let ((arities (read-sub-arities context base
                                            (arity-header-offset first))))
             (and (pair? arities)
                  (list (apply min (map arity-nreq arities))
                        0
                        (or-map (lambda (arity)
                                  (or (positive? (arity-nopt arity))
                                      (arity-has-rest? arity)
                                      (arity-has-keyword-args? arity)
                                      (arity-allow-other-keys? arity)))
                                arities))))
           (list (arity-nreq first)
                 (arity-nopt first)
                 (arity-has-rest? first)))))))

(define* (find-program-docstring addr #:optional
                                 (context (find-debug-context addr)))
  (and=>
   (and context
        (elf-section-by-name (debug-context-elf context) ".guile.docstrs"))
   (lambda (sec)
     ;; struct docstr {
     ;;   uint32_t pc;
     ;;   uint32_t str;
     ;; }
     (let ((start (elf-section-offset sec))
           (bv (elf-bytes (debug-context-elf context)))
           (text-offset (- addr
                           (debug-context-text-base context)
                           (debug-context-base context))))
       (binary-search
        start
        (+ start (elf-section-size sec))
        8
        (lambda (pos continue-before continue-after)
          (let ((pc (bytevector-u32-native-ref bv pos)))
            (cond
             ((< text-offset pc) (continue-before))
             ((< pc text-offset) (continue-after))
             (else
              (let ((strtab (elf-section (debug-context-elf context)
                                         (elf-section-link sec)))
                    (idx (bytevector-u32-native-ref bv (+ pos 4))))
                (string-table-ref bv (+ (elf-section-offset strtab) idx)))))))
        (lambda ()
          #f))))))

(define* (find-program-properties addr #:optional
                                  (context (find-debug-context addr)))
  (define (add-name-and-docstring props)
    (define (maybe-acons k v tail)
      (if v (acons k v tail) tail))
    (let ((name (and=> (find-program-debug-info addr context)
                       program-debug-info-name))
          (docstring (find-program-docstring addr context)))
      (maybe-acons 'name name
                   (maybe-acons 'documentation docstring props))))
  (add-name-and-docstring
   (cond
    ((and context
          (elf-section-by-name (debug-context-elf context) ".guile.procprops"))
     => (lambda (sec)
          ;; struct procprop {
          ;;   uint32_t pc;
          ;;   uint32_t offset;
          ;; }
          (define procprop-len 8)
          (let* ((start (elf-section-offset sec))
                 (bv (elf-bytes (debug-context-elf context)))
                 (text-offset (- addr
                                 (debug-context-text-base context)
                                 (debug-context-base context))))
            (define (unpack-scm addr)
              (pointer->scm (make-pointer addr)))
            (define (load-non-immediate offset)
              (unpack-scm (+ (debug-context-base context) offset)))
            (binary-search
             start (+ start (elf-section-size sec)) 8
             (lambda (pos continue-before continue-after)
               (let ((pc (bytevector-u32-native-ref bv pos)))
                 (cond
                  ((< text-offset pc) (continue-before))
                  ((< pc text-offset) (continue-after))
                  (else
                   (load-non-immediate
                    (bytevector-u32-native-ref bv (+ pos 4)))))))
             (lambda ()
               '())))))
    (else '()))))

(define-record-type <source>
  (make-source pre-pc file line column)
  source?
  (pre-pc source-pre-pc)
  (file source-file)
  (line source-line)
  (column source-column))

(define (make-source/dwarf pc file line column)
  (make-source pc file
               ;; Convert DWARF-numbered (1-based) lines and
               ;; columns to Guile conventions (0-based).
               (and line (1- line)) (and column (1- column))))

;; FIXME
(define (source-post-pc source)
  (source-pre-pc source))

;; Lines are zero-indexed inside Guile, but users expect them to be
;; one-indexed. Columns, on the other hand, are zero-indexed to both. Go
;; figure.
(define (source-line-for-user source)
  (and (source-line source) (1+ (source-line source))))

(define* (find-source-for-addr addr #:optional
                               (context (find-debug-context addr))
                               #:key exact?)
  (and=>
   (and context
        (false-if-exception
         (elf->dwarf-context (debug-context-elf context))))
   (lambda (dwarf-ctx)
     (let* ((base (debug-context-base context))
            (pc (- addr base)))
       (or-map (lambda (die)
                 (and=>
                  (die-line-prog die)
                  (lambda (prog)
                    (call-with-values
                        (lambda () (line-prog-scan-to-pc prog pc))
                      (lambda (pc* file line col)
                        (and pc* (or (= pc pc*) (not exact?))
                             (make-source/dwarf (+ pc* base)
                                                file line col)))))))
               (read-die-roots dwarf-ctx))))))

(define* (find-program-die addr #:optional
                           (context (find-debug-context addr)))
  (and=> (and context
              (false-if-exception
               (elf->dwarf-context (debug-context-elf context))))
         (lambda (dwarf-ctx)
           (find-die-by-pc (read-die-roots dwarf-ctx)
                           (- addr (debug-context-base context))))))

(define* (find-program-sources addr #:optional
                               (context (find-debug-context addr)))
  (cond
   ((find-program-die addr context)
    => (lambda (die)
         (let* ((base (debug-context-base context))
                (low-pc (die-ref die 'low-pc))
                (high-pc (die-high-pc die))
                (prog (let line-prog ((die die))
                        (and die
                             (or (die-line-prog die)
                                 (line-prog (ctx-die (die-ctx die))))))))
           (cond
            ((and low-pc high-pc prog)
             (let lp ((sources '()))
               (call-with-values (lambda ()
                                   (if (null? sources)
                                       (line-prog-scan-to-pc prog low-pc)
                                       (line-prog-advance prog)))
                 (lambda (pc file line col)
                   (if (and pc (< pc high-pc))
                       ;; For the first source, it's probable that the
                       ;; address of the line program is before the
                       ;; low-pc, since the line program is for the
                       ;; entire compilation unit, and there are no
                       ;; redundant "rows" in the line program.
                       ;; Therefore in that case use the addr of low-pc
                       ;; instead of the one we got back.
                       (let ((addr (+ (if (null? sources) low-pc pc) base)))
                         (lp (cons (make-source/dwarf addr file line col)
                                   sources)))
                       (reverse sources))))))
            (else '())))))
   (else '())))

(define* (fold-source-locations proc seed context)
  "Fold @var{proc} over all source locations in @var{context}.
@var{proc} will be called with two arguments: the source object and the
seed."
  (cond
   ((and context
         (false-if-exception
          (elf->dwarf-context (debug-context-elf context))))
    =>
    (lambda (dwarf-ctx)
      (let ((base (debug-context-base context)))
        (fold
         (lambda (die seed)
           (cond
            ((die-line-prog die)
             =>
             (lambda (prog)
               (let lp ((seed seed))
                 (call-with-values
                     (lambda () (line-prog-advance prog))
                   (lambda (pc* file line col)
                     (if pc*
                         (lp
                          (proc (make-source/dwarf (+ pc* base) file line col)
                                seed))
                         seed))))))
            (else seed)))
         seed
         (read-die-roots dwarf-ctx)))))
   (else seed)))