How to instrument function creation and invocation in v8

179 views
Skip to first unread message

WENZHI CUI

unread,
Dec 2, 2015, 5:27:11 PM12/2/15
to v8-users
Hi all,
  Sorry if this post disturbs you. I searched for this problem on google but didn't find a perfect fit.
  I want to instrument all function events in v8. In specific, I want to instrument some bookkeeping code (for example, print out something) when a function object get created and when we call that function. I am wondering if that's possible by tweaking the source code of v8 slightly. 

Thanks,
Wenzhi Cui

Jakob Kummerow

unread,
Dec 3, 2015, 4:36:36 AM12/3/15
to v8-users
For tracing function invocation, there's the --trace flag.

There's no existing instrumentation to observe function object creation. You can try hooking into Factory::NewFunction, but I'm not sure if there are code paths that bypass this.


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

WENZHI CUI

unread,
Dec 3, 2015, 11:15:14 AM12/3/15
to v8-u...@googlegroups.com
Hi Jacob,
  Thank you very much for your reply.
  I haven't got time to look at the trace API closely. However, I do want to instrument some code before and after each function invocation
, is that possible with trace API or I can simply add my own trace functions?


Thanks,
Wenzhi Cui

You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/8JOKJY7o5Uc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.

Jakob Kummerow

unread,
Dec 3, 2015, 11:22:34 AM12/3/15
to v8-users
It's not an API, it's a flag. Just try it to see what it does, and look at the source (grep for FLAG_trace) to see how it's implemented.

WENZHI CUI

unread,
Dec 3, 2015, 11:49:13 AM12/3/15
to v8-u...@googlegroups.com
Hi Jacob,
  I found kTraceEnter and kTraceExit. I think I can insert my code there. Thank you!

Thanks,
Wenzhi Cui

tobias...@w-hs.de

unread,
Dec 6, 2017, 5:45:44 AM12/6/17
to v8-users
Hi,
I also want to trace javascript function calls inside of chromium (V8). I am using the --trace flag as suggested. However, it does not trace "native" function calls.

Example:
I have the following simple website:
<html>
<head></head>
<body>
<h1> A simple test script</h1>
<div id="thediv"></div>
<script>
function foo(var0){
 
return(bar(var0.concat(" World!")));
}
function bar(var1){
 
return(bar2(var1));
}
function bar2(var1){
 
return(var1);
}
var ret = foo("Hello");
console
.log(ret);
alert
(document.getElementById("thediv"));
</script></body></html>



When I open it in headless chromium I get the following tracing information:
  
1: ~+0(this=0x191cc7a940b9 <JSGlobal Object>) {
2:  ~foo+0(this=0x191cc7a940b9 <JSGlobal Object>, 0x3946ed57cd59 <String[5]: Hello>) {  
3:  ~bar+0(this=0x191cc7a940b9 <JSGlobal Object>, 0x191cc7af20d9 <String[12]: Hello World!>) {  
4:    ~bar2+0(this=0x191cc7a940b9 <JSGlobal Object>, 0x191cc7af20d9 <String[12]: Hello World!>) {  
4:    } -> 0x191cc7af20d9 <String[12]: Hello World!>  
3:   } -> 0x191cc7af20d9 <String[12]: Hello World!>  
2:  } -> 0x191cc7af20d9 <String[12]: Hello World!>



Functions like concat(),  getElementById(), or log() are not traced. I checked the source code of traceEnter and traceExit (Line 825 and below) but native functions are not traced by those. Can anyone give me some advice what I have to do to trace intrinsic functions
 or other function calls (e.g., functions defined in the ECMA specification or browser speficic APIs) within chromium.

Any help is appriciated!

Cheers,
Tobias

TraceEnter implementation

RUNTIME_FUNCTION(Runtime_TraceEnter) {
 
SealHandleScope shs(isolate);
 DCHECK_EQ
(0, args.length());
 
PrintIndentation(isolate);
 
JavaScriptFrame::PrintTop(isolate, stdout, true, false);
 
PrintF(" {\n");
 
return isolate->heap()->undefined_value();
}

Camillo Bruni

unread,
Dec 7, 2017, 5:35:34 AM12/7/17
to v8-users
Hi Tobias,

Most probably you would have to manually annotate all CodeStubAssembler builtins with a print statement to get the proper output.
There are several macros where you might be able to inject a runtime call or a direct C++ call (see TF_BUILTIN and friends).
The same counts for all C++ runtime functions and builtins (see RUNTIME_FUNCTION and BUILTIN macros),

Cheers,
Camillo
Reply all
Reply to author
Forward
0 new messages