blob: 334a2010532bd3373b7547ede2ff4142ea7988e7 [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"
39#include "src/sampler.h"
40#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) {
60 i::Code* code = function->code();
61 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) {
68 v8::Local<v8::Value> func = context->Global()->Get(v8_str(func_name));
Ben Murdoche0cee9b2011-05-25 10:26:03 +010069 CHECK(func->IsFunction());
70 JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func));
71 return IsAddressWithinFuncCode(js_func, addr);
Steve Blocka7e24c12009-10-30 11:49:00 +000072}
73
74
Iain Merrick9ac36c92010-09-13 15:29:50 +010075// This C++ function is called as a constructor, to grab the frame pointer
76// from the calling function. When this function runs, the stack contains
77// a C_Entry frame and a Construct frame above the calling function's frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078static void construct_call(const v8::FunctionCallbackInfo<v8::Value>& args) {
79 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
80 i::StackFrameIterator frame_iterator(isolate);
Iain Merrick9ac36c92010-09-13 15:29:50 +010081 CHECK(frame_iterator.frame()->is_exit());
82 frame_iterator.Advance();
83 CHECK(frame_iterator.frame()->is_construct());
84 frame_iterator.Advance();
85 i::StackFrame* calling_frame = frame_iterator.frame();
86 CHECK(calling_frame->is_java_script());
Steve Blocka7e24c12009-10-30 11:49:00 +000087
Iain Merrick9ac36c92010-09-13 15:29:50 +010088#if defined(V8_HOST_ARCH_32_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +010089 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp());
Iain Merrick9ac36c92010-09-13 15:29:50 +010090 args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1));
91#elif defined(V8_HOST_ARCH_64_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +010092 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp());
Ben Murdochf87a2032010-10-22 12:50:53 +010093 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff);
94 int32_t high_bits = static_cast<int32_t>(fp >> 32);
Iain Merrick9ac36c92010-09-13 15:29:50 +010095 args.This()->Set(v8_str("low_bits"), v8_num(low_bits));
96 args.This()->Set(v8_str("high_bits"), v8_num(high_bits));
97#else
98#error Host architecture is neither 32-bit nor 64-bit.
99#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 args.GetReturnValue().Set(args.This());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100101}
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
Iain Merrick9ac36c92010-09-13 15:29:50 +0100104// Use the API to create a JSFunction object that calls the above C++ function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105void CreateFramePointerGrabberConstructor(v8::Local<v8::Context> context,
106 const char* constructor_name) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100107 Local<v8::FunctionTemplate> constructor_template =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 v8::FunctionTemplate::New(context->GetIsolate(), construct_call);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100109 constructor_template->SetClassName(v8_str("FPGrabber"));
110 Local<Function> fun = constructor_template->GetFunction();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 context->Global()->Set(v8_str(constructor_name), fun);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100112}
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
114
115// Creates a global function named 'func_name' that calls the tracing
116// function 'trace_func_name' with an actual EBP register value,
Iain Merrick9ac36c92010-09-13 15:29:50 +0100117// encoded as one or two Smis.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118static void CreateTraceCallerFunction(v8::Local<v8::Context> context,
119 const char* func_name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 const char* trace_func_name) {
121 i::EmbeddedVector<char, 256> trace_call_buf;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 i::SNPrintF(trace_call_buf,
123 "function %s() {"
124 " fp = new FPGrabber();"
125 " %s(fp.low_bits, fp.high_bits);"
126 "}",
127 func_name, trace_func_name);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100128
129 // Create the FPGrabber function, which grabs the caller's frame pointer
130 // when called as a constructor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 CreateFramePointerGrabberConstructor(context, "FPGrabber");
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133 // Compile the script.
John Reck59135872010-11-02 12:39:01 -0700134 CompileRun(trace_call_buf.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000135}
136
137
Steve Block6ded16b2010-05-10 14:33:55 +0100138// This test verifies that stack tracing works when called during
139// execution of a native function called from JS code. In this case,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140// TickSample::Trace uses Isolate::c_entry_fp as a starting point for stack
Steve Block6ded16b2010-05-10 14:33:55 +0100141// walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000142TEST(CFromJSStackTrace) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100143 // BUG(1303) Inlining of JSFuncDoTrace() in JSTrace below breaks this test.
144 i::FLAG_use_inlining = false;
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 i::TraceExtension::InitTraceEnv(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 v8::HandleScope scope(CcTest::isolate());
150 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
151 v8::Context::Scope context_scope(context);
152
Steve Block6ded16b2010-05-10 14:33:55 +0100153 // Create global function JSFuncDoTrace which calls
154 // extension function trace() with the current frame pointer value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 CreateTraceCallerFunction(context, "JSFuncDoTrace", "trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100156 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 "function JSTrace() {"
158 " JSFuncDoTrace();"
159 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100160 "JSTrace();\n"
161 "true;");
162 CHECK(!result.IsEmpty());
163 // When stack tracer is invoked, the stack should look as follows:
164 // script [JS]
165 // JSTrace() [JS]
166 // JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100167 // trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100168 // DoTrace(EBP) [native]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 // TickSample::Trace
Ben Murdochb0fe1622011-05-05 13:52:32 +0100170
Steve Block44f0eee2011-05-26 01:26:41 +0100171 CHECK(sample.has_external_callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::Trace), sample.external_callback);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173
Steve Block6ded16b2010-05-10 14:33:55 +0100174 // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 unsigned base = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100176 CHECK_GT(sample.frames_count, base + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100177
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 CHECK(IsAddressWithinFuncCode(
179 context, "JSFuncDoTrace", sample.stack[base + 0]));
180 CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000181}
182
183
Steve Block6ded16b2010-05-10 14:33:55 +0100184// This test verifies that stack tracing works when called during
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185// execution of JS code. However, as calling TickSample::Trace requires
Steve Block6ded16b2010-05-10 14:33:55 +0100186// entering native code, we can only emulate pure JS by erasing
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187// Isolate::c_entry_fp value. In this case, TickSample::Trace uses passed frame
Steve Block6ded16b2010-05-10 14:33:55 +0100188// pointer value as a starting point for stack walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000189TEST(PureJSStackTrace) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100190 // This test does not pass with inlining enabled since inlined functions
191 // don't appear in the stack trace.
192 i::FLAG_use_inlining = false;
193
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 i::TraceExtension::InitTraceEnv(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 v8::HandleScope scope(CcTest::isolate());
198 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
199 v8::Context::Scope context_scope(context);
200
Steve Block6ded16b2010-05-10 14:33:55 +0100201 // Create global function JSFuncDoTrace which calls
202 // extension function js_trace() with the current frame pointer value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 CreateTraceCallerFunction(context, "JSFuncDoTrace", "js_trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100204 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 "function JSTrace() {"
206 " JSFuncDoTrace();"
207 "};\n"
208 "function OuterJSTrace() {"
209 " JSTrace();"
210 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100211 "OuterJSTrace();\n"
212 "true;");
213 CHECK(!result.IsEmpty());
214 // When stack tracer is invoked, the stack should look as follows:
215 // script [JS]
216 // OuterJSTrace() [JS]
217 // JSTrace() [JS]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100218 // JSFuncDoTrace() [JS]
219 // js_trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100220 // DoTraceHideCEntryFPAddress(EBP) [native]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 // TickSample::Trace
Steve Block6ded16b2010-05-10 14:33:55 +0100222 //
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223
Steve Block44f0eee2011-05-26 01:26:41 +0100224 CHECK(sample.has_external_callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::JSTrace), sample.external_callback);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100226
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228 unsigned base = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100229 CHECK_GT(sample.frames_count, base + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 0]));
231 CHECK(IsAddressWithinFuncCode(
232 context, "OuterJSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000233}
234
235
Steve Blockd0582a62009-12-15 09:54:21 +0000236static void CFuncDoTrace(byte dummy_parameter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 Address fp;
238#ifdef __GNUC__
239 fp = reinterpret_cast<Address>(__builtin_frame_address(0));
Steve Blockd0582a62009-12-15 09:54:21 +0000240#elif defined _MSC_VER
241 // Approximate a frame pointer address. We compile without base pointers,
242 // so we can't trust ebp/rbp.
243 fp = &dummy_parameter - 2 * sizeof(void*); // NOLINT
244#else
245#error Unexpected platform.
Steve Blocka7e24c12009-10-30 11:49:00 +0000246#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 i::TraceExtension::DoTrace(fp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000248}
249
250
251static int CFunc(int depth) {
252 if (depth <= 0) {
Steve Blockd0582a62009-12-15 09:54:21 +0000253 CFuncDoTrace(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 return 0;
255 } else {
256 return CFunc(depth - 1) + 1;
257 }
258}
259
260
Steve Block6ded16b2010-05-10 14:33:55 +0100261// This test verifies that stack tracing doesn't crash when called on
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262// pure native code. TickSample::Trace only unrolls JS code, so we can't
Steve Block6ded16b2010-05-10 14:33:55 +0100263// get any meaningful info here.
Steve Blocka7e24c12009-10-30 11:49:00 +0000264TEST(PureCStackTrace) {
265 TickSample sample;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 i::TraceExtension::InitTraceEnv(&sample);
267 v8::HandleScope scope(CcTest::isolate());
268 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
269 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 // Check that sampler doesn't crash
271 CHECK_EQ(10, CFunc(10));
272}
273
274
275TEST(JsEntrySp) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 v8::HandleScope scope(CcTest::isolate());
277 v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION);
278 v8::Context::Scope context_scope(context);
279 CHECK_EQ(0, i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 CompileRun("a = 1; b = a + 1;");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 CHECK_EQ(0, i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 CompileRun("js_entry_sp();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 CHECK_EQ(0, i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 CompileRun("js_entry_sp_level2();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 CHECK_EQ(0, i::TraceExtension::GetJsEntrySp());
Steve Blocka7e24c12009-10-30 11:49:00 +0000286}