blob: bf72184fb013d0a337f8238a4c523fc0e7014ef4 [file] [log] [blame]
Iain Merrick9ac36c92010-09-13 15:29:50 +01001// Copyright 2010 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.
Steve Blocka7e24c12009-10-30 11:49:00 +000027//
28// Tests of profiler-related functions from log.h
29
30#ifdef ENABLE_LOGGING_AND_PROFILING
31
32#include <stdlib.h>
33
34#include "v8.h"
35
Ben Murdoche0cee9b2011-05-25 10:26:03 +010036#include "api.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "codegen.h"
38#include "log.h"
39#include "top.h"
40#include "cctest.h"
41#include "disassembler.h"
42#include "register-allocator-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010043#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45using v8::Function;
46using v8::Local;
47using v8::Object;
48using v8::Script;
49using v8::String;
50using v8::Value;
51
52using v8::internal::byte;
53using v8::internal::Address;
54using v8::internal::Handle;
55using v8::internal::JSFunction;
56using v8::internal::StackTracer;
57using v8::internal::TickSample;
58using v8::internal::Top;
59
60namespace i = v8::internal;
61
62
63static v8::Persistent<v8::Context> env;
64
65
66static struct {
67 TickSample* sample;
68} trace_env = { NULL };
69
70
71static void InitTraceEnv(TickSample* sample) {
72 trace_env.sample = sample;
73}
74
75
76static void DoTrace(Address fp) {
Leon Clarked91b9f72010-01-27 17:25:45 +000077 trace_env.sample->fp = fp;
Steve Blocka7e24c12009-10-30 11:49:00 +000078 // sp is only used to define stack high bound
79 trace_env.sample->sp =
Leon Clarked91b9f72010-01-27 17:25:45 +000080 reinterpret_cast<Address>(trace_env.sample) - 10240;
Steve Blocka7e24c12009-10-30 11:49:00 +000081 StackTracer::Trace(trace_env.sample);
82}
83
84
85// Hide c_entry_fp to emulate situation when sampling is done while
86// pure JS code is being executed
87static void DoTraceHideCEntryFPAddress(Address fp) {
88 v8::internal::Address saved_c_frame_fp = *(Top::c_entry_fp_address());
89 CHECK(saved_c_frame_fp);
90 *(Top::c_entry_fp_address()) = 0;
91 DoTrace(fp);
92 *(Top::c_entry_fp_address()) = saved_c_frame_fp;
93}
94
95
Steve Blocka7e24c12009-10-30 11:49:00 +000096// --- T r a c e E x t e n s i o n ---
97
98class TraceExtension : public v8::Extension {
99 public:
100 TraceExtension() : v8::Extension("v8/trace", kSource) { }
101 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
102 v8::Handle<String> name);
103 static v8::Handle<v8::Value> Trace(const v8::Arguments& args);
104 static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args);
105 static v8::Handle<v8::Value> JSEntrySP(const v8::Arguments& args);
106 static v8::Handle<v8::Value> JSEntrySPLevel2(const v8::Arguments& args);
107 private:
108 static Address GetFP(const v8::Arguments& args);
109 static const char* kSource;
110};
111
112
113const char* TraceExtension::kSource =
114 "native function trace();"
115 "native function js_trace();"
116 "native function js_entry_sp();"
117 "native function js_entry_sp_level2();";
118
119v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction(
120 v8::Handle<String> name) {
121 if (name->Equals(String::New("trace"))) {
122 return v8::FunctionTemplate::New(TraceExtension::Trace);
123 } else if (name->Equals(String::New("js_trace"))) {
124 return v8::FunctionTemplate::New(TraceExtension::JSTrace);
125 } else if (name->Equals(String::New("js_entry_sp"))) {
126 return v8::FunctionTemplate::New(TraceExtension::JSEntrySP);
127 } else if (name->Equals(String::New("js_entry_sp_level2"))) {
128 return v8::FunctionTemplate::New(TraceExtension::JSEntrySPLevel2);
129 } else {
130 CHECK(false);
131 return v8::Handle<v8::FunctionTemplate>();
132 }
133}
134
135
136Address TraceExtension::GetFP(const v8::Arguments& args) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100137 // Convert frame pointer from encoding as smis in the arguments to a pointer.
138 CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform.
139#if defined(V8_HOST_ARCH_32_BIT)
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 Address fp = *reinterpret_cast<Address*>(*args[0]);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100141#elif defined(V8_HOST_ARCH_64_BIT)
142 int64_t low_bits = *reinterpret_cast<uint64_t*>(*args[0]) >> 32;
143 int64_t high_bits = *reinterpret_cast<uint64_t*>(*args[1]);
144 Address fp = reinterpret_cast<Address>(high_bits | low_bits);
145#else
146#error Host architecture is neither 32-bit nor 64-bit.
147#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 printf("Trace: %p\n", fp);
149 return fp;
150}
151
152
153v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) {
154 DoTrace(GetFP(args));
155 return v8::Undefined();
156}
157
158
159v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) {
160 DoTraceHideCEntryFPAddress(GetFP(args));
161 return v8::Undefined();
162}
163
164
165static Address GetJsEntrySp() {
166 CHECK_NE(NULL, Top::GetCurrentThread());
167 return Top::js_entry_sp(Top::GetCurrentThread());
168}
169
170
171v8::Handle<v8::Value> TraceExtension::JSEntrySP(const v8::Arguments& args) {
172 CHECK_NE(0, GetJsEntrySp());
173 return v8::Undefined();
174}
175
176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177v8::Handle<v8::Value> TraceExtension::JSEntrySPLevel2(
178 const v8::Arguments& args) {
179 v8::HandleScope scope;
180 const Address js_entry_sp = GetJsEntrySp();
181 CHECK_NE(0, js_entry_sp);
182 CompileRun("js_entry_sp();");
183 CHECK_EQ(js_entry_sp, GetJsEntrySp());
184 return v8::Undefined();
185}
186
187
188static TraceExtension kTraceExtension;
189v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension);
190
191
192static void InitializeVM() {
193 if (env.IsEmpty()) {
194 v8::HandleScope scope;
195 const char* extensions[] = { "v8/trace" };
196 v8::ExtensionConfiguration config(1, extensions);
197 env = v8::Context::New(&config);
198 }
199 v8::HandleScope scope;
200 env->Enter();
201}
202
203
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100204static bool IsAddressWithinFuncCode(JSFunction* function, Address addr) {
205 i::Code* code = function->code();
206 return code->contains(addr);
207}
208
209static bool IsAddressWithinFuncCode(const char* func_name, Address addr) {
210 v8::Local<v8::Value> func = env->Global()->Get(v8_str(func_name));
211 CHECK(func->IsFunction());
212 JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func));
213 return IsAddressWithinFuncCode(js_func, addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214}
215
216
Iain Merrick9ac36c92010-09-13 15:29:50 +0100217// This C++ function is called as a constructor, to grab the frame pointer
218// from the calling function. When this function runs, the stack contains
219// a C_Entry frame and a Construct frame above the calling function's frame.
220static v8::Handle<Value> construct_call(const v8::Arguments& args) {
221 i::StackFrameIterator frame_iterator;
222 CHECK(frame_iterator.frame()->is_exit());
223 frame_iterator.Advance();
224 CHECK(frame_iterator.frame()->is_construct());
225 frame_iterator.Advance();
226 i::StackFrame* calling_frame = frame_iterator.frame();
227 CHECK(calling_frame->is_java_script());
Steve Blocka7e24c12009-10-30 11:49:00 +0000228
Iain Merrick9ac36c92010-09-13 15:29:50 +0100229#if defined(V8_HOST_ARCH_32_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100230 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100231 args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1));
232#elif defined(V8_HOST_ARCH_64_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100233 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp());
Ben Murdochf87a2032010-10-22 12:50:53 +0100234 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff);
235 int32_t high_bits = static_cast<int32_t>(fp >> 32);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100236 args.This()->Set(v8_str("low_bits"), v8_num(low_bits));
237 args.This()->Set(v8_str("high_bits"), v8_num(high_bits));
238#else
239#error Host architecture is neither 32-bit nor 64-bit.
240#endif
241 return args.This();
242}
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
Steve Blocka7e24c12009-10-30 11:49:00 +0000244
Iain Merrick9ac36c92010-09-13 15:29:50 +0100245// Use the API to create a JSFunction object that calls the above C++ function.
246void CreateFramePointerGrabberConstructor(const char* constructor_name) {
247 Local<v8::FunctionTemplate> constructor_template =
248 v8::FunctionTemplate::New(construct_call);
249 constructor_template->SetClassName(v8_str("FPGrabber"));
250 Local<Function> fun = constructor_template->GetFunction();
251 env->Global()->Set(v8_str(constructor_name), fun);
252}
Steve Blocka7e24c12009-10-30 11:49:00 +0000253
254
255// Creates a global function named 'func_name' that calls the tracing
256// function 'trace_func_name' with an actual EBP register value,
Iain Merrick9ac36c92010-09-13 15:29:50 +0100257// encoded as one or two Smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000258static void CreateTraceCallerFunction(const char* func_name,
259 const char* trace_func_name) {
260 i::EmbeddedVector<char, 256> trace_call_buf;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100261 i::OS::SNPrintF(trace_call_buf,
John Reck59135872010-11-02 12:39:01 -0700262 "function %s() {"
263 " fp = new FPGrabber();"
264 " %s(fp.low_bits, fp.high_bits);"
265 "}",
266 func_name, trace_func_name);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100267
268 // Create the FPGrabber function, which grabs the caller's frame pointer
269 // when called as a constructor.
270 CreateFramePointerGrabberConstructor("FPGrabber");
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
272 // Compile the script.
John Reck59135872010-11-02 12:39:01 -0700273 CompileRun(trace_call_buf.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000274}
275
276
Steve Block6ded16b2010-05-10 14:33:55 +0100277// This test verifies that stack tracing works when called during
278// execution of a native function called from JS code. In this case,
279// StackTracer uses Top::c_entry_fp as a starting point for stack
280// walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000281TEST(CFromJSStackTrace) {
282 TickSample sample;
283 InitTraceEnv(&sample);
284
285 InitializeVM();
286 v8::HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100287 // Create global function JSFuncDoTrace which calls
288 // extension function trace() with the current frame pointer value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 CreateTraceCallerFunction("JSFuncDoTrace", "trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100290 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 "function JSTrace() {"
292 " JSFuncDoTrace();"
293 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100294 "JSTrace();\n"
295 "true;");
296 CHECK(!result.IsEmpty());
297 // When stack tracer is invoked, the stack should look as follows:
298 // script [JS]
299 // JSTrace() [JS]
300 // JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100301 // trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100302 // DoTrace(EBP) [native]
303 // StackTracer::Trace
Ben Murdochb0fe1622011-05-05 13:52:32 +0100304
305 // The VM state tracking keeps track of external callbacks and puts
306 // them at the top of the sample stack.
307 int base = 0;
308 CHECK(sample.stack[0] == FUNCTION_ADDR(TraceExtension::Trace));
309 base++;
310
Steve Block6ded16b2010-05-10 14:33:55 +0100311 // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace"
Ben Murdochb0fe1622011-05-05 13:52:32 +0100312 CHECK_GT(sample.frames_count, base + 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100313 CHECK(IsAddressWithinFuncCode("JSFuncDoTrace", sample.stack[base + 0]));
314 CHECK(IsAddressWithinFuncCode("JSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000315}
316
317
Steve Block6ded16b2010-05-10 14:33:55 +0100318// This test verifies that stack tracing works when called during
319// execution of JS code. However, as calling StackTracer requires
320// entering native code, we can only emulate pure JS by erasing
321// Top::c_entry_fp value. In this case, StackTracer uses passed frame
322// pointer value as a starting point for stack walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000323TEST(PureJSStackTrace) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100324 // This test does not pass with inlining enabled since inlined functions
325 // don't appear in the stack trace.
326 i::FLAG_use_inlining = false;
327
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 TickSample sample;
329 InitTraceEnv(&sample);
330
331 InitializeVM();
332 v8::HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100333 // Create global function JSFuncDoTrace which calls
334 // extension function js_trace() with the current frame pointer value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 CreateTraceCallerFunction("JSFuncDoTrace", "js_trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100336 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 "function JSTrace() {"
338 " JSFuncDoTrace();"
339 "};\n"
340 "function OuterJSTrace() {"
341 " JSTrace();"
342 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100343 "OuterJSTrace();\n"
344 "true;");
345 CHECK(!result.IsEmpty());
346 // When stack tracer is invoked, the stack should look as follows:
347 // script [JS]
348 // OuterJSTrace() [JS]
349 // JSTrace() [JS]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100350 // JSFuncDoTrace() [JS]
351 // js_trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100352 // DoTraceHideCEntryFPAddress(EBP) [native]
353 // StackTracer::Trace
354 //
Ben Murdochb0fe1622011-05-05 13:52:32 +0100355
356 // The VM state tracking keeps track of external callbacks and puts
357 // them at the top of the sample stack.
358 int base = 0;
359 CHECK(sample.stack[0] == FUNCTION_ADDR(TraceExtension::JSTrace));
360 base++;
361
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
Ben Murdochb0fe1622011-05-05 13:52:32 +0100363 CHECK_GT(sample.frames_count, base + 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100364 CHECK(IsAddressWithinFuncCode("JSTrace", sample.stack[base + 0]));
365 CHECK(IsAddressWithinFuncCode("OuterJSTrace", sample.stack[base + 1]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000366}
367
368
Steve Blockd0582a62009-12-15 09:54:21 +0000369static void CFuncDoTrace(byte dummy_parameter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 Address fp;
371#ifdef __GNUC__
372 fp = reinterpret_cast<Address>(__builtin_frame_address(0));
Steve Blockd0582a62009-12-15 09:54:21 +0000373#elif defined _MSC_VER
374 // Approximate a frame pointer address. We compile without base pointers,
375 // so we can't trust ebp/rbp.
376 fp = &dummy_parameter - 2 * sizeof(void*); // NOLINT
377#else
378#error Unexpected platform.
Steve Blocka7e24c12009-10-30 11:49:00 +0000379#endif
380 DoTrace(fp);
381}
382
383
384static int CFunc(int depth) {
385 if (depth <= 0) {
Steve Blockd0582a62009-12-15 09:54:21 +0000386 CFuncDoTrace(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 return 0;
388 } else {
389 return CFunc(depth - 1) + 1;
390 }
391}
392
393
Steve Block6ded16b2010-05-10 14:33:55 +0100394// This test verifies that stack tracing doesn't crash when called on
395// pure native code. StackTracer only unrolls JS code, so we can't
396// get any meaningful info here.
Steve Blocka7e24c12009-10-30 11:49:00 +0000397TEST(PureCStackTrace) {
398 TickSample sample;
399 InitTraceEnv(&sample);
400 // Check that sampler doesn't crash
401 CHECK_EQ(10, CFunc(10));
402}
403
404
405TEST(JsEntrySp) {
406 InitializeVM();
407 v8::HandleScope scope;
408 CHECK_EQ(0, GetJsEntrySp());
409 CompileRun("a = 1; b = a + 1;");
410 CHECK_EQ(0, GetJsEntrySp());
411 CompileRun("js_entry_sp();");
412 CHECK_EQ(0, GetJsEntrySp());
413 CompileRun("js_entry_sp_level2();");
414 CHECK_EQ(0, GetJsEntrySp());
415}
416
417#endif // ENABLE_LOGGING_AND_PROFILING