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