Skip to content

Commit

Permalink
[inspector] better stacks for promises
Browse files Browse the repository at this point in the history
- 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 v8#6
7) finished #2
8) started #3 - use stack v8#6 as scheduled
9) scheduled #3 - collected stack v8#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}
  • Loading branch information
alexkozy authored and Commit bot committed Apr 28, 2017
1 parent 6408032 commit f2bd913
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 15 deletions.
21 changes: 12 additions & 9 deletions src/inspector/v8-debugger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -913,26 +913,28 @@ 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 -->
// - asyncTaskStarted
// - 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) {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions src/inspector/v8-stack-trace-impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/inspector/v8-stack-trace-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit f2bd913

Please sign in to comment.