1 ;;; debug.el --- debuggers and related commands for Emacs
4 ;; Last-Modified: 08 Jan 1992
6 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
26 (defvar debug-function-list nil
27 "List of functions currently set for debug on entry.")
30 (setq debugger
'debug
)
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
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...")
42 (debugger-match-data (match-data))
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!
51 (cursor-in-echo-area nil
))
54 (save-window-excursion
55 (pop-to-buffer debugger-buffer
)
57 (let ((standard-output (current-buffer))
58 (print-escape-newlines t
)
61 (goto-char (point-min))
63 (delete-region (point)
65 (search-forward "\n debug(")
69 (cond ((memq (car debugger-args
) '(lambda debug
))
70 (insert "Entering:\n")
71 (if (eq (car debugger-args
) 'debug
)
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))
85 ((eq (car debugger-args
) 'error
)
86 (insert "Signalling: ")
87 (prin1 (nth 1 debugger-args
) (current-buffer))
89 ((eq (car debugger-args
) t
)
90 (insert "Beginning evaluation of function call form:\n"))
92 (prin1 (if (eq (car debugger-args
) 'nil
)
93 (cdr debugger-args
) debugger-args
)
97 (let ((inhibit-trace t
)
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
)
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."
113 (setq debugger-step-after-exit t
)
114 (message "Proceeding, will debug on next eval or call.")
115 (exit-recursive-edit))
117 (defun debugger-continue ()
118 "Continue, evaluating this expression without stopping."
120 (message "Continuing.")
121 (exit-recursive-edit))
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))
133 (defun debugger-jump ()
134 "Continue to exit from this frame, with all debug-on-entry suspended."
136 ;; Compensate for the two extra stack frames for debugger-jump.
137 (let ((debugger-frame-offset (+ debugger-frame-offset
2)))
139 ;; Turn off all debug-on-entry functions
140 ;; but leave them in the list.
141 (let ((list debug-function-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))
149 (defun debugger-reenable ()
150 "Turn all debug-on-entry functions back on."
151 (let ((list debug-function-list
))
153 (or (consp (symbol-function (car list
)))
154 (debug-convert-byte-code (car list
)))
156 (debug-on-entry-1 (car list
) (symbol-function (car list
)) t
))
157 (setq list
(cdr list
)))))
159 (defun debugger-frame-number ()
160 "Return number of frames in backtrace before the one point points at."
163 (let ((opoint (point))
165 (goto-char (point-min))
166 (if (or (equal (buffer-substring (point) (+ (point) 6))
168 (equal (buffer-substring (point) (+ (point) 6))
176 (if (= (following-char) ?\
()
181 (setq count
(1+ count
)))
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 "")
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."
196 (let ((level (debugger-frame-number)))
197 (backtrace-debug (+ level debugger-frame-offset
) t
))
198 (if (= (following-char) ?
)
199 (let ((buffer-read-only nil
))
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."
209 (let ((level (debugger-frame-number)))
210 (backtrace-debug (+ level debugger-frame-offset
) nil
))
211 (if (= (following-char) ?
*)
212 (let ((buffer-read-only nil
))
217 (defun debugger-eval-expression (exp)
218 (interactive "xEval: ")
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
)))
226 (defvar debugger-mode-map nil
)
227 (if debugger-mode-map
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
)))
245 (put 'debugger-mode
'mode-class
'special
)
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 `*'.
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.
257 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
258 which functions will enter the debugger when called.
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
))
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): ")
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
)))
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."
296 (completing-read "Cancel debug on entry (to function): "
297 ;; Make an "alist" of the functions
298 ;; that now have debug on entry.
301 debug-function-list
))
303 (if name
(intern name
)))))
305 (if (and function
(not (string= function
"")))
308 (debug-on-entry-1 function
(symbol-function function
) nil
))
309 (setq debug-function-list
(delq function debug-function-list
))
311 (message "Cancelling debug-on-entry for all functions")
312 (mapcar 'cancel-debug-on-entry debug-function-list
)))
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
))
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
)))
325 (setq body
(cons (nth 4 contents
) body
)))
326 (fset function
(cons 'lambda
(cons (car contents
) body
)))))))
328 (defun debug-on-entry-1 (function defn flag
)
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
))
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
)))
345 (nconc prec
(cons '(debug 'debug
) tail
))
346 (nconc prec
(cdr tail
))))))))
348 (defun debugger-list-functions ()
349 "Display a list of all the functions now set to debug on entry."
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
))
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."))))
363 ;;; debug.el ends here