0

There are cases Emacs wont print stack traces on error

How can I make elisp print a stack trace from my own code? (into the stdout/stderr for example)

Something like Python's traceback:

import traceback
traceback.print_exc()
  • Which cases? What's the use case of the function? (how Emacs (or elisp) can print to stdout/stderr?) – xuchunyang Apr 9 at 8:31
  • I wanted to avoid getting into the use-case because I'd just like to get the stack-trace in any context where I'm running elisp and not focus on the particular case why. I've run into a few cases where stack traces are totally ignored, asked about it here, and there wasn't a straightforward answer. – ideasman42 Apr 9 at 10:53
0
(let ((debug-on-error t))
  (error "test"))

With emacs -Q --batch -l tmp/test.el the following is printed to stderr:

Debugger entered--Lisp error: (error "test")
  signal(error ("test"))
  error("test")
  (let ((debug-on-error t)) (error "test"))
  eval-buffer(#<buffer  *load*> nil "/home/wasa/tmp/test.el" nil t)  ; Reading at buffer position 44
  load-with-code-conversion("/home/wasa/tmp/test.el" "/home/wasa/tmp/test.el" nil t)
  load("/home/wasa/tmp/test.el" nil t)
  command-line-1(("-l" "tmp/test.el"))
  command-line()
  normal-top-level()

If there's a situation where it doesn't work, chances are errors are inhibited by the code in question.

| improve this answer | |
  • Is there a way to do this that gets the stack trace but doesn't cause the code to halt? - e.g: gets the stack-trace as a string? – ideasman42 Apr 9 at 12:18
  • This doesn't work in all cases, eg: in a function registered via jit-lock-register: Error during redisplay: (jit-lock-function 1501) signaled (error "test") – ideasman42 Apr 9 at 12:22
  • Hence the "chances are errors are inhibited by the code in question", popping up a debugger during redisplay is not an option. – wasamasa Apr 10 at 7:14
0

I'm not sure just what you're asking, but does using function backtrace in your code, at the place where you want to see a backtrace, help? C-h f says:

backtrace is a compiled Lisp function in subr.el.

(backtrace)

Print a trace of Lisp function calls currently active.

Output stream used is value of standard-output.

| improve this answer | |
  • This is almost there, but prints to stdout instead of stderr. Instead you need (let ((standard-output 'external-debugging-output)) (backtrace)) – wasamasa Apr 10 at 7:19
  • @wasamasa: Yes, that info helps. (But the question does say, "into the stdout/stderr for example".) – Drew Apr 10 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.