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