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