blob: c85f6c0bc3af97bbd1779db2cb6aeb7024963801 [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
36#include "codegen.h"
37#include "log.h"
38#include "top.h"
39#include "cctest.h"
40#include "disassembler.h"
41#include "register-allocator-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042#include "vm-state-inl.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;
54using v8::internal::JSFunction;
55using v8::internal::StackTracer;
56using v8::internal::TickSample;
57using v8::internal::Top;
58
59namespace i = v8::internal;
60
61
62static v8::Persistent<v8::Context> env;
63
64
65static struct {
66 TickSample* sample;
67} trace_env = { NULL };
68
69
70static void InitTraceEnv(TickSample* sample) {
71 trace_env.sample = sample;
72}
73
74
75static void DoTrace(Address fp) {
Leon Clarked91b9f72010-01-27 17:25:45 +000076 trace_env.sample->fp = fp;
Steve Blocka7e24c12009-10-30 11:49:00 +000077 // sp is only used to define stack high bound
78 trace_env.sample->sp =
Leon Clarked91b9f72010-01-27 17:25:45 +000079 reinterpret_cast<Address>(trace_env.sample) - 10240;
Steve Blocka7e24c12009-10-30 11:49:00 +000080 StackTracer::Trace(trace_env.sample);
81}
82
83
84// Hide c_entry_fp to emulate situation when sampling is done while
85// pure JS code is being executed
86static void DoTraceHideCEntryFPAddress(Address fp) {
87 v8::internal::Address saved_c_frame_fp = *(Top::c_entry_fp_address());
88 CHECK(saved_c_frame_fp);
89 *(Top::c_entry_fp_address()) = 0;
90 DoTrace(fp);
91 *(Top::c_entry_fp_address()) = saved_c_frame_fp;
92}
93
94
Steve Blocka7e24c12009-10-30 11:49:00 +000095// --- T r a c e E x t e n s i o n ---
96
97class TraceExtension : public v8::Extension {
98 public:
99 TraceExtension() : v8::Extension("v8/trace", kSource) { }
100 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
101 v8::Handle<String> name);
102 static v8::Handle<v8::Value> Trace(const v8::Arguments& args);
103 static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args);
104 static v8::Handle<v8::Value> JSEntrySP(const v8::Arguments& args);
105 static v8::Handle<v8::Value> JSEntrySPLevel2(const v8::Arguments& args);
106 private:
107 static Address GetFP(const v8::Arguments& args);
108 static const char* kSource;
109};
110
111
112const char* TraceExtension::kSource =
113 "native function trace();"
114 "native function js_trace();"
115 "native function js_entry_sp();"
116 "native function js_entry_sp_level2();";
117
118v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction(
119 v8::Handle<String> name) {
120 if (name->Equals(String::New("trace"))) {
121 return v8::FunctionTemplate::New(TraceExtension::Trace);
122 } else if (name->Equals(String::New("js_trace"))) {
123 return v8::FunctionTemplate::New(TraceExtension::JSTrace);
124 } else if (name->Equals(String::New("js_entry_sp"))) {
125 return v8::FunctionTemplate::New(TraceExtension::JSEntrySP);
126 } else if (name->Equals(String::New("js_entry_sp_level2"))) {
127 return v8::FunctionTemplate::New(TraceExtension::JSEntrySPLevel2);
128 } else {
129 CHECK(false);
130 return v8::Handle<v8::FunctionTemplate>();
131 }
132}
133
134
135Address TraceExtension::GetFP(const v8::Arguments& args) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100136 // Convert frame pointer from encoding as smis in the arguments to a pointer.
137 CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform.
138#if defined(V8_HOST_ARCH_32_BIT)
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 Address fp = *reinterpret_cast<Address*>(*args[0]);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100140#elif defined(V8_HOST_ARCH_64_BIT)
141 int64_t low_bits = *reinterpret_cast<uint64_t*>(*args[0]) >> 32;
142 int64_t high_bits = *reinterpret_cast<uint64_t*>(*args[1]);
143 Address fp = reinterpret_cast<Address>(high_bits | low_bits);
144#else
145#error Host architecture is neither 32-bit nor 64-bit.
146#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 printf("Trace: %p\n", fp);
148 return fp;
149}
150
151
152v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) {
153 DoTrace(GetFP(args));
154 return v8::Undefined();
155}
156
157
158v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) {
159 DoTraceHideCEntryFPAddress(GetFP(args));
160 return v8::Undefined();
161}
162
163
164static Address GetJsEntrySp() {
165 CHECK_NE(NULL, Top::GetCurrentThread());
166 return Top::js_entry_sp(Top::GetCurrentThread());
167}
168
169
170v8::Handle<v8::Value> TraceExtension::JSEntrySP(const v8::Arguments& args) {
171 CHECK_NE(0, GetJsEntrySp());
172 return v8::Undefined();
173}
174
175
Steve Blocka7e24c12009-10-30 11:49:00 +0000176v8::Handle<v8::Value> TraceExtension::JSEntrySPLevel2(
177 const v8::Arguments& args) {
178 v8::HandleScope scope;
179 const Address js_entry_sp = GetJsEntrySp();
180 CHECK_NE(0, js_entry_sp);
181 CompileRun("js_entry_sp();");
182 CHECK_EQ(js_entry_sp, GetJsEntrySp());
183 return v8::Undefined();
184}
185
186
187static TraceExtension kTraceExtension;
188v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension);
189
190
191static void InitializeVM() {
192 if (env.IsEmpty()) {
193 v8::HandleScope scope;
194 const char* extensions[] = { "v8/trace" };
195 v8::ExtensionConfiguration config(1, extensions);
196 env = v8::Context::New(&config);
197 }
198 v8::HandleScope scope;
199 env->Enter();
200}
201
202
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100203static void CheckJSFunctionAtAddress(const char* func_name, Address addr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100204 CHECK(i::Heap::Contains(addr));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100205 i::Object* obj = i::HeapObject::FromAddress(addr);
Leon Clarkef7060e22010-06-03 12:02:55 +0100206 CHECK(obj->IsJSFunction());
207 CHECK(JSFunction::cast(obj)->shared()->name()->IsString());
208 i::SmartPointer<char> found_name =
209 i::String::cast(
210 JSFunction::cast(
211 obj)->shared()->name())->ToCString();
212 CHECK_EQ(func_name, *found_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000213}
214
215
Iain Merrick9ac36c92010-09-13 15:29:50 +0100216// This C++ function is called as a constructor, to grab the frame pointer
217// from the calling function. When this function runs, the stack contains
218// a C_Entry frame and a Construct frame above the calling function's frame.
219static v8::Handle<Value> construct_call(const v8::Arguments& args) {
220 i::StackFrameIterator frame_iterator;
221 CHECK(frame_iterator.frame()->is_exit());
222 frame_iterator.Advance();
223 CHECK(frame_iterator.frame()->is_construct());
224 frame_iterator.Advance();
225 i::StackFrame* calling_frame = frame_iterator.frame();
226 CHECK(calling_frame->is_java_script());
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
Iain Merrick9ac36c92010-09-13 15:29:50 +0100228#if defined(V8_HOST_ARCH_32_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100229 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100230 args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1));
231#elif defined(V8_HOST_ARCH_64_BIT)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100232 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp());
Ben Murdochf87a2032010-10-22 12:50:53 +0100233 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff);
234 int32_t high_bits = static_cast<int32_t>(fp >> 32);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100235 args.This()->Set(v8_str("low_bits"), v8_num(low_bits));
236 args.This()->Set(v8_str("high_bits"), v8_num(high_bits));
237#else
238#error Host architecture is neither 32-bit nor 64-bit.
239#endif
240 return args.This();
241}
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
Iain Merrick9ac36c92010-09-13 15:29:50 +0100244// Use the API to create a JSFunction object that calls the above C++ function.
245void CreateFramePointerGrabberConstructor(const char* constructor_name) {
246 Local<v8::FunctionTemplate> constructor_template =
247 v8::FunctionTemplate::New(construct_call);
248 constructor_template->SetClassName(v8_str("FPGrabber"));
249 Local<Function> fun = constructor_template->GetFunction();
250 env->Global()->Set(v8_str(constructor_name), fun);
251}
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253
254// Creates a global function named 'func_name' that calls the tracing
255// function 'trace_func_name' with an actual EBP register value,
Iain Merrick9ac36c92010-09-13 15:29:50 +0100256// encoded as one or two Smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000257static void CreateTraceCallerFunction(const char* func_name,
258 const char* trace_func_name) {
259 i::EmbeddedVector<char, 256> trace_call_buf;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100260 i::OS::SNPrintF(trace_call_buf,
John Reck59135872010-11-02 12:39:01 -0700261 "function %s() {"
262 " fp = new FPGrabber();"
263 " %s(fp.low_bits, fp.high_bits);"
264 "}",
265 func_name, trace_func_name);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100266
267 // Create the FPGrabber function, which grabs the caller's frame pointer
268 // when called as a constructor.
269 CreateFramePointerGrabberConstructor("FPGrabber");
Steve Blocka7e24c12009-10-30 11:49:00 +0000270
271 // Compile the script.
John Reck59135872010-11-02 12:39:01 -0700272 CompileRun(trace_call_buf.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000273}
274
275
Steve Block6ded16b2010-05-10 14:33:55 +0100276// This test verifies that stack tracing works when called during
277// execution of a native function called from JS code. In this case,
278// StackTracer uses Top::c_entry_fp as a starting point for stack
279// walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000280TEST(CFromJSStackTrace) {
281 TickSample sample;
282 InitTraceEnv(&sample);
283
284 InitializeVM();
285 v8::HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100286 // Create global function JSFuncDoTrace which calls
287 // extension function trace() with the current frame pointer value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 CreateTraceCallerFunction("JSFuncDoTrace", "trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100289 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 "function JSTrace() {"
291 " JSFuncDoTrace();"
292 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100293 "JSTrace();\n"
294 "true;");
295 CHECK(!result.IsEmpty());
296 // When stack tracer is invoked, the stack should look as follows:
297 // script [JS]
298 // JSTrace() [JS]
299 // JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100300 // trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100301 // DoTrace(EBP) [native]
302 // StackTracer::Trace
Ben Murdochb0fe1622011-05-05 13:52:32 +0100303
304 // The VM state tracking keeps track of external callbacks and puts
305 // them at the top of the sample stack.
306 int base = 0;
307 CHECK(sample.stack[0] == FUNCTION_ADDR(TraceExtension::Trace));
308 base++;
309
Steve Block6ded16b2010-05-10 14:33:55 +0100310 // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace"
Ben Murdochb0fe1622011-05-05 13:52:32 +0100311 CHECK_GT(sample.frames_count, base + 1);
312 CheckJSFunctionAtAddress("JSFuncDoTrace", sample.stack[base + 0]);
313 CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 1]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000314}
315
316
Steve Block6ded16b2010-05-10 14:33:55 +0100317// This test verifies that stack tracing works when called during
318// execution of JS code. However, as calling StackTracer requires
319// entering native code, we can only emulate pure JS by erasing
320// Top::c_entry_fp value. In this case, StackTracer uses passed frame
321// pointer value as a starting point for stack walking.
Steve Blocka7e24c12009-10-30 11:49:00 +0000322TEST(PureJSStackTrace) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100323 // This test does not pass with inlining enabled since inlined functions
324 // don't appear in the stack trace.
325 i::FLAG_use_inlining = false;
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 TickSample sample;
328 InitTraceEnv(&sample);
329
330 InitializeVM();
331 v8::HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +0100332 // Create global function JSFuncDoTrace which calls
333 // extension function js_trace() with the current frame pointer value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 CreateTraceCallerFunction("JSFuncDoTrace", "js_trace");
Steve Block6ded16b2010-05-10 14:33:55 +0100335 Local<Value> result = CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 "function JSTrace() {"
337 " JSFuncDoTrace();"
338 "};\n"
339 "function OuterJSTrace() {"
340 " JSTrace();"
341 "};\n"
Steve Block6ded16b2010-05-10 14:33:55 +0100342 "OuterJSTrace();\n"
343 "true;");
344 CHECK(!result.IsEmpty());
345 // When stack tracer is invoked, the stack should look as follows:
346 // script [JS]
347 // OuterJSTrace() [JS]
348 // JSTrace() [JS]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100349 // JSFuncDoTrace() [JS]
350 // js_trace(EBP) [native (extension)]
Steve Block6ded16b2010-05-10 14:33:55 +0100351 // DoTraceHideCEntryFPAddress(EBP) [native]
352 // StackTracer::Trace
353 //
354 // The last JS function called. It is only visible through
355 // sample.function, as its return address is above captured EBP value.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100356 CheckJSFunctionAtAddress("JSFuncDoTrace", sample.function);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100357
358 // The VM state tracking keeps track of external callbacks and puts
359 // them at the top of the sample stack.
360 int base = 0;
361 CHECK(sample.stack[0] == FUNCTION_ADDR(TraceExtension::JSTrace));
362 base++;
363
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace"
Ben Murdochb0fe1622011-05-05 13:52:32 +0100365 CHECK_GT(sample.frames_count, base + 1);
366 CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 0]);
367 CheckJSFunctionAtAddress("OuterJSTrace", sample.stack[base + 1]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368}
369
370
Steve Blockd0582a62009-12-15 09:54:21 +0000371static void CFuncDoTrace(byte dummy_parameter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 Address fp;
373#ifdef __GNUC__
374 fp = reinterpret_cast<Address>(__builtin_frame_address(0));
Steve Blockd0582a62009-12-15 09:54:21 +0000375#elif defined _MSC_VER
376 // Approximate a frame pointer address. We compile without base pointers,
377 // so we can't trust ebp/rbp.
378 fp = &dummy_parameter - 2 * sizeof(void*); // NOLINT
379#else
380#error Unexpected platform.
Steve Blocka7e24c12009-10-30 11:49:00 +0000381#endif
382 DoTrace(fp);
383}
384
385
386static int CFunc(int depth) {
387 if (depth <= 0) {
Steve Blockd0582a62009-12-15 09:54:21 +0000388 CFuncDoTrace(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 return 0;
390 } else {
391 return CFunc(depth - 1) + 1;
392 }
393}
394
395
Steve Block6ded16b2010-05-10 14:33:55 +0100396// This test verifies that stack tracing doesn't crash when called on
397// pure native code. StackTracer only unrolls JS code, so we can't
398// get any meaningful info here.
Steve Blocka7e24c12009-10-30 11:49:00 +0000399TEST(PureCStackTrace) {
400 TickSample sample;
401 InitTraceEnv(&sample);
402 // Check that sampler doesn't crash
403 CHECK_EQ(10, CFunc(10));
404}
405
406
407TEST(JsEntrySp) {
408 InitializeVM();
409 v8::HandleScope scope;
410 CHECK_EQ(0, GetJsEntrySp());
411 CompileRun("a = 1; b = a + 1;");
412 CHECK_EQ(0, GetJsEntrySp());
413 CompileRun("js_entry_sp();");
414 CHECK_EQ(0, GetJsEntrySp());
415 CompileRun("js_entry_sp_level2();");
416 CHECK_EQ(0, GetJsEntrySp());
417}
418
419#endif // ENABLE_LOGGING_AND_PROFILING