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