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