19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright 2008 the V8 project authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Redistribution and use in source and binary forms, with or without
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// modification, are permitted provided that the following conditions are
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// met:
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions of source code must retain the above copyright
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       notice, this list of conditions and the following disclaimer.
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions in binary form must reproduce the above
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       copyright notice, this list of conditions and the following
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       disclaimer in the documentation and/or other materials provided
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       with the distribution.
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Neither the name of Google Inc. nor the names of its
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       contributors may be used to endorse or promote products derived
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       from this software without specific prior written permission.
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Flags: --expose-debug-as debug
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Get the Debug object exposed from the debug context global object.
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comDebug = debug.Debug
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Simple debug event handler which first time hit will perform 1000 steps and
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// second time hit will evaluate and store the value of "i". If requires that
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// the global property "state" is initially zero.
359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar bp1, bp2;
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction listener(event, exec_state, event_data, data) {
397be3c996bea370e151c9fe4ecf7f779cdc5f87adkasperl@chromium.org  if (event == Debug.DebugEvent.Break) {
409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    if (state == 0) {
419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      exec_state.prepareStep(Debug.StepAction.StepIn, 1000);
429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      state = 1;
439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    } else if (state == 1) {
449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      result = exec_state.frame().evaluate("i").value();
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      // Clear the break point on line 2 if set.
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      if (bp2) {
479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        Debug.clearBreakPoint(bp2);
489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      }
499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Add the debug event listener.
54245aa859d34fd516161c48ef4c69d38d9b889284iposva@chromium.orgDebug.setListener(listener);
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Test debug event for break point.
579a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction f() {
582356e6fbe66ac3aa027b61cb43a3c3619b3c3a5evegorov@chromium.org  var i;                        // Line 1.
592356e6fbe66ac3aa027b61cb43a3c3619b3c3a5evegorov@chromium.org  for (i = 0; i < 1000; i++) {  // Line 2.
602356e6fbe66ac3aa027b61cb43a3c3619b3c3a5evegorov@chromium.org    x = 1;                      // Line 3.
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Set a breakpoint on the for statement (line 1).
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.combp1 = Debug.setBreakPoint(f, 1);
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Check that performing 1000 steps will make i 499.
689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstate = 0;
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comresult = -1;
709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comf();
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(499, result);
729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Check that performing 1000 steps with a break point on the statement in the
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// for loop (line 2) will only make i 0 as a real break point breaks even when
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// multiple steps have been requested.
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstate = 0;
779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comresult = -1;
782356e6fbe66ac3aa027b61cb43a3c3619b3c3a5evegorov@chromium.orgbp2 = Debug.setBreakPoint(f, 3);
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comf();
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(0, result);
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Get rid of the debug event listener.
83b302e56e5b70c4504faa2adf4ec3efb64a9d3e38sgjesse@chromium.orgDebug.setListener(null);
84