command.scm 31 KB

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