How to debug elisp?

DebuggingEmacsElisp

Debugging Problem Overview


Normally the easiest way to debug is using printf. What can I do to debug emacs-lisp? How can I print something to emacs editor from elisp? Or is there any way to debug elisp code?

For example, how can I check if the following code is run in .emacs file?

(load "auctex.el" nil t t)

Debugging Solutions


Solution 1 - Debugging

The debugger (edebug) is pretty straight forward to use. Go to the definition of the function, and type M-x edebug-defun. The next time it is called, you'll be able to step through the code as with every other debugger. Type ? for the list of keybindings, or check out the documentation for edebug.

Solution 2 - Debugging

There are two debuggers in GNU Emacs:

  • edebug -- explained in another post here
  • debug

I use debug. These are common entry points (ways to use it):

  • M-x debug-on-entry followed by a function you want to enter using the debugger.

  • M-x toggle-debug-on-error -- Enter the debugger when when an error is raised.

  • M-x toggle-debug-on-quit -- Enter the debugger when the user hits C-g.

  • Place explicit calls to function debug at particular places (breakpoints) in your code, to enter the debugger at those places:

(debug)

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

Solution 3 - Debugging

This is useful for printing values

(message "Hello (%s)" foo)

but doesn't work so well for data structures. For that, use

(prin1 list-foo)

or (prin1-to-string) to embed it in a (message).

Solution 4 - Debugging

The easiest way to debug may be to run your code interactively. You can do that in a lisp buffer by placing your point after the expression and running C-x C-e (eval-last-sexp).

Alternatively:

(message "hello world")

C-h f message to find out more about the built in message function. If you generate lots of messages, you may want to customize the variable message-log-max to a larger value.

Solution 5 - Debugging

To answer your questions one by one:

  • print something: there's a million ways. (message "Hello") puts the string in the echo area; (insert "hello") puts the string into the current buffer at point ...
  • how can I check if the following code is run: I'd just replace "auctex.el" with (say) "frotzumotzulous" (i.e., any string at all, as long as it doesn't name a real file) and then see if you get an error message. If you get no error, then clearly that code isn't being run.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - DebuggingTrey JacksonView Answer on Stackoverflow
Solution 2 - DebuggingDrewView Answer on Stackoverflow
Solution 3 - DebuggingjplindstromView Answer on Stackoverflow
Solution 4 - DebuggingDave BacherView Answer on Stackoverflow
Solution 5 - Debuggingoffby1View Answer on Stackoverflow