source: trunk/j/src/org/armedbear/lisp/trace.lisp @ 8506

Last change on this file since 8506 was 8506, checked in by piso, 17 years ago

Work in progress.

File size: 3.6 KB
Line 
1;;; trace.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: trace.lisp,v 1.10 2005-02-08 16:43:30 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package #:system)
21
22(defconstant *untraced-function* (make-symbol "untraced-function"))
23
24(defvar *traced-functions* nil)
25
26(defvar *trace-depth* 0)
27
28(defun list-traced-functions ()
29  *traced-functions*)
30
31(defmacro trace (&rest args)
32  (if args
33      `(progn
34        (setf *trace-depth* 0)
35        (expand-trace ',args))
36      `(list-traced-functions)))
37
38(defun expand-trace (args)
39  (let ((results ())
40        (breakp nil))
41    (let ((index (position :break args)))
42      (when index
43        (setf breakp (nth (1+ index) args))
44        (setf args (append (subseq args 0 index) (subseq args (+ index 2))))))
45    (dolist (arg args)
46      (if (trace-1 arg breakp)
47          (push arg results)))
48    results))
49
50(defun trace-1 (name breakp)
51  (unless (fboundp name)
52    (error "~S is not the name of a function" name))
53  (if (member name *traced-functions*)
54      (%format t "~S is already being traced." name)
55      (let* ((untraced-function (symbol-function name))
56             (trace-function
57              (lambda (&rest args)
58                (with-standard-io-syntax
59                    (%format *trace-output* (indent "~D: ~S~%") *trace-depth*
60                             (cons name args)))
61                (when breakp
62                  (break))
63                (incf *trace-depth*)
64                (let ((r (multiple-value-list (apply untraced-function args))))
65                  (decf *trace-depth*)
66                  (with-standard-io-syntax
67                    (%format *trace-output* (indent "~D: ~A returned") *trace-depth* name)
68                      (dolist (val r)
69                        (%format *trace-output* " ~S" val))
70                    (terpri *trace-output*))
71                  (values-list r)))))
72        (let ((*warn-on-redefinition* nil))
73          (setf (symbol-function name) trace-function))
74        (setf (get name *untraced-function*) untraced-function)
75        (push name *traced-functions*)
76        name)))
77
78(defun indent (string)
79  (concatenate 'string
80               (make-string (* (1+ *trace-depth*) 2) :initial-element #\space)
81               string))
82
83(defmacro untrace (&rest args)
84  (cond ((null args)
85         `(progn
86            (untrace-all)
87            (setf *trace-depth* 0)
88            t))
89        (t
90         `(progn
91            (untrace-n ',args)
92            (setf *trace-depth* 0)
93            t))))
94
95(defun untrace-all ()
96  (dolist (arg *traced-functions*)
97    (untrace-1 arg)))
98
99(defun untrace-n (args)
100  (dolist (arg args)
101    (if (member arg *traced-functions*)
102        (untrace-1 arg)
103        (%format t "~S is not being traced.~%" arg))))
104
105(defun untrace-1 (symbol)
106  (let ((untraced-function (get symbol *untraced-function*))
107        (*warn-on-redefinition* nil))
108    (setf (symbol-function symbol) untraced-function)
109    (remprop symbol *untraced-function*)
110    (setf *traced-functions* (remove symbol *traced-functions*))))
Note: See TracBrowser for help on using the repository browser.