Added the ability to remove orphaned module shared pointers from a ModuleList.

This is helping us track down some extra references to ModuleSP objects that
are causing things to get kept around for too long. 

Added a module pointer accessor to target and change a lot of code to use 
it where it would be more efficient.

"taret delete" can now specify "--clean=1" which will cleanup the global module
list for any orphaned module in the shared module cache which can save memory
and also help track down module reference leaks like we have now.

llvm-svn: 137294
This commit is contained in:
Greg Clayton 2011-08-11 02:48:45 +00:00
parent 8e4c74bb7c
commit aa149cbd86
20 changed files with 137 additions and 82 deletions

View File

@ -363,6 +363,9 @@ public:
size_t
Remove (ModuleList &module_list);
size_t
RemoveOrphans ();
bool
ResolveFileAddress (lldb::addr_t vm_addr,
Address& so_addr);
@ -428,6 +431,9 @@ public:
const ConstString *object_name_ptr,
ModuleList &matching_module_list);
static uint32_t
RemoveOrphanSharedModules ();
protected:
//------------------------------------------------------------------
// Class typedefs.

View File

@ -347,6 +347,9 @@ public:
lldb::ModuleSP
GetExecutableModule ();
Module*
GetExecutableModulePointer ();
//------------------------------------------------------------------
/// Set the main executable module.
///

View File

@ -804,7 +804,7 @@ SBProcess::GetDescription (SBStream &description)
{
char path[PATH_MAX];
GetTarget().GetExecutable().GetPath (path, sizeof(path));
Module *exe_module = m_opaque_sp->GetTarget().GetExecutableModule ().get();
Module *exe_module = m_opaque_sp->GetTarget().GetExecutableModulePointer();
const char *exe_name = NULL;
if (exe_module)
exe_name = exe_module->GetFileSpec().GetFilename().AsCString();

View File

@ -459,9 +459,9 @@ SBTarget::GetExecutable ()
SBFileSpec exe_file_spec;
if (m_opaque_sp)
{
ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ());
if (exe_module_sp)
exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec());
Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
if (exe_module)
exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
}
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));

View File

@ -306,7 +306,6 @@ CommandObjectBreakpointSet::Execute
else if (!m_options.m_func_regexp.empty())
break_type = eSetTypeFunctionRegexp;
ModuleSP module_sp = target->GetExecutableModule();
Breakpoint *bp = NULL;
FileSpec module_spec;
bool use_module = false;

View File

@ -374,17 +374,16 @@ public:
Error error;
const uint32_t argc = args.GetArgumentCount();
Target *target = m_interpreter.GetExecutionContext().target;
ModuleSP exe_module_sp;
if (target)
{
exe_module_sp = target->GetExecutableModule();
if (exe_module_sp)
Module *exe_module = target->GetExecutableModulePointer();
if (exe_module)
{
m_options.launch_info.GetExecutableFile () = exe_module_sp->GetFileSpec();
m_options.launch_info.GetExecutableFile () = exe_module->GetFileSpec();
char exe_path[PATH_MAX];
if (m_options.launch_info.GetExecutableFile ().GetPath (exe_path, sizeof(exe_path)))
m_options.launch_info.GetArguments().AppendArgument (exe_path);
m_options.launch_info.GetArchitecture() = exe_module_sp->GetArchitecture();
m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
}
}

View File

@ -162,7 +162,7 @@ public:
// If our listener is NULL, users aren't allows to launch
char filename[PATH_MAX];
const Module *exe_module = target->GetExecutableModule().get();
const Module *exe_module = target->GetExecutableModulePointer();
if (exe_module == NULL)
{
@ -762,22 +762,22 @@ public:
{
// Okay, we're done. Last step is to warn if the executable module has changed:
char new_path[PATH_MAX];
ModuleSP new_exec_module_sp (target->GetExecutableModule());
if (!old_exec_module_sp)
{
// We might not have a module if we attached to a raw pid...
ModuleSP new_module_sp (target->GetExecutableModule());
if (new_module_sp)
if (new_exec_module_sp)
{
new_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
}
}
else if (old_exec_module_sp->GetFileSpec() != target->GetExecutableModule()->GetFileSpec())
else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
{
char old_path[PATH_MAX];
old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
target->GetExecutableModule()->GetFileSpec().GetPath (new_path, PATH_MAX);
old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
old_path, new_path);

View File

@ -26,6 +26,7 @@
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/Options.h"
#include "lldb/Interpreter/OptionGroupArchitecture.h"
#include "lldb/Interpreter/OptionGroupBoolean.h"
#include "lldb/Interpreter/OptionGroupFile.h"
#include "lldb/Interpreter/OptionGroupVariable.h"
#include "lldb/Interpreter/OptionGroupPlatform.h"
@ -52,11 +53,11 @@ DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bo
{
const ArchSpec &target_arch = target->GetArchitecture();
ModuleSP exe_module_sp (target->GetExecutableModule ());
Module *exe_module = target->GetExecutableModulePointer();
char exe_path[PATH_MAX];
bool exe_valid = false;
if (exe_module_sp)
exe_valid = exe_module_sp->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
if (exe_module)
exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
if (!exe_valid)
::strcpy (exe_path, "<none>");
@ -410,12 +411,16 @@ class CommandObjectTargetDelete : public CommandObject
{
public:
CommandObjectTargetDelete (CommandInterpreter &interpreter) :
CommandObject (interpreter,
"target delete",
"Delete one or more targets by target index.",
NULL,
0)
CommandObject (interpreter,
"target delete",
"Delete one or more targets by target index.",
NULL,
0),
m_option_group (interpreter),
m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', 0, eArgTypeNone, "Perform extra cleanup to minimize memory consumption after deleting the target.", false)
{
m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
m_option_group.Finalize();
}
virtual
@ -487,12 +492,28 @@ public:
target_list.DeleteTarget(target_sp);
target_sp->Destroy();
}
// If "--clean" was specified, prune any orphaned shared modules from
// the global shared module list
if (m_cleanup_option.GetOptionValue ())
{
ModuleList::RemoveOrphanSharedModules();
}
result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
result.SetStatus(eReturnStatusSuccessFinishResult);
}
return result.Succeeded();
}
Options *
GetOptions ()
{
return &m_option_group;
}
protected:
OptionGroupOptions m_option_group;
OptionGroupBoolean m_cleanup_option;
};

View File

@ -1256,17 +1256,17 @@ Debugger::FormatPrompt
(::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
(::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
{
ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
if (exe_module_sp)
Module *exe_module = exe_ctx->process->GetTarget().GetExecutableModulePointer();
if (exe_module)
{
if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
{
format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
var_success = format_file_spec;
}
else
{
format_file_spec = exe_module_sp->GetFileSpec();
format_file_spec = exe_module->GetFileSpec();
var_success = format_file_spec;
}
}

View File

@ -108,6 +108,28 @@ ModuleList::Remove (ModuleSP &module_sp)
return false;
}
size_t
ModuleList::RemoveOrphans ()
{
Mutex::Locker locker(m_modules_mutex);
collection::reverse_iterator pos = m_modules.rbegin();
size_t remove_count = 0;
while (pos != m_modules.rend())
{
if (pos->unique())
{
pos = m_modules.erase (pos);
++remove_count;
}
else
{
++pos;
}
}
return remove_count;
}
size_t
ModuleList::Remove (ModuleList &module_list)
{
@ -680,6 +702,12 @@ ModuleList::FindSharedModules
return shared_module_list.FindModules (&in_file_spec, &arch, uuid_ptr, object_name_ptr, matching_module_list);
}
uint32_t
ModuleList::RemoveOrphanSharedModules ()
{
return GetSharedModuleList ().RemoveOrphans();
}
Error
ModuleList::GetSharedModule
(

View File

@ -307,7 +307,7 @@ ValueObjectRegister::GetClangType ()
Process *process = m_reg_ctx_sp->CalculateProcess ();
if (process)
{
Module *exe_module = process->GetTarget().GetExecutableModule ().get();
Module *exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module)
{
m_clang_type = exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize (m_reg_info.encoding,
@ -338,7 +338,7 @@ ValueObjectRegister::GetClangAST ()
Process *process = m_reg_ctx_sp->CalculateProcess ();
if (process)
{
Module *exe_module = process->GetTarget().GetExecutableModule ().get();
Module *exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module)
return exe_module->GetClangASTContext().getASTContext();
}

View File

@ -89,7 +89,7 @@ DynamicLoaderMacOSXDYLD::CreateInstance (Process* process, bool force)
if (!create)
{
create = true;
Module* exe_module = process->GetTarget().GetExecutableModule().get();
Module* exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module)
{
ObjectFile *object_file = exe_module->GetObjectFile();
@ -225,7 +225,7 @@ DynamicLoaderMacOSXDYLD::LocateDYLD()
}
// Check some default values
Module *executable = m_process->GetTarget().GetExecutableModule().get();
Module *executable = m_process->GetTarget().GetExecutableModulePointer();
if (executable)
{
@ -267,7 +267,7 @@ DynamicLoaderMacOSXDYLD::FindTargetModuleForDYLDImageInfo (const DYLDImageInfo &
{
if (module_sp)
{
if (image_info.UUIDValid())
if (image_info_uuid_is_valid)
{
if (module_sp->GetUUID() != image_info.uuid)
module_sp.reset();
@ -1217,7 +1217,7 @@ DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands(DYLDImageInfo::co
if (exe_module_sp)
{
if (exe_module_sp.get() != m_process->GetTarget().GetExecutableModule().get())
if (exe_module_sp.get() != m_process->GetTarget().GetExecutableModulePointer())
{
// Don't load dependent images since we are in dyld where we will know
// and find out about all images that are loaded

View File

@ -52,7 +52,7 @@ DynamicLoaderMacOSXKernel::CreateInstance (Process* process, bool force)
bool create = force;
if (!create)
{
Module* exe_module = process->GetTarget().GetExecutableModule().get();
Module* exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module)
{
ObjectFile *object_file = exe_module->GetObjectFile();

View File

@ -59,14 +59,14 @@ bool
ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
{
// For now we are just making sure the file exists for a given module
ModuleSP exe_module_sp(target.GetExecutableModule());
if (exe_module_sp.get())
Module *exe_module = target.GetExecutableModulePointer();
if (exe_module)
{
const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
if (triple_ref.getOS() == llvm::Triple::Darwin &&
triple_ref.getVendor() == llvm::Triple::Apple)
{
ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
ObjectFile *exe_objfile = exe_module->GetObjectFile();
if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
return true;

View File

@ -104,9 +104,9 @@ bool
ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
{
// For now we are just making sure the file exists for a given module
ModuleSP exe_module_sp(target.GetExecutableModule());
if (exe_module_sp.get())
return exe_module_sp->GetFileSpec().Exists();
Module *exe_module = target.GetExecutableModulePointer();
if (exe_module)
return exe_module->GetFileSpec().Exists();
// However, if there is no executable module, we return true since we might be preparing to attach.
return true;
}

View File

@ -2019,7 +2019,7 @@ Process::Launch
m_dyld_ap.reset();
m_process_input_reader.reset();
Module *exe_module = m_target.GetExecutableModule().get();
Module *exe_module = m_target.GetExecutableModulePointer();
if (exe_module)
{
char local_exec_file_path[PATH_MAX];
@ -2327,8 +2327,7 @@ Process::CompleteAttach ()
ModuleSP module_sp (modules.GetModuleAtIndex(i));
if (module_sp && module_sp->IsExecutable())
{
ModuleSP target_exe_module_sp (m_target.GetExecutableModule());
if (target_exe_module_sp != module_sp)
if (m_target.GetExecutableModulePointer() != module_sp.get())
m_target.SetExecutableModule (module_sp, false);
break;
}
@ -3319,11 +3318,11 @@ Process::GetSettingsController ()
void
Process::UpdateInstanceName ()
{
ModuleSP module_sp = GetTarget().GetExecutableModule();
if (module_sp)
Module *module = GetTarget().GetExecutableModulePointer();
if (module)
{
StreamString sstr;
sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
sstr.Printf ("%s", module->GetFileSpec().GetFilename().AsCString());
GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
sstr.GetData());

View File

@ -98,8 +98,9 @@ Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
}
else
{
if (GetExecutableModule())
s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Module *exe_module = GetExecutableModulePointer();
if (exe_module)
s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
else
s->PutCString ("No executable module.");
}
@ -437,10 +438,13 @@ Target::EnableBreakpointByID (break_id_t break_id)
ModuleSP
Target::GetExecutableModule ()
{
ModuleSP executable_sp;
if (m_images.GetSize() > 0)
executable_sp = m_images.GetModuleAtIndex(0);
return executable_sp;
return m_images.GetModuleAtIndex(0);
}
Module*
Target::GetExecutableModulePointer ()
{
return m_images.GetModulePointerAtIndex(0);
}
void
@ -915,14 +919,11 @@ Target::ImageSearchPathsChanged
)
{
Target *target = (Target *)baton;
if (target->m_images.GetSize() > 1)
ModuleSP exe_module_sp (target->GetExecutableModule());
if (exe_module_sp)
{
ModuleSP exe_module_sp (target->GetExecutableModule());
if (exe_module_sp)
{
target->m_images.Clear();
target->SetExecutableModule (exe_module_sp, true);
}
target->m_images.Clear();
target->SetExecutableModule (exe_module_sp, true);
}
}
@ -1013,14 +1014,13 @@ Target::UpdateInstanceName ()
{
StreamString sstr;
ModuleSP module_sp = GetExecutableModule();
if (module_sp)
Module *exe_module = GetExecutableModulePointer();
if (exe_module)
{
sstr.Printf ("%s_%s",
module_sp->GetFileSpec().GetFilename().AsCString(),
module_sp->GetArchitecture().GetArchitectureName());
GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
sstr.GetData());
exe_module->GetFileSpec().GetFilename().AsCString(),
exe_module->GetArchitecture().GetArchitectureName());
GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
}
}

View File

@ -148,15 +148,15 @@ TargetList::FindTargetWithExecutableAndArchitecture
collection::const_iterator pos, end = m_target_list.end();
for (pos = m_target_list.begin(); pos != end; ++pos)
{
ModuleSP module_sp ((*pos)->GetExecutableModule());
Module *exe_module = (*pos)->GetExecutableModulePointer();
if (module_sp)
if (exe_module)
{
if (FileSpec::Equal (exe_file_spec, module_sp->GetFileSpec(), full_match))
if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match))
{
if (exe_arch_ptr)
{
if (*exe_arch_ptr != module_sp->GetArchitecture())
if (*exe_arch_ptr != exe_module->GetArchitecture())
continue;
}
target_sp = *pos;

View File

@ -65,9 +65,9 @@ ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
ModuleSP executableModuleSP (target.GetExecutableModule());
Module *exe_module = target.GetExecutableModulePointer();
if (!executableModuleSP)
if (exe_module == NULL)
{
if (log)
log->Printf ("Can't execute code without an executable module.");
@ -75,7 +75,7 @@ ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
}
else
{
ObjectFile *objectFile = executableModuleSP->GetObjectFile();
ObjectFile *objectFile = exe_module->GetObjectFile();
if (!objectFile)
{
if (log)
@ -181,9 +181,9 @@ ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
ModuleSP executableModuleSP (target.GetExecutableModule());
Module *exe_module = target.GetExecutableModulePointer();
if (!executableModuleSP)
if (exe_module == NULL)
{
if (log)
log->Printf ("Can't execute code without an executable module.");
@ -191,7 +191,7 @@ ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
}
else
{
ObjectFile *objectFile = executableModuleSP->GetObjectFile();
ObjectFile *objectFile = exe_module->GetObjectFile();
if (!objectFile)
{
if (log)
@ -204,7 +204,7 @@ ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
{
if (log)
log->Printf ("Could not find entry point address for executable module \"%s\".",
executableModuleSP->GetFileSpec().GetFilename().AsCString());
exe_module->GetFileSpec().GetFilename().AsCString());
return;
}
}

View File

@ -117,12 +117,12 @@ ThreadPlanAssemblyTracer::InitializeTracer()
m_abi = process.GetABI().get();
ModuleSP exe_module_sp (target.GetExecutableModule());
Module *exe_module = target.GetExecutableModulePointer();
if (exe_module_sp)
if (exe_module)
{
m_intptr_type = TypeFromUser(exe_module_sp->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, arch.GetAddressByteSize() * 8),
exe_module_sp->GetClangASTContext().getASTContext());
m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, arch.GetAddressByteSize() * 8),
exe_module->GetClangASTContext().getASTContext());
}
const unsigned int buf_size = 32;