command.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. ;;; Repl commands
  2. ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2020, 2021 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system repl command)
  19. #:use-module (system base pmatch)
  20. #:autoload (system base compile) (compile-file)
  21. #:use-module (system repl common)
  22. #:use-module (system repl debug)
  23. #:autoload (system vm disassembler) (disassemble-image
  24. disassemble-program
  25. disassemble-file)
  26. #:use-module (system vm loader)
  27. #:use-module (system vm program)
  28. #:use-module (system vm trap-state)
  29. #:autoload (system base language) (lookup-language language-reader
  30. language-title language-name)
  31. #:autoload (system vm trace) (call-with-trace)
  32. #:use-module (ice-9 format)
  33. #:use-module (ice-9 session)
  34. #:use-module (ice-9 documentation)
  35. #:use-module (ice-9 rdelim)
  36. #:use-module (ice-9 control)
  37. #:use-module ((ice-9 pretty-print) #:select ((pretty-print . pp)))
  38. #:use-module ((system vm inspect) #:select ((inspect . %inspect)))
  39. #:use-module (rnrs bytevectors)
  40. #:autoload (statprof) (statprof)
  41. #:export (meta-command define-meta-command))
  42. ;;;
  43. ;;; Meta command interface
  44. ;;;
  45. (define *command-table*
  46. '((help (help h) (show) (apropos a) (describe d))
  47. (module (module m) (import use) (load l) (reload re) (binding b) (in))
  48. (language (language L))
  49. (compile (compile c) (compile-file cc)
  50. (expand exp) (optimize opt) (optimize-cps optx)
  51. (disassemble x) (disassemble-file xx))
  52. (profile (time t) (profile pr) (trace tr))
  53. (debug (backtrace bt) (up) (down) (frame fr)
  54. (locals) (error-message error)
  55. (break br bp) (break-at-source break-at bs)
  56. (step s) (step-instruction si)
  57. (next n) (next-instruction ni)
  58. (finish)
  59. (tracepoint tp)
  60. (traps) (delete del) (disable) (enable)
  61. (registers regs))
  62. (inspect (inspect i) (pretty-print pp))
  63. (system (gc) (statistics stat) (option o)
  64. (quit q continue cont))))
  65. (define *show-table*
  66. '((show (warranty w) (copying c) (version v))))
  67. (define (group-name g) (car g))
  68. (define (group-commands g) (cdr g))
  69. (define *command-infos* (make-hash-table))
  70. (define (command-name c) (car c))
  71. (define (command-abbrevs c) (cdr c))
  72. (define (command-info c) (hashq-ref *command-infos* (command-name c)))
  73. (define (command-procedure c) (command-info-procedure (command-info c)))
  74. (define (command-doc c) (procedure-documentation (command-procedure c)))
  75. (define (make-command-info proc arguments-reader)
  76. (cons proc arguments-reader))
  77. (define (command-info-procedure info)
  78. (car info))
  79. (define (command-info-arguments-reader info)
  80. (cdr info))
  81. (define (command-usage c)
  82. (let ((doc (command-doc c)))
  83. (substring doc 0 (string-index doc #\newline))))
  84. (define (command-summary c)
  85. (let* ((doc (command-doc c))
  86. (start (1+ (string-index doc #\newline))))
  87. (cond ((string-index doc #\newline start)
  88. => (lambda (end) (substring doc start end)))
  89. (else (substring doc start)))))
  90. (define (lookup-group name)
  91. (assq name *command-table*))
  92. (define* (lookup-command key #:optional (table *command-table*))
  93. (let loop ((groups table) (commands '()))
  94. (cond ((and (null? groups) (null? commands)) #f)
  95. ((null? commands)
  96. (loop (cdr groups) (cdar groups)))
  97. ((memq key (car commands)) (car commands))
  98. (else (loop groups (cdr commands))))))
  99. (define* (display-group group #:optional (abbrev? #t))
  100. (format #t "~:(~A~) Commands~:[~; [abbrev]~]:~2%" (group-name group) abbrev?)
  101. (for-each (lambda (c)
  102. (display-summary (command-usage c)
  103. (if abbrev? (command-abbrevs c) '())
  104. (command-summary c)))
  105. (group-commands group))
  106. (newline))
  107. (define (display-command command)
  108. (display "Usage: ")
  109. (display (command-doc command))
  110. (newline))
  111. (define (display-summary usage abbrevs summary)
  112. (let* ((usage-len (string-length usage))
  113. (abbrevs (if (pair? abbrevs)
  114. (format #f "[,~A~{ ,~A~}]" (car abbrevs) (cdr abbrevs))
  115. ""))
  116. (abbrevs-len (string-length abbrevs)))
  117. (format #t " ,~A~A~A - ~A\n"
  118. usage
  119. (cond
  120. ((> abbrevs-len 32)
  121. (error "abbrevs too long" abbrevs))
  122. ((> (+ usage-len abbrevs-len) 32)
  123. (format #f "~%~v_" (+ 2 (- 32 abbrevs-len))))
  124. (else
  125. (format #f "~v_" (- 32 abbrevs-len usage-len))))
  126. abbrevs
  127. summary)))
  128. (define (read-command repl)
  129. (catch #t
  130. (lambda () (read))
  131. (lambda (key . args)
  132. (pmatch args
  133. ((,subr ,msg ,args . ,rest)
  134. (format #t "Throw to key `~a' while reading command:\n" key)
  135. (display-error #f (current-output-port) subr msg args rest))
  136. (else
  137. (format #t "Throw to key `~a' with args `~s' while reading command.\n"
  138. key args)))
  139. (force-output)
  140. *unspecified*)))
  141. (define (read-command-arguments c repl)
  142. ((command-info-arguments-reader (command-info c)) repl))
  143. (define (meta-command repl)
  144. (let ((command (read-command repl)))
  145. (cond
  146. ((eq? command *unspecified*)) ; read error, already signaled; pass.
  147. ((not (symbol? command))
  148. (format #t "Meta-command not a symbol: ~s~%" command))
  149. ((lookup-command command)
  150. => (lambda (c)
  151. (and=> (read-command-arguments c repl)
  152. (lambda (args) (apply (command-procedure c) repl args)))))
  153. (else
  154. (format #t "Unknown meta command: ~A~%" command)))))
  155. (define (add-meta-command! name category proc argument-reader)
  156. (hashq-set! *command-infos* name (make-command-info proc argument-reader))
  157. (if category
  158. (let ((entry (assq category *command-table*)))
  159. (if entry
  160. (set-cdr! entry (append (cdr entry) (list (list name))))
  161. (set! *command-table*
  162. (append *command-table*
  163. (list (list category (list name)))))))))
  164. (define-syntax define-meta-command
  165. (syntax-rules ()
  166. ((_ ((name category) repl (expression0 ...) . datums) docstring b0 b1 ...)
  167. (add-meta-command!
  168. 'name
  169. 'category
  170. (lambda* (repl expression0 ... . datums)
  171. docstring
  172. b0 b1 ...)
  173. (lambda (repl)
  174. (define (handle-read-error form-name key args)
  175. (pmatch args
  176. ((,subr ,msg ,args . ,rest)
  177. (format #t "Throw to key `~a' while reading ~@[argument `~A' of ~]command `~A':\n"
  178. key form-name 'name)
  179. (display-error #f (current-output-port) subr msg args rest))
  180. (else
  181. (format #t "Throw to key `~a' with args `~s' while reading ~@[ argument `~A' of ~]command `~A'.\n"
  182. key args form-name 'name)))
  183. (abort))
  184. (% (let* ((expression0
  185. (catch #t
  186. (lambda ()
  187. (repl-reader
  188. ""
  189. (lambda* (#:optional (port (current-input-port)))
  190. ((language-reader (repl-language repl))
  191. port (current-module)))))
  192. (lambda (k . args)
  193. (handle-read-error 'expression0 k args))))
  194. ...)
  195. (append
  196. (list expression0 ...)
  197. (catch #t
  198. (lambda ()
  199. (let ((port (open-input-string (read-line))))
  200. (let lp ((out '()))
  201. (let ((x (read port)))
  202. (if (eof-object? x)
  203. (reverse out)
  204. (lp (cons x out)))))))
  205. (lambda (k . args)
  206. (handle-read-error #f k args)))))
  207. (lambda (k) #f))))) ; the abort handler
  208. ((_ ((name category) repl . datums) docstring b0 b1 ...)
  209. (define-meta-command ((name category) repl () . datums)
  210. docstring b0 b1 ...))
  211. ((_ (name repl (expression0 ...) . datums) docstring b0 b1 ...)
  212. (define-meta-command ((name #f) repl (expression0 ...) . datums)
  213. docstring b0 b1 ...))
  214. ((_ (name repl . datums) docstring b0 b1 ...)
  215. (define-meta-command ((name #f) repl () . datums)
  216. docstring b0 b1 ...))))
  217. ;;;
  218. ;;; Help commands
  219. ;;;
  220. (define-meta-command (help repl . args)
  221. "help [all | GROUP | [-c] COMMAND]
  222. Show help.
  223. With one argument, tries to look up the argument as a group name, giving
  224. help on that group if successful. Otherwise tries to look up the
  225. argument as a command, giving help on the command.
  226. If there is a command whose name is also a group name, use the ,help
  227. -c COMMAND form to give help on the command instead of the group.
  228. Without any argument, a list of help commands and command groups
  229. are displayed."
  230. (pmatch args
  231. (()
  232. (display-group (lookup-group 'help))
  233. (display "Command Groups:\n\n")
  234. (display-summary "help all" #f "List all commands")
  235. (for-each (lambda (g)
  236. (let* ((name (symbol->string (group-name g)))
  237. (usage (string-append "help " name))
  238. (header (string-append "List " name " commands")))
  239. (display-summary usage #f header)))
  240. (cdr *command-table*))
  241. (newline)
  242. (display
  243. "Type `,help -c COMMAND' to show documentation of a particular command.")
  244. (newline))
  245. ((all)
  246. (for-each display-group *command-table*))
  247. ((,group) (guard (lookup-group group))
  248. (display-group (lookup-group group)))
  249. ((,command) (guard (lookup-command command))
  250. (display-command (lookup-command command)))
  251. ((-c ,command) (guard (lookup-command command))
  252. (display-command (lookup-command command)))
  253. ((,command)
  254. (format #t "Unknown command or group: ~A~%" command))
  255. ((-c ,command)
  256. (format #t "Unknown command: ~A~%" command))
  257. (else
  258. (format #t "Bad arguments: ~A~%" args))))
  259. (define-meta-command (show repl . args)
  260. "show [TOPIC]
  261. Gives information about Guile.
  262. With one argument, tries to show a particular piece of information;
  263. currently supported topics are `warranty' (or `w'), `copying' (or `c'),
  264. and `version' (or `v').
  265. Without any argument, a list of topics is displayed."
  266. (pmatch args
  267. (()
  268. (display-group (car *show-table*) #f)
  269. (newline))
  270. ((,topic) (guard (lookup-command topic *show-table*))
  271. ((command-procedure (lookup-command topic *show-table*)) repl))
  272. ((,command)
  273. (format #t "Unknown topic: ~A~%" command))
  274. (else
  275. (format #t "Bad arguments: ~A~%" args))))
  276. ;;; `warranty', `copying' and `version' are "hidden" meta-commands, only
  277. ;;; accessible via `show'. They have an entry in *command-infos* but not
  278. ;;; in *command-table*.
  279. (define-meta-command (warranty repl)
  280. "show warranty
  281. Details on the lack of warranty."
  282. (display *warranty*)
  283. (newline))
  284. (define-meta-command (copying repl)
  285. "show copying
  286. Show the LGPLv3."
  287. (display *copying*)
  288. (newline))
  289. (define-meta-command (version repl)
  290. "show version
  291. Version information."
  292. (display *version*)
  293. (newline))
  294. (define-meta-command (apropos repl regexp)
  295. "apropos REGEXP
  296. Find bindings/modules/packages."
  297. (apropos (->string regexp)))
  298. (define-meta-command (describe repl (form))
  299. "describe OBJ
  300. Show description/documentation."
  301. (display
  302. (object-documentation
  303. (let ((input (repl-parse repl form)))
  304. (if (symbol? input)
  305. (module-ref (current-module) input)
  306. (repl-eval repl input)))))
  307. (newline))
  308. (define-meta-command (option repl . args)
  309. "option [NAME] [EXP]
  310. List/show/set options."
  311. (pmatch args
  312. (()
  313. (for-each (lambda (spec)
  314. (format #t " ~A~24t~A\n" (car spec) (cadr spec)))
  315. (repl-options repl)))
  316. ((,name)
  317. (display (repl-option-ref repl name))
  318. (newline))
  319. ((,name ,exp)
  320. ;; Would be nice to evaluate in the current language, but the REPL
  321. ;; option parser doesn't permit that, currently.
  322. (repl-option-set! repl name (eval exp (current-module))))))
  323. (define-meta-command (quit repl)
  324. "quit
  325. Quit this session."
  326. (throw 'quit))
  327. ;;;
  328. ;;; Module commands
  329. ;;;
  330. (define-meta-command (module repl . args)
  331. "module [MODULE]
  332. Change modules / Show current module."
  333. (pmatch args
  334. (() (puts (module-name (current-module))))
  335. ((,mod-name) (guard (list? mod-name))
  336. (set-current-module (resolve-module mod-name)))
  337. (,mod-name (set-current-module (resolve-module mod-name)))))
  338. (define-meta-command (import repl . args)
  339. "import [MODULE ...]
  340. Import modules / List those imported."
  341. (let ()
  342. (define (use name)
  343. (let ((mod (resolve-interface name)))
  344. (if mod
  345. (module-use! (current-module) mod)
  346. (format #t "No such module: ~A~%" name))))
  347. (if (null? args)
  348. (for-each puts (map module-name (module-uses (current-module))))
  349. (for-each use args))))
  350. (define-meta-command (load repl file)
  351. "load FILE
  352. Load a file in the current module."
  353. (load (->string file)))
  354. (define-meta-command (reload repl . args)
  355. "reload [MODULE]
  356. Reload the given module, or the current module if none was given."
  357. (pmatch args
  358. (() (reload-module (current-module)))
  359. ((,mod-name) (guard (list? mod-name))
  360. (reload-module (resolve-module mod-name)))
  361. (,mod-name (reload-module (resolve-module mod-name)))))
  362. (define-meta-command (binding repl)
  363. "binding
  364. List current bindings."
  365. (module-for-each (lambda (k v) (format #t "~23A ~A\n" k v))
  366. (current-module)))
  367. (define-meta-command (in repl module command-or-expression . args)
  368. "in MODULE COMMAND-OR-EXPRESSION
  369. Evaluate an expression or command in the context of module."
  370. (let ((m (resolve-module module #:ensure #f)))
  371. (if m
  372. (pmatch command-or-expression
  373. (('unquote ,command) (guard (lookup-command command))
  374. (save-module-excursion
  375. (lambda ()
  376. (set-current-module m)
  377. (apply (command-procedure (lookup-command command)) repl args))))
  378. (,expression
  379. (guard (null? args))
  380. (repl-print repl (eval expression m)))
  381. (else
  382. (format #t "Invalid arguments to `in': expected a single expression or a command.\n")))
  383. (format #t "No such module: ~s\n" module))))
  384. ;;;
  385. ;;; Language commands
  386. ;;;
  387. (define-meta-command (language repl name)
  388. "language LANGUAGE
  389. Change languages."
  390. (let ((lang (lookup-language name))
  391. (cur (repl-language repl)))
  392. (format #t "Happy hacking with ~a! To switch back, type `,L ~a'.\n"
  393. (language-title lang) (language-name cur))
  394. (current-language lang)
  395. (set! (repl-language repl) lang)))
  396. ;;;
  397. ;;; Compile commands
  398. ;;;
  399. (define (load-image x)
  400. (let ((thunk (load-thunk-from-memory x)))
  401. (find-mapped-elf-image (program-code thunk))))
  402. (define-meta-command (compile repl (form))
  403. "compile EXP
  404. Generate compiled code."
  405. (let ((x (repl-compile repl (repl-parse repl form))))
  406. (cond ((bytevector? x) (disassemble-image (load-image x)))
  407. (else (repl-print repl x)))))
  408. (define-meta-command (compile-file repl file . opts)
  409. "compile-file FILE
  410. Compile a file."
  411. (compile-file (->string file) #:opts opts))
  412. (define-meta-command (expand repl (form))
  413. "expand EXP
  414. Expand any macros in a form."
  415. (let ((x (repl-expand repl (repl-parse repl form))))
  416. (run-hook before-print-hook x)
  417. (pp x)))
  418. (define-meta-command (optimize repl (form))
  419. "optimize EXP
  420. Run the optimizer on a piece of code and print the result."
  421. (let ((x (repl-optimize repl (repl-parse repl form))))
  422. (run-hook before-print-hook x)
  423. (pp x)))
  424. (define-meta-command (optimize-cps repl (form))
  425. "optimize-cps EXP
  426. Run the CPS optimizer on a piece of code and print the result."
  427. (repl-optimize-cps repl (repl-parse repl form)))
  428. (define-meta-command (disassemble repl (form))
  429. "disassemble EXP
  430. Disassemble a compiled procedure."
  431. (let ((obj (repl-eval repl (repl-parse repl form))))
  432. (cond
  433. ((program? obj)
  434. (disassemble-program obj))
  435. ((bytevector? obj)
  436. (disassemble-image (load-image obj)))
  437. (else
  438. (format #t
  439. "Argument to ,disassemble not a procedure or a bytevector: ~a~%"
  440. obj)))))
  441. (define-meta-command (disassemble-file repl file)
  442. "disassemble-file FILE
  443. Disassemble a file."
  444. (disassemble-file (->string file)))
  445. ;;;
  446. ;;; Profile commands
  447. ;;;
  448. (define-meta-command (time repl (form))
  449. "time EXP
  450. Time execution."
  451. (let* ((gc-start (gc-run-time))
  452. (real-start (get-internal-real-time))
  453. (run-start (get-internal-run-time))
  454. (result (repl-eval repl (repl-parse repl form)))
  455. (run-end (get-internal-run-time))
  456. (real-end (get-internal-real-time))
  457. (gc-end (gc-run-time)))
  458. (define (diff start end)
  459. (/ (- end start) 1.0 internal-time-units-per-second))
  460. (repl-print repl result)
  461. (format #t ";; ~,6Fs real time, ~,6Fs run time. ~,6Fs spent in GC.\n"
  462. (diff real-start real-end)
  463. (diff run-start run-end)
  464. (diff gc-start gc-end))
  465. result))
  466. (define-meta-command (profile repl (form) . opts)
  467. "profile EXP
  468. Profile execution."
  469. ;; FIXME opts
  470. (apply statprof
  471. (repl-prepare-eval-thunk repl (repl-parse repl form))
  472. opts))
  473. (define-meta-command (trace repl (form) . opts)
  474. "trace EXP
  475. Trace execution."
  476. ;; FIXME: doc options, or somehow deal with them better
  477. (apply call-with-trace
  478. (repl-prepare-eval-thunk repl (repl-parse repl form))
  479. (cons* #:width (terminal-width) opts)))
  480. ;;;
  481. ;;; Debug commands
  482. ;;;
  483. (define-syntax define-stack-command
  484. (lambda (x)
  485. (syntax-case x ()
  486. ((_ (name repl . args) docstring body body* ...)
  487. #`(define-meta-command (name repl . args)
  488. docstring
  489. (let ((debug (repl-debug repl)))
  490. (if debug
  491. (letrec-syntax
  492. ((#,(datum->syntax #'repl 'frames)
  493. (identifier-syntax (debug-frames debug)))
  494. (#,(datum->syntax #'repl 'message)
  495. (identifier-syntax (debug-error-message debug)))
  496. (#,(datum->syntax #'repl 'index)
  497. (identifier-syntax
  498. (id (debug-index debug))
  499. ((set! id exp) (set! (debug-index debug) exp))))
  500. (#,(datum->syntax #'repl 'cur)
  501. (identifier-syntax
  502. (vector-ref #,(datum->syntax #'repl 'frames)
  503. #,(datum->syntax #'repl 'index)))))
  504. body body* ...)
  505. (format #t "Nothing to debug.~%"))))))))
  506. (define-stack-command (backtrace repl #:optional count
  507. #:key (width (terminal-width)) full?)
  508. "backtrace [COUNT] [#:width W] [#:full? F]
  509. Print a backtrace.
  510. Print a backtrace of all stack frames, or innermost COUNT frames.
  511. If COUNT is negative, the last COUNT frames will be shown."
  512. (print-frames frames
  513. #:count count
  514. #:width width
  515. #:full? full?))
  516. (define-stack-command (up repl #:optional (count 1))
  517. "up [COUNT]
  518. Select a calling stack frame.
  519. Select and print stack frames that called this one.
  520. An argument says how many frames up to go."
  521. (cond
  522. ((or (not (integer? count)) (<= count 0))
  523. (format #t "Invalid argument to `up': expected a positive integer for COUNT.~%"))
  524. ((>= (+ count index) (vector-length frames))
  525. (cond
  526. ((= index (1- (vector-length frames)))
  527. (format #t "Already at outermost frame.\n"))
  528. (else
  529. (set! index (1- (vector-length frames)))
  530. (print-frame cur #:index index))))
  531. (else
  532. (set! index (+ count index))
  533. (print-frame cur #:index index))))
  534. (define-stack-command (down repl #:optional (count 1))
  535. "down [COUNT]
  536. Select a called stack frame.
  537. Select and print stack frames called by this one.
  538. An argument says how many frames down to go."
  539. (cond
  540. ((or (not (integer? count)) (<= count 0))
  541. (format #t "Invalid argument to `down': expected a positive integer for COUNT.~%"))
  542. ((< (- index count) 0)
  543. (cond
  544. ((zero? index)
  545. (format #t "Already at innermost frame.\n"))
  546. (else
  547. (set! index 0)
  548. (print-frame cur #:index index))))
  549. (else
  550. (set! index (- index count))
  551. (print-frame cur #:index index))))
  552. (define-stack-command (frame repl #:optional idx)
  553. "frame [IDX]
  554. Show a frame.
  555. Show the selected frame.
  556. With an argument, select a frame by index, then show it."
  557. (cond
  558. (idx
  559. (cond
  560. ((or (not (integer? idx)) (< idx 0))
  561. (format #t "Invalid argument to `frame': expected a non-negative integer for IDX.~%"))
  562. ((< idx (vector-length frames))
  563. (set! index idx)
  564. (print-frame cur #:index index))
  565. (else
  566. (format #t "No such frame.~%"))))
  567. (else (print-frame cur #:index index))))
  568. (define-stack-command (locals repl #:key (width (terminal-width)))
  569. "locals
  570. Show local variables.
  571. Show locally-bound variables in the selected frame."
  572. (print-locals cur #:width width))
  573. (define-stack-command (error-message repl)
  574. "error-message
  575. Show error message.
  576. Display the message associated with the error that started the current
  577. debugging REPL."
  578. (format #t "~a~%" (if (string? message) message "No error message")))
  579. (define-meta-command (break repl (form))
  580. "break PROCEDURE
  581. Break on calls to PROCEDURE.
  582. Starts a recursive prompt when PROCEDURE is called."
  583. (let ((proc (repl-eval repl (repl-parse repl form))))
  584. (if (not (procedure? proc))
  585. (error "Not a procedure: ~a" proc)
  586. (let ((idx (add-trap-at-procedure-call! proc)))
  587. (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
  588. (define-meta-command (break-at-source repl file line)
  589. "break-at-source FILE LINE
  590. Break when control reaches the given source location.
  591. Starts a recursive prompt when control reaches line LINE of file FILE.
  592. Note that the given source location must be inside a procedure."
  593. (let ((file (if (symbol? file) (symbol->string file) file)))
  594. (let ((idx (add-trap-at-source-location! file line)))
  595. (format #t "Trap ~a: ~a.~%" idx (trap-name idx)))))
  596. (define (repl-pop-continuation-resumer repl msg)
  597. ;; Capture the dynamic environment with this prompt thing. The result
  598. ;; is a procedure that takes a frame and number of values returned.
  599. (% (call-with-values
  600. (lambda ()
  601. (abort
  602. (lambda (k)
  603. ;; Call frame->stack-vector before reinstating the
  604. ;; continuation, so that we catch the %stacks fluid at
  605. ;; the time of capture.
  606. (lambda (frame . values)
  607. (k frame
  608. (frame->stack-vector
  609. (frame-previous frame))
  610. values)))))
  611. (lambda (from stack values)
  612. (format #t "~a~%" msg)
  613. (if (null? values)
  614. (format #t "No return values.~%")
  615. (begin
  616. (format #t "Return values:~%")
  617. (for-each (lambda (x) (repl-print repl x)) values)))
  618. ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
  619. #:debug (make-debug stack 0 msg))))))
  620. (define-stack-command (finish repl)
  621. "finish
  622. Run until the current frame finishes.
  623. Resume execution, breaking when the current frame finishes."
  624. (let ((handler (repl-pop-continuation-resumer
  625. repl (format #f "Return from ~a" cur))))
  626. (add-ephemeral-trap-at-frame-finish! cur handler)
  627. (throw 'quit)))
  628. (define (repl-next-resumer msg)
  629. ;; Capture the dynamic environment with this prompt thing. The
  630. ;; result is a procedure that takes a frame.
  631. (% (let ((stack (abort
  632. (lambda (k)
  633. ;; Call frame->stack-vector before reinstating the
  634. ;; continuation, so that we catch the %stacks fluid
  635. ;; at the time of capture.
  636. (lambda (frame)
  637. (k (frame->stack-vector frame)))))))
  638. (format #t "~a~%" msg)
  639. ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
  640. #:debug (make-debug stack 0 msg)))))
  641. (define-stack-command (step repl)
  642. "step
  643. Step until control reaches a different source location.
  644. Step until control reaches a different source location."
  645. (let ((msg (format #f "Step into ~a" cur)))
  646. (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
  647. #:into? #t #:instruction? #f)
  648. (throw 'quit)))
  649. (define-stack-command (step-instruction repl)
  650. "step-instruction
  651. Step until control reaches a different instruction.
  652. Step until control reaches a different VM instruction."
  653. (let ((msg (format #f "Step into ~a" cur)))
  654. (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
  655. #:into? #t #:instruction? #t)
  656. (throw 'quit)))
  657. (define-stack-command (next repl)
  658. "next
  659. Step until control reaches a different source location in the current frame.
  660. Step until control reaches a different source location in the current frame."
  661. (let ((msg (format #f "Step into ~a" cur)))
  662. (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
  663. #:into? #f #:instruction? #f)
  664. (throw 'quit)))
  665. (define-stack-command (next-instruction repl)
  666. "next-instruction
  667. Step until control reaches a different instruction in the current frame.
  668. Step until control reaches a different VM instruction in the current frame."
  669. (let ((msg (format #f "Step into ~a" cur)))
  670. (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
  671. #:into? #f #:instruction? #t)
  672. (throw 'quit)))
  673. (define-meta-command (tracepoint repl (form))
  674. "tracepoint PROCEDURE
  675. Add a tracepoint to PROCEDURE.
  676. A tracepoint will print out the procedure and its arguments, when it is
  677. called, and its return value(s) when it returns."
  678. (let ((proc (repl-eval repl (repl-parse repl form))))
  679. (if (not (procedure? proc))
  680. (error "Not a procedure: ~a" proc)
  681. (let ((idx (add-trace-at-procedure-call! proc)))
  682. (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
  683. (define-meta-command (traps repl)
  684. "traps
  685. Show the set of currently attached traps.
  686. Show the set of currently attached traps (breakpoints and tracepoints)."
  687. (let ((traps (list-traps)))
  688. (if (null? traps)
  689. (format #t "No traps set.~%")
  690. (for-each (lambda (idx)
  691. (format #t " ~a: ~a~a~%"
  692. idx (trap-name idx)
  693. (if (trap-enabled? idx) "" " (disabled)")))
  694. traps))))
  695. (define-meta-command (delete repl idx)
  696. "delete IDX
  697. Delete a trap.
  698. Delete a trap."
  699. (if (not (integer? idx))
  700. (error "expected a trap index (a non-negative integer)" idx)
  701. (delete-trap! idx)))
  702. (define-meta-command (disable repl idx)
  703. "disable IDX
  704. Disable a trap.
  705. Disable a trap."
  706. (if (not (integer? idx))
  707. (error "expected a trap index (a non-negative integer)" idx)
  708. (disable-trap! idx)))
  709. (define-meta-command (enable repl idx)
  710. "enable IDX
  711. Enable a trap.
  712. Enable a trap."
  713. (if (not (integer? idx))
  714. (error "expected a trap index (a non-negative integer)" idx)
  715. (enable-trap! idx)))
  716. (define-stack-command (registers repl)
  717. "registers
  718. Print registers.
  719. Print the registers of the current frame."
  720. (print-registers cur))
  721. (define-meta-command (width repl #:optional x)
  722. "width [X]
  723. Set debug output width.
  724. Set the number of screen columns in the output from `backtrace' and
  725. `locals'."
  726. (terminal-width x)
  727. (format #t "Set screen width to ~a columns.~%" (terminal-width)))
  728. ;;;
  729. ;;; Inspection commands
  730. ;;;
  731. (define-meta-command (inspect repl (form))
  732. "inspect EXP
  733. Inspect the result(s) of evaluating EXP."
  734. (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
  735. (lambda args
  736. (for-each %inspect args))))
  737. (define-meta-command (pretty-print repl (form))
  738. "pretty-print EXP
  739. Pretty-print the result(s) of evaluating EXP."
  740. (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
  741. (lambda args
  742. (for-each
  743. (lambda (x)
  744. (run-hook before-print-hook x)
  745. (pp x))
  746. args))))
  747. ;;;
  748. ;;; System commands
  749. ;;;
  750. (define-meta-command (gc repl)
  751. "gc
  752. Garbage collection."
  753. (gc))
  754. (define-meta-command (statistics repl)
  755. "statistics
  756. Display statistics."
  757. (let ((this-tms (times))
  758. (this-gcs (gc-stats))
  759. (last-tms (repl-tm-stats repl))
  760. (last-gcs (repl-gc-stats repl)))
  761. ;; GC times
  762. (let ((this-times (assq-ref this-gcs 'gc-times))
  763. (last-times (assq-ref last-gcs 'gc-times)))
  764. (display-diff-stat "GC times:" #t this-times last-times "times")
  765. (newline))
  766. ;; Memory size
  767. (let ((this-heap (assq-ref this-gcs 'heap-size))
  768. (this-free (assq-ref this-gcs 'heap-free-size)))
  769. (display-stat-title "Memory size:" "current" "limit")
  770. (display-stat "heap" #f (- this-heap this-free) this-heap "bytes")
  771. (newline))
  772. ;; Cells collected
  773. (let ((this-alloc (assq-ref this-gcs 'heap-total-allocated))
  774. (last-alloc (assq-ref last-gcs 'heap-total-allocated)))
  775. (display-stat-title "Bytes allocated:" "diff" "total")
  776. (display-diff-stat "allocated" #f this-alloc last-alloc "bytes")
  777. (newline))
  778. ;; GC time taken
  779. (let ((this-total (assq-ref this-gcs 'gc-time-taken))
  780. (last-total (assq-ref last-gcs 'gc-time-taken)))
  781. (display-stat-title "GC time taken:" "diff" "total")
  782. (display-time-stat "total" this-total last-total)
  783. (newline))
  784. ;; Process time spent
  785. (let ((this-utime (tms:utime this-tms))
  786. (last-utime (tms:utime last-tms))
  787. (this-stime (tms:stime this-tms))
  788. (last-stime (tms:stime last-tms))
  789. (this-cutime (tms:cutime this-tms))
  790. (last-cutime (tms:cutime last-tms))
  791. (this-cstime (tms:cstime this-tms))
  792. (last-cstime (tms:cstime last-tms)))
  793. (display-stat-title "Process time spent:" "diff" "total")
  794. (display-time-stat "user" this-utime last-utime)
  795. (display-time-stat "system" this-stime last-stime)
  796. (display-time-stat "child user" this-cutime last-cutime)
  797. (display-time-stat "child system" this-cstime last-cstime)
  798. (newline))
  799. ;; Save statistics
  800. ;; Save statistics
  801. (set! (repl-tm-stats repl) this-tms)
  802. (set! (repl-gc-stats repl) this-gcs)))
  803. (define (display-stat title flag field1 field2 unit)
  804. (let ((fmt (format #f "~~20~AA ~~10@A /~~10@A ~~A~~%" (if flag "" "@"))))
  805. (format #t fmt title field1 field2 unit)))
  806. (define (display-stat-title title field1 field2)
  807. (display-stat title #t field1 field2 ""))
  808. (define (display-diff-stat title flag this last unit)
  809. (display-stat title flag (- this last) this unit))
  810. (define (display-time-stat title this last)
  811. (define (conv num)
  812. (format #f "~10,2F" (exact->inexact (/ num internal-time-units-per-second))))
  813. (display-stat title #f (conv (- this last)) (conv this) "s"))
  814. (define (display-mips-stat title this-time this-clock last-time last-clock)
  815. (define (mips time clock)
  816. (if (= time 0) "----" (format #f "~10,2F" (/ clock time 1000000.0))))
  817. (display-stat title #f
  818. (mips (- this-time last-time) (- this-clock last-clock))
  819. (mips this-time this-clock) "mips"))