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