2009-02-25 16:00:21 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Tests of profiler-related functions from log.h
|
|
|
|
|
2009-02-26 14:30:30 +00:00
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
2009-02-26 13:45:11 +00:00
|
|
|
|
2009-02-25 16:00:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
#include "codegen.h"
|
2009-02-25 16:00:21 +00:00
|
|
|
#include "log.h"
|
2009-03-20 14:49:12 +00:00
|
|
|
#include "top.h"
|
2009-02-25 16:00:21 +00:00
|
|
|
#include "cctest.h"
|
2009-05-07 09:24:43 +00:00
|
|
|
#include "disassembler.h"
|
2009-02-25 16:00:21 +00:00
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::Script;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
|
|
|
|
|
|
|
using v8::internal::byte;
|
2009-05-07 09:24:43 +00:00
|
|
|
using v8::internal::Address;
|
2009-02-25 16:00:21 +00:00
|
|
|
using v8::internal::Handle;
|
|
|
|
using v8::internal::JSFunction;
|
|
|
|
using v8::internal::StackTracer;
|
|
|
|
using v8::internal::TickSample;
|
2009-03-20 14:49:12 +00:00
|
|
|
using v8::internal::Top;
|
2009-02-25 16:00:21 +00:00
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
namespace i = v8::internal;
|
|
|
|
|
2009-02-25 16:00:21 +00:00
|
|
|
|
|
|
|
static v8::Persistent<v8::Context> env;
|
|
|
|
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
StackTracer* tracer;
|
|
|
|
TickSample* sample;
|
2009-03-20 14:49:12 +00:00
|
|
|
} trace_env = { NULL, NULL };
|
2009-02-25 16:00:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void InitTraceEnv(StackTracer* tracer, TickSample* sample) {
|
|
|
|
trace_env.tracer = tracer;
|
|
|
|
trace_env.sample = sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
static void DoTrace(Address fp) {
|
|
|
|
trace_env.sample->fp = reinterpret_cast<uintptr_t>(fp);
|
2009-03-20 14:49:12 +00:00
|
|
|
// sp is only used to define stack high bound
|
|
|
|
trace_env.sample->sp =
|
|
|
|
reinterpret_cast<unsigned int>(trace_env.sample) - 10240;
|
2009-02-25 16:00:21 +00:00
|
|
|
trace_env.tracer->Trace(trace_env.sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 14:49:12 +00:00
|
|
|
// Hide c_entry_fp to emulate situation when sampling is done while
|
|
|
|
// pure JS code is being executed
|
2009-05-07 09:24:43 +00:00
|
|
|
static void DoTraceHideCEntryFPAddress(Address fp) {
|
2009-03-20 14:49:12 +00:00
|
|
|
v8::internal::Address saved_c_frame_fp = *(Top::c_entry_fp_address());
|
|
|
|
CHECK(saved_c_frame_fp);
|
|
|
|
*(Top::c_entry_fp_address()) = 0;
|
2009-02-25 16:00:21 +00:00
|
|
|
DoTrace(fp);
|
2009-03-20 14:49:12 +00:00
|
|
|
*(Top::c_entry_fp_address()) = saved_c_frame_fp;
|
2009-02-25 16:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
static void CheckRetAddrIsInFunction(const char* func_name,
|
|
|
|
Address ret_addr,
|
|
|
|
Address func_start_addr,
|
|
|
|
unsigned int func_len) {
|
|
|
|
printf("CheckRetAddrIsInFunction \"%s\": %p %p %p\n",
|
|
|
|
func_name, func_start_addr, ret_addr, func_start_addr + func_len);
|
|
|
|
CHECK_GE(ret_addr, func_start_addr);
|
|
|
|
CHECK_GE(func_start_addr + func_len, ret_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckRetAddrIsInJSFunction(const char* func_name,
|
|
|
|
Address ret_addr,
|
|
|
|
Handle<JSFunction> func) {
|
|
|
|
v8::internal::Code* func_code = func->code();
|
|
|
|
CheckRetAddrIsInFunction(
|
|
|
|
func_name, ret_addr,
|
|
|
|
func_code->instruction_start(),
|
|
|
|
func_code->ExecutableSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-25 16:00:21 +00:00
|
|
|
// --- T r a c e E x t e n s i o n ---
|
|
|
|
|
|
|
|
class TraceExtension : public v8::Extension {
|
|
|
|
public:
|
|
|
|
TraceExtension() : v8::Extension("v8/trace", kSource) { }
|
|
|
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
|
2009-03-20 14:49:12 +00:00
|
|
|
v8::Handle<String> name);
|
2009-02-25 16:00:21 +00:00
|
|
|
static v8::Handle<v8::Value> Trace(const v8::Arguments& args);
|
2009-03-20 14:49:12 +00:00
|
|
|
static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args);
|
2009-02-25 16:00:21 +00:00
|
|
|
private:
|
2009-05-07 09:24:43 +00:00
|
|
|
static Address GetFP(const v8::Arguments& args);
|
2009-02-25 16:00:21 +00:00
|
|
|
static const char* kSource;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-20 14:49:12 +00:00
|
|
|
const char* TraceExtension::kSource =
|
|
|
|
"native function trace();"
|
|
|
|
"native function js_trace();";
|
2009-02-25 16:00:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction(
|
2009-03-20 14:49:12 +00:00
|
|
|
v8::Handle<String> name) {
|
|
|
|
if (name->Equals(String::New("trace"))) {
|
|
|
|
return v8::FunctionTemplate::New(TraceExtension::Trace);
|
|
|
|
} else if (name->Equals(String::New("js_trace"))) {
|
|
|
|
return v8::FunctionTemplate::New(TraceExtension::JSTrace);
|
|
|
|
} else {
|
|
|
|
CHECK(false);
|
|
|
|
return v8::Handle<v8::FunctionTemplate>();
|
|
|
|
}
|
2009-02-25 16:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
Address TraceExtension::GetFP(const v8::Arguments& args) {
|
2009-02-25 16:00:21 +00:00
|
|
|
CHECK_EQ(1, args.Length());
|
2009-05-07 09:24:43 +00:00
|
|
|
Address fp = reinterpret_cast<Address>(args[0]->Int32Value() << 2);
|
|
|
|
printf("Trace: %p\n", fp);
|
2009-03-20 14:49:12 +00:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) {
|
|
|
|
DoTrace(GetFP(args));
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) {
|
|
|
|
DoTraceHideCEntryFPAddress(GetFP(args));
|
2009-02-25 16:00:21 +00:00
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static TraceExtension kTraceExtension;
|
|
|
|
v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension);
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
static void InitializeVM() {
|
|
|
|
if (env.IsEmpty()) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
const char* extensions[] = { "v8/trace" };
|
|
|
|
v8::ExtensionConfiguration config(1, extensions);
|
|
|
|
env = v8::Context::New(&config);
|
|
|
|
}
|
|
|
|
v8::HandleScope scope;
|
|
|
|
env->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<JSFunction> CompileFunction(const char* source) {
|
|
|
|
return v8::Utils::OpenHandle(*Script::Compile(String::New(source)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CompileRun(const char* source) {
|
|
|
|
Script::Compile(String::New(source))->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Local<Value> GetGlobalProperty(const char* name) {
|
|
|
|
return env->Global()->Get(String::New(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<JSFunction> GetGlobalJSFunction(const char* name) {
|
|
|
|
Handle<JSFunction> js_func(JSFunction::cast(
|
|
|
|
*(v8::Utils::OpenHandle(
|
|
|
|
*GetGlobalProperty(name)))));
|
|
|
|
return js_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckRetAddrIsInJSFunction(const char* func_name,
|
|
|
|
Address ret_addr) {
|
|
|
|
CheckRetAddrIsInJSFunction(func_name, ret_addr,
|
|
|
|
GetGlobalJSFunction(func_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SetGlobalProperty(const char* name, Local<Value> value) {
|
|
|
|
env->Global()->Set(String::New(name), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<v8::internal::String> NewString(const char* s) {
|
|
|
|
return i::Factory::NewStringFromAscii(i::CStrVector(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace v8 { namespace internal {
|
|
|
|
|
|
|
|
class CodeGeneratorPatcher {
|
|
|
|
public:
|
|
|
|
CodeGeneratorPatcher() {
|
|
|
|
CodeGenerator::InlineRuntimeLUT genGetFramePointer =
|
|
|
|
{&CodeGenerator::GenerateGetFramePointer, "_GetFramePointer"};
|
|
|
|
// _FastCharCodeAt is not used in our tests.
|
|
|
|
bool result = CodeGenerator::PatchInlineRuntimeEntry(
|
|
|
|
NewString("_FastCharCodeAt"),
|
|
|
|
genGetFramePointer, &oldInlineEntry);
|
|
|
|
CHECK(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CodeGeneratorPatcher() {
|
|
|
|
CHECK(CodeGenerator::PatchInlineRuntimeEntry(
|
|
|
|
NewString("_GetFramePointer"),
|
|
|
|
oldInlineEntry, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CodeGenerator::InlineRuntimeLUT oldInlineEntry;
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a global function named 'func_name' that calls the tracing
|
|
|
|
// function 'trace_func_name' with an actual EBP register value,
|
|
|
|
// shifted right to be presented as Smi.
|
|
|
|
static void CreateTraceCallerFunction(const char* func_name,
|
|
|
|
const char* trace_func_name) {
|
|
|
|
i::EmbeddedVector<char, 256> trace_call_buf;
|
|
|
|
i::OS::SNPrintF(trace_call_buf, "%s(%%_GetFramePointer());", trace_func_name);
|
|
|
|
|
|
|
|
// Compile the script.
|
|
|
|
i::CodeGeneratorPatcher patcher;
|
|
|
|
bool allow_natives_syntax = i::FLAG_allow_natives_syntax;
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
Handle<JSFunction> func = CompileFunction(trace_call_buf.start());
|
|
|
|
CHECK(!func.is_null());
|
|
|
|
i::FLAG_allow_natives_syntax = allow_natives_syntax;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
v8::internal::Code* func_code = func->code();
|
|
|
|
CHECK(func_code->IsCode());
|
|
|
|
func_code->Print();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SetGlobalProperty(func_name, v8::ToApi<Value>(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CFromJSStackTrace) {
|
|
|
|
TickSample sample;
|
|
|
|
StackTracer tracer(reinterpret_cast<uintptr_t>(&sample));
|
|
|
|
InitTraceEnv(&tracer, &sample);
|
|
|
|
|
|
|
|
InitializeVM();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
CreateTraceCallerFunction("JSFuncDoTrace", "trace");
|
|
|
|
CompileRun(
|
|
|
|
"function JSTrace() {"
|
|
|
|
" JSFuncDoTrace();"
|
|
|
|
"};\n"
|
|
|
|
"JSTrace();");
|
|
|
|
CHECK_GT(sample.frames_count, 1);
|
|
|
|
// Stack sampling will start from the first JS function, i.e. "JSFuncDoTrace"
|
|
|
|
CheckRetAddrIsInJSFunction("JSFuncDoTrace",
|
|
|
|
sample.stack[0]);
|
|
|
|
CheckRetAddrIsInJSFunction("JSTrace",
|
|
|
|
sample.stack[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PureJSStackTrace) {
|
|
|
|
TickSample sample;
|
|
|
|
StackTracer tracer(reinterpret_cast<uintptr_t>(&sample));
|
|
|
|
InitTraceEnv(&tracer, &sample);
|
|
|
|
|
|
|
|
InitializeVM();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
CreateTraceCallerFunction("JSFuncDoTrace", "js_trace");
|
|
|
|
CompileRun(
|
|
|
|
"function JSTrace() {"
|
|
|
|
" JSFuncDoTrace();"
|
|
|
|
"};\n"
|
|
|
|
"function OuterJSTrace() {"
|
|
|
|
" JSTrace();"
|
|
|
|
"};\n"
|
|
|
|
"OuterJSTrace();");
|
|
|
|
CHECK_GT(sample.frames_count, 1);
|
|
|
|
// Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
|
|
|
|
CheckRetAddrIsInJSFunction("JSTrace",
|
|
|
|
sample.stack[0]);
|
|
|
|
CheckRetAddrIsInJSFunction("OuterJSTrace",
|
|
|
|
sample.stack[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 14:49:12 +00:00
|
|
|
static void CFuncDoTrace() {
|
2009-05-07 09:24:43 +00:00
|
|
|
Address fp;
|
2009-03-20 14:49:12 +00:00
|
|
|
#ifdef __GNUC__
|
2009-05-07 09:24:43 +00:00
|
|
|
fp = reinterpret_cast<Address>(__builtin_frame_address(0));
|
2009-03-20 14:49:12 +00:00
|
|
|
#elif defined _MSC_VER
|
|
|
|
__asm mov [fp], ebp // NOLINT
|
|
|
|
#endif
|
|
|
|
DoTrace(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int CFunc(int depth) {
|
|
|
|
if (depth <= 0) {
|
|
|
|
CFuncDoTrace();
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return CFunc(depth - 1) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PureCStackTrace) {
|
|
|
|
TickSample sample;
|
2009-05-07 09:24:43 +00:00
|
|
|
StackTracer tracer(reinterpret_cast<uintptr_t>(&sample));
|
2009-03-20 14:49:12 +00:00
|
|
|
InitTraceEnv(&tracer, &sample);
|
|
|
|
// Check that sampler doesn't crash
|
|
|
|
CHECK_EQ(10, CFunc(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-26 14:30:30 +00:00
|
|
|
#endif // ENABLE_LOGGING_AND_PROFILING
|