Skip to content

[LLDB] added getName method in SBModule #150331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lldb/include/lldb/API/SBModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ class LLDB_API SBModule {
/// Remove any global modules which are no longer needed.
static void GarbageCollectAllocatedModules();

/// If this Module represents a specific object or part within a larger file,
/// returns the name of that object or part. Otherwise, returns
/// nullptr.
const char *GetObjectName() const;

private:
friend class SBAddress;
friend class SBFrame;
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/API/SBModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,11 @@ void SBModule::GarbageCollectAllocatedModules() {
const bool mandatory = false;
ModuleList::RemoveOrphanSharedModules(mandatory);
}

const char *SBModule::GetObjectName() const {
LLDB_INSTRUMENT_VA(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a newline after LLDB_INSTRUMENT_VA

Suggested change
LLDB_INSTRUMENT_VA(this);
LLDB_INSTRUMENT_VA(this);


if (!m_opaque_sp)
return nullptr;
return m_opaque_sp->GetObjectName().AsCString();
}
11 changes: 10 additions & 1 deletion lldb/test/API/python_api/sbmodule/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
C_SOURCES := main.c
C_SOURCES := main.c a.c b.c
MAKE_DSYM := NO

all: a.out

a.out: main.o libfoo.a
$(LD) $(LDFLAGS) $^ -o $@

libfoo.a: a.o b.o
$(AR) $(ARFLAGS) $@ $^

include Makefile.rules
39 changes: 39 additions & 0 deletions lldb/test/API/python_api/sbmodule/TestSBModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@ def tearDown(self):
if self.background_pid:
os.kill(self.background_pid, signal.SIGKILL)

@skipIfRemote
def test_GetObjectName(self):
"""Test the SBModule::GetObjectName() method"""
self.build()
exe = self.getBuildArtifact("a.out")
libfoo_path = self.getBuildArtifact("libfoo.a")
target_exe = self.dbg.CreateTarget(exe)
self.assertTrue(target_exe.IsValid(), "Target for a.out is valid")

# Test that the executable module has no object name (usually the first module in the target)
exe_module = target_exe.GetModuleAtIndex(0)
self.assertTrue(exe_module.IsValid(), "Executable module is valid")
self.assertIsNone(
exe_module.GetObjectName(), "a.out should have no object name"
)

# check archive member names
module_specs = lldb.SBModuleSpecList.GetModuleSpecifications(libfoo_path)
self.assertGreater(
module_specs.GetSize(), 0, "Archive should have at least one module spec"
)
found = set()
expected = {"a.o", "b.o"}
for i in range(module_specs.GetSize()):
spec = module_specs.GetSpecAtIndex(i)
obj_name = spec.GetObjectName()
self.assertIsInstance(obj_name, str)
self.assertIn(obj_name, expected, f"Unexpected object name: {obj_name}")
# create a module from the arhive using the sepc
module = lldb.SBModule(spec)
self.assertTrue(module.IsValid(), "Module is valid")
self.assertTrue(module.IsValid(), f"Module for {obj_name} is valid")
self.assertEqual(
module.GetObjectName(), obj_name, f"Object name for {obj_name} matches"
)
found.add(obj_name)

self.assertEqual(found, expected, "Did not find all expected archive members")

@skipUnlessDarwin
@skipIfRemote
def test_module_is_file_backed(self):
Expand Down
11 changes: 11 additions & 0 deletions lldb/test/API/python_api/sbmodule/a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int __a_global = 1;

int a(int arg) {
int result = arg + __a_global;
return result;
}

int aa(int arg1) {
int result1 = arg1 - __a_global;
return result1;
}
11 changes: 11 additions & 0 deletions lldb/test/API/python_api/sbmodule/b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
static int __b_global = 2;
char __extra[4096]; // Make sure sizeof b.o differs from a.o
int b(int arg) {
int result = arg + __b_global;
return result;
}

int bb(int arg1) {
int result2 = arg1 - __b_global;
return result2;
}
2 changes: 2 additions & 0 deletions lldb/test/API/python_api/sbmodule/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern int a(int);
extern int b(int);
int main() {
while (1) // break here
;
Expand Down
Loading