test-debug.cc revision 85b71799222b55eb5dd74ea26efe0c64ab655c8c
1// Copyright 2011 the V8 project authors. All rights reserved. 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 28#ifdef ENABLE_DEBUGGER_SUPPORT 29 30#include <stdlib.h> 31 32#include "v8.h" 33 34#include "api.h" 35#include "cctest.h" 36#include "compilation-cache.h" 37#include "debug.h" 38#include "deoptimizer.h" 39#include "platform.h" 40#include "stub-cache.h" 41#include "utils.h" 42 43 44using ::v8::internal::EmbeddedVector; 45using ::v8::internal::Object; 46using ::v8::internal::OS; 47using ::v8::internal::Handle; 48using ::v8::internal::Heap; 49using ::v8::internal::JSGlobalProxy; 50using ::v8::internal::Code; 51using ::v8::internal::Debug; 52using ::v8::internal::Debugger; 53using ::v8::internal::CommandMessage; 54using ::v8::internal::CommandMessageQueue; 55using ::v8::internal::StepAction; 56using ::v8::internal::StepIn; // From StepAction enum 57using ::v8::internal::StepNext; // From StepAction enum 58using ::v8::internal::StepOut; // From StepAction enum 59using ::v8::internal::Vector; 60using ::v8::internal::StrLength; 61 62// Size of temp buffer for formatting small strings. 63#define SMALL_STRING_BUFFER_SIZE 80 64 65// --- A d d i t i o n a l C h e c k H e l p e r s 66 67 68// Helper function used by the CHECK_EQ function when given Address 69// arguments. Should not be called directly. 70static inline void CheckEqualsHelper(const char* file, int line, 71 const char* expected_source, 72 ::v8::internal::Address expected, 73 const char* value_source, 74 ::v8::internal::Address value) { 75 if (expected != value) { 76 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " 77 "Expected: %i\n# Found: %i", 78 expected_source, value_source, expected, value); 79 } 80} 81 82 83// Helper function used by the CHECK_NE function when given Address 84// arguments. Should not be called directly. 85static inline void CheckNonEqualsHelper(const char* file, int line, 86 const char* unexpected_source, 87 ::v8::internal::Address unexpected, 88 const char* value_source, 89 ::v8::internal::Address value) { 90 if (unexpected == value) { 91 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i", 92 unexpected_source, value_source, value); 93 } 94} 95 96 97// Helper function used by the CHECK function when given code 98// arguments. Should not be called directly. 99static inline void CheckEqualsHelper(const char* file, int line, 100 const char* expected_source, 101 const Code* expected, 102 const char* value_source, 103 const Code* value) { 104 if (expected != value) { 105 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# " 106 "Expected: %p\n# Found: %p", 107 expected_source, value_source, expected, value); 108 } 109} 110 111 112static inline void CheckNonEqualsHelper(const char* file, int line, 113 const char* expected_source, 114 const Code* expected, 115 const char* value_source, 116 const Code* value) { 117 if (expected == value) { 118 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", 119 expected_source, value_source, value); 120 } 121} 122 123 124// --- H e l p e r C l a s s e s 125 126 127// Helper class for creating a V8 enviromnent for running tests 128class DebugLocalContext { 129 public: 130 inline DebugLocalContext( 131 v8::ExtensionConfiguration* extensions = 0, 132 v8::Handle<v8::ObjectTemplate> global_template = 133 v8::Handle<v8::ObjectTemplate>(), 134 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) 135 : context_(v8::Context::New(extensions, global_template, global_object)) { 136 context_->Enter(); 137 } 138 inline ~DebugLocalContext() { 139 context_->Exit(); 140 context_.Dispose(); 141 } 142 inline v8::Context* operator->() { return *context_; } 143 inline v8::Context* operator*() { return *context_; } 144 inline bool IsReady() { return !context_.IsEmpty(); } 145 void ExposeDebug() { 146 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 147 // Expose the debug context global object in the global object for testing. 148 debug->Load(); 149 debug->debug_context()->set_security_token( 150 v8::Utils::OpenHandle(*context_)->security_token()); 151 152 Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( 153 v8::Utils::OpenHandle(*context_->Global()))); 154 Handle<v8::internal::String> debug_string = 155 FACTORY->LookupAsciiSymbol("debug"); 156 SetProperty(global, debug_string, 157 Handle<Object>(debug->debug_context()->global_proxy()), DONT_ENUM, 158 ::v8::internal::kNonStrictMode); 159 } 160 161 private: 162 v8::Persistent<v8::Context> context_; 163}; 164 165 166// --- H e l p e r F u n c t i o n s 167 168 169// Compile and run the supplied source and return the fequested function. 170static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, 171 const char* source, 172 const char* function_name) { 173 v8::Script::Compile(v8::String::New(source))->Run(); 174 return v8::Local<v8::Function>::Cast( 175 (*env)->Global()->Get(v8::String::New(function_name))); 176} 177 178 179// Compile and run the supplied source and return the requested function. 180static v8::Local<v8::Function> CompileFunction(const char* source, 181 const char* function_name) { 182 v8::Script::Compile(v8::String::New(source))->Run(); 183 return v8::Local<v8::Function>::Cast( 184 v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name))); 185} 186 187 188// Is there any debug info for the function? 189static bool HasDebugInfo(v8::Handle<v8::Function> fun) { 190 Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun); 191 Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); 192 return Debug::HasDebugInfo(shared); 193} 194 195 196// Set a break point in a function and return the associated break point 197// number. 198static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) { 199 static int break_point = 0; 200 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); 201 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 202 debug->SetBreakPoint( 203 shared, 204 Handle<Object>(v8::internal::Smi::FromInt(++break_point)), 205 &position); 206 return break_point; 207} 208 209 210// Set a break point in a function and return the associated break point 211// number. 212static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) { 213 return SetBreakPoint(v8::Utils::OpenHandle(*fun), position); 214} 215 216 217// Set a break point in a function using the Debug object and return the 218// associated break point number. 219static int SetBreakPointFromJS(const char* function_name, 220 int line, int position) { 221 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 222 OS::SNPrintF(buffer, 223 "debug.Debug.setBreakPoint(%s,%d,%d)", 224 function_name, line, position); 225 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 226 v8::Handle<v8::String> str = v8::String::New(buffer.start()); 227 return v8::Script::Compile(str)->Run()->Int32Value(); 228} 229 230 231// Set a break point in a script identified by id using the global Debug object. 232static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) { 233 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 234 if (column >= 0) { 235 // Column specified set script break point on precise location. 236 OS::SNPrintF(buffer, 237 "debug.Debug.setScriptBreakPointById(%d,%d,%d)", 238 script_id, line, column); 239 } else { 240 // Column not specified set script break point on line. 241 OS::SNPrintF(buffer, 242 "debug.Debug.setScriptBreakPointById(%d,%d)", 243 script_id, line); 244 } 245 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 246 { 247 v8::TryCatch try_catch; 248 v8::Handle<v8::String> str = v8::String::New(buffer.start()); 249 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); 250 CHECK(!try_catch.HasCaught()); 251 return value->Int32Value(); 252 } 253} 254 255 256// Set a break point in a script identified by name using the global Debug 257// object. 258static int SetScriptBreakPointByNameFromJS(const char* script_name, 259 int line, int column) { 260 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 261 if (column >= 0) { 262 // Column specified set script break point on precise location. 263 OS::SNPrintF(buffer, 264 "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", 265 script_name, line, column); 266 } else { 267 // Column not specified set script break point on line. 268 OS::SNPrintF(buffer, 269 "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", 270 script_name, line); 271 } 272 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 273 { 274 v8::TryCatch try_catch; 275 v8::Handle<v8::String> str = v8::String::New(buffer.start()); 276 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run(); 277 CHECK(!try_catch.HasCaught()); 278 return value->Int32Value(); 279 } 280} 281 282 283// Clear a break point. 284static void ClearBreakPoint(int break_point) { 285 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 286 debug->ClearBreakPoint( 287 Handle<Object>(v8::internal::Smi::FromInt(break_point))); 288} 289 290 291// Clear a break point using the global Debug object. 292static void ClearBreakPointFromJS(int break_point_number) { 293 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 294 OS::SNPrintF(buffer, 295 "debug.Debug.clearBreakPoint(%d)", 296 break_point_number); 297 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 298 v8::Script::Compile(v8::String::New(buffer.start()))->Run(); 299} 300 301 302static void EnableScriptBreakPointFromJS(int break_point_number) { 303 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 304 OS::SNPrintF(buffer, 305 "debug.Debug.enableScriptBreakPoint(%d)", 306 break_point_number); 307 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 308 v8::Script::Compile(v8::String::New(buffer.start()))->Run(); 309} 310 311 312static void DisableScriptBreakPointFromJS(int break_point_number) { 313 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 314 OS::SNPrintF(buffer, 315 "debug.Debug.disableScriptBreakPoint(%d)", 316 break_point_number); 317 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 318 v8::Script::Compile(v8::String::New(buffer.start()))->Run(); 319} 320 321 322static void ChangeScriptBreakPointConditionFromJS(int break_point_number, 323 const char* condition) { 324 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 325 OS::SNPrintF(buffer, 326 "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")", 327 break_point_number, condition); 328 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 329 v8::Script::Compile(v8::String::New(buffer.start()))->Run(); 330} 331 332 333static void ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number, 334 int ignoreCount) { 335 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 336 OS::SNPrintF(buffer, 337 "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)", 338 break_point_number, ignoreCount); 339 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; 340 v8::Script::Compile(v8::String::New(buffer.start()))->Run(); 341} 342 343 344// Change break on exception. 345static void ChangeBreakOnException(bool caught, bool uncaught) { 346 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 347 debug->ChangeBreakOnException(v8::internal::BreakException, caught); 348 debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); 349} 350 351 352// Change break on exception using the global Debug object. 353static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) { 354 if (caught) { 355 v8::Script::Compile( 356 v8::String::New("debug.Debug.setBreakOnException()"))->Run(); 357 } else { 358 v8::Script::Compile( 359 v8::String::New("debug.Debug.clearBreakOnException()"))->Run(); 360 } 361 if (uncaught) { 362 v8::Script::Compile( 363 v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run(); 364 } else { 365 v8::Script::Compile( 366 v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run(); 367 } 368} 369 370 371// Prepare to step to next break location. 372static void PrepareStep(StepAction step_action) { 373 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 374 debug->PrepareStep(step_action, 1); 375} 376 377 378// This function is in namespace v8::internal to be friend with class 379// v8::internal::Debug. 380namespace v8 { 381namespace internal { 382 383// Collect the currently debugged functions. 384Handle<FixedArray> GetDebuggedFunctions() { 385 Debug* debug = Isolate::Current()->debug(); 386 387 v8::internal::DebugInfoListNode* node = debug->debug_info_list_; 388 389 // Find the number of debugged functions. 390 int count = 0; 391 while (node) { 392 count++; 393 node = node->next(); 394 } 395 396 // Allocate array for the debugged functions 397 Handle<FixedArray> debugged_functions = 398 FACTORY->NewFixedArray(count); 399 400 // Run through the debug info objects and collect all functions. 401 count = 0; 402 while (node) { 403 debugged_functions->set(count++, *node->debug_info()); 404 node = node->next(); 405 } 406 407 return debugged_functions; 408} 409 410 411static Handle<Code> ComputeCallDebugBreak(int argc) { 412 CALL_HEAP_FUNCTION( 413 v8::internal::Isolate::Current(), 414 v8::internal::Isolate::Current()->stub_cache()->ComputeCallDebugBreak( 415 argc, Code::CALL_IC), 416 Code); 417} 418 419 420// Check that the debugger has been fully unloaded. 421void CheckDebuggerUnloaded(bool check_functions) { 422 // Check that the debugger context is cleared and that there is no debug 423 // information stored for the debugger. 424 CHECK(Isolate::Current()->debug()->debug_context().is_null()); 425 CHECK_EQ(NULL, Isolate::Current()->debug()->debug_info_list_); 426 427 // Collect garbage to ensure weak handles are cleared. 428 HEAP->CollectAllGarbage(false); 429 HEAP->CollectAllGarbage(false); 430 431 // Iterate the head and check that there are no debugger related objects left. 432 HeapIterator iterator; 433 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { 434 CHECK(!obj->IsDebugInfo()); 435 CHECK(!obj->IsBreakPointInfo()); 436 437 // If deep check of functions is requested check that no debug break code 438 // is left in all functions. 439 if (check_functions) { 440 if (obj->IsJSFunction()) { 441 JSFunction* fun = JSFunction::cast(obj); 442 for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) { 443 RelocInfo::Mode rmode = it.rinfo()->rmode(); 444 if (RelocInfo::IsCodeTarget(rmode)) { 445 CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address())); 446 } else if (RelocInfo::IsJSReturn(rmode)) { 447 CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo())); 448 } 449 } 450 } 451 } 452 } 453} 454 455 456void ForceUnloadDebugger() { 457 Isolate::Current()->debugger()->never_unload_debugger_ = false; 458 Isolate::Current()->debugger()->UnloadDebugger(); 459} 460 461 462} } // namespace v8::internal 463 464 465// Check that the debugger has been fully unloaded. 466static void CheckDebuggerUnloaded(bool check_functions = false) { 467 // Let debugger to unload itself synchronously 468 v8::Debug::ProcessDebugMessages(); 469 470 v8::internal::CheckDebuggerUnloaded(check_functions); 471} 472 473 474// Inherit from BreakLocationIterator to get access to protected parts for 475// testing. 476class TestBreakLocationIterator: public v8::internal::BreakLocationIterator { 477 public: 478 explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info) 479 : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {} 480 v8::internal::RelocIterator* it() { return reloc_iterator_; } 481 v8::internal::RelocIterator* it_original() { 482 return reloc_iterator_original_; 483 } 484}; 485 486 487// Compile a function, set a break point and check that the call at the break 488// location in the code is the expected debug_break function. 489void CheckDebugBreakFunction(DebugLocalContext* env, 490 const char* source, const char* name, 491 int position, v8::internal::RelocInfo::Mode mode, 492 Code* debug_break) { 493 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 494 495 // Create function and set the break point. 496 Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle( 497 *CompileFunction(env, source, name)); 498 int bp = SetBreakPoint(fun, position); 499 500 // Check that the debug break function is as expected. 501 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared()); 502 CHECK(Debug::HasDebugInfo(shared)); 503 TestBreakLocationIterator it1(Debug::GetDebugInfo(shared)); 504 it1.FindBreakLocationFromPosition(position); 505 v8::internal::RelocInfo::Mode actual_mode = it1.it()->rinfo()->rmode(); 506 if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { 507 actual_mode = v8::internal::RelocInfo::CODE_TARGET; 508 } 509 CHECK_EQ(mode, actual_mode); 510 if (mode != v8::internal::RelocInfo::JS_RETURN) { 511 CHECK_EQ(debug_break, 512 Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address())); 513 } else { 514 CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo())); 515 } 516 517 // Clear the break point and check that the debug break function is no longer 518 // there 519 ClearBreakPoint(bp); 520 CHECK(!debug->HasDebugInfo(shared)); 521 CHECK(debug->EnsureDebugInfo(shared)); 522 TestBreakLocationIterator it2(Debug::GetDebugInfo(shared)); 523 it2.FindBreakLocationFromPosition(position); 524 actual_mode = it2.it()->rinfo()->rmode(); 525 if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) { 526 actual_mode = v8::internal::RelocInfo::CODE_TARGET; 527 } 528 CHECK_EQ(mode, actual_mode); 529 if (mode == v8::internal::RelocInfo::JS_RETURN) { 530 CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo())); 531 } 532} 533 534 535// --- D e b u g E v e n t H a n d l e r s 536// --- 537// --- The different tests uses a number of debug event handlers. 538// --- 539 540 541// Source for the JavaScript function which picks out the function 542// name of a frame. 543const char* frame_function_name_source = 544 "function frame_function_name(exec_state, frame_number) {" 545 " return exec_state.frame(frame_number).func().name();" 546 "}"; 547v8::Local<v8::Function> frame_function_name; 548 549 550// Source for the JavaScript function which pick out the name of the 551// first argument of a frame. 552const char* frame_argument_name_source = 553 "function frame_argument_name(exec_state, frame_number) {" 554 " return exec_state.frame(frame_number).argumentName(0);" 555 "}"; 556v8::Local<v8::Function> frame_argument_name; 557 558 559// Source for the JavaScript function which pick out the value of the 560// first argument of a frame. 561const char* frame_argument_value_source = 562 "function frame_argument_value(exec_state, frame_number) {" 563 " return exec_state.frame(frame_number).argumentValue(0).value_;" 564 "}"; 565v8::Local<v8::Function> frame_argument_value; 566 567 568// Source for the JavaScript function which pick out the name of the 569// first argument of a frame. 570const char* frame_local_name_source = 571 "function frame_local_name(exec_state, frame_number) {" 572 " return exec_state.frame(frame_number).localName(0);" 573 "}"; 574v8::Local<v8::Function> frame_local_name; 575 576 577// Source for the JavaScript function which pick out the value of the 578// first argument of a frame. 579const char* frame_local_value_source = 580 "function frame_local_value(exec_state, frame_number) {" 581 " return exec_state.frame(frame_number).localValue(0).value_;" 582 "}"; 583v8::Local<v8::Function> frame_local_value; 584 585 586// Source for the JavaScript function which picks out the source line for the 587// top frame. 588const char* frame_source_line_source = 589 "function frame_source_line(exec_state) {" 590 " return exec_state.frame(0).sourceLine();" 591 "}"; 592v8::Local<v8::Function> frame_source_line; 593 594 595// Source for the JavaScript function which picks out the source column for the 596// top frame. 597const char* frame_source_column_source = 598 "function frame_source_column(exec_state) {" 599 " return exec_state.frame(0).sourceColumn();" 600 "}"; 601v8::Local<v8::Function> frame_source_column; 602 603 604// Source for the JavaScript function which picks out the script name for the 605// top frame. 606const char* frame_script_name_source = 607 "function frame_script_name(exec_state) {" 608 " return exec_state.frame(0).func().script().name();" 609 "}"; 610v8::Local<v8::Function> frame_script_name; 611 612 613// Source for the JavaScript function which picks out the script data for the 614// top frame. 615const char* frame_script_data_source = 616 "function frame_script_data(exec_state) {" 617 " return exec_state.frame(0).func().script().data();" 618 "}"; 619v8::Local<v8::Function> frame_script_data; 620 621 622// Source for the JavaScript function which picks out the script data from 623// AfterCompile event 624const char* compiled_script_data_source = 625 "function compiled_script_data(event_data) {" 626 " return event_data.script().data();" 627 "}"; 628v8::Local<v8::Function> compiled_script_data; 629 630 631// Source for the JavaScript function which returns the number of frames. 632static const char* frame_count_source = 633 "function frame_count(exec_state) {" 634 " return exec_state.frameCount();" 635 "}"; 636v8::Handle<v8::Function> frame_count; 637 638 639// Global variable to store the last function hit - used by some tests. 640char last_function_hit[80]; 641 642// Global variable to store the name and data for last script hit - used by some 643// tests. 644char last_script_name_hit[80]; 645char last_script_data_hit[80]; 646 647// Global variables to store the last source position - used by some tests. 648int last_source_line = -1; 649int last_source_column = -1; 650 651// Debug event handler which counts the break points which have been hit. 652int break_point_hit_count = 0; 653int break_point_hit_count_deoptimize = 0; 654static void DebugEventBreakPointHitCount(v8::DebugEvent event, 655 v8::Handle<v8::Object> exec_state, 656 v8::Handle<v8::Object> event_data, 657 v8::Handle<v8::Value> data) { 658 Debug* debug = v8::internal::Isolate::Current()->debug(); 659 // When hitting a debug event listener there must be a break set. 660 CHECK_NE(debug->break_id(), 0); 661 662 // Count the number of breaks. 663 if (event == v8::Break) { 664 break_point_hit_count++; 665 if (!frame_function_name.IsEmpty()) { 666 // Get the name of the function. 667 const int argc = 2; 668 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; 669 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, 670 argc, argv); 671 if (result->IsUndefined()) { 672 last_function_hit[0] = '\0'; 673 } else { 674 CHECK(result->IsString()); 675 v8::Handle<v8::String> function_name(result->ToString()); 676 function_name->WriteAscii(last_function_hit); 677 } 678 } 679 680 if (!frame_source_line.IsEmpty()) { 681 // Get the source line. 682 const int argc = 1; 683 v8::Handle<v8::Value> argv[argc] = { exec_state }; 684 v8::Handle<v8::Value> result = frame_source_line->Call(exec_state, 685 argc, argv); 686 CHECK(result->IsNumber()); 687 last_source_line = result->Int32Value(); 688 } 689 690 if (!frame_source_column.IsEmpty()) { 691 // Get the source column. 692 const int argc = 1; 693 v8::Handle<v8::Value> argv[argc] = { exec_state }; 694 v8::Handle<v8::Value> result = frame_source_column->Call(exec_state, 695 argc, argv); 696 CHECK(result->IsNumber()); 697 last_source_column = result->Int32Value(); 698 } 699 700 if (!frame_script_name.IsEmpty()) { 701 // Get the script name of the function script. 702 const int argc = 1; 703 v8::Handle<v8::Value> argv[argc] = { exec_state }; 704 v8::Handle<v8::Value> result = frame_script_name->Call(exec_state, 705 argc, argv); 706 if (result->IsUndefined()) { 707 last_script_name_hit[0] = '\0'; 708 } else { 709 CHECK(result->IsString()); 710 v8::Handle<v8::String> script_name(result->ToString()); 711 script_name->WriteAscii(last_script_name_hit); 712 } 713 } 714 715 if (!frame_script_data.IsEmpty()) { 716 // Get the script data of the function script. 717 const int argc = 1; 718 v8::Handle<v8::Value> argv[argc] = { exec_state }; 719 v8::Handle<v8::Value> result = frame_script_data->Call(exec_state, 720 argc, argv); 721 if (result->IsUndefined()) { 722 last_script_data_hit[0] = '\0'; 723 } else { 724 result = result->ToString(); 725 CHECK(result->IsString()); 726 v8::Handle<v8::String> script_data(result->ToString()); 727 script_data->WriteAscii(last_script_data_hit); 728 } 729 } 730 731 // Perform a full deoptimization when the specified number of 732 // breaks have been hit. 733 if (break_point_hit_count == break_point_hit_count_deoptimize) { 734 i::Deoptimizer::DeoptimizeAll(); 735 } 736 } else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) { 737 const int argc = 1; 738 v8::Handle<v8::Value> argv[argc] = { event_data }; 739 v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state, 740 argc, argv); 741 if (result->IsUndefined()) { 742 last_script_data_hit[0] = '\0'; 743 } else { 744 result = result->ToString(); 745 CHECK(result->IsString()); 746 v8::Handle<v8::String> script_data(result->ToString()); 747 script_data->WriteAscii(last_script_data_hit); 748 } 749 } 750} 751 752 753// Debug event handler which counts a number of events and collects the stack 754// height if there is a function compiled for that. 755int exception_hit_count = 0; 756int uncaught_exception_hit_count = 0; 757int last_js_stack_height = -1; 758 759static void DebugEventCounterClear() { 760 break_point_hit_count = 0; 761 exception_hit_count = 0; 762 uncaught_exception_hit_count = 0; 763} 764 765static void DebugEventCounter(v8::DebugEvent event, 766 v8::Handle<v8::Object> exec_state, 767 v8::Handle<v8::Object> event_data, 768 v8::Handle<v8::Value> data) { 769 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 770 771 // When hitting a debug event listener there must be a break set. 772 CHECK_NE(debug->break_id(), 0); 773 774 // Count the number of breaks. 775 if (event == v8::Break) { 776 break_point_hit_count++; 777 } else if (event == v8::Exception) { 778 exception_hit_count++; 779 780 // Check whether the exception was uncaught. 781 v8::Local<v8::String> fun_name = v8::String::New("uncaught"); 782 v8::Local<v8::Function> fun = 783 v8::Function::Cast(*event_data->Get(fun_name)); 784 v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL); 785 if (result->IsTrue()) { 786 uncaught_exception_hit_count++; 787 } 788 } 789 790 // Collect the JavsScript stack height if the function frame_count is 791 // compiled. 792 if (!frame_count.IsEmpty()) { 793 static const int kArgc = 1; 794 v8::Handle<v8::Value> argv[kArgc] = { exec_state }; 795 // Using exec_state as receiver is just to have a receiver. 796 v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv); 797 last_js_stack_height = result->Int32Value(); 798 } 799} 800 801 802// Debug event handler which evaluates a number of expressions when a break 803// point is hit. Each evaluated expression is compared with an expected value. 804// For this debug event handler to work the following two global varaibles 805// must be initialized. 806// checks: An array of expressions and expected results 807// evaluate_check_function: A JavaScript function (see below) 808 809// Structure for holding checks to do. 810struct EvaluateCheck { 811 const char* expr; // An expression to evaluate when a break point is hit. 812 v8::Handle<v8::Value> expected; // The expected result. 813}; 814// Array of checks to do. 815struct EvaluateCheck* checks = NULL; 816// Source for The JavaScript function which can do the evaluation when a break 817// point is hit. 818const char* evaluate_check_source = 819 "function evaluate_check(exec_state, expr, expected) {" 820 " return exec_state.frame(0).evaluate(expr).value() === expected;" 821 "}"; 822v8::Local<v8::Function> evaluate_check_function; 823 824// The actual debug event described by the longer comment above. 825static void DebugEventEvaluate(v8::DebugEvent event, 826 v8::Handle<v8::Object> exec_state, 827 v8::Handle<v8::Object> event_data, 828 v8::Handle<v8::Value> data) { 829 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 830 // When hitting a debug event listener there must be a break set. 831 CHECK_NE(debug->break_id(), 0); 832 833 if (event == v8::Break) { 834 for (int i = 0; checks[i].expr != NULL; i++) { 835 const int argc = 3; 836 v8::Handle<v8::Value> argv[argc] = { exec_state, 837 v8::String::New(checks[i].expr), 838 checks[i].expected }; 839 v8::Handle<v8::Value> result = 840 evaluate_check_function->Call(exec_state, argc, argv); 841 if (!result->IsTrue()) { 842 v8::String::AsciiValue ascii(checks[i].expected->ToString()); 843 V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii); 844 } 845 } 846 } 847} 848 849 850// This debug event listener removes a breakpoint in a function 851int debug_event_remove_break_point = 0; 852static void DebugEventRemoveBreakPoint(v8::DebugEvent event, 853 v8::Handle<v8::Object> exec_state, 854 v8::Handle<v8::Object> event_data, 855 v8::Handle<v8::Value> data) { 856 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 857 // When hitting a debug event listener there must be a break set. 858 CHECK_NE(debug->break_id(), 0); 859 860 if (event == v8::Break) { 861 break_point_hit_count++; 862 v8::Handle<v8::Function> fun = v8::Handle<v8::Function>::Cast(data); 863 ClearBreakPoint(debug_event_remove_break_point); 864 } 865} 866 867 868// Debug event handler which counts break points hit and performs a step 869// afterwards. 870StepAction step_action = StepIn; // Step action to perform when stepping. 871static void DebugEventStep(v8::DebugEvent event, 872 v8::Handle<v8::Object> exec_state, 873 v8::Handle<v8::Object> event_data, 874 v8::Handle<v8::Value> data) { 875 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 876 // When hitting a debug event listener there must be a break set. 877 CHECK_NE(debug->break_id(), 0); 878 879 if (event == v8::Break) { 880 break_point_hit_count++; 881 PrepareStep(step_action); 882 } 883} 884 885 886// Debug event handler which counts break points hit and performs a step 887// afterwards. For each call the expected function is checked. 888// For this debug event handler to work the following two global varaibles 889// must be initialized. 890// expected_step_sequence: An array of the expected function call sequence. 891// frame_function_name: A JavaScript function (see below). 892 893// String containing the expected function call sequence. Note: this only works 894// if functions have name length of one. 895const char* expected_step_sequence = NULL; 896 897// The actual debug event described by the longer comment above. 898static void DebugEventStepSequence(v8::DebugEvent event, 899 v8::Handle<v8::Object> exec_state, 900 v8::Handle<v8::Object> event_data, 901 v8::Handle<v8::Value> data) { 902 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 903 // When hitting a debug event listener there must be a break set. 904 CHECK_NE(debug->break_id(), 0); 905 906 if (event == v8::Break || event == v8::Exception) { 907 // Check that the current function is the expected. 908 CHECK(break_point_hit_count < 909 StrLength(expected_step_sequence)); 910 const int argc = 2; 911 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; 912 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, 913 argc, argv); 914 CHECK(result->IsString()); 915 v8::String::AsciiValue function_name(result->ToString()); 916 CHECK_EQ(1, StrLength(*function_name)); 917 CHECK_EQ((*function_name)[0], 918 expected_step_sequence[break_point_hit_count]); 919 920 // Perform step. 921 break_point_hit_count++; 922 PrepareStep(step_action); 923 } 924} 925 926 927// Debug event handler which performs a garbage collection. 928static void DebugEventBreakPointCollectGarbage( 929 v8::DebugEvent event, 930 v8::Handle<v8::Object> exec_state, 931 v8::Handle<v8::Object> event_data, 932 v8::Handle<v8::Value> data) { 933 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 934 // When hitting a debug event listener there must be a break set. 935 CHECK_NE(debug->break_id(), 0); 936 937 // Perform a garbage collection when break point is hit and continue. Based 938 // on the number of break points hit either scavenge or mark compact 939 // collector is used. 940 if (event == v8::Break) { 941 break_point_hit_count++; 942 if (break_point_hit_count % 2 == 0) { 943 // Scavenge. 944 HEAP->CollectGarbage(v8::internal::NEW_SPACE); 945 } else { 946 // Mark sweep compact. 947 HEAP->CollectAllGarbage(true); 948 } 949 } 950} 951 952 953// Debug event handler which re-issues a debug break and calls the garbage 954// collector to have the heap verified. 955static void DebugEventBreak(v8::DebugEvent event, 956 v8::Handle<v8::Object> exec_state, 957 v8::Handle<v8::Object> event_data, 958 v8::Handle<v8::Value> data) { 959 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 960 // When hitting a debug event listener there must be a break set. 961 CHECK_NE(debug->break_id(), 0); 962 963 if (event == v8::Break) { 964 // Count the number of breaks. 965 break_point_hit_count++; 966 967 // Run the garbage collector to enforce heap verification if option 968 // --verify-heap is set. 969 HEAP->CollectGarbage(v8::internal::NEW_SPACE); 970 971 // Set the break flag again to come back here as soon as possible. 972 v8::Debug::DebugBreak(); 973 } 974} 975 976 977// Debug event handler which re-issues a debug break until a limit has been 978// reached. 979int max_break_point_hit_count = 0; 980bool terminate_after_max_break_point_hit = false; 981static void DebugEventBreakMax(v8::DebugEvent event, 982 v8::Handle<v8::Object> exec_state, 983 v8::Handle<v8::Object> event_data, 984 v8::Handle<v8::Value> data) { 985 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 986 // When hitting a debug event listener there must be a break set. 987 CHECK_NE(debug->break_id(), 0); 988 989 if (event == v8::Break) { 990 if (break_point_hit_count < max_break_point_hit_count) { 991 // Count the number of breaks. 992 break_point_hit_count++; 993 994 // Collect the JavsScript stack height if the function frame_count is 995 // compiled. 996 if (!frame_count.IsEmpty()) { 997 static const int kArgc = 1; 998 v8::Handle<v8::Value> argv[kArgc] = { exec_state }; 999 // Using exec_state as receiver is just to have a receiver. 1000 v8::Handle<v8::Value> result = 1001 frame_count->Call(exec_state, kArgc, argv); 1002 last_js_stack_height = result->Int32Value(); 1003 } 1004 1005 // Set the break flag again to come back here as soon as possible. 1006 v8::Debug::DebugBreak(); 1007 1008 } else if (terminate_after_max_break_point_hit) { 1009 // Terminate execution after the last break if requested. 1010 v8::V8::TerminateExecution(); 1011 } 1012 1013 // Perform a full deoptimization when the specified number of 1014 // breaks have been hit. 1015 if (break_point_hit_count == break_point_hit_count_deoptimize) { 1016 i::Deoptimizer::DeoptimizeAll(); 1017 } 1018 } 1019} 1020 1021 1022// --- M e s s a g e C a l l b a c k 1023 1024 1025// Message callback which counts the number of messages. 1026int message_callback_count = 0; 1027 1028static void MessageCallbackCountClear() { 1029 message_callback_count = 0; 1030} 1031 1032static void MessageCallbackCount(v8::Handle<v8::Message> message, 1033 v8::Handle<v8::Value> data) { 1034 message_callback_count++; 1035} 1036 1037 1038// --- T h e A c t u a l T e s t s 1039 1040 1041// Test that the debug break function is the expected one for different kinds 1042// of break locations. 1043TEST(DebugStub) { 1044 using ::v8::internal::Builtins; 1045 using ::v8::internal::Isolate; 1046 v8::HandleScope scope; 1047 DebugLocalContext env; 1048 1049 CheckDebugBreakFunction(&env, 1050 "function f1(){}", "f1", 1051 0, 1052 v8::internal::RelocInfo::JS_RETURN, 1053 NULL); 1054 CheckDebugBreakFunction(&env, 1055 "function f2(){x=1;}", "f2", 1056 0, 1057 v8::internal::RelocInfo::CODE_TARGET_CONTEXT, 1058 Isolate::Current()->builtins()->builtin( 1059 Builtins::kStoreIC_DebugBreak)); 1060 CheckDebugBreakFunction(&env, 1061 "function f3(){var a=x;}", "f3", 1062 0, 1063 v8::internal::RelocInfo::CODE_TARGET_CONTEXT, 1064 Isolate::Current()->builtins()->builtin( 1065 Builtins::kLoadIC_DebugBreak)); 1066 1067// TODO(1240753): Make the test architecture independent or split 1068// parts of the debugger into architecture dependent files. This 1069// part currently disabled as it is not portable between IA32/ARM. 1070// Currently on ICs for keyed store/load on ARM. 1071#if !defined (__arm__) && !defined(__thumb__) 1072 CheckDebugBreakFunction( 1073 &env, 1074 "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}", 1075 "f4", 1076 0, 1077 v8::internal::RelocInfo::CODE_TARGET, 1078 Isolate::Current()->builtins()->builtin( 1079 Builtins::kKeyedStoreIC_DebugBreak)); 1080 CheckDebugBreakFunction( 1081 &env, 1082 "function f5(){var index='propertyName'; var a={}; return a[index];}", 1083 "f5", 1084 0, 1085 v8::internal::RelocInfo::CODE_TARGET, 1086 Isolate::Current()->builtins()->builtin( 1087 Builtins::kKeyedLoadIC_DebugBreak)); 1088#endif 1089 1090 // Check the debug break code stubs for call ICs with different number of 1091 // parameters. 1092 Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0); 1093 Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1); 1094 Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4); 1095 1096 CheckDebugBreakFunction(&env, 1097 "function f4_0(){x();}", "f4_0", 1098 0, 1099 v8::internal::RelocInfo::CODE_TARGET_CONTEXT, 1100 *debug_break_0); 1101 1102 CheckDebugBreakFunction(&env, 1103 "function f4_1(){x(1);}", "f4_1", 1104 0, 1105 v8::internal::RelocInfo::CODE_TARGET_CONTEXT, 1106 *debug_break_1); 1107 1108 CheckDebugBreakFunction(&env, 1109 "function f4_4(){x(1,2,3,4);}", "f4_4", 1110 0, 1111 v8::internal::RelocInfo::CODE_TARGET_CONTEXT, 1112 *debug_break_4); 1113} 1114 1115 1116// Test that the debug info in the VM is in sync with the functions being 1117// debugged. 1118TEST(DebugInfo) { 1119 v8::HandleScope scope; 1120 DebugLocalContext env; 1121 // Create a couple of functions for the test. 1122 v8::Local<v8::Function> foo = 1123 CompileFunction(&env, "function foo(){}", "foo"); 1124 v8::Local<v8::Function> bar = 1125 CompileFunction(&env, "function bar(){}", "bar"); 1126 // Initially no functions are debugged. 1127 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); 1128 CHECK(!HasDebugInfo(foo)); 1129 CHECK(!HasDebugInfo(bar)); 1130 // One function (foo) is debugged. 1131 int bp1 = SetBreakPoint(foo, 0); 1132 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); 1133 CHECK(HasDebugInfo(foo)); 1134 CHECK(!HasDebugInfo(bar)); 1135 // Two functions are debugged. 1136 int bp2 = SetBreakPoint(bar, 0); 1137 CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); 1138 CHECK(HasDebugInfo(foo)); 1139 CHECK(HasDebugInfo(bar)); 1140 // One function (bar) is debugged. 1141 ClearBreakPoint(bp1); 1142 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); 1143 CHECK(!HasDebugInfo(foo)); 1144 CHECK(HasDebugInfo(bar)); 1145 // No functions are debugged. 1146 ClearBreakPoint(bp2); 1147 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); 1148 CHECK(!HasDebugInfo(foo)); 1149 CHECK(!HasDebugInfo(bar)); 1150} 1151 1152 1153// Test that a break point can be set at an IC store location. 1154TEST(BreakPointICStore) { 1155 break_point_hit_count = 0; 1156 v8::HandleScope scope; 1157 DebugLocalContext env; 1158 1159 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1160 v8::Undefined()); 1161 v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run(); 1162 v8::Local<v8::Function> foo = 1163 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1164 1165 // Run without breakpoints. 1166 foo->Call(env->Global(), 0, NULL); 1167 CHECK_EQ(0, break_point_hit_count); 1168 1169 // Run with breakpoint 1170 int bp = SetBreakPoint(foo, 0); 1171 foo->Call(env->Global(), 0, NULL); 1172 CHECK_EQ(1, break_point_hit_count); 1173 foo->Call(env->Global(), 0, NULL); 1174 CHECK_EQ(2, break_point_hit_count); 1175 1176 // Run without breakpoints. 1177 ClearBreakPoint(bp); 1178 foo->Call(env->Global(), 0, NULL); 1179 CHECK_EQ(2, break_point_hit_count); 1180 1181 v8::Debug::SetDebugEventListener(NULL); 1182 CheckDebuggerUnloaded(); 1183} 1184 1185 1186// Test that a break point can be set at an IC load location. 1187TEST(BreakPointICLoad) { 1188 break_point_hit_count = 0; 1189 v8::HandleScope scope; 1190 DebugLocalContext env; 1191 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1192 v8::Undefined()); 1193 v8::Script::Compile(v8::String::New("bar=1"))->Run(); 1194 v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run(); 1195 v8::Local<v8::Function> foo = 1196 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1197 1198 // Run without breakpoints. 1199 foo->Call(env->Global(), 0, NULL); 1200 CHECK_EQ(0, break_point_hit_count); 1201 1202 // Run with breakpoint. 1203 int bp = SetBreakPoint(foo, 0); 1204 foo->Call(env->Global(), 0, NULL); 1205 CHECK_EQ(1, break_point_hit_count); 1206 foo->Call(env->Global(), 0, NULL); 1207 CHECK_EQ(2, break_point_hit_count); 1208 1209 // Run without breakpoints. 1210 ClearBreakPoint(bp); 1211 foo->Call(env->Global(), 0, NULL); 1212 CHECK_EQ(2, break_point_hit_count); 1213 1214 v8::Debug::SetDebugEventListener(NULL); 1215 CheckDebuggerUnloaded(); 1216} 1217 1218 1219// Test that a break point can be set at an IC call location. 1220TEST(BreakPointICCall) { 1221 break_point_hit_count = 0; 1222 v8::HandleScope scope; 1223 DebugLocalContext env; 1224 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1225 v8::Undefined()); 1226 v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); 1227 v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run(); 1228 v8::Local<v8::Function> foo = 1229 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1230 1231 // Run without breakpoints. 1232 foo->Call(env->Global(), 0, NULL); 1233 CHECK_EQ(0, break_point_hit_count); 1234 1235 // Run with breakpoint 1236 int bp = SetBreakPoint(foo, 0); 1237 foo->Call(env->Global(), 0, NULL); 1238 CHECK_EQ(1, break_point_hit_count); 1239 foo->Call(env->Global(), 0, NULL); 1240 CHECK_EQ(2, break_point_hit_count); 1241 1242 // Run without breakpoints. 1243 ClearBreakPoint(bp); 1244 foo->Call(env->Global(), 0, NULL); 1245 CHECK_EQ(2, break_point_hit_count); 1246 1247 v8::Debug::SetDebugEventListener(NULL); 1248 CheckDebuggerUnloaded(); 1249} 1250 1251 1252// Test that a break point can be set at an IC call location and survive a GC. 1253TEST(BreakPointICCallWithGC) { 1254 break_point_hit_count = 0; 1255 v8::HandleScope scope; 1256 DebugLocalContext env; 1257 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, 1258 v8::Undefined()); 1259 v8::Script::Compile(v8::String::New("function bar(){return 1;}"))->Run(); 1260 v8::Script::Compile(v8::String::New("function foo(){return bar();}"))->Run(); 1261 v8::Local<v8::Function> foo = 1262 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1263 1264 // Run without breakpoints. 1265 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1266 CHECK_EQ(0, break_point_hit_count); 1267 1268 // Run with breakpoint. 1269 int bp = SetBreakPoint(foo, 0); 1270 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1271 CHECK_EQ(1, break_point_hit_count); 1272 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1273 CHECK_EQ(2, break_point_hit_count); 1274 1275 // Run without breakpoints. 1276 ClearBreakPoint(bp); 1277 foo->Call(env->Global(), 0, NULL); 1278 CHECK_EQ(2, break_point_hit_count); 1279 1280 v8::Debug::SetDebugEventListener(NULL); 1281 CheckDebuggerUnloaded(); 1282} 1283 1284 1285// Test that a break point can be set at an IC call location and survive a GC. 1286TEST(BreakPointConstructCallWithGC) { 1287 break_point_hit_count = 0; 1288 v8::HandleScope scope; 1289 DebugLocalContext env; 1290 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, 1291 v8::Undefined()); 1292 v8::Script::Compile(v8::String::New("function bar(){ this.x = 1;}"))->Run(); 1293 v8::Script::Compile(v8::String::New( 1294 "function foo(){return new bar(1).x;}"))->Run(); 1295 v8::Local<v8::Function> foo = 1296 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1297 1298 // Run without breakpoints. 1299 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1300 CHECK_EQ(0, break_point_hit_count); 1301 1302 // Run with breakpoint. 1303 int bp = SetBreakPoint(foo, 0); 1304 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1305 CHECK_EQ(1, break_point_hit_count); 1306 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value()); 1307 CHECK_EQ(2, break_point_hit_count); 1308 1309 // Run without breakpoints. 1310 ClearBreakPoint(bp); 1311 foo->Call(env->Global(), 0, NULL); 1312 CHECK_EQ(2, break_point_hit_count); 1313 1314 v8::Debug::SetDebugEventListener(NULL); 1315 CheckDebuggerUnloaded(); 1316} 1317 1318 1319// Test that a break point can be set at a return store location. 1320TEST(BreakPointReturn) { 1321 break_point_hit_count = 0; 1322 v8::HandleScope scope; 1323 DebugLocalContext env; 1324 1325 // Create a functions for checking the source line and column when hitting 1326 // a break point. 1327 frame_source_line = CompileFunction(&env, 1328 frame_source_line_source, 1329 "frame_source_line"); 1330 frame_source_column = CompileFunction(&env, 1331 frame_source_column_source, 1332 "frame_source_column"); 1333 1334 1335 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1336 v8::Undefined()); 1337 v8::Script::Compile(v8::String::New("function foo(){}"))->Run(); 1338 v8::Local<v8::Function> foo = 1339 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 1340 1341 // Run without breakpoints. 1342 foo->Call(env->Global(), 0, NULL); 1343 CHECK_EQ(0, break_point_hit_count); 1344 1345 // Run with breakpoint 1346 int bp = SetBreakPoint(foo, 0); 1347 foo->Call(env->Global(), 0, NULL); 1348 CHECK_EQ(1, break_point_hit_count); 1349 CHECK_EQ(0, last_source_line); 1350 CHECK_EQ(15, last_source_column); 1351 foo->Call(env->Global(), 0, NULL); 1352 CHECK_EQ(2, break_point_hit_count); 1353 CHECK_EQ(0, last_source_line); 1354 CHECK_EQ(15, last_source_column); 1355 1356 // Run without breakpoints. 1357 ClearBreakPoint(bp); 1358 foo->Call(env->Global(), 0, NULL); 1359 CHECK_EQ(2, break_point_hit_count); 1360 1361 v8::Debug::SetDebugEventListener(NULL); 1362 CheckDebuggerUnloaded(); 1363} 1364 1365 1366static void CallWithBreakPoints(v8::Local<v8::Object> recv, 1367 v8::Local<v8::Function> f, 1368 int break_point_count, 1369 int call_count) { 1370 break_point_hit_count = 0; 1371 for (int i = 0; i < call_count; i++) { 1372 f->Call(recv, 0, NULL); 1373 CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); 1374 } 1375} 1376 1377// Test GC during break point processing. 1378TEST(GCDuringBreakPointProcessing) { 1379 break_point_hit_count = 0; 1380 v8::HandleScope scope; 1381 DebugLocalContext env; 1382 1383 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage, 1384 v8::Undefined()); 1385 v8::Local<v8::Function> foo; 1386 1387 // Test IC store break point with garbage collection. 1388 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); 1389 SetBreakPoint(foo, 0); 1390 CallWithBreakPoints(env->Global(), foo, 1, 10); 1391 1392 // Test IC load break point with garbage collection. 1393 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); 1394 SetBreakPoint(foo, 0); 1395 CallWithBreakPoints(env->Global(), foo, 1, 10); 1396 1397 // Test IC call break point with garbage collection. 1398 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); 1399 SetBreakPoint(foo, 0); 1400 CallWithBreakPoints(env->Global(), foo, 1, 10); 1401 1402 // Test return break point with garbage collection. 1403 foo = CompileFunction(&env, "function foo(){}", "foo"); 1404 SetBreakPoint(foo, 0); 1405 CallWithBreakPoints(env->Global(), foo, 1, 25); 1406 1407 // Test debug break slot break point with garbage collection. 1408 foo = CompileFunction(&env, "function foo(){var a;}", "foo"); 1409 SetBreakPoint(foo, 0); 1410 CallWithBreakPoints(env->Global(), foo, 1, 25); 1411 1412 v8::Debug::SetDebugEventListener(NULL); 1413 CheckDebuggerUnloaded(); 1414} 1415 1416 1417// Call the function three times with different garbage collections in between 1418// and make sure that the break point survives. 1419static void CallAndGC(v8::Local<v8::Object> recv, 1420 v8::Local<v8::Function> f, 1421 bool force_compaction) { 1422 break_point_hit_count = 0; 1423 1424 for (int i = 0; i < 3; i++) { 1425 // Call function. 1426 f->Call(recv, 0, NULL); 1427 CHECK_EQ(1 + i * 3, break_point_hit_count); 1428 1429 // Scavenge and call function. 1430 HEAP->CollectGarbage(v8::internal::NEW_SPACE); 1431 f->Call(recv, 0, NULL); 1432 CHECK_EQ(2 + i * 3, break_point_hit_count); 1433 1434 // Mark sweep (and perhaps compact) and call function. 1435 HEAP->CollectAllGarbage(force_compaction); 1436 f->Call(recv, 0, NULL); 1437 CHECK_EQ(3 + i * 3, break_point_hit_count); 1438 } 1439} 1440 1441 1442static void TestBreakPointSurviveGC(bool force_compaction) { 1443 break_point_hit_count = 0; 1444 v8::HandleScope scope; 1445 DebugLocalContext env; 1446 1447 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1448 v8::Undefined()); 1449 v8::Local<v8::Function> foo; 1450 1451 // Test IC store break point with garbage collection. 1452 { 1453 v8::Local<v8::Function> bar = 1454 CompileFunction(&env, "function foo(){}", "foo"); 1455 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); 1456 SetBreakPoint(foo, 0); 1457 } 1458 CallAndGC(env->Global(), foo, force_compaction); 1459 1460 // Test IC load break point with garbage collection. 1461 { 1462 v8::Local<v8::Function> bar = 1463 CompileFunction(&env, "function foo(){}", "foo"); 1464 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); 1465 SetBreakPoint(foo, 0); 1466 } 1467 CallAndGC(env->Global(), foo, force_compaction); 1468 1469 // Test IC call break point with garbage collection. 1470 { 1471 v8::Local<v8::Function> bar = 1472 CompileFunction(&env, "function foo(){}", "foo"); 1473 foo = CompileFunction(&env, 1474 "function bar(){};function foo(){bar();}", 1475 "foo"); 1476 SetBreakPoint(foo, 0); 1477 } 1478 CallAndGC(env->Global(), foo, force_compaction); 1479 1480 // Test return break point with garbage collection. 1481 { 1482 v8::Local<v8::Function> bar = 1483 CompileFunction(&env, "function foo(){}", "foo"); 1484 foo = CompileFunction(&env, "function foo(){}", "foo"); 1485 SetBreakPoint(foo, 0); 1486 } 1487 CallAndGC(env->Global(), foo, force_compaction); 1488 1489 // Test non IC break point with garbage collection. 1490 { 1491 v8::Local<v8::Function> bar = 1492 CompileFunction(&env, "function foo(){}", "foo"); 1493 foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); 1494 SetBreakPoint(foo, 0); 1495 } 1496 CallAndGC(env->Global(), foo, force_compaction); 1497 1498 1499 v8::Debug::SetDebugEventListener(NULL); 1500 CheckDebuggerUnloaded(); 1501} 1502 1503 1504// Test that a break point can be set at a return store location. 1505TEST(BreakPointSurviveGC) { 1506 TestBreakPointSurviveGC(false); 1507 TestBreakPointSurviveGC(true); 1508} 1509 1510 1511// Test that break points can be set using the global Debug object. 1512TEST(BreakPointThroughJavaScript) { 1513 break_point_hit_count = 0; 1514 v8::HandleScope scope; 1515 DebugLocalContext env; 1516 env.ExposeDebug(); 1517 1518 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1519 v8::Undefined()); 1520 v8::Script::Compile(v8::String::New("function bar(){}"))->Run(); 1521 v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run(); 1522 // 012345678901234567890 1523 // 1 2 1524 // Break points are set at position 3 and 9 1525 v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()")); 1526 1527 // Run without breakpoints. 1528 foo->Run(); 1529 CHECK_EQ(0, break_point_hit_count); 1530 1531 // Run with one breakpoint 1532 int bp1 = SetBreakPointFromJS("foo", 0, 3); 1533 foo->Run(); 1534 CHECK_EQ(1, break_point_hit_count); 1535 foo->Run(); 1536 CHECK_EQ(2, break_point_hit_count); 1537 1538 // Run with two breakpoints 1539 int bp2 = SetBreakPointFromJS("foo", 0, 9); 1540 foo->Run(); 1541 CHECK_EQ(4, break_point_hit_count); 1542 foo->Run(); 1543 CHECK_EQ(6, break_point_hit_count); 1544 1545 // Run with one breakpoint 1546 ClearBreakPointFromJS(bp2); 1547 foo->Run(); 1548 CHECK_EQ(7, break_point_hit_count); 1549 foo->Run(); 1550 CHECK_EQ(8, break_point_hit_count); 1551 1552 // Run without breakpoints. 1553 ClearBreakPointFromJS(bp1); 1554 foo->Run(); 1555 CHECK_EQ(8, break_point_hit_count); 1556 1557 v8::Debug::SetDebugEventListener(NULL); 1558 CheckDebuggerUnloaded(); 1559 1560 // Make sure that the break point numbers are consecutive. 1561 CHECK_EQ(1, bp1); 1562 CHECK_EQ(2, bp2); 1563} 1564 1565 1566// Test that break points on scripts identified by name can be set using the 1567// global Debug object. 1568TEST(ScriptBreakPointByNameThroughJavaScript) { 1569 break_point_hit_count = 0; 1570 v8::HandleScope scope; 1571 DebugLocalContext env; 1572 env.ExposeDebug(); 1573 1574 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1575 v8::Undefined()); 1576 1577 v8::Local<v8::String> script = v8::String::New( 1578 "function f() {\n" 1579 " function h() {\n" 1580 " a = 0; // line 2\n" 1581 " }\n" 1582 " b = 1; // line 4\n" 1583 " return h();\n" 1584 "}\n" 1585 "\n" 1586 "function g() {\n" 1587 " function h() {\n" 1588 " a = 0;\n" 1589 " }\n" 1590 " b = 2; // line 12\n" 1591 " h();\n" 1592 " b = 3; // line 14\n" 1593 " f(); // line 15\n" 1594 "}"); 1595 1596 // Compile the script and get the two functions. 1597 v8::ScriptOrigin origin = 1598 v8::ScriptOrigin(v8::String::New("test")); 1599 v8::Script::Compile(script, &origin)->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 // Call f and g without break points. 1606 break_point_hit_count = 0; 1607 f->Call(env->Global(), 0, NULL); 1608 CHECK_EQ(0, break_point_hit_count); 1609 g->Call(env->Global(), 0, NULL); 1610 CHECK_EQ(0, break_point_hit_count); 1611 1612 // Call f and g with break point on line 12. 1613 int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0); 1614 break_point_hit_count = 0; 1615 f->Call(env->Global(), 0, NULL); 1616 CHECK_EQ(0, break_point_hit_count); 1617 g->Call(env->Global(), 0, NULL); 1618 CHECK_EQ(1, break_point_hit_count); 1619 1620 // Remove the break point again. 1621 break_point_hit_count = 0; 1622 ClearBreakPointFromJS(sbp1); 1623 f->Call(env->Global(), 0, NULL); 1624 CHECK_EQ(0, break_point_hit_count); 1625 g->Call(env->Global(), 0, NULL); 1626 CHECK_EQ(0, break_point_hit_count); 1627 1628 // Call f and g with break point on line 2. 1629 int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0); 1630 break_point_hit_count = 0; 1631 f->Call(env->Global(), 0, NULL); 1632 CHECK_EQ(1, break_point_hit_count); 1633 g->Call(env->Global(), 0, NULL); 1634 CHECK_EQ(2, break_point_hit_count); 1635 1636 // Call f and g with break point on line 2, 4, 12, 14 and 15. 1637 int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0); 1638 int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0); 1639 int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0); 1640 int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0); 1641 break_point_hit_count = 0; 1642 f->Call(env->Global(), 0, NULL); 1643 CHECK_EQ(2, break_point_hit_count); 1644 g->Call(env->Global(), 0, NULL); 1645 CHECK_EQ(7, break_point_hit_count); 1646 1647 // Remove all the break points again. 1648 break_point_hit_count = 0; 1649 ClearBreakPointFromJS(sbp2); 1650 ClearBreakPointFromJS(sbp3); 1651 ClearBreakPointFromJS(sbp4); 1652 ClearBreakPointFromJS(sbp5); 1653 ClearBreakPointFromJS(sbp6); 1654 f->Call(env->Global(), 0, NULL); 1655 CHECK_EQ(0, break_point_hit_count); 1656 g->Call(env->Global(), 0, NULL); 1657 CHECK_EQ(0, break_point_hit_count); 1658 1659 v8::Debug::SetDebugEventListener(NULL); 1660 CheckDebuggerUnloaded(); 1661 1662 // Make sure that the break point numbers are consecutive. 1663 CHECK_EQ(1, sbp1); 1664 CHECK_EQ(2, sbp2); 1665 CHECK_EQ(3, sbp3); 1666 CHECK_EQ(4, sbp4); 1667 CHECK_EQ(5, sbp5); 1668 CHECK_EQ(6, sbp6); 1669} 1670 1671 1672TEST(ScriptBreakPointByIdThroughJavaScript) { 1673 break_point_hit_count = 0; 1674 v8::HandleScope scope; 1675 DebugLocalContext env; 1676 env.ExposeDebug(); 1677 1678 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1679 v8::Undefined()); 1680 1681 v8::Local<v8::String> source = v8::String::New( 1682 "function f() {\n" 1683 " function h() {\n" 1684 " a = 0; // line 2\n" 1685 " }\n" 1686 " b = 1; // line 4\n" 1687 " return h();\n" 1688 "}\n" 1689 "\n" 1690 "function g() {\n" 1691 " function h() {\n" 1692 " a = 0;\n" 1693 " }\n" 1694 " b = 2; // line 12\n" 1695 " h();\n" 1696 " b = 3; // line 14\n" 1697 " f(); // line 15\n" 1698 "}"); 1699 1700 // Compile the script and get the two functions. 1701 v8::ScriptOrigin origin = 1702 v8::ScriptOrigin(v8::String::New("test")); 1703 v8::Local<v8::Script> script = v8::Script::Compile(source, &origin); 1704 script->Run(); 1705 v8::Local<v8::Function> f = 1706 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1707 v8::Local<v8::Function> g = 1708 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); 1709 1710 // Get the script id knowing that internally it is a 32 integer. 1711 uint32_t script_id = script->Id()->Uint32Value(); 1712 1713 // Call f and g without break points. 1714 break_point_hit_count = 0; 1715 f->Call(env->Global(), 0, NULL); 1716 CHECK_EQ(0, break_point_hit_count); 1717 g->Call(env->Global(), 0, NULL); 1718 CHECK_EQ(0, break_point_hit_count); 1719 1720 // Call f and g with break point on line 12. 1721 int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); 1722 break_point_hit_count = 0; 1723 f->Call(env->Global(), 0, NULL); 1724 CHECK_EQ(0, break_point_hit_count); 1725 g->Call(env->Global(), 0, NULL); 1726 CHECK_EQ(1, break_point_hit_count); 1727 1728 // Remove the break point again. 1729 break_point_hit_count = 0; 1730 ClearBreakPointFromJS(sbp1); 1731 f->Call(env->Global(), 0, NULL); 1732 CHECK_EQ(0, break_point_hit_count); 1733 g->Call(env->Global(), 0, NULL); 1734 CHECK_EQ(0, break_point_hit_count); 1735 1736 // Call f and g with break point on line 2. 1737 int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0); 1738 break_point_hit_count = 0; 1739 f->Call(env->Global(), 0, NULL); 1740 CHECK_EQ(1, break_point_hit_count); 1741 g->Call(env->Global(), 0, NULL); 1742 CHECK_EQ(2, break_point_hit_count); 1743 1744 // Call f and g with break point on line 2, 4, 12, 14 and 15. 1745 int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0); 1746 int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0); 1747 int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0); 1748 int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0); 1749 break_point_hit_count = 0; 1750 f->Call(env->Global(), 0, NULL); 1751 CHECK_EQ(2, break_point_hit_count); 1752 g->Call(env->Global(), 0, NULL); 1753 CHECK_EQ(7, break_point_hit_count); 1754 1755 // Remove all the break points again. 1756 break_point_hit_count = 0; 1757 ClearBreakPointFromJS(sbp2); 1758 ClearBreakPointFromJS(sbp3); 1759 ClearBreakPointFromJS(sbp4); 1760 ClearBreakPointFromJS(sbp5); 1761 ClearBreakPointFromJS(sbp6); 1762 f->Call(env->Global(), 0, NULL); 1763 CHECK_EQ(0, break_point_hit_count); 1764 g->Call(env->Global(), 0, NULL); 1765 CHECK_EQ(0, break_point_hit_count); 1766 1767 v8::Debug::SetDebugEventListener(NULL); 1768 CheckDebuggerUnloaded(); 1769 1770 // Make sure that the break point numbers are consecutive. 1771 CHECK_EQ(1, sbp1); 1772 CHECK_EQ(2, sbp2); 1773 CHECK_EQ(3, sbp3); 1774 CHECK_EQ(4, sbp4); 1775 CHECK_EQ(5, sbp5); 1776 CHECK_EQ(6, sbp6); 1777} 1778 1779 1780// Test conditional script break points. 1781TEST(EnableDisableScriptBreakPoint) { 1782 break_point_hit_count = 0; 1783 v8::HandleScope scope; 1784 DebugLocalContext env; 1785 env.ExposeDebug(); 1786 1787 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1788 v8::Undefined()); 1789 1790 v8::Local<v8::String> script = v8::String::New( 1791 "function f() {\n" 1792 " a = 0; // line 1\n" 1793 "};"); 1794 1795 // Compile the script and get function f. 1796 v8::ScriptOrigin origin = 1797 v8::ScriptOrigin(v8::String::New("test")); 1798 v8::Script::Compile(script, &origin)->Run(); 1799 v8::Local<v8::Function> f = 1800 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1801 1802 // Set script break point on line 1 (in function f). 1803 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); 1804 1805 // Call f while enabeling and disabling the script break point. 1806 break_point_hit_count = 0; 1807 f->Call(env->Global(), 0, NULL); 1808 CHECK_EQ(1, break_point_hit_count); 1809 1810 DisableScriptBreakPointFromJS(sbp); 1811 f->Call(env->Global(), 0, NULL); 1812 CHECK_EQ(1, break_point_hit_count); 1813 1814 EnableScriptBreakPointFromJS(sbp); 1815 f->Call(env->Global(), 0, NULL); 1816 CHECK_EQ(2, break_point_hit_count); 1817 1818 DisableScriptBreakPointFromJS(sbp); 1819 f->Call(env->Global(), 0, NULL); 1820 CHECK_EQ(2, break_point_hit_count); 1821 1822 // Reload the script and get f again checking that the disabeling survives. 1823 v8::Script::Compile(script, &origin)->Run(); 1824 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1825 f->Call(env->Global(), 0, NULL); 1826 CHECK_EQ(2, break_point_hit_count); 1827 1828 EnableScriptBreakPointFromJS(sbp); 1829 f->Call(env->Global(), 0, NULL); 1830 CHECK_EQ(3, break_point_hit_count); 1831 1832 v8::Debug::SetDebugEventListener(NULL); 1833 CheckDebuggerUnloaded(); 1834} 1835 1836 1837// Test conditional script break points. 1838TEST(ConditionalScriptBreakPoint) { 1839 break_point_hit_count = 0; 1840 v8::HandleScope scope; 1841 DebugLocalContext env; 1842 env.ExposeDebug(); 1843 1844 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1845 v8::Undefined()); 1846 1847 v8::Local<v8::String> script = v8::String::New( 1848 "count = 0;\n" 1849 "function f() {\n" 1850 " g(count++); // line 2\n" 1851 "};\n" 1852 "function g(x) {\n" 1853 " var a=x; // line 5\n" 1854 "};"); 1855 1856 // Compile the script and get function f. 1857 v8::ScriptOrigin origin = 1858 v8::ScriptOrigin(v8::String::New("test")); 1859 v8::Script::Compile(script, &origin)->Run(); 1860 v8::Local<v8::Function> f = 1861 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1862 1863 // Set script break point on line 5 (in function g). 1864 int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0); 1865 1866 // Call f with different conditions on the script break point. 1867 break_point_hit_count = 0; 1868 ChangeScriptBreakPointConditionFromJS(sbp1, "false"); 1869 f->Call(env->Global(), 0, NULL); 1870 CHECK_EQ(0, break_point_hit_count); 1871 1872 ChangeScriptBreakPointConditionFromJS(sbp1, "true"); 1873 break_point_hit_count = 0; 1874 f->Call(env->Global(), 0, NULL); 1875 CHECK_EQ(1, break_point_hit_count); 1876 1877 ChangeScriptBreakPointConditionFromJS(sbp1, "x % 2 == 0"); 1878 break_point_hit_count = 0; 1879 for (int i = 0; i < 10; i++) { 1880 f->Call(env->Global(), 0, NULL); 1881 } 1882 CHECK_EQ(5, break_point_hit_count); 1883 1884 // Reload the script and get f again checking that the condition survives. 1885 v8::Script::Compile(script, &origin)->Run(); 1886 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1887 1888 break_point_hit_count = 0; 1889 for (int i = 0; i < 10; i++) { 1890 f->Call(env->Global(), 0, NULL); 1891 } 1892 CHECK_EQ(5, break_point_hit_count); 1893 1894 v8::Debug::SetDebugEventListener(NULL); 1895 CheckDebuggerUnloaded(); 1896} 1897 1898 1899// Test ignore count on script break points. 1900TEST(ScriptBreakPointIgnoreCount) { 1901 break_point_hit_count = 0; 1902 v8::HandleScope scope; 1903 DebugLocalContext env; 1904 env.ExposeDebug(); 1905 1906 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1907 v8::Undefined()); 1908 1909 v8::Local<v8::String> script = v8::String::New( 1910 "function f() {\n" 1911 " a = 0; // line 1\n" 1912 "};"); 1913 1914 // Compile the script and get function f. 1915 v8::ScriptOrigin origin = 1916 v8::ScriptOrigin(v8::String::New("test")); 1917 v8::Script::Compile(script, &origin)->Run(); 1918 v8::Local<v8::Function> f = 1919 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1920 1921 // Set script break point on line 1 (in function f). 1922 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); 1923 1924 // Call f with different ignores on the script break point. 1925 break_point_hit_count = 0; 1926 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1); 1927 f->Call(env->Global(), 0, NULL); 1928 CHECK_EQ(0, break_point_hit_count); 1929 f->Call(env->Global(), 0, NULL); 1930 CHECK_EQ(1, break_point_hit_count); 1931 1932 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5); 1933 break_point_hit_count = 0; 1934 for (int i = 0; i < 10; i++) { 1935 f->Call(env->Global(), 0, NULL); 1936 } 1937 CHECK_EQ(5, break_point_hit_count); 1938 1939 // Reload the script and get f again checking that the ignore survives. 1940 v8::Script::Compile(script, &origin)->Run(); 1941 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1942 1943 break_point_hit_count = 0; 1944 for (int i = 0; i < 10; i++) { 1945 f->Call(env->Global(), 0, NULL); 1946 } 1947 CHECK_EQ(5, break_point_hit_count); 1948 1949 v8::Debug::SetDebugEventListener(NULL); 1950 CheckDebuggerUnloaded(); 1951} 1952 1953 1954// Test that script break points survive when a script is reloaded. 1955TEST(ScriptBreakPointReload) { 1956 break_point_hit_count = 0; 1957 v8::HandleScope scope; 1958 DebugLocalContext env; 1959 env.ExposeDebug(); 1960 1961 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 1962 v8::Undefined()); 1963 1964 v8::Local<v8::Function> f; 1965 v8::Local<v8::String> script = v8::String::New( 1966 "function f() {\n" 1967 " function h() {\n" 1968 " a = 0; // line 2\n" 1969 " }\n" 1970 " b = 1; // line 4\n" 1971 " return h();\n" 1972 "}"); 1973 1974 v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1")); 1975 v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2")); 1976 1977 // Set a script break point before the script is loaded. 1978 SetScriptBreakPointByNameFromJS("1", 2, 0); 1979 1980 // Compile the script and get the function. 1981 v8::Script::Compile(script, &origin_1)->Run(); 1982 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1983 1984 // Call f and check that the script break point is active. 1985 break_point_hit_count = 0; 1986 f->Call(env->Global(), 0, NULL); 1987 CHECK_EQ(1, break_point_hit_count); 1988 1989 // Compile the script again with a different script data and get the 1990 // function. 1991 v8::Script::Compile(script, &origin_2)->Run(); 1992 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 1993 1994 // Call f and check that no break points are set. 1995 break_point_hit_count = 0; 1996 f->Call(env->Global(), 0, NULL); 1997 CHECK_EQ(0, break_point_hit_count); 1998 1999 // Compile the script again and get the function. 2000 v8::Script::Compile(script, &origin_1)->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(1, break_point_hit_count); 2007 2008 v8::Debug::SetDebugEventListener(NULL); 2009 CheckDebuggerUnloaded(); 2010} 2011 2012 2013// Test when several scripts has the same script data 2014TEST(ScriptBreakPointMultiple) { 2015 break_point_hit_count = 0; 2016 v8::HandleScope scope; 2017 DebugLocalContext env; 2018 env.ExposeDebug(); 2019 2020 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2021 v8::Undefined()); 2022 2023 v8::Local<v8::Function> f; 2024 v8::Local<v8::String> script_f = v8::String::New( 2025 "function f() {\n" 2026 " a = 0; // line 1\n" 2027 "}"); 2028 2029 v8::Local<v8::Function> g; 2030 v8::Local<v8::String> script_g = v8::String::New( 2031 "function g() {\n" 2032 " b = 0; // line 1\n" 2033 "}"); 2034 2035 v8::ScriptOrigin origin = 2036 v8::ScriptOrigin(v8::String::New("test")); 2037 2038 // Set a script break point before the scripts are loaded. 2039 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); 2040 2041 // Compile the scripts with same script data and get the functions. 2042 v8::Script::Compile(script_f, &origin)->Run(); 2043 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 2044 v8::Script::Compile(script_g, &origin)->Run(); 2045 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); 2046 2047 // Call f and g and check that the script break point is active. 2048 break_point_hit_count = 0; 2049 f->Call(env->Global(), 0, NULL); 2050 CHECK_EQ(1, break_point_hit_count); 2051 g->Call(env->Global(), 0, NULL); 2052 CHECK_EQ(2, break_point_hit_count); 2053 2054 // Clear the script break point. 2055 ClearBreakPointFromJS(sbp); 2056 2057 // Call f and g and check that the script break point is no longer active. 2058 break_point_hit_count = 0; 2059 f->Call(env->Global(), 0, NULL); 2060 CHECK_EQ(0, break_point_hit_count); 2061 g->Call(env->Global(), 0, NULL); 2062 CHECK_EQ(0, break_point_hit_count); 2063 2064 // Set script break point with the scripts loaded. 2065 sbp = SetScriptBreakPointByNameFromJS("test", 1, 0); 2066 2067 // Call f and g and check that the script break point is active. 2068 break_point_hit_count = 0; 2069 f->Call(env->Global(), 0, NULL); 2070 CHECK_EQ(1, break_point_hit_count); 2071 g->Call(env->Global(), 0, NULL); 2072 CHECK_EQ(2, break_point_hit_count); 2073 2074 v8::Debug::SetDebugEventListener(NULL); 2075 CheckDebuggerUnloaded(); 2076} 2077 2078 2079// Test the script origin which has both name and line offset. 2080TEST(ScriptBreakPointLineOffset) { 2081 break_point_hit_count = 0; 2082 v8::HandleScope scope; 2083 DebugLocalContext env; 2084 env.ExposeDebug(); 2085 2086 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2087 v8::Undefined()); 2088 2089 v8::Local<v8::Function> f; 2090 v8::Local<v8::String> script = v8::String::New( 2091 "function f() {\n" 2092 " a = 0; // line 8 as this script has line offset 7\n" 2093 " b = 0; // line 9 as this script has line offset 7\n" 2094 "}"); 2095 2096 // Create script origin both name and line offset. 2097 v8::ScriptOrigin origin(v8::String::New("test.html"), 2098 v8::Integer::New(7)); 2099 2100 // Set two script break points before the script is loaded. 2101 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0); 2102 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); 2103 2104 // Compile the script and get the function. 2105 v8::Script::Compile(script, &origin)->Run(); 2106 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 2107 2108 // Call f and check that the script break point is active. 2109 break_point_hit_count = 0; 2110 f->Call(env->Global(), 0, NULL); 2111 CHECK_EQ(2, break_point_hit_count); 2112 2113 // Clear the script break points. 2114 ClearBreakPointFromJS(sbp1); 2115 ClearBreakPointFromJS(sbp2); 2116 2117 // Call f and check that no script break points are active. 2118 break_point_hit_count = 0; 2119 f->Call(env->Global(), 0, NULL); 2120 CHECK_EQ(0, break_point_hit_count); 2121 2122 // Set a script break point with the script loaded. 2123 sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0); 2124 2125 // Call f and check that the script break point is active. 2126 break_point_hit_count = 0; 2127 f->Call(env->Global(), 0, NULL); 2128 CHECK_EQ(1, break_point_hit_count); 2129 2130 v8::Debug::SetDebugEventListener(NULL); 2131 CheckDebuggerUnloaded(); 2132} 2133 2134 2135// Test script break points set on lines. 2136TEST(ScriptBreakPointLine) { 2137 v8::HandleScope scope; 2138 DebugLocalContext env; 2139 env.ExposeDebug(); 2140 2141 // Create a function for checking the function when hitting a break point. 2142 frame_function_name = CompileFunction(&env, 2143 frame_function_name_source, 2144 "frame_function_name"); 2145 2146 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2147 v8::Undefined()); 2148 2149 v8::Local<v8::Function> f; 2150 v8::Local<v8::Function> g; 2151 v8::Local<v8::String> script = v8::String::New( 2152 "a = 0 // line 0\n" 2153 "function f() {\n" 2154 " a = 1; // line 2\n" 2155 "}\n" 2156 " a = 2; // line 4\n" 2157 " /* xx */ function g() { // line 5\n" 2158 " function h() { // line 6\n" 2159 " a = 3; // line 7\n" 2160 " }\n" 2161 " h(); // line 9\n" 2162 " a = 4; // line 10\n" 2163 " }\n" 2164 " a=5; // line 12"); 2165 2166 // Set a couple script break point before the script is loaded. 2167 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1); 2168 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1); 2169 int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1); 2170 2171 // Compile the script and get the function. 2172 break_point_hit_count = 0; 2173 v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0)); 2174 v8::Script::Compile(script, &origin)->Run(); 2175 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 2176 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); 2177 2178 // Check that a break point was hit when the script was run. 2179 CHECK_EQ(1, break_point_hit_count); 2180 CHECK_EQ(0, StrLength(last_function_hit)); 2181 2182 // Call f and check that the script break point. 2183 f->Call(env->Global(), 0, NULL); 2184 CHECK_EQ(2, break_point_hit_count); 2185 CHECK_EQ("f", last_function_hit); 2186 2187 // Call g and check that the script break point. 2188 g->Call(env->Global(), 0, NULL); 2189 CHECK_EQ(3, break_point_hit_count); 2190 CHECK_EQ("g", last_function_hit); 2191 2192 // Clear the script break point on g and set one on h. 2193 ClearBreakPointFromJS(sbp3); 2194 int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1); 2195 2196 // Call g and check that the script break point in h is hit. 2197 g->Call(env->Global(), 0, NULL); 2198 CHECK_EQ(4, break_point_hit_count); 2199 CHECK_EQ("h", last_function_hit); 2200 2201 // Clear break points in f and h. Set a new one in the script between 2202 // functions f and g and test that there is no break points in f and g any 2203 // more. 2204 ClearBreakPointFromJS(sbp2); 2205 ClearBreakPointFromJS(sbp4); 2206 int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1); 2207 break_point_hit_count = 0; 2208 f->Call(env->Global(), 0, NULL); 2209 g->Call(env->Global(), 0, NULL); 2210 CHECK_EQ(0, break_point_hit_count); 2211 2212 // Reload the script which should hit two break points. 2213 break_point_hit_count = 0; 2214 v8::Script::Compile(script, &origin)->Run(); 2215 CHECK_EQ(2, break_point_hit_count); 2216 CHECK_EQ(0, StrLength(last_function_hit)); 2217 2218 // Set a break point in the code after the last function decleration. 2219 int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1); 2220 2221 // Reload the script which should hit three break points. 2222 break_point_hit_count = 0; 2223 v8::Script::Compile(script, &origin)->Run(); 2224 CHECK_EQ(3, break_point_hit_count); 2225 CHECK_EQ(0, StrLength(last_function_hit)); 2226 2227 // Clear the last break points, and reload the script which should not hit any 2228 // break points. 2229 ClearBreakPointFromJS(sbp1); 2230 ClearBreakPointFromJS(sbp5); 2231 ClearBreakPointFromJS(sbp6); 2232 break_point_hit_count = 0; 2233 v8::Script::Compile(script, &origin)->Run(); 2234 CHECK_EQ(0, break_point_hit_count); 2235 2236 v8::Debug::SetDebugEventListener(NULL); 2237 CheckDebuggerUnloaded(); 2238} 2239 2240 2241// Test top level script break points set on lines. 2242TEST(ScriptBreakPointLineTopLevel) { 2243 v8::HandleScope scope; 2244 DebugLocalContext env; 2245 env.ExposeDebug(); 2246 2247 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2248 v8::Undefined()); 2249 2250 v8::Local<v8::String> script = v8::String::New( 2251 "function f() {\n" 2252 " a = 1; // line 1\n" 2253 "}\n" 2254 "a = 2; // line 3\n"); 2255 v8::Local<v8::Function> f; 2256 { 2257 v8::HandleScope scope; 2258 v8::Script::Compile(script, v8::String::New("test.html"))->Run(); 2259 } 2260 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 2261 2262 HEAP->CollectAllGarbage(false); 2263 2264 SetScriptBreakPointByNameFromJS("test.html", 3, -1); 2265 2266 // Call f and check that there was no break points. 2267 break_point_hit_count = 0; 2268 f->Call(env->Global(), 0, NULL); 2269 CHECK_EQ(0, break_point_hit_count); 2270 2271 // Recompile and run script and check that break point was hit. 2272 break_point_hit_count = 0; 2273 v8::Script::Compile(script, v8::String::New("test.html"))->Run(); 2274 CHECK_EQ(1, break_point_hit_count); 2275 2276 // Call f and check that there are still no break points. 2277 break_point_hit_count = 0; 2278 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 2279 CHECK_EQ(0, break_point_hit_count); 2280 2281 v8::Debug::SetDebugEventListener(NULL); 2282 CheckDebuggerUnloaded(); 2283} 2284 2285 2286// Test that it is possible to add and remove break points in a top level 2287// function which has no references but has not been collected yet. 2288TEST(ScriptBreakPointTopLevelCrash) { 2289 v8::HandleScope scope; 2290 DebugLocalContext env; 2291 env.ExposeDebug(); 2292 2293 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2294 v8::Undefined()); 2295 2296 v8::Local<v8::String> script_source = v8::String::New( 2297 "function f() {\n" 2298 " return 0;\n" 2299 "}\n" 2300 "f()"); 2301 2302 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); 2303 { 2304 v8::HandleScope scope; 2305 break_point_hit_count = 0; 2306 v8::Script::Compile(script_source, v8::String::New("test.html"))->Run(); 2307 CHECK_EQ(1, break_point_hit_count); 2308 } 2309 2310 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 3, -1); 2311 ClearBreakPointFromJS(sbp1); 2312 ClearBreakPointFromJS(sbp2); 2313 2314 v8::Debug::SetDebugEventListener(NULL); 2315 CheckDebuggerUnloaded(); 2316} 2317 2318 2319// Test that it is possible to remove the last break point for a function 2320// inside the break handling of that break point. 2321TEST(RemoveBreakPointInBreak) { 2322 v8::HandleScope scope; 2323 DebugLocalContext env; 2324 2325 v8::Local<v8::Function> foo = 2326 CompileFunction(&env, "function foo(){a=1;}", "foo"); 2327 debug_event_remove_break_point = SetBreakPoint(foo, 0); 2328 2329 // Register the debug event listener pasing the function 2330 v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo); 2331 2332 break_point_hit_count = 0; 2333 foo->Call(env->Global(), 0, NULL); 2334 CHECK_EQ(1, break_point_hit_count); 2335 2336 break_point_hit_count = 0; 2337 foo->Call(env->Global(), 0, NULL); 2338 CHECK_EQ(0, break_point_hit_count); 2339 2340 v8::Debug::SetDebugEventListener(NULL); 2341 CheckDebuggerUnloaded(); 2342} 2343 2344 2345// Test that the debugger statement causes a break. 2346TEST(DebuggerStatement) { 2347 break_point_hit_count = 0; 2348 v8::HandleScope scope; 2349 DebugLocalContext env; 2350 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2351 v8::Undefined()); 2352 v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run(); 2353 v8::Script::Compile(v8::String::New( 2354 "function foo(){debugger;debugger;}"))->Run(); 2355 v8::Local<v8::Function> foo = 2356 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 2357 v8::Local<v8::Function> bar = 2358 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar"))); 2359 2360 // Run function with debugger statement 2361 bar->Call(env->Global(), 0, NULL); 2362 CHECK_EQ(1, break_point_hit_count); 2363 2364 // Run function with two debugger statement 2365 foo->Call(env->Global(), 0, NULL); 2366 CHECK_EQ(3, break_point_hit_count); 2367 2368 v8::Debug::SetDebugEventListener(NULL); 2369 CheckDebuggerUnloaded(); 2370} 2371 2372 2373// Test setting a breakpoint on the debugger statement. 2374TEST(DebuggerStatementBreakpoint) { 2375 break_point_hit_count = 0; 2376 v8::HandleScope scope; 2377 DebugLocalContext env; 2378 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 2379 v8::Undefined()); 2380 v8::Script::Compile(v8::String::New("function foo(){debugger;}"))->Run(); 2381 v8::Local<v8::Function> foo = 2382 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 2383 2384 // The debugger statement triggers breakpint hit 2385 foo->Call(env->Global(), 0, NULL); 2386 CHECK_EQ(1, break_point_hit_count); 2387 2388 int bp = SetBreakPoint(foo, 0); 2389 2390 // Set breakpoint does not duplicate hits 2391 foo->Call(env->Global(), 0, NULL); 2392 CHECK_EQ(2, break_point_hit_count); 2393 2394 ClearBreakPoint(bp); 2395 v8::Debug::SetDebugEventListener(NULL); 2396 CheckDebuggerUnloaded(); 2397} 2398 2399 2400// Thest that the evaluation of expressions when a break point is hit generates 2401// the correct results. 2402TEST(DebugEvaluate) { 2403 v8::HandleScope scope; 2404 DebugLocalContext env; 2405 env.ExposeDebug(); 2406 2407 // Create a function for checking the evaluation when hitting a break point. 2408 evaluate_check_function = CompileFunction(&env, 2409 evaluate_check_source, 2410 "evaluate_check"); 2411 // Register the debug event listener 2412 v8::Debug::SetDebugEventListener(DebugEventEvaluate); 2413 2414 // Different expected vaules of x and a when in a break point (u = undefined, 2415 // d = Hello, world!). 2416 struct EvaluateCheck checks_uu[] = { 2417 {"x", v8::Undefined()}, 2418 {"a", v8::Undefined()}, 2419 {NULL, v8::Handle<v8::Value>()} 2420 }; 2421 struct EvaluateCheck checks_hu[] = { 2422 {"x", v8::String::New("Hello, world!")}, 2423 {"a", v8::Undefined()}, 2424 {NULL, v8::Handle<v8::Value>()} 2425 }; 2426 struct EvaluateCheck checks_hh[] = { 2427 {"x", v8::String::New("Hello, world!")}, 2428 {"a", v8::String::New("Hello, world!")}, 2429 {NULL, v8::Handle<v8::Value>()} 2430 }; 2431 2432 // Simple test function. The "y=0" is in the function foo to provide a break 2433 // location. For "y=0" the "y" is at position 15 in the barbar function 2434 // therefore setting breakpoint at position 15 will break at "y=0" and 2435 // setting it higher will break after. 2436 v8::Local<v8::Function> foo = CompileFunction(&env, 2437 "function foo(x) {" 2438 " var a;" 2439 " y=0;" // To ensure break location 1. 2440 " a=x;" 2441 " y=0;" // To ensure break location 2. 2442 "}", 2443 "foo"); 2444 const int foo_break_position_1 = 15; 2445 const int foo_break_position_2 = 29; 2446 2447 // Arguments with one parameter "Hello, world!" 2448 v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") }; 2449 2450 // Call foo with breakpoint set before a=x and undefined as parameter. 2451 int bp = SetBreakPoint(foo, foo_break_position_1); 2452 checks = checks_uu; 2453 foo->Call(env->Global(), 0, NULL); 2454 2455 // Call foo with breakpoint set before a=x and parameter "Hello, world!". 2456 checks = checks_hu; 2457 foo->Call(env->Global(), 1, argv_foo); 2458 2459 // Call foo with breakpoint set after a=x and parameter "Hello, world!". 2460 ClearBreakPoint(bp); 2461 SetBreakPoint(foo, foo_break_position_2); 2462 checks = checks_hh; 2463 foo->Call(env->Global(), 1, argv_foo); 2464 2465 // Test function with an inner function. The "y=0" is in function barbar 2466 // to provide a break location. For "y=0" the "y" is at position 8 in the 2467 // barbar function therefore setting breakpoint at position 8 will break at 2468 // "y=0" and setting it higher will break after. 2469 v8::Local<v8::Function> bar = CompileFunction(&env, 2470 "y = 0;" 2471 "x = 'Goodbye, world!';" 2472 "function bar(x, b) {" 2473 " var a;" 2474 " function barbar() {" 2475 " y=0; /* To ensure break location.*/" 2476 " a=x;" 2477 " };" 2478 " debug.Debug.clearAllBreakPoints();" 2479 " barbar();" 2480 " y=0;a=x;" 2481 "}", 2482 "bar"); 2483 const int barbar_break_position = 8; 2484 2485 // Call bar setting breakpoint before a=x in barbar and undefined as 2486 // parameter. 2487 checks = checks_uu; 2488 v8::Handle<v8::Value> argv_bar_1[2] = { 2489 v8::Undefined(), 2490 v8::Number::New(barbar_break_position) 2491 }; 2492 bar->Call(env->Global(), 2, argv_bar_1); 2493 2494 // Call bar setting breakpoint before a=x in barbar and parameter 2495 // "Hello, world!". 2496 checks = checks_hu; 2497 v8::Handle<v8::Value> argv_bar_2[2] = { 2498 v8::String::New("Hello, world!"), 2499 v8::Number::New(barbar_break_position) 2500 }; 2501 bar->Call(env->Global(), 2, argv_bar_2); 2502 2503 // Call bar setting breakpoint after a=x in barbar and parameter 2504 // "Hello, world!". 2505 checks = checks_hh; 2506 v8::Handle<v8::Value> argv_bar_3[2] = { 2507 v8::String::New("Hello, world!"), 2508 v8::Number::New(barbar_break_position + 1) 2509 }; 2510 bar->Call(env->Global(), 2, argv_bar_3); 2511 2512 v8::Debug::SetDebugEventListener(NULL); 2513 CheckDebuggerUnloaded(); 2514} 2515 2516// Copies a C string to a 16-bit string. Does not check for buffer overflow. 2517// Does not use the V8 engine to convert strings, so it can be used 2518// in any thread. Returns the length of the string. 2519int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) { 2520 int i; 2521 for (i = 0; input_buffer[i] != '\0'; ++i) { 2522 // ASCII does not use chars > 127, but be careful anyway. 2523 output_buffer[i] = static_cast<unsigned char>(input_buffer[i]); 2524 } 2525 output_buffer[i] = 0; 2526 return i; 2527} 2528 2529// Copies a 16-bit string to a C string by dropping the high byte of 2530// each character. Does not check for buffer overflow. 2531// Can be used in any thread. Requires string length as an input. 2532int Utf16ToAscii(const uint16_t* input_buffer, int length, 2533 char* output_buffer, int output_len = -1) { 2534 if (output_len >= 0) { 2535 if (length > output_len - 1) { 2536 length = output_len - 1; 2537 } 2538 } 2539 2540 for (int i = 0; i < length; ++i) { 2541 output_buffer[i] = static_cast<char>(input_buffer[i]); 2542 } 2543 output_buffer[length] = '\0'; 2544 return length; 2545} 2546 2547 2548// We match parts of the message to get evaluate result int value. 2549bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) { 2550 if (strstr(message, "\"command\":\"evaluate\"") == NULL) { 2551 return false; 2552 } 2553 const char* prefix = "\"text\":\""; 2554 char* pos1 = strstr(message, prefix); 2555 if (pos1 == NULL) { 2556 return false; 2557 } 2558 pos1 += strlen(prefix); 2559 char* pos2 = strchr(pos1, '"'); 2560 if (pos2 == NULL) { 2561 return false; 2562 } 2563 Vector<char> buf(buffer, buffer_size); 2564 int len = static_cast<int>(pos2 - pos1); 2565 if (len > buffer_size - 1) { 2566 len = buffer_size - 1; 2567 } 2568 OS::StrNCpy(buf, pos1, len); 2569 buffer[buffer_size - 1] = '\0'; 2570 return true; 2571} 2572 2573 2574struct EvaluateResult { 2575 static const int kBufferSize = 20; 2576 char buffer[kBufferSize]; 2577}; 2578 2579struct DebugProcessDebugMessagesData { 2580 static const int kArraySize = 5; 2581 int counter; 2582 EvaluateResult results[kArraySize]; 2583 2584 void reset() { 2585 counter = 0; 2586 } 2587 EvaluateResult* current() { 2588 return &results[counter % kArraySize]; 2589 } 2590 void next() { 2591 counter++; 2592 } 2593}; 2594 2595DebugProcessDebugMessagesData process_debug_messages_data; 2596 2597static void DebugProcessDebugMessagesHandler( 2598 const uint16_t* message, 2599 int length, 2600 v8::Debug::ClientData* client_data) { 2601 2602 const int kBufferSize = 100000; 2603 char print_buffer[kBufferSize]; 2604 Utf16ToAscii(message, length, print_buffer, kBufferSize); 2605 2606 EvaluateResult* array_item = process_debug_messages_data.current(); 2607 2608 bool res = GetEvaluateStringResult(print_buffer, 2609 array_item->buffer, 2610 EvaluateResult::kBufferSize); 2611 if (res) { 2612 process_debug_messages_data.next(); 2613 } 2614} 2615 2616// Test that the evaluation of expressions works even from ProcessDebugMessages 2617// i.e. with empty stack. 2618TEST(DebugEvaluateWithoutStack) { 2619 v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler); 2620 2621 v8::HandleScope scope; 2622 DebugLocalContext env; 2623 2624 const char* source = 2625 "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }"; 2626 2627 v8::Script::Compile(v8::String::New(source))->Run(); 2628 2629 v8::Debug::ProcessDebugMessages(); 2630 2631 const int kBufferSize = 1000; 2632 uint16_t buffer[kBufferSize]; 2633 2634 const char* command_111 = "{\"seq\":111," 2635 "\"type\":\"request\"," 2636 "\"command\":\"evaluate\"," 2637 "\"arguments\":{" 2638 " \"global\":true," 2639 " \"expression\":\"v1\",\"disable_break\":true" 2640 "}}"; 2641 2642 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_111, buffer)); 2643 2644 const char* command_112 = "{\"seq\":112," 2645 "\"type\":\"request\"," 2646 "\"command\":\"evaluate\"," 2647 "\"arguments\":{" 2648 " \"global\":true," 2649 " \"expression\":\"getAnimal()\",\"disable_break\":true" 2650 "}}"; 2651 2652 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_112, buffer)); 2653 2654 const char* command_113 = "{\"seq\":113," 2655 "\"type\":\"request\"," 2656 "\"command\":\"evaluate\"," 2657 "\"arguments\":{" 2658 " \"global\":true," 2659 " \"expression\":\"239 + 566\",\"disable_break\":true" 2660 "}}"; 2661 2662 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_113, buffer)); 2663 2664 v8::Debug::ProcessDebugMessages(); 2665 2666 CHECK_EQ(3, process_debug_messages_data.counter); 2667 2668 CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0); 2669 CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer), 2670 0); 2671 CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0); 2672 2673 v8::Debug::SetMessageHandler(NULL); 2674 v8::Debug::SetDebugEventListener(NULL); 2675 CheckDebuggerUnloaded(); 2676} 2677 2678 2679// Simple test of the stepping mechanism using only store ICs. 2680TEST(DebugStepLinear) { 2681 v8::HandleScope scope; 2682 DebugLocalContext env; 2683 2684 // Create a function for testing stepping. 2685 v8::Local<v8::Function> foo = CompileFunction(&env, 2686 "function foo(){a=1;b=1;c=1;}", 2687 "foo"); 2688 2689 // Run foo to allow it to get optimized. 2690 CompileRun("a=0; b=0; c=0; foo();"); 2691 2692 SetBreakPoint(foo, 3); 2693 2694 // Register a debug event listener which steps and counts. 2695 v8::Debug::SetDebugEventListener(DebugEventStep); 2696 2697 step_action = StepIn; 2698 break_point_hit_count = 0; 2699 foo->Call(env->Global(), 0, NULL); 2700 2701 // With stepping all break locations are hit. 2702 CHECK_EQ(4, break_point_hit_count); 2703 2704 v8::Debug::SetDebugEventListener(NULL); 2705 CheckDebuggerUnloaded(); 2706 2707 // Register a debug event listener which just counts. 2708 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); 2709 2710 SetBreakPoint(foo, 3); 2711 break_point_hit_count = 0; 2712 foo->Call(env->Global(), 0, NULL); 2713 2714 // Without stepping only active break points are hit. 2715 CHECK_EQ(1, break_point_hit_count); 2716 2717 v8::Debug::SetDebugEventListener(NULL); 2718 CheckDebuggerUnloaded(); 2719} 2720 2721 2722// Test of the stepping mechanism for keyed load in a loop. 2723TEST(DebugStepKeyedLoadLoop) { 2724 v8::HandleScope scope; 2725 DebugLocalContext env; 2726 2727 // Register a debug event listener which steps and counts. 2728 v8::Debug::SetDebugEventListener(DebugEventStep); 2729 2730 // Create a function for testing stepping of keyed load. The statement 'y=1' 2731 // is there to have more than one breakable statement in the loop, TODO(315). 2732 v8::Local<v8::Function> foo = CompileFunction( 2733 &env, 2734 "function foo(a) {\n" 2735 " var x;\n" 2736 " var len = a.length;\n" 2737 " for (var i = 0; i < len; i++) {\n" 2738 " y = 1;\n" 2739 " x = a[i];\n" 2740 " }\n" 2741 "}\n" 2742 "y=0\n", 2743 "foo"); 2744 2745 // Create array [0,1,2,3,4,5,6,7,8,9] 2746 v8::Local<v8::Array> a = v8::Array::New(10); 2747 for (int i = 0; i < 10; i++) { 2748 a->Set(v8::Number::New(i), v8::Number::New(i)); 2749 } 2750 2751 // Call function without any break points to ensure inlining is in place. 2752 const int kArgc = 1; 2753 v8::Handle<v8::Value> args[kArgc] = { a }; 2754 foo->Call(env->Global(), kArgc, args); 2755 2756 // Setup break point and step through the function. 2757 SetBreakPoint(foo, 3); 2758 step_action = StepNext; 2759 break_point_hit_count = 0; 2760 foo->Call(env->Global(), kArgc, args); 2761 2762 // With stepping all break locations are hit. 2763 CHECK_EQ(33, break_point_hit_count); 2764 2765 v8::Debug::SetDebugEventListener(NULL); 2766 CheckDebuggerUnloaded(); 2767} 2768 2769 2770// Test of the stepping mechanism for keyed store in a loop. 2771TEST(DebugStepKeyedStoreLoop) { 2772 v8::HandleScope scope; 2773 DebugLocalContext env; 2774 2775 // Register a debug event listener which steps and counts. 2776 v8::Debug::SetDebugEventListener(DebugEventStep); 2777 2778 // Create a function for testing stepping of keyed store. The statement 'y=1' 2779 // is there to have more than one breakable statement in the loop, TODO(315). 2780 v8::Local<v8::Function> foo = CompileFunction( 2781 &env, 2782 "function foo(a) {\n" 2783 " var len = a.length;\n" 2784 " for (var i = 0; i < len; i++) {\n" 2785 " y = 1;\n" 2786 " a[i] = 42;\n" 2787 " }\n" 2788 "}\n" 2789 "y=0\n", 2790 "foo"); 2791 2792 // Create array [0,1,2,3,4,5,6,7,8,9] 2793 v8::Local<v8::Array> a = v8::Array::New(10); 2794 for (int i = 0; i < 10; i++) { 2795 a->Set(v8::Number::New(i), v8::Number::New(i)); 2796 } 2797 2798 // Call function without any break points to ensure inlining is in place. 2799 const int kArgc = 1; 2800 v8::Handle<v8::Value> args[kArgc] = { a }; 2801 foo->Call(env->Global(), kArgc, args); 2802 2803 // Setup break point and step through the function. 2804 SetBreakPoint(foo, 3); 2805 step_action = StepNext; 2806 break_point_hit_count = 0; 2807 foo->Call(env->Global(), kArgc, args); 2808 2809 // With stepping all break locations are hit. 2810 CHECK_EQ(32, break_point_hit_count); 2811 2812 v8::Debug::SetDebugEventListener(NULL); 2813 CheckDebuggerUnloaded(); 2814} 2815 2816 2817// Test of the stepping mechanism for named load in a loop. 2818TEST(DebugStepNamedLoadLoop) { 2819 v8::HandleScope scope; 2820 DebugLocalContext env; 2821 2822 // Register a debug event listener which steps and counts. 2823 v8::Debug::SetDebugEventListener(DebugEventStep); 2824 2825 // Create a function for testing stepping of named load. 2826 v8::Local<v8::Function> foo = CompileFunction( 2827 &env, 2828 "function foo() {\n" 2829 " var a = [];\n" 2830 " var s = \"\";\n" 2831 " for (var i = 0; i < 10; i++) {\n" 2832 " var v = new V(i, i + 1);\n" 2833 " v.y;\n" 2834 " a.length;\n" // Special case: array length. 2835 " s.length;\n" // Special case: string length. 2836 " }\n" 2837 "}\n" 2838 "function V(x, y) {\n" 2839 " this.x = x;\n" 2840 " this.y = y;\n" 2841 "}\n", 2842 "foo"); 2843 2844 // Call function without any break points to ensure inlining is in place. 2845 foo->Call(env->Global(), 0, NULL); 2846 2847 // Setup break point and step through the function. 2848 SetBreakPoint(foo, 4); 2849 step_action = StepNext; 2850 break_point_hit_count = 0; 2851 foo->Call(env->Global(), 0, NULL); 2852 2853 // With stepping all break locations are hit. 2854 CHECK_EQ(53, break_point_hit_count); 2855 2856 v8::Debug::SetDebugEventListener(NULL); 2857 CheckDebuggerUnloaded(); 2858} 2859 2860 2861static void DoDebugStepNamedStoreLoop(int expected) { 2862 v8::HandleScope scope; 2863 DebugLocalContext env; 2864 2865 // Register a debug event listener which steps and counts. 2866 v8::Debug::SetDebugEventListener(DebugEventStep); 2867 2868 // Create a function for testing stepping of named store. 2869 v8::Local<v8::Function> foo = CompileFunction( 2870 &env, 2871 "function foo() {\n" 2872 " var a = {a:1};\n" 2873 " for (var i = 0; i < 10; i++) {\n" 2874 " a.a = 2\n" 2875 " }\n" 2876 "}\n", 2877 "foo"); 2878 2879 // Call function without any break points to ensure inlining is in place. 2880 foo->Call(env->Global(), 0, NULL); 2881 2882 // Setup break point and step through the function. 2883 SetBreakPoint(foo, 3); 2884 step_action = StepNext; 2885 break_point_hit_count = 0; 2886 foo->Call(env->Global(), 0, NULL); 2887 2888 // With stepping all expected break locations are hit. 2889 CHECK_EQ(expected, break_point_hit_count); 2890 2891 v8::Debug::SetDebugEventListener(NULL); 2892 CheckDebuggerUnloaded(); 2893} 2894 2895 2896// Test of the stepping mechanism for named load in a loop. 2897TEST(DebugStepNamedStoreLoop) { 2898 DoDebugStepNamedStoreLoop(22); 2899} 2900 2901 2902// Test the stepping mechanism with different ICs. 2903TEST(DebugStepLinearMixedICs) { 2904 v8::HandleScope scope; 2905 DebugLocalContext env; 2906 2907 // Register a debug event listener which steps and counts. 2908 v8::Debug::SetDebugEventListener(DebugEventStep); 2909 2910 // Create a function for testing stepping. 2911 v8::Local<v8::Function> foo = CompileFunction(&env, 2912 "function bar() {};" 2913 "function foo() {" 2914 " var x;" 2915 " var index='name';" 2916 " var y = {};" 2917 " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); 2918 2919 // Run functions to allow them to get optimized. 2920 CompileRun("a=0; b=0; bar(); foo();"); 2921 2922 SetBreakPoint(foo, 0); 2923 2924 step_action = StepIn; 2925 break_point_hit_count = 0; 2926 foo->Call(env->Global(), 0, NULL); 2927 2928 // With stepping all break locations are hit. 2929 CHECK_EQ(11, break_point_hit_count); 2930 2931 v8::Debug::SetDebugEventListener(NULL); 2932 CheckDebuggerUnloaded(); 2933 2934 // Register a debug event listener which just counts. 2935 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); 2936 2937 SetBreakPoint(foo, 0); 2938 break_point_hit_count = 0; 2939 foo->Call(env->Global(), 0, NULL); 2940 2941 // Without stepping only active break points are hit. 2942 CHECK_EQ(1, break_point_hit_count); 2943 2944 v8::Debug::SetDebugEventListener(NULL); 2945 CheckDebuggerUnloaded(); 2946} 2947 2948 2949TEST(DebugStepDeclarations) { 2950 v8::HandleScope scope; 2951 DebugLocalContext env; 2952 2953 // Register a debug event listener which steps and counts. 2954 v8::Debug::SetDebugEventListener(DebugEventStep); 2955 2956 // Create a function for testing stepping. Run it to allow it to get 2957 // optimized. 2958 const char* src = "function foo() { " 2959 " var a;" 2960 " var b = 1;" 2961 " var c = foo;" 2962 " var d = Math.floor;" 2963 " var e = b + d(1.2);" 2964 "}" 2965 "foo()"; 2966 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 2967 2968 SetBreakPoint(foo, 0); 2969 2970 // Stepping through the declarations. 2971 step_action = StepIn; 2972 break_point_hit_count = 0; 2973 foo->Call(env->Global(), 0, NULL); 2974 CHECK_EQ(6, break_point_hit_count); 2975 2976 // Get rid of the debug event listener. 2977 v8::Debug::SetDebugEventListener(NULL); 2978 CheckDebuggerUnloaded(); 2979} 2980 2981 2982TEST(DebugStepLocals) { 2983 v8::HandleScope scope; 2984 DebugLocalContext env; 2985 2986 // Register a debug event listener which steps and counts. 2987 v8::Debug::SetDebugEventListener(DebugEventStep); 2988 2989 // Create a function for testing stepping. Run it to allow it to get 2990 // optimized. 2991 const char* src = "function foo() { " 2992 " var a,b;" 2993 " a = 1;" 2994 " b = a + 2;" 2995 " b = 1 + 2 + 3;" 2996 " a = Math.floor(b);" 2997 "}" 2998 "foo()"; 2999 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3000 3001 SetBreakPoint(foo, 0); 3002 3003 // Stepping through the declarations. 3004 step_action = StepIn; 3005 break_point_hit_count = 0; 3006 foo->Call(env->Global(), 0, NULL); 3007 CHECK_EQ(6, break_point_hit_count); 3008 3009 // Get rid of the debug event listener. 3010 v8::Debug::SetDebugEventListener(NULL); 3011 CheckDebuggerUnloaded(); 3012} 3013 3014 3015TEST(DebugStepIf) { 3016 v8::HandleScope scope; 3017 DebugLocalContext env; 3018 3019 // Register a debug event listener which steps and counts. 3020 v8::Debug::SetDebugEventListener(DebugEventStep); 3021 3022 // Create a function for testing stepping. Run it to allow it to get 3023 // optimized. 3024 const int argc = 1; 3025 const char* src = "function foo(x) { " 3026 " a = 1;" 3027 " if (x) {" 3028 " b = 1;" 3029 " } else {" 3030 " c = 1;" 3031 " d = 1;" 3032 " }" 3033 "}" 3034 "a=0; b=0; c=0; d=0; foo()"; 3035 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3036 SetBreakPoint(foo, 0); 3037 3038 // Stepping through the true part. 3039 step_action = StepIn; 3040 break_point_hit_count = 0; 3041 v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; 3042 foo->Call(env->Global(), argc, argv_true); 3043 CHECK_EQ(4, break_point_hit_count); 3044 3045 // Stepping through the false part. 3046 step_action = StepIn; 3047 break_point_hit_count = 0; 3048 v8::Handle<v8::Value> argv_false[argc] = { v8::False() }; 3049 foo->Call(env->Global(), argc, argv_false); 3050 CHECK_EQ(5, break_point_hit_count); 3051 3052 // Get rid of the debug event listener. 3053 v8::Debug::SetDebugEventListener(NULL); 3054 CheckDebuggerUnloaded(); 3055} 3056 3057 3058TEST(DebugStepSwitch) { 3059 v8::HandleScope scope; 3060 DebugLocalContext env; 3061 3062 // Register a debug event listener which steps and counts. 3063 v8::Debug::SetDebugEventListener(DebugEventStep); 3064 3065 // Create a function for testing stepping. Run it to allow it to get 3066 // optimized. 3067 const int argc = 1; 3068 const char* src = "function foo(x) { " 3069 " a = 1;" 3070 " switch (x) {" 3071 " case 1:" 3072 " b = 1;" 3073 " case 2:" 3074 " c = 1;" 3075 " break;" 3076 " case 3:" 3077 " d = 1;" 3078 " e = 1;" 3079 " f = 1;" 3080 " break;" 3081 " }" 3082 "}" 3083 "a=0; b=0; c=0; d=0; e=0; f=0; foo()"; 3084 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3085 SetBreakPoint(foo, 0); 3086 3087 // One case with fall-through. 3088 step_action = StepIn; 3089 break_point_hit_count = 0; 3090 v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) }; 3091 foo->Call(env->Global(), argc, argv_1); 3092 CHECK_EQ(6, break_point_hit_count); 3093 3094 // Another case. 3095 step_action = StepIn; 3096 break_point_hit_count = 0; 3097 v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) }; 3098 foo->Call(env->Global(), argc, argv_2); 3099 CHECK_EQ(5, break_point_hit_count); 3100 3101 // Last case. 3102 step_action = StepIn; 3103 break_point_hit_count = 0; 3104 v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) }; 3105 foo->Call(env->Global(), argc, argv_3); 3106 CHECK_EQ(7, break_point_hit_count); 3107 3108 // Get rid of the debug event listener. 3109 v8::Debug::SetDebugEventListener(NULL); 3110 CheckDebuggerUnloaded(); 3111} 3112 3113 3114TEST(DebugStepWhile) { 3115 v8::HandleScope scope; 3116 DebugLocalContext env; 3117 3118 // Register a debug event listener which steps and counts. 3119 v8::Debug::SetDebugEventListener(DebugEventStep); 3120 3121 // Create a function for testing stepping. Run it to allow it to get 3122 // optimized. 3123 const int argc = 1; 3124 const char* src = "function foo(x) { " 3125 " var a = 0;" 3126 " while (a < x) {" 3127 " a++;" 3128 " }" 3129 "}" 3130 "foo()"; 3131 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3132 SetBreakPoint(foo, 8); // "var a = 0;" 3133 3134 // Looping 10 times. 3135 step_action = StepIn; 3136 break_point_hit_count = 0; 3137 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; 3138 foo->Call(env->Global(), argc, argv_10); 3139 CHECK_EQ(22, break_point_hit_count); 3140 3141 // Looping 100 times. 3142 step_action = StepIn; 3143 break_point_hit_count = 0; 3144 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; 3145 foo->Call(env->Global(), argc, argv_100); 3146 CHECK_EQ(202, break_point_hit_count); 3147 3148 // Get rid of the debug event listener. 3149 v8::Debug::SetDebugEventListener(NULL); 3150 CheckDebuggerUnloaded(); 3151} 3152 3153 3154TEST(DebugStepDoWhile) { 3155 v8::HandleScope scope; 3156 DebugLocalContext env; 3157 3158 // Register a debug event listener which steps and counts. 3159 v8::Debug::SetDebugEventListener(DebugEventStep); 3160 3161 // Create a function for testing stepping. Run it to allow it to get 3162 // optimized. 3163 const int argc = 1; 3164 const char* src = "function foo(x) { " 3165 " var a = 0;" 3166 " do {" 3167 " a++;" 3168 " } while (a < x)" 3169 "}" 3170 "foo()"; 3171 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3172 SetBreakPoint(foo, 8); // "var a = 0;" 3173 3174 // Looping 10 times. 3175 step_action = StepIn; 3176 break_point_hit_count = 0; 3177 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; 3178 foo->Call(env->Global(), argc, argv_10); 3179 CHECK_EQ(22, break_point_hit_count); 3180 3181 // Looping 100 times. 3182 step_action = StepIn; 3183 break_point_hit_count = 0; 3184 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; 3185 foo->Call(env->Global(), argc, argv_100); 3186 CHECK_EQ(202, break_point_hit_count); 3187 3188 // Get rid of the debug event listener. 3189 v8::Debug::SetDebugEventListener(NULL); 3190 CheckDebuggerUnloaded(); 3191} 3192 3193 3194TEST(DebugStepFor) { 3195 v8::HandleScope scope; 3196 DebugLocalContext env; 3197 3198 // Register a debug event listener which steps and counts. 3199 v8::Debug::SetDebugEventListener(DebugEventStep); 3200 3201 // Create a function for testing stepping. Run it to allow it to get 3202 // optimized. 3203 const int argc = 1; 3204 const char* src = "function foo(x) { " 3205 " a = 1;" 3206 " for (i = 0; i < x; i++) {" 3207 " b = 1;" 3208 " }" 3209 "}" 3210 "a=0; b=0; i=0; foo()"; 3211 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3212 3213 SetBreakPoint(foo, 8); // "a = 1;" 3214 3215 // Looping 10 times. 3216 step_action = StepIn; 3217 break_point_hit_count = 0; 3218 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; 3219 foo->Call(env->Global(), argc, argv_10); 3220 CHECK_EQ(23, break_point_hit_count); 3221 3222 // Looping 100 times. 3223 step_action = StepIn; 3224 break_point_hit_count = 0; 3225 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; 3226 foo->Call(env->Global(), argc, argv_100); 3227 CHECK_EQ(203, break_point_hit_count); 3228 3229 // Get rid of the debug event listener. 3230 v8::Debug::SetDebugEventListener(NULL); 3231 CheckDebuggerUnloaded(); 3232} 3233 3234 3235TEST(DebugStepForContinue) { 3236 v8::HandleScope scope; 3237 DebugLocalContext env; 3238 3239 // Register a debug event listener which steps and counts. 3240 v8::Debug::SetDebugEventListener(DebugEventStep); 3241 3242 // Create a function for testing stepping. Run it to allow it to get 3243 // optimized. 3244 const int argc = 1; 3245 const char* src = "function foo(x) { " 3246 " var a = 0;" 3247 " var b = 0;" 3248 " var c = 0;" 3249 " for (var i = 0; i < x; i++) {" 3250 " a++;" 3251 " if (a % 2 == 0) continue;" 3252 " b++;" 3253 " c++;" 3254 " }" 3255 " return b;" 3256 "}" 3257 "foo()"; 3258 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3259 v8::Handle<v8::Value> result; 3260 SetBreakPoint(foo, 8); // "var a = 0;" 3261 3262 // Each loop generates 4 or 5 steps depending on whether a is equal. 3263 3264 // Looping 10 times. 3265 step_action = StepIn; 3266 break_point_hit_count = 0; 3267 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; 3268 result = foo->Call(env->Global(), argc, argv_10); 3269 CHECK_EQ(5, result->Int32Value()); 3270 CHECK_EQ(50, break_point_hit_count); 3271 3272 // Looping 100 times. 3273 step_action = StepIn; 3274 break_point_hit_count = 0; 3275 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; 3276 result = foo->Call(env->Global(), argc, argv_100); 3277 CHECK_EQ(50, result->Int32Value()); 3278 CHECK_EQ(455, break_point_hit_count); 3279 3280 // Get rid of the debug event listener. 3281 v8::Debug::SetDebugEventListener(NULL); 3282 CheckDebuggerUnloaded(); 3283} 3284 3285 3286TEST(DebugStepForBreak) { 3287 v8::HandleScope scope; 3288 DebugLocalContext env; 3289 3290 // Register a debug event listener which steps and counts. 3291 v8::Debug::SetDebugEventListener(DebugEventStep); 3292 3293 // Create a function for testing stepping. Run it to allow it to get 3294 // optimized. 3295 const int argc = 1; 3296 const char* src = "function foo(x) { " 3297 " var a = 0;" 3298 " var b = 0;" 3299 " var c = 0;" 3300 " for (var i = 0; i < 1000; i++) {" 3301 " a++;" 3302 " if (a == x) break;" 3303 " b++;" 3304 " c++;" 3305 " }" 3306 " return b;" 3307 "}" 3308 "foo()"; 3309 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3310 v8::Handle<v8::Value> result; 3311 SetBreakPoint(foo, 8); // "var a = 0;" 3312 3313 // Each loop generates 5 steps except for the last (when break is executed) 3314 // which only generates 4. 3315 3316 // Looping 10 times. 3317 step_action = StepIn; 3318 break_point_hit_count = 0; 3319 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) }; 3320 result = foo->Call(env->Global(), argc, argv_10); 3321 CHECK_EQ(9, result->Int32Value()); 3322 CHECK_EQ(53, break_point_hit_count); 3323 3324 // Looping 100 times. 3325 step_action = StepIn; 3326 break_point_hit_count = 0; 3327 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) }; 3328 result = foo->Call(env->Global(), argc, argv_100); 3329 CHECK_EQ(99, result->Int32Value()); 3330 CHECK_EQ(503, break_point_hit_count); 3331 3332 // Get rid of the debug event listener. 3333 v8::Debug::SetDebugEventListener(NULL); 3334 CheckDebuggerUnloaded(); 3335} 3336 3337 3338TEST(DebugStepForIn) { 3339 v8::HandleScope scope; 3340 DebugLocalContext env; 3341 3342 // Register a debug event listener which steps and counts. 3343 v8::Debug::SetDebugEventListener(DebugEventStep); 3344 3345 // Create a function for testing stepping. Run it to allow it to get 3346 // optimized. 3347 v8::Local<v8::Function> foo; 3348 const char* src_1 = "function foo() { " 3349 " var a = [1, 2];" 3350 " for (x in a) {" 3351 " b = 0;" 3352 " }" 3353 "}" 3354 "foo()"; 3355 foo = CompileFunction(&env, src_1, "foo"); 3356 SetBreakPoint(foo, 0); // "var a = ..." 3357 3358 step_action = StepIn; 3359 break_point_hit_count = 0; 3360 foo->Call(env->Global(), 0, NULL); 3361 CHECK_EQ(6, break_point_hit_count); 3362 3363 // Create a function for testing stepping. Run it to allow it to get 3364 // optimized. 3365 const char* src_2 = "function foo() { " 3366 " var a = {a:[1, 2, 3]};" 3367 " for (x in a.a) {" 3368 " b = 0;" 3369 " }" 3370 "}" 3371 "foo()"; 3372 foo = CompileFunction(&env, src_2, "foo"); 3373 SetBreakPoint(foo, 0); // "var a = ..." 3374 3375 step_action = StepIn; 3376 break_point_hit_count = 0; 3377 foo->Call(env->Global(), 0, NULL); 3378 CHECK_EQ(8, break_point_hit_count); 3379 3380 // Get rid of the debug event listener. 3381 v8::Debug::SetDebugEventListener(NULL); 3382 CheckDebuggerUnloaded(); 3383} 3384 3385 3386TEST(DebugStepWith) { 3387 v8::HandleScope scope; 3388 DebugLocalContext env; 3389 3390 // Register a debug event listener which steps and counts. 3391 v8::Debug::SetDebugEventListener(DebugEventStep); 3392 3393 // Create a function for testing stepping. Run it to allow it to get 3394 // optimized. 3395 const char* src = "function foo(x) { " 3396 " var a = {};" 3397 " with (a) {}" 3398 " with (b) {}" 3399 "}" 3400 "foo()"; 3401 env->Global()->Set(v8::String::New("b"), v8::Object::New()); 3402 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3403 v8::Handle<v8::Value> result; 3404 SetBreakPoint(foo, 8); // "var a = {};" 3405 3406 step_action = StepIn; 3407 break_point_hit_count = 0; 3408 foo->Call(env->Global(), 0, NULL); 3409 CHECK_EQ(4, break_point_hit_count); 3410 3411 // Get rid of the debug event listener. 3412 v8::Debug::SetDebugEventListener(NULL); 3413 CheckDebuggerUnloaded(); 3414} 3415 3416 3417TEST(DebugConditional) { 3418 v8::HandleScope scope; 3419 DebugLocalContext env; 3420 3421 // Register a debug event listener which steps and counts. 3422 v8::Debug::SetDebugEventListener(DebugEventStep); 3423 3424 // Create a function for testing stepping. Run it to allow it to get 3425 // optimized. 3426 const char* src = "function foo(x) { " 3427 " var a;" 3428 " a = x ? 1 : 2;" 3429 " return a;" 3430 "}" 3431 "foo()"; 3432 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); 3433 SetBreakPoint(foo, 0); // "var a;" 3434 3435 step_action = StepIn; 3436 break_point_hit_count = 0; 3437 foo->Call(env->Global(), 0, NULL); 3438 CHECK_EQ(5, break_point_hit_count); 3439 3440 step_action = StepIn; 3441 break_point_hit_count = 0; 3442 const int argc = 1; 3443 v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; 3444 foo->Call(env->Global(), argc, argv_true); 3445 CHECK_EQ(5, break_point_hit_count); 3446 3447 // Get rid of the debug event listener. 3448 v8::Debug::SetDebugEventListener(NULL); 3449 CheckDebuggerUnloaded(); 3450} 3451 3452 3453TEST(StepInOutSimple) { 3454 v8::HandleScope scope; 3455 DebugLocalContext env; 3456 3457 // Create a function for checking the function when hitting a break point. 3458 frame_function_name = CompileFunction(&env, 3459 frame_function_name_source, 3460 "frame_function_name"); 3461 3462 // Register a debug event listener which steps and counts. 3463 v8::Debug::SetDebugEventListener(DebugEventStepSequence); 3464 3465 // Create a function for testing stepping. Run it to allow it to get 3466 // optimized. 3467 const char* src = "function a() {b();c();}; " 3468 "function b() {c();}; " 3469 "function c() {}; " 3470 "a(); b(); c()"; 3471 v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); 3472 SetBreakPoint(a, 0); 3473 3474 // Step through invocation of a with step in. 3475 step_action = StepIn; 3476 break_point_hit_count = 0; 3477 expected_step_sequence = "abcbaca"; 3478 a->Call(env->Global(), 0, NULL); 3479 CHECK_EQ(StrLength(expected_step_sequence), 3480 break_point_hit_count); 3481 3482 // Step through invocation of a with step next. 3483 step_action = StepNext; 3484 break_point_hit_count = 0; 3485 expected_step_sequence = "aaa"; 3486 a->Call(env->Global(), 0, NULL); 3487 CHECK_EQ(StrLength(expected_step_sequence), 3488 break_point_hit_count); 3489 3490 // Step through invocation of a with step out. 3491 step_action = StepOut; 3492 break_point_hit_count = 0; 3493 expected_step_sequence = "a"; 3494 a->Call(env->Global(), 0, NULL); 3495 CHECK_EQ(StrLength(expected_step_sequence), 3496 break_point_hit_count); 3497 3498 // Get rid of the debug event listener. 3499 v8::Debug::SetDebugEventListener(NULL); 3500 CheckDebuggerUnloaded(); 3501} 3502 3503 3504TEST(StepInOutTree) { 3505 v8::HandleScope scope; 3506 DebugLocalContext env; 3507 3508 // Create a function for checking the function when hitting a break point. 3509 frame_function_name = CompileFunction(&env, 3510 frame_function_name_source, 3511 "frame_function_name"); 3512 3513 // Register a debug event listener which steps and counts. 3514 v8::Debug::SetDebugEventListener(DebugEventStepSequence); 3515 3516 // Create a function for testing stepping. Run it to allow it to get 3517 // optimized. 3518 const char* src = "function a() {b(c(d()),d());c(d());d()}; " 3519 "function b(x,y) {c();}; " 3520 "function c(x) {}; " 3521 "function d() {}; " 3522 "a(); b(); c(); d()"; 3523 v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); 3524 SetBreakPoint(a, 0); 3525 3526 // Step through invocation of a with step in. 3527 step_action = StepIn; 3528 break_point_hit_count = 0; 3529 expected_step_sequence = "adacadabcbadacada"; 3530 a->Call(env->Global(), 0, NULL); 3531 CHECK_EQ(StrLength(expected_step_sequence), 3532 break_point_hit_count); 3533 3534 // Step through invocation of a with step next. 3535 step_action = StepNext; 3536 break_point_hit_count = 0; 3537 expected_step_sequence = "aaaa"; 3538 a->Call(env->Global(), 0, NULL); 3539 CHECK_EQ(StrLength(expected_step_sequence), 3540 break_point_hit_count); 3541 3542 // Step through invocation of a with step out. 3543 step_action = StepOut; 3544 break_point_hit_count = 0; 3545 expected_step_sequence = "a"; 3546 a->Call(env->Global(), 0, NULL); 3547 CHECK_EQ(StrLength(expected_step_sequence), 3548 break_point_hit_count); 3549 3550 // Get rid of the debug event listener. 3551 v8::Debug::SetDebugEventListener(NULL); 3552 CheckDebuggerUnloaded(true); 3553} 3554 3555 3556TEST(StepInOutBranch) { 3557 v8::HandleScope scope; 3558 DebugLocalContext env; 3559 3560 // Create a function for checking the function when hitting a break point. 3561 frame_function_name = CompileFunction(&env, 3562 frame_function_name_source, 3563 "frame_function_name"); 3564 3565 // Register a debug event listener which steps and counts. 3566 v8::Debug::SetDebugEventListener(DebugEventStepSequence); 3567 3568 // Create a function for testing stepping. Run it to allow it to get 3569 // optimized. 3570 const char* src = "function a() {b(false);c();}; " 3571 "function b(x) {if(x){c();};}; " 3572 "function c() {}; " 3573 "a(); b(); c()"; 3574 v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); 3575 SetBreakPoint(a, 0); 3576 3577 // Step through invocation of a. 3578 step_action = StepIn; 3579 break_point_hit_count = 0; 3580 expected_step_sequence = "abbaca"; 3581 a->Call(env->Global(), 0, NULL); 3582 CHECK_EQ(StrLength(expected_step_sequence), 3583 break_point_hit_count); 3584 3585 // Get rid of the debug event listener. 3586 v8::Debug::SetDebugEventListener(NULL); 3587 CheckDebuggerUnloaded(); 3588} 3589 3590 3591// Test that step in does not step into native functions. 3592TEST(DebugStepNatives) { 3593 v8::HandleScope scope; 3594 DebugLocalContext env; 3595 3596 // Create a function for testing stepping. 3597 v8::Local<v8::Function> foo = CompileFunction( 3598 &env, 3599 "function foo(){debugger;Math.sin(1);}", 3600 "foo"); 3601 3602 // Register a debug event listener which steps and counts. 3603 v8::Debug::SetDebugEventListener(DebugEventStep); 3604 3605 step_action = StepIn; 3606 break_point_hit_count = 0; 3607 foo->Call(env->Global(), 0, NULL); 3608 3609 // With stepping all break locations are hit. 3610 CHECK_EQ(3, break_point_hit_count); 3611 3612 v8::Debug::SetDebugEventListener(NULL); 3613 CheckDebuggerUnloaded(); 3614 3615 // Register a debug event listener which just counts. 3616 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); 3617 3618 break_point_hit_count = 0; 3619 foo->Call(env->Global(), 0, NULL); 3620 3621 // Without stepping only active break points are hit. 3622 CHECK_EQ(1, break_point_hit_count); 3623 3624 v8::Debug::SetDebugEventListener(NULL); 3625 CheckDebuggerUnloaded(); 3626} 3627 3628 3629// Test that step in works with function.apply. 3630TEST(DebugStepFunctionApply) { 3631 v8::HandleScope scope; 3632 DebugLocalContext env; 3633 3634 // Create a function for testing stepping. 3635 v8::Local<v8::Function> foo = CompileFunction( 3636 &env, 3637 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" 3638 "function foo(){ debugger; bar.apply(this, [1,2,3]); }", 3639 "foo"); 3640 3641 // Register a debug event listener which steps and counts. 3642 v8::Debug::SetDebugEventListener(DebugEventStep); 3643 3644 step_action = StepIn; 3645 break_point_hit_count = 0; 3646 foo->Call(env->Global(), 0, NULL); 3647 3648 // With stepping all break locations are hit. 3649 CHECK_EQ(7, break_point_hit_count); 3650 3651 v8::Debug::SetDebugEventListener(NULL); 3652 CheckDebuggerUnloaded(); 3653 3654 // Register a debug event listener which just counts. 3655 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); 3656 3657 break_point_hit_count = 0; 3658 foo->Call(env->Global(), 0, NULL); 3659 3660 // Without stepping only the debugger statement is hit. 3661 CHECK_EQ(1, break_point_hit_count); 3662 3663 v8::Debug::SetDebugEventListener(NULL); 3664 CheckDebuggerUnloaded(); 3665} 3666 3667 3668// Test that step in works with function.call. 3669TEST(DebugStepFunctionCall) { 3670 v8::HandleScope scope; 3671 DebugLocalContext env; 3672 3673 // Create a function for testing stepping. 3674 v8::Local<v8::Function> foo = CompileFunction( 3675 &env, 3676 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" 3677 "function foo(a){ debugger;" 3678 " if (a) {" 3679 " bar.call(this, 1, 2, 3);" 3680 " } else {" 3681 " bar.call(this, 0);" 3682 " }" 3683 "}", 3684 "foo"); 3685 3686 // Register a debug event listener which steps and counts. 3687 v8::Debug::SetDebugEventListener(DebugEventStep); 3688 step_action = StepIn; 3689 3690 // Check stepping where the if condition in bar is false. 3691 break_point_hit_count = 0; 3692 foo->Call(env->Global(), 0, NULL); 3693 CHECK_EQ(6, break_point_hit_count); 3694 3695 // Check stepping where the if condition in bar is true. 3696 break_point_hit_count = 0; 3697 const int argc = 1; 3698 v8::Handle<v8::Value> argv[argc] = { v8::True() }; 3699 foo->Call(env->Global(), argc, argv); 3700 CHECK_EQ(8, break_point_hit_count); 3701 3702 v8::Debug::SetDebugEventListener(NULL); 3703 CheckDebuggerUnloaded(); 3704 3705 // Register a debug event listener which just counts. 3706 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount); 3707 3708 break_point_hit_count = 0; 3709 foo->Call(env->Global(), 0, NULL); 3710 3711 // Without stepping only the debugger statement is hit. 3712 CHECK_EQ(1, break_point_hit_count); 3713 3714 v8::Debug::SetDebugEventListener(NULL); 3715 CheckDebuggerUnloaded(); 3716} 3717 3718 3719// Tests that breakpoint will be hit if it's set in script. 3720TEST(PauseInScript) { 3721 v8::HandleScope scope; 3722 DebugLocalContext env; 3723 env.ExposeDebug(); 3724 3725 // Register a debug event listener which counts. 3726 v8::Debug::SetDebugEventListener(DebugEventCounter); 3727 3728 // Create a script that returns a function. 3729 const char* src = "(function (evt) {})"; 3730 const char* script_name = "StepInHandlerTest"; 3731 3732 // Set breakpoint in the script. 3733 SetScriptBreakPointByNameFromJS(script_name, 0, -1); 3734 break_point_hit_count = 0; 3735 3736 v8::ScriptOrigin origin(v8::String::New(script_name), v8::Integer::New(0)); 3737 v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(src), 3738 &origin); 3739 v8::Local<v8::Value> r = script->Run(); 3740 3741 CHECK(r->IsFunction()); 3742 CHECK_EQ(1, break_point_hit_count); 3743 3744 // Get rid of the debug event listener. 3745 v8::Debug::SetDebugEventListener(NULL); 3746 CheckDebuggerUnloaded(); 3747} 3748 3749 3750// Test break on exceptions. For each exception break combination the number 3751// of debug event exception callbacks and message callbacks are collected. The 3752// number of debug event exception callbacks are used to check that the 3753// debugger is called correctly and the number of message callbacks is used to 3754// check that uncaught exceptions are still returned even if there is a break 3755// for them. 3756TEST(BreakOnException) { 3757 v8::HandleScope scope; 3758 DebugLocalContext env; 3759 env.ExposeDebug(); 3760 3761 v8::internal::Isolate::Current()->TraceException(false); 3762 3763 // Create functions for testing break on exception. 3764 v8::Local<v8::Function> throws = 3765 CompileFunction(&env, "function throws(){throw 1;}", "throws"); 3766 v8::Local<v8::Function> caught = 3767 CompileFunction(&env, 3768 "function caught(){try {throws();} catch(e) {};}", 3769 "caught"); 3770 v8::Local<v8::Function> notCaught = 3771 CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); 3772 3773 v8::V8::AddMessageListener(MessageCallbackCount); 3774 v8::Debug::SetDebugEventListener(DebugEventCounter); 3775 3776 // Initial state should be no break on exceptions. 3777 DebugEventCounterClear(); 3778 MessageCallbackCountClear(); 3779 caught->Call(env->Global(), 0, NULL); 3780 CHECK_EQ(0, exception_hit_count); 3781 CHECK_EQ(0, uncaught_exception_hit_count); 3782 CHECK_EQ(0, message_callback_count); 3783 notCaught->Call(env->Global(), 0, NULL); 3784 CHECK_EQ(0, exception_hit_count); 3785 CHECK_EQ(0, uncaught_exception_hit_count); 3786 CHECK_EQ(1, message_callback_count); 3787 3788 // No break on exception 3789 DebugEventCounterClear(); 3790 MessageCallbackCountClear(); 3791 ChangeBreakOnException(false, false); 3792 caught->Call(env->Global(), 0, NULL); 3793 CHECK_EQ(0, exception_hit_count); 3794 CHECK_EQ(0, uncaught_exception_hit_count); 3795 CHECK_EQ(0, message_callback_count); 3796 notCaught->Call(env->Global(), 0, NULL); 3797 CHECK_EQ(0, exception_hit_count); 3798 CHECK_EQ(0, uncaught_exception_hit_count); 3799 CHECK_EQ(1, message_callback_count); 3800 3801 // Break on uncaught exception 3802 DebugEventCounterClear(); 3803 MessageCallbackCountClear(); 3804 ChangeBreakOnException(false, true); 3805 caught->Call(env->Global(), 0, NULL); 3806 CHECK_EQ(0, exception_hit_count); 3807 CHECK_EQ(0, uncaught_exception_hit_count); 3808 CHECK_EQ(0, message_callback_count); 3809 notCaught->Call(env->Global(), 0, NULL); 3810 CHECK_EQ(1, exception_hit_count); 3811 CHECK_EQ(1, uncaught_exception_hit_count); 3812 CHECK_EQ(1, message_callback_count); 3813 3814 // Break on exception and uncaught exception 3815 DebugEventCounterClear(); 3816 MessageCallbackCountClear(); 3817 ChangeBreakOnException(true, true); 3818 caught->Call(env->Global(), 0, NULL); 3819 CHECK_EQ(1, exception_hit_count); 3820 CHECK_EQ(0, uncaught_exception_hit_count); 3821 CHECK_EQ(0, message_callback_count); 3822 notCaught->Call(env->Global(), 0, NULL); 3823 CHECK_EQ(2, exception_hit_count); 3824 CHECK_EQ(1, uncaught_exception_hit_count); 3825 CHECK_EQ(1, message_callback_count); 3826 3827 // Break on exception 3828 DebugEventCounterClear(); 3829 MessageCallbackCountClear(); 3830 ChangeBreakOnException(true, false); 3831 caught->Call(env->Global(), 0, NULL); 3832 CHECK_EQ(1, exception_hit_count); 3833 CHECK_EQ(0, uncaught_exception_hit_count); 3834 CHECK_EQ(0, message_callback_count); 3835 notCaught->Call(env->Global(), 0, NULL); 3836 CHECK_EQ(2, exception_hit_count); 3837 CHECK_EQ(1, uncaught_exception_hit_count); 3838 CHECK_EQ(1, message_callback_count); 3839 3840 // No break on exception using JavaScript 3841 DebugEventCounterClear(); 3842 MessageCallbackCountClear(); 3843 ChangeBreakOnExceptionFromJS(false, false); 3844 caught->Call(env->Global(), 0, NULL); 3845 CHECK_EQ(0, exception_hit_count); 3846 CHECK_EQ(0, uncaught_exception_hit_count); 3847 CHECK_EQ(0, message_callback_count); 3848 notCaught->Call(env->Global(), 0, NULL); 3849 CHECK_EQ(0, exception_hit_count); 3850 CHECK_EQ(0, uncaught_exception_hit_count); 3851 CHECK_EQ(1, message_callback_count); 3852 3853 // Break on uncaught exception using JavaScript 3854 DebugEventCounterClear(); 3855 MessageCallbackCountClear(); 3856 ChangeBreakOnExceptionFromJS(false, true); 3857 caught->Call(env->Global(), 0, NULL); 3858 CHECK_EQ(0, exception_hit_count); 3859 CHECK_EQ(0, uncaught_exception_hit_count); 3860 CHECK_EQ(0, message_callback_count); 3861 notCaught->Call(env->Global(), 0, NULL); 3862 CHECK_EQ(1, exception_hit_count); 3863 CHECK_EQ(1, uncaught_exception_hit_count); 3864 CHECK_EQ(1, message_callback_count); 3865 3866 // Break on exception and uncaught exception using JavaScript 3867 DebugEventCounterClear(); 3868 MessageCallbackCountClear(); 3869 ChangeBreakOnExceptionFromJS(true, true); 3870 caught->Call(env->Global(), 0, NULL); 3871 CHECK_EQ(1, exception_hit_count); 3872 CHECK_EQ(0, message_callback_count); 3873 CHECK_EQ(0, uncaught_exception_hit_count); 3874 notCaught->Call(env->Global(), 0, NULL); 3875 CHECK_EQ(2, exception_hit_count); 3876 CHECK_EQ(1, uncaught_exception_hit_count); 3877 CHECK_EQ(1, message_callback_count); 3878 3879 // Break on exception using JavaScript 3880 DebugEventCounterClear(); 3881 MessageCallbackCountClear(); 3882 ChangeBreakOnExceptionFromJS(true, false); 3883 caught->Call(env->Global(), 0, NULL); 3884 CHECK_EQ(1, exception_hit_count); 3885 CHECK_EQ(0, uncaught_exception_hit_count); 3886 CHECK_EQ(0, message_callback_count); 3887 notCaught->Call(env->Global(), 0, NULL); 3888 CHECK_EQ(2, exception_hit_count); 3889 CHECK_EQ(1, uncaught_exception_hit_count); 3890 CHECK_EQ(1, message_callback_count); 3891 3892 v8::Debug::SetDebugEventListener(NULL); 3893 CheckDebuggerUnloaded(); 3894 v8::V8::RemoveMessageListeners(MessageCallbackCount); 3895} 3896 3897 3898// Test break on exception from compiler errors. When compiling using 3899// v8::Script::Compile there is no JavaScript stack whereas when compiling using 3900// eval there are JavaScript frames. 3901TEST(BreakOnCompileException) { 3902 v8::HandleScope scope; 3903 DebugLocalContext env; 3904 3905 // For this test, we want to break on uncaught exceptions: 3906 ChangeBreakOnException(false, true); 3907 3908 v8::internal::Isolate::Current()->TraceException(false); 3909 3910 // Create a function for checking the function when hitting a break point. 3911 frame_count = CompileFunction(&env, frame_count_source, "frame_count"); 3912 3913 v8::V8::AddMessageListener(MessageCallbackCount); 3914 v8::Debug::SetDebugEventListener(DebugEventCounter); 3915 3916 DebugEventCounterClear(); 3917 MessageCallbackCountClear(); 3918 3919 // Check initial state. 3920 CHECK_EQ(0, exception_hit_count); 3921 CHECK_EQ(0, uncaught_exception_hit_count); 3922 CHECK_EQ(0, message_callback_count); 3923 CHECK_EQ(-1, last_js_stack_height); 3924 3925 // Throws SyntaxError: Unexpected end of input 3926 v8::Script::Compile(v8::String::New("+++")); 3927 CHECK_EQ(1, exception_hit_count); 3928 CHECK_EQ(1, uncaught_exception_hit_count); 3929 CHECK_EQ(1, message_callback_count); 3930 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. 3931 3932 // Throws SyntaxError: Unexpected identifier 3933 v8::Script::Compile(v8::String::New("x x")); 3934 CHECK_EQ(2, exception_hit_count); 3935 CHECK_EQ(2, uncaught_exception_hit_count); 3936 CHECK_EQ(2, message_callback_count); 3937 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. 3938 3939 // Throws SyntaxError: Unexpected end of input 3940 v8::Script::Compile(v8::String::New("eval('+++')"))->Run(); 3941 CHECK_EQ(3, exception_hit_count); 3942 CHECK_EQ(3, uncaught_exception_hit_count); 3943 CHECK_EQ(3, message_callback_count); 3944 CHECK_EQ(1, last_js_stack_height); 3945 3946 // Throws SyntaxError: Unexpected identifier 3947 v8::Script::Compile(v8::String::New("eval('x x')"))->Run(); 3948 CHECK_EQ(4, exception_hit_count); 3949 CHECK_EQ(4, uncaught_exception_hit_count); 3950 CHECK_EQ(4, message_callback_count); 3951 CHECK_EQ(1, last_js_stack_height); 3952} 3953 3954 3955TEST(StepWithException) { 3956 v8::HandleScope scope; 3957 DebugLocalContext env; 3958 3959 // For this test, we want to break on uncaught exceptions: 3960 ChangeBreakOnException(false, true); 3961 3962 // Create a function for checking the function when hitting a break point. 3963 frame_function_name = CompileFunction(&env, 3964 frame_function_name_source, 3965 "frame_function_name"); 3966 3967 // Register a debug event listener which steps and counts. 3968 v8::Debug::SetDebugEventListener(DebugEventStepSequence); 3969 3970 // Create functions for testing stepping. 3971 const char* src = "function a() { n(); }; " 3972 "function b() { c(); }; " 3973 "function c() { n(); }; " 3974 "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " 3975 "function e() { n(); }; " 3976 "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " 3977 "function g() { h(); }; " 3978 "function h() { x = 1; throw 1; }; "; 3979 3980 // Step through invocation of a. 3981 v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); 3982 SetBreakPoint(a, 0); 3983 step_action = StepIn; 3984 break_point_hit_count = 0; 3985 expected_step_sequence = "aa"; 3986 a->Call(env->Global(), 0, NULL); 3987 CHECK_EQ(StrLength(expected_step_sequence), 3988 break_point_hit_count); 3989 3990 // Step through invocation of b + c. 3991 v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); 3992 SetBreakPoint(b, 0); 3993 step_action = StepIn; 3994 break_point_hit_count = 0; 3995 expected_step_sequence = "bcc"; 3996 b->Call(env->Global(), 0, NULL); 3997 CHECK_EQ(StrLength(expected_step_sequence), 3998 break_point_hit_count); 3999 // Step through invocation of d + e. 4000 v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); 4001 SetBreakPoint(d, 0); 4002 ChangeBreakOnException(false, true); 4003 step_action = StepIn; 4004 break_point_hit_count = 0; 4005 expected_step_sequence = "ddedd"; 4006 d->Call(env->Global(), 0, NULL); 4007 CHECK_EQ(StrLength(expected_step_sequence), 4008 break_point_hit_count); 4009 4010 // Step through invocation of d + e now with break on caught exceptions. 4011 ChangeBreakOnException(true, true); 4012 step_action = StepIn; 4013 break_point_hit_count = 0; 4014 expected_step_sequence = "ddeedd"; 4015 d->Call(env->Global(), 0, NULL); 4016 CHECK_EQ(StrLength(expected_step_sequence), 4017 break_point_hit_count); 4018 4019 // Step through invocation of f + g + h. 4020 v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); 4021 SetBreakPoint(f, 0); 4022 ChangeBreakOnException(false, true); 4023 step_action = StepIn; 4024 break_point_hit_count = 0; 4025 expected_step_sequence = "ffghhff"; 4026 f->Call(env->Global(), 0, NULL); 4027 CHECK_EQ(StrLength(expected_step_sequence), 4028 break_point_hit_count); 4029 4030 // Step through invocation of f + g + h now with break on caught exceptions. 4031 ChangeBreakOnException(true, true); 4032 step_action = StepIn; 4033 break_point_hit_count = 0; 4034 expected_step_sequence = "ffghhhff"; 4035 f->Call(env->Global(), 0, NULL); 4036 CHECK_EQ(StrLength(expected_step_sequence), 4037 break_point_hit_count); 4038 4039 // Get rid of the debug event listener. 4040 v8::Debug::SetDebugEventListener(NULL); 4041 CheckDebuggerUnloaded(); 4042} 4043 4044 4045TEST(DebugBreak) { 4046 v8::HandleScope scope; 4047 DebugLocalContext env; 4048 4049 // This test should be run with option --verify-heap. As --verify-heap is 4050 // only available in debug mode only check for it in that case. 4051#ifdef DEBUG 4052 CHECK(v8::internal::FLAG_verify_heap); 4053#endif 4054 4055 // Register a debug event listener which sets the break flag and counts. 4056 v8::Debug::SetDebugEventListener(DebugEventBreak); 4057 4058 // Create a function for testing stepping. 4059 const char* src = "function f0() {}" 4060 "function f1(x1) {}" 4061 "function f2(x1,x2) {}" 4062 "function f3(x1,x2,x3) {}"; 4063 v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); 4064 v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); 4065 v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); 4066 v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); 4067 4068 // Call the function to make sure it is compiled. 4069 v8::Handle<v8::Value> argv[] = { v8::Number::New(1), 4070 v8::Number::New(1), 4071 v8::Number::New(1), 4072 v8::Number::New(1) }; 4073 4074 // Call all functions to make sure that they are compiled. 4075 f0->Call(env->Global(), 0, NULL); 4076 f1->Call(env->Global(), 0, NULL); 4077 f2->Call(env->Global(), 0, NULL); 4078 f3->Call(env->Global(), 0, NULL); 4079 4080 // Set the debug break flag. 4081 v8::Debug::DebugBreak(); 4082 4083 // Call all functions with different argument count. 4084 break_point_hit_count = 0; 4085 for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) { 4086 f0->Call(env->Global(), i, argv); 4087 f1->Call(env->Global(), i, argv); 4088 f2->Call(env->Global(), i, argv); 4089 f3->Call(env->Global(), i, argv); 4090 } 4091 4092 // One break for each function called. 4093 CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count); 4094 4095 // Get rid of the debug event listener. 4096 v8::Debug::SetDebugEventListener(NULL); 4097 CheckDebuggerUnloaded(); 4098} 4099 4100 4101// Test to ensure that JavaScript code keeps running while the debug break 4102// through the stack limit flag is set but breaks are disabled. 4103TEST(DisableBreak) { 4104 v8::HandleScope scope; 4105 DebugLocalContext env; 4106 4107 // Register a debug event listener which sets the break flag and counts. 4108 v8::Debug::SetDebugEventListener(DebugEventCounter); 4109 4110 // Create a function for testing stepping. 4111 const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; 4112 v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); 4113 4114 // Set the debug break flag. 4115 v8::Debug::DebugBreak(); 4116 4117 // Call all functions with different argument count. 4118 break_point_hit_count = 0; 4119 f->Call(env->Global(), 0, NULL); 4120 CHECK_EQ(1, break_point_hit_count); 4121 4122 { 4123 v8::Debug::DebugBreak(); 4124 v8::internal::DisableBreak disable_break(true); 4125 f->Call(env->Global(), 0, NULL); 4126 CHECK_EQ(1, break_point_hit_count); 4127 } 4128 4129 f->Call(env->Global(), 0, NULL); 4130 CHECK_EQ(2, break_point_hit_count); 4131 4132 // Get rid of the debug event listener. 4133 v8::Debug::SetDebugEventListener(NULL); 4134 CheckDebuggerUnloaded(); 4135} 4136 4137static const char* kSimpleExtensionSource = 4138 "(function Foo() {" 4139 " return 4;" 4140 "})() "; 4141 4142// http://crbug.com/28933 4143// Test that debug break is disabled when bootstrapper is active. 4144TEST(NoBreakWhenBootstrapping) { 4145 v8::HandleScope scope; 4146 4147 // Register a debug event listener which sets the break flag and counts. 4148 v8::Debug::SetDebugEventListener(DebugEventCounter); 4149 4150 // Set the debug break flag. 4151 v8::Debug::DebugBreak(); 4152 break_point_hit_count = 0; 4153 { 4154 // Create a context with an extension to make sure that some JavaScript 4155 // code is executed during bootstrapping. 4156 v8::RegisterExtension(new v8::Extension("simpletest", 4157 kSimpleExtensionSource)); 4158 const char* extension_names[] = { "simpletest" }; 4159 v8::ExtensionConfiguration extensions(1, extension_names); 4160 v8::Persistent<v8::Context> context = v8::Context::New(&extensions); 4161 context.Dispose(); 4162 } 4163 // Check that no DebugBreak events occured during the context creation. 4164 CHECK_EQ(0, break_point_hit_count); 4165 4166 // Get rid of the debug event listener. 4167 v8::Debug::SetDebugEventListener(NULL); 4168 CheckDebuggerUnloaded(); 4169} 4170 4171static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) { 4172 v8::Handle<v8::Array> result = v8::Array::New(3); 4173 result->Set(v8::Integer::New(0), v8::String::New("a")); 4174 result->Set(v8::Integer::New(1), v8::String::New("b")); 4175 result->Set(v8::Integer::New(2), v8::String::New("c")); 4176 return result; 4177} 4178 4179 4180static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) { 4181 v8::Handle<v8::Array> result = v8::Array::New(2); 4182 result->Set(v8::Integer::New(0), v8::Number::New(1)); 4183 result->Set(v8::Integer::New(1), v8::Number::New(10)); 4184 return result; 4185} 4186 4187 4188static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name, 4189 const v8::AccessorInfo& info) { 4190 v8::String::AsciiValue n(name); 4191 if (strcmp(*n, "a") == 0) { 4192 return v8::String::New("AA"); 4193 } else if (strcmp(*n, "b") == 0) { 4194 return v8::String::New("BB"); 4195 } else if (strcmp(*n, "c") == 0) { 4196 return v8::String::New("CC"); 4197 } else { 4198 return v8::Undefined(); 4199 } 4200 4201 return name; 4202} 4203 4204 4205static v8::Handle<v8::Value> IndexedGetter(uint32_t index, 4206 const v8::AccessorInfo& info) { 4207 return v8::Number::New(index + 1); 4208} 4209 4210 4211TEST(InterceptorPropertyMirror) { 4212 // Create a V8 environment with debug access. 4213 v8::HandleScope scope; 4214 DebugLocalContext env; 4215 env.ExposeDebug(); 4216 4217 // Create object with named interceptor. 4218 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); 4219 named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); 4220 env->Global()->Set(v8::String::New("intercepted_named"), 4221 named->NewInstance()); 4222 4223 // Create object with indexed interceptor. 4224 v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(); 4225 indexed->SetIndexedPropertyHandler(IndexedGetter, 4226 NULL, 4227 NULL, 4228 NULL, 4229 IndexedEnum); 4230 env->Global()->Set(v8::String::New("intercepted_indexed"), 4231 indexed->NewInstance()); 4232 4233 // Create object with both named and indexed interceptor. 4234 v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New(); 4235 both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum); 4236 both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum); 4237 env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance()); 4238 4239 // Get mirrors for the three objects with interceptor. 4240 CompileRun( 4241 "named_mirror = debug.MakeMirror(intercepted_named);" 4242 "indexed_mirror = debug.MakeMirror(intercepted_indexed);" 4243 "both_mirror = debug.MakeMirror(intercepted_both)"); 4244 CHECK(CompileRun( 4245 "named_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4246 CHECK(CompileRun( 4247 "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4248 CHECK(CompileRun( 4249 "both_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4250 4251 // Get the property names from the interceptors 4252 CompileRun( 4253 "named_names = named_mirror.propertyNames();" 4254 "indexed_names = indexed_mirror.propertyNames();" 4255 "both_names = both_mirror.propertyNames()"); 4256 CHECK_EQ(3, CompileRun("named_names.length")->Int32Value()); 4257 CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value()); 4258 CHECK_EQ(5, CompileRun("both_names.length")->Int32Value()); 4259 4260 // Check the expected number of properties. 4261 const char* source; 4262 source = "named_mirror.properties().length"; 4263 CHECK_EQ(3, CompileRun(source)->Int32Value()); 4264 4265 source = "indexed_mirror.properties().length"; 4266 CHECK_EQ(2, CompileRun(source)->Int32Value()); 4267 4268 source = "both_mirror.properties().length"; 4269 CHECK_EQ(5, CompileRun(source)->Int32Value()); 4270 4271 // 1 is PropertyKind.Named; 4272 source = "both_mirror.properties(1).length"; 4273 CHECK_EQ(3, CompileRun(source)->Int32Value()); 4274 4275 // 2 is PropertyKind.Indexed; 4276 source = "both_mirror.properties(2).length"; 4277 CHECK_EQ(2, CompileRun(source)->Int32Value()); 4278 4279 // 3 is PropertyKind.Named | PropertyKind.Indexed; 4280 source = "both_mirror.properties(3).length"; 4281 CHECK_EQ(5, CompileRun(source)->Int32Value()); 4282 4283 // Get the interceptor properties for the object with only named interceptor. 4284 CompileRun("named_values = named_mirror.properties()"); 4285 4286 // Check that the properties are interceptor properties. 4287 for (int i = 0; i < 3; i++) { 4288 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 4289 OS::SNPrintF(buffer, 4290 "named_values[%d] instanceof debug.PropertyMirror", i); 4291 CHECK(CompileRun(buffer.start())->BooleanValue()); 4292 4293 // 5 is PropertyType.Interceptor 4294 OS::SNPrintF(buffer, "named_values[%d].propertyType()", i); 4295 CHECK_EQ(5, CompileRun(buffer.start())->Int32Value()); 4296 4297 OS::SNPrintF(buffer, "named_values[%d].isNative()", i); 4298 CHECK(CompileRun(buffer.start())->BooleanValue()); 4299 } 4300 4301 // Get the interceptor properties for the object with only indexed 4302 // interceptor. 4303 CompileRun("indexed_values = indexed_mirror.properties()"); 4304 4305 // Check that the properties are interceptor properties. 4306 for (int i = 0; i < 2; i++) { 4307 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 4308 OS::SNPrintF(buffer, 4309 "indexed_values[%d] instanceof debug.PropertyMirror", i); 4310 CHECK(CompileRun(buffer.start())->BooleanValue()); 4311 } 4312 4313 // Get the interceptor properties for the object with both types of 4314 // interceptors. 4315 CompileRun("both_values = both_mirror.properties()"); 4316 4317 // Check that the properties are interceptor properties. 4318 for (int i = 0; i < 5; i++) { 4319 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; 4320 OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); 4321 CHECK(CompileRun(buffer.start())->BooleanValue()); 4322 } 4323 4324 // Check the property names. 4325 source = "both_values[0].name() == 'a'"; 4326 CHECK(CompileRun(source)->BooleanValue()); 4327 4328 source = "both_values[1].name() == 'b'"; 4329 CHECK(CompileRun(source)->BooleanValue()); 4330 4331 source = "both_values[2].name() == 'c'"; 4332 CHECK(CompileRun(source)->BooleanValue()); 4333 4334 source = "both_values[3].name() == 1"; 4335 CHECK(CompileRun(source)->BooleanValue()); 4336 4337 source = "both_values[4].name() == 10"; 4338 CHECK(CompileRun(source)->BooleanValue()); 4339} 4340 4341 4342TEST(HiddenPrototypePropertyMirror) { 4343 // Create a V8 environment with debug access. 4344 v8::HandleScope scope; 4345 DebugLocalContext env; 4346 env.ExposeDebug(); 4347 4348 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); 4349 t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0)); 4350 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); 4351 t1->SetHiddenPrototype(true); 4352 t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1)); 4353 v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); 4354 t2->SetHiddenPrototype(true); 4355 t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2)); 4356 v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); 4357 t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3)); 4358 4359 // Create object and set them on the global object. 4360 v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance(); 4361 env->Global()->Set(v8::String::New("o0"), o0); 4362 v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance(); 4363 env->Global()->Set(v8::String::New("o1"), o1); 4364 v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance(); 4365 env->Global()->Set(v8::String::New("o2"), o2); 4366 v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance(); 4367 env->Global()->Set(v8::String::New("o3"), o3); 4368 4369 // Get mirrors for the four objects. 4370 CompileRun( 4371 "o0_mirror = debug.MakeMirror(o0);" 4372 "o1_mirror = debug.MakeMirror(o1);" 4373 "o2_mirror = debug.MakeMirror(o2);" 4374 "o3_mirror = debug.MakeMirror(o3)"); 4375 CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4376 CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4377 CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4378 CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4379 4380 // Check that each object has one property. 4381 CHECK_EQ(1, CompileRun( 4382 "o0_mirror.propertyNames().length")->Int32Value()); 4383 CHECK_EQ(1, CompileRun( 4384 "o1_mirror.propertyNames().length")->Int32Value()); 4385 CHECK_EQ(1, CompileRun( 4386 "o2_mirror.propertyNames().length")->Int32Value()); 4387 CHECK_EQ(1, CompileRun( 4388 "o3_mirror.propertyNames().length")->Int32Value()); 4389 4390 // Set o1 as prototype for o0. o1 has the hidden prototype flag so all 4391 // properties on o1 should be seen on o0. 4392 o0->Set(v8::String::New("__proto__"), o1); 4393 CHECK_EQ(2, CompileRun( 4394 "o0_mirror.propertyNames().length")->Int32Value()); 4395 CHECK_EQ(0, CompileRun( 4396 "o0_mirror.property('x').value().value()")->Int32Value()); 4397 CHECK_EQ(1, CompileRun( 4398 "o0_mirror.property('y').value().value()")->Int32Value()); 4399 4400 // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden 4401 // prototype flag. o2 also has the hidden prototype flag so all properties 4402 // on o2 should be seen on o0 as well as properties on o1. 4403 o0->Set(v8::String::New("__proto__"), o2); 4404 CHECK_EQ(3, CompileRun( 4405 "o0_mirror.propertyNames().length")->Int32Value()); 4406 CHECK_EQ(0, CompileRun( 4407 "o0_mirror.property('x').value().value()")->Int32Value()); 4408 CHECK_EQ(1, CompileRun( 4409 "o0_mirror.property('y').value().value()")->Int32Value()); 4410 CHECK_EQ(2, CompileRun( 4411 "o0_mirror.property('z').value().value()")->Int32Value()); 4412 4413 // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and 4414 // o2 has the hidden prototype flag. o3 does not have the hidden prototype 4415 // flag so properties on o3 should not be seen on o0 whereas the properties 4416 // from o1 and o2 should still be seen on o0. 4417 // Final prototype chain: o0 -> o1 -> o2 -> o3 4418 // Hidden prototypes: ^^ ^^ 4419 o0->Set(v8::String::New("__proto__"), o3); 4420 CHECK_EQ(3, CompileRun( 4421 "o0_mirror.propertyNames().length")->Int32Value()); 4422 CHECK_EQ(1, CompileRun( 4423 "o3_mirror.propertyNames().length")->Int32Value()); 4424 CHECK_EQ(0, CompileRun( 4425 "o0_mirror.property('x').value().value()")->Int32Value()); 4426 CHECK_EQ(1, CompileRun( 4427 "o0_mirror.property('y').value().value()")->Int32Value()); 4428 CHECK_EQ(2, CompileRun( 4429 "o0_mirror.property('z').value().value()")->Int32Value()); 4430 CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue()); 4431 4432 // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. 4433 CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue()); 4434} 4435 4436 4437static v8::Handle<v8::Value> ProtperyXNativeGetter( 4438 v8::Local<v8::String> property, const v8::AccessorInfo& info) { 4439 return v8::Integer::New(10); 4440} 4441 4442 4443TEST(NativeGetterPropertyMirror) { 4444 // Create a V8 environment with debug access. 4445 v8::HandleScope scope; 4446 DebugLocalContext env; 4447 env.ExposeDebug(); 4448 4449 v8::Handle<v8::String> name = v8::String::New("x"); 4450 // Create object with named accessor. 4451 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); 4452 named->SetAccessor(name, &ProtperyXNativeGetter, NULL, 4453 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); 4454 4455 // Create object with named property getter. 4456 env->Global()->Set(v8::String::New("instance"), named->NewInstance()); 4457 CHECK_EQ(10, CompileRun("instance.x")->Int32Value()); 4458 4459 // Get mirror for the object with property getter. 4460 CompileRun("instance_mirror = debug.MakeMirror(instance);"); 4461 CHECK(CompileRun( 4462 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4463 4464 CompileRun("named_names = instance_mirror.propertyNames();"); 4465 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); 4466 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); 4467 CHECK(CompileRun( 4468 "instance_mirror.property('x').value().isNumber()")->BooleanValue()); 4469 CHECK(CompileRun( 4470 "instance_mirror.property('x').value().value() == 10")->BooleanValue()); 4471} 4472 4473 4474static v8::Handle<v8::Value> ProtperyXNativeGetterThrowingError( 4475 v8::Local<v8::String> property, const v8::AccessorInfo& info) { 4476 return CompileRun("throw new Error('Error message');"); 4477} 4478 4479 4480TEST(NativeGetterThrowingErrorPropertyMirror) { 4481 // Create a V8 environment with debug access. 4482 v8::HandleScope scope; 4483 DebugLocalContext env; 4484 env.ExposeDebug(); 4485 4486 v8::Handle<v8::String> name = v8::String::New("x"); 4487 // Create object with named accessor. 4488 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); 4489 named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, 4490 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None); 4491 4492 // Create object with named property getter. 4493 env->Global()->Set(v8::String::New("instance"), named->NewInstance()); 4494 4495 // Get mirror for the object with property getter. 4496 CompileRun("instance_mirror = debug.MakeMirror(instance);"); 4497 CHECK(CompileRun( 4498 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4499 CompileRun("named_names = instance_mirror.propertyNames();"); 4500 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); 4501 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue()); 4502 CHECK(CompileRun( 4503 "instance_mirror.property('x').value().isError()")->BooleanValue()); 4504 4505 // Check that the message is that passed to the Error constructor. 4506 CHECK(CompileRun( 4507 "instance_mirror.property('x').value().message() == 'Error message'")-> 4508 BooleanValue()); 4509} 4510 4511 4512// Test that hidden properties object is not returned as an unnamed property 4513// among regular properties. 4514// See http://crbug.com/26491 4515TEST(NoHiddenProperties) { 4516 // Create a V8 environment with debug access. 4517 v8::HandleScope scope; 4518 DebugLocalContext env; 4519 env.ExposeDebug(); 4520 4521 // Create an object in the global scope. 4522 const char* source = "var obj = {a: 1};"; 4523 v8::Script::Compile(v8::String::New(source))->Run(); 4524 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( 4525 env->Global()->Get(v8::String::New("obj"))); 4526 // Set a hidden property on the object. 4527 obj->SetHiddenValue(v8::String::New("v8::test-debug::a"), 4528 v8::Int32::New(11)); 4529 4530 // Get mirror for the object with property getter. 4531 CompileRun("var obj_mirror = debug.MakeMirror(obj);"); 4532 CHECK(CompileRun( 4533 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4534 CompileRun("var named_names = obj_mirror.propertyNames();"); 4535 // There should be exactly one property. But there is also an unnamed 4536 // property whose value is hidden properties dictionary. The latter 4537 // property should not be in the list of reguar properties. 4538 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value()); 4539 CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue()); 4540 CHECK(CompileRun( 4541 "obj_mirror.property('a').value().value() == 1")->BooleanValue()); 4542 4543 // Object created by t0 will become hidden prototype of object 'obj'. 4544 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); 4545 t0->InstanceTemplate()->Set(v8::String::New("b"), v8::Number::New(2)); 4546 t0->SetHiddenPrototype(true); 4547 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); 4548 t1->InstanceTemplate()->Set(v8::String::New("c"), v8::Number::New(3)); 4549 4550 // Create proto objects, add hidden properties to them and set them on 4551 // the global object. 4552 v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance(); 4553 protoObj->SetHiddenValue(v8::String::New("v8::test-debug::b"), 4554 v8::Int32::New(12)); 4555 env->Global()->Set(v8::String::New("protoObj"), protoObj); 4556 v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance(); 4557 grandProtoObj->SetHiddenValue(v8::String::New("v8::test-debug::c"), 4558 v8::Int32::New(13)); 4559 env->Global()->Set(v8::String::New("grandProtoObj"), grandProtoObj); 4560 4561 // Setting prototypes: obj->protoObj->grandProtoObj 4562 protoObj->Set(v8::String::New("__proto__"), grandProtoObj); 4563 obj->Set(v8::String::New("__proto__"), protoObj); 4564 4565 // Get mirror for the object with property getter. 4566 CompileRun("var obj_mirror = debug.MakeMirror(obj);"); 4567 CHECK(CompileRun( 4568 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue()); 4569 CompileRun("var named_names = obj_mirror.propertyNames();"); 4570 // There should be exactly two properties - one from the object itself and 4571 // another from its hidden prototype. 4572 CHECK_EQ(2, CompileRun("named_names.length")->Int32Value()); 4573 CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" 4574 "named_names[1] == 'b'")->BooleanValue()); 4575 CHECK(CompileRun( 4576 "obj_mirror.property('a').value().value() == 1")->BooleanValue()); 4577 CHECK(CompileRun( 4578 "obj_mirror.property('b').value().value() == 2")->BooleanValue()); 4579} 4580 4581 4582// Multithreaded tests of JSON debugger protocol 4583 4584// Support classes 4585 4586// Provides synchronization between k threads, where k is an input to the 4587// constructor. The Wait() call blocks a thread until it is called for the 4588// k'th time, then all calls return. Each ThreadBarrier object can only 4589// be used once. 4590class ThreadBarrier { 4591 public: 4592 explicit ThreadBarrier(int num_threads); 4593 ~ThreadBarrier(); 4594 void Wait(); 4595 private: 4596 int num_threads_; 4597 int num_blocked_; 4598 v8::internal::Mutex* lock_; 4599 v8::internal::Semaphore* sem_; 4600 bool invalid_; 4601}; 4602 4603ThreadBarrier::ThreadBarrier(int num_threads) 4604 : num_threads_(num_threads), num_blocked_(0) { 4605 lock_ = OS::CreateMutex(); 4606 sem_ = OS::CreateSemaphore(0); 4607 invalid_ = false; // A barrier may only be used once. Then it is invalid. 4608} 4609 4610// Do not call, due to race condition with Wait(). 4611// Could be resolved with Pthread condition variables. 4612ThreadBarrier::~ThreadBarrier() { 4613 lock_->Lock(); 4614 delete lock_; 4615 delete sem_; 4616} 4617 4618void ThreadBarrier::Wait() { 4619 lock_->Lock(); 4620 CHECK(!invalid_); 4621 if (num_blocked_ == num_threads_ - 1) { 4622 // Signal and unblock all waiting threads. 4623 for (int i = 0; i < num_threads_ - 1; ++i) { 4624 sem_->Signal(); 4625 } 4626 invalid_ = true; 4627 printf("BARRIER\n\n"); 4628 fflush(stdout); 4629 lock_->Unlock(); 4630 } else { // Wait for the semaphore. 4631 ++num_blocked_; 4632 lock_->Unlock(); // Potential race condition with destructor because 4633 sem_->Wait(); // these two lines are not atomic. 4634 } 4635} 4636 4637// A set containing enough barriers and semaphores for any of the tests. 4638class Barriers { 4639 public: 4640 Barriers(); 4641 void Initialize(); 4642 ThreadBarrier barrier_1; 4643 ThreadBarrier barrier_2; 4644 ThreadBarrier barrier_3; 4645 ThreadBarrier barrier_4; 4646 ThreadBarrier barrier_5; 4647 v8::internal::Semaphore* semaphore_1; 4648 v8::internal::Semaphore* semaphore_2; 4649}; 4650 4651Barriers::Barriers() : barrier_1(2), barrier_2(2), 4652 barrier_3(2), barrier_4(2), barrier_5(2) {} 4653 4654void Barriers::Initialize() { 4655 semaphore_1 = OS::CreateSemaphore(0); 4656 semaphore_2 = OS::CreateSemaphore(0); 4657} 4658 4659 4660// We match parts of the message to decide if it is a break message. 4661bool IsBreakEventMessage(char *message) { 4662 const char* type_event = "\"type\":\"event\""; 4663 const char* event_break = "\"event\":\"break\""; 4664 // Does the message contain both type:event and event:break? 4665 return strstr(message, type_event) != NULL && 4666 strstr(message, event_break) != NULL; 4667} 4668 4669 4670// We match parts of the message to decide if it is a exception message. 4671bool IsExceptionEventMessage(char *message) { 4672 const char* type_event = "\"type\":\"event\""; 4673 const char* event_exception = "\"event\":\"exception\""; 4674 // Does the message contain both type:event and event:exception? 4675 return strstr(message, type_event) != NULL && 4676 strstr(message, event_exception) != NULL; 4677} 4678 4679 4680// We match the message wether it is an evaluate response message. 4681bool IsEvaluateResponseMessage(char* message) { 4682 const char* type_response = "\"type\":\"response\""; 4683 const char* command_evaluate = "\"command\":\"evaluate\""; 4684 // Does the message contain both type:response and command:evaluate? 4685 return strstr(message, type_response) != NULL && 4686 strstr(message, command_evaluate) != NULL; 4687} 4688 4689 4690static int StringToInt(const char* s) { 4691 return atoi(s); // NOLINT 4692} 4693 4694 4695// We match parts of the message to get evaluate result int value. 4696int GetEvaluateIntResult(char *message) { 4697 const char* value = "\"value\":"; 4698 char* pos = strstr(message, value); 4699 if (pos == NULL) { 4700 return -1; 4701 } 4702 int res = -1; 4703 res = StringToInt(pos + strlen(value)); 4704 return res; 4705} 4706 4707 4708// We match parts of the message to get hit breakpoint id. 4709int GetBreakpointIdFromBreakEventMessage(char *message) { 4710 const char* breakpoints = "\"breakpoints\":["; 4711 char* pos = strstr(message, breakpoints); 4712 if (pos == NULL) { 4713 return -1; 4714 } 4715 int res = -1; 4716 res = StringToInt(pos + strlen(breakpoints)); 4717 return res; 4718} 4719 4720 4721// We match parts of the message to get total frames number. 4722int GetTotalFramesInt(char *message) { 4723 const char* prefix = "\"totalFrames\":"; 4724 char* pos = strstr(message, prefix); 4725 if (pos == NULL) { 4726 return -1; 4727 } 4728 pos += strlen(prefix); 4729 int res = StringToInt(pos); 4730 return res; 4731} 4732 4733 4734// We match parts of the message to get source line. 4735int GetSourceLineFromBreakEventMessage(char *message) { 4736 const char* source_line = "\"sourceLine\":"; 4737 char* pos = strstr(message, source_line); 4738 if (pos == NULL) { 4739 return -1; 4740 } 4741 int res = -1; 4742 res = StringToInt(pos + strlen(source_line)); 4743 return res; 4744} 4745 4746/* Test MessageQueues */ 4747/* Tests the message queues that hold debugger commands and 4748 * response messages to the debugger. Fills queues and makes 4749 * them grow. 4750 */ 4751Barriers message_queue_barriers; 4752 4753// This is the debugger thread, that executes no v8 calls except 4754// placing JSON debugger commands in the queue. 4755class MessageQueueDebuggerThread : public v8::internal::Thread { 4756 public: 4757 MessageQueueDebuggerThread() 4758 : Thread("MessageQueueDebuggerThread") { } 4759 void Run(); 4760}; 4761 4762static void MessageHandler(const uint16_t* message, int length, 4763 v8::Debug::ClientData* client_data) { 4764 static char print_buffer[1000]; 4765 Utf16ToAscii(message, length, print_buffer); 4766 if (IsBreakEventMessage(print_buffer)) { 4767 // Lets test script wait until break occurs to send commands. 4768 // Signals when a break is reported. 4769 message_queue_barriers.semaphore_2->Signal(); 4770 } 4771 4772 // Allow message handler to block on a semaphore, to test queueing of 4773 // messages while blocked. 4774 message_queue_barriers.semaphore_1->Wait(); 4775} 4776 4777void MessageQueueDebuggerThread::Run() { 4778 const int kBufferSize = 1000; 4779 uint16_t buffer_1[kBufferSize]; 4780 uint16_t buffer_2[kBufferSize]; 4781 const char* command_1 = 4782 "{\"seq\":117," 4783 "\"type\":\"request\"," 4784 "\"command\":\"evaluate\"," 4785 "\"arguments\":{\"expression\":\"1+2\"}}"; 4786 const char* command_2 = 4787 "{\"seq\":118," 4788 "\"type\":\"request\"," 4789 "\"command\":\"evaluate\"," 4790 "\"arguments\":{\"expression\":\"1+a\"}}"; 4791 const char* command_3 = 4792 "{\"seq\":119," 4793 "\"type\":\"request\"," 4794 "\"command\":\"evaluate\"," 4795 "\"arguments\":{\"expression\":\"c.d * b\"}}"; 4796 const char* command_continue = 4797 "{\"seq\":106," 4798 "\"type\":\"request\"," 4799 "\"command\":\"continue\"}"; 4800 const char* command_single_step = 4801 "{\"seq\":107," 4802 "\"type\":\"request\"," 4803 "\"command\":\"continue\"," 4804 "\"arguments\":{\"stepaction\":\"next\"}}"; 4805 4806 /* Interleaved sequence of actions by the two threads:*/ 4807 // Main thread compiles and runs source_1 4808 message_queue_barriers.semaphore_1->Signal(); 4809 message_queue_barriers.barrier_1.Wait(); 4810 // Post 6 commands, filling the command queue and making it expand. 4811 // These calls return immediately, but the commands stay on the queue 4812 // until the execution of source_2. 4813 // Note: AsciiToUtf16 executes before SendCommand, so command is copied 4814 // to buffer before buffer is sent to SendCommand. 4815 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); 4816 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); 4817 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); 4818 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); 4819 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); 4820 message_queue_barriers.barrier_2.Wait(); 4821 // Main thread compiles and runs source_2. 4822 // Queued commands are executed at the start of compilation of source_2( 4823 // beforeCompile event). 4824 // Free the message handler to process all the messages from the queue. 7 4825 // messages are expected: 2 afterCompile events and 5 responses. 4826 // All the commands added so far will fail to execute as long as call stack 4827 // is empty on beforeCompile event. 4828 for (int i = 0; i < 6 ; ++i) { 4829 message_queue_barriers.semaphore_1->Signal(); 4830 } 4831 message_queue_barriers.barrier_3.Wait(); 4832 // Main thread compiles and runs source_3. 4833 // Don't stop in the afterCompile handler. 4834 message_queue_barriers.semaphore_1->Signal(); 4835 // source_3 includes a debugger statement, which causes a break event. 4836 // Wait on break event from hitting "debugger" statement 4837 message_queue_barriers.semaphore_2->Wait(); 4838 // These should execute after the "debugger" statement in source_2 4839 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1)); 4840 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2)); 4841 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2)); 4842 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2)); 4843 // Run after 2 break events, 4 responses. 4844 for (int i = 0; i < 6 ; ++i) { 4845 message_queue_barriers.semaphore_1->Signal(); 4846 } 4847 // Wait on break event after a single step executes. 4848 message_queue_barriers.semaphore_2->Wait(); 4849 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1)); 4850 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2)); 4851 // Run after 2 responses. 4852 for (int i = 0; i < 2 ; ++i) { 4853 message_queue_barriers.semaphore_1->Signal(); 4854 } 4855 // Main thread continues running source_3 to end, waits for this thread. 4856} 4857 4858 4859// This thread runs the v8 engine. 4860TEST(MessageQueues) { 4861 MessageQueueDebuggerThread message_queue_debugger_thread; 4862 4863 // Create a V8 environment 4864 v8::HandleScope scope; 4865 DebugLocalContext env; 4866 message_queue_barriers.Initialize(); 4867 v8::Debug::SetMessageHandler(MessageHandler); 4868 message_queue_debugger_thread.Start(); 4869 4870 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; 4871 const char* source_2 = "e = 17;"; 4872 const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;"; 4873 4874 // See MessageQueueDebuggerThread::Run for interleaved sequence of 4875 // API calls and events in the two threads. 4876 CompileRun(source_1); 4877 message_queue_barriers.barrier_1.Wait(); 4878 message_queue_barriers.barrier_2.Wait(); 4879 CompileRun(source_2); 4880 message_queue_barriers.barrier_3.Wait(); 4881 CompileRun(source_3); 4882 message_queue_debugger_thread.Join(); 4883 fflush(stdout); 4884} 4885 4886 4887class TestClientData : public v8::Debug::ClientData { 4888 public: 4889 TestClientData() { 4890 constructor_call_counter++; 4891 } 4892 virtual ~TestClientData() { 4893 destructor_call_counter++; 4894 } 4895 4896 static void ResetCounters() { 4897 constructor_call_counter = 0; 4898 destructor_call_counter = 0; 4899 } 4900 4901 static int constructor_call_counter; 4902 static int destructor_call_counter; 4903}; 4904 4905int TestClientData::constructor_call_counter = 0; 4906int TestClientData::destructor_call_counter = 0; 4907 4908 4909// Tests that MessageQueue doesn't destroy client data when expands and 4910// does destroy when it dies. 4911TEST(MessageQueueExpandAndDestroy) { 4912 TestClientData::ResetCounters(); 4913 { // Create a scope for the queue. 4914 CommandMessageQueue queue(1); 4915 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4916 new TestClientData())); 4917 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4918 new TestClientData())); 4919 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4920 new TestClientData())); 4921 CHECK_EQ(0, TestClientData::destructor_call_counter); 4922 queue.Get().Dispose(); 4923 CHECK_EQ(1, TestClientData::destructor_call_counter); 4924 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4925 new TestClientData())); 4926 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4927 new TestClientData())); 4928 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4929 new TestClientData())); 4930 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4931 new TestClientData())); 4932 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(), 4933 new TestClientData())); 4934 CHECK_EQ(1, TestClientData::destructor_call_counter); 4935 queue.Get().Dispose(); 4936 CHECK_EQ(2, TestClientData::destructor_call_counter); 4937 } 4938 // All the client data should be destroyed when the queue is destroyed. 4939 CHECK_EQ(TestClientData::destructor_call_counter, 4940 TestClientData::destructor_call_counter); 4941} 4942 4943 4944static int handled_client_data_instances_count = 0; 4945static void MessageHandlerCountingClientData( 4946 const v8::Debug::Message& message) { 4947 if (message.GetClientData() != NULL) { 4948 handled_client_data_instances_count++; 4949 } 4950} 4951 4952 4953// Tests that all client data passed to the debugger are sent to the handler. 4954TEST(SendClientDataToHandler) { 4955 // Create a V8 environment 4956 v8::HandleScope scope; 4957 DebugLocalContext env; 4958 TestClientData::ResetCounters(); 4959 handled_client_data_instances_count = 0; 4960 v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData); 4961 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;"; 4962 const int kBufferSize = 1000; 4963 uint16_t buffer[kBufferSize]; 4964 const char* command_1 = 4965 "{\"seq\":117," 4966 "\"type\":\"request\"," 4967 "\"command\":\"evaluate\"," 4968 "\"arguments\":{\"expression\":\"1+2\"}}"; 4969 const char* command_2 = 4970 "{\"seq\":118," 4971 "\"type\":\"request\"," 4972 "\"command\":\"evaluate\"," 4973 "\"arguments\":{\"expression\":\"1+a\"}}"; 4974 const char* command_continue = 4975 "{\"seq\":106," 4976 "\"type\":\"request\"," 4977 "\"command\":\"continue\"}"; 4978 4979 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer), 4980 new TestClientData()); 4981 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL); 4982 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), 4983 new TestClientData()); 4984 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), 4985 new TestClientData()); 4986 // All the messages will be processed on beforeCompile event. 4987 CompileRun(source_1); 4988 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); 4989 CHECK_EQ(3, TestClientData::constructor_call_counter); 4990 CHECK_EQ(TestClientData::constructor_call_counter, 4991 handled_client_data_instances_count); 4992 CHECK_EQ(TestClientData::constructor_call_counter, 4993 TestClientData::destructor_call_counter); 4994} 4995 4996 4997/* Test ThreadedDebugging */ 4998/* This test interrupts a running infinite loop that is 4999 * occupying the v8 thread by a break command from the 5000 * debugger thread. It then changes the value of a 5001 * global object, to make the loop terminate. 5002 */ 5003 5004Barriers threaded_debugging_barriers; 5005 5006class V8Thread : public v8::internal::Thread { 5007 public: 5008 V8Thread() : Thread("V8Thread") { } 5009 void Run(); 5010}; 5011 5012class DebuggerThread : public v8::internal::Thread { 5013 public: 5014 DebuggerThread() : Thread("DebuggerThread") { } 5015 void Run(); 5016}; 5017 5018 5019static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) { 5020 threaded_debugging_barriers.barrier_1.Wait(); 5021 return v8::Undefined(); 5022} 5023 5024 5025static void ThreadedMessageHandler(const v8::Debug::Message& message) { 5026 static char print_buffer[1000]; 5027 v8::String::Value json(message.GetJSON()); 5028 Utf16ToAscii(*json, json.length(), print_buffer); 5029 if (IsBreakEventMessage(print_buffer)) { 5030 // Check that we are inside the while loop. 5031 int source_line = GetSourceLineFromBreakEventMessage(print_buffer); 5032 CHECK(8 <= source_line && source_line <= 13); 5033 threaded_debugging_barriers.barrier_2.Wait(); 5034 } 5035} 5036 5037 5038void V8Thread::Run() { 5039 const char* source = 5040 "flag = true;\n" 5041 "function bar( new_value ) {\n" 5042 " flag = new_value;\n" 5043 " return \"Return from bar(\" + new_value + \")\";\n" 5044 "}\n" 5045 "\n" 5046 "function foo() {\n" 5047 " var x = 1;\n" 5048 " while ( flag == true ) {\n" 5049 " if ( x == 1 ) {\n" 5050 " ThreadedAtBarrier1();\n" 5051 " }\n" 5052 " x = x + 1;\n" 5053 " }\n" 5054 "}\n" 5055 "\n" 5056 "foo();\n"; 5057 5058 v8::V8::Initialize(); 5059 v8::HandleScope scope; 5060 DebugLocalContext env; 5061 v8::Debug::SetMessageHandler2(&ThreadedMessageHandler); 5062 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); 5063 global_template->Set(v8::String::New("ThreadedAtBarrier1"), 5064 v8::FunctionTemplate::New(ThreadedAtBarrier1)); 5065 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); 5066 v8::Context::Scope context_scope(context); 5067 5068 CompileRun(source); 5069} 5070 5071void DebuggerThread::Run() { 5072 const int kBufSize = 1000; 5073 uint16_t buffer[kBufSize]; 5074 5075 const char* command_1 = "{\"seq\":102," 5076 "\"type\":\"request\"," 5077 "\"command\":\"evaluate\"," 5078 "\"arguments\":{\"expression\":\"bar(false)\"}}"; 5079 const char* command_2 = "{\"seq\":103," 5080 "\"type\":\"request\"," 5081 "\"command\":\"continue\"}"; 5082 5083 threaded_debugging_barriers.barrier_1.Wait(); 5084 v8::Debug::DebugBreak(); 5085 threaded_debugging_barriers.barrier_2.Wait(); 5086 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); 5087 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); 5088} 5089 5090 5091TEST(ThreadedDebugging) { 5092 DebuggerThread debugger_thread; 5093 V8Thread v8_thread; 5094 5095 // Create a V8 environment 5096 threaded_debugging_barriers.Initialize(); 5097 5098 v8_thread.Start(); 5099 debugger_thread.Start(); 5100 5101 v8_thread.Join(); 5102 debugger_thread.Join(); 5103} 5104 5105/* Test RecursiveBreakpoints */ 5106/* In this test, the debugger evaluates a function with a breakpoint, after 5107 * hitting a breakpoint in another function. We do this with both values 5108 * of the flag enabling recursive breakpoints, and verify that the second 5109 * breakpoint is hit when enabled, and missed when disabled. 5110 */ 5111 5112class BreakpointsV8Thread : public v8::internal::Thread { 5113 public: 5114 BreakpointsV8Thread() : Thread("BreakpointsV8Thread") { } 5115 void Run(); 5116}; 5117 5118class BreakpointsDebuggerThread : public v8::internal::Thread { 5119 public: 5120 explicit BreakpointsDebuggerThread(bool global_evaluate) 5121 : Thread("BreakpointsDebuggerThread"), 5122 global_evaluate_(global_evaluate) {} 5123 void Run(); 5124 5125 private: 5126 bool global_evaluate_; 5127}; 5128 5129 5130Barriers* breakpoints_barriers; 5131int break_event_breakpoint_id; 5132int evaluate_int_result; 5133 5134static void BreakpointsMessageHandler(const v8::Debug::Message& message) { 5135 static char print_buffer[1000]; 5136 v8::String::Value json(message.GetJSON()); 5137 Utf16ToAscii(*json, json.length(), print_buffer); 5138 5139 if (IsBreakEventMessage(print_buffer)) { 5140 break_event_breakpoint_id = 5141 GetBreakpointIdFromBreakEventMessage(print_buffer); 5142 breakpoints_barriers->semaphore_1->Signal(); 5143 } else if (IsEvaluateResponseMessage(print_buffer)) { 5144 evaluate_int_result = GetEvaluateIntResult(print_buffer); 5145 breakpoints_barriers->semaphore_1->Signal(); 5146 } 5147} 5148 5149 5150void BreakpointsV8Thread::Run() { 5151 const char* source_1 = "var y_global = 3;\n" 5152 "function cat( new_value ) {\n" 5153 " var x = new_value;\n" 5154 " y_global = y_global + 4;\n" 5155 " x = 3 * x + 1;\n" 5156 " y_global = y_global + 5;\n" 5157 " return x;\n" 5158 "}\n" 5159 "\n" 5160 "function dog() {\n" 5161 " var x = 1;\n" 5162 " x = y_global;" 5163 " var z = 3;" 5164 " x += 100;\n" 5165 " return x;\n" 5166 "}\n" 5167 "\n"; 5168 const char* source_2 = "cat(17);\n" 5169 "cat(19);\n"; 5170 5171 v8::V8::Initialize(); 5172 v8::HandleScope scope; 5173 DebugLocalContext env; 5174 v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler); 5175 5176 CompileRun(source_1); 5177 breakpoints_barriers->barrier_1.Wait(); 5178 breakpoints_barriers->barrier_2.Wait(); 5179 CompileRun(source_2); 5180} 5181 5182 5183void BreakpointsDebuggerThread::Run() { 5184 const int kBufSize = 1000; 5185 uint16_t buffer[kBufSize]; 5186 5187 const char* command_1 = "{\"seq\":101," 5188 "\"type\":\"request\"," 5189 "\"command\":\"setbreakpoint\"," 5190 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; 5191 const char* command_2 = "{\"seq\":102," 5192 "\"type\":\"request\"," 5193 "\"command\":\"setbreakpoint\"," 5194 "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}"; 5195 const char* command_3; 5196 if (this->global_evaluate_) { 5197 command_3 = "{\"seq\":103," 5198 "\"type\":\"request\"," 5199 "\"command\":\"evaluate\"," 5200 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false," 5201 "\"global\":true}}"; 5202 } else { 5203 command_3 = "{\"seq\":103," 5204 "\"type\":\"request\"," 5205 "\"command\":\"evaluate\"," 5206 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}"; 5207 } 5208 const char* command_4; 5209 if (this->global_evaluate_) { 5210 command_4 = "{\"seq\":104," 5211 "\"type\":\"request\"," 5212 "\"command\":\"evaluate\"," 5213 "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true," 5214 "\"global\":true}}"; 5215 } else { 5216 command_4 = "{\"seq\":104," 5217 "\"type\":\"request\"," 5218 "\"command\":\"evaluate\"," 5219 "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}"; 5220 } 5221 const char* command_5 = "{\"seq\":105," 5222 "\"type\":\"request\"," 5223 "\"command\":\"continue\"}"; 5224 const char* command_6 = "{\"seq\":106," 5225 "\"type\":\"request\"," 5226 "\"command\":\"continue\"}"; 5227 const char* command_7; 5228 if (this->global_evaluate_) { 5229 command_7 = "{\"seq\":107," 5230 "\"type\":\"request\"," 5231 "\"command\":\"evaluate\"," 5232 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true," 5233 "\"global\":true}}"; 5234 } else { 5235 command_7 = "{\"seq\":107," 5236 "\"type\":\"request\"," 5237 "\"command\":\"evaluate\"," 5238 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}"; 5239 } 5240 const char* command_8 = "{\"seq\":108," 5241 "\"type\":\"request\"," 5242 "\"command\":\"continue\"}"; 5243 5244 5245 // v8 thread initializes, runs source_1 5246 breakpoints_barriers->barrier_1.Wait(); 5247 // 1:Set breakpoint in cat() (will get id 1). 5248 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); 5249 // 2:Set breakpoint in dog() (will get id 2). 5250 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); 5251 breakpoints_barriers->barrier_2.Wait(); 5252 // V8 thread starts compiling source_2. 5253 // Automatic break happens, to run queued commands 5254 // breakpoints_barriers->semaphore_1->Wait(); 5255 // Commands 1 through 3 run, thread continues. 5256 // v8 thread runs source_2 to breakpoint in cat(). 5257 // message callback receives break event. 5258 breakpoints_barriers->semaphore_1->Wait(); 5259 // Must have hit breakpoint #1. 5260 CHECK_EQ(1, break_event_breakpoint_id); 5261 // 4:Evaluate dog() (which has a breakpoint). 5262 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer)); 5263 // V8 thread hits breakpoint in dog(). 5264 breakpoints_barriers->semaphore_1->Wait(); // wait for break event 5265 // Must have hit breakpoint #2. 5266 CHECK_EQ(2, break_event_breakpoint_id); 5267 // 5:Evaluate (x + 1). 5268 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer)); 5269 // Evaluate (x + 1) finishes. 5270 breakpoints_barriers->semaphore_1->Wait(); 5271 // Must have result 108. 5272 CHECK_EQ(108, evaluate_int_result); 5273 // 6:Continue evaluation of dog(). 5274 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer)); 5275 // Evaluate dog() finishes. 5276 breakpoints_barriers->semaphore_1->Wait(); 5277 // Must have result 107. 5278 CHECK_EQ(107, evaluate_int_result); 5279 // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint 5280 // in cat(19). 5281 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer)); 5282 // Message callback gets break event. 5283 breakpoints_barriers->semaphore_1->Wait(); // wait for break event 5284 // Must have hit breakpoint #1. 5285 CHECK_EQ(1, break_event_breakpoint_id); 5286 // 8: Evaluate dog() with breaks disabled. 5287 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer)); 5288 // Evaluate dog() finishes. 5289 breakpoints_barriers->semaphore_1->Wait(); 5290 // Must have result 116. 5291 CHECK_EQ(116, evaluate_int_result); 5292 // 9: Continue evaluation of source2, reach end. 5293 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer)); 5294} 5295 5296void TestRecursiveBreakpointsGeneric(bool global_evaluate) { 5297 i::FLAG_debugger_auto_break = true; 5298 5299 BreakpointsDebuggerThread breakpoints_debugger_thread(global_evaluate); 5300 BreakpointsV8Thread breakpoints_v8_thread; 5301 5302 // Create a V8 environment 5303 Barriers stack_allocated_breakpoints_barriers; 5304 stack_allocated_breakpoints_barriers.Initialize(); 5305 breakpoints_barriers = &stack_allocated_breakpoints_barriers; 5306 5307 breakpoints_v8_thread.Start(); 5308 breakpoints_debugger_thread.Start(); 5309 5310 breakpoints_v8_thread.Join(); 5311 breakpoints_debugger_thread.Join(); 5312} 5313 5314TEST(RecursiveBreakpoints) { 5315 TestRecursiveBreakpointsGeneric(false); 5316} 5317 5318TEST(RecursiveBreakpointsGlobal) { 5319 TestRecursiveBreakpointsGeneric(true); 5320} 5321 5322 5323static void DummyDebugEventListener(v8::DebugEvent event, 5324 v8::Handle<v8::Object> exec_state, 5325 v8::Handle<v8::Object> event_data, 5326 v8::Handle<v8::Value> data) { 5327} 5328 5329 5330TEST(SetDebugEventListenerOnUninitializedVM) { 5331 v8::Debug::SetDebugEventListener(DummyDebugEventListener); 5332} 5333 5334 5335static void DummyMessageHandler(const v8::Debug::Message& message) { 5336} 5337 5338 5339TEST(SetMessageHandlerOnUninitializedVM) { 5340 v8::Debug::SetMessageHandler2(DummyMessageHandler); 5341} 5342 5343 5344TEST(DebugBreakOnUninitializedVM) { 5345 v8::Debug::DebugBreak(); 5346} 5347 5348 5349TEST(SendCommandToUninitializedVM) { 5350 const char* dummy_command = "{}"; 5351 uint16_t dummy_buffer[80]; 5352 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer); 5353 v8::Debug::SendCommand(dummy_buffer, dummy_length); 5354} 5355 5356 5357// Source for a JavaScript function which returns the data parameter of a 5358// function called in the context of the debugger. If no data parameter is 5359// passed it throws an exception. 5360static const char* debugger_call_with_data_source = 5361 "function debugger_call_with_data(exec_state, data) {" 5362 " if (data) return data;" 5363 " throw 'No data!'" 5364 "}"; 5365v8::Handle<v8::Function> debugger_call_with_data; 5366 5367 5368// Source for a JavaScript function which returns the data parameter of a 5369// function called in the context of the debugger. If no data parameter is 5370// passed it throws an exception. 5371static const char* debugger_call_with_closure_source = 5372 "var x = 3;" 5373 "(function (exec_state) {" 5374 " if (exec_state.y) return x - 1;" 5375 " exec_state.y = x;" 5376 " return exec_state.y" 5377 "})"; 5378v8::Handle<v8::Function> debugger_call_with_closure; 5379 5380// Function to retrieve the number of JavaScript frames by calling a JavaScript 5381// in the debugger. 5382static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) { 5383 CHECK(v8::Debug::Call(frame_count)->IsNumber()); 5384 CHECK_EQ(args[0]->Int32Value(), 5385 v8::Debug::Call(frame_count)->Int32Value()); 5386 return v8::Undefined(); 5387} 5388 5389 5390// Function to retrieve the source line of the top JavaScript frame by calling a 5391// JavaScript function in the debugger. 5392static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) { 5393 CHECK(v8::Debug::Call(frame_source_line)->IsNumber()); 5394 CHECK_EQ(args[0]->Int32Value(), 5395 v8::Debug::Call(frame_source_line)->Int32Value()); 5396 return v8::Undefined(); 5397} 5398 5399 5400// Function to test passing an additional parameter to a JavaScript function 5401// called in the debugger. It also tests that functions called in the debugger 5402// can throw exceptions. 5403static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) { 5404 v8::Handle<v8::String> data = v8::String::New("Test"); 5405 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString()); 5406 5407 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); 5408 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); 5409 5410 v8::TryCatch catcher; 5411 v8::Debug::Call(debugger_call_with_data); 5412 CHECK(catcher.HasCaught()); 5413 CHECK(catcher.Exception()->IsString()); 5414 5415 return v8::Undefined(); 5416} 5417 5418 5419// Function to test using a JavaScript with closure in the debugger. 5420static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) { 5421 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber()); 5422 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value()); 5423 return v8::Undefined(); 5424} 5425 5426 5427// Test functions called through the debugger. 5428TEST(CallFunctionInDebugger) { 5429 // Create and enter a context with the functions CheckFrameCount, 5430 // CheckSourceLine and CheckDataParameter installed. 5431 v8::HandleScope scope; 5432 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); 5433 global_template->Set(v8::String::New("CheckFrameCount"), 5434 v8::FunctionTemplate::New(CheckFrameCount)); 5435 global_template->Set(v8::String::New("CheckSourceLine"), 5436 v8::FunctionTemplate::New(CheckSourceLine)); 5437 global_template->Set(v8::String::New("CheckDataParameter"), 5438 v8::FunctionTemplate::New(CheckDataParameter)); 5439 global_template->Set(v8::String::New("CheckClosure"), 5440 v8::FunctionTemplate::New(CheckClosure)); 5441 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); 5442 v8::Context::Scope context_scope(context); 5443 5444 // Compile a function for checking the number of JavaScript frames. 5445 v8::Script::Compile(v8::String::New(frame_count_source))->Run(); 5446 frame_count = v8::Local<v8::Function>::Cast( 5447 context->Global()->Get(v8::String::New("frame_count"))); 5448 5449 // Compile a function for returning the source line for the top frame. 5450 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run(); 5451 frame_source_line = v8::Local<v8::Function>::Cast( 5452 context->Global()->Get(v8::String::New("frame_source_line"))); 5453 5454 // Compile a function returning the data parameter. 5455 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run(); 5456 debugger_call_with_data = v8::Local<v8::Function>::Cast( 5457 context->Global()->Get(v8::String::New("debugger_call_with_data"))); 5458 5459 // Compile a function capturing closure. 5460 debugger_call_with_closure = v8::Local<v8::Function>::Cast( 5461 v8::Script::Compile( 5462 v8::String::New(debugger_call_with_closure_source))->Run()); 5463 5464 // Calling a function through the debugger returns 0 frames if there are 5465 // no JavaScript frames. 5466 CHECK_EQ(v8::Integer::New(0), v8::Debug::Call(frame_count)); 5467 5468 // Test that the number of frames can be retrieved. 5469 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run(); 5470 v8::Script::Compile(v8::String::New("function f() {" 5471 " CheckFrameCount(2);" 5472 "}; f()"))->Run(); 5473 5474 // Test that the source line can be retrieved. 5475 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run(); 5476 v8::Script::Compile(v8::String::New("function f() {\n" 5477 " CheckSourceLine(1)\n" 5478 " CheckSourceLine(2)\n" 5479 " CheckSourceLine(3)\n" 5480 "}; f()"))->Run(); 5481 5482 // Test that a parameter can be passed to a function called in the debugger. 5483 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run(); 5484 5485 // Test that a function with closure can be run in the debugger. 5486 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run(); 5487 5488 5489 // Test that the source line is correct when there is a line offset. 5490 v8::ScriptOrigin origin(v8::String::New("test"), 5491 v8::Integer::New(7)); 5492 v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run(); 5493 v8::Script::Compile(v8::String::New("function f() {\n" 5494 " CheckSourceLine(8)\n" 5495 " CheckSourceLine(9)\n" 5496 " CheckSourceLine(10)\n" 5497 "}; f()"), &origin)->Run(); 5498} 5499 5500 5501// Debugger message handler which counts the number of breaks. 5502static void SendContinueCommand(); 5503static void MessageHandlerBreakPointHitCount( 5504 const v8::Debug::Message& message) { 5505 if (message.IsEvent() && message.GetEvent() == v8::Break) { 5506 // Count the number of breaks. 5507 break_point_hit_count++; 5508 5509 SendContinueCommand(); 5510 } 5511} 5512 5513 5514// Test that clearing the debug event listener actually clears all break points 5515// and related information. 5516TEST(DebuggerUnload) { 5517 DebugLocalContext env; 5518 5519 // Check debugger is unloaded before it is used. 5520 CheckDebuggerUnloaded(); 5521 5522 // Set a debug event listener. 5523 break_point_hit_count = 0; 5524 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 5525 v8::Undefined()); 5526 { 5527 v8::HandleScope scope; 5528 // Create a couple of functions for the test. 5529 v8::Local<v8::Function> foo = 5530 CompileFunction(&env, "function foo(){x=1}", "foo"); 5531 v8::Local<v8::Function> bar = 5532 CompileFunction(&env, "function bar(){y=2}", "bar"); 5533 5534 // Set some break points. 5535 SetBreakPoint(foo, 0); 5536 SetBreakPoint(foo, 4); 5537 SetBreakPoint(bar, 0); 5538 SetBreakPoint(bar, 4); 5539 5540 // Make sure that the break points are there. 5541 break_point_hit_count = 0; 5542 foo->Call(env->Global(), 0, NULL); 5543 CHECK_EQ(2, break_point_hit_count); 5544 bar->Call(env->Global(), 0, NULL); 5545 CHECK_EQ(4, break_point_hit_count); 5546 } 5547 5548 // Remove the debug event listener without clearing breakpoints. Do this 5549 // outside a handle scope. 5550 v8::Debug::SetDebugEventListener(NULL); 5551 CheckDebuggerUnloaded(true); 5552 5553 // Now set a debug message handler. 5554 break_point_hit_count = 0; 5555 v8::Debug::SetMessageHandler2(MessageHandlerBreakPointHitCount); 5556 { 5557 v8::HandleScope scope; 5558 5559 // Get the test functions again. 5560 v8::Local<v8::Function> foo = 5561 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 5562 v8::Local<v8::Function> bar = 5563 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); 5564 5565 foo->Call(env->Global(), 0, NULL); 5566 CHECK_EQ(0, break_point_hit_count); 5567 5568 // Set break points and run again. 5569 SetBreakPoint(foo, 0); 5570 SetBreakPoint(foo, 4); 5571 foo->Call(env->Global(), 0, NULL); 5572 CHECK_EQ(2, break_point_hit_count); 5573 } 5574 5575 // Remove the debug message handler without clearing breakpoints. Do this 5576 // outside a handle scope. 5577 v8::Debug::SetMessageHandler2(NULL); 5578 CheckDebuggerUnloaded(true); 5579} 5580 5581 5582// Sends continue command to the debugger. 5583static void SendContinueCommand() { 5584 const int kBufferSize = 1000; 5585 uint16_t buffer[kBufferSize]; 5586 const char* command_continue = 5587 "{\"seq\":0," 5588 "\"type\":\"request\"," 5589 "\"command\":\"continue\"}"; 5590 5591 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer)); 5592} 5593 5594 5595// Debugger message handler which counts the number of times it is called. 5596static int message_handler_hit_count = 0; 5597static void MessageHandlerHitCount(const v8::Debug::Message& message) { 5598 message_handler_hit_count++; 5599 5600 static char print_buffer[1000]; 5601 v8::String::Value json(message.GetJSON()); 5602 Utf16ToAscii(*json, json.length(), print_buffer); 5603 if (IsExceptionEventMessage(print_buffer)) { 5604 // Send a continue command for exception events. 5605 SendContinueCommand(); 5606 } 5607} 5608 5609 5610// Test clearing the debug message handler. 5611TEST(DebuggerClearMessageHandler) { 5612 v8::HandleScope scope; 5613 DebugLocalContext env; 5614 5615 // Check debugger is unloaded before it is used. 5616 CheckDebuggerUnloaded(); 5617 5618 // Set a debug message handler. 5619 v8::Debug::SetMessageHandler2(MessageHandlerHitCount); 5620 5621 // Run code to throw a unhandled exception. This should end up in the message 5622 // handler. 5623 CompileRun("throw 1"); 5624 5625 // The message handler should be called. 5626 CHECK_GT(message_handler_hit_count, 0); 5627 5628 // Clear debug message handler. 5629 message_handler_hit_count = 0; 5630 v8::Debug::SetMessageHandler(NULL); 5631 5632 // Run code to throw a unhandled exception. This should end up in the message 5633 // handler. 5634 CompileRun("throw 1"); 5635 5636 // The message handler should not be called more. 5637 CHECK_EQ(0, message_handler_hit_count); 5638 5639 CheckDebuggerUnloaded(true); 5640} 5641 5642 5643// Debugger message handler which clears the message handler while active. 5644static void MessageHandlerClearingMessageHandler( 5645 const v8::Debug::Message& message) { 5646 message_handler_hit_count++; 5647 5648 // Clear debug message handler. 5649 v8::Debug::SetMessageHandler(NULL); 5650} 5651 5652 5653// Test clearing the debug message handler while processing a debug event. 5654TEST(DebuggerClearMessageHandlerWhileActive) { 5655 v8::HandleScope scope; 5656 DebugLocalContext env; 5657 5658 // Check debugger is unloaded before it is used. 5659 CheckDebuggerUnloaded(); 5660 5661 // Set a debug message handler. 5662 v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler); 5663 5664 // Run code to throw a unhandled exception. This should end up in the message 5665 // handler. 5666 CompileRun("throw 1"); 5667 5668 // The message handler should be called. 5669 CHECK_EQ(1, message_handler_hit_count); 5670 5671 CheckDebuggerUnloaded(true); 5672} 5673 5674 5675/* Test DebuggerHostDispatch */ 5676/* In this test, the debugger waits for a command on a breakpoint 5677 * and is dispatching host commands while in the infinite loop. 5678 */ 5679 5680class HostDispatchV8Thread : public v8::internal::Thread { 5681 public: 5682 HostDispatchV8Thread() : Thread("HostDispatchV8Thread") { } 5683 void Run(); 5684}; 5685 5686class HostDispatchDebuggerThread : public v8::internal::Thread { 5687 public: 5688 HostDispatchDebuggerThread() : Thread("HostDispatchDebuggerThread") { } 5689 void Run(); 5690}; 5691 5692Barriers* host_dispatch_barriers; 5693 5694static void HostDispatchMessageHandler(const v8::Debug::Message& message) { 5695 static char print_buffer[1000]; 5696 v8::String::Value json(message.GetJSON()); 5697 Utf16ToAscii(*json, json.length(), print_buffer); 5698} 5699 5700 5701static void HostDispatchDispatchHandler() { 5702 host_dispatch_barriers->semaphore_1->Signal(); 5703} 5704 5705 5706void HostDispatchV8Thread::Run() { 5707 const char* source_1 = "var y_global = 3;\n" 5708 "function cat( new_value ) {\n" 5709 " var x = new_value;\n" 5710 " y_global = 4;\n" 5711 " x = 3 * x + 1;\n" 5712 " y_global = 5;\n" 5713 " return x;\n" 5714 "}\n" 5715 "\n"; 5716 const char* source_2 = "cat(17);\n"; 5717 5718 v8::V8::Initialize(); 5719 v8::HandleScope scope; 5720 DebugLocalContext env; 5721 5722 // Setup message and host dispatch handlers. 5723 v8::Debug::SetMessageHandler2(HostDispatchMessageHandler); 5724 v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */); 5725 5726 CompileRun(source_1); 5727 host_dispatch_barriers->barrier_1.Wait(); 5728 host_dispatch_barriers->barrier_2.Wait(); 5729 CompileRun(source_2); 5730} 5731 5732 5733void HostDispatchDebuggerThread::Run() { 5734 const int kBufSize = 1000; 5735 uint16_t buffer[kBufSize]; 5736 5737 const char* command_1 = "{\"seq\":101," 5738 "\"type\":\"request\"," 5739 "\"command\":\"setbreakpoint\"," 5740 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}"; 5741 const char* command_2 = "{\"seq\":102," 5742 "\"type\":\"request\"," 5743 "\"command\":\"continue\"}"; 5744 5745 // v8 thread initializes, runs source_1 5746 host_dispatch_barriers->barrier_1.Wait(); 5747 // 1: Set breakpoint in cat(). 5748 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer)); 5749 5750 host_dispatch_barriers->barrier_2.Wait(); 5751 // v8 thread starts compiling source_2. 5752 // Break happens, to run queued commands and host dispatches. 5753 // Wait for host dispatch to be processed. 5754 host_dispatch_barriers->semaphore_1->Wait(); 5755 // 2: Continue evaluation 5756 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer)); 5757} 5758 5759 5760TEST(DebuggerHostDispatch) { 5761 HostDispatchDebuggerThread host_dispatch_debugger_thread; 5762 HostDispatchV8Thread host_dispatch_v8_thread; 5763 i::FLAG_debugger_auto_break = true; 5764 5765 // Create a V8 environment 5766 Barriers stack_allocated_host_dispatch_barriers; 5767 stack_allocated_host_dispatch_barriers.Initialize(); 5768 host_dispatch_barriers = &stack_allocated_host_dispatch_barriers; 5769 5770 host_dispatch_v8_thread.Start(); 5771 host_dispatch_debugger_thread.Start(); 5772 5773 host_dispatch_v8_thread.Join(); 5774 host_dispatch_debugger_thread.Join(); 5775} 5776 5777 5778/* Test DebugMessageDispatch */ 5779/* In this test, the V8 thread waits for a message from the debug thread. 5780 * The DebugMessageDispatchHandler is executed from the debugger thread 5781 * which signals the V8 thread to wake up. 5782 */ 5783 5784class DebugMessageDispatchV8Thread : public v8::internal::Thread { 5785 public: 5786 DebugMessageDispatchV8Thread() : Thread("DebugMessageDispatchV8Thread") { } 5787 void Run(); 5788}; 5789 5790class DebugMessageDispatchDebuggerThread : public v8::internal::Thread { 5791 public: 5792 DebugMessageDispatchDebuggerThread() 5793 : Thread("DebugMessageDispatchDebuggerThread") { } 5794 void Run(); 5795}; 5796 5797Barriers* debug_message_dispatch_barriers; 5798 5799 5800static void DebugMessageHandler() { 5801 debug_message_dispatch_barriers->semaphore_1->Signal(); 5802} 5803 5804 5805void DebugMessageDispatchV8Thread::Run() { 5806 v8::V8::Initialize(); 5807 v8::HandleScope scope; 5808 DebugLocalContext env; 5809 5810 // Setup debug message dispatch handler. 5811 v8::Debug::SetDebugMessageDispatchHandler(DebugMessageHandler); 5812 5813 CompileRun("var y = 1 + 2;\n"); 5814 debug_message_dispatch_barriers->barrier_1.Wait(); 5815 debug_message_dispatch_barriers->semaphore_1->Wait(); 5816 debug_message_dispatch_barriers->barrier_2.Wait(); 5817} 5818 5819 5820void DebugMessageDispatchDebuggerThread::Run() { 5821 debug_message_dispatch_barriers->barrier_1.Wait(); 5822 SendContinueCommand(); 5823 debug_message_dispatch_barriers->barrier_2.Wait(); 5824} 5825 5826 5827TEST(DebuggerDebugMessageDispatch) { 5828 DebugMessageDispatchDebuggerThread debug_message_dispatch_debugger_thread; 5829 DebugMessageDispatchV8Thread debug_message_dispatch_v8_thread; 5830 5831 i::FLAG_debugger_auto_break = true; 5832 5833 // Create a V8 environment 5834 Barriers stack_allocated_debug_message_dispatch_barriers; 5835 stack_allocated_debug_message_dispatch_barriers.Initialize(); 5836 debug_message_dispatch_barriers = 5837 &stack_allocated_debug_message_dispatch_barriers; 5838 5839 debug_message_dispatch_v8_thread.Start(); 5840 debug_message_dispatch_debugger_thread.Start(); 5841 5842 debug_message_dispatch_v8_thread.Join(); 5843 debug_message_dispatch_debugger_thread.Join(); 5844} 5845 5846 5847TEST(DebuggerAgent) { 5848 v8::V8::Initialize(); 5849 i::Debugger* debugger = i::Isolate::Current()->debugger(); 5850 // Make sure these ports is not used by other tests to allow tests to run in 5851 // parallel. 5852 const int kPort1 = 5858; 5853 const int kPort2 = 5857; 5854 const int kPort3 = 5856; 5855 5856 // Make a string with the port2 number. 5857 const int kPortBufferLen = 6; 5858 char port2_str[kPortBufferLen]; 5859 OS::SNPrintF(i::Vector<char>(port2_str, kPortBufferLen), "%d", kPort2); 5860 5861 bool ok; 5862 5863 // Initialize the socket library. 5864 i::Socket::Setup(); 5865 5866 // Test starting and stopping the agent without any client connection. 5867 debugger->StartAgent("test", kPort1); 5868 debugger->StopAgent(); 5869 // Test starting the agent, connecting a client and shutting down the agent 5870 // with the client connected. 5871 ok = debugger->StartAgent("test", kPort2); 5872 CHECK(ok); 5873 debugger->WaitForAgent(); 5874 i::Socket* client = i::OS::CreateSocket(); 5875 ok = client->Connect("localhost", port2_str); 5876 CHECK(ok); 5877 // It is important to wait for a message from the agent. Otherwise, 5878 // we can close the server socket during "accept" syscall, making it failing 5879 // (at least on Linux), and the test will work incorrectly. 5880 char buf; 5881 ok = client->Receive(&buf, 1) == 1; 5882 CHECK(ok); 5883 debugger->StopAgent(); 5884 delete client; 5885 5886 // Test starting and stopping the agent with the required port already 5887 // occoupied. 5888 i::Socket* server = i::OS::CreateSocket(); 5889 server->Bind(kPort3); 5890 5891 debugger->StartAgent("test", kPort3); 5892 debugger->StopAgent(); 5893 5894 delete server; 5895} 5896 5897 5898class DebuggerAgentProtocolServerThread : public i::Thread { 5899 public: 5900 explicit DebuggerAgentProtocolServerThread(int port) 5901 : Thread("DebuggerAgentProtocolServerThread"), 5902 port_(port), 5903 server_(NULL), 5904 client_(NULL), 5905 listening_(OS::CreateSemaphore(0)) { 5906 } 5907 ~DebuggerAgentProtocolServerThread() { 5908 // Close both sockets. 5909 delete client_; 5910 delete server_; 5911 delete listening_; 5912 } 5913 5914 void Run(); 5915 void WaitForListening() { listening_->Wait(); } 5916 char* body() { return *body_; } 5917 5918 private: 5919 int port_; 5920 i::SmartArrayPointer<char> body_; 5921 i::Socket* server_; // Server socket used for bind/accept. 5922 i::Socket* client_; // Single client connection used by the test. 5923 i::Semaphore* listening_; // Signalled when the server is in listen mode. 5924}; 5925 5926 5927void DebuggerAgentProtocolServerThread::Run() { 5928 bool ok; 5929 5930 // Create the server socket and bind it to the requested port. 5931 server_ = i::OS::CreateSocket(); 5932 CHECK(server_ != NULL); 5933 ok = server_->Bind(port_); 5934 CHECK(ok); 5935 5936 // Listen for new connections. 5937 ok = server_->Listen(1); 5938 CHECK(ok); 5939 listening_->Signal(); 5940 5941 // Accept a connection. 5942 client_ = server_->Accept(); 5943 CHECK(client_ != NULL); 5944 5945 // Receive a debugger agent protocol message. 5946 i::DebuggerAgentUtil::ReceiveMessage(client_); 5947} 5948 5949 5950TEST(DebuggerAgentProtocolOverflowHeader) { 5951 // Make sure this port is not used by other tests to allow tests to run in 5952 // parallel. 5953 const int kPort = 5860; 5954 static const char* kLocalhost = "localhost"; 5955 5956 // Make a string with the port number. 5957 const int kPortBufferLen = 6; 5958 char port_str[kPortBufferLen]; 5959 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort); 5960 5961 // Initialize the socket library. 5962 i::Socket::Setup(); 5963 5964 // Create a socket server to receive a debugger agent message. 5965 DebuggerAgentProtocolServerThread* server = 5966 new DebuggerAgentProtocolServerThread(kPort); 5967 server->Start(); 5968 server->WaitForListening(); 5969 5970 // Connect. 5971 i::Socket* client = i::OS::CreateSocket(); 5972 CHECK(client != NULL); 5973 bool ok = client->Connect(kLocalhost, port_str); 5974 CHECK(ok); 5975 5976 // Send headers which overflow the receive buffer. 5977 static const int kBufferSize = 1000; 5978 char buffer[kBufferSize]; 5979 5980 // Long key and short value: XXXX....XXXX:0\r\n. 5981 for (int i = 0; i < kBufferSize - 4; i++) { 5982 buffer[i] = 'X'; 5983 } 5984 buffer[kBufferSize - 4] = ':'; 5985 buffer[kBufferSize - 3] = '0'; 5986 buffer[kBufferSize - 2] = '\r'; 5987 buffer[kBufferSize - 1] = '\n'; 5988 client->Send(buffer, kBufferSize); 5989 5990 // Short key and long value: X:XXXX....XXXX\r\n. 5991 buffer[0] = 'X'; 5992 buffer[1] = ':'; 5993 for (int i = 2; i < kBufferSize - 2; i++) { 5994 buffer[i] = 'X'; 5995 } 5996 buffer[kBufferSize - 2] = '\r'; 5997 buffer[kBufferSize - 1] = '\n'; 5998 client->Send(buffer, kBufferSize); 5999 6000 // Add empty body to request. 6001 const char* content_length_zero_header = "Content-Length:0\r\n"; 6002 client->Send(content_length_zero_header, 6003 StrLength(content_length_zero_header)); 6004 client->Send("\r\n", 2); 6005 6006 // Wait until data is received. 6007 server->Join(); 6008 6009 // Check for empty body. 6010 CHECK(server->body() == NULL); 6011 6012 // Close the client before the server to avoid TIME_WAIT issues. 6013 client->Shutdown(); 6014 delete client; 6015 delete server; 6016} 6017 6018 6019// Test for issue http://code.google.com/p/v8/issues/detail?id=289. 6020// Make sure that DebugGetLoadedScripts doesn't return scripts 6021// with disposed external source. 6022class EmptyExternalStringResource : public v8::String::ExternalStringResource { 6023 public: 6024 EmptyExternalStringResource() { empty_[0] = 0; } 6025 virtual ~EmptyExternalStringResource() {} 6026 virtual size_t length() const { return empty_.length(); } 6027 virtual const uint16_t* data() const { return empty_.start(); } 6028 private: 6029 ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; 6030}; 6031 6032 6033TEST(DebugGetLoadedScripts) { 6034 v8::HandleScope scope; 6035 DebugLocalContext env; 6036 env.ExposeDebug(); 6037 6038 EmptyExternalStringResource source_ext_str; 6039 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str); 6040 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source); 6041 Handle<i::ExternalTwoByteString> i_source( 6042 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); 6043 // This situation can happen if source was an external string disposed 6044 // by its owner. 6045 i_source->set_resource(0); 6046 6047 bool allow_natives_syntax = i::FLAG_allow_natives_syntax; 6048 i::FLAG_allow_natives_syntax = true; 6049 CompileRun( 6050 "var scripts = %DebugGetLoadedScripts();" 6051 "var count = scripts.length;" 6052 "for (var i = 0; i < count; ++i) {" 6053 " scripts[i].line_ends;" 6054 "}"); 6055 // Must not crash while accessing line_ends. 6056 i::FLAG_allow_natives_syntax = allow_natives_syntax; 6057 6058 // Some scripts are retrieved - at least the number of native scripts. 6059 CHECK_GT((*env)->Global()->Get(v8::String::New("count"))->Int32Value(), 8); 6060} 6061 6062 6063// Test script break points set on lines. 6064TEST(ScriptNameAndData) { 6065 v8::HandleScope scope; 6066 DebugLocalContext env; 6067 env.ExposeDebug(); 6068 6069 // Create functions for retrieving script name and data for the function on 6070 // the top frame when hitting a break point. 6071 frame_script_name = CompileFunction(&env, 6072 frame_script_name_source, 6073 "frame_script_name"); 6074 frame_script_data = CompileFunction(&env, 6075 frame_script_data_source, 6076 "frame_script_data"); 6077 compiled_script_data = CompileFunction(&env, 6078 compiled_script_data_source, 6079 "compiled_script_data"); 6080 6081 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount, 6082 v8::Undefined()); 6083 6084 // Test function source. 6085 v8::Local<v8::String> script = v8::String::New( 6086 "function f() {\n" 6087 " debugger;\n" 6088 "}\n"); 6089 6090 v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name")); 6091 v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1); 6092 script1->SetData(v8::String::New("data")); 6093 script1->Run(); 6094 v8::Local<v8::Function> f; 6095 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6096 6097 f->Call(env->Global(), 0, NULL); 6098 CHECK_EQ(1, break_point_hit_count); 6099 CHECK_EQ("name", last_script_name_hit); 6100 CHECK_EQ("data", last_script_data_hit); 6101 6102 // Compile the same script again without setting data. As the compilation 6103 // cache is disabled when debugging expect the data to be missing. 6104 v8::Script::Compile(script, &origin1)->Run(); 6105 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6106 f->Call(env->Global(), 0, NULL); 6107 CHECK_EQ(2, break_point_hit_count); 6108 CHECK_EQ("name", last_script_name_hit); 6109 CHECK_EQ("", last_script_data_hit); // Undefined results in empty string. 6110 6111 v8::Local<v8::String> data_obj_source = v8::String::New( 6112 "({ a: 'abc',\n" 6113 " b: 123,\n" 6114 " toString: function() { return this.a + ' ' + this.b; }\n" 6115 "})\n"); 6116 v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run(); 6117 v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name")); 6118 v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2); 6119 script2->Run(); 6120 script2->SetData(data_obj->ToString()); 6121 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6122 f->Call(env->Global(), 0, NULL); 6123 CHECK_EQ(3, break_point_hit_count); 6124 CHECK_EQ("new name", last_script_name_hit); 6125 CHECK_EQ("abc 123", last_script_data_hit); 6126 6127 v8::Handle<v8::Script> script3 = 6128 v8::Script::Compile(script, &origin2, NULL, 6129 v8::String::New("in compile")); 6130 CHECK_EQ("in compile", last_script_data_hit); 6131 script3->Run(); 6132 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6133 f->Call(env->Global(), 0, NULL); 6134 CHECK_EQ(4, break_point_hit_count); 6135 CHECK_EQ("in compile", last_script_data_hit); 6136} 6137 6138 6139static v8::Persistent<v8::Context> expected_context; 6140static v8::Handle<v8::Value> expected_context_data; 6141 6142 6143// Check that the expected context is the one generating the debug event. 6144static void ContextCheckMessageHandler(const v8::Debug::Message& message) { 6145 CHECK(message.GetEventContext() == expected_context); 6146 CHECK(message.GetEventContext()->GetData()->StrictEquals( 6147 expected_context_data)); 6148 message_handler_hit_count++; 6149 6150 static char print_buffer[1000]; 6151 v8::String::Value json(message.GetJSON()); 6152 Utf16ToAscii(*json, json.length(), print_buffer); 6153 6154 // Send a continue command for break events. 6155 if (IsBreakEventMessage(print_buffer)) { 6156 SendContinueCommand(); 6157 } 6158} 6159 6160 6161// Test which creates two contexts and sets different embedder data on each. 6162// Checks that this data is set correctly and that when the debug message 6163// handler is called the expected context is the one active. 6164TEST(ContextData) { 6165 v8::HandleScope scope; 6166 6167 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); 6168 6169 // Create two contexts. 6170 v8::Persistent<v8::Context> context_1; 6171 v8::Persistent<v8::Context> context_2; 6172 v8::Handle<v8::ObjectTemplate> global_template = 6173 v8::Handle<v8::ObjectTemplate>(); 6174 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); 6175 context_1 = v8::Context::New(NULL, global_template, global_object); 6176 context_2 = v8::Context::New(NULL, global_template, global_object); 6177 6178 // Default data value is undefined. 6179 CHECK(context_1->GetData()->IsUndefined()); 6180 CHECK(context_2->GetData()->IsUndefined()); 6181 6182 // Set and check different data values. 6183 v8::Handle<v8::String> data_1 = v8::String::New("1"); 6184 v8::Handle<v8::String> data_2 = v8::String::New("2"); 6185 context_1->SetData(data_1); 6186 context_2->SetData(data_2); 6187 CHECK(context_1->GetData()->StrictEquals(data_1)); 6188 CHECK(context_2->GetData()->StrictEquals(data_2)); 6189 6190 // Simple test function which causes a break. 6191 const char* source = "function f() { debugger; }"; 6192 6193 // Enter and run function in the first context. 6194 { 6195 v8::Context::Scope context_scope(context_1); 6196 expected_context = context_1; 6197 expected_context_data = data_1; 6198 v8::Local<v8::Function> f = CompileFunction(source, "f"); 6199 f->Call(context_1->Global(), 0, NULL); 6200 } 6201 6202 6203 // Enter and run function in the second context. 6204 { 6205 v8::Context::Scope context_scope(context_2); 6206 expected_context = context_2; 6207 expected_context_data = data_2; 6208 v8::Local<v8::Function> f = CompileFunction(source, "f"); 6209 f->Call(context_2->Global(), 0, NULL); 6210 } 6211 6212 // Two times compile event and two times break event. 6213 CHECK_GT(message_handler_hit_count, 4); 6214 6215 v8::Debug::SetMessageHandler2(NULL); 6216 CheckDebuggerUnloaded(); 6217} 6218 6219 6220// Debug message handler which issues a debug break when it hits a break event. 6221static int message_handler_break_hit_count = 0; 6222static void DebugBreakMessageHandler(const v8::Debug::Message& message) { 6223 // Schedule a debug break for break events. 6224 if (message.IsEvent() && message.GetEvent() == v8::Break) { 6225 message_handler_break_hit_count++; 6226 if (message_handler_break_hit_count == 1) { 6227 v8::Debug::DebugBreak(); 6228 } 6229 } 6230 6231 // Issue a continue command if this event will not cause the VM to start 6232 // running. 6233 if (!message.WillStartRunning()) { 6234 SendContinueCommand(); 6235 } 6236} 6237 6238 6239// Test that a debug break can be scheduled while in a message handler. 6240TEST(DebugBreakInMessageHandler) { 6241 v8::HandleScope scope; 6242 DebugLocalContext env; 6243 6244 v8::Debug::SetMessageHandler2(DebugBreakMessageHandler); 6245 6246 // Test functions. 6247 const char* script = "function f() { debugger; g(); } function g() { }"; 6248 CompileRun(script); 6249 v8::Local<v8::Function> f = 6250 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6251 v8::Local<v8::Function> g = 6252 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); 6253 6254 // Call f then g. The debugger statement in f will casue a break which will 6255 // cause another break. 6256 f->Call(env->Global(), 0, NULL); 6257 CHECK_EQ(2, message_handler_break_hit_count); 6258 // Calling g will not cause any additional breaks. 6259 g->Call(env->Global(), 0, NULL); 6260 CHECK_EQ(2, message_handler_break_hit_count); 6261} 6262 6263 6264#ifndef V8_INTERPRETED_REGEXP 6265// Debug event handler which gets the function on the top frame and schedules a 6266// break a number of times. 6267static void DebugEventDebugBreak( 6268 v8::DebugEvent event, 6269 v8::Handle<v8::Object> exec_state, 6270 v8::Handle<v8::Object> event_data, 6271 v8::Handle<v8::Value> data) { 6272 6273 if (event == v8::Break) { 6274 break_point_hit_count++; 6275 6276 // Get the name of the top frame function. 6277 if (!frame_function_name.IsEmpty()) { 6278 // Get the name of the function. 6279 const int argc = 2; 6280 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; 6281 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state, 6282 argc, argv); 6283 if (result->IsUndefined()) { 6284 last_function_hit[0] = '\0'; 6285 } else { 6286 CHECK(result->IsString()); 6287 v8::Handle<v8::String> function_name(result->ToString()); 6288 function_name->WriteAscii(last_function_hit); 6289 } 6290 } 6291 6292 // Keep forcing breaks. 6293 if (break_point_hit_count < 20) { 6294 v8::Debug::DebugBreak(); 6295 } 6296 } 6297} 6298 6299 6300TEST(RegExpDebugBreak) { 6301 // This test only applies to native regexps. 6302 v8::HandleScope scope; 6303 DebugLocalContext env; 6304 6305 // Create a function for checking the function when hitting a break point. 6306 frame_function_name = CompileFunction(&env, 6307 frame_function_name_source, 6308 "frame_function_name"); 6309 6310 // Test RegExp which matches white spaces and comments at the begining of a 6311 // source line. 6312 const char* script = 6313 "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" 6314 "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; 6315 6316 v8::Local<v8::Function> f = CompileFunction(script, "f"); 6317 const int argc = 1; 6318 v8::Handle<v8::Value> argv[argc] = { v8::String::New(" /* xxx */ a=0;") }; 6319 v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv); 6320 CHECK_EQ(12, result->Int32Value()); 6321 6322 v8::Debug::SetDebugEventListener(DebugEventDebugBreak); 6323 v8::Debug::DebugBreak(); 6324 result = f->Call(env->Global(), argc, argv); 6325 6326 // Check that there was only one break event. Matching RegExp should not 6327 // cause Break events. 6328 CHECK_EQ(1, break_point_hit_count); 6329 CHECK_EQ("f", last_function_hit); 6330} 6331#endif // V8_INTERPRETED_REGEXP 6332 6333 6334// Common part of EvalContextData and NestedBreakEventContextData tests. 6335static void ExecuteScriptForContextCheck() { 6336 // Create a context. 6337 v8::Persistent<v8::Context> context_1; 6338 v8::Handle<v8::ObjectTemplate> global_template = 6339 v8::Handle<v8::ObjectTemplate>(); 6340 context_1 = v8::Context::New(NULL, global_template); 6341 6342 // Default data value is undefined. 6343 CHECK(context_1->GetData()->IsUndefined()); 6344 6345 // Set and check a data value. 6346 v8::Handle<v8::String> data_1 = v8::String::New("1"); 6347 context_1->SetData(data_1); 6348 CHECK(context_1->GetData()->StrictEquals(data_1)); 6349 6350 // Simple test function with eval that causes a break. 6351 const char* source = "function f() { eval('debugger;'); }"; 6352 6353 // Enter and run function in the context. 6354 { 6355 v8::Context::Scope context_scope(context_1); 6356 expected_context = context_1; 6357 expected_context_data = data_1; 6358 v8::Local<v8::Function> f = CompileFunction(source, "f"); 6359 f->Call(context_1->Global(), 0, NULL); 6360 } 6361} 6362 6363 6364// Test which creates a context and sets embedder data on it. Checks that this 6365// data is set correctly and that when the debug message handler is called for 6366// break event in an eval statement the expected context is the one returned by 6367// Message.GetEventContext. 6368TEST(EvalContextData) { 6369 v8::HandleScope scope; 6370 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); 6371 6372 ExecuteScriptForContextCheck(); 6373 6374 // One time compile event and one time break event. 6375 CHECK_GT(message_handler_hit_count, 2); 6376 v8::Debug::SetMessageHandler2(NULL); 6377 CheckDebuggerUnloaded(); 6378} 6379 6380 6381static bool sent_eval = false; 6382static int break_count = 0; 6383static int continue_command_send_count = 0; 6384// Check that the expected context is the one generating the debug event 6385// including the case of nested break event. 6386static void DebugEvalContextCheckMessageHandler( 6387 const v8::Debug::Message& message) { 6388 CHECK(message.GetEventContext() == expected_context); 6389 CHECK(message.GetEventContext()->GetData()->StrictEquals( 6390 expected_context_data)); 6391 message_handler_hit_count++; 6392 6393 static char print_buffer[1000]; 6394 v8::String::Value json(message.GetJSON()); 6395 Utf16ToAscii(*json, json.length(), print_buffer); 6396 6397 if (IsBreakEventMessage(print_buffer)) { 6398 break_count++; 6399 if (!sent_eval) { 6400 sent_eval = true; 6401 6402 const int kBufferSize = 1000; 6403 uint16_t buffer[kBufferSize]; 6404 const char* eval_command = 6405 "{\"seq\":0," 6406 "\"type\":\"request\"," 6407 "\"command\":\"evaluate\"," 6408 "\"arguments\":{\"expression\":\"debugger;\"," 6409 "\"global\":true,\"disable_break\":false}}"; 6410 6411 // Send evaluate command. 6412 v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer)); 6413 return; 6414 } else { 6415 // It's a break event caused by the evaluation request above. 6416 SendContinueCommand(); 6417 continue_command_send_count++; 6418 } 6419 } else if (IsEvaluateResponseMessage(print_buffer) && 6420 continue_command_send_count < 2) { 6421 // Response to the evaluation request. We're still on the breakpoint so 6422 // send continue. 6423 SendContinueCommand(); 6424 continue_command_send_count++; 6425 } 6426} 6427 6428 6429// Tests that context returned for break event is correct when the event occurs 6430// in 'evaluate' debugger request. 6431TEST(NestedBreakEventContextData) { 6432 v8::HandleScope scope; 6433 break_count = 0; 6434 message_handler_hit_count = 0; 6435 v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler); 6436 6437 ExecuteScriptForContextCheck(); 6438 6439 // One time compile event and two times break event. 6440 CHECK_GT(message_handler_hit_count, 3); 6441 6442 // One break from the source and another from the evaluate request. 6443 CHECK_EQ(break_count, 2); 6444 v8::Debug::SetMessageHandler2(NULL); 6445 CheckDebuggerUnloaded(); 6446} 6447 6448 6449// Debug event listener which counts the script collected events. 6450int script_collected_count = 0; 6451static void DebugEventScriptCollectedEvent(v8::DebugEvent event, 6452 v8::Handle<v8::Object> exec_state, 6453 v8::Handle<v8::Object> event_data, 6454 v8::Handle<v8::Value> data) { 6455 // Count the number of breaks. 6456 if (event == v8::ScriptCollected) { 6457 script_collected_count++; 6458 } 6459} 6460 6461 6462// Test that scripts collected are reported through the debug event listener. 6463TEST(ScriptCollectedEvent) { 6464 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 6465 break_point_hit_count = 0; 6466 script_collected_count = 0; 6467 v8::HandleScope scope; 6468 DebugLocalContext env; 6469 6470 // Request the loaded scripts to initialize the debugger script cache. 6471 debug->GetLoadedScripts(); 6472 6473 // Do garbage collection to ensure that only the script in this test will be 6474 // collected afterwards. 6475 HEAP->CollectAllGarbage(false); 6476 6477 script_collected_count = 0; 6478 v8::Debug::SetDebugEventListener(DebugEventScriptCollectedEvent, 6479 v8::Undefined()); 6480 { 6481 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); 6482 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); 6483 } 6484 6485 // Do garbage collection to collect the script above which is no longer 6486 // referenced. 6487 HEAP->CollectAllGarbage(false); 6488 6489 CHECK_EQ(2, script_collected_count); 6490 6491 v8::Debug::SetDebugEventListener(NULL); 6492 CheckDebuggerUnloaded(); 6493} 6494 6495 6496// Debug event listener which counts the script collected events. 6497int script_collected_message_count = 0; 6498static void ScriptCollectedMessageHandler(const v8::Debug::Message& message) { 6499 // Count the number of scripts collected. 6500 if (message.IsEvent() && message.GetEvent() == v8::ScriptCollected) { 6501 script_collected_message_count++; 6502 v8::Handle<v8::Context> context = message.GetEventContext(); 6503 CHECK(context.IsEmpty()); 6504 } 6505} 6506 6507 6508// Test that GetEventContext doesn't fail and return empty handle for 6509// ScriptCollected events. 6510TEST(ScriptCollectedEventContext) { 6511 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 6512 script_collected_message_count = 0; 6513 v8::HandleScope scope; 6514 6515 { // Scope for the DebugLocalContext. 6516 DebugLocalContext env; 6517 6518 // Request the loaded scripts to initialize the debugger script cache. 6519 debug->GetLoadedScripts(); 6520 6521 // Do garbage collection to ensure that only the script in this test will be 6522 // collected afterwards. 6523 HEAP->CollectAllGarbage(false); 6524 6525 v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler); 6526 { 6527 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); 6528 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); 6529 } 6530 } 6531 6532 // Do garbage collection to collect the script above which is no longer 6533 // referenced. 6534 HEAP->CollectAllGarbage(false); 6535 6536 CHECK_EQ(2, script_collected_message_count); 6537 6538 v8::Debug::SetMessageHandler2(NULL); 6539} 6540 6541 6542// Debug event listener which counts the after compile events. 6543int after_compile_message_count = 0; 6544static void AfterCompileMessageHandler(const v8::Debug::Message& message) { 6545 // Count the number of scripts collected. 6546 if (message.IsEvent()) { 6547 if (message.GetEvent() == v8::AfterCompile) { 6548 after_compile_message_count++; 6549 } else if (message.GetEvent() == v8::Break) { 6550 SendContinueCommand(); 6551 } 6552 } 6553} 6554 6555 6556// Tests that after compile event is sent as many times as there are scripts 6557// compiled. 6558TEST(AfterCompileMessageWhenMessageHandlerIsReset) { 6559 v8::HandleScope scope; 6560 DebugLocalContext env; 6561 after_compile_message_count = 0; 6562 const char* script = "var a=1"; 6563 6564 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6565 v8::Script::Compile(v8::String::New(script))->Run(); 6566 v8::Debug::SetMessageHandler2(NULL); 6567 6568 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6569 v8::Debug::DebugBreak(); 6570 v8::Script::Compile(v8::String::New(script))->Run(); 6571 6572 // Setting listener to NULL should cause debugger unload. 6573 v8::Debug::SetMessageHandler2(NULL); 6574 CheckDebuggerUnloaded(); 6575 6576 // Compilation cache should be disabled when debugger is active. 6577 CHECK_EQ(2, after_compile_message_count); 6578} 6579 6580 6581// Tests that break event is sent when message handler is reset. 6582TEST(BreakMessageWhenMessageHandlerIsReset) { 6583 v8::HandleScope scope; 6584 DebugLocalContext env; 6585 after_compile_message_count = 0; 6586 const char* script = "function f() {};"; 6587 6588 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6589 v8::Script::Compile(v8::String::New(script))->Run(); 6590 v8::Debug::SetMessageHandler2(NULL); 6591 6592 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6593 v8::Debug::DebugBreak(); 6594 v8::Local<v8::Function> f = 6595 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6596 f->Call(env->Global(), 0, NULL); 6597 6598 // Setting message handler to NULL should cause debugger unload. 6599 v8::Debug::SetMessageHandler2(NULL); 6600 CheckDebuggerUnloaded(); 6601 6602 // Compilation cache should be disabled when debugger is active. 6603 CHECK_EQ(1, after_compile_message_count); 6604} 6605 6606 6607static int exception_event_count = 0; 6608static void ExceptionMessageHandler(const v8::Debug::Message& message) { 6609 if (message.IsEvent() && message.GetEvent() == v8::Exception) { 6610 exception_event_count++; 6611 SendContinueCommand(); 6612 } 6613} 6614 6615 6616// Tests that exception event is sent when message handler is reset. 6617TEST(ExceptionMessageWhenMessageHandlerIsReset) { 6618 v8::HandleScope scope; 6619 DebugLocalContext env; 6620 6621 // For this test, we want to break on uncaught exceptions: 6622 ChangeBreakOnException(false, true); 6623 6624 exception_event_count = 0; 6625 const char* script = "function f() {throw new Error()};"; 6626 6627 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6628 v8::Script::Compile(v8::String::New(script))->Run(); 6629 v8::Debug::SetMessageHandler2(NULL); 6630 6631 v8::Debug::SetMessageHandler2(ExceptionMessageHandler); 6632 v8::Local<v8::Function> f = 6633 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); 6634 f->Call(env->Global(), 0, NULL); 6635 6636 // Setting message handler to NULL should cause debugger unload. 6637 v8::Debug::SetMessageHandler2(NULL); 6638 CheckDebuggerUnloaded(); 6639 6640 CHECK_EQ(1, exception_event_count); 6641} 6642 6643 6644// Tests after compile event is sent when there are some provisional 6645// breakpoints out of the scripts lines range. 6646TEST(ProvisionalBreakpointOnLineOutOfRange) { 6647 v8::HandleScope scope; 6648 DebugLocalContext env; 6649 env.ExposeDebug(); 6650 const char* script = "function f() {};"; 6651 const char* resource_name = "test_resource"; 6652 6653 // Set a couple of provisional breakpoint on lines out of the script lines 6654 // range. 6655 int sbp1 = SetScriptBreakPointByNameFromJS(resource_name, 3, 6656 -1 /* no column */); 6657 int sbp2 = SetScriptBreakPointByNameFromJS(resource_name, 5, 5); 6658 6659 after_compile_message_count = 0; 6660 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); 6661 6662 v8::ScriptOrigin origin( 6663 v8::String::New(resource_name), 6664 v8::Integer::New(10), 6665 v8::Integer::New(1)); 6666 // Compile a script whose first line number is greater than the breakpoints' 6667 // lines. 6668 v8::Script::Compile(v8::String::New(script), &origin)->Run(); 6669 6670 // If the script is compiled successfully there is exactly one after compile 6671 // event. In case of an exception in debugger code after compile event is not 6672 // sent. 6673 CHECK_EQ(1, after_compile_message_count); 6674 6675 ClearBreakPointFromJS(sbp1); 6676 ClearBreakPointFromJS(sbp2); 6677 v8::Debug::SetMessageHandler2(NULL); 6678} 6679 6680 6681static void BreakMessageHandler(const v8::Debug::Message& message) { 6682 i::Isolate* isolate = i::Isolate::Current(); 6683 if (message.IsEvent() && message.GetEvent() == v8::Break) { 6684 // Count the number of breaks. 6685 break_point_hit_count++; 6686 6687 v8::HandleScope scope; 6688 v8::Handle<v8::String> json = message.GetJSON(); 6689 6690 SendContinueCommand(); 6691 } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) { 6692 v8::HandleScope scope; 6693 6694 bool is_debug_break = isolate->stack_guard()->IsDebugBreak(); 6695 // Force DebugBreak flag while serializer is working. 6696 isolate->stack_guard()->DebugBreak(); 6697 6698 // Force serialization to trigger some internal JS execution. 6699 v8::Handle<v8::String> json = message.GetJSON(); 6700 6701 // Restore previous state. 6702 if (is_debug_break) { 6703 isolate->stack_guard()->DebugBreak(); 6704 } else { 6705 isolate->stack_guard()->Continue(i::DEBUGBREAK); 6706 } 6707 } 6708} 6709 6710 6711// Test that if DebugBreak is forced it is ignored when code from 6712// debug-delay.js is executed. 6713TEST(NoDebugBreakInAfterCompileMessageHandler) { 6714 v8::HandleScope scope; 6715 DebugLocalContext env; 6716 6717 // Register a debug event listener which sets the break flag and counts. 6718 v8::Debug::SetMessageHandler2(BreakMessageHandler); 6719 6720 // Set the debug break flag. 6721 v8::Debug::DebugBreak(); 6722 6723 // Create a function for testing stepping. 6724 const char* src = "function f() { eval('var x = 10;'); } "; 6725 v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); 6726 6727 // There should be only one break event. 6728 CHECK_EQ(1, break_point_hit_count); 6729 6730 // Set the debug break flag again. 6731 v8::Debug::DebugBreak(); 6732 f->Call(env->Global(), 0, NULL); 6733 // There should be one more break event when the script is evaluated in 'f'. 6734 CHECK_EQ(2, break_point_hit_count); 6735 6736 // Get rid of the debug message handler. 6737 v8::Debug::SetMessageHandler2(NULL); 6738 CheckDebuggerUnloaded(); 6739} 6740 6741 6742static int counting_message_handler_counter; 6743 6744static void CountingMessageHandler(const v8::Debug::Message& message) { 6745 counting_message_handler_counter++; 6746} 6747 6748// Test that debug messages get processed when ProcessDebugMessages is called. 6749TEST(ProcessDebugMessages) { 6750 v8::HandleScope scope; 6751 DebugLocalContext env; 6752 6753 counting_message_handler_counter = 0; 6754 6755 v8::Debug::SetMessageHandler2(CountingMessageHandler); 6756 6757 const int kBufferSize = 1000; 6758 uint16_t buffer[kBufferSize]; 6759 const char* scripts_command = 6760 "{\"seq\":0," 6761 "\"type\":\"request\"," 6762 "\"command\":\"scripts\"}"; 6763 6764 // Send scripts command. 6765 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); 6766 6767 CHECK_EQ(0, counting_message_handler_counter); 6768 v8::Debug::ProcessDebugMessages(); 6769 // At least one message should come 6770 CHECK_GE(counting_message_handler_counter, 1); 6771 6772 counting_message_handler_counter = 0; 6773 6774 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); 6775 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); 6776 CHECK_EQ(0, counting_message_handler_counter); 6777 v8::Debug::ProcessDebugMessages(); 6778 // At least two messages should come 6779 CHECK_GE(counting_message_handler_counter, 2); 6780 6781 // Get rid of the debug message handler. 6782 v8::Debug::SetMessageHandler2(NULL); 6783 CheckDebuggerUnloaded(); 6784} 6785 6786 6787struct BacktraceData { 6788 static int frame_counter; 6789 static void MessageHandler(const v8::Debug::Message& message) { 6790 char print_buffer[1000]; 6791 v8::String::Value json(message.GetJSON()); 6792 Utf16ToAscii(*json, json.length(), print_buffer, 1000); 6793 6794 if (strstr(print_buffer, "backtrace") == NULL) { 6795 return; 6796 } 6797 frame_counter = GetTotalFramesInt(print_buffer); 6798 } 6799}; 6800 6801int BacktraceData::frame_counter; 6802 6803 6804// Test that debug messages get processed when ProcessDebugMessages is called. 6805TEST(Backtrace) { 6806 v8::HandleScope scope; 6807 DebugLocalContext env; 6808 6809 v8::Debug::SetMessageHandler2(BacktraceData::MessageHandler); 6810 6811 const int kBufferSize = 1000; 6812 uint16_t buffer[kBufferSize]; 6813 const char* scripts_command = 6814 "{\"seq\":0," 6815 "\"type\":\"request\"," 6816 "\"command\":\"backtrace\"}"; 6817 6818 // Check backtrace from ProcessDebugMessages. 6819 BacktraceData::frame_counter = -10; 6820 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); 6821 v8::Debug::ProcessDebugMessages(); 6822 CHECK_EQ(BacktraceData::frame_counter, 0); 6823 6824 v8::Handle<v8::String> void0 = v8::String::New("void(0)"); 6825 v8::Handle<v8::Script> script = v8::Script::Compile(void0, void0); 6826 6827 // Check backtrace from "void(0)" script. 6828 BacktraceData::frame_counter = -10; 6829 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer)); 6830 script->Run(); 6831 CHECK_EQ(BacktraceData::frame_counter, 1); 6832 6833 // Get rid of the debug message handler. 6834 v8::Debug::SetMessageHandler2(NULL); 6835 CheckDebuggerUnloaded(); 6836} 6837 6838 6839TEST(GetMirror) { 6840 v8::HandleScope scope; 6841 DebugLocalContext env; 6842 v8::Handle<v8::Value> obj = v8::Debug::GetMirror(v8::String::New("hodja")); 6843 v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast( 6844 v8::Script::New( 6845 v8::String::New( 6846 "function runTest(mirror) {" 6847 " return mirror.isString() && (mirror.length() == 5);" 6848 "}" 6849 "" 6850 "runTest;"))->Run()); 6851 v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj); 6852 CHECK(result->IsTrue()); 6853} 6854 6855 6856// Test that the debug break flag works with function.apply. 6857TEST(DebugBreakFunctionApply) { 6858 v8::HandleScope scope; 6859 DebugLocalContext env; 6860 6861 // Create a function for testing breaking in apply. 6862 v8::Local<v8::Function> foo = CompileFunction( 6863 &env, 6864 "function baz(x) { }" 6865 "function bar(x) { baz(); }" 6866 "function foo(){ bar.apply(this, [1]); }", 6867 "foo"); 6868 6869 // Register a debug event listener which steps and counts. 6870 v8::Debug::SetDebugEventListener(DebugEventBreakMax); 6871 6872 // Set the debug break flag before calling the code using function.apply. 6873 v8::Debug::DebugBreak(); 6874 6875 // Limit the number of debug breaks. This is a regression test for issue 493 6876 // where this test would enter an infinite loop. 6877 break_point_hit_count = 0; 6878 max_break_point_hit_count = 10000; // 10000 => infinite loop. 6879 foo->Call(env->Global(), 0, NULL); 6880 6881 // When keeping the debug break several break will happen. 6882 CHECK_EQ(3, break_point_hit_count); 6883 6884 v8::Debug::SetDebugEventListener(NULL); 6885 CheckDebuggerUnloaded(); 6886} 6887 6888 6889v8::Handle<v8::Context> debugee_context; 6890v8::Handle<v8::Context> debugger_context; 6891 6892 6893// Property getter that checks that current and calling contexts 6894// are both the debugee contexts. 6895static v8::Handle<v8::Value> NamedGetterWithCallingContextCheck( 6896 v8::Local<v8::String> name, 6897 const v8::AccessorInfo& info) { 6898 CHECK_EQ(0, strcmp(*v8::String::AsciiValue(name), "a")); 6899 v8::Handle<v8::Context> current = v8::Context::GetCurrent(); 6900 CHECK(current == debugee_context); 6901 CHECK(current != debugger_context); 6902 v8::Handle<v8::Context> calling = v8::Context::GetCalling(); 6903 CHECK(calling == debugee_context); 6904 CHECK(calling != debugger_context); 6905 return v8::Int32::New(1); 6906} 6907 6908 6909// Debug event listener that checks if the first argument of a function is 6910// an object with property 'a' == 1. If the property has custom accessor 6911// this handler will eventually invoke it. 6912static void DebugEventGetAtgumentPropertyValue( 6913 v8::DebugEvent event, 6914 v8::Handle<v8::Object> exec_state, 6915 v8::Handle<v8::Object> event_data, 6916 v8::Handle<v8::Value> data) { 6917 if (event == v8::Break) { 6918 break_point_hit_count++; 6919 CHECK(debugger_context == v8::Context::GetCurrent()); 6920 v8::Handle<v8::Function> func(v8::Function::Cast(*CompileRun( 6921 "(function(exec_state) {\n" 6922 " return (exec_state.frame(0).argumentValue(0).property('a').\n" 6923 " value().value() == 1);\n" 6924 "})"))); 6925 const int argc = 1; 6926 v8::Handle<v8::Value> argv[argc] = { exec_state }; 6927 v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv); 6928 CHECK(result->IsTrue()); 6929 } 6930} 6931 6932 6933TEST(CallingContextIsNotDebugContext) { 6934 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug(); 6935 // Create and enter a debugee context. 6936 v8::HandleScope scope; 6937 DebugLocalContext env; 6938 env.ExposeDebug(); 6939 6940 // Save handles to the debugger and debugee contexts to be used in 6941 // NamedGetterWithCallingContextCheck. 6942 debugee_context = v8::Local<v8::Context>(*env); 6943 debugger_context = v8::Utils::ToLocal(debug->debug_context()); 6944 6945 // Create object with 'a' property accessor. 6946 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New(); 6947 named->SetAccessor(v8::String::New("a"), 6948 NamedGetterWithCallingContextCheck); 6949 env->Global()->Set(v8::String::New("obj"), 6950 named->NewInstance()); 6951 6952 // Register the debug event listener 6953 v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue); 6954 6955 // Create a function that invokes debugger. 6956 v8::Local<v8::Function> foo = CompileFunction( 6957 &env, 6958 "function bar(x) { debugger; }" 6959 "function foo(){ bar(obj); }", 6960 "foo"); 6961 6962 break_point_hit_count = 0; 6963 foo->Call(env->Global(), 0, NULL); 6964 CHECK_EQ(1, break_point_hit_count); 6965 6966 v8::Debug::SetDebugEventListener(NULL); 6967 debugee_context = v8::Handle<v8::Context>(); 6968 debugger_context = v8::Handle<v8::Context>(); 6969 CheckDebuggerUnloaded(); 6970} 6971 6972 6973TEST(DebugContextIsPreservedBetweenAccesses) { 6974 v8::HandleScope scope; 6975 v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext(); 6976 v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext(); 6977 CHECK_EQ(*context1, *context2); 6978} 6979 6980 6981static v8::Handle<v8::Value> expected_callback_data; 6982static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { 6983 CHECK(details.GetEventContext() == expected_context); 6984 CHECK_EQ(expected_callback_data, details.GetCallbackData()); 6985} 6986 6987// Check that event details contain context where debug event occured. 6988TEST(DebugEventContext) { 6989 v8::HandleScope scope; 6990 expected_callback_data = v8::Int32::New(2010); 6991 v8::Debug::SetDebugEventListener2(DebugEventContextChecker, 6992 expected_callback_data); 6993 expected_context = v8::Context::New(); 6994 v8::Context::Scope context_scope(expected_context); 6995 v8::Script::Compile(v8::String::New("(function(){debugger;})();"))->Run(); 6996 expected_context.Dispose(); 6997 expected_context.Clear(); 6998 v8::Debug::SetDebugEventListener(NULL); 6999 expected_context_data = v8::Handle<v8::Value>(); 7000 CheckDebuggerUnloaded(); 7001} 7002 7003 7004static void* expected_break_data; 7005static bool was_debug_break_called; 7006static bool was_debug_event_called; 7007static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) { 7008 if (details.GetEvent() == v8::BreakForCommand) { 7009 CHECK_EQ(expected_break_data, details.GetClientData()); 7010 was_debug_event_called = true; 7011 } else if (details.GetEvent() == v8::Break) { 7012 was_debug_break_called = true; 7013 } 7014} 7015 7016 7017// Check that event details contain context where debug event occured. 7018TEST(DebugEventBreakData) { 7019 v8::HandleScope scope; 7020 DebugLocalContext env; 7021 v8::Debug::SetDebugEventListener2(DebugEventBreakDataChecker); 7022 7023 TestClientData::constructor_call_counter = 0; 7024 TestClientData::destructor_call_counter = 0; 7025 7026 expected_break_data = NULL; 7027 was_debug_event_called = false; 7028 was_debug_break_called = false; 7029 v8::Debug::DebugBreakForCommand(); 7030 v8::Script::Compile(v8::String::New("(function(x){return x;})(1);"))->Run(); 7031 CHECK(was_debug_event_called); 7032 CHECK(!was_debug_break_called); 7033 7034 TestClientData* data1 = new TestClientData(); 7035 expected_break_data = data1; 7036 was_debug_event_called = false; 7037 was_debug_break_called = false; 7038 v8::Debug::DebugBreakForCommand(data1); 7039 v8::Script::Compile(v8::String::New("(function(x){return x+1;})(1);"))->Run(); 7040 CHECK(was_debug_event_called); 7041 CHECK(!was_debug_break_called); 7042 7043 expected_break_data = NULL; 7044 was_debug_event_called = false; 7045 was_debug_break_called = false; 7046 v8::Debug::DebugBreak(); 7047 v8::Script::Compile(v8::String::New("(function(x){return x+2;})(1);"))->Run(); 7048 CHECK(!was_debug_event_called); 7049 CHECK(was_debug_break_called); 7050 7051 TestClientData* data2 = new TestClientData(); 7052 expected_break_data = data2; 7053 was_debug_event_called = false; 7054 was_debug_break_called = false; 7055 v8::Debug::DebugBreak(); 7056 v8::Debug::DebugBreakForCommand(data2); 7057 v8::Script::Compile(v8::String::New("(function(x){return x+3;})(1);"))->Run(); 7058 CHECK(was_debug_event_called); 7059 CHECK(was_debug_break_called); 7060 7061 CHECK_EQ(2, TestClientData::constructor_call_counter); 7062 CHECK_EQ(TestClientData::constructor_call_counter, 7063 TestClientData::destructor_call_counter); 7064 7065 v8::Debug::SetDebugEventListener(NULL); 7066 CheckDebuggerUnloaded(); 7067} 7068 7069static bool debug_event_break_deoptimize_done = false; 7070 7071static void DebugEventBreakDeoptimize(v8::DebugEvent event, 7072 v8::Handle<v8::Object> exec_state, 7073 v8::Handle<v8::Object> event_data, 7074 v8::Handle<v8::Value> data) { 7075 if (event == v8::Break) { 7076 if (!frame_function_name.IsEmpty()) { 7077 // Get the name of the function. 7078 const int argc = 2; 7079 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) }; 7080 v8::Handle<v8::Value> result = 7081 frame_function_name->Call(exec_state, argc, argv); 7082 if (!result->IsUndefined()) { 7083 char fn[80]; 7084 CHECK(result->IsString()); 7085 v8::Handle<v8::String> function_name(result->ToString()); 7086 function_name->WriteAscii(fn); 7087 if (strcmp(fn, "bar") == 0) { 7088 i::Deoptimizer::DeoptimizeAll(); 7089 debug_event_break_deoptimize_done = true; 7090 } 7091 } 7092 } 7093 7094 v8::Debug::DebugBreak(); 7095 } 7096} 7097 7098 7099// Test deoptimization when execution is broken using the debug break stack 7100// check interrupt. 7101TEST(DeoptimizeDuringDebugBreak) { 7102 v8::HandleScope scope; 7103 DebugLocalContext env; 7104 env.ExposeDebug(); 7105 7106 // Create a function for checking the function when hitting a break point. 7107 frame_function_name = CompileFunction(&env, 7108 frame_function_name_source, 7109 "frame_function_name"); 7110 7111 7112 // Set a debug event listener which will keep interrupting execution until 7113 // debug break. When inside function bar it will deoptimize all functions. 7114 // This tests lazy deoptimization bailout for the stack check, as the first 7115 // time in function bar when using debug break and no break points will be at 7116 // the initial stack check. 7117 v8::Debug::SetDebugEventListener(DebugEventBreakDeoptimize, 7118 v8::Undefined()); 7119 7120 // Compile and run function bar which will optimize it for some flag settings. 7121 v8::Script::Compile(v8::String::New("function bar(){}; bar()"))->Run(); 7122 7123 // Set debug break and call bar again. 7124 v8::Debug::DebugBreak(); 7125 v8::Script::Compile(v8::String::New("bar()"))->Run(); 7126 7127 CHECK(debug_event_break_deoptimize_done); 7128 7129 v8::Debug::SetDebugEventListener(NULL); 7130} 7131 7132 7133static void DebugEventBreakWithOptimizedStack(v8::DebugEvent event, 7134 v8::Handle<v8::Object> exec_state, 7135 v8::Handle<v8::Object> event_data, 7136 v8::Handle<v8::Value> data) { 7137 if (event == v8::Break) { 7138 if (!frame_function_name.IsEmpty()) { 7139 for (int i = 0; i < 2; i++) { 7140 const int argc = 2; 7141 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(i) }; 7142 // Get the name of the function in frame i. 7143 v8::Handle<v8::Value> result = 7144 frame_function_name->Call(exec_state, argc, argv); 7145 CHECK(result->IsString()); 7146 v8::Handle<v8::String> function_name(result->ToString()); 7147 CHECK(function_name->Equals(v8::String::New("loop"))); 7148 // Get the name of the first argument in frame i. 7149 result = frame_argument_name->Call(exec_state, argc, argv); 7150 CHECK(result->IsString()); 7151 v8::Handle<v8::String> argument_name(result->ToString()); 7152 CHECK(argument_name->Equals(v8::String::New("count"))); 7153 // Get the value of the first argument in frame i. If the 7154 // funtion is optimized the value will be undefined, otherwise 7155 // the value will be '1 - i'. 7156 // 7157 // TODO(3141533): We should be able to get the real value for 7158 // optimized frames. 7159 result = frame_argument_value->Call(exec_state, argc, argv); 7160 CHECK(result->IsUndefined() || (result->Int32Value() == 1 - i)); 7161 // Get the name of the first local variable. 7162 result = frame_local_name->Call(exec_state, argc, argv); 7163 CHECK(result->IsString()); 7164 v8::Handle<v8::String> local_name(result->ToString()); 7165 CHECK(local_name->Equals(v8::String::New("local"))); 7166 // Get the value of the first local variable. If the function 7167 // is optimized the value will be undefined, otherwise it will 7168 // be 42. 7169 // 7170 // TODO(3141533): We should be able to get the real value for 7171 // optimized frames. 7172 result = frame_local_value->Call(exec_state, argc, argv); 7173 CHECK(result->IsUndefined() || (result->Int32Value() == 42)); 7174 } 7175 } 7176 } 7177} 7178 7179 7180static v8::Handle<v8::Value> ScheduleBreak(const v8::Arguments& args) { 7181 v8::Debug::SetDebugEventListener(DebugEventBreakWithOptimizedStack, 7182 v8::Undefined()); 7183 v8::Debug::DebugBreak(); 7184 return v8::Undefined(); 7185} 7186 7187 7188TEST(DebugBreakStackInspection) { 7189 v8::HandleScope scope; 7190 DebugLocalContext env; 7191 7192 frame_function_name = 7193 CompileFunction(&env, frame_function_name_source, "frame_function_name"); 7194 frame_argument_name = 7195 CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); 7196 frame_argument_value = CompileFunction(&env, 7197 frame_argument_value_source, 7198 "frame_argument_value"); 7199 frame_local_name = 7200 CompileFunction(&env, frame_local_name_source, "frame_local_name"); 7201 frame_local_value = 7202 CompileFunction(&env, frame_local_value_source, "frame_local_value"); 7203 7204 v8::Handle<v8::FunctionTemplate> schedule_break_template = 7205 v8::FunctionTemplate::New(ScheduleBreak); 7206 v8::Handle<v8::Function> schedule_break = 7207 schedule_break_template->GetFunction(); 7208 env->Global()->Set(v8_str("scheduleBreak"), schedule_break); 7209 7210 const char* src = 7211 "function loop(count) {" 7212 " var local = 42;" 7213 " if (count < 1) { scheduleBreak(); loop(count + 1); }" 7214 "}" 7215 "loop(0);"; 7216 v8::Script::Compile(v8::String::New(src))->Run(); 7217} 7218 7219 7220// Test that setting the terminate execution flag during debug break processing. 7221static void TestDebugBreakInLoop(const char* loop_head, 7222 const char** loop_bodies, 7223 const char* loop_tail) { 7224 // Receive 100 breaks for each test and then terminate JavaScript execution. 7225 static const int kBreaksPerTest = 100; 7226 7227 for (int i = 0; i < 1 && loop_bodies[i] != NULL; i++) { 7228 // Perform a lazy deoptimization after various numbers of breaks 7229 // have been hit. 7230 for (int j = 0; j < 10; j++) { 7231 break_point_hit_count_deoptimize = j; 7232 if (j == 10) { 7233 break_point_hit_count_deoptimize = kBreaksPerTest; 7234 } 7235 7236 break_point_hit_count = 0; 7237 max_break_point_hit_count = kBreaksPerTest; 7238 terminate_after_max_break_point_hit = true; 7239 7240 EmbeddedVector<char, 1024> buffer; 7241 OS::SNPrintF(buffer, 7242 "function f() {%s%s%s}", 7243 loop_head, loop_bodies[i], loop_tail); 7244 7245 // Function with infinite loop. 7246 CompileRun(buffer.start()); 7247 7248 // Set the debug break to enter the debugger as soon as possible. 7249 v8::Debug::DebugBreak(); 7250 7251 // Call function with infinite loop. 7252 CompileRun("f();"); 7253 CHECK_EQ(kBreaksPerTest, break_point_hit_count); 7254 7255 CHECK(!v8::V8::IsExecutionTerminating()); 7256 } 7257 } 7258} 7259 7260 7261TEST(DebugBreakLoop) { 7262 v8::HandleScope scope; 7263 DebugLocalContext env; 7264 7265 // Register a debug event listener which sets the break flag and counts. 7266 v8::Debug::SetDebugEventListener(DebugEventBreakMax); 7267 7268 // Create a function for getting the frame count when hitting the break. 7269 frame_count = CompileFunction(&env, frame_count_source, "frame_count"); 7270 7271 CompileRun("var a = 1;"); 7272 CompileRun("function g() { }"); 7273 CompileRun("function h() { }"); 7274 7275 const char* loop_bodies[] = { 7276 "", 7277 "g()", 7278 "if (a == 0) { g() }", 7279 "if (a == 1) { g() }", 7280 "if (a == 0) { g() } else { h() }", 7281 "if (a == 0) { continue }", 7282 "if (a == 1) { continue }", 7283 "switch (a) { case 1: g(); }", 7284 "switch (a) { case 1: continue; }", 7285 "switch (a) { case 1: g(); break; default: h() }", 7286 "switch (a) { case 1: continue; break; default: h() }", 7287 NULL 7288 }; 7289 7290 TestDebugBreakInLoop("while (true) {", loop_bodies, "}"); 7291 TestDebugBreakInLoop("while (a == 1) {", loop_bodies, "}"); 7292 7293 TestDebugBreakInLoop("do {", loop_bodies, "} while (true)"); 7294 TestDebugBreakInLoop("do {", loop_bodies, "} while (a == 1)"); 7295 7296 TestDebugBreakInLoop("for (;;) {", loop_bodies, "}"); 7297 TestDebugBreakInLoop("for (;a == 1;) {", loop_bodies, "}"); 7298 7299 // Get rid of the debug event listener. 7300 v8::Debug::SetDebugEventListener(NULL); 7301 CheckDebuggerUnloaded(); 7302} 7303 7304 7305#endif // ENABLE_DEBUGGER_SUPPORT 7306