<rdar://problem/13443931>

Fixed a crasher in the SourceManager where it wasn't checking the m_target member variable for NULL.

In doing this fix, I hardened this class to have weak pointers to the debugger and target in case they do go away. I also changed SBSourceManager to hold onto weak pointers to the debugger and target so they don't keep objects alive by holding a strong reference to them.

llvm-svn: 177365
This commit is contained in:
Greg Clayton 2013-03-19 00:20:55 +00:00
parent 8dfb68e039
commit 9585fbfc67
8 changed files with 231 additions and 87 deletions

View File

@ -159,10 +159,7 @@ public:
// information, but it can look up files by absolute path and display them to you.
// To get the target's source manager, call GetSourceManager on the target instead.
SourceManager &
GetSourceManager ()
{
return m_source_manager;
}
GetSourceManager ();
public:
@ -362,7 +359,7 @@ protected:
TargetList m_target_list;
PlatformList m_platform_list;
Listener m_listener;
SourceManager m_source_manager; // This is a scratch source manager that we return if we have no targets.
std::auto_ptr<SourceManager> m_source_manager_ap; // This is a scratch source manager that we return if we have no targets.
SourceManager::SourceFileCache m_source_file_cache; // All the source managers for targets created in this debugger used this shared
// source file cache.
std::auto_ptr<CommandInterpreter> m_command_interpreter_ap;

View File

@ -113,8 +113,8 @@ public:
//------------------------------------------------------------------
// A source manager can be made with a non-null target, in which case it can use the path remappings to find
// source files that are not in their build locations. With no target it won't be able to do this.
SourceManager (Debugger &debugger);
SourceManager (Target &target);
SourceManager (const lldb::DebuggerSP &debugger_sp);
SourceManager (const lldb::TargetSP &target_sp);
~SourceManager();
@ -180,8 +180,8 @@ protected:
uint32_t m_last_line;
uint32_t m_last_count;
bool m_default_set;
Target *m_target;
Debugger *m_debugger;
lldb::TargetWP m_target_wp;
lldb::DebuggerWP m_debugger_wp;
private:
//------------------------------------------------------------------

View File

@ -24,7 +24,6 @@
#include "lldb/Core/Event.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/UserSettingsController.h"
#include "lldb/Core/SourceManager.h"
#include "lldb/Expression/ClangPersistentVariables.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/OptionValueBoolean.h"
@ -1120,10 +1119,7 @@ public:
}
SourceManager &
GetSourceManager ()
{
return m_source_manager;
}
GetSourceManager ();
//------------------------------------------------------------------
// Methods.
@ -1164,7 +1160,7 @@ protected:
std::auto_ptr<ClangASTImporter> m_ast_importer_ap;
ClangPersistentVariables m_persistent_variables; ///< These are the persistent variables associated with this process for the expression parser.
SourceManager m_source_manager;
std::auto_ptr<SourceManager> m_source_manager_ap;
typedef std::map<lldb::user_id_t, StopHookSP> StopHookCollection;
StopHookCollection m_stop_hooks;

View File

@ -27,22 +27,24 @@ namespace lldb_private
class SourceManagerImpl
{
public:
SourceManagerImpl (const lldb::DebuggerSP &debugger_sp)
SourceManagerImpl (const lldb::DebuggerSP &debugger_sp) :
m_debugger_wp (debugger_sp),
m_target_wp ()
{
m_debugger_sp = debugger_sp;
}
SourceManagerImpl (const lldb::TargetSP &target_sp)
SourceManagerImpl (const lldb::TargetSP &target_sp) :
m_debugger_wp (),
m_target_wp (target_sp)
{
m_target_sp = target_sp;
}
SourceManagerImpl (const SourceManagerImpl &rhs)
{
if (&rhs == this)
return;
m_debugger_sp = rhs.m_debugger_sp;
m_target_sp = rhs.m_target_sp;
m_debugger_wp = rhs.m_debugger_wp;
m_target_wp = rhs.m_target_wp;
}
size_t
@ -56,27 +58,35 @@ namespace lldb_private
if (!file)
return 0;
if (m_debugger_sp)
return m_debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
line,
context_before,
context_after,
current_line_cstr,
s);
else if (m_target_sp)
return m_target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
line,
context_before,
context_after,
current_line_cstr,
s);
lldb::TargetSP target_sp (m_target_wp.lock());
if (target_sp)
{
return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
line,
context_before,
context_after,
current_line_cstr,
s);
}
else
return 0;
{
lldb::DebuggerSP debugger_sp (m_debugger_wp.lock());
if (debugger_sp)
{
return debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
line,
context_before,
context_after,
current_line_cstr,
s);
}
}
return 0;
}
private:
lldb::DebuggerSP m_debugger_sp;
lldb::TargetSP m_target_sp;
lldb::DebuggerWP m_debugger_wp;
lldb::TargetWP m_target_wp;
};
}

