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.
30Debug = debug.Debug
31
32Debug.setListener(function() {});
33
34function f() {a=1;b=2}
35function g() {
36  a=1;
37  b=2;
38}
39
40bp = Debug.setBreakPoint(f, 0, 0);
41assertEquals("() {[B0]a=1;b=2}", Debug.showBreakPoints(f));
42Debug.clearBreakPoint(bp);
43assertEquals("() {a=1;b=2}", Debug.showBreakPoints(f));
44bp1 = Debug.setBreakPoint(f, 0, 8);
45assertEquals("() {a=1;[B0]b=2}", Debug.showBreakPoints(f));
46bp2 = Debug.setBreakPoint(f, 0, 4);
47assertEquals("() {[B0]a=1;[B1]b=2}", Debug.showBreakPoints(f));
48bp3 = Debug.setBreakPoint(f, 0, 11);
49assertEquals("() {[B0]a=1;[B1]b=2[B2]}", Debug.showBreakPoints(f));
50Debug.clearBreakPoint(bp1);
51assertEquals("() {[B0]a=1;b=2[B1]}", Debug.showBreakPoints(f));
52Debug.clearBreakPoint(bp2);
53assertEquals("() {a=1;b=2[B0]}", Debug.showBreakPoints(f));
54Debug.clearBreakPoint(bp3);
55assertEquals("() {a=1;b=2}", Debug.showBreakPoints(f));
56
57// The following test checks that the Debug.showBreakPoints(g) produces output
58// like follows when changein breakpoints.
59//
60// function g() {
61//   [BX]a=1;
62//   [BX]b=2;
63// }[BX]
64
65// Test set and clear breakpoint at the first possible location (line 0,
66// position 0).
67bp = Debug.setBreakPoint(g, 0, 0);
68// function g() {
69//   [B0]a=1;
70//   b=2;
71// }
72assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
73Debug.clearBreakPoint(bp);
74// function g() {
75//   a=1;
76//   b=2;
77// }
78assertTrue(Debug.showBreakPoints(g).indexOf("[B0]") < 0);
79
80// Second test set and clear breakpoints on lines 1, 2 and 3 (position = 0).
81bp1 = Debug.setBreakPoint(g, 2, 0);
82// function g() {
83//   a=1;
84//   [B0]b=2;
85// }
86assertTrue(Debug.showBreakPoints(g).indexOf("[B0]b=2;") > 0);
87bp2 = Debug.setBreakPoint(g, 1, 0);
88// function g() {
89//   [B0]a=1;
90//   [B1]b=2;
91// }
92assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
93assertTrue(Debug.showBreakPoints(g).indexOf("[B1]b=2;") > 0);
94bp3 = Debug.setBreakPoint(g, 3, 0);
95// function g() {
96//   [B0]a=1;
97//   [B1]b=2;
98// }[B2]
99assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
100assertTrue(Debug.showBreakPoints(g).indexOf("[B1]b=2;") > 0);
101assertTrue(Debug.showBreakPoints(g).indexOf("[B2]}") > 0);
102Debug.clearBreakPoint(bp1);
103// function g() {
104//   [B0]a=1;
105//   b=2;
106// }[B1]
107assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
108assertTrue(Debug.showBreakPoints(g).indexOf("[B1]}") > 0);
109assertTrue(Debug.showBreakPoints(g).indexOf("[B2]") < 0);
110Debug.clearBreakPoint(bp2);
111// function g() {
112//   a=1;
113//   b=2;
114// }[B0]
115assertTrue(Debug.showBreakPoints(g).indexOf("[B0]}") > 0);
116assertTrue(Debug.showBreakPoints(g).indexOf("[B1]") < 0);
117Debug.clearBreakPoint(bp3);
118// function g() {
119//   a=1;
120//   b=2;
121// }
122assertTrue(Debug.showBreakPoints(g).indexOf("[B0]") < 0);
123
124
125// Tests for setting break points by script id and position.
126function setBreakpointByPosition(f, position, opt_position_alignment)
127{
128  var break_point = Debug.setBreakPointByScriptIdAndPosition(
129      Debug.findScript(f).id,
130      position + Debug.sourcePosition(f),
131      "",
132      true, opt_position_alignment);
133  return break_point.number();
134}
135
136bp = setBreakpointByPosition(f, 0);
137assertEquals("() {[B0]a=1;b=2}", Debug.showBreakPoints(f));
138Debug.clearBreakPoint(bp);
139assertEquals("() {a=1;b=2}", Debug.showBreakPoints(f));
140bp1 = setBreakpointByPosition(f, 8);
141assertEquals("() {a=1;[B0]b=2}", Debug.showBreakPoints(f));
142bp2 = setBreakpointByPosition(f, 4);
143assertEquals("() {[B0]a=1;[B1]b=2}", Debug.showBreakPoints(f));
144bp3 = setBreakpointByPosition(f, 11);
145assertEquals("() {[B0]a=1;[B1]b=2[B2]}", Debug.showBreakPoints(f));
146Debug.clearBreakPoint(bp1);
147assertEquals("() {[B0]a=1;b=2[B1]}", Debug.showBreakPoints(f));
148Debug.clearBreakPoint(bp2);
149assertEquals("() {a=1;b=2[B0]}", Debug.showBreakPoints(f));
150Debug.clearBreakPoint(bp3);
151assertEquals("() {a=1;b=2}", Debug.showBreakPoints(f));
152
153bp = setBreakpointByPosition(g, 0);
154//function g() {
155//[B0]a=1;
156//b=2;
157//}
158assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
159Debug.clearBreakPoint(bp);
160//function g() {
161//a=1;
162//b=2;
163//}
164assertTrue(Debug.showBreakPoints(g).indexOf("[B0]") < 0);
165
166//Second test set and clear breakpoints on lines 1, 2 and 3 (column = 0).
167bp1 = setBreakpointByPosition(g, 12);
168//function g() {
169//a=1;
170//[B0]b=2;
171//}
172assertTrue(Debug.showBreakPoints(g).indexOf("[B0]b=2;") > 0);
173bp2 = setBreakpointByPosition(g, 5);
174//function g() {
175//[B0]a=1;
176//[B1]b=2;
177//}
178assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
179assertTrue(Debug.showBreakPoints(g).indexOf("[B1]b=2;") > 0);
180bp3 = setBreakpointByPosition(g, 19);
181//function g() {
182//[B0]a=1;
183//[B1]b=2;
184//}[B2]
185assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
186assertTrue(Debug.showBreakPoints(g).indexOf("[B1]b=2;") > 0);
187assertTrue(Debug.showBreakPoints(g).indexOf("[B2]}") > 0);
188Debug.clearBreakPoint(bp1);
189//function g() {
190//[B0]a=1;
191//b=2;
192//}[B1]
193assertTrue(Debug.showBreakPoints(g).indexOf("[B0]a=1;") > 0);
194assertTrue(Debug.showBreakPoints(g).indexOf("[B1]}") > 0);
195assertTrue(Debug.showBreakPoints(g).indexOf("[B2]") < 0);
196Debug.clearBreakPoint(bp2);
197//function g() {
198//a=1;
199//b=2;
200//}[B0]
201assertTrue(Debug.showBreakPoints(g).indexOf("[B0]}") > 0);
202assertTrue(Debug.showBreakPoints(g).indexOf("[B1]") < 0);
203Debug.clearBreakPoint(bp3);
204//function g() {
205//a=1;
206//b=2;
207//}
208assertTrue(Debug.showBreakPoints(g).indexOf("[B0]") < 0);
209
210// Tests for setting break points without statement aligment.
211// (This may be sensitive to compiler break position map generation).
212function h() {a=f(f2(1,2),f3())+f3();b=f3();}
213var scenario = [
214  [6, "{a=[B0]f("],
215  [7, "{a=f([B0]f2("],
216  [16, "f2(1,2),[B0]f3()"],
217  [22, "+[B0]f3()"]
218];
219for(var i = 0; i < scenario.length; i++) {
220  bp1 = setBreakpointByPosition(h, scenario[i][0],
221      Debug.BreakPositionAlignment.BreakPosition);
222  assertTrue(Debug.showBreakPoints(h, undefined,
223      Debug.BreakPositionAlignment.BreakPosition).indexOf(scenario[i][1]) > 0);
224  Debug.clearBreakPoint(bp1);
225}
226