1// Copyright 2012 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// Flags: --expose-debug-as debug 29// Get the Debug object exposed from the debug context global object. 30 31Debug = debug.Debug 32 33function FindCallFrame(exec_state, frame_code) { 34 var number = Number(frame_code); 35 if (number >= 0) { 36 return exec_state.frame(number); 37 } else { 38 for (var i = 0; i < exec_state.frameCount(); i++) { 39 var frame = exec_state.frame(i); 40 var func_mirror = frame.func(); 41 if (frame_code == func_mirror.name()) { 42 return frame; 43 } 44 } 45 } 46 throw new Error("Failed to find function name " + function_name); 47} 48 49function TestCase(test_scenario, expected_output) { 50 // Global variable, accessed from eval'd script. 51 test_output = ""; 52 53 function TestCode() { 54 function A() { 55 // Extra stack variable. To make function not slim. 56 // Restarter doesn't work on slim function when stopped on 'debugger' 57 // statement. (There is no padding for 'debugger' statement). 58 var o = {}; 59 test_output += 'A'; 60 test_output += '='; 61 debugger; 62 return 'Capybara'; 63 } 64 function B(p1, p2) { 65 test_output += 'B'; 66 return A(); 67 } 68 function C() { 69 test_output += 'C'; 70 // Function call with argument adaptor is intentional. 71 return B(); 72 } 73 function D() { 74 test_output += 'D'; 75 // Function call with argument adaptor is intentional. 76 return C(1, 2); 77 } 78 function E() { 79 test_output += 'E'; 80 return D(); 81 } 82 function F() { 83 test_output += 'F'; 84 return E(); 85 } 86 return F(); 87 } 88 89 var scenario_pos = 0; 90 91 function DebuggerStatementHandler(exec_state) { 92 while (true) { 93 assertTrue(scenario_pos < test_scenario.length); 94 var change_code = test_scenario[scenario_pos++]; 95 if (change_code == '=') { 96 // Continue. 97 return; 98 } 99 var frame = FindCallFrame(exec_state, change_code); 100 var error = frame.restart(); 101 if (typeof error === 'string') 102 throw new Error(error); 103 } 104 } 105 106 var saved_exception = null; 107 108 function listener(event, exec_state, event_data, data) { 109 if (saved_exception != null) { 110 return; 111 } 112 if (event == Debug.DebugEvent.Break) { 113 try { 114 DebuggerStatementHandler(exec_state); 115 } catch (e) { 116 saved_exception = e; 117 } 118 } else { 119 print("Other: " + event); 120 } 121 } 122 123 Debug.setListener(listener); 124 assertEquals("Capybara", TestCode()); 125 Debug.setListener(null); 126 127 if (saved_exception) { 128 print("Exception: " + saved_exception); 129 print("Stack: " + saved_exception.stack); 130 assertUnreachable(); 131 } 132 133 print(test_output); 134 135 assertEquals(expected_output, test_output); 136} 137 138TestCase('0==', "FEDCBA=A="); 139TestCase('1==', "FEDCBA=BA="); 140TestCase('2==', "FEDCBA=CBA="); 141TestCase('3==', "FEDCBA=DCBA="); 142TestCase('4==', "FEDCBA=EDCBA="); 143TestCase('5==', "FEDCBA=FEDCBA="); 144 145TestCase('=', "FEDCBA="); 146 147TestCase('C==', "FEDCBA=CBA="); 148 149TestCase('B=C=A=D==', "FEDCBA=BA=CBA=A=DCBA="); 150 151// Successive restarts don't work now and require additional fix. 152//TestCase('BCDE==', "FEDCBA=EDCBA="); 153//TestCase('BC=BCDE==', "FEDCBA=CBA=EDCBA="); 154//TestCase('EF==', "FEDCBA=FEDCBA="); 155