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