Go to the first, previous, next, last section, table of contents.


The edebug 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 section `Edebug' in The GNU Emacs Lisp Reference Manual.

Here is a bugged function definition for triangle-recursively. See section Recursion in Place of a Counter, for a review of it. This example is presented without indentation to the left of the defun, as explained below.

(defun triangle-recursively-bugged (number)
  "Return sum of numbers 1 through NUMBER inclusive.
Uses recursion."
  (if (= number 1)                   
      1                              
    (+ number                        
       (triangle-recursively-bugged   
        (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 Interactive mode.)

However, to prepare this function definition for Edebug, you must first instrument the code using a different command. In Emacs version 19, you can do this by positioning your cursor within the definition and typing the following:

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 loading Edebug, you can use its standard keybindings, such as C-u C-M-x (eval-defun with a prefix argument) for edebug-defun.)

In Emacs version 18, you need to load Edebug yourself; you can do this by putting the appropriate load command in your `.emacs' file.

If you are reading this in Info, you can instrument the triangle-recursively-bugged function shown above. edebug-defun fails to locate the bounds of a definition whose defun line is indented; so the example is presented without the usual spaces to the left of the defun.

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 arrow at the left hand side of that line that looks like this: `=>'. The arrow marks the line where the function is executing.

=>-!-(if (= number 1)

In the example, the location of point is displayed with a star, `-!-' (in Info, it is displayed as `-!-').

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 = C-c

This means the value of number is 3, which is ASCII CTL-C (the third letter of the alphabet).

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 section `Edebug' in The GNU Emacs Lisp Reference Manual.


Go to the first, previous, next, last section, table of contents.