17.4 The edebug Source Level Debugger

Edebug is a source level debugger. Edebug normally displays the source of the code you are debugging, with an arrow at the left that shows which line you are currently executing.

You can walk through the execution of a function, line by line, or run quickly until reaching a breakpoint where execution stops.

Edebug is described in Edebug in The GNU Emacs Lisp Reference Manual.

Here is a bugged function definition for triangle-recursively. See Recursion in place of a counter, for a review of it.

(defun triangle-recursively-bugged (number)
  "Return sum of numbers 1 through NUMBER inclusive.
Uses recursion."
  (if (= number 1)
    (+ number
        (1= number)))))               ; Error here.

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). (By default, the eval-defun command works only in Emacs Lisp mode or in Lisp Interaction mode.)

However, to prepare this function definition for Edebug, you must first instrument the code using a different command. You can do this by positioning your cursor within or just after the definition and typing

M-x edebug-defun RET

This will cause Emacs to load Edebug automatically if it is not already loaded, and properly instrument the function.

After instrumenting the function, place your cursor after the following expression and type C-x C-e (eval-last-sexp):

(triangle-recursively-bugged 3)

You will be jumped back to the source for triangle-recursively-bugged and the cursor positioned at the beginning of the if line of the function. Also, you will see an arrowhead at the left hand side of that line. The arrowhead marks the line where the function is executing. (In the following examples, we show the arrowhead with ‘=>’; in a windowing system, you may see the arrowhead as a solid triangle in the window fringe.)

=>∗(if (= number 1)

In the example, the location of point is displayed as ‘’ (in a printed book, it is displayed with a five pointed star).

If you now press SPC, point will move to the next expression to be executed; the line will look like this:

=>(if ∗(= number 1)

As you continue to press SPC, point will move from expression to expression. At the same time, whenever an expression returns a value, that value will be displayed in the echo area. For example, after you move point past number, you will see the following:

Result: 3 (#o3, #x3, ?\C-c)

This means the value of number is 3, which is octal three, hexadecimal three, and ASCII Control-C (the third letter of the alphabet, in case you need to know this information).

You can continue moving through the code until you reach the line with the error. Before evaluation, that line looks like this:

=>        ∗(1= number)))))               ; Error here.

When you press SPC once again, you will produce an error message that says:

Symbol's function definition is void: 1=

This is the bug.

Press q to quit Edebug.

To remove instrumentation from a function definition, simply re-evaluate it with a command that does not instrument it. For example, you could place your cursor after the definition’s closing parenthesis and type C-x C-e.

Edebug does a great deal more than walk with you through a function. You can set it so it races through on its own, stopping only at an error or at specified stopping points; you can cause it to display the changing values of various expressions; you can find out how many times a function is called, and more.

Edebug is described in Edebug in The GNU Emacs Lisp Reference Manual.