AsyncStackTrace.cpp [plain text]
#include "config.h"
#include "AsyncStackTrace.h"
#include "ScriptCallStack.h"
namespace Inspector {
RefPtr<AsyncStackTrace> AsyncStackTrace::create(Ref<ScriptCallStack>&& callStack, bool singleShot, RefPtr<AsyncStackTrace> parent)
{
ASSERT(callStack->size());
return adoptRef(*new AsyncStackTrace(WTFMove(callStack), singleShot, WTFMove(parent)));
}
AsyncStackTrace::AsyncStackTrace(Ref<ScriptCallStack>&& callStack, bool singleShot, RefPtr<AsyncStackTrace> parent)
: m_callStack(WTFMove(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->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* previousNode = lastUnlockedAncestor;
auto* sourceNode = lastUnlockedAncestor->m_parent.get();
lastUnlockedAncestor->remove();
while (sourceNode) {
previousNode->m_parent = AsyncStackTrace::create(sourceNode->m_callStack.copyRef(), true, nullptr);
previousNode->m_parent->m_childCount = 1;
previousNode = previousNode->m_parent.get();
if (sourceNode == newStackTraceRoot)
break;
sourceNode = sourceNode->m_parent.get();
}
previousNode->m_truncated = true;
}
void AsyncStackTrace::remove()
{
if (!m_parent)
return;
ASSERT(m_parent->m_childCount);
m_parent->m_childCount--;
m_parent = nullptr;
}
}