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

Last change on this file since 9266 was 9171, checked in by piso, 16 years ago

Work in progress (tested).

File size: 4.5 KB
Line 
1;;; trace.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: trace.lisp,v 1.15 2005-05-14 19:00:58 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(export 'untraced-function) ;; For FIND-GENERIC-FUNCTION in clos.lisp.
23
24(require 'format)
25
26(defvar *trace-info-hashtable* (make-hash-table :test #'equal))
27
28(defstruct trace-info name untraced-function breakp)
29
30(defvar *trace-depth* 0)
31
32(defun list-traced-functions ()
33  (copy-list *traced-names*))
34
35(defmacro trace (&rest args)
36  (if args
37      (expand-trace args)
38      `(list-traced-functions)))
39
40(defun expand-trace (args)
41  (let ((results ())
42        (breakp nil))
43    (let ((index (position :break args)))
44      (when index
45        (setf breakp (nth (1+ index) args))
46        (setf args (append (subseq args 0 index) (subseq args (+ index 2))))))
47    (dolist (arg args)
48      (let ((info (make-trace-info :name arg
49                                   :breakp breakp)))
50        (push `(trace-1 ',arg ,info) results)))
51    `(list ,@(nreverse results))))
52
53(defun trace-1 (name info)
54  (unless (fboundp name)
55    (error "~S is not the name of a function." name))
56  (if (member name *traced-names* :test #'equal)
57      (format t "~S is already being traced." name)
58      (let* ((untraced-function (fdefinition name))
59             (traced-function
60              (traced-function name info untraced-function)))
61        (setf (trace-info-untraced-function info) untraced-function)
62        (let ((*warn-on-redefinition* nil))
63          (setf (fdefinition name) traced-function))
64        (setf (gethash name *trace-info-hashtable*) info)
65        (push name *traced-names*)))
66  name)
67
68(defun traced-function (name info untraced-function)
69  (let ((breakp (trace-info-breakp info)))
70    (lambda (&rest args)
71      (with-standard-io-syntax
72        (let ((*print-readably* nil)
73              (*print-structure* nil))
74          (format *trace-output* (indent "~D: ~S~%") *trace-depth*
75                  (cons name args))))
76      (when breakp
77        (break))
78      (incf *trace-depth*)
79      (let ((r (multiple-value-list (apply untraced-function args))))
80        (decf *trace-depth*)
81        (with-standard-io-syntax
82          (let ((*print-readably* nil)
83                (*print-structure* nil))
84            (format *trace-output* (indent "~D: ~A returned") *trace-depth* name)
85            (dolist (val r)
86              (format *trace-output* " ~S" val))
87            (terpri *trace-output*)))
88        (values-list r)))))
89
90(defun untraced-function (name)
91  (let ((info (gethash name *trace-info-hashtable*)))
92    (and info (trace-info-untraced-function info))))
93
94(defun trace-redefined-update (name untraced-function)
95  (when (and *traced-names* (find name *traced-names* :test #'equal))
96    (let* ((info (gethash name *trace-info-hashtable*))
97           (traced-function (traced-function name info untraced-function)))
98      (setf (trace-info-untraced-function info) untraced-function)
99      (let ((*traced-names* '()))
100        (setf (fdefinition name) traced-function)))))
101
102(defun indent (string)
103  (concatenate 'string
104               (make-string (* (1+ *trace-depth*) 2) :initial-element #\space)
105               string))
106
107(defmacro untrace (&rest args)
108  (cond ((null args)
109         `(untrace-all))
110        (t
111         `(progn ,@(mapcar (lambda (arg) `(untrace-1 ',arg)) args) t))))
112
113(defun untrace-all ()
114  (dolist (arg *traced-names*)
115    (untrace-1 arg))
116  t)
117
118(defun untrace-1 (name)
119  (cond ((member name *traced-names* :test #'equal)
120         (let* ((trace-info (gethash name *trace-info-hashtable*))
121                (untraced-function (trace-info-untraced-function trace-info))
122                (*warn-on-redefinition* nil))
123           (remhash name *trace-info-hashtable*)
124           (setf *traced-names* (remove name *traced-names*))
125           (setf (fdefinition name) untraced-function)))
126        (t
127         (format t "~S is not being traced.~%" name)))
128  nil)
Note: See TracBrowser for help on using the repository browser.