Mercurial > emacs
annotate lisp/emacs-lisp/debug.el @ 911:e18a3f6689a9
entered into RCS
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Mon, 03 Aug 1992 16:41:27 +0000 |
parents | 213978acbc1e |
children | 2c7997f249eb |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
1 ;;; debug.el --- debuggers and related commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keyword: lisp, tools |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
7 |
473 | 8 ;; This file is part of GNU Emacs. |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
473 | 13 ;; any later version. |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
24 ;;; Code: |
473 | 25 |
26 (defvar debug-function-list nil | |
27 "List of functions currently set for debug on entry.") | |
28 | |
29 ;;;###autoload | |
30 (setq debugger 'debug) | |
31 ;;;###autoload | |
32 (defun debug (&rest debugger-args) | |
33 "Enter debugger. Returns if user says \"continue\". | |
34 Arguments are mainly for use when this is called from the internals | |
35 of the evaluator. | |
36 | |
37 You may call with no args, or you may pass nil as the first arg and | |
38 any other args you like. In that case, the list of args after the | |
39 first will be printed into the backtrace buffer." | |
40 (message "Entering debugger...") | |
41 (let (debugger-value | |
42 (debugger-match-data (match-data)) | |
43 (debug-on-error nil) | |
44 (debug-on-quit nil) | |
45 (debugger-buffer (let ((default-major-mode 'fundamental-mode)) | |
46 (generate-new-buffer "*Backtrace*"))) | |
47 (debugger-old-buffer (current-buffer)) | |
48 (debugger-step-after-exit nil) | |
49 ;; Don't keep reading from an executing kbd macro! | |
50 (executing-macro nil) | |
51 (cursor-in-echo-area nil)) | |
52 (unwind-protect | |
53 (save-excursion | |
54 (save-window-excursion | |
55 (pop-to-buffer debugger-buffer) | |
56 (erase-buffer) | |
57 (let ((standard-output (current-buffer)) | |
58 (print-escape-newlines t) | |
59 (print-length 50)) | |
60 (backtrace)) | |
61 (goto-char (point-min)) | |
62 (debugger-mode) | |
63 (delete-region (point) | |
64 (progn | |
65 (search-forward "\n debug(") | |
66 (forward-line 1) | |
67 (point))) | |
68 (debugger-reenable) | |
69 (cond ((memq (car debugger-args) '(lambda debug)) | |
70 (insert "Entering:\n") | |
71 (if (eq (car debugger-args) 'debug) | |
72 (progn | |
73 (backtrace-debug 4 t) | |
74 (delete-char 1) | |
75 (insert ?*) | |
76 (beginning-of-line)))) | |
77 ((eq (car debugger-args) 'exit) | |
78 (insert "Return value: ") | |
79 (setq debugger-value (nth 1 debugger-args)) | |
80 (prin1 debugger-value (current-buffer)) | |
81 (insert ?\n) | |
82 (delete-char 1) | |
83 (insert ? ) | |
84 (beginning-of-line)) | |
85 ((eq (car debugger-args) 'error) | |
86 (insert "Signalling: ") | |
87 (prin1 (nth 1 debugger-args) (current-buffer)) | |
88 (insert ?\n)) | |
89 ((eq (car debugger-args) t) | |
90 (insert "Beginning evaluation of function call form:\n")) | |
91 (t | |
92 (prin1 (if (eq (car debugger-args) 'nil) | |
93 (cdr debugger-args) debugger-args) | |
94 (current-buffer)) | |
95 (insert ?\n))) | |
96 (message "") | |
97 (let ((inhibit-trace t) | |
98 (standard-output nil) | |
99 (buffer-read-only t)) | |
100 (message "") | |
101 (recursive-edit)))) | |
102 ;; So that users do not try to execute debugger commands | |
103 ;; in an invalid context | |
104 (kill-buffer debugger-buffer) | |
105 (store-match-data debugger-match-data)) | |
106 (setq debug-on-next-call debugger-step-after-exit) | |
107 debugger-value)) | |
108 | |
109 (defun debugger-step-through () | |
110 "Proceed, stepping through subexpressions of this expression. | |
111 Enter another debugger on next entry to eval, apply or funcall." | |
112 (interactive) | |
113 (setq debugger-step-after-exit t) | |
114 (message "Proceeding, will debug on next eval or call.") | |
115 (exit-recursive-edit)) | |
116 | |
117 (defun debugger-continue () | |
118 "Continue, evaluating this expression without stopping." | |
119 (interactive) | |
120 (message "Continuing.") | |
121 (exit-recursive-edit)) | |
122 | |
123 (defun debugger-return-value (val) | |
124 "Continue, specifying value to return. | |
125 This is only useful when the value returned from the debugger | |
126 will be used, such as in a debug on exit from a frame." | |
127 (interactive "XReturn value (evaluated): ") | |
128 (setq debugger-value val) | |
129 (princ "Returning " t) | |
130 (prin1 debugger-value) | |
131 (exit-recursive-edit)) | |
132 | |
133 (defun debugger-jump () | |
134 "Continue to exit from this frame, with all debug-on-entry suspended." | |
135 (interactive) | |
136 ;; Compensate for the two extra stack frames for debugger-jump. | |
137 (let ((debugger-frame-offset (+ debugger-frame-offset 2))) | |
138 (debugger-frame)) | |
139 ;; Turn off all debug-on-entry functions | |
140 ;; but leave them in the list. | |
141 (let ((list debug-function-list)) | |
142 (while list | |
143 (fset (car list) | |
144 (debug-on-entry-1 (car list) (symbol-function (car list)) nil)) | |
145 (setq list (cdr list)))) | |
146 (message "Continuing through this frame") | |
147 (exit-recursive-edit)) | |
148 | |
149 (defun debugger-reenable () | |
150 "Turn all debug-on-entry functions back on." | |
151 (let ((list debug-function-list)) | |
152 (while list | |
153 (or (consp (symbol-function (car list))) | |
154 (debug-convert-byte-code (car list))) | |
155 (fset (car list) | |
156 (debug-on-entry-1 (car list) (symbol-function (car list)) t)) | |
157 (setq list (cdr list))))) | |
158 | |
159 (defun debugger-frame-number () | |
160 "Return number of frames in backtrace before the one point points at." | |
161 (save-excursion | |
162 (beginning-of-line) | |
163 (let ((opoint (point)) | |
164 (count 0)) | |
165 (goto-char (point-min)) | |
166 (if (or (equal (buffer-substring (point) (+ (point) 6)) | |
167 "Signal") | |
168 (equal (buffer-substring (point) (+ (point) 6)) | |
169 "Return")) | |
170 (progn | |
171 (search-forward ":") | |
172 (forward-sexp 1))) | |
173 (forward-line 1) | |
174 (while (progn | |
175 (forward-char 2) | |
176 (if (= (following-char) ?\() | |
177 (forward-sexp 1) | |
178 (forward-sexp 2)) | |
179 (forward-line 1) | |
180 (<= (point) opoint)) | |
181 (setq count (1+ count))) | |
182 count))) | |
183 | |
184 ;; Chosen empirically to account for all the frames | |
185 ;; that will exist when debugger-frame is called | |
186 ;; within the first one that appears in the backtrace buffer. | |
187 ;; Assumes debugger-frame is called from a key; | |
188 ;; will be wrong if it is called with Meta-x. | |
189 (defconst debugger-frame-offset 8 "") | |
190 | |
191 (defun debugger-frame () | |
192 "Request entry to debugger when this frame exits. | |
193 Applies to the frame whose line point is on in the backtrace." | |
194 (interactive) | |
195 (beginning-of-line) | |
196 (let ((level (debugger-frame-number))) | |
197 (backtrace-debug (+ level debugger-frame-offset) t)) | |
198 (if (= (following-char) ? ) | |
199 (let ((buffer-read-only nil)) | |
200 (delete-char 1) | |
201 (insert ?*))) | |
202 (beginning-of-line)) | |
203 | |
204 (defun debugger-frame-clear () | |
205 "Do not enter to debugger when this frame exits. | |
206 Applies to the frame whose line point is on in the backtrace." | |
207 (interactive) | |
208 (beginning-of-line) | |
209 (let ((level (debugger-frame-number))) | |
210 (backtrace-debug (+ level debugger-frame-offset) nil)) | |
211 (if (= (following-char) ?*) | |
212 (let ((buffer-read-only nil)) | |
213 (delete-char 1) | |
214 (insert ? ))) | |
215 (beginning-of-line)) | |
216 | |
217 (defun debugger-eval-expression (exp) | |
218 (interactive "xEval: ") | |
219 (save-excursion | |
220 (if (null (buffer-name debugger-old-buffer)) | |
221 ;; old buffer deleted | |
222 (setq debugger-old-buffer (current-buffer))) | |
223 (set-buffer debugger-old-buffer) | |
224 (eval-expression exp))) | |
225 | |
226 (defvar debugger-mode-map nil) | |
227 (if debugger-mode-map | |
228 nil | |
229 (let ((loop ? )) | |
230 (setq debugger-mode-map (make-keymap)) | |
231 (suppress-keymap debugger-mode-map) | |
232 (define-key debugger-mode-map "-" 'negative-argument) | |
233 (define-key debugger-mode-map "b" 'debugger-frame) | |
234 (define-key debugger-mode-map "c" 'debugger-continue) | |
235 (define-key debugger-mode-map "j" 'debugger-jump) | |
236 (define-key debugger-mode-map "r" 'debugger-return-value) | |
237 (define-key debugger-mode-map "u" 'debugger-frame-clear) | |
238 (define-key debugger-mode-map "d" 'debugger-step-through) | |
239 (define-key debugger-mode-map "l" 'debugger-list-functions) | |
240 (define-key debugger-mode-map "h" 'describe-mode) | |
241 (define-key debugger-mode-map "q" 'top-level) | |
242 (define-key debugger-mode-map "e" 'debugger-eval-expression) | |
243 (define-key debugger-mode-map " " 'next-line))) | |
244 | |
245 (put 'debugger-mode 'mode-class 'special) | |
246 | |
247 (defun debugger-mode () | |
248 "Mode for backtrace buffers, selected in debugger. | |
249 \\<debugger-mode-map> | |
250 A line starts with `*' if exiting that frame will call the debugger. | |
251 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. | |
252 | |
253 When in debugger due to frame being exited, | |
254 use the \\[debugger-return-value] command to override the value | |
255 being returned from that frame. | |
256 | |
257 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control | |
258 which functions will enter the debugger when called. | |
259 | |
260 Complete list of commands: | |
261 \\{debugger-mode-map}" | |
262 (kill-all-local-variables) | |
263 (setq major-mode 'debugger-mode) | |
264 (setq mode-name "Debugger") | |
265 (setq truncate-lines t) | |
266 (set-syntax-table emacs-lisp-mode-syntax-table) | |
267 (use-local-map debugger-mode-map)) | |
268 | |
269 ;;;###autoload | |
270 (defun debug-on-entry (function) | |
271 "Request FUNCTION to invoke debugger each time it is called. | |
272 If the user continues, FUNCTION's execution proceeds. | |
273 Works by modifying the definition of FUNCTION, | |
274 which must be written in Lisp, not predefined. | |
275 Use \\[cancel-debug-on-entry] to cancel the effect of this command. | |
276 Redefining FUNCTION also does that." | |
277 (interactive "aDebug on entry (to function): ") | |
278 (debugger-reenable) | |
279 (if (subrp (symbol-function function)) | |
280 (error "Function %s is a primitive" function)) | |
281 (or (consp (symbol-function function)) | |
282 (debug-convert-byte-code function)) | |
283 (or (consp (symbol-function function)) | |
284 (error "Definition of %s is not a list" function)) | |
285 (fset function (debug-on-entry-1 function (symbol-function function) t)) | |
286 (or (memq function debug-function-list) | |
287 (setq debug-function-list (cons function debug-function-list))) | |
288 function) | |
289 | |
290 ;;;###autoload | |
291 (defun cancel-debug-on-entry (&optional function) | |
292 "Undo effect of \\[debug-on-entry] on FUNCTION. | |
293 If argument is nil or an empty string, cancel for all functions." | |
477 | 294 (interactive |
295 (list (let ((name | |
296 (completing-read "Cancel debug on entry (to function): " | |
297 ;; Make an "alist" of the functions | |
298 ;; that now have debug on entry. | |
299 (mapcar 'list | |
300 (mapcar 'symbol-name | |
301 debug-function-list)) | |
302 nil t nil))) | |
303 (if name (intern name))))) | |
473 | 304 (debugger-reenable) |
305 (if (and function (not (string= function ""))) | |
306 (progn | |
307 (fset function | |
308 (debug-on-entry-1 function (symbol-function function) nil)) | |
309 (setq debug-function-list (delq function debug-function-list)) | |
310 function) | |
311 (message "Cancelling debug-on-entry for all functions") | |
312 (mapcar 'cancel-debug-on-entry debug-function-list))) | |
313 | |
314 (defun debug-convert-byte-code (function) | |
315 (let ((defn (symbol-function function))) | |
316 (if (not (consp defn)) | |
317 ;; Assume a compiled code object. | |
318 (let* ((contents (append defn nil)) | |
319 (body | |
320 (list (list 'byte-code (nth 1 contents) | |
321 (nth 2 contents) (nth 3 contents))))) | |
322 (if (nthcdr 5 contents) | |
323 (setq body (cons (list 'interactive (nth 5 contents)) body))) | |
324 (if (nth 4 contents) | |
325 (setq body (cons (nth 4 contents) body))) | |
326 (fset function (cons 'lambda (cons (car contents) body))))))) | |
327 | |
328 (defun debug-on-entry-1 (function defn flag) | |
329 (if (subrp defn) | |
330 (error "%s is a built-in function" function) | |
331 (if (eq (car defn) 'macro) | |
332 (debug-on-entry-1 function (cdr defn) flag) | |
333 (or (eq (car defn) 'lambda) | |
334 (error "%s not user-defined Lisp function" function)) | |
335 (let (tail prec) | |
336 (if (stringp (car (nthcdr 2 defn))) | |
337 (setq tail (nthcdr 3 defn) | |
338 prec (list (car defn) (car (cdr defn)) | |
339 (car (cdr (cdr defn))))) | |
340 (setq tail (nthcdr 2 defn) | |
341 prec (list (car defn) (car (cdr defn))))) | |
342 (if (eq flag (equal (car tail) '(debug 'debug))) | |
343 defn | |
344 (if flag | |
345 (nconc prec (cons '(debug 'debug) tail)) | |
346 (nconc prec (cdr tail)))))))) | |
347 | |
348 (defun debugger-list-functions () | |
349 "Display a list of all the functions now set to debug on entry." | |
350 (interactive) | |
351 (with-output-to-temp-buffer "*Help*" | |
352 (if (null debug-function-list) | |
353 (princ "No debug-on-entry functions now\n") | |
354 (princ "Functions set to debug on entry:\n\n") | |
355 (let ((list debug-function-list)) | |
356 (while list | |
357 (prin1 (car list)) | |
358 (terpri) | |
359 (setq list (cdr list)))) | |
360 (princ "Note: if you have redefined a function, then it may no longer\n") | |
361 (princ "be set to debug on entry, even if it is in the list.")))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
362 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
477
diff
changeset
|
363 ;;; debug.el ends here |