annotate lisp/utils/trace.el @ 131:869e1851236b xemacs-20-1p4

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