v8/test/inspector/debugger/external-stack-trace.js
Alexey Kozyatinskiy 885a5669aa Reland [inspector] introduced stackTraceId and externalAsyncTask API
Sometimes we need to capture stack trace on one debugger and use it
later as a parent stack on another debugger (e.g. worker.postMessage).

This CL includes following addition to our protocol and v8-inspector.h:
  - added Runtime.StackTraceId, this id represents stack trace captured
    on debugger with given id,
  - protocol client can fetch Runtime.StackTrace by
    Runtime.StacKTraceId using Debugger.getStackTrace method,
  - externalParent field is added to Debugger.paused event, it may
    contain external parent stack trace,
  - V8Inspector::storeCurrentStackTrace captures current stack trace
    and returns V8StackTraceId for embedder this id can be used as
    argument for V8Inspector::externalAsyncTaskStarted and
    V8Inspector::externalAsyncTaskFinished method. Any async stack
    trace captured between these calls will get passed external stack
    trace as external parent. These methods are designed to be called
    on different debuggers. If async task is scheduled and started on
    one debugger user should continue to use asyncTask* API,
  - Debugger.enable methods returns unique debuggerId.

TBR=dgozman@chromium.org,jgruber@chromium.org

Bug: chromium:778796
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I2c1a2b2e30ed69ccb61d10f08686f4edb09f50e4
Reviewed-on: https://chromium-review.googlesource.com/786274
Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49591}
2017-11-22 20:56:10 +00:00

171 lines
6.3 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
InspectorTest.log('Tests external stack traces');
let contextGroup1 = new InspectorTest.ContextGroup();
let session1 = contextGroup1.connect();
let Protocol1 = session1.Protocol;
let contextGroup2 = new InspectorTest.ContextGroup();
let session2 = contextGroup2.connect();
let Protocol2 = session2.Protocol;
let utilsScript = `
function store(description) {
let buffer = inspector.storeCurrentStackTrace(description);
return '[' + new Int32Array(buffer).join(',') + ']';
}
function started(id) {
inspector.externalAsyncTaskStarted(Int32Array.from(JSON.parse(id)).buffer);
}
function finished(id) {
inspector.externalAsyncTaskFinished(Int32Array.from(JSON.parse(id)).buffer);
}
//# sourceURL=utils.js`;
contextGroup1.addScript(utilsScript);
contextGroup2.addScript(utilsScript);
InspectorTest.runAsyncTestSuite([
async function testDebuggerId() {
InspectorTest.log('Enabling debugger first time..');
let {result: {debuggerId}} = await Protocol1.Debugger.enable();
let firstDebuggerId = debuggerId;
InspectorTest.log('Enabling debugger again..');
({result: {debuggerId}} = await Protocol1.Debugger.enable());
if (firstDebuggerId !== debuggerId) {
InspectorTest.log(
'FAIL: second Debugger.enable returns different debugger id');
} else {
InspectorTest.log(
'> second Debugger.enable returns the same debugger id');
}
InspectorTest.log('Enabling debugger in another context group..');
({result: {debuggerId}} = await Protocol2.Debugger.enable());
if (firstDebuggerId === debuggerId) {
InspectorTest.log(
'FAIL: Debugger.enable in another context group returns the same debugger id');
} else {
InspectorTest.log(
'> Debugger.enable in another context group returns own debugger id');
}
},
async function testInstrumentation() {
Protocol1.Debugger.enable();
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 32});
let result = await Protocol1.Runtime.evaluate(
{expression: 'id = inspector.storeCurrentStackTrace(\'stack\')'});
let stackTraceId = result.result.result.objectId;
Protocol1.Runtime.evaluate({
expression: `inspector.externalAsyncTaskStarted(id);
debugger;
inspector.externalAsyncTaskFinished(id);`
});
let {params: {callFrames, asyncStackTraceId}} =
await Protocol1.Debugger.oncePaused();
result = await Protocol1.Debugger.getStackTrace(
{stackTraceId: asyncStackTraceId});
InspectorTest.logMessage(result);
await Protocol1.Debugger.disable();
},
async function testDisableStacksAfterStored() {
Protocol1.Debugger.enable();
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 32});
let result = await Protocol1.Runtime.evaluate(
{expression: 'id = inspector.storeCurrentStackTrace(\'stack\')'});
let stackTraceId = result.result.result.objectId;
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 0});
Protocol1.Runtime.evaluate({
expression: `inspector.externalAsyncTaskStarted(id);
debugger;
inspector.externalAsyncTaskFinished(id);`
});
let {params: {callFrames, asyncStackTraceId}} =
await Protocol1.Debugger.oncePaused();
if (!asyncStackTraceId) {
InspectorTest.log('> external async stack trace is empty');
} else {
InspectorTest.log('FAIL: external async stack trace is reported');
}
await Protocol1.Debugger.disable();
},
async function testDisableStacksAfterStarted() {
Protocol1.Debugger.enable();
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 32});
let result = await Protocol1.Runtime.evaluate(
{expression: 'id = inspector.storeCurrentStackTrace(\'stack\')'});
let stackTraceId = result.result.result.objectId;
Protocol1.Runtime.evaluate(
{expression: 'inspector.externalAsyncTaskStarted(id);'});
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 0});
Protocol1.Runtime.evaluate({
expression: `debugger;
inspector.externalAsyncTaskFinished(id);`
});
let {params: {callFrames, asyncStackTraceId}} =
await Protocol1.Debugger.oncePaused();
if (!asyncStackTraceId) {
InspectorTest.log('> external async stack trace is empty');
} else {
InspectorTest.log('FAIL: external async stack trace is reported');
}
await Protocol1.Debugger.disable();
},
async function testExternalStacks() {
let debuggerId1 = (await Protocol1.Debugger.enable()).result.debuggerId;
let debuggerId2 = (await Protocol2.Debugger.enable()).result.debuggerId;
Protocol1.Debugger.setAsyncCallStackDepth({maxDepth: 32});
Protocol2.Debugger.setAsyncCallStackDepth({maxDepth: 32});
let stackTraceId1 = (await Protocol1.Runtime.evaluate({
expression: 'store(\'stack\')//# sourceURL=expr1-1.js'
})).result.result.value;
let stackTraceId2 = (await Protocol2.Runtime.evaluate({
expression: `started('${stackTraceId1}');
id = store('stack2');
finished('${stackTraceId1}');
id
//# sourceURL=expr2.js`
})).result.result.value;
Protocol1.Runtime.evaluate({
expression: `started('${stackTraceId2}');
debugger;
finished('${stackTraceId2}');
id
//# sourceURL=expr1-2.js`
});
let {params: {callFrames, asyncStackTraceId}} =
await Protocol1.Debugger.oncePaused();
let debuggers = new Map(
[[debuggerId1, Protocol1.Debugger], [debuggerId2, Protocol2.Debugger]]);
let sessions = new Map([[debuggerId1, session1], [debuggerId2, session2]]);
let currentDebuggerId = debuggerId1;
while (true) {
sessions.get(currentDebuggerId).logCallFrames(callFrames);
if (asyncStackTraceId) {
currentDebuggerId = asyncStackTraceId.debuggerId;
let {result: {stackTrace}} =
await debuggers.get(currentDebuggerId).getStackTrace({
stackTraceId: asyncStackTraceId
});
InspectorTest.log(`-- ${stackTrace.description} --`);
callFrames = stackTrace.callFrames;
asyncStackTraceId = stackTrace.parentId;
} else {
break;
}
}
Protocol1.Debugger.disable();
await Protocol2.Debugger.disable();
}
]);