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