debug.el 36 KB

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