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