4

How can you go about debugging an error with a defadvice involved?

Searching for solutions all I can find is recommendations not to use defadvice because of the debugging difficulties. Amen to that, but other peoples defadvices are everywhere and I'm always running into bugs I can't even begin to track down.

tony day
  • 512
  • 2
  • 10
  • Finding where the defadvice is is my very first road block and [this answer](http://stackoverflow.com/questions/10967367/how-can-i-locate-the-defadvice-for-an-advised-function-in-emacs) suggests an obvious command doesn't exist. – tony day Jul 09 '13 at 03:33
  • You could put a breakpoint in the `ad-defactivate` the very first thing you load Emacs and then wait for your advice to trigger it and work from there... That's a lot of troubles, but if you only need to do it once... –  Jul 09 '13 at 06:38

2 Answers2

8

One more thing I could think of:

  1. Temporary rename defadvice into defadvice-old.

  2. Write this new version of defadvice:


(defmacro defadvice (function args &rest body)
  `(progn
     (put ',(cadr args) 'source-position
          (cons byte-compile-current-file byte-compile-read-position))
     (defadvice-old ,function ,args ,@body)))
  1. Examine the (symbol-plist <<name of advice>>) once you need it, it would have the position in the file of the file that used the macro.
  • Oh, that's very clever. You should cross-post the idea in that other Q&A. – phils Jul 09 '13 at 07:16
  • Thanks,, excellent hack. This idea should make it into emacs as defadvice-defadvice. – tony day Jul 09 '13 at 08:09
  • @phils I know that some people who work on the Emacs own sources are regulars on SO. Perhaps some of them will step in / maybe there's some pitfalls on having `defadice` remembering the advice position. If not - this looks worth of feature request. I'd maybe send one then. –  Jul 09 '13 at 09:15
  • @tonyday or `defadice-rec` :P whichever dialect you prefer. –  Jul 09 '13 at 09:16
3

One option is to make the advice body call a function to do its work, and then you can debug the function instead.

Note that you can evaluate (ad-deactivate 'function) to deactivate all advice for the specified function (this will revert the function to its unadvised state). This might help if some advice is causing you serious problems while you are trying to track it down.

phils
  • 71,335
  • 11
  • 153
  • 198