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