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