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.benn...@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.
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.
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits