Skip to content

Commit 3e39328

Browse files
authored
[lldb] Fix TestModuleLoadedNotifys API test to work correctly on most of Linux targets (#94672)
The different build configuration and target Linux system can load a different number of .so libraries. Add and check own libraries.
1 parent 58550a7 commit 3e39328

File tree

7 files changed

+84
-18
lines changed

7 files changed

+84
-18
lines changed
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
CXX_SOURCES := main.cpp
2-
3-
include Makefile.rules
1+
CXX_SOURCES := main.cpp
2+
LD_EXTRAS := -L. -l_d -l_c -l_a -l_b
3+
4+
a.out: lib_b lib_a lib_c lib_d
5+
6+
include Makefile.rules
7+
8+
lib_a: lib_b
9+
$(MAKE) -f $(MAKEFILE_RULES) \
10+
DYLIB_ONLY=YES DYLIB_CXX_SOURCES=a.cpp DYLIB_NAME=_a \
11+
LD_EXTRAS="-L. -l_b"
12+
13+
lib_b:
14+
$(MAKE) -f $(MAKEFILE_RULES) \
15+
DYLIB_ONLY=YES DYLIB_CXX_SOURCES=b.cpp DYLIB_NAME=_b
16+
17+
lib_c:
18+
$(MAKE) -f $(MAKEFILE_RULES) \
19+
DYLIB_ONLY=YES DYLIB_CXX_SOURCES=c.cpp DYLIB_NAME=_c
20+
21+
lib_d:
22+
$(MAKE) -f $(MAKEFILE_RULES) \
23+
DYLIB_ONLY=YES DYLIB_CXX_SOURCES=d.cpp DYLIB_NAME=_d

lldb/test/API/functionalities/target-new-solib-notifications/TestModuleLoadedNotifys.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,47 @@
99
from lldbsuite.test import lldbutil
1010

1111

12+
@skipUnlessPlatform(["linux"] + lldbplatformutil.getDarwinOSTriples())
1213
class ModuleLoadedNotifysTestCase(TestBase):
1314
NO_DEBUG_INFO_TESTCASE = True
1415

1516
# At least DynamicLoaderDarwin and DynamicLoaderPOSIXDYLD should batch up
1617
# notifications about newly added/removed libraries. Other DynamicLoaders may
1718
# not be written this way.
18-
@skipUnlessPlatform(["linux"] + lldbplatformutil.getDarwinOSTriples())
1919
def setUp(self):
2020
# Call super's setUp().
2121
TestBase.setUp(self)
2222
# Find the line number to break inside main().
2323
self.line = line_number("main.cpp", "// breakpoint")
2424

25+
def setup_test(self, solibs):
26+
if lldb.remote_platform:
27+
path = lldb.remote_platform.GetWorkingDirectory()
28+
for f in solibs:
29+
lldbutil.install_to_target(self, self.getBuildArtifact(f))
30+
else:
31+
path = self.getBuildDir()
32+
if self.dylibPath in os.environ:
33+
sep = self.platformContext.shlib_path_separator
34+
path = os.environ[self.dylibPath] + sep + path
35+
self.runCmd(
36+
"settings append target.env-vars '{}={}'".format(self.dylibPath, path)
37+
)
38+
self.default_path = path
39+
2540
def test_launch_notifications(self):
2641
"""Test that lldb broadcasts newly loaded libraries in batches."""
42+
43+
expected_solibs = [
44+
"lib_a." + self.platformContext.shlib_extension,
45+
"lib_b." + self.platformContext.shlib_extension,
46+
"lib_c." + self.platformContext.shlib_extension,
47+
"lib_d." + self.platformContext.shlib_extension,
48+
]
49+
2750
self.build()
51+
self.setup_test(expected_solibs)
52+
2853
exe = self.getBuildArtifact("a.out")
2954
self.dbg.SetAsync(False)
3055

@@ -70,6 +95,8 @@ def test_launch_notifications(self):
7095
total_modules_added_events = 0
7196
total_modules_removed_events = 0
7297
already_loaded_modules = []
98+
max_solibs_per_event = 0
99+
max_solib_chunk_per_event = []
73100
while listener.GetNextEvent(event):
74101
if lldb.SBTarget.EventIsTargetEvent(event):
75102
if event.GetType() == lldb.SBTarget.eBroadcastBitModulesLoaded:
@@ -91,12 +118,17 @@ def test_launch_notifications(self):
91118
"{} is already loaded".format(module),
92119
)
93120
already_loaded_modules.append(module)
94-
if self.TraceOn():
95-
added_files.append(module.GetFileSpec().GetFilename())
121+
added_files.append(module.GetFileSpec().GetFilename())
96122
if self.TraceOn():
97123
# print all of the binaries that have been added
98124
print("Loaded files: %s" % (", ".join(added_files)))
99125

126+
# We will check the latest biggest chunk of loaded solibs.
127+
# We expect all of our solibs in the last chunk of loaded modules.
128+
if solib_count >= max_solibs_per_event:
129+
max_solib_chunk_per_event = added_files.copy()
130+
max_solibs_per_event = solib_count
131+
100132
if event.GetType() == lldb.SBTarget.eBroadcastBitModulesUnloaded:
101133
solib_count = lldb.SBTarget.GetNumModulesFromEvent(event)
102134
total_modules_removed_events += 1
@@ -115,9 +147,7 @@ def test_launch_notifications(self):
115147
# binaries in batches. Check that we got back more than 1 solib per event.
116148
# In practice on Darwin today, we get back two events for a do-nothing c
117149
# program: a.out and dyld, and then all the rest of the system libraries.
118-
# On Linux we get events for ld.so, [vdso], the binary and then all libraries.
119-
120-
avg_solibs_added_per_event = round(
121-
float(total_solibs_added) / float(total_modules_added_events)
122-
)
123-
self.assertGreater(avg_solibs_added_per_event, 1)
150+
# On Linux we get events for ld.so, [vdso], the binary and then all libraries,
151+
# but the different configurations could load a different number of .so modules
152+
# per event.
153+
self.assertLessEqual(set(expected_solibs), set(max_solib_chunk_per_event))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern "C" int b_function();
2+
3+
extern "C" int a_function() { return b_function(); }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern "C" int b_function() { return 500; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern "C" int c_function() { return 600; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern "C" int d_function() { return 700; }
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
#include <stdio.h>
2-
int main ()
3-
{
4-
puts("running"); // breakpoint here
5-
return 0;
6-
}
1+
#include <stdio.h>
2+
3+
extern "C" int a_function();
4+
extern "C" int c_function();
5+
extern "C" int b_function();
6+
extern "C" int d_function();
7+
8+
int main() {
9+
a_function();
10+
b_function();
11+
c_function();
12+
d_function();
13+
14+
puts("running"); // breakpoint here
15+
return 0;
16+
}

0 commit comments

Comments
 (0)