test-log-stack-tracer.cc 10.9 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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.
27 28 29 30 31
//
// Tests of profiler-related functions from log.h

#include <stdlib.h>

32
#include "include/v8-profiler.h"
33 34 35 36 37
#include "src/api.h"
#include "src/codegen.h"
#include "src/disassembler.h"
#include "src/isolate.h"
#include "src/log.h"
38
#include "src/v8.h"
39 40 41
#include "src/vm-state-inl.h"
#include "test/cctest/cctest.h"
#include "test/cctest/trace-extension.h"
42 43 44 45 46 47

using v8::Function;
using v8::Local;
using v8::Object;
using v8::Script;
using v8::String;
48
using v8::TickSample;
49 50 51
using v8::Value;

using v8::internal::byte;
52
using v8::internal::Address;
53
using v8::internal::Handle;
54
using v8::internal::Isolate;
55 56
using v8::internal::JSFunction;

57 58
static bool IsAddressWithinFuncCode(JSFunction* function, void* addr) {
  Address address = reinterpret_cast<Address>(addr);
59
  i::AbstractCode* code = function->abstract_code();
60
  return code->contains(address);
61 62
}

63
static bool IsAddressWithinFuncCode(v8::Local<v8::Context> context,
64
                                    const char* func_name, void* addr) {
65 66
  v8::Local<v8::Value> func =
      context->Global()->Get(context, v8_str(func_name)).ToLocalChecked();
67 68 69
  CHECK(func->IsFunction());
  JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func));
  return IsAddressWithinFuncCode(js_func, addr);
70 71 72
}


73 74 75
// This C++ function is called as a constructor, to grab the frame pointer
// from the calling function.  When this function runs, the stack contains
// a C_Entry frame and a Construct frame above the calling function's frame.
76
static void construct_call(const v8::FunctionCallbackInfo<v8::Value>& args) {
77 78
  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
  i::StackFrameIterator frame_iterator(isolate);
79 80
  CHECK(frame_iterator.frame()->is_exit() ||
        frame_iterator.frame()->is_builtin_exit());
81 82 83
  frame_iterator.Advance();
  CHECK(frame_iterator.frame()->is_construct());
  frame_iterator.Advance();
84 85 86 87 88
  if (i::FLAG_ignition) {
    // Skip over bytecode handler frame.
    CHECK(frame_iterator.frame()->type() == i::StackFrame::STUB);
    frame_iterator.Advance();
  }
89 90 91
  i::StackFrame* calling_frame = frame_iterator.frame();
  CHECK(calling_frame->is_java_script());

92
  v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
93
#if defined(V8_HOST_ARCH_32_BIT)
94
  int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp());
95 96 97
  args.This()
      ->Set(context, v8_str("low_bits"), v8_num(low_bits >> 1))
      .FromJust();
98
#elif defined(V8_HOST_ARCH_64_BIT)
99
  uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp());
100 101
  int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff);
  int32_t high_bits = static_cast<int32_t>(fp >> 32);
102 103
  args.This()->Set(context, v8_str("low_bits"), v8_num(low_bits)).FromJust();
  args.This()->Set(context, v8_str("high_bits"), v8_num(high_bits)).FromJust();
104 105 106
#else
#error Host architecture is neither 32-bit nor 64-bit.
#endif
107
  args.GetReturnValue().Set(args.This());
108
}
109 110


111
// Use the API to create a JSFunction object that calls the above C++ function.
112 113
void CreateFramePointerGrabberConstructor(v8::Local<v8::Context> context,
                                          const char* constructor_name) {
114
    Local<v8::FunctionTemplate> constructor_template =
115
        v8::FunctionTemplate::New(context->GetIsolate(), construct_call);
116
    constructor_template->SetClassName(v8_str("FPGrabber"));
117 118 119
    Local<Function> fun =
        constructor_template->GetFunction(context).ToLocalChecked();
    context->Global()->Set(context, v8_str(constructor_name), fun).FromJust();
120
}
121 122 123 124


// Creates a global function named 'func_name' that calls the tracing
// function 'trace_func_name' with an actual EBP register value,
125
// encoded as one or two Smis.
126 127
static void CreateTraceCallerFunction(v8::Local<v8::Context> context,
                                      const char* func_name,
128 129
                                      const char* trace_func_name) {
  i::EmbeddedVector<char, 256> trace_call_buf;
130 131 132 133 134 135
  i::SNPrintF(trace_call_buf,
              "function %s() {"
              "  fp = new FPGrabber();"
              "  %s(fp.low_bits, fp.high_bits);"
              "}",
              func_name, trace_func_name);
136 137 138

  // Create the FPGrabber function, which grabs the caller's frame pointer
  // when called as a constructor.
139
  CreateFramePointerGrabberConstructor(context, "FPGrabber");
140 141

  // Compile the script.
142
  CompileRun(trace_call_buf.start());
143 144 145
}


