1// Copyright 2010 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. 30Debug = debug.Debug 31 32 33function ParsedResponse(json) { 34 this.response_ = eval('(' + json + ')'); 35 this.refs_ = []; 36 if (this.response_.refs) { 37 for (var i = 0; i < this.response_.refs.length; i++) { 38 this.refs_[this.response_.refs[i].handle] = this.response_.refs[i]; 39 } 40 } 41} 42 43 44ParsedResponse.prototype.response = function() { 45 return this.response_; 46} 47 48 49ParsedResponse.prototype.body = function() { 50 return this.response_.body; 51} 52 53 54ParsedResponse.prototype.running = function() { 55 return this.response_.running; 56} 57 58 59ParsedResponse.prototype.lookup = function(handle) { 60 return this.refs_[handle]; 61} 62 63 64listener_complete = false; 65exception = false; 66break_count = 0; 67expected_return_value = 0; 68debugger_source_position = 0; 69 70// Listener which expects to do four steps to reach returning from the function. 71function listener(event, exec_state, event_data, data) { 72 try { 73 if (event == Debug.DebugEvent.Break) 74 { 75 break_count++; 76 if (break_count < 4) { 77 assertFalse(exec_state.frame(0).isAtReturn()) 78 switch (break_count) { 79 case 1: 80 // Collect the position of the debugger statement. 81 debugger_source_position = exec_state.frame(0).sourcePosition(); 82 break; 83 case 2: 84 // Position now at the if statement. 85 assertEquals(debugger_source_position + 10, 86 exec_state.frame(0).sourcePosition()); 87 break; 88 case 3: 89 // Position now at either of the returns. 90 if (expected_return_value == 1) { 91 assertEquals(debugger_source_position + 19, 92 exec_state.frame(0).sourcePosition()); 93 } else { 94 assertEquals(debugger_source_position + 38, 95 exec_state.frame(0).sourcePosition()); 96 } 97 break; 98 default: 99 fail("Unexpected"); 100 } 101 exec_state.prepareStep(Debug.StepAction.StepIn, 1); 102 } else { 103 // Position at the end of the function. 104 assertEquals(debugger_source_position + 50, 105 exec_state.frame(0).sourcePosition()); 106 107 // Just about to return from the function. 108 assertTrue(exec_state.frame(0).isAtReturn()) 109 assertEquals(expected_return_value, 110 exec_state.frame(0).returnValue().value()); 111 112 // Check the same using the JSON commands. 113 var dcp = exec_state.debugCommandProcessor(false); 114 var request = '{"seq":0,"type":"request","command":"backtrace"}'; 115 var resp = dcp.processDebugJSONRequest(request); 116 response = new ParsedResponse(resp); 117 frames = response.body().frames; 118 assertTrue(frames[0].atReturn); 119 assertEquals(expected_return_value, 120 response.lookup(frames[0].returnValue.ref).value); 121 122 listener_complete = true; 123 } 124 } 125 } catch (e) { 126 exception = e 127 }; 128}; 129 130// Add the debug event listener. 131Debug.setListener(listener); 132 133// Four steps from the debugger statement in this function will position us at 134// the function return. 135// 0 1 2 3 4 5 136// 0123456789012345678901234567890123456789012345678901 137 138function f(x) {debugger; if (x) { return 1; } else { return 2; } }; 139 140// Call f expecting different return values. 141break_count = 0; 142expected_return_value = 2; 143listener_complete = false; 144f(); 145assertFalse(exception, "exception in listener") 146assertTrue(listener_complete); 147assertEquals(4, break_count); 148 149break_count = 0; 150expected_return_value = 1; 151listener_complete = false; 152f(true); 153assertFalse(exception, "exception in listener") 154assertTrue(listener_complete); 155assertEquals(4, break_count); 156 157break_count = 0; 158expected_return_value = 2; 159listener_complete = false; 160f(false); 161assertFalse(exception, "exception in listener") 162assertTrue(listener_complete); 163assertEquals(4, break_count); 164