Debug Emacs Lisp with Edebug & Debug

Posted by:

Programming in Emacs Lisp – Debugging After using Emacs for over 4 years, I am still feeling like scratching the surface of the functionalities of this feature-laden editor. It is a text editor with so many extensions that can do almost anything you want with flexibility and customizability.

There are two ways to debug elisp code: debug and edebug.

debug

These are 4 common entry points (ways to use it):

  1. M-x debug-on-entry followed by a function you want to enter using the debugger.
  2. M-x toggle-debug-on-error – Enter the debugger when when an error is raised.
  3. ~ M-x toggle-debug-on-quit ~– Enter the debugger when the user hits C-g.
  4. Place explicit calls to function debug at particular places (breakpoints) in your code, to enter the debugger at those places:


    You step through the debugger using d, or ~c ~to skip over the details of a particular evaluation.

Useful Setting for debug


Edebug – the Source Level Debugger

Edebug, one of the best debugging tools, is a wonderful tool to debug and learn elisp. And it is a Source Level Debugger edebug – Programming in Emacs Lisp.

see here , Debugging Elisp Part 2: Advanced topics · Endless Parentheses and Using Edebug – GNU Emacs Lisp Reference Manual for more details.

yet another good tutorial My work flow to debug emacs plugin | Chen’s blog.

Normally, you would install this definition by positioning your cursor after the function’s closing parenthesis and typing C-x C-e (eval-last-sexp) or else by positioning your cursor within the definition and typing C-M-x (eval-defun).

The following is the steps to edebug:

  1. eval-buffer or function-to-debug (not necessary if everything’s already defined).
  2. Invoke C-u C-M-x on function-to-debug to instrument it – turn on edebug on function-to-debug; or M-x edebug-defun <RET>.
  3. Invoke M-x function-to-debug or some other function calls thhe function-to-debug, and you’ll be taken immediately into edebug, with point right before the (function-to-debug) expression.
  4. In edebug, with point right before a function call, you can hit i to instrument and step into that function, even if it was not prevously instrumented.
  5. Press SPC to step through the code, e to evaluate expressions (you can use this to find the values of numbers), h to continue until the specified point, b to set a breakpont, g to execute until a breakpoint, and q to stop debugging.

Comparison of key strokes of debug and edebug

Function edebug debug
engage C-h f then C-u C-M-x embed (debug) or M-x debug-on-entry RET triangle-bugged RET
step-in n d
step-over h j
quit c c
step out sexp o  
disengage C-h f then C-M-x remove (debug)
0

Add a Comment