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" |
| 36 | #include "src/debug.h" |
| 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; |
| 55 | using ::v8::internal::Debugger; |
| 56 | using ::v8::internal::CommandMessage; |
| 57 | using ::v8::internal::CommandMessageQueue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | using ::v8::internal::StackFrame; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | using ::v8::internal::StepAction; |
| 60 | using ::v8::internal::StepIn; // From StepAction enum |
| 61 | using ::v8::internal::StepNext; // From StepAction enum |
| 62 | using ::v8::internal::StepOut; // From StepAction enum |
| 63 | using ::v8::internal::Vector; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 64 | using ::v8::internal::StrLength; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | |
| 66 | // Size of temp buffer for formatting small strings. |
| 67 | #define SMALL_STRING_BUFFER_SIZE 80 |
| 68 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | // --- H e l p e r C l a s s e s |
| 70 | |
| 71 | |
| 72 | // Helper class for creating a V8 enviromnent for running tests |
| 73 | class DebugLocalContext { |
| 74 | public: |
| 75 | inline DebugLocalContext( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | v8::Isolate* isolate, v8::ExtensionConfiguration* extensions = 0, |
| 77 | v8::Handle<v8::ObjectTemplate> global_template = |
| 78 | v8::Handle<v8::ObjectTemplate>(), |
| 79 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) |
| 80 | : scope_(isolate), |
| 81 | context_(v8::Context::New(isolate, extensions, global_template, |
| 82 | global_object)) { |
| 83 | context_->Enter(); |
| 84 | } |
| 85 | inline DebugLocalContext( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | v8::ExtensionConfiguration* extensions = 0, |
| 87 | v8::Handle<v8::ObjectTemplate> global_template = |
| 88 | v8::Handle<v8::ObjectTemplate>(), |
| 89 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | : scope_(CcTest::isolate()), |
| 91 | context_(v8::Context::New(CcTest::isolate(), extensions, |
| 92 | global_template, global_object)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | context_->Enter(); |
| 94 | } |
| 95 | inline ~DebugLocalContext() { |
| 96 | context_->Exit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | inline v8::Local<v8::Context> context() { return context_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | inline v8::Context* operator->() { return *context_; } |
| 100 | inline v8::Context* operator*() { return *context_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | inline v8::Isolate* GetIsolate() { return context_->GetIsolate(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | inline bool IsReady() { return !context_.IsEmpty(); } |
| 103 | void ExposeDebug() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | v8::internal::Isolate* isolate = |
| 105 | reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate()); |
| 106 | v8::internal::Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | // Expose the debug context global object in the global object for testing. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | CHECK(isolate->debug()->Load()); |
| 109 | Handle<v8::internal::Context> debug_context = |
| 110 | isolate->debug()->debug_context(); |
| 111 | debug_context->set_security_token( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | v8::Utils::OpenHandle(*context_)->security_token()); |
| 113 | |
| 114 | Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( |
| 115 | v8::Utils::OpenHandle(*context_->Global()))); |
| 116 | Handle<v8::internal::String> debug_string = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 117 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("debug")); |
| 118 | v8::internal::Runtime::DefineObjectProperty(global, debug_string, |
| 119 | handle(debug_context->global_proxy(), isolate), DONT_ENUM).Check(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 121 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | v8::HandleScope scope_; |
| 124 | v8::Local<v8::Context> context_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
| 128 | // --- H e l p e r F u n c t i o n s |
| 129 | |
| 130 | |
| 131 | // Compile and run the supplied source and return the fequested function. |
| 132 | static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, |
| 133 | const char* source, |
| 134 | const char* function_name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), source)) |
| 136 | ->Run(); |
| 137 | return v8::Local<v8::Function>::Cast((*env)->Global()->Get( |
| 138 | v8::String::NewFromUtf8(env->GetIsolate(), function_name))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | // Compile and run the supplied source and return the requested function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | static v8::Local<v8::Function> CompileFunction(v8::Isolate* isolate, |
| 144 | const char* source, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | const char* function_name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, source))->Run(); |
| 147 | v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | return v8::Local<v8::Function>::Cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | global->Get(v8::String::NewFromUtf8(isolate, function_name))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | // Is there any debug info for the function? |
| 154 | static bool HasDebugInfo(v8::Handle<v8::Function> fun) { |
| 155 | Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun); |
| 156 | Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); |
| 157 | return Debug::HasDebugInfo(shared); |
| 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. |
| 177 | static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) { |
| 178 | return SetBreakPoint(v8::Utils::OpenHandle(*fun), position); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // Set a break point in a function using the Debug object and return the |
| 183 | // associated break point number. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | static int SetBreakPointFromJS(v8::Isolate* isolate, |
| 185 | const char* function_name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | int line, int position) { |
| 187 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | SNPrintF(buffer, |
| 189 | "debug.Debug.setBreakPoint(%s,%d,%d)", |
| 190 | function_name, line, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 192 | v8::Handle<v8::String> str = v8::String::NewFromUtf8(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | return v8::Script::Compile(str)->Run()->Int32Value(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // 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] | 198 | static int SetScriptBreakPointByIdFromJS(v8::Isolate* isolate, int script_id, |
| 199 | int line, int column) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 201 | if (column >= 0) { |
| 202 | // Column specified set script break point on precise location. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 203 | SNPrintF(buffer, |
| 204 | "debug.Debug.setScriptBreakPointById(%d,%d,%d)", |
| 205 | script_id, line, column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | } else { |
| 207 | // Column not specified set script break point on line. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 208 | SNPrintF(buffer, |
| 209 | "debug.Debug.setScriptBreakPointById(%d,%d)", |
| 210 | script_id, line); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | } |
| 212 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 213 | { |
| 214 | v8::TryCatch try_catch; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | v8::Handle<v8::String> str = |
| 216 | v8::String::NewFromUtf8(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
| 218 | CHECK(!try_catch.HasCaught()); |
| 219 | return value->Int32Value(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // Set a break point in a script identified by name using the global Debug |
| 225 | // object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | static int SetScriptBreakPointByNameFromJS(v8::Isolate* isolate, |
| 227 | const char* script_name, int line, |
| 228 | int column) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 230 | if (column >= 0) { |
| 231 | // Column specified set script break point on precise location. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 232 | SNPrintF(buffer, |
| 233 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", |
| 234 | script_name, line, column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | } else { |
| 236 | // Column not specified set script break point on line. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 237 | SNPrintF(buffer, |
| 238 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", |
| 239 | script_name, line); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 242 | { |
| 243 | v8::TryCatch try_catch; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 244 | v8::Handle<v8::String> str = |
| 245 | v8::String::NewFromUtf8(isolate, buffer.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
| 247 | CHECK(!try_catch.HasCaught()); |
| 248 | return value->Int32Value(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | // Clear a break point. |
| 254 | static void ClearBreakPoint(int break_point) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 256 | v8::internal::Debug* debug = isolate->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 257 | debug->ClearBreakPoint( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 258 | Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | // Clear a break point using the global Debug object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 263 | static void ClearBreakPointFromJS(v8::Isolate* isolate, |
| 264 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 266 | SNPrintF(buffer, |
| 267 | "debug.Debug.clearBreakPoint(%d)", |
| 268 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 270 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | static void EnableScriptBreakPointFromJS(v8::Isolate* isolate, |
| 275 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 277 | SNPrintF(buffer, |
| 278 | "debug.Debug.enableScriptBreakPoint(%d)", |
| 279 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 281 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | static void DisableScriptBreakPointFromJS(v8::Isolate* isolate, |
| 286 | int break_point_number) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 288 | SNPrintF(buffer, |
| 289 | "debug.Debug.disableScriptBreakPoint(%d)", |
| 290 | break_point_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 291 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 292 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | static void ChangeScriptBreakPointConditionFromJS(v8::Isolate* isolate, |
| 297 | int break_point_number, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | const char* condition) { |
| 299 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 300 | SNPrintF(buffer, |
| 301 | "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")", |
| 302 | break_point_number, condition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 303 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 304 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 308 | static void ChangeScriptBreakPointIgnoreCountFromJS(v8::Isolate* isolate, |
| 309 | int break_point_number, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | int ignoreCount) { |
| 311 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 312 | SNPrintF(buffer, |
| 313 | "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)", |
| 314 | break_point_number, ignoreCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 316 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | // Change break on exception. |
| 321 | static void ChangeBreakOnException(bool caught, bool uncaught) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 323 | debug->ChangeBreakOnException(v8::internal::BreakException, caught); |
| 324 | debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | // Change break on exception using the global Debug object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 329 | static void ChangeBreakOnExceptionFromJS(v8::Isolate* isolate, bool caught, |
| 330 | bool uncaught) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 331 | if (caught) { |
| 332 | v8::Script::Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | v8::String::NewFromUtf8(isolate, "debug.Debug.setBreakOnException()")) |
| 334 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | } else { |
| 336 | v8::Script::Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 337 | v8::String::NewFromUtf8(isolate, "debug.Debug.clearBreakOnException()")) |
| 338 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | } |
| 340 | if (uncaught) { |
| 341 | v8::Script::Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 342 | v8::String::NewFromUtf8( |
| 343 | isolate, "debug.Debug.setBreakOnUncaughtException()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | } else { |
| 345 | v8::Script::Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | v8::String::NewFromUtf8( |
| 347 | isolate, "debug.Debug.clearBreakOnUncaughtException()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | // Prepare to step to next break location. |
| 353 | static void PrepareStep(StepAction step_action) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 354 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
| 355 | debug->PrepareStep(step_action, 1, StackFrame::NO_ID); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | // This function is in namespace v8::internal to be friend with class |
| 360 | // v8::internal::Debug. |
| 361 | namespace v8 { |
| 362 | namespace internal { |
| 363 | |
| 364 | // Collect the currently debugged functions. |
| 365 | Handle<FixedArray> GetDebuggedFunctions() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 366 | Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 367 | |
| 368 | v8::internal::DebugInfoListNode* node = debug->debug_info_list_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | |
| 370 | // Find the number of debugged functions. |
| 371 | int count = 0; |
| 372 | while (node) { |
| 373 | count++; |
| 374 | node = node->next(); |
| 375 | } |
| 376 | |
| 377 | // Allocate array for the debugged functions |
| 378 | Handle<FixedArray> debugged_functions = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 379 | CcTest::i_isolate()->factory()->NewFixedArray(count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | |
| 381 | // Run through the debug info objects and collect all functions. |
| 382 | count = 0; |
| 383 | while (node) { |
| 384 | debugged_functions->set(count++, *node->debug_info()); |
| 385 | node = node->next(); |
| 386 | } |
| 387 | |
| 388 | return debugged_functions; |
| 389 | } |
| 390 | |
| 391 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | // Check that the debugger has been fully unloaded. |
| 393 | void CheckDebuggerUnloaded(bool check_functions) { |
| 394 | // Check that the debugger context is cleared and that there is no debug |
| 395 | // information stored for the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | CHECK(CcTest::i_isolate()->debug()->debug_context().is_null()); |
| 397 | CHECK_EQ(NULL, CcTest::i_isolate()->debug()->debug_info_list_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 398 | |
| 399 | // Collect garbage to ensure weak handles are cleared. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 400 | CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 401 | CcTest::heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | |
| 403 | // 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] | 404 | HeapIterator iterator(CcTest::heap()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 405 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | CHECK(!obj->IsDebugInfo()); |
| 407 | CHECK(!obj->IsBreakPointInfo()); |
| 408 | |
| 409 | // If deep check of functions is requested check that no debug break code |
| 410 | // is left in all functions. |
| 411 | if (check_functions) { |
| 412 | if (obj->IsJSFunction()) { |
| 413 | JSFunction* fun = JSFunction::cast(obj); |
| 414 | for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) { |
| 415 | RelocInfo::Mode rmode = it.rinfo()->rmode(); |
| 416 | if (RelocInfo::IsCodeTarget(rmode)) { |
| 417 | CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address())); |
| 418 | } else if (RelocInfo::IsJSReturn(rmode)) { |
| 419 | CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo())); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | } } // namespace v8::internal |
| 429 | |
| 430 | |
| 431 | // Check that the debugger has been fully unloaded. |
| 432 | static void CheckDebuggerUnloaded(bool check_functions = false) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 433 | // Let debugger to unload itself synchronously |
| 434 | v8::Debug::ProcessDebugMessages(); |
| 435 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | v8::internal::CheckDebuggerUnloaded(check_functions); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | // Inherit from BreakLocationIterator to get access to protected parts for |
| 441 | // testing. |
| 442 | class TestBreakLocationIterator: public v8::internal::BreakLocationIterator { |
| 443 | public: |
| 444 | explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info) |
| 445 | : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {} |
| 446 | v8::internal::RelocIterator* it() { return reloc_iterator_; } |
| 447 | v8::internal::RelocIterator* it_original() { |
| 448 | return reloc_iterator_original_; |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | |
| 453 | // Compile a function, set a break point and check that the call at the break |
| 454 | // location in the code is the expected debug_break function. |
| 455 | void CheckDebugBreakFunction(DebugLocalContext* env, |
| 456 | const char* source, const char* name, |
| 457 | int position, v8::internal::RelocInfo::Mode mode, |
| 458 | Code* debug_break) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 459 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 460 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 461 | // Create function and set the break point. |
| 462 | Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle( |
| 463 | *CompileFunction(env, source, name)); |
| 464 | int bp = SetBreakPoint(fun, position); |
| 465 | |
| 466 | // Check that the debug break function is as expected. |
| 467 | Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); |
| 468 | CHECK(Debug::HasDebugInfo(shared)); |
| 469 | TestBreakLocationIterator it1(Debug::GetDebugInfo(shared)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 470 | it1.FindBreakLocationFromPosition(position, v8::internal::STATEMENT_ALIGNED); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 471 | v8::internal::RelocInfo::Mode actual_mode = it1.it()->rinfo()->rmode(); |
| 472 | if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { |
| 473 | actual_mode = v8::internal::RelocInfo::CODE_TARGET; |
| 474 | } |
| 475 | CHECK_EQ(mode, actual_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | if (mode != v8::internal::RelocInfo::JS_RETURN) { |
| 477 | CHECK_EQ(debug_break, |
| 478 | Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address())); |
| 479 | } else { |
| 480 | CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo())); |
| 481 | } |
| 482 | |
| 483 | // Clear the break point and check that the debug break function is no longer |
| 484 | // there |
| 485 | ClearBreakPoint(bp); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 486 | CHECK(!debug->HasDebugInfo(shared)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 487 | CHECK(debug->EnsureDebugInfo(shared, fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | TestBreakLocationIterator it2(Debug::GetDebugInfo(shared)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | it2.FindBreakLocationFromPosition(position, v8::internal::STATEMENT_ALIGNED); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 490 | actual_mode = it2.it()->rinfo()->rmode(); |
| 491 | if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { |
| 492 | actual_mode = v8::internal::RelocInfo::CODE_TARGET; |
| 493 | } |
| 494 | CHECK_EQ(mode, actual_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | if (mode == v8::internal::RelocInfo::JS_RETURN) { |
| 496 | CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo())); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | // --- D e b u g E v e n t H a n d l e r s |
| 502 | // --- |
| 503 | // --- The different tests uses a number of debug event handlers. |
| 504 | // --- |
| 505 | |
| 506 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 507 | // Source for the JavaScript function which picks out the function |
| 508 | // name of a frame. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 509 | const char* frame_function_name_source = |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 510 | "function frame_function_name(exec_state, frame_number) {" |
| 511 | " return exec_state.frame(frame_number).func().name();" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | "}"; |
| 513 | v8::Local<v8::Function> frame_function_name; |
| 514 | |
| 515 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 516 | // Source for the JavaScript function which pick out the name of the |
| 517 | // first argument of a frame. |
| 518 | const char* frame_argument_name_source = |
| 519 | "function frame_argument_name(exec_state, frame_number) {" |
| 520 | " return exec_state.frame(frame_number).argumentName(0);" |
| 521 | "}"; |
| 522 | v8::Local<v8::Function> frame_argument_name; |
| 523 | |
| 524 | |
| 525 | // Source for the JavaScript function which pick out the value of the |
| 526 | // first argument of a frame. |
| 527 | const char* frame_argument_value_source = |
| 528 | "function frame_argument_value(exec_state, frame_number) {" |
| 529 | " return exec_state.frame(frame_number).argumentValue(0).value_;" |
| 530 | "}"; |
| 531 | v8::Local<v8::Function> frame_argument_value; |
| 532 | |
| 533 | |
| 534 | // Source for the JavaScript function which pick out the name of the |
| 535 | // first argument of a frame. |
| 536 | const char* frame_local_name_source = |
| 537 | "function frame_local_name(exec_state, frame_number) {" |
| 538 | " return exec_state.frame(frame_number).localName(0);" |
| 539 | "}"; |
| 540 | v8::Local<v8::Function> frame_local_name; |
| 541 | |
| 542 | |
| 543 | // Source for the JavaScript function which pick out the value of the |
| 544 | // first argument of a frame. |
| 545 | const char* frame_local_value_source = |
| 546 | "function frame_local_value(exec_state, frame_number) {" |
| 547 | " return exec_state.frame(frame_number).localValue(0).value_;" |
| 548 | "}"; |
| 549 | v8::Local<v8::Function> frame_local_value; |
| 550 | |
| 551 | |
| 552 | // 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] | 553 | // top frame. |
| 554 | const char* frame_source_line_source = |
| 555 | "function frame_source_line(exec_state) {" |
| 556 | " return exec_state.frame(0).sourceLine();" |
| 557 | "}"; |
| 558 | v8::Local<v8::Function> frame_source_line; |
| 559 | |
| 560 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 561 | // 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] | 562 | // top frame. |
| 563 | const char* frame_source_column_source = |
| 564 | "function frame_source_column(exec_state) {" |
| 565 | " return exec_state.frame(0).sourceColumn();" |
| 566 | "}"; |
| 567 | v8::Local<v8::Function> frame_source_column; |
| 568 | |
| 569 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 570 | // 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] | 571 | // top frame. |
| 572 | const char* frame_script_name_source = |
| 573 | "function frame_script_name(exec_state) {" |
| 574 | " return exec_state.frame(0).func().script().name();" |
| 575 | "}"; |
| 576 | v8::Local<v8::Function> frame_script_name; |
| 577 | |
| 578 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 579 | // Source for the JavaScript function which returns the number of frames. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 580 | static const char* frame_count_source = |
| 581 | "function frame_count(exec_state) {" |
| 582 | " return exec_state.frameCount();" |
| 583 | "}"; |
| 584 | v8::Handle<v8::Function> frame_count; |
| 585 | |
| 586 | |
| 587 | // Global variable to store the last function hit - used by some tests. |
| 588 | char last_function_hit[80]; |
| 589 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 590 | // 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] | 591 | char last_script_name_hit[80]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 592 | |
| 593 | // Global variables to store the last source position - used by some tests. |
| 594 | int last_source_line = -1; |
| 595 | int last_source_column = -1; |
| 596 | |
| 597 | // Debug event handler which counts the break points which have been hit. |
| 598 | int break_point_hit_count = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 599 | int break_point_hit_count_deoptimize = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 600 | static void DebugEventBreakPointHitCount( |
| 601 | const v8::Debug::EventDetails& event_details) { |
| 602 | v8::DebugEvent event = event_details.GetEvent(); |
| 603 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
| 604 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 605 | Debug* debug = isolate->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 606 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 607 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 608 | |
| 609 | // Count the number of breaks. |
| 610 | if (event == v8::Break) { |
| 611 | break_point_hit_count++; |
| 612 | if (!frame_function_name.IsEmpty()) { |
| 613 | // Get the name of the function. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 614 | const int argc = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 615 | v8::Handle<v8::Value> argv[argc] = { |
| 616 | exec_state, v8::Integer::New(CcTest::isolate(), 0) |
| 617 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 618 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 619 | argc, argv); |
| 620 | if (result->IsUndefined()) { |
| 621 | last_function_hit[0] = '\0'; |
| 622 | } else { |
| 623 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 624 | v8::Handle<v8::String> function_name(result.As<v8::String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 625 | function_name->WriteUtf8(last_function_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | if (!frame_source_line.IsEmpty()) { |
| 630 | // Get the source line. |
| 631 | const int argc = 1; |
| 632 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 633 | v8::Handle<v8::Value> result = frame_source_line->Call(exec_state, |
| 634 | argc, argv); |
| 635 | CHECK(result->IsNumber()); |
| 636 | last_source_line = result->Int32Value(); |
| 637 | } |
| 638 | |
| 639 | if (!frame_source_column.IsEmpty()) { |
| 640 | // Get the source column. |
| 641 | const int argc = 1; |
| 642 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 643 | v8::Handle<v8::Value> result = frame_source_column->Call(exec_state, |
| 644 | argc, argv); |
| 645 | CHECK(result->IsNumber()); |
| 646 | last_source_column = result->Int32Value(); |
| 647 | } |
| 648 | |
| 649 | if (!frame_script_name.IsEmpty()) { |
| 650 | // Get the script name of the function script. |
| 651 | const int argc = 1; |
| 652 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 653 | v8::Handle<v8::Value> result = frame_script_name->Call(exec_state, |
| 654 | argc, argv); |
| 655 | if (result->IsUndefined()) { |
| 656 | last_script_name_hit[0] = '\0'; |
| 657 | } else { |
| 658 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 659 | v8::Handle<v8::String> script_name(result.As<v8::String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 660 | script_name->WriteUtf8(last_script_name_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 663 | |
| 664 | // Perform a full deoptimization when the specified number of |
| 665 | // breaks have been hit. |
| 666 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 667 | i::Deoptimizer::DeoptimizeAll(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 668 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
| 672 | |
| 673 | // Debug event handler which counts a number of events and collects the stack |
| 674 | // height if there is a function compiled for that. |
| 675 | int exception_hit_count = 0; |
| 676 | int uncaught_exception_hit_count = 0; |
| 677 | int last_js_stack_height = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 678 | v8::Handle<v8::Function> debug_event_listener_callback; |
| 679 | int debug_event_listener_callback_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 680 | |
| 681 | static void DebugEventCounterClear() { |
| 682 | break_point_hit_count = 0; |
| 683 | exception_hit_count = 0; |
| 684 | uncaught_exception_hit_count = 0; |
| 685 | } |
| 686 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 687 | static void DebugEventCounter( |
| 688 | const v8::Debug::EventDetails& event_details) { |
| 689 | v8::DebugEvent event = event_details.GetEvent(); |
| 690 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
| 691 | v8::Handle<v8::Object> event_data = event_details.GetEventData(); |
| 692 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 693 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 694 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 695 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 696 | |
| 697 | // Count the number of breaks. |
| 698 | if (event == v8::Break) { |
| 699 | break_point_hit_count++; |
| 700 | } else if (event == v8::Exception) { |
| 701 | exception_hit_count++; |
| 702 | |
| 703 | // Check whether the exception was uncaught. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 704 | v8::Local<v8::String> fun_name = |
| 705 | v8::String::NewFromUtf8(CcTest::isolate(), "uncaught"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 706 | v8::Local<v8::Function> fun = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 707 | v8::Local<v8::Function>::Cast(event_data->Get(fun_name)); |
| 708 | v8::Local<v8::Value> result = fun->Call(event_data, 0, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | if (result->IsTrue()) { |
| 710 | uncaught_exception_hit_count++; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // Collect the JavsScript stack height if the function frame_count is |
| 715 | // compiled. |
| 716 | if (!frame_count.IsEmpty()) { |
| 717 | static const int kArgc = 1; |
| 718 | v8::Handle<v8::Value> argv[kArgc] = { exec_state }; |
| 719 | // Using exec_state as receiver is just to have a receiver. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 720 | v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | last_js_stack_height = result->Int32Value(); |
| 722 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 723 | |
| 724 | // Run callback from DebugEventListener and check the result. |
| 725 | if (!debug_event_listener_callback.IsEmpty()) { |
| 726 | v8::Handle<v8::Value> result = |
| 727 | debug_event_listener_callback->Call(event_data, 0, NULL); |
| 728 | CHECK(!result.IsEmpty()); |
| 729 | CHECK_EQ(debug_event_listener_callback_result, result->Int32Value()); |
| 730 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | // Debug event handler which evaluates a number of expressions when a break |
| 735 | // point is hit. Each evaluated expression is compared with an expected value. |
| 736 | // For this debug event handler to work the following two global varaibles |
| 737 | // must be initialized. |
| 738 | // checks: An array of expressions and expected results |
| 739 | // evaluate_check_function: A JavaScript function (see below) |
| 740 | |
| 741 | // Structure for holding checks to do. |
| 742 | struct EvaluateCheck { |
| 743 | const char* expr; // An expression to evaluate when a break point is hit. |
| 744 | v8::Handle<v8::Value> expected; // The expected result. |
| 745 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 746 | |
| 747 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 748 | // Array of checks to do. |
| 749 | struct EvaluateCheck* checks = NULL; |
| 750 | // Source for The JavaScript function which can do the evaluation when a break |
| 751 | // point is hit. |
| 752 | const char* evaluate_check_source = |
| 753 | "function evaluate_check(exec_state, expr, expected) {" |
| 754 | " return exec_state.frame(0).evaluate(expr).value() === expected;" |
| 755 | "}"; |
| 756 | v8::Local<v8::Function> evaluate_check_function; |
| 757 | |
| 758 | // The actual debug event described by the longer comment above. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 759 | static void DebugEventEvaluate( |
| 760 | const v8::Debug::EventDetails& event_details) { |
| 761 | v8::DebugEvent event = event_details.GetEvent(); |
| 762 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
| 763 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 764 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 765 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 766 | |
| 767 | if (event == v8::Break) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 768 | break_point_hit_count++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 769 | for (int i = 0; checks[i].expr != NULL; i++) { |
| 770 | const int argc = 3; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 771 | v8::Handle<v8::Value> argv[argc] = { |
| 772 | exec_state, |
| 773 | v8::String::NewFromUtf8(CcTest::isolate(), checks[i].expr), |
| 774 | checks[i].expected}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 775 | v8::Handle<v8::Value> result = |
| 776 | evaluate_check_function->Call(exec_state, argc, argv); |
| 777 | if (!result->IsTrue()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 778 | v8::String::Utf8Value utf8(checks[i].expected); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 779 | V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *utf8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | |
| 786 | // This debug event listener removes a breakpoint in a function |
| 787 | int debug_event_remove_break_point = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 788 | static void DebugEventRemoveBreakPoint( |
| 789 | const v8::Debug::EventDetails& event_details) { |
| 790 | v8::DebugEvent event = event_details.GetEvent(); |
| 791 | v8::Handle<v8::Value> data = event_details.GetCallbackData(); |
| 792 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 793 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 794 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 795 | |
| 796 | if (event == v8::Break) { |
| 797 | break_point_hit_count++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 798 | CHECK(data->IsFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | ClearBreakPoint(debug_event_remove_break_point); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | |
| 804 | // Debug event handler which counts break points hit and performs a step |
| 805 | // afterwards. |
| 806 | StepAction step_action = StepIn; // Step action to perform when stepping. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 807 | static void DebugEventStep( |
| 808 | const v8::Debug::EventDetails& event_details) { |
| 809 | v8::DebugEvent event = event_details.GetEvent(); |
| 810 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 811 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 812 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | |
| 814 | if (event == v8::Break) { |
| 815 | break_point_hit_count++; |
| 816 | PrepareStep(step_action); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | |
| 821 | // Debug event handler which counts break points hit and performs a step |
| 822 | // afterwards. For each call the expected function is checked. |
| 823 | // For this debug event handler to work the following two global varaibles |
| 824 | // must be initialized. |
| 825 | // expected_step_sequence: An array of the expected function call sequence. |
| 826 | // frame_function_name: A JavaScript function (see below). |
| 827 | |
| 828 | // String containing the expected function call sequence. Note: this only works |
| 829 | // if functions have name length of one. |
| 830 | const char* expected_step_sequence = NULL; |
| 831 | |
| 832 | // The actual debug event described by the longer comment above. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 833 | static void DebugEventStepSequence( |
| 834 | const v8::Debug::EventDetails& event_details) { |
| 835 | v8::DebugEvent event = event_details.GetEvent(); |
| 836 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
| 837 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 838 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 839 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 840 | |
| 841 | if (event == v8::Break || event == v8::Exception) { |
| 842 | // Check that the current function is the expected. |
| 843 | CHECK(break_point_hit_count < |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 844 | StrLength(expected_step_sequence)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 845 | const int argc = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 846 | v8::Handle<v8::Value> argv[argc] = { |
| 847 | exec_state, v8::Integer::New(CcTest::isolate(), 0) |
| 848 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 849 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 850 | argc, argv); |
| 851 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 852 | v8::String::Utf8Value function_name(result->ToString(CcTest::isolate())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 853 | CHECK_EQ(1, StrLength(*function_name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 854 | CHECK_EQ((*function_name)[0], |
| 855 | expected_step_sequence[break_point_hit_count]); |
| 856 | |
| 857 | // Perform step. |
| 858 | break_point_hit_count++; |
| 859 | PrepareStep(step_action); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | |
| 864 | // Debug event handler which performs a garbage collection. |
| 865 | static void DebugEventBreakPointCollectGarbage( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 866 | const v8::Debug::EventDetails& event_details) { |
| 867 | v8::DebugEvent event = event_details.GetEvent(); |
| 868 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 870 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 871 | |
| 872 | // Perform a garbage collection when break point is hit and continue. Based |
| 873 | // on the number of break points hit either scavenge or mark compact |
| 874 | // collector is used. |
| 875 | if (event == v8::Break) { |
| 876 | break_point_hit_count++; |
| 877 | if (break_point_hit_count % 2 == 0) { |
| 878 | // Scavenge. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 879 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 880 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 881 | // Mark sweep compact. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 882 | CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | // Debug event handler which re-issues a debug break and calls the garbage |
| 889 | // collector to have the heap verified. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 890 | static void DebugEventBreak( |
| 891 | const v8::Debug::EventDetails& event_details) { |
| 892 | v8::DebugEvent event = event_details.GetEvent(); |
| 893 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 895 | CHECK_NE(debug->break_id(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 896 | |
| 897 | if (event == v8::Break) { |
| 898 | // Count the number of breaks. |
| 899 | break_point_hit_count++; |
| 900 | |
| 901 | // Run the garbage collector to enforce heap verification if option |
| 902 | // --verify-heap is set. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 903 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 904 | |
| 905 | // 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] | 906 | v8::Debug::DebugBreak(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 911 | // Debug event handler which re-issues a debug break until a limit has been |
| 912 | // reached. |
| 913 | int max_break_point_hit_count = 0; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 914 | bool terminate_after_max_break_point_hit = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 915 | static void DebugEventBreakMax( |
| 916 | const v8::Debug::EventDetails& event_details) { |
| 917 | v8::DebugEvent event = event_details.GetEvent(); |
| 918 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
| 919 | v8::Isolate* v8_isolate = CcTest::isolate(); |
| 920 | v8::internal::Isolate* isolate = CcTest::i_isolate(); |
| 921 | v8::internal::Debug* debug = isolate->debug(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 922 | // When hitting a debug event listener there must be a break set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 923 | CHECK_NE(debug->break_id(), 0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 924 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 925 | if (event == v8::Break) { |
| 926 | if (break_point_hit_count < max_break_point_hit_count) { |
| 927 | // Count the number of breaks. |
| 928 | break_point_hit_count++; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 929 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 930 | // Collect the JavsScript stack height if the function frame_count is |
| 931 | // compiled. |
| 932 | if (!frame_count.IsEmpty()) { |
| 933 | static const int kArgc = 1; |
| 934 | v8::Handle<v8::Value> argv[kArgc] = { exec_state }; |
| 935 | // Using exec_state as receiver is just to have a receiver. |
| 936 | v8::Handle<v8::Value> result = |
| 937 | frame_count->Call(exec_state, kArgc, argv); |
| 938 | last_js_stack_height = result->Int32Value(); |
| 939 | } |
| 940 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 941 | // 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] | 942 | v8::Debug::DebugBreak(v8_isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 943 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 944 | } else if (terminate_after_max_break_point_hit) { |
| 945 | // Terminate execution after the last break if requested. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 946 | v8::V8::TerminateExecution(v8_isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 947 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 948 | |
| 949 | // Perform a full deoptimization when the specified number of |
| 950 | // breaks have been hit. |
| 951 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | i::Deoptimizer::DeoptimizeAll(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 953 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | // --- M e s s a g e C a l l b a c k |
| 959 | |
| 960 | |
| 961 | // Message callback which counts the number of messages. |
| 962 | int message_callback_count = 0; |
| 963 | |
| 964 | static void MessageCallbackCountClear() { |
| 965 | message_callback_count = 0; |
| 966 | } |
| 967 | |
| 968 | static void MessageCallbackCount(v8::Handle<v8::Message> message, |
| 969 | v8::Handle<v8::Value> data) { |
| 970 | message_callback_count++; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | // --- T h e A c t u a l T e s t s |
| 975 | |
| 976 | |
| 977 | // Test that the debug break function is the expected one for different kinds |
| 978 | // of break locations. |
| 979 | TEST(DebugStub) { |
| 980 | using ::v8::internal::Builtins; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 981 | using ::v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 982 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 983 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 984 | |
| 985 | CheckDebugBreakFunction(&env, |
| 986 | "function f1(){}", "f1", |
| 987 | 0, |
| 988 | v8::internal::RelocInfo::JS_RETURN, |
| 989 | NULL); |
| 990 | CheckDebugBreakFunction(&env, |
| 991 | "function f2(){x=1;}", "f2", |
| 992 | 0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 993 | v8::internal::RelocInfo::CODE_TARGET, |
| 994 | CcTest::i_isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 995 | Builtins::kStoreIC_DebugBreak)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 996 | CheckDebugBreakFunction(&env, |
| 997 | "function f3(){var a=x;}", "f3", |
| 998 | 0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 999 | v8::internal::RelocInfo::CODE_TARGET, |
| 1000 | CcTest::i_isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1001 | Builtins::kLoadIC_DebugBreak)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1002 | |
| 1003 | // TODO(1240753): Make the test architecture independent or split |
| 1004 | // parts of the debugger into architecture dependent files. This |
| 1005 | // part currently disabled as it is not portable between IA32/ARM. |
| 1006 | // Currently on ICs for keyed store/load on ARM. |
| 1007 | #if !defined (__arm__) && !defined(__thumb__) |
| 1008 | CheckDebugBreakFunction( |
| 1009 | &env, |
| 1010 | "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}", |
| 1011 | "f4", |
| 1012 | 0, |
| 1013 | v8::internal::RelocInfo::CODE_TARGET, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1014 | CcTest::i_isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1015 | Builtins::kKeyedStoreIC_DebugBreak)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | CheckDebugBreakFunction( |
| 1017 | &env, |
| 1018 | "function f5(){var index='propertyName'; var a={}; return a[index];}", |
| 1019 | "f5", |
| 1020 | 0, |
| 1021 | v8::internal::RelocInfo::CODE_TARGET, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1022 | CcTest::i_isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1023 | Builtins::kKeyedLoadIC_DebugBreak)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1024 | #endif |
| 1025 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1026 | CheckDebugBreakFunction( |
| 1027 | &env, |
| 1028 | "function f6(a){return a==null;}", |
| 1029 | "f6", |
| 1030 | 0, |
| 1031 | v8::internal::RelocInfo::CODE_TARGET, |
| 1032 | CcTest::i_isolate()->builtins()->builtin( |
| 1033 | Builtins::kCompareNilIC_DebugBreak)); |
| 1034 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1035 | // Check the debug break code stubs for call ICs with different number of |
| 1036 | // parameters. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1037 | // TODO(verwaest): XXX update test. |
| 1038 | // Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0); |
| 1039 | // Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1); |
| 1040 | // Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1041 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1042 | // CheckDebugBreakFunction(&env, |
| 1043 | // "function f4_0(){x();}", "f4_0", |
| 1044 | // 0, |
| 1045 | // v8::internal::RelocInfo::CODE_TARGET, |
| 1046 | // *debug_break_0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1047 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1048 | // CheckDebugBreakFunction(&env, |
| 1049 | // "function f4_1(){x(1);}", "f4_1", |
| 1050 | // 0, |
| 1051 | // v8::internal::RelocInfo::CODE_TARGET, |
| 1052 | // *debug_break_1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1053 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1054 | // CheckDebugBreakFunction(&env, |
| 1055 | // "function f4_4(){x(1,2,3,4);}", "f4_4", |
| 1056 | // 0, |
| 1057 | // v8::internal::RelocInfo::CODE_TARGET, |
| 1058 | // *debug_break_4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | // Test that the debug info in the VM is in sync with the functions being |
| 1063 | // debugged. |
| 1064 | TEST(DebugInfo) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1065 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1066 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1067 | // Create a couple of functions for the test. |
| 1068 | v8::Local<v8::Function> foo = |
| 1069 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1070 | v8::Local<v8::Function> bar = |
| 1071 | CompileFunction(&env, "function bar(){}", "bar"); |
| 1072 | // Initially no functions are debugged. |
| 1073 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1074 | CHECK(!HasDebugInfo(foo)); |
| 1075 | CHECK(!HasDebugInfo(bar)); |
| 1076 | // One function (foo) is debugged. |
| 1077 | int bp1 = SetBreakPoint(foo, 0); |
| 1078 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1079 | CHECK(HasDebugInfo(foo)); |
| 1080 | CHECK(!HasDebugInfo(bar)); |
| 1081 | // Two functions are debugged. |
| 1082 | int bp2 = SetBreakPoint(bar, 0); |
| 1083 | CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); |
| 1084 | CHECK(HasDebugInfo(foo)); |
| 1085 | CHECK(HasDebugInfo(bar)); |
| 1086 | // One function (bar) is debugged. |
| 1087 | ClearBreakPoint(bp1); |
| 1088 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1089 | CHECK(!HasDebugInfo(foo)); |
| 1090 | CHECK(HasDebugInfo(bar)); |
| 1091 | // No functions are debugged. |
| 1092 | ClearBreakPoint(bp2); |
| 1093 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1094 | CHECK(!HasDebugInfo(foo)); |
| 1095 | CHECK(!HasDebugInfo(bar)); |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | // Test that a break point can be set at an IC store location. |
| 1100 | TEST(BreakPointICStore) { |
| 1101 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1102 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1103 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1104 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1105 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 1106 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 1107 | "function foo(){bar=0;}"))->Run(); |
| 1108 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1109 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Run without breakpoints. |
| 1112 | foo->Call(env->Global(), 0, NULL); |
| 1113 | CHECK_EQ(0, break_point_hit_count); |
| 1114 | |
| 1115 | // Run with breakpoint |
| 1116 | int bp = SetBreakPoint(foo, 0); |
| 1117 | foo->Call(env->Global(), 0, NULL); |
| 1118 | CHECK_EQ(1, break_point_hit_count); |
| 1119 | foo->Call(env->Global(), 0, NULL); |
| 1120 | CHECK_EQ(2, break_point_hit_count); |
| 1121 | |
| 1122 | // Run without breakpoints. |
| 1123 | ClearBreakPoint(bp); |
| 1124 | foo->Call(env->Global(), 0, NULL); |
| 1125 | CHECK_EQ(2, break_point_hit_count); |
| 1126 | |
| 1127 | v8::Debug::SetDebugEventListener(NULL); |
| 1128 | CheckDebuggerUnloaded(); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | // Test that a break point can be set at an IC load location. |
| 1133 | TEST(BreakPointICLoad) { |
| 1134 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1136 | v8::HandleScope scope(env->GetIsolate()); |
| 1137 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 1138 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "bar=1")) |
| 1139 | ->Run(); |
| 1140 | v8::Script::Compile( |
| 1141 | v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){var x=bar;}")) |
| 1142 | ->Run(); |
| 1143 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1144 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1145 | |
| 1146 | // Run without breakpoints. |
| 1147 | foo->Call(env->Global(), 0, NULL); |
| 1148 | CHECK_EQ(0, break_point_hit_count); |
| 1149 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1150 | // Run with breakpoint. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1151 | int bp = SetBreakPoint(foo, 0); |
| 1152 | foo->Call(env->Global(), 0, NULL); |
| 1153 | CHECK_EQ(1, break_point_hit_count); |
| 1154 | foo->Call(env->Global(), 0, NULL); |
| 1155 | CHECK_EQ(2, break_point_hit_count); |
| 1156 | |
| 1157 | // Run without breakpoints. |
| 1158 | ClearBreakPoint(bp); |
| 1159 | foo->Call(env->Global(), 0, NULL); |
| 1160 | CHECK_EQ(2, break_point_hit_count); |
| 1161 | |
| 1162 | v8::Debug::SetDebugEventListener(NULL); |
| 1163 | CheckDebuggerUnloaded(); |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | // Test that a break point can be set at an IC call location. |
| 1168 | TEST(BreakPointICCall) { |
| 1169 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1170 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1171 | v8::HandleScope scope(env->GetIsolate()); |
| 1172 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 1173 | v8::Script::Compile( |
| 1174 | v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){}"))->Run(); |
| 1175 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 1176 | "function foo(){bar();}"))->Run(); |
| 1177 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1178 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Run without breakpoints. |
| 1181 | foo->Call(env->Global(), 0, NULL); |
| 1182 | CHECK_EQ(0, break_point_hit_count); |
| 1183 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1184 | // Run with breakpoint |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1185 | int bp = SetBreakPoint(foo, 0); |
| 1186 | foo->Call(env->Global(), 0, NULL); |
| 1187 | CHECK_EQ(1, break_point_hit_count); |
| 1188 | foo->Call(env->Global(), 0, NULL); |
| 1189 | CHECK_EQ(2, break_point_hit_count); |
| 1190 | |
| 1191 | // Run without breakpoints. |
| 1192 | ClearBreakPoint(bp); |
| 1193 | foo->Call(env->Global(), 0, NULL); |
| 1194 | CHECK_EQ(2, break_point_hit_count); |
| 1195 | |
| 1196 | v8::Debug::SetDebugEventListener(NULL); |
| 1197 | CheckDebuggerUnloaded(); |
| 1198 | } |
| 1199 | |
| 1200 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1201 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1202 | TEST(BreakPointICCallWithGC) { |
| 1203 | break_point_hit_count = 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1204 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1205 | v8::HandleScope scope(env->GetIsolate()); |
| 1206 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage); |
| 1207 | v8::Script::Compile( |
| 1208 | v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){return 1;}")) |
| 1209 | ->Run(); |
| 1210 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 1211 | "function foo(){return bar();}")) |
| 1212 | ->Run(); |
| 1213 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1214 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1215 | |
| 1216 | // Run without breakpoints. |
| 1217 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1218 | CHECK_EQ(0, break_point_hit_count); |
| 1219 | |
| 1220 | // Run with breakpoint. |
| 1221 | int bp = SetBreakPoint(foo, 0); |
| 1222 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1223 | CHECK_EQ(1, break_point_hit_count); |
| 1224 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1225 | CHECK_EQ(2, break_point_hit_count); |
| 1226 | |
| 1227 | // Run without breakpoints. |
| 1228 | ClearBreakPoint(bp); |
| 1229 | foo->Call(env->Global(), 0, NULL); |
| 1230 | CHECK_EQ(2, break_point_hit_count); |
| 1231 | |
| 1232 | v8::Debug::SetDebugEventListener(NULL); |
| 1233 | CheckDebuggerUnloaded(); |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1238 | TEST(BreakPointConstructCallWithGC) { |
| 1239 | break_point_hit_count = 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1240 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1241 | v8::HandleScope scope(env->GetIsolate()); |
| 1242 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage); |
| 1243 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 1244 | "function bar(){ this.x = 1;}")) |
| 1245 | ->Run(); |
| 1246 | v8::Script::Compile( |
| 1247 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 1248 | "function foo(){return new bar(1).x;}"))->Run(); |
| 1249 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1250 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1251 | |
| 1252 | // Run without breakpoints. |
| 1253 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1254 | CHECK_EQ(0, break_point_hit_count); |
| 1255 | |
| 1256 | // Run with breakpoint. |
| 1257 | int bp = SetBreakPoint(foo, 0); |
| 1258 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1259 | CHECK_EQ(1, break_point_hit_count); |
| 1260 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1261 | CHECK_EQ(2, break_point_hit_count); |
| 1262 | |
| 1263 | // Run without breakpoints. |
| 1264 | ClearBreakPoint(bp); |
| 1265 | foo->Call(env->Global(), 0, NULL); |
| 1266 | CHECK_EQ(2, break_point_hit_count); |
| 1267 | |
| 1268 | v8::Debug::SetDebugEventListener(NULL); |
| 1269 | CheckDebuggerUnloaded(); |
| 1270 | } |
| 1271 | |
| 1272 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1273 | // Test that a break point can be set at a return store location. |
| 1274 | TEST(BreakPointReturn) { |
| 1275 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1276 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1277 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1278 | |
| 1279 | // Create a functions for checking the source line and column when hitting |
| 1280 | // a break point. |
| 1281 | frame_source_line = CompileFunction(&env, |
| 1282 | frame_source_line_source, |
| 1283 | "frame_source_line"); |
| 1284 | frame_source_column = CompileFunction(&env, |
| 1285 | frame_source_column_source, |
| 1286 | "frame_source_column"); |
| 1287 | |
| 1288 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1289 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 1290 | v8::Script::Compile( |
| 1291 | v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){}"))->Run(); |
| 1292 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 1293 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Run without breakpoints. |
| 1296 | foo->Call(env->Global(), 0, NULL); |
| 1297 | CHECK_EQ(0, break_point_hit_count); |
| 1298 | |
| 1299 | // Run with breakpoint |
| 1300 | int bp = SetBreakPoint(foo, 0); |
| 1301 | foo->Call(env->Global(), 0, NULL); |
| 1302 | CHECK_EQ(1, break_point_hit_count); |
| 1303 | CHECK_EQ(0, last_source_line); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1304 | CHECK_EQ(15, last_source_column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1305 | foo->Call(env->Global(), 0, NULL); |
| 1306 | CHECK_EQ(2, break_point_hit_count); |
| 1307 | CHECK_EQ(0, last_source_line); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1308 | CHECK_EQ(15, last_source_column); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Run without breakpoints. |
| 1311 | ClearBreakPoint(bp); |
| 1312 | foo->Call(env->Global(), 0, NULL); |
| 1313 | CHECK_EQ(2, break_point_hit_count); |
| 1314 | |
| 1315 | v8::Debug::SetDebugEventListener(NULL); |
| 1316 | CheckDebuggerUnloaded(); |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | static void CallWithBreakPoints(v8::Local<v8::Object> recv, |
| 1321 | v8::Local<v8::Function> f, |
| 1322 | int break_point_count, |
| 1323 | int call_count) { |
| 1324 | break_point_hit_count = 0; |
| 1325 | for (int i = 0; i < call_count; i++) { |
| 1326 | f->Call(recv, 0, NULL); |
| 1327 | CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); |
| 1328 | } |
| 1329 | } |
| 1330 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1331 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1332 | // Test GC during break point processing. |
| 1333 | TEST(GCDuringBreakPointProcessing) { |
| 1334 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1335 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1336 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1337 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1338 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1339 | v8::Local<v8::Function> foo; |
| 1340 | |
| 1341 | // Test IC store break point with garbage collection. |
| 1342 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1343 | SetBreakPoint(foo, 0); |
| 1344 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1345 | |
| 1346 | // Test IC load break point with garbage collection. |
| 1347 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1348 | SetBreakPoint(foo, 0); |
| 1349 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1350 | |
| 1351 | // Test IC call break point with garbage collection. |
| 1352 | foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); |
| 1353 | SetBreakPoint(foo, 0); |
| 1354 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1355 | |
| 1356 | // Test return break point with garbage collection. |
| 1357 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1358 | SetBreakPoint(foo, 0); |
| 1359 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1360 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1361 | // Test debug break slot break point with garbage collection. |
| 1362 | foo = CompileFunction(&env, "function foo(){var a;}", "foo"); |
| 1363 | SetBreakPoint(foo, 0); |
| 1364 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1365 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1366 | v8::Debug::SetDebugEventListener(NULL); |
| 1367 | CheckDebuggerUnloaded(); |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | // Call the function three times with different garbage collections in between |
| 1372 | // and make sure that the break point survives. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1373 | static void CallAndGC(v8::Local<v8::Object> recv, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1374 | v8::Local<v8::Function> f) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1375 | break_point_hit_count = 0; |
| 1376 | |
| 1377 | for (int i = 0; i < 3; i++) { |
| 1378 | // Call function. |
| 1379 | f->Call(recv, 0, NULL); |
| 1380 | CHECK_EQ(1 + i * 3, break_point_hit_count); |
| 1381 | |
| 1382 | // Scavenge and call function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1383 | CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1384 | f->Call(recv, 0, NULL); |
| 1385 | CHECK_EQ(2 + i * 3, break_point_hit_count); |
| 1386 | |
| 1387 | // Mark sweep (and perhaps compact) and call function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1388 | CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1389 | f->Call(recv, 0, NULL); |
| 1390 | CHECK_EQ(3 + i * 3, break_point_hit_count); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1395 | // Test that a break point can be set at a return store location. |
| 1396 | TEST(BreakPointSurviveGC) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1397 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1398 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1399 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1400 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1401 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1402 | v8::Local<v8::Function> foo; |
| 1403 | |
| 1404 | // Test IC store break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1405 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1406 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1407 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1408 | SetBreakPoint(foo, 0); |
| 1409 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1410 | CallAndGC(env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1411 | |
| 1412 | // Test IC load break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1413 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1414 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1415 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1416 | SetBreakPoint(foo, 0); |
| 1417 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1418 | CallAndGC(env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Test IC call break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1421 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1422 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1423 | foo = CompileFunction(&env, |
| 1424 | "function bar(){};function foo(){bar();}", |
| 1425 | "foo"); |
| 1426 | SetBreakPoint(foo, 0); |
| 1427 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1428 | CallAndGC(env->Global(), foo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1429 | |
| 1430 | // Test return break point with garbage collection. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1431 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1432 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1433 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1434 | SetBreakPoint(foo, 0); |
| 1435 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1436 | CallAndGC(env->Global(), foo); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1437 | |
| 1438 | // Test non IC break point with garbage collection. |
| 1439 | { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1440 | CompileFunction(&env, "function foo(){}", "foo"); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1441 | foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); |
| 1442 | SetBreakPoint(foo, 0); |
| 1443 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1444 | CallAndGC(env->Global(), foo); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1445 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1446 | |
| 1447 | v8::Debug::SetDebugEventListener(NULL); |
| 1448 | CheckDebuggerUnloaded(); |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | // Test that break points can be set using the global Debug object. |
| 1453 | TEST(BreakPointThroughJavaScript) { |
| 1454 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1456 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1457 | env.ExposeDebug(); |
| 1458 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1459 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 1460 | v8::Script::Compile( |
| 1461 | v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){}"))->Run(); |
| 1462 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 1463 | "function foo(){bar();bar();}")) |
| 1464 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1465 | // 012345678901234567890 |
| 1466 | // 1 2 |
| 1467 | // Break points are set at position 3 and 9 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1468 | v8::Local<v8::Script> foo = |
| 1469 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "foo()")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1470 | |
| 1471 | // Run without breakpoints. |
| 1472 | foo->Run(); |
| 1473 | CHECK_EQ(0, break_point_hit_count); |
| 1474 | |
| 1475 | // Run with one breakpoint |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1476 | int bp1 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1477 | foo->Run(); |
| 1478 | CHECK_EQ(1, break_point_hit_count); |
| 1479 | foo->Run(); |
| 1480 | CHECK_EQ(2, break_point_hit_count); |
| 1481 | |
| 1482 | // Run with two breakpoints |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1483 | int bp2 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 9); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1484 | foo->Run(); |
| 1485 | CHECK_EQ(4, break_point_hit_count); |
| 1486 | foo->Run(); |
| 1487 | CHECK_EQ(6, break_point_hit_count); |
| 1488 | |
| 1489 | // Run with one breakpoint |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1490 | ClearBreakPointFromJS(env->GetIsolate(), bp2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1491 | foo->Run(); |
| 1492 | CHECK_EQ(7, break_point_hit_count); |
| 1493 | foo->Run(); |
| 1494 | CHECK_EQ(8, break_point_hit_count); |
| 1495 | |
| 1496 | // Run without breakpoints. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1497 | ClearBreakPointFromJS(env->GetIsolate(), bp1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1498 | foo->Run(); |
| 1499 | CHECK_EQ(8, break_point_hit_count); |
| 1500 | |
| 1501 | v8::Debug::SetDebugEventListener(NULL); |
| 1502 | CheckDebuggerUnloaded(); |
| 1503 | |
| 1504 | // Make sure that the break point numbers are consecutive. |
| 1505 | CHECK_EQ(1, bp1); |
| 1506 | CHECK_EQ(2, bp2); |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | // Test that break points on scripts identified by name can be set using the |
| 1511 | // global Debug object. |
| 1512 | TEST(ScriptBreakPointByNameThroughJavaScript) { |
| 1513 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1515 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1516 | env.ExposeDebug(); |
| 1517 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1518 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1519 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1520 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 1521 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1522 | "function f() {\n" |
| 1523 | " function h() {\n" |
| 1524 | " a = 0; // line 2\n" |
| 1525 | " }\n" |
| 1526 | " b = 1; // line 4\n" |
| 1527 | " return h();\n" |
| 1528 | "}\n" |
| 1529 | "\n" |
| 1530 | "function g() {\n" |
| 1531 | " function h() {\n" |
| 1532 | " a = 0;\n" |
| 1533 | " }\n" |
| 1534 | " b = 2; // line 12\n" |
| 1535 | " h();\n" |
| 1536 | " b = 3; // line 14\n" |
| 1537 | " f(); // line 15\n" |
| 1538 | "}"); |
| 1539 | |
| 1540 | // Compile the script and get the two functions. |
| 1541 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1542 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1543 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1544 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 1545 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
| 1546 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
| 1547 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Call f and g without break points. |
| 1550 | break_point_hit_count = 0; |
| 1551 | f->Call(env->Global(), 0, NULL); |
| 1552 | CHECK_EQ(0, break_point_hit_count); |
| 1553 | g->Call(env->Global(), 0, NULL); |
| 1554 | CHECK_EQ(0, break_point_hit_count); |
| 1555 | |
| 1556 | // Call f and g with break point on line 12. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1557 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 12, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1558 | break_point_hit_count = 0; |
| 1559 | f->Call(env->Global(), 0, NULL); |
| 1560 | CHECK_EQ(0, break_point_hit_count); |
| 1561 | g->Call(env->Global(), 0, NULL); |
| 1562 | CHECK_EQ(1, break_point_hit_count); |
| 1563 | |
| 1564 | // Remove the break point again. |
| 1565 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1566 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1567 | f->Call(env->Global(), 0, NULL); |
| 1568 | CHECK_EQ(0, break_point_hit_count); |
| 1569 | g->Call(env->Global(), 0, NULL); |
| 1570 | CHECK_EQ(0, break_point_hit_count); |
| 1571 | |
| 1572 | // Call f and g with break point on line 2. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1573 | int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1574 | break_point_hit_count = 0; |
| 1575 | f->Call(env->Global(), 0, NULL); |
| 1576 | CHECK_EQ(1, break_point_hit_count); |
| 1577 | g->Call(env->Global(), 0, NULL); |
| 1578 | CHECK_EQ(2, break_point_hit_count); |
| 1579 | |
| 1580 | // 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] | 1581 | int sbp3 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 4, 0); |
| 1582 | int sbp4 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 12, 0); |
| 1583 | int sbp5 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 14, 0); |
| 1584 | int sbp6 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 15, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1585 | break_point_hit_count = 0; |
| 1586 | f->Call(env->Global(), 0, NULL); |
| 1587 | CHECK_EQ(2, break_point_hit_count); |
| 1588 | g->Call(env->Global(), 0, NULL); |
| 1589 | CHECK_EQ(7, break_point_hit_count); |
| 1590 | |
| 1591 | // Remove all the break points again. |
| 1592 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1593 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 1594 | ClearBreakPointFromJS(env->GetIsolate(), sbp3); |
| 1595 | ClearBreakPointFromJS(env->GetIsolate(), sbp4); |
| 1596 | ClearBreakPointFromJS(env->GetIsolate(), sbp5); |
| 1597 | ClearBreakPointFromJS(env->GetIsolate(), sbp6); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1598 | f->Call(env->Global(), 0, NULL); |
| 1599 | CHECK_EQ(0, break_point_hit_count); |
| 1600 | g->Call(env->Global(), 0, NULL); |
| 1601 | CHECK_EQ(0, break_point_hit_count); |
| 1602 | |
| 1603 | v8::Debug::SetDebugEventListener(NULL); |
| 1604 | CheckDebuggerUnloaded(); |
| 1605 | |
| 1606 | // Make sure that the break point numbers are consecutive. |
| 1607 | CHECK_EQ(1, sbp1); |
| 1608 | CHECK_EQ(2, sbp2); |
| 1609 | CHECK_EQ(3, sbp3); |
| 1610 | CHECK_EQ(4, sbp4); |
| 1611 | CHECK_EQ(5, sbp5); |
| 1612 | CHECK_EQ(6, sbp6); |
| 1613 | } |
| 1614 | |
| 1615 | |
| 1616 | TEST(ScriptBreakPointByIdThroughJavaScript) { |
| 1617 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1618 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1619 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1620 | env.ExposeDebug(); |
| 1621 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1622 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1623 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1624 | v8::Local<v8::String> source = v8::String::NewFromUtf8( |
| 1625 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1626 | "function f() {\n" |
| 1627 | " function h() {\n" |
| 1628 | " a = 0; // line 2\n" |
| 1629 | " }\n" |
| 1630 | " b = 1; // line 4\n" |
| 1631 | " return h();\n" |
| 1632 | "}\n" |
| 1633 | "\n" |
| 1634 | "function g() {\n" |
| 1635 | " function h() {\n" |
| 1636 | " a = 0;\n" |
| 1637 | " }\n" |
| 1638 | " b = 2; // line 12\n" |
| 1639 | " h();\n" |
| 1640 | " b = 3; // line 14\n" |
| 1641 | " f(); // line 15\n" |
| 1642 | "}"); |
| 1643 | |
| 1644 | // Compile the script and get the two functions. |
| 1645 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1646 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1647 | v8::Local<v8::Script> script = v8::Script::Compile(source, &origin); |
| 1648 | script->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1649 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 1650 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
| 1651 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
| 1652 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Get the script id knowing that internally it is a 32 integer. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1655 | int script_id = script->GetUnboundScript()->GetId(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1656 | |
| 1657 | // Call f and g without break points. |
| 1658 | break_point_hit_count = 0; |
| 1659 | f->Call(env->Global(), 0, NULL); |
| 1660 | CHECK_EQ(0, break_point_hit_count); |
| 1661 | g->Call(env->Global(), 0, NULL); |
| 1662 | CHECK_EQ(0, break_point_hit_count); |
| 1663 | |
| 1664 | // Call f and g with break point on line 12. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1665 | int sbp1 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1666 | break_point_hit_count = 0; |
| 1667 | f->Call(env->Global(), 0, NULL); |
| 1668 | CHECK_EQ(0, break_point_hit_count); |
| 1669 | g->Call(env->Global(), 0, NULL); |
| 1670 | CHECK_EQ(1, break_point_hit_count); |
| 1671 | |
| 1672 | // Remove the break point again. |
| 1673 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1674 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1675 | f->Call(env->Global(), 0, NULL); |
| 1676 | CHECK_EQ(0, break_point_hit_count); |
| 1677 | g->Call(env->Global(), 0, NULL); |
| 1678 | CHECK_EQ(0, break_point_hit_count); |
| 1679 | |
| 1680 | // Call f and g with break point on line 2. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1681 | int sbp2 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1682 | break_point_hit_count = 0; |
| 1683 | f->Call(env->Global(), 0, NULL); |
| 1684 | CHECK_EQ(1, break_point_hit_count); |
| 1685 | g->Call(env->Global(), 0, NULL); |
| 1686 | CHECK_EQ(2, break_point_hit_count); |
| 1687 | |
| 1688 | // 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] | 1689 | int sbp3 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 4, 0); |
| 1690 | int sbp4 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); |
| 1691 | int sbp5 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 14, 0); |
| 1692 | int sbp6 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 15, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1693 | break_point_hit_count = 0; |
| 1694 | f->Call(env->Global(), 0, NULL); |
| 1695 | CHECK_EQ(2, break_point_hit_count); |
| 1696 | g->Call(env->Global(), 0, NULL); |
| 1697 | CHECK_EQ(7, break_point_hit_count); |
| 1698 | |
| 1699 | // Remove all the break points again. |
| 1700 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1701 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 1702 | ClearBreakPointFromJS(env->GetIsolate(), sbp3); |
| 1703 | ClearBreakPointFromJS(env->GetIsolate(), sbp4); |
| 1704 | ClearBreakPointFromJS(env->GetIsolate(), sbp5); |
| 1705 | ClearBreakPointFromJS(env->GetIsolate(), sbp6); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1706 | f->Call(env->Global(), 0, NULL); |
| 1707 | CHECK_EQ(0, break_point_hit_count); |
| 1708 | g->Call(env->Global(), 0, NULL); |
| 1709 | CHECK_EQ(0, break_point_hit_count); |
| 1710 | |
| 1711 | v8::Debug::SetDebugEventListener(NULL); |
| 1712 | CheckDebuggerUnloaded(); |
| 1713 | |
| 1714 | // Make sure that the break point numbers are consecutive. |
| 1715 | CHECK_EQ(1, sbp1); |
| 1716 | CHECK_EQ(2, sbp2); |
| 1717 | CHECK_EQ(3, sbp3); |
| 1718 | CHECK_EQ(4, sbp4); |
| 1719 | CHECK_EQ(5, sbp5); |
| 1720 | CHECK_EQ(6, sbp6); |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | // Test conditional script break points. |
| 1725 | TEST(EnableDisableScriptBreakPoint) { |
| 1726 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1727 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1728 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1729 | env.ExposeDebug(); |
| 1730 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1731 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1732 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1733 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 1734 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1735 | "function f() {\n" |
| 1736 | " a = 0; // line 1\n" |
| 1737 | "};"); |
| 1738 | |
| 1739 | // Compile the script and get function f. |
| 1740 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1741 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1742 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1743 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 1744 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1745 | |
| 1746 | // Set script break point on line 1 (in function f). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1747 | int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Call f while enabeling and disabling the script break point. |
| 1750 | break_point_hit_count = 0; |
| 1751 | f->Call(env->Global(), 0, NULL); |
| 1752 | CHECK_EQ(1, break_point_hit_count); |
| 1753 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1754 | DisableScriptBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1755 | f->Call(env->Global(), 0, NULL); |
| 1756 | CHECK_EQ(1, break_point_hit_count); |
| 1757 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1758 | EnableScriptBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | f->Call(env->Global(), 0, NULL); |
| 1760 | CHECK_EQ(2, break_point_hit_count); |
| 1761 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1762 | DisableScriptBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1763 | f->Call(env->Global(), 0, NULL); |
| 1764 | CHECK_EQ(2, break_point_hit_count); |
| 1765 | |
| 1766 | // Reload the script and get f again checking that the disabeling survives. |
| 1767 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1768 | f = v8::Local<v8::Function>::Cast( |
| 1769 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | f->Call(env->Global(), 0, NULL); |
| 1771 | CHECK_EQ(2, break_point_hit_count); |
| 1772 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1773 | EnableScriptBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1774 | f->Call(env->Global(), 0, NULL); |
| 1775 | CHECK_EQ(3, break_point_hit_count); |
| 1776 | |
| 1777 | v8::Debug::SetDebugEventListener(NULL); |
| 1778 | CheckDebuggerUnloaded(); |
| 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | // Test conditional script break points. |
| 1783 | TEST(ConditionalScriptBreakPoint) { |
| 1784 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1785 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1786 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1787 | env.ExposeDebug(); |
| 1788 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1789 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1790 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1791 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 1792 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1793 | "count = 0;\n" |
| 1794 | "function f() {\n" |
| 1795 | " g(count++); // line 2\n" |
| 1796 | "};\n" |
| 1797 | "function g(x) {\n" |
| 1798 | " var a=x; // line 5\n" |
| 1799 | "};"); |
| 1800 | |
| 1801 | // Compile the script and get function f. |
| 1802 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1803 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1804 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1805 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 1806 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1807 | |
| 1808 | // Set script break point on line 5 (in function g). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1809 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 5, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1810 | |
| 1811 | // Call f with different conditions on the script break point. |
| 1812 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1813 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "false"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1814 | f->Call(env->Global(), 0, NULL); |
| 1815 | CHECK_EQ(0, break_point_hit_count); |
| 1816 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1817 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "true"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1818 | break_point_hit_count = 0; |
| 1819 | f->Call(env->Global(), 0, NULL); |
| 1820 | CHECK_EQ(1, break_point_hit_count); |
| 1821 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1822 | ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "x % 2 == 0"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1823 | break_point_hit_count = 0; |
| 1824 | for (int i = 0; i < 10; i++) { |
| 1825 | f->Call(env->Global(), 0, NULL); |
| 1826 | } |
| 1827 | CHECK_EQ(5, break_point_hit_count); |
| 1828 | |
| 1829 | // Reload the script and get f again checking that the condition survives. |
| 1830 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1831 | f = v8::Local<v8::Function>::Cast( |
| 1832 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1833 | |
| 1834 | break_point_hit_count = 0; |
| 1835 | for (int i = 0; i < 10; i++) { |
| 1836 | f->Call(env->Global(), 0, NULL); |
| 1837 | } |
| 1838 | CHECK_EQ(5, break_point_hit_count); |
| 1839 | |
| 1840 | v8::Debug::SetDebugEventListener(NULL); |
| 1841 | CheckDebuggerUnloaded(); |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | // Test ignore count on script break points. |
| 1846 | TEST(ScriptBreakPointIgnoreCount) { |
| 1847 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1848 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1849 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | env.ExposeDebug(); |
| 1851 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1852 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1853 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1854 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 1855 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1856 | "function f() {\n" |
| 1857 | " a = 0; // line 1\n" |
| 1858 | "};"); |
| 1859 | |
| 1860 | // Compile the script and get function f. |
| 1861 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1862 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1863 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1864 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 1865 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1866 | |
| 1867 | // Set script break point on line 1 (in function f). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1868 | int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1869 | |
| 1870 | // Call f with different ignores on the script break point. |
| 1871 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1872 | ChangeScriptBreakPointIgnoreCountFromJS(env->GetIsolate(), sbp, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1873 | f->Call(env->Global(), 0, NULL); |
| 1874 | CHECK_EQ(0, break_point_hit_count); |
| 1875 | f->Call(env->Global(), 0, NULL); |
| 1876 | CHECK_EQ(1, break_point_hit_count); |
| 1877 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1878 | ChangeScriptBreakPointIgnoreCountFromJS(env->GetIsolate(), sbp, 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1879 | break_point_hit_count = 0; |
| 1880 | for (int i = 0; i < 10; i++) { |
| 1881 | f->Call(env->Global(), 0, NULL); |
| 1882 | } |
| 1883 | CHECK_EQ(5, break_point_hit_count); |
| 1884 | |
| 1885 | // Reload the script and get f again checking that the ignore survives. |
| 1886 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1887 | f = v8::Local<v8::Function>::Cast( |
| 1888 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1889 | |
| 1890 | break_point_hit_count = 0; |
| 1891 | for (int i = 0; i < 10; i++) { |
| 1892 | f->Call(env->Global(), 0, NULL); |
| 1893 | } |
| 1894 | CHECK_EQ(5, break_point_hit_count); |
| 1895 | |
| 1896 | v8::Debug::SetDebugEventListener(NULL); |
| 1897 | CheckDebuggerUnloaded(); |
| 1898 | } |
| 1899 | |
| 1900 | |
| 1901 | // Test that script break points survive when a script is reloaded. |
| 1902 | TEST(ScriptBreakPointReload) { |
| 1903 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1904 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1905 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1906 | env.ExposeDebug(); |
| 1907 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1908 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1909 | |
| 1910 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1911 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 1912 | env->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1913 | "function f() {\n" |
| 1914 | " function h() {\n" |
| 1915 | " a = 0; // line 2\n" |
| 1916 | " }\n" |
| 1917 | " b = 1; // line 4\n" |
| 1918 | " return h();\n" |
| 1919 | "}"); |
| 1920 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1921 | v8::ScriptOrigin origin_1 = |
| 1922 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "1")); |
| 1923 | v8::ScriptOrigin origin_2 = |
| 1924 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "2")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1925 | |
| 1926 | // Set a script break point before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1927 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "1", 2, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1928 | |
| 1929 | // Compile the script and get the function. |
| 1930 | v8::Script::Compile(script, &origin_1)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1931 | f = v8::Local<v8::Function>::Cast( |
| 1932 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Call f and check that the script break point is active. |
| 1935 | break_point_hit_count = 0; |
| 1936 | f->Call(env->Global(), 0, NULL); |
| 1937 | CHECK_EQ(1, break_point_hit_count); |
| 1938 | |
| 1939 | // Compile the script again with a different script data and get the |
| 1940 | // function. |
| 1941 | v8::Script::Compile(script, &origin_2)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1942 | f = v8::Local<v8::Function>::Cast( |
| 1943 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1944 | |
| 1945 | // Call f and check that no break points are set. |
| 1946 | break_point_hit_count = 0; |
| 1947 | f->Call(env->Global(), 0, NULL); |
| 1948 | CHECK_EQ(0, break_point_hit_count); |
| 1949 | |
| 1950 | // Compile the script again and get the function. |
| 1951 | v8::Script::Compile(script, &origin_1)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1952 | f = v8::Local<v8::Function>::Cast( |
| 1953 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Call f and check that the script break point is active. |
| 1956 | break_point_hit_count = 0; |
| 1957 | f->Call(env->Global(), 0, NULL); |
| 1958 | CHECK_EQ(1, break_point_hit_count); |
| 1959 | |
| 1960 | v8::Debug::SetDebugEventListener(NULL); |
| 1961 | CheckDebuggerUnloaded(); |
| 1962 | } |
| 1963 | |
| 1964 | |
| 1965 | // Test when several scripts has the same script data |
| 1966 | TEST(ScriptBreakPointMultiple) { |
| 1967 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1968 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1969 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1970 | env.ExposeDebug(); |
| 1971 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1972 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1973 | |
| 1974 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1975 | v8::Local<v8::String> script_f = |
| 1976 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 1977 | "function f() {\n" |
| 1978 | " a = 0; // line 1\n" |
| 1979 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1980 | |
| 1981 | v8::Local<v8::Function> g; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1982 | v8::Local<v8::String> script_g = |
| 1983 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 1984 | "function g() {\n" |
| 1985 | " b = 0; // line 1\n" |
| 1986 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1987 | |
| 1988 | v8::ScriptOrigin origin = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1989 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1990 | |
| 1991 | // Set a script break point before the scripts are loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1992 | int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Compile the scripts with same script data and get the functions. |
| 1995 | v8::Script::Compile(script_f, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1996 | f = v8::Local<v8::Function>::Cast( |
| 1997 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1998 | v8::Script::Compile(script_g, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1999 | g = v8::Local<v8::Function>::Cast( |
| 2000 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2001 | |
| 2002 | // Call f and g and check that the script break point is active. |
| 2003 | break_point_hit_count = 0; |
| 2004 | f->Call(env->Global(), 0, NULL); |
| 2005 | CHECK_EQ(1, break_point_hit_count); |
| 2006 | g->Call(env->Global(), 0, NULL); |
| 2007 | CHECK_EQ(2, break_point_hit_count); |
| 2008 | |
| 2009 | // Clear the script break point. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2010 | ClearBreakPointFromJS(env->GetIsolate(), sbp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Call f and g and check that the script break point is no longer active. |
| 2013 | break_point_hit_count = 0; |
| 2014 | f->Call(env->Global(), 0, NULL); |
| 2015 | CHECK_EQ(0, break_point_hit_count); |
| 2016 | g->Call(env->Global(), 0, NULL); |
| 2017 | CHECK_EQ(0, break_point_hit_count); |
| 2018 | |
| 2019 | // Set script break point with the scripts loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2020 | sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Call f and g and check that the script break point is active. |
| 2023 | break_point_hit_count = 0; |
| 2024 | f->Call(env->Global(), 0, NULL); |
| 2025 | CHECK_EQ(1, break_point_hit_count); |
| 2026 | g->Call(env->Global(), 0, NULL); |
| 2027 | CHECK_EQ(2, break_point_hit_count); |
| 2028 | |
| 2029 | v8::Debug::SetDebugEventListener(NULL); |
| 2030 | CheckDebuggerUnloaded(); |
| 2031 | } |
| 2032 | |
| 2033 | |
| 2034 | // Test the script origin which has both name and line offset. |
| 2035 | TEST(ScriptBreakPointLineOffset) { |
| 2036 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2037 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2038 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2039 | env.ExposeDebug(); |
| 2040 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2041 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2042 | |
| 2043 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2044 | v8::Local<v8::String> script = v8::String::NewFromUtf8( |
| 2045 | env->GetIsolate(), |
| 2046 | "function f() {\n" |
| 2047 | " a = 0; // line 8 as this script has line offset 7\n" |
| 2048 | " b = 0; // line 9 as this script has line offset 7\n" |
| 2049 | "}"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2050 | |
| 2051 | // Create script origin both name and line offset. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2052 | v8::ScriptOrigin origin( |
| 2053 | v8::String::NewFromUtf8(env->GetIsolate(), "test.html"), |
| 2054 | v8::Integer::New(env->GetIsolate(), 7)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Set two script break points before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2057 | int sbp1 = |
| 2058 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 8, 0); |
| 2059 | int sbp2 = |
| 2060 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Compile the script and get the function. |
| 2063 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2064 | f = v8::Local<v8::Function>::Cast( |
| 2065 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2066 | |
| 2067 | // Call f and check that the script break point is active. |
| 2068 | break_point_hit_count = 0; |
| 2069 | f->Call(env->Global(), 0, NULL); |
| 2070 | CHECK_EQ(2, break_point_hit_count); |
| 2071 | |
| 2072 | // Clear the script break points. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2073 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 2074 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2075 | |
| 2076 | // Call f and check that no script break points are active. |
| 2077 | break_point_hit_count = 0; |
| 2078 | f->Call(env->Global(), 0, NULL); |
| 2079 | CHECK_EQ(0, break_point_hit_count); |
| 2080 | |
| 2081 | // Set a script break point with the script loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2082 | sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Call f and check that the script break point is active. |
| 2085 | break_point_hit_count = 0; |
| 2086 | f->Call(env->Global(), 0, NULL); |
| 2087 | CHECK_EQ(1, break_point_hit_count); |
| 2088 | |
| 2089 | v8::Debug::SetDebugEventListener(NULL); |
| 2090 | CheckDebuggerUnloaded(); |
| 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | // Test script break points set on lines. |
| 2095 | TEST(ScriptBreakPointLine) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2096 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2097 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2098 | env.ExposeDebug(); |
| 2099 | |
| 2100 | // Create a function for checking the function when hitting a break point. |
| 2101 | frame_function_name = CompileFunction(&env, |
| 2102 | frame_function_name_source, |
| 2103 | "frame_function_name"); |
| 2104 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2105 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2106 | |
| 2107 | v8::Local<v8::Function> f; |
| 2108 | v8::Local<v8::Function> g; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2109 | v8::Local<v8::String> script = |
| 2110 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 2111 | "a = 0 // line 0\n" |
| 2112 | "function f() {\n" |
| 2113 | " a = 1; // line 2\n" |
| 2114 | "}\n" |
| 2115 | " a = 2; // line 4\n" |
| 2116 | " /* xx */ function g() { // line 5\n" |
| 2117 | " function h() { // line 6\n" |
| 2118 | " a = 3; // line 7\n" |
| 2119 | " }\n" |
| 2120 | " h(); // line 9\n" |
| 2121 | " a = 4; // line 10\n" |
| 2122 | " }\n" |
| 2123 | " a=5; // line 12"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2124 | |
| 2125 | // Set a couple script break point before the script is loaded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2126 | int sbp1 = |
| 2127 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 0, -1); |
| 2128 | int sbp2 = |
| 2129 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 1, -1); |
| 2130 | int sbp3 = |
| 2131 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 5, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2132 | |
| 2133 | // Compile the script and get the function. |
| 2134 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2135 | v8::ScriptOrigin origin( |
| 2136 | v8::String::NewFromUtf8(env->GetIsolate(), "test.html"), |
| 2137 | v8::Integer::New(env->GetIsolate(), 0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2138 | v8::Script::Compile(script, &origin)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2139 | f = v8::Local<v8::Function>::Cast( |
| 2140 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
| 2141 | g = v8::Local<v8::Function>::Cast( |
| 2142 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2143 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2144 | // Check that a break point was hit when the script was run. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2145 | CHECK_EQ(1, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2146 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | |
| 2148 | // Call f and check that the script break point. |
| 2149 | f->Call(env->Global(), 0, NULL); |
| 2150 | CHECK_EQ(2, break_point_hit_count); |
| 2151 | CHECK_EQ("f", last_function_hit); |
| 2152 | |
| 2153 | // Call g and check that the script break point. |
| 2154 | g->Call(env->Global(), 0, NULL); |
| 2155 | CHECK_EQ(3, break_point_hit_count); |
| 2156 | CHECK_EQ("g", last_function_hit); |
| 2157 | |
| 2158 | // Clear the script break point on g and set one on h. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2159 | ClearBreakPointFromJS(env->GetIsolate(), sbp3); |
| 2160 | int sbp4 = |
| 2161 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 6, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Call g and check that the script break point in h is hit. |
| 2164 | g->Call(env->Global(), 0, NULL); |
| 2165 | CHECK_EQ(4, break_point_hit_count); |
| 2166 | CHECK_EQ("h", last_function_hit); |
| 2167 | |
| 2168 | // Clear break points in f and h. Set a new one in the script between |
| 2169 | // functions f and g and test that there is no break points in f and g any |
| 2170 | // more. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2171 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 2172 | ClearBreakPointFromJS(env->GetIsolate(), sbp4); |
| 2173 | int sbp5 = |
| 2174 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 4, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2175 | break_point_hit_count = 0; |
| 2176 | f->Call(env->Global(), 0, NULL); |
| 2177 | g->Call(env->Global(), 0, NULL); |
| 2178 | CHECK_EQ(0, break_point_hit_count); |
| 2179 | |
| 2180 | // Reload the script which should hit two break points. |
| 2181 | break_point_hit_count = 0; |
| 2182 | v8::Script::Compile(script, &origin)->Run(); |
| 2183 | CHECK_EQ(2, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2184 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Set a break point in the code after the last function decleration. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2187 | int sbp6 = |
| 2188 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 12, -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2189 | |
| 2190 | // Reload the script which should hit three break points. |
| 2191 | break_point_hit_count = 0; |
| 2192 | v8::Script::Compile(script, &origin)->Run(); |
| 2193 | CHECK_EQ(3, break_point_hit_count); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2194 | CHECK_EQ(0, StrLength(last_function_hit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2195 | |
| 2196 | // Clear the last break points, and reload the script which should not hit any |
| 2197 | // break points. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2198 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 2199 | ClearBreakPointFromJS(env->GetIsolate(), sbp5); |
| 2200 | ClearBreakPointFromJS(env->GetIsolate(), sbp6); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2201 | break_point_hit_count = 0; |
| 2202 | v8::Script::Compile(script, &origin)->Run(); |
| 2203 | CHECK_EQ(0, break_point_hit_count); |
| 2204 | |
| 2205 | v8::Debug::SetDebugEventListener(NULL); |
| 2206 | CheckDebuggerUnloaded(); |
| 2207 | } |
| 2208 | |
| 2209 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2210 | // Test top level script break points set on lines. |
| 2211 | TEST(ScriptBreakPointLineTopLevel) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2212 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2213 | v8::HandleScope scope(env->GetIsolate()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2214 | env.ExposeDebug(); |
| 2215 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2216 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2217 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2218 | v8::Local<v8::String> script = |
| 2219 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 2220 | "function f() {\n" |
| 2221 | " a = 1; // line 1\n" |
| 2222 | "}\n" |
| 2223 | "a = 2; // line 3\n"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2224 | v8::Local<v8::Function> f; |
| 2225 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2226 | v8::HandleScope scope(env->GetIsolate()); |
| 2227 | CompileRunWithOrigin(script, "test.html"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2228 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2229 | f = v8::Local<v8::Function>::Cast( |
| 2230 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2231 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2232 | CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2233 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2234 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2235 | |
| 2236 | // Call f and check that there was no break points. |
| 2237 | break_point_hit_count = 0; |
| 2238 | f->Call(env->Global(), 0, NULL); |
| 2239 | CHECK_EQ(0, break_point_hit_count); |
| 2240 | |
| 2241 | // Recompile and run script and check that break point was hit. |
| 2242 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2243 | CompileRunWithOrigin(script, "test.html"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2244 | CHECK_EQ(1, break_point_hit_count); |
| 2245 | |
| 2246 | // Call f and check that there are still no break points. |
| 2247 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2248 | f = v8::Local<v8::Function>::Cast( |
| 2249 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2250 | CHECK_EQ(0, break_point_hit_count); |
| 2251 | |
| 2252 | v8::Debug::SetDebugEventListener(NULL); |
| 2253 | CheckDebuggerUnloaded(); |
| 2254 | } |
| 2255 | |
| 2256 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2257 | // Test that it is possible to add and remove break points in a top level |
| 2258 | // function which has no references but has not been collected yet. |
| 2259 | TEST(ScriptBreakPointTopLevelCrash) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2260 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2261 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2262 | env.ExposeDebug(); |
| 2263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2264 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2265 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2266 | v8::Local<v8::String> script_source = |
| 2267 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 2268 | "function f() {\n" |
| 2269 | " return 0;\n" |
| 2270 | "}\n" |
| 2271 | "f()"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2272 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2273 | int sbp1 = |
| 2274 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2275 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2276 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2277 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2278 | CompileRunWithOrigin(script_source, "test.html"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2279 | CHECK_EQ(1, break_point_hit_count); |
| 2280 | } |
| 2281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2282 | int sbp2 = |
| 2283 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); |
| 2284 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 2285 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2286 | |
| 2287 | v8::Debug::SetDebugEventListener(NULL); |
| 2288 | CheckDebuggerUnloaded(); |
| 2289 | } |
| 2290 | |
| 2291 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2292 | // Test that it is possible to remove the last break point for a function |
| 2293 | // inside the break handling of that break point. |
| 2294 | TEST(RemoveBreakPointInBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2295 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2296 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2297 | |
| 2298 | v8::Local<v8::Function> foo = |
| 2299 | CompileFunction(&env, "function foo(){a=1;}", "foo"); |
| 2300 | debug_event_remove_break_point = SetBreakPoint(foo, 0); |
| 2301 | |
| 2302 | // Register the debug event listener pasing the function |
| 2303 | v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo); |
| 2304 | |
| 2305 | break_point_hit_count = 0; |
| 2306 | foo->Call(env->Global(), 0, NULL); |
| 2307 | CHECK_EQ(1, break_point_hit_count); |
| 2308 | |
| 2309 | break_point_hit_count = 0; |
| 2310 | foo->Call(env->Global(), 0, NULL); |
| 2311 | CHECK_EQ(0, break_point_hit_count); |
| 2312 | |
| 2313 | v8::Debug::SetDebugEventListener(NULL); |
| 2314 | CheckDebuggerUnloaded(); |
| 2315 | } |
| 2316 | |
| 2317 | |
| 2318 | // Test that the debugger statement causes a break. |
| 2319 | TEST(DebuggerStatement) { |
| 2320 | break_point_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2321 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2322 | v8::HandleScope scope(env->GetIsolate()); |
| 2323 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 2324 | v8::Script::Compile( |
| 2325 | v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){debugger}")) |
| 2326 | ->Run(); |
| 2327 | v8::Script::Compile( |
| 2328 | v8::String::NewFromUtf8(env->GetIsolate(), |
| 2329 | "function foo(){debugger;debugger;}"))->Run(); |
| 2330 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 2331 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
| 2332 | v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast( |
| 2333 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "bar"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2334 | |
| 2335 | // Run function with debugger statement |
| 2336 | bar->Call(env->Global(), 0, NULL); |
| 2337 | CHECK_EQ(1, break_point_hit_count); |
| 2338 | |
| 2339 | // Run function with two debugger statement |
| 2340 | foo->Call(env->Global(), 0, NULL); |
| 2341 | CHECK_EQ(3, break_point_hit_count); |
| 2342 | |
| 2343 | v8::Debug::SetDebugEventListener(NULL); |
| 2344 | CheckDebuggerUnloaded(); |
| 2345 | } |
| 2346 | |
| 2347 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2348 | // Test setting a breakpoint on the debugger statement. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2349 | TEST(DebuggerStatementBreakpoint) { |
| 2350 | break_point_hit_count = 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2351 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2352 | v8::HandleScope scope(env->GetIsolate()); |
| 2353 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 2354 | v8::Script::Compile( |
| 2355 | v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){debugger;}")) |
| 2356 | ->Run(); |
| 2357 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 2358 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo"))); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2359 | |
| 2360 | // The debugger statement triggers breakpint hit |
| 2361 | foo->Call(env->Global(), 0, NULL); |
| 2362 | CHECK_EQ(1, break_point_hit_count); |
| 2363 | |
| 2364 | int bp = SetBreakPoint(foo, 0); |
| 2365 | |
| 2366 | // Set breakpoint does not duplicate hits |
| 2367 | foo->Call(env->Global(), 0, NULL); |
| 2368 | CHECK_EQ(2, break_point_hit_count); |
| 2369 | |
| 2370 | ClearBreakPoint(bp); |
| 2371 | v8::Debug::SetDebugEventListener(NULL); |
| 2372 | CheckDebuggerUnloaded(); |
| 2373 | } |
| 2374 | |
| 2375 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2376 | // 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] | 2377 | // the correct results. |
| 2378 | TEST(DebugEvaluate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2379 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2380 | v8::Isolate* isolate = env->GetIsolate(); |
| 2381 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2382 | env.ExposeDebug(); |
| 2383 | |
| 2384 | // Create a function for checking the evaluation when hitting a break point. |
| 2385 | evaluate_check_function = CompileFunction(&env, |
| 2386 | evaluate_check_source, |
| 2387 | "evaluate_check"); |
| 2388 | // Register the debug event listener |
| 2389 | v8::Debug::SetDebugEventListener(DebugEventEvaluate); |
| 2390 | |
| 2391 | // Different expected vaules of x and a when in a break point (u = undefined, |
| 2392 | // d = Hello, world!). |
| 2393 | struct EvaluateCheck checks_uu[] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2394 | {"x", v8::Undefined(isolate)}, |
| 2395 | {"a", v8::Undefined(isolate)}, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2396 | {NULL, v8::Handle<v8::Value>()} |
| 2397 | }; |
| 2398 | struct EvaluateCheck checks_hu[] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2399 | {"x", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")}, |
| 2400 | {"a", v8::Undefined(isolate)}, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2401 | {NULL, v8::Handle<v8::Value>()} |
| 2402 | }; |
| 2403 | struct EvaluateCheck checks_hh[] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2404 | {"x", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")}, |
| 2405 | {"a", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")}, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2406 | {NULL, v8::Handle<v8::Value>()} |
| 2407 | }; |
| 2408 | |
| 2409 | // 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] | 2410 | // 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] | 2411 | // therefore setting breakpoint at position 15 will break at "y=0" and |
| 2412 | // setting it higher will break after. |
| 2413 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2414 | "function foo(x) {" |
| 2415 | " var a;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2416 | " y=0;" // To ensure break location 1. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2417 | " a=x;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2418 | " y=0;" // To ensure break location 2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2419 | "}", |
| 2420 | "foo"); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2421 | const int foo_break_position_1 = 15; |
| 2422 | const int foo_break_position_2 = 29; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Arguments with one parameter "Hello, world!" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2425 | v8::Handle<v8::Value> argv_foo[1] = { |
| 2426 | v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2427 | |
| 2428 | // Call foo with breakpoint set before a=x and undefined as parameter. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2429 | int bp = SetBreakPoint(foo, foo_break_position_1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2430 | checks = checks_uu; |
| 2431 | foo->Call(env->Global(), 0, NULL); |
| 2432 | |
| 2433 | // Call foo with breakpoint set before a=x and parameter "Hello, world!". |
| 2434 | checks = checks_hu; |
| 2435 | foo->Call(env->Global(), 1, argv_foo); |
| 2436 | |
| 2437 | // Call foo with breakpoint set after a=x and parameter "Hello, world!". |
| 2438 | ClearBreakPoint(bp); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2439 | SetBreakPoint(foo, foo_break_position_2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2440 | checks = checks_hh; |
| 2441 | foo->Call(env->Global(), 1, argv_foo); |
| 2442 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2443 | // Test that overriding Object.prototype will not interfere into evaluation |
| 2444 | // on call frame. |
| 2445 | v8::Local<v8::Function> zoo = |
| 2446 | CompileFunction(&env, |
| 2447 | "x = undefined;" |
| 2448 | "function zoo(t) {" |
| 2449 | " var a=x;" |
| 2450 | " Object.prototype.x = 42;" |
| 2451 | " x=t;" |
| 2452 | " y=0;" // To ensure break location. |
| 2453 | " delete Object.prototype.x;" |
| 2454 | " x=a;" |
| 2455 | "}", |
| 2456 | "zoo"); |
| 2457 | const int zoo_break_position = 50; |
| 2458 | |
| 2459 | // Arguments with one parameter "Hello, world!" |
| 2460 | v8::Handle<v8::Value> argv_zoo[1] = { |
| 2461 | v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")}; |
| 2462 | |
| 2463 | // Call zoo with breakpoint set at y=0. |
| 2464 | DebugEventCounterClear(); |
| 2465 | bp = SetBreakPoint(zoo, zoo_break_position); |
| 2466 | checks = checks_hu; |
| 2467 | zoo->Call(env->Global(), 1, argv_zoo); |
| 2468 | CHECK_EQ(1, break_point_hit_count); |
| 2469 | ClearBreakPoint(bp); |
| 2470 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2471 | // Test function with an inner function. The "y=0" is in function barbar |
| 2472 | // to provide a break location. For "y=0" the "y" is at position 8 in the |
| 2473 | // barbar function therefore setting breakpoint at position 8 will break at |
| 2474 | // "y=0" and setting it higher will break after. |
| 2475 | v8::Local<v8::Function> bar = CompileFunction(&env, |
| 2476 | "y = 0;" |
| 2477 | "x = 'Goodbye, world!';" |
| 2478 | "function bar(x, b) {" |
| 2479 | " var a;" |
| 2480 | " function barbar() {" |
| 2481 | " y=0; /* To ensure break location.*/" |
| 2482 | " a=x;" |
| 2483 | " };" |
| 2484 | " debug.Debug.clearAllBreakPoints();" |
| 2485 | " barbar();" |
| 2486 | " y=0;a=x;" |
| 2487 | "}", |
| 2488 | "bar"); |
| 2489 | const int barbar_break_position = 8; |
| 2490 | |
| 2491 | // Call bar setting breakpoint before a=x in barbar and undefined as |
| 2492 | // parameter. |
| 2493 | checks = checks_uu; |
| 2494 | v8::Handle<v8::Value> argv_bar_1[2] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2495 | v8::Undefined(isolate), |
| 2496 | v8::Number::New(isolate, barbar_break_position) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2497 | }; |
| 2498 | bar->Call(env->Global(), 2, argv_bar_1); |
| 2499 | |
| 2500 | // Call bar setting breakpoint before a=x in barbar and parameter |
| 2501 | // "Hello, world!". |
| 2502 | checks = checks_hu; |
| 2503 | v8::Handle<v8::Value> argv_bar_2[2] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2504 | v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!"), |
| 2505 | v8::Number::New(env->GetIsolate(), barbar_break_position) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | }; |
| 2507 | bar->Call(env->Global(), 2, argv_bar_2); |
| 2508 | |
| 2509 | // Call bar setting breakpoint after a=x in barbar and parameter |
| 2510 | // "Hello, world!". |
| 2511 | checks = checks_hh; |
| 2512 | v8::Handle<v8::Value> argv_bar_3[2] = { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2513 | v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!"), |
| 2514 | v8::Number::New(env->GetIsolate(), barbar_break_position + 1) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2515 | }; |
| 2516 | bar->Call(env->Global(), 2, argv_bar_3); |
| 2517 | |
| 2518 | v8::Debug::SetDebugEventListener(NULL); |
| 2519 | CheckDebuggerUnloaded(); |
| 2520 | } |
| 2521 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2522 | |
| 2523 | int debugEventCount = 0; |
| 2524 | static void CheckDebugEvent(const v8::Debug::EventDetails& eventDetails) { |
| 2525 | if (eventDetails.GetEvent() == v8::Break) ++debugEventCount; |
| 2526 | } |
| 2527 | |
| 2528 | |
| 2529 | // Test that the conditional breakpoints work event if code generation from |
| 2530 | // strings is prohibited in the debugee context. |
| 2531 | TEST(ConditionalBreakpointWithCodeGenerationDisallowed) { |
| 2532 | DebugLocalContext env; |
| 2533 | v8::HandleScope scope(env->GetIsolate()); |
| 2534 | env.ExposeDebug(); |
| 2535 | |
| 2536 | v8::Debug::SetDebugEventListener(CheckDebugEvent); |
| 2537 | |
| 2538 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2539 | "function foo(x) {\n" |
| 2540 | " var s = 'String value2';\n" |
| 2541 | " return s + x;\n" |
| 2542 | "}", |
| 2543 | "foo"); |
| 2544 | |
| 2545 | // Set conditional breakpoint with condition 'true'. |
| 2546 | CompileRun("debug.Debug.setBreakPoint(foo, 2, 0, 'true')"); |
| 2547 | |
| 2548 | debugEventCount = 0; |
| 2549 | env->AllowCodeGenerationFromStrings(false); |
| 2550 | foo->Call(env->Global(), 0, NULL); |
| 2551 | CHECK_EQ(1, debugEventCount); |
| 2552 | |
| 2553 | v8::Debug::SetDebugEventListener(NULL); |
| 2554 | CheckDebuggerUnloaded(); |
| 2555 | } |
| 2556 | |
| 2557 | |
| 2558 | bool checkedDebugEvals = true; |
| 2559 | v8::Handle<v8::Function> checkGlobalEvalFunction; |
| 2560 | v8::Handle<v8::Function> checkFrameEvalFunction; |
| 2561 | static void CheckDebugEval(const v8::Debug::EventDetails& eventDetails) { |
| 2562 | if (eventDetails.GetEvent() == v8::Break) { |
| 2563 | ++debugEventCount; |
| 2564 | v8::HandleScope handleScope(CcTest::isolate()); |
| 2565 | |
| 2566 | v8::Handle<v8::Value> args[] = { eventDetails.GetExecutionState() }; |
| 2567 | CHECK(checkGlobalEvalFunction->Call( |
| 2568 | eventDetails.GetEventContext()->Global(), 1, args)->IsTrue()); |
| 2569 | CHECK(checkFrameEvalFunction->Call( |
| 2570 | eventDetails.GetEventContext()->Global(), 1, args)->IsTrue()); |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | // Test that the evaluation of expressions when a break point is hit generates |
| 2576 | // the correct results in case code generation from strings is disallowed in the |
| 2577 | // debugee context. |
| 2578 | TEST(DebugEvaluateWithCodeGenerationDisallowed) { |
| 2579 | DebugLocalContext env; |
| 2580 | v8::HandleScope scope(env->GetIsolate()); |
| 2581 | env.ExposeDebug(); |
| 2582 | |
| 2583 | v8::Debug::SetDebugEventListener(CheckDebugEval); |
| 2584 | |
| 2585 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2586 | "var global = 'Global';\n" |
| 2587 | "function foo(x) {\n" |
| 2588 | " var local = 'Local';\n" |
| 2589 | " debugger;\n" |
| 2590 | " return local + x;\n" |
| 2591 | "}", |
| 2592 | "foo"); |
| 2593 | checkGlobalEvalFunction = CompileFunction(&env, |
| 2594 | "function checkGlobalEval(exec_state) {\n" |
| 2595 | " return exec_state.evaluateGlobal('global').value() === 'Global';\n" |
| 2596 | "}", |
| 2597 | "checkGlobalEval"); |
| 2598 | |
| 2599 | checkFrameEvalFunction = CompileFunction(&env, |
| 2600 | "function checkFrameEval(exec_state) {\n" |
| 2601 | " return exec_state.frame(0).evaluate('local').value() === 'Local';\n" |
| 2602 | "}", |
| 2603 | "checkFrameEval"); |
| 2604 | debugEventCount = 0; |
| 2605 | env->AllowCodeGenerationFromStrings(false); |
| 2606 | foo->Call(env->Global(), 0, NULL); |
| 2607 | CHECK_EQ(1, debugEventCount); |
| 2608 | |
| 2609 | checkGlobalEvalFunction.Clear(); |
| 2610 | checkFrameEvalFunction.Clear(); |
| 2611 | v8::Debug::SetDebugEventListener(NULL); |
| 2612 | CheckDebuggerUnloaded(); |
| 2613 | } |
| 2614 | |
| 2615 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2616 | // Copies a C string to a 16-bit string. Does not check for buffer overflow. |
| 2617 | // Does not use the V8 engine to convert strings, so it can be used |
| 2618 | // in any thread. Returns the length of the string. |
| 2619 | int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) { |
| 2620 | int i; |
| 2621 | for (i = 0; input_buffer[i] != '\0'; ++i) { |
| 2622 | // ASCII does not use chars > 127, but be careful anyway. |
| 2623 | output_buffer[i] = static_cast<unsigned char>(input_buffer[i]); |
| 2624 | } |
| 2625 | output_buffer[i] = 0; |
| 2626 | return i; |
| 2627 | } |
| 2628 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2629 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2630 | // Copies a 16-bit string to a C string by dropping the high byte of |
| 2631 | // each character. Does not check for buffer overflow. |
| 2632 | // Can be used in any thread. Requires string length as an input. |
| 2633 | int Utf16ToAscii(const uint16_t* input_buffer, int length, |
| 2634 | char* output_buffer, int output_len = -1) { |
| 2635 | if (output_len >= 0) { |
| 2636 | if (length > output_len - 1) { |
| 2637 | length = output_len - 1; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | for (int i = 0; i < length; ++i) { |
| 2642 | output_buffer[i] = static_cast<char>(input_buffer[i]); |
| 2643 | } |
| 2644 | output_buffer[length] = '\0'; |
| 2645 | return length; |
| 2646 | } |
| 2647 | |
| 2648 | |
| 2649 | // We match parts of the message to get evaluate result int value. |
| 2650 | bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2651 | if (strstr(message, "\"command\":\"evaluate\"") == NULL) { |
| 2652 | return false; |
| 2653 | } |
| 2654 | const char* prefix = "\"text\":\""; |
| 2655 | char* pos1 = strstr(message, prefix); |
| 2656 | if (pos1 == NULL) { |
| 2657 | return false; |
| 2658 | } |
| 2659 | pos1 += strlen(prefix); |
| 2660 | char* pos2 = strchr(pos1, '"'); |
| 2661 | if (pos2 == NULL) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2662 | return false; |
| 2663 | } |
| 2664 | Vector<char> buf(buffer, buffer_size); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2665 | int len = static_cast<int>(pos2 - pos1); |
| 2666 | if (len > buffer_size - 1) { |
| 2667 | len = buffer_size - 1; |
| 2668 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2669 | StrNCpy(buf, pos1, len); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2670 | buffer[buffer_size - 1] = '\0'; |
| 2671 | return true; |
| 2672 | } |
| 2673 | |
| 2674 | |
| 2675 | struct EvaluateResult { |
| 2676 | static const int kBufferSize = 20; |
| 2677 | char buffer[kBufferSize]; |
| 2678 | }; |
| 2679 | |
| 2680 | struct DebugProcessDebugMessagesData { |
| 2681 | static const int kArraySize = 5; |
| 2682 | int counter; |
| 2683 | EvaluateResult results[kArraySize]; |
| 2684 | |
| 2685 | void reset() { |
| 2686 | counter = 0; |
| 2687 | } |
| 2688 | EvaluateResult* current() { |
| 2689 | return &results[counter % kArraySize]; |
| 2690 | } |
| 2691 | void next() { |
| 2692 | counter++; |
| 2693 | } |
| 2694 | }; |
| 2695 | |
| 2696 | DebugProcessDebugMessagesData process_debug_messages_data; |
| 2697 | |
| 2698 | static void DebugProcessDebugMessagesHandler( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2699 | const v8::Debug::Message& message) { |
| 2700 | v8::Handle<v8::String> json = message.GetJSON(); |
| 2701 | v8::String::Utf8Value utf8(json); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2702 | EvaluateResult* array_item = process_debug_messages_data.current(); |
| 2703 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2704 | bool res = GetEvaluateStringResult(*utf8, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2705 | array_item->buffer, |
| 2706 | EvaluateResult::kBufferSize); |
| 2707 | if (res) { |
| 2708 | process_debug_messages_data.next(); |
| 2709 | } |
| 2710 | } |
| 2711 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2712 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2713 | // Test that the evaluation of expressions works even from ProcessDebugMessages |
| 2714 | // i.e. with empty stack. |
| 2715 | TEST(DebugEvaluateWithoutStack) { |
| 2716 | v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler); |
| 2717 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2718 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2719 | v8::HandleScope scope(env->GetIsolate()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2720 | |
| 2721 | const char* source = |
| 2722 | "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }"; |
| 2723 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2724 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), source)) |
| 2725 | ->Run(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2726 | |
| 2727 | v8::Debug::ProcessDebugMessages(); |
| 2728 | |
| 2729 | const int kBufferSize = 1000; |
| 2730 | uint16_t buffer[kBufferSize]; |
| 2731 | |
| 2732 | const char* command_111 = "{\"seq\":111," |
| 2733 | "\"type\":\"request\"," |
| 2734 | "\"command\":\"evaluate\"," |
| 2735 | "\"arguments\":{" |
| 2736 | " \"global\":true," |
| 2737 | " \"expression\":\"v1\",\"disable_break\":true" |
| 2738 | "}}"; |
| 2739 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2740 | v8::Isolate* isolate = CcTest::isolate(); |
| 2741 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_111, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2742 | |
| 2743 | const char* command_112 = "{\"seq\":112," |
| 2744 | "\"type\":\"request\"," |
| 2745 | "\"command\":\"evaluate\"," |
| 2746 | "\"arguments\":{" |
| 2747 | " \"global\":true," |
| 2748 | " \"expression\":\"getAnimal()\",\"disable_break\":true" |
| 2749 | "}}"; |
| 2750 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2751 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_112, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2752 | |
| 2753 | const char* command_113 = "{\"seq\":113," |
| 2754 | "\"type\":\"request\"," |
| 2755 | "\"command\":\"evaluate\"," |
| 2756 | "\"arguments\":{" |
| 2757 | " \"global\":true," |
| 2758 | " \"expression\":\"239 + 566\",\"disable_break\":true" |
| 2759 | "}}"; |
| 2760 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2761 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_113, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2762 | |
| 2763 | v8::Debug::ProcessDebugMessages(); |
| 2764 | |
| 2765 | CHECK_EQ(3, process_debug_messages_data.counter); |
| 2766 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2767 | CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0); |
| 2768 | CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer), |
| 2769 | 0); |
| 2770 | CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2771 | |
| 2772 | v8::Debug::SetMessageHandler(NULL); |
| 2773 | v8::Debug::SetDebugEventListener(NULL); |
| 2774 | CheckDebuggerUnloaded(); |
| 2775 | } |
| 2776 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2777 | |
| 2778 | // Simple test of the stepping mechanism using only store ICs. |
| 2779 | TEST(DebugStepLinear) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2780 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2781 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2782 | |
| 2783 | // Create a function for testing stepping. |
| 2784 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2785 | "function foo(){a=1;b=1;c=1;}", |
| 2786 | "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2787 | |
| 2788 | // Run foo to allow it to get optimized. |
| 2789 | CompileRun("a=0; b=0; c=0; foo();"); |
| 2790 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2791 | SetBreakPoint(foo, 3); |
| 2792 | |
| 2793 | // Register a debug event listener which steps and counts. |
| 2794 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2795 | |
| 2796 | step_action = StepIn; |
| 2797 | break_point_hit_count = 0; |
| 2798 | foo->Call(env->Global(), 0, NULL); |
| 2799 | |
| 2800 | // With stepping all break locations are hit. |
| 2801 | CHECK_EQ(4, break_point_hit_count); |
| 2802 | |
| 2803 | v8::Debug::SetDebugEventListener(NULL); |
| 2804 | CheckDebuggerUnloaded(); |
| 2805 | |
| 2806 | // Register a debug event listener which just counts. |
| 2807 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 2808 | |
| 2809 | SetBreakPoint(foo, 3); |
| 2810 | break_point_hit_count = 0; |
| 2811 | foo->Call(env->Global(), 0, NULL); |
| 2812 | |
| 2813 | // Without stepping only active break points are hit. |
| 2814 | CHECK_EQ(1, break_point_hit_count); |
| 2815 | |
| 2816 | v8::Debug::SetDebugEventListener(NULL); |
| 2817 | CheckDebuggerUnloaded(); |
| 2818 | } |
| 2819 | |
| 2820 | |
| 2821 | // Test of the stepping mechanism for keyed load in a loop. |
| 2822 | TEST(DebugStepKeyedLoadLoop) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2823 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2824 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2825 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2826 | // Register a debug event listener which steps and counts. |
| 2827 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2828 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2829 | // Create a function for testing stepping of keyed load. The statement 'y=1' |
| 2830 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2831 | v8::Local<v8::Function> foo = CompileFunction( |
| 2832 | &env, |
| 2833 | "function foo(a) {\n" |
| 2834 | " var x;\n" |
| 2835 | " var len = a.length;\n" |
| 2836 | " for (var i = 0; i < len; i++) {\n" |
| 2837 | " y = 1;\n" |
| 2838 | " x = a[i];\n" |
| 2839 | " }\n" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2840 | "}\n" |
| 2841 | "y=0\n", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2842 | "foo"); |
| 2843 | |
| 2844 | // Create array [0,1,2,3,4,5,6,7,8,9] |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2845 | v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2846 | for (int i = 0; i < 10; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2847 | a->Set(v8::Number::New(env->GetIsolate(), i), |
| 2848 | v8::Number::New(env->GetIsolate(), i)); |
| 2849 | } |
| 2850 | |
| 2851 | // Call function without any break points to ensure inlining is in place. |
| 2852 | const int kArgc = 1; |
| 2853 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2854 | foo->Call(env->Global(), kArgc, args); |
| 2855 | |
| 2856 | // Set up break point and step through the function. |
| 2857 | SetBreakPoint(foo, 3); |
| 2858 | step_action = StepNext; |
| 2859 | break_point_hit_count = 0; |
| 2860 | foo->Call(env->Global(), kArgc, args); |
| 2861 | |
| 2862 | // With stepping all break locations are hit. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2863 | CHECK_EQ(45, break_point_hit_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2864 | |
| 2865 | v8::Debug::SetDebugEventListener(NULL); |
| 2866 | CheckDebuggerUnloaded(); |
| 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | // Test of the stepping mechanism for keyed store in a loop. |
| 2871 | TEST(DebugStepKeyedStoreLoop) { |
| 2872 | DebugLocalContext env; |
| 2873 | v8::HandleScope scope(env->GetIsolate()); |
| 2874 | |
| 2875 | // Register a debug event listener which steps and counts. |
| 2876 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2877 | |
| 2878 | // Create a function for testing stepping of keyed store. The statement 'y=1' |
| 2879 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2880 | v8::Local<v8::Function> foo = CompileFunction( |
| 2881 | &env, |
| 2882 | "function foo(a) {\n" |
| 2883 | " var len = a.length;\n" |
| 2884 | " for (var i = 0; i < len; i++) {\n" |
| 2885 | " y = 1;\n" |
| 2886 | " a[i] = 42;\n" |
| 2887 | " }\n" |
| 2888 | "}\n" |
| 2889 | "y=0\n", |
| 2890 | "foo"); |
| 2891 | |
| 2892 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2893 | v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); |
| 2894 | for (int i = 0; i < 10; i++) { |
| 2895 | a->Set(v8::Number::New(env->GetIsolate(), i), |
| 2896 | v8::Number::New(env->GetIsolate(), i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | // Call function without any break points to ensure inlining is in place. |
| 2900 | const int kArgc = 1; |
| 2901 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2902 | foo->Call(env->Global(), kArgc, args); |
| 2903 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2904 | // Set up break point and step through the function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2905 | SetBreakPoint(foo, 3); |
| 2906 | step_action = StepNext; |
| 2907 | break_point_hit_count = 0; |
| 2908 | foo->Call(env->Global(), kArgc, args); |
| 2909 | |
| 2910 | // With stepping all break locations are hit. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2911 | CHECK_EQ(44, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2912 | |
| 2913 | v8::Debug::SetDebugEventListener(NULL); |
| 2914 | CheckDebuggerUnloaded(); |
| 2915 | } |
| 2916 | |
| 2917 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2918 | // Test of the stepping mechanism for named load in a loop. |
| 2919 | TEST(DebugStepNamedLoadLoop) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2920 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2921 | v8::HandleScope scope(env->GetIsolate()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2922 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2923 | // Register a debug event listener which steps and counts. |
| 2924 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2925 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2926 | // Create a function for testing stepping of named load. |
| 2927 | v8::Local<v8::Function> foo = CompileFunction( |
| 2928 | &env, |
| 2929 | "function foo() {\n" |
| 2930 | " var a = [];\n" |
| 2931 | " var s = \"\";\n" |
| 2932 | " for (var i = 0; i < 10; i++) {\n" |
| 2933 | " var v = new V(i, i + 1);\n" |
| 2934 | " v.y;\n" |
| 2935 | " a.length;\n" // Special case: array length. |
| 2936 | " s.length;\n" // Special case: string length. |
| 2937 | " }\n" |
| 2938 | "}\n" |
| 2939 | "function V(x, y) {\n" |
| 2940 | " this.x = x;\n" |
| 2941 | " this.y = y;\n" |
| 2942 | "}\n", |
| 2943 | "foo"); |
| 2944 | |
| 2945 | // Call function without any break points to ensure inlining is in place. |
| 2946 | foo->Call(env->Global(), 0, NULL); |
| 2947 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2948 | // Set up break point and step through the function. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2949 | SetBreakPoint(foo, 4); |
| 2950 | step_action = StepNext; |
| 2951 | break_point_hit_count = 0; |
| 2952 | foo->Call(env->Global(), 0, NULL); |
| 2953 | |
| 2954 | // With stepping all break locations are hit. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2955 | CHECK_EQ(65, break_point_hit_count); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2956 | |
| 2957 | v8::Debug::SetDebugEventListener(NULL); |
| 2958 | CheckDebuggerUnloaded(); |
| 2959 | } |
| 2960 | |
| 2961 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2962 | static void DoDebugStepNamedStoreLoop(int expected) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2963 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2964 | v8::HandleScope scope(env->GetIsolate()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2965 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2966 | // Register a debug event listener which steps and counts. |
| 2967 | v8::Debug::SetDebugEventListener(DebugEventStep); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2968 | |
| 2969 | // Create a function for testing stepping of named store. |
| 2970 | v8::Local<v8::Function> foo = CompileFunction( |
| 2971 | &env, |
| 2972 | "function foo() {\n" |
| 2973 | " var a = {a:1};\n" |
| 2974 | " for (var i = 0; i < 10; i++) {\n" |
| 2975 | " a.a = 2\n" |
| 2976 | " }\n" |
| 2977 | "}\n", |
| 2978 | "foo"); |
| 2979 | |
| 2980 | // Call function without any break points to ensure inlining is in place. |
| 2981 | foo->Call(env->Global(), 0, NULL); |
| 2982 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2983 | // Set up break point and step through the function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2984 | SetBreakPoint(foo, 3); |
| 2985 | step_action = StepNext; |
| 2986 | break_point_hit_count = 0; |
| 2987 | foo->Call(env->Global(), 0, NULL); |
| 2988 | |
| 2989 | // With stepping all expected break locations are hit. |
| 2990 | CHECK_EQ(expected, break_point_hit_count); |
| 2991 | |
| 2992 | v8::Debug::SetDebugEventListener(NULL); |
| 2993 | CheckDebuggerUnloaded(); |
| 2994 | } |
| 2995 | |
| 2996 | |
| 2997 | // Test of the stepping mechanism for named load in a loop. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2998 | TEST(DebugStepNamedStoreLoop) { DoDebugStepNamedStoreLoop(34); } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2999 | |
| 3000 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3001 | // Test the stepping mechanism with different ICs. |
| 3002 | TEST(DebugStepLinearMixedICs) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3003 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3004 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3005 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3006 | // Register a debug event listener which steps and counts. |
| 3007 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3008 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3009 | // Create a function for testing stepping. |
| 3010 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 3011 | "function bar() {};" |
| 3012 | "function foo() {" |
| 3013 | " var x;" |
| 3014 | " var index='name';" |
| 3015 | " var y = {};" |
| 3016 | " 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] | 3017 | |
| 3018 | // Run functions to allow them to get optimized. |
| 3019 | CompileRun("a=0; b=0; bar(); foo();"); |
| 3020 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3021 | SetBreakPoint(foo, 0); |
| 3022 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3023 | step_action = StepIn; |
| 3024 | break_point_hit_count = 0; |
| 3025 | foo->Call(env->Global(), 0, NULL); |
| 3026 | |
| 3027 | // With stepping all break locations are hit. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3028 | CHECK_EQ(11, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3029 | |
| 3030 | v8::Debug::SetDebugEventListener(NULL); |
| 3031 | CheckDebuggerUnloaded(); |
| 3032 | |
| 3033 | // Register a debug event listener which just counts. |
| 3034 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3035 | |
| 3036 | SetBreakPoint(foo, 0); |
| 3037 | break_point_hit_count = 0; |
| 3038 | foo->Call(env->Global(), 0, NULL); |
| 3039 | |
| 3040 | // Without stepping only active break points are hit. |
| 3041 | CHECK_EQ(1, break_point_hit_count); |
| 3042 | |
| 3043 | v8::Debug::SetDebugEventListener(NULL); |
| 3044 | CheckDebuggerUnloaded(); |
| 3045 | } |
| 3046 | |
| 3047 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3048 | TEST(DebugStepDeclarations) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3049 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3050 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3051 | |
| 3052 | // Register a debug event listener which steps and counts. |
| 3053 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3054 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3055 | // Create a function for testing stepping. Run it to allow it to get |
| 3056 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3057 | const char* src = "function foo() { " |
| 3058 | " var a;" |
| 3059 | " var b = 1;" |
| 3060 | " var c = foo;" |
| 3061 | " var d = Math.floor;" |
| 3062 | " var e = b + d(1.2);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3063 | "}" |
| 3064 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3065 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3066 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3067 | SetBreakPoint(foo, 0); |
| 3068 | |
| 3069 | // Stepping through the declarations. |
| 3070 | step_action = StepIn; |
| 3071 | break_point_hit_count = 0; |
| 3072 | foo->Call(env->Global(), 0, NULL); |
| 3073 | CHECK_EQ(6, break_point_hit_count); |
| 3074 | |
| 3075 | // Get rid of the debug event listener. |
| 3076 | v8::Debug::SetDebugEventListener(NULL); |
| 3077 | CheckDebuggerUnloaded(); |
| 3078 | } |
| 3079 | |
| 3080 | |
| 3081 | TEST(DebugStepLocals) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3082 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3083 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3084 | |
| 3085 | // Register a debug event listener which steps and counts. |
| 3086 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3087 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3088 | // Create a function for testing stepping. Run it to allow it to get |
| 3089 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3090 | const char* src = "function foo() { " |
| 3091 | " var a,b;" |
| 3092 | " a = 1;" |
| 3093 | " b = a + 2;" |
| 3094 | " b = 1 + 2 + 3;" |
| 3095 | " a = Math.floor(b);" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3096 | "}" |
| 3097 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3098 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3099 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3100 | SetBreakPoint(foo, 0); |
| 3101 | |
| 3102 | // Stepping through the declarations. |
| 3103 | step_action = StepIn; |
| 3104 | break_point_hit_count = 0; |
| 3105 | foo->Call(env->Global(), 0, NULL); |
| 3106 | CHECK_EQ(6, break_point_hit_count); |
| 3107 | |
| 3108 | // Get rid of the debug event listener. |
| 3109 | v8::Debug::SetDebugEventListener(NULL); |
| 3110 | CheckDebuggerUnloaded(); |
| 3111 | } |
| 3112 | |
| 3113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3114 | TEST(DebugStepIf) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3115 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3116 | v8::Isolate* isolate = env->GetIsolate(); |
| 3117 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3118 | |
| 3119 | // Register a debug event listener which steps and counts. |
| 3120 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3121 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3122 | // Create a function for testing stepping. Run it to allow it to get |
| 3123 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3124 | const int argc = 1; |
| 3125 | const char* src = "function foo(x) { " |
| 3126 | " a = 1;" |
| 3127 | " if (x) {" |
| 3128 | " b = 1;" |
| 3129 | " } else {" |
| 3130 | " c = 1;" |
| 3131 | " d = 1;" |
| 3132 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3133 | "}" |
| 3134 | "a=0; b=0; c=0; d=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3135 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3136 | SetBreakPoint(foo, 0); |
| 3137 | |
| 3138 | // Stepping through the true part. |
| 3139 | step_action = StepIn; |
| 3140 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3141 | v8::Handle<v8::Value> argv_true[argc] = { v8::True(isolate) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3142 | foo->Call(env->Global(), argc, argv_true); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3143 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3144 | |
| 3145 | // Stepping through the false part. |
| 3146 | step_action = StepIn; |
| 3147 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3148 | v8::Handle<v8::Value> argv_false[argc] = { v8::False(isolate) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3149 | foo->Call(env->Global(), argc, argv_false); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3150 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3151 | |
| 3152 | // Get rid of the debug event listener. |
| 3153 | v8::Debug::SetDebugEventListener(NULL); |
| 3154 | CheckDebuggerUnloaded(); |
| 3155 | } |
| 3156 | |
| 3157 | |
| 3158 | TEST(DebugStepSwitch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3159 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3160 | v8::Isolate* isolate = env->GetIsolate(); |
| 3161 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Register a debug event listener which steps and counts. |
| 3164 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3165 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3166 | // Create a function for testing stepping. Run it to allow it to get |
| 3167 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3168 | const int argc = 1; |
| 3169 | const char* src = "function foo(x) { " |
| 3170 | " a = 1;" |
| 3171 | " switch (x) {" |
| 3172 | " case 1:" |
| 3173 | " b = 1;" |
| 3174 | " case 2:" |
| 3175 | " c = 1;" |
| 3176 | " break;" |
| 3177 | " case 3:" |
| 3178 | " d = 1;" |
| 3179 | " e = 1;" |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3180 | " f = 1;" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3181 | " break;" |
| 3182 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3183 | "}" |
| 3184 | "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] | 3185 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3186 | SetBreakPoint(foo, 0); |
| 3187 | |
| 3188 | // One case with fall-through. |
| 3189 | step_action = StepIn; |
| 3190 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3191 | v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(isolate, 1) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3192 | foo->Call(env->Global(), argc, argv_1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3193 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3194 | |
| 3195 | // Another case. |
| 3196 | step_action = StepIn; |
| 3197 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3198 | v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(isolate, 2) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3199 | foo->Call(env->Global(), argc, argv_2); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3200 | CHECK_EQ(5, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3201 | |
| 3202 | // Last case. |
| 3203 | step_action = StepIn; |
| 3204 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3205 | v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(isolate, 3) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3206 | foo->Call(env->Global(), argc, argv_3); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3207 | CHECK_EQ(7, break_point_hit_count); |
| 3208 | |
| 3209 | // Get rid of the debug event listener. |
| 3210 | v8::Debug::SetDebugEventListener(NULL); |
| 3211 | CheckDebuggerUnloaded(); |
| 3212 | } |
| 3213 | |
| 3214 | |
| 3215 | TEST(DebugStepWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3216 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3217 | v8::Isolate* isolate = env->GetIsolate(); |
| 3218 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3219 | |
| 3220 | // Register a debug event listener which steps and counts. |
| 3221 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3222 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3223 | // Create a function for testing stepping. Run it to allow it to get |
| 3224 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3225 | const int argc = 1; |
| 3226 | const char* src = "function foo(x) { " |
| 3227 | " var a = 0;" |
| 3228 | " while (a < x) {" |
| 3229 | " a++;" |
| 3230 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3231 | "}" |
| 3232 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3233 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3234 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3235 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3236 | // Looping 0 times. We still should break at the while-condition once. |
| 3237 | step_action = StepIn; |
| 3238 | break_point_hit_count = 0; |
| 3239 | v8::Handle<v8::Value> argv_0[argc] = { v8::Number::New(isolate, 0) }; |
| 3240 | foo->Call(env->Global(), argc, argv_0); |
| 3241 | CHECK_EQ(3, break_point_hit_count); |
| 3242 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3243 | // Looping 10 times. |
| 3244 | step_action = StepIn; |
| 3245 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3246 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(isolate, 10) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3247 | foo->Call(env->Global(), argc, argv_10); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3248 | CHECK_EQ(23, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3249 | |
| 3250 | // Looping 100 times. |
| 3251 | step_action = StepIn; |
| 3252 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3253 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(isolate, 100) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3254 | foo->Call(env->Global(), argc, argv_100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3255 | CHECK_EQ(203, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3256 | |
| 3257 | // Get rid of the debug event listener. |
| 3258 | v8::Debug::SetDebugEventListener(NULL); |
| 3259 | CheckDebuggerUnloaded(); |
| 3260 | } |
| 3261 | |
| 3262 | |
| 3263 | TEST(DebugStepDoWhile) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3264 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3265 | v8::Isolate* isolate = env->GetIsolate(); |
| 3266 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3267 | |
| 3268 | // Register a debug event listener which steps and counts. |
| 3269 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3270 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3271 | // Create a function for testing stepping. Run it to allow it to get |
| 3272 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3273 | const int argc = 1; |
| 3274 | const char* src = "function foo(x) { " |
| 3275 | " var a = 0;" |
| 3276 | " do {" |
| 3277 | " a++;" |
| 3278 | " } while (a < x)" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3279 | "}" |
| 3280 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3281 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3282 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3283 | |
| 3284 | // Looping 10 times. |
| 3285 | step_action = StepIn; |
| 3286 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3287 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(isolate, 10) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3288 | foo->Call(env->Global(), argc, argv_10); |
| 3289 | CHECK_EQ(22, break_point_hit_count); |
| 3290 | |
| 3291 | // Looping 100 times. |
| 3292 | step_action = StepIn; |
| 3293 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3294 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(isolate, 100) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3295 | foo->Call(env->Global(), argc, argv_100); |
| 3296 | CHECK_EQ(202, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Get rid of the debug event listener. |
| 3299 | v8::Debug::SetDebugEventListener(NULL); |
| 3300 | CheckDebuggerUnloaded(); |
| 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | TEST(DebugStepFor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3305 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3306 | v8::Isolate* isolate = env->GetIsolate(); |
| 3307 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3308 | |
| 3309 | // Register a debug event listener which steps and counts. |
| 3310 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3311 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3312 | // Create a function for testing stepping. Run it to allow it to get |
| 3313 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3314 | const int argc = 1; |
| 3315 | const char* src = "function foo(x) { " |
| 3316 | " a = 1;" |
| 3317 | " for (i = 0; i < x; i++) {" |
| 3318 | " b = 1;" |
| 3319 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3320 | "}" |
| 3321 | "a=0; b=0; i=0; foo()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3322 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3323 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3324 | SetBreakPoint(foo, 8); // "a = 1;" |
| 3325 | |
| 3326 | // Looping 10 times. |
| 3327 | step_action = StepIn; |
| 3328 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3329 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(isolate, 10) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3330 | foo->Call(env->Global(), argc, argv_10); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3331 | CHECK_EQ(45, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3332 | |
| 3333 | // Looping 100 times. |
| 3334 | step_action = StepIn; |
| 3335 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3336 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(isolate, 100) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3337 | foo->Call(env->Global(), argc, argv_100); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3338 | CHECK_EQ(405, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3339 | |
| 3340 | // Get rid of the debug event listener. |
| 3341 | v8::Debug::SetDebugEventListener(NULL); |
| 3342 | CheckDebuggerUnloaded(); |
| 3343 | } |
| 3344 | |
| 3345 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3346 | TEST(DebugStepForContinue) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3347 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3348 | v8::Isolate* isolate = env->GetIsolate(); |
| 3349 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3350 | |
| 3351 | // Register a debug event listener which steps and counts. |
| 3352 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3353 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3354 | // Create a function for testing stepping. Run it to allow it to get |
| 3355 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3356 | const int argc = 1; |
| 3357 | const char* src = "function foo(x) { " |
| 3358 | " var a = 0;" |
| 3359 | " var b = 0;" |
| 3360 | " var c = 0;" |
| 3361 | " for (var i = 0; i < x; i++) {" |
| 3362 | " a++;" |
| 3363 | " if (a % 2 == 0) continue;" |
| 3364 | " b++;" |
| 3365 | " c++;" |
| 3366 | " }" |
| 3367 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3368 | "}" |
| 3369 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3370 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3371 | v8::Handle<v8::Value> result; |
| 3372 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3373 | |
| 3374 | // Each loop generates 4 or 5 steps depending on whether a is equal. |
| 3375 | |
| 3376 | // Looping 10 times. |
| 3377 | step_action = StepIn; |
| 3378 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3379 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(isolate, 10) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3380 | result = foo->Call(env->Global(), argc, argv_10); |
| 3381 | CHECK_EQ(5, result->Int32Value()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3382 | CHECK_EQ(62, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3383 | |
| 3384 | // Looping 100 times. |
| 3385 | step_action = StepIn; |
| 3386 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3387 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(isolate, 100) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3388 | result = foo->Call(env->Global(), argc, argv_100); |
| 3389 | CHECK_EQ(50, result->Int32Value()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3390 | CHECK_EQ(557, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3391 | |
| 3392 | // Get rid of the debug event listener. |
| 3393 | v8::Debug::SetDebugEventListener(NULL); |
| 3394 | CheckDebuggerUnloaded(); |
| 3395 | } |
| 3396 | |
| 3397 | |
| 3398 | TEST(DebugStepForBreak) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3399 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3400 | v8::Isolate* isolate = env->GetIsolate(); |
| 3401 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3402 | |
| 3403 | // Register a debug event listener which steps and counts. |
| 3404 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3405 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3406 | // Create a function for testing stepping. Run it to allow it to get |
| 3407 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3408 | const int argc = 1; |
| 3409 | const char* src = "function foo(x) { " |
| 3410 | " var a = 0;" |
| 3411 | " var b = 0;" |
| 3412 | " var c = 0;" |
| 3413 | " for (var i = 0; i < 1000; i++) {" |
| 3414 | " a++;" |
| 3415 | " if (a == x) break;" |
| 3416 | " b++;" |
| 3417 | " c++;" |
| 3418 | " }" |
| 3419 | " return b;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3420 | "}" |
| 3421 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3422 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3423 | v8::Handle<v8::Value> result; |
| 3424 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3425 | |
| 3426 | // Each loop generates 5 steps except for the last (when break is executed) |
| 3427 | // which only generates 4. |
| 3428 | |
| 3429 | // Looping 10 times. |
| 3430 | step_action = StepIn; |
| 3431 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3432 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(isolate, 10) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3433 | result = foo->Call(env->Global(), argc, argv_10); |
| 3434 | CHECK_EQ(9, result->Int32Value()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3435 | CHECK_EQ(64, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3436 | |
| 3437 | // Looping 100 times. |
| 3438 | step_action = StepIn; |
| 3439 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3440 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(isolate, 100) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3441 | result = foo->Call(env->Global(), argc, argv_100); |
| 3442 | CHECK_EQ(99, result->Int32Value()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3443 | CHECK_EQ(604, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3444 | |
| 3445 | // Get rid of the debug event listener. |
| 3446 | v8::Debug::SetDebugEventListener(NULL); |
| 3447 | CheckDebuggerUnloaded(); |
| 3448 | } |
| 3449 | |
| 3450 | |
| 3451 | TEST(DebugStepForIn) { |
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::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3454 | |
| 3455 | // Register a debug event listener which steps and counts. |
| 3456 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3457 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3458 | // Create a function for testing stepping. Run it to allow it to get |
| 3459 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3460 | v8::Local<v8::Function> foo; |
| 3461 | const char* src_1 = "function foo() { " |
| 3462 | " var a = [1, 2];" |
| 3463 | " for (x in a) {" |
| 3464 | " b = 0;" |
| 3465 | " }" |
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 | foo = CompileFunction(&env, src_1, "foo"); |
| 3469 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3470 | |
| 3471 | step_action = StepIn; |
| 3472 | break_point_hit_count = 0; |
| 3473 | foo->Call(env->Global(), 0, NULL); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3474 | CHECK_EQ(8, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3475 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3476 | // Create a function for testing stepping. Run it to allow it to get |
| 3477 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3478 | const char* src_2 = "function foo() { " |
| 3479 | " var a = {a:[1, 2, 3]};" |
| 3480 | " for (x in a.a) {" |
| 3481 | " b = 0;" |
| 3482 | " }" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3483 | "}" |
| 3484 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3485 | foo = CompileFunction(&env, src_2, "foo"); |
| 3486 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3487 | |
| 3488 | step_action = StepIn; |
| 3489 | break_point_hit_count = 0; |
| 3490 | foo->Call(env->Global(), 0, NULL); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3491 | CHECK_EQ(10, break_point_hit_count); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3492 | |
| 3493 | // Get rid of the debug event listener. |
| 3494 | v8::Debug::SetDebugEventListener(NULL); |
| 3495 | CheckDebuggerUnloaded(); |
| 3496 | } |
| 3497 | |
| 3498 | |
| 3499 | TEST(DebugStepWith) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3500 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3501 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3502 | |
| 3503 | // Register a debug event listener which steps and counts. |
| 3504 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3505 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3506 | // Create a function for testing stepping. Run it to allow it to get |
| 3507 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3508 | const char* src = "function foo(x) { " |
| 3509 | " var a = {};" |
| 3510 | " with (a) {}" |
| 3511 | " with (b) {}" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3512 | "}" |
| 3513 | "foo()"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3514 | env->Global()->Set(v8::String::NewFromUtf8(env->GetIsolate(), "b"), |
| 3515 | v8::Object::New(env->GetIsolate())); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3516 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3517 | v8::Handle<v8::Value> result; |
| 3518 | SetBreakPoint(foo, 8); // "var a = {};" |
| 3519 | |
| 3520 | step_action = StepIn; |
| 3521 | break_point_hit_count = 0; |
| 3522 | foo->Call(env->Global(), 0, NULL); |
| 3523 | CHECK_EQ(4, break_point_hit_count); |
| 3524 | |
| 3525 | // Get rid of the debug event listener. |
| 3526 | v8::Debug::SetDebugEventListener(NULL); |
| 3527 | CheckDebuggerUnloaded(); |
| 3528 | } |
| 3529 | |
| 3530 | |
| 3531 | TEST(DebugConditional) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3532 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3533 | v8::Isolate* isolate = env->GetIsolate(); |
| 3534 | v8::HandleScope scope(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3535 | |
| 3536 | // Register a debug event listener which steps and counts. |
| 3537 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3538 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3539 | // Create a function for testing stepping. Run it to allow it to get |
| 3540 | // optimized. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3541 | const char* src = "function foo(x) { " |
| 3542 | " var a;" |
| 3543 | " a = x ? 1 : 2;" |
| 3544 | " return a;" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3545 | "}" |
| 3546 | "foo()"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3547 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3548 | SetBreakPoint(foo, 0); // "var a;" |
| 3549 | |
| 3550 | step_action = StepIn; |
| 3551 | break_point_hit_count = 0; |
| 3552 | foo->Call(env->Global(), 0, NULL); |
| 3553 | CHECK_EQ(5, break_point_hit_count); |
| 3554 | |
| 3555 | step_action = StepIn; |
| 3556 | break_point_hit_count = 0; |
| 3557 | const int argc = 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3558 | v8::Handle<v8::Value> argv_true[argc] = { v8::True(isolate) }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3559 | foo->Call(env->Global(), argc, argv_true); |
| 3560 | CHECK_EQ(5, break_point_hit_count); |
| 3561 | |
| 3562 | // Get rid of the debug event listener. |
| 3563 | v8::Debug::SetDebugEventListener(NULL); |
| 3564 | CheckDebuggerUnloaded(); |
| 3565 | } |
| 3566 | |
| 3567 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3568 | TEST(StepInOutSimple) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3569 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3570 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3571 | |
| 3572 | // Create a function for checking the function when hitting a break point. |
| 3573 | frame_function_name = CompileFunction(&env, |
| 3574 | frame_function_name_source, |
| 3575 | "frame_function_name"); |
| 3576 | |
| 3577 | // Register a debug event listener which steps and counts. |
| 3578 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
| 3579 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3580 | // Create a function for testing stepping. Run it to allow it to get |
| 3581 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3582 | const char* src = "function a() {b();c();}; " |
| 3583 | "function b() {c();}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3584 | "function c() {}; " |
| 3585 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3586 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3587 | SetBreakPoint(a, 0); |
| 3588 | |
| 3589 | // Step through invocation of a with step in. |
| 3590 | step_action = StepIn; |
| 3591 | break_point_hit_count = 0; |
| 3592 | expected_step_sequence = "abcbaca"; |
| 3593 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3594 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3595 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3596 | |
| 3597 | // Step through invocation of a with step next. |
| 3598 | step_action = StepNext; |
| 3599 | break_point_hit_count = 0; |
| 3600 | expected_step_sequence = "aaa"; |
| 3601 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3602 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3603 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Step through invocation of a with step out. |
| 3606 | step_action = StepOut; |
| 3607 | break_point_hit_count = 0; |
| 3608 | expected_step_sequence = "a"; |
| 3609 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3610 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3611 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3612 | |
| 3613 | // Get rid of the debug event listener. |
| 3614 | v8::Debug::SetDebugEventListener(NULL); |
| 3615 | CheckDebuggerUnloaded(); |
| 3616 | } |
| 3617 | |
| 3618 | |
| 3619 | TEST(StepInOutTree) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3620 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3621 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Create a function for checking the function when hitting a break point. |
| 3624 | frame_function_name = CompileFunction(&env, |
| 3625 | frame_function_name_source, |
| 3626 | "frame_function_name"); |
| 3627 | |
| 3628 | // Register a debug event listener which steps and counts. |
| 3629 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
| 3630 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3631 | // Create a function for testing stepping. Run it to allow it to get |
| 3632 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3633 | const char* src = "function a() {b(c(d()),d());c(d());d()}; " |
| 3634 | "function b(x,y) {c();}; " |
| 3635 | "function c(x) {}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3636 | "function d() {}; " |
| 3637 | "a(); b(); c(); d()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3638 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3639 | SetBreakPoint(a, 0); |
| 3640 | |
| 3641 | // Step through invocation of a with step in. |
| 3642 | step_action = StepIn; |
| 3643 | break_point_hit_count = 0; |
| 3644 | expected_step_sequence = "adacadabcbadacada"; |
| 3645 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3646 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3647 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3648 | |
| 3649 | // Step through invocation of a with step next. |
| 3650 | step_action = StepNext; |
| 3651 | break_point_hit_count = 0; |
| 3652 | expected_step_sequence = "aaaa"; |
| 3653 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3654 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3655 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3656 | |
| 3657 | // Step through invocation of a with step out. |
| 3658 | step_action = StepOut; |
| 3659 | break_point_hit_count = 0; |
| 3660 | expected_step_sequence = "a"; |
| 3661 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3662 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3663 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3664 | |
| 3665 | // Get rid of the debug event listener. |
| 3666 | v8::Debug::SetDebugEventListener(NULL); |
| 3667 | CheckDebuggerUnloaded(true); |
| 3668 | } |
| 3669 | |
| 3670 | |
| 3671 | TEST(StepInOutBranch) { |
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 checking the function when hitting a break point. |
| 3676 | frame_function_name = CompileFunction(&env, |
| 3677 | frame_function_name_source, |
| 3678 | "frame_function_name"); |
| 3679 | |
| 3680 | // Register a debug event listener which steps and counts. |
| 3681 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
| 3682 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3683 | // Create a function for testing stepping. Run it to allow it to get |
| 3684 | // optimized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3685 | const char* src = "function a() {b(false);c();}; " |
| 3686 | "function b(x) {if(x){c();};}; " |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3687 | "function c() {}; " |
| 3688 | "a(); b(); c()"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3689 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3690 | SetBreakPoint(a, 0); |
| 3691 | |
| 3692 | // Step through invocation of a. |
| 3693 | step_action = StepIn; |
| 3694 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3695 | expected_step_sequence = "abbaca"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3696 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3697 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3698 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3699 | |
| 3700 | // Get rid of the debug event listener. |
| 3701 | v8::Debug::SetDebugEventListener(NULL); |
| 3702 | CheckDebuggerUnloaded(); |
| 3703 | } |
| 3704 | |
| 3705 | |
| 3706 | // Test that step in does not step into native functions. |
| 3707 | TEST(DebugStepNatives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3708 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3709 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3710 | |
| 3711 | // Create a function for testing stepping. |
| 3712 | v8::Local<v8::Function> foo = CompileFunction( |
| 3713 | &env, |
| 3714 | "function foo(){debugger;Math.sin(1);}", |
| 3715 | "foo"); |
| 3716 | |
| 3717 | // Register a debug event listener which steps and counts. |
| 3718 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3719 | |
| 3720 | step_action = StepIn; |
| 3721 | break_point_hit_count = 0; |
| 3722 | foo->Call(env->Global(), 0, NULL); |
| 3723 | |
| 3724 | // With stepping all break locations are hit. |
| 3725 | CHECK_EQ(3, break_point_hit_count); |
| 3726 | |
| 3727 | v8::Debug::SetDebugEventListener(NULL); |
| 3728 | CheckDebuggerUnloaded(); |
| 3729 | |
| 3730 | // Register a debug event listener which just counts. |
| 3731 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3732 | |
| 3733 | break_point_hit_count = 0; |
| 3734 | foo->Call(env->Global(), 0, NULL); |
| 3735 | |
| 3736 | // Without stepping only active break points are hit. |
| 3737 | CHECK_EQ(1, break_point_hit_count); |
| 3738 | |
| 3739 | v8::Debug::SetDebugEventListener(NULL); |
| 3740 | CheckDebuggerUnloaded(); |
| 3741 | } |
| 3742 | |
| 3743 | |
| 3744 | // Test that step in works with function.apply. |
| 3745 | TEST(DebugStepFunctionApply) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3746 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3747 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Create a function for testing stepping. |
| 3750 | v8::Local<v8::Function> foo = CompileFunction( |
| 3751 | &env, |
| 3752 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3753 | "function foo(){ debugger; bar.apply(this, [1,2,3]); }", |
| 3754 | "foo"); |
| 3755 | |
| 3756 | // Register a debug event listener which steps and counts. |
| 3757 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3758 | |
| 3759 | step_action = StepIn; |
| 3760 | break_point_hit_count = 0; |
| 3761 | foo->Call(env->Global(), 0, NULL); |
| 3762 | |
| 3763 | // With stepping all break locations are hit. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3764 | CHECK_EQ(7, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3765 | |
| 3766 | v8::Debug::SetDebugEventListener(NULL); |
| 3767 | CheckDebuggerUnloaded(); |
| 3768 | |
| 3769 | // Register a debug event listener which just counts. |
| 3770 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3771 | |
| 3772 | break_point_hit_count = 0; |
| 3773 | foo->Call(env->Global(), 0, NULL); |
| 3774 | |
| 3775 | // Without stepping only the debugger statement is hit. |
| 3776 | CHECK_EQ(1, break_point_hit_count); |
| 3777 | |
| 3778 | v8::Debug::SetDebugEventListener(NULL); |
| 3779 | CheckDebuggerUnloaded(); |
| 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | // Test that step in works with function.call. |
| 3784 | TEST(DebugStepFunctionCall) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3785 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3786 | v8::Isolate* isolate = env->GetIsolate(); |
| 3787 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3788 | |
| 3789 | // Create a function for testing stepping. |
| 3790 | v8::Local<v8::Function> foo = CompileFunction( |
| 3791 | &env, |
| 3792 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3793 | "function foo(a){ debugger;" |
| 3794 | " if (a) {" |
| 3795 | " bar.call(this, 1, 2, 3);" |
| 3796 | " } else {" |
| 3797 | " bar.call(this, 0);" |
| 3798 | " }" |
| 3799 | "}", |
| 3800 | "foo"); |
| 3801 | |
| 3802 | // Register a debug event listener which steps and counts. |
| 3803 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3804 | step_action = StepIn; |
| 3805 | |
| 3806 | // Check stepping where the if condition in bar is false. |
| 3807 | break_point_hit_count = 0; |
| 3808 | foo->Call(env->Global(), 0, NULL); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3809 | CHECK_EQ(6, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3810 | |
| 3811 | // Check stepping where the if condition in bar is true. |
| 3812 | break_point_hit_count = 0; |
| 3813 | const int argc = 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3814 | v8::Handle<v8::Value> argv[argc] = { v8::True(isolate) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3815 | foo->Call(env->Global(), argc, argv); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3816 | CHECK_EQ(8, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3817 | |
| 3818 | v8::Debug::SetDebugEventListener(NULL); |
| 3819 | CheckDebuggerUnloaded(); |
| 3820 | |
| 3821 | // Register a debug event listener which just counts. |
| 3822 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3823 | |
| 3824 | break_point_hit_count = 0; |
| 3825 | foo->Call(env->Global(), 0, NULL); |
| 3826 | |
| 3827 | // Without stepping only the debugger statement is hit. |
| 3828 | CHECK_EQ(1, break_point_hit_count); |
| 3829 | |
| 3830 | v8::Debug::SetDebugEventListener(NULL); |
| 3831 | CheckDebuggerUnloaded(); |
| 3832 | } |
| 3833 | |
| 3834 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3835 | // Tests that breakpoint will be hit if it's set in script. |
| 3836 | TEST(PauseInScript) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3837 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3838 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3839 | env.ExposeDebug(); |
| 3840 | |
| 3841 | // Register a debug event listener which counts. |
| 3842 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 3843 | |
| 3844 | // Create a script that returns a function. |
| 3845 | const char* src = "(function (evt) {})"; |
| 3846 | const char* script_name = "StepInHandlerTest"; |
| 3847 | |
| 3848 | // Set breakpoint in the script. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3849 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), script_name, 0, -1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3850 | break_point_hit_count = 0; |
| 3851 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3852 | v8::ScriptOrigin origin( |
| 3853 | v8::String::NewFromUtf8(env->GetIsolate(), script_name), |
| 3854 | v8::Integer::New(env->GetIsolate(), 0)); |
| 3855 | v8::Handle<v8::Script> script = v8::Script::Compile( |
| 3856 | v8::String::NewFromUtf8(env->GetIsolate(), src), &origin); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3857 | v8::Local<v8::Value> r = script->Run(); |
| 3858 | |
| 3859 | CHECK(r->IsFunction()); |
| 3860 | CHECK_EQ(1, break_point_hit_count); |
| 3861 | |
| 3862 | // Get rid of the debug event listener. |
| 3863 | v8::Debug::SetDebugEventListener(NULL); |
| 3864 | CheckDebuggerUnloaded(); |
| 3865 | } |
| 3866 | |
| 3867 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3868 | // Test break on exceptions. For each exception break combination the number |
| 3869 | // of debug event exception callbacks and message callbacks are collected. The |
| 3870 | // number of debug event exception callbacks are used to check that the |
| 3871 | // debugger is called correctly and the number of message callbacks is used to |
| 3872 | // check that uncaught exceptions are still returned even if there is a break |
| 3873 | // for them. |
| 3874 | TEST(BreakOnException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3875 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3876 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3877 | env.ExposeDebug(); |
| 3878 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3879 | // Create functions for testing break on exception. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3880 | CompileFunction(&env, "function throws(){throw 1;}", "throws"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3881 | v8::Local<v8::Function> caught = |
| 3882 | CompileFunction(&env, |
| 3883 | "function caught(){try {throws();} catch(e) {};}", |
| 3884 | "caught"); |
| 3885 | v8::Local<v8::Function> notCaught = |
| 3886 | CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); |
| 3887 | |
| 3888 | v8::V8::AddMessageListener(MessageCallbackCount); |
| 3889 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 3890 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 3891 | // Initial state should be no break on exceptions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3892 | DebugEventCounterClear(); |
| 3893 | MessageCallbackCountClear(); |
| 3894 | caught->Call(env->Global(), 0, NULL); |
| 3895 | CHECK_EQ(0, exception_hit_count); |
| 3896 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3897 | CHECK_EQ(0, message_callback_count); |
| 3898 | notCaught->Call(env->Global(), 0, NULL); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 3899 | CHECK_EQ(0, exception_hit_count); |
| 3900 | CHECK_EQ(0, uncaught_exception_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3901 | CHECK_EQ(1, message_callback_count); |
| 3902 | |
| 3903 | // No break on exception |
| 3904 | DebugEventCounterClear(); |
| 3905 | MessageCallbackCountClear(); |
| 3906 | ChangeBreakOnException(false, false); |
| 3907 | caught->Call(env->Global(), 0, NULL); |
| 3908 | CHECK_EQ(0, exception_hit_count); |
| 3909 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3910 | CHECK_EQ(0, message_callback_count); |
| 3911 | notCaught->Call(env->Global(), 0, NULL); |
| 3912 | CHECK_EQ(0, exception_hit_count); |
| 3913 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3914 | CHECK_EQ(1, message_callback_count); |
| 3915 | |
| 3916 | // Break on uncaught exception |
| 3917 | DebugEventCounterClear(); |
| 3918 | MessageCallbackCountClear(); |
| 3919 | ChangeBreakOnException(false, true); |
| 3920 | caught->Call(env->Global(), 0, NULL); |
| 3921 | CHECK_EQ(0, exception_hit_count); |
| 3922 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3923 | CHECK_EQ(0, message_callback_count); |
| 3924 | notCaught->Call(env->Global(), 0, NULL); |
| 3925 | CHECK_EQ(1, exception_hit_count); |
| 3926 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3927 | CHECK_EQ(1, message_callback_count); |
| 3928 | |
| 3929 | // Break on exception and uncaught exception |
| 3930 | DebugEventCounterClear(); |
| 3931 | MessageCallbackCountClear(); |
| 3932 | ChangeBreakOnException(true, true); |
| 3933 | caught->Call(env->Global(), 0, NULL); |
| 3934 | CHECK_EQ(1, exception_hit_count); |
| 3935 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3936 | CHECK_EQ(0, message_callback_count); |
| 3937 | notCaught->Call(env->Global(), 0, NULL); |
| 3938 | CHECK_EQ(2, exception_hit_count); |
| 3939 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3940 | CHECK_EQ(1, message_callback_count); |
| 3941 | |
| 3942 | // Break on exception |
| 3943 | DebugEventCounterClear(); |
| 3944 | MessageCallbackCountClear(); |
| 3945 | ChangeBreakOnException(true, false); |
| 3946 | caught->Call(env->Global(), 0, NULL); |
| 3947 | CHECK_EQ(1, exception_hit_count); |
| 3948 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3949 | CHECK_EQ(0, message_callback_count); |
| 3950 | notCaught->Call(env->Global(), 0, NULL); |
| 3951 | CHECK_EQ(2, exception_hit_count); |
| 3952 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3953 | CHECK_EQ(1, message_callback_count); |
| 3954 | |
| 3955 | // No break on exception using JavaScript |
| 3956 | DebugEventCounterClear(); |
| 3957 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3958 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3959 | caught->Call(env->Global(), 0, NULL); |
| 3960 | CHECK_EQ(0, exception_hit_count); |
| 3961 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3962 | CHECK_EQ(0, message_callback_count); |
| 3963 | notCaught->Call(env->Global(), 0, NULL); |
| 3964 | CHECK_EQ(0, exception_hit_count); |
| 3965 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3966 | CHECK_EQ(1, message_callback_count); |
| 3967 | |
| 3968 | // Break on uncaught exception using JavaScript |
| 3969 | DebugEventCounterClear(); |
| 3970 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3971 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3972 | caught->Call(env->Global(), 0, NULL); |
| 3973 | CHECK_EQ(0, exception_hit_count); |
| 3974 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3975 | CHECK_EQ(0, message_callback_count); |
| 3976 | notCaught->Call(env->Global(), 0, NULL); |
| 3977 | CHECK_EQ(1, exception_hit_count); |
| 3978 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3979 | CHECK_EQ(1, message_callback_count); |
| 3980 | |
| 3981 | // Break on exception and uncaught exception using JavaScript |
| 3982 | DebugEventCounterClear(); |
| 3983 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3984 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3985 | caught->Call(env->Global(), 0, NULL); |
| 3986 | CHECK_EQ(1, exception_hit_count); |
| 3987 | CHECK_EQ(0, message_callback_count); |
| 3988 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3989 | notCaught->Call(env->Global(), 0, NULL); |
| 3990 | CHECK_EQ(2, exception_hit_count); |
| 3991 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3992 | CHECK_EQ(1, message_callback_count); |
| 3993 | |
| 3994 | // Break on exception using JavaScript |
| 3995 | DebugEventCounterClear(); |
| 3996 | MessageCallbackCountClear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3997 | ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3998 | caught->Call(env->Global(), 0, NULL); |
| 3999 | CHECK_EQ(1, exception_hit_count); |
| 4000 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 4001 | CHECK_EQ(0, message_callback_count); |
| 4002 | notCaught->Call(env->Global(), 0, NULL); |
| 4003 | CHECK_EQ(2, exception_hit_count); |
| 4004 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4005 | CHECK_EQ(1, message_callback_count); |
| 4006 | |
| 4007 | v8::Debug::SetDebugEventListener(NULL); |
| 4008 | CheckDebuggerUnloaded(); |
| 4009 | v8::V8::RemoveMessageListeners(MessageCallbackCount); |
| 4010 | } |
| 4011 | |
| 4012 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4013 | TEST(EvalJSInDebugEventListenerOnNativeReThrownException) { |
| 4014 | DebugLocalContext env; |
| 4015 | v8::HandleScope scope(env->GetIsolate()); |
| 4016 | env.ExposeDebug(); |
| 4017 | |
| 4018 | // Create functions for testing break on exception. |
| 4019 | v8::Local<v8::Function> noThrowJS = CompileFunction( |
| 4020 | &env, "function noThrowJS(){var a=[1]; a.push(2); return a.length;}", |
| 4021 | "noThrowJS"); |
| 4022 | |
| 4023 | debug_event_listener_callback = noThrowJS; |
| 4024 | debug_event_listener_callback_result = 2; |
| 4025 | |
| 4026 | v8::V8::AddMessageListener(MessageCallbackCount); |
| 4027 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4028 | // Break on uncaught exception |
| 4029 | ChangeBreakOnException(false, true); |
| 4030 | DebugEventCounterClear(); |
| 4031 | MessageCallbackCountClear(); |
| 4032 | |
| 4033 | // ReThrow native error |
| 4034 | { |
| 4035 | v8::TryCatch tryCatch; |
| 4036 | env->GetIsolate()->ThrowException(v8::Exception::TypeError( |
| 4037 | v8::String::NewFromUtf8(env->GetIsolate(), "Type error"))); |
| 4038 | CHECK(tryCatch.HasCaught()); |
| 4039 | tryCatch.ReThrow(); |
| 4040 | } |
| 4041 | CHECK_EQ(1, exception_hit_count); |
| 4042 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4043 | CHECK_EQ(0, message_callback_count); // FIXME: Should it be 1 ? |
| 4044 | CHECK(!debug_event_listener_callback.IsEmpty()); |
| 4045 | |
| 4046 | debug_event_listener_callback.Clear(); |
| 4047 | } |
| 4048 | |
| 4049 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4050 | // Test break on exception from compiler errors. When compiling using |
| 4051 | // v8::Script::Compile there is no JavaScript stack whereas when compiling using |
| 4052 | // eval there are JavaScript frames. |
| 4053 | TEST(BreakOnCompileException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4054 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4055 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4056 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4057 | // For this test, we want to break on uncaught exceptions: |
| 4058 | ChangeBreakOnException(false, true); |
| 4059 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4060 | // Create a function for checking the function when hitting a break point. |
| 4061 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 4062 | |
| 4063 | v8::V8::AddMessageListener(MessageCallbackCount); |
| 4064 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4065 | |
| 4066 | DebugEventCounterClear(); |
| 4067 | MessageCallbackCountClear(); |
| 4068 | |
| 4069 | // Check initial state. |
| 4070 | CHECK_EQ(0, exception_hit_count); |
| 4071 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 4072 | CHECK_EQ(0, message_callback_count); |
| 4073 | CHECK_EQ(-1, last_js_stack_height); |
| 4074 | |
| 4075 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4076 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "+++")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4077 | CHECK_EQ(1, exception_hit_count); |
| 4078 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4079 | CHECK_EQ(1, message_callback_count); |
| 4080 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4081 | |
| 4082 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4083 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "x x")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4084 | CHECK_EQ(2, exception_hit_count); |
| 4085 | CHECK_EQ(2, uncaught_exception_hit_count); |
| 4086 | CHECK_EQ(2, message_callback_count); |
| 4087 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4088 | |
| 4089 | // Throws SyntaxError: Unexpected end of input |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4090 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "eval('+++')")) |
| 4091 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4092 | CHECK_EQ(3, exception_hit_count); |
| 4093 | CHECK_EQ(3, uncaught_exception_hit_count); |
| 4094 | CHECK_EQ(3, message_callback_count); |
| 4095 | CHECK_EQ(1, last_js_stack_height); |
| 4096 | |
| 4097 | // Throws SyntaxError: Unexpected identifier |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4098 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "eval('x x')")) |
| 4099 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4100 | CHECK_EQ(4, exception_hit_count); |
| 4101 | CHECK_EQ(4, uncaught_exception_hit_count); |
| 4102 | CHECK_EQ(4, message_callback_count); |
| 4103 | CHECK_EQ(1, last_js_stack_height); |
| 4104 | } |
| 4105 | |
| 4106 | |
| 4107 | TEST(StepWithException) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4108 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4109 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4110 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4111 | // For this test, we want to break on uncaught exceptions: |
| 4112 | ChangeBreakOnException(false, true); |
| 4113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4114 | // Create a function for checking the function when hitting a break point. |
| 4115 | frame_function_name = CompileFunction(&env, |
| 4116 | frame_function_name_source, |
| 4117 | "frame_function_name"); |
| 4118 | |
| 4119 | // Register a debug event listener which steps and counts. |
| 4120 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
| 4121 | |
| 4122 | // Create functions for testing stepping. |
| 4123 | const char* src = "function a() { n(); }; " |
| 4124 | "function b() { c(); }; " |
| 4125 | "function c() { n(); }; " |
| 4126 | "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " |
| 4127 | "function e() { n(); }; " |
| 4128 | "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " |
| 4129 | "function g() { h(); }; " |
| 4130 | "function h() { x = 1; throw 1; }; "; |
| 4131 | |
| 4132 | // Step through invocation of a. |
| 4133 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 4134 | SetBreakPoint(a, 0); |
| 4135 | step_action = StepIn; |
| 4136 | break_point_hit_count = 0; |
| 4137 | expected_step_sequence = "aa"; |
| 4138 | a->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4139 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4140 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4141 | |
| 4142 | // Step through invocation of b + c. |
| 4143 | v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); |
| 4144 | SetBreakPoint(b, 0); |
| 4145 | step_action = StepIn; |
| 4146 | break_point_hit_count = 0; |
| 4147 | expected_step_sequence = "bcc"; |
| 4148 | b->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4149 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4150 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4151 | // Step through invocation of d + e. |
| 4152 | v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); |
| 4153 | SetBreakPoint(d, 0); |
| 4154 | ChangeBreakOnException(false, true); |
| 4155 | step_action = StepIn; |
| 4156 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4157 | expected_step_sequence = "ddedd"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4158 | d->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4159 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4160 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4161 | |
| 4162 | // Step through invocation of d + e now with break on caught exceptions. |
| 4163 | ChangeBreakOnException(true, true); |
| 4164 | step_action = StepIn; |
| 4165 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4166 | expected_step_sequence = "ddeedd"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4167 | d->Call(env->Global(), 0, NULL); |
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 f + g + h. |
| 4172 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4173 | SetBreakPoint(f, 0); |
| 4174 | ChangeBreakOnException(false, true); |
| 4175 | step_action = StepIn; |
| 4176 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4177 | expected_step_sequence = "ffghhff"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4178 | f->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4179 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4180 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4181 | |
| 4182 | // Step through invocation of f + g + h now with break on caught exceptions. |
| 4183 | ChangeBreakOnException(true, true); |
| 4184 | step_action = StepIn; |
| 4185 | break_point_hit_count = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4186 | expected_step_sequence = "ffghhhff"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4187 | f->Call(env->Global(), 0, NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4188 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4189 | break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4190 | |
| 4191 | // Get rid of the debug event listener. |
| 4192 | v8::Debug::SetDebugEventListener(NULL); |
| 4193 | CheckDebuggerUnloaded(); |
| 4194 | } |
| 4195 | |
| 4196 | |
| 4197 | TEST(DebugBreak) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4198 | i::FLAG_stress_compaction = false; |
| 4199 | #ifdef VERIFY_HEAP |
| 4200 | i::FLAG_verify_heap = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4201 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4202 | DebugLocalContext env; |
| 4203 | v8::Isolate* isolate = env->GetIsolate(); |
| 4204 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4205 | |
| 4206 | // Register a debug event listener which sets the break flag and counts. |
| 4207 | v8::Debug::SetDebugEventListener(DebugEventBreak); |
| 4208 | |
| 4209 | // Create a function for testing stepping. |
| 4210 | const char* src = "function f0() {}" |
| 4211 | "function f1(x1) {}" |
| 4212 | "function f2(x1,x2) {}" |
| 4213 | "function f3(x1,x2,x3) {}"; |
| 4214 | v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); |
| 4215 | v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); |
| 4216 | v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); |
| 4217 | v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); |
| 4218 | |
| 4219 | // Call the function to make sure it is compiled. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4220 | v8::Handle<v8::Value> argv[] = { v8::Number::New(isolate, 1), |
| 4221 | v8::Number::New(isolate, 1), |
| 4222 | v8::Number::New(isolate, 1), |
| 4223 | v8::Number::New(isolate, 1) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | |
| 4225 | // Call all functions to make sure that they are compiled. |
| 4226 | f0->Call(env->Global(), 0, NULL); |
| 4227 | f1->Call(env->Global(), 0, NULL); |
| 4228 | f2->Call(env->Global(), 0, NULL); |
| 4229 | f3->Call(env->Global(), 0, NULL); |
| 4230 | |
| 4231 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4232 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4233 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4234 | |
| 4235 | // Call all functions with different argument count. |
| 4236 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4237 | for (unsigned int i = 0; i < arraysize(argv); i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4238 | f0->Call(env->Global(), i, argv); |
| 4239 | f1->Call(env->Global(), i, argv); |
| 4240 | f2->Call(env->Global(), i, argv); |
| 4241 | f3->Call(env->Global(), i, argv); |
| 4242 | } |
| 4243 | |
| 4244 | // One break for each function called. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4245 | CHECK_EQ(4 * arraysize(argv), break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4246 | |
| 4247 | // Get rid of the debug event listener. |
| 4248 | v8::Debug::SetDebugEventListener(NULL); |
| 4249 | CheckDebuggerUnloaded(); |
| 4250 | } |
| 4251 | |
| 4252 | |
| 4253 | // Test to ensure that JavaScript code keeps running while the debug break |
| 4254 | // through the stack limit flag is set but breaks are disabled. |
| 4255 | TEST(DisableBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4256 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4257 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4258 | |
| 4259 | // Register a debug event listener which sets the break flag and counts. |
| 4260 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4261 | |
| 4262 | // Create a function for testing stepping. |
| 4263 | const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; |
| 4264 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4265 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4266 | // Set, test and cancel debug break. |
| 4267 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4268 | CHECK(v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4269 | v8::Debug::CancelDebugBreak(env->GetIsolate()); |
| 4270 | CHECK(!v8::Debug::CheckDebugBreak(env->GetIsolate())); |
| 4271 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4272 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4273 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4274 | |
| 4275 | // Call all functions with different argument count. |
| 4276 | break_point_hit_count = 0; |
| 4277 | f->Call(env->Global(), 0, NULL); |
| 4278 | CHECK_EQ(1, break_point_hit_count); |
| 4279 | |
| 4280 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4281 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 4282 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); |
| 4283 | v8::internal::DisableBreak disable_break(isolate->debug(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4284 | f->Call(env->Global(), 0, NULL); |
| 4285 | CHECK_EQ(1, break_point_hit_count); |
| 4286 | } |
| 4287 | |
| 4288 | f->Call(env->Global(), 0, NULL); |
| 4289 | CHECK_EQ(2, break_point_hit_count); |
| 4290 | |
| 4291 | // Get rid of the debug event listener. |
| 4292 | v8::Debug::SetDebugEventListener(NULL); |
| 4293 | CheckDebuggerUnloaded(); |
| 4294 | } |
| 4295 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4296 | static const char* kSimpleExtensionSource = |
| 4297 | "(function Foo() {" |
| 4298 | " return 4;" |
| 4299 | "})() "; |
| 4300 | |
| 4301 | // http://crbug.com/28933 |
| 4302 | // Test that debug break is disabled when bootstrapper is active. |
| 4303 | TEST(NoBreakWhenBootstrapping) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4304 | v8::Isolate* isolate = CcTest::isolate(); |
| 4305 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Register a debug event listener which sets the break flag and counts. |
| 4308 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4309 | |
| 4310 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4311 | v8::Debug::DebugBreak(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4312 | break_point_hit_count = 0; |
| 4313 | { |
| 4314 | // Create a context with an extension to make sure that some JavaScript |
| 4315 | // code is executed during bootstrapping. |
| 4316 | v8::RegisterExtension(new v8::Extension("simpletest", |
| 4317 | kSimpleExtensionSource)); |
| 4318 | const char* extension_names[] = { "simpletest" }; |
| 4319 | v8::ExtensionConfiguration extensions(1, extension_names); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4320 | v8::HandleScope handle_scope(isolate); |
| 4321 | v8::Context::New(isolate, &extensions); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4322 | } |
| 4323 | // Check that no DebugBreak events occured during the context creation. |
| 4324 | CHECK_EQ(0, break_point_hit_count); |
| 4325 | |
| 4326 | // Get rid of the debug event listener. |
| 4327 | v8::Debug::SetDebugEventListener(NULL); |
| 4328 | CheckDebuggerUnloaded(); |
| 4329 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4330 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4331 | |
| 4332 | static void NamedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
| 4333 | v8::Handle<v8::Array> result = v8::Array::New(info.GetIsolate(), 3); |
| 4334 | result->Set(v8::Integer::New(info.GetIsolate(), 0), |
| 4335 | v8::String::NewFromUtf8(info.GetIsolate(), "a")); |
| 4336 | result->Set(v8::Integer::New(info.GetIsolate(), 1), |
| 4337 | v8::String::NewFromUtf8(info.GetIsolate(), "b")); |
| 4338 | result->Set(v8::Integer::New(info.GetIsolate(), 2), |
| 4339 | v8::String::NewFromUtf8(info.GetIsolate(), "c")); |
| 4340 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4344 | static void IndexedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
| 4345 | v8::Isolate* isolate = info.GetIsolate(); |
| 4346 | v8::Handle<v8::Array> result = v8::Array::New(isolate, 2); |
| 4347 | result->Set(v8::Integer::New(isolate, 0), v8::Number::New(isolate, 1)); |
| 4348 | result->Set(v8::Integer::New(isolate, 1), v8::Number::New(isolate, 10)); |
| 4349 | info.GetReturnValue().Set(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4353 | static void NamedGetter(v8::Local<v8::Name> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4354 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4355 | if (name->IsSymbol()) return; |
| 4356 | v8::String::Utf8Value n(v8::Local<v8::String>::Cast(name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4357 | if (strcmp(*n, "a") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4358 | info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "AA")); |
| 4359 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4360 | } else if (strcmp(*n, "b") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4361 | info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "BB")); |
| 4362 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4363 | } else if (strcmp(*n, "c") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4364 | info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "CC")); |
| 4365 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4366 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4367 | info.GetReturnValue().SetUndefined(); |
| 4368 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4369 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4370 | info.GetReturnValue().Set(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4374 | static void IndexedGetter(uint32_t index, |
| 4375 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4376 | info.GetReturnValue().Set(static_cast<double>(index + 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | |
| 4380 | TEST(InterceptorPropertyMirror) { |
| 4381 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4382 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4383 | v8::Isolate* isolate = env->GetIsolate(); |
| 4384 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4385 | env.ExposeDebug(); |
| 4386 | |
| 4387 | // Create object with named interceptor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4388 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4389 | named->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4390 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4391 | env->Global()->Set( |
| 4392 | v8::String::NewFromUtf8(isolate, "intercepted_named"), |
| 4393 | named->NewInstance()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4394 | |
| 4395 | // Create object with indexed interceptor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4396 | v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4397 | indexed->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4398 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4399 | env->Global()->Set( |
| 4400 | v8::String::NewFromUtf8(isolate, "intercepted_indexed"), |
| 4401 | indexed->NewInstance()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4402 | |
| 4403 | // Create object with both named and indexed interceptor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4404 | v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New(isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4405 | both->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 4406 | NamedGetter, NULL, NULL, NULL, NamedEnum)); |
| 4407 | both->SetHandler(v8::IndexedPropertyHandlerConfiguration( |
| 4408 | IndexedGetter, NULL, NULL, NULL, IndexedEnum)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4409 | env->Global()->Set( |
| 4410 | v8::String::NewFromUtf8(isolate, "intercepted_both"), |
| 4411 | both->NewInstance()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4412 | |
| 4413 | // Get mirrors for the three objects with interceptor. |
| 4414 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4415 | "var named_mirror = debug.MakeMirror(intercepted_named);" |
| 4416 | "var indexed_mirror = debug.MakeMirror(intercepted_indexed);" |
| 4417 | "var both_mirror = debug.MakeMirror(intercepted_both)"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4418 | CHECK(CompileRun( |
| 4419 | "named_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4420 | CHECK(CompileRun( |
| 4421 | "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4422 | CHECK(CompileRun( |
| 4423 | "both_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4424 | |
| 4425 | // Get the property names from the interceptors |
| 4426 | CompileRun( |
| 4427 | "named_names = named_mirror.propertyNames();" |
| 4428 | "indexed_names = indexed_mirror.propertyNames();" |
| 4429 | "both_names = both_mirror.propertyNames()"); |
| 4430 | CHECK_EQ(3, CompileRun("named_names.length")->Int32Value()); |
| 4431 | CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value()); |
| 4432 | CHECK_EQ(5, CompileRun("both_names.length")->Int32Value()); |
| 4433 | |
| 4434 | // Check the expected number of properties. |
| 4435 | const char* source; |
| 4436 | source = "named_mirror.properties().length"; |
| 4437 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4438 | |
| 4439 | source = "indexed_mirror.properties().length"; |
| 4440 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4441 | |
| 4442 | source = "both_mirror.properties().length"; |
| 4443 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4444 | |
| 4445 | // 1 is PropertyKind.Named; |
| 4446 | source = "both_mirror.properties(1).length"; |
| 4447 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4448 | |
| 4449 | // 2 is PropertyKind.Indexed; |
| 4450 | source = "both_mirror.properties(2).length"; |
| 4451 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4452 | |
| 4453 | // 3 is PropertyKind.Named | PropertyKind.Indexed; |
| 4454 | source = "both_mirror.properties(3).length"; |
| 4455 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4456 | |
| 4457 | // Get the interceptor properties for the object with only named interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4458 | CompileRun("var named_values = named_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4459 | |
| 4460 | // Check that the properties are interceptor properties. |
| 4461 | for (int i = 0; i < 3; i++) { |
| 4462 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4463 | SNPrintF(buffer, |
| 4464 | "named_values[%d] instanceof debug.PropertyMirror", i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4465 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4466 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4467 | SNPrintF(buffer, "named_values[%d].isNative()", i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4468 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4469 | } |
| 4470 | |
| 4471 | // Get the interceptor properties for the object with only indexed |
| 4472 | // interceptor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4473 | CompileRun("var indexed_values = indexed_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4474 | |
| 4475 | // Check that the properties are interceptor properties. |
| 4476 | for (int i = 0; i < 2; i++) { |
| 4477 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4478 | SNPrintF(buffer, |
| 4479 | "indexed_values[%d] instanceof debug.PropertyMirror", i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4480 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4481 | } |
| 4482 | |
| 4483 | // Get the interceptor properties for the object with both types of |
| 4484 | // interceptors. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4485 | CompileRun("var both_values = both_mirror.properties()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4486 | |
| 4487 | // Check that the properties are interceptor properties. |
| 4488 | for (int i = 0; i < 5; i++) { |
| 4489 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4490 | SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4491 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4492 | } |
| 4493 | |
| 4494 | // Check the property names. |
| 4495 | source = "both_values[0].name() == 'a'"; |
| 4496 | CHECK(CompileRun(source)->BooleanValue()); |
| 4497 | |
| 4498 | source = "both_values[1].name() == 'b'"; |
| 4499 | CHECK(CompileRun(source)->BooleanValue()); |
| 4500 | |
| 4501 | source = "both_values[2].name() == 'c'"; |
| 4502 | CHECK(CompileRun(source)->BooleanValue()); |
| 4503 | |
| 4504 | source = "both_values[3].name() == 1"; |
| 4505 | CHECK(CompileRun(source)->BooleanValue()); |
| 4506 | |
| 4507 | source = "both_values[4].name() == 10"; |
| 4508 | CHECK(CompileRun(source)->BooleanValue()); |
| 4509 | } |
| 4510 | |
| 4511 | |
| 4512 | TEST(HiddenPrototypePropertyMirror) { |
| 4513 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4514 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4515 | v8::Isolate* isolate = env->GetIsolate(); |
| 4516 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4517 | env.ExposeDebug(); |
| 4518 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4519 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4520 | t0->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "x"), |
| 4521 | v8::Number::New(isolate, 0)); |
| 4522 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4523 | t1->SetHiddenPrototype(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4524 | t1->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "y"), |
| 4525 | v8::Number::New(isolate, 1)); |
| 4526 | v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4527 | t2->SetHiddenPrototype(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4528 | t2->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "z"), |
| 4529 | v8::Number::New(isolate, 2)); |
| 4530 | v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(isolate); |
| 4531 | t3->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "u"), |
| 4532 | v8::Number::New(isolate, 3)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4533 | |
| 4534 | // Create object and set them on the global object. |
| 4535 | v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4536 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "o0"), o0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4537 | v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4538 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "o1"), o1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4539 | v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4540 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "o2"), o2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4541 | v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4542 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "o3"), o3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4543 | |
| 4544 | // Get mirrors for the four objects. |
| 4545 | CompileRun( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4546 | "var o0_mirror = debug.MakeMirror(o0);" |
| 4547 | "var o1_mirror = debug.MakeMirror(o1);" |
| 4548 | "var o2_mirror = debug.MakeMirror(o2);" |
| 4549 | "var o3_mirror = debug.MakeMirror(o3)"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4550 | CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4551 | CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4552 | CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4553 | CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4554 | |
| 4555 | // Check that each object has one property. |
| 4556 | CHECK_EQ(1, CompileRun( |
| 4557 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4558 | CHECK_EQ(1, CompileRun( |
| 4559 | "o1_mirror.propertyNames().length")->Int32Value()); |
| 4560 | CHECK_EQ(1, CompileRun( |
| 4561 | "o2_mirror.propertyNames().length")->Int32Value()); |
| 4562 | CHECK_EQ(1, CompileRun( |
| 4563 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4564 | |
| 4565 | // Set o1 as prototype for o0. o1 has the hidden prototype flag so all |
| 4566 | // properties on o1 should be seen on o0. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4567 | o0->Set(v8::String::NewFromUtf8(isolate, "__proto__"), o1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4568 | CHECK_EQ(2, CompileRun( |
| 4569 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4570 | CHECK_EQ(0, CompileRun( |
| 4571 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4572 | CHECK_EQ(1, CompileRun( |
| 4573 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4574 | |
| 4575 | // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden |
| 4576 | // prototype flag. o2 also has the hidden prototype flag so all properties |
| 4577 | // on o2 should be seen on o0 as well as properties on o1. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4578 | o0->Set(v8::String::NewFromUtf8(isolate, "__proto__"), o2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4579 | CHECK_EQ(3, CompileRun( |
| 4580 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4581 | CHECK_EQ(0, CompileRun( |
| 4582 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4583 | CHECK_EQ(1, CompileRun( |
| 4584 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4585 | CHECK_EQ(2, CompileRun( |
| 4586 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4587 | |
| 4588 | // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and |
| 4589 | // o2 has the hidden prototype flag. o3 does not have the hidden prototype |
| 4590 | // flag so properties on o3 should not be seen on o0 whereas the properties |
| 4591 | // from o1 and o2 should still be seen on o0. |
| 4592 | // Final prototype chain: o0 -> o1 -> o2 -> o3 |
| 4593 | // Hidden prototypes: ^^ ^^ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4594 | o0->Set(v8::String::NewFromUtf8(isolate, "__proto__"), o3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4595 | CHECK_EQ(3, CompileRun( |
| 4596 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4597 | CHECK_EQ(1, CompileRun( |
| 4598 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4599 | CHECK_EQ(0, CompileRun( |
| 4600 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4601 | CHECK_EQ(1, CompileRun( |
| 4602 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4603 | CHECK_EQ(2, CompileRun( |
| 4604 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4605 | CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue()); |
| 4606 | |
| 4607 | // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. |
| 4608 | CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue()); |
| 4609 | } |
| 4610 | |
| 4611 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4612 | static void ProtperyXNativeGetter( |
| 4613 | v8::Local<v8::String> property, |
| 4614 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4615 | info.GetReturnValue().Set(10); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | |
| 4619 | TEST(NativeGetterPropertyMirror) { |
| 4620 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4621 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4622 | v8::Isolate* isolate = env->GetIsolate(); |
| 4623 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4624 | env.ExposeDebug(); |
| 4625 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4626 | v8::Handle<v8::String> name = v8::String::NewFromUtf8(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4627 | // Create object with named accessor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4628 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4629 | named->SetAccessor(name, &ProtperyXNativeGetter, NULL, |
| 4630 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4631 | |
| 4632 | // Create object with named property getter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4633 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "instance"), |
| 4634 | named->NewInstance()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4635 | CHECK_EQ(10, CompileRun("instance.x")->Int32Value()); |
| 4636 | |
| 4637 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4638 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4639 | CHECK(CompileRun( |
| 4640 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4641 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4642 | CompileRun("var named_names = instance_mirror.propertyNames();"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4643 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4644 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4645 | CHECK(CompileRun( |
| 4646 | "instance_mirror.property('x').value().isNumber()")->BooleanValue()); |
| 4647 | CHECK(CompileRun( |
| 4648 | "instance_mirror.property('x').value().value() == 10")->BooleanValue()); |
| 4649 | } |
| 4650 | |
| 4651 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4652 | static void ProtperyXNativeGetterThrowingError( |
| 4653 | v8::Local<v8::String> property, |
| 4654 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 4655 | CompileRun("throw new Error('Error message');"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | |
| 4659 | TEST(NativeGetterThrowingErrorPropertyMirror) { |
| 4660 | // Create a V8 environment with debug access. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4661 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4662 | v8::Isolate* isolate = env->GetIsolate(); |
| 4663 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4664 | env.ExposeDebug(); |
| 4665 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4666 | v8::Handle<v8::String> name = v8::String::NewFromUtf8(isolate, "x"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4667 | // Create object with named accessor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4668 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4669 | named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, |
| 4670 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4671 | |
| 4672 | // Create object with named property getter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4673 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "instance"), |
| 4674 | named->NewInstance()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4675 | |
| 4676 | // Get mirror for the object with property getter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4677 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4678 | CHECK(CompileRun( |
| 4679 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4680 | CompileRun("named_names = instance_mirror.propertyNames();"); |
| 4681 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4682 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4683 | CHECK(CompileRun( |
| 4684 | "instance_mirror.property('x').value().isError()")->BooleanValue()); |
| 4685 | |
| 4686 | // Check that the message is that passed to the Error constructor. |
| 4687 | CHECK(CompileRun( |
| 4688 | "instance_mirror.property('x').value().message() == 'Error message'")-> |
| 4689 | BooleanValue()); |
| 4690 | } |
| 4691 | |
| 4692 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4693 | // Test that hidden properties object is not returned as an unnamed property |
| 4694 | // among regular properties. |
| 4695 | // See http://crbug.com/26491 |
| 4696 | TEST(NoHiddenProperties) { |
| 4697 | // Create a V8 environment with debug access. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4698 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4699 | v8::Isolate* isolate = env->GetIsolate(); |
| 4700 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4701 | env.ExposeDebug(); |
| 4702 | |
| 4703 | // Create an object in the global scope. |
| 4704 | const char* source = "var obj = {a: 1};"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4705 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, source)) |
| 4706 | ->Run(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4707 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4708 | env->Global()->Get(v8::String::NewFromUtf8(isolate, "obj"))); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4709 | // Set a hidden property on the object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4710 | obj->SetHiddenValue( |
| 4711 | v8::String::NewFromUtf8(isolate, "v8::test-debug::a"), |
| 4712 | v8::Int32::New(isolate, 11)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4713 | |
| 4714 | // Get mirror for the object with property getter. |
| 4715 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4716 | CHECK(CompileRun( |
| 4717 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4718 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4719 | // There should be exactly one property. But there is also an unnamed |
| 4720 | // property whose value is hidden properties dictionary. The latter |
| 4721 | // property should not be in the list of reguar properties. |
| 4722 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4723 | CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue()); |
| 4724 | CHECK(CompileRun( |
| 4725 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4726 | |
| 4727 | // Object created by t0 will become hidden prototype of object 'obj'. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4728 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); |
| 4729 | t0->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "b"), |
| 4730 | v8::Number::New(isolate, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4731 | t0->SetHiddenPrototype(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4732 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); |
| 4733 | t1->InstanceTemplate()->Set(v8::String::NewFromUtf8(isolate, "c"), |
| 4734 | v8::Number::New(isolate, 3)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Create proto objects, add hidden properties to them and set them on |
| 4737 | // the global object. |
| 4738 | v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4739 | protoObj->SetHiddenValue( |
| 4740 | v8::String::NewFromUtf8(isolate, "v8::test-debug::b"), |
| 4741 | v8::Int32::New(isolate, 12)); |
| 4742 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "protoObj"), |
| 4743 | protoObj); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4744 | v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4745 | grandProtoObj->SetHiddenValue( |
| 4746 | v8::String::NewFromUtf8(isolate, "v8::test-debug::c"), |
| 4747 | v8::Int32::New(isolate, 13)); |
| 4748 | env->Global()->Set( |
| 4749 | v8::String::NewFromUtf8(isolate, "grandProtoObj"), |
| 4750 | grandProtoObj); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4751 | |
| 4752 | // Setting prototypes: obj->protoObj->grandProtoObj |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4753 | protoObj->Set(v8::String::NewFromUtf8(isolate, "__proto__"), |
| 4754 | grandProtoObj); |
| 4755 | obj->Set(v8::String::NewFromUtf8(isolate, "__proto__"), protoObj); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4756 | |
| 4757 | // Get mirror for the object with property getter. |
| 4758 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4759 | CHECK(CompileRun( |
| 4760 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4761 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4762 | // There should be exactly two properties - one from the object itself and |
| 4763 | // another from its hidden prototype. |
| 4764 | CHECK_EQ(2, CompileRun("named_names.length")->Int32Value()); |
| 4765 | CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" |
| 4766 | "named_names[1] == 'b'")->BooleanValue()); |
| 4767 | CHECK(CompileRun( |
| 4768 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4769 | CHECK(CompileRun( |
| 4770 | "obj_mirror.property('b').value().value() == 2")->BooleanValue()); |
| 4771 | } |
| 4772 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4773 | |
| 4774 | // Multithreaded tests of JSON debugger protocol |
| 4775 | |
| 4776 | // Support classes |
| 4777 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4778 | // Provides synchronization between N threads, where N is a template parameter. |
| 4779 | // The Wait() call blocks a thread until it is called for the Nth time, then all |
| 4780 | // calls return. Each ThreadBarrier object can only be used once. |
| 4781 | template <int N> |
| 4782 | class ThreadBarrier FINAL { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4783 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4784 | ThreadBarrier() : num_blocked_(0) {} |
| 4785 | |
| 4786 | ~ThreadBarrier() { |
| 4787 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4788 | if (num_blocked_ != 0) { |
| 4789 | CHECK_EQ(N, num_blocked_); |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | void Wait() { |
| 4794 | LockGuard<Mutex> lock_guard(&mutex_); |
| 4795 | CHECK_LT(num_blocked_, N); |
| 4796 | num_blocked_++; |
| 4797 | if (N == num_blocked_) { |
| 4798 | // Signal and unblock all waiting threads. |
| 4799 | cv_.NotifyAll(); |
| 4800 | printf("BARRIER\n\n"); |
| 4801 | fflush(stdout); |
| 4802 | } else { // Wait for the semaphore. |
| 4803 | while (num_blocked_ < N) { |
| 4804 | cv_.Wait(&mutex_); |
| 4805 | } |
| 4806 | } |
| 4807 | CHECK_EQ(N, num_blocked_); |
| 4808 | } |
| 4809 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4810 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4811 | ConditionVariable cv_; |
| 4812 | Mutex mutex_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4813 | int num_blocked_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4814 | |
| 4815 | STATIC_ASSERT(N > 0); |
| 4816 | |
| 4817 | DISALLOW_COPY_AND_ASSIGN(ThreadBarrier); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4818 | }; |
| 4819 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4820 | |
| 4821 | // A set containing enough barriers and semaphores for any of the tests. |
| 4822 | class Barriers { |
| 4823 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4824 | Barriers() : semaphore_1(0), semaphore_2(0) {} |
| 4825 | ThreadBarrier<2> barrier_1; |
| 4826 | ThreadBarrier<2> barrier_2; |
| 4827 | ThreadBarrier<2> barrier_3; |
| 4828 | ThreadBarrier<2> barrier_4; |
| 4829 | ThreadBarrier<2> barrier_5; |
| 4830 | v8::base::Semaphore semaphore_1; |
| 4831 | v8::base::Semaphore semaphore_2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4832 | }; |
| 4833 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4834 | |
| 4835 | // We match parts of the message to decide if it is a break message. |
| 4836 | bool IsBreakEventMessage(char *message) { |
| 4837 | const char* type_event = "\"type\":\"event\""; |
| 4838 | const char* event_break = "\"event\":\"break\""; |
| 4839 | // Does the message contain both type:event and event:break? |
| 4840 | return strstr(message, type_event) != NULL && |
| 4841 | strstr(message, event_break) != NULL; |
| 4842 | } |
| 4843 | |
| 4844 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4845 | // We match parts of the message to decide if it is a exception message. |
| 4846 | bool IsExceptionEventMessage(char *message) { |
| 4847 | const char* type_event = "\"type\":\"event\""; |
| 4848 | const char* event_exception = "\"event\":\"exception\""; |
| 4849 | // Does the message contain both type:event and event:exception? |
| 4850 | return strstr(message, type_event) != NULL && |
| 4851 | strstr(message, event_exception) != NULL; |
| 4852 | } |
| 4853 | |
| 4854 | |
| 4855 | // We match the message wether it is an evaluate response message. |
| 4856 | bool IsEvaluateResponseMessage(char* message) { |
| 4857 | const char* type_response = "\"type\":\"response\""; |
| 4858 | const char* command_evaluate = "\"command\":\"evaluate\""; |
| 4859 | // Does the message contain both type:response and command:evaluate? |
| 4860 | return strstr(message, type_response) != NULL && |
| 4861 | strstr(message, command_evaluate) != NULL; |
| 4862 | } |
| 4863 | |
| 4864 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4865 | static int StringToInt(const char* s) { |
| 4866 | return atoi(s); // NOLINT |
| 4867 | } |
| 4868 | |
| 4869 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4870 | // We match parts of the message to get evaluate result int value. |
| 4871 | int GetEvaluateIntResult(char *message) { |
| 4872 | const char* value = "\"value\":"; |
| 4873 | char* pos = strstr(message, value); |
| 4874 | if (pos == NULL) { |
| 4875 | return -1; |
| 4876 | } |
| 4877 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4878 | res = StringToInt(pos + strlen(value)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4879 | return res; |
| 4880 | } |
| 4881 | |
| 4882 | |
| 4883 | // We match parts of the message to get hit breakpoint id. |
| 4884 | int GetBreakpointIdFromBreakEventMessage(char *message) { |
| 4885 | const char* breakpoints = "\"breakpoints\":["; |
| 4886 | char* pos = strstr(message, breakpoints); |
| 4887 | if (pos == NULL) { |
| 4888 | return -1; |
| 4889 | } |
| 4890 | int res = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4891 | res = StringToInt(pos + strlen(breakpoints)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4892 | return res; |
| 4893 | } |
| 4894 | |
| 4895 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4896 | // We match parts of the message to get total frames number. |
| 4897 | int GetTotalFramesInt(char *message) { |
| 4898 | const char* prefix = "\"totalFrames\":"; |
| 4899 | char* pos = strstr(message, prefix); |
| 4900 | if (pos == NULL) { |
| 4901 | return -1; |
| 4902 | } |
| 4903 | pos += strlen(prefix); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4904 | int res = StringToInt(pos); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4905 | return res; |
| 4906 | } |
| 4907 | |
| 4908 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 4909 | // We match parts of the message to get source line. |
| 4910 | int GetSourceLineFromBreakEventMessage(char *message) { |
| 4911 | const char* source_line = "\"sourceLine\":"; |
| 4912 | char* pos = strstr(message, source_line); |
| 4913 | if (pos == NULL) { |
| 4914 | return -1; |
| 4915 | } |
| 4916 | int res = -1; |
| 4917 | res = StringToInt(pos + strlen(source_line)); |
| 4918 | return res; |
| 4919 | } |
| 4920 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4921 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4922 | /* Test MessageQueues */ |
| 4923 | /* Tests the message queues that hold debugger commands and |
| 4924 | * response messages to the debugger. Fills queues and makes |
| 4925 | * them grow. |
| 4926 | */ |
| 4927 | Barriers message_queue_barriers; |
| 4928 | |
| 4929 | // This is the debugger thread, that executes no v8 calls except |
| 4930 | // placing JSON debugger commands in the queue. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4931 | class MessageQueueDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4932 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4933 | MessageQueueDebuggerThread() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4934 | : Thread(Options("MessageQueueDebuggerThread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4935 | void Run(); |
| 4936 | }; |
| 4937 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4938 | |
| 4939 | static void MessageHandler(const v8::Debug::Message& message) { |
| 4940 | v8::Handle<v8::String> json = message.GetJSON(); |
| 4941 | v8::String::Utf8Value utf8(json); |
| 4942 | if (IsBreakEventMessage(*utf8)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4943 | // Lets test script wait until break occurs to send commands. |
| 4944 | // Signals when a break is reported. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4945 | message_queue_barriers.semaphore_2.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | // Allow message handler to block on a semaphore, to test queueing of |
| 4949 | // messages while blocked. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4950 | message_queue_barriers.semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4953 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4954 | void MessageQueueDebuggerThread::Run() { |
| 4955 | const int kBufferSize = 1000; |
| 4956 | uint16_t buffer_1[kBufferSize]; |
| 4957 | uint16_t buffer_2[kBufferSize]; |
| 4958 | const char* command_1 = |
| 4959 | "{\"seq\":117," |
| 4960 | "\"type\":\"request\"," |
| 4961 | "\"command\":\"evaluate\"," |
| 4962 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 4963 | const char* command_2 = |
| 4964 | "{\"seq\":118," |
| 4965 | "\"type\":\"request\"," |
| 4966 | "\"command\":\"evaluate\"," |
| 4967 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 4968 | const char* command_3 = |
| 4969 | "{\"seq\":119," |
| 4970 | "\"type\":\"request\"," |
| 4971 | "\"command\":\"evaluate\"," |
| 4972 | "\"arguments\":{\"expression\":\"c.d * b\"}}"; |
| 4973 | const char* command_continue = |
| 4974 | "{\"seq\":106," |
| 4975 | "\"type\":\"request\"," |
| 4976 | "\"command\":\"continue\"}"; |
| 4977 | const char* command_single_step = |
| 4978 | "{\"seq\":107," |
| 4979 | "\"type\":\"request\"," |
| 4980 | "\"command\":\"continue\"," |
| 4981 | "\"arguments\":{\"stepaction\":\"next\"}}"; |
| 4982 | |
| 4983 | /* Interleaved sequence of actions by the two threads:*/ |
| 4984 | // Main thread compiles and runs source_1 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4985 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4986 | message_queue_barriers.barrier_1.Wait(); |
| 4987 | // Post 6 commands, filling the command queue and making it expand. |
| 4988 | // These calls return immediately, but the commands stay on the queue |
| 4989 | // until the execution of source_2. |
| 4990 | // Note: AsciiToUtf16 executes before SendCommand, so command is copied |
| 4991 | // to buffer before buffer is sent to SendCommand. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4992 | v8::Isolate* isolate = CcTest::isolate(); |
| 4993 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 4994 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 4995 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4996 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4997 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4998 | message_queue_barriers.barrier_2.Wait(); |
| 4999 | // Main thread compiles and runs source_2. |
| 5000 | // Queued commands are executed at the start of compilation of source_2( |
| 5001 | // beforeCompile event). |
| 5002 | // Free the message handler to process all the messages from the queue. 7 |
| 5003 | // messages are expected: 2 afterCompile events and 5 responses. |
| 5004 | // All the commands added so far will fail to execute as long as call stack |
| 5005 | // is empty on beforeCompile event. |
| 5006 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5007 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5008 | } |
| 5009 | message_queue_barriers.barrier_3.Wait(); |
| 5010 | // Main thread compiles and runs source_3. |
| 5011 | // Don't stop in the afterCompile handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5012 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5013 | // source_3 includes a debugger statement, which causes a break event. |
| 5014 | // Wait on break event from hitting "debugger" statement |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5015 | message_queue_barriers.semaphore_2.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5016 | // These should execute after the "debugger" statement in source_2 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5017 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 5018 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 5019 | v8::Debug::SendCommand(isolate, buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 5020 | v8::Debug::SendCommand( |
| 5021 | isolate, buffer_2, AsciiToUtf16(command_single_step, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5022 | // Run after 2 break events, 4 responses. |
| 5023 | for (int i = 0; i < 6 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5024 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5025 | } |
| 5026 | // Wait on break event after a single step executes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5027 | message_queue_barriers.semaphore_2.Wait(); |
| 5028 | v8::Debug::SendCommand(isolate, buffer_1, AsciiToUtf16(command_2, buffer_1)); |
| 5029 | v8::Debug::SendCommand( |
| 5030 | isolate, buffer_2, AsciiToUtf16(command_continue, buffer_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5031 | // Run after 2 responses. |
| 5032 | for (int i = 0; i < 2 ; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5033 | message_queue_barriers.semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5034 | } |
| 5035 | // Main thread continues running source_3 to end, waits for this thread. |
| 5036 | } |
| 5037 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5038 | |
| 5039 | // This thread runs the v8 engine. |
| 5040 | TEST(MessageQueues) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5041 | MessageQueueDebuggerThread message_queue_debugger_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5042 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5043 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5044 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5045 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5046 | v8::Debug::SetMessageHandler(MessageHandler); |
| 5047 | message_queue_debugger_thread.Start(); |
| 5048 | |
| 5049 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5050 | const char* source_2 = "e = 17;"; |
| 5051 | const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; |
| 5052 | |
| 5053 | // See MessageQueueDebuggerThread::Run for interleaved sequence of |
| 5054 | // API calls and events in the two threads. |
| 5055 | CompileRun(source_1); |
| 5056 | message_queue_barriers.barrier_1.Wait(); |
| 5057 | message_queue_barriers.barrier_2.Wait(); |
| 5058 | CompileRun(source_2); |
| 5059 | message_queue_barriers.barrier_3.Wait(); |
| 5060 | CompileRun(source_3); |
| 5061 | message_queue_debugger_thread.Join(); |
| 5062 | fflush(stdout); |
| 5063 | } |
| 5064 | |
| 5065 | |
| 5066 | class TestClientData : public v8::Debug::ClientData { |
| 5067 | public: |
| 5068 | TestClientData() { |
| 5069 | constructor_call_counter++; |
| 5070 | } |
| 5071 | virtual ~TestClientData() { |
| 5072 | destructor_call_counter++; |
| 5073 | } |
| 5074 | |
| 5075 | static void ResetCounters() { |
| 5076 | constructor_call_counter = 0; |
| 5077 | destructor_call_counter = 0; |
| 5078 | } |
| 5079 | |
| 5080 | static int constructor_call_counter; |
| 5081 | static int destructor_call_counter; |
| 5082 | }; |
| 5083 | |
| 5084 | int TestClientData::constructor_call_counter = 0; |
| 5085 | int TestClientData::destructor_call_counter = 0; |
| 5086 | |
| 5087 | |
| 5088 | // Tests that MessageQueue doesn't destroy client data when expands and |
| 5089 | // does destroy when it dies. |
| 5090 | TEST(MessageQueueExpandAndDestroy) { |
| 5091 | TestClientData::ResetCounters(); |
| 5092 | { // Create a scope for the queue. |
| 5093 | CommandMessageQueue queue(1); |
| 5094 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5095 | new TestClientData())); |
| 5096 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5097 | new TestClientData())); |
| 5098 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5099 | new TestClientData())); |
| 5100 | CHECK_EQ(0, TestClientData::destructor_call_counter); |
| 5101 | queue.Get().Dispose(); |
| 5102 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5103 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5104 | new TestClientData())); |
| 5105 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5106 | new TestClientData())); |
| 5107 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5108 | new TestClientData())); |
| 5109 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5110 | new TestClientData())); |
| 5111 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5112 | new TestClientData())); |
| 5113 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5114 | queue.Get().Dispose(); |
| 5115 | CHECK_EQ(2, TestClientData::destructor_call_counter); |
| 5116 | } |
| 5117 | // All the client data should be destroyed when the queue is destroyed. |
| 5118 | CHECK_EQ(TestClientData::destructor_call_counter, |
| 5119 | TestClientData::destructor_call_counter); |
| 5120 | } |
| 5121 | |
| 5122 | |
| 5123 | static int handled_client_data_instances_count = 0; |
| 5124 | static void MessageHandlerCountingClientData( |
| 5125 | const v8::Debug::Message& message) { |
| 5126 | if (message.GetClientData() != NULL) { |
| 5127 | handled_client_data_instances_count++; |
| 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | |
| 5132 | // Tests that all client data passed to the debugger are sent to the handler. |
| 5133 | TEST(SendClientDataToHandler) { |
| 5134 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5136 | v8::Isolate* isolate = env->GetIsolate(); |
| 5137 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5138 | TestClientData::ResetCounters(); |
| 5139 | handled_client_data_instances_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5140 | v8::Debug::SetMessageHandler(MessageHandlerCountingClientData); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5141 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 5142 | const int kBufferSize = 1000; |
| 5143 | uint16_t buffer[kBufferSize]; |
| 5144 | const char* command_1 = |
| 5145 | "{\"seq\":117," |
| 5146 | "\"type\":\"request\"," |
| 5147 | "\"command\":\"evaluate\"," |
| 5148 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5149 | const char* command_2 = |
| 5150 | "{\"seq\":118," |
| 5151 | "\"type\":\"request\"," |
| 5152 | "\"command\":\"evaluate\"," |
| 5153 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5154 | const char* command_continue = |
| 5155 | "{\"seq\":106," |
| 5156 | "\"type\":\"request\"," |
| 5157 | "\"command\":\"continue\"}"; |
| 5158 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5159 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_1, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5160 | new TestClientData()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5161 | v8::Debug::SendCommand( |
| 5162 | isolate, buffer, AsciiToUtf16(command_2, buffer), NULL); |
| 5163 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_2, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5164 | new TestClientData()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5165 | v8::Debug::SendCommand(isolate, buffer, AsciiToUtf16(command_2, buffer), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5166 | new TestClientData()); |
| 5167 | // All the messages will be processed on beforeCompile event. |
| 5168 | CompileRun(source_1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5169 | v8::Debug::SendCommand( |
| 5170 | isolate, buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5171 | CHECK_EQ(3, TestClientData::constructor_call_counter); |
| 5172 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5173 | handled_client_data_instances_count); |
| 5174 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5175 | TestClientData::destructor_call_counter); |
| 5176 | } |
| 5177 | |
| 5178 | |
| 5179 | /* Test ThreadedDebugging */ |
| 5180 | /* This test interrupts a running infinite loop that is |
| 5181 | * occupying the v8 thread by a break command from the |
| 5182 | * debugger thread. It then changes the value of a |
| 5183 | * global object, to make the loop terminate. |
| 5184 | */ |
| 5185 | |
| 5186 | Barriers threaded_debugging_barriers; |
| 5187 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5188 | class V8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5189 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5190 | V8Thread() : Thread(Options("V8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5191 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5192 | v8::Isolate* isolate() { return isolate_; } |
| 5193 | |
| 5194 | private: |
| 5195 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5196 | }; |
| 5197 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5198 | class DebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5199 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5200 | explicit DebuggerThread(v8::Isolate* isolate) |
| 5201 | : Thread(Options("DebuggerThread")), isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5202 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5203 | |
| 5204 | private: |
| 5205 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5206 | }; |
| 5207 | |
| 5208 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5209 | static void ThreadedAtBarrier1( |
| 5210 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5211 | threaded_debugging_barriers.barrier_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | |
| 5215 | static void ThreadedMessageHandler(const v8::Debug::Message& message) { |
| 5216 | static char print_buffer[1000]; |
| 5217 | v8::String::Value json(message.GetJSON()); |
| 5218 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5219 | if (IsBreakEventMessage(print_buffer)) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5220 | // Check that we are inside the while loop. |
| 5221 | int source_line = GetSourceLineFromBreakEventMessage(print_buffer); |
| 5222 | CHECK(8 <= source_line && source_line <= 13); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5223 | threaded_debugging_barriers.barrier_2.Wait(); |
| 5224 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | |
| 5228 | void V8Thread::Run() { |
| 5229 | const char* source = |
| 5230 | "flag = true;\n" |
| 5231 | "function bar( new_value ) {\n" |
| 5232 | " flag = new_value;\n" |
| 5233 | " return \"Return from bar(\" + new_value + \")\";\n" |
| 5234 | "}\n" |
| 5235 | "\n" |
| 5236 | "function foo() {\n" |
| 5237 | " var x = 1;\n" |
| 5238 | " while ( flag == true ) {\n" |
| 5239 | " if ( x == 1 ) {\n" |
| 5240 | " ThreadedAtBarrier1();\n" |
| 5241 | " }\n" |
| 5242 | " x = x + 1;\n" |
| 5243 | " }\n" |
| 5244 | "}\n" |
| 5245 | "\n" |
| 5246 | "foo();\n"; |
| 5247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5248 | isolate_ = v8::Isolate::New(); |
| 5249 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5250 | { |
| 5251 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5252 | DebugLocalContext env(isolate_); |
| 5253 | v8::HandleScope scope(isolate_); |
| 5254 | v8::Debug::SetMessageHandler(&ThreadedMessageHandler); |
| 5255 | v8::Handle<v8::ObjectTemplate> global_template = |
| 5256 | v8::ObjectTemplate::New(env->GetIsolate()); |
| 5257 | global_template->Set( |
| 5258 | v8::String::NewFromUtf8(env->GetIsolate(), "ThreadedAtBarrier1"), |
| 5259 | v8::FunctionTemplate::New(isolate_, ThreadedAtBarrier1)); |
| 5260 | v8::Handle<v8::Context> context = |
| 5261 | v8::Context::New(isolate_, NULL, global_template); |
| 5262 | v8::Context::Scope context_scope(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5264 | CompileRun(source); |
| 5265 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5266 | threaded_debugging_barriers.barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5267 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5270 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5271 | void DebuggerThread::Run() { |
| 5272 | const int kBufSize = 1000; |
| 5273 | uint16_t buffer[kBufSize]; |
| 5274 | |
| 5275 | const char* command_1 = "{\"seq\":102," |
| 5276 | "\"type\":\"request\"," |
| 5277 | "\"command\":\"evaluate\"," |
| 5278 | "\"arguments\":{\"expression\":\"bar(false)\"}}"; |
| 5279 | const char* command_2 = "{\"seq\":103," |
| 5280 | "\"type\":\"request\"," |
| 5281 | "\"command\":\"continue\"}"; |
| 5282 | |
| 5283 | threaded_debugging_barriers.barrier_1.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5284 | v8::Debug::DebugBreak(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5285 | threaded_debugging_barriers.barrier_2.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5286 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
| 5287 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5288 | threaded_debugging_barriers.barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5291 | |
| 5292 | TEST(ThreadedDebugging) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5293 | V8Thread v8_thread; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5295 | // Create a V8 environment |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5296 | v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5297 | threaded_debugging_barriers.barrier_3.Wait(); |
| 5298 | DebuggerThread debugger_thread(v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5299 | debugger_thread.Start(); |
| 5300 | |
| 5301 | v8_thread.Join(); |
| 5302 | debugger_thread.Join(); |
| 5303 | } |
| 5304 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5305 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5306 | /* Test RecursiveBreakpoints */ |
| 5307 | /* In this test, the debugger evaluates a function with a breakpoint, after |
| 5308 | * hitting a breakpoint in another function. We do this with both values |
| 5309 | * of the flag enabling recursive breakpoints, and verify that the second |
| 5310 | * breakpoint is hit when enabled, and missed when disabled. |
| 5311 | */ |
| 5312 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5313 | class BreakpointsV8Thread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5314 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5315 | BreakpointsV8Thread() : Thread(Options("BreakpointsV8Thread")) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5316 | void Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5317 | |
| 5318 | v8::Isolate* isolate() { return isolate_; } |
| 5319 | |
| 5320 | private: |
| 5321 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5322 | }; |
| 5323 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5324 | class BreakpointsDebuggerThread : public v8::base::Thread { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5325 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5326 | BreakpointsDebuggerThread(bool global_evaluate, v8::Isolate* isolate) |
| 5327 | : Thread(Options("BreakpointsDebuggerThread")), |
| 5328 | global_evaluate_(global_evaluate), |
| 5329 | isolate_(isolate) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5330 | void Run(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5331 | |
| 5332 | private: |
| 5333 | bool global_evaluate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5334 | v8::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5335 | }; |
| 5336 | |
| 5337 | |
| 5338 | Barriers* breakpoints_barriers; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5339 | int break_event_breakpoint_id; |
| 5340 | int evaluate_int_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5341 | |
| 5342 | static void BreakpointsMessageHandler(const v8::Debug::Message& message) { |
| 5343 | static char print_buffer[1000]; |
| 5344 | v8::String::Value json(message.GetJSON()); |
| 5345 | Utf16ToAscii(*json, json.length(), print_buffer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5346 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5347 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5348 | break_event_breakpoint_id = |
| 5349 | GetBreakpointIdFromBreakEventMessage(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5350 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5351 | } else if (IsEvaluateResponseMessage(print_buffer)) { |
| 5352 | evaluate_int_result = GetEvaluateIntResult(print_buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5353 | breakpoints_barriers->semaphore_1.Signal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | |
| 5358 | void BreakpointsV8Thread::Run() { |
| 5359 | const char* source_1 = "var y_global = 3;\n" |
| 5360 | "function cat( new_value ) {\n" |
| 5361 | " var x = new_value;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5362 | " y_global = y_global + 4;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5363 | " x = 3 * x + 1;\n" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5364 | " y_global = y_global + 5;\n" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5365 | " return x;\n" |
| 5366 | "}\n" |
| 5367 | "\n" |
| 5368 | "function dog() {\n" |
| 5369 | " var x = 1;\n" |
| 5370 | " x = y_global;" |
| 5371 | " var z = 3;" |
| 5372 | " x += 100;\n" |
| 5373 | " return x;\n" |
| 5374 | "}\n" |
| 5375 | "\n"; |
| 5376 | const char* source_2 = "cat(17);\n" |
| 5377 | "cat(19);\n"; |
| 5378 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5379 | isolate_ = v8::Isolate::New(); |
| 5380 | breakpoints_barriers->barrier_3.Wait(); |
| 5381 | { |
| 5382 | v8::Isolate::Scope isolate_scope(isolate_); |
| 5383 | DebugLocalContext env(isolate_); |
| 5384 | v8::HandleScope scope(isolate_); |
| 5385 | v8::Debug::SetMessageHandler(&BreakpointsMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5386 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5387 | CompileRun(source_1); |
| 5388 | breakpoints_barriers->barrier_1.Wait(); |
| 5389 | breakpoints_barriers->barrier_2.Wait(); |
| 5390 | CompileRun(source_2); |
| 5391 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5392 | breakpoints_barriers->barrier_4.Wait(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5393 | isolate_->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
| 5396 | |
| 5397 | void BreakpointsDebuggerThread::Run() { |
| 5398 | const int kBufSize = 1000; |
| 5399 | uint16_t buffer[kBufSize]; |
| 5400 | |
| 5401 | const char* command_1 = "{\"seq\":101," |
| 5402 | "\"type\":\"request\"," |
| 5403 | "\"command\":\"setbreakpoint\"," |
| 5404 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5405 | const char* command_2 = "{\"seq\":102," |
| 5406 | "\"type\":\"request\"," |
| 5407 | "\"command\":\"setbreakpoint\"," |
| 5408 | "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5409 | const char* command_3; |
| 5410 | if (this->global_evaluate_) { |
| 5411 | command_3 = "{\"seq\":103," |
| 5412 | "\"type\":\"request\"," |
| 5413 | "\"command\":\"evaluate\"," |
| 5414 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," |
| 5415 | "\"global\":true}}"; |
| 5416 | } else { |
| 5417 | command_3 = "{\"seq\":103," |
| 5418 | "\"type\":\"request\"," |
| 5419 | "\"command\":\"evaluate\"," |
| 5420 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; |
| 5421 | } |
| 5422 | const char* command_4; |
| 5423 | if (this->global_evaluate_) { |
| 5424 | command_4 = "{\"seq\":104," |
| 5425 | "\"type\":\"request\"," |
| 5426 | "\"command\":\"evaluate\"," |
| 5427 | "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," |
| 5428 | "\"global\":true}}"; |
| 5429 | } else { |
| 5430 | command_4 = "{\"seq\":104," |
| 5431 | "\"type\":\"request\"," |
| 5432 | "\"command\":\"evaluate\"," |
| 5433 | "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; |
| 5434 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5435 | const char* command_5 = "{\"seq\":105," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5436 | "\"type\":\"request\"," |
| 5437 | "\"command\":\"continue\"}"; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5438 | const char* command_6 = "{\"seq\":106," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5439 | "\"type\":\"request\"," |
| 5440 | "\"command\":\"continue\"}"; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5441 | const char* command_7; |
| 5442 | if (this->global_evaluate_) { |
| 5443 | command_7 = "{\"seq\":107," |
| 5444 | "\"type\":\"request\"," |
| 5445 | "\"command\":\"evaluate\"," |
| 5446 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," |
| 5447 | "\"global\":true}}"; |
| 5448 | } else { |
| 5449 | command_7 = "{\"seq\":107," |
| 5450 | "\"type\":\"request\"," |
| 5451 | "\"command\":\"evaluate\"," |
| 5452 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; |
| 5453 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5454 | const char* command_8 = "{\"seq\":108," |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5455 | "\"type\":\"request\"," |
| 5456 | "\"command\":\"continue\"}"; |
| 5457 | |
| 5458 | |
| 5459 | // v8 thread initializes, runs source_1 |
| 5460 | breakpoints_barriers->barrier_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5461 | // 1:Set breakpoint in cat() (will get id 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5462 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_1, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5463 | // 2:Set breakpoint in dog() (will get id 2). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5464 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_2, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5465 | breakpoints_barriers->barrier_2.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5466 | // V8 thread starts compiling source_2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5467 | // Automatic break happens, to run queued commands |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5468 | // breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5469 | // Commands 1 through 3 run, thread continues. |
| 5470 | // v8 thread runs source_2 to breakpoint in cat(). |
| 5471 | // message callback receives break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5472 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5473 | // Must have hit breakpoint #1. |
| 5474 | CHECK_EQ(1, break_event_breakpoint_id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5475 | // 4:Evaluate dog() (which has a breakpoint). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5476 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_3, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5477 | // V8 thread hits breakpoint in dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5478 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5479 | // Must have hit breakpoint #2. |
| 5480 | CHECK_EQ(2, break_event_breakpoint_id); |
| 5481 | // 5:Evaluate (x + 1). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5482 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_4, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5483 | // Evaluate (x + 1) finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5484 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5485 | // Must have result 108. |
| 5486 | CHECK_EQ(108, evaluate_int_result); |
| 5487 | // 6:Continue evaluation of dog(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5488 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_5, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5489 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5490 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5491 | // Must have result 107. |
| 5492 | CHECK_EQ(107, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5493 | // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint |
| 5494 | // in cat(19). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5495 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_6, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5496 | // Message callback gets break event. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5497 | breakpoints_barriers->semaphore_1.Wait(); // wait for break event |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5498 | // Must have hit breakpoint #1. |
| 5499 | CHECK_EQ(1, break_event_breakpoint_id); |
| 5500 | // 8: Evaluate dog() with breaks disabled. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5501 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_7, buffer)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5502 | // Evaluate dog() finishes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5503 | breakpoints_barriers->semaphore_1.Wait(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5504 | // Must have result 116. |
| 5505 | CHECK_EQ(116, evaluate_int_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5506 | // 9: Continue evaluation of source2, reach end. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5507 | v8::Debug::SendCommand(isolate_, buffer, AsciiToUtf16(command_8, buffer)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5508 | breakpoints_barriers->barrier_4.Wait(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5509 | } |
| 5510 | |
Leon Clarke | 888f672 | 2010-01-27 15:57:47 +0000 | [diff] [blame] | 5511 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5512 | void TestRecursiveBreakpointsGeneric(bool global_evaluate) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5513 | BreakpointsV8Thread breakpoints_v8_thread; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5514 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5515 | // Create a V8 environment |
| 5516 | Barriers stack_allocated_breakpoints_barriers; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5517 | breakpoints_barriers = &stack_allocated_breakpoints_barriers; |
| 5518 | |
| 5519 | breakpoints_v8_thread.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5520 | breakpoints_barriers->barrier_3.Wait(); |
| 5521 | BreakpointsDebuggerThread breakpoints_debugger_thread( |
| 5522 | global_evaluate, breakpoints_v8_thread.isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5523 | breakpoints_debugger_thread.Start(); |
| 5524 | |
| 5525 | breakpoints_v8_thread.Join(); |
| 5526 | breakpoints_debugger_thread.Join(); |
| 5527 | } |
| 5528 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5529 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5530 | TEST(RecursiveBreakpoints) { |
| 5531 | TestRecursiveBreakpointsGeneric(false); |
| 5532 | } |
| 5533 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5534 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5535 | TEST(RecursiveBreakpointsGlobal) { |
| 5536 | TestRecursiveBreakpointsGeneric(true); |
| 5537 | } |
| 5538 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5539 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5540 | static void DummyDebugEventListener( |
| 5541 | const v8::Debug::EventDetails& event_details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | |
| 5545 | TEST(SetDebugEventListenerOnUninitializedVM) { |
| 5546 | v8::Debug::SetDebugEventListener(DummyDebugEventListener); |
| 5547 | } |
| 5548 | |
| 5549 | |
| 5550 | static void DummyMessageHandler(const v8::Debug::Message& message) { |
| 5551 | } |
| 5552 | |
| 5553 | |
| 5554 | TEST(SetMessageHandlerOnUninitializedVM) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5555 | v8::Debug::SetMessageHandler(DummyMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | |
| 5559 | // Source for a JavaScript function which returns the data parameter of a |
| 5560 | // function called in the context of the debugger. If no data parameter is |
| 5561 | // passed it throws an exception. |
| 5562 | static const char* debugger_call_with_data_source = |
| 5563 | "function debugger_call_with_data(exec_state, data) {" |
| 5564 | " if (data) return data;" |
| 5565 | " throw 'No data!'" |
| 5566 | "}"; |
| 5567 | v8::Handle<v8::Function> debugger_call_with_data; |
| 5568 | |
| 5569 | |
| 5570 | // Source for a JavaScript function which returns the data parameter of a |
| 5571 | // function called in the context of the debugger. If no data parameter is |
| 5572 | // passed it throws an exception. |
| 5573 | static const char* debugger_call_with_closure_source = |
| 5574 | "var x = 3;" |
| 5575 | "(function (exec_state) {" |
| 5576 | " if (exec_state.y) return x - 1;" |
| 5577 | " exec_state.y = x;" |
| 5578 | " return exec_state.y" |
| 5579 | "})"; |
| 5580 | v8::Handle<v8::Function> debugger_call_with_closure; |
| 5581 | |
| 5582 | // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 5583 | // in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5584 | static void CheckFrameCount(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5585 | CHECK(v8::Debug::Call(frame_count)->IsNumber()); |
| 5586 | CHECK_EQ(args[0]->Int32Value(), |
| 5587 | v8::Debug::Call(frame_count)->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | |
| 5591 | // Function to retrieve the source line of the top JavaScript frame by calling a |
| 5592 | // JavaScript function in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5593 | static void CheckSourceLine(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5594 | CHECK(v8::Debug::Call(frame_source_line)->IsNumber()); |
| 5595 | CHECK_EQ(args[0]->Int32Value(), |
| 5596 | v8::Debug::Call(frame_source_line)->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | |
| 5600 | // Function to test passing an additional parameter to a JavaScript function |
| 5601 | // called in the debugger. It also tests that functions called in the debugger |
| 5602 | // can throw exceptions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5603 | static void CheckDataParameter( |
| 5604 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 5605 | v8::Handle<v8::String> data = |
| 5606 | v8::String::NewFromUtf8(args.GetIsolate(), "Test"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5607 | CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString()); |
| 5608 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5609 | for (int i = 0; i < 3; i++) { |
| 5610 | v8::TryCatch catcher; |
| 5611 | CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 5612 | CHECK(catcher.HasCaught()); |
| 5613 | CHECK(catcher.Exception()->IsString()); |
| 5614 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | |
| 5618 | // Function to test using a JavaScript with closure in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5619 | static void CheckClosure(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5620 | CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber()); |
| 5621 | CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | |
| 5625 | // Test functions called through the debugger. |
| 5626 | TEST(CallFunctionInDebugger) { |
| 5627 | // Create and enter a context with the functions CheckFrameCount, |
| 5628 | // CheckSourceLine and CheckDataParameter installed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5629 | v8::Isolate* isolate = CcTest::isolate(); |
| 5630 | v8::HandleScope scope(isolate); |
| 5631 | v8::Handle<v8::ObjectTemplate> global_template = |
| 5632 | v8::ObjectTemplate::New(isolate); |
| 5633 | global_template->Set( |
| 5634 | v8::String::NewFromUtf8(isolate, "CheckFrameCount"), |
| 5635 | v8::FunctionTemplate::New(isolate, CheckFrameCount)); |
| 5636 | global_template->Set( |
| 5637 | v8::String::NewFromUtf8(isolate, "CheckSourceLine"), |
| 5638 | v8::FunctionTemplate::New(isolate, CheckSourceLine)); |
| 5639 | global_template->Set( |
| 5640 | v8::String::NewFromUtf8(isolate, "CheckDataParameter"), |
| 5641 | v8::FunctionTemplate::New(isolate, CheckDataParameter)); |
| 5642 | global_template->Set( |
| 5643 | v8::String::NewFromUtf8(isolate, "CheckClosure"), |
| 5644 | v8::FunctionTemplate::New(isolate, CheckClosure)); |
| 5645 | v8::Handle<v8::Context> context = v8::Context::New(isolate, |
| 5646 | NULL, |
| 5647 | global_template); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5648 | v8::Context::Scope context_scope(context); |
| 5649 | |
| 5650 | // Compile a function for checking the number of JavaScript frames. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5651 | v8::Script::Compile( |
| 5652 | v8::String::NewFromUtf8(isolate, frame_count_source))->Run(); |
| 5653 | frame_count = v8::Local<v8::Function>::Cast(context->Global()->Get( |
| 5654 | v8::String::NewFromUtf8(isolate, "frame_count"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5655 | |
| 5656 | // Compile a function for returning the source line for the top frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5657 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5658 | frame_source_line_source))->Run(); |
| 5659 | frame_source_line = v8::Local<v8::Function>::Cast(context->Global()->Get( |
| 5660 | v8::String::NewFromUtf8(isolate, "frame_source_line"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5661 | |
| 5662 | // Compile a function returning the data parameter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5663 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5664 | debugger_call_with_data_source)) |
| 5665 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5666 | debugger_call_with_data = v8::Local<v8::Function>::Cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5667 | context->Global()->Get(v8::String::NewFromUtf8( |
| 5668 | isolate, "debugger_call_with_data"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5669 | |
| 5670 | // Compile a function capturing closure. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5671 | debugger_call_with_closure = |
| 5672 | v8::Local<v8::Function>::Cast(v8::Script::Compile( |
| 5673 | v8::String::NewFromUtf8(isolate, |
| 5674 | debugger_call_with_closure_source))->Run()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5675 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5676 | // Calling a function through the debugger returns 0 frames if there are |
| 5677 | // no JavaScript frames. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5678 | CHECK_EQ(v8::Integer::New(isolate, 0), |
| 5679 | v8::Debug::Call(frame_count)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5680 | |
| 5681 | // Test that the number of frames can be retrieved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5682 | v8::Script::Compile( |
| 5683 | v8::String::NewFromUtf8(isolate, "CheckFrameCount(1)"))->Run(); |
| 5684 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5685 | "function f() {" |
| 5686 | " CheckFrameCount(2);" |
| 5687 | "}; f()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5688 | |
| 5689 | // Test that the source line can be retrieved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5690 | v8::Script::Compile( |
| 5691 | v8::String::NewFromUtf8(isolate, "CheckSourceLine(0)"))->Run(); |
| 5692 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5693 | "function f() {\n" |
| 5694 | " CheckSourceLine(1)\n" |
| 5695 | " CheckSourceLine(2)\n" |
| 5696 | " CheckSourceLine(3)\n" |
| 5697 | "}; f()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5698 | |
| 5699 | // Test that a parameter can be passed to a function called in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5700 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5701 | "CheckDataParameter()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5702 | |
| 5703 | // Test that a function with closure can be run in the debugger. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5704 | v8::Script::Compile( |
| 5705 | v8::String::NewFromUtf8(isolate, "CheckClosure()"))->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5706 | |
| 5707 | // Test that the source line is correct when there is a line offset. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5708 | v8::ScriptOrigin origin(v8::String::NewFromUtf8(isolate, "test"), |
| 5709 | v8::Integer::New(isolate, 7)); |
| 5710 | v8::Script::Compile( |
| 5711 | v8::String::NewFromUtf8(isolate, "CheckSourceLine(7)"), &origin) |
| 5712 | ->Run(); |
| 5713 | v8::Script::Compile(v8::String::NewFromUtf8(isolate, |
| 5714 | "function f() {\n" |
| 5715 | " CheckSourceLine(8)\n" |
| 5716 | " CheckSourceLine(9)\n" |
| 5717 | " CheckSourceLine(10)\n" |
| 5718 | "}; f()"), |
| 5719 | &origin)->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | |
| 5723 | // Debugger message handler which counts the number of breaks. |
| 5724 | static void SendContinueCommand(); |
| 5725 | static void MessageHandlerBreakPointHitCount( |
| 5726 | const v8::Debug::Message& message) { |
| 5727 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 5728 | // Count the number of breaks. |
| 5729 | break_point_hit_count++; |
| 5730 | |
| 5731 | SendContinueCommand(); |
| 5732 | } |
| 5733 | } |
| 5734 | |
| 5735 | |
| 5736 | // Test that clearing the debug event listener actually clears all break points |
| 5737 | // and related information. |
| 5738 | TEST(DebuggerUnload) { |
| 5739 | DebugLocalContext env; |
| 5740 | |
| 5741 | // Check debugger is unloaded before it is used. |
| 5742 | CheckDebuggerUnloaded(); |
| 5743 | |
| 5744 | // Set a debug event listener. |
| 5745 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5746 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
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 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5749 | // Create a couple of functions for the test. |
| 5750 | v8::Local<v8::Function> foo = |
| 5751 | CompileFunction(&env, "function foo(){x=1}", "foo"); |
| 5752 | v8::Local<v8::Function> bar = |
| 5753 | CompileFunction(&env, "function bar(){y=2}", "bar"); |
| 5754 | |
| 5755 | // Set some break points. |
| 5756 | SetBreakPoint(foo, 0); |
| 5757 | SetBreakPoint(foo, 4); |
| 5758 | SetBreakPoint(bar, 0); |
| 5759 | SetBreakPoint(bar, 4); |
| 5760 | |
| 5761 | // Make sure that the break points are there. |
| 5762 | break_point_hit_count = 0; |
| 5763 | foo->Call(env->Global(), 0, NULL); |
| 5764 | CHECK_EQ(2, break_point_hit_count); |
| 5765 | bar->Call(env->Global(), 0, NULL); |
| 5766 | CHECK_EQ(4, break_point_hit_count); |
| 5767 | } |
| 5768 | |
| 5769 | // Remove the debug event listener without clearing breakpoints. Do this |
| 5770 | // outside a handle scope. |
| 5771 | v8::Debug::SetDebugEventListener(NULL); |
| 5772 | CheckDebuggerUnloaded(true); |
| 5773 | |
| 5774 | // Now set a debug message handler. |
| 5775 | break_point_hit_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5776 | v8::Debug::SetMessageHandler(MessageHandlerBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5777 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5778 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5779 | |
| 5780 | // Get the test functions again. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5781 | v8::Local<v8::Function> foo(v8::Local<v8::Function>::Cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5782 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5783 | |
| 5784 | foo->Call(env->Global(), 0, NULL); |
| 5785 | CHECK_EQ(0, break_point_hit_count); |
| 5786 | |
| 5787 | // Set break points and run again. |
| 5788 | SetBreakPoint(foo, 0); |
| 5789 | SetBreakPoint(foo, 4); |
| 5790 | foo->Call(env->Global(), 0, NULL); |
| 5791 | CHECK_EQ(2, break_point_hit_count); |
| 5792 | } |
| 5793 | |
| 5794 | // Remove the debug message handler without clearing breakpoints. Do this |
| 5795 | // outside a handle scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5796 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5797 | CheckDebuggerUnloaded(true); |
| 5798 | } |
| 5799 | |
| 5800 | |
| 5801 | // Sends continue command to the debugger. |
| 5802 | static void SendContinueCommand() { |
| 5803 | const int kBufferSize = 1000; |
| 5804 | uint16_t buffer[kBufferSize]; |
| 5805 | const char* command_continue = |
| 5806 | "{\"seq\":0," |
| 5807 | "\"type\":\"request\"," |
| 5808 | "\"command\":\"continue\"}"; |
| 5809 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5810 | v8::Debug::SendCommand( |
| 5811 | CcTest::isolate(), buffer, AsciiToUtf16(command_continue, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | |
| 5815 | // Debugger message handler which counts the number of times it is called. |
| 5816 | static int message_handler_hit_count = 0; |
| 5817 | static void MessageHandlerHitCount(const v8::Debug::Message& message) { |
| 5818 | message_handler_hit_count++; |
| 5819 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5820 | static char print_buffer[1000]; |
| 5821 | v8::String::Value json(message.GetJSON()); |
| 5822 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5823 | if (IsExceptionEventMessage(print_buffer)) { |
| 5824 | // Send a continue command for exception events. |
| 5825 | SendContinueCommand(); |
| 5826 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | |
| 5830 | // Test clearing the debug message handler. |
| 5831 | TEST(DebuggerClearMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5832 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5833 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5834 | |
| 5835 | // Check debugger is unloaded before it is used. |
| 5836 | CheckDebuggerUnloaded(); |
| 5837 | |
| 5838 | // Set a debug message handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5839 | v8::Debug::SetMessageHandler(MessageHandlerHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5840 | |
| 5841 | // Run code to throw a unhandled exception. This should end up in the message |
| 5842 | // handler. |
| 5843 | CompileRun("throw 1"); |
| 5844 | |
| 5845 | // The message handler should be called. |
| 5846 | CHECK_GT(message_handler_hit_count, 0); |
| 5847 | |
| 5848 | // Clear debug message handler. |
| 5849 | message_handler_hit_count = 0; |
| 5850 | v8::Debug::SetMessageHandler(NULL); |
| 5851 | |
| 5852 | // Run code to throw a unhandled exception. This should end up in the message |
| 5853 | // handler. |
| 5854 | CompileRun("throw 1"); |
| 5855 | |
| 5856 | // The message handler should not be called more. |
| 5857 | CHECK_EQ(0, message_handler_hit_count); |
| 5858 | |
| 5859 | CheckDebuggerUnloaded(true); |
| 5860 | } |
| 5861 | |
| 5862 | |
| 5863 | // Debugger message handler which clears the message handler while active. |
| 5864 | static void MessageHandlerClearingMessageHandler( |
| 5865 | const v8::Debug::Message& message) { |
| 5866 | message_handler_hit_count++; |
| 5867 | |
| 5868 | // Clear debug message handler. |
| 5869 | v8::Debug::SetMessageHandler(NULL); |
| 5870 | } |
| 5871 | |
| 5872 | |
| 5873 | // Test clearing the debug message handler while processing a debug event. |
| 5874 | TEST(DebuggerClearMessageHandlerWhileActive) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5875 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5876 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5877 | |
| 5878 | // Check debugger is unloaded before it is used. |
| 5879 | CheckDebuggerUnloaded(); |
| 5880 | |
| 5881 | // Set a debug message handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5882 | v8::Debug::SetMessageHandler(MessageHandlerClearingMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Run code to throw a unhandled exception. This should end up in the message |
| 5885 | // handler. |
| 5886 | CompileRun("throw 1"); |
| 5887 | |
| 5888 | // The message handler should be called. |
| 5889 | CHECK_EQ(1, message_handler_hit_count); |
| 5890 | |
| 5891 | CheckDebuggerUnloaded(true); |
| 5892 | } |
| 5893 | |
| 5894 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5895 | // Test for issue http://code.google.com/p/v8/issues/detail?id=289. |
| 5896 | // Make sure that DebugGetLoadedScripts doesn't return scripts |
| 5897 | // with disposed external source. |
| 5898 | class EmptyExternalStringResource : public v8::String::ExternalStringResource { |
| 5899 | public: |
| 5900 | EmptyExternalStringResource() { empty_[0] = 0; } |
| 5901 | virtual ~EmptyExternalStringResource() {} |
| 5902 | virtual size_t length() const { return empty_.length(); } |
| 5903 | virtual const uint16_t* data() const { return empty_.start(); } |
| 5904 | private: |
| 5905 | ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; |
| 5906 | }; |
| 5907 | |
| 5908 | |
| 5909 | TEST(DebugGetLoadedScripts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5910 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5911 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5912 | env.ExposeDebug(); |
| 5913 | |
| 5914 | EmptyExternalStringResource source_ext_str; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5915 | v8::Local<v8::String> source = |
| 5916 | v8::String::NewExternal(env->GetIsolate(), &source_ext_str); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5917 | v8::Handle<v8::Script> evil_script(v8::Script::Compile(source)); |
| 5918 | // "use" evil_script to make the compiler happy. |
| 5919 | (void) evil_script; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5920 | Handle<i::ExternalTwoByteString> i_source( |
| 5921 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 5922 | // This situation can happen if source was an external string disposed |
| 5923 | // by its owner. |
| 5924 | i_source->set_resource(0); |
| 5925 | |
| 5926 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 5927 | i::FLAG_allow_natives_syntax = true; |
| 5928 | CompileRun( |
| 5929 | "var scripts = %DebugGetLoadedScripts();" |
| 5930 | "var count = scripts.length;" |
| 5931 | "for (var i = 0; i < count; ++i) {" |
| 5932 | " scripts[i].line_ends;" |
| 5933 | "}"); |
| 5934 | // Must not crash while accessing line_ends. |
| 5935 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
| 5936 | |
| 5937 | // Some scripts are retrieved - at least the number of native scripts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5938 | CHECK_GT((*env) |
| 5939 | ->Global() |
| 5940 | ->Get(v8::String::NewFromUtf8(env->GetIsolate(), "count")) |
| 5941 | ->Int32Value(), |
| 5942 | 8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | |
| 5946 | // Test script break points set on lines. |
| 5947 | TEST(ScriptNameAndData) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5948 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5949 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5950 | env.ExposeDebug(); |
| 5951 | |
| 5952 | // Create functions for retrieving script name and data for the function on |
| 5953 | // the top frame when hitting a break point. |
| 5954 | frame_script_name = CompileFunction(&env, |
| 5955 | frame_script_name_source, |
| 5956 | "frame_script_name"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5957 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5958 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5959 | |
| 5960 | // Test function source. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5961 | v8::Local<v8::String> script = v8::String::NewFromUtf8(env->GetIsolate(), |
| 5962 | "function f() {\n" |
| 5963 | " debugger;\n" |
| 5964 | "}\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5965 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5966 | v8::ScriptOrigin origin1 = |
| 5967 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "name")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5968 | v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5969 | script1->Run(); |
| 5970 | v8::Local<v8::Function> f; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5971 | f = v8::Local<v8::Function>::Cast( |
| 5972 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5973 | |
| 5974 | f->Call(env->Global(), 0, NULL); |
| 5975 | CHECK_EQ(1, break_point_hit_count); |
| 5976 | CHECK_EQ("name", last_script_name_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5977 | |
| 5978 | // Compile the same script again without setting data. As the compilation |
| 5979 | // cache is disabled when debugging expect the data to be missing. |
| 5980 | v8::Script::Compile(script, &origin1)->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5981 | f = v8::Local<v8::Function>::Cast( |
| 5982 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5983 | f->Call(env->Global(), 0, NULL); |
| 5984 | CHECK_EQ(2, break_point_hit_count); |
| 5985 | CHECK_EQ("name", last_script_name_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5986 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5987 | v8::Local<v8::String> data_obj_source = v8::String::NewFromUtf8( |
| 5988 | env->GetIsolate(), |
| 5989 | "({ a: 'abc',\n" |
| 5990 | " b: 123,\n" |
| 5991 | " toString: function() { return this.a + ' ' + this.b; }\n" |
| 5992 | "})\n"); |
| 5993 | v8::Script::Compile(data_obj_source)->Run(); |
| 5994 | v8::ScriptOrigin origin2 = |
| 5995 | v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "new name")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5996 | v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2); |
| 5997 | script2->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5998 | f = v8::Local<v8::Function>::Cast( |
| 5999 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6000 | f->Call(env->Global(), 0, NULL); |
| 6001 | CHECK_EQ(3, break_point_hit_count); |
| 6002 | CHECK_EQ("new name", last_script_name_hit); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6003 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6004 | v8::Handle<v8::Script> script3 = v8::Script::Compile(script, &origin2); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6005 | script3->Run(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6006 | f = v8::Local<v8::Function>::Cast( |
| 6007 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6008 | f->Call(env->Global(), 0, NULL); |
| 6009 | CHECK_EQ(4, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6013 | static v8::Handle<v8::Context> expected_context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6014 | static v8::Handle<v8::Value> expected_context_data; |
| 6015 | |
| 6016 | |
| 6017 | // Check that the expected context is the one generating the debug event. |
| 6018 | static void ContextCheckMessageHandler(const v8::Debug::Message& message) { |
| 6019 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6020 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6021 | expected_context_data)); |
| 6022 | message_handler_hit_count++; |
| 6023 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6024 | static char print_buffer[1000]; |
| 6025 | v8::String::Value json(message.GetJSON()); |
| 6026 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6027 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6028 | // Send a continue command for break events. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6029 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6030 | SendContinueCommand(); |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | |
| 6035 | // Test which creates two contexts and sets different embedder data on each. |
| 6036 | // Checks that this data is set correctly and that when the debug message |
| 6037 | // handler is called the expected context is the one active. |
| 6038 | TEST(ContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6039 | v8::Isolate* isolate = CcTest::isolate(); |
| 6040 | v8::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6041 | |
| 6042 | // Create two contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6043 | v8::Handle<v8::Context> context_1; |
| 6044 | v8::Handle<v8::Context> context_2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6045 | v8::Handle<v8::ObjectTemplate> global_template = |
| 6046 | v8::Handle<v8::ObjectTemplate>(); |
| 6047 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6048 | context_1 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6049 | context_2 = v8::Context::New(isolate, NULL, global_template, global_object); |
| 6050 | |
| 6051 | v8::Debug::SetMessageHandler(ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6052 | |
| 6053 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6054 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
| 6055 | CHECK(context_2->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6056 | |
| 6057 | // Set and check different data values. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6058 | v8::Handle<v8::String> data_1 = v8::String::NewFromUtf8(isolate, "1"); |
| 6059 | v8::Handle<v8::String> data_2 = v8::String::NewFromUtf8(isolate, "2"); |
| 6060 | context_1->SetEmbedderData(0, data_1); |
| 6061 | context_2->SetEmbedderData(0, data_2); |
| 6062 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
| 6063 | CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6064 | |
| 6065 | // Simple test function which causes a break. |
| 6066 | const char* source = "function f() { debugger; }"; |
| 6067 | |
| 6068 | // Enter and run function in the first context. |
| 6069 | { |
| 6070 | v8::Context::Scope context_scope(context_1); |
| 6071 | expected_context = context_1; |
| 6072 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6073 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6074 | f->Call(context_1->Global(), 0, NULL); |
| 6075 | } |
| 6076 | |
| 6077 | |
| 6078 | // Enter and run function in the second context. |
| 6079 | { |
| 6080 | v8::Context::Scope context_scope(context_2); |
| 6081 | expected_context = context_2; |
| 6082 | expected_context_data = data_2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6083 | v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6084 | f->Call(context_2->Global(), 0, NULL); |
| 6085 | } |
| 6086 | |
| 6087 | // Two times compile event and two times break event. |
| 6088 | CHECK_GT(message_handler_hit_count, 4); |
| 6089 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6090 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6091 | CheckDebuggerUnloaded(); |
| 6092 | } |
| 6093 | |
| 6094 | |
| 6095 | // Debug message handler which issues a debug break when it hits a break event. |
| 6096 | static int message_handler_break_hit_count = 0; |
| 6097 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
| 6098 | // Schedule a debug break for break events. |
| 6099 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6100 | message_handler_break_hit_count++; |
| 6101 | if (message_handler_break_hit_count == 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6102 | v8::Debug::DebugBreak(message.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6103 | } |
| 6104 | } |
| 6105 | |
| 6106 | // Issue a continue command if this event will not cause the VM to start |
| 6107 | // running. |
| 6108 | if (!message.WillStartRunning()) { |
| 6109 | SendContinueCommand(); |
| 6110 | } |
| 6111 | } |
| 6112 | |
| 6113 | |
| 6114 | // Test that a debug break can be scheduled while in a message handler. |
| 6115 | TEST(DebugBreakInMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6116 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6117 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6118 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6119 | v8::Debug::SetMessageHandler(DebugBreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6120 | |
| 6121 | // Test functions. |
| 6122 | const char* script = "function f() { debugger; g(); } function g() { }"; |
| 6123 | CompileRun(script); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6124 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 6125 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
| 6126 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
| 6127 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6128 | |
| 6129 | // Call f then g. The debugger statement in f will casue a break which will |
| 6130 | // cause another break. |
| 6131 | f->Call(env->Global(), 0, NULL); |
| 6132 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6133 | // Calling g will not cause any additional breaks. |
| 6134 | g->Call(env->Global(), 0, NULL); |
| 6135 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6136 | } |
| 6137 | |
| 6138 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6139 | #ifndef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6140 | // Debug event handler which gets the function on the top frame and schedules a |
| 6141 | // break a number of times. |
| 6142 | static void DebugEventDebugBreak( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6143 | const v8::Debug::EventDetails& event_details) { |
| 6144 | v8::DebugEvent event = event_details.GetEvent(); |
| 6145 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6146 | |
| 6147 | if (event == v8::Break) { |
| 6148 | break_point_hit_count++; |
| 6149 | |
| 6150 | // Get the name of the top frame function. |
| 6151 | if (!frame_function_name.IsEmpty()) { |
| 6152 | // Get the name of the function. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6153 | const int argc = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6154 | v8::Handle<v8::Value> argv[argc] = { |
| 6155 | exec_state, v8::Integer::New(CcTest::isolate(), 0) |
| 6156 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6157 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 6158 | argc, argv); |
| 6159 | if (result->IsUndefined()) { |
| 6160 | last_function_hit[0] = '\0'; |
| 6161 | } else { |
| 6162 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6163 | v8::Handle<v8::String> function_name( |
| 6164 | result->ToString(CcTest::isolate())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6165 | function_name->WriteUtf8(last_function_hit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | |
| 6169 | // Keep forcing breaks. |
| 6170 | if (break_point_hit_count < 20) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6171 | v8::Debug::DebugBreak(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6172 | } |
| 6173 | } |
| 6174 | } |
| 6175 | |
| 6176 | |
| 6177 | TEST(RegExpDebugBreak) { |
| 6178 | // This test only applies to native regexps. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6179 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6180 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6181 | |
| 6182 | // Create a function for checking the function when hitting a break point. |
| 6183 | frame_function_name = CompileFunction(&env, |
| 6184 | frame_function_name_source, |
| 6185 | "frame_function_name"); |
| 6186 | |
| 6187 | // Test RegExp which matches white spaces and comments at the begining of a |
| 6188 | // source line. |
| 6189 | const char* script = |
| 6190 | "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" |
| 6191 | "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; |
| 6192 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6193 | v8::Local<v8::Function> f = CompileFunction(env->GetIsolate(), script, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6194 | const int argc = 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6195 | v8::Handle<v8::Value> argv[argc] = { |
| 6196 | v8::String::NewFromUtf8(env->GetIsolate(), " /* xxx */ a=0;")}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6197 | v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv); |
| 6198 | CHECK_EQ(12, result->Int32Value()); |
| 6199 | |
| 6200 | v8::Debug::SetDebugEventListener(DebugEventDebugBreak); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6201 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6202 | result = f->Call(env->Global(), argc, argv); |
| 6203 | |
| 6204 | // Check that there was only one break event. Matching RegExp should not |
| 6205 | // cause Break events. |
| 6206 | CHECK_EQ(1, break_point_hit_count); |
| 6207 | CHECK_EQ("f", last_function_hit); |
| 6208 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6209 | #endif // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6210 | |
| 6211 | |
| 6212 | // Common part of EvalContextData and NestedBreakEventContextData tests. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6213 | static void ExecuteScriptForContextCheck( |
| 6214 | v8::Debug::MessageHandler message_handler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6215 | // Create a context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6216 | v8::Handle<v8::Context> context_1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6217 | v8::Handle<v8::ObjectTemplate> global_template = |
| 6218 | v8::Handle<v8::ObjectTemplate>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6219 | context_1 = |
| 6220 | v8::Context::New(CcTest::isolate(), NULL, global_template); |
| 6221 | |
| 6222 | v8::Debug::SetMessageHandler(message_handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6223 | |
| 6224 | // Default data value is undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6225 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6226 | |
| 6227 | // Set and check a data value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6228 | v8::Handle<v8::String> data_1 = |
| 6229 | v8::String::NewFromUtf8(CcTest::isolate(), "1"); |
| 6230 | context_1->SetEmbedderData(0, data_1); |
| 6231 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6232 | |
| 6233 | // Simple test function with eval that causes a break. |
| 6234 | const char* source = "function f() { eval('debugger;'); }"; |
| 6235 | |
| 6236 | // Enter and run function in the context. |
| 6237 | { |
| 6238 | v8::Context::Scope context_scope(context_1); |
| 6239 | expected_context = context_1; |
| 6240 | expected_context_data = data_1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6241 | v8::Local<v8::Function> f = CompileFunction(CcTest::isolate(), source, "f"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6242 | f->Call(context_1->Global(), 0, NULL); |
| 6243 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6244 | |
| 6245 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | // Test which creates a context and sets embedder data on it. Checks that this |
| 6250 | // data is set correctly and that when the debug message handler is called for |
| 6251 | // break event in an eval statement the expected context is the one returned by |
| 6252 | // Message.GetEventContext. |
| 6253 | TEST(EvalContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6254 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6255 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6256 | ExecuteScriptForContextCheck(ContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6257 | |
| 6258 | // One time compile event and one time break event. |
| 6259 | CHECK_GT(message_handler_hit_count, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6260 | CheckDebuggerUnloaded(); |
| 6261 | } |
| 6262 | |
| 6263 | |
| 6264 | static bool sent_eval = false; |
| 6265 | static int break_count = 0; |
| 6266 | static int continue_command_send_count = 0; |
| 6267 | // Check that the expected context is the one generating the debug event |
| 6268 | // including the case of nested break event. |
| 6269 | static void DebugEvalContextCheckMessageHandler( |
| 6270 | const v8::Debug::Message& message) { |
| 6271 | CHECK(message.GetEventContext() == expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6272 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6273 | expected_context_data)); |
| 6274 | message_handler_hit_count++; |
| 6275 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6276 | static char print_buffer[1000]; |
| 6277 | v8::String::Value json(message.GetJSON()); |
| 6278 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6279 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6280 | v8::Isolate* isolate = message.GetIsolate(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6281 | if (IsBreakEventMessage(print_buffer)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6282 | break_count++; |
| 6283 | if (!sent_eval) { |
| 6284 | sent_eval = true; |
| 6285 | |
| 6286 | const int kBufferSize = 1000; |
| 6287 | uint16_t buffer[kBufferSize]; |
| 6288 | const char* eval_command = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6289 | "{\"seq\":0," |
| 6290 | "\"type\":\"request\"," |
| 6291 | "\"command\":\"evaluate\"," |
| 6292 | "\"arguments\":{\"expression\":\"debugger;\"," |
| 6293 | "\"global\":true,\"disable_break\":false}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6294 | |
| 6295 | // Send evaluate command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6296 | v8::Debug::SendCommand( |
| 6297 | isolate, buffer, AsciiToUtf16(eval_command, buffer)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6298 | return; |
| 6299 | } else { |
| 6300 | // It's a break event caused by the evaluation request above. |
| 6301 | SendContinueCommand(); |
| 6302 | continue_command_send_count++; |
| 6303 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6304 | } else if (IsEvaluateResponseMessage(print_buffer) && |
| 6305 | continue_command_send_count < 2) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6306 | // Response to the evaluation request. We're still on the breakpoint so |
| 6307 | // send continue. |
| 6308 | SendContinueCommand(); |
| 6309 | continue_command_send_count++; |
| 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | |
| 6314 | // Tests that context returned for break event is correct when the event occurs |
| 6315 | // in 'evaluate' debugger request. |
| 6316 | TEST(NestedBreakEventContextData) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6317 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6318 | break_count = 0; |
| 6319 | message_handler_hit_count = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6320 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6321 | ExecuteScriptForContextCheck(DebugEvalContextCheckMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6322 | |
| 6323 | // One time compile event and two times break event. |
| 6324 | CHECK_GT(message_handler_hit_count, 3); |
| 6325 | |
| 6326 | // One break from the source and another from the evaluate request. |
| 6327 | CHECK_EQ(break_count, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6328 | CheckDebuggerUnloaded(); |
| 6329 | } |
| 6330 | |
| 6331 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6332 | // Debug event listener which counts the after compile events. |
| 6333 | int after_compile_message_count = 0; |
| 6334 | static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 6335 | // Count the number of scripts collected. |
| 6336 | if (message.IsEvent()) { |
| 6337 | if (message.GetEvent() == v8::AfterCompile) { |
| 6338 | after_compile_message_count++; |
| 6339 | } else if (message.GetEvent() == v8::Break) { |
| 6340 | SendContinueCommand(); |
| 6341 | } |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | |
| 6346 | // Tests that after compile event is sent as many times as there are scripts |
| 6347 | // compiled. |
| 6348 | TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6349 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6350 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6351 | after_compile_message_count = 0; |
| 6352 | const char* script = "var a=1"; |
| 6353 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6354 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
| 6355 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), script)) |
| 6356 | ->Run(); |
| 6357 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6358 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6359 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
| 6360 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 6361 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), script)) |
| 6362 | ->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6363 | |
| 6364 | // Setting listener to NULL should cause debugger unload. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6365 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6366 | CheckDebuggerUnloaded(); |
| 6367 | |
| 6368 | // Compilation cache should be disabled when debugger is active. |
| 6369 | CHECK_EQ(2, after_compile_message_count); |
| 6370 | } |
| 6371 | |
| 6372 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6373 | // Syntax error event handler which counts a number of events. |
| 6374 | int compile_error_event_count = 0; |
| 6375 | |
| 6376 | static void CompileErrorEventCounterClear() { |
| 6377 | compile_error_event_count = 0; |
| 6378 | } |
| 6379 | |
| 6380 | static void CompileErrorEventCounter( |
| 6381 | const v8::Debug::EventDetails& event_details) { |
| 6382 | v8::DebugEvent event = event_details.GetEvent(); |
| 6383 | |
| 6384 | if (event == v8::CompileError) { |
| 6385 | compile_error_event_count++; |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | |
| 6390 | // Tests that syntax error event is sent as many times as there are scripts |
| 6391 | // with syntax error compiled. |
| 6392 | TEST(SyntaxErrorMessageOnSyntaxException) { |
| 6393 | DebugLocalContext env; |
| 6394 | v8::HandleScope scope(env->GetIsolate()); |
| 6395 | |
| 6396 | // For this test, we want to break on uncaught exceptions: |
| 6397 | ChangeBreakOnException(false, true); |
| 6398 | |
| 6399 | v8::Debug::SetDebugEventListener(CompileErrorEventCounter); |
| 6400 | |
| 6401 | CompileErrorEventCounterClear(); |
| 6402 | |
| 6403 | // Check initial state. |
| 6404 | CHECK_EQ(0, compile_error_event_count); |
| 6405 | |
| 6406 | // Throws SyntaxError: Unexpected end of input |
| 6407 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "+++")); |
| 6408 | CHECK_EQ(1, compile_error_event_count); |
| 6409 | |
| 6410 | v8::Script::Compile( |
| 6411 | v8::String::NewFromUtf8(env->GetIsolate(), "/sel\\/: \\")); |
| 6412 | CHECK_EQ(2, compile_error_event_count); |
| 6413 | |
| 6414 | v8::Script::Compile( |
| 6415 | v8::String::NewFromUtf8(env->GetIsolate(), "JSON.parse('1234:')")); |
| 6416 | CHECK_EQ(2, compile_error_event_count); |
| 6417 | |
| 6418 | v8::Script::Compile( |
| 6419 | v8::String::NewFromUtf8(env->GetIsolate(), "new RegExp('/\\/\\\\');")); |
| 6420 | CHECK_EQ(2, compile_error_event_count); |
| 6421 | |
| 6422 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "throw 1;")); |
| 6423 | CHECK_EQ(2, compile_error_event_count); |
| 6424 | } |
| 6425 | |
| 6426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6427 | // Tests that break event is sent when message handler is reset. |
| 6428 | TEST(BreakMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6429 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6430 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6431 | after_compile_message_count = 0; |
| 6432 | const char* script = "function f() {};"; |
| 6433 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6434 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
| 6435 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), script)) |
| 6436 | ->Run(); |
| 6437 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6438 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6439 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
| 6440 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 6441 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 6442 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6443 | f->Call(env->Global(), 0, NULL); |
| 6444 | |
| 6445 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6446 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6447 | CheckDebuggerUnloaded(); |
| 6448 | |
| 6449 | // Compilation cache should be disabled when debugger is active. |
| 6450 | CHECK_EQ(1, after_compile_message_count); |
| 6451 | } |
| 6452 | |
| 6453 | |
| 6454 | static int exception_event_count = 0; |
| 6455 | static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 6456 | if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 6457 | exception_event_count++; |
| 6458 | SendContinueCommand(); |
| 6459 | } |
| 6460 | } |
| 6461 | |
| 6462 | |
| 6463 | // Tests that exception event is sent when message handler is reset. |
| 6464 | TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6465 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6466 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 6467 | |
| 6468 | // For this test, we want to break on uncaught exceptions: |
| 6469 | ChangeBreakOnException(false, true); |
| 6470 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6471 | exception_event_count = 0; |
| 6472 | const char* script = "function f() {throw new Error()};"; |
| 6473 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6474 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
| 6475 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), script)) |
| 6476 | ->Run(); |
| 6477 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6478 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6479 | v8::Debug::SetMessageHandler(ExceptionMessageHandler); |
| 6480 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 6481 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f"))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6482 | f->Call(env->Global(), 0, NULL); |
| 6483 | |
| 6484 | // Setting message handler to NULL should cause debugger unload. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6485 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6486 | CheckDebuggerUnloaded(); |
| 6487 | |
| 6488 | CHECK_EQ(1, exception_event_count); |
| 6489 | } |
| 6490 | |
| 6491 | |
| 6492 | // Tests after compile event is sent when there are some provisional |
| 6493 | // breakpoints out of the scripts lines range. |
| 6494 | TEST(ProvisionalBreakpointOnLineOutOfRange) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6495 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6496 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6497 | env.ExposeDebug(); |
| 6498 | const char* script = "function f() {};"; |
| 6499 | const char* resource_name = "test_resource"; |
| 6500 | |
| 6501 | // Set a couple of provisional breakpoint on lines out of the script lines |
| 6502 | // range. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6503 | int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, |
| 6504 | 3, -1 /* no column */); |
| 6505 | int sbp2 = |
| 6506 | SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, 5, 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6507 | |
| 6508 | after_compile_message_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6509 | v8::Debug::SetMessageHandler(AfterCompileMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6510 | |
| 6511 | v8::ScriptOrigin origin( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6512 | v8::String::NewFromUtf8(env->GetIsolate(), resource_name), |
| 6513 | v8::Integer::New(env->GetIsolate(), 10), |
| 6514 | v8::Integer::New(env->GetIsolate(), 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6515 | // Compile a script whose first line number is greater than the breakpoints' |
| 6516 | // lines. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6517 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), script), |
| 6518 | &origin)->Run(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6519 | |
| 6520 | // If the script is compiled successfully there is exactly one after compile |
| 6521 | // event. In case of an exception in debugger code after compile event is not |
| 6522 | // sent. |
| 6523 | CHECK_EQ(1, after_compile_message_count); |
| 6524 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6525 | ClearBreakPointFromJS(env->GetIsolate(), sbp1); |
| 6526 | ClearBreakPointFromJS(env->GetIsolate(), sbp2); |
| 6527 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
| 6530 | |
| 6531 | static void BreakMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6532 | i::Isolate* isolate = CcTest::i_isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6533 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6534 | // Count the number of breaks. |
| 6535 | break_point_hit_count++; |
| 6536 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6537 | i::HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6538 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6539 | |
| 6540 | SendContinueCommand(); |
| 6541 | } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6542 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6543 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6544 | int current_count = break_point_hit_count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6545 | |
| 6546 | // Force serialization to trigger some internal JS execution. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6547 | message.GetJSON(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6548 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6549 | CHECK_EQ(current_count, break_point_hit_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6550 | } |
| 6551 | } |
| 6552 | |
| 6553 | |
| 6554 | // Test that if DebugBreak is forced it is ignored when code from |
| 6555 | // debug-delay.js is executed. |
| 6556 | TEST(NoDebugBreakInAfterCompileMessageHandler) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6557 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6558 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6559 | |
| 6560 | // Register a debug event listener which sets the break flag and counts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6561 | v8::Debug::SetMessageHandler(BreakMessageHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6562 | |
| 6563 | // Set the debug break flag. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6564 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6565 | |
| 6566 | // Create a function for testing stepping. |
| 6567 | const char* src = "function f() { eval('var x = 10;'); } "; |
| 6568 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 6569 | |
| 6570 | // There should be only one break event. |
| 6571 | CHECK_EQ(1, break_point_hit_count); |
| 6572 | |
| 6573 | // Set the debug break flag again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6574 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6575 | f->Call(env->Global(), 0, NULL); |
| 6576 | // There should be one more break event when the script is evaluated in 'f'. |
| 6577 | CHECK_EQ(2, break_point_hit_count); |
| 6578 | |
| 6579 | // Get rid of the debug message handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6580 | v8::Debug::SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6581 | CheckDebuggerUnloaded(); |
| 6582 | } |
| 6583 | |
| 6584 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6585 | static int counting_message_handler_counter; |
| 6586 | |
| 6587 | static void CountingMessageHandler(const v8::Debug::Message& message) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6588 | if (message.IsResponse()) counting_message_handler_counter++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6591 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6592 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6593 | TEST(ProcessDebugMessages) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6594 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6595 | v8::Isolate* isolate = env->GetIsolate(); |
| 6596 | v8::HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6597 | |
| 6598 | counting_message_handler_counter = 0; |
| 6599 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6600 | v8::Debug::SetMessageHandler(CountingMessageHandler); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6601 | |
| 6602 | const int kBufferSize = 1000; |
| 6603 | uint16_t buffer[kBufferSize]; |
| 6604 | const char* scripts_command = |
| 6605 | "{\"seq\":0," |
| 6606 | "\"type\":\"request\"," |
| 6607 | "\"command\":\"scripts\"}"; |
| 6608 | |
| 6609 | // Send scripts command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6610 | v8::Debug::SendCommand( |
| 6611 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6612 | |
| 6613 | CHECK_EQ(0, counting_message_handler_counter); |
| 6614 | v8::Debug::ProcessDebugMessages(); |
| 6615 | // At least one message should come |
| 6616 | CHECK_GE(counting_message_handler_counter, 1); |
| 6617 | |
| 6618 | counting_message_handler_counter = 0; |
| 6619 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6620 | v8::Debug::SendCommand( |
| 6621 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6622 | v8::Debug::SendCommand( |
| 6623 | isolate, buffer, AsciiToUtf16(scripts_command, buffer)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6624 | CHECK_EQ(0, counting_message_handler_counter); |
| 6625 | v8::Debug::ProcessDebugMessages(); |
| 6626 | // At least two messages should come |
| 6627 | CHECK_GE(counting_message_handler_counter, 2); |
| 6628 | |
| 6629 | // Get rid of the debug message handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6630 | v8::Debug::SetMessageHandler(NULL); |
| 6631 | CheckDebuggerUnloaded(); |
| 6632 | } |
| 6633 | |
| 6634 | |
| 6635 | class SendCommandThread; |
| 6636 | static SendCommandThread* send_command_thread_ = NULL; |
| 6637 | |
| 6638 | |
| 6639 | class SendCommandThread : public v8::base::Thread { |
| 6640 | public: |
| 6641 | explicit SendCommandThread(v8::Isolate* isolate) |
| 6642 | : Thread(Options("SendCommandThread")), |
| 6643 | semaphore_(0), |
| 6644 | isolate_(isolate) {} |
| 6645 | |
| 6646 | static void CountingAndSignallingMessageHandler( |
| 6647 | const v8::Debug::Message& message) { |
| 6648 | if (message.IsResponse()) { |
| 6649 | counting_message_handler_counter++; |
| 6650 | send_command_thread_->semaphore_.Signal(); |
| 6651 | } |
| 6652 | } |
| 6653 | |
| 6654 | virtual void Run() { |
| 6655 | semaphore_.Wait(); |
| 6656 | const int kBufferSize = 1000; |
| 6657 | uint16_t buffer[kBufferSize]; |
| 6658 | const char* scripts_command = |
| 6659 | "{\"seq\":0," |
| 6660 | "\"type\":\"request\"," |
| 6661 | "\"command\":\"scripts\"}"; |
| 6662 | int length = AsciiToUtf16(scripts_command, buffer); |
| 6663 | // Send scripts command. |
| 6664 | |
| 6665 | for (int i = 0; i < 20; i++) { |
| 6666 | v8::base::ElapsedTimer timer; |
| 6667 | timer.Start(); |
| 6668 | CHECK_EQ(i, counting_message_handler_counter); |
| 6669 | // Queue debug message. |
| 6670 | v8::Debug::SendCommand(isolate_, buffer, length); |
| 6671 | // Wait for the message handler to pick up the response. |
| 6672 | semaphore_.Wait(); |
| 6673 | i::PrintF("iteration %d took %f ms\n", i, |
| 6674 | timer.Elapsed().InMillisecondsF()); |
| 6675 | } |
| 6676 | |
| 6677 | v8::V8::TerminateExecution(isolate_); |
| 6678 | } |
| 6679 | |
| 6680 | void StartSending() { semaphore_.Signal(); } |
| 6681 | |
| 6682 | private: |
| 6683 | v8::base::Semaphore semaphore_; |
| 6684 | v8::Isolate* isolate_; |
| 6685 | }; |
| 6686 | |
| 6687 | |
| 6688 | static void StartSendingCommands( |
| 6689 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 6690 | send_command_thread_->StartSending(); |
| 6691 | } |
| 6692 | |
| 6693 | |
| 6694 | TEST(ProcessDebugMessagesThreaded) { |
| 6695 | DebugLocalContext env; |
| 6696 | v8::Isolate* isolate = env->GetIsolate(); |
| 6697 | v8::HandleScope scope(isolate); |
| 6698 | |
| 6699 | counting_message_handler_counter = 0; |
| 6700 | |
| 6701 | v8::Debug::SetMessageHandler( |
| 6702 | SendCommandThread::CountingAndSignallingMessageHandler); |
| 6703 | send_command_thread_ = new SendCommandThread(isolate); |
| 6704 | send_command_thread_->Start(); |
| 6705 | |
| 6706 | v8::Handle<v8::FunctionTemplate> start = |
| 6707 | v8::FunctionTemplate::New(isolate, StartSendingCommands); |
| 6708 | env->Global()->Set(v8_str("start"), start->GetFunction()); |
| 6709 | |
| 6710 | CompileRun("start(); while (true) { }"); |
| 6711 | |
| 6712 | CHECK_EQ(20, counting_message_handler_counter); |
| 6713 | |
| 6714 | v8::Debug::SetMessageHandler(NULL); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6715 | CheckDebuggerUnloaded(); |
| 6716 | } |
| 6717 | |
| 6718 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6719 | struct BacktraceData { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6720 | static int frame_counter; |
| 6721 | static void MessageHandler(const v8::Debug::Message& message) { |
| 6722 | char print_buffer[1000]; |
| 6723 | v8::String::Value json(message.GetJSON()); |
| 6724 | Utf16ToAscii(*json, json.length(), print_buffer, 1000); |
| 6725 | |
| 6726 | if (strstr(print_buffer, "backtrace") == NULL) { |
| 6727 | return; |
| 6728 | } |
| 6729 | frame_counter = GetTotalFramesInt(print_buffer); |
| 6730 | } |
| 6731 | }; |
| 6732 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6733 | int BacktraceData::frame_counter; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6734 | |
| 6735 | |
| 6736 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6737 | TEST(Backtrace) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6738 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6739 | v8::Isolate* isolate = env->GetIsolate(); |
| 6740 | v8::HandleScope scope(isolate); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6741 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6742 | v8::Debug::SetMessageHandler(BacktraceData::MessageHandler); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6743 | |
| 6744 | const int kBufferSize = 1000; |
| 6745 | uint16_t buffer[kBufferSize]; |
| 6746 | const char* scripts_command = |
| 6747 | "{\"seq\":0," |
| 6748 | "\"type\":\"request\"," |
| 6749 | "\"command\":\"backtrace\"}"; |
| 6750 | |
| 6751 | // Check backtrace from ProcessDebugMessages. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6752 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6753 | v8::Debug::SendCommand( |
| 6754 | isolate, |
| 6755 | buffer, |
| 6756 | AsciiToUtf16(scripts_command, buffer), |
| 6757 | NULL); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6758 | v8::Debug::ProcessDebugMessages(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6759 | CHECK_EQ(BacktraceData::frame_counter, 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6760 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6761 | v8::Handle<v8::String> void0 = |
| 6762 | v8::String::NewFromUtf8(env->GetIsolate(), "void(0)"); |
| 6763 | v8::Handle<v8::Script> script = CompileWithOrigin(void0, void0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6764 | |
| 6765 | // Check backtrace from "void(0)" script. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6766 | BacktraceData::frame_counter = -10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6767 | v8::Debug::SendCommand( |
| 6768 | isolate, |
| 6769 | buffer, |
| 6770 | AsciiToUtf16(scripts_command, buffer), |
| 6771 | NULL); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6772 | script->Run(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6773 | CHECK_EQ(BacktraceData::frame_counter, 1); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6774 | |
| 6775 | // Get rid of the debug message handler. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6776 | v8::Debug::SetMessageHandler(NULL); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6777 | CheckDebuggerUnloaded(); |
| 6778 | } |
| 6779 | |
| 6780 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6781 | TEST(GetMirror) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6782 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6783 | v8::Isolate* isolate = env->GetIsolate(); |
| 6784 | v8::HandleScope scope(isolate); |
| 6785 | v8::Handle<v8::Value> obj = |
| 6786 | v8::Debug::GetMirror(v8::String::NewFromUtf8(isolate, "hodja")); |
| 6787 | v8::ScriptCompiler::Source source(v8_str( |
| 6788 | "function runTest(mirror) {" |
| 6789 | " return mirror.isString() && (mirror.length() == 5);" |
| 6790 | "}" |
| 6791 | "" |
| 6792 | "runTest;")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6793 | v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6794 | v8::ScriptCompiler::CompileUnbound(isolate, &source) |
| 6795 | ->BindToCurrentContext() |
| 6796 | ->Run()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6797 | v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj); |
| 6798 | CHECK(result->IsTrue()); |
| 6799 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6800 | |
| 6801 | |
| 6802 | // Test that the debug break flag works with function.apply. |
| 6803 | TEST(DebugBreakFunctionApply) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6804 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6805 | v8::HandleScope scope(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6806 | |
| 6807 | // Create a function for testing breaking in apply. |
| 6808 | v8::Local<v8::Function> foo = CompileFunction( |
| 6809 | &env, |
| 6810 | "function baz(x) { }" |
| 6811 | "function bar(x) { baz(); }" |
| 6812 | "function foo(){ bar.apply(this, [1]); }", |
| 6813 | "foo"); |
| 6814 | |
| 6815 | // Register a debug event listener which steps and counts. |
| 6816 | v8::Debug::SetDebugEventListener(DebugEventBreakMax); |
| 6817 | |
| 6818 | // Set the debug break flag before calling the code using function.apply. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6819 | v8::Debug::DebugBreak(env->GetIsolate()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6820 | |
| 6821 | // Limit the number of debug breaks. This is a regression test for issue 493 |
| 6822 | // where this test would enter an infinite loop. |
| 6823 | break_point_hit_count = 0; |
| 6824 | max_break_point_hit_count = 10000; // 10000 => infinite loop. |
| 6825 | foo->Call(env->Global(), 0, NULL); |
| 6826 | |
| 6827 | // When keeping the debug break several break will happen. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6828 | CHECK_GT(break_point_hit_count, 1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6829 | |
| 6830 | v8::Debug::SetDebugEventListener(NULL); |
| 6831 | CheckDebuggerUnloaded(); |
| 6832 | } |
| 6833 | |
| 6834 | |
| 6835 | v8::Handle<v8::Context> debugee_context; |
| 6836 | v8::Handle<v8::Context> debugger_context; |
| 6837 | |
| 6838 | |
| 6839 | // Property getter that checks that current and calling contexts |
| 6840 | // are both the debugee contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6841 | static void NamedGetterWithCallingContextCheck( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6842 | v8::Local<v8::String> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6843 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 6844 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(name), "a")); |
| 6845 | v8::Handle<v8::Context> current = info.GetIsolate()->GetCurrentContext(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6846 | CHECK(current == debugee_context); |
| 6847 | CHECK(current != debugger_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6848 | v8::Handle<v8::Context> calling = info.GetIsolate()->GetCallingContext(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6849 | CHECK(calling == debugee_context); |
| 6850 | CHECK(calling != debugger_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6851 | info.GetReturnValue().Set(1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | |
| 6855 | // Debug event listener that checks if the first argument of a function is |
| 6856 | // an object with property 'a' == 1. If the property has custom accessor |
| 6857 | // this handler will eventually invoke it. |
| 6858 | static void DebugEventGetAtgumentPropertyValue( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6859 | const v8::Debug::EventDetails& event_details) { |
| 6860 | v8::DebugEvent event = event_details.GetEvent(); |
| 6861 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6862 | if (event == v8::Break) { |
| 6863 | break_point_hit_count++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6864 | CHECK(debugger_context == CcTest::isolate()->GetCurrentContext()); |
| 6865 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(CompileRun( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6866 | "(function(exec_state) {\n" |
| 6867 | " return (exec_state.frame(0).argumentValue(0).property('a').\n" |
| 6868 | " value().value() == 1);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6869 | "})")); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6870 | const int argc = 1; |
| 6871 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 6872 | v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv); |
| 6873 | CHECK(result->IsTrue()); |
| 6874 | } |
| 6875 | } |
| 6876 | |
| 6877 | |
| 6878 | TEST(CallingContextIsNotDebugContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6879 | v8::internal::Debug* debug = CcTest::i_isolate()->debug(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6880 | // Create and enter a debugee context. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6881 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6882 | v8::Isolate* isolate = env->GetIsolate(); |
| 6883 | v8::HandleScope scope(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6884 | env.ExposeDebug(); |
| 6885 | |
| 6886 | // Save handles to the debugger and debugee contexts to be used in |
| 6887 | // NamedGetterWithCallingContextCheck. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6888 | debugee_context = env.context(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6889 | debugger_context = v8::Utils::ToLocal(debug->debug_context()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6890 | |
| 6891 | // Create object with 'a' property accessor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6892 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); |
| 6893 | named->SetAccessor(v8::String::NewFromUtf8(isolate, "a"), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6894 | NamedGetterWithCallingContextCheck); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6895 | env->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6896 | named->NewInstance()); |
| 6897 | |
| 6898 | // Register the debug event listener |
| 6899 | v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue); |
| 6900 | |
| 6901 | // Create a function that invokes debugger. |
| 6902 | v8::Local<v8::Function> foo = CompileFunction( |
| 6903 | &env, |
| 6904 | "function bar(x) { debugger; }" |
| 6905 | "function foo(){ bar(obj); }", |
| 6906 | "foo"); |
| 6907 | |
| 6908 | break_point_hit_count = 0; |
| 6909 | foo->Call(env->Global(), 0, NULL); |
| 6910 | CHECK_EQ(1, break_point_hit_count); |
| 6911 | |
| 6912 | v8::Debug::SetDebugEventListener(NULL); |
| 6913 | debugee_context = v8::Handle<v8::Context>(); |
| 6914 | debugger_context = v8::Handle<v8::Context>(); |
| 6915 | CheckDebuggerUnloaded(); |
| 6916 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6917 | |
| 6918 | |
| 6919 | TEST(DebugContextIsPreservedBetweenAccesses) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6920 | v8::HandleScope scope(CcTest::isolate()); |
| 6921 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6922 | v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext(); |
| 6923 | v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6924 | CHECK(v8::Utils::OpenHandle(*context1).is_identical_to( |
| 6925 | v8::Utils::OpenHandle(*context2))); |
| 6926 | v8::Debug::SetDebugEventListener(NULL); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6927 | } |
| 6928 | |
| 6929 | |
| 6930 | static v8::Handle<v8::Value> expected_callback_data; |
| 6931 | static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { |
| 6932 | CHECK(details.GetEventContext() == expected_context); |
| 6933 | CHECK_EQ(expected_callback_data, details.GetCallbackData()); |
| 6934 | } |
| 6935 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6936 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6937 | // Check that event details contain context where debug event occured. |
| 6938 | TEST(DebugEventContext) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6939 | v8::Isolate* isolate = CcTest::isolate(); |
| 6940 | v8::HandleScope scope(isolate); |
| 6941 | expected_context = v8::Context::New(isolate); |
| 6942 | expected_callback_data = v8::Int32::New(isolate, 2010); |
| 6943 | v8::Debug::SetDebugEventListener(DebugEventContextChecker, |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6944 | expected_callback_data); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6945 | v8::Context::Scope context_scope(expected_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6946 | v8::Script::Compile( |
| 6947 | v8::String::NewFromUtf8(isolate, "(function(){debugger;})();"))->Run(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6948 | expected_context.Clear(); |
| 6949 | v8::Debug::SetDebugEventListener(NULL); |
| 6950 | expected_context_data = v8::Handle<v8::Value>(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6951 | CheckDebuggerUnloaded(); |
| 6952 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6953 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6954 | |
| 6955 | static void* expected_break_data; |
| 6956 | static bool was_debug_break_called; |
| 6957 | static bool was_debug_event_called; |
| 6958 | static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) { |
| 6959 | if (details.GetEvent() == v8::BreakForCommand) { |
| 6960 | CHECK_EQ(expected_break_data, details.GetClientData()); |
| 6961 | was_debug_event_called = true; |
| 6962 | } else if (details.GetEvent() == v8::Break) { |
| 6963 | was_debug_break_called = true; |
| 6964 | } |
| 6965 | } |
| 6966 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6967 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6968 | // Check that event details contain context where debug event occured. |
| 6969 | TEST(DebugEventBreakData) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6970 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6971 | v8::Isolate* isolate = env->GetIsolate(); |
| 6972 | v8::HandleScope scope(isolate); |
| 6973 | v8::Debug::SetDebugEventListener(DebugEventBreakDataChecker); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6974 | |
| 6975 | TestClientData::constructor_call_counter = 0; |
| 6976 | TestClientData::destructor_call_counter = 0; |
| 6977 | |
| 6978 | expected_break_data = NULL; |
| 6979 | was_debug_event_called = false; |
| 6980 | was_debug_break_called = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6981 | v8::Debug::DebugBreakForCommand(isolate, NULL); |
| 6982 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 6983 | "(function(x){return x;})(1);")) |
| 6984 | ->Run(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6985 | CHECK(was_debug_event_called); |
| 6986 | CHECK(!was_debug_break_called); |
| 6987 | |
| 6988 | TestClientData* data1 = new TestClientData(); |
| 6989 | expected_break_data = data1; |
| 6990 | was_debug_event_called = false; |
| 6991 | was_debug_break_called = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6992 | v8::Debug::DebugBreakForCommand(isolate, data1); |
| 6993 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 6994 | "(function(x){return x+1;})(1);")) |
| 6995 | ->Run(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6996 | CHECK(was_debug_event_called); |
| 6997 | CHECK(!was_debug_break_called); |
| 6998 | |
| 6999 | expected_break_data = NULL; |
| 7000 | was_debug_event_called = false; |
| 7001 | was_debug_break_called = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7002 | v8::Debug::DebugBreak(isolate); |
| 7003 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 7004 | "(function(x){return x+2;})(1);")) |
| 7005 | ->Run(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7006 | CHECK(!was_debug_event_called); |
| 7007 | CHECK(was_debug_break_called); |
| 7008 | |
| 7009 | TestClientData* data2 = new TestClientData(); |
| 7010 | expected_break_data = data2; |
| 7011 | was_debug_event_called = false; |
| 7012 | was_debug_break_called = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7013 | v8::Debug::DebugBreak(isolate); |
| 7014 | v8::Debug::DebugBreakForCommand(isolate, data2); |
| 7015 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 7016 | "(function(x){return x+3;})(1);")) |
| 7017 | ->Run(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7018 | CHECK(was_debug_event_called); |
| 7019 | CHECK(was_debug_break_called); |
| 7020 | |
| 7021 | CHECK_EQ(2, TestClientData::constructor_call_counter); |
| 7022 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 7023 | TestClientData::destructor_call_counter); |
| 7024 | |
| 7025 | v8::Debug::SetDebugEventListener(NULL); |
| 7026 | CheckDebuggerUnloaded(); |
| 7027 | } |
| 7028 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7029 | static bool debug_event_break_deoptimize_done = false; |
| 7030 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7031 | static void DebugEventBreakDeoptimize( |
| 7032 | const v8::Debug::EventDetails& event_details) { |
| 7033 | v8::DebugEvent event = event_details.GetEvent(); |
| 7034 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7035 | if (event == v8::Break) { |
| 7036 | if (!frame_function_name.IsEmpty()) { |
| 7037 | // Get the name of the function. |
| 7038 | const int argc = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7039 | v8::Handle<v8::Value> argv[argc] = { |
| 7040 | exec_state, v8::Integer::New(CcTest::isolate(), 0) |
| 7041 | }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7042 | v8::Handle<v8::Value> result = |
| 7043 | frame_function_name->Call(exec_state, argc, argv); |
| 7044 | if (!result->IsUndefined()) { |
| 7045 | char fn[80]; |
| 7046 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7047 | v8::Handle<v8::String> function_name( |
| 7048 | result->ToString(CcTest::isolate())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7049 | function_name->WriteUtf8(fn); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7050 | if (strcmp(fn, "bar") == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7051 | i::Deoptimizer::DeoptimizeAll(CcTest::i_isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7052 | debug_event_break_deoptimize_done = true; |
| 7053 | } |
| 7054 | } |
| 7055 | } |
| 7056 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7057 | v8::Debug::DebugBreak(CcTest::isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7058 | } |
| 7059 | } |
| 7060 | |
| 7061 | |
| 7062 | // Test deoptimization when execution is broken using the debug break stack |
| 7063 | // check interrupt. |
| 7064 | TEST(DeoptimizeDuringDebugBreak) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7065 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7066 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7067 | env.ExposeDebug(); |
| 7068 | |
| 7069 | // Create a function for checking the function when hitting a break point. |
| 7070 | frame_function_name = CompileFunction(&env, |
| 7071 | frame_function_name_source, |
| 7072 | "frame_function_name"); |
| 7073 | |
| 7074 | |
| 7075 | // Set a debug event listener which will keep interrupting execution until |
| 7076 | // debug break. When inside function bar it will deoptimize all functions. |
| 7077 | // This tests lazy deoptimization bailout for the stack check, as the first |
| 7078 | // time in function bar when using debug break and no break points will be at |
| 7079 | // the initial stack check. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7080 | v8::Debug::SetDebugEventListener(DebugEventBreakDeoptimize); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7081 | |
| 7082 | // Compile and run function bar which will optimize it for some flag settings. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7083 | v8::Script::Compile(v8::String::NewFromUtf8( |
| 7084 | env->GetIsolate(), "function bar(){}; bar()"))->Run(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7085 | |
| 7086 | // Set debug break and call bar again. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7087 | v8::Debug::DebugBreak(env->GetIsolate()); |
| 7088 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "bar()")) |
| 7089 | ->Run(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7090 | |
| 7091 | CHECK(debug_event_break_deoptimize_done); |
| 7092 | |
| 7093 | v8::Debug::SetDebugEventListener(NULL); |
| 7094 | } |
| 7095 | |
| 7096 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7097 | static void DebugEventBreakWithOptimizedStack( |
| 7098 | const v8::Debug::EventDetails& event_details) { |
| 7099 | v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); |
| 7100 | v8::DebugEvent event = event_details.GetEvent(); |
| 7101 | v8::Handle<v8::Object> exec_state = event_details.GetExecutionState(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7102 | if (event == v8::Break) { |
| 7103 | if (!frame_function_name.IsEmpty()) { |
| 7104 | for (int i = 0; i < 2; i++) { |
| 7105 | const int argc = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7106 | v8::Handle<v8::Value> argv[argc] = { |
| 7107 | exec_state, v8::Integer::New(isolate, i) |
| 7108 | }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7109 | // Get the name of the function in frame i. |
| 7110 | v8::Handle<v8::Value> result = |
| 7111 | frame_function_name->Call(exec_state, argc, argv); |
| 7112 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7113 | v8::Handle<v8::String> function_name(result->ToString(isolate)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7114 | CHECK(function_name->Equals(v8::String::NewFromUtf8(isolate, "loop"))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7115 | // Get the name of the first argument in frame i. |
| 7116 | result = frame_argument_name->Call(exec_state, argc, argv); |
| 7117 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7118 | v8::Handle<v8::String> argument_name(result->ToString(isolate)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7119 | CHECK(argument_name->Equals(v8::String::NewFromUtf8(isolate, "count"))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7120 | // Get the value of the first argument in frame i. If the |
| 7121 | // funtion is optimized the value will be undefined, otherwise |
| 7122 | // the value will be '1 - i'. |
| 7123 | // |
| 7124 | // TODO(3141533): We should be able to get the real value for |
| 7125 | // optimized frames. |
| 7126 | result = frame_argument_value->Call(exec_state, argc, argv); |
| 7127 | CHECK(result->IsUndefined() || (result->Int32Value() == 1 - i)); |
| 7128 | // Get the name of the first local variable. |
| 7129 | result = frame_local_name->Call(exec_state, argc, argv); |
| 7130 | CHECK(result->IsString()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7131 | v8::Handle<v8::String> local_name(result->ToString(isolate)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7132 | CHECK(local_name->Equals(v8::String::NewFromUtf8(isolate, "local"))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7133 | // Get the value of the first local variable. If the function |
| 7134 | // is optimized the value will be undefined, otherwise it will |
| 7135 | // be 42. |
| 7136 | // |
| 7137 | // TODO(3141533): We should be able to get the real value for |
| 7138 | // optimized frames. |
| 7139 | result = frame_local_value->Call(exec_state, argc, argv); |
| 7140 | CHECK(result->IsUndefined() || (result->Int32Value() == 42)); |
| 7141 | } |
| 7142 | } |
| 7143 | } |
| 7144 | } |
| 7145 | |
| 7146 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7147 | static void ScheduleBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7148 | v8::Debug::SetDebugEventListener(DebugEventBreakWithOptimizedStack); |
| 7149 | v8::Debug::DebugBreak(args.GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7150 | } |
| 7151 | |
| 7152 | |
| 7153 | TEST(DebugBreakStackInspection) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7154 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7155 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7156 | |
| 7157 | frame_function_name = |
| 7158 | CompileFunction(&env, frame_function_name_source, "frame_function_name"); |
| 7159 | frame_argument_name = |
| 7160 | CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); |
| 7161 | frame_argument_value = CompileFunction(&env, |
| 7162 | frame_argument_value_source, |
| 7163 | "frame_argument_value"); |
| 7164 | frame_local_name = |
| 7165 | CompileFunction(&env, frame_local_name_source, "frame_local_name"); |
| 7166 | frame_local_value = |
| 7167 | CompileFunction(&env, frame_local_value_source, "frame_local_value"); |
| 7168 | |
| 7169 | v8::Handle<v8::FunctionTemplate> schedule_break_template = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7170 | v8::FunctionTemplate::New(env->GetIsolate(), ScheduleBreak); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7171 | v8::Handle<v8::Function> schedule_break = |
| 7172 | schedule_break_template->GetFunction(); |
| 7173 | env->Global()->Set(v8_str("scheduleBreak"), schedule_break); |
| 7174 | |
| 7175 | const char* src = |
| 7176 | "function loop(count) {" |
| 7177 | " var local = 42;" |
| 7178 | " if (count < 1) { scheduleBreak(); loop(count + 1); }" |
| 7179 | "}" |
| 7180 | "loop(0);"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7181 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), src))->Run(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7182 | } |
| 7183 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7184 | |
| 7185 | // Test that setting the terminate execution flag during debug break processing. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7186 | static void TestDebugBreakInLoop(const char* loop_head, |
| 7187 | const char** loop_bodies, |
| 7188 | const char* loop_tail) { |
| 7189 | // Receive 100 breaks for each test and then terminate JavaScript execution. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7190 | static const int kBreaksPerTest = 100; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7191 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7192 | for (int i = 0; loop_bodies[i] != NULL; i++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7193 | // Perform a lazy deoptimization after various numbers of breaks |
| 7194 | // have been hit. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7195 | for (int j = 0; j < 7; j++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7196 | break_point_hit_count_deoptimize = j; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7197 | if (j == 6) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7198 | break_point_hit_count_deoptimize = kBreaksPerTest; |
| 7199 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7200 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7201 | break_point_hit_count = 0; |
| 7202 | max_break_point_hit_count = kBreaksPerTest; |
| 7203 | terminate_after_max_break_point_hit = true; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7204 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7205 | EmbeddedVector<char, 1024> buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7206 | SNPrintF(buffer, |
| 7207 | "function f() {%s%s%s}", |
| 7208 | loop_head, loop_bodies[i], loop_tail); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7209 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7210 | // Function with infinite loop. |
| 7211 | CompileRun(buffer.start()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7212 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7213 | // Set the debug break to enter the debugger as soon as possible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7214 | v8::Debug::DebugBreak(CcTest::isolate()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7215 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7216 | // Call function with infinite loop. |
| 7217 | CompileRun("f();"); |
| 7218 | CHECK_EQ(kBreaksPerTest, break_point_hit_count); |
| 7219 | |
| 7220 | CHECK(!v8::V8::IsExecutionTerminating()); |
| 7221 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7226 | TEST(DebugBreakLoop) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7227 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7228 | v8::HandleScope scope(env->GetIsolate()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7229 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7230 | // Register a debug event listener which sets the break flag and counts. |
| 7231 | v8::Debug::SetDebugEventListener(DebugEventBreakMax); |
| 7232 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7233 | // Create a function for getting the frame count when hitting the break. |
| 7234 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 7235 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7236 | CompileRun("var a = 1;"); |
| 7237 | CompileRun("function g() { }"); |
| 7238 | CompileRun("function h() { }"); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7239 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7240 | const char* loop_bodies[] = { |
| 7241 | "", |
| 7242 | "g()", |
| 7243 | "if (a == 0) { g() }", |
| 7244 | "if (a == 1) { g() }", |
| 7245 | "if (a == 0) { g() } else { h() }", |
| 7246 | "if (a == 0) { continue }", |
| 7247 | "if (a == 1) { continue }", |
| 7248 | "switch (a) { case 1: g(); }", |
| 7249 | "switch (a) { case 1: continue; }", |
| 7250 | "switch (a) { case 1: g(); break; default: h() }", |
| 7251 | "switch (a) { case 1: continue; break; default: h() }", |
| 7252 | NULL |
| 7253 | }; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7254 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7255 | TestDebugBreakInLoop("while (true) {", loop_bodies, "}"); |
| 7256 | TestDebugBreakInLoop("while (a == 1) {", loop_bodies, "}"); |
| 7257 | |
| 7258 | TestDebugBreakInLoop("do {", loop_bodies, "} while (true)"); |
| 7259 | TestDebugBreakInLoop("do {", loop_bodies, "} while (a == 1)"); |
| 7260 | |
| 7261 | TestDebugBreakInLoop("for (;;) {", loop_bodies, "}"); |
| 7262 | TestDebugBreakInLoop("for (;a == 1;) {", loop_bodies, "}"); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7263 | |
| 7264 | // Get rid of the debug event listener. |
| 7265 | v8::Debug::SetDebugEventListener(NULL); |
| 7266 | CheckDebuggerUnloaded(); |
| 7267 | } |
| 7268 | |
| 7269 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7270 | v8::Local<v8::Script> inline_script; |
| 7271 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7272 | static void DebugBreakInlineListener( |
| 7273 | const v8::Debug::EventDetails& event_details) { |
| 7274 | v8::DebugEvent event = event_details.GetEvent(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7275 | if (event != v8::Break) return; |
| 7276 | |
| 7277 | int expected_frame_count = 4; |
| 7278 | int expected_line_number[] = {1, 4, 7, 12}; |
| 7279 | |
| 7280 | i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script); |
| 7281 | i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast( |
| 7282 | i::JSFunction::cast(*compiled_script)->shared()->script())); |
| 7283 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7284 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7285 | char script[128]; |
| 7286 | i::Vector<char> script_vector(script, sizeof(script)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7287 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7288 | v8::Local<v8::Value> result = CompileRun(script); |
| 7289 | |
| 7290 | int frame_count = result->Int32Value(); |
| 7291 | CHECK_EQ(expected_frame_count, frame_count); |
| 7292 | |
| 7293 | for (int i = 0; i < frame_count; i++) { |
| 7294 | // The 5. element in the returned array of GetFrameDetails contains the |
| 7295 | // source position of that frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7296 | SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[5]", break_id, i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7297 | v8::Local<v8::Value> result = CompileRun(script); |
| 7298 | CHECK_EQ(expected_line_number[i], |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7299 | i::Script::GetLineNumber(source_script, result->Int32Value())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7300 | } |
| 7301 | v8::Debug::SetDebugEventListener(NULL); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7302 | v8::V8::TerminateExecution(CcTest::isolate()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | |
| 7306 | TEST(DebugBreakInline) { |
| 7307 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7308 | DebugLocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7309 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7310 | const char* source = |
| 7311 | "function debug(b) { \n" |
| 7312 | " if (b) debugger; \n" |
| 7313 | "} \n" |
| 7314 | "function f(b) { \n" |
| 7315 | " debug(b) \n" |
| 7316 | "}; \n" |
| 7317 | "function g(b) { \n" |
| 7318 | " f(b); \n" |
| 7319 | "}; \n" |
| 7320 | "g(false); \n" |
| 7321 | "g(false); \n" |
| 7322 | "%OptimizeFunctionOnNextCall(g); \n" |
| 7323 | "g(true);"; |
| 7324 | v8::Debug::SetDebugEventListener(DebugBreakInlineListener); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7325 | inline_script = |
| 7326 | v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), source)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7327 | inline_script->Run(); |
| 7328 | } |
| 7329 | |
| 7330 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7331 | static void DebugEventStepNext( |
| 7332 | const v8::Debug::EventDetails& event_details) { |
| 7333 | v8::DebugEvent event = event_details.GetEvent(); |
| 7334 | if (event == v8::Break) { |
| 7335 | PrepareStep(StepNext); |
| 7336 | } |
| 7337 | } |
| 7338 | |
| 7339 | |
| 7340 | static void RunScriptInANewCFrame(const char* source) { |
| 7341 | v8::TryCatch try_catch; |
| 7342 | CompileRun(source); |
| 7343 | CHECK(try_catch.HasCaught()); |
| 7344 | } |
| 7345 | |
| 7346 | |
| 7347 | TEST(Regress131642) { |
| 7348 | // Bug description: |
| 7349 | // When doing StepNext through the first script, the debugger is not reset |
| 7350 | // after exiting through exception. A flawed implementation enabling the |
| 7351 | // debugger to step into Array.prototype.forEach breaks inside the callback |
| 7352 | // for forEach in the second script under the assumption that we are in a |
| 7353 | // recursive call. In an attempt to step out, we crawl the stack using the |
| 7354 | // recorded frame pointer from the first script and fail when not finding it |
| 7355 | // on the stack. |
| 7356 | DebugLocalContext env; |
| 7357 | v8::HandleScope scope(env->GetIsolate()); |
| 7358 | v8::Debug::SetDebugEventListener(DebugEventStepNext); |
| 7359 | |
| 7360 | // We step through the first script. It exits through an exception. We run |
| 7361 | // this inside a new frame to record a different FP than the second script |
| 7362 | // would expect. |
| 7363 | const char* script_1 = "debugger; throw new Error();"; |
| 7364 | RunScriptInANewCFrame(script_1); |
| 7365 | |
| 7366 | // The second script uses forEach. |
| 7367 | const char* script_2 = "[0].forEach(function() { });"; |
| 7368 | CompileRun(script_2); |
| 7369 | |
| 7370 | v8::Debug::SetDebugEventListener(NULL); |
| 7371 | } |
| 7372 | |
| 7373 | |
| 7374 | // Import from test-heap.cc |
| 7375 | int CountNativeContexts(); |
| 7376 | |
| 7377 | |
| 7378 | static void NopListener(const v8::Debug::EventDetails& event_details) { |
| 7379 | } |
| 7380 | |
| 7381 | |
| 7382 | TEST(DebuggerCreatesContextIffActive) { |
| 7383 | DebugLocalContext env; |
| 7384 | v8::HandleScope scope(env->GetIsolate()); |
| 7385 | CHECK_EQ(1, CountNativeContexts()); |
| 7386 | |
| 7387 | v8::Debug::SetDebugEventListener(NULL); |
| 7388 | CompileRun("debugger;"); |
| 7389 | CHECK_EQ(1, CountNativeContexts()); |
| 7390 | |
| 7391 | v8::Debug::SetDebugEventListener(NopListener); |
| 7392 | CompileRun("debugger;"); |
| 7393 | CHECK_EQ(2, CountNativeContexts()); |
| 7394 | |
| 7395 | v8::Debug::SetDebugEventListener(NULL); |
| 7396 | } |
| 7397 | |
| 7398 | |
| 7399 | TEST(LiveEditEnabled) { |
| 7400 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7401 | LocalContext env; |
| 7402 | v8::HandleScope scope(env->GetIsolate()); |
| 7403 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), true); |
| 7404 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7405 | } |
| 7406 | |
| 7407 | |
| 7408 | TEST(LiveEditDisabled) { |
| 7409 | v8::internal::FLAG_allow_natives_syntax = true; |
| 7410 | LocalContext env; |
| 7411 | v8::HandleScope scope(env->GetIsolate()); |
| 7412 | v8::Debug::SetLiveEditEnabled(env->GetIsolate(), false); |
| 7413 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7414 | } |
| 7415 | |
| 7416 | |
| 7417 | TEST(PrecompiledFunction) { |
| 7418 | // Regression test for crbug.com/346207. If we have preparse data, parsing the |
| 7419 | // function in the presence of the debugger (and breakpoints) should still |
| 7420 | // succeed. The bug was that preparsing was done lazily and parsing was done |
| 7421 | // eagerly, so, the symbol streams didn't match. |
| 7422 | DebugLocalContext env; |
| 7423 | v8::HandleScope scope(env->GetIsolate()); |
| 7424 | env.ExposeDebug(); |
| 7425 | v8::Debug::SetDebugEventListener(DebugBreakInlineListener); |
| 7426 | |
| 7427 | v8::Local<v8::Function> break_here = |
| 7428 | CompileFunction(&env, "function break_here(){}", "break_here"); |
| 7429 | SetBreakPoint(break_here, 0); |
| 7430 | |
| 7431 | const char* source = |
| 7432 | "var a = b = c = 1; \n" |
| 7433 | "function this_is_lazy() { \n" |
| 7434 | // This symbol won't appear in the preparse data. |
| 7435 | " var a; \n" |
| 7436 | "} \n" |
| 7437 | "function bar() { \n" |
| 7438 | " return \"bar\"; \n" |
| 7439 | "}; \n" |
| 7440 | "a = b = c = 2; \n" |
| 7441 | "bar(); \n"; |
| 7442 | v8::Local<v8::Value> result = ParserCacheCompileRun(source); |
| 7443 | CHECK(result->IsString()); |
| 7444 | v8::String::Utf8Value utf8(result); |
| 7445 | CHECK_EQ("bar", *utf8); |
| 7446 | |
| 7447 | v8::Debug::SetDebugEventListener(NULL); |
| 7448 | CheckDebuggerUnloaded(); |
| 7449 | } |
| 7450 | |
| 7451 | |
| 7452 | static void DebugBreakStackTraceListener( |
| 7453 | const v8::Debug::EventDetails& event_details) { |
| 7454 | v8::StackTrace::CurrentStackTrace(CcTest::isolate(), 10); |
| 7455 | } |
| 7456 | |
| 7457 | |
| 7458 | static void AddDebugBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7459 | v8::Debug::DebugBreak(args.GetIsolate()); |
| 7460 | } |
| 7461 | |
| 7462 | |
| 7463 | TEST(DebugBreakStackTrace) { |
| 7464 | DebugLocalContext env; |
| 7465 | v8::HandleScope scope(env->GetIsolate()); |
| 7466 | v8::Debug::SetDebugEventListener(DebugBreakStackTraceListener); |
| 7467 | v8::Handle<v8::FunctionTemplate> add_debug_break_template = |
| 7468 | v8::FunctionTemplate::New(env->GetIsolate(), AddDebugBreak); |
| 7469 | v8::Handle<v8::Function> add_debug_break = |
| 7470 | add_debug_break_template->GetFunction(); |
| 7471 | env->Global()->Set(v8_str("add_debug_break"), add_debug_break); |
| 7472 | |
| 7473 | CompileRun("(function loop() {" |
| 7474 | " for (var j = 0; j < 1000; j++) {" |
| 7475 | " for (var i = 0; i < 1000; i++) {" |
| 7476 | " if (i == 999) add_debug_break();" |
| 7477 | " }" |
| 7478 | " }" |
| 7479 | "})()"); |
| 7480 | } |
| 7481 | |
| 7482 | |
| 7483 | v8::base::Semaphore terminate_requested_semaphore(0); |
| 7484 | v8::base::Semaphore terminate_fired_semaphore(0); |
| 7485 | bool terminate_already_fired = false; |
| 7486 | |
| 7487 | |
| 7488 | static void DebugBreakTriggerTerminate( |
| 7489 | const v8::Debug::EventDetails& event_details) { |
| 7490 | if (event_details.GetEvent() != v8::Break || terminate_already_fired) return; |
| 7491 | terminate_requested_semaphore.Signal(); |
| 7492 | // Wait for at most 2 seconds for the terminate request. |
| 7493 | CHECK(terminate_fired_semaphore.WaitFor(v8::base::TimeDelta::FromSeconds(2))); |
| 7494 | terminate_already_fired = true; |
| 7495 | } |
| 7496 | |
| 7497 | |
| 7498 | class TerminationThread : public v8::base::Thread { |
| 7499 | public: |
| 7500 | explicit TerminationThread(v8::Isolate* isolate) |
| 7501 | : Thread(Options("terminator")), isolate_(isolate) {} |
| 7502 | |
| 7503 | virtual void Run() { |
| 7504 | terminate_requested_semaphore.Wait(); |
| 7505 | v8::V8::TerminateExecution(isolate_); |
| 7506 | terminate_fired_semaphore.Signal(); |
| 7507 | } |
| 7508 | |
| 7509 | private: |
| 7510 | v8::Isolate* isolate_; |
| 7511 | }; |
| 7512 | |
| 7513 | |
| 7514 | TEST(DebugBreakOffThreadTerminate) { |
| 7515 | DebugLocalContext env; |
| 7516 | v8::Isolate* isolate = env->GetIsolate(); |
| 7517 | v8::HandleScope scope(isolate); |
| 7518 | v8::Debug::SetDebugEventListener(DebugBreakTriggerTerminate); |
| 7519 | TerminationThread terminator(isolate); |
| 7520 | terminator.Start(); |
| 7521 | v8::TryCatch try_catch; |
| 7522 | v8::Debug::DebugBreak(isolate); |
| 7523 | CompileRun("while (true);"); |
| 7524 | CHECK(try_catch.HasTerminated()); |
| 7525 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7526 | |
| 7527 | |
| 7528 | static void DebugEventExpectNoException( |
| 7529 | const v8::Debug::EventDetails& event_details) { |
| 7530 | v8::DebugEvent event = event_details.GetEvent(); |
| 7531 | CHECK_NE(v8::Exception, event); |
| 7532 | } |
| 7533 | |
| 7534 | |
| 7535 | static void TryCatchWrappedThrowCallback( |
| 7536 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7537 | v8::TryCatch try_catch; |
| 7538 | CompileRun("throw 'rejection';"); |
| 7539 | CHECK(try_catch.HasCaught()); |
| 7540 | } |
| 7541 | |
| 7542 | |
| 7543 | TEST(DebugPromiseInterceptedByTryCatch) { |
| 7544 | DebugLocalContext env; |
| 7545 | v8::Isolate* isolate = env->GetIsolate(); |
| 7546 | v8::HandleScope scope(isolate); |
| 7547 | v8::Debug::SetDebugEventListener(&DebugEventExpectNoException); |
| 7548 | ChangeBreakOnException(false, true); |
| 7549 | |
| 7550 | v8::Handle<v8::FunctionTemplate> fun = |
| 7551 | v8::FunctionTemplate::New(isolate, TryCatchWrappedThrowCallback); |
| 7552 | env->Global()->Set(v8_str("fun"), fun->GetFunction()); |
| 7553 | |
| 7554 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7555 | CompileRun( |
| 7556 | "var r;" |
| 7557 | "p.chain(function() { r = 'resolved'; }," |
| 7558 | " function() { r = 'rejected'; });"); |
| 7559 | CHECK(CompileRun("r")->Equals(v8_str("resolved"))); |
| 7560 | } |
| 7561 | |
| 7562 | |
| 7563 | static int exception_event_counter = 0; |
| 7564 | |
| 7565 | |
| 7566 | static void DebugEventCountException( |
| 7567 | const v8::Debug::EventDetails& event_details) { |
| 7568 | v8::DebugEvent event = event_details.GetEvent(); |
| 7569 | if (event == v8::Exception) exception_event_counter++; |
| 7570 | } |
| 7571 | |
| 7572 | |
| 7573 | static void ThrowCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 7574 | CompileRun("throw 'rejection';"); |
| 7575 | } |
| 7576 | |
| 7577 | |
| 7578 | TEST(DebugPromiseRejectedByCallback) { |
| 7579 | DebugLocalContext env; |
| 7580 | v8::Isolate* isolate = env->GetIsolate(); |
| 7581 | v8::HandleScope scope(isolate); |
| 7582 | v8::Debug::SetDebugEventListener(&DebugEventCountException); |
| 7583 | ChangeBreakOnException(false, true); |
| 7584 | exception_event_counter = 0; |
| 7585 | |
| 7586 | v8::Handle<v8::FunctionTemplate> fun = |
| 7587 | v8::FunctionTemplate::New(isolate, ThrowCallback); |
| 7588 | env->Global()->Set(v8_str("fun"), fun->GetFunction()); |
| 7589 | |
| 7590 | CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); |
| 7591 | CompileRun( |
| 7592 | "var r;" |
| 7593 | "p.chain(function() { r = 'resolved'; }," |
| 7594 | " function(e) { r = 'rejected' + e; });"); |
| 7595 | CHECK(CompileRun("r")->Equals(v8_str("rejectedrejection"))); |
| 7596 | CHECK_EQ(1, exception_event_counter); |
| 7597 | } |
| 7598 | |
| 7599 | |
| 7600 | TEST(DebugBreakOnExceptionInObserveCallback) { |
| 7601 | DebugLocalContext env; |
| 7602 | v8::Isolate* isolate = env->GetIsolate(); |
| 7603 | v8::HandleScope scope(isolate); |
| 7604 | v8::Debug::SetDebugEventListener(&DebugEventCountException); |
| 7605 | // Break on uncaught exception |
| 7606 | ChangeBreakOnException(false, true); |
| 7607 | exception_event_counter = 0; |
| 7608 | |
| 7609 | v8::Handle<v8::FunctionTemplate> fun = |
| 7610 | v8::FunctionTemplate::New(isolate, ThrowCallback); |
| 7611 | env->Global()->Set(v8_str("fun"), fun->GetFunction()); |
| 7612 | |
| 7613 | CompileRun( |
| 7614 | "var obj = {};" |
| 7615 | "var callbackRan = false;" |
| 7616 | "Object.observe(obj, function() {" |
| 7617 | " callbackRan = true;" |
| 7618 | " throw Error('foo');" |
| 7619 | "});" |
| 7620 | "obj.prop = 1"); |
| 7621 | CHECK(CompileRun("callbackRan")->BooleanValue()); |
| 7622 | CHECK_EQ(1, exception_event_counter); |
| 7623 | } |
| 7624 | |
| 7625 | |
| 7626 | static void DebugHarmonyScopingListener( |
| 7627 | const v8::Debug::EventDetails& event_details) { |
| 7628 | v8::DebugEvent event = event_details.GetEvent(); |
| 7629 | if (event != v8::Break) return; |
| 7630 | |
| 7631 | int break_id = CcTest::i_isolate()->debug()->break_id(); |
| 7632 | |
| 7633 | char script[128]; |
| 7634 | i::Vector<char> script_vector(script, sizeof(script)); |
| 7635 | SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
| 7636 | ExpectInt32(script, 1); |
| 7637 | |
| 7638 | SNPrintF(script_vector, "var frame = new FrameMirror(%d, 0);", break_id); |
| 7639 | CompileRun(script); |
| 7640 | ExpectInt32("frame.evaluate('x').value_", 1); |
| 7641 | ExpectInt32("frame.evaluate('y').value_", 2); |
| 7642 | |
| 7643 | CompileRun("var allScopes = frame.allScopes()"); |
| 7644 | ExpectInt32("allScopes.length", 2); |
| 7645 | |
| 7646 | ExpectBoolean("allScopes[0].scopeType() === ScopeType.Script", true); |
| 7647 | |
| 7648 | ExpectInt32("allScopes[0].scopeObject().value_.x", 1); |
| 7649 | |
| 7650 | ExpectInt32("allScopes[0].scopeObject().value_.y", 2); |
| 7651 | |
| 7652 | CompileRun("allScopes[0].setVariableValue('x', 5);"); |
| 7653 | CompileRun("allScopes[0].setVariableValue('y', 6);"); |
| 7654 | ExpectInt32("frame.evaluate('x + y').value_", 11); |
| 7655 | } |
| 7656 | |
| 7657 | |
| 7658 | TEST(DebugBreakInLexicalScopes) { |
| 7659 | i::FLAG_harmony_scoping = true; |
| 7660 | i::FLAG_allow_natives_syntax = true; |
| 7661 | |
| 7662 | DebugLocalContext env; |
| 7663 | v8::Isolate* isolate = env->GetIsolate(); |
| 7664 | v8::HandleScope scope(isolate); |
| 7665 | v8::Debug::SetDebugEventListener(DebugHarmonyScopingListener); |
| 7666 | |
| 7667 | CompileRun( |
| 7668 | "'use strict'; \n" |
| 7669 | "let x = 1; \n"); |
| 7670 | ExpectInt32( |
| 7671 | "'use strict'; \n" |
| 7672 | "let y = 2; \n" |
| 7673 | "debugger; \n" |
| 7674 | "x * y", |
| 7675 | 30); |
| 7676 | ExpectInt32( |
| 7677 | "x = 1; y = 2; \n" |
| 7678 | "debugger;" |
| 7679 | "x * y", |
| 7680 | 30); |
| 7681 | } |