annotate lisp/emacs-lisp/trace.el @ 13643:146c5daf3019

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