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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::Script;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
|
|
|
|
|
|
|
using v8::internal::byte;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void DoTrace(unsigned int fp) {
|
|
|
|
trace_env.sample->fp = 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
|
|
|
|
static void DoTraceHideCEntryFPAddress(unsigned int fp) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- 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-03-20 14:49:12 +00:00
|
|
|
static unsigned int 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-03-20 14:49:12 +00:00
|
|
|
unsigned int TraceExtension::GetFP(const v8::Arguments& args) {
|
2009-02-25 16:00:21 +00:00
|
|
|
CHECK_EQ(1, args.Length());
|
|
|
|
unsigned int fp = args[0]->Int32Value() << 2;
|
|
|
|
printf("Trace: %08x\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-03-20 14:49:12 +00:00
|
|
|
static void CFuncDoTrace() {
|
|
|
|
unsigned int fp;
|
|
|
|
#ifdef __GNUC__
|
|
|
|
fp = reinterpret_cast<unsigned int>(__builtin_frame_address(0));
|
|
|
|
#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;
|
|
|
|
StackTracer tracer(reinterpret_cast<unsigned int>(&sample));
|
|
|
|
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
|