3 ;; Copyright (C) 2001, 2009, 2010, 2011 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 (disassemble x) (disassemble-file xx))
57 (profile (time t) (profile pr) (trace tr))
58 (debug (backtrace bt) (up) (down) (frame fr)
59 (procedure proc) (locals) (error-message error)
60 (break br bp) (break-at-source break-at bs)
61 (step s) (step-instruction si)
62 (next n) (next-instruction ni)
65 (traps) (delete del) (disable) (enable)
67 (inspect (inspect i) (pretty-print pp))
68 (system (gc) (statistics stat) (option o)
69 (quit q continue cont))))
72 '((show (warranty w) (copying c) (version v))))
74 (define (group-name g) (car g))
75 (define (group-commands g) (cdr g))
77 (define *command-infos* (make-hash-table))
78 (define (command-name c) (car c))
79 (define (command-abbrevs c) (cdr c))
80 (define (command-info c) (hashq-ref *command-infos* (command-name c)))
81 (define (command-procedure c) (command-info-procedure (command-info c)))
82 (define (command-doc c) (procedure-documentation (command-procedure c)))
84 (define (make-command-info proc arguments-reader)
85 (cons proc arguments-reader))
87 (define (command-info-procedure info)
90 (define (command-info-arguments-reader info)
93 (define (command-usage c)
94 (let ((doc (command-doc c)))
95 (substring doc 0 (string-index doc #\newline))))
97 (define (command-summary c)
98 (let* ((doc (command-doc c))
99 (start (1+ (string-index doc #\newline))))
100 (cond ((string-index doc #\newline start)
101 => (lambda (end) (substring doc start end)))
102 (else (substring doc start)))))
104 (define (lookup-group name)
105 (assq name *command-table*))
107 (define* (lookup-command key #:optional (table *command-table*))
108 (let loop ((groups table) (commands '()))
109 (cond ((and (null? groups) (null? commands)) #f)
111 (loop (cdr groups) (cdar groups)))
112 ((memq key (car commands)) (car commands))
113 (else (loop groups (cdr commands))))))
115 (define* (display-group group #:optional (abbrev? #t))
116 (format #t "~:(~A~) Commands~:[~; [abbrev]~]:~2%" (group-name group) abbrev?)
117 (for-each (lambda (c)
118 (display-summary (command-usage c)
119 (if abbrev? (command-abbrevs c) '())
120 (command-summary c)))
121 (group-commands group))
124 (define (display-command command)
126 (display (command-doc command))
129 (define (display-summary usage abbrevs summary)
130 (let* ((usage-len (string-length usage))
131 (abbrevs (if (pair? abbrevs)
132 (format #f "[,~A~{ ,~A~}]" (car abbrevs) (cdr abbrevs))
134 (abbrevs-len (string-length abbrevs)))
135 (format #t " ,~A~A~A - ~A\n"
139 (error "abbrevs too long" abbrevs))
140 ((> (+ usage-len abbrevs-len) 32)
141 (format #f "~%~v_" (+ 2 (- 32 abbrevs-len))))
143 (format #f "~v_" (- 32 abbrevs-len usage-len))))
147 (define (read-command repl)
152 ((,subr ,msg ,args . ,rest)
153 (format #t "Throw to key `~a' while reading command:\n" key)
154 (display-error #f (current-output-port) subr msg args rest))
156 (format #t "Throw to key `~a' with args `~s' while reading command.\n"
161 (define (read-command-arguments c repl)
162 ((command-info-arguments-reader (command-info c)) repl))
164 (define (meta-command repl)
165 (let ((command (read-command repl)))
167 ((eq? command *unspecified*)) ; read error, already signalled; pass.
168 ((not (symbol? command))
169 (format #t "Meta-command not a symbol: ~s~%" command))
170 ((lookup-command command)
172 (and=> (read-command-arguments c repl)
173 (lambda (args) (apply (command-procedure c) repl args)))))
175 (format #t "Unknown meta command: ~A~%" command)))))
177 (define (add-meta-command! name category proc argument-reader)
178 (hashq-set! *command-infos* name (make-command-info proc argument-reader))
180 (let ((entry (assq category *command-table*)))
182 (set-cdr! entry (append (cdr entry) (list (list name))))
183 (set! *command-table*
184 (append *command-table*
185 (list (list category (list name)))))))))
187 (define-syntax define-meta-command
189 ((_ ((name category) repl (expression0 ...) . datums) docstring b0 b1 ...)
193 (lambda* (repl expression0 ... . datums)
197 (define (handle-read-error form-name key args)
199 ((,subr ,msg ,args . ,rest)
200 (format #t "Throw to key `~a' while reading ~@[argument `~A' of ~]command `~A':\n"
202 (display-error #f (current-output-port) subr msg args rest))
204 (format #t "Throw to key `~a' with args `~s' while reading ~@[ argument `~A' of ~]command `~A'.\n"
205 key args form-name 'name)))
207 (% (let* ((expression0
212 (lambda* (#:optional (port (current-input-port)))
213 ((language-reader (repl-language repl))
214 port (current-module)))))
216 (handle-read-error 'expression0 k args))))
219 (list expression0 ...)
222 (let ((port (open-input-string (read-line))))
224 (let ((x (read port)))
227 (lp (cons x out)))))))
229 (handle-read-error #f k args)))))
230 (lambda (k) #f))))) ; the abort handler
232 ((_ ((name category) repl . datums) docstring b0 b1 ...)
233 (define-meta-command ((name category) repl () . datums)
234 docstring b0 b1 ...))
236 ((_ (name repl (expression0 ...) . datums) docstring b0 b1 ...)
237 (define-meta-command ((name #f) repl (expression0 ...) . datums)
238 docstring b0 b1 ...))
240 ((_ (name repl . datums) docstring b0 b1 ...)
241 (define-meta-command ((name #f) repl () . datums)
242 docstring b0 b1 ...))))
250 (define-meta-command (help repl . args)
251 "help [all | GROUP | [-c] COMMAND]
254 With one argument, tries to look up the argument as a group name, giving
255 help on that group if successful. Otherwise tries to look up the
256 argument as a command, giving help on the command.
258 If there is a command whose name is also a group name, use the ,help
259 -c COMMAND form to give help on the command instead of the group.
261 Without any argument, a list of help commands and command groups
265 (display-group (lookup-group 'help))
266 (display "Command Groups:\n\n")
267 (display-summary "help all" #f "List all commands")
268 (for-each (lambda (g)
269 (let* ((name (symbol->string (group-name g)))
270 (usage (string-append "help " name))
271 (header (string-append "List " name " commands")))
272 (display-summary usage #f header)))
273 (cdr *command-table*))
276 "Type `,help -c COMMAND' to show documentation of a particular command.")
279 (for-each display-group *command-table*))
280 ((,group) (guard (lookup-group group))
281 (display-group (lookup-group group)))
282 ((,command) (guard (lookup-command command))
283 (display-command (lookup-command command)))
284 ((-c ,command) (guard (lookup-command command))
285 (display-command (lookup-command command)))
287 (format #t "Unknown command or group: ~A~%" command))
289 (format #t "Unknown command: ~A~%" command))
291 (format #t "Bad arguments: ~A~%" args))))
293 (define-meta-command (show repl . args)
295 Gives information about Guile.
297 With one argument, tries to show a particular piece of information;
299 currently supported topics are `warranty' (or `w'), `copying' (or `c'),
300 and `version' (or `v').
302 Without any argument, a list of topics is displayed."
305 (display-group (car *show-table*) #f)
307 ((,topic) (guard (lookup-command topic *show-table*))
308 ((command-procedure (lookup-command topic *show-table*)) repl))
310 (format #t "Unknown topic: ~A~%" command))
312 (format #t "Bad arguments: ~A~%" args))))
314 ;;; `warranty', `copying' and `version' are "hidden" meta-commands, only
315 ;;; accessible via `show'. They have an entry in *command-infos* but not
316 ;;; in *command-table*.
318 (define-meta-command (warranty repl)
320 Details on the lack of warranty."
324 (define-meta-command (copying repl)
330 (define-meta-command (version repl)
332 Version information."
336 (define-meta-command (apropos repl regexp)
338 Find bindings/modules/packages."
339 (apropos (->string regexp)))
341 (define-meta-command (describe repl (form))
343 Show description/documentation."
344 (display (object-documentation (repl-eval repl (repl-parse repl form))))
347 (define-meta-command (option repl . args)
349 List/show/set options."
352 (for-each (lambda (spec)
353 (format #t " ~A~24t~A\n" (car spec) (cadr spec)))
354 (repl-options repl)))
356 (display (repl-option-ref repl key))
359 (repl-option-set! repl key val))))
361 (define-meta-command (quit repl)
371 (define-meta-command (module repl . args)
373 Change modules / Show current module."
375 (() (puts (module-name (current-module))))
376 ((,mod-name) (guard (list? mod-name))
377 (set-current-module (resolve-module mod-name)))
378 (,mod-name (set-current-module (resolve-module mod-name)))))
380 (define-meta-command (import repl . args)
382 Import modules / List those imported."
385 (let ((mod (resolve-interface name)))
387 (module-use! (current-module) mod)
388 (format #t "No such module: ~A~%" name))))
390 (for-each puts (map module-name (module-uses (current-module))))
391 (for-each use args))))
393 (define-meta-command (load repl file)
395 Load a file in the current module."
396 (load (->string file)))
398 (define-meta-command (reload repl . args)
400 Reload the given module, or the current module if none was given."
402 (() (reload-module (current-module)))
403 ((,mod-name) (guard (list? mod-name))
404 (reload-module (resolve-module mod-name)))
405 (,mod-name (reload-module (resolve-module mod-name)))))
407 (define-meta-command (binding repl)
409 List current bindings."
410 (module-for-each (lambda (k v) (format #t "~23A ~A\n" k v))
413 (define-meta-command (in repl module command-or-expression . args)
414 "in MODULE COMMAND-OR-EXPRESSION
415 Evaluate an expression or command in the context of module."
416 (let ((m (resolve-module module #:ensure #f)))
418 (pmatch command-or-expression
419 (('unquote ,command) (guard (lookup-command command))
420 (save-module-excursion
422 (set-current-module m)
423 (apply (command-procedure (lookup-command command)) repl args))))
426 (repl-print repl (eval expression m)))
428 (format #t "Invalid arguments to `in': expected a single expression or a command.\n")))
429 (format #t "No such module: ~s\n" module))))
433 ;;; Language commands
436 (define-meta-command (language repl name)
439 (let ((lang (lookup-language name))
440 (cur (repl-language repl)))
441 (format #t "Happy hacking with ~a! To switch back, type `,L ~a'.\n"
442 (language-title lang) (language-name cur))
443 (set! (repl-language repl) lang)))
450 (define-meta-command (compile repl (form))
452 Generate compiled code."
453 (let ((x (repl-compile repl (repl-parse repl form))))
454 (cond ((objcode? x) (guile:disassemble x))
455 (else (repl-print repl x)))))
457 (define-meta-command (compile-file repl file . opts)
460 (compile-file (->string file) #:opts opts))
462 (define (guile:disassemble x)
463 ((@ (language assembly disassemble) disassemble) x))
465 (define-meta-command (disassemble repl (form))
467 Disassemble a compiled procedure."
468 (let ((obj (repl-eval repl (repl-parse repl form))))
469 (if (or (program? obj) (objcode? obj))
470 (guile:disassemble obj)
471 (format #t "Argument to ,disassemble not a procedure or objcode: ~a~%"
474 (define-meta-command (disassemble-file repl file)
475 "disassemble-file FILE
477 (guile:disassemble (load-objcode (->string file))))
484 (define-meta-command (time repl (form))
487 (let* ((gc-start (gc-run-time))
488 (real-start (get-internal-real-time))
489 (run-start (get-internal-run-time))
490 (result (repl-eval repl (repl-parse repl form)))
491 (run-end (get-internal-run-time))
492 (real-end (get-internal-real-time))
493 (gc-end (gc-run-time)))
494 (define (diff start end)
495 (/ (- end start) 1.0 internal-time-units-per-second))
496 (repl-print repl result)
497 (format #t ";; ~,6Fs real time, ~,6Fs run time. ~,6Fs spent in GC.\n"
498 (diff real-start real-end)
499 (diff run-start run-end)
500 (diff gc-start gc-end))
503 (define-meta-command (profile repl (form) . opts)
508 (repl-prepare-eval-thunk repl (repl-parse repl form))
511 (define-meta-command (trace repl (form) . opts)
514 ;; FIXME: doc options, or somehow deal with them better
515 (apply call-with-trace
516 (repl-prepare-eval-thunk repl (repl-parse repl form))
524 (define-syntax define-stack-command
527 ((_ (name repl . args) docstring body body* ...)
528 #`(define-meta-command (name repl . args)
530 (let ((debug (repl-debug repl)))
533 ((#,(datum->syntax #'repl 'frames)
534 (identifier-syntax (debug-frames debug)))
535 (#,(datum->syntax #'repl 'message)
536 (identifier-syntax (debug-error-message debug)))
537 (#,(datum->syntax #'repl 'for-trap?)
538 (identifier-syntax (debug-for-trap? debug)))
539 (#,(datum->syntax #'repl 'index)
541 (id (debug-index debug))
542 ((set! id exp) (set! (debug-index debug) exp))))
543 (#,(datum->syntax #'repl 'cur)
545 (vector-ref #,(datum->syntax #'repl 'frames)
546 #,(datum->syntax #'repl 'index)))))
548 (format #t "Nothing to debug.~%"))))))))
550 (define-stack-command (backtrace repl #:optional count
551 #:key (width (terminal-width)) full?)
552 "backtrace [COUNT] [#:width W] [#:full? F]
555 Print a backtrace of all stack frames, or innermost COUNT frames.
556 If COUNT is negative, the last COUNT frames will be shown."
561 #:for-trap? for-trap?))
563 (define-stack-command (up repl #:optional (count 1))
565 Select a calling stack frame.
567 Select and print stack frames that called this one.
568 An argument says how many frames up to go."
570 ((or (not (integer? count)) (<= count 0))
571 (format #t "Invalid argument to `up': expected a positive integer for COUNT.~%"))
572 ((>= (+ count index) (vector-length frames))
574 ((= index (1- (vector-length frames)))
575 (format #t "Already at outermost frame.\n"))
577 (set! index (1- (vector-length frames)))
578 (print-frame cur #:index index
579 #:next-source? (and (zero? index) for-trap?)))))
581 (set! index (+ count index))
582 (print-frame cur #:index index
583 #:next-source? (and (zero? index) for-trap?)))))
585 (define-stack-command (down repl #:optional (count 1))
587 Select a called stack frame.
589 Select and print stack frames called by this one.
590 An argument says how many frames down to go."
592 ((or (not (integer? count)) (<= count 0))
593 (format #t "Invalid argument to `down': expected a positive integer for COUNT.~%"))
594 ((< (- index count) 0)
597 (format #t "Already at innermost frame.\n"))
600 (print-frame cur #:index index #:next-source? for-trap?))))
602 (set! index (- index count))
603 (print-frame cur #:index index
604 #:next-source? (and (zero? index) for-trap?)))))
606 (define-stack-command (frame repl #:optional idx)
610 Show the selected frame.
611 With an argument, select a frame by index, then show it."
615 ((or (not (integer? idx)) (< idx 0))
616 (format #t "Invalid argument to `frame': expected a non-negative integer for IDX.~%"))
617 ((< idx (vector-length frames))
619 (print-frame cur #:index index
620 #:next-source? (and (zero? index) for-trap?)))
622 (format #t "No such frame.~%"))))
623 (else (print-frame cur #:index index
624 #:next-source? (and (zero? index) for-trap?)))))
626 (define-stack-command (procedure repl)
628 Print the procedure for the selected frame."
629 (repl-print repl (frame-procedure cur)))
631 (define-stack-command (locals repl #:key (width (terminal-width)))
633 Show local variables.
635 Show locally-bound variables in the selected frame."
636 (print-locals cur #:width width))
638 (define-stack-command (error-message repl)
642 Display the message associated with the error that started the current
644 (format #t "~a~%" (if (string? message) message "No error message")))
646 (define-meta-command (break repl (form))
648 Break on calls to PROCEDURE.
650 Starts a recursive prompt when PROCEDURE is called."
651 (let ((proc (repl-eval repl (repl-parse repl form))))
652 (if (not (procedure? proc))
653 (error "Not a procedure: ~a" proc)
654 (let ((idx (add-trap-at-procedure-call! proc)))
655 (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
657 (define-meta-command (break-at-source repl file line)
658 "break-at-source FILE LINE
659 Break when control reaches the given source location.
661 Starts a recursive prompt when control reaches line LINE of file FILE.
662 Note that the given source location must be inside a procedure."
663 (let ((file (if (symbol? file) (symbol->string file) file)))
664 (let ((idx (add-trap-at-source-location! file line)))
665 (format #t "Trap ~a: ~a.~%" idx (trap-name idx)))))
667 (define (repl-pop-continuation-resumer repl msg)
668 ;; Capture the dynamic environment with this prompt thing. The
669 ;; result is a procedure that takes a frame.
674 ;; Call frame->stack-vector before reinstating the
675 ;; continuation, so that we catch the %stacks fluid at
676 ;; the time of capture.
680 (frame-previous frame)))))))
682 (format #t "~a~%" msg)
683 (let ((vals (frame-return-values from)))
685 (format #t "No return values.~%")
687 (format #t "Return values:~%")
688 (for-each (lambda (x) (repl-print repl x)) vals))))
689 ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
690 #:debug (make-debug stack 0 msg #t))))))
692 (define-stack-command (finish repl)
694 Run until the current frame finishes.
696 Resume execution, breaking when the current frame finishes."
697 (let ((handler (repl-pop-continuation-resumer
698 repl (format #f "Return from ~a" cur))))
699 (add-ephemeral-trap-at-frame-finish! cur handler)
702 (define (repl-next-resumer msg)
703 ;; Capture the dynamic environment with this prompt thing. The
704 ;; result is a procedure that takes a frame.
705 (% (let ((stack (abort
707 ;; Call frame->stack-vector before reinstating the
708 ;; continuation, so that we catch the %stacks fluid
709 ;; at the time of capture.
711 (k (frame->stack-vector frame)))))))
712 (format #t "~a~%" msg)
713 ((module-ref (resolve-interface '(system repl repl)) 'start-repl)
714 #:debug (make-debug stack 0 msg #t)))))
716 (define-stack-command (step repl)
718 Step until control reaches a different source location.
720 Step until control reaches a different source location."
721 (let ((msg (format #f "Step into ~a" cur)))
722 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
723 #:into? #t #:instruction? #f)
726 (define-stack-command (step-instruction repl)
728 Step until control reaches a different instruction.
730 Step until control reaches a different VM instruction."
731 (let ((msg (format #f "Step into ~a" cur)))
732 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
733 #:into? #t #:instruction? #t)
736 (define-stack-command (next repl)
738 Step until control reaches a different source location in the current frame.
740 Step until control reaches a different source location in the current frame."
741 (let ((msg (format #f "Step into ~a" cur)))
742 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
743 #:into? #f #:instruction? #f)
746 (define-stack-command (next-instruction repl)
748 Step until control reaches a different instruction in the current frame.
750 Step until control reaches a different VM instruction in the current frame."
751 (let ((msg (format #f "Step into ~a" cur)))
752 (add-ephemeral-stepping-trap! cur (repl-next-resumer msg)
753 #:into? #f #:instruction? #t)
756 (define-meta-command (tracepoint repl (form))
757 "tracepoint PROCEDURE
758 Add a tracepoint to PROCEDURE.
760 A tracepoint will print out the procedure and its arguments, when it is
761 called, and its return value(s) when it returns."
762 (let ((proc (repl-eval repl (repl-parse repl form))))
763 (if (not (procedure? proc))
764 (error "Not a procedure: ~a" proc)
765 (let ((idx (add-trace-at-procedure-call! proc)))
766 (format #t "Trap ~a: ~a.~%" idx (trap-name idx))))))
768 (define-meta-command (traps repl)
770 Show the set of currently attached traps.
772 Show the set of currently attached traps (breakpoints and tracepoints)."
773 (let ((traps (list-traps)))
775 (format #t "No traps set.~%")
776 (for-each (lambda (idx)
777 (format #t " ~a: ~a~a~%"
779 (if (trap-enabled? idx) "" " (disabled)")))
782 (define-meta-command (delete repl idx)
787 (if (not (integer? idx))
788 (error "expected a trap index (a non-negative integer)" idx)
791 (define-meta-command (disable repl idx)
796 (if (not (integer? idx))
797 (error "expected a trap index (a non-negative integer)" idx)
798 (disable-trap! idx)))
800 (define-meta-command (enable repl idx)
805 (if (not (integer? idx))
806 (error "expected a trap index (a non-negative integer)" idx)
809 (define-stack-command (registers repl)
813 Print the registers of the current frame."
814 (print-registers cur))
816 (define-meta-command (width repl #:optional x)
818 Set debug output width.
820 Set the number of screen columns in the output from `backtrace' and
823 (format #t "Set screen width to ~a columns.~%" (terminal-width)))
828 ;;; Inspection commands
831 (define-meta-command (inspect repl (form))
833 Inspect the result(s) of evaluating EXP."
834 (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
836 (for-each %inspect args))))
838 (define-meta-command (pretty-print repl (form))
840 Pretty-print the result(s) of evaluating EXP."
841 (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form))
845 (run-hook before-print-hook x)
854 (define-meta-command (gc repl)
859 (define-meta-command (statistics repl)
862 (let ((this-tms (times))
863 (this-gcs (gc-stats))
864 (last-tms (repl-tm-stats repl))
865 (last-gcs (repl-gc-stats repl)))
867 (let ((this-times (assq-ref this-gcs 'gc-times))
868 (last-times (assq-ref last-gcs 'gc-times)))
869 (display-diff-stat "GC times:" #t this-times last-times "times")
872 (let ((this-heap (assq-ref this-gcs 'heap-size))
873 (this-free (assq-ref this-gcs 'heap-free-size)))
874 (display-stat-title "Memory size:" "current" "limit")
875 (display-stat "heap" #f (- this-heap this-free) this-heap "bytes")
878 (let ((this-alloc (assq-ref this-gcs 'heap-total-allocated))
879 (last-alloc (assq-ref last-gcs 'heap-total-allocated)))
880 (display-stat-title "Bytes allocated:" "diff" "total")
881 (display-diff-stat "allocated" #f this-alloc last-alloc "bytes")
884 (let ((this-total (assq-ref this-gcs 'gc-time-taken))
885 (last-total (assq-ref last-gcs 'gc-time-taken)))
886 (display-stat-title "GC time taken:" "diff" "total")
887 (display-time-stat "total" this-total last-total)
889 ;; Process time spent
890 (let ((this-utime (tms:utime this-tms))
891 (last-utime (tms:utime last-tms))
892 (this-stime (tms:stime this-tms))
893 (last-stime (tms:stime last-tms))
894 (this-cutime (tms:cutime this-tms))
895 (last-cutime (tms:cutime last-tms))
896 (this-cstime (tms:cstime this-tms))
897 (last-cstime (tms:cstime last-tms)))
898 (display-stat-title "Process time spent:" "diff" "total")
899 (display-time-stat "user" this-utime last-utime)
900 (display-time-stat "system" this-stime last-stime)
901 (display-time-stat "child user" this-cutime last-cutime)
902 (display-time-stat "child system" this-cstime last-cstime)
906 (set! (repl-tm-stats repl) this-tms)
907 (set! (repl-gc-stats repl) this-gcs)))
909 (define (display-stat title flag field1 field2 unit)
910 (let ((fmt (format #f "~~20~AA ~~10@A /~~10@A ~~A~~%" (if flag "" "@"))))
911 (format #t fmt title field1 field2 unit)))
913 (define (display-stat-title title field1 field2)
914 (display-stat title #t field1 field2 ""))
916 (define (display-diff-stat title flag this last unit)
917 (display-stat title flag (- this last) this unit))
919 (define (display-time-stat title this last)
921 (format #f "~10,2F" (exact->inexact (/ num internal-time-units-per-second))))
922 (display-stat title #f (conv (- this last)) (conv this) "s"))
924 (define (display-mips-stat title this-time this-clock last-time last-clock)
925 (define (mips time clock)
926 (if (= time 0) "----" (format #f "~10,2F" (/ clock time 1000000.0))))
927 (display-stat title #f
928 (mips (- this-time last-time) (- this-clock last-clock))
929 (mips this-time this-clock) "mips"))