]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/debug.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-24
[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 (pop-to-buffer debugger-buffer)
175 (debugger-mode)
176 (debugger-setup-buffer debugger-args)
177 (when noninteractive
178 ;; If the backtrace is long, save the beginning
179 ;; and the end, but discard the middle.
180 (when (> (count-lines (point-min) (point-max))
181 debugger-batch-max-lines)
182 (goto-char (point-min))
183 (forward-line (/ 2 debugger-batch-max-lines))
184 (let ((middlestart (point)))
185 (goto-char (point-max))
186 (forward-line (- (/ 2 debugger-batch-max-lines)
187 debugger-batch-max-lines))
188 (delete-region middlestart (point)))
189 (insert "...\n"))
190 (goto-char (point-min))
191 (message "%s" (buffer-string))
192 (kill-emacs))
193 (if (eq (car debugger-args) 'debug)
194 ;; Skip the frames for backtrace-debug, byte-code,
195 ;; and implement-debug-on-entry.
196 (backtrace-debug 4 t))
197 (message "")
198 (let ((standard-output nil)
199 (buffer-read-only t))
200 (message "")
201 ;; Make sure we unbind buffer-read-only in the right buffer.
202 (save-excursion
203 (recursive-edit)))))
204 ;; Kill or at least neuter the backtrace buffer, so that users
205 ;; don't try to execute debugger commands in an invalid context.
206 (if (get-buffer-window debugger-buffer 0)
207 ;; Still visible despite the save-window-excursion? Maybe it
208 ;; it's in a pop-up frame. It would be annoying to delete and
209 ;; recreate it every time the debugger stops, so instead we'll
210 ;; erase it and hide it but keep it alive.
211 (with-current-buffer debugger-buffer
212 (erase-buffer)
213 (fundamental-mode)
214 (with-selected-window (get-buffer-window debugger-buffer 0)
215 (bury-buffer)))
216 (kill-buffer debugger-buffer))
217 (set-match-data debugger-outer-match-data)))
218 ;; Put into effect the modified values of these variables
219 ;; in case the user set them with the `e' command.
220 (setq load-read-function debugger-outer-load-read-function)
221 (setq overriding-local-map debugger-outer-overriding-local-map)
222 (setq overriding-terminal-local-map
223 debugger-outer-overriding-terminal-local-map)
224 (setq track-mouse debugger-outer-track-mouse)
225 (setq last-command debugger-outer-last-command)
226 (setq this-command debugger-outer-this-command)
227 (with-no-warnings
228 (setq unread-command-char debugger-outer-unread-command-char))
229 (setq unread-command-events debugger-outer-unread-command-events)
230 (setq unread-post-input-method-events
231 debugger-outer-unread-post-input-method-events)
232 (setq last-input-event debugger-outer-last-input-event)
233 (setq last-command-event debugger-outer-last-command-event)
234 (setq last-nonmenu-event debugger-outer-last-nonmenu-event)
235 (setq last-event-frame debugger-outer-last-event-frame)
236 (setq standard-input debugger-outer-standard-input)
237 (setq standard-output debugger-outer-standard-output)
238 (setq inhibit-redisplay debugger-outer-inhibit-redisplay)
239 (setq cursor-in-echo-area debugger-outer-cursor-in-echo-area)
240 (setq debug-on-next-call debugger-step-after-exit)
241 debugger-value)))
242 \f
243 (defun debugger-setup-buffer (debugger-args)
244 "Initialize the `*Backtrace*' buffer for entry to the debugger.
245 That buffer should be current already."
246 (setq buffer-read-only nil)
247 (erase-buffer)
248 (set-buffer-multibyte nil)
249 (let ((standard-output (current-buffer))
250 (print-escape-newlines t)
251 (print-level 8)
252 (print-length 50))
253 (backtrace))
254 (goto-char (point-min))
255 (delete-region (point)
256 (progn
257 (search-forward "\n debug(")
258 (forward-line (if (eq (car debugger-args) 'debug)
259 2 ; Remove implement-debug-on-entry frame.
260 1))
261 (point)))
262 (insert "Debugger entered")
263 ;; lambda is for debug-on-call when a function call is next.
264 ;; debug is for debug-on-entry function called.
265 (cond ((memq (car debugger-args) '(lambda debug))
266 (insert "--entering a function:\n")
267 (if (eq (car debugger-args) 'debug)
268 (progn
269 (delete-char 1)
270 (insert ?*)
271 (beginning-of-line))))
272 ;; Exiting a function.
273 ((eq (car debugger-args) 'exit)
274 (insert "--returning value: ")
275 (setq debugger-value (nth 1 debugger-args))
276 (prin1 debugger-value (current-buffer))
277 (insert ?\n)
278 (delete-char 1)
279 (insert ? )
280 (beginning-of-line))
281 ;; Debugger entered for an error.
282 ((eq (car debugger-args) 'error)
283 (insert "--Lisp error: ")
284 (prin1 (nth 1 debugger-args) (current-buffer))
285 (insert ?\n))
286 ;; debug-on-call, when the next thing is an eval.
287 ((eq (car debugger-args) t)
288 (insert "--beginning evaluation of function call form:\n"))
289 ;; User calls debug directly.
290 (t
291 (insert ": ")
292 (prin1 (if (eq (car debugger-args) 'nil)
293 (cdr debugger-args) debugger-args)
294 (current-buffer))
295 (insert ?\n)))
296 ;; After any frame that uses eval-buffer,
297 ;; insert a line that states the buffer position it's reading at.
298 (save-excursion
299 (while (re-search-forward "^ eval-buffer(" nil t)
300 (end-of-line)
301 (insert (format "\n ;;; Reading at buffer position %d"
302 (with-current-buffer (nth 2 (backtrace-frame (debugger-frame-number)))
303 (point))))))
304 (debugger-make-xrefs))
305
306 (defun debugger-make-xrefs (&optional buffer)
307 "Attach cross-references to symbol names in the `*Backtrace*' buffer."
308 (interactive "b")
309 (save-excursion
310 (set-buffer (or buffer (current-buffer)))
311 (setq buffer (current-buffer))
312 (let ((buffer-read-only nil)
313 (old-end (point-min)) (new-end (point-min)))
314 ;; If we saved an old backtrace, find the common part
315 ;; between the new and the old.
316 ;; Compare line by line, starting from the end,
317 ;; because that's the part that is likely to be unchanged.
318 (if debugger-previous-backtrace
319 (let (old-start new-start (all-match t))
320 (goto-char (point-max))
321 (with-temp-buffer
322 (insert debugger-previous-backtrace)
323 (while (and all-match (not (bobp)))
324 (setq old-end (point))
325 (forward-line -1)
326 (setq old-start (point))
327 (with-current-buffer buffer
328 (setq new-end (point))
329 (forward-line -1)
330 (setq new-start (point)))
331 (if (not (zerop
332 (compare-buffer-substrings
333 (current-buffer) old-start old-end
334 buffer new-start new-end)))
335 (setq all-match nil))))
336 ;; Now new-end is the position of the start of the
337 ;; unchanged part in the current buffer, and old-end is
338 ;; the position of that same text in the saved old
339 ;; backtrace. But we must subtract (point-min) since strings are
340 ;; indexed in origin 0.
341
342 ;; Replace the unchanged part of the backtrace
343 ;; with the text from debugger-previous-backtrace,
344 ;; since that already has the proper xrefs.
345 ;; With this optimization, we only need to scan
346 ;; the changed part of the backtrace.
347 (delete-region new-end (point-max))
348 (goto-char (point-max))
349 (insert (substring debugger-previous-backtrace
350 (- old-end (point-min))))
351 ;; Make the unchanged part of the backtrace inaccessible
352 ;; so it won't be scanned.
353 (narrow-to-region (point-min) new-end)))
354
355 ;; Scan the new part of the backtrace, inserting xrefs.
356 (goto-char (point-min))
357 (while (progn
358 (skip-syntax-forward "^w_")
359 (not (eobp)))
360 (let* ((beg (point))
361 (end (progn (skip-syntax-forward "w_") (point)))
362 (sym (intern-soft (buffer-substring-no-properties
363 beg end)))
364 (file (and sym (symbol-file sym 'defun))))
365 (when file
366 (goto-char beg)
367 ;; help-xref-button needs to operate on something matched
368 ;; by a regexp, so set that up for it.
369 (re-search-forward "\\(\\(\\sw\\|\\s_\\)+\\)")
370 (help-xref-button 1 'help-function-def sym file)))
371 (forward-line 1))
372 (widen))
373 (setq debugger-previous-backtrace (buffer-string))))
374 \f
375 (defun debugger-step-through ()
376 "Proceed, stepping through subexpressions of this expression.
377 Enter another debugger on next entry to eval, apply or funcall."
378 (interactive)
379 (setq debugger-step-after-exit t)
380 (setq debugger-jumping-flag t)
381 (add-hook 'post-command-hook 'debugger-reenable)
382 (message "Proceeding, will debug on next eval or call.")
383 (exit-recursive-edit))
384
385 (defun debugger-continue ()
386 "Continue, evaluating this expression without stopping."
387 (interactive)
388 (unless debugger-may-continue
389 (error "Cannot continue"))
390 (message "Continuing.")
391 (exit-recursive-edit))
392
393 (defun debugger-return-value (val)
394 "Continue, specifying value to return.
395 This is only useful when the value returned from the debugger
396 will be used, such as in a debug on exit from a frame."
397 (interactive "XReturn value (evaluated): ")
398 (setq debugger-value val)
399 (princ "Returning " t)
400 (prin1 debugger-value)
401 (exit-recursive-edit))
402
403 (defun debugger-jump ()
404 "Continue to exit from this frame, with all debug-on-entry suspended."
405 (interactive)
406 (debugger-frame)
407 (setq debugger-jumping-flag t)
408 (add-hook 'post-command-hook 'debugger-reenable)
409 (message "Continuing through this frame")
410 (exit-recursive-edit))
411
412 (defun debugger-reenable ()
413 "Turn all debug-on-entry functions back on.
414 This function is put on `post-command-hook' by `debugger-jump' and
415 removes itself from that hook."
416 (setq debugger-jumping-flag nil)
417 (remove-hook 'post-command-hook 'debugger-reenable))
418
419 (defun debugger-frame-number ()
420 "Return number of frames in backtrace before the one point points at."
421 (save-excursion
422 (beginning-of-line)
423 (let ((opoint (point))
424 (count 0))
425 (while (not (eq (cadr (backtrace-frame count)) 'debug))
426 (setq count (1+ count)))
427 ;; Skip implement-debug-on-entry frame.
428 (when (eq 'implement-debug-on-entry (cadr (backtrace-frame (1+ count))))
429 (setq count (1+ count)))
430 (goto-char (point-min))
431 (when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):")
432 (goto-char (match-end 0))
433 (forward-sexp 1))
434 (forward-line 1)
435 (while (progn
436 (forward-char 2)
437 (if (= (following-char) ?\()
438 (forward-sexp 1)
439 (forward-sexp 2))
440 (forward-line 1)
441 (<= (point) opoint))
442 (if (looking-at " *;;;")
443 (forward-line 1))
444 (setq count (1+ count)))
445 count)))
446
447 (defun debugger-frame ()
448 "Request entry to debugger when this frame exits.
449 Applies to the frame whose line point is on in the backtrace."
450 (interactive)
451 (save-excursion
452 (beginning-of-line)
453 (if (looking-at " *;;;\\|[a-z]")
454 (error "This line is not a function call")))
455 (beginning-of-line)
456 (backtrace-debug (debugger-frame-number) t)
457 (if (= (following-char) ? )
458 (let ((buffer-read-only nil))
459 (delete-char 1)
460 (insert ?*)))
461 (beginning-of-line))
462
463 (defun debugger-frame-clear ()
464 "Do not enter debugger when this frame exits.
465 Applies to the frame whose line point is on in the backtrace."
466 (interactive)
467 (save-excursion
468 (beginning-of-line)
469 (if (looking-at " *;;;\\|[a-z]")
470 (error "This line is not a function call")))
471 (beginning-of-line)
472 (backtrace-debug (debugger-frame-number) nil)
473 (if (= (following-char) ?*)
474 (let ((buffer-read-only nil))
475 (delete-char 1)
476 (insert ? )))
477 (beginning-of-line))
478
479 (put 'debugger-env-macro 'lisp-indent-function 0)
480 (defmacro debugger-env-macro (&rest body)
481 "Run BODY in original environment."
482 `(save-excursion
483 (if (null (buffer-name debugger-old-buffer))
484 ;; old buffer deleted
485 (setq debugger-old-buffer (current-buffer)))
486 (set-buffer debugger-old-buffer)
487 (let ((load-read-function debugger-outer-load-read-function)
488 (overriding-terminal-local-map
489 debugger-outer-overriding-terminal-local-map)
490 (overriding-local-map debugger-outer-overriding-local-map)
491 (track-mouse debugger-outer-track-mouse)
492 (last-command debugger-outer-last-command)
493 (this-command debugger-outer-this-command)
494 (unread-command-events debugger-outer-unread-command-events)
495 (unread-post-input-method-events
496 debugger-outer-unread-post-input-method-events)
497 (last-input-event debugger-outer-last-input-event)
498 (last-command-event debugger-outer-last-command-event)
499 (last-nonmenu-event debugger-outer-last-nonmenu-event)
500 (last-event-frame debugger-outer-last-event-frame)
501 (standard-input debugger-outer-standard-input)
502 (standard-output debugger-outer-standard-output)
503 (inhibit-redisplay debugger-outer-inhibit-redisplay)
504 (cursor-in-echo-area debugger-outer-cursor-in-echo-area))
505 (set-match-data debugger-outer-match-data)
506 (prog1
507 (let ((save-ucc (with-no-warnings unread-command-char)))
508 (unwind-protect
509 (progn
510 (with-no-warnings
511 (setq unread-command-char debugger-outer-unread-command-char))
512 (prog1 (progn ,@body)
513 (with-no-warnings
514 (setq debugger-outer-unread-command-char unread-command-char))))
515 (with-no-warnings
516 (setq unread-command-char save-ucc))))
517 (setq debugger-outer-match-data (match-data))
518 (setq debugger-outer-load-read-function load-read-function)
519 (setq debugger-outer-overriding-terminal-local-map
520 overriding-terminal-local-map)
521 (setq debugger-outer-overriding-local-map overriding-local-map)
522 (setq debugger-outer-track-mouse track-mouse)
523 (setq debugger-outer-last-command last-command)
524 (setq debugger-outer-this-command this-command)
525 (setq debugger-outer-unread-command-events unread-command-events)
526 (setq debugger-outer-unread-post-input-method-events
527 unread-post-input-method-events)
528 (setq debugger-outer-last-input-event last-input-event)
529 (setq debugger-outer-last-command-event last-command-event)
530 (setq debugger-outer-last-nonmenu-event last-nonmenu-event)
531 (setq debugger-outer-last-event-frame last-event-frame)
532 (setq debugger-outer-standard-input standard-input)
533 (setq debugger-outer-standard-output standard-output)
534 (setq debugger-outer-inhibit-redisplay inhibit-redisplay)
535 (setq debugger-outer-cursor-in-echo-area cursor-in-echo-area)
536 ))))
537
538 (defun debugger-eval-expression (exp)
539 "Eval an expression, in an environment like that outside the debugger."
540 (interactive
541 (list (read-from-minibuffer "Eval: "
542 nil read-expression-map t
543 'read-expression-history)))
544 (debugger-env-macro (eval-expression exp)))
545 \f
546 (defvar debugger-mode-map
547 (let ((map (make-keymap)))
548 (set-keymap-parent map button-buffer-map)
549 (suppress-keymap map)
550 (define-key map "-" 'negative-argument)
551 (define-key map "b" 'debugger-frame)
552 (define-key map "c" 'debugger-continue)
553 (define-key map "j" 'debugger-jump)
554 (define-key map "r" 'debugger-return-value)
555 (define-key map "u" 'debugger-frame-clear)
556 (define-key map "d" 'debugger-step-through)
557 (define-key map "l" 'debugger-list-functions)
558 (define-key map "h" 'describe-mode)
559 (define-key map "q" 'top-level)
560 (define-key map "e" 'debugger-eval-expression)
561 (define-key map " " 'next-line)
562 (define-key map "R" 'debugger-record-expression)
563 (define-key map "\C-m" 'help-follow)
564 (define-key map [mouse-2] 'push-button)
565 map))
566
567 (defcustom debugger-record-buffer "*Debugger-record*"
568 "*Buffer name for expression values, for \\[debugger-record-expression]."
569 :type 'string
570 :group 'debugger
571 :version "20.3")
572
573 (defun debugger-record-expression (exp)
574 "Display a variable's value and record it in `*Backtrace-record*' buffer."
575 (interactive
576 (list (read-from-minibuffer
577 "Record Eval: "
578 nil
579 read-expression-map t
580 'read-expression-history)))
581 (let* ((buffer (get-buffer-create debugger-record-buffer))
582 (standard-output buffer))
583 (princ (format "Debugger Eval (%s): " exp))
584 (princ (debugger-eval-expression exp))
585 (terpri))
586
587 (with-current-buffer (get-buffer debugger-record-buffer)
588 (save-excursion
589 (forward-line -1)
590 (message
591 (buffer-substring (point) (progn (end-of-line) (point)))))))
592
593 (put 'debugger-mode 'mode-class 'special)
594
595 (defun debugger-mode ()
596 "Mode for backtrace buffers, selected in debugger.
597 \\<debugger-mode-map>
598 A line starts with `*' if exiting that frame will call the debugger.
599 Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'.
600
601 When in debugger due to frame being exited,
602 use the \\[debugger-return-value] command to override the value
603 being returned from that frame.
604
605 Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control
606 which functions will enter the debugger when called.
607
608 Complete list of commands:
609 \\{debugger-mode-map}"
610 (kill-all-local-variables)
611 (setq major-mode 'debugger-mode)
612 (setq mode-name "Debugger")
613 (setq truncate-lines t)
614 (set-syntax-table emacs-lisp-mode-syntax-table)
615 (use-local-map debugger-mode-map)
616 (run-mode-hooks 'debugger-mode-hook))
617 \f
618 ;; When you change this, you may also need to change the number of
619 ;; frames that the debugger skips.
620 (defun implement-debug-on-entry ()
621 "Conditionally call the debugger.
622 A call to this function is inserted by `debug-on-entry' to cause
623 functions to break on entry."
624 (if (or inhibit-debug-on-entry debugger-jumping-flag)
625 nil
626 (funcall debugger 'debug)))
627
628 ;;;###autoload
629 (defun debug-on-entry (function)
630 "Request FUNCTION to invoke debugger each time it is called.
631 If you tell the debugger to continue, FUNCTION's execution proceeds.
632 This works by modifying the definition of FUNCTION,
633 which must be written in Lisp, not predefined.
634 Use \\[cancel-debug-on-entry] to cancel the effect of this command.
635 Redefining FUNCTION also cancels it."
636 (interactive "aDebug on entry (to function): ")
637 ;; Handle a function that has been aliased to some other function.
638 (if (and (subrp (symbol-function function))
639 (eq (cdr (subr-arity (symbol-function function))) 'unevalled))
640 (error "Function %s is a special form" function))
641 (if (or (symbolp (symbol-function function))
642 (subrp (symbol-function function)))
643 ;; Create a wrapper in which we can then add the necessary debug call.
644 (fset function `(lambda (&rest debug-on-entry-args)
645 ,(interactive-form (symbol-function function))
646 (apply ',(symbol-function function)
647 debug-on-entry-args))))
648 (or (consp (symbol-function function))
649 (debug-convert-byte-code function))
650 (or (consp (symbol-function function))
651 (error "Definition of %s is not a list" function))
652 (fset function (debug-on-entry-1 function t))
653 (or (memq function debug-function-list)
654 (push function debug-function-list))
655 function)
656
657 ;;;###autoload
658 (defun cancel-debug-on-entry (&optional function)
659 "Undo effect of \\[debug-on-entry] on FUNCTION.
660 If argument is nil or an empty string, cancel for all functions."
661 (interactive
662 (list (let ((name
663 (completing-read "Cancel debug on entry (to function): "
664 (mapcar 'symbol-name debug-function-list)
665 nil t nil)))
666 (if name (intern name)))))
667 (if (and function (not (string= function "")))
668 (progn
669 (let ((f (debug-on-entry-1 function nil)))
670 (condition-case nil
671 (if (and (equal (nth 1 f) '(&rest debug-on-entry-args))
672 (eq (car (nth 3 f)) 'apply))
673 ;; `f' is a wrapper introduced in debug-on-entry.
674 ;; Get rid of it since we don't need it any more.
675 (setq f (nth 1 (nth 1 (nth 3 f)))))
676 (error nil))
677 (fset function f))
678 (setq debug-function-list (delq function debug-function-list))
679 function)
680 (message "Cancelling debug-on-entry for all functions")
681 (mapcar 'cancel-debug-on-entry debug-function-list)))
682
683 (defun debug-convert-byte-code (function)
684 (let ((defn (symbol-function function)))
685 (if (not (consp defn))
686 ;; Assume a compiled code object.
687 (let* ((contents (append defn nil))
688 (body
689 (list (list 'byte-code (nth 1 contents)
690 (nth 2 contents) (nth 3 contents)))))
691 (if (nthcdr 5 contents)
692 (setq body (cons (list 'interactive (nth 5 contents)) body)))
693 (if (nth 4 contents)
694 ;; Use `documentation' here, to get the actual string,
695 ;; in case the compiled function has a reference
696 ;; to the .elc file.
697 (setq body (cons (documentation function) body)))
698 (fset function (cons 'lambda (cons (car contents) body)))))))
699
700 (defun debug-on-entry-1 (function flag)
701 (let* ((defn (symbol-function function))
702 (tail defn))
703 (if (subrp tail)
704 (error "%s is a built-in function" function)
705 (if (eq (car tail) 'macro) (setq tail (cdr tail)))
706 (if (eq (car tail) 'lambda) (setq tail (cdr tail))
707 (error "%s not user-defined Lisp function" function))
708 ;; Skip the docstring.
709 (when (and (stringp (cadr tail)) (cddr tail))
710 (setq tail (cdr tail)))
711 ;; Skip the interactive form.
712 (when (eq 'interactive (car-safe (cadr tail)))
713 (setq tail (cdr tail)))
714 (unless (eq flag (equal (cadr tail) '(implement-debug-on-entry)))
715 ;; Add/remove debug statement as needed.
716 (if flag
717 (setcdr tail (cons '(implement-debug-on-entry) (cdr tail)))
718 (setcdr tail (cddr tail))))
719 defn)))
720
721 (defun debugger-list-functions ()
722 "Display a list of all the functions now set to debug on entry."
723 (interactive)
724 (require 'help-mode)
725 (help-setup-xref '(debugger-list-functions) (interactive-p))
726 (with-output-to-temp-buffer (help-buffer)
727 (with-current-buffer standard-output
728 (if (null debug-function-list)
729 (princ "No debug-on-entry functions now\n")
730 (princ "Functions set to debug on entry:\n\n")
731 (dolist (fun debug-function-list)
732 (make-text-button (point) (progn (prin1 fun) (point))
733 'type 'help-function
734 'help-args (list fun))
735 (terpri))
736 (terpri)
737 (princ "Note: if you have redefined a function, then it may no longer\n")
738 (princ "be set to debug on entry, even if it is in the list.")))))
739
740 (provide 'debug)
741
742 ;; arch-tag: b6ec7047-f801-4103-9c63-d69322db9d3b
743 ;;; debug.el ends here