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