Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 27 | // |
| 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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 42 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | |
| 44 | using v8::Function; |
| 45 | using v8::Local; |
| 46 | using v8::Object; |
| 47 | using v8::Script; |
| 48 | using v8::String; |
| 49 | using v8::Value; |
| 50 | |
| 51 | using v8::internal::byte; |
| 52 | using v8::internal::Address; |
| 53 | using v8::internal::Handle; |
| 54 | using v8::internal::JSFunction; |
| 55 | using v8::internal::StackTracer; |
| 56 | using v8::internal::TickSample; |
| 57 | using v8::internal::Top; |
| 58 | |
| 59 | namespace i = v8::internal; |
| 60 | |
| 61 | |
| 62 | static v8::Persistent<v8::Context> env; |
| 63 | |
| 64 | |
| 65 | static struct { |
| 66 | TickSample* sample; |
| 67 | } trace_env = { NULL }; |
| 68 | |
| 69 | |
| 70 | static void InitTraceEnv(TickSample* sample) { |
| 71 | trace_env.sample = sample; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static void DoTrace(Address fp) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 76 | trace_env.sample->fp = fp; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | // sp is only used to define stack high bound |
| 78 | trace_env.sample->sp = |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 79 | reinterpret_cast<Address>(trace_env.sample) - 10240; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | 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 |
| 86 | static 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | // --- T r a c e E x t e n s i o n --- |
| 96 | |
| 97 | class 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 | |
| 112 | const 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 | |
| 118 | v8::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 | |
| 135 | Address TraceExtension::GetFP(const v8::Arguments& args) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 136 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | Address fp = *reinterpret_cast<Address*>(*args[0]); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 140 | #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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | printf("Trace: %p\n", fp); |
| 148 | return fp; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) { |
| 153 | DoTrace(GetFP(args)); |
| 154 | return v8::Undefined(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) { |
| 159 | DoTraceHideCEntryFPAddress(GetFP(args)); |
| 160 | return v8::Undefined(); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static Address GetJsEntrySp() { |
| 165 | CHECK_NE(NULL, Top::GetCurrentThread()); |
| 166 | return Top::js_entry_sp(Top::GetCurrentThread()); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | v8::Handle<v8::Value> TraceExtension::JSEntrySP(const v8::Arguments& args) { |
| 171 | CHECK_NE(0, GetJsEntrySp()); |
| 172 | return v8::Undefined(); |
| 173 | } |
| 174 | |
| 175 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | v8::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 | |
| 187 | static TraceExtension kTraceExtension; |
| 188 | v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension); |
| 189 | |
| 190 | |
| 191 | static 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 203 | static void CheckJSFunctionAtAddress(const char* func_name, Address addr) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 204 | CHECK(i::Heap::Contains(addr)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 205 | i::Object* obj = i::HeapObject::FromAddress(addr); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 206 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 216 | // 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. |
| 219 | static 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 228 | #if defined(V8_HOST_ARCH_32_BIT) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 229 | int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp()); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 230 | args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1)); |
| 231 | #elif defined(V8_HOST_ARCH_64_BIT) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 232 | uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 233 | int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff); |
| 234 | int32_t high_bits = static_cast<int32_t>(fp >> 32); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 235 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 244 | // Use the API to create a JSFunction object that calls the above C++ function. |
| 245 | void 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | |
| 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 Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 256 | // encoded as one or two Smis. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | static void CreateTraceCallerFunction(const char* func_name, |
| 258 | const char* trace_func_name) { |
| 259 | i::EmbeddedVector<char, 256> trace_call_buf; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 260 | i::OS::SNPrintF(trace_call_buf, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 261 | "function %s() {" |
| 262 | " fp = new FPGrabber();" |
| 263 | " %s(fp.low_bits, fp.high_bits);" |
| 264 | "}", |
| 265 | func_name, trace_func_name); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 266 | |
| 267 | // Create the FPGrabber function, which grabs the caller's frame pointer |
| 268 | // when called as a constructor. |
| 269 | CreateFramePointerGrabberConstructor("FPGrabber"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | |
| 271 | // Compile the script. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 272 | CompileRun(trace_call_buf.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 276 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | TEST(CFromJSStackTrace) { |
| 281 | TickSample sample; |
| 282 | InitTraceEnv(&sample); |
| 283 | |
| 284 | InitializeVM(); |
| 285 | v8::HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 286 | // Create global function JSFuncDoTrace which calls |
| 287 | // extension function trace() with the current frame pointer value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | CreateTraceCallerFunction("JSFuncDoTrace", "trace"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 289 | Local<Value> result = CompileRun( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | "function JSTrace() {" |
| 291 | " JSFuncDoTrace();" |
| 292 | "};\n" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 293 | "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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 300 | // trace(EBP) [native (extension)] |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 301 | // DoTrace(EBP) [native] |
| 302 | // StackTracer::Trace |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 303 | |
| 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 310 | // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 311 | CHECK_GT(sample.frames_count, base + 1); |
| 312 | CheckJSFunctionAtAddress("JSFuncDoTrace", sample.stack[base + 0]); |
| 313 | CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 317 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | TEST(PureJSStackTrace) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 323 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 327 | TickSample sample; |
| 328 | InitTraceEnv(&sample); |
| 329 | |
| 330 | InitializeVM(); |
| 331 | v8::HandleScope scope; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 332 | // Create global function JSFuncDoTrace which calls |
| 333 | // extension function js_trace() with the current frame pointer value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | CreateTraceCallerFunction("JSFuncDoTrace", "js_trace"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 335 | Local<Value> result = CompileRun( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 336 | "function JSTrace() {" |
| 337 | " JSFuncDoTrace();" |
| 338 | "};\n" |
| 339 | "function OuterJSTrace() {" |
| 340 | " JSTrace();" |
| 341 | "};\n" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 342 | "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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 349 | // JSFuncDoTrace() [JS] |
| 350 | // js_trace(EBP) [native (extension)] |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 351 | // 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 356 | CheckJSFunctionAtAddress("JSFuncDoTrace", sample.function); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 357 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 365 | CHECK_GT(sample.frames_count, base + 1); |
| 366 | CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 0]); |
| 367 | CheckJSFunctionAtAddress("OuterJSTrace", sample.stack[base + 1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 371 | static void CFuncDoTrace(byte dummy_parameter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | Address fp; |
| 373 | #ifdef __GNUC__ |
| 374 | fp = reinterpret_cast<Address>(__builtin_frame_address(0)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 375 | #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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 381 | #endif |
| 382 | DoTrace(fp); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | static int CFunc(int depth) { |
| 387 | if (depth <= 0) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 388 | CFuncDoTrace(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | return 0; |
| 390 | } else { |
| 391 | return CFunc(depth - 1) + 1; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 396 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | TEST(PureCStackTrace) { |
| 400 | TickSample sample; |
| 401 | InitTraceEnv(&sample); |
| 402 | // Check that sampler doesn't crash |
| 403 | CHECK_EQ(10, CFunc(10)); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | TEST(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 |