1 ;;; Guile VM debugging facilities
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 02110-1301 USA
21 (define-module (system vm inspect)
22 #:use-module (system base pmatch)
23 #:use-module (system base syntax)
24 #:use-module (system vm vm)
25 #:use-module (system vm frame)
26 #:use-module ((language assembly disassemble)
27 #:select ((disassemble . %disassemble)))
28 #:use-module (ice-9 rdelim)
29 #:use-module (ice-9 pretty-print)
30 #:use-module (ice-9 format)
31 #:use-module (system vm program)
35 (define (reverse-hashq h)
36 (let ((ret (make-hash-table)))
39 (hashq-set! ret v (cons k (hashq-ref ret v '()))))
43 (define (catch-bad-arguments thunk bad-args-thunk)
44 (catch 'wrong-number-of-args
46 (catch 'keyword-argument-error
53 (define (read-args prompt)
54 (define (read* reader)
55 (repl-reader prompt reader))
60 ((eof-object? chr) (list chr))
61 ((char=? chr #\newline) (cmd (next)))
62 ((char-whitespace? chr) (cmd (next)))
65 (let ((tok (read* read)))
66 (args (list tok) (next))))))
67 (define (args out chr)
69 ((eof-object? chr) (reverse out))
70 ((char=? chr #\newline) (reverse out))
71 ((char-whitespace? chr) (args out (next)))
74 (let ((tok (read* read)))
75 (args (cons tok out) (next))))))
84 (define-syntax-rule (define-command ((mod cname alias ...) . args)
87 (let ((c (lambda* args body ...)))
88 (set-procedure-property! c 'name 'cname)
89 (module-define! mod 'cname c)
90 (module-add! mod 'alias (module-local-variable mod 'cname))
94 (let ((commands (make-module)))
96 (format #f "~20@y inspect> " x))
98 (define-command ((commands quit q continue cont c))
102 (define-command ((commands print p))
103 "Print the current object using `pretty-print'."
106 (define-command ((commands write w))
107 "Print the current object using `write'."
110 (define-command ((commands display d))
111 "Print the current object using `display'."
114 (define-command ((commands disassemble x))
115 "Disassemble the current object, which should be objcode or a procedure."
120 (format #t "Error disassembling object: ~a\n" args))))
122 (define-command ((commands help h ?) #:optional cmd)
123 "Show this help message."
124 (let ((rhash (reverse-hashq (module-obarray commands))))
125 (define (help-cmd cmd)
126 (let* ((v (module-local-variable commands cmd))
128 (canonical-name (procedure-name p)))
130 (format #t "~a~{ ~:@(~a~)~}~?~%~a~&~%"
131 canonical-name (program-lambda-list p)
132 "~#[~:;~40t(aliases: ~@{~a~^, ~})~]"
133 (delq canonical-name (hashq-ref rhash v))
134 (procedure-documentation p))))
138 ((and (symbol? cmd) (module-local-variable commands cmd))
141 (format #t "Invalid command ~s.~%" cmd)
142 (format #t "Try `help' for a list of commands~%"))))
147 (procedure-name (variable-ref k)))
150 (string<? (symbol->string x)
151 (symbol->string y))))))
152 (format #t "Available commands:~%~%")
153 (for-each help-cmd names))))))
155 (define (handle cmd . args)
158 (module-local-variable commands cmd))
160 (let ((proc (variable-ref var)))
163 (apply (variable-ref var) args))
165 (format (current-error-port)
166 "Invalid arguments to ~a. Try `help ~a'.~%"
167 (procedure-name proc) (procedure-name proc)))))))
168 ; ((and (integer? cmd) (exact? cmd))
174 (format (current-error-port)
175 "~&Unknown command: ~a. Try `help'.~%" cmd)
183 (save-module-excursion
185 (set-current-module commands)
186 (read-args prompt))))
189 (apply values args)))))