1993-05-18 03:19:14 +00:00
|
|
|
;;; trace.el --- tracing facility for Emacs Lisp functions
|
|
|
|
|
2006-12-07 05:06:17 +00:00
|
|
|
;; Copyright (C) 1993, 1998, 2000, 2001, 2002, 2003, 2004,
|
2010-01-13 00:35:10 -08:00
|
|
|
;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
|
2000-04-13 19:12:13 +00:00
|
|
|
;; Maintainer: FSF
|
1993-05-18 03:19:14 +00:00
|
|
|
;; Created: 15 Dec 1992
|
1993-11-23 20:12:25 +00:00
|
|
|
;; Keywords: tools, lisp
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1993-05-18 03:19:14 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2008-05-06 03:21:21 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
;; LCD Archive Entry:
|
|
|
|
;; trace|Hans Chalupsky|hans@cs.buffalo.edu|
|
|
|
|
;; Tracing facility for Emacs Lisp functions|
|
|
|
|
;; 1993/05/18 00:41:16|2.0|~/packages/trace.el.Z|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Introduction:
|
|
|
|
;; =============
|
2002-09-25 17:45:29 +00:00
|
|
|
;; A simple trace package that utilizes advice.el. It generates trace
|
1993-05-18 03:19:14 +00:00
|
|
|
;; information in a Lisp-style fashion and inserts it into a trace output
|
|
|
|
;; buffer. Tracing can be done in the background (or silently) so that
|
|
|
|
;; generation of trace output won't interfere with what you are currently
|
|
|
|
;; doing.
|
|
|
|
|
|
|
|
;; Requirement:
|
|
|
|
;; ============
|
|
|
|
;; trace.el needs advice.el version 2.0 or later which you can get from the
|
|
|
|
;; same place from where you got trace.el.
|
|
|
|
|
|
|
|
;; Restrictions:
|
|
|
|
;; =============
|
|
|
|
;; - Traced subrs when called interactively will always show nil as the
|
|
|
|
;; value of their arguments.
|
|
|
|
;; - Only functions/macros/subrs that are called via their function cell will
|
|
|
|
;; generate trace output, hence, you won't get trace output for:
|
|
|
|
;; + Subrs called directly from other subrs/C-code
|
|
|
|
;; + Compiled calls to subrs that have special byte-codes associated
|
|
|
|
;; with them (e.g., car, cdr, ...)
|
|
|
|
;; + Macros that were expanded during compilation
|
|
|
|
;; - All the restrictions that apply to advice.el
|
|
|
|
|
|
|
|
;; Installation:
|
|
|
|
;; =============
|
|
|
|
;; Put this file together with advice.el (version 2.0 or later) somewhere
|
|
|
|
;; into your Emacs `load-path', byte-compile it/them for efficiency, and
|
|
|
|
;; put the following autoload declarations into your .emacs
|
|
|
|
;;
|
|
|
|
;; (autoload 'trace-function "trace" "Trace a function" t)
|
|
|
|
;; (autoload 'trace-function-background "trace" "Trace a function" t)
|
|
|
|
;;
|
|
|
|
;; or explicitly load it with (require 'trace) or (load "trace").
|
|
|
|
|
|
|
|
;; Usage:
|
|
|
|
;; ======
|
|
|
|
;; - To trace a function say `M-x trace-function' which will ask you for the
|
|
|
|
;; name of the function/subr/macro to trace, as well as for the buffer
|
|
|
|
;; into which trace output should go.
|
|
|
|
;; - If you want to trace a function that switches buffers or does other
|
|
|
|
;; display oriented stuff use `M-x trace-function-background' which will
|
|
|
|
;; generate the trace output silently in the background without popping
|
|
|
|
;; up windows and doing other irritating stuff.
|
|
|
|
;; - To untrace a function say `M-x untrace-function'.
|
|
|
|
;; - To untrace all currently traced functions say `M-x untrace-all'.
|
|
|
|
|
|
|
|
;; Examples:
|
|
|
|
;; =========
|
|
|
|
;;
|
|
|
|
;; (defun fact (n)
|
|
|
|
;; (if (= n 0) 1
|
|
|
|
;; (* n (fact (1- n)))))
|
|
|
|
;; fact
|
2002-09-25 17:45:29 +00:00
|
|
|
;;
|
1993-05-18 03:19:14 +00:00
|
|
|
;; (trace-function 'fact)
|
|
|
|
;; fact
|
|
|
|
;;
|
|
|
|
;; Now, evaluating this...
|
|
|
|
;;
|
|
|
|
;; (fact 4)
|
|
|
|
;; 24
|
|
|
|
;;
|
|
|
|
;; ...will generate the following in *trace-buffer*:
|
|
|
|
;;
|
|
|
|
;; 1 -> fact: n=4
|
|
|
|
;; | 2 -> fact: n=3
|
|
|
|
;; | | 3 -> fact: n=2
|
|
|
|
;; | | | 4 -> fact: n=1
|
|
|
|
;; | | | | 5 -> fact: n=0
|
|
|
|
;; | | | | 5 <- fact: 1
|
|
|
|
;; | | | 4 <- fact: 1
|
|
|
|
;; | | 3 <- fact: 2
|
|
|
|
;; | 2 <- fact: 6
|
|
|
|
;; 1 <- fact: 24
|
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;; (defun ack (x y z)
|
2002-09-25 17:45:29 +00:00
|
|
|
;; (if (= x 0)
|
1993-05-18 03:19:14 +00:00
|
|
|
;; (+ y z)
|
2002-09-25 17:45:29 +00:00
|
|
|
;; (if (and (<= x 2) (= z 0))
|
1993-05-18 03:19:14 +00:00
|
|
|
;; (1- x)
|
2002-09-25 17:45:29 +00:00
|
|
|
;; (if (and (> x 2) (= z 0))
|
1993-05-18 03:19:14 +00:00
|
|
|
;; y
|
|
|
|
;; (ack (1- x) y (ack x y (1- z)))))))
|
|
|
|
;; ack
|
|
|
|
;;
|
|
|
|
;; (trace-function 'ack)
|
|
|
|
;; ack
|
|
|
|
;;
|
|
|
|
;; Try this for some interesting trace output:
|
|
|
|
;;
|
|
|
|
;; (ack 3 3 1)
|
|
|
|
;; 27
|
|
|
|
;;
|
2002-09-25 17:45:29 +00:00
|
|
|
;;
|
1993-05-18 03:19:14 +00:00
|
|
|
;; The following does something similar to the functionality of the package
|
|
|
|
;; log-message.el by Robert Potter, which is giving you a chance to look at
|
|
|
|
;; messages that might have whizzed by too quickly (you won't see subr
|
|
|
|
;; generated messages though):
|
|
|
|
;;
|
|
|
|
;; (trace-function-background 'message "*Message Log*")
|
|
|
|
|
|
|
|
|
|
|
|
;;; Change Log:
|
|
|
|
|
|
|
|
;; Revision 2.0 1993/05/18 00:41:16 hans
|
|
|
|
;; * Adapted for advice.el 2.0; it now also works
|
|
|
|
;; for GNU Emacs-19 and Lemacs
|
|
|
|
;; * Separate function `trace-function-background'
|
|
|
|
;; * Separate pieces of advice for foreground and background tracing
|
|
|
|
;; * Less insane handling of interactive trace buffer specification
|
|
|
|
;; * String arguments and values are now printed properly
|
|
|
|
;;
|
|
|
|
;; Revision 1.1 1992/12/15 22:45:15 hans
|
|
|
|
;; * Created, first public release
|
|
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'advice)
|
|
|
|
|
1998-04-05 18:26:32 +00:00
|
|
|
(defgroup trace nil
|
2005-07-04 02:47:21 +00:00
|
|
|
"Tracing facility for Emacs Lisp functions."
|
1998-04-05 18:26:32 +00:00
|
|
|
:prefix "trace-"
|
|
|
|
:group 'lisp)
|
|
|
|
|
1993-05-18 03:19:14 +00:00
|
|
|
;;;###autoload
|
* textmodes/tex-mode.el (tex-alt-dvi-print-command)
(tex-dvi-print-command, tex-bibtex-command, tex-start-commands)
(tex-start-options, slitex-run-command, latex-run-command)
(tex-run-command, tex-directory):
* textmodes/ispell.el (ispell-html-skip-alists)
(ispell-tex-skip-alists, ispell-tex-skip-alists):
* textmodes/fill.el (adaptive-fill-first-line-regexp):
(adaptive-fill-regexp):
* textmodes/dns-mode.el (auto-mode-alist):
* progmodes/python.el (interpreter-mode-alist):
* progmodes/etags.el (tags-compression-info-list):
* progmodes/etags.el (tags-file-name):
* net/browse-url.el (browse-url-galeon-program)
(browse-url-firefox-program):
* mail/sendmail.el (mail-signature-file)
(mail-citation-prefix-regexp):
* international/mule-conf.el (eight-bit):
* international/latexenc.el (latex-inputenc-coding-alist):
* international/fontset.el (x-pixel-size-width-font-regexp):
* emacs-lisp/warnings.el (warning-type-format):
* emacs-lisp/trace.el (trace-buffer):
* emacs-lisp/lisp-mode.el (lisp-interaction-mode-map)
(emacs-lisp-mode-map):
* calendar/holidays.el (holiday-solar-holidays)
(holiday-bahai-holidays, holiday-islamic-holidays)
(holiday-christian-holidays, holiday-hebrew-holidays)
(hebrew-holidays-4, hebrew-holidays-3, hebrew-holidays-2)
(hebrew-holidays-1, holiday-oriental-holidays)
(holiday-general-holidays):
* x-dnd.el (x-dnd-known-types):
* tool-bar.el (tool-bar):
* startup.el (site-run-file):
* shell.el (shell-dumb-shell-regexp):
* rfn-eshadow.el (file-name-shadow-tty-properties)
(file-name-shadow-properties):
* paths.el (remote-shell-program, news-directory):
* mouse.el ([C-down-mouse-3]):
* menu-bar.el (menu-bar-tools-menu):
* jka-cmpr-hook.el (jka-compr-load-suffixes)
(jka-compr-mode-alist-additions, jka-compr-compression-info-list)
(jka-compr-compression-info-list):
* isearch.el (search-whitespace-regexp):
* image-file.el (image-file-name-extensions):
* find-dired.el (find-ls-option):
* files.el (directory-listing-before-filename-regexp)
(directory-free-space-args, insert-directory-program)
(list-directory-brief-switches, magic-fallback-mode-alist)
(magic-fallback-mode-alist, auto-mode-interpreter-regexp)
(automount-dir-prefix):
* faces.el (face-x-resources, x-font-regexp, x-font-regexp-head)
(x-font-regexp-slant, x-font-regexp-weight, face-x-resources)
(face-font-registry-alternatives, face-font-registry-alternatives)
(face-font-family-alternatives):
* facemenu.el (facemenu-add-new-face, facemenu-background-menu)
(facemenu-foreground-menu, facemenu-face-menu):
* epa-hook.el (epa-file-name-regexp):
* dnd.el (dnd-protocol-alist):
* textmodes/rst.el (auto-mode-alist):
* button.el (default-button): Purecopy strings.
2009-11-06 05:16:23 +00:00
|
|
|
(defcustom trace-buffer (purecopy "*trace-output*")
|
2009-07-22 02:45:34 +00:00
|
|
|
"Trace output will by default go to that buffer."
|
1998-04-05 18:26:32 +00:00
|
|
|
:type 'string
|
|
|
|
:group 'trace)
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
;; Current level of traced function invocation:
|
|
|
|
(defvar trace-level 0)
|
|
|
|
|
|
|
|
;; Semi-cryptic name used for a piece of trace advice:
|
|
|
|
(defvar trace-advice-name 'trace-function\ )
|
|
|
|
|
|
|
|
;; Used to separate new trace output from previous traced runs:
|
|
|
|
(defvar trace-separator (format "%s\n" (make-string 70 ?=)))
|
|
|
|
|
2005-02-27 02:30:58 +00:00
|
|
|
(defvar inhibit-trace nil
|
|
|
|
"If non-nil, all tracing is temporarily inhibited.")
|
|
|
|
|
1993-05-18 03:19:14 +00:00
|
|
|
(defun trace-entry-message (function level argument-bindings)
|
|
|
|
;; Generates a string that describes that FUNCTION has been entered at
|
|
|
|
;; trace LEVEL with ARGUMENT-BINDINGS.
|
|
|
|
(format "%s%s%d -> %s: %s\n"
|
|
|
|
(mapconcat 'char-to-string (make-string (1- level) ?|) " ")
|
|
|
|
(if (> level 1) " " "")
|
|
|
|
level
|
|
|
|
function
|
2008-06-05 05:46:11 +00:00
|
|
|
(let ((print-circle t))
|
|
|
|
(mapconcat (lambda (binding)
|
|
|
|
(concat
|
|
|
|
(symbol-name (ad-arg-binding-field binding 'name))
|
|
|
|
"="
|
|
|
|
;; do this so we'll see strings:
|
|
|
|
(prin1-to-string
|
|
|
|
(ad-arg-binding-field binding 'value))))
|
|
|
|
argument-bindings
|
|
|
|
" "))))
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
(defun trace-exit-message (function level value)
|
|
|
|
;; Generates a string that describes that FUNCTION has been exited at
|
|
|
|
;; trace LEVEL and that it returned VALUE.
|
|
|
|
(format "%s%s%d <- %s: %s\n"
|
|
|
|
(mapconcat 'char-to-string (make-string (1- level) ?|) " ")
|
|
|
|
(if (> level 1) " " "")
|
|
|
|
level
|
|
|
|
function
|
|
|
|
;; do this so we'll see strings:
|
2008-06-05 05:46:11 +00:00
|
|
|
(let ((print-circle t)) (prin1-to-string value))))
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
(defun trace-make-advice (function buffer background)
|
|
|
|
;; Builds the piece of advice to be added to FUNCTION's advice info
|
|
|
|
;; so that it will generate the proper trace output in BUFFER
|
|
|
|
;; (quietly if BACKGROUND is t).
|
|
|
|
(ad-make-advice
|
|
|
|
trace-advice-name nil t
|
2005-02-27 02:30:58 +00:00
|
|
|
`(advice
|
|
|
|
lambda ()
|
|
|
|
(let ((trace-level (1+ trace-level))
|
|
|
|
(trace-buffer (get-buffer-create ,buffer)))
|
|
|
|
(unless inhibit-trace
|
|
|
|
(with-current-buffer trace-buffer
|
2008-06-10 16:08:48 +00:00
|
|
|
(set (make-local-variable 'window-point-insertion-type) t)
|
|
|
|
,(unless background '(display-buffer trace-buffer))
|
2005-02-27 02:30:58 +00:00
|
|
|
(goto-char (point-max))
|
|
|
|
;; Insert a separator from previous trace output:
|
|
|
|
(if (= trace-level 1) (insert trace-separator))
|
|
|
|
(insert
|
|
|
|
(trace-entry-message
|
|
|
|
',function trace-level ad-arg-bindings))))
|
|
|
|
ad-do-it
|
|
|
|
(unless inhibit-trace
|
|
|
|
(with-current-buffer trace-buffer
|
2008-04-19 03:10:31 +00:00
|
|
|
,(unless background '(display-buffer trace-buffer))
|
2005-02-27 02:30:58 +00:00
|
|
|
(goto-char (point-max))
|
|
|
|
(insert
|
|
|
|
(trace-exit-message
|
|
|
|
',function trace-level ad-return-value))))))))
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
(defun trace-function-internal (function buffer background)
|
|
|
|
;; Adds trace advice for FUNCTION and activates it.
|
|
|
|
(ad-add-advice
|
|
|
|
function
|
|
|
|
(trace-make-advice function (or buffer trace-buffer) background)
|
|
|
|
'around 'last)
|
|
|
|
(ad-activate function nil))
|
|
|
|
|
|
|
|
(defun trace-is-traced (function)
|
|
|
|
(ad-find-advice function 'around trace-advice-name))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun trace-function (function &optional buffer)
|
|
|
|
"Traces FUNCTION with trace output going to BUFFER.
|
|
|
|
For every call of FUNCTION Lisp-style trace messages that display argument
|
2002-09-25 17:45:29 +00:00
|
|
|
and return values will be inserted into BUFFER. This function generates the
|
1993-05-18 03:19:14 +00:00
|
|
|
trace advice for FUNCTION and activates it together with any other advice
|
Fix typos in docstrings.
* image-dired.el (image-dired-display-thumbs): Fix typo in docstring.
(image-dired-read-comment): Doc fix.
* json.el (json-object-type, json-array-type, json-key-type, json-false)
(json-null, json-read-number):
* minibuffer.el (completion-in-region-functions):
* calendar/cal-tex.el (cal-tex-daily-end, cal-tex-number-weeks)
(cal-tex-cursor-week):
* emacs-lisp/trace.el (trace-function):
* eshell/em-basic.el (eshell/printnl):
* eshell/em-dirs.el (eshell-last-dir-ring, eshell-parse-drive-letter)
(eshell-read-last-dir-ring, eshell-write-last-dir-ring):
* obsolete/levents.el (allocate-event, event-key, event-object)
(event-point, event-process, event-timestamp, event-to-character)
(event-window, event-x, event-x-pixel, event-y, event-y-pixel):
* textmodes/reftex-vars.el (reftex-index-macros-builtin)
(reftex-section-levels, reftex-auto-recenter-toc, reftex-toc-mode-hook)
(reftex-cite-punctuation, reftex-search-unrecursed-path-first)
(reftex-highlight-selection): Fix typos in docstrings.
2010-03-22 17:50:29 +01:00
|
|
|
there might be!! The trace BUFFER will popup whenever FUNCTION is called.
|
1993-05-18 03:19:14 +00:00
|
|
|
Do not use this to trace functions that switch buffers or do any other
|
|
|
|
display oriented stuff, use `trace-function-background' instead."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(intern (completing-read "Trace function: " obarray 'fboundp t))
|
|
|
|
(read-buffer "Output to buffer: " trace-buffer)))
|
|
|
|
(trace-function-internal function buffer nil))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun trace-function-background (function &optional buffer)
|
|
|
|
"Traces FUNCTION with trace output going quietly to BUFFER.
|
2006-08-31 23:10:24 +00:00
|
|
|
When this tracing is enabled, every call to FUNCTION writes
|
|
|
|
a Lisp-style trace message (showing the arguments and return value)
|
|
|
|
into BUFFER. This function generates advice to trace FUNCTION
|
|
|
|
and activates it together with any other advice there might be.
|
|
|
|
The trace output goes to BUFFER quietly, without changing
|
|
|
|
the window or buffer configuration.
|
|
|
|
|
|
|
|
BUFFER defaults to `trace-buffer'."
|
1993-05-18 03:19:14 +00:00
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(intern
|
|
|
|
(completing-read "Trace function in background: " obarray 'fboundp t))
|
|
|
|
(read-buffer "Output to buffer: " trace-buffer)))
|
|
|
|
(trace-function-internal function buffer t))
|
|
|
|
|
|
|
|
(defun untrace-function (function)
|
|
|
|
"Untraces FUNCTION and possibly activates all remaining advice.
|
|
|
|
Activation is performed with `ad-update', hence remaining advice will get
|
2002-09-25 17:45:29 +00:00
|
|
|
activated only if the advice of FUNCTION is currently active. If FUNCTION
|
1993-05-18 03:19:14 +00:00
|
|
|
was not traced this is a noop."
|
|
|
|
(interactive
|
|
|
|
(list (ad-read-advised-function "Untrace function: " 'trace-is-traced)))
|
2005-02-27 02:30:58 +00:00
|
|
|
(when (trace-is-traced function)
|
|
|
|
(ad-remove-advice function 'around trace-advice-name)
|
|
|
|
(ad-update function)))
|
1993-05-18 03:19:14 +00:00
|
|
|
|
|
|
|
(defun untrace-all ()
|
|
|
|
"Untraces all currently traced functions."
|
|
|
|
(interactive)
|
|
|
|
(ad-do-advised-functions (function)
|
|
|
|
(untrace-function function)))
|
|
|
|
|
|
|
|
(provide 'trace)
|
|
|
|
|
2005-02-27 02:30:58 +00:00
|
|
|
;; arch-tag: cfd170a7-4932-4331-8c8b-b7151942e5a1
|
1993-05-18 03:19:14 +00:00
|
|
|
;;; trace.el ends here
|