debug.scm 30 KB

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