libguile-2.2-gdb.scm 17 KB

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