diff --git a/lldb/bindings/interface/SBTarget.i b/lldb/bindings/interface/SBTarget.i --- a/lldb/bindings/interface/SBTarget.i +++ b/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. diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h --- a/lldb/include/lldb/API/SBTarget.h +++ b/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 diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp --- a/lldb/source/API/SBTarget.cpp +++ b/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, ()); diff --git a/lldb/test/API/python_api/target/TestTargetAPI.py b/lldb/test/API/python_api/target/TestTargetAPI.py --- a/lldb/test/API/python_api/target/TestTargetAPI.py +++ b/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)