3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program 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
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define-module (system repl command)
23 :use-syntax (system base syntax)
24 :use-module (system base language)
25 :use-module (system repl common)
26 :use-module (system il glil)
27 :use-module (system vm core)
28 :autoload (system vm trace) (vm-trace)
29 :autoload (system vm disasm) (disassemble-program disassemble-dumpcode)
30 :autoload (system vm profile) (vm-profile)
31 :use-module (ice-9 format)
32 :use-module (ice-9 session)
33 :export (meta-command))
35 (define (puts x) (display x) (newline))
37 (define (user-error msg . args)
38 (throw 'user-error #f msg args #f))
45 (define *command-table*
46 '((help (help h) (apropos a) (describe d) (option o) (quit q))
47 (module (module m) (use u) (import i) (load l) (binding b) (lsmod lm))
48 (package (package p) (lspkg lp) (autopackage) (globals g))
49 (language (language L))
50 (compile (compile c) (compile-file cc)
51 (disassemble x) (disassemble-file xx))
52 (profile (time t) (profile pr))
53 (debug (backtrace bt) (debugger db) (trace tr) (step st))
54 (system (statistics stat) (gc))))
56 (define (group-name g) (car g))
57 (define (group-commands g) (cdr g))
59 (define *command-module* (current-module))
60 (define (command-name c) (car c))
61 (define (command-abbrev c) (if (null? (cdr c)) #f (cadr c)))
62 (define (command-procedure c) (module-ref *command-module* (command-name c)))
63 (define (command-doc c) (procedure-documentation (command-procedure c)))
65 (define (command-usage c)
66 (let ((doc (command-doc c)))
67 (substring doc 0 (string-index doc #\newline))))
69 (define (command-summary c)
70 (let* ((doc (command-doc c))
71 (start (1+ (string-index doc #\newline))))
72 (cond ((string-index doc #\newline start)
73 => (lambda (end) (substring doc start end)))
74 (else (substring doc start)))))
76 (define (lookup-group name)
77 (assq name *command-table*))
79 (define (lookup-command key)
80 (let loop ((groups *command-table*) (commands '()))
81 (cond ((and (null? groups) (null? commands)) #f)
83 (loop (cdr groups) (cdar groups)))
84 ((memq key (car commands)) (car commands))
85 (else (loop groups (cdr commands))))))
87 (define (display-group group . opts)
88 (format #t "~:(~A~) Commands [abbrev]:~2%" (group-name group))
90 (display-summary (command-usage c)
93 (group-commands group))
96 (define (display-command command)
98 (display (command-doc command))
101 (define (display-summary usage abbrev summary)
102 (let ((abbrev (if abbrev (format #f "[,~A]" abbrev) "")))
103 (format #t " ,~24A ~8@A - ~A\n" usage abbrev summary)))
105 (define (meta-command repl line)
106 (let ((input (call-with-input-string (string-append "(" line ")") read)))
107 (if (not (null? input))
108 (do ((key (car input))
109 (args (cdr input) (cdr args))
110 (opts '() (cons (make-keyword-from-dash-symbol (car args)) opts)))
112 (not (symbol? (car args)))
113 (not (eq? (string-ref (symbol->string (car args)) 0) #\-)))
114 (let ((c (lookup-command key)))
116 (cond ((memq :h opts) (display-command c))
117 (else (apply (command-procedure c)
118 repl (append! args opts))))
119 (user-error "Unknown meta command: ~A" key))))))))
126 (define (help repl . args)
129 The optional argument can be either one of command groups or
130 command names. Without argument, a list of help commands and
131 all command groups are displayed, as you have already seen :)"
134 (display-group (lookup-group 'help))
135 (display "Command Groups:\n\n")
136 (display-summary "help all" #f "List all commands")
137 (for-each (lambda (g)
138 (let* ((name (symbol->string (group-name g)))
139 (usage (string-append "help " name))
140 (header (string-append "List " name " commands")))
141 (display-summary usage #f header)))
142 (cdr *command-table*))
144 (display "Enter `,COMMAND -h' to display documentation of each command.")
147 (for-each display-group *command-table*))
148 ((? lookup-group group)
149 (display-group (lookup-group group)))
150 (else (user-error "Unknown command group: ~A" (car args)))))
152 (define guile-apropos apropos)
153 (define (apropos repl regexp)
154 "apropos [options] REGEXP
155 Find bindings/modules/packages."
156 (guile-apropos (object->string regexp display)))
158 (define (describe repl obj)
160 Show description/documentation."
161 (display "Not implemented yet\n"))
163 (define (option repl . args)
164 "option [KEY [VALUE]]
165 List/show/set options."
166 (display "Not implemented yet\n"))
178 (define (module repl . args)
180 Change modules / Show current module."
182 (() (puts (binding repl.module)))))
184 (define (use repl . args)
188 (let ((mod (resolve-interface name)))
190 (module-use! repl.module mod)
191 (user-error "No such module: ~A" name))))
193 (for-each puts (map module-name
194 (cons repl.module (module-uses repl.module))))
195 (for-each (lambda (name)
197 ((pair? name) (use name))
199 (cond ((find-one-module (symbol->string name)) => use)))
200 (else (user-error "Invalid module name: ~A" name))))
203 (define (import repl . args)
205 Import modules / List those imported."
207 (let ((mod (resolve-interface name)))
209 (module-use! repl.module mod)
210 (user-error "No such module: ~A" name))))
212 (for-each puts (map module-name
213 (cons repl.module (module-uses repl.module))))
214 (for-each (lambda (name)
216 ((pair? name) (use name))
218 (and-let* ((m (find-one-module (symbol->string name))))
220 (else (user-error "Invalid module name: ~A" name))))
223 (define (load repl file . opts)
225 Load a file in the current module."
226 (apply repl-load-file repl (->string file) opts))
228 (define (binding repl . opts)
230 List current bindings."
231 (fold (lambda (s v d) (format #t "~23A ~A\n" s v)) #f repl.module))
233 (define (lsmod repl . args)
237 (set! repl.module (resolve-module name))
238 (module-use! repl.module repl.value-history))
241 (let ((name (car args)))
243 ((pair? name) (use name))
245 (and-let* ((m (find-one-module (symbol->string name))))
247 (else (user-error "Invalid module name: ~A" name))))))
254 (define (package repl)
256 List available packages/modules."
257 (for-each puts (find-module "")))
261 List available packages/modules."
262 (for-each puts (find-module "")))
264 (define (autopackage repl)
266 List available packages/modules."
267 (for-each puts (find-module "")))
269 (define (globals repl)
271 List all global variables."
272 (global-fold (lambda (s v d) (format #t "~A\t~S\n" s v)) #f))
276 ;;; Language commands
279 (define (language repl name)
282 (set! repl.language (lookup-language name))
290 (define (compile repl form . opts)
291 "compile [options] FORM
292 Generate compiled code.
294 -e Stop after expanding syntax/macro
295 -t Stop after translating into GHIL
296 -c Stop after generating GLIL
298 -O Enable optimization
299 -D Add debug information"
300 (let ((x (apply repl-compile repl form opts)))
301 (cond ((or (memq :e opts) (memq :t opts)) (puts x))
302 ((memq :c opts) (pprint-glil x))
303 (else (disassemble-dumpcode x)))))
305 (define (compile-file repl file . opts)
306 "compile-file [options] FILE
308 (apply repl-compile-file repl (->string file) opts))
310 (define (disassemble repl prog)
312 Disassemble a program."
313 (disassemble-program (repl-eval repl prog)))
315 (define (disassemble-file repl file)
316 "disassemble-file FILE
318 (disassemble-dumpcode (load-dumpcode (->string file))))
321 (object->string x display))
328 (define (profile repl form . opts)
331 (apply vm-profile repl.vm (repl-compile repl form) opts))
338 (define guile-backtrace backtrace)
339 (define (backtrace repl)
341 Show backtrace (if any)."
344 (define (debugger repl)
349 (define (trace repl form . opts)
352 (apply vm-trace repl.vm (repl-compile repl form) opts))
357 (display "Not implemented yet\n"))
364 (define (time repl form)
367 (let* ((vms-start (vm-stats repl.vm))
368 (gc-start (gc-run-time))
370 (result (repl-eval repl form))
372 (gc-end (gc-run-time))
373 (vms-end (vm-stats repl.vm)))
374 (define (get proc start end)
375 (/ (- (proc end) (proc start)) internal-time-units-per-second))
376 (repl-print repl result)
377 (display "clock utime stime cutime cstime gctime\n")
378 (format #t "~5,2F ~5,2F ~5,2F ~6,2F ~6,2F ~6,2F\n"
379 (get tms:clock tms-start tms-end)
380 (get tms:utime tms-start tms-end)
381 (get tms:stime tms-start tms-end)
382 (get tms:cutime tms-start tms-end)
383 (get tms:cstime tms-start tms-end)
384 (get id gc-start gc-end))
397 (define (statistics repl)
400 (let ((this-tms (times))
401 (this-vms (vm-stats repl.vm))
402 (this-gcs (gc-stats))
403 (last-tms repl.tm-stats)
404 (last-vms repl.vm-stats)
405 (last-gcs repl.gc-stats))
407 (let ((this-times (assq-ref this-gcs 'gc-times))
408 (last-times (assq-ref last-gcs 'gc-times)))
409 (display-diff-stat "GC times:" #t this-times last-times "times")
412 (let ((this-cells (assq-ref this-gcs 'cells-allocated))
413 (this-heap (assq-ref this-gcs 'cell-heap-size))
414 (this-bytes (assq-ref this-gcs 'bytes-malloced))
415 (this-malloc (assq-ref this-gcs 'gc-malloc-threshold)))
416 (display-stat-title "Memory size:" "current" "limit")
417 (display-stat "heap" #f this-cells this-heap "cells")
418 (display-stat "malloc" #f this-bytes this-malloc "bytes")
421 (let ((this-marked (assq-ref this-gcs 'cells-marked))
422 (last-marked (assq-ref last-gcs 'cells-marked))
423 (this-swept (assq-ref this-gcs 'cells-swept))
424 (last-swept (assq-ref last-gcs 'cells-swept)))
425 (display-stat-title "Cells collected:" "diff" "total")
426 (display-diff-stat "marked" #f this-marked last-marked "cells")
427 (display-diff-stat "swept" #f this-swept last-swept "cells")
430 (let ((this-mark (assq-ref this-gcs 'gc-mark-time-taken))
431 (last-mark (assq-ref last-gcs 'gc-mark-time-taken))
432 (this-sweep (assq-ref this-gcs 'gc-sweep-time-taken))
433 (last-sweep (assq-ref last-gcs 'gc-sweep-time-taken))
434 (this-total (assq-ref this-gcs 'gc-time-taken))
435 (last-total (assq-ref last-gcs 'gc-time-taken)))
436 (display-stat-title "GC time taken:" "diff" "total")
437 (display-time-stat "mark" this-mark last-mark)
438 (display-time-stat "sweep" this-sweep last-sweep)
439 (display-time-stat "total" this-total last-total)
441 ;; Process time spent
442 (let ((this-utime (tms:utime this-tms))
443 (last-utime (tms:utime last-tms))
444 (this-stime (tms:stime this-tms))
445 (last-stime (tms:stime last-tms))
446 (this-cutime (tms:cutime this-tms))
447 (last-cutime (tms:cutime last-tms))
448 (this-cstime (tms:cstime this-tms))
449 (last-cstime (tms:cstime last-tms)))
450 (display-stat-title "Process time spent:" "diff" "total")
451 (display-time-stat "user" this-utime last-utime)
452 (display-time-stat "system" this-stime last-stime)
453 (display-time-stat "child user" this-cutime last-cutime)
454 (display-time-stat "child system" this-cstime last-cstime)
457 (let ((this-time (vms:time this-vms))
458 (last-time (vms:time last-vms))
459 (this-clock (vms:clock this-vms))
460 (last-clock (vms:clock last-vms)))
461 (display-stat-title "VM statistics:" "diff" "total")
462 (display-time-stat "time spent" this-time last-time)
463 (display-diff-stat "bogoclock" #f this-clock last-clock "clock")
464 (display-mips-stat "bogomips" this-time this-clock last-time last-clock)
468 (set! repl.tm-stats this-tms)
469 (set! repl.vm-stats this-vms)
470 (set! repl.gc-stats this-gcs)))
472 (define (display-stat title flag field1 field2 unit)
473 (let ((str (format #f "~~20~AA ~~10@A /~~10@A ~~A~~%" (if flag "" "@"))))
474 (format #t str title field1 field2 unit)))
476 (define (display-stat-title title field1 field2)
477 (display-stat title #t field1 field2 ""))
479 (define (display-diff-stat title flag this last unit)
480 (display-stat title flag (- this last) this unit))
482 (define (display-time-stat title this last)
484 (format #f "~10,2F" (/ num internal-time-units-per-second)))
485 (display-stat title #f (conv (- this last)) (conv this) "s"))
487 (define (display-mips-stat title this-time this-clock last-time last-clock)
488 (define (mips time clock)
489 (if (= time 0) "----" (format #f "~10,2F" (/ clock time 1000000))))
490 (display-stat title #f
491 (mips (- this-time last-time) (- this-clock last-clock))
492 (mips this-time this-clock) "mips"))