To investigate a problem that happens in the middle of a program, one useful technique is to enter the debugger whenever a certain function is called. You can do this to the function in which the problem occurs, and then step through the function, or you can do this to a function called shortly before the problem, step quickly over the call to that function, and then step through its caller.
This function requests function-name to invoke the debugger each time it is called. It works by inserting the form
(debug 'debug)into the function definition as the first form.Any function defined as Lisp code may be set to break on entry, regardless of whether it is interpreted code or compiled code. If the function is a command, it will enter the debugger when called from Lisp and when called interactively (after the reading of the arguments). You can't debug primitive functions (i.e., those written in C) this way.
When
debug-on-entryis called interactively, it prompts for function-name in the minibuffer.If the function is already set up to invoke the debugger on entry,
debug-on-entrydoes nothing.Please note: if you redefine a function after using
debug-on-entryon it, the code to enter the debugger is lost.
debug-on-entryreturns function-name.(defun fact (n) (if (zerop n) 1 (* n (fact (1- n))))) ⇒ fact (debug-on-entry 'fact) ⇒ fact (fact 3) ------ Buffer: *Backtrace* ------ Entering: * fact(3) eval-region(4870 4878 t) byte-code("...") eval-last-sexp(nil) (let ...) eval-insert-last-sexp(nil) * call-interactively(eval-insert-last-sexp) ------ Buffer: *Backtrace* ------ (symbol-function 'fact) ⇒ (lambda (n) (debug (quote debug)) (if (zerop n) 1 (* n (fact (1- n)))))
This function undoes the effect of
debug-on-entryon function-name. When called interactively, it prompts for function-name in the minibuffer. If function-name isnilor the empty string, it cancels debugging for all functions.If
cancel-debug-on-entryis called more than once on the same function, the second call does nothing.cancel-debug-on-entryreturns function-name.