#include "lldb/Target/UnwindLLDB.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/FuncUnwinders.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/RegisterContextUnwind.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
using namespace lldb;
using namespace lldb_private;
UnwindLLDB::UnwindLLDB(Thread &thread)
: Unwind(thread), m_frames(), m_unwind_complete(false),
m_user_supplied_trap_handler_functions() {
ProcessSP process_sp(thread.GetProcess());
if (process_sp) {
Args args;
process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames(args);
size_t count = args.GetArgumentCount();
for (size_t i = 0; i < count; i++) {
const char *func_name = args.GetArgumentAtIndex(i);
m_user_supplied_trap_handler_functions.push_back(ConstString(func_name));
}
}
}
uint32_t UnwindLLDB::DoGetFrameCount() {
if (!m_unwind_complete) {
#if DEBUG_FRAME_SPEED
#define FRAME_COUNT 10000
using namespace std::chrono;
auto time_value = steady_clock::now();
#endif
if (!AddFirstFrame())
return 0;
ProcessSP process_sp(m_thread.GetProcess());
ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
while (AddOneMoreFrame(abi)) {
#if DEBUG_FRAME_SPEED
if ((m_frames.size() % FRAME_COUNT) == 0) {
const auto now = steady_clock::now();
const auto delta_t = now - time_value;
printf("%u frames in %.9f ms (%g frames/sec)\n", FRAME_COUNT,
duration<double, std::milli>(delta_t).count(),
(float)FRAME_COUNT / duration<double>(delta_t).count());
time_value = now;
}
#endif
}
}
return m_frames.size();
}
bool UnwindLLDB::AddFirstFrame() {
if (m_frames.size() > 0)
return true;
ProcessSP process_sp(m_thread.GetProcess());
ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
CursorSP first_cursor_sp(new Cursor());
RegisterContextLLDBSP reg_ctx_sp(new RegisterContextUnwind(
m_thread, RegisterContextLLDBSP(), first_cursor_sp->sctx, 0, *this));
if (reg_ctx_sp.get() == nullptr)
goto unwind_done;
if (!reg_ctx_sp->IsValid())
goto unwind_done;
if (!reg_ctx_sp->GetCFA(first_cursor_sp->cfa))
goto unwind_done;
if (!reg_ctx_sp->ReadPC(first_cursor_sp->start_pc))
goto unwind_done;
first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
m_frames.push_back(first_cursor_sp);
UpdateUnwindPlanForFirstFrameIfInvalid(abi);
return true;
unwind_done:
Log *log = GetLog(LLDBLog::Unwind);
if (log) {
LLDB_LOGF(log, "th%d Unwind of this thread is complete.",
m_thread.GetIndexID());
}
m_unwind_complete = true;
return false;
}
UnwindLLDB::CursorSP UnwindLLDB::GetOneMoreFrame(ABI *abi) {
assert(m_frames.size() != 0 &&
"Get one more frame called with empty frame list");
if (m_unwind_complete)
return nullptr;
Log *log = GetLog(LLDBLog::Unwind);
CursorSP prev_frame = m_frames.back();
uint32_t cur_idx = m_frames.size();
CursorSP cursor_sp(new Cursor());
RegisterContextLLDBSP reg_ctx_sp(new RegisterContextUnwind(
m_thread, prev_frame->reg_ctx_lldb_sp, cursor_sp->sctx, cur_idx, *this));
uint64_t max_stack_depth = m_thread.GetMaxBacktraceDepth();
if (cur_idx >= max_stack_depth) {
LLDB_LOGF(log,
"%*sFrame %d unwound too many frames, assuming unwind has "
"gone astray, stopping.",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (reg_ctx_sp.get() == nullptr) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log, "%*sFrame %d did not get a RegisterContext, stopping.",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (!reg_ctx_sp->IsValid()) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log,
"%*sFrame %d invalid RegisterContext for this frame, "
"stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (!reg_ctx_sp->GetCFA(cursor_sp->cfa)) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log,
"%*sFrame %d did not get CFA for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
if (!reg_ctx_sp->IsTrapHandlerFrame()) {
if (!reg_ctx_sp->TryFallbackUnwindPlan() ||
!reg_ctx_sp->GetCFA(cursor_sp->cfa) ||
!abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log,
"%*sFrame %d did not get a valid CFA for this frame, "
"stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
} else {
LLDB_LOGF(log,
"%*sFrame %d had a bad CFA value but we switched the "
"UnwindPlan being used and got one that looks more "
"realistic.",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
}
}
}
if (!reg_ctx_sp->ReadPC(cursor_sp->start_pc)) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log,
"%*sFrame %d did not get PC for this frame, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (abi && !abi->CodeAddressIsValid(cursor_sp->start_pc) &&
!prev_frame->reg_ctx_lldb_sp->IsTrapHandlerFrame()) {
if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
return nullptr;
return GetOneMoreFrame(abi);
}
LLDB_LOGF(log, "%*sFrame %d did not get a valid PC, stopping stack walk",
cur_idx < 100 ? cur_idx : 100, "", cur_idx);
return nullptr;
}
if (prev_frame->start_pc == cursor_sp->start_pc &&
prev_frame->cfa == cursor_sp->cfa) {
LLDB_LOGF(log,
"th%d pc of this frame is the same as the previous frame and "
"CFAs for both frames are identical -- stopping unwind",
m_thread.GetIndexID());
return nullptr;
}
cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
return cursor_sp;
}
void UnwindLLDB::UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi) {
assert(m_frames.size() == 1 && "No. of cursor frames are not 1");
bool old_m_unwind_complete = m_unwind_complete;
CursorSP old_m_candidate_frame = m_candidate_frame;
AddOneMoreFrame(abi);
for (uint32_t i = 1; i < m_frames.size(); i++)
m_frames.pop_back();
m_unwind_complete = old_m_unwind_complete;
m_candidate_frame = old_m_candidate_frame;
}
bool UnwindLLDB::AddOneMoreFrame(ABI *abi) {
Log *log = GetLog(LLDBLog::Unwind);
if (m_frames.empty())
return false;
if (m_unwind_complete)
return false;
CursorSP new_frame = m_candidate_frame;
if (new_frame == nullptr)
new_frame = GetOneMoreFrame(abi);
if (new_frame == nullptr) {
LLDB_LOGF(log, "th%d Unwind of this thread is complete.",
m_thread.GetIndexID());
m_unwind_complete = true;
return false;
}
m_frames.push_back(new_frame);
m_candidate_frame = GetOneMoreFrame(abi);
if (m_candidate_frame)
return true;
if (!m_frames[m_frames.size() - 2]
->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
return true;
}
m_frames.pop_back();
CursorSP new_frame_v2 = GetOneMoreFrame(abi);
if (new_frame_v2 == nullptr) {
m_frames.push_back(new_frame);
return true;
}
m_frames.push_back(new_frame_v2);
m_candidate_frame = GetOneMoreFrame(abi);
if (m_candidate_frame) {
return m_frames[m_frames.size() - 2]->reg_ctx_lldb_sp->GetCFA(
m_frames[m_frames.size() - 2]->cfa);
}
m_frames.pop_back();
m_frames.push_back(new_frame);
return true;
}
bool UnwindLLDB::DoGetFrameInfoAtIndex(uint32_t idx, addr_t &cfa, addr_t &pc,
bool &behaves_like_zeroth_frame) {
if (m_frames.size() == 0) {
if (!AddFirstFrame())
return false;
}
ProcessSP process_sp(m_thread.GetProcess());
ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
while (idx >= m_frames.size() && AddOneMoreFrame(abi))
;
if (idx < m_frames.size()) {
cfa = m_frames[idx]->cfa;
pc = m_frames[idx]->start_pc;
if (idx == 0) {
behaves_like_zeroth_frame = true;
} else if (m_frames[idx - 1]->reg_ctx_lldb_sp->IsTrapHandlerFrame()) {
behaves_like_zeroth_frame = true;
} else if (m_frames[idx]->reg_ctx_lldb_sp->IsTrapHandlerFrame()) {
behaves_like_zeroth_frame = true;
} else if (m_frames[idx]->reg_ctx_lldb_sp->BehavesLikeZerothFrame()) {
behaves_like_zeroth_frame = true;
} else {
behaves_like_zeroth_frame = false;
}
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;
}
ProcessSP process_sp(m_thread.GetProcess());
ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
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;
}
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 ®loc,
uint32_t starting_frame_num, bool pc_reg) {
int64_t frame_num = starting_frame_num;
if (static_cast<size_t>(frame_num) >= m_frames.size())
return false;
if (pc_reg) {
UnwindLLDB::RegisterSearchResult result;
result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
lldb_regnum, regloc);
return result == UnwindLLDB::RegisterSearchResult::eRegisterFound;
}
while (frame_num >= 0) {
UnwindLLDB::RegisterSearchResult result;
result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
lldb_regnum, regloc);
if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
regloc.type ==
UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext) {
return true;
}
if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister &&
frame_num > 0) {
result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
lldb_regnum = regloc.location.register_number;
}
if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
return true;
if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
return false;
frame_num--;
}
return false;
}