blob: 0a1ff87d1cb2bcad27f7d9dfcd6c12a263b1ccb0 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "test/cctest/trace-extension.h"
29
30#include "src/sampler.h"
31#include "test/cctest/cctest.h"
32
33namespace v8 {
34namespace internal {
35
36const char* TraceExtension::kSource =
37 "native function trace();"
38 "native function js_trace();"
39 "native function js_entry_sp();"
40 "native function js_entry_sp_level2();";
41
42
43v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunctionTemplate(
44 v8::Isolate* isolate, v8::Handle<v8::String> name) {
45 if (name->Equals(v8::String::NewFromUtf8(isolate, "trace"))) {
46 return v8::FunctionTemplate::New(isolate, TraceExtension::Trace);
47 } else if (name->Equals(v8::String::NewFromUtf8(isolate, "js_trace"))) {
48 return v8::FunctionTemplate::New(isolate, TraceExtension::JSTrace);
49 } else if (name->Equals(v8::String::NewFromUtf8(isolate, "js_entry_sp"))) {
50 return v8::FunctionTemplate::New(isolate, TraceExtension::JSEntrySP);
51 } else if (name->Equals(v8::String::NewFromUtf8(isolate,
52 "js_entry_sp_level2"))) {
53 return v8::FunctionTemplate::New(isolate, TraceExtension::JSEntrySPLevel2);
54 } else {
55 CHECK(false);
56 return v8::Handle<v8::FunctionTemplate>();
57 }
58}
59
60
61Address TraceExtension::GetFP(const v8::FunctionCallbackInfo<v8::Value>& args) {
62 // Convert frame pointer from encoding as smis in the arguments to a pointer.
63 CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform.
64#if defined(V8_HOST_ARCH_32_BIT)
65 Address fp = *reinterpret_cast<Address*>(*args[0]);
66#elif defined(V8_HOST_ARCH_64_BIT)
67 int64_t low_bits = *reinterpret_cast<uint64_t*>(*args[0]) >> 32;
68 int64_t high_bits = *reinterpret_cast<uint64_t*>(*args[1]);
69 Address fp = reinterpret_cast<Address>(high_bits | low_bits);
70#else
71#error Host architecture is neither 32-bit nor 64-bit.
72#endif
73 printf("Trace: %p\n", fp);
74 return fp;
75}
76
77
78static struct {
79 TickSample* sample;
80} trace_env = { NULL };
81
82
83void TraceExtension::InitTraceEnv(TickSample* sample) {
84 trace_env.sample = sample;
85}
86
87
88void TraceExtension::DoTrace(Address fp) {
89 RegisterState regs;
90 regs.fp = fp;
91 // sp is only used to define stack high bound
92 regs.sp =
93 reinterpret_cast<Address>(trace_env.sample) - 10240;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040094 trace_env.sample->Init(CcTest::i_isolate(), regs,
95 TickSample::kSkipCEntryFrame);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096}
97
98
99void TraceExtension::Trace(const v8::FunctionCallbackInfo<v8::Value>& args) {
100 DoTrace(GetFP(args));
101}
102
103
104// Hide c_entry_fp to emulate situation when sampling is done while
105// pure JS code is being executed
106static void DoTraceHideCEntryFPAddress(Address fp) {
107 v8::internal::Address saved_c_frame_fp =
108 *(CcTest::i_isolate()->c_entry_fp_address());
109 CHECK(saved_c_frame_fp);
110 *(CcTest::i_isolate()->c_entry_fp_address()) = 0;
111 i::TraceExtension::DoTrace(fp);
112 *(CcTest::i_isolate()->c_entry_fp_address()) = saved_c_frame_fp;
113}
114
115
116void TraceExtension::JSTrace(const v8::FunctionCallbackInfo<v8::Value>& args) {
117 DoTraceHideCEntryFPAddress(GetFP(args));
118}
119
120
121Address TraceExtension::GetJsEntrySp() {
122 CHECK_NE(NULL, CcTest::i_isolate()->thread_local_top());
123 return CcTest::i_isolate()->js_entry_sp();
124}
125
126
127void TraceExtension::JSEntrySP(
128 const v8::FunctionCallbackInfo<v8::Value>& args) {
129 CHECK_NE(0, GetJsEntrySp());
130}
131
132
133void TraceExtension::JSEntrySPLevel2(
134 const v8::FunctionCallbackInfo<v8::Value>& args) {
135 v8::HandleScope scope(args.GetIsolate());
136 const Address js_entry_sp = GetJsEntrySp();
137 CHECK_NE(0, js_entry_sp);
138 CompileRun("js_entry_sp();");
139 CHECK_EQ(js_entry_sp, GetJsEntrySp());
140}
141
142
143} } // namespace v8::internal