From 676576b6f027516c8ea1b45f6da3cd1b94a851a2 Mon Sep 17 00:00:00 2001 From: Med Ismail Bennani Date: Wed, 10 Nov 2021 19:53:14 +0000 Subject: [lldb/Plugins] Refactor ScriptedThread register context creation This patch changes the ScriptedThread class to create the register context when Process::RefreshStateAfterStop is called rather than doing it in the thread constructor. This is required to update the thread state for execution control. Differential Revision: https://reviews.llvm.org/D112167 Signed-off-by: Med Ismail Bennani --- .../Plugins/Process/scripted/ScriptedThread.cpp | 83 +++++++++++----------- 1 file changed, 42 insertions(+), 41 deletions(-) (limited to 'lldb/source/Plugins/Process/scripted/ScriptedThread.cpp') diff --git a/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp b/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp index dbe9e5019ff8..1adbd4e7799d 100644 --- a/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp +++ b/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp @@ -64,32 +64,6 @@ ScriptedThread::ScriptedThread(ScriptedProcess &process, Status &error) m_script_object_sp = object_sp; SetID(scripted_thread_interface->GetThreadID()); - - llvm::Optional reg_data = - scripted_thread_interface->GetRegisterContext(); - if (!reg_data) { - error.SetErrorString("Failed to get scripted thread registers data."); - return; - } - - DataBufferSP data_sp( - std::make_shared(reg_data->c_str(), reg_data->size())); - - if (!data_sp->GetByteSize()) { - error.SetErrorString("Failed to copy raw registers data."); - return; - } - - std::shared_ptr reg_ctx_memory = - std::make_shared( - *this, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); - if (!reg_ctx_memory) { - error.SetErrorString("Failed to create a register context."); - return; - } - - reg_ctx_memory->SetAllRegisterData(data_sp); - m_reg_context_sp = reg_ctx_memory; } ScriptedThread::~ScriptedThread() { DestroyThread(); } @@ -115,21 +89,48 @@ void ScriptedThread::WillResume(StateType resume_state) {} void ScriptedThread::ClearStackFrames() { Thread::ClearStackFrames(); } RegisterContextSP ScriptedThread::GetRegisterContext() { - if (!m_reg_context_sp) { - m_reg_context_sp = std::make_shared( - *this, LLDB_INVALID_ADDRESS); - GetInterface()->GetRegisterContext(); - } + if (!m_reg_context_sp) + m_reg_context_sp = CreateRegisterContextForFrame(nullptr); return m_reg_context_sp; } RegisterContextSP ScriptedThread::CreateRegisterContextForFrame(StackFrame *frame) { - uint32_t concrete_frame_idx = frame ? frame->GetConcreteFrameIndex() : 0; + const uint32_t concrete_frame_idx = + frame ? frame->GetConcreteFrameIndex() : 0; + + if (concrete_frame_idx) + return GetUnwinder().CreateRegisterContextForFrame(frame); + + lldb::RegisterContextSP reg_ctx_sp; + Status error; + + llvm::Optional reg_data = GetInterface()->GetRegisterContext(); + if (!reg_data) + return GetInterface()->ErrorWithMessage( + LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers data.", + error, LIBLLDB_LOG_THREAD); + + DataBufferSP data_sp( + std::make_shared(reg_data->c_str(), reg_data->size())); + + if (!data_sp->GetByteSize()) + return GetInterface()->ErrorWithMessage( + LLVM_PRETTY_FUNCTION, "Failed to copy raw registers data.", error, + LIBLLDB_LOG_THREAD); - if (concrete_frame_idx == 0) - return GetRegisterContext(); - return GetUnwinder().CreateRegisterContextForFrame(frame); + std::shared_ptr reg_ctx_memory = + std::make_shared( + *this, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); + if (!reg_ctx_memory) + return GetInterface()->ErrorWithMessage( + LLVM_PRETTY_FUNCTION, "Failed to create a register context.", error, + LIBLLDB_LOG_THREAD); + + reg_ctx_memory->SetAllRegisterData(data_sp); + m_reg_context_sp = reg_ctx_memory; + + return m_reg_context_sp; } bool ScriptedThread::CalculateStopInfo() { @@ -142,13 +143,15 @@ bool ScriptedThread::CalculateStopInfo() { if (!dict_sp->GetValueForKeyAsInteger("type", stop_reason_type)) return GetInterface()->ErrorWithMessage( LLVM_PRETTY_FUNCTION, - "Couldn't find value for key 'type' in stop reason dictionary.", error); + "Couldn't find value for key 'type' in stop reason dictionary.", error, + LIBLLDB_LOG_THREAD); StructuredData::Dictionary *data_dict; if (!dict_sp->GetValueForKeyAsDictionary("data", data_dict)) return GetInterface()->ErrorWithMessage( LLVM_PRETTY_FUNCTION, - "Couldn't find value for key 'type' in stop reason dictionary.", error); + "Couldn't find value for key 'type' in stop reason dictionary.", error, + LIBLLDB_LOG_THREAD); switch (stop_reason_type) { case lldb::eStopReasonNone: @@ -175,7 +178,7 @@ bool ScriptedThread::CalculateStopInfo() { llvm::Twine("Unsupported stop reason type (" + llvm::Twine(stop_reason_type) + llvm::Twine(").")) .str(), - error); + error, LIBLLDB_LOG_THREAD); } SetStopInfo(stop_info_sp); @@ -183,9 +186,7 @@ bool ScriptedThread::CalculateStopInfo() { } void ScriptedThread::RefreshStateAfterStop() { - // TODO: Implement - if (m_reg_context_sp) - m_reg_context_sp->InvalidateAllRegisters(); + GetRegisterContext()->InvalidateIfNeeded(/*force=*/false); } lldb::ScriptedThreadInterfaceSP ScriptedThread::GetInterface() const { -- cgit v1.2.3