8

I would like to be able to perform manipulations on a given JS app, and then simply get a large log of all the functions that have been called. This is possible in Chrome, but only if one puts a breakpoint somewhere. My problem is when I am reverse-engineering a given website (only for self-teaching purposes, of course) it often takes me a whole lot of time to figure out where to even start from. Something like that will help me tremendously because I will no longer have to search within the code, rather, I will just do a user action, and grab the stack log afterwards.

I suppose that there should be a way to intercept (or wrap) every function call, so that it is dumped to the log before the function is called.

user802232
  • 2,541
  • 7
  • 34
  • 40
  • I don't think you can wrap any `func()` calls as it uses the internal `[[Call]]` method, but if it is, I'm very interested to know. http://stackoverflow.com/questions/7329923/is-it-possible-to-override-the-call-function – pimvdb Sep 17 '11 at 11:21

4 Answers4

2

Try this article: http://eriwen.com/javascript/stacktrace-update/ or this post: http://ivan-ghandhi.livejournal.com/942493.html

and, probably, this: How can I get a Javascript stack trace when I throw an exception?

Community
  • 1
  • 1
c69
  • 19,951
  • 7
  • 52
  • 82
0

In Firebug, you can use the profiler to log every function called. Use console.profile() and console.profileEnd() to trigger it programatically.

However, this will not give you proper stack traces. (Are you sure that's what you want?)

To log methods of specific objects, you can overwrite them like so:

for (var key in obj) {
    if (typeof obj[key] == 'function') {
        (function(){
            var origFun = obj[key];
            obj[key] = function () {
                var result = origFun.apply(this, arguments);
                console.log('call to method', key, 'with arguments', arguments,' - Result:', result);
                // console.trace(); // for a trace with every call
                return result;
            };
        })();
    }
}
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • Profiling is nice and even Webkit has it, however, I cannot really get results in the way they are executed, rather they are sorted by the performance overhead they put on the working application. Maybe I will this code snippet a try, by wrapping the constructor of something really generic, like Object, or at least somewhere further up the hierarchy – user802232 Sep 17 '11 at 12:02
0

Maybe aspect oriented programming (AOP) can provide an answer. I just found out about aspectJS which could help intercept and log function calls

user802232
  • 2,541
  • 7
  • 34
  • 40
0

You can use dynatrace. Dynatrace is a profiling tool for IE and FF. Dynatrace can monitor your application while it is running, and then serves you a timeline of all what happened. In the timeline, there is blocks representing the javascript activity. You can right-click on it (purepath), and then walk through the whole call stack. You can export that to excel or other If you want. You can add markers in your code, those markers will appear on the timeline and in the purepath: if(typeof(_dt_addMark)!="undefined") _dt_addMark('MyCustomTimerName');

alternatively, if you only want to find "a way to intercept (or wrap) every function call", there is a low-tech solution, if you are using a real webbapp (single-load javascript app): bookmarklets

With bookmarklets, once you have loaded your page, you can execute some custom javascript. So what you can do there, is to override the functions methods that you want to observe with the same function containing logging (so just copy and paste the function, and add some console.log in there). This actually works even with native js functions.

Olivvv
  • 1,140
  • 1
  • 13
  • 37