PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/emacs-lisp/trace.el

https://gitlab.com/freesoftware/emacs
Emacs Lisp | 338 lines | 162 code | 44 blank | 132 comment | 9 complexity | 81815475dc203014623f83e542ee8f0c MD5 | raw file
  1. ;;; trace.el --- tracing facility for Emacs Lisp functions -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993, 1998, 2000-2022 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 <https://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 (get-buffer-create 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. (print-escape-newlines t))
  151. (format "%s%s%d -> %S%s\n"
  152. (mapconcat #'char-to-string (make-string (max 0 (1- level)) ?|) " ")
  153. (if (> level 1) " " "")
  154. level
  155. ;; FIXME: Make it so we can click the function name to jump to its
  156. ;; definition and/or untrace it.
  157. (cons function args)
  158. context)))
  159. (defun trace-exit-message (function level value context)
  160. "Generate a string that describes that FUNCTION has exited.
  161. LEVEL is the trace level, VALUE value returned by FUNCTION,
  162. and CONTEXT is a string describing the dynamic context (e.g. values of
  163. some global variables)."
  164. (let ((print-circle t)
  165. (print-escape-newlines t))
  166. (format "%s%s%d <- %s: %S%s\n"
  167. (mapconcat 'char-to-string (make-string (1- level) ?|) " ")
  168. (if (> level 1) " " "")
  169. level
  170. function
  171. ;; Do this so we'll see strings:
  172. value
  173. context)))
  174. (defvar trace--timer nil)
  175. (defun trace--display-buffer (buf)
  176. (unless (or trace--timer
  177. (get-buffer-window buf 'visible))
  178. (setq trace--timer
  179. ;; Postpone the display to some later time, in case we
  180. ;; can't actually do it now.
  181. (run-with-timer 0 nil
  182. (lambda ()
  183. (setq trace--timer nil)
  184. (display-buffer buf nil 0))))))
  185. (defun trace-make-advice (function buffer background context)
  186. "Build the piece of advice to be added to trace FUNCTION.
  187. FUNCTION is the name of the traced function.
  188. BUFFER is the buffer where the trace should be printed.
  189. BACKGROUND if nil means to display BUFFER.
  190. CONTEXT if non-nil should be a function that returns extra info that should
  191. be printed along with the arguments in the trace."
  192. (lambda (body &rest args)
  193. (let ((trace-level (1+ trace-level))
  194. (trace-buffer (get-buffer-create buffer))
  195. (deactivate-mark nil) ;Protect deactivate-mark.
  196. (ctx (funcall context)))
  197. (unless inhibit-trace
  198. (with-current-buffer trace-buffer
  199. (setq-local window-point-insertion-type t)
  200. (unless background (trace--display-buffer trace-buffer))
  201. (goto-char (point-max))
  202. ;; Insert a separator from previous trace output:
  203. (if (= trace-level 1) (insert trace-separator))
  204. (insert
  205. (trace-entry-message
  206. function trace-level args ctx))))
  207. (let ((result))
  208. (unwind-protect
  209. (setq result (list (apply body args)))
  210. (unless inhibit-trace
  211. (let ((ctx (funcall context)))
  212. (with-current-buffer trace-buffer
  213. (unless background (trace--display-buffer trace-buffer))
  214. (goto-char (point-max))
  215. (insert
  216. (trace-exit-message
  217. function
  218. trace-level
  219. (if result (car result) '\!non-local\ exit\!)
  220. ctx))))))
  221. (car result)))))
  222. (defun trace-function-internal (function buffer background context)
  223. "Add trace advice for FUNCTION."
  224. (advice-add
  225. function :around
  226. (trace-make-advice function (or buffer trace-buffer) background
  227. (or context (lambda () "")))
  228. `((name . ,trace-advice-name) (depth . -100))))
  229. (defun trace-is-traced (function)
  230. (advice-member-p trace-advice-name function))
  231. (defun trace--read-args (prompt)
  232. "Read a function name, prompting with string PROMPT.
  233. If `current-prefix-arg' is non-nil, also read a buffer and a \"context\"
  234. \(Lisp expression). Return (FUNCTION BUFFER FUNCTION-CONTEXT)."
  235. (cons
  236. (let ((default (function-called-at-point)))
  237. (intern (completing-read (format-prompt prompt default)
  238. obarray 'fboundp t nil nil
  239. (if default (symbol-name default)))))
  240. (when current-prefix-arg
  241. (list
  242. (read-buffer (format-prompt "Output to buffer" trace-buffer))
  243. (let ((exp
  244. (let ((minibuffer-completing-symbol t))
  245. (read-from-minibuffer "Context expression: "
  246. nil read-expression-map t
  247. 'read-expression-history))))
  248. (lambda ()
  249. (let ((print-circle t)
  250. (print-escape-newlines t))
  251. (concat " [" (prin1-to-string (eval exp t)) "]"))))))))
  252. ;;;###autoload
  253. (defun trace-function-foreground (function &optional buffer context)
  254. "Trace calls to function FUNCTION.
  255. With a prefix argument, also prompt for the trace buffer (default
  256. `trace-buffer'), and a Lisp expression CONTEXT. When called from
  257. Lisp, CONTEXT should be a function of no arguments which returns
  258. a value to insert into BUFFER during the trace.
  259. Tracing a function causes every call to that function to insert
  260. into BUFFER Lisp-style trace messages that display the function's
  261. arguments and return values. It also evaluates CONTEXT, if that is
  262. non-nil, and inserts its value too. For example, you can use this
  263. to track the current buffer, or position of point.
  264. This function creates BUFFER if it does not exist. This buffer will
  265. popup whenever FUNCTION is called. Do not use this function to trace
  266. functions that switch buffers, or do any other display-oriented
  267. stuff - use `trace-function-background' instead.
  268. To stop tracing a function, use `untrace-function' or `untrace-all'."
  269. (interactive (trace--read-args "Trace function"))
  270. (trace-function-internal function buffer nil context))
  271. ;;;###autoload
  272. (defun trace-function-background (function &optional buffer context)
  273. "Trace calls to function FUNCTION, quietly.
  274. This is like `trace-function-foreground', but without popping up
  275. the output buffer or changing the window configuration."
  276. (interactive (trace--read-args "Trace function in background"))
  277. (trace-function-internal function buffer t context))
  278. ;;;###autoload
  279. (defalias 'trace-function 'trace-function-foreground)
  280. (defun untrace-function (function)
  281. "Untraces FUNCTION and possibly activates all remaining advice.
  282. Activation is performed with `ad-update', hence remaining advice will get
  283. activated only if the advice of FUNCTION is currently active. If FUNCTION
  284. was not traced this is a noop."
  285. (interactive
  286. (list (intern (completing-read "Untrace function: "
  287. obarray #'trace-is-traced t))))
  288. (advice-remove function trace-advice-name))
  289. (defun untrace-all ()
  290. "Untraces all currently traced functions."
  291. (interactive)
  292. (mapatoms #'untrace-function))
  293. (provide 'trace)
  294. ;;; trace.el ends here