]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/debug.el
(define-obsolete-function-alias): Fix typo in docstring.
[gnu-emacs] / lisp / emacs-lisp / debug.el
1 ;;; debug.el --- debuggers and related commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1994, 2001, 2003, 2005
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, tools, maint
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This is a major mode documented in the Emacs Lisp manual.
29
30 ;;; Code:
31
32 (require 'button)
33
34 (defgroup debugger nil
35 "Debuggers and related commands for Emacs."
36 :prefix "debugger-"
37 :group 'debug)
38
39 (defcustom debugger-mode-hook nil
40 "*Hooks run when `debugger-mode' is turned on."
41 :type 'hook
42 :group 'debugger
43 :version "20.3")
44
45 (defcustom debugger-batch-max-lines 40
46 "*Maximum lines to show in debugger buffer in a noninteractive Emacs.
47 When the debugger is entered and Emacs is running in batch mode,
48 if the backtrace text has more than this many lines,
49 the middle is discarded, and just the beginning and end are displayed."
50 :type 'integer
51 :group 'debugger
52 :version "21.1")
53
54 (defvar debug-function-list nil
55 "List of functions currently set for debug on entry.")
56
57 (defvar debugger-step-after-exit nil
58 "Non-nil means \"single-step\" after the debugger exits.")
59
60 (defvar debugger-value nil
61 "This is the value for the debugger to return, when it returns.")
62
63 (defvar debugger-old-buffer nil
64 "This is the buffer that was current when the debugger was entered.")
65
66 (defvar debugger-previous-backtrace nil
67 "The contents of the previous backtrace (including text properties).
68 This is to optimize `debugger-make-xrefs'.")
69
70 (defvar debugger-outer-match-data)
71 (defvar debugger-outer-load-read-function)
72 (defvar debugger-outer-overriding-local-map)
73 (defvar debugger-outer-overriding-terminal-local-map)
74 (defvar debugger-outer-track-mouse)
75 (defvar debugger-outer-last-command)
76 (defvar debugger-outer-this-command)
77 ;; unread-command-char is obsolete,
78 ;; but we still save and restore it
79 ;; in case some user program still tries to set it.
80 (defvar debugger-outer-unread-command-char)
81 (defvar debugger-outer-unread-command-events)
82 (defvar debugger-outer-unread-post-input-method-events)
83 (defvar debugger-outer-last-input-event)
84 (defvar debugger-outer-last-command-event)
85 (defvar debugger-outer-last-nonmenu-event)
86 (defvar debugger-outer-last-event-frame)
87 (defvar debugger-outer-standard-input)
88 (defvar debugger-outer-standard-output)
89 (defvar debugger-outer-inhibit-redisplay)
90 (defvar debugger-outer-cursor-in-echo-area)
91
92 (defvar inhibit-debug-on-entry nil
93 "Non-nil means that debug-on-entry is disabled.")
94
95 (defvar debugger-jumping-flag nil
96 "Non-nil means that debug-on-entry is disabled.
97 This variable is used by `debugger-jump', `debugger-step-through',
98 and `debugger-reenable' to temporarily disable debug-on-entry.")
99
100 ;;;###autoload
101 (setq debugger 'debug)
102 ;;;###autoload
103 (defun debug (&rest debugger-args)
104 "Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'.
105 Arguments are mainly for use when this is called from the internals
106 of the evaluator.
107
108 You may call with no args, or you may pass nil as the first arg and
109 any other args you like. In that case, the list of args after the
110 first will be printed into the backtrace buffer."
111 (interactive)
112 (if inhibit-redisplay
113 ;; Don't really try to enter debugger within an eval from redisplay.
114 debugger-value
115 (unless noninteractive
116 (message "Entering debugger..."))
117 (let (debugger-value
118 (debug-on-error nil)
119 (debug-on-quit nil)
120 (debugger-buffer (let ((default-major-mode 'fundamental-mode))
121 (get-buffer-create "*Backtrace*")))
122 (debugger-old-buffer (current-buffer))
123 (debugger-step-after-exit nil)
124 ;; Don't keep reading from an executing kbd macro!
125 (executing-kbd-macro nil)
126 ;; Save the outer values of these vars for the `e' command
127 ;; before we replace the values.
128 (debugger-outer-match-data (match-data))
129 (debugger-outer-load-read-function load-read-function)
130 (debugger-outer-overriding-local-map overriding-local-map)
131 (debugger-outer-overriding-terminal-local-map
132 overriding-terminal-local-map)
133 (debugger-outer-track-mouse track-mouse)
134 (debugger-outer-last-command last-command)
135 (debugger-outer-this-command this-command)
136 (debugger-outer-unread-command-char
137 (with-no-warnings unread-command-char))
138 (debugger-outer-unread-command-events unread-command-events)
139 (debugger-outer-unread-post-input-method-events
140 unread-post-input-method-events)
141 (debugger-outer-last-input-event last-input-event)
142 (debugger-outer-last-command-event last-command-event)
143 (debugger-outer-last-nonmenu-event last-nonmenu-event)
144 (debugger-outer-last-event-frame last-event-frame)
145 (debugger-outer-standard-input standard-input)
146 (debugger-outer-standard-output standard-output)
147 (debugger-outer-inhibit-redisplay inhibit-redisplay)
148 (debugger-outer-cursor-in-echo-area cursor-in-echo-area))
149 ;; Set this instead of binding it, so that `q'
150 ;; will not restore it.
151 (setq overriding-terminal-local-map nil)
152 ;; Don't let these magic variables affect the debugger itself.
153 (let ((last-command nil) this-command track-mouse
154 (inhibit-trace t)
155 (inhibit-debug-on-entry t)
156 unread-command-events
157 unread-post-input-method-events
158 last-input-event last-command-event last-nonmenu-event
159 last-event-frame
160 overriding-local-map
161 load-read-function
162 ;; If we are inside a minibuffer, allow nesting
163 ;; so that we don't get an error from the `e' command.
164 (enable-recursive-minibuffers
165 (or enable-recursive-minibuffers (> (minibuffer-depth) 0)))
166 (standard-input t) (standard-output t)
167 inhibit-redisplay
168 (cursor-in-echo-area nil))
169 (unwind-protect
170 (save-excursion
171 (save-window-excursion
172 (with-no-warnings
173 (setq unread-command-char -1))
174 (when (eq (car debugger-args) 'debug)
175 ;; Skip the frames for backtrace-debug, byte-code,
176 ;; and implement-debug-on-entry.
177 (backtrace-debug 4 t)
178 ;; Place an extra debug-on-exit for macro's.
179 (when (eq 'lambda (car-safe (cadr (backtrace-frame 4))))
180 (backtrace-debug 5 t)))
181 (pop-to-buffer debugger-buffer)
182 (debugger-mode)
183 (debugger-setup-buffer debugger-args)
184 (when noninteractive
185 ;; If the backtrace is long, save the beginning
186 ;; and the end, but discard the middle.
187 (when (> (count-lines (point-min) (point-max))
188 debugger-batch-max-lines)
189 (goto-char (point-min))
190 (forward-line (/ 2 debugger-batch-max-lines))
191 (let ((middlestart (point)))
192 (goto-char (point-max))
193 (forward-line (- (/ 2 debugger-batch-max-lines)
194 debugger-batch-max-lines))
195 (delete-region middlestart (point)))
196 (insert "...\n"))
197 (goto-char (point-min))
198 (message "%s" (buffer-string))
199 (kill-emacs))
200 (message "")
201 (let ((standard-output nil)
202 (buffer-read-only t))
203 (message "")
204 ;; Make sure we unbind buffer-read-only in the right buffer.
205 (save-excursion
206 (recursive-edit)))))
207 ;; Kill or at least neuter the backtrace buffer, so that users
208 ;; don't try to execute debugger commands in an invalid context.
209 (if (get-buffer-window debugger-buffer 0)
210 ;; Still visible despite the save-window-excursion? Maybe it
211 ;; it's in a pop-up frame. It would be annoying to delete and
212 ;; recreate it every time the debugger stops, so instead we'll
213 ;; erase it and hide it but keep it alive.
214 (with-current-buffer debugger-buffer
215 (erase-buffer)
216 (fundamental-mode)
217 (with-selected-window (get-buffer-window debugger-buffer 0)
218 (bury-buffer)))
219 (kill-buffer debugger-buffer))
220 (set-match-data debugger-outer-match-data)))
221 ;; Put into effect the modified values of these variables
222 ;; in case the user set them with the `e' command.
223 (setq load-read-function debugger-outer-load-read-function)
224 (setq overriding-local-map debugger-outer-overriding-local-map)
225 (setq overriding-terminal-local-map
226 debugger-outer-overriding-terminal-local-map)
227 (setq track-mouse debugger-outer-track-mouse)
228 (setq last-command debugger-outer-last-command)
229 (setq this-command debugger-outer-this-command)
230 (with-no-warnings
231 (setq unread-command-char debugger-outer-unread-command-char))
232 (setq unread-command-events debugger-outer-unread-command-events)
233 (setq unread-post-input-method-events
234 debugger-outer-unread-post-input-method-events)
235 (setq last-input-event debugger-outer-last-input-event)
236 (setq last-command-event debugger-outer-last-command-event)
237 (setq last-nonmenu-event debugger-outer-last-nonmenu-event)
238 (setq last-event-frame debugger-outer-last-event-frame)
239 (setq standard-input debugger-outer-standard-input)
240 (setq standard-output debugger-outer-standard-output)
241 (setq inhibit-redisplay debugger-outer-inhibit-redisplay)
242 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area)
243 (setq debug-on-next-call debugger-step-after-exit)
244 debugger-value)))
245 \f
246 (defun debugger-setup-buffer (debugger-args)
247 "Initialize the `*Backtrace*' buffer for entry to the debugger.
248 That buffer should be current already."
249 (setq buffer-read-only nil)
250 (erase-buffer)
251 (set-buffer-multibyte nil)
252 (let ((standard-output (current-buffer))
253 (print-escape-newlines t)
254 (print-level 8)
255 (print-length 50))
256 (backtrace))
257 (goto-char (point-min))
258 (delete-region (point)
259 (progn
260 (search-forward "\n debug(")
261 (forward-line (if (eq (car debugger-args) 'debug)
262 2 ; Remove implement-debug-on-entry frame.
263 1))
264 (point)))
265 (insert "Debugger entered")
266 ;; lambda is for debug-on-call when a function call is next.
267 ;; debug is for debug-on-entry function called.
268 (cond ((memq (car debugger-args) '(lambda debug))
269 (insert "--entering a function:\n"))
270 ;; Exiting a function.
271 ((eq (car debugger-args) 'exit)
272 (insert "--returning value: ")
273 (setq debugger-value (nth 1 debugger-args))
274 (prin1 debugger-value (current-buffer))
275 (insert ?\n)
276 (delete-char 1)
277 (insert ? )
278 (beginning-of-line))
279 ;; Debugger entered for an error.
280 ((eq (car debugger-args) 'error)
281 (insert "--Lisp error: ")
282 (prin1 (nth 1 debugger-args) (current-buffer))
283 (insert ?\n))
284 ;; debug-on-call, when the next thing is an eval.
285 ((eq (car debugger-args) t)
286 (insert "--beginning evaluation of function call form:\n"))
287 ;; User calls debug directly.
288 (t
289 (insert ": ")
290 (prin1 (if (eq (car debugger-args) 'nil)
291 (cdr debugger-args) debugger-args)
292 (current-buffer))
293 (insert ?\n)))
294 ;; After any frame that uses eval-buffer,
295 ;; insert a line that states the buffer position it's reading at.
296 (save-excursion
297 (while (re-search-forward "^ eval-buffer(" nil t)
298 (end-of-line)
299 (insert (format "\n ;;; Reading at buffer position %d"
300 (with-current-buffer (nth 2 (backtrace-frame (debugger-frame-number)))
301 (point))))))
302 (debugger-make-xrefs))
303
304 (defun debugger-make-xrefs (&optional buffer)
305 "Attach cross-references to function names in the `*Backtrace*' buffer."
306 (interactive "b")
307 (save-excursion
308 (set-buffer (or buffer (current-buffer)))
309 (setq buffer (current-buffer))
310 (let ((buffer-read-only nil)
311 (old-end (point-min)) (new-end (point-min)))
312 ;; If we saved an old backtrace, find the common part
313 ;; between the new and the old.
314 ;; Compare line by line, starting from the end,
315 ;; because that's the part that is likely to be unchanged.
316 (if debugger-previous-backtrace
317 (let (old-start new-start (all-match t))
318 (goto-char (point-max))
319 (with-temp-buffer
320 (insert debugger-previous-backtrace)
321 (while (and all-match (not (bobp)))
322 (setq old-end (point))
323 (forward-line -1)
324 (setq old-start (point))
325 (with-current-buffer buffer
326 (setq new-end (point))
327 (forward-line -1)
328 (setq new-start (point)))
329 (if (not (zerop
330 (compare-buffer-substrings
331 (current-buffer) old-start old-end
332 buffer new-start new-end)))
333 (setq all-match nil))))
334 ;; Now new-end is the position of the start of the
335 ;; unchanged part in the current buffer, and old-end is
336 ;; the position of that same text in the saved old
337 ;; backtrace. But we must subtract (point-min) since strings are
338 ;; indexed in origin 0.
339
340 ;; Replace the unchanged part of the backtrace
341 ;; with the text from debugger-previous-backtrace,
342 ;; since that already has the proper xrefs.
343 ;; With this optimization, we only need to scan
344 ;; the changed part of the backtrace.
345 (delete-region new-end (point-max))
346 (goto-char (point-max))
347 (insert (substring debugger-previous-backtrace
348 (- old-end (point-min))))
349 ;; Make the unchanged part of the backtrace inaccessible
350 ;; so it won't be scanned.
351 (narrow-to-region (point-min) new-end)))
352
353 ;; Scan the new part of the backtrace, inserting xrefs.
354 (goto-char (point-min))
355 (while (progn
356 (goto-char (+ (point) 2))
357 (skip-syntax-forward "^w_")
358 (not (eobp)))
359 (let* ((beg (point))
360 (end (progn (skip-syntax-forward "w_") (point)))
361 (sym (intern-soft (buffer-substring-no-properties
362 beg end)))
363 (file (and sym (symbol-file sym 'defun))))
364 (when file
365 (goto-char beg)
366 ;; help-xref-button needs to operate on something matched
367 ;; by a regexp, so set that up for it.
368 (re-search-forward "\\(\\sw\\|\\s_\\)+")
369 (help-xref-button 0 'help-function-def sym file)))
370 (forward-line 1))
371 (widen))
372 (setq debugger-previous-backtrace (buffer-string))))
373 \f
374 (defun debugger-step-through ()
375 "Proceed, stepping through subexpressions of this expression.
376 Enter another debugger on next entry to eval, apply or funcall."
377 (interactive)
378 (setq debugger-step-after-exit t)
379 (setq debugger-jumping-flag t)
380 (add-hook 'post-command-hook 'debugger-reenable)
381 (message "Proceeding, will debug on next eval or call.")
382 (exit-recursive-edit))
383
384 (defun debugger-continue ()
385 "Continue, evaluating this expression without stopping."
386 (interactive)
387 (unless debugger-may-continue
388 (error "Cannot continue"))
389 (message "Continuing.")
390 (exit-recursive-edit))
391
392 (defun debugger-return-value (val)
393 "Continue, specifying value to return.
394 This is only useful when the value returned from the debugger
395 will be used, such as in a debug on exit from a frame."
396 (interactive "XReturn value (evaluated): ")
397 (setq debugger-value val)
398 (princ "Returning " t)
399 (prin1 debugger-value)
400 (exit-recursive-edit))
401
402 (defun debugger-jump ()
403 "Continue to exit from this frame, with all debug-on-entry suspended."
404 (interactive)
405 (debugger-frame)
406 (setq debugger-jumping-flag t)
407 (add-hook 'post-command-hook 'debugger-reenable)
408 (message "Continuing through this frame")
409 (exit-recursive-edit))
410
411 (defun debugger-reenable ()
412 "Turn all debug-on-entry functions back on.
413 This function is put on `post-command-hook' by `debugger-jump' and
414 removes itself from that hook."
415 (setq debugger-jumping-flag nil)
416 (remove-hook 'post-command-hook 'debugger-reenable))
417
418 (defun debugger-frame-number ()
419 "Return number of frames in backtrace before the one point points at."
420 (save-excursion
421 (beginning-of-line)
422 (let ((opoint (point))
423 (count 0))
424 (while (not (eq (cadr (backtrace-frame count)) 'debug))
425 (setq count (1+ count)))
426 ;; Skip implement-debug-on-entry frame.
427 (when (eq 'implement-debug-on-entry (cadr (backtrace-frame (1+ count))))
428 (setq count (1+ count)))
429 (goto-char (point-min))
430 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
431 (goto-char (match-end 0))
432 (forward-sexp 1))
433 (forward-line 1)
434 (while (progn
435 (forward-char 2)
436 (if (= (following-char) ?\()
437 (forward-sexp 1)
438 (forward-sexp 2))
439 (forward-line 1)
440 (<= (point) opoint))
441 (if (looking-at " *;;;")
442 (forward-line 1))
443 (setq count (1+ count)))
444 count)))
445
446 (defun debugger-frame ()
447 "Request entry to debugger when this frame exits.
448 Applies to the frame whose line point is on in the backtrace."
449 (interactive)
450 (save-excursion
451 (beginning-of-line)
452 (if (looking-at " *;;;\\|[a-z]")
453 (error "This line is not a function call")))
454 (beginning-of-line)
455 (backtrace-debug (debugger-frame-number) t)
456 (if (= (following-char) ? )
457 (let ((buffer-read-only nil))
458 (delete-char 1)
459 (insert ?*)))
460 (beginning-of-line))
461
462 (defun debugger-frame-clear ()
463 "Do not enter debugger when this frame exits.
464 Applies to the frame whose line point is on in the backtrace."
465 (interactive)
466 (save-excursion
467 (beginning-of-line)
468 (if (looking-at " *;;;\\|[a-z]")
469 (error "This line is not a function call")))
470 (beginning-of-line)
471 (backtrace-debug (debugger-frame-number) nil)
472 (if (= (following-char) ?*)
473 (let ((buffer-read-only nil))
474 (delete-char 1)
475 (insert ? )))
476 (beginning-of-line))
477
478 (put 'debugger-env-macro 'lisp-indent-function 0)
479 (defmacro debugger-env-macro (&rest body)
480 "Run BODY in original environment."
481 `(save-excursion
482 (if (null (buffer-name debugger-old-buffer))
483 ;; old buffer deleted
484 (setq debugger-old-buffer (current-buffer)))
485 (set-buffer debugger-old-buffer)
486 (let ((load-read-function debugger-outer-load-read-function)
487 (overriding-terminal-local-map
488 debugger-outer-overriding-terminal-local-map)
489 (overriding-local-map debugger-outer-overriding-local-map)
490 (track-mouse debugger-outer-track-mouse)
491 (last-command debugger-outer-last-command)
492 (this-command debugger-outer-this-command)
493 (unread-command-events debugger-outer-unread-command-events)
494 (unread-post-input-method-events
495 debugger-outer-unread-post-input-method-events)
496 (last-input-event debugger-outer-last-input-event)
497 (last-command-event debugger-outer-last-command-event)
498 (last-nonmenu-event debugger-outer-last-nonmenu-event)
499 (last-event-frame debugger-outer-last-event-frame)
500 (standard-input debugger-outer-standard-input)
501 (standard-output debugger-outer-standard-output)
502 (inhibit-redisplay debugger-outer-inhibit-redisplay)
503 (cursor-in-echo-area debugger-outer-cursor-in-echo-area))
504 (set-match-data debugger-outer-match-data)
505 (prog1
506 (let ((save-ucc (with-no-warnings unread-command-char)))
507 (unwind-protect
508 (progn
509 (with-no-warnings
510 (setq unread-command-char debugger-outer-unread-command-char))
511 (prog1 (progn ,@body)
512 (with-no-warnings
513 (setq debugger-outer-unread-command-char unread-command-char))))
514 (with-no-warnings
515 (setq unread-command-char save-ucc))))
516 (setq debugger-outer-match-data (match-data))
517 (setq debugger-outer-load-read-function load-read-function)
518 (setq debugger-outer-overriding-terminal-local-map
519 overriding-terminal-local-map)
520 (setq debugger-outer-overriding-local-map overriding-local-map)
521 (setq debugger-outer-track-mouse track-mouse)
522 (setq debugger-outer-last-command last-command)
523 (setq debugger-outer-this-command this-command)
524 (setq debugger-outer-unread-command-events unread-command-events)
525 (setq debugger-outer-unread-post-input-method-events
526 unread-post-input-method-events)
527 (setq debugger-outer-last-input-event last-input-event)
528 (setq debugger-outer-last-command-event last-command-event)
529 (setq debugger-outer-last-nonmenu-event last-nonmenu-event)
530 (setq debugger-outer-last-event-frame last-event-frame)
531 (setq debugger-outer-standard-input standard-input)
532 (setq debugger-outer-standard-output standard-output)
533 (setq debugger-outer-inhibit-redisplay inhibit-redisplay)
534 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area)
535 ))))
536
537 (defun debugger-eval-expression (exp)
538 "Eval an expression, in an environment like that outside the debugger."
539 (interactive
540 (list (read-from-minibuffer "Eval: "
541 nil read-expression-map t
542 'read-expression-history)))
543 (debugger-env-macro (eval-expression exp)))
544 \f
545 (defvar debugger-mode-map
546 (let ((map (make-keymap)))
547 (set-keymap-parent map button-buffer-map)
548 (suppress-keymap map)
549 (define-key map "-" 'negative-argument)
550 (define-key map "b" 'debugger-frame)
551 (define-key map "c" 'debugger-continue)
552 (define-key map "j" 'debugger-jump)
553 (define-key map "r" 'debugger-return-value)
554 (define-key map "u" 'debugger-frame-clear)
555 (define-key map "d" 'debugger-step-through)
556 (define-key map "l" 'debugger-list-functions)
557 (define-key map "h" 'describe-mode)
558 (define-key map "q" 'top-level)
559 (define-key map "e" 'debugger-eval-expression)
560 (define-key map " " 'next-line)
561 (define-key map "R" 'debugger-record-expression)
562 (define-key map "\C-m" 'help-follow)
563 (define-key map [mouse-2] 'push-button)
564 map))
565
566 (defcustom debugger-record-buffer "*Debugger-record*"
567 "*Buffer name for expression values, for \\[debugger-record-expression]."
568 :type 'string
569 :group 'debugger
570 :version "20.3")
571
572 (defun debugger-record-expression (exp)
573 "Display a variable's value and record it in `*Backtrace-record*' buffer."
574 (interactive
575 (list (read-from-minibuffer
576 "Record Eval: "
577 nil
578 read-expression-map t
579 'read-expression-history)))
580 (let* ((buffer (get-buffer-create debugger-record-buffer))
581 (standard-output buffer))
582 (princ (format "Debugger Eval (%s): " exp))
583 (princ (debugger-eval-expression exp))
584 (terpri))
585
586 (with-current-buffer (get-buffer debugger-record-buffer)
587 (message "%s"
588 (buffer-substring (line-beginning-position 0)
589 (line-end-position 0)))))
590
591 (put 'debugger-mode 'mode-class 'special)
592
593 (defun debugger-mode ()
594 "Mode for backtrace buffers, selected in debugger.
595 \\<debugger-mode-map>
596 A line starts with `*' if exiting that frame will call the debugger.
597 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
598
599 When in debugger due to frame being exited,
600 use the \\[debugger-return-value] command to override the value
601 being returned from that frame.
602
603 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
604 which functions will enter the debugger when called.
605
606 Complete list of commands:
607 \\{debugger-mode-map}"
608 (kill-all-local-variables)
609 (setq major-mode 'debugger-mode)
610 (setq mode-name "Debugger")
611 (setq truncate-lines t)
612 (set-syntax-table emacs-lisp-mode-syntax-table)
613 (use-local-map debugger-mode-map)
614 (run-mode-hooks 'debugger-mode-hook))
615 \f
616 ;; When you change this, you may also need to change the number of
617 ;; frames that the debugger skips.
618 (defun implement-debug-on-entry ()
619 "Conditionally call the debugger.
620 A call to this function is inserted by `debug-on-entry' to cause
621 functions to break on entry."
622 (if (or inhibit-debug-on-entry debugger-jumping-flag)
623 nil
624 (funcall debugger 'debug)))
625
626 ;;;###autoload
627 (defun debug-on-entry (function)
628 "Request FUNCTION to invoke debugger each time it is called.
629 If you tell the debugger to continue, FUNCTION's execution proceeds.
630 This works by modifying the definition of FUNCTION,
631 which must be written in Lisp, not predefined.
632 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
633 Redefining FUNCTION also cancels it."
634 (interactive "aDebug on entry (to function): ")
635 (when (and (subrp (symbol-function function))
636 (eq (cdr (subr-arity (symbol-function function))) 'unevalled))
637 (error "Function %s is a special form" function))
638 (if (or (symbolp (symbol-function function))
639 (subrp (symbol-function function)))
640 ;; The function is built-in or aliased to another function.
641 ;; Create a wrapper in which we can add the debug call.
642 (fset function `(lambda (&rest debug-on-entry-args)
643 ,(interactive-form (symbol-function function))
644 (apply ',(symbol-function function)
645 debug-on-entry-args)))
646 (when (eq (car-safe (symbol-function function)) 'autoload)
647 ;; The function is autoloaded. Load its real definition.
648 (load (cadr (symbol-function function)) nil noninteractive nil t))
649 (when (or (not (consp (symbol-function function)))
650 (and (eq (car (symbol-function function)) 'macro)
651 (not (consp (cdr (symbol-function function))))))
652 ;; The function is byte-compiled. Create a wrapper in which
653 ;; we can add the debug call.
654 (debug-convert-byte-code function)))
655 (unless (consp (symbol-function function))
656 (error "Definition of %s is not a list" function))
657 (fset function (debug-on-entry-1 function t))
658 (unless (memq function debug-function-list)
659 (push function debug-function-list))
660 function)
661
662 ;;;###autoload
663 (defun cancel-debug-on-entry (&optional function)
664 "Undo effect of \\[debug-on-entry] on FUNCTION.
665 If argument is nil or an empty string, cancel for all functions."
666 (interactive
667 (list (let ((name
668 (completing-read "Cancel debug on entry (to function): "
669 (mapcar 'symbol-name debug-function-list)
670 nil t nil)))
671 (if name (intern name)))))
672 (if (and function (not (string= function "")))
673 (progn
674 (let ((defn (debug-on-entry-1 function nil)))
675 (condition-case nil
676 (when (and (equal (nth 1 defn) '(&rest debug-on-entry-args))
677 (eq (car (nth 3 defn)) 'apply))
678 ;; `defn' is a wrapper introduced in debug-on-entry.
679 ;; Get rid of it since we don't need it any more.
680 (setq defn (nth 1 (nth 1 (nth 3 defn)))))
681 (error nil))
682 (fset function defn))
683 (setq debug-function-list (delq function debug-function-list))
684 function)
685 (message "Cancelling debug-on-entry for all functions")
686 (mapcar 'cancel-debug-on-entry debug-function-list)))
687
688 (defun debug-convert-byte-code (function)
689 (let* ((defn (symbol-function function))
690 (macro (eq (car-safe defn) 'macro)))
691 (when macro (setq defn (cdr defn)))
692 (unless (consp defn)
693 ;; Assume a compiled code object.
694 (let* ((contents (append defn nil))
695 (body
696 (list (list 'byte-code (nth 1 contents)
697 (nth 2 contents) (nth 3 contents)))))
698 (if (nthcdr 5 contents)
699 (setq body (cons (list 'interactive (nth 5 contents)) body)))
700 (if (nth 4 contents)
701 ;; Use `documentation' here, to get the actual string,
702 ;; in case the compiled function has a reference
703 ;; to the .elc file.
704 (setq body (cons (documentation function) body)))
705 (setq defn (cons 'lambda (cons (car contents) body))))
706 (when macro (setq defn (cons 'macro defn)))
707 (fset function defn))))
708
709 (defun debug-on-entry-1 (function flag)
710 (let* ((defn (symbol-function function))
711 (tail defn))
712 (when (eq (car-safe tail) 'macro)
713 (setq tail (cdr tail)))
714 (if (not (eq (car-safe tail) 'lambda))
715 ;; Only signal an error when we try to set debug-on-entry.
716 ;; When we try to clear debug-on-entry, we are now done.
717 (when flag
718 (error "%s is not a user-defined Lisp function" function))
719 (setq tail (cdr tail))
720 ;; Skip the docstring.
721 (when (and (stringp (cadr tail)) (cddr tail))
722 (setq tail (cdr tail)))
723 ;; Skip the interactive form.
724 (when (eq 'interactive (car-safe (cadr tail)))
725 (setq tail (cdr tail)))
726 (unless (eq flag (equal (cadr tail) '(implement-debug-on-entry)))
727 ;; Add/remove debug statement as needed.
728 (if flag
729 (setcdr tail (cons '(implement-debug-on-entry) (cdr tail)))
730 (setcdr tail (cddr tail)))))
731 defn))
732
733 (defun debugger-list-functions ()
734 "Display a list of all the functions now set to debug on entry."
735 (interactive)
736 (require 'help-mode)
737 (help-setup-xref '(debugger-list-functions) (interactive-p))
738 (with-output-to-temp-buffer (help-buffer)
739 (with-current-buffer standard-output
740 (if (null debug-function-list)
741 (princ "No debug-on-entry functions now\n")
742 (princ "Functions set to debug on entry:\n\n")
743 (dolist (fun debug-function-list)
744 (make-text-button (point) (progn (prin1 fun) (point))
745 'type 'help-function
746 'help-args (list fun))
747 (terpri))
748 (terpri)
749 (princ "Note: if you have redefined a function, then it may no longer\n")
750 (princ "be set to debug on entry, even if it is in the list.")))))
751
752 (provide 'debug)
753
754 ;; arch-tag: b6ec7047-f801-4103-9c63-d69322db9d3b
755 ;;; debug.el ends here