17

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.

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.

  • (meekly says hello) ;) Awesome note that the debugger is interactive – I never thought to check that :) – Sean Allred Dec 2 '14 at 5:39
  • @SeanAllred yes, I only found out a couple of weeks ago. – Malabarba Dec 2 '14 at 9:05

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.