ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2007-2008 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 | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 35 | #include "compilation-cache.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 36 | #include "debug.h" |
| 37 | #include "platform.h" |
| 38 | #include "stub-cache.h" |
| 39 | #include "cctest.h" |
| 40 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 41 | |
| 42 | using ::v8::internal::EmbeddedVector; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 43 | using ::v8::internal::Object; |
| 44 | using ::v8::internal::OS; |
| 45 | using ::v8::internal::Handle; |
| 46 | using ::v8::internal::Heap; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 47 | using ::v8::internal::JSGlobalProxy; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 48 | using ::v8::internal::Code; |
| 49 | using ::v8::internal::Debug; |
| 50 | using ::v8::internal::Debugger; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 51 | using ::v8::internal::CommandMessage; |
| 52 | using ::v8::internal::CommandMessageQueue; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 53 | using ::v8::internal::StepAction; |
| 54 | using ::v8::internal::StepIn; // From StepAction enum |
| 55 | using ::v8::internal::StepNext; // From StepAction enum |
| 56 | using ::v8::internal::StepOut; // From StepAction enum |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 57 | using ::v8::internal::Vector; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 58 | using ::v8::internal::StrLength; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 59 | |
| 60 | // Size of temp buffer for formatting small strings. |
| 61 | #define SMALL_STRING_BUFFER_SIZE 80 |
| 62 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 63 | // --- A d d i t i o n a l C h e c k H e l p e r s |
| 64 | |
| 65 | |
| 66 | // Helper function used by the CHECK_EQ function when given Address |
| 67 | // arguments. Should not be called directly. |
| 68 | static inline void CheckEqualsHelper(const char* file, int line, |
| 69 | const char* expected_source, |
| 70 | ::v8::internal::Address expected, |
| 71 | const char* value_source, |
| 72 | ::v8::internal::Address value) { |
| 73 | if (expected != value) { |
| 74 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " |
| 75 | "Expected: %i\n# Found: %i", |
| 76 | expected_source, value_source, expected, value); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | // Helper function used by the CHECK_NE function when given Address |
| 82 | // arguments. Should not be called directly. |
| 83 | static inline void CheckNonEqualsHelper(const char* file, int line, |
| 84 | const char* unexpected_source, |
| 85 | ::v8::internal::Address unexpected, |
| 86 | const char* value_source, |
| 87 | ::v8::internal::Address value) { |
| 88 | if (unexpected == value) { |
| 89 | V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i", |
| 90 | unexpected_source, value_source, value); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | // Helper function used by the CHECK function when given code |
| 96 | // arguments. Should not be called directly. |
| 97 | static inline void CheckEqualsHelper(const char* file, int line, |
| 98 | const char* expected_source, |
| 99 | const Code* expected, |
| 100 | const char* value_source, |
| 101 | const Code* value) { |
| 102 | if (expected != value) { |
| 103 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " |
| 104 | "Expected: %p\n# Found: %p", |
| 105 | expected_source, value_source, expected, value); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static inline void CheckNonEqualsHelper(const char* file, int line, |
| 111 | const char* expected_source, |
| 112 | const Code* expected, |
| 113 | const char* value_source, |
| 114 | const Code* value) { |
| 115 | if (expected == value) { |
| 116 | V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", |
| 117 | expected_source, value_source, value); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // --- H e l p e r C l a s s e s |
| 123 | |
| 124 | |
| 125 | // Helper class for creating a V8 enviromnent for running tests |
| 126 | class DebugLocalContext { |
| 127 | public: |
| 128 | inline DebugLocalContext( |
| 129 | v8::ExtensionConfiguration* extensions = 0, |
| 130 | v8::Handle<v8::ObjectTemplate> global_template = |
| 131 | v8::Handle<v8::ObjectTemplate>(), |
| 132 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) |
| 133 | : context_(v8::Context::New(extensions, global_template, global_object)) { |
| 134 | context_->Enter(); |
| 135 | } |
| 136 | inline ~DebugLocalContext() { |
| 137 | context_->Exit(); |
| 138 | context_.Dispose(); |
| 139 | } |
| 140 | inline v8::Context* operator->() { return *context_; } |
| 141 | inline v8::Context* operator*() { return *context_; } |
| 142 | inline bool IsReady() { return !context_.IsEmpty(); } |
| 143 | void ExposeDebug() { |
| 144 | // Expose the debug context global object in the global object for testing. |
| 145 | Debug::Load(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 146 | Debug::debug_context()->set_security_token( |
| 147 | v8::Utils::OpenHandle(*context_)->security_token()); |
| 148 | |
| 149 | Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 150 | v8::Utils::OpenHandle(*context_->Global()))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 151 | Handle<v8::internal::String> debug_string = |
| 152 | v8::internal::Factory::LookupAsciiSymbol("debug"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 153 | SetProperty(global, debug_string, |
| 154 | Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 155 | } |
| 156 | private: |
| 157 | v8::Persistent<v8::Context> context_; |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | // --- H e l p e r F u n c t i o n s |
| 162 | |
| 163 | |
| 164 | // Compile and run the supplied source and return the fequested function. |
| 165 | static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, |
| 166 | const char* source, |
| 167 | const char* function_name) { |
| 168 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 169 | return v8::Local<v8::Function>::Cast( |
| 170 | (*env)->Global()->Get(v8::String::New(function_name))); |
| 171 | } |
| 172 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 173 | |
| 174 | // Compile and run the supplied source and return the requested function. |
| 175 | static v8::Local<v8::Function> CompileFunction(const char* source, |
| 176 | const char* function_name) { |
| 177 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 178 | return v8::Local<v8::Function>::Cast( |
| 179 | v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name))); |
| 180 | } |
| 181 | |
| 182 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 183 | // Is there any debug info for the function? |
| 184 | static bool HasDebugInfo(v8::Handle<v8::Function> fun) { |
| 185 | Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun); |
| 186 | Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); |
| 187 | return Debug::HasDebugInfo(shared); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // Set a break point in a function and return the associated break point |
| 192 | // number. |
| 193 | static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) { |
| 194 | static int break_point = 0; |
| 195 | Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); |
| 196 | Debug::SetBreakPoint( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 197 | shared, |
| 198 | Handle<Object>(v8::internal::Smi::FromInt(++break_point)), |
| 199 | &position); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 200 | return break_point; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | // Set a break point in a function and return the associated break point |
| 205 | // number. |
| 206 | static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) { |
| 207 | return SetBreakPoint(v8::Utils::OpenHandle(*fun), position); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | // Set a break point in a function using the Debug object and return the |
| 212 | // associated break point number. |
| 213 | static int SetBreakPointFromJS(const char* function_name, |
| 214 | int line, int position) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 215 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 216 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 217 | "debug.Debug.setBreakPoint(%s,%d,%d)", |
| 218 | function_name, line, position); |
| 219 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 220 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 221 | return v8::Script::Compile(str)->Run()->Int32Value(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 225 | // Set a break point in a script identified by id using the global Debug object. |
| 226 | static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 227 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 228 | if (column >= 0) { |
| 229 | // Column specified set script break point on precise location. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 230 | OS::SNPrintF(buffer, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 231 | "debug.Debug.setScriptBreakPointById(%d,%d,%d)", |
| 232 | script_id, line, column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 233 | } else { |
| 234 | // Column not specified set script break point on line. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 235 | OS::SNPrintF(buffer, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 236 | "debug.Debug.setScriptBreakPointById(%d,%d)", |
| 237 | script_id, line); |
| 238 | } |
| 239 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
| 240 | { |
| 241 | v8::TryCatch try_catch; |
| 242 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 243 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 244 | CHECK(!try_catch.HasCaught()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 245 | return value->Int32Value(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | // Set a break point in a script identified by name using the global Debug |
| 251 | // object. |
| 252 | static int SetScriptBreakPointByNameFromJS(const char* script_name, |
| 253 | int line, int column) { |
| 254 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 255 | if (column >= 0) { |
| 256 | // Column specified set script break point on precise location. |
| 257 | OS::SNPrintF(buffer, |
| 258 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", |
| 259 | script_name, line, column); |
| 260 | } else { |
| 261 | // Column not specified set script break point on line. |
| 262 | OS::SNPrintF(buffer, |
| 263 | "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", |
| 264 | script_name, line); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 265 | } |
| 266 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 267 | { |
| 268 | v8::TryCatch try_catch; |
| 269 | v8::Handle<v8::String> str = v8::String::New(buffer.start()); |
| 270 | v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 271 | CHECK(!try_catch.HasCaught()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 272 | return value->Int32Value(); |
| 273 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
| 277 | // Clear a break point. |
| 278 | static void ClearBreakPoint(int break_point) { |
| 279 | Debug::ClearBreakPoint( |
| 280 | Handle<Object>(v8::internal::Smi::FromInt(break_point))); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // Clear a break point using the global Debug object. |
| 285 | static void ClearBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 286 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 287 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 288 | "debug.Debug.clearBreakPoint(%d)", |
| 289 | break_point_number); |
| 290 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 291 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
| 295 | static void EnableScriptBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 296 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 297 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 298 | "debug.Debug.enableScriptBreakPoint(%d)", |
| 299 | break_point_number); |
| 300 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 301 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | static void DisableScriptBreakPointFromJS(int break_point_number) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 306 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 307 | OS::SNPrintF(buffer, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 308 | "debug.Debug.disableScriptBreakPoint(%d)", |
| 309 | break_point_number); |
| 310 | buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 311 | v8::Script::Compile(v8::String::New(buffer.start()))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | static void ChangeScriptBreakPointConditionFromJS(int break_point_number, |
| 316 | const char* condition) { |
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.changeScriptBreakPointCondition(%d, \"%s\")", |
| 320 | break_point_number, condition); |
| 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 ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number, |
| 327 | int ignoreCount) { |
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.changeScriptBreakPointIgnoreCount(%d, %d)", |
| 331 | break_point_number, ignoreCount); |
| 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 | // Change break on exception. |
| 338 | static void ChangeBreakOnException(bool caught, bool uncaught) { |
| 339 | Debug::ChangeBreakOnException(v8::internal::BreakException, caught); |
| 340 | Debug::ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | // Change break on exception using the global Debug object. |
| 345 | static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) { |
| 346 | if (caught) { |
| 347 | v8::Script::Compile( |
| 348 | v8::String::New("debug.Debug.setBreakOnException()"))->Run(); |
| 349 | } else { |
| 350 | v8::Script::Compile( |
| 351 | v8::String::New("debug.Debug.clearBreakOnException()"))->Run(); |
| 352 | } |
| 353 | if (uncaught) { |
| 354 | v8::Script::Compile( |
| 355 | v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run(); |
| 356 | } else { |
| 357 | v8::Script::Compile( |
| 358 | v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run(); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | |
| 363 | // Prepare to step to next break location. |
| 364 | static void PrepareStep(StepAction step_action) { |
| 365 | Debug::PrepareStep(step_action, 1); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | // This function is in namespace v8::internal to be friend with class |
| 370 | // v8::internal::Debug. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 371 | namespace v8 { |
| 372 | namespace internal { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 373 | |
| 374 | // Collect the currently debugged functions. |
| 375 | Handle<FixedArray> GetDebuggedFunctions() { |
| 376 | v8::internal::DebugInfoListNode* node = Debug::debug_info_list_; |
| 377 | |
| 378 | // Find the number of debugged functions. |
| 379 | int count = 0; |
| 380 | while (node) { |
| 381 | count++; |
| 382 | node = node->next(); |
| 383 | } |
| 384 | |
| 385 | // Allocate array for the debugged functions |
| 386 | Handle<FixedArray> debugged_functions = |
| 387 | v8::internal::Factory::NewFixedArray(count); |
| 388 | |
| 389 | // Run through the debug info objects and collect all functions. |
| 390 | count = 0; |
| 391 | while (node) { |
| 392 | debugged_functions->set(count++, *node->debug_info()); |
| 393 | node = node->next(); |
| 394 | } |
| 395 | |
| 396 | return debugged_functions; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static Handle<Code> ComputeCallDebugBreak(int argc) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 401 | CALL_HEAP_FUNCTION( |
| 402 | v8::internal::StubCache::ComputeCallDebugBreak(argc, Code::CALL_IC), |
| 403 | Code); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 404 | } |
| 405 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 406 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 407 | // Check that the debugger has been fully unloaded. |
| 408 | void CheckDebuggerUnloaded(bool check_functions) { |
| 409 | // Check that the debugger context is cleared and that there is no debug |
| 410 | // information stored for the debugger. |
| 411 | CHECK(Debug::debug_context().is_null()); |
| 412 | CHECK_EQ(NULL, Debug::debug_info_list_); |
| 413 | |
| 414 | // Collect garbage to ensure weak handles are cleared. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 415 | Heap::CollectAllGarbage(false); |
| 416 | Heap::CollectAllGarbage(false); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 417 | |
| 418 | // Iterate the head and check that there are no debugger related objects left. |
| 419 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 420 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 421 | CHECK(!obj->IsDebugInfo()); |
| 422 | CHECK(!obj->IsBreakPointInfo()); |
| 423 | |
| 424 | // If deep check of functions is requested check that no debug break code |
| 425 | // is left in all functions. |
| 426 | if (check_functions) { |
| 427 | if (obj->IsJSFunction()) { |
| 428 | JSFunction* fun = JSFunction::cast(obj); |
| 429 | for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) { |
| 430 | RelocInfo::Mode rmode = it.rinfo()->rmode(); |
| 431 | if (RelocInfo::IsCodeTarget(rmode)) { |
| 432 | CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address())); |
| 433 | } else if (RelocInfo::IsJSReturn(rmode)) { |
| 434 | CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo())); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 443 | void ForceUnloadDebugger() { |
| 444 | Debugger::never_unload_debugger_ = false; |
| 445 | Debugger::UnloadDebugger(); |
| 446 | } |
| 447 | |
| 448 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 449 | } } // namespace v8::internal |
| 450 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 451 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 452 | // Check that the debugger has been fully unloaded. |
| 453 | static void CheckDebuggerUnloaded(bool check_functions = false) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 454 | // Let debugger to unload itself synchronously |
| 455 | v8::Debug::ProcessDebugMessages(); |
| 456 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 457 | v8::internal::CheckDebuggerUnloaded(check_functions); |
| 458 | } |
| 459 | |
| 460 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 461 | // Inherit from BreakLocationIterator to get access to protected parts for |
| 462 | // testing. |
| 463 | class TestBreakLocationIterator: public v8::internal::BreakLocationIterator { |
| 464 | public: |
| 465 | explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info) |
| 466 | : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {} |
| 467 | v8::internal::RelocIterator* it() { return reloc_iterator_; } |
| 468 | v8::internal::RelocIterator* it_original() { |
| 469 | return reloc_iterator_original_; |
| 470 | } |
| 471 | }; |
| 472 | |
| 473 | |
| 474 | // Compile a function, set a break point and check that the call at the break |
| 475 | // location in the code is the expected debug_break function. |
| 476 | void CheckDebugBreakFunction(DebugLocalContext* env, |
| 477 | const char* source, const char* name, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 478 | int position, v8::internal::RelocInfo::Mode mode, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 479 | Code* debug_break) { |
| 480 | // Create function and set the break point. |
| 481 | Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle( |
| 482 | *CompileFunction(env, source, name)); |
| 483 | int bp = SetBreakPoint(fun, position); |
| 484 | |
| 485 | // Check that the debug break function is as expected. |
| 486 | Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); |
| 487 | CHECK(Debug::HasDebugInfo(shared)); |
| 488 | TestBreakLocationIterator it1(Debug::GetDebugInfo(shared)); |
| 489 | it1.FindBreakLocationFromPosition(position); |
| 490 | CHECK_EQ(mode, it1.it()->rinfo()->rmode()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 491 | if (mode != v8::internal::RelocInfo::JS_RETURN) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 492 | CHECK_EQ(debug_break, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 493 | Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 494 | } else { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 495 | CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Clear the break point and check that the debug break function is no longer |
| 499 | // there |
| 500 | ClearBreakPoint(bp); |
| 501 | CHECK(!Debug::HasDebugInfo(shared)); |
| 502 | CHECK(Debug::EnsureDebugInfo(shared)); |
| 503 | TestBreakLocationIterator it2(Debug::GetDebugInfo(shared)); |
| 504 | it2.FindBreakLocationFromPosition(position); |
| 505 | CHECK_EQ(mode, it2.it()->rinfo()->rmode()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 506 | if (mode == v8::internal::RelocInfo::JS_RETURN) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 507 | CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | |
| 512 | // --- D e b u g E v e n t H a n d l e r s |
| 513 | // --- |
| 514 | // --- The different tests uses a number of debug event handlers. |
| 515 | // --- |
| 516 | |
| 517 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 518 | // Source for The JavaScript function which picks out the function name of the |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 519 | // top frame. |
| 520 | const char* frame_function_name_source = |
| 521 | "function frame_function_name(exec_state) {" |
| 522 | " return exec_state.frame(0).func().name();" |
| 523 | "}"; |
| 524 | v8::Local<v8::Function> frame_function_name; |
| 525 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 526 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 527 | // Source for The JavaScript function which picks out the source line for the |
| 528 | // top frame. |
| 529 | const char* frame_source_line_source = |
| 530 | "function frame_source_line(exec_state) {" |
| 531 | " return exec_state.frame(0).sourceLine();" |
| 532 | "}"; |
| 533 | v8::Local<v8::Function> frame_source_line; |
| 534 | |
| 535 | |
| 536 | // Source for The JavaScript function which picks out the source column for the |
| 537 | // top frame. |
| 538 | const char* frame_source_column_source = |
| 539 | "function frame_source_column(exec_state) {" |
| 540 | " return exec_state.frame(0).sourceColumn();" |
| 541 | "}"; |
| 542 | v8::Local<v8::Function> frame_source_column; |
| 543 | |
| 544 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 545 | // Source for The JavaScript function which picks out the script name for the |
| 546 | // top frame. |
| 547 | const char* frame_script_name_source = |
| 548 | "function frame_script_name(exec_state) {" |
| 549 | " return exec_state.frame(0).func().script().name();" |
| 550 | "}"; |
| 551 | v8::Local<v8::Function> frame_script_name; |
| 552 | |
| 553 | |
| 554 | // Source for The JavaScript function which picks out the script data for the |
| 555 | // top frame. |
| 556 | const char* frame_script_data_source = |
| 557 | "function frame_script_data(exec_state) {" |
| 558 | " return exec_state.frame(0).func().script().data();" |
| 559 | "}"; |
| 560 | v8::Local<v8::Function> frame_script_data; |
| 561 | |
| 562 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 563 | // Source for The JavaScript function which picks out the script data from |
| 564 | // AfterCompile event |
| 565 | const char* compiled_script_data_source = |
| 566 | "function compiled_script_data(event_data) {" |
| 567 | " return event_data.script().data();" |
| 568 | "}"; |
| 569 | v8::Local<v8::Function> compiled_script_data; |
| 570 | |
| 571 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 572 | // Source for The JavaScript function which returns the number of frames. |
| 573 | static const char* frame_count_source = |
| 574 | "function frame_count(exec_state) {" |
| 575 | " return exec_state.frameCount();" |
| 576 | "}"; |
| 577 | v8::Handle<v8::Function> frame_count; |
| 578 | |
| 579 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 580 | // Global variable to store the last function hit - used by some tests. |
| 581 | char last_function_hit[80]; |
| 582 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 583 | // Global variable to store the name and data for last script hit - used by some |
| 584 | // tests. |
| 585 | char last_script_name_hit[80]; |
| 586 | char last_script_data_hit[80]; |
| 587 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 588 | // Global variables to store the last source position - used by some tests. |
| 589 | int last_source_line = -1; |
| 590 | int last_source_column = -1; |
| 591 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 592 | // Debug event handler which counts the break points which have been hit. |
| 593 | int break_point_hit_count = 0; |
| 594 | static void DebugEventBreakPointHitCount(v8::DebugEvent event, |
| 595 | v8::Handle<v8::Object> exec_state, |
| 596 | v8::Handle<v8::Object> event_data, |
| 597 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 598 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 599 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 600 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 601 | // Count the number of breaks. |
| 602 | if (event == v8::Break) { |
| 603 | break_point_hit_count++; |
| 604 | if (!frame_function_name.IsEmpty()) { |
| 605 | // Get the name of the function. |
| 606 | const int argc = 1; |
| 607 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 608 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 609 | argc, argv); |
| 610 | if (result->IsUndefined()) { |
| 611 | last_function_hit[0] = '\0'; |
| 612 | } else { |
| 613 | CHECK(result->IsString()); |
| 614 | v8::Handle<v8::String> function_name(result->ToString()); |
| 615 | function_name->WriteAscii(last_function_hit); |
| 616 | } |
| 617 | } |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 618 | |
| 619 | if (!frame_source_line.IsEmpty()) { |
| 620 | // Get the source line. |
| 621 | const int argc = 1; |
| 622 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 623 | v8::Handle<v8::Value> result = frame_source_line->Call(exec_state, |
| 624 | argc, argv); |
| 625 | CHECK(result->IsNumber()); |
| 626 | last_source_line = result->Int32Value(); |
| 627 | } |
| 628 | |
| 629 | if (!frame_source_column.IsEmpty()) { |
| 630 | // Get the source column. |
| 631 | const int argc = 1; |
| 632 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 633 | v8::Handle<v8::Value> result = frame_source_column->Call(exec_state, |
| 634 | argc, argv); |
| 635 | CHECK(result->IsNumber()); |
| 636 | last_source_column = result->Int32Value(); |
| 637 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 638 | |
| 639 | if (!frame_script_name.IsEmpty()) { |
| 640 | // Get the script name of the function script. |
| 641 | const int argc = 1; |
| 642 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 643 | v8::Handle<v8::Value> result = frame_script_name->Call(exec_state, |
| 644 | argc, argv); |
| 645 | if (result->IsUndefined()) { |
| 646 | last_script_name_hit[0] = '\0'; |
| 647 | } else { |
| 648 | CHECK(result->IsString()); |
| 649 | v8::Handle<v8::String> script_name(result->ToString()); |
| 650 | script_name->WriteAscii(last_script_name_hit); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (!frame_script_data.IsEmpty()) { |
| 655 | // Get the script data of the function script. |
| 656 | const int argc = 1; |
| 657 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 658 | v8::Handle<v8::Value> result = frame_script_data->Call(exec_state, |
| 659 | argc, argv); |
| 660 | if (result->IsUndefined()) { |
| 661 | last_script_data_hit[0] = '\0'; |
| 662 | } else { |
| 663 | result = result->ToString(); |
| 664 | CHECK(result->IsString()); |
| 665 | v8::Handle<v8::String> script_data(result->ToString()); |
| 666 | script_data->WriteAscii(last_script_data_hit); |
| 667 | } |
| 668 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 669 | } else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) { |
| 670 | const int argc = 1; |
| 671 | v8::Handle<v8::Value> argv[argc] = { event_data }; |
| 672 | v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state, |
| 673 | argc, argv); |
| 674 | if (result->IsUndefined()) { |
| 675 | last_script_data_hit[0] = '\0'; |
| 676 | } else { |
| 677 | result = result->ToString(); |
| 678 | CHECK(result->IsString()); |
| 679 | v8::Handle<v8::String> script_data(result->ToString()); |
| 680 | script_data->WriteAscii(last_script_data_hit); |
| 681 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 686 | // Debug event handler which counts a number of events and collects the stack |
| 687 | // height if there is a function compiled for that. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 688 | int exception_hit_count = 0; |
| 689 | int uncaught_exception_hit_count = 0; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 690 | int last_js_stack_height = -1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 691 | |
| 692 | static void DebugEventCounterClear() { |
| 693 | break_point_hit_count = 0; |
| 694 | exception_hit_count = 0; |
| 695 | uncaught_exception_hit_count = 0; |
| 696 | } |
| 697 | |
| 698 | static void DebugEventCounter(v8::DebugEvent event, |
| 699 | v8::Handle<v8::Object> exec_state, |
| 700 | v8::Handle<v8::Object> event_data, |
| 701 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 702 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 703 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 704 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 705 | // Count the number of breaks. |
| 706 | if (event == v8::Break) { |
| 707 | break_point_hit_count++; |
| 708 | } else if (event == v8::Exception) { |
| 709 | exception_hit_count++; |
| 710 | |
| 711 | // Check whether the exception was uncaught. |
| 712 | v8::Local<v8::String> fun_name = v8::String::New("uncaught"); |
| 713 | v8::Local<v8::Function> fun = |
| 714 | v8::Function::Cast(*event_data->Get(fun_name)); |
| 715 | v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL); |
| 716 | if (result->IsTrue()) { |
| 717 | uncaught_exception_hit_count++; |
| 718 | } |
| 719 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 720 | |
| 721 | // Collect the JavsScript stack height if the function frame_count is |
| 722 | // compiled. |
| 723 | if (!frame_count.IsEmpty()) { |
| 724 | static const int kArgc = 1; |
| 725 | v8::Handle<v8::Value> argv[kArgc] = { exec_state }; |
| 726 | // Using exec_state as receiver is just to have a receiver. |
| 727 | v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv); |
| 728 | last_js_stack_height = result->Int32Value(); |
| 729 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | |
| 733 | // Debug event handler which evaluates a number of expressions when a break |
| 734 | // point is hit. Each evaluated expression is compared with an expected value. |
| 735 | // For this debug event handler to work the following two global varaibles |
| 736 | // must be initialized. |
| 737 | // checks: An array of expressions and expected results |
| 738 | // evaluate_check_function: A JavaScript function (see below) |
| 739 | |
| 740 | // Structure for holding checks to do. |
| 741 | struct EvaluateCheck { |
| 742 | const char* expr; // An expression to evaluate when a break point is hit. |
| 743 | v8::Handle<v8::Value> expected; // The expected result. |
| 744 | }; |
| 745 | // Array of checks to do. |
| 746 | struct EvaluateCheck* checks = NULL; |
| 747 | // Source for The JavaScript function which can do the evaluation when a break |
| 748 | // point is hit. |
| 749 | const char* evaluate_check_source = |
| 750 | "function evaluate_check(exec_state, expr, expected) {" |
| 751 | " return exec_state.frame(0).evaluate(expr).value() === expected;" |
| 752 | "}"; |
| 753 | v8::Local<v8::Function> evaluate_check_function; |
| 754 | |
| 755 | // The actual debug event described by the longer comment above. |
| 756 | static void DebugEventEvaluate(v8::DebugEvent event, |
| 757 | v8::Handle<v8::Object> exec_state, |
| 758 | v8::Handle<v8::Object> event_data, |
| 759 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 760 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 761 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 762 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 763 | if (event == v8::Break) { |
| 764 | for (int i = 0; checks[i].expr != NULL; i++) { |
| 765 | const int argc = 3; |
| 766 | v8::Handle<v8::Value> argv[argc] = { exec_state, |
| 767 | v8::String::New(checks[i].expr), |
| 768 | checks[i].expected }; |
| 769 | v8::Handle<v8::Value> result = |
| 770 | evaluate_check_function->Call(exec_state, argc, argv); |
| 771 | if (!result->IsTrue()) { |
| 772 | v8::String::AsciiValue ascii(checks[i].expected->ToString()); |
| 773 | V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | |
| 780 | // This debug event listener removes a breakpoint in a function |
| 781 | int debug_event_remove_break_point = 0; |
| 782 | static void DebugEventRemoveBreakPoint(v8::DebugEvent event, |
| 783 | v8::Handle<v8::Object> exec_state, |
| 784 | v8::Handle<v8::Object> event_data, |
| 785 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 786 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 787 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 788 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 789 | if (event == v8::Break) { |
| 790 | break_point_hit_count++; |
| 791 | v8::Handle<v8::Function> fun = v8::Handle<v8::Function>::Cast(data); |
| 792 | ClearBreakPoint(debug_event_remove_break_point); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | |
| 797 | // Debug event handler which counts break points hit and performs a step |
| 798 | // afterwards. |
| 799 | StepAction step_action = StepIn; // Step action to perform when stepping. |
| 800 | static void DebugEventStep(v8::DebugEvent event, |
| 801 | v8::Handle<v8::Object> exec_state, |
| 802 | v8::Handle<v8::Object> event_data, |
| 803 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 804 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 805 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 806 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 807 | if (event == v8::Break) { |
| 808 | break_point_hit_count++; |
| 809 | PrepareStep(step_action); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | |
| 814 | // Debug event handler which counts break points hit and performs a step |
| 815 | // afterwards. For each call the expected function is checked. |
| 816 | // For this debug event handler to work the following two global varaibles |
| 817 | // must be initialized. |
| 818 | // expected_step_sequence: An array of the expected function call sequence. |
| 819 | // frame_function_name: A JavaScript function (see below). |
| 820 | |
| 821 | // String containing the expected function call sequence. Note: this only works |
| 822 | // if functions have name length of one. |
| 823 | const char* expected_step_sequence = NULL; |
| 824 | |
| 825 | // The actual debug event described by the longer comment above. |
| 826 | static void DebugEventStepSequence(v8::DebugEvent event, |
| 827 | v8::Handle<v8::Object> exec_state, |
| 828 | v8::Handle<v8::Object> event_data, |
| 829 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 830 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 831 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 832 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 833 | if (event == v8::Break || event == v8::Exception) { |
| 834 | // Check that the current function is the expected. |
| 835 | CHECK(break_point_hit_count < |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 836 | StrLength(expected_step_sequence)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 837 | const int argc = 1; |
| 838 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 839 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 840 | argc, argv); |
| 841 | CHECK(result->IsString()); |
| 842 | v8::String::AsciiValue function_name(result->ToString()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 843 | CHECK_EQ(1, StrLength(*function_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 844 | CHECK_EQ((*function_name)[0], |
| 845 | expected_step_sequence[break_point_hit_count]); |
| 846 | |
| 847 | // Perform step. |
| 848 | break_point_hit_count++; |
| 849 | PrepareStep(step_action); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | |
| 854 | // Debug event handler which performs a garbage collection. |
| 855 | static void DebugEventBreakPointCollectGarbage( |
| 856 | v8::DebugEvent event, |
| 857 | v8::Handle<v8::Object> exec_state, |
| 858 | v8::Handle<v8::Object> event_data, |
| 859 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 860 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 861 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 862 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 863 | // Perform a garbage collection when break point is hit and continue. Based |
| 864 | // on the number of break points hit either scavenge or mark compact |
| 865 | // collector is used. |
| 866 | if (event == v8::Break) { |
| 867 | break_point_hit_count++; |
| 868 | if (break_point_hit_count % 2 == 0) { |
| 869 | // Scavenge. |
| 870 | Heap::CollectGarbage(0, v8::internal::NEW_SPACE); |
| 871 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 872 | // Mark sweep compact. |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 873 | Heap::CollectAllGarbage(true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | |
| 879 | // Debug event handler which re-issues a debug break and calls the garbage |
| 880 | // collector to have the heap verified. |
| 881 | static void DebugEventBreak(v8::DebugEvent event, |
| 882 | v8::Handle<v8::Object> exec_state, |
| 883 | v8::Handle<v8::Object> event_data, |
| 884 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 885 | // When hitting a debug event listener there must be a break set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 886 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 887 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 888 | if (event == v8::Break) { |
| 889 | // Count the number of breaks. |
| 890 | break_point_hit_count++; |
| 891 | |
| 892 | // Run the garbage collector to enforce heap verification if option |
| 893 | // --verify-heap is set. |
| 894 | Heap::CollectGarbage(0, v8::internal::NEW_SPACE); |
| 895 | |
| 896 | // Set the break flag again to come back here as soon as possible. |
| 897 | v8::Debug::DebugBreak(); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 902 | // Debug event handler which re-issues a debug break until a limit has been |
| 903 | // reached. |
| 904 | int max_break_point_hit_count = 0; |
| 905 | static void DebugEventBreakMax(v8::DebugEvent event, |
| 906 | v8::Handle<v8::Object> exec_state, |
| 907 | v8::Handle<v8::Object> event_data, |
| 908 | v8::Handle<v8::Value> data) { |
| 909 | // When hitting a debug event listener there must be a break set. |
| 910 | CHECK_NE(v8::internal::Debug::break_id(), 0); |
| 911 | |
| 912 | if (event == v8::Break && break_point_hit_count < max_break_point_hit_count) { |
| 913 | // Count the number of breaks. |
| 914 | break_point_hit_count++; |
| 915 | |
| 916 | // Set the break flag again to come back here as soon as possible. |
| 917 | v8::Debug::DebugBreak(); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 922 | // --- M e s s a g e C a l l b a c k |
| 923 | |
| 924 | |
| 925 | // Message callback which counts the number of messages. |
| 926 | int message_callback_count = 0; |
| 927 | |
| 928 | static void MessageCallbackCountClear() { |
| 929 | message_callback_count = 0; |
| 930 | } |
| 931 | |
| 932 | static void MessageCallbackCount(v8::Handle<v8::Message> message, |
| 933 | v8::Handle<v8::Value> data) { |
| 934 | message_callback_count++; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | // --- T h e A c t u a l T e s t s |
| 939 | |
| 940 | |
| 941 | // Test that the debug break function is the expected one for different kinds |
| 942 | // of break locations. |
| 943 | TEST(DebugStub) { |
| 944 | using ::v8::internal::Builtins; |
| 945 | v8::HandleScope scope; |
| 946 | DebugLocalContext env; |
| 947 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 948 | CheckDebugBreakFunction(&env, |
| 949 | "function f1(){}", "f1", |
| 950 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 951 | v8::internal::RelocInfo::JS_RETURN, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 952 | NULL); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 953 | CheckDebugBreakFunction(&env, |
| 954 | "function f2(){x=1;}", "f2", |
| 955 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 956 | v8::internal::RelocInfo::CODE_TARGET, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 957 | Builtins::builtin(Builtins::StoreIC_DebugBreak)); |
| 958 | CheckDebugBreakFunction(&env, |
| 959 | "function f3(){var a=x;}", "f3", |
| 960 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 961 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 962 | Builtins::builtin(Builtins::LoadIC_DebugBreak)); |
| 963 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 964 | // TODO(1240753): Make the test architecture independent or split |
| 965 | // parts of the debugger into architecture dependent files. This |
| 966 | // 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] | 967 | // Currently on ICs for keyed store/load on ARM. |
| 968 | #if !defined (__arm__) && !defined(__thumb__) |
| 969 | CheckDebugBreakFunction( |
| 970 | &env, |
| 971 | "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}", |
| 972 | "f4", |
| 973 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 974 | v8::internal::RelocInfo::CODE_TARGET, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 975 | Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak)); |
| 976 | CheckDebugBreakFunction( |
| 977 | &env, |
| 978 | "function f5(){var index='propertyName'; var a={}; return a[index];}", |
| 979 | "f5", |
| 980 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 981 | v8::internal::RelocInfo::CODE_TARGET, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 982 | Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak)); |
| 983 | #endif |
| 984 | |
| 985 | // Check the debug break code stubs for call ICs with different number of |
| 986 | // parameters. |
| 987 | Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0); |
| 988 | Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1); |
| 989 | Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4); |
| 990 | |
| 991 | CheckDebugBreakFunction(&env, |
| 992 | "function f4_0(){x();}", "f4_0", |
| 993 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 994 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 995 | *debug_break_0); |
| 996 | |
| 997 | CheckDebugBreakFunction(&env, |
| 998 | "function f4_1(){x(1);}", "f4_1", |
| 999 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1000 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1001 | *debug_break_1); |
| 1002 | |
| 1003 | CheckDebugBreakFunction(&env, |
| 1004 | "function f4_4(){x(1,2,3,4);}", "f4_4", |
| 1005 | 0, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1006 | v8::internal::RelocInfo::CODE_TARGET_CONTEXT, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1007 | *debug_break_4); |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | // Test that the debug info in the VM is in sync with the functions being |
| 1012 | // debugged. |
| 1013 | TEST(DebugInfo) { |
| 1014 | v8::HandleScope scope; |
| 1015 | DebugLocalContext env; |
| 1016 | // Create a couple of functions for the test. |
| 1017 | v8::Local<v8::Function> foo = |
| 1018 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1019 | v8::Local<v8::Function> bar = |
| 1020 | CompileFunction(&env, "function bar(){}", "bar"); |
| 1021 | // Initially no functions are debugged. |
| 1022 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1023 | CHECK(!HasDebugInfo(foo)); |
| 1024 | CHECK(!HasDebugInfo(bar)); |
| 1025 | // One function (foo) is debugged. |
| 1026 | int bp1 = SetBreakPoint(foo, 0); |
| 1027 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1028 | CHECK(HasDebugInfo(foo)); |
| 1029 | CHECK(!HasDebugInfo(bar)); |
| 1030 | // Two functions are debugged. |
| 1031 | int bp2 = SetBreakPoint(bar, 0); |
| 1032 | CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); |
| 1033 | CHECK(HasDebugInfo(foo)); |
| 1034 | CHECK(HasDebugInfo(bar)); |
| 1035 | // One function (bar) is debugged. |
| 1036 | ClearBreakPoint(bp1); |
| 1037 | CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); |
| 1038 | CHECK(!HasDebugInfo(foo)); |
| 1039 | CHECK(HasDebugInfo(bar)); |
| 1040 | // No functions are debugged. |
| 1041 | ClearBreakPoint(bp2); |
| 1042 | CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); |
| 1043 | CHECK(!HasDebugInfo(foo)); |
| 1044 | CHECK(!HasDebugInfo(bar)); |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | // Test that a break point can be set at an IC store location. |
| 1049 | TEST(BreakPointICStore) { |
| 1050 | break_point_hit_count = 0; |
| 1051 | v8::HandleScope scope; |
| 1052 | DebugLocalContext env; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1053 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1054 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1055 | v8::Undefined()); |
| 1056 | v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run(); |
| 1057 | v8::Local<v8::Function> foo = |
| 1058 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1059 | |
| 1060 | // Run without breakpoints. |
| 1061 | foo->Call(env->Global(), 0, NULL); |
| 1062 | CHECK_EQ(0, break_point_hit_count); |
| 1063 | |
| 1064 | // Run with breakpoint |
| 1065 | int bp = SetBreakPoint(foo, 0); |
| 1066 | foo->Call(env->Global(), 0, NULL); |
| 1067 | CHECK_EQ(1, break_point_hit_count); |
| 1068 | foo->Call(env->Global(), 0, NULL); |
| 1069 | CHECK_EQ(2, break_point_hit_count); |
| 1070 | |
| 1071 | // Run without breakpoints. |
| 1072 | ClearBreakPoint(bp); |
| 1073 | foo->Call(env->Global(), 0, NULL); |
| 1074 | CHECK_EQ(2, break_point_hit_count); |
| 1075 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1076 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1077 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | // Test that a break point can be set at an IC load location. |
| 1082 | TEST(BreakPointICLoad) { |
| 1083 | break_point_hit_count = 0; |
| 1084 | v8::HandleScope scope; |
| 1085 | DebugLocalContext env; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1086 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1087 | v8::Undefined()); |
| 1088 | v8::Script::Compile(v8::String::New("bar=1"))->Run(); |
| 1089 | v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run(); |
| 1090 | v8::Local<v8::Function> foo = |
| 1091 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1092 | |
| 1093 | // Run without breakpoints. |
| 1094 | foo->Call(env->Global(), 0, NULL); |
| 1095 | CHECK_EQ(0, break_point_hit_count); |
| 1096 | |
| 1097 | // Run with breakpoint |
| 1098 | int bp = SetBreakPoint(foo, 0); |
| 1099 | foo->Call(env->Global(), 0, NULL); |
| 1100 | CHECK_EQ(1, break_point_hit_count); |
| 1101 | foo->Call(env->Global(), 0, NULL); |
| 1102 | CHECK_EQ(2, break_point_hit_count); |
| 1103 | |
| 1104 | // Run without breakpoints. |
| 1105 | ClearBreakPoint(bp); |
| 1106 | foo->Call(env->Global(), 0, NULL); |
| 1107 | CHECK_EQ(2, break_point_hit_count); |
| 1108 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1109 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1110 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | // Test that a break point can be set at an IC call location. |
| 1115 | TEST(BreakPointICCall) { |
| 1116 | break_point_hit_count = 0; |
| 1117 | v8::HandleScope scope; |
| 1118 | DebugLocalContext env; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1119 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1120 | v8::Undefined()); |
| 1121 | v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); |
| 1122 | v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run(); |
| 1123 | v8::Local<v8::Function> foo = |
| 1124 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1125 | |
| 1126 | // Run without breakpoints. |
| 1127 | foo->Call(env->Global(), 0, NULL); |
| 1128 | CHECK_EQ(0, break_point_hit_count); |
| 1129 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1130 | // Run with breakpoint. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1131 | int bp = SetBreakPoint(foo, 0); |
| 1132 | foo->Call(env->Global(), 0, NULL); |
| 1133 | CHECK_EQ(1, break_point_hit_count); |
| 1134 | foo->Call(env->Global(), 0, NULL); |
| 1135 | CHECK_EQ(2, break_point_hit_count); |
| 1136 | |
| 1137 | // Run without breakpoints. |
| 1138 | ClearBreakPoint(bp); |
| 1139 | foo->Call(env->Global(), 0, NULL); |
| 1140 | CHECK_EQ(2, break_point_hit_count); |
| 1141 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1142 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1143 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1147 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1148 | TEST(BreakPointICCallWithGC) { |
| 1149 | break_point_hit_count = 0; |
| 1150 | v8::HandleScope scope; |
| 1151 | DebugLocalContext env; |
| 1152 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
| 1153 | v8::Undefined()); |
| 1154 | v8::Script::Compile(v8::String::New("function bar(){return 1;}"))->Run(); |
| 1155 | v8::Script::Compile(v8::String::New("function foo(){return bar();}"))->Run(); |
| 1156 | v8::Local<v8::Function> foo = |
| 1157 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1158 | |
| 1159 | // Run without breakpoints. |
| 1160 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1161 | CHECK_EQ(0, break_point_hit_count); |
| 1162 | |
| 1163 | // Run with breakpoint. |
| 1164 | int bp = SetBreakPoint(foo, 0); |
| 1165 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1166 | CHECK_EQ(1, break_point_hit_count); |
| 1167 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1168 | CHECK_EQ(2, break_point_hit_count); |
| 1169 | |
| 1170 | // Run without breakpoints. |
| 1171 | ClearBreakPoint(bp); |
| 1172 | foo->Call(env->Global(), 0, NULL); |
| 1173 | CHECK_EQ(2, break_point_hit_count); |
| 1174 | |
| 1175 | v8::Debug::SetDebugEventListener(NULL); |
| 1176 | CheckDebuggerUnloaded(); |
| 1177 | } |
| 1178 | |
| 1179 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1180 | // Test that a break point can be set at an IC call location and survive a GC. |
| 1181 | TEST(BreakPointConstructCallWithGC) { |
| 1182 | break_point_hit_count = 0; |
| 1183 | v8::HandleScope scope; |
| 1184 | DebugLocalContext env; |
| 1185 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
| 1186 | v8::Undefined()); |
| 1187 | v8::Script::Compile(v8::String::New("function bar(){ this.x = 1;}"))->Run(); |
| 1188 | v8::Script::Compile(v8::String::New( |
| 1189 | "function foo(){return new bar(1).x;}"))->Run(); |
| 1190 | v8::Local<v8::Function> foo = |
| 1191 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1192 | |
| 1193 | // Run without breakpoints. |
| 1194 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1195 | CHECK_EQ(0, break_point_hit_count); |
| 1196 | |
| 1197 | // Run with breakpoint. |
| 1198 | int bp = SetBreakPoint(foo, 0); |
| 1199 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1200 | CHECK_EQ(1, break_point_hit_count); |
| 1201 | CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); |
| 1202 | CHECK_EQ(2, break_point_hit_count); |
| 1203 | |
| 1204 | // Run without breakpoints. |
| 1205 | ClearBreakPoint(bp); |
| 1206 | foo->Call(env->Global(), 0, NULL); |
| 1207 | CHECK_EQ(2, break_point_hit_count); |
| 1208 | |
| 1209 | v8::Debug::SetDebugEventListener(NULL); |
| 1210 | CheckDebuggerUnloaded(); |
| 1211 | } |
| 1212 | |
| 1213 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1214 | // Test that a break point can be set at a return store location. |
| 1215 | TEST(BreakPointReturn) { |
| 1216 | break_point_hit_count = 0; |
| 1217 | v8::HandleScope scope; |
| 1218 | DebugLocalContext env; |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Create a functions for checking the source line and column when hitting |
| 1221 | // a break point. |
| 1222 | frame_source_line = CompileFunction(&env, |
| 1223 | frame_source_line_source, |
| 1224 | "frame_source_line"); |
| 1225 | frame_source_column = CompileFunction(&env, |
| 1226 | frame_source_column_source, |
| 1227 | "frame_source_column"); |
| 1228 | |
| 1229 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1230 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1231 | v8::Undefined()); |
| 1232 | v8::Script::Compile(v8::String::New("function foo(){}"))->Run(); |
| 1233 | v8::Local<v8::Function> foo = |
| 1234 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 1235 | |
| 1236 | // Run without breakpoints. |
| 1237 | foo->Call(env->Global(), 0, NULL); |
| 1238 | CHECK_EQ(0, break_point_hit_count); |
| 1239 | |
| 1240 | // Run with breakpoint |
| 1241 | int bp = SetBreakPoint(foo, 0); |
| 1242 | foo->Call(env->Global(), 0, NULL); |
| 1243 | CHECK_EQ(1, break_point_hit_count); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1244 | CHECK_EQ(0, last_source_line); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1245 | CHECK_EQ(15, last_source_column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1246 | foo->Call(env->Global(), 0, NULL); |
| 1247 | CHECK_EQ(2, break_point_hit_count); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1248 | CHECK_EQ(0, last_source_line); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1249 | CHECK_EQ(15, last_source_column); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Run without breakpoints. |
| 1252 | ClearBreakPoint(bp); |
| 1253 | foo->Call(env->Global(), 0, NULL); |
| 1254 | CHECK_EQ(2, break_point_hit_count); |
| 1255 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1256 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1257 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | static void CallWithBreakPoints(v8::Local<v8::Object> recv, |
| 1262 | v8::Local<v8::Function> f, |
| 1263 | int break_point_count, |
| 1264 | int call_count) { |
| 1265 | break_point_hit_count = 0; |
| 1266 | for (int i = 0; i < call_count; i++) { |
| 1267 | f->Call(recv, 0, NULL); |
| 1268 | CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // Test GC during break point processing. |
| 1273 | TEST(GCDuringBreakPointProcessing) { |
| 1274 | break_point_hit_count = 0; |
| 1275 | v8::HandleScope scope; |
| 1276 | DebugLocalContext env; |
| 1277 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1278 | v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1279 | v8::Undefined()); |
| 1280 | v8::Local<v8::Function> foo; |
| 1281 | |
| 1282 | // Test IC store break point with garbage collection. |
| 1283 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1284 | SetBreakPoint(foo, 0); |
| 1285 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1286 | |
| 1287 | // Test IC load break point with garbage collection. |
| 1288 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1289 | SetBreakPoint(foo, 0); |
| 1290 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1291 | |
| 1292 | // Test IC call break point with garbage collection. |
| 1293 | foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); |
| 1294 | SetBreakPoint(foo, 0); |
| 1295 | CallWithBreakPoints(env->Global(), foo, 1, 10); |
| 1296 | |
| 1297 | // Test return break point with garbage collection. |
| 1298 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1299 | SetBreakPoint(foo, 0); |
| 1300 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1301 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1302 | // Test debug break slot break point with garbage collection. |
| 1303 | foo = CompileFunction(&env, "function foo(){var a;}", "foo"); |
| 1304 | SetBreakPoint(foo, 0); |
| 1305 | CallWithBreakPoints(env->Global(), foo, 1, 25); |
| 1306 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1307 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1308 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | // Call the function three times with different garbage collections in between |
| 1313 | // and make sure that the break point survives. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1314 | static void CallAndGC(v8::Local<v8::Object> recv, |
| 1315 | v8::Local<v8::Function> f, |
| 1316 | bool force_compaction) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1317 | break_point_hit_count = 0; |
| 1318 | |
| 1319 | for (int i = 0; i < 3; i++) { |
| 1320 | // Call function. |
| 1321 | f->Call(recv, 0, NULL); |
| 1322 | CHECK_EQ(1 + i * 3, break_point_hit_count); |
| 1323 | |
| 1324 | // Scavenge and call function. |
| 1325 | Heap::CollectGarbage(0, v8::internal::NEW_SPACE); |
| 1326 | f->Call(recv, 0, NULL); |
| 1327 | CHECK_EQ(2 + i * 3, break_point_hit_count); |
| 1328 | |
| 1329 | // Mark sweep (and perhaps compact) and call function. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1330 | Heap::CollectAllGarbage(force_compaction); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1331 | f->Call(recv, 0, NULL); |
| 1332 | CHECK_EQ(3 + i * 3, break_point_hit_count); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1337 | static void TestBreakPointSurviveGC(bool force_compaction) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1338 | break_point_hit_count = 0; |
| 1339 | v8::HandleScope scope; |
| 1340 | DebugLocalContext env; |
| 1341 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1342 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1343 | v8::Undefined()); |
| 1344 | v8::Local<v8::Function> foo; |
| 1345 | |
| 1346 | // Test IC store break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1347 | { |
| 1348 | v8::Local<v8::Function> bar = |
| 1349 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1350 | foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); |
| 1351 | SetBreakPoint(foo, 0); |
| 1352 | } |
| 1353 | CallAndGC(env->Global(), foo, force_compaction); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Test IC load break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1356 | { |
| 1357 | v8::Local<v8::Function> bar = |
| 1358 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1359 | foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); |
| 1360 | SetBreakPoint(foo, 0); |
| 1361 | } |
| 1362 | CallAndGC(env->Global(), foo, force_compaction); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Test IC call break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1365 | { |
| 1366 | v8::Local<v8::Function> bar = |
| 1367 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1368 | foo = CompileFunction(&env, |
| 1369 | "function bar(){};function foo(){bar();}", |
| 1370 | "foo"); |
| 1371 | SetBreakPoint(foo, 0); |
| 1372 | } |
| 1373 | CallAndGC(env->Global(), foo, force_compaction); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1374 | |
| 1375 | // Test return break point with garbage collection. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1376 | { |
| 1377 | v8::Local<v8::Function> bar = |
| 1378 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1379 | foo = CompileFunction(&env, "function foo(){}", "foo"); |
| 1380 | SetBreakPoint(foo, 0); |
| 1381 | } |
| 1382 | CallAndGC(env->Global(), foo, force_compaction); |
| 1383 | |
| 1384 | // Test non IC break point with garbage collection. |
| 1385 | { |
| 1386 | v8::Local<v8::Function> bar = |
| 1387 | CompileFunction(&env, "function foo(){}", "foo"); |
| 1388 | foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); |
| 1389 | SetBreakPoint(foo, 0); |
| 1390 | } |
| 1391 | CallAndGC(env->Global(), foo, force_compaction); |
| 1392 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1393 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1394 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1395 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 1399 | // Test that a break point can be set at a return store location. |
| 1400 | TEST(BreakPointSurviveGC) { |
| 1401 | TestBreakPointSurviveGC(false); |
| 1402 | TestBreakPointSurviveGC(true); |
| 1403 | } |
| 1404 | |
| 1405 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1406 | // Test that break points can be set using the global Debug object. |
| 1407 | TEST(BreakPointThroughJavaScript) { |
| 1408 | break_point_hit_count = 0; |
| 1409 | v8::HandleScope scope; |
| 1410 | DebugLocalContext env; |
| 1411 | env.ExposeDebug(); |
| 1412 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1413 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1414 | v8::Undefined()); |
| 1415 | v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); |
| 1416 | v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run(); |
| 1417 | // 012345678901234567890 |
| 1418 | // 1 2 |
| 1419 | // Break points are set at position 3 and 9 |
| 1420 | v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()")); |
| 1421 | |
| 1422 | // Run without breakpoints. |
| 1423 | foo->Run(); |
| 1424 | CHECK_EQ(0, break_point_hit_count); |
| 1425 | |
| 1426 | // Run with one breakpoint |
| 1427 | int bp1 = SetBreakPointFromJS("foo", 0, 3); |
| 1428 | foo->Run(); |
| 1429 | CHECK_EQ(1, break_point_hit_count); |
| 1430 | foo->Run(); |
| 1431 | CHECK_EQ(2, break_point_hit_count); |
| 1432 | |
| 1433 | // Run with two breakpoints |
| 1434 | int bp2 = SetBreakPointFromJS("foo", 0, 9); |
| 1435 | foo->Run(); |
| 1436 | CHECK_EQ(4, break_point_hit_count); |
| 1437 | foo->Run(); |
| 1438 | CHECK_EQ(6, break_point_hit_count); |
| 1439 | |
| 1440 | // Run with one breakpoint |
| 1441 | ClearBreakPointFromJS(bp2); |
| 1442 | foo->Run(); |
| 1443 | CHECK_EQ(7, break_point_hit_count); |
| 1444 | foo->Run(); |
| 1445 | CHECK_EQ(8, break_point_hit_count); |
| 1446 | |
| 1447 | // Run without breakpoints. |
| 1448 | ClearBreakPointFromJS(bp1); |
| 1449 | foo->Run(); |
| 1450 | CHECK_EQ(8, break_point_hit_count); |
| 1451 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1452 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1453 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Make sure that the break point numbers are consecutive. |
| 1456 | CHECK_EQ(1, bp1); |
| 1457 | CHECK_EQ(2, bp2); |
| 1458 | } |
| 1459 | |
| 1460 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1461 | // Test that break points on scripts identified by name can be set using the |
| 1462 | // global Debug object. |
| 1463 | TEST(ScriptBreakPointByNameThroughJavaScript) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1464 | break_point_hit_count = 0; |
| 1465 | v8::HandleScope scope; |
| 1466 | DebugLocalContext env; |
| 1467 | env.ExposeDebug(); |
| 1468 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1469 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1470 | v8::Undefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1471 | |
| 1472 | v8::Local<v8::String> script = v8::String::New( |
| 1473 | "function f() {\n" |
| 1474 | " function h() {\n" |
| 1475 | " a = 0; // line 2\n" |
| 1476 | " }\n" |
| 1477 | " b = 1; // line 4\n" |
| 1478 | " return h();\n" |
| 1479 | "}\n" |
| 1480 | "\n" |
| 1481 | "function g() {\n" |
| 1482 | " function h() {\n" |
| 1483 | " a = 0;\n" |
| 1484 | " }\n" |
| 1485 | " b = 2; // line 12\n" |
| 1486 | " h();\n" |
| 1487 | " b = 3; // line 14\n" |
| 1488 | " f(); // line 15\n" |
| 1489 | "}"); |
| 1490 | |
| 1491 | // Compile the script and get the two functions. |
| 1492 | v8::ScriptOrigin origin = |
| 1493 | v8::ScriptOrigin(v8::String::New("test")); |
| 1494 | v8::Script::Compile(script, &origin)->Run(); |
| 1495 | v8::Local<v8::Function> f = |
| 1496 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1497 | v8::Local<v8::Function> g = |
| 1498 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 1499 | |
| 1500 | // Call f and g without break points. |
| 1501 | break_point_hit_count = 0; |
| 1502 | f->Call(env->Global(), 0, NULL); |
| 1503 | CHECK_EQ(0, break_point_hit_count); |
| 1504 | g->Call(env->Global(), 0, NULL); |
| 1505 | CHECK_EQ(0, break_point_hit_count); |
| 1506 | |
| 1507 | // Call f and g with break point on line 12. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1508 | int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1509 | break_point_hit_count = 0; |
| 1510 | f->Call(env->Global(), 0, NULL); |
| 1511 | CHECK_EQ(0, break_point_hit_count); |
| 1512 | g->Call(env->Global(), 0, NULL); |
| 1513 | CHECK_EQ(1, break_point_hit_count); |
| 1514 | |
| 1515 | // Remove the break point again. |
| 1516 | break_point_hit_count = 0; |
| 1517 | ClearBreakPointFromJS(sbp1); |
| 1518 | f->Call(env->Global(), 0, NULL); |
| 1519 | CHECK_EQ(0, break_point_hit_count); |
| 1520 | g->Call(env->Global(), 0, NULL); |
| 1521 | CHECK_EQ(0, break_point_hit_count); |
| 1522 | |
| 1523 | // Call f and g with break point on line 2. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1524 | int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1525 | break_point_hit_count = 0; |
| 1526 | f->Call(env->Global(), 0, NULL); |
| 1527 | CHECK_EQ(1, break_point_hit_count); |
| 1528 | g->Call(env->Global(), 0, NULL); |
| 1529 | CHECK_EQ(2, break_point_hit_count); |
| 1530 | |
| 1531 | // 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] | 1532 | int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0); |
| 1533 | int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0); |
| 1534 | int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0); |
| 1535 | int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1536 | break_point_hit_count = 0; |
| 1537 | f->Call(env->Global(), 0, NULL); |
| 1538 | CHECK_EQ(2, break_point_hit_count); |
| 1539 | g->Call(env->Global(), 0, NULL); |
| 1540 | CHECK_EQ(7, break_point_hit_count); |
| 1541 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1542 | // Remove all the break points again. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1543 | break_point_hit_count = 0; |
| 1544 | ClearBreakPointFromJS(sbp2); |
| 1545 | ClearBreakPointFromJS(sbp3); |
| 1546 | ClearBreakPointFromJS(sbp4); |
| 1547 | ClearBreakPointFromJS(sbp5); |
| 1548 | ClearBreakPointFromJS(sbp6); |
| 1549 | f->Call(env->Global(), 0, NULL); |
| 1550 | CHECK_EQ(0, break_point_hit_count); |
| 1551 | g->Call(env->Global(), 0, NULL); |
| 1552 | CHECK_EQ(0, break_point_hit_count); |
| 1553 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1554 | v8::Debug::SetDebugEventListener(NULL); |
| 1555 | CheckDebuggerUnloaded(); |
| 1556 | |
| 1557 | // Make sure that the break point numbers are consecutive. |
| 1558 | CHECK_EQ(1, sbp1); |
| 1559 | CHECK_EQ(2, sbp2); |
| 1560 | CHECK_EQ(3, sbp3); |
| 1561 | CHECK_EQ(4, sbp4); |
| 1562 | CHECK_EQ(5, sbp5); |
| 1563 | CHECK_EQ(6, sbp6); |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | TEST(ScriptBreakPointByIdThroughJavaScript) { |
| 1568 | break_point_hit_count = 0; |
| 1569 | v8::HandleScope scope; |
| 1570 | DebugLocalContext env; |
| 1571 | env.ExposeDebug(); |
| 1572 | |
| 1573 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 1574 | v8::Undefined()); |
| 1575 | |
| 1576 | v8::Local<v8::String> source = v8::String::New( |
| 1577 | "function f() {\n" |
| 1578 | " function h() {\n" |
| 1579 | " a = 0; // line 2\n" |
| 1580 | " }\n" |
| 1581 | " b = 1; // line 4\n" |
| 1582 | " return h();\n" |
| 1583 | "}\n" |
| 1584 | "\n" |
| 1585 | "function g() {\n" |
| 1586 | " function h() {\n" |
| 1587 | " a = 0;\n" |
| 1588 | " }\n" |
| 1589 | " b = 2; // line 12\n" |
| 1590 | " h();\n" |
| 1591 | " b = 3; // line 14\n" |
| 1592 | " f(); // line 15\n" |
| 1593 | "}"); |
| 1594 | |
| 1595 | // Compile the script and get the two functions. |
| 1596 | v8::ScriptOrigin origin = |
| 1597 | v8::ScriptOrigin(v8::String::New("test")); |
| 1598 | v8::Local<v8::Script> script = v8::Script::Compile(source, &origin); |
| 1599 | script->Run(); |
| 1600 | v8::Local<v8::Function> f = |
| 1601 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1602 | v8::Local<v8::Function> g = |
| 1603 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 1604 | |
| 1605 | // Get the script id knowing that internally it is a 32 integer. |
| 1606 | uint32_t script_id = script->Id()->Uint32Value(); |
| 1607 | |
| 1608 | // Call f and g without break points. |
| 1609 | break_point_hit_count = 0; |
| 1610 | f->Call(env->Global(), 0, NULL); |
| 1611 | CHECK_EQ(0, break_point_hit_count); |
| 1612 | g->Call(env->Global(), 0, NULL); |
| 1613 | CHECK_EQ(0, break_point_hit_count); |
| 1614 | |
| 1615 | // Call f and g with break point on line 12. |
| 1616 | int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); |
| 1617 | break_point_hit_count = 0; |
| 1618 | f->Call(env->Global(), 0, NULL); |
| 1619 | CHECK_EQ(0, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1620 | g->Call(env->Global(), 0, NULL); |
| 1621 | CHECK_EQ(1, break_point_hit_count); |
| 1622 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1623 | // Remove the break point again. |
| 1624 | break_point_hit_count = 0; |
| 1625 | ClearBreakPointFromJS(sbp1); |
| 1626 | f->Call(env->Global(), 0, NULL); |
| 1627 | CHECK_EQ(0, break_point_hit_count); |
| 1628 | g->Call(env->Global(), 0, NULL); |
| 1629 | CHECK_EQ(0, break_point_hit_count); |
| 1630 | |
| 1631 | // Call f and g with break point on line 2. |
| 1632 | int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0); |
| 1633 | break_point_hit_count = 0; |
| 1634 | f->Call(env->Global(), 0, NULL); |
| 1635 | CHECK_EQ(1, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1636 | g->Call(env->Global(), 0, NULL); |
| 1637 | CHECK_EQ(2, break_point_hit_count); |
| 1638 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1639 | // Call f and g with break point on line 2, 4, 12, 14 and 15. |
| 1640 | int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0); |
| 1641 | int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); |
| 1642 | int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0); |
| 1643 | int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0); |
| 1644 | break_point_hit_count = 0; |
| 1645 | f->Call(env->Global(), 0, NULL); |
| 1646 | CHECK_EQ(2, break_point_hit_count); |
| 1647 | g->Call(env->Global(), 0, NULL); |
| 1648 | CHECK_EQ(7, break_point_hit_count); |
| 1649 | |
| 1650 | // Remove all the break points again. |
| 1651 | break_point_hit_count = 0; |
| 1652 | ClearBreakPointFromJS(sbp2); |
| 1653 | ClearBreakPointFromJS(sbp3); |
| 1654 | ClearBreakPointFromJS(sbp4); |
| 1655 | ClearBreakPointFromJS(sbp5); |
| 1656 | ClearBreakPointFromJS(sbp6); |
| 1657 | f->Call(env->Global(), 0, NULL); |
| 1658 | CHECK_EQ(0, break_point_hit_count); |
| 1659 | g->Call(env->Global(), 0, NULL); |
| 1660 | CHECK_EQ(0, break_point_hit_count); |
| 1661 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1662 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1663 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1664 | |
| 1665 | // Make sure that the break point numbers are consecutive. |
| 1666 | CHECK_EQ(1, sbp1); |
| 1667 | CHECK_EQ(2, sbp2); |
| 1668 | CHECK_EQ(3, sbp3); |
| 1669 | CHECK_EQ(4, sbp4); |
| 1670 | CHECK_EQ(5, sbp5); |
| 1671 | CHECK_EQ(6, sbp6); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | |
| 1675 | // Test conditional script break points. |
| 1676 | TEST(EnableDisableScriptBreakPoint) { |
| 1677 | break_point_hit_count = 0; |
| 1678 | v8::HandleScope scope; |
| 1679 | DebugLocalContext env; |
| 1680 | env.ExposeDebug(); |
| 1681 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1682 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1683 | v8::Undefined()); |
| 1684 | |
| 1685 | v8::Local<v8::String> script = v8::String::New( |
| 1686 | "function f() {\n" |
| 1687 | " a = 0; // line 1\n" |
| 1688 | "};"); |
| 1689 | |
| 1690 | // Compile the script and get function f. |
| 1691 | v8::ScriptOrigin origin = |
| 1692 | v8::ScriptOrigin(v8::String::New("test")); |
| 1693 | v8::Script::Compile(script, &origin)->Run(); |
| 1694 | v8::Local<v8::Function> f = |
| 1695 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1696 | |
| 1697 | // Set script break point on line 1 (in function f). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1698 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Call f while enabeling and disabling the script break point. |
| 1701 | break_point_hit_count = 0; |
| 1702 | f->Call(env->Global(), 0, NULL); |
| 1703 | CHECK_EQ(1, break_point_hit_count); |
| 1704 | |
| 1705 | DisableScriptBreakPointFromJS(sbp); |
| 1706 | f->Call(env->Global(), 0, NULL); |
| 1707 | CHECK_EQ(1, break_point_hit_count); |
| 1708 | |
| 1709 | EnableScriptBreakPointFromJS(sbp); |
| 1710 | f->Call(env->Global(), 0, NULL); |
| 1711 | CHECK_EQ(2, break_point_hit_count); |
| 1712 | |
| 1713 | DisableScriptBreakPointFromJS(sbp); |
| 1714 | f->Call(env->Global(), 0, NULL); |
| 1715 | CHECK_EQ(2, break_point_hit_count); |
| 1716 | |
| 1717 | // Reload the script and get f again checking that the disabeling survives. |
| 1718 | v8::Script::Compile(script, &origin)->Run(); |
| 1719 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1720 | f->Call(env->Global(), 0, NULL); |
| 1721 | CHECK_EQ(2, break_point_hit_count); |
| 1722 | |
| 1723 | EnableScriptBreakPointFromJS(sbp); |
| 1724 | f->Call(env->Global(), 0, NULL); |
| 1725 | CHECK_EQ(3, break_point_hit_count); |
| 1726 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1727 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1728 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | // Test conditional script break points. |
| 1733 | TEST(ConditionalScriptBreakPoint) { |
| 1734 | break_point_hit_count = 0; |
| 1735 | v8::HandleScope scope; |
| 1736 | DebugLocalContext env; |
| 1737 | env.ExposeDebug(); |
| 1738 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1739 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1740 | v8::Undefined()); |
| 1741 | |
| 1742 | v8::Local<v8::String> script = v8::String::New( |
| 1743 | "count = 0;\n" |
| 1744 | "function f() {\n" |
| 1745 | " g(count++); // line 2\n" |
| 1746 | "};\n" |
| 1747 | "function g(x) {\n" |
| 1748 | " var a=x; // line 5\n" |
| 1749 | "};"); |
| 1750 | |
| 1751 | // Compile the script and get function f. |
| 1752 | v8::ScriptOrigin origin = |
| 1753 | v8::ScriptOrigin(v8::String::New("test")); |
| 1754 | v8::Script::Compile(script, &origin)->Run(); |
| 1755 | v8::Local<v8::Function> f = |
| 1756 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1757 | |
| 1758 | // Set script break point on line 5 (in function g). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1759 | int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Call f with different conditions on the script break point. |
| 1762 | break_point_hit_count = 0; |
| 1763 | ChangeScriptBreakPointConditionFromJS(sbp1, "false"); |
| 1764 | f->Call(env->Global(), 0, NULL); |
| 1765 | CHECK_EQ(0, break_point_hit_count); |
| 1766 | |
| 1767 | ChangeScriptBreakPointConditionFromJS(sbp1, "true"); |
| 1768 | break_point_hit_count = 0; |
| 1769 | f->Call(env->Global(), 0, NULL); |
| 1770 | CHECK_EQ(1, break_point_hit_count); |
| 1771 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1772 | ChangeScriptBreakPointConditionFromJS(sbp1, "x % 2 == 0"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1773 | break_point_hit_count = 0; |
| 1774 | for (int i = 0; i < 10; i++) { |
| 1775 | f->Call(env->Global(), 0, NULL); |
| 1776 | } |
| 1777 | CHECK_EQ(5, break_point_hit_count); |
| 1778 | |
| 1779 | // Reload the script and get f again checking that the condition survives. |
| 1780 | v8::Script::Compile(script, &origin)->Run(); |
| 1781 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1782 | |
| 1783 | break_point_hit_count = 0; |
| 1784 | for (int i = 0; i < 10; i++) { |
| 1785 | f->Call(env->Global(), 0, NULL); |
| 1786 | } |
| 1787 | CHECK_EQ(5, break_point_hit_count); |
| 1788 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1789 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1790 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | // Test ignore count on script break points. |
| 1795 | TEST(ScriptBreakPointIgnoreCount) { |
| 1796 | break_point_hit_count = 0; |
| 1797 | v8::HandleScope scope; |
| 1798 | DebugLocalContext env; |
| 1799 | env.ExposeDebug(); |
| 1800 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1801 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1802 | v8::Undefined()); |
| 1803 | |
| 1804 | v8::Local<v8::String> script = v8::String::New( |
| 1805 | "function f() {\n" |
| 1806 | " a = 0; // line 1\n" |
| 1807 | "};"); |
| 1808 | |
| 1809 | // Compile the script and get function f. |
| 1810 | v8::ScriptOrigin origin = |
| 1811 | v8::ScriptOrigin(v8::String::New("test")); |
| 1812 | v8::Script::Compile(script, &origin)->Run(); |
| 1813 | v8::Local<v8::Function> f = |
| 1814 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1815 | |
| 1816 | // Set script break point on line 1 (in function f). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1817 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1818 | |
| 1819 | // Call f with different ignores on the script break point. |
| 1820 | break_point_hit_count = 0; |
| 1821 | ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1); |
| 1822 | f->Call(env->Global(), 0, NULL); |
| 1823 | CHECK_EQ(0, break_point_hit_count); |
| 1824 | f->Call(env->Global(), 0, NULL); |
| 1825 | CHECK_EQ(1, break_point_hit_count); |
| 1826 | |
| 1827 | ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5); |
| 1828 | break_point_hit_count = 0; |
| 1829 | for (int i = 0; i < 10; i++) { |
| 1830 | f->Call(env->Global(), 0, NULL); |
| 1831 | } |
| 1832 | CHECK_EQ(5, break_point_hit_count); |
| 1833 | |
| 1834 | // Reload the script and get f again checking that the ignore survives. |
| 1835 | v8::Script::Compile(script, &origin)->Run(); |
| 1836 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1837 | |
| 1838 | break_point_hit_count = 0; |
| 1839 | for (int i = 0; i < 10; i++) { |
| 1840 | f->Call(env->Global(), 0, NULL); |
| 1841 | } |
| 1842 | CHECK_EQ(5, break_point_hit_count); |
| 1843 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1844 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1845 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | // Test that script break points survive when a script is reloaded. |
| 1850 | TEST(ScriptBreakPointReload) { |
| 1851 | break_point_hit_count = 0; |
| 1852 | v8::HandleScope scope; |
| 1853 | DebugLocalContext env; |
| 1854 | env.ExposeDebug(); |
| 1855 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1856 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1857 | v8::Undefined()); |
| 1858 | |
| 1859 | v8::Local<v8::Function> f; |
| 1860 | v8::Local<v8::String> script = v8::String::New( |
| 1861 | "function f() {\n" |
| 1862 | " function h() {\n" |
| 1863 | " a = 0; // line 2\n" |
| 1864 | " }\n" |
| 1865 | " b = 1; // line 4\n" |
| 1866 | " return h();\n" |
| 1867 | "}"); |
| 1868 | |
| 1869 | v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1")); |
| 1870 | v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2")); |
| 1871 | |
| 1872 | // Set a script break point before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1873 | SetScriptBreakPointByNameFromJS("1", 2, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Compile the script and get the function. |
| 1876 | v8::Script::Compile(script, &origin_1)->Run(); |
| 1877 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1878 | |
| 1879 | // Call f and check that the script break point is active. |
| 1880 | break_point_hit_count = 0; |
| 1881 | f->Call(env->Global(), 0, NULL); |
| 1882 | CHECK_EQ(1, break_point_hit_count); |
| 1883 | |
| 1884 | // Compile the script again with a different script data and get the |
| 1885 | // function. |
| 1886 | v8::Script::Compile(script, &origin_2)->Run(); |
| 1887 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1888 | |
| 1889 | // Call f and check that no break points are set. |
| 1890 | break_point_hit_count = 0; |
| 1891 | f->Call(env->Global(), 0, NULL); |
| 1892 | CHECK_EQ(0, break_point_hit_count); |
| 1893 | |
| 1894 | // Compile the script again and get the function. |
| 1895 | v8::Script::Compile(script, &origin_1)->Run(); |
| 1896 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1897 | |
| 1898 | // Call f and check that the script break point is active. |
| 1899 | break_point_hit_count = 0; |
| 1900 | f->Call(env->Global(), 0, NULL); |
| 1901 | CHECK_EQ(1, break_point_hit_count); |
| 1902 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1903 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1904 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | |
| 1908 | // Test when several scripts has the same script data |
| 1909 | TEST(ScriptBreakPointMultiple) { |
| 1910 | break_point_hit_count = 0; |
| 1911 | v8::HandleScope scope; |
| 1912 | DebugLocalContext env; |
| 1913 | env.ExposeDebug(); |
| 1914 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1915 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1916 | v8::Undefined()); |
| 1917 | |
| 1918 | v8::Local<v8::Function> f; |
| 1919 | v8::Local<v8::String> script_f = v8::String::New( |
| 1920 | "function f() {\n" |
| 1921 | " a = 0; // line 1\n" |
| 1922 | "}"); |
| 1923 | |
| 1924 | v8::Local<v8::Function> g; |
| 1925 | v8::Local<v8::String> script_g = v8::String::New( |
| 1926 | "function g() {\n" |
| 1927 | " b = 0; // line 1\n" |
| 1928 | "}"); |
| 1929 | |
| 1930 | v8::ScriptOrigin origin = |
| 1931 | v8::ScriptOrigin(v8::String::New("test")); |
| 1932 | |
| 1933 | // Set a script break point before the scripts are loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1934 | int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Compile the scripts with same script data and get the functions. |
| 1937 | v8::Script::Compile(script_f, &origin)->Run(); |
| 1938 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 1939 | v8::Script::Compile(script_g, &origin)->Run(); |
| 1940 | g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 1941 | |
| 1942 | // Call f and g and check that the script break point is active. |
| 1943 | break_point_hit_count = 0; |
| 1944 | f->Call(env->Global(), 0, NULL); |
| 1945 | CHECK_EQ(1, break_point_hit_count); |
| 1946 | g->Call(env->Global(), 0, NULL); |
| 1947 | CHECK_EQ(2, break_point_hit_count); |
| 1948 | |
| 1949 | // Clear the script break point. |
| 1950 | ClearBreakPointFromJS(sbp); |
| 1951 | |
| 1952 | // Call f and g and check that the script break point is no longer active. |
| 1953 | break_point_hit_count = 0; |
| 1954 | f->Call(env->Global(), 0, NULL); |
| 1955 | CHECK_EQ(0, break_point_hit_count); |
| 1956 | g->Call(env->Global(), 0, NULL); |
| 1957 | CHECK_EQ(0, break_point_hit_count); |
| 1958 | |
| 1959 | // Set script break point with the scripts loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1960 | sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1961 | |
| 1962 | // Call f and g and check that the script break point is active. |
| 1963 | break_point_hit_count = 0; |
| 1964 | f->Call(env->Global(), 0, NULL); |
| 1965 | CHECK_EQ(1, break_point_hit_count); |
| 1966 | g->Call(env->Global(), 0, NULL); |
| 1967 | CHECK_EQ(2, break_point_hit_count); |
| 1968 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1969 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1970 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | |
| 1974 | // Test the script origin which has both name and line offset. |
| 1975 | TEST(ScriptBreakPointLineOffset) { |
| 1976 | break_point_hit_count = 0; |
| 1977 | v8::HandleScope scope; |
| 1978 | DebugLocalContext env; |
| 1979 | env.ExposeDebug(); |
| 1980 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1981 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1982 | v8::Undefined()); |
| 1983 | |
| 1984 | v8::Local<v8::Function> f; |
| 1985 | v8::Local<v8::String> script = v8::String::New( |
| 1986 | "function f() {\n" |
| 1987 | " a = 0; // line 8 as this script has line offset 7\n" |
| 1988 | " b = 0; // line 9 as this script has line offset 7\n" |
| 1989 | "}"); |
| 1990 | |
| 1991 | // Create script origin both name and line offset. |
| 1992 | v8::ScriptOrigin origin(v8::String::New("test.html"), |
| 1993 | v8::Integer::New(7)); |
| 1994 | |
| 1995 | // Set two script break points before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1996 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0); |
| 1997 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1998 | |
| 1999 | // Compile the script and get the function. |
| 2000 | v8::Script::Compile(script, &origin)->Run(); |
| 2001 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2002 | |
| 2003 | // Call f and check that the script break point is active. |
| 2004 | break_point_hit_count = 0; |
| 2005 | f->Call(env->Global(), 0, NULL); |
| 2006 | CHECK_EQ(2, break_point_hit_count); |
| 2007 | |
| 2008 | // Clear the script break points. |
| 2009 | ClearBreakPointFromJS(sbp1); |
| 2010 | ClearBreakPointFromJS(sbp2); |
| 2011 | |
| 2012 | // Call f and check that no script break points are active. |
| 2013 | break_point_hit_count = 0; |
| 2014 | f->Call(env->Global(), 0, NULL); |
| 2015 | CHECK_EQ(0, break_point_hit_count); |
| 2016 | |
| 2017 | // Set a script break point with the script loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Call f and check that the script break point is active. |
| 2021 | break_point_hit_count = 0; |
| 2022 | f->Call(env->Global(), 0, NULL); |
| 2023 | CHECK_EQ(1, break_point_hit_count); |
| 2024 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2025 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2026 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | |
| 2030 | // Test script break points set on lines. |
| 2031 | TEST(ScriptBreakPointLine) { |
| 2032 | v8::HandleScope scope; |
| 2033 | DebugLocalContext env; |
| 2034 | env.ExposeDebug(); |
| 2035 | |
| 2036 | // Create a function for checking the function when hitting a break point. |
| 2037 | frame_function_name = CompileFunction(&env, |
| 2038 | frame_function_name_source, |
| 2039 | "frame_function_name"); |
| 2040 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2041 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2042 | v8::Undefined()); |
| 2043 | |
| 2044 | v8::Local<v8::Function> f; |
| 2045 | v8::Local<v8::Function> g; |
| 2046 | v8::Local<v8::String> script = v8::String::New( |
| 2047 | "a = 0 // line 0\n" |
| 2048 | "function f() {\n" |
| 2049 | " a = 1; // line 2\n" |
| 2050 | "}\n" |
| 2051 | " a = 2; // line 4\n" |
| 2052 | " /* xx */ function g() { // line 5\n" |
| 2053 | " function h() { // line 6\n" |
| 2054 | " a = 3; // line 7\n" |
| 2055 | " }\n" |
| 2056 | " h(); // line 9\n" |
| 2057 | " a = 4; // line 10\n" |
| 2058 | " }\n" |
| 2059 | " a=5; // line 12"); |
| 2060 | |
| 2061 | // Set a couple script break point before the script is loaded. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2062 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1); |
| 2063 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1); |
| 2064 | int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2065 | |
| 2066 | // Compile the script and get the function. |
| 2067 | break_point_hit_count = 0; |
| 2068 | v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0)); |
| 2069 | v8::Script::Compile(script, &origin)->Run(); |
| 2070 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2071 | g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 2072 | |
| 2073 | // Chesk that a break point was hit when the script was run. |
| 2074 | CHECK_EQ(1, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2075 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2076 | |
| 2077 | // Call f and check that the script break point. |
| 2078 | f->Call(env->Global(), 0, NULL); |
| 2079 | CHECK_EQ(2, break_point_hit_count); |
| 2080 | CHECK_EQ("f", last_function_hit); |
| 2081 | |
| 2082 | // Call g and check that the script break point. |
| 2083 | g->Call(env->Global(), 0, NULL); |
| 2084 | CHECK_EQ(3, break_point_hit_count); |
| 2085 | CHECK_EQ("g", last_function_hit); |
| 2086 | |
| 2087 | // Clear the script break point on g and set one on h. |
| 2088 | ClearBreakPointFromJS(sbp3); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2089 | int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Call g and check that the script break point in h is hit. |
| 2092 | g->Call(env->Global(), 0, NULL); |
| 2093 | CHECK_EQ(4, break_point_hit_count); |
| 2094 | CHECK_EQ("h", last_function_hit); |
| 2095 | |
| 2096 | // Clear break points in f and h. Set a new one in the script between |
| 2097 | // functions f and g and test that there is no break points in f and g any |
| 2098 | // more. |
| 2099 | ClearBreakPointFromJS(sbp2); |
| 2100 | ClearBreakPointFromJS(sbp4); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2101 | int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2102 | break_point_hit_count = 0; |
| 2103 | f->Call(env->Global(), 0, NULL); |
| 2104 | g->Call(env->Global(), 0, NULL); |
| 2105 | CHECK_EQ(0, break_point_hit_count); |
| 2106 | |
| 2107 | // Reload the script which should hit two break points. |
| 2108 | break_point_hit_count = 0; |
| 2109 | v8::Script::Compile(script, &origin)->Run(); |
| 2110 | CHECK_EQ(2, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2111 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2112 | |
| 2113 | // 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] | 2114 | int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Reload the script which should hit three break points. |
| 2117 | break_point_hit_count = 0; |
| 2118 | v8::Script::Compile(script, &origin)->Run(); |
| 2119 | CHECK_EQ(3, break_point_hit_count); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2120 | CHECK_EQ(0, StrLength(last_function_hit)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2121 | |
| 2122 | // Clear the last break points, and reload the script which should not hit any |
| 2123 | // break points. |
| 2124 | ClearBreakPointFromJS(sbp1); |
| 2125 | ClearBreakPointFromJS(sbp5); |
| 2126 | ClearBreakPointFromJS(sbp6); |
| 2127 | break_point_hit_count = 0; |
| 2128 | v8::Script::Compile(script, &origin)->Run(); |
| 2129 | CHECK_EQ(0, break_point_hit_count); |
| 2130 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2131 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2132 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2136 | // Test top level script break points set on lines. |
| 2137 | TEST(ScriptBreakPointLineTopLevel) { |
| 2138 | v8::HandleScope scope; |
| 2139 | DebugLocalContext env; |
| 2140 | env.ExposeDebug(); |
| 2141 | |
| 2142 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2143 | v8::Undefined()); |
| 2144 | |
| 2145 | v8::Local<v8::String> script = v8::String::New( |
| 2146 | "function f() {\n" |
| 2147 | " a = 1; // line 1\n" |
| 2148 | "}\n" |
| 2149 | "a = 2; // line 3\n"); |
| 2150 | v8::Local<v8::Function> f; |
| 2151 | { |
| 2152 | v8::HandleScope scope; |
| 2153 | v8::Script::Compile(script, v8::String::New("test.html"))->Run(); |
| 2154 | } |
| 2155 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2156 | |
| 2157 | Heap::CollectAllGarbage(false); |
| 2158 | |
| 2159 | SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2160 | |
| 2161 | // Call f and check that there was no break points. |
| 2162 | break_point_hit_count = 0; |
| 2163 | f->Call(env->Global(), 0, NULL); |
| 2164 | CHECK_EQ(0, break_point_hit_count); |
| 2165 | |
| 2166 | // Recompile and run script and check that break point was hit. |
| 2167 | break_point_hit_count = 0; |
| 2168 | v8::Script::Compile(script, v8::String::New("test.html"))->Run(); |
| 2169 | CHECK_EQ(1, break_point_hit_count); |
| 2170 | |
| 2171 | // Call f and check that there are still no break points. |
| 2172 | break_point_hit_count = 0; |
| 2173 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 2174 | CHECK_EQ(0, break_point_hit_count); |
| 2175 | |
| 2176 | v8::Debug::SetDebugEventListener(NULL); |
| 2177 | CheckDebuggerUnloaded(); |
| 2178 | } |
| 2179 | |
| 2180 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2181 | // Test that it is possible to add and remove break points in a top level |
| 2182 | // function which has no references but has not been collected yet. |
| 2183 | TEST(ScriptBreakPointTopLevelCrash) { |
| 2184 | v8::HandleScope scope; |
| 2185 | DebugLocalContext env; |
| 2186 | env.ExposeDebug(); |
| 2187 | |
| 2188 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2189 | v8::Undefined()); |
| 2190 | |
| 2191 | v8::Local<v8::String> script_source = v8::String::New( |
| 2192 | "function f() {\n" |
| 2193 | " return 0;\n" |
| 2194 | "}\n" |
| 2195 | "f()"); |
| 2196 | |
| 2197 | int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2198 | { |
| 2199 | v8::HandleScope scope; |
| 2200 | break_point_hit_count = 0; |
| 2201 | v8::Script::Compile(script_source, v8::String::New("test.html"))->Run(); |
| 2202 | CHECK_EQ(1, break_point_hit_count); |
| 2203 | } |
| 2204 | |
| 2205 | int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); |
| 2206 | ClearBreakPointFromJS(sbp1); |
| 2207 | ClearBreakPointFromJS(sbp2); |
| 2208 | |
| 2209 | v8::Debug::SetDebugEventListener(NULL); |
| 2210 | CheckDebuggerUnloaded(); |
| 2211 | } |
| 2212 | |
| 2213 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2214 | // Test that it is possible to remove the last break point for a function |
| 2215 | // inside the break handling of that break point. |
| 2216 | TEST(RemoveBreakPointInBreak) { |
| 2217 | v8::HandleScope scope; |
| 2218 | DebugLocalContext env; |
| 2219 | |
| 2220 | v8::Local<v8::Function> foo = |
| 2221 | CompileFunction(&env, "function foo(){a=1;}", "foo"); |
| 2222 | debug_event_remove_break_point = SetBreakPoint(foo, 0); |
| 2223 | |
| 2224 | // Register the debug event listener pasing the function |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2225 | v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2226 | |
| 2227 | break_point_hit_count = 0; |
| 2228 | foo->Call(env->Global(), 0, NULL); |
| 2229 | CHECK_EQ(1, break_point_hit_count); |
| 2230 | |
| 2231 | break_point_hit_count = 0; |
| 2232 | foo->Call(env->Global(), 0, NULL); |
| 2233 | CHECK_EQ(0, break_point_hit_count); |
| 2234 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2235 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2236 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | |
| 2240 | // Test that the debugger statement causes a break. |
| 2241 | TEST(DebuggerStatement) { |
| 2242 | break_point_hit_count = 0; |
| 2243 | v8::HandleScope scope; |
| 2244 | DebugLocalContext env; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2245 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2246 | v8::Undefined()); |
| 2247 | v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run(); |
| 2248 | v8::Script::Compile(v8::String::New( |
| 2249 | "function foo(){debugger;debugger;}"))->Run(); |
| 2250 | v8::Local<v8::Function> foo = |
| 2251 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 2252 | v8::Local<v8::Function> bar = |
| 2253 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar"))); |
| 2254 | |
| 2255 | // Run function with debugger statement |
| 2256 | bar->Call(env->Global(), 0, NULL); |
| 2257 | CHECK_EQ(1, break_point_hit_count); |
| 2258 | |
| 2259 | // Run function with two debugger statement |
| 2260 | foo->Call(env->Global(), 0, NULL); |
| 2261 | CHECK_EQ(3, break_point_hit_count); |
| 2262 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2263 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2264 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2268 | // Test setting a breakpoint on the debugger statement. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2269 | TEST(DebuggerStatementBreakpoint) { |
| 2270 | break_point_hit_count = 0; |
| 2271 | v8::HandleScope scope; |
| 2272 | DebugLocalContext env; |
| 2273 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 2274 | v8::Undefined()); |
| 2275 | v8::Script::Compile(v8::String::New("function foo(){debugger;}"))->Run(); |
| 2276 | v8::Local<v8::Function> foo = |
| 2277 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 2278 | |
| 2279 | // The debugger statement triggers breakpint hit |
| 2280 | foo->Call(env->Global(), 0, NULL); |
| 2281 | CHECK_EQ(1, break_point_hit_count); |
| 2282 | |
| 2283 | int bp = SetBreakPoint(foo, 0); |
| 2284 | |
| 2285 | // Set breakpoint does not duplicate hits |
| 2286 | foo->Call(env->Global(), 0, NULL); |
| 2287 | CHECK_EQ(2, break_point_hit_count); |
| 2288 | |
| 2289 | ClearBreakPoint(bp); |
| 2290 | v8::Debug::SetDebugEventListener(NULL); |
| 2291 | CheckDebuggerUnloaded(); |
| 2292 | } |
| 2293 | |
| 2294 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2295 | // Thest that the evaluation of expressions when a break point is hit generates |
| 2296 | // the correct results. |
| 2297 | TEST(DebugEvaluate) { |
| 2298 | v8::HandleScope scope; |
| 2299 | DebugLocalContext env; |
| 2300 | env.ExposeDebug(); |
| 2301 | |
| 2302 | // Create a function for checking the evaluation when hitting a break point. |
| 2303 | evaluate_check_function = CompileFunction(&env, |
| 2304 | evaluate_check_source, |
| 2305 | "evaluate_check"); |
| 2306 | // Register the debug event listener |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2307 | v8::Debug::SetDebugEventListener(DebugEventEvaluate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2308 | |
| 2309 | // Different expected vaules of x and a when in a break point (u = undefined, |
| 2310 | // d = Hello, world!). |
| 2311 | struct EvaluateCheck checks_uu[] = { |
| 2312 | {"x", v8::Undefined()}, |
| 2313 | {"a", v8::Undefined()}, |
| 2314 | {NULL, v8::Handle<v8::Value>()} |
| 2315 | }; |
| 2316 | struct EvaluateCheck checks_hu[] = { |
| 2317 | {"x", v8::String::New("Hello, world!")}, |
| 2318 | {"a", v8::Undefined()}, |
| 2319 | {NULL, v8::Handle<v8::Value>()} |
| 2320 | }; |
| 2321 | struct EvaluateCheck checks_hh[] = { |
| 2322 | {"x", v8::String::New("Hello, world!")}, |
| 2323 | {"a", v8::String::New("Hello, world!")}, |
| 2324 | {NULL, v8::Handle<v8::Value>()} |
| 2325 | }; |
| 2326 | |
| 2327 | // Simple test function. The "y=0" is in the function foo to provide a break |
| 2328 | // location. For "y=0" the "y" is at position 15 in the barbar function |
| 2329 | // therefore setting breakpoint at position 15 will break at "y=0" and |
| 2330 | // setting it higher will break after. |
| 2331 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2332 | "function foo(x) {" |
| 2333 | " var a;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2334 | " y=0;" // To ensure break location 1. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2335 | " a=x;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2336 | " y=0;" // To ensure break location 2. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2337 | "}", |
| 2338 | "foo"); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2339 | const int foo_break_position_1 = 15; |
| 2340 | const int foo_break_position_2 = 29; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2341 | |
| 2342 | // Arguments with one parameter "Hello, world!" |
| 2343 | v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") }; |
| 2344 | |
| 2345 | // 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] | 2346 | int bp = SetBreakPoint(foo, foo_break_position_1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2347 | checks = checks_uu; |
| 2348 | foo->Call(env->Global(), 0, NULL); |
| 2349 | |
| 2350 | // Call foo with breakpoint set before a=x and parameter "Hello, world!". |
| 2351 | checks = checks_hu; |
| 2352 | foo->Call(env->Global(), 1, argv_foo); |
| 2353 | |
| 2354 | // Call foo with breakpoint set after a=x and parameter "Hello, world!". |
| 2355 | ClearBreakPoint(bp); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2356 | SetBreakPoint(foo, foo_break_position_2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2357 | checks = checks_hh; |
| 2358 | foo->Call(env->Global(), 1, argv_foo); |
| 2359 | |
| 2360 | // Test function with an inner function. The "y=0" is in function barbar |
| 2361 | // to provide a break location. For "y=0" the "y" is at position 8 in the |
| 2362 | // barbar function therefore setting breakpoint at position 8 will break at |
| 2363 | // "y=0" and setting it higher will break after. |
| 2364 | v8::Local<v8::Function> bar = CompileFunction(&env, |
| 2365 | "y = 0;" |
| 2366 | "x = 'Goodbye, world!';" |
| 2367 | "function bar(x, b) {" |
| 2368 | " var a;" |
| 2369 | " function barbar() {" |
| 2370 | " y=0; /* To ensure break location.*/" |
| 2371 | " a=x;" |
| 2372 | " };" |
| 2373 | " debug.Debug.clearAllBreakPoints();" |
| 2374 | " barbar();" |
| 2375 | " y=0;a=x;" |
| 2376 | "}", |
| 2377 | "bar"); |
| 2378 | const int barbar_break_position = 8; |
| 2379 | |
| 2380 | // Call bar setting breakpoint before a=x in barbar and undefined as |
| 2381 | // parameter. |
| 2382 | checks = checks_uu; |
| 2383 | v8::Handle<v8::Value> argv_bar_1[2] = { |
| 2384 | v8::Undefined(), |
| 2385 | v8::Number::New(barbar_break_position) |
| 2386 | }; |
| 2387 | bar->Call(env->Global(), 2, argv_bar_1); |
| 2388 | |
| 2389 | // Call bar setting breakpoint before a=x in barbar and parameter |
| 2390 | // "Hello, world!". |
| 2391 | checks = checks_hu; |
| 2392 | v8::Handle<v8::Value> argv_bar_2[2] = { |
| 2393 | v8::String::New("Hello, world!"), |
| 2394 | v8::Number::New(barbar_break_position) |
| 2395 | }; |
| 2396 | bar->Call(env->Global(), 2, argv_bar_2); |
| 2397 | |
| 2398 | // Call bar setting breakpoint after a=x in barbar and parameter |
| 2399 | // "Hello, world!". |
| 2400 | checks = checks_hh; |
| 2401 | v8::Handle<v8::Value> argv_bar_3[2] = { |
| 2402 | v8::String::New("Hello, world!"), |
| 2403 | v8::Number::New(barbar_break_position + 1) |
| 2404 | }; |
| 2405 | bar->Call(env->Global(), 2, argv_bar_3); |
| 2406 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2407 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2408 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2411 | // Copies a C string to a 16-bit string. Does not check for buffer overflow. |
| 2412 | // Does not use the V8 engine to convert strings, so it can be used |
| 2413 | // in any thread. Returns the length of the string. |
| 2414 | int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) { |
| 2415 | int i; |
| 2416 | for (i = 0; input_buffer[i] != '\0'; ++i) { |
| 2417 | // ASCII does not use chars > 127, but be careful anyway. |
| 2418 | output_buffer[i] = static_cast<unsigned char>(input_buffer[i]); |
| 2419 | } |
| 2420 | output_buffer[i] = 0; |
| 2421 | return i; |
| 2422 | } |
| 2423 | |
| 2424 | // Copies a 16-bit string to a C string by dropping the high byte of |
| 2425 | // each character. Does not check for buffer overflow. |
| 2426 | // Can be used in any thread. Requires string length as an input. |
| 2427 | int Utf16ToAscii(const uint16_t* input_buffer, int length, |
| 2428 | char* output_buffer, int output_len = -1) { |
| 2429 | if (output_len >= 0) { |
| 2430 | if (length > output_len - 1) { |
| 2431 | length = output_len - 1; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | for (int i = 0; i < length; ++i) { |
| 2436 | output_buffer[i] = static_cast<char>(input_buffer[i]); |
| 2437 | } |
| 2438 | output_buffer[length] = '\0'; |
| 2439 | return length; |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | // We match parts of the message to get evaluate result int value. |
| 2444 | bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) { |
| 2445 | if (strstr(message, "\"command\":\"evaluate\"") == NULL) { |
| 2446 | return false; |
| 2447 | } |
| 2448 | const char* prefix = "\"text\":\""; |
| 2449 | char* pos1 = strstr(message, prefix); |
| 2450 | if (pos1 == NULL) { |
| 2451 | return false; |
| 2452 | } |
| 2453 | pos1 += strlen(prefix); |
| 2454 | char* pos2 = strchr(pos1, '"'); |
| 2455 | if (pos2 == NULL) { |
| 2456 | return false; |
| 2457 | } |
| 2458 | Vector<char> buf(buffer, buffer_size); |
| 2459 | int len = static_cast<int>(pos2 - pos1); |
| 2460 | if (len > buffer_size - 1) { |
| 2461 | len = buffer_size - 1; |
| 2462 | } |
| 2463 | OS::StrNCpy(buf, pos1, len); |
| 2464 | buffer[buffer_size - 1] = '\0'; |
| 2465 | return true; |
| 2466 | } |
| 2467 | |
| 2468 | |
| 2469 | struct EvaluateResult { |
| 2470 | static const int kBufferSize = 20; |
| 2471 | char buffer[kBufferSize]; |
| 2472 | }; |
| 2473 | |
| 2474 | struct DebugProcessDebugMessagesData { |
| 2475 | static const int kArraySize = 5; |
| 2476 | int counter; |
| 2477 | EvaluateResult results[kArraySize]; |
| 2478 | |
| 2479 | void reset() { |
| 2480 | counter = 0; |
| 2481 | } |
| 2482 | EvaluateResult* current() { |
| 2483 | return &results[counter % kArraySize]; |
| 2484 | } |
| 2485 | void next() { |
| 2486 | counter++; |
| 2487 | } |
| 2488 | }; |
| 2489 | |
| 2490 | DebugProcessDebugMessagesData process_debug_messages_data; |
| 2491 | |
| 2492 | static void DebugProcessDebugMessagesHandler( |
| 2493 | const uint16_t* message, |
| 2494 | int length, |
| 2495 | v8::Debug::ClientData* client_data) { |
| 2496 | |
| 2497 | const int kBufferSize = 100000; |
| 2498 | char print_buffer[kBufferSize]; |
| 2499 | Utf16ToAscii(message, length, print_buffer, kBufferSize); |
| 2500 | |
| 2501 | EvaluateResult* array_item = process_debug_messages_data.current(); |
| 2502 | |
| 2503 | bool res = GetEvaluateStringResult(print_buffer, |
| 2504 | array_item->buffer, |
| 2505 | EvaluateResult::kBufferSize); |
| 2506 | if (res) { |
| 2507 | process_debug_messages_data.next(); |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | // Test that the evaluation of expressions works even from ProcessDebugMessages |
| 2512 | // i.e. with empty stack. |
| 2513 | TEST(DebugEvaluateWithoutStack) { |
| 2514 | v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler); |
| 2515 | |
| 2516 | v8::HandleScope scope; |
| 2517 | DebugLocalContext env; |
| 2518 | |
| 2519 | const char* source = |
| 2520 | "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }"; |
| 2521 | |
| 2522 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 2523 | |
| 2524 | v8::Debug::ProcessDebugMessages(); |
| 2525 | |
| 2526 | const int kBufferSize = 1000; |
| 2527 | uint16_t buffer[kBufferSize]; |
| 2528 | |
| 2529 | const char* command_111 = "{\"seq\":111," |
| 2530 | "\"type\":\"request\"," |
| 2531 | "\"command\":\"evaluate\"," |
| 2532 | "\"arguments\":{" |
| 2533 | " \"global\":true," |
| 2534 | " \"expression\":\"v1\",\"disable_break\":true" |
| 2535 | "}}"; |
| 2536 | |
| 2537 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_111, buffer)); |
| 2538 | |
| 2539 | const char* command_112 = "{\"seq\":112," |
| 2540 | "\"type\":\"request\"," |
| 2541 | "\"command\":\"evaluate\"," |
| 2542 | "\"arguments\":{" |
| 2543 | " \"global\":true," |
| 2544 | " \"expression\":\"getAnimal()\",\"disable_break\":true" |
| 2545 | "}}"; |
| 2546 | |
| 2547 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_112, buffer)); |
| 2548 | |
| 2549 | const char* command_113 = "{\"seq\":113," |
| 2550 | "\"type\":\"request\"," |
| 2551 | "\"command\":\"evaluate\"," |
| 2552 | "\"arguments\":{" |
| 2553 | " \"global\":true," |
| 2554 | " \"expression\":\"239 + 566\",\"disable_break\":true" |
| 2555 | "}}"; |
| 2556 | |
| 2557 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_113, buffer)); |
| 2558 | |
| 2559 | v8::Debug::ProcessDebugMessages(); |
| 2560 | |
| 2561 | CHECK_EQ(3, process_debug_messages_data.counter); |
| 2562 | |
| 2563 | CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0); |
| 2564 | CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer), |
| 2565 | 0); |
| 2566 | CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0); |
| 2567 | |
| 2568 | v8::Debug::SetMessageHandler(NULL); |
| 2569 | v8::Debug::SetDebugEventListener(NULL); |
| 2570 | CheckDebuggerUnloaded(); |
| 2571 | } |
| 2572 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2573 | |
| 2574 | // Simple test of the stepping mechanism using only store ICs. |
| 2575 | TEST(DebugStepLinear) { |
| 2576 | v8::HandleScope scope; |
| 2577 | DebugLocalContext env; |
| 2578 | |
| 2579 | // Create a function for testing stepping. |
| 2580 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2581 | "function foo(){a=1;b=1;c=1;}", |
| 2582 | "foo"); |
| 2583 | SetBreakPoint(foo, 3); |
| 2584 | |
| 2585 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2586 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2587 | |
| 2588 | step_action = StepIn; |
| 2589 | break_point_hit_count = 0; |
| 2590 | foo->Call(env->Global(), 0, NULL); |
| 2591 | |
| 2592 | // With stepping all break locations are hit. |
| 2593 | CHECK_EQ(4, break_point_hit_count); |
| 2594 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2595 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2596 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2597 | |
| 2598 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2599 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2600 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2601 | SetBreakPoint(foo, 3); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2602 | break_point_hit_count = 0; |
| 2603 | foo->Call(env->Global(), 0, NULL); |
| 2604 | |
| 2605 | // Without stepping only active break points are hit. |
| 2606 | CHECK_EQ(1, break_point_hit_count); |
| 2607 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2608 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2609 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2613 | // Test of the stepping mechanism for keyed load in a loop. |
| 2614 | TEST(DebugStepKeyedLoadLoop) { |
| 2615 | v8::HandleScope scope; |
| 2616 | DebugLocalContext env; |
| 2617 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2618 | // Register a debug event listener which steps and counts. |
| 2619 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2620 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2621 | // Create a function for testing stepping of keyed load. The statement 'y=1' |
| 2622 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2623 | v8::Local<v8::Function> foo = CompileFunction( |
| 2624 | &env, |
| 2625 | "function foo(a) {\n" |
| 2626 | " var x;\n" |
| 2627 | " var len = a.length;\n" |
| 2628 | " for (var i = 0; i < len; i++) {\n" |
| 2629 | " y = 1;\n" |
| 2630 | " x = a[i];\n" |
| 2631 | " }\n" |
| 2632 | "}\n", |
| 2633 | "foo"); |
| 2634 | |
| 2635 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2636 | v8::Local<v8::Array> a = v8::Array::New(10); |
| 2637 | for (int i = 0; i < 10; i++) { |
| 2638 | a->Set(v8::Number::New(i), v8::Number::New(i)); |
| 2639 | } |
| 2640 | |
| 2641 | // Call function without any break points to ensure inlining is in place. |
| 2642 | const int kArgc = 1; |
| 2643 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2644 | foo->Call(env->Global(), kArgc, args); |
| 2645 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2646 | // Setup break point and step through the function. |
| 2647 | SetBreakPoint(foo, 3); |
| 2648 | step_action = StepNext; |
| 2649 | break_point_hit_count = 0; |
| 2650 | foo->Call(env->Global(), kArgc, args); |
| 2651 | |
| 2652 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2653 | CHECK_EQ(33, break_point_hit_count); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2654 | |
| 2655 | v8::Debug::SetDebugEventListener(NULL); |
| 2656 | CheckDebuggerUnloaded(); |
| 2657 | } |
| 2658 | |
| 2659 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2660 | // Test of the stepping mechanism for keyed store in a loop. |
| 2661 | TEST(DebugStepKeyedStoreLoop) { |
| 2662 | v8::HandleScope scope; |
| 2663 | DebugLocalContext env; |
| 2664 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2665 | // Register a debug event listener which steps and counts. |
| 2666 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2667 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2668 | // Create a function for testing stepping of keyed store. The statement 'y=1' |
| 2669 | // is there to have more than one breakable statement in the loop, TODO(315). |
| 2670 | v8::Local<v8::Function> foo = CompileFunction( |
| 2671 | &env, |
| 2672 | "function foo(a) {\n" |
| 2673 | " var len = a.length;\n" |
| 2674 | " for (var i = 0; i < len; i++) {\n" |
| 2675 | " y = 1;\n" |
| 2676 | " a[i] = 42;\n" |
| 2677 | " }\n" |
| 2678 | "}\n", |
| 2679 | "foo"); |
| 2680 | |
| 2681 | // Create array [0,1,2,3,4,5,6,7,8,9] |
| 2682 | v8::Local<v8::Array> a = v8::Array::New(10); |
| 2683 | for (int i = 0; i < 10; i++) { |
| 2684 | a->Set(v8::Number::New(i), v8::Number::New(i)); |
| 2685 | } |
| 2686 | |
| 2687 | // Call function without any break points to ensure inlining is in place. |
| 2688 | const int kArgc = 1; |
| 2689 | v8::Handle<v8::Value> args[kArgc] = { a }; |
| 2690 | foo->Call(env->Global(), kArgc, args); |
| 2691 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2692 | // Setup break point and step through the function. |
| 2693 | SetBreakPoint(foo, 3); |
| 2694 | step_action = StepNext; |
| 2695 | break_point_hit_count = 0; |
| 2696 | foo->Call(env->Global(), kArgc, args); |
| 2697 | |
| 2698 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2699 | CHECK_EQ(32, break_point_hit_count); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2700 | |
| 2701 | v8::Debug::SetDebugEventListener(NULL); |
| 2702 | CheckDebuggerUnloaded(); |
| 2703 | } |
| 2704 | |
| 2705 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2706 | // Test of the stepping mechanism for named load in a loop. |
| 2707 | TEST(DebugStepNamedLoadLoop) { |
| 2708 | v8::HandleScope scope; |
| 2709 | DebugLocalContext env; |
| 2710 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2711 | // Register a debug event listener which steps and counts. |
| 2712 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2713 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2714 | // Create a function for testing stepping of named load. |
| 2715 | v8::Local<v8::Function> foo = CompileFunction( |
| 2716 | &env, |
| 2717 | "function foo() {\n" |
| 2718 | " var a = [];\n" |
| 2719 | " var s = \"\";\n" |
| 2720 | " for (var i = 0; i < 10; i++) {\n" |
| 2721 | " var v = new V(i, i + 1);\n" |
| 2722 | " v.y;\n" |
| 2723 | " a.length;\n" // Special case: array length. |
| 2724 | " s.length;\n" // Special case: string length. |
| 2725 | " }\n" |
| 2726 | "}\n" |
| 2727 | "function V(x, y) {\n" |
| 2728 | " this.x = x;\n" |
| 2729 | " this.y = y;\n" |
| 2730 | "}\n", |
| 2731 | "foo"); |
| 2732 | |
| 2733 | // Call function without any break points to ensure inlining is in place. |
| 2734 | foo->Call(env->Global(), 0, NULL); |
| 2735 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2736 | // Setup break point and step through the function. |
| 2737 | SetBreakPoint(foo, 4); |
| 2738 | step_action = StepNext; |
| 2739 | break_point_hit_count = 0; |
| 2740 | foo->Call(env->Global(), 0, NULL); |
| 2741 | |
| 2742 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2743 | CHECK_EQ(53, break_point_hit_count); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2744 | |
| 2745 | v8::Debug::SetDebugEventListener(NULL); |
| 2746 | CheckDebuggerUnloaded(); |
| 2747 | } |
| 2748 | |
| 2749 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 2750 | static void DoDebugStepNamedStoreLoop(int expected, bool full_compiler = true) { |
| 2751 | v8::HandleScope scope; |
| 2752 | DebugLocalContext env; |
| 2753 | |
| 2754 | // Register a debug event listener which steps and counts before compiling the |
| 2755 | // function to ensure the full compiler is used. |
| 2756 | if (full_compiler) { |
| 2757 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2758 | } |
| 2759 | |
| 2760 | // Create a function for testing stepping of named store. |
| 2761 | v8::Local<v8::Function> foo = CompileFunction( |
| 2762 | &env, |
| 2763 | "function foo() {\n" |
| 2764 | " var a = {a:1};\n" |
| 2765 | " for (var i = 0; i < 10; i++) {\n" |
| 2766 | " a.a = 2\n" |
| 2767 | " }\n" |
| 2768 | "}\n", |
| 2769 | "foo"); |
| 2770 | |
| 2771 | // Call function without any break points to ensure inlining is in place. |
| 2772 | foo->Call(env->Global(), 0, NULL); |
| 2773 | |
| 2774 | // Register a debug event listener which steps and counts after compiling the |
| 2775 | // function to ensure the optimizing compiler is used. |
| 2776 | if (!full_compiler) { |
| 2777 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2778 | } |
| 2779 | |
| 2780 | // Setup break point and step through the function. |
| 2781 | SetBreakPoint(foo, 3); |
| 2782 | step_action = StepNext; |
| 2783 | break_point_hit_count = 0; |
| 2784 | foo->Call(env->Global(), 0, NULL); |
| 2785 | |
| 2786 | // With stepping all expected break locations are hit. |
| 2787 | CHECK_EQ(expected, break_point_hit_count); |
| 2788 | |
| 2789 | v8::Debug::SetDebugEventListener(NULL); |
| 2790 | CheckDebuggerUnloaded(); |
| 2791 | } |
| 2792 | |
| 2793 | |
| 2794 | // Test of the stepping mechanism for named load in a loop. |
| 2795 | TEST(DebugStepNamedStoreLoopFull) { |
| 2796 | // With the full compiler it is possible to break on the for statement. |
| 2797 | DoDebugStepNamedStoreLoop(22); |
| 2798 | } |
| 2799 | |
| 2800 | |
| 2801 | // Test of the stepping mechanism for named load in a loop. |
| 2802 | TEST(DebugStepNamedStoreLoopOptimizing) { |
| 2803 | // With the optimizing compiler it is not possible to break on the for |
| 2804 | // statement as it uses a local variable thus no IC's. |
| 2805 | DoDebugStepNamedStoreLoop(11, false); |
| 2806 | } |
| 2807 | |
| 2808 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2809 | // Test the stepping mechanism with different ICs. |
| 2810 | TEST(DebugStepLinearMixedICs) { |
| 2811 | v8::HandleScope scope; |
| 2812 | DebugLocalContext env; |
| 2813 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2814 | // Register a debug event listener which steps and counts. |
| 2815 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2816 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2817 | // Create a function for testing stepping. |
| 2818 | v8::Local<v8::Function> foo = CompileFunction(&env, |
| 2819 | "function bar() {};" |
| 2820 | "function foo() {" |
| 2821 | " var x;" |
| 2822 | " var index='name';" |
| 2823 | " var y = {};" |
| 2824 | " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); |
| 2825 | SetBreakPoint(foo, 0); |
| 2826 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2827 | step_action = StepIn; |
| 2828 | break_point_hit_count = 0; |
| 2829 | foo->Call(env->Global(), 0, NULL); |
| 2830 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2831 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2832 | CHECK_EQ(11, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2833 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2834 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2835 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2836 | |
| 2837 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2838 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2839 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2840 | SetBreakPoint(foo, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2841 | break_point_hit_count = 0; |
| 2842 | foo->Call(env->Global(), 0, NULL); |
| 2843 | |
| 2844 | // Without stepping only active break points are hit. |
| 2845 | CHECK_EQ(1, break_point_hit_count); |
| 2846 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2847 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2848 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2852 | TEST(DebugStepDeclarations) { |
| 2853 | v8::HandleScope scope; |
| 2854 | DebugLocalContext env; |
| 2855 | |
| 2856 | // Register a debug event listener which steps and counts. |
| 2857 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2858 | |
| 2859 | // Create a function for testing stepping. |
| 2860 | const char* src = "function foo() { " |
| 2861 | " var a;" |
| 2862 | " var b = 1;" |
| 2863 | " var c = foo;" |
| 2864 | " var d = Math.floor;" |
| 2865 | " var e = b + d(1.2);" |
| 2866 | "}"; |
| 2867 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 2868 | SetBreakPoint(foo, 0); |
| 2869 | |
| 2870 | // Stepping through the declarations. |
| 2871 | step_action = StepIn; |
| 2872 | break_point_hit_count = 0; |
| 2873 | foo->Call(env->Global(), 0, NULL); |
| 2874 | CHECK_EQ(6, break_point_hit_count); |
| 2875 | |
| 2876 | // Get rid of the debug event listener. |
| 2877 | v8::Debug::SetDebugEventListener(NULL); |
| 2878 | CheckDebuggerUnloaded(); |
| 2879 | } |
| 2880 | |
| 2881 | |
| 2882 | TEST(DebugStepLocals) { |
| 2883 | v8::HandleScope scope; |
| 2884 | DebugLocalContext env; |
| 2885 | |
| 2886 | // Register a debug event listener which steps and counts. |
| 2887 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 2888 | |
| 2889 | // Create a function for testing stepping. |
| 2890 | const char* src = "function foo() { " |
| 2891 | " var a,b;" |
| 2892 | " a = 1;" |
| 2893 | " b = a + 2;" |
| 2894 | " b = 1 + 2 + 3;" |
| 2895 | " a = Math.floor(b);" |
| 2896 | "}"; |
| 2897 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 2898 | SetBreakPoint(foo, 0); |
| 2899 | |
| 2900 | // Stepping through the declarations. |
| 2901 | step_action = StepIn; |
| 2902 | break_point_hit_count = 0; |
| 2903 | foo->Call(env->Global(), 0, NULL); |
| 2904 | CHECK_EQ(6, break_point_hit_count); |
| 2905 | |
| 2906 | // Get rid of the debug event listener. |
| 2907 | v8::Debug::SetDebugEventListener(NULL); |
| 2908 | CheckDebuggerUnloaded(); |
| 2909 | } |
| 2910 | |
| 2911 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2912 | TEST(DebugStepIf) { |
| 2913 | v8::HandleScope scope; |
| 2914 | DebugLocalContext env; |
| 2915 | |
| 2916 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2917 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2918 | |
| 2919 | // Create a function for testing stepping. |
| 2920 | const int argc = 1; |
| 2921 | const char* src = "function foo(x) { " |
| 2922 | " a = 1;" |
| 2923 | " if (x) {" |
| 2924 | " b = 1;" |
| 2925 | " } else {" |
| 2926 | " c = 1;" |
| 2927 | " d = 1;" |
| 2928 | " }" |
| 2929 | "}"; |
| 2930 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 2931 | SetBreakPoint(foo, 0); |
| 2932 | |
| 2933 | // Stepping through the true part. |
| 2934 | step_action = StepIn; |
| 2935 | break_point_hit_count = 0; |
| 2936 | v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; |
| 2937 | foo->Call(env->Global(), argc, argv_true); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2938 | CHECK_EQ(4, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2939 | |
| 2940 | // Stepping through the false part. |
| 2941 | step_action = StepIn; |
| 2942 | break_point_hit_count = 0; |
| 2943 | v8::Handle<v8::Value> argv_false[argc] = { v8::False() }; |
| 2944 | foo->Call(env->Global(), argc, argv_false); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2945 | CHECK_EQ(5, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2946 | |
| 2947 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2948 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2949 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | |
| 2953 | TEST(DebugStepSwitch) { |
| 2954 | v8::HandleScope scope; |
| 2955 | DebugLocalContext env; |
| 2956 | |
| 2957 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2958 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2959 | |
| 2960 | // Create a function for testing stepping. |
| 2961 | const int argc = 1; |
| 2962 | const char* src = "function foo(x) { " |
| 2963 | " a = 1;" |
| 2964 | " switch (x) {" |
| 2965 | " case 1:" |
| 2966 | " b = 1;" |
| 2967 | " case 2:" |
| 2968 | " c = 1;" |
| 2969 | " break;" |
| 2970 | " case 3:" |
| 2971 | " d = 1;" |
| 2972 | " e = 1;" |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2973 | " f = 1;" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2974 | " break;" |
| 2975 | " }" |
| 2976 | "}"; |
| 2977 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 2978 | SetBreakPoint(foo, 0); |
| 2979 | |
| 2980 | // One case with fall-through. |
| 2981 | step_action = StepIn; |
| 2982 | break_point_hit_count = 0; |
| 2983 | v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) }; |
| 2984 | foo->Call(env->Global(), argc, argv_1); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2985 | CHECK_EQ(6, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2986 | |
| 2987 | // Another case. |
| 2988 | step_action = StepIn; |
| 2989 | break_point_hit_count = 0; |
| 2990 | v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) }; |
| 2991 | foo->Call(env->Global(), argc, argv_2); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2992 | CHECK_EQ(5, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2993 | |
| 2994 | // Last case. |
| 2995 | step_action = StepIn; |
| 2996 | break_point_hit_count = 0; |
| 2997 | v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) }; |
| 2998 | foo->Call(env->Global(), argc, argv_3); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2999 | CHECK_EQ(7, break_point_hit_count); |
| 3000 | |
| 3001 | // Get rid of the debug event listener. |
| 3002 | v8::Debug::SetDebugEventListener(NULL); |
| 3003 | CheckDebuggerUnloaded(); |
| 3004 | } |
| 3005 | |
| 3006 | |
| 3007 | TEST(DebugStepWhile) { |
| 3008 | v8::HandleScope scope; |
| 3009 | DebugLocalContext env; |
| 3010 | |
| 3011 | // Register a debug event listener which steps and counts. |
| 3012 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3013 | |
| 3014 | // Create a function for testing stepping. |
| 3015 | const int argc = 1; |
| 3016 | const char* src = "function foo(x) { " |
| 3017 | " var a = 0;" |
| 3018 | " while (a < x) {" |
| 3019 | " a++;" |
| 3020 | " }" |
| 3021 | "}"; |
| 3022 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3023 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3024 | |
| 3025 | // Looping 10 times. |
| 3026 | step_action = StepIn; |
| 3027 | break_point_hit_count = 0; |
| 3028 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3029 | foo->Call(env->Global(), argc, argv_10); |
| 3030 | CHECK_EQ(23, break_point_hit_count); |
| 3031 | |
| 3032 | // Looping 100 times. |
| 3033 | step_action = StepIn; |
| 3034 | break_point_hit_count = 0; |
| 3035 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3036 | foo->Call(env->Global(), argc, argv_100); |
| 3037 | CHECK_EQ(203, break_point_hit_count); |
| 3038 | |
| 3039 | // Get rid of the debug event listener. |
| 3040 | v8::Debug::SetDebugEventListener(NULL); |
| 3041 | CheckDebuggerUnloaded(); |
| 3042 | } |
| 3043 | |
| 3044 | |
| 3045 | TEST(DebugStepDoWhile) { |
| 3046 | v8::HandleScope scope; |
| 3047 | DebugLocalContext env; |
| 3048 | |
| 3049 | // Register a debug event listener which steps and counts. |
| 3050 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3051 | |
| 3052 | // Create a function for testing stepping. |
| 3053 | const int argc = 1; |
| 3054 | const char* src = "function foo(x) { " |
| 3055 | " var a = 0;" |
| 3056 | " do {" |
| 3057 | " a++;" |
| 3058 | " } while (a < x)" |
| 3059 | "}"; |
| 3060 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3061 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3062 | |
| 3063 | // Looping 10 times. |
| 3064 | step_action = StepIn; |
| 3065 | break_point_hit_count = 0; |
| 3066 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3067 | foo->Call(env->Global(), argc, argv_10); |
| 3068 | CHECK_EQ(22, break_point_hit_count); |
| 3069 | |
| 3070 | // Looping 100 times. |
| 3071 | step_action = StepIn; |
| 3072 | break_point_hit_count = 0; |
| 3073 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3074 | foo->Call(env->Global(), argc, argv_100); |
| 3075 | CHECK_EQ(202, break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3076 | |
| 3077 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3078 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3079 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | |
| 3083 | TEST(DebugStepFor) { |
| 3084 | v8::HandleScope scope; |
| 3085 | DebugLocalContext env; |
| 3086 | |
| 3087 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3088 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3089 | |
| 3090 | // Create a function for testing stepping. |
| 3091 | const int argc = 1; |
| 3092 | const char* src = "function foo(x) { " |
| 3093 | " a = 1;" |
| 3094 | " for (i = 0; i < x; i++) {" |
| 3095 | " b = 1;" |
| 3096 | " }" |
| 3097 | "}"; |
| 3098 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3099 | SetBreakPoint(foo, 8); // "a = 1;" |
| 3100 | |
| 3101 | // Looping 10 times. |
| 3102 | step_action = StepIn; |
| 3103 | break_point_hit_count = 0; |
| 3104 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3105 | foo->Call(env->Global(), argc, argv_10); |
| 3106 | CHECK_EQ(23, break_point_hit_count); |
| 3107 | |
| 3108 | // Looping 100 times. |
| 3109 | step_action = StepIn; |
| 3110 | break_point_hit_count = 0; |
| 3111 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3112 | foo->Call(env->Global(), argc, argv_100); |
| 3113 | CHECK_EQ(203, break_point_hit_count); |
| 3114 | |
| 3115 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3116 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3117 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3121 | TEST(DebugStepForContinue) { |
| 3122 | v8::HandleScope scope; |
| 3123 | DebugLocalContext env; |
| 3124 | |
| 3125 | // Register a debug event listener which steps and counts. |
| 3126 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3127 | |
| 3128 | // Create a function for testing stepping. |
| 3129 | const int argc = 1; |
| 3130 | const char* src = "function foo(x) { " |
| 3131 | " var a = 0;" |
| 3132 | " var b = 0;" |
| 3133 | " var c = 0;" |
| 3134 | " for (var i = 0; i < x; i++) {" |
| 3135 | " a++;" |
| 3136 | " if (a % 2 == 0) continue;" |
| 3137 | " b++;" |
| 3138 | " c++;" |
| 3139 | " }" |
| 3140 | " return b;" |
| 3141 | "}"; |
| 3142 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3143 | v8::Handle<v8::Value> result; |
| 3144 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3145 | |
| 3146 | // Each loop generates 4 or 5 steps depending on whether a is equal. |
| 3147 | |
| 3148 | // Looping 10 times. |
| 3149 | step_action = StepIn; |
| 3150 | break_point_hit_count = 0; |
| 3151 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3152 | result = foo->Call(env->Global(), argc, argv_10); |
| 3153 | CHECK_EQ(5, result->Int32Value()); |
| 3154 | CHECK_EQ(50, break_point_hit_count); |
| 3155 | |
| 3156 | // Looping 100 times. |
| 3157 | step_action = StepIn; |
| 3158 | break_point_hit_count = 0; |
| 3159 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3160 | result = foo->Call(env->Global(), argc, argv_100); |
| 3161 | CHECK_EQ(50, result->Int32Value()); |
| 3162 | CHECK_EQ(455, break_point_hit_count); |
| 3163 | |
| 3164 | // Get rid of the debug event listener. |
| 3165 | v8::Debug::SetDebugEventListener(NULL); |
| 3166 | CheckDebuggerUnloaded(); |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | TEST(DebugStepForBreak) { |
| 3171 | v8::HandleScope scope; |
| 3172 | DebugLocalContext env; |
| 3173 | |
| 3174 | // Register a debug event listener which steps and counts. |
| 3175 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3176 | |
| 3177 | // Create a function for testing stepping. |
| 3178 | const int argc = 1; |
| 3179 | const char* src = "function foo(x) { " |
| 3180 | " var a = 0;" |
| 3181 | " var b = 0;" |
| 3182 | " var c = 0;" |
| 3183 | " for (var i = 0; i < 1000; i++) {" |
| 3184 | " a++;" |
| 3185 | " if (a == x) break;" |
| 3186 | " b++;" |
| 3187 | " c++;" |
| 3188 | " }" |
| 3189 | " return b;" |
| 3190 | "}"; |
| 3191 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3192 | v8::Handle<v8::Value> result; |
| 3193 | SetBreakPoint(foo, 8); // "var a = 0;" |
| 3194 | |
| 3195 | // Each loop generates 5 steps except for the last (when break is executed) |
| 3196 | // which only generates 4. |
| 3197 | |
| 3198 | // Looping 10 times. |
| 3199 | step_action = StepIn; |
| 3200 | break_point_hit_count = 0; |
| 3201 | v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; |
| 3202 | result = foo->Call(env->Global(), argc, argv_10); |
| 3203 | CHECK_EQ(9, result->Int32Value()); |
| 3204 | CHECK_EQ(53, break_point_hit_count); |
| 3205 | |
| 3206 | // Looping 100 times. |
| 3207 | step_action = StepIn; |
| 3208 | break_point_hit_count = 0; |
| 3209 | v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; |
| 3210 | result = foo->Call(env->Global(), argc, argv_100); |
| 3211 | CHECK_EQ(99, result->Int32Value()); |
| 3212 | CHECK_EQ(503, break_point_hit_count); |
| 3213 | |
| 3214 | // Get rid of the debug event listener. |
| 3215 | v8::Debug::SetDebugEventListener(NULL); |
| 3216 | CheckDebuggerUnloaded(); |
| 3217 | } |
| 3218 | |
| 3219 | |
| 3220 | TEST(DebugStepForIn) { |
| 3221 | v8::HandleScope scope; |
| 3222 | DebugLocalContext env; |
| 3223 | |
| 3224 | // Register a debug event listener which steps and counts. |
| 3225 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3226 | |
| 3227 | v8::Local<v8::Function> foo; |
| 3228 | const char* src_1 = "function foo() { " |
| 3229 | " var a = [1, 2];" |
| 3230 | " for (x in a) {" |
| 3231 | " b = 0;" |
| 3232 | " }" |
| 3233 | "}"; |
| 3234 | foo = CompileFunction(&env, src_1, "foo"); |
| 3235 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3236 | |
| 3237 | step_action = StepIn; |
| 3238 | break_point_hit_count = 0; |
| 3239 | foo->Call(env->Global(), 0, NULL); |
| 3240 | CHECK_EQ(6, break_point_hit_count); |
| 3241 | |
| 3242 | const char* src_2 = "function foo() { " |
| 3243 | " var a = {a:[1, 2, 3]};" |
| 3244 | " for (x in a.a) {" |
| 3245 | " b = 0;" |
| 3246 | " }" |
| 3247 | "}"; |
| 3248 | foo = CompileFunction(&env, src_2, "foo"); |
| 3249 | SetBreakPoint(foo, 0); // "var a = ..." |
| 3250 | |
| 3251 | step_action = StepIn; |
| 3252 | break_point_hit_count = 0; |
| 3253 | foo->Call(env->Global(), 0, NULL); |
| 3254 | CHECK_EQ(8, break_point_hit_count); |
| 3255 | |
| 3256 | // Get rid of the debug event listener. |
| 3257 | v8::Debug::SetDebugEventListener(NULL); |
| 3258 | CheckDebuggerUnloaded(); |
| 3259 | } |
| 3260 | |
| 3261 | |
| 3262 | TEST(DebugStepWith) { |
| 3263 | v8::HandleScope scope; |
| 3264 | DebugLocalContext env; |
| 3265 | |
| 3266 | // Register a debug event listener which steps and counts. |
| 3267 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3268 | |
| 3269 | // Create a function for testing stepping. |
| 3270 | const char* src = "function foo(x) { " |
| 3271 | " var a = {};" |
| 3272 | " with (a) {}" |
| 3273 | " with (b) {}" |
| 3274 | "}"; |
| 3275 | env->Global()->Set(v8::String::New("b"), v8::Object::New()); |
| 3276 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3277 | v8::Handle<v8::Value> result; |
| 3278 | SetBreakPoint(foo, 8); // "var a = {};" |
| 3279 | |
| 3280 | step_action = StepIn; |
| 3281 | break_point_hit_count = 0; |
| 3282 | foo->Call(env->Global(), 0, NULL); |
| 3283 | CHECK_EQ(4, break_point_hit_count); |
| 3284 | |
| 3285 | // Get rid of the debug event listener. |
| 3286 | v8::Debug::SetDebugEventListener(NULL); |
| 3287 | CheckDebuggerUnloaded(); |
| 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | TEST(DebugConditional) { |
| 3292 | v8::HandleScope scope; |
| 3293 | DebugLocalContext env; |
| 3294 | |
| 3295 | // Register a debug event listener which steps and counts. |
| 3296 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3297 | |
| 3298 | // Create a function for testing stepping. |
| 3299 | const char* src = "function foo(x) { " |
| 3300 | " var a;" |
| 3301 | " a = x ? 1 : 2;" |
| 3302 | " return a;" |
| 3303 | "}"; |
| 3304 | v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); |
| 3305 | SetBreakPoint(foo, 0); // "var a;" |
| 3306 | |
| 3307 | step_action = StepIn; |
| 3308 | break_point_hit_count = 0; |
| 3309 | foo->Call(env->Global(), 0, NULL); |
| 3310 | CHECK_EQ(5, break_point_hit_count); |
| 3311 | |
| 3312 | step_action = StepIn; |
| 3313 | break_point_hit_count = 0; |
| 3314 | const int argc = 1; |
| 3315 | v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; |
| 3316 | foo->Call(env->Global(), argc, argv_true); |
| 3317 | CHECK_EQ(5, break_point_hit_count); |
| 3318 | |
| 3319 | // Get rid of the debug event listener. |
| 3320 | v8::Debug::SetDebugEventListener(NULL); |
| 3321 | CheckDebuggerUnloaded(); |
| 3322 | } |
| 3323 | |
| 3324 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3325 | TEST(StepInOutSimple) { |
| 3326 | v8::HandleScope scope; |
| 3327 | DebugLocalContext env; |
| 3328 | |
| 3329 | // Create a function for checking the function when hitting a break point. |
| 3330 | frame_function_name = CompileFunction(&env, |
| 3331 | frame_function_name_source, |
| 3332 | "frame_function_name"); |
| 3333 | |
| 3334 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3335 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3336 | |
| 3337 | // Create functions for testing stepping. |
| 3338 | const char* src = "function a() {b();c();}; " |
| 3339 | "function b() {c();}; " |
| 3340 | "function c() {}; "; |
| 3341 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3342 | SetBreakPoint(a, 0); |
| 3343 | |
| 3344 | // Step through invocation of a with step in. |
| 3345 | step_action = StepIn; |
| 3346 | break_point_hit_count = 0; |
| 3347 | expected_step_sequence = "abcbaca"; |
| 3348 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3349 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3350 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3351 | |
| 3352 | // Step through invocation of a with step next. |
| 3353 | step_action = StepNext; |
| 3354 | break_point_hit_count = 0; |
| 3355 | expected_step_sequence = "aaa"; |
| 3356 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3357 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3358 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3359 | |
| 3360 | // Step through invocation of a with step out. |
| 3361 | step_action = StepOut; |
| 3362 | break_point_hit_count = 0; |
| 3363 | expected_step_sequence = "a"; |
| 3364 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3365 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3366 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3369 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3370 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | TEST(StepInOutTree) { |
| 3375 | v8::HandleScope scope; |
| 3376 | DebugLocalContext env; |
| 3377 | |
| 3378 | // Create a function for checking the function when hitting a break point. |
| 3379 | frame_function_name = CompileFunction(&env, |
| 3380 | frame_function_name_source, |
| 3381 | "frame_function_name"); |
| 3382 | |
| 3383 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3384 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Create functions for testing stepping. |
| 3387 | const char* src = "function a() {b(c(d()),d());c(d());d()}; " |
| 3388 | "function b(x,y) {c();}; " |
| 3389 | "function c(x) {}; " |
| 3390 | "function d() {}; "; |
| 3391 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3392 | SetBreakPoint(a, 0); |
| 3393 | |
| 3394 | // Step through invocation of a with step in. |
| 3395 | step_action = StepIn; |
| 3396 | break_point_hit_count = 0; |
| 3397 | expected_step_sequence = "adacadabcbadacada"; |
| 3398 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3399 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3400 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3401 | |
| 3402 | // Step through invocation of a with step next. |
| 3403 | step_action = StepNext; |
| 3404 | break_point_hit_count = 0; |
| 3405 | expected_step_sequence = "aaaa"; |
| 3406 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3407 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3408 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3409 | |
| 3410 | // Step through invocation of a with step out. |
| 3411 | step_action = StepOut; |
| 3412 | break_point_hit_count = 0; |
| 3413 | expected_step_sequence = "a"; |
| 3414 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3415 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3416 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3417 | |
| 3418 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3419 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3420 | CheckDebuggerUnloaded(true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | TEST(StepInOutBranch) { |
| 3425 | v8::HandleScope scope; |
| 3426 | DebugLocalContext env; |
| 3427 | |
| 3428 | // Create a function for checking the function when hitting a break point. |
| 3429 | frame_function_name = CompileFunction(&env, |
| 3430 | frame_function_name_source, |
| 3431 | "frame_function_name"); |
| 3432 | |
| 3433 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3434 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3435 | |
| 3436 | // Create functions for testing stepping. |
| 3437 | const char* src = "function a() {b(false);c();}; " |
| 3438 | "function b(x) {if(x){c();};}; " |
| 3439 | "function c() {}; "; |
| 3440 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3441 | SetBreakPoint(a, 0); |
| 3442 | |
| 3443 | // Step through invocation of a. |
| 3444 | step_action = StepIn; |
| 3445 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3446 | expected_step_sequence = "abbaca"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3447 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3448 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3449 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3450 | |
| 3451 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3452 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3453 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | |
| 3457 | // Test that step in does not step into native functions. |
| 3458 | TEST(DebugStepNatives) { |
| 3459 | v8::HandleScope scope; |
| 3460 | DebugLocalContext env; |
| 3461 | |
| 3462 | // Create a function for testing stepping. |
| 3463 | v8::Local<v8::Function> foo = CompileFunction( |
| 3464 | &env, |
| 3465 | "function foo(){debugger;Math.sin(1);}", |
| 3466 | "foo"); |
| 3467 | |
| 3468 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3469 | v8::Debug::SetDebugEventListener(DebugEventStep); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3470 | |
| 3471 | step_action = StepIn; |
| 3472 | break_point_hit_count = 0; |
| 3473 | foo->Call(env->Global(), 0, NULL); |
| 3474 | |
| 3475 | // With stepping all break locations are hit. |
| 3476 | CHECK_EQ(3, break_point_hit_count); |
| 3477 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3478 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3479 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3480 | |
| 3481 | // Register a debug event listener which just counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3482 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3483 | |
| 3484 | break_point_hit_count = 0; |
| 3485 | foo->Call(env->Global(), 0, NULL); |
| 3486 | |
| 3487 | // Without stepping only active break points are hit. |
| 3488 | CHECK_EQ(1, break_point_hit_count); |
| 3489 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3490 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3491 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3495 | // Test that step in works with function.apply. |
| 3496 | TEST(DebugStepFunctionApply) { |
| 3497 | v8::HandleScope scope; |
| 3498 | DebugLocalContext env; |
| 3499 | |
| 3500 | // Create a function for testing stepping. |
| 3501 | v8::Local<v8::Function> foo = CompileFunction( |
| 3502 | &env, |
| 3503 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3504 | "function foo(){ debugger; bar.apply(this, [1,2,3]); }", |
| 3505 | "foo"); |
| 3506 | |
| 3507 | // Register a debug event listener which steps and counts. |
| 3508 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3509 | |
| 3510 | step_action = StepIn; |
| 3511 | break_point_hit_count = 0; |
| 3512 | foo->Call(env->Global(), 0, NULL); |
| 3513 | |
| 3514 | // With stepping all break locations are hit. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3515 | CHECK_EQ(7, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3516 | |
| 3517 | v8::Debug::SetDebugEventListener(NULL); |
| 3518 | CheckDebuggerUnloaded(); |
| 3519 | |
| 3520 | // Register a debug event listener which just counts. |
| 3521 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3522 | |
| 3523 | break_point_hit_count = 0; |
| 3524 | foo->Call(env->Global(), 0, NULL); |
| 3525 | |
| 3526 | // Without stepping only the debugger statement is hit. |
| 3527 | CHECK_EQ(1, break_point_hit_count); |
| 3528 | |
| 3529 | v8::Debug::SetDebugEventListener(NULL); |
| 3530 | CheckDebuggerUnloaded(); |
| 3531 | } |
| 3532 | |
| 3533 | |
| 3534 | // Test that step in works with function.call. |
| 3535 | TEST(DebugStepFunctionCall) { |
| 3536 | v8::HandleScope scope; |
| 3537 | DebugLocalContext env; |
| 3538 | |
| 3539 | // Create a function for testing stepping. |
| 3540 | v8::Local<v8::Function> foo = CompileFunction( |
| 3541 | &env, |
| 3542 | "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" |
| 3543 | "function foo(a){ debugger;" |
| 3544 | " if (a) {" |
| 3545 | " bar.call(this, 1, 2, 3);" |
| 3546 | " } else {" |
| 3547 | " bar.call(this, 0);" |
| 3548 | " }" |
| 3549 | "}", |
| 3550 | "foo"); |
| 3551 | |
| 3552 | // Register a debug event listener which steps and counts. |
| 3553 | v8::Debug::SetDebugEventListener(DebugEventStep); |
| 3554 | step_action = StepIn; |
| 3555 | |
| 3556 | // Check stepping where the if condition in bar is false. |
| 3557 | break_point_hit_count = 0; |
| 3558 | foo->Call(env->Global(), 0, NULL); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3559 | CHECK_EQ(6, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3560 | |
| 3561 | // Check stepping where the if condition in bar is true. |
| 3562 | break_point_hit_count = 0; |
| 3563 | const int argc = 1; |
| 3564 | v8::Handle<v8::Value> argv[argc] = { v8::True() }; |
| 3565 | foo->Call(env->Global(), argc, argv); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3566 | CHECK_EQ(8, break_point_hit_count); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3567 | |
| 3568 | v8::Debug::SetDebugEventListener(NULL); |
| 3569 | CheckDebuggerUnloaded(); |
| 3570 | |
| 3571 | // Register a debug event listener which just counts. |
| 3572 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); |
| 3573 | |
| 3574 | break_point_hit_count = 0; |
| 3575 | foo->Call(env->Global(), 0, NULL); |
| 3576 | |
| 3577 | // Without stepping only the debugger statement is hit. |
| 3578 | CHECK_EQ(1, break_point_hit_count); |
| 3579 | |
| 3580 | v8::Debug::SetDebugEventListener(NULL); |
| 3581 | CheckDebuggerUnloaded(); |
| 3582 | } |
| 3583 | |
| 3584 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3585 | // Tests that breakpoint will be hit if it's set in script. |
| 3586 | TEST(PauseInScript) { |
| 3587 | v8::HandleScope scope; |
| 3588 | DebugLocalContext env; |
| 3589 | env.ExposeDebug(); |
| 3590 | |
| 3591 | // Register a debug event listener which counts. |
| 3592 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 3593 | |
| 3594 | // Create a script that returns a function. |
| 3595 | const char* src = "(function (evt) {})"; |
| 3596 | const char* script_name = "StepInHandlerTest"; |
| 3597 | |
| 3598 | // Set breakpoint in the script. |
| 3599 | SetScriptBreakPointByNameFromJS(script_name, 0, -1); |
| 3600 | break_point_hit_count = 0; |
| 3601 | |
| 3602 | v8::ScriptOrigin origin(v8::String::New(script_name), v8::Integer::New(0)); |
| 3603 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(src), |
| 3604 | &origin); |
| 3605 | v8::Local<v8::Value> r = script->Run(); |
| 3606 | |
| 3607 | CHECK(r->IsFunction()); |
| 3608 | CHECK_EQ(1, break_point_hit_count); |
| 3609 | |
| 3610 | // Get rid of the debug event listener. |
| 3611 | v8::Debug::SetDebugEventListener(NULL); |
| 3612 | CheckDebuggerUnloaded(); |
| 3613 | } |
| 3614 | |
| 3615 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3616 | // Test break on exceptions. For each exception break combination the number |
| 3617 | // of debug event exception callbacks and message callbacks are collected. The |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3618 | // 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] | 3619 | // debugger is called correctly and the number of message callbacks is used to |
| 3620 | // check that uncaught exceptions are still returned even if there is a break |
| 3621 | // for them. |
| 3622 | TEST(BreakOnException) { |
| 3623 | v8::HandleScope scope; |
| 3624 | DebugLocalContext env; |
| 3625 | env.ExposeDebug(); |
| 3626 | |
| 3627 | v8::internal::Top::TraceException(false); |
| 3628 | |
| 3629 | // Create functions for testing break on exception. |
| 3630 | v8::Local<v8::Function> throws = |
| 3631 | CompileFunction(&env, "function throws(){throw 1;}", "throws"); |
| 3632 | v8::Local<v8::Function> caught = |
| 3633 | CompileFunction(&env, |
| 3634 | "function caught(){try {throws();} catch(e) {};}", |
| 3635 | "caught"); |
| 3636 | v8::Local<v8::Function> notCaught = |
| 3637 | CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); |
| 3638 | |
| 3639 | v8::V8::AddMessageListener(MessageCallbackCount); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3640 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3641 | |
| 3642 | // Initial state should be break on uncaught exception. |
| 3643 | DebugEventCounterClear(); |
| 3644 | MessageCallbackCountClear(); |
| 3645 | caught->Call(env->Global(), 0, NULL); |
| 3646 | CHECK_EQ(0, exception_hit_count); |
| 3647 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3648 | CHECK_EQ(0, message_callback_count); |
| 3649 | notCaught->Call(env->Global(), 0, NULL); |
| 3650 | CHECK_EQ(1, exception_hit_count); |
| 3651 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3652 | CHECK_EQ(1, message_callback_count); |
| 3653 | |
| 3654 | // No break on exception |
| 3655 | DebugEventCounterClear(); |
| 3656 | MessageCallbackCountClear(); |
| 3657 | ChangeBreakOnException(false, false); |
| 3658 | caught->Call(env->Global(), 0, NULL); |
| 3659 | CHECK_EQ(0, exception_hit_count); |
| 3660 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3661 | CHECK_EQ(0, message_callback_count); |
| 3662 | notCaught->Call(env->Global(), 0, NULL); |
| 3663 | CHECK_EQ(0, exception_hit_count); |
| 3664 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3665 | CHECK_EQ(1, message_callback_count); |
| 3666 | |
| 3667 | // Break on uncaught exception |
| 3668 | DebugEventCounterClear(); |
| 3669 | MessageCallbackCountClear(); |
| 3670 | ChangeBreakOnException(false, true); |
| 3671 | caught->Call(env->Global(), 0, NULL); |
| 3672 | CHECK_EQ(0, exception_hit_count); |
| 3673 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3674 | CHECK_EQ(0, message_callback_count); |
| 3675 | notCaught->Call(env->Global(), 0, NULL); |
| 3676 | CHECK_EQ(1, exception_hit_count); |
| 3677 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3678 | CHECK_EQ(1, message_callback_count); |
| 3679 | |
| 3680 | // Break on exception and uncaught exception |
| 3681 | DebugEventCounterClear(); |
| 3682 | MessageCallbackCountClear(); |
| 3683 | ChangeBreakOnException(true, true); |
| 3684 | caught->Call(env->Global(), 0, NULL); |
| 3685 | CHECK_EQ(1, exception_hit_count); |
| 3686 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3687 | CHECK_EQ(0, message_callback_count); |
| 3688 | notCaught->Call(env->Global(), 0, NULL); |
| 3689 | CHECK_EQ(2, exception_hit_count); |
| 3690 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3691 | CHECK_EQ(1, message_callback_count); |
| 3692 | |
| 3693 | // Break on exception |
| 3694 | DebugEventCounterClear(); |
| 3695 | MessageCallbackCountClear(); |
| 3696 | ChangeBreakOnException(true, false); |
| 3697 | caught->Call(env->Global(), 0, NULL); |
| 3698 | CHECK_EQ(1, exception_hit_count); |
| 3699 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3700 | CHECK_EQ(0, message_callback_count); |
| 3701 | notCaught->Call(env->Global(), 0, NULL); |
| 3702 | CHECK_EQ(2, exception_hit_count); |
| 3703 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3704 | CHECK_EQ(1, message_callback_count); |
| 3705 | |
| 3706 | // No break on exception using JavaScript |
| 3707 | DebugEventCounterClear(); |
| 3708 | MessageCallbackCountClear(); |
| 3709 | ChangeBreakOnExceptionFromJS(false, false); |
| 3710 | caught->Call(env->Global(), 0, NULL); |
| 3711 | CHECK_EQ(0, exception_hit_count); |
| 3712 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3713 | CHECK_EQ(0, message_callback_count); |
| 3714 | notCaught->Call(env->Global(), 0, NULL); |
| 3715 | CHECK_EQ(0, exception_hit_count); |
| 3716 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3717 | CHECK_EQ(1, message_callback_count); |
| 3718 | |
| 3719 | // Break on uncaught exception using JavaScript |
| 3720 | DebugEventCounterClear(); |
| 3721 | MessageCallbackCountClear(); |
| 3722 | ChangeBreakOnExceptionFromJS(false, true); |
| 3723 | caught->Call(env->Global(), 0, NULL); |
| 3724 | CHECK_EQ(0, exception_hit_count); |
| 3725 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3726 | CHECK_EQ(0, message_callback_count); |
| 3727 | notCaught->Call(env->Global(), 0, NULL); |
| 3728 | CHECK_EQ(1, exception_hit_count); |
| 3729 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3730 | CHECK_EQ(1, message_callback_count); |
| 3731 | |
| 3732 | // Break on exception and uncaught exception using JavaScript |
| 3733 | DebugEventCounterClear(); |
| 3734 | MessageCallbackCountClear(); |
| 3735 | ChangeBreakOnExceptionFromJS(true, true); |
| 3736 | caught->Call(env->Global(), 0, NULL); |
| 3737 | CHECK_EQ(1, exception_hit_count); |
| 3738 | CHECK_EQ(0, message_callback_count); |
| 3739 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3740 | notCaught->Call(env->Global(), 0, NULL); |
| 3741 | CHECK_EQ(2, exception_hit_count); |
| 3742 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3743 | CHECK_EQ(1, message_callback_count); |
| 3744 | |
| 3745 | // Break on exception using JavaScript |
| 3746 | DebugEventCounterClear(); |
| 3747 | MessageCallbackCountClear(); |
| 3748 | ChangeBreakOnExceptionFromJS(true, false); |
| 3749 | caught->Call(env->Global(), 0, NULL); |
| 3750 | CHECK_EQ(1, exception_hit_count); |
| 3751 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3752 | CHECK_EQ(0, message_callback_count); |
| 3753 | notCaught->Call(env->Global(), 0, NULL); |
| 3754 | CHECK_EQ(2, exception_hit_count); |
| 3755 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3756 | CHECK_EQ(1, message_callback_count); |
| 3757 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3758 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3759 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3760 | v8::V8::RemoveMessageListeners(MessageCallbackCount); |
| 3761 | } |
| 3762 | |
| 3763 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3764 | // Test break on exception from compiler errors. When compiling using |
| 3765 | // v8::Script::Compile there is no JavaScript stack whereas when compiling using |
| 3766 | // eval there are JavaScript frames. |
| 3767 | TEST(BreakOnCompileException) { |
| 3768 | v8::HandleScope scope; |
| 3769 | DebugLocalContext env; |
| 3770 | |
| 3771 | v8::internal::Top::TraceException(false); |
| 3772 | |
| 3773 | // Create a function for checking the function when hitting a break point. |
| 3774 | frame_count = CompileFunction(&env, frame_count_source, "frame_count"); |
| 3775 | |
| 3776 | v8::V8::AddMessageListener(MessageCallbackCount); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3777 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3778 | |
| 3779 | DebugEventCounterClear(); |
| 3780 | MessageCallbackCountClear(); |
| 3781 | |
| 3782 | // Check initial state. |
| 3783 | CHECK_EQ(0, exception_hit_count); |
| 3784 | CHECK_EQ(0, uncaught_exception_hit_count); |
| 3785 | CHECK_EQ(0, message_callback_count); |
| 3786 | CHECK_EQ(-1, last_js_stack_height); |
| 3787 | |
| 3788 | // Throws SyntaxError: Unexpected end of input |
| 3789 | v8::Script::Compile(v8::String::New("+++")); |
| 3790 | CHECK_EQ(1, exception_hit_count); |
| 3791 | CHECK_EQ(1, uncaught_exception_hit_count); |
| 3792 | CHECK_EQ(1, message_callback_count); |
| 3793 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 3794 | |
| 3795 | // Throws SyntaxError: Unexpected identifier |
| 3796 | v8::Script::Compile(v8::String::New("x x")); |
| 3797 | CHECK_EQ(2, exception_hit_count); |
| 3798 | CHECK_EQ(2, uncaught_exception_hit_count); |
| 3799 | CHECK_EQ(2, message_callback_count); |
| 3800 | CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. |
| 3801 | |
| 3802 | // Throws SyntaxError: Unexpected end of input |
| 3803 | v8::Script::Compile(v8::String::New("eval('+++')"))->Run(); |
| 3804 | CHECK_EQ(3, exception_hit_count); |
| 3805 | CHECK_EQ(3, uncaught_exception_hit_count); |
| 3806 | CHECK_EQ(3, message_callback_count); |
| 3807 | CHECK_EQ(1, last_js_stack_height); |
| 3808 | |
| 3809 | // Throws SyntaxError: Unexpected identifier |
| 3810 | v8::Script::Compile(v8::String::New("eval('x x')"))->Run(); |
| 3811 | CHECK_EQ(4, exception_hit_count); |
| 3812 | CHECK_EQ(4, uncaught_exception_hit_count); |
| 3813 | CHECK_EQ(4, message_callback_count); |
| 3814 | CHECK_EQ(1, last_js_stack_height); |
| 3815 | } |
| 3816 | |
| 3817 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3818 | TEST(StepWithException) { |
| 3819 | v8::HandleScope scope; |
| 3820 | DebugLocalContext env; |
| 3821 | |
| 3822 | // Create a function for checking the function when hitting a break point. |
| 3823 | frame_function_name = CompileFunction(&env, |
| 3824 | frame_function_name_source, |
| 3825 | "frame_function_name"); |
| 3826 | |
| 3827 | // Register a debug event listener which steps and counts. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3828 | v8::Debug::SetDebugEventListener(DebugEventStepSequence); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3829 | |
| 3830 | // Create functions for testing stepping. |
| 3831 | const char* src = "function a() { n(); }; " |
| 3832 | "function b() { c(); }; " |
| 3833 | "function c() { n(); }; " |
| 3834 | "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " |
| 3835 | "function e() { n(); }; " |
| 3836 | "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " |
| 3837 | "function g() { h(); }; " |
| 3838 | "function h() { x = 1; throw 1; }; "; |
| 3839 | |
| 3840 | // Step through invocation of a. |
| 3841 | v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); |
| 3842 | SetBreakPoint(a, 0); |
| 3843 | step_action = StepIn; |
| 3844 | break_point_hit_count = 0; |
| 3845 | expected_step_sequence = "aa"; |
| 3846 | a->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3847 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3848 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3849 | |
| 3850 | // Step through invocation of b + c. |
| 3851 | v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); |
| 3852 | SetBreakPoint(b, 0); |
| 3853 | step_action = StepIn; |
| 3854 | break_point_hit_count = 0; |
| 3855 | expected_step_sequence = "bcc"; |
| 3856 | b->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3857 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3858 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3859 | // Step through invocation of d + e. |
| 3860 | v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); |
| 3861 | SetBreakPoint(d, 0); |
| 3862 | ChangeBreakOnException(false, true); |
| 3863 | step_action = StepIn; |
| 3864 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3865 | expected_step_sequence = "ddedd"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3866 | d->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3867 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3868 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3869 | |
| 3870 | // Step through invocation of d + e now with break on caught exceptions. |
| 3871 | ChangeBreakOnException(true, true); |
| 3872 | step_action = StepIn; |
| 3873 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3874 | expected_step_sequence = "ddeedd"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3875 | d->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3876 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3877 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3878 | |
| 3879 | // Step through invocation of f + g + h. |
| 3880 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 3881 | SetBreakPoint(f, 0); |
| 3882 | ChangeBreakOnException(false, true); |
| 3883 | step_action = StepIn; |
| 3884 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3885 | expected_step_sequence = "ffghhff"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3886 | f->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3887 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3888 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3889 | |
| 3890 | // Step through invocation of f + g + h now with break on caught exceptions. |
| 3891 | ChangeBreakOnException(true, true); |
| 3892 | step_action = StepIn; |
| 3893 | break_point_hit_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3894 | expected_step_sequence = "ffghhhff"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3895 | f->Call(env->Global(), 0, NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3896 | CHECK_EQ(StrLength(expected_step_sequence), |
| 3897 | break_point_hit_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3898 | |
| 3899 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3900 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3901 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | |
| 3905 | TEST(DebugBreak) { |
| 3906 | v8::HandleScope scope; |
| 3907 | DebugLocalContext env; |
| 3908 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 3909 | // This test should be run with option --verify-heap. As --verify-heap is |
| 3910 | // only available in debug mode only check for it in that case. |
| 3911 | #ifdef DEBUG |
| 3912 | CHECK(v8::internal::FLAG_verify_heap); |
| 3913 | #endif |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3914 | |
| 3915 | // 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] | 3916 | v8::Debug::SetDebugEventListener(DebugEventBreak); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3917 | |
| 3918 | // Create a function for testing stepping. |
| 3919 | const char* src = "function f0() {}" |
| 3920 | "function f1(x1) {}" |
| 3921 | "function f2(x1,x2) {}" |
| 3922 | "function f3(x1,x2,x3) {}"; |
| 3923 | v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); |
| 3924 | v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); |
| 3925 | v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); |
| 3926 | v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); |
| 3927 | |
| 3928 | // Call the function to make sure it is compiled. |
| 3929 | v8::Handle<v8::Value> argv[] = { v8::Number::New(1), |
| 3930 | v8::Number::New(1), |
| 3931 | v8::Number::New(1), |
| 3932 | v8::Number::New(1) }; |
| 3933 | |
| 3934 | // Call all functions to make sure that they are compiled. |
| 3935 | f0->Call(env->Global(), 0, NULL); |
| 3936 | f1->Call(env->Global(), 0, NULL); |
| 3937 | f2->Call(env->Global(), 0, NULL); |
| 3938 | f3->Call(env->Global(), 0, NULL); |
| 3939 | |
| 3940 | // Set the debug break flag. |
| 3941 | v8::Debug::DebugBreak(); |
| 3942 | |
| 3943 | // Call all functions with different argument count. |
| 3944 | break_point_hit_count = 0; |
| 3945 | for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) { |
| 3946 | f0->Call(env->Global(), i, argv); |
| 3947 | f1->Call(env->Global(), i, argv); |
| 3948 | f2->Call(env->Global(), i, argv); |
| 3949 | f3->Call(env->Global(), i, argv); |
| 3950 | } |
| 3951 | |
| 3952 | // One break for each function called. |
| 3953 | CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count); |
| 3954 | |
| 3955 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3956 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3957 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | |
| 3961 | // Test to ensure that JavaScript code keeps running while the debug break |
| 3962 | // through the stack limit flag is set but breaks are disabled. |
| 3963 | TEST(DisableBreak) { |
| 3964 | v8::HandleScope scope; |
| 3965 | DebugLocalContext env; |
| 3966 | |
| 3967 | // 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] | 3968 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3969 | |
| 3970 | // Create a function for testing stepping. |
| 3971 | const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; |
| 3972 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 3973 | |
| 3974 | // Set the debug break flag. |
| 3975 | v8::Debug::DebugBreak(); |
| 3976 | |
| 3977 | // Call all functions with different argument count. |
| 3978 | break_point_hit_count = 0; |
| 3979 | f->Call(env->Global(), 0, NULL); |
| 3980 | CHECK_EQ(1, break_point_hit_count); |
| 3981 | |
| 3982 | { |
| 3983 | v8::Debug::DebugBreak(); |
| 3984 | v8::internal::DisableBreak disable_break(true); |
| 3985 | f->Call(env->Global(), 0, NULL); |
| 3986 | CHECK_EQ(1, break_point_hit_count); |
| 3987 | } |
| 3988 | |
| 3989 | f->Call(env->Global(), 0, NULL); |
| 3990 | CHECK_EQ(2, break_point_hit_count); |
| 3991 | |
| 3992 | // Get rid of the debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 3993 | v8::Debug::SetDebugEventListener(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3994 | CheckDebuggerUnloaded(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3997 | static const char* kSimpleExtensionSource = |
| 3998 | "(function Foo() {" |
| 3999 | " return 4;" |
| 4000 | "})() "; |
| 4001 | |
| 4002 | // http://crbug.com/28933 |
| 4003 | // Test that debug break is disabled when bootstrapper is active. |
| 4004 | TEST(NoBreakWhenBootstrapping) { |
| 4005 | v8::HandleScope scope; |
| 4006 | |
| 4007 | // Register a debug event listener which sets the break flag and counts. |
| 4008 | v8::Debug::SetDebugEventListener(DebugEventCounter); |
| 4009 | |
| 4010 | // Set the debug break flag. |
| 4011 | v8::Debug::DebugBreak(); |
| 4012 | break_point_hit_count = 0; |
| 4013 | { |
| 4014 | // Create a context with an extension to make sure that some JavaScript |
| 4015 | // code is executed during bootstrapping. |
| 4016 | v8::RegisterExtension(new v8::Extension("simpletest", |
| 4017 | kSimpleExtensionSource)); |
| 4018 | const char* extension_names[] = { "simpletest" }; |
| 4019 | v8::ExtensionConfiguration extensions(1, extension_names); |
| 4020 | v8::Persistent<v8::Context> context = v8::Context::New(&extensions); |
| 4021 | context.Dispose(); |
| 4022 | } |
| 4023 | // Check that no DebugBreak events occured during the context creation. |
| 4024 | CHECK_EQ(0, break_point_hit_count); |
| 4025 | |
| 4026 | // Get rid of the debug event listener. |
| 4027 | v8::Debug::SetDebugEventListener(NULL); |
| 4028 | CheckDebuggerUnloaded(); |
| 4029 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4030 | |
| 4031 | static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) { |
| 4032 | v8::Handle<v8::Array> result = v8::Array::New(3); |
| 4033 | result->Set(v8::Integer::New(0), v8::String::New("a")); |
| 4034 | result->Set(v8::Integer::New(1), v8::String::New("b")); |
| 4035 | result->Set(v8::Integer::New(2), v8::String::New("c")); |
| 4036 | return result; |
| 4037 | } |
| 4038 | |
| 4039 | |
| 4040 | static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) { |
| 4041 | v8::Handle<v8::Array> result = v8::Array::New(2); |
| 4042 | result->Set(v8::Integer::New(0), v8::Number::New(1)); |
| 4043 | result->Set(v8::Integer::New(1), v8::Number::New(10)); |
| 4044 | return result; |
| 4045 | } |
| 4046 | |
| 4047 | |
| 4048 | static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name, |
| 4049 | const v8::AccessorInfo& info) { |
| 4050 | v8::String::AsciiValue n(name); |
| 4051 | if (strcmp(*n, "a") == 0) { |
| 4052 | return v8::String::New("AA"); |
| 4053 | } else if (strcmp(*n, "b") == 0) { |
| 4054 | return v8::String::New("BB"); |
| 4055 | } else if (strcmp(*n, "c") == 0) { |
| 4056 | return v8::String::New("CC"); |
| 4057 | } else { |
| 4058 | return v8::Undefined(); |
| 4059 | } |
| 4060 | |
| 4061 | return name; |
| 4062 | } |
| 4063 | |
| 4064 | |
| 4065 | static v8::Handle<v8::Value> IndexedGetter(uint32_t index, |
| 4066 | const v8::AccessorInfo& info) { |
| 4067 | return v8::Number::New(index + 1); |
| 4068 | } |
| 4069 | |
| 4070 | |
| 4071 | TEST(InterceptorPropertyMirror) { |
| 4072 | // Create a V8 environment with debug access. |
| 4073 | v8::HandleScope scope; |
| 4074 | DebugLocalContext env; |
| 4075 | env.ExposeDebug(); |
| 4076 | |
| 4077 | // Create object with named interceptor. |
| 4078 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4079 | named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); |
| 4080 | env->Global()->Set(v8::String::New("intercepted_named"), |
| 4081 | named->NewInstance()); |
| 4082 | |
| 4083 | // Create object with indexed interceptor. |
| 4084 | v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(); |
| 4085 | indexed->SetIndexedPropertyHandler(IndexedGetter, |
| 4086 | NULL, |
| 4087 | NULL, |
| 4088 | NULL, |
| 4089 | IndexedEnum); |
| 4090 | env->Global()->Set(v8::String::New("intercepted_indexed"), |
| 4091 | indexed->NewInstance()); |
| 4092 | |
| 4093 | // Create object with both named and indexed interceptor. |
| 4094 | v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New(); |
| 4095 | both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); |
| 4096 | both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum); |
| 4097 | env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance()); |
| 4098 | |
| 4099 | // Get mirrors for the three objects with interceptor. |
| 4100 | CompileRun( |
| 4101 | "named_mirror = debug.MakeMirror(intercepted_named);" |
| 4102 | "indexed_mirror = debug.MakeMirror(intercepted_indexed);" |
| 4103 | "both_mirror = debug.MakeMirror(intercepted_both)"); |
| 4104 | CHECK(CompileRun( |
| 4105 | "named_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4106 | CHECK(CompileRun( |
| 4107 | "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4108 | CHECK(CompileRun( |
| 4109 | "both_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4110 | |
| 4111 | // Get the property names from the interceptors |
| 4112 | CompileRun( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4113 | "named_names = named_mirror.propertyNames();" |
| 4114 | "indexed_names = indexed_mirror.propertyNames();" |
| 4115 | "both_names = both_mirror.propertyNames()"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4116 | CHECK_EQ(3, CompileRun("named_names.length")->Int32Value()); |
| 4117 | CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value()); |
| 4118 | CHECK_EQ(5, CompileRun("both_names.length")->Int32Value()); |
| 4119 | |
| 4120 | // Check the expected number of properties. |
| 4121 | const char* source; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4122 | source = "named_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4123 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4124 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4125 | source = "indexed_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4126 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4127 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4128 | source = "both_mirror.properties().length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4129 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4130 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4131 | // 1 is PropertyKind.Named; |
| 4132 | source = "both_mirror.properties(1).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4133 | CHECK_EQ(3, CompileRun(source)->Int32Value()); |
| 4134 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4135 | // 2 is PropertyKind.Indexed; |
| 4136 | source = "both_mirror.properties(2).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4137 | CHECK_EQ(2, CompileRun(source)->Int32Value()); |
| 4138 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4139 | // 3 is PropertyKind.Named | PropertyKind.Indexed; |
| 4140 | source = "both_mirror.properties(3).length"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4141 | CHECK_EQ(5, CompileRun(source)->Int32Value()); |
| 4142 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4143 | // Get the interceptor properties for the object with only named interceptor. |
| 4144 | CompileRun("named_values = named_mirror.properties()"); |
| 4145 | |
| 4146 | // Check that the properties are interceptor properties. |
| 4147 | for (int i = 0; i < 3; i++) { |
| 4148 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4149 | OS::SNPrintF(buffer, |
| 4150 | "named_values[%d] instanceof debug.PropertyMirror", i); |
| 4151 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4152 | |
| 4153 | // 4 is PropertyType.Interceptor |
| 4154 | OS::SNPrintF(buffer, "named_values[%d].propertyType()", i); |
| 4155 | CHECK_EQ(4, CompileRun(buffer.start())->Int32Value()); |
| 4156 | |
| 4157 | OS::SNPrintF(buffer, "named_values[%d].isNative()", i); |
| 4158 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4159 | } |
| 4160 | |
| 4161 | // Get the interceptor properties for the object with only indexed |
| 4162 | // interceptor. |
| 4163 | CompileRun("indexed_values = indexed_mirror.properties()"); |
| 4164 | |
| 4165 | // Check that the properties are interceptor properties. |
| 4166 | for (int i = 0; i < 2; i++) { |
| 4167 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4168 | OS::SNPrintF(buffer, |
| 4169 | "indexed_values[%d] instanceof debug.PropertyMirror", i); |
| 4170 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4171 | } |
| 4172 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4173 | // Get the interceptor properties for the object with both types of |
| 4174 | // interceptors. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4175 | CompileRun("both_values = both_mirror.properties()"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4176 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4177 | // Check that the properties are interceptor properties. |
| 4178 | for (int i = 0; i < 5; i++) { |
| 4179 | EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; |
| 4180 | OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); |
| 4181 | CHECK(CompileRun(buffer.start())->BooleanValue()); |
| 4182 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4183 | |
| 4184 | // Check the property names. |
| 4185 | source = "both_values[0].name() == 'a'"; |
| 4186 | CHECK(CompileRun(source)->BooleanValue()); |
| 4187 | |
| 4188 | source = "both_values[1].name() == 'b'"; |
| 4189 | CHECK(CompileRun(source)->BooleanValue()); |
| 4190 | |
| 4191 | source = "both_values[2].name() == 'c'"; |
| 4192 | CHECK(CompileRun(source)->BooleanValue()); |
| 4193 | |
| 4194 | source = "both_values[3].name() == 1"; |
| 4195 | CHECK(CompileRun(source)->BooleanValue()); |
| 4196 | |
| 4197 | source = "both_values[4].name() == 10"; |
| 4198 | CHECK(CompileRun(source)->BooleanValue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 4202 | TEST(HiddenPrototypePropertyMirror) { |
| 4203 | // Create a V8 environment with debug access. |
| 4204 | v8::HandleScope scope; |
| 4205 | DebugLocalContext env; |
| 4206 | env.ExposeDebug(); |
| 4207 | |
| 4208 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 4209 | t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0)); |
| 4210 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 4211 | t1->SetHiddenPrototype(true); |
| 4212 | t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1)); |
| 4213 | v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 4214 | t2->SetHiddenPrototype(true); |
| 4215 | t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2)); |
| 4216 | v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 4217 | t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3)); |
| 4218 | |
| 4219 | // Create object and set them on the global object. |
| 4220 | v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
| 4221 | env->Global()->Set(v8::String::New("o0"), o0); |
| 4222 | v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 4223 | env->Global()->Set(v8::String::New("o1"), o1); |
| 4224 | v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 4225 | env->Global()->Set(v8::String::New("o2"), o2); |
| 4226 | v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 4227 | env->Global()->Set(v8::String::New("o3"), o3); |
| 4228 | |
| 4229 | // Get mirrors for the four objects. |
| 4230 | CompileRun( |
| 4231 | "o0_mirror = debug.MakeMirror(o0);" |
| 4232 | "o1_mirror = debug.MakeMirror(o1);" |
| 4233 | "o2_mirror = debug.MakeMirror(o2);" |
| 4234 | "o3_mirror = debug.MakeMirror(o3)"); |
| 4235 | CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4236 | CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4237 | CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4238 | CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4239 | |
| 4240 | // Check that each object has one property. |
| 4241 | CHECK_EQ(1, CompileRun( |
| 4242 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4243 | CHECK_EQ(1, CompileRun( |
| 4244 | "o1_mirror.propertyNames().length")->Int32Value()); |
| 4245 | CHECK_EQ(1, CompileRun( |
| 4246 | "o2_mirror.propertyNames().length")->Int32Value()); |
| 4247 | CHECK_EQ(1, CompileRun( |
| 4248 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4249 | |
| 4250 | // Set o1 as prototype for o0. o1 has the hidden prototype flag so all |
| 4251 | // properties on o1 should be seen on o0. |
| 4252 | o0->Set(v8::String::New("__proto__"), o1); |
| 4253 | CHECK_EQ(2, CompileRun( |
| 4254 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4255 | CHECK_EQ(0, CompileRun( |
| 4256 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4257 | CHECK_EQ(1, CompileRun( |
| 4258 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4259 | |
| 4260 | // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden |
| 4261 | // prototype flag. o2 also has the hidden prototype flag so all properties |
| 4262 | // on o2 should be seen on o0 as well as properties on o1. |
| 4263 | o0->Set(v8::String::New("__proto__"), o2); |
| 4264 | CHECK_EQ(3, CompileRun( |
| 4265 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4266 | CHECK_EQ(0, CompileRun( |
| 4267 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4268 | CHECK_EQ(1, CompileRun( |
| 4269 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4270 | CHECK_EQ(2, CompileRun( |
| 4271 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4272 | |
| 4273 | // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and |
| 4274 | // o2 has the hidden prototype flag. o3 does not have the hidden prototype |
| 4275 | // flag so properties on o3 should not be seen on o0 whereas the properties |
| 4276 | // from o1 and o2 should still be seen on o0. |
| 4277 | // Final prototype chain: o0 -> o1 -> o2 -> o3 |
| 4278 | // Hidden prototypes: ^^ ^^ |
| 4279 | o0->Set(v8::String::New("__proto__"), o3); |
| 4280 | CHECK_EQ(3, CompileRun( |
| 4281 | "o0_mirror.propertyNames().length")->Int32Value()); |
| 4282 | CHECK_EQ(1, CompileRun( |
| 4283 | "o3_mirror.propertyNames().length")->Int32Value()); |
| 4284 | CHECK_EQ(0, CompileRun( |
| 4285 | "o0_mirror.property('x').value().value()")->Int32Value()); |
| 4286 | CHECK_EQ(1, CompileRun( |
| 4287 | "o0_mirror.property('y').value().value()")->Int32Value()); |
| 4288 | CHECK_EQ(2, CompileRun( |
| 4289 | "o0_mirror.property('z').value().value()")->Int32Value()); |
| 4290 | CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue()); |
| 4291 | |
| 4292 | // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. |
| 4293 | CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue()); |
| 4294 | } |
| 4295 | |
| 4296 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4297 | static v8::Handle<v8::Value> ProtperyXNativeGetter( |
| 4298 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 4299 | return v8::Integer::New(10); |
| 4300 | } |
| 4301 | |
| 4302 | |
| 4303 | TEST(NativeGetterPropertyMirror) { |
| 4304 | // Create a V8 environment with debug access. |
| 4305 | v8::HandleScope scope; |
| 4306 | DebugLocalContext env; |
| 4307 | env.ExposeDebug(); |
| 4308 | |
| 4309 | v8::Handle<v8::String> name = v8::String::New("x"); |
| 4310 | // Create object with named accessor. |
| 4311 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4312 | named->SetAccessor(name, &ProtperyXNativeGetter, NULL, |
| 4313 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4314 | |
| 4315 | // Create object with named property getter. |
| 4316 | env->Global()->Set(v8::String::New("instance"), named->NewInstance()); |
| 4317 | CHECK_EQ(10, CompileRun("instance.x")->Int32Value()); |
| 4318 | |
| 4319 | // Get mirror for the object with property getter. |
| 4320 | CompileRun("instance_mirror = debug.MakeMirror(instance);"); |
| 4321 | CHECK(CompileRun( |
| 4322 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4323 | |
| 4324 | CompileRun("named_names = instance_mirror.propertyNames();"); |
| 4325 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4326 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4327 | CHECK(CompileRun( |
| 4328 | "instance_mirror.property('x').value().isNumber()")->BooleanValue()); |
| 4329 | CHECK(CompileRun( |
| 4330 | "instance_mirror.property('x').value().value() == 10")->BooleanValue()); |
| 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | static v8::Handle<v8::Value> ProtperyXNativeGetterThrowingError( |
| 4335 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 4336 | return CompileRun("throw new Error('Error message');"); |
| 4337 | } |
| 4338 | |
| 4339 | |
| 4340 | TEST(NativeGetterThrowingErrorPropertyMirror) { |
| 4341 | // Create a V8 environment with debug access. |
| 4342 | v8::HandleScope scope; |
| 4343 | DebugLocalContext env; |
| 4344 | env.ExposeDebug(); |
| 4345 | |
| 4346 | v8::Handle<v8::String> name = v8::String::New("x"); |
| 4347 | // Create object with named accessor. |
| 4348 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 4349 | named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, |
| 4350 | v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); |
| 4351 | |
| 4352 | // Create object with named property getter. |
| 4353 | env->Global()->Set(v8::String::New("instance"), named->NewInstance()); |
| 4354 | |
| 4355 | // Get mirror for the object with property getter. |
| 4356 | CompileRun("instance_mirror = debug.MakeMirror(instance);"); |
| 4357 | CHECK(CompileRun( |
| 4358 | "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4359 | CompileRun("named_names = instance_mirror.propertyNames();"); |
| 4360 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4361 | CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); |
| 4362 | CHECK(CompileRun( |
| 4363 | "instance_mirror.property('x').value().isError()")->BooleanValue()); |
| 4364 | |
| 4365 | // Check that the message is that passed to the Error constructor. |
| 4366 | CHECK(CompileRun( |
| 4367 | "instance_mirror.property('x').value().message() == 'Error message'")-> |
| 4368 | BooleanValue()); |
| 4369 | } |
| 4370 | |
| 4371 | |
ager@chromium.org | c730f77 | 2009-11-11 10:11:16 +0000 | [diff] [blame] | 4372 | // Test that hidden properties object is not returned as an unnamed property |
| 4373 | // among regular properties. |
| 4374 | // See http://crbug.com/26491 |
| 4375 | TEST(NoHiddenProperties) { |
| 4376 | // Create a V8 environment with debug access. |
| 4377 | v8::HandleScope scope; |
| 4378 | DebugLocalContext env; |
| 4379 | env.ExposeDebug(); |
| 4380 | |
| 4381 | // Create an object in the global scope. |
| 4382 | const char* source = "var obj = {a: 1};"; |
| 4383 | v8::Script::Compile(v8::String::New(source))->Run(); |
| 4384 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( |
| 4385 | env->Global()->Get(v8::String::New("obj"))); |
| 4386 | // Set a hidden property on the object. |
| 4387 | obj->SetHiddenValue(v8::String::New("v8::test-debug::a"), |
| 4388 | v8::Int32::New(11)); |
| 4389 | |
| 4390 | // Get mirror for the object with property getter. |
| 4391 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4392 | CHECK(CompileRun( |
| 4393 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4394 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4395 | // There should be exactly one property. But there is also an unnamed |
| 4396 | // property whose value is hidden properties dictionary. The latter |
| 4397 | // property should not be in the list of reguar properties. |
| 4398 | CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); |
| 4399 | CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue()); |
| 4400 | CHECK(CompileRun( |
| 4401 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4402 | |
| 4403 | // Object created by t0 will become hidden prototype of object 'obj'. |
| 4404 | v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 4405 | t0->InstanceTemplate()->Set(v8::String::New("b"), v8::Number::New(2)); |
| 4406 | t0->SetHiddenPrototype(true); |
| 4407 | v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 4408 | t1->InstanceTemplate()->Set(v8::String::New("c"), v8::Number::New(3)); |
| 4409 | |
| 4410 | // Create proto objects, add hidden properties to them and set them on |
| 4411 | // the global object. |
| 4412 | v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance(); |
| 4413 | protoObj->SetHiddenValue(v8::String::New("v8::test-debug::b"), |
| 4414 | v8::Int32::New(12)); |
| 4415 | env->Global()->Set(v8::String::New("protoObj"), protoObj); |
| 4416 | v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance(); |
| 4417 | grandProtoObj->SetHiddenValue(v8::String::New("v8::test-debug::c"), |
| 4418 | v8::Int32::New(13)); |
| 4419 | env->Global()->Set(v8::String::New("grandProtoObj"), grandProtoObj); |
| 4420 | |
| 4421 | // Setting prototypes: obj->protoObj->grandProtoObj |
| 4422 | protoObj->Set(v8::String::New("__proto__"), grandProtoObj); |
| 4423 | obj->Set(v8::String::New("__proto__"), protoObj); |
| 4424 | |
| 4425 | // Get mirror for the object with property getter. |
| 4426 | CompileRun("var obj_mirror = debug.MakeMirror(obj);"); |
| 4427 | CHECK(CompileRun( |
| 4428 | "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); |
| 4429 | CompileRun("var named_names = obj_mirror.propertyNames();"); |
| 4430 | // There should be exactly two properties - one from the object itself and |
| 4431 | // another from its hidden prototype. |
| 4432 | CHECK_EQ(2, CompileRun("named_names.length")->Int32Value()); |
| 4433 | CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" |
| 4434 | "named_names[1] == 'b'")->BooleanValue()); |
| 4435 | CHECK(CompileRun( |
| 4436 | "obj_mirror.property('a').value().value() == 1")->BooleanValue()); |
| 4437 | CHECK(CompileRun( |
| 4438 | "obj_mirror.property('b').value().value() == 2")->BooleanValue()); |
| 4439 | } |
| 4440 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4441 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4442 | // Multithreaded tests of JSON debugger protocol |
| 4443 | |
| 4444 | // Support classes |
| 4445 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4446 | // Provides synchronization between k threads, where k is an input to the |
| 4447 | // constructor. The Wait() call blocks a thread until it is called for the |
| 4448 | // k'th time, then all calls return. Each ThreadBarrier object can only |
| 4449 | // be used once. |
| 4450 | class ThreadBarrier { |
| 4451 | public: |
| 4452 | explicit ThreadBarrier(int num_threads); |
| 4453 | ~ThreadBarrier(); |
| 4454 | void Wait(); |
| 4455 | private: |
| 4456 | int num_threads_; |
| 4457 | int num_blocked_; |
| 4458 | v8::internal::Mutex* lock_; |
| 4459 | v8::internal::Semaphore* sem_; |
| 4460 | bool invalid_; |
| 4461 | }; |
| 4462 | |
| 4463 | ThreadBarrier::ThreadBarrier(int num_threads) |
| 4464 | : num_threads_(num_threads), num_blocked_(0) { |
| 4465 | lock_ = OS::CreateMutex(); |
| 4466 | sem_ = OS::CreateSemaphore(0); |
| 4467 | invalid_ = false; // A barrier may only be used once. Then it is invalid. |
| 4468 | } |
| 4469 | |
| 4470 | // Do not call, due to race condition with Wait(). |
| 4471 | // Could be resolved with Pthread condition variables. |
| 4472 | ThreadBarrier::~ThreadBarrier() { |
| 4473 | lock_->Lock(); |
| 4474 | delete lock_; |
| 4475 | delete sem_; |
| 4476 | } |
| 4477 | |
| 4478 | void ThreadBarrier::Wait() { |
| 4479 | lock_->Lock(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4480 | CHECK(!invalid_); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4481 | if (num_blocked_ == num_threads_ - 1) { |
| 4482 | // Signal and unblock all waiting threads. |
| 4483 | for (int i = 0; i < num_threads_ - 1; ++i) { |
| 4484 | sem_->Signal(); |
| 4485 | } |
| 4486 | invalid_ = true; |
| 4487 | printf("BARRIER\n\n"); |
| 4488 | fflush(stdout); |
| 4489 | lock_->Unlock(); |
| 4490 | } else { // Wait for the semaphore. |
| 4491 | ++num_blocked_; |
| 4492 | lock_->Unlock(); // Potential race condition with destructor because |
| 4493 | sem_->Wait(); // these two lines are not atomic. |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | // A set containing enough barriers and semaphores for any of the tests. |
| 4498 | class Barriers { |
| 4499 | public: |
| 4500 | Barriers(); |
| 4501 | void Initialize(); |
| 4502 | ThreadBarrier barrier_1; |
| 4503 | ThreadBarrier barrier_2; |
| 4504 | ThreadBarrier barrier_3; |
| 4505 | ThreadBarrier barrier_4; |
| 4506 | ThreadBarrier barrier_5; |
| 4507 | v8::internal::Semaphore* semaphore_1; |
| 4508 | v8::internal::Semaphore* semaphore_2; |
| 4509 | }; |
| 4510 | |
| 4511 | Barriers::Barriers() : barrier_1(2), barrier_2(2), |
| 4512 | barrier_3(2), barrier_4(2), barrier_5(2) {} |
| 4513 | |
| 4514 | void Barriers::Initialize() { |
| 4515 | semaphore_1 = OS::CreateSemaphore(0); |
| 4516 | semaphore_2 = OS::CreateSemaphore(0); |
| 4517 | } |
| 4518 | |
| 4519 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4520 | // 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] | 4521 | bool IsBreakEventMessage(char *message) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4522 | const char* type_event = "\"type\":\"event\""; |
| 4523 | const char* event_break = "\"event\":\"break\""; |
| 4524 | // Does the message contain both type:event and event:break? |
| 4525 | return strstr(message, type_event) != NULL && |
| 4526 | strstr(message, event_break) != NULL; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4530 | // We match parts of the message to decide if it is a exception message. |
| 4531 | bool IsExceptionEventMessage(char *message) { |
| 4532 | const char* type_event = "\"type\":\"event\""; |
| 4533 | const char* event_exception = "\"event\":\"exception\""; |
| 4534 | // Does the message contain both type:event and event:exception? |
| 4535 | return strstr(message, type_event) != NULL && |
| 4536 | strstr(message, event_exception) != NULL; |
| 4537 | } |
| 4538 | |
| 4539 | |
| 4540 | // We match the message wether it is an evaluate response message. |
| 4541 | bool IsEvaluateResponseMessage(char* message) { |
| 4542 | const char* type_response = "\"type\":\"response\""; |
| 4543 | const char* command_evaluate = "\"command\":\"evaluate\""; |
| 4544 | // Does the message contain both type:response and command:evaluate? |
| 4545 | return strstr(message, type_response) != NULL && |
| 4546 | strstr(message, command_evaluate) != NULL; |
| 4547 | } |
| 4548 | |
| 4549 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4550 | static int StringToInt(const char* s) { |
| 4551 | return atoi(s); // NOLINT |
| 4552 | } |
| 4553 | |
| 4554 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4555 | // We match parts of the message to get evaluate result int value. |
| 4556 | int GetEvaluateIntResult(char *message) { |
| 4557 | const char* value = "\"value\":"; |
| 4558 | char* pos = strstr(message, value); |
| 4559 | if (pos == NULL) { |
| 4560 | return -1; |
| 4561 | } |
| 4562 | int res = -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4563 | res = StringToInt(pos + strlen(value)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4564 | return res; |
| 4565 | } |
| 4566 | |
| 4567 | |
| 4568 | // We match parts of the message to get hit breakpoint id. |
| 4569 | int GetBreakpointIdFromBreakEventMessage(char *message) { |
| 4570 | const char* breakpoints = "\"breakpoints\":["; |
| 4571 | char* pos = strstr(message, breakpoints); |
| 4572 | if (pos == NULL) { |
| 4573 | return -1; |
| 4574 | } |
| 4575 | int res = -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4576 | res = StringToInt(pos + strlen(breakpoints)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4577 | return res; |
| 4578 | } |
| 4579 | |
| 4580 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4581 | // We match parts of the message to get total frames number. |
| 4582 | int GetTotalFramesInt(char *message) { |
| 4583 | const char* prefix = "\"totalFrames\":"; |
| 4584 | char* pos = strstr(message, prefix); |
| 4585 | if (pos == NULL) { |
| 4586 | return -1; |
| 4587 | } |
| 4588 | pos += strlen(prefix); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4589 | int res = StringToInt(pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4590 | return res; |
| 4591 | } |
| 4592 | |
| 4593 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4594 | /* Test MessageQueues */ |
| 4595 | /* Tests the message queues that hold debugger commands and |
| 4596 | * response messages to the debugger. Fills queues and makes |
| 4597 | * them grow. |
| 4598 | */ |
| 4599 | Barriers message_queue_barriers; |
| 4600 | |
| 4601 | // This is the debugger thread, that executes no v8 calls except |
| 4602 | // placing JSON debugger commands in the queue. |
| 4603 | class MessageQueueDebuggerThread : public v8::internal::Thread { |
| 4604 | public: |
| 4605 | void Run(); |
| 4606 | }; |
| 4607 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4608 | static void MessageHandler(const uint16_t* message, int length, |
| 4609 | v8::Debug::ClientData* client_data) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4610 | static char print_buffer[1000]; |
| 4611 | Utf16ToAscii(message, length, print_buffer); |
| 4612 | if (IsBreakEventMessage(print_buffer)) { |
| 4613 | // Lets test script wait until break occurs to send commands. |
| 4614 | // Signals when a break is reported. |
| 4615 | message_queue_barriers.semaphore_2->Signal(); |
| 4616 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4617 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4618 | // Allow message handler to block on a semaphore, to test queueing of |
| 4619 | // messages while blocked. |
| 4620 | message_queue_barriers.semaphore_1->Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4623 | void MessageQueueDebuggerThread::Run() { |
| 4624 | const int kBufferSize = 1000; |
| 4625 | uint16_t buffer_1[kBufferSize]; |
| 4626 | uint16_t buffer_2[kBufferSize]; |
| 4627 | const char* command_1 = |
| 4628 | "{\"seq\":117," |
| 4629 | "\"type\":\"request\"," |
| 4630 | "\"command\":\"evaluate\"," |
| 4631 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 4632 | const char* command_2 = |
| 4633 | "{\"seq\":118," |
| 4634 | "\"type\":\"request\"," |
| 4635 | "\"command\":\"evaluate\"," |
| 4636 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 4637 | const char* command_3 = |
| 4638 | "{\"seq\":119," |
| 4639 | "\"type\":\"request\"," |
| 4640 | "\"command\":\"evaluate\"," |
| 4641 | "\"arguments\":{\"expression\":\"c.d * b\"}}"; |
| 4642 | const char* command_continue = |
| 4643 | "{\"seq\":106," |
| 4644 | "\"type\":\"request\"," |
| 4645 | "\"command\":\"continue\"}"; |
| 4646 | const char* command_single_step = |
| 4647 | "{\"seq\":107," |
| 4648 | "\"type\":\"request\"," |
| 4649 | "\"command\":\"continue\"," |
| 4650 | "\"arguments\":{\"stepaction\":\"next\"}}"; |
| 4651 | |
| 4652 | /* Interleaved sequence of actions by the two threads:*/ |
| 4653 | // Main thread compiles and runs source_1 |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4654 | message_queue_barriers.semaphore_1->Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4655 | message_queue_barriers.barrier_1.Wait(); |
| 4656 | // Post 6 commands, filling the command queue and making it expand. |
| 4657 | // These calls return immediately, but the commands stay on the queue |
| 4658 | // until the execution of source_2. |
| 4659 | // Note: AsciiToUtf16 executes before SendCommand, so command is copied |
| 4660 | // to buffer before buffer is sent to SendCommand. |
| 4661 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 4662 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 4663 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4664 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); |
| 4665 | 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] | 4666 | message_queue_barriers.barrier_2.Wait(); |
| 4667 | // Main thread compiles and runs source_2. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4668 | // Queued commands are executed at the start of compilation of source_2( |
| 4669 | // beforeCompile event). |
| 4670 | // Free the message handler to process all the messages from the queue. 7 |
| 4671 | // messages are expected: 2 afterCompile events and 5 responses. |
| 4672 | // All the commands added so far will fail to execute as long as call stack |
| 4673 | // is empty on beforeCompile event. |
| 4674 | for (int i = 0; i < 6 ; ++i) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4675 | message_queue_barriers.semaphore_1->Signal(); |
| 4676 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4677 | message_queue_barriers.barrier_3.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4678 | // Main thread compiles and runs source_3. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4679 | // Don't stop in the afterCompile handler. |
| 4680 | message_queue_barriers.semaphore_1->Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4681 | // source_3 includes a debugger statement, which causes a break event. |
| 4682 | // Wait on break event from hitting "debugger" statement |
| 4683 | message_queue_barriers.semaphore_2->Wait(); |
| 4684 | // These should execute after the "debugger" statement in source_2 |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4685 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); |
| 4686 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); |
| 4687 | 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] | 4688 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2)); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4689 | // Run after 2 break events, 4 responses. |
| 4690 | for (int i = 0; i < 6 ; ++i) { |
| 4691 | message_queue_barriers.semaphore_1->Signal(); |
| 4692 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4693 | // Wait on break event after a single step executes. |
| 4694 | message_queue_barriers.semaphore_2->Wait(); |
| 4695 | v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1)); |
| 4696 | v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2)); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4697 | // Run after 2 responses. |
| 4698 | for (int i = 0; i < 2 ; ++i) { |
| 4699 | message_queue_barriers.semaphore_1->Signal(); |
| 4700 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4701 | // Main thread continues running source_3 to end, waits for this thread. |
| 4702 | } |
| 4703 | |
| 4704 | MessageQueueDebuggerThread message_queue_debugger_thread; |
| 4705 | |
| 4706 | // This thread runs the v8 engine. |
| 4707 | TEST(MessageQueues) { |
| 4708 | // Create a V8 environment |
| 4709 | v8::HandleScope scope; |
| 4710 | DebugLocalContext env; |
| 4711 | message_queue_barriers.Initialize(); |
| 4712 | v8::Debug::SetMessageHandler(MessageHandler); |
| 4713 | message_queue_debugger_thread.Start(); |
| 4714 | |
| 4715 | const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; |
| 4716 | const char* source_2 = "e = 17;"; |
| 4717 | const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; |
| 4718 | |
| 4719 | // See MessageQueueDebuggerThread::Run for interleaved sequence of |
| 4720 | // API calls and events in the two threads. |
| 4721 | CompileRun(source_1); |
| 4722 | message_queue_barriers.barrier_1.Wait(); |
| 4723 | message_queue_barriers.barrier_2.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4724 | CompileRun(source_2); |
| 4725 | message_queue_barriers.barrier_3.Wait(); |
| 4726 | CompileRun(source_3); |
| 4727 | message_queue_debugger_thread.Join(); |
| 4728 | fflush(stdout); |
| 4729 | } |
| 4730 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4731 | |
| 4732 | class TestClientData : public v8::Debug::ClientData { |
| 4733 | public: |
| 4734 | TestClientData() { |
| 4735 | constructor_call_counter++; |
| 4736 | } |
| 4737 | virtual ~TestClientData() { |
| 4738 | destructor_call_counter++; |
| 4739 | } |
| 4740 | |
| 4741 | static void ResetCounters() { |
| 4742 | constructor_call_counter = 0; |
| 4743 | destructor_call_counter = 0; |
| 4744 | } |
| 4745 | |
| 4746 | static int constructor_call_counter; |
| 4747 | static int destructor_call_counter; |
| 4748 | }; |
| 4749 | |
| 4750 | int TestClientData::constructor_call_counter = 0; |
| 4751 | int TestClientData::destructor_call_counter = 0; |
| 4752 | |
| 4753 | |
| 4754 | // Tests that MessageQueue doesn't destroy client data when expands and |
| 4755 | // does destroy when it dies. |
| 4756 | TEST(MessageQueueExpandAndDestroy) { |
| 4757 | TestClientData::ResetCounters(); |
| 4758 | { // Create a scope for the queue. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4759 | CommandMessageQueue queue(1); |
| 4760 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4761 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4762 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4763 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4764 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4765 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4766 | CHECK_EQ(0, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4767 | queue.Get().Dispose(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4768 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
| 4769 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4770 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4771 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4772 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4773 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4774 | new TestClientData())); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4775 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 4776 | new TestClientData())); |
| 4777 | queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), |
| 4778 | new TestClientData())); |
| 4779 | CHECK_EQ(1, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4780 | queue.Get().Dispose(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4781 | CHECK_EQ(2, TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4782 | } |
| 4783 | // 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] | 4784 | CHECK_EQ(TestClientData::destructor_call_counter, |
| 4785 | TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | |
| 4789 | static int handled_client_data_instances_count = 0; |
| 4790 | static void MessageHandlerCountingClientData( |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4791 | const v8::Debug::Message& message) { |
| 4792 | if (message.GetClientData() != NULL) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4793 | handled_client_data_instances_count++; |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | |
| 4798 | // Tests that all client data passed to the debugger are sent to the handler. |
| 4799 | TEST(SendClientDataToHandler) { |
| 4800 | // Create a V8 environment |
| 4801 | v8::HandleScope scope; |
| 4802 | DebugLocalContext env; |
| 4803 | TestClientData::ResetCounters(); |
| 4804 | handled_client_data_instances_count = 0; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4805 | v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData); |
| 4806 | 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] | 4807 | const int kBufferSize = 1000; |
| 4808 | uint16_t buffer[kBufferSize]; |
| 4809 | const char* command_1 = |
| 4810 | "{\"seq\":117," |
| 4811 | "\"type\":\"request\"," |
| 4812 | "\"command\":\"evaluate\"," |
| 4813 | "\"arguments\":{\"expression\":\"1+2\"}}"; |
| 4814 | const char* command_2 = |
| 4815 | "{\"seq\":118," |
| 4816 | "\"type\":\"request\"," |
| 4817 | "\"command\":\"evaluate\"," |
| 4818 | "\"arguments\":{\"expression\":\"1+a\"}}"; |
| 4819 | const char* command_continue = |
| 4820 | "{\"seq\":106," |
| 4821 | "\"type\":\"request\"," |
| 4822 | "\"command\":\"continue\"}"; |
| 4823 | |
| 4824 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer), |
| 4825 | new TestClientData()); |
| 4826 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL); |
| 4827 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), |
| 4828 | new TestClientData()); |
| 4829 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), |
| 4830 | new TestClientData()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4831 | // All the messages will be processed on beforeCompile event. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4832 | CompileRun(source_1); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4833 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4834 | CHECK_EQ(3, TestClientData::constructor_call_counter); |
| 4835 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 4836 | handled_client_data_instances_count); |
| 4837 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 4838 | TestClientData::destructor_call_counter); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4842 | /* Test ThreadedDebugging */ |
| 4843 | /* This test interrupts a running infinite loop that is |
| 4844 | * occupying the v8 thread by a break command from the |
| 4845 | * debugger thread. It then changes the value of a |
| 4846 | * global object, to make the loop terminate. |
| 4847 | */ |
| 4848 | |
| 4849 | Barriers threaded_debugging_barriers; |
| 4850 | |
| 4851 | class V8Thread : public v8::internal::Thread { |
| 4852 | public: |
| 4853 | void Run(); |
| 4854 | }; |
| 4855 | |
| 4856 | class DebuggerThread : public v8::internal::Thread { |
| 4857 | public: |
| 4858 | void Run(); |
| 4859 | }; |
| 4860 | |
| 4861 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4862 | static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) { |
| 4863 | threaded_debugging_barriers.barrier_1.Wait(); |
| 4864 | return v8::Undefined(); |
| 4865 | } |
| 4866 | |
| 4867 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4868 | static void ThreadedMessageHandler(const v8::Debug::Message& message) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4869 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4870 | v8::String::Value json(message.GetJSON()); |
| 4871 | Utf16ToAscii(*json, json.length(), print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4872 | if (IsBreakEventMessage(print_buffer)) { |
| 4873 | threaded_debugging_barriers.barrier_2.Wait(); |
| 4874 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | |
| 4878 | void V8Thread::Run() { |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4879 | const char* source = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4880 | "flag = true;\n" |
| 4881 | "function bar( new_value ) {\n" |
| 4882 | " flag = new_value;\n" |
| 4883 | " return \"Return from bar(\" + new_value + \")\";\n" |
| 4884 | "}\n" |
| 4885 | "\n" |
| 4886 | "function foo() {\n" |
| 4887 | " var x = 1;\n" |
| 4888 | " while ( flag == true ) {\n" |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4889 | " if ( x == 1 ) {\n" |
| 4890 | " ThreadedAtBarrier1();\n" |
| 4891 | " }\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4892 | " x = x + 1;\n" |
| 4893 | " }\n" |
| 4894 | "}\n" |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4895 | "\n" |
| 4896 | "foo();\n"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4897 | |
| 4898 | v8::HandleScope scope; |
| 4899 | DebugLocalContext env; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4900 | v8::Debug::SetMessageHandler2(&ThreadedMessageHandler); |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4901 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 4902 | global_template->Set(v8::String::New("ThreadedAtBarrier1"), |
| 4903 | v8::FunctionTemplate::New(ThreadedAtBarrier1)); |
| 4904 | v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); |
| 4905 | v8::Context::Scope context_scope(context); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4906 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 4907 | CompileRun(source); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | void DebuggerThread::Run() { |
| 4911 | const int kBufSize = 1000; |
| 4912 | uint16_t buffer[kBufSize]; |
| 4913 | |
| 4914 | const char* command_1 = "{\"seq\":102," |
| 4915 | "\"type\":\"request\"," |
| 4916 | "\"command\":\"evaluate\"," |
| 4917 | "\"arguments\":{\"expression\":\"bar(false)\"}}"; |
| 4918 | const char* command_2 = "{\"seq\":103," |
| 4919 | "\"type\":\"request\"," |
| 4920 | "\"command\":\"continue\"}"; |
| 4921 | |
| 4922 | threaded_debugging_barriers.barrier_1.Wait(); |
| 4923 | v8::Debug::DebugBreak(); |
| 4924 | threaded_debugging_barriers.barrier_2.Wait(); |
| 4925 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
| 4926 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
| 4927 | } |
| 4928 | |
| 4929 | DebuggerThread debugger_thread; |
| 4930 | V8Thread v8_thread; |
| 4931 | |
| 4932 | TEST(ThreadedDebugging) { |
| 4933 | // Create a V8 environment |
| 4934 | threaded_debugging_barriers.Initialize(); |
| 4935 | |
| 4936 | v8_thread.Start(); |
| 4937 | debugger_thread.Start(); |
| 4938 | |
| 4939 | v8_thread.Join(); |
| 4940 | debugger_thread.Join(); |
| 4941 | } |
| 4942 | |
| 4943 | /* Test RecursiveBreakpoints */ |
| 4944 | /* In this test, the debugger evaluates a function with a breakpoint, after |
| 4945 | * hitting a breakpoint in another function. We do this with both values |
| 4946 | * of the flag enabling recursive breakpoints, and verify that the second |
| 4947 | * breakpoint is hit when enabled, and missed when disabled. |
| 4948 | */ |
| 4949 | |
| 4950 | class BreakpointsV8Thread : public v8::internal::Thread { |
| 4951 | public: |
| 4952 | void Run(); |
| 4953 | }; |
| 4954 | |
| 4955 | class BreakpointsDebuggerThread : public v8::internal::Thread { |
| 4956 | public: |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4957 | explicit BreakpointsDebuggerThread(bool global_evaluate) |
| 4958 | : global_evaluate_(global_evaluate) {} |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4959 | void Run(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4960 | |
| 4961 | private: |
| 4962 | bool global_evaluate_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4963 | }; |
| 4964 | |
| 4965 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4966 | Barriers* breakpoints_barriers; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4967 | int break_event_breakpoint_id; |
| 4968 | int evaluate_int_result; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4969 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4970 | static void BreakpointsMessageHandler(const v8::Debug::Message& message) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4971 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 4972 | v8::String::Value json(message.GetJSON()); |
| 4973 | Utf16ToAscii(*json, json.length(), print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4974 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4975 | if (IsBreakEventMessage(print_buffer)) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4976 | break_event_breakpoint_id = |
| 4977 | GetBreakpointIdFromBreakEventMessage(print_buffer); |
| 4978 | breakpoints_barriers->semaphore_1->Signal(); |
| 4979 | } else if (IsEvaluateResponseMessage(print_buffer)) { |
| 4980 | evaluate_int_result = GetEvaluateIntResult(print_buffer); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4981 | breakpoints_barriers->semaphore_1->Signal(); |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | |
| 4986 | void BreakpointsV8Thread::Run() { |
| 4987 | const char* source_1 = "var y_global = 3;\n" |
| 4988 | "function cat( new_value ) {\n" |
| 4989 | " var x = new_value;\n" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4990 | " y_global = y_global + 4;\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4991 | " x = 3 * x + 1;\n" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4992 | " y_global = y_global + 5;\n" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4993 | " return x;\n" |
| 4994 | "}\n" |
| 4995 | "\n" |
| 4996 | "function dog() {\n" |
| 4997 | " var x = 1;\n" |
| 4998 | " x = y_global;" |
| 4999 | " var z = 3;" |
| 5000 | " x += 100;\n" |
| 5001 | " return x;\n" |
| 5002 | "}\n" |
| 5003 | "\n"; |
| 5004 | const char* source_2 = "cat(17);\n" |
| 5005 | "cat(19);\n"; |
| 5006 | |
| 5007 | v8::HandleScope scope; |
| 5008 | DebugLocalContext env; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5009 | v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5010 | |
| 5011 | CompileRun(source_1); |
| 5012 | breakpoints_barriers->barrier_1.Wait(); |
| 5013 | breakpoints_barriers->barrier_2.Wait(); |
| 5014 | CompileRun(source_2); |
| 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | void BreakpointsDebuggerThread::Run() { |
| 5019 | const int kBufSize = 1000; |
| 5020 | uint16_t buffer[kBufSize]; |
| 5021 | |
| 5022 | const char* command_1 = "{\"seq\":101," |
| 5023 | "\"type\":\"request\"," |
| 5024 | "\"command\":\"setbreakpoint\"," |
| 5025 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5026 | const char* command_2 = "{\"seq\":102," |
| 5027 | "\"type\":\"request\"," |
| 5028 | "\"command\":\"setbreakpoint\"," |
| 5029 | "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5030 | const char* command_3; |
| 5031 | if (this->global_evaluate_) { |
| 5032 | command_3 = "{\"seq\":103," |
| 5033 | "\"type\":\"request\"," |
| 5034 | "\"command\":\"evaluate\"," |
| 5035 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," |
| 5036 | "\"global\":true}}"; |
| 5037 | } else { |
| 5038 | command_3 = "{\"seq\":103," |
| 5039 | "\"type\":\"request\"," |
| 5040 | "\"command\":\"evaluate\"," |
| 5041 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; |
| 5042 | } |
| 5043 | const char* command_4; |
| 5044 | if (this->global_evaluate_) { |
| 5045 | command_4 = "{\"seq\":104," |
| 5046 | "\"type\":\"request\"," |
| 5047 | "\"command\":\"evaluate\"," |
| 5048 | "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," |
| 5049 | "\"global\":true}}"; |
| 5050 | } else { |
| 5051 | command_4 = "{\"seq\":104," |
| 5052 | "\"type\":\"request\"," |
| 5053 | "\"command\":\"evaluate\"," |
| 5054 | "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; |
| 5055 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5056 | const char* command_5 = "{\"seq\":105," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5057 | "\"type\":\"request\"," |
| 5058 | "\"command\":\"continue\"}"; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5059 | const char* command_6 = "{\"seq\":106," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5060 | "\"type\":\"request\"," |
| 5061 | "\"command\":\"continue\"}"; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5062 | const char* command_7; |
| 5063 | if (this->global_evaluate_) { |
| 5064 | command_7 = "{\"seq\":107," |
| 5065 | "\"type\":\"request\"," |
| 5066 | "\"command\":\"evaluate\"," |
| 5067 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," |
| 5068 | "\"global\":true}}"; |
| 5069 | } else { |
| 5070 | command_7 = "{\"seq\":107," |
| 5071 | "\"type\":\"request\"," |
| 5072 | "\"command\":\"evaluate\"," |
| 5073 | "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; |
| 5074 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5075 | const char* command_8 = "{\"seq\":108," |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5076 | "\"type\":\"request\"," |
| 5077 | "\"command\":\"continue\"}"; |
| 5078 | |
| 5079 | |
| 5080 | // v8 thread initializes, runs source_1 |
| 5081 | breakpoints_barriers->barrier_1.Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5082 | // 1:Set breakpoint in cat() (will get id 1). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5083 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5084 | // 2:Set breakpoint in dog() (will get id 2). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5085 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5086 | breakpoints_barriers->barrier_2.Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5087 | // V8 thread starts compiling source_2. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5088 | // Automatic break happens, to run queued commands |
| 5089 | // breakpoints_barriers->semaphore_1->Wait(); |
| 5090 | // Commands 1 through 3 run, thread continues. |
| 5091 | // v8 thread runs source_2 to breakpoint in cat(). |
| 5092 | // message callback receives break event. |
| 5093 | breakpoints_barriers->semaphore_1->Wait(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5094 | // Must have hit breakpoint #1. |
| 5095 | CHECK_EQ(1, break_event_breakpoint_id); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5096 | // 4:Evaluate dog() (which has a breakpoint). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5097 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5098 | // V8 thread hits breakpoint in dog(). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5099 | breakpoints_barriers->semaphore_1->Wait(); // wait for break event |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5100 | // Must have hit breakpoint #2. |
| 5101 | CHECK_EQ(2, break_event_breakpoint_id); |
| 5102 | // 5:Evaluate (x + 1). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5103 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5104 | // Evaluate (x + 1) finishes. |
| 5105 | breakpoints_barriers->semaphore_1->Wait(); |
| 5106 | // Must have result 108. |
| 5107 | CHECK_EQ(108, evaluate_int_result); |
| 5108 | // 6:Continue evaluation of dog(). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5109 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5110 | // Evaluate dog() finishes. |
| 5111 | breakpoints_barriers->semaphore_1->Wait(); |
| 5112 | // Must have result 107. |
| 5113 | CHECK_EQ(107, evaluate_int_result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5114 | // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint |
| 5115 | // in cat(19). |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5116 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5117 | // Message callback gets break event. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5118 | breakpoints_barriers->semaphore_1->Wait(); // wait for break event |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5119 | // Must have hit breakpoint #1. |
| 5120 | CHECK_EQ(1, break_event_breakpoint_id); |
| 5121 | // 8: Evaluate dog() with breaks disabled. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5122 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5123 | // Evaluate dog() finishes. |
| 5124 | breakpoints_barriers->semaphore_1->Wait(); |
| 5125 | // Must have result 116. |
| 5126 | CHECK_EQ(116, evaluate_int_result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5127 | // 9: Continue evaluation of source2, reach end. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5128 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5131 | void TestRecursiveBreakpointsGeneric(bool global_evaluate) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5132 | i::FLAG_debugger_auto_break = true; |
| 5133 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5134 | BreakpointsDebuggerThread breakpoints_debugger_thread(global_evaluate); |
| 5135 | BreakpointsV8Thread breakpoints_v8_thread; |
| 5136 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5137 | // Create a V8 environment |
| 5138 | Barriers stack_allocated_breakpoints_barriers; |
| 5139 | stack_allocated_breakpoints_barriers.Initialize(); |
| 5140 | breakpoints_barriers = &stack_allocated_breakpoints_barriers; |
| 5141 | |
| 5142 | breakpoints_v8_thread.Start(); |
| 5143 | breakpoints_debugger_thread.Start(); |
| 5144 | |
| 5145 | breakpoints_v8_thread.Join(); |
| 5146 | breakpoints_debugger_thread.Join(); |
| 5147 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5148 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5149 | TEST(RecursiveBreakpoints) { |
| 5150 | TestRecursiveBreakpointsGeneric(false); |
| 5151 | } |
| 5152 | |
| 5153 | TEST(RecursiveBreakpointsGlobal) { |
| 5154 | TestRecursiveBreakpointsGeneric(true); |
| 5155 | } |
| 5156 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5157 | |
| 5158 | static void DummyDebugEventListener(v8::DebugEvent event, |
| 5159 | v8::Handle<v8::Object> exec_state, |
| 5160 | v8::Handle<v8::Object> event_data, |
| 5161 | v8::Handle<v8::Value> data) { |
| 5162 | } |
| 5163 | |
| 5164 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5165 | TEST(SetDebugEventListenerOnUninitializedVM) { |
| 5166 | v8::Debug::SetDebugEventListener(DummyDebugEventListener); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5170 | static void DummyMessageHandler(const v8::Debug::Message& message) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | |
| 5174 | TEST(SetMessageHandlerOnUninitializedVM) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5175 | v8::Debug::SetMessageHandler2(DummyMessageHandler); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | |
| 5179 | TEST(DebugBreakOnUninitializedVM) { |
| 5180 | v8::Debug::DebugBreak(); |
| 5181 | } |
| 5182 | |
| 5183 | |
| 5184 | TEST(SendCommandToUninitializedVM) { |
| 5185 | const char* dummy_command = "{}"; |
| 5186 | uint16_t dummy_buffer[80]; |
| 5187 | int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer); |
| 5188 | v8::Debug::SendCommand(dummy_buffer, dummy_length); |
| 5189 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5190 | |
| 5191 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5192 | // Source for a JavaScript function which returns the data parameter of a |
| 5193 | // function called in the context of the debugger. If no data parameter is |
| 5194 | // passed it throws an exception. |
| 5195 | static const char* debugger_call_with_data_source = |
| 5196 | "function debugger_call_with_data(exec_state, data) {" |
| 5197 | " if (data) return data;" |
| 5198 | " throw 'No data!'" |
| 5199 | "}"; |
| 5200 | v8::Handle<v8::Function> debugger_call_with_data; |
| 5201 | |
| 5202 | |
| 5203 | // Source for a JavaScript function which returns the data parameter of a |
| 5204 | // function called in the context of the debugger. If no data parameter is |
| 5205 | // passed it throws an exception. |
| 5206 | static const char* debugger_call_with_closure_source = |
| 5207 | "var x = 3;" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5208 | "(function (exec_state) {" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5209 | " if (exec_state.y) return x - 1;" |
| 5210 | " exec_state.y = x;" |
| 5211 | " return exec_state.y" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5212 | "})"; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5213 | v8::Handle<v8::Function> debugger_call_with_closure; |
| 5214 | |
| 5215 | // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 5216 | // in the debugger. |
| 5217 | static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) { |
| 5218 | CHECK(v8::Debug::Call(frame_count)->IsNumber()); |
| 5219 | CHECK_EQ(args[0]->Int32Value(), |
| 5220 | v8::Debug::Call(frame_count)->Int32Value()); |
| 5221 | return v8::Undefined(); |
| 5222 | } |
| 5223 | |
| 5224 | |
| 5225 | // Function to retrieve the source line of the top JavaScript frame by calling a |
| 5226 | // JavaScript function in the debugger. |
| 5227 | static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) { |
| 5228 | CHECK(v8::Debug::Call(frame_source_line)->IsNumber()); |
| 5229 | CHECK_EQ(args[0]->Int32Value(), |
| 5230 | v8::Debug::Call(frame_source_line)->Int32Value()); |
| 5231 | return v8::Undefined(); |
| 5232 | } |
| 5233 | |
| 5234 | |
| 5235 | // Function to test passing an additional parameter to a JavaScript function |
| 5236 | // called in the debugger. It also tests that functions called in the debugger |
| 5237 | // can throw exceptions. |
| 5238 | static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) { |
| 5239 | v8::Handle<v8::String> data = v8::String::New("Test"); |
| 5240 | CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString()); |
| 5241 | |
| 5242 | CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 5243 | CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 5244 | |
| 5245 | v8::TryCatch catcher; |
| 5246 | v8::Debug::Call(debugger_call_with_data); |
| 5247 | CHECK(catcher.HasCaught()); |
| 5248 | CHECK(catcher.Exception()->IsString()); |
| 5249 | |
| 5250 | return v8::Undefined(); |
| 5251 | } |
| 5252 | |
| 5253 | |
| 5254 | // Function to test using a JavaScript with closure in the debugger. |
| 5255 | static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) { |
| 5256 | CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber()); |
| 5257 | CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value()); |
| 5258 | return v8::Undefined(); |
| 5259 | } |
| 5260 | |
| 5261 | |
| 5262 | // Test functions called through the debugger. |
| 5263 | TEST(CallFunctionInDebugger) { |
| 5264 | // Create and enter a context with the functions CheckFrameCount, |
| 5265 | // CheckSourceLine and CheckDataParameter installed. |
| 5266 | v8::HandleScope scope; |
| 5267 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 5268 | global_template->Set(v8::String::New("CheckFrameCount"), |
| 5269 | v8::FunctionTemplate::New(CheckFrameCount)); |
| 5270 | global_template->Set(v8::String::New("CheckSourceLine"), |
| 5271 | v8::FunctionTemplate::New(CheckSourceLine)); |
| 5272 | global_template->Set(v8::String::New("CheckDataParameter"), |
| 5273 | v8::FunctionTemplate::New(CheckDataParameter)); |
| 5274 | global_template->Set(v8::String::New("CheckClosure"), |
| 5275 | v8::FunctionTemplate::New(CheckClosure)); |
| 5276 | v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); |
| 5277 | v8::Context::Scope context_scope(context); |
| 5278 | |
| 5279 | // Compile a function for checking the number of JavaScript frames. |
| 5280 | v8::Script::Compile(v8::String::New(frame_count_source))->Run(); |
| 5281 | frame_count = v8::Local<v8::Function>::Cast( |
| 5282 | context->Global()->Get(v8::String::New("frame_count"))); |
| 5283 | |
| 5284 | // Compile a function for returning the source line for the top frame. |
| 5285 | v8::Script::Compile(v8::String::New(frame_source_line_source))->Run(); |
| 5286 | frame_source_line = v8::Local<v8::Function>::Cast( |
| 5287 | context->Global()->Get(v8::String::New("frame_source_line"))); |
| 5288 | |
| 5289 | // Compile a function returning the data parameter. |
| 5290 | v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run(); |
| 5291 | debugger_call_with_data = v8::Local<v8::Function>::Cast( |
| 5292 | context->Global()->Get(v8::String::New("debugger_call_with_data"))); |
| 5293 | |
| 5294 | // Compile a function capturing closure. |
| 5295 | debugger_call_with_closure = v8::Local<v8::Function>::Cast( |
| 5296 | v8::Script::Compile( |
| 5297 | v8::String::New(debugger_call_with_closure_source))->Run()); |
| 5298 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 5299 | // Calling a function through the debugger returns 0 frames if there are |
| 5300 | // no JavaScript frames. |
| 5301 | CHECK_EQ(v8::Integer::New(0), v8::Debug::Call(frame_count)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5302 | |
| 5303 | // Test that the number of frames can be retrieved. |
| 5304 | v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run(); |
| 5305 | v8::Script::Compile(v8::String::New("function f() {" |
| 5306 | " CheckFrameCount(2);" |
| 5307 | "}; f()"))->Run(); |
| 5308 | |
| 5309 | // Test that the source line can be retrieved. |
| 5310 | v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run(); |
| 5311 | v8::Script::Compile(v8::String::New("function f() {\n" |
| 5312 | " CheckSourceLine(1)\n" |
| 5313 | " CheckSourceLine(2)\n" |
| 5314 | " CheckSourceLine(3)\n" |
| 5315 | "}; f()"))->Run(); |
| 5316 | |
| 5317 | // Test that a parameter can be passed to a function called in the debugger. |
| 5318 | v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run(); |
| 5319 | |
| 5320 | // Test that a function with closure can be run in the debugger. |
| 5321 | v8::Script::Compile(v8::String::New("CheckClosure()"))->Run(); |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 5322 | |
| 5323 | |
| 5324 | // Test that the source line is correct when there is a line offset. |
| 5325 | v8::ScriptOrigin origin(v8::String::New("test"), |
| 5326 | v8::Integer::New(7)); |
| 5327 | v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run(); |
| 5328 | v8::Script::Compile(v8::String::New("function f() {\n" |
| 5329 | " CheckSourceLine(8)\n" |
| 5330 | " CheckSourceLine(9)\n" |
| 5331 | " CheckSourceLine(10)\n" |
| 5332 | "}; f()"), &origin)->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5333 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5334 | |
| 5335 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5336 | // Debugger message handler which counts the number of breaks. |
| 5337 | static void SendContinueCommand(); |
| 5338 | static void MessageHandlerBreakPointHitCount( |
| 5339 | const v8::Debug::Message& message) { |
| 5340 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 5341 | // Count the number of breaks. |
| 5342 | break_point_hit_count++; |
| 5343 | |
| 5344 | SendContinueCommand(); |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5349 | // Test that clearing the debug event listener actually clears all break points |
| 5350 | // and related information. |
| 5351 | TEST(DebuggerUnload) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5352 | DebugLocalContext env; |
| 5353 | |
| 5354 | // Check debugger is unloaded before it is used. |
| 5355 | CheckDebuggerUnloaded(); |
| 5356 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5357 | // Set a debug event listener. |
| 5358 | break_point_hit_count = 0; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5359 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 5360 | v8::Undefined()); |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5361 | { |
| 5362 | v8::HandleScope scope; |
| 5363 | // Create a couple of functions for the test. |
| 5364 | v8::Local<v8::Function> foo = |
| 5365 | CompileFunction(&env, "function foo(){x=1}", "foo"); |
| 5366 | v8::Local<v8::Function> bar = |
| 5367 | CompileFunction(&env, "function bar(){y=2}", "bar"); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5368 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5369 | // Set some break points. |
| 5370 | SetBreakPoint(foo, 0); |
| 5371 | SetBreakPoint(foo, 4); |
| 5372 | SetBreakPoint(bar, 0); |
| 5373 | SetBreakPoint(bar, 4); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5374 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5375 | // Make sure that the break points are there. |
| 5376 | break_point_hit_count = 0; |
| 5377 | foo->Call(env->Global(), 0, NULL); |
| 5378 | CHECK_EQ(2, break_point_hit_count); |
| 5379 | bar->Call(env->Global(), 0, NULL); |
| 5380 | CHECK_EQ(4, break_point_hit_count); |
| 5381 | } |
sgjesse@chromium.org | f457d1f | 2009-06-17 14:18:36 +0000 | [diff] [blame] | 5382 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5383 | // Remove the debug event listener without clearing breakpoints. Do this |
| 5384 | // outside a handle scope. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5385 | v8::Debug::SetDebugEventListener(NULL); |
| 5386 | CheckDebuggerUnloaded(true); |
| 5387 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5388 | // Now set a debug message handler. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5389 | break_point_hit_count = 0; |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5390 | v8::Debug::SetMessageHandler2(MessageHandlerBreakPointHitCount); |
| 5391 | { |
| 5392 | v8::HandleScope scope; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5393 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5394 | // Get the test functions again. |
| 5395 | v8::Local<v8::Function> foo = |
| 5396 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
| 5397 | v8::Local<v8::Function> bar = |
| 5398 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5399 | |
sgjesse@chromium.org | a9eaf5c | 2009-06-17 14:04:55 +0000 | [diff] [blame] | 5400 | foo->Call(env->Global(), 0, NULL); |
| 5401 | CHECK_EQ(0, break_point_hit_count); |
| 5402 | |
| 5403 | // Set break points and run again. |
| 5404 | SetBreakPoint(foo, 0); |
| 5405 | SetBreakPoint(foo, 4); |
| 5406 | foo->Call(env->Global(), 0, NULL); |
| 5407 | CHECK_EQ(2, break_point_hit_count); |
| 5408 | } |
| 5409 | |
| 5410 | // Remove the debug message handler without clearing breakpoints. Do this |
| 5411 | // outside a handle scope. |
| 5412 | v8::Debug::SetMessageHandler2(NULL); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5413 | CheckDebuggerUnloaded(true); |
| 5414 | } |
| 5415 | |
| 5416 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5417 | // Sends continue command to the debugger. |
| 5418 | static void SendContinueCommand() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5419 | const int kBufferSize = 1000; |
| 5420 | uint16_t buffer[kBufferSize]; |
| 5421 | const char* command_continue = |
| 5422 | "{\"seq\":0," |
| 5423 | "\"type\":\"request\"," |
| 5424 | "\"command\":\"continue\"}"; |
| 5425 | |
| 5426 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); |
| 5427 | } |
| 5428 | |
| 5429 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5430 | // Debugger message handler which counts the number of times it is called. |
| 5431 | static int message_handler_hit_count = 0; |
| 5432 | static void MessageHandlerHitCount(const v8::Debug::Message& message) { |
| 5433 | message_handler_hit_count++; |
| 5434 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5435 | static char print_buffer[1000]; |
| 5436 | v8::String::Value json(message.GetJSON()); |
| 5437 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5438 | if (IsExceptionEventMessage(print_buffer)) { |
| 5439 | // Send a continue command for exception events. |
| 5440 | SendContinueCommand(); |
| 5441 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5445 | // Test clearing the debug message handler. |
| 5446 | TEST(DebuggerClearMessageHandler) { |
| 5447 | v8::HandleScope scope; |
| 5448 | DebugLocalContext env; |
| 5449 | |
| 5450 | // Check debugger is unloaded before it is used. |
| 5451 | CheckDebuggerUnloaded(); |
| 5452 | |
| 5453 | // Set a debug message handler. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5454 | v8::Debug::SetMessageHandler2(MessageHandlerHitCount); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5455 | |
| 5456 | // Run code to throw a unhandled exception. This should end up in the message |
| 5457 | // handler. |
| 5458 | CompileRun("throw 1"); |
| 5459 | |
| 5460 | // The message handler should be called. |
| 5461 | CHECK_GT(message_handler_hit_count, 0); |
| 5462 | |
| 5463 | // Clear debug message handler. |
| 5464 | message_handler_hit_count = 0; |
| 5465 | v8::Debug::SetMessageHandler(NULL); |
| 5466 | |
| 5467 | // Run code to throw a unhandled exception. This should end up in the message |
| 5468 | // handler. |
| 5469 | CompileRun("throw 1"); |
| 5470 | |
| 5471 | // The message handler should not be called more. |
| 5472 | CHECK_EQ(0, message_handler_hit_count); |
| 5473 | |
| 5474 | CheckDebuggerUnloaded(true); |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | // Debugger message handler which clears the message handler while active. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5479 | static void MessageHandlerClearingMessageHandler( |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5480 | const v8::Debug::Message& message) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5481 | message_handler_hit_count++; |
| 5482 | |
| 5483 | // Clear debug message handler. |
| 5484 | v8::Debug::SetMessageHandler(NULL); |
| 5485 | } |
| 5486 | |
| 5487 | |
| 5488 | // Test clearing the debug message handler while processing a debug event. |
| 5489 | TEST(DebuggerClearMessageHandlerWhileActive) { |
| 5490 | v8::HandleScope scope; |
| 5491 | DebugLocalContext env; |
| 5492 | |
| 5493 | // Check debugger is unloaded before it is used. |
| 5494 | CheckDebuggerUnloaded(); |
| 5495 | |
| 5496 | // Set a debug message handler. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5497 | v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 5498 | |
| 5499 | // Run code to throw a unhandled exception. This should end up in the message |
| 5500 | // handler. |
| 5501 | CompileRun("throw 1"); |
| 5502 | |
| 5503 | // The message handler should be called. |
| 5504 | CHECK_EQ(1, message_handler_hit_count); |
| 5505 | |
| 5506 | CheckDebuggerUnloaded(true); |
| 5507 | } |
| 5508 | |
| 5509 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5510 | /* Test DebuggerHostDispatch */ |
| 5511 | /* In this test, the debugger waits for a command on a breakpoint |
| 5512 | * and is dispatching host commands while in the infinite loop. |
| 5513 | */ |
| 5514 | |
| 5515 | class HostDispatchV8Thread : public v8::internal::Thread { |
| 5516 | public: |
| 5517 | void Run(); |
| 5518 | }; |
| 5519 | |
| 5520 | class HostDispatchDebuggerThread : public v8::internal::Thread { |
| 5521 | public: |
| 5522 | void Run(); |
| 5523 | }; |
| 5524 | |
| 5525 | Barriers* host_dispatch_barriers; |
| 5526 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5527 | static void HostDispatchMessageHandler(const v8::Debug::Message& message) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5528 | static char print_buffer[1000]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5529 | v8::String::Value json(message.GetJSON()); |
| 5530 | Utf16ToAscii(*json, json.length(), print_buffer); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5534 | static void HostDispatchDispatchHandler() { |
| 5535 | host_dispatch_barriers->semaphore_1->Signal(); |
| 5536 | } |
| 5537 | |
| 5538 | |
| 5539 | void HostDispatchV8Thread::Run() { |
| 5540 | const char* source_1 = "var y_global = 3;\n" |
| 5541 | "function cat( new_value ) {\n" |
| 5542 | " var x = new_value;\n" |
| 5543 | " y_global = 4;\n" |
| 5544 | " x = 3 * x + 1;\n" |
| 5545 | " y_global = 5;\n" |
| 5546 | " return x;\n" |
| 5547 | "}\n" |
| 5548 | "\n"; |
| 5549 | const char* source_2 = "cat(17);\n"; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5550 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5551 | v8::HandleScope scope; |
| 5552 | DebugLocalContext env; |
| 5553 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5554 | // Setup message and host dispatch handlers. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 5555 | v8::Debug::SetMessageHandler2(HostDispatchMessageHandler); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5556 | v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5557 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5558 | CompileRun(source_1); |
| 5559 | host_dispatch_barriers->barrier_1.Wait(); |
| 5560 | host_dispatch_barriers->barrier_2.Wait(); |
| 5561 | CompileRun(source_2); |
| 5562 | } |
sgjesse@chromium.org | 3afc158 | 2009-04-16 22:31:44 +0000 | [diff] [blame] | 5563 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5564 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5565 | void HostDispatchDebuggerThread::Run() { |
| 5566 | const int kBufSize = 1000; |
| 5567 | uint16_t buffer[kBufSize]; |
sgjesse@chromium.org | 3afc158 | 2009-04-16 22:31:44 +0000 | [diff] [blame] | 5568 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5569 | const char* command_1 = "{\"seq\":101," |
| 5570 | "\"type\":\"request\"," |
| 5571 | "\"command\":\"setbreakpoint\"," |
| 5572 | "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; |
| 5573 | const char* command_2 = "{\"seq\":102," |
| 5574 | "\"type\":\"request\"," |
| 5575 | "\"command\":\"continue\"}"; |
| 5576 | |
| 5577 | // v8 thread initializes, runs source_1 |
| 5578 | host_dispatch_barriers->barrier_1.Wait(); |
| 5579 | // 1: Set breakpoint in cat(). |
| 5580 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); |
| 5581 | |
| 5582 | host_dispatch_barriers->barrier_2.Wait(); |
| 5583 | // v8 thread starts compiling source_2. |
| 5584 | // Break happens, to run queued commands and host dispatches. |
| 5585 | // Wait for host dispatch to be processed. |
| 5586 | host_dispatch_barriers->semaphore_1->Wait(); |
| 5587 | // 2: Continue evaluation |
| 5588 | v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); |
| 5589 | } |
| 5590 | |
| 5591 | HostDispatchDebuggerThread host_dispatch_debugger_thread; |
| 5592 | HostDispatchV8Thread host_dispatch_v8_thread; |
| 5593 | |
| 5594 | |
| 5595 | TEST(DebuggerHostDispatch) { |
| 5596 | i::FLAG_debugger_auto_break = true; |
| 5597 | |
| 5598 | // Create a V8 environment |
| 5599 | Barriers stack_allocated_host_dispatch_barriers; |
| 5600 | stack_allocated_host_dispatch_barriers.Initialize(); |
| 5601 | host_dispatch_barriers = &stack_allocated_host_dispatch_barriers; |
| 5602 | |
| 5603 | host_dispatch_v8_thread.Start(); |
| 5604 | host_dispatch_debugger_thread.Start(); |
| 5605 | |
| 5606 | host_dispatch_v8_thread.Join(); |
| 5607 | host_dispatch_debugger_thread.Join(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 5608 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5609 | |
| 5610 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5611 | /* Test DebugMessageDispatch */ |
| 5612 | /* In this test, the V8 thread waits for a message from the debug thread. |
| 5613 | * The DebugMessageDispatchHandler is executed from the debugger thread |
| 5614 | * which signals the V8 thread to wake up. |
| 5615 | */ |
| 5616 | |
| 5617 | class DebugMessageDispatchV8Thread : public v8::internal::Thread { |
| 5618 | public: |
| 5619 | void Run(); |
| 5620 | }; |
| 5621 | |
| 5622 | class DebugMessageDispatchDebuggerThread : public v8::internal::Thread { |
| 5623 | public: |
| 5624 | void Run(); |
| 5625 | }; |
| 5626 | |
| 5627 | Barriers* debug_message_dispatch_barriers; |
| 5628 | |
| 5629 | |
| 5630 | static void DebugMessageHandler() { |
| 5631 | debug_message_dispatch_barriers->semaphore_1->Signal(); |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | void DebugMessageDispatchV8Thread::Run() { |
| 5636 | v8::HandleScope scope; |
| 5637 | DebugLocalContext env; |
| 5638 | |
| 5639 | // Setup debug message dispatch handler. |
| 5640 | v8::Debug::SetDebugMessageDispatchHandler(DebugMessageHandler); |
| 5641 | |
| 5642 | CompileRun("var y = 1 + 2;\n"); |
| 5643 | debug_message_dispatch_barriers->barrier_1.Wait(); |
| 5644 | debug_message_dispatch_barriers->semaphore_1->Wait(); |
| 5645 | debug_message_dispatch_barriers->barrier_2.Wait(); |
| 5646 | } |
| 5647 | |
| 5648 | |
| 5649 | void DebugMessageDispatchDebuggerThread::Run() { |
| 5650 | debug_message_dispatch_barriers->barrier_1.Wait(); |
| 5651 | SendContinueCommand(); |
| 5652 | debug_message_dispatch_barriers->barrier_2.Wait(); |
| 5653 | } |
| 5654 | |
| 5655 | DebugMessageDispatchDebuggerThread debug_message_dispatch_debugger_thread; |
| 5656 | DebugMessageDispatchV8Thread debug_message_dispatch_v8_thread; |
| 5657 | |
| 5658 | |
| 5659 | TEST(DebuggerDebugMessageDispatch) { |
| 5660 | i::FLAG_debugger_auto_break = true; |
| 5661 | |
| 5662 | // Create a V8 environment |
| 5663 | Barriers stack_allocated_debug_message_dispatch_barriers; |
| 5664 | stack_allocated_debug_message_dispatch_barriers.Initialize(); |
| 5665 | debug_message_dispatch_barriers = |
| 5666 | &stack_allocated_debug_message_dispatch_barriers; |
| 5667 | |
| 5668 | debug_message_dispatch_v8_thread.Start(); |
| 5669 | debug_message_dispatch_debugger_thread.Start(); |
| 5670 | |
| 5671 | debug_message_dispatch_v8_thread.Join(); |
| 5672 | debug_message_dispatch_debugger_thread.Join(); |
| 5673 | } |
| 5674 | |
| 5675 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5676 | TEST(DebuggerAgent) { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5677 | // 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] | 5678 | // parallel. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5679 | const int kPort1 = 5858; |
| 5680 | const int kPort2 = 5857; |
| 5681 | const int kPort3 = 5856; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5682 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5683 | // Make a string with the port2 number. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5684 | const int kPortBufferLen = 6; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5685 | char port2_str[kPortBufferLen]; |
| 5686 | OS::SNPrintF(i::Vector<char>(port2_str, kPortBufferLen), "%d", kPort2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5687 | |
| 5688 | bool ok; |
| 5689 | |
| 5690 | // Initialize the socket library. |
| 5691 | i::Socket::Setup(); |
| 5692 | |
| 5693 | // Test starting and stopping the agent without any client connection. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5694 | i::Debugger::StartAgent("test", kPort1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5695 | i::Debugger::StopAgent(); |
| 5696 | |
| 5697 | // Test starting the agent, connecting a client and shutting down the agent |
| 5698 | // with the client connected. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5699 | ok = i::Debugger::StartAgent("test", kPort2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5700 | CHECK(ok); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5701 | i::Debugger::WaitForAgent(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5702 | i::Socket* client = i::OS::CreateSocket(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5703 | ok = client->Connect("localhost", port2_str); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5704 | CHECK(ok); |
| 5705 | i::Debugger::StopAgent(); |
| 5706 | delete client; |
| 5707 | |
| 5708 | // Test starting and stopping the agent with the required port already |
| 5709 | // occoupied. |
| 5710 | i::Socket* server = i::OS::CreateSocket(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5711 | server->Bind(kPort3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5712 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5713 | i::Debugger::StartAgent("test", kPort3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5714 | i::Debugger::StopAgent(); |
| 5715 | |
| 5716 | delete server; |
| 5717 | } |
| 5718 | |
| 5719 | |
| 5720 | class DebuggerAgentProtocolServerThread : public i::Thread { |
| 5721 | public: |
| 5722 | explicit DebuggerAgentProtocolServerThread(int port) |
| 5723 | : port_(port), server_(NULL), client_(NULL), |
| 5724 | listening_(OS::CreateSemaphore(0)) { |
| 5725 | } |
| 5726 | ~DebuggerAgentProtocolServerThread() { |
| 5727 | // Close both sockets. |
| 5728 | delete client_; |
| 5729 | delete server_; |
| 5730 | delete listening_; |
| 5731 | } |
| 5732 | |
| 5733 | void Run(); |
| 5734 | void WaitForListening() { listening_->Wait(); } |
| 5735 | char* body() { return *body_; } |
| 5736 | |
| 5737 | private: |
| 5738 | int port_; |
| 5739 | i::SmartPointer<char> body_; |
| 5740 | i::Socket* server_; // Server socket used for bind/accept. |
| 5741 | i::Socket* client_; // Single client connection used by the test. |
| 5742 | i::Semaphore* listening_; // Signalled when the server is in listen mode. |
| 5743 | }; |
| 5744 | |
| 5745 | |
| 5746 | void DebuggerAgentProtocolServerThread::Run() { |
| 5747 | bool ok; |
| 5748 | |
| 5749 | // Create the server socket and bind it to the requested port. |
| 5750 | server_ = i::OS::CreateSocket(); |
| 5751 | CHECK(server_ != NULL); |
| 5752 | ok = server_->Bind(port_); |
| 5753 | CHECK(ok); |
| 5754 | |
| 5755 | // Listen for new connections. |
| 5756 | ok = server_->Listen(1); |
| 5757 | CHECK(ok); |
| 5758 | listening_->Signal(); |
| 5759 | |
| 5760 | // Accept a connection. |
| 5761 | client_ = server_->Accept(); |
| 5762 | CHECK(client_ != NULL); |
| 5763 | |
| 5764 | // Receive a debugger agent protocol message. |
| 5765 | i::DebuggerAgentUtil::ReceiveMessage(client_); |
| 5766 | } |
| 5767 | |
| 5768 | |
| 5769 | TEST(DebuggerAgentProtocolOverflowHeader) { |
| 5770 | // Make sure this port is not used by other tests to allow tests to run in |
| 5771 | // parallel. |
| 5772 | const int kPort = 5860; |
| 5773 | static const char* kLocalhost = "localhost"; |
| 5774 | |
| 5775 | // Make a string with the port number. |
| 5776 | const int kPortBufferLen = 6; |
| 5777 | char port_str[kPortBufferLen]; |
| 5778 | OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort); |
| 5779 | |
| 5780 | // Initialize the socket library. |
| 5781 | i::Socket::Setup(); |
| 5782 | |
| 5783 | // Create a socket server to receive a debugger agent message. |
| 5784 | DebuggerAgentProtocolServerThread* server = |
| 5785 | new DebuggerAgentProtocolServerThread(kPort); |
| 5786 | server->Start(); |
| 5787 | server->WaitForListening(); |
| 5788 | |
| 5789 | // Connect. |
| 5790 | i::Socket* client = i::OS::CreateSocket(); |
| 5791 | CHECK(client != NULL); |
| 5792 | bool ok = client->Connect(kLocalhost, port_str); |
| 5793 | CHECK(ok); |
| 5794 | |
| 5795 | // Send headers which overflow the receive buffer. |
| 5796 | static const int kBufferSize = 1000; |
| 5797 | char buffer[kBufferSize]; |
| 5798 | |
| 5799 | // Long key and short value: XXXX....XXXX:0\r\n. |
| 5800 | for (int i = 0; i < kBufferSize - 4; i++) { |
| 5801 | buffer[i] = 'X'; |
| 5802 | } |
| 5803 | buffer[kBufferSize - 4] = ':'; |
| 5804 | buffer[kBufferSize - 3] = '0'; |
| 5805 | buffer[kBufferSize - 2] = '\r'; |
| 5806 | buffer[kBufferSize - 1] = '\n'; |
| 5807 | client->Send(buffer, kBufferSize); |
| 5808 | |
| 5809 | // Short key and long value: X:XXXX....XXXX\r\n. |
| 5810 | buffer[0] = 'X'; |
| 5811 | buffer[1] = ':'; |
| 5812 | for (int i = 2; i < kBufferSize - 2; i++) { |
| 5813 | buffer[i] = 'X'; |
| 5814 | } |
| 5815 | buffer[kBufferSize - 2] = '\r'; |
| 5816 | buffer[kBufferSize - 1] = '\n'; |
| 5817 | client->Send(buffer, kBufferSize); |
| 5818 | |
| 5819 | // Add empty body to request. |
| 5820 | const char* content_length_zero_header = "Content-Length:0\r\n"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5821 | client->Send(content_length_zero_header, |
| 5822 | StrLength(content_length_zero_header)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5823 | client->Send("\r\n", 2); |
| 5824 | |
| 5825 | // Wait until data is received. |
| 5826 | server->Join(); |
| 5827 | |
| 5828 | // Check for empty body. |
| 5829 | CHECK(server->body() == NULL); |
| 5830 | |
| 5831 | // Close the client before the server to avoid TIME_WAIT issues. |
| 5832 | client->Shutdown(); |
| 5833 | delete client; |
| 5834 | delete server; |
| 5835 | } |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5836 | |
| 5837 | |
| 5838 | // Test for issue http://code.google.com/p/v8/issues/detail?id=289. |
| 5839 | // Make sure that DebugGetLoadedScripts doesn't return scripts |
| 5840 | // with disposed external source. |
| 5841 | class EmptyExternalStringResource : public v8::String::ExternalStringResource { |
| 5842 | public: |
| 5843 | EmptyExternalStringResource() { empty_[0] = 0; } |
| 5844 | virtual ~EmptyExternalStringResource() {} |
| 5845 | virtual size_t length() const { return empty_.length(); } |
| 5846 | virtual const uint16_t* data() const { return empty_.start(); } |
| 5847 | private: |
| 5848 | ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; |
| 5849 | }; |
| 5850 | |
| 5851 | |
| 5852 | TEST(DebugGetLoadedScripts) { |
| 5853 | v8::HandleScope scope; |
| 5854 | DebugLocalContext env; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5855 | env.ExposeDebug(); |
| 5856 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5857 | EmptyExternalStringResource source_ext_str; |
| 5858 | v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str); |
| 5859 | v8::Handle<v8::Script> evil_script = v8::Script::Compile(source); |
| 5860 | Handle<i::ExternalTwoByteString> i_source( |
| 5861 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 5862 | // This situation can happen if source was an external string disposed |
| 5863 | // by its owner. |
| 5864 | i_source->set_resource(0); |
| 5865 | |
| 5866 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 5867 | i::FLAG_allow_natives_syntax = true; |
| 5868 | CompileRun( |
| 5869 | "var scripts = %DebugGetLoadedScripts();" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5870 | "var count = scripts.length;" |
| 5871 | "for (var i = 0; i < count; ++i) {" |
| 5872 | " scripts[i].line_ends;" |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5873 | "}"); |
| 5874 | // Must not crash while accessing line_ends. |
| 5875 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5876 | |
| 5877 | // Some scripts are retrieved - at least the number of native scripts. |
| 5878 | CHECK_GT((*env)->Global()->Get(v8::String::New("count"))->Int32Value(), 8); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5879 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5880 | |
| 5881 | |
| 5882 | // Test script break points set on lines. |
| 5883 | TEST(ScriptNameAndData) { |
| 5884 | v8::HandleScope scope; |
| 5885 | DebugLocalContext env; |
| 5886 | env.ExposeDebug(); |
| 5887 | |
| 5888 | // Create functions for retrieving script name and data for the function on |
| 5889 | // the top frame when hitting a break point. |
| 5890 | frame_script_name = CompileFunction(&env, |
| 5891 | frame_script_name_source, |
| 5892 | "frame_script_name"); |
| 5893 | frame_script_data = CompileFunction(&env, |
| 5894 | frame_script_data_source, |
| 5895 | "frame_script_data"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5896 | compiled_script_data = CompileFunction(&env, |
| 5897 | compiled_script_data_source, |
| 5898 | "compiled_script_data"); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5899 | |
| 5900 | v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, |
| 5901 | v8::Undefined()); |
| 5902 | |
| 5903 | // Test function source. |
| 5904 | v8::Local<v8::String> script = v8::String::New( |
| 5905 | "function f() {\n" |
| 5906 | " debugger;\n" |
| 5907 | "}\n"); |
| 5908 | |
| 5909 | v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name")); |
| 5910 | v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1); |
| 5911 | script1->SetData(v8::String::New("data")); |
| 5912 | script1->Run(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5913 | v8::Local<v8::Function> f; |
| 5914 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 5915 | |
| 5916 | f->Call(env->Global(), 0, NULL); |
| 5917 | CHECK_EQ(1, break_point_hit_count); |
| 5918 | CHECK_EQ("name", last_script_name_hit); |
| 5919 | CHECK_EQ("data", last_script_data_hit); |
| 5920 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5921 | // Compile the same script again without setting data. As the compilation |
| 5922 | // cache is disabled when debugging expect the data to be missing. |
| 5923 | v8::Script::Compile(script, &origin1)->Run(); |
| 5924 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 5925 | f->Call(env->Global(), 0, NULL); |
| 5926 | CHECK_EQ(2, break_point_hit_count); |
| 5927 | CHECK_EQ("name", last_script_name_hit); |
| 5928 | CHECK_EQ("", last_script_data_hit); // Undefined results in empty string. |
| 5929 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5930 | v8::Local<v8::String> data_obj_source = v8::String::New( |
| 5931 | "({ a: 'abc',\n" |
| 5932 | " b: 123,\n" |
| 5933 | " toString: function() { return this.a + ' ' + this.b; }\n" |
| 5934 | "})\n"); |
| 5935 | v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run(); |
| 5936 | v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name")); |
| 5937 | v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2); |
| 5938 | script2->Run(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 5939 | script2->SetData(data_obj->ToString()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5940 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 5941 | f->Call(env->Global(), 0, NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5942 | CHECK_EQ(3, break_point_hit_count); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5943 | CHECK_EQ("new name", last_script_name_hit); |
| 5944 | CHECK_EQ("abc 123", last_script_data_hit); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5945 | |
| 5946 | v8::Handle<v8::Script> script3 = |
| 5947 | v8::Script::Compile(script, &origin2, NULL, |
| 5948 | v8::String::New("in compile")); |
| 5949 | CHECK_EQ("in compile", last_script_data_hit); |
| 5950 | script3->Run(); |
| 5951 | f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 5952 | f->Call(env->Global(), 0, NULL); |
| 5953 | CHECK_EQ(4, break_point_hit_count); |
| 5954 | CHECK_EQ("in compile", last_script_data_hit); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5955 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5956 | |
| 5957 | |
| 5958 | static v8::Persistent<v8::Context> expected_context; |
| 5959 | static v8::Handle<v8::Value> expected_context_data; |
| 5960 | |
| 5961 | |
| 5962 | // Check that the expected context is the one generating the debug event. |
| 5963 | static void ContextCheckMessageHandler(const v8::Debug::Message& message) { |
| 5964 | CHECK(message.GetEventContext() == expected_context); |
| 5965 | CHECK(message.GetEventContext()->GetData()->StrictEquals( |
| 5966 | expected_context_data)); |
| 5967 | message_handler_hit_count++; |
| 5968 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5969 | static char print_buffer[1000]; |
| 5970 | v8::String::Value json(message.GetJSON()); |
| 5971 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 5972 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5973 | // Send a continue command for break events. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5974 | if (IsBreakEventMessage(print_buffer)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5975 | SendContinueCommand(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | |
| 5980 | // Test which creates two contexts and sets different embedder data on each. |
| 5981 | // Checks that this data is set correctly and that when the debug message |
| 5982 | // handler is called the expected context is the one active. |
| 5983 | TEST(ContextData) { |
| 5984 | v8::HandleScope scope; |
| 5985 | |
| 5986 | v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); |
| 5987 | |
| 5988 | // Create two contexts. |
| 5989 | v8::Persistent<v8::Context> context_1; |
| 5990 | v8::Persistent<v8::Context> context_2; |
| 5991 | v8::Handle<v8::ObjectTemplate> global_template = |
| 5992 | v8::Handle<v8::ObjectTemplate>(); |
| 5993 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); |
| 5994 | context_1 = v8::Context::New(NULL, global_template, global_object); |
| 5995 | context_2 = v8::Context::New(NULL, global_template, global_object); |
| 5996 | |
| 5997 | // Default data value is undefined. |
| 5998 | CHECK(context_1->GetData()->IsUndefined()); |
| 5999 | CHECK(context_2->GetData()->IsUndefined()); |
| 6000 | |
| 6001 | // Set and check different data values. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 6002 | v8::Handle<v8::String> data_1 = v8::String::New("1"); |
| 6003 | v8::Handle<v8::String> data_2 = v8::String::New("2"); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6004 | context_1->SetData(data_1); |
| 6005 | context_2->SetData(data_2); |
| 6006 | CHECK(context_1->GetData()->StrictEquals(data_1)); |
| 6007 | CHECK(context_2->GetData()->StrictEquals(data_2)); |
| 6008 | |
| 6009 | // Simple test function which causes a break. |
| 6010 | const char* source = "function f() { debugger; }"; |
| 6011 | |
| 6012 | // Enter and run function in the first context. |
| 6013 | { |
| 6014 | v8::Context::Scope context_scope(context_1); |
| 6015 | expected_context = context_1; |
| 6016 | expected_context_data = data_1; |
| 6017 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6018 | f->Call(context_1->Global(), 0, NULL); |
| 6019 | } |
| 6020 | |
| 6021 | |
| 6022 | // Enter and run function in the second context. |
| 6023 | { |
| 6024 | v8::Context::Scope context_scope(context_2); |
| 6025 | expected_context = context_2; |
| 6026 | expected_context_data = data_2; |
| 6027 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6028 | f->Call(context_2->Global(), 0, NULL); |
| 6029 | } |
| 6030 | |
| 6031 | // Two times compile event and two times break event. |
| 6032 | CHECK_GT(message_handler_hit_count, 4); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6033 | |
| 6034 | v8::Debug::SetMessageHandler2(NULL); |
| 6035 | CheckDebuggerUnloaded(); |
| 6036 | } |
| 6037 | |
| 6038 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6039 | // Debug message handler which issues a debug break when it hits a break event. |
| 6040 | static int message_handler_break_hit_count = 0; |
| 6041 | static void DebugBreakMessageHandler(const v8::Debug::Message& message) { |
| 6042 | // Schedule a debug break for break events. |
| 6043 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6044 | message_handler_break_hit_count++; |
| 6045 | if (message_handler_break_hit_count == 1) { |
| 6046 | v8::Debug::DebugBreak(); |
| 6047 | } |
| 6048 | } |
| 6049 | |
| 6050 | // Issue a continue command if this event will not cause the VM to start |
| 6051 | // running. |
| 6052 | if (!message.WillStartRunning()) { |
| 6053 | SendContinueCommand(); |
| 6054 | } |
| 6055 | } |
| 6056 | |
| 6057 | |
| 6058 | // Test that a debug break can be scheduled while in a message handler. |
| 6059 | TEST(DebugBreakInMessageHandler) { |
| 6060 | v8::HandleScope scope; |
| 6061 | DebugLocalContext env; |
| 6062 | |
| 6063 | v8::Debug::SetMessageHandler2(DebugBreakMessageHandler); |
| 6064 | |
| 6065 | // Test functions. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6066 | const char* script = "function f() { debugger; g(); } function g() { }"; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6067 | CompileRun(script); |
| 6068 | v8::Local<v8::Function> f = |
| 6069 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6070 | v8::Local<v8::Function> g = |
| 6071 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); |
| 6072 | |
| 6073 | // Call f then g. The debugger statement in f will casue a break which will |
| 6074 | // cause another break. |
| 6075 | f->Call(env->Global(), 0, NULL); |
| 6076 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6077 | // Calling g will not cause any additional breaks. |
| 6078 | g->Call(env->Global(), 0, NULL); |
| 6079 | CHECK_EQ(2, message_handler_break_hit_count); |
| 6080 | } |
| 6081 | |
| 6082 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6083 | #ifndef V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6084 | // Debug event handler which gets the function on the top frame and schedules a |
| 6085 | // break a number of times. |
| 6086 | static void DebugEventDebugBreak( |
| 6087 | v8::DebugEvent event, |
| 6088 | v8::Handle<v8::Object> exec_state, |
| 6089 | v8::Handle<v8::Object> event_data, |
| 6090 | v8::Handle<v8::Value> data) { |
| 6091 | |
| 6092 | if (event == v8::Break) { |
| 6093 | break_point_hit_count++; |
| 6094 | |
| 6095 | // Get the name of the top frame function. |
| 6096 | if (!frame_function_name.IsEmpty()) { |
| 6097 | // Get the name of the function. |
| 6098 | const int argc = 1; |
| 6099 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 6100 | v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, |
| 6101 | argc, argv); |
| 6102 | if (result->IsUndefined()) { |
| 6103 | last_function_hit[0] = '\0'; |
| 6104 | } else { |
| 6105 | CHECK(result->IsString()); |
| 6106 | v8::Handle<v8::String> function_name(result->ToString()); |
| 6107 | function_name->WriteAscii(last_function_hit); |
| 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | // Keep forcing breaks. |
| 6112 | if (break_point_hit_count < 20) { |
| 6113 | v8::Debug::DebugBreak(); |
| 6114 | } |
| 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | |
| 6119 | TEST(RegExpDebugBreak) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 6120 | // This test only applies to native regexps. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6121 | v8::HandleScope scope; |
| 6122 | DebugLocalContext env; |
| 6123 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6124 | // Create a function for checking the function when hitting a break point. |
| 6125 | frame_function_name = CompileFunction(&env, |
| 6126 | frame_function_name_source, |
| 6127 | "frame_function_name"); |
| 6128 | |
| 6129 | // Test RegExp which matches white spaces and comments at the begining of a |
| 6130 | // source line. |
| 6131 | const char* script = |
| 6132 | "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" |
| 6133 | "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; |
| 6134 | |
| 6135 | v8::Local<v8::Function> f = CompileFunction(script, "f"); |
| 6136 | const int argc = 1; |
| 6137 | v8::Handle<v8::Value> argv[argc] = { v8::String::New(" /* xxx */ a=0;") }; |
| 6138 | v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv); |
| 6139 | CHECK_EQ(12, result->Int32Value()); |
| 6140 | |
| 6141 | v8::Debug::SetDebugEventListener(DebugEventDebugBreak); |
| 6142 | v8::Debug::DebugBreak(); |
| 6143 | result = f->Call(env->Global(), argc, argv); |
| 6144 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6145 | // Check that there was only one break event. Matching RegExp should not |
| 6146 | // cause Break events. |
| 6147 | CHECK_EQ(1, break_point_hit_count); |
| 6148 | CHECK_EQ("f", last_function_hit); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6149 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6150 | #endif // V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 6151 | |
| 6152 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6153 | // Common part of EvalContextData and NestedBreakEventContextData tests. |
| 6154 | static void ExecuteScriptForContextCheck() { |
| 6155 | // Create a context. |
| 6156 | v8::Persistent<v8::Context> context_1; |
| 6157 | v8::Handle<v8::ObjectTemplate> global_template = |
| 6158 | v8::Handle<v8::ObjectTemplate>(); |
| 6159 | v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); |
| 6160 | context_1 = v8::Context::New(NULL, global_template, global_object); |
| 6161 | |
| 6162 | // Default data value is undefined. |
| 6163 | CHECK(context_1->GetData()->IsUndefined()); |
| 6164 | |
| 6165 | // Set and check a data value. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 6166 | v8::Handle<v8::String> data_1 = v8::String::New("1"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6167 | context_1->SetData(data_1); |
| 6168 | CHECK(context_1->GetData()->StrictEquals(data_1)); |
| 6169 | |
| 6170 | // Simple test function with eval that causes a break. |
| 6171 | const char* source = "function f() { eval('debugger;'); }"; |
| 6172 | |
| 6173 | // Enter and run function in the context. |
| 6174 | { |
| 6175 | v8::Context::Scope context_scope(context_1); |
| 6176 | expected_context = context_1; |
| 6177 | expected_context_data = data_1; |
| 6178 | v8::Local<v8::Function> f = CompileFunction(source, "f"); |
| 6179 | f->Call(context_1->Global(), 0, NULL); |
| 6180 | } |
| 6181 | } |
| 6182 | |
| 6183 | |
| 6184 | // Test which creates a context and sets embedder data on it. Checks that this |
| 6185 | // data is set correctly and that when the debug message handler is called for |
| 6186 | // break event in an eval statement the expected context is the one returned by |
| 6187 | // Message.GetEventContext. |
| 6188 | TEST(EvalContextData) { |
| 6189 | v8::HandleScope scope; |
| 6190 | v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); |
| 6191 | |
| 6192 | ExecuteScriptForContextCheck(); |
| 6193 | |
| 6194 | // One time compile event and one time break event. |
| 6195 | CHECK_GT(message_handler_hit_count, 2); |
| 6196 | v8::Debug::SetMessageHandler2(NULL); |
| 6197 | CheckDebuggerUnloaded(); |
| 6198 | } |
| 6199 | |
| 6200 | |
| 6201 | static bool sent_eval = false; |
| 6202 | static int break_count = 0; |
| 6203 | static int continue_command_send_count = 0; |
| 6204 | // Check that the expected context is the one generating the debug event |
| 6205 | // including the case of nested break event. |
| 6206 | static void DebugEvalContextCheckMessageHandler( |
| 6207 | const v8::Debug::Message& message) { |
| 6208 | CHECK(message.GetEventContext() == expected_context); |
| 6209 | CHECK(message.GetEventContext()->GetData()->StrictEquals( |
| 6210 | expected_context_data)); |
| 6211 | message_handler_hit_count++; |
| 6212 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6213 | static char print_buffer[1000]; |
| 6214 | v8::String::Value json(message.GetJSON()); |
| 6215 | Utf16ToAscii(*json, json.length(), print_buffer); |
| 6216 | |
| 6217 | if (IsBreakEventMessage(print_buffer)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6218 | break_count++; |
| 6219 | if (!sent_eval) { |
| 6220 | sent_eval = true; |
| 6221 | |
| 6222 | const int kBufferSize = 1000; |
| 6223 | uint16_t buffer[kBufferSize]; |
| 6224 | const char* eval_command = |
| 6225 | "{\"seq\":0," |
| 6226 | "\"type\":\"request\"," |
| 6227 | "\"command\":\"evaluate\"," |
| 6228 | "arguments:{\"expression\":\"debugger;\"," |
| 6229 | "\"global\":true,\"disable_break\":false}}"; |
| 6230 | |
| 6231 | // Send evaluate command. |
| 6232 | v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer)); |
| 6233 | return; |
| 6234 | } else { |
| 6235 | // It's a break event caused by the evaluation request above. |
| 6236 | SendContinueCommand(); |
| 6237 | continue_command_send_count++; |
| 6238 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6239 | } else if (IsEvaluateResponseMessage(print_buffer) && |
| 6240 | continue_command_send_count < 2) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6241 | // Response to the evaluation request. We're still on the breakpoint so |
| 6242 | // send continue. |
| 6243 | SendContinueCommand(); |
| 6244 | continue_command_send_count++; |
| 6245 | } |
| 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | // Tests that context returned for break event is correct when the event occurs |
| 6250 | // in 'evaluate' debugger request. |
| 6251 | TEST(NestedBreakEventContextData) { |
| 6252 | v8::HandleScope scope; |
| 6253 | break_count = 0; |
| 6254 | message_handler_hit_count = 0; |
| 6255 | v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler); |
| 6256 | |
| 6257 | ExecuteScriptForContextCheck(); |
| 6258 | |
| 6259 | // One time compile event and two times break event. |
| 6260 | CHECK_GT(message_handler_hit_count, 3); |
| 6261 | |
| 6262 | // One break from the source and another from the evaluate request. |
| 6263 | CHECK_EQ(break_count, 2); |
| 6264 | v8::Debug::SetMessageHandler2(NULL); |
| 6265 | CheckDebuggerUnloaded(); |
| 6266 | } |
| 6267 | |
| 6268 | |
| 6269 | // Debug event listener which counts the script collected events. |
| 6270 | int script_collected_count = 0; |
| 6271 | static void DebugEventScriptCollectedEvent(v8::DebugEvent event, |
| 6272 | v8::Handle<v8::Object> exec_state, |
| 6273 | v8::Handle<v8::Object> event_data, |
| 6274 | v8::Handle<v8::Value> data) { |
| 6275 | // Count the number of breaks. |
| 6276 | if (event == v8::ScriptCollected) { |
| 6277 | script_collected_count++; |
| 6278 | } |
| 6279 | } |
| 6280 | |
| 6281 | |
| 6282 | // Test that scripts collected are reported through the debug event listener. |
| 6283 | TEST(ScriptCollectedEvent) { |
| 6284 | break_point_hit_count = 0; |
| 6285 | script_collected_count = 0; |
| 6286 | v8::HandleScope scope; |
| 6287 | DebugLocalContext env; |
| 6288 | |
| 6289 | // Request the loaded scripts to initialize the debugger script cache. |
| 6290 | Debug::GetLoadedScripts(); |
| 6291 | |
| 6292 | // Do garbage collection to ensure that only the script in this test will be |
| 6293 | // collected afterwards. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6294 | Heap::CollectAllGarbage(false); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6295 | |
| 6296 | script_collected_count = 0; |
| 6297 | v8::Debug::SetDebugEventListener(DebugEventScriptCollectedEvent, |
| 6298 | v8::Undefined()); |
| 6299 | { |
| 6300 | v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); |
| 6301 | v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); |
| 6302 | } |
| 6303 | |
| 6304 | // Do garbage collection to collect the script above which is no longer |
| 6305 | // referenced. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6306 | Heap::CollectAllGarbage(false); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6307 | |
| 6308 | CHECK_EQ(2, script_collected_count); |
| 6309 | |
| 6310 | v8::Debug::SetDebugEventListener(NULL); |
| 6311 | CheckDebuggerUnloaded(); |
| 6312 | } |
| 6313 | |
| 6314 | |
| 6315 | // Debug event listener which counts the script collected events. |
| 6316 | int script_collected_message_count = 0; |
| 6317 | static void ScriptCollectedMessageHandler(const v8::Debug::Message& message) { |
| 6318 | // Count the number of scripts collected. |
| 6319 | if (message.IsEvent() && message.GetEvent() == v8::ScriptCollected) { |
| 6320 | script_collected_message_count++; |
| 6321 | v8::Handle<v8::Context> context = message.GetEventContext(); |
| 6322 | CHECK(context.IsEmpty()); |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | |
| 6327 | // Test that GetEventContext doesn't fail and return empty handle for |
| 6328 | // ScriptCollected events. |
| 6329 | TEST(ScriptCollectedEventContext) { |
| 6330 | script_collected_message_count = 0; |
| 6331 | v8::HandleScope scope; |
| 6332 | |
| 6333 | { // Scope for the DebugLocalContext. |
| 6334 | DebugLocalContext env; |
| 6335 | |
| 6336 | // Request the loaded scripts to initialize the debugger script cache. |
| 6337 | Debug::GetLoadedScripts(); |
| 6338 | |
| 6339 | // Do garbage collection to ensure that only the script in this test will be |
| 6340 | // collected afterwards. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6341 | Heap::CollectAllGarbage(false); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6342 | |
| 6343 | v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler); |
| 6344 | { |
| 6345 | v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); |
| 6346 | v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); |
| 6347 | } |
| 6348 | } |
| 6349 | |
| 6350 | // Do garbage collection to collect the script above which is no longer |
| 6351 | // referenced. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6352 | Heap::CollectAllGarbage(false); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 6353 | |
| 6354 | CHECK_EQ(2, script_collected_message_count); |
| 6355 | |
| 6356 | v8::Debug::SetMessageHandler2(NULL); |
| 6357 | } |
| 6358 | |
| 6359 | |
| 6360 | // Debug event listener which counts the after compile events. |
| 6361 | int after_compile_message_count = 0; |
| 6362 | static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 6363 | // Count the number of scripts collected. |
| 6364 | if (message.IsEvent()) { |
| 6365 | if (message.GetEvent() == v8::AfterCompile) { |
| 6366 | after_compile_message_count++; |
| 6367 | } else if (message.GetEvent() == v8::Break) { |
| 6368 | SendContinueCommand(); |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | |
| 6373 | |
| 6374 | // Tests that after compile event is sent as many times as there are scripts |
| 6375 | // compiled. |
| 6376 | TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
| 6377 | v8::HandleScope scope; |
| 6378 | DebugLocalContext env; |
| 6379 | after_compile_message_count = 0; |
| 6380 | const char* script = "var a=1"; |
| 6381 | |
| 6382 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6383 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6384 | v8::Debug::SetMessageHandler2(NULL); |
| 6385 | |
| 6386 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6387 | v8::Debug::DebugBreak(); |
| 6388 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6389 | |
| 6390 | // Setting listener to NULL should cause debugger unload. |
| 6391 | v8::Debug::SetMessageHandler2(NULL); |
| 6392 | CheckDebuggerUnloaded(); |
| 6393 | |
| 6394 | // Compilation cache should be disabled when debugger is active. |
| 6395 | CHECK_EQ(2, after_compile_message_count); |
| 6396 | } |
| 6397 | |
| 6398 | |
| 6399 | // Tests that break event is sent when message handler is reset. |
| 6400 | TEST(BreakMessageWhenMessageHandlerIsReset) { |
| 6401 | v8::HandleScope scope; |
| 6402 | DebugLocalContext env; |
| 6403 | after_compile_message_count = 0; |
| 6404 | const char* script = "function f() {};"; |
| 6405 | |
| 6406 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6407 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6408 | v8::Debug::SetMessageHandler2(NULL); |
| 6409 | |
| 6410 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6411 | v8::Debug::DebugBreak(); |
| 6412 | v8::Local<v8::Function> f = |
| 6413 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6414 | f->Call(env->Global(), 0, NULL); |
| 6415 | |
| 6416 | // Setting message handler to NULL should cause debugger unload. |
| 6417 | v8::Debug::SetMessageHandler2(NULL); |
| 6418 | CheckDebuggerUnloaded(); |
| 6419 | |
| 6420 | // Compilation cache should be disabled when debugger is active. |
| 6421 | CHECK_EQ(1, after_compile_message_count); |
| 6422 | } |
| 6423 | |
| 6424 | |
| 6425 | static int exception_event_count = 0; |
| 6426 | static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 6427 | if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 6428 | exception_event_count++; |
| 6429 | SendContinueCommand(); |
| 6430 | } |
| 6431 | } |
| 6432 | |
| 6433 | |
| 6434 | // Tests that exception event is sent when message handler is reset. |
| 6435 | TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
| 6436 | v8::HandleScope scope; |
| 6437 | DebugLocalContext env; |
| 6438 | exception_event_count = 0; |
| 6439 | const char* script = "function f() {throw new Error()};"; |
| 6440 | |
| 6441 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6442 | v8::Script::Compile(v8::String::New(script))->Run(); |
| 6443 | v8::Debug::SetMessageHandler2(NULL); |
| 6444 | |
| 6445 | v8::Debug::SetMessageHandler2(ExceptionMessageHandler); |
| 6446 | v8::Local<v8::Function> f = |
| 6447 | v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 6448 | f->Call(env->Global(), 0, NULL); |
| 6449 | |
| 6450 | // Setting message handler to NULL should cause debugger unload. |
| 6451 | v8::Debug::SetMessageHandler2(NULL); |
| 6452 | CheckDebuggerUnloaded(); |
| 6453 | |
| 6454 | CHECK_EQ(1, exception_event_count); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6455 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6456 | |
| 6457 | |
| 6458 | // Tests after compile event is sent when there are some provisional |
| 6459 | // breakpoints out of the scripts lines range. |
| 6460 | TEST(ProvisionalBreakpointOnLineOutOfRange) { |
| 6461 | v8::HandleScope scope; |
| 6462 | DebugLocalContext env; |
| 6463 | env.ExposeDebug(); |
| 6464 | const char* script = "function f() {};"; |
| 6465 | const char* resource_name = "test_resource"; |
| 6466 | |
| 6467 | // Set a couple of provisional breakpoint on lines out of the script lines |
| 6468 | // range. |
| 6469 | int sbp1 = SetScriptBreakPointByNameFromJS(resource_name, 3, |
| 6470 | -1 /* no column */); |
| 6471 | int sbp2 = SetScriptBreakPointByNameFromJS(resource_name, 5, 5); |
| 6472 | |
| 6473 | after_compile_message_count = 0; |
| 6474 | v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 6475 | |
| 6476 | v8::ScriptOrigin origin( |
| 6477 | v8::String::New(resource_name), |
| 6478 | v8::Integer::New(10), |
| 6479 | v8::Integer::New(1)); |
| 6480 | // Compile a script whose first line number is greater than the breakpoints' |
| 6481 | // lines. |
| 6482 | v8::Script::Compile(v8::String::New(script), &origin)->Run(); |
| 6483 | |
| 6484 | // If the script is compiled successfully there is exactly one after compile |
| 6485 | // event. In case of an exception in debugger code after compile event is not |
| 6486 | // sent. |
| 6487 | CHECK_EQ(1, after_compile_message_count); |
| 6488 | |
| 6489 | ClearBreakPointFromJS(sbp1); |
| 6490 | ClearBreakPointFromJS(sbp2); |
| 6491 | v8::Debug::SetMessageHandler2(NULL); |
| 6492 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6493 | |
| 6494 | |
| 6495 | static void BreakMessageHandler(const v8::Debug::Message& message) { |
| 6496 | if (message.IsEvent() && message.GetEvent() == v8::Break) { |
| 6497 | // Count the number of breaks. |
| 6498 | break_point_hit_count++; |
| 6499 | |
| 6500 | v8::HandleScope scope; |
| 6501 | v8::Handle<v8::String> json = message.GetJSON(); |
| 6502 | |
| 6503 | SendContinueCommand(); |
| 6504 | } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { |
| 6505 | v8::HandleScope scope; |
| 6506 | |
| 6507 | bool is_debug_break = i::StackGuard::IsDebugBreak(); |
| 6508 | // Force DebugBreak flag while serializer is working. |
| 6509 | i::StackGuard::DebugBreak(); |
| 6510 | |
| 6511 | // Force serialization to trigger some internal JS execution. |
| 6512 | v8::Handle<v8::String> json = message.GetJSON(); |
| 6513 | |
| 6514 | // Restore previous state. |
| 6515 | if (is_debug_break) { |
| 6516 | i::StackGuard::DebugBreak(); |
| 6517 | } else { |
| 6518 | i::StackGuard::Continue(i::DEBUGBREAK); |
| 6519 | } |
| 6520 | } |
| 6521 | } |
| 6522 | |
| 6523 | |
| 6524 | // Test that if DebugBreak is forced it is ignored when code from |
| 6525 | // debug-delay.js is executed. |
| 6526 | TEST(NoDebugBreakInAfterCompileMessageHandler) { |
| 6527 | v8::HandleScope scope; |
| 6528 | DebugLocalContext env; |
| 6529 | |
| 6530 | // Register a debug event listener which sets the break flag and counts. |
| 6531 | v8::Debug::SetMessageHandler2(BreakMessageHandler); |
| 6532 | |
| 6533 | // Set the debug break flag. |
| 6534 | v8::Debug::DebugBreak(); |
| 6535 | |
| 6536 | // Create a function for testing stepping. |
| 6537 | const char* src = "function f() { eval('var x = 10;'); } "; |
| 6538 | v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); |
| 6539 | |
| 6540 | // There should be only one break event. |
| 6541 | CHECK_EQ(1, break_point_hit_count); |
| 6542 | |
| 6543 | // Set the debug break flag again. |
| 6544 | v8::Debug::DebugBreak(); |
| 6545 | f->Call(env->Global(), 0, NULL); |
| 6546 | // There should be one more break event when the script is evaluated in 'f'. |
| 6547 | CHECK_EQ(2, break_point_hit_count); |
| 6548 | |
| 6549 | // Get rid of the debug message handler. |
| 6550 | v8::Debug::SetMessageHandler2(NULL); |
| 6551 | CheckDebuggerUnloaded(); |
| 6552 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6553 | |
| 6554 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6555 | static int counting_message_handler_counter; |
| 6556 | |
| 6557 | static void CountingMessageHandler(const v8::Debug::Message& message) { |
| 6558 | counting_message_handler_counter++; |
| 6559 | } |
| 6560 | |
| 6561 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6562 | TEST(ProcessDebugMessages) { |
| 6563 | v8::HandleScope scope; |
| 6564 | DebugLocalContext env; |
| 6565 | |
| 6566 | counting_message_handler_counter = 0; |
| 6567 | |
| 6568 | v8::Debug::SetMessageHandler2(CountingMessageHandler); |
| 6569 | |
| 6570 | const int kBufferSize = 1000; |
| 6571 | uint16_t buffer[kBufferSize]; |
| 6572 | const char* scripts_command = |
| 6573 | "{\"seq\":0," |
| 6574 | "\"type\":\"request\"," |
| 6575 | "\"command\":\"scripts\"}"; |
| 6576 | |
| 6577 | // Send scripts command. |
| 6578 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6579 | |
| 6580 | CHECK_EQ(0, counting_message_handler_counter); |
| 6581 | v8::Debug::ProcessDebugMessages(); |
| 6582 | // At least one message should come |
| 6583 | CHECK_GE(counting_message_handler_counter, 1); |
| 6584 | |
| 6585 | counting_message_handler_counter = 0; |
| 6586 | |
| 6587 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6588 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6589 | CHECK_EQ(0, counting_message_handler_counter); |
| 6590 | v8::Debug::ProcessDebugMessages(); |
| 6591 | // At least two messages should come |
| 6592 | CHECK_GE(counting_message_handler_counter, 2); |
| 6593 | |
| 6594 | // Get rid of the debug message handler. |
| 6595 | v8::Debug::SetMessageHandler2(NULL); |
| 6596 | CheckDebuggerUnloaded(); |
| 6597 | } |
| 6598 | |
| 6599 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6600 | struct BacktraceData { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6601 | static int frame_counter; |
| 6602 | static void MessageHandler(const v8::Debug::Message& message) { |
| 6603 | char print_buffer[1000]; |
| 6604 | v8::String::Value json(message.GetJSON()); |
| 6605 | Utf16ToAscii(*json, json.length(), print_buffer, 1000); |
| 6606 | |
| 6607 | if (strstr(print_buffer, "backtrace") == NULL) { |
| 6608 | return; |
| 6609 | } |
| 6610 | frame_counter = GetTotalFramesInt(print_buffer); |
| 6611 | } |
| 6612 | }; |
| 6613 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6614 | int BacktraceData::frame_counter; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6615 | |
| 6616 | |
| 6617 | // Test that debug messages get processed when ProcessDebugMessages is called. |
| 6618 | TEST(Backtrace) { |
| 6619 | v8::HandleScope scope; |
| 6620 | DebugLocalContext env; |
| 6621 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6622 | v8::Debug::SetMessageHandler2(BacktraceData::MessageHandler); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6623 | |
| 6624 | const int kBufferSize = 1000; |
| 6625 | uint16_t buffer[kBufferSize]; |
| 6626 | const char* scripts_command = |
| 6627 | "{\"seq\":0," |
| 6628 | "\"type\":\"request\"," |
| 6629 | "\"command\":\"backtrace\"}"; |
| 6630 | |
| 6631 | // Check backtrace from ProcessDebugMessages. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6632 | BacktraceData::frame_counter = -10; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6633 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6634 | v8::Debug::ProcessDebugMessages(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6635 | CHECK_EQ(BacktraceData::frame_counter, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6636 | |
| 6637 | v8::Handle<v8::String> void0 = v8::String::New("void(0)"); |
| 6638 | v8::Handle<v8::Script> script = v8::Script::Compile(void0, void0); |
| 6639 | |
| 6640 | // Check backtrace from "void(0)" script. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6641 | BacktraceData::frame_counter = -10; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6642 | v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); |
| 6643 | script->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6644 | CHECK_EQ(BacktraceData::frame_counter, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6645 | |
| 6646 | // Get rid of the debug message handler. |
| 6647 | v8::Debug::SetMessageHandler2(NULL); |
| 6648 | CheckDebuggerUnloaded(); |
| 6649 | } |
| 6650 | |
| 6651 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6652 | TEST(GetMirror) { |
| 6653 | v8::HandleScope scope; |
| 6654 | DebugLocalContext env; |
| 6655 | v8::Handle<v8::Value> obj = v8::Debug::GetMirror(v8::String::New("hodja")); |
| 6656 | v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast( |
| 6657 | v8::Script::New( |
| 6658 | v8::String::New( |
| 6659 | "function runTest(mirror) {" |
| 6660 | " return mirror.isString() && (mirror.length() == 5);" |
| 6661 | "}" |
| 6662 | "" |
| 6663 | "runTest;"))->Run()); |
| 6664 | v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj); |
| 6665 | CHECK(result->IsTrue()); |
| 6666 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6667 | |
| 6668 | |
| 6669 | // Test that the debug break flag works with function.apply. |
| 6670 | TEST(DebugBreakFunctionApply) { |
| 6671 | v8::HandleScope scope; |
| 6672 | DebugLocalContext env; |
| 6673 | |
| 6674 | // Create a function for testing breaking in apply. |
| 6675 | v8::Local<v8::Function> foo = CompileFunction( |
| 6676 | &env, |
| 6677 | "function baz(x) { }" |
| 6678 | "function bar(x) { baz(); }" |
| 6679 | "function foo(){ bar.apply(this, [1]); }", |
| 6680 | "foo"); |
| 6681 | |
| 6682 | // Register a debug event listener which steps and counts. |
| 6683 | v8::Debug::SetDebugEventListener(DebugEventBreakMax); |
| 6684 | |
| 6685 | // Set the debug break flag before calling the code using function.apply. |
| 6686 | v8::Debug::DebugBreak(); |
| 6687 | |
| 6688 | // Limit the number of debug breaks. This is a regression test for issue 493 |
| 6689 | // where this test would enter an infinite loop. |
| 6690 | break_point_hit_count = 0; |
| 6691 | max_break_point_hit_count = 10000; // 10000 => infinite loop. |
| 6692 | foo->Call(env->Global(), 0, NULL); |
| 6693 | |
| 6694 | // When keeping the debug break several break will happen. |
| 6695 | CHECK_EQ(3, break_point_hit_count); |
| 6696 | |
| 6697 | v8::Debug::SetDebugEventListener(NULL); |
| 6698 | CheckDebuggerUnloaded(); |
| 6699 | } |
| 6700 | |
| 6701 | |
| 6702 | v8::Handle<v8::Context> debugee_context; |
| 6703 | v8::Handle<v8::Context> debugger_context; |
| 6704 | |
| 6705 | |
| 6706 | // Property getter that checks that current and calling contexts |
| 6707 | // are both the debugee contexts. |
| 6708 | static v8::Handle<v8::Value> NamedGetterWithCallingContextCheck( |
| 6709 | v8::Local<v8::String> name, |
| 6710 | const v8::AccessorInfo& info) { |
| 6711 | CHECK_EQ(0, strcmp(*v8::String::AsciiValue(name), "a")); |
| 6712 | v8::Handle<v8::Context> current = v8::Context::GetCurrent(); |
| 6713 | CHECK(current == debugee_context); |
| 6714 | CHECK(current != debugger_context); |
| 6715 | v8::Handle<v8::Context> calling = v8::Context::GetCalling(); |
| 6716 | CHECK(calling == debugee_context); |
| 6717 | CHECK(calling != debugger_context); |
| 6718 | return v8::Int32::New(1); |
| 6719 | } |
| 6720 | |
| 6721 | |
| 6722 | // Debug event listener that checks if the first argument of a function is |
| 6723 | // an object with property 'a' == 1. If the property has custom accessor |
| 6724 | // this handler will eventually invoke it. |
| 6725 | static void DebugEventGetAtgumentPropertyValue( |
| 6726 | v8::DebugEvent event, |
| 6727 | v8::Handle<v8::Object> exec_state, |
| 6728 | v8::Handle<v8::Object> event_data, |
| 6729 | v8::Handle<v8::Value> data) { |
| 6730 | if (event == v8::Break) { |
| 6731 | break_point_hit_count++; |
| 6732 | CHECK(debugger_context == v8::Context::GetCurrent()); |
| 6733 | v8::Handle<v8::Function> func(v8::Function::Cast(*CompileRun( |
| 6734 | "(function(exec_state) {\n" |
| 6735 | " return (exec_state.frame(0).argumentValue(0).property('a').\n" |
| 6736 | " value().value() == 1);\n" |
| 6737 | "})"))); |
| 6738 | const int argc = 1; |
| 6739 | v8::Handle<v8::Value> argv[argc] = { exec_state }; |
| 6740 | v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv); |
| 6741 | CHECK(result->IsTrue()); |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | |
| 6746 | TEST(CallingContextIsNotDebugContext) { |
| 6747 | // Create and enter a debugee context. |
| 6748 | v8::HandleScope scope; |
| 6749 | DebugLocalContext env; |
| 6750 | env.ExposeDebug(); |
| 6751 | |
| 6752 | // Save handles to the debugger and debugee contexts to be used in |
| 6753 | // NamedGetterWithCallingContextCheck. |
| 6754 | debugee_context = v8::Local<v8::Context>(*env); |
| 6755 | debugger_context = v8::Utils::ToLocal(Debug::debug_context()); |
| 6756 | |
| 6757 | // Create object with 'a' property accessor. |
| 6758 | v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); |
| 6759 | named->SetAccessor(v8::String::New("a"), |
| 6760 | NamedGetterWithCallingContextCheck); |
| 6761 | env->Global()->Set(v8::String::New("obj"), |
| 6762 | named->NewInstance()); |
| 6763 | |
| 6764 | // Register the debug event listener |
| 6765 | v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue); |
| 6766 | |
| 6767 | // Create a function that invokes debugger. |
| 6768 | v8::Local<v8::Function> foo = CompileFunction( |
| 6769 | &env, |
| 6770 | "function bar(x) { debugger; }" |
| 6771 | "function foo(){ bar(obj); }", |
| 6772 | "foo"); |
| 6773 | |
| 6774 | break_point_hit_count = 0; |
| 6775 | foo->Call(env->Global(), 0, NULL); |
| 6776 | CHECK_EQ(1, break_point_hit_count); |
| 6777 | |
| 6778 | v8::Debug::SetDebugEventListener(NULL); |
| 6779 | debugee_context = v8::Handle<v8::Context>(); |
| 6780 | debugger_context = v8::Handle<v8::Context>(); |
| 6781 | CheckDebuggerUnloaded(); |
| 6782 | } |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6783 | |
| 6784 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6785 | TEST(DebugContextIsPreservedBetweenAccesses) { |
| 6786 | v8::HandleScope scope; |
| 6787 | v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext(); |
| 6788 | v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext(); |
| 6789 | CHECK_EQ(*context1, *context2); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | |
| 6793 | static v8::Handle<v8::Value> expected_callback_data; |
| 6794 | static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { |
| 6795 | CHECK(details.GetEventContext() == expected_context); |
| 6796 | CHECK_EQ(expected_callback_data, details.GetCallbackData()); |
| 6797 | } |
| 6798 | |
| 6799 | // Check that event details contain context where debug event occured. |
| 6800 | TEST(DebugEventContext) { |
| 6801 | v8::HandleScope scope; |
| 6802 | expected_callback_data = v8::Int32::New(2010); |
| 6803 | v8::Debug::SetDebugEventListener2(DebugEventContextChecker, |
| 6804 | expected_callback_data); |
| 6805 | expected_context = v8::Context::New(); |
| 6806 | v8::Context::Scope context_scope(expected_context); |
| 6807 | v8::Script::Compile(v8::String::New("(function(){debugger;})();"))->Run(); |
| 6808 | expected_context.Dispose(); |
| 6809 | expected_context.Clear(); |
| 6810 | v8::Debug::SetDebugEventListener(NULL); |
| 6811 | expected_context_data = v8::Handle<v8::Value>(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6812 | CheckDebuggerUnloaded(); |
| 6813 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6814 | |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 6815 | |
| 6816 | static void* expected_break_data; |
| 6817 | static bool was_debug_break_called; |
| 6818 | static bool was_debug_event_called; |
| 6819 | static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) { |
| 6820 | if (details.GetEvent() == v8::BreakForCommand) { |
| 6821 | CHECK_EQ(expected_break_data, details.GetClientData()); |
| 6822 | was_debug_event_called = true; |
| 6823 | } else if (details.GetEvent() == v8::Break) { |
| 6824 | was_debug_break_called = true; |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | // Check that event details contain context where debug event occured. |
| 6829 | TEST(DebugEventBreakData) { |
| 6830 | v8::HandleScope scope; |
| 6831 | DebugLocalContext env; |
| 6832 | v8::Debug::SetDebugEventListener2(DebugEventBreakDataChecker); |
| 6833 | |
| 6834 | TestClientData::constructor_call_counter = 0; |
| 6835 | TestClientData::destructor_call_counter = 0; |
| 6836 | |
| 6837 | expected_break_data = NULL; |
| 6838 | was_debug_event_called = false; |
| 6839 | was_debug_break_called = false; |
| 6840 | v8::Debug::DebugBreakForCommand(); |
| 6841 | v8::Script::Compile(v8::String::New("(function(x){return x;})(1);"))->Run(); |
| 6842 | CHECK(was_debug_event_called); |
| 6843 | CHECK(!was_debug_break_called); |
| 6844 | |
| 6845 | TestClientData* data1 = new TestClientData(); |
| 6846 | expected_break_data = data1; |
| 6847 | was_debug_event_called = false; |
| 6848 | was_debug_break_called = false; |
| 6849 | v8::Debug::DebugBreakForCommand(data1); |
| 6850 | v8::Script::Compile(v8::String::New("(function(x){return x+1;})(1);"))->Run(); |
| 6851 | CHECK(was_debug_event_called); |
| 6852 | CHECK(!was_debug_break_called); |
| 6853 | |
| 6854 | expected_break_data = NULL; |
| 6855 | was_debug_event_called = false; |
| 6856 | was_debug_break_called = false; |
| 6857 | v8::Debug::DebugBreak(); |
| 6858 | v8::Script::Compile(v8::String::New("(function(x){return x+2;})(1);"))->Run(); |
| 6859 | CHECK(!was_debug_event_called); |
| 6860 | CHECK(was_debug_break_called); |
| 6861 | |
| 6862 | TestClientData* data2 = new TestClientData(); |
| 6863 | expected_break_data = data2; |
| 6864 | was_debug_event_called = false; |
| 6865 | was_debug_break_called = false; |
| 6866 | v8::Debug::DebugBreak(); |
| 6867 | v8::Debug::DebugBreakForCommand(data2); |
| 6868 | v8::Script::Compile(v8::String::New("(function(x){return x+3;})(1);"))->Run(); |
| 6869 | CHECK(was_debug_event_called); |
| 6870 | CHECK(was_debug_break_called); |
| 6871 | |
| 6872 | CHECK_EQ(2, TestClientData::constructor_call_counter); |
| 6873 | CHECK_EQ(TestClientData::constructor_call_counter, |
| 6874 | TestClientData::destructor_call_counter); |
| 6875 | |
| 6876 | v8::Debug::SetDebugEventListener(NULL); |
| 6877 | CheckDebuggerUnloaded(); |
| 6878 | } |
| 6879 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6880 | #endif // ENABLE_DEBUGGER_SUPPORT |