[Lldb-commits] [PATCH] D111827: [lldb/API] Add GetIndexInDebugger to SBTarget

Med Ismail Bennani via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 14 11:47:25 PDT 2021


mib created this revision.
mib added reviewers: jingham, JDevlieghere.
mib added a project: LLDB.
Herald added a subscriber: arphaman.
mib requested review of this revision.
Herald added a subscriber: lldb-commits.

This patch adds a new SBTarget::GetIndexInDebugger method to the SBAPI.

As its name suggests, this can be used to get the target's index in the
debugger's targets list.

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111827

Files:
  lldb/bindings/interface/SBTarget.i
  lldb/include/lldb/API/SBTarget.h
  lldb/source/API/SBTarget.cpp
  lldb/test/API/python_api/target/TestTargetAPI.py


Index: lldb/test/API/python_api/target/TestTargetAPI.py
===================================================================
--- lldb/test/API/python_api/target/TestTargetAPI.py
+++ lldb/test/API/python_api/target/TestTargetAPI.py
@@ -497,3 +497,15 @@
             module = target.GetModuleAtIndex(i)
             self.assertTrue(target.IsLoaded(module), "Running the target should "
                             "have loaded its modules.")
+
+    def test_get_index_in_debugger(self):
+        """Exercise SBTarget.GetIndexInDebugger() API."""
+        self.build()
+        target = self.create_simple_target('a.out')
+
+        self.assertEqual(target.GetIndexInDebugger(), 0)
+        process = target.GetProcess()
+        self.assertNotEqual(process.GetTarget().GetIndexInDebugger(), 0)
+        process = target.LaunchSimple(None, None,
+                                      self.get_process_working_directory())
+        self.assertEqual(process.GetTarget().GetIndexInDebugger(), 0)
Index: lldb/source/API/SBTarget.cpp
===================================================================
--- lldb/source/API/SBTarget.cpp
+++ lldb/source/API/SBTarget.cpp
@@ -183,6 +183,19 @@
   return LLDB_RECORD_RESULT(sb_process);
 }
 
+uint32_t SBTarget::GetIndexInDebugger() const {
+  LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetIndexInDebugger);
+
+  SBDebugger dbg = GetDebugger();
+
+  for (uint32_t i = 0; i < dbg.GetNumTargets(); i++) {
+    if (dbg.GetTargetAtIndex(i) == *this)
+      return i;
+  }
+
+  return UINT32_MAX;
+}
+
 SBPlatform SBTarget::GetPlatform() {
   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBTarget, GetPlatform);
 
@@ -2510,6 +2523,7 @@
   LLDB_REGISTER_METHOD_CONST(bool, SBTarget, IsValid, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBTarget, operator bool, ());
   LLDB_REGISTER_METHOD(lldb::SBProcess, SBTarget, GetProcess, ());
+  LLDB_REGISTER_METHOD_CONST(uint32_t, SBTarget, GetIndexInDebugger, ());
   LLDB_REGISTER_METHOD(lldb::SBPlatform, SBTarget, GetPlatform, ());
   LLDB_REGISTER_METHOD_CONST(lldb::SBDebugger, SBTarget, GetDebugger, ());
   LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBTarget, GetStatistics, ());
Index: lldb/include/lldb/API/SBTarget.h
===================================================================
--- lldb/include/lldb/API/SBTarget.h
+++ lldb/include/lldb/API/SBTarget.h
@@ -66,6 +66,8 @@
 
   lldb::SBProcess GetProcess();
 
+  uint32_t GetIndexInDebugger() const;
+
   /// Sets whether we should collect statistics on lldb or not.
   ///
   /// \param[in] v
Index: lldb/bindings/interface/SBTarget.i
===================================================================
--- lldb/bindings/interface/SBTarget.i
+++ lldb/bindings/interface/SBTarget.i
@@ -96,6 +96,9 @@
     lldb::SBProcess
     GetProcess ();
 
+    uint32_t
+    GetIndexInDebugger () const;
+
 
     %feature("docstring", "
     Return the platform object associated with the target.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111827.379796.patch
Type: text/x-patch
Size: 2932 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211014/96320e8b/attachment.bin>


More information about the lldb-commits mailing list