trace.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. ;;; trace.el --- tracing facility for Emacs Lisp functions -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993, 1998, 2000-2017 Free Software Foundation, Inc.
  3. ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
  4. ;; Maintainer: emacs-devel@gnu.org
  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 nadvice.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. ;; Restrictions:
  31. ;; =============
  32. ;; - Traced subrs when called interactively will always show nil as the
  33. ;; value of their arguments.
  34. ;; - Only functions/macros/subrs that are called via their function cell will
  35. ;; generate trace output, hence, you won't get trace output for:
  36. ;; + Subrs called directly from other subrs/C-code
  37. ;; + Compiled calls to subrs that have special byte-codes associated
  38. ;; with them (e.g., car, cdr, ...)
  39. ;; + Macros that were expanded during compilation
  40. ;; - All the restrictions that apply to nadvice.el
  41. ;; Usage:
  42. ;; ======
  43. ;; - To trace a function say `M-x trace-function', which will ask you for the
  44. ;; name of the function/subr/macro to trace.
  45. ;; - If you want to trace a function that switches buffers or does other
  46. ;; display oriented stuff use `M-x trace-function-background', which will
  47. ;; generate the trace output silently in the background without popping
  48. ;; up windows and doing other irritating stuff.
  49. ;; - To untrace a function say `M-x untrace-function'.
  50. ;; - To untrace all currently traced functions say `M-x untrace-all'.
  51. ;; Examples:
  52. ;; =========
  53. ;;
  54. ;; (defun fact (n)
  55. ;; (if (= n 0) 1
  56. ;; (* n (fact (1- n)))))
  57. ;; fact
  58. ;;
  59. ;; (trace-function 'fact)
  60. ;; fact
  61. ;;
  62. ;; Now, evaluating this...
  63. ;;
  64. ;; (fact 4)
  65. ;; 24
  66. ;;
  67. ;; ...will generate the following in *trace-buffer*:
  68. ;;
  69. ;; 1 -> fact: n=4
  70. ;; | 2 -> fact: n=3
  71. ;; | | 3 -> fact: n=2
  72. ;; | | | 4 -> fact: n=1
  73. ;; | | | | 5 -> fact: n=0
  74. ;; | | | | 5 <- fact: 1
  75. ;; | | | 4 <- fact: 1
  76. ;; | | 3 <- fact: 2
  77. ;; | 2 <- fact: 6
  78. ;; 1 <- fact: 24
  79. ;;
  80. ;;
  81. ;; (defun ack (x y z)
  82. ;; (if (= x 0)
  83. ;; (+ y z)
  84. ;; (if (and (<= x 2) (= z 0))
  85. ;; (1- x)
  86. ;; (if (and (> x 2) (= z 0))
  87. ;; y
  88. ;; (ack (1- x) y (ack x y (1- z)))))))
  89. ;; ack
  90. ;;
  91. ;; (trace-function 'ack)
  92. ;; ack
  93. ;;
  94. ;; Try this for some interesting trace output:
  95. ;;
  96. ;; (ack 3 3 1)
  97. ;; 27
  98. ;;
  99. ;;
  100. ;; The following does something similar to the functionality of the package
  101. ;; log-message.el by Robert Potter, which is giving you a chance to look at
  102. ;; messages that might have whizzed by too quickly (you won't see subr
  103. ;; generated messages though):
  104. ;;
  105. ;; (trace-function-background 'message "*Message Log*")
  106. ;;; Change Log:
  107. ;; Revision 2.0 1993/05/18 00:41:16 hans
  108. ;; * Adapted for advice.el 2.0; it now also works
  109. ;; for GNU Emacs-19 and Lemacs
  110. ;; * Separate function `trace-function-background'
  111. ;; * Separate pieces of advice for foreground and background tracing
  112. ;; * Less insane handling of interactive trace buffer specification
  113. ;; * String arguments and values are now printed properly
  114. ;;
  115. ;; Revision 1.1 1992/12/15 22:45:15 hans
  116. ;; * Created, first public release
  117. ;;; Code:
  118. (defgroup trace nil
  119. "Tracing facility for Emacs Lisp functions."
  120. :prefix "trace-"
  121. :group 'lisp)
  122. ;;;###autoload
  123. (defcustom trace-buffer "*trace-output*"
  124. "Trace output will by default go to that buffer."
  125. :type 'string)
  126. ;; Current level of traced function invocation:
  127. (defvar trace-level 0)
  128. ;; Semi-cryptic name used for a piece of trace advice:
  129. (defvar trace-advice-name 'trace-function\ )
  130. ;; Used to separate new trace output from previous traced runs:
  131. (defvar trace-separator (format "%s\n" (make-string 70 ?=)))
  132. (defvar inhibit-trace nil
  133. "If non-nil, all tracing is temporarily inhibited.")
  134. ;;;###autoload
  135. (defun trace-values (&rest values)
  136. "Helper function to get internal values.
  137. You can call this function to add internal values in the trace buffer."
  138. (unless inhibit-trace
  139. (with-current-buffer trace-buffer
  140. (goto-char (point-max))
  141. (insert
  142. (trace-entry-message
  143. 'trace-values trace-level values "")))))
  144. (defun trace-entry-message (function level args context)
  145. "Generate a string that describes that FUNCTION has been entered.
  146. LEVEL is the trace level, ARGS is the list of arguments passed to FUNCTION,
  147. and CONTEXT is a string describing the dynamic context (e.g. values of
  148. some global variables)."
  149. (let ((print-circle t))
  150. (format "%s%s%d -> %S%s\n"
  151. (mapconcat 'char-to-string (make-string (1- level) ?|) " ")
  152. (if (> level 1) " " "")
  153. level
  154. ;; FIXME: Make it so we can click the function name to jump to its
  155. ;; definition and/or untrace it.
  156. (cons function args)
  157. context)))
  158. (defun trace-exit-message (function level value context)
  159. "Generate a string that describes that FUNCTION has exited.
  160. LEVEL is the trace level, VALUE value returned by FUNCTION,
  161. and CONTEXT is a string describing the dynamic context (e.g. values of
  162. some global variables)."
  163. (let ((print-circle t))
  164. (format "%s%s%d <- %s: %S%s\n"
  165. (mapconcat 'char-to-string (make-string (1- level) ?|) " ")
  166. (if (> level 1) " " "")
  167. level
  168. function
  169. ;; Do this so we'll see strings:
  170. value
  171. context)))
  172. (defvar trace--timer nil)
  173. (defun trace--display-buffer (buf)
  174. (unless (or trace--timer
  175. (get-buffer-window buf 'visible))
  176. (setq trace--timer
  177. ;; Postpone the display to some later time, in case we
  178. ;; can't actually do it now.
  179. (run-with-timer 0 nil
  180. (lambda ()
  181. (setq trace--timer nil)
  182. (display-buffer buf nil 0))))))
  183. (defun trace-make-advice (function buffer background context)
  184. "Build the piece of advice to be added to trace FUNCTION.
  185. FUNCTION is the name of the traced function.
  186. BUFFER is the buffer where the trace should be printed.
  187. BACKGROUND if nil means to display BUFFER.
  188. CONTEXT if non-nil should be a function that returns extra info that should
  189. be printed along with the arguments in the trace."
  190. (lambda (body &rest args)
  191. (let ((trace-level (1+ trace-level))
  192. (trace-buffer (get-buffer-create buffer))
  193. (deactivate-mark nil) ;Protect deactivate-mark.
  194. (ctx (funcall context)))
  195. (unless inhibit-trace
  196. (with-current-buffer trace-buffer
  197. (set (make-local-variable 'window-point-insertion-type) t)
  198. (unless background (trace--display-buffer trace-buffer))
  199. (goto-char (point-max))
  200. ;; Insert a separator from previous trace output:
  201. (if (= trace-level 1) (insert trace-separator))
  202. (insert
  203. (trace-entry-message
  204. function trace-level args ctx))))
  205. (let ((result))
  206. (unwind-protect
  207. (setq result (list (apply body args)))
  208. (unless inhibit-trace
  209. (let ((ctx (funcall context)))
  210. (with-current-buffer trace-buffer
  211. (unless background (trace--display-buffer trace-buffer))
  212. (goto-char (point-max))
  213. (insert
  214. (trace-exit-message
  215. function
  216. trace-level
  217. (if result (car result) '\!non-local\ exit\!)
  218. ctx))))))
  219. (car result)))))
  220. (defun trace-function-internal (function buffer background context)
  221. "Add trace advice for FUNCTION."
  222. (advice-add
  223. function :around
  224. (trace-make-advice function (or buffer trace-buffer) background
  225. (or context (lambda () "")))
  226. `((name . ,trace-advice-name) (depth . -100))))
  227. (defun trace-is-traced (function)
  228. (advice-member-p trace-advice-name function))
  229. (defun trace--read-args (prompt)
  230. "Read a function name, prompting with string PROMPT.
  231. If `current-prefix-arg' is non-nil, also read a buffer and a \"context\"
  232. \(Lisp expression). Return (FUNCTION BUFFER FUNCTION-CONTEXT)."
  233. (cons
  234. (let ((default (function-called-at-point))
  235. (beg (string-match ":[ \t]*\\'" prompt)))
  236. (intern (completing-read (if default
  237. (format
  238. "%s (default %s)%s"
  239. (substring prompt 0 beg)
  240. default
  241. (if beg (substring prompt beg) ": "))
  242. prompt)
  243. obarray 'fboundp t nil nil
  244. (if default (symbol-name default)))))
  245. (when current-prefix-arg
  246. (list
  247. (read-buffer "Output to buffer: " trace-buffer)
  248. (let ((exp
  249. (let ((minibuffer-completing-symbol t))
  250. (read-from-minibuffer "Context expression: "
  251. nil read-expression-map t
  252. 'read-expression-history))))
  253. (lambda ()
  254. (let ((print-circle t))
  255. (concat " [" (prin1-to-string (eval exp t)) "]"))))))))
  256. ;;;###autoload
  257. (defun trace-function-foreground (function &optional buffer context)
  258. "Trace calls to function FUNCTION.
  259. With a prefix argument, also prompt for the trace buffer (default
  260. `trace-buffer'), and a Lisp expression CONTEXT.
  261. Tracing a function causes every call to that function to insert
  262. into BUFFER Lisp-style trace messages that display the function's
  263. arguments and return values. It also evaluates CONTEXT, if that is
  264. non-nil, and inserts its value too. For example, you can use this
  265. to track the current buffer, or position of point.
  266. This function creates BUFFER if it does not exist. This buffer will
  267. popup whenever FUNCTION is called. Do not use this function to trace
  268. functions that switch buffers, or do any other display-oriented
  269. stuff - use `trace-function-background' instead.
  270. To stop tracing a function, use `untrace-function' or `untrace-all'."
  271. (interactive (trace--read-args "Trace function: "))
  272. (trace-function-internal function buffer nil context))
  273. ;;;###autoload
  274. (defun trace-function-background (function &optional buffer context)
  275. "Trace calls to function FUNCTION, quietly.
  276. This is like `trace-function-foreground', but without popping up
  277. the output buffer or changing the window configuration."
  278. (interactive (trace--read-args "Trace function in background: "))
  279. (trace-function-internal function buffer t context))
  280. ;;;###autoload
  281. (defalias 'trace-function 'trace-function-foreground)
  282. (defun untrace-function (function)
  283. "Untraces FUNCTION and possibly activates all remaining advice.
  284. Activation is performed with `ad-update', hence remaining advice will get
  285. activated only if the advice of FUNCTION is currently active. If FUNCTION
  286. was not traced this is a noop."
  287. (interactive
  288. (list (intern (completing-read "Untrace function: "
  289. obarray #'trace-is-traced t))))
  290. (advice-remove function trace-advice-name))
  291. (defun untrace-all ()
  292. "Untraces all currently traced functions."
  293. (interactive)
  294. (mapatoms #'untrace-function))
  295. (provide 'trace)
  296. ;;; trace.el ends here