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