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