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
32// Simple debug event handler which just counts the number of break points hit.
33var break_point_hit_count;
34
35function listener(event, exec_state, event_data, data) {
36  if (event == Debug.DebugEvent.Break) {
37    break_point_hit_count++;
38  }
39};
40
41// Add the debug event listener.
42Debug.setListener(listener);
43
44// Test functions.
45count = 0;
46function f() {};
47function g() {h(count++)};
48function h(x) {var a=x; return a};
49
50
51// Conditional breakpoint which syntax error.
52break_point_hit_count = 0;
53bp = Debug.setBreakPoint(f, 0, 0, '{{{');
54f();
55assertEquals(0, break_point_hit_count);
56assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
57Debug.clearBreakPoint(bp);
58
59// Conditional breakpoint which evaluates to false.
60break_point_hit_count = 0;
61bp = Debug.setBreakPoint(f, 0, 0, 'false');
62f();
63assertEquals(0, break_point_hit_count);
64assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
65Debug.clearBreakPoint(bp);
66
67// Conditional breakpoint which evaluates to true.
68break_point_hit_count = 0;
69bp = Debug.setBreakPoint(f, 0, 0, 'true');
70f();
71assertEquals(1, break_point_hit_count);
72assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
73Debug.clearBreakPoint(bp);
74
75// Conditional breakpoint which different types of quotes.
76break_point_hit_count = 0;
77bp = Debug.setBreakPoint(f, 0, 0, '"a" == "a"');
78f();
79assertEquals(1, break_point_hit_count);
80assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
81Debug.clearBreakPoint(bp);
82break_point_hit_count = 0;
83bp = Debug.setBreakPoint(f, 0, 0, "'a' == 'a'");
84f();
85assertEquals(1, break_point_hit_count);
86assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
87Debug.clearBreakPoint(bp);
88
89// Changing condition.
90break_point_hit_count = 0;
91bp = Debug.setBreakPoint(f, 0, 0, '"ab".indexOf("b") > 0');
92f();
93assertEquals(1, break_point_hit_count);
94assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
95Debug.changeBreakPointCondition(bp, 'Math.sin(Math.PI/2) > 1');
96f();
97assertEquals(1, break_point_hit_count);
98assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
99Debug.changeBreakPointCondition(bp, '1==1');
100f();
101assertEquals(2, break_point_hit_count);
102assertEquals(2, Debug.findBreakPoint(bp, false).hit_count());
103Debug.clearBreakPoint(bp);
104
105// Conditional breakpoint which checks global variable.
106break_point_hit_count = 0;
107bp = Debug.setBreakPoint(f, 0, 0, 'x==1');
108f();
109assertEquals(0, break_point_hit_count);
110assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
111x=1;
112f();
113assertEquals(1, break_point_hit_count);
114assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
115Debug.clearBreakPoint(bp);
116
117// Conditional breakpoint which checks global variable.
118break_point_hit_count = 0;
119bp = Debug.setBreakPoint(g, 0, 0, 'count % 2 == 0');
120for (var i = 0; i < 10; i++) {
121  g();
122}
123assertEquals(5, break_point_hit_count);
124assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
125Debug.clearBreakPoint(bp);
126
127// Conditional breakpoint which checks a parameter.
128break_point_hit_count = 0;
129bp = Debug.setBreakPoint(h, 0, 0, 'x % 2 == 0');
130for (var i = 0; i < 10; i++) {
131  g();
132}
133assertEquals(5, break_point_hit_count);
134assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
135Debug.clearBreakPoint(bp);
136
137// Conditional breakpoint which checks a local variable.
138break_point_hit_count = 0;
139bp = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
140for (var i = 0; i < 10; i++) {
141  g();
142}
143assertEquals(5, break_point_hit_count);
144assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
145Debug.clearBreakPoint(bp);
146
147// Multiple conditional breakpoint which the same condition.
148break_point_hit_count = 0;
149bp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
150bp2 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
151for (var i = 0; i < 10; i++) {
152  g();
153}
154assertEquals(5, break_point_hit_count);
155assertEquals(5, Debug.findBreakPoint(bp1, false).hit_count());
156assertEquals(5, Debug.findBreakPoint(bp2, false).hit_count());
157Debug.clearBreakPoint(bp1);
158Debug.clearBreakPoint(bp2);
159
160// Multiple conditional breakpoint which different conditions.
161break_point_hit_count = 0;
162bp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
163bp2 = Debug.setBreakPoint(h, 0, 22, '(a + 1) % 2 == 0');
164for (var i = 0; i < 10; i++) {
165  g();
166}
167assertEquals(10, break_point_hit_count);
168assertEquals(5, Debug.findBreakPoint(bp1, false).hit_count());
169assertEquals(5, Debug.findBreakPoint(bp2, false).hit_count());
170Debug.clearBreakPoint(bp1);
171Debug.clearBreakPoint(bp2);
172