Emacs provides a good amount of debugging facilities including M-x toggle-debug-on-error
, M-x toggle-debug-on-quit
, debug on signal (which can be used by sending USR2
to Emacs from outside), debug-on-entry
(of a function), debug-on-message
(when seeing a specific regexp match of a message) and finally, debug
itself as alternative to instrumenting a function with C-u C-M-x
.
Both debug
and edebug
offer enough functionality to inspect the state of Emacs when evaluating the code that you're interested in, hit e
and enter an expression.
However, while edebug
jumps to the place in the instrumented function and therefore gives you a clue where to look (which is kind of silly since you already know what exactly you've instrumented), debug
doesn't do this at all. I've pulled off a smaller hack after discovering that whenever debug
evaluates a buffer, it emits the value of point associated with the error; in other words using this information on the buffer can give you a line number in the backtrace!
(with-eval-after-load 'debug
(defun debugger-setup-buffer (debugger-args)
"Initialize the `*Backtrace*' buffer for entry to the debugger.
That buffer should be current already."
(setq buffer-read-only nil)
(erase-buffer)
(set-buffer-multibyte t) ;Why was it nil ? -stef
(setq buffer-undo-list t)
(let ((standard-output (current-buffer))
(print-escape-newlines t)
(print-level 8)
(print-length 50))
(backtrace))
(goto-char (point-min))
(delete-region (point)
(progn
(search-forward "\n debug(")
(forward-line (if (eq (car debugger-args) 'debug)
2 ; Remove implement-debug-on-entry frame.
1))
(point)))
(insert "Debugger entered")
;; lambda is for debug-on-call when a function call is next.
;; debug is for debug-on-entry function called.
(pcase (car debugger-args)
((or `lambda `debug)
(insert "--entering a function:\n"))
;; Exiting a function.
(`exit
(insert "--returning value: ")
(setq debugger-value (nth 1 debugger-args))
(prin1 debugger-value (current-buffer))
(insert ?\n)
(delete-char 1)
(insert ? )
(beginning-of-line))
;; Debugger entered for an error.
(`error
(insert "--Lisp error: ")
(prin1 (nth 1 debugger-args) (current-buffer))
(insert ?\n))
;; debug-on-call, when the next thing is an eval.
(`t
(insert "--beginning evaluation of function call form:\n"))
;; User calls debug directly.
(_
(insert ": ")
(prin1 (if (eq (car debugger-args) 'nil)
(cdr debugger-args) debugger-args)
(current-buffer))
(insert ?\n)))
;; After any frame that uses eval-buffer,
;; insert a line that states the buffer position it's reading at.
(save-excursion
(let ((tem eval-buffer-list))
(while (and tem
(re-search-forward "^ eval-\\(buffer\\|region\\)(" nil t))
(beginning-of-line)
(insert (format "Error at line %d in %s: "
(with-current-buffer (car tem)
(line-number-at-pos (point)))
(with-current-buffer (car tem)
(buffer-name))))
(pop tem))))
(debugger-make-xrefs)))
With this the original question in the title should be answered. As for your problem with getting a backtrace in the first place, I'm out of useful ideas.
nil
debug-on-error
? Doesn't that help?t
and then proceed to eval an error-throwing function.)debug-ignored-errors
doesn't list any errors. If you setdebug-on-signal
to non-nil
, and it was the case the other code handled the error, you will be able to get the error before the other code did.