Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #include "src/api.h" |
| 35 | #include "src/codegen.h" |
| 36 | #include "src/disassembler.h" |
| 37 | #include "src/isolate.h" |
| 38 | #include "src/log.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 39 | #include "src/profiler/sampler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | #include "src/vm-state-inl.h" |
| 41 | #include "test/cctest/cctest.h" |
| 42 | #include "test/cctest/trace-extension.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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 54 | using v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | using v8::internal::JSFunction; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | using v8::internal::TickSample; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 59 | static bool IsAddressWithinFuncCode(JSFunction* function, Address addr) { |
| 60 | i::Code* code = function->code(); |
| 61 | return code->contains(addr); |
| 62 | } |
| 63 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | |
| 65 | static bool IsAddressWithinFuncCode(v8::Local<v8::Context> context, |
| 66 | const char* func_name, |
| 67 | Address addr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 68 | v8::Local<v8::Value> func = |
| 69 | context->Global()->Get(context, v8_str(func_name)).ToLocalChecked(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 70 | CHECK(func->IsFunction()); |
| 71 | JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func)); |
| 72 | return IsAddressWithinFuncCode(js_func, addr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 76 | // This C++ function is called as a constructor, to grab the frame pointer |
| 77 | // from the calling function. When this function runs, the stack contains |
| 78 | // a C_Entry frame and a Construct frame above the calling function's frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | static void construct_call(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 80 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
| 81 | i::StackFrameIterator frame_iterator(isolate); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 82 | CHECK(frame_iterator.frame()->is_exit()); |
| 83 | frame_iterator.Advance(); |
| 84 | CHECK(frame_iterator.frame()->is_construct()); |
| 85 | frame_iterator.Advance(); |
| 86 | i::StackFrame* calling_frame = frame_iterator.frame(); |
| 87 | CHECK(calling_frame->is_java_script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 89 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 90 | #if defined(V8_HOST_ARCH_32_BIT) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 91 | int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 92 | args.This() |
| 93 | ->Set(context, v8_str("low_bits"), v8_num(low_bits >> 1)) |
| 94 | .FromJust(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 95 | #elif defined(V8_HOST_ARCH_64_BIT) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 96 | uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 97 | int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff); |
| 98 | int32_t high_bits = static_cast<int32_t>(fp >> 32); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 99 | args.This()->Set(context, v8_str("low_bits"), v8_num(low_bits)).FromJust(); |
| 100 | args.This()->Set(context, v8_str("high_bits"), v8_num(high_bits)).FromJust(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 101 | #else |
| 102 | #error Host architecture is neither 32-bit nor 64-bit. |
| 103 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | args.GetReturnValue().Set(args.This()); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 105 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 108 | // Use the API to create a JSFunction object that calls the above C++ function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | void CreateFramePointerGrabberConstructor(v8::Local<v8::Context> context, |
| 110 | const char* constructor_name) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 111 | Local<v8::FunctionTemplate> constructor_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | v8::FunctionTemplate::New(context->GetIsolate(), construct_call); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 113 | constructor_template->SetClassName(v8_str("FPGrabber")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 114 | Local<Function> fun = |
| 115 | constructor_template->GetFunction(context).ToLocalChecked(); |
| 116 | context->Global()->Set(context, v8_str(constructor_name), fun).FromJust(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 117 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | // Creates a global function named 'func_name' that calls the tracing |
| 121 | // function 'trace_func_name' with an actual EBP register value, |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 122 | // encoded as one or two Smis. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | static void CreateTraceCallerFunction(v8::Local<v8::Context> context, |
| 124 | const char* func_name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | const char* trace_func_name) { |
| 126 | i::EmbeddedVector<char, 256> trace_call_buf; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 127 | i::SNPrintF(trace_call_buf, |
| 128 | "function %s() {" |
| 129 | " fp = new FPGrabber();" |
| 130 | " %s(fp.low_bits, fp.high_bits);" |
| 131 | "}", |
| 132 | func_name, trace_func_name); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 133 | |
| 134 | // Create the FPGrabber function, which grabs the caller's frame pointer |
| 135 | // when called as a constructor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | CreateFramePointerGrabberConstructor(context, "FPGrabber"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | |
| 138 | // Compile the script. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 139 | CompileRun(trace_call_buf.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 143 | // This test verifies that stack tracing works when called during |
| 144 | // execution of a native function called from JS code. In this case, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | // TickSample::Trace uses Isolate::c_entry_fp as a starting point for stack |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 146 | // walking. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | TEST(CFromJSStackTrace) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 148 | // BUG(1303) Inlining of JSFuncDoTrace() in JSTrace below breaks this test. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 149 | i::FLAG_turbo_inlining = false; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 150 | i::FLAG_use_inlining = false; |
| 151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | TickSample sample; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | i::TraceExtension::InitTraceEnv(&sample); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 155 | v8::HandleScope scope(CcTest::isolate()); |
| 156 | v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION); |
| 157 | v8::Context::Scope context_scope(context); |
| 158 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 159 | // Create global function JSFuncDoTrace which calls |
| 160 | // extension function trace() with the current frame pointer value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | CreateTraceCallerFunction(context, "JSFuncDoTrace", "trace"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 162 | Local<Value> result = CompileRun( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | "function JSTrace() {" |
| 164 | " JSFuncDoTrace();" |
| 165 | "};\n" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 166 | "JSTrace();\n" |
| 167 | "true;"); |
| 168 | CHECK(!result.IsEmpty()); |
| 169 | // When stack tracer is invoked, the stack should look as follows: |
| 170 | // script [JS] |
| 171 | // JSTrace() [JS] |
| 172 | // JSFuncDoTrace() [JS] [captures EBP value and encodes it as Smi] |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 173 | // trace(EBP) [native (extension)] |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 174 | // DoTrace(EBP) [native] |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | // TickSample::Trace |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 176 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 177 | CHECK(sample.has_external_callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 178 | CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::Trace), sample.external_callback); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 179 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 180 | // Stack tracing will start from the first JS function, i.e. "JSFuncDoTrace" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | unsigned base = 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 182 | CHECK_GT(sample.frames_count, base + 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 183 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | CHECK(IsAddressWithinFuncCode( |
| 185 | context, "JSFuncDoTrace", sample.stack[base + 0])); |
| 186 | CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 1])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 190 | // This test verifies that stack tracing works when called during |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | // execution of JS code. However, as calling TickSample::Trace requires |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 192 | // entering native code, we can only emulate pure JS by erasing |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | // Isolate::c_entry_fp value. In this case, TickSample::Trace uses passed frame |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 194 | // pointer value as a starting point for stack walking. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | TEST(PureJSStackTrace) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 196 | // This test does not pass with inlining enabled since inlined functions |
| 197 | // don't appear in the stack trace. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 198 | i::FLAG_turbo_inlining = false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 199 | i::FLAG_use_inlining = false; |
| 200 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | TickSample sample; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 202 | i::TraceExtension::InitTraceEnv(&sample); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 204 | v8::HandleScope scope(CcTest::isolate()); |
| 205 | v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION); |
| 206 | v8::Context::Scope context_scope(context); |
| 207 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 208 | // Create global function JSFuncDoTrace which calls |
| 209 | // extension function js_trace() with the current frame pointer value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | CreateTraceCallerFunction(context, "JSFuncDoTrace", "js_trace"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 211 | Local<Value> result = CompileRun( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | "function JSTrace() {" |
| 213 | " JSFuncDoTrace();" |
| 214 | "};\n" |
| 215 | "function OuterJSTrace() {" |
| 216 | " JSTrace();" |
| 217 | "};\n" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 218 | "OuterJSTrace();\n" |
| 219 | "true;"); |
| 220 | CHECK(!result.IsEmpty()); |
| 221 | // When stack tracer is invoked, the stack should look as follows: |
| 222 | // script [JS] |
| 223 | // OuterJSTrace() [JS] |
| 224 | // JSTrace() [JS] |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 225 | // JSFuncDoTrace() [JS] |
| 226 | // js_trace(EBP) [native (extension)] |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 227 | // DoTraceHideCEntryFPAddress(EBP) [native] |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 228 | // TickSample::Trace |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 229 | // |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 230 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 231 | CHECK(sample.has_external_callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 232 | CHECK_EQ(FUNCTION_ADDR(i::TraceExtension::JSTrace), sample.external_callback); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 233 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | unsigned base = 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 236 | CHECK_GT(sample.frames_count, base + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 237 | CHECK(IsAddressWithinFuncCode(context, "JSTrace", sample.stack[base + 0])); |
| 238 | CHECK(IsAddressWithinFuncCode( |
| 239 | context, "OuterJSTrace", sample.stack[base + 1])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 243 | static void CFuncDoTrace(byte dummy_parameter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | Address fp; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 245 | #if V8_HAS_BUILTIN_FRAME_ADDRESS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | fp = reinterpret_cast<Address>(__builtin_frame_address(0)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 247 | #elif V8_CC_MSVC |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 248 | // Approximate a frame pointer address. We compile without base pointers, |
| 249 | // so we can't trust ebp/rbp. |
| 250 | fp = &dummy_parameter - 2 * sizeof(void*); // NOLINT |
| 251 | #else |
| 252 | #error Unexpected platform. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 253 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | i::TraceExtension::DoTrace(fp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | static int CFunc(int depth) { |
| 259 | if (depth <= 0) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 260 | CFuncDoTrace(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | } else { |
| 263 | return CFunc(depth - 1) + 1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 268 | // This test verifies that stack tracing doesn't crash when called on |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 269 | // pure native code. TickSample::Trace only unrolls JS code, so we can't |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 270 | // get any meaningful info here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | TEST(PureCStackTrace) { |
| 272 | TickSample sample; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | i::TraceExtension::InitTraceEnv(&sample); |
| 274 | v8::HandleScope scope(CcTest::isolate()); |
| 275 | v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION); |
| 276 | v8::Context::Scope context_scope(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | // Check that sampler doesn't crash |
| 278 | CHECK_EQ(10, CFunc(10)); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | TEST(JsEntrySp) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 283 | v8::HandleScope scope(CcTest::isolate()); |
| 284 | v8::Local<v8::Context> context = CcTest::NewContext(TRACE_EXTENSION); |
| 285 | v8::Context::Scope context_scope(context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 286 | CHECK(!i::TraceExtension::GetJsEntrySp()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | CompileRun("a = 1; b = a + 1;"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 288 | CHECK(!i::TraceExtension::GetJsEntrySp()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | CompileRun("js_entry_sp();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 290 | CHECK(!i::TraceExtension::GetJsEntrySp()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 291 | CompileRun("js_entry_sp_level2();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 292 | CHECK(!i::TraceExtension::GetJsEntrySp()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | } |