Yet another short tip for today. (Don’t worry, longer, more involved posts will still happen here!)
A few years ago I wrote about a few useful debugging features of Emacs. A few days ago I learned that there are more of them now! About two years after I published that post, Emacs learned how to invoke the debugger every time some variable changes its value. Needless to say, this is potentially extremely useful! All you have to do is to say M-x debug-on-variable-change
, type the variable name and run the debugged code. After you’re done, just say M-x
cancel-debug-on-variable-change
.
You can read the relevant portions of the Elisp reference to learn that this has a few limitations. One is that only dynamic variables are watchable this way (but – as the manual mentions – finding places where lexical variables are modified is very easy, since you only need to study the code in the actual scope of these variables). Another one is that the changes to the object (e.g., a list) the variable is bound to are not detected (which makes sense, since the value of the variable, as in: “the place in memory the variable points to”, stays the same. The third and last one is that (rather unsurprisingly) C code can manipulate variables in a way undetectable by this mechanism.
Happy debugging!