[Lldb-commits] [lldb] 2c0cbc4 - GetModule, GetExeModule methods added
Ilya Bukonkin via lldb-commits
lldb-commits at lists.llvm.org
Thu Oct 29 13:45:11 PDT 2020
Author: Ilya Bukonkin
Date: 2020-10-29T23:44:51+03:00
New Revision: 2c0cbc47cae4e69195a883771664ef3d5a54c7d2
URL: https://github.com/llvm/llvm-project/commit/2c0cbc47cae4e69195a883771664ef3d5a54c7d2
DIFF: https://github.com/llvm/llvm-project/commit/2c0cbc47cae4e69195a883771664ef3d5a54c7d2.diff
LOG: GetModule, GetExeModule methods added
Added:
lldb/test/API/functionalities/type_get_module/Makefile
lldb/test/API/functionalities/type_get_module/TestTypeGetModule.py
lldb/test/API/functionalities/type_get_module/compile_unit1.c
lldb/test/API/functionalities/type_get_module/compile_unit2.c
lldb/test/API/functionalities/type_get_module/main.c
Modified:
lldb/bindings/interface/SBType.i
lldb/include/lldb/API/SBModule.h
lldb/include/lldb/API/SBType.h
lldb/include/lldb/Symbol/Type.h
lldb/source/API/SBType.cpp
lldb/source/Symbol/Type.cpp
Removed:
################################################################################
diff --git a/lldb/bindings/interface/SBType.i b/lldb/bindings/interface/SBType.i
index 3cd82452084b..fd2dda188454 100644
--- a/lldb/bindings/interface/SBType.i
+++ b/lldb/bindings/interface/SBType.i
@@ -277,6 +277,9 @@ public:
lldb::SBTypeEnumMemberList
GetEnumMembers();
+ lldb::SBModule
+ GetModule();
+
const char*
GetName();
@@ -330,6 +333,7 @@ public:
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.''')
diff --git a/lldb/include/lldb/API/SBModule.h b/lldb/include/lldb/API/SBModule.h
index ec6e7c21b058..dd783fe4107d 100644
--- a/lldb/include/lldb/API/SBModule.h
+++ b/lldb/include/lldb/API/SBModule.h
@@ -300,6 +300,7 @@ class LLDB_API SBModule {
friend class SBSection;
friend class SBSymbolContext;
friend class SBTarget;
+ friend class SBType;
explicit SBModule(const lldb::ModuleSP &module_sp);
diff --git a/lldb/include/lldb/API/SBType.h b/lldb/include/lldb/API/SBType.h
index b0af43351192..5f487aa5d258 100644
--- a/lldb/include/lldb/API/SBType.h
+++ b/lldb/include/lldb/API/SBType.h
@@ -185,6 +185,8 @@ class SBType {
lldb::SBTypeMemberFunction GetMemberFunctionAtIndex(uint32_t idx);
+ lldb::SBModule GetModule();
+
const char *GetName();
const char *GetDisplayTypeName();
diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index 06fc1d5da0aa..dd917cfb7ca8 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -109,12 +109,18 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
void DumpTypeName(Stream *s);
- // Since Type instances only keep a "SymbolFile *" internally, other classes
- // like TypeImpl need make sure the module is still around before playing
- // with
- // Type instances. They can store a weak pointer to the Module;
+ /// Since Type instances only keep a "SymbolFile *" internally, other classes
+ /// like TypeImpl need make sure the module is still around before playing
+ /// with
+ /// 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);
@@ -261,6 +267,8 @@ class TypeImpl {
void Clear();
+ lldb::ModuleSP GetModule() const;
+
ConstString GetName() const;
ConstString GetDisplayTypeName() const;
@@ -288,8 +296,12 @@ class TypeImpl {
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;
};
diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp
index 1c1d4e3a3ed9..772ac87145bb 100644
--- a/lldb/source/API/SBType.cpp
+++ b/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 @@ uint32_t SBType::GetTypeFlags() {
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 @@ void RegisterMethods<SBType>(Registry &R) {
(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, ());
diff --git a/lldb/source/Symbol/Type.cpp b/lldb/source/Symbol/Type.cpp
index 378523d00896..df5dab19dbf3 100644
--- a/lldb/source/Symbol/Type.cpp
+++ b/lldb/source/Symbol/Type.cpp
@@ -727,6 +727,14 @@ ModuleSP Type::GetModule() {
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 @@ TypeImpl::TypeImpl(const CompilerType &static_type,
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 @@ void TypeImpl::SetType(const CompilerType &compiler_type,
}
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 @@ bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const {
// 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 @@ bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const {
// 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 @@ void TypeImpl::Clear() {
m_dynamic_type.Clear();
}
+ModuleSP TypeImpl::GetModule() const {
+ lldb::ModuleSP module_sp;
+ if (CheckExeModule(module_sp))
+ return module_sp;
+ return nullptr;
+}
+
ConstString TypeImpl::GetName() const {
ModuleSP module_sp;
if (CheckModule(module_sp)) {
diff --git a/lldb/test/API/functionalities/type_get_module/Makefile b/lldb/test/API/functionalities/type_get_module/Makefile
new file mode 100644
index 000000000000..35074dcea34f
--- /dev/null
+++ b/lldb/test/API/functionalities/type_get_module/Makefile
@@ -0,0 +1,2 @@
+C_SOURCES := main.c compile_unit1.c compile_unit2.c
+include Makefile.rules
diff --git a/lldb/test/API/functionalities/type_get_module/TestTypeGetModule.py b/lldb/test/API/functionalities/type_get_module/TestTypeGetModule.py
new file mode 100644
index 000000000000..e6acecc76e17
--- /dev/null
+++ b/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())
diff --git a/lldb/test/API/functionalities/type_get_module/compile_unit1.c b/lldb/test/API/functionalities/type_get_module/compile_unit1.c
new file mode 100644
index 000000000000..0f967ef66810
--- /dev/null
+++ b/lldb/test/API/functionalities/type_get_module/compile_unit1.c
@@ -0,0 +1,6 @@
+struct compile_unit1_type {
+ int x;
+ int y;
+};
+
+struct compile_unit1_type compile_unit1_var = {1, 1};
diff --git a/lldb/test/API/functionalities/type_get_module/compile_unit2.c b/lldb/test/API/functionalities/type_get_module/compile_unit2.c
new file mode 100644
index 000000000000..ca7735e27042
--- /dev/null
+++ b/lldb/test/API/functionalities/type_get_module/compile_unit2.c
@@ -0,0 +1,6 @@
+struct compile_unit2_type {
+ int x;
+ int y;
+};
+
+struct compile_unit2_type compile_unit2_var = {2, 2};
diff --git a/lldb/test/API/functionalities/type_get_module/main.c b/lldb/test/API/functionalities/type_get_module/main.c
new file mode 100644
index 000000000000..76e8197013aa
--- /dev/null
+++ b/lldb/test/API/functionalities/type_get_module/main.c
@@ -0,0 +1 @@
+int main() { return 0; }
More information about the lldb-commits
mailing list