llvm-project/lldb/source/API/SBSourceManager.cpp

145 lines
4.8 KiB
C++
Raw Normal View History

//===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBTarget.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBFileSpec.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/SourceManager.h"
#include "lldb/Target/Target.h"
namespace lldb_private
{
class SourceManagerImpl
{
public:
SourceManagerImpl (const lldb::DebuggerSP &debugger_sp) :
m_debugger_wp (debugger_sp),
m_target_wp ()
{
}
SourceManagerImpl (const lldb::TargetSP &target_sp) :
m_debugger_wp (),
m_target_wp (target_sp)
{
}
SourceManagerImpl (const SourceManagerImpl &rhs)
{
if (&rhs == this)
return;
m_debugger_wp = rhs.m_debugger_wp;
m_target_wp = rhs.m_target_wp;
}
size_t
DisplaySourceLinesWithLineNumbers (const lldb_private::FileSpec &file,
uint32_t line,
uint32_t context_before,
uint32_t context_after,
const char *current_line_cstr,
lldb_private::Stream *s)
{
if (!file)
return 0;
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
{
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::DebuggerWP m_debugger_wp;
lldb::TargetWP m_target_wp;
};
}
using namespace lldb;
using namespace lldb_private;
SBSourceManager::SBSourceManager (const SBDebugger &debugger)
{
m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
}
SBSourceManager::SBSourceManager (const SBTarget &target)
{
SBFrame is now threadsafe using some extra tricks. One issue is that stack frames might go away (the object itself, not the actual logical frame) when we are single stepping due to the way we currently sometimes end up flushing frames when stepping in/out/over. They later will come back to life represented by another object yet they have the same StackID. Now when you get a lldb::SBFrame object, it will track the frame it is initialized with until the thread goes away or the StackID no longer exists in the stack for the thread it was created on. It uses a weak_ptr to both the frame and thread and also stores the StackID. These three items allow us to determine when the stack frame object has gone away (the weak_ptr will be NULL) and allows us to find the correct frame again. In our test suite we had such cases where we were just getting lucky when something like this happened: 1 - stop at breakpoint 2 - get first frame in thread where we stopped 3 - run an expression that causes the program to JIT and run code 4 - run more expressions on the frame from step 2 which was very very luckily still around inside a shared pointer, yet, not part of the current thread (a new stack frame object had appeared with the same stack ID and depth). We now avoid all such issues and properly keep up to date, or we start returning errors when the frame doesn't exist and always responds with invalid answers. Also fixed the UserSettingsController (not going to rewrite this just yet) so that it doesn't crash on shutdown. Using weak_ptr's came in real handy to track when the master controller has already gone away and this allowed me to pull out the previous NotifyOwnerIsShuttingDown() patch as it is no longer needed. llvm-svn: 149231
2012-01-30 15:41:31 +08:00
m_opaque_ap.reset(new SourceManagerImpl (target.GetSP()));
}
SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
{
if (&rhs == this)
return;
m_opaque_ap.reset(new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
}
const lldb::SBSourceManager &
SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
{
m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
return *this;
}
SBSourceManager::~SBSourceManager()
{
}
size_t
SBSourceManager::DisplaySourceLinesWithLineNumbers
(
const SBFileSpec &file,
uint32_t line,
uint32_t context_before,
uint32_t context_after,
const char *current_line_cstr,
SBStream &s
)
{
if (m_opaque_ap.get() == NULL)
return 0;
return m_opaque_ap->DisplaySourceLinesWithLineNumbers (file.ref(),
line,
context_before,
context_after,
current_line_cstr,
s.get());
}