blob: e38d74e1bd68993aec479bf5046bc487dd763126 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +000027//
28// Tests of profiler-related functions from log.h
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include <stdlib.h>
31
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034#include "src/api.h"
35#include "src/codegen.h"
36#include "src/disassembler.h"
37#include "src/isolate.h"
38#include "src/log.h"
Ben Murdochc5610432016-08-08 18:44:38 +010039#include "src/profiler/tick-sample.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#include "src/vm-state-inl.h"
41#include "test/cctest/cctest.h"
42#include "test/cctest/trace-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44using v8::Function;
45using v8::Local;
46using v8::Object;
47using v8::Script;
48using v8::String;
49using v8::Value;
50
51using v8::internal::byte;
52using v8::internal::Address;
53using v8::internal::Handle;
Steve Block44f0eee2011-05-26 01:26:41 +010054using v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +000055using v8::internal::JSFunction;
Steve Blocka7e24c12009-10-30 11:49:00 +000056using v8::internal::TickSample;
Steve Blocka7e24c12009-10-30 11:49:00 +000057
Steve Blocka7e24c12009-10-30 11:49:00 +000058
Ben Murdoche0cee9b2011-05-25 10:26:03 +010059static bool IsAddressWithinFuncCode(JSFunction* function, Address addr) {
Ben Murdochda12d292016-06-02 14:46:10 +010060 i::AbstractCode* code = function->abstract_code();
Ben Murdoche0cee9b2011-05-25 10:26:03 +010061 return code->contains(addr);
62}
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064
65static bool IsAddressWithinFuncCode(v8::Local<v8::Context> context,
66 const char* func_name,
67 Address addr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 v8::Local<v8::Value> func =
69 context->Global()->Get(context, v8_str(func_name)).ToLocalChecked();
Ben Murdoche0cee9b2011-05-25 10:26:03 +010070 CHECK(func->IsFunction());
71 JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func));
72 return IsAddressWithinFuncCode(js_func, addr);
Steve Blocka7e24c12009-10-30 11:49:00 +000073}
74
75
Iain Merrick9ac36c92010-09-13 15:29:50 +010076// This C++ function is called as a constructor, to grab the frame pointer
77// from the calling function. When this function runs, the stack contains
78// a C_Entry frame and a Construct frame above the calling function's frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079static void construct_call(const v8::FunctionCallbackInfo<v8::Value>& args) {
80 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
81 i::StackFrameIterator frame_iterator(isolate);
Iain Merrick9ac36c92010-09-13 15:29:50 +010082 CHECK(frame_iterator.frame()->is_exit());
83 frame_iterator.Advance();
84 CHECK(frame_iterator.frame()->is_construct());
85 frame_iterator.Advance();
Ben Murdoch097c5b22016-05-18 11:27:45 +010086 if (i::FLAG_ignition) {
87 // Skip over bytecode handler frame.
88 CHECK(frame_iterator.frame()->type() == i::StackFrame::STUB);
89 frame_iterator.Advance();
90 }
Iain Merrick9ac36c92010-09-13 15:29:50 +010091 i::StackFrame* calling_frame = frame_iterator.frame();
92 CHECK(calling_frame->is_java_script());
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
Iain Merrick9ac36c92010-09-13 15:29:50 +010095#if defined(V8_HOST_ARCH_32_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +010096 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 args.This()
98 ->Set(context, v8_str("low_bits"), v8_num(low_bits >> 1))
99 .FromJust();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100100#elif defined(V8_HOST_ARCH_64_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100101 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp());
Ben Murdochf87a2032010-10-22 12:50:53 +0100102 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff);
103 int32_t high_bits = static_cast<int32_t>(fp >> 32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 args.This()->Set(context, v8_str("low_bits"), v8_num(low_bits)).FromJust();
105 args.This()->Set(context, v8_str("high_bits"), v8_num(high_bits)).FromJust();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100106#else
107#error Host architecture is neither 32-bit nor 64-bit.
108#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 args.GetReturnValue().Set(args.This());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100110}
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112
Iain Merrick9ac36c92010-09-13 15:29:50 +0100113// Use the API to create a JSFunction object that calls the above C++ function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114void CreateFramePointerGrabberConstructor(v8::Local<v8::Context> context,
115 const char* constructor_name) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100116 Local<v8::FunctionTemplate> constructor_template =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 v8::FunctionTemplate::New(context->GetIsolate(), construct_call);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100118 constructor_template->SetClassName(v8_str("FPGrabber"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 Local<Function> fun =
120 constructor_template->GetFunction(context).ToLocalChecked();
121 context->Global()->Set(context, v8_str(constructor_name), fun).FromJust();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100122}
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
124
125// Creates a global function named 'func_name' that calls the tracing
126// function 'trace_func_name' with an actual EBP register value,
Iain Merrick9ac36c92010-09-13 15:29:50 +0100127// encoded as one or two Smis.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128static void CreateTraceCallerFunction(v8::Local<v8::Context> context,
129 const char* func_name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 const char* trace_func_name) {
131 i::EmbeddedVector<char, 256> trace_call_buf;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 i::SNPrintF(trace_call_buf,
133 "function %s() {"
134 " fp = new FPGrabber();"
135 " %s(fp.low_bits, fp.high_bits);"
136 "}",
137 func_name, trace_func_name);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100138
139 // Create the FPGrabber function, which grabs the caller's frame pointer
140 // when called as a constructor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 CreateFramePointerGrabberConstructor(context, "FPGrabber");
Steve Blocka7e24c12009-10-30 11:49:00 +0000142
143 // Compile the script.
John Reck59135872010-11-02 12:39:01 -0700144 CompileRun(trace_call_buf.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000145}
146
147
Steve Block6ded16b2010-05-10 14:33:55 +0100148// This test verifies that stack tracing works when called during
149// execution of a native function called from JS code. In this case,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150// TickSample::Trace uses Isolate::c_entry_fp as a starting point for stack
Steve Block6ded16b2010-05-10 14:33:55 +0100151// walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000152TEST(CFromJSStackTrace) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100153 // BUG(1303) Inlining of JSFuncDoTrace() in JSTrace below breaks this test.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 i::FLAG_turbo_inlining = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100155 i::FLAG_use_inlining = false;
156
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 i::TraceExtension::InitTraceEnv(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000159
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 v8::HandleScope scope(CcTest::isolate());
161 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
162 v8::Context::Scope context_scope(context);
163
Steve Block6ded16b2010-05-10 14:33:55 +0100164 // Create global function JSFuncDoTrace which calls
165 // extension function trace() with the current frame pointer value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 CreateTraceCallerFunction(context, "JSFuncDoTrace", "trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100167 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 "function JSTrace() {"
169 " JSFuncDoTrace();"
170 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100171 "JSTrace();\n"
172 "true;");
173 CHECK(!result.IsEmpty());
174 // When stack tracer is invoked, the stack should look as follows:
175 // script [JS]
176 // JSTrace() [JS]
177 // JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100178 // trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100179 // DoTrace(EBP) [native]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 // TickSample::Trace
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181
Steve Block44f0eee2011-05-26 01:26:41 +0100182 CHECK(sample.has_external_callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100183 CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::Trace),
184 sample.external_callback_entry);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100185
Steve Block6ded16b2010-05-10 14:33:55 +0100186 // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 unsigned base = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100188 CHECK_GT(sample.frames_count, base + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100189
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 CHECK(IsAddressWithinFuncCode(
191 context, "JSFuncDoTrace", sample.stack[base + 0]));
192 CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
195
Steve Block6ded16b2010-05-10 14:33:55 +0100196// This test verifies that stack tracing works when called during
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197// execution of JS code. However, as calling TickSample::Trace requires
Steve Block6ded16b2010-05-10 14:33:55 +0100198// entering native code, we can only emulate pure JS by erasing
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199// Isolate::c_entry_fp value. In this case, TickSample::Trace uses passed frame
Steve Block6ded16b2010-05-10 14:33:55 +0100200// pointer value as a starting point for stack walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000201TEST(PureJSStackTrace) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100202 // This test does not pass with inlining enabled since inlined functions
203 // don't appear in the stack trace.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 i::FLAG_turbo_inlining = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205 i::FLAG_use_inlining = false;
206
Steve Blocka7e24c12009-10-30 11:49:00 +0000207 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 i::TraceExtension::InitTraceEnv(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000209
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 v8::HandleScope scope(CcTest::isolate());
211 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
212 v8::Context::Scope context_scope(context);
213
Steve Block6ded16b2010-05-10 14:33:55 +0100214 // Create global function JSFuncDoTrace which calls
215 // extension function js_trace() with the current frame pointer value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 CreateTraceCallerFunction(context, "JSFuncDoTrace", "js_trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100217 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 "function JSTrace() {"
219 " JSFuncDoTrace();"
220 "};\n"
221 "function OuterJSTrace() {"
222 " JSTrace();"
223 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100224 "OuterJSTrace();\n"
225 "true;");
226 CHECK(!result.IsEmpty());
227 // When stack tracer is invoked, the stack should look as follows:
228 // script [JS]
229 // OuterJSTrace() [JS]
230 // JSTrace() [JS]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100231 // JSFuncDoTrace() [JS]
232 // js_trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100233 // DoTraceHideCEntryFPAddress(EBP) [native]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 // TickSample::Trace
Steve Block6ded16b2010-05-10 14:33:55 +0100235 //
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236
Steve Block44f0eee2011-05-26 01:26:41 +0100237 CHECK(sample.has_external_callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100238 CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::JSTrace),
239 sample.external_callback_entry);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100240
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 unsigned base = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100243 CHECK_GT(sample.frames_count, base + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 0]));
245 CHECK(IsAddressWithinFuncCode(
246 context, "OuterJSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000247}
248
249
Steve Blockd0582a62009-12-15 09:54:21 +0000250static void CFuncDoTrace(byte dummy_parameter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 Address fp;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400252#if V8_HAS_BUILTIN_FRAME_ADDRESS
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 fp = reinterpret_cast<Address>(__builtin_frame_address(0));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400254#elif V8_CC_MSVC
Steve Blockd0582a62009-12-15 09:54:21 +0000255 // Approximate a frame pointer address. We compile without base pointers,
256 // so we can't trust ebp/rbp.
257 fp = &dummy_parameter - 2 * sizeof(void*); // NOLINT
258#else
259#error Unexpected platform.
Steve Blocka7e24c12009-10-30 11:49:00 +0000260#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 i::TraceExtension::DoTrace(fp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262}
263
264
265static int CFunc(int depth) {
266 if (depth <= 0) {
Steve Blockd0582a62009-12-15 09:54:21 +0000267 CFuncDoTrace(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 return 0;
269 } else {
270 return CFunc(depth - 1) + 1;
271 }
272}
273
274
Steve Block6ded16b2010-05-10 14:33:55 +0100275// This test verifies that stack tracing doesn't crash when called on
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276// pure native code. TickSample::Trace only unrolls JS code, so we can't
Steve Block6ded16b2010-05-10 14:33:55 +0100277// get any meaningful info here.
Steve Blocka7e24c12009-10-30 11:49:00 +0000278TEST(PureCStackTrace) {
279 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 i::TraceExtension::InitTraceEnv(&sample);
281 v8::HandleScope scope(CcTest::isolate());
282 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
283 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 // Check that sampler doesn't crash
285 CHECK_EQ(10, CFunc(10));
286}
287
288
289TEST(JsEntrySp) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 v8::HandleScope scope(CcTest::isolate());
291 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
292 v8::Context::Scope context_scope(context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 CHECK(!i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 CompileRun("a = 1; b = a + 1;");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000295 CHECK(!i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 CompileRun("js_entry_sp();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 CHECK(!i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 CompileRun("js_entry_sp_level2();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299 CHECK(!i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000300}