command.scm 31 KB

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