146 147
// This test verifies that stack tracing works when called during
// execution of a native function called from JS code. In this case,
148
// TickSample::Trace uses Isolate::c_entry_fp as a starting point for stack
149
// walking.
150
TEST(CFromJSStackTrace) {
151
  // BUG(1303) Inlining of JSFuncDoTrace() in JSTrace below breaks this test.
152
  i::FLAG_turbo_inlining = false;
153
  i::FLAG_use_inlining = false;
154

155
  TickSample sample;
156
  i::TraceExtension::InitTraceEnv(&sample);
157

158
  v8::HandleScope scope(CcTest::isolate());
159 160 161
  v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
  v8::Context::Scope context_scope(context);

162 163
  // Create global function JSFuncDoTrace which calls
  // extension function trace() with the current frame pointer value.
164
  CreateTraceCallerFunction(context, "JSFuncDoTrace", "trace");
165
  Local<Value> result = CompileRun(
166 167 168
      "function JSTrace() {"
      "         JSFuncDoTrace();"
      "};\n"
169 170 171
      "JSTrace();\n"
      "true;");
  CHECK(!result.IsEmpty());
172 173 174 175
  // When stack tracer is invoked, the stack should look as follows:
  // script [JS]
  //   JSTrace() [JS]
  //     JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi]
176
  //       trace(EBP) [native (extension)]
177
  //         DoTrace(EBP) [native]
178
  //           TickSample::Trace
179

180
  CHECK(sample.has_external_callback);
181 182
  CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::Trace),
           sample.external_callback_entry);
183

184
  // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace"
185
  unsigned base = 0;
186
  CHECK_GT(sample.frames_count, base + 1);
187

188 189 190
  CHECK(IsAddressWithinFuncCode(
      context, "JSFuncDoTrace", sample.stack[base + 0]));
  CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 1]));
191 192 193
}


194
// This test verifies that stack tracing works when called during
195
// execution of JS code. However, as calling TickSample::Trace requires
196
// entering native code, we can only emulate pure JS by erasing
197
// Isolate::c_entry_fp value. In this case, TickSample::Trace uses passed frame
198
// pointer value as a starting point for stack walking.
199
TEST(PureJSStackTrace) {
200 201
  // This test does not pass with inlining enabled since inlined functions
  // don't appear in the stack trace.
202
  i::FLAG_turbo_inlining = false;
203 204
  i::FLAG_use_inlining = false;

205
  TickSample sample;
206
  i::TraceExtension::InitTraceEnv(&sample);
207

208
  v8::HandleScope scope(CcTest::isolate());
209 210 211
  v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
  v8::Context::Scope context_scope(context);

212 213
  // Create global function JSFuncDoTrace which calls
  // extension function js_trace() with the current frame pointer value.
214
  CreateTraceCallerFunction(context, "JSFuncDoTrace", "js_trace");
215
  Local<Value> result = CompileRun(
216 217 218 219 220 221
      "function JSTrace() {"
      "         JSFuncDoTrace();"
      "};\n"
      "function OuterJSTrace() {"
      "         JSTrace();"
      "};\n"
222 223 224
      "OuterJSTrace();\n"
      "true;");
  CHECK(!result.IsEmpty());
225 226 227 228
  // When stack tracer is invoked, the stack should look as follows:
  // script [JS]
  //   OuterJSTrace() [JS]
  //     JSTrace() [JS]
229 230
  //       JSFuncDoTrace() [JS]
  //         js_trace(EBP) [native (extension)]
231
  //           DoTraceHideCEntryFPAddress(EBP) [native]
232
  //             TickSample::Trace
233
  //
234

235
  CHECK(sample.has_external_callback);
236 237
  CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::JSTrace),
           sample.external_callback_entry);
238

239
  // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
240
  unsigned base = 0;
241
  CHECK_GT(sample.frames_count, base + 1);
242 243 244
  CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 0]));
  CHECK(IsAddressWithinFuncCode(
      context, "OuterJSTrace", sample.stack[base + 1]));
245 246 247
}


248
static void CFuncDoTrace(byte dummy_parameter) {
249
  Address fp;
250
#if V8_HAS_BUILTIN_FRAME_ADDRESS
251
  fp = reinterpret_cast<Address>(__builtin_frame_address(0));
252
#elif V8_CC_MSVC
253 254
  // Approximate a frame pointer address. We compile without base pointers,
  // so we can't trust ebp/rbp.
255
  fp = &dummy_parameter - 2 * sizeof(void*);  // NOLINT
256 257
#else
#error Unexpected platform.
258
#endif
259
  i::TraceExtension::DoTrace(fp);
260 261 262 263 264
}


static int CFunc(int depth) {
  if (depth <= 0) {
265
    CFuncDoTrace(0);
266 267 268 269 270 271 272
    return 0;
  } else {
    return CFunc(depth - 1) + 1;
  }
}


273
// This test verifies that stack tracing doesn't crash when called on
274
// pure native code. TickSample::Trace only unrolls JS code, so we can't
275
// get any meaningful info here.
276 277
TEST(PureCStackTrace) {
  TickSample sample;
278
  i::TraceExtension::InitTraceEnv(&sample);
279 280 281
  v8::HandleScope scope(CcTest::isolate());
  v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
  v8::Context::Scope context_scope(context);
282 283 284 285 286
  // Check that sampler doesn't crash
  CHECK_EQ(10, CFunc(10));
}


287
TEST(JsEntrySp) {
288
  v8::HandleScope scope(CcTest::isolate());
289 290
  v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
  v8::Context::Scope context_scope(context);
291
  CHECK(!i::TraceExtension::GetJsEntrySp());
292
  CompileRun("a = 1; b = a + 1;");
293
  CHECK(!i::TraceExtension::GetJsEntrySp());
294
  CompileRun("js_entry_sp();");
295
  CHECK(!i::TraceExtension::GetJsEntrySp());
296
  CompileRun("js_entry_sp_level2();");
297
  CHECK(!i::TraceExtension::GetJsEntrySp());
298
}