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
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.
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.
28 // Flags: --expose-debug-as debug
29 // Get the Debug object exposed from the debug context global object.
32 // Simple function which stores the last debug event.
33 var listenerComplete = false;
34 var exception = false;
41 var base_request = '"seq":0,"type":"request","command":"setbreakpoint"'
43 function safeEval(code) {
45 return eval('(' + code + ')');
47 assertEquals(void 0, e);
52 function testArguments(dcp, arguments, success, is_script) {
53 var request = '{' + base_request + ',"arguments":' + arguments + '}'
54 var json_response = dcp.processDebugJSONRequest(request);
55 var response = safeEval(json_response);
57 assertTrue(response.success, request + ' -> ' + json_response);
59 assertEquals('scriptName', response.body.type, request + ' -> ' + json_response);
61 assertEquals('scriptId', response.body.type, request + ' -> ' + json_response);
64 assertFalse(response.success, request + ' -> ' + json_response);
69 function listener(event, exec_state, event_data, data) {
71 if (event == Debug.DebugEvent.Break) {
72 // Get the debug command processor.
73 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
75 // Test some illegal setbreakpoint requests.
76 var request = '{' + base_request + '}'
77 var response = safeEval(dcp.processDebugJSONRequest(request));
78 assertFalse(response.success);
82 testArguments(dcp, '{}', false);
83 testArguments(dcp, '{"type":"xx"}', false);
84 testArguments(dcp, '{"type":"function"}', false);
85 testArguments(dcp, '{"type":"script"}', false);
86 testArguments(dcp, '{"target":"f"}', false);
87 testArguments(dcp, '{"type":"xx","target":"xx"}', false);
88 testArguments(dcp, '{"type":"function","target":1}', false);
89 testArguments(dcp, '{"type":"function","target":"f","line":-1}', false);
90 testArguments(dcp, '{"type":"function","target":"f","column":-1}', false);
91 testArguments(dcp, '{"type":"function","target":"f","ignoreCount":-1}', false);
92 testArguments(dcp, '{"type":"handle","target":"-1"}', false);
93 mirror = debug.MakeMirror(o);
94 testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', false);
96 // Test some legal setbreakpoint requests.
97 testArguments(dcp, '{"type":"function","target":"f"}', true, false);
98 testArguments(dcp, '{"type":"function","target":"h"}', true, false);
99 testArguments(dcp, '{"type":"function","target":"f","line":1}', true, false);
100 testArguments(dcp, '{"type":"function","target":"f","position":1}', true, false);
101 testArguments(dcp, '{"type":"function","target":"f","condition":"i == 1"}', true, false);
102 testArguments(dcp, '{"type":"function","target":"f","enabled":true}', true, false);
103 testArguments(dcp, '{"type":"function","target":"f","enabled":false}', true, false);
104 testArguments(dcp, '{"type":"function","target":"f","ignoreCount":7}', true, false);
106 testArguments(dcp, '{"type":"script","target":"test"}', true, true);
107 testArguments(dcp, '{"type":"script","target":"test"}', true, true);
108 testArguments(dcp, '{"type":"script","target":"test","line":1}', true, true);
109 testArguments(dcp, '{"type":"script","target":"test","column":1}', true, true);
111 testArguments(dcp, '{"type":"scriptId","target":' + f_script_id + ',"line":' + f_line + '}', true, false);
112 testArguments(dcp, '{"type":"scriptId","target":' + g_script_id + ',"line":' + g_line + '}', true, false);
113 testArguments(dcp, '{"type":"scriptId","target":' + h_script_id + ',"line":' + h_line + '}', true, false);
115 mirror = debug.MakeMirror(f);
116 testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', true, false);
117 mirror = debug.MakeMirror(o.a);
118 testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', true, false);
120 testArguments(dcp, '{"type":"script","target":"sourceUrlScript","line":0}', true, true);
122 // Set a break point on a line with the comment, and check that actual position
123 // is the next line after the comment.
124 request = '{"type":"scriptId","target":' + g_script_id + ',"line":' + (g_line + 1) + '}';
125 response = testArguments(dcp, request, true, false);
126 assertEquals(g_line + 2, response.body.actual_locations[0].line);
128 // Indicate that all was processed.
129 listenerComplete = true;
136 // Add the debug event listener.
137 Debug.setListener(listener);
148 eval('function h(){}');
149 eval('function sourceUrlFunc() { a = 2; }\n//# sourceURL=sourceUrlScript');
151 o = {a:function(){},b:function(){}}
153 // Check the script ids for the test functions.
154 f_script_id = Debug.findScript(f).id;
155 g_script_id = Debug.findScript(g).id;
156 h_script_id = Debug.findScript(h).id;
157 sourceURL_script_id = Debug.findScript(sourceUrlFunc).id;
159 assertTrue(f_script_id > 0, "invalid script id for f");
160 assertTrue(g_script_id > 0, "invalid script id for g");
161 assertTrue(h_script_id > 0, "invalid script id for h");
162 assertTrue(sourceURL_script_id > 0, "invalid script id for sourceUrlFunc");
163 assertEquals(f_script_id, g_script_id);
165 // Get the source line for the test functions.
166 f_line = Debug.findFunctionSourceLocation(f).line;
167 g_line = Debug.findFunctionSourceLocation(g).line;
168 h_line = Debug.findFunctionSourceLocation(h).line;
169 assertTrue(f_line > 0, "invalid line for f");
170 assertTrue(g_line > 0, "invalid line for g");
171 assertTrue(f_line < g_line);
172 assertEquals(h_line, 0, "invalid line for h");
174 // Set a break point and call to invoke the debug event listener.
175 Debug.setBreakPoint(g, 0, 0);
178 // Make sure that the debug event listener was invoked.
179 assertTrue(listenerComplete, "listener did not run to completion: " + exception);
181 // Try setting breakpoint by url specified in sourceURL
183 var breakListenerCalled = false;
185 function breakListener(event) {
186 if (event == Debug.DebugEvent.Break)
187 breakListenerCalled = true;
190 Debug.setListener(breakListener);
194 assertTrue(breakListenerCalled, "Break listener not called on breakpoint set by sourceURL");
197 // Breakpoint in a script with no statements test case. If breakpoint is set
198 // to the script body, its actual position is taken from the nearest statement
199 // below or like in this case is reset to the very end of the script.
200 // Unless some precautions made, this position becomes out-of-range and
201 // we get an exception.
203 // Gets a script of 'i1' function and sets the breakpoint at line #4 which
205 function SetBreakpointInI1Script() {
206 var i_script = Debug.findScript(i1);
207 assertTrue(!!i_script, "invalid script for i1");
208 Debug.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
212 // Creates the eval script and tries to set the breakpoint.
213 // The tricky part is that the script function must be strongly reachable at the
214 // moment. Since there's no way of simply getting the pointer to the function,
215 // we run this code while the script function is being activated on stack.
216 eval('SetBreakpointInI1Script()\nfunction i1(){}\n\n\n\nfunction i2(){}\n');