AsyncStackTrace.cpp [plain text]
#include "config.h"
#include "AsyncStackTrace.h"
#include "InspectorValues.h"
#include "ScriptCallStack.h"
namespace Inspector {
RefPtr<AsyncStackTrace> AsyncStackTrace::create(RefPtr<ScriptCallStack> callStack, bool singleShot, RefPtr<AsyncStackTrace> parent)
{
ASSERT(callStack && callStack->size());
return adoptRef(*new AsyncStackTrace(WTFMove(callStack), singleShot, WTFMove(parent)));
}
AsyncStackTrace::AsyncStackTrace(RefPtr<ScriptCallStack> callStack, bool singleShot, RefPtr<AsyncStackTrace> parent)
: m_callStack(callStack)
, m_parent(parent)
, m_singleShot(singleShot)
{
if (m_parent)
m_parent->m_childCount++;
}
AsyncStackTrace::~AsyncStackTrace()
{
if (m_parent)
remove();
ASSERT(!m_childCount);
}
bool AsyncStackTrace::isPending() const
{
return m_state == State::Pending;
}
bool AsyncStackTrace::isLocked() const
{
return m_state == State::Pending || m_state == State::Active || m_childCount > 1;
}
void AsyncStackTrace::willDispatchAsyncCall(size_t maxDepth)
{
ASSERT(m_state == State::Pending);
m_state = State::Active;
truncate(maxDepth);
}
void AsyncStackTrace::didDispatchAsyncCall()
{
ASSERT(m_state == State::Active || m_state == State::Canceled);
if (m_state == State::Active && !m_singleShot) {
m_state = State::Pending;
return;
}
m_state = State::Dispatched;
if (!m_childCount)
remove();
}
void AsyncStackTrace::didCancelAsyncCall()
{
if (m_state == State::Canceled)
return;
if (m_state == State::Pending && !m_childCount)
remove();
m_state = State::Canceled;
}
RefPtr<Inspector::Protocol::Console::StackTrace> AsyncStackTrace::buildInspectorObject() const
{
RefPtr<Inspector::Protocol::Console::StackTrace> topStackTrace;
RefPtr<Inspector::Protocol::Console::StackTrace> previousStackTrace;
auto* stackTrace = this;
while (stackTrace) {
auto callStack = stackTrace->m_callStack;
ASSERT(callStack && callStack->size());
RefPtr<Inspector::Protocol::Console::StackTrace> protocolObject = Inspector::Protocol::Console::StackTrace::create()
.setCallFrames(callStack->buildInspectorArray())
.release();
if (stackTrace->m_truncated)
protocolObject->setTruncated(true);
if (callStack->at(0).isNative())
protocolObject->setTopCallFrameIsBoundary(true);
if (!topStackTrace)
topStackTrace = protocolObject;
if (previousStackTrace)
previousStackTrace->setParentStackTrace(protocolObject);
previousStackTrace = protocolObject;
stackTrace = stackTrace->m_parent.get();
}
return topStackTrace;
}
void AsyncStackTrace::truncate(size_t maxDepth)
{
AsyncStackTrace* lastUnlockedAncestor = nullptr;
size_t depth = 0;
auto* newStackTraceRoot = this;
while (newStackTraceRoot) {
depth += newStackTraceRoot->m_callStack->size();
if (depth >= maxDepth)
break;
auto* parent = newStackTraceRoot->m_parent.get();
if (!lastUnlockedAncestor && parent && parent->isLocked())
lastUnlockedAncestor = newStackTraceRoot;
newStackTraceRoot = parent;
}
if (!newStackTraceRoot || !newStackTraceRoot->m_parent)
return;
if (!lastUnlockedAncestor) {
newStackTraceRoot->m_truncated = true;
newStackTraceRoot->remove();
return;
}
auto* currentNode = lastUnlockedAncestor;
while (currentNode->m_parent) {
auto& parentNode = currentNode->m_parent;
currentNode->m_parent = AsyncStackTrace::create(parentNode->m_callStack, true, parentNode->m_parent);
currentNode = currentNode->m_parent.get();
if (parentNode.get() == newStackTraceRoot)
break;
}
currentNode->m_truncated = true;
currentNode->remove();
auto& firstLockedAncestor = lastUnlockedAncestor->m_parent;
firstLockedAncestor->m_childCount--;
}
void AsyncStackTrace::remove()
{
if (!m_parent)
return;
ASSERT(m_parent->m_childCount);
m_parent->m_childCount--;
m_parent = nullptr;
}
}