3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (system repl command)
23 #:use-module (system base syntax)
24 #:use-module (system base pmatch)
25 #:use-module (system base compile)
26 #:use-module (system repl common)
27 #:use-module (system repl debug)
28 #:use-module (system vm objcode)
29 #:use-module (system vm program)
30 #:use-module (system vm trap-state)
31 #:use-module (system vm vm)
32 #:use-module ((system vm frame) #:select (frame-return-values))
33 #:autoload (system base language) (lookup-language language-reader)
34 #:autoload (system vm trace) (call-with-trace)
35 #:use-module (ice-9 format)
36 #:use-module (ice-9 session)
37 #:use-module (ice-9 documentation)
38 #:use-module (ice-9 and-let-star)
39 #:use-module (ice-9 rdelim)
40 #:use-module (ice-9 control)
41 #:use-module ((ice-9 pretty-print) #:select ((pretty-print . pp)))
42 #:use-module ((system vm inspect) #:select ((inspect . %inspect)))
43 #:use-module (statprof)
44 #:export (meta-command define-meta-command))
48 ;;; Meta command interface
51 (define *command-table*
52 '((help (help h) (show) (apropos a) (describe d))
53 (module (module m) (import use) (load l) (reload re) (binding b) (in))
54 (language (language L))
55 (compile (compile c) (compile-file cc)
56 (expand exp) (optimize opt)
57 (disassemble x) (disassemble-file xx))
58 (profile (time t) (profile pr) (trace tr))
59 (debug (backtrace bt) (up) (down) (frame fr)
60 (procedure proc) (locals) (error-message error)
61 (break br bp) (break-at-source break-at bs)
62 (step s) (step-instruction si)
63 (next n) (next-instruction ni)
66 (traps) (delete del) (disable) (enable)
68 (inspect (inspect i) (pretty-print pp))
69 (system (gc) (statistics stat) (option o)
70 (quit q continue cont))))
73 '((show (warranty w) (copying c) (version v))))
75 (define (group-name g) (car g))
76 (define (group-commands g) (cdr g))
78 (define *command-infos* (make-hash-table))
79 (define (command-name c) (car c))
80 (define (command-abbrevs c) (cdr c))
81 (define (command-info c) (hashq-ref *command-infos* (command-name c)))
82 (define (command-procedure c) (command-info-procedure (command-info c)))
83 (define (command-doc c) (procedure-documentation (command-procedure c)))
85 (define (make-command-info proc arguments-reader)
86 (cons proc arguments-reader))
88 (define (command-info-procedure info)
91 (define (command-info-arguments-reader info)
94 (define (command-usage c)
95 (let ((doc (command-doc c)))
96 (substring doc 0 (string-index doc #\newline))))
98 (define (command-summary c)
99 (let* ((doc (command-doc c))
100 (start (1+ (string-index doc #\newline))))
101 (cond ((string-index doc #\newline start)
102 => (lambda (end) (substring doc start end)))
103 (else (substring doc start)))))
105 (define (lookup-group name)
106 (assq name *command-table*))
108 (define* (lookup-command key #:optional (table *command-table*))
109 (let loop ((groups table) (commands '()))
110 (cond ((and (null? groups) (null? commands)) #f)
112 (loop (cdr groups) (cdar groups)))
113 ((memq key (car commands)) (car commands))
114 (else (loop groups (cdr commands))))))
116 (define* (display-group group #:optional (abbrev? #t))
117 (format #t "~:(~A~) Commands~:[~; [abbrev]~]:~2%" (group-name group) abbrev?)
118 (for-each (lambda (c)
119 (display-summary (command-usage c)
120 (if abbrev? (command-abbrevs c) '())
121 (command-summary c)))
122 (group-commands group))
125 (define (display-command command)
127 (display (command-doc command))
130 (define (display-summary usage abbrevs summary)
131 (let* ((usage-len (string-length usage))
132 (abbrevs (if (pair? abbrevs)
133 (format #f "[,~A~{ ,~A~}]" (car abbrevs) (cdr abbrevs))
135 (abbrevs-len (string-length abbrevs)))
136 (format #t " ,~A~A~A - ~A\n"
140 (error "abbrevs too long" abbrevs))
141 ((> (+ usage-len abbrevs-len) 32)
142 (format #f "~%~v_" (+ 2 (- 32 abbrevs-len))))
144 (format #f "~v_" (- 32 abbrevs-len usage-len))))
148 (define (read-command repl)
153 ((,subr ,msg ,args . ,rest)
154 (format #t "Throw to key `~a' while reading command:\n" key)
155 (display-error #f (current-output-port) subr msg args rest))
157 (format #t "Throw to key `~a' with args `~s' while reading command.\n"
162 (define (read-command-arguments c repl)
163 ((command-info-arguments-reader (command-info c)) repl))
165 (define (meta-command repl)
166 (let ((command (read-command repl)))
168 ((eq? command *unspecified*)) ; read error, already signalled; pass.
169 ((not (symbol? command))
170 (format #t "Meta-command not a symbol: ~s~%" command))
171 ((lookup-command command)
173 (and=> (read-command-arguments c repl)
174 (lambda (args) (apply (command-procedure c) repl args)))))
176 (format #t "Unknown meta command: ~A~%" command)))))
178 (define (add-meta-command! name category proc argument-reader)
179 (hashq-set! *command-infos* name (make-command-info proc argument-reader))
181 (let ((entry (assq category *command-table*)))
183 (set-cdr! entry (append (cdr entry) (list (list name))))
184 (set! *command-table*
185 (append *command-table*
186 (list (list category (list name)))))))))
188 (define-syntax define-meta-command
190 ((_ ((name category) repl (expression0 ...) . datums) docstring b0 b1 ...)
194 (lambda* (repl expression0 ... . datums)
198 (define (handle-read-error form-name key args)
200 ((,subr ,msg ,args . ,rest)
201 (format #t "Throw to key `~a' while reading ~@[argument `~A' of ~]command `~A':\n"
203 (display-error #f (current-output-port) subr msg args rest))
205 (format #t "Throw to key `~a' with args `~s' while reading ~@[ argument `~A' of ~]command `~A'.\n"
206 key args form-name 'name)))
208 (% (let* ((expression0
213 (lambda* (#:optional (port (current-input-port)))
214 ((language-reader (repl-language repl))
215 port (current-module)))))
217 (handle-read-error 'expression0 k args))))
220 (list expression0 ...)
223 (let ((port (open-input-string (read-line))))
225 (let ((x (read port)))
228 (lp (cons x out)))))))
230 (handle-read-error #f k args)))))
231 (lambda (k) #f))))) ; the abort handler
233 ((_ ((name category) repl . datums) docstring b0 b1 ...)
234 (define-meta-command ((name category) repl () . datums)
235 docstring b0 b1 ...))
237 ((_ (name repl (expression0 ...) . datums) docstring b0 b1 ...)
238 (define-meta-command ((name #f) repl (expression0 ...) . datums)
239 docstring b0 b1 ...))
241 ((_ (name repl . datums) docstring b0 b1 ...)
242 (define-meta-command ((name #f) repl () . datums)
243 docstring b0 b1 ...))))
251 (define-meta-command (help repl . args)
252 "help [all | GROUP | [-c] COMMAND]
255 With one argument, tries to look up the argument as a group name, giving
256 help on that group if successful. Otherwise tries to look up the
257 argument as a command, giving help on the command.
259 If there is a command whose name is also a group name, use the ,help
260 -c COMMAND form to give help on the command instead of the group.
262 Without any argument, a list of help commands and command groups
266 (display-group (lookup-group 'help))
267 (display "Command Groups:\n\n")
268 (display-summary "help all" #f "List all commands")
269 (for-each (lambda (g)
270 (let* ((name (symbol->string (group-name g)))
271 (usage (string-append "help " name))
272 (header (string-append "List " name " commands")))
273 (display-summary usage #f header)))
274 (cdr *command-table*))
277 "Type `,help -c COMMAND' to show documentation of a particular command.")
280 (for-each display-group *command-table*))
281 ((,group) (guard (lookup-group group))
282 (display-group (lookup-group group)))
283 ((,command) (guard (lookup-command command))
284 (display-command (lookup-command command)))
285 ((-c ,command) (guard (lookup-command command))
286 (display-command (lookup-command command)))
288 (format #t "Unknown command or group: ~A~%" command))
290 (format #t "Unknown command: ~A~%" command))
292 (format #t "Bad arguments: ~A~%" args))))
294 (define-meta-command (show repl . args)
296 Gives information about Guile.
298 With one argument, tries to show a particular piece of information;
300 currently supported topics are `warranty' (or `w'), `copying' (or `c'),
301 and `version' (or `v').
303 Without any argument, a list of topics is displayed."
306 (display-group (car *show-table*) #f)
308 ((,topic) (guard (lookup-command topic *show-table*))
309 ((command-procedure (lookup-command topic *show-table*)) repl))
311 (format #t "Unknown topic: ~A~%" command))
313 (format #t "Bad arguments: ~A~%" args))))
315 ;;; `warranty', `copying' and `version' are "hidden" meta-commands, only
316 ;;; accessible via `show'. They have an entry in *command-infos* but not
317 ;;; in *command-table*.
319 (define-meta-command (warranty repl)
321 Details on the lack of warranty."
325 (define-meta-command (copying repl)
331 (define-meta-command (version repl)
333 Version information."
337 (define-meta-command (apropos repl regexp)
339 Find bindings/modules/packages."
340 (apropos (->string regexp)))
342 (define-meta-command (describe repl (form))
344 Show description/documentation."
346 (object-documentation
347 (let ((input (repl-parse repl form)))
349 (module-ref (current-module) input)
350 (repl-eval repl input)))))
353 (define-meta-command (option repl . args)
355 List/show/set options."
358 (for-each (lambda (spec)
359 (format #t " ~A~24t~A\n" (car spec) (cadr spec)))
360 (repl-options repl)))
362 (display (repl-option-ref repl key))
365 (repl-option-set! repl key val))))
367 (define-meta-command (quit repl)
377 (define-meta-command (module repl . args)
379 Change modules / Show current module."
381 (() (puts (module-name (current-module))))
382 ((,mod-name) (guard (list? mod-name))
383 (set-current-module (resolve-module mod-name)))
384 (,mod-name (set-current-module (resolve-module mod-name)))))
386 (define-meta-command (import repl . args)
388 Import modules / List those imported."
391 (let ((mod (resolve-interface name)))
393 (module-use! (current-module) mod)
394 (format #t "No such module: ~A~%" name))))
396 (for-each puts (map module-name (module-uses (current-module))))
397 (for-each use args))))
399 (define-meta-command (load repl file)
401 Load a file in the current module."
402 (load (->string file)))
404 (define-meta-command (reload repl . args)
406 Reload the given module, or the current module if none was given."
408 (() (reload-module (current-module)))
409 ((,mod-name) (guard (list? mod-name))
410 (reload-module (resolve-module mod-name)))
411 (,mod-name (reload-module (resolve-module mod-name)))))
413 (define-meta-command (binding repl)
415 List current bindings."
416 (module-for-each (lambda (k v) (format #t "~23A ~A\n" k v))
419 (define-meta-command (in repl module command-or-expression . args)
420 "in MODULE COMMAND-OR-EXPRESSION
421 Evaluate an expression or command in the context of module."
422 (let ((m (resolve-module module #:ensure #f)))
424 (pmatch command-or-expression
425 (('unquote ,command) (guard (lookup-command command))
426 (save-module-excursion
428 (set-current-module m)
429 (apply (command-procedure (lookup-command command)) repl args))))
432 (repl-print repl (eval expression m)))
434 (format #t "Invalid arguments to `in': expected a single expression or a command.\n")))
435 (format #t "No such module: ~s\n" module))))
439 ;;; Language commands
442 (define-meta-command (language repl name)
445 (let ((lang (lookup-language name))
446 (cur (repl-language repl)))
447 (format #t "Happy hacking with ~a! To switch back, type `,L ~a'.\n"
448 (language-title lang) (language-name cur))
449 (fluid-set! *current-language* lang)
450 (set! (repl-language repl) lang)))
457 (define-meta-command (compile repl (form))
459 Generate compiled code."
460 (let ((x (repl-compile repl (repl-parse repl form))))
461 (cond ((objcode? x) (guile:disassemble x))
462 (else (repl-print repl x)))))
464 (define-meta-command (compile-file repl file . opts)
467 (compile-file (->string file) #:opts opts))
469 (define-meta-command (expand repl (form))
471 Expand any macros in a form."
472 (let ((x (repl-expand repl (repl-parse repl form))))
473 (run-hook before-print-hook x)
476 (define-meta-command (optimize repl (form))
478 Run the optimizer on a piece of code and print the result."
479 (let ((x (repl-optimize repl (repl-parse repl form))))
480 (run-hook before-print-hook x)
483 (define (guile:disassemble x)
484 ((@ (language assembly disassemble) disassemble) x))
486 (define-meta-command (disassemble repl (form))
488 Disassemble a compiled procedure."
489 (let ((obj (repl-eval repl (repl-parse repl form))))
490 (if (or (program? obj) (objcode? obj))
491 (guile:disassemble obj)
492 (format #t "Argument to ,disassemble not a procedure or objcode: ~a~%"
495 (define-meta-command (disassemble-file repl file)
496 "disassemble-file FILE
498 (guile:disassemble (load-thunk-from-file (->string file))))
505 (define-meta-command (time repl (form))
508 (let* ((gc-start (gc-run-time))
509 (real-start (get-internal-real-time))
510 (run-start (get-internal-run-time))
511 (result (repl-eval repl (repl-parse repl form)))
512 (run-end (get-internal-run-time))
513 (real-end (get-internal-real-time))
514 (gc-end (gc-run-time)))
515 (define (diff start end)
516 (/ (- end start) 1.0 internal-time-units-per-second))
517 (repl-print repl result)
518 (format #t ";; ~,6Fs real time, ~,6Fs run time. ~,6Fs spent in GC.\n"
519 (diff real-start real-end)
520 (diff run-start run-end)
521 (diff gc-start gc-end))
524 (define-meta-command (profile repl (form) . opts)
529 (repl-prepare-eval-thunk repl (repl-parse repl form))
532 (define-meta-command (trace repl (form) . opts)
535 ;; FIXME: doc options, or somehow deal with them better
536 (apply call-with-trace
537 (repl-prepare-eval-thunk repl (repl-parse repl form))
538 (cons* #:width (terminal-width) opts)))
545 (define-syntax define-stack-command
548 ((_ (name repl . args) docstring body body* ...)
549 #`(define-meta-command (name repl . args)
551 (let ((debug (repl-debug repl)))
554 ((#,(datum->syntax #'repl 'frames)
555 (identifier-syntax (debug-frames debug)))
556 (#,(datum->syntax #'repl 'message)
557 (identifier-syntax (debug-error-message debug)))
558 (#,(datum->syntax #'repl 'for-trap?)
559 (identifier-syntax (debug-for-trap? debug)))
560 (#,(datum->syntax #'repl 'index)
562 (id (debug-index debug))
563 ((set! id exp) (set! (debug-index debug) exp))))
564 (#,(datum->syntax #'repl 'cur)
566 (vector-ref #,(datum->syntax #'repl 'frames)
567 #,(datum->syntax #'repl 'index)))))
569 (format #t "Nothing to debug.~%"))))))))
571 (define-stack-command (backtrace repl #:optional count
572 #:key (width (terminal-width)) full?)
573 "backtrace [COUNT] [#:width W] [#:full? F]
576 Print a backtrace of all stack frames, or innermost COUNT frames.
577 If COUNT is negative, the last COUNT frames will be shown."
582 #:for-trap? for-trap?))
584 (define-stack-command (up repl #:optional (count 1))
586 Select a calling stack frame.
588 Select and print stack frames that called this one.
589 An argument says how many frames up to go."
591 ((or (not (integer? count)) (<= count 0))
592 (format #t "Invalid argument to `up': expected a positive integer for COUNT.~%"))
593 ((>= (+ count index) (vector-length frames))
595 ((= index (1- (vector-length frames)))
596 (format #t "Already at outermost frame.\n"))
598 (set! index (1- (vector-length frames)))
599 (print-frame cur #:index index
600 #:next-source? (and (zero? index) for-trap?)))))
602 (set! index (+ count index))
603 (print-frame cur #:index index
604 #:next-source? (and (zero? index) for-trap?)))))
606 (define-stack-command (down repl #:optional (count 1))
608 Select a called stack frame.
610 Select and print stack frames called by this one.
611 An argument says how many frames down to go."
613 ((or (not (integer? count)) (<= count 0))
614 (format #t "Invalid argument to `down': expected a positive integer for COUNT.~%"))
615 ((< (- index count) 0)
618 (format #t "Already at innermost frame.\n"))
621 (print-frame cur #:index index #:next-source? for-trap?))))
623 (set! index (- index count))
624 (print-frame cur #:index index
625 #:next-source? (and (zero? index) for-trap?)))))
627 (define-stack-command (frame repl #:optional idx)
631 Show the selected frame.
632 With an argument, select a frame by index, then show it."
636 ((or (not (integer? idx)) (< idx 0))
637 (format #t "Invalid argument to `frame': expected a non-negative integer for IDX.~%"))
638 ((< idx (vector-length frames))
640 (print-frame cur #:index index
641 #:next-source? (and (zero? index) for-trap?)))
643 (format #t "No such frame.~%"))))
644 (else (print-frame cur #:index index
645 #:next-source? (and (zero? index) for-trap?)))))
647 (define-stack-command (procedure repl)
649 Print the procedure for the selected frame."
650 (repl-print repl (frame-procedure cur)))
652 (define-stack-command (locals repl #:key (width (terminal-width)))
654 Show local variables.
656 Show locally-bound variables in the selected frame."
657 (print-locals cur #:width width))
659 (define-stack-command (error-message repl)
663 Display the message associated with the error that started the current
665 (format #t "~a~%" (if (string? message) message "No error message")))
667 (define-meta-command (break repl (form))
669 Break on calls to PROCEDURE.
671 Starts a recursive prompt when PROCEDURE is called."
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-trap-at-procedure-call! proc)))
676 (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
678 (define-meta-command (break-at-source repl file line)
679 "break-at-source FILE LINE
680 Break when control reaches the given source location.
682 Starts a recursive prompt when control reaches line LINE of file FILE.
683 Note that the given source location must be inside a procedure."
684 (let ((file (if (symbol? file) (symbol->string file) file)))
685 (let ((idx (add-trap-at-source-location! file line)))
686 (format #t "Trap ~a: ~a.~%" idx (trap-name idx)))))
688 (define (repl-pop-continuation-resumer repl msg)
689 ;; Capture the dynamic environment with this prompt thing. The
690 ;; result is a procedure that takes a frame.
695 ;; Call frame->stack-vector before reinstating the
696 ;; continuation, so that we catch the %stacks fluid at
697 ;; the time of capture.
701 (frame-previous frame)))))))
703 (format #t "~a~%" msg)
704 (let ((vals (frame-return-values from)))
706 (format #t "No return values.~%")
708 (format #t "Return values:~%")
709 (for-each (lambda (x) (repl-print repl x)) vals))))
710 ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
711 #:debug (make-debug stack 0 msg #t))))))
713 (define-stack-command (finish repl)
715 Run until the current frame finishes.
717 Resume execution, breaking when the current frame finishes."
718 (let ((handler (repl-pop-continuation-resumer
719 repl (format #f "Return from ~a" cur))))
720 (add-ephemeral-trap-at-frame-finish! cur handler)
723 (define (repl-next-resumer msg)
724 ;; Capture the dynamic environment with this prompt thing. The
725 ;; result is a procedure that takes a frame.
726 (% (let ((stack (abort
728 ;; Call frame->stack-vector before reinstating the
729 ;; continuation, so that we catch the %stacks fluid
730 ;; at the time of capture.
732 (k (frame->stack-vector frame)))))))
733 (format #t "~a~%" msg)
734 ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
735 #:debug (make-debug stack 0 msg #t)))))
737 (define-stack-command (step repl)
739 Step until control reaches a different source location.
741 Step until control reaches a different source location."
742 (let ((msg (format #f "Step into ~a" cur)))
743 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
744 #:into? #t #:instruction? #f)
747 (define-stack-command (step-instruction repl)
749 Step until control reaches a different instruction.
751 Step until control reaches a different VM instruction."
752 (let ((msg (format #f "Step into ~a" cur)))
753 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
754 #:into? #t #:instruction? #t)
757 (define-stack-command (next repl)
759 Step until control reaches a different source location in the current frame.
761 Step until control reaches a different source location in the current frame."
762 (let ((msg (format #f "Step into ~a" cur)))
763 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
764 #:into? #f #:instruction? #f)
767 (define-stack-command (next-instruction repl)
769 Step until control reaches a different instruction in the current frame.
771 Step until control reaches a different VM instruction in the current frame."
772 (let ((msg (format #f "Step into ~a" cur)))
773 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
774 #:into? #f #:instruction? #t)
777 (define-meta-command (tracepoint repl (form))
778 "tracepoint PROCEDURE
779 Add a tracepoint to PROCEDURE.
781 A tracepoint will print out the procedure and its arguments, when it is
782 called, and its return value(s) when it returns."
783 (let ((proc (repl-eval repl (repl-parse repl form))))
784 (if (not (procedure? proc))
785 (error "Not a procedure: ~a" proc)
786 (let ((idx (add-trace-at-procedure-call! proc)))
787 (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
789 (define-meta-command (traps repl)
791 Show the set of currently attached traps.
793 Show the set of currently attached traps (breakpoints and tracepoints)."
794 (let ((traps (list-traps)))
796 (format #t "No traps set.~%")
797 (for-each (lambda (idx)
798 (format #t " ~a: ~a~a~%"
800 (if (trap-enabled? idx) "" " (disabled)")))
803 (define-meta-command (delete repl idx)
808 (if (not (integer? idx))
809 (error "expected a trap index (a non-negative integer)" idx)
812 (define-meta-command (disable repl idx)
817 (if (not (integer? idx))
818 (error "expected a trap index (a non-negative integer)" idx)
819 (disable-trap! idx)))
821 (define-meta-command (enable repl idx)
826 (if (not (integer? idx))
827 (error "expected a trap index (a non-negative integer)" idx)
830 (define-stack-command (registers repl)
834 Print the registers of the current frame."
835 (print-registers cur))
837 (define-meta-command (width repl #:optional x)
839 Set debug output width.
841 Set the number of screen columns in the output from `backtrace' and
844 (format #t "Set screen width to ~a columns.~%" (terminal-width)))
849 ;;; Inspection commands
852 (define-meta-command (inspect repl (form))
854 Inspect the result(s) of evaluating EXP."
855 (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
857 (for-each %inspect args))))
859 (define-meta-command (pretty-print repl (form))
861 Pretty-print the result(s) of evaluating EXP."
862 (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
866 (run-hook before-print-hook x)
875 (define-meta-command (gc repl)
880 (define-meta-command (statistics repl)
883 (let ((this-tms (times))
884 (this-gcs (gc-stats))
885 (last-tms (repl-tm-stats repl))
886 (last-gcs (repl-gc-stats repl)))
888 (let ((this-times (assq-ref this-gcs 'gc-times))
889 (last-times (assq-ref last-gcs 'gc-times)))
890 (display-diff-stat "GC times:" #t this-times last-times "times")
893 (let ((this-heap (assq-ref this-gcs 'heap-size))
894 (this-free (assq-ref this-gcs 'heap-free-size)))
895 (display-stat-title "Memory size:" "current" "limit")
896 (display-stat "heap" #f (- this-heap this-free) this-heap "bytes")
899 (let ((this-alloc (assq-ref this-gcs 'heap-total-allocated))
900 (last-alloc (assq-ref last-gcs 'heap-total-allocated)))
901 (display-stat-title "Bytes allocated:" "diff" "total")
902 (display-diff-stat "allocated" #f this-alloc last-alloc "bytes")
905 (let ((this-total (assq-ref this-gcs 'gc-time-taken))
906 (last-total (assq-ref last-gcs 'gc-time-taken)))
907 (display-stat-title "GC time taken:" "diff" "total")
908 (display-time-stat "total" this-total last-total)
910 ;; Process time spent
911 (let ((this-utime (tms:utime this-tms))
912 (last-utime (tms:utime last-tms))
913 (this-stime (tms:stime this-tms))
914 (last-stime (tms:stime last-tms))
915 (this-cutime (tms:cutime this-tms))
916 (last-cutime (tms:cutime last-tms))
917 (this-cstime (tms:cstime this-tms))
918 (last-cstime (tms:cstime last-tms)))
919 (display-stat-title "Process time spent:" "diff" "total")
920 (display-time-stat "user" this-utime last-utime)
921 (display-time-stat "system" this-stime last-stime)
922 (display-time-stat "child user" this-cutime last-cutime)
923 (display-time-stat "child system" this-cstime last-cstime)
927 (set! (repl-tm-stats repl) this-tms)
928 (set! (repl-gc-stats repl) this-gcs)))
930 (define (display-stat title flag field1 field2 unit)
931 (let ((fmt (format #f "~~20~AA ~~10@A /~~10@A ~~A~~%" (if flag "" "@"))))
932 (format #t fmt title field1 field2 unit)))
934 (define (display-stat-title title field1 field2)
935 (display-stat title #t field1 field2 ""))
937 (define (display-diff-stat title flag this last unit)
938 (display-stat title flag (- this last) this unit))
940 (define (display-time-stat title this last)
942 (format #f "~10,2F" (exact->inexact (/ num internal-time-units-per-second))))
943 (display-stat title #f (conv (- this last)) (conv this) "s"))
945 (define (display-mips-stat title this-time this-clock last-time last-clock)
946 (define (mips time clock)
947 (if (= time 0) "----" (format #f "~10,2F" (/ clock time 1000000.0))))
948 (display-stat title #f
949 (mips (- this-time last-time) (- this-clock last-clock))
950 (mips this-time this-clock) "mips"))