Tracing Function Invocations

"Daniel Gredler" <[email protected]> Mon, 18 Feb 2008 15:50:54 -0500
Newsgroups gmane.comp.mozilla.devel.jsdebugger
Message-ID <[email protected]>
Hi guys,

I'm trying to use the jsdIDebuggerService interface in a Firefox extension
to trace function invocations. Specifically, I'm using the debugger
service's functionHook. This hook takes two arguments: the current frame and
the invocation type.

However, I'm having trouble accessing the name of the function being
invoked, as well as the names and values of the arguments being passed to
the function. The problem is that frame.functionName and
frame.script.functionObject refer to the function within which the
invocation is made, not the function being invoked.

Has anyone used the functionHook to trace and/or log JavaScript function
calls? If not, has anyone used a different method to do achieve this? I'm
attaching a snippet of code below so as to give a better idea of what I'm
doing (wrong).

Take care,

Daniel



var functionHook = function(frame, type) {
    if(type != jsdICallHook.TYPE_FUNCTION_CALL) return;
    // Start with indentation.
    var s = "";
    var parent = frame.callingFrame;
    while(parent) {
        if(!parent.isDebugger && !parent.isNative) s += "   ";
        parent = parent.callingFrame;
    }
    // Continue with argument info.
    var args = "";
    var p = new Object();
    frame.script.functionObject.getProperties(p, {});
    p = p.value;
    for(var i = p.length - 1; i >= 0; i--) {
        if(p[i].flags & jsdIProperty.FLAG_ARGUMENT) {
            args += p[i].name.stringValue + ": " + formatValue(p[i].value) +
", ";
        }
    }
    if(args.lastIndexOf(", ") == args.length - 2) {
        args = args.substring(0, args.length - 2);
    }
    // Continue with other information.
    var line = frame.line;
    var funcName = frame.functionName;
    // Log the gathered information.
    s += funcName + "(" + args + ") @ line " + line + " of " + file;
    ftLog.push(s);
}
debuggerService.on();
debuggerService.functionHook = { onCall: functionHook };

-- 
Daniel Gredler
http://daniel.gredler.net/