[Lldb-commits] [lldb] 676576b - [lldb/Plugins] Refactor ScriptedThread register context creation

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 10 15:14:32 PST 2021


Author: Med Ismail Bennani
Date: 2021-11-11T00:13:58+01:00
New Revision: 676576b6f027516c8ea1b45f6da3cd1b94a851a2

URL: https://github.com/llvm/llvm-project/commit/676576b6f027516c8ea1b45f6da3cd1b94a851a2
DIFF: https://github.com/llvm/llvm-project/commit/676576b6f027516c8ea1b45f6da3cd1b94a851a2.diff

LOG: [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 <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
    lldb/source/Plugins/Process/scripted/ScriptedProcess.h
    lldb/source/Plugins/Process/scripted/ScriptedThread.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
index 23659bdb8a47..15d3d43d9993 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
+++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
@@ -20,7 +20,6 @@
 #include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Target/MemoryRegionInfo.h"
 #include "lldb/Target/RegisterContext.h"
-
 #include "lldb/Utility/State.h"
 
 #include <mutex>
@@ -291,6 +290,7 @@ bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
   // actually new threads will get added to new_thread_list.
 
   CheckInterpreterAndScriptObject();
+  m_thread_plans.ClearThreadCache();
 
   Status error;
   ScriptLanguage language = m_interpreter->GetLanguage();
@@ -316,6 +316,12 @@ bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
   return new_thread_list.GetSize(false) > 0;
 }
 
+void ScriptedProcess::RefreshStateAfterStop() {
+  // Let all threads recover from stopping and do any clean up based on the
+  // previous thread state (if any).
+  m_thread_list.RefreshStateAfterStop();
+}
+
 bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
   info.Clear();
   info.SetProcessID(GetID());

diff  --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.h b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
index 3f8d53908339..c8355f35548a 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
+++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
@@ -75,7 +75,7 @@ class ScriptedProcess : public Process {
 
   Status DoDestroy() override;
 
-  void RefreshStateAfterStop() override{};
+  void RefreshStateAfterStop() override;
 
   bool IsAlive() override;
 

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<std::string> 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<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
-
-  if (!data_sp->GetByteSize()) {
-    error.SetErrorString("Failed to copy raw registers data.");
-    return;
-  }
-
-  std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
-      std::make_shared<RegisterContextMemory>(
-          *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<RegisterContextThreadMemory>(
-        *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<std::string> reg_data = GetInterface()->GetRegisterContext();
+  if (!reg_data)
+    return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
+        LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers data.",
+        error, LIBLLDB_LOG_THREAD);
+
+  DataBufferSP data_sp(
+      std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
+
+  if (!data_sp->GetByteSize())
+    return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
+        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<RegisterContextMemory> reg_ctx_memory =
+      std::make_shared<RegisterContextMemory>(
+          *this, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
+  if (!reg_ctx_memory)
+    return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
+        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<bool>(
         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<bool>(
         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 {


        


More information about the lldb-commits mailing list