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