1// Copyright 2008 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. 30var Debug = debug.Debug 31 32// Simple function which stores the last debug event. 33var listenerComplete = false; 34var exception = false; 35 36var base_request = '"seq":0,"type":"request","command":"clearbreakpointgroup"'; 37var scriptId = null; 38var muteListener = false; 39 40function safeEval(code) { 41 try { 42 muteListener = true; 43 return eval('(' + code + ')'); 44 } catch (e) { 45 assertEquals(void 0, e); 46 return undefined; 47 } finally { 48 muteListener = false; 49 } 50} 51 52function testArguments(dcp, arguments, success) { 53 var request = '{' + base_request + ',"arguments":' + arguments + '}' 54 var json_response = dcp.processDebugJSONRequest(request); 55 var response = safeEval(json_response); 56 if (success) { 57 assertTrue(response.success, json_response); 58 } else { 59 assertFalse(response.success, json_response); 60 } 61} 62 63function listener(event, exec_state, event_data, data) { 64 if (muteListener) return; 65 try { 66 if (event == Debug.DebugEvent.Break) { 67 // Get the debug command processor. 68 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); 69 70 // Clear breakpoint group 1. 71 testArguments(dcp, '{"groupId":1}', true); 72 73 // Indicate that all was processed. 74 listenerComplete = true; 75 } else if (event == Debug.DebugEvent.AfterCompile) { 76 scriptId = event_data.script().id(); 77 assertEquals(source, event_data.script().source()); 78 } 79 } catch (e) { 80 exception = e 81 }; 82}; 83 84 85// Add the debug event listener. 86Debug.setListener(listener); 87 88var source = 'function f(n) {\nreturn n+1;\n}\nfunction g() {return f(10);}' + 89 '\nvar r = g(); g;'; 90eval(source); 91 92assertNotNull(scriptId); 93 94var groupId1 = 1; 95var groupId2 = 2; 96// Set a break point and call to invoke the debug event listener. 97var bp1 = Debug.setScriptBreakPointById(scriptId, 1, null, null, groupId1); 98var bp2 = Debug.setScriptBreakPointById(scriptId, 1, null, null, groupId2); 99var bp3 = Debug.setScriptBreakPointById(scriptId, 1, null, null, null); 100var bp4 = Debug.setScriptBreakPointById(scriptId, 3, null, null, groupId1); 101var bp5 = Debug.setScriptBreakPointById(scriptId, 4, null, null, groupId2); 102 103assertEquals(5, Debug.scriptBreakPoints().length); 104 105// Call function 'g' from the compiled script to trigger breakpoint. 106g(); 107 108// Make sure that the debug event listener vas invoked. 109assertTrue(listenerComplete, 110 "listener did not run to completion: " + exception); 111 112var breakpoints = Debug.scriptBreakPoints(); 113assertEquals(3, breakpoints.length); 114var breakpointNumbers = breakpoints.map( 115 function(scriptBreakpoint) { return scriptBreakpoint.number(); }, 116 breakpointNumbers); 117 118// Check that all breakpoints from group 1 were deleted and all the 119// rest are preserved. 120assertEquals([bp2, bp3, bp5].sort(), breakpointNumbers.sort()); 121 122assertFalse(exception, "exception in listener"); 123 124// Clear all breakpoints to allow the test to run again (--stress-opt). 125Debug.clearBreakPoint(bp2); 126Debug.clearBreakPoint(bp3); 127Debug.clearBreakPoint(bp5); 128