yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +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 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 28 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 29 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | |
| 32 | #include "v8.h" |
| 33 | |
| 34 | #include "api.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 35 | #include "cctest.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 36 | #include "compilation-cache.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 37 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 38 | #include "deoptimizer.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 39 | #include "platform.h" |
| 40 | #include "stub-cache.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 41 | #include "utils.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 42 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 43 | |
| 44 | using ::v8::internal::EmbeddedVector; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 45 | using ::v8::internal::Object; |
| 46 | using ::v8::internal::OS; |
| 47 | using ::v8::internal::Handle; |
| 48 | using ::v8::internal::Heap; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 49 | using ::v8::internal::JSGlobalProxy; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 50 | using ::v8::internal::Code; |
| 51 | using ::v8::internal::Debug; |
| 52 | using ::v8::internal::Debugger; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 53 | using ::v8::internal::CommandMessage; |
| 54 | using ::v8::internal::CommandMessageQueue; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 55 | using ::v8::internal::StepAction; |
| 56 | using ::v8::internal::StepIn; // From StepAction enum |
| 57 | using ::v8::internal::StepNext; // From StepAction enum |
| 58 | using ::v8::internal::StepOut; // From StepAction enum |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 59 | using ::v8::internal::Vector; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 60 | using ::v8::internal::StrLength; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 61 | |
| 62 | // Size of temp buffer for formatting small strings. |
| 63 | #define SMALL_STRING_BUFFER_SIZE 80 |
| 64 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 65 | // --- A d d i t i o n a l C h e c k H e l p e r s |
| 66 | |
| 67 | |
| 68 | // Helper function used by the CHECK_EQ function when given Address |
| 69 | // arguments. Should not be called directly. |
| 70 | static inline void CheckEqualsHelper(const char* file, int line, |
| 71 | const char* expected_source, |
| 72 | ::v8::internal::Address expected, |
| 73 | const char* value_source, |
| 74 | ::v8::internal::Address value) { |
| 75 | if (expected != value) { |
| 76 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " |
| 77 | "Expected: %i\n# Found: %i", |
| 78 | expected_source, value_source, expected, value); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | // Helper function used by the CHECK_NE function when given Address |
| 84 | // arguments. Should not be called directly. |
| 85 | static inline void CheckNonEqualsHelper(const char* file, int line, |
| 86 | const char* unexpected_source, |
| 87 | ::v8::internal::Address unexpected, |
| 88 | const char* value_source, |
| 89 | ::v8::internal::Address value) { |
| 90 | if (unexpected == value) { |
| 91 | V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i", |
| 92 | unexpected_source, value_source, value); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // Helper function used by the CHECK function when given code |
| 98 | // arguments. Should not be called directly. |
| 99 | static inline void CheckEqualsHelper(const char* file, int line, |
| 100 | const char* expected_source, |
| 101 | const Code* expected, |
| 102 | const char* value_source, |
| 103 | const Code* value) { |
| 104 | if (expected != value) { |
| 105 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " |
| 106 | "Expected: %p\n# Found: %p", |
| 107 | expected_source, value_source, expected, value); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static inline void CheckNonEqualsHelper(const char* file, int line, |
| 113 | const char* expected_source, |
| 114 | const Code* expected, |
| 115 | const char* value_source, |
| 116 | const Code* value) { |
| 117 | if (expected == value) { |
| 118 | V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", |
| 119 | expected_source, value_source, value); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | // --- H e l p e r C l a s s e s |
| 125 | |
| 126 | |
| 127 | // Helper class for creating a V8 enviromnent for running tests |
| 128 | class DebugLocalContext { |
| 129 | public: |
| 130 | inline DebugLocalContext( |
| 131 | v8::ExtensionConfiguration* extensions = 0, |
| 132 | v8::Handle<v8::ObjectTemplate> global_template = |
| 133 | v8::Handle<v8::ObjectTemplate>(), |
| 134 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) |
| 135 | : context_(v8::Context::New(extensions, global_template, global_object)) { |
| 136 | context_->Enter(); |
| 137 | } |
| 138 | inline ~DebugLocalContext() { |
| 139 | context_->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 140 | context_.Dispose(context_->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 141 | } |
| 142 | inline v8::Context* operator->() { return *context_; } |
| 143 | inline v8::Context* operator*() { return *context_; } |
| 144 | inline bool IsReady() { return !context_.IsEmpty(); } |
| 145 | void ExposeDebug() { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 146 | v8::internal::Isolate* isolate = |
| 147 | reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 148 | v8::internal::Debug* debug = isolate->debug(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 149 | // Expose the debug context global object in the global object for testing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 150 | debug->Load(); |
| 151 | debug->debug_context()->set_security_token( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 152 | v8::Utils::OpenHandle(*context_)->security_token()); |
| 153 | |
| 154 | Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 155 | v8::Utils::OpenHandle(*context_->Global()))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 156 | Handle<v8::internal::String> debug_string = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 157 | FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("debug")); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 158 | SetProperty(isolate, global, debug_string, |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 159 | Handle<Object>(debug->debug_context()->global_proxy(), isolate), |
| 160 | DONT_ENUM, |
| 161 | ::v8::internal::kNonStrictMode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 162 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 163 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 164 | private: |
| 165 | v8::Persistent<v8::Context> context_; |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | // --- H e l p e r F u n c t i o n s |
| 170 | |
| 171 | |
| 172 | // Compile and run the supplied source and return the fequested function. |
| 173 | static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, |
| 174 | const char* source, |
| 175 | const char* function_name) { |
| 176 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 177 | return v8::Local<v8::Function>::Cast( |
| 178 | (*env)->Global()->Get(v8::String::New(function_name))); |
| 179 | } |
| 180 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 181 | |
| 182 | // Compile and run the supplied source and return the requested function. |
| 183 | static v8::Local<v8::Function> CompileFunction(const char* source, |
| 184 | const char* function_name) { |
| 185 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 186 | return v8::Local<v8::Function>::Cast( |
| 187 | v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name))); |
| 188 | } |
| 189 | |
| 190 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 191 | // Is there any debug info for the function? |
| 192 | static bool HasDebugInfo(v8::Handle<v8::Function> fun) { |
| 193 | Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun); |
| 194 | Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); |
| 195 | return Debug::HasDebugInfo(shared); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | // Set a break point in a function and return the associated break point |
| 200 | // number. |
| 201 | static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) { |
| 202 | static int break_point = 0; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 203 | v8::internal::Isolate* isolate = fun->GetIsolate(); |
| 204 | v8::internal::Debug* debug = isolate->debug(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 205 | debug->SetBreakPoint( |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 206 | fun, |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 207 | Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate), |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 208 | &position); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 209 | return break_point; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | // Set a break point in a function and return the associated break point |
| 214 | // number. |
| 215 | static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) { |
| 216 | return SetBreakPoint(v8::Utils::OpenHandle(*fun), position); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | // Set a break point in a function using the Debug object and return the |
| 221 | // associated break point number. |
| 222 | static int SetBreakPointFromJS(const char* function_name, |
| 223 | int line, int position) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 224 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 225 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 226 | "debug.Debug.setBreakPoint(%s,%d,%d)", |
| 227 | function_name, line, position); |
| 228 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 229 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 230 | return v8::Script::Compile(str)->Run()->Int32Value(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 234 | // Set a break point in a script identified by id using the global Debug object. |
| 235 | static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 236 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 237 | if (column >= 0) { |
| 238 | // Column specified set script break point on precise location. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 239 | OS::SNPrintF(buffer, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 240 | "debug.Debug.setScriptBreakPointById(%d,%d,%d)", |
| 241 | script_id, line, column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 242 | } else { |
| 243 | // Column not specified set script break point on line. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 244 | OS::SNPrintF(buffer, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 245 | "debug.Debug.setScriptBreakPointById(%d,%d)", |
| 246 | script_id, line); |
| 247 | } |
| 248 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 249 | { |
| 250 | v8::TryCatch try_catch; |
| 251 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 252 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 253 | CHECK(!try_catch.HasCaught()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | return value->Int32Value(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // Set a break point in a script identified by name using the global Debug |
| 260 | // object. |
| 261 | static int SetScriptBreakPointByNameFromJS(const char* script_name, |
| 262 | int line, int column) { |
| 263 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 264 | if (column >= 0) { |
| 265 | // Column specified set script break point on precise location. |
| 266 | OS::SNPrintF(buffer, |
| 267 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", |
| 268 | script_name, line, column); |
| 269 | } else { |
| 270 | // Column not specified set script break point on line. |
| 271 | OS::SNPrintF(buffer, |
| 272 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", |
| 273 | script_name, line); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 274 | } |
| 275 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 276 | { |
| 277 | v8::TryCatch try_catch; |
| 278 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 279 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 280 | CHECK(!try_catch.HasCaught()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 281 | return value->Int32Value(); |
| 282 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | // Clear a break point. |
| 287 | static void ClearBreakPoint(int break_point) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 288 | v8::internal::Isolate* isolate = v8::internal::Isolate::Current(); |
| 289 | v8::internal::Debug* debug = isolate->debug(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 290 | debug->ClearBreakPoint( |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 291 | Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
| 295 | // Clear a break point using the global Debug object. |
| 296 | static void ClearBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 297 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 298 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 299 | "debug.Debug.clearBreakPoint(%d)", |
| 300 | break_point_number); |
| 301 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 302 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | static void EnableScriptBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 307 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 308 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 309 | "debug.Debug.enableScriptBreakPoint(%d)", |
| 310 | break_point_number); |
| 311 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 312 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | static void DisableScriptBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 317 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 318 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 319 | "debug.Debug.disableScriptBreakPoint(%d)", |
| 320 | break_point_number); |
| 321 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 322 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
| 326 | static void ChangeScriptBreakPointConditionFromJS(int break_point_number, |
| 327 | const char* condition) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 328 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 329 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 330 | "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")", |
| 331 | break_point_number, condition); |
| 332 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 333 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | static void ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number, |
| 338 | int ignoreCount) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 339 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 340 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 341 | "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)", |
| 342 | break_point_number, ignoreCount); |
| 343 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 344 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | // Change break on exception. |
| 349 | static void ChangeBreakOnException(bool caught, bool uncaught) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
| 351 | debug->ChangeBreakOnException(v8::internal::BreakException, caught); |
| 352 | debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | |
| 356 | // Change break on exception using the global Debug object. |
| 357 | static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) { |
| 358 | if (caught) { |
| 359 | v8::Script::Compile( |
| 360 | v8::String::New("debug.Debug.setBreakOnException()"))->Run(); |
| 361 | } else { |
| 362 | v8::Script::Compile( |
| 363 | v8::String::New("debug.Debug.clearBreakOnException()"))->Run(); |
| 364 | } |
| 365 | if (uncaught) { |
| 366 | v8::Script::Compile( |
| 367 | v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run(); |
| 368 | } else { |
| 369 | v8::Script::Compile( |
| 370 | v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | |
| 375 | // Prepare to step to next break location. |
| 376 | static void PrepareStep(StepAction step_action) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 377 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
| 378 | debug->PrepareStep(step_action, 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
| 382 | // This function is in namespace v8::internal to be friend with class |
| 383 | // v8::internal::Debug. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 384 | namespace v8 { |
| 385 | namespace internal { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 386 | |
| 387 | // Collect the currently debugged functions. |
| 388 | Handle<FixedArray> GetDebuggedFunctions() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 389 | Debug* debug = Isolate::Current()->debug(); |
| 390 | |
| 391 | v8::internal::DebugInfoListNode* node = debug->debug_info_list_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 392 | |
| 393 | // Find the number of debugged functions. |
| 394 | int count = 0; |
| 395 | while (node) { |
| 396 | count++; |
| 397 | node = node->next(); |
| 398 | } |
| 399 | |
| 400 | // Allocate array for the debugged functions |
| 401 | Handle<FixedArray> debugged_functions = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 402 | FACTORY->NewFixedArray(count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 403 | |
| 404 | // Run through the debug info objects and collect all functions. |
| 405 | count = 0; |
| 406 | while (node) { |
| 407 | debugged_functions->set(count++, *node->debug_info()); |
| 408 | node = node->next(); |
| 409 | } |
| 410 | |
| 411 | return debugged_functions; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | static Handle<Code> ComputeCallDebugBreak(int argc) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 416 | return Isolate::Current()->stub_cache()->ComputeCallDebugBreak(argc, |
| 417 | Code::CALL_IC); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 420 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 421 | // Check that the debugger has been fully unloaded. |
| 422 | void CheckDebuggerUnloaded(bool check_functions) { |
| 423 | // Check that the debugger context is cleared and that there is no debug |
| 424 | // information stored for the debugger. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 425 | CHECK(Isolate::Current()->debug()->debug_context().is_null()); |
| 426 | CHECK_EQ(NULL, Isolate::Current()->debug()->debug_info_list_); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 427 | |
| 428 | // Collect garbage to ensure weak handles are cleared. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 429 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 430 | HEAP->CollectAllGarbage(Heap::kMakeHeapIterableMask); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 431 | |
| 432 | // Iterate the head and check that there are no debugger related objects left. |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 433 | HeapIterator iterator(HEAP); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 434 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 435 | CHECK(!obj->IsDebugInfo()); |
| 436 | CHECK(!obj->IsBreakPointInfo()); |
| 437 | |
| 438 | // If deep check of functions is requested check that no debug break code |
| 439 | // is left in all functions. |
| 440 | if (check_functions) { |
| 441 | if (obj->IsJSFunction()) { |
| 442 | JSFunction* fun = JSFunction::cast(obj); |
| 443 | for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) { |
| 444 | RelocInfo::Mode rmode = it.rinfo()->rmode(); |
| 445 | if (RelocInfo::IsCodeTarget(rmode)) { |
| 446 | CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address())); |
| 447 | } else if (RelocInfo::IsJSReturn(rmode)) { |
| 448 | CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo())); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 457 | void ForceUnloadDebugger() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 458 | Isolate::Current()->debugger()->never_unload_debugger_ = false; |
| 459 | Isolate::Current()->debugger()->UnloadDebugger(); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 463 | } } // namespace v8::internal |
| 464 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 465 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 466 | // Check that the debugger has been fully unloaded. |
| 467 | static void CheckDebuggerUnloaded(bool check_functions = false) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 468 | // Let debugger to unload itself synchronously |
| 469 | v8::Debug::ProcessDebugMessages(); |
| 470 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 471 | v8::internal::CheckDebuggerUnloaded(check_functions); |
| 472 | } |
| 473 | |
| 474 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 475 | // Inherit from BreakLocationIterator to get access to protected parts for |
| 476 | // testing. |
| 477 | class TestBreakLocationIterator: public v8::internal::BreakLocationIterator { |
| 478 | public: |
| 479 | explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info) |
| 480 | : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {} |
| 481 | v8::internal::RelocIterator* it() { return reloc_iterator_; } |
| 482 | v8::internal::RelocIterator* it_original() { |
| 483 | return reloc_iterator_original_; |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | |
| 488 | // Compile a function, set a break point and check that the call at the break |
| 489 | // location in the code is the expected debug_break function. |
| 490 | void CheckDebugBreakFunction(DebugLocalContext* env, |
| 491 | const char* source, const char* name, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 492 | int position, v8::internal::RelocInfo::Mode mode, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 493 | Code* debug_break) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 494 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
| 495 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 496 | // Create function and set the break point. |
| 497 | Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle( |
| 498 | *CompileFunction(env, source, name)); |
| 499 | int bp = SetBreakPoint(fun, position); |
| 500 | |
| 501 | // Check that the debug break function is as expected. |
| 502 | Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); |
| 503 | CHECK(Debug::HasDebugInfo(shared)); |
| 504 | TestBreakLocationIterator it1(Debug::GetDebugInfo(shared)); |
| 505 | it1.FindBreakLocationFromPosition(position); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 506 | v8::internal::RelocInfo::Mode actual_mode = it1.it()->rinfo()->rmode(); |
| 507 | if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { |
| 508 | actual_mode = v8::internal::RelocInfo::CODE_TARGET; |
| 509 | } |
| 510 | CHECK_EQ(mode, actual_mode); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 511 | if (mode != v8::internal::RelocInfo::JS_RETURN) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 512 | CHECK_EQ(debug_break, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 513 | Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 514 | } else { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 515 | CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // Clear the break point and check that the debug break function is no longer |
| 519 | // there |
| 520 | ClearBreakPoint(bp); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 521 | CHECK(!debug->HasDebugInfo(shared)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 522 | CHECK(debug->EnsureDebugInfo(shared, fun)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 523 | TestBreakLocationIterator it2(Debug::GetDebugInfo(shared)); |
| 524 | it2.FindBreakLocationFromPosition(position); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 525 | actual_mode = it2.it()->rinfo()->rmode(); |
| 526 | if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { |
| 527 | actual_mode = v8::internal::RelocInfo::CODE_TARGET; |
| 528 | } |
| 529 | CHECK_EQ(mode, actual_mode); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 530 | if (mode == v8::internal::RelocInfo::JS_RETURN) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 531 | CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | |
| 536 | // --- D e b u g E v e n t H a n d l e r s |
| 537 | // --- |
| 538 | // --- The different tests uses a number of debug event handlers. |
| 539 | // --- |
| 540 | |
| 541 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 542 | // Source for the JavaScript function which picks out the function |
| 543 | // name of a frame. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 544 | const char* frame_function_name_source = |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 545 | "function frame_function_name(exec_state, frame_number) {" |
| 546 | " return exec_state.frame(frame_number).func().name();" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 547 | "}"; |
| 548 | v8::Local<v8::Function> frame_function_name; |
| 549 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 550 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 551 | // Source for the JavaScript function which pick out the name of the |
| 552 | // first argument of a frame. |
| 553 | const char* frame_argument_name_source = |
| 554 | "function frame_argument_name(exec_state, frame_number) {" |
| 555 | " return exec_state.frame(frame_number).argumentName(0);" |
| 556 | "}"; |
| 557 | v8::Local<v8::Function> frame_argument_name; |
| 558 | |
| 559 | |
| 560 | // Source for the JavaScript function which pick out the value of the |
| 561 | // first argument of a frame. |
| 562 | const char* frame_argument_value_source = |
| 563 | "function frame_argument_value(exec_state, frame_number) {" |
| 564 | " return exec_state.frame(frame_number).argumentValue(0).value_;" |
| 565 | "}"; |
| 566 | v8::Local<v8::Function> frame_argument_value; |
| 567 | |
| 568 | |
| 569 | // Source for the JavaScript function which pick out the name of the |
| 570 | // first argument of a frame. |
| 571 | const char* frame_local_name_source = |
| 572 | "function frame_local_name(exec_state, frame_number) {" |
| 573 | " return exec_state.frame(frame_number).localName(0);" |
| 574 | "}"; |
| 575 | v8::Local<v8::Function> frame_local_name; |
| 576 | |
| 577 | |
| 578 | // Source for the JavaScript function which pick out the value of the |
| 579 | // first argument of a frame. |
| 580 | const char* frame_local_value_source = |
| 581 | "function frame_local_value(exec_state, frame_number) {" |
| 582 | " return exec_state.frame(frame_number).localValue(0).value_;" |
| 583 | "}"; |
| 584 | v8::Local<v8::Function> frame_local_value; |
| 585 | |
| 586 | |
| 587 | // Source for the JavaScript function which picks out the source line for the |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 588 | // top frame. |
| 589 | const char* frame_source_line_source = |
| 590 | "function frame_source_line(exec_state) {" |
| 591 | " return exec_state.frame(0).sourceLine();" |
| 592 | "}"; |
| 593 | v8::Local<v8::Function> frame_source_line; |
| 594 | |
| 595 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 596 | // Source for the JavaScript function which picks out the source column for the |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 597 | // top frame. |
| 598 | const char* frame_source_column_source = |
| 599 | "function frame_source_column(exec_state) {" |
| 600 | " return exec_state.frame(0).sourceColumn();" |
| 601 | "}"; |
| 602 | v8::Local<v8::Function> frame_source_column; |
| 603 | |
| 604 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 605 | // Source for the JavaScript function which picks out the script name for the |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 606 | // top frame. |
| 607 | const char* frame_script_name_source = |
| 608 | "function frame_script_name(exec_state) {" |
| 609 | " return exec_state.frame(0).func().script().name();" |
| 610 | "}"; |
| 611 | v8::Local<v8::Function> frame_script_name; |
| 612 | |
| 613 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 614 | // Source for the JavaScript function which picks out the script data for the |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 615 | // top frame. |
| 616 | const char* frame_script_data_source = |
| 617 | "function frame_script_data(exec_state) {" |
| 618 | " return exec_state.frame(0).func().script().data();" |
| 619 | "}"; |
| 620 | v8::Local<v8::Function> frame_script_data; |
| 621 | |
| 622 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 623 | // Source for the JavaScript function which picks out the script data from |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 624 | // AfterCompile event |
| 625 | const char* compiled_script_data_source = |
| 626 | "function compiled_script_data(event_data) {" |
| 627 | " return event_data.script().data();" |
| 628 | "}"; |
| 629 | v8::Local<v8::Function> compiled_script_data; |
| 630 | |
| 631 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 632 | // Source for the JavaScript function which returns the number of frames. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 633 | static const char* frame_count_source = |
| 634 | "function frame_count(exec_state) {" |
| 635 | " return exec_state.frameCount();" |
| 636 | "}"; |
| 637 | v8::Handle<v8::Function> frame_count; |
| 638 | |
| 639 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 640 | // Global variable to store the last function hit - used by some tests. |
| 641 | char last_function_hit[80]; |
| 642 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 643 | // Global variable to store the name and data for last script hit - used by some |
| 644 | // tests. |
| 645 | char last_script_name_hit[80]; |
| 646 | char last_script_data_hit[80]; |
| 647 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 648 | // Global variables to store the last source position - used by some tests. |
| 649 | int last_source_line = -1; |
| 650 | int last_source_column = -1; |
| 651 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 652 | // Debug event handler which counts the break points which have been hit. |
| 653 | int break_point_hit_count = 0; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 654 | int break_point_hit_count_deoptimize = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 655 | static void DebugEventBreakPointHitCount(v8::DebugEvent event, |
| 656 | v8::Handle<v8::Object> exec_state, |
| 657 | v8::Handle<v8::Object> event_data, |
| 658 | v8::Handle<v8::Value> data) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 659 | v8::internal::Isolate* isolate = v8::internal::Isolate::Current(); |
| 660 | Debug* debug = isolate->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 661 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 662 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 663 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 664 | // Count the number of breaks. |
| 665 | if (event == v8::Break) { |
| 666 | break_point_hit_count++; |
| 667 | if (!frame_function_name.IsEmpty()) { |
| 668 | // Get the name of the function. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 669 | const int argc = 2; |
| 670 | v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 671 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 672 | argc, argv); |
| 673 | if (result->IsUndefined()) { |
| 674 | last_function_hit[0] = '\0'; |
| 675 | } else { |
| 676 | CHECK(result->IsString()); |
| 677 | v8::Handle<v8::String> function_name(result->ToString()); |
| 678 | function_name->WriteAscii(last_function_hit); |
| 679 | } |
| 680 | } |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 681 | |
| 682 | if (!frame_source_line.IsEmpty()) { |
| 683 | // Get the source line. |
| 684 | const int argc = 1; |
| 685 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 686 | v8::Handle<v8::Value> result = frame_source_line->Call(exec_state, |
| 687 | argc, argv); |
| 688 | CHECK(result->IsNumber()); |
| 689 | last_source_line = result->Int32Value(); |
| 690 | } |
| 691 | |
| 692 | if (!frame_source_column.IsEmpty()) { |
| 693 | // Get the source column. |
| 694 | const int argc = 1; |
| 695 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 696 | v8::Handle<v8::Value> result = frame_source_column->Call(exec_state, |
| 697 | argc, argv); |
| 698 | CHECK(result->IsNumber()); |
| 699 | last_source_column = result->Int32Value(); |
| 700 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 701 | |
| 702 | if (!frame_script_name.IsEmpty()) { |
| 703 | // Get the script name of the function script. |
| 704 | const int argc = 1; |
| 705 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 706 | v8::Handle<v8::Value> result = frame_script_name->Call(exec_state, |
| 707 | argc, argv); |
| 708 | if (result->IsUndefined()) { |
| 709 | last_script_name_hit[0] = '\0'; |
| 710 | } else { |
| 711 | CHECK(result->IsString()); |
| 712 | v8::Handle<v8::String> script_name(result->ToString()); |
| 713 | script_name->WriteAscii(last_script_name_hit); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | if (!frame_script_data.IsEmpty()) { |
| 718 | // Get the script data of the function script. |
| 719 | const int argc = 1; |
| 720 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 721 | v8::Handle<v8::Value> result = frame_script_data->Call(exec_state, |
| 722 | argc, argv); |
| 723 | if (result->IsUndefined()) { |
| 724 | last_script_data_hit[0] = '\0'; |
| 725 | } else { |
| 726 | result = result->ToString(); |
| 727 | CHECK(result->IsString()); |
| 728 | v8::Handle<v8::String> script_data(result->ToString()); |
| 729 | script_data->WriteAscii(last_script_data_hit); |
| 730 | } |
| 731 | } |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 732 | |
| 733 | // Perform a full deoptimization when the specified number of |
| 734 | // breaks have been hit. |
| 735 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 736 | i::Deoptimizer::DeoptimizeAll(isolate); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 737 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 738 | } else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) { |
| 739 | const int argc = 1; |
| 740 | v8::Handle<v8::Value> argv[argc] = { event_data }; |
| 741 | v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state, |
| 742 | argc, argv); |
| 743 | if (result->IsUndefined()) { |
| 744 | last_script_data_hit[0] = '\0'; |
| 745 | } else { |
| 746 | result = result->ToString(); |
| 747 | CHECK(result->IsString()); |
| 748 | v8::Handle<v8::String> script_data(result->ToString()); |
| 749 | script_data->WriteAscii(last_script_data_hit); |
| 750 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 755 | // Debug event handler which counts a number of events and collects the stack |
| 756 | // height if there is a function compiled for that. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 757 | int exception_hit_count = 0; |
| 758 | int uncaught_exception_hit_count = 0; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 759 | int last_js_stack_height = -1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 760 | |
| 761 | static void DebugEventCounterClear() { |
| 762 | break_point_hit_count = 0; |
| 763 | exception_hit_count = 0; |
| 764 | uncaught_exception_hit_count = 0; |
| 765 | } |
| 766 | |
| 767 | static void DebugEventCounter(v8::DebugEvent event, |
| 768 | v8::Handle<v8::Object> exec_state, |
| 769 | v8::Handle<v8::Object> event_data, |
| 770 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 771 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
| 772 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 773 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 774 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 775 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 776 | // Count the number of breaks. |
| 777 | if (event == v8::Break) { |
| 778 | break_point_hit_count++; |
| 779 | } else if (event == v8::Exception) { |
| 780 | exception_hit_count++; |
| 781 | |
| 782 | // Check whether the exception was uncaught. |
| 783 | v8::Local<v8::String> fun_name = v8::String::New("uncaught"); |
| 784 | v8::Local<v8::Function> fun = |
| 785 | v8::Function::Cast(*event_data->Get(fun_name)); |
| 786 | v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL); |
| 787 | if (result->IsTrue()) { |
| 788 | uncaught_exception_hit_count++; |
| 789 | } |
| 790 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 791 | |
| 792 | // Collect the JavsScript stack height if the function frame_count is |
| 793 | // compiled. |
| 794 | if (!frame_count.IsEmpty()) { |
| 795 | static const int kArgc = 1; |
| 796 | v8::Handle<v8::Value> argv[kArgc] = { exec_state }; |
| 797 | // Using exec_state as receiver is just to have a receiver. |
| 798 | v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv); |
| 799 | last_js_stack_height = result->Int32Value(); |
| 800 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | |
| 804 | // Debug event handler which evaluates a number of expressions when a break |
| 805 | // point is hit. Each evaluated expression is compared with an expected value. |
| 806 | // For this debug event handler to work the following two global varaibles |
| 807 | // must be initialized. |
| 808 | // checks: An array of expressions and expected results |
| 809 | // evaluate_check_function: A JavaScript function (see below) |
| 810 | |
| 811 | // Structure for holding checks to do. |
| 812 | struct EvaluateCheck { |
| 813 | const char* expr; // An expression to evaluate when a break point is hit. |
| 814 | v8::Handle<v8::Value> expected; // The expected result. |
| 815 | }; |
| 816 | // Array of checks to do. |
| 817 | struct EvaluateCheck* checks = NULL; |
| 818 | // Source for The JavaScript function which can do the evaluation when a break |
| 819 | // point is hit. |
| 820 | const char* evaluate_check_source = |
| 821 | "function evaluate_check(exec_state, expr, expected) {" |
| 822 | " return exec_state.frame(0).evaluate(expr).value() === expected;" |
| 823 | "}"; |
| 824 | v8::Local<v8::Function> evaluate_check_function; |
| 825 | |
| 826 | // The actual debug event described by the longer comment above. |
| 827 | static void DebugEventEvaluate(v8::DebugEvent event, |
| 828 | v8::Handle<v8::Object> exec_state, |
| 829 | v8::Handle<v8::Object> event_data, |
| 830 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 831 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 832 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 833 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 834 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 835 | if (event == v8::Break) { |
| 836 | for (int i = 0; checks[i].expr != NULL; i++) { |
| 837 | const int argc = 3; |
| 838 | v8::Handle<v8::Value> argv[argc] = { exec_state, |
| 839 | v8::String::New(checks[i].expr), |
| 840 | checks[i].expected }; |
| 841 | v8::Handle<v8::Value> result = |
| 842 | evaluate_check_function->Call(exec_state, argc, argv); |
| 843 | if (!result->IsTrue()) { |
| 844 | v8::String::AsciiValue ascii(checks[i].expected->ToString()); |
| 845 | V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
| 852 | // This debug event listener removes a breakpoint in a function |
| 853 | int debug_event_remove_break_point = 0; |
| 854 | static void DebugEventRemoveBreakPoint(v8::DebugEvent event, |
| 855 | v8::Handle<v8::Object> exec_state, |
| 856 | v8::Handle<v8::Object> event_data, |
| 857 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 858 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 859 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 860 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 861 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 862 | if (event == v8::Break) { |
| 863 | break_point_hit_count++; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 864 | CHECK(data->IsFunction()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 865 | ClearBreakPoint(debug_event_remove_break_point); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | |
| 870 | // Debug event handler which counts break points hit and performs a step |
| 871 | // afterwards. |
| 872 | StepAction step_action = StepIn; // Step action to perform when stepping. |
| 873 | static void DebugEventStep(v8::DebugEvent event, |
| 874 | v8::Handle<v8::Object> exec_state, |
| 875 | v8::Handle<v8::Object> event_data, |
| 876 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 877 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 878 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 879 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 880 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 881 | if (event == v8::Break) { |
| 882 | break_point_hit_count++; |
| 883 | PrepareStep(step_action); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | // Debug event handler which counts break points hit and performs a step |
| 889 | // afterwards. For each call the expected function is checked. |
| 890 | // For this debug event handler to work the following two global varaibles |
| 891 | // must be initialized. |
| 892 | // expected_step_sequence: An array of the expected function call sequence. |
| 893 | // frame_function_name: A JavaScript function (see below). |
| 894 | |
| 895 | // String containing the expected function call sequence. Note: this only works |
| 896 | // if functions have name length of one. |
| 897 | const char* expected_step_sequence = NULL; |
| 898 | |
| 899 | // The actual debug event described by the longer comment above. |
| 900 | static void DebugEventStepSequence(v8::DebugEvent event, |
| 901 | v8::Handle<v8::Object> exec_state, |
| 902 | v8::Handle<v8::Object> event_data, |
| 903 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 904 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 905 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 906 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 907 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 908 | if (event == v8::Break || event == v8::Exception) { |
| 909 | // Check that the current function is the expected. |
| 910 | CHECK(break_point_hit_count < |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 911 | StrLength(expected_step_sequence)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 912 | const int argc = 2; |
| 913 | v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 914 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 915 | argc, argv); |
| 916 | CHECK(result->IsString()); |
| 917 | v8::String::AsciiValue function_name(result->ToString()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 918 | CHECK_EQ(1, StrLength(*function_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 919 | CHECK_EQ((*function_name)[0], |
| 920 | expected_step_sequence[break_point_hit_count]); |
| 921 | |
| 922 | // Perform step. |
| 923 | break_point_hit_count++; |
| 924 | PrepareStep(step_action); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | |
| 929 | // Debug event handler which performs a garbage collection. |
| 930 | static void DebugEventBreakPointCollectGarbage( |
| 931 | v8::DebugEvent event, |
| 932 | v8::Handle<v8::Object> exec_state, |
| 933 | v8::Handle<v8::Object> event_data, |
| 934 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 935 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 936 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 937 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 938 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 939 | // Perform a garbage collection when break point is hit and continue. Based |
| 940 | // on the number of break points hit either scavenge or mark compact |
| 941 | // collector is used. |
| 942 | if (event == v8::Break) { |
| 943 | break_point_hit_count++; |
| 944 | if (break_point_hit_count % 2 == 0) { |
| 945 | // Scavenge. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 946 | HEAP->CollectGarbage(v8::internal::NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 947 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 948 | // Mark sweep compact. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 949 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | |
| 955 | // Debug event handler which re-issues a debug break and calls the garbage |
| 956 | // collector to have the heap verified. |
| 957 | static void DebugEventBreak(v8::DebugEvent event, |
| 958 | v8::Handle<v8::Object> exec_state, |
| 959 | v8::Handle<v8::Object> event_data, |
| 960 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 961 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 962 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 963 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 964 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 965 | if (event == v8::Break) { |
| 966 | // Count the number of breaks. |
| 967 | break_point_hit_count++; |
| 968 | |
| 969 | // Run the garbage collector to enforce heap verification if option |
| 970 | // --verify-heap is set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 971 | HEAP->CollectGarbage(v8::internal::NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 972 | |
| 973 | // Set the break flag again to come back here as soon as possible. |
| 974 | v8::Debug::DebugBreak(); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 979 | // Debug event handler which re-issues a debug break until a limit has been |
| 980 | // reached. |
| 981 | int max_break_point_hit_count = 0; |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 982 | bool terminate_after_max_break_point_hit = false; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 983 | static void DebugEventBreakMax(v8::DebugEvent event, |
| 984 | v8::Handle<v8::Object> exec_state, |
| 985 | v8::Handle<v8::Object> event_data, |
| 986 | v8::Handle<v8::Value> data) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 987 | v8::internal::Isolate* isolate = v8::internal::Isolate::Current(); |
| 988 | v8::internal::Debug* debug = isolate->debug(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 989 | // When hitting a debug event listener there must be a break set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 990 | CHECK_NE(debug->break_id(), 0); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 991 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 992 | if (event == v8::Break) { |
| 993 | if (break_point_hit_count < max_break_point_hit_count) { |
| 994 | // Count the number of breaks. |
| 995 | break_point_hit_count++; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 996 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 997 | // Collect the JavsScript stack height if the function frame_count is |
| 998 | // compiled. |
| 999 | if (!frame_count.IsEmpty()) { |
| 1000 | static const int kArgc = 1; |
| 1001 | v8::Handle<v8::Value> argv[kArgc] = { exec_state }; |
| 1002 | // Using exec_state as receiver is just to have a receiver. |
| 1003 | v8::Handle<v8::Value> result = |
| 1004 | frame_count->Call(exec_state, kArgc, argv); |
| 1005 | last_js_stack_height = result->Int32Value(); |
| 1006 | } |
| 1007 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 1008 | // Set the break flag again to come back here as soon as possible. |
| 1009 | v8::Debug::DebugBreak(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1010 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 1011 | } else if (terminate_after_max_break_point_hit) { |
| 1012 | // Terminate execution after the last break if requested. |
| 1013 | v8::V8::TerminateExecution(); |
| 1014 | } |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Perform a full deoptimization when the specified number of |
| 1017 | // breaks have been hit. |
| 1018 | if (break_point_hit_count == break_point_hit_count_deoptimize) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 1019 | i::Deoptimizer::DeoptimizeAll(isolate); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1020 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1025 | // --- M e s s a g e C a l l b a c k |
| 1026 | |
| 1027 | |
| 1028 | // Message callback which counts the number of messages. |
| 1029 | int message_callback_count = 0; |
| 1030 | |
| 1031 | static void MessageCallbackCountClear() { |
| 1032 | message_callback_count = 0; |
| 1033 | } |
| 1034 | |
| 1035 | static void MessageCallbackCount(v8::Handle<v8::Message> message, |
| 1036 | v8::Handle<v8::Value> data) { |
| 1037 | message_callback_count++; |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | // --- T h e A c t u a l T e s t s |
| 1042 | |
| 1043 | |
| 1044 | // Test that the debug break function is the expected one for different kinds |
| 1045 | // of break locations. |
| 1046 | TEST(DebugStub) { |
| 1047 | using ::v8::internal::Builtins; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1048 | using ::v8::internal::Isolate; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1049 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1050 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1051 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1052 | CheckDebugBreakFunction(&env, |
| 1053 | "function f1(){}", "f1", |
| 1054 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1055 | v8::internal::RelocInfo::JS_RETURN, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1056 | NULL); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1057 | CheckDebugBreakFunction(&env, |
| 1058 | "function f2(){x=1;}", "f2", |
| 1059 | 0, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1060 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1061 | Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1062 | Builtins::kStoreIC_DebugBreak)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1063 | CheckDebugBreakFunction(&env, |
| 1064 | "function f3(){var a=x;}", "f3", |
| 1065 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1066 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1067 | Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1068 | Builtins::kLoadIC_DebugBreak)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1069 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1070 | // TODO(1240753): Make the test architecture independent or split |
| 1071 | // parts of the debugger into architecture dependent files. This |
| 1072 | // part currently disabled as it is not portable between IA32/ARM. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1073 | // Currently on ICs for keyed store/load on ARM. |
| 1074 | #if !defined (__arm__) && !defined(__thumb__) |
| 1075 | CheckDebugBreakFunction( |
| 1076 | &env, |
| 1077 | "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}", |
| 1078 | "f4", |
| 1079 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1080 | v8::internal::RelocInfo::CODE_TARGET, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1081 | Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1082 | Builtins::kKeyedStoreIC_DebugBreak)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1083 | CheckDebugBreakFunction( |
| 1084 | &env, |
| 1085 | "function f5(){var index='propertyName'; var a={}; return a[index];}", |
| 1086 | "f5", |
| 1087 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1088 | v8::internal::RelocInfo::CODE_TARGET, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1089 | Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1090 | Builtins::kKeyedLoadIC_DebugBreak)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1091 | #endif |
| 1092 | |
| 1093 | // Check the debug break code stubs for call ICs with different number of |
| 1094 | // parameters. |
| 1095 | Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0); |
| 1096 | Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1); |
| 1097 | Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4); |
| 1098 | |
| 1099 | CheckDebugBreakFunction(&env, |
| 1100 | "function f4_0(){x();}", "f4_0", |
| 1101 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1102 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1103 | *debug_break_0); |
| 1104 | |
| 1105 | CheckDebugBreakFunction(&env, |
| 1106 | "function f4_1(){x(1);}", "f4_1", |
| 1107 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1108 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1109 | *debug_break_1); |
| 1110 | |
| 1111 | CheckDebugBreakFunction(&env, |
| 1112 | "function f4_4(){x(1,2,3,4);}", "f4_4", |
| 1113 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1114 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1115 | *debug_break_4); |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | // Test that the debug info in the VM is in sync with the functions being |
| 1120 | // debugged. |
| 1121 | TEST(DebugInfo) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1122 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1123 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1124 | // Create a couple of functions for the test. |
| 1125 | v8::Local<v8::Function> foo = |
| 1126 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1127 | v8::Local<v8::Function> bar = |
| 1128 | CompileFunction(&env, "function bar(){}", "bar"); |
| 1129 | // Initially no functions are debugged. |
| 1130 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1131 | CHECK(!HasDebugInfo(foo)); |
| 1132 | CHECK(!HasDebugInfo(bar)); |
| 1133 | // One function (foo) is debugged. |
| 1134 | int bp1 = SetBreakPoint(foo, 0); |
| 1135 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1136 | CHECK(HasDebugInfo(foo)); |
| 1137 | CHECK(!HasDebugInfo(bar)); |
| 1138 | // Two functions are debugged. |
| 1139 | int bp2 = SetBreakPoint(bar, 0); |
| 1140 | CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); |
| 1141 | CHECK(HasDebugInfo(foo)); |
| 1142 | CHECK(HasDebugInfo(bar)); |
| 1143 | // One function (bar) is debugged. |
| 1144 | ClearBreakPoint(bp1); |
| 1145 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1146 | CHECK(!HasDebugInfo(foo)); |
| 1147 | CHECK(HasDebugInfo(bar)); |
| 1148 | // No functions are debugged. |
| 1149 | ClearBreakPoint(bp2); |
| 1150 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1151 | CHECK(!HasDebugInfo(foo)); |
| 1152 | CHECK(!HasDebugInfo(bar)); |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | // Test that a break point can be set at an IC store location. |
| 1157 | TEST(BreakPointICStore) { |
| 1158 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1159 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1160 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1161 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1162 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1163 | v8::Undefined()); |
| 1164 | v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run(); |
| 1165 | v8::Local<v8::Function> foo = |
| 1166 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1167 | |
| 1168 | // Run without breakpoints. |
| 1169 | foo->Call(env->Global(), 0, NULL); |
| 1170 | CHECK_EQ(0, break_point_hit_count); |
| 1171 | |
| 1172 | // Run with breakpoint |
| 1173 | int bp = SetBreakPoint(foo, 0); |
| 1174 | foo->Call(env->Global(), 0, NULL); |
| 1175 | CHECK_EQ(1, break_point_hit_count); |
| 1176 | foo->Call(env->Global(), 0, NULL); |
| 1177 | CHECK_EQ(2, break_point_hit_count); |
| 1178 | |
| 1179 | // Run without breakpoints. |
| 1180 | ClearBreakPoint(bp); |
| 1181 | foo->Call(env->Global(), 0, NULL); |
| 1182 | CHECK_EQ(2, break_point_hit_count); |
| 1183 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1184 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1185 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | // Test that a break point can be set at an IC load location. |
| 1190 | TEST(BreakPointICLoad) { |
| 1191 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1192 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1193 | v8::HandleScope scope(env->GetIsolate()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1194 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1195 | v8::Undefined()); |
| 1196 | v8::Script::Compile(v8::String::New("bar=1"))->Run(); |
| 1197 | v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run(); |
| 1198 | v8::Local<v8::Function> foo = |
| 1199 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1200 | |
| 1201 | // Run without breakpoints. |
| 1202 | foo->Call(env->Global(), 0, NULL); |
| 1203 | CHECK_EQ(0, break_point_hit_count); |
| 1204 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1205 | // Run with breakpoint. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1206 | int bp = SetBreakPoint(foo, 0); |
| 1207 | foo->Call(env->Global(), 0, NULL); |
| 1208 | CHECK_EQ(1, break_point_hit_count); |
| 1209 | foo->Call(env->Global(), 0, NULL); |
| 1210 | CHECK_EQ(2, break_point_hit_count); |
| 1211 | |
| 1212 | // Run without breakpoints. |
| 1213 | ClearBreakPoint(bp); |
| 1214 | foo->Call(env->Global(), 0, NULL); |
| 1215 | CHECK_EQ(2, break_point_hit_count); |
| 1216 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1217 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1218 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | // Test that a break point can be set at an IC call location. |
| 1223 | TEST(BreakPointICCall) { |
| 1224 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1225 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1226 | v8::HandleScope scope(env->GetIsolate()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1227 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1228 | v8::Undefined()); |
| 1229 | v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); |
| 1230 | v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run(); |
| 1231 | v8::Local<v8::Function> foo = |
| 1232 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1233 | |
| 1234 | // Run without breakpoints. |
| 1235 | foo->Call(env->Global(), 0, NULL); |
| 1236 | CHECK_EQ(0, break_point_hit_count); |
| 1237 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1238 | // Run with breakpoint |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1239 | int bp = SetBreakPoint(foo, 0); |
| 1240 | foo->Call(env->Global(), 0, NULL); |
| 1241 | CHECK_EQ(1, break_point_hit_count); |
| 1242 | foo->Call(env->Global(), 0, NULL); |
| 1243 | CHECK_EQ(2, break_point_hit_count); |
| 1244 | |
| 1245 | // Run without breakpoints. |
| 1246 | ClearBreakPoint(bp); |
| 1247 | foo->Call(env->Global(), 0, NULL); |
| 1248 | CHECK_EQ(2, break_point_hit_count); |
| 1249 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1250 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1251 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1255 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1256 | TEST(BreakPointICCallWithGC) { |
| 1257 | break_point_hit_count = 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1258 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1259 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1260 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
| 1261 | v8::Undefined()); |
| 1262 | v8::Script::Compile(v8::String::New("function bar(){return 1;}"))->Run(); |
| 1263 | v8::Script::Compile(v8::String::New("function foo(){return bar();}"))->Run(); |
| 1264 | v8::Local<v8::Function> foo = |
| 1265 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1266 | |
| 1267 | // Run without breakpoints. |
| 1268 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1269 | CHECK_EQ(0, break_point_hit_count); |
| 1270 | |
| 1271 | // Run with breakpoint. |
| 1272 | int bp = SetBreakPoint(foo, 0); |
| 1273 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1274 | CHECK_EQ(1, break_point_hit_count); |
| 1275 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1276 | CHECK_EQ(2, break_point_hit_count); |
| 1277 | |
| 1278 | // Run without breakpoints. |
| 1279 | ClearBreakPoint(bp); |
| 1280 | foo->Call(env->Global(), 0, NULL); |
| 1281 | CHECK_EQ(2, break_point_hit_count); |
| 1282 | |
| 1283 | v8::Debug::SetDebugEventListener(NULL); |
| 1284 | CheckDebuggerUnloaded(); |
| 1285 | } |
| 1286 | |
| 1287 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1288 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1289 | TEST(BreakPointConstructCallWithGC) { |
| 1290 | break_point_hit_count = 0; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1291 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1292 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1293 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
| 1294 | v8::Undefined()); |
| 1295 | v8::Script::Compile(v8::String::New("function bar(){ this.x = 1;}"))->Run(); |
| 1296 | v8::Script::Compile(v8::String::New( |
| 1297 | "function foo(){return new bar(1).x;}"))->Run(); |
| 1298 | v8::Local<v8::Function> foo = |
| 1299 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1300 | |
| 1301 | // Run without breakpoints. |
| 1302 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1303 | CHECK_EQ(0, break_point_hit_count); |
| 1304 | |
| 1305 | // Run with breakpoint. |
| 1306 | int bp = SetBreakPoint(foo, 0); |
| 1307 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1308 | CHECK_EQ(1, break_point_hit_count); |
| 1309 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1310 | CHECK_EQ(2, break_point_hit_count); |
| 1311 | |
| 1312 | // Run without breakpoints. |
| 1313 | ClearBreakPoint(bp); |
| 1314 | foo->Call(env->Global(), 0, NULL); |
| 1315 | CHECK_EQ(2, break_point_hit_count); |
| 1316 | |
| 1317 | v8::Debug::SetDebugEventListener(NULL); |
| 1318 | CheckDebuggerUnloaded(); |
| 1319 | } |
| 1320 | |
| 1321 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1322 | // Test that a break point can be set at a return store location. |
| 1323 | TEST(BreakPointReturn) { |
| 1324 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1325 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1326 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | // Create a functions for checking the source line and column when hitting |
| 1329 | // a break point. |
| 1330 | frame_source_line = CompileFunction(&env, |
| 1331 | frame_source_line_source, |
| 1332 | "frame_source_line"); |
| 1333 | frame_source_column = CompileFunction(&env, |
| 1334 | frame_source_column_source, |
| 1335 | "frame_source_column"); |
| 1336 | |
| 1337 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1338 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1339 | v8::Undefined()); |
| 1340 | v8::Script::Compile(v8::String::New("function foo(){}"))->Run(); |
| 1341 | v8::Local<v8::Function> foo = |
| 1342 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1343 | |
| 1344 | // Run without breakpoints. |
| 1345 | foo->Call(env->Global(), 0, NULL); |
| 1346 | CHECK_EQ(0, break_point_hit_count); |
| 1347 | |
| 1348 | // Run with breakpoint |
| 1349 | int bp = SetBreakPoint(foo, 0); |
| 1350 | foo->Call(env->Global(), 0, NULL); |
| 1351 | CHECK_EQ(1, break_point_hit_count); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1352 | CHECK_EQ(0, last_source_line); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1353 | CHECK_EQ(15, last_source_column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1354 | foo->Call(env->Global(), 0, NULL); |
| 1355 | CHECK_EQ(2, break_point_hit_count); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1356 | CHECK_EQ(0, last_source_line); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1357 | CHECK_EQ(15, last_source_column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Run without breakpoints. |
| 1360 | ClearBreakPoint(bp); |
| 1361 | foo->Call(env->Global(), 0, NULL); |
| 1362 | CHECK_EQ(2, break_point_hit_count); |
| 1363 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1364 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1365 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | static void CallWithBreakPoints(v8::Local<v8::Object> recv, |
| 1370 | v8::Local<v8::Function> f, |
| 1371 | int break_point_count, |
| 1372 | int call_count) { |
| 1373 | break_point_hit_count = 0; |
| 1374 | for (int i = 0; i < call_count; i++) { |
| 1375 | f->Call(recv, 0, NULL); |
| 1376 | CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | // Test GC during break point processing. |
| 1381 | TEST(GCDuringBreakPointProcessing) { |
| 1382 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1383 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1384 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1385 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1386 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1387 | v8::Undefined()); |
| 1388 | v8::Local<v8::Function> foo; |
| 1389 | |
| 1390 | // Test IC store break point with garbage collection. |
| 1391 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1392 | SetBreakPoint(foo, 0); |
| 1393 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1394 | |
| 1395 | // Test IC load break point with garbage collection. |
| 1396 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1397 | SetBreakPoint(foo, 0); |
| 1398 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1399 | |
| 1400 | // Test IC call break point with garbage collection. |
| 1401 | foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); |
| 1402 | SetBreakPoint(foo, 0); |
| 1403 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1404 | |
| 1405 | // Test return break point with garbage collection. |
| 1406 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1407 | SetBreakPoint(foo, 0); |
| 1408 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1409 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1410 | // Test debug break slot break point with garbage collection. |
| 1411 | foo = CompileFunction(&env, "function foo(){var a;}", "foo"); |
| 1412 | SetBreakPoint(foo, 0); |
| 1413 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1414 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1415 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1416 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | // Call the function three times with different garbage collections in between |
| 1421 | // and make sure that the break point survives. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1422 | static void CallAndGC(v8::Local<v8::Object> recv, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1423 | v8::Local<v8::Function> f) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1424 | break_point_hit_count = 0; |
| 1425 | |
| 1426 | for (int i = 0; i < 3; i++) { |
| 1427 | // Call function. |
| 1428 | f->Call(recv, 0, NULL); |
| 1429 | CHECK_EQ(1 + i * 3, break_point_hit_count); |
| 1430 | |
| 1431 | // Scavenge and call function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1432 | HEAP->CollectGarbage(v8::internal::NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1433 | f->Call(recv, 0, NULL); |
| 1434 | CHECK_EQ(2 + i * 3, break_point_hit_count); |
| 1435 | |
| 1436 | // Mark sweep (and perhaps compact) and call function. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1437 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1438 | f->Call(recv, 0, NULL); |
| 1439 | CHECK_EQ(3 + i * 3, break_point_hit_count); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1444 | // Test that a break point can be set at a return store location. |
| 1445 | TEST(BreakPointSurviveGC) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1446 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1447 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1448 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1449 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1450 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1451 | v8::Undefined()); |
| 1452 | v8::Local<v8::Function> foo; |
| 1453 | |
| 1454 | // Test IC store break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1455 | { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1456 | CompileFunction(&env, "function foo(){}", "foo"); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1457 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1458 | SetBreakPoint(foo, 0); |
| 1459 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1460 | CallAndGC(env->Global(), foo); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Test IC load break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1463 | { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1464 | CompileFunction(&env, "function foo(){}", "foo"); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1465 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1466 | SetBreakPoint(foo, 0); |
| 1467 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1468 | CallAndGC(env->Global(), foo); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Test IC call break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1471 | { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1472 | CompileFunction(&env, "function foo(){}", "foo"); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1473 | foo = CompileFunction(&env, |
| 1474 | "function bar(){};function foo(){bar();}", |
| 1475 | "foo"); |
| 1476 | SetBreakPoint(foo, 0); |
| 1477 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1478 | CallAndGC(env->Global(), foo); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Test return break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1481 | { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1482 | CompileFunction(&env, "function foo(){}", "foo"); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1483 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1484 | SetBreakPoint(foo, 0); |
| 1485 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1486 | CallAndGC(env->Global(), foo); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Test non IC break point with garbage collection. |
| 1489 | { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1490 | CompileFunction(&env, "function foo(){}", "foo"); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1491 | foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); |
| 1492 | SetBreakPoint(foo, 0); |
| 1493 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1494 | CallAndGC(env->Global(), foo); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1495 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1496 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1497 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1498 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | |
| 1502 | // Test that break points can be set using the global Debug object. |
| 1503 | TEST(BreakPointThroughJavaScript) { |
| 1504 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1505 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1506 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1507 | env.ExposeDebug(); |
| 1508 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1509 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1510 | v8::Undefined()); |
| 1511 | v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); |
| 1512 | v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run(); |
| 1513 | // 012345678901234567890 |
| 1514 | // 1 2 |
| 1515 | // Break points are set at position 3 and 9 |
| 1516 | v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()")); |
| 1517 | |
| 1518 | // Run without breakpoints. |
| 1519 | foo->Run(); |
| 1520 | CHECK_EQ(0, break_point_hit_count); |
| 1521 | |
| 1522 | // Run with one breakpoint |
| 1523 | int bp1 = SetBreakPointFromJS("foo", 0, 3); |
| 1524 | foo->Run(); |
| 1525 | CHECK_EQ(1, break_point_hit_count); |
| 1526 | foo->Run(); |
| 1527 | CHECK_EQ(2, break_point_hit_count); |
| 1528 | |
| 1529 | // Run with two breakpoints |
| 1530 | int bp2 = SetBreakPointFromJS("foo", 0, 9); |
| 1531 | foo->Run(); |
| 1532 | CHECK_EQ(4, break_point_hit_count); |
| 1533 | foo->Run(); |
| 1534 | CHECK_EQ(6, break_point_hit_count); |
| 1535 | |
| 1536 | // Run with one breakpoint |
| 1537 | ClearBreakPointFromJS(bp2); |
| 1538 | foo->Run(); |
| 1539 | CHECK_EQ(7, break_point_hit_count); |
| 1540 | foo->Run(); |
| 1541 | CHECK_EQ(8, break_point_hit_count); |
| 1542 | |
| 1543 | // Run without breakpoints. |
| 1544 | ClearBreakPointFromJS(bp1); |
| 1545 | foo->Run(); |
| 1546 | CHECK_EQ(8, break_point_hit_count); |
| 1547 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1548 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1549 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1550 | |
| 1551 | // Make sure that the break point numbers are consecutive. |
| 1552 | CHECK_EQ(1, bp1); |
| 1553 | CHECK_EQ(2, bp2); |
| 1554 | } |
| 1555 | |
| 1556 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1557 | // Test that break points on scripts identified by name can be set using the |
| 1558 | // global Debug object. |
| 1559 | TEST(ScriptBreakPointByNameThroughJavaScript) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1560 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1561 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1562 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1563 | env.ExposeDebug(); |
| 1564 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1565 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1566 | v8::Undefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1567 | |
| 1568 | v8::Local<v8::String> script = v8::String::New( |
| 1569 | "function f() {\n" |
| 1570 | " function h() {\n" |
| 1571 | " a = 0; // line 2\n" |
| 1572 | " }\n" |
| 1573 | " b = 1; // line 4\n" |
| 1574 | " return h();\n" |
| 1575 | "}\n" |
| 1576 | "\n" |
| 1577 | "function g() {\n" |
| 1578 | " function h() {\n" |
| 1579 | " a = 0;\n" |
| 1580 | " }\n" |
| 1581 | " b = 2; // line 12\n" |
| 1582 | " h();\n" |
| 1583 | " b = 3; // line 14\n" |
| 1584 | " f(); // line 15\n" |
| 1585 | "}"); |
| 1586 | |
| 1587 | // Compile the script and get the two functions. |
| 1588 | v8::ScriptOrigin origin = |
| 1589 | v8::ScriptOrigin(v8::String::New("test")); |
| 1590 | v8::Script::Compile(script, &origin)->Run(); |
| 1591 | v8::Local<v8::Function> f = |
| 1592 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1593 | v8::Local<v8::Function> g = |
| 1594 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 1595 | |
| 1596 | // Call f and g without break points. |
| 1597 | break_point_hit_count = 0; |
| 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 | // Call f and g with break point on line 12. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1604 | int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1605 | break_point_hit_count = 0; |
| 1606 | f->Call(env->Global(), 0, NULL); |
| 1607 | CHECK_EQ(0, break_point_hit_count); |
| 1608 | g->Call(env->Global(), 0, NULL); |
| 1609 | CHECK_EQ(1, break_point_hit_count); |
| 1610 | |
| 1611 | // Remove the break point again. |
| 1612 | break_point_hit_count = 0; |
| 1613 | ClearBreakPointFromJS(sbp1); |
| 1614 | f->Call(env->Global(), 0, NULL); |
| 1615 | CHECK_EQ(0, break_point_hit_count); |
| 1616 | g->Call(env->Global(), 0, NULL); |
| 1617 | CHECK_EQ(0, break_point_hit_count); |
| 1618 | |
| 1619 | // Call f and g with break point on line 2. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1620 | int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1621 | break_point_hit_count = 0; |
| 1622 | f->Call(env->Global(), 0, NULL); |
| 1623 | CHECK_EQ(1, break_point_hit_count); |
| 1624 | g->Call(env->Global(), 0, NULL); |
| 1625 | CHECK_EQ(2, break_point_hit_count); |
| 1626 | |
| 1627 | // Call f and g with break point on line 2, 4, 12, 14 and 15. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1628 | int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0); |
| 1629 | int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0); |
| 1630 | int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0); |
| 1631 | int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1632 | break_point_hit_count = 0; |
| 1633 | f->Call(env->Global(), 0, NULL); |
| 1634 | CHECK_EQ(2, break_point_hit_count); |
| 1635 | g->Call(env->Global(), 0, NULL); |
| 1636 | CHECK_EQ(7, break_point_hit_count); |
| 1637 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1638 | // Remove all the break points again. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1639 | break_point_hit_count = 0; |
| 1640 | ClearBreakPointFromJS(sbp2); |
| 1641 | ClearBreakPointFromJS(sbp3); |
| 1642 | ClearBreakPointFromJS(sbp4); |
| 1643 | ClearBreakPointFromJS(sbp5); |
| 1644 | ClearBreakPointFromJS(sbp6); |
| 1645 | f->Call(env->Global(), 0, NULL); |
| 1646 | CHECK_EQ(0, break_point_hit_count); |
| 1647 | g->Call(env->Global(), 0, NULL); |
| 1648 | CHECK_EQ(0, break_point_hit_count); |
| 1649 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1650 | v8::Debug::SetDebugEventListener(NULL); |
| 1651 | CheckDebuggerUnloaded(); |
| 1652 | |
| 1653 | // Make sure that the break point numbers are consecutive. |
| 1654 | CHECK_EQ(1, sbp1); |
| 1655 | CHECK_EQ(2, sbp2); |
| 1656 | CHECK_EQ(3, sbp3); |
| 1657 | CHECK_EQ(4, sbp4); |
| 1658 | CHECK_EQ(5, sbp5); |
| 1659 | CHECK_EQ(6, sbp6); |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | TEST(ScriptBreakPointByIdThroughJavaScript) { |
| 1664 | break_point_hit_count = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1665 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1666 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1667 | env.ExposeDebug(); |
| 1668 | |
| 1669 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 1670 | v8::Undefined()); |
| 1671 | |
| 1672 | v8::Local<v8::String> source = v8::String::New( |
| 1673 | "function f() {\n" |
| 1674 | " function h() {\n" |
| 1675 | " a = 0; // line 2\n" |
| 1676 | " }\n" |
| 1677 | " b = 1; // line 4\n" |
| 1678 | " return h();\n" |
| 1679 | "}\n" |
| 1680 | "\n" |
| 1681 | "function g() {\n" |
| 1682 | " function h() {\n" |
| 1683 | " a = 0;\n" |
| 1684 | " }\n" |
| 1685 | " b = 2; // line 12\n" |
| 1686 | " h();\n" |
| 1687 | " b = 3; // line 14\n" |
| 1688 | " f(); // line 15\n" |
| 1689 | "}"); |
| 1690 | |
| 1691 | // Compile the script and get the two functions. |
| 1692 | v8::ScriptOrigin origin = |
| 1693 | v8::ScriptOrigin(v8::String::New("test")); |
| 1694 | v8::Local<v8::Script> script = v8::Script::Compile(source, &origin); |
| 1695 | script->Run(); |
| 1696 | v8::Local<v8::Function> f = |
| 1697 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1698 | v8::Local<v8::Function> g = |
| 1699 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 1700 | |
| 1701 | // Get the script id knowing that internally it is a 32 integer. |
| 1702 | uint32_t script_id = script->Id()->Uint32Value(); |
| 1703 | |
| 1704 | // Call f and g without break points. |
| 1705 | break_point_hit_count = 0; |
| 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 | // Call f and g with break point on line 12. |
| 1712 | int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); |
| 1713 | break_point_hit_count = 0; |
| 1714 | f->Call(env->Global(), 0, NULL); |
| 1715 | CHECK_EQ(0, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1716 | g->Call(env->Global(), 0, NULL); |
| 1717 | CHECK_EQ(1, break_point_hit_count); |
| 1718 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1719 | // Remove the break point again. |
| 1720 | break_point_hit_count = 0; |
| 1721 | ClearBreakPointFromJS(sbp1); |
| 1722 | f->Call(env->Global(), 0, NULL); |
| 1723 | CHECK_EQ(0, break_point_hit_count); |
| 1724 | g->Call(env->Global(), 0, NULL); |
| 1725 | CHECK_EQ(0, break_point_hit_count); |
| 1726 | |
| 1727 | // Call f and g with break point on line 2. |
| 1728 | int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0); |
| 1729 | break_point_hit_count = 0; |
| 1730 | f->Call(env->Global(), 0, NULL); |
| 1731 | CHECK_EQ(1, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1732 | g->Call(env->Global(), 0, NULL); |
| 1733 | CHECK_EQ(2, break_point_hit_count); |
| 1734 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1735 | // Call f and g with break point on line 2, 4, 12, 14 and 15. |
| 1736 | int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0); |
| 1737 | int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); |
| 1738 | int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0); |
| 1739 | int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0); |
| 1740 | break_point_hit_count = 0; |
| 1741 | f->Call(env->Global(), 0, NULL); |
| 1742 | CHECK_EQ(2, break_point_hit_count); |
| 1743 | g->Call(env->Global(), 0, NULL); |
| 1744 | CHECK_EQ(7, break_point_hit_count); |
| 1745 | |
| 1746 | // Remove all the break points again. |
| 1747 | break_point_hit_count = 0; |
| 1748 | ClearBreakPointFromJS(sbp2); |
| 1749 | ClearBreakPointFromJS(sbp3); |
| 1750 | ClearBreakPointFromJS(sbp4); |
| 1751 | ClearBreakPointFromJS(sbp5); |
| 1752 | ClearBreakPointFromJS(sbp6); |
| 1753 | f->Call(env->Global(), 0, NULL); |
| 1754 | CHECK_EQ(0, break_point_hit_count); |
| 1755 | g->Call(env->Global(), 0, NULL); |
| 1756 | CHECK_EQ(0, break_point_hit_count); |
| 1757 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1758 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1759 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Make sure that the break point numbers are consecutive. |
| 1762 | CHECK_EQ(1, sbp1); |
| 1763 | CHECK_EQ(2, sbp2); |
| 1764 | CHECK_EQ(3, sbp3); |
| 1765 | CHECK_EQ(4, sbp4); |
| 1766 | CHECK_EQ(5, sbp5); |
| 1767 | CHECK_EQ(6, sbp6); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | |
| 1771 | // Test conditional script break points. |
| 1772 | TEST(EnableDisableScriptBreakPoint) { |
| 1773 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1774 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1775 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1776 | env.ExposeDebug(); |
| 1777 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1778 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1779 | v8::Undefined()); |
| 1780 | |
| 1781 | v8::Local<v8::String> script = v8::String::New( |
| 1782 | "function f() {\n" |
| 1783 | " a = 0; // line 1\n" |
| 1784 | "};"); |
| 1785 | |
| 1786 | // Compile the script and get function f. |
| 1787 | v8::ScriptOrigin origin = |
| 1788 | v8::ScriptOrigin(v8::String::New("test")); |
| 1789 | v8::Script::Compile(script, &origin)->Run(); |
| 1790 | v8::Local<v8::Function> f = |
| 1791 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1792 | |
| 1793 | // Set script break point on line 1 (in function f). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1794 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1795 | |
| 1796 | // Call f while enabeling and disabling the script break point. |
| 1797 | break_point_hit_count = 0; |
| 1798 | f->Call(env->Global(), 0, NULL); |
| 1799 | CHECK_EQ(1, break_point_hit_count); |
| 1800 | |
| 1801 | DisableScriptBreakPointFromJS(sbp); |
| 1802 | f->Call(env->Global(), 0, NULL); |
| 1803 | CHECK_EQ(1, break_point_hit_count); |
| 1804 | |
| 1805 | EnableScriptBreakPointFromJS(sbp); |
| 1806 | f->Call(env->Global(), 0, NULL); |
| 1807 | CHECK_EQ(2, break_point_hit_count); |
| 1808 | |
| 1809 | DisableScriptBreakPointFromJS(sbp); |
| 1810 | f->Call(env->Global(), 0, NULL); |
| 1811 | CHECK_EQ(2, break_point_hit_count); |
| 1812 | |
| 1813 | // Reload the script and get f again checking that the disabeling survives. |
| 1814 | v8::Script::Compile(script, &origin)->Run(); |
| 1815 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1816 | f->Call(env->Global(), 0, NULL); |
| 1817 | CHECK_EQ(2, break_point_hit_count); |
| 1818 | |
| 1819 | EnableScriptBreakPointFromJS(sbp); |
| 1820 | f->Call(env->Global(), 0, NULL); |
| 1821 | CHECK_EQ(3, break_point_hit_count); |
| 1822 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1823 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1824 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | |
| 1828 | // Test conditional script break points. |
| 1829 | TEST(ConditionalScriptBreakPoint) { |
| 1830 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1831 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1832 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1833 | env.ExposeDebug(); |
| 1834 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1835 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1836 | v8::Undefined()); |
| 1837 | |
| 1838 | v8::Local<v8::String> script = v8::String::New( |
| 1839 | "count = 0;\n" |
| 1840 | "function f() {\n" |
| 1841 | " g(count++); // line 2\n" |
| 1842 | "};\n" |
| 1843 | "function g(x) {\n" |
| 1844 | " var a=x; // line 5\n" |
| 1845 | "};"); |
| 1846 | |
| 1847 | // Compile the script and get function f. |
| 1848 | v8::ScriptOrigin origin = |
| 1849 | v8::ScriptOrigin(v8::String::New("test")); |
| 1850 | v8::Script::Compile(script, &origin)->Run(); |
| 1851 | v8::Local<v8::Function> f = |
| 1852 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1853 | |
| 1854 | // Set script break point on line 5 (in function g). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1855 | int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Call f with different conditions on the script break point. |
| 1858 | break_point_hit_count = 0; |
| 1859 | ChangeScriptBreakPointConditionFromJS(sbp1, "false"); |
| 1860 | f->Call(env->Global(), 0, NULL); |
| 1861 | CHECK_EQ(0, break_point_hit_count); |
| 1862 | |
| 1863 | ChangeScriptBreakPointConditionFromJS(sbp1, "true"); |
| 1864 | break_point_hit_count = 0; |
| 1865 | f->Call(env->Global(), 0, NULL); |
| 1866 | CHECK_EQ(1, break_point_hit_count); |
| 1867 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1868 | ChangeScriptBreakPointConditionFromJS(sbp1, "x % 2 == 0"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1869 | break_point_hit_count = 0; |
| 1870 | for (int i = 0; i < 10; i++) { |
| 1871 | f->Call(env->Global(), 0, NULL); |
| 1872 | } |
| 1873 | CHECK_EQ(5, break_point_hit_count); |
| 1874 | |
| 1875 | // Reload the script and get f again checking that the condition survives. |
| 1876 | v8::Script::Compile(script, &origin)->Run(); |
| 1877 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1878 | |
| 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 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1885 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1886 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | // Test ignore count on script break points. |
| 1891 | TEST(ScriptBreakPointIgnoreCount) { |
| 1892 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1893 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1894 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1895 | env.ExposeDebug(); |
| 1896 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1897 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1898 | v8::Undefined()); |
| 1899 | |
| 1900 | v8::Local<v8::String> script = v8::String::New( |
| 1901 | "function f() {\n" |
| 1902 | " a = 0; // line 1\n" |
| 1903 | "};"); |
| 1904 | |
| 1905 | // Compile the script and get function f. |
| 1906 | v8::ScriptOrigin origin = |
| 1907 | v8::ScriptOrigin(v8::String::New("test")); |
| 1908 | v8::Script::Compile(script, &origin)->Run(); |
| 1909 | v8::Local<v8::Function> f = |
| 1910 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1911 | |
| 1912 | // Set script break point on line 1 (in function f). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1913 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Call f with different ignores on the script break point. |
| 1916 | break_point_hit_count = 0; |
| 1917 | ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1); |
| 1918 | f->Call(env->Global(), 0, NULL); |
| 1919 | CHECK_EQ(0, break_point_hit_count); |
| 1920 | f->Call(env->Global(), 0, NULL); |
| 1921 | CHECK_EQ(1, break_point_hit_count); |
| 1922 | |
| 1923 | ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5); |
| 1924 | break_point_hit_count = 0; |
| 1925 | for (int i = 0; i < 10; i++) { |
| 1926 | f->Call(env->Global(), 0, NULL); |
| 1927 | } |
| 1928 | CHECK_EQ(5, break_point_hit_count); |
| 1929 | |
| 1930 | // Reload the script and get f again checking that the ignore survives. |
| 1931 | v8::Script::Compile(script, &origin)->Run(); |
| 1932 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1933 | |
| 1934 | break_point_hit_count = 0; |
| 1935 | for (int i = 0; i < 10; i++) { |
| 1936 | f->Call(env->Global(), 0, NULL); |
| 1937 | } |
| 1938 | CHECK_EQ(5, break_point_hit_count); |
| 1939 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1940 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1941 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | |
| 1945 | // Test that script break points survive when a script is reloaded. |
| 1946 | TEST(ScriptBreakPointReload) { |
| 1947 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1948 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1949 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1950 | env.ExposeDebug(); |
| 1951 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1952 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1953 | v8::Undefined()); |
| 1954 | |
| 1955 | v8::Local<v8::Function> f; |
| 1956 | v8::Local<v8::String> script = v8::String::New( |
| 1957 | "function f() {\n" |
| 1958 | " function h() {\n" |
| 1959 | " a = 0; // line 2\n" |
| 1960 | " }\n" |
| 1961 | " b = 1; // line 4\n" |
| 1962 | " return h();\n" |
| 1963 | "}"); |
| 1964 | |
| 1965 | v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1")); |
| 1966 | v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2")); |
| 1967 | |
| 1968 | // Set a script break point before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1969 | SetScriptBreakPointByNameFromJS("1", 2, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1970 | |
| 1971 | // Compile the script and get the function. |
| 1972 | v8::Script::Compile(script, &origin_1)->Run(); |
| 1973 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1974 | |
| 1975 | // Call f and check that the script break point is active. |
| 1976 | break_point_hit_count = 0; |
| 1977 | f->Call(env->Global(), 0, NULL); |
| 1978 | CHECK_EQ(1, break_point_hit_count); |
| 1979 | |
| 1980 | // Compile the script again with a different script data and get the |
| 1981 | // function. |
| 1982 | v8::Script::Compile(script, &origin_2)->Run(); |
| 1983 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1984 | |
| 1985 | // Call f and check that no break points are set. |
| 1986 | break_point_hit_count = 0; |
| 1987 | f->Call(env->Global(), 0, NULL); |
| 1988 | CHECK_EQ(0, break_point_hit_count); |
| 1989 | |
| 1990 | // Compile the script again and get the function. |
| 1991 | v8::Script::Compile(script, &origin_1)->Run(); |
| 1992 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1993 | |
| 1994 | // Call f and check that the script break point is active. |
| 1995 | break_point_hit_count = 0; |
| 1996 | f->Call(env->Global(), 0, NULL); |
| 1997 | CHECK_EQ(1, break_point_hit_count); |
| 1998 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1999 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2000 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | // Test when several scripts has the same script data |
| 2005 | TEST(ScriptBreakPointMultiple) { |
| 2006 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2007 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2008 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2009 | env.ExposeDebug(); |
| 2010 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2011 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2012 | v8::Undefined()); |
| 2013 | |
| 2014 | v8::Local<v8::Function> f; |
| 2015 | v8::Local<v8::String> script_f = v8::String::New( |
| 2016 | "function f() {\n" |
| 2017 | " a = 0; // line 1\n" |
| 2018 | "}"); |
| 2019 | |
| 2020 | v8::Local<v8::Function> g; |
| 2021 | v8::Local<v8::String> script_g = v8::String::New( |
| 2022 | "function g() {\n" |
| 2023 | " b = 0; // line 1\n" |
| 2024 | "}"); |
| 2025 | |
| 2026 | v8::ScriptOrigin origin = |
| 2027 | v8::ScriptOrigin(v8::String::New("test")); |
| 2028 | |
| 2029 | // Set a script break point before the scripts are loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2030 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Compile the scripts with same script data and get the functions. |
| 2033 | v8::Script::Compile(script_f, &origin)->Run(); |
| 2034 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2035 | v8::Script::Compile(script_g, &origin)->Run(); |
| 2036 | g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 2037 | |
| 2038 | // Call f and g and check that the script break point is active. |
| 2039 | break_point_hit_count = 0; |
| 2040 | f->Call(env->Global(), 0, NULL); |
| 2041 | CHECK_EQ(1, break_point_hit_count); |
| 2042 | g->Call(env->Global(), 0, NULL); |
| 2043 | CHECK_EQ(2, break_point_hit_count); |
| 2044 | |
| 2045 | // Clear the script break point. |
| 2046 | ClearBreakPointFromJS(sbp); |
| 2047 | |
| 2048 | // Call f and g and check that the script break point is no longer active. |
| 2049 | break_point_hit_count = 0; |
| 2050 | f->Call(env->Global(), 0, NULL); |
| 2051 | CHECK_EQ(0, break_point_hit_count); |
| 2052 | g->Call(env->Global(), 0, NULL); |
| 2053 | CHECK_EQ(0, break_point_hit_count); |
| 2054 | |
| 2055 | // Set script break point with the scripts loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2056 | sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Call f and g and check that the script break point is active. |
| 2059 | break_point_hit_count = 0; |
| 2060 | f->Call(env->Global(), 0, NULL); |
| 2061 | CHECK_EQ(1, break_point_hit_count); |
| 2062 | g->Call(env->Global(), 0, NULL); |
| 2063 | CHECK_EQ(2, break_point_hit_count); |
| 2064 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2065 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2066 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | |
| 2070 | // Test the script origin which has both name and line offset. |
| 2071 | TEST(ScriptBreakPointLineOffset) { |
| 2072 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2073 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2074 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2075 | env.ExposeDebug(); |
| 2076 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2077 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2078 | v8::Undefined()); |
| 2079 | |
| 2080 | v8::Local<v8::Function> f; |
| 2081 | v8::Local<v8::String> script = v8::String::New( |
| 2082 | "function f() {\n" |
| 2083 | " a = 0; // line 8 as this script has line offset 7\n" |
| 2084 | " b = 0; // line 9 as this script has line offset 7\n" |
| 2085 | "}"); |
| 2086 | |
| 2087 | // Create script origin both name and line offset. |
| 2088 | v8::ScriptOrigin origin(v8::String::New("test.html"), |
| 2089 | v8::Integer::New(7)); |
| 2090 | |
| 2091 | // Set two script break points before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2092 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0); |
| 2093 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2094 | |
| 2095 | // Compile the script and get the function. |
| 2096 | v8::Script::Compile(script, &origin)->Run(); |
| 2097 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2098 | |
| 2099 | // Call f and check that the script break point is active. |
| 2100 | break_point_hit_count = 0; |
| 2101 | f->Call(env->Global(), 0, NULL); |
| 2102 | CHECK_EQ(2, break_point_hit_count); |
| 2103 | |
| 2104 | // Clear the script break points. |
| 2105 | ClearBreakPointFromJS(sbp1); |
| 2106 | ClearBreakPointFromJS(sbp2); |
| 2107 | |
| 2108 | // Call f and check that no script break points are active. |
| 2109 | break_point_hit_count = 0; |
| 2110 | f->Call(env->Global(), 0, NULL); |
| 2111 | CHECK_EQ(0, break_point_hit_count); |
| 2112 | |
| 2113 | // Set a script break point with the script loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2114 | sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Call f and check that the script break point is active. |
| 2117 | break_point_hit_count = 0; |
| 2118 | f->Call(env->Global(), 0, NULL); |
| 2119 | CHECK_EQ(1, break_point_hit_count); |
| 2120 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2121 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2122 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | // Test script break points set on lines. |
| 2127 | TEST(ScriptBreakPointLine) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2128 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2129 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2130 | env.ExposeDebug(); |
| 2131 | |
| 2132 | // Create a function for checking the function when hitting a break point. |
| 2133 | frame_function_name = CompileFunction(&env, |
| 2134 | frame_function_name_source, |
| 2135 | "frame_function_name"); |
| 2136 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2137 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2138 | v8::Undefined()); |
| 2139 | |
| 2140 | v8::Local<v8::Function> f; |
| 2141 | v8::Local<v8::Function> g; |
| 2142 | v8::Local<v8::String> script = v8::String::New( |
| 2143 | "a = 0 // line 0\n" |
| 2144 | "function f() {\n" |
| 2145 | " a = 1; // line 2\n" |
| 2146 | "}\n" |
| 2147 | " a = 2; // line 4\n" |
| 2148 | " /* xx */ function g() { // line 5\n" |
| 2149 | " function h() { // line 6\n" |
| 2150 | " a = 3; // line 7\n" |
| 2151 | " }\n" |
| 2152 | " h(); // line 9\n" |
| 2153 | " a = 4; // line 10\n" |
| 2154 | " }\n" |
| 2155 | " a=5; // line 12"); |
| 2156 | |
| 2157 | // Set a couple script break point before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2158 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1); |
| 2159 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1); |
| 2160 | int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2161 | |
| 2162 | // Compile the script and get the function. |
| 2163 | break_point_hit_count = 0; |
| 2164 | v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0)); |
| 2165 | v8::Script::Compile(script, &origin)->Run(); |
| 2166 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2167 | g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 2168 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 2169 | // Check that a break point was hit when the script was run. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2170 | CHECK_EQ(1, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2171 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2172 | |
| 2173 | // Call f and check that the script break point. |
| 2174 | f->Call(env->Global(), 0, NULL); |
| 2175 | CHECK_EQ(2, break_point_hit_count); |
| 2176 | CHECK_EQ("f", last_function_hit); |
| 2177 | |
| 2178 | // Call g and check that the script break point. |
| 2179 | g->Call(env->Global(), 0, NULL); |
| 2180 | CHECK_EQ(3, break_point_hit_count); |
| 2181 | CHECK_EQ("g", last_function_hit); |
| 2182 | |
| 2183 | // Clear the script break point on g and set one on h. |
| 2184 | ClearBreakPointFromJS(sbp3); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2186 | |
| 2187 | // Call g and check that the script break point in h is hit. |
| 2188 | g->Call(env->Global(), 0, NULL); |
| 2189 | CHECK_EQ(4, break_point_hit_count); |
| 2190 | CHECK_EQ("h", last_function_hit); |
| 2191 | |
| 2192 | // Clear break points in f and h. Set a new one in the script between |
| 2193 | // functions f and g and test that there is no break points in f and g any |
| 2194 | // more. |
| 2195 | ClearBreakPointFromJS(sbp2); |
| 2196 | ClearBreakPointFromJS(sbp4); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2197 | int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2198 | break_point_hit_count = 0; |
| 2199 | f->Call(env->Global(), 0, NULL); |
| 2200 | g->Call(env->Global(), 0, NULL); |
| 2201 | CHECK_EQ(0, break_point_hit_count); |
| 2202 | |
| 2203 | // Reload the script which should hit two break points. |
| 2204 | break_point_hit_count = 0; |
| 2205 | v8::Script::Compile(script, &origin)->Run(); |
| 2206 | CHECK_EQ(2, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2207 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Set a break point in the code after the last function decleration. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2210 | int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2211 | |
| 2212 | // Reload the script which should hit three break points. |
| 2213 | break_point_hit_count = 0; |
| 2214 | v8::Script::Compile(script, &origin)->Run(); |
| 2215 | CHECK_EQ(3, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2216 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2217 | |
| 2218 | // Clear the last break points, and reload the script which should not hit any |
| 2219 | // break points. |
| 2220 | ClearBreakPointFromJS(sbp1); |
| 2221 | ClearBreakPointFromJS(sbp5); |
| 2222 | ClearBreakPointFromJS(sbp6); |
| 2223 | break_point_hit_count = 0; |
| 2224 | v8::Script::Compile(script, &origin)->Run(); |
| 2225 | CHECK_EQ(0, break_point_hit_count); |
| 2226 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2227 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2228 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2232 | // Test top level script break points set on lines. |
| 2233 | TEST(ScriptBreakPointLineTopLevel) { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2234 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2235 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2236 | env.ExposeDebug(); |
| 2237 | |
| 2238 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2239 | v8::Undefined()); |
| 2240 | |
| 2241 | v8::Local<v8::String> script = v8::String::New( |
| 2242 | "function f() {\n" |
| 2243 | " a = 1; // line 1\n" |
| 2244 | "}\n" |
| 2245 | "a = 2; // line 3\n"); |
| 2246 | v8::Local<v8::Function> f; |
| 2247 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2248 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2249 | v8::Script::Compile(script, v8::String::New("test.html"))->Run(); |
| 2250 | } |
| 2251 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2252 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2253 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2254 | |
| 2255 | SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2256 | |
| 2257 | // Call f and check that there was no break points. |
| 2258 | break_point_hit_count = 0; |
| 2259 | f->Call(env->Global(), 0, NULL); |
| 2260 | CHECK_EQ(0, break_point_hit_count); |
| 2261 | |
| 2262 | // Recompile and run script and check that break point was hit. |
| 2263 | break_point_hit_count = 0; |
| 2264 | v8::Script::Compile(script, v8::String::New("test.html"))->Run(); |
| 2265 | CHECK_EQ(1, break_point_hit_count); |
| 2266 | |
| 2267 | // Call f and check that there are still no break points. |
| 2268 | break_point_hit_count = 0; |
| 2269 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2270 | CHECK_EQ(0, break_point_hit_count); |
| 2271 | |
| 2272 | v8::Debug::SetDebugEventListener(NULL); |
| 2273 | CheckDebuggerUnloaded(); |
| 2274 | } |
| 2275 | |
| 2276 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2277 | // Test that it is possible to add and remove break points in a top level |
| 2278 | // function which has no references but has not been collected yet. |
| 2279 | TEST(ScriptBreakPointTopLevelCrash) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2280 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2281 | v8::HandleScope scope(env->GetIsolate()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2282 | env.ExposeDebug(); |
| 2283 | |
| 2284 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2285 | v8::Undefined()); |
| 2286 | |
| 2287 | v8::Local<v8::String> script_source = v8::String::New( |
| 2288 | "function f() {\n" |
| 2289 | " return 0;\n" |
| 2290 | "}\n" |
| 2291 | "f()"); |
| 2292 | |
| 2293 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2294 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2295 | v8::HandleScope scope(env->GetIsolate()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2296 | break_point_hit_count = 0; |
| 2297 | v8::Script::Compile(script_source, v8::String::New("test.html"))->Run(); |
| 2298 | CHECK_EQ(1, break_point_hit_count); |
| 2299 | } |
| 2300 | |
| 2301 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2302 | ClearBreakPointFromJS(sbp1); |
| 2303 | ClearBreakPointFromJS(sbp2); |
| 2304 | |
| 2305 | v8::Debug::SetDebugEventListener(NULL); |
| 2306 | CheckDebuggerUnloaded(); |
| 2307 | } |
| 2308 | |
| 2309 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2310 | // Test that it is possible to remove the last break point for a function |
| 2311 | // inside the break handling of that break point. |
| 2312 | TEST(RemoveBreakPointInBreak) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2313 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2314 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2315 | |
| 2316 | v8::Local<v8::Function> foo = |
| 2317 | CompileFunction(&env, "function foo(){a=1;}", "foo"); |
| 2318 | debug_event_remove_break_point = SetBreakPoint(foo, 0); |
| 2319 | |
| 2320 | // Register the debug event listener pasing the function |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2321 | v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2322 | |
| 2323 | break_point_hit_count = 0; |
| 2324 | foo->Call(env->Global(), 0, NULL); |
| 2325 | CHECK_EQ(1, break_point_hit_count); |
| 2326 | |
| 2327 | break_point_hit_count = 0; |
| 2328 | foo->Call(env->Global(), 0, NULL); |
| 2329 | CHECK_EQ(0, break_point_hit_count); |
| 2330 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2331 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2332 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | |
| 2336 | // Test that the debugger statement causes a break. |
| 2337 | TEST(DebuggerStatement) { |
| 2338 | break_point_hit_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2339 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2340 | v8::HandleScope scope(env->GetIsolate()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2341 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2342 | v8::Undefined()); |
| 2343 | v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run(); |
| 2344 | v8::Script::Compile(v8::String::New( |
| 2345 | "function foo(){debugger;debugger;}"))->Run(); |
| 2346 | v8::Local<v8::Function> foo = |
| 2347 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 2348 | v8::Local<v8::Function> bar = |
| 2349 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar"))); |
| 2350 | |
| 2351 | // Run function with debugger statement |
| 2352 | bar->Call(env->Global(), 0, NULL); |
| 2353 | CHECK_EQ(1, break_point_hit_count); |
| 2354 | |
| 2355 | // Run function with two debugger statement |
| 2356 | foo->Call(env->Global(), 0, NULL); |
| 2357 | CHECK_EQ(3, break_point_hit_count); |
| 2358 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2359 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2360 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2364 | // Test setting a breakpoint on the debugger statement. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2365 | TEST(DebuggerStatementBreakpoint) { |
| 2366 | break_point_hit_count = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2367 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2368 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2369 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2370 | v8::Undefined()); |
| 2371 | v8::Script::Compile(v8::String::New("function foo(){debugger;}"))->Run(); |
| 2372 | v8::Local<v8::Function> foo = |
| 2373 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 2374 | |
| 2375 | // The debugger statement triggers breakpint hit |
| 2376 | foo->Call(env->Global(), 0, NULL); |
| 2377 | CHECK_EQ(1, break_point_hit_count); |
| 2378 | |
| 2379 | int bp = SetBreakPoint(foo, 0); |
| 2380 | |
| 2381 | // Set breakpoint does not duplicate hits |
| 2382 | foo->Call(env->Global(), 0, NULL); |
| 2383 | CHECK_EQ(2, break_point_hit_count); |
| 2384 | |
| 2385 | ClearBreakPoint(bp); |
| 2386 | v8::Debug::SetDebugEventListener(NULL); |
| 2387 | CheckDebuggerUnloaded(); |
| 2388 | } |
| 2389 | |
| 2390 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2391 | // Test that the evaluation of expressions when a break point is hit generates |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2392 | // the correct results. |
| 2393 | TEST(DebugEvaluate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2394 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2395 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2396 | env.ExposeDebug(); |
| 2397 | |
| 2398 | // Create a function for checking the evaluation when hitting a break point. |
| 2399 | evaluate_check_function = CompileFunction(&env, |
| 2400 | evaluate_check_source, |
| 2401 | "evaluate_check"); |
| 2402 | // Register the debug event listener |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2403 | v8::Debug::SetDebugEventListener(DebugEventEvaluate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Different expected vaules of x and a when in a break point (u = undefined, |
| 2406 | // d = Hello, world!). |
| 2407 | struct EvaluateCheck checks_uu[] = { |
| 2408 | {"x", v8::Undefined()}, |
| 2409 | {"a", v8::Undefined()}, |
| 2410 | {NULL, v8::Handle<v8::Value>()} |
| 2411 | }; |
| 2412 | struct EvaluateCheck checks_hu[] = { |
| 2413 | {"x", v8::String::New("Hello, world!")}, |
| 2414 | {"a", v8::Undefined()}, |
| 2415 | {NULL, v8::Handle<v8::Value>()} |
| 2416 | }; |
| 2417 | struct EvaluateCheck checks_hh[] = { |
| 2418 | {"x", v8::String::New("Hello, world!")}, |
| 2419 | {"a", v8::String::New("Hello, world!")}, |
| 2420 | {NULL, v8::Handle<v8::Value>()} |
| 2421 | }; |
| 2422 | |
| 2423 | // Simple test function. The "y=0" is in the function foo to provide a break |
| 2424 | // location. For "y=0" the "y" is at position 15 in the barbar function |
| 2425 | // therefore setting breakpoint at position 15 will break at "y=0" and |
| 2426 | // setting it higher will break after. |
| 2427 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2428 | "function foo(x) {" |
| 2429 | " var a;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2430 | " y=0;" // To ensure break location 1. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2431 | " a=x;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2432 | " y=0;" // To ensure break location 2. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2433 | "}", |
| 2434 | "foo"); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2435 | const int foo_break_position_1 = 15; |
| 2436 | const int foo_break_position_2 = 29; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2437 | |
| 2438 | // Arguments with one parameter "Hello, world!" |
| 2439 | v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") }; |
| 2440 | |
| 2441 | // Call foo with breakpoint set before a=x and undefined as parameter. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2442 | int bp = SetBreakPoint(foo, foo_break_position_1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2443 | checks = checks_uu; |
| 2444 | foo->Call(env->Global(), 0, NULL); |
| 2445 | |
| 2446 | // Call foo with breakpoint set before a=x and parameter "Hello, world!". |
| 2447 | checks = checks_hu; |
| 2448 | foo->Call(env->Global(), 1, argv_foo); |
| 2449 | |
| 2450 | // Call foo with breakpoint set after a=x and parameter "Hello, world!". |
| 2451 | ClearBreakPoint(bp); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2452 | SetBreakPoint(foo, foo_break_position_2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2453 | checks = checks_hh; |
| 2454 | foo->Call(env->Global(), 1, argv_foo); |
| 2455 | |
| 2456 | // Test function with an inner function. The "y=0" is in function barbar |
| 2457 | // to provide a break location. For "y=0" the "y" is at position 8 in the |
| 2458 | // barbar function therefore setting breakpoint at position 8 will break at |
| 2459 | // "y=0" and setting it higher will break after. |
| 2460 | v8::Local<v8::Function> bar = CompileFunction(&env, |
| 2461 | "y = 0;" |
| 2462 | "x = 'Goodbye, world!';" |
| 2463 | "function bar(x, b) {" |
| 2464 | " var a;" |
| 2465 | " function barbar() {" |
| 2466 | " y=0; /* To ensure break location.*/" |
| 2467 | " a=x;" |
| 2468 | " };" |
| 2469 | " debug.Debug.clearAllBreakPoints();" |
| 2470 | " barbar();" |
| 2471 | " y=0;a=x;" |
| 2472 | "}", |
| 2473 | "bar"); |
| 2474 | const int barbar_break_position = 8; |
| 2475 | |
| 2476 | // Call bar setting breakpoint before a=x in barbar and undefined as |
| 2477 | // parameter. |
| 2478 | checks = checks_uu; |
| 2479 | v8::Handle<v8::Value> argv_bar_1[2] = { |
| 2480 | v8::Undefined(), |
| 2481 | v8::Number::New(barbar_break_position) |
| 2482 | }; |
| 2483 | bar->Call(env->Global(), 2, argv_bar_1); |
| 2484 | |
| 2485 | // Call bar setting breakpoint before a=x in barbar and parameter |
| 2486 | // "Hello, world!". |
| 2487 | checks = checks_hu; |
| 2488 | v8::Handle<v8::Value> argv_bar_2[2] = { |
| 2489 | v8::String::New("Hello, world!"), |
| 2490 | v8::Number::New(barbar_break_position) |
| 2491 | }; |
| 2492 | bar->Call(env->Global(), 2, argv_bar_2); |
| 2493 | |
| 2494 | // Call bar setting breakpoint after a=x in barbar and parameter |
| 2495 | // "Hello, world!". |
| 2496 | checks = checks_hh; |
| 2497 | v8::Handle<v8::Value> argv_bar_3[2] = { |
| 2498 | v8::String::New("Hello, world!"), |
| 2499 | v8::Number::New(barbar_break_position + 1) |
| 2500 | }; |
| 2501 | bar->Call(env->Global(), 2, argv_bar_3); |
| 2502 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2503 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2504 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2507 | |
| 2508 | int debugEventCount = 0; |
| 2509 | static void CheckDebugEvent(const v8::Debug::EventDetails& eventDetails) { |
| 2510 | if (eventDetails.GetEvent() == v8::Break) ++debugEventCount; |
| 2511 | } |
| 2512 | |
| 2513 | // Test that the conditional breakpoints work event if code generation from |
| 2514 | // strings is prohibited in the debugee context. |
| 2515 | TEST(ConditionalBreakpointWithCodeGenerationDisallowed) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2516 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2517 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2518 | env.ExposeDebug(); |
| 2519 | |
| 2520 | v8::Debug::SetDebugEventListener2(CheckDebugEvent); |
| 2521 | |
| 2522 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2523 | "function foo(x) {\n" |
| 2524 | " var s = 'String value2';\n" |
| 2525 | " return s + x;\n" |
| 2526 | "}", |
| 2527 | "foo"); |
| 2528 | |
| 2529 | // Set conditional breakpoint with condition 'true'. |
| 2530 | CompileRun("debug.Debug.setBreakPoint(foo, 2, 0, 'true')"); |
| 2531 | |
| 2532 | debugEventCount = 0; |
| 2533 | env->AllowCodeGenerationFromStrings(false); |
| 2534 | foo->Call(env->Global(), 0, NULL); |
| 2535 | CHECK_EQ(1, debugEventCount); |
| 2536 | |
| 2537 | v8::Debug::SetDebugEventListener2(NULL); |
| 2538 | CheckDebuggerUnloaded(); |
| 2539 | } |
| 2540 | |
| 2541 | |
| 2542 | bool checkedDebugEvals = true; |
| 2543 | v8::Handle<v8::Function> checkGlobalEvalFunction; |
| 2544 | v8::Handle<v8::Function> checkFrameEvalFunction; |
| 2545 | static void CheckDebugEval(const v8::Debug::EventDetails& eventDetails) { |
| 2546 | if (eventDetails.GetEvent() == v8::Break) { |
| 2547 | ++debugEventCount; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2548 | v8::HandleScope handleScope(v8::Isolate::GetCurrent()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2549 | |
| 2550 | v8::Handle<v8::Value> args[] = { eventDetails.GetExecutionState() }; |
| 2551 | CHECK(checkGlobalEvalFunction->Call( |
| 2552 | eventDetails.GetEventContext()->Global(), 1, args)->IsTrue()); |
| 2553 | CHECK(checkFrameEvalFunction->Call( |
| 2554 | eventDetails.GetEventContext()->Global(), 1, args)->IsTrue()); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | // Test that the evaluation of expressions when a break point is hit generates |
| 2559 | // the correct results in case code generation from strings is disallowed in the |
| 2560 | // debugee context. |
| 2561 | TEST(DebugEvaluateWithCodeGenerationDisallowed) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2562 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2563 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2564 | env.ExposeDebug(); |
| 2565 | |
| 2566 | v8::Debug::SetDebugEventListener2(CheckDebugEval); |
| 2567 | |
| 2568 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2569 | "var global = 'Global';\n" |
| 2570 | "function foo(x) {\n" |
| 2571 | " var local = 'Local';\n" |
| 2572 | " debugger;\n" |
| 2573 | " return local + x;\n" |
| 2574 | "}", |
| 2575 | "foo"); |
| 2576 | checkGlobalEvalFunction = CompileFunction(&env, |
| 2577 | "function checkGlobalEval(exec_state) {\n" |
| 2578 | " return exec_state.evaluateGlobal('global').value() === 'Global';\n" |
| 2579 | "}", |
| 2580 | "checkGlobalEval"); |
| 2581 | |
| 2582 | checkFrameEvalFunction = CompileFunction(&env, |
| 2583 | "function checkFrameEval(exec_state) {\n" |
| 2584 | " return exec_state.frame(0).evaluate('local').value() === 'Local';\n" |
| 2585 | "}", |
| 2586 | "checkFrameEval"); |
| 2587 | debugEventCount = 0; |
| 2588 | env->AllowCodeGenerationFromStrings(false); |
| 2589 | foo->Call(env->Global(), 0, NULL); |
| 2590 | CHECK_EQ(1, debugEventCount); |
| 2591 | |
| 2592 | checkGlobalEvalFunction.Clear(); |
| 2593 | checkFrameEvalFunction.Clear(); |
| 2594 | v8::Debug::SetDebugEventListener2(NULL); |
| 2595 | CheckDebuggerUnloaded(); |
| 2596 | } |
| 2597 | |
| 2598 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2599 | // Copies a C string to a 16-bit string. Does not check for buffer overflow. |
| 2600 | // Does not use the V8 engine to convert strings, so it can be used |
| 2601 | // in any thread. Returns the length of the string. |
| 2602 | int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) { |
| 2603 | int i; |
| 2604 | for (i = 0; input_buffer[i] != '\0'; ++i) { |
| 2605 | // ASCII does not use chars > 127, but be careful anyway. |
| 2606 | output_buffer[i] = static_cast<unsigned char>(input_buffer[i]); |
| 2607 | } |
| 2608 | output_buffer[i] = 0; |
| 2609 | return i; |
| 2610 | } |
| 2611 | |
| 2612 | // Copies a 16-bit string to a C string by dropping the high byte of |
| 2613 | // each character. Does not check for buffer overflow. |
| 2614 | // Can be used in any thread. Requires string length as an input. |
| 2615 | int Utf16ToAscii(const uint16_t* input_buffer, int length, |
| 2616 | char* output_buffer, int output_len = -1) { |
| 2617 | if (output_len >= 0) { |
| 2618 | if (length > output_len - 1) { |
| 2619 | length = output_len - 1; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | for (int i = 0; i < length; ++i) { |
| 2624 | output_buffer[i] = static_cast<char>(input_buffer[i]); |
| 2625 | } |
| 2626 | output_buffer[length] = '\0'; |
| 2627 | return length; |
| 2628 | } |
| 2629 | |
| 2630 | |
| 2631 | // We match parts of the message to get evaluate result int value. |
| 2632 | bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) { |
| 2633 | if (strstr(message, "\"command\":\"evaluate\"") == NULL) { |
| 2634 | return false; |
| 2635 | } |
| 2636 | const char* prefix = "\"text\":\""; |
| 2637 | char* pos1 = strstr(message, prefix); |
| 2638 | if (pos1 == NULL) { |
| 2639 | return false; |
| 2640 | } |
| 2641 | pos1 += strlen(prefix); |
| 2642 | char* pos2 = strchr(pos1, '"'); |
| 2643 | if (pos2 == NULL) { |
| 2644 | return false; |
| 2645 | } |
| 2646 | Vector<char> buf(buffer, buffer_size); |
| 2647 | int len = static_cast<int>(pos2 - pos1); |
| 2648 | if (len > buffer_size - 1) { |
| 2649 | len = buffer_size - 1; |
| 2650 | } |
| 2651 | OS::StrNCpy(buf, pos1, len); |
| 2652 | buffer[buffer_size - 1] = '\0'; |
| 2653 | return true; |
| 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | struct EvaluateResult { |
| 2658 | static const int kBufferSize = 20; |
| 2659 | char buffer[kBufferSize]; |
| 2660 | }; |
| 2661 | |
| 2662 | struct DebugProcessDebugMessagesData { |
| 2663 | static const int kArraySize = 5; |
| 2664 | int counter; |
| 2665 | EvaluateResult results[kArraySize]; |
| 2666 | |
| 2667 | void reset() { |
| 2668 | counter = 0; |
| 2669 | } |
| 2670 | EvaluateResult* current() { |
| 2671 | return &results[counter % kArraySize]; |
| 2672 | } |
| 2673 | void next() { |
| 2674 | counter++; |
| 2675 | } |
| 2676 | }; |
| 2677 | |
| 2678 | DebugProcessDebugMessagesData process_debug_messages_data; |
| 2679 | |
| 2680 | static void DebugProcessDebugMessagesHandler( |
| 2681 | const uint16_t* message, |
| 2682 | int length, |
| 2683 | v8::Debug::ClientData* client_data) { |
| 2684 | |
| 2685 | const int kBufferSize = 100000; |
| 2686 | char print_buffer[kBufferSize]; |
| 2687 | Utf16ToAscii(message, length, print_buffer, kBufferSize); |
| 2688 | |
| 2689 | EvaluateResult* array_item = process_debug_messages_data.current(); |
| 2690 | |
| 2691 | bool res = GetEvaluateStringResult(print_buffer, |
| 2692 | array_item->buffer, |
| 2693 | EvaluateResult::kBufferSize); |
| 2694 | if (res) { |
| 2695 | process_debug_messages_data.next(); |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | // Test that the evaluation of expressions works even from ProcessDebugMessages |
| 2700 | // i.e. with empty stack. |
| 2701 | TEST(DebugEvaluateWithoutStack) { |
| 2702 | v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler); |
| 2703 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2704 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2705 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2706 | |
| 2707 | const char* source = |
| 2708 | "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }"; |
| 2709 | |
| 2710 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 2711 | |
| 2712 | v8::Debug::ProcessDebugMessages(); |
| 2713 | |
| 2714 | const int kBufferSize = 1000; |
| 2715 | uint16_t buffer[kBufferSize]; |
| 2716 | |
| 2717 | const char* command_111 = "{\"seq\":111," |
| 2718 | "\"type\":\"request\"," |
| 2719 | "\"command\":\"evaluate\"," |
| 2720 | "\"arguments\":{" |
| 2721 | " \"global\":true," |
| 2722 | " \"expression\":\"v1\",\"disable_break\":true" |
| 2723 | "}}"; |
| 2724 | |
| 2725 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_111, buffer)); |
| 2726 | |
| 2727 | const char* command_112 = "{\"seq\":112," |
| 2728 | "\"type\":\"request\"," |
| 2729 | "\"command\":\"evaluate\"," |
| 2730 | "\"arguments\":{" |
| 2731 | " \"global\":true," |
| 2732 | " \"expression\":\"getAnimal()\",\"disable_break\":true" |
| 2733 | "}}"; |
| 2734 | |
| 2735 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_112, buffer)); |
| 2736 | |
| 2737 | const char* command_113 = "{\"seq\":113," |
| 2738 | "\"type\":\"request\"," |
| 2739 | "\"command\":\"evaluate\"," |
| 2740 | "\"arguments\":{" |
| 2741 | " \"global\":true," |
| 2742 | " \"expression\":\"239 + 566\",\"disable_break\":true" |
| 2743 | "}}"; |
| 2744 | |
| 2745 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_113, buffer)); |
| 2746 | |
| 2747 | v8::Debug::ProcessDebugMessages(); |
| 2748 | |
| 2749 | CHECK_EQ(3, process_debug_messages_data.counter); |
| 2750 | |
| 2751 | CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0); |
| 2752 | CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer), |
| 2753 | 0); |
| 2754 | CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0); |
| 2755 | |
| 2756 | v8::Debug::SetMessageHandler(NULL); |
| 2757 | v8::Debug::SetDebugEventListener(NULL); |
| 2758 | CheckDebuggerUnloaded(); |
| 2759 | } |
| 2760 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2761 | |
| 2762 | // Simple test of the stepping mechanism using only store ICs. |
| 2763 | TEST(DebugStepLinear) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2764 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2765 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2766 | |
| 2767 | // Create a function for testing stepping. |
| 2768 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2769 | "function foo(){a=1;b=1;c=1;}", |
| 2770 | "foo"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2771 | |
| 2772 | // Run foo to allow it to get optimized. |
| 2773 | CompileRun("a=0; b=0; c=0; foo();"); |
| 2774 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2775 | SetBreakPoint(foo, 3); |
| 2776 | |
| 2777 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2778 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2779 | |
| 2780 | step_action = StepIn; |
| 2781 | break_point_hit_count = 0; |
| 2782 | foo->Call(env->Global(), 0, NULL); |
| 2783 | |
| 2784 | // With stepping all break locations are hit. |
| 2785 | CHECK_EQ(4, break_point_hit_count); |
| 2786 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2787 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2788 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2789 | |
| 2790 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2791 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2792 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2793 | SetBreakPoint(foo, 3); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2794 | break_point_hit_count = 0; |
| 2795 | foo->Call(env->Global(), 0, NULL); |
| 2796 | |
| 2797 | // Without stepping only active break points are hit. |
| 2798 | CHECK_EQ(1, break_point_hit_count); |
| 2799 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2800 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2801 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2805 | // Test of the stepping mechanism for keyed load in a loop. |
| 2806 | TEST(DebugStepKeyedLoadLoop) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2807 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2808 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2809 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2810 | // Register a debug event listener which steps and counts. |
| 2811 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2812 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2813 | // Create a function for testing stepping of keyed load. The statement 'y=1' |
| 2814 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2815 | v8::Local<v8::Function> foo = CompileFunction( |
| 2816 | &env, |
| 2817 | "function foo(a) {\n" |
| 2818 | " var x;\n" |
| 2819 | " var len = a.length;\n" |
| 2820 | " for (var i = 0; i < len; i++) {\n" |
| 2821 | " y = 1;\n" |
| 2822 | " x = a[i];\n" |
| 2823 | " }\n" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2824 | "}\n" |
| 2825 | "y=0\n", |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2826 | "foo"); |
| 2827 | |
| 2828 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2829 | v8::Local<v8::Array> a = v8::Array::New(10); |
| 2830 | for (int i = 0; i < 10; i++) { |
| 2831 | a->Set(v8::Number::New(i), v8::Number::New(i)); |
| 2832 | } |
| 2833 | |
| 2834 | // Call function without any break points to ensure inlining is in place. |
| 2835 | const int kArgc = 1; |
| 2836 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2837 | foo->Call(env->Global(), kArgc, args); |
| 2838 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2839 | // Set up break point and step through the function. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2840 | SetBreakPoint(foo, 3); |
| 2841 | step_action = StepNext; |
| 2842 | break_point_hit_count = 0; |
| 2843 | foo->Call(env->Global(), kArgc, args); |
| 2844 | |
| 2845 | // With stepping all break locations are hit. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 2846 | CHECK_EQ(34, break_point_hit_count); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2847 | |
| 2848 | v8::Debug::SetDebugEventListener(NULL); |
| 2849 | CheckDebuggerUnloaded(); |
| 2850 | } |
| 2851 | |
| 2852 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2853 | // Test of the stepping mechanism for keyed store in a loop. |
| 2854 | TEST(DebugStepKeyedStoreLoop) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2855 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2856 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2857 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2858 | // Register a debug event listener which steps and counts. |
| 2859 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2860 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2861 | // Create a function for testing stepping of keyed store. The statement 'y=1' |
| 2862 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2863 | v8::Local<v8::Function> foo = CompileFunction( |
| 2864 | &env, |
| 2865 | "function foo(a) {\n" |
| 2866 | " var len = a.length;\n" |
| 2867 | " for (var i = 0; i < len; i++) {\n" |
| 2868 | " y = 1;\n" |
| 2869 | " a[i] = 42;\n" |
| 2870 | " }\n" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2871 | "}\n" |
| 2872 | "y=0\n", |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2873 | "foo"); |
| 2874 | |
| 2875 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2876 | v8::Local<v8::Array> a = v8::Array::New(10); |
| 2877 | for (int i = 0; i < 10; i++) { |
| 2878 | a->Set(v8::Number::New(i), v8::Number::New(i)); |
| 2879 | } |
| 2880 | |
| 2881 | // Call function without any break points to ensure inlining is in place. |
| 2882 | const int kArgc = 1; |
| 2883 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2884 | foo->Call(env->Global(), kArgc, args); |
| 2885 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2886 | // Set up break point and step through the function. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2887 | SetBreakPoint(foo, 3); |
| 2888 | step_action = StepNext; |
| 2889 | break_point_hit_count = 0; |
| 2890 | foo->Call(env->Global(), kArgc, args); |
| 2891 | |
| 2892 | // With stepping all break locations are hit. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 2893 | CHECK_EQ(33, break_point_hit_count); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2894 | |
| 2895 | v8::Debug::SetDebugEventListener(NULL); |
| 2896 | CheckDebuggerUnloaded(); |
| 2897 | } |
| 2898 | |
| 2899 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2900 | // Test of the stepping mechanism for named load in a loop. |
| 2901 | TEST(DebugStepNamedLoadLoop) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2902 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2903 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2904 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2905 | // Register a debug event listener which steps and counts. |
| 2906 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2907 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2908 | // Create a function for testing stepping of named load. |
| 2909 | v8::Local<v8::Function> foo = CompileFunction( |
| 2910 | &env, |
| 2911 | "function foo() {\n" |
| 2912 | " var a = [];\n" |
| 2913 | " var s = \"\";\n" |
| 2914 | " for (var i = 0; i < 10; i++) {\n" |
| 2915 | " var v = new V(i, i + 1);\n" |
| 2916 | " v.y;\n" |
| 2917 | " a.length;\n" // Special case: array length. |
| 2918 | " s.length;\n" // Special case: string length. |
| 2919 | " }\n" |
| 2920 | "}\n" |
| 2921 | "function V(x, y) {\n" |
| 2922 | " this.x = x;\n" |
| 2923 | " this.y = y;\n" |
| 2924 | "}\n", |
| 2925 | "foo"); |
| 2926 | |
| 2927 | // Call function without any break points to ensure inlining is in place. |
| 2928 | foo->Call(env->Global(), 0, NULL); |
| 2929 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2930 | // Set up break point and step through the function. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2931 | SetBreakPoint(foo, 4); |
| 2932 | step_action = StepNext; |
| 2933 | break_point_hit_count = 0; |
| 2934 | foo->Call(env->Global(), 0, NULL); |
| 2935 | |
| 2936 | // With stepping all break locations are hit. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 2937 | CHECK_EQ(54, break_point_hit_count); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2938 | |
| 2939 | v8::Debug::SetDebugEventListener(NULL); |
| 2940 | CheckDebuggerUnloaded(); |
| 2941 | } |
| 2942 | |
| 2943 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2944 | static void DoDebugStepNamedStoreLoop(int expected) { |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2945 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2946 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2947 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2948 | // Register a debug event listener which steps and counts. |
| 2949 | v8::Debug::SetDebugEventListener(DebugEventStep); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Create a function for testing stepping of named store. |
| 2952 | v8::Local<v8::Function> foo = CompileFunction( |
| 2953 | &env, |
| 2954 | "function foo() {\n" |
| 2955 | " var a = {a:1};\n" |
| 2956 | " for (var i = 0; i < 10; i++) {\n" |
| 2957 | " a.a = 2\n" |
| 2958 | " }\n" |
| 2959 | "}\n", |
| 2960 | "foo"); |
| 2961 | |
| 2962 | // Call function without any break points to ensure inlining is in place. |
| 2963 | foo->Call(env->Global(), 0, NULL); |
| 2964 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2965 | // Set up break point and step through the function. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2966 | SetBreakPoint(foo, 3); |
| 2967 | step_action = StepNext; |
| 2968 | break_point_hit_count = 0; |
| 2969 | foo->Call(env->Global(), 0, NULL); |
| 2970 | |
| 2971 | // With stepping all expected break locations are hit. |
| 2972 | CHECK_EQ(expected, break_point_hit_count); |
| 2973 | |
| 2974 | v8::Debug::SetDebugEventListener(NULL); |
| 2975 | CheckDebuggerUnloaded(); |
| 2976 | } |
| 2977 | |
| 2978 | |
| 2979 | // Test of the stepping mechanism for named load in a loop. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2980 | TEST(DebugStepNamedStoreLoop) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 2981 | DoDebugStepNamedStoreLoop(23); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2985 | // Test the stepping mechanism with different ICs. |
| 2986 | TEST(DebugStepLinearMixedICs) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2987 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2988 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2989 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2990 | // Register a debug event listener which steps and counts. |
| 2991 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2992 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2993 | // Create a function for testing stepping. |
| 2994 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2995 | "function bar() {};" |
| 2996 | "function foo() {" |
| 2997 | " var x;" |
| 2998 | " var index='name';" |
| 2999 | " var y = {};" |
| 3000 | " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3001 | |
| 3002 | // Run functions to allow them to get optimized. |
| 3003 | CompileRun("a=0; b=0; bar(); foo();"); |
| 3004 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3005 | SetBreakPoint(foo, 0); |
| 3006 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3007 | step_action = StepIn; |
| 3008 | break_point_hit_count = 0; |
| 3009 | foo->Call(env->Global(), 0, NULL); |
| 3010 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 3011 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3012 | CHECK_EQ(11, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3013 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3014 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3015 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3016 | |
| 3017 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3018 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3019 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3020 | SetBreakPoint(foo, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3021 | break_point_hit_count = 0; |
| 3022 | foo->Call(env->Global(), 0, NULL); |
| 3023 | |
| 3024 | // Without stepping only active break points are hit. |
| 3025 | CHECK_EQ(1, break_point_hit_count); |
| 3026 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3027 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3028 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3032 | TEST(DebugStepDeclarations) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3033 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3034 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3035 | |
| 3036 | // Register a debug event listener which steps and counts. |
| 3037 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3038 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3039 | // Create a function for testing stepping. Run it to allow it to get |
| 3040 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3041 | const char* src = "function foo() { " |
| 3042 | " var a;" |
| 3043 | " var b = 1;" |
| 3044 | " var c = foo;" |
| 3045 | " var d = Math.floor;" |
| 3046 | " var e = b + d(1.2);" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3047 | "}" |
| 3048 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3049 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3050 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3051 | SetBreakPoint(foo, 0); |
| 3052 | |
| 3053 | // Stepping through the declarations. |
| 3054 | step_action = StepIn; |
| 3055 | break_point_hit_count = 0; |
| 3056 | foo->Call(env->Global(), 0, NULL); |
| 3057 | CHECK_EQ(6, break_point_hit_count); |
| 3058 | |
| 3059 | // Get rid of the debug event listener. |
| 3060 | v8::Debug::SetDebugEventListener(NULL); |
| 3061 | CheckDebuggerUnloaded(); |
| 3062 | } |
| 3063 | |
| 3064 | |
| 3065 | TEST(DebugStepLocals) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3066 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3067 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3068 | |
| 3069 | // Register a debug event listener which steps and counts. |
| 3070 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3071 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3072 | // Create a function for testing stepping. Run it to allow it to get |
| 3073 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3074 | const char* src = "function foo() { " |
| 3075 | " var a,b;" |
| 3076 | " a = 1;" |
| 3077 | " b = a + 2;" |
| 3078 | " b = 1 + 2 + 3;" |
| 3079 | " a = Math.floor(b);" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3080 | "}" |
| 3081 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3082 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3083 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3084 | SetBreakPoint(foo, 0); |
| 3085 | |
| 3086 | // Stepping through the declarations. |
| 3087 | step_action = StepIn; |
| 3088 | break_point_hit_count = 0; |
| 3089 | foo->Call(env->Global(), 0, NULL); |
| 3090 | CHECK_EQ(6, break_point_hit_count); |
| 3091 | |
| 3092 | // Get rid of the debug event listener. |
| 3093 | v8::Debug::SetDebugEventListener(NULL); |
| 3094 | CheckDebuggerUnloaded(); |
| 3095 | } |
| 3096 | |
| 3097 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3098 | TEST(DebugStepIf) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3099 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3100 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3101 | |
| 3102 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3103 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3104 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3105 | // Create a function for testing stepping. Run it to allow it to get |
| 3106 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3107 | const int argc = 1; |
| 3108 | const char* src = "function foo(x) { " |
| 3109 | " a = 1;" |
| 3110 | " if (x) {" |
| 3111 | " b = 1;" |
| 3112 | " } else {" |
| 3113 | " c = 1;" |
| 3114 | " d = 1;" |
| 3115 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3116 | "}" |
| 3117 | "a=0; b=0; c=0; d=0; foo()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3118 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3119 | SetBreakPoint(foo, 0); |
| 3120 | |
| 3121 | // Stepping through the true part. |
| 3122 | step_action = StepIn; |
| 3123 | break_point_hit_count = 0; |
| 3124 | v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; |
| 3125 | foo->Call(env->Global(), argc, argv_true); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3126 | CHECK_EQ(4, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Stepping through the false part. |
| 3129 | step_action = StepIn; |
| 3130 | break_point_hit_count = 0; |
| 3131 | v8::Handle<v8::Value> argv_false[argc] = { v8::False() }; |
| 3132 | foo->Call(env->Global(), argc, argv_false); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3133 | CHECK_EQ(5, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3134 | |
| 3135 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3136 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3137 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | |
| 3141 | TEST(DebugStepSwitch) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3142 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3143 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3144 | |
| 3145 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3146 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3147 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3148 | // Create a function for testing stepping. Run it to allow it to get |
| 3149 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3150 | const int argc = 1; |
| 3151 | const char* src = "function foo(x) { " |
| 3152 | " a = 1;" |
| 3153 | " switch (x) {" |
| 3154 | " case 1:" |
| 3155 | " b = 1;" |
| 3156 | " case 2:" |
| 3157 | " c = 1;" |
| 3158 | " break;" |
| 3159 | " case 3:" |
| 3160 | " d = 1;" |
| 3161 | " e = 1;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3162 | " f = 1;" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3163 | " break;" |
| 3164 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3165 | "}" |
| 3166 | "a=0; b=0; c=0; d=0; e=0; f=0; foo()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3167 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3168 | SetBreakPoint(foo, 0); |
| 3169 | |
| 3170 | // One case with fall-through. |
| 3171 | step_action = StepIn; |
| 3172 | break_point_hit_count = 0; |
| 3173 | v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) }; |
| 3174 | foo->Call(env->Global(), argc, argv_1); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3175 | CHECK_EQ(6, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3176 | |
| 3177 | // Another case. |
| 3178 | step_action = StepIn; |
| 3179 | break_point_hit_count = 0; |
| 3180 | v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) }; |
| 3181 | foo->Call(env->Global(), argc, argv_2); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3182 | CHECK_EQ(5, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3183 | |
| 3184 | // Last case. |
| 3185 | step_action = StepIn; |
| 3186 | break_point_hit_count = 0; |
| 3187 | v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) }; |
| 3188 | foo->Call(env->Global(), argc, argv_3); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3189 | CHECK_EQ(7, break_point_hit_count); |
| 3190 | |
| 3191 | // Get rid of the debug event listener. |
| 3192 | v8::Debug::SetDebugEventListener(NULL); |
| 3193 | CheckDebuggerUnloaded(); |
| 3194 | } |
| 3195 | |
| 3196 | |
| 3197 | TEST(DebugStepWhile) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3198 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3199 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3200 | |
| 3201 | // Register a debug event listener which steps and counts. |
| 3202 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3203 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3204 | // Create a function for testing stepping. Run it to allow it to get |
| 3205 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3206 | const int argc = 1; |
| 3207 | const char* src = "function foo(x) { " |
| 3208 | " var a = 0;" |
| 3209 | " while (a < x) {" |
| 3210 | " a++;" |
| 3211 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3212 | "}" |
| 3213 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3214 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3215 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3216 | |
| 3217 | // Looping 10 times. |
| 3218 | step_action = StepIn; |
| 3219 | break_point_hit_count = 0; |
| 3220 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3221 | foo->Call(env->Global(), argc, argv_10); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3222 | CHECK_EQ(22, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3223 | |
| 3224 | // Looping 100 times. |
| 3225 | step_action = StepIn; |
| 3226 | break_point_hit_count = 0; |
| 3227 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3228 | foo->Call(env->Global(), argc, argv_100); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3229 | CHECK_EQ(202, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3230 | |
| 3231 | // Get rid of the debug event listener. |
| 3232 | v8::Debug::SetDebugEventListener(NULL); |
| 3233 | CheckDebuggerUnloaded(); |
| 3234 | } |
| 3235 | |
| 3236 | |
| 3237 | TEST(DebugStepDoWhile) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3238 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3239 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3240 | |
| 3241 | // Register a debug event listener which steps and counts. |
| 3242 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3243 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3244 | // Create a function for testing stepping. Run it to allow it to get |
| 3245 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3246 | const int argc = 1; |
| 3247 | const char* src = "function foo(x) { " |
| 3248 | " var a = 0;" |
| 3249 | " do {" |
| 3250 | " a++;" |
| 3251 | " } while (a < x)" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3252 | "}" |
| 3253 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3254 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3255 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3256 | |
| 3257 | // Looping 10 times. |
| 3258 | step_action = StepIn; |
| 3259 | break_point_hit_count = 0; |
| 3260 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3261 | foo->Call(env->Global(), argc, argv_10); |
| 3262 | CHECK_EQ(22, break_point_hit_count); |
| 3263 | |
| 3264 | // Looping 100 times. |
| 3265 | step_action = StepIn; |
| 3266 | break_point_hit_count = 0; |
| 3267 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3268 | foo->Call(env->Global(), argc, argv_100); |
| 3269 | CHECK_EQ(202, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3272 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3273 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | |
| 3277 | TEST(DebugStepFor) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3278 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3279 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3280 | |
| 3281 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3282 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3283 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3284 | // Create a function for testing stepping. Run it to allow it to get |
| 3285 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3286 | const int argc = 1; |
| 3287 | const char* src = "function foo(x) { " |
| 3288 | " a = 1;" |
| 3289 | " for (i = 0; i < x; i++) {" |
| 3290 | " b = 1;" |
| 3291 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3292 | "}" |
| 3293 | "a=0; b=0; i=0; foo()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3294 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3295 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3296 | SetBreakPoint(foo, 8); // "a = 1;" |
| 3297 | |
| 3298 | // Looping 10 times. |
| 3299 | step_action = StepIn; |
| 3300 | break_point_hit_count = 0; |
| 3301 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3302 | foo->Call(env->Global(), argc, argv_10); |
| 3303 | CHECK_EQ(23, break_point_hit_count); |
| 3304 | |
| 3305 | // Looping 100 times. |
| 3306 | step_action = StepIn; |
| 3307 | break_point_hit_count = 0; |
| 3308 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3309 | foo->Call(env->Global(), argc, argv_100); |
| 3310 | CHECK_EQ(203, break_point_hit_count); |
| 3311 | |
| 3312 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3313 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3314 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3318 | TEST(DebugStepForContinue) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3319 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3320 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3321 | |
| 3322 | // Register a debug event listener which steps and counts. |
| 3323 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3324 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3325 | // Create a function for testing stepping. Run it to allow it to get |
| 3326 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3327 | const int argc = 1; |
| 3328 | const char* src = "function foo(x) { " |
| 3329 | " var a = 0;" |
| 3330 | " var b = 0;" |
| 3331 | " var c = 0;" |
| 3332 | " for (var i = 0; i < x; i++) {" |
| 3333 | " a++;" |
| 3334 | " if (a % 2 == 0) continue;" |
| 3335 | " b++;" |
| 3336 | " c++;" |
| 3337 | " }" |
| 3338 | " return b;" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3339 | "}" |
| 3340 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3341 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3342 | v8::Handle<v8::Value> result; |
| 3343 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3344 | |
| 3345 | // Each loop generates 4 or 5 steps depending on whether a is equal. |
| 3346 | |
| 3347 | // Looping 10 times. |
| 3348 | step_action = StepIn; |
| 3349 | break_point_hit_count = 0; |
| 3350 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3351 | result = foo->Call(env->Global(), argc, argv_10); |
| 3352 | CHECK_EQ(5, result->Int32Value()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3353 | CHECK_EQ(51, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3354 | |
| 3355 | // Looping 100 times. |
| 3356 | step_action = StepIn; |
| 3357 | break_point_hit_count = 0; |
| 3358 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3359 | result = foo->Call(env->Global(), argc, argv_100); |
| 3360 | CHECK_EQ(50, result->Int32Value()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3361 | CHECK_EQ(456, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3362 | |
| 3363 | // Get rid of the debug event listener. |
| 3364 | v8::Debug::SetDebugEventListener(NULL); |
| 3365 | CheckDebuggerUnloaded(); |
| 3366 | } |
| 3367 | |
| 3368 | |
| 3369 | TEST(DebugStepForBreak) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3370 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3371 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3372 | |
| 3373 | // Register a debug event listener which steps and counts. |
| 3374 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3375 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3376 | // Create a function for testing stepping. Run it to allow it to get |
| 3377 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3378 | const int argc = 1; |
| 3379 | const char* src = "function foo(x) { " |
| 3380 | " var a = 0;" |
| 3381 | " var b = 0;" |
| 3382 | " var c = 0;" |
| 3383 | " for (var i = 0; i < 1000; i++) {" |
| 3384 | " a++;" |
| 3385 | " if (a == x) break;" |
| 3386 | " b++;" |
| 3387 | " c++;" |
| 3388 | " }" |
| 3389 | " return b;" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3390 | "}" |
| 3391 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3392 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3393 | v8::Handle<v8::Value> result; |
| 3394 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3395 | |
| 3396 | // Each loop generates 5 steps except for the last (when break is executed) |
| 3397 | // which only generates 4. |
| 3398 | |
| 3399 | // Looping 10 times. |
| 3400 | step_action = StepIn; |
| 3401 | break_point_hit_count = 0; |
| 3402 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3403 | result = foo->Call(env->Global(), argc, argv_10); |
| 3404 | CHECK_EQ(9, result->Int32Value()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3405 | CHECK_EQ(54, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3406 | |
| 3407 | // Looping 100 times. |
| 3408 | step_action = StepIn; |
| 3409 | break_point_hit_count = 0; |
| 3410 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3411 | result = foo->Call(env->Global(), argc, argv_100); |
| 3412 | CHECK_EQ(99, result->Int32Value()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3413 | CHECK_EQ(504, break_point_hit_count); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3414 | |
| 3415 | // Get rid of the debug event listener. |
| 3416 | v8::Debug::SetDebugEventListener(NULL); |
| 3417 | CheckDebuggerUnloaded(); |
| 3418 | } |
| 3419 | |
| 3420 | |
| 3421 | TEST(DebugStepForIn) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3422 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3423 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3424 | |
| 3425 | // Register a debug event listener which steps and counts. |
| 3426 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3427 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3428 | // Create a function for testing stepping. Run it to allow it to get |
| 3429 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3430 | v8::Local<v8::Function> foo; |
| 3431 | const char* src_1 = "function foo() { " |
| 3432 | " var a = [1, 2];" |
| 3433 | " for (x in a) {" |
| 3434 | " b = 0;" |
| 3435 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3436 | "}" |
| 3437 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3438 | foo = CompileFunction(&env, src_1, "foo"); |
| 3439 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3440 | |
| 3441 | step_action = StepIn; |
| 3442 | break_point_hit_count = 0; |
| 3443 | foo->Call(env->Global(), 0, NULL); |
| 3444 | CHECK_EQ(6, break_point_hit_count); |
| 3445 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3446 | // Create a function for testing stepping. Run it to allow it to get |
| 3447 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3448 | const char* src_2 = "function foo() { " |
| 3449 | " var a = {a:[1, 2, 3]};" |
| 3450 | " for (x in a.a) {" |
| 3451 | " b = 0;" |
| 3452 | " }" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3453 | "}" |
| 3454 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3455 | foo = CompileFunction(&env, src_2, "foo"); |
| 3456 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3457 | |
| 3458 | step_action = StepIn; |
| 3459 | break_point_hit_count = 0; |
| 3460 | foo->Call(env->Global(), 0, NULL); |
| 3461 | CHECK_EQ(8, break_point_hit_count); |
| 3462 | |
| 3463 | // Get rid of the debug event listener. |
| 3464 | v8::Debug::SetDebugEventListener(NULL); |
| 3465 | CheckDebuggerUnloaded(); |
| 3466 | } |
| 3467 | |
| 3468 | |
| 3469 | TEST(DebugStepWith) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3470 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3471 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3472 | |
| 3473 | // Register a debug event listener which steps and counts. |
| 3474 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3475 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3476 | // Create a function for testing stepping. Run it to allow it to get |
| 3477 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3478 | const char* src = "function foo(x) { " |
| 3479 | " var a = {};" |
| 3480 | " with (a) {}" |
| 3481 | " with (b) {}" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3482 | "}" |
| 3483 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3484 | env->Global()->Set(v8::String::New("b"), v8::Object::New()); |
| 3485 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3486 | v8::Handle<v8::Value> result; |
| 3487 | SetBreakPoint(foo, 8); // "var a = {};" |
| 3488 | |
| 3489 | step_action = StepIn; |
| 3490 | break_point_hit_count = 0; |
| 3491 | foo->Call(env->Global(), 0, NULL); |
| 3492 | CHECK_EQ(4, break_point_hit_count); |
| 3493 | |
| 3494 | // Get rid of the debug event listener. |
| 3495 | v8::Debug::SetDebugEventListener(NULL); |
| 3496 | CheckDebuggerUnloaded(); |
| 3497 | } |
| 3498 | |
| 3499 | |
| 3500 | TEST(DebugConditional) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3501 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3502 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3503 | |
| 3504 | // Register a debug event listener which steps and counts. |
| 3505 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3506 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3507 | // Create a function for testing stepping. Run it to allow it to get |
| 3508 | // optimized. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3509 | const char* src = "function foo(x) { " |
| 3510 | " var a;" |
| 3511 | " a = x ? 1 : 2;" |
| 3512 | " return a;" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3513 | "}" |
| 3514 | "foo()"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3515 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3516 | SetBreakPoint(foo, 0); // "var a;" |
| 3517 | |
| 3518 | step_action = StepIn; |
| 3519 | break_point_hit_count = 0; |
| 3520 | foo->Call(env->Global(), 0, NULL); |
| 3521 | CHECK_EQ(5, break_point_hit_count); |
| 3522 | |
| 3523 | step_action = StepIn; |
| 3524 | break_point_hit_count = 0; |
| 3525 | const int argc = 1; |
| 3526 | v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; |
| 3527 | foo->Call(env->Global(), argc, argv_true); |
| 3528 | CHECK_EQ(5, break_point_hit_count); |
| 3529 | |
| 3530 | // Get rid of the debug event listener. |
| 3531 | v8::Debug::SetDebugEventListener(NULL); |
| 3532 | CheckDebuggerUnloaded(); |
| 3533 | } |
| 3534 | |
| 3535 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3536 | TEST(StepInOutSimple) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3537 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3538 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3539 | |
| 3540 | // Create a function for checking the function when hitting a break point. |
| 3541 | frame_function_name = CompileFunction(&env, |
| 3542 | frame_function_name_source, |
| 3543 | "frame_function_name"); |
| 3544 | |
| 3545 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3546 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3547 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3548 | // Create a function for testing stepping. Run it to allow it to get |
| 3549 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3550 | const char* src = "function a() {b();c();}; " |
| 3551 | "function b() {c();}; " |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3552 | "function c() {}; " |
| 3553 | "a(); b(); c()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3554 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3555 | SetBreakPoint(a, 0); |
| 3556 | |
| 3557 | // Step through invocation of a with step in. |
| 3558 | step_action = StepIn; |
| 3559 | break_point_hit_count = 0; |
| 3560 | expected_step_sequence = "abcbaca"; |
| 3561 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3562 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3563 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3564 | |
| 3565 | // Step through invocation of a with step next. |
| 3566 | step_action = StepNext; |
| 3567 | break_point_hit_count = 0; |
| 3568 | expected_step_sequence = "aaa"; |
| 3569 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3570 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3571 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Step through invocation of a with step out. |
| 3574 | step_action = StepOut; |
| 3575 | break_point_hit_count = 0; |
| 3576 | expected_step_sequence = "a"; |
| 3577 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3578 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3579 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3580 | |
| 3581 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3582 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3583 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | TEST(StepInOutTree) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3588 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3589 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3590 | |
| 3591 | // Create a function for checking the function when hitting a break point. |
| 3592 | frame_function_name = CompileFunction(&env, |
| 3593 | frame_function_name_source, |
| 3594 | "frame_function_name"); |
| 3595 | |
| 3596 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3597 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3598 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3599 | // Create a function for testing stepping. Run it to allow it to get |
| 3600 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3601 | const char* src = "function a() {b(c(d()),d());c(d());d()}; " |
| 3602 | "function b(x,y) {c();}; " |
| 3603 | "function c(x) {}; " |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3604 | "function d() {}; " |
| 3605 | "a(); b(); c(); d()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3606 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3607 | SetBreakPoint(a, 0); |
| 3608 | |
| 3609 | // Step through invocation of a with step in. |
| 3610 | step_action = StepIn; |
| 3611 | break_point_hit_count = 0; |
| 3612 | expected_step_sequence = "adacadabcbadacada"; |
| 3613 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3614 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3615 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3616 | |
| 3617 | // Step through invocation of a with step next. |
| 3618 | step_action = StepNext; |
| 3619 | break_point_hit_count = 0; |
| 3620 | expected_step_sequence = "aaaa"; |
| 3621 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3622 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3623 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3624 | |
| 3625 | // Step through invocation of a with step out. |
| 3626 | step_action = StepOut; |
| 3627 | break_point_hit_count = 0; |
| 3628 | expected_step_sequence = "a"; |
| 3629 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3630 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3631 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3632 | |
| 3633 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3634 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3635 | CheckDebuggerUnloaded(true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | TEST(StepInOutBranch) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3640 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3641 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3642 | |
| 3643 | // Create a function for checking the function when hitting a break point. |
| 3644 | frame_function_name = CompileFunction(&env, |
| 3645 | frame_function_name_source, |
| 3646 | "frame_function_name"); |
| 3647 | |
| 3648 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3649 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3650 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3651 | // Create a function for testing stepping. Run it to allow it to get |
| 3652 | // optimized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3653 | const char* src = "function a() {b(false);c();}; " |
| 3654 | "function b(x) {if(x){c();};}; " |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3655 | "function c() {}; " |
| 3656 | "a(); b(); c()"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3657 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3658 | SetBreakPoint(a, 0); |
| 3659 | |
| 3660 | // Step through invocation of a. |
| 3661 | step_action = StepIn; |
| 3662 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3663 | expected_step_sequence = "abbaca"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3664 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3665 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3666 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3667 | |
| 3668 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3669 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3670 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | |
| 3674 | // Test that step in does not step into native functions. |
| 3675 | TEST(DebugStepNatives) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3676 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3677 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3678 | |
| 3679 | // Create a function for testing stepping. |
| 3680 | v8::Local<v8::Function> foo = CompileFunction( |
| 3681 | &env, |
| 3682 | "function foo(){debugger;Math.sin(1);}", |
| 3683 | "foo"); |
| 3684 | |
| 3685 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3686 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3687 | |
| 3688 | step_action = StepIn; |
| 3689 | break_point_hit_count = 0; |
| 3690 | foo->Call(env->Global(), 0, NULL); |
| 3691 | |
| 3692 | // With stepping all break locations are hit. |
| 3693 | CHECK_EQ(3, break_point_hit_count); |
| 3694 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3695 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3696 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3697 | |
| 3698 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3699 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3700 | |
| 3701 | break_point_hit_count = 0; |
| 3702 | foo->Call(env->Global(), 0, NULL); |
| 3703 | |
| 3704 | // Without stepping only active break points are hit. |
| 3705 | CHECK_EQ(1, break_point_hit_count); |
| 3706 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3707 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3708 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3712 | // Test that step in works with function.apply. |
| 3713 | TEST(DebugStepFunctionApply) { |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3714 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3715 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3716 | |
| 3717 | // Create a function for testing stepping. |
| 3718 | v8::Local<v8::Function> foo = CompileFunction( |
| 3719 | &env, |
| 3720 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3721 | "function foo(){ debugger; bar.apply(this, [1,2,3]); }", |
| 3722 | "foo"); |
| 3723 | |
| 3724 | // Register a debug event listener which steps and counts. |
| 3725 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3726 | |
| 3727 | step_action = StepIn; |
| 3728 | break_point_hit_count = 0; |
| 3729 | foo->Call(env->Global(), 0, NULL); |
| 3730 | |
| 3731 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3732 | CHECK_EQ(7, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3733 | |
| 3734 | v8::Debug::SetDebugEventListener(NULL); |
| 3735 | CheckDebuggerUnloaded(); |
| 3736 | |
| 3737 | // Register a debug event listener which just counts. |
| 3738 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3739 | |
| 3740 | break_point_hit_count = 0; |
| 3741 | foo->Call(env->Global(), 0, NULL); |
| 3742 | |
| 3743 | // Without stepping only the debugger statement is hit. |
| 3744 | CHECK_EQ(1, break_point_hit_count); |
| 3745 | |
| 3746 | v8::Debug::SetDebugEventListener(NULL); |
| 3747 | CheckDebuggerUnloaded(); |
| 3748 | } |
| 3749 | |
| 3750 | |
| 3751 | // Test that step in works with function.call. |
| 3752 | TEST(DebugStepFunctionCall) { |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3753 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3754 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3755 | |
| 3756 | // Create a function for testing stepping. |
| 3757 | v8::Local<v8::Function> foo = CompileFunction( |
| 3758 | &env, |
| 3759 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3760 | "function foo(a){ debugger;" |
| 3761 | " if (a) {" |
| 3762 | " bar.call(this, 1, 2, 3);" |
| 3763 | " } else {" |
| 3764 | " bar.call(this, 0);" |
| 3765 | " }" |
| 3766 | "}", |
| 3767 | "foo"); |
| 3768 | |
| 3769 | // Register a debug event listener which steps and counts. |
| 3770 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3771 | step_action = StepIn; |
| 3772 | |
| 3773 | // Check stepping where the if condition in bar is false. |
| 3774 | break_point_hit_count = 0; |
| 3775 | foo->Call(env->Global(), 0, NULL); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3776 | CHECK_EQ(6, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3777 | |
| 3778 | // Check stepping where the if condition in bar is true. |
| 3779 | break_point_hit_count = 0; |
| 3780 | const int argc = 1; |
| 3781 | v8::Handle<v8::Value> argv[argc] = { v8::True() }; |
| 3782 | foo->Call(env->Global(), argc, argv); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3783 | CHECK_EQ(8, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3784 | |
| 3785 | v8::Debug::SetDebugEventListener(NULL); |
| 3786 | CheckDebuggerUnloaded(); |
| 3787 | |
| 3788 | // Register a debug event listener which just counts. |
| 3789 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3790 | |
| 3791 | break_point_hit_count = 0; |
| 3792 | foo->Call(env->Global(), 0, NULL); |
| 3793 | |
| 3794 | // Without stepping only the debugger statement is hit. |
| 3795 | CHECK_EQ(1, break_point_hit_count); |
| 3796 | |
| 3797 | v8::Debug::SetDebugEventListener(NULL); |
| 3798 | CheckDebuggerUnloaded(); |
| 3799 | } |
| 3800 | |
| 3801 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3802 | // Tests that breakpoint will be hit if it's set in script. |
| 3803 | TEST(PauseInScript) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3804 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3805 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3806 | env.ExposeDebug(); |
| 3807 | |
| 3808 | // Register a debug event listener which counts. |
| 3809 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 3810 | |
| 3811 | // Create a script that returns a function. |
| 3812 | const char* src = "(function (evt) {})"; |
| 3813 | const char* script_name = "StepInHandlerTest"; |
| 3814 | |
| 3815 | // Set breakpoint in the script. |
| 3816 | SetScriptBreakPointByNameFromJS(script_name, 0, -1); |
| 3817 | break_point_hit_count = 0; |
| 3818 | |
| 3819 | v8::ScriptOrigin origin(v8::String::New(script_name), v8::Integer::New(0)); |
| 3820 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(src), |
| 3821 | &origin); |
| 3822 | v8::Local<v8::Value> r = script->Run(); |
| 3823 | |
| 3824 | CHECK(r->IsFunction()); |
| 3825 | CHECK_EQ(1, break_point_hit_count); |
| 3826 | |
| 3827 | // Get rid of the debug event listener. |
| 3828 | v8::Debug::SetDebugEventListener(NULL); |
| 3829 | CheckDebuggerUnloaded(); |
| 3830 | } |
| 3831 | |
| 3832 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3833 | // Test break on exceptions. For each exception break combination the number |
| 3834 | // of debug event exception callbacks and message callbacks are collected. The |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3835 | // number of debug event exception callbacks are used to check that the |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3836 | // debugger is called correctly and the number of message callbacks is used to |
| 3837 | // check that uncaught exceptions are still returned even if there is a break |
| 3838 | // for them. |
| 3839 | TEST(BreakOnException) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3840 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3841 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3842 | env.ExposeDebug(); |
| 3843 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3844 | v8::internal::Isolate::Current()->TraceException(false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3845 | |
| 3846 | // Create functions for testing break on exception. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 3847 | CompileFunction(&env, "function throws(){throw 1;}", "throws"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3848 | v8::Local<v8::Function> caught = |
| 3849 | CompileFunction(&env, |
| 3850 | "function caught(){try {throws();} catch(e) {};}", |
| 3851 | "caught"); |
| 3852 | v8::Local<v8::Function> notCaught = |
| 3853 | CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); |
| 3854 | |
| 3855 | v8::V8::AddMessageListener(MessageCallbackCount); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3856 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3857 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3858 | // Initial state should be no break on exceptions. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3859 | DebugEventCounterClear(); |
| 3860 | MessageCallbackCountClear(); |
| 3861 | caught->Call(env->Global(), 0, NULL); |
| 3862 | CHECK_EQ(0, exception_hit_count); |
| 3863 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3864 | CHECK_EQ(0, message_callback_count); |
| 3865 | notCaught->Call(env->Global(), 0, NULL); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3866 | CHECK_EQ(0, exception_hit_count); |
| 3867 | CHECK_EQ(0, uncaught_exception_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3868 | CHECK_EQ(1, message_callback_count); |
| 3869 | |
| 3870 | // No break on exception |
| 3871 | DebugEventCounterClear(); |
| 3872 | MessageCallbackCountClear(); |
| 3873 | ChangeBreakOnException(false, false); |
| 3874 | caught->Call(env->Global(), 0, NULL); |
| 3875 | CHECK_EQ(0, exception_hit_count); |
| 3876 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3877 | CHECK_EQ(0, message_callback_count); |
| 3878 | notCaught->Call(env->Global(), 0, NULL); |
| 3879 | CHECK_EQ(0, exception_hit_count); |
| 3880 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3881 | CHECK_EQ(1, message_callback_count); |
| 3882 | |
| 3883 | // Break on uncaught exception |
| 3884 | DebugEventCounterClear(); |
| 3885 | MessageCallbackCountClear(); |
| 3886 | ChangeBreakOnException(false, true); |
| 3887 | caught->Call(env->Global(), 0, NULL); |
| 3888 | CHECK_EQ(0, exception_hit_count); |
| 3889 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3890 | CHECK_EQ(0, message_callback_count); |
| 3891 | notCaught->Call(env->Global(), 0, NULL); |
| 3892 | CHECK_EQ(1, exception_hit_count); |
| 3893 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3894 | CHECK_EQ(1, message_callback_count); |
| 3895 | |
| 3896 | // Break on exception and uncaught exception |
| 3897 | DebugEventCounterClear(); |
| 3898 | MessageCallbackCountClear(); |
| 3899 | ChangeBreakOnException(true, true); |
| 3900 | caught->Call(env->Global(), 0, NULL); |
| 3901 | CHECK_EQ(1, exception_hit_count); |
| 3902 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3903 | CHECK_EQ(0, message_callback_count); |
| 3904 | notCaught->Call(env->Global(), 0, NULL); |
| 3905 | CHECK_EQ(2, exception_hit_count); |
| 3906 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3907 | CHECK_EQ(1, message_callback_count); |
| 3908 | |
| 3909 | // Break on exception |
| 3910 | DebugEventCounterClear(); |
| 3911 | MessageCallbackCountClear(); |
| 3912 | ChangeBreakOnException(true, false); |
| 3913 | caught->Call(env->Global(), 0, NULL); |
| 3914 | CHECK_EQ(1, exception_hit_count); |
| 3915 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3916 | CHECK_EQ(0, message_callback_count); |
| 3917 | notCaught->Call(env->Global(), 0, NULL); |
| 3918 | CHECK_EQ(2, exception_hit_count); |
| 3919 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3920 | CHECK_EQ(1, message_callback_count); |
| 3921 | |
| 3922 | // No break on exception using JavaScript |
| 3923 | DebugEventCounterClear(); |
| 3924 | MessageCallbackCountClear(); |
| 3925 | ChangeBreakOnExceptionFromJS(false, false); |
| 3926 | caught->Call(env->Global(), 0, NULL); |
| 3927 | CHECK_EQ(0, exception_hit_count); |
| 3928 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3929 | CHECK_EQ(0, message_callback_count); |
| 3930 | notCaught->Call(env->Global(), 0, NULL); |
| 3931 | CHECK_EQ(0, exception_hit_count); |
| 3932 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3933 | CHECK_EQ(1, message_callback_count); |
| 3934 | |
| 3935 | // Break on uncaught exception using JavaScript |
| 3936 | DebugEventCounterClear(); |
| 3937 | MessageCallbackCountClear(); |
| 3938 | ChangeBreakOnExceptionFromJS(false, true); |
| 3939 | caught->Call(env->Global(), 0, NULL); |
| 3940 | CHECK_EQ(0, exception_hit_count); |
| 3941 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3942 | CHECK_EQ(0, message_callback_count); |
| 3943 | notCaught->Call(env->Global(), 0, NULL); |
| 3944 | CHECK_EQ(1, exception_hit_count); |
| 3945 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3946 | CHECK_EQ(1, message_callback_count); |
| 3947 | |
| 3948 | // Break on exception and uncaught exception using JavaScript |
| 3949 | DebugEventCounterClear(); |
| 3950 | MessageCallbackCountClear(); |
| 3951 | ChangeBreakOnExceptionFromJS(true, true); |
| 3952 | caught->Call(env->Global(), 0, NULL); |
| 3953 | CHECK_EQ(1, exception_hit_count); |
| 3954 | CHECK_EQ(0, message_callback_count); |
| 3955 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3956 | notCaught->Call(env->Global(), 0, NULL); |
| 3957 | CHECK_EQ(2, exception_hit_count); |
| 3958 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3959 | CHECK_EQ(1, message_callback_count); |
| 3960 | |
| 3961 | // Break on exception using JavaScript |
| 3962 | DebugEventCounterClear(); |
| 3963 | MessageCallbackCountClear(); |
| 3964 | ChangeBreakOnExceptionFromJS(true, false); |
| 3965 | caught->Call(env->Global(), 0, NULL); |
| 3966 | CHECK_EQ(1, exception_hit_count); |
| 3967 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3968 | CHECK_EQ(0, message_callback_count); |
| 3969 | notCaught->Call(env->Global(), 0, NULL); |
| 3970 | CHECK_EQ(2, exception_hit_count); |
| 3971 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3972 | CHECK_EQ(1, message_callback_count); |
| 3973 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3974 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3975 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3976 | v8::V8::RemoveMessageListeners(MessageCallbackCount); |
| 3977 | } |
| 3978 | |
| 3979 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3980 | // Test break on exception from compiler errors. When compiling using |
| 3981 | // v8::Script::Compile there is no JavaScript stack whereas when compiling using |
| 3982 | // eval there are JavaScript frames. |
| 3983 | TEST(BreakOnCompileException) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3984 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3985 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3986 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3987 | // For this test, we want to break on uncaught exceptions: |
| 3988 | ChangeBreakOnException(false, true); |
| 3989 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3990 | v8::internal::Isolate::Current()->TraceException(false); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3991 | |
| 3992 | // Create a function for checking the function when hitting a break point. |
| 3993 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 3994 | |
| 3995 | v8::V8::AddMessageListener(MessageCallbackCount); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3996 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3997 | |
| 3998 | DebugEventCounterClear(); |
| 3999 | MessageCallbackCountClear(); |
| 4000 | |
| 4001 | // Check initial state. |
| 4002 | CHECK_EQ(0, exception_hit_count); |
| 4003 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 4004 | CHECK_EQ(0, message_callback_count); |
| 4005 | CHECK_EQ(-1, last_js_stack_height); |
| 4006 | |
| 4007 | // Throws SyntaxError: Unexpected end of input |
| 4008 | v8::Script::Compile(v8::String::New("+++")); |
| 4009 | CHECK_EQ(1, exception_hit_count); |
| 4010 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 4011 | CHECK_EQ(1, message_callback_count); |
| 4012 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4013 | |
| 4014 | // Throws SyntaxError: Unexpected identifier |
| 4015 | v8::Script::Compile(v8::String::New("x x")); |
| 4016 | CHECK_EQ(2, exception_hit_count); |
| 4017 | CHECK_EQ(2, uncaught_exception_hit_count); |
| 4018 | CHECK_EQ(2, message_callback_count); |
| 4019 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 4020 | |
| 4021 | // Throws SyntaxError: Unexpected end of input |
| 4022 | v8::Script::Compile(v8::String::New("eval('+++')"))->Run(); |
| 4023 | CHECK_EQ(3, exception_hit_count); |
| 4024 | CHECK_EQ(3, uncaught_exception_hit_count); |
| 4025 | CHECK_EQ(3, message_callback_count); |
| 4026 | CHECK_EQ(1, last_js_stack_height); |
| 4027 | |
| 4028 | // Throws SyntaxError: Unexpected identifier |
| 4029 | v8::Script::Compile(v8::String::New("eval('x x')"))->Run(); |
| 4030 | CHECK_EQ(4, exception_hit_count); |
| 4031 | CHECK_EQ(4, uncaught_exception_hit_count); |
| 4032 | CHECK_EQ(4, message_callback_count); |
| 4033 | CHECK_EQ(1, last_js_stack_height); |
| 4034 | } |
| 4035 | |
| 4036 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4037 | TEST(StepWithException) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4038 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4039 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4040 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4041 | // For this test, we want to break on uncaught exceptions: |
| 4042 | ChangeBreakOnException(false, true); |
| 4043 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4044 | // Create a function for checking the function when hitting a break point. |
| 4045 | frame_function_name = CompileFunction(&env, |
| 4046 | frame_function_name_source, |
| 4047 | "frame_function_name"); |
| 4048 | |
| 4049 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4050 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4051 | |
| 4052 | // Create functions for testing stepping. |
| 4053 | const char* src = "function a() { n(); }; " |
| 4054 | "function b() { c(); }; " |
| 4055 | "function c() { n(); }; " |
| 4056 | "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " |
| 4057 | "function e() { n(); }; " |
| 4058 | "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " |
| 4059 | "function g() { h(); }; " |
| 4060 | "function h() { x = 1; throw 1; }; "; |
| 4061 | |
| 4062 | // Step through invocation of a. |
| 4063 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 4064 | SetBreakPoint(a, 0); |
| 4065 | step_action = StepIn; |
| 4066 | break_point_hit_count = 0; |
| 4067 | expected_step_sequence = "aa"; |
| 4068 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4069 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4070 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4071 | |
| 4072 | // Step through invocation of b + c. |
| 4073 | v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); |
| 4074 | SetBreakPoint(b, 0); |
| 4075 | step_action = StepIn; |
| 4076 | break_point_hit_count = 0; |
| 4077 | expected_step_sequence = "bcc"; |
| 4078 | b->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4079 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4080 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4081 | // Step through invocation of d + e. |
| 4082 | v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); |
| 4083 | SetBreakPoint(d, 0); |
| 4084 | ChangeBreakOnException(false, true); |
| 4085 | step_action = StepIn; |
| 4086 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 4087 | expected_step_sequence = "ddedd"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4088 | d->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4089 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4090 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4091 | |
| 4092 | // Step through invocation of d + e now with break on caught exceptions. |
| 4093 | ChangeBreakOnException(true, true); |
| 4094 | step_action = StepIn; |
| 4095 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 4096 | expected_step_sequence = "ddeedd"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4097 | d->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4098 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4099 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4100 | |
| 4101 | // Step through invocation of f + g + h. |
| 4102 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4103 | SetBreakPoint(f, 0); |
| 4104 | ChangeBreakOnException(false, true); |
| 4105 | step_action = StepIn; |
| 4106 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 4107 | expected_step_sequence = "ffghhff"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4108 | f->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4109 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4110 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4111 | |
| 4112 | // Step through invocation of f + g + h now with break on caught exceptions. |
| 4113 | ChangeBreakOnException(true, true); |
| 4114 | step_action = StepIn; |
| 4115 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 4116 | expected_step_sequence = "ffghhhff"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4117 | f->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4118 | CHECK_EQ(StrLength(expected_step_sequence), |
| 4119 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4120 | |
| 4121 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4122 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4123 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | |
| 4127 | TEST(DebugBreak) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4128 | #ifdef VERIFY_HEAP |
| 4129 | i::FLAG_verify_heap = true; |
| 4130 | #endif |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4131 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4132 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4133 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4134 | // Register a debug event listener which sets the break flag and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4135 | v8::Debug::SetDebugEventListener(DebugEventBreak); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4136 | |
| 4137 | // Create a function for testing stepping. |
| 4138 | const char* src = "function f0() {}" |
| 4139 | "function f1(x1) {}" |
| 4140 | "function f2(x1,x2) {}" |
| 4141 | "function f3(x1,x2,x3) {}"; |
| 4142 | v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); |
| 4143 | v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); |
| 4144 | v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); |
| 4145 | v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); |
| 4146 | |
| 4147 | // Call the function to make sure it is compiled. |
| 4148 | v8::Handle<v8::Value> argv[] = { v8::Number::New(1), |
| 4149 | v8::Number::New(1), |
| 4150 | v8::Number::New(1), |
| 4151 | v8::Number::New(1) }; |
| 4152 | |
| 4153 | // Call all functions to make sure that they are compiled. |
| 4154 | f0->Call(env->Global(), 0, NULL); |
| 4155 | f1->Call(env->Global(), 0, NULL); |
| 4156 | f2->Call(env->Global(), 0, NULL); |
| 4157 | f3->Call(env->Global(), 0, NULL); |
| 4158 | |
| 4159 | // Set the debug break flag. |
| 4160 | v8::Debug::DebugBreak(); |
| 4161 | |
| 4162 | // Call all functions with different argument count. |
| 4163 | break_point_hit_count = 0; |
| 4164 | for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) { |
| 4165 | f0->Call(env->Global(), i, argv); |
| 4166 | f1->Call(env->Global(), i, argv); |
| 4167 | f2->Call(env->Global(), i, argv); |
| 4168 | f3->Call(env->Global(), i, argv); |
| 4169 | } |
| 4170 | |
| 4171 | // One break for each function called. |
| 4172 | CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count); |
| 4173 | |
| 4174 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4175 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4176 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | |
| 4180 | // Test to ensure that JavaScript code keeps running while the debug break |
| 4181 | // through the stack limit flag is set but breaks are disabled. |
| 4182 | TEST(DisableBreak) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4183 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4184 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4185 | |
| 4186 | // Register a debug event listener which sets the break flag and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4187 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4188 | |
| 4189 | // Create a function for testing stepping. |
| 4190 | const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; |
| 4191 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 4192 | |
| 4193 | // Set the debug break flag. |
| 4194 | v8::Debug::DebugBreak(); |
| 4195 | |
| 4196 | // Call all functions with different argument count. |
| 4197 | break_point_hit_count = 0; |
| 4198 | f->Call(env->Global(), 0, NULL); |
| 4199 | CHECK_EQ(1, break_point_hit_count); |
| 4200 | |
| 4201 | { |
| 4202 | v8::Debug::DebugBreak(); |
| 4203 | v8::internal::DisableBreak disable_break(true); |
| 4204 | f->Call(env->Global(), 0, NULL); |
| 4205 | CHECK_EQ(1, break_point_hit_count); |
| 4206 | } |
| 4207 | |
| 4208 | f->Call(env->Global(), 0, NULL); |
| 4209 | CHECK_EQ(2, break_point_hit_count); |
| 4210 | |
| 4211 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 4212 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4213 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4216 | static const char* kSimpleExtensionSource = |
| 4217 | "(function Foo() {" |
| 4218 | " return 4;" |
| 4219 | "})() "; |
| 4220 | |
| 4221 | // http://crbug.com/28933 |
| 4222 | // Test that debug break is disabled when bootstrapper is active. |
| 4223 | TEST(NoBreakWhenBootstrapping) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4224 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4225 | |
| 4226 | // Register a debug event listener which sets the break flag and counts. |
| 4227 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4228 | |
| 4229 | // Set the debug break flag. |
| 4230 | v8::Debug::DebugBreak(); |
| 4231 | break_point_hit_count = 0; |
| 4232 | { |
| 4233 | // Create a context with an extension to make sure that some JavaScript |
| 4234 | // code is executed during bootstrapping. |
| 4235 | v8::RegisterExtension(new v8::Extension("simpletest", |
| 4236 | kSimpleExtensionSource)); |
| 4237 | const char* extension_names[] = { "simpletest" }; |
| 4238 | v8::ExtensionConfiguration extensions(1, extension_names); |
| 4239 | v8::Persistent<v8::Context> context = v8::Context::New(&extensions); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 4240 | context.Dispose(context->GetIsolate()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4241 | } |
| 4242 | // Check that no DebugBreak events occured during the context creation. |
| 4243 | CHECK_EQ(0, break_point_hit_count); |
| 4244 | |
| 4245 | // Get rid of the debug event listener. |
| 4246 | v8::Debug::SetDebugEventListener(NULL); |
| 4247 | CheckDebuggerUnloaded(); |
| 4248 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4249 | |
| 4250 | static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) { |
| 4251 | v8::Handle<v8::Array> result = v8::Array::New(3); |
| 4252 | result->Set(v8::Integer::New(0), v8::String::New("a")); |
| 4253 | result->Set(v8::Integer::New(1), v8::String::New("b")); |
| 4254 | result->Set(v8::Integer::New(2), v8::String::New("c")); |
| 4255 | return result; |
| 4256 | } |
| 4257 | |
| 4258 | |
| 4259 | static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) { |
| 4260 | v8::Handle<v8::Array> result = v8::Array::New(2); |
| 4261 | result->Set(v8::Integer::New(0), v8::Number::New(1)); |
| 4262 | result->Set(v8::Integer::New(1), v8::Number::New(10)); |
| 4263 | return result; |
| 4264 | } |
| 4265 | |
| 4266 | |
| 4267 | static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name, |
| 4268 | const v8::AccessorInfo& info) { |
| 4269 | v8::String::AsciiValue n(name); |
| 4270 | if (strcmp(*n, "a") == 0) { |
| 4271 | return v8::String::New("AA"); |
| 4272 | } else if (strcmp(*n, "b") == 0) { |
| 4273 | return v8::String::New("BB"); |
| 4274 | } else if (strcmp(*n, "c") == 0) { |
| 4275 | return v8::String::New("CC"); |
| 4276 | } else { |
| 4277 | return v8::Undefined(); |
| 4278 | } |
| 4279 | |
| 4280 | return name; |
| 4281 | } |
| 4282 | |
| 4283 | |
| 4284 | static v8::Handle<v8::Value> IndexedGetter(uint32_t index, |
| 4285 | const v8::AccessorInfo& info) { |
| 4286 | return v8::Number::New(index + 1); |
| 4287 | } |
| 4288 | |
| 4289 | |
| 4290 | TEST(InterceptorPropertyMirror) { |
| 4291 | // Create a V8 environment with debug access. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4292 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4293 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4294 | env.ExposeDebug(); |
| 4295 | |
| 4296 | // Create object with named interceptor. |
| 4297 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4298 | named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); |
| 4299 | env->Global()->Set(v8::String::New("intercepted_named"), |
| 4300 | named->NewInstance()); |
| 4301 | |
| 4302 | // Create object with indexed interceptor. |
| 4303 | v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(); |
| 4304 | indexed->SetIndexedPropertyHandler(IndexedGetter, |
| 4305 | NULL, |
| 4306 | NULL, |
| 4307 | NULL, |
| 4308 | IndexedEnum); |
| 4309 | env->Global()->Set(v8::String::New("intercepted_indexed"), |
| 4310 | indexed->NewInstance()); |
| 4311 | |
| 4312 | // Create object with both named and indexed interceptor. |
| 4313 | v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New(); |
| 4314 | both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); |
| 4315 | both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum); |
| 4316 | env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance()); |
| 4317 | |
| 4318 | // Get mirrors for the three objects with interceptor. |
| 4319 | CompileRun( |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4320 | "var named_mirror = debug.MakeMirror(intercepted_named);" |
| 4321 | "var indexed_mirror = debug.MakeMirror(intercepted_indexed);" |
| 4322 | "var both_mirror = debug.MakeMirror(intercepted_both)"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4323 | CHECK(CompileRun( |
| 4324 | "named_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4325 | CHECK(CompileRun( |
| 4326 | "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4327 | CHECK(CompileRun( |
| 4328 | "both_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4329 | |
| 4330 | // Get the property names from the interceptors |
| 4331 | CompileRun( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4332 | "named_names = named_mirror.propertyNames();" |
| 4333 | "indexed_names = indexed_mirror.propertyNames();" |
| 4334 | "both_names = both_mirror.propertyNames()"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4335 | CHECK_EQ(3, CompileRun("named_names.length")->Int32Value()); |
| 4336 | CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value()); |
| 4337 | CHECK_EQ(5, CompileRun("both_names.length")->Int32Value()); |
| 4338 | |
| 4339 | // Check the expected number of properties. |
| 4340 | const char* source; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4341 | source = "named_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4342 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4343 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4344 | source = "indexed_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4345 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4346 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4347 | source = "both_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4348 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4349 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4350 | // 1 is PropertyKind.Named; |
| 4351 | source = "both_mirror.properties(1).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4352 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4353 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4354 | // 2 is PropertyKind.Indexed; |
| 4355 | source = "both_mirror.properties(2).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4356 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4357 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4358 | // 3 is PropertyKind.Named | PropertyKind.Indexed; |
| 4359 | source = "both_mirror.properties(3).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4360 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4361 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4362 | // Get the interceptor properties for the object with only named interceptor. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4363 | CompileRun("var named_values = named_mirror.properties()"); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4364 | |
| 4365 | // Check that the properties are interceptor properties. |
| 4366 | for (int i = 0; i < 3; i++) { |
| 4367 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4368 | OS::SNPrintF(buffer, |
| 4369 | "named_values[%d] instanceof debug.PropertyMirror", i); |
| 4370 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4371 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4372 | OS::SNPrintF(buffer, "named_values[%d].propertyType()", i); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4373 | CHECK_EQ(v8::internal::INTERCEPTOR, |
| 4374 | CompileRun(buffer.start())->Int32Value()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4375 | |
| 4376 | OS::SNPrintF(buffer, "named_values[%d].isNative()", i); |
| 4377 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4378 | } |
| 4379 | |
| 4380 | // Get the interceptor properties for the object with only indexed |
| 4381 | // interceptor. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4382 | CompileRun("var indexed_values = indexed_mirror.properties()"); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4383 | |
| 4384 | // Check that the properties are interceptor properties. |
| 4385 | for (int i = 0; i < 2; i++) { |
| 4386 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4387 | OS::SNPrintF(buffer, |
| 4388 | "indexed_values[%d] instanceof debug.PropertyMirror", i); |
| 4389 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4390 | } |
| 4391 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4392 | // Get the interceptor properties for the object with both types of |
| 4393 | // interceptors. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4394 | CompileRun("var both_values = both_mirror.properties()"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4395 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4396 | // Check that the properties are interceptor properties. |
| 4397 | for (int i = 0; i < 5; i++) { |
| 4398 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4399 | OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); |
| 4400 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4401 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4402 | |
| 4403 | // Check the property names. |
| 4404 | source = "both_values[0].name() == 'a'"; |
| 4405 | CHECK(CompileRun(source)->BooleanValue()); |
| 4406 | |
| 4407 | source = "both_values[1].name() == 'b'"; |
| 4408 | CHECK(CompileRun(source)->BooleanValue()); |
| 4409 | |
| 4410 | source = "both_values[2].name() == 'c'"; |
| 4411 | CHECK(CompileRun(source)->BooleanValue()); |
| 4412 | |
| 4413 | source = "both_values[3].name() == 1"; |
| 4414 | CHECK(CompileRun(source)->BooleanValue()); |
| 4415 | |
| 4416 | source = "both_values[4].name() == 10"; |
| 4417 | CHECK(CompileRun(source)->BooleanValue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 4421 | TEST(HiddenPrototypePropertyMirror) { |
| 4422 | // Create a V8 environment with debug access. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 4423 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4424 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 4425 | env.ExposeDebug(); |
| 4426 | |
| 4427 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 4428 | t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0)); |
| 4429 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 4430 | t1->SetHiddenPrototype(true); |
| 4431 | t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1)); |
| 4432 | v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 4433 | t2->SetHiddenPrototype(true); |
| 4434 | t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2)); |
| 4435 | v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 4436 | t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3)); |
| 4437 | |
| 4438 | // Create object and set them on the global object. |
| 4439 | v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
| 4440 | env->Global()->Set(v8::String::New("o0"), o0); |
| 4441 | v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 4442 | env->Global()->Set(v8::String::New("o1"), o1); |
| 4443 | v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 4444 | env->Global()->Set(v8::String::New("o2"), o2); |
| 4445 | v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 4446 | env->Global()->Set(v8::String::New("o3"), o3); |
| 4447 | |
| 4448 | // Get mirrors for the four objects. |
| 4449 | CompileRun( |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4450 | "var o0_mirror = debug.MakeMirror(o0);" |
| 4451 | "var o1_mirror = debug.MakeMirror(o1);" |
| 4452 | "var o2_mirror = debug.MakeMirror(o2);" |
| 4453 | "var o3_mirror = debug.MakeMirror(o3)"); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 4454 | CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4455 | CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4456 | CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4457 | CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4458 | |
| 4459 | // Check that each object has one property. |
| 4460 | CHECK_EQ(1, CompileRun( |
| 4461 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4462 | CHECK_EQ(1, CompileRun( |
| 4463 | "o1_mirror.propertyNames().length")->Int32Value()); |
| 4464 | CHECK_EQ(1, CompileRun( |
| 4465 | "o2_mirror.propertyNames().length")->Int32Value()); |
| 4466 | CHECK_EQ(1, CompileRun( |
| 4467 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4468 | |
| 4469 | // Set o1 as prototype for o0. o1 has the hidden prototype flag so all |
| 4470 | // properties on o1 should be seen on o0. |
| 4471 | o0->Set(v8::String::New("__proto__"), o1); |
| 4472 | CHECK_EQ(2, CompileRun( |
| 4473 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4474 | CHECK_EQ(0, CompileRun( |
| 4475 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4476 | CHECK_EQ(1, CompileRun( |
| 4477 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4478 | |
| 4479 | // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden |
| 4480 | // prototype flag. o2 also has the hidden prototype flag so all properties |
| 4481 | // on o2 should be seen on o0 as well as properties on o1. |
| 4482 | o0->Set(v8::String::New("__proto__"), o2); |
| 4483 | CHECK_EQ(3, CompileRun( |
| 4484 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4485 | CHECK_EQ(0, CompileRun( |
| 4486 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4487 | CHECK_EQ(1, CompileRun( |
| 4488 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4489 | CHECK_EQ(2, CompileRun( |
| 4490 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4491 | |
| 4492 | // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and |
| 4493 | // o2 has the hidden prototype flag. o3 does not have the hidden prototype |
| 4494 | // flag so properties on o3 should not be seen on o0 whereas the properties |
| 4495 | // from o1 and o2 should still be seen on o0. |
| 4496 | // Final prototype chain: o0 -> o1 -> o2 -> o3 |
| 4497 | // Hidden prototypes: ^^ ^^ |
| 4498 | o0->Set(v8::String::New("__proto__"), o3); |
| 4499 | CHECK_EQ(3, CompileRun( |
| 4500 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4501 | CHECK_EQ(1, CompileRun( |
| 4502 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4503 | CHECK_EQ(0, CompileRun( |
| 4504 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4505 | CHECK_EQ(1, CompileRun( |
| 4506 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4507 | CHECK_EQ(2, CompileRun( |
| 4508 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4509 | CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue()); |
| 4510 | |
| 4511 | // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. |
| 4512 | CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue()); |
| 4513 | } |
| 4514 | |
| 4515 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4516 | static v8::Handle<v8::Value> ProtperyXNativeGetter( |
| 4517 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 4518 | return v8::Integer::New(10); |
| 4519 | } |
| 4520 | |
| 4521 | |
| 4522 | TEST(NativeGetterPropertyMirror) { |
| 4523 | // Create a V8 environment with debug access. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4524 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4525 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4526 | env.ExposeDebug(); |
| 4527 | |
| 4528 | v8::Handle<v8::String> name = v8::String::New("x"); |
| 4529 | // Create object with named accessor. |
| 4530 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4531 | named->SetAccessor(name, &ProtperyXNativeGetter, NULL, |
| 4532 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4533 | |
| 4534 | // Create object with named property getter. |
| 4535 | env->Global()->Set(v8::String::New("instance"), named->NewInstance()); |
| 4536 | CHECK_EQ(10, CompileRun("instance.x")->Int32Value()); |
| 4537 | |
| 4538 | // Get mirror for the object with property getter. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4539 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4540 | CHECK(CompileRun( |
| 4541 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4542 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4543 | CompileRun("var named_names = instance_mirror.propertyNames();"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4544 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4545 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4546 | CHECK(CompileRun( |
| 4547 | "instance_mirror.property('x').value().isNumber()")->BooleanValue()); |
| 4548 | CHECK(CompileRun( |
| 4549 | "instance_mirror.property('x').value().value() == 10")->BooleanValue()); |
| 4550 | } |
| 4551 | |
| 4552 | |
| 4553 | static v8::Handle<v8::Value> ProtperyXNativeGetterThrowingError( |
| 4554 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 4555 | return CompileRun("throw new Error('Error message');"); |
| 4556 | } |
| 4557 | |
| 4558 | |
| 4559 | TEST(NativeGetterThrowingErrorPropertyMirror) { |
| 4560 | // Create a V8 environment with debug access. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4561 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4562 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4563 | env.ExposeDebug(); |
| 4564 | |
| 4565 | v8::Handle<v8::String> name = v8::String::New("x"); |
| 4566 | // Create object with named accessor. |
| 4567 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4568 | named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, |
| 4569 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4570 | |
| 4571 | // Create object with named property getter. |
| 4572 | env->Global()->Set(v8::String::New("instance"), named->NewInstance()); |
| 4573 | |
| 4574 | // Get mirror for the object with property getter. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4575 | CompileRun("var instance_mirror = debug.MakeMirror(instance);"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4576 | CHECK(CompileRun( |
| 4577 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4578 | CompileRun("named_names = instance_mirror.propertyNames();"); |
| 4579 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4580 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4581 | CHECK(CompileRun( |
| 4582 | "instance_mirror.property('x').value().isError()")->BooleanValue()); |
| 4583 | |
| 4584 | // Check that the message is that passed to the Error constructor. |
| 4585 | CHECK(CompileRun( |
| 4586 | "instance_mirror.property('x').value().message() == 'Error message'")-> |
| 4587 | BooleanValue()); |
| 4588 | } |
| 4589 | |
| 4590 | |
ager@chromium.org | c730f77 | 2009-11-11 10:11:16 +0000 | [diff] [blame] | 4591 | // Test that hidden properties object is not returned as an unnamed property |
| 4592 | // among regular properties. |
| 4593 | // See http://crbug.com/26491 |
| 4594 | TEST(NoHiddenProperties) { |
| 4595 | // Create a V8 environment with debug access. |
ager@chromium.org | c730f77 | 2009-11-11 10:11:16 +0000 | [diff] [blame] | 4596 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4597 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | c730f77 | 2009-11-11 10:11:16 +0000 | [diff] [blame] | 4598 | env.ExposeDebug(); |
| 4599 | |
| 4600 | // Create an object in the global scope. |
| 4601 | const char* source = "var obj = {a: 1};"; |
| 4602 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 4603 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( |
| 4604 | env->Global()->Get(v8::String::New("obj"))); |
| 4605 | // Set a hidden property on the object. |
| 4606 | obj->SetHiddenValue(v8::String::New("v8::test-debug::a"), |
| 4607 | v8::Int32::New(11)); |
| 4608 | |
| 4609 | // Get mirror for the object with property getter. |
| 4610 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4611 | CHECK(CompileRun( |
| 4612 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4613 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4614 | // There should be exactly one property. But there is also an unnamed |
| 4615 | // property whose value is hidden properties dictionary. The latter |
| 4616 | // property should not be in the list of reguar properties. |
| 4617 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4618 | CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue()); |
| 4619 | CHECK(CompileRun( |
| 4620 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4621 | |
| 4622 | // Object created by t0 will become hidden prototype of object 'obj'. |
| 4623 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 4624 | t0->InstanceTemplate()->Set(v8::String::New("b"), v8::Number::New(2)); |
| 4625 | t0->SetHiddenPrototype(true); |
| 4626 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 4627 | t1->InstanceTemplate()->Set(v8::String::New("c"), v8::Number::New(3)); |
| 4628 | |
| 4629 | // Create proto objects, add hidden properties to them and set them on |
| 4630 | // the global object. |
| 4631 | v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance(); |
| 4632 | protoObj->SetHiddenValue(v8::String::New("v8::test-debug::b"), |
| 4633 | v8::Int32::New(12)); |
| 4634 | env->Global()->Set(v8::String::New("protoObj"), protoObj); |
| 4635 | v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance(); |
| 4636 | grandProtoObj->SetHiddenValue(v8::String::New("v8::test-debug::c"), |
| 4637 | v8::Int32::New(13)); |
| 4638 | env->Global()->Set(v8::String::New("grandProtoObj"), grandProtoObj); |
| 4639 | |
| 4640 | // Setting prototypes: obj->protoObj->grandProtoObj |
| 4641 | protoObj->Set(v8::String::New("__proto__"), grandProtoObj); |
| 4642 | obj->Set(v8::String::New("__proto__"), protoObj); |
| 4643 | |
| 4644 | // Get mirror for the object with property getter. |
| 4645 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4646 | CHECK(CompileRun( |
| 4647 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4648 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4649 | // There should be exactly two properties - one from the object itself and |
| 4650 | // another from its hidden prototype. |
| 4651 | CHECK_EQ(2, CompileRun("named_names.length")->Int32Value()); |
| 4652 | CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" |
| 4653 | "named_names[1] == 'b'")->BooleanValue()); |
| 4654 | CHECK(CompileRun( |
| 4655 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4656 | CHECK(CompileRun( |
| 4657 | "obj_mirror.property('b').value().value() == 2")->BooleanValue()); |
| 4658 | } |
| 4659 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4660 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4661 | // Multithreaded tests of JSON debugger protocol |
| 4662 | |
| 4663 | // Support classes |
| 4664 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4665 | // Provides synchronization between k threads, where k is an input to the |
| 4666 | // constructor. The Wait() call blocks a thread until it is called for the |
| 4667 | // k'th time, then all calls return. Each ThreadBarrier object can only |
| 4668 | // be used once. |
| 4669 | class ThreadBarrier { |
| 4670 | public: |
| 4671 | explicit ThreadBarrier(int num_threads); |
| 4672 | ~ThreadBarrier(); |
| 4673 | void Wait(); |
| 4674 | private: |
| 4675 | int num_threads_; |
| 4676 | int num_blocked_; |
| 4677 | v8::internal::Mutex* lock_; |
| 4678 | v8::internal::Semaphore* sem_; |
| 4679 | bool invalid_; |
| 4680 | }; |
| 4681 | |
| 4682 | ThreadBarrier::ThreadBarrier(int num_threads) |
| 4683 | : num_threads_(num_threads), num_blocked_(0) { |
| 4684 | lock_ = OS::CreateMutex(); |
| 4685 | sem_ = OS::CreateSemaphore(0); |
| 4686 | invalid_ = false; // A barrier may only be used once. Then it is invalid. |
| 4687 | } |
| 4688 | |
| 4689 | // Do not call, due to race condition with Wait(). |
| 4690 | // Could be resolved with Pthread condition variables. |
| 4691 | ThreadBarrier::~ThreadBarrier() { |
| 4692 | lock_->Lock(); |
| 4693 | delete lock_; |
| 4694 | delete sem_; |
| 4695 | } |
| 4696 | |
| 4697 | void ThreadBarrier::Wait() { |
| 4698 | lock_->Lock(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4699 | CHECK(!invalid_); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4700 | if (num_blocked_ == num_threads_ - 1) { |
| 4701 | // Signal and unblock all waiting threads. |
| 4702 | for (int i = 0; i < num_threads_ - 1; ++i) { |
| 4703 | sem_->Signal(); |
| 4704 | } |
| 4705 | invalid_ = true; |
| 4706 | printf("BARRIER\n\n"); |
| 4707 | fflush(stdout); |
| 4708 | lock_->Unlock(); |
| 4709 | } else { // Wait for the semaphore. |
| 4710 | ++num_blocked_; |
| 4711 | lock_->Unlock(); // Potential race condition with destructor because |
| 4712 | sem_->Wait(); // these two lines are not atomic. |
| 4713 | } |
| 4714 | } |
| 4715 | |
| 4716 | // A set containing enough barriers and semaphores for any of the tests. |
| 4717 | class Barriers { |
| 4718 | public: |
| 4719 | Barriers(); |
| 4720 | void Initialize(); |
| 4721 | ThreadBarrier barrier_1; |
| 4722 | ThreadBarrier barrier_2; |
| 4723 | ThreadBarrier barrier_3; |
| 4724 | ThreadBarrier barrier_4; |
| 4725 | ThreadBarrier barrier_5; |
| 4726 | v8::internal::Semaphore* semaphore_1; |
| 4727 | v8::internal::Semaphore* semaphore_2; |
| 4728 | }; |
| 4729 | |
| 4730 | Barriers::Barriers() : barrier_1(2), barrier_2(2), |
| 4731 | barrier_3(2), barrier_4(2), barrier_5(2) {} |
| 4732 | |
| 4733 | void Barriers::Initialize() { |
| 4734 | semaphore_1 = OS::CreateSemaphore(0); |
| 4735 | semaphore_2 = OS::CreateSemaphore(0); |
| 4736 | } |
| 4737 | |
| 4738 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4739 | // We match parts of the message to decide if it is a break message. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4740 | bool IsBreakEventMessage(char *message) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4741 | const char* type_event = "\"type\":\"event\""; |
| 4742 | const char* event_break = "\"event\":\"break\""; |
| 4743 | // Does the message contain both type:event and event:break? |
| 4744 | return strstr(message, type_event) != NULL && |
| 4745 | strstr(message, event_break) != NULL; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4749 | // We match parts of the message to decide if it is a exception message. |
| 4750 | bool IsExceptionEventMessage(char *message) { |
| 4751 | const char* type_event = "\"type\":\"event\""; |
| 4752 | const char* event_exception = "\"event\":\"exception\""; |
| 4753 | // Does the message contain both type:event and event:exception? |
| 4754 | return strstr(message, type_event) != NULL && |
| 4755 | strstr(message, event_exception) != NULL; |
| 4756 | } |
| 4757 | |
| 4758 | |
| 4759 | // We match the message wether it is an evaluate response message. |
| 4760 | bool IsEvaluateResponseMessage(char* message) { |
| 4761 | const char* type_response = "\"type\":\"response\""; |
| 4762 | const char* command_evaluate = "\"command\":\"evaluate\""; |
| 4763 | // Does the message contain both type:response and command:evaluate? |
| 4764 | return strstr(message, type_response) != NULL && |
| 4765 | strstr(message, command_evaluate) != NULL; |
| 4766 | } |
| 4767 | |
| 4768 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4769 | static int StringToInt(const char* s) { |
| 4770 | return atoi(s); // NOLINT |
| 4771 | } |
| 4772 | |
| 4773 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4774 | // We match parts of the message to get evaluate result int value. |
| 4775 | int GetEvaluateIntResult(char *message) { |
| 4776 | const char* value = "\"value\":"; |
| 4777 | char* pos = strstr(message, value); |
| 4778 | if (pos == NULL) { |
| 4779 | return -1; |
| 4780 | } |
| 4781 | int res = -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4782 | res = StringToInt(pos + strlen(value)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4783 | return res; |
| 4784 | } |
| 4785 | |
| 4786 | |
| 4787 | // We match parts of the message to get hit breakpoint id. |
| 4788 | int GetBreakpointIdFromBreakEventMessage(char *message) { |
| 4789 | const char* breakpoints = "\"breakpoints\":["; |
| 4790 | char* pos = strstr(message, breakpoints); |
| 4791 | if (pos == NULL) { |
| 4792 | return -1; |
| 4793 | } |
| 4794 | int res = -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4795 | res = StringToInt(pos + strlen(breakpoints)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4796 | return res; |
| 4797 | } |
| 4798 | |
| 4799 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4800 | // We match parts of the message to get total frames number. |
| 4801 | int GetTotalFramesInt(char *message) { |
| 4802 | const char* prefix = "\"totalFrames\":"; |
| 4803 | char* pos = strstr(message, prefix); |
| 4804 | if (pos == NULL) { |
| 4805 | return -1; |
| 4806 | } |
| 4807 | pos += strlen(prefix); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4808 | int res = StringToInt(pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4809 | return res; |
| 4810 | } |
| 4811 | |
| 4812 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4813 | // We match parts of the message to get source line. |
| 4814 | int GetSourceLineFromBreakEventMessage(char *message) { |
| 4815 | const char* source_line = "\"sourceLine\":"; |
| 4816 | char* pos = strstr(message, source_line); |
| 4817 | if (pos == NULL) { |
| 4818 | return -1; |
| 4819 | } |
| 4820 | int res = -1; |
| 4821 | res = StringToInt(pos + strlen(source_line)); |
| 4822 | return res; |
| 4823 | } |
| 4824 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4825 | /* Test MessageQueues */ |
| 4826 | /* Tests the message queues that hold debugger commands and |
| 4827 | * response messages to the debugger. Fills queues and makes |
| 4828 | * them grow. |
| 4829 | */ |
| 4830 | Barriers message_queue_barriers; |
| 4831 | |
| 4832 | // This is the debugger thread, that executes no v8 calls except |
| 4833 | // placing JSON debugger commands in the queue. |
| 4834 | class MessageQueueDebuggerThread : public v8::internal::Thread { |
| 4835 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4836 | MessageQueueDebuggerThread() |
| 4837 | : Thread("MessageQueueDebuggerThread") { } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4838 | void Run(); |
| 4839 | }; |
| 4840 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4841 | static void MessageHandler(const uint16_t* message, int length, |
| 4842 | v8::Debug::ClientData* client_data) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4843 | static char print_buffer[1000]; |
| 4844 | Utf16ToAscii(message, length, print_buffer); |
| 4845 | if (IsBreakEventMessage(print_buffer)) { |
| 4846 | // Lets test script wait until break occurs to send commands. |
| 4847 | // Signals when a break is reported. |
| 4848 | message_queue_barriers.semaphore_2->Signal(); |
| 4849 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4850 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4851 | // Allow message handler to block on a semaphore, to test queueing of |
| 4852 | // messages while blocked. |
| 4853 | message_queue_barriers.semaphore_1->Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4856 | void MessageQueueDebuggerThread::Run() { |
| 4857 | const int kBufferSize = 1000; |
| 4858 | uint16_t buffer_1[kBufferSize]; |
| 4859 | uint16_t buffer_2[kBufferSize]; |
| 4860 | const char* command_1 = |
| 4861 | "{\"seq\":117," |
| 4862 | "\"type\":\"request\"," |
| 4863 | "\"command\":\"evaluate\"," |
| 4864 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 4865 | const char* command_2 = |
| 4866 | "{\"seq\":118," |
| 4867 | "\"type\":\"request\"," |
| 4868 | "\"command\":\"evaluate\"," |
| 4869 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 4870 | const char* command_3 = |
| 4871 | "{\"seq\":119," |
| 4872 | "\"type\":\"request\"," |
| 4873 | "\"command\":\"evaluate\"," |
| 4874 | "\"arguments\":{\"expression\":\"c.d * b\"}}"; |
| 4875 | const char* command_continue = |
| 4876 | "{\"seq\":106," |
| 4877 | "\"type\":\"request\"," |
| 4878 | "\"command\":\"continue\"}"; |
| 4879 | const char* command_single_step = |
| 4880 | "{\"seq\":107," |
| 4881 | "\"type\":\"request\"," |
| 4882 | "\"command\":\"continue\"," |
| 4883 | "\"arguments\":{\"stepaction\":\"next\"}}"; |
| 4884 | |
| 4885 | /* Interleaved sequence of actions by the two threads:*/ |
| 4886 | // Main thread compiles and runs source_1 |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4887 | message_queue_barriers.semaphore_1->Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4888 | message_queue_barriers.barrier_1.Wait(); |
| 4889 | // Post 6 commands, filling the command queue and making it expand. |
| 4890 | // These calls return immediately, but the commands stay on the queue |
| 4891 | // until the execution of source_2. |
| 4892 | // Note: AsciiToUtf16 executes before SendCommand, so command is copied |
| 4893 | // to buffer before buffer is sent to SendCommand. |
| 4894 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 4895 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 4896 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4897 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4898 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4899 | message_queue_barriers.barrier_2.Wait(); |
| 4900 | // Main thread compiles and runs source_2. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4901 | // Queued commands are executed at the start of compilation of source_2( |
| 4902 | // beforeCompile event). |
| 4903 | // Free the message handler to process all the messages from the queue. 7 |
| 4904 | // messages are expected: 2 afterCompile events and 5 responses. |
| 4905 | // All the commands added so far will fail to execute as long as call stack |
| 4906 | // is empty on beforeCompile event. |
| 4907 | for (int i = 0; i < 6 ; ++i) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4908 | message_queue_barriers.semaphore_1->Signal(); |
| 4909 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4910 | message_queue_barriers.barrier_3.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4911 | // Main thread compiles and runs source_3. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4912 | // Don't stop in the afterCompile handler. |
| 4913 | message_queue_barriers.semaphore_1->Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4914 | // source_3 includes a debugger statement, which causes a break event. |
| 4915 | // Wait on break event from hitting "debugger" statement |
| 4916 | message_queue_barriers.semaphore_2->Wait(); |
| 4917 | // These should execute after the "debugger" statement in source_2 |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4918 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 4919 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 4920 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4921 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2)); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4922 | // Run after 2 break events, 4 responses. |
| 4923 | for (int i = 0; i < 6 ; ++i) { |
| 4924 | message_queue_barriers.semaphore_1->Signal(); |
| 4925 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4926 | // Wait on break event after a single step executes. |
| 4927 | message_queue_barriers.semaphore_2->Wait(); |
| 4928 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1)); |
| 4929 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2)); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4930 | // Run after 2 responses. |
| 4931 | for (int i = 0; i < 2 ; ++i) { |
| 4932 | message_queue_barriers.semaphore_1->Signal(); |
| 4933 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4934 | // Main thread continues running source_3 to end, waits for this thread. |
| 4935 | } |
| 4936 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4937 | |
| 4938 | // This thread runs the v8 engine. |
| 4939 | TEST(MessageQueues) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4940 | MessageQueueDebuggerThread message_queue_debugger_thread; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4941 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4942 | // Create a V8 environment |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4943 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4944 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4945 | message_queue_barriers.Initialize(); |
| 4946 | v8::Debug::SetMessageHandler(MessageHandler); |
| 4947 | message_queue_debugger_thread.Start(); |
| 4948 | |
| 4949 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 4950 | const char* source_2 = "e = 17;"; |
| 4951 | const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; |
| 4952 | |
| 4953 | // See MessageQueueDebuggerThread::Run for interleaved sequence of |
| 4954 | // API calls and events in the two threads. |
| 4955 | CompileRun(source_1); |
| 4956 | message_queue_barriers.barrier_1.Wait(); |
| 4957 | message_queue_barriers.barrier_2.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4958 | CompileRun(source_2); |
| 4959 | message_queue_barriers.barrier_3.Wait(); |
| 4960 | CompileRun(source_3); |
| 4961 | message_queue_debugger_thread.Join(); |
| 4962 | fflush(stdout); |
| 4963 | } |
| 4964 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4965 | |
| 4966 | class TestClientData : public v8::Debug::ClientData { |
| 4967 | public: |
| 4968 | TestClientData() { |
| 4969 | constructor_call_counter++; |
| 4970 | } |
| 4971 | virtual ~TestClientData() { |
| 4972 | destructor_call_counter++; |
| 4973 | } |
| 4974 | |
| 4975 | static void ResetCounters() { |
| 4976 | constructor_call_counter = 0; |
| 4977 | destructor_call_counter = 0; |
| 4978 | } |
| 4979 | |
| 4980 | static int constructor_call_counter; |
| 4981 | static int destructor_call_counter; |
| 4982 | }; |
| 4983 | |
| 4984 | int TestClientData::constructor_call_counter = 0; |
| 4985 | int TestClientData::destructor_call_counter = 0; |
| 4986 | |
| 4987 | |
| 4988 | // Tests that MessageQueue doesn't destroy client data when expands and |
| 4989 | // does destroy when it dies. |
| 4990 | TEST(MessageQueueExpandAndDestroy) { |
| 4991 | TestClientData::ResetCounters(); |
| 4992 | { // Create a scope for the queue. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4993 | CommandMessageQueue queue(1); |
| 4994 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4995 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4996 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4997 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4998 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4999 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5000 | CHECK_EQ(0, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5001 | queue.Get().Dispose(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5002 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 5003 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5004 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5005 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5006 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5007 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5008 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5009 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5010 | new TestClientData())); |
| 5011 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 5012 | new TestClientData())); |
| 5013 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5014 | queue.Get().Dispose(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5015 | CHECK_EQ(2, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5016 | } |
| 5017 | // All the client data should be destroyed when the queue is destroyed. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5018 | CHECK_EQ(TestClientData::destructor_call_counter, |
| 5019 | TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | |
| 5023 | static int handled_client_data_instances_count = 0; |
| 5024 | static void MessageHandlerCountingClientData( |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5025 | const v8::Debug::Message& message) { |
| 5026 | if (message.GetClientData() != NULL) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5027 | handled_client_data_instances_count++; |
| 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | |
| 5032 | // Tests that all client data passed to the debugger are sent to the handler. |
| 5033 | TEST(SendClientDataToHandler) { |
| 5034 | // Create a V8 environment |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5035 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5036 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5037 | TestClientData::ResetCounters(); |
| 5038 | handled_client_data_instances_count = 0; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5039 | v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData); |
| 5040 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5041 | const int kBufferSize = 1000; |
| 5042 | uint16_t buffer[kBufferSize]; |
| 5043 | const char* command_1 = |
| 5044 | "{\"seq\":117," |
| 5045 | "\"type\":\"request\"," |
| 5046 | "\"command\":\"evaluate\"," |
| 5047 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 5048 | const char* command_2 = |
| 5049 | "{\"seq\":118," |
| 5050 | "\"type\":\"request\"," |
| 5051 | "\"command\":\"evaluate\"," |
| 5052 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 5053 | const char* command_continue = |
| 5054 | "{\"seq\":106," |
| 5055 | "\"type\":\"request\"," |
| 5056 | "\"command\":\"continue\"}"; |
| 5057 | |
| 5058 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer), |
| 5059 | new TestClientData()); |
| 5060 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL); |
| 5061 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), |
| 5062 | new TestClientData()); |
| 5063 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), |
| 5064 | new TestClientData()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5065 | // All the messages will be processed on beforeCompile event. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5066 | CompileRun(source_1); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5067 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5068 | CHECK_EQ(3, TestClientData::constructor_call_counter); |
| 5069 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5070 | handled_client_data_instances_count); |
| 5071 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 5072 | TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5076 | /* Test ThreadedDebugging */ |
| 5077 | /* This test interrupts a running infinite loop that is |
| 5078 | * occupying the v8 thread by a break command from the |
| 5079 | * debugger thread. It then changes the value of a |
| 5080 | * global object, to make the loop terminate. |
| 5081 | */ |
| 5082 | |
| 5083 | Barriers threaded_debugging_barriers; |
| 5084 | |
| 5085 | class V8Thread : public v8::internal::Thread { |
| 5086 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5087 | V8Thread() : Thread("V8Thread") { } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5088 | void Run(); |
| 5089 | }; |
| 5090 | |
| 5091 | class DebuggerThread : public v8::internal::Thread { |
| 5092 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5093 | DebuggerThread() : Thread("DebuggerThread") { } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5094 | void Run(); |
| 5095 | }; |
| 5096 | |
| 5097 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5098 | static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) { |
| 5099 | threaded_debugging_barriers.barrier_1.Wait(); |
| 5100 | return v8::Undefined(); |
| 5101 | } |
| 5102 | |
| 5103 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5104 | static void ThreadedMessageHandler(const v8::Debug::Message& message) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5105 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5106 | v8::String::Value json(message.GetJSON()); |
| 5107 | Utf16ToAscii(*json, json.length(), print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5108 | if (IsBreakEventMessage(print_buffer)) { |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 5109 | // Check that we are inside the while loop. |
| 5110 | int source_line = GetSourceLineFromBreakEventMessage(print_buffer); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 5111 | // TODO(2047): This should really be 8 <= source_line <= 13; but we |
| 5112 | // currently have an off-by-one error when calculating the source |
| 5113 | // position corresponding to the program counter at the debug break. |
| 5114 | CHECK(7 <= source_line && source_line <= 13); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5115 | threaded_debugging_barriers.barrier_2.Wait(); |
| 5116 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | |
| 5120 | void V8Thread::Run() { |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5121 | const char* source = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5122 | "flag = true;\n" |
| 5123 | "function bar( new_value ) {\n" |
| 5124 | " flag = new_value;\n" |
| 5125 | " return \"Return from bar(\" + new_value + \")\";\n" |
| 5126 | "}\n" |
| 5127 | "\n" |
| 5128 | "function foo() {\n" |
| 5129 | " var x = 1;\n" |
| 5130 | " while ( flag == true ) {\n" |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5131 | " if ( x == 1 ) {\n" |
| 5132 | " ThreadedAtBarrier1();\n" |
| 5133 | " }\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5134 | " x = x + 1;\n" |
| 5135 | " }\n" |
| 5136 | "}\n" |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5137 | "\n" |
| 5138 | "foo();\n"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5139 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5140 | v8::V8::Initialize(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5141 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5142 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5143 | v8::Debug::SetMessageHandler2(&ThreadedMessageHandler); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5144 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 5145 | global_template->Set(v8::String::New("ThreadedAtBarrier1"), |
| 5146 | v8::FunctionTemplate::New(ThreadedAtBarrier1)); |
| 5147 | v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); |
| 5148 | v8::Context::Scope context_scope(context); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5149 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 5150 | CompileRun(source); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | void DebuggerThread::Run() { |
| 5154 | const int kBufSize = 1000; |
| 5155 | uint16_t buffer[kBufSize]; |
| 5156 | |
| 5157 | const char* command_1 = "{\"seq\":102," |
| 5158 | "\"type\":\"request\"," |
| 5159 | "\"command\":\"evaluate\"," |
| 5160 | "\"arguments\":{\"expression\":\"bar(false)\"}}"; |
| 5161 | const char* command_2 = "{\"seq\":103," |
| 5162 | "\"type\":\"request\"," |
| 5163 | "\"command\":\"continue\"}"; |
| 5164 | |
| 5165 | threaded_debugging_barriers.barrier_1.Wait(); |
| 5166 | v8::Debug::DebugBreak(); |
| 5167 | threaded_debugging_barriers.barrier_2.Wait(); |
| 5168 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
| 5169 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
| 5170 | } |
| 5171 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5172 | |
| 5173 | TEST(ThreadedDebugging) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5174 | DebuggerThread debugger_thread; |
| 5175 | V8Thread v8_thread; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5176 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5177 | // Create a V8 environment |
| 5178 | threaded_debugging_barriers.Initialize(); |
| 5179 | |
| 5180 | v8_thread.Start(); |
| 5181 | debugger_thread.Start(); |
| 5182 | |
| 5183 | v8_thread.Join(); |
| 5184 | debugger_thread.Join(); |
| 5185 | } |
| 5186 | |
| 5187 | /* Test RecursiveBreakpoints */ |
| 5188 | /* In this test, the debugger evaluates a function with a breakpoint, after |
| 5189 | * hitting a breakpoint in another function. We do this with both values |
| 5190 | * of the flag enabling recursive breakpoints, and verify that the second |
| 5191 | * breakpoint is hit when enabled, and missed when disabled. |
| 5192 | */ |
| 5193 | |
| 5194 | class BreakpointsV8Thread : public v8::internal::Thread { |
| 5195 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5196 | BreakpointsV8Thread() : Thread("BreakpointsV8Thread") { } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5197 | void Run(); |
| 5198 | }; |
| 5199 | |
| 5200 | class BreakpointsDebuggerThread : public v8::internal::Thread { |
| 5201 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5202 | explicit BreakpointsDebuggerThread(bool global_evaluate) |
| 5203 | : Thread("BreakpointsDebuggerThread"), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5204 | global_evaluate_(global_evaluate) {} |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5205 | void Run(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5206 | |
| 5207 | private: |
| 5208 | bool global_evaluate_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5209 | }; |
| 5210 | |
| 5211 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5212 | Barriers* breakpoints_barriers; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5213 | int break_event_breakpoint_id; |
| 5214 | int evaluate_int_result; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5215 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5216 | static void BreakpointsMessageHandler(const v8::Debug::Message& message) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5217 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5218 | v8::String::Value json(message.GetJSON()); |
| 5219 | Utf16ToAscii(*json, json.length(), print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5220 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 5221 | if (IsBreakEventMessage(print_buffer)) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5222 | break_event_breakpoint_id = |
| 5223 | GetBreakpointIdFromBreakEventMessage(print_buffer); |
| 5224 | breakpoints_barriers->semaphore_1->Signal(); |
| 5225 | } else if (IsEvaluateResponseMessage(print_buffer)) { |
| 5226 | evaluate_int_result = GetEvaluateIntResult(print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5227 | breakpoints_barriers->semaphore_1->Signal(); |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | |
| 5232 | void BreakpointsV8Thread::Run() { |
| 5233 | const char* source_1 = "var y_global = 3;\n" |
| 5234 | "function cat( new_value ) {\n" |
| 5235 | " var x = new_value;\n" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5236 | " y_global = y_global + 4;\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5237 | " x = 3 * x + 1;\n" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5238 | " y_global = y_global + 5;\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5239 | " return x;\n" |
| 5240 | "}\n" |
| 5241 | "\n" |
| 5242 | "function dog() {\n" |
| 5243 | " var x = 1;\n" |
| 5244 | " x = y_global;" |
| 5245 | " var z = 3;" |
| 5246 | " x += 100;\n" |
| 5247 | " return x;\n" |
| 5248 | "}\n" |
| 5249 | "\n"; |
| 5250 | const char* source_2 = "cat(17);\n" |
| 5251 | "cat(19);\n"; |
| 5252 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5253 | v8::V8::Initialize(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5254 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5255 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5256 | v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5257 | |
| 5258 | CompileRun(source_1); |
| 5259 | breakpoints_barriers->barrier_1.Wait(); |
| 5260 | breakpoints_barriers->barrier_2.Wait(); |
| 5261 | CompileRun(source_2); |
| 5262 | } |
| 5263 | |
| 5264 | |
| 5265 | void BreakpointsDebuggerThread::Run() { |
| 5266 | const int kBufSize = 1000; |
| 5267 | uint16_t buffer[kBufSize]; |
| 5268 | |
| 5269 | const char* command_1 = "{\"seq\":101," |
| 5270 | "\"type\":\"request\"," |
| 5271 | "\"command\":\"setbreakpoint\"," |
| 5272 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5273 | const char* command_2 = "{\"seq\":102," |
| 5274 | "\"type\":\"request\"," |
| 5275 | "\"command\":\"setbreakpoint\"," |
| 5276 | "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5277 | const char* command_3; |
| 5278 | if (this->global_evaluate_) { |
| 5279 | command_3 = "{\"seq\":103," |
| 5280 | "\"type\":\"request\"," |
| 5281 | "\"command\":\"evaluate\"," |
| 5282 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," |
| 5283 | "\"global\":true}}"; |
| 5284 | } else { |
| 5285 | command_3 = "{\"seq\":103," |
| 5286 | "\"type\":\"request\"," |
| 5287 | "\"command\":\"evaluate\"," |
| 5288 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; |
| 5289 | } |
| 5290 | const char* command_4; |
| 5291 | if (this->global_evaluate_) { |
| 5292 | command_4 = "{\"seq\":104," |
| 5293 | "\"type\":\"request\"," |
| 5294 | "\"command\":\"evaluate\"," |
| 5295 | "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," |
| 5296 | "\"global\":true}}"; |
| 5297 | } else { |
| 5298 | command_4 = "{\"seq\":104," |
| 5299 | "\"type\":\"request\"," |
| 5300 | "\"command\":\"evaluate\"," |
| 5301 | "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; |
| 5302 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5303 | const char* command_5 = "{\"seq\":105," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5304 | "\"type\":\"request\"," |
| 5305 | "\"command\":\"continue\"}"; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5306 | const char* command_6 = "{\"seq\":106," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5307 | "\"type\":\"request\"," |
| 5308 | "\"command\":\"continue\"}"; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5309 | const char* command_7; |
| 5310 | if (this->global_evaluate_) { |
| 5311 | command_7 = "{\"seq\":107," |
| 5312 | "\"type\":\"request\"," |
| 5313 | "\"command\":\"evaluate\"," |
| 5314 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," |
| 5315 | "\"global\":true}}"; |
| 5316 | } else { |
| 5317 | command_7 = "{\"seq\":107," |
| 5318 | "\"type\":\"request\"," |
| 5319 | "\"command\":\"evaluate\"," |
| 5320 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; |
| 5321 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5322 | const char* command_8 = "{\"seq\":108," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5323 | "\"type\":\"request\"," |
| 5324 | "\"command\":\"continue\"}"; |
| 5325 | |
| 5326 | |
| 5327 | // v8 thread initializes, runs source_1 |
| 5328 | breakpoints_barriers->barrier_1.Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5329 | // 1:Set breakpoint in cat() (will get id 1). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5330 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5331 | // 2:Set breakpoint in dog() (will get id 2). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5332 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5333 | breakpoints_barriers->barrier_2.Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5334 | // V8 thread starts compiling source_2. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5335 | // Automatic break happens, to run queued commands |
| 5336 | // breakpoints_barriers->semaphore_1->Wait(); |
| 5337 | // Commands 1 through 3 run, thread continues. |
| 5338 | // v8 thread runs source_2 to breakpoint in cat(). |
| 5339 | // message callback receives break event. |
| 5340 | breakpoints_barriers->semaphore_1->Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5341 | // Must have hit breakpoint #1. |
| 5342 | CHECK_EQ(1, break_event_breakpoint_id); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5343 | // 4:Evaluate dog() (which has a breakpoint). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5344 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5345 | // V8 thread hits breakpoint in dog(). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5346 | breakpoints_barriers->semaphore_1->Wait(); // wait for break event |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5347 | // Must have hit breakpoint #2. |
| 5348 | CHECK_EQ(2, break_event_breakpoint_id); |
| 5349 | // 5:Evaluate (x + 1). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5350 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5351 | // Evaluate (x + 1) finishes. |
| 5352 | breakpoints_barriers->semaphore_1->Wait(); |
| 5353 | // Must have result 108. |
| 5354 | CHECK_EQ(108, evaluate_int_result); |
| 5355 | // 6:Continue evaluation of dog(). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5356 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5357 | // Evaluate dog() finishes. |
| 5358 | breakpoints_barriers->semaphore_1->Wait(); |
| 5359 | // Must have result 107. |
| 5360 | CHECK_EQ(107, evaluate_int_result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5361 | // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint |
| 5362 | // in cat(19). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5363 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5364 | // Message callback gets break event. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5365 | breakpoints_barriers->semaphore_1->Wait(); // wait for break event |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5366 | // Must have hit breakpoint #1. |
| 5367 | CHECK_EQ(1, break_event_breakpoint_id); |
| 5368 | // 8: Evaluate dog() with breaks disabled. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5369 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5370 | // Evaluate dog() finishes. |
| 5371 | breakpoints_barriers->semaphore_1->Wait(); |
| 5372 | // Must have result 116. |
| 5373 | CHECK_EQ(116, evaluate_int_result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5374 | // 9: Continue evaluation of source2, reach end. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5375 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5378 | void TestRecursiveBreakpointsGeneric(bool global_evaluate) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5379 | i::FLAG_debugger_auto_break = true; |
| 5380 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5381 | BreakpointsDebuggerThread breakpoints_debugger_thread(global_evaluate); |
| 5382 | BreakpointsV8Thread breakpoints_v8_thread; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5383 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5384 | // Create a V8 environment |
| 5385 | Barriers stack_allocated_breakpoints_barriers; |
| 5386 | stack_allocated_breakpoints_barriers.Initialize(); |
| 5387 | breakpoints_barriers = &stack_allocated_breakpoints_barriers; |
| 5388 | |
| 5389 | breakpoints_v8_thread.Start(); |
| 5390 | breakpoints_debugger_thread.Start(); |
| 5391 | |
| 5392 | breakpoints_v8_thread.Join(); |
| 5393 | breakpoints_debugger_thread.Join(); |
| 5394 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5395 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5396 | TEST(RecursiveBreakpoints) { |
| 5397 | TestRecursiveBreakpointsGeneric(false); |
| 5398 | } |
| 5399 | |
| 5400 | TEST(RecursiveBreakpointsGlobal) { |
| 5401 | TestRecursiveBreakpointsGeneric(true); |
| 5402 | } |
| 5403 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5404 | |
| 5405 | static void DummyDebugEventListener(v8::DebugEvent event, |
| 5406 | v8::Handle<v8::Object> exec_state, |
| 5407 | v8::Handle<v8::Object> event_data, |
| 5408 | v8::Handle<v8::Value> data) { |
| 5409 | } |
| 5410 | |
| 5411 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5412 | TEST(SetDebugEventListenerOnUninitializedVM) { |
| 5413 | v8::Debug::SetDebugEventListener(DummyDebugEventListener); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5417 | static void DummyMessageHandler(const v8::Debug::Message& message) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | |
| 5421 | TEST(SetMessageHandlerOnUninitializedVM) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5422 | v8::Debug::SetMessageHandler2(DummyMessageHandler); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | |
| 5426 | TEST(DebugBreakOnUninitializedVM) { |
| 5427 | v8::Debug::DebugBreak(); |
| 5428 | } |
| 5429 | |
| 5430 | |
| 5431 | TEST(SendCommandToUninitializedVM) { |
| 5432 | const char* dummy_command = "{}"; |
| 5433 | uint16_t dummy_buffer[80]; |
| 5434 | int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer); |
| 5435 | v8::Debug::SendCommand(dummy_buffer, dummy_length); |
| 5436 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5437 | |
| 5438 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5439 | // Source for a JavaScript function which returns the data parameter of a |
| 5440 | // function called in the context of the debugger. If no data parameter is |
| 5441 | // passed it throws an exception. |
| 5442 | static const char* debugger_call_with_data_source = |
| 5443 | "function debugger_call_with_data(exec_state, data) {" |
| 5444 | " if (data) return data;" |
| 5445 | " throw 'No data!'" |
| 5446 | "}"; |
| 5447 | v8::Handle<v8::Function> debugger_call_with_data; |
| 5448 | |
| 5449 | |
| 5450 | // Source for a JavaScript function which returns the data parameter of a |
| 5451 | // function called in the context of the debugger. If no data parameter is |
| 5452 | // passed it throws an exception. |
| 5453 | static const char* debugger_call_with_closure_source = |
| 5454 | "var x = 3;" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5455 | "(function (exec_state) {" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5456 | " if (exec_state.y) return x - 1;" |
| 5457 | " exec_state.y = x;" |
| 5458 | " return exec_state.y" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5459 | "})"; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5460 | v8::Handle<v8::Function> debugger_call_with_closure; |
| 5461 | |
| 5462 | // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 5463 | // in the debugger. |
| 5464 | static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) { |
| 5465 | CHECK(v8::Debug::Call(frame_count)->IsNumber()); |
| 5466 | CHECK_EQ(args[0]->Int32Value(), |
| 5467 | v8::Debug::Call(frame_count)->Int32Value()); |
| 5468 | return v8::Undefined(); |
| 5469 | } |
| 5470 | |
| 5471 | |
| 5472 | // Function to retrieve the source line of the top JavaScript frame by calling a |
| 5473 | // JavaScript function in the debugger. |
| 5474 | static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) { |
| 5475 | CHECK(v8::Debug::Call(frame_source_line)->IsNumber()); |
| 5476 | CHECK_EQ(args[0]->Int32Value(), |
| 5477 | v8::Debug::Call(frame_source_line)->Int32Value()); |
| 5478 | return v8::Undefined(); |
| 5479 | } |
| 5480 | |
| 5481 | |
| 5482 | // Function to test passing an additional parameter to a JavaScript function |
| 5483 | // called in the debugger. It also tests that functions called in the debugger |
| 5484 | // can throw exceptions. |
| 5485 | static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) { |
| 5486 | v8::Handle<v8::String> data = v8::String::New("Test"); |
| 5487 | CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString()); |
| 5488 | |
| 5489 | CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 5490 | CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 5491 | |
| 5492 | v8::TryCatch catcher; |
| 5493 | v8::Debug::Call(debugger_call_with_data); |
| 5494 | CHECK(catcher.HasCaught()); |
| 5495 | CHECK(catcher.Exception()->IsString()); |
| 5496 | |
| 5497 | return v8::Undefined(); |
| 5498 | } |
| 5499 | |
| 5500 | |
| 5501 | // Function to test using a JavaScript with closure in the debugger. |
| 5502 | static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) { |
| 5503 | CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber()); |
| 5504 | CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value()); |
| 5505 | return v8::Undefined(); |
| 5506 | } |
| 5507 | |
| 5508 | |
| 5509 | // Test functions called through the debugger. |
| 5510 | TEST(CallFunctionInDebugger) { |
| 5511 | // Create and enter a context with the functions CheckFrameCount, |
| 5512 | // CheckSourceLine and CheckDataParameter installed. |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5513 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5514 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 5515 | global_template->Set(v8::String::New("CheckFrameCount"), |
| 5516 | v8::FunctionTemplate::New(CheckFrameCount)); |
| 5517 | global_template->Set(v8::String::New("CheckSourceLine"), |
| 5518 | v8::FunctionTemplate::New(CheckSourceLine)); |
| 5519 | global_template->Set(v8::String::New("CheckDataParameter"), |
| 5520 | v8::FunctionTemplate::New(CheckDataParameter)); |
| 5521 | global_template->Set(v8::String::New("CheckClosure"), |
| 5522 | v8::FunctionTemplate::New(CheckClosure)); |
| 5523 | v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); |
| 5524 | v8::Context::Scope context_scope(context); |
| 5525 | |
| 5526 | // Compile a function for checking the number of JavaScript frames. |
| 5527 | v8::Script::Compile(v8::String::New(frame_count_source))->Run(); |
| 5528 | frame_count = v8::Local<v8::Function>::Cast( |
| 5529 | context->Global()->Get(v8::String::New("frame_count"))); |
| 5530 | |
| 5531 | // Compile a function for returning the source line for the top frame. |
| 5532 | v8::Script::Compile(v8::String::New(frame_source_line_source))->Run(); |
| 5533 | frame_source_line = v8::Local<v8::Function>::Cast( |
| 5534 | context->Global()->Get(v8::String::New("frame_source_line"))); |
| 5535 | |
| 5536 | // Compile a function returning the data parameter. |
| 5537 | v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run(); |
| 5538 | debugger_call_with_data = v8::Local<v8::Function>::Cast( |
| 5539 | context->Global()->Get(v8::String::New("debugger_call_with_data"))); |
| 5540 | |
| 5541 | // Compile a function capturing closure. |
| 5542 | debugger_call_with_closure = v8::Local<v8::Function>::Cast( |
| 5543 | v8::Script::Compile( |
| 5544 | v8::String::New(debugger_call_with_closure_source))->Run()); |
| 5545 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 5546 | // Calling a function through the debugger returns 0 frames if there are |
| 5547 | // no JavaScript frames. |
| 5548 | CHECK_EQ(v8::Integer::New(0), v8::Debug::Call(frame_count)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5549 | |
| 5550 | // Test that the number of frames can be retrieved. |
| 5551 | v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run(); |
| 5552 | v8::Script::Compile(v8::String::New("function f() {" |
| 5553 | " CheckFrameCount(2);" |
| 5554 | "}; f()"))->Run(); |
| 5555 | |
| 5556 | // Test that the source line can be retrieved. |
| 5557 | v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run(); |
| 5558 | v8::Script::Compile(v8::String::New("function f() {\n" |
| 5559 | " CheckSourceLine(1)\n" |
| 5560 | " CheckSourceLine(2)\n" |
| 5561 | " CheckSourceLine(3)\n" |
| 5562 | "}; f()"))->Run(); |
| 5563 | |
| 5564 | // Test that a parameter can be passed to a function called in the debugger. |
| 5565 | v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run(); |
| 5566 | |
| 5567 | // Test that a function with closure can be run in the debugger. |
| 5568 | v8::Script::Compile(v8::String::New("CheckClosure()"))->Run(); |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 5569 | |
| 5570 | |
| 5571 | // Test that the source line is correct when there is a line offset. |
| 5572 | v8::ScriptOrigin origin(v8::String::New("test"), |
| 5573 | v8::Integer::New(7)); |
| 5574 | v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run(); |
| 5575 | v8::Script::Compile(v8::String::New("function f() {\n" |
| 5576 | " CheckSourceLine(8)\n" |
| 5577 | " CheckSourceLine(9)\n" |
| 5578 | " CheckSourceLine(10)\n" |
| 5579 | "}; f()"), &origin)->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5580 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5581 | |
| 5582 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5583 | // Debugger message handler which counts the number of breaks. |
| 5584 | static void SendContinueCommand(); |
| 5585 | static void MessageHandlerBreakPointHitCount( |
| 5586 | const v8::Debug::Message& message) { |
| 5587 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 5588 | // Count the number of breaks. |
| 5589 | break_point_hit_count++; |
| 5590 | |
| 5591 | SendContinueCommand(); |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5596 | // Test that clearing the debug event listener actually clears all break points |
| 5597 | // and related information. |
| 5598 | TEST(DebuggerUnload) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5599 | DebugLocalContext env; |
| 5600 | |
| 5601 | // Check debugger is unloaded before it is used. |
| 5602 | CheckDebuggerUnloaded(); |
| 5603 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5604 | // Set a debug event listener. |
| 5605 | break_point_hit_count = 0; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5606 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 5607 | v8::Undefined()); |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5608 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5609 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5610 | // Create a couple of functions for the test. |
| 5611 | v8::Local<v8::Function> foo = |
| 5612 | CompileFunction(&env, "function foo(){x=1}", "foo"); |
| 5613 | v8::Local<v8::Function> bar = |
| 5614 | CompileFunction(&env, "function bar(){y=2}", "bar"); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5615 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5616 | // Set some break points. |
| 5617 | SetBreakPoint(foo, 0); |
| 5618 | SetBreakPoint(foo, 4); |
| 5619 | SetBreakPoint(bar, 0); |
| 5620 | SetBreakPoint(bar, 4); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5621 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5622 | // Make sure that the break points are there. |
| 5623 | break_point_hit_count = 0; |
| 5624 | foo->Call(env->Global(), 0, NULL); |
| 5625 | CHECK_EQ(2, break_point_hit_count); |
| 5626 | bar->Call(env->Global(), 0, NULL); |
| 5627 | CHECK_EQ(4, break_point_hit_count); |
| 5628 | } |
sgjesse@chromium.org | f457d1f | 2009-06-17 14:18:36 +0000 | [diff] [blame] | 5629 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5630 | // Remove the debug event listener without clearing breakpoints. Do this |
| 5631 | // outside a handle scope. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5632 | v8::Debug::SetDebugEventListener(NULL); |
| 5633 | CheckDebuggerUnloaded(true); |
| 5634 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5635 | // Now set a debug message handler. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5636 | break_point_hit_count = 0; |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5637 | v8::Debug::SetMessageHandler2(MessageHandlerBreakPointHitCount); |
| 5638 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5639 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5640 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5641 | // Get the test functions again. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5642 | v8::Local<v8::Function> foo(v8::Local<v8::Function>::Cast( |
| 5643 | env->Global()->Get(v8::String::New("foo")))); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5644 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5645 | foo->Call(env->Global(), 0, NULL); |
| 5646 | CHECK_EQ(0, break_point_hit_count); |
| 5647 | |
| 5648 | // Set break points and run again. |
| 5649 | SetBreakPoint(foo, 0); |
| 5650 | SetBreakPoint(foo, 4); |
| 5651 | foo->Call(env->Global(), 0, NULL); |
| 5652 | CHECK_EQ(2, break_point_hit_count); |
| 5653 | } |
| 5654 | |
| 5655 | // Remove the debug message handler without clearing breakpoints. Do this |
| 5656 | // outside a handle scope. |
| 5657 | v8::Debug::SetMessageHandler2(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5658 | CheckDebuggerUnloaded(true); |
| 5659 | } |
| 5660 | |
| 5661 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5662 | // Sends continue command to the debugger. |
| 5663 | static void SendContinueCommand() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5664 | const int kBufferSize = 1000; |
| 5665 | uint16_t buffer[kBufferSize]; |
| 5666 | const char* command_continue = |
| 5667 | "{\"seq\":0," |
| 5668 | "\"type\":\"request\"," |
| 5669 | "\"command\":\"continue\"}"; |
| 5670 | |
| 5671 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); |
| 5672 | } |
| 5673 | |
| 5674 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5675 | // Debugger message handler which counts the number of times it is called. |
| 5676 | static int message_handler_hit_count = 0; |
| 5677 | static void MessageHandlerHitCount(const v8::Debug::Message& message) { |
| 5678 | message_handler_hit_count++; |
| 5679 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5680 | static char print_buffer[1000]; |
| 5681 | v8::String::Value json(message.GetJSON()); |
| 5682 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5683 | if (IsExceptionEventMessage(print_buffer)) { |
| 5684 | // Send a continue command for exception events. |
| 5685 | SendContinueCommand(); |
| 5686 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5690 | // Test clearing the debug message handler. |
| 5691 | TEST(DebuggerClearMessageHandler) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5692 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5693 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5694 | |
| 5695 | // Check debugger is unloaded before it is used. |
| 5696 | CheckDebuggerUnloaded(); |
| 5697 | |
| 5698 | // Set a debug message handler. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5699 | v8::Debug::SetMessageHandler2(MessageHandlerHitCount); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5700 | |
| 5701 | // Run code to throw a unhandled exception. This should end up in the message |
| 5702 | // handler. |
| 5703 | CompileRun("throw 1"); |
| 5704 | |
| 5705 | // The message handler should be called. |
| 5706 | CHECK_GT(message_handler_hit_count, 0); |
| 5707 | |
| 5708 | // Clear debug message handler. |
| 5709 | message_handler_hit_count = 0; |
| 5710 | v8::Debug::SetMessageHandler(NULL); |
| 5711 | |
| 5712 | // Run code to throw a unhandled exception. This should end up in the message |
| 5713 | // handler. |
| 5714 | CompileRun("throw 1"); |
| 5715 | |
| 5716 | // The message handler should not be called more. |
| 5717 | CHECK_EQ(0, message_handler_hit_count); |
| 5718 | |
| 5719 | CheckDebuggerUnloaded(true); |
| 5720 | } |
| 5721 | |
| 5722 | |
| 5723 | // Debugger message handler which clears the message handler while active. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5724 | static void MessageHandlerClearingMessageHandler( |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5725 | const v8::Debug::Message& message) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5726 | message_handler_hit_count++; |
| 5727 | |
| 5728 | // Clear debug message handler. |
| 5729 | v8::Debug::SetMessageHandler(NULL); |
| 5730 | } |
| 5731 | |
| 5732 | |
| 5733 | // Test clearing the debug message handler while processing a debug event. |
| 5734 | TEST(DebuggerClearMessageHandlerWhileActive) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5735 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5736 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5737 | |
| 5738 | // Check debugger is unloaded before it is used. |
| 5739 | CheckDebuggerUnloaded(); |
| 5740 | |
| 5741 | // Set a debug message handler. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5742 | v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5743 | |
| 5744 | // Run code to throw a unhandled exception. This should end up in the message |
| 5745 | // handler. |
| 5746 | CompileRun("throw 1"); |
| 5747 | |
| 5748 | // The message handler should be called. |
| 5749 | CHECK_EQ(1, message_handler_hit_count); |
| 5750 | |
| 5751 | CheckDebuggerUnloaded(true); |
| 5752 | } |
| 5753 | |
| 5754 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5755 | /* Test DebuggerHostDispatch */ |
| 5756 | /* In this test, the debugger waits for a command on a breakpoint |
| 5757 | * and is dispatching host commands while in the infinite loop. |
| 5758 | */ |
| 5759 | |
| 5760 | class HostDispatchV8Thread : public v8::internal::Thread { |
| 5761 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5762 | HostDispatchV8Thread() : Thread("HostDispatchV8Thread") { } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5763 | void Run(); |
| 5764 | }; |
| 5765 | |
| 5766 | class HostDispatchDebuggerThread : public v8::internal::Thread { |
| 5767 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5768 | HostDispatchDebuggerThread() : Thread("HostDispatchDebuggerThread") { } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5769 | void Run(); |
| 5770 | }; |
| 5771 | |
| 5772 | Barriers* host_dispatch_barriers; |
| 5773 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5774 | static void HostDispatchMessageHandler(const v8::Debug::Message& message) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5775 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5776 | v8::String::Value json(message.GetJSON()); |
| 5777 | Utf16ToAscii(*json, json.length(), print_buffer); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5781 | static void HostDispatchDispatchHandler() { |
| 5782 | host_dispatch_barriers->semaphore_1->Signal(); |
| 5783 | } |
| 5784 | |
| 5785 | |
| 5786 | void HostDispatchV8Thread::Run() { |
| 5787 | const char* source_1 = "var y_global = 3;\n" |
| 5788 | "function cat( new_value ) {\n" |
| 5789 | " var x = new_value;\n" |
| 5790 | " y_global = 4;\n" |
| 5791 | " x = 3 * x + 1;\n" |
| 5792 | " y_global = 5;\n" |
| 5793 | " return x;\n" |
| 5794 | "}\n" |
| 5795 | "\n"; |
| 5796 | const char* source_2 = "cat(17);\n"; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5797 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5798 | v8::V8::Initialize(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5799 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5800 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5801 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5802 | // Set up message and host dispatch handlers. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5803 | v8::Debug::SetMessageHandler2(HostDispatchMessageHandler); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5804 | v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5805 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5806 | CompileRun(source_1); |
| 5807 | host_dispatch_barriers->barrier_1.Wait(); |
| 5808 | host_dispatch_barriers->barrier_2.Wait(); |
| 5809 | CompileRun(source_2); |
| 5810 | } |
sgjesse@chromium.org | 3afc158 | 2009-04-16 22:31:44 +0000 | [diff] [blame] | 5811 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5812 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5813 | void HostDispatchDebuggerThread::Run() { |
| 5814 | const int kBufSize = 1000; |
| 5815 | uint16_t buffer[kBufSize]; |
sgjesse@chromium.org | 3afc158 | 2009-04-16 22:31:44 +0000 | [diff] [blame] | 5816 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5817 | const char* command_1 = "{\"seq\":101," |
| 5818 | "\"type\":\"request\"," |
| 5819 | "\"command\":\"setbreakpoint\"," |
| 5820 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5821 | const char* command_2 = "{\"seq\":102," |
| 5822 | "\"type\":\"request\"," |
| 5823 | "\"command\":\"continue\"}"; |
| 5824 | |
| 5825 | // v8 thread initializes, runs source_1 |
| 5826 | host_dispatch_barriers->barrier_1.Wait(); |
| 5827 | // 1: Set breakpoint in cat(). |
| 5828 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
| 5829 | |
| 5830 | host_dispatch_barriers->barrier_2.Wait(); |
| 5831 | // v8 thread starts compiling source_2. |
| 5832 | // Break happens, to run queued commands and host dispatches. |
| 5833 | // Wait for host dispatch to be processed. |
| 5834 | host_dispatch_barriers->semaphore_1->Wait(); |
| 5835 | // 2: Continue evaluation |
| 5836 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
| 5837 | } |
| 5838 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5839 | |
| 5840 | TEST(DebuggerHostDispatch) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5841 | HostDispatchDebuggerThread host_dispatch_debugger_thread; |
| 5842 | HostDispatchV8Thread host_dispatch_v8_thread; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5843 | i::FLAG_debugger_auto_break = true; |
| 5844 | |
| 5845 | // Create a V8 environment |
| 5846 | Barriers stack_allocated_host_dispatch_barriers; |
| 5847 | stack_allocated_host_dispatch_barriers.Initialize(); |
| 5848 | host_dispatch_barriers = &stack_allocated_host_dispatch_barriers; |
| 5849 | |
| 5850 | host_dispatch_v8_thread.Start(); |
| 5851 | host_dispatch_debugger_thread.Start(); |
| 5852 | |
| 5853 | host_dispatch_v8_thread.Join(); |
| 5854 | host_dispatch_debugger_thread.Join(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5855 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5856 | |
| 5857 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5858 | /* Test DebugMessageDispatch */ |
| 5859 | /* In this test, the V8 thread waits for a message from the debug thread. |
| 5860 | * The DebugMessageDispatchHandler is executed from the debugger thread |
| 5861 | * which signals the V8 thread to wake up. |
| 5862 | */ |
| 5863 | |
| 5864 | class DebugMessageDispatchV8Thread : public v8::internal::Thread { |
| 5865 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5866 | DebugMessageDispatchV8Thread() : Thread("DebugMessageDispatchV8Thread") { } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5867 | void Run(); |
| 5868 | }; |
| 5869 | |
| 5870 | class DebugMessageDispatchDebuggerThread : public v8::internal::Thread { |
| 5871 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5872 | DebugMessageDispatchDebuggerThread() |
| 5873 | : Thread("DebugMessageDispatchDebuggerThread") { } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5874 | void Run(); |
| 5875 | }; |
| 5876 | |
| 5877 | Barriers* debug_message_dispatch_barriers; |
| 5878 | |
| 5879 | |
| 5880 | static void DebugMessageHandler() { |
| 5881 | debug_message_dispatch_barriers->semaphore_1->Signal(); |
| 5882 | } |
| 5883 | |
| 5884 | |
| 5885 | void DebugMessageDispatchV8Thread::Run() { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5886 | v8::V8::Initialize(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5887 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5888 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5889 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5890 | // Set up debug message dispatch handler. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5891 | v8::Debug::SetDebugMessageDispatchHandler(DebugMessageHandler); |
| 5892 | |
| 5893 | CompileRun("var y = 1 + 2;\n"); |
| 5894 | debug_message_dispatch_barriers->barrier_1.Wait(); |
| 5895 | debug_message_dispatch_barriers->semaphore_1->Wait(); |
| 5896 | debug_message_dispatch_barriers->barrier_2.Wait(); |
| 5897 | } |
| 5898 | |
| 5899 | |
| 5900 | void DebugMessageDispatchDebuggerThread::Run() { |
| 5901 | debug_message_dispatch_barriers->barrier_1.Wait(); |
| 5902 | SendContinueCommand(); |
| 5903 | debug_message_dispatch_barriers->barrier_2.Wait(); |
| 5904 | } |
| 5905 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5906 | |
| 5907 | TEST(DebuggerDebugMessageDispatch) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5908 | DebugMessageDispatchDebuggerThread debug_message_dispatch_debugger_thread; |
| 5909 | DebugMessageDispatchV8Thread debug_message_dispatch_v8_thread; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5910 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5911 | i::FLAG_debugger_auto_break = true; |
| 5912 | |
| 5913 | // Create a V8 environment |
| 5914 | Barriers stack_allocated_debug_message_dispatch_barriers; |
| 5915 | stack_allocated_debug_message_dispatch_barriers.Initialize(); |
| 5916 | debug_message_dispatch_barriers = |
| 5917 | &stack_allocated_debug_message_dispatch_barriers; |
| 5918 | |
| 5919 | debug_message_dispatch_v8_thread.Start(); |
| 5920 | debug_message_dispatch_debugger_thread.Start(); |
| 5921 | |
| 5922 | debug_message_dispatch_v8_thread.Join(); |
| 5923 | debug_message_dispatch_debugger_thread.Join(); |
| 5924 | } |
| 5925 | |
| 5926 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5927 | TEST(DebuggerAgent) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5928 | v8::V8::Initialize(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5929 | i::Debugger* debugger = i::Isolate::Current()->debugger(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5930 | // Make sure these ports is not used by other tests to allow tests to run in |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5931 | // parallel. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 5932 | const int kPort1 = 5858 + FlagDependentPortOffset(); |
| 5933 | const int kPort2 = 5857 + FlagDependentPortOffset(); |
| 5934 | const int kPort3 = 5856 + FlagDependentPortOffset(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5935 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5936 | // Make a string with the port2 number. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5937 | const int kPortBufferLen = 6; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5938 | char port2_str[kPortBufferLen]; |
| 5939 | OS::SNPrintF(i::Vector<char>(port2_str, kPortBufferLen), "%d", kPort2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5940 | |
| 5941 | bool ok; |
| 5942 | |
| 5943 | // Initialize the socket library. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5944 | i::Socket::SetUp(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Test starting and stopping the agent without any client connection. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5947 | debugger->StartAgent("test", kPort1); |
| 5948 | debugger->StopAgent(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5949 | // Test starting the agent, connecting a client and shutting down the agent |
| 5950 | // with the client connected. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5951 | ok = debugger->StartAgent("test", kPort2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5952 | CHECK(ok); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5953 | debugger->WaitForAgent(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5954 | i::Socket* client = i::OS::CreateSocket(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5955 | ok = client->Connect("localhost", port2_str); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5956 | CHECK(ok); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5957 | // It is important to wait for a message from the agent. Otherwise, |
| 5958 | // we can close the server socket during "accept" syscall, making it failing |
| 5959 | // (at least on Linux), and the test will work incorrectly. |
| 5960 | char buf; |
| 5961 | ok = client->Receive(&buf, 1) == 1; |
| 5962 | CHECK(ok); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5963 | debugger->StopAgent(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5964 | delete client; |
| 5965 | |
| 5966 | // Test starting and stopping the agent with the required port already |
| 5967 | // occoupied. |
| 5968 | i::Socket* server = i::OS::CreateSocket(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5969 | server->Bind(kPort3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5970 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5971 | debugger->StartAgent("test", kPort3); |
| 5972 | debugger->StopAgent(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5973 | |
| 5974 | delete server; |
| 5975 | } |
| 5976 | |
| 5977 | |
| 5978 | class DebuggerAgentProtocolServerThread : public i::Thread { |
| 5979 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5980 | explicit DebuggerAgentProtocolServerThread(int port) |
| 5981 | : Thread("DebuggerAgentProtocolServerThread"), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5982 | port_(port), |
| 5983 | server_(NULL), |
| 5984 | client_(NULL), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5985 | listening_(OS::CreateSemaphore(0)) { |
| 5986 | } |
| 5987 | ~DebuggerAgentProtocolServerThread() { |
| 5988 | // Close both sockets. |
| 5989 | delete client_; |
| 5990 | delete server_; |
| 5991 | delete listening_; |
| 5992 | } |
| 5993 | |
| 5994 | void Run(); |
| 5995 | void WaitForListening() { listening_->Wait(); } |
| 5996 | char* body() { return *body_; } |
| 5997 | |
| 5998 | private: |
| 5999 | int port_; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 6000 | i::SmartArrayPointer<char> body_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6001 | i::Socket* server_; // Server socket used for bind/accept. |
| 6002 | i::Socket* client_; // Single client connection used by the test. |
| 6003 | i::Semaphore* listening_; // Signalled when the server is in listen mode. |
| 6004 | }; |
| 6005 | |
| 6006 | |
| 6007 | void DebuggerAgentProtocolServerThread::Run() { |
| 6008 | bool ok; |
| 6009 | |
| 6010 | // Create the server socket and bind it to the requested port. |
| 6011 | server_ = i::OS::CreateSocket(); |
| 6012 | CHECK(server_ != NULL); |
| 6013 | ok = server_->Bind(port_); |
| 6014 | CHECK(ok); |
| 6015 | |
| 6016 | // Listen for new connections. |
| 6017 | ok = server_->Listen(1); |
| 6018 | CHECK(ok); |
| 6019 | listening_->Signal(); |
| 6020 | |
| 6021 | // Accept a connection. |
| 6022 | client_ = server_->Accept(); |
| 6023 | CHECK(client_ != NULL); |
| 6024 | |
| 6025 | // Receive a debugger agent protocol message. |
| 6026 | i::DebuggerAgentUtil::ReceiveMessage(client_); |
| 6027 | } |
| 6028 | |
| 6029 | |
| 6030 | TEST(DebuggerAgentProtocolOverflowHeader) { |
| 6031 | // Make sure this port is not used by other tests to allow tests to run in |
| 6032 | // parallel. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6033 | const int kPort = 5860 + FlagDependentPortOffset(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6034 | static const char* kLocalhost = "localhost"; |
| 6035 | |
| 6036 | // Make a string with the port number. |
| 6037 | const int kPortBufferLen = 6; |
| 6038 | char port_str[kPortBufferLen]; |
| 6039 | OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort); |
| 6040 | |
| 6041 | // Initialize the socket library. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6042 | i::Socket::SetUp(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6043 | |
| 6044 | // Create a socket server to receive a debugger agent message. |
| 6045 | DebuggerAgentProtocolServerThread* server = |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6046 | new DebuggerAgentProtocolServerThread(kPort); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6047 | server->Start(); |
| 6048 | server->WaitForListening(); |
| 6049 | |
| 6050 | // Connect. |
| 6051 | i::Socket* client = i::OS::CreateSocket(); |
| 6052 | CHECK(client != NULL); |
| 6053 | bool ok = client->Connect(kLocalhost, port_str); |
| 6054 | CHECK(ok); |
| 6055 | |
| 6056 | // Send headers which overflow the receive buffer. |
| 6057 | static const int kBufferSize = 1000; |
| 6058 | char buffer[kBufferSize]; |
| 6059 | |
| 6060 | // Long key and short value: XXXX....XXXX:0\r\n. |
| 6061 | for (int i = 0; i < kBufferSize - 4; i++) { |
| 6062 | buffer[i] = 'X'; |
| 6063 | } |
| 6064 | buffer[kBufferSize - 4] = ':'; |
| 6065 | buffer[kBufferSize - 3] = '0'; |
| 6066 | buffer[kBufferSize - 2] = '\r'; |
| 6067 | buffer[kBufferSize - 1] = '\n'; |
| 6068 | client->Send(buffer, kBufferSize); |
| 6069 | |
| 6070 | // Short key and long value: X:XXXX....XXXX\r\n. |
| 6071 | buffer[0] = 'X'; |
| 6072 | buffer[1] = ':'; |
| 6073 | for (int i = 2; i < kBufferSize - 2; i++) { |
| 6074 | buffer[i] = 'X'; |
| 6075 | } |
| 6076 | buffer[kBufferSize - 2] = '\r'; |
| 6077 | buffer[kBufferSize - 1] = '\n'; |
| 6078 | client->Send(buffer, kBufferSize); |
| 6079 | |
| 6080 | // Add empty body to request. |
| 6081 | const char* content_length_zero_header = "Content-Length:0\r\n"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6082 | client->Send(content_length_zero_header, |
| 6083 | StrLength(content_length_zero_header)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6084 | client->Send("\r\n", 2); |
| 6085 | |
| 6086 | // Wait until data is received. |
| 6087 | server->Join(); |
| 6088 | |
| 6089 | // Check for empty body. |
| 6090 | CHECK(server->body() == NULL); |
| 6091 | |
| 6092 | // Close the client before the server to avoid TIME_WAIT issues. |
| 6093 | client->Shutdown(); |
| 6094 | delete client; |
| 6095 | delete server; |
| 6096 | } |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6097 | |
| 6098 | |
| 6099 | // Test for issue http://code.google.com/p/v8/issues/detail?id=289. |
| 6100 | // Make sure that DebugGetLoadedScripts doesn't return scripts |
| 6101 | // with disposed external source. |
| 6102 | class EmptyExternalStringResource : public v8::String::ExternalStringResource { |
| 6103 | public: |
| 6104 | EmptyExternalStringResource() { empty_[0] = 0; } |
| 6105 | virtual ~EmptyExternalStringResource() {} |
| 6106 | virtual size_t length() const { return empty_.length(); } |
| 6107 | virtual const uint16_t* data() const { return empty_.start(); } |
| 6108 | private: |
| 6109 | ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; |
| 6110 | }; |
| 6111 | |
| 6112 | |
| 6113 | TEST(DebugGetLoadedScripts) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6114 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6115 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6116 | env.ExposeDebug(); |
| 6117 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6118 | EmptyExternalStringResource source_ext_str; |
| 6119 | v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6120 | v8::Handle<v8::Script> evil_script(v8::Script::Compile(source)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6121 | // "use" evil_script to make the compiler happy. |
| 6122 | (void) evil_script; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6123 | Handle<i::ExternalTwoByteString> i_source( |
| 6124 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 6125 | // This situation can happen if source was an external string disposed |
| 6126 | // by its owner. |
| 6127 | i_source->set_resource(0); |
| 6128 | |
| 6129 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 6130 | i::FLAG_allow_natives_syntax = true; |
| 6131 | CompileRun( |
| 6132 | "var scripts = %DebugGetLoadedScripts();" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6133 | "var count = scripts.length;" |
| 6134 | "for (var i = 0; i < count; ++i) {" |
| 6135 | " scripts[i].line_ends;" |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6136 | "}"); |
| 6137 | // Must not crash while accessing line_ends. |
| 6138 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6139 | |
| 6140 | // Some scripts are retrieved - at least the number of native scripts. |
| 6141 | CHECK_GT((*env)->Global()->Get(v8::String::New("count"))->Int32Value(), 8); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 6142 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6143 | |
| 6144 | |
| 6145 | // Test script break points set on lines. |
| 6146 | TEST(ScriptNameAndData) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6147 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6148 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6149 | env.ExposeDebug(); |
| 6150 | |
| 6151 | // Create functions for retrieving script name and data for the function on |
| 6152 | // the top frame when hitting a break point. |
| 6153 | frame_script_name = CompileFunction(&env, |
| 6154 | frame_script_name_source, |
| 6155 | "frame_script_name"); |
| 6156 | frame_script_data = CompileFunction(&env, |
| 6157 | frame_script_data_source, |
| 6158 | "frame_script_data"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6159 | compiled_script_data = CompileFunction(&env, |
| 6160 | compiled_script_data_source, |
| 6161 | "compiled_script_data"); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6162 | |
| 6163 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 6164 | v8::Undefined()); |
| 6165 | |
| 6166 | // Test function source. |
| 6167 | v8::Local<v8::String> script = v8::String::New( |
| 6168 | "function f() {\n" |
| 6169 | " debugger;\n" |
| 6170 | "}\n"); |
| 6171 | |
| 6172 | v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name")); |
| 6173 | v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1); |
| 6174 | script1->SetData(v8::String::New("data")); |
| 6175 | script1->Run(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6176 | v8::Local<v8::Function> f; |
| 6177 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6178 | |
| 6179 | f->Call(env->Global(), 0, NULL); |
| 6180 | CHECK_EQ(1, break_point_hit_count); |
| 6181 | CHECK_EQ("name", last_script_name_hit); |
| 6182 | CHECK_EQ("data", last_script_data_hit); |
| 6183 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6184 | // Compile the same script again without setting data. As the compilation |
| 6185 | // cache is disabled when debugging expect the data to be missing. |
| 6186 | v8::Script::Compile(script, &origin1)->Run(); |
| 6187 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6188 | f->Call(env->Global(), 0, NULL); |
| 6189 | CHECK_EQ(2, break_point_hit_count); |
| 6190 | CHECK_EQ("name", last_script_name_hit); |
| 6191 | CHECK_EQ("", last_script_data_hit); // Undefined results in empty string. |
| 6192 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6193 | v8::Local<v8::String> data_obj_source = v8::String::New( |
| 6194 | "({ a: 'abc',\n" |
| 6195 | " b: 123,\n" |
| 6196 | " toString: function() { return this.a + ' ' + this.b; }\n" |
| 6197 | "})\n"); |
| 6198 | v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run(); |
| 6199 | v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name")); |
| 6200 | v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2); |
| 6201 | script2->Run(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 6202 | script2->SetData(data_obj->ToString()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6203 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6204 | f->Call(env->Global(), 0, NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6205 | CHECK_EQ(3, break_point_hit_count); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6206 | CHECK_EQ("new name", last_script_name_hit); |
| 6207 | CHECK_EQ("abc 123", last_script_data_hit); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6208 | |
| 6209 | v8::Handle<v8::Script> script3 = |
| 6210 | v8::Script::Compile(script, &origin2, NULL, |
| 6211 | v8::String::New("in compile")); |
| 6212 | CHECK_EQ("in compile", last_script_data_hit); |
| 6213 | script3->Run(); |
| 6214 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6215 | f->Call(env->Global(), 0, NULL); |
| 6216 | CHECK_EQ(4, break_point_hit_count); |
| 6217 | CHECK_EQ("in compile", last_script_data_hit); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6218 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6219 | |
| 6220 | |
| 6221 | static v8::Persistent<v8::Context> expected_context; |
| 6222 | static v8::Handle<v8::Value> expected_context_data; |
| 6223 | |
| 6224 | |
| 6225 | // Check that the expected context is the one generating the debug event. |
| 6226 | static void ContextCheckMessageHandler(const v8::Debug::Message& message) { |
| 6227 | CHECK(message.GetEventContext() == expected_context); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6228 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6229 | expected_context_data)); |
| 6230 | message_handler_hit_count++; |
| 6231 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6232 | static char print_buffer[1000]; |
| 6233 | v8::String::Value json(message.GetJSON()); |
| 6234 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6235 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6236 | // Send a continue command for break events. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6237 | if (IsBreakEventMessage(print_buffer)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6238 | SendContinueCommand(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | |
| 6243 | // Test which creates two contexts and sets different embedder data on each. |
| 6244 | // Checks that this data is set correctly and that when the debug message |
| 6245 | // handler is called the expected context is the one active. |
| 6246 | TEST(ContextData) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6247 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6248 | |
| 6249 | v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); |
| 6250 | |
| 6251 | // Create two contexts. |
| 6252 | v8::Persistent<v8::Context> context_1; |
| 6253 | v8::Persistent<v8::Context> context_2; |
| 6254 | v8::Handle<v8::ObjectTemplate> global_template = |
| 6255 | v8::Handle<v8::ObjectTemplate>(); |
| 6256 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); |
| 6257 | context_1 = v8::Context::New(NULL, global_template, global_object); |
| 6258 | context_2 = v8::Context::New(NULL, global_template, global_object); |
| 6259 | |
| 6260 | // Default data value is undefined. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6261 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
| 6262 | CHECK(context_2->GetEmbedderData(0)->IsUndefined()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6263 | |
| 6264 | // Set and check different data values. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 6265 | v8::Handle<v8::String> data_1 = v8::String::New("1"); |
| 6266 | v8::Handle<v8::String> data_2 = v8::String::New("2"); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6267 | context_1->SetEmbedderData(0, data_1); |
| 6268 | context_2->SetEmbedderData(0, data_2); |
| 6269 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
| 6270 | CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6271 | |
| 6272 | // Simple test function which causes a break. |
| 6273 | const char* source = "function f() { debugger; }"; |
| 6274 | |
| 6275 | // Enter and run function in the first context. |
| 6276 | { |
| 6277 | v8::Context::Scope context_scope(context_1); |
| 6278 | expected_context = context_1; |
| 6279 | expected_context_data = data_1; |
| 6280 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6281 | f->Call(context_1->Global(), 0, NULL); |
| 6282 | } |
| 6283 | |
| 6284 | |
| 6285 | // Enter and run function in the second context. |
| 6286 | { |
| 6287 | v8::Context::Scope context_scope(context_2); |
| 6288 | expected_context = context_2; |
| 6289 | expected_context_data = data_2; |
| 6290 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6291 | f->Call(context_2->Global(), 0, NULL); |
| 6292 | } |
| 6293 | |
| 6294 | // Two times compile event and two times break event. |
| 6295 | CHECK_GT(message_handler_hit_count, 4); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6296 | |
| 6297 | v8::Debug::SetMessageHandler2(NULL); |
| 6298 | CheckDebuggerUnloaded(); |
| 6299 | } |
| 6300 | |
| 6301 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6302 | // Debug message handler which issues a debug break when it hits a break event. |
| 6303 | static int message_handler_break_hit_count = 0; |
| 6304 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
| 6305 | // Schedule a debug break for break events. |
| 6306 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6307 | message_handler_break_hit_count++; |
| 6308 | if (message_handler_break_hit_count == 1) { |
| 6309 | v8::Debug::DebugBreak(); |
| 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | // Issue a continue command if this event will not cause the VM to start |
| 6314 | // running. |
| 6315 | if (!message.WillStartRunning()) { |
| 6316 | SendContinueCommand(); |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | |
| 6321 | // Test that a debug break can be scheduled while in a message handler. |
| 6322 | TEST(DebugBreakInMessageHandler) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6323 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6324 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6325 | |
| 6326 | v8::Debug::SetMessageHandler2(DebugBreakMessageHandler); |
| 6327 | |
| 6328 | // Test functions. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6329 | const char* script = "function f() { debugger; g(); } function g() { }"; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6330 | CompileRun(script); |
| 6331 | v8::Local<v8::Function> f = |
| 6332 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6333 | v8::Local<v8::Function> g = |
| 6334 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 6335 | |
| 6336 | // Call f then g. The debugger statement in f will casue a break which will |
| 6337 | // cause another break. |
| 6338 | f->Call(env->Global(), 0, NULL); |
| 6339 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6340 | // Calling g will not cause any additional breaks. |
| 6341 | g->Call(env->Global(), 0, NULL); |
| 6342 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6343 | } |
| 6344 | |
| 6345 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6346 | #ifndef V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6347 | // Debug event handler which gets the function on the top frame and schedules a |
| 6348 | // break a number of times. |
| 6349 | static void DebugEventDebugBreak( |
| 6350 | v8::DebugEvent event, |
| 6351 | v8::Handle<v8::Object> exec_state, |
| 6352 | v8::Handle<v8::Object> event_data, |
| 6353 | v8::Handle<v8::Value> data) { |
| 6354 | |
| 6355 | if (event == v8::Break) { |
| 6356 | break_point_hit_count++; |
| 6357 | |
| 6358 | // Get the name of the top frame function. |
| 6359 | if (!frame_function_name.IsEmpty()) { |
| 6360 | // Get the name of the function. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6361 | const int argc = 2; |
| 6362 | v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6363 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 6364 | argc, argv); |
| 6365 | if (result->IsUndefined()) { |
| 6366 | last_function_hit[0] = '\0'; |
| 6367 | } else { |
| 6368 | CHECK(result->IsString()); |
| 6369 | v8::Handle<v8::String> function_name(result->ToString()); |
| 6370 | function_name->WriteAscii(last_function_hit); |
| 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | // Keep forcing breaks. |
| 6375 | if (break_point_hit_count < 20) { |
| 6376 | v8::Debug::DebugBreak(); |
| 6377 | } |
| 6378 | } |
| 6379 | } |
| 6380 | |
| 6381 | |
| 6382 | TEST(RegExpDebugBreak) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 6383 | // This test only applies to native regexps. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6384 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6385 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6386 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6387 | // Create a function for checking the function when hitting a break point. |
| 6388 | frame_function_name = CompileFunction(&env, |
| 6389 | frame_function_name_source, |
| 6390 | "frame_function_name"); |
| 6391 | |
| 6392 | // Test RegExp which matches white spaces and comments at the begining of a |
| 6393 | // source line. |
| 6394 | const char* script = |
| 6395 | "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" |
| 6396 | "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; |
| 6397 | |
| 6398 | v8::Local<v8::Function> f = CompileFunction(script, "f"); |
| 6399 | const int argc = 1; |
| 6400 | v8::Handle<v8::Value> argv[argc] = { v8::String::New(" /* xxx */ a=0;") }; |
| 6401 | v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv); |
| 6402 | CHECK_EQ(12, result->Int32Value()); |
| 6403 | |
| 6404 | v8::Debug::SetDebugEventListener(DebugEventDebugBreak); |
| 6405 | v8::Debug::DebugBreak(); |
| 6406 | result = f->Call(env->Global(), argc, argv); |
| 6407 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6408 | // Check that there was only one break event. Matching RegExp should not |
| 6409 | // cause Break events. |
| 6410 | CHECK_EQ(1, break_point_hit_count); |
| 6411 | CHECK_EQ("f", last_function_hit); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6412 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6413 | #endif // V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6414 | |
| 6415 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6416 | // Common part of EvalContextData and NestedBreakEventContextData tests. |
| 6417 | static void ExecuteScriptForContextCheck() { |
| 6418 | // Create a context. |
| 6419 | v8::Persistent<v8::Context> context_1; |
| 6420 | v8::Handle<v8::ObjectTemplate> global_template = |
| 6421 | v8::Handle<v8::ObjectTemplate>(); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 6422 | context_1 = v8::Context::New(NULL, global_template); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6423 | |
| 6424 | // Default data value is undefined. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6425 | CHECK(context_1->GetEmbedderData(0)->IsUndefined()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6426 | |
| 6427 | // Set and check a data value. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 6428 | v8::Handle<v8::String> data_1 = v8::String::New("1"); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6429 | context_1->SetEmbedderData(0, data_1); |
| 6430 | CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6431 | |
| 6432 | // Simple test function with eval that causes a break. |
| 6433 | const char* source = "function f() { eval('debugger;'); }"; |
| 6434 | |
| 6435 | // Enter and run function in the context. |
| 6436 | { |
| 6437 | v8::Context::Scope context_scope(context_1); |
| 6438 | expected_context = context_1; |
| 6439 | expected_context_data = data_1; |
| 6440 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6441 | f->Call(context_1->Global(), 0, NULL); |
| 6442 | } |
| 6443 | } |
| 6444 | |
| 6445 | |
| 6446 | // Test which creates a context and sets embedder data on it. Checks that this |
| 6447 | // data is set correctly and that when the debug message handler is called for |
| 6448 | // break event in an eval statement the expected context is the one returned by |
| 6449 | // Message.GetEventContext. |
| 6450 | TEST(EvalContextData) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6451 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6452 | v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); |
| 6453 | |
| 6454 | ExecuteScriptForContextCheck(); |
| 6455 | |
| 6456 | // One time compile event and one time break event. |
| 6457 | CHECK_GT(message_handler_hit_count, 2); |
| 6458 | v8::Debug::SetMessageHandler2(NULL); |
| 6459 | CheckDebuggerUnloaded(); |
| 6460 | } |
| 6461 | |
| 6462 | |
| 6463 | static bool sent_eval = false; |
| 6464 | static int break_count = 0; |
| 6465 | static int continue_command_send_count = 0; |
| 6466 | // Check that the expected context is the one generating the debug event |
| 6467 | // including the case of nested break event. |
| 6468 | static void DebugEvalContextCheckMessageHandler( |
| 6469 | const v8::Debug::Message& message) { |
| 6470 | CHECK(message.GetEventContext() == expected_context); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 6471 | CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals( |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6472 | expected_context_data)); |
| 6473 | message_handler_hit_count++; |
| 6474 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6475 | static char print_buffer[1000]; |
| 6476 | v8::String::Value json(message.GetJSON()); |
| 6477 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6478 | |
| 6479 | if (IsBreakEventMessage(print_buffer)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6480 | break_count++; |
| 6481 | if (!sent_eval) { |
| 6482 | sent_eval = true; |
| 6483 | |
| 6484 | const int kBufferSize = 1000; |
| 6485 | uint16_t buffer[kBufferSize]; |
| 6486 | const char* eval_command = |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 6487 | "{\"seq\":0," |
| 6488 | "\"type\":\"request\"," |
| 6489 | "\"command\":\"evaluate\"," |
| 6490 | "\"arguments\":{\"expression\":\"debugger;\"," |
| 6491 | "\"global\":true,\"disable_break\":false}}"; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6492 | |
| 6493 | // Send evaluate command. |
| 6494 | v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer)); |
| 6495 | return; |
| 6496 | } else { |
| 6497 | // It's a break event caused by the evaluation request above. |
| 6498 | SendContinueCommand(); |
| 6499 | continue_command_send_count++; |
| 6500 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6501 | } else if (IsEvaluateResponseMessage(print_buffer) && |
| 6502 | continue_command_send_count < 2) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6503 | // Response to the evaluation request. We're still on the breakpoint so |
| 6504 | // send continue. |
| 6505 | SendContinueCommand(); |
| 6506 | continue_command_send_count++; |
| 6507 | } |
| 6508 | } |
| 6509 | |
| 6510 | |
| 6511 | // Tests that context returned for break event is correct when the event occurs |
| 6512 | // in 'evaluate' debugger request. |
| 6513 | TEST(NestedBreakEventContextData) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6514 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6515 | break_count = 0; |
| 6516 | message_handler_hit_count = 0; |
| 6517 | v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler); |
| 6518 | |
| 6519 | ExecuteScriptForContextCheck(); |
| 6520 | |
| 6521 | // One time compile event and two times break event. |
| 6522 | CHECK_GT(message_handler_hit_count, 3); |
| 6523 | |
| 6524 | // One break from the source and another from the evaluate request. |
| 6525 | CHECK_EQ(break_count, 2); |
| 6526 | v8::Debug::SetMessageHandler2(NULL); |
| 6527 | CheckDebuggerUnloaded(); |
| 6528 | } |
| 6529 | |
| 6530 | |
| 6531 | // Debug event listener which counts the script collected events. |
| 6532 | int script_collected_count = 0; |
| 6533 | static void DebugEventScriptCollectedEvent(v8::DebugEvent event, |
| 6534 | v8::Handle<v8::Object> exec_state, |
| 6535 | v8::Handle<v8::Object> event_data, |
| 6536 | v8::Handle<v8::Value> data) { |
| 6537 | // Count the number of breaks. |
| 6538 | if (event == v8::ScriptCollected) { |
| 6539 | script_collected_count++; |
| 6540 | } |
| 6541 | } |
| 6542 | |
| 6543 | |
| 6544 | // Test that scripts collected are reported through the debug event listener. |
| 6545 | TEST(ScriptCollectedEvent) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6546 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6547 | break_point_hit_count = 0; |
| 6548 | script_collected_count = 0; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6549 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6550 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6551 | |
| 6552 | // Request the loaded scripts to initialize the debugger script cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6553 | debug->GetLoadedScripts(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6554 | |
| 6555 | // Do garbage collection to ensure that only the script in this test will be |
| 6556 | // collected afterwards. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6557 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6558 | |
| 6559 | script_collected_count = 0; |
| 6560 | v8::Debug::SetDebugEventListener(DebugEventScriptCollectedEvent, |
| 6561 | v8::Undefined()); |
| 6562 | { |
| 6563 | v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); |
| 6564 | v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); |
| 6565 | } |
| 6566 | |
| 6567 | // Do garbage collection to collect the script above which is no longer |
| 6568 | // referenced. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6569 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6570 | |
| 6571 | CHECK_EQ(2, script_collected_count); |
| 6572 | |
| 6573 | v8::Debug::SetDebugEventListener(NULL); |
| 6574 | CheckDebuggerUnloaded(); |
| 6575 | } |
| 6576 | |
| 6577 | |
| 6578 | // Debug event listener which counts the script collected events. |
| 6579 | int script_collected_message_count = 0; |
| 6580 | static void ScriptCollectedMessageHandler(const v8::Debug::Message& message) { |
| 6581 | // Count the number of scripts collected. |
| 6582 | if (message.IsEvent() && message.GetEvent() == v8::ScriptCollected) { |
| 6583 | script_collected_message_count++; |
| 6584 | v8::Handle<v8::Context> context = message.GetEventContext(); |
| 6585 | CHECK(context.IsEmpty()); |
| 6586 | } |
| 6587 | } |
| 6588 | |
| 6589 | |
| 6590 | // Test that GetEventContext doesn't fail and return empty handle for |
| 6591 | // ScriptCollected events. |
| 6592 | TEST(ScriptCollectedEventContext) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6593 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 6594 | v8::internal::Debug* debug = |
| 6595 | reinterpret_cast<v8::internal::Isolate*>(isolate)->debug(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6596 | script_collected_message_count = 0; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6597 | v8::HandleScope scope(isolate); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6598 | |
| 6599 | { // Scope for the DebugLocalContext. |
| 6600 | DebugLocalContext env; |
| 6601 | |
| 6602 | // Request the loaded scripts to initialize the debugger script cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6603 | debug->GetLoadedScripts(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6604 | |
| 6605 | // Do garbage collection to ensure that only the script in this test will be |
| 6606 | // collected afterwards. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6607 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6608 | |
| 6609 | v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler); |
| 6610 | { |
| 6611 | v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); |
| 6612 | v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | // Do garbage collection to collect the script above which is no longer |
| 6617 | // referenced. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6618 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6619 | |
| 6620 | CHECK_EQ(2, script_collected_message_count); |
| 6621 | |
| 6622 | v8::Debug::SetMessageHandler2(NULL); |
| 6623 | } |
| 6624 | |
| 6625 | |
| 6626 | // Debug event listener which counts the after compile events. |
| 6627 | int after_compile_message_count = 0; |
| 6628 | static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 6629 | // Count the number of scripts collected. |
| 6630 | if (message.IsEvent()) { |
| 6631 | if (message.GetEvent() == v8::AfterCompile) { |
| 6632 | after_compile_message_count++; |
| 6633 | } else if (message.GetEvent() == v8::Break) { |
| 6634 | SendContinueCommand(); |
| 6635 | } |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | |
| 6640 | // Tests that after compile event is sent as many times as there are scripts |
| 6641 | // compiled. |
| 6642 | TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6643 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6644 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6645 | after_compile_message_count = 0; |
| 6646 | const char* script = "var a=1"; |
| 6647 | |
| 6648 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6649 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6650 | v8::Debug::SetMessageHandler2(NULL); |
| 6651 | |
| 6652 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6653 | v8::Debug::DebugBreak(); |
| 6654 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6655 | |
| 6656 | // Setting listener to NULL should cause debugger unload. |
| 6657 | v8::Debug::SetMessageHandler2(NULL); |
| 6658 | CheckDebuggerUnloaded(); |
| 6659 | |
| 6660 | // Compilation cache should be disabled when debugger is active. |
| 6661 | CHECK_EQ(2, after_compile_message_count); |
| 6662 | } |
| 6663 | |
| 6664 | |
| 6665 | // Tests that break event is sent when message handler is reset. |
| 6666 | TEST(BreakMessageWhenMessageHandlerIsReset) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6667 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6668 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6669 | after_compile_message_count = 0; |
| 6670 | const char* script = "function f() {};"; |
| 6671 | |
| 6672 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6673 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6674 | v8::Debug::SetMessageHandler2(NULL); |
| 6675 | |
| 6676 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6677 | v8::Debug::DebugBreak(); |
| 6678 | v8::Local<v8::Function> f = |
| 6679 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6680 | f->Call(env->Global(), 0, NULL); |
| 6681 | |
| 6682 | // Setting message handler to NULL should cause debugger unload. |
| 6683 | v8::Debug::SetMessageHandler2(NULL); |
| 6684 | CheckDebuggerUnloaded(); |
| 6685 | |
| 6686 | // Compilation cache should be disabled when debugger is active. |
| 6687 | CHECK_EQ(1, after_compile_message_count); |
| 6688 | } |
| 6689 | |
| 6690 | |
| 6691 | static int exception_event_count = 0; |
| 6692 | static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 6693 | if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 6694 | exception_event_count++; |
| 6695 | SendContinueCommand(); |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | |
| 6700 | // Tests that exception event is sent when message handler is reset. |
| 6701 | TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6702 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6703 | v8::HandleScope scope(env->GetIsolate()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6704 | |
| 6705 | // For this test, we want to break on uncaught exceptions: |
| 6706 | ChangeBreakOnException(false, true); |
| 6707 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6708 | exception_event_count = 0; |
| 6709 | const char* script = "function f() {throw new Error()};"; |
| 6710 | |
| 6711 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6712 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6713 | v8::Debug::SetMessageHandler2(NULL); |
| 6714 | |
| 6715 | v8::Debug::SetMessageHandler2(ExceptionMessageHandler); |
| 6716 | v8::Local<v8::Function> f = |
| 6717 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6718 | f->Call(env->Global(), 0, NULL); |
| 6719 | |
| 6720 | // Setting message handler to NULL should cause debugger unload. |
| 6721 | v8::Debug::SetMessageHandler2(NULL); |
| 6722 | CheckDebuggerUnloaded(); |
| 6723 | |
| 6724 | CHECK_EQ(1, exception_event_count); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6725 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6726 | |
| 6727 | |
| 6728 | // Tests after compile event is sent when there are some provisional |
| 6729 | // breakpoints out of the scripts lines range. |
| 6730 | TEST(ProvisionalBreakpointOnLineOutOfRange) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6731 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6732 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6733 | env.ExposeDebug(); |
| 6734 | const char* script = "function f() {};"; |
| 6735 | const char* resource_name = "test_resource"; |
| 6736 | |
| 6737 | // Set a couple of provisional breakpoint on lines out of the script lines |
| 6738 | // range. |
| 6739 | int sbp1 = SetScriptBreakPointByNameFromJS(resource_name, 3, |
| 6740 | -1 /* no column */); |
| 6741 | int sbp2 = SetScriptBreakPointByNameFromJS(resource_name, 5, 5); |
| 6742 | |
| 6743 | after_compile_message_count = 0; |
| 6744 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6745 | |
| 6746 | v8::ScriptOrigin origin( |
| 6747 | v8::String::New(resource_name), |
| 6748 | v8::Integer::New(10), |
| 6749 | v8::Integer::New(1)); |
| 6750 | // Compile a script whose first line number is greater than the breakpoints' |
| 6751 | // lines. |
| 6752 | v8::Script::Compile(v8::String::New(script), &origin)->Run(); |
| 6753 | |
| 6754 | // If the script is compiled successfully there is exactly one after compile |
| 6755 | // event. In case of an exception in debugger code after compile event is not |
| 6756 | // sent. |
| 6757 | CHECK_EQ(1, after_compile_message_count); |
| 6758 | |
| 6759 | ClearBreakPointFromJS(sbp1); |
| 6760 | ClearBreakPointFromJS(sbp2); |
| 6761 | v8::Debug::SetMessageHandler2(NULL); |
| 6762 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6763 | |
| 6764 | |
| 6765 | static void BreakMessageHandler(const v8::Debug::Message& message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6766 | i::Isolate* isolate = i::Isolate::Current(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6767 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6768 | // Count the number of breaks. |
| 6769 | break_point_hit_count++; |
| 6770 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6771 | i::HandleScope scope(isolate); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6772 | message.GetJSON(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6773 | |
| 6774 | SendContinueCommand(); |
| 6775 | } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6776 | i::HandleScope scope(isolate); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6777 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6778 | bool is_debug_break = isolate->stack_guard()->IsDebugBreak(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6779 | // Force DebugBreak flag while serializer is working. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6780 | isolate->stack_guard()->DebugBreak(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6781 | |
| 6782 | // Force serialization to trigger some internal JS execution. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6783 | message.GetJSON(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6784 | |
| 6785 | // Restore previous state. |
| 6786 | if (is_debug_break) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6787 | isolate->stack_guard()->DebugBreak(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6788 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6789 | isolate->stack_guard()->Continue(i::DEBUGBREAK); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6790 | } |
| 6791 | } |
| 6792 | } |
| 6793 | |
| 6794 | |
| 6795 | // Test that if DebugBreak is forced it is ignored when code from |
| 6796 | // debug-delay.js is executed. |
| 6797 | TEST(NoDebugBreakInAfterCompileMessageHandler) { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6798 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6799 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6800 | |
| 6801 | // Register a debug event listener which sets the break flag and counts. |
| 6802 | v8::Debug::SetMessageHandler2(BreakMessageHandler); |
| 6803 | |
| 6804 | // Set the debug break flag. |
| 6805 | v8::Debug::DebugBreak(); |
| 6806 | |
| 6807 | // Create a function for testing stepping. |
| 6808 | const char* src = "function f() { eval('var x = 10;'); } "; |
| 6809 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 6810 | |
| 6811 | // There should be only one break event. |
| 6812 | CHECK_EQ(1, break_point_hit_count); |
| 6813 | |
| 6814 | // Set the debug break flag again. |
| 6815 | v8::Debug::DebugBreak(); |
| 6816 | f->Call(env->Global(), 0, NULL); |
| 6817 | // There should be one more break event when the script is evaluated in 'f'. |
| 6818 | CHECK_EQ(2, break_point_hit_count); |
| 6819 | |
| 6820 | // Get rid of the debug message handler. |
| 6821 | v8::Debug::SetMessageHandler2(NULL); |
| 6822 | CheckDebuggerUnloaded(); |
| 6823 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6824 | |
| 6825 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6826 | static int counting_message_handler_counter; |
| 6827 | |
| 6828 | static void CountingMessageHandler(const v8::Debug::Message& message) { |
| 6829 | counting_message_handler_counter++; |
| 6830 | } |
| 6831 | |
| 6832 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6833 | TEST(ProcessDebugMessages) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6834 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6835 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6836 | |
| 6837 | counting_message_handler_counter = 0; |
| 6838 | |
| 6839 | v8::Debug::SetMessageHandler2(CountingMessageHandler); |
| 6840 | |
| 6841 | const int kBufferSize = 1000; |
| 6842 | uint16_t buffer[kBufferSize]; |
| 6843 | const char* scripts_command = |
| 6844 | "{\"seq\":0," |
| 6845 | "\"type\":\"request\"," |
| 6846 | "\"command\":\"scripts\"}"; |
| 6847 | |
| 6848 | // Send scripts command. |
| 6849 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6850 | |
| 6851 | CHECK_EQ(0, counting_message_handler_counter); |
| 6852 | v8::Debug::ProcessDebugMessages(); |
| 6853 | // At least one message should come |
| 6854 | CHECK_GE(counting_message_handler_counter, 1); |
| 6855 | |
| 6856 | counting_message_handler_counter = 0; |
| 6857 | |
| 6858 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6859 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6860 | CHECK_EQ(0, counting_message_handler_counter); |
| 6861 | v8::Debug::ProcessDebugMessages(); |
| 6862 | // At least two messages should come |
| 6863 | CHECK_GE(counting_message_handler_counter, 2); |
| 6864 | |
| 6865 | // Get rid of the debug message handler. |
| 6866 | v8::Debug::SetMessageHandler2(NULL); |
| 6867 | CheckDebuggerUnloaded(); |
| 6868 | } |
| 6869 | |
| 6870 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6871 | struct BacktraceData { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6872 | static int frame_counter; |
| 6873 | static void MessageHandler(const v8::Debug::Message& message) { |
| 6874 | char print_buffer[1000]; |
| 6875 | v8::String::Value json(message.GetJSON()); |
| 6876 | Utf16ToAscii(*json, json.length(), print_buffer, 1000); |
| 6877 | |
| 6878 | if (strstr(print_buffer, "backtrace") == NULL) { |
| 6879 | return; |
| 6880 | } |
| 6881 | frame_counter = GetTotalFramesInt(print_buffer); |
| 6882 | } |
| 6883 | }; |
| 6884 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6885 | int BacktraceData::frame_counter; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6886 | |
| 6887 | |
| 6888 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6889 | TEST(Backtrace) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6890 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6891 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6892 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6893 | v8::Debug::SetMessageHandler2(BacktraceData::MessageHandler); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6894 | |
| 6895 | const int kBufferSize = 1000; |
| 6896 | uint16_t buffer[kBufferSize]; |
| 6897 | const char* scripts_command = |
| 6898 | "{\"seq\":0," |
| 6899 | "\"type\":\"request\"," |
| 6900 | "\"command\":\"backtrace\"}"; |
| 6901 | |
| 6902 | // Check backtrace from ProcessDebugMessages. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6903 | BacktraceData::frame_counter = -10; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6904 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6905 | v8::Debug::ProcessDebugMessages(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6906 | CHECK_EQ(BacktraceData::frame_counter, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6907 | |
| 6908 | v8::Handle<v8::String> void0 = v8::String::New("void(0)"); |
| 6909 | v8::Handle<v8::Script> script = v8::Script::Compile(void0, void0); |
| 6910 | |
| 6911 | // Check backtrace from "void(0)" script. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6912 | BacktraceData::frame_counter = -10; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6913 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6914 | script->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6915 | CHECK_EQ(BacktraceData::frame_counter, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6916 | |
| 6917 | // Get rid of the debug message handler. |
| 6918 | v8::Debug::SetMessageHandler2(NULL); |
| 6919 | CheckDebuggerUnloaded(); |
| 6920 | } |
| 6921 | |
| 6922 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6923 | TEST(GetMirror) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6924 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6925 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6926 | v8::Handle<v8::Value> obj = v8::Debug::GetMirror(v8::String::New("hodja")); |
| 6927 | v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast( |
| 6928 | v8::Script::New( |
| 6929 | v8::String::New( |
| 6930 | "function runTest(mirror) {" |
| 6931 | " return mirror.isString() && (mirror.length() == 5);" |
| 6932 | "}" |
| 6933 | "" |
| 6934 | "runTest;"))->Run()); |
| 6935 | v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj); |
| 6936 | CHECK(result->IsTrue()); |
| 6937 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6938 | |
| 6939 | |
| 6940 | // Test that the debug break flag works with function.apply. |
| 6941 | TEST(DebugBreakFunctionApply) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6942 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6943 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6944 | |
| 6945 | // Create a function for testing breaking in apply. |
| 6946 | v8::Local<v8::Function> foo = CompileFunction( |
| 6947 | &env, |
| 6948 | "function baz(x) { }" |
| 6949 | "function bar(x) { baz(); }" |
| 6950 | "function foo(){ bar.apply(this, [1]); }", |
| 6951 | "foo"); |
| 6952 | |
| 6953 | // Register a debug event listener which steps and counts. |
| 6954 | v8::Debug::SetDebugEventListener(DebugEventBreakMax); |
| 6955 | |
| 6956 | // Set the debug break flag before calling the code using function.apply. |
| 6957 | v8::Debug::DebugBreak(); |
| 6958 | |
| 6959 | // Limit the number of debug breaks. This is a regression test for issue 493 |
| 6960 | // where this test would enter an infinite loop. |
| 6961 | break_point_hit_count = 0; |
| 6962 | max_break_point_hit_count = 10000; // 10000 => infinite loop. |
| 6963 | foo->Call(env->Global(), 0, NULL); |
| 6964 | |
| 6965 | // When keeping the debug break several break will happen. |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6966 | CHECK_GT(break_point_hit_count, 1); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6967 | |
| 6968 | v8::Debug::SetDebugEventListener(NULL); |
| 6969 | CheckDebuggerUnloaded(); |
| 6970 | } |
| 6971 | |
| 6972 | |
| 6973 | v8::Handle<v8::Context> debugee_context; |
| 6974 | v8::Handle<v8::Context> debugger_context; |
| 6975 | |
| 6976 | |
| 6977 | // Property getter that checks that current and calling contexts |
| 6978 | // are both the debugee contexts. |
| 6979 | static v8::Handle<v8::Value> NamedGetterWithCallingContextCheck( |
| 6980 | v8::Local<v8::String> name, |
| 6981 | const v8::AccessorInfo& info) { |
| 6982 | CHECK_EQ(0, strcmp(*v8::String::AsciiValue(name), "a")); |
| 6983 | v8::Handle<v8::Context> current = v8::Context::GetCurrent(); |
| 6984 | CHECK(current == debugee_context); |
| 6985 | CHECK(current != debugger_context); |
| 6986 | v8::Handle<v8::Context> calling = v8::Context::GetCalling(); |
| 6987 | CHECK(calling == debugee_context); |
| 6988 | CHECK(calling != debugger_context); |
| 6989 | return v8::Int32::New(1); |
| 6990 | } |
| 6991 | |
| 6992 | |
| 6993 | // Debug event listener that checks if the first argument of a function is |
| 6994 | // an object with property 'a' == 1. If the property has custom accessor |
| 6995 | // this handler will eventually invoke it. |
| 6996 | static void DebugEventGetAtgumentPropertyValue( |
| 6997 | v8::DebugEvent event, |
| 6998 | v8::Handle<v8::Object> exec_state, |
| 6999 | v8::Handle<v8::Object> event_data, |
| 7000 | v8::Handle<v8::Value> data) { |
| 7001 | if (event == v8::Break) { |
| 7002 | break_point_hit_count++; |
| 7003 | CHECK(debugger_context == v8::Context::GetCurrent()); |
| 7004 | v8::Handle<v8::Function> func(v8::Function::Cast(*CompileRun( |
| 7005 | "(function(exec_state) {\n" |
| 7006 | " return (exec_state.frame(0).argumentValue(0).property('a').\n" |
| 7007 | " value().value() == 1);\n" |
| 7008 | "})"))); |
| 7009 | const int argc = 1; |
| 7010 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 7011 | v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv); |
| 7012 | CHECK(result->IsTrue()); |
| 7013 | } |
| 7014 | } |
| 7015 | |
| 7016 | |
| 7017 | TEST(CallingContextIsNotDebugContext) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7018 | v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7019 | // Create and enter a debugee context. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7020 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7021 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7022 | env.ExposeDebug(); |
| 7023 | |
| 7024 | // Save handles to the debugger and debugee contexts to be used in |
| 7025 | // NamedGetterWithCallingContextCheck. |
| 7026 | debugee_context = v8::Local<v8::Context>(*env); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7027 | debugger_context = v8::Utils::ToLocal(debug->debug_context()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7028 | |
| 7029 | // Create object with 'a' property accessor. |
| 7030 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 7031 | named->SetAccessor(v8::String::New("a"), |
| 7032 | NamedGetterWithCallingContextCheck); |
| 7033 | env->Global()->Set(v8::String::New("obj"), |
| 7034 | named->NewInstance()); |
| 7035 | |
| 7036 | // Register the debug event listener |
| 7037 | v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue); |
| 7038 | |
| 7039 | // Create a function that invokes debugger. |
| 7040 | v8::Local<v8::Function> foo = CompileFunction( |
| 7041 | &env, |
| 7042 | "function bar(x) { debugger; }" |
| 7043 | "function foo(){ bar(obj); }", |
| 7044 | "foo"); |
| 7045 | |
| 7046 | break_point_hit_count = 0; |
| 7047 | foo->Call(env->Global(), 0, NULL); |
| 7048 | CHECK_EQ(1, break_point_hit_count); |
| 7049 | |
| 7050 | v8::Debug::SetDebugEventListener(NULL); |
| 7051 | debugee_context = v8::Handle<v8::Context>(); |
| 7052 | debugger_context = v8::Handle<v8::Context>(); |
| 7053 | CheckDebuggerUnloaded(); |
| 7054 | } |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7055 | |
| 7056 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7057 | TEST(DebugContextIsPreservedBetweenAccesses) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7058 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7059 | v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext(); |
| 7060 | v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext(); |
| 7061 | CHECK_EQ(*context1, *context2); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
| 7064 | |
| 7065 | static v8::Handle<v8::Value> expected_callback_data; |
| 7066 | static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { |
| 7067 | CHECK(details.GetEventContext() == expected_context); |
| 7068 | CHECK_EQ(expected_callback_data, details.GetCallbackData()); |
| 7069 | } |
| 7070 | |
| 7071 | // Check that event details contain context where debug event occured. |
| 7072 | TEST(DebugEventContext) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7073 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 7074 | expected_callback_data = v8::Int32::New(2010); |
| 7075 | v8::Debug::SetDebugEventListener2(DebugEventContextChecker, |
| 7076 | expected_callback_data); |
| 7077 | expected_context = v8::Context::New(); |
| 7078 | v8::Context::Scope context_scope(expected_context); |
| 7079 | v8::Script::Compile(v8::String::New("(function(){debugger;})();"))->Run(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 7080 | expected_context.Dispose(expected_context->GetIsolate()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 7081 | expected_context.Clear(); |
| 7082 | v8::Debug::SetDebugEventListener(NULL); |
| 7083 | expected_context_data = v8::Handle<v8::Value>(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7084 | CheckDebuggerUnloaded(); |
| 7085 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 7086 | |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 7087 | |
| 7088 | static void* expected_break_data; |
| 7089 | static bool was_debug_break_called; |
| 7090 | static bool was_debug_event_called; |
| 7091 | static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) { |
| 7092 | if (details.GetEvent() == v8::BreakForCommand) { |
| 7093 | CHECK_EQ(expected_break_data, details.GetClientData()); |
| 7094 | was_debug_event_called = true; |
| 7095 | } else if (details.GetEvent() == v8::Break) { |
| 7096 | was_debug_break_called = true; |
| 7097 | } |
| 7098 | } |
| 7099 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7100 | |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 7101 | // Check that event details contain context where debug event occured. |
| 7102 | TEST(DebugEventBreakData) { |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 7103 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7104 | v8::HandleScope scope(env->GetIsolate()); |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 7105 | v8::Debug::SetDebugEventListener2(DebugEventBreakDataChecker); |
| 7106 | |
| 7107 | TestClientData::constructor_call_counter = 0; |
| 7108 | TestClientData::destructor_call_counter = 0; |
| 7109 | |
| 7110 | expected_break_data = NULL; |
| 7111 | was_debug_event_called = false; |
| 7112 | was_debug_break_called = false; |
| 7113 | v8::Debug::DebugBreakForCommand(); |
| 7114 | v8::Script::Compile(v8::String::New("(function(x){return x;})(1);"))->Run(); |
| 7115 | CHECK(was_debug_event_called); |
| 7116 | CHECK(!was_debug_break_called); |
| 7117 | |
| 7118 | TestClientData* data1 = new TestClientData(); |
| 7119 | expected_break_data = data1; |
| 7120 | was_debug_event_called = false; |
| 7121 | was_debug_break_called = false; |
| 7122 | v8::Debug::DebugBreakForCommand(data1); |
| 7123 | v8::Script::Compile(v8::String::New("(function(x){return x+1;})(1);"))->Run(); |
| 7124 | CHECK(was_debug_event_called); |
| 7125 | CHECK(!was_debug_break_called); |
| 7126 | |
| 7127 | expected_break_data = NULL; |
| 7128 | was_debug_event_called = false; |
| 7129 | was_debug_break_called = false; |
| 7130 | v8::Debug::DebugBreak(); |
| 7131 | v8::Script::Compile(v8::String::New("(function(x){return x+2;})(1);"))->Run(); |
| 7132 | CHECK(!was_debug_event_called); |
| 7133 | CHECK(was_debug_break_called); |
| 7134 | |
| 7135 | TestClientData* data2 = new TestClientData(); |
| 7136 | expected_break_data = data2; |
| 7137 | was_debug_event_called = false; |
| 7138 | was_debug_break_called = false; |
| 7139 | v8::Debug::DebugBreak(); |
| 7140 | v8::Debug::DebugBreakForCommand(data2); |
| 7141 | v8::Script::Compile(v8::String::New("(function(x){return x+3;})(1);"))->Run(); |
| 7142 | CHECK(was_debug_event_called); |
| 7143 | CHECK(was_debug_break_called); |
| 7144 | |
| 7145 | CHECK_EQ(2, TestClientData::constructor_call_counter); |
| 7146 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 7147 | TestClientData::destructor_call_counter); |
| 7148 | |
| 7149 | v8::Debug::SetDebugEventListener(NULL); |
| 7150 | CheckDebuggerUnloaded(); |
| 7151 | } |
| 7152 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7153 | static bool debug_event_break_deoptimize_done = false; |
| 7154 | |
| 7155 | static void DebugEventBreakDeoptimize(v8::DebugEvent event, |
| 7156 | v8::Handle<v8::Object> exec_state, |
| 7157 | v8::Handle<v8::Object> event_data, |
| 7158 | v8::Handle<v8::Value> data) { |
| 7159 | if (event == v8::Break) { |
| 7160 | if (!frame_function_name.IsEmpty()) { |
| 7161 | // Get the name of the function. |
| 7162 | const int argc = 2; |
| 7163 | v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; |
| 7164 | v8::Handle<v8::Value> result = |
| 7165 | frame_function_name->Call(exec_state, argc, argv); |
| 7166 | if (!result->IsUndefined()) { |
| 7167 | char fn[80]; |
| 7168 | CHECK(result->IsString()); |
| 7169 | v8::Handle<v8::String> function_name(result->ToString()); |
| 7170 | function_name->WriteAscii(fn); |
| 7171 | if (strcmp(fn, "bar") == 0) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 7172 | i::Deoptimizer::DeoptimizeAll(v8::internal::Isolate::Current()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7173 | debug_event_break_deoptimize_done = true; |
| 7174 | } |
| 7175 | } |
| 7176 | } |
| 7177 | |
| 7178 | v8::Debug::DebugBreak(); |
| 7179 | } |
| 7180 | } |
| 7181 | |
| 7182 | |
| 7183 | // Test deoptimization when execution is broken using the debug break stack |
| 7184 | // check interrupt. |
| 7185 | TEST(DeoptimizeDuringDebugBreak) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7186 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7187 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7188 | env.ExposeDebug(); |
| 7189 | |
| 7190 | // Create a function for checking the function when hitting a break point. |
| 7191 | frame_function_name = CompileFunction(&env, |
| 7192 | frame_function_name_source, |
| 7193 | "frame_function_name"); |
| 7194 | |
| 7195 | |
| 7196 | // Set a debug event listener which will keep interrupting execution until |
| 7197 | // debug break. When inside function bar it will deoptimize all functions. |
| 7198 | // This tests lazy deoptimization bailout for the stack check, as the first |
| 7199 | // time in function bar when using debug break and no break points will be at |
| 7200 | // the initial stack check. |
| 7201 | v8::Debug::SetDebugEventListener(DebugEventBreakDeoptimize, |
| 7202 | v8::Undefined()); |
| 7203 | |
| 7204 | // Compile and run function bar which will optimize it for some flag settings. |
| 7205 | v8::Script::Compile(v8::String::New("function bar(){}; bar()"))->Run(); |
| 7206 | |
| 7207 | // Set debug break and call bar again. |
| 7208 | v8::Debug::DebugBreak(); |
| 7209 | v8::Script::Compile(v8::String::New("bar()"))->Run(); |
| 7210 | |
| 7211 | CHECK(debug_event_break_deoptimize_done); |
| 7212 | |
| 7213 | v8::Debug::SetDebugEventListener(NULL); |
| 7214 | } |
| 7215 | |
| 7216 | |
| 7217 | static void DebugEventBreakWithOptimizedStack(v8::DebugEvent event, |
| 7218 | v8::Handle<v8::Object> exec_state, |
| 7219 | v8::Handle<v8::Object> event_data, |
| 7220 | v8::Handle<v8::Value> data) { |
| 7221 | if (event == v8::Break) { |
| 7222 | if (!frame_function_name.IsEmpty()) { |
| 7223 | for (int i = 0; i < 2; i++) { |
| 7224 | const int argc = 2; |
| 7225 | v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(i) }; |
| 7226 | // Get the name of the function in frame i. |
| 7227 | v8::Handle<v8::Value> result = |
| 7228 | frame_function_name->Call(exec_state, argc, argv); |
| 7229 | CHECK(result->IsString()); |
| 7230 | v8::Handle<v8::String> function_name(result->ToString()); |
| 7231 | CHECK(function_name->Equals(v8::String::New("loop"))); |
| 7232 | // Get the name of the first argument in frame i. |
| 7233 | result = frame_argument_name->Call(exec_state, argc, argv); |
| 7234 | CHECK(result->IsString()); |
| 7235 | v8::Handle<v8::String> argument_name(result->ToString()); |
| 7236 | CHECK(argument_name->Equals(v8::String::New("count"))); |
| 7237 | // Get the value of the first argument in frame i. If the |
| 7238 | // funtion is optimized the value will be undefined, otherwise |
| 7239 | // the value will be '1 - i'. |
| 7240 | // |
| 7241 | // TODO(3141533): We should be able to get the real value for |
| 7242 | // optimized frames. |
| 7243 | result = frame_argument_value->Call(exec_state, argc, argv); |
| 7244 | CHECK(result->IsUndefined() || (result->Int32Value() == 1 - i)); |
| 7245 | // Get the name of the first local variable. |
| 7246 | result = frame_local_name->Call(exec_state, argc, argv); |
| 7247 | CHECK(result->IsString()); |
| 7248 | v8::Handle<v8::String> local_name(result->ToString()); |
| 7249 | CHECK(local_name->Equals(v8::String::New("local"))); |
| 7250 | // Get the value of the first local variable. If the function |
| 7251 | // is optimized the value will be undefined, otherwise it will |
| 7252 | // be 42. |
| 7253 | // |
| 7254 | // TODO(3141533): We should be able to get the real value for |
| 7255 | // optimized frames. |
| 7256 | result = frame_local_value->Call(exec_state, argc, argv); |
| 7257 | CHECK(result->IsUndefined() || (result->Int32Value() == 42)); |
| 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | } |
| 7262 | |
| 7263 | |
| 7264 | static v8::Handle<v8::Value> ScheduleBreak(const v8::Arguments& args) { |
| 7265 | v8::Debug::SetDebugEventListener(DebugEventBreakWithOptimizedStack, |
| 7266 | v8::Undefined()); |
| 7267 | v8::Debug::DebugBreak(); |
| 7268 | return v8::Undefined(); |
| 7269 | } |
| 7270 | |
| 7271 | |
| 7272 | TEST(DebugBreakStackInspection) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7273 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7274 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7275 | |
| 7276 | frame_function_name = |
| 7277 | CompileFunction(&env, frame_function_name_source, "frame_function_name"); |
| 7278 | frame_argument_name = |
| 7279 | CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); |
| 7280 | frame_argument_value = CompileFunction(&env, |
| 7281 | frame_argument_value_source, |
| 7282 | "frame_argument_value"); |
| 7283 | frame_local_name = |
| 7284 | CompileFunction(&env, frame_local_name_source, "frame_local_name"); |
| 7285 | frame_local_value = |
| 7286 | CompileFunction(&env, frame_local_value_source, "frame_local_value"); |
| 7287 | |
| 7288 | v8::Handle<v8::FunctionTemplate> schedule_break_template = |
| 7289 | v8::FunctionTemplate::New(ScheduleBreak); |
| 7290 | v8::Handle<v8::Function> schedule_break = |
| 7291 | schedule_break_template->GetFunction(); |
| 7292 | env->Global()->Set(v8_str("scheduleBreak"), schedule_break); |
| 7293 | |
| 7294 | const char* src = |
| 7295 | "function loop(count) {" |
| 7296 | " var local = 42;" |
| 7297 | " if (count < 1) { scheduleBreak(); loop(count + 1); }" |
| 7298 | "}" |
| 7299 | "loop(0);"; |
| 7300 | v8::Script::Compile(v8::String::New(src))->Run(); |
| 7301 | } |
| 7302 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7303 | |
| 7304 | // Test that setting the terminate execution flag during debug break processing. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7305 | static void TestDebugBreakInLoop(const char* loop_head, |
| 7306 | const char** loop_bodies, |
| 7307 | const char* loop_tail) { |
| 7308 | // Receive 100 breaks for each test and then terminate JavaScript execution. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7309 | static const int kBreaksPerTest = 100; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7310 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7311 | for (int i = 0; loop_bodies[i] != NULL; i++) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7312 | // Perform a lazy deoptimization after various numbers of breaks |
| 7313 | // have been hit. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7314 | for (int j = 0; j < 11; j++) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7315 | break_point_hit_count_deoptimize = j; |
| 7316 | if (j == 10) { |
| 7317 | break_point_hit_count_deoptimize = kBreaksPerTest; |
| 7318 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7319 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7320 | break_point_hit_count = 0; |
| 7321 | max_break_point_hit_count = kBreaksPerTest; |
| 7322 | terminate_after_max_break_point_hit = true; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7323 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7324 | EmbeddedVector<char, 1024> buffer; |
| 7325 | OS::SNPrintF(buffer, |
| 7326 | "function f() {%s%s%s}", |
| 7327 | loop_head, loop_bodies[i], loop_tail); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7328 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7329 | // Function with infinite loop. |
| 7330 | CompileRun(buffer.start()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7331 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7332 | // Set the debug break to enter the debugger as soon as possible. |
| 7333 | v8::Debug::DebugBreak(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7334 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7335 | // Call function with infinite loop. |
| 7336 | CompileRun("f();"); |
| 7337 | CHECK_EQ(kBreaksPerTest, break_point_hit_count); |
| 7338 | |
| 7339 | CHECK(!v8::V8::IsExecutionTerminating()); |
| 7340 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7341 | } |
| 7342 | } |
| 7343 | |
| 7344 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7345 | TEST(DebugBreakLoop) { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7346 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7347 | v8::HandleScope scope(env->GetIsolate()); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7348 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7349 | // Register a debug event listener which sets the break flag and counts. |
| 7350 | v8::Debug::SetDebugEventListener(DebugEventBreakMax); |
| 7351 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7352 | // Create a function for getting the frame count when hitting the break. |
| 7353 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 7354 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7355 | CompileRun("var a = 1;"); |
| 7356 | CompileRun("function g() { }"); |
| 7357 | CompileRun("function h() { }"); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7358 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7359 | const char* loop_bodies[] = { |
| 7360 | "", |
| 7361 | "g()", |
| 7362 | "if (a == 0) { g() }", |
| 7363 | "if (a == 1) { g() }", |
| 7364 | "if (a == 0) { g() } else { h() }", |
| 7365 | "if (a == 0) { continue }", |
| 7366 | "if (a == 1) { continue }", |
| 7367 | "switch (a) { case 1: g(); }", |
| 7368 | "switch (a) { case 1: continue; }", |
| 7369 | "switch (a) { case 1: g(); break; default: h() }", |
| 7370 | "switch (a) { case 1: continue; break; default: h() }", |
| 7371 | NULL |
| 7372 | }; |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7373 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7374 | TestDebugBreakInLoop("while (true) {", loop_bodies, "}"); |
| 7375 | TestDebugBreakInLoop("while (a == 1) {", loop_bodies, "}"); |
| 7376 | |
| 7377 | TestDebugBreakInLoop("do {", loop_bodies, "} while (true)"); |
| 7378 | TestDebugBreakInLoop("do {", loop_bodies, "} while (a == 1)"); |
| 7379 | |
| 7380 | TestDebugBreakInLoop("for (;;) {", loop_bodies, "}"); |
| 7381 | TestDebugBreakInLoop("for (;a == 1;) {", loop_bodies, "}"); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 7382 | |
| 7383 | // Get rid of the debug event listener. |
| 7384 | v8::Debug::SetDebugEventListener(NULL); |
| 7385 | CheckDebuggerUnloaded(); |
| 7386 | } |
| 7387 | |
| 7388 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 7389 | v8::Local<v8::Script> inline_script; |
| 7390 | |
| 7391 | static void DebugBreakInlineListener(v8::DebugEvent event, |
| 7392 | v8::Handle<v8::Object> exec_state, |
| 7393 | v8::Handle<v8::Object> event_data, |
| 7394 | v8::Handle<v8::Value> data) { |
| 7395 | if (event != v8::Break) return; |
| 7396 | |
| 7397 | int expected_frame_count = 4; |
| 7398 | int expected_line_number[] = {1, 4, 7, 12}; |
| 7399 | |
| 7400 | i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script); |
| 7401 | i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast( |
| 7402 | i::JSFunction::cast(*compiled_script)->shared()->script())); |
| 7403 | |
| 7404 | int break_id = v8::internal::Isolate::Current()->debug()->break_id(); |
| 7405 | char script[128]; |
| 7406 | i::Vector<char> script_vector(script, sizeof(script)); |
| 7407 | OS::SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); |
| 7408 | v8::Local<v8::Value> result = CompileRun(script); |
| 7409 | |
| 7410 | int frame_count = result->Int32Value(); |
| 7411 | CHECK_EQ(expected_frame_count, frame_count); |
| 7412 | |
| 7413 | for (int i = 0; i < frame_count; i++) { |
| 7414 | // The 5. element in the returned array of GetFrameDetails contains the |
| 7415 | // source position of that frame. |
| 7416 | OS::SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[5]", break_id, i); |
| 7417 | v8::Local<v8::Value> result = CompileRun(script); |
| 7418 | CHECK_EQ(expected_line_number[i], |
| 7419 | i::GetScriptLineNumber(source_script, result->Int32Value())); |
| 7420 | } |
| 7421 | v8::Debug::SetDebugEventListener(NULL); |
| 7422 | v8::V8::TerminateExecution(); |
| 7423 | } |
| 7424 | |
| 7425 | |
| 7426 | TEST(DebugBreakInline) { |
| 7427 | i::FLAG_allow_natives_syntax = true; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 7428 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7429 | v8::HandleScope scope(env->GetIsolate()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 7430 | const char* source = |
| 7431 | "function debug(b) { \n" |
| 7432 | " if (b) debugger; \n" |
| 7433 | "} \n" |
| 7434 | "function f(b) { \n" |
| 7435 | " debug(b) \n" |
| 7436 | "}; \n" |
| 7437 | "function g(b) { \n" |
| 7438 | " f(b); \n" |
| 7439 | "}; \n" |
| 7440 | "g(false); \n" |
| 7441 | "g(false); \n" |
| 7442 | "%OptimizeFunctionOnNextCall(g); \n" |
| 7443 | "g(true);"; |
| 7444 | v8::Debug::SetDebugEventListener(DebugBreakInlineListener); |
| 7445 | inline_script = v8::Script::Compile(v8::String::New(source)); |
| 7446 | inline_script->Run(); |
| 7447 | } |
| 7448 | |
| 7449 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7450 | static void DebugEventStepNext(v8::DebugEvent event, |
| 7451 | v8::Handle<v8::Object> exec_state, |
| 7452 | v8::Handle<v8::Object> event_data, |
| 7453 | v8::Handle<v8::Value> data) { |
| 7454 | if (event == v8::Break) { |
| 7455 | PrepareStep(StepNext); |
| 7456 | } |
| 7457 | } |
| 7458 | |
| 7459 | |
| 7460 | static void RunScriptInANewCFrame(const char* source) { |
| 7461 | v8::TryCatch try_catch; |
| 7462 | CompileRun(source); |
| 7463 | CHECK(try_catch.HasCaught()); |
| 7464 | } |
| 7465 | |
| 7466 | |
| 7467 | TEST(Regress131642) { |
| 7468 | // Bug description: |
| 7469 | // When doing StepNext through the first script, the debugger is not reset |
| 7470 | // after exiting through exception. A flawed implementation enabling the |
| 7471 | // debugger to step into Array.prototype.forEach breaks inside the callback |
| 7472 | // for forEach in the second script under the assumption that we are in a |
| 7473 | // recursive call. In an attempt to step out, we crawl the stack using the |
| 7474 | // recorded frame pointer from the first script and fail when not finding it |
| 7475 | // on the stack. |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7476 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7477 | v8::HandleScope scope(env->GetIsolate()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7478 | v8::Debug::SetDebugEventListener(DebugEventStepNext); |
| 7479 | |
| 7480 | // We step through the first script. It exits through an exception. We run |
| 7481 | // this inside a new frame to record a different FP than the second script |
| 7482 | // would expect. |
| 7483 | const char* script_1 = "debugger; throw new Error();"; |
| 7484 | RunScriptInANewCFrame(script_1); |
| 7485 | |
| 7486 | // The second script uses forEach. |
| 7487 | const char* script_2 = "[0].forEach(function() { });"; |
| 7488 | CompileRun(script_2); |
| 7489 | |
| 7490 | v8::Debug::SetDebugEventListener(NULL); |
| 7491 | } |
| 7492 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7493 | |
| 7494 | // Import from test-heap.cc |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7495 | int CountNativeContexts(); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7496 | |
| 7497 | |
| 7498 | static void NopListener(v8::DebugEvent event, |
| 7499 | v8::Handle<v8::Object> exec_state, |
| 7500 | v8::Handle<v8::Object> event_data, |
| 7501 | v8::Handle<v8::Value> data) { |
| 7502 | } |
| 7503 | |
| 7504 | |
| 7505 | TEST(DebuggerCreatesContextIffActive) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7506 | DebugLocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7507 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7508 | CHECK_EQ(1, CountNativeContexts()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7509 | |
| 7510 | v8::Debug::SetDebugEventListener(NULL); |
| 7511 | CompileRun("debugger;"); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7512 | CHECK_EQ(1, CountNativeContexts()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7513 | |
| 7514 | v8::Debug::SetDebugEventListener(NopListener); |
| 7515 | CompileRun("debugger;"); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7516 | CHECK_EQ(2, CountNativeContexts()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 7517 | |
| 7518 | v8::Debug::SetDebugEventListener(NULL); |
| 7519 | } |
| 7520 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 7521 | |
| 7522 | TEST(LiveEditEnabled) { |
| 7523 | v8::internal::FLAG_allow_natives_syntax = true; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7524 | LocalContext env; |
| 7525 | v8::HandleScope scope(env->GetIsolate()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 7526 | v8::Debug::SetLiveEditEnabled(true); |
| 7527 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7528 | } |
| 7529 | |
| 7530 | |
| 7531 | TEST(LiveEditDisabled) { |
| 7532 | v8::internal::FLAG_allow_natives_syntax = true; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7533 | LocalContext env; |
| 7534 | v8::HandleScope scope(env->GetIsolate()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 7535 | v8::Debug::SetLiveEditEnabled(false); |
| 7536 | CompileRun("%LiveEditCompareStrings('', '')"); |
| 7537 | } |
| 7538 | |
| 7539 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 7540 | #endif // ENABLE_DEBUGGER_SUPPORT |