[inspector] better stacks for promises

- we should always set creation async stack if it's available regardless existing of current parent async stack,
- we should cleanup parent link iff there is no creation and schedule async stack for parent.

Let's consider example: Promise.resolve().then(x => x).then(x => x), there is three promises which will call following instrumentation:
1) created #1 (Promise.resolve()) - collected stack #1
2) scheduled #1 - collected stack #2
3) created #2 with #1 as parent (first .then) - collected stack #3
4) created #3 with #2 as parent (first .then) - collected stack #4
5) started #2 - use stack #2 as scheduled
6) scheduled #2 - collected stack #6
7) finished #2
8) started #3 - use stack #6 as scheduled
9) scheduled #3 - collected stack #7
10) finished #3

If we collect stacks between step 4 and 5, it's possible to collect scheduled stack #2 but still have creation stack for #2 - stack #3 - so we always need to add creation event if scheduled is collected.

If we collect stacks between created and scheduled we should not remove parent link even if parent was not scheduled yet.

BUG=v8:6189
R=dgozman@chromium.org

Review-Url: https://codereview.chromium.org/2844753002
Cr-Commit-Position: refs/heads/master@{#44990}
This commit is contained in:
kozyatinskiy 2017-04-28 14:07:01 -07:00 committed by Commit bot
parent 6408032e61
commit f2bd913cd4
6 changed files with 375 additions and 15 deletions

View File

@ -913,10 +913,6 @@ void V8Debugger::asyncTaskCanceledForStack(void* task) {
void V8Debugger::asyncTaskStartedForStack(void* task) {
if (!m_maxAsyncCallStackDepth) return;
m_currentTasks.push_back(task);
auto parentIt = m_parentTask.find(task);
AsyncTaskToStackTrace::iterator stackIt = m_asyncTaskStacks.find(
parentIt == m_parentTask.end() ? task : parentIt->second);
// Needs to support following order of events:
// - asyncTaskScheduled
// <-- attached here -->
@ -924,15 +920,21 @@ void V8Debugger::asyncTaskStartedForStack(void* task) {
// - asyncTaskCanceled <-- canceled before finished
// <-- async stack requested here -->
// - asyncTaskFinished
std::weak_ptr<AsyncStackTrace> asyncParent;
if (stackIt != m_asyncTaskStacks.end()) asyncParent = stackIt->second;
m_currentTasks.push_back(task);
auto parentIt = m_parentTask.find(task);
AsyncTaskToStackTrace::iterator stackIt = m_asyncTaskStacks.find(
parentIt == m_parentTask.end() ? task : parentIt->second);
if (stackIt != m_asyncTaskStacks.end()) {
m_currentAsyncParent.push_back(stackIt->second.lock());
} else {
m_currentAsyncParent.emplace_back();
}
auto itCreation = m_asyncTaskCreationStacks.find(task);
if (asyncParent.lock() && itCreation != m_asyncTaskCreationStacks.end()) {
if (itCreation != m_asyncTaskCreationStacks.end()) {
m_currentAsyncCreation.push_back(itCreation->second.lock());
} else {
m_currentAsyncCreation.emplace_back();
}
m_currentAsyncParent.push_back(asyncParent.lock());
}
void V8Debugger::asyncTaskFinishedForStack(void* task) {
@ -1041,7 +1043,8 @@ void V8Debugger::collectOldAsyncStacksIfNeeded() {
}
for (auto it = m_parentTask.begin(); it != m_parentTask.end();) {
if (m_asyncTaskCreationStacks.find(it->second) ==
m_asyncTaskCreationStacks.end()) {
m_asyncTaskCreationStacks.end() &&
m_asyncTaskStacks.find(it->second) == m_asyncTaskStacks.end()) {
it = m_parentTask.erase(it);
} else {
++it;

View File

@ -63,8 +63,14 @@ void calculateAsyncChain(V8Debugger* debugger, int contextGroupId,
std::unique_ptr<protocol::Runtime::StackTrace> buildInspectorObjectCommon(
const std::vector<std::shared_ptr<StackFrame>>& frames,
const String16& description,
const std::shared_ptr<AsyncStackTrace>& asyncParent,
const std::shared_ptr<AsyncStackTrace>& asyncCreation, int maxAsyncDepth) {
if (asyncParent && frames.empty() &&
description == asyncParent->description() && !asyncCreation) {
return asyncParent->buildInspectorObject(nullptr, maxAsyncDepth);
}
std::unique_ptr<protocol::Array<protocol::Runtime::CallFrame>>
inspectorFrames = protocol::Array<protocol::Runtime::CallFrame>::create();
for (size_t i = 0; i < frames.size(); i++) {
@ -74,6 +80,7 @@ std::unique_ptr<protocol::Runtime::StackTrace> buildInspectorObjectCommon(
protocol::Runtime::StackTrace::create()
.setCallFrames(std::move(inspectorFrames))
.build();
if (!description.isEmpty()) stackTrace->setDescription(description);
if (asyncParent && maxAsyncDepth > 0) {
stackTrace->setParent(asyncParent->buildInspectorObject(asyncCreation.get(),
maxAsyncDepth - 1));
@ -206,7 +213,7 @@ StringView V8StackTraceImpl::topFunctionName() const {
std::unique_ptr<protocol::Runtime::StackTrace>
V8StackTraceImpl::buildInspectorObjectImpl() const {
return buildInspectorObjectCommon(m_frames, m_asyncParent.lock(),
return buildInspectorObjectCommon(m_frames, String16(), m_asyncParent.lock(),
m_asyncCreation.lock(), m_maxAsyncDepth);
}
@ -292,9 +299,8 @@ std::unique_ptr<protocol::Runtime::StackTrace>
AsyncStackTrace::buildInspectorObject(AsyncStackTrace* asyncCreation,
int maxAsyncDepth) const {
std::unique_ptr<protocol::Runtime::StackTrace> stackTrace =
buildInspectorObjectCommon(m_frames, m_asyncParent.lock(),
buildInspectorObjectCommon(m_frames, m_description, m_asyncParent.lock(),
m_asyncCreation.lock(), maxAsyncDepth);
if (!m_description.isEmpty()) stackTrace->setDescription(m_description);
if (asyncCreation && !asyncCreation->isEmpty()) {
stackTrace->setPromiseCreationFrame(
asyncCreation->m_frames[0]->buildInspectorObject());
@ -304,6 +310,8 @@ AsyncStackTrace::buildInspectorObject(AsyncStackTrace* asyncCreation,
int AsyncStackTrace::contextGroupId() const { return m_contextGroupId; }
const String16& AsyncStackTrace::description() const { return m_description; }
std::weak_ptr<AsyncStackTrace> AsyncStackTrace::parent() const {
return m_asyncParent;
}

View File

@ -97,6 +97,7 @@ class AsyncStackTrace {
AsyncStackTrace* asyncCreation, int maxAsyncDepth) const;
int contextGroupId() const;
const String16& description() const;
std::weak_ptr<AsyncStackTrace> parent() const;
std::weak_ptr<AsyncStackTrace> creation() const;
bool isEmpty() const;

View File

@ -95,7 +95,7 @@ actual async chain len: 1
inspector.setMaxAsyncTaskStacks(3)
Run expression 'console.trace(42)' with async chain len: 2
actual async chain len: 0
actual async chain len: 1
inspector.setMaxAsyncTaskStacks(3)
Run expression 'console.trace(42)' with async chain len: 3
@ -123,7 +123,7 @@ actual async chain len: 1
inspector.setMaxAsyncTaskStacks(4)
Run expression 'console.trace(42)' with async chain len: 3
actual async chain len: 0
actual async chain len: 1
inspector.setMaxAsyncTaskStacks(4)
Run expression 'console.trace(42)' with async chain len: 1
@ -171,7 +171,7 @@ actual async chain len: 2
inspector.setMaxAsyncTaskStacks(6)
Run expression 'console.trace(42)' with async chain len: 3
actual async chain len: 2
actual async chain len: 1
inspector.setMaxAsyncTaskStacks(6)
Run expression 'console.trace(42)' with async chain len: 1

View File

@ -0,0 +1,297 @@
Checks correctness of promise chains when limit hit
inspector.setMaxAsyncTaskStacks(3)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
}
timestamp : <timestamp>
type : trace
}
}
inspector.setMaxAsyncTaskStacks(4)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
parent : {
callFrames : [
]
description : Promise.resolve
promiseCreationFrame : {
columnNumber : 46
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
}
timestamp : <timestamp>
type : trace
}
}
inspector.setMaxAsyncTaskStacks(5)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
parent : {
callFrames : [
]
description : Promise.resolve
parent : {
callFrames : [
]
description : Promise.resolve
promiseCreationFrame : {
columnNumber : 32
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
promiseCreationFrame : {
columnNumber : 46
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
}
timestamp : <timestamp>
type : trace
}
}
inspector.setMaxAsyncTaskStacks(6)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
parent : {
callFrames : [
]
description : Promise.resolve
promiseCreationFrame : {
columnNumber : 46
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
}
timestamp : <timestamp>
type : trace
}
}
inspector.setMaxAsyncTaskStacks(7)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
parent : {
callFrames : [
]
description : Promise.resolve
parent : {
callFrames : [
]
description : Promise.resolve
parent : {
callFrames : [
[0] : {
columnNumber : 8
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
]
description : Promise.resolve
promiseCreationFrame : {
columnNumber : 18
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
promiseCreationFrame : {
columnNumber : 32
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
promiseCreationFrame : {
columnNumber : 46
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
}
timestamp : <timestamp>
type : trace
}
}
inspector.setMaxAsyncTaskStacks(8)
Run expression 'console.trace()' with async chain len: 3
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : console.trace
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 67
functionName : Promise.resolve.then.then.then
lineNumber : 0
scriptId : <scriptId>
url :
}
]
parent : {
callFrames : [
]
description : Promise.resolve
parent : {
callFrames : [
]
description : Promise.resolve
parent : {
callFrames : [
[0] : {
columnNumber : 8
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
]
description : Promise.resolve
promiseCreationFrame : {
columnNumber : 18
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
promiseCreationFrame : {
columnNumber : 32
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
promiseCreationFrame : {
columnNumber : 46
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
}
}
timestamp : <timestamp>
type : trace
}
}

View File

@ -0,0 +1,51 @@
// 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.
(async function test(){
InspectorTest.log('Checks correctness of promise chains when limit hit');
await Protocol.Runtime.enable();
await Protocol.Debugger.enable();
Protocol.Debugger.setAsyncCallStackDepth({maxDepth: 128});
await setMaxAsyncTaskStacks(3);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
await setMaxAsyncTaskStacks(4);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
await setMaxAsyncTaskStacks(5);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
await setMaxAsyncTaskStacks(6);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
await setMaxAsyncTaskStacks(7);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
await setMaxAsyncTaskStacks(8);
runWithAsyncChainPromise(3, 'console.trace()');
InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
InspectorTest.completeTest();
})();
function runWithAsyncChainPromise(len, source) {
InspectorTest.log(`Run expression '${source}' with async chain len: ${len}`);
let then = '.then(() => 1)';
let pause = `.then(() => { ${source} })`;
Protocol.Runtime.evaluate({
expression: `Promise.resolve()${then.repeat(len - 1)}${pause}`
});
}
async function setMaxAsyncTaskStacks(max) {
let expression = `inspector.setMaxAsyncTaskStacks(${max})`;
InspectorTest.log(expression);
await Protocol.Runtime.evaluate({expression});
}