Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [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. |
| 27 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 28 | #include <stdlib.h> |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "src/api.h" |
| 33 | #include "src/base/platform/condition-variable.h" |
| 34 | #include "src/base/platform/platform.h" |
| 35 | #include "src/compilation-cache.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | #include "src/debug/debug.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | #include "src/deoptimizer.h" |
| 38 | #include "src/frames.h" |
| 39 | #include "src/utils.h" |
| 40 | #include "test/cctest/cctest.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
| 42 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | using ::v8::base::Mutex; |
| 44 | using ::v8::base::LockGuard; |
| 45 | using ::v8::base::ConditionVariable; |
| 46 | using ::v8::base::OS; |
| 47 | using ::v8::base::Semaphore; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | using ::v8::internal::EmbeddedVector; |
| 49 | using ::v8::internal::Object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | using ::v8::internal::Handle; |
| 51 | using ::v8::internal::Heap; |
| 52 | using ::v8::internal::JSGlobalProxy; |
| 53 | using ::v8::internal::Code; |
| 54 | using ::v8::internal::Debug; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | using ::v8::internal::CommandMessage; |
| 56 | using ::v8::internal::CommandMessageQueue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | using ::v8::internal::StackFrame; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | using ::v8::internal::StepAction; |
| 59 | using ::v8::internal::StepIn; // From StepAction enum |
| 60 | using ::v8::internal::StepNext; // From StepAction enum |
| 61 | using ::v8::internal::StepOut; // From StepAction enum |
| 62 | using ::v8::internal::Vector; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 63 | using ::v8::internal::StrLength; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | // Size of temp buffer for formatting small strings. |
| 66 | #define SMALL_STRING_BUFFER_SIZE 80 |
| 67 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | // --- H e l p e r C l a s s e s |
| 69 | |
| 70 | |
| 71 | // Helper class for creating a V8 enviromnent for running tests |
| 72 | class DebugLocalContext { |
| 73 | public: |
| 74 | inline DebugLocalContext( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | v8::Isolate* isolate, v8::ExtensionConfiguration* extensions = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | v8::Local<v8::ObjectTemplate> global_template = |
| 77 | v8::Local<v8::ObjectTemplate>(), |
| 78 | v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | : scope_(isolate), |
| 80 | context_(v8::Context::New(isolate, extensions, global_template, |
| 81 | global_object)) { |
| 82 | context_->Enter(); |
| 83 | } |
| 84 | inline DebugLocalContext( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | v8::ExtensionConfiguration* extensions = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | v8::Local<v8::ObjectTemplate> global_template = |
| 87 | v8::Local<v8::ObjectTemplate>(), |
| 88 | v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 89 | : scope_(CcTest::isolate()), |
| 90 | context_(v8::Context::New(CcTest::isolate(), extensions, |
| 91 | global_template, global_object)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | context_->Enter(); |
| 93 | } |
| 94 | inline ~DebugLocalContext() { |
| 95 | context_->Exit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | inline v8::Local<v8::Context> context() { return context_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | inline v8::Context* operator->() { return *context_; } |
| 99 | inline v8::Context* operator*() { return *context_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | inline v8::Isolate* GetIsolate() { return context_->GetIsolate(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | inline bool IsReady() { return !context_.IsEmpty(); } |
| 102 | void ExposeDebug() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | v8::internal::Isolate* isolate = |
| 104 | reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate()); |
| 105 | v8::internal::Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | // Expose the debug context global object in the global object for testing. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | CHECK(isolate->debug()->Load()); |
| 108 | Handle<v8::internal::Context> debug_context = |
| 109 | isolate->debug()->debug_context(); |
| 110 | debug_context->set_security_token( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | v8::Utils::OpenHandle(*context_)->security_token()); |
| 112 | |
| 113 | Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( |
| 114 | v8::Utils::OpenHandle(*context_->Global()))); |
| 115 | Handle<v8::internal::String> debug_string = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("debug")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | v8::internal::JSObject::SetOwnPropertyIgnoreAttributes( |
| 118 | global, debug_string, handle(debug_context->global_proxy()), |
| 119 | v8::internal::DONT_ENUM) |
| 120 | .Check(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 122 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | v8::HandleScope scope_; |
| 125 | v8::Local<v8::Context> context_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | |
| 129 | // --- H e l p e r F u n c t i o n s |
| 130 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | // Compile and run the supplied source and return the requested function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | static v8::Local<v8::Function> CompileFunction(v8::Isolate* isolate, |
| 133 | const char* source, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | const char* function_name) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | CompileRunChecked(isolate, source); |
| 136 | v8::Local<v8::String> name = v8_str(isolate, function_name); |
| 137 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 138 | v8::MaybeLocal<v8::Value> maybe_function = |
| 139 | context->Global()->Get(context, name); |
| 140 | return v8::Local<v8::Function>::Cast(maybe_function.ToLocalChecked()); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // Compile and run the supplied source and return the requested function. |
| 145 | static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, |
| 146 | const char* source, |
| 147 | const char* function_name) { |
| 148 | return CompileFunction(env->GetIsolate(), source, function_name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | // Is there any debug info for the function? |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 153 | static bool HasDebugInfo(v8::Local<v8::Function> fun) { |
| 154 | Handle<v8::internal::JSFunction> f = |
| 155 | Handle<v8::internal::JSFunction>::cast(v8::Utils::OpenHandle(*fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 156 | Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | return shared->HasDebugInfo(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | // Set a break point in a function and return the associated break point |
| 162 | // number. |
| 163 | static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) { |
| 164 | static int break_point = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 165 | v8::internal::Isolate* isolate = fun->GetIsolate(); |
| 166 | v8::internal::Debug* debug = isolate->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 167 | debug->SetBreakPoint( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | fun, |
| 169 | Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate), |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 170 | &position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | return break_point; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | // Set a break point in a function and return the associated break point |
| 176 | // number. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | static int SetBreakPoint(v8::Local<v8::Function> fun, int position) { |
| 178 | return SetBreakPoint( |
| 179 | i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*fun)), position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | // Set a break point in a function using the Debug object and return the |
| 184 | // associated break point number. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 185 | static int SetBreakPointFromJS(v8::Isolate* isolate, |
| 186 | const char* function_name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | int line, int position) { |
| 188 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | SNPrintF(buffer, |
| 190 | "debug.Debug.setBreakPoint(%s,%d,%d)", |
| 191 | function_name, line, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); |
| 194 | return value->Int32Value(isolate->GetCurrentContext()).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | // Set a break point in a script identified by id using the global Debug object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | static int SetScriptBreakPointByIdFromJS(v8::Isolate* isolate, int script_id, |
| 200 | int line, int column) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 202 | if (column >= 0) { |
| 203 | // Column specified set script break point on precise location. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 204 | SNPrintF(buffer, |
| 205 | "debug.Debug.setScriptBreakPointById(%d,%d,%d)", |
| 206 | script_id, line, column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | } else { |
| 208 | // Column not specified set script break point on line. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | SNPrintF(buffer, |
| 210 | "debug.Debug.setScriptBreakPointById(%d,%d)", |
| 211 | script_id, line); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | } |
| 213 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 214 | { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 215 | v8::TryCatch try_catch(isolate); |
| 216 | v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | CHECK(!try_catch.HasCaught()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | return value->Int32Value(isolate->GetCurrentContext()).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | // Set a break point in a script identified by name using the global Debug |
| 224 | // object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 225 | static int SetScriptBreakPointByNameFromJS(v8::Isolate* isolate, |
| 226 | const char* script_name, int line, |
| 227 | int column) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 229 | if (column >= 0) { |
| 230 | // Column specified set script break point on precise location. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 231 | SNPrintF(buffer, |
| 232 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", |
| 233 | script_name, line, column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | } else { |
| 235 | // Column not specified set script break point on line. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 236 | SNPrintF(buffer, |
| 237 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", |
| 238 | script_name, line); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 239 | } |
| 240 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 241 | { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | v8::TryCatch try_catch(isolate); |
| 243 | v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | CHECK(!try_catch.HasCaught()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 245 | return value->Int32Value(isolate->GetCurrentContext()).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | // Clear a break point. |
| 251 | static void ClearBreakPoint(int break_point) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 253 | v8::internal::Debug* debug = isolate->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 254 | debug->ClearBreakPoint( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | // Clear a break point using the global Debug object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | static void ClearBreakPointFromJS(v8::Isolate* isolate, |
| 261 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 263 | SNPrintF(buffer, |
| 264 | "debug.Debug.clearBreakPoint(%d)", |
| 265 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 267 | CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 271 | static void EnableScriptBreakPointFromJS(v8::Isolate* isolate, |
| 272 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | SNPrintF(buffer, |
| 275 | "debug.Debug.enableScriptBreakPoint(%d)", |
| 276 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 278 | CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | static void DisableScriptBreakPointFromJS(v8::Isolate* isolate, |
| 283 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | SNPrintF(buffer, |
| 286 | "debug.Debug.disableScriptBreakPoint(%d)", |
| 287 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 289 | CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 293 | static void ChangeScriptBreakPointConditionFromJS(v8::Isolate* isolate, |
| 294 | int break_point_number, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | const char* condition) { |
| 296 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | SNPrintF(buffer, |
| 298 | "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")", |
| 299 | break_point_number, condition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 301 | CompileRunChecked(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | // Change break on exception. |
| 306 | static void ChangeBreakOnException(bool caught, bool uncaught) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 307 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 308 | debug->ChangeBreakOnException(v8::internal::BreakException, caught); |
| 309 | debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | // Change break on exception using the global Debug object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | static void ChangeBreakOnExceptionFromJS(v8::Isolate* isolate, bool caught, |
| 315 | bool uncaught) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 316 | if (caught) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 317 | CompileRunChecked(isolate, "debug.Debug.setBreakOnException()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 319 | CompileRunChecked(isolate, "debug.Debug.clearBreakOnException()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | } |
| 321 | if (uncaught) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | CompileRunChecked(isolate, "debug.Debug.setBreakOnUncaughtException()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 324 | CompileRunChecked(isolate, "debug.Debug.clearBreakOnUncaughtException()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | |
| 329 | // Prepare to step to next break location. |
| 330 | static void PrepareStep(StepAction step_action) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 332 | debug->PrepareStep(step_action); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 336 | static void ClearStepping() { CcTest::i_isolate()->debug()->ClearStepping(); } |
| 337 | |
| 338 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | // This function is in namespace v8::internal to be friend with class |
| 340 | // v8::internal::Debug. |
| 341 | namespace v8 { |
| 342 | namespace internal { |
| 343 | |
| 344 | // Collect the currently debugged functions. |
| 345 | Handle<FixedArray> GetDebuggedFunctions() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 347 | |
| 348 | v8::internal::DebugInfoListNode* node = debug->debug_info_list_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | |
| 350 | // Find the number of debugged functions. |
| 351 | int count = 0; |
| 352 | while (node) { |
| 353 | count++; |
| 354 | node = node->next(); |
| 355 | } |
| 356 | |
| 357 | // Allocate array for the debugged functions |
| 358 | Handle<FixedArray> debugged_functions = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | CcTest::i_isolate()->factory()->NewFixedArray(count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | |
| 361 | // Run through the debug info objects and collect all functions. |
| 362 | count = 0; |
| 363 | while (node) { |
| 364 | debugged_functions->set(count++, *node->debug_info()); |
| 365 | node = node->next(); |
| 366 | } |
| 367 | |
| 368 | return debugged_functions; |
| 369 | } |
| 370 | |
| 371 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | // Check that the debugger has been fully unloaded. |
| 373 | void CheckDebuggerUnloaded(bool check_functions) { |
| 374 | // Check that the debugger context is cleared and that there is no debug |
| 375 | // information stored for the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 376 | CHECK(CcTest::i_isolate()->debug()->debug_context().is_null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 377 | CHECK(!CcTest::i_isolate()->debug()->debug_info_list_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | |
| 379 | // Collect garbage to ensure weak handles are cleared. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 380 | CcTest::heap()->CollectAllGarbage(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 381 | CcTest::heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | |
| 383 | // Iterate the head and check that there are no debugger related objects left. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 384 | HeapIterator iterator(CcTest::heap()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 385 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | CHECK(!obj->IsDebugInfo()); |
| 387 | CHECK(!obj->IsBreakPointInfo()); |
| 388 | |
| 389 | // If deep check of functions is requested check that no debug break code |
| 390 | // is left in all functions. |
| 391 | if (check_functions) { |
| 392 | if (obj->IsJSFunction()) { |
| 393 | JSFunction* fun = JSFunction::cast(obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 394 | for (RelocIterator it(fun->shared()->code(), |
| 395 | RelocInfo::kDebugBreakSlotMask); |
| 396 | !it.done(); it.next()) { |
| 397 | CHECK(!it.rinfo()->IsPatchedDebugBreakSlotSequence()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 405 | } // namespace internal |
| 406 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | |
| 408 | |
| 409 | // Check that the debugger has been fully unloaded. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 410 | static void CheckDebuggerUnloaded(v8::Isolate* isolate, |
| 411 | bool check_functions = false) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 412 | // Let debugger to unload itself synchronously |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 413 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 414 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 415 | v8::internal::CheckDebuggerUnloaded(check_functions); |
| 416 | } |
| 417 | |
| 418 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 419 | // --- D e b u g E v e n t H a n d l e r s |
| 420 | // --- |
| 421 | // --- The different tests uses a number of debug event handlers. |
| 422 | // --- |
| 423 | |
| 424 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 425 | // Source for the JavaScript function which picks out the function |
| 426 | // name of a frame. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 427 | const char* frame_function_name_source = |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 428 | "function frame_function_name(exec_state, frame_number) {" |
| 429 | " return exec_state.frame(frame_number).func().name();" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | "}"; |
| 431 | v8::Local<v8::Function> frame_function_name; |
| 432 | |
| 433 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 434 | // Source for the JavaScript function which pick out the name of the |
| 435 | // first argument of a frame. |
| 436 | const char* frame_argument_name_source = |
| 437 | "function frame_argument_name(exec_state, frame_number) {" |
| 438 | " return exec_state.frame(frame_number).argumentName(0);" |
| 439 | "}"; |
| 440 | v8::Local<v8::Function> frame_argument_name; |
| 441 | |
| 442 | |
| 443 | // Source for the JavaScript function which pick out the value of the |
| 444 | // first argument of a frame. |
| 445 | const char* frame_argument_value_source = |
| 446 | "function frame_argument_value(exec_state, frame_number) {" |
| 447 | " return exec_state.frame(frame_number).argumentValue(0).value_;" |
| 448 | "}"; |
| 449 | v8::Local<v8::Function> frame_argument_value; |
| 450 | |
| 451 | |
| 452 | // Source for the JavaScript function which pick out the name of the |
| 453 | // first argument of a frame. |
| 454 | const char* frame_local_name_source = |
| 455 | "function frame_local_name(exec_state, frame_number) {" |
| 456 | " return exec_state.frame(frame_number).localName(0);" |
| 457 | "}"; |
| 458 | v8::Local<v8::Function> frame_local_name; |
| 459 | |
| 460 | |
| 461 | // Source for the JavaScript function which pick out the value of the |
| 462 | // first argument of a frame. |
| 463 | const char* frame_local_value_source = |
| 464 | "function frame_local_value(exec_state, frame_number) {" |
| 465 | " return exec_state.frame(frame_number).localValue(0).value_;" |
| 466 | "}"; |
| 467 | v8::Local<v8::Function> frame_local_value; |
| 468 | |
| 469 | |
| 470 | // Source for the JavaScript function which picks out the source line for the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 471 | // top frame. |
| 472 | const char* frame_source_line_source = |
| 473 | "function frame_source_line(exec_state) {" |
| 474 | " return exec_state.frame(0).sourceLine();" |
| 475 | "}"; |
| 476 | v8::Local<v8::Function> frame_source_line; |
| 477 | |
| 478 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 479 | // Source for the JavaScript function which picks out the source column for the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | // top frame. |
| 481 | const char* frame_source_column_source = |
| 482 | "function frame_source_column(exec_state) {" |
| 483 | " return exec_state.frame(0).sourceColumn();" |
| 484 | "}"; |
| 485 | v8::Local<v8::Function> frame_source_column; |
| 486 | |
| 487 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 488 | // Source for the JavaScript function which picks out the script name for the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | // top frame. |
| 490 | const char* frame_script_name_source = |
| 491 | "function frame_script_name(exec_state) {" |
| 492 | " return exec_state.frame(0).func().script().name();" |
| 493 | "}"; |
| 494 | v8::Local<v8::Function> frame_script_name; |
| 495 | |
| 496 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 497 | // Source for the JavaScript function which returns the number of frames. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | static const char* frame_count_source = |
| 499 | "function frame_count(exec_state) {" |
| 500 | " return exec_state.frameCount();" |
| 501 | "}"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | v8::Local<v8::Function> frame_count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 503 | |
| 504 | |
| 505 | // Global variable to store the last function hit - used by some tests. |
| 506 | char last_function_hit[80]; |
| 507 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 508 | // Global variable to store the name for last script hit - used by some tests. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 509 | char last_script_name_hit[80]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 510 | |
| 511 | // Global variables to store the last source position - used by some tests. |
| 512 | int last_source_line = -1; |
| 513 | int last_source_column = -1; |
| 514 | |
| 515 | // Debug event handler which counts the break points which have been hit. |
| 516 | int break_point_hit_count = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 517 | int break_point_hit_count_deoptimize = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 518 | static void DebugEventBreakPointHitCount( |
| 519 | const v8::Debug::EventDetails& event_details) { |
| 520 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 521 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 522 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 524 | Debug* debug = isolate->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 525 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 526 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | |
| 528 | // Count the number of breaks. |
| 529 | if (event == v8::Break) { |
| 530 | break_point_hit_count++; |
| 531 | if (!frame_function_name.IsEmpty()) { |
| 532 | // Get the name of the function. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 533 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 534 | v8::Local<v8::Value> argv[argc] = { |
| 535 | exec_state, v8::Integer::New(CcTest::isolate(), 0)}; |
| 536 | v8::Local<v8::Value> result = |
| 537 | frame_function_name->Call(context, exec_state, argc, argv) |
| 538 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | if (result->IsUndefined()) { |
| 540 | last_function_hit[0] = '\0'; |
| 541 | } else { |
| 542 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 543 | v8::Local<v8::String> function_name(result.As<v8::String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 544 | function_name->WriteUtf8(last_function_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | if (!frame_source_line.IsEmpty()) { |
| 549 | // Get the source line. |
| 550 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 551 | v8::Local<v8::Value> argv[argc] = {exec_state}; |
| 552 | v8::Local<v8::Value> result = |
| 553 | frame_source_line->Call(context, exec_state, argc, argv) |
| 554 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | CHECK(result->IsNumber()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 556 | last_source_line = result->Int32Value(context).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | if (!frame_source_column.IsEmpty()) { |
| 560 | // Get the source column. |
| 561 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 562 | v8::Local<v8::Value> argv[argc] = {exec_state}; |
| 563 | v8::Local<v8::Value> result = |
| 564 | frame_source_column->Call(context, exec_state, argc, argv) |
| 565 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 566 | CHECK(result->IsNumber()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 567 | last_source_column = result->Int32Value(context).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | if (!frame_script_name.IsEmpty()) { |
| 571 | // Get the script name of the function script. |
| 572 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 573 | v8::Local<v8::Value> argv[argc] = {exec_state}; |
| 574 | v8::Local<v8::Value> result = |
| 575 | frame_script_name->Call(context, exec_state, argc, argv) |
| 576 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 577 | if (result->IsUndefined()) { |
| 578 | last_script_name_hit[0] = '\0'; |
| 579 | } else { |
| 580 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 581 | v8::Local<v8::String> script_name(result.As<v8::String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 582 | script_name->WriteUtf8(last_script_name_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 585 | |
| 586 | // Perform a full deoptimization when the specified number of |
| 587 | // breaks have been hit. |
| 588 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 589 | i::Deoptimizer::DeoptimizeAll(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 590 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | |
| 595 | // Debug event handler which counts a number of events and collects the stack |
| 596 | // height if there is a function compiled for that. |
| 597 | int exception_hit_count = 0; |
| 598 | int uncaught_exception_hit_count = 0; |
| 599 | int last_js_stack_height = -1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 600 | v8::Local<v8::Function> debug_event_listener_callback; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 601 | int debug_event_listener_callback_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 602 | |
| 603 | static void DebugEventCounterClear() { |
| 604 | break_point_hit_count = 0; |
| 605 | exception_hit_count = 0; |
| 606 | uncaught_exception_hit_count = 0; |
| 607 | } |
| 608 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 609 | static void DebugEventCounter( |
| 610 | const v8::Debug::EventDetails& event_details) { |
| 611 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 612 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 613 | v8::Local<v8::Object> event_data = event_details.GetEventData(); |
| 614 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 615 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 616 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 617 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 618 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 619 | |
| 620 | // Count the number of breaks. |
| 621 | if (event == v8::Break) { |
| 622 | break_point_hit_count++; |
| 623 | } else if (event == v8::Exception) { |
| 624 | exception_hit_count++; |
| 625 | |
| 626 | // Check whether the exception was uncaught. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 627 | v8::Local<v8::String> fun_name = v8_str(CcTest::isolate(), "uncaught"); |
| 628 | v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast( |
| 629 | event_data->Get(context, fun_name).ToLocalChecked()); |
| 630 | v8::Local<v8::Value> result = |
| 631 | fun->Call(context, event_data, 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 632 | if (result->IsTrue()) { |
| 633 | uncaught_exception_hit_count++; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // Collect the JavsScript stack height if the function frame_count is |
| 638 | // compiled. |
| 639 | if (!frame_count.IsEmpty()) { |
| 640 | static const int kArgc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 641 | v8::Local<v8::Value> argv[kArgc] = {exec_state}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 642 | // Using exec_state as receiver is just to have a receiver. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 643 | v8::Local<v8::Value> result = |
| 644 | frame_count->Call(context, exec_state, kArgc, argv).ToLocalChecked(); |
| 645 | last_js_stack_height = result->Int32Value(context).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 646 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 647 | |
| 648 | // Run callback from DebugEventListener and check the result. |
| 649 | if (!debug_event_listener_callback.IsEmpty()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 650 | v8::Local<v8::Value> result = |
| 651 | debug_event_listener_callback->Call(context, event_data, 0, NULL) |
| 652 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 653 | CHECK(!result.IsEmpty()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 654 | CHECK_EQ(debug_event_listener_callback_result, |
| 655 | result->Int32Value(context).FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 656 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | // Debug event handler which evaluates a number of expressions when a break |
| 661 | // point is hit. Each evaluated expression is compared with an expected value. |
| 662 | // For this debug event handler to work the following two global varaibles |
| 663 | // must be initialized. |
| 664 | // checks: An array of expressions and expected results |
| 665 | // evaluate_check_function: A JavaScript function (see below) |
| 666 | |
| 667 | // Structure for holding checks to do. |
| 668 | struct EvaluateCheck { |
| 669 | const char* expr; // An expression to evaluate when a break point is hit. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 670 | v8::Local<v8::Value> expected; // The expected result. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 671 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 672 | |
| 673 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 674 | // Array of checks to do. |
| 675 | struct EvaluateCheck* checks = NULL; |
| 676 | // Source for The JavaScript function which can do the evaluation when a break |
| 677 | // point is hit. |
| 678 | const char* evaluate_check_source = |
| 679 | "function evaluate_check(exec_state, expr, expected) {" |
| 680 | " return exec_state.frame(0).evaluate(expr).value() === expected;" |
| 681 | "}"; |
| 682 | v8::Local<v8::Function> evaluate_check_function; |
| 683 | |
| 684 | // The actual debug event described by the longer comment above. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 685 | static void DebugEventEvaluate( |
| 686 | const v8::Debug::EventDetails& event_details) { |
| 687 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 688 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 689 | v8::Isolate* isolate = CcTest::isolate(); |
| 690 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 691 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 693 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 694 | |
| 695 | if (event == v8::Break) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 696 | break_point_hit_count++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 697 | for (int i = 0; checks[i].expr != NULL; i++) { |
| 698 | const int argc = 3; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 699 | v8::Local<v8::String> string = v8_str(isolate, checks[i].expr); |
| 700 | v8::Local<v8::Value> argv[argc] = {exec_state, string, |
| 701 | checks[i].expected}; |
| 702 | v8::Local<v8::Value> result = |
| 703 | evaluate_check_function->Call(context, exec_state, argc, argv) |
| 704 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 705 | if (!result->IsTrue()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 706 | v8::String::Utf8Value utf8(checks[i].expected); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 707 | V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *utf8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | |
| 714 | // This debug event listener removes a breakpoint in a function |
| 715 | int debug_event_remove_break_point = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 716 | static void DebugEventRemoveBreakPoint( |
| 717 | const v8::Debug::EventDetails& event_details) { |
| 718 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 719 | v8::Local<v8::Value> data = event_details.GetCallbackData(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 720 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 722 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 723 | |
| 724 | if (event == v8::Break) { |
| 725 | break_point_hit_count++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 726 | CHECK(data->IsFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | ClearBreakPoint(debug_event_remove_break_point); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | |
| 732 | // Debug event handler which counts break points hit and performs a step |
| 733 | // afterwards. |
| 734 | StepAction step_action = StepIn; // Step action to perform when stepping. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 735 | static void DebugEventStep( |
| 736 | const v8::Debug::EventDetails& event_details) { |
| 737 | v8::DebugEvent event = event_details.GetEvent(); |
| 738 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 739 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 740 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 741 | |
| 742 | if (event == v8::Break) { |
| 743 | break_point_hit_count++; |
| 744 | PrepareStep(step_action); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | // Debug event handler which counts break points hit and performs a step |
| 750 | // afterwards. For each call the expected function is checked. |
| 751 | // For this debug event handler to work the following two global varaibles |
| 752 | // must be initialized. |
| 753 | // expected_step_sequence: An array of the expected function call sequence. |
| 754 | // frame_function_name: A JavaScript function (see below). |
| 755 | |
| 756 | // String containing the expected function call sequence. Note: this only works |
| 757 | // if functions have name length of one. |
| 758 | const char* expected_step_sequence = NULL; |
| 759 | |
| 760 | // The actual debug event described by the longer comment above. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 761 | static void DebugEventStepSequence( |
| 762 | const v8::Debug::EventDetails& event_details) { |
| 763 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 764 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 765 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 766 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 767 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 768 | |
| 769 | if (event == v8::Break || event == v8::Exception) { |
| 770 | // Check that the current function is the expected. |
| 771 | CHECK(break_point_hit_count < |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 772 | StrLength(expected_step_sequence)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 773 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 774 | v8::Local<v8::Value> argv[argc] = {exec_state, |
| 775 | v8::Integer::New(CcTest::isolate(), 0)}; |
| 776 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
| 777 | v8::Local<v8::Value> result = |
| 778 | frame_function_name->Call(context, exec_state, argc, argv) |
| 779 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 780 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 781 | v8::String::Utf8Value function_name( |
| 782 | result->ToString(context).ToLocalChecked()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 783 | CHECK_EQ(1, StrLength(*function_name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 784 | CHECK_EQ((*function_name)[0], |
| 785 | expected_step_sequence[break_point_hit_count]); |
| 786 | |
| 787 | // Perform step. |
| 788 | break_point_hit_count++; |
| 789 | PrepareStep(step_action); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | |
| 794 | // Debug event handler which performs a garbage collection. |
| 795 | static void DebugEventBreakPointCollectGarbage( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 796 | const v8::Debug::EventDetails& event_details) { |
| 797 | v8::DebugEvent event = event_details.GetEvent(); |
| 798 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 800 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 801 | |
| 802 | // Perform a garbage collection when break point is hit and continue. Based |
| 803 | // on the number of break points hit either scavenge or mark compact |
| 804 | // collector is used. |
| 805 | if (event == v8::Break) { |
| 806 | break_point_hit_count++; |
| 807 | if (break_point_hit_count % 2 == 0) { |
| 808 | // Scavenge. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 809 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 810 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 811 | // Mark sweep compact. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 812 | CcTest::heap()->CollectAllGarbage(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | |
| 818 | // Debug event handler which re-issues a debug break and calls the garbage |
| 819 | // collector to have the heap verified. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 820 | static void DebugEventBreak( |
| 821 | const v8::Debug::EventDetails& event_details) { |
| 822 | v8::DebugEvent event = event_details.GetEvent(); |
| 823 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 824 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 825 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 826 | |
| 827 | if (event == v8::Break) { |
| 828 | // Count the number of breaks. |
| 829 | break_point_hit_count++; |
| 830 | |
| 831 | // Run the garbage collector to enforce heap verification if option |
| 832 | // --verify-heap is set. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 833 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 834 | |
| 835 | // Set the break flag again to come back here as soon as possible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 836 | v8::Debug::DebugBreak(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 841 | // Debug event handler which re-issues a debug break until a limit has been |
| 842 | // reached. |
| 843 | int max_break_point_hit_count = 0; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 844 | bool terminate_after_max_break_point_hit = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 845 | static void DebugEventBreakMax( |
| 846 | const v8::Debug::EventDetails& event_details) { |
| 847 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 848 | v8::Isolate* v8_isolate = CcTest::isolate(); |
| 849 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 850 | v8::internal::Debug* debug = isolate->debug(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 851 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 852 | CHECK_NE(debug->break_id(), 0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 853 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 854 | if (event == v8::Break) { |
| 855 | if (break_point_hit_count < max_break_point_hit_count) { |
| 856 | // Count the number of breaks. |
| 857 | break_point_hit_count++; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 858 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 859 | // Set the break flag again to come back here as soon as possible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 860 | v8::Debug::DebugBreak(v8_isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 861 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 862 | } else if (terminate_after_max_break_point_hit) { |
| 863 | // Terminate execution after the last break if requested. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 864 | v8_isolate->TerminateExecution(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 865 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 866 | |
| 867 | // Perform a full deoptimization when the specified number of |
| 868 | // breaks have been hit. |
| 869 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 870 | i::Deoptimizer::DeoptimizeAll(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 871 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 876 | // --- M e s s a g e C a l l b a c k |
| 877 | |
| 878 | |
| 879 | // Message callback which counts the number of messages. |
| 880 | int message_callback_count = 0; |
| 881 | |
| 882 | static void MessageCallbackCountClear() { |
| 883 | message_callback_count = 0; |
| 884 | } |
| 885 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 886 | static void MessageCallbackCount(v8::Local<v8::Message> message, |
| 887 | v8::Local<v8::Value> data) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 888 | message_callback_count++; |
| 889 | } |
| 890 | |
| 891 | |
| 892 | // --- T h e A c t u a l T e s t s |
| 893 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | // Test that the debug info in the VM is in sync with the functions being |
| 895 | // debugged. |
| 896 | TEST(DebugInfo) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 897 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 898 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 899 | // Create a couple of functions for the test. |
| 900 | v8::Local<v8::Function> foo = |
| 901 | CompileFunction(&env, "function foo(){}", "foo"); |
| 902 | v8::Local<v8::Function> bar = |
| 903 | CompileFunction(&env, "function bar(){}", "bar"); |
| 904 | // Initially no functions are debugged. |
| 905 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 906 | CHECK(!HasDebugInfo(foo)); |
| 907 | CHECK(!HasDebugInfo(bar)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 908 | EnableDebugger(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 909 | // One function (foo) is debugged. |
| 910 | int bp1 = SetBreakPoint(foo, 0); |
| 911 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 912 | CHECK(HasDebugInfo(foo)); |
| 913 | CHECK(!HasDebugInfo(bar)); |
| 914 | // Two functions are debugged. |
| 915 | int bp2 = SetBreakPoint(bar, 0); |
| 916 | CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); |
| 917 | CHECK(HasDebugInfo(foo)); |
| 918 | CHECK(HasDebugInfo(bar)); |
| 919 | // One function (bar) is debugged. |
| 920 | ClearBreakPoint(bp1); |
| 921 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 922 | CHECK(!HasDebugInfo(foo)); |
| 923 | CHECK(HasDebugInfo(bar)); |
| 924 | // No functions are debugged. |
| 925 | ClearBreakPoint(bp2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 926 | DisableDebugger(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 927 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 928 | CHECK(!HasDebugInfo(foo)); |
| 929 | CHECK(!HasDebugInfo(bar)); |
| 930 | } |
| 931 | |
| 932 | |
| 933 | // Test that a break point can be set at an IC store location. |
| 934 | TEST(BreakPointICStore) { |
| 935 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 936 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 937 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 938 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 939 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 940 | DebugEventBreakPointHitCount); |
| 941 | v8::Local<v8::Function> foo = |
| 942 | CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 943 | |
| 944 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 945 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 946 | CHECK_EQ(0, break_point_hit_count); |
| 947 | |
| 948 | // Run with breakpoint |
| 949 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 950 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 951 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 952 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 953 | CHECK_EQ(2, break_point_hit_count); |
| 954 | |
| 955 | // Run without breakpoints. |
| 956 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 957 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | CHECK_EQ(2, break_point_hit_count); |
| 959 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 960 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 961 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | |
| 965 | // Test that a break point can be set at an IC load location. |
| 966 | TEST(BreakPointICLoad) { |
| 967 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 968 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 969 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 970 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 971 | DebugEventBreakPointHitCount); |
| 972 | |
| 973 | CompileRunChecked(env->GetIsolate(), "bar=1"); |
| 974 | v8::Local<v8::Function> foo = |
| 975 | CompileFunction(&env, "function foo(){var x=bar;}", "foo"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 976 | |
| 977 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 978 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 979 | CHECK_EQ(0, break_point_hit_count); |
| 980 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 981 | // Run with breakpoint. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 982 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 983 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 984 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 985 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 986 | CHECK_EQ(2, break_point_hit_count); |
| 987 | |
| 988 | // Run without breakpoints. |
| 989 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 990 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | CHECK_EQ(2, break_point_hit_count); |
| 992 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 993 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 994 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | |
| 998 | // Test that a break point can be set at an IC call location. |
| 999 | TEST(BreakPointICCall) { |
| 1000 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1001 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1002 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1003 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1004 | DebugEventBreakPointHitCount); |
| 1005 | CompileRunChecked(env->GetIsolate(), "function bar(){}"); |
| 1006 | v8::Local<v8::Function> foo = |
| 1007 | CompileFunction(&env, "function foo(){bar();}", "foo"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1010 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1011 | CHECK_EQ(0, break_point_hit_count); |
| 1012 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1013 | // Run with breakpoint |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1014 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1015 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1017 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1018 | CHECK_EQ(2, break_point_hit_count); |
| 1019 | |
| 1020 | // Run without breakpoints. |
| 1021 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1022 | foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1023 | CHECK_EQ(2, break_point_hit_count); |
| 1024 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1025 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1026 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1030 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1031 | TEST(BreakPointICCallWithGC) { |
| 1032 | break_point_hit_count = 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1033 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1034 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1035 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1036 | DebugEventBreakPointCollectGarbage); |
| 1037 | CompileRunChecked(env->GetIsolate(), "function bar(){return 1;}"); |
| 1038 | v8::Local<v8::Function> foo = |
| 1039 | CompileFunction(&env, "function foo(){return bar();}", "foo"); |
| 1040 | v8::Local<v8::Context> context = env.context(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1041 | |
| 1042 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1043 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1044 | .ToLocalChecked() |
| 1045 | ->Int32Value(context) |
| 1046 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1047 | CHECK_EQ(0, break_point_hit_count); |
| 1048 | |
| 1049 | // Run with breakpoint. |
| 1050 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1051 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1052 | .ToLocalChecked() |
| 1053 | ->Int32Value(context) |
| 1054 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1055 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1056 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1057 | .ToLocalChecked() |
| 1058 | ->Int32Value(context) |
| 1059 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1060 | CHECK_EQ(2, break_point_hit_count); |
| 1061 | |
| 1062 | // Run without breakpoints. |
| 1063 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1064 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1065 | CHECK_EQ(2, break_point_hit_count); |
| 1066 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1067 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1068 | CheckDebuggerUnloaded(env->GetIsolate()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1073 | TEST(BreakPointConstructCallWithGC) { |
| 1074 | break_point_hit_count = 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1075 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1076 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1077 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1078 | DebugEventBreakPointCollectGarbage); |
| 1079 | CompileRunChecked(env->GetIsolate(), "function bar(){ this.x = 1;}"); |
| 1080 | v8::Local<v8::Function> foo = |
| 1081 | CompileFunction(&env, "function foo(){return new bar(1).x;}", "foo"); |
| 1082 | v8::Local<v8::Context> context = env.context(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1083 | |
| 1084 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1085 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1086 | .ToLocalChecked() |
| 1087 | ->Int32Value(context) |
| 1088 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1089 | CHECK_EQ(0, break_point_hit_count); |
| 1090 | |
| 1091 | // Run with breakpoint. |
| 1092 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1093 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1094 | .ToLocalChecked() |
| 1095 | ->Int32Value(context) |
| 1096 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1097 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1098 | CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) |
| 1099 | .ToLocalChecked() |
| 1100 | ->Int32Value(context) |
| 1101 | .FromJust()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1102 | CHECK_EQ(2, break_point_hit_count); |
| 1103 | |
| 1104 | // Run without breakpoints. |
| 1105 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1106 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1107 | CHECK_EQ(2, break_point_hit_count); |
| 1108 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1109 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1110 | CheckDebuggerUnloaded(env->GetIsolate()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1114 | // Test that a break point can be set at a return store location. |
| 1115 | TEST(BreakPointReturn) { |
| 1116 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1117 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1118 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Create a functions for checking the source line and column when hitting |
| 1121 | // a break point. |
| 1122 | frame_source_line = CompileFunction(&env, |
| 1123 | frame_source_line_source, |
| 1124 | "frame_source_line"); |
| 1125 | frame_source_column = CompileFunction(&env, |
| 1126 | frame_source_column_source, |
| 1127 | "frame_source_column"); |
| 1128 | |
| 1129 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1130 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1131 | DebugEventBreakPointHitCount); |
| 1132 | v8::Local<v8::Function> foo = |
| 1133 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1134 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | |
| 1136 | // Run without breakpoints. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1137 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1138 | CHECK_EQ(0, break_point_hit_count); |
| 1139 | |
| 1140 | // Run with breakpoint |
| 1141 | int bp = SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1142 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1143 | CHECK_EQ(1, break_point_hit_count); |
| 1144 | CHECK_EQ(0, last_source_line); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1145 | CHECK_EQ(15, last_source_column); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1146 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1147 | CHECK_EQ(2, break_point_hit_count); |
| 1148 | CHECK_EQ(0, last_source_line); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1149 | CHECK_EQ(15, last_source_column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Run without breakpoints. |
| 1152 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1153 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1154 | CHECK_EQ(2, break_point_hit_count); |
| 1155 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1156 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1157 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1161 | static void CallWithBreakPoints(v8::Local<v8::Context> context, |
| 1162 | v8::Local<v8::Object> recv, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1163 | v8::Local<v8::Function> f, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1164 | int break_point_count, int call_count) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1165 | break_point_hit_count = 0; |
| 1166 | for (int i = 0; i < call_count; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1167 | f->Call(context, recv, 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1168 | CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1172 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1173 | // Test GC during break point processing. |
| 1174 | TEST(GCDuringBreakPointProcessing) { |
| 1175 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1176 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1177 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1178 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1179 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1180 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1181 | DebugEventBreakPointCollectGarbage); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1182 | v8::Local<v8::Function> foo; |
| 1183 | |
| 1184 | // Test IC store break point with garbage collection. |
| 1185 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1186 | SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1187 | CallWithBreakPoints(context, env->Global(), foo, 1, 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1188 | |
| 1189 | // Test IC load break point with garbage collection. |
| 1190 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1191 | SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1192 | CallWithBreakPoints(context, env->Global(), foo, 1, 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1193 | |
| 1194 | // Test IC call break point with garbage collection. |
| 1195 | foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); |
| 1196 | SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1197 | CallWithBreakPoints(context, env->Global(), foo, 1, 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Test return break point with garbage collection. |
| 1200 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1201 | SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1202 | CallWithBreakPoints(context, env->Global(), foo, 1, 25); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1203 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1204 | // Test debug break slot break point with garbage collection. |
| 1205 | foo = CompileFunction(&env, "function foo(){var a;}", "foo"); |
| 1206 | SetBreakPoint(foo, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1207 | CallWithBreakPoints(context, env->Global(), foo, 1, 25); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1208 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1209 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1210 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | // Call the function three times with different garbage collections in between |
| 1215 | // and make sure that the break point survives. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1216 | static void CallAndGC(v8::Local<v8::Context> context, |
| 1217 | v8::Local<v8::Object> recv, v8::Local<v8::Function> f) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | break_point_hit_count = 0; |
| 1219 | |
| 1220 | for (int i = 0; i < 3; i++) { |
| 1221 | // Call function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1222 | f->Call(context, recv, 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1223 | CHECK_EQ(1 + i * 3, break_point_hit_count); |
| 1224 | |
| 1225 | // Scavenge and call function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1226 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1227 | f->Call(context, recv, 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1228 | CHECK_EQ(2 + i * 3, break_point_hit_count); |
| 1229 | |
| 1230 | // Mark sweep (and perhaps compact) and call function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1231 | CcTest::heap()->CollectAllGarbage(); |
| 1232 | f->Call(context, recv, 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1233 | CHECK_EQ(3 + i * 3, break_point_hit_count); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1238 | // Test that a break point can be set at a return store location. |
| 1239 | TEST(BreakPointSurviveGC) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1240 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1241 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1242 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1243 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1244 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1245 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1246 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1247 | v8::Local<v8::Function> foo; |
| 1248 | |
| 1249 | // Test IC store break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1250 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1251 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1252 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1253 | SetBreakPoint(foo, 0); |
| 1254 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1255 | CallAndGC(context, env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Test IC load break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1258 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1259 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1260 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1261 | SetBreakPoint(foo, 0); |
| 1262 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1263 | CallAndGC(context, env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1264 | |
| 1265 | // Test IC call break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1266 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1267 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1268 | foo = CompileFunction(&env, |
| 1269 | "function bar(){};function foo(){bar();}", |
| 1270 | "foo"); |
| 1271 | SetBreakPoint(foo, 0); |
| 1272 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1273 | CallAndGC(context, env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Test return break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1276 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1277 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1278 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1279 | SetBreakPoint(foo, 0); |
| 1280 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1281 | CallAndGC(context, env->Global(), foo); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1282 | |
| 1283 | // Test non IC break point with garbage collection. |
| 1284 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1285 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1286 | foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); |
| 1287 | SetBreakPoint(foo, 0); |
| 1288 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1289 | CallAndGC(context, env->Global(), foo); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1290 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1291 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1292 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1293 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | // Test that break points can be set using the global Debug object. |
| 1298 | TEST(BreakPointThroughJavaScript) { |
| 1299 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1300 | DebugLocalContext env; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1301 | v8::Isolate* isolate = env->GetIsolate(); |
| 1302 | v8::HandleScope scope(isolate); |
| 1303 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1304 | env.ExposeDebug(); |
| 1305 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1306 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
| 1307 | CompileRunChecked(isolate, "function bar(){}"); |
| 1308 | CompileFunction(isolate, "function foo(){bar();bar();}", "foo"); |
| 1309 | // 012345678901234567890 |
| 1310 | // 1 2 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1311 | // Break points are set at position 3 and 9 |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1312 | v8::Local<v8::String> source = v8_str(env->GetIsolate(), "foo()"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1313 | v8::Local<v8::Script> foo = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1314 | v8::Script::Compile(context, source).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1315 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1316 | CHECK_EQ(0, break_point_hit_count); |
| 1317 | |
| 1318 | // Run with one breakpoint |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1319 | int bp1 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 3); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1320 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1321 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1322 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1323 | CHECK_EQ(2, break_point_hit_count); |
| 1324 | |
| 1325 | // Run with two breakpoints |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1326 | int bp2 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 9); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1327 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | CHECK_EQ(4, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1329 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1330 | CHECK_EQ(6, break_point_hit_count); |
| 1331 | |
| 1332 | // Run with one breakpoint |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1333 | ClearBreakPointFromJS(env->GetIsolate(), bp2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1334 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1335 | CHECK_EQ(7, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1336 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1337 | CHECK_EQ(8, break_point_hit_count); |
| 1338 | |
| 1339 | // Run without breakpoints. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1340 | ClearBreakPointFromJS(env->GetIsolate(), bp1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1341 | foo->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1342 | CHECK_EQ(8, break_point_hit_count); |
| 1343 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1344 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 1345 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Make sure that the break point numbers are consecutive. |
| 1348 | CHECK_EQ(1, bp1); |
| 1349 | CHECK_EQ(2, bp2); |
| 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | // Test that break points on scripts identified by name can be set using the |
| 1354 | // global Debug object. |
| 1355 | TEST(ScriptBreakPointByNameThroughJavaScript) { |
| 1356 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1357 | DebugLocalContext env; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1358 | v8::Isolate* isolate = env->GetIsolate(); |
| 1359 | v8::HandleScope scope(isolate); |
| 1360 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1361 | env.ExposeDebug(); |
| 1362 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1363 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1364 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1365 | v8::Local<v8::String> script = v8_str(isolate, |
| 1366 | "function f() {\n" |
| 1367 | " function h() {\n" |
| 1368 | " a = 0; // line 2\n" |
| 1369 | " }\n" |
| 1370 | " b = 1; // line 4\n" |
| 1371 | " return h();\n" |
| 1372 | "}\n" |
| 1373 | "\n" |
| 1374 | "function g() {\n" |
| 1375 | " function h() {\n" |
| 1376 | " a = 0;\n" |
| 1377 | " }\n" |
| 1378 | " b = 2; // line 12\n" |
| 1379 | " h();\n" |
| 1380 | " b = 3; // line 14\n" |
| 1381 | " f(); // line 15\n" |
| 1382 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Compile the script and get the two functions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1385 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); |
| 1386 | v8::Script::Compile(context, script, &origin) |
| 1387 | .ToLocalChecked() |
| 1388 | ->Run(context) |
| 1389 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1390 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1391 | env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1392 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1393 | env->Global()->Get(context, v8_str(isolate, "g")).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1394 | |
| 1395 | // Call f and g without break points. |
| 1396 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1397 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1398 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1399 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1400 | CHECK_EQ(0, break_point_hit_count); |
| 1401 | |
| 1402 | // Call f and g with break point on line 12. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1403 | int sbp1 = SetScriptBreakPointByNameFromJS(isolate, "test", 12, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1404 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1405 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1406 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1407 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1408 | CHECK_EQ(1, break_point_hit_count); |
| 1409 | |
| 1410 | // Remove the break point again. |
| 1411 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1412 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1413 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1414 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1415 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1416 | CHECK_EQ(0, break_point_hit_count); |
| 1417 | |
| 1418 | // Call f and g with break point on line 2. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1419 | int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1420 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1421 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1422 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1423 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1424 | CHECK_EQ(2, break_point_hit_count); |
| 1425 | |
| 1426 | // Call f and g with break point on line 2, 4, 12, 14 and 15. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1427 | int sbp3 = SetScriptBreakPointByNameFromJS(isolate, "test", 4, 0); |
| 1428 | int sbp4 = SetScriptBreakPointByNameFromJS(isolate, "test", 12, 0); |
| 1429 | int sbp5 = SetScriptBreakPointByNameFromJS(isolate, "test", 14, 0); |
| 1430 | int sbp6 = SetScriptBreakPointByNameFromJS(isolate, "test", 15, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1431 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1432 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1433 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1434 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1435 | CHECK_EQ(7, break_point_hit_count); |
| 1436 | |
| 1437 | // Remove all the break points again. |
| 1438 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1439 | ClearBreakPointFromJS(isolate, sbp2); |
| 1440 | ClearBreakPointFromJS(isolate, sbp3); |
| 1441 | ClearBreakPointFromJS(isolate, sbp4); |
| 1442 | ClearBreakPointFromJS(isolate, sbp5); |
| 1443 | ClearBreakPointFromJS(isolate, sbp6); |
| 1444 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1445 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1446 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1447 | CHECK_EQ(0, break_point_hit_count); |
| 1448 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1449 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 1450 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1451 | |
| 1452 | // Make sure that the break point numbers are consecutive. |
| 1453 | CHECK_EQ(1, sbp1); |
| 1454 | CHECK_EQ(2, sbp2); |
| 1455 | CHECK_EQ(3, sbp3); |
| 1456 | CHECK_EQ(4, sbp4); |
| 1457 | CHECK_EQ(5, sbp5); |
| 1458 | CHECK_EQ(6, sbp6); |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | TEST(ScriptBreakPointByIdThroughJavaScript) { |
| 1463 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1464 | DebugLocalContext env; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1465 | v8::Isolate* isolate = env->GetIsolate(); |
| 1466 | v8::HandleScope scope(isolate); |
| 1467 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | env.ExposeDebug(); |
| 1469 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1470 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1471 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1472 | v8::Local<v8::String> source = v8_str(isolate, |
| 1473 | "function f() {\n" |
| 1474 | " function h() {\n" |
| 1475 | " a = 0; // line 2\n" |
| 1476 | " }\n" |
| 1477 | " b = 1; // line 4\n" |
| 1478 | " return h();\n" |
| 1479 | "}\n" |
| 1480 | "\n" |
| 1481 | "function g() {\n" |
| 1482 | " function h() {\n" |
| 1483 | " a = 0;\n" |
| 1484 | " }\n" |
| 1485 | " b = 2; // line 12\n" |
| 1486 | " h();\n" |
| 1487 | " b = 3; // line 14\n" |
| 1488 | " f(); // line 15\n" |
| 1489 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Compile the script and get the two functions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1492 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); |
| 1493 | v8::Local<v8::Script> script = |
| 1494 | v8::Script::Compile(context, source, &origin).ToLocalChecked(); |
| 1495 | script->Run(context).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1496 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1497 | env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1498 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1499 | env->Global()->Get(context, v8_str(isolate, "g")).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1500 | |
| 1501 | // Get the script id knowing that internally it is a 32 integer. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1502 | int script_id = script->GetUnboundScript()->GetId(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Call f and g without break points. |
| 1505 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1506 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1507 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1508 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1509 | CHECK_EQ(0, break_point_hit_count); |
| 1510 | |
| 1511 | // Call f and g with break point on line 12. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1512 | int sbp1 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1513 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1514 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1515 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1516 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1517 | CHECK_EQ(1, break_point_hit_count); |
| 1518 | |
| 1519 | // Remove the break point again. |
| 1520 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1521 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1522 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1523 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1524 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1525 | CHECK_EQ(0, break_point_hit_count); |
| 1526 | |
| 1527 | // Call f and g with break point on line 2. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1528 | int sbp2 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1529 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1530 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1532 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1533 | CHECK_EQ(2, break_point_hit_count); |
| 1534 | |
| 1535 | // Call f and g with break point on line 2, 4, 12, 14 and 15. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1536 | int sbp3 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 4, 0); |
| 1537 | int sbp4 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); |
| 1538 | int sbp5 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 14, 0); |
| 1539 | int sbp6 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 15, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1540 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1541 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1542 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1543 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1544 | CHECK_EQ(7, break_point_hit_count); |
| 1545 | |
| 1546 | // Remove all the break points again. |
| 1547 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1548 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 1549 | ClearBreakPointFromJS(env->GetIsolate(), sbp3); |
| 1550 | ClearBreakPointFromJS(env->GetIsolate(), sbp4); |
| 1551 | ClearBreakPointFromJS(env->GetIsolate(), sbp5); |
| 1552 | ClearBreakPointFromJS(env->GetIsolate(), sbp6); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1553 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1554 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1555 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1556 | CHECK_EQ(0, break_point_hit_count); |
| 1557 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1558 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 1559 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Make sure that the break point numbers are consecutive. |
| 1562 | CHECK_EQ(1, sbp1); |
| 1563 | CHECK_EQ(2, sbp2); |
| 1564 | CHECK_EQ(3, sbp3); |
| 1565 | CHECK_EQ(4, sbp4); |
| 1566 | CHECK_EQ(5, sbp5); |
| 1567 | CHECK_EQ(6, sbp6); |
| 1568 | } |
| 1569 | |
| 1570 | |
| 1571 | // Test conditional script break points. |
| 1572 | TEST(EnableDisableScriptBreakPoint) { |
| 1573 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1574 | DebugLocalContext env; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1575 | v8::Isolate* isolate = env->GetIsolate(); |
| 1576 | v8::HandleScope scope(isolate); |
| 1577 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1578 | env.ExposeDebug(); |
| 1579 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1580 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1581 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1582 | v8::Local<v8::String> script = v8_str(isolate, |
| 1583 | "function f() {\n" |
| 1584 | " a = 0; // line 1\n" |
| 1585 | "};"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Compile the script and get function f. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1588 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); |
| 1589 | v8::Script::Compile(context, script, &origin) |
| 1590 | .ToLocalChecked() |
| 1591 | ->Run(context) |
| 1592 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1593 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1594 | env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Set script break point on line 1 (in function f). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1597 | int sbp = SetScriptBreakPointByNameFromJS(isolate, "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1598 | |
| 1599 | // Call f while enabeling and disabling the script break point. |
| 1600 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1601 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1602 | CHECK_EQ(1, break_point_hit_count); |
| 1603 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1604 | DisableScriptBreakPointFromJS(isolate, sbp); |
| 1605 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1606 | CHECK_EQ(1, break_point_hit_count); |
| 1607 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1608 | EnableScriptBreakPointFromJS(isolate, sbp); |
| 1609 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1610 | CHECK_EQ(2, break_point_hit_count); |
| 1611 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1612 | DisableScriptBreakPointFromJS(isolate, sbp); |
| 1613 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1614 | CHECK_EQ(2, break_point_hit_count); |
| 1615 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1616 | // Reload the script and get f again checking that the disabling survives. |
| 1617 | v8::Script::Compile(context, script, &origin) |
| 1618 | .ToLocalChecked() |
| 1619 | ->Run(context) |
| 1620 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1621 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1622 | env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); |
| 1623 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1624 | CHECK_EQ(2, break_point_hit_count); |
| 1625 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1626 | EnableScriptBreakPointFromJS(isolate, sbp); |
| 1627 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1628 | CHECK_EQ(3, break_point_hit_count); |
| 1629 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1630 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 1631 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | // Test conditional script break points. |
| 1636 | TEST(ConditionalScriptBreakPoint) { |
| 1637 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1638 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1639 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1640 | env.ExposeDebug(); |
| 1641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1642 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1643 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1644 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1645 | v8::Local<v8::String> script = v8_str(env->GetIsolate(), |
| 1646 | "count = 0;\n" |
| 1647 | "function f() {\n" |
| 1648 | " g(count++); // line 2\n" |
| 1649 | "};\n" |
| 1650 | "function g(x) {\n" |
| 1651 | " var a=x; // line 5\n" |
| 1652 | "};"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Compile the script and get function f. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1655 | v8::Local<v8::Context> context = env.context(); |
| 1656 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(env->GetIsolate(), "test")); |
| 1657 | v8::Script::Compile(context, script, &origin) |
| 1658 | .ToLocalChecked() |
| 1659 | ->Run(context) |
| 1660 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1661 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1662 | env->Global() |
| 1663 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1664 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1665 | |
| 1666 | // Set script break point on line 5 (in function g). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1667 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 5, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Call f with different conditions on the script break point. |
| 1670 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1671 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "false"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1672 | f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1673 | CHECK_EQ(0, break_point_hit_count); |
| 1674 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1675 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "true"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1676 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1677 | f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1678 | CHECK_EQ(1, break_point_hit_count); |
| 1679 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1680 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "x % 2 == 0"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1681 | break_point_hit_count = 0; |
| 1682 | for (int i = 0; i < 10; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1683 | f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1684 | } |
| 1685 | CHECK_EQ(5, break_point_hit_count); |
| 1686 | |
| 1687 | // Reload the script and get f again checking that the condition survives. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1688 | v8::Script::Compile(context, script, &origin) |
| 1689 | .ToLocalChecked() |
| 1690 | ->Run(context) |
| 1691 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1692 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1693 | env->Global() |
| 1694 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1695 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1696 | |
| 1697 | break_point_hit_count = 0; |
| 1698 | for (int i = 0; i < 10; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1699 | f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1700 | } |
| 1701 | CHECK_EQ(5, break_point_hit_count); |
| 1702 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1703 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1704 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1708 | // Test that script break points survive when a script is reloaded. |
| 1709 | TEST(ScriptBreakPointReload) { |
| 1710 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1712 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1713 | env.ExposeDebug(); |
| 1714 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1715 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1716 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1717 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1718 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1719 | v8::Local<v8::Function> f; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1720 | v8::Local<v8::String> script = v8_str(env->GetIsolate(), |
| 1721 | "function f() {\n" |
| 1722 | " function h() {\n" |
| 1723 | " a = 0; // line 2\n" |
| 1724 | " }\n" |
| 1725 | " b = 1; // line 4\n" |
| 1726 | " return h();\n" |
| 1727 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1728 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1729 | v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8_str(env->GetIsolate(), "1")); |
| 1730 | v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8_str(env->GetIsolate(), "2")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Set a script break point before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1733 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "1", 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1734 | |
| 1735 | // Compile the script and get the function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1736 | v8::Script::Compile(context, script, &origin_1) |
| 1737 | .ToLocalChecked() |
| 1738 | ->Run(context) |
| 1739 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1740 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1741 | env->Global() |
| 1742 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1743 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Call f and check that the script break point is active. |
| 1746 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1747 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1748 | CHECK_EQ(1, break_point_hit_count); |
| 1749 | |
| 1750 | // Compile the script again with a different script data and get the |
| 1751 | // function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1752 | v8::Script::Compile(context, script, &origin_2) |
| 1753 | .ToLocalChecked() |
| 1754 | ->Run(context) |
| 1755 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1756 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1757 | env->Global() |
| 1758 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1759 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Call f and check that no break points are set. |
| 1762 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1763 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1764 | CHECK_EQ(0, break_point_hit_count); |
| 1765 | |
| 1766 | // Compile the script again and get the function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1767 | v8::Script::Compile(context, script, &origin_1) |
| 1768 | .ToLocalChecked() |
| 1769 | ->Run(context) |
| 1770 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1771 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1772 | env->Global() |
| 1773 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1774 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Call f and check that the script break point is active. |
| 1777 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1778 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1779 | CHECK_EQ(1, break_point_hit_count); |
| 1780 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1781 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1782 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | // Test when several scripts has the same script data |
| 1787 | TEST(ScriptBreakPointMultiple) { |
| 1788 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1789 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1790 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1791 | env.ExposeDebug(); |
| 1792 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1793 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1794 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1795 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1796 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1797 | v8::Local<v8::Function> f; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1798 | v8::Local<v8::String> script_f = v8_str(env->GetIsolate(), |
| 1799 | "function f() {\n" |
| 1800 | " a = 0; // line 1\n" |
| 1801 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1802 | |
| 1803 | v8::Local<v8::Function> g; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1804 | v8::Local<v8::String> script_g = v8_str(env->GetIsolate(), |
| 1805 | "function g() {\n" |
| 1806 | " b = 0; // line 1\n" |
| 1807 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1808 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1809 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1810 | |
| 1811 | // Set a script break point before the scripts are loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1812 | int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Compile the scripts with same script data and get the functions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1815 | v8::Script::Compile(context, script_f, &origin) |
| 1816 | .ToLocalChecked() |
| 1817 | ->Run(context) |
| 1818 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1819 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1820 | env->Global() |
| 1821 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1822 | .ToLocalChecked()); |
| 1823 | v8::Script::Compile(context, script_g, &origin) |
| 1824 | .ToLocalChecked() |
| 1825 | ->Run(context) |
| 1826 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1827 | g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1828 | env->Global() |
| 1829 | ->Get(context, v8_str(env->GetIsolate(), "g")) |
| 1830 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1831 | |
| 1832 | // Call f and g and check that the script break point is active. |
| 1833 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1834 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1835 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1836 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1837 | CHECK_EQ(2, break_point_hit_count); |
| 1838 | |
| 1839 | // Clear the script break point. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1840 | ClearBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Call f and g and check that the script break point is no longer active. |
| 1843 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1844 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1845 | CHECK_EQ(0, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1846 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1847 | CHECK_EQ(0, break_point_hit_count); |
| 1848 | |
| 1849 | // Set script break point with the scripts loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1850 | sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Call f and g and check that the script break point is active. |
| 1853 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1854 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1855 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1856 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1857 | CHECK_EQ(2, break_point_hit_count); |
| 1858 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1859 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1860 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | |
| 1864 | // Test the script origin which has both name and line offset. |
| 1865 | TEST(ScriptBreakPointLineOffset) { |
| 1866 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1867 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1868 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1869 | env.ExposeDebug(); |
| 1870 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1871 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1872 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1873 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1874 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1875 | v8::Local<v8::Function> f; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1876 | v8::Local<v8::String> script = |
| 1877 | v8_str(env->GetIsolate(), |
| 1878 | "function f() {\n" |
| 1879 | " a = 0; // line 8 as this script has line offset 7\n" |
| 1880 | " b = 0; // line 9 as this script has line offset 7\n" |
| 1881 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Create script origin both name and line offset. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1884 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), "test.html"), |
| 1885 | v8::Integer::New(env->GetIsolate(), 7)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1886 | |
| 1887 | // Set two script break points before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1888 | int sbp1 = |
| 1889 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 8, 0); |
| 1890 | int sbp2 = |
| 1891 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Compile the script and get the function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1894 | v8::Script::Compile(context, script, &origin) |
| 1895 | .ToLocalChecked() |
| 1896 | ->Run(context) |
| 1897 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1898 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1899 | env->Global() |
| 1900 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1901 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1902 | |
| 1903 | // Call f and check that the script break point is active. |
| 1904 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1905 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1906 | CHECK_EQ(2, break_point_hit_count); |
| 1907 | |
| 1908 | // Clear the script break points. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1909 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 1910 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Call f and check that no script break points are active. |
| 1913 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1914 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1915 | CHECK_EQ(0, break_point_hit_count); |
| 1916 | |
| 1917 | // Set a script break point with the script loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1918 | sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Call f and check that the script break point is active. |
| 1921 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1922 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1923 | CHECK_EQ(1, break_point_hit_count); |
| 1924 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1925 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 1926 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | // Test script break points set on lines. |
| 1931 | TEST(ScriptBreakPointLine) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1933 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1934 | env.ExposeDebug(); |
| 1935 | |
| 1936 | // Create a function for checking the function when hitting a break point. |
| 1937 | frame_function_name = CompileFunction(&env, |
| 1938 | frame_function_name_source, |
| 1939 | "frame_function_name"); |
| 1940 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1941 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 1942 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1944 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1945 | v8::Local<v8::Function> f; |
| 1946 | v8::Local<v8::Function> g; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1947 | v8::Local<v8::String> script = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1948 | v8_str(env->GetIsolate(), |
| 1949 | "a = 0 // line 0\n" |
| 1950 | "function f() {\n" |
| 1951 | " a = 1; // line 2\n" |
| 1952 | "}\n" |
| 1953 | " a = 2; // line 4\n" |
| 1954 | " /* xx */ function g() { // line 5\n" |
| 1955 | " function h() { // line 6\n" |
| 1956 | " a = 3; // line 7\n" |
| 1957 | " }\n" |
| 1958 | " h(); // line 9\n" |
| 1959 | " a = 4; // line 10\n" |
| 1960 | " }\n" |
| 1961 | " a=5; // line 12"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Set a couple script break point before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1964 | int sbp1 = |
| 1965 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 0, -1); |
| 1966 | int sbp2 = |
| 1967 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 1, -1); |
| 1968 | int sbp3 = |
| 1969 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 5, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1970 | |
| 1971 | // Compile the script and get the function. |
| 1972 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1973 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), "test.html"), |
| 1974 | v8::Integer::New(env->GetIsolate(), 0)); |
| 1975 | v8::Script::Compile(context, script, &origin) |
| 1976 | .ToLocalChecked() |
| 1977 | ->Run(context) |
| 1978 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1979 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1980 | env->Global() |
| 1981 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 1982 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1983 | g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1984 | env->Global() |
| 1985 | ->Get(context, v8_str(env->GetIsolate(), "g")) |
| 1986 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1987 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1988 | // Check that a break point was hit when the script was run. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1989 | CHECK_EQ(1, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1990 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1991 | |
| 1992 | // Call f and check that the script break point. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1993 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1994 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1995 | CHECK_EQ(0, strcmp("f", last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Call g and check that the script break point. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1998 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1999 | CHECK_EQ(3, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2000 | CHECK_EQ(0, strcmp("g", last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2001 | |
| 2002 | // Clear the script break point on g and set one on h. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2003 | ClearBreakPointFromJS(env->GetIsolate(), sbp3); |
| 2004 | int sbp4 = |
| 2005 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 6, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Call g and check that the script break point in h is hit. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2008 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2009 | CHECK_EQ(4, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2010 | CHECK_EQ(0, strcmp("h", last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Clear break points in f and h. Set a new one in the script between |
| 2013 | // functions f and g and test that there is no break points in f and g any |
| 2014 | // more. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2015 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 2016 | ClearBreakPointFromJS(env->GetIsolate(), sbp4); |
| 2017 | int sbp5 = |
| 2018 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 4, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2019 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2020 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 2021 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2022 | CHECK_EQ(0, break_point_hit_count); |
| 2023 | |
| 2024 | // Reload the script which should hit two break points. |
| 2025 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2026 | v8::Script::Compile(context, script, &origin) |
| 2027 | .ToLocalChecked() |
| 2028 | ->Run(context) |
| 2029 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2030 | CHECK_EQ(2, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2031 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2032 | |
| 2033 | // Set a break point in the code after the last function decleration. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2034 | int sbp6 = |
| 2035 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 12, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2036 | |
| 2037 | // Reload the script which should hit three break points. |
| 2038 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2039 | v8::Script::Compile(context, script, &origin) |
| 2040 | .ToLocalChecked() |
| 2041 | ->Run(context) |
| 2042 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2043 | CHECK_EQ(3, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2044 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Clear the last break points, and reload the script which should not hit any |
| 2047 | // break points. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2048 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 2049 | ClearBreakPointFromJS(env->GetIsolate(), sbp5); |
| 2050 | ClearBreakPointFromJS(env->GetIsolate(), sbp6); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2051 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2052 | v8::Script::Compile(context, script, &origin) |
| 2053 | .ToLocalChecked() |
| 2054 | ->Run(context) |
| 2055 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2056 | CHECK_EQ(0, break_point_hit_count); |
| 2057 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2058 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2059 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2063 | // Test top level script break points set on lines. |
| 2064 | TEST(ScriptBreakPointLineTopLevel) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2065 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2066 | v8::HandleScope scope(env->GetIsolate()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2067 | env.ExposeDebug(); |
| 2068 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2069 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2070 | DebugEventBreakPointHitCount); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2071 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2072 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2073 | v8::Local<v8::String> script = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2074 | v8_str(env->GetIsolate(), |
| 2075 | "function f() {\n" |
| 2076 | " a = 1; // line 1\n" |
| 2077 | "}\n" |
| 2078 | "a = 2; // line 3\n"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2079 | v8::Local<v8::Function> f; |
| 2080 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2081 | v8::HandleScope scope(env->GetIsolate()); |
| 2082 | CompileRunWithOrigin(script, "test.html"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2083 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2084 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2085 | env->Global() |
| 2086 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 2087 | .ToLocalChecked()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2088 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2089 | CcTest::heap()->CollectAllGarbage(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2090 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2091 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2092 | |
| 2093 | // Call f and check that there was no break points. |
| 2094 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2095 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2096 | CHECK_EQ(0, break_point_hit_count); |
| 2097 | |
| 2098 | // Recompile and run script and check that break point was hit. |
| 2099 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2100 | CompileRunWithOrigin(script, "test.html"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2101 | CHECK_EQ(1, break_point_hit_count); |
| 2102 | |
| 2103 | // Call f and check that there are still no break points. |
| 2104 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2105 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2106 | env->Global() |
| 2107 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 2108 | .ToLocalChecked()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2109 | CHECK_EQ(0, break_point_hit_count); |
| 2110 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2111 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2112 | CheckDebuggerUnloaded(env->GetIsolate()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2116 | // Test that it is possible to add and remove break points in a top level |
| 2117 | // function which has no references but has not been collected yet. |
| 2118 | TEST(ScriptBreakPointTopLevelCrash) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2119 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2120 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2121 | env.ExposeDebug(); |
| 2122 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2123 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2124 | DebugEventBreakPointHitCount); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2125 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2126 | v8::Local<v8::String> script_source = v8_str(env->GetIsolate(), |
| 2127 | "function f() {\n" |
| 2128 | " return 0;\n" |
| 2129 | "}\n" |
| 2130 | "f()"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2131 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2132 | int sbp1 = |
| 2133 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2134 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2135 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2136 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2137 | CompileRunWithOrigin(script_source, "test.html"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2138 | CHECK_EQ(1, break_point_hit_count); |
| 2139 | } |
| 2140 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2141 | int sbp2 = |
| 2142 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
| 2143 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 2144 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2145 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2146 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2147 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2151 | // Test that it is possible to remove the last break point for a function |
| 2152 | // inside the break handling of that break point. |
| 2153 | TEST(RemoveBreakPointInBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2154 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2155 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2156 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2157 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2158 | v8::Local<v8::Function> foo = |
| 2159 | CompileFunction(&env, "function foo(){a=1;}", "foo"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2160 | |
| 2161 | // Register the debug event listener pasing the function |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2162 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2163 | DebugEventRemoveBreakPoint, foo); |
| 2164 | |
| 2165 | debug_event_remove_break_point = SetBreakPoint(foo, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2166 | |
| 2167 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2168 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2169 | CHECK_EQ(1, break_point_hit_count); |
| 2170 | |
| 2171 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2172 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2173 | CHECK_EQ(0, break_point_hit_count); |
| 2174 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2175 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2176 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | // Test that the debugger statement causes a break. |
| 2181 | TEST(DebuggerStatement) { |
| 2182 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2183 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2184 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2185 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2186 | DebugEventBreakPointHitCount); |
| 2187 | v8::Local<v8::Context> context = env.context(); |
| 2188 | v8::Script::Compile(context, |
| 2189 | v8_str(env->GetIsolate(), "function bar(){debugger}")) |
| 2190 | .ToLocalChecked() |
| 2191 | ->Run(context) |
| 2192 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2193 | v8::Script::Compile( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2194 | context, v8_str(env->GetIsolate(), "function foo(){debugger;debugger;}")) |
| 2195 | .ToLocalChecked() |
| 2196 | ->Run(context) |
| 2197 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2198 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2199 | env->Global() |
| 2200 | ->Get(context, v8_str(env->GetIsolate(), "foo")) |
| 2201 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2202 | v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2203 | env->Global() |
| 2204 | ->Get(context, v8_str(env->GetIsolate(), "bar")) |
| 2205 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Run function with debugger statement |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2208 | bar->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2209 | CHECK_EQ(1, break_point_hit_count); |
| 2210 | |
| 2211 | // Run function with two debugger statement |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2212 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2213 | CHECK_EQ(3, break_point_hit_count); |
| 2214 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2215 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2216 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2220 | // Test setting a breakpoint on the debugger statement. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2221 | TEST(DebuggerStatementBreakpoint) { |
| 2222 | break_point_hit_count = 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2223 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2224 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2225 | v8::Local<v8::Context> context = env.context(); |
| 2226 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2227 | DebugEventBreakPointHitCount); |
| 2228 | v8::Script::Compile(context, |
| 2229 | v8_str(env->GetIsolate(), "function foo(){debugger;}")) |
| 2230 | .ToLocalChecked() |
| 2231 | ->Run(context) |
| 2232 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2233 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2234 | env->Global() |
| 2235 | ->Get(context, v8_str(env->GetIsolate(), "foo")) |
| 2236 | .ToLocalChecked()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2237 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2238 | // The debugger statement triggers breakpoint hit |
| 2239 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2240 | CHECK_EQ(1, break_point_hit_count); |
| 2241 | |
| 2242 | int bp = SetBreakPoint(foo, 0); |
| 2243 | |
| 2244 | // Set breakpoint does not duplicate hits |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2245 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2246 | CHECK_EQ(2, break_point_hit_count); |
| 2247 | |
| 2248 | ClearBreakPoint(bp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2249 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2250 | CheckDebuggerUnloaded(env->GetIsolate()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2254 | // Test that the evaluation of expressions when a break point is hit generates |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2255 | // the correct results. |
| 2256 | TEST(DebugEvaluate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2257 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2258 | v8::Isolate* isolate = env->GetIsolate(); |
| 2259 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2260 | env.ExposeDebug(); |
| 2261 | |
| 2262 | // Create a function for checking the evaluation when hitting a break point. |
| 2263 | evaluate_check_function = CompileFunction(&env, |
| 2264 | evaluate_check_source, |
| 2265 | "evaluate_check"); |
| 2266 | // Register the debug event listener |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2267 | v8::Debug::SetDebugEventListener(isolate, DebugEventEvaluate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2268 | |
| 2269 | // Different expected vaules of x and a when in a break point (u = undefined, |
| 2270 | // d = Hello, world!). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2271 | struct EvaluateCheck checks_uu[] = {{"x", v8::Undefined(isolate)}, |
| 2272 | {"a", v8::Undefined(isolate)}, |
| 2273 | {NULL, v8::Local<v8::Value>()}}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2274 | struct EvaluateCheck checks_hu[] = { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2275 | {"x", v8_str(env->GetIsolate(), "Hello, world!")}, |
| 2276 | {"a", v8::Undefined(isolate)}, |
| 2277 | {NULL, v8::Local<v8::Value>()}}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2278 | struct EvaluateCheck checks_hh[] = { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2279 | {"x", v8_str(env->GetIsolate(), "Hello, world!")}, |
| 2280 | {"a", v8_str(env->GetIsolate(), "Hello, world!")}, |
| 2281 | {NULL, v8::Local<v8::Value>()}}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2282 | |
| 2283 | // Simple test function. The "y=0" is in the function foo to provide a break |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2284 | // location. For "y=0" the "y" is at position 15 in the foo function |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2285 | // therefore setting breakpoint at position 15 will break at "y=0" and |
| 2286 | // setting it higher will break after. |
| 2287 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2288 | "function foo(x) {" |
| 2289 | " var a;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2290 | " y=0;" // To ensure break location 1. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2291 | " a=x;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2292 | " y=0;" // To ensure break location 2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2293 | "}", |
| 2294 | "foo"); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2295 | const int foo_break_position_1 = 15; |
| 2296 | const int foo_break_position_2 = 29; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2297 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2298 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2299 | // Arguments with one parameter "Hello, world!" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2300 | v8::Local<v8::Value> argv_foo[1] = { |
| 2301 | v8_str(env->GetIsolate(), "Hello, world!")}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2302 | |
| 2303 | // Call foo with breakpoint set before a=x and undefined as parameter. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2304 | int bp = SetBreakPoint(foo, foo_break_position_1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2305 | checks = checks_uu; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2306 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Call foo with breakpoint set before a=x and parameter "Hello, world!". |
| 2309 | checks = checks_hu; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2310 | foo->Call(context, env->Global(), 1, argv_foo).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2311 | |
| 2312 | // Call foo with breakpoint set after a=x and parameter "Hello, world!". |
| 2313 | ClearBreakPoint(bp); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2314 | SetBreakPoint(foo, foo_break_position_2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2315 | checks = checks_hh; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2316 | foo->Call(context, env->Global(), 1, argv_foo).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2317 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2318 | // Test that overriding Object.prototype will not interfere into evaluation |
| 2319 | // on call frame. |
| 2320 | v8::Local<v8::Function> zoo = |
| 2321 | CompileFunction(&env, |
| 2322 | "x = undefined;" |
| 2323 | "function zoo(t) {" |
| 2324 | " var a=x;" |
| 2325 | " Object.prototype.x = 42;" |
| 2326 | " x=t;" |
| 2327 | " y=0;" // To ensure break location. |
| 2328 | " delete Object.prototype.x;" |
| 2329 | " x=a;" |
| 2330 | "}", |
| 2331 | "zoo"); |
| 2332 | const int zoo_break_position = 50; |
| 2333 | |
| 2334 | // Arguments with one parameter "Hello, world!" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2335 | v8::Local<v8::Value> argv_zoo[1] = { |
| 2336 | v8_str(env->GetIsolate(), "Hello, world!")}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2337 | |
| 2338 | // Call zoo with breakpoint set at y=0. |
| 2339 | DebugEventCounterClear(); |
| 2340 | bp = SetBreakPoint(zoo, zoo_break_position); |
| 2341 | checks = checks_hu; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2342 | zoo->Call(context, env->Global(), 1, argv_zoo).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2343 | CHECK_EQ(1, break_point_hit_count); |
| 2344 | ClearBreakPoint(bp); |
| 2345 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2346 | // Test function with an inner function. The "y=0" is in function barbar |
| 2347 | // to provide a break location. For "y=0" the "y" is at position 8 in the |
| 2348 | // barbar function therefore setting breakpoint at position 8 will break at |
| 2349 | // "y=0" and setting it higher will break after. |
| 2350 | v8::Local<v8::Function> bar = CompileFunction(&env, |
| 2351 | "y = 0;" |
| 2352 | "x = 'Goodbye, world!';" |
| 2353 | "function bar(x, b) {" |
| 2354 | " var a;" |
| 2355 | " function barbar() {" |
| 2356 | " y=0; /* To ensure break location.*/" |
| 2357 | " a=x;" |
| 2358 | " };" |
| 2359 | " debug.Debug.clearAllBreakPoints();" |
| 2360 | " barbar();" |
| 2361 | " y=0;a=x;" |
| 2362 | "}", |
| 2363 | "bar"); |
| 2364 | const int barbar_break_position = 8; |
| 2365 | |
| 2366 | // Call bar setting breakpoint before a=x in barbar and undefined as |
| 2367 | // parameter. |
| 2368 | checks = checks_uu; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2369 | v8::Local<v8::Value> argv_bar_1[2] = { |
| 2370 | v8::Undefined(isolate), v8::Number::New(isolate, barbar_break_position)}; |
| 2371 | bar->Call(context, env->Global(), 2, argv_bar_1).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2372 | |
| 2373 | // Call bar setting breakpoint before a=x in barbar and parameter |
| 2374 | // "Hello, world!". |
| 2375 | checks = checks_hu; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2376 | v8::Local<v8::Value> argv_bar_2[2] = { |
| 2377 | v8_str(env->GetIsolate(), "Hello, world!"), |
| 2378 | v8::Number::New(env->GetIsolate(), barbar_break_position)}; |
| 2379 | bar->Call(context, env->Global(), 2, argv_bar_2).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2380 | |
| 2381 | // Call bar setting breakpoint after a=x in barbar and parameter |
| 2382 | // "Hello, world!". |
| 2383 | checks = checks_hh; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2384 | v8::Local<v8::Value> argv_bar_3[2] = { |
| 2385 | v8_str(env->GetIsolate(), "Hello, world!"), |
| 2386 | v8::Number::New(env->GetIsolate(), barbar_break_position + 1)}; |
| 2387 | bar->Call(context, env->Global(), 2, argv_bar_3).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2388 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2389 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 2390 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2393 | |
| 2394 | int debugEventCount = 0; |
| 2395 | static void CheckDebugEvent(const v8::Debug::EventDetails& eventDetails) { |
| 2396 | if (eventDetails.GetEvent() == v8::Break) ++debugEventCount; |
| 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | // Test that the conditional breakpoints work event if code generation from |
| 2401 | // strings is prohibited in the debugee context. |
| 2402 | TEST(ConditionalBreakpointWithCodeGenerationDisallowed) { |
| 2403 | DebugLocalContext env; |
| 2404 | v8::HandleScope scope(env->GetIsolate()); |
| 2405 | env.ExposeDebug(); |
| 2406 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2407 | v8::Debug::SetDebugEventListener(env->GetIsolate(), CheckDebugEvent); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2408 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2409 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2410 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2411 | "function foo(x) {\n" |
| 2412 | " var s = 'String value2';\n" |
| 2413 | " return s + x;\n" |
| 2414 | "}", |
| 2415 | "foo"); |
| 2416 | |
| 2417 | // Set conditional breakpoint with condition 'true'. |
| 2418 | CompileRun("debug.Debug.setBreakPoint(foo, 2, 0, 'true')"); |
| 2419 | |
| 2420 | debugEventCount = 0; |
| 2421 | env->AllowCodeGenerationFromStrings(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2422 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2423 | CHECK_EQ(1, debugEventCount); |
| 2424 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2425 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2426 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | |
| 2430 | bool checkedDebugEvals = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2431 | v8::Local<v8::Function> checkGlobalEvalFunction; |
| 2432 | v8::Local<v8::Function> checkFrameEvalFunction; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2433 | static void CheckDebugEval(const v8::Debug::EventDetails& eventDetails) { |
| 2434 | if (eventDetails.GetEvent() == v8::Break) { |
| 2435 | ++debugEventCount; |
| 2436 | v8::HandleScope handleScope(CcTest::isolate()); |
| 2437 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2438 | v8::Local<v8::Value> args[] = {eventDetails.GetExecutionState()}; |
| 2439 | CHECK( |
| 2440 | checkGlobalEvalFunction->Call(eventDetails.GetEventContext(), |
| 2441 | eventDetails.GetEventContext()->Global(), |
| 2442 | 1, args) |
| 2443 | .ToLocalChecked() |
| 2444 | ->IsTrue()); |
| 2445 | CHECK(checkFrameEvalFunction->Call(eventDetails.GetEventContext(), |
| 2446 | eventDetails.GetEventContext()->Global(), |
| 2447 | 1, args) |
| 2448 | .ToLocalChecked() |
| 2449 | ->IsTrue()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | // Test that the evaluation of expressions when a break point is hit generates |
| 2455 | // the correct results in case code generation from strings is disallowed in the |
| 2456 | // debugee context. |
| 2457 | TEST(DebugEvaluateWithCodeGenerationDisallowed) { |
| 2458 | DebugLocalContext env; |
| 2459 | v8::HandleScope scope(env->GetIsolate()); |
| 2460 | env.ExposeDebug(); |
| 2461 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2462 | v8::Debug::SetDebugEventListener(env->GetIsolate(), CheckDebugEval); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2463 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2464 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2465 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2466 | "var global = 'Global';\n" |
| 2467 | "function foo(x) {\n" |
| 2468 | " var local = 'Local';\n" |
| 2469 | " debugger;\n" |
| 2470 | " return local + x;\n" |
| 2471 | "}", |
| 2472 | "foo"); |
| 2473 | checkGlobalEvalFunction = CompileFunction(&env, |
| 2474 | "function checkGlobalEval(exec_state) {\n" |
| 2475 | " return exec_state.evaluateGlobal('global').value() === 'Global';\n" |
| 2476 | "}", |
| 2477 | "checkGlobalEval"); |
| 2478 | |
| 2479 | checkFrameEvalFunction = CompileFunction(&env, |
| 2480 | "function checkFrameEval(exec_state) {\n" |
| 2481 | " return exec_state.frame(0).evaluate('local').value() === 'Local';\n" |
| 2482 | "}", |
| 2483 | "checkFrameEval"); |
| 2484 | debugEventCount = 0; |
| 2485 | env->AllowCodeGenerationFromStrings(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2486 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2487 | CHECK_EQ(1, debugEventCount); |
| 2488 | |
| 2489 | checkGlobalEvalFunction.Clear(); |
| 2490 | checkFrameEvalFunction.Clear(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2491 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2492 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2496 | // Copies a C string to a 16-bit string. Does not check for buffer overflow. |
| 2497 | // Does not use the V8 engine to convert strings, so it can be used |
| 2498 | // in any thread. Returns the length of the string. |
| 2499 | int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) { |
| 2500 | int i; |
| 2501 | for (i = 0; input_buffer[i] != '\0'; ++i) { |
| 2502 | // ASCII does not use chars > 127, but be careful anyway. |
| 2503 | output_buffer[i] = static_cast<unsigned char>(input_buffer[i]); |
| 2504 | } |
| 2505 | output_buffer[i] = 0; |
| 2506 | return i; |
| 2507 | } |
| 2508 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2509 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2510 | // Copies a 16-bit string to a C string by dropping the high byte of |
| 2511 | // each character. Does not check for buffer overflow. |
| 2512 | // Can be used in any thread. Requires string length as an input. |
| 2513 | int Utf16ToAscii(const uint16_t* input_buffer, int length, |
| 2514 | char* output_buffer, int output_len = -1) { |
| 2515 | if (output_len >= 0) { |
| 2516 | if (length > output_len - 1) { |
| 2517 | length = output_len - 1; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | for (int i = 0; i < length; ++i) { |
| 2522 | output_buffer[i] = static_cast<char>(input_buffer[i]); |
| 2523 | } |
| 2524 | output_buffer[length] = '\0'; |
| 2525 | return length; |
| 2526 | } |
| 2527 | |
| 2528 | |
| 2529 | // We match parts of the message to get evaluate result int value. |
| 2530 | bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2531 | if (strstr(message, "\"command\":\"evaluate\"") == NULL) { |
| 2532 | return false; |
| 2533 | } |
| 2534 | const char* prefix = "\"text\":\""; |
| 2535 | char* pos1 = strstr(message, prefix); |
| 2536 | if (pos1 == NULL) { |
| 2537 | return false; |
| 2538 | } |
| 2539 | pos1 += strlen(prefix); |
| 2540 | char* pos2 = strchr(pos1, '"'); |
| 2541 | if (pos2 == NULL) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2542 | return false; |
| 2543 | } |
| 2544 | Vector<char> buf(buffer, buffer_size); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2545 | int len = static_cast<int>(pos2 - pos1); |
| 2546 | if (len > buffer_size - 1) { |
| 2547 | len = buffer_size - 1; |
| 2548 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2549 | StrNCpy(buf, pos1, len); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2550 | buffer[buffer_size - 1] = '\0'; |
| 2551 | return true; |
| 2552 | } |
| 2553 | |
| 2554 | |
| 2555 | struct EvaluateResult { |
| 2556 | static const int kBufferSize = 20; |
| 2557 | char buffer[kBufferSize]; |
| 2558 | }; |
| 2559 | |
| 2560 | struct DebugProcessDebugMessagesData { |
| 2561 | static const int kArraySize = 5; |
| 2562 | int counter; |
| 2563 | EvaluateResult results[kArraySize]; |
| 2564 | |
| 2565 | void reset() { |
| 2566 | counter = 0; |
| 2567 | } |
| 2568 | EvaluateResult* current() { |
| 2569 | return &results[counter % kArraySize]; |
| 2570 | } |
| 2571 | void next() { |
| 2572 | counter++; |
| 2573 | } |
| 2574 | }; |
| 2575 | |
| 2576 | DebugProcessDebugMessagesData process_debug_messages_data; |
| 2577 | |
| 2578 | static void DebugProcessDebugMessagesHandler( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2579 | const v8::Debug::Message& message) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2580 | v8::Local<v8::String> json = message.GetJSON(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2581 | v8::String::Utf8Value utf8(json); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2582 | EvaluateResult* array_item = process_debug_messages_data.current(); |
| 2583 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2584 | bool res = GetEvaluateStringResult(*utf8, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2585 | array_item->buffer, |
| 2586 | EvaluateResult::kBufferSize); |
| 2587 | if (res) { |
| 2588 | process_debug_messages_data.next(); |
| 2589 | } |
| 2590 | } |
| 2591 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2592 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2593 | // Test that the evaluation of expressions works even from ProcessDebugMessages |
| 2594 | // i.e. with empty stack. |
| 2595 | TEST(DebugEvaluateWithoutStack) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2596 | DebugLocalContext env; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2597 | v8::Debug::SetMessageHandler(env->GetIsolate(), |
| 2598 | DebugProcessDebugMessagesHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2599 | v8::HandleScope scope(env->GetIsolate()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2600 | |
| 2601 | const char* source = |
| 2602 | "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }"; |
| 2603 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2604 | v8::Local<v8::Context> context = env.context(); |
| 2605 | v8::Script::Compile(context, v8_str(env->GetIsolate(), source)) |
| 2606 | .ToLocalChecked() |
| 2607 | ->Run(context) |
| 2608 | .ToLocalChecked(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2609 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2610 | v8::Debug::ProcessDebugMessages(env->GetIsolate()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2611 | |
| 2612 | const int kBufferSize = 1000; |
| 2613 | uint16_t buffer[kBufferSize]; |
| 2614 | |
| 2615 | const char* command_111 = "{\"seq\":111," |
| 2616 | "\"type\":\"request\"," |
| 2617 | "\"command\":\"evaluate\"," |
| 2618 | "\"arguments\":{" |
| 2619 | " \"global\":true," |
| 2620 | " \"expression\":\"v1\",\"disable_break\":true" |
| 2621 | "}}"; |
| 2622 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2623 | v8::Isolate* isolate = CcTest::isolate(); |
| 2624 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_111, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2625 | |
| 2626 | const char* command_112 = "{\"seq\":112," |
| 2627 | "\"type\":\"request\"," |
| 2628 | "\"command\":\"evaluate\"," |
| 2629 | "\"arguments\":{" |
| 2630 | " \"global\":true," |
| 2631 | " \"expression\":\"getAnimal()\",\"disable_break\":true" |
| 2632 | "}}"; |
| 2633 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2634 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_112, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2635 | |
| 2636 | const char* command_113 = "{\"seq\":113," |
| 2637 | "\"type\":\"request\"," |
| 2638 | "\"command\":\"evaluate\"," |
| 2639 | "\"arguments\":{" |
| 2640 | " \"global\":true," |
| 2641 | " \"expression\":\"239 + 566\",\"disable_break\":true" |
| 2642 | "}}"; |
| 2643 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2644 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_113, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2645 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2646 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2647 | |
| 2648 | CHECK_EQ(3, process_debug_messages_data.counter); |
| 2649 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2650 | CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0); |
| 2651 | CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer), |
| 2652 | 0); |
| 2653 | CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2654 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2655 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 2656 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2657 | CheckDebuggerUnloaded(env->GetIsolate()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Simple test of the stepping mechanism using only store ICs. |
| 2662 | TEST(DebugStepLinear) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2663 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2664 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2665 | |
| 2666 | // Create a function for testing stepping. |
| 2667 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2668 | "function foo(){a=1;b=1;c=1;}", |
| 2669 | "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2670 | |
| 2671 | // Run foo to allow it to get optimized. |
| 2672 | CompileRun("a=0; b=0; c=0; foo();"); |
| 2673 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2674 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2675 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
| 2676 | |
| 2677 | SetBreakPoint(foo, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2678 | |
| 2679 | step_action = StepIn; |
| 2680 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2681 | v8::Local<v8::Context> context = env.context(); |
| 2682 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2683 | |
| 2684 | // With stepping all break locations are hit. |
| 2685 | CHECK_EQ(4, break_point_hit_count); |
| 2686 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2687 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2688 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2689 | |
| 2690 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2691 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2692 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2693 | |
| 2694 | SetBreakPoint(foo, 3); |
| 2695 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2696 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2697 | |
| 2698 | // Without stepping only active break points are hit. |
| 2699 | CHECK_EQ(1, break_point_hit_count); |
| 2700 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2701 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2702 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | // Test of the stepping mechanism for keyed load in a loop. |
| 2707 | TEST(DebugStepKeyedLoadLoop) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2708 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2709 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2710 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2711 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2712 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2713 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2714 | // Create a function for testing stepping of keyed load. The statement 'y=1' |
| 2715 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2716 | v8::Local<v8::Function> foo = CompileFunction( |
| 2717 | &env, |
| 2718 | "function foo(a) {\n" |
| 2719 | " var x;\n" |
| 2720 | " var len = a.length;\n" |
| 2721 | " for (var i = 0; i < len; i++) {\n" |
| 2722 | " y = 1;\n" |
| 2723 | " x = a[i];\n" |
| 2724 | " }\n" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2725 | "}\n" |
| 2726 | "y=0\n", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2727 | "foo"); |
| 2728 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2729 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2730 | // Create array [0,1,2,3,4,5,6,7,8,9] |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2731 | v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2732 | for (int i = 0; i < 10; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2733 | CHECK(a->Set(context, v8::Number::New(env->GetIsolate(), i), |
| 2734 | v8::Number::New(env->GetIsolate(), i)) |
| 2735 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | // Call function without any break points to ensure inlining is in place. |
| 2739 | const int kArgc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2740 | v8::Local<v8::Value> args[kArgc] = {a}; |
| 2741 | foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2742 | |
| 2743 | // Set up break point and step through the function. |
| 2744 | SetBreakPoint(foo, 3); |
| 2745 | step_action = StepNext; |
| 2746 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2747 | foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2748 | |
| 2749 | // With stepping all break locations are hit. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 2750 | CHECK_EQ(44, break_point_hit_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2751 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2752 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2753 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | |
| 2757 | // Test of the stepping mechanism for keyed store in a loop. |
| 2758 | TEST(DebugStepKeyedStoreLoop) { |
| 2759 | DebugLocalContext env; |
| 2760 | v8::HandleScope scope(env->GetIsolate()); |
| 2761 | |
| 2762 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2763 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Create a function for testing stepping of keyed store. The statement 'y=1' |
| 2766 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2767 | v8::Local<v8::Function> foo = CompileFunction( |
| 2768 | &env, |
| 2769 | "function foo(a) {\n" |
| 2770 | " var len = a.length;\n" |
| 2771 | " for (var i = 0; i < len; i++) {\n" |
| 2772 | " y = 1;\n" |
| 2773 | " a[i] = 42;\n" |
| 2774 | " }\n" |
| 2775 | "}\n" |
| 2776 | "y=0\n", |
| 2777 | "foo"); |
| 2778 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2779 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2780 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2781 | v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); |
| 2782 | for (int i = 0; i < 10; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2783 | CHECK(a->Set(context, v8::Number::New(env->GetIsolate(), i), |
| 2784 | v8::Number::New(env->GetIsolate(), i)) |
| 2785 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | // Call function without any break points to ensure inlining is in place. |
| 2789 | const int kArgc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2790 | v8::Local<v8::Value> args[kArgc] = {a}; |
| 2791 | foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2792 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2793 | // Set up break point and step through the function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2794 | SetBreakPoint(foo, 3); |
| 2795 | step_action = StepNext; |
| 2796 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2797 | foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2798 | |
| 2799 | // With stepping all break locations are hit. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2800 | CHECK_EQ(44, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2801 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2802 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2803 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2807 | // Test of the stepping mechanism for named load in a loop. |
| 2808 | TEST(DebugStepNamedLoadLoop) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2809 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2810 | v8::HandleScope scope(env->GetIsolate()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2811 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2812 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2813 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2814 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2815 | v8::Local<v8::Context> context = env.context(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2816 | // Create a function for testing stepping of named load. |
| 2817 | v8::Local<v8::Function> foo = CompileFunction( |
| 2818 | &env, |
| 2819 | "function foo() {\n" |
| 2820 | " var a = [];\n" |
| 2821 | " var s = \"\";\n" |
| 2822 | " for (var i = 0; i < 10; i++) {\n" |
| 2823 | " var v = new V(i, i + 1);\n" |
| 2824 | " v.y;\n" |
| 2825 | " a.length;\n" // Special case: array length. |
| 2826 | " s.length;\n" // Special case: string length. |
| 2827 | " }\n" |
| 2828 | "}\n" |
| 2829 | "function V(x, y) {\n" |
| 2830 | " this.x = x;\n" |
| 2831 | " this.y = y;\n" |
| 2832 | "}\n", |
| 2833 | "foo"); |
| 2834 | |
| 2835 | // Call function without any break points to ensure inlining is in place. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2836 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2837 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2838 | // Set up break point and step through the function. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2839 | SetBreakPoint(foo, 4); |
| 2840 | step_action = StepNext; |
| 2841 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2842 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2843 | |
| 2844 | // With stepping all break locations are hit. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2845 | CHECK_EQ(65, break_point_hit_count); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2846 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2847 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2848 | CheckDebuggerUnloaded(env->GetIsolate()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2852 | static void DoDebugStepNamedStoreLoop(int expected) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2853 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2854 | v8::HandleScope scope(env->GetIsolate()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2855 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2856 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2857 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2858 | |
| 2859 | // Create a function for testing stepping of named store. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2860 | v8::Local<v8::Context> context = env.context(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2861 | v8::Local<v8::Function> foo = CompileFunction( |
| 2862 | &env, |
| 2863 | "function foo() {\n" |
| 2864 | " var a = {a:1};\n" |
| 2865 | " for (var i = 0; i < 10; i++) {\n" |
| 2866 | " a.a = 2\n" |
| 2867 | " }\n" |
| 2868 | "}\n", |
| 2869 | "foo"); |
| 2870 | |
| 2871 | // Call function without any break points to ensure inlining is in place. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2872 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2873 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2874 | // Set up break point and step through the function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2875 | SetBreakPoint(foo, 3); |
| 2876 | step_action = StepNext; |
| 2877 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2878 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2879 | |
| 2880 | // With stepping all expected break locations are hit. |
| 2881 | CHECK_EQ(expected, break_point_hit_count); |
| 2882 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2883 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2884 | CheckDebuggerUnloaded(env->GetIsolate()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | // Test of the stepping mechanism for named load in a loop. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2889 | TEST(DebugStepNamedStoreLoop) { DoDebugStepNamedStoreLoop(34); } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2890 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2891 | // Test the stepping mechanism with different ICs. |
| 2892 | TEST(DebugStepLinearMixedICs) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2893 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2894 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2895 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2896 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2897 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2898 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2899 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2900 | // Create a function for testing stepping. |
| 2901 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2902 | "function bar() {};" |
| 2903 | "function foo() {" |
| 2904 | " var x;" |
| 2905 | " var index='name';" |
| 2906 | " var y = {};" |
| 2907 | " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2908 | |
| 2909 | // Run functions to allow them to get optimized. |
| 2910 | CompileRun("a=0; b=0; bar(); foo();"); |
| 2911 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2912 | SetBreakPoint(foo, 0); |
| 2913 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2914 | step_action = StepIn; |
| 2915 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2916 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2917 | |
| 2918 | // With stepping all break locations are hit. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 2919 | CHECK_EQ(10, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2920 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2921 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2922 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2923 | |
| 2924 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2925 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2926 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2927 | |
| 2928 | SetBreakPoint(foo, 0); |
| 2929 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2930 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2931 | |
| 2932 | // Without stepping only active break points are hit. |
| 2933 | CHECK_EQ(1, break_point_hit_count); |
| 2934 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2935 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2936 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2940 | TEST(DebugStepDeclarations) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2941 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2942 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2943 | |
| 2944 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2945 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2946 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2947 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2948 | // Create a function for testing stepping. Run it to allow it to get |
| 2949 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2950 | const char* src = "function foo() { " |
| 2951 | " var a;" |
| 2952 | " var b = 1;" |
| 2953 | " var c = foo;" |
| 2954 | " var d = Math.floor;" |
| 2955 | " var e = b + d(1.2);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2956 | "}" |
| 2957 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2958 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2959 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2960 | SetBreakPoint(foo, 0); |
| 2961 | |
| 2962 | // Stepping through the declarations. |
| 2963 | step_action = StepIn; |
| 2964 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2965 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 2966 | CHECK_EQ(5, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2967 | |
| 2968 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2969 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2970 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | TEST(DebugStepLocals) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2975 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2976 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2977 | |
| 2978 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2979 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2980 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2981 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2982 | // Create a function for testing stepping. Run it to allow it to get |
| 2983 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2984 | const char* src = "function foo() { " |
| 2985 | " var a,b;" |
| 2986 | " a = 1;" |
| 2987 | " b = a + 2;" |
| 2988 | " b = 1 + 2 + 3;" |
| 2989 | " a = Math.floor(b);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2990 | "}" |
| 2991 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2992 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2993 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2994 | SetBreakPoint(foo, 0); |
| 2995 | |
| 2996 | // Stepping through the declarations. |
| 2997 | step_action = StepIn; |
| 2998 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2999 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 3000 | CHECK_EQ(5, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3001 | |
| 3002 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3003 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3004 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3008 | TEST(DebugStepIf) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3009 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3010 | v8::Isolate* isolate = env->GetIsolate(); |
| 3011 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3012 | |
| 3013 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3014 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3015 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3016 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3017 | // Create a function for testing stepping. Run it to allow it to get |
| 3018 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3019 | const int argc = 1; |
| 3020 | const char* src = "function foo(x) { " |
| 3021 | " a = 1;" |
| 3022 | " if (x) {" |
| 3023 | " b = 1;" |
| 3024 | " } else {" |
| 3025 | " c = 1;" |
| 3026 | " d = 1;" |
| 3027 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3028 | "}" |
| 3029 | "a=0; b=0; c=0; d=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3030 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3031 | SetBreakPoint(foo, 0); |
| 3032 | |
| 3033 | // Stepping through the true part. |
| 3034 | step_action = StepIn; |
| 3035 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3036 | v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; |
| 3037 | foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3038 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3039 | |
| 3040 | // Stepping through the false part. |
| 3041 | step_action = StepIn; |
| 3042 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3043 | v8::Local<v8::Value> argv_false[argc] = {v8::False(isolate)}; |
| 3044 | foo->Call(context, env->Global(), argc, argv_false).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3045 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3046 | |
| 3047 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3048 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3049 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | |
| 3053 | TEST(DebugStepSwitch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3054 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3055 | v8::Isolate* isolate = env->GetIsolate(); |
| 3056 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3057 | |
| 3058 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3059 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3060 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3061 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3062 | // Create a function for testing stepping. Run it to allow it to get |
| 3063 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3064 | const int argc = 1; |
| 3065 | const char* src = "function foo(x) { " |
| 3066 | " a = 1;" |
| 3067 | " switch (x) {" |
| 3068 | " case 1:" |
| 3069 | " b = 1;" |
| 3070 | " case 2:" |
| 3071 | " c = 1;" |
| 3072 | " break;" |
| 3073 | " case 3:" |
| 3074 | " d = 1;" |
| 3075 | " e = 1;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3076 | " f = 1;" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3077 | " break;" |
| 3078 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3079 | "}" |
| 3080 | "a=0; b=0; c=0; d=0; e=0; f=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3081 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3082 | SetBreakPoint(foo, 0); |
| 3083 | |
| 3084 | // One case with fall-through. |
| 3085 | step_action = StepIn; |
| 3086 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3087 | v8::Local<v8::Value> argv_1[argc] = {v8::Number::New(isolate, 1)}; |
| 3088 | foo->Call(context, env->Global(), argc, argv_1).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3089 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3090 | |
| 3091 | // Another case. |
| 3092 | step_action = StepIn; |
| 3093 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3094 | v8::Local<v8::Value> argv_2[argc] = {v8::Number::New(isolate, 2)}; |
| 3095 | foo->Call(context, env->Global(), argc, argv_2).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3096 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Last case. |
| 3099 | step_action = StepIn; |
| 3100 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3101 | v8::Local<v8::Value> argv_3[argc] = {v8::Number::New(isolate, 3)}; |
| 3102 | foo->Call(context, env->Global(), argc, argv_3).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3103 | CHECK_EQ(7, break_point_hit_count); |
| 3104 | |
| 3105 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3106 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3107 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | |
| 3111 | TEST(DebugStepWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3112 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3113 | v8::Isolate* isolate = env->GetIsolate(); |
| 3114 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3115 | |
| 3116 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3117 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3118 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3119 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3120 | // Create a function for testing stepping. Run it to allow it to get |
| 3121 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3122 | const int argc = 1; |
| 3123 | const char* src = "function foo(x) { " |
| 3124 | " var a = 0;" |
| 3125 | " while (a < x) {" |
| 3126 | " a++;" |
| 3127 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3128 | "}" |
| 3129 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3130 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3131 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3132 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3133 | // Looping 0 times. We still should break at the while-condition once. |
| 3134 | step_action = StepIn; |
| 3135 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3136 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3137 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3138 | CHECK_EQ(3, break_point_hit_count); |
| 3139 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3140 | // Looping 10 times. |
| 3141 | step_action = StepIn; |
| 3142 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3143 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3144 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3145 | CHECK_EQ(23, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3146 | |
| 3147 | // Looping 100 times. |
| 3148 | step_action = StepIn; |
| 3149 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3150 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3151 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3152 | CHECK_EQ(203, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3153 | |
| 3154 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3155 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3156 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | TEST(DebugStepDoWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3161 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3162 | v8::Isolate* isolate = env->GetIsolate(); |
| 3163 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3164 | |
| 3165 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3166 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3167 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3168 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3169 | // Create a function for testing stepping. Run it to allow it to get |
| 3170 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3171 | const int argc = 1; |
| 3172 | const char* src = "function foo(x) { " |
| 3173 | " var a = 0;" |
| 3174 | " do {" |
| 3175 | " a++;" |
| 3176 | " } while (a < x)" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3177 | "}" |
| 3178 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3179 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3180 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3181 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3182 | // Looping 0 times. |
| 3183 | step_action = StepIn; |
| 3184 | break_point_hit_count = 0; |
| 3185 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3186 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
| 3187 | CHECK_EQ(4, break_point_hit_count); |
| 3188 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3189 | // Looping 10 times. |
| 3190 | step_action = StepIn; |
| 3191 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3192 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3193 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3194 | CHECK_EQ(22, break_point_hit_count); |
| 3195 | |
| 3196 | // Looping 100 times. |
| 3197 | step_action = StepIn; |
| 3198 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3199 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3200 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3201 | CHECK_EQ(202, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3202 | |
| 3203 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3204 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3205 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | |
| 3209 | TEST(DebugStepFor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3210 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3211 | v8::Isolate* isolate = env->GetIsolate(); |
| 3212 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3213 | |
| 3214 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3215 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3216 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3217 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3218 | // Create a function for testing stepping. Run it to allow it to get |
| 3219 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3220 | const int argc = 1; |
| 3221 | const char* src = "function foo(x) { " |
| 3222 | " a = 1;" |
| 3223 | " for (i = 0; i < x; i++) {" |
| 3224 | " b = 1;" |
| 3225 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3226 | "}" |
| 3227 | "a=0; b=0; i=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3228 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3229 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3230 | SetBreakPoint(foo, 8); // "a = 1;" |
| 3231 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3232 | // Looping 0 times. |
| 3233 | step_action = StepIn; |
| 3234 | break_point_hit_count = 0; |
| 3235 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3236 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
| 3237 | CHECK_EQ(4, break_point_hit_count); |
| 3238 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3239 | // Looping 10 times. |
| 3240 | step_action = StepIn; |
| 3241 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3242 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3243 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3244 | CHECK_EQ(34, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Looping 100 times. |
| 3247 | step_action = StepIn; |
| 3248 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3249 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3250 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3251 | CHECK_EQ(304, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3252 | |
| 3253 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3254 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3255 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3259 | TEST(DebugStepForContinue) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3260 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3261 | v8::Isolate* isolate = env->GetIsolate(); |
| 3262 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3263 | |
| 3264 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3265 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3266 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3267 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3268 | // Create a function for testing stepping. Run it to allow it to get |
| 3269 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3270 | const int argc = 1; |
| 3271 | const char* src = "function foo(x) { " |
| 3272 | " var a = 0;" |
| 3273 | " var b = 0;" |
| 3274 | " var c = 0;" |
| 3275 | " for (var i = 0; i < x; i++) {" |
| 3276 | " a++;" |
| 3277 | " if (a % 2 == 0) continue;" |
| 3278 | " b++;" |
| 3279 | " c++;" |
| 3280 | " }" |
| 3281 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3282 | "}" |
| 3283 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3284 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3285 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3286 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3287 | |
| 3288 | // Each loop generates 4 or 5 steps depending on whether a is equal. |
| 3289 | |
| 3290 | // Looping 10 times. |
| 3291 | step_action = StepIn; |
| 3292 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3293 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3294 | result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3295 | CHECK_EQ(5, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3296 | CHECK_EQ(62, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3297 | |
| 3298 | // Looping 100 times. |
| 3299 | step_action = StepIn; |
| 3300 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3301 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3302 | result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3303 | CHECK_EQ(50, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3304 | CHECK_EQ(557, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3305 | |
| 3306 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3307 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3308 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | |
| 3312 | TEST(DebugStepForBreak) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3313 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3314 | v8::Isolate* isolate = env->GetIsolate(); |
| 3315 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3316 | |
| 3317 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3318 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3319 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3320 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3321 | // Create a function for testing stepping. Run it to allow it to get |
| 3322 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3323 | const int argc = 1; |
| 3324 | const char* src = "function foo(x) { " |
| 3325 | " var a = 0;" |
| 3326 | " var b = 0;" |
| 3327 | " var c = 0;" |
| 3328 | " for (var i = 0; i < 1000; i++) {" |
| 3329 | " a++;" |
| 3330 | " if (a == x) break;" |
| 3331 | " b++;" |
| 3332 | " c++;" |
| 3333 | " }" |
| 3334 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3335 | "}" |
| 3336 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3337 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3338 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3339 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3340 | |
| 3341 | // Each loop generates 5 steps except for the last (when break is executed) |
| 3342 | // which only generates 4. |
| 3343 | |
| 3344 | // Looping 10 times. |
| 3345 | step_action = StepIn; |
| 3346 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3347 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3348 | result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3349 | CHECK_EQ(9, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3350 | CHECK_EQ(64, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3351 | |
| 3352 | // Looping 100 times. |
| 3353 | step_action = StepIn; |
| 3354 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3355 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3356 | result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3357 | CHECK_EQ(99, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3358 | CHECK_EQ(604, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3359 | |
| 3360 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3361 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3362 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | |
| 3366 | TEST(DebugStepForIn) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3367 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3368 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3369 | |
| 3370 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3371 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3372 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3373 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3374 | // Create a function for testing stepping. Run it to allow it to get |
| 3375 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3376 | v8::Local<v8::Function> foo; |
| 3377 | const char* src_1 = "function foo() { " |
| 3378 | " var a = [1, 2];" |
| 3379 | " for (x in a) {" |
| 3380 | " b = 0;" |
| 3381 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3382 | "}" |
| 3383 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3384 | foo = CompileFunction(&env, src_1, "foo"); |
| 3385 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3386 | |
| 3387 | step_action = StepIn; |
| 3388 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3389 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3390 | CHECK_EQ(8, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3391 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3392 | // Create a function for testing stepping. Run it to allow it to get |
| 3393 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3394 | const char* src_2 = "function foo() { " |
| 3395 | " var a = {a:[1, 2, 3]};" |
| 3396 | " for (x in a.a) {" |
| 3397 | " b = 0;" |
| 3398 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3399 | "}" |
| 3400 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3401 | foo = CompileFunction(&env, src_2, "foo"); |
| 3402 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3403 | |
| 3404 | step_action = StepIn; |
| 3405 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3406 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3407 | CHECK_EQ(10, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3408 | |
| 3409 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3410 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3411 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | |
| 3415 | TEST(DebugStepWith) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3416 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3417 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3418 | |
| 3419 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3420 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3421 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3422 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3423 | // Create a function for testing stepping. Run it to allow it to get |
| 3424 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3425 | const char* src = "function foo(x) { " |
| 3426 | " var a = {};" |
| 3427 | " with (a) {}" |
| 3428 | " with (b) {}" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3429 | "}" |
| 3430 | "foo()"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3431 | CHECK(env->Global() |
| 3432 | ->Set(context, v8_str(env->GetIsolate(), "b"), |
| 3433 | v8::Object::New(env->GetIsolate())) |
| 3434 | .FromJust()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3435 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3436 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3437 | SetBreakPoint(foo, 8); // "var a = {};" |
| 3438 | |
| 3439 | step_action = StepIn; |
| 3440 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3441 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3442 | CHECK_EQ(4, break_point_hit_count); |
| 3443 | |
| 3444 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3445 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3446 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | |
| 3450 | TEST(DebugConditional) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3451 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3452 | v8::Isolate* isolate = env->GetIsolate(); |
| 3453 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3454 | |
| 3455 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3456 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3458 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3459 | // Create a function for testing stepping. Run it to allow it to get |
| 3460 | // optimized. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 3461 | const char* src = |
| 3462 | "function foo(x) { " |
| 3463 | " return x ? 1 : 2;" |
| 3464 | "}" |
| 3465 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3466 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3467 | SetBreakPoint(foo, 0); // "var a;" |
| 3468 | |
| 3469 | step_action = StepIn; |
| 3470 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3471 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 3472 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3473 | |
| 3474 | step_action = StepIn; |
| 3475 | break_point_hit_count = 0; |
| 3476 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3477 | v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; |
| 3478 | foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 3479 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3480 | |
| 3481 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3482 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3483 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3487 | TEST(StepInOutSimple) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3488 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3489 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3490 | |
| 3491 | // Create a function for checking the function when hitting a break point. |
| 3492 | frame_function_name = CompileFunction(&env, |
| 3493 | frame_function_name_source, |
| 3494 | "frame_function_name"); |
| 3495 | |
| 3496 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3497 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3498 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3499 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3500 | // Create a function for testing stepping. Run it to allow it to get |
| 3501 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3502 | const char* src = "function a() {b();c();}; " |
| 3503 | "function b() {c();}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3504 | "function c() {}; " |
| 3505 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3506 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3507 | SetBreakPoint(a, 0); |
| 3508 | |
| 3509 | // Step through invocation of a with step in. |
| 3510 | step_action = StepIn; |
| 3511 | break_point_hit_count = 0; |
| 3512 | expected_step_sequence = "abcbaca"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3513 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3514 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3515 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Step through invocation of a with step next. |
| 3518 | step_action = StepNext; |
| 3519 | break_point_hit_count = 0; |
| 3520 | expected_step_sequence = "aaa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3521 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3522 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3523 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3524 | |
| 3525 | // Step through invocation of a with step out. |
| 3526 | step_action = StepOut; |
| 3527 | break_point_hit_count = 0; |
| 3528 | expected_step_sequence = "a"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3529 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3530 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3531 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3532 | |
| 3533 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3534 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3535 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | |
| 3539 | TEST(StepInOutTree) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3540 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3541 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3542 | |
| 3543 | // Create a function for checking the function when hitting a break point. |
| 3544 | frame_function_name = CompileFunction(&env, |
| 3545 | frame_function_name_source, |
| 3546 | "frame_function_name"); |
| 3547 | |
| 3548 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3549 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3550 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3551 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3552 | // Create a function for testing stepping. Run it to allow it to get |
| 3553 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3554 | const char* src = "function a() {b(c(d()),d());c(d());d()}; " |
| 3555 | "function b(x,y) {c();}; " |
| 3556 | "function c(x) {}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3557 | "function d() {}; " |
| 3558 | "a(); b(); c(); d()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3559 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3560 | SetBreakPoint(a, 0); |
| 3561 | |
| 3562 | // Step through invocation of a with step in. |
| 3563 | step_action = StepIn; |
| 3564 | break_point_hit_count = 0; |
| 3565 | expected_step_sequence = "adacadabcbadacada"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3566 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3567 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3568 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3569 | |
| 3570 | // Step through invocation of a with step next. |
| 3571 | step_action = StepNext; |
| 3572 | break_point_hit_count = 0; |
| 3573 | expected_step_sequence = "aaaa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3574 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3575 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3576 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3577 | |
| 3578 | // Step through invocation of a with step out. |
| 3579 | step_action = StepOut; |
| 3580 | break_point_hit_count = 0; |
| 3581 | expected_step_sequence = "a"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3582 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3583 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3584 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3585 | |
| 3586 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3587 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3588 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | |
| 3592 | TEST(StepInOutBranch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3593 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3594 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3595 | |
| 3596 | // Create a function for checking the function when hitting a break point. |
| 3597 | frame_function_name = CompileFunction(&env, |
| 3598 | frame_function_name_source, |
| 3599 | "frame_function_name"); |
| 3600 | |
| 3601 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3602 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3603 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3604 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3605 | // Create a function for testing stepping. Run it to allow it to get |
| 3606 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3607 | const char* src = "function a() {b(false);c();}; " |
| 3608 | "function b(x) {if(x){c();};}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3609 | "function c() {}; " |
| 3610 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3611 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3612 | SetBreakPoint(a, 0); |
| 3613 | |
| 3614 | // Step through invocation of a. |
| 3615 | step_action = StepIn; |
| 3616 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3617 | expected_step_sequence = "abbaca"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3618 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3619 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3620 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3621 | |
| 3622 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3623 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3624 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | |
| 3628 | // Test that step in does not step into native functions. |
| 3629 | TEST(DebugStepNatives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3630 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3631 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3632 | |
| 3633 | // Create a function for testing stepping. |
| 3634 | v8::Local<v8::Function> foo = CompileFunction( |
| 3635 | &env, |
| 3636 | "function foo(){debugger;Math.sin(1);}", |
| 3637 | "foo"); |
| 3638 | |
| 3639 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3640 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3642 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3643 | step_action = StepIn; |
| 3644 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3645 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3646 | |
| 3647 | // With stepping all break locations are hit. |
| 3648 | CHECK_EQ(3, break_point_hit_count); |
| 3649 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3650 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3651 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3652 | |
| 3653 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3654 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 3655 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3656 | |
| 3657 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3658 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3659 | |
| 3660 | // Without stepping only active break points are hit. |
| 3661 | CHECK_EQ(1, break_point_hit_count); |
| 3662 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3663 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3664 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | // Test that step in works with function.apply. |
| 3669 | TEST(DebugStepFunctionApply) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3670 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3671 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3672 | |
| 3673 | // Create a function for testing stepping. |
| 3674 | v8::Local<v8::Function> foo = CompileFunction( |
| 3675 | &env, |
| 3676 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3677 | "function foo(){ debugger; bar.apply(this, [1,2,3]); }", |
| 3678 | "foo"); |
| 3679 | |
| 3680 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3681 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3682 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3683 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3684 | step_action = StepIn; |
| 3685 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3686 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3687 | |
| 3688 | // With stepping all break locations are hit. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3689 | CHECK_EQ(7, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3690 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3691 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3692 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3693 | |
| 3694 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3695 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 3696 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3697 | |
| 3698 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3699 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3700 | |
| 3701 | // Without stepping only the debugger statement is hit. |
| 3702 | CHECK_EQ(1, break_point_hit_count); |
| 3703 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3704 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3705 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | |
| 3709 | // Test that step in works with function.call. |
| 3710 | TEST(DebugStepFunctionCall) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3711 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3712 | v8::Isolate* isolate = env->GetIsolate(); |
| 3713 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3714 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3715 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3716 | // Create a function for testing stepping. |
| 3717 | v8::Local<v8::Function> foo = CompileFunction( |
| 3718 | &env, |
| 3719 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3720 | "function foo(a){ debugger;" |
| 3721 | " if (a) {" |
| 3722 | " bar.call(this, 1, 2, 3);" |
| 3723 | " } else {" |
| 3724 | " bar.call(this, 0);" |
| 3725 | " }" |
| 3726 | "}", |
| 3727 | "foo"); |
| 3728 | |
| 3729 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3730 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3731 | step_action = StepIn; |
| 3732 | |
| 3733 | // Check stepping where the if condition in bar is false. |
| 3734 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3735 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3736 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3737 | |
| 3738 | // Check stepping where the if condition in bar is true. |
| 3739 | break_point_hit_count = 0; |
| 3740 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3741 | v8::Local<v8::Value> argv[argc] = {v8::True(isolate)}; |
| 3742 | foo->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3743 | CHECK_EQ(8, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3744 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3745 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3746 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3747 | |
| 3748 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3749 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3750 | |
| 3751 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3752 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3753 | |
| 3754 | // Without stepping only the debugger statement is hit. |
| 3755 | CHECK_EQ(1, break_point_hit_count); |
| 3756 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3757 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3758 | CheckDebuggerUnloaded(isolate); |
| 3759 | } |
| 3760 | |
| 3761 | |
| 3762 | // Test that step in works with Function.call.apply. |
| 3763 | TEST(DebugStepFunctionCallApply) { |
| 3764 | DebugLocalContext env; |
| 3765 | v8::Isolate* isolate = env->GetIsolate(); |
| 3766 | v8::HandleScope scope(isolate); |
| 3767 | |
| 3768 | v8::Local<v8::Context> context = env.context(); |
| 3769 | // Create a function for testing stepping. |
| 3770 | v8::Local<v8::Function> foo = |
| 3771 | CompileFunction(&env, |
| 3772 | "function bar() { }" |
| 3773 | "function foo(){ debugger;" |
| 3774 | " Function.call.apply(bar);" |
| 3775 | " Function.call.apply(Function.call, " |
| 3776 | "[Function.call, bar]);" |
| 3777 | "}", |
| 3778 | "foo"); |
| 3779 | |
| 3780 | // Register a debug event listener which steps and counts. |
| 3781 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
| 3782 | step_action = StepIn; |
| 3783 | |
| 3784 | break_point_hit_count = 0; |
| 3785 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3786 | CHECK_EQ(6, break_point_hit_count); |
| 3787 | |
| 3788 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3789 | CheckDebuggerUnloaded(isolate); |
| 3790 | |
| 3791 | // Register a debug event listener which just counts. |
| 3792 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
| 3793 | |
| 3794 | break_point_hit_count = 0; |
| 3795 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3796 | |
| 3797 | // Without stepping only the debugger statement is hit. |
| 3798 | CHECK_EQ(1, break_point_hit_count); |
| 3799 | |
| 3800 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3801 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3805 | // Tests that breakpoint will be hit if it's set in script. |
| 3806 | TEST(PauseInScript) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3807 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3808 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3809 | env.ExposeDebug(); |
| 3810 | |
| 3811 | // Register a debug event listener which counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3812 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3813 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3814 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3815 | // Create a script that returns a function. |
| 3816 | const char* src = "(function (evt) {})"; |
| 3817 | const char* script_name = "StepInHandlerTest"; |
| 3818 | |
| 3819 | // Set breakpoint in the script. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3820 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), script_name, 0, -1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3821 | break_point_hit_count = 0; |
| 3822 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3823 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), script_name), |
| 3824 | v8::Integer::New(env->GetIsolate(), 0)); |
| 3825 | v8::Local<v8::Script> script = |
| 3826 | v8::Script::Compile(context, v8_str(env->GetIsolate(), src), &origin) |
| 3827 | .ToLocalChecked(); |
| 3828 | v8::Local<v8::Value> r = script->Run(context).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3829 | |
| 3830 | CHECK(r->IsFunction()); |
| 3831 | CHECK_EQ(1, break_point_hit_count); |
| 3832 | |
| 3833 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3834 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3835 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 3836 | } |
| 3837 | |
| 3838 | |
| 3839 | static void DebugEventCounterCheck(int caught, int uncaught, int message) { |
| 3840 | CHECK_EQ(caught, exception_hit_count); |
| 3841 | CHECK_EQ(uncaught, uncaught_exception_hit_count); |
| 3842 | CHECK_EQ(message, message_callback_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3846 | // Test break on exceptions. For each exception break combination the number |
| 3847 | // of debug event exception callbacks and message callbacks are collected. The |
| 3848 | // number of debug event exception callbacks are used to check that the |
| 3849 | // debugger is called correctly and the number of message callbacks is used to |
| 3850 | // check that uncaught exceptions are still returned even if there is a break |
| 3851 | // for them. |
| 3852 | TEST(BreakOnException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3853 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3854 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3855 | env.ExposeDebug(); |
| 3856 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3857 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3858 | // Create functions for testing break on exception. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3859 | CompileFunction(&env, "function throws(){throw 1;}", "throws"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3860 | v8::Local<v8::Function> caught = |
| 3861 | CompileFunction(&env, |
| 3862 | "function caught(){try {throws();} catch(e) {};}", |
| 3863 | "caught"); |
| 3864 | v8::Local<v8::Function> notCaught = |
| 3865 | CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3866 | v8::Local<v8::Function> notCaughtFinally = CompileFunction( |
| 3867 | &env, "function notCaughtFinally(){try{throws();}finally{}}", |
| 3868 | "notCaughtFinally"); |
| 3869 | // In this edge case, even though this finally does not propagate the |
| 3870 | // exception, the debugger considers this uncaught, since we want to break |
| 3871 | // at the first throw for the general case where finally implicitly rethrows. |
| 3872 | v8::Local<v8::Function> edgeCaseFinally = CompileFunction( |
| 3873 | &env, "function caughtFinally(){L:try{throws();}finally{break L;}}", |
| 3874 | "caughtFinally"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3875 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3876 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 3877 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3878 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 3879 | // Initial state should be no break on exceptions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3880 | DebugEventCounterClear(); |
| 3881 | MessageCallbackCountClear(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3882 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3883 | DebugEventCounterCheck(0, 0, 0); |
| 3884 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3885 | DebugEventCounterCheck(0, 0, 1); |
| 3886 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3887 | DebugEventCounterCheck(0, 0, 2); |
| 3888 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3889 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3890 | |
| 3891 | // No break on exception |
| 3892 | DebugEventCounterClear(); |
| 3893 | MessageCallbackCountClear(); |
| 3894 | ChangeBreakOnException(false, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3895 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3896 | DebugEventCounterCheck(0, 0, 0); |
| 3897 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3898 | DebugEventCounterCheck(0, 0, 1); |
| 3899 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3900 | DebugEventCounterCheck(0, 0, 2); |
| 3901 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3902 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3903 | |
| 3904 | // Break on uncaught exception |
| 3905 | DebugEventCounterClear(); |
| 3906 | MessageCallbackCountClear(); |
| 3907 | ChangeBreakOnException(false, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3908 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3909 | DebugEventCounterCheck(0, 0, 0); |
| 3910 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3911 | DebugEventCounterCheck(1, 1, 1); |
| 3912 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3913 | DebugEventCounterCheck(2, 2, 2); |
| 3914 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3915 | DebugEventCounterCheck(3, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Break on exception and uncaught exception |
| 3918 | DebugEventCounterClear(); |
| 3919 | MessageCallbackCountClear(); |
| 3920 | ChangeBreakOnException(true, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3921 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3922 | DebugEventCounterCheck(1, 0, 0); |
| 3923 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3924 | DebugEventCounterCheck(2, 1, 1); |
| 3925 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3926 | DebugEventCounterCheck(3, 2, 2); |
| 3927 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3928 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3929 | |
| 3930 | // Break on exception |
| 3931 | DebugEventCounterClear(); |
| 3932 | MessageCallbackCountClear(); |
| 3933 | ChangeBreakOnException(true, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3934 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3935 | DebugEventCounterCheck(1, 0, 0); |
| 3936 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3937 | DebugEventCounterCheck(2, 1, 1); |
| 3938 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3939 | DebugEventCounterCheck(3, 2, 2); |
| 3940 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3941 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3942 | |
| 3943 | // No break on exception using JavaScript |
| 3944 | DebugEventCounterClear(); |
| 3945 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3946 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3947 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3948 | DebugEventCounterCheck(0, 0, 0); |
| 3949 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3950 | DebugEventCounterCheck(0, 0, 1); |
| 3951 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3952 | DebugEventCounterCheck(0, 0, 2); |
| 3953 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3954 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3955 | |
| 3956 | // Break on uncaught exception using JavaScript |
| 3957 | DebugEventCounterClear(); |
| 3958 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3959 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3960 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3961 | DebugEventCounterCheck(0, 0, 0); |
| 3962 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3963 | DebugEventCounterCheck(1, 1, 1); |
| 3964 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3965 | DebugEventCounterCheck(2, 2, 2); |
| 3966 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3967 | DebugEventCounterCheck(3, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3968 | |
| 3969 | // Break on exception and uncaught exception using JavaScript |
| 3970 | DebugEventCounterClear(); |
| 3971 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3972 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3973 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3974 | DebugEventCounterCheck(1, 0, 0); |
| 3975 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3976 | DebugEventCounterCheck(2, 1, 1); |
| 3977 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3978 | DebugEventCounterCheck(3, 2, 2); |
| 3979 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3980 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3981 | |
| 3982 | // Break on exception using JavaScript |
| 3983 | DebugEventCounterClear(); |
| 3984 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3985 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3986 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3987 | DebugEventCounterCheck(1, 0, 0); |
| 3988 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3989 | DebugEventCounterCheck(2, 1, 1); |
| 3990 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3991 | DebugEventCounterCheck(3, 2, 2); |
| 3992 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3993 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3994 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3995 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3996 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 3997 | env->GetIsolate()->RemoveMessageListeners(MessageCallbackCount); |
| 3998 | } |
| 3999 | |
| 4000 | |
| 4001 | static void try_finally_original_message(v8::Local<v8::Message> message, |
| 4002 | v8::Local<v8::Value> data) { |
| 4003 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
| 4004 | CHECK_EQ(2, message->GetLineNumber(context).FromJust()); |
| 4005 | CHECK_EQ(2, message->GetStartColumn(context).FromJust()); |
| 4006 | message_callback_count++; |
| 4007 | } |
| 4008 | |
| 4009 | |
| 4010 | TEST(TryFinallyOriginalMessage) { |
| 4011 | // Test that the debugger plays nicely with the pending message. |
| 4012 | message_callback_count = 0; |
| 4013 | DebugEventCounterClear(); |
| 4014 | DebugLocalContext env; |
| 4015 | v8::Isolate* isolate = CcTest::isolate(); |
| 4016 | isolate->AddMessageListener(try_finally_original_message); |
| 4017 | v8::Debug::SetDebugEventListener(isolate, DebugEventCounter); |
| 4018 | ChangeBreakOnException(true, true); |
| 4019 | v8::HandleScope scope(isolate); |
| 4020 | CompileRun( |
| 4021 | "try {\n" |
| 4022 | " throw 1;\n" |
| 4023 | "} finally {\n" |
| 4024 | "}\n"); |
| 4025 | DebugEventCounterCheck(1, 1, 1); |
| 4026 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4027 | isolate->RemoveMessageListeners(try_finally_original_message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4031 | TEST(EvalJSInDebugEventListenerOnNativeReThrownException) { |
| 4032 | DebugLocalContext env; |
| 4033 | v8::HandleScope scope(env->GetIsolate()); |
| 4034 | env.ExposeDebug(); |
| 4035 | |
| 4036 | // Create functions for testing break on exception. |
| 4037 | v8::Local<v8::Function> noThrowJS = CompileFunction( |
| 4038 | &env, "function noThrowJS(){var a=[1]; a.push(2); return a.length;}", |
| 4039 | "noThrowJS"); |
| 4040 | |
| 4041 | debug_event_listener_callback = noThrowJS; |
| 4042 | debug_event_listener_callback_result = 2; |
| 4043 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4044 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 4045 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4046 | // Break on uncaught exception |
| 4047 | ChangeBreakOnException(false, true); |
| 4048 | DebugEventCounterClear(); |
| 4049 | MessageCallbackCountClear(); |
| 4050 | |
| 4051 | // ReThrow native error |
| 4052 | { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4053 | v8::TryCatch tryCatch(env->GetIsolate()); |
| 4054 | env->GetIsolate()->ThrowException( |
| 4055 | v8::Exception::TypeError(v8_str(env->GetIsolate(), "Type error"))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4056 | CHECK(tryCatch.HasCaught()); |
| 4057 | tryCatch.ReThrow(); |
| 4058 | } |
| 4059 | CHECK_EQ(1, exception_hit_count); |
| 4060 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4061 | CHECK_EQ(0, message_callback_count); // FIXME: Should it be 1 ? |
| 4062 | CHECK(!debug_event_listener_callback.IsEmpty()); |
| 4063 | |
| 4064 | debug_event_listener_callback.Clear(); |
| 4065 | } |
| 4066 | |
| 4067 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4068 | // Test break on exception from compiler errors. When compiling using |
| 4069 | // v8::Script::Compile there is no JavaScript stack whereas when compiling using |
| 4070 | // eval there are JavaScript frames. |
| 4071 | TEST(BreakOnCompileException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4072 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4073 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4074 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4075 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4076 | // For this test, we want to break on uncaught exceptions: |
| 4077 | ChangeBreakOnException(false, true); |
| 4078 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4079 | // Create a function for checking the function when hitting a break point. |
| 4080 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 4081 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4082 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 4083 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4084 | |
| 4085 | DebugEventCounterClear(); |
| 4086 | MessageCallbackCountClear(); |
| 4087 | |
| 4088 | // Check initial state. |
| 4089 | CHECK_EQ(0, exception_hit_count); |
| 4090 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 4091 | CHECK_EQ(0, message_callback_count); |
| 4092 | CHECK_EQ(-1, last_js_stack_height); |
| 4093 | |
| 4094 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4095 | CHECK( |
| 4096 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4097 | CHECK_EQ(1, exception_hit_count); |
| 4098 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4099 | CHECK_EQ(1, message_callback_count); |
| 4100 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4101 | |
| 4102 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4103 | CHECK( |
| 4104 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "x x")).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4105 | CHECK_EQ(2, exception_hit_count); |
| 4106 | CHECK_EQ(2, uncaught_exception_hit_count); |
| 4107 | CHECK_EQ(2, message_callback_count); |
| 4108 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4109 | |
| 4110 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4111 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('+++')")) |
| 4112 | .ToLocalChecked() |
| 4113 | ->Run(context) |
| 4114 | .IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4115 | CHECK_EQ(3, exception_hit_count); |
| 4116 | CHECK_EQ(3, uncaught_exception_hit_count); |
| 4117 | CHECK_EQ(3, message_callback_count); |
| 4118 | CHECK_EQ(1, last_js_stack_height); |
| 4119 | |
| 4120 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4121 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('x x')")) |
| 4122 | .ToLocalChecked() |
| 4123 | ->Run(context) |
| 4124 | .IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4125 | CHECK_EQ(4, exception_hit_count); |
| 4126 | CHECK_EQ(4, uncaught_exception_hit_count); |
| 4127 | CHECK_EQ(4, message_callback_count); |
| 4128 | CHECK_EQ(1, last_js_stack_height); |
| 4129 | } |
| 4130 | |
| 4131 | |
| 4132 | TEST(StepWithException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4133 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4134 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4135 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4136 | // For this test, we want to break on uncaught exceptions: |
| 4137 | ChangeBreakOnException(false, true); |
| 4138 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4139 | // Create a function for checking the function when hitting a break point. |
| 4140 | frame_function_name = CompileFunction(&env, |
| 4141 | frame_function_name_source, |
| 4142 | "frame_function_name"); |
| 4143 | |
| 4144 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4145 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4146 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4147 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4148 | // Create functions for testing stepping. |
| 4149 | const char* src = "function a() { n(); }; " |
| 4150 | "function b() { c(); }; " |
| 4151 | "function c() { n(); }; " |
| 4152 | "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " |
| 4153 | "function e() { n(); }; " |
| 4154 | "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " |
| 4155 | "function g() { h(); }; " |
| 4156 | "function h() { x = 1; throw 1; }; "; |
| 4157 | |
| 4158 | // Step through invocation of a. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4159 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4160 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 4161 | SetBreakPoint(a, 0); |
| 4162 | step_action = StepIn; |
| 4163 | break_point_hit_count = 0; |
| 4164 | expected_step_sequence = "aa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4165 | CHECK(a->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4166 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4167 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4168 | |
| 4169 | // Step through invocation of b + c. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4170 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4171 | v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); |
| 4172 | SetBreakPoint(b, 0); |
| 4173 | step_action = StepIn; |
| 4174 | break_point_hit_count = 0; |
| 4175 | expected_step_sequence = "bcc"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4176 | CHECK(b->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4177 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4178 | break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4179 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4180 | // Step through invocation of d + e. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4181 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4182 | v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); |
| 4183 | SetBreakPoint(d, 0); |
| 4184 | ChangeBreakOnException(false, true); |
| 4185 | step_action = StepIn; |
| 4186 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4187 | expected_step_sequence = "ddedd"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4188 | d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4189 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4190 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4191 | |
| 4192 | // Step through invocation of d + e now with break on caught exceptions. |
| 4193 | ChangeBreakOnException(true, true); |
| 4194 | step_action = StepIn; |
| 4195 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4196 | expected_step_sequence = "ddeedd"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4197 | d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4198 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4199 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4200 | |
| 4201 | // Step through invocation of f + g + h. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4202 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4203 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4204 | SetBreakPoint(f, 0); |
| 4205 | ChangeBreakOnException(false, true); |
| 4206 | step_action = StepIn; |
| 4207 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4208 | expected_step_sequence = "ffghhff"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4209 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4210 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4211 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4212 | |
| 4213 | // Step through invocation of f + g + h now with break on caught exceptions. |
| 4214 | ChangeBreakOnException(true, true); |
| 4215 | step_action = StepIn; |
| 4216 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4217 | expected_step_sequence = "ffghhhff"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4218 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4219 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4220 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4221 | |
| 4222 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4223 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 4224 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | TEST(DebugBreak) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4229 | i::FLAG_stress_compaction = false; |
| 4230 | #ifdef VERIFY_HEAP |
| 4231 | i::FLAG_verify_heap = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4232 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4233 | DebugLocalContext env; |
| 4234 | v8::Isolate* isolate = env->GetIsolate(); |
| 4235 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4236 | |
| 4237 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4238 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreak); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4239 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4240 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4241 | // Create a function for testing stepping. |
| 4242 | const char* src = "function f0() {}" |
| 4243 | "function f1(x1) {}" |
| 4244 | "function f2(x1,x2) {}" |
| 4245 | "function f3(x1,x2,x3) {}"; |
| 4246 | v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); |
| 4247 | v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); |
| 4248 | v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); |
| 4249 | v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); |
| 4250 | |
| 4251 | // Call the function to make sure it is compiled. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4252 | v8::Local<v8::Value> argv[] = { |
| 4253 | v8::Number::New(isolate, 1), v8::Number::New(isolate, 1), |
| 4254 | v8::Number::New(isolate, 1), v8::Number::New(isolate, 1)}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4255 | |
| 4256 | // Call all functions to make sure that they are compiled. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4257 | f0->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4258 | f1->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4259 | f2->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4260 | f3->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4261 | |
| 4262 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4263 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4264 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4265 | |
| 4266 | // Call all functions with different argument count. |
| 4267 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4268 | for (unsigned int i = 0; i < arraysize(argv); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4269 | f0->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4270 | f1->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4271 | f2->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4272 | f3->Call(context, env->Global(), i, argv).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | // One break for each function called. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4276 | CHECK(4 * arraysize(argv) == break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4277 | |
| 4278 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4279 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4280 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | |
| 4284 | // Test to ensure that JavaScript code keeps running while the debug break |
| 4285 | // through the stack limit flag is set but breaks are disabled. |
| 4286 | TEST(DisableBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4287 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4288 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4289 | |
| 4290 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4291 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4293 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4294 | // Create a function for testing stepping. |
| 4295 | const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; |
| 4296 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4298 | // Set, test and cancel debug break. |
| 4299 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4300 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4301 | v8::Debug::CancelDebugBreak(env->GetIsolate()); |
| 4302 | CHECK(!v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4303 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4304 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4305 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Call all functions with different argument count. |
| 4308 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4309 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4310 | CHECK_EQ(1, break_point_hit_count); |
| 4311 | |
| 4312 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4313 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4314 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); |
| 4315 | v8::internal::DisableBreak disable_break(isolate->debug(), true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4316 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4317 | CHECK_EQ(1, break_point_hit_count); |
| 4318 | } |
| 4319 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4320 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4321 | CHECK_EQ(2, break_point_hit_count); |
| 4322 | |
| 4323 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4324 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 4325 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 4326 | } |
| 4327 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4328 | TEST(DisableDebuggerStatement) { |
| 4329 | DebugLocalContext env; |
| 4330 | v8::HandleScope scope(env->GetIsolate()); |
| 4331 | |
| 4332 | // Register a debug event listener which sets the break flag and counts. |
| 4333 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
| 4334 | CompileRun("debugger;"); |
| 4335 | CHECK_EQ(1, break_point_hit_count); |
| 4336 | |
| 4337 | // Check that we ignore debugger statement when breakpoints aren't active. |
| 4338 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); |
| 4339 | isolate->debug()->set_break_points_active(false); |
| 4340 | CompileRun("debugger;"); |
| 4341 | CHECK_EQ(1, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4344 | static const char* kSimpleExtensionSource = |
| 4345 | "(function Foo() {" |
| 4346 | " return 4;" |
| 4347 | "})() "; |
| 4348 | |
| 4349 | // http://crbug.com/28933 |
| 4350 | // Test that debug break is disabled when bootstrapper is active. |
| 4351 | TEST(NoBreakWhenBootstrapping) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4352 | v8::Isolate* isolate = CcTest::isolate(); |
| 4353 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4354 | |
| 4355 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4356 | v8::Debug::SetDebugEventListener(isolate, DebugEventCounter); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4357 | |
| 4358 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4359 | v8::Debug::DebugBreak(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4360 | break_point_hit_count = 0; |
| 4361 | { |
| 4362 | // Create a context with an extension to make sure that some JavaScript |
| 4363 | // code is executed during bootstrapping. |
| 4364 | v8::RegisterExtension(new v8::Extension("simpletest", |
| 4365 | kSimpleExtensionSource)); |
| 4366 | const char* extension_names[] = { "simpletest" }; |
| 4367 | v8::ExtensionConfiguration extensions(1, extension_names); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4368 | v8::HandleScope handle_scope(isolate); |
| 4369 | v8::Context::New(isolate, &extensions); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4370 | } |
| 4371 | // Check that no DebugBreak events occured during the context creation. |
| 4372 | CHECK_EQ(0, break_point_hit_count); |
| 4373 | |
| 4374 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4375 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4376 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4377 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4378 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4379 | |
| 4380 | static void NamedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4381 | v8::Local<v8::Array> result = v8::Array::New(info.GetIsolate(), 3); |
| 4382 | v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
| 4383 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 0), |
| 4384 | v8_str(info.GetIsolate(), "a")) |
| 4385 | .FromJust()); |
| 4386 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 1), |
| 4387 | v8_str(info.GetIsolate(), "b")) |
| 4388 | .FromJust()); |
| 4389 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 2), |
| 4390 | v8_str(info.GetIsolate(), "c")) |
| 4391 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4392 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4396 | static void IndexedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
| 4397 | v8::Isolate* isolate = info.GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4398 | v8::Local<v8::Array> result = v8::Array::New(isolate, 2); |
| 4399 | v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
| 4400 | CHECK(result->Set(context, v8::Integer::New(isolate, 0), |
| 4401 | v8::Number::New(isolate, 1)) |
| 4402 | .FromJust()); |
| 4403 | CHECK(result->Set(context, v8::Integer::New(isolate, 1), |
| 4404 | v8::Number::New(isolate, 10)) |
| 4405 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4406 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4410 | static void NamedGetter(v8::Local<v8::Name> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4411 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4412 | if (name->IsSymbol()) return; |
| 4413 | v8::String::Utf8Value n(v8::Local<v8::String>::Cast(name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4414 | if (strcmp(*n, "a") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4415 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "AA")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4416 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4417 | } else if (strcmp(*n, "b") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4418 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "BB")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4419 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4420 | } else if (strcmp(*n, "c") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4421 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "CC")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4422 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4423 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4424 | info.GetReturnValue().SetUndefined(); |
| 4425 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4426 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4427 | info.GetReturnValue().Set(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4431 | static void IndexedGetter(uint32_t index, |
| 4432 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4433 | info.GetReturnValue().Set(static_cast<double>(index + 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | |
| 4437 | TEST(InterceptorPropertyMirror) { |
| 4438 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4439 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4440 | v8::Isolate* isolate = env->GetIsolate(); |
| 4441 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4442 | env.ExposeDebug(); |
| 4443 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4444 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4445 | // Create object with named interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4446 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4447 | named->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4448 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4449 | CHECK(env->Global() |
| 4450 | ->Set(context, v8_str(isolate, "intercepted_named"), |
| 4451 | named->NewInstance(context).ToLocalChecked()) |
| 4452 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4453 | |
| 4454 | // Create object with indexed interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4455 | v8::Local<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4456 | indexed->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4457 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4458 | CHECK(env->Global() |
| 4459 | ->Set(context, v8_str(isolate, "intercepted_indexed"), |
| 4460 | indexed->NewInstance(context).ToLocalChecked()) |
| 4461 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4462 | |
| 4463 | // Create object with both named and indexed interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4464 | v8::Local<v8::ObjectTemplate> both = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4465 | both->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4466 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
| 4467 | both->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4468 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4469 | CHECK(env->Global() |
| 4470 | ->Set(context, v8_str(isolate, "intercepted_both"), |
| 4471 | both->NewInstance(context).ToLocalChecked()) |
| 4472 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4473 | |
| 4474 | // Get mirrors for the three objects with interceptor. |
| 4475 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4476 | "var named_mirror = debug.MakeMirror(intercepted_named);" |
| 4477 | "var indexed_mirror = debug.MakeMirror(intercepted_indexed);" |
| 4478 | "var both_mirror = debug.MakeMirror(intercepted_both)"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4479 | CHECK(CompileRun("named_mirror instanceof debug.ObjectMirror") |
| 4480 | ->BooleanValue(context) |
| 4481 | .FromJust()); |
| 4482 | CHECK(CompileRun("indexed_mirror instanceof debug.ObjectMirror") |
| 4483 | ->BooleanValue(context) |
| 4484 | .FromJust()); |
| 4485 | CHECK(CompileRun("both_mirror instanceof debug.ObjectMirror") |
| 4486 | ->BooleanValue(context) |
| 4487 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4488 | |
| 4489 | // Get the property names from the interceptors |
| 4490 | CompileRun( |
| 4491 | "named_names = named_mirror.propertyNames();" |
| 4492 | "indexed_names = indexed_mirror.propertyNames();" |
| 4493 | "both_names = both_mirror.propertyNames()"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4494 | CHECK_EQ(3, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4495 | CHECK_EQ(2, |
| 4496 | CompileRun("indexed_names.length")->Int32Value(context).FromJust()); |
| 4497 | CHECK_EQ(5, CompileRun("both_names.length")->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4498 | |
| 4499 | // Check the expected number of properties. |
| 4500 | const char* source; |
| 4501 | source = "named_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4502 | CHECK_EQ(3, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4503 | |
| 4504 | source = "indexed_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4505 | CHECK_EQ(2, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4506 | |
| 4507 | source = "both_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4508 | CHECK_EQ(5, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4509 | |
| 4510 | // Get the interceptor properties for the object with only named interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4511 | CompileRun("var named_values = named_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4512 | |
| 4513 | // Check that the properties are interceptor properties. |
| 4514 | for (int i = 0; i < 3; i++) { |
| 4515 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4516 | SNPrintF(buffer, |
| 4517 | "named_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4518 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4519 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4520 | SNPrintF(buffer, "named_values[%d].isNative()", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4521 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | // Get the interceptor properties for the object with only indexed |
| 4525 | // interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4526 | CompileRun("var indexed_values = indexed_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4527 | |
| 4528 | // Check that the properties are interceptor properties. |
| 4529 | for (int i = 0; i < 2; i++) { |
| 4530 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4531 | SNPrintF(buffer, |
| 4532 | "indexed_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4533 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | // Get the interceptor properties for the object with both types of |
| 4537 | // interceptors. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4538 | CompileRun("var both_values = both_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4539 | |
| 4540 | // Check that the properties are interceptor properties. |
| 4541 | for (int i = 0; i < 5; i++) { |
| 4542 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4543 | SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4544 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | // Check the property names. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4548 | source = "both_values[0].name() == '1'"; |
| 4549 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4550 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4551 | source = "both_values[1].name() == '10'"; |
| 4552 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4553 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4554 | source = "both_values[2].name() == 'a'"; |
| 4555 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4556 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4557 | source = "both_values[3].name() == 'b'"; |
| 4558 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4559 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4560 | source = "both_values[4].name() == 'c'"; |
| 4561 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | |
| 4565 | TEST(HiddenPrototypePropertyMirror) { |
| 4566 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4567 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4568 | v8::Isolate* isolate = env->GetIsolate(); |
| 4569 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4570 | env.ExposeDebug(); |
| 4571 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4572 | v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4573 | t0->InstanceTemplate()->Set(v8_str(isolate, "x"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4574 | v8::Number::New(isolate, 0)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4575 | v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4576 | t1->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4577 | t1->InstanceTemplate()->Set(v8_str(isolate, "y"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4578 | v8::Number::New(isolate, 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4579 | v8::Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4580 | t2->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4581 | t2->InstanceTemplate()->Set(v8_str(isolate, "z"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4582 | v8::Number::New(isolate, 2)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4583 | v8::Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(isolate); |
| 4584 | t3->InstanceTemplate()->Set(v8_str(isolate, "u"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4585 | v8::Number::New(isolate, 3)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4586 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4587 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4588 | // Create object and set them on the global object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4589 | v8::Local<v8::Object> o0 = t0->GetFunction(context) |
| 4590 | .ToLocalChecked() |
| 4591 | ->NewInstance(context) |
| 4592 | .ToLocalChecked(); |
| 4593 | CHECK(env->Global()->Set(context, v8_str(isolate, "o0"), o0).FromJust()); |
| 4594 | v8::Local<v8::Object> o1 = t1->GetFunction(context) |
| 4595 | .ToLocalChecked() |
| 4596 | ->NewInstance(context) |
| 4597 | .ToLocalChecked(); |
| 4598 | CHECK(env->Global()->Set(context, v8_str(isolate, "o1"), o1).FromJust()); |
| 4599 | v8::Local<v8::Object> o2 = t2->GetFunction(context) |
| 4600 | .ToLocalChecked() |
| 4601 | ->NewInstance(context) |
| 4602 | .ToLocalChecked(); |
| 4603 | CHECK(env->Global()->Set(context, v8_str(isolate, "o2"), o2).FromJust()); |
| 4604 | v8::Local<v8::Object> o3 = t3->GetFunction(context) |
| 4605 | .ToLocalChecked() |
| 4606 | ->NewInstance(context) |
| 4607 | .ToLocalChecked(); |
| 4608 | CHECK(env->Global()->Set(context, v8_str(isolate, "o3"), o3).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4609 | |
| 4610 | // Get mirrors for the four objects. |
| 4611 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4612 | "var o0_mirror = debug.MakeMirror(o0);" |
| 4613 | "var o1_mirror = debug.MakeMirror(o1);" |
| 4614 | "var o2_mirror = debug.MakeMirror(o2);" |
| 4615 | "var o3_mirror = debug.MakeMirror(o3)"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4616 | CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror") |
| 4617 | ->BooleanValue(context) |
| 4618 | .FromJust()); |
| 4619 | CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror") |
| 4620 | ->BooleanValue(context) |
| 4621 | .FromJust()); |
| 4622 | CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror") |
| 4623 | ->BooleanValue(context) |
| 4624 | .FromJust()); |
| 4625 | CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror") |
| 4626 | ->BooleanValue(context) |
| 4627 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4628 | |
| 4629 | // Check that each object has one property. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4630 | CHECK_EQ(1, CompileRun("o0_mirror.propertyNames().length") |
| 4631 | ->Int32Value(context) |
| 4632 | .FromJust()); |
| 4633 | CHECK_EQ(1, CompileRun("o1_mirror.propertyNames().length") |
| 4634 | ->Int32Value(context) |
| 4635 | .FromJust()); |
| 4636 | CHECK_EQ(1, CompileRun("o2_mirror.propertyNames().length") |
| 4637 | ->Int32Value(context) |
| 4638 | .FromJust()); |
| 4639 | CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") |
| 4640 | ->Int32Value(context) |
| 4641 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4642 | |
| 4643 | // Set o1 as prototype for o0. o1 has the hidden prototype flag so all |
| 4644 | // properties on o1 should be seen on o0. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4645 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o1).FromJust()); |
| 4646 | CHECK_EQ(2, CompileRun("o0_mirror.propertyNames().length") |
| 4647 | ->Int32Value(context) |
| 4648 | .FromJust()); |
| 4649 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4650 | ->Int32Value(context) |
| 4651 | .FromJust()); |
| 4652 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4653 | ->Int32Value(context) |
| 4654 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4655 | |
| 4656 | // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden |
| 4657 | // prototype flag. o2 also has the hidden prototype flag so all properties |
| 4658 | // on o2 should be seen on o0 as well as properties on o1. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4659 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o2).FromJust()); |
| 4660 | CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") |
| 4661 | ->Int32Value(context) |
| 4662 | .FromJust()); |
| 4663 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4664 | ->Int32Value(context) |
| 4665 | .FromJust()); |
| 4666 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4667 | ->Int32Value(context) |
| 4668 | .FromJust()); |
| 4669 | CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") |
| 4670 | ->Int32Value(context) |
| 4671 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and |
| 4674 | // o2 has the hidden prototype flag. o3 does not have the hidden prototype |
| 4675 | // flag so properties on o3 should not be seen on o0 whereas the properties |
| 4676 | // from o1 and o2 should still be seen on o0. |
| 4677 | // Final prototype chain: o0 -> o1 -> o2 -> o3 |
| 4678 | // Hidden prototypes: ^^ ^^ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4679 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o3).FromJust()); |
| 4680 | CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") |
| 4681 | ->Int32Value(context) |
| 4682 | .FromJust()); |
| 4683 | CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") |
| 4684 | ->Int32Value(context) |
| 4685 | .FromJust()); |
| 4686 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4687 | ->Int32Value(context) |
| 4688 | .FromJust()); |
| 4689 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4690 | ->Int32Value(context) |
| 4691 | .FromJust()); |
| 4692 | CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") |
| 4693 | ->Int32Value(context) |
| 4694 | .FromJust()); |
| 4695 | CHECK(CompileRun("o0_mirror.property('u').isUndefined()") |
| 4696 | ->BooleanValue(context) |
| 4697 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4698 | |
| 4699 | // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4700 | CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror") |
| 4701 | ->BooleanValue(context) |
| 4702 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4706 | static void ProtperyXNativeGetter( |
| 4707 | v8::Local<v8::String> property, |
| 4708 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4709 | info.GetReturnValue().Set(10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | |
| 4713 | TEST(NativeGetterPropertyMirror) { |
| 4714 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4715 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4716 | v8::Isolate* isolate = env->GetIsolate(); |
| 4717 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4718 | env.ExposeDebug(); |
| 4719 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4720 | v8::Local<v8::Context> context = env.context(); |
| 4721 | v8::Local<v8::String> name = v8_str(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4722 | // Create object with named accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4723 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
| 4724 | named->SetAccessor(name, &ProtperyXNativeGetter, NULL, v8::Local<v8::Value>(), |
| 4725 | v8::DEFAULT, v8::None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4726 | |
| 4727 | // Create object with named property getter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4728 | CHECK(env->Global() |
| 4729 | ->Set(context, v8_str(isolate, "instance"), |
| 4730 | named->NewInstance(context).ToLocalChecked()) |
| 4731 | .FromJust()); |
| 4732 | CHECK_EQ(10, CompileRun("instance.x")->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4733 | |
| 4734 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4735 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4736 | CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") |
| 4737 | ->BooleanValue(context) |
| 4738 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4739 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4740 | CompileRun("var named_names = instance_mirror.propertyNames();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4741 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4742 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); |
| 4743 | CHECK(CompileRun("instance_mirror.property('x').value().isNumber()") |
| 4744 | ->BooleanValue(context) |
| 4745 | .FromJust()); |
| 4746 | CHECK(CompileRun("instance_mirror.property('x').value().value() == 10") |
| 4747 | ->BooleanValue(context) |
| 4748 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4752 | static void ProtperyXNativeGetterThrowingError( |
| 4753 | v8::Local<v8::String> property, |
| 4754 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4755 | CompileRun("throw new Error('Error message');"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | |
| 4759 | TEST(NativeGetterThrowingErrorPropertyMirror) { |
| 4760 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4761 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4762 | v8::Isolate* isolate = env->GetIsolate(); |
| 4763 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4764 | env.ExposeDebug(); |
| 4765 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4766 | v8::Local<v8::Context> context = env.context(); |
| 4767 | v8::Local<v8::String> name = v8_str(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4768 | // Create object with named accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4769 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4770 | named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4771 | v8::Local<v8::Value>(), v8::DEFAULT, v8::None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4772 | |
| 4773 | // Create object with named property getter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4774 | CHECK(env->Global() |
| 4775 | ->Set(context, v8_str(isolate, "instance"), |
| 4776 | named->NewInstance(context).ToLocalChecked()) |
| 4777 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4778 | |
| 4779 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4780 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4781 | CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") |
| 4782 | ->BooleanValue(context) |
| 4783 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4784 | CompileRun("named_names = instance_mirror.propertyNames();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4785 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4786 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); |
| 4787 | CHECK(CompileRun("instance_mirror.property('x').value().isError()") |
| 4788 | ->BooleanValue(context) |
| 4789 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4790 | |
| 4791 | // Check that the message is that passed to the Error constructor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4792 | CHECK( |
| 4793 | CompileRun( |
| 4794 | "instance_mirror.property('x').value().message() == 'Error message'") |
| 4795 | ->BooleanValue(context) |
| 4796 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4800 | // Test that hidden properties object is not returned as an unnamed property |
| 4801 | // among regular properties. |
| 4802 | // See http://crbug.com/26491 |
| 4803 | TEST(NoHiddenProperties) { |
| 4804 | // Create a V8 environment with debug access. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4805 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4806 | v8::Isolate* isolate = env->GetIsolate(); |
| 4807 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4808 | env.ExposeDebug(); |
| 4809 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4810 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4811 | // Create an object in the global scope. |
| 4812 | const char* source = "var obj = {a: 1};"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4813 | v8::Script::Compile(context, v8_str(isolate, source)) |
| 4814 | .ToLocalChecked() |
| 4815 | ->Run(context) |
| 4816 | .ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4817 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4818 | env->Global()->Get(context, v8_str(isolate, "obj")).ToLocalChecked()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4819 | // Set a hidden property on the object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4820 | obj->SetPrivate( |
| 4821 | env.context(), |
| 4822 | v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::a")), |
| 4823 | v8::Int32::New(isolate, 11)) |
| 4824 | .FromJust(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4825 | |
| 4826 | // Get mirror for the object with property getter. |
| 4827 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4828 | CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") |
| 4829 | ->BooleanValue(context) |
| 4830 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4831 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4832 | // There should be exactly one property. But there is also an unnamed |
| 4833 | // property whose value is hidden properties dictionary. The latter |
| 4834 | // property should not be in the list of reguar properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4835 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4836 | CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue(context).FromJust()); |
| 4837 | CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") |
| 4838 | ->BooleanValue(context) |
| 4839 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4840 | |
| 4841 | // Object created by t0 will become hidden prototype of object 'obj'. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4842 | v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4843 | t0->InstanceTemplate()->Set(v8_str(isolate, "b"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4844 | v8::Number::New(isolate, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4845 | t0->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4846 | v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
| 4847 | t1->InstanceTemplate()->Set(v8_str(isolate, "c"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4848 | v8::Number::New(isolate, 3)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4849 | |
| 4850 | // Create proto objects, add hidden properties to them and set them on |
| 4851 | // the global object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4852 | v8::Local<v8::Object> protoObj = t0->GetFunction(context) |
| 4853 | .ToLocalChecked() |
| 4854 | ->NewInstance(context) |
| 4855 | .ToLocalChecked(); |
| 4856 | protoObj->SetPrivate( |
| 4857 | env.context(), |
| 4858 | v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::b")), |
| 4859 | v8::Int32::New(isolate, 12)) |
| 4860 | .FromJust(); |
| 4861 | CHECK(env->Global() |
| 4862 | ->Set(context, v8_str(isolate, "protoObj"), protoObj) |
| 4863 | .FromJust()); |
| 4864 | v8::Local<v8::Object> grandProtoObj = t1->GetFunction(context) |
| 4865 | .ToLocalChecked() |
| 4866 | ->NewInstance(context) |
| 4867 | .ToLocalChecked(); |
| 4868 | grandProtoObj->SetPrivate(env.context(), |
| 4869 | v8::Private::New( |
| 4870 | isolate, v8_str(isolate, "v8::test-debug::c")), |
| 4871 | v8::Int32::New(isolate, 13)) |
| 4872 | .FromJust(); |
| 4873 | CHECK(env->Global() |
| 4874 | ->Set(context, v8_str(isolate, "grandProtoObj"), grandProtoObj) |
| 4875 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4876 | |
| 4877 | // Setting prototypes: obj->protoObj->grandProtoObj |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4878 | CHECK(protoObj->Set(context, v8_str(isolate, "__proto__"), grandProtoObj) |
| 4879 | .FromJust()); |
| 4880 | CHECK(obj->Set(context, v8_str(isolate, "__proto__"), protoObj).FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4881 | |
| 4882 | // Get mirror for the object with property getter. |
| 4883 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4884 | CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") |
| 4885 | ->BooleanValue(context) |
| 4886 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4887 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4888 | // There should be exactly two properties - one from the object itself and |
| 4889 | // another from its hidden prototype. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4890 | CHECK_EQ(2, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4891 | CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4892 | "named_names[1] == 'b'") |
| 4893 | ->BooleanValue(context) |
| 4894 | .FromJust()); |
| 4895 | CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") |
| 4896 | ->BooleanValue(context) |
| 4897 | .FromJust()); |
| 4898 | CHECK(CompileRun("obj_mirror.property('b').value().value() == 2") |
| 4899 | ->BooleanValue(context) |
| 4900 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4903 | |
| 4904 | // Multithreaded tests of JSON debugger protocol |
| 4905 | |
| 4906 | // Support classes |
| 4907 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4908 | // Provides synchronization between N threads, where N is a template parameter. |
| 4909 | // The Wait() call blocks a thread until it is called for the Nth time, then all |
| 4910 | // calls return. Each ThreadBarrier object can only be used once. |
| 4911 | template <int N> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4912 | class ThreadBarrier final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4913 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4914 | ThreadBarrier() : num_blocked_(0) {} |
| 4915 | |
| 4916 | ~ThreadBarrier() { |
| 4917 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4918 | if (num_blocked_ != 0) { |
| 4919 | CHECK_EQ(N, num_blocked_); |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | void Wait() { |
| 4924 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4925 | CHECK_LT(num_blocked_, N); |
| 4926 | num_blocked_++; |
| 4927 | if (N == num_blocked_) { |
| 4928 | // Signal and unblock all waiting threads. |
| 4929 | cv_.NotifyAll(); |
| 4930 | printf("BARRIER\n\n"); |
| 4931 | fflush(stdout); |
| 4932 | } else { // Wait for the semaphore. |
| 4933 | while (num_blocked_ < N) { |
| 4934 | cv_.Wait(&mutex_); |
| 4935 | } |
| 4936 | } |
| 4937 | CHECK_EQ(N, num_blocked_); |
| 4938 | } |
| 4939 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4940 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4941 | ConditionVariable cv_; |
| 4942 | Mutex mutex_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4943 | int num_blocked_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4944 | |
| 4945 | STATIC_ASSERT(N > 0); |
| 4946 | |
| 4947 | DISALLOW_COPY_AND_ASSIGN(ThreadBarrier); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4948 | }; |
| 4949 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4950 | |
| 4951 | // A set containing enough barriers and semaphores for any of the tests. |
| 4952 | class Barriers { |
| 4953 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4954 | Barriers() : semaphore_1(0), semaphore_2(0) {} |
| 4955 | ThreadBarrier<2> barrier_1; |
| 4956 | ThreadBarrier<2> barrier_2; |
| 4957 | ThreadBarrier<2> barrier_3; |
| 4958 | ThreadBarrier<2> barrier_4; |
| 4959 | ThreadBarrier<2> barrier_5; |
| 4960 | v8::base::Semaphore semaphore_1; |
| 4961 | v8::base::Semaphore semaphore_2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4962 | }; |
| 4963 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4964 | |
| 4965 | // We match parts of the message to decide if it is a break message. |
| 4966 | bool IsBreakEventMessage(char *message) { |
| 4967 | const char* type_event = "\"type\":\"event\""; |
| 4968 | const char* event_break = "\"event\":\"break\""; |
| 4969 | // Does the message contain both type:event and event:break? |
| 4970 | return strstr(message, type_event) != NULL && |
| 4971 | strstr(message, event_break) != NULL; |
| 4972 | } |
| 4973 | |
| 4974 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4975 | // We match parts of the message to decide if it is a exception message. |
| 4976 | bool IsExceptionEventMessage(char *message) { |
| 4977 | const char* type_event = "\"type\":\"event\""; |
| 4978 | const char* event_exception = "\"event\":\"exception\""; |
| 4979 | // Does the message contain both type:event and event:exception? |
| 4980 | return strstr(message, type_event) != NULL && |
| 4981 | strstr(message, event_exception) != NULL; |
| 4982 | } |
| 4983 | |
| 4984 | |
| 4985 | // We match the message wether it is an evaluate response message. |
| 4986 | bool IsEvaluateResponseMessage(char* message) { |
| 4987 | const char* type_response = "\"type\":\"response\""; |
| 4988 | const char* command_evaluate = "\"command\":\"evaluate\""; |
| 4989 | // Does the message contain both type:response and command:evaluate? |
| 4990 | return strstr(message, type_response) != NULL && |
| 4991 | strstr(message, command_evaluate) != NULL; |
| 4992 | } |
| 4993 | |
| 4994 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4995 | static int StringToInt(const char* s) { |
| 4996 | return atoi(s); // NOLINT |
| 4997 | } |
| 4998 | |
| 4999 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5000 | // We match parts of the message to get evaluate result int value. |
| 5001 | int GetEvaluateIntResult(char *message) { |
| 5002 | const char* value = "\"value\":"; |
| 5003 | char* pos = strstr(message, value); |
| 5004 | if (pos == NULL) { |
| 5005 | return -1; |
| 5006 | } |
| 5007 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5008 | res = StringToInt(pos + strlen(value)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5009 | return res; |
| 5010 | } |
| 5011 | |
| 5012 | |
| 5013 | // We match parts of the message to get hit breakpoint id. |
| 5014 | int GetBreakpointIdFromBreakEventMessage(char *message) { |
| 5015 | const char* breakpoints = "\"breakpoints\":["; |
| 5016 | char* pos = strstr(message, breakpoints); |
| 5017 | if (pos == NULL) { |
| 5018 | return -1; |
| 5019 | } |
| 5020 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5021 | res = StringToInt(pos + strlen(breakpoints)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5022 | return res; |
| 5023 | } |
| 5024 | |
| 5025 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5026 | // We match parts of the message to get total frames number. |
| 5027 | int GetTotalFramesInt(char *message) { |
| 5028 | const char* prefix = "\"totalFrames\":"; |
| 5029 | char* pos = strstr(message, prefix); |
| 5030 | if (pos == NULL) { |
| 5031 | return -1; |
| 5032 | } |
| 5033 | pos += strlen(prefix); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5034 | int res = StringToInt(pos); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5035 | return res; |
| 5036 | } |
| 5037 | |
| 5038 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5039 | // We match parts of the message to get source line. |
| 5040 | int GetSourceLineFromBreakEventMessage(char *message) { |
| 5041 | const char* source_line = "\"sourceLine\":"; |
| 5042 | char* pos = strstr(message, source_line); |
| 5043 | if (pos == NULL) { |
| 5044 | return -1; |
| 5045 | } |
| 5046 | int res = -1; |
| 5047 | res = StringToInt(pos + strlen(source_line)); |
| 5048 | return res; |
| 5049 | } |
| 5050 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5051 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5052 | /* Test MessageQueues */ |
| 5053 | /* Tests the message queues that hold debugger commands and |
| 5054 | * response messages to the debugger. Fills queues and makes |
| 5055 | * them grow. |
| 5056 | */ |
| 5057 | Barriers message_queue_barriers; |
| 5058 | |
| 5059 | // This is the debugger thread, that executes no v8 calls except |
| 5060 | // placing JSON debugger commands in the queue. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5061 | class MessageQueueDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5062 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5063 | MessageQueueDebuggerThread() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5064 | : Thread(Options("MessageQueueDebuggerThread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5065 | void Run(); |
| 5066 | }; |
| 5067 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5068 | |
| 5069 | static void MessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5070 | v8::Local<v8::String> json = message.GetJSON(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5071 | v8::String::Utf8Value utf8(json); |
| 5072 | if (IsBreakEventMessage(*utf8)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5073 | // Lets test script wait until break occurs to send commands. |
| 5074 | // Signals when a break is reported. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5075 | message_queue_barriers.semaphore_2.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | // Allow message handler to block on a semaphore, to test queueing of |
| 5079 | // messages while blocked. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5080 | message_queue_barriers.semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5083 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5084 | void MessageQueueDebuggerThread::Run() { |
| 5085 | const int kBufferSize = 1000; |
| 5086 | uint16_t buffer_1[kBufferSize]; |
| 5087 | uint16_t buffer_2[kBufferSize]; |
| 5088 | const char* command_1 = |
| 5089 | "{\"seq\":117," |
| 5090 | "\"type\":\"request\"," |
| 5091 | "\"command\":\"evaluate\"," |
| 5092 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5093 | const char* command_2 = |
| 5094 | "{\"seq\":118," |
| 5095 | "\"type\":\"request\"," |
| 5096 | "\"command\":\"evaluate\"," |
| 5097 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5098 | const char* command_3 = |
| 5099 | "{\"seq\":119," |
| 5100 | "\"type\":\"request\"," |
| 5101 | "\"command\":\"evaluate\"," |
| 5102 | "\"arguments\":{\"expression\":\"c.d * b\"}}"; |
| 5103 | const char* command_continue = |
| 5104 | "{\"seq\":106," |
| 5105 | "\"type\":\"request\"," |
| 5106 | "\"command\":\"continue\"}"; |
| 5107 | const char* command_single_step = |
| 5108 | "{\"seq\":107," |
| 5109 | "\"type\":\"request\"," |
| 5110 | "\"command\":\"continue\"," |
| 5111 | "\"arguments\":{\"stepaction\":\"next\"}}"; |
| 5112 | |
| 5113 | /* Interleaved sequence of actions by the two threads:*/ |
| 5114 | // Main thread compiles and runs source_1 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5115 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5116 | message_queue_barriers.barrier_1.Wait(); |
| 5117 | // Post 6 commands, filling the command queue and making it expand. |
| 5118 | // These calls return immediately, but the commands stay on the queue |
| 5119 | // until the execution of source_2. |
| 5120 | // Note: AsciiToUtf16 executes before SendCommand, so command is copied |
| 5121 | // to buffer before buffer is sent to SendCommand. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5122 | v8::Isolate* isolate = CcTest::isolate(); |
| 5123 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 5124 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 5125 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5126 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5127 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5128 | message_queue_barriers.barrier_2.Wait(); |
| 5129 | // Main thread compiles and runs source_2. |
| 5130 | // Queued commands are executed at the start of compilation of source_2( |
| 5131 | // beforeCompile event). |
| 5132 | // Free the message handler to process all the messages from the queue. 7 |
| 5133 | // messages are expected: 2 afterCompile events and 5 responses. |
| 5134 | // All the commands added so far will fail to execute as long as call stack |
| 5135 | // is empty on beforeCompile event. |
| 5136 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5137 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5138 | } |
| 5139 | message_queue_barriers.barrier_3.Wait(); |
| 5140 | // Main thread compiles and runs source_3. |
| 5141 | // Don't stop in the afterCompile handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5142 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5143 | // source_3 includes a debugger statement, which causes a break event. |
| 5144 | // Wait on break event from hitting "debugger" statement |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5145 | message_queue_barriers.semaphore_2.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5146 | // These should execute after the "debugger" statement in source_2 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5147 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 5148 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 5149 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5150 | v8::Debug::SendCommand( |
| 5151 | isolate, buffer_2, AsciiToUtf16(command_single_step, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5152 | // Run after 2 break events, 4 responses. |
| 5153 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5154 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5155 | } |
| 5156 | // Wait on break event after a single step executes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5157 | message_queue_barriers.semaphore_2.Wait(); |
| 5158 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_2, buffer_1)); |
| 5159 | v8::Debug::SendCommand( |
| 5160 | isolate, buffer_2, AsciiToUtf16(command_continue, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5161 | // Run after 2 responses. |
| 5162 | for (int i = 0; i < 2 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5163 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5164 | } |
| 5165 | // Main thread continues running source_3 to end, waits for this thread. |
| 5166 | } |
| 5167 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5168 | |
| 5169 | // This thread runs the v8 engine. |
| 5170 | TEST(MessageQueues) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5171 | MessageQueueDebuggerThread message_queue_debugger_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5172 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5173 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5174 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5175 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5176 | v8::Debug::SetMessageHandler(env->GetIsolate(), MessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5177 | message_queue_debugger_thread.Start(); |
| 5178 | |
| 5179 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5180 | const char* source_2 = "e = 17;"; |
| 5181 | const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; |
| 5182 | |
| 5183 | // See MessageQueueDebuggerThread::Run for interleaved sequence of |
| 5184 | // API calls and events in the two threads. |
| 5185 | CompileRun(source_1); |
| 5186 | message_queue_barriers.barrier_1.Wait(); |
| 5187 | message_queue_barriers.barrier_2.Wait(); |
| 5188 | CompileRun(source_2); |
| 5189 | message_queue_barriers.barrier_3.Wait(); |
| 5190 | CompileRun(source_3); |
| 5191 | message_queue_debugger_thread.Join(); |
| 5192 | fflush(stdout); |
| 5193 | } |
| 5194 | |
| 5195 | |
| 5196 | class TestClientData : public v8::Debug::ClientData { |
| 5197 | public: |
| 5198 | TestClientData() { |
| 5199 | constructor_call_counter++; |
| 5200 | } |
| 5201 | virtual ~TestClientData() { |
| 5202 | destructor_call_counter++; |
| 5203 | } |
| 5204 | |
| 5205 | static void ResetCounters() { |
| 5206 | constructor_call_counter = 0; |
| 5207 | destructor_call_counter = 0; |
| 5208 | } |
| 5209 | |
| 5210 | static int constructor_call_counter; |
| 5211 | static int destructor_call_counter; |
| 5212 | }; |
| 5213 | |
| 5214 | int TestClientData::constructor_call_counter = 0; |
| 5215 | int TestClientData::destructor_call_counter = 0; |
| 5216 | |
| 5217 | |
| 5218 | // Tests that MessageQueue doesn't destroy client data when expands and |
| 5219 | // does destroy when it dies. |
| 5220 | TEST(MessageQueueExpandAndDestroy) { |
| 5221 | TestClientData::ResetCounters(); |
| 5222 | { // Create a scope for the queue. |
| 5223 | CommandMessageQueue queue(1); |
| 5224 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5225 | new TestClientData())); |
| 5226 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5227 | new TestClientData())); |
| 5228 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5229 | new TestClientData())); |
| 5230 | CHECK_EQ(0, TestClientData::destructor_call_counter); |
| 5231 | queue.Get().Dispose(); |
| 5232 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5233 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5234 | new TestClientData())); |
| 5235 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5236 | new TestClientData())); |
| 5237 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5238 | new TestClientData())); |
| 5239 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5240 | new TestClientData())); |
| 5241 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5242 | new TestClientData())); |
| 5243 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5244 | queue.Get().Dispose(); |
| 5245 | CHECK_EQ(2, TestClientData::destructor_call_counter); |
| 5246 | } |
| 5247 | // All the client data should be destroyed when the queue is destroyed. |
| 5248 | CHECK_EQ(TestClientData::destructor_call_counter, |
| 5249 | TestClientData::destructor_call_counter); |
| 5250 | } |
| 5251 | |
| 5252 | |
| 5253 | static int handled_client_data_instances_count = 0; |
| 5254 | static void MessageHandlerCountingClientData( |
| 5255 | const v8::Debug::Message& message) { |
| 5256 | if (message.GetClientData() != NULL) { |
| 5257 | handled_client_data_instances_count++; |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | |
| 5262 | // Tests that all client data passed to the debugger are sent to the handler. |
| 5263 | TEST(SendClientDataToHandler) { |
| 5264 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5265 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5266 | v8::Isolate* isolate = env->GetIsolate(); |
| 5267 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5268 | TestClientData::ResetCounters(); |
| 5269 | handled_client_data_instances_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5270 | v8::Debug::SetMessageHandler(isolate, MessageHandlerCountingClientData); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5271 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5272 | const int kBufferSize = 1000; |
| 5273 | uint16_t buffer[kBufferSize]; |
| 5274 | const char* command_1 = |
| 5275 | "{\"seq\":117," |
| 5276 | "\"type\":\"request\"," |
| 5277 | "\"command\":\"evaluate\"," |
| 5278 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5279 | const char* command_2 = |
| 5280 | "{\"seq\":118," |
| 5281 | "\"type\":\"request\"," |
| 5282 | "\"command\":\"evaluate\"," |
| 5283 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5284 | const char* command_continue = |
| 5285 | "{\"seq\":106," |
| 5286 | "\"type\":\"request\"," |
| 5287 | "\"command\":\"continue\"}"; |
| 5288 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5289 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_1, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5290 | new TestClientData()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5291 | v8::Debug::SendCommand( |
| 5292 | isolate, buffer, AsciiToUtf16(command_2, buffer), NULL); |
| 5293 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_2, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5294 | new TestClientData()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5295 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_2, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5296 | new TestClientData()); |
| 5297 | // All the messages will be processed on beforeCompile event. |
| 5298 | CompileRun(source_1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5299 | v8::Debug::SendCommand( |
| 5300 | isolate, buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5301 | CHECK_EQ(3, TestClientData::constructor_call_counter); |
| 5302 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5303 | handled_client_data_instances_count); |
| 5304 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5305 | TestClientData::destructor_call_counter); |
| 5306 | } |
| 5307 | |
| 5308 | |
| 5309 | /* Test ThreadedDebugging */ |
| 5310 | /* This test interrupts a running infinite loop that is |
| 5311 | * occupying the v8 thread by a break command from the |
| 5312 | * debugger thread. It then changes the value of a |
| 5313 | * global object, to make the loop terminate. |
| 5314 | */ |
| 5315 | |
| 5316 | Barriers threaded_debugging_barriers; |
| 5317 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5318 | class V8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5319 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5320 | V8Thread() : Thread(Options("V8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5321 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5322 | v8::Isolate* isolate() { return isolate_; } |
| 5323 | |
| 5324 | private: |
| 5325 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5326 | }; |
| 5327 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5328 | class DebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5329 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5330 | explicit DebuggerThread(v8::Isolate* isolate) |
| 5331 | : Thread(Options("DebuggerThread")), isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5332 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5333 | |
| 5334 | private: |
| 5335 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5336 | }; |
| 5337 | |
| 5338 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5339 | static void ThreadedAtBarrier1( |
| 5340 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5341 | threaded_debugging_barriers.barrier_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | |
| 5345 | static void ThreadedMessageHandler(const v8::Debug::Message& message) { |
| 5346 | static char print_buffer[1000]; |
| 5347 | v8::String::Value json(message.GetJSON()); |
| 5348 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5349 | if (IsBreakEventMessage(print_buffer)) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5350 | // Check that we are inside the while loop. |
| 5351 | int source_line = GetSourceLineFromBreakEventMessage(print_buffer); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5352 | CHECK(4 <= source_line && source_line <= 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5353 | threaded_debugging_barriers.barrier_2.Wait(); |
| 5354 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | |
| 5358 | void V8Thread::Run() { |
| 5359 | const char* source = |
| 5360 | "flag = true;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5361 | "\n" |
| 5362 | "function foo() {\n" |
| 5363 | " var x = 1;\n" |
| 5364 | " while ( flag == true ) {\n" |
| 5365 | " if ( x == 1 ) {\n" |
| 5366 | " ThreadedAtBarrier1();\n" |
| 5367 | " }\n" |
| 5368 | " x = x + 1;\n" |
| 5369 | " }\n" |
| 5370 | "}\n" |
| 5371 | "\n" |
| 5372 | "foo();\n"; |
| 5373 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5374 | v8::Isolate::CreateParams create_params; |
| 5375 | create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 5376 | isolate_ = v8::Isolate::New(create_params); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5377 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5378 | { |
| 5379 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5380 | DebugLocalContext env(isolate_); |
| 5381 | v8::HandleScope scope(isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5382 | v8::Debug::SetMessageHandler(isolate_, &ThreadedMessageHandler); |
| 5383 | v8::Local<v8::ObjectTemplate> global_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5384 | v8::ObjectTemplate::New(env->GetIsolate()); |
| 5385 | global_template->Set( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5386 | v8_str(env->GetIsolate(), "ThreadedAtBarrier1"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5387 | v8::FunctionTemplate::New(isolate_, ThreadedAtBarrier1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5388 | v8::Local<v8::Context> context = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5389 | v8::Context::New(isolate_, NULL, global_template); |
| 5390 | v8::Context::Scope context_scope(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5391 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5392 | CompileRun(source); |
| 5393 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5394 | threaded_debugging_barriers.barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5395 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5398 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5399 | void DebuggerThread::Run() { |
| 5400 | const int kBufSize = 1000; |
| 5401 | uint16_t buffer[kBufSize]; |
| 5402 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5403 | const char* command_1 = |
| 5404 | "{\"seq\":102," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5405 | "\"type\":\"request\"," |
| 5406 | "\"command\":\"evaluate\"," |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5407 | "\"arguments\":{\"expression\":\"flag = false\"}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5408 | const char* command_2 = "{\"seq\":103," |
| 5409 | "\"type\":\"request\"," |
| 5410 | "\"command\":\"continue\"}"; |
| 5411 | |
| 5412 | threaded_debugging_barriers.barrier_1.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5413 | v8::Debug::DebugBreak(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5414 | threaded_debugging_barriers.barrier_2.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5415 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
| 5416 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5417 | threaded_debugging_barriers.barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5420 | |
| 5421 | TEST(ThreadedDebugging) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5422 | V8Thread v8_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5423 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5424 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5425 | v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5426 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5427 | DebuggerThread debugger_thread(v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5428 | debugger_thread.Start(); |
| 5429 | |
| 5430 | v8_thread.Join(); |
| 5431 | debugger_thread.Join(); |
| 5432 | } |
| 5433 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5434 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5435 | /* Test RecursiveBreakpoints */ |
| 5436 | /* In this test, the debugger evaluates a function with a breakpoint, after |
| 5437 | * hitting a breakpoint in another function. We do this with both values |
| 5438 | * of the flag enabling recursive breakpoints, and verify that the second |
| 5439 | * breakpoint is hit when enabled, and missed when disabled. |
| 5440 | */ |
| 5441 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5442 | class BreakpointsV8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5443 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5444 | BreakpointsV8Thread() : Thread(Options("BreakpointsV8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5445 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5446 | |
| 5447 | v8::Isolate* isolate() { return isolate_; } |
| 5448 | |
| 5449 | private: |
| 5450 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5451 | }; |
| 5452 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5453 | class BreakpointsDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5454 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5455 | BreakpointsDebuggerThread(bool global_evaluate, v8::Isolate* isolate) |
| 5456 | : Thread(Options("BreakpointsDebuggerThread")), |
| 5457 | global_evaluate_(global_evaluate), |
| 5458 | isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5459 | void Run(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5460 | |
| 5461 | private: |
| 5462 | bool global_evaluate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5463 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5464 | }; |
| 5465 | |
| 5466 | |
| 5467 | Barriers* breakpoints_barriers; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5468 | int break_event_breakpoint_id; |
| 5469 | int evaluate_int_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5470 | |
| 5471 | static void BreakpointsMessageHandler(const v8::Debug::Message& message) { |
| 5472 | static char print_buffer[1000]; |
| 5473 | v8::String::Value json(message.GetJSON()); |
| 5474 | Utf16ToAscii(*json, json.length(), print_buffer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5475 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5476 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5477 | break_event_breakpoint_id = |
| 5478 | GetBreakpointIdFromBreakEventMessage(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5479 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5480 | } else if (IsEvaluateResponseMessage(print_buffer)) { |
| 5481 | evaluate_int_result = GetEvaluateIntResult(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5482 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | |
| 5487 | void BreakpointsV8Thread::Run() { |
| 5488 | const char* source_1 = "var y_global = 3;\n" |
| 5489 | "function cat( new_value ) {\n" |
| 5490 | " var x = new_value;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5491 | " y_global = y_global + 4;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5492 | " x = 3 * x + 1;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5493 | " y_global = y_global + 5;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5494 | " return x;\n" |
| 5495 | "}\n" |
| 5496 | "\n" |
| 5497 | "function dog() {\n" |
| 5498 | " var x = 1;\n" |
| 5499 | " x = y_global;" |
| 5500 | " var z = 3;" |
| 5501 | " x += 100;\n" |
| 5502 | " return x;\n" |
| 5503 | "}\n" |
| 5504 | "\n"; |
| 5505 | const char* source_2 = "cat(17);\n" |
| 5506 | "cat(19);\n"; |
| 5507 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5508 | v8::Isolate::CreateParams create_params; |
| 5509 | create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 5510 | isolate_ = v8::Isolate::New(create_params); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5511 | breakpoints_barriers->barrier_3.Wait(); |
| 5512 | { |
| 5513 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5514 | DebugLocalContext env(isolate_); |
| 5515 | v8::HandleScope scope(isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5516 | v8::Debug::SetMessageHandler(isolate_, &BreakpointsMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5517 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5518 | CompileRun(source_1); |
| 5519 | breakpoints_barriers->barrier_1.Wait(); |
| 5520 | breakpoints_barriers->barrier_2.Wait(); |
| 5521 | CompileRun(source_2); |
| 5522 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5523 | breakpoints_barriers->barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5524 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
| 5527 | |
| 5528 | void BreakpointsDebuggerThread::Run() { |
| 5529 | const int kBufSize = 1000; |
| 5530 | uint16_t buffer[kBufSize]; |
| 5531 | |
| 5532 | const char* command_1 = "{\"seq\":101," |
| 5533 | "\"type\":\"request\"," |
| 5534 | "\"command\":\"setbreakpoint\"," |
| 5535 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5536 | const char* command_2 = "{\"seq\":102," |
| 5537 | "\"type\":\"request\"," |
| 5538 | "\"command\":\"setbreakpoint\"," |
| 5539 | "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5540 | const char* command_3; |
| 5541 | if (this->global_evaluate_) { |
| 5542 | command_3 = "{\"seq\":103," |
| 5543 | "\"type\":\"request\"," |
| 5544 | "\"command\":\"evaluate\"," |
| 5545 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," |
| 5546 | "\"global\":true}}"; |
| 5547 | } else { |
| 5548 | command_3 = "{\"seq\":103," |
| 5549 | "\"type\":\"request\"," |
| 5550 | "\"command\":\"evaluate\"," |
| 5551 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; |
| 5552 | } |
| 5553 | const char* command_4; |
| 5554 | if (this->global_evaluate_) { |
| 5555 | command_4 = "{\"seq\":104," |
| 5556 | "\"type\":\"request\"," |
| 5557 | "\"command\":\"evaluate\"," |
| 5558 | "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," |
| 5559 | "\"global\":true}}"; |
| 5560 | } else { |
| 5561 | command_4 = "{\"seq\":104," |
| 5562 | "\"type\":\"request\"," |
| 5563 | "\"command\":\"evaluate\"," |
| 5564 | "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; |
| 5565 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5566 | const char* command_5 = "{\"seq\":105," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5567 | "\"type\":\"request\"," |
| 5568 | "\"command\":\"continue\"}"; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5569 | const char* command_6 = "{\"seq\":106," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5570 | "\"type\":\"request\"," |
| 5571 | "\"command\":\"continue\"}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5572 | const char* command_7; |
| 5573 | if (this->global_evaluate_) { |
| 5574 | command_7 = "{\"seq\":107," |
| 5575 | "\"type\":\"request\"," |
| 5576 | "\"command\":\"evaluate\"," |
| 5577 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," |
| 5578 | "\"global\":true}}"; |
| 5579 | } else { |
| 5580 | command_7 = "{\"seq\":107," |
| 5581 | "\"type\":\"request\"," |
| 5582 | "\"command\":\"evaluate\"," |
| 5583 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; |
| 5584 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5585 | const char* command_8 = "{\"seq\":108," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5586 | "\"type\":\"request\"," |
| 5587 | "\"command\":\"continue\"}"; |
| 5588 | |
| 5589 | |
| 5590 | // v8 thread initializes, runs source_1 |
| 5591 | breakpoints_barriers->barrier_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5592 | // 1:Set breakpoint in cat() (will get id 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5593 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5594 | // 2:Set breakpoint in dog() (will get id 2). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5595 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5596 | breakpoints_barriers->barrier_2.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5597 | // V8 thread starts compiling source_2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5598 | // Automatic break happens, to run queued commands |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5599 | // breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5600 | // Commands 1 through 3 run, thread continues. |
| 5601 | // v8 thread runs source_2 to breakpoint in cat(). |
| 5602 | // message callback receives break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5603 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5604 | // Must have hit breakpoint #1. |
| 5605 | CHECK_EQ(1, break_event_breakpoint_id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5606 | // 4:Evaluate dog() (which has a breakpoint). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5607 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_3, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5608 | // V8 thread hits breakpoint in dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5609 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5610 | // Must have hit breakpoint #2. |
| 5611 | CHECK_EQ(2, break_event_breakpoint_id); |
| 5612 | // 5:Evaluate (x + 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5613 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_4, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5614 | // Evaluate (x + 1) finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5615 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5616 | // Must have result 108. |
| 5617 | CHECK_EQ(108, evaluate_int_result); |
| 5618 | // 6:Continue evaluation of dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5619 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_5, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5620 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5621 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5622 | // Must have result 107. |
| 5623 | CHECK_EQ(107, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5624 | // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint |
| 5625 | // in cat(19). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5626 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_6, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5627 | // Message callback gets break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5628 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5629 | // Must have hit breakpoint #1. |
| 5630 | CHECK_EQ(1, break_event_breakpoint_id); |
| 5631 | // 8: Evaluate dog() with breaks disabled. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5632 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_7, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5633 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5634 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5635 | // Must have result 116. |
| 5636 | CHECK_EQ(116, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5637 | // 9: Continue evaluation of source2, reach end. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5638 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_8, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5639 | breakpoints_barriers->barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame] | 5642 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5643 | void TestRecursiveBreakpointsGeneric(bool global_evaluate) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5644 | BreakpointsV8Thread breakpoints_v8_thread; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5645 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5646 | // Create a V8 environment |
| 5647 | Barriers stack_allocated_breakpoints_barriers; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5648 | breakpoints_barriers = &stack_allocated_breakpoints_barriers; |
| 5649 | |
| 5650 | breakpoints_v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5651 | breakpoints_barriers->barrier_3.Wait(); |
| 5652 | BreakpointsDebuggerThread breakpoints_debugger_thread( |
| 5653 | global_evaluate, breakpoints_v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5654 | breakpoints_debugger_thread.Start(); |
| 5655 | |
| 5656 | breakpoints_v8_thread.Join(); |
| 5657 | breakpoints_debugger_thread.Join(); |
| 5658 | } |
| 5659 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5660 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5661 | TEST(RecursiveBreakpoints) { |
| 5662 | TestRecursiveBreakpointsGeneric(false); |
| 5663 | } |
| 5664 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5665 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5666 | TEST(RecursiveBreakpointsGlobal) { |
| 5667 | TestRecursiveBreakpointsGeneric(true); |
| 5668 | } |
| 5669 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5670 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5671 | TEST(SetDebugEventListenerOnUninitializedVM) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5672 | v8::Debug::SetDebugEventListener(CcTest::isolate(), DummyDebugEventListener); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | |
| 5676 | static void DummyMessageHandler(const v8::Debug::Message& message) { |
| 5677 | } |
| 5678 | |
| 5679 | |
| 5680 | TEST(SetMessageHandlerOnUninitializedVM) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5681 | v8::Debug::SetMessageHandler(CcTest::isolate(), DummyMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | |
| 5685 | // Source for a JavaScript function which returns the data parameter of a |
| 5686 | // function called in the context of the debugger. If no data parameter is |
| 5687 | // passed it throws an exception. |
| 5688 | static const char* debugger_call_with_data_source = |
| 5689 | "function debugger_call_with_data(exec_state, data) {" |
| 5690 | " if (data) return data;" |
| 5691 | " throw 'No data!'" |
| 5692 | "}"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5693 | v8::Local<v8::Function> debugger_call_with_data; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5694 | |
| 5695 | |
| 5696 | // Source for a JavaScript function which returns the data parameter of a |
| 5697 | // function called in the context of the debugger. If no data parameter is |
| 5698 | // passed it throws an exception. |
| 5699 | static const char* debugger_call_with_closure_source = |
| 5700 | "var x = 3;" |
| 5701 | "(function (exec_state) {" |
| 5702 | " if (exec_state.y) return x - 1;" |
| 5703 | " exec_state.y = x;" |
| 5704 | " return exec_state.y" |
| 5705 | "})"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5706 | v8::Local<v8::Function> debugger_call_with_closure; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5707 | |
| 5708 | // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 5709 | // in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5710 | static void CheckFrameCount(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5711 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5712 | CHECK(v8::Debug::Call(context, frame_count).ToLocalChecked()->IsNumber()); |
| 5713 | CHECK_EQ(args[0]->Int32Value(context).FromJust(), |
| 5714 | v8::Debug::Call(context, frame_count) |
| 5715 | .ToLocalChecked() |
| 5716 | ->Int32Value(context) |
| 5717 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | |
| 5721 | // Function to retrieve the source line of the top JavaScript frame by calling a |
| 5722 | // JavaScript function in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5723 | static void CheckSourceLine(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5724 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5725 | CHECK( |
| 5726 | v8::Debug::Call(context, frame_source_line).ToLocalChecked()->IsNumber()); |
| 5727 | CHECK_EQ(args[0]->Int32Value(context).FromJust(), |
| 5728 | v8::Debug::Call(context, frame_source_line) |
| 5729 | .ToLocalChecked() |
| 5730 | ->Int32Value(context) |
| 5731 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | |
| 5735 | // Function to test passing an additional parameter to a JavaScript function |
| 5736 | // called in the debugger. It also tests that functions called in the debugger |
| 5737 | // can throw exceptions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5738 | static void CheckDataParameter( |
| 5739 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5740 | v8::Local<v8::String> data = v8_str(args.GetIsolate(), "Test"); |
| 5741 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5742 | CHECK(v8::Debug::Call(context, debugger_call_with_data, data) |
| 5743 | .ToLocalChecked() |
| 5744 | ->IsString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5745 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5746 | for (int i = 0; i < 3; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5747 | v8::TryCatch catcher(args.GetIsolate()); |
| 5748 | CHECK(v8::Debug::Call(context, debugger_call_with_data).IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5749 | CHECK(catcher.HasCaught()); |
| 5750 | CHECK(catcher.Exception()->IsString()); |
| 5751 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | |
| 5755 | // Function to test using a JavaScript with closure in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5756 | static void CheckClosure(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5757 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5758 | CHECK(v8::Debug::Call(context, debugger_call_with_closure) |
| 5759 | .ToLocalChecked() |
| 5760 | ->IsNumber()); |
| 5761 | CHECK_EQ(3, v8::Debug::Call(context, debugger_call_with_closure) |
| 5762 | .ToLocalChecked() |
| 5763 | ->Int32Value(context) |
| 5764 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | |
| 5768 | // Test functions called through the debugger. |
| 5769 | TEST(CallFunctionInDebugger) { |
| 5770 | // Create and enter a context with the functions CheckFrameCount, |
| 5771 | // CheckSourceLine and CheckDataParameter installed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5772 | v8::Isolate* isolate = CcTest::isolate(); |
| 5773 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5774 | v8::Local<v8::ObjectTemplate> global_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5775 | v8::ObjectTemplate::New(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5776 | global_template->Set(v8_str(isolate, "CheckFrameCount"), |
| 5777 | v8::FunctionTemplate::New(isolate, CheckFrameCount)); |
| 5778 | global_template->Set(v8_str(isolate, "CheckSourceLine"), |
| 5779 | v8::FunctionTemplate::New(isolate, CheckSourceLine)); |
| 5780 | global_template->Set(v8_str(isolate, "CheckDataParameter"), |
| 5781 | v8::FunctionTemplate::New(isolate, CheckDataParameter)); |
| 5782 | global_template->Set(v8_str(isolate, "CheckClosure"), |
| 5783 | v8::FunctionTemplate::New(isolate, CheckClosure)); |
| 5784 | v8::Local<v8::Context> context = |
| 5785 | v8::Context::New(isolate, NULL, global_template); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5786 | v8::Context::Scope context_scope(context); |
| 5787 | |
| 5788 | // Compile a function for checking the number of JavaScript frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5789 | v8::Script::Compile(context, v8_str(isolate, frame_count_source)) |
| 5790 | .ToLocalChecked() |
| 5791 | ->Run(context) |
| 5792 | .ToLocalChecked(); |
| 5793 | frame_count = v8::Local<v8::Function>::Cast( |
| 5794 | context->Global() |
| 5795 | ->Get(context, v8_str(isolate, "frame_count")) |
| 5796 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5797 | |
| 5798 | // Compile a function for returning the source line for the top frame. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5799 | v8::Script::Compile(context, v8_str(isolate, frame_source_line_source)) |
| 5800 | .ToLocalChecked() |
| 5801 | ->Run(context) |
| 5802 | .ToLocalChecked(); |
| 5803 | frame_source_line = v8::Local<v8::Function>::Cast( |
| 5804 | context->Global() |
| 5805 | ->Get(context, v8_str(isolate, "frame_source_line")) |
| 5806 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5807 | |
| 5808 | // Compile a function returning the data parameter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5809 | v8::Script::Compile(context, v8_str(isolate, debugger_call_with_data_source)) |
| 5810 | .ToLocalChecked() |
| 5811 | ->Run(context) |
| 5812 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5813 | debugger_call_with_data = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5814 | context->Global() |
| 5815 | ->Get(context, v8_str(isolate, "debugger_call_with_data")) |
| 5816 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5817 | |
| 5818 | // Compile a function capturing closure. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5819 | debugger_call_with_closure = v8::Local<v8::Function>::Cast( |
| 5820 | v8::Script::Compile(context, |
| 5821 | v8_str(isolate, debugger_call_with_closure_source)) |
| 5822 | .ToLocalChecked() |
| 5823 | ->Run(context) |
| 5824 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5825 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5826 | // Calling a function through the debugger returns 0 frames if there are |
| 5827 | // no JavaScript frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5828 | CHECK(v8::Integer::New(isolate, 0) |
| 5829 | ->Equals(context, |
| 5830 | v8::Debug::Call(context, frame_count).ToLocalChecked()) |
| 5831 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5832 | |
| 5833 | // Test that the number of frames can be retrieved. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5834 | v8::Script::Compile(context, v8_str(isolate, "CheckFrameCount(1)")) |
| 5835 | .ToLocalChecked() |
| 5836 | ->Run(context) |
| 5837 | .ToLocalChecked(); |
| 5838 | v8::Script::Compile(context, v8_str(isolate, |
| 5839 | "function f() {" |
| 5840 | " CheckFrameCount(2);" |
| 5841 | "}; f()")) |
| 5842 | .ToLocalChecked() |
| 5843 | ->Run(context) |
| 5844 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5845 | |
| 5846 | // Test that the source line can be retrieved. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5847 | v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(0)")) |
| 5848 | .ToLocalChecked() |
| 5849 | ->Run(context) |
| 5850 | .ToLocalChecked(); |
| 5851 | v8::Script::Compile(context, v8_str(isolate, |
| 5852 | "function f() {\n" |
| 5853 | " CheckSourceLine(1)\n" |
| 5854 | " CheckSourceLine(2)\n" |
| 5855 | " CheckSourceLine(3)\n" |
| 5856 | "}; f()")) |
| 5857 | .ToLocalChecked() |
| 5858 | ->Run(context) |
| 5859 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5860 | |
| 5861 | // Test that a parameter can be passed to a function called in the debugger. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5862 | v8::Script::Compile(context, v8_str(isolate, "CheckDataParameter()")) |
| 5863 | .ToLocalChecked() |
| 5864 | ->Run(context) |
| 5865 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5866 | |
| 5867 | // Test that a function with closure can be run in the debugger. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5868 | v8::Script::Compile(context, v8_str(isolate, "CheckClosure()")) |
| 5869 | .ToLocalChecked() |
| 5870 | ->Run(context) |
| 5871 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5872 | |
| 5873 | // Test that the source line is correct when there is a line offset. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5874 | v8::ScriptOrigin origin(v8_str(isolate, "test"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5875 | v8::Integer::New(isolate, 7)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5876 | v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(7)"), &origin) |
| 5877 | .ToLocalChecked() |
| 5878 | ->Run(context) |
| 5879 | .ToLocalChecked(); |
| 5880 | v8::Script::Compile(context, v8_str(isolate, |
| 5881 | "function f() {\n" |
| 5882 | " CheckSourceLine(8)\n" |
| 5883 | " CheckSourceLine(9)\n" |
| 5884 | " CheckSourceLine(10)\n" |
| 5885 | "}; f()"), |
| 5886 | &origin) |
| 5887 | .ToLocalChecked() |
| 5888 | ->Run(context) |
| 5889 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | |
| 5893 | // Debugger message handler which counts the number of breaks. |
| 5894 | static void SendContinueCommand(); |
| 5895 | static void MessageHandlerBreakPointHitCount( |
| 5896 | const v8::Debug::Message& message) { |
| 5897 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 5898 | // Count the number of breaks. |
| 5899 | break_point_hit_count++; |
| 5900 | |
| 5901 | SendContinueCommand(); |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | |
| 5906 | // Test that clearing the debug event listener actually clears all break points |
| 5907 | // and related information. |
| 5908 | TEST(DebuggerUnload) { |
| 5909 | DebugLocalContext env; |
| 5910 | |
| 5911 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5912 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5913 | |
| 5914 | // Set a debug event listener. |
| 5915 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5916 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 5917 | DebugEventBreakPointHitCount); |
| 5918 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5919 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5920 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5921 | // Create a couple of functions for the test. |
| 5922 | v8::Local<v8::Function> foo = |
| 5923 | CompileFunction(&env, "function foo(){x=1}", "foo"); |
| 5924 | v8::Local<v8::Function> bar = |
| 5925 | CompileFunction(&env, "function bar(){y=2}", "bar"); |
| 5926 | |
| 5927 | // Set some break points. |
| 5928 | SetBreakPoint(foo, 0); |
| 5929 | SetBreakPoint(foo, 4); |
| 5930 | SetBreakPoint(bar, 0); |
| 5931 | SetBreakPoint(bar, 4); |
| 5932 | |
| 5933 | // Make sure that the break points are there. |
| 5934 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5935 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5936 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5937 | bar->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5938 | CHECK_EQ(4, break_point_hit_count); |
| 5939 | } |
| 5940 | |
| 5941 | // Remove the debug event listener without clearing breakpoints. Do this |
| 5942 | // outside a handle scope. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5943 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 5944 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Now set a debug message handler. |
| 5947 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5948 | v8::Debug::SetMessageHandler(env->GetIsolate(), |
| 5949 | MessageHandlerBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5950 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5951 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5952 | |
| 5953 | // Get the test functions again. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5954 | v8::Local<v8::Function> foo(v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5955 | env->Global() |
| 5956 | ->Get(context, v8_str(env->GetIsolate(), "foo")) |
| 5957 | .ToLocalChecked())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5958 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5959 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5960 | CHECK_EQ(0, break_point_hit_count); |
| 5961 | |
| 5962 | // Set break points and run again. |
| 5963 | SetBreakPoint(foo, 0); |
| 5964 | SetBreakPoint(foo, 4); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5965 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5966 | CHECK_EQ(2, break_point_hit_count); |
| 5967 | } |
| 5968 | |
| 5969 | // Remove the debug message handler without clearing breakpoints. Do this |
| 5970 | // outside a handle scope. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5971 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 5972 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | |
| 5976 | // Sends continue command to the debugger. |
| 5977 | static void SendContinueCommand() { |
| 5978 | const int kBufferSize = 1000; |
| 5979 | uint16_t buffer[kBufferSize]; |
| 5980 | const char* command_continue = |
| 5981 | "{\"seq\":0," |
| 5982 | "\"type\":\"request\"," |
| 5983 | "\"command\":\"continue\"}"; |
| 5984 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5985 | v8::Debug::SendCommand( |
| 5986 | CcTest::isolate(), buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
| 5989 | |
| 5990 | // Debugger message handler which counts the number of times it is called. |
| 5991 | static int message_handler_hit_count = 0; |
| 5992 | static void MessageHandlerHitCount(const v8::Debug::Message& message) { |
| 5993 | message_handler_hit_count++; |
| 5994 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5995 | static char print_buffer[1000]; |
| 5996 | v8::String::Value json(message.GetJSON()); |
| 5997 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5998 | if (IsExceptionEventMessage(print_buffer)) { |
| 5999 | // Send a continue command for exception events. |
| 6000 | SendContinueCommand(); |
| 6001 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | |
| 6005 | // Test clearing the debug message handler. |
| 6006 | TEST(DebuggerClearMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6007 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6008 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6009 | |
| 6010 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6011 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6012 | |
| 6013 | // Set a debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6014 | v8::Debug::SetMessageHandler(env->GetIsolate(), MessageHandlerHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6015 | |
| 6016 | // Run code to throw a unhandled exception. This should end up in the message |
| 6017 | // handler. |
| 6018 | CompileRun("throw 1"); |
| 6019 | |
| 6020 | // The message handler should be called. |
| 6021 | CHECK_GT(message_handler_hit_count, 0); |
| 6022 | |
| 6023 | // Clear debug message handler. |
| 6024 | message_handler_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6025 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6026 | |
| 6027 | // Run code to throw a unhandled exception. This should end up in the message |
| 6028 | // handler. |
| 6029 | CompileRun("throw 1"); |
| 6030 | |
| 6031 | // The message handler should not be called more. |
| 6032 | CHECK_EQ(0, message_handler_hit_count); |
| 6033 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6034 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | |
| 6038 | // Debugger message handler which clears the message handler while active. |
| 6039 | static void MessageHandlerClearingMessageHandler( |
| 6040 | const v8::Debug::Message& message) { |
| 6041 | message_handler_hit_count++; |
| 6042 | |
| 6043 | // Clear debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6044 | v8::Debug::SetMessageHandler(message.GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | |
| 6048 | // Test clearing the debug message handler while processing a debug event. |
| 6049 | TEST(DebuggerClearMessageHandlerWhileActive) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6050 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6051 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6052 | |
| 6053 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6054 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6055 | |
| 6056 | // Set a debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6057 | v8::Debug::SetMessageHandler(env->GetIsolate(), |
| 6058 | MessageHandlerClearingMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6059 | |
| 6060 | // Run code to throw a unhandled exception. This should end up in the message |
| 6061 | // handler. |
| 6062 | CompileRun("throw 1"); |
| 6063 | |
| 6064 | // The message handler should be called. |
| 6065 | CHECK_EQ(1, message_handler_hit_count); |
| 6066 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6067 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6071 | // Test for issue http://code.google.com/p/v8/issues/detail?id=289. |
| 6072 | // Make sure that DebugGetLoadedScripts doesn't return scripts |
| 6073 | // with disposed external source. |
| 6074 | class EmptyExternalStringResource : public v8::String::ExternalStringResource { |
| 6075 | public: |
| 6076 | EmptyExternalStringResource() { empty_[0] = 0; } |
| 6077 | virtual ~EmptyExternalStringResource() {} |
| 6078 | virtual size_t length() const { return empty_.length(); } |
| 6079 | virtual const uint16_t* data() const { return empty_.start(); } |
| 6080 | private: |
| 6081 | ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; |
| 6082 | }; |
| 6083 | |
| 6084 | |
| 6085 | TEST(DebugGetLoadedScripts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6086 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6087 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6088 | env.ExposeDebug(); |
| 6089 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6090 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6091 | EmptyExternalStringResource source_ext_str; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6092 | v8::Local<v8::String> source = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6093 | v8::String::NewExternalTwoByte(env->GetIsolate(), &source_ext_str) |
| 6094 | .ToLocalChecked(); |
| 6095 | CHECK(v8::Script::Compile(context, source).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6096 | Handle<i::ExternalTwoByteString> i_source( |
| 6097 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 6098 | // This situation can happen if source was an external string disposed |
| 6099 | // by its owner. |
| 6100 | i_source->set_resource(0); |
| 6101 | |
| 6102 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 6103 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6104 | EnableDebugger(env->GetIsolate()); |
| 6105 | v8::MaybeLocal<v8::Value> result = |
| 6106 | CompileRun(env.context(), |
| 6107 | "var scripts = %DebugGetLoadedScripts();" |
| 6108 | "var count = scripts.length;" |
| 6109 | "for (var i = 0; i < count; ++i) {" |
| 6110 | " var lines = scripts[i].lineCount();" |
| 6111 | " if (lines < 1) throw 'lineCount';" |
| 6112 | " var last = -1;" |
| 6113 | " for (var j = 0; j < lines; ++j) {" |
| 6114 | " var end = scripts[i].lineEnd(j);" |
| 6115 | " if (last >= end) throw 'lineEnd';" |
| 6116 | " last = end;" |
| 6117 | " }" |
| 6118 | "}"); |
| 6119 | CHECK(!result.IsEmpty()); |
| 6120 | DisableDebugger(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6121 | // Must not crash while accessing line_ends. |
| 6122 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
| 6123 | |
| 6124 | // Some scripts are retrieved - at least the number of native scripts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6125 | CHECK_GT(env->Global() |
| 6126 | ->Get(context, v8_str(env->GetIsolate(), "count")) |
| 6127 | .ToLocalChecked() |
| 6128 | ->Int32Value(context) |
| 6129 | .FromJust(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6130 | 8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | |
| 6134 | // Test script break points set on lines. |
| 6135 | TEST(ScriptNameAndData) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6136 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6137 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6138 | env.ExposeDebug(); |
| 6139 | |
| 6140 | // Create functions for retrieving script name and data for the function on |
| 6141 | // the top frame when hitting a break point. |
| 6142 | frame_script_name = CompileFunction(&env, |
| 6143 | frame_script_name_source, |
| 6144 | "frame_script_name"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6145 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6146 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 6147 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6149 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6150 | // Test function source. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6151 | v8::Local<v8::String> script = v8_str(env->GetIsolate(), |
| 6152 | "function f() {\n" |
| 6153 | " debugger;\n" |
| 6154 | "}\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6155 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6156 | v8::ScriptOrigin origin1 = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6157 | v8::ScriptOrigin(v8_str(env->GetIsolate(), "name")); |
| 6158 | v8::Local<v8::Script> script1 = |
| 6159 | v8::Script::Compile(context, script, &origin1).ToLocalChecked(); |
| 6160 | script1->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6161 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6162 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6163 | env->Global() |
| 6164 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6165 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6166 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6167 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6168 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6169 | CHECK_EQ(0, strcmp("name", last_script_name_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6170 | |
| 6171 | // Compile the same script again without setting data. As the compilation |
| 6172 | // cache is disabled when debugging expect the data to be missing. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6173 | v8::Script::Compile(context, script, &origin1) |
| 6174 | .ToLocalChecked() |
| 6175 | ->Run(context) |
| 6176 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6177 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6178 | env->Global() |
| 6179 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6180 | .ToLocalChecked()); |
| 6181 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6182 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6183 | CHECK_EQ(0, strcmp("name", last_script_name_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6184 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6185 | v8::Local<v8::String> data_obj_source = |
| 6186 | v8_str(env->GetIsolate(), |
| 6187 | "({ a: 'abc',\n" |
| 6188 | " b: 123,\n" |
| 6189 | " toString: function() { return this.a + ' ' + this.b; }\n" |
| 6190 | "})\n"); |
| 6191 | v8::Script::Compile(context, data_obj_source) |
| 6192 | .ToLocalChecked() |
| 6193 | ->Run(context) |
| 6194 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6195 | v8::ScriptOrigin origin2 = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6196 | v8::ScriptOrigin(v8_str(env->GetIsolate(), "new name")); |
| 6197 | v8::Local<v8::Script> script2 = |
| 6198 | v8::Script::Compile(context, script, &origin2).ToLocalChecked(); |
| 6199 | script2->Run(context).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6200 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6201 | env->Global() |
| 6202 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6203 | .ToLocalChecked()); |
| 6204 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6205 | CHECK_EQ(3, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6206 | CHECK_EQ(0, strcmp("new name", last_script_name_hit)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6207 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6208 | v8::Local<v8::Script> script3 = |
| 6209 | v8::Script::Compile(context, script, &origin2).ToLocalChecked(); |
| 6210 | script3->Run(context).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6211 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6212 | env->Global() |
| 6213 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6214 | .ToLocalChecked()); |
| 6215 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6216 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6220 | static v8::Local<v8::Context> expected_context; |
| 6221 | static v8::Local<v8::Value> expected_context_data; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6222 | |
| 6223 | |
| 6224 | // Check that the expected context is the one generating the debug event. |
| 6225 | static void ContextCheckMessageHandler(const v8::Debug::Message& message) { |
| 6226 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6227 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6228 | expected_context_data)); |
| 6229 | message_handler_hit_count++; |
| 6230 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6231 | static char print_buffer[1000]; |
| 6232 | v8::String::Value json(message.GetJSON()); |
| 6233 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6235 | // Send a continue command for break events. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6236 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6237 | SendContinueCommand(); |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | |
| 6242 | // Test which creates two contexts and sets different embedder data on each. |
| 6243 | // Checks that this data is set correctly and that when the debug message |
| 6244 | // handler is called the expected context is the one active. |
| 6245 | TEST(ContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6246 | v8::Isolate* isolate = CcTest::isolate(); |
| 6247 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6248 | |
| 6249 | // Create two contexts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6250 | v8::Local<v8::Context> context_1; |
| 6251 | v8::Local<v8::Context> context_2; |
| 6252 | v8::Local<v8::ObjectTemplate> global_template = |
| 6253 | v8::Local<v8::ObjectTemplate>(); |
| 6254 | v8::Local<v8::Value> global_object = v8::Local<v8::Value>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6255 | context_1 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6256 | context_2 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6257 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6258 | v8::Debug::SetMessageHandler(isolate, ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6259 | |
| 6260 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6261 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
| 6262 | CHECK(context_2->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6263 | |
| 6264 | // Set and check different data values. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6265 | v8::Local<v8::String> data_1 = v8_str(isolate, "1"); |
| 6266 | v8::Local<v8::String> data_2 = v8_str(isolate, "2"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6267 | context_1->SetEmbedderData(0, data_1); |
| 6268 | context_2->SetEmbedderData(0, data_2); |
| 6269 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
| 6270 | CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6271 | |
| 6272 | // Simple test function which causes a break. |
| 6273 | const char* source = "function f() { debugger; }"; |
| 6274 | |
| 6275 | // Enter and run function in the first context. |
| 6276 | { |
| 6277 | v8::Context::Scope context_scope(context_1); |
| 6278 | expected_context = context_1; |
| 6279 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6280 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6281 | f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | |
| 6285 | // Enter and run function in the second context. |
| 6286 | { |
| 6287 | v8::Context::Scope context_scope(context_2); |
| 6288 | expected_context = context_2; |
| 6289 | expected_context_data = data_2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6290 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6291 | f->Call(context_2, context_2->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | // Two times compile event and two times break event. |
| 6295 | CHECK_GT(message_handler_hit_count, 4); |
| 6296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6297 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6298 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | |
| 6302 | // Debug message handler which issues a debug break when it hits a break event. |
| 6303 | static int message_handler_break_hit_count = 0; |
| 6304 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
| 6305 | // Schedule a debug break for break events. |
| 6306 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6307 | message_handler_break_hit_count++; |
| 6308 | if (message_handler_break_hit_count == 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6309 | v8::Debug::DebugBreak(message.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | // Issue a continue command if this event will not cause the VM to start |
| 6314 | // running. |
| 6315 | if (!message.WillStartRunning()) { |
| 6316 | SendContinueCommand(); |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | |
| 6321 | // Test that a debug break can be scheduled while in a message handler. |
| 6322 | TEST(DebugBreakInMessageHandler) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6323 | i::FLAG_turbo_inlining = false; // Make sure g is not inlined into f. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6324 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6325 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6326 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6327 | v8::Debug::SetMessageHandler(env->GetIsolate(), DebugBreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6328 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6329 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6330 | // Test functions. |
| 6331 | const char* script = "function f() { debugger; g(); } function g() { }"; |
| 6332 | CompileRun(script); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6333 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6334 | env->Global() |
| 6335 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6336 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6337 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6338 | env->Global() |
| 6339 | ->Get(context, v8_str(env->GetIsolate(), "g")) |
| 6340 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6341 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6342 | // Call f then g. The debugger statement in f will cause a break which will |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6343 | // cause another break. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6344 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6345 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6346 | // Calling g will not cause any additional breaks. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6347 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6348 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6349 | } |
| 6350 | |
| 6351 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6352 | #ifndef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6353 | // Debug event handler which gets the function on the top frame and schedules a |
| 6354 | // break a number of times. |
| 6355 | static void DebugEventDebugBreak( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6356 | const v8::Debug::EventDetails& event_details) { |
| 6357 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6358 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 6359 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6360 | if (event == v8::Break) { |
| 6361 | break_point_hit_count++; |
| 6362 | |
| 6363 | // Get the name of the top frame function. |
| 6364 | if (!frame_function_name.IsEmpty()) { |
| 6365 | // Get the name of the function. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6366 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6367 | v8::Local<v8::Value> argv[argc] = { |
| 6368 | exec_state, v8::Integer::New(CcTest::isolate(), 0)}; |
| 6369 | v8::Local<v8::Value> result = |
| 6370 | frame_function_name->Call(context, exec_state, argc, argv) |
| 6371 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6372 | if (result->IsUndefined()) { |
| 6373 | last_function_hit[0] = '\0'; |
| 6374 | } else { |
| 6375 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6376 | v8::Local<v8::String> function_name( |
| 6377 | result->ToString(context).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6378 | function_name->WriteUtf8(last_function_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | // Keep forcing breaks. |
| 6383 | if (break_point_hit_count < 20) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6384 | v8::Debug::DebugBreak(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6385 | } |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | |
| 6390 | TEST(RegExpDebugBreak) { |
| 6391 | // This test only applies to native regexps. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6392 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6393 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6394 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6395 | // Create a function for checking the function when hitting a break point. |
| 6396 | frame_function_name = CompileFunction(&env, |
| 6397 | frame_function_name_source, |
| 6398 | "frame_function_name"); |
| 6399 | |
| 6400 | // Test RegExp which matches white spaces and comments at the begining of a |
| 6401 | // source line. |
| 6402 | const char* script = |
| 6403 | "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" |
| 6404 | "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; |
| 6405 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6406 | v8::Local<v8::Function> f = CompileFunction(env->GetIsolate(), script, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6407 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6408 | v8::Local<v8::Value> argv[argc] = { |
| 6409 | v8_str(env->GetIsolate(), " /* xxx */ a=0;")}; |
| 6410 | v8::Local<v8::Value> result = |
| 6411 | f->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
| 6412 | CHECK_EQ(12, result->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6413 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6414 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventDebugBreak); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6415 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6416 | result = f->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6417 | |
| 6418 | // Check that there was only one break event. Matching RegExp should not |
| 6419 | // cause Break events. |
| 6420 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6421 | CHECK_EQ(0, strcmp("f", last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6422 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6423 | #endif // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6424 | |
| 6425 | |
| 6426 | // Common part of EvalContextData and NestedBreakEventContextData tests. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6427 | static void ExecuteScriptForContextCheck( |
| 6428 | v8::Debug::MessageHandler message_handler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6429 | // Create a context. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6430 | v8::Local<v8::Context> context_1; |
| 6431 | v8::Local<v8::ObjectTemplate> global_template = |
| 6432 | v8::Local<v8::ObjectTemplate>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6433 | context_1 = |
| 6434 | v8::Context::New(CcTest::isolate(), NULL, global_template); |
| 6435 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6436 | v8::Debug::SetMessageHandler(CcTest::isolate(), message_handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6437 | |
| 6438 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6439 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6440 | |
| 6441 | // Set and check a data value. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6442 | v8::Local<v8::String> data_1 = v8_str(CcTest::isolate(), "1"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6443 | context_1->SetEmbedderData(0, data_1); |
| 6444 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6445 | |
| 6446 | // Simple test function with eval that causes a break. |
| 6447 | const char* source = "function f() { eval('debugger;'); }"; |
| 6448 | |
| 6449 | // Enter and run function in the context. |
| 6450 | { |
| 6451 | v8::Context::Scope context_scope(context_1); |
| 6452 | expected_context = context_1; |
| 6453 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6454 | v8::Local<v8::Function> f = CompileFunction(CcTest::isolate(), source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6455 | f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6456 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6458 | v8::Debug::SetMessageHandler(CcTest::isolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
| 6461 | |
| 6462 | // Test which creates a context and sets embedder data on it. Checks that this |
| 6463 | // data is set correctly and that when the debug message handler is called for |
| 6464 | // break event in an eval statement the expected context is the one returned by |
| 6465 | // Message.GetEventContext. |
| 6466 | TEST(EvalContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6467 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6468 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6469 | ExecuteScriptForContextCheck(ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6470 | |
| 6471 | // One time compile event and one time break event. |
| 6472 | CHECK_GT(message_handler_hit_count, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6473 | CheckDebuggerUnloaded(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | |
| 6477 | static bool sent_eval = false; |
| 6478 | static int break_count = 0; |
| 6479 | static int continue_command_send_count = 0; |
| 6480 | // Check that the expected context is the one generating the debug event |
| 6481 | // including the case of nested break event. |
| 6482 | static void DebugEvalContextCheckMessageHandler( |
| 6483 | const v8::Debug::Message& message) { |
| 6484 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6485 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6486 | expected_context_data)); |
| 6487 | message_handler_hit_count++; |
| 6488 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6489 | static char print_buffer[1000]; |
| 6490 | v8::String::Value json(message.GetJSON()); |
| 6491 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6492 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6493 | v8::Isolate* isolate = message.GetIsolate(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6494 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6495 | break_count++; |
| 6496 | if (!sent_eval) { |
| 6497 | sent_eval = true; |
| 6498 | |
| 6499 | const int kBufferSize = 1000; |
| 6500 | uint16_t buffer[kBufferSize]; |
| 6501 | const char* eval_command = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6502 | "{\"seq\":0," |
| 6503 | "\"type\":\"request\"," |
| 6504 | "\"command\":\"evaluate\"," |
| 6505 | "\"arguments\":{\"expression\":\"debugger;\"," |
| 6506 | "\"global\":true,\"disable_break\":false}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6507 | |
| 6508 | // Send evaluate command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6509 | v8::Debug::SendCommand( |
| 6510 | isolate, buffer, AsciiToUtf16(eval_command, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6511 | return; |
| 6512 | } else { |
| 6513 | // It's a break event caused by the evaluation request above. |
| 6514 | SendContinueCommand(); |
| 6515 | continue_command_send_count++; |
| 6516 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6517 | } else if (IsEvaluateResponseMessage(print_buffer) && |
| 6518 | continue_command_send_count < 2) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6519 | // Response to the evaluation request. We're still on the breakpoint so |
| 6520 | // send continue. |
| 6521 | SendContinueCommand(); |
| 6522 | continue_command_send_count++; |
| 6523 | } |
| 6524 | } |
| 6525 | |
| 6526 | |
| 6527 | // Tests that context returned for break event is correct when the event occurs |
| 6528 | // in 'evaluate' debugger request. |
| 6529 | TEST(NestedBreakEventContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6530 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6531 | break_count = 0; |
| 6532 | message_handler_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6533 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6534 | ExecuteScriptForContextCheck(DebugEvalContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6535 | |
| 6536 | // One time compile event and two times break event. |
| 6537 | CHECK_GT(message_handler_hit_count, 3); |
| 6538 | |
| 6539 | // One break from the source and another from the evaluate request. |
| 6540 | CHECK_EQ(break_count, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6541 | CheckDebuggerUnloaded(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6545 | // Debug event listener which counts the after compile events. |
| 6546 | int after_compile_message_count = 0; |
| 6547 | static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 6548 | // Count the number of scripts collected. |
| 6549 | if (message.IsEvent()) { |
| 6550 | if (message.GetEvent() == v8::AfterCompile) { |
| 6551 | after_compile_message_count++; |
| 6552 | } else if (message.GetEvent() == v8::Break) { |
| 6553 | SendContinueCommand(); |
| 6554 | } |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | |
| 6559 | // Tests that after compile event is sent as many times as there are scripts |
| 6560 | // compiled. |
| 6561 | TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6562 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6563 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6564 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6565 | after_compile_message_count = 0; |
| 6566 | const char* script = "var a=1"; |
| 6567 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6568 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6569 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6570 | .ToLocalChecked() |
| 6571 | ->Run(context) |
| 6572 | .ToLocalChecked(); |
| 6573 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6574 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6575 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6576 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6577 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6578 | .ToLocalChecked() |
| 6579 | ->Run(context) |
| 6580 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6581 | |
| 6582 | // Setting listener to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6583 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6584 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6585 | |
| 6586 | // Compilation cache should be disabled when debugger is active. |
| 6587 | CHECK_EQ(2, after_compile_message_count); |
| 6588 | } |
| 6589 | |
| 6590 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6591 | // Syntax error event handler which counts a number of events. |
| 6592 | int compile_error_event_count = 0; |
| 6593 | |
| 6594 | static void CompileErrorEventCounterClear() { |
| 6595 | compile_error_event_count = 0; |
| 6596 | } |
| 6597 | |
| 6598 | static void CompileErrorEventCounter( |
| 6599 | const v8::Debug::EventDetails& event_details) { |
| 6600 | v8::DebugEvent event = event_details.GetEvent(); |
| 6601 | |
| 6602 | if (event == v8::CompileError) { |
| 6603 | compile_error_event_count++; |
| 6604 | } |
| 6605 | } |
| 6606 | |
| 6607 | |
| 6608 | // Tests that syntax error event is sent as many times as there are scripts |
| 6609 | // with syntax error compiled. |
| 6610 | TEST(SyntaxErrorMessageOnSyntaxException) { |
| 6611 | DebugLocalContext env; |
| 6612 | v8::HandleScope scope(env->GetIsolate()); |
| 6613 | |
| 6614 | // For this test, we want to break on uncaught exceptions: |
| 6615 | ChangeBreakOnException(false, true); |
| 6616 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6617 | v8::Debug::SetDebugEventListener(env->GetIsolate(), CompileErrorEventCounter); |
| 6618 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6619 | |
| 6620 | CompileErrorEventCounterClear(); |
| 6621 | |
| 6622 | // Check initial state. |
| 6623 | CHECK_EQ(0, compile_error_event_count); |
| 6624 | |
| 6625 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6626 | CHECK( |
| 6627 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6628 | CHECK_EQ(1, compile_error_event_count); |
| 6629 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6630 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "/sel\\/: \\")) |
| 6631 | .IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6632 | CHECK_EQ(2, compile_error_event_count); |
| 6633 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6634 | v8::Local<v8::Script> script = |
| 6635 | v8::Script::Compile(context, |
| 6636 | v8_str(env->GetIsolate(), "JSON.parse('1234:')")) |
| 6637 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6638 | CHECK_EQ(2, compile_error_event_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6639 | CHECK(script->Run(context).IsEmpty()); |
| 6640 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6642 | v8::Script::Compile(context, |
| 6643 | v8_str(env->GetIsolate(), "new RegExp('/\\/\\\\');")) |
| 6644 | .ToLocalChecked(); |
| 6645 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6646 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6647 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "throw 1;")) |
| 6648 | .ToLocalChecked(); |
| 6649 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6653 | // Tests that break event is sent when message handler is reset. |
| 6654 | TEST(BreakMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6655 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6656 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6657 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6658 | after_compile_message_count = 0; |
| 6659 | const char* script = "function f() {};"; |
| 6660 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6661 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6662 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6663 | .ToLocalChecked() |
| 6664 | ->Run(context) |
| 6665 | .ToLocalChecked(); |
| 6666 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6667 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6668 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6669 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 6670 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6671 | env->Global() |
| 6672 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6673 | .ToLocalChecked()); |
| 6674 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6675 | |
| 6676 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6677 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6678 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6679 | |
| 6680 | // Compilation cache should be disabled when debugger is active. |
| 6681 | CHECK_EQ(1, after_compile_message_count); |
| 6682 | } |
| 6683 | |
| 6684 | |
| 6685 | static int exception_event_count = 0; |
| 6686 | static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 6687 | if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 6688 | exception_event_count++; |
| 6689 | SendContinueCommand(); |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | |
| 6694 | // Tests that exception event is sent when message handler is reset. |
| 6695 | TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6696 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6697 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 6698 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6699 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 6700 | // For this test, we want to break on uncaught exceptions: |
| 6701 | ChangeBreakOnException(false, true); |
| 6702 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6703 | exception_event_count = 0; |
| 6704 | const char* script = "function f() {throw new Error()};"; |
| 6705 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6706 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6707 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6708 | .ToLocalChecked() |
| 6709 | ->Run(context) |
| 6710 | .ToLocalChecked(); |
| 6711 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6712 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6713 | v8::Debug::SetMessageHandler(env->GetIsolate(), ExceptionMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6714 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6715 | env->Global() |
| 6716 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6717 | .ToLocalChecked()); |
| 6718 | CHECK(f->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6719 | |
| 6720 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6721 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6722 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6723 | |
| 6724 | CHECK_EQ(1, exception_event_count); |
| 6725 | } |
| 6726 | |
| 6727 | |
| 6728 | // Tests after compile event is sent when there are some provisional |
| 6729 | // breakpoints out of the scripts lines range. |
| 6730 | TEST(ProvisionalBreakpointOnLineOutOfRange) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6731 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6732 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6733 | env.ExposeDebug(); |
| 6734 | const char* script = "function f() {};"; |
| 6735 | const char* resource_name = "test_resource"; |
| 6736 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6737 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6738 | v8::Local<v8::Context> context = env.context(); |
| 6739 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6740 | // Set a couple of provisional breakpoint on lines out of the script lines |
| 6741 | // range. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6742 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, |
| 6743 | 3, -1 /* no column */); |
| 6744 | int sbp2 = |
| 6745 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, 5, 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6746 | |
| 6747 | after_compile_message_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6748 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6749 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), resource_name), |
| 6750 | v8::Integer::New(env->GetIsolate(), 10), |
| 6751 | v8::Integer::New(env->GetIsolate(), 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6752 | // Compile a script whose first line number is greater than the breakpoints' |
| 6753 | // lines. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6754 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script), &origin) |
| 6755 | .ToLocalChecked() |
| 6756 | ->Run(context) |
| 6757 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6758 | |
| 6759 | // If the script is compiled successfully there is exactly one after compile |
| 6760 | // event. In case of an exception in debugger code after compile event is not |
| 6761 | // sent. |
| 6762 | CHECK_EQ(1, after_compile_message_count); |
| 6763 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6764 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 6765 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6766 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6767 | } |
| 6768 | |
| 6769 | |
| 6770 | static void BreakMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6771 | i::Isolate* isolate = CcTest::i_isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6772 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6773 | // Count the number of breaks. |
| 6774 | break_point_hit_count++; |
| 6775 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6776 | i::HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6777 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6778 | |
| 6779 | SendContinueCommand(); |
| 6780 | } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6781 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6782 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6783 | int current_count = break_point_hit_count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6784 | |
| 6785 | // Force serialization to trigger some internal JS execution. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6786 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6787 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6788 | CHECK_EQ(current_count, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | |
| 6793 | // Test that if DebugBreak is forced it is ignored when code from |
| 6794 | // debug-delay.js is executed. |
| 6795 | TEST(NoDebugBreakInAfterCompileMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6796 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6797 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6798 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6799 | |
| 6800 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6801 | v8::Debug::SetMessageHandler(env->GetIsolate(), BreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6802 | |
| 6803 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6804 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6805 | |
| 6806 | // Create a function for testing stepping. |
| 6807 | const char* src = "function f() { eval('var x = 10;'); } "; |
| 6808 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 6809 | |
| 6810 | // There should be only one break event. |
| 6811 | CHECK_EQ(1, break_point_hit_count); |
| 6812 | |
| 6813 | // Set the debug break flag again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6814 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6815 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6816 | // There should be one more break event when the script is evaluated in 'f'. |
| 6817 | CHECK_EQ(2, break_point_hit_count); |
| 6818 | |
| 6819 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6820 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6821 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6822 | } |
| 6823 | |
| 6824 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6825 | static int counting_message_handler_counter; |
| 6826 | |
| 6827 | static void CountingMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6828 | if (message.IsResponse()) counting_message_handler_counter++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6831 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6832 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6833 | TEST(ProcessDebugMessages) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6834 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6835 | v8::Isolate* isolate = env->GetIsolate(); |
| 6836 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6837 | |
| 6838 | counting_message_handler_counter = 0; |
| 6839 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6840 | v8::Debug::SetMessageHandler(isolate, CountingMessageHandler); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6841 | |
| 6842 | const int kBufferSize = 1000; |
| 6843 | uint16_t buffer[kBufferSize]; |
| 6844 | const char* scripts_command = |
| 6845 | "{\"seq\":0," |
| 6846 | "\"type\":\"request\"," |
| 6847 | "\"command\":\"scripts\"}"; |
| 6848 | |
| 6849 | // Send scripts command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6850 | v8::Debug::SendCommand( |
| 6851 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6852 | |
| 6853 | CHECK_EQ(0, counting_message_handler_counter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6854 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6855 | // At least one message should come |
| 6856 | CHECK_GE(counting_message_handler_counter, 1); |
| 6857 | |
| 6858 | counting_message_handler_counter = 0; |
| 6859 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6860 | v8::Debug::SendCommand( |
| 6861 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6862 | v8::Debug::SendCommand( |
| 6863 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6864 | CHECK_EQ(0, counting_message_handler_counter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6865 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6866 | // At least two messages should come |
| 6867 | CHECK_GE(counting_message_handler_counter, 2); |
| 6868 | |
| 6869 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6870 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6871 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6872 | } |
| 6873 | |
| 6874 | |
| 6875 | class SendCommandThread; |
| 6876 | static SendCommandThread* send_command_thread_ = NULL; |
| 6877 | |
| 6878 | |
| 6879 | class SendCommandThread : public v8::base::Thread { |
| 6880 | public: |
| 6881 | explicit SendCommandThread(v8::Isolate* isolate) |
| 6882 | : Thread(Options("SendCommandThread")), |
| 6883 | semaphore_(0), |
| 6884 | isolate_(isolate) {} |
| 6885 | |
| 6886 | static void CountingAndSignallingMessageHandler( |
| 6887 | const v8::Debug::Message& message) { |
| 6888 | if (message.IsResponse()) { |
| 6889 | counting_message_handler_counter++; |
| 6890 | send_command_thread_->semaphore_.Signal(); |
| 6891 | } |
| 6892 | } |
| 6893 | |
| 6894 | virtual void Run() { |
| 6895 | semaphore_.Wait(); |
| 6896 | const int kBufferSize = 1000; |
| 6897 | uint16_t buffer[kBufferSize]; |
| 6898 | const char* scripts_command = |
| 6899 | "{\"seq\":0," |
| 6900 | "\"type\":\"request\"," |
| 6901 | "\"command\":\"scripts\"}"; |
| 6902 | int length = AsciiToUtf16(scripts_command, buffer); |
| 6903 | // Send scripts command. |
| 6904 | |
| 6905 | for (int i = 0; i < 20; i++) { |
| 6906 | v8::base::ElapsedTimer timer; |
| 6907 | timer.Start(); |
| 6908 | CHECK_EQ(i, counting_message_handler_counter); |
| 6909 | // Queue debug message. |
| 6910 | v8::Debug::SendCommand(isolate_, buffer, length); |
| 6911 | // Wait for the message handler to pick up the response. |
| 6912 | semaphore_.Wait(); |
| 6913 | i::PrintF("iteration %d took %f ms\n", i, |
| 6914 | timer.Elapsed().InMillisecondsF()); |
| 6915 | } |
| 6916 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6917 | isolate_->TerminateExecution(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | void StartSending() { semaphore_.Signal(); } |
| 6921 | |
| 6922 | private: |
| 6923 | v8::base::Semaphore semaphore_; |
| 6924 | v8::Isolate* isolate_; |
| 6925 | }; |
| 6926 | |
| 6927 | |
| 6928 | static void StartSendingCommands( |
| 6929 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 6930 | send_command_thread_->StartSending(); |
| 6931 | } |
| 6932 | |
| 6933 | |
| 6934 | TEST(ProcessDebugMessagesThreaded) { |
| 6935 | DebugLocalContext env; |
| 6936 | v8::Isolate* isolate = env->GetIsolate(); |
| 6937 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6938 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6939 | |
| 6940 | counting_message_handler_counter = 0; |
| 6941 | |
| 6942 | v8::Debug::SetMessageHandler( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6943 | isolate, SendCommandThread::CountingAndSignallingMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6944 | send_command_thread_ = new SendCommandThread(isolate); |
| 6945 | send_command_thread_->Start(); |
| 6946 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6947 | v8::Local<v8::FunctionTemplate> start = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6948 | v8::FunctionTemplate::New(isolate, StartSendingCommands); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6949 | CHECK(env->Global() |
| 6950 | ->Set(context, v8_str("start"), |
| 6951 | start->GetFunction(context).ToLocalChecked()) |
| 6952 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6953 | |
| 6954 | CompileRun("start(); while (true) { }"); |
| 6955 | |
| 6956 | CHECK_EQ(20, counting_message_handler_counter); |
| 6957 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6958 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6959 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6960 | } |
| 6961 | |
| 6962 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6963 | struct BacktraceData { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6964 | static int frame_counter; |
| 6965 | static void MessageHandler(const v8::Debug::Message& message) { |
| 6966 | char print_buffer[1000]; |
| 6967 | v8::String::Value json(message.GetJSON()); |
| 6968 | Utf16ToAscii(*json, json.length(), print_buffer, 1000); |
| 6969 | |
| 6970 | if (strstr(print_buffer, "backtrace") == NULL) { |
| 6971 | return; |
| 6972 | } |
| 6973 | frame_counter = GetTotalFramesInt(print_buffer); |
| 6974 | } |
| 6975 | }; |
| 6976 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6977 | int BacktraceData::frame_counter; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6978 | |
| 6979 | |
| 6980 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6981 | TEST(Backtrace) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6982 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6983 | v8::Isolate* isolate = env->GetIsolate(); |
| 6984 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6985 | v8::Local<v8::Context> context = env.context(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6986 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6987 | v8::Debug::SetMessageHandler(isolate, BacktraceData::MessageHandler); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6988 | |
| 6989 | const int kBufferSize = 1000; |
| 6990 | uint16_t buffer[kBufferSize]; |
| 6991 | const char* scripts_command = |
| 6992 | "{\"seq\":0," |
| 6993 | "\"type\":\"request\"," |
| 6994 | "\"command\":\"backtrace\"}"; |
| 6995 | |
| 6996 | // Check backtrace from ProcessDebugMessages. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6997 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6998 | v8::Debug::SendCommand( |
| 6999 | isolate, |
| 7000 | buffer, |
| 7001 | AsciiToUtf16(scripts_command, buffer), |
| 7002 | NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7003 | v8::Debug::ProcessDebugMessages(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7004 | CHECK_EQ(BacktraceData::frame_counter, 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7005 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7006 | v8::Local<v8::String> void0 = v8_str(env->GetIsolate(), "void(0)"); |
| 7007 | v8::Local<v8::Script> script = CompileWithOrigin(void0, void0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7008 | |
| 7009 | // Check backtrace from "void(0)" script. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7010 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7011 | v8::Debug::SendCommand( |
| 7012 | isolate, |
| 7013 | buffer, |
| 7014 | AsciiToUtf16(scripts_command, buffer), |
| 7015 | NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7016 | script->Run(context).ToLocalChecked(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7017 | CHECK_EQ(BacktraceData::frame_counter, 1); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7018 | |
| 7019 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7020 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 7021 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
| 7024 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7025 | TEST(GetMirror) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7026 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7027 | v8::Isolate* isolate = env->GetIsolate(); |
| 7028 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7029 | v8::Local<v8::Context> context = env.context(); |
| 7030 | v8::Local<v8::Value> obj = |
| 7031 | v8::Debug::GetMirror(context, v8_str(isolate, "hodja")).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7032 | v8::ScriptCompiler::Source source(v8_str( |
| 7033 | "function runTest(mirror) {" |
| 7034 | " return mirror.isString() && (mirror.length() == 5);" |
| 7035 | "}" |
| 7036 | "" |
| 7037 | "runTest;")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7038 | v8::Local<v8::Function> run_test = v8::Local<v8::Function>::Cast( |
| 7039 | v8::ScriptCompiler::CompileUnboundScript(isolate, &source) |
| 7040 | .ToLocalChecked() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7041 | ->BindToCurrentContext() |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7042 | ->Run(context) |
| 7043 | .ToLocalChecked()); |
| 7044 | v8::Local<v8::Value> result = |
| 7045 | run_test->Call(context, env->Global(), 1, &obj).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7046 | CHECK(result->IsTrue()); |
| 7047 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7048 | |
| 7049 | |
| 7050 | // Test that the debug break flag works with function.apply. |
| 7051 | TEST(DebugBreakFunctionApply) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7052 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7053 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7054 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7055 | |
| 7056 | // Create a function for testing breaking in apply. |
| 7057 | v8::Local<v8::Function> foo = CompileFunction( |
| 7058 | &env, |
| 7059 | "function baz(x) { }" |
| 7060 | "function bar(x) { baz(); }" |
| 7061 | "function foo(){ bar.apply(this, [1]); }", |
| 7062 | "foo"); |
| 7063 | |
| 7064 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7065 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7066 | |
| 7067 | // Set the debug break flag before calling the code using function.apply. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7068 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7069 | |
| 7070 | // Limit the number of debug breaks. This is a regression test for issue 493 |
| 7071 | // where this test would enter an infinite loop. |
| 7072 | break_point_hit_count = 0; |
| 7073 | max_break_point_hit_count = 10000; // 10000 => infinite loop. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7074 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7075 | |
| 7076 | // When keeping the debug break several break will happen. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7077 | CHECK_GT(break_point_hit_count, 1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7078 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7079 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7080 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7084 | v8::Local<v8::Context> debugee_context; |
| 7085 | v8::Local<v8::Context> debugger_context; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7086 | |
| 7087 | |
| 7088 | // Property getter that checks that current and calling contexts |
| 7089 | // are both the debugee contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7090 | static void NamedGetterWithCallingContextCheck( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7091 | v8::Local<v8::String> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7092 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 7093 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(name), "a")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7094 | v8::Local<v8::Context> current = info.GetIsolate()->GetCurrentContext(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7095 | CHECK(current == debugee_context); |
| 7096 | CHECK(current != debugger_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7097 | info.GetReturnValue().Set(1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | // Debug event listener that checks if the first argument of a function is |
| 7102 | // an object with property 'a' == 1. If the property has custom accessor |
| 7103 | // this handler will eventually invoke it. |
| 7104 | static void DebugEventGetAtgumentPropertyValue( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7105 | const v8::Debug::EventDetails& event_details) { |
| 7106 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7107 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7108 | if (event == v8::Break) { |
| 7109 | break_point_hit_count++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7110 | CHECK(debugger_context == CcTest::isolate()->GetCurrentContext()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7111 | v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(CompileRun( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7112 | "(function(exec_state) {\n" |
| 7113 | " return (exec_state.frame(0).argumentValue(0).property('a').\n" |
| 7114 | " value().value() == 1);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7115 | "})")); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7116 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7117 | v8::Local<v8::Value> argv[argc] = {exec_state}; |
| 7118 | v8::Local<v8::Value> result = |
| 7119 | func->Call(debugger_context, exec_state, argc, argv).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7120 | CHECK(result->IsTrue()); |
| 7121 | } |
| 7122 | } |
| 7123 | |
| 7124 | |
| 7125 | TEST(CallingContextIsNotDebugContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7126 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7127 | // Create and enter a debugee context. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7128 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7129 | v8::Isolate* isolate = env->GetIsolate(); |
| 7130 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7131 | env.ExposeDebug(); |
| 7132 | |
| 7133 | // Save handles to the debugger and debugee contexts to be used in |
| 7134 | // NamedGetterWithCallingContextCheck. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7135 | debugee_context = env.context(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7136 | debugger_context = v8::Utils::ToLocal(debug->debug_context()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7137 | |
| 7138 | // Create object with 'a' property accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7139 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
| 7140 | named->SetAccessor(v8_str(isolate, "a"), NamedGetterWithCallingContextCheck); |
| 7141 | CHECK(env->Global() |
| 7142 | ->Set(debugee_context, v8_str(isolate, "obj"), |
| 7143 | named->NewInstance(debugee_context).ToLocalChecked()) |
| 7144 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7145 | |
| 7146 | // Register the debug event listener |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7147 | v8::Debug::SetDebugEventListener(isolate, DebugEventGetAtgumentPropertyValue); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7148 | |
| 7149 | // Create a function that invokes debugger. |
| 7150 | v8::Local<v8::Function> foo = CompileFunction( |
| 7151 | &env, |
| 7152 | "function bar(x) { debugger; }" |
| 7153 | "function foo(){ bar(obj); }", |
| 7154 | "foo"); |
| 7155 | |
| 7156 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7157 | foo->Call(debugee_context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7158 | CHECK_EQ(1, break_point_hit_count); |
| 7159 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7160 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 7161 | debugee_context = v8::Local<v8::Context>(); |
| 7162 | debugger_context = v8::Local<v8::Context>(); |
| 7163 | CheckDebuggerUnloaded(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7164 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7165 | |
| 7166 | |
| 7167 | TEST(DebugContextIsPreservedBetweenAccesses) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7168 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7169 | v8::Debug::SetDebugEventListener(CcTest::isolate(), |
| 7170 | DebugEventBreakPointHitCount); |
| 7171 | v8::Local<v8::Context> context1 = |
| 7172 | v8::Debug::GetDebugContext(CcTest::isolate()); |
| 7173 | v8::Local<v8::Context> context2 = |
| 7174 | v8::Debug::GetDebugContext(CcTest::isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7175 | CHECK(v8::Utils::OpenHandle(*context1).is_identical_to( |
| 7176 | v8::Utils::OpenHandle(*context2))); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7177 | v8::Debug::SetDebugEventListener(CcTest::isolate(), nullptr); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7181 | TEST(NoDebugContextWhenDebuggerDisabled) { |
| 7182 | v8::HandleScope scope(CcTest::isolate()); |
| 7183 | v8::Local<v8::Context> context = |
| 7184 | v8::Debug::GetDebugContext(CcTest::isolate()); |
| 7185 | CHECK(context.IsEmpty()); |
| 7186 | } |
| 7187 | |
| 7188 | |
| 7189 | static v8::Local<v8::Value> expected_callback_data; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7190 | static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { |
| 7191 | CHECK(details.GetEventContext() == expected_context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7192 | CHECK(expected_callback_data->Equals(details.GetEventContext(), |
| 7193 | details.GetCallbackData()) |
| 7194 | .FromJust()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7195 | } |
| 7196 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7197 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7198 | // Check that event details contain context where debug event occured. |
| 7199 | TEST(DebugEventContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7200 | v8::Isolate* isolate = CcTest::isolate(); |
| 7201 | v8::HandleScope scope(isolate); |
| 7202 | expected_context = v8::Context::New(isolate); |
| 7203 | expected_callback_data = v8::Int32::New(isolate, 2010); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7204 | v8::Debug::SetDebugEventListener(isolate, DebugEventContextChecker, |
| 7205 | expected_callback_data); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7206 | v8::Context::Scope context_scope(expected_context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7207 | v8::Script::Compile(expected_context, |
| 7208 | v8_str(isolate, "(function(){debugger;})();")) |
| 7209 | .ToLocalChecked() |
| 7210 | ->Run(expected_context) |
| 7211 | .ToLocalChecked(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7212 | expected_context.Clear(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7213 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 7214 | expected_context_data = v8::Local<v8::Value>(); |
| 7215 | CheckDebuggerUnloaded(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7216 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7217 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7218 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7219 | static bool debug_event_break_deoptimize_done = false; |
| 7220 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7221 | static void DebugEventBreakDeoptimize( |
| 7222 | const v8::Debug::EventDetails& event_details) { |
| 7223 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7224 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 7225 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7226 | if (event == v8::Break) { |
| 7227 | if (!frame_function_name.IsEmpty()) { |
| 7228 | // Get the name of the function. |
| 7229 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7230 | v8::Local<v8::Value> argv[argc] = { |
| 7231 | exec_state, v8::Integer::New(CcTest::isolate(), 0)}; |
| 7232 | v8::Local<v8::Value> result = |
| 7233 | frame_function_name->Call(context, exec_state, argc, argv) |
| 7234 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7235 | if (!result->IsUndefined()) { |
| 7236 | char fn[80]; |
| 7237 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7238 | v8::Local<v8::String> function_name( |
| 7239 | result->ToString(context).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7240 | function_name->WriteUtf8(fn); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7241 | if (strcmp(fn, "bar") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7242 | i::Deoptimizer::DeoptimizeAll(CcTest::i_isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7243 | debug_event_break_deoptimize_done = true; |
| 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7248 | v8::Debug::DebugBreak(CcTest::isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7249 | } |
| 7250 | } |
| 7251 | |
| 7252 | |
| 7253 | // Test deoptimization when execution is broken using the debug break stack |
| 7254 | // check interrupt. |
| 7255 | TEST(DeoptimizeDuringDebugBreak) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7256 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7257 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7258 | env.ExposeDebug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7259 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7260 | |
| 7261 | // Create a function for checking the function when hitting a break point. |
| 7262 | frame_function_name = CompileFunction(&env, |
| 7263 | frame_function_name_source, |
| 7264 | "frame_function_name"); |
| 7265 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7266 | // Set a debug event listener which will keep interrupting execution until |
| 7267 | // debug break. When inside function bar it will deoptimize all functions. |
| 7268 | // This tests lazy deoptimization bailout for the stack check, as the first |
| 7269 | // time in function bar when using debug break and no break points will be at |
| 7270 | // the initial stack check. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7271 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 7272 | DebugEventBreakDeoptimize); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7273 | |
| 7274 | // Compile and run function bar which will optimize it for some flag settings. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7275 | v8::Local<v8::Function> f = CompileFunction(&env, "function bar(){}", "bar"); |
| 7276 | f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7277 | |
| 7278 | // Set debug break and call bar again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7279 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7280 | f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7281 | |
| 7282 | CHECK(debug_event_break_deoptimize_done); |
| 7283 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7284 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7285 | } |
| 7286 | |
| 7287 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7288 | static void DebugEventBreakWithOptimizedStack( |
| 7289 | const v8::Debug::EventDetails& event_details) { |
| 7290 | v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); |
| 7291 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7292 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 7293 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7294 | if (event == v8::Break) { |
| 7295 | if (!frame_function_name.IsEmpty()) { |
| 7296 | for (int i = 0; i < 2; i++) { |
| 7297 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7298 | v8::Local<v8::Value> argv[argc] = {exec_state, |
| 7299 | v8::Integer::New(isolate, i)}; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7300 | // Get the name of the function in frame i. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7301 | v8::Local<v8::Value> result = |
| 7302 | frame_function_name->Call(context, exec_state, argc, argv) |
| 7303 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7304 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7305 | v8::Local<v8::String> function_name( |
| 7306 | result->ToString(context).ToLocalChecked()); |
| 7307 | CHECK( |
| 7308 | function_name->Equals(context, v8_str(isolate, "loop")).FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7309 | // Get the name of the first argument in frame i. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7310 | result = frame_argument_name->Call(context, exec_state, argc, argv) |
| 7311 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7312 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7313 | v8::Local<v8::String> argument_name( |
| 7314 | result->ToString(context).ToLocalChecked()); |
| 7315 | CHECK(argument_name->Equals(context, v8_str(isolate, "count")) |
| 7316 | .FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7317 | // Get the value of the first argument in frame i. If the |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7318 | // function is optimized the value will be undefined, otherwise |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7319 | // the value will be '1 - i'. |
| 7320 | // |
| 7321 | // TODO(3141533): We should be able to get the real value for |
| 7322 | // optimized frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7323 | result = frame_argument_value->Call(context, exec_state, argc, argv) |
| 7324 | .ToLocalChecked(); |
| 7325 | CHECK(result->IsUndefined() || |
| 7326 | (result->Int32Value(context).FromJust() == 1 - i)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7327 | // Get the name of the first local variable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7328 | result = frame_local_name->Call(context, exec_state, argc, argv) |
| 7329 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7330 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7331 | v8::Local<v8::String> local_name( |
| 7332 | result->ToString(context).ToLocalChecked()); |
| 7333 | CHECK(local_name->Equals(context, v8_str(isolate, "local")).FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7334 | // Get the value of the first local variable. If the function |
| 7335 | // is optimized the value will be undefined, otherwise it will |
| 7336 | // be 42. |
| 7337 | // |
| 7338 | // TODO(3141533): We should be able to get the real value for |
| 7339 | // optimized frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7340 | result = frame_local_value->Call(context, exec_state, argc, argv) |
| 7341 | .ToLocalChecked(); |
| 7342 | CHECK(result->IsUndefined() || |
| 7343 | (result->Int32Value(context).FromJust() == 42)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7344 | } |
| 7345 | } |
| 7346 | } |
| 7347 | } |
| 7348 | |
| 7349 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7350 | static void ScheduleBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7351 | v8::Debug::SetDebugEventListener(args.GetIsolate(), |
| 7352 | DebugEventBreakWithOptimizedStack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7353 | v8::Debug::DebugBreak(args.GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7354 | } |
| 7355 | |
| 7356 | |
| 7357 | TEST(DebugBreakStackInspection) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7358 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7359 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7360 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7361 | |
| 7362 | frame_function_name = |
| 7363 | CompileFunction(&env, frame_function_name_source, "frame_function_name"); |
| 7364 | frame_argument_name = |
| 7365 | CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); |
| 7366 | frame_argument_value = CompileFunction(&env, |
| 7367 | frame_argument_value_source, |
| 7368 | "frame_argument_value"); |
| 7369 | frame_local_name = |
| 7370 | CompileFunction(&env, frame_local_name_source, "frame_local_name"); |
| 7371 | frame_local_value = |
| 7372 | CompileFunction(&env, frame_local_value_source, "frame_local_value"); |
| 7373 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7374 | v8::Local<v8::FunctionTemplate> schedule_break_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7375 | v8::FunctionTemplate::New(env->GetIsolate(), ScheduleBreak); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7376 | v8::Local<v8::Function> schedule_break = |
| 7377 | schedule_break_template->GetFunction(context).ToLocalChecked(); |
| 7378 | CHECK(env->Global() |
| 7379 | ->Set(context, v8_str("scheduleBreak"), schedule_break) |
| 7380 | .FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7381 | |
| 7382 | const char* src = |
| 7383 | "function loop(count) {" |
| 7384 | " var local = 42;" |
| 7385 | " if (count < 1) { scheduleBreak(); loop(count + 1); }" |
| 7386 | "}" |
| 7387 | "loop(0);"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7388 | v8::Script::Compile(context, v8_str(env->GetIsolate(), src)) |
| 7389 | .ToLocalChecked() |
| 7390 | ->Run(context) |
| 7391 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7392 | } |
| 7393 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7394 | |
| 7395 | // Test that setting the terminate execution flag during debug break processing. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7396 | static void TestDebugBreakInLoop(const char* loop_head, |
| 7397 | const char** loop_bodies, |
| 7398 | const char* loop_tail) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7399 | // Receive 10 breaks for each test and then terminate JavaScript execution. |
| 7400 | static const int kBreaksPerTest = 10; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7401 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7402 | for (int i = 0; loop_bodies[i] != NULL; i++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7403 | // Perform a lazy deoptimization after various numbers of breaks |
| 7404 | // have been hit. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7405 | |
| 7406 | EmbeddedVector<char, 1024> buffer; |
| 7407 | SNPrintF(buffer, "function f() {%s%s%s}", loop_head, loop_bodies[i], |
| 7408 | loop_tail); |
| 7409 | |
| 7410 | i::PrintF("%s\n", buffer.start()); |
| 7411 | |
| 7412 | for (int j = 0; j < 3; j++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7413 | break_point_hit_count_deoptimize = j; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7414 | if (j == 2) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7415 | break_point_hit_count_deoptimize = kBreaksPerTest; |
| 7416 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7417 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7418 | break_point_hit_count = 0; |
| 7419 | max_break_point_hit_count = kBreaksPerTest; |
| 7420 | terminate_after_max_break_point_hit = true; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7421 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7422 | // Function with infinite loop. |
| 7423 | CompileRun(buffer.start()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7424 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7425 | // Set the debug break to enter the debugger as soon as possible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7426 | v8::Debug::DebugBreak(CcTest::isolate()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7427 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7428 | // Call function with infinite loop. |
| 7429 | CompileRun("f();"); |
| 7430 | CHECK_EQ(kBreaksPerTest, break_point_hit_count); |
| 7431 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7432 | CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7433 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7438 | static const char* loop_bodies_1[] = {"", |
| 7439 | "g()", |
| 7440 | "if (a == 0) { g() }", |
| 7441 | "if (a == 1) { g() }", |
| 7442 | "if (a == 0) { g() } else { h() }", |
| 7443 | "if (a == 0) { continue }", |
| 7444 | NULL}; |
| 7445 | |
| 7446 | |
| 7447 | static const char* loop_bodies_2[] = { |
| 7448 | "if (a == 1) { continue }", |
| 7449 | "switch (a) { case 1: g(); }", |
| 7450 | "switch (a) { case 1: continue; }", |
| 7451 | "switch (a) { case 1: g(); break; default: h() }", |
| 7452 | "switch (a) { case 1: continue; break; default: h() }", |
| 7453 | NULL}; |
| 7454 | |
| 7455 | |
| 7456 | void DebugBreakLoop(const char* loop_header, const char** loop_bodies, |
| 7457 | const char* loop_footer) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7458 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7459 | v8::HandleScope scope(env->GetIsolate()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7460 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7461 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7462 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7463 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7464 | CompileRun( |
| 7465 | "var a = 1;\n" |
| 7466 | "function g() { }\n" |
| 7467 | "function h() { }"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7468 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7469 | TestDebugBreakInLoop(loop_header, loop_bodies, loop_footer); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7470 | |
| 7471 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7472 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7473 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 7474 | } |
| 7475 | |
| 7476 | |
| 7477 | TEST(DebugBreakInWhileTrue1) { |
| 7478 | DebugBreakLoop("while (true) {", loop_bodies_1, "}"); |
| 7479 | } |
| 7480 | |
| 7481 | |
| 7482 | TEST(DebugBreakInWhileTrue2) { |
| 7483 | DebugBreakLoop("while (true) {", loop_bodies_2, "}"); |
| 7484 | } |
| 7485 | |
| 7486 | |
| 7487 | TEST(DebugBreakInWhileCondition1) { |
| 7488 | DebugBreakLoop("while (a == 1) {", loop_bodies_1, "}"); |
| 7489 | } |
| 7490 | |
| 7491 | |
| 7492 | TEST(DebugBreakInWhileCondition2) { |
| 7493 | DebugBreakLoop("while (a == 1) {", loop_bodies_2, "}"); |
| 7494 | } |
| 7495 | |
| 7496 | |
| 7497 | TEST(DebugBreakInDoWhileTrue1) { |
| 7498 | DebugBreakLoop("do {", loop_bodies_1, "} while (true)"); |
| 7499 | } |
| 7500 | |
| 7501 | |
| 7502 | TEST(DebugBreakInDoWhileTrue2) { |
| 7503 | DebugBreakLoop("do {", loop_bodies_2, "} while (true)"); |
| 7504 | } |
| 7505 | |
| 7506 | |
| 7507 | TEST(DebugBreakInDoWhileCondition1) { |
| 7508 | DebugBreakLoop("do {", loop_bodies_1, "} while (a == 1)"); |
| 7509 | } |
| 7510 | |
| 7511 | |
| 7512 | TEST(DebugBreakInDoWhileCondition2) { |
| 7513 | DebugBreakLoop("do {", loop_bodies_2, "} while (a == 1)"); |
| 7514 | } |
| 7515 | |
| 7516 | |
| 7517 | TEST(DebugBreakInFor1) { DebugBreakLoop("for (;;) {", loop_bodies_1, "}"); } |
| 7518 | |
| 7519 | |
| 7520 | TEST(DebugBreakInFor2) { DebugBreakLoop("for (;;) {", loop_bodies_2, "}"); } |
| 7521 | |
| 7522 | |
| 7523 | TEST(DebugBreakInForCondition1) { |
| 7524 | DebugBreakLoop("for (;a == 1;) {", loop_bodies_1, "}"); |
| 7525 | } |
| 7526 | |
| 7527 | |
| 7528 | TEST(DebugBreakInForCondition2) { |
| 7529 | DebugBreakLoop("for (;a == 1;) {", loop_bodies_2, "}"); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7530 | } |
| 7531 | |
| 7532 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7533 | v8::Local<v8::Script> inline_script; |
| 7534 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7535 | static void DebugBreakInlineListener( |
| 7536 | const v8::Debug::EventDetails& event_details) { |
| 7537 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7538 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7539 | if (event != v8::Break) return; |
| 7540 | |
| 7541 | int expected_frame_count = 4; |
| 7542 | int expected_line_number[] = {1, 4, 7, 12}; |
| 7543 | |
| 7544 | i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script); |
| 7545 | i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast( |
| 7546 | i::JSFunction::cast(*compiled_script)->shared()->script())); |
| 7547 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7548 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7549 | char script[128]; |
| 7550 | i::Vector<char> script_vector(script, sizeof(script)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7551 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7552 | v8::Local<v8::Value> result = CompileRun(script); |
| 7553 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7554 | int frame_count = result->Int32Value(context).FromJust(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7555 | CHECK_EQ(expected_frame_count, frame_count); |
| 7556 | |
| 7557 | for (int i = 0; i < frame_count; i++) { |
| 7558 | // The 5. element in the returned array of GetFrameDetails contains the |
| 7559 | // source position of that frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7560 | SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[5]", break_id, i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7561 | v8::Local<v8::Value> result = CompileRun(script); |
| 7562 | CHECK_EQ(expected_line_number[i], |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7563 | i::Script::GetLineNumber(source_script, |
| 7564 | result->Int32Value(context).FromJust())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7565 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7566 | v8::Debug::SetDebugEventListener(CcTest::isolate(), nullptr); |
| 7567 | CcTest::isolate()->TerminateExecution(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7568 | } |
| 7569 | |
| 7570 | |
| 7571 | TEST(DebugBreakInline) { |
| 7572 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7573 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7574 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7575 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7576 | const char* source = |
| 7577 | "function debug(b) { \n" |
| 7578 | " if (b) debugger; \n" |
| 7579 | "} \n" |
| 7580 | "function f(b) { \n" |
| 7581 | " debug(b) \n" |
| 7582 | "}; \n" |
| 7583 | "function g(b) { \n" |
| 7584 | " f(b); \n" |
| 7585 | "}; \n" |
| 7586 | "g(false); \n" |
| 7587 | "g(false); \n" |
| 7588 | "%OptimizeFunctionOnNextCall(g); \n" |
| 7589 | "g(true);"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7590 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7591 | inline_script = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7592 | v8::Script::Compile(context, v8_str(env->GetIsolate(), source)) |
| 7593 | .ToLocalChecked(); |
| 7594 | inline_script->Run(context).ToLocalChecked(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7595 | } |
| 7596 | |
| 7597 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7598 | static void DebugEventStepNext( |
| 7599 | const v8::Debug::EventDetails& event_details) { |
| 7600 | v8::DebugEvent event = event_details.GetEvent(); |
| 7601 | if (event == v8::Break) { |
| 7602 | PrepareStep(StepNext); |
| 7603 | } |
| 7604 | } |
| 7605 | |
| 7606 | |
| 7607 | static void RunScriptInANewCFrame(const char* source) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7608 | v8::TryCatch try_catch(CcTest::isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7609 | CompileRun(source); |
| 7610 | CHECK(try_catch.HasCaught()); |
| 7611 | } |
| 7612 | |
| 7613 | |
| 7614 | TEST(Regress131642) { |
| 7615 | // Bug description: |
| 7616 | // When doing StepNext through the first script, the debugger is not reset |
| 7617 | // after exiting through exception. A flawed implementation enabling the |
| 7618 | // debugger to step into Array.prototype.forEach breaks inside the callback |
| 7619 | // for forEach in the second script under the assumption that we are in a |
| 7620 | // recursive call. In an attempt to step out, we crawl the stack using the |
| 7621 | // recorded frame pointer from the first script and fail when not finding it |
| 7622 | // on the stack. |
| 7623 | DebugLocalContext env; |
| 7624 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7625 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepNext); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7626 | |
| 7627 | // We step through the first script. It exits through an exception. We run |
| 7628 | // this inside a new frame to record a different FP than the second script |
| 7629 | // would expect. |
| 7630 | const char* script_1 = "debugger; throw new Error();"; |
| 7631 | RunScriptInANewCFrame(script_1); |
| 7632 | |
| 7633 | // The second script uses forEach. |
| 7634 | const char* script_2 = "[0].forEach(function() { });"; |
| 7635 | CompileRun(script_2); |
| 7636 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7637 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7638 | } |
| 7639 | |
| 7640 | |
| 7641 | // Import from test-heap.cc |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7642 | namespace v8 { |
| 7643 | namespace internal { |
| 7644 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7645 | int CountNativeContexts(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7646 | } |
| 7647 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7648 | |
| 7649 | |
| 7650 | static void NopListener(const v8::Debug::EventDetails& event_details) { |
| 7651 | } |
| 7652 | |
| 7653 | |
| 7654 | TEST(DebuggerCreatesContextIffActive) { |
| 7655 | DebugLocalContext env; |
| 7656 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7657 | CHECK_EQ(1, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7658 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7659 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7660 | CompileRun("debugger;"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7661 | CHECK_EQ(1, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7662 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7663 | v8::Debug::SetDebugEventListener(env->GetIsolate(), NopListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7664 | CompileRun("debugger;"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7665 | CHECK_EQ(2, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7666 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7667 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | |
| 7671 | TEST(LiveEditEnabled) { |
| 7672 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7673 | LocalContext env; |
| 7674 | v8::HandleScope scope(env->GetIsolate()); |
| 7675 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), true); |
| 7676 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7677 | } |
| 7678 | |
| 7679 | |
| 7680 | TEST(LiveEditDisabled) { |
| 7681 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7682 | LocalContext env; |
| 7683 | v8::HandleScope scope(env->GetIsolate()); |
| 7684 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), false); |
| 7685 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7686 | } |
| 7687 | |
| 7688 | |
| 7689 | TEST(PrecompiledFunction) { |
| 7690 | // Regression test for crbug.com/346207. If we have preparse data, parsing the |
| 7691 | // function in the presence of the debugger (and breakpoints) should still |
| 7692 | // succeed. The bug was that preparsing was done lazily and parsing was done |
| 7693 | // eagerly, so, the symbol streams didn't match. |
| 7694 | DebugLocalContext env; |
| 7695 | v8::HandleScope scope(env->GetIsolate()); |
| 7696 | env.ExposeDebug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7697 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7698 | |
| 7699 | v8::Local<v8::Function> break_here = |
| 7700 | CompileFunction(&env, "function break_here(){}", "break_here"); |
| 7701 | SetBreakPoint(break_here, 0); |
| 7702 | |
| 7703 | const char* source = |
| 7704 | "var a = b = c = 1; \n" |
| 7705 | "function this_is_lazy() { \n" |
| 7706 | // This symbol won't appear in the preparse data. |
| 7707 | " var a; \n" |
| 7708 | "} \n" |
| 7709 | "function bar() { \n" |
| 7710 | " return \"bar\"; \n" |
| 7711 | "}; \n" |
| 7712 | "a = b = c = 2; \n" |
| 7713 | "bar(); \n"; |
| 7714 | v8::Local<v8::Value> result = ParserCacheCompileRun(source); |
| 7715 | CHECK(result->IsString()); |
| 7716 | v8::String::Utf8Value utf8(result); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7717 | CHECK_EQ(0, strcmp("bar", *utf8)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7718 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7719 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7720 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7721 | } |
| 7722 | |
| 7723 | |
| 7724 | static void DebugBreakStackTraceListener( |
| 7725 | const v8::Debug::EventDetails& event_details) { |
| 7726 | v8::StackTrace::CurrentStackTrace(CcTest::isolate(), 10); |
| 7727 | } |
| 7728 | |
| 7729 | |
| 7730 | static void AddDebugBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7731 | v8::Debug::DebugBreak(args.GetIsolate()); |
| 7732 | } |
| 7733 | |
| 7734 | |
| 7735 | TEST(DebugBreakStackTrace) { |
| 7736 | DebugLocalContext env; |
| 7737 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7738 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 7739 | DebugBreakStackTraceListener); |
| 7740 | v8::Local<v8::Context> context = env.context(); |
| 7741 | v8::Local<v8::FunctionTemplate> add_debug_break_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7742 | v8::FunctionTemplate::New(env->GetIsolate(), AddDebugBreak); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7743 | v8::Local<v8::Function> add_debug_break = |
| 7744 | add_debug_break_template->GetFunction(context).ToLocalChecked(); |
| 7745 | CHECK(env->Global() |
| 7746 | ->Set(context, v8_str("add_debug_break"), add_debug_break) |
| 7747 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7748 | |
| 7749 | CompileRun("(function loop() {" |
| 7750 | " for (var j = 0; j < 1000; j++) {" |
| 7751 | " for (var i = 0; i < 1000; i++) {" |
| 7752 | " if (i == 999) add_debug_break();" |
| 7753 | " }" |
| 7754 | " }" |
| 7755 | "})()"); |
| 7756 | } |
| 7757 | |
| 7758 | |
| 7759 | v8::base::Semaphore terminate_requested_semaphore(0); |
| 7760 | v8::base::Semaphore terminate_fired_semaphore(0); |
| 7761 | bool terminate_already_fired = false; |
| 7762 | |
| 7763 | |
| 7764 | static void DebugBreakTriggerTerminate( |
| 7765 | const v8::Debug::EventDetails& event_details) { |
| 7766 | if (event_details.GetEvent() != v8::Break || terminate_already_fired) return; |
| 7767 | terminate_requested_semaphore.Signal(); |
| 7768 | // Wait for at most 2 seconds for the terminate request. |
| 7769 | CHECK(terminate_fired_semaphore.WaitFor(v8::base::TimeDelta::FromSeconds(2))); |
| 7770 | terminate_already_fired = true; |
| 7771 | } |
| 7772 | |
| 7773 | |
| 7774 | class TerminationThread : public v8::base::Thread { |
| 7775 | public: |
| 7776 | explicit TerminationThread(v8::Isolate* isolate) |
| 7777 | : Thread(Options("terminator")), isolate_(isolate) {} |
| 7778 | |
| 7779 | virtual void Run() { |
| 7780 | terminate_requested_semaphore.Wait(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7781 | isolate_->TerminateExecution(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7782 | terminate_fired_semaphore.Signal(); |
| 7783 | } |
| 7784 | |
| 7785 | private: |
| 7786 | v8::Isolate* isolate_; |
| 7787 | }; |
| 7788 | |
| 7789 | |
| 7790 | TEST(DebugBreakOffThreadTerminate) { |
| 7791 | DebugLocalContext env; |
| 7792 | v8::Isolate* isolate = env->GetIsolate(); |
| 7793 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7794 | v8::Debug::SetDebugEventListener(isolate, DebugBreakTriggerTerminate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7795 | TerminationThread terminator(isolate); |
| 7796 | terminator.Start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7797 | v8::TryCatch try_catch(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7798 | v8::Debug::DebugBreak(isolate); |
| 7799 | CompileRun("while (true);"); |
| 7800 | CHECK(try_catch.HasTerminated()); |
| 7801 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7802 | |
| 7803 | |
| 7804 | static void DebugEventExpectNoException( |
| 7805 | const v8::Debug::EventDetails& event_details) { |
| 7806 | v8::DebugEvent event = event_details.GetEvent(); |
| 7807 | CHECK_NE(v8::Exception, event); |
| 7808 | } |
| 7809 | |
| 7810 | |
| 7811 | static void TryCatchWrappedThrowCallback( |
| 7812 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7813 | v8::TryCatch try_catch(args.GetIsolate()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7814 | CompileRun("throw 'rejection';"); |
| 7815 | CHECK(try_catch.HasCaught()); |
| 7816 | } |
| 7817 | |
| 7818 | |
| 7819 | TEST(DebugPromiseInterceptedByTryCatch) { |
| 7820 | DebugLocalContext env; |
| 7821 | v8::Isolate* isolate = env->GetIsolate(); |
| 7822 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7823 | v8::Debug::SetDebugEventListener(isolate, &DebugEventExpectNoException); |
| 7824 | v8::Local<v8::Context> context = env.context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7825 | ChangeBreakOnException(false, true); |
| 7826 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7827 | v8::Local<v8::FunctionTemplate> fun = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7828 | v8::FunctionTemplate::New(isolate, TryCatchWrappedThrowCallback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7829 | CHECK(env->Global() |
| 7830 | ->Set(context, v8_str("fun"), |
| 7831 | fun->GetFunction(context).ToLocalChecked()) |
| 7832 | .FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7833 | |
| 7834 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7835 | CompileRun( |
| 7836 | "var r;" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 7837 | "p.then(function() { r = 'resolved'; }," |
| 7838 | " function() { r = 'rejected'; });"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7839 | CHECK(CompileRun("r")->Equals(context, v8_str("resolved")).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7840 | } |
| 7841 | |
| 7842 | |
| 7843 | static int exception_event_counter = 0; |
| 7844 | |
| 7845 | |
| 7846 | static void DebugEventCountException( |
| 7847 | const v8::Debug::EventDetails& event_details) { |
| 7848 | v8::DebugEvent event = event_details.GetEvent(); |
| 7849 | if (event == v8::Exception) exception_event_counter++; |
| 7850 | } |
| 7851 | |
| 7852 | |
| 7853 | static void ThrowCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7854 | CompileRun("throw 'rejection';"); |
| 7855 | } |
| 7856 | |
| 7857 | |
| 7858 | TEST(DebugPromiseRejectedByCallback) { |
| 7859 | DebugLocalContext env; |
| 7860 | v8::Isolate* isolate = env->GetIsolate(); |
| 7861 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7862 | v8::Debug::SetDebugEventListener(isolate, &DebugEventCountException); |
| 7863 | v8::Local<v8::Context> context = env.context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7864 | ChangeBreakOnException(false, true); |
| 7865 | exception_event_counter = 0; |
| 7866 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7867 | v8::Local<v8::FunctionTemplate> fun = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7868 | v8::FunctionTemplate::New(isolate, ThrowCallback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7869 | CHECK(env->Global() |
| 7870 | ->Set(context, v8_str("fun"), |
| 7871 | fun->GetFunction(context).ToLocalChecked()) |
| 7872 | .FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7873 | |
| 7874 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7875 | CompileRun( |
| 7876 | "var r;" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 7877 | "p.then(function() { r = 'resolved'; }," |
| 7878 | " function(e) { r = 'rejected' + e; });"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7879 | CHECK( |
| 7880 | CompileRun("r")->Equals(context, v8_str("rejectedrejection")).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7881 | CHECK_EQ(1, exception_event_counter); |
| 7882 | } |
| 7883 | |
| 7884 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7885 | static void DebugHarmonyScopingListener( |
| 7886 | const v8::Debug::EventDetails& event_details) { |
| 7887 | v8::DebugEvent event = event_details.GetEvent(); |
| 7888 | if (event != v8::Break) return; |
| 7889 | |
| 7890 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
| 7891 | |
| 7892 | char script[128]; |
| 7893 | i::Vector<char> script_vector(script, sizeof(script)); |
| 7894 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
| 7895 | ExpectInt32(script, 1); |
| 7896 | |
| 7897 | SNPrintF(script_vector, "var frame = new FrameMirror(%d, 0);", break_id); |
| 7898 | CompileRun(script); |
| 7899 | ExpectInt32("frame.evaluate('x').value_", 1); |
| 7900 | ExpectInt32("frame.evaluate('y').value_", 2); |
| 7901 | |
| 7902 | CompileRun("var allScopes = frame.allScopes()"); |
| 7903 | ExpectInt32("allScopes.length", 2); |
| 7904 | |
| 7905 | ExpectBoolean("allScopes[0].scopeType() === ScopeType.Script", true); |
| 7906 | |
| 7907 | ExpectInt32("allScopes[0].scopeObject().value_.x", 1); |
| 7908 | |
| 7909 | ExpectInt32("allScopes[0].scopeObject().value_.y", 2); |
| 7910 | |
| 7911 | CompileRun("allScopes[0].setVariableValue('x', 5);"); |
| 7912 | CompileRun("allScopes[0].setVariableValue('y', 6);"); |
| 7913 | ExpectInt32("frame.evaluate('x + y').value_", 11); |
| 7914 | } |
| 7915 | |
| 7916 | |
| 7917 | TEST(DebugBreakInLexicalScopes) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7918 | i::FLAG_allow_natives_syntax = true; |
| 7919 | |
| 7920 | DebugLocalContext env; |
| 7921 | v8::Isolate* isolate = env->GetIsolate(); |
| 7922 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7923 | v8::Debug::SetDebugEventListener(isolate, DebugHarmonyScopingListener); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7924 | |
| 7925 | CompileRun( |
| 7926 | "'use strict'; \n" |
| 7927 | "let x = 1; \n"); |
| 7928 | ExpectInt32( |
| 7929 | "'use strict'; \n" |
| 7930 | "let y = 2; \n" |
| 7931 | "debugger; \n" |
| 7932 | "x * y", |
| 7933 | 30); |
| 7934 | ExpectInt32( |
| 7935 | "x = 1; y = 2; \n" |
| 7936 | "debugger;" |
| 7937 | "x * y", |
| 7938 | 30); |
| 7939 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7940 | |
| 7941 | static int after_compile_handler_depth = 0; |
| 7942 | static void HandleInterrupt(v8::Isolate* isolate, void* data) { |
| 7943 | CHECK_EQ(0, after_compile_handler_depth); |
| 7944 | } |
| 7945 | |
| 7946 | static void NoInterruptsOnDebugEvent( |
| 7947 | const v8::Debug::EventDetails& event_details) { |
| 7948 | if (event_details.GetEvent() != v8::AfterCompile) return; |
| 7949 | ++after_compile_handler_depth; |
| 7950 | // Do not allow nested AfterCompile events. |
| 7951 | CHECK(after_compile_handler_depth <= 1); |
| 7952 | v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); |
| 7953 | isolate->RequestInterrupt(&HandleInterrupt, nullptr); |
| 7954 | CompileRun("function foo() {}; foo();"); |
| 7955 | --after_compile_handler_depth; |
| 7956 | } |
| 7957 | |
| 7958 | |
| 7959 | TEST(NoInterruptsInDebugListener) { |
| 7960 | DebugLocalContext env; |
| 7961 | v8::Debug::SetDebugEventListener(env->GetIsolate(), NoInterruptsOnDebugEvent); |
| 7962 | CompileRun("void(0);"); |
| 7963 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7964 | |
| 7965 | class TestBreakLocation : public i::BreakLocation { |
| 7966 | public: |
| 7967 | using i::BreakLocation::GetIterator; |
| 7968 | using i::BreakLocation::Iterator; |
| 7969 | }; |
| 7970 | |
| 7971 | TEST(BreakLocationIterator) { |
| 7972 | DebugLocalContext env; |
| 7973 | v8::Isolate* isolate = env->GetIsolate(); |
| 7974 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 7975 | v8::HandleScope scope(isolate); |
| 7976 | |
| 7977 | v8::Local<v8::Value> result = CompileRun( |
| 7978 | "function f() {\n" |
| 7979 | " debugger; \n" |
| 7980 | " f(); \n" |
| 7981 | " debugger; \n" |
| 7982 | "} \n" |
| 7983 | "f"); |
| 7984 | Handle<i::Object> function_obj = v8::Utils::OpenHandle(*result); |
| 7985 | Handle<i::JSFunction> function = Handle<i::JSFunction>::cast(function_obj); |
| 7986 | Handle<i::SharedFunctionInfo> shared(function->shared()); |
| 7987 | |
| 7988 | EnableDebugger(isolate); |
| 7989 | CHECK(i_isolate->debug()->EnsureDebugInfo(shared, function)); |
| 7990 | |
| 7991 | Handle<i::DebugInfo> debug_info(shared->GetDebugInfo()); |
| 7992 | int code_size = debug_info->abstract_code()->Size(); |
| 7993 | |
| 7994 | bool found_return = false; |
| 7995 | bool found_call = false; |
| 7996 | bool found_debugger = false; |
| 7997 | |
| 7998 | // Test public interface. |
| 7999 | for (int i = 0; i < code_size; i++) { |
| 8000 | i::BreakLocation location = i::BreakLocation::FromCodeOffset(debug_info, i); |
| 8001 | if (location.IsCall()) found_call = true; |
| 8002 | if (location.IsReturn()) found_return = true; |
| 8003 | if (location.IsDebuggerStatement()) found_debugger = true; |
| 8004 | } |
| 8005 | CHECK(found_call); |
| 8006 | CHECK(found_return); |
| 8007 | CHECK(found_debugger); |
| 8008 | |
| 8009 | // Test underlying implementation. |
| 8010 | TestBreakLocation::Iterator* iterator = |
| 8011 | TestBreakLocation::GetIterator(debug_info, i::ALL_BREAK_LOCATIONS); |
| 8012 | CHECK(iterator->GetBreakLocation().IsDebuggerStatement()); |
| 8013 | CHECK_EQ(7, iterator->GetBreakLocation().position()); |
| 8014 | iterator->Next(); |
| 8015 | CHECK(iterator->GetBreakLocation().IsDebugBreakSlot()); |
| 8016 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8017 | iterator->Next(); |
| 8018 | CHECK(iterator->GetBreakLocation().IsCall()); |
| 8019 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8020 | iterator->Next(); |
| 8021 | CHECK(iterator->GetBreakLocation().IsDebuggerStatement()); |
| 8022 | CHECK_EQ(37, iterator->GetBreakLocation().position()); |
| 8023 | iterator->Next(); |
| 8024 | CHECK(iterator->GetBreakLocation().IsReturn()); |
| 8025 | CHECK_EQ(50, iterator->GetBreakLocation().position()); |
| 8026 | iterator->Next(); |
| 8027 | CHECK(iterator->Done()); |
| 8028 | delete iterator; |
| 8029 | |
| 8030 | iterator = TestBreakLocation::GetIterator(debug_info, i::CALLS_AND_RETURNS); |
| 8031 | CHECK(iterator->GetBreakLocation().IsCall()); |
| 8032 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8033 | iterator->Next(); |
| 8034 | CHECK(iterator->GetBreakLocation().IsReturn()); |
| 8035 | CHECK_EQ(50, iterator->GetBreakLocation().position()); |
| 8036 | iterator->Next(); |
| 8037 | CHECK(iterator->Done()); |
| 8038 | delete iterator; |
| 8039 | |
| 8040 | DisableDebugger(isolate); |
| 8041 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 8042 | |
| 8043 | TEST(DisableTailCallElimination) { |
| 8044 | i::FLAG_allow_natives_syntax = true; |
| 8045 | i::FLAG_harmony_tailcalls = true; |
| 8046 | // TODO(ishell, 4698): Investigate why TurboFan in --always-opt mode makes |
| 8047 | // stack[2].getFunctionName() return null. |
| 8048 | i::FLAG_turbo_inlining = false; |
| 8049 | |
| 8050 | DebugLocalContext env; |
| 8051 | v8::Isolate* isolate = env->GetIsolate(); |
| 8052 | v8::HandleScope scope(isolate); |
| 8053 | CHECK(v8::Debug::IsTailCallEliminationEnabled(isolate)); |
| 8054 | |
| 8055 | CompileRun( |
| 8056 | "'use strict'; \n" |
| 8057 | "Error.prepareStackTrace = (error,stack) => { \n" |
| 8058 | " error.strace = stack; \n" |
| 8059 | " return error.message + \"\\n at \" + stack.join(\"\\n at \"); \n" |
| 8060 | "} \n" |
| 8061 | " \n" |
| 8062 | "function getCaller() { \n" |
| 8063 | " var e = new Error(); \n" |
| 8064 | " e.stack; // prepare stack trace \n" |
| 8065 | " var stack = e.strace; \n" |
| 8066 | " %GlobalPrint('caller: '); \n" |
| 8067 | " %GlobalPrint(stack[2].getFunctionName()); \n" |
| 8068 | " %GlobalPrint('\\n'); \n" |
| 8069 | " return stack[2].getFunctionName(); \n" |
| 8070 | "} \n" |
| 8071 | "function f() { \n" |
| 8072 | " var caller = getCaller(); \n" |
| 8073 | " if (caller === 'g') return 1; \n" |
| 8074 | " if (caller === 'h') return 2; \n" |
| 8075 | " return 0; \n" |
| 8076 | "} \n" |
| 8077 | "function g() { \n" |
| 8078 | " return f(); \n" |
| 8079 | "} \n" |
| 8080 | "function h() { \n" |
| 8081 | " var result = g(); \n" |
| 8082 | " return result; \n" |
| 8083 | "} \n" |
| 8084 | "%NeverOptimizeFunction(getCaller); \n" |
| 8085 | "%NeverOptimizeFunction(f); \n" |
| 8086 | "%NeverOptimizeFunction(h); \n" |
| 8087 | ""); |
| 8088 | ExpectInt32("h();", 2); |
| 8089 | ExpectInt32("h(); %OptimizeFunctionOnNextCall(g); h();", 2); |
| 8090 | v8::Debug::SetTailCallEliminationEnabled(isolate, false); |
| 8091 | CHECK(!v8::Debug::IsTailCallEliminationEnabled(isolate)); |
| 8092 | ExpectInt32("h();", 1); |
| 8093 | ExpectInt32("h(); %OptimizeFunctionOnNextCall(g); h();", 1); |
| 8094 | v8::Debug::SetTailCallEliminationEnabled(isolate, true); |
| 8095 | CHECK(v8::Debug::IsTailCallEliminationEnabled(isolate)); |
| 8096 | ExpectInt32("h();", 2); |
| 8097 | ExpectInt32("h(); %OptimizeFunctionOnNextCall(g); h();", 2); |
| 8098 | } |
| 8099 | |
| 8100 | TEST(DebugStepNextTailCallEliminiation) { |
| 8101 | i::FLAG_allow_natives_syntax = true; |
| 8102 | i::FLAG_harmony_tailcalls = true; |
| 8103 | // TODO(ishell, 4698): Investigate why TurboFan in --always-opt mode makes |
| 8104 | // stack[2].getFunctionName() return null. |
| 8105 | i::FLAG_turbo_inlining = false; |
| 8106 | |
| 8107 | DebugLocalContext env; |
| 8108 | env.ExposeDebug(); |
| 8109 | v8::Isolate* isolate = env->GetIsolate(); |
| 8110 | v8::HandleScope scope(isolate); |
| 8111 | CHECK(v8::Debug::IsTailCallEliminationEnabled(isolate)); |
| 8112 | |
| 8113 | const char* source = |
| 8114 | "'use strict'; \n" |
| 8115 | "var Debug = debug.Debug; \n" |
| 8116 | "var exception = null; \n" |
| 8117 | "var breaks = 0; \n" |
| 8118 | "var log = []; \n" |
| 8119 | "function f(x) { \n" |
| 8120 | " if (x == 2) { \n" |
| 8121 | " debugger; // Break a \n" |
| 8122 | " } \n" |
| 8123 | " if (x-- > 0) { // Break b \n" |
| 8124 | " return f(x); // Break c \n" |
| 8125 | " } \n" |
| 8126 | "} // Break e \n" |
| 8127 | "function listener(event, exec_state, event_data, data) {\n" |
| 8128 | " if (event != Debug.DebugEvent.Break) return; \n" |
| 8129 | " try { \n" |
| 8130 | " var line = exec_state.frame(0).sourceLineText(); \n" |
| 8131 | " var col = exec_state.frame(0).sourceColumn(); \n" |
| 8132 | " var match = line.match(/\\/\\/ Break (\\w)/); \n" |
| 8133 | " log.push(match[1] + col); \n" |
| 8134 | " exec_state.prepareStep(Debug.StepAction.StepNext); \n" |
| 8135 | " } catch (e) { \n" |
| 8136 | " exception = e; \n" |
| 8137 | " }; \n" |
| 8138 | "}; \n" |
| 8139 | "Debug.setListener(listener); \n" |
| 8140 | "f(4); \n" |
| 8141 | "Debug.setListener(null); // Break d \n"; |
| 8142 | |
| 8143 | CompileRun(source); |
| 8144 | ExpectNull("exception"); |
| 8145 | ExpectString("JSON.stringify(log)", "[\"a4\",\"b2\",\"c4\",\"c11\",\"d0\"]"); |
| 8146 | |
| 8147 | v8::Debug::SetTailCallEliminationEnabled(isolate, false); |
| 8148 | CompileRun( |
| 8149 | "log = []; \n" |
| 8150 | "Debug.setListener(listener); \n" |
| 8151 | "f(5); \n" |
| 8152 | "Debug.setListener(null); // Break f \n"); |
| 8153 | ExpectNull("exception"); |
| 8154 | ExpectString("JSON.stringify(log)", |
| 8155 | "[\"a4\",\"b2\",\"c4\",\"e0\",\"e0\",\"e0\",\"e0\",\"f0\"]"); |
| 8156 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 8157 | |
| 8158 | size_t current_action = 0; |
| 8159 | StepAction actions[] = {StepNext, StepNext}; |
| 8160 | static void DebugStepOverFunctionWithCaughtExceptionListener( |
| 8161 | const v8::Debug::EventDetails& event_details) { |
| 8162 | v8::DebugEvent event = event_details.GetEvent(); |
| 8163 | if (event != v8::Break) return; |
| 8164 | ++break_point_hit_count; |
| 8165 | if (current_action >= 2) return; |
| 8166 | PrepareStep(actions[current_action]); |
| 8167 | } |
| 8168 | |
| 8169 | TEST(DebugStepOverFunctionWithCaughtException) { |
| 8170 | i::FLAG_allow_natives_syntax = true; |
| 8171 | |
| 8172 | DebugLocalContext env; |
| 8173 | v8::Isolate* isolate = env->GetIsolate(); |
| 8174 | v8::HandleScope scope(isolate); |
| 8175 | v8::Debug::SetDebugEventListener( |
| 8176 | isolate, DebugStepOverFunctionWithCaughtExceptionListener); |
| 8177 | |
| 8178 | break_point_hit_count = 0; |
| 8179 | CompileRun( |
| 8180 | "function foo() {\n" |
| 8181 | " try { throw new Error(); } catch (e) {}\n" |
| 8182 | "}\n" |
| 8183 | "debugger;\n" |
| 8184 | "foo();\n" |
| 8185 | "foo();\n"); |
| 8186 | |
| 8187 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 8188 | CHECK_EQ(break_point_hit_count, 4); |
| 8189 | } |