1 ;;; debug.el --- debuggers and related commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
7 ;; Keywords: lisp, tools, maint
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; This is a major mode documented in the Emacs Lisp manual.
32 (defgroup debugger nil
33 "Debuggers and related commands for Emacs."
37 (defcustom debugger-mode-hook nil
38 "*Hooks run when `debugger-mode' is turned on."
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."
52 (defvar debug-function-list nil
53 "List of functions currently set for debug on entry.")
55 (defvar debugger-step-after-exit nil
56 "Non-nil means \"single-step\" after the debugger exits.")
58 (defvar debugger-value nil
59 "This is the value for the debugger to return, when it returns.")
61 (defvar debugger-old-buffer nil
62 "This is the buffer that was current when the debugger was entered.")
64 (defvar debugger-previous-backtrace nil
65 "The contents of the previous backtrace (including text properties).
66 This is to optimize `debugger-make-xrefs'.")
68 (defvar debugger-outer-match-data
)
69 (defvar debugger-outer-load-read-function
)
70 (defvar debugger-outer-overriding-local-map
)
71 (defvar debugger-outer-overriding-terminal-local-map
)
72 (defvar debugger-outer-track-mouse
)
73 (defvar debugger-outer-last-command
)
74 (defvar debugger-outer-this-command
)
75 ;; unread-command-char is obsolete,
76 ;; but we still save and restore it
77 ;; in case some user program still tries to set it.
78 (defvar debugger-outer-unread-command-char
)
79 (defvar debugger-outer-unread-command-events
)
80 (defvar debugger-outer-unread-post-input-method-events
)
81 (defvar debugger-outer-last-input-event
)
82 (defvar debugger-outer-last-command-event
)
83 (defvar debugger-outer-last-nonmenu-event
)
84 (defvar debugger-outer-last-event-frame
)
85 (defvar debugger-outer-standard-input
)
86 (defvar debugger-outer-standard-output
)
87 (defvar debugger-outer-inhibit-redisplay
)
88 (defvar debugger-outer-cursor-in-echo-area
)
89 (defvar debugger-will-be-back nil
90 "Non-nil if we expect to get back in the debugger soon.")
92 (defvar inhibit-debug-on-entry nil
93 "Non-nil means that debug-on-entry is disabled.")
95 (defvar debugger-jumping-flag nil
96 "Non-nil means that debug-on-entry is disabled.
97 This variable is used by `debugger-jump', `debugger-step-through',
98 and `debugger-reenable' to temporarily disable debug-on-entry.")
100 (defvar inhibit-trace
) ;Not yet implemented.
103 (setq debugger
'debug
)
105 (defun debug (&rest debugger-args
)
106 "Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'.
107 Arguments are mainly for use when this is called from the internals
110 You may call with no args, or you may pass nil as the first arg and
111 any other args you like. In that case, the list of args after the
112 first will be printed into the backtrace buffer."
114 (if inhibit-redisplay
115 ;; Don't really try to enter debugger within an eval from redisplay.
117 (unless noninteractive
118 (message "Entering debugger..."))
122 (debugger-buffer (let ((default-major-mode 'fundamental-mode
))
123 (get-buffer-create "*Backtrace*")))
124 (debugger-old-buffer (current-buffer))
125 (debugger-step-after-exit nil
)
126 (debugger-will-be-back nil
)
127 ;; Don't keep reading from an executing kbd macro!
128 (executing-kbd-macro nil
)
129 ;; Save the outer values of these vars for the `e' command
130 ;; before we replace the values.
131 (debugger-outer-match-data (match-data))
132 (debugger-outer-load-read-function load-read-function
)
133 (debugger-outer-overriding-local-map overriding-local-map
)
134 (debugger-outer-overriding-terminal-local-map
135 overriding-terminal-local-map
)
136 (debugger-outer-track-mouse track-mouse
)
137 (debugger-outer-last-command last-command
)
138 (debugger-outer-this-command this-command
)
139 (debugger-outer-unread-command-char
140 (with-no-warnings unread-command-char
))
141 (debugger-outer-unread-command-events unread-command-events
)
142 (debugger-outer-unread-post-input-method-events
143 unread-post-input-method-events
)
144 (debugger-outer-last-input-event last-input-event
)
145 (debugger-outer-last-command-event last-command-event
)
146 (debugger-outer-last-nonmenu-event last-nonmenu-event
)
147 (debugger-outer-last-event-frame last-event-frame
)
148 (debugger-outer-standard-input standard-input
)
149 (debugger-outer-standard-output standard-output
)
150 (debugger-outer-inhibit-redisplay inhibit-redisplay
)
151 (debugger-outer-cursor-in-echo-area cursor-in-echo-area
)
152 (debugger-with-timeout-suspend (with-timeout-suspend)))
153 ;; Set this instead of binding it, so that `q'
154 ;; will not restore it.
155 (setq overriding-terminal-local-map nil
)
156 ;; Don't let these magic variables affect the debugger itself.
157 (let ((last-command nil
) this-command track-mouse
159 (inhibit-debug-on-entry t
)
160 unread-command-events
161 unread-post-input-method-events
162 last-input-event last-command-event last-nonmenu-event
166 ;; If we are inside a minibuffer, allow nesting
167 ;; so that we don't get an error from the `e' command.
168 (enable-recursive-minibuffers
169 (or enable-recursive-minibuffers
(> (minibuffer-depth) 0)))
170 (standard-input t
) (standard-output t
)
172 (cursor-in-echo-area nil
))
175 (save-window-excursion
177 (setq unread-command-char -
1))
178 (when (eq (car debugger-args
) 'debug
)
179 ;; Skip the frames for backtrace-debug, byte-code,
180 ;; and implement-debug-on-entry.
181 (backtrace-debug 4 t
)
182 ;; Place an extra debug-on-exit for macro's.
183 (when (eq 'lambda
(car-safe (cadr (backtrace-frame 4))))
184 (backtrace-debug 5 t
)))
185 (pop-to-buffer debugger-buffer
)
187 (debugger-setup-buffer debugger-args
)
189 ;; If the backtrace is long, save the beginning
190 ;; and the end, but discard the middle.
191 (when (> (count-lines (point-min) (point-max))
192 debugger-batch-max-lines
)
193 (goto-char (point-min))
194 (forward-line (/ 2 debugger-batch-max-lines
))
195 (let ((middlestart (point)))
196 (goto-char (point-max))
197 (forward-line (- (/ 2 debugger-batch-max-lines
)
198 debugger-batch-max-lines
))
199 (delete-region middlestart
(point)))
201 (goto-char (point-min))
202 (message "%s" (buffer-string))
205 (let ((standard-output nil
)
206 (buffer-read-only t
))
208 ;; Make sure we unbind buffer-read-only in the right buffer.
211 ;; Kill or at least neuter the backtrace buffer, so that users
212 ;; don't try to execute debugger commands in an invalid context.
213 (if (get-buffer-window debugger-buffer
0)
214 ;; Still visible despite the save-window-excursion? Maybe it
215 ;; it's in a pop-up frame. It would be annoying to delete and
216 ;; recreate it every time the debugger stops, so instead we'll
217 ;; erase it (and maybe hide it) but keep it alive.
218 (with-current-buffer debugger-buffer
221 (with-selected-window (get-buffer-window debugger-buffer
0)
222 (when (and (window-dedicated-p (selected-window))
223 (not debugger-will-be-back
))
224 ;; If the window is not dedicated, burying the buffer
225 ;; will mean that the frame created for it is left
226 ;; around showing some random buffer, and next time we
227 ;; pop to the debugger buffer we'll create yet
229 ;; If debugger-will-be-back is non-nil, the frame
230 ;; would need to be de-iconified anyway immediately
231 ;; after when we re-enter the debugger, so iconifying it
232 ;; here would cause flashing.
233 ;; Drew Adams is not happy with this: he wants to frame
234 ;; to be left at the top-level, still working on how
237 (kill-buffer debugger-buffer
))
238 (with-timeout-unsuspend debugger-with-timeout-suspend
)
239 (set-match-data debugger-outer-match-data
)))
240 ;; Put into effect the modified values of these variables
241 ;; in case the user set them with the `e' command.
242 (setq load-read-function debugger-outer-load-read-function
)
243 (setq overriding-local-map debugger-outer-overriding-local-map
)
244 (setq overriding-terminal-local-map
245 debugger-outer-overriding-terminal-local-map
)
246 (setq track-mouse debugger-outer-track-mouse
)
247 (setq last-command debugger-outer-last-command
)
248 (setq this-command debugger-outer-this-command
)
250 (setq unread-command-char debugger-outer-unread-command-char
))
251 (setq unread-command-events debugger-outer-unread-command-events
)
252 (setq unread-post-input-method-events
253 debugger-outer-unread-post-input-method-events
)
254 (setq last-input-event debugger-outer-last-input-event
)
255 (setq last-command-event debugger-outer-last-command-event
)
256 (setq last-nonmenu-event debugger-outer-last-nonmenu-event
)
257 (setq last-event-frame debugger-outer-last-event-frame
)
258 (setq standard-input debugger-outer-standard-input
)
259 (setq standard-output debugger-outer-standard-output
)
260 (setq inhibit-redisplay debugger-outer-inhibit-redisplay
)
261 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area
)
262 (setq debug-on-next-call debugger-step-after-exit
)
265 (defun debugger-setup-buffer (debugger-args)
266 "Initialize the `*Backtrace*' buffer for entry to the debugger.
267 That buffer should be current already."
268 (setq buffer-read-only nil
)
270 (set-buffer-multibyte nil
)
271 (setq buffer-undo-list t
)
272 (let ((standard-output (current-buffer))
273 (print-escape-newlines t
)
277 (goto-char (point-min))
278 (delete-region (point)
280 (search-forward "\n debug(")
281 (forward-line (if (eq (car debugger-args
) 'debug
)
282 2 ; Remove implement-debug-on-entry frame.
285 (insert "Debugger entered")
286 ;; lambda is for debug-on-call when a function call is next.
287 ;; debug is for debug-on-entry function called.
288 (cond ((memq (car debugger-args
) '(lambda debug
))
289 (insert "--entering a function:\n"))
290 ;; Exiting a function.
291 ((eq (car debugger-args
) 'exit
)
292 (insert "--returning value: ")
293 (setq debugger-value
(nth 1 debugger-args
))
294 (prin1 debugger-value
(current-buffer))
299 ;; Debugger entered for an error.
300 ((eq (car debugger-args
) 'error
)
301 (insert "--Lisp error: ")
302 (prin1 (nth 1 debugger-args
) (current-buffer))
304 ;; debug-on-call, when the next thing is an eval.
305 ((eq (car debugger-args
) t
)
306 (insert "--beginning evaluation of function call form:\n"))
307 ;; User calls debug directly.
310 (prin1 (if (eq (car debugger-args
) 'nil
)
311 (cdr debugger-args
) debugger-args
)
314 ;; After any frame that uses eval-buffer,
315 ;; insert a line that states the buffer position it's reading at.
317 (let ((tem eval-buffer-list
))
319 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t
))
321 (insert (format " ; Reading at buffer position %d"
322 ;; This will get the wrong result
323 ;; if there are two nested eval-region calls
324 ;; for the same buffer. That's not a very useful case.
325 (with-current-buffer (car tem
)
328 (debugger-make-xrefs))
330 (defun debugger-make-xrefs (&optional buffer
)
331 "Attach cross-references to function names in the `*Backtrace*' buffer."
334 (set-buffer (or buffer
(current-buffer)))
335 (setq buffer
(current-buffer))
336 (let ((inhibit-read-only t
)
337 (old-end (point-min)) (new-end (point-min)))
338 ;; If we saved an old backtrace, find the common part
339 ;; between the new and the old.
340 ;; Compare line by line, starting from the end,
341 ;; because that's the part that is likely to be unchanged.
342 (if debugger-previous-backtrace
343 (let (old-start new-start
(all-match t
))
344 (goto-char (point-max))
346 (insert debugger-previous-backtrace
)
347 (while (and all-match
(not (bobp)))
348 (setq old-end
(point))
350 (setq old-start
(point))
351 (with-current-buffer buffer
352 (setq new-end
(point))
354 (setq new-start
(point)))
356 (let ((case-fold-search nil
))
357 (compare-buffer-substrings
358 (current-buffer) old-start old-end
359 buffer new-start new-end
))))
360 (setq all-match nil
))))
361 ;; Now new-end is the position of the start of the
362 ;; unchanged part in the current buffer, and old-end is
363 ;; the position of that same text in the saved old
364 ;; backtrace. But we must subtract (point-min) since strings are
365 ;; indexed in origin 0.
367 ;; Replace the unchanged part of the backtrace
368 ;; with the text from debugger-previous-backtrace,
369 ;; since that already has the proper xrefs.
370 ;; With this optimization, we only need to scan
371 ;; the changed part of the backtrace.
372 (delete-region new-end
(point-max))
373 (goto-char (point-max))
374 (insert (substring debugger-previous-backtrace
375 (- old-end
(point-min))))
376 ;; Make the unchanged part of the backtrace inaccessible
377 ;; so it won't be scanned.
378 (narrow-to-region (point-min) new-end
)))
380 ;; Scan the new part of the backtrace, inserting xrefs.
381 (goto-char (point-min))
383 (goto-char (+ (point) 2))
384 (skip-syntax-forward "^w_")
387 (end (progn (skip-syntax-forward "w_") (point)))
388 (sym (intern-soft (buffer-substring-no-properties
390 (file (and sym
(symbol-file sym
'defun
))))
393 ;; help-xref-button needs to operate on something matched
394 ;; by a regexp, so set that up for it.
395 (re-search-forward "\\(\\sw\\|\\s_\\)+")
396 (help-xref-button 0 'help-function-def sym file
)))
399 (setq debugger-previous-backtrace
(buffer-string))))
401 (defun debugger-step-through ()
402 "Proceed, stepping through subexpressions of this expression.
403 Enter another debugger on next entry to eval, apply or funcall."
405 (setq debugger-step-after-exit t
)
406 (setq debugger-jumping-flag t
)
407 (setq debugger-will-be-back t
)
408 (add-hook 'post-command-hook
'debugger-reenable
)
409 (message "Proceeding, will debug on next eval or call.")
410 (exit-recursive-edit))
412 (defun debugger-continue ()
413 "Continue, evaluating this expression without stopping."
415 (unless debugger-may-continue
416 (error "Cannot continue"))
417 (message "Continuing.")
419 ;; Check to see if we've flagged some frame for debug-on-exit, in which
420 ;; case we'll probably come back to the debugger soon.
421 (goto-char (point-min))
422 (if (re-search-forward "^\\* " nil t
)
423 (setq debugger-will-be-back t
)))
424 (exit-recursive-edit))
426 (defun debugger-return-value (val)
427 "Continue, specifying value to return.
428 This is only useful when the value returned from the debugger
429 will be used, such as in a debug on exit from a frame."
430 (interactive "XReturn value (evaluated): ")
431 (setq debugger-value val
)
432 (princ "Returning " t
)
433 (prin1 debugger-value
)
435 ;; Check to see if we've flagged some frame for debug-on-exit, in which
436 ;; case we'll probably come back to the debugger soon.
437 (goto-char (point-min))
438 (if (re-search-forward "^\\* " nil t
)
439 (setq debugger-will-be-back t
)))
440 (exit-recursive-edit))
442 (defun debugger-jump ()
443 "Continue to exit from this frame, with all debug-on-entry suspended."
446 (setq debugger-jumping-flag t
)
447 (add-hook 'post-command-hook
'debugger-reenable
)
448 (message "Continuing through this frame")
449 (setq debugger-will-be-back t
)
450 (exit-recursive-edit))
452 (defun debugger-reenable ()
453 "Turn all debug-on-entry functions back on.
454 This function is put on `post-command-hook' by `debugger-jump' and
455 removes itself from that hook."
456 (setq debugger-jumping-flag nil
)
457 (remove-hook 'post-command-hook
'debugger-reenable
))
459 (defun debugger-frame-number ()
460 "Return number of frames in backtrace before the one point points at."
463 (let ((opoint (point))
465 (while (not (eq (cadr (backtrace-frame count
)) 'debug
))
466 (setq count
(1+ count
)))
467 ;; Skip implement-debug-on-entry frame.
468 (when (eq 'implement-debug-on-entry
(cadr (backtrace-frame (1+ count
))))
469 (setq count
(1+ count
)))
470 (goto-char (point-min))
471 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
472 (goto-char (match-end 0))
477 (if (= (following-char) ?\
()
482 (if (looking-at " *;;;")
484 (setq count
(1+ count
)))
487 (defun debugger-frame ()
488 "Request entry to debugger when this frame exits.
489 Applies to the frame whose line point is on in the backtrace."
493 (if (looking-at " *;;;\\|[a-z]")
494 (error "This line is not a function call")))
496 (backtrace-debug (debugger-frame-number) t
)
497 (if (= (following-char) ?
)
498 (let ((inhibit-read-only t
))
503 (defun debugger-frame-clear ()
504 "Do not enter debugger when this frame exits.
505 Applies to the frame whose line point is on in the backtrace."
509 (if (looking-at " *;;;\\|[a-z]")
510 (error "This line is not a function call")))
512 (backtrace-debug (debugger-frame-number) nil
)
513 (if (= (following-char) ?
*)
514 (let ((inhibit-read-only t
))
519 (put 'debugger-env-macro
'lisp-indent-function
0)
520 (defmacro debugger-env-macro
(&rest body
)
521 "Run BODY in original environment."
523 (if (null (buffer-name debugger-old-buffer
))
524 ;; old buffer deleted
525 (setq debugger-old-buffer
(current-buffer)))
526 (set-buffer debugger-old-buffer
)
527 (let ((load-read-function debugger-outer-load-read-function
)
528 (overriding-terminal-local-map
529 debugger-outer-overriding-terminal-local-map
)
530 (overriding-local-map debugger-outer-overriding-local-map
)
531 (track-mouse debugger-outer-track-mouse
)
532 (last-command debugger-outer-last-command
)
533 (this-command debugger-outer-this-command
)
534 (unread-command-events debugger-outer-unread-command-events
)
535 (unread-post-input-method-events
536 debugger-outer-unread-post-input-method-events
)
537 (last-input-event debugger-outer-last-input-event
)
538 (last-command-event debugger-outer-last-command-event
)
539 (last-nonmenu-event debugger-outer-last-nonmenu-event
)
540 (last-event-frame debugger-outer-last-event-frame
)
541 (standard-input debugger-outer-standard-input
)
542 (standard-output debugger-outer-standard-output
)
543 (inhibit-redisplay debugger-outer-inhibit-redisplay
)
544 (cursor-in-echo-area debugger-outer-cursor-in-echo-area
))
545 (set-match-data debugger-outer-match-data
)
547 (let ((save-ucc (with-no-warnings unread-command-char
)))
551 (setq unread-command-char debugger-outer-unread-command-char
))
552 (prog1 (progn ,@body
)
554 (setq debugger-outer-unread-command-char unread-command-char
))))
556 (setq unread-command-char save-ucc
))))
557 (setq debugger-outer-match-data
(match-data))
558 (setq debugger-outer-load-read-function load-read-function
)
559 (setq debugger-outer-overriding-terminal-local-map
560 overriding-terminal-local-map
)
561 (setq debugger-outer-overriding-local-map overriding-local-map
)
562 (setq debugger-outer-track-mouse track-mouse
)
563 (setq debugger-outer-last-command last-command
)
564 (setq debugger-outer-this-command this-command
)
565 (setq debugger-outer-unread-command-events unread-command-events
)
566 (setq debugger-outer-unread-post-input-method-events
567 unread-post-input-method-events
)
568 (setq debugger-outer-last-input-event last-input-event
)
569 (setq debugger-outer-last-command-event last-command-event
)
570 (setq debugger-outer-last-nonmenu-event last-nonmenu-event
)
571 (setq debugger-outer-last-event-frame last-event-frame
)
572 (setq debugger-outer-standard-input standard-input
)
573 (setq debugger-outer-standard-output standard-output
)
574 (setq debugger-outer-inhibit-redisplay inhibit-redisplay
)
575 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area
)
578 (defun debugger-eval-expression (exp)
579 "Eval an expression, in an environment like that outside the debugger."
581 (list (read-from-minibuffer "Eval: "
582 nil read-expression-map t
583 'read-expression-history
)))
584 (debugger-env-macro (eval-expression exp
)))
586 (defvar debugger-mode-map
587 (let ((map (make-keymap))
588 (menu-map (make-sparse-keymap)))
589 (set-keymap-parent map button-buffer-map
)
590 (suppress-keymap map
)
591 (define-key map
"-" 'negative-argument
)
592 (define-key map
"b" 'debugger-frame
)
593 (define-key map
"c" 'debugger-continue
)
594 (define-key map
"j" 'debugger-jump
)
595 (define-key map
"r" 'debugger-return-value
)
596 (define-key map
"u" 'debugger-frame-clear
)
597 (define-key map
"d" 'debugger-step-through
)
598 (define-key map
"l" 'debugger-list-functions
)
599 (define-key map
"h" 'describe-mode
)
600 (define-key map
"q" 'top-level
)
601 (define-key map
"e" 'debugger-eval-expression
)
602 (define-key map
" " 'next-line
)
603 (define-key map
"R" 'debugger-record-expression
)
604 (define-key map
"\C-m" 'debug-help-follow
)
605 (define-key map
[mouse-2
] 'push-button
)
606 (define-key map
[menu-bar debugger
] (cons "Debugger" menu-map
))
607 (define-key menu-map
[deb-top
]
608 '(menu-item "Quit" top-level
609 :help
"Quit debugging and return to top level"))
610 (define-key menu-map
[deb-s0
] '("--"))
611 (define-key menu-map
[deb-descr
]
612 '(menu-item "Describe Debugger Mode" describe-mode
613 :help
"Display documentation for debugger-mode"))
614 (define-key menu-map
[deb-hfol
]
615 '(menu-item "Help Follow" debug-help-follow
616 :help
"Follow cross-reference"))
617 (define-key menu-map
[deb-nxt
]
618 '(menu-item "Next Line" next-line
619 :help
"Move cursor down"))
620 (define-key menu-map
[deb-s1
] '("--"))
621 (define-key menu-map
[deb-lfunc
]
622 '(menu-item "List debug on entry functions" debugger-list-functions
623 :help
"Display a list of all the functions now set to debug on entry"))
624 (define-key menu-map
[deb-fclear
]
625 '(menu-item "Cancel debug frame" debugger-frame-clear
626 :help
"Do not enter debugger when this frame exits"))
627 (define-key menu-map
[deb-frame
]
628 '(menu-item "Debug frame" debugger-frame
629 :help
"Request entry to debugger when this frame exits"))
630 (define-key menu-map
[deb-s2
] '("--"))
631 (define-key menu-map
[deb-ret
]
632 '(menu-item "Return value..." debugger-return-value
633 :help
"Continue, specifying value to return."))
634 (define-key menu-map
[deb-rec
]
635 '(menu-item "Display and Record Expression" debugger-record-expression
636 :help
"Display a variable's value and record it in `*Backtrace-record*' buffer"))
637 (define-key menu-map
[deb-eval
]
638 '(menu-item "Eval Expression..." debugger-eval-expression
639 :help
"Eval an expression, in an environment like that outside the debugger"))
640 (define-key menu-map
[deb-jump
]
641 '(menu-item "Jump" debugger-jump
642 :help
"Continue to exit from this frame, with all debug-on-entry suspended"))
643 (define-key menu-map
[deb-cont
]
644 '(menu-item "Continue" debugger-continue
645 :help
"Continue, evaluating this expression without stopping"))
646 (define-key menu-map
[deb-step
]
647 '(menu-item "Step through" debugger-step-through
648 :help
"Proceed, stepping through subexpressions of this expression"))
651 (put 'debugger-mode
'mode-class
'special
)
653 (defun debugger-mode ()
654 "Mode for backtrace buffers, selected in debugger.
655 \\<debugger-mode-map>
656 A line starts with `*' if exiting that frame will call the debugger.
657 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
659 When in debugger due to frame being exited,
660 use the \\[debugger-return-value] command to override the value
661 being returned from that frame.
663 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
664 which functions will enter the debugger when called.
666 Complete list of commands:
667 \\{debugger-mode-map}"
668 (kill-all-local-variables)
669 (setq major-mode
'debugger-mode
)
670 (setq mode-name
"Debugger")
671 (setq truncate-lines t
)
672 (set-syntax-table emacs-lisp-mode-syntax-table
)
673 (use-local-map debugger-mode-map
)
674 (run-mode-hooks 'debugger-mode-hook
))
676 (defcustom debugger-record-buffer
"*Debugger-record*"
677 "*Buffer name for expression values, for \\[debugger-record-expression]."
682 (defun debugger-record-expression (exp)
683 "Display a variable's value and record it in `*Backtrace-record*' buffer."
685 (list (read-from-minibuffer
688 read-expression-map t
689 'read-expression-history
)))
690 (let* ((buffer (get-buffer-create debugger-record-buffer
))
691 (standard-output buffer
))
692 (princ (format "Debugger Eval (%s): " exp
))
693 (princ (debugger-eval-expression exp
))
696 (with-current-buffer (get-buffer debugger-record-buffer
)
698 (buffer-substring (line-beginning-position 0)
699 (line-end-position 0)))))
701 (declare-function help-xref-interned
"help-mode" (symbol))
703 (defun debug-help-follow (&optional pos
)
704 "Follow cross-reference at POS, defaulting to point.
706 For the cross-reference format, see `help-make-xrefs'."
709 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
710 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
711 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
714 (unless (push-button pos
)
715 ;; check if the symbol under point is a function or variable
719 (goto-char pos
) (skip-syntax-backward "w_")
720 (buffer-substring (point)
721 (progn (skip-syntax-forward "w_")
723 (when (or (boundp sym
) (fboundp sym
) (facep sym
))
724 (help-xref-interned sym
)))))
726 ;; When you change this, you may also need to change the number of
727 ;; frames that the debugger skips.
728 (defun implement-debug-on-entry ()
729 "Conditionally call the debugger.
730 A call to this function is inserted by `debug-on-entry' to cause
731 functions to break on entry."
732 (if (or inhibit-debug-on-entry debugger-jumping-flag
)
734 (funcall debugger
'debug
)))
736 (defun debugger-special-form-p (symbol)
737 "Return whether SYMBOL is a special form."
738 (and (fboundp symbol
)
739 (subrp (symbol-function symbol
))
740 (eq (cdr (subr-arity (symbol-function symbol
))) 'unevalled
)))
743 (defun debug-on-entry (function)
744 "Request FUNCTION to invoke debugger each time it is called.
746 When called interactively, prompt for FUNCTION in the minibuffer.
748 This works by modifying the definition of FUNCTION. If you tell the
749 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
750 normal function or a macro written in Lisp, you can also step through
751 its execution. FUNCTION can also be a primitive that is not a special
752 form, in which case stepping is not possible. Break-on-entry for
753 primitive functions only works when that function is called from Lisp.
755 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
756 Redefining FUNCTION also cancels it."
758 (let ((fn (function-called-at-point)) val
)
759 (when (debugger-special-form-p fn
)
761 (setq val
(completing-read
763 (format "Debug on entry to function (default %s): " fn
)
764 "Debug on entry to function: ")
767 (and (fboundp symbol
)
768 (not (debugger-special-form-p symbol
))))
769 t nil nil
(symbol-name fn
)))
770 (list (if (equal val
"") fn
(intern val
)))))
771 (when (debugger-special-form-p function
)
772 (error "Function %s is a special form" function
))
773 (if (or (symbolp (symbol-function function
))
774 (subrp (symbol-function function
)))
775 ;; The function is built-in or aliased to another function.
776 ;; Create a wrapper in which we can add the debug call.
777 (fset function
`(lambda (&rest debug-on-entry-args
)
778 ,(interactive-form (symbol-function function
))
779 (apply ',(symbol-function function
)
780 debug-on-entry-args
)))
781 (when (eq (car-safe (symbol-function function
)) 'autoload
)
782 ;; The function is autoloaded. Load its real definition.
783 (load (cadr (symbol-function function
)) nil noninteractive nil t
))
784 (when (or (not (consp (symbol-function function
)))
785 (and (eq (car (symbol-function function
)) 'macro
)
786 (not (consp (cdr (symbol-function function
))))))
787 ;; The function is byte-compiled. Create a wrapper in which
788 ;; we can add the debug call.
789 (debug-convert-byte-code function
)))
790 (unless (consp (symbol-function function
))
791 (error "Definition of %s is not a list" function
))
792 (fset function
(debug-on-entry-1 function t
))
793 (unless (memq function debug-function-list
)
794 (push function debug-function-list
))
798 (defun cancel-debug-on-entry (&optional function
)
799 "Undo effect of \\[debug-on-entry] on FUNCTION.
800 If FUNCTION is nil, cancel debug-on-entry for all functions.
801 When called interactively, prompt for FUNCTION in the minibuffer.
802 To specify a nil argument interactively, exit with an empty minibuffer."
806 "Cancel debug on entry to function (default all functions): "
807 (mapcar 'symbol-name debug-function-list
) nil t
)))
809 (unless (string= name
"")
812 (not (string= function
""))) ; Pre 22.1 compatibility test.
814 (let ((defn (debug-on-entry-1 function nil
)))
816 (when (and (equal (nth 1 defn
) '(&rest debug-on-entry-args
))
817 (eq (car (nth 3 defn
)) 'apply
))
818 ;; `defn' is a wrapper introduced in debug-on-entry.
819 ;; Get rid of it since we don't need it any more.
820 (setq defn
(nth 1 (nth 1 (nth 3 defn
)))))
822 (fset function defn
))
823 (setq debug-function-list
(delq function debug-function-list
))
825 (message "Cancelling debug-on-entry for all functions")
826 (mapcar 'cancel-debug-on-entry debug-function-list
)))
828 (defun debug-convert-byte-code (function)
829 (let* ((defn (symbol-function function
))
830 (macro (eq (car-safe defn
) 'macro
)))
831 (when macro
(setq defn
(cdr defn
)))
833 ;; Assume a compiled code object.
834 (let* ((contents (append defn nil
))
836 (list (list 'byte-code
(nth 1 contents
)
837 (nth 2 contents
) (nth 3 contents
)))))
838 (if (nthcdr 5 contents
)
839 (setq body
(cons (list 'interactive
(nth 5 contents
)) body
)))
841 ;; Use `documentation' here, to get the actual string,
842 ;; in case the compiled function has a reference
844 (setq body
(cons (documentation function
) body
)))
845 (setq defn
(cons 'lambda
(cons (car contents
) body
))))
846 (when macro
(setq defn
(cons 'macro defn
)))
847 (fset function defn
))))
849 (defun debug-on-entry-1 (function flag
)
850 (let* ((defn (symbol-function function
))
852 (when (eq (car-safe tail
) 'macro
)
853 (setq tail
(cdr tail
)))
854 (if (not (eq (car-safe tail
) 'lambda
))
855 ;; Only signal an error when we try to set debug-on-entry.
856 ;; When we try to clear debug-on-entry, we are now done.
858 (error "%s is not a user-defined Lisp function" function
))
859 (setq tail
(cdr tail
))
860 ;; Skip the docstring.
861 (when (and (stringp (cadr tail
)) (cddr tail
))
862 (setq tail
(cdr tail
)))
863 ;; Skip the interactive form.
864 (when (eq 'interactive
(car-safe (cadr tail
)))
865 (setq tail
(cdr tail
)))
866 (unless (eq flag
(equal (cadr tail
) '(implement-debug-on-entry)))
867 ;; Add/remove debug statement as needed.
869 (setcdr tail
(cons '(implement-debug-on-entry) (cdr tail
)))
870 (setcdr tail
(cddr tail
)))))
873 (defun debugger-list-functions ()
874 "Display a list of all the functions now set to debug on entry."
877 (help-setup-xref '(debugger-list-functions) (interactive-p))
878 (with-output-to-temp-buffer (help-buffer)
879 (with-current-buffer standard-output
880 (if (null debug-function-list
)
881 (princ "No debug-on-entry functions now\n")
882 (princ "Functions set to debug on entry:\n\n")
883 (dolist (fun debug-function-list
)
884 (make-text-button (point) (progn (prin1 fun
) (point))
886 'help-args
(list fun
))
889 (princ "Note: if you have redefined a function, then it may no longer\n")
890 (princ "be set to debug on entry, even if it is in the list.")))))
894 ;; arch-tag: b6ec7047-f801-4103-9c63-d69322db9d3b
895 ;;; debug.el ends here