2014-01-17 10:52:00 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/trace-extension.h"
|
2014-01-17 10:52:00 +00:00
|
|
|
|
2016-07-06 18:37:10 +00:00
|
|
|
#include "include/v8-profiler.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/vm-state-inl.h"
|
2018-11-03 00:13:22 +00:00
|
|
|
#include "src/objects/smi.h"
|
2019-08-21 15:22:17 +00:00
|
|
|
#include "src/profiler/tick-sample.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2014-01-17 10:52:00 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
const char* TraceExtension::kSource =
|
|
|
|
"native function trace();"
|
|
|
|
"native function js_trace();"
|
|
|
|
"native function js_entry_sp();"
|
|
|
|
"native function js_entry_sp_level2();";
|
|
|
|
|
|
|
|
|
2015-09-10 13:09:53 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> TraceExtension::GetNativeFunctionTemplate(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::String> name) {
|
2020-03-09 10:41:45 +00:00
|
|
|
if (name->StrictEquals(v8::String::NewFromUtf8Literal(isolate, "trace"))) {
|
2014-01-17 10:52:00 +00:00
|
|
|
return v8::FunctionTemplate::New(isolate, TraceExtension::Trace);
|
2018-10-25 10:13:41 +00:00
|
|
|
} else if (name->StrictEquals(
|
2020-03-09 10:41:45 +00:00
|
|
|
v8::String::NewFromUtf8Literal(isolate, "js_trace"))) {
|
2014-01-17 10:52:00 +00:00
|
|
|
return v8::FunctionTemplate::New(isolate, TraceExtension::JSTrace);
|
2018-10-25 10:13:41 +00:00
|
|
|
} else if (name->StrictEquals(
|
2020-03-09 10:41:45 +00:00
|
|
|
v8::String::NewFromUtf8Literal(isolate, "js_entry_sp"))) {
|
2014-01-17 10:52:00 +00:00
|
|
|
return v8::FunctionTemplate::New(isolate, TraceExtension::JSEntrySP);
|
2020-03-09 10:41:45 +00:00
|
|
|
} else if (name->StrictEquals(v8::String::NewFromUtf8Literal(
|
|
|
|
isolate, "js_entry_sp_level2"))) {
|
2014-01-17 10:52:00 +00:00
|
|
|
return v8::FunctionTemplate::New(isolate, TraceExtension::JSEntrySPLevel2);
|
|
|
|
}
|
2017-12-19 11:39:50 +00:00
|
|
|
UNREACHABLE();
|
2014-01-17 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Address TraceExtension::GetFP(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
// Convert frame pointer from encoding as smis in the arguments to a pointer.
|
|
|
|
CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform.
|
|
|
|
#if defined(V8_HOST_ARCH_32_BIT)
|
|
|
|
Address fp = *reinterpret_cast<Address*>(*args[0]);
|
|
|
|
#elif defined(V8_HOST_ARCH_64_BIT)
|
2018-06-05 10:45:41 +00:00
|
|
|
uint64_t kSmiValueMask =
|
|
|
|
(static_cast<uintptr_t>(1) << (kSmiValueSize - 1)) - 1;
|
|
|
|
uint64_t low_bits =
|
2018-11-03 00:13:22 +00:00
|
|
|
Smi(*reinterpret_cast<Address*>(*args[0])).value() & kSmiValueMask;
|
2018-06-05 10:45:41 +00:00
|
|
|
uint64_t high_bits =
|
2018-11-03 00:13:22 +00:00
|
|
|
Smi(*reinterpret_cast<Address*>(*args[1])).value() & kSmiValueMask;
|
2018-06-05 10:45:41 +00:00
|
|
|
Address fp =
|
|
|
|
static_cast<Address>((high_bits << (kSmiValueSize - 1)) | low_bits);
|
2014-01-17 10:52:00 +00:00
|
|
|
#else
|
|
|
|
#error Host architecture is neither 32-bit nor 64-bit.
|
|
|
|
#endif
|
2018-04-13 22:28:05 +00:00
|
|
|
printf("Trace: %p\n", reinterpret_cast<void*>(fp));
|
2014-01-17 10:52:00 +00:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:22:17 +00:00
|
|
|
static struct { TickSample* sample; } trace_env = {nullptr};
|
2014-01-17 10:52:00 +00:00
|
|
|
|
2019-08-21 15:22:17 +00:00
|
|
|
void TraceExtension::InitTraceEnv(TickSample* sample) {
|
2014-01-17 10:52:00 +00:00
|
|
|
trace_env.sample = sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceExtension::DoTrace(Address fp) {
|
|
|
|
RegisterState regs;
|
2018-04-13 22:28:05 +00:00
|
|
|
regs.fp = reinterpret_cast<void*>(fp);
|
2014-01-17 10:52:00 +00:00
|
|
|
// sp is only used to define stack high bound
|
2018-04-13 22:28:05 +00:00
|
|
|
regs.sp = reinterpret_cast<void*>(
|
|
|
|
reinterpret_cast<Address>(trace_env.sample) - 10240);
|
2019-08-21 15:22:17 +00:00
|
|
|
trace_env.sample->Init(CcTest::i_isolate(), regs,
|
|
|
|
TickSample::kSkipCEntryFrame, true);
|
2014-01-17 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TraceExtension::Trace(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-01-20 14:14:29 +00:00
|
|
|
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
|
|
|
|
i::VMState<EXTERNAL> state(isolate);
|
2018-04-13 22:28:05 +00:00
|
|
|
Address address = reinterpret_cast<Address>(&TraceExtension::Trace);
|
2015-01-20 14:14:29 +00:00
|
|
|
i::ExternalCallbackScope call_scope(isolate, address);
|
2014-01-17 10:52:00 +00:00
|
|
|
DoTrace(GetFP(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hide c_entry_fp to emulate situation when sampling is done while
|
|
|
|
// pure JS code is being executed
|
|
|
|
static void DoTraceHideCEntryFPAddress(Address fp) {
|
|
|
|
v8::internal::Address saved_c_frame_fp =
|
|
|
|
*(CcTest::i_isolate()->c_entry_fp_address());
|
|
|
|
CHECK(saved_c_frame_fp);
|
|
|
|
*(CcTest::i_isolate()->c_entry_fp_address()) = 0;
|
|
|
|
i::TraceExtension::DoTrace(fp);
|
|
|
|
*(CcTest::i_isolate()->c_entry_fp_address()) = saved_c_frame_fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TraceExtension::JSTrace(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-01-20 14:14:29 +00:00
|
|
|
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
|
|
|
|
i::VMState<EXTERNAL> state(isolate);
|
2018-04-13 22:28:05 +00:00
|
|
|
Address address = reinterpret_cast<Address>(&TraceExtension::JSTrace);
|
2015-01-20 14:14:29 +00:00
|
|
|
i::ExternalCallbackScope call_scope(isolate, address);
|
2014-01-17 10:52:00 +00:00
|
|
|
DoTraceHideCEntryFPAddress(GetFP(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Address TraceExtension::GetJsEntrySp() {
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(CcTest::i_isolate()->thread_local_top());
|
2014-01-17 10:52:00 +00:00
|
|
|
return CcTest::i_isolate()->js_entry_sp();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TraceExtension::JSEntrySP(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(GetJsEntrySp());
|
2014-01-17 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TraceExtension::JSEntrySPLevel2(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::HandleScope scope(args.GetIsolate());
|
|
|
|
const Address js_entry_sp = GetJsEntrySp();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(js_entry_sp);
|
2014-01-17 10:52:00 +00:00
|
|
|
CompileRun("js_entry_sp();");
|
|
|
|
CHECK_EQ(js_entry_sp, GetJsEntrySp());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|