3 ;;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;;; This program is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU General Public License as
7 ;;; published by the Free Software Foundation; either version 2, or
8 ;;; (at your option) any later version.
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 GNU
13 ;;; General Public License for more details.
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this software; 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
20 (define-module (ice-9 debugger)
21 :use-module (ice-9 debug))
23 (if (memq 'readline *features*)
24 (define-module (ice-9 debugger)
25 :use-module (ice-9 readline)))
28 (define debugger-prompt "debug> ")
30 (define-public (debug)
31 (let ((stack (fluid-ref the-last-stack)))
33 (let ((state (make-state stack 0)))
34 (display "This is the Guile debugger; type \"help\" for help.")
36 (display "There are ")
37 (write (stack-length stack))
38 (display " frames on the stack.")
41 (write-state-short state)
42 (read-and-dispatch-commands state (current-input-port)))
43 (display "Nothing to debug.\n"))))
45 (define (read-and-dispatch-commands state port)
48 (with-fluids ((last-command #f))
49 (let loop ((state state))
50 (loop (read-and-dispatch-command state port)))))
54 (define (read-and-dispatch-command state port)
56 (set-readline-prompt! debugger-prompt)
57 (display debugger-prompt))
58 (force-output) ;This should not be necessary...
59 (let ((token (read-token port)))
60 (cond ((eof-object? token)
61 (throw 'exit-debugger))
63 (discard-rest-of-line port)
64 (catch-user-errors port (lambda () (run-last-command state))))
66 (catch-user-errors port
68 (dispatch-command token command-table state port)))))))
70 (define (run-last-command state)
71 (let ((procedure (fluid-ref last-command)))
75 (define (catch-user-errors port thunk)
76 (catch 'debugger-user-error
78 (lambda (key . objects)
79 (apply user-warning objects)
80 (discard-rest-of-line port))))
82 (define last-command (make-fluid))
84 (define (user-warning . objects)
85 (for-each (lambda (object)
90 (define (user-error . objects)
91 (apply throw 'debugger-user-error objects))
95 (define (dispatch-command string table state port)
96 (let ((value (command-table-value table string)))
98 (dispatch-command/value value state port)
99 (user-error "Unknown command: " string))))
101 (define (dispatch-command/value value state port)
102 (cond ((command? value)
103 (dispatch-command/command value state port))
104 ((command-table? value)
105 (dispatch-command/table value state port))
107 (dispatch-command/name value state port))
109 (error "Unrecognized command-table value: " value))))
111 (define (dispatch-command/command command state port)
112 (let ((procedure (command-procedure command))
113 (arguments ((command-parser command) port)))
114 (let ((procedure (lambda (state) (apply procedure state arguments))))
115 (warn-about-extra-args port)
116 (fluid-set! last-command procedure)
119 (define (warn-about-extra-args port)
120 ;; **** modify this to show the arguments.
121 (let ((char (skip-whitespace port)))
122 (cond ((eof-object? char) #f)
123 ((char=? #\newline char) (read-char port))
125 (user-warning "Extra arguments at end of line: "
126 (read-rest-of-line port))))))
128 (define (dispatch-command/table table state port)
129 (let ((token (read-token port)))
130 (if (or (eof-object? token)
132 (user-error "Command name too short.")
133 (dispatch-command token table state port))))
135 (define (dispatch-command/name name state port)
136 (let ((value (lookup-command name)))
138 (apply user-error "Unknown command name: " name))
139 ((command-table? value)
140 (apply user-error "Partial command name: " name))
142 (dispatch-command/value value state port)))))
144 ;;;; Command definition
146 (define (define-command name argument-template documentation procedure)
147 (let ((name (canonicalize-command-name name)))
150 (argument-template->parser argument-template)
156 (define (define-command-alias name1 name2)
157 (let ((name1 (canonicalize-command-name name1)))
158 (add-command name1 (canonicalize-command-name name2) command-table)
161 (define (argument-template->parser template)
162 ;; Deliberately handles only cases that occur in "commands.scm".
163 (cond ((eq? 'tokens template)
165 (let loop ((tokens '()))
166 (let ((token (read-token port)))
167 (if (or (eof-object? token)
169 (list (reverse! tokens))
170 (loop (cons token tokens)))))))
174 ((and (pair? template)
175 (null? (cdr template))
176 (eq? 'object (car template)))
179 ((and (pair? template)
180 (equal? ''optional (car template))
181 (pair? (cdr template))
182 (null? (cddr template)))
183 (case (cadr template)
186 (let ((token (read-token port)))
187 (if (or (eof-object? token)
193 (list (parse-optional-exact-integer port))))
194 ((exact-nonnegative-integer)
196 (list (parse-optional-exact-nonnegative-integer port))))
199 (list (parse-optional-object port))))
201 (error "Malformed argument template: " template))))
203 (error "Malformed argument template: " template))))
205 (define (parse-optional-exact-integer port)
206 (let ((object (parse-optional-object port)))
208 (and (integer? object)
211 (user-error "Argument not an exact integer: " object))))
213 (define (parse-optional-exact-nonnegative-integer port)
214 (let ((object (parse-optional-object port)))
216 (and (integer? object)
218 (not (negative? object))))
220 (user-error "Argument not an exact non-negative integer: " object))))
222 (define (parse-optional-object port)
223 (let ((terminator (skip-whitespace port)))
224 (if (or (eof-object? terminator)
225 (eq? #\newline terminator))
227 (let ((object (read port)))
228 (if (eof-object? object)
234 (define (lookup-command name)
235 (let loop ((table command-table) (strings name))
236 (let ((value (command-table-value table (car strings))))
237 (cond ((or (not value) (null? (cdr strings))) value)
238 ((command-table? value) (loop value (cdr strings)))
241 (define (command-table-value table string)
242 (let ((entry (command-table-entry table string)))
246 (define (command-table-entry table string)
247 (let loop ((entries (command-table-entries table)))
248 (and (not (null? entries))
249 (let ((entry (car entries)))
250 (if (and (<= (cadr entry)
251 (string-length string)
252 (string-length (car entry)))
253 (= (string-length string)
254 (match-strings (car entry) string)))
256 (loop (cdr entries)))))))
258 (define (match-strings s1 s2)
259 (let ((n (min (string-length s1) (string-length s2))))
262 ((char=? (string-ref s1 i) (string-ref s2 i)) (loop (+ i 1)))
265 (define (write-command-name name)
267 (for-each (lambda (string)
272 (define (add-command name value table)
273 (let loop ((strings name) (table table))
275 (or (let loop ((entries (command-table-entries table)))
276 (and (not (null? entries))
277 (if (string=? (car strings) (caar entries))
279 (loop (cdr entries)))))
280 (let ((entry (list (car strings) #f #f)))
282 (let ((entries (command-table-entries table)))
283 (if (or (null? entries)
284 (string<? (car strings) (caar entries)))
287 (let loop ((prev entries) (this (cdr entries)))
289 (string<? (car strings) (caar this)))
290 (set-cdr! prev (cons entry this))
291 (loop this (cdr this))))
293 (compute-string-abbreviations! entries)
294 (set-command-table-entries! table entries))
296 (if (null? (cdr strings))
297 (set-car! (cddr entry) value)
299 (if (command-table? (caddr entry))
301 (let ((table (make-command-table '())))
302 (set-car! (cddr entry) table)
305 (define (canonicalize-command-name name)
306 (cond ((and (string? name)
307 (not (string-null? name)))
309 ((let loop ((name name))
312 (not (string-null? (car name)))
313 (or (null? (cdr name))
317 (error "Illegal command name: " name))))
319 (define (compute-string-abbreviations! entries)
320 (let loop ((entries entries) (index 0))
324 (let* ((char (string-ref (car entry) index))
325 (group (assv char groups)))
327 (set-cdr! group (cons entry (cdr group)))
329 (cons (list char entry)
334 (let ((index (+ index 1)))
335 (if (null? (cddr group))
336 (set-car! (cdadr group) index)
338 (let loop ((entries (cdr group)))
339 (and (not (null? entries))
340 (if (= index (string-length (caar entries)))
342 (loop (cdr entries)))))))
345 (set-car! (cdr entry) index)
346 (delq entry (cdr group)))
353 (define command-table-rtd (make-record-type "command-table" '(entries)))
354 (define make-command-table (record-constructor command-table-rtd '(entries)))
355 (define command-table? (record-predicate command-table-rtd))
356 (define command-table-entries (record-accessor command-table-rtd 'entries))
357 (define set-command-table-entries!
358 (record-modifier command-table-rtd 'entries))
361 (make-record-type "command"
362 '(name parser documentation procedure)))
365 (record-constructor command-rtd
366 '(name parser documentation procedure)))
368 (define command? (record-predicate command-rtd))
369 (define command-name (record-accessor command-rtd 'name))
370 (define command-parser (record-accessor command-rtd 'parser))
371 (define command-documentation (record-accessor command-rtd 'documentation))
372 (define command-procedure (record-accessor command-rtd 'procedure))
374 (define state-rtd (make-record-type "debugger-state" '(stack index)))
375 (define state? (record-predicate state-rtd))
376 (define make-state (record-constructor state-rtd '(stack index)))
377 (define state-stack (record-accessor state-rtd 'stack))
378 (define state-index (record-accessor state-rtd 'index))
380 (define (new-state-index state index)
381 (make-state (state-stack state) index))
383 ;;;; Character parsing
385 (define (read-token port)
389 (let ((char (peek-char port)))
390 (cond ((eof-object? char)
392 ((char=? #\newline char)
394 ((char-whitespace? char)
398 (let ((terminator (skip-comment port)))
399 (if (eof-object? char)
404 (loop (cons char chars)))))))
414 (list->string (reverse! chars))))))
415 (skip-whitespace port)
418 (define (skip-whitespace port)
419 (let ((char (peek-char port)))
420 (cond ((or (eof-object? char)
421 (char=? #\newline char))
423 ((char-whitespace? char)
425 (skip-whitespace port))
431 (define (skip-comment port)
432 (let ((char (peek-char port)))
433 (if (or (eof-object? char)
434 (char=? #\newline char))
438 (skip-comment port)))))
440 (define (read-rest-of-line port)
441 (let loop ((chars '()))
442 (let ((char (read-char port)))
443 (if (or (eof-object? char)
444 (char=? #\newline char))
445 (list->string (reverse! chars))
446 (loop (cons char chars))))))
448 (define (discard-rest-of-line port)
450 (if (not (let ((char (read-char port)))
451 (or (eof-object? char)
452 (char=? #\newline char))))
457 (define command-table (make-command-table '()))
459 (define-command "help" 'tokens
460 "Type \"help\" followed by a command name for full documentation."
461 (lambda (state tokens)
462 (let loop ((name (if (null? tokens) '("help") tokens)))
463 (let ((value (lookup-command name)))
465 (write-command-name name)
466 (display " is not a known command name.")
469 (display (command-documentation value))
471 (if (equal? '("help") (command-name value))
473 (display "Available commands are:")
475 (for-each (lambda (entry)
476 (if (not (list? (caddr entry)))
479 (display (car entry))
481 (command-table-entries command-table)))))
482 ((command-table? value)
484 (write-command-name name)
485 (display "\" command requires a subcommand.")
487 (display "Available subcommands are:")
489 (for-each (lambda (entry)
490 (if (not (list? (caddr entry)))
493 (write-command-name name)
495 (display (car entry))
497 (command-table-entries value)))
501 (error "Unknown value from lookup-command:" value)))))
504 (define-command "frame" '('optional exact-nonnegative-integer)
505 "Select and print a stack frame.
506 With no argument, print the selected stack frame. (See also \"info frame\").
507 An argument specifies the frame to select; it must be a stack-frame number."
509 (let ((state (if n (select-frame-absolute state n) state)))
510 (write-state-short state)
513 (define-command "up" '('optional exact-integer)
514 "Move N frames up the stack. For positive numbers N, this advances
515 toward the outermost frame, to higher frame numbers, to frames
516 that have existed longer. N defaults to one."
518 (let ((state (select-frame-relative state (or n 1))))
519 (write-state-short state)
522 (define-command "down" '('optional exact-integer)
523 "Move N frames down the stack. For positive numbers N, this
524 advances toward the innermost frame, to lower frame numbers, to
525 frames that were created more recently. N defaults to one."
527 (let ((state (select-frame-relative state (- (or n 1)))))
528 (write-state-short state)
531 (define (eval-handler key . args)
533 (make-stack #t eval-handler)
534 (current-output-port)
538 (define-command "evaluate" '(object)
539 "Evaluate an expression.
540 The expression must appear on the same line as the command,
541 however it may be continued over multiple lines."
542 (lambda (state expression)
543 (let ((source (frame-source (stack-ref (state-stack state)
544 (state-index state)))))
546 (display "No environment for this frame.\n")
551 (let* ((env (memoized-environment source))
552 (value (local-eval expression env)))
560 (define-command "backtrace" '('optional exact-integer)
561 "Print backtrace of all stack frames, or innermost COUNT frames.
562 With a negative argument, print outermost -COUNT frames.
563 If the number of frames aren't explicitly given, the debug option
564 `depth' determines the maximum number of frames printed."
565 (lambda (state n-frames)
566 (let ((stack (state-stack state)))
567 ;; Kludge around lack of call-with-values.
570 ;;(do ((index start (+ index 1)))
572 ;;(write-state-short* stack index))
574 ;; Use builtin backtrace instead:
575 (display-backtrace stack
576 (current-output-port)
577 (if (memq 'backwards (debug-options))
582 (let ((end (stack-length stack)))
583 (cond ((not n-frames) ;(>= (abs n-frames) end))
584 (values 0 (min end (cadr (memq 'depth (debug-options))))))
588 (values (+ end n-frames) end))))))
591 (define-command "quit" '()
594 (throw 'exit-debugger)))
596 (define-command '("info" "frame") '()
597 "All about selected stack frame."
599 (write-state-long state)
602 (define-command '("info" "args") '()
603 "Argument variables of current stack frame."
605 (let ((index (state-index state)))
606 (let ((frame (stack-ref (state-stack state) index)))
607 (write-frame-index-long frame)
608 (write-frame-args-long frame)))
611 (define-command-alias "f" "frame")
612 (define-command-alias '("info" "f") '("info" "frame"))
613 (define-command-alias "bt" "backtrace")
614 (define-command-alias "where" "backtrace")
615 (define-command-alias '("info" "stack") "backtrace")
619 (define (select-frame-absolute state number)
620 (new-state-index state
622 (let ((end (stack-length (state-stack state))))
626 (state-stack state))))
628 (define (select-frame-relative state delta)
629 (new-state-index state
630 (let ((index (+ (state-index state) delta))
631 (end (stack-length (state-stack state))))
632 (cond ((< index 0) 0)
633 ((>= index end) (- end 1))
636 (define (write-state-short state)
638 (write-state-short* (state-stack state) (state-index state)))
640 (define (write-state-short* stack index)
641 (write-frame-index-short stack index)
643 (write-frame-short (stack-ref stack index))
646 (define (write-frame-index-short stack index)
647 (let ((s (number->string (frame-number (stack-ref stack index)))))
650 (write-chars #\space (- 4 (string-length s)))))
652 (define (write-frame-short frame)
653 (if (frame-procedure? frame)
654 (write-frame-short/application frame)
655 (write-frame-short/expression frame)))
657 (define (write-frame-short/application frame)
659 (write (let ((procedure (frame-procedure frame)))
660 (or (and (procedure? procedure)
661 (procedure-name procedure))
663 (if (frame-evaluating-args? frame)
666 (for-each (lambda (argument)
669 (frame-arguments frame))
672 ;;; Use builtin function instead:
673 (set! write-frame-short/application
675 (display-application frame (current-output-port) 12)))
677 (define (write-frame-short/expression frame)
678 (write (let* ((source (frame-source frame))
679 (copy (source-property source 'copy)))
682 (unmemoize source)))))
684 (define (write-state-long state)
685 (let ((index (state-index state)))
686 (let ((frame (stack-ref (state-stack state) index)))
687 (write-frame-index-long frame)
688 (write-frame-long frame))))
690 (define (write-frame-index-long frame)
691 (display "Stack frame: ")
692 (write (frame-number frame))
693 (if (frame-real? frame)
697 (define (write-frame-long frame)
698 (if (frame-procedure? frame)
699 (write-frame-long/application frame)
700 (write-frame-long/expression frame)))
702 (define (write-frame-long/application frame)
703 (display "This frame is an application.")
705 (if (frame-source frame)
707 (display "The corresponding expression is:")
709 (display-source frame)
711 (display "The procedure being applied is: ")
712 (write (let ((procedure (frame-procedure frame)))
713 (or (and (procedure? procedure)
714 (procedure-name procedure))
717 (display "The procedure's arguments are")
718 (if (frame-evaluating-args? frame)
719 (display " being evaluated.")
722 (write (frame-arguments frame))))
725 (define (display-source frame)
727 (write (let* ((source (frame-source frame))
728 (copy (source-property source 'copy)))
731 (unmemoize source)))))
733 (define (write-frame-long/expression frame)
734 (display "This frame is an evaluation.")
736 (display "The expression being evaluated is:")
738 (display-source frame)
741 (define (write-frame-args-long frame)
742 (if (frame-procedure? frame)
743 (let ((arguments (frame-arguments frame)))
744 (let ((n (length arguments)))
745 (display "This frame has ")
747 (display " argument")
750 (write-char (if (null? arguments) #\. #\:))
752 (for-each (lambda (argument)
758 (display "This frame is an evaluation frame; it has no arguments.")
761 (define (write-chars char n)