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
  • Which cases? What's the use case of the function? (how Emacs (or elisp) can print to stdout/stderr?)
    – xuchunyang
    Commented Apr 9, 2020 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
    Commented Apr 9, 2020 at 10:53

2 Answers 2


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.


Print a trace of Lisp function calls currently active.

Output stream used is value of standard-output.

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

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

  • 1
    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
    Commented Apr 9, 2020 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
    Commented Apr 9, 2020 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
    Commented Apr 10, 2020 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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