| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
(require 'button) |
|---|
| 33 |
|
|---|
| 34 |
(defgroup debugger nil |
|---|
| 35 |
"Debuggers and related commands for Emacs." |
|---|
| 36 |
:prefix "debugger-" |
|---|
| 37 |
:group 'debug) |
|---|
| 38 |
|
|---|
| 39 |
(defcustom debugger-mode-hook nil |
|---|
| 40 |
"*Hooks run when `debugger-mode' is turned on." |
|---|
| 41 |
:type 'hook |
|---|
| 42 |
:group 'debugger |
|---|
| 43 |
:version "20.3") |
|---|
| 44 |
|
|---|
| 45 |
(defcustom debugger-batch-max-lines 40 |
|---|
| 46 |
"*Maximum lines to show in debugger buffer in a noninteractive Emacs. |
|---|
| 47 |
When the debugger is entered and Emacs is running in batch mode, |
|---|
| 48 |
if the backtrace text has more than this many lines, |
|---|
| 49 |
the middle is discarded, and just the beginning and end are displayed." |
|---|
| 50 |
:type 'integer |
|---|
| 51 |
:group 'debugger |
|---|
| 52 |
:version "21.1") |
|---|
| 53 |
|
|---|
| 54 |
(defvar debug-function-list nil |
|---|
| 55 |
"List of functions currently set for debug on entry.") |
|---|
| 56 |
|
|---|
| 57 |
(defvar debugger-step-after-exit nil |
|---|
| 58 |
"Non-nil means \"single-step\" after the debugger exits.") |
|---|
| 59 |
|
|---|
| 60 |
(defvar debugger-value nil |
|---|
| 61 |
"This is the value for the debugger to return, when it returns.") |
|---|
| 62 |
|
|---|
| 63 |
(defvar debugger-old-buffer nil |
|---|
| 64 |
"This is the buffer that was current when the debugger was entered.") |
|---|
| 65 |
|
|---|
| 66 |
(defvar debugger-previous-backtrace nil |
|---|
| 67 |
"The contents of the previous backtrace (including text properties). |
|---|
| 68 |
This is to optimize `debugger-make-xrefs'.") |
|---|
| 69 |
|
|---|
| 70 |
(defvar debugger-outer-match-data) |
|---|
| 71 |
(defvar debugger-outer-load-read-function) |
|---|
| 72 |
(defvar debugger-outer-overriding-local-map) |
|---|
| 73 |
(defvar debugger-outer-overriding-terminal-local-map) |
|---|
| 74 |
(defvar debugger-outer-track-mouse) |
|---|
| 75 |
(defvar debugger-outer-last-command) |
|---|
| 76 |
(defvar debugger-outer-this-command) |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
(defvar debugger-outer-unread-command-char) |
|---|
| 81 |
(defvar debugger-outer-unread-command-events) |
|---|
| 82 |
(defvar debugger-outer-unread-post-input-method-events) |
|---|
| 83 |
(defvar debugger-outer-last-input-event) |
|---|
| 84 |
(defvar debugger-outer-last-command-event) |
|---|
| 85 |
(defvar debugger-outer-last-nonmenu-event) |
|---|
| 86 |
(defvar debugger-outer-last-event-frame) |
|---|
| 87 |
(defvar debugger-outer-standard-input) |
|---|
| 88 |
(defvar debugger-outer-standard-output) |
|---|
| 89 |
(defvar debugger-outer-inhibit-redisplay) |
|---|
| 90 |
(defvar debugger-outer-cursor-in-echo-area) |
|---|
| 91 |
(defvar debugger-will-be-back nil |
|---|
| 92 |
"Non-nil if we expect to get back in the debugger soon.") |
|---|
| 93 |
|
|---|
| 94 |
(defvar inhibit-debug-on-entry nil |
|---|
| 95 |
"Non-nil means that debug-on-entry is disabled.") |
|---|
| 96 |
|
|---|
| 97 |
(defvar debugger-jumping-flag nil |
|---|
| 98 |
"Non-nil means that debug-on-entry is disabled. |
|---|
| 99 |
This variable is used by `debugger-jump', `debugger-step-through', |
|---|
| 100 |
and `debugger-reenable' to temporarily disable debug-on-entry.") |
|---|
| 101 |
|
|---|
| 102 |
(defvar inhibit-trace) |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
(setq debugger 'debug) |
|---|
| 106 |
|
|---|
| 107 |
(defun debug (&rest debugger-args) |
|---|
| 108 |
"Enter debugger. To return, type \\<debugger-mode-map>`\\[debugger-continue]'. |
|---|
| 109 |
Arguments are mainly for use when this is called from the internals |
|---|
| 110 |
of the evaluator. |
|---|
| 111 |
|
|---|
| 112 |
You may call with no args, or you may pass nil as the first arg and |
|---|
| 113 |
any other args you like. In that case, the list of args after the |
|---|
| 114 |
first will be printed into the backtrace buffer." |
|---|
| 115 |
(interactive) |
|---|
| 116 |
(if inhibit-redisplay |
|---|
| 117 |
|
|---|
| 118 |
debugger-value |
|---|
| 119 |
(unless noninteractive |
|---|
| 120 |
(message "Entering debugger...")) |
|---|
| 121 |
(let (debugger-value |
|---|
| 122 |
(debug-on-error nil) |
|---|
| 123 |
(debug-on-quit nil) |
|---|
| 124 |
(debugger-buffer (let ((default-major-mode 'fundamental-mode)) |
|---|
| 125 |
(get-buffer-create "*Backtrace*"))) |
|---|
| 126 |
(debugger-old-buffer (current-buffer)) |
|---|
| 127 |
(debugger-step-after-exit nil) |
|---|
| 128 |
(debugger-will-be-back nil) |
|---|
| 129 |
|
|---|
| 130 |
(executing-kbd-macro nil) |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
(debugger-outer-match-data (match-data)) |
|---|
| 134 |
(debugger-outer-load-read-function load-read-function) |
|---|
| 135 |
(debugger-outer-overriding-local-map overriding-local-map) |
|---|
| 136 |
(debugger-outer-overriding-terminal-local-map |
|---|
| 137 |
overriding-terminal-local-map) |
|---|
| 138 |
(debugger-outer-track-mouse track-mouse) |
|---|
| 139 |
(debugger-outer-last-command last-command) |
|---|
| 140 |
(debugger-outer-this-command this-command) |
|---|
| 141 |
(debugger-outer-unread-command-char |
|---|
| 142 |
(with-no-warnings unread-command-char)) |
|---|
| 143 |
(debugger-outer-unread-command-events unread-command-events) |
|---|
| 144 |
(debugger-outer-unread-post-input-method-events |
|---|
| 145 |
unread-post-input-method-events) |
|---|
| 146 |
(debugger-outer-last-input-event last-input-event) |
|---|
| 147 |
(debugger-outer-last-command-event last-command-event) |
|---|
| 148 |
(debugger-outer-last-nonmenu-event last-nonmenu-event) |
|---|
| 149 |
(debugger-outer-last-event-frame last-event-frame) |
|---|
| 150 |
(debugger-outer-standard-input standard-input) |
|---|
| 151 |
(debugger-outer-standard-output standard-output) |
|---|
| 152 |
(debugger-outer-inhibit-redisplay inhibit-redisplay) |
|---|
| 153 |
(debugger-outer-cursor-in-echo-area cursor-in-echo-area) |
|---|
| 154 |
(debugger-with-timeout-suspend (with-timeout-suspend))) |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
(setq overriding-terminal-local-map nil) |
|---|
| 158 |
|
|---|
| 159 |
(let ((last-command nil) this-command track-mouse |
|---|
| 160 |
(inhibit-trace t) |
|---|
| 161 |
(inhibit-debug-on-entry t) |
|---|
| 162 |
unread-command-events |
|---|
| 163 |
unread-post-input-method-events |
|---|
| 164 |
last-input-event last-command-event last-nonmenu-event |
|---|
| 165 |
last-event-frame |
|---|
| 166 |
overriding-local-map |
|---|
| 167 |
load-read-function |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
(enable-recursive-minibuffers |
|---|
| 171 |
(or enable-recursive-minibuffers (> (minibuffer-depth) 0))) |
|---|
| 172 |
(standard-input t) (standard-output t) |
|---|
| 173 |
inhibit-redisplay |
|---|
| 174 |
(cursor-in-echo-area nil)) |
|---|
| 175 |
(unwind-protect |
|---|
| 176 |
(save-excursion |
|---|
| 177 |
(save-window-excursion |
|---|
| 178 |
(with-no-warnings |
|---|
| 179 |
(setq unread-command-char -1)) |
|---|
| 180 |
(when (eq (car debugger-args) 'debug) |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
(backtrace-debug 4 t) |
|---|
| 184 |
|
|---|
| 185 |
(when (eq 'lambda (car-safe (cadr (backtrace-frame 4)))) |
|---|
| 186 |
(backtrace-debug 5 t))) |
|---|
| 187 |
(pop-to-buffer debugger-buffer) |
|---|
| 188 |
(debugger-mode) |
|---|
| 189 |
(debugger-setup-buffer debugger-args) |
|---|
| 190 |
(when noninteractive |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
(when (> (count-lines (point-min) (point-max)) |
|---|
| 194 |
debugger-batch-max-lines) |
|---|
| 195 |
(goto-char (point-min)) |
|---|
| 196 |
(forward-line (/ 2 debugger-batch-max-lines)) |
|---|
| 197 |
(let ((middlestart (point))) |
|---|
| 198 |
(goto-char (point-max)) |
|---|
| 199 |
(forward-line (- (/ 2 debugger-batch-max-lines) |
|---|
| 200 |
debugger-batch-max-lines)) |
|---|
| 201 |
(delete-region middlestart (point))) |
|---|
| 202 |
(insert "...\n")) |
|---|
| 203 |
(goto-char (point-min)) |
|---|
| 204 |
(message "%s" (buffer-string)) |
|---|
| 205 |
(kill-emacs)) |
|---|
| 206 |
(message "") |
|---|
| 207 |
(let ((standard-output nil) |
|---|
| 208 |
(buffer-read-only t)) |
|---|
| 209 |
(message "") |
|---|
| 210 |
|
|---|
| 211 |
(save-excursion |
|---|
| 212 |
(recursive-edit))))) |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
(if (get-buffer-window debugger-buffer 0) |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
(with-current-buffer debugger-buffer |
|---|
| 221 |
(erase-buffer) |
|---|
| 222 |
(fundamental-mode) |
|---|
| 223 |
(with-selected-window (get-buffer-window debugger-buffer 0) |
|---|
| 224 |
(when (and (window-dedicated-p (selected-window)) |
|---|
| 225 |
(not debugger-will-be-back)) |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
(quit-window)))) |
|---|
| 238 |
(kill-buffer debugger-buffer)) |
|---|
| 239 |
(with-timeout-unsuspend debugger-with-timeout-suspend) |
|---|
| 240 |
(set-match-data debugger-outer-match-data))) |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
(setq load-read-function debugger-outer-load-read-function) |
|---|
| 244 |
(setq overriding-local-map debugger-outer-overriding-local-map) |
|---|
| 245 |
(setq overriding-terminal-local-map |
|---|
| 246 |
debugger-outer-overriding-terminal-local-map) |
|---|
| 247 |
(setq track-mouse debugger-outer-track-mouse) |
|---|
| 248 |
(setq last-command debugger-outer-last-command) |
|---|
| 249 |
(setq this-command debugger-outer-this-command) |
|---|
| 250 |
(with-no-warnings |
|---|
| 251 |
(setq unread-command-char debugger-outer-unread-command-char)) |
|---|
| 252 |
(setq unread-command-events debugger-outer-unread-command-events) |
|---|
| 253 |
(setq unread-post-input-method-events |
|---|
| 254 |
debugger-outer-unread-post-input-method-events) |
|---|
| 255 |
(setq last-input-event debugger-outer-last-input-event) |
|---|
| 256 |
(setq last-command-event debugger-outer-last-command-event) |
|---|
| 257 |
(setq last-nonmenu-event debugger-outer-last-nonmenu-event) |
|---|
| 258 |
(setq last-event-frame debugger-outer-last-event-frame) |
|---|
| 259 |
(setq standard-input debugger-outer-standard-input) |
|---|
| 260 |
(setq standard-output debugger-outer-standard-output) |
|---|
| 261 |
(setq inhibit-redisplay debugger-outer-inhibit-redisplay) |
|---|
| 262 |
(setq cursor-in-echo-area debugger-outer-cursor-in-echo-area) |
|---|
| 263 |
(setq debug-on-next-call debugger-step-after-exit) |
|---|
| 264 |
debugger-value))) |
|---|
| 265 |
|
|---|
| 266 |
(defun debugger-setup-buffer (debugger-args) |
|---|
| 267 |
"Initialize the `*Backtrace*' buffer for entry to the debugger. |
|---|
| 268 |
That buffer should be current already." |
|---|
| 269 |
(setq buffer-read-only nil) |
|---|
| 270 |
(erase-buffer) |
|---|
| 271 |
(set-buffer-multibyte nil) |
|---|
| 272 |
(setq buffer-undo-list t) |
|---|
| 273 |
(let ((standard-output (current-buffer)) |
|---|
| 274 |
(print-escape-newlines t) |
|---|
| 275 |
(print-level 8) |
|---|
| 276 |
(print-length 50)) |
|---|
| 277 |
(backtrace)) |
|---|
| 278 |
(goto-char (point-min)) |
|---|
| 279 |
(delete-region (point) |
|---|
| 280 |
(progn |
|---|
| 281 |
(search-forward "\n debug(") |
|---|
| 282 |
(forward-line (if (eq (car debugger-args) 'debug) |
|---|
| 283 |
2 |
|---|
| 284 |
1)) |
|---|
| 285 |
(point))) |
|---|
| 286 |
(insert "Debugger entered") |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
(cond ((memq (car debugger-args) '(lambda debug)) |
|---|
| 290 |
(insert "--entering a function:\n")) |
|---|
| 291 |
|
|---|
| 292 |
((eq (car debugger-args) 'exit) |
|---|
| 293 |
(insert "--returning value: ") |
|---|
| 294 |
(setq debugger-value (nth 1 debugger-args)) |
|---|
| 295 |
(prin1 debugger-value (current-buffer)) |
|---|
| 296 |
(insert ?\n) |
|---|
| 297 |
(delete-char 1) |
|---|
| 298 |
(insert ? ) |
|---|
| 299 |
(beginning-of-line)) |
|---|
| 300 |
|
|---|
| 301 |
((eq (car debugger-args) 'error) |
|---|
| 302 |
(insert "--Lisp error: ") |
|---|
| 303 |
(prin1 (nth 1 debugger-args) (current-buffer)) |
|---|
| 304 |
(insert ?\n)) |
|---|
| 305 |
|
|---|
| 306 |
((eq (car debugger-args) t) |
|---|
| 307 |
(insert "--beginning evaluation of function call form:\n")) |
|---|
| 308 |
|
|---|
| 309 |
(t |
|---|
| 310 |
(insert ": ") |
|---|
| 311 |
(prin1 (if (eq (car debugger-args) 'nil) |
|---|
| 312 |
(cdr debugger-args) debugger-args) |
|---|
| 313 |
(current-buffer)) |
|---|
| 314 |
(insert ?\n))) |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
(save-excursion |
|---|
| 318 |
(let ((tem eval-buffer-list)) |
|---|
| 319 |
(while (and tem |
|---|
| 320 |
(re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t)) |
|---|
| 321 |
(end-of-line) |
|---|
| 322 |
(insert (format " ; Reading at buffer position %d" |
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
(with-current-buffer (car tem) |
|---|
| 327 |
(point)))) |
|---|
| 328 |
(pop tem)))) |
|---|
| 329 |
(debugger-make-xrefs)) |
|---|
| 330 |
|
|---|
| 331 |
(defun debugger-make-xrefs (&optional buffer) |
|---|
| 332 |
"Attach cross-references to function names in the `*Backtrace*' buffer." |
|---|
| 333 |
(interactive "b") |
|---|
| 334 |
(save-excursion |
|---|
| 335 |
(set-buffer (or buffer (current-buffer))) |
|---|
| 336 |
(setq buffer (current-buffer)) |
|---|
| 337 |
(let ((inhibit-read-only t) |
|---|
| 338 |
(old-end (point-min)) (new-end (point-min))) |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
(if debugger-previous-backtrace |
|---|
| 344 |
(let (old-start new-start (all-match t)) |
|---|
| 345 |
(goto-char (point-max)) |
|---|
| 346 |
(with-temp-buffer |
|---|
| 347 |
(insert debugger-previous-backtrace) |
|---|
| 348 |
(while (and all-match (not (bobp))) |
|---|
| 349 |
(setq old-end (point)) |
|---|
| 350 |
(forward-line -1) |
|---|
| 351 |
(setq old-start (point)) |
|---|
| 352 |
(with-current-buffer buffer |
|---|
| 353 |
(setq new-end (point)) |
|---|
| 354 |
(forward-line -1) |
|---|
| 355 |
(setq new-start (point))) |
|---|
| 356 |
(if (not (zerop |
|---|
| 357 |
(let ((case-fold-search nil)) |
|---|
| 358 |
(compare-buffer-substrings |
|---|
| 359 |
(current-buffer) old-start old-end |
|---|
| 360 |
buffer new-start new-end)))) |
|---|
| 361 |
(setq all-match nil)))) |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
(delete-region new-end (point-max)) |
|---|
| 374 |
(goto-char (point-max)) |
|---|
| 375 |
(insert (substring debugger-previous-backtrace |
|---|
| 376 |
(- old-end (point-min)))) |
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
(narrow-to-region (point-min) new-end))) |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
(goto-char (point-min)) |
|---|
| 383 |
(while (progn |
|---|
| 384 |
(goto-char (+ (point) 2)) |
|---|
| 385 |
(skip-syntax-forward "^w_") |
|---|
| 386 |
(not (eobp))) |
|---|
| 387 |
(let* ((beg (point)) |
|---|
| 388 |
(end (progn (skip-syntax-forward "w_") (point))) |
|---|
| 389 |
(sym (intern-soft (buffer-substring-no-properties |
|---|
| 390 |
beg end))) |
|---|
| 391 |
(file (and sym (symbol-file sym 'defun)))) |
|---|
| 392 |
(when file |
|---|
| 393 |
(goto-char beg) |
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
(re-search-forward "\\(\\sw\\|\\s_\\)+") |
|---|
| 397 |
(help-xref-button 0 'help-function-def sym file))) |
|---|
| 398 |
(forward-line 1)) |
|---|
| 399 |
(widen)) |
|---|
| 400 |
(setq debugger-previous-backtrace (buffer-string)))) |
|---|
| 401 |
|
|---|
| 402 |
(defun debugger-step-through () |
|---|
| 403 |
"Proceed, stepping through subexpressions of this expression. |
|---|
| 404 |
Enter another debugger on next entry to eval, apply or funcall." |
|---|
| 405 |
(interactive) |
|---|
| 406 |
(setq debugger-step-after-exit t) |
|---|
| 407 |
(setq debugger-jumping-flag t) |
|---|
| 408 |
(setq debugger-will-be-back t) |
|---|
| 409 |
(add-hook 'post-command-hook 'debugger-reenable) |
|---|
| 410 |
(message "Proceeding, will debug on next eval or call.") |
|---|
| 411 |
(exit-recursive-edit)) |
|---|
| 412 |
|
|---|
| 413 |
(defun debugger-continue () |
|---|
| 414 |
"Continue, evaluating this expression without stopping." |
|---|
| 415 |
(interactive) |
|---|
| 416 |
(unless debugger-may-continue |
|---|
| 417 |
(error "Cannot continue")) |
|---|
| 418 |
(message "Continuing.") |
|---|
| 419 |
(save-excursion |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
(goto-char (point-min)) |
|---|
| 423 |
(if (re-search-forward "^\\* " nil t) |
|---|
| 424 |
(setq debugger-will-be-back t))) |
|---|
| 425 |
(exit-recursive-edit)) |
|---|
| 426 |
|
|---|
| 427 |
(defun debugger-return-value (val) |
|---|
| 428 |
"Continue, specifying value to return. |
|---|
| 429 |
This is only useful when the value returned from the debugger |
|---|
| 430 |
will be used, such as in a debug on exit from a frame." |
|---|
| 431 |
(interactive "XReturn value (evaluated): ") |
|---|
| 432 |
(setq debugger-value val) |
|---|
| 433 |
(princ "Returning " t) |
|---|
| 434 |
(prin1 debugger-value) |
|---|
| 435 |
(save-excursion |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
(goto-char (point-min)) |
|---|
| 439 |
(if (re-search-forward "^\\* " nil t) |
|---|
| 440 |
(setq debugger-will-be-back t))) |
|---|
| 441 |
(exit-recursive-edit)) |
|---|
| 442 |
|
|---|
| 443 |
(defun debugger-jump () |
|---|
| 444 |
"Continue to exit from this frame, with all debug-on-entry suspended." |
|---|
| 445 |
(interactive) |
|---|
| 446 |
(debugger-frame) |
|---|
| 447 |
(setq debugger-jumping-flag t) |
|---|
| 448 |
(add-hook 'post-command-hook 'debugger-reenable) |
|---|
| 449 |
(message "Continuing through this frame") |
|---|
| 450 |
(setq debugger-will-be-back t) |
|---|
| 451 |
(exit-recursive-edit)) |
|---|
| 452 |
|
|---|
| 453 |
(defun debugger-reenable () |
|---|
| 454 |
"Turn all debug-on-entry functions back on. |
|---|
| 455 |
This function is put on `post-command-hook' by `debugger-jump' and |
|---|
| 456 |
removes itself from that hook." |
|---|
| 457 |
(setq debugger-jumping-flag nil) |
|---|
| 458 |
(remove-hook 'post-command-hook 'debugger-reenable)) |
|---|
| 459 |
|
|---|
| 460 |
(defun debugger-frame-number () |
|---|
| 461 |
"Return number of frames in backtrace before the one point points at." |
|---|
| 462 |
(save-excursion |
|---|
| 463 |
(beginning-of-line) |
|---|
| 464 |
(let ((opoint (point)) |
|---|
| 465 |
(count 0)) |
|---|
| 466 |
(while (not (eq (cadr (backtrace-frame count)) 'debug)) |
|---|
| 467 |
(setq count (1+ count))) |
|---|
| 468 |
|
|---|
| 469 |
(when (eq 'implement-debug-on-entry (cadr (backtrace-frame (1+ count)))) |
|---|
| 470 |
(setq count (1+ count))) |
|---|
| 471 |
(goto-char (point-min)) |
|---|
| 472 |
(when (looking-at "Debugger entered--\\(Lisp error\\|returning value\\):") |
|---|
| 473 |
(goto-char (match-end 0)) |
|---|
| 474 |
(forward-sexp 1)) |
|---|
| 475 |
(forward-line 1) |
|---|
| 476 |
(while (progn |
|---|
| 477 |
(forward-char 2) |
|---|
| 478 |
(if (= (following-char) ?\() |
|---|
| 479 |
(forward-sexp 1) |
|---|
| 480 |
(forward-sexp 2)) |
|---|
| 481 |
(forward-line 1) |
|---|
| 482 |
(<= (point) opoint)) |
|---|
| 483 |
(if (looking-at " *;;;") |
|---|
| 484 |
(forward-line 1)) |
|---|
| 485 |
(setq count (1+ count))) |
|---|
| 486 |
count))) |
|---|
| 487 |
|
|---|
| 488 |
(defun debugger-frame () |
|---|
| 489 |
"Request entry to debugger when this frame exits. |
|---|
| 490 |
Applies to the frame whose line point is on in the backtrace." |
|---|
| 491 |
(interactive) |
|---|
| 492 |
(save-excursion |
|---|
| 493 |
(beginning-of-line) |
|---|
| 494 |
(if (looking-at " *;;;\\|[a-z]") |
|---|
| 495 |
(error "This line is not a function call"))) |
|---|
| 496 |
(beginning-of-line) |
|---|
| 497 |
(backtrace-debug (debugger-frame-number) t) |
|---|
| 498 |
(if (= (following-char) ? ) |
|---|
| 499 |
(let ((inhibit-read-only t)) |
|---|
| 500 |
(delete-char 1) |
|---|
| 501 |
(insert ?*))) |
|---|
| 502 |
(beginning-of-line)) |
|---|
| 503 |
|
|---|
| 504 |
(defun debugger-frame-clear () |
|---|
| 505 |
"Do not enter debugger when this frame exits. |
|---|
| 506 |
Applies to the frame whose line point is on in the backtrace." |
|---|
| 507 |
(interactive) |
|---|
| 508 |
(save-excursion |
|---|
| 509 |
(beginning-of-line) |
|---|
| 510 |
(if (looking-at " *;;;\\|[a-z]") |
|---|
| 511 |
(error "This line is not a function call"))) |
|---|
| 512 |
(beginning-of-line) |
|---|
| 513 |
(backtrace-debug (debugger-frame-number) nil) |
|---|
| 514 |
(if (= (following-char) ?*) |
|---|
| 515 |
(let ((inhibit-read-only t)) |
|---|
| 516 |
(delete-char 1) |
|---|
| 517 |
(insert ? ))) |
|---|
| 518 |
(beginning-of-line)) |
|---|
| 519 |
|
|---|
| 520 |
(put 'debugger-env-macro 'lisp-indent-function 0) |
|---|
| 521 |
(defmacro debugger-env-macro (&rest body) |
|---|
| 522 |
"Run BODY in original environment." |
|---|
| 523 |
`(save-excursion |
|---|
| 524 |
(if (null (buffer-name debugger-old-buffer)) |
|---|
| 525 |
|
|---|
| 526 |
(setq debugger-old-buffer (current-buffer))) |
|---|
| 527 |
(set-buffer debugger-old-buffer) |
|---|
| 528 |
(let ((load-read-function debugger-outer-load-read-function) |
|---|
| 529 |
(overriding-terminal-local-map |
|---|
| 530 |
debugger-outer-overriding-terminal-local-map) |
|---|
| 531 |
(overriding-local-map debugger-outer-overriding-local-map) |
|---|
| 532 |
(track-mouse debugger-outer-track-mouse) |
|---|
| 533 |
(last-command debugger-outer-last-command) |
|---|
| 534 |
(this-command debugger-outer-this-command) |
|---|
| 535 |
(unread-command-events debugger-outer-unread-command-events) |
|---|
| 536 |
(unread-post-input-method-events |
|---|
| 537 |
debugger-outer-unread-post-input-method-events) |
|---|
| 538 |
(last-input-event debugger-outer-last-input-event) |
|---|
| 539 |
(last-command-event debugger-outer-last-command-event) |
|---|
| 540 |
(last-nonmenu-event debugger-outer-last-nonmenu-event) |
|---|
| 541 |
(last-event-frame debugger-outer-last-event-frame) |
|---|
| 542 |
(standard-input debugger-outer-standard-input) |
|---|
| 543 |
(standard-output debugger-outer-standard-output) |
|---|
| 544 |
(inhibit-redisplay debugger-outer-inhibit-redisplay) |
|---|
| 545 |
(cursor-in-echo-area debugger-outer-cursor-in-echo-area)) |
|---|
| 546 |
(set-match-data debugger-outer-match-data) |
|---|
| 547 |
(prog1 |
|---|
| 548 |
(let ((save-ucc (with-no-warnings unread-command-char))) |
|---|
| 549 |
(unwind-protect |
|---|
| 550 |
(progn |
|---|
| 551 |
(with-no-warnings |
|---|
| 552 |
(setq unread-command-char debugger-outer-unread-command-char)) |
|---|
| 553 |
(prog1 (progn ,@body) |
|---|
| 554 |
(with-no-warnings |
|---|
| 555 |
(setq debugger-outer-unread-command-char unread-command-char)))) |
|---|
| 556 |
(with-no-warnings |
|---|
| 557 |
(setq unread-command-char save-ucc)))) |
|---|
| 558 |
(setq debugger-outer-match-data (match-data)) |
|---|
| 559 |
(setq debugger-outer-load-read-function load-read-function) |
|---|
| 560 |
(setq debugger-outer-overriding-terminal-local-map |
|---|
| 561 |
overriding-terminal-local-map) |
|---|
| 562 |
(setq debugger-outer-overriding-local-map overriding-local-map) |
|---|
| 563 |
(setq debugger-outer-track-mouse track-mouse) |
|---|
| 564 |
(setq debugger-outer-last-command last-command) |
|---|
| 565 |
(setq debugger-outer-this-command this-command) |
|---|
| 566 |
(setq debugger-outer-unread-command-events unread-command-events) |
|---|
| 567 |
(setq debugger-outer-unread-post-input-method-events |
|---|
| 568 |
unread-post-input-method-events) |
|---|
| 569 |
(setq debugger-outer-last-input-event last-input-event) |
|---|
| 570 |
(setq debugger-outer-last-command-event last-command-event) |
|---|
| 571 |
(setq debugger-outer-last-nonmenu-event last-nonmenu-event) |
|---|
| 572 |
(setq debugger-outer-last-event-frame last-event-frame) |
|---|
| 573 |
(setq debugger-outer-standard-input standard-input) |
|---|
| 574 |
(setq debugger-outer-standard-output standard-output) |
|---|
| 575 |
(setq debugger-outer-inhibit-redisplay inhibit-redisplay) |
|---|
| 576 |
(setq debugger-outer-cursor-in-echo-area cursor-in-echo-area) |
|---|
| 577 |
)))) |
|---|
| 578 |
|
|---|
| 579 |
(defun debugger-eval-expression (exp) |
|---|
| 580 |
"Eval an expression, in an environment like that outside the debugger." |
|---|
| 581 |
(interactive |
|---|
| 582 |
(list (read-from-minibuffer "Eval: " |
|---|
| 583 |
nil read-expression-map t |
|---|
| 584 |
'read-expression-history))) |
|---|
| 585 |
(debugger-env-macro (eval-expression exp))) |
|---|
| 586 |
|
|---|
| 587 |
(defvar debugger-mode-map |
|---|
| 588 |
(let ((map (make-keymap))) |
|---|
| 589 |
(set-keymap-parent map button-buffer-map) |
|---|
| 590 |
(suppress-keymap map) |
|---|
| 591 |
(define-key map "-" 'negative-argument) |
|---|
| 592 |
(define-key map "b" 'debugger-frame) |
|---|
| 593 |
(define-key map "c" 'debugger-continue) |
|---|
| 594 |
(define-key map "j" 'debugger-jump) |
|---|
| 595 |
(define-key map "r" 'debugger-return-value) |
|---|
| 596 |
(define-key map "u" 'debugger-frame-clear) |
|---|
| 597 |
(define-key map "d" 'debugger-step-through) |
|---|
| 598 |
(define-key map "l" 'debugger-list-functions) |
|---|
| 599 |
(define-key map "h" 'describe-mode) |
|---|
| 600 |
(define-key map "q" 'top-level) |
|---|
| 601 |
(define-key map "e" 'debugger-eval-expression) |
|---|
| 602 |
(define-key map " " 'next-line) |
|---|
| 603 |
(define-key map "R" 'debugger-record-expression) |
|---|
| 604 |
(define-key map "\C-m" 'debug-help-follow) |
|---|
| 605 |
(define-key map [mouse-2] 'push-button) |
|---|
| 606 |
map)) |
|---|
| 607 |
|
|---|
| 608 |
(put 'debugger-mode 'mode-class 'special) |
|---|
| 609 |
|
|---|
| 610 |
(defun debugger-mode () |
|---|
| 611 |
"Mode for backtrace buffers, selected in debugger. |
|---|
| 612 |
\\<debugger-mode-map> |
|---|
| 613 |
A line starts with `*' if exiting that frame will call the debugger. |
|---|
| 614 |
Type \\[debugger-frame] or \\[debugger-frame-clear] to set or remove the `*'. |
|---|
| 615 |
|
|---|
| 616 |
When in debugger due to frame being exited, |
|---|
| 617 |
use the \\[debugger-return-value] command to override the value |
|---|
| 618 |
being returned from that frame. |
|---|
| 619 |
|
|---|
| 620 |
Use \\[debug-on-entry] and \\[cancel-debug-on-entry] to control |
|---|
| 621 |
which functions will enter the debugger when called. |
|---|
| 622 |
|
|---|
| 623 |
Complete list of commands: |
|---|
| 624 |
\\{debugger-mode-map}" |
|---|
| 625 |
(kill-all-local-variables) |
|---|
| 626 |
(setq major-mode 'debugger-mode) |
|---|
| 627 |
(setq mode-name "Debugger") |
|---|
| 628 |
(setq truncate-lines t) |
|---|
| 629 |
(set-syntax-table emacs-lisp-mode-syntax-table) |
|---|
| 630 |
(use-local-map debugger-mode-map) |
|---|
| 631 |
(run-mode-hooks 'debugger-mode-hook)) |
|---|
| 632 |
|
|---|
| 633 |
(defcustom debugger-record-buffer "*Debugger-record*" |
|---|
| 634 |
"*Buffer name for expression values, for \\[debugger-record-expression]." |
|---|
| 635 |
:type 'string |
|---|
| 636 |
:group 'debugger |
|---|
| 637 |
:version "20.3") |
|---|
| 638 |
|
|---|
| 639 |
(defun debugger-record-expression (exp) |
|---|
| 640 |
"Display a variable's value and record it in `*Backtrace-record*' buffer." |
|---|
| 641 |
(interactive |
|---|
| 642 |
(list (read-from-minibuffer |
|---|
| 643 |
"Record Eval: " |
|---|
| 644 |
nil |
|---|
| 645 |
read-expression-map t |
|---|
| 646 |
'read-expression-history))) |
|---|
| 647 |
(let* ((buffer (get-buffer-create debugger-record-buffer)) |
|---|
| 648 |
(standard-output buffer)) |
|---|
| 649 |
(princ (format "Debugger Eval (%s): " exp)) |
|---|
| 650 |
(princ (debugger-eval-expression exp)) |
|---|
| 651 |
(terpri)) |
|---|
| 652 |
|
|---|
| 653 |
(with-current-buffer (get-buffer debugger-record-buffer) |
|---|
| 654 |
(message "%s" |
|---|
| 655 |
(buffer-substring (line-beginning-position 0) |
|---|
| 656 |
(line-end-position 0))))) |
|---|
| 657 |
|
|---|
| 658 |
(defun debug-help-follow (&optional pos) |
|---|
| 659 |
"Follow cross-reference at POS, defaulting to point. |
|---|
| 660 |
|
|---|
| 661 |
For the cross-reference format, see `help-make-xrefs'." |
|---|
| 662 |
(interactive "d") |
|---|
| 663 |
(require 'help-mode) |
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
(unless pos |
|---|
| 668 |
(setq pos (point))) |
|---|
| 669 |
(unless (push-button pos) |
|---|
| 670 |
|
|---|
| 671 |
(let ((sym |
|---|
| 672 |
(intern |
|---|
| 673 |
(save-excursion |
|---|
| 674 |
(goto-char pos) (skip-syntax-backward "w_") |
|---|
| 675 |
(buffer-substring (point) |
|---|
| 676 |
(progn (skip-syntax-forward "w_") |
|---|
| 677 |
(point))))))) |
|---|
| 678 |
(when (or (boundp sym) (fboundp sym) (facep sym)) |
|---|
| 679 |
(help-xref-interned sym))))) |
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
(defun implement-debug-on-entry () |
|---|
| 684 |
"Conditionally call the debugger. |
|---|
| 685 |
A call to this function is inserted by `debug-on-entry' to cause |
|---|
| 686 |
functions to break on entry." |
|---|
| 687 |
(if (or inhibit-debug-on-entry debugger-jumping-flag) |
|---|
| 688 |
nil |
|---|
| 689 |
(funcall debugger 'debug))) |
|---|
| 690 |
|
|---|
| 691 |
(defun debugger-special-form-p (symbol) |
|---|
| 692 |
"Return whether SYMBOL is a special form." |
|---|
| 693 |
(and (fboundp symbol) |
|---|
| 694 |
(subrp (symbol-function symbol)) |
|---|
| 695 |
(eq (cdr (subr-arity (symbol-function symbol))) 'unevalled))) |
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
(defun debug-on-entry (function) |
|---|
| 699 |
"Request FUNCTION to invoke debugger each time it is called. |
|---|
| 700 |
|
|---|
| 701 |
When called interactively, prompt for FUNCTION in the minibuffer. |
|---|
| 702 |
|
|---|
| 703 |
This works by modifying the definition of FUNCTION. If you tell the |
|---|
| 704 |
debugger to continue, FUNCTION's execution proceeds. If FUNCTION is a |
|---|
| 705 |
normal function or a macro written in Lisp, you can also step through |
|---|
| 706 |
its execution. FUNCTION can also be a primitive that is not a special |
|---|
| 707 |
form, in which case stepping is not possible. Break-on-entry for |
|---|
| 708 |
primitive functions only works when that function is called from Lisp. |
|---|
| 709 |
|
|---|
| 710 |
Use \\[cancel-debug-on-entry] to cancel the effect of this command. |
|---|
| 711 |
Redefining FUNCTION also cancels it." |
|---|
| 712 |
(interactive |
|---|
| 713 |
(let ((fn (function-called-at-point)) val) |
|---|
| 714 |
(when (debugger-special-form-p fn) |
|---|
| 715 |
(setq fn nil)) |
|---|
| 716 |
(setq val (completing-read |
|---|
| 717 |
(if fn |
|---|
| 718 |
(format "Debug on entry to function (default %s): " fn) |
|---|
| 719 |
"Debug on entry to function: ") |
|---|
| 720 |
obarray |
|---|
| 721 |
#'(lambda (symbol) |
|---|
| 722 |
(and (fboundp symbol) |
|---|
| 723 |
(not (debugger-special-form-p symbol)))) |
|---|
| 724 |
t nil nil (symbol-name fn))) |
|---|
| 725 |
(list (if (equal val "") fn (intern val))))) |
|---|
| 726 |
(when (debugger-special-form-p function) |
|---|
| 727 |
(error "Function %s is a special form" function)) |
|---|
| 728 |
(if (or (symbolp (symbol-function function)) |
|---|
| 729 |
(subrp (symbol-function function))) |
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
(fset function `(lambda (&rest debug-on-entry-args) |
|---|
| 733 |
,(interactive-form (symbol-function function)) |
|---|
| 734 |
(apply ',(symbol-function function) |
|---|
| 735 |
debug-on-entry-args))) |
|---|
| 736 |
(when (eq (car-safe (symbol-function function)) 'autoload) |
|---|
| 737 |
|
|---|
| 738 |
(load (cadr (symbol-function function)) nil noninteractive nil t)) |
|---|
| 739 |
(when (or (not (consp (symbol-function function))) |
|---|
| 740 |
(and (eq (car (symbol-function function)) 'macro) |
|---|
| 741 |
(not (consp (cdr (symbol-function function)))))) |
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
(debug-convert-byte-code function))) |
|---|
| 745 |
(unless (consp (symbol-function function)) |
|---|
| 746 |
(error "Definition of %s is not a list" function)) |
|---|
| 747 |
(fset function (debug-on-entry-1 function t)) |
|---|
| 748 |
(unless (memq function debug-function-list) |
|---|
| 749 |
(push function debug-function-list)) |
|---|
| 750 |
function) |
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
(defun cancel-debug-on-entry (&optional function) |
|---|
| 754 |
"Undo effect of \\[debug-on-entry] on FUNCTION. |
|---|
| 755 |
If FUNCTION is nil, cancel debug-on-entry for all functions. |
|---|
| 756 |
When called interactively, prompt for FUNCTION in the minibuffer. |
|---|
| 757 |
To specify a nil argument interactively, exit with an empty minibuffer." |
|---|
| 758 |
(interactive |
|---|
| 759 |
(list (let ((name |
|---|
| 760 |
(completing-read |
|---|
| 761 |
"Cancel debug on entry to function (default all functions): " |
|---|
| 762 |
(mapcar 'symbol-name debug-function-list) nil t))) |
|---|
| 763 |
(when name |
|---|
| 764 |
(unless (string= name "") |
|---|
| 765 |
(intern name)))))) |
|---|
| 766 |
(if (and function |
|---|
| 767 |
(not (string= function ""))) |
|---|
| 768 |
(progn |
|---|
| 769 |
(let ((defn (debug-on-entry-1 function nil))) |
|---|
| 770 |
(condition-case nil |
|---|
| 771 |
(when (and (equal (nth 1 defn) '(&rest debug-on-entry-args)) |
|---|
| 772 |
(eq (car (nth 3 defn)) 'apply)) |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
(setq defn (nth 1 (nth 1 (nth 3 defn))))) |
|---|
| 776 |
(error nil)) |
|---|
| 777 |
(fset function defn)) |
|---|
| 778 |
(setq debug-function-list (delq function debug-function-list)) |
|---|
| 779 |
function) |
|---|
| 780 |
(message "Cancelling debug-on-entry for all functions") |
|---|
| 781 |
(mapcar 'cancel-debug-on-entry debug-function-list))) |
|---|
| 782 |
|
|---|
| 783 |
(defun debug-convert-byte-code (function) |
|---|
| 784 |
(let* ((defn (symbol-function function)) |
|---|
| 785 |
(macro (eq (car-safe defn) 'macro))) |
|---|
| 786 |
(when macro (setq defn (cdr defn))) |
|---|
| 787 |
(unless (consp defn) |
|---|
| 788 |
|
|---|
| 789 |
(let* ((contents (append defn nil)) |
|---|
| 790 |
(body |
|---|
| 791 |
(list (list 'byte-code (nth 1 contents) |
|---|
| 792 |
(nth 2 contents) (nth 3 contents))))) |
|---|
| 793 |
(if (nthcdr 5 contents) |
|---|
| 794 |
(setq body (cons (list 'interactive (nth 5 contents)) body))) |
|---|
| 795 |
(if (nth 4 contents) |
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
(setq body (cons (documentation function) body))) |
|---|
| 800 |
(setq defn (cons 'lambda (cons (car contents) body)))) |
|---|
| 801 |
(when macro (setq defn (cons 'macro defn))) |
|---|
| 802 |
(fset function defn)))) |
|---|
| 803 |
|
|---|
| 804 |
(defun debug-on-entry-1 (function flag) |
|---|
| 805 |
(let* ((defn (symbol-function function)) |
|---|
| 806 |
(tail defn)) |
|---|
| 807 |
(when (eq (car-safe tail) 'macro) |
|---|
| 808 |
(setq tail (cdr tail))) |
|---|
| 809 |
(if (not (eq (car-safe tail) 'lambda)) |
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
(when flag |
|---|
| 813 |
(error "%s is not a user-defined Lisp function" function)) |
|---|
| 814 |
(setq tail (cdr tail)) |
|---|
| 815 |
|
|---|
| 816 |
(when (and (stringp (cadr tail)) (cddr tail)) |
|---|
|
|---|