Skip to content

Commit fa17d12

Browse files
committed
[lldb] Add SBModule::GarbageCollectAllocatedModules and clear modules after each test run
Right now the only places in the SB API where lldb:: ModuleSP instances are destroyed are in SBDebugger::MemoryPressureDetected (where it's just attempted but not guaranteed) and in SBDebugger::DeleteTarget (which will be removed in D83933). Tests that directly create an lldb::ModuleSP and never create a target therefore currently leak lldb::Module instances. This triggers the sanity checks in lldbtest that make sure that the global module list is empty after a test. This patch adds SBModule::GarbageCollectAllocatedModules as an explicit way to clean orphaned lldb::ModuleSP instances. Also we now start calling this method at the end of each test run and move the sanity check behind that call to make this work. This way even tests that don't create targets can pass the sanity check. This fixes TestUnicodeSymbols.py when D83865 is applied (which makes that the sanity checks actually fail the test). Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D83876 (cherry picked from commit c2f9454)
1 parent 5288c94 commit fa17d12

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lldb/bindings/interface/SBModule.i

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ public:
353353
static uint32_t
354354
GetNumberAllocatedModules();
355355

356+
%feature("docstring", "
357+
Removes all modules which are no longer needed by any part of LLDB from
358+
the module cache.
359+
360+
This is an implementation detail exposed for testing and should not be
361+
relied upon. Use SBDebugger::MemoryPressureDetected instead to reduce
362+
LLDB's memory consumption during execution.
363+
") GarbageCollectAllocatedModules;
364+
static void
365+
GarbageCollectAllocatedModules();
366+
356367
STRING_EXTENSION(SBModule)
357368

358369
#ifdef SWIGPYTHON

lldb/include/lldb/API/SBModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ class LLDB_API SBModule {
291291
/// Get the number of global modules.
292292
static uint32_t GetNumberAllocatedModules();
293293

294+
/// Remove any global modules which are no longer needed.
295+
static void GarbageCollectAllocatedModules();
296+
294297
private:
295298
friend class SBAddress;
296299
friend class SBFrame;

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,17 @@ def tearDown(self):
10291029
lldb.SBDebugger.Destroy(self.dbg)
10301030
del self.dbg
10311031

1032+
# All modules should be orphaned now so that they can be cleared from
1033+
# the shared module cache.
1034+
lldb.SBModule.GarbageCollectAllocatedModules()
1035+
1036+
# Modules are not orphaned during reproducer replay because they're
1037+
# leaked on purpose.
1038+
if not configuration.is_reproducer():
1039+
# Assert that the global module cache is empty.
1040+
self.assertEqual(lldb.SBModule.GetNumberAllocatedModules(), 0)
1041+
1042+
10321043
# =========================================================
10331044
# Various callbacks to allow introspection of test progress
10341045
# =========================================================
@@ -1984,13 +1995,9 @@ def tearDown(self):
19841995
for target in targets:
19851996
self.dbg.DeleteTarget(target)
19861997

1987-
# Modules are not orphaned during reproducer replay because they're
1988-
# leaked on purpose.
19891998
if not configuration.is_reproducer():
19901999
# Assert that all targets are deleted.
1991-
assert self.dbg.GetNumTargets() == 0
1992-
# Assert that the global module cache is empty.
1993-
assert lldb.SBModule.GetNumberAllocatedModules() == 0
2000+
self.assertEqual(self.dbg.GetNumTargets(), 0)
19942001

19952002
# Do this last, to make sure it's in reverse order from how we setup.
19962003
Base.tearDown(self)

lldb/source/API/SBModule.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,13 @@ uint32_t SBModule::GetNumberAllocatedModules() {
690690
return Module::GetNumberAllocatedModules();
691691
}
692692

693+
void SBModule::GarbageCollectAllocatedModules() {
694+
LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBModule,
695+
GarbageCollectAllocatedModules);
696+
const bool mandatory = false;
697+
ModuleList::RemoveOrphanSharedModules(mandatory);
698+
}
699+
693700
namespace lldb_private {
694701
namespace repro {
695702

0 commit comments

Comments
 (0)