View File

@ -554,7 +554,7 @@ Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
m_target_list (*this),
m_platform_list (),
m_listener ("lldb.Debugger"),
m_source_manager(*this),
m_source_manager_ap(),
m_source_file_cache(),
m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
m_input_reader_stack (),
@ -2651,3 +2651,12 @@ Debugger::EnableLog (const char *channel, const char **categories, const char *l
return false;
}
SourceManager &
Debugger::GetSourceManager ()
{
if (m_source_manager_ap.get() == NULL)
m_source_manager_ap.reset (new SourceManager (shared_from_this()));
return *m_source_manager_ap;
}

View File

@ -25,8 +25,10 @@
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/Target.h"
using namespace lldb;
using namespace lldb_private;
static inline bool is_newline_char(char ch)
{
return ch == '\n' || ch == '\r';
@ -36,24 +38,23 @@ static inline bool is_newline_char(char ch)
//----------------------------------------------------------------------
// SourceManager constructor
//----------------------------------------------------------------------
SourceManager::SourceManager(Target &target) :
SourceManager::SourceManager(const TargetSP &target_sp) :
m_last_file_sp (),
m_last_line (0),
m_last_count (0),
m_default_set(false),
m_target (&target),
m_debugger(NULL)
m_target_wp (target_sp),
m_debugger_wp(target_sp->GetDebugger().shared_from_this())
{
m_debugger = &(m_target->GetDebugger());
}
SourceManager::SourceManager(Debugger &debugger) :
SourceManager::SourceManager(const DebuggerSP &debugger_sp) :
m_last_file_sp (),
m_last_line (0),
m_last_count (0),
m_default_set(false),
m_target (NULL),
m_debugger (&debugger)
m_target_wp (),
m_debugger_wp (debugger_sp)
{
}
@ -69,23 +70,27 @@ SourceManager::GetFile (const FileSpec &file_spec)
{
bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec);
DebuggerSP debugger_sp (m_debugger_wp.lock());
FileSP file_sp;
if (same_as_previous)
file_sp = m_last_file_sp;
else
file_sp = m_debugger->GetSourceFileCache().FindSourceFile (file_spec);
else if (debugger_sp)
file_sp = debugger_sp->GetSourceFileCache().FindSourceFile (file_spec);
TargetSP target_sp (m_target_wp.lock());
// It the target source path map has been updated, get this file again so we
// can successfully remap the source file
if (file_sp && file_sp->GetSourceMapModificationID() != m_target->GetSourcePathMap().GetModificationID())
if (target_sp && file_sp && file_sp->GetSourceMapModificationID() != target_sp->GetSourcePathMap().GetModificationID())
file_sp.reset();
// If file_sp is no good or it points to a non-existent file, reset it.
if (!file_sp || !file_sp->GetFileSpec().Exists())
{
file_sp.reset (new File (file_spec, m_target));
file_sp.reset (new File (file_spec, target_sp.get()));
m_debugger->GetSourceFileCache().AddSourceFile(file_sp);
if (debugger_sp)
debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
}
return file_sp;
}
@ -195,6 +200,7 @@ SourceManager::DisplayMoreWithLineNumbers (Stream *s,
const SymbolContextList *bp_locs)
{
// If we get called before anybody has set a default file and line, then try to figure it out here.
const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
if (!m_default_set)
{
FileSpec tmp_spec;
@ -226,7 +232,7 @@ SourceManager::DisplayMoreWithLineNumbers (Stream *s,
else
m_last_line = 1;
}
else
else if (have_default_file_line)
m_last_line += m_last_count;
}
else
@ -267,38 +273,43 @@ SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line)
}
else if (!m_default_set)
{
// If nobody has set the default file and line then try here. If there's no executable, then we
// will try again later when there is one. Otherwise, if we can't find it we won't look again,
// somebody will have to set it (for instance when we stop somewhere...)
Module *executable_ptr = m_target->GetExecutableModulePointer();
if (executable_ptr)
TargetSP target_sp (m_target_wp.lock());
if (target_sp)
{
SymbolContextList sc_list;
ConstString main_name("main");
bool symbols_okay = false; // Force it to be a debug symbol.
bool inlines_okay = true;
bool append = false;
size_t num_matches = executable_ptr->FindFunctions (main_name,
NULL,
lldb::eFunctionNameTypeBase,
inlines_okay,
symbols_okay,
append,
sc_list);
for (size_t idx = 0; idx < num_matches; idx++)
// If nobody has set the default file and line then try here. If there's no executable, then we
// will try again later when there is one. Otherwise, if we can't find it we won't look again,
// somebody will have to set it (for instance when we stop somewhere...)
Module *executable_ptr = target_sp->GetExecutableModulePointer();
if (executable_ptr)
{
SymbolContext sc;
sc_list.GetContextAtIndex(idx, sc);
if (sc.function)
SymbolContextList sc_list;
ConstString main_name("main");
bool symbols_okay = false; // Force it to be a debug symbol.
bool inlines_okay = true;
bool append = false;
size_t num_matches = executable_ptr->FindFunctions (main_name,
NULL,
lldb::eFunctionNameTypeBase,
inlines_okay,
symbols_okay,
append,
sc_list);
for (size_t idx = 0; idx < num_matches; idx++)
{
lldb_private::LineEntry line_entry;
if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
SymbolContext sc;
sc_list.GetContextAtIndex(idx, sc);
if (sc.function)
{
SetDefaultFileAndLine (line_entry.file,
line_entry.line);
file_spec = m_last_file_sp->GetFileSpec();
line = m_last_line;
return true;
lldb_private::LineEntry line_entry;
if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
{
SetDefaultFileAndLine (line_entry.file,
line_entry.line);
file_spec = m_last_file_sp->GetFileSpec();
line = m_last_line;
return true;
}
}
}
}

View File

@ -27,6 +27,7 @@
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/SourceManager.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/Timer.h"
#include "lldb/Core/ValueObject.h"
@ -79,7 +80,7 @@ Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::Plat
m_scratch_ast_source_ap (NULL),
m_ast_importer_ap (NULL),
m_persistent_variables (),
m_source_manager(*this),
m_source_manager_ap(),
m_stop_hooks (),
m_stop_hook_next_id (0),
m_suppress_stop_hooks (false),
@ -1908,6 +1909,15 @@ Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) c
return opcode_addr;
}
SourceManager &
Target::GetSourceManager ()
{
if (m_source_manager_ap.get() == NULL)
m_source_manager_ap.reset (new SourceManager(shared_from_this()));
return *m_source_manager_ap;
}
lldb::user_id_t
Target::AddStopHook (Target::StopHookSP &new_hook_sp)
{

View File

@ -60,6 +60,55 @@
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
264B3DE516F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 26F5C26A10F3D9A4009D5894;
remoteInfo = "lldb-tool";
};
264B3DE716F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 26680207115FD0ED008E1FE4;
remoteInfo = LLDB;
};
264B3DE916F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 26579F68126A25920007C5CB;
remoteInfo = "darwin-debug";
};
264B3DEB16F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 2689FFCA13353D7A00698AC0;
remoteInfo = "lldb-core";
};
264B3DED16F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 26DC6A101337FE6900FF7998;
remoteInfo = "lldb-platform";
};
264B3DEF16F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = EDC6D49914E5C19B001B75F8;
remoteInfo = launcherXPCService;
};
264B3DF116F7E47600D1E7AB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C86C5C316F7A35000844407 /* lldb.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = EDE274EC14EDCE1F005B0F75;
remoteInfo = launcherRootXPCService;
};
4C1E379016F40BCD00FF10BB /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 4C1E373116F4035D00FF10BB /* Project object */;
@ -130,12 +179,12 @@
4C1E377816F4089E00FF10BB /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
4C1E37B316F79E4600FF10BB /* fmts_tester.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = fmts_tester.mm; sourceTree = "<group>"; };
4C1E37B416F79E4600FF10BB /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
4C1E37BA16F79E9D00FF10BB /* formatters */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = formatters; sourceTree = BUILT_PRODUCTS_DIR; };
4C1E37BA16F79E9D00FF10BB /* lldb-perf-formatters */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "lldb-perf-formatters"; sourceTree = BUILT_PRODUCTS_DIR; };
4C1E37DB16F7A03900FF10BB /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework; sourceTree = "<absolute>"; };
4C1E37DC16F7A03900FF10BB /* LLDB.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LLDB.framework; path = "tools/lldb-perf/../../build/Debug/LLDB.framework"; sourceTree = "<group>"; };
4C86C5C316F7A35000844407 /* lldb.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = file; name = lldb.xcodeproj; path = ../../lldb.xcodeproj; sourceTree = "<group>"; };
4C86C5C416F7A36A00844407 /* LLDB.framework */ = {isa = PBXFileReference; lastKnownFileType = file; name = LLDB.framework; path = build/Debug/LLDB.framework; sourceTree = "<group>"; };
4C86C5C616F7A37800844407 /* LLDB.framework */ = {isa = PBXFileReference; lastKnownFileType = file; name = LLDB.framework; path = build/Debug/LLDB.framework; sourceTree = "<group>"; };
4C86C5C316F7A35000844407 /* lldb.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = lldb.xcodeproj; path = ../../lldb.xcodeproj; sourceTree = "<group>"; };
4C86C5C416F7A36A00844407 /* LLDB.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LLDB.framework; path = build/Debug/LLDB.framework; sourceTree = "<group>"; };
4C86C5C616F7A37800844407 /* LLDB.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LLDB.framework; path = build/Debug/LLDB.framework; sourceTree = "<group>"; };
4C86C5D116F7CC8900844407 /* format-tester */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "format-tester"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@ -194,7 +243,7 @@
children = (
4C1E373916F4035D00FF10BB /* liblldbperf.a */,
4C1E376D16F4087A00FF10BB /* lldb-perf-sketch */,
4C1E37BA16F79E9D00FF10BB /* formatters */,
4C1E37BA16F79E9D00FF10BB /* lldb-perf-formatters */,
4C86C5D116F7CC8900844407 /* format-tester */,
);
name = Products;
@ -284,6 +333,15 @@
};
4C86C5DE16F7D18F00844407 /* Products */ = {
isa = PBXGroup;
children = (
264B3DE616F7E47600D1E7AB /* lldb */,
264B3DE816F7E47600D1E7AB /* LLDB.framework */,
264B3DEA16F7E47600D1E7AB /* darwin-debug */,
264B3DEC16F7E47600D1E7AB /* liblldb-core.a */,
264B3DEE16F7E47600D1E7AB /* lldb-platform */,
264B3DF016F7E47600D1E7AB /* com.apple.lldb.launcherXPCService.xpc */,
264B3DF216F7E47600D1E7AB /* com.apple.lldb.launcherRootXPCService.xpc */,
);
name = Products;
sourceTree = "<group>";
};
@ -364,7 +422,7 @@
);
name = "lldb-perf-formatters";
productName = formatters;
productReference = 4C1E37BA16F79E9D00FF10BB /* formatters */;
productReference = 4C1E37BA16F79E9D00FF10BB /* lldb-perf-formatters */;
productType = "com.apple.product-type.tool";
};
4C86C5D016F7CC8900844407 /* format-tester */ = {
@ -419,6 +477,58 @@
};
/* End PBXProject section */
/* Begin PBXReferenceProxy section */
264B3DE616F7E47600D1E7AB /* lldb */ = {
isa = PBXReferenceProxy;
fileType = "compiled.mach-o.executable";
path = lldb;
remoteRef = 264B3DE516F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DE816F7E47600D1E7AB /* LLDB.framework */ = {
isa = PBXReferenceProxy;
fileType = wrapper.framework;
path = LLDB.framework;
remoteRef = 264B3DE716F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DEA16F7E47600D1E7AB /* darwin-debug */ = {
isa = PBXReferenceProxy;
fileType = "compiled.mach-o.executable";
path = "darwin-debug";
remoteRef = 264B3DE916F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DEC16F7E47600D1E7AB /* liblldb-core.a */ = {
isa = PBXReferenceProxy;
fileType = "compiled.mach-o.dylib";
path = "liblldb-core.a";
remoteRef = 264B3DEB16F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DEE16F7E47600D1E7AB /* lldb-platform */ = {
isa = PBXReferenceProxy;
fileType = "compiled.mach-o.executable";
path = "lldb-platform";
remoteRef = 264B3DED16F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DF016F7E47600D1E7AB /* com.apple.lldb.launcherXPCService.xpc */ = {
isa = PBXReferenceProxy;
fileType = wrapper.cfbundle;
path = com.apple.lldb.launcherXPCService.xpc;
remoteRef = 264B3DEF16F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
264B3DF216F7E47600D1E7AB /* com.apple.lldb.launcherRootXPCService.xpc */ = {
isa = PBXReferenceProxy;
fileType = wrapper.cfbundle;
path = com.apple.lldb.launcherRootXPCService.xpc;
remoteRef = 264B3DF116F7E47600D1E7AB /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
/* End PBXReferenceProxy section */
/* Begin PBXSourcesBuildPhase section */
4C1E373516F4035D00FF10BB /* Sources */ = {
isa = PBXSourcesBuildPhase;
@ -720,6 +830,7 @@
4C86C5D916F7CC8900844407 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};