Index: lldb/bindings/interface/SBType.i =================================================================== --- lldb/bindings/interface/SBType.i +++ lldb/bindings/interface/SBType.i @@ -277,6 +277,9 @@ lldb::SBTypeEnumMemberList GetEnumMembers(); + lldb::SBModule + GetModule(); + const char* GetName(); @@ -330,6 +333,7 @@ return template_args return None + module = property(GetModule, None, doc='''A read only property that returns the module in which type is defined.''') name = property(GetName, None, doc='''A read only property that returns the name for this type as a string.''') size = property(GetByteSize, None, doc='''A read only property that returns size in bytes for this type as an integer.''') is_pointer = property(IsPointerType, None, doc='''A read only property that returns a boolean value that indicates if this type is a pointer type.''') Index: lldb/include/lldb/API/SBModule.h =================================================================== --- lldb/include/lldb/API/SBModule.h +++ lldb/include/lldb/API/SBModule.h @@ -300,6 +300,7 @@ friend class SBSection; friend class SBSymbolContext; friend class SBTarget; + friend class SBType; explicit SBModule(const lldb::ModuleSP &module_sp); Index: lldb/include/lldb/API/SBType.h =================================================================== --- lldb/include/lldb/API/SBType.h +++ lldb/include/lldb/API/SBType.h @@ -185,6 +185,8 @@ lldb::SBTypeMemberFunction GetMemberFunctionAtIndex(uint32_t idx); + lldb::SBModule GetModule(); + const char *GetName(); const char *GetDisplayTypeName(); Index: lldb/include/lldb/Symbol/Type.h =================================================================== --- lldb/include/lldb/Symbol/Type.h +++ lldb/include/lldb/Symbol/Type.h @@ -114,6 +114,12 @@ // Type instances. They can store a weak pointer to the Module; lldb::ModuleSP GetModule(); + // GetModule may return module for compile unit's object file. + // GetExeModule returns module for executable object file that contains + // compile unit where type was actualy defined. + // GetModule and GetExeModule may return the same value. + lldb::ModuleSP GetExeModule(); + void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name, ExecutionContextScope *exe_scope); @@ -260,6 +266,8 @@ void Clear(); + lldb::ModuleSP GetModule(); + ConstString GetName() const; ConstString GetDisplayTypeName() const; @@ -287,8 +295,12 @@ private: bool CheckModule(lldb::ModuleSP &module_sp) const; + bool CheckExeModule(lldb::ModuleSP &module_sp) const; + bool CheckModuleCommon(const lldb::ModuleWP &input_module_wp, + lldb::ModuleSP &module_sp) const; lldb::ModuleWP m_module_wp; + lldb::ModuleWP m_exe_module_wp; CompilerType m_static_type; CompilerType m_dynamic_type; }; Index: lldb/source/API/SBType.cpp =================================================================== --- lldb/source/API/SBType.cpp +++ lldb/source/API/SBType.cpp @@ -9,6 +9,7 @@ #include "lldb/API/SBType.h" #include "SBReproducerPrivate.h" #include "lldb/API/SBDefines.h" +#include "lldb/API/SBModule.h" #include "lldb/API/SBStream.h" #include "lldb/API/SBTypeEnumMember.h" #include "lldb/Core/Mangled.h" @@ -495,6 +496,17 @@ return m_opaque_sp->GetCompilerType(true).GetTypeInfo(); } +lldb::SBModule SBType::GetModule() { + LLDB_RECORD_METHOD_NO_ARGS(lldb::SBModule, SBType, GetModule); + + lldb::SBModule sb_module; + if (!IsValid()) + return LLDB_RECORD_RESULT(sb_module); + + sb_module.SetSP(m_opaque_sp->GetModule()); + return LLDB_RECORD_RESULT(sb_module); +} + const char *SBType::GetName() { LLDB_RECORD_METHOD_NO_ARGS(const char *, SBType, GetName); @@ -950,6 +962,7 @@ (uint32_t)); LLDB_REGISTER_METHOD(bool, SBType, IsTypeComplete, ()); LLDB_REGISTER_METHOD(uint32_t, SBType, GetTypeFlags, ()); + LLDB_REGISTER_METHOD(lldb::SBModule, SBType, GetModule, ()); LLDB_REGISTER_METHOD(const char *, SBType, GetName, ()); LLDB_REGISTER_METHOD(const char *, SBType, GetDisplayTypeName, ()); LLDB_REGISTER_METHOD(lldb::TypeClass, SBType, GetTypeClass, ()); Index: lldb/source/Symbol/Type.cpp =================================================================== --- lldb/source/Symbol/Type.cpp +++ lldb/source/Symbol/Type.cpp @@ -727,6 +727,14 @@ return ModuleSP(); } +ModuleSP Type::GetExeModule() { + if (m_compiler_type) { + SymbolFile *symbol_file = m_compiler_type.GetTypeSystem()->GetSymbolFile(); + return symbol_file->GetObjectFile()->GetModule(); + } + return ModuleSP(); +} + TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) { if (in_type_sp) { m_compiler_type = in_type_sp->GetForwardCompilerType(); @@ -821,6 +829,7 @@ void TypeImpl::SetType(const lldb::TypeSP &type_sp) { if (type_sp) { m_static_type = type_sp->GetForwardCompilerType(); + m_exe_module_wp = type_sp->GetExeModule(); m_module_wp = type_sp->GetModule(); } else { m_static_type.Clear(); @@ -847,6 +856,15 @@ } bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { + return CheckModuleCommon(m_module_wp, module_sp); +} + +bool TypeImpl::CheckExeModule(lldb::ModuleSP &module_sp) const { + return CheckModuleCommon(m_exe_module_wp, module_sp); +} + +bool TypeImpl::CheckModuleCommon(const lldb::ModuleWP &input_module_wp, + lldb::ModuleSP &module_sp) const { // Check if we have a module for this type. If we do and the shared pointer // is can be successfully initialized with m_module_wp, return true. Else // return false if we didn't have a module, or if we had a module and it has @@ -855,7 +873,7 @@ // this function returns true. If we have a module, the "module_sp" will be // filled in with a strong reference to the module so that the module will at // least stay around long enough for the type query to succeed. - module_sp = m_module_wp.lock(); + module_sp = input_module_wp.lock(); if (!module_sp) { lldb::ModuleWP empty_module_wp; // If either call to "std::weak_ptr::owner_before(...) value returns true, @@ -863,9 +881,9 @@ // reference to a valid shared pointer. This helps us know if we had a // valid reference to a section which is now invalid because the module it // was in was deleted - if (empty_module_wp.owner_before(m_module_wp) || - m_module_wp.owner_before(empty_module_wp)) { - // m_module_wp had a valid reference to a module, but all strong + if (empty_module_wp.owner_before(input_module_wp) || + input_module_wp.owner_before(empty_module_wp)) { + // input_module_wp had a valid reference to a module, but all strong // references have been released and the module has been deleted return false; } @@ -899,6 +917,13 @@ m_dynamic_type.Clear(); } +ModuleSP TypeImpl::GetModule() { + lldb::ModuleSP module_sp; + if (CheckExeModule(module_sp)) + return module_sp; + return nullptr; +} + ConstString TypeImpl::GetName() const { ModuleSP module_sp; if (CheckModule(module_sp)) { Index: lldb/test/API/functionalities/type_get_module/Makefile =================================================================== --- /dev/null +++ lldb/test/API/functionalities/type_get_module/Makefile @@ -0,0 +1,3 @@ + +C_SOURCES := main.c compile_unit1.c compile_unit2.c +include Makefile.rules Index: lldb/test/API/functionalities/type_get_module/TestTypeGetModule.py =================================================================== --- /dev/null +++ lldb/test/API/functionalities/type_get_module/TestTypeGetModule.py @@ -0,0 +1,42 @@ +""" +Test that SBType returns SBModule of executable file but not +of compile unit's object file. +""" + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +import lldbsuite.test.lldbutil as lldbutil + + +class TestTypeGetModule(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + def test(self): + self.build() + target = lldbutil.run_to_breakpoint_make_target(self) + exe_module = target.GetModuleAtIndex(0) + + type1_name = 'compile_unit1_type' + type2_name = 'compile_unit2_type' + + num_comp_units = exe_module.GetNumCompileUnits() + self.assertEqual(num_comp_units, 3) + + comp_unit = exe_module.GetCompileUnitAtIndex(1) + type_name = comp_unit.GetTypes().GetTypeAtIndex(0).GetName() + self.assertEqual(type_name, type1_name) + + comp_unit = exe_module.GetCompileUnitAtIndex(2) + type_name = comp_unit.GetTypes().GetTypeAtIndex(0).GetName() + self.assertEqual(type_name, type2_name) + + type1 = target.FindFirstType(type1_name) + self.assertTrue(type1.IsValid()) + + type2 = target.FindFirstType(type2_name) + self.assertTrue(type2.IsValid()) + + self.assertTrue(exe_module == type1.GetModule() and + exe_module == type2.GetModule()) Index: lldb/test/API/functionalities/type_get_module/compile_unit1.c =================================================================== --- /dev/null +++ lldb/test/API/functionalities/type_get_module/compile_unit1.c @@ -0,0 +1,7 @@ + +struct compile_unit1_type { + int x; + int y; +}; + +struct compile_unit1_type compile_unit1_var = {1, 1}; Index: lldb/test/API/functionalities/type_get_module/compile_unit2.c =================================================================== --- /dev/null +++ lldb/test/API/functionalities/type_get_module/compile_unit2.c @@ -0,0 +1,7 @@ + +struct compile_unit2_type { + int x; + int y; +}; + +struct compile_unit2_type compile_unit2_var = {2, 2}; Index: lldb/test/API/functionalities/type_get_module/main.c =================================================================== --- /dev/null +++ lldb/test/API/functionalities/type_get_module/main.c @@ -0,0 +1,2 @@ + +int main() { return 0; }