Endless Parentheses

Concise ramblings on Emacs productivity.

Debugging Elisp Part 1: Earn your independence

Debugging Emacs-Lisp post series

Running into errors is not only a consequence of tinkering with your editor, it is the only road to graduating in Emacs. Therefore, it stands to reason that Emacs would contain the most impressive debugging utilities know to mankind, Edebug.

Learning to Edebug is the right decision for anyone who doesn't know how to Edebug.

  • It's easy as pie, you have no excuse not to learn it.
  • It helps you solve your own problems. Not that the community isn't helpful, it just allows you to help yourself—which will eventually allow you to help the community.
  • If you're a newcomer, it will teach you more about elisp than most tutorials out there.
  • If you're anything other than a newcomer, then it's about time and you should be ashamed of yourself.

A quick first stop

Before delving into Edebug, you should be aware of toggle-debug-on-error and toggle-debug-on-quit (which happen to be in our toggle-map). Though not as powerful as the alternative, they're a quick n' dirty way to get a backtrace of your problem.

How to Edebug a function

  1. Go to where the function is defined. You can usually do that with C-h f (which calls describe function) or just M-x find-function.
  2. Press C-u C-M-x. This turns on Edebug for that function.
  3. Now, just invoke the function (or some other command that calls that function).

The next time the Edebugged function gets called, Emacs will display its source code and will take you through it step-by-step. Press n to continue to the next step or c to stop fooling around and skip to the end.

For instance, to solve the issue that lead me to write this, we could perform the following sequence of commands:

M-x find-function RET package-menu--generate RET
C-u C-M-x
M-x paradox-list-packages RET

Once a function has been instrumented, it will trigger Edebug every time it is called. To undo this effect (to remove instrumentation) simply visit its definition again and hit C-M-x without the prefix.q

Tags: edebug, programming, debugging, emacs

Say thanks on Gratipay
comments powered by Disqus