* doc/lispref/debugging.texi (Error Debugging): Mention debug-on-message.
[bpt/emacs.git] / lisp / emacs-lisp / debug.el
CommitLineData
c0274f38
ER
1;;; debug.el --- debuggers and related commands for Emacs
2
acaf905b 3;; Copyright (C) 1985-1986, 1994, 2001-2012 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Maintainer: FSF
e9571d2a 6;; Keywords: lisp, tools, maint
e5167999 7
0231f2dc
JB
8;; This file is part of GNU Emacs.
9
d6cba7ae 10;; GNU Emacs is free software: you can redistribute it and/or modify
0231f2dc 11;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
0231f2dc
JB
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
d6cba7ae 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0231f2dc 22
e41b2db1
ER
23;;; Commentary:
24
07f3fdb1 25;; This is a major mode documented in the Emacs Lisp manual.
e41b2db1 26
e5167999 27;;; Code:
0231f2dc 28
bb9a622e
MB
29(require 'button)
30
666b9413
SE
31(defgroup debugger nil
32 "Debuggers and related commands for Emacs."
33 :prefix "debugger-"
34 :group 'debug)
8c1cd093 35
666b9413 36(defcustom debugger-mode-hook nil
cb711556 37 "Hooks run when `debugger-mode' is turned on."
666b9413 38 :type 'hook
cd32a7ba
DN
39 :group 'debugger
40 :version "20.3")
8c1cd093 41
63ca439e 42(defcustom debugger-batch-max-lines 40
cb711556 43 "Maximum lines to show in debugger buffer in a noninteractive Emacs.
63ca439e
GM
44When the debugger is entered and Emacs is running in batch mode,
45if the backtrace text has more than this many lines,
46the middle is discarded, and just the beginning and end are displayed."
47 :type 'integer
48 :group 'debugger
49 :version "21.1")
0231f2dc 50
fa2bcf43
MR
51(defcustom debugger-bury-or-kill 'bury
52 "How to proceed with the debugger buffer when exiting `debug'.
53The value used here affects the behavior of operations on any
54window previously showing the debugger buffer.
55
56`nil' means that if its window is not deleted when exiting the
57 debugger, invoking `switch-to-prev-buffer' will usually show
58 the debugger buffer again.
59
60`append' means that if the window is not deleted, the debugger
61 buffer moves to the end of the window's previous buffers so
62 it's less likely that a future invocation of
63 `switch-to-prev-buffer' will switch to it. Also, it moves the
64 buffer to the end of the frame's buffer list.
65
66`bury' means that if the window is not deleted, its buffer is
67 removed from the window's list of previous buffers. Also, it
68 moves the buffer to the end of the frame's buffer list. This
69 value provides the most reliable remedy to not have
70 `switch-to-prev-buffer' switch to the debugger buffer again
71 without killing the buffer.
72
73`kill' means to kill the debugger buffer.
74
75The value used here is passed to `quit-restore-window'."
76 :type '(choice
77 (const :tag "Keep alive" nil)
37ab5092
MR
78 (const :tag "Append" append)
79 (const :tag "Bury" bury)
80 (const :tag "Kill" kill))
fa2bcf43 81 :group 'debugger
68146aa5 82 :version "24.3")
fa2bcf43 83
15cf150d
LK
84(defvar debug-function-list nil
85 "List of functions currently set for debug on entry.")
86
87(defvar debugger-step-after-exit nil
88 "Non-nil means \"single-step\" after the debugger exits.")
2eeeb0d2
RS
89
90(defvar debugger-value nil
91 "This is the value for the debugger to return, when it returns.")
92
93(defvar debugger-old-buffer nil
94 "This is the buffer that was current when the debugger was entered.")
95
fa2bcf43
MR
96(defvar debugger-previous-window nil
97 "This is the window last showing the debugger buffer.")
98
4dece104
MR
99(defvar debugger-previous-window-height nil
100 "The last recorded height of `debugger-previous-window'.")
101
25c58854
RS
102(defvar debugger-previous-backtrace nil
103 "The contents of the previous backtrace (including text properties).
104This is to optimize `debugger-make-xrefs'.")
105
cf1c8cd9 106(defvar debugger-outer-match-data)
80ddb8ed
RS
107(defvar debugger-outer-load-read-function)
108(defvar debugger-outer-overriding-local-map)
29e2b496 109(defvar debugger-outer-overriding-terminal-local-map)
35cf010d
RS
110(defvar debugger-outer-track-mouse)
111(defvar debugger-outer-last-command)
112(defvar debugger-outer-this-command)
35cf010d 113(defvar debugger-outer-unread-command-events)
0359db82 114(defvar debugger-outer-unread-post-input-method-events)
35cf010d
RS
115(defvar debugger-outer-last-input-event)
116(defvar debugger-outer-last-command-event)
117(defvar debugger-outer-last-nonmenu-event)
118(defvar debugger-outer-last-event-frame)
119(defvar debugger-outer-standard-input)
120(defvar debugger-outer-standard-output)
bf90c2c0 121(defvar debugger-outer-inhibit-redisplay)
35cf010d 122(defvar debugger-outer-cursor-in-echo-area)
9b530428
SM
123(defvar debugger-will-be-back nil
124 "Non-nil if we expect to get back in the debugger soon.")
35cf010d 125
b6b77a90
LK
126(defvar inhibit-debug-on-entry nil
127 "Non-nil means that debug-on-entry is disabled.")
128
129(defvar debugger-jumping-flag nil
130 "Non-nil means that debug-on-entry is disabled.
9b54d453
LK
131This variable is used by `debugger-jump', `debugger-step-through',
132and `debugger-reenable' to temporarily disable debug-on-entry.")
cf9a1b69 133
d48f0f00 134(defvar inhibit-trace) ;Not yet implemented.
77932668 135
e24e27be
SM
136(defvar debugger-args nil
137 "Arguments with which the debugger was called.
138It is a list expected to take the form (CAUSE . REST)
139where CAUSE can be:
140- debug: called for entry to a flagged function.
141- t: called because of debug-on-next-call.
142- lambda: same thing but via `funcall'.
143- exit: called because of exit of a flagged function.
144- error: called because of `debug-on-error'.")
145
0231f2dc
JB
146;;;###autoload
147(setq debugger 'debug)
148;;;###autoload
149(defun debug (&rest debugger-args)
a7c33da2 150 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
0231f2dc
JB
151Arguments are mainly for use when this is called from the internals
152of the evaluator.
153
154You may call with no args, or you may pass nil as the first arg and
155any other args you like. In that case, the list of args after the
156first will be printed into the backtrace buffer."
73e78cf1 157 (interactive)
e734f584
RS
158 (if inhibit-redisplay
159 ;; Don't really try to enter debugger within an eval from redisplay.
160 debugger-value
161 (unless noninteractive
162 (message "Entering debugger..."))
163 (let (debugger-value
e227544d
SM
164 (debugger-previous-state
165 (if (get-buffer "*Backtrace*")
166 (with-current-buffer (get-buffer "*Backtrace*")
167 (list major-mode (buffer-string)))))
72f16325 168 (debugger-buffer (get-buffer-create "*Backtrace*"))
96e8d411 169 (debugger-old-buffer (current-buffer))
fa2bcf43 170 (debugger-window nil)
e734f584 171 (debugger-step-after-exit nil)
9b530428 172 (debugger-will-be-back nil)
e734f584
RS
173 ;; Don't keep reading from an executing kbd macro!
174 (executing-kbd-macro nil)
175 ;; Save the outer values of these vars for the `e' command
176 ;; before we replace the values.
177 (debugger-outer-match-data (match-data))
178 (debugger-outer-load-read-function load-read-function)
179 (debugger-outer-overriding-local-map overriding-local-map)
180 (debugger-outer-overriding-terminal-local-map
181 overriding-terminal-local-map)
182 (debugger-outer-track-mouse track-mouse)
183 (debugger-outer-last-command last-command)
184 (debugger-outer-this-command this-command)
e734f584
RS
185 (debugger-outer-unread-command-events unread-command-events)
186 (debugger-outer-unread-post-input-method-events
187 unread-post-input-method-events)
188 (debugger-outer-last-input-event last-input-event)
189 (debugger-outer-last-command-event last-command-event)
190 (debugger-outer-last-nonmenu-event last-nonmenu-event)
191 (debugger-outer-last-event-frame last-event-frame)
192 (debugger-outer-standard-input standard-input)
193 (debugger-outer-standard-output standard-output)
194 (debugger-outer-inhibit-redisplay inhibit-redisplay)
385480e5
RS
195 (debugger-outer-cursor-in-echo-area cursor-in-echo-area)
196 (debugger-with-timeout-suspend (with-timeout-suspend)))
e734f584
RS
197 ;; Set this instead of binding it, so that `q'
198 ;; will not restore it.
199 (setq overriding-terminal-local-map nil)
200 ;; Don't let these magic variables affect the debugger itself.
201 (let ((last-command nil) this-command track-mouse
cf9a1b69
SM
202 (inhibit-trace t)
203 (inhibit-debug-on-entry t)
12cf32ce 204 unread-command-events
e734f584
RS
205 unread-post-input-method-events
206 last-input-event last-command-event last-nonmenu-event
207 last-event-frame
208 overriding-local-map
209 load-read-function
210 ;; If we are inside a minibuffer, allow nesting
211 ;; so that we don't get an error from the `e' command.
212 (enable-recursive-minibuffers
213 (or enable-recursive-minibuffers (> (minibuffer-depth) 0)))
214 (standard-input t) (standard-output t)
215 inhibit-redisplay
88ded8ac
MR
216 (cursor-in-echo-area nil)
217 (window-configuration (current-window-configuration)))
e734f584
RS
218 (unwind-protect
219 (save-excursion
fa2bcf43
MR
220 (when (eq (car debugger-args) 'debug)
221 ;; Skip the frames for backtrace-debug, byte-code,
222 ;; and implement-debug-on-entry.
223 (backtrace-debug 4 t)
224 ;; Place an extra debug-on-exit for macro's.
225 (when (eq 'lambda (car-safe (cadr (backtrace-frame 4))))
226 (backtrace-debug 5 t)))
227 (pop-to-buffer
228 debugger-buffer
229 `((display-buffer-reuse-window
230 display-buffer-in-previous-window)
231 . (,(when debugger-previous-window
232 `(previous-window . ,debugger-previous-window)))))
233 (setq debugger-window (selected-window))
4dece104
MR
234 (if (eq debugger-previous-window debugger-window)
235 (when debugger-jumping-flag
236 ;; Try to restore previous height of debugger
237 ;; window.
238 (condition-case nil
239 (window-resize
240 debugger-window
241 (- debugger-previous-window-height
242 (window-total-size debugger-window)))
243 (error nil)))
244 (setq debugger-previous-window debugger-window))
fa2bcf43
MR
245 (debugger-mode)
246 (debugger-setup-buffer debugger-args)
247 (when noninteractive
248 ;; If the backtrace is long, save the beginning
249 ;; and the end, but discard the middle.
250 (when (> (count-lines (point-min) (point-max))
251 debugger-batch-max-lines)
63ca439e 252 (goto-char (point-min))
fa2bcf43
MR
253 (forward-line (/ 2 debugger-batch-max-lines))
254 (let ((middlestart (point)))
255 (goto-char (point-max))
256 (forward-line (- (/ 2 debugger-batch-max-lines)
257 debugger-batch-max-lines))
258 (delete-region middlestart (point)))
259 (insert "...\n"))
260 (goto-char (point-min))
261 (message "%s" (buffer-string))
262 (kill-emacs -1))
263 (message "")
264 (let ((standard-output nil)
265 (buffer-read-only t))
35cf010d 266 (message "")
fa2bcf43
MR
267 ;; Make sure we unbind buffer-read-only in the right buffer.
268 (save-excursion
269 (recursive-edit))))
fb0104da
MR
270 (when (and (window-live-p debugger-window)
271 (eq (window-buffer debugger-window) debugger-buffer))
272 ;; Record height of debugger window.
273 (setq debugger-previous-window-height
274 (window-total-size debugger-window)))
88ded8ac
MR
275 (if debugger-will-be-back
276 ;; Restore previous window configuration (Bug#12623).
277 (set-window-configuration window-configuration)
278 (when (and (window-live-p debugger-window)
279 (eq (window-buffer debugger-window) debugger-buffer))
280 (progn
88ded8ac
MR
281 ;; Unshow debugger-buffer.
282 (quit-restore-window debugger-window debugger-bury-or-kill)
283 ;; Restore current buffer (Bug#12502).
284 (set-buffer debugger-old-buffer))))
fa2bcf43
MR
285 ;; Restore previous state of debugger-buffer in case we were
286 ;; in a recursive invocation of the debugger, otherwise just
287 ;; erase the buffer and put it into fundamental mode.
288 (when (buffer-live-p debugger-buffer)
289 (with-current-buffer debugger-buffer
290 (let ((inhibit-read-only t))
291 (erase-buffer)
292 (if (null debugger-previous-state)
293 (fundamental-mode)
294 (insert (nth 1 debugger-previous-state))
295 (funcall (nth 0 debugger-previous-state))))))
385480e5 296 (with-timeout-unsuspend debugger-with-timeout-suspend)
e734f584
RS
297 (set-match-data debugger-outer-match-data)))
298 ;; Put into effect the modified values of these variables
299 ;; in case the user set them with the `e' command.
300 (setq load-read-function debugger-outer-load-read-function)
301 (setq overriding-local-map debugger-outer-overriding-local-map)
302 (setq overriding-terminal-local-map
303 debugger-outer-overriding-terminal-local-map)
304 (setq track-mouse debugger-outer-track-mouse)
305 (setq last-command debugger-outer-last-command)
306 (setq this-command debugger-outer-this-command)
e734f584
RS
307 (setq unread-command-events debugger-outer-unread-command-events)
308 (setq unread-post-input-method-events
309 debugger-outer-unread-post-input-method-events)
310 (setq last-input-event debugger-outer-last-input-event)
311 (setq last-command-event debugger-outer-last-command-event)
312 (setq last-nonmenu-event debugger-outer-last-nonmenu-event)
313 (setq last-event-frame debugger-outer-last-event-frame)
314 (setq standard-input debugger-outer-standard-input)
315 (setq standard-output debugger-outer-standard-output)
316 (setq inhibit-redisplay debugger-outer-inhibit-redisplay)
317 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area)
318 (setq debug-on-next-call debugger-step-after-exit)
319 debugger-value)))
0231f2dc 320\f
63ca439e
GM
321(defun debugger-setup-buffer (debugger-args)
322 "Initialize the `*Backtrace*' buffer for entry to the debugger.
323That buffer should be current already."
324 (setq buffer-read-only nil)
325 (erase-buffer)
c61dc887 326 (set-buffer-multibyte t) ;Why was it nil ? -stef
49c23b70 327 (setq buffer-undo-list t)
63ca439e
GM
328 (let ((standard-output (current-buffer))
329 (print-escape-newlines t)
330 (print-level 8)
331 (print-length 50))
332 (backtrace))
333 (goto-char (point-min))
334 (delete-region (point)
335 (progn
336 (search-forward "\n debug(")
439368ed 337 (forward-line (if (eq (car debugger-args) 'debug)
449808f7 338 2 ; Remove implement-debug-on-entry frame.
439368ed 339 1))
63ca439e
GM
340 (point)))
341 (insert "Debugger entered")
342 ;; lambda is for debug-on-call when a function call is next.
343 ;; debug is for debug-on-entry function called.
e24e27be
SM
344 (pcase (car debugger-args)
345 ((or `lambda `debug)
346 (insert "--entering a function:\n"))
347 ;; Exiting a function.
348 (`exit
349 (insert "--returning value: ")
350 (setq debugger-value (nth 1 debugger-args))
351 (prin1 debugger-value (current-buffer))
352 (insert ?\n)
353 (delete-char 1)
354 (insert ? )
355 (beginning-of-line))
356 ;; Debugger entered for an error.
357 (`error
358 (insert "--Lisp error: ")
359 (prin1 (nth 1 debugger-args) (current-buffer))
360 (insert ?\n))
361 ;; debug-on-call, when the next thing is an eval.
362 (`t
363 (insert "--beginning evaluation of function call form:\n"))
364 ;; User calls debug directly.
365 (_
366 (insert ": ")
367 (prin1 (if (eq (car debugger-args) 'nil)
368 (cdr debugger-args) debugger-args)
369 (current-buffer))
370 (insert ?\n)))
27bde5f0
RS
371 ;; After any frame that uses eval-buffer,
372 ;; insert a line that states the buffer position it's reading at.
373 (save-excursion
245f0222
RS
374 (let ((tem eval-buffer-list))
375 (while (and tem
376 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t))
377 (end-of-line)
378 (insert (format " ; Reading at buffer position %d"
379 ;; This will get the wrong result
380 ;; if there are two nested eval-region calls
381 ;; for the same buffer. That's not a very useful case.
382 (with-current-buffer (car tem)
383 (point))))
384 (pop tem))))
cc1bde62
SS
385 (debugger-make-xrefs))
386
387(defun debugger-make-xrefs (&optional buffer)
dc64809a 388 "Attach cross-references to function names in the `*Backtrace*' buffer."
cc1bde62 389 (interactive "b")
9a529312 390 (with-current-buffer (or buffer (current-buffer))
8e39b2e8
GM
391 (save-excursion
392 (setq buffer (current-buffer))
393 (let ((inhibit-read-only t)
5f66aa05
GM
394 (old-end (point-min)) (new-end (point-min)))
395 ;; If we saved an old backtrace, find the common part
396 ;; between the new and the old.
397 ;; Compare line by line, starting from the end,
398 ;; because that's the part that is likely to be unchanged.
399 (if debugger-previous-backtrace
400 (let (old-start new-start (all-match t))
401 (goto-char (point-max))
402 (with-temp-buffer
403 (insert debugger-previous-backtrace)
404 (while (and all-match (not (bobp)))
405 (setq old-end (point))
406 (forward-line -1)
407 (setq old-start (point))
408 (with-current-buffer buffer
409 (setq new-end (point))
410 (forward-line -1)
411 (setq new-start (point)))
412 (if (not (zerop
413 (let ((case-fold-search nil))
414 (compare-buffer-substrings
415 (current-buffer) old-start old-end
416 buffer new-start new-end))))
417 (setq all-match nil))))
418 ;; Now new-end is the position of the start of the
419 ;; unchanged part in the current buffer, and old-end is
420 ;; the position of that same text in the saved old
421 ;; backtrace. But we must subtract (point-min) since strings are
422 ;; indexed in origin 0.
25c58854 423
5f66aa05
GM
424 ;; Replace the unchanged part of the backtrace
425 ;; with the text from debugger-previous-backtrace,
426 ;; since that already has the proper xrefs.
427 ;; With this optimization, we only need to scan
428 ;; the changed part of the backtrace.
429 (delete-region new-end (point-max))
430 (goto-char (point-max))
431 (insert (substring debugger-previous-backtrace
432 (- old-end (point-min))))
433 ;; Make the unchanged part of the backtrace inaccessible
434 ;; so it won't be scanned.
435 (narrow-to-region (point-min) new-end)))
a1506d29 436
5f66aa05
GM
437 ;; Scan the new part of the backtrace, inserting xrefs.
438 (goto-char (point-min))
439 (while (progn
440 (goto-char (+ (point) 2))
441 (skip-syntax-forward "^w_")
442 (not (eobp)))
443 (let* ((beg (point))
444 (end (progn (skip-syntax-forward "w_") (point)))
445 (sym (intern-soft (buffer-substring-no-properties
446 beg end)))
447 (file (and sym (symbol-file sym 'defun))))
448 (when file
449 (goto-char beg)
450 ;; help-xref-button needs to operate on something matched
451 ;; by a regexp, so set that up for it.
452 (re-search-forward "\\(\\sw\\|\\s_\\)+")
453 (help-xref-button 0 'help-function-def sym file)))
454 (forward-line 1))
455 (widen))
8e39b2e8 456 (setq debugger-previous-backtrace (buffer-string)))))
63ca439e 457\f
0231f2dc
JB
458(defun debugger-step-through ()
459 "Proceed, stepping through subexpressions of this expression.
460Enter another debugger on next entry to eval, apply or funcall."
461 (interactive)
462 (setq debugger-step-after-exit t)
9b54d453 463 (setq debugger-jumping-flag t)
9b530428 464 (setq debugger-will-be-back t)
9b54d453 465 (add-hook 'post-command-hook 'debugger-reenable)
0231f2dc
JB
466 (message "Proceeding, will debug on next eval or call.")
467 (exit-recursive-edit))
468
469(defun debugger-continue ()
470 "Continue, evaluating this expression without stopping."
471 (interactive)
094e0928
GM
472 (unless debugger-may-continue
473 (error "Cannot continue"))
0231f2dc 474 (message "Continuing.")
9b530428
SM
475 (save-excursion
476 ;; Check to see if we've flagged some frame for debug-on-exit, in which
477 ;; case we'll probably come back to the debugger soon.
478 (goto-char (point-min))
479 (if (re-search-forward "^\\* " nil t)
480 (setq debugger-will-be-back t)))
0231f2dc
JB
481 (exit-recursive-edit))
482
483(defun debugger-return-value (val)
484 "Continue, specifying value to return.
485This is only useful when the value returned from the debugger
486will be used, such as in a debug on exit from a frame."
487 (interactive "XReturn value (evaluated): ")
e24e27be
SM
488 (when (memq (car debugger-args) '(t lambda error debug))
489 (error "Cannot return a value %s"
490 (if (eq (car debugger-args) 'error)
491 "from an error" "at function entrance")))
0231f2dc
JB
492 (setq debugger-value val)
493 (princ "Returning " t)
494 (prin1 debugger-value)
9b530428
SM
495 (save-excursion
496 ;; Check to see if we've flagged some frame for debug-on-exit, in which
497 ;; case we'll probably come back to the debugger soon.
498 (goto-char (point-min))
499 (if (re-search-forward "^\\* " nil t)
500 (setq debugger-will-be-back t)))
0231f2dc
JB
501 (exit-recursive-edit))
502
503(defun debugger-jump ()
504 "Continue to exit from this frame, with all debug-on-entry suspended."
505 (interactive)
27bde5f0 506 (debugger-frame)
b6b77a90
LK
507 (setq debugger-jumping-flag t)
508 (add-hook 'post-command-hook 'debugger-reenable)
0231f2dc 509 (message "Continuing through this frame")
9b530428 510 (setq debugger-will-be-back t)
0231f2dc
JB
511 (exit-recursive-edit))
512
513(defun debugger-reenable ()
b6b77a90
LK
514 "Turn all debug-on-entry functions back on.
515This function is put on `post-command-hook' by `debugger-jump' and
516removes itself from that hook."
517 (setq debugger-jumping-flag nil)
518 (remove-hook 'post-command-hook 'debugger-reenable))
0231f2dc
JB
519
520(defun debugger-frame-number ()
521 "Return number of frames in backtrace before the one point points at."
522 (save-excursion
523 (beginning-of-line)
524 (let ((opoint (point))
525 (count 0))
27bde5f0
RS
526 (while (not (eq (cadr (backtrace-frame count)) 'debug))
527 (setq count (1+ count)))
449808f7
LK
528 ;; Skip implement-debug-on-entry frame.
529 (when (eq 'implement-debug-on-entry (cadr (backtrace-frame (1+ count))))
439368ed 530 (setq count (1+ count)))
0231f2dc 531 (goto-char (point-min))
439368ed
LK
532 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
533 (goto-char (match-end 0))
534 (forward-sexp 1))
0231f2dc
JB
535 (forward-line 1)
536 (while (progn
537 (forward-char 2)
538 (if (= (following-char) ?\()
539 (forward-sexp 1)
540 (forward-sexp 2))
541 (forward-line 1)
542 (<= (point) opoint))
65fe45f2
RS
543 (if (looking-at " *;;;")
544 (forward-line 1))
0231f2dc
JB
545 (setq count (1+ count)))
546 count)))
547
0231f2dc
JB
548(defun debugger-frame ()
549 "Request entry to debugger when this frame exits.
550Applies to the frame whose line point is on in the backtrace."
551 (interactive)
fa5b1b57
RS
552 (save-excursion
553 (beginning-of-line)
554 (if (looking-at " *;;;\\|[a-z]")
555 (error "This line is not a function call")))
0231f2dc 556 (beginning-of-line)
27bde5f0 557 (backtrace-debug (debugger-frame-number) t)
0231f2dc 558 (if (= (following-char) ? )
9b530428 559 (let ((inhibit-read-only t))
0231f2dc
JB
560 (delete-char 1)
561 (insert ?*)))
562 (beginning-of-line))
563
564(defun debugger-frame-clear ()
c519f68f 565 "Do not enter debugger when this frame exits.
0231f2dc
JB
566Applies to the frame whose line point is on in the backtrace."
567 (interactive)
fa5b1b57
RS
568 (save-excursion
569 (beginning-of-line)
570 (if (looking-at " *;;;\\|[a-z]")
571 (error "This line is not a function call")))
0231f2dc 572 (beginning-of-line)
27bde5f0 573 (backtrace-debug (debugger-frame-number) nil)
0231f2dc 574 (if (= (following-char) ?*)
9b530428 575 (let ((inhibit-read-only t))
0231f2dc
JB
576 (delete-char 1)
577 (insert ? )))
578 (beginning-of-line))
579
8c1cd093
KH
580(defmacro debugger-env-macro (&rest body)
581 "Run BODY in original environment."
f291fe60 582 (declare (indent 0))
6c2599ed
SS
583 `(save-excursion
584 (if (null (buffer-name debugger-old-buffer))
585 ;; old buffer deleted
586 (setq debugger-old-buffer (current-buffer)))
587 (set-buffer debugger-old-buffer)
588 (let ((load-read-function debugger-outer-load-read-function)
589 (overriding-terminal-local-map
590 debugger-outer-overriding-terminal-local-map)
591 (overriding-local-map debugger-outer-overriding-local-map)
592 (track-mouse debugger-outer-track-mouse)
593 (last-command debugger-outer-last-command)
594 (this-command debugger-outer-this-command)
6c2599ed
SS
595 (unread-command-events debugger-outer-unread-command-events)
596 (unread-post-input-method-events
597 debugger-outer-unread-post-input-method-events)
598 (last-input-event debugger-outer-last-input-event)
599 (last-command-event debugger-outer-last-command-event)
600 (last-nonmenu-event debugger-outer-last-nonmenu-event)
601 (last-event-frame debugger-outer-last-event-frame)
602 (standard-input debugger-outer-standard-input)
603 (standard-output debugger-outer-standard-output)
604 (inhibit-redisplay debugger-outer-inhibit-redisplay)
605 (cursor-in-echo-area debugger-outer-cursor-in-echo-area))
606 (set-match-data debugger-outer-match-data)
12cf32ce 607 (prog1
bfeae2cf 608 (progn ,@body)
6c2599ed
SS
609 (setq debugger-outer-match-data (match-data))
610 (setq debugger-outer-load-read-function load-read-function)
611 (setq debugger-outer-overriding-terminal-local-map
612 overriding-terminal-local-map)
613 (setq debugger-outer-overriding-local-map overriding-local-map)
614 (setq debugger-outer-track-mouse track-mouse)
615 (setq debugger-outer-last-command last-command)
616 (setq debugger-outer-this-command this-command)
6c2599ed
SS
617 (setq debugger-outer-unread-command-events unread-command-events)
618 (setq debugger-outer-unread-post-input-method-events
619 unread-post-input-method-events)
620 (setq debugger-outer-last-input-event last-input-event)
621 (setq debugger-outer-last-command-event last-command-event)
622 (setq debugger-outer-last-nonmenu-event last-nonmenu-event)
623 (setq debugger-outer-last-event-frame last-event-frame)
624 (setq debugger-outer-standard-input standard-input)
625 (setq debugger-outer-standard-output standard-output)
626 (setq debugger-outer-inhibit-redisplay inhibit-redisplay)
627 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area)
628 ))))
8c1cd093 629
0231f2dc 630(defun debugger-eval-expression (exp)
8782c06b
RS
631 "Eval an expression, in an environment like that outside the debugger."
632 (interactive
633 (list (read-from-minibuffer "Eval: "
634 nil read-expression-map t
635 'read-expression-history)))
8c1cd093 636 (debugger-env-macro (eval-expression exp)))
0231f2dc 637\f
78d54dee 638(defvar debugger-mode-map
b2371818
DN
639 (let ((map (make-keymap))
640 (menu-map (make-sparse-keymap)))
78d54dee
SM
641 (set-keymap-parent map button-buffer-map)
642 (suppress-keymap map)
643 (define-key map "-" 'negative-argument)
644 (define-key map "b" 'debugger-frame)
645 (define-key map "c" 'debugger-continue)
646 (define-key map "j" 'debugger-jump)
647 (define-key map "r" 'debugger-return-value)
648 (define-key map "u" 'debugger-frame-clear)
649 (define-key map "d" 'debugger-step-through)
650 (define-key map "l" 'debugger-list-functions)
651 (define-key map "h" 'describe-mode)
652 (define-key map "q" 'top-level)
653 (define-key map "e" 'debugger-eval-expression)
654 (define-key map " " 'next-line)
655 (define-key map "R" 'debugger-record-expression)
245f0222 656 (define-key map "\C-m" 'debug-help-follow)
78d54dee 657 (define-key map [mouse-2] 'push-button)
b2371818
DN
658 (define-key map [menu-bar debugger] (cons "Debugger" menu-map))
659 (define-key menu-map [deb-top]
660 '(menu-item "Quit" top-level
f6ec6415 661 :help "Quit debugging and return to top level"))
b2371818
DN
662 (define-key menu-map [deb-s0] '("--"))
663 (define-key menu-map [deb-descr]
664 '(menu-item "Describe Debugger Mode" describe-mode
665 :help "Display documentation for debugger-mode"))
666 (define-key menu-map [deb-hfol]
667 '(menu-item "Help Follow" debug-help-follow
668 :help "Follow cross-reference"))
669 (define-key menu-map [deb-nxt]
670 '(menu-item "Next Line" next-line
671 :help "Move cursor down"))
672 (define-key menu-map [deb-s1] '("--"))
673 (define-key menu-map [deb-lfunc]
674 '(menu-item "List debug on entry functions" debugger-list-functions
675 :help "Display a list of all the functions now set to debug on entry"))
676 (define-key menu-map [deb-fclear]
677 '(menu-item "Cancel debug frame" debugger-frame-clear
678 :help "Do not enter debugger when this frame exits"))
679 (define-key menu-map [deb-frame]
680 '(menu-item "Debug frame" debugger-frame
681 :help "Request entry to debugger when this frame exits"))
682 (define-key menu-map [deb-s2] '("--"))
683 (define-key menu-map [deb-ret]
684 '(menu-item "Return value..." debugger-return-value
685 :help "Continue, specifying value to return."))
686 (define-key menu-map [deb-rec]
687 '(menu-item "Display and Record Expression" debugger-record-expression
688 :help "Display a variable's value and record it in `*Backtrace-record*' buffer"))
689 (define-key menu-map [deb-eval]
690 '(menu-item "Eval Expression..." debugger-eval-expression
691 :help "Eval an expression, in an environment like that outside the debugger"))
692 (define-key menu-map [deb-jump]
693 '(menu-item "Jump" debugger-jump
694 :help "Continue to exit from this frame, with all debug-on-entry suspended"))
695 (define-key menu-map [deb-cont]
696 '(menu-item "Continue" debugger-continue
697 :help "Continue, evaluating this expression without stopping"))
698 (define-key menu-map [deb-step]
699 '(menu-item "Step through" debugger-step-through
700 :help "Proceed, stepping through subexpressions of this expression"))
78d54dee 701 map))
028d38a2
RS
702
703(put 'debugger-mode 'mode-class 'special)
704
705(defun debugger-mode ()
706 "Mode for backtrace buffers, selected in debugger.
707\\<debugger-mode-map>
708A line starts with `*' if exiting that frame will call the debugger.
709Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
710
711When in debugger due to frame being exited,
712use the \\[debugger-return-value] command to override the value
713being returned from that frame.
714
715Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
716which functions will enter the debugger when called.
717
718Complete list of commands:
719\\{debugger-mode-map}"
720 (kill-all-local-variables)
721 (setq major-mode 'debugger-mode)
722 (setq mode-name "Debugger")
723 (setq truncate-lines t)
724 (set-syntax-table emacs-lisp-mode-syntax-table)
725 (use-local-map debugger-mode-map)
726 (run-mode-hooks 'debugger-mode-hook))
245f0222 727\f
666b9413 728(defcustom debugger-record-buffer "*Debugger-record*"
cb711556 729 "Buffer name for expression values, for \\[debugger-record-expression]."
666b9413 730 :type 'string
cd32a7ba
DN
731 :group 'debugger
732 :version "20.3")
8c1cd093
KH
733
734(defun debugger-record-expression (exp)
735 "Display a variable's value and record it in `*Backtrace-record*' buffer."
736 (interactive
737 (list (read-from-minibuffer
738 "Record Eval: "
739 nil
740 read-expression-map t
741 'read-expression-history)))
742 (let* ((buffer (get-buffer-create debugger-record-buffer))
743 (standard-output buffer))
744 (princ (format "Debugger Eval (%s): " exp))
745 (princ (debugger-eval-expression exp))
746 (terpri))
747
748 (with-current-buffer (get-buffer debugger-record-buffer)
8e71c318 749 (message "%s"
be940bc5
SM
750 (buffer-substring (line-beginning-position 0)
751 (line-end-position 0)))))
0231f2dc 752
f63b822e
GM
753(declare-function help-xref-interned "help-mode" (symbol))
754
245f0222
RS
755(defun debug-help-follow (&optional pos)
756 "Follow cross-reference at POS, defaulting to point.
757
758For the cross-reference format, see `help-make-xrefs'."
759 (interactive "d")
760 (require 'help-mode)
a5f1e154
LT
761 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
762 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
763 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
245f0222
RS
764 (unless pos
765 (setq pos (point)))
766 (unless (push-button pos)
767 ;; check if the symbol under point is a function or variable
768 (let ((sym
769 (intern
770 (save-excursion
771 (goto-char pos) (skip-syntax-backward "w_")
772 (buffer-substring (point)
773 (progn (skip-syntax-forward "w_")
774 (point)))))))
775 (when (or (boundp sym) (fboundp sym) (facep sym))
1fe3c8f5 776 (help-xref-interned sym)))))
0231f2dc 777\f
449808f7
LK
778;; When you change this, you may also need to change the number of
779;; frames that the debugger skips.
780(defun implement-debug-on-entry ()
781 "Conditionally call the debugger.
782A call to this function is inserted by `debug-on-entry' to cause
783functions to break on entry."
784 (if (or inhibit-debug-on-entry debugger-jumping-flag)
785 nil
786 (funcall debugger 'debug)))
787
ce5ce46d
LK
788(defun debugger-special-form-p (symbol)
789 "Return whether SYMBOL is a special form."
790 (and (fboundp symbol)
791 (subrp (symbol-function symbol))
792 (eq (cdr (subr-arity (symbol-function symbol))) 'unevalled)))
793
0231f2dc
JB
794;;;###autoload
795(defun debug-on-entry (function)
796 "Request FUNCTION to invoke debugger each time it is called.
8ac3941d 797
8e71c318 798When called interactively, prompt for FUNCTION in the minibuffer.
8ac3941d
LK
799
800This works by modifying the definition of FUNCTION. If you tell the
801debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
802normal function or a macro written in Lisp, you can also step through
803its execution. FUNCTION can also be a primitive that is not a special
804form, in which case stepping is not possible. Break-on-entry for
805primitive functions only works when that function is called from Lisp.
806
0231f2dc 807Use \\[cancel-debug-on-entry] to cancel the effect of this command.
2512acba 808Redefining FUNCTION also cancels it."
ce5ce46d
LK
809 (interactive
810 (let ((fn (function-called-at-point)) val)
811 (when (debugger-special-form-p fn)
812 (setq fn nil))
3731a850 813 (setq val (completing-read
ce5ce46d
LK
814 (if fn
815 (format "Debug on entry to function (default %s): " fn)
816 "Debug on entry to function: ")
817 obarray
818 #'(lambda (symbol)
819 (and (fboundp symbol)
820 (not (debugger-special-form-p symbol))))
821 t nil nil (symbol-name fn)))
822 (list (if (equal val "") fn (intern val)))))
4eb61348 823 ;; FIXME: Use advice.el.
ce5ce46d 824 (when (debugger-special-form-p function)
d7029908 825 (error "Function %s is a special form" function))
8e71c318 826 (if (or (symbolp (symbol-function function))
7473b6ad 827 (subrp (symbol-function function)))
d7029908
LK
828 ;; The function is built-in or aliased to another function.
829 ;; Create a wrapper in which we can add the debug call.
f9e899b6 830 (fset function `(lambda (&rest debug-on-entry-args)
7473b6ad 831 ,(interactive-form (symbol-function function))
8e71c318 832 (apply ',(symbol-function function)
d7029908 833 debug-on-entry-args)))
7abaf5cc 834 (when (autoloadp (symbol-function function))
d7029908 835 ;; The function is autoloaded. Load its real definition.
7abaf5cc 836 (autoload-do-load (symbol-function function) function))
d7029908
LK
837 (when (or (not (consp (symbol-function function)))
838 (and (eq (car (symbol-function function)) 'macro)
839 (not (consp (cdr (symbol-function function))))))
840 ;; The function is byte-compiled. Create a wrapper in which
841 ;; we can add the debug call.
842 (debug-convert-byte-code function)))
843 (unless (consp (symbol-function function))
844 (error "Definition of %s is not a list" function))
449808f7 845 (fset function (debug-on-entry-1 function t))
d7029908
LK
846 (unless (memq function debug-function-list)
847 (push function debug-function-list))
0231f2dc
JB
848 function)
849
850;;;###autoload
851(defun cancel-debug-on-entry (&optional function)
852 "Undo effect of \\[debug-on-entry] on FUNCTION.
dd72a03a 853If FUNCTION is nil, cancel debug-on-entry for all functions.
8e71c318
LT
854When called interactively, prompt for FUNCTION in the minibuffer.
855To specify a nil argument interactively, exit with an empty minibuffer."
10a4c11f
JB
856 (interactive
857 (list (let ((name
922a9de3 858 (completing-read
5b76833f 859 "Cancel debug on entry to function (default all functions): "
922a9de3 860 (mapcar 'symbol-name debug-function-list) nil t)))
dd72a03a
LK
861 (when name
862 (unless (string= name "")
863 (intern name))))))
864 (if (and function
865 (not (string= function ""))) ; Pre 22.1 compatibility test.
0231f2dc 866 (progn
d7029908 867 (let ((defn (debug-on-entry-1 function nil)))
7473b6ad 868 (condition-case nil
d7029908
LK
869 (when (and (equal (nth 1 defn) '(&rest debug-on-entry-args))
870 (eq (car (nth 3 defn)) 'apply))
871 ;; `defn' is a wrapper introduced in debug-on-entry.
872 ;; Get rid of it since we don't need it any more.
873 (setq defn (nth 1 (nth 1 (nth 3 defn)))))
7473b6ad 874 (error nil))
d7029908 875 (fset function defn))
0231f2dc
JB
876 (setq debug-function-list (delq function debug-function-list))
877 function)
878 (message "Cancelling debug-on-entry for all functions")
879 (mapcar 'cancel-debug-on-entry debug-function-list)))
880
4eb61348
SM
881(defun debug-arglist (definition)
882 ;; FIXME: copied from ad-arglist.
883 "Return the argument list of DEFINITION."
884 (require 'help-fns)
885 (help-function-arglist definition 'preserve-names))
886
0231f2dc 887(defun debug-convert-byte-code (function)
d7029908
LK
888 (let* ((defn (symbol-function function))
889 (macro (eq (car-safe defn) 'macro)))
890 (when macro (setq defn (cdr defn)))
4eb61348
SM
891 (when (byte-code-function-p defn)
892 (let* ((args (debug-arglist defn))
d7029908 893 (body
4eb61348
SM
894 `((,(if (memq '&rest args) #'apply #'funcall)
895 ,defn
896 ,@(remq '&rest (remq '&optional args))))))
897 (if (> (length defn) 5)
a8406c20
SM
898 ;; The mere presence of field 5 is sufficient to make
899 ;; it interactive.
4eb61348 900 (push `(interactive ,(aref defn 5)) body))
a8406c20 901 (if (and (> (length defn) 4) (aref defn 4))
d7029908
LK
902 ;; Use `documentation' here, to get the actual string,
903 ;; in case the compiled function has a reference
904 ;; to the .elc file.
905 (setq body (cons (documentation function) body)))
4eb61348 906 (setq defn `(closure (t) ,args ,@body)))
d7029908
LK
907 (when macro (setq defn (cons 'macro defn)))
908 (fset function defn))))
0231f2dc 909
449808f7
LK
910(defun debug-on-entry-1 (function flag)
911 (let* ((defn (symbol-function function))
912 (tail defn))
8e71c318 913 (when (eq (car-safe tail) 'macro)
d7029908 914 (setq tail (cdr tail)))
4eb61348 915 (if (not (memq (car-safe tail) '(closure lambda)))
d7029908
LK
916 ;; Only signal an error when we try to set debug-on-entry.
917 ;; When we try to clear debug-on-entry, we are now done.
918 (when flag
919 (error "%s is not a user-defined Lisp function" function))
4eb61348 920 (if (eq (car tail) 'closure) (setq tail (cdr tail)))
d7029908 921 (setq tail (cdr tail))
a29cf450
SM
922 ;; Skip the docstring.
923 (when (and (stringp (cadr tail)) (cddr tail))
924 (setq tail (cdr tail)))
925 ;; Skip the interactive form.
926 (when (eq 'interactive (car-safe (cadr tail)))
927 (setq tail (cdr tail)))
449808f7 928 (unless (eq flag (equal (cadr tail) '(implement-debug-on-entry)))
a29cf450 929 ;; Add/remove debug statement as needed.
4eb61348
SM
930 (setcdr tail (if flag
931 (cons '(implement-debug-on-entry) (cdr tail))
932 (cddr tail)))))
d7029908 933 defn))
0231f2dc
JB
934
935(defun debugger-list-functions ()
936 "Display a list of all the functions now set to debug on entry."
937 (interactive)
78d54dee 938 (require 'help-mode)
32226619
JB
939 (help-setup-xref '(debugger-list-functions)
940 (called-interactively-p 'interactive))
78d54dee
SM
941 (with-output-to-temp-buffer (help-buffer)
942 (with-current-buffer standard-output
943 (if (null debug-function-list)
944 (princ "No debug-on-entry functions now\n")
945 (princ "Functions set to debug on entry:\n\n")
946 (dolist (fun debug-function-list)
947 (make-text-button (point) (progn (prin1 fun) (point))
948 'type 'help-function
949 'help-args (list fun))
950 (terpri))
951 (terpri)
952 (princ "Note: if you have redefined a function, then it may no longer\n")
953 (princ "be set to debug on entry, even if it is in the list.")))))
c0274f38 954
896546cd
RS
955(provide 'debug)
956
c0274f38 957;;; debug.el ends here