kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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. |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +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 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 36 | #include "codegen.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 37 | #include "log.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 38 | #include "top.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 39 | #include "cctest.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 40 | #include "disassembler.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 41 | #include "register-allocator-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | #include "vm-state-inl.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +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; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 52 | using v8::internal::Address; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 53 | using v8::internal::Handle; |
| 54 | using v8::internal::JSFunction; |
| 55 | using v8::internal::StackTracer; |
| 56 | using v8::internal::TickSample; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 57 | using v8::internal::Top; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 58 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 59 | namespace i = v8::internal; |
| 60 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 61 | |
| 62 | static v8::Persistent<v8::Context> env; |
| 63 | |
| 64 | |
| 65 | static struct { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 66 | TickSample* sample; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 67 | } trace_env = { NULL }; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 68 | |
| 69 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 70 | static void InitTraceEnv(TickSample* sample) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 71 | trace_env.sample = sample; |
| 72 | } |
| 73 | |
| 74 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 75 | static void DoTrace(Address fp) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 76 | trace_env.sample->fp = fp; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 77 | // sp is only used to define stack high bound |
| 78 | trace_env.sample->sp = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 79 | reinterpret_cast<Address>(trace_env.sample) - 10240; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 80 | StackTracer::Trace(trace_env.sample); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 84 | // Hide c_entry_fp to emulate situation when sampling is done while |
| 85 | // pure JS code is being executed |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 86 | static void DoTraceHideCEntryFPAddress(Address fp) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 90 | DoTrace(fp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 91 | *(Top::c_entry_fp_address()) = saved_c_frame_fp; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +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( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 101 | v8::Handle<String> name); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 102 | static v8::Handle<v8::Value> Trace(const v8::Arguments& args); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 103 | static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 104 | static v8::Handle<v8::Value> JSEntrySP(const v8::Arguments& args); |
| 105 | static v8::Handle<v8::Value> JSEntrySPLevel2(const v8::Arguments& args); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 106 | private: |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 107 | static Address GetFP(const v8::Arguments& args); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 108 | static const char* kSource; |
| 109 | }; |
| 110 | |
| 111 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 112 | const char* TraceExtension::kSource = |
| 113 | "native function trace();" |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 114 | "native function js_trace();" |
| 115 | "native function js_entry_sp();" |
| 116 | "native function js_entry_sp_level2();"; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 117 | |
| 118 | v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 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); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 128 | } else { |
| 129 | CHECK(false); |
| 130 | return v8::Handle<v8::FunctionTemplate>(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 135 | Address TraceExtension::GetFP(const v8::Arguments& args) { |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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) |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 139 | Address fp = *reinterpret_cast<Address*>(*args[0]); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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 |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 147 | printf("Trace: %p\n", fp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 148 | return fp; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
| 152 | v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 160 | return v8::Undefined(); |
| 161 | } |
| 162 | |
| 163 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +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 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 187 | static TraceExtension kTraceExtension; |
| 188 | v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension); |
| 189 | |
| 190 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 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 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 203 | static void CheckJSFunctionAtAddress(const char* func_name, Address addr) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 204 | CHECK(i::Heap::Contains(addr)); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 205 | i::Object* obj = i::HeapObject::FromAddress(addr); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [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); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 227 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 228 | #if defined(V8_HOST_ARCH_32_BIT) |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 229 | int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp()); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 230 | args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1)); |
| 231 | #elif defined(V8_HOST_ARCH_64_BIT) |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 232 | uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 233 | int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff); |
| 234 | int32_t high_bits = static_cast<int32_t>(fp >> 32); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 242 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 243 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [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 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +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, |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 256 | // encoded as one or two Smis. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +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; |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 260 | i::OS::SNPrintF(trace_call_buf, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 261 | "function %s() {" |
| 262 | " fp = new FPGrabber();" |
| 263 | " %s(fp.low_bits, fp.high_bits);" |
| 264 | "}", |
| 265 | func_name, trace_func_name); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 266 | |
| 267 | // Create the FPGrabber function, which grabs the caller's frame pointer |
| 268 | // when called as a constructor. |
| 269 | CreateFramePointerGrabberConstructor("FPGrabber"); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 270 | |
| 271 | // Compile the script. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 272 | CompileRun(trace_call_buf.start()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [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. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 280 | TEST(CFromJSStackTrace) { |
| 281 | TickSample sample; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 282 | InitTraceEnv(&sample); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 283 | |
| 284 | InitializeVM(); |
| 285 | v8::HandleScope scope; |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 286 | // Create global function JSFuncDoTrace which calls |
| 287 | // extension function trace() with the current frame pointer value. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 288 | CreateTraceCallerFunction("JSFuncDoTrace", "trace"); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 289 | Local<Value> result = CompileRun( |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 290 | "function JSTrace() {" |
| 291 | " JSFuncDoTrace();" |
| 292 | "};\n" |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 293 | "JSTrace();\n" |
| 294 | "true;"); |
| 295 | CHECK(!result.IsEmpty()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 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] |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 300 | // trace(EBP) [native (extension)] |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 301 | // DoTrace(EBP) [native] |
| 302 | // StackTracer::Trace |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [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 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 310 | // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 311 | CHECK_GT(sample.frames_count, base + 1); |
| 312 | CheckJSFunctionAtAddress("JSFuncDoTrace", sample.stack[base + 0]); |
| 313 | CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 1]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [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. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 322 | TEST(PureJSStackTrace) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [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 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 327 | TickSample sample; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 328 | InitTraceEnv(&sample); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 329 | |
| 330 | InitializeVM(); |
| 331 | v8::HandleScope scope; |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 332 | // Create global function JSFuncDoTrace which calls |
| 333 | // extension function js_trace() with the current frame pointer value. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 334 | CreateTraceCallerFunction("JSFuncDoTrace", "js_trace"); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 335 | Local<Value> result = CompileRun( |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 336 | "function JSTrace() {" |
| 337 | " JSFuncDoTrace();" |
| 338 | "};\n" |
| 339 | "function OuterJSTrace() {" |
| 340 | " JSTrace();" |
| 341 | "};\n" |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 342 | "OuterJSTrace();\n" |
| 343 | "true;"); |
| 344 | CHECK(!result.IsEmpty()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 345 | // When stack tracer is invoked, the stack should look as follows: |
| 346 | // script [JS] |
| 347 | // OuterJSTrace() [JS] |
| 348 | // JSTrace() [JS] |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 349 | // JSFuncDoTrace() [JS] |
| 350 | // js_trace(EBP) [native (extension)] |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [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. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 356 | CheckJSFunctionAtAddress("JSFuncDoTrace", sample.function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [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 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 364 | // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 365 | CHECK_GT(sample.frames_count, base + 1); |
| 366 | CheckJSFunctionAtAddress("JSTrace", sample.stack[base + 0]); |
| 367 | CheckJSFunctionAtAddress("OuterJSTrace", sample.stack[base + 1]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 371 | static void CFuncDoTrace(byte dummy_parameter) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 372 | Address fp; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 373 | #ifdef __GNUC__ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 374 | fp = reinterpret_cast<Address>(__builtin_frame_address(0)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +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. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 381 | #endif |
| 382 | DoTrace(fp); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | static int CFunc(int depth) { |
| 387 | if (depth <= 0) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 388 | CFuncDoTrace(0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 389 | return 0; |
| 390 | } else { |
| 391 | return CFunc(depth - 1) + 1; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [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. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 399 | TEST(PureCStackTrace) { |
| 400 | TickSample sample; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 401 | InitTraceEnv(&sample); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 402 | // Check that sampler doesn't crash |
| 403 | CHECK_EQ(10, CFunc(10)); |
| 404 | } |
| 405 | |
| 406 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 419 | #endif // ENABLE_LOGGING_AND_PROFILING |