Mercurial > emacs
comparison lisp/emacs-lisp/trace.el @ 88155:d7ddb3e565de
sync with trunk
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 16 Jan 2006 00:03:54 +0000 |
parents | 9c74f4f1d1c0 |
children |
comparison
equal
deleted
inserted
replaced
88154:8ce476d3ba36 | 88155:d7ddb3e565de |
---|---|
1 ;;; trace.el --- tracing facility for Emacs Lisp functions | 1 ;;; trace.el --- tracing facility for Emacs Lisp functions |
2 | 2 |
3 ;; Copyright (C) 1993 Free Software Foundation, Inc. | 3 ;; Copyright (C) 1993, 1998, 2000, 2002, 2003, 2004, |
4 ;; 2005 Free Software Foundation, Inc. | |
4 | 5 |
5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu> | 6 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu> |
6 ;; Maintainer: FSF | 7 ;; Maintainer: FSF |
7 ;; Created: 15 Dec 1992 | 8 ;; Created: 15 Dec 1992 |
8 ;; Keywords: tools, lisp | 9 ;; Keywords: tools, lisp |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 ;; GNU General Public License for more details. | 21 ;; GNU General Public License for more details. |
21 | 22 |
22 ;; You should have received a copy of the GNU General Public License | 23 ;; You should have received a copy of the GNU General Public License |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | 24 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02111-1307, USA. | 26 ;; Boston, MA 02110-1301, USA. |
26 | 27 |
27 ;; LCD Archive Entry: | 28 ;; LCD Archive Entry: |
28 ;; trace|Hans Chalupsky|hans@cs.buffalo.edu| | 29 ;; trace|Hans Chalupsky|hans@cs.buffalo.edu| |
29 ;; Tracing facility for Emacs Lisp functions| | 30 ;; Tracing facility for Emacs Lisp functions| |
30 ;; 1993/05/18 00:41:16|2.0|~/packages/trace.el.Z| | 31 ;; 1993/05/18 00:41:16|2.0|~/packages/trace.el.Z| |
154 ;;; Code: | 155 ;;; Code: |
155 | 156 |
156 (require 'advice) | 157 (require 'advice) |
157 | 158 |
158 (defgroup trace nil | 159 (defgroup trace nil |
159 "Tracing facility for Emacs Lisp functions" | 160 "Tracing facility for Emacs Lisp functions." |
160 :prefix "trace-" | 161 :prefix "trace-" |
161 :group 'lisp) | 162 :group 'lisp) |
162 | 163 |
163 ;;;###autoload | 164 ;;;###autoload |
164 (defcustom trace-buffer "*trace-output*" | 165 (defcustom trace-buffer "*trace-output*" |
172 ;; Semi-cryptic name used for a piece of trace advice: | 173 ;; Semi-cryptic name used for a piece of trace advice: |
173 (defvar trace-advice-name 'trace-function\ ) | 174 (defvar trace-advice-name 'trace-function\ ) |
174 | 175 |
175 ;; Used to separate new trace output from previous traced runs: | 176 ;; Used to separate new trace output from previous traced runs: |
176 (defvar trace-separator (format "%s\n" (make-string 70 ?=))) | 177 (defvar trace-separator (format "%s\n" (make-string 70 ?=))) |
178 | |
179 (defvar inhibit-trace nil | |
180 "If non-nil, all tracing is temporarily inhibited.") | |
177 | 181 |
178 (defun trace-entry-message (function level argument-bindings) | 182 (defun trace-entry-message (function level argument-bindings) |
179 ;; Generates a string that describes that FUNCTION has been entered at | 183 ;; Generates a string that describes that FUNCTION has been entered at |
180 ;; trace LEVEL with ARGUMENT-BINDINGS. | 184 ;; trace LEVEL with ARGUMENT-BINDINGS. |
181 (format "%s%s%d -> %s: %s\n" | 185 (format "%s%s%d -> %s: %s\n" |
182 (mapconcat 'char-to-string (make-string (1- level) ?|) " ") | 186 (mapconcat 'char-to-string (make-string (1- level) ?|) " ") |
183 (if (> level 1) " " "") | 187 (if (> level 1) " " "") |
184 level | 188 level |
185 function | 189 function |
186 (mapconcat (function | 190 (mapconcat (lambda (binding) |
187 (lambda (binding) | 191 (concat |
188 (concat | 192 (symbol-name (ad-arg-binding-field binding 'name)) |
189 (symbol-name (ad-arg-binding-field binding 'name)) | 193 "=" |
190 "=" | 194 ;; do this so we'll see strings: |
191 ;; do this so we'll see strings: | 195 (prin1-to-string |
192 (prin1-to-string | 196 (ad-arg-binding-field binding 'value)))) |
193 (ad-arg-binding-field binding 'value))))) | |
194 argument-bindings | 197 argument-bindings |
195 " "))) | 198 " "))) |
196 | 199 |
197 (defun trace-exit-message (function level value) | 200 (defun trace-exit-message (function level value) |
198 ;; Generates a string that describes that FUNCTION has been exited at | 201 ;; Generates a string that describes that FUNCTION has been exited at |
209 ;; Builds the piece of advice to be added to FUNCTION's advice info | 212 ;; Builds the piece of advice to be added to FUNCTION's advice info |
210 ;; so that it will generate the proper trace output in BUFFER | 213 ;; so that it will generate the proper trace output in BUFFER |
211 ;; (quietly if BACKGROUND is t). | 214 ;; (quietly if BACKGROUND is t). |
212 (ad-make-advice | 215 (ad-make-advice |
213 trace-advice-name nil t | 216 trace-advice-name nil t |
214 (cond (background | 217 `(advice |
215 `(advice | 218 lambda () |
216 lambda () | 219 (let ((trace-level (1+ trace-level)) |
217 (let ((trace-level (1+ trace-level)) | 220 (trace-buffer (get-buffer-create ,buffer))) |
218 (trace-buffer (get-buffer-create ,buffer))) | 221 (unless inhibit-trace |
219 (save-excursion | 222 (with-current-buffer trace-buffer |
220 (set-buffer trace-buffer) | 223 ,(unless background '(pop-to-buffer trace-buffer)) |
221 (goto-char (point-max)) | 224 (goto-char (point-max)) |
222 ;; Insert a separator from previous trace output: | 225 ;; Insert a separator from previous trace output: |
223 (if (= trace-level 1) (insert trace-separator)) | 226 (if (= trace-level 1) (insert trace-separator)) |
224 (insert | 227 (insert |
225 (trace-entry-message | 228 (trace-entry-message |
226 ',function trace-level ad-arg-bindings))) | 229 ',function trace-level ad-arg-bindings)))) |
227 ad-do-it | 230 ad-do-it |
228 (save-excursion | 231 (unless inhibit-trace |
229 (set-buffer trace-buffer) | 232 (with-current-buffer trace-buffer |
230 (goto-char (point-max)) | 233 ,(unless background '(pop-to-buffer trace-buffer)) |
231 (insert | 234 (goto-char (point-max)) |
232 (trace-exit-message | 235 (insert |
233 ',function trace-level ad-return-value)))))) | 236 (trace-exit-message |
234 (t `(advice | 237 ',function trace-level ad-return-value)))))))) |
235 lambda () | |
236 (let ((trace-level (1+ trace-level)) | |
237 (trace-buffer (get-buffer-create ,buffer))) | |
238 (pop-to-buffer trace-buffer) | |
239 (goto-char (point-max)) | |
240 ;; Insert a separator from previous trace output: | |
241 (if (= trace-level 1) (insert trace-separator)) | |
242 (insert | |
243 (trace-entry-message | |
244 ',function trace-level ad-arg-bindings)) | |
245 ad-do-it | |
246 (pop-to-buffer trace-buffer) | |
247 (goto-char (point-max)) | |
248 (insert | |
249 (trace-exit-message | |
250 ',function trace-level ad-return-value)))))))) | |
251 | 238 |
252 (defun trace-function-internal (function buffer background) | 239 (defun trace-function-internal (function buffer background) |
253 ;; Adds trace advice for FUNCTION and activates it. | 240 ;; Adds trace advice for FUNCTION and activates it. |
254 (ad-add-advice | 241 (ad-add-advice |
255 function | 242 function |
295 Activation is performed with `ad-update', hence remaining advice will get | 282 Activation is performed with `ad-update', hence remaining advice will get |
296 activated only if the advice of FUNCTION is currently active. If FUNCTION | 283 activated only if the advice of FUNCTION is currently active. If FUNCTION |
297 was not traced this is a noop." | 284 was not traced this is a noop." |
298 (interactive | 285 (interactive |
299 (list (ad-read-advised-function "Untrace function: " 'trace-is-traced))) | 286 (list (ad-read-advised-function "Untrace function: " 'trace-is-traced))) |
300 (cond ((trace-is-traced function) | 287 (when (trace-is-traced function) |
301 (ad-remove-advice function 'around trace-advice-name) | 288 (ad-remove-advice function 'around trace-advice-name) |
302 (ad-update function)))) | 289 (ad-update function))) |
303 | 290 |
304 (defun untrace-all () | 291 (defun untrace-all () |
305 "Untraces all currently traced functions." | 292 "Untraces all currently traced functions." |
306 (interactive) | 293 (interactive) |
307 (ad-do-advised-functions (function) | 294 (ad-do-advised-functions (function) |
308 (untrace-function function))) | 295 (untrace-function function))) |
309 | 296 |
310 (provide 'trace) | 297 (provide 'trace) |
311 | 298 |
299 ;; arch-tag: cfd170a7-4932-4331-8c8b-b7151942e5a1 | |
312 ;;; trace.el ends here | 300 ;;; trace.el ends here |