1 ;;;; Copyright (C) 1996, 1997, 1998 Free Software Foundation
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
18 ;;;; The author can be reached at djurfeldt@nada.kth.se
19 ;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
23 (define-module (ice-9 debug))
28 (define-public (frame-number->index n)
29 (if (memq 'backwards (debug-options))
31 (- (stack-length (fluid-ref the-last-stack)) n 1)))
36 ;;; This code is just an experimental prototype (e. g., it is not
37 ;;; thread safe), but since it's at the same time useful, it's
40 (define traced-procedures '())
42 (define-public (trace . args)
44 (nameify traced-procedures)
46 (for-each (lambda (proc)
47 (if (not (procedure? proc))
48 (error "trace: Wrong type argument:" proc))
49 (set-procedure-property! proc 'trace #t)
50 (if (not (memq proc traced-procedures))
51 (set! traced-procedures
52 (cons proc traced-procedures))))
54 (set! apply-frame-handler trace-entry)
55 (set! exit-frame-handler trace-exit)
60 (define-public (untrace . args)
62 (not (null? traced-procedures)))
63 (apply untrace traced-procedures)
65 (for-each (lambda (proc)
66 (set-procedure-property! proc 'trace #f)
67 (set! traced-procedures (delq! proc traced-procedures)))
69 (if (null? traced-procedures)
70 (debug-disable 'trace))
75 (let ((name (procedure-name proc)))
79 (define trace-level 0)
80 (add-hook! abort-hook (lambda () (set! trace-level 0)))
82 (define (trace-entry key cont tail)
83 (if (eq? (stack-id cont) 'repl-stack)
84 (let ((cep (current-error-port))
85 (frame (last-stack-frame cont)))
87 (set! trace-level (+ trace-level 1)))
88 (let indent ((n trace-level))
89 (cond ((> n 1) (display "| " cep) (indent (- n 1)))))
90 (display-application frame cep)))
91 ;; It's not necessary to call the continuation since
92 ;; execution will continue if the handler returns
96 (define (trace-exit key cont retval)
97 (if (eq? (stack-id cont) 'repl-stack)
98 (let ((cep (current-error-port)))
99 (set! trace-level (- trace-level 1))
100 (let indent ((n trace-level))
101 (cond ((> n 0) (display "| " cep) (indent (- n 1)))))
106 ;;; A fix to get the error handling working together with the module system.
108 (variable-set! (builtin-variable 'debug-options) debug-options)
112 (debug-enable 'debug)
113 (read-enable 'positions)