Moshe's Blog

Tips and tricks learnt along the way.

When Programming Goes Bad - Debugging the Impossible

| Comments

Sometimes code for a project gets so big and so deeply indirected that debugging is nearly impossible.

We had a bug similar to this where a controller was changing models which triggered avalanches of cascades that no human could follow. The only thing we knew was that a select with an id of elusive was being changed through jQuery.

After trying in vain to figure out where the change was happening, I had an aha! moment

This is basically how I solved it:

(function($) {
    var oldAttr = $.fn.attr;
    var oldProp = $.fn.prop;
    $.fn.attr = function() {
        if (this.id === 'elusive') {
            debugger;           
        }
        return oldAttr.apply(this, arguments);
    };
    $.fn.prop = function() {
        if (this.id === 'elusive') {
            debugger;           
        }
        return oldProp(this, arguments);
    };
})(jQuery);

Then it’s just a matter of looking at the call stack and working backwards from there.

Comments