Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / emacs-lisp / debug.el
1 ;;; debug.el --- debuggers and related commands for Emacs
2
3 ;; Copyright (C) 1985-1986, 1994, 2001-2011 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 ;;;###autoload
102 (setq debugger 'debug)
103 ;;;###autoload
104 (defun debug (&rest debugger-args)
105 "Enter debugger. \\<debugger-mode-map>`\\[debugger-continue]' returns from the debugger.
106 Arguments are mainly for use when this is called from the internals
107 of the evaluator.
108
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 (buffer-live-p debugger-buffer)
242 (with-current-buffer debugger-buffer
243 (let ((inhibit-read-only t))
244 (erase-buffer)
245 (if (null debugger-previous-state)
246 (fundamental-mode)
247 (insert (nth 1 debugger-previous-state))
248 (funcall (nth 0 debugger-previous-state))))))
249 (with-timeout-unsuspend debugger-with-timeout-suspend)
250 (set-match-data debugger-outer-match-data)))
251 ;; Put into effect the modified values of these variables
252 ;; in case the user set them with the `e' command.
253 (setq load-read-function debugger-outer-load-read-function)
254 (setq overriding-local-map debugger-outer-overriding-local-map)
255 (setq overriding-terminal-local-map
256 debugger-outer-overriding-terminal-local-map)
257 (setq track-mouse debugger-outer-track-mouse)
258 (setq last-command debugger-outer-last-command)
259 (setq this-command debugger-outer-this-command)
260 (with-no-warnings
261 (setq unread-command-char debugger-outer-unread-command-char))
262 (setq unread-command-events debugger-outer-unread-command-events)
263 (setq unread-post-input-method-events
264 debugger-outer-unread-post-input-method-events)
265 (setq last-input-event debugger-outer-last-input-event)
266 (setq last-command-event debugger-outer-last-command-event)
267 (setq last-nonmenu-event debugger-outer-last-nonmenu-event)
268 (setq last-event-frame debugger-outer-last-event-frame)
269 (setq standard-input debugger-outer-standard-input)
270 (setq standard-output debugger-outer-standard-output)
271 (setq inhibit-redisplay debugger-outer-inhibit-redisplay)
272 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area)
273 (setq debug-on-next-call debugger-step-after-exit)
274 debugger-value)))
275 \f
276 (defun debugger-setup-buffer (debugger-args)
277 "Initialize the `*Backtrace*' buffer for entry to the debugger.
278 That buffer should be current already."
279 (setq buffer-read-only nil)
280 (erase-buffer)
281 (set-buffer-multibyte t) ;Why was it nil ? -stef
282 (setq buffer-undo-list t)
283 (let ((standard-output (current-buffer))
284 (print-escape-newlines t)
285 (print-level 8)
286 (print-length 50))
287 (backtrace))
288 (goto-char (point-min))
289 (delete-region (point)
290 (progn
291 (search-forward "\n debug(")
292 (forward-line (if (eq (car debugger-args) 'debug)
293 2 ; Remove implement-debug-on-entry frame.
294 1))
295 (point)))
296 (insert "Debugger entered")
297 ;; lambda is for debug-on-call when a function call is next.
298 ;; debug is for debug-on-entry function called.
299 (cond ((memq (car debugger-args) '(lambda debug))
300 (insert "--entering a function:\n"))
301 ;; Exiting a function.
302 ((eq (car debugger-args) 'exit)
303 (insert "--returning value: ")
304 (setq debugger-value (nth 1 debugger-args))
305 (prin1 debugger-value (current-buffer))
306 (insert ?\n)
307 (delete-char 1)
308 (insert ? )
309 (beginning-of-line))
310 ;; Debugger entered for an error.
311 ((eq (car debugger-args) 'error)
312 (insert "--Lisp error: ")
313 (prin1 (nth 1 debugger-args) (current-buffer))
314 (insert ?\n))
315 ;; debug-on-call, when the next thing is an eval.
316 ((eq (car debugger-args) t)
317 (insert "--beginning evaluation of function call form:\n"))
318 ;; User calls debug directly.
319 (t
320 (insert ": ")
321 (prin1 (if (eq (car debugger-args) 'nil)
322 (cdr debugger-args) debugger-args)
323 (current-buffer))
324 (insert ?\n)))
325 ;; After any frame that uses eval-buffer,
326 ;; insert a line that states the buffer position it's reading at.
327 (save-excursion
328 (let ((tem eval-buffer-list))
329 (while (and tem
330 (re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t))
331 (end-of-line)
332 (insert (format " ; Reading at buffer position %d"
333 ;; This will get the wrong result
334 ;; if there are two nested eval-region calls
335 ;; for the same buffer. That's not a very useful case.
336 (with-current-buffer (car tem)
337 (point))))
338 (pop tem))))
339 (debugger-make-xrefs))
340
341 (defun debugger-make-xrefs (&optional buffer)
342 "Attach cross-references to function names in the `*Backtrace*' buffer."
343 (interactive "b")
344 (with-current-buffer (or buffer (current-buffer))
345 (save-excursion
346 (setq buffer (current-buffer))
347 (let ((inhibit-read-only t)
348 (old-end (point-min)) (new-end (point-min)))
349 ;; If we saved an old backtrace, find the common part
350 ;; between the new and the old.
351 ;; Compare line by line, starting from the end,
352 ;; because that's the part that is likely to be unchanged.
353 (if debugger-previous-backtrace
354 (let (old-start new-start (all-match t))
355 (goto-char (point-max))
356 (with-temp-buffer
357 (insert debugger-previous-backtrace)
358 (while (and all-match (not (bobp)))
359 (setq old-end (point))
360 (forward-line -1)
361 (setq old-start (point))
362 (with-current-buffer buffer
363 (setq new-end (point))
364 (forward-line -1)
365 (setq new-start (point)))
366 (if (not (zerop
367 (let ((case-fold-search nil))
368 (compare-buffer-substrings
369 (current-buffer) old-start old-end
370 buffer new-start new-end))))
371 (setq all-match nil))))
372 ;; Now new-end is the position of the start of the
373 ;; unchanged part in the current buffer, and old-end is
374 ;; the position of that same text in the saved old
375 ;; backtrace. But we must subtract (point-min) since strings are
376 ;; indexed in origin 0.
377
378 ;; Replace the unchanged part of the backtrace
379 ;; with the text from debugger-previous-backtrace,
380 ;; since that already has the proper xrefs.
381 ;; With this optimization, we only need to scan
382 ;; the changed part of the backtrace.
383 (delete-region new-end (point-max))
384 (goto-char (point-max))
385 (insert (substring debugger-previous-backtrace
386 (- old-end (point-min))))
387 ;; Make the unchanged part of the backtrace inaccessible
388 ;; so it won't be scanned.
389 (narrow-to-region (point-min) new-end)))
390
391 ;; Scan the new part of the backtrace, inserting xrefs.
392 (goto-char (point-min))
393 (while (progn
394 (goto-char (+ (point) 2))
395 (skip-syntax-forward "^w_")
396 (not (eobp)))
397 (let* ((beg (point))
398 (end (progn (skip-syntax-forward "w_") (point)))
399 (sym (intern-soft (buffer-substring-no-properties
400 beg end)))
401 (file (and sym (symbol-file sym 'defun))))
402 (when file
403 (goto-char beg)
404 ;; help-xref-button needs to operate on something matched
405 ;; by a regexp, so set that up for it.
406 (re-search-forward "\\(\\sw\\|\\s_\\)+")
407 (help-xref-button 0 'help-function-def sym file)))
408 (forward-line 1))
409 (widen))
410 (setq debugger-previous-backtrace (buffer-string)))))
411 \f
412 (defun debugger-step-through ()
413 "Proceed, stepping through subexpressions of this expression.
414 Enter another debugger on next entry to eval, apply or funcall."
415 (interactive)
416 (setq debugger-step-after-exit t)
417 (setq debugger-jumping-flag t)
418 (setq debugger-will-be-back t)
419 (add-hook 'post-command-hook 'debugger-reenable)
420 (message "Proceeding, will debug on next eval or call.")
421 (exit-recursive-edit))
422
423 (defun debugger-continue ()
424 "Continue, evaluating this expression without stopping."
425 (interactive)
426 (unless debugger-may-continue
427 (error "Cannot continue"))
428 (message "Continuing.")
429 (save-excursion
430 ;; Check to see if we've flagged some frame for debug-on-exit, in which
431 ;; case we'll probably come back to the debugger soon.
432 (goto-char (point-min))
433 (if (re-search-forward "^\\* " nil t)
434 (setq debugger-will-be-back t)))
435 (exit-recursive-edit))
436
437 (defun debugger-return-value (val)
438 "Continue, specifying value to return.
439 This is only useful when the value returned from the debugger
440 will be used, such as in a debug on exit from a frame."
441 (interactive "XReturn value (evaluated): ")
442 (setq debugger-value val)
443 (princ "Returning " t)
444 (prin1 debugger-value)
445 (save-excursion
446 ;; Check to see if we've flagged some frame for debug-on-exit, in which
447 ;; case we'll probably come back to the debugger soon.
448 (goto-char (point-min))
449 (if (re-search-forward "^\\* " nil t)
450 (setq debugger-will-be-back t)))
451 (exit-recursive-edit))
452
453 (defun debugger-jump ()
454 "Continue to exit from this frame, with all debug-on-entry suspended."
455 (interactive)
456 (debugger-frame)
457 (setq debugger-jumping-flag t)
458 (add-hook 'post-command-hook 'debugger-reenable)
459 (message "Continuing through this frame")
460 (setq debugger-will-be-back t)
461 (exit-recursive-edit))
462
463 (defun debugger-reenable ()
464 "Turn all debug-on-entry functions back on.
465 This function is put on `post-command-hook' by `debugger-jump' and
466 removes itself from that hook."
467 (setq debugger-jumping-flag nil)
468 (remove-hook 'post-command-hook 'debugger-reenable))
469
470 (defun debugger-frame-number ()
471 "Return number of frames in backtrace before the one point points at."
472 (save-excursion
473 (beginning-of-line)
474 (let ((opoint (point))
475 (count 0))
476 (while (not (eq (cadr (backtrace-frame count)) 'debug))
477 (setq count (1+ count)))
478 ;; Skip implement-debug-on-entry frame.
479 (when (eq 'implement-debug-on-entry (cadr (backtrace-frame (1+ count))))
480 (setq count (1+ count)))
481 (goto-char (point-min))
482 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
483 (goto-char (match-end 0))
484 (forward-sexp 1))
485 (forward-line 1)
486 (while (progn
487 (forward-char 2)
488 (if (= (following-char) ?\()
489 (forward-sexp 1)
490 (forward-sexp 2))
491 (forward-line 1)
492 (<= (point) opoint))
493 (if (looking-at " *;;;")
494 (forward-line 1))
495 (setq count (1+ count)))
496 count)))
497
498 (defun debugger-frame ()
499 "Request entry to debugger when this frame exits.
500 Applies to the frame whose line point is on in the backtrace."
501 (interactive)
502 (save-excursion
503 (beginning-of-line)
504 (if (looking-at " *;;;\\|[a-z]")
505 (error "This line is not a function call")))
506 (beginning-of-line)
507 (backtrace-debug (debugger-frame-number) t)
508 (if (= (following-char) ? )
509 (let ((inhibit-read-only t))
510 (delete-char 1)
511 (insert ?*)))
512 (beginning-of-line))
513
514 (defun debugger-frame-clear ()
515 "Do not enter debugger when this frame exits.
516 Applies to the frame whose line point is on in the backtrace."
517 (interactive)
518 (save-excursion
519 (beginning-of-line)
520 (if (looking-at " *;;;\\|[a-z]")
521 (error "This line is not a function call")))
522 (beginning-of-line)
523 (backtrace-debug (debugger-frame-number) nil)
524 (if (= (following-char) ?*)
525 (let ((inhibit-read-only t))
526 (delete-char 1)
527 (insert ? )))
528 (beginning-of-line))
529
530 (defmacro debugger-env-macro (&rest body)
531 "Run BODY in original environment."
532 (declare (indent 0))
533 `(save-excursion
534 (if (null (buffer-name debugger-old-buffer))
535 ;; old buffer deleted
536 (setq debugger-old-buffer (current-buffer)))
537 (set-buffer debugger-old-buffer)
538 (let ((load-read-function debugger-outer-load-read-function)
539 (overriding-terminal-local-map
540 debugger-outer-overriding-terminal-local-map)
541 (overriding-local-map debugger-outer-overriding-local-map)
542 (track-mouse debugger-outer-track-mouse)
543 (last-command debugger-outer-last-command)
544 (this-command debugger-outer-this-command)
545 (unread-command-events debugger-outer-unread-command-events)
546 (unread-post-input-method-events
547 debugger-outer-unread-post-input-method-events)
548 (last-input-event debugger-outer-last-input-event)
549 (last-command-event debugger-outer-last-command-event)
550 (last-nonmenu-event debugger-outer-last-nonmenu-event)
551 (last-event-frame debugger-outer-last-event-frame)
552 (standard-input debugger-outer-standard-input)
553 (standard-output debugger-outer-standard-output)
554 (inhibit-redisplay debugger-outer-inhibit-redisplay)
555 (cursor-in-echo-area debugger-outer-cursor-in-echo-area))
556 (set-match-data debugger-outer-match-data)
557 (prog1
558 (let ((save-ucc (with-no-warnings unread-command-char)))
559 (unwind-protect
560 (progn
561 (with-no-warnings
562 (setq unread-command-char debugger-outer-unread-command-char))
563 (prog1 (progn ,@body)
564 (with-no-warnings
565 (setq debugger-outer-unread-command-char unread-command-char))))
566 (with-no-warnings
567 (setq unread-command-char save-ucc))))
568 (setq debugger-outer-match-data (match-data))
569 (setq debugger-outer-load-read-function load-read-function)
570 (setq debugger-outer-overriding-terminal-local-map
571 overriding-terminal-local-map)
572 (setq debugger-outer-overriding-local-map overriding-local-map)
573 (setq debugger-outer-track-mouse track-mouse)
574 (setq debugger-outer-last-command last-command)
575 (setq debugger-outer-this-command this-command)
576 (setq debugger-outer-unread-command-events unread-command-events)
577 (setq debugger-outer-unread-post-input-method-events
578 unread-post-input-method-events)
579 (setq debugger-outer-last-input-event last-input-event)
580 (setq debugger-outer-last-command-event last-command-event)
581 (setq debugger-outer-last-nonmenu-event last-nonmenu-event)
582 (setq debugger-outer-last-event-frame last-event-frame)
583 (setq debugger-outer-standard-input standard-input)
584 (setq debugger-outer-standard-output standard-output)
585 (setq debugger-outer-inhibit-redisplay inhibit-redisplay)
586 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area)
587 ))))
588
589 (defun debugger-eval-expression (exp)
590 "Eval an expression, in an environment like that outside the debugger."
591 (interactive
592 (list (read-from-minibuffer "Eval: "
593 nil read-expression-map t
594 'read-expression-history)))
595 (debugger-env-macro (eval-expression exp)))
596 \f
597 (defvar debugger-mode-map
598 (let ((map (make-keymap))
599 (menu-map (make-sparse-keymap)))
600 (set-keymap-parent map button-buffer-map)
601 (suppress-keymap map)
602 (define-key map "-" 'negative-argument)
603 (define-key map "b" 'debugger-frame)
604 (define-key map "c" 'debugger-continue)
605 (define-key map "j" 'debugger-jump)
606 (define-key map "r" 'debugger-return-value)
607 (define-key map "u" 'debugger-frame-clear)
608 (define-key map "d" 'debugger-step-through)
609 (define-key map "l" 'debugger-list-functions)
610 (define-key map "h" 'describe-mode)
611 (define-key map "q" 'top-level)
612 (define-key map "e" 'debugger-eval-expression)
613 (define-key map " " 'next-line)
614 (define-key map "R" 'debugger-record-expression)
615 (define-key map "\C-m" 'debug-help-follow)
616 (define-key map [mouse-2] 'push-button)
617 (define-key map [menu-bar debugger] (cons "Debugger" menu-map))
618 (define-key menu-map [deb-top]
619 '(menu-item "Quit" top-level
620 :help "Quit debugging and return to top level"))
621 (define-key menu-map [deb-s0] '("--"))
622 (define-key menu-map [deb-descr]
623 '(menu-item "Describe Debugger Mode" describe-mode
624 :help "Display documentation for debugger-mode"))
625 (define-key menu-map [deb-hfol]
626 '(menu-item "Help Follow" debug-help-follow
627 :help "Follow cross-reference"))
628 (define-key menu-map [deb-nxt]
629 '(menu-item "Next Line" next-line
630 :help "Move cursor down"))
631 (define-key menu-map [deb-s1] '("--"))
632 (define-key menu-map [deb-lfunc]
633 '(menu-item "List debug on entry functions" debugger-list-functions
634 :help "Display a list of all the functions now set to debug on entry"))
635 (define-key menu-map [deb-fclear]
636 '(menu-item "Cancel debug frame" debugger-frame-clear
637 :help "Do not enter debugger when this frame exits"))
638 (define-key menu-map [deb-frame]
639 '(menu-item "Debug frame" debugger-frame
640 :help "Request entry to debugger when this frame exits"))
641 (define-key menu-map [deb-s2] '("--"))
642 (define-key menu-map [deb-ret]
643 '(menu-item "Return value..." debugger-return-value
644 :help "Continue, specifying value to return."))
645 (define-key menu-map [deb-rec]
646 '(menu-item "Display and Record Expression" debugger-record-expression
647 :help "Display a variable's value and record it in `*Backtrace-record*' buffer"))
648 (define-key menu-map [deb-eval]
649 '(menu-item "Eval Expression..." debugger-eval-expression
650 :help "Eval an expression, in an environment like that outside the debugger"))
651 (define-key menu-map [deb-jump]
652 '(menu-item "Jump" debugger-jump
653 :help "Continue to exit from this frame, with all debug-on-entry suspended"))
654 (define-key menu-map [deb-cont]
655 '(menu-item "Continue" debugger-continue
656 :help "Continue, evaluating this expression without stopping"))
657 (define-key menu-map [deb-step]
658 '(menu-item "Step through" debugger-step-through
659 :help "Proceed, stepping through subexpressions of this expression"))
660 map))
661
662 (put 'debugger-mode 'mode-class 'special)
663
664 (defun debugger-mode ()
665 "Mode for backtrace buffers, selected in debugger.
666 \\<debugger-mode-map>
667 A line starts with `*' if exiting that frame will call the debugger.
668 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
669
670 When in debugger due to frame being exited,
671 use the \\[debugger-return-value] command to override the value
672 being returned from that frame.
673
674 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
675 which functions will enter the debugger when called.
676
677 Complete list of commands:
678 \\{debugger-mode-map}"
679 (kill-all-local-variables)
680 (setq major-mode 'debugger-mode)
681 (setq mode-name "Debugger")
682 (setq truncate-lines t)
683 (set-syntax-table emacs-lisp-mode-syntax-table)
684 (use-local-map debugger-mode-map)
685 (run-mode-hooks 'debugger-mode-hook))
686 \f
687 (defcustom debugger-record-buffer "*Debugger-record*"
688 "Buffer name for expression values, for \\[debugger-record-expression]."
689 :type 'string
690 :group 'debugger
691 :version "20.3")
692
693 (defun debugger-record-expression (exp)
694 "Display a variable's value and record it in `*Backtrace-record*' buffer."
695 (interactive
696 (list (read-from-minibuffer
697 "Record Eval: "
698 nil
699 read-expression-map t
700 'read-expression-history)))
701 (let* ((buffer (get-buffer-create debugger-record-buffer))
702 (standard-output buffer))
703 (princ (format "Debugger Eval (%s): " exp))
704 (princ (debugger-eval-expression exp))
705 (terpri))
706
707 (with-current-buffer (get-buffer debugger-record-buffer)
708 (message "%s"
709 (buffer-substring (line-beginning-position 0)
710 (line-end-position 0)))))
711
712 (declare-function help-xref-interned "help-mode" (symbol))
713
714 (defun debug-help-follow (&optional pos)
715 "Follow cross-reference at POS, defaulting to point.
716
717 For the cross-reference format, see `help-make-xrefs'."
718 (interactive "d")
719 (require 'help-mode)
720 ;; Ideally we'd just do (call-interactively 'help-follow) except that this
721 ;; assumes we're already in a *Help* buffer and reuses it, so it ends up
722 ;; incorrectly "reusing" the *Backtrace* buffer to show the help info.
723 (unless pos
724 (setq pos (point)))
725 (unless (push-button pos)
726 ;; check if the symbol under point is a function or variable
727 (let ((sym
728 (intern
729 (save-excursion
730 (goto-char pos) (skip-syntax-backward "w_")
731 (buffer-substring (point)
732 (progn (skip-syntax-forward "w_")
733 (point)))))))
734 (when (or (boundp sym) (fboundp sym) (facep sym))
735 (help-xref-interned sym)))))
736 \f
737 ;; When you change this, you may also need to change the number of
738 ;; frames that the debugger skips.
739 (defun implement-debug-on-entry ()
740 "Conditionally call the debugger.
741 A call to this function is inserted by `debug-on-entry' to cause
742 functions to break on entry."
743 (if (or inhibit-debug-on-entry debugger-jumping-flag)
744 nil
745 (funcall debugger 'debug)))
746
747 (defun debugger-special-form-p (symbol)
748 "Return whether SYMBOL is a special form."
749 (and (fboundp symbol)
750 (subrp (symbol-function symbol))
751 (eq (cdr (subr-arity (symbol-function symbol))) 'unevalled)))
752
753 ;;;###autoload
754 (defun debug-on-entry (function)
755 "Request FUNCTION to invoke debugger each time it is called.
756
757 When called interactively, prompt for FUNCTION in the minibuffer.
758
759 This works by modifying the definition of FUNCTION. If you tell the
760 debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a
761 normal function or a macro written in Lisp, you can also step through
762 its execution. FUNCTION can also be a primitive that is not a special
763 form, in which case stepping is not possible. Break-on-entry for
764 primitive functions only works when that function is called from Lisp.
765
766 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
767 Redefining FUNCTION also cancels it."
768 (interactive
769 (let ((fn (function-called-at-point)) val)
770 (when (debugger-special-form-p fn)
771 (setq fn nil))
772 (setq val (completing-read
773 (if fn
774 (format "Debug on entry to function (default %s): " fn)
775 "Debug on entry to function: ")
776 obarray
777 #'(lambda (symbol)
778 (and (fboundp symbol)
779 (not (debugger-special-form-p symbol))))
780 t nil nil (symbol-name fn)))
781 (list (if (equal val "") fn (intern val)))))
782 ;; FIXME: Use advice.el.
783 (when (debugger-special-form-p function)
784 (error "Function %s is a special form" function))
785 (if (or (symbolp (symbol-function function))
786 (subrp (symbol-function function)))
787 ;; The function is built-in or aliased to another function.
788 ;; Create a wrapper in which we can add the debug call.
789 (fset function `(lambda (&rest debug-on-entry-args)
790 ,(interactive-form (symbol-function function))
791 (apply ',(symbol-function function)
792 debug-on-entry-args)))
793 (when (eq (car-safe (symbol-function function)) 'autoload)
794 ;; The function is autoloaded. Load its real definition.
795 (load (cadr (symbol-function function)) nil noninteractive nil t))
796 (when (or (not (consp (symbol-function function)))
797 (and (eq (car (symbol-function function)) 'macro)
798 (not (consp (cdr (symbol-function function))))))
799 ;; The function is byte-compiled. Create a wrapper in which
800 ;; we can add the debug call.
801 (debug-convert-byte-code function)))
802 (unless (consp (symbol-function function))
803 (error "Definition of %s is not a list" function))
804 (fset function (debug-on-entry-1 function t))
805 (unless (memq function debug-function-list)
806 (push function debug-function-list))
807 function)
808
809 ;;;###autoload
810 (defun cancel-debug-on-entry (&optional function)
811 "Undo effect of \\[debug-on-entry] on FUNCTION.
812 If FUNCTION is nil, cancel debug-on-entry for all functions.
813 When called interactively, prompt for FUNCTION in the minibuffer.
814 To specify a nil argument interactively, exit with an empty minibuffer."
815 (interactive
816 (list (let ((name
817 (completing-read
818 "Cancel debug on entry to function (default all functions): "
819 (mapcar 'symbol-name debug-function-list) nil t)))
820 (when name
821 (unless (string= name "")
822 (intern name))))))
823 (if (and function
824 (not (string= function ""))) ; Pre 22.1 compatibility test.
825 (progn
826 (let ((defn (debug-on-entry-1 function nil)))
827 (condition-case nil
828 (when (and (equal (nth 1 defn) '(&rest debug-on-entry-args))
829 (eq (car (nth 3 defn)) 'apply))
830 ;; `defn' is a wrapper introduced in debug-on-entry.
831 ;; Get rid of it since we don't need it any more.
832 (setq defn (nth 1 (nth 1 (nth 3 defn)))))
833 (error nil))
834 (fset function defn))
835 (setq debug-function-list (delq function debug-function-list))
836 function)
837 (message "Cancelling debug-on-entry for all functions")
838 (mapcar 'cancel-debug-on-entry debug-function-list)))
839
840 (defun debug-arglist (definition)
841 ;; FIXME: copied from ad-arglist.
842 "Return the argument list of DEFINITION."
843 (require 'help-fns)
844 (help-function-arglist definition 'preserve-names))
845
846 (defun debug-convert-byte-code (function)
847 (let* ((defn (symbol-function function))
848 (macro (eq (car-safe defn) 'macro)))
849 (when macro (setq defn (cdr defn)))
850 (when (byte-code-function-p defn)
851 (let* ((args (debug-arglist defn))
852 (body
853 `((,(if (memq '&rest args) #'apply #'funcall)
854 ,defn
855 ,@(remq '&rest (remq '&optional args))))))
856 (if (> (length defn) 5)
857 (push `(interactive ,(aref defn 5)) body))
858 (if (aref defn 4)
859 ;; Use `documentation' here, to get the actual string,
860 ;; in case the compiled function has a reference
861 ;; to the .elc file.
862 (setq body (cons (documentation function) body)))
863 (setq defn `(closure (t) ,args ,@body)))
864 (when macro (setq defn (cons 'macro defn)))
865 (fset function defn))))
866
867 (defun debug-on-entry-1 (function flag)
868 (let* ((defn (symbol-function function))
869 (tail defn))
870 (when (eq (car-safe tail) 'macro)
871 (setq tail (cdr tail)))
872 (if (not (memq (car-safe tail) '(closure lambda)))
873 ;; Only signal an error when we try to set debug-on-entry.
874 ;; When we try to clear debug-on-entry, we are now done.
875 (when flag
876 (error "%s is not a user-defined Lisp function" function))
877 (if (eq (car tail) 'closure) (setq tail (cdr tail)))
878 (setq tail (cdr tail))
879 ;; Skip the docstring.
880 (when (and (stringp (cadr tail)) (cddr tail))
881 (setq tail (cdr tail)))
882 ;; Skip the interactive form.
883 (when (eq 'interactive (car-safe (cadr tail)))
884 (setq tail (cdr tail)))
885 (unless (eq flag (equal (cadr tail) '(implement-debug-on-entry)))
886 ;; Add/remove debug statement as needed.
887 (setcdr tail (if flag
888 (cons '(implement-debug-on-entry) (cdr tail))
889 (cddr tail)))))
890 defn))
891
892 (defun debugger-list-functions ()
893 "Display a list of all the functions now set to debug on entry."
894 (interactive)
895 (require 'help-mode)
896 (help-setup-xref '(debugger-list-functions)
897 (called-interactively-p 'interactive))
898 (with-output-to-temp-buffer (help-buffer)
899 (with-current-buffer standard-output
900 (if (null debug-function-list)
901 (princ "No debug-on-entry functions now\n")
902 (princ "Functions set to debug on entry:\n\n")
903 (dolist (fun debug-function-list)
904 (make-text-button (point) (progn (prin1 fun) (point))
905 'type 'help-function
906 'help-args (list fun))
907 (terpri))
908 (terpri)
909 (princ "Note: if you have redefined a function, then it may no longer\n")
910 (princ "be set to debug on entry, even if it is in the list.")))))
911
912 (provide 'debug)
913
914 ;;; debug.el ends here