-2

I want to dynamically hook the information about the current executing function in V8.

For example the function name, line number, or file name, etc.

Is it possible for V8 to set some breakpoints to get this information when the page or app is running in it?

For example:

< script >
  function f1() {
    ...
    f2();
    ...
  }

function f2() {
  ...
}
f1(); 
< /script>

When f1() is executing, get the f1's function name and linenumbers; then f1() call f2(), f2() is executing, get the f2's function name and linenumbers; finally, when f2() is finish, back to f1(), get the f1()'s function name and linenumbers.

That is what I mean by "information about the current executing function in V8".

wangrl
  • 5
  • 5

1 Answers1

1

You can use new Error().stack and then parse the output.

(function() {
    var b = function() {
        var trace = new Error().stack.split("\n")[1];
        console.log(trace);
    };

    var a = function() {
        b();
    };

    a();
})();

Output:

at b (file:///home/inonit/tmp/stack.html:6:16)

From there you can extract the file name, function name, line number, and column number if you like.

David P. Caldwell
  • 3,394
  • 1
  • 19
  • 32
  • Thanks for your answer. But I wonder it is possible to make some breakpoint in V8 to get this information instead of add "new Error().stack" into the script? – wangrl Jun 24 '15 at 06:54
  • 1
    It is unclear what you mean by "a breakpoint in v8." When do you want this information? – David P. Caldwell Jun 24 '15 at 13:00
  • I want that information when I use GDB to dynamically debug web applications. I wonder if it is possible for V8 to set breakpoints in somewhere to get this information when the app is running. – wangrl Jun 25 '15 at 02:28
  • Well if you're using GDB and you're way down in there already, I suppose you could figure out how the `stack` property works and then figure out how to retrieve the information. But no, it turns out I don't know the answer to your question, unfortunately. – David P. Caldwell Jun 25 '15 at 02:38