diff options
Diffstat (limited to 'devtools/server/tracer/tests')
3 files changed, 136 insertions, 74 deletions
diff --git a/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js b/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js index bd6e646b3b..ef15fd5cfb 100644 --- a/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js +++ b/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js @@ -1,6 +1,6 @@ "use strict"; -/* global global, loadSubScript */ +/* global global */ try { // For some reason WorkerDebuggerGlobalScope.global doesn't expose JS variables @@ -8,8 +8,11 @@ try { const dbg = new Debugger(global); const [debuggee] = dbg.getDebuggees(); - /* global startTracing, stopTracing, addTracingListener, removeTracingListener */ - loadSubScript("resource://devtools/server/tracer/tracer.jsm"); + const { JSTracer } = ChromeUtils.importESModule( + "resource://devtools/server/tracer/tracer.sys.mjs", + { global: "contextual" } + ); + const frames = []; const listener = { onTracingFrame(args) { @@ -19,13 +22,13 @@ try { return true; }, }; - addTracingListener(listener); - startTracing({ global, prefix: "testWorkerPrefix" }); + JSTracer.addTracingListener(listener); + JSTracer.startTracing({ global, prefix: "testWorkerPrefix" }); debuggee.executeInGlobal("foo()"); - stopTracing(); - removeTracingListener(listener); + JSTracer.stopTracing(); + JSTracer.removeTracingListener(listener); // Send the frames to the main thread to do the assertions there. postMessage(JSON.stringify(frames)); diff --git a/devtools/server/tracer/tests/browser/browser_document_tracer.js b/devtools/server/tracer/tests/browser/browser_document_tracer.js index dcf2c9eb4d..d7d351d6dd 100644 --- a/devtools/server/tracer/tests/browser/browser_document_tracer.js +++ b/devtools/server/tracer/tests/browser/browser_document_tracer.js @@ -17,12 +17,10 @@ add_task(async function testTracingWorker() { const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { - const { - addTracingListener, - removeTracingListener, - startTracing, - stopTracing, - } = ChromeUtils.import("resource://devtools/server/tracer/tracer.jsm"); + const { JSTracer } = ChromeUtils.importESModule( + "resource://devtools/server/tracer/tracer.sys.mjs", + { global: "shared" } + ); // We have to fake opening DevTools otherwise DebuggerNotificationObserver wouldn't work // and the tracer wouldn't be able to trace the DOM events. @@ -35,10 +33,10 @@ add_task(async function testTracingWorker() { }, }; info("Register a tracing listener"); - addTracingListener(listener); + JSTracer.addTracingListener(listener); info("Start tracing the iframe"); - startTracing({ global: content, traceDOMEvents: true }); + JSTracer.startTracing({ global: content, traceDOMEvents: true }); info("Dispatch a click event on the iframe"); EventUtils.synthesizeMouseAtCenter( @@ -58,8 +56,8 @@ add_task(async function testTracingWorker() { is(lastFrame.formatedDisplayName, "λ bar"); is(lastFrame.currentDOMEvent, "setTimeoutCallback"); - stopTracing(); - removeTracingListener(listener); + JSTracer.stopTracing(); + JSTracer.removeTracingListener(listener); ChromeUtils.notifyDevToolsClosed(); }); diff --git a/devtools/server/tracer/tests/xpcshell/test_tracer.js b/devtools/server/tracer/tests/xpcshell/test_tracer.js index 0f38052ba5..8435cb8691 100644 --- a/devtools/server/tracer/tests/xpcshell/test_tracer.js +++ b/devtools/server/tracer/tests/xpcshell/test_tracer.js @@ -3,8 +3,10 @@ "use strict"; -const { addTracingListener, removeTracingListener, startTracing, stopTracing } = - ChromeUtils.import("resource://devtools/server/tracer/tracer.jsm"); +const { JSTracer } = ChromeUtils.importESModule( + "resource://devtools/server/tracer/tracer.sys.mjs", + { global: "shared" } +); add_task(async function () { // Because this test uses evalInSandbox, we need to tweak the following prefs @@ -30,13 +32,13 @@ add_task(async function testTracingContentGlobal() { }; info("Register a tracing listener"); - addTracingListener(listener); + JSTracer.addTracingListener(listener); const sandbox = Cu.Sandbox("https://example.com"); Cu.evalInSandbox("function bar() {}; function foo() {bar()};", sandbox); info("Start tracing"); - startTracing({ global: sandbox, prefix: "testContentPrefix" }); + JSTracer.startTracing({ global: sandbox, prefix: "testContentPrefix" }); Assert.equal(toggles.length, 1); Assert.equal(toggles[0], true); @@ -56,7 +58,7 @@ add_task(async function testTracingContentGlobal() { Assert.ok(lastFrame.frame); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); Assert.equal(toggles.length, 2); Assert.equal(toggles[1], false); @@ -65,19 +67,19 @@ add_task(async function testTracingContentGlobal() { Assert.equal(frames.length, 0); info("Start tracing again, and recall code"); - startTracing({ global: sandbox, prefix: "testContentPrefix" }); + JSTracer.startTracing({ global: sandbox, prefix: "testContentPrefix" }); sandbox.foo(); info("New traces are logged"); Assert.equal(frames.length, 2); info("Unregister the listener and recall code"); - removeTracingListener(listener); + JSTracer.removeTracingListener(listener); sandbox.foo(); info("No more traces are logged"); Assert.equal(frames.length, 2); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingJSMGlobal() { @@ -103,10 +105,10 @@ add_task(async function testTracingJSMGlobal() { ); info("Register a tracing listener"); - addTracingListener(listenerSandbox.listener); + JSTracer.addTracingListener(listenerSandbox.listener); info("Start tracing"); - startTracing({ global: null, prefix: "testPrefix" }); + JSTracer.startTracing({ global: null, prefix: "testPrefix" }); Assert.equal(listenerSandbox.toggles.length, 1); Assert.equal(listenerSandbox.toggles[0], true); @@ -131,11 +133,11 @@ add_task(async function testTracingJSMGlobal() { Assert.ok(lastFrame.frame); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); Assert.equal(listenerSandbox.toggles.length, 2); Assert.equal(listenerSandbox.toggles[1], false); - removeTracingListener(listenerSandbox.listener); + JSTracer.removeTracingListener(listenerSandbox.listener); }); add_task(async function testTracingValues() { @@ -153,7 +155,7 @@ add_task(async function testTracingValues() { } info("Start tracing"); - startTracing({ global: sandbox, traceValues: true, loggingMethod }); + JSTracer.startTracing({ global: sandbox, traceValues: true, loggingMethod }); info("Call some code"); sandbox.foo(); @@ -167,7 +169,7 @@ add_task(async function testTracingValues() { ); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingFunctionReturn() { @@ -185,7 +187,11 @@ add_task(async function testTracingFunctionReturn() { } info("Start tracing"); - startTracing({ global: sandbox, traceFunctionReturn: true, loggingMethod }); + JSTracer.startTracing({ + global: sandbox, + traceFunctionReturn: true, + loggingMethod, + }); info("Call some code"); sandbox.foo(); @@ -198,7 +204,7 @@ add_task(async function testTracingFunctionReturn() { Assert.stringContains(logs[4], "λ foo return"); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingFunctionReturnAndValues() { @@ -216,7 +222,7 @@ add_task(async function testTracingFunctionReturnAndValues() { } info("Start tracing"); - startTracing({ + JSTracer.startTracing({ global: sandbox, traceFunctionReturn: true, traceValues: true, @@ -236,7 +242,7 @@ add_task(async function testTracingFunctionReturnAndValues() { Assert.stringContains(logs[6], "λ foo return undefined"); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingStep() { @@ -246,22 +252,23 @@ add_task(async function testTracingStep() { function foo() { bar(); /* line 3 */ second(); /* line 4 */ + dump("foo\\n"); } function bar() { - let res; /* line 7 */ - if (1 === 1) { /* line 8 */ - res = "string"; /* line 9 */ + let res; /* line 8 */ + if (1 === 1) { /* line 9 */ + res = "string"; /* line 10 */ } else { res = "nope" } - return res; /* line 13 */ + return res; /* line 14 */ }; function second() { - let x = 0; /* line 16 */ - for (let i = 0; i < 2; i++) { /* line 17 */ - x++; /* line 18 */ + let x = 0; /* line 17 */ + for (let i = 0; i < 2; i++) { /* line 18 */ + x++; /* line 19 */ } - return null; /* line 20 */ + return null; /* line 21 */ }; foo();`; Cu.evalInSandbox(source, sandbox, null, "file.js", 1); @@ -273,7 +280,7 @@ foo();`; } info("Start tracing"); - startTracing({ + JSTracer.startTracing({ global: sandbox, traceSteps: true, loggingMethod, @@ -287,47 +294,46 @@ foo();`; Assert.stringContains(logs[1], "λ foo"); Assert.stringContains(logs[1], "file.js:3:3"); - // Each "step" only prints the location and nothing more - Assert.stringContains(logs[2], "file.js:3:3"); - - Assert.stringContains(logs[3], "λ bar"); - Assert.stringContains(logs[3], "file.js:6:16"); + Assert.stringContains(logs[2], "λ bar"); + Assert.stringContains(logs[2], "file.js:7:16"); - Assert.stringContains(logs[4], "file.js:8:7"); + // Each "step" only prints the location and nothing more + Assert.stringContains(logs[3], "file.js:9:7"); - Assert.stringContains(logs[5], "file.js:9:5"); + Assert.stringContains(logs[4], "file.js:10:5"); - Assert.stringContains(logs[6], "file.js:13:3"); + Assert.stringContains(logs[5], "file.js:14:3"); - Assert.stringContains(logs[7], "file.js:4:3"); + Assert.stringContains(logs[6], "file.js:4:3"); - Assert.stringContains(logs[8], "λ second"); - Assert.stringContains(logs[8], "file.js:15:19"); + Assert.stringContains(logs[7], "λ second"); + Assert.stringContains(logs[7], "file.js:16:19"); - Assert.stringContains(logs[9], "file.js:16:11"); + Assert.stringContains(logs[8], "file.js:17:11"); // For loop - Assert.stringContains(logs[10], "file.js:17:16"); + Assert.stringContains(logs[9], "file.js:18:16"); - Assert.stringContains(logs[11], "file.js:17:19"); + Assert.stringContains(logs[10], "file.js:18:19"); - Assert.stringContains(logs[12], "file.js:18:5"); + Assert.stringContains(logs[11], "file.js:19:5"); - Assert.stringContains(logs[13], "file.js:17:26"); + Assert.stringContains(logs[12], "file.js:18:26"); - Assert.stringContains(logs[14], "file.js:17:19"); + Assert.stringContains(logs[13], "file.js:18:19"); - Assert.stringContains(logs[15], "file.js:18:5"); + Assert.stringContains(logs[14], "file.js:19:5"); - Assert.stringContains(logs[16], "file.js:17:26"); + Assert.stringContains(logs[15], "file.js:18:26"); - Assert.stringContains(logs[17], "file.js:17:19"); + Assert.stringContains(logs[16], "file.js:18:19"); // End of for loop - Assert.stringContains(logs[18], "file.js:20:3"); + Assert.stringContains(logs[17], "file.js:21:3"); + Assert.stringContains(logs[18], "file.js:5:3"); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingPauseOnStep() { @@ -348,7 +354,7 @@ add_task(async function testTracingPauseOnStep() { } info("Start tracing without pause"); - startTracing({ + JSTracer.startTracing({ global: sandbox, loggingMethod, }); @@ -366,13 +372,13 @@ add_task(async function testTracingPauseOnStep() { Assert.equal(sandbox.counter, 1); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); logs.length = 0; sandbox.counter = 0; info("Start tracing with 0ms pause"); - startTracing({ + JSTracer.startTracing({ global: sandbox, pauseOnStep: 0, loggingMethod, @@ -415,13 +421,13 @@ add_task(async function testTracingPauseOnStep() { Assert.equal(sandbox.counter, 1); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); logs.length = 0; sandbox.counter = 0; info("Start tracing with 250ms pause"); - startTracing({ + JSTracer.startTracing({ global: sandbox, pauseOnStep: 250, loggingMethod, @@ -463,7 +469,7 @@ add_task(async function testTracingPauseOnStep() { Assert.greater(Cu.now() - startTimestamp, 250); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); }); add_task(async function testTracingFilterSourceUrl() { @@ -485,7 +491,7 @@ add_task(async function testTracingFilterSourceUrl() { } info("Start tracing"); - startTracing({ + JSTracer.startTracing({ global: sandbox, filterFrameSourceUrl: "second", loggingMethod, @@ -500,5 +506,60 @@ add_task(async function testTracingFilterSourceUrl() { Assert.stringContains(logs[1], "second.js:1:18"); info("Stop tracing"); - stopTracing(); + JSTracer.stopTracing(); +}); + +add_task(async function testTracingAllGlobals() { + // Test the `traceAllGlobals` flag + + // Create two distinct globals in order to verify that both are traced + const sandbox1 = Cu.Sandbox("https://example.com"); + const sandbox2 = Cu.Sandbox("https://example.com"); + + const source1 = `function foo() { bar(); }`; + Cu.evalInSandbox(source1, sandbox1, null, "sandbox1.js", 1); + + const source2 = `function bar() { }`; + Cu.evalInSandbox(source2, sandbox2, null, "sandbox2.js", 1); + // Expose `bar` from sandbox2 as global in sandbox1, so that `foo` from sandbox1 can call it. + sandbox1.bar = sandbox2.bar; + + // Pass an override method to catch all strings tentatively logged to stdout + // + // But in this test, we have to evaluate it in a special sandbox which will be ignored by the tracer. + // Otherwise, the tracer would do an infinite loop on this loggingMethod. + const ignoredGlobal = new Cu.Sandbox(null, { invisibleToDebugger: true }); + const loggingMethodString = ` + var logs = []; + function loggingMethod(str) { + logs.push(str); + }; + `; + Cu.evalInSandbox( + loggingMethodString, + ignoredGlobal, + null, + "loggin-method.js", + 1 + ); + const { loggingMethod, logs } = ignoredGlobal; + + info("Start tracing on all globals"); + JSTracer.startTracing({ + traceAllGlobals: true, + loggingMethod, + }); + + // Call some code while being careful to not call anything else which may be traced + sandbox1.foo(); + + JSTracer.stopTracing(); + + Assert.equal(logs.length, 4); + Assert.equal(logs[0], "Start tracing JavaScript\n"); + Assert.stringContains(logs[1], "λ foo"); + Assert.stringContains(logs[1], "sandbox1.js:1:18"); + Assert.stringContains(logs[2], "λ bar"); + Assert.stringContains(logs[2], "sandbox2.js:1:18"); + Assert.equal(logs[3], "Stop tracing JavaScript\n"); }); |