repl.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. ;;; REPL commands
  2. ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Handy REPL commands for development.
  18. ;;;
  19. ;;; Code:
  20. (define-module (hoot repl)
  21. #:use-module (hoot reflect)
  22. #:use-module (ice-9 control)
  23. #:use-module (ice-9 match)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (system repl command)
  27. #:use-module (system repl common)
  28. #:use-module (system repl debug)
  29. #:use-module (system repl repl)
  30. #:use-module (wasm dump)
  31. #:use-module (wasm types)
  32. #:use-module (wasm vm))
  33. (define (for-each/index proc lst)
  34. (let loop ((lst lst) (i 0))
  35. (match lst
  36. (() *unspecified*)
  37. ((x . rest)
  38. (proc i x)
  39. (loop rest (+ i 1))))))
  40. (define (print-list proc title items)
  41. (format #t "~a:\n" title)
  42. (for-each/index (lambda (i item)
  43. (format #t " ~a:\t" i)
  44. (proc item))
  45. items))
  46. (define (print-stack stack)
  47. (match (wasm-stack-items stack)
  48. (() (display "Empty stack.\n"))
  49. (items
  50. (print-list (lambda (x) (format #t "~s\n" x))
  51. "Value stack"
  52. items))))
  53. (define (print-locals locals)
  54. (if (zero? (vector-length locals))
  55. (display "No locals.\n")
  56. (print-list (lambda (x) (format #t "~s\n" x))
  57. "Locals"
  58. (vector->list locals))))
  59. (define (print-runtime-error e)
  60. (print-exception (current-output-port) #f
  61. (exception-kind e)
  62. (exception-args e))
  63. (newline)
  64. (print-stack (wasm-runtime-error-stack e))
  65. (newline)
  66. (print-locals (wasm-runtime-error-locals e))
  67. (newline)
  68. (print-location (validated-wasm-ref
  69. (wasm-instance-module
  70. (wasm-runtime-error-instance e)))
  71. (wasm-runtime-error-position e)))
  72. (define-syntax-rule (with-exception-handling body ...)
  73. (with-exception-handler (lambda (e) (print-runtime-error e))
  74. (lambda () body ...)
  75. #:unwind? #t
  76. #:unwind-for-type &wasm-runtime-error))
  77. (define (block-type-repr type)
  78. (match type
  79. ((? func-sig?)
  80. (match (type-repr type)
  81. (('func params+results ...)
  82. params+results)))
  83. ((? ref-type?)
  84. `((param ,(val-type-repr type))))
  85. (_ `((param ,type)))))
  86. (define (print-location wasm path)
  87. (define invalid-path '(-1))
  88. (define (path-remainder path i)
  89. (match path
  90. ((idx . rest)
  91. (if (and (= idx i) (not (null? rest))) rest invalid-path))))
  92. (define (here? path i)
  93. (match path
  94. ((idx) (= i idx))
  95. (_ #f)))
  96. (define (indent level)
  97. (unless (= level 0)
  98. (display " ")
  99. (indent (- level 1))))
  100. (define (print-block-type type)
  101. (for-each (lambda (x)
  102. (format #t " ~s" x))
  103. (block-type-repr type)))
  104. (define (print-instr level instr path)
  105. (match instr
  106. (((and op (or 'block 'loop)) _ (or ($ <type-use> _ sig) sig) body)
  107. (format #t "(~a" op)
  108. (print-block-type sig)
  109. (newline)
  110. (print-instrs (+ level 1) body path)
  111. (display ")"))
  112. (('if _ (or ($ <type-use> _ sig) sig) consequent alternate)
  113. (display "(if")
  114. (print-block-type sig)
  115. (unless (null? consequent)
  116. (newline)
  117. (indent (+ level 1))
  118. (display "(then\n")
  119. (print-instrs (+ level 2) consequent
  120. (path-remainder path 0))
  121. (display ")"))
  122. (unless (null? alternate)
  123. (newline)
  124. (indent (+ level 1))
  125. (display "(else\n")
  126. (print-instrs (+ level 2) alternate
  127. (path-remainder path 1))
  128. (display ")"))
  129. (display ")"))
  130. (_
  131. (write instr))))
  132. (define (print-instrs level instrs path)
  133. (indent level)
  134. (let loop ((instrs instrs)
  135. (i 0))
  136. (match instrs
  137. (() #t)
  138. ((instr . rest)
  139. (if (here? path i)
  140. (begin
  141. (display "<<< ")
  142. (print-instr level instr (path-remainder path i))
  143. (display " >>>"))
  144. (print-instr level instr (path-remainder path i)))
  145. (unless (null? rest)
  146. (newline)
  147. (indent level)
  148. (loop rest (+ i 1)))))))
  149. (define (count-imports kind)
  150. (fold (lambda (i sum)
  151. (match i
  152. (($ <import> _ _ k)
  153. (if (eq? kind k) (+ sum 1) sum))))
  154. 0 (wasm-imports wasm)))
  155. (match path
  156. (('func idx . path*)
  157. (match (list-ref (wasm-funcs wasm) (- idx (count-imports 'func)))
  158. (($ <func> id ($ <type-use> _ sig) locals body)
  159. (format #t "(func ~a" idx)
  160. (print-block-type sig)
  161. (newline)
  162. (print-instrs 1 body path*)
  163. (display ")"))))
  164. (('global idx . path*)
  165. (match (list-ref (wasm-globals wasm) (- idx (count-imports 'global)))
  166. (($ <global> id ($ <global-type> mutable? type) init)
  167. (let ((t (val-type-repr type)))
  168. (format #t "(global ~a " idx)
  169. (write (if mutable? `(mut ,t) t))
  170. (newline)
  171. (print-instrs 1 init path*)
  172. (display ")")))))
  173. (('data idx . path*)
  174. (match (list-ref (wasm-datas wasm) idx)
  175. (($ <data> id mode mem offset init)
  176. (format #t "(data ~a ~a ~a ~a\n" idx mode mem offset)
  177. (print-instrs 1 init path*)
  178. (display ")"))))
  179. (('elem idx j . path*)
  180. (match (list-ref (wasm-elems wasm) idx)
  181. (($ <elem> id mode table type offset inits)
  182. (let ((t (val-type-repr type)))
  183. (format #t "(elem ~a ~a ~a ~a" idx mode table t)
  184. (when offset
  185. (newline)
  186. (print-instrs 1 offset (if (= j 0) path* invalid-path)))
  187. (let loop ((inits inits) (i 1))
  188. (match inits
  189. (() #t)
  190. ((init . rest)
  191. (newline)
  192. (print-instrs 1 init (if (= j 1) path* invalid-path))
  193. (loop rest (+ i 1)))))
  194. (display ")"))))))
  195. (newline))
  196. (define (wasm-trace path instr instance stack blocks locals)
  197. (let ((instr (match instr ; abbreviate blocks
  198. (((and (or 'block 'loop) op) _ type . _)
  199. `(,op ,(block-type-repr type) ...))
  200. (('if _ type . _)
  201. `(if ,(block-type-repr type) ...))
  202. (_ instr))))
  203. (define (abbrev x)
  204. (match x
  205. ((? wasm-null?) 'null)
  206. ((? wasm-struct?) 'struct)
  207. ((? wasm-array?) 'array)
  208. ((? wasm-func?) 'func)
  209. (_ x)))
  210. (format #t "⌄ instr: ~a\n" instr)
  211. (format #t " loc: ~a @ ~a\n" instance (reverse path))
  212. (format #t " stack: ~s\n" (map abbrev (wasm-stack-items stack)))
  213. (format #t " locals: ~a\n" (map abbrev (vector->list locals)))))
  214. (define (->wasm x)
  215. (match x
  216. ((? wasm? wasm) wasm)
  217. ((? validated-wasm? mod) (validated-wasm-ref mod))
  218. ((? wasm-instance? instance)
  219. (validated-wasm-ref (wasm-instance-module instance)))
  220. ((? hoot-module? mod)
  221. (validated-wasm-ref
  222. (wasm-instance-module
  223. (hoot-module-instance mod))))))
  224. (define-record-type <wasm-debug>
  225. (make-wasm-debug position instruction instance stack blocks locals)
  226. wasm-debug?
  227. (position wasm-debug-position)
  228. (instruction wasm-debug-instruction)
  229. (instance wasm-debug-instance)
  230. (stack wasm-debug-stack)
  231. (blocks wasm-debug-blocks)
  232. (locals wasm-debug-locals)
  233. (continue? wasm-debug-continue? set-wasm-debug-continue!))
  234. (define current-wasm-debug (make-parameter #f))
  235. (define-syntax-rule (when-debugging body ...)
  236. (if (current-wasm-debug)
  237. (begin body ...)
  238. (error "not in a WASM debugger")))
  239. ;; This code is based on error-string in (system repl
  240. ;; exception-handling) and adapted to work with Guile's new exception
  241. ;; objects.
  242. (define (error-message exn stack)
  243. (let ((key (exception-kind exn))
  244. (args (exception-args exn)))
  245. (call-with-output-string
  246. (lambda (port)
  247. (let ((frame (and (< 0 (vector-length stack)) (vector-ref stack 0))))
  248. (print-exception port frame key args))))))
  249. (define (enter-wasm-debugger exn)
  250. (let* ((tag (and (pair? (fluid-ref %stacks))
  251. (cdr (fluid-ref %stacks))))
  252. (stack (stack->vector (make-stack #t 3 tag 0 1)))
  253. (msg (error-message exn stack))
  254. (wasm-debug (make-wasm-debug (wasm-runtime-error-position exn)
  255. (wasm-runtime-error-instruction exn)
  256. (wasm-runtime-error-instance exn)
  257. (wasm-runtime-error-stack exn)
  258. (wasm-runtime-error-blocks exn)
  259. (wasm-runtime-error-locals exn))))
  260. (parameterize ((current-wasm-debug wasm-debug))
  261. (format #t "~a\n" msg)
  262. (format #t "Entering WASM debug prompt. ")
  263. (format #t "Type `,help wasm' for info or `,q' to continue.\n")
  264. (start-repl #:debug (make-debug stack 0 msg))
  265. (wasm-debug-continue? wasm-debug))))
  266. (define (wasm-step position instruction instance stack blocks locals)
  267. (let ((wasm-debug (make-wasm-debug (reverse position) instruction instance stack
  268. blocks locals)))
  269. (parameterize ((current-wasm-debug wasm-debug))
  270. (format #t "Instruction: ~a\n" instruction)
  271. (format #t "Location: ~a\n" (reverse position))
  272. (start-repl))))
  273. (define (reset-instruction-listener)
  274. (current-instruction-listener
  275. (lambda (position instr instance stack blocks locals) #t)))
  276. (define (continue)
  277. (set-wasm-debug-continue! (current-wasm-debug) #t)
  278. (throw 'quit))
  279. (define-meta-command ((wasm-dump wasm) repl #:optional exp)
  280. "wasm-dump [WASM]
  281. Display information about WASM, or the current WASM instance when debugging."
  282. (dump-wasm (->wasm
  283. (cond
  284. (exp (repl-eval repl exp))
  285. ((current-wasm-debug) => wasm-debug-instance)
  286. (else (error "no WASM object specified"))))
  287. #:dump-func-defs? #f))
  288. (define-meta-command ((wasm-trace wasm) repl exp)
  289. "wasm-trace EXP
  290. Evaluate EXP with verbose WASM tracing enabled."
  291. (with-exception-handling
  292. (parameterize ((current-instruction-listener wasm-trace))
  293. (call-with-values (lambda () (repl-eval repl exp))
  294. (lambda vals
  295. (for-each (lambda (v) (repl-print repl v)) vals))))))
  296. (define-meta-command ((wasm-freq wasm) repl exp)
  297. "wasm-freq EXP
  298. Evaluate EXP and count how many times each WASM instruction is evaluated."
  299. (let ((count 0)
  300. (histogram (make-hash-table)))
  301. (define (wasm-stats path instr instance stack blocks locals)
  302. (set! count (+ count 1))
  303. (match instr
  304. ((op . _)
  305. (hashq-set! histogram op (+ (hashq-ref histogram op 0) 1)))))
  306. (with-exception-handling
  307. (parameterize ((current-instruction-listener wasm-stats))
  308. (call-with-values (lambda () (repl-eval repl exp))
  309. (lambda vals
  310. (display "op\tcount\n")
  311. (display "--\t-----\n")
  312. (for-each (match-lambda
  313. ((op . k)
  314. (format #t "~a\t~a\n" op k)))
  315. (sort (hash-fold alist-cons '() histogram)
  316. (lambda (a b) (< (cdr a) (cdr b)))))
  317. (format #t "\n~a instructions total\n\n" count)
  318. (for-each (lambda (v) (repl-print repl v)) vals)))))))
  319. (define-meta-command ((wasm-catch wasm) repl exp)
  320. "wasm-catch EXP
  321. Catch and debug WASM runtime errors that are raised by evaluating EXP."
  322. (let ((thunk (repl-prepare-eval-thunk repl exp)))
  323. (call/ec
  324. (lambda (return)
  325. (with-exception-handler (lambda (exn)
  326. (if (wasm-runtime-error? exn)
  327. (unless (enter-wasm-debugger exn)
  328. (reset-instruction-listener)
  329. (return))
  330. (raise-exception exn)))
  331. (lambda ()
  332. (call-with-values (lambda () (%start-stack #t thunk))
  333. (lambda vals
  334. (reset-instruction-listener)
  335. (for-each (lambda (v) (repl-print repl v)) vals)))))))))
  336. (define-meta-command ((wasm-stack wasm) repl)
  337. "wasm-stack
  338. Print the state of the WASM stack in the current context."
  339. (when-debugging
  340. (print-stack (wasm-debug-stack (current-wasm-debug)))))
  341. (define-meta-command ((wasm-locals wasm) repl)
  342. "wasm-locals
  343. Print the state of the WASM locals in the current context."
  344. (when-debugging
  345. (print-locals (wasm-debug-locals (current-wasm-debug)))))
  346. (define-meta-command ((wasm-pos wasm) repl)
  347. "wasm-pos
  348. Highlight the instruction where WASM execution has paused."
  349. (when-debugging
  350. (let ((debug (current-wasm-debug)))
  351. (print-location (->wasm (wasm-debug-instance debug)) (wasm-debug-position debug)))))
  352. (define-meta-command ((wasm-eval wasm) repl instr)
  353. "wasm-eval INSTR
  354. Evaluate the WASM instruction INSTR in the current debug context."
  355. (when-debugging
  356. (let ((execute (@@ (wasm vm) execute)))
  357. (match (current-wasm-debug)
  358. (($ <wasm-debug> position _ instance stack blocks locals)
  359. (execute (repl-eval repl instr) position instance stack blocks locals))))))
  360. (define-meta-command ((wasm-continue wasm) repl)
  361. "wasm-continue
  362. Set WASM execution to continue without interruption until the next error."
  363. (when-debugging
  364. (reset-instruction-listener)
  365. (when (current-wasm-debug)
  366. (continue))))
  367. (define-meta-command ((wasm-step wasm) repl)
  368. "wasm-step
  369. Set WASM execution to pause before each instruction."
  370. (when-debugging
  371. (current-instruction-listener wasm-step)
  372. (when (current-wasm-debug)
  373. (continue))))