Skip to content

[lldb] Add SBModule::GarbageCollectAllocatedModules and clear modules… #1939

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

Merged
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
11 changes: 11 additions & 0 deletions lldb/bindings/interface/SBModule.i
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,17 @@ public:
static uint32_t
GetNumberAllocatedModules();

%feature("docstring", "
Removes all modules which are no longer needed by any part of LLDB from
the module cache.

This is an implementation detail exposed for testing and should not be
relied upon. Use SBDebugger::MemoryPressureDetected instead to reduce
LLDB's memory consumption during execution.
") GarbageCollectAllocatedModules;
static void
GarbageCollectAllocatedModules();

STRING_EXTENSION(SBModule)

#ifdef SWIGPYTHON
Expand Down
3 changes: 3 additions & 0 deletions lldb/include/lldb/API/SBModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ class LLDB_API SBModule {
/// Get the number of global modules.
static uint32_t GetNumberAllocatedModules();

/// Remove any global modules which are no longer needed.
static void GarbageCollectAllocatedModules();

private:
friend class SBAddress;
friend class SBFrame;
Expand Down
17 changes: 12 additions & 5 deletions lldb/packages/Python/lldbsuite/test/lldbtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,17 @@ def tearDown(self):
lldb.SBDebugger.Destroy(self.dbg)
del self.dbg

# All modules should be orphaned now so that they can be cleared from
# the shared module cache.
lldb.SBModule.GarbageCollectAllocatedModules()

# Modules are not orphaned during reproducer replay because they're
# leaked on purpose.
if not configuration.is_reproducer():
# Assert that the global module cache is empty.
self.assertEqual(lldb.SBModule.GetNumberAllocatedModules(), 0)


# =========================================================
# Various callbacks to allow introspection of test progress
# =========================================================
Expand Down Expand Up @@ -1984,13 +1995,9 @@ def tearDown(self):
for target in targets:
self.dbg.DeleteTarget(target)

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

# Do this last, to make sure it's in reverse order from how we setup.
Base.tearDown(self)
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/API/SBModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,13 @@ uint32_t SBModule::GetNumberAllocatedModules() {
return Module::GetNumberAllocatedModules();
}

void SBModule::GarbageCollectAllocatedModules() {
LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBModule,
GarbageCollectAllocatedModules);
const bool mandatory = false;
ModuleList::RemoveOrphanSharedModules(mandatory);
}

namespace lldb_private {
namespace repro {

Expand Down