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

Last change on this file since 4819 was 4819, checked in by piso, 18 years ago

Work in progress.

File size: 2.9 KB
Line 
1;;; trace.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: trace.lisp,v 1.3 2003-11-18 01:15:12 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(defparameter *traced-functions* nil)
25
26(defun list-traced-functions ()
27  *traced-functions*)
28
29(defmacro trace (&rest args)
30  (if args
31      (expand-trace args)
32      '(list-traced-functions)))
33
34(defun expand-trace (args)
35  (let ((results ()))
36    (dolist (arg args)
37      (if (trace-1 arg)
38          (push arg results)))
39    `',results))
40
41(defparameter *trace-depth* 0)
42
43(defun trace-1 (symbol)
44  (unless (fboundp symbol)
45    (error "~S is not the name of a function" symbol))
46  (when (member symbol *traced-functions*)
47    (format t "~S is already being traced." symbol))
48  (let* ((untraced-function (symbol-function symbol))
49         (trace-function
50          (lambda (&rest args)
51            (format t (indent "~D: ~S~%") *trace-depth*
52                    (append (list symbol) args))
53            (incf *trace-depth*)
54            (let ((r (multiple-value-list (apply untraced-function args))))
55              (decf *trace-depth*)
56              (format t (indent "~D: ~A returned") *trace-depth* symbol)
57              (dolist (val r)
58                (format t " ~S" val))
59              (format t "~%")
60              (values-list r)))))
61    (setf (symbol-function symbol) trace-function)
62    (setf (get symbol *untraced-function*) untraced-function)
63    (push symbol *traced-functions*)
64    symbol))
65
66(defun indent (string)
67  (concatenate 'string
68               (make-string (* (1+ *trace-depth*) 2) :initial-element #\space)
69               string))
70
71(defmacro untrace (&rest args)
72  (if (null args)
73      (untrace-all)
74      (dolist (arg args)
75        (if (member arg *traced-functions*)
76            (untrace-1 arg)
77            (format t "~S is not being traced.~%" arg)))))
78
79(defun untrace-all ()
80  (dolist (arg *traced-functions*)
81    (untrace-1 arg)))
82
83(defun untrace-1 (symbol)
84  (let ((untraced-function (get symbol *untraced-function*)))
85    (setf (symbol-function symbol) untraced-function)
86    (remprop symbol *untraced-function*)
87    (setf *traced-functions* (remove symbol *traced-functions*))))
Note: See TracBrowser for help on using the repository browser.