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