1 ;;;; Guile Debugger command loop
3 ;;; Copyright (C) 1999, 2001, 2002, 2003, 2006 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 2.1 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
19 (define-module (ice-9 debugger command-loop)
20 #:use-module ((ice-9 debugger commands) :prefix debugger:)
21 #:export (debugger-command-loop
22 debugger-command-loop-error
23 debugger-command-loop-quit)
26 ;;; {Interface used by (ice-9 debugger).}
28 (define (debugger-command-loop state)
29 (read-and-dispatch-commands state (current-input-port)))
31 (define (debugger-command-loop-error message)
34 (define (debugger-command-loop-quit)
35 (throw 'exit-debugger))
39 (define debugger-prompt "debug> ")
41 (define (debugger-handler key . args)
46 (fluid-set! the-last-stack (fluid-ref before-signal-stack))
47 (apply display-error #f (current-error-port) args))
49 (display "Internal debugger error:\n")
50 (save-stack debugger-handler)
51 (apply throw key args)))
52 (throw 'exit-debugger)) ;Pop the stack
54 (define (read-and-dispatch-commands state port)
59 (with-fluids ((last-command #f))
61 (read-and-dispatch-command state port)
67 (define set-readline-prompt! #f)
69 (define (read-and-dispatch-command state port)
72 ;; Import set-readline-prompt! if we haven't already.
73 (or set-readline-prompt!
74 (set! set-readline-prompt!
75 (module-ref (resolve-module '(ice-9 readline))
76 'set-readline-prompt!)))
77 (set-readline-prompt! debugger-prompt debugger-prompt))
78 (display debugger-prompt))
79 (force-output) ;This should not be necessary...
80 (let ((token (read-token port)))
81 (cond ((eof-object? token)
82 (throw 'exit-debugger))
84 (discard-rest-of-line port)
85 (catch-user-errors port (lambda () (run-last-command state))))
87 (catch-user-errors port
89 (dispatch-command token command-table state port)))))))
91 (define (run-last-command state)
92 (let ((procedure (fluid-ref last-command)))
96 (define (catch-user-errors port thunk)
97 (catch 'debugger-user-error
99 (lambda (key . objects)
100 (apply user-warning objects)
101 (discard-rest-of-line port))))
103 (define last-command (make-fluid))
105 (define (user-warning . objects)
106 (for-each (lambda (object)
111 (define (user-error . objects)
112 (apply throw 'debugger-user-error objects))
114 ;;;; Command dispatch
116 (define (dispatch-command string table state port)
117 (let ((value (command-table-value table string)))
119 (dispatch-command/value value state port)
120 (user-error "Unknown command: " string))))
122 (define (dispatch-command/value value state port)
123 (cond ((command? value)
124 (dispatch-command/command value state port))
125 ((command-table? value)
126 (dispatch-command/table value state port))
128 (dispatch-command/name value state port))
130 (error "Unrecognized command-table value: " value))))
132 (define (dispatch-command/command command state port)
133 (let ((procedure (command-procedure command))
134 (arguments ((command-parser command) port)))
135 (let ((procedure (lambda (state) (apply procedure state arguments))))
136 (warn-about-extra-args port)
137 (fluid-set! last-command procedure)
140 (define (warn-about-extra-args port)
141 ;; **** modify this to show the arguments.
142 (let ((char (skip-whitespace port)))
143 (cond ((eof-object? char) #f)
144 ((char=? #\newline char) (read-char port))
146 (user-warning "Extra arguments at end of line: "
147 (read-rest-of-line port))))))
149 (define (dispatch-command/table table state port)
150 (let ((token (read-token port)))
151 (if (or (eof-object? token)
153 (user-error "Command name too short.")
154 (dispatch-command token table state port))))
156 (define (dispatch-command/name name state port)
157 (let ((value (lookup-command name)))
159 (apply user-error "Unknown command name: " name))
160 ((command-table? value)
161 (apply user-error "Partial command name: " name))
163 (dispatch-command/value value state port)))))
165 ;;;; Command definition
167 (define (define-command name argument-template procedure)
168 (let ((name (canonicalize-command-name name)))
171 (argument-template->parser argument-template)
172 (procedure-documentation procedure)
177 (define (define-command-alias name1 name2)
178 (let ((name1 (canonicalize-command-name name1)))
179 (add-command name1 (canonicalize-command-name name2) command-table)
182 (define (argument-template->parser template)
183 ;; Deliberately handles only cases that occur in "commands.scm".
184 (cond ((eq? 'tokens template)
186 (let loop ((tokens '()))
187 (let ((token (read-token port)))
188 (if (or (eof-object? token)
190 (list (reverse! tokens))
191 (loop (cons token tokens)))))))
195 ((and (pair? template)
196 (null? (cdr template))
197 (eq? 'object (car template)))
200 ((and (pair? template)
201 (equal? ''optional (car template))
202 (pair? (cdr template))
203 (null? (cddr template)))
204 (case (cadr template)
207 (let ((token (read-token port)))
208 (if (or (eof-object? token)
214 (list (parse-optional-exact-integer port))))
215 ((exact-nonnegative-integer)
217 (list (parse-optional-exact-nonnegative-integer port))))
220 (list (parse-optional-object port))))
222 (error "Malformed argument template: " template))))
224 (error "Malformed argument template: " template))))
226 (define (parse-optional-exact-integer port)
227 (let ((object (parse-optional-object port)))
229 (and (integer? object)
232 (user-error "Argument not an exact integer: " object))))
234 (define (parse-optional-exact-nonnegative-integer port)
235 (let ((object (parse-optional-object port)))
237 (and (integer? object)
239 (not (negative? object))))
241 (user-error "Argument not an exact non-negative integer: " object))))
243 (define (parse-optional-object port)
244 (let ((terminator (skip-whitespace port)))
245 (if (or (eof-object? terminator)
246 (eq? #\newline terminator))
248 (let ((object (read port)))
249 (if (eof-object? object)
255 (define (lookup-command name)
256 (let loop ((table command-table) (strings name))
257 (let ((value (command-table-value table (car strings))))
258 (cond ((or (not value) (null? (cdr strings))) value)
259 ((command-table? value) (loop value (cdr strings)))
262 (define (command-table-value table string)
263 (let ((entry (command-table-entry table string)))
267 (define (command-table-entry table string)
268 (let loop ((entries (command-table-entries table)))
269 (and (not (null? entries))
270 (let ((entry (car entries)))
271 (if (and (<= (cadr entry)
272 (string-length string)
273 (string-length (car entry)))
274 (= (string-length string)
275 (match-strings (car entry) string)))
277 (loop (cdr entries)))))))
279 (define (match-strings s1 s2)
280 (let ((n (min (string-length s1) (string-length s2))))
283 ((char=? (string-ref s1 i) (string-ref s2 i)) (loop (+ i 1)))
286 (define (write-command-name name)
288 (for-each (lambda (string)
293 (define (add-command name value table)
294 (let loop ((strings name) (table table))
296 (or (let loop ((entries (command-table-entries table)))
297 (and (not (null? entries))
298 (if (string=? (car strings) (caar entries))
300 (loop (cdr entries)))))
301 (let ((entry (list (car strings) #f #f)))
303 (let ((entries (command-table-entries table)))
304 (if (or (null? entries)
305 (string<? (car strings) (caar entries)))
308 (let loop ((prev entries) (this (cdr entries)))
310 (string<? (car strings) (caar this)))
311 (set-cdr! prev (cons entry this))
312 (loop this (cdr this))))
314 (compute-string-abbreviations! entries)
315 (set-command-table-entries! table entries))
317 (if (null? (cdr strings))
318 (set-car! (cddr entry) value)
320 (if (command-table? (caddr entry))
322 (let ((table (make-command-table '())))
323 (set-car! (cddr entry) table)
326 (define (canonicalize-command-name name)
327 (cond ((and (string? name)
328 (not (string-null? name)))
330 ((let loop ((name name))
333 (not (string-null? (car name)))
334 (or (null? (cdr name))
338 (error "Illegal command name: " name))))
340 (define (compute-string-abbreviations! entries)
341 (let loop ((entries entries) (index 0))
345 (let* ((char (string-ref (car entry) index))
346 (group (assv char groups)))
348 (set-cdr! group (cons entry (cdr group)))
350 (cons (list char entry)
355 (let ((index (+ index 1)))
356 (if (null? (cddr group))
357 (set-car! (cdadr group) index)
359 (let loop ((entries (cdr group)))
360 (and (not (null? entries))
361 (if (= index (string-length (caar entries)))
363 (loop (cdr entries)))))))
366 (set-car! (cdr entry) index)
367 (delq entry (cdr group)))
374 (define command-table-rtd (make-record-type "command-table" '(entries)))
375 (define make-command-table (record-constructor command-table-rtd '(entries)))
376 (define command-table? (record-predicate command-table-rtd))
377 (define command-table-entries (record-accessor command-table-rtd 'entries))
378 (define set-command-table-entries!
379 (record-modifier command-table-rtd 'entries))
382 (make-record-type "command"
383 '(name parser documentation procedure)))
386 (record-constructor command-rtd
387 '(name parser documentation procedure)))
389 (define command? (record-predicate command-rtd))
390 (define command-name (record-accessor command-rtd 'name))
391 (define command-parser (record-accessor command-rtd 'parser))
392 (define command-documentation (record-accessor command-rtd 'documentation))
393 (define command-procedure (record-accessor command-rtd 'procedure))
395 ;;;; Character parsing
397 (define (read-token port)
401 (let ((char (peek-char port)))
402 (cond ((eof-object? char)
404 ((char=? #\newline char)
406 ((char-whitespace? char)
410 (let ((terminator (skip-comment port)))
411 (if (eof-object? char)
416 (loop (cons char chars)))))))
426 (list->string (reverse! chars))))))
427 (skip-whitespace port)
430 (define (skip-whitespace port)
431 (let ((char (peek-char port)))
432 (cond ((or (eof-object? char)
433 (char=? #\newline char))
435 ((char-whitespace? char)
437 (skip-whitespace port))
443 (define (skip-comment port)
444 (let ((char (peek-char port)))
445 (if (or (eof-object? char)
446 (char=? #\newline char))
450 (skip-comment port)))))
452 (define (read-rest-of-line port)
453 (let loop ((chars '()))
454 (let ((char (read-char port)))
455 (if (or (eof-object? char)
456 (char=? #\newline char))
457 (list->string (reverse! chars))
458 (loop (cons char chars))))))
460 (define (discard-rest-of-line port)
462 (if (not (let ((char (read-char port)))
463 (or (eof-object? char)
464 (char=? #\newline char))))
469 (define command-table (make-command-table '()))
471 (define-command "help" 'tokens
472 (lambda (state tokens)
473 "Type \"help\" followed by a command name for full documentation."
474 (let loop ((name (if (null? tokens) '("help") tokens)))
475 (let ((value (lookup-command name)))
477 (write-command-name name)
478 (display " is not a known command name.")
481 (display (command-documentation value))
483 (if (equal? '("help") (command-name value))
485 (display "Available commands are:")
487 (for-each (lambda (entry)
488 (if (not (list? (caddr entry)))
491 (display (car entry))
493 (command-table-entries command-table)))))
494 ((command-table? value)
496 (write-command-name name)
497 (display "\" command requires a subcommand.")
499 (display "Available subcommands are:")
501 (for-each (lambda (entry)
502 (if (not (list? (caddr entry)))
505 (write-command-name name)
507 (display (car entry))
509 (command-table-entries value)))
513 (error "Unknown value from lookup-command:" value)))))
516 (define-command "frame" '('optional exact-nonnegative-integer) debugger:frame)
518 (define-command "position" '() debugger:position)
520 (define-command "up" '('optional exact-integer) debugger:up)
522 (define-command "down" '('optional exact-integer) debugger:down)
524 (define-command "backtrace" '('optional exact-integer) debugger:backtrace)
526 (define-command "evaluate" '(object) debugger:evaluate)
528 (define-command '("info" "args") '() debugger:info-args)
530 (define-command '("info" "frame") '() debugger:info-frame)
532 (define-command "quit" '()
535 (debugger-command-loop-quit)))
537 (define-command-alias "f" "frame")
538 (define-command-alias '("info" "f") '("info" "frame"))
539 (define-command-alias "bt" "backtrace")
540 (define-command-alias "where" "backtrace")
541 (define-command-alias "p" "evaluate")
542 (define-command-alias '("info" "stack") "backtrace")