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