Commit | Line | Data |
---|---|---|
31c0dbab RS |
1 | ;;; trace.el --- tracing facility for Emacs Lisp functions |
2 | ||
3 | ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Hans Chalupsky <hans@cs.buffalo.edu> | |
6 | ;; Created: 15 Dec 1992 | |
b7f66977 | 7 | ;; Keywords: tools, lisp |
31c0dbab RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | ||
25 | ;; LCD Archive Entry: | |
26 | ;; trace|Hans Chalupsky|hans@cs.buffalo.edu| | |
27 | ;; Tracing facility for Emacs Lisp functions| | |
28 | ;; 1993/05/18 00:41:16|2.0|~/packages/trace.el.Z| | |
29 | ||
30 | ||
31 | ;;; Commentary: | |
32 | ||
33 | ;; Introduction: | |
34 | ;; ============= | |
35 | ;; A simple trace package that utilizes advice.el. It generates trace | |
36 | ;; information in a Lisp-style fashion and inserts it into a trace output | |
37 | ;; buffer. Tracing can be done in the background (or silently) so that | |
38 | ;; generation of trace output won't interfere with what you are currently | |
39 | ;; doing. | |
40 | ||
41 | ;; How to get the latest trace.el: | |
42 | ;; =============================== | |
43 | ;; You can get the latest version of this file either via anonymous ftp from | |
44 | ;; ftp.cs.buffalo.edu (128.205.32.9) with pathname /pub/Emacs/trace.el, | |
45 | ;; or send email to hans@cs.buffalo.edu and I'll mail it to you. | |
46 | ||
47 | ;; Requirement: | |
48 | ;; ============ | |
49 | ;; trace.el needs advice.el version 2.0 or later which you can get from the | |
50 | ;; same place from where you got trace.el. | |
51 | ||
52 | ;; Restrictions: | |
53 | ;; ============= | |
54 | ;; - Traced subrs when called interactively will always show nil as the | |
55 | ;; value of their arguments. | |
56 | ;; - Only functions/macros/subrs that are called via their function cell will | |
57 | ;; generate trace output, hence, you won't get trace output for: | |
58 | ;; + Subrs called directly from other subrs/C-code | |
59 | ;; + Compiled calls to subrs that have special byte-codes associated | |
60 | ;; with them (e.g., car, cdr, ...) | |
61 | ;; + Macros that were expanded during compilation | |
62 | ;; - All the restrictions that apply to advice.el | |
63 | ||
64 | ;; Installation: | |
65 | ;; ============= | |
66 | ;; Put this file together with advice.el (version 2.0 or later) somewhere | |
67 | ;; into your Emacs `load-path', byte-compile it/them for efficiency, and | |
68 | ;; put the following autoload declarations into your .emacs | |
69 | ;; | |
70 | ;; (autoload 'trace-function "trace" "Trace a function" t) | |
71 | ;; (autoload 'trace-function-background "trace" "Trace a function" t) | |
72 | ;; | |
73 | ;; or explicitly load it with (require 'trace) or (load "trace"). | |
74 | ||
75 | ;; Comments, suggestions, bug reports | |
76 | ;; ================================== | |
77 | ;; are strongly appreciated, please email them to hans@cs.buffalo.edu. | |
78 | ||
79 | ;; Usage: | |
80 | ;; ====== | |
81 | ;; - To trace a function say `M-x trace-function' which will ask you for the | |
82 | ;; name of the function/subr/macro to trace, as well as for the buffer | |
83 | ;; into which trace output should go. | |
84 | ;; - If you want to trace a function that switches buffers or does other | |
85 | ;; display oriented stuff use `M-x trace-function-background' which will | |
86 | ;; generate the trace output silently in the background without popping | |
87 | ;; up windows and doing other irritating stuff. | |
88 | ;; - To untrace a function say `M-x untrace-function'. | |
89 | ;; - To untrace all currently traced functions say `M-x untrace-all'. | |
90 | ||
91 | ;; Examples: | |
92 | ;; ========= | |
93 | ;; | |
94 | ;; (defun fact (n) | |
95 | ;; (if (= n 0) 1 | |
96 | ;; (* n (fact (1- n))))) | |
97 | ;; fact | |
98 | ;; | |
99 | ;; (trace-function 'fact) | |
100 | ;; fact | |
101 | ;; | |
102 | ;; Now, evaluating this... | |
103 | ;; | |
104 | ;; (fact 4) | |
105 | ;; 24 | |
106 | ;; | |
107 | ;; ...will generate the following in *trace-buffer*: | |
108 | ;; | |
109 | ;; 1 -> fact: n=4 | |
110 | ;; | 2 -> fact: n=3 | |
111 | ;; | | 3 -> fact: n=2 | |
112 | ;; | | | 4 -> fact: n=1 | |
113 | ;; | | | | 5 -> fact: n=0 | |
114 | ;; | | | | 5 <- fact: 1 | |
115 | ;; | | | 4 <- fact: 1 | |
116 | ;; | | 3 <- fact: 2 | |
117 | ;; | 2 <- fact: 6 | |
118 | ;; 1 <- fact: 24 | |
119 | ;; | |
120 | ;; | |
121 | ;; (defun ack (x y z) | |
122 | ;; (if (= x 0) | |
123 | ;; (+ y z) | |
124 | ;; (if (and (<= x 2) (= z 0)) | |
125 | ;; (1- x) | |
126 | ;; (if (and (> x 2) (= z 0)) | |
127 | ;; y | |
128 | ;; (ack (1- x) y (ack x y (1- z))))))) | |
129 | ;; ack | |
130 | ;; | |
131 | ;; (trace-function 'ack) | |
132 | ;; ack | |
133 | ;; | |
134 | ;; Try this for some interesting trace output: | |
135 | ;; | |
136 | ;; (ack 3 3 1) | |
137 | ;; 27 | |
138 | ;; | |
139 | ;; | |
140 | ;; The following does something similar to the functionality of the package | |
141 | ;; log-message.el by Robert Potter, which is giving you a chance to look at | |
142 | ;; messages that might have whizzed by too quickly (you won't see subr | |
143 | ;; generated messages though): | |
144 | ;; | |
145 | ;; (trace-function-background 'message "*Message Log*") | |
146 | ||
147 | ||
148 | ;;; Change Log: | |
149 | ||
150 | ;; Revision 2.0 1993/05/18 00:41:16 hans | |
151 | ;; * Adapted for advice.el 2.0; it now also works | |
152 | ;; for GNU Emacs-19 and Lemacs | |
153 | ;; * Separate function `trace-function-background' | |
154 | ;; * Separate pieces of advice for foreground and background tracing | |
155 | ;; * Less insane handling of interactive trace buffer specification | |
156 | ;; * String arguments and values are now printed properly | |
157 | ;; | |
158 | ;; Revision 1.1 1992/12/15 22:45:15 hans | |
159 | ;; * Created, first public release | |
160 | ||
161 | ||
162 | ;;; Code: | |
163 | ||
164 | (require 'advice) | |
165 | ||
31c0dbab RS |
166 | ;;;###autoload |
167 | (defvar trace-buffer "*trace-output*" | |
168 | "*Trace output will by default go to that buffer.") | |
169 | ||
170 | ;; Current level of traced function invocation: | |
171 | (defvar trace-level 0) | |
172 | ||
173 | ;; Semi-cryptic name used for a piece of trace advice: | |
174 | (defvar trace-advice-name 'trace-function\ ) | |
175 | ||
176 | ;; Used to separate new trace output from previous traced runs: | |
177 | (defvar trace-separator (format "%s\n" (make-string 70 ?=))) | |
178 | ||
179 | (defun trace-entry-message (function level argument-bindings) | |
180 | ;; Generates a string that describes that FUNCTION has been entered at | |
181 | ;; trace LEVEL with ARGUMENT-BINDINGS. | |
182 | (format "%s%s%d -> %s: %s\n" | |
183 | (mapconcat 'char-to-string (make-string (1- level) ?|) " ") | |
184 | (if (> level 1) " " "") | |
185 | level | |
186 | function | |
187 | (mapconcat (function | |
188 | (lambda (binding) | |
189 | (concat | |
190 | (symbol-name (ad-arg-binding-field binding 'name)) | |
191 | "=" | |
192 | ;; do this so we'll see strings: | |
193 | (prin1-to-string | |
194 | (ad-arg-binding-field binding 'value))))) | |
195 | argument-bindings | |
196 | " "))) | |
197 | ||
198 | (defun trace-exit-message (function level value) | |
199 | ;; Generates a string that describes that FUNCTION has been exited at | |
200 | ;; trace LEVEL and that it returned VALUE. | |
201 | (format "%s%s%d <- %s: %s\n" | |
202 | (mapconcat 'char-to-string (make-string (1- level) ?|) " ") | |
203 | (if (> level 1) " " "") | |
204 | level | |
205 | function | |
206 | ;; do this so we'll see strings: | |
207 | (prin1-to-string value))) | |
208 | ||
209 | (defun trace-make-advice (function buffer background) | |
210 | ;; Builds the piece of advice to be added to FUNCTION's advice info | |
211 | ;; so that it will generate the proper trace output in BUFFER | |
212 | ;; (quietly if BACKGROUND is t). | |
213 | (ad-make-advice | |
214 | trace-advice-name nil t | |
215 | (cond (background | |
216 | (` (advice | |
217 | lambda () | |
218 | (let ((trace-level (1+ trace-level)) | |
219 | (trace-buffer (get-buffer-create (, buffer)))) | |
220 | (save-excursion | |
221 | (set-buffer trace-buffer) | |
222 | (goto-char (point-max)) | |
223 | ;; Insert a separator from previous trace output: | |
224 | (if (= trace-level 1) (insert trace-separator)) | |
225 | (insert | |
226 | (trace-entry-message | |
227 | '(, function) trace-level ad-arg-bindings))) | |
228 | ad-do-it | |
229 | (save-excursion | |
230 | (set-buffer trace-buffer) | |
231 | (goto-char (point-max)) | |
232 | (insert | |
233 | (trace-exit-message | |
234 | '(, function) trace-level ad-return-value))))))) | |
235 | (t (` (advice | |
236 | lambda () | |
237 | (let ((trace-level (1+ trace-level)) | |
238 | (trace-buffer (get-buffer-create (, buffer)))) | |
239 | (pop-to-buffer trace-buffer) | |
240 | (goto-char (point-max)) | |
241 | ;; Insert a separator from previous trace output: | |
242 | (if (= trace-level 1) (insert trace-separator)) | |
243 | (insert | |
244 | (trace-entry-message | |
245 | '(, function) trace-level ad-arg-bindings)) | |
246 | ad-do-it | |
247 | (pop-to-buffer trace-buffer) | |
248 | (goto-char (point-max)) | |
249 | (insert | |
250 | (trace-exit-message | |
251 | '(, function) trace-level ad-return-value))))))))) | |
252 | ||
253 | (defun trace-function-internal (function buffer background) | |
254 | ;; Adds trace advice for FUNCTION and activates it. | |
255 | (ad-add-advice | |
256 | function | |
257 | (trace-make-advice function (or buffer trace-buffer) background) | |
258 | 'around 'last) | |
259 | (ad-activate function nil)) | |
260 | ||
261 | (defun trace-is-traced (function) | |
262 | (ad-find-advice function 'around trace-advice-name)) | |
263 | ||
264 | ;;;###autoload | |
265 | (defun trace-function (function &optional buffer) | |
266 | "Traces FUNCTION with trace output going to BUFFER. | |
267 | For every call of FUNCTION Lisp-style trace messages that display argument | |
268 | and return values will be inserted into BUFFER. This function generates the | |
269 | trace advice for FUNCTION and activates it together with any other advice | |
270 | there might be!! The trace BUFFER will popup whenever FUNCTION is called. | |
271 | Do not use this to trace functions that switch buffers or do any other | |
272 | display oriented stuff, use `trace-function-background' instead." | |
273 | (interactive | |
274 | (list | |
275 | (intern (completing-read "Trace function: " obarray 'fboundp t)) | |
276 | (read-buffer "Output to buffer: " trace-buffer))) | |
277 | (trace-function-internal function buffer nil)) | |
278 | ||
279 | ;;;###autoload | |
280 | (defun trace-function-background (function &optional buffer) | |
281 | "Traces FUNCTION with trace output going quietly to BUFFER. | |
282 | For every call of FUNCTION Lisp-style trace messages that display argument | |
283 | and return values will be inserted into BUFFER. This function generates the | |
284 | trace advice for FUNCTION and activates it together with any other advice | |
285 | there might be!! Trace output will quietly go to BUFFER without changing | |
286 | the window or buffer configuration at all." | |
287 | (interactive | |
288 | (list | |
289 | (intern | |
290 | (completing-read "Trace function in background: " obarray 'fboundp t)) | |
291 | (read-buffer "Output to buffer: " trace-buffer))) | |
292 | (trace-function-internal function buffer t)) | |
293 | ||
294 | (defun untrace-function (function) | |
295 | "Untraces FUNCTION and possibly activates all remaining advice. | |
296 | Activation is performed with `ad-update', hence remaining advice will get | |
297 | activated only if the advice of FUNCTION is currently active. If FUNCTION | |
298 | was not traced this is a noop." | |
299 | (interactive | |
300 | (list (ad-read-advised-function "Untrace function: " 'trace-is-traced))) | |
301 | (cond ((trace-is-traced function) | |
302 | (ad-remove-advice function 'around trace-advice-name) | |
303 | (ad-update function)))) | |
304 | ||
305 | (defun untrace-all () | |
306 | "Untraces all currently traced functions." | |
307 | (interactive) | |
308 | (ad-do-advised-functions (function) | |
309 | (untrace-function function))) | |
310 | ||
311 | (provide 'trace) | |
312 | ||
313 | ;;; trace.el ends here |