1// 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;
94  trace_env.sample->Init(CcTest::i_isolate(), regs);
95}
96
97
98void TraceExtension::Trace(const v8::FunctionCallbackInfo<v8::Value>& args) {
99  DoTrace(GetFP(args));
100}
101
102
103// Hide c_entry_fp to emulate situation when sampling is done while
104// pure JS code is being executed
105static void DoTraceHideCEntryFPAddress(Address fp) {
106  v8::internal::Address saved_c_frame_fp =
107      *(CcTest::i_isolate()->c_entry_fp_address());
108  CHECK(saved_c_frame_fp);
109  *(CcTest::i_isolate()->c_entry_fp_address()) = 0;
110  i::TraceExtension::DoTrace(fp);
111  *(CcTest::i_isolate()->c_entry_fp_address()) = saved_c_frame_fp;
112}
113
114
115void TraceExtension::JSTrace(const v8::FunctionCallbackInfo<v8::Value>& args) {
116  DoTraceHideCEntryFPAddress(GetFP(args));
117}
118
119
120Address TraceExtension::GetJsEntrySp() {
121  CHECK_NE(NULL, CcTest::i_isolate()->thread_local_top());
122  return CcTest::i_isolate()->js_entry_sp();
123}
124
125
126void TraceExtension::JSEntrySP(
127    const v8::FunctionCallbackInfo<v8::Value>& args) {
128  CHECK_NE(0, GetJsEntrySp());
129}
130
131
132void TraceExtension::JSEntrySPLevel2(
133    const v8::FunctionCallbackInfo<v8::Value>& args) {
134  v8::HandleScope scope(args.GetIsolate());
135  const Address js_entry_sp = GetJsEntrySp();
136  CHECK_NE(0, js_entry_sp);
137  CompileRun("js_entry_sp();");
138  CHECK_EQ(js_entry_sp, GetJsEntrySp());
139}
140
141
142} }  // namespace v8::internal
143