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

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

Initial checkin.

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