libguile-2.2-gdb.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. ;;; GDB debugging support for Guile.
  2. ;;;
  3. ;;; Copyright 2014, 2015 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; This program is free software; you can redistribute it and/or modify it
  6. ;;; under the terms of the GNU General Public License as published by
  7. ;;; the Free Software Foundation; either version 3 of the License, or (at
  8. ;;; your option) any later version.
  9. ;;;
  10. ;;; This program is distributed in the hope that it will be useful, but
  11. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;; GNU General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU General Public License
  16. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. (define-module (guile-gdb)
  18. #:use-module (system base types)
  19. #:use-module (system vm debug)
  20. #:use-module ((gdb) #:hide (symbol? frame?))
  21. #:use-module ((gdb) #:select ((symbol? . gdb:symbol?) (frame? . gdb:frame?)))
  22. #:use-module (gdb printing)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-41)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 binary-ports)
  28. #:export (%gdb-memory-backend
  29. display-vm-frames))
  30. ;;; Commentary:
  31. ;;;
  32. ;;; This file defines GDB extensions to pretty-print 'SCM' objects, and
  33. ;;; to walk Guile's virtual machine stack.
  34. ;;;
  35. ;;; This file is installed under a name that follows the convention that
  36. ;;; allows GDB to auto-load it anytime the user is debugging libguile
  37. ;;; (info "(gdb) objfile-gdbdotext file").
  38. ;;;
  39. ;;; Code:
  40. (define (type-name-from-descriptor descriptor-array type-number)
  41. "Return the name of the type TYPE-NUMBER as seen in DESCRIPTOR-ARRAY, or #f
  42. if the information is not available."
  43. (let ((descriptors (lookup-global-symbol descriptor-array)))
  44. (and descriptors
  45. (let ((code (type-code (symbol-type descriptors))))
  46. (or (= TYPE_CODE_ARRAY code)
  47. (= TYPE_CODE_PTR code)))
  48. (let* ((type-descr (value-subscript (symbol-value descriptors)
  49. type-number))
  50. (name (value-field type-descr "name")))
  51. (value->string name)))))
  52. (define %gdb-memory-backend
  53. ;; The GDB back-end to access the inferior's memory.
  54. (let ((void* (type-pointer (lookup-type "void"))))
  55. (define (dereference-word address)
  56. ;; Return the word at ADDRESS.
  57. (value->integer
  58. (value-dereference (value-cast (make-value address)
  59. (type-pointer void*)))))
  60. (define (open address size)
  61. ;; Return a port to the SIZE bytes starting at ADDRESS.
  62. (if size
  63. (open-memory #:start address #:size size)
  64. (open-memory #:start address)))
  65. (define (type-name kind number)
  66. ;; Return the type name of KIND type NUMBER.
  67. (type-name-from-descriptor (case kind
  68. ((smob) "scm_smobs")
  69. ((port) "scm_ptobs"))
  70. number))
  71. (memory-backend dereference-word open type-name)))
  72. ;;;
  73. ;;; GDB pretty-printer registration.
  74. ;;;
  75. (define scm-value->string
  76. (lambda* (value #:optional (backend %gdb-memory-backend))
  77. "Return a representation of value VALUE as a string."
  78. (object->string (scm->object (value->integer value) backend))))
  79. (define (make-scm-pretty-printer-worker obj)
  80. (define (list->iterator list)
  81. (make-iterator list list
  82. (let ((n 0))
  83. (lambda (iter)
  84. (match (iterator-progress iter)
  85. (() (end-of-iteration))
  86. ((elt . list)
  87. (set-iterator-progress! iter list)
  88. (let ((name (format #f "[~a]" n)))
  89. (set! n (1+ n))
  90. (cons name (object->string elt)))))))))
  91. (cond
  92. ((string? obj)
  93. (make-pretty-printer-worker
  94. "string" ; display hint
  95. (lambda (printer) obj)
  96. #f))
  97. ((and (array? obj)
  98. (match (array-shape obj)
  99. (((0 _)) #t)
  100. (_ #f)))
  101. (make-pretty-printer-worker
  102. "array" ; display hint
  103. (lambda (printer)
  104. (let ((tag (array-type obj)))
  105. (case tag
  106. ((#t) "#<vector>")
  107. ((b) "#<bitvector>")
  108. (else (format #f "#<~avector>" tag)))))
  109. (lambda (printer)
  110. (list->iterator (array->list obj)))))
  111. ((inferior-struct? obj)
  112. (make-pretty-printer-worker
  113. "array" ; display hint
  114. (lambda (printer)
  115. (format #f "#<struct ~a>" (inferior-struct-name obj)))
  116. (lambda (printer)
  117. (list->iterator (inferior-struct-fields obj)))))
  118. (else
  119. (make-pretty-printer-worker
  120. #f ; display hint
  121. (lambda (printer)
  122. (object->string obj))
  123. #f))))
  124. (define %scm-pretty-printer
  125. (make-pretty-printer
  126. "SCM"
  127. (lambda (pp value)
  128. (let ((name (type-name (value-type value))))
  129. (and (and name (string=? name "SCM"))
  130. (make-scm-pretty-printer-worker
  131. (scm->object (value->integer value) %gdb-memory-backend)))))))
  132. (define* (register-pretty-printer #:optional objfile)
  133. (prepend-pretty-printer! objfile %scm-pretty-printer))
  134. (register-pretty-printer)
  135. ;;;
  136. ;;; VM stack walking.
  137. ;;;
  138. (define ip-type (type-pointer (lookup-type "scm_t_uint32")))
  139. (define fp-type (type-pointer (lookup-type "SCM")))
  140. (define sp-type (type-pointer (lookup-type "SCM")))
  141. (define-record-type <vm-frame>
  142. (make-vm-frame ip sp fp saved-ip saved-fp)
  143. vm-frame?
  144. (ip vm-frame-ip)
  145. (sp vm-frame-sp)
  146. (fp vm-frame-fp)
  147. (saved-ip vm-frame-saved-ip)
  148. (saved-fp vm-frame-saved-fp))
  149. ;; See libguile/frames.h.
  150. (define* (vm-frame ip sp fp #:optional (backend %gdb-memory-backend))
  151. "Return the components of the stack frame at FP."
  152. (make-vm-frame ip
  153. sp
  154. fp
  155. (value-dereference (value-cast (value-sub fp 1)
  156. (type-pointer ip-type)))
  157. (value-dereference (value-cast (value-sub fp 2)
  158. (type-pointer fp-type)))))
  159. (define (vm-engine-frame? frame)
  160. (let ((sym (frame-function frame)))
  161. (and sym
  162. (member (symbol-name sym)
  163. '("vm_debug_engine" "vm_regular_engine")))))
  164. (define (find-vp)
  165. "Find the scm_vm pointer for the current thread."
  166. (let loop ((frame (newest-frame)))
  167. (and frame
  168. (if (vm-engine-frame? frame)
  169. (frame-read-var frame "vp")
  170. (loop (frame-older frame))))))
  171. (define (newest-vm-frame)
  172. "Return the newest VM frame or #f."
  173. (let ((vp (find-vp)))
  174. (and vp
  175. (vm-frame (value-field vp "ip")
  176. (value-field vp "sp")
  177. (value-field vp "fp")))))
  178. (define* (vm-frame-older frame #:optional (backend %gdb-memory-backend))
  179. (let ((ip (vm-frame-saved-ip frame))
  180. (sp (value-sub (vm-frame-fp frame) 3))
  181. (fp (vm-frame-saved-fp frame)))
  182. (and (not (zero? (value->integer fp)))
  183. (vm-frame ip sp fp backend))))
  184. (define (vm-frames)
  185. "Return a SRFI-41 stream of the current VM frame stack."
  186. (stream-unfold identity
  187. vm-frame?
  188. vm-frame-older
  189. (newest-vm-frame)))
  190. (define (vm-frame-locals frame)
  191. (let ((fp (vm-frame-fp frame))
  192. (sp (vm-frame-sp frame)))
  193. (let lp ((slot 0) (ptr fp))
  194. (if (value<=? ptr sp)
  195. (acons (string-append "v" (number->string slot))
  196. (value-dereference ptr)
  197. (lp (1+ slot) (value-add ptr 1)))
  198. '()))))
  199. (define (lookup-symbol-or-false name)
  200. (match (lookup-symbol name)
  201. (#f #f)
  202. ((sym _) sym)))
  203. (define (find-mapped-elf-image addr)
  204. (let ((array (lookup-symbol-or-false "mapped_elf_images"))
  205. (count (lookup-symbol-or-false "mapped_elf_images_count")))
  206. (and array count
  207. (let ((array (symbol-value array))
  208. (count (value->integer (symbol-value count))))
  209. (let lp ((start 0) (end count))
  210. (if (< start end)
  211. (let ((n (+ start (ash (- end start) -1))))
  212. (if (value<? addr (value-field (value-add array n) "end"))
  213. (lp start n)
  214. (lp (1+ n) end)))
  215. (let ((mei (value-add array start)))
  216. (and (value<=? (value-field mei "start") addr)
  217. mei))))))))
  218. (define (vm-frame-program-debug-info frame)
  219. (let ((addr (vm-frame-ip frame)))
  220. (and=> (find-mapped-elf-image addr)
  221. (lambda (mei)
  222. (let* ((start (value->integer (value-field mei "start")))
  223. (size (- (value->integer (value-field mei "end"))
  224. start))
  225. (mem-port (open-memory #:start start #:size size))
  226. (bv (get-bytevector-all mem-port))
  227. (ctx (debug-context-from-image bv)))
  228. ;; The image is in this process at "bv", but in the
  229. ;; inferior at mei.start. Therefore we relocate addr
  230. ;; before we look for the PDI.
  231. (let ((addr (+ (value->integer addr)
  232. (- (debug-context-base ctx) start))))
  233. (find-program-debug-info addr ctx)))))))
  234. (define (vm-frame-function-name frame)
  235. (define (default-name)
  236. "[unknown]")
  237. (cond
  238. ((vm-frame-program-debug-info frame)
  239. => (lambda (pdi)
  240. (or (and=> (program-debug-info-name pdi) symbol->string)
  241. "[anonymous]")))
  242. (else
  243. (let ((ip (vm-frame-ip frame)))
  244. (define (ip-in-symbol? name)
  245. (let ((sym (lookup-symbol-or-false name)))
  246. (and sym
  247. (let* ((val (symbol-value sym))
  248. (size (type-sizeof (value-type val)))
  249. (char* (type-pointer (arch-char-type (current-arch))))
  250. (val-as-char* (value-cast val char*)))
  251. (and (value<=? val-as-char* ip)
  252. (value<? ip (value-add val-as-char* size)))))))
  253. (cond
  254. ((ip-in-symbol? "vm_boot_continuation_code") "[boot continuation]")
  255. ;; FIXME: For subrs, read the name from slot 0 in the frame.
  256. ((ip-in-symbol? "subr_stub_code") "[subr call]")
  257. ((ip-in-symbol? "vm_builtin_apply_code") "apply")
  258. ((ip-in-symbol? "vm_builtin_values_code") "values")
  259. ((ip-in-symbol? "vm_builtin_abort_to_prompt_code") "abort-to-prompt")
  260. ((ip-in-symbol? "vm_builtin_call_with_values_code") "call-with-values")
  261. ((ip-in-symbol? "vm_builtin_call_with_current_continuation_code")
  262. "call-with-current-continuation")
  263. ((ip-in-symbol? "continuation_stub_code") "[continuation]")
  264. ((ip-in-symbol? "compose_continuation_code") "[delimited continuation]")
  265. ((ip-in-symbol? "foreign_stub_code") "[ffi call]")
  266. (else (default-name)))))))
  267. (define (vm-frame-source frame)
  268. (let* ((ip (value->integer (vm-frame-ip frame)))
  269. (pdi (vm-frame-program-debug-info frame)))
  270. (and pdi
  271. (find-source-for-addr (program-debug-info-addr pdi)
  272. (program-debug-info-context pdi)))))
  273. (define* (dump-vm-frame frame #:optional (port (current-output-port)))
  274. (format port " name: ~a~%" (vm-frame-function-name frame))
  275. (format port " ip: 0x~x~%" (value->integer (vm-frame-ip frame)))
  276. (format port " fp: 0x~x~%" (value->integer (vm-frame-fp frame)))
  277. (for-each (match-lambda
  278. ((name . val)
  279. (let ((obj (scm->object (value->integer val) %gdb-memory-backend)))
  280. (format port " ~a: ~a~%" name obj))))
  281. (vm-frame-locals frame)))
  282. (define* (display-vm-frames #:optional (port (current-output-port)))
  283. "Display the VM frames on PORT."
  284. (stream-for-each (lambda (frame)
  285. (dump-vm-frame frame port))
  286. (vm-frames)))
  287. ;;;
  288. ;;; Frame filters.
  289. ;;;
  290. (define-syntax compile-time-cond
  291. (lambda (x)
  292. (syntax-case x ()
  293. ((_ (test body ...) clause ...)
  294. (if (eval (syntax->datum #'test) (current-module))
  295. #'(begin body ...)
  296. #'(compile-time-cond clause ...)))
  297. ((_)
  298. #'(begin)))))
  299. (compile-time-cond
  300. ((false-if-exception (resolve-interface '(gdb frame-filters)))
  301. (use-modules (gdb frame-filters))
  302. (define (snarfy-frame-decorator dec)
  303. (let* ((frame (decorated-frame-frame dec))
  304. (sym (frame-function frame)))
  305. (or
  306. (and sym
  307. (gdb:symbol? sym)
  308. (let ((c-name (symbol-name sym)))
  309. (match (lookup-symbol (string-append "s_" c-name))
  310. (#f #f)
  311. ((scheme-name-sym _)
  312. (and (string-prefix?
  313. "const char ["
  314. (type-print-name (symbol-type scheme-name-sym)))
  315. (let* ((scheme-name-value (symbol-value scheme-name-sym))
  316. (scheme-name (value->string scheme-name-value))
  317. (name (format #f "~a [~a]" scheme-name c-name)))
  318. (redecorate-frame dec #:function-name name)))))))
  319. dec)))
  320. (define* (vm-frame-filter gdb-frames #:optional (vm-frames (vm-frames)))
  321. (define (synthesize-frame gdb-frame vm-frame)
  322. (let* ((ip (value->integer (vm-frame-ip vm-frame)))
  323. (source (vm-frame-source vm-frame)))
  324. (redecorate-frame gdb-frame
  325. #:function-name (vm-frame-function-name vm-frame)
  326. #:address ip
  327. #:filename (and=> source source-file)
  328. #:line (and=> source source-line-for-user)
  329. #:arguments '()
  330. #:locals (vm-frame-locals vm-frame)
  331. #:children '())))
  332. (define (recur gdb-frame gdb-frames vm-frames)
  333. (stream-cons gdb-frame
  334. (vm-frame-filter gdb-frames vm-frames)))
  335. (cond
  336. ((or (stream-null? gdb-frames)
  337. (not (lookup-symbol "vm_boot_continuation_code")))
  338. gdb-frames)
  339. (else
  340. (let ((gdb-frame (stream-car gdb-frames))
  341. (gdb-frames (stream-cdr gdb-frames)))
  342. (match (lookup-symbol "vm_boot_continuation_code")
  343. ((boot-sym _)
  344. (let ((boot-ptr (symbol-value boot-sym)))
  345. (cond
  346. ((vm-engine-frame? (decorated-frame-frame gdb-frame))
  347. (let lp ((children (reverse
  348. (decorated-frame-children gdb-frame)))
  349. (vm-frames vm-frames))
  350. (define (finish reversed-children vm-frames)
  351. (let ((children (reverse reversed-children)))
  352. (recur (redecorate-frame gdb-frame #:children children)
  353. gdb-frames
  354. vm-frames)))
  355. (cond
  356. ((stream-null? vm-frames)
  357. (finish children vm-frames))
  358. (else
  359. (let* ((vm-frame (stream-car vm-frames))
  360. (vm-frames (stream-cdr vm-frames)))
  361. (if (value=? (vm-frame-ip vm-frame) boot-ptr)
  362. ;; Drop the boot frame and finish.
  363. (finish children vm-frames)
  364. (lp (cons (synthesize-frame gdb-frame vm-frame)
  365. children)
  366. vm-frames)))))))
  367. (else
  368. (recur gdb-frame gdb-frames vm-frames))))))))))
  369. (add-frame-filter!
  370. (make-decorating-frame-filter "guile-snarf-decorator"
  371. snarfy-frame-decorator
  372. #:objfile (current-objfile)))
  373. (add-frame-filter!
  374. (make-frame-filter "guile-vm-frame-filter"
  375. vm-frame-filter
  376. #:objfile (current-objfile))))
  377. (#t #f))
  378. ;;; libguile-2.2-gdb.scm ends here