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