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