Skip to content

Commit 402109e

Browse files
barsolo2000Bar Soloveychik
andauthored
[LLDB] added getName method in SBModule (#150331)
added getName method in SBModule.h and .cpp in order to get the name of the module from m_object_name. --------- Co-authored-by: Bar Soloveychik <[email protected]>
1 parent 6127e46 commit 402109e

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

lldb/include/lldb/API/SBModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ class LLDB_API SBModule {
296296
/// Remove any global modules which are no longer needed.
297297
static void GarbageCollectAllocatedModules();
298298

299+
/// If this Module represents a specific object or part within a larger file,
300+
/// returns the name of that object or part. Otherwise, returns
301+
/// nullptr.
302+
const char *GetObjectName() const;
303+
299304
private:
300305
friend class SBAddress;
301306
friend class SBFrame;

lldb/source/API/SBModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,11 @@ void SBModule::GarbageCollectAllocatedModules() {
671671
const bool mandatory = false;
672672
ModuleList::RemoveOrphanSharedModules(mandatory);
673673
}
674+
675+
const char *SBModule::GetObjectName() const {
676+
LLDB_INSTRUMENT_VA(this);
677+
678+
if (!m_opaque_sp)
679+
return nullptr;
680+
return m_opaque_sp->GetObjectName().AsCString();
681+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
C_SOURCES := main.c
1+
C_SOURCES := main.c a.c b.c
2+
MAKE_DSYM := NO
3+
4+
all: a.out
5+
6+
a.out: main.o libfoo.a
7+
$(LD) $(LDFLAGS) $^ -o $@
8+
9+
libfoo.a: a.o b.o
10+
$(AR) $(ARFLAGS) $@ $^
211

312
include Makefile.rules

lldb/test/API/python_api/sbmodule/TestSBModule.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,45 @@ def tearDown(self):
1818
if self.background_pid:
1919
os.kill(self.background_pid, signal.SIGKILL)
2020

21+
@skipIfRemote
22+
def test_GetObjectName(self):
23+
"""Test the SBModule::GetObjectName() method"""
24+
self.build()
25+
exe = self.getBuildArtifact("a.out")
26+
libfoo_path = self.getBuildArtifact("libfoo.a")
27+
target_exe = self.dbg.CreateTarget(exe)
28+
self.assertTrue(target_exe.IsValid(), "Target for a.out is valid")
29+
30+
# Test that the executable module has no object name (usually the first module in the target)
31+
exe_module = target_exe.GetModuleAtIndex(0)
32+
self.assertTrue(exe_module.IsValid(), "Executable module is valid")
33+
self.assertIsNone(
34+
exe_module.GetObjectName(), "a.out should have no object name"
35+
)
36+
37+
# check archive member names
38+
module_specs = lldb.SBModuleSpecList.GetModuleSpecifications(libfoo_path)
39+
self.assertGreater(
40+
module_specs.GetSize(), 0, "Archive should have at least one module spec"
41+
)
42+
found = set()
43+
expected = {"a.o", "b.o"}
44+
for i in range(module_specs.GetSize()):
45+
spec = module_specs.GetSpecAtIndex(i)
46+
obj_name = spec.GetObjectName()
47+
self.assertIsInstance(obj_name, str)
48+
self.assertIn(obj_name, expected, f"Unexpected object name: {obj_name}")
49+
# create a module from the arhive using the sepc
50+
module = lldb.SBModule(spec)
51+
self.assertTrue(module.IsValid(), "Module is valid")
52+
self.assertTrue(module.IsValid(), f"Module for {obj_name} is valid")
53+
self.assertEqual(
54+
module.GetObjectName(), obj_name, f"Object name for {obj_name} matches"
55+
)
56+
found.add(obj_name)
57+
58+
self.assertEqual(found, expected, "Did not find all expected archive members")
59+
2160
@skipUnlessDarwin
2261
@skipIfRemote
2362
def test_module_is_file_backed(self):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int __a_global = 1;
2+
3+
int a(int arg) {
4+
int result = arg + __a_global;
5+
return result;
6+
}
7+
8+
int aa(int arg1) {
9+
int result1 = arg1 - __a_global;
10+
return result1;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
static int __b_global = 2;
2+
char __extra[4096]; // Make sure sizeof b.o differs from a.o
3+
int b(int arg) {
4+
int result = arg + __b_global;
5+
return result;
6+
}
7+
8+
int bb(int arg1) {
9+
int result2 = arg1 - __b_global;
10+
return result2;
11+
}

lldb/test/API/python_api/sbmodule/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
extern int a(int);
2+
extern int b(int);
13
int main() {
24
while (1) // break here
35
;

0 commit comments

Comments
 (0)