trace.el 10.0 KB

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