diff --git a/toolkit/devtools/client/dbg-client.jsm b/toolkit/devtools/client/dbg-client.jsm index 5ffdd46636d5..5bf90b96cfcb 100644 --- a/toolkit/devtools/client/dbg-client.jsm +++ b/toolkit/devtools/client/dbg-client.jsm @@ -28,6 +28,8 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/NetUtil.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/Timer.jsm"); +Cu.import("resource://gre/modules/devtools/Console.jsm"); + let promise = Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js").Promise; const { defer, resolve, reject } = promise; @@ -1407,17 +1409,28 @@ ThreadClient.prototype = { * @param function aOnResponse * Called with the thread's response. */ - setBreakpoint: function (aLocation, aOnResponse) { + setBreakpoint: function ({ url, line, column, condition }, aOnResponse) { // A helper function that sets the breakpoint. let doSetBreakpoint = function (aCallback) { - let packet = { to: this._actor, type: "setBreakpoint", - location: aLocation }; + const location = { + url: url, + line: line, + column: column + }; + + let packet = { + to: this._actor, + type: "setBreakpoint", + location: location, + condition: condition + }; this.client.request(packet, function (aResponse) { // Ignoring errors, since the user may be setting a breakpoint in a // dead script that will reappear on a page reload. if (aOnResponse) { - let bpClient = new BreakpointClient(this.client, aResponse.actor, - aLocation); + let bpClient = new BreakpointClient(this.client, + aResponse.actor, + location); if (aCallback) { aCallback(aOnResponse(aResponse, bpClient)); } else { diff --git a/toolkit/devtools/server/actors/root.js b/toolkit/devtools/server/actors/root.js index 4be5bbdbabe9..07bb2a3bac80 100644 --- a/toolkit/devtools/server/actors/root.js +++ b/toolkit/devtools/server/actors/root.js @@ -187,6 +187,8 @@ RootActor.prototype = { storageInspector: true, // Wether storage inspector is read only storageInspectorReadOnly: true, + // Wether conditional breakpoints are supported + conditionalBreakpoints: true } }; }, diff --git a/toolkit/devtools/server/actors/script.js b/toolkit/devtools/server/actors/script.js index 841d3d47eca8..3175b5a0fb6b 100644 --- a/toolkit/devtools/server/actors/script.js +++ b/toolkit/devtools/server/actors/script.js @@ -38,7 +38,7 @@ function BreakpointStore() { // // is an object // - // { url, line[, actor] } + // { url, line, column[, actor] } // // where the `actor` property is optional. this._breakpoints = Object.create(null); @@ -58,6 +58,7 @@ BreakpointStore.prototype = { * - line * - column (optional; omission implies that the breakpoint is for * the whole line) + * - condition (optional) * - actor (optional) */ addBreakpoint: function (aBreakpoint) { @@ -1374,7 +1375,8 @@ ThreadActor.prototype = { let response = this._createAndStoreBreakpoint({ url: url, line: line, - column: column + column: column, + condition: aRequest.condition }); // If the original location of our generated location is different from // the original location we attempted to set the breakpoint on, we will @@ -1442,11 +1444,13 @@ ThreadActor.prototype = { let storedBp = this.breakpointStore.getBreakpoint(aLocation); if (storedBp.actor) { actor = storedBp.actor; + actor.condition = aLocation.condition; } else { storedBp.actor = actor = new BreakpointActor(this, { url: aLocation.url, line: aLocation.line, - column: aLocation.column + column: aLocation.column, + condition: aLocation.condition }); this.threadLifetimePool.addActor(actor); } @@ -4221,15 +4225,17 @@ FrameActor.prototype.requestTypes = { * @param object aLocation * The location of the breakpoint as specified in the protocol. */ -function BreakpointActor(aThreadActor, aLocation) +function BreakpointActor(aThreadActor, { url, line, column, condition }) { this.scripts = []; this.threadActor = aThreadActor; - this.location = aLocation; + this.location = { url: url, line: line, column: column }; + this.condition = condition; } BreakpointActor.prototype = { actorPrefix: "breakpoint", + condition: null, /** * Called when this same breakpoint is added to another Debugger.Script @@ -4255,6 +4261,14 @@ BreakpointActor.prototype = { this.scripts = []; }, + isValidCondition: function(aFrame) { + if(!this.condition) { + return true; + } + var res = aFrame.eval(this.condition); + return res.return; + }, + /** * A function that the engine calls when a breakpoint has been hit. * @@ -4271,7 +4285,9 @@ BreakpointActor.prototype = { column: this.location.column })); - if (this.threadActor.sources.isBlackBoxed(url) || aFrame.onStep) { + if (this.threadActor.sources.isBlackBoxed(url) + || aFrame.onStep + || !this.isValidCondition(aFrame)) { return undefined; } diff --git a/toolkit/devtools/server/main.js b/toolkit/devtools/server/main.js index 1fdca2634aa5..699126240bf0 100644 --- a/toolkit/devtools/server/main.js +++ b/toolkit/devtools/server/main.js @@ -65,6 +65,7 @@ this.promised = promised; this.all = all; Cu.import("resource://gre/modules/devtools/SourceMap.jsm"); +Cu.import("resource://gre/modules/devtools/Console.jsm"); function dumpn(str) { if (wantLogging) { diff --git a/toolkit/devtools/server/tests/unit/head_dbg.js b/toolkit/devtools/server/tests/unit/head_dbg.js index 69f0fbcc22a8..135ec33ef10e 100644 --- a/toolkit/devtools/server/tests/unit/head_dbg.js +++ b/toolkit/devtools/server/tests/unit/head_dbg.js @@ -30,6 +30,7 @@ function tryImport(url) { tryImport("resource://gre/modules/devtools/dbg-server.jsm"); tryImport("resource://gre/modules/devtools/dbg-client.jsm"); tryImport("resource://gre/modules/devtools/Loader.jsm"); +tryImport("resource://gre/modules/devtools/Console.jsm"); function testExceptionHook(ex) { try { diff --git a/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-01.js b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-01.js new file mode 100644 index 000000000000..3d229b50ea32 --- /dev/null +++ b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-01.js @@ -0,0 +1,61 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Check conditional breakpoint when condition evaluates to true. + */ + +var gDebuggee; +var gClient; +var gThreadClient; + +function run_test() +{ + initTestDebuggerServer(); + gDebuggee = addTestGlobal("test-conditional-breakpoint"); + gClient = new DebuggerClient(DebuggerServer.connectPipe()); + gClient.connect(function () { + attachTestTabAndResume(gClient, "test-conditional-breakpoint", function (aResponse, aTabClient, aThreadClient) { + gThreadClient = aThreadClient; + test_simple_breakpoint(); + }); + }); + do_test_pending(); +} + +function test_simple_breakpoint() +{ + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + gThreadClient.setBreakpoint({ + url: "test.js", + line: 3, + condition: "a === 1" + }, function (aResponse, bpClient) { + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + // Check the return value. + do_check_eq(aPacket.why.type, "breakpoint"); + do_check_eq(aPacket.frame.where.line, 3); + + // Remove the breakpoint. + bpClient.remove(function (aResponse) { + gThreadClient.resume(function () { + finishClient(gClient); + }); + }); + + }); + // Continue until the breakpoint is hit. + gThreadClient.resume(); + + }); + + }); + + Components.utils.evalInSandbox("debugger;\n" + // 1 + "var a = 1;\n" + // 2 + "var b = 2;\n", // 3 + gDebuggee, + "1.8", + "test.js", + 1); +} diff --git a/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-02.js b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-02.js new file mode 100644 index 000000000000..f74cedc4e704 --- /dev/null +++ b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-02.js @@ -0,0 +1,60 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Check conditional breakpoint when condition evaluates to false. + */ + +var gDebuggee; +var gClient; +var gThreadClient; + +function run_test() +{ + initTestDebuggerServer(); + gDebuggee = addTestGlobal("test-conditional-breakpoint"); + gClient = new DebuggerClient(DebuggerServer.connectPipe()); + gClient.connect(function () { + attachTestTabAndResume(gClient, "test-conditional-breakpoint", function (aResponse, aTabClient, aThreadClient) { + gThreadClient = aThreadClient; + test_simple_breakpoint(); + }); + }); + do_test_pending(); +} + +function test_simple_breakpoint() +{ + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + gThreadClient.setBreakpoint({ + url: "test.js", + line: 3, + condition: "a === 2" + }, function (aResponse, bpClient) { + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + // Check the return value. + do_check_eq(aPacket.why.type, "debuggerStatement"); + do_check_eq(aPacket.frame.where.line, 4); + + // Remove the breakpoint. + bpClient.remove(function (aResponse) { + gThreadClient.resume(function () { + finishClient(gClient); + }); + }); + + }); + // Continue until the breakpoint is hit. + gThreadClient.resume(); + }); + }); + + Components.utils.evalInSandbox("debugger;\n" + // 1 + "var a = 1;\n" + // 2 + "var b = 2;\n" + // 3 + "debugger;", // 4 + gDebuggee, + "1.8", + "test.js", + 1); +} diff --git a/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-03.js b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-03.js new file mode 100644 index 000000000000..c340a1cab30a --- /dev/null +++ b/toolkit/devtools/server/tests/unit/test_conditional_breakpoint-03.js @@ -0,0 +1,62 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Check conditional breakpoint when condition throws and make sure it is ignored + */ + +var gDebuggee; +var gClient; +var gThreadClient; + +function run_test() +{ + initTestDebuggerServer(); + gDebuggee = addTestGlobal("test-conditional-breakpoint"); + gClient = new DebuggerClient(DebuggerServer.connectPipe()); + gClient.connect(function () { + attachTestTabAndResume(gClient, "test-conditional-breakpoint", function (aResponse, aTabClient, aThreadClient) { + gThreadClient = aThreadClient; + test_simple_breakpoint(); + }); + }); + do_test_pending(); +} + +function test_simple_breakpoint() +{ + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + gThreadClient.setBreakpoint({ + url: "test.js", + line: 3, + condition: "throw new Error()" + }, function (aResponse, bpClient) { + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { + // Check the return value. + do_check_eq(aPacket.why.type, "debuggerStatement"); + do_check_eq(aPacket.frame.where.line, 4); + + // Remove the breakpoint. + bpClient.remove(function (aResponse) { + gThreadClient.resume(function () { + finishClient(gClient); + }); + }); + + }); + // Continue until the breakpoint is hit. + gThreadClient.resume(); + + }); + + }); + + Components.utils.evalInSandbox("debugger;\n" + // 1 + "var a = 1;\n" + // 2 + "var b = 2;\n" + // 3 + "debugger;", // 4 + gDebuggee, + "1.8", + "test.js", + 1); +} diff --git a/toolkit/devtools/server/tests/unit/xpcshell.ini b/toolkit/devtools/server/tests/unit/xpcshell.ini index 8808865d1111..b7e548bd282f 100644 --- a/toolkit/devtools/server/tests/unit/xpcshell.ini +++ b/toolkit/devtools/server/tests/unit/xpcshell.ini @@ -112,6 +112,9 @@ reason = bug 820380 [test_breakpoint-16.js] [test_breakpoint-17.js] [test_breakpoint-18.js] +[test_conditional_breakpoint-01.js] +[test_conditional_breakpoint-02.js] +[test_conditional_breakpoint-03.js] [test_eventlooplag_actor.js] run-if = toolkit == "gonk" [test_listsources-01.js]