debug.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. ;;; Guile runtime debug information
  2. ;;; Copyright (C) 2013, 2014, 2015, 2018 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public
  6. ;;; License as published by the Free Software Foundation; either
  7. ;;; version 3 of the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this library; if not, write to the Free Software
  16. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Commentary:
  18. ;;;
  19. ;;; Guile's bytecode compiler and linker serialize debugging information
  20. ;;; into separate sections of the ELF image. This module reads those
  21. ;;; sections.
  22. ;;;
  23. ;;; Code:
  24. (define-module (system vm debug)
  25. #:use-module (system vm elf)
  26. #:use-module (system vm dwarf)
  27. #:use-module (system vm loader)
  28. #:use-module (system foreign)
  29. #:use-module (rnrs bytevectors)
  30. #:use-module (ice-9 match)
  31. #:use-module ((srfi srfi-1) #:select (fold split-at))
  32. #:use-module (srfi srfi-9)
  33. #:export (debug-context-image
  34. debug-context-base
  35. debug-context-length
  36. debug-context-text-base
  37. program-debug-info-name
  38. program-debug-info-context
  39. program-debug-info-image
  40. program-debug-info-offset
  41. program-debug-info-size
  42. program-debug-info-addr
  43. program-debug-info-u32-offset
  44. program-debug-info-u32-offset-end
  45. arity?
  46. arity-low-pc
  47. arity-high-pc
  48. arity-nreq
  49. arity-nopt
  50. arity-nlocals
  51. arity-has-rest?
  52. arity-allow-other-keys?
  53. arity-has-keyword-args?
  54. arity-keyword-args
  55. arity-is-case-lambda?
  56. arity-definitions
  57. arity-code
  58. debug-context-from-image
  59. fold-all-debug-contexts
  60. for-each-elf-symbol
  61. find-debug-context
  62. find-program-debug-info
  63. arity-arguments-alist
  64. find-program-arities
  65. find-program-arity
  66. find-program-minimum-arity
  67. find-program-docstring
  68. find-program-properties
  69. source?
  70. source-pre-pc
  71. source-post-pc
  72. source-file
  73. source-line
  74. source-line-for-user
  75. source-column
  76. find-source-for-addr
  77. find-program-sources
  78. fold-source-locations))
  79. ;;; A compiled procedure comes from a specific loaded ELF image. A
  80. ;;; debug context identifies that image.
  81. ;;;
  82. (define-record-type <debug-context>
  83. (make-debug-context elf base text-base)
  84. debug-context?
  85. (elf debug-context-elf)
  86. ;; Address at which this image is loaded in memory, in bytes.
  87. (base debug-context-base)
  88. ;; Offset of the text section relative to the image start, in bytes.
  89. (text-base debug-context-text-base))
  90. (define (debug-context-image context)
  91. "Return the bytevector aliasing the mapped ELF image corresponding to
  92. @var{context}."
  93. (elf-bytes (debug-context-elf context)))
  94. (define (debug-context-length context)
  95. "Return the size of the mapped ELF image corresponding to
  96. @var{context}, in bytes."
  97. (bytevector-length (debug-context-image context)))
  98. (define (for-each-elf-symbol context proc)
  99. "Call @var{proc} on each symbol in the symbol table of @var{context}."
  100. (let ((elf (debug-context-elf context)))
  101. (cond
  102. ((elf-section-by-name elf ".symtab")
  103. => (lambda (symtab)
  104. (let ((len (elf-symbol-table-len symtab))
  105. (strtab (elf-section elf (elf-section-link symtab))))
  106. (let lp ((n 0))
  107. (when (< n len)
  108. (proc (elf-symbol-table-ref elf symtab n strtab))
  109. (lp (1+ n))))))))))
  110. ;;; A program debug info (PDI) is a handle on debugging meta-data for a
  111. ;;; particular program.
  112. ;;;
  113. (define-record-type <program-debug-info>
  114. (make-program-debug-info context name offset size)
  115. program-debug-info?
  116. (context program-debug-info-context)
  117. (name program-debug-info-name)
  118. ;; Offset of the procedure in the text section, in bytes.
  119. (offset program-debug-info-offset)
  120. (size program-debug-info-size))
  121. (define (program-debug-info-addr pdi)
  122. "Return the address in memory of the entry of the program represented
  123. by the debugging info @var{pdi}."
  124. (+ (program-debug-info-offset pdi)
  125. (debug-context-text-base (program-debug-info-context pdi))
  126. (debug-context-base (program-debug-info-context pdi))))
  127. (define (program-debug-info-image pdi)
  128. "Return the ELF image containing @var{pdi}, as a bytevector."
  129. (debug-context-image (program-debug-info-context pdi)))
  130. (define (program-debug-info-u32-offset pdi)
  131. "Return the start address of the program represented by @var{pdi}, as
  132. an offset from the beginning of the ELF image in 32-bit units."
  133. (/ (+ (program-debug-info-offset pdi)
  134. (debug-context-text-base (program-debug-info-context pdi)))
  135. 4))
  136. (define (program-debug-info-u32-offset-end pdi)
  137. "Return the end address of the program represented by @var{pdi}, as an
  138. offset from the beginning of the ELF image in 32-bit units."
  139. (/ (+ (program-debug-info-size pdi)
  140. (program-debug-info-offset pdi)
  141. (debug-context-text-base (program-debug-info-context pdi)))
  142. 4))
  143. (define (debug-context-from-image bv)
  144. "Build a debugging context corresponding to a given ELF image."
  145. (let* ((elf (parse-elf bv))
  146. (base (pointer-address (bytevector->pointer (elf-bytes elf))))
  147. (text-base (elf-section-offset
  148. (or (elf-section-by-name elf ".rtl-text")
  149. (error "ELF object has no text section")))))
  150. (make-debug-context elf base text-base)))
  151. (define (fold-all-debug-contexts proc seed)
  152. "Fold @var{proc} over debug contexts corresponding to all images that
  153. are mapped at the time this procedure is called. Any images mapped
  154. during the fold are omitted."
  155. (fold (lambda (image seed)
  156. (proc (debug-context-from-image image) seed))
  157. seed
  158. (all-mapped-elf-images)))
  159. (define (find-debug-context addr)
  160. "Find and return the debugging context corresponding to the ELF image
  161. containing the address @var{addr}. @var{addr} is an integer. If no ELF
  162. image is found, return @code{#f}. It's possible for an bytecode program
  163. not to have an ELF image if the program was defined in as a stub in C."
  164. (and=> (find-mapped-elf-image addr)
  165. debug-context-from-image))
  166. (define-inlinable (binary-search start end inc try failure)
  167. (let lp ((start start) (end end))
  168. (if (eqv? start end)
  169. (failure)
  170. (let ((mid (+ start (* inc (floor/ (- end start) (* 2 inc))))))
  171. (try mid
  172. (lambda ()
  173. (lp start mid))
  174. (lambda ()
  175. (lp (+ mid inc) end)))))))
  176. (define (find-elf-symbol elf text-offset)
  177. "Search the symbol table of @var{elf} for the ELF symbol containing
  178. @var{text-offset}. @var{text-offset} is a byte offset in the text
  179. section of the ELF image. Returns an ELF symbol, or @code{#f}."
  180. (and=>
  181. (elf-section-by-name elf ".symtab")
  182. (lambda (symtab)
  183. (let ((strtab (elf-section elf (elf-section-link symtab))))
  184. (binary-search
  185. 0 (elf-symbol-table-len symtab) 1
  186. (lambda (n continue-before continue-after)
  187. (let* ((sym (elf-symbol-table-ref elf symtab n strtab))
  188. (val (elf-symbol-value sym))
  189. (size (elf-symbol-size sym)))
  190. (cond
  191. ((< text-offset val) (continue-before))
  192. ((<= (+ val size) text-offset) (continue-after))
  193. (else sym))))
  194. (lambda ()
  195. #f))))))
  196. (define* (find-program-debug-info addr #:optional
  197. (context (find-debug-context addr)))
  198. "Find and return the @code{<program-debug-info>} containing
  199. @var{addr}, or @code{#f}."
  200. (cond
  201. ((and context
  202. (find-elf-symbol (debug-context-elf context)
  203. (- addr
  204. (debug-context-base context)
  205. (debug-context-text-base context))))
  206. => (lambda (sym)
  207. (make-program-debug-info context
  208. (and=> (elf-symbol-name sym)
  209. ;; The name might be #f if
  210. ;; the string table was
  211. ;; stripped somehow.
  212. (lambda (x)
  213. (and (string? x)
  214. (not (string-null? x))
  215. (string->symbol x))))
  216. (elf-symbol-value sym)
  217. (elf-symbol-size sym))))
  218. (else #f)))
  219. (define-record-type <arity>
  220. (make-arity context base header-offset)
  221. arity?
  222. (context arity-context)
  223. (base arity-base)
  224. (header-offset arity-header-offset))
  225. (define arities-prefix-len 4)
  226. (define arity-header-len (* 7 4))
  227. ;;; struct arity_header {
  228. ;;; uint32_t low_pc;
  229. ;;; uint32_t high_pc;
  230. ;;; uint32_t offset;
  231. ;;; uint32_t flags;
  232. ;;; uint32_t nreq;
  233. ;;; uint32_t nopt;
  234. ;;; uint32_t nlocals;
  235. ;;; }
  236. (define (arity-low-pc* bv header-pos)
  237. (bytevector-u32-native-ref bv (+ header-pos (* 0 4))))
  238. (define (arity-high-pc* bv header-pos)
  239. (bytevector-u32-native-ref bv (+ header-pos (* 1 4))))
  240. (define (arity-offset* bv header-pos)
  241. (bytevector-u32-native-ref bv (+ header-pos (* 2 4))))
  242. (define (arity-flags* bv header-pos)
  243. (bytevector-u32-native-ref bv (+ header-pos (* 3 4))))
  244. (define (arity-nreq* bv header-pos)
  245. (bytevector-u32-native-ref bv (+ header-pos (* 4 4))))
  246. (define (arity-nopt* bv header-pos)
  247. (bytevector-u32-native-ref bv (+ header-pos (* 5 4))))
  248. (define (arity-nlocals* bv header-pos)
  249. (bytevector-u32-native-ref bv (+ header-pos (* 6 4))))
  250. ;;; #x1: has-rest?
  251. ;;; #x2: allow-other-keys?
  252. ;;; #x4: has-keyword-args?
  253. ;;; #x8: is-case-lambda?
  254. ;;; #x10: is-in-case-lambda?
  255. (define (has-rest? flags) (not (zero? (logand flags (ash 1 0)))))
  256. (define (allow-other-keys? flags) (not (zero? (logand flags (ash 1 1)))))
  257. (define (has-keyword-args? flags) (not (zero? (logand flags (ash 1 2)))))
  258. (define (is-case-lambda? flags) (not (zero? (logand flags (ash 1 3)))))
  259. (define (is-in-case-lambda? flags) (not (zero? (logand flags (ash 1 4)))))
  260. (define (arity-low-pc arity)
  261. (let ((ctx (arity-context arity)))
  262. (+ (debug-context-base ctx)
  263. (debug-context-text-base ctx)
  264. (arity-low-pc* (elf-bytes (debug-context-elf ctx))
  265. (arity-header-offset arity)))))
  266. (define (arity-high-pc arity)
  267. (let ((ctx (arity-context arity)))
  268. (+ (debug-context-base ctx)
  269. (debug-context-text-base ctx)
  270. (arity-high-pc* (elf-bytes (debug-context-elf ctx))
  271. (arity-header-offset arity)))))
  272. (define (arity-nreq arity)
  273. (arity-nreq* (elf-bytes (debug-context-elf (arity-context arity)))
  274. (arity-header-offset arity)))
  275. (define (arity-nopt arity)
  276. (arity-nopt* (elf-bytes (debug-context-elf (arity-context arity)))
  277. (arity-header-offset arity)))
  278. (define (arity-nlocals arity)
  279. (arity-nlocals* (elf-bytes (debug-context-elf (arity-context arity)))
  280. (arity-header-offset arity)))
  281. (define (arity-flags arity)
  282. (arity-flags* (elf-bytes (debug-context-elf (arity-context arity)))
  283. (arity-header-offset arity)))
  284. (define (arity-has-rest? arity) (has-rest? (arity-flags arity)))
  285. (define (arity-allow-other-keys? arity) (allow-other-keys? (arity-flags arity)))
  286. (define (arity-has-keyword-args? arity) (has-keyword-args? (arity-flags arity)))
  287. (define (arity-is-case-lambda? arity) (is-case-lambda? (arity-flags arity)))
  288. (define (arity-is-in-case-lambda? arity) (is-in-case-lambda? (arity-flags arity)))
  289. (define (arity-keyword-args arity)
  290. (define (unpack-scm n)
  291. (pointer->scm (make-pointer n)))
  292. (if (arity-has-keyword-args? arity)
  293. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  294. (header (arity-header-offset arity))
  295. (link-offset (arity-offset* bv header))
  296. (link (+ (arity-base arity) link-offset))
  297. (offset (bytevector-u32-native-ref bv link)))
  298. (unpack-scm (+ (debug-context-base (arity-context arity)) offset)))
  299. '()))
  300. (define (arity-load-symbol arity)
  301. (let ((elf (debug-context-elf (arity-context arity))))
  302. (cond
  303. ((elf-section-by-name elf ".guile.arities")
  304. =>
  305. (lambda (sec)
  306. (let* ((strtab (elf-section elf (elf-section-link sec)))
  307. (bv (elf-bytes elf))
  308. (strtab-offset (elf-section-offset strtab)))
  309. (lambda (n)
  310. (string->symbol (string-table-ref bv (+ strtab-offset n)))))))
  311. (else (error "couldn't find arities section")))))
  312. (define* (arity-definitions arity)
  313. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  314. (load-symbol (arity-load-symbol arity))
  315. (header (arity-header-offset arity))
  316. (nlocals (arity-nlocals* bv header))
  317. (flags (arity-flags* bv header))
  318. (link-offset (arity-offset* bv header))
  319. (link (+ (arity-base arity)
  320. link-offset
  321. (if (has-keyword-args? flags) 4 0))))
  322. (define (read-uleb128 bv pos)
  323. ;; Unrolled by one.
  324. (let ((b (bytevector-u8-ref bv pos)))
  325. (if (zero? (logand b #x80))
  326. (values b
  327. (1+ pos))
  328. (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
  329. (let ((b (bytevector-u8-ref bv pos)))
  330. (if (zero? (logand b #x80))
  331. (values (logior (ash b shift) n)
  332. (1+ pos))
  333. (lp (logior (ash (logxor #x80 b) shift) n)
  334. (1+ pos)
  335. (+ shift 7))))))))
  336. (define (load-definitions pos names)
  337. (let lp ((pos pos) (names names))
  338. (match names
  339. (() '())
  340. ((name . names)
  341. (call-with-values (lambda () (read-uleb128 bv pos))
  342. (lambda (def-offset pos)
  343. (call-with-values (lambda () (read-uleb128 bv pos))
  344. (lambda (slot+representation pos)
  345. (let ((slot (ash slot+representation -3))
  346. (representation (case (logand slot+representation #x7)
  347. ((0) 'scm)
  348. ((1) 'f64)
  349. ((2) 'u64)
  350. ((3) 's64)
  351. ((4) 'ptr)
  352. (else 'unknown))))
  353. (cons (vector name def-offset slot representation)
  354. (lp pos names)))))))))))
  355. (define (load-symbols pos)
  356. (let lp ((pos pos) (n nlocals) (out '()))
  357. (if (zero? n)
  358. (load-definitions pos (reverse out))
  359. (call-with-values (lambda () (read-uleb128 bv pos))
  360. (lambda (strtab-offset pos)
  361. strtab-offset
  362. (lp pos
  363. (1- n)
  364. (cons (if (zero? strtab-offset)
  365. #f
  366. (load-symbol strtab-offset))
  367. out)))))))
  368. (when (is-case-lambda? flags)
  369. (error "invalid request for definitions of case-lambda wrapper arity"))
  370. (load-symbols link)))
  371. (define (arity-code arity)
  372. (let* ((ctx (arity-context arity))
  373. (bv (elf-bytes (debug-context-elf ctx)))
  374. (header (arity-header-offset arity))
  375. (base-addr (+ (debug-context-base ctx) (debug-context-text-base ctx)))
  376. (low-pc (+ base-addr (arity-low-pc* bv header)))
  377. (high-pc (+ base-addr (arity-high-pc* bv header))))
  378. ;; FIXME: We should be able to use a sub-bytevector operation here;
  379. ;; it would be safer.
  380. (pointer->bytevector (make-pointer low-pc) (- high-pc low-pc))))
  381. (define* (arity-locals arity #:optional nlocals)
  382. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  383. (load-symbol (arity-load-symbol arity))
  384. (header (arity-header-offset arity))
  385. (nlocals (if nlocals
  386. (if (<= 0 nlocals (arity-nlocals* bv header))
  387. nlocals
  388. (error "request for too many locals"))
  389. (arity-nlocals* bv header)))
  390. (flags (arity-flags* bv header))
  391. (link-offset (arity-offset* bv header))
  392. (link (+ (arity-base arity)
  393. link-offset
  394. (if (has-keyword-args? flags) 4 0))))
  395. (define (read-uleb128 bv pos)
  396. ;; Unrolled by one.
  397. (let ((b (bytevector-u8-ref bv pos)))
  398. (if (zero? (logand b #x80))
  399. (values b
  400. (1+ pos))
  401. (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
  402. (let ((b (bytevector-u8-ref bv pos)))
  403. (if (zero? (logand b #x80))
  404. (values (logior (ash b shift) n)
  405. (1+ pos))
  406. (lp (logior (ash (logxor #x80 b) shift) n)
  407. (1+ pos)
  408. (+ shift 7))))))))
  409. (define (load-symbols pos n)
  410. (let lp ((pos pos) (n n) (out '()))
  411. (if (zero? n)
  412. (reverse out)
  413. (call-with-values (lambda () (read-uleb128 bv pos))
  414. (lambda (strtab-offset pos)
  415. strtab-offset
  416. (lp pos
  417. (1- n)
  418. (cons (if (zero? strtab-offset)
  419. #f
  420. (load-symbol strtab-offset))
  421. out)))))))
  422. (when (is-case-lambda? flags)
  423. (error "invalid request for locals of case-lambda wrapper arity"))
  424. (load-symbols link nlocals)))
  425. (define (arity-arguments-alist arity)
  426. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  427. (header (arity-header-offset arity))
  428. (flags (arity-flags* bv header))
  429. (nreq (arity-nreq* bv header))
  430. (nopt (arity-nopt* bv header))
  431. (nargs (+ nreq nopt (if (has-rest? flags) 1 0)))
  432. (nargs+closure (1+ nargs)))
  433. (when (is-case-lambda? flags)
  434. (error "invalid request for locals of case-lambda wrapper arity"))
  435. (match (arity-locals arity nargs+closure)
  436. ((closure . args)
  437. (call-with-values (lambda () (split-at args nreq))
  438. (lambda (req args)
  439. (call-with-values (lambda () (split-at args nopt))
  440. (lambda (opt args)
  441. `((required . ,req)
  442. (optional . ,opt)
  443. (keyword . ,(arity-keyword-args arity))
  444. (allow-other-keys? . ,(allow-other-keys? flags))
  445. (rest . ,(and (has-rest? flags) (car args))))))))))))
  446. (define (find-first-arity context base addr)
  447. (let* ((bv (elf-bytes (debug-context-elf context)))
  448. (text-offset (- addr
  449. (debug-context-text-base context)
  450. (debug-context-base context))))
  451. (binary-search
  452. (+ base arities-prefix-len)
  453. (+ base (bytevector-u32-native-ref bv base))
  454. arity-header-len
  455. (lambda (pos continue-before continue-after)
  456. (let lp ((pos pos))
  457. (cond
  458. ((is-in-case-lambda? (arity-flags* bv pos))
  459. (lp (- pos arity-header-len)))
  460. ((< text-offset (arity-low-pc* bv pos))
  461. (continue-before))
  462. ((<= (arity-high-pc* bv pos) text-offset)
  463. (continue-after))
  464. (else
  465. (make-arity context base pos)))))
  466. (lambda ()
  467. #f))))
  468. (define (read-sub-arities context base outer-header-offset)
  469. (let* ((bv (elf-bytes (debug-context-elf context)))
  470. (headers-end (+ base (bytevector-u32-native-ref bv base)))
  471. (low-pc (arity-low-pc* bv outer-header-offset))
  472. (high-pc (arity-high-pc* bv outer-header-offset)))
  473. (let lp ((pos (+ outer-header-offset arity-header-len)) (out '()))
  474. (if (and (< pos headers-end) (<= (arity-high-pc* bv pos) high-pc))
  475. (lp (+ pos arity-header-len)
  476. (cons (make-arity context base pos) out))
  477. (reverse out)))))
  478. (define* (find-program-arities addr #:optional
  479. (context (find-debug-context addr)))
  480. (and=>
  481. (and context
  482. (elf-section-by-name (debug-context-elf context) ".guile.arities"))
  483. (lambda (sec)
  484. (let* ((base (elf-section-offset sec))
  485. (first (find-first-arity context base addr)))
  486. (cond
  487. ((not first) '())
  488. ((arity-is-case-lambda? first)
  489. (read-sub-arities context base (arity-header-offset first)))
  490. (else (list first)))))))
  491. (define* (find-program-arity addr #:optional
  492. (context (find-debug-context addr)))
  493. (let lp ((arities (or (find-program-arities addr context) '())))
  494. (match arities
  495. (() #f)
  496. ((arity . arities)
  497. (if (and (<= (arity-low-pc arity) addr)
  498. (< addr (arity-high-pc arity)))
  499. arity
  500. (lp arities))))))
  501. (define* (find-program-minimum-arity addr #:optional
  502. (context (find-debug-context addr)))
  503. (and=>
  504. (and context
  505. (elf-section-by-name (debug-context-elf context) ".guile.arities"))
  506. (lambda (sec)
  507. (let* ((base (elf-section-offset sec))
  508. (first (find-first-arity context base addr)))
  509. (if (arity-is-case-lambda? first)
  510. (let ((arities (read-sub-arities context base
  511. (arity-header-offset first))))
  512. (and (pair? arities)
  513. (list (apply min (map arity-nreq arities))
  514. 0
  515. (or-map (lambda (arity)
  516. (or (positive? (arity-nopt arity))
  517. (arity-has-rest? arity)
  518. (arity-has-keyword-args? arity)
  519. (arity-allow-other-keys? arity)))
  520. arities))))
  521. (list (arity-nreq first)
  522. (arity-nopt first)
  523. (arity-has-rest? first)))))))
  524. (define* (find-program-docstring addr #:optional
  525. (context (find-debug-context addr)))
  526. (and=>
  527. (and context
  528. (elf-section-by-name (debug-context-elf context) ".guile.docstrs"))
  529. (lambda (sec)
  530. ;; struct docstr {
  531. ;; uint32_t pc;
  532. ;; uint32_t str;
  533. ;; }
  534. (let ((start (elf-section-offset sec))
  535. (bv (elf-bytes (debug-context-elf context)))
  536. (text-offset (- addr
  537. (debug-context-text-base context)
  538. (debug-context-base context))))
  539. (binary-search
  540. start
  541. (+ start (elf-section-size sec))
  542. 8
  543. (lambda (pos continue-before continue-after)
  544. (let ((pc (bytevector-u32-native-ref bv pos)))
  545. (cond
  546. ((< text-offset pc) (continue-before))
  547. ((< pc text-offset) (continue-after))
  548. (else
  549. (let ((strtab (elf-section (debug-context-elf context)
  550. (elf-section-link sec)))
  551. (idx (bytevector-u32-native-ref bv (+ pos 4))))
  552. (string-table-ref bv (+ (elf-section-offset strtab) idx)))))))
  553. (lambda ()
  554. #f))))))
  555. (define* (find-program-properties addr #:optional
  556. (context (find-debug-context addr)))
  557. (define (add-name-and-docstring props)
  558. (define (maybe-acons k v tail)
  559. (if v (acons k v tail) tail))
  560. (let ((name (and=> (find-program-debug-info addr context)
  561. program-debug-info-name))
  562. (docstring (find-program-docstring addr context)))
  563. (maybe-acons 'name name
  564. (maybe-acons 'documentation docstring props))))
  565. (add-name-and-docstring
  566. (cond
  567. ((and context
  568. (elf-section-by-name (debug-context-elf context) ".guile.procprops"))
  569. => (lambda (sec)
  570. ;; struct procprop {
  571. ;; uint32_t pc;
  572. ;; uint32_t offset;
  573. ;; }
  574. (define procprop-len 8)
  575. (let* ((start (elf-section-offset sec))
  576. (bv (elf-bytes (debug-context-elf context)))
  577. (text-offset (- addr
  578. (debug-context-text-base context)
  579. (debug-context-base context))))
  580. (define (unpack-scm addr)
  581. (pointer->scm (make-pointer addr)))
  582. (define (load-non-immediate offset)
  583. (unpack-scm (+ (debug-context-base context) offset)))
  584. (binary-search
  585. start (+ start (elf-section-size sec)) 8
  586. (lambda (pos continue-before continue-after)
  587. (let ((pc (bytevector-u32-native-ref bv pos)))
  588. (cond
  589. ((< text-offset pc) (continue-before))
  590. ((< pc text-offset) (continue-after))
  591. (else
  592. (load-non-immediate
  593. (bytevector-u32-native-ref bv (+ pos 4)))))))
  594. (lambda ()
  595. '())))))
  596. (else '()))))
  597. (define-record-type <source>
  598. (make-source pre-pc file line column)
  599. source?
  600. (pre-pc source-pre-pc)
  601. (file source-file)
  602. (line source-line)
  603. (column source-column))
  604. (define (make-source/dwarf pc file line column)
  605. (make-source pc file
  606. ;; Convert DWARF-numbered (1-based) lines and
  607. ;; columns to Guile conventions (0-based).
  608. (and line (1- line)) (and column (1- column))))
  609. ;; FIXME
  610. (define (source-post-pc source)
  611. (source-pre-pc source))
  612. ;; Lines are zero-indexed inside Guile, but users expect them to be
  613. ;; one-indexed. Columns, on the other hand, are zero-indexed to both. Go
  614. ;; figure.
  615. (define (source-line-for-user source)
  616. (and (source-line source) (1+ (source-line source))))
  617. (define* (find-source-for-addr addr #:optional
  618. (context (find-debug-context addr))
  619. #:key exact?)
  620. (and=>
  621. (and context
  622. (false-if-exception
  623. (elf->dwarf-context (debug-context-elf context))))
  624. (lambda (dwarf-ctx)
  625. (let* ((base (debug-context-base context))
  626. (pc (- addr base)))
  627. (or-map (lambda (die)
  628. (and=>
  629. (die-line-prog die)
  630. (lambda (prog)
  631. (call-with-values
  632. (lambda () (line-prog-scan-to-pc prog pc))
  633. (lambda (pc* file line col)
  634. (and pc* (or (= pc pc*) (not exact?))
  635. (make-source/dwarf (+ pc* base)
  636. file line col)))))))
  637. (read-die-roots dwarf-ctx))))))
  638. (define* (find-program-die addr #:optional
  639. (context (find-debug-context addr)))
  640. (and=> (and context
  641. (false-if-exception
  642. (elf->dwarf-context (debug-context-elf context))))
  643. (lambda (dwarf-ctx)
  644. (find-die-by-pc (read-die-roots dwarf-ctx)
  645. (- addr (debug-context-base context))))))
  646. (define* (find-program-sources addr #:optional
  647. (context (find-debug-context addr)))
  648. (cond
  649. ((find-program-die addr context)
  650. => (lambda (die)
  651. (let* ((base (debug-context-base context))
  652. (low-pc (die-ref die 'low-pc))
  653. (high-pc (die-high-pc die))
  654. (prog (let line-prog ((die die))
  655. (and die
  656. (or (die-line-prog die)
  657. (line-prog (ctx-die (die-ctx die))))))))
  658. (cond
  659. ((and low-pc high-pc prog)
  660. (let lp ((sources '()))
  661. (call-with-values (lambda ()
  662. (if (null? sources)
  663. (line-prog-scan-to-pc prog low-pc)
  664. (line-prog-advance prog)))
  665. (lambda (pc file line col)
  666. (if (and pc (< pc high-pc))
  667. ;; For the first source, it's probable that the
  668. ;; address of the line program is before the
  669. ;; low-pc, since the line program is for the
  670. ;; entire compilation unit, and there are no
  671. ;; redundant "rows" in the line program.
  672. ;; Therefore in that case use the addr of low-pc
  673. ;; instead of the one we got back.
  674. (let ((addr (+ (if (null? sources) low-pc pc) base)))
  675. (lp (cons (make-source/dwarf addr file line col)
  676. sources)))
  677. (reverse sources))))))
  678. (else '())))))
  679. (else '())))
  680. (define* (fold-source-locations proc seed context)
  681. "Fold @var{proc} over all source locations in @var{context}.
  682. @var{proc} will be called with two arguments: the source object and the
  683. seed."
  684. (cond
  685. ((and context
  686. (false-if-exception
  687. (elf->dwarf-context (debug-context-elf context))))
  688. =>
  689. (lambda (dwarf-ctx)
  690. (let ((base (debug-context-base context)))
  691. (fold
  692. (lambda (die seed)
  693. (cond
  694. ((die-line-prog die)
  695. =>
  696. (lambda (prog)
  697. (let lp ((seed seed))
  698. (call-with-values
  699. (lambda () (line-prog-advance prog))
  700. (lambda (pc* file line col)
  701. (if pc*
  702. (lp
  703. (proc (make-source/dwarf (+ pc* base) file line col)
  704. seed))
  705. seed))))))
  706. (else seed)))
  707. seed
  708. (read-die-roots dwarf-ctx)))))
  709. (else seed)))