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. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2750 | CHECK_EQ(45, 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 | |
| 2891 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2892 | // Test the stepping mechanism with different ICs. |
| 2893 | TEST(DebugStepLinearMixedICs) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2894 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2895 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2896 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2897 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2898 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2899 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2900 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2901 | // Create a function for testing stepping. |
| 2902 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2903 | "function bar() {};" |
| 2904 | "function foo() {" |
| 2905 | " var x;" |
| 2906 | " var index='name';" |
| 2907 | " var y = {};" |
| 2908 | " 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] | 2909 | |
| 2910 | // Run functions to allow them to get optimized. |
| 2911 | CompileRun("a=0; b=0; bar(); foo();"); |
| 2912 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2913 | SetBreakPoint(foo, 0); |
| 2914 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2915 | step_action = StepIn; |
| 2916 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2917 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2918 | |
| 2919 | // With stepping all break locations are hit. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2920 | CHECK_EQ(11, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2921 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2922 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2923 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2926 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 2927 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2928 | |
| 2929 | SetBreakPoint(foo, 0); |
| 2930 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2931 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2932 | |
| 2933 | // Without stepping only active break points are hit. |
| 2934 | CHECK_EQ(1, break_point_hit_count); |
| 2935 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2936 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2937 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2941 | TEST(DebugStepDeclarations) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2942 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2943 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2944 | |
| 2945 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2946 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2947 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2948 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2949 | // Create a function for testing stepping. Run it to allow it to get |
| 2950 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2951 | const char* src = "function foo() { " |
| 2952 | " var a;" |
| 2953 | " var b = 1;" |
| 2954 | " var c = foo;" |
| 2955 | " var d = Math.floor;" |
| 2956 | " var e = b + d(1.2);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2957 | "}" |
| 2958 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2959 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2960 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2961 | SetBreakPoint(foo, 0); |
| 2962 | |
| 2963 | // Stepping through the declarations. |
| 2964 | step_action = StepIn; |
| 2965 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2966 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2967 | CHECK_EQ(6, break_point_hit_count); |
| 2968 | |
| 2969 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2970 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 2971 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | |
| 2975 | TEST(DebugStepLocals) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2976 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2977 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2978 | |
| 2979 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2980 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2981 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2982 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2983 | // Create a function for testing stepping. Run it to allow it to get |
| 2984 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2985 | const char* src = "function foo() { " |
| 2986 | " var a,b;" |
| 2987 | " a = 1;" |
| 2988 | " b = a + 2;" |
| 2989 | " b = 1 + 2 + 3;" |
| 2990 | " a = Math.floor(b);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2991 | "}" |
| 2992 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2993 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2994 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2995 | SetBreakPoint(foo, 0); |
| 2996 | |
| 2997 | // Stepping through the declarations. |
| 2998 | step_action = StepIn; |
| 2999 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3000 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3001 | CHECK_EQ(6, break_point_hit_count); |
| 3002 | |
| 3003 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3004 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3005 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3009 | TEST(DebugStepIf) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3010 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3011 | v8::Isolate* isolate = env->GetIsolate(); |
| 3012 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3013 | |
| 3014 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3015 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3016 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3017 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3018 | // Create a function for testing stepping. Run it to allow it to get |
| 3019 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3020 | const int argc = 1; |
| 3021 | const char* src = "function foo(x) { " |
| 3022 | " a = 1;" |
| 3023 | " if (x) {" |
| 3024 | " b = 1;" |
| 3025 | " } else {" |
| 3026 | " c = 1;" |
| 3027 | " d = 1;" |
| 3028 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3029 | "}" |
| 3030 | "a=0; b=0; c=0; d=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3031 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3032 | SetBreakPoint(foo, 0); |
| 3033 | |
| 3034 | // Stepping through the true part. |
| 3035 | step_action = StepIn; |
| 3036 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3037 | v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; |
| 3038 | foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3039 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Stepping through the false part. |
| 3042 | step_action = StepIn; |
| 3043 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3044 | v8::Local<v8::Value> argv_false[argc] = {v8::False(isolate)}; |
| 3045 | foo->Call(context, env->Global(), argc, argv_false).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3046 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3047 | |
| 3048 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3049 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3050 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | |
| 3054 | TEST(DebugStepSwitch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3055 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3056 | v8::Isolate* isolate = env->GetIsolate(); |
| 3057 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3060 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3061 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3062 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3063 | // Create a function for testing stepping. Run it to allow it to get |
| 3064 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3065 | const int argc = 1; |
| 3066 | const char* src = "function foo(x) { " |
| 3067 | " a = 1;" |
| 3068 | " switch (x) {" |
| 3069 | " case 1:" |
| 3070 | " b = 1;" |
| 3071 | " case 2:" |
| 3072 | " c = 1;" |
| 3073 | " break;" |
| 3074 | " case 3:" |
| 3075 | " d = 1;" |
| 3076 | " e = 1;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3077 | " f = 1;" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3078 | " break;" |
| 3079 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3080 | "}" |
| 3081 | "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] | 3082 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3083 | SetBreakPoint(foo, 0); |
| 3084 | |
| 3085 | // One case with fall-through. |
| 3086 | step_action = StepIn; |
| 3087 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3088 | v8::Local<v8::Value> argv_1[argc] = {v8::Number::New(isolate, 1)}; |
| 3089 | foo->Call(context, env->Global(), argc, argv_1).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3090 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3091 | |
| 3092 | // Another case. |
| 3093 | step_action = StepIn; |
| 3094 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3095 | v8::Local<v8::Value> argv_2[argc] = {v8::Number::New(isolate, 2)}; |
| 3096 | foo->Call(context, env->Global(), argc, argv_2).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3097 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Last case. |
| 3100 | step_action = StepIn; |
| 3101 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3102 | v8::Local<v8::Value> argv_3[argc] = {v8::Number::New(isolate, 3)}; |
| 3103 | foo->Call(context, env->Global(), argc, argv_3).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3104 | CHECK_EQ(7, break_point_hit_count); |
| 3105 | |
| 3106 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3107 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3108 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | |
| 3112 | TEST(DebugStepWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3113 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3114 | v8::Isolate* isolate = env->GetIsolate(); |
| 3115 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3116 | |
| 3117 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3118 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3120 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3121 | // Create a function for testing stepping. Run it to allow it to get |
| 3122 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3123 | const int argc = 1; |
| 3124 | const char* src = "function foo(x) { " |
| 3125 | " var a = 0;" |
| 3126 | " while (a < x) {" |
| 3127 | " a++;" |
| 3128 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3129 | "}" |
| 3130 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3131 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3132 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3133 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3134 | // Looping 0 times. We still should break at the while-condition once. |
| 3135 | step_action = StepIn; |
| 3136 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3137 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3138 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3139 | CHECK_EQ(3, break_point_hit_count); |
| 3140 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3141 | // Looping 10 times. |
| 3142 | step_action = StepIn; |
| 3143 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3144 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3145 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3146 | CHECK_EQ(23, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3147 | |
| 3148 | // Looping 100 times. |
| 3149 | step_action = StepIn; |
| 3150 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3151 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3152 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3153 | CHECK_EQ(203, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3154 | |
| 3155 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3156 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3157 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | |
| 3161 | TEST(DebugStepDoWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3162 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3163 | v8::Isolate* isolate = env->GetIsolate(); |
| 3164 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3165 | |
| 3166 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3167 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3168 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3169 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3170 | // Create a function for testing stepping. Run it to allow it to get |
| 3171 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3172 | const int argc = 1; |
| 3173 | const char* src = "function foo(x) { " |
| 3174 | " var a = 0;" |
| 3175 | " do {" |
| 3176 | " a++;" |
| 3177 | " } while (a < x)" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3178 | "}" |
| 3179 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3180 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3181 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3182 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3183 | // Looping 0 times. |
| 3184 | step_action = StepIn; |
| 3185 | break_point_hit_count = 0; |
| 3186 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3187 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
| 3188 | CHECK_EQ(4, break_point_hit_count); |
| 3189 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3190 | // Looping 10 times. |
| 3191 | step_action = StepIn; |
| 3192 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3193 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3194 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3195 | CHECK_EQ(22, break_point_hit_count); |
| 3196 | |
| 3197 | // Looping 100 times. |
| 3198 | step_action = StepIn; |
| 3199 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3200 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3201 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3202 | CHECK_EQ(202, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3203 | |
| 3204 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3205 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3206 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | TEST(DebugStepFor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3211 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3212 | v8::Isolate* isolate = env->GetIsolate(); |
| 3213 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3216 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3217 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3218 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3219 | // Create a function for testing stepping. Run it to allow it to get |
| 3220 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3221 | const int argc = 1; |
| 3222 | const char* src = "function foo(x) { " |
| 3223 | " a = 1;" |
| 3224 | " for (i = 0; i < x; i++) {" |
| 3225 | " b = 1;" |
| 3226 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3227 | "}" |
| 3228 | "a=0; b=0; i=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3229 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3230 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3231 | SetBreakPoint(foo, 8); // "a = 1;" |
| 3232 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3233 | // Looping 0 times. |
| 3234 | step_action = StepIn; |
| 3235 | break_point_hit_count = 0; |
| 3236 | v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; |
| 3237 | foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); |
| 3238 | CHECK_EQ(4, break_point_hit_count); |
| 3239 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3240 | // Looping 10 times. |
| 3241 | step_action = StepIn; |
| 3242 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3243 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3244 | foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3245 | CHECK_EQ(34, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3246 | |
| 3247 | // Looping 100 times. |
| 3248 | step_action = StepIn; |
| 3249 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3250 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3251 | foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3252 | CHECK_EQ(304, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3255 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3256 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3260 | TEST(DebugStepForContinue) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3261 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3262 | v8::Isolate* isolate = env->GetIsolate(); |
| 3263 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3264 | |
| 3265 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3266 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3267 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3268 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3269 | // Create a function for testing stepping. Run it to allow it to get |
| 3270 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3271 | const int argc = 1; |
| 3272 | const char* src = "function foo(x) { " |
| 3273 | " var a = 0;" |
| 3274 | " var b = 0;" |
| 3275 | " var c = 0;" |
| 3276 | " for (var i = 0; i < x; i++) {" |
| 3277 | " a++;" |
| 3278 | " if (a % 2 == 0) continue;" |
| 3279 | " b++;" |
| 3280 | " c++;" |
| 3281 | " }" |
| 3282 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3283 | "}" |
| 3284 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3285 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3286 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3287 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3288 | |
| 3289 | // Each loop generates 4 or 5 steps depending on whether a is equal. |
| 3290 | |
| 3291 | // Looping 10 times. |
| 3292 | step_action = StepIn; |
| 3293 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3294 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3295 | result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3296 | CHECK_EQ(5, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3297 | CHECK_EQ(62, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3298 | |
| 3299 | // Looping 100 times. |
| 3300 | step_action = StepIn; |
| 3301 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3302 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3303 | result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3304 | CHECK_EQ(50, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3305 | CHECK_EQ(557, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3306 | |
| 3307 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3308 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3309 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | |
| 3313 | TEST(DebugStepForBreak) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3314 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3315 | v8::Isolate* isolate = env->GetIsolate(); |
| 3316 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3317 | |
| 3318 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3319 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3320 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3321 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3322 | // Create a function for testing stepping. Run it to allow it to get |
| 3323 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3324 | const int argc = 1; |
| 3325 | const char* src = "function foo(x) { " |
| 3326 | " var a = 0;" |
| 3327 | " var b = 0;" |
| 3328 | " var c = 0;" |
| 3329 | " for (var i = 0; i < 1000; i++) {" |
| 3330 | " a++;" |
| 3331 | " if (a == x) break;" |
| 3332 | " b++;" |
| 3333 | " c++;" |
| 3334 | " }" |
| 3335 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3336 | "}" |
| 3337 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3338 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3339 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3340 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3341 | |
| 3342 | // Each loop generates 5 steps except for the last (when break is executed) |
| 3343 | // which only generates 4. |
| 3344 | |
| 3345 | // Looping 10 times. |
| 3346 | step_action = StepIn; |
| 3347 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3348 | v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; |
| 3349 | result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); |
| 3350 | CHECK_EQ(9, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3351 | CHECK_EQ(64, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3352 | |
| 3353 | // Looping 100 times. |
| 3354 | step_action = StepIn; |
| 3355 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3356 | v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; |
| 3357 | result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); |
| 3358 | CHECK_EQ(99, result->Int32Value(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3359 | CHECK_EQ(604, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3360 | |
| 3361 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3362 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3363 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | |
| 3367 | TEST(DebugStepForIn) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3368 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3369 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3370 | |
| 3371 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3372 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3373 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3374 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3375 | // Create a function for testing stepping. Run it to allow it to get |
| 3376 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3377 | v8::Local<v8::Function> foo; |
| 3378 | const char* src_1 = "function foo() { " |
| 3379 | " var a = [1, 2];" |
| 3380 | " for (x in a) {" |
| 3381 | " b = 0;" |
| 3382 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3383 | "}" |
| 3384 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3385 | foo = CompileFunction(&env, src_1, "foo"); |
| 3386 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3387 | |
| 3388 | step_action = StepIn; |
| 3389 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3390 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3391 | CHECK_EQ(8, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3392 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3393 | // Create a function for testing stepping. Run it to allow it to get |
| 3394 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3395 | const char* src_2 = "function foo() { " |
| 3396 | " var a = {a:[1, 2, 3]};" |
| 3397 | " for (x in a.a) {" |
| 3398 | " b = 0;" |
| 3399 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3400 | "}" |
| 3401 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3402 | foo = CompileFunction(&env, src_2, "foo"); |
| 3403 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3404 | |
| 3405 | step_action = StepIn; |
| 3406 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3407 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3408 | CHECK_EQ(10, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3409 | |
| 3410 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3411 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3412 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | |
| 3416 | TEST(DebugStepWith) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3417 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3418 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3419 | |
| 3420 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3421 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3422 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3423 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3424 | // Create a function for testing stepping. Run it to allow it to get |
| 3425 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3426 | const char* src = "function foo(x) { " |
| 3427 | " var a = {};" |
| 3428 | " with (a) {}" |
| 3429 | " with (b) {}" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3430 | "}" |
| 3431 | "foo()"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3432 | CHECK(env->Global() |
| 3433 | ->Set(context, v8_str(env->GetIsolate(), "b"), |
| 3434 | v8::Object::New(env->GetIsolate())) |
| 3435 | .FromJust()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3436 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3437 | v8::Local<v8::Value> result; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3438 | SetBreakPoint(foo, 8); // "var a = {};" |
| 3439 | |
| 3440 | step_action = StepIn; |
| 3441 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3442 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3443 | CHECK_EQ(4, break_point_hit_count); |
| 3444 | |
| 3445 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3446 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3447 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | |
| 3451 | TEST(DebugConditional) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3452 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3453 | v8::Isolate* isolate = env->GetIsolate(); |
| 3454 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3455 | |
| 3456 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3457 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3458 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3459 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3460 | // Create a function for testing stepping. Run it to allow it to get |
| 3461 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3462 | const char* src = "function foo(x) { " |
| 3463 | " var a;" |
| 3464 | " a = x ? 1 : 2;" |
| 3465 | " return a;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3466 | "}" |
| 3467 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3468 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3469 | SetBreakPoint(foo, 0); // "var a;" |
| 3470 | |
| 3471 | step_action = StepIn; |
| 3472 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3473 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3474 | CHECK_EQ(4, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3475 | |
| 3476 | step_action = StepIn; |
| 3477 | break_point_hit_count = 0; |
| 3478 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3479 | v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; |
| 3480 | foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); |
| 3481 | CHECK_EQ(4, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3482 | |
| 3483 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3484 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3485 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3489 | TEST(StepInOutSimple) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3490 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3491 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3492 | |
| 3493 | // Create a function for checking the function when hitting a break point. |
| 3494 | frame_function_name = CompileFunction(&env, |
| 3495 | frame_function_name_source, |
| 3496 | "frame_function_name"); |
| 3497 | |
| 3498 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3499 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3500 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3501 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3502 | // Create a function for testing stepping. Run it to allow it to get |
| 3503 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3504 | const char* src = "function a() {b();c();}; " |
| 3505 | "function b() {c();}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3506 | "function c() {}; " |
| 3507 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3508 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3509 | SetBreakPoint(a, 0); |
| 3510 | |
| 3511 | // Step through invocation of a with step in. |
| 3512 | step_action = StepIn; |
| 3513 | break_point_hit_count = 0; |
| 3514 | expected_step_sequence = "abcbaca"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3515 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3516 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3517 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3518 | |
| 3519 | // Step through invocation of a with step next. |
| 3520 | step_action = StepNext; |
| 3521 | break_point_hit_count = 0; |
| 3522 | expected_step_sequence = "aaa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3523 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3524 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3525 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3526 | |
| 3527 | // Step through invocation of a with step out. |
| 3528 | step_action = StepOut; |
| 3529 | break_point_hit_count = 0; |
| 3530 | expected_step_sequence = "a"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3531 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3532 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3533 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3536 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3537 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | TEST(StepInOutTree) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3542 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3543 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3544 | |
| 3545 | // Create a function for checking the function when hitting a break point. |
| 3546 | frame_function_name = CompileFunction(&env, |
| 3547 | frame_function_name_source, |
| 3548 | "frame_function_name"); |
| 3549 | |
| 3550 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3551 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3552 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3553 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3554 | // Create a function for testing stepping. Run it to allow it to get |
| 3555 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3556 | const char* src = "function a() {b(c(d()),d());c(d());d()}; " |
| 3557 | "function b(x,y) {c();}; " |
| 3558 | "function c(x) {}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3559 | "function d() {}; " |
| 3560 | "a(); b(); c(); d()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3561 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3562 | SetBreakPoint(a, 0); |
| 3563 | |
| 3564 | // Step through invocation of a with step in. |
| 3565 | step_action = StepIn; |
| 3566 | break_point_hit_count = 0; |
| 3567 | expected_step_sequence = "adacadabcbadacada"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3568 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3569 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3570 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3571 | |
| 3572 | // Step through invocation of a with step next. |
| 3573 | step_action = StepNext; |
| 3574 | break_point_hit_count = 0; |
| 3575 | expected_step_sequence = "aaaa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3576 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3577 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3578 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3579 | |
| 3580 | // Step through invocation of a with step out. |
| 3581 | step_action = StepOut; |
| 3582 | break_point_hit_count = 0; |
| 3583 | expected_step_sequence = "a"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3584 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3585 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3586 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3587 | |
| 3588 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3589 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3590 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | |
| 3594 | TEST(StepInOutBranch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3595 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3596 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3597 | |
| 3598 | // Create a function for checking the function when hitting a break point. |
| 3599 | frame_function_name = CompileFunction(&env, |
| 3600 | frame_function_name_source, |
| 3601 | "frame_function_name"); |
| 3602 | |
| 3603 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3604 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3605 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3606 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3607 | // Create a function for testing stepping. Run it to allow it to get |
| 3608 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3609 | const char* src = "function a() {b(false);c();}; " |
| 3610 | "function b(x) {if(x){c();};}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3611 | "function c() {}; " |
| 3612 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3613 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3614 | SetBreakPoint(a, 0); |
| 3615 | |
| 3616 | // Step through invocation of a. |
| 3617 | step_action = StepIn; |
| 3618 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3619 | expected_step_sequence = "abbaca"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3620 | a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3621 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3622 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3623 | |
| 3624 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3625 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3626 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | |
| 3630 | // Test that step in does not step into native functions. |
| 3631 | TEST(DebugStepNatives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3632 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3633 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3634 | |
| 3635 | // Create a function for testing stepping. |
| 3636 | v8::Local<v8::Function> foo = CompileFunction( |
| 3637 | &env, |
| 3638 | "function foo(){debugger;Math.sin(1);}", |
| 3639 | "foo"); |
| 3640 | |
| 3641 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3642 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3643 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3644 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3645 | step_action = StepIn; |
| 3646 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3647 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3648 | |
| 3649 | // With stepping all break locations are hit. |
| 3650 | CHECK_EQ(3, break_point_hit_count); |
| 3651 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3652 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3653 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3656 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 3657 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3658 | |
| 3659 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3660 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3661 | |
| 3662 | // Without stepping only active break points are hit. |
| 3663 | CHECK_EQ(1, break_point_hit_count); |
| 3664 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3665 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3666 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | // Test that step in works with function.apply. |
| 3671 | TEST(DebugStepFunctionApply) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3672 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3673 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3674 | |
| 3675 | // Create a function for testing stepping. |
| 3676 | v8::Local<v8::Function> foo = CompileFunction( |
| 3677 | &env, |
| 3678 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3679 | "function foo(){ debugger; bar.apply(this, [1,2,3]); }", |
| 3680 | "foo"); |
| 3681 | |
| 3682 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3683 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3684 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3685 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3686 | step_action = StepIn; |
| 3687 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3688 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3689 | |
| 3690 | // With stepping all break locations are hit. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3691 | CHECK_EQ(7, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3692 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3693 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3694 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3695 | |
| 3696 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3697 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 3698 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3699 | |
| 3700 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3701 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3702 | |
| 3703 | // Without stepping only the debugger statement is hit. |
| 3704 | CHECK_EQ(1, break_point_hit_count); |
| 3705 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3706 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3707 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | |
| 3711 | // Test that step in works with function.call. |
| 3712 | TEST(DebugStepFunctionCall) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3713 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3714 | v8::Isolate* isolate = env->GetIsolate(); |
| 3715 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3716 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3717 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3718 | // Create a function for testing stepping. |
| 3719 | v8::Local<v8::Function> foo = CompileFunction( |
| 3720 | &env, |
| 3721 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3722 | "function foo(a){ debugger;" |
| 3723 | " if (a) {" |
| 3724 | " bar.call(this, 1, 2, 3);" |
| 3725 | " } else {" |
| 3726 | " bar.call(this, 0);" |
| 3727 | " }" |
| 3728 | "}", |
| 3729 | "foo"); |
| 3730 | |
| 3731 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3732 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3733 | step_action = StepIn; |
| 3734 | |
| 3735 | // Check stepping where the if condition in bar is false. |
| 3736 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3737 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3738 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3739 | |
| 3740 | // Check stepping where the if condition in bar is true. |
| 3741 | break_point_hit_count = 0; |
| 3742 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3743 | v8::Local<v8::Value> argv[argc] = {v8::True(isolate)}; |
| 3744 | foo->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3745 | CHECK_EQ(8, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3746 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3747 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3748 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3749 | |
| 3750 | // Register a debug event listener which just counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3751 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3752 | |
| 3753 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3754 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3755 | |
| 3756 | // Without stepping only the debugger statement is hit. |
| 3757 | CHECK_EQ(1, break_point_hit_count); |
| 3758 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3759 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3760 | CheckDebuggerUnloaded(isolate); |
| 3761 | } |
| 3762 | |
| 3763 | |
| 3764 | // Test that step in works with Function.call.apply. |
| 3765 | TEST(DebugStepFunctionCallApply) { |
| 3766 | DebugLocalContext env; |
| 3767 | v8::Isolate* isolate = env->GetIsolate(); |
| 3768 | v8::HandleScope scope(isolate); |
| 3769 | |
| 3770 | v8::Local<v8::Context> context = env.context(); |
| 3771 | // Create a function for testing stepping. |
| 3772 | v8::Local<v8::Function> foo = |
| 3773 | CompileFunction(&env, |
| 3774 | "function bar() { }" |
| 3775 | "function foo(){ debugger;" |
| 3776 | " Function.call.apply(bar);" |
| 3777 | " Function.call.apply(Function.call, " |
| 3778 | "[Function.call, bar]);" |
| 3779 | "}", |
| 3780 | "foo"); |
| 3781 | |
| 3782 | // Register a debug event listener which steps and counts. |
| 3783 | v8::Debug::SetDebugEventListener(isolate, DebugEventStep); |
| 3784 | step_action = StepIn; |
| 3785 | |
| 3786 | break_point_hit_count = 0; |
| 3787 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3788 | CHECK_EQ(6, break_point_hit_count); |
| 3789 | |
| 3790 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3791 | CheckDebuggerUnloaded(isolate); |
| 3792 | |
| 3793 | // Register a debug event listener which just counts. |
| 3794 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreakPointHitCount); |
| 3795 | |
| 3796 | break_point_hit_count = 0; |
| 3797 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3798 | |
| 3799 | // Without stepping only the debugger statement is hit. |
| 3800 | CHECK_EQ(1, break_point_hit_count); |
| 3801 | |
| 3802 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 3803 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3807 | // Tests that breakpoint will be hit if it's set in script. |
| 3808 | TEST(PauseInScript) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3809 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3810 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3811 | env.ExposeDebug(); |
| 3812 | |
| 3813 | // Register a debug event listener which counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3814 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3815 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3816 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3817 | // Create a script that returns a function. |
| 3818 | const char* src = "(function (evt) {})"; |
| 3819 | const char* script_name = "StepInHandlerTest"; |
| 3820 | |
| 3821 | // Set breakpoint in the script. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3822 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), script_name, 0, -1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3823 | break_point_hit_count = 0; |
| 3824 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3825 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), script_name), |
| 3826 | v8::Integer::New(env->GetIsolate(), 0)); |
| 3827 | v8::Local<v8::Script> script = |
| 3828 | v8::Script::Compile(context, v8_str(env->GetIsolate(), src), &origin) |
| 3829 | .ToLocalChecked(); |
| 3830 | v8::Local<v8::Value> r = script->Run(context).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3831 | |
| 3832 | CHECK(r->IsFunction()); |
| 3833 | CHECK_EQ(1, break_point_hit_count); |
| 3834 | |
| 3835 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3836 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3837 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 3838 | } |
| 3839 | |
| 3840 | |
| 3841 | static void DebugEventCounterCheck(int caught, int uncaught, int message) { |
| 3842 | CHECK_EQ(caught, exception_hit_count); |
| 3843 | CHECK_EQ(uncaught, uncaught_exception_hit_count); |
| 3844 | CHECK_EQ(message, message_callback_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3848 | // Test break on exceptions. For each exception break combination the number |
| 3849 | // of debug event exception callbacks and message callbacks are collected. The |
| 3850 | // number of debug event exception callbacks are used to check that the |
| 3851 | // debugger is called correctly and the number of message callbacks is used to |
| 3852 | // check that uncaught exceptions are still returned even if there is a break |
| 3853 | // for them. |
| 3854 | TEST(BreakOnException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3855 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3856 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3857 | env.ExposeDebug(); |
| 3858 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3859 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3860 | // Create functions for testing break on exception. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3861 | CompileFunction(&env, "function throws(){throw 1;}", "throws"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3862 | v8::Local<v8::Function> caught = |
| 3863 | CompileFunction(&env, |
| 3864 | "function caught(){try {throws();} catch(e) {};}", |
| 3865 | "caught"); |
| 3866 | v8::Local<v8::Function> notCaught = |
| 3867 | CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3868 | v8::Local<v8::Function> notCaughtFinally = CompileFunction( |
| 3869 | &env, "function notCaughtFinally(){try{throws();}finally{}}", |
| 3870 | "notCaughtFinally"); |
| 3871 | // In this edge case, even though this finally does not propagate the |
| 3872 | // exception, the debugger considers this uncaught, since we want to break |
| 3873 | // at the first throw for the general case where finally implicitly rethrows. |
| 3874 | v8::Local<v8::Function> edgeCaseFinally = CompileFunction( |
| 3875 | &env, "function caughtFinally(){L:try{throws();}finally{break L;}}", |
| 3876 | "caughtFinally"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3877 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3878 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 3879 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3880 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 3881 | // Initial state should be no break on exceptions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3882 | DebugEventCounterClear(); |
| 3883 | MessageCallbackCountClear(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3884 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3885 | DebugEventCounterCheck(0, 0, 0); |
| 3886 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3887 | DebugEventCounterCheck(0, 0, 1); |
| 3888 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3889 | DebugEventCounterCheck(0, 0, 2); |
| 3890 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3891 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3892 | |
| 3893 | // No break on exception |
| 3894 | DebugEventCounterClear(); |
| 3895 | MessageCallbackCountClear(); |
| 3896 | ChangeBreakOnException(false, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3897 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3898 | DebugEventCounterCheck(0, 0, 0); |
| 3899 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3900 | DebugEventCounterCheck(0, 0, 1); |
| 3901 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3902 | DebugEventCounterCheck(0, 0, 2); |
| 3903 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3904 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3905 | |
| 3906 | // Break on uncaught exception |
| 3907 | DebugEventCounterClear(); |
| 3908 | MessageCallbackCountClear(); |
| 3909 | ChangeBreakOnException(false, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3910 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3911 | DebugEventCounterCheck(0, 0, 0); |
| 3912 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3913 | DebugEventCounterCheck(1, 1, 1); |
| 3914 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3915 | DebugEventCounterCheck(2, 2, 2); |
| 3916 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3917 | DebugEventCounterCheck(3, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3918 | |
| 3919 | // Break on exception and uncaught exception |
| 3920 | DebugEventCounterClear(); |
| 3921 | MessageCallbackCountClear(); |
| 3922 | ChangeBreakOnException(true, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3923 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3924 | DebugEventCounterCheck(1, 0, 0); |
| 3925 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3926 | DebugEventCounterCheck(2, 1, 1); |
| 3927 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3928 | DebugEventCounterCheck(3, 2, 2); |
| 3929 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3930 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3931 | |
| 3932 | // Break on exception |
| 3933 | DebugEventCounterClear(); |
| 3934 | MessageCallbackCountClear(); |
| 3935 | ChangeBreakOnException(true, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3936 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3937 | DebugEventCounterCheck(1, 0, 0); |
| 3938 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3939 | DebugEventCounterCheck(2, 1, 1); |
| 3940 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3941 | DebugEventCounterCheck(3, 2, 2); |
| 3942 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3943 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3944 | |
| 3945 | // No break on exception using JavaScript |
| 3946 | DebugEventCounterClear(); |
| 3947 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3948 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3949 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3950 | DebugEventCounterCheck(0, 0, 0); |
| 3951 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3952 | DebugEventCounterCheck(0, 0, 1); |
| 3953 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3954 | DebugEventCounterCheck(0, 0, 2); |
| 3955 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3956 | DebugEventCounterCheck(0, 0, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3957 | |
| 3958 | // Break on uncaught exception using JavaScript |
| 3959 | DebugEventCounterClear(); |
| 3960 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3961 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3962 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3963 | DebugEventCounterCheck(0, 0, 0); |
| 3964 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3965 | DebugEventCounterCheck(1, 1, 1); |
| 3966 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3967 | DebugEventCounterCheck(2, 2, 2); |
| 3968 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3969 | DebugEventCounterCheck(3, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3970 | |
| 3971 | // Break on exception and uncaught exception using JavaScript |
| 3972 | DebugEventCounterClear(); |
| 3973 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3974 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3975 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3976 | DebugEventCounterCheck(1, 0, 0); |
| 3977 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3978 | DebugEventCounterCheck(2, 1, 1); |
| 3979 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3980 | DebugEventCounterCheck(3, 2, 2); |
| 3981 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3982 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3983 | |
| 3984 | // Break on exception using JavaScript |
| 3985 | DebugEventCounterClear(); |
| 3986 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3987 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3988 | caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3989 | DebugEventCounterCheck(1, 0, 0); |
| 3990 | CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3991 | DebugEventCounterCheck(2, 1, 1); |
| 3992 | CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); |
| 3993 | DebugEventCounterCheck(3, 2, 2); |
| 3994 | edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 3995 | DebugEventCounterCheck(4, 3, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3996 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3997 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 3998 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 3999 | env->GetIsolate()->RemoveMessageListeners(MessageCallbackCount); |
| 4000 | } |
| 4001 | |
| 4002 | |
| 4003 | static void try_finally_original_message(v8::Local<v8::Message> message, |
| 4004 | v8::Local<v8::Value> data) { |
| 4005 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
| 4006 | CHECK_EQ(2, message->GetLineNumber(context).FromJust()); |
| 4007 | CHECK_EQ(2, message->GetStartColumn(context).FromJust()); |
| 4008 | message_callback_count++; |
| 4009 | } |
| 4010 | |
| 4011 | |
| 4012 | TEST(TryFinallyOriginalMessage) { |
| 4013 | // Test that the debugger plays nicely with the pending message. |
| 4014 | message_callback_count = 0; |
| 4015 | DebugEventCounterClear(); |
| 4016 | DebugLocalContext env; |
| 4017 | v8::Isolate* isolate = CcTest::isolate(); |
| 4018 | isolate->AddMessageListener(try_finally_original_message); |
| 4019 | v8::Debug::SetDebugEventListener(isolate, DebugEventCounter); |
| 4020 | ChangeBreakOnException(true, true); |
| 4021 | v8::HandleScope scope(isolate); |
| 4022 | CompileRun( |
| 4023 | "try {\n" |
| 4024 | " throw 1;\n" |
| 4025 | "} finally {\n" |
| 4026 | "}\n"); |
| 4027 | DebugEventCounterCheck(1, 1, 1); |
| 4028 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4029 | isolate->RemoveMessageListeners(try_finally_original_message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4033 | TEST(EvalJSInDebugEventListenerOnNativeReThrownException) { |
| 4034 | DebugLocalContext env; |
| 4035 | v8::HandleScope scope(env->GetIsolate()); |
| 4036 | env.ExposeDebug(); |
| 4037 | |
| 4038 | // Create functions for testing break on exception. |
| 4039 | v8::Local<v8::Function> noThrowJS = CompileFunction( |
| 4040 | &env, "function noThrowJS(){var a=[1]; a.push(2); return a.length;}", |
| 4041 | "noThrowJS"); |
| 4042 | |
| 4043 | debug_event_listener_callback = noThrowJS; |
| 4044 | debug_event_listener_callback_result = 2; |
| 4045 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4046 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 4047 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4048 | // Break on uncaught exception |
| 4049 | ChangeBreakOnException(false, true); |
| 4050 | DebugEventCounterClear(); |
| 4051 | MessageCallbackCountClear(); |
| 4052 | |
| 4053 | // ReThrow native error |
| 4054 | { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4055 | v8::TryCatch tryCatch(env->GetIsolate()); |
| 4056 | env->GetIsolate()->ThrowException( |
| 4057 | v8::Exception::TypeError(v8_str(env->GetIsolate(), "Type error"))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4058 | CHECK(tryCatch.HasCaught()); |
| 4059 | tryCatch.ReThrow(); |
| 4060 | } |
| 4061 | CHECK_EQ(1, exception_hit_count); |
| 4062 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4063 | CHECK_EQ(0, message_callback_count); // FIXME: Should it be 1 ? |
| 4064 | CHECK(!debug_event_listener_callback.IsEmpty()); |
| 4065 | |
| 4066 | debug_event_listener_callback.Clear(); |
| 4067 | } |
| 4068 | |
| 4069 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4070 | // Test break on exception from compiler errors. When compiling using |
| 4071 | // v8::Script::Compile there is no JavaScript stack whereas when compiling using |
| 4072 | // eval there are JavaScript frames. |
| 4073 | TEST(BreakOnCompileException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4074 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4075 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4076 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4077 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4078 | // For this test, we want to break on uncaught exceptions: |
| 4079 | ChangeBreakOnException(false, true); |
| 4080 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4081 | // Create a function for checking the function when hitting a break point. |
| 4082 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 4083 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4084 | env->GetIsolate()->AddMessageListener(MessageCallbackCount); |
| 4085 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4086 | |
| 4087 | DebugEventCounterClear(); |
| 4088 | MessageCallbackCountClear(); |
| 4089 | |
| 4090 | // Check initial state. |
| 4091 | CHECK_EQ(0, exception_hit_count); |
| 4092 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 4093 | CHECK_EQ(0, message_callback_count); |
| 4094 | CHECK_EQ(-1, last_js_stack_height); |
| 4095 | |
| 4096 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4097 | CHECK( |
| 4098 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4099 | CHECK_EQ(1, exception_hit_count); |
| 4100 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4101 | CHECK_EQ(1, message_callback_count); |
| 4102 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4103 | |
| 4104 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4105 | CHECK( |
| 4106 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "x x")).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4107 | CHECK_EQ(2, exception_hit_count); |
| 4108 | CHECK_EQ(2, uncaught_exception_hit_count); |
| 4109 | CHECK_EQ(2, message_callback_count); |
| 4110 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4111 | |
| 4112 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4113 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('+++')")) |
| 4114 | .ToLocalChecked() |
| 4115 | ->Run(context) |
| 4116 | .IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4117 | CHECK_EQ(3, exception_hit_count); |
| 4118 | CHECK_EQ(3, uncaught_exception_hit_count); |
| 4119 | CHECK_EQ(3, message_callback_count); |
| 4120 | CHECK_EQ(1, last_js_stack_height); |
| 4121 | |
| 4122 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4123 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('x x')")) |
| 4124 | .ToLocalChecked() |
| 4125 | ->Run(context) |
| 4126 | .IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4127 | CHECK_EQ(4, exception_hit_count); |
| 4128 | CHECK_EQ(4, uncaught_exception_hit_count); |
| 4129 | CHECK_EQ(4, message_callback_count); |
| 4130 | CHECK_EQ(1, last_js_stack_height); |
| 4131 | } |
| 4132 | |
| 4133 | |
| 4134 | TEST(StepWithException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4135 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4136 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4137 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4138 | // For this test, we want to break on uncaught exceptions: |
| 4139 | ChangeBreakOnException(false, true); |
| 4140 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4141 | // Create a function for checking the function when hitting a break point. |
| 4142 | frame_function_name = CompileFunction(&env, |
| 4143 | frame_function_name_source, |
| 4144 | "frame_function_name"); |
| 4145 | |
| 4146 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4147 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4149 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4150 | // Create functions for testing stepping. |
| 4151 | const char* src = "function a() { n(); }; " |
| 4152 | "function b() { c(); }; " |
| 4153 | "function c() { n(); }; " |
| 4154 | "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " |
| 4155 | "function e() { n(); }; " |
| 4156 | "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " |
| 4157 | "function g() { h(); }; " |
| 4158 | "function h() { x = 1; throw 1; }; "; |
| 4159 | |
| 4160 | // Step through invocation of a. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4161 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4162 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 4163 | SetBreakPoint(a, 0); |
| 4164 | step_action = StepIn; |
| 4165 | break_point_hit_count = 0; |
| 4166 | expected_step_sequence = "aa"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4167 | CHECK(a->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4168 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4169 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4170 | |
| 4171 | // Step through invocation of b + c. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4172 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4173 | v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); |
| 4174 | SetBreakPoint(b, 0); |
| 4175 | step_action = StepIn; |
| 4176 | break_point_hit_count = 0; |
| 4177 | expected_step_sequence = "bcc"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4178 | CHECK(b->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4179 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4180 | break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4181 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4182 | // Step through invocation of d + e. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4183 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4184 | v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); |
| 4185 | SetBreakPoint(d, 0); |
| 4186 | ChangeBreakOnException(false, true); |
| 4187 | step_action = StepIn; |
| 4188 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4189 | expected_step_sequence = "ddedd"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4190 | d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4191 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4192 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4193 | |
| 4194 | // Step through invocation of d + e now with break on caught exceptions. |
| 4195 | ChangeBreakOnException(true, true); |
| 4196 | step_action = StepIn; |
| 4197 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4198 | expected_step_sequence = "ddeedd"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4199 | d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4200 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4201 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4202 | |
| 4203 | // Step through invocation of f + g + h. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4204 | ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4205 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4206 | SetBreakPoint(f, 0); |
| 4207 | ChangeBreakOnException(false, true); |
| 4208 | step_action = StepIn; |
| 4209 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4210 | expected_step_sequence = "ffghhff"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4211 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4212 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4213 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4214 | |
| 4215 | // Step through invocation of f + g + h now with break on caught exceptions. |
| 4216 | ChangeBreakOnException(true, true); |
| 4217 | step_action = StepIn; |
| 4218 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4219 | expected_step_sequence = "ffghhhff"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4220 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4221 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4222 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4223 | |
| 4224 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4225 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 4226 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | |
| 4230 | TEST(DebugBreak) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4231 | i::FLAG_stress_compaction = false; |
| 4232 | #ifdef VERIFY_HEAP |
| 4233 | i::FLAG_verify_heap = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4234 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4235 | DebugLocalContext env; |
| 4236 | v8::Isolate* isolate = env->GetIsolate(); |
| 4237 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4238 | |
| 4239 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4240 | v8::Debug::SetDebugEventListener(isolate, DebugEventBreak); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4241 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4242 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4243 | // Create a function for testing stepping. |
| 4244 | const char* src = "function f0() {}" |
| 4245 | "function f1(x1) {}" |
| 4246 | "function f2(x1,x2) {}" |
| 4247 | "function f3(x1,x2,x3) {}"; |
| 4248 | v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); |
| 4249 | v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); |
| 4250 | v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); |
| 4251 | v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); |
| 4252 | |
| 4253 | // Call the function to make sure it is compiled. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4254 | v8::Local<v8::Value> argv[] = { |
| 4255 | v8::Number::New(isolate, 1), v8::Number::New(isolate, 1), |
| 4256 | v8::Number::New(isolate, 1), v8::Number::New(isolate, 1)}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4257 | |
| 4258 | // Call all functions to make sure that they are compiled. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4259 | f0->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4260 | f1->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4261 | f2->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
| 4262 | f3->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4263 | |
| 4264 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4265 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4266 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4267 | |
| 4268 | // Call all functions with different argument count. |
| 4269 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4270 | for (unsigned int i = 0; i < arraysize(argv); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4271 | f0->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4272 | f1->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4273 | f2->Call(context, env->Global(), i, argv).ToLocalChecked(); |
| 4274 | f3->Call(context, env->Global(), i, argv).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | // One break for each function called. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4278 | CHECK(4 * arraysize(argv) == break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4279 | |
| 4280 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4281 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4282 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | |
| 4286 | // Test to ensure that JavaScript code keeps running while the debug break |
| 4287 | // through the stack limit flag is set but breaks are disabled. |
| 4288 | TEST(DisableBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4289 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4290 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4291 | |
| 4292 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4293 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4294 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4295 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4296 | // Create a function for testing stepping. |
| 4297 | const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; |
| 4298 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4299 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4300 | // Set, test and cancel debug break. |
| 4301 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4302 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4303 | v8::Debug::CancelDebugBreak(env->GetIsolate()); |
| 4304 | CHECK(!v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4305 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4306 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4307 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4308 | |
| 4309 | // Call all functions with different argument count. |
| 4310 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4311 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4312 | CHECK_EQ(1, break_point_hit_count); |
| 4313 | |
| 4314 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4315 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4316 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); |
| 4317 | v8::internal::DisableBreak disable_break(isolate->debug(), true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4318 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4319 | CHECK_EQ(1, break_point_hit_count); |
| 4320 | } |
| 4321 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4322 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4323 | CHECK_EQ(2, break_point_hit_count); |
| 4324 | |
| 4325 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4326 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 4327 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 4328 | } |
| 4329 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4330 | TEST(DisableDebuggerStatement) { |
| 4331 | DebugLocalContext env; |
| 4332 | v8::HandleScope scope(env->GetIsolate()); |
| 4333 | |
| 4334 | // Register a debug event listener which sets the break flag and counts. |
| 4335 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventCounter); |
| 4336 | CompileRun("debugger;"); |
| 4337 | CHECK_EQ(1, break_point_hit_count); |
| 4338 | |
| 4339 | // Check that we ignore debugger statement when breakpoints aren't active. |
| 4340 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); |
| 4341 | isolate->debug()->set_break_points_active(false); |
| 4342 | CompileRun("debugger;"); |
| 4343 | CHECK_EQ(1, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4346 | static const char* kSimpleExtensionSource = |
| 4347 | "(function Foo() {" |
| 4348 | " return 4;" |
| 4349 | "})() "; |
| 4350 | |
| 4351 | // http://crbug.com/28933 |
| 4352 | // Test that debug break is disabled when bootstrapper is active. |
| 4353 | TEST(NoBreakWhenBootstrapping) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4354 | v8::Isolate* isolate = CcTest::isolate(); |
| 4355 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4356 | |
| 4357 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4358 | v8::Debug::SetDebugEventListener(isolate, DebugEventCounter); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4359 | |
| 4360 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4361 | v8::Debug::DebugBreak(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4362 | break_point_hit_count = 0; |
| 4363 | { |
| 4364 | // Create a context with an extension to make sure that some JavaScript |
| 4365 | // code is executed during bootstrapping. |
| 4366 | v8::RegisterExtension(new v8::Extension("simpletest", |
| 4367 | kSimpleExtensionSource)); |
| 4368 | const char* extension_names[] = { "simpletest" }; |
| 4369 | v8::ExtensionConfiguration extensions(1, extension_names); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4370 | v8::HandleScope handle_scope(isolate); |
| 4371 | v8::Context::New(isolate, &extensions); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4372 | } |
| 4373 | // Check that no DebugBreak events occured during the context creation. |
| 4374 | CHECK_EQ(0, break_point_hit_count); |
| 4375 | |
| 4376 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4377 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 4378 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4379 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4380 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4381 | |
| 4382 | static void NamedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4383 | v8::Local<v8::Array> result = v8::Array::New(info.GetIsolate(), 3); |
| 4384 | v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
| 4385 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 0), |
| 4386 | v8_str(info.GetIsolate(), "a")) |
| 4387 | .FromJust()); |
| 4388 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 1), |
| 4389 | v8_str(info.GetIsolate(), "b")) |
| 4390 | .FromJust()); |
| 4391 | CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 2), |
| 4392 | v8_str(info.GetIsolate(), "c")) |
| 4393 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4394 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4398 | static void IndexedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
| 4399 | v8::Isolate* isolate = info.GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4400 | v8::Local<v8::Array> result = v8::Array::New(isolate, 2); |
| 4401 | v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
| 4402 | CHECK(result->Set(context, v8::Integer::New(isolate, 0), |
| 4403 | v8::Number::New(isolate, 1)) |
| 4404 | .FromJust()); |
| 4405 | CHECK(result->Set(context, v8::Integer::New(isolate, 1), |
| 4406 | v8::Number::New(isolate, 10)) |
| 4407 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4408 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4412 | static void NamedGetter(v8::Local<v8::Name> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4413 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4414 | if (name->IsSymbol()) return; |
| 4415 | v8::String::Utf8Value n(v8::Local<v8::String>::Cast(name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4416 | if (strcmp(*n, "a") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4417 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "AA")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4418 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4419 | } else if (strcmp(*n, "b") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4420 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "BB")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4421 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4422 | } else if (strcmp(*n, "c") == 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4423 | info.GetReturnValue().Set(v8_str(info.GetIsolate(), "CC")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4424 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4425 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4426 | info.GetReturnValue().SetUndefined(); |
| 4427 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4428 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4429 | info.GetReturnValue().Set(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4433 | static void IndexedGetter(uint32_t index, |
| 4434 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4435 | info.GetReturnValue().Set(static_cast<double>(index + 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | |
| 4439 | TEST(InterceptorPropertyMirror) { |
| 4440 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4441 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4442 | v8::Isolate* isolate = env->GetIsolate(); |
| 4443 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4444 | env.ExposeDebug(); |
| 4445 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4446 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4447 | // Create object with named interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4448 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4449 | named->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4450 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4451 | CHECK(env->Global() |
| 4452 | ->Set(context, v8_str(isolate, "intercepted_named"), |
| 4453 | named->NewInstance(context).ToLocalChecked()) |
| 4454 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4455 | |
| 4456 | // Create object with indexed interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4457 | v8::Local<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4458 | indexed->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4459 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4460 | CHECK(env->Global() |
| 4461 | ->Set(context, v8_str(isolate, "intercepted_indexed"), |
| 4462 | indexed->NewInstance(context).ToLocalChecked()) |
| 4463 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4464 | |
| 4465 | // Create object with both named and indexed interceptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4466 | v8::Local<v8::ObjectTemplate> both = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4467 | both->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4468 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
| 4469 | both->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4470 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4471 | CHECK(env->Global() |
| 4472 | ->Set(context, v8_str(isolate, "intercepted_both"), |
| 4473 | both->NewInstance(context).ToLocalChecked()) |
| 4474 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4475 | |
| 4476 | // Get mirrors for the three objects with interceptor. |
| 4477 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4478 | "var named_mirror = debug.MakeMirror(intercepted_named);" |
| 4479 | "var indexed_mirror = debug.MakeMirror(intercepted_indexed);" |
| 4480 | "var both_mirror = debug.MakeMirror(intercepted_both)"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4481 | CHECK(CompileRun("named_mirror instanceof debug.ObjectMirror") |
| 4482 | ->BooleanValue(context) |
| 4483 | .FromJust()); |
| 4484 | CHECK(CompileRun("indexed_mirror instanceof debug.ObjectMirror") |
| 4485 | ->BooleanValue(context) |
| 4486 | .FromJust()); |
| 4487 | CHECK(CompileRun("both_mirror instanceof debug.ObjectMirror") |
| 4488 | ->BooleanValue(context) |
| 4489 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Get the property names from the interceptors |
| 4492 | CompileRun( |
| 4493 | "named_names = named_mirror.propertyNames();" |
| 4494 | "indexed_names = indexed_mirror.propertyNames();" |
| 4495 | "both_names = both_mirror.propertyNames()"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4496 | CHECK_EQ(3, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4497 | CHECK_EQ(2, |
| 4498 | CompileRun("indexed_names.length")->Int32Value(context).FromJust()); |
| 4499 | CHECK_EQ(5, CompileRun("both_names.length")->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4500 | |
| 4501 | // Check the expected number of properties. |
| 4502 | const char* source; |
| 4503 | source = "named_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4504 | CHECK_EQ(3, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4505 | |
| 4506 | source = "indexed_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4507 | CHECK_EQ(2, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4508 | |
| 4509 | source = "both_mirror.properties().length"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4510 | CHECK_EQ(5, CompileRun(source)->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4511 | |
| 4512 | // Get the interceptor properties for the object with only named interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4513 | CompileRun("var named_values = named_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4514 | |
| 4515 | // Check that the properties are interceptor properties. |
| 4516 | for (int i = 0; i < 3; i++) { |
| 4517 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4518 | SNPrintF(buffer, |
| 4519 | "named_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4520 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4521 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4522 | SNPrintF(buffer, "named_values[%d].isNative()", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4523 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | // Get the interceptor properties for the object with only indexed |
| 4527 | // interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4528 | CompileRun("var indexed_values = indexed_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4529 | |
| 4530 | // Check that the properties are interceptor properties. |
| 4531 | for (int i = 0; i < 2; i++) { |
| 4532 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4533 | SNPrintF(buffer, |
| 4534 | "indexed_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4535 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | // Get the interceptor properties for the object with both types of |
| 4539 | // interceptors. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4540 | CompileRun("var both_values = both_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4541 | |
| 4542 | // Check that the properties are interceptor properties. |
| 4543 | for (int i = 0; i < 5; i++) { |
| 4544 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4545 | SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4546 | CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | // Check the property names. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4550 | source = "both_values[0].name() == '1'"; |
| 4551 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4552 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4553 | source = "both_values[1].name() == '10'"; |
| 4554 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4555 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4556 | source = "both_values[2].name() == 'a'"; |
| 4557 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4558 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4559 | source = "both_values[3].name() == 'b'"; |
| 4560 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4561 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4562 | source = "both_values[4].name() == 'c'"; |
| 4563 | CHECK(CompileRun(source)->BooleanValue(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | |
| 4567 | TEST(HiddenPrototypePropertyMirror) { |
| 4568 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4569 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4570 | v8::Isolate* isolate = env->GetIsolate(); |
| 4571 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4572 | env.ExposeDebug(); |
| 4573 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4574 | v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4575 | t0->InstanceTemplate()->Set(v8_str(isolate, "x"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4576 | v8::Number::New(isolate, 0)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4577 | v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4578 | t1->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4579 | t1->InstanceTemplate()->Set(v8_str(isolate, "y"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4580 | v8::Number::New(isolate, 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4581 | v8::Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4582 | t2->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4583 | t2->InstanceTemplate()->Set(v8_str(isolate, "z"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4584 | v8::Number::New(isolate, 2)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4585 | v8::Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(isolate); |
| 4586 | t3->InstanceTemplate()->Set(v8_str(isolate, "u"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4587 | v8::Number::New(isolate, 3)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4588 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4589 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4590 | // Create object and set them on the global object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4591 | v8::Local<v8::Object> o0 = t0->GetFunction(context) |
| 4592 | .ToLocalChecked() |
| 4593 | ->NewInstance(context) |
| 4594 | .ToLocalChecked(); |
| 4595 | CHECK(env->Global()->Set(context, v8_str(isolate, "o0"), o0).FromJust()); |
| 4596 | v8::Local<v8::Object> o1 = t1->GetFunction(context) |
| 4597 | .ToLocalChecked() |
| 4598 | ->NewInstance(context) |
| 4599 | .ToLocalChecked(); |
| 4600 | CHECK(env->Global()->Set(context, v8_str(isolate, "o1"), o1).FromJust()); |
| 4601 | v8::Local<v8::Object> o2 = t2->GetFunction(context) |
| 4602 | .ToLocalChecked() |
| 4603 | ->NewInstance(context) |
| 4604 | .ToLocalChecked(); |
| 4605 | CHECK(env->Global()->Set(context, v8_str(isolate, "o2"), o2).FromJust()); |
| 4606 | v8::Local<v8::Object> o3 = t3->GetFunction(context) |
| 4607 | .ToLocalChecked() |
| 4608 | ->NewInstance(context) |
| 4609 | .ToLocalChecked(); |
| 4610 | CHECK(env->Global()->Set(context, v8_str(isolate, "o3"), o3).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Get mirrors for the four objects. |
| 4613 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4614 | "var o0_mirror = debug.MakeMirror(o0);" |
| 4615 | "var o1_mirror = debug.MakeMirror(o1);" |
| 4616 | "var o2_mirror = debug.MakeMirror(o2);" |
| 4617 | "var o3_mirror = debug.MakeMirror(o3)"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4618 | CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror") |
| 4619 | ->BooleanValue(context) |
| 4620 | .FromJust()); |
| 4621 | CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror") |
| 4622 | ->BooleanValue(context) |
| 4623 | .FromJust()); |
| 4624 | CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror") |
| 4625 | ->BooleanValue(context) |
| 4626 | .FromJust()); |
| 4627 | CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror") |
| 4628 | ->BooleanValue(context) |
| 4629 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Check that each object has one property. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4632 | CHECK_EQ(1, CompileRun("o0_mirror.propertyNames().length") |
| 4633 | ->Int32Value(context) |
| 4634 | .FromJust()); |
| 4635 | CHECK_EQ(1, CompileRun("o1_mirror.propertyNames().length") |
| 4636 | ->Int32Value(context) |
| 4637 | .FromJust()); |
| 4638 | CHECK_EQ(1, CompileRun("o2_mirror.propertyNames().length") |
| 4639 | ->Int32Value(context) |
| 4640 | .FromJust()); |
| 4641 | CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") |
| 4642 | ->Int32Value(context) |
| 4643 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4644 | |
| 4645 | // Set o1 as prototype for o0. o1 has the hidden prototype flag so all |
| 4646 | // properties on o1 should be seen on o0. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4647 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o1).FromJust()); |
| 4648 | CHECK_EQ(2, CompileRun("o0_mirror.propertyNames().length") |
| 4649 | ->Int32Value(context) |
| 4650 | .FromJust()); |
| 4651 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4652 | ->Int32Value(context) |
| 4653 | .FromJust()); |
| 4654 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4655 | ->Int32Value(context) |
| 4656 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4657 | |
| 4658 | // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden |
| 4659 | // prototype flag. o2 also has the hidden prototype flag so all properties |
| 4660 | // 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] | 4661 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o2).FromJust()); |
| 4662 | CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") |
| 4663 | ->Int32Value(context) |
| 4664 | .FromJust()); |
| 4665 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4666 | ->Int32Value(context) |
| 4667 | .FromJust()); |
| 4668 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4669 | ->Int32Value(context) |
| 4670 | .FromJust()); |
| 4671 | CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") |
| 4672 | ->Int32Value(context) |
| 4673 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4674 | |
| 4675 | // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and |
| 4676 | // o2 has the hidden prototype flag. o3 does not have the hidden prototype |
| 4677 | // flag so properties on o3 should not be seen on o0 whereas the properties |
| 4678 | // from o1 and o2 should still be seen on o0. |
| 4679 | // Final prototype chain: o0 -> o1 -> o2 -> o3 |
| 4680 | // Hidden prototypes: ^^ ^^ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4681 | CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o3).FromJust()); |
| 4682 | CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") |
| 4683 | ->Int32Value(context) |
| 4684 | .FromJust()); |
| 4685 | CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") |
| 4686 | ->Int32Value(context) |
| 4687 | .FromJust()); |
| 4688 | CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") |
| 4689 | ->Int32Value(context) |
| 4690 | .FromJust()); |
| 4691 | CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") |
| 4692 | ->Int32Value(context) |
| 4693 | .FromJust()); |
| 4694 | CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") |
| 4695 | ->Int32Value(context) |
| 4696 | .FromJust()); |
| 4697 | CHECK(CompileRun("o0_mirror.property('u').isUndefined()") |
| 4698 | ->BooleanValue(context) |
| 4699 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4700 | |
| 4701 | // 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] | 4702 | CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror") |
| 4703 | ->BooleanValue(context) |
| 4704 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4708 | static void ProtperyXNativeGetter( |
| 4709 | v8::Local<v8::String> property, |
| 4710 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4711 | info.GetReturnValue().Set(10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | |
| 4715 | TEST(NativeGetterPropertyMirror) { |
| 4716 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4717 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4718 | v8::Isolate* isolate = env->GetIsolate(); |
| 4719 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4720 | env.ExposeDebug(); |
| 4721 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4722 | v8::Local<v8::Context> context = env.context(); |
| 4723 | v8::Local<v8::String> name = v8_str(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4724 | // Create object with named accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4725 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
| 4726 | named->SetAccessor(name, &ProtperyXNativeGetter, NULL, v8::Local<v8::Value>(), |
| 4727 | v8::DEFAULT, v8::None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4728 | |
| 4729 | // Create object with named property getter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4730 | CHECK(env->Global() |
| 4731 | ->Set(context, v8_str(isolate, "instance"), |
| 4732 | named->NewInstance(context).ToLocalChecked()) |
| 4733 | .FromJust()); |
| 4734 | CHECK_EQ(10, CompileRun("instance.x")->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4737 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4738 | CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") |
| 4739 | ->BooleanValue(context) |
| 4740 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4741 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4742 | CompileRun("var named_names = instance_mirror.propertyNames();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4743 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4744 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); |
| 4745 | CHECK(CompileRun("instance_mirror.property('x').value().isNumber()") |
| 4746 | ->BooleanValue(context) |
| 4747 | .FromJust()); |
| 4748 | CHECK(CompileRun("instance_mirror.property('x').value().value() == 10") |
| 4749 | ->BooleanValue(context) |
| 4750 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4754 | static void ProtperyXNativeGetterThrowingError( |
| 4755 | v8::Local<v8::String> property, |
| 4756 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4757 | CompileRun("throw new Error('Error message');"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | TEST(NativeGetterThrowingErrorPropertyMirror) { |
| 4762 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4763 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4764 | v8::Isolate* isolate = env->GetIsolate(); |
| 4765 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4766 | env.ExposeDebug(); |
| 4767 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4768 | v8::Local<v8::Context> context = env.context(); |
| 4769 | v8::Local<v8::String> name = v8_str(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4770 | // Create object with named accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4771 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4772 | named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4773 | v8::Local<v8::Value>(), v8::DEFAULT, v8::None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4774 | |
| 4775 | // Create object with named property getter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4776 | CHECK(env->Global() |
| 4777 | ->Set(context, v8_str(isolate, "instance"), |
| 4778 | named->NewInstance(context).ToLocalChecked()) |
| 4779 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4780 | |
| 4781 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4782 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4783 | CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") |
| 4784 | ->BooleanValue(context) |
| 4785 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4786 | CompileRun("named_names = instance_mirror.propertyNames();"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4787 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4788 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); |
| 4789 | CHECK(CompileRun("instance_mirror.property('x').value().isError()") |
| 4790 | ->BooleanValue(context) |
| 4791 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4792 | |
| 4793 | // Check that the message is that passed to the Error constructor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4794 | CHECK( |
| 4795 | CompileRun( |
| 4796 | "instance_mirror.property('x').value().message() == 'Error message'") |
| 4797 | ->BooleanValue(context) |
| 4798 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4802 | // Test that hidden properties object is not returned as an unnamed property |
| 4803 | // among regular properties. |
| 4804 | // See http://crbug.com/26491 |
| 4805 | TEST(NoHiddenProperties) { |
| 4806 | // Create a V8 environment with debug access. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4807 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4808 | v8::Isolate* isolate = env->GetIsolate(); |
| 4809 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4810 | env.ExposeDebug(); |
| 4811 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4812 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4813 | // Create an object in the global scope. |
| 4814 | const char* source = "var obj = {a: 1};"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4815 | v8::Script::Compile(context, v8_str(isolate, source)) |
| 4816 | .ToLocalChecked() |
| 4817 | ->Run(context) |
| 4818 | .ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4819 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4820 | env->Global()->Get(context, v8_str(isolate, "obj")).ToLocalChecked()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4821 | // Set a hidden property on the object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4822 | obj->SetPrivate( |
| 4823 | env.context(), |
| 4824 | v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::a")), |
| 4825 | v8::Int32::New(isolate, 11)) |
| 4826 | .FromJust(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4827 | |
| 4828 | // Get mirror for the object with property getter. |
| 4829 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4830 | CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") |
| 4831 | ->BooleanValue(context) |
| 4832 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4833 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4834 | // There should be exactly one property. But there is also an unnamed |
| 4835 | // property whose value is hidden properties dictionary. The latter |
| 4836 | // property should not be in the list of reguar properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4837 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
| 4838 | CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue(context).FromJust()); |
| 4839 | CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") |
| 4840 | ->BooleanValue(context) |
| 4841 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4842 | |
| 4843 | // Object created by t0 will become hidden prototype of object 'obj'. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4844 | v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4845 | t0->InstanceTemplate()->Set(v8_str(isolate, "b"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4846 | v8::Number::New(isolate, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4847 | t0->SetHiddenPrototype(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4848 | v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
| 4849 | t1->InstanceTemplate()->Set(v8_str(isolate, "c"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4850 | v8::Number::New(isolate, 3)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4851 | |
| 4852 | // Create proto objects, add hidden properties to them and set them on |
| 4853 | // the global object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4854 | v8::Local<v8::Object> protoObj = t0->GetFunction(context) |
| 4855 | .ToLocalChecked() |
| 4856 | ->NewInstance(context) |
| 4857 | .ToLocalChecked(); |
| 4858 | protoObj->SetPrivate( |
| 4859 | env.context(), |
| 4860 | v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::b")), |
| 4861 | v8::Int32::New(isolate, 12)) |
| 4862 | .FromJust(); |
| 4863 | CHECK(env->Global() |
| 4864 | ->Set(context, v8_str(isolate, "protoObj"), protoObj) |
| 4865 | .FromJust()); |
| 4866 | v8::Local<v8::Object> grandProtoObj = t1->GetFunction(context) |
| 4867 | .ToLocalChecked() |
| 4868 | ->NewInstance(context) |
| 4869 | .ToLocalChecked(); |
| 4870 | grandProtoObj->SetPrivate(env.context(), |
| 4871 | v8::Private::New( |
| 4872 | isolate, v8_str(isolate, "v8::test-debug::c")), |
| 4873 | v8::Int32::New(isolate, 13)) |
| 4874 | .FromJust(); |
| 4875 | CHECK(env->Global() |
| 4876 | ->Set(context, v8_str(isolate, "grandProtoObj"), grandProtoObj) |
| 4877 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Setting prototypes: obj->protoObj->grandProtoObj |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4880 | CHECK(protoObj->Set(context, v8_str(isolate, "__proto__"), grandProtoObj) |
| 4881 | .FromJust()); |
| 4882 | CHECK(obj->Set(context, v8_str(isolate, "__proto__"), protoObj).FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4883 | |
| 4884 | // Get mirror for the object with property getter. |
| 4885 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4886 | CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") |
| 4887 | ->BooleanValue(context) |
| 4888 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4889 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4890 | // There should be exactly two properties - one from the object itself and |
| 4891 | // another from its hidden prototype. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4892 | CHECK_EQ(2, CompileRun("named_names.length")->Int32Value(context).FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4893 | CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4894 | "named_names[1] == 'b'") |
| 4895 | ->BooleanValue(context) |
| 4896 | .FromJust()); |
| 4897 | CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") |
| 4898 | ->BooleanValue(context) |
| 4899 | .FromJust()); |
| 4900 | CHECK(CompileRun("obj_mirror.property('b').value().value() == 2") |
| 4901 | ->BooleanValue(context) |
| 4902 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4905 | |
| 4906 | // Multithreaded tests of JSON debugger protocol |
| 4907 | |
| 4908 | // Support classes |
| 4909 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4910 | // Provides synchronization between N threads, where N is a template parameter. |
| 4911 | // The Wait() call blocks a thread until it is called for the Nth time, then all |
| 4912 | // calls return. Each ThreadBarrier object can only be used once. |
| 4913 | template <int N> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4914 | class ThreadBarrier final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4915 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4916 | ThreadBarrier() : num_blocked_(0) {} |
| 4917 | |
| 4918 | ~ThreadBarrier() { |
| 4919 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4920 | if (num_blocked_ != 0) { |
| 4921 | CHECK_EQ(N, num_blocked_); |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | void Wait() { |
| 4926 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4927 | CHECK_LT(num_blocked_, N); |
| 4928 | num_blocked_++; |
| 4929 | if (N == num_blocked_) { |
| 4930 | // Signal and unblock all waiting threads. |
| 4931 | cv_.NotifyAll(); |
| 4932 | printf("BARRIER\n\n"); |
| 4933 | fflush(stdout); |
| 4934 | } else { // Wait for the semaphore. |
| 4935 | while (num_blocked_ < N) { |
| 4936 | cv_.Wait(&mutex_); |
| 4937 | } |
| 4938 | } |
| 4939 | CHECK_EQ(N, num_blocked_); |
| 4940 | } |
| 4941 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4942 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4943 | ConditionVariable cv_; |
| 4944 | Mutex mutex_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4945 | int num_blocked_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4946 | |
| 4947 | STATIC_ASSERT(N > 0); |
| 4948 | |
| 4949 | DISALLOW_COPY_AND_ASSIGN(ThreadBarrier); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4950 | }; |
| 4951 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4952 | |
| 4953 | // A set containing enough barriers and semaphores for any of the tests. |
| 4954 | class Barriers { |
| 4955 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4956 | Barriers() : semaphore_1(0), semaphore_2(0) {} |
| 4957 | ThreadBarrier<2> barrier_1; |
| 4958 | ThreadBarrier<2> barrier_2; |
| 4959 | ThreadBarrier<2> barrier_3; |
| 4960 | ThreadBarrier<2> barrier_4; |
| 4961 | ThreadBarrier<2> barrier_5; |
| 4962 | v8::base::Semaphore semaphore_1; |
| 4963 | v8::base::Semaphore semaphore_2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4964 | }; |
| 4965 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4966 | |
| 4967 | // We match parts of the message to decide if it is a break message. |
| 4968 | bool IsBreakEventMessage(char *message) { |
| 4969 | const char* type_event = "\"type\":\"event\""; |
| 4970 | const char* event_break = "\"event\":\"break\""; |
| 4971 | // Does the message contain both type:event and event:break? |
| 4972 | return strstr(message, type_event) != NULL && |
| 4973 | strstr(message, event_break) != NULL; |
| 4974 | } |
| 4975 | |
| 4976 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4977 | // We match parts of the message to decide if it is a exception message. |
| 4978 | bool IsExceptionEventMessage(char *message) { |
| 4979 | const char* type_event = "\"type\":\"event\""; |
| 4980 | const char* event_exception = "\"event\":\"exception\""; |
| 4981 | // Does the message contain both type:event and event:exception? |
| 4982 | return strstr(message, type_event) != NULL && |
| 4983 | strstr(message, event_exception) != NULL; |
| 4984 | } |
| 4985 | |
| 4986 | |
| 4987 | // We match the message wether it is an evaluate response message. |
| 4988 | bool IsEvaluateResponseMessage(char* message) { |
| 4989 | const char* type_response = "\"type\":\"response\""; |
| 4990 | const char* command_evaluate = "\"command\":\"evaluate\""; |
| 4991 | // Does the message contain both type:response and command:evaluate? |
| 4992 | return strstr(message, type_response) != NULL && |
| 4993 | strstr(message, command_evaluate) != NULL; |
| 4994 | } |
| 4995 | |
| 4996 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4997 | static int StringToInt(const char* s) { |
| 4998 | return atoi(s); // NOLINT |
| 4999 | } |
| 5000 | |
| 5001 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5002 | // We match parts of the message to get evaluate result int value. |
| 5003 | int GetEvaluateIntResult(char *message) { |
| 5004 | const char* value = "\"value\":"; |
| 5005 | char* pos = strstr(message, value); |
| 5006 | if (pos == NULL) { |
| 5007 | return -1; |
| 5008 | } |
| 5009 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5010 | res = StringToInt(pos + strlen(value)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5011 | return res; |
| 5012 | } |
| 5013 | |
| 5014 | |
| 5015 | // We match parts of the message to get hit breakpoint id. |
| 5016 | int GetBreakpointIdFromBreakEventMessage(char *message) { |
| 5017 | const char* breakpoints = "\"breakpoints\":["; |
| 5018 | char* pos = strstr(message, breakpoints); |
| 5019 | if (pos == NULL) { |
| 5020 | return -1; |
| 5021 | } |
| 5022 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5023 | res = StringToInt(pos + strlen(breakpoints)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5024 | return res; |
| 5025 | } |
| 5026 | |
| 5027 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5028 | // We match parts of the message to get total frames number. |
| 5029 | int GetTotalFramesInt(char *message) { |
| 5030 | const char* prefix = "\"totalFrames\":"; |
| 5031 | char* pos = strstr(message, prefix); |
| 5032 | if (pos == NULL) { |
| 5033 | return -1; |
| 5034 | } |
| 5035 | pos += strlen(prefix); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5036 | int res = StringToInt(pos); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5037 | return res; |
| 5038 | } |
| 5039 | |
| 5040 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5041 | // We match parts of the message to get source line. |
| 5042 | int GetSourceLineFromBreakEventMessage(char *message) { |
| 5043 | const char* source_line = "\"sourceLine\":"; |
| 5044 | char* pos = strstr(message, source_line); |
| 5045 | if (pos == NULL) { |
| 5046 | return -1; |
| 5047 | } |
| 5048 | int res = -1; |
| 5049 | res = StringToInt(pos + strlen(source_line)); |
| 5050 | return res; |
| 5051 | } |
| 5052 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5053 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5054 | /* Test MessageQueues */ |
| 5055 | /* Tests the message queues that hold debugger commands and |
| 5056 | * response messages to the debugger. Fills queues and makes |
| 5057 | * them grow. |
| 5058 | */ |
| 5059 | Barriers message_queue_barriers; |
| 5060 | |
| 5061 | // This is the debugger thread, that executes no v8 calls except |
| 5062 | // placing JSON debugger commands in the queue. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5063 | class MessageQueueDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5064 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5065 | MessageQueueDebuggerThread() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5066 | : Thread(Options("MessageQueueDebuggerThread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5067 | void Run(); |
| 5068 | }; |
| 5069 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5070 | |
| 5071 | static void MessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5072 | v8::Local<v8::String> json = message.GetJSON(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5073 | v8::String::Utf8Value utf8(json); |
| 5074 | if (IsBreakEventMessage(*utf8)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5075 | // Lets test script wait until break occurs to send commands. |
| 5076 | // Signals when a break is reported. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5077 | message_queue_barriers.semaphore_2.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | // Allow message handler to block on a semaphore, to test queueing of |
| 5081 | // messages while blocked. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5082 | message_queue_barriers.semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5085 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5086 | void MessageQueueDebuggerThread::Run() { |
| 5087 | const int kBufferSize = 1000; |
| 5088 | uint16_t buffer_1[kBufferSize]; |
| 5089 | uint16_t buffer_2[kBufferSize]; |
| 5090 | const char* command_1 = |
| 5091 | "{\"seq\":117," |
| 5092 | "\"type\":\"request\"," |
| 5093 | "\"command\":\"evaluate\"," |
| 5094 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5095 | const char* command_2 = |
| 5096 | "{\"seq\":118," |
| 5097 | "\"type\":\"request\"," |
| 5098 | "\"command\":\"evaluate\"," |
| 5099 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5100 | const char* command_3 = |
| 5101 | "{\"seq\":119," |
| 5102 | "\"type\":\"request\"," |
| 5103 | "\"command\":\"evaluate\"," |
| 5104 | "\"arguments\":{\"expression\":\"c.d * b\"}}"; |
| 5105 | const char* command_continue = |
| 5106 | "{\"seq\":106," |
| 5107 | "\"type\":\"request\"," |
| 5108 | "\"command\":\"continue\"}"; |
| 5109 | const char* command_single_step = |
| 5110 | "{\"seq\":107," |
| 5111 | "\"type\":\"request\"," |
| 5112 | "\"command\":\"continue\"," |
| 5113 | "\"arguments\":{\"stepaction\":\"next\"}}"; |
| 5114 | |
| 5115 | /* Interleaved sequence of actions by the two threads:*/ |
| 5116 | // Main thread compiles and runs source_1 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5117 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5118 | message_queue_barriers.barrier_1.Wait(); |
| 5119 | // Post 6 commands, filling the command queue and making it expand. |
| 5120 | // These calls return immediately, but the commands stay on the queue |
| 5121 | // until the execution of source_2. |
| 5122 | // Note: AsciiToUtf16 executes before SendCommand, so command is copied |
| 5123 | // to buffer before buffer is sent to SendCommand. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5124 | v8::Isolate* isolate = CcTest::isolate(); |
| 5125 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 5126 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 5127 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5128 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5129 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5130 | message_queue_barriers.barrier_2.Wait(); |
| 5131 | // Main thread compiles and runs source_2. |
| 5132 | // Queued commands are executed at the start of compilation of source_2( |
| 5133 | // beforeCompile event). |
| 5134 | // Free the message handler to process all the messages from the queue. 7 |
| 5135 | // messages are expected: 2 afterCompile events and 5 responses. |
| 5136 | // All the commands added so far will fail to execute as long as call stack |
| 5137 | // is empty on beforeCompile event. |
| 5138 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5139 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5140 | } |
| 5141 | message_queue_barriers.barrier_3.Wait(); |
| 5142 | // Main thread compiles and runs source_3. |
| 5143 | // Don't stop in the afterCompile handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5144 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5145 | // source_3 includes a debugger statement, which causes a break event. |
| 5146 | // Wait on break event from hitting "debugger" statement |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5147 | message_queue_barriers.semaphore_2.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5148 | // These should execute after the "debugger" statement in source_2 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5149 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 5150 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 5151 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5152 | v8::Debug::SendCommand( |
| 5153 | isolate, buffer_2, AsciiToUtf16(command_single_step, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5154 | // Run after 2 break events, 4 responses. |
| 5155 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5156 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5157 | } |
| 5158 | // Wait on break event after a single step executes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5159 | message_queue_barriers.semaphore_2.Wait(); |
| 5160 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_2, buffer_1)); |
| 5161 | v8::Debug::SendCommand( |
| 5162 | isolate, buffer_2, AsciiToUtf16(command_continue, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5163 | // Run after 2 responses. |
| 5164 | for (int i = 0; i < 2 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5165 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5166 | } |
| 5167 | // Main thread continues running source_3 to end, waits for this thread. |
| 5168 | } |
| 5169 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5170 | |
| 5171 | // This thread runs the v8 engine. |
| 5172 | TEST(MessageQueues) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5173 | MessageQueueDebuggerThread message_queue_debugger_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5174 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5175 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5176 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5177 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5178 | v8::Debug::SetMessageHandler(env->GetIsolate(), MessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5179 | message_queue_debugger_thread.Start(); |
| 5180 | |
| 5181 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5182 | const char* source_2 = "e = 17;"; |
| 5183 | const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; |
| 5184 | |
| 5185 | // See MessageQueueDebuggerThread::Run for interleaved sequence of |
| 5186 | // API calls and events in the two threads. |
| 5187 | CompileRun(source_1); |
| 5188 | message_queue_barriers.barrier_1.Wait(); |
| 5189 | message_queue_barriers.barrier_2.Wait(); |
| 5190 | CompileRun(source_2); |
| 5191 | message_queue_barriers.barrier_3.Wait(); |
| 5192 | CompileRun(source_3); |
| 5193 | message_queue_debugger_thread.Join(); |
| 5194 | fflush(stdout); |
| 5195 | } |
| 5196 | |
| 5197 | |
| 5198 | class TestClientData : public v8::Debug::ClientData { |
| 5199 | public: |
| 5200 | TestClientData() { |
| 5201 | constructor_call_counter++; |
| 5202 | } |
| 5203 | virtual ~TestClientData() { |
| 5204 | destructor_call_counter++; |
| 5205 | } |
| 5206 | |
| 5207 | static void ResetCounters() { |
| 5208 | constructor_call_counter = 0; |
| 5209 | destructor_call_counter = 0; |
| 5210 | } |
| 5211 | |
| 5212 | static int constructor_call_counter; |
| 5213 | static int destructor_call_counter; |
| 5214 | }; |
| 5215 | |
| 5216 | int TestClientData::constructor_call_counter = 0; |
| 5217 | int TestClientData::destructor_call_counter = 0; |
| 5218 | |
| 5219 | |
| 5220 | // Tests that MessageQueue doesn't destroy client data when expands and |
| 5221 | // does destroy when it dies. |
| 5222 | TEST(MessageQueueExpandAndDestroy) { |
| 5223 | TestClientData::ResetCounters(); |
| 5224 | { // Create a scope for the queue. |
| 5225 | CommandMessageQueue queue(1); |
| 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 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5231 | new TestClientData())); |
| 5232 | CHECK_EQ(0, TestClientData::destructor_call_counter); |
| 5233 | queue.Get().Dispose(); |
| 5234 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 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 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5244 | new TestClientData())); |
| 5245 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5246 | queue.Get().Dispose(); |
| 5247 | CHECK_EQ(2, TestClientData::destructor_call_counter); |
| 5248 | } |
| 5249 | // All the client data should be destroyed when the queue is destroyed. |
| 5250 | CHECK_EQ(TestClientData::destructor_call_counter, |
| 5251 | TestClientData::destructor_call_counter); |
| 5252 | } |
| 5253 | |
| 5254 | |
| 5255 | static int handled_client_data_instances_count = 0; |
| 5256 | static void MessageHandlerCountingClientData( |
| 5257 | const v8::Debug::Message& message) { |
| 5258 | if (message.GetClientData() != NULL) { |
| 5259 | handled_client_data_instances_count++; |
| 5260 | } |
| 5261 | } |
| 5262 | |
| 5263 | |
| 5264 | // Tests that all client data passed to the debugger are sent to the handler. |
| 5265 | TEST(SendClientDataToHandler) { |
| 5266 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5267 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5268 | v8::Isolate* isolate = env->GetIsolate(); |
| 5269 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5270 | TestClientData::ResetCounters(); |
| 5271 | handled_client_data_instances_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5272 | v8::Debug::SetMessageHandler(isolate, MessageHandlerCountingClientData); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5273 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5274 | const int kBufferSize = 1000; |
| 5275 | uint16_t buffer[kBufferSize]; |
| 5276 | const char* command_1 = |
| 5277 | "{\"seq\":117," |
| 5278 | "\"type\":\"request\"," |
| 5279 | "\"command\":\"evaluate\"," |
| 5280 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5281 | const char* command_2 = |
| 5282 | "{\"seq\":118," |
| 5283 | "\"type\":\"request\"," |
| 5284 | "\"command\":\"evaluate\"," |
| 5285 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5286 | const char* command_continue = |
| 5287 | "{\"seq\":106," |
| 5288 | "\"type\":\"request\"," |
| 5289 | "\"command\":\"continue\"}"; |
| 5290 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5291 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_1, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5292 | new TestClientData()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5293 | v8::Debug::SendCommand( |
| 5294 | isolate, buffer, AsciiToUtf16(command_2, buffer), NULL); |
| 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()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5297 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_2, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5298 | new TestClientData()); |
| 5299 | // All the messages will be processed on beforeCompile event. |
| 5300 | CompileRun(source_1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5301 | v8::Debug::SendCommand( |
| 5302 | isolate, buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5303 | CHECK_EQ(3, TestClientData::constructor_call_counter); |
| 5304 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5305 | handled_client_data_instances_count); |
| 5306 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5307 | TestClientData::destructor_call_counter); |
| 5308 | } |
| 5309 | |
| 5310 | |
| 5311 | /* Test ThreadedDebugging */ |
| 5312 | /* This test interrupts a running infinite loop that is |
| 5313 | * occupying the v8 thread by a break command from the |
| 5314 | * debugger thread. It then changes the value of a |
| 5315 | * global object, to make the loop terminate. |
| 5316 | */ |
| 5317 | |
| 5318 | Barriers threaded_debugging_barriers; |
| 5319 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5320 | class V8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5321 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5322 | V8Thread() : Thread(Options("V8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5323 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5324 | v8::Isolate* isolate() { return isolate_; } |
| 5325 | |
| 5326 | private: |
| 5327 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5328 | }; |
| 5329 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5330 | class DebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5331 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5332 | explicit DebuggerThread(v8::Isolate* isolate) |
| 5333 | : Thread(Options("DebuggerThread")), isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5334 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5335 | |
| 5336 | private: |
| 5337 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5338 | }; |
| 5339 | |
| 5340 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5341 | static void ThreadedAtBarrier1( |
| 5342 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5343 | threaded_debugging_barriers.barrier_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | |
| 5347 | static void ThreadedMessageHandler(const v8::Debug::Message& message) { |
| 5348 | static char print_buffer[1000]; |
| 5349 | v8::String::Value json(message.GetJSON()); |
| 5350 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5351 | if (IsBreakEventMessage(print_buffer)) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5352 | // Check that we are inside the while loop. |
| 5353 | int source_line = GetSourceLineFromBreakEventMessage(print_buffer); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5354 | CHECK(4 <= source_line && source_line <= 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5355 | threaded_debugging_barriers.barrier_2.Wait(); |
| 5356 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | |
| 5360 | void V8Thread::Run() { |
| 5361 | const char* source = |
| 5362 | "flag = true;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5363 | "\n" |
| 5364 | "function foo() {\n" |
| 5365 | " var x = 1;\n" |
| 5366 | " while ( flag == true ) {\n" |
| 5367 | " if ( x == 1 ) {\n" |
| 5368 | " ThreadedAtBarrier1();\n" |
| 5369 | " }\n" |
| 5370 | " x = x + 1;\n" |
| 5371 | " }\n" |
| 5372 | "}\n" |
| 5373 | "\n" |
| 5374 | "foo();\n"; |
| 5375 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5376 | v8::Isolate::CreateParams create_params; |
| 5377 | create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 5378 | isolate_ = v8::Isolate::New(create_params); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5379 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5380 | { |
| 5381 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5382 | DebugLocalContext env(isolate_); |
| 5383 | v8::HandleScope scope(isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5384 | v8::Debug::SetMessageHandler(isolate_, &ThreadedMessageHandler); |
| 5385 | v8::Local<v8::ObjectTemplate> global_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5386 | v8::ObjectTemplate::New(env->GetIsolate()); |
| 5387 | global_template->Set( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5388 | v8_str(env->GetIsolate(), "ThreadedAtBarrier1"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5389 | v8::FunctionTemplate::New(isolate_, ThreadedAtBarrier1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5390 | v8::Local<v8::Context> context = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5391 | v8::Context::New(isolate_, NULL, global_template); |
| 5392 | v8::Context::Scope context_scope(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5393 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5394 | CompileRun(source); |
| 5395 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5396 | threaded_debugging_barriers.barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5397 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5400 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5401 | void DebuggerThread::Run() { |
| 5402 | const int kBufSize = 1000; |
| 5403 | uint16_t buffer[kBufSize]; |
| 5404 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5405 | const char* command_1 = |
| 5406 | "{\"seq\":102," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5407 | "\"type\":\"request\"," |
| 5408 | "\"command\":\"evaluate\"," |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5409 | "\"arguments\":{\"expression\":\"flag = false\"}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5410 | const char* command_2 = "{\"seq\":103," |
| 5411 | "\"type\":\"request\"," |
| 5412 | "\"command\":\"continue\"}"; |
| 5413 | |
| 5414 | threaded_debugging_barriers.barrier_1.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5415 | v8::Debug::DebugBreak(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5416 | threaded_debugging_barriers.barrier_2.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5417 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
| 5418 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5419 | threaded_debugging_barriers.barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5422 | |
| 5423 | TEST(ThreadedDebugging) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5424 | V8Thread v8_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5425 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5426 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5427 | v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5428 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5429 | DebuggerThread debugger_thread(v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5430 | debugger_thread.Start(); |
| 5431 | |
| 5432 | v8_thread.Join(); |
| 5433 | debugger_thread.Join(); |
| 5434 | } |
| 5435 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5436 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5437 | /* Test RecursiveBreakpoints */ |
| 5438 | /* In this test, the debugger evaluates a function with a breakpoint, after |
| 5439 | * hitting a breakpoint in another function. We do this with both values |
| 5440 | * of the flag enabling recursive breakpoints, and verify that the second |
| 5441 | * breakpoint is hit when enabled, and missed when disabled. |
| 5442 | */ |
| 5443 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5444 | class BreakpointsV8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5445 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5446 | BreakpointsV8Thread() : Thread(Options("BreakpointsV8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5447 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5448 | |
| 5449 | v8::Isolate* isolate() { return isolate_; } |
| 5450 | |
| 5451 | private: |
| 5452 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5453 | }; |
| 5454 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5455 | class BreakpointsDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5456 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5457 | BreakpointsDebuggerThread(bool global_evaluate, v8::Isolate* isolate) |
| 5458 | : Thread(Options("BreakpointsDebuggerThread")), |
| 5459 | global_evaluate_(global_evaluate), |
| 5460 | isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5461 | void Run(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5462 | |
| 5463 | private: |
| 5464 | bool global_evaluate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5465 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5466 | }; |
| 5467 | |
| 5468 | |
| 5469 | Barriers* breakpoints_barriers; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5470 | int break_event_breakpoint_id; |
| 5471 | int evaluate_int_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5472 | |
| 5473 | static void BreakpointsMessageHandler(const v8::Debug::Message& message) { |
| 5474 | static char print_buffer[1000]; |
| 5475 | v8::String::Value json(message.GetJSON()); |
| 5476 | Utf16ToAscii(*json, json.length(), print_buffer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5477 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5478 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5479 | break_event_breakpoint_id = |
| 5480 | GetBreakpointIdFromBreakEventMessage(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5481 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5482 | } else if (IsEvaluateResponseMessage(print_buffer)) { |
| 5483 | evaluate_int_result = GetEvaluateIntResult(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5484 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | |
| 5488 | |
| 5489 | void BreakpointsV8Thread::Run() { |
| 5490 | const char* source_1 = "var y_global = 3;\n" |
| 5491 | "function cat( new_value ) {\n" |
| 5492 | " var x = new_value;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5493 | " y_global = y_global + 4;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5494 | " x = 3 * x + 1;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5495 | " y_global = y_global + 5;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5496 | " return x;\n" |
| 5497 | "}\n" |
| 5498 | "\n" |
| 5499 | "function dog() {\n" |
| 5500 | " var x = 1;\n" |
| 5501 | " x = y_global;" |
| 5502 | " var z = 3;" |
| 5503 | " x += 100;\n" |
| 5504 | " return x;\n" |
| 5505 | "}\n" |
| 5506 | "\n"; |
| 5507 | const char* source_2 = "cat(17);\n" |
| 5508 | "cat(19);\n"; |
| 5509 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5510 | v8::Isolate::CreateParams create_params; |
| 5511 | create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 5512 | isolate_ = v8::Isolate::New(create_params); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5513 | breakpoints_barriers->barrier_3.Wait(); |
| 5514 | { |
| 5515 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5516 | DebugLocalContext env(isolate_); |
| 5517 | v8::HandleScope scope(isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5518 | v8::Debug::SetMessageHandler(isolate_, &BreakpointsMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5519 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5520 | CompileRun(source_1); |
| 5521 | breakpoints_barriers->barrier_1.Wait(); |
| 5522 | breakpoints_barriers->barrier_2.Wait(); |
| 5523 | CompileRun(source_2); |
| 5524 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5525 | breakpoints_barriers->barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5526 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | |
| 5530 | void BreakpointsDebuggerThread::Run() { |
| 5531 | const int kBufSize = 1000; |
| 5532 | uint16_t buffer[kBufSize]; |
| 5533 | |
| 5534 | const char* command_1 = "{\"seq\":101," |
| 5535 | "\"type\":\"request\"," |
| 5536 | "\"command\":\"setbreakpoint\"," |
| 5537 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5538 | const char* command_2 = "{\"seq\":102," |
| 5539 | "\"type\":\"request\"," |
| 5540 | "\"command\":\"setbreakpoint\"," |
| 5541 | "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5542 | const char* command_3; |
| 5543 | if (this->global_evaluate_) { |
| 5544 | command_3 = "{\"seq\":103," |
| 5545 | "\"type\":\"request\"," |
| 5546 | "\"command\":\"evaluate\"," |
| 5547 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," |
| 5548 | "\"global\":true}}"; |
| 5549 | } else { |
| 5550 | command_3 = "{\"seq\":103," |
| 5551 | "\"type\":\"request\"," |
| 5552 | "\"command\":\"evaluate\"," |
| 5553 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; |
| 5554 | } |
| 5555 | const char* command_4; |
| 5556 | if (this->global_evaluate_) { |
| 5557 | command_4 = "{\"seq\":104," |
| 5558 | "\"type\":\"request\"," |
| 5559 | "\"command\":\"evaluate\"," |
| 5560 | "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," |
| 5561 | "\"global\":true}}"; |
| 5562 | } else { |
| 5563 | command_4 = "{\"seq\":104," |
| 5564 | "\"type\":\"request\"," |
| 5565 | "\"command\":\"evaluate\"," |
| 5566 | "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; |
| 5567 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5568 | const char* command_5 = "{\"seq\":105," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5569 | "\"type\":\"request\"," |
| 5570 | "\"command\":\"continue\"}"; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5571 | const char* command_6 = "{\"seq\":106," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5572 | "\"type\":\"request\"," |
| 5573 | "\"command\":\"continue\"}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5574 | const char* command_7; |
| 5575 | if (this->global_evaluate_) { |
| 5576 | command_7 = "{\"seq\":107," |
| 5577 | "\"type\":\"request\"," |
| 5578 | "\"command\":\"evaluate\"," |
| 5579 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," |
| 5580 | "\"global\":true}}"; |
| 5581 | } else { |
| 5582 | command_7 = "{\"seq\":107," |
| 5583 | "\"type\":\"request\"," |
| 5584 | "\"command\":\"evaluate\"," |
| 5585 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; |
| 5586 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5587 | const char* command_8 = "{\"seq\":108," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5588 | "\"type\":\"request\"," |
| 5589 | "\"command\":\"continue\"}"; |
| 5590 | |
| 5591 | |
| 5592 | // v8 thread initializes, runs source_1 |
| 5593 | breakpoints_barriers->barrier_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5594 | // 1:Set breakpoint in cat() (will get id 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5595 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5596 | // 2:Set breakpoint in dog() (will get id 2). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5597 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5598 | breakpoints_barriers->barrier_2.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5599 | // V8 thread starts compiling source_2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5600 | // Automatic break happens, to run queued commands |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5601 | // breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5602 | // Commands 1 through 3 run, thread continues. |
| 5603 | // v8 thread runs source_2 to breakpoint in cat(). |
| 5604 | // message callback receives break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5605 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5606 | // Must have hit breakpoint #1. |
| 5607 | CHECK_EQ(1, break_event_breakpoint_id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5608 | // 4:Evaluate dog() (which has a breakpoint). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5609 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_3, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5610 | // V8 thread hits breakpoint in dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5611 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5612 | // Must have hit breakpoint #2. |
| 5613 | CHECK_EQ(2, break_event_breakpoint_id); |
| 5614 | // 5:Evaluate (x + 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5615 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_4, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5616 | // Evaluate (x + 1) finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5617 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5618 | // Must have result 108. |
| 5619 | CHECK_EQ(108, evaluate_int_result); |
| 5620 | // 6:Continue evaluation of dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5621 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_5, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5622 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5623 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5624 | // Must have result 107. |
| 5625 | CHECK_EQ(107, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5626 | // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint |
| 5627 | // in cat(19). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5628 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_6, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5629 | // Message callback gets break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5630 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5631 | // Must have hit breakpoint #1. |
| 5632 | CHECK_EQ(1, break_event_breakpoint_id); |
| 5633 | // 8: Evaluate dog() with breaks disabled. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5634 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_7, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5635 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5636 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5637 | // Must have result 116. |
| 5638 | CHECK_EQ(116, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5639 | // 9: Continue evaluation of source2, reach end. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5640 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_8, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5641 | breakpoints_barriers->barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame] | 5644 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5645 | void TestRecursiveBreakpointsGeneric(bool global_evaluate) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5646 | BreakpointsV8Thread breakpoints_v8_thread; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5647 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5648 | // Create a V8 environment |
| 5649 | Barriers stack_allocated_breakpoints_barriers; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5650 | breakpoints_barriers = &stack_allocated_breakpoints_barriers; |
| 5651 | |
| 5652 | breakpoints_v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5653 | breakpoints_barriers->barrier_3.Wait(); |
| 5654 | BreakpointsDebuggerThread breakpoints_debugger_thread( |
| 5655 | global_evaluate, breakpoints_v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5656 | breakpoints_debugger_thread.Start(); |
| 5657 | |
| 5658 | breakpoints_v8_thread.Join(); |
| 5659 | breakpoints_debugger_thread.Join(); |
| 5660 | } |
| 5661 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5662 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5663 | TEST(RecursiveBreakpoints) { |
| 5664 | TestRecursiveBreakpointsGeneric(false); |
| 5665 | } |
| 5666 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5667 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5668 | TEST(RecursiveBreakpointsGlobal) { |
| 5669 | TestRecursiveBreakpointsGeneric(true); |
| 5670 | } |
| 5671 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5672 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5673 | TEST(SetDebugEventListenerOnUninitializedVM) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5674 | v8::Debug::SetDebugEventListener(CcTest::isolate(), DummyDebugEventListener); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | |
| 5678 | static void DummyMessageHandler(const v8::Debug::Message& message) { |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | TEST(SetMessageHandlerOnUninitializedVM) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5683 | v8::Debug::SetMessageHandler(CcTest::isolate(), DummyMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | |
| 5687 | // Source for a JavaScript function which returns the data parameter of a |
| 5688 | // function called in the context of the debugger. If no data parameter is |
| 5689 | // passed it throws an exception. |
| 5690 | static const char* debugger_call_with_data_source = |
| 5691 | "function debugger_call_with_data(exec_state, data) {" |
| 5692 | " if (data) return data;" |
| 5693 | " throw 'No data!'" |
| 5694 | "}"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5695 | v8::Local<v8::Function> debugger_call_with_data; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5696 | |
| 5697 | |
| 5698 | // Source for a JavaScript function which returns the data parameter of a |
| 5699 | // function called in the context of the debugger. If no data parameter is |
| 5700 | // passed it throws an exception. |
| 5701 | static const char* debugger_call_with_closure_source = |
| 5702 | "var x = 3;" |
| 5703 | "(function (exec_state) {" |
| 5704 | " if (exec_state.y) return x - 1;" |
| 5705 | " exec_state.y = x;" |
| 5706 | " return exec_state.y" |
| 5707 | "})"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5708 | v8::Local<v8::Function> debugger_call_with_closure; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5709 | |
| 5710 | // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 5711 | // in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5712 | static void CheckFrameCount(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5713 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5714 | CHECK(v8::Debug::Call(context, frame_count).ToLocalChecked()->IsNumber()); |
| 5715 | CHECK_EQ(args[0]->Int32Value(context).FromJust(), |
| 5716 | v8::Debug::Call(context, frame_count) |
| 5717 | .ToLocalChecked() |
| 5718 | ->Int32Value(context) |
| 5719 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | |
| 5723 | // Function to retrieve the source line of the top JavaScript frame by calling a |
| 5724 | // JavaScript function in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5725 | static void CheckSourceLine(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5726 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5727 | CHECK( |
| 5728 | v8::Debug::Call(context, frame_source_line).ToLocalChecked()->IsNumber()); |
| 5729 | CHECK_EQ(args[0]->Int32Value(context).FromJust(), |
| 5730 | v8::Debug::Call(context, frame_source_line) |
| 5731 | .ToLocalChecked() |
| 5732 | ->Int32Value(context) |
| 5733 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
| 5736 | |
| 5737 | // Function to test passing an additional parameter to a JavaScript function |
| 5738 | // called in the debugger. It also tests that functions called in the debugger |
| 5739 | // can throw exceptions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5740 | static void CheckDataParameter( |
| 5741 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5742 | v8::Local<v8::String> data = v8_str(args.GetIsolate(), "Test"); |
| 5743 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5744 | CHECK(v8::Debug::Call(context, debugger_call_with_data, data) |
| 5745 | .ToLocalChecked() |
| 5746 | ->IsString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5747 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5748 | for (int i = 0; i < 3; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5749 | v8::TryCatch catcher(args.GetIsolate()); |
| 5750 | CHECK(v8::Debug::Call(context, debugger_call_with_data).IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5751 | CHECK(catcher.HasCaught()); |
| 5752 | CHECK(catcher.Exception()->IsString()); |
| 5753 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | |
| 5757 | // Function to test using a JavaScript with closure in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5758 | static void CheckClosure(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5759 | v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
| 5760 | CHECK(v8::Debug::Call(context, debugger_call_with_closure) |
| 5761 | .ToLocalChecked() |
| 5762 | ->IsNumber()); |
| 5763 | CHECK_EQ(3, v8::Debug::Call(context, debugger_call_with_closure) |
| 5764 | .ToLocalChecked() |
| 5765 | ->Int32Value(context) |
| 5766 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | |
| 5770 | // Test functions called through the debugger. |
| 5771 | TEST(CallFunctionInDebugger) { |
| 5772 | // Create and enter a context with the functions CheckFrameCount, |
| 5773 | // CheckSourceLine and CheckDataParameter installed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5774 | v8::Isolate* isolate = CcTest::isolate(); |
| 5775 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5776 | v8::Local<v8::ObjectTemplate> global_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5777 | v8::ObjectTemplate::New(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5778 | global_template->Set(v8_str(isolate, "CheckFrameCount"), |
| 5779 | v8::FunctionTemplate::New(isolate, CheckFrameCount)); |
| 5780 | global_template->Set(v8_str(isolate, "CheckSourceLine"), |
| 5781 | v8::FunctionTemplate::New(isolate, CheckSourceLine)); |
| 5782 | global_template->Set(v8_str(isolate, "CheckDataParameter"), |
| 5783 | v8::FunctionTemplate::New(isolate, CheckDataParameter)); |
| 5784 | global_template->Set(v8_str(isolate, "CheckClosure"), |
| 5785 | v8::FunctionTemplate::New(isolate, CheckClosure)); |
| 5786 | v8::Local<v8::Context> context = |
| 5787 | v8::Context::New(isolate, NULL, global_template); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5788 | v8::Context::Scope context_scope(context); |
| 5789 | |
| 5790 | // Compile a function for checking the number of JavaScript frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5791 | v8::Script::Compile(context, v8_str(isolate, frame_count_source)) |
| 5792 | .ToLocalChecked() |
| 5793 | ->Run(context) |
| 5794 | .ToLocalChecked(); |
| 5795 | frame_count = v8::Local<v8::Function>::Cast( |
| 5796 | context->Global() |
| 5797 | ->Get(context, v8_str(isolate, "frame_count")) |
| 5798 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5799 | |
| 5800 | // Compile a function for returning the source line for the top frame. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5801 | v8::Script::Compile(context, v8_str(isolate, frame_source_line_source)) |
| 5802 | .ToLocalChecked() |
| 5803 | ->Run(context) |
| 5804 | .ToLocalChecked(); |
| 5805 | frame_source_line = v8::Local<v8::Function>::Cast( |
| 5806 | context->Global() |
| 5807 | ->Get(context, v8_str(isolate, "frame_source_line")) |
| 5808 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5809 | |
| 5810 | // Compile a function returning the data parameter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5811 | v8::Script::Compile(context, v8_str(isolate, debugger_call_with_data_source)) |
| 5812 | .ToLocalChecked() |
| 5813 | ->Run(context) |
| 5814 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5815 | debugger_call_with_data = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5816 | context->Global() |
| 5817 | ->Get(context, v8_str(isolate, "debugger_call_with_data")) |
| 5818 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5819 | |
| 5820 | // Compile a function capturing closure. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5821 | debugger_call_with_closure = v8::Local<v8::Function>::Cast( |
| 5822 | v8::Script::Compile(context, |
| 5823 | v8_str(isolate, debugger_call_with_closure_source)) |
| 5824 | .ToLocalChecked() |
| 5825 | ->Run(context) |
| 5826 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5827 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5828 | // Calling a function through the debugger returns 0 frames if there are |
| 5829 | // no JavaScript frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5830 | CHECK(v8::Integer::New(isolate, 0) |
| 5831 | ->Equals(context, |
| 5832 | v8::Debug::Call(context, frame_count).ToLocalChecked()) |
| 5833 | .FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5834 | |
| 5835 | // Test that the number of frames can be retrieved. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5836 | v8::Script::Compile(context, v8_str(isolate, "CheckFrameCount(1)")) |
| 5837 | .ToLocalChecked() |
| 5838 | ->Run(context) |
| 5839 | .ToLocalChecked(); |
| 5840 | v8::Script::Compile(context, v8_str(isolate, |
| 5841 | "function f() {" |
| 5842 | " CheckFrameCount(2);" |
| 5843 | "}; f()")) |
| 5844 | .ToLocalChecked() |
| 5845 | ->Run(context) |
| 5846 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5847 | |
| 5848 | // Test that the source line can be retrieved. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5849 | v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(0)")) |
| 5850 | .ToLocalChecked() |
| 5851 | ->Run(context) |
| 5852 | .ToLocalChecked(); |
| 5853 | v8::Script::Compile(context, v8_str(isolate, |
| 5854 | "function f() {\n" |
| 5855 | " CheckSourceLine(1)\n" |
| 5856 | " CheckSourceLine(2)\n" |
| 5857 | " CheckSourceLine(3)\n" |
| 5858 | "}; f()")) |
| 5859 | .ToLocalChecked() |
| 5860 | ->Run(context) |
| 5861 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5862 | |
| 5863 | // 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] | 5864 | v8::Script::Compile(context, v8_str(isolate, "CheckDataParameter()")) |
| 5865 | .ToLocalChecked() |
| 5866 | ->Run(context) |
| 5867 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5868 | |
| 5869 | // Test that a function with closure can be run in the debugger. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5870 | v8::Script::Compile(context, v8_str(isolate, "CheckClosure()")) |
| 5871 | .ToLocalChecked() |
| 5872 | ->Run(context) |
| 5873 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5874 | |
| 5875 | // 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] | 5876 | v8::ScriptOrigin origin(v8_str(isolate, "test"), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5877 | v8::Integer::New(isolate, 7)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5878 | v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(7)"), &origin) |
| 5879 | .ToLocalChecked() |
| 5880 | ->Run(context) |
| 5881 | .ToLocalChecked(); |
| 5882 | v8::Script::Compile(context, v8_str(isolate, |
| 5883 | "function f() {\n" |
| 5884 | " CheckSourceLine(8)\n" |
| 5885 | " CheckSourceLine(9)\n" |
| 5886 | " CheckSourceLine(10)\n" |
| 5887 | "}; f()"), |
| 5888 | &origin) |
| 5889 | .ToLocalChecked() |
| 5890 | ->Run(context) |
| 5891 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | |
| 5895 | // Debugger message handler which counts the number of breaks. |
| 5896 | static void SendContinueCommand(); |
| 5897 | static void MessageHandlerBreakPointHitCount( |
| 5898 | const v8::Debug::Message& message) { |
| 5899 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 5900 | // Count the number of breaks. |
| 5901 | break_point_hit_count++; |
| 5902 | |
| 5903 | SendContinueCommand(); |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | |
| 5908 | // Test that clearing the debug event listener actually clears all break points |
| 5909 | // and related information. |
| 5910 | TEST(DebuggerUnload) { |
| 5911 | DebugLocalContext env; |
| 5912 | |
| 5913 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5914 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5915 | |
| 5916 | // Set a debug event listener. |
| 5917 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5918 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 5919 | DebugEventBreakPointHitCount); |
| 5920 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5921 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5922 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5923 | // Create a couple of functions for the test. |
| 5924 | v8::Local<v8::Function> foo = |
| 5925 | CompileFunction(&env, "function foo(){x=1}", "foo"); |
| 5926 | v8::Local<v8::Function> bar = |
| 5927 | CompileFunction(&env, "function bar(){y=2}", "bar"); |
| 5928 | |
| 5929 | // Set some break points. |
| 5930 | SetBreakPoint(foo, 0); |
| 5931 | SetBreakPoint(foo, 4); |
| 5932 | SetBreakPoint(bar, 0); |
| 5933 | SetBreakPoint(bar, 4); |
| 5934 | |
| 5935 | // Make sure that the break points are there. |
| 5936 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5937 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5938 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5939 | bar->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5940 | CHECK_EQ(4, break_point_hit_count); |
| 5941 | } |
| 5942 | |
| 5943 | // Remove the debug event listener without clearing breakpoints. Do this |
| 5944 | // outside a handle scope. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5945 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 5946 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5947 | |
| 5948 | // Now set a debug message handler. |
| 5949 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5950 | v8::Debug::SetMessageHandler(env->GetIsolate(), |
| 5951 | MessageHandlerBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5952 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5953 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5954 | |
| 5955 | // Get the test functions again. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5956 | v8::Local<v8::Function> foo(v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5957 | env->Global() |
| 5958 | ->Get(context, v8_str(env->GetIsolate(), "foo")) |
| 5959 | .ToLocalChecked())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5960 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5961 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5962 | CHECK_EQ(0, break_point_hit_count); |
| 5963 | |
| 5964 | // Set break points and run again. |
| 5965 | SetBreakPoint(foo, 0); |
| 5966 | SetBreakPoint(foo, 4); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5967 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5968 | CHECK_EQ(2, break_point_hit_count); |
| 5969 | } |
| 5970 | |
| 5971 | // Remove the debug message handler without clearing breakpoints. Do this |
| 5972 | // outside a handle scope. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5973 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 5974 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
| 5977 | |
| 5978 | // Sends continue command to the debugger. |
| 5979 | static void SendContinueCommand() { |
| 5980 | const int kBufferSize = 1000; |
| 5981 | uint16_t buffer[kBufferSize]; |
| 5982 | const char* command_continue = |
| 5983 | "{\"seq\":0," |
| 5984 | "\"type\":\"request\"," |
| 5985 | "\"command\":\"continue\"}"; |
| 5986 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5987 | v8::Debug::SendCommand( |
| 5988 | CcTest::isolate(), buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | |
| 5992 | // Debugger message handler which counts the number of times it is called. |
| 5993 | static int message_handler_hit_count = 0; |
| 5994 | static void MessageHandlerHitCount(const v8::Debug::Message& message) { |
| 5995 | message_handler_hit_count++; |
| 5996 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5997 | static char print_buffer[1000]; |
| 5998 | v8::String::Value json(message.GetJSON()); |
| 5999 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6000 | if (IsExceptionEventMessage(print_buffer)) { |
| 6001 | // Send a continue command for exception events. |
| 6002 | SendContinueCommand(); |
| 6003 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | |
| 6007 | // Test clearing the debug message handler. |
| 6008 | TEST(DebuggerClearMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6009 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6010 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6011 | |
| 6012 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6013 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6014 | |
| 6015 | // Set a debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6016 | v8::Debug::SetMessageHandler(env->GetIsolate(), MessageHandlerHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6017 | |
| 6018 | // Run code to throw a unhandled exception. This should end up in the message |
| 6019 | // handler. |
| 6020 | CompileRun("throw 1"); |
| 6021 | |
| 6022 | // The message handler should be called. |
| 6023 | CHECK_GT(message_handler_hit_count, 0); |
| 6024 | |
| 6025 | // Clear debug message handler. |
| 6026 | message_handler_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6027 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6028 | |
| 6029 | // Run code to throw a unhandled exception. This should end up in the message |
| 6030 | // handler. |
| 6031 | CompileRun("throw 1"); |
| 6032 | |
| 6033 | // The message handler should not be called more. |
| 6034 | CHECK_EQ(0, message_handler_hit_count); |
| 6035 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6036 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | |
| 6040 | // Debugger message handler which clears the message handler while active. |
| 6041 | static void MessageHandlerClearingMessageHandler( |
| 6042 | const v8::Debug::Message& message) { |
| 6043 | message_handler_hit_count++; |
| 6044 | |
| 6045 | // Clear debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6046 | v8::Debug::SetMessageHandler(message.GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | |
| 6050 | // Test clearing the debug message handler while processing a debug event. |
| 6051 | TEST(DebuggerClearMessageHandlerWhileActive) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6052 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6053 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6054 | |
| 6055 | // Check debugger is unloaded before it is used. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6056 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6057 | |
| 6058 | // Set a debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6059 | v8::Debug::SetMessageHandler(env->GetIsolate(), |
| 6060 | MessageHandlerClearingMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6061 | |
| 6062 | // Run code to throw a unhandled exception. This should end up in the message |
| 6063 | // handler. |
| 6064 | CompileRun("throw 1"); |
| 6065 | |
| 6066 | // The message handler should be called. |
| 6067 | CHECK_EQ(1, message_handler_hit_count); |
| 6068 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6069 | CheckDebuggerUnloaded(env->GetIsolate(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6073 | // Test for issue http://code.google.com/p/v8/issues/detail?id=289. |
| 6074 | // Make sure that DebugGetLoadedScripts doesn't return scripts |
| 6075 | // with disposed external source. |
| 6076 | class EmptyExternalStringResource : public v8::String::ExternalStringResource { |
| 6077 | public: |
| 6078 | EmptyExternalStringResource() { empty_[0] = 0; } |
| 6079 | virtual ~EmptyExternalStringResource() {} |
| 6080 | virtual size_t length() const { return empty_.length(); } |
| 6081 | virtual const uint16_t* data() const { return empty_.start(); } |
| 6082 | private: |
| 6083 | ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; |
| 6084 | }; |
| 6085 | |
| 6086 | |
| 6087 | TEST(DebugGetLoadedScripts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6088 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6089 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6090 | env.ExposeDebug(); |
| 6091 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6092 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6093 | EmptyExternalStringResource source_ext_str; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6094 | v8::Local<v8::String> source = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6095 | v8::String::NewExternalTwoByte(env->GetIsolate(), &source_ext_str) |
| 6096 | .ToLocalChecked(); |
| 6097 | CHECK(v8::Script::Compile(context, source).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6098 | Handle<i::ExternalTwoByteString> i_source( |
| 6099 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 6100 | // This situation can happen if source was an external string disposed |
| 6101 | // by its owner. |
| 6102 | i_source->set_resource(0); |
| 6103 | |
| 6104 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 6105 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6106 | EnableDebugger(env->GetIsolate()); |
| 6107 | v8::MaybeLocal<v8::Value> result = |
| 6108 | CompileRun(env.context(), |
| 6109 | "var scripts = %DebugGetLoadedScripts();" |
| 6110 | "var count = scripts.length;" |
| 6111 | "for (var i = 0; i < count; ++i) {" |
| 6112 | " var lines = scripts[i].lineCount();" |
| 6113 | " if (lines < 1) throw 'lineCount';" |
| 6114 | " var last = -1;" |
| 6115 | " for (var j = 0; j < lines; ++j) {" |
| 6116 | " var end = scripts[i].lineEnd(j);" |
| 6117 | " if (last >= end) throw 'lineEnd';" |
| 6118 | " last = end;" |
| 6119 | " }" |
| 6120 | "}"); |
| 6121 | CHECK(!result.IsEmpty()); |
| 6122 | DisableDebugger(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6123 | // Must not crash while accessing line_ends. |
| 6124 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
| 6125 | |
| 6126 | // Some scripts are retrieved - at least the number of native scripts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6127 | CHECK_GT(env->Global() |
| 6128 | ->Get(context, v8_str(env->GetIsolate(), "count")) |
| 6129 | .ToLocalChecked() |
| 6130 | ->Int32Value(context) |
| 6131 | .FromJust(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6132 | 8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | |
| 6136 | // Test script break points set on lines. |
| 6137 | TEST(ScriptNameAndData) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6138 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6139 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6140 | env.ExposeDebug(); |
| 6141 | |
| 6142 | // Create functions for retrieving script name and data for the function on |
| 6143 | // the top frame when hitting a break point. |
| 6144 | frame_script_name = CompileFunction(&env, |
| 6145 | frame_script_name_source, |
| 6146 | "frame_script_name"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6147 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6148 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 6149 | DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6150 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6151 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6152 | // Test function source. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6153 | v8::Local<v8::String> script = v8_str(env->GetIsolate(), |
| 6154 | "function f() {\n" |
| 6155 | " debugger;\n" |
| 6156 | "}\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6157 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6158 | v8::ScriptOrigin origin1 = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6159 | v8::ScriptOrigin(v8_str(env->GetIsolate(), "name")); |
| 6160 | v8::Local<v8::Script> script1 = |
| 6161 | v8::Script::Compile(context, script, &origin1).ToLocalChecked(); |
| 6162 | script1->Run(context).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6163 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6164 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6165 | env->Global() |
| 6166 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6167 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6168 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6169 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6170 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6171 | CHECK_EQ(0, strcmp("name", last_script_name_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6172 | |
| 6173 | // Compile the same script again without setting data. As the compilation |
| 6174 | // cache is disabled when debugging expect the data to be missing. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6175 | v8::Script::Compile(context, script, &origin1) |
| 6176 | .ToLocalChecked() |
| 6177 | ->Run(context) |
| 6178 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6179 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6180 | env->Global() |
| 6181 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6182 | .ToLocalChecked()); |
| 6183 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6184 | CHECK_EQ(2, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6185 | CHECK_EQ(0, strcmp("name", last_script_name_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6186 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6187 | v8::Local<v8::String> data_obj_source = |
| 6188 | v8_str(env->GetIsolate(), |
| 6189 | "({ a: 'abc',\n" |
| 6190 | " b: 123,\n" |
| 6191 | " toString: function() { return this.a + ' ' + this.b; }\n" |
| 6192 | "})\n"); |
| 6193 | v8::Script::Compile(context, data_obj_source) |
| 6194 | .ToLocalChecked() |
| 6195 | ->Run(context) |
| 6196 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6197 | v8::ScriptOrigin origin2 = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6198 | v8::ScriptOrigin(v8_str(env->GetIsolate(), "new name")); |
| 6199 | v8::Local<v8::Script> script2 = |
| 6200 | v8::Script::Compile(context, script, &origin2).ToLocalChecked(); |
| 6201 | script2->Run(context).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6202 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6203 | env->Global() |
| 6204 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6205 | .ToLocalChecked()); |
| 6206 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6207 | CHECK_EQ(3, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6208 | CHECK_EQ(0, strcmp("new name", last_script_name_hit)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6209 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6210 | v8::Local<v8::Script> script3 = |
| 6211 | v8::Script::Compile(context, script, &origin2).ToLocalChecked(); |
| 6212 | script3->Run(context).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6213 | f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6214 | env->Global() |
| 6215 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6216 | .ToLocalChecked()); |
| 6217 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6218 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6222 | static v8::Local<v8::Context> expected_context; |
| 6223 | static v8::Local<v8::Value> expected_context_data; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6224 | |
| 6225 | |
| 6226 | // Check that the expected context is the one generating the debug event. |
| 6227 | static void ContextCheckMessageHandler(const v8::Debug::Message& message) { |
| 6228 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6229 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6230 | expected_context_data)); |
| 6231 | message_handler_hit_count++; |
| 6232 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6233 | static char print_buffer[1000]; |
| 6234 | v8::String::Value json(message.GetJSON()); |
| 6235 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6237 | // Send a continue command for break events. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6238 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6239 | SendContinueCommand(); |
| 6240 | } |
| 6241 | } |
| 6242 | |
| 6243 | |
| 6244 | // Test which creates two contexts and sets different embedder data on each. |
| 6245 | // Checks that this data is set correctly and that when the debug message |
| 6246 | // handler is called the expected context is the one active. |
| 6247 | TEST(ContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6248 | v8::Isolate* isolate = CcTest::isolate(); |
| 6249 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6250 | |
| 6251 | // Create two contexts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6252 | v8::Local<v8::Context> context_1; |
| 6253 | v8::Local<v8::Context> context_2; |
| 6254 | v8::Local<v8::ObjectTemplate> global_template = |
| 6255 | v8::Local<v8::ObjectTemplate>(); |
| 6256 | v8::Local<v8::Value> global_object = v8::Local<v8::Value>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6257 | context_1 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6258 | context_2 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6259 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6260 | v8::Debug::SetMessageHandler(isolate, ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6261 | |
| 6262 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6263 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
| 6264 | CHECK(context_2->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6265 | |
| 6266 | // Set and check different data values. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6267 | v8::Local<v8::String> data_1 = v8_str(isolate, "1"); |
| 6268 | v8::Local<v8::String> data_2 = v8_str(isolate, "2"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6269 | context_1->SetEmbedderData(0, data_1); |
| 6270 | context_2->SetEmbedderData(0, data_2); |
| 6271 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
| 6272 | CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6273 | |
| 6274 | // Simple test function which causes a break. |
| 6275 | const char* source = "function f() { debugger; }"; |
| 6276 | |
| 6277 | // Enter and run function in the first context. |
| 6278 | { |
| 6279 | v8::Context::Scope context_scope(context_1); |
| 6280 | expected_context = context_1; |
| 6281 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6282 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6283 | f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6284 | } |
| 6285 | |
| 6286 | |
| 6287 | // Enter and run function in the second context. |
| 6288 | { |
| 6289 | v8::Context::Scope context_scope(context_2); |
| 6290 | expected_context = context_2; |
| 6291 | expected_context_data = data_2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6292 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6293 | f->Call(context_2, context_2->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | // Two times compile event and two times break event. |
| 6297 | CHECK_GT(message_handler_hit_count, 4); |
| 6298 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6299 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6300 | CheckDebuggerUnloaded(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | |
| 6304 | // Debug message handler which issues a debug break when it hits a break event. |
| 6305 | static int message_handler_break_hit_count = 0; |
| 6306 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
| 6307 | // Schedule a debug break for break events. |
| 6308 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6309 | message_handler_break_hit_count++; |
| 6310 | if (message_handler_break_hit_count == 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6311 | v8::Debug::DebugBreak(message.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | // Issue a continue command if this event will not cause the VM to start |
| 6316 | // running. |
| 6317 | if (!message.WillStartRunning()) { |
| 6318 | SendContinueCommand(); |
| 6319 | } |
| 6320 | } |
| 6321 | |
| 6322 | |
| 6323 | // Test that a debug break can be scheduled while in a message handler. |
| 6324 | TEST(DebugBreakInMessageHandler) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6325 | 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] | 6326 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6327 | v8::HandleScope scope(env->GetIsolate()); |
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::Debug::SetMessageHandler(env->GetIsolate(), DebugBreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6330 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6331 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6332 | // Test functions. |
| 6333 | const char* script = "function f() { debugger; g(); } function g() { }"; |
| 6334 | CompileRun(script); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6335 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6336 | env->Global() |
| 6337 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6338 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6339 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6340 | env->Global() |
| 6341 | ->Get(context, v8_str(env->GetIsolate(), "g")) |
| 6342 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6343 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6344 | // 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] | 6345 | // cause another break. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6346 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6347 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6348 | // Calling g will not cause any additional breaks. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6349 | g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6350 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6351 | } |
| 6352 | |
| 6353 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6354 | #ifndef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6355 | // Debug event handler which gets the function on the top frame and schedules a |
| 6356 | // break a number of times. |
| 6357 | static void DebugEventDebugBreak( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6358 | const v8::Debug::EventDetails& event_details) { |
| 6359 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6360 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 6361 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6362 | if (event == v8::Break) { |
| 6363 | break_point_hit_count++; |
| 6364 | |
| 6365 | // Get the name of the top frame function. |
| 6366 | if (!frame_function_name.IsEmpty()) { |
| 6367 | // Get the name of the function. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6368 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6369 | v8::Local<v8::Value> argv[argc] = { |
| 6370 | exec_state, v8::Integer::New(CcTest::isolate(), 0)}; |
| 6371 | v8::Local<v8::Value> result = |
| 6372 | frame_function_name->Call(context, exec_state, argc, argv) |
| 6373 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6374 | if (result->IsUndefined()) { |
| 6375 | last_function_hit[0] = '\0'; |
| 6376 | } else { |
| 6377 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6378 | v8::Local<v8::String> function_name( |
| 6379 | result->ToString(context).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6380 | function_name->WriteUtf8(last_function_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6381 | } |
| 6382 | } |
| 6383 | |
| 6384 | // Keep forcing breaks. |
| 6385 | if (break_point_hit_count < 20) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6386 | v8::Debug::DebugBreak(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6387 | } |
| 6388 | } |
| 6389 | } |
| 6390 | |
| 6391 | |
| 6392 | TEST(RegExpDebugBreak) { |
| 6393 | // This test only applies to native regexps. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6394 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6395 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6396 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6397 | // Create a function for checking the function when hitting a break point. |
| 6398 | frame_function_name = CompileFunction(&env, |
| 6399 | frame_function_name_source, |
| 6400 | "frame_function_name"); |
| 6401 | |
| 6402 | // Test RegExp which matches white spaces and comments at the begining of a |
| 6403 | // source line. |
| 6404 | const char* script = |
| 6405 | "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" |
| 6406 | "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; |
| 6407 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6408 | v8::Local<v8::Function> f = CompileFunction(env->GetIsolate(), script, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6409 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6410 | v8::Local<v8::Value> argv[argc] = { |
| 6411 | v8_str(env->GetIsolate(), " /* xxx */ a=0;")}; |
| 6412 | v8::Local<v8::Value> result = |
| 6413 | f->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
| 6414 | CHECK_EQ(12, result->Int32Value(context).FromJust()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6415 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6416 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventDebugBreak); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6417 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6418 | result = f->Call(context, env->Global(), argc, argv).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6419 | |
| 6420 | // Check that there was only one break event. Matching RegExp should not |
| 6421 | // cause Break events. |
| 6422 | CHECK_EQ(1, break_point_hit_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6423 | CHECK_EQ(0, strcmp("f", last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6424 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6425 | #endif // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6426 | |
| 6427 | |
| 6428 | // Common part of EvalContextData and NestedBreakEventContextData tests. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6429 | static void ExecuteScriptForContextCheck( |
| 6430 | v8::Debug::MessageHandler message_handler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6431 | // Create a context. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6432 | v8::Local<v8::Context> context_1; |
| 6433 | v8::Local<v8::ObjectTemplate> global_template = |
| 6434 | v8::Local<v8::ObjectTemplate>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6435 | context_1 = |
| 6436 | v8::Context::New(CcTest::isolate(), NULL, global_template); |
| 6437 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6438 | v8::Debug::SetMessageHandler(CcTest::isolate(), message_handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6439 | |
| 6440 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6441 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6442 | |
| 6443 | // Set and check a data value. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6444 | v8::Local<v8::String> data_1 = v8_str(CcTest::isolate(), "1"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6445 | context_1->SetEmbedderData(0, data_1); |
| 6446 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6447 | |
| 6448 | // Simple test function with eval that causes a break. |
| 6449 | const char* source = "function f() { eval('debugger;'); }"; |
| 6450 | |
| 6451 | // Enter and run function in the context. |
| 6452 | { |
| 6453 | v8::Context::Scope context_scope(context_1); |
| 6454 | expected_context = context_1; |
| 6455 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6456 | v8::Local<v8::Function> f = CompileFunction(CcTest::isolate(), source, "f"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6457 | f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6458 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6459 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6460 | v8::Debug::SetMessageHandler(CcTest::isolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | |
| 6464 | // Test which creates a context and sets embedder data on it. Checks that this |
| 6465 | // data is set correctly and that when the debug message handler is called for |
| 6466 | // break event in an eval statement the expected context is the one returned by |
| 6467 | // Message.GetEventContext. |
| 6468 | TEST(EvalContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6469 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6470 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6471 | ExecuteScriptForContextCheck(ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6472 | |
| 6473 | // One time compile event and one time break event. |
| 6474 | CHECK_GT(message_handler_hit_count, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6475 | CheckDebuggerUnloaded(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | |
| 6479 | static bool sent_eval = false; |
| 6480 | static int break_count = 0; |
| 6481 | static int continue_command_send_count = 0; |
| 6482 | // Check that the expected context is the one generating the debug event |
| 6483 | // including the case of nested break event. |
| 6484 | static void DebugEvalContextCheckMessageHandler( |
| 6485 | const v8::Debug::Message& message) { |
| 6486 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6487 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6488 | expected_context_data)); |
| 6489 | message_handler_hit_count++; |
| 6490 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6491 | static char print_buffer[1000]; |
| 6492 | v8::String::Value json(message.GetJSON()); |
| 6493 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6494 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6495 | v8::Isolate* isolate = message.GetIsolate(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6496 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6497 | break_count++; |
| 6498 | if (!sent_eval) { |
| 6499 | sent_eval = true; |
| 6500 | |
| 6501 | const int kBufferSize = 1000; |
| 6502 | uint16_t buffer[kBufferSize]; |
| 6503 | const char* eval_command = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6504 | "{\"seq\":0," |
| 6505 | "\"type\":\"request\"," |
| 6506 | "\"command\":\"evaluate\"," |
| 6507 | "\"arguments\":{\"expression\":\"debugger;\"," |
| 6508 | "\"global\":true,\"disable_break\":false}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6509 | |
| 6510 | // Send evaluate command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6511 | v8::Debug::SendCommand( |
| 6512 | isolate, buffer, AsciiToUtf16(eval_command, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6513 | return; |
| 6514 | } else { |
| 6515 | // It's a break event caused by the evaluation request above. |
| 6516 | SendContinueCommand(); |
| 6517 | continue_command_send_count++; |
| 6518 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6519 | } else if (IsEvaluateResponseMessage(print_buffer) && |
| 6520 | continue_command_send_count < 2) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6521 | // Response to the evaluation request. We're still on the breakpoint so |
| 6522 | // send continue. |
| 6523 | SendContinueCommand(); |
| 6524 | continue_command_send_count++; |
| 6525 | } |
| 6526 | } |
| 6527 | |
| 6528 | |
| 6529 | // Tests that context returned for break event is correct when the event occurs |
| 6530 | // in 'evaluate' debugger request. |
| 6531 | TEST(NestedBreakEventContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6532 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6533 | break_count = 0; |
| 6534 | message_handler_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6535 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6536 | ExecuteScriptForContextCheck(DebugEvalContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6537 | |
| 6538 | // One time compile event and two times break event. |
| 6539 | CHECK_GT(message_handler_hit_count, 3); |
| 6540 | |
| 6541 | // One break from the source and another from the evaluate request. |
| 6542 | CHECK_EQ(break_count, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6543 | CheckDebuggerUnloaded(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6547 | // Debug event listener which counts the after compile events. |
| 6548 | int after_compile_message_count = 0; |
| 6549 | static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 6550 | // Count the number of scripts collected. |
| 6551 | if (message.IsEvent()) { |
| 6552 | if (message.GetEvent() == v8::AfterCompile) { |
| 6553 | after_compile_message_count++; |
| 6554 | } else if (message.GetEvent() == v8::Break) { |
| 6555 | SendContinueCommand(); |
| 6556 | } |
| 6557 | } |
| 6558 | } |
| 6559 | |
| 6560 | |
| 6561 | // Tests that after compile event is sent as many times as there are scripts |
| 6562 | // compiled. |
| 6563 | TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6564 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6565 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6566 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6567 | after_compile_message_count = 0; |
| 6568 | const char* script = "var a=1"; |
| 6569 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6570 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6571 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6572 | .ToLocalChecked() |
| 6573 | ->Run(context) |
| 6574 | .ToLocalChecked(); |
| 6575 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6576 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6577 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6578 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6579 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6580 | .ToLocalChecked() |
| 6581 | ->Run(context) |
| 6582 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6583 | |
| 6584 | // Setting listener to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6585 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6586 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Compilation cache should be disabled when debugger is active. |
| 6589 | CHECK_EQ(2, after_compile_message_count); |
| 6590 | } |
| 6591 | |
| 6592 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6593 | // Syntax error event handler which counts a number of events. |
| 6594 | int compile_error_event_count = 0; |
| 6595 | |
| 6596 | static void CompileErrorEventCounterClear() { |
| 6597 | compile_error_event_count = 0; |
| 6598 | } |
| 6599 | |
| 6600 | static void CompileErrorEventCounter( |
| 6601 | const v8::Debug::EventDetails& event_details) { |
| 6602 | v8::DebugEvent event = event_details.GetEvent(); |
| 6603 | |
| 6604 | if (event == v8::CompileError) { |
| 6605 | compile_error_event_count++; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | |
| 6610 | // Tests that syntax error event is sent as many times as there are scripts |
| 6611 | // with syntax error compiled. |
| 6612 | TEST(SyntaxErrorMessageOnSyntaxException) { |
| 6613 | DebugLocalContext env; |
| 6614 | v8::HandleScope scope(env->GetIsolate()); |
| 6615 | |
| 6616 | // For this test, we want to break on uncaught exceptions: |
| 6617 | ChangeBreakOnException(false, true); |
| 6618 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6619 | v8::Debug::SetDebugEventListener(env->GetIsolate(), CompileErrorEventCounter); |
| 6620 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6621 | |
| 6622 | CompileErrorEventCounterClear(); |
| 6623 | |
| 6624 | // Check initial state. |
| 6625 | CHECK_EQ(0, compile_error_event_count); |
| 6626 | |
| 6627 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6628 | CHECK( |
| 6629 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6630 | CHECK_EQ(1, compile_error_event_count); |
| 6631 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6632 | CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "/sel\\/: \\")) |
| 6633 | .IsEmpty()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6634 | CHECK_EQ(2, compile_error_event_count); |
| 6635 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6636 | v8::Local<v8::Script> script = |
| 6637 | v8::Script::Compile(context, |
| 6638 | v8_str(env->GetIsolate(), "JSON.parse('1234:')")) |
| 6639 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6640 | CHECK_EQ(2, compile_error_event_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6641 | CHECK(script->Run(context).IsEmpty()); |
| 6642 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6643 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6644 | v8::Script::Compile(context, |
| 6645 | v8_str(env->GetIsolate(), "new RegExp('/\\/\\\\');")) |
| 6646 | .ToLocalChecked(); |
| 6647 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6648 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6649 | v8::Script::Compile(context, v8_str(env->GetIsolate(), "throw 1;")) |
| 6650 | .ToLocalChecked(); |
| 6651 | CHECK_EQ(3, compile_error_event_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6655 | // Tests that break event is sent when message handler is reset. |
| 6656 | TEST(BreakMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6657 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6658 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6659 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6660 | after_compile_message_count = 0; |
| 6661 | const char* script = "function f() {};"; |
| 6662 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6663 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6664 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6665 | .ToLocalChecked() |
| 6666 | ->Run(context) |
| 6667 | .ToLocalChecked(); |
| 6668 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6669 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6670 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6671 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 6672 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6673 | env->Global() |
| 6674 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6675 | .ToLocalChecked()); |
| 6676 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6677 | |
| 6678 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6679 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6680 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6681 | |
| 6682 | // Compilation cache should be disabled when debugger is active. |
| 6683 | CHECK_EQ(1, after_compile_message_count); |
| 6684 | } |
| 6685 | |
| 6686 | |
| 6687 | static int exception_event_count = 0; |
| 6688 | static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 6689 | if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 6690 | exception_event_count++; |
| 6691 | SendContinueCommand(); |
| 6692 | } |
| 6693 | } |
| 6694 | |
| 6695 | |
| 6696 | // Tests that exception event is sent when message handler is reset. |
| 6697 | TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6698 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6699 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 6700 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6701 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 6702 | // For this test, we want to break on uncaught exceptions: |
| 6703 | ChangeBreakOnException(false, true); |
| 6704 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6705 | exception_event_count = 0; |
| 6706 | const char* script = "function f() {throw new Error()};"; |
| 6707 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6708 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6709 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) |
| 6710 | .ToLocalChecked() |
| 6711 | ->Run(context) |
| 6712 | .ToLocalChecked(); |
| 6713 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6714 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6715 | v8::Debug::SetMessageHandler(env->GetIsolate(), ExceptionMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6716 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6717 | env->Global() |
| 6718 | ->Get(context, v8_str(env->GetIsolate(), "f")) |
| 6719 | .ToLocalChecked()); |
| 6720 | CHECK(f->Call(context, env->Global(), 0, NULL).IsEmpty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6721 | |
| 6722 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6723 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6724 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6725 | |
| 6726 | CHECK_EQ(1, exception_event_count); |
| 6727 | } |
| 6728 | |
| 6729 | |
| 6730 | // Tests after compile event is sent when there are some provisional |
| 6731 | // breakpoints out of the scripts lines range. |
| 6732 | TEST(ProvisionalBreakpointOnLineOutOfRange) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6733 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6734 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6735 | env.ExposeDebug(); |
| 6736 | const char* script = "function f() {};"; |
| 6737 | const char* resource_name = "test_resource"; |
| 6738 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6739 | v8::Debug::SetMessageHandler(env->GetIsolate(), AfterCompileMessageHandler); |
| 6740 | v8::Local<v8::Context> context = env.context(); |
| 6741 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6742 | // Set a couple of provisional breakpoint on lines out of the script lines |
| 6743 | // range. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6744 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, |
| 6745 | 3, -1 /* no column */); |
| 6746 | int sbp2 = |
| 6747 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, 5, 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6748 | |
| 6749 | after_compile_message_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6750 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6751 | v8::ScriptOrigin origin(v8_str(env->GetIsolate(), resource_name), |
| 6752 | v8::Integer::New(env->GetIsolate(), 10), |
| 6753 | v8::Integer::New(env->GetIsolate(), 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6754 | // Compile a script whose first line number is greater than the breakpoints' |
| 6755 | // lines. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6756 | v8::Script::Compile(context, v8_str(env->GetIsolate(), script), &origin) |
| 6757 | .ToLocalChecked() |
| 6758 | ->Run(context) |
| 6759 | .ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6760 | |
| 6761 | // If the script is compiled successfully there is exactly one after compile |
| 6762 | // event. In case of an exception in debugger code after compile event is not |
| 6763 | // sent. |
| 6764 | CHECK_EQ(1, after_compile_message_count); |
| 6765 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6766 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 6767 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6768 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | |
| 6772 | static void BreakMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6773 | i::Isolate* isolate = CcTest::i_isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6774 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6775 | // Count the number of breaks. |
| 6776 | break_point_hit_count++; |
| 6777 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6778 | i::HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6779 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6780 | |
| 6781 | SendContinueCommand(); |
| 6782 | } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6783 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6784 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6785 | int current_count = break_point_hit_count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6786 | |
| 6787 | // Force serialization to trigger some internal JS execution. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6788 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6789 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6790 | CHECK_EQ(current_count, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6791 | } |
| 6792 | } |
| 6793 | |
| 6794 | |
| 6795 | // Test that if DebugBreak is forced it is ignored when code from |
| 6796 | // debug-delay.js is executed. |
| 6797 | TEST(NoDebugBreakInAfterCompileMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6798 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6799 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6800 | v8::Local<v8::Context> context = env.context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6801 | |
| 6802 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6803 | v8::Debug::SetMessageHandler(env->GetIsolate(), BreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6804 | |
| 6805 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6806 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6807 | |
| 6808 | // Create a function for testing stepping. |
| 6809 | const char* src = "function f() { eval('var x = 10;'); } "; |
| 6810 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 6811 | |
| 6812 | // There should be only one break event. |
| 6813 | CHECK_EQ(1, break_point_hit_count); |
| 6814 | |
| 6815 | // Set the debug break flag again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6816 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6817 | f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6818 | // There should be one more break event when the script is evaluated in 'f'. |
| 6819 | CHECK_EQ(2, break_point_hit_count); |
| 6820 | |
| 6821 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6822 | v8::Debug::SetMessageHandler(env->GetIsolate(), nullptr); |
| 6823 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6827 | static int counting_message_handler_counter; |
| 6828 | |
| 6829 | static void CountingMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6830 | if (message.IsResponse()) counting_message_handler_counter++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6833 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6834 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6835 | TEST(ProcessDebugMessages) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6836 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6837 | v8::Isolate* isolate = env->GetIsolate(); |
| 6838 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6839 | |
| 6840 | counting_message_handler_counter = 0; |
| 6841 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6842 | v8::Debug::SetMessageHandler(isolate, CountingMessageHandler); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6843 | |
| 6844 | const int kBufferSize = 1000; |
| 6845 | uint16_t buffer[kBufferSize]; |
| 6846 | const char* scripts_command = |
| 6847 | "{\"seq\":0," |
| 6848 | "\"type\":\"request\"," |
| 6849 | "\"command\":\"scripts\"}"; |
| 6850 | |
| 6851 | // Send scripts command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6852 | v8::Debug::SendCommand( |
| 6853 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6854 | |
| 6855 | CHECK_EQ(0, counting_message_handler_counter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6856 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6857 | // At least one message should come |
| 6858 | CHECK_GE(counting_message_handler_counter, 1); |
| 6859 | |
| 6860 | counting_message_handler_counter = 0; |
| 6861 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6862 | v8::Debug::SendCommand( |
| 6863 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6864 | v8::Debug::SendCommand( |
| 6865 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6866 | CHECK_EQ(0, counting_message_handler_counter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6867 | v8::Debug::ProcessDebugMessages(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6868 | // At least two messages should come |
| 6869 | CHECK_GE(counting_message_handler_counter, 2); |
| 6870 | |
| 6871 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6872 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6873 | CheckDebuggerUnloaded(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | |
| 6877 | class SendCommandThread; |
| 6878 | static SendCommandThread* send_command_thread_ = NULL; |
| 6879 | |
| 6880 | |
| 6881 | class SendCommandThread : public v8::base::Thread { |
| 6882 | public: |
| 6883 | explicit SendCommandThread(v8::Isolate* isolate) |
| 6884 | : Thread(Options("SendCommandThread")), |
| 6885 | semaphore_(0), |
| 6886 | isolate_(isolate) {} |
| 6887 | |
| 6888 | static void CountingAndSignallingMessageHandler( |
| 6889 | const v8::Debug::Message& message) { |
| 6890 | if (message.IsResponse()) { |
| 6891 | counting_message_handler_counter++; |
| 6892 | send_command_thread_->semaphore_.Signal(); |
| 6893 | } |
| 6894 | } |
| 6895 | |
| 6896 | virtual void Run() { |
| 6897 | semaphore_.Wait(); |
| 6898 | const int kBufferSize = 1000; |
| 6899 | uint16_t buffer[kBufferSize]; |
| 6900 | const char* scripts_command = |
| 6901 | "{\"seq\":0," |
| 6902 | "\"type\":\"request\"," |
| 6903 | "\"command\":\"scripts\"}"; |
| 6904 | int length = AsciiToUtf16(scripts_command, buffer); |
| 6905 | // Send scripts command. |
| 6906 | |
| 6907 | for (int i = 0; i < 20; i++) { |
| 6908 | v8::base::ElapsedTimer timer; |
| 6909 | timer.Start(); |
| 6910 | CHECK_EQ(i, counting_message_handler_counter); |
| 6911 | // Queue debug message. |
| 6912 | v8::Debug::SendCommand(isolate_, buffer, length); |
| 6913 | // Wait for the message handler to pick up the response. |
| 6914 | semaphore_.Wait(); |
| 6915 | i::PrintF("iteration %d took %f ms\n", i, |
| 6916 | timer.Elapsed().InMillisecondsF()); |
| 6917 | } |
| 6918 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6919 | isolate_->TerminateExecution(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | void StartSending() { semaphore_.Signal(); } |
| 6923 | |
| 6924 | private: |
| 6925 | v8::base::Semaphore semaphore_; |
| 6926 | v8::Isolate* isolate_; |
| 6927 | }; |
| 6928 | |
| 6929 | |
| 6930 | static void StartSendingCommands( |
| 6931 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 6932 | send_command_thread_->StartSending(); |
| 6933 | } |
| 6934 | |
| 6935 | |
| 6936 | TEST(ProcessDebugMessagesThreaded) { |
| 6937 | DebugLocalContext env; |
| 6938 | v8::Isolate* isolate = env->GetIsolate(); |
| 6939 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6940 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6941 | |
| 6942 | counting_message_handler_counter = 0; |
| 6943 | |
| 6944 | v8::Debug::SetMessageHandler( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6945 | isolate, SendCommandThread::CountingAndSignallingMessageHandler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6946 | send_command_thread_ = new SendCommandThread(isolate); |
| 6947 | send_command_thread_->Start(); |
| 6948 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6949 | v8::Local<v8::FunctionTemplate> start = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6950 | v8::FunctionTemplate::New(isolate, StartSendingCommands); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6951 | CHECK(env->Global() |
| 6952 | ->Set(context, v8_str("start"), |
| 6953 | start->GetFunction(context).ToLocalChecked()) |
| 6954 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6955 | |
| 6956 | CompileRun("start(); while (true) { }"); |
| 6957 | |
| 6958 | CHECK_EQ(20, counting_message_handler_counter); |
| 6959 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6960 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 6961 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6965 | struct BacktraceData { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6966 | static int frame_counter; |
| 6967 | static void MessageHandler(const v8::Debug::Message& message) { |
| 6968 | char print_buffer[1000]; |
| 6969 | v8::String::Value json(message.GetJSON()); |
| 6970 | Utf16ToAscii(*json, json.length(), print_buffer, 1000); |
| 6971 | |
| 6972 | if (strstr(print_buffer, "backtrace") == NULL) { |
| 6973 | return; |
| 6974 | } |
| 6975 | frame_counter = GetTotalFramesInt(print_buffer); |
| 6976 | } |
| 6977 | }; |
| 6978 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6979 | int BacktraceData::frame_counter; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6980 | |
| 6981 | |
| 6982 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6983 | TEST(Backtrace) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6984 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6985 | v8::Isolate* isolate = env->GetIsolate(); |
| 6986 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6987 | v8::Local<v8::Context> context = env.context(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6988 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6989 | v8::Debug::SetMessageHandler(isolate, BacktraceData::MessageHandler); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6990 | |
| 6991 | const int kBufferSize = 1000; |
| 6992 | uint16_t buffer[kBufferSize]; |
| 6993 | const char* scripts_command = |
| 6994 | "{\"seq\":0," |
| 6995 | "\"type\":\"request\"," |
| 6996 | "\"command\":\"backtrace\"}"; |
| 6997 | |
| 6998 | // Check backtrace from ProcessDebugMessages. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6999 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7000 | v8::Debug::SendCommand( |
| 7001 | isolate, |
| 7002 | buffer, |
| 7003 | AsciiToUtf16(scripts_command, buffer), |
| 7004 | NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7005 | v8::Debug::ProcessDebugMessages(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7006 | CHECK_EQ(BacktraceData::frame_counter, 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7007 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7008 | v8::Local<v8::String> void0 = v8_str(env->GetIsolate(), "void(0)"); |
| 7009 | v8::Local<v8::Script> script = CompileWithOrigin(void0, void0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7010 | |
| 7011 | // Check backtrace from "void(0)" script. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7012 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7013 | v8::Debug::SendCommand( |
| 7014 | isolate, |
| 7015 | buffer, |
| 7016 | AsciiToUtf16(scripts_command, buffer), |
| 7017 | NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7018 | script->Run(context).ToLocalChecked(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7019 | CHECK_EQ(BacktraceData::frame_counter, 1); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7020 | |
| 7021 | // Get rid of the debug message handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7022 | v8::Debug::SetMessageHandler(isolate, nullptr); |
| 7023 | CheckDebuggerUnloaded(isolate); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7024 | } |
| 7025 | |
| 7026 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7027 | TEST(GetMirror) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7028 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7029 | v8::Isolate* isolate = env->GetIsolate(); |
| 7030 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7031 | v8::Local<v8::Context> context = env.context(); |
| 7032 | v8::Local<v8::Value> obj = |
| 7033 | v8::Debug::GetMirror(context, v8_str(isolate, "hodja")).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7034 | v8::ScriptCompiler::Source source(v8_str( |
| 7035 | "function runTest(mirror) {" |
| 7036 | " return mirror.isString() && (mirror.length() == 5);" |
| 7037 | "}" |
| 7038 | "" |
| 7039 | "runTest;")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7040 | v8::Local<v8::Function> run_test = v8::Local<v8::Function>::Cast( |
| 7041 | v8::ScriptCompiler::CompileUnboundScript(isolate, &source) |
| 7042 | .ToLocalChecked() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7043 | ->BindToCurrentContext() |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7044 | ->Run(context) |
| 7045 | .ToLocalChecked()); |
| 7046 | v8::Local<v8::Value> result = |
| 7047 | run_test->Call(context, env->Global(), 1, &obj).ToLocalChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7048 | CHECK(result->IsTrue()); |
| 7049 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7050 | |
| 7051 | |
| 7052 | // Test that the debug break flag works with function.apply. |
| 7053 | TEST(DebugBreakFunctionApply) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7054 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7055 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7056 | v8::Local<v8::Context> context = env.context(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7057 | |
| 7058 | // Create a function for testing breaking in apply. |
| 7059 | v8::Local<v8::Function> foo = CompileFunction( |
| 7060 | &env, |
| 7061 | "function baz(x) { }" |
| 7062 | "function bar(x) { baz(); }" |
| 7063 | "function foo(){ bar.apply(this, [1]); }", |
| 7064 | "foo"); |
| 7065 | |
| 7066 | // Register a debug event listener which steps and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7067 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7068 | |
| 7069 | // Set the debug break flag before calling the code using function.apply. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7070 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7071 | |
| 7072 | // Limit the number of debug breaks. This is a regression test for issue 493 |
| 7073 | // where this test would enter an infinite loop. |
| 7074 | break_point_hit_count = 0; |
| 7075 | max_break_point_hit_count = 10000; // 10000 => infinite loop. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7076 | foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7077 | |
| 7078 | // When keeping the debug break several break will happen. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7079 | CHECK_GT(break_point_hit_count, 1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7080 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7081 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7082 | CheckDebuggerUnloaded(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7086 | v8::Local<v8::Context> debugee_context; |
| 7087 | v8::Local<v8::Context> debugger_context; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7088 | |
| 7089 | |
| 7090 | // Property getter that checks that current and calling contexts |
| 7091 | // are both the debugee contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7092 | static void NamedGetterWithCallingContextCheck( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7093 | v8::Local<v8::String> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7094 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 7095 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(name), "a")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7096 | v8::Local<v8::Context> current = info.GetIsolate()->GetCurrentContext(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7097 | CHECK(current == debugee_context); |
| 7098 | CHECK(current != debugger_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7099 | info.GetReturnValue().Set(1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | |
| 7103 | // Debug event listener that checks if the first argument of a function is |
| 7104 | // an object with property 'a' == 1. If the property has custom accessor |
| 7105 | // this handler will eventually invoke it. |
| 7106 | static void DebugEventGetAtgumentPropertyValue( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7107 | const v8::Debug::EventDetails& event_details) { |
| 7108 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7109 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7110 | if (event == v8::Break) { |
| 7111 | break_point_hit_count++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7112 | CHECK(debugger_context == CcTest::isolate()->GetCurrentContext()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7113 | v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(CompileRun( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7114 | "(function(exec_state) {\n" |
| 7115 | " return (exec_state.frame(0).argumentValue(0).property('a').\n" |
| 7116 | " value().value() == 1);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7117 | "})")); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7118 | const int argc = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7119 | v8::Local<v8::Value> argv[argc] = {exec_state}; |
| 7120 | v8::Local<v8::Value> result = |
| 7121 | func->Call(debugger_context, exec_state, argc, argv).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7122 | CHECK(result->IsTrue()); |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | |
| 7127 | TEST(CallingContextIsNotDebugContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7128 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7129 | // Create and enter a debugee context. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7130 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7131 | v8::Isolate* isolate = env->GetIsolate(); |
| 7132 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7133 | env.ExposeDebug(); |
| 7134 | |
| 7135 | // Save handles to the debugger and debugee contexts to be used in |
| 7136 | // NamedGetterWithCallingContextCheck. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7137 | debugee_context = env.context(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7138 | debugger_context = v8::Utils::ToLocal(debug->debug_context()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7139 | |
| 7140 | // Create object with 'a' property accessor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7141 | v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
| 7142 | named->SetAccessor(v8_str(isolate, "a"), NamedGetterWithCallingContextCheck); |
| 7143 | CHECK(env->Global() |
| 7144 | ->Set(debugee_context, v8_str(isolate, "obj"), |
| 7145 | named->NewInstance(debugee_context).ToLocalChecked()) |
| 7146 | .FromJust()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7147 | |
| 7148 | // Register the debug event listener |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7149 | v8::Debug::SetDebugEventListener(isolate, DebugEventGetAtgumentPropertyValue); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7150 | |
| 7151 | // Create a function that invokes debugger. |
| 7152 | v8::Local<v8::Function> foo = CompileFunction( |
| 7153 | &env, |
| 7154 | "function bar(x) { debugger; }" |
| 7155 | "function foo(){ bar(obj); }", |
| 7156 | "foo"); |
| 7157 | |
| 7158 | break_point_hit_count = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7159 | foo->Call(debugee_context, env->Global(), 0, NULL).ToLocalChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7160 | CHECK_EQ(1, break_point_hit_count); |
| 7161 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7162 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 7163 | debugee_context = v8::Local<v8::Context>(); |
| 7164 | debugger_context = v8::Local<v8::Context>(); |
| 7165 | CheckDebuggerUnloaded(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7166 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7167 | |
| 7168 | |
| 7169 | TEST(DebugContextIsPreservedBetweenAccesses) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7170 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7171 | v8::Debug::SetDebugEventListener(CcTest::isolate(), |
| 7172 | DebugEventBreakPointHitCount); |
| 7173 | v8::Local<v8::Context> context1 = |
| 7174 | v8::Debug::GetDebugContext(CcTest::isolate()); |
| 7175 | v8::Local<v8::Context> context2 = |
| 7176 | v8::Debug::GetDebugContext(CcTest::isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7177 | CHECK(v8::Utils::OpenHandle(*context1).is_identical_to( |
| 7178 | v8::Utils::OpenHandle(*context2))); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7179 | v8::Debug::SetDebugEventListener(CcTest::isolate(), nullptr); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7183 | TEST(NoDebugContextWhenDebuggerDisabled) { |
| 7184 | v8::HandleScope scope(CcTest::isolate()); |
| 7185 | v8::Local<v8::Context> context = |
| 7186 | v8::Debug::GetDebugContext(CcTest::isolate()); |
| 7187 | CHECK(context.IsEmpty()); |
| 7188 | } |
| 7189 | |
| 7190 | |
| 7191 | static v8::Local<v8::Value> expected_callback_data; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7192 | static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { |
| 7193 | CHECK(details.GetEventContext() == expected_context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7194 | CHECK(expected_callback_data->Equals(details.GetEventContext(), |
| 7195 | details.GetCallbackData()) |
| 7196 | .FromJust()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7197 | } |
| 7198 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7199 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7200 | // Check that event details contain context where debug event occured. |
| 7201 | TEST(DebugEventContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7202 | v8::Isolate* isolate = CcTest::isolate(); |
| 7203 | v8::HandleScope scope(isolate); |
| 7204 | expected_context = v8::Context::New(isolate); |
| 7205 | expected_callback_data = v8::Int32::New(isolate, 2010); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7206 | v8::Debug::SetDebugEventListener(isolate, DebugEventContextChecker, |
| 7207 | expected_callback_data); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7208 | v8::Context::Scope context_scope(expected_context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7209 | v8::Script::Compile(expected_context, |
| 7210 | v8_str(isolate, "(function(){debugger;})();")) |
| 7211 | .ToLocalChecked() |
| 7212 | ->Run(expected_context) |
| 7213 | .ToLocalChecked(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7214 | expected_context.Clear(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7215 | v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 7216 | expected_context_data = v8::Local<v8::Value>(); |
| 7217 | CheckDebuggerUnloaded(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7218 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7219 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7220 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7221 | static bool debug_event_break_deoptimize_done = false; |
| 7222 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7223 | static void DebugEventBreakDeoptimize( |
| 7224 | const v8::Debug::EventDetails& event_details) { |
| 7225 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7226 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 7227 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7228 | if (event == v8::Break) { |
| 7229 | if (!frame_function_name.IsEmpty()) { |
| 7230 | // Get the name of the function. |
| 7231 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7232 | v8::Local<v8::Value> argv[argc] = { |
| 7233 | exec_state, v8::Integer::New(CcTest::isolate(), 0)}; |
| 7234 | v8::Local<v8::Value> result = |
| 7235 | frame_function_name->Call(context, exec_state, argc, argv) |
| 7236 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7237 | if (!result->IsUndefined()) { |
| 7238 | char fn[80]; |
| 7239 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7240 | v8::Local<v8::String> function_name( |
| 7241 | result->ToString(context).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7242 | function_name->WriteUtf8(fn); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7243 | if (strcmp(fn, "bar") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7244 | i::Deoptimizer::DeoptimizeAll(CcTest::i_isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7245 | debug_event_break_deoptimize_done = true; |
| 7246 | } |
| 7247 | } |
| 7248 | } |
| 7249 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7250 | v8::Debug::DebugBreak(CcTest::isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7251 | } |
| 7252 | } |
| 7253 | |
| 7254 | |
| 7255 | // Test deoptimization when execution is broken using the debug break stack |
| 7256 | // check interrupt. |
| 7257 | TEST(DeoptimizeDuringDebugBreak) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7258 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7259 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7260 | env.ExposeDebug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7261 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7262 | |
| 7263 | // Create a function for checking the function when hitting a break point. |
| 7264 | frame_function_name = CompileFunction(&env, |
| 7265 | frame_function_name_source, |
| 7266 | "frame_function_name"); |
| 7267 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7268 | // Set a debug event listener which will keep interrupting execution until |
| 7269 | // debug break. When inside function bar it will deoptimize all functions. |
| 7270 | // This tests lazy deoptimization bailout for the stack check, as the first |
| 7271 | // time in function bar when using debug break and no break points will be at |
| 7272 | // the initial stack check. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7273 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 7274 | DebugEventBreakDeoptimize); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7275 | |
| 7276 | // 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] | 7277 | v8::Local<v8::Function> f = CompileFunction(&env, "function bar(){}", "bar"); |
| 7278 | f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7279 | |
| 7280 | // Set debug break and call bar again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7281 | v8::Debug::DebugBreak(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7282 | f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7283 | |
| 7284 | CHECK(debug_event_break_deoptimize_done); |
| 7285 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7286 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7290 | static void DebugEventBreakWithOptimizedStack( |
| 7291 | const v8::Debug::EventDetails& event_details) { |
| 7292 | v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); |
| 7293 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7294 | v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); |
| 7295 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7296 | if (event == v8::Break) { |
| 7297 | if (!frame_function_name.IsEmpty()) { |
| 7298 | for (int i = 0; i < 2; i++) { |
| 7299 | const int argc = 2; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7300 | v8::Local<v8::Value> argv[argc] = {exec_state, |
| 7301 | v8::Integer::New(isolate, i)}; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7302 | // Get the name of the function in frame i. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7303 | v8::Local<v8::Value> result = |
| 7304 | frame_function_name->Call(context, exec_state, argc, argv) |
| 7305 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7306 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7307 | v8::Local<v8::String> function_name( |
| 7308 | result->ToString(context).ToLocalChecked()); |
| 7309 | CHECK( |
| 7310 | function_name->Equals(context, v8_str(isolate, "loop")).FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7311 | // Get the name of the first argument in frame i. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7312 | result = frame_argument_name->Call(context, exec_state, argc, argv) |
| 7313 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7314 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7315 | v8::Local<v8::String> argument_name( |
| 7316 | result->ToString(context).ToLocalChecked()); |
| 7317 | CHECK(argument_name->Equals(context, v8_str(isolate, "count")) |
| 7318 | .FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7319 | // Get the value of the first argument in frame i. If the |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 7320 | // function is optimized the value will be undefined, otherwise |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7321 | // the value will be '1 - i'. |
| 7322 | // |
| 7323 | // TODO(3141533): We should be able to get the real value for |
| 7324 | // optimized frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7325 | result = frame_argument_value->Call(context, exec_state, argc, argv) |
| 7326 | .ToLocalChecked(); |
| 7327 | CHECK(result->IsUndefined() || |
| 7328 | (result->Int32Value(context).FromJust() == 1 - i)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7329 | // Get the name of the first local variable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7330 | result = frame_local_name->Call(context, exec_state, argc, argv) |
| 7331 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7332 | CHECK(result->IsString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7333 | v8::Local<v8::String> local_name( |
| 7334 | result->ToString(context).ToLocalChecked()); |
| 7335 | CHECK(local_name->Equals(context, v8_str(isolate, "local")).FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7336 | // Get the value of the first local variable. If the function |
| 7337 | // is optimized the value will be undefined, otherwise it will |
| 7338 | // be 42. |
| 7339 | // |
| 7340 | // TODO(3141533): We should be able to get the real value for |
| 7341 | // optimized frames. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7342 | result = frame_local_value->Call(context, exec_state, argc, argv) |
| 7343 | .ToLocalChecked(); |
| 7344 | CHECK(result->IsUndefined() || |
| 7345 | (result->Int32Value(context).FromJust() == 42)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7346 | } |
| 7347 | } |
| 7348 | } |
| 7349 | } |
| 7350 | |
| 7351 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7352 | static void ScheduleBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7353 | v8::Debug::SetDebugEventListener(args.GetIsolate(), |
| 7354 | DebugEventBreakWithOptimizedStack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7355 | v8::Debug::DebugBreak(args.GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7356 | } |
| 7357 | |
| 7358 | |
| 7359 | TEST(DebugBreakStackInspection) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7360 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7361 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7362 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7363 | |
| 7364 | frame_function_name = |
| 7365 | CompileFunction(&env, frame_function_name_source, "frame_function_name"); |
| 7366 | frame_argument_name = |
| 7367 | CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); |
| 7368 | frame_argument_value = CompileFunction(&env, |
| 7369 | frame_argument_value_source, |
| 7370 | "frame_argument_value"); |
| 7371 | frame_local_name = |
| 7372 | CompileFunction(&env, frame_local_name_source, "frame_local_name"); |
| 7373 | frame_local_value = |
| 7374 | CompileFunction(&env, frame_local_value_source, "frame_local_value"); |
| 7375 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7376 | v8::Local<v8::FunctionTemplate> schedule_break_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7377 | v8::FunctionTemplate::New(env->GetIsolate(), ScheduleBreak); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7378 | v8::Local<v8::Function> schedule_break = |
| 7379 | schedule_break_template->GetFunction(context).ToLocalChecked(); |
| 7380 | CHECK(env->Global() |
| 7381 | ->Set(context, v8_str("scheduleBreak"), schedule_break) |
| 7382 | .FromJust()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7383 | |
| 7384 | const char* src = |
| 7385 | "function loop(count) {" |
| 7386 | " var local = 42;" |
| 7387 | " if (count < 1) { scheduleBreak(); loop(count + 1); }" |
| 7388 | "}" |
| 7389 | "loop(0);"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7390 | v8::Script::Compile(context, v8_str(env->GetIsolate(), src)) |
| 7391 | .ToLocalChecked() |
| 7392 | ->Run(context) |
| 7393 | .ToLocalChecked(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7394 | } |
| 7395 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7396 | |
| 7397 | // Test that setting the terminate execution flag during debug break processing. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7398 | static void TestDebugBreakInLoop(const char* loop_head, |
| 7399 | const char** loop_bodies, |
| 7400 | const char* loop_tail) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7401 | // Receive 10 breaks for each test and then terminate JavaScript execution. |
| 7402 | static const int kBreaksPerTest = 10; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7403 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7404 | for (int i = 0; loop_bodies[i] != NULL; i++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7405 | // Perform a lazy deoptimization after various numbers of breaks |
| 7406 | // have been hit. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7407 | |
| 7408 | EmbeddedVector<char, 1024> buffer; |
| 7409 | SNPrintF(buffer, "function f() {%s%s%s}", loop_head, loop_bodies[i], |
| 7410 | loop_tail); |
| 7411 | |
| 7412 | i::PrintF("%s\n", buffer.start()); |
| 7413 | |
| 7414 | for (int j = 0; j < 3; j++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7415 | break_point_hit_count_deoptimize = j; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7416 | if (j == 2) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7417 | break_point_hit_count_deoptimize = kBreaksPerTest; |
| 7418 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7419 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7420 | break_point_hit_count = 0; |
| 7421 | max_break_point_hit_count = kBreaksPerTest; |
| 7422 | terminate_after_max_break_point_hit = true; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7423 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7424 | // Function with infinite loop. |
| 7425 | CompileRun(buffer.start()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7426 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7427 | // Set the debug break to enter the debugger as soon as possible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7428 | v8::Debug::DebugBreak(CcTest::isolate()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7429 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7430 | // Call function with infinite loop. |
| 7431 | CompileRun("f();"); |
| 7432 | CHECK_EQ(kBreaksPerTest, break_point_hit_count); |
| 7433 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7434 | CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7435 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7436 | } |
| 7437 | } |
| 7438 | |
| 7439 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7440 | static const char* loop_bodies_1[] = {"", |
| 7441 | "g()", |
| 7442 | "if (a == 0) { g() }", |
| 7443 | "if (a == 1) { g() }", |
| 7444 | "if (a == 0) { g() } else { h() }", |
| 7445 | "if (a == 0) { continue }", |
| 7446 | NULL}; |
| 7447 | |
| 7448 | |
| 7449 | static const char* loop_bodies_2[] = { |
| 7450 | "if (a == 1) { continue }", |
| 7451 | "switch (a) { case 1: g(); }", |
| 7452 | "switch (a) { case 1: continue; }", |
| 7453 | "switch (a) { case 1: g(); break; default: h() }", |
| 7454 | "switch (a) { case 1: continue; break; default: h() }", |
| 7455 | NULL}; |
| 7456 | |
| 7457 | |
| 7458 | void DebugBreakLoop(const char* loop_header, const char** loop_bodies, |
| 7459 | const char* loop_footer) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7460 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7461 | v8::HandleScope scope(env->GetIsolate()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7462 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7463 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7464 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7465 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7466 | CompileRun( |
| 7467 | "var a = 1;\n" |
| 7468 | "function g() { }\n" |
| 7469 | "function h() { }"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7470 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7471 | TestDebugBreakInLoop(loop_header, loop_bodies, loop_footer); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7472 | |
| 7473 | // Get rid of the debug event listener. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7474 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7475 | CheckDebuggerUnloaded(env->GetIsolate()); |
| 7476 | } |
| 7477 | |
| 7478 | |
| 7479 | TEST(DebugBreakInWhileTrue1) { |
| 7480 | DebugBreakLoop("while (true) {", loop_bodies_1, "}"); |
| 7481 | } |
| 7482 | |
| 7483 | |
| 7484 | TEST(DebugBreakInWhileTrue2) { |
| 7485 | DebugBreakLoop("while (true) {", loop_bodies_2, "}"); |
| 7486 | } |
| 7487 | |
| 7488 | |
| 7489 | TEST(DebugBreakInWhileCondition1) { |
| 7490 | DebugBreakLoop("while (a == 1) {", loop_bodies_1, "}"); |
| 7491 | } |
| 7492 | |
| 7493 | |
| 7494 | TEST(DebugBreakInWhileCondition2) { |
| 7495 | DebugBreakLoop("while (a == 1) {", loop_bodies_2, "}"); |
| 7496 | } |
| 7497 | |
| 7498 | |
| 7499 | TEST(DebugBreakInDoWhileTrue1) { |
| 7500 | DebugBreakLoop("do {", loop_bodies_1, "} while (true)"); |
| 7501 | } |
| 7502 | |
| 7503 | |
| 7504 | TEST(DebugBreakInDoWhileTrue2) { |
| 7505 | DebugBreakLoop("do {", loop_bodies_2, "} while (true)"); |
| 7506 | } |
| 7507 | |
| 7508 | |
| 7509 | TEST(DebugBreakInDoWhileCondition1) { |
| 7510 | DebugBreakLoop("do {", loop_bodies_1, "} while (a == 1)"); |
| 7511 | } |
| 7512 | |
| 7513 | |
| 7514 | TEST(DebugBreakInDoWhileCondition2) { |
| 7515 | DebugBreakLoop("do {", loop_bodies_2, "} while (a == 1)"); |
| 7516 | } |
| 7517 | |
| 7518 | |
| 7519 | TEST(DebugBreakInFor1) { DebugBreakLoop("for (;;) {", loop_bodies_1, "}"); } |
| 7520 | |
| 7521 | |
| 7522 | TEST(DebugBreakInFor2) { DebugBreakLoop("for (;;) {", loop_bodies_2, "}"); } |
| 7523 | |
| 7524 | |
| 7525 | TEST(DebugBreakInForCondition1) { |
| 7526 | DebugBreakLoop("for (;a == 1;) {", loop_bodies_1, "}"); |
| 7527 | } |
| 7528 | |
| 7529 | |
| 7530 | TEST(DebugBreakInForCondition2) { |
| 7531 | DebugBreakLoop("for (;a == 1;) {", loop_bodies_2, "}"); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7535 | v8::Local<v8::Script> inline_script; |
| 7536 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7537 | static void DebugBreakInlineListener( |
| 7538 | const v8::Debug::EventDetails& event_details) { |
| 7539 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7540 | v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7541 | if (event != v8::Break) return; |
| 7542 | |
| 7543 | int expected_frame_count = 4; |
| 7544 | int expected_line_number[] = {1, 4, 7, 12}; |
| 7545 | |
| 7546 | i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script); |
| 7547 | i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast( |
| 7548 | i::JSFunction::cast(*compiled_script)->shared()->script())); |
| 7549 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7550 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7551 | char script[128]; |
| 7552 | i::Vector<char> script_vector(script, sizeof(script)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7553 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7554 | v8::Local<v8::Value> result = CompileRun(script); |
| 7555 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7556 | int frame_count = result->Int32Value(context).FromJust(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7557 | CHECK_EQ(expected_frame_count, frame_count); |
| 7558 | |
| 7559 | for (int i = 0; i < frame_count; i++) { |
| 7560 | // The 5. element in the returned array of GetFrameDetails contains the |
| 7561 | // source position of that frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7562 | SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[5]", break_id, i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7563 | v8::Local<v8::Value> result = CompileRun(script); |
| 7564 | CHECK_EQ(expected_line_number[i], |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7565 | i::Script::GetLineNumber(source_script, |
| 7566 | result->Int32Value(context).FromJust())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7567 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7568 | v8::Debug::SetDebugEventListener(CcTest::isolate(), nullptr); |
| 7569 | CcTest::isolate()->TerminateExecution(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7570 | } |
| 7571 | |
| 7572 | |
| 7573 | TEST(DebugBreakInline) { |
| 7574 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7575 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7576 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7577 | v8::Local<v8::Context> context = env.context(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7578 | const char* source = |
| 7579 | "function debug(b) { \n" |
| 7580 | " if (b) debugger; \n" |
| 7581 | "} \n" |
| 7582 | "function f(b) { \n" |
| 7583 | " debug(b) \n" |
| 7584 | "}; \n" |
| 7585 | "function g(b) { \n" |
| 7586 | " f(b); \n" |
| 7587 | "}; \n" |
| 7588 | "g(false); \n" |
| 7589 | "g(false); \n" |
| 7590 | "%OptimizeFunctionOnNextCall(g); \n" |
| 7591 | "g(true);"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7592 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7593 | inline_script = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7594 | v8::Script::Compile(context, v8_str(env->GetIsolate(), source)) |
| 7595 | .ToLocalChecked(); |
| 7596 | inline_script->Run(context).ToLocalChecked(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7600 | static void DebugEventStepNext( |
| 7601 | const v8::Debug::EventDetails& event_details) { |
| 7602 | v8::DebugEvent event = event_details.GetEvent(); |
| 7603 | if (event == v8::Break) { |
| 7604 | PrepareStep(StepNext); |
| 7605 | } |
| 7606 | } |
| 7607 | |
| 7608 | |
| 7609 | static void RunScriptInANewCFrame(const char* source) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7610 | v8::TryCatch try_catch(CcTest::isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7611 | CompileRun(source); |
| 7612 | CHECK(try_catch.HasCaught()); |
| 7613 | } |
| 7614 | |
| 7615 | |
| 7616 | TEST(Regress131642) { |
| 7617 | // Bug description: |
| 7618 | // When doing StepNext through the first script, the debugger is not reset |
| 7619 | // after exiting through exception. A flawed implementation enabling the |
| 7620 | // debugger to step into Array.prototype.forEach breaks inside the callback |
| 7621 | // for forEach in the second script under the assumption that we are in a |
| 7622 | // recursive call. In an attempt to step out, we crawl the stack using the |
| 7623 | // recorded frame pointer from the first script and fail when not finding it |
| 7624 | // on the stack. |
| 7625 | DebugLocalContext env; |
| 7626 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7627 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugEventStepNext); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7628 | |
| 7629 | // We step through the first script. It exits through an exception. We run |
| 7630 | // this inside a new frame to record a different FP than the second script |
| 7631 | // would expect. |
| 7632 | const char* script_1 = "debugger; throw new Error();"; |
| 7633 | RunScriptInANewCFrame(script_1); |
| 7634 | |
| 7635 | // The second script uses forEach. |
| 7636 | const char* script_2 = "[0].forEach(function() { });"; |
| 7637 | CompileRun(script_2); |
| 7638 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7639 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | |
| 7643 | // Import from test-heap.cc |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7644 | namespace v8 { |
| 7645 | namespace internal { |
| 7646 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7647 | int CountNativeContexts(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7648 | } |
| 7649 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7650 | |
| 7651 | |
| 7652 | static void NopListener(const v8::Debug::EventDetails& event_details) { |
| 7653 | } |
| 7654 | |
| 7655 | |
| 7656 | TEST(DebuggerCreatesContextIffActive) { |
| 7657 | DebugLocalContext env; |
| 7658 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7659 | CHECK_EQ(1, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7660 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7661 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7662 | CompileRun("debugger;"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7663 | CHECK_EQ(1, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7664 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7665 | v8::Debug::SetDebugEventListener(env->GetIsolate(), NopListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7666 | CompileRun("debugger;"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7667 | CHECK_EQ(2, v8::internal::CountNativeContexts()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7668 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7669 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
| 7672 | |
| 7673 | TEST(LiveEditEnabled) { |
| 7674 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7675 | LocalContext env; |
| 7676 | v8::HandleScope scope(env->GetIsolate()); |
| 7677 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), true); |
| 7678 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7679 | } |
| 7680 | |
| 7681 | |
| 7682 | TEST(LiveEditDisabled) { |
| 7683 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7684 | LocalContext env; |
| 7685 | v8::HandleScope scope(env->GetIsolate()); |
| 7686 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), false); |
| 7687 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7688 | } |
| 7689 | |
| 7690 | |
| 7691 | TEST(PrecompiledFunction) { |
| 7692 | // Regression test for crbug.com/346207. If we have preparse data, parsing the |
| 7693 | // function in the presence of the debugger (and breakpoints) should still |
| 7694 | // succeed. The bug was that preparsing was done lazily and parsing was done |
| 7695 | // eagerly, so, the symbol streams didn't match. |
| 7696 | DebugLocalContext env; |
| 7697 | v8::HandleScope scope(env->GetIsolate()); |
| 7698 | env.ExposeDebug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7699 | v8::Debug::SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7700 | |
| 7701 | v8::Local<v8::Function> break_here = |
| 7702 | CompileFunction(&env, "function break_here(){}", "break_here"); |
| 7703 | SetBreakPoint(break_here, 0); |
| 7704 | |
| 7705 | const char* source = |
| 7706 | "var a = b = c = 1; \n" |
| 7707 | "function this_is_lazy() { \n" |
| 7708 | // This symbol won't appear in the preparse data. |
| 7709 | " var a; \n" |
| 7710 | "} \n" |
| 7711 | "function bar() { \n" |
| 7712 | " return \"bar\"; \n" |
| 7713 | "}; \n" |
| 7714 | "a = b = c = 2; \n" |
| 7715 | "bar(); \n"; |
| 7716 | v8::Local<v8::Value> result = ParserCacheCompileRun(source); |
| 7717 | CHECK(result->IsString()); |
| 7718 | v8::String::Utf8Value utf8(result); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7719 | CHECK_EQ(0, strcmp("bar", *utf8)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7720 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7721 | v8::Debug::SetDebugEventListener(env->GetIsolate(), nullptr); |
| 7722 | CheckDebuggerUnloaded(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | |
| 7726 | static void DebugBreakStackTraceListener( |
| 7727 | const v8::Debug::EventDetails& event_details) { |
| 7728 | v8::StackTrace::CurrentStackTrace(CcTest::isolate(), 10); |
| 7729 | } |
| 7730 | |
| 7731 | |
| 7732 | static void AddDebugBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7733 | v8::Debug::DebugBreak(args.GetIsolate()); |
| 7734 | } |
| 7735 | |
| 7736 | |
| 7737 | TEST(DebugBreakStackTrace) { |
| 7738 | DebugLocalContext env; |
| 7739 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7740 | v8::Debug::SetDebugEventListener(env->GetIsolate(), |
| 7741 | DebugBreakStackTraceListener); |
| 7742 | v8::Local<v8::Context> context = env.context(); |
| 7743 | v8::Local<v8::FunctionTemplate> add_debug_break_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7744 | v8::FunctionTemplate::New(env->GetIsolate(), AddDebugBreak); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7745 | v8::Local<v8::Function> add_debug_break = |
| 7746 | add_debug_break_template->GetFunction(context).ToLocalChecked(); |
| 7747 | CHECK(env->Global() |
| 7748 | ->Set(context, v8_str("add_debug_break"), add_debug_break) |
| 7749 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7750 | |
| 7751 | CompileRun("(function loop() {" |
| 7752 | " for (var j = 0; j < 1000; j++) {" |
| 7753 | " for (var i = 0; i < 1000; i++) {" |
| 7754 | " if (i == 999) add_debug_break();" |
| 7755 | " }" |
| 7756 | " }" |
| 7757 | "})()"); |
| 7758 | } |
| 7759 | |
| 7760 | |
| 7761 | v8::base::Semaphore terminate_requested_semaphore(0); |
| 7762 | v8::base::Semaphore terminate_fired_semaphore(0); |
| 7763 | bool terminate_already_fired = false; |
| 7764 | |
| 7765 | |
| 7766 | static void DebugBreakTriggerTerminate( |
| 7767 | const v8::Debug::EventDetails& event_details) { |
| 7768 | if (event_details.GetEvent() != v8::Break || terminate_already_fired) return; |
| 7769 | terminate_requested_semaphore.Signal(); |
| 7770 | // Wait for at most 2 seconds for the terminate request. |
| 7771 | CHECK(terminate_fired_semaphore.WaitFor(v8::base::TimeDelta::FromSeconds(2))); |
| 7772 | terminate_already_fired = true; |
| 7773 | } |
| 7774 | |
| 7775 | |
| 7776 | class TerminationThread : public v8::base::Thread { |
| 7777 | public: |
| 7778 | explicit TerminationThread(v8::Isolate* isolate) |
| 7779 | : Thread(Options("terminator")), isolate_(isolate) {} |
| 7780 | |
| 7781 | virtual void Run() { |
| 7782 | terminate_requested_semaphore.Wait(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7783 | isolate_->TerminateExecution(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7784 | terminate_fired_semaphore.Signal(); |
| 7785 | } |
| 7786 | |
| 7787 | private: |
| 7788 | v8::Isolate* isolate_; |
| 7789 | }; |
| 7790 | |
| 7791 | |
| 7792 | TEST(DebugBreakOffThreadTerminate) { |
| 7793 | DebugLocalContext env; |
| 7794 | v8::Isolate* isolate = env->GetIsolate(); |
| 7795 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7796 | v8::Debug::SetDebugEventListener(isolate, DebugBreakTriggerTerminate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7797 | TerminationThread terminator(isolate); |
| 7798 | terminator.Start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7799 | v8::TryCatch try_catch(env->GetIsolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7800 | v8::Debug::DebugBreak(isolate); |
| 7801 | CompileRun("while (true);"); |
| 7802 | CHECK(try_catch.HasTerminated()); |
| 7803 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7804 | |
| 7805 | |
| 7806 | static void DebugEventExpectNoException( |
| 7807 | const v8::Debug::EventDetails& event_details) { |
| 7808 | v8::DebugEvent event = event_details.GetEvent(); |
| 7809 | CHECK_NE(v8::Exception, event); |
| 7810 | } |
| 7811 | |
| 7812 | |
| 7813 | static void TryCatchWrappedThrowCallback( |
| 7814 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7815 | v8::TryCatch try_catch(args.GetIsolate()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7816 | CompileRun("throw 'rejection';"); |
| 7817 | CHECK(try_catch.HasCaught()); |
| 7818 | } |
| 7819 | |
| 7820 | |
| 7821 | TEST(DebugPromiseInterceptedByTryCatch) { |
| 7822 | DebugLocalContext env; |
| 7823 | v8::Isolate* isolate = env->GetIsolate(); |
| 7824 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7825 | v8::Debug::SetDebugEventListener(isolate, &DebugEventExpectNoException); |
| 7826 | v8::Local<v8::Context> context = env.context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7827 | ChangeBreakOnException(false, true); |
| 7828 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7829 | v8::Local<v8::FunctionTemplate> fun = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7830 | v8::FunctionTemplate::New(isolate, TryCatchWrappedThrowCallback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7831 | CHECK(env->Global() |
| 7832 | ->Set(context, v8_str("fun"), |
| 7833 | fun->GetFunction(context).ToLocalChecked()) |
| 7834 | .FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7835 | |
| 7836 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7837 | CompileRun( |
| 7838 | "var r;" |
| 7839 | "p.chain(function() { r = 'resolved'; }," |
| 7840 | " function() { r = 'rejected'; });"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7841 | CHECK(CompileRun("r")->Equals(context, v8_str("resolved")).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7842 | } |
| 7843 | |
| 7844 | |
| 7845 | static int exception_event_counter = 0; |
| 7846 | |
| 7847 | |
| 7848 | static void DebugEventCountException( |
| 7849 | const v8::Debug::EventDetails& event_details) { |
| 7850 | v8::DebugEvent event = event_details.GetEvent(); |
| 7851 | if (event == v8::Exception) exception_event_counter++; |
| 7852 | } |
| 7853 | |
| 7854 | |
| 7855 | static void ThrowCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7856 | CompileRun("throw 'rejection';"); |
| 7857 | } |
| 7858 | |
| 7859 | |
| 7860 | TEST(DebugPromiseRejectedByCallback) { |
| 7861 | DebugLocalContext env; |
| 7862 | v8::Isolate* isolate = env->GetIsolate(); |
| 7863 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7864 | v8::Debug::SetDebugEventListener(isolate, &DebugEventCountException); |
| 7865 | v8::Local<v8::Context> context = env.context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7866 | ChangeBreakOnException(false, true); |
| 7867 | exception_event_counter = 0; |
| 7868 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7869 | v8::Local<v8::FunctionTemplate> fun = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7870 | v8::FunctionTemplate::New(isolate, ThrowCallback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7871 | CHECK(env->Global() |
| 7872 | ->Set(context, v8_str("fun"), |
| 7873 | fun->GetFunction(context).ToLocalChecked()) |
| 7874 | .FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7875 | |
| 7876 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7877 | CompileRun( |
| 7878 | "var r;" |
| 7879 | "p.chain(function() { r = 'resolved'; }," |
| 7880 | " function(e) { r = 'rejected' + e; });"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7881 | CHECK( |
| 7882 | CompileRun("r")->Equals(context, v8_str("rejectedrejection")).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7883 | CHECK_EQ(1, exception_event_counter); |
| 7884 | } |
| 7885 | |
| 7886 | |
| 7887 | TEST(DebugBreakOnExceptionInObserveCallback) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7888 | i::FLAG_harmony_object_observe = true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7889 | DebugLocalContext env; |
| 7890 | v8::Isolate* isolate = env->GetIsolate(); |
| 7891 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7892 | v8::Debug::SetDebugEventListener(isolate, &DebugEventCountException); |
| 7893 | v8::Local<v8::Context> context = env.context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7894 | // Break on uncaught exception |
| 7895 | ChangeBreakOnException(false, true); |
| 7896 | exception_event_counter = 0; |
| 7897 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7898 | v8::Local<v8::FunctionTemplate> fun = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7899 | v8::FunctionTemplate::New(isolate, ThrowCallback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7900 | CHECK(env->Global() |
| 7901 | ->Set(context, v8_str("fun"), |
| 7902 | fun->GetFunction(context).ToLocalChecked()) |
| 7903 | .FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7904 | |
| 7905 | CompileRun( |
| 7906 | "var obj = {};" |
| 7907 | "var callbackRan = false;" |
| 7908 | "Object.observe(obj, function() {" |
| 7909 | " callbackRan = true;" |
| 7910 | " throw Error('foo');" |
| 7911 | "});" |
| 7912 | "obj.prop = 1"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7913 | CHECK(CompileRun("callbackRan")->BooleanValue(context).FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7914 | CHECK_EQ(1, exception_event_counter); |
| 7915 | } |
| 7916 | |
| 7917 | |
| 7918 | static void DebugHarmonyScopingListener( |
| 7919 | const v8::Debug::EventDetails& event_details) { |
| 7920 | v8::DebugEvent event = event_details.GetEvent(); |
| 7921 | if (event != v8::Break) return; |
| 7922 | |
| 7923 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
| 7924 | |
| 7925 | char script[128]; |
| 7926 | i::Vector<char> script_vector(script, sizeof(script)); |
| 7927 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
| 7928 | ExpectInt32(script, 1); |
| 7929 | |
| 7930 | SNPrintF(script_vector, "var frame = new FrameMirror(%d, 0);", break_id); |
| 7931 | CompileRun(script); |
| 7932 | ExpectInt32("frame.evaluate('x').value_", 1); |
| 7933 | ExpectInt32("frame.evaluate('y').value_", 2); |
| 7934 | |
| 7935 | CompileRun("var allScopes = frame.allScopes()"); |
| 7936 | ExpectInt32("allScopes.length", 2); |
| 7937 | |
| 7938 | ExpectBoolean("allScopes[0].scopeType() === ScopeType.Script", true); |
| 7939 | |
| 7940 | ExpectInt32("allScopes[0].scopeObject().value_.x", 1); |
| 7941 | |
| 7942 | ExpectInt32("allScopes[0].scopeObject().value_.y", 2); |
| 7943 | |
| 7944 | CompileRun("allScopes[0].setVariableValue('x', 5);"); |
| 7945 | CompileRun("allScopes[0].setVariableValue('y', 6);"); |
| 7946 | ExpectInt32("frame.evaluate('x + y').value_", 11); |
| 7947 | } |
| 7948 | |
| 7949 | |
| 7950 | TEST(DebugBreakInLexicalScopes) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7951 | i::FLAG_allow_natives_syntax = true; |
| 7952 | |
| 7953 | DebugLocalContext env; |
| 7954 | v8::Isolate* isolate = env->GetIsolate(); |
| 7955 | v8::HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7956 | v8::Debug::SetDebugEventListener(isolate, DebugHarmonyScopingListener); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7957 | |
| 7958 | CompileRun( |
| 7959 | "'use strict'; \n" |
| 7960 | "let x = 1; \n"); |
| 7961 | ExpectInt32( |
| 7962 | "'use strict'; \n" |
| 7963 | "let y = 2; \n" |
| 7964 | "debugger; \n" |
| 7965 | "x * y", |
| 7966 | 30); |
| 7967 | ExpectInt32( |
| 7968 | "x = 1; y = 2; \n" |
| 7969 | "debugger;" |
| 7970 | "x * y", |
| 7971 | 30); |
| 7972 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7973 | |
| 7974 | static int after_compile_handler_depth = 0; |
| 7975 | static void HandleInterrupt(v8::Isolate* isolate, void* data) { |
| 7976 | CHECK_EQ(0, after_compile_handler_depth); |
| 7977 | } |
| 7978 | |
| 7979 | static void NoInterruptsOnDebugEvent( |
| 7980 | const v8::Debug::EventDetails& event_details) { |
| 7981 | if (event_details.GetEvent() != v8::AfterCompile) return; |
| 7982 | ++after_compile_handler_depth; |
| 7983 | // Do not allow nested AfterCompile events. |
| 7984 | CHECK(after_compile_handler_depth <= 1); |
| 7985 | v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); |
| 7986 | isolate->RequestInterrupt(&HandleInterrupt, nullptr); |
| 7987 | CompileRun("function foo() {}; foo();"); |
| 7988 | --after_compile_handler_depth; |
| 7989 | } |
| 7990 | |
| 7991 | |
| 7992 | TEST(NoInterruptsInDebugListener) { |
| 7993 | DebugLocalContext env; |
| 7994 | v8::Debug::SetDebugEventListener(env->GetIsolate(), NoInterruptsOnDebugEvent); |
| 7995 | CompileRun("void(0);"); |
| 7996 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 7997 | |
| 7998 | class TestBreakLocation : public i::BreakLocation { |
| 7999 | public: |
| 8000 | using i::BreakLocation::GetIterator; |
| 8001 | using i::BreakLocation::Iterator; |
| 8002 | }; |
| 8003 | |
| 8004 | TEST(BreakLocationIterator) { |
| 8005 | DebugLocalContext env; |
| 8006 | v8::Isolate* isolate = env->GetIsolate(); |
| 8007 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 8008 | v8::HandleScope scope(isolate); |
| 8009 | |
| 8010 | v8::Local<v8::Value> result = CompileRun( |
| 8011 | "function f() {\n" |
| 8012 | " debugger; \n" |
| 8013 | " f(); \n" |
| 8014 | " debugger; \n" |
| 8015 | "} \n" |
| 8016 | "f"); |
| 8017 | Handle<i::Object> function_obj = v8::Utils::OpenHandle(*result); |
| 8018 | Handle<i::JSFunction> function = Handle<i::JSFunction>::cast(function_obj); |
| 8019 | Handle<i::SharedFunctionInfo> shared(function->shared()); |
| 8020 | |
| 8021 | EnableDebugger(isolate); |
| 8022 | CHECK(i_isolate->debug()->EnsureDebugInfo(shared, function)); |
| 8023 | |
| 8024 | Handle<i::DebugInfo> debug_info(shared->GetDebugInfo()); |
| 8025 | int code_size = debug_info->abstract_code()->Size(); |
| 8026 | |
| 8027 | bool found_return = false; |
| 8028 | bool found_call = false; |
| 8029 | bool found_debugger = false; |
| 8030 | |
| 8031 | // Test public interface. |
| 8032 | for (int i = 0; i < code_size; i++) { |
| 8033 | i::BreakLocation location = i::BreakLocation::FromCodeOffset(debug_info, i); |
| 8034 | if (location.IsCall()) found_call = true; |
| 8035 | if (location.IsReturn()) found_return = true; |
| 8036 | if (location.IsDebuggerStatement()) found_debugger = true; |
| 8037 | } |
| 8038 | CHECK(found_call); |
| 8039 | CHECK(found_return); |
| 8040 | CHECK(found_debugger); |
| 8041 | |
| 8042 | // Test underlying implementation. |
| 8043 | TestBreakLocation::Iterator* iterator = |
| 8044 | TestBreakLocation::GetIterator(debug_info, i::ALL_BREAK_LOCATIONS); |
| 8045 | CHECK(iterator->GetBreakLocation().IsDebuggerStatement()); |
| 8046 | CHECK_EQ(7, iterator->GetBreakLocation().position()); |
| 8047 | iterator->Next(); |
| 8048 | CHECK(iterator->GetBreakLocation().IsDebugBreakSlot()); |
| 8049 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8050 | iterator->Next(); |
| 8051 | CHECK(iterator->GetBreakLocation().IsCall()); |
| 8052 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8053 | iterator->Next(); |
| 8054 | CHECK(iterator->GetBreakLocation().IsDebuggerStatement()); |
| 8055 | CHECK_EQ(37, iterator->GetBreakLocation().position()); |
| 8056 | iterator->Next(); |
| 8057 | CHECK(iterator->GetBreakLocation().IsReturn()); |
| 8058 | CHECK_EQ(50, iterator->GetBreakLocation().position()); |
| 8059 | iterator->Next(); |
| 8060 | CHECK(iterator->Done()); |
| 8061 | delete iterator; |
| 8062 | |
| 8063 | iterator = TestBreakLocation::GetIterator(debug_info, i::CALLS_AND_RETURNS); |
| 8064 | CHECK(iterator->GetBreakLocation().IsCall()); |
| 8065 | CHECK_EQ(22, iterator->GetBreakLocation().position()); |
| 8066 | iterator->Next(); |
| 8067 | CHECK(iterator->GetBreakLocation().IsReturn()); |
| 8068 | CHECK_EQ(50, iterator->GetBreakLocation().position()); |
| 8069 | iterator->Next(); |
| 8070 | CHECK(iterator->Done()); |
| 8071 | delete iterator; |
| 8072 | |
| 8073 | DisableDebugger(isolate); |
| 8074 | } |