18

If some emacs-lisp function, such as a major mode hook, is taking longer than expected to execute, I would sometimes like to interrupt it and look at the current stack. Ideally, I'd also like to be able to look at variable values, and so on, in the debugger. I'm not talking about code that I am deliberately executing in emacs-lisp mode, but rather code that gets executed in the normal operation of emacs.

How can I interrupt execution of emacs-lisp code directly in emacs? I do not know up front which function I need to debug.

For example, typing C-g, while it interrupts execution, also tells me nothing about which function was interrupted.

Kirill
  • 1,019
  • 7
  • 19

1 Answers1

18

You can make C-g trigger the debugger by turning on debug-on-quit. Just do

M-x toggle-debug-on-quit

Now every time you hit C-g you'll get a backtrace of what was going on. This backtrace is interactive, so you can keep stepping through the function with d, and you can start moving out with c. (Give it a try, it's easier to see than to read).

This is the same backtrace you get by adding (debug) inside your code. Here's a short blog post on it, and the relevant Elisp manual page.

Drew
  • 75,699
  • 9
  • 109
  • 225
Malabarba
  • 22,878
  • 6
  • 78
  • 163