debug.scm 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. ;;; Guile runtime debug information
  2. ;;; Copyright (C) 2013, 2014 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 pos)
  345. (cons (vector name def-offset slot)
  346. (lp pos names))))))))))
  347. (define (load-symbols pos)
  348. (let lp ((pos pos) (n nlocals) (out '()))
  349. (if (zero? n)
  350. (load-definitions pos (reverse out))
  351. (call-with-values (lambda () (read-uleb128 bv pos))
  352. (lambda (strtab-offset pos)
  353. strtab-offset
  354. (lp pos
  355. (1- n)
  356. (cons (if (zero? strtab-offset)
  357. #f
  358. (load-symbol strtab-offset))
  359. out)))))))
  360. (when (is-case-lambda? flags)
  361. (error "invalid request for definitions of case-lambda wrapper arity"))
  362. (load-symbols link)))
  363. (define (arity-code arity)
  364. (let* ((ctx (arity-context arity))
  365. (bv (elf-bytes (debug-context-elf ctx)))
  366. (header (arity-header-offset arity))
  367. (base-addr (+ (debug-context-base ctx) (debug-context-text-base ctx)))
  368. (low-pc (+ base-addr (arity-low-pc* bv header)))
  369. (high-pc (+ base-addr (arity-high-pc* bv header))))
  370. ;; FIXME: We should be able to use a sub-bytevector operation here;
  371. ;; it would be safer.
  372. (pointer->bytevector (make-pointer low-pc) (- high-pc low-pc))))
  373. (define* (arity-locals arity #:optional nlocals)
  374. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  375. (load-symbol (arity-load-symbol arity))
  376. (header (arity-header-offset arity))
  377. (nlocals (if nlocals
  378. (if (<= 0 nlocals (arity-nlocals* bv header))
  379. nlocals
  380. (error "request for too many locals"))
  381. (arity-nlocals* bv header)))
  382. (flags (arity-flags* bv header))
  383. (link-offset (arity-offset* bv header))
  384. (link (+ (arity-base arity)
  385. link-offset
  386. (if (has-keyword-args? flags) 4 0))))
  387. (define (read-uleb128 bv pos)
  388. ;; Unrolled by one.
  389. (let ((b (bytevector-u8-ref bv pos)))
  390. (if (zero? (logand b #x80))
  391. (values b
  392. (1+ pos))
  393. (let lp ((n (logxor #x80 b)) (pos (1+ pos)) (shift 7))
  394. (let ((b (bytevector-u8-ref bv pos)))
  395. (if (zero? (logand b #x80))
  396. (values (logior (ash b shift) n)
  397. (1+ pos))
  398. (lp (logior (ash (logxor #x80 b) shift) n)
  399. (1+ pos)
  400. (+ shift 7))))))))
  401. (define (load-symbols pos n)
  402. (let lp ((pos pos) (n n) (out '()))
  403. (if (zero? n)
  404. (reverse out)
  405. (call-with-values (lambda () (read-uleb128 bv pos))
  406. (lambda (strtab-offset pos)
  407. strtab-offset
  408. (lp pos
  409. (1- n)
  410. (cons (if (zero? strtab-offset)
  411. #f
  412. (load-symbol strtab-offset))
  413. out)))))))
  414. (when (is-case-lambda? flags)
  415. (error "invalid request for locals of case-lambda wrapper arity"))
  416. (load-symbols link nlocals)))
  417. (define (arity-arguments-alist arity)
  418. (let* ((bv (elf-bytes (debug-context-elf (arity-context arity))))
  419. (header (arity-header-offset arity))
  420. (flags (arity-flags* bv header))
  421. (nreq (arity-nreq* bv header))
  422. (nopt (arity-nopt* bv header))
  423. (nargs (+ nreq nopt (if (has-rest? flags) 1 0))))
  424. (when (is-case-lambda? flags)
  425. (error "invalid request for locals of case-lambda wrapper arity"))
  426. (let ((args (arity-locals arity nargs)))
  427. (call-with-values (lambda () (split-at args nreq))
  428. (lambda (req args)
  429. (call-with-values (lambda () (split-at args nopt))
  430. (lambda (opt args)
  431. `((required . ,req)
  432. (optional . ,opt)
  433. (keyword . ,(arity-keyword-args arity))
  434. (allow-other-keys? . ,(allow-other-keys? flags))
  435. (rest . ,(and (has-rest? flags) (car args)))))))))))
  436. (define (find-first-arity context base addr)
  437. (let* ((bv (elf-bytes (debug-context-elf context)))
  438. (text-offset (- addr
  439. (debug-context-text-base context)
  440. (debug-context-base context))))
  441. (binary-search
  442. (+ base arities-prefix-len)
  443. (+ base (bytevector-u32-native-ref bv base))
  444. arity-header-len
  445. (lambda (pos continue-before continue-after)
  446. (let lp ((pos pos))
  447. (cond
  448. ((is-in-case-lambda? (arity-flags* bv pos))
  449. (lp (- pos arity-header-len)))
  450. ((< text-offset (arity-low-pc* bv pos))
  451. (continue-before))
  452. ((<= (arity-high-pc* bv pos) text-offset)
  453. (continue-after))
  454. (else
  455. (make-arity context base pos)))))
  456. (lambda ()
  457. #f))))
  458. (define (read-sub-arities context base outer-header-offset)
  459. (let* ((bv (elf-bytes (debug-context-elf context)))
  460. (headers-end (+ base (bytevector-u32-native-ref bv base)))
  461. (low-pc (arity-low-pc* bv outer-header-offset))
  462. (high-pc (arity-high-pc* bv outer-header-offset)))
  463. (let lp ((pos (+ outer-header-offset arity-header-len)) (out '()))
  464. (if (and (< pos headers-end) (<= (arity-high-pc* bv pos) high-pc))
  465. (lp (+ pos arity-header-len)
  466. (cons (make-arity context base pos) out))
  467. (reverse out)))))
  468. (define* (find-program-arities addr #:optional
  469. (context (find-debug-context addr)))
  470. (and=>
  471. (and context
  472. (elf-section-by-name (debug-context-elf context) ".guile.arities"))
  473. (lambda (sec)
  474. (let* ((base (elf-section-offset sec))
  475. (first (find-first-arity context base addr)))
  476. (cond
  477. ((not first) '())
  478. ((arity-is-case-lambda? first)
  479. (read-sub-arities context base (arity-header-offset first)))
  480. (else (list first)))))))
  481. (define* (find-program-arity addr #:optional
  482. (context (find-debug-context addr)))
  483. (let lp ((arities (or (find-program-arities addr context) '())))
  484. (match arities
  485. (() #f)
  486. ((arity . arities)
  487. (if (and (<= (arity-low-pc arity) addr)
  488. (< addr (arity-high-pc arity)))
  489. arity
  490. (lp arities))))))
  491. (define* (find-program-minimum-arity addr #:optional
  492. (context (find-debug-context addr)))
  493. (and=>
  494. (and context
  495. (elf-section-by-name (debug-context-elf context) ".guile.arities"))
  496. (lambda (sec)
  497. (let* ((base (elf-section-offset sec))
  498. (first (find-first-arity context base addr)))
  499. (if (arity-is-case-lambda? first)
  500. (let ((arities (read-sub-arities context base
  501. (arity-header-offset first))))
  502. (and (pair? arities)
  503. (list (apply min (map arity-nreq arities))
  504. 0
  505. (or-map (lambda (arity)
  506. (or (positive? (arity-nopt arity))
  507. (arity-has-rest? arity)
  508. (arity-has-keyword-args? arity)
  509. (arity-allow-other-keys? arity)))
  510. arities))))
  511. (list (arity-nreq first)
  512. (arity-nopt first)
  513. (arity-has-rest? first)))))))
  514. (define* (find-program-docstring addr #:optional
  515. (context (find-debug-context addr)))
  516. (and=>
  517. (and context
  518. (elf-section-by-name (debug-context-elf context) ".guile.docstrs"))
  519. (lambda (sec)
  520. ;; struct docstr {
  521. ;; uint32_t pc;
  522. ;; uint32_t str;
  523. ;; }
  524. (let ((start (elf-section-offset sec))
  525. (bv (elf-bytes (debug-context-elf context)))
  526. (text-offset (- addr
  527. (debug-context-text-base context)
  528. (debug-context-base context))))
  529. (binary-search
  530. start
  531. (+ start (elf-section-size sec))
  532. 8
  533. (lambda (pos continue-before continue-after)
  534. (let ((pc (bytevector-u32-native-ref bv pos)))
  535. (cond
  536. ((< text-offset pc) (continue-before))
  537. ((< pc text-offset) (continue-after))
  538. (else
  539. (let ((strtab (elf-section (debug-context-elf context)
  540. (elf-section-link sec)))
  541. (idx (bytevector-u32-native-ref bv (+ pos 4))))
  542. (string-table-ref bv (+ (elf-section-offset strtab) idx)))))))
  543. (lambda ()
  544. #f))))))
  545. (define* (find-program-properties addr #:optional
  546. (context (find-debug-context addr)))
  547. (define (add-name-and-docstring props)
  548. (define (maybe-acons k v tail)
  549. (if v (acons k v tail) tail))
  550. (let ((name (and=> (find-program-debug-info addr context)
  551. program-debug-info-name))
  552. (docstring (find-program-docstring addr context)))
  553. (maybe-acons 'name name
  554. (maybe-acons 'documentation docstring props))))
  555. (add-name-and-docstring
  556. (cond
  557. ((and context
  558. (elf-section-by-name (debug-context-elf context) ".guile.procprops"))
  559. => (lambda (sec)
  560. ;; struct procprop {
  561. ;; uint32_t pc;
  562. ;; uint32_t offset;
  563. ;; }
  564. (define procprop-len 8)
  565. (let* ((start (elf-section-offset sec))
  566. (bv (elf-bytes (debug-context-elf context)))
  567. (text-offset (- addr
  568. (debug-context-text-base context)
  569. (debug-context-base context))))
  570. (define (unpack-scm addr)
  571. (pointer->scm (make-pointer addr)))
  572. (define (load-non-immediate offset)
  573. (unpack-scm (+ (debug-context-base context) offset)))
  574. (binary-search
  575. start (+ start (elf-section-size sec)) 8
  576. (lambda (pos continue-before continue-after)
  577. (let ((pc (bytevector-u32-native-ref bv pos)))
  578. (cond
  579. ((< text-offset pc) (continue-before))
  580. ((< pc text-offset) (continue-after))
  581. (else
  582. (load-non-immediate
  583. (bytevector-u32-native-ref bv (+ pos 4)))))))
  584. (lambda ()
  585. '())))))
  586. (else '()))))
  587. (define-record-type <source>
  588. (make-source pre-pc file line column)
  589. source?
  590. (pre-pc source-pre-pc)
  591. (file source-file)
  592. (line source-line)
  593. (column source-column))
  594. (define (make-source/dwarf pc file line column)
  595. (make-source pc file
  596. ;; Convert DWARF-numbered (1-based) lines and
  597. ;; columns to Guile conventions (0-based).
  598. (and line (1- line)) (and column (1- column))))
  599. ;; FIXME
  600. (define (source-post-pc source)
  601. (source-pre-pc source))
  602. ;; Lines are zero-indexed inside Guile, but users expect them to be
  603. ;; one-indexed. Columns, on the other hand, are zero-indexed to both. Go
  604. ;; figure.
  605. (define (source-line-for-user source)
  606. (and (source-line source) (1+ (source-line source))))
  607. (define* (find-source-for-addr addr #:optional
  608. (context (find-debug-context addr))
  609. #:key exact?)
  610. (and=>
  611. (and context
  612. (false-if-exception
  613. (elf->dwarf-context (debug-context-elf context))))
  614. (lambda (dwarf-ctx)
  615. (let* ((base (debug-context-base context))
  616. (pc (- addr base)))
  617. (or-map (lambda (die)
  618. (and=>
  619. (die-line-prog die)
  620. (lambda (prog)
  621. (call-with-values
  622. (lambda () (line-prog-scan-to-pc prog pc))
  623. (lambda (pc* file line col)
  624. (and pc* (or (= pc pc*) (not exact?))
  625. (make-source/dwarf (+ pc* base)
  626. file line col)))))))
  627. (read-die-roots dwarf-ctx))))))
  628. (define* (find-program-die addr #:optional
  629. (context (find-debug-context addr)))
  630. (and=> (and context
  631. (false-if-exception
  632. (elf->dwarf-context (debug-context-elf context))))
  633. (lambda (dwarf-ctx)
  634. (find-die-by-pc (read-die-roots dwarf-ctx)
  635. (- addr (debug-context-base context))))))
  636. (define* (find-program-sources addr #:optional
  637. (context (find-debug-context addr)))
  638. (cond
  639. ((find-program-die addr context)
  640. => (lambda (die)
  641. (let* ((base (debug-context-base context))
  642. (low-pc (die-ref die 'low-pc))
  643. (high-pc (die-high-pc die))
  644. (prog (let line-prog ((die die))
  645. (and die
  646. (or (die-line-prog die)
  647. (line-prog (ctx-die (die-ctx die))))))))
  648. (cond
  649. ((and low-pc high-pc prog)
  650. (let lp ((sources '()))
  651. (call-with-values (lambda ()
  652. (if (null? sources)
  653. (line-prog-scan-to-pc prog low-pc)
  654. (line-prog-advance prog)))
  655. (lambda (pc file line col)
  656. (if (and pc (< pc high-pc))
  657. ;; For the first source, it's probable that the
  658. ;; address of the line program is before the
  659. ;; low-pc, since the line program is for the
  660. ;; entire compilation unit, and there are no
  661. ;; redundant "rows" in the line program.
  662. ;; Therefore in that case use the addr of low-pc
  663. ;; instead of the one we got back.
  664. (let ((addr (+ (if (null? sources) low-pc pc) base)))
  665. (lp (cons (make-source/dwarf addr file line col)
  666. sources)))
  667. (reverse sources))))))
  668. (else '())))))
  669. (else '())))
  670. (define* (fold-source-locations proc seed context)
  671. "Fold @var{proc} over all source locations in @var{context}.
  672. @var{proc} will be called with two arguments: the source object and the
  673. seed."
  674. (cond
  675. ((and context
  676. (false-if-exception
  677. (elf->dwarf-context (debug-context-elf context))))
  678. =>
  679. (lambda (dwarf-ctx)
  680. (let ((base (debug-context-base context)))
  681. (fold
  682. (lambda (die seed)
  683. (cond
  684. ((die-line-prog die)
  685. =>
  686. (lambda (prog)
  687. (let lp ((seed seed))
  688. (call-with-values
  689. (lambda () (line-prog-advance prog))
  690. (lambda (pc* file line col)
  691. (if pc*
  692. (lp
  693. (proc (make-source/dwarf (+ pc* base) file line col)
  694. seed))
  695. seed))))))
  696. (else seed)))
  697. seed
  698. (read-die-roots dwarf-ctx)))))
  699. (else seed)))