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