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