command.scm 31 KB

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