mib updated this revision to Diff 404068.
mib added a reviewer: jingham.
mib added a comment.

Address @jingham concerns by making this fully optional:

- Change python default value to 0. Thread indices in lldb are 1-based, so 0 
sounds like a good error_value.
- Check for python returned integer and replace it by `LLDB_INVALID_INDEX32` to 
prevent `ScriptedProcess::RefreshStateAfterStop` to select a thread with it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118482/new/

https://reviews.llvm.org/D118482

Files:
  lldb/examples/python/scripted_process/scripted_process.py
  lldb/include/lldb/Interpreter/ScriptedProcessInterface.h
  lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
  
lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.h
  lldb/test/API/functionalities/scripted_process/stack_core_scripted_process.py

Index: lldb/test/API/functionalities/scripted_process/stack_core_scripted_process.py
===================================================================
--- lldb/test/API/functionalities/scripted_process/stack_core_scripted_process.py
+++ lldb/test/API/functionalities/scripted_process/stack_core_scripted_process.py
@@ -57,6 +57,9 @@
 
         return data
 
+    def get_selected_thread_index(self):
+        return 3
+
     def get_loaded_images(self):
         # TODO: Iterate over corefile_target modules and build a data structure
         # from it.
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.h
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.h
@@ -44,6 +44,8 @@
 
   StructuredData::DictionarySP GetThreadWithID(lldb::tid_t tid) override;
 
+  uint32_t GetSelectedThreadIndex() override;
+
   StructuredData::DictionarySP GetRegistersForThread(lldb::tid_t tid) override;
 
   lldb::DataExtractorSP ReadMemoryAtAddress(lldb::addr_t address, size_t size,
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
@@ -113,6 +113,21 @@
   return dict;
 }
 
+uint32_t ScriptedProcessPythonInterface::GetSelectedThreadIndex() {
+  Status error;
+  StructuredData::ObjectSP obj = Dispatch("get_selected_thread_index", error);
+
+  if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
+    return LLDB_INVALID_INDEX32;
+
+  uint64_t val = obj->GetIntegerValue(LLDB_INVALID_INDEX32);
+
+  if (!val || std::numeric_limits<uint32_t>::max() <= val)
+    return LLDB_INVALID_INDEX32;
+
+  return static_cast<uint32_t>(val);
+}
+
 StructuredData::DictionarySP
 ScriptedProcessPythonInterface::GetRegistersForThread(lldb::tid_t tid) {
   // TODO: Implement
Index: lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
===================================================================
--- lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
+++ lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
@@ -359,6 +359,11 @@
 void ScriptedProcess::RefreshStateAfterStop() {
   // Let all threads recover from stopping and do any clean up based on the
   // previous thread state (if any).
+  uint32_t selected_thread_index = GetInterface().GetSelectedThreadIndex();
+
+  if (selected_thread_index != LLDB_INVALID_INDEX32 &&
+      selected_thread_index <= m_thread_list.GetSize())
+    m_thread_list.SetSelectedThreadByIndexID(selected_thread_index);
 }
 
 bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
Index: lldb/include/lldb/Interpreter/ScriptedProcessInterface.h
===================================================================
--- lldb/include/lldb/Interpreter/ScriptedProcessInterface.h
+++ lldb/include/lldb/Interpreter/ScriptedProcessInterface.h
@@ -48,6 +48,8 @@
     return nullptr;
   }
 
+  virtual uint32_t GetSelectedThreadIndex() { return LLDB_INVALID_INDEX32; }
+
   virtual StructuredData::DictionarySP GetRegistersForThread(lldb::tid_t tid) {
     return nullptr;
   }
Index: lldb/examples/python/scripted_process/scripted_process.py
===================================================================
--- lldb/examples/python/scripted_process/scripted_process.py
+++ lldb/examples/python/scripted_process/scripted_process.py
@@ -76,6 +76,15 @@
         """
         pass
 
+    def get_selected_thread_index(self):
+        """ Get the index of the selected scripted process thread.
+
+        Returns:
+            Int: The thread index that should be selected by lldb.
+                 (default: 0)
+        """
+        return 0
+
     @abstractmethod
     def get_registers_for_thread(self, tid):
         """ Get the register context dictionary for a certain thread of
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to