1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved. 2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without 3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are 4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met: 5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions of source code must retain the above copyright 7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// notice, this list of conditions and the following disclaimer. 8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions in binary form must reproduce the above 9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// copyright notice, this list of conditions and the following 10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// disclaimer in the documentation and/or other materials provided 11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// with the distribution. 12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Neither the name of Google Inc. nor the names of its 13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contributors may be used to endorse or promote products derived 14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// from this software without specific prior written permission. 15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Flags: --expose-debug-as debug 29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Get the Debug object exposed from the debug context global object. 30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug = debug.Debug 31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Simple debug event handler which just counts the number of break points hit. 33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar break_point_hit_count; 34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction listener(event, exec_state, event_data, data) { 36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block if (event == Debug.DebugEvent.Break) { 37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block break_point_hit_count++; 38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}; 40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Add the debug event listener. 42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.setListener(listener); 43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test functions. 45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockcount = 0; 46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction f() {}; 47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction g() {h(count++)}; 487f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdochfunction h(x) {var a=x; return a}; 49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which syntax error. 52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, '{{{'); 54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, break_point_hit_count); 56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which evaluates to false. 59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, 'false'); 61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, break_point_hit_count); 63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which evaluates to true. 66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, 'true'); 68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which different types of quotes. 73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, '"a" == "a"'); 75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, "'a' == 'a'"); 80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Changing condition. 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, '"ab".indexOf("b") > 0'); 87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.changeBreakPointCondition(bp, 'Math.sin(Math.PI/2) > 1'); 90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.changeBreakPointCondition(bp, '1==1'); 93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(2, break_point_hit_count); 95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which checks global variable. 98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(f, 0, 0, 'x==1'); 100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, break_point_hit_count); 102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockx=1; 103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockf(); 104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, break_point_hit_count); 105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which checks global variable. 108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(g, 0, 0, 'count % 2 == 0'); 110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0; i < 10; i++) { 111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block g(); 112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, break_point_hit_count); 114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which checks a parameter. 117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbp = Debug.setBreakPoint(h, 0, 0, 'x % 2 == 0'); 119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0; i < 10; i++) { 120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block g(); 121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, break_point_hit_count); 123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Conditional breakpoint which checks a local variable. 126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 127bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochbp = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0'); 128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0; i < 10; i++) { 129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block g(); 130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, break_point_hit_count); 132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp); 133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Multiple conditional breakpoint which the same condition. 135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 136bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochbp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0'); 137bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochbp2 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0'); 138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0; i < 10; i++) { 139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block g(); 140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, break_point_hit_count); 142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp1); 143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp2); 144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Multiple conditional breakpoint which different conditions. 146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockbreak_point_hit_count = 0; 147bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochbp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0'); 148bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochbp2 = Debug.setBreakPoint(h, 0, 22, '(a + 1) % 2 == 0'); 149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0; i < 10; i++) { 150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block g(); 151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, break_point_hit_count); 153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp1); 154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug.clearBreakPoint(bp2); 155