Greg Clayton e1cd1be6d6 Switching back to using std::tr1::shared_ptr. We originally switched away
due to RTTI worries since llvm and clang don't use RTTI, but I was able to 
switch back with no issues as far as I can tell. Once the RTTI issue wasn't
an issue, we were looking for a way to properly track weak pointers to objects
to solve some of the threading issues we have been running into which naturally
led us back to std::tr1::weak_ptr. We also wanted the ability to make a shared 
pointer from just a pointer, which is also easily solved using the 
std::tr1::enable_shared_from_this class. 

The main reason for this move back is so we can start properly having weak
references to objects. Currently a lldb_private::Thread class has a refrence
to its parent lldb_private::Process. This doesn't work well when we now hand
out a SBThread object that contains a shared pointer to a lldb_private::Thread
as this SBThread can be held onto by external clients and if they end up
using one of these objects we can easily crash.

So the next task is to start adopting std::tr1::weak_ptr where ever it makes
sense which we can do with lldb_private::Debugger, lldb_private::Target,
lldb_private::Process, lldb_private::Thread, lldb_private::StackFrame, and
many more objects now that they are no longer using intrusive ref counted
pointer objects (you can't do std::tr1::weak_ptr functionality with intrusive
pointers).

llvm-svn: 149207
2012-01-29 20:56:30 +00:00

260 lines
7.4 KiB
C++

//===-- UnwindLLDB.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/Core/Module.h"
#include "lldb/Core/Log.h"
#include "lldb/Symbol/FuncUnwinders.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "UnwindLLDB.h"
#include "RegisterContextLLDB.h"
using namespace lldb;
using namespace lldb_private;
UnwindLLDB::UnwindLLDB (Thread &thread) :
Unwind (thread),
m_frames()
{
}
uint32_t
UnwindLLDB::DoGetFrameCount()
{
if (m_frames.empty())
{
//#define DEBUG_FRAME_SPEED 1
#if DEBUG_FRAME_SPEED
#define FRAME_COUNT 10000
TimeValue time_value (TimeValue::Now());
#endif
if (!AddFirstFrame ())
return 0;
ABI *abi = m_thread.GetProcess().GetABI().get();
while (AddOneMoreFrame (abi))
{
#if DEBUG_FRAME_SPEED
if ((m_frames.size() % FRAME_COUNT) == 0)
{
TimeValue now(TimeValue::Now());
uint64_t delta_t = now - time_value;
printf ("%u frames in %llu.%09llu ms (%g frames/sec)\n",
FRAME_COUNT,
delta_t / TimeValue::NanoSecPerSec,
delta_t % TimeValue::NanoSecPerSec,
(float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
time_value = now;
}
#endif
}
}
return m_frames.size ();
}
bool
UnwindLLDB::AddFirstFrame ()
{
// First, set up the 0th (initial) frame
CursorSP first_cursor_sp(new Cursor ());
RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
RegisterContextLLDBSP(),
first_cursor_sp->sctx,
0, *this));
if (reg_ctx_sp.get() == NULL)
return false;
if (!reg_ctx_sp->IsValid())
return false;
if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
return false;
if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
return false;
// Everything checks out, so release the auto pointer value and let the
// cursor own it in its shared pointer
first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
m_frames.push_back (first_cursor_sp);
return true;
}
// For adding a non-zero stack frame to m_frames.
bool
UnwindLLDB::AddOneMoreFrame (ABI *abi)
{
LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
CursorSP cursor_sp(new Cursor ());
// Frame zero is a little different
if (m_frames.size() == 0)
return false;
uint32_t cur_idx = m_frames.size ();
RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
cursor_sp->sctx,
cur_idx,
*this));
if (reg_ctx_sp.get() == NULL)
return false;
if (!reg_ctx_sp->IsValid())
{
if (log)
{
log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
return false;
}
if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
{
if (log)
{
log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
return false;
}
if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
{
if (log)
{
log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
return false;
}
if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
{
if (log)
{
log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
return false;
}
if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
{
if (log)
{
log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
return false;
}
if (!m_frames.empty())
{
if (m_frames.back()->start_pc == cursor_sp->start_pc)
{
if (m_frames.back()->cfa == cursor_sp->cfa)
return false; // Infinite loop where the current cursor is the same as the previous one...
else if (abi->StackUsesFrames())
{
// We might have a CFA that is not using the frame pointer and
// we want to validate that the frame pointer is valid.
if (reg_ctx_sp->GetFP() == 0)
return false;
}
}
}
cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
m_frames.push_back (cursor_sp);
return true;
}
bool
UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
{
if (m_frames.size() == 0)
{
if (!AddFirstFrame())
return false;
}
ABI *abi = m_thread.GetProcess().GetABI().get();
while (idx >= m_frames.size() && AddOneMoreFrame (abi))
;
if (idx < m_frames.size ())
{
cfa = m_frames[idx]->cfa;
pc = m_frames[idx]->start_pc;
return true;
}
return false;
}
lldb::RegisterContextSP
UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
{
lldb::RegisterContextSP reg_ctx_sp;
uint32_t idx = frame->GetConcreteFrameIndex ();
if (idx == 0)
{
return m_thread.GetRegisterContext();
}
if (m_frames.size() == 0)
{
if (!AddFirstFrame())
return reg_ctx_sp;
}
ABI *abi = m_thread.GetProcess().GetABI().get();
while (idx >= m_frames.size())
{
if (!AddOneMoreFrame (abi))
break;
}
const uint32_t num_frames = m_frames.size();
if (idx < num_frames)
{
Cursor *frame_cursor = m_frames[idx].get();
reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp->shared_from_this();
}
return reg_ctx_sp;
}
UnwindLLDB::RegisterContextLLDBSP
UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
{
RegisterContextLLDBSP reg_ctx_sp;
if (frame_num < m_frames.size())
reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
return reg_ctx_sp;
}
bool
UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc, uint32_t starting_frame_num)
{
int64_t frame_num = starting_frame_num;
if (frame_num >= m_frames.size())
return false;
while (frame_num >= 0)
{
if (m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc, false))
return true;
frame_num--;
}
return false;
}