https://github.com/felipepiovezan updated 
https://github.com/llvm/llvm-project/pull/129275

>From cd6661b5fb7a9a71352c79740d4b0c0601e61d43 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiove...@apple.com>
Date: Fri, 28 Feb 2025 09:11:11 -0800
Subject: [PATCH 1/4] [lldb] Add ability to inspect backing threads with
 `thread info`

When OS plugins are present, it can be helpful to query information
about the backing thread behind an OS thread, if it exists. There is no
mechanism to do so prior to this commit.

As a first step, this commit enhances `thread info` with a
`--backing-thread` flag, causing the command to use the backing thread
of the selected thread, if it exists.
---
 lldb/source/Commands/CommandObjectThread.cpp  |  8 +++++++
 lldb/source/Commands/Options.td               |  3 +++
 .../python_os_plugin/TestPythonOSPlugin.py    | 24 +++++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/lldb/source/Commands/CommandObjectThread.cpp 
b/lldb/source/Commands/CommandObjectThread.cpp
index cd3d2d89333f1..224c523e69c5c 100644
--- a/lldb/source/Commands/CommandObjectThread.cpp
+++ b/lldb/source/Commands/CommandObjectThread.cpp
@@ -1270,6 +1270,7 @@ class CommandObjectThreadInfo : public 
CommandObjectIterateOverThreads {
     void OptionParsingStarting(ExecutionContext *execution_context) override {
       m_json_thread = false;
       m_json_stopinfo = false;
+      m_backing_thread = false;
     }
 
     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
@@ -1286,6 +1287,10 @@ class CommandObjectThreadInfo : public 
CommandObjectIterateOverThreads {
         m_json_stopinfo = true;
         break;
 
+      case 'b':
+        m_backing_thread = true;
+        break;
+
       default:
         llvm_unreachable("Unimplemented option");
       }
@@ -1298,6 +1303,7 @@ class CommandObjectThreadInfo : public 
CommandObjectIterateOverThreads {
 
     bool m_json_thread;
     bool m_json_stopinfo;
+    bool m_backing_thread;
   };
 
   CommandObjectThreadInfo(CommandInterpreter &interpreter)
@@ -1334,6 +1340,8 @@ class CommandObjectThreadInfo : public 
CommandObjectIterateOverThreads {
     }
 
     Thread *thread = thread_sp.get();
+    if (m_options.m_backing_thread && thread->GetBackingThread())
+      thread = thread->GetBackingThread().get();
 
     Stream &strm = result.GetOutputStream();
     if (!thread->GetDescription(strm, eDescriptionLevelFull,
diff --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td
index 8831fed38435b..cc579d767eb06 100644
--- a/lldb/source/Commands/Options.td
+++ b/lldb/source/Commands/Options.td
@@ -1108,6 +1108,9 @@ let Command = "thread info" in {
     " JSON format.">;
   def thread_info_stop_info : Option<"stop-info", "s">, Desc<"Display the "
     "extended stop info in JSON format.">;
+  def thread_info_backing_thread : Option<"backing-thread", "b">,
+    Desc<"If this is an OS plugin thread, query the backing thread instead; 
has"
+    " no effect otherwise.">;
 }
 
 let Command = "thread return" in {
diff --git 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
index fe78edd98f4d4..33e4f75adc0d0 100644
--- 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
+++ 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
@@ -131,6 +131,25 @@ def run_python_os_funcionality(self):
             "Make sure there is no thread 0x333333333 after we unload the 
python OS plug-in",
         )
 
+    tid_regex = re.compile("tid = (0x[0-9a-fA-F]+)")
+    def get_tid_from_thread_info_command(self, thread, use_backing_thread):
+        interp = self.dbg.GetCommandInterpreter()
+        result = lldb.SBCommandReturnObject()
+
+        backing_thread_arg = ""
+        if use_backing_thread:
+            backing_thread_arg = "--backing-thread"
+
+        interp.HandleCommand(
+            "thread info {0} {1}".format(thread.GetIndexID(), 
backing_thread_arg),
+            result,
+            True,
+        )
+        self.assertTrue(result.Succeeded(), "failed to run thread info")
+        match = self.tid_regex.search(result.GetOutput())
+        self.assertNotEqual(match, None)
+        return match.group(1)
+
     def run_python_os_step(self):
         """Test that the Python operating system plugin works correctly and 
allows single stepping of a virtual thread that is backed by a real thread"""
 
@@ -209,6 +228,11 @@ def run_python_os_step(self):
         # it to
         thread.StepOver()
 
+        tid_os = self.get_tid_from_thread_info_command(thread, False)
+        self.assertEqual(tid_os, "0x111111111")
+        tid_real = self.get_tid_from_thread_info_command(thread, True)
+        self.assertNotEqual(tid_os, tid_real)
+
         frame = thread.GetFrameAtIndex(0)
         self.assertTrue(
             frame.IsValid(), "Make sure we get a frame from thread 0x111111111"

>From aab5adf2786b8512ab35cf2532a4821f65f72abb Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiove...@apple.com>
Date: Fri, 28 Feb 2025 09:25:59 -0800
Subject: [PATCH 2/4] fixup! Format python file

---
 .../plugins/python_os_plugin/TestPythonOSPlugin.py               | 1 +
 1 file changed, 1 insertion(+)

diff --git 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
index 33e4f75adc0d0..8cf67642c1eeb 100644
--- 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
+++ 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
@@ -132,6 +132,7 @@ def run_python_os_funcionality(self):
         )
 
     tid_regex = re.compile("tid = (0x[0-9a-fA-F]+)")
+
     def get_tid_from_thread_info_command(self, thread, use_backing_thread):
         interp = self.dbg.GetCommandInterpreter()
         result = lldb.SBCommandReturnObject()

>From 4b3786ae3ce2d9c42a6f179793e97bef029172b5 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiove...@apple.com>
Date: Fri, 28 Feb 2025 13:04:33 -0800
Subject: [PATCH 3/4] fixup! Relax regex to accept decimal numbers

---
 .../plugins/python_os_plugin/TestPythonOSPlugin.py              | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
index 8cf67642c1eeb..4cd0d6422e7c6 100644
--- 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
+++ 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
@@ -131,7 +131,7 @@ def run_python_os_funcionality(self):
             "Make sure there is no thread 0x333333333 after we unload the 
python OS plug-in",
         )
 
-    tid_regex = re.compile("tid = (0x[0-9a-fA-F]+)")
+    tid_regex = re.compile(r"tid = ((0x)?[0-9a-fA-F]+)")
 
     def get_tid_from_thread_info_command(self, thread, use_backing_thread):
         interp = self.dbg.GetCommandInterpreter()

>From 44981cd6016fe151a7cbd59c0642a6b5faca3105 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiove...@apple.com>
Date: Fri, 28 Feb 2025 14:33:40 -0800
Subject: [PATCH 4/4] fixup! Another attempt at making decimal conversion work

---
 .../plugins/python_os_plugin/TestPythonOSPlugin.py            | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
index 4cd0d6422e7c6..e4997f0742d02 100644
--- 
a/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
+++ 
b/lldb/test/API/functionalities/plugins/python_os_plugin/TestPythonOSPlugin.py
@@ -149,7 +149,7 @@ def get_tid_from_thread_info_command(self, thread, 
use_backing_thread):
         self.assertTrue(result.Succeeded(), "failed to run thread info")
         match = self.tid_regex.search(result.GetOutput())
         self.assertNotEqual(match, None)
-        return match.group(1)
+        return int(match.group(1), 0)
 
     def run_python_os_step(self):
         """Test that the Python operating system plugin works correctly and 
allows single stepping of a virtual thread that is backed by a real thread"""
@@ -230,7 +230,7 @@ def run_python_os_step(self):
         thread.StepOver()
 
         tid_os = self.get_tid_from_thread_info_command(thread, False)
-        self.assertEqual(tid_os, "0x111111111")
+        self.assertEqual(tid_os, 0x111111111)
         tid_real = self.get_tid_from_thread_info_command(thread, True)
         self.assertNotEqual(tid_os, tid_real)
 

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to