1 ;;; trace.el --- tracing facility for Emacs Lisp functions
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
6 ;; Created: 15 Dec 1992
7 ;; Keywords: tools, lisp
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;;; Synched up with: FSF 19.34.
29 ;; trace|Hans Chalupsky|hans@cs.buffalo.edu|
30 ;; Tracing facility for Emacs Lisp functions|
31 ;; 1993/05/18 00:41:16|2.0|~/packages/trace.el.Z|
38 ;; A simple trace package that utilizes advice.el. It generates trace
39 ;; information in a Lisp-style fashion and inserts it into a trace output
40 ;; buffer. Tracing can be done in the background (or silently) so that
41 ;; generation of trace output won't interfere with what you are currently
44 ;; How to get the latest trace.el:
45 ;; ===============================
46 ;; You can get the latest version of this file either via anonymous ftp from
47 ;; ftp.cs.buffalo.edu (128.205.32.9) with pathname /pub/Emacs/trace.el,
48 ;; or send email to hans@cs.buffalo.edu and I'll mail it to you.
52 ;; trace.el needs advice.el version 2.0 or later which you can get from the
53 ;; same place from where you got trace.el.
57 ;; - Traced subrs when called interactively will always show nil as the
58 ;; value of their arguments.
59 ;; - Only functions/macros/subrs that are called via their function cell will
60 ;; generate trace output, hence, you won't get trace output for:
61 ;; + Subrs called directly from other subrs/C-code
62 ;; + Compiled calls to subrs that have special byte-codes associated
63 ;; with them (e.g., car, cdr, ...)
64 ;; + Macros that were expanded during compilation
65 ;; - All the restrictions that apply to advice.el
69 ;; Put this file together with advice.el (version 2.0 or later) somewhere
70 ;; into your Emacs `load-path', byte-compile it/them for efficiency, and
71 ;; put the following autoload declarations into your .emacs
73 ;; (autoload 'trace-function "trace" "Trace a function" t)
74 ;; (autoload 'trace-function-background "trace" "Trace a function" t)
76 ;; or explicitly load it with (require 'trace) or (load "trace").
78 ;; Comments, suggestions, bug reports
79 ;; ==================================
80 ;; are strongly appreciated, please email them to hans@cs.buffalo.edu.
84 ;; - To trace a function say `M-x trace-function' which will ask you for the
85 ;; name of the function/subr/macro to trace, as well as for the buffer
86 ;; into which trace output should go.
87 ;; - If you want to trace a function that switches buffers or does other
88 ;; display oriented stuff use `M-x trace-function-background' which will
89 ;; generate the trace output silently in the background without popping
90 ;; up windows and doing other irritating stuff.
91 ;; - To untrace a function say `M-x untrace-function'.
92 ;; - To untrace all currently traced functions say `M-x untrace-all'.
99 ;; (* n (fact (1- n)))))
102 ;; (trace-function 'fact)
105 ;; Now, evaluating this...
110 ;; ...will generate the following in *trace-buffer*:
114 ;; | | 3 -> fact: n=2
115 ;; | | | 4 -> fact: n=1
116 ;; | | | | 5 -> fact: n=0
117 ;; | | | | 5 <- fact: 1
118 ;; | | | 4 <- fact: 1
124 ;; (defun ack (x y z)
127 ;; (if (and (<= x 2) (= z 0))
129 ;; (if (and (> x 2) (= z 0))
131 ;; (ack (1- x) y (ack x y (1- z)))))))
134 ;; (trace-function 'ack)
137 ;; Try this for some interesting trace output:
143 ;; The following does something similar to the functionality of the package
144 ;; log-message.el by Robert Potter, which is giving you a chance to look at
145 ;; messages that might have whizzed by too quickly (you won't see subr
146 ;; generated messages though):
148 ;; (trace-function-background 'message "*Message Log*")
153 ;; Revision 2.0 1993/05/18 00:41:16 hans
154 ;; * Adapted for advice.el 2.0; it now also works
155 ;; for GNU Emacs-19 and XEmacs
156 ;; * Separate function `trace-function-background'
157 ;; * Separate pieces of advice for foreground and background tracing
158 ;; * Less insane handling of interactive trace buffer specification
159 ;; * String arguments and values are now printed properly
161 ;; Revision 1.1 1992/12/15 22:45:15 hans
162 ;; * Created, first public release
169 ;; XEmacs change (leave the version # in place)
170 (defconst trace-version "2.0")
173 (defvar trace-buffer "*trace-output*"
174 "*Trace output will by default go to that buffer.")
176 ;; Current level of traced function invocation:
177 (defvar trace-level 0)
179 ;; Semi-cryptic name used for a piece of trace advice:
180 (defvar trace-advice-name 'trace-function\ )
182 ;; Used to separate new trace output from previous traced runs:
183 (defvar trace-separator (format "%s\n" (make-string 70 ?=)))
185 (defun trace-entry-message (function level argument-bindings)
186 ;; Generates a string that describes that FUNCTION has been entered at
187 ;; trace LEVEL with ARGUMENT-BINDINGS.
188 (format "%s%s%d -> %s: %s\n"
189 (mapconcat 'char-to-string (make-string (1- level) ?|) " ")
190 (if (> level 1) " " "")
196 (symbol-name (ad-arg-binding-field binding 'name))
198 ;; do this so we'll see strings:
200 (ad-arg-binding-field binding 'value)))))
204 (defun trace-exit-message (function level value)
205 ;; Generates a string that describes that FUNCTION has been exited at
206 ;; trace LEVEL and that it returned VALUE.
207 (format "%s%s%d <- %s: %s\n"
208 (mapconcat 'char-to-string (make-string (1- level) ?|) " ")
209 (if (> level 1) " " "")
212 ;; do this so we'll see strings:
213 (prin1-to-string value)))
215 (defun trace-make-advice (function buffer background)
216 ;; Builds the piece of advice to be added to FUNCTION's advice info
217 ;; so that it will generate the proper trace output in BUFFER
218 ;; (quietly if BACKGROUND is t).
220 trace-advice-name nil t
224 (let ((trace-level (1+ trace-level))
225 (trace-buffer (get-buffer-create (, buffer))))
227 (set-buffer trace-buffer)
228 (goto-char (point-max))
229 ;; Insert a separator from previous trace output:
230 (if (= trace-level 1) (insert trace-separator))
233 '(, function) trace-level ad-arg-bindings)))
236 (set-buffer trace-buffer)
237 (goto-char (point-max))
240 '(, function) trace-level ad-return-value)))))))
243 (let ((trace-level (1+ trace-level))
244 (trace-buffer (get-buffer-create (, buffer))))
245 (pop-to-buffer trace-buffer)
246 (goto-char (point-max))
247 ;; Insert a separator from previous trace output:
248 (if (= trace-level 1) (insert trace-separator))
251 '(, function) trace-level ad-arg-bindings))
253 (pop-to-buffer trace-buffer)
254 (goto-char (point-max))
257 '(, function) trace-level ad-return-value)))))))))
259 (defun trace-function-internal (function buffer background)
260 ;; Adds trace advice for FUNCTION and activates it.
263 (trace-make-advice function (or buffer trace-buffer) background)
265 (ad-activate function nil))
267 (defun trace-is-traced (function)
268 (ad-find-advice function 'around trace-advice-name))
270 ;; shamelessly stolen from find-func
271 (defun trace-function-read (prompt)
272 "Read and return an interned function symbol.
273 Default to the one near point."
274 (let* ((symb (function-at-point))
275 (enable-recursive-minibuffers t)
276 (val (completing-read
278 (if symb (format " (default %s)" symb) ""))
279 obarray 'fboundp t nil 'function-history)))
280 (if (equal val "") symb (intern val))))
284 (defun trace-function (function &optional buffer)
285 "Traces FUNCTION with trace output going to BUFFER.
286 For every call of FUNCTION Lisp-style trace messages that display argument
287 and return values will be inserted into BUFFER. This function generates the
288 trace advice for FUNCTION and activates it together with any other advice
289 there might be!! The trace BUFFER will popup whenever FUNCTION is called.
290 Do not use this to trace functions that switch buffers or do any other
291 display oriented stuff, use `trace-function-background' instead."
294 (trace-function-read "Trace function%s: ")
295 (read-buffer "Output to buffer: " trace-buffer)))
296 (trace-function-internal function buffer nil))
299 (defun trace-function-background (function &optional buffer)
300 "Traces FUNCTION with trace output going quietly to BUFFER.
301 For every call of FUNCTION Lisp-style trace messages that display argument
302 and return values will be inserted into BUFFER. This function generates the
303 trace advice for FUNCTION and activates it together with any other advice
304 there might be!! Trace output will quietly go to BUFFER without changing
305 the window or buffer configuration at all."
308 (trace-function-read "Trace function in background%s: ")
309 (read-buffer "Output to buffer: " trace-buffer)))
310 (trace-function-internal function buffer t))
312 (defun untrace-function (function)
313 "Untraces FUNCTION and possibly activates all remaining advice.
314 Activation is performed with `ad-update', hence remaining advice will get
315 activated only if the advice of FUNCTION is currently active. If FUNCTION
316 was not traced this is a noop."
318 (list (ad-read-advised-function "Untrace function: " 'trace-is-traced)))
319 (cond ((trace-is-traced function)
320 (ad-remove-advice function 'around trace-advice-name)
321 (ad-update function))))
323 (defun untrace-all ()
324 "Untraces all currently traced functions."
326 (ad-do-advised-functions (function)
327 (untrace-function function)))
331 ;;; trace.el ends here