HRESULT ThreadController::PopulateCallStack()
{
    ATLENSURE_RETURN_HR(ThreadHelpers::IsOnPDMThread(m_dispatchThreadId), E_UNEXPECTED);
    ATLENSURE_RETURN_HR(this->IsAtBreak(), E_NOT_VALID_STATE);

    // Lock the access to the callstack so that we are thread safe
    CComCritSecLock<CComAutoCriticalSection> lock(m_csCallFramesLock);

    CComPtr<IEnumDebugStackFrames> pStackFrameEnum;
    HRESULT hr = m_spCurrentBrokenThread->EnumStackFrames(&pStackFrameEnum);
    if (hr == S_OK)
    {
        if ((hr = pStackFrameEnum->Reset()) != S_OK)
        {
            return hr;
        }

        shared_ptr<DebugStackFrameDescriptor> spFrameDescriptor(new DebugStackFrameDescriptor());
        ULONG nFetched;
        for (ULONG ulIndex = 0; (pStackFrameEnum->Next(1, spFrameDescriptor.get(), &nFetched) == S_OK) && (nFetched == 1); ulIndex++)
        {
            // Get a new identifier for this source node
            ULONG newFrameId = ThreadController::CreateUniqueFrameId();
            ULONG newPropertyId = ThreadController::CreateUniquePropertyId();

            // Create object that represents the source file
            CComObject<CallFrame>* pFrame;
            hr = CComObject<CallFrame>::CreateInstance(&pFrame);
            BPT_FAIL_IF_NOT_S_OK(hr);

            hr = pFrame->Initialize(m_dispatchThreadId, newFrameId, spFrameDescriptor, m_spSourceController);
            BPT_FAIL_IF_NOT_S_OK(hr);

            // Add to our maps
            m_callFrameMap[newFrameId] = pFrame;
            m_callFrames.push_back(newFrameId);

            // Get the information about this call frame
            shared_ptr<CallFrameInfo> spFrameInfo;
            CComPtr<IDebugStackFrame> spStackFrame;
            CComPtr<IDebugProperty> spLocalsDebugProperty;
            hr = pFrame->GetCallFrameInfo(spFrameInfo, spStackFrame, spLocalsDebugProperty);
            BPT_FAIL_IF_NOT_S_OK(hr);

            // Store that info in our maps
            m_callFramesInfoMap[newFrameId] = spFrameInfo;
            m_debugFrameMap[newFrameId] = spStackFrame;
            m_propertyMap[newPropertyId] = spLocalsDebugProperty;

            // Link the locals to the frame id for later lookup
            m_localsMap[newFrameId] = newPropertyId;
        }
    }

    return S_OK;
}