Home | History | Annotate | Download | only in Utility
      1 //===-- RegisterContextLLDB.cpp --------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 
     11 #include "lldb/lldb-private.h"
     12 #include "lldb/Core/Address.h"
     13 #include "lldb/Core/AddressRange.h"
     14 #include "lldb/Core/DataBufferHeap.h"
     15 #include "lldb/Core/Log.h"
     16 #include "lldb/Core/Module.h"
     17 #include "lldb/Core/RegisterValue.h"
     18 #include "lldb/Core/Value.h"
     19 #include "lldb/Expression/DWARFExpression.h"
     20 #include "lldb/Symbol/DWARFCallFrameInfo.h"
     21 #include "lldb/Symbol/FuncUnwinders.h"
     22 #include "lldb/Symbol/Function.h"
     23 #include "lldb/Symbol/ObjectFile.h"
     24 #include "lldb/Symbol/SymbolContext.h"
     25 #include "lldb/Symbol/Symbol.h"
     26 #include "lldb/Target/ABI.h"
     27 #include "lldb/Target/ExecutionContext.h"
     28 #include "lldb/Target/Process.h"
     29 #include "lldb/Target/StackFrame.h"
     30 #include "lldb/Target/Target.h"
     31 #include "lldb/Target/Thread.h"
     32 #include "lldb/Target/DynamicLoader.h"
     33 
     34 #include "RegisterContextLLDB.h"
     35 
     36 using namespace lldb;
     37 using namespace lldb_private;
     38 
     39 RegisterContextLLDB::RegisterContextLLDB
     40 (
     41     Thread& thread,
     42     const SharedPtr &next_frame,
     43     SymbolContext& sym_ctx,
     44     uint32_t frame_number,
     45     UnwindLLDB& unwind_lldb
     46 ) :
     47     RegisterContext (thread, frame_number),
     48     m_thread(thread),
     49     m_fast_unwind_plan_sp (),
     50     m_full_unwind_plan_sp (),
     51     m_all_registers_available(false),
     52     m_frame_type (-1),
     53     m_cfa (LLDB_INVALID_ADDRESS),
     54     m_start_pc (),
     55     m_current_pc (),
     56     m_current_offset (0),
     57     m_current_offset_backed_up_one (0),
     58     m_sym_ctx(sym_ctx),
     59     m_sym_ctx_valid (false),
     60     m_frame_number (frame_number),
     61     m_registers(),
     62     m_parent_unwind (unwind_lldb)
     63 {
     64     m_sym_ctx.Clear(false);
     65     m_sym_ctx_valid = false;
     66 
     67     if (IsFrameZero ())
     68     {
     69         InitializeZerothFrame ();
     70     }
     71     else
     72     {
     73         InitializeNonZerothFrame ();
     74     }
     75 
     76     // This same code exists over in the GetFullUnwindPlanForFrame() but it may not have been executed yet
     77     if (IsFrameZero()
     78         || next_frame->m_frame_type == eSigtrampFrame
     79         || next_frame->m_frame_type == eDebuggerFrame)
     80     {
     81         m_all_registers_available = true;
     82     }
     83 }
     84 
     85 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the zeroth frame or currently
     86 // executing frame.
     87 
     88 void
     89 RegisterContextLLDB::InitializeZerothFrame()
     90 {
     91     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
     92     ExecutionContext exe_ctx(m_thread.shared_from_this());
     93     RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
     94 
     95     if (reg_ctx_sp.get() == NULL)
     96     {
     97         m_frame_type = eNotAValidFrame;
     98         return;
     99     }
    100 
    101     addr_t current_pc = reg_ctx_sp->GetPC();
    102 
    103     if (current_pc == LLDB_INVALID_ADDRESS)
    104     {
    105         m_frame_type = eNotAValidFrame;
    106         return;
    107     }
    108 
    109     Process *process = exe_ctx.GetProcessPtr();
    110 
    111     // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
    112     // this will strip bit zero in case we read a PC from memory or from the LR.
    113     // (which would be a no-op in frame 0 where we get it from the register set,
    114     // but still a good idea to make the call here for other ABIs that may exist.)
    115     ABI *abi = process->GetABI().get();
    116     if (abi)
    117         current_pc = abi->FixCodeAddress(current_pc);
    118 
    119     // Initialize m_current_pc, an Address object, based on current_pc, an addr_t.
    120     process->GetTarget().GetSectionLoadList().ResolveLoadAddress (current_pc, m_current_pc);
    121 
    122     // If we don't have a Module for some reason, we're not going to find symbol/function information - just
    123     // stick in some reasonable defaults and hope we can unwind past this frame.
    124     ModuleSP pc_module_sp (m_current_pc.GetModule());
    125     if (!m_current_pc.IsValid() || !pc_module_sp)
    126     {
    127         UnwindLogMsg ("using architectural default unwind method");
    128     }
    129 
    130     // We require that eSymbolContextSymbol be successfully filled in or this context is of no use to us.
    131     if (pc_module_sp.get()
    132         && (pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
    133     {
    134         m_sym_ctx_valid = true;
    135     }
    136 
    137     AddressRange addr_range;
    138     m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range);
    139 
    140     static ConstString g_sigtramp_name ("_sigtramp");
    141     if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == g_sigtramp_name) ||
    142         (m_sym_ctx.symbol   && m_sym_ctx.symbol->GetName()   == g_sigtramp_name))
    143     {
    144         m_frame_type = eSigtrampFrame;
    145     }
    146     else
    147     {
    148         // FIXME:  Detect eDebuggerFrame here.
    149         m_frame_type = eNormalFrame;
    150     }
    151 
    152     // If we were able to find a symbol/function, set addr_range to the bounds of that symbol/function.
    153     // else treat the current pc value as the start_pc and record no offset.
    154     if (addr_range.GetBaseAddress().IsValid())
    155     {
    156         m_start_pc = addr_range.GetBaseAddress();
    157         if (m_current_pc.GetSection() == m_start_pc.GetSection())
    158         {
    159             m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
    160         }
    161         else if (m_current_pc.GetModule() == m_start_pc.GetModule())
    162         {
    163             // This means that whatever symbol we kicked up isn't really correct
    164             // --- we should not cross section boundaries ... We really should NULL out
    165             // the function/symbol in this case unless there is a bad assumption
    166             // here due to inlined functions?
    167             m_current_offset = m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
    168         }
    169         m_current_offset_backed_up_one = m_current_offset;
    170     }
    171     else
    172     {
    173         m_start_pc = m_current_pc;
    174         m_current_offset = -1;
    175         m_current_offset_backed_up_one = -1;
    176     }
    177 
    178     // We've set m_frame_type and m_sym_ctx before these calls.
    179 
    180     m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
    181     m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
    182 
    183     UnwindPlan::RowSP active_row;
    184     int cfa_offset = 0;
    185     int row_register_kind = -1;
    186     if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    187     {
    188         active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
    189         row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
    190         if (active_row.get() && log)
    191         {
    192             StreamString active_row_strm;
    193             active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
    194             UnwindLogMsg ("%s", active_row_strm.GetString().c_str());
    195         }
    196     }
    197 
    198     if (!active_row.get())
    199     {
    200         m_frame_type = eNotAValidFrame;
    201         return;
    202     }
    203 
    204 
    205     addr_t cfa_regval = LLDB_INVALID_ADDRESS;
    206     if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval))
    207     {
    208         m_frame_type = eNotAValidFrame;
    209         return;
    210     }
    211 
    212     cfa_offset = active_row->GetCFAOffset ();
    213     m_cfa = cfa_regval + cfa_offset;
    214 
    215     UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset);
    216     UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64 " using %s UnwindPlan",
    217             (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()),
    218             (uint64_t) m_cfa,
    219             m_full_unwind_plan_sp->GetSourceName().GetCString());
    220 }
    221 
    222 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the RegisterContextLLDB "below" it
    223 // to provide things like its current pc value.
    224 
    225 void
    226 RegisterContextLLDB::InitializeNonZerothFrame()
    227 {
    228     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
    229     if (IsFrameZero ())
    230     {
    231         m_frame_type = eNotAValidFrame;
    232         return;
    233     }
    234 
    235     if (!GetNextFrame().get() || !GetNextFrame()->IsValid())
    236     {
    237         m_frame_type = eNotAValidFrame;
    238         return;
    239     }
    240     if (!m_thread.GetRegisterContext())
    241     {
    242         m_frame_type = eNotAValidFrame;
    243         return;
    244     }
    245 
    246     addr_t pc;
    247     if (!ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
    248     {
    249         UnwindLogMsg ("could not get pc value");
    250         m_frame_type = eNotAValidFrame;
    251         return;
    252     }
    253 
    254     if (log)
    255     {
    256         UnwindLogMsg ("pc = 0x%16.16" PRIx64, pc);
    257         addr_t reg_val;
    258         if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
    259             UnwindLogMsg ("fp = 0x%16.16" PRIx64, reg_val);
    260         if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
    261             UnwindLogMsg ("sp = 0x%16.16" PRIx64, reg_val);
    262     }
    263 
    264     // A pc of 0x0 means it's the end of the stack crawl
    265     if (pc == 0)
    266     {
    267         m_frame_type = eNotAValidFrame;
    268         return;
    269     }
    270 
    271     ExecutionContext exe_ctx(m_thread.shared_from_this());
    272     Process *process = exe_ctx.GetProcessPtr();
    273     // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
    274     // this will strip bit zero in case we read a PC from memory or from the LR.
    275     ABI *abi = process->GetABI().get();
    276     if (abi)
    277         pc = abi->FixCodeAddress(pc);
    278 
    279     process->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, m_current_pc);
    280 
    281     // If we don't have a Module for some reason, we're not going to find symbol/function information - just
    282     // stick in some reasonable defaults and hope we can unwind past this frame.
    283     ModuleSP pc_module_sp (m_current_pc.GetModule());
    284     if (!m_current_pc.IsValid() || !pc_module_sp)
    285     {
    286         UnwindLogMsg ("using architectural default unwind method");
    287 
    288         // Test the pc value to see if we know it's in an unmapped/non-executable region of memory.
    289         uint32_t permissions;
    290         if (process->GetLoadAddressPermissions(pc, permissions)
    291             && (permissions & ePermissionsExecutable) == 0)
    292         {
    293             // If this is the second frame off the stack, we may have unwound the first frame
    294             // incorrectly.  But using the architecture default unwind plan may get us back on
    295             // track -- albeit possibly skipping a real frame.  Give this frame a clearly-invalid
    296             // pc and see if we can get any further.
    297             if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsFrameZero())
    298             {
    299                 UnwindLogMsg ("had a pc of 0x%" PRIx64 " which is not in executable memory but on frame 1 -- allowing it once.",
    300                          (uint64_t) pc);
    301                 m_frame_type = eSkipFrame;
    302             }
    303             else
    304             {
    305                 // anywhere other than the second frame, a non-executable pc means we're off in the weeds -- stop now.
    306                 m_frame_type = eNotAValidFrame;
    307                 return;
    308             }
    309         }
    310 
    311         if (abi)
    312         {
    313             m_fast_unwind_plan_sp.reset ();
    314             m_full_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
    315             abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp);
    316             if (m_frame_type != eSkipFrame)  // don't override eSkipFrame
    317             {
    318                 m_frame_type = eNormalFrame;
    319             }
    320             m_all_registers_available = false;
    321             m_current_offset = -1;
    322             m_current_offset_backed_up_one = -1;
    323             addr_t cfa_regval = LLDB_INVALID_ADDRESS;
    324             int row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
    325             UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0);
    326             if (row.get())
    327             {
    328                 uint32_t cfa_regnum = row->GetCFARegister();
    329                 int cfa_offset = row->GetCFAOffset();
    330                 if (!ReadGPRValue (row_register_kind, cfa_regnum, cfa_regval))
    331                 {
    332                     UnwindLogMsg ("failed to get cfa value");
    333                     if (m_frame_type != eSkipFrame)   // don't override eSkipFrame
    334                     {
    335                         m_frame_type = eNormalFrame;
    336                     }
    337                     return;
    338                 }
    339                 m_cfa = cfa_regval + cfa_offset;
    340 
    341                 // A couple of sanity checks..
    342                 if (cfa_regval == LLDB_INVALID_ADDRESS || cfa_regval == 0 || cfa_regval == 1)
    343                 {
    344                     UnwindLogMsg ("could not find a valid cfa address");
    345                     m_frame_type = eNotAValidFrame;
    346                     return;
    347                 }
    348 
    349                 // cfa_regval should point into the stack memory; if we can query memory region permissions,
    350                 // see if the memory is allocated & readable.
    351                 if (process->GetLoadAddressPermissions(cfa_regval, permissions)
    352                     && (permissions & ePermissionsReadable) == 0)
    353                 {
    354                     m_frame_type = eNotAValidFrame;
    355                     return;
    356                 }
    357             }
    358             else
    359             {
    360                 UnwindLogMsg ("could not find a row for function offset zero");
    361                 m_frame_type = eNotAValidFrame;
    362                 return;
    363             }
    364 
    365             UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64, (uint64_t) m_cfa);
    366             return;
    367         }
    368         m_frame_type = eNotAValidFrame;
    369         return;
    370     }
    371 
    372     // We require that eSymbolContextSymbol be successfully filled in or this context is of no use to us.
    373     if ((pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
    374     {
    375         m_sym_ctx_valid = true;
    376     }
    377 
    378     AddressRange addr_range;
    379     if (!m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
    380     {
    381         m_sym_ctx_valid = false;
    382     }
    383 
    384     bool decr_pc_and_recompute_addr_range = false;
    385 
    386     // If the symbol lookup failed...
    387     if (m_sym_ctx_valid == false)
    388        decr_pc_and_recompute_addr_range = true;
    389 
    390     // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp),
    391     // and our "current" pc is the start of a function...
    392     if (m_sym_ctx_valid
    393         && GetNextFrame()->m_frame_type != eSigtrampFrame
    394         && GetNextFrame()->m_frame_type != eDebuggerFrame
    395         && addr_range.GetBaseAddress().IsValid()
    396         && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection()
    397         && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset())
    398     {
    399         decr_pc_and_recompute_addr_range = true;
    400     }
    401 
    402     // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc"
    403     // value is pointing to the next function, e.g. if a function ends with a CALL instruction.
    404     // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function
    405     // to the ABI plugin and consult that.
    406     if (decr_pc_and_recompute_addr_range)
    407     {
    408         Address temporary_pc(m_current_pc);
    409         temporary_pc.SetOffset(m_current_pc.GetOffset() - 1);
    410         m_sym_ctx.Clear(false);
    411         m_sym_ctx_valid = false;
    412         if ((pc_module_sp->ResolveSymbolContextForAddress (temporary_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
    413         {
    414             m_sym_ctx_valid = true;
    415         }
    416         if (!m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false,  addr_range))
    417         {
    418             m_sym_ctx_valid = false;
    419         }
    420     }
    421 
    422     // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function.
    423     // else treat the current pc value as the start_pc and record no offset.
    424     if (addr_range.GetBaseAddress().IsValid())
    425     {
    426         m_start_pc = addr_range.GetBaseAddress();
    427         m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
    428         m_current_offset_backed_up_one = m_current_offset;
    429         if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0)
    430         {
    431             m_current_offset_backed_up_one--;
    432             if (m_sym_ctx_valid)
    433                 m_current_pc.SetOffset(m_current_pc.GetOffset() - 1);
    434         }
    435     }
    436     else
    437     {
    438         m_start_pc = m_current_pc;
    439         m_current_offset = -1;
    440         m_current_offset_backed_up_one = -1;
    441     }
    442 
    443     static ConstString sigtramp_name ("_sigtramp");
    444     if ((m_sym_ctx.function && m_sym_ctx.function->GetMangled().GetMangledName() == sigtramp_name)
    445         || (m_sym_ctx.symbol && m_sym_ctx.symbol->GetMangled().GetMangledName() == sigtramp_name))
    446     {
    447         m_frame_type = eSigtrampFrame;
    448     }
    449     else
    450     {
    451         // FIXME:  Detect eDebuggerFrame here.
    452         if (m_frame_type != eSkipFrame) // don't override eSkipFrame
    453         {
    454             m_frame_type = eNormalFrame;
    455         }
    456     }
    457 
    458     // We've set m_frame_type and m_sym_ctx before this call.
    459     m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
    460 
    461     UnwindPlan::RowSP active_row;
    462     int cfa_offset = 0;
    463     int row_register_kind = -1;
    464 
    465     // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get
    466     // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.)
    467 
    468     if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    469     {
    470         active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
    471         row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind ();
    472         if (active_row.get() && log)
    473         {
    474             StreamString active_row_strm;
    475             active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
    476             UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
    477         }
    478     }
    479     else
    480     {
    481         m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
    482         if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    483         {
    484             active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
    485             row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
    486             if (active_row.get() && log)
    487             {
    488                 StreamString active_row_strm;
    489                 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
    490                 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
    491             }
    492         }
    493     }
    494 
    495     if (!active_row.get())
    496     {
    497         m_frame_type = eNotAValidFrame;
    498         return;
    499     }
    500 
    501     addr_t cfa_regval = LLDB_INVALID_ADDRESS;
    502     if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval))
    503     {
    504         UnwindLogMsg ("failed to get cfa reg %d/%d", row_register_kind, active_row->GetCFARegister());
    505         m_frame_type = eNotAValidFrame;
    506         return;
    507     }
    508 
    509     cfa_offset = active_row->GetCFAOffset ();
    510     m_cfa = cfa_regval + cfa_offset;
    511 
    512     UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset);
    513 
    514     // A couple of sanity checks..
    515     if (cfa_regval == LLDB_INVALID_ADDRESS || cfa_regval == 0 || cfa_regval == 1)
    516     {
    517         UnwindLogMsg ("could not find a valid cfa address");
    518         m_frame_type = eNotAValidFrame;
    519         return;
    520     }
    521 
    522     // If we have a bad stack setup, we can get the same CFA value multiple times -- or even
    523     // more devious, we can actually oscillate between two CFA values.  Detect that here and
    524     // break out to avoid a possible infinite loop in lldb trying to unwind the stack.
    525     addr_t next_frame_cfa;
    526     addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
    527     if (GetNextFrame().get() && GetNextFrame()->GetCFA(next_frame_cfa))
    528     {
    529         bool repeating_frames = false;
    530         if (next_frame_cfa == m_cfa)
    531         {
    532             repeating_frames = true;
    533         }
    534         else
    535         {
    536             if (GetNextFrame()->GetNextFrame() && GetNextFrame()->GetNextFrame()->GetCFA(next_next_frame_cfa)
    537                 && next_next_frame_cfa == m_cfa)
    538             {
    539                 repeating_frames = true;
    540             }
    541         }
    542         if (repeating_frames && abi->FunctionCallsChangeCFA())
    543         {
    544             UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping");
    545             m_frame_type = eNotAValidFrame;
    546             return;
    547         }
    548     }
    549 
    550     UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64,
    551             (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa);
    552 }
    553 
    554 
    555 bool
    556 RegisterContextLLDB::IsFrameZero () const
    557 {
    558     return m_frame_number == 0;
    559 }
    560 
    561 
    562 // Find a fast unwind plan for this frame, if possible.
    563 //
    564 // On entry to this method,
    565 //
    566 //   1. m_frame_type should already be set to eSigtrampFrame/eDebuggerFrame if either of those are correct,
    567 //   2. m_sym_ctx should already be filled in, and
    568 //   3. m_current_pc should have the current pc value for this frame
    569 //   4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
    570 
    571 UnwindPlanSP
    572 RegisterContextLLDB::GetFastUnwindPlanForFrame ()
    573 {
    574     UnwindPlanSP unwind_plan_sp;
    575     ModuleSP pc_module_sp (m_current_pc.GetModule());
    576 
    577     if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
    578         return unwind_plan_sp;
    579 
    580     if (IsFrameZero ())
    581         return unwind_plan_sp;
    582 
    583     FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx));
    584     if (!func_unwinders_sp)
    585         return unwind_plan_sp;
    586 
    587     // If we're in _sigtramp(), unwinding past this frame requires special knowledge.
    588     if (m_frame_type == eSigtrampFrame || m_frame_type == eDebuggerFrame)
    589         return unwind_plan_sp;
    590 
    591     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (m_thread);
    592     if (unwind_plan_sp)
    593     {
    594         if (unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    595         {
    596             Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
    597             if (log && log->GetVerbose())
    598             {
    599                 if (m_fast_unwind_plan_sp)
    600                     UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan");
    601                 else
    602                     UnwindLogMsgVerbose ("frame");
    603             }
    604             m_frame_type = eNormalFrame;
    605             return unwind_plan_sp;
    606         }
    607         else
    608         {
    609             unwind_plan_sp.reset();
    610         }
    611     }
    612     return unwind_plan_sp;
    613 }
    614 
    615 // On entry to this method,
    616 //
    617 //   1. m_frame_type should already be set to eSigtrampFrame/eDebuggerFrame if either of those are correct,
    618 //   2. m_sym_ctx should already be filled in, and
    619 //   3. m_current_pc should have the current pc value for this frame
    620 //   4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
    621 
    622 UnwindPlanSP
    623 RegisterContextLLDB::GetFullUnwindPlanForFrame ()
    624 {
    625     UnwindPlanSP unwind_plan_sp;
    626     UnwindPlanSP arch_default_unwind_plan_sp;
    627     ExecutionContext exe_ctx(m_thread.shared_from_this());
    628     Process *process = exe_ctx.GetProcessPtr();
    629     ABI *abi = process ? process->GetABI().get() : NULL;
    630     if (abi)
    631     {
    632         arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
    633         abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
    634     }
    635 
    636     bool behaves_like_zeroth_frame = false;
    637     if (IsFrameZero ()
    638         || GetNextFrame()->m_frame_type == eSigtrampFrame
    639         || GetNextFrame()->m_frame_type == eDebuggerFrame)
    640     {
    641         behaves_like_zeroth_frame = true;
    642         // If this frame behaves like a 0th frame (currently executing or
    643         // interrupted asynchronously), all registers can be retrieved.
    644         m_all_registers_available = true;
    645     }
    646 
    647     // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0
    648     // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan
    649     // Also, if this Process can report on memory region attributes, any non-executable region means
    650     // we jumped through a bad function pointer - handle the same way as 0x0.
    651     // Note, if the symbol context has a function for the symbol, then we don't need to do this check.
    652 
    653     if ((!m_sym_ctx_valid  || m_sym_ctx.function == NULL) && behaves_like_zeroth_frame && m_current_pc.IsValid())
    654     {
    655         uint32_t permissions;
    656         addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr());
    657         if (current_pc_addr == 0
    658             || (process->GetLoadAddressPermissions(current_pc_addr, permissions)
    659                 && (permissions & ePermissionsExecutable) == 0))
    660         {
    661             unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
    662             abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
    663             m_frame_type = eNormalFrame;
    664             return unwind_plan_sp;
    665         }
    666     }
    667 
    668     // No Module for the current pc, try using the architecture default unwind.
    669     ModuleSP pc_module_sp (m_current_pc.GetModule());
    670     if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
    671     {
    672         m_frame_type = eNormalFrame;
    673         return arch_default_unwind_plan_sp;
    674     }
    675 
    676     FuncUnwindersSP func_unwinders_sp;
    677     if (m_sym_ctx_valid)
    678     {
    679         func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
    680     }
    681 
    682     // No FuncUnwinders available for this pc (i.e. a stripped function symbol and -fomit-frame-pointer).
    683     // Try using the eh_frame information relative to the current PC,
    684     // and finally fall back on the architectural default unwind.
    685     if (!func_unwinders_sp)
    686     {
    687         DWARFCallFrameInfo *eh_frame = pc_module_sp && pc_module_sp->GetObjectFile() ?
    688             pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo() : nullptr;
    689 
    690         m_frame_type = eNormalFrame;
    691         if (eh_frame && m_current_pc.IsValid())
    692         {
    693             unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
    694             // Even with -fomit-frame-pointer, we can try eh_frame to get back on track.
    695             if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp))
    696                 return unwind_plan_sp;
    697             else
    698                 unwind_plan_sp.reset();
    699         }
    700         return arch_default_unwind_plan_sp;
    701     }
    702 
    703     // If we're in _sigtramp(), unwinding past this frame requires special knowledge.  On Mac OS X this knowledge
    704     // is properly encoded in the eh_frame section, so prefer that if available.
    705     // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of
    706     // how to unwind out of sigtramp.
    707     if (m_frame_type == eSigtrampFrame)
    708     {
    709         m_fast_unwind_plan_sp.reset();
    710         unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
    711         if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    712             return unwind_plan_sp;
    713     }
    714 
    715     // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero
    716     // This comes up if we have hand-written functions in a Module and hand-written eh_frame.  The assembly
    717     // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the
    718     // right thing.  It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous
    719     // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites).
    720     // But there is not.
    721     if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx))
    722     {
    723         unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
    724         if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    725         {
    726             UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it",
    727                            unwind_plan_sp->GetSourceName().GetCString());
    728             return unwind_plan_sp;
    729         }
    730     }
    731 
    732     // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions
    733     if (behaves_like_zeroth_frame)
    734     {
    735         unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (m_thread);
    736         if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    737         {
    738             UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
    739             return unwind_plan_sp;
    740         }
    741     }
    742 
    743     // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites
    744     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
    745     if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    746     {
    747         UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
    748         return unwind_plan_sp;
    749     }
    750 
    751     // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've
    752     // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible.
    753     unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (m_thread);
    754     if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
    755     {
    756         UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
    757         return unwind_plan_sp;
    758     }
    759 
    760     // If nothing else, use the architectural default UnwindPlan and hope that does the job.
    761     UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString());
    762     return arch_default_unwind_plan_sp;
    763 }
    764 
    765 
    766 void
    767 RegisterContextLLDB::InvalidateAllRegisters ()
    768 {
    769     m_frame_type = eNotAValidFrame;
    770 }
    771 
    772 size_t
    773 RegisterContextLLDB::GetRegisterCount ()
    774 {
    775     return m_thread.GetRegisterContext()->GetRegisterCount();
    776 }
    777 
    778 const RegisterInfo *
    779 RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg)
    780 {
    781     return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg);
    782 }
    783 
    784 size_t
    785 RegisterContextLLDB::GetRegisterSetCount ()
    786 {
    787     return m_thread.GetRegisterContext()->GetRegisterSetCount ();
    788 }
    789 
    790 const RegisterSet *
    791 RegisterContextLLDB::GetRegisterSet (size_t reg_set)
    792 {
    793     return m_thread.GetRegisterContext()->GetRegisterSet (reg_set);
    794 }
    795 
    796 uint32_t
    797 RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num)
    798 {
    799     return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num);
    800 }
    801 
    802 bool
    803 RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
    804                                                             const RegisterInfo *reg_info,
    805                                                             RegisterValue &value)
    806 {
    807     if (!IsValid())
    808         return false;
    809     bool success = false;
    810 
    811     switch (regloc.type)
    812     {
    813     case UnwindLLDB::RegisterLocation::eRegisterInRegister:
    814         {
    815             const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
    816 
    817             if (!other_reg_info)
    818                 return false;
    819 
    820             if (IsFrameZero ())
    821             {
    822                 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value);
    823             }
    824             else
    825             {
    826                 success = GetNextFrame()->ReadRegister (other_reg_info, value);
    827             }
    828         }
    829         break;
    830     case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
    831         success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size);
    832         break;
    833 
    834     case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
    835         break;
    836     case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
    837         assert ("FIXME debugger inferior function call unwind");
    838         break;
    839     case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
    840         {
    841             Error error (ReadRegisterValueFromMemory(reg_info,
    842                                                      regloc.location.target_memory_location,
    843                                                      reg_info->byte_size,
    844                                                      value));
    845             success = error.Success();
    846         }
    847         break;
    848     default:
    849         assert ("Unknown RegisterLocation type.");
    850         break;
    851     }
    852     return success;
    853 }
    854 
    855 bool
    856 RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
    857                                                            const RegisterInfo *reg_info,
    858                                                            const RegisterValue &value)
    859 {
    860     if (!IsValid())
    861         return false;
    862 
    863     bool success = false;
    864 
    865     switch (regloc.type)
    866     {
    867         case UnwindLLDB::RegisterLocation::eRegisterInRegister:
    868             {
    869                 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
    870                 if (IsFrameZero ())
    871                 {
    872                     success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value);
    873                 }
    874                 else
    875                 {
    876                     success = GetNextFrame()->WriteRegister (other_reg_info, value);
    877                 }
    878             }
    879             break;
    880         case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
    881         case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
    882             break;
    883         case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
    884             assert ("FIXME debugger inferior function call unwind");
    885             break;
    886         case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
    887             {
    888                 Error error (WriteRegisterValueToMemory (reg_info,
    889                                                          regloc.location.target_memory_location,
    890                                                          reg_info->byte_size,
    891                                                          value));
    892                 success = error.Success();
    893             }
    894             break;
    895         default:
    896             assert ("Unknown RegisterLocation type.");
    897             break;
    898     }
    899     return success;
    900 }
    901 
    902 
    903 bool
    904 RegisterContextLLDB::IsValid () const
    905 {
    906     return m_frame_type != eNotAValidFrame;
    907 }
    908 
    909 // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther
    910 // up the stack if we keep looking.  It's always the second frame in an unwind (i.e. the first frame after
    911 // frame zero) where unwinding can be the trickiest.  Ideally we'll mark up this frame in some way so the
    912 // user knows we're displaying bad data and we may have skipped one frame of their real program in the
    913 // process of getting back on track.
    914 
    915 bool
    916 RegisterContextLLDB::IsSkipFrame () const
    917 {
    918     return m_frame_type == eSkipFrame;
    919 }
    920 
    921 // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value?
    922 
    923 enum UnwindLLDB::RegisterSearchResult
    924 RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc)
    925 {
    926     // Have we already found this register location?
    927     if (!m_registers.empty())
    928     {
    929         std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator;
    930         iterator = m_registers.find (lldb_regnum);
    931         if (iterator != m_registers.end())
    932         {
    933             regloc = iterator->second;
    934             UnwindLogMsg ("supplying caller's saved reg %d's location, cached", lldb_regnum);
    935             return UnwindLLDB::RegisterSearchResult::eRegisterFound;
    936         }
    937     }
    938 
    939     uint32_t sp_regnum = LLDB_INVALID_REGNUM;
    940     uint32_t pc_regnum = LLDB_INVALID_REGNUM;
    941     m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, eRegisterKindLLDB, sp_regnum);
    942     m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, eRegisterKindLLDB, pc_regnum);
    943 
    944     // Are we looking for the CALLER's stack pointer?  The stack pointer is defined to be the same as THIS frame's
    945     // CFA so just return the CFA value.  This is true on x86-32/x86-64 at least.
    946     if (sp_regnum != LLDB_INVALID_REGNUM && sp_regnum == lldb_regnum)
    947     {
    948         // make sure we won't lose precision copying an addr_t (m_cfa) into a uint64_t (.inferred_value)
    949         assert (sizeof (addr_t) <= sizeof (uint64_t));
    950         regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
    951         regloc.location.inferred_value = m_cfa;
    952         m_registers[lldb_regnum] = regloc;
    953         UnwindLogMsg ("supplying caller's stack pointer (%d) value, computed from CFA", lldb_regnum);
    954         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
    955     }
    956 
    957     // Look through the available UnwindPlans for the register location.
    958 
    959     UnwindPlan::Row::RegisterLocation unwindplan_regloc;
    960     bool have_unwindplan_regloc = false;
    961     RegisterKind unwindplan_registerkind = (RegisterKind)-1;
    962 
    963     if (m_fast_unwind_plan_sp)
    964     {
    965         UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
    966         unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind ();
    967         uint32_t row_regnum;
    968         if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum))
    969         {
    970             UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme",
    971                     lldb_regnum, (int) unwindplan_registerkind);
    972             return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
    973         }
    974         if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc))
    975         {
    976             UnwindLogMsg ("supplying caller's saved reg %d's location using FastUnwindPlan", lldb_regnum);
    977             have_unwindplan_regloc = true;
    978         }
    979     }
    980 
    981     if (!have_unwindplan_regloc)
    982     {
    983         // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet
    984         if (!m_full_unwind_plan_sp)
    985             m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
    986 
    987         if (m_full_unwind_plan_sp)
    988         {
    989             UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
    990             unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind ();
    991             uint32_t row_regnum;
    992             bool row_register_rewritten_to_return_address_reg = false;
    993 
    994             // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm),
    995             // look for the return address register number in the UnwindPlan's row.
    996             if (lldb_regnum == pc_regnum && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM)
    997             {
    998                row_regnum = m_full_unwind_plan_sp->GetReturnAddressRegister();
    999                row_register_rewritten_to_return_address_reg = true;
   1000                UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting reg %d instead",
   1001                        row_regnum);
   1002             }
   1003             else
   1004             {
   1005                 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum))
   1006                 {
   1007                     if (unwindplan_registerkind == eRegisterKindGeneric)
   1008                         UnwindLogMsg ("could not convert lldb regnum %d into eRegisterKindGeneric reg numbering scheme", lldb_regnum);
   1009                     else
   1010                         UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme",
   1011                                 lldb_regnum, (int) unwindplan_registerkind);
   1012                     return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1013                 }
   1014             }
   1015 
   1016             if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc))
   1017             {
   1018                 have_unwindplan_regloc = true;
   1019                 UnwindLogMsg ("supplying caller's saved reg %d's location using %s UnwindPlan", lldb_regnum,
   1020                               m_full_unwind_plan_sp->GetSourceName().GetCString());
   1021             }
   1022 
   1023             // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and
   1024             // it hasn't been saved anywhere yet -- that is, it's still live in the actual register.
   1025             // Handle this specially.
   1026 
   1027             if (have_unwindplan_regloc == false
   1028                 && row_register_rewritten_to_return_address_reg == true
   1029                 && IsFrameZero()
   1030                 && row_regnum != LLDB_INVALID_REGNUM)
   1031             {
   1032                 uint32_t ra_regnum_in_lldb_reg_numbering;
   1033                 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, row_regnum, eRegisterKindLLDB, ra_regnum_in_lldb_reg_numbering))
   1034                 {
   1035                     lldb_private::UnwindLLDB::RegisterLocation new_regloc;
   1036                     new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
   1037                     new_regloc.location.register_number = ra_regnum_in_lldb_reg_numbering;
   1038                     m_registers[lldb_regnum] = new_regloc;
   1039                     regloc = new_regloc;
   1040                     UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0, saved in %d", lldb_regnum, ra_regnum_in_lldb_reg_numbering);
   1041                     return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1042                 }
   1043             }
   1044 
   1045             // If this architecture stores the return address in a register (it defines a Return Address register)
   1046             // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the
   1047             // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this
   1048             // is an impossible situation and the instruction emulation code has likely been misled.
   1049             // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the
   1050             // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so
   1051             // the stack walk can get past this point.
   1052 
   1053             // Special note:  If the Full UnwindPlan was generated from the compiler, don't second-guess it
   1054             // when we're at a call site location.
   1055 
   1056             // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering
   1057             uint32_t arch_default_ra_regnum = LLDB_INVALID_REGNUM;
   1058             if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, unwindplan_registerkind, arch_default_ra_regnum)
   1059                 && arch_default_ra_regnum != LLDB_INVALID_REGNUM
   1060                 && pc_regnum != LLDB_INVALID_REGNUM
   1061                 && pc_regnum == lldb_regnum
   1062                 && unwindplan_regloc.IsInOtherRegister()
   1063                 && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum
   1064                 && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes
   1065                 && !m_all_registers_available)
   1066             {
   1067                 UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame",
   1068                               m_full_unwind_plan_sp->GetSourceName().GetCString());
   1069 
   1070                 // Throw away the full unwindplan; install the arch default unwindplan
   1071                 InvalidateFullUnwindPlan();
   1072 
   1073                 // Now re-fetch the pc value we're searching for
   1074                 uint32_t arch_default_pc_reg = LLDB_INVALID_REGNUM;
   1075                 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
   1076                 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, m_full_unwind_plan_sp->GetRegisterKind(), arch_default_pc_reg)
   1077                     && arch_default_pc_reg != LLDB_INVALID_REGNUM
   1078                     && active_row
   1079                     && active_row->GetRegisterInfo (arch_default_pc_reg, unwindplan_regloc))
   1080                 {
   1081                     have_unwindplan_regloc = true;
   1082                 }
   1083                 else
   1084                 {
   1085                     have_unwindplan_regloc = false;
   1086                 }
   1087             }
   1088         }
   1089     }
   1090 
   1091 
   1092     ExecutionContext exe_ctx(m_thread.shared_from_this());
   1093     Process *process = exe_ctx.GetProcessPtr();
   1094     if (have_unwindplan_regloc == false)
   1095     {
   1096         // If a volatile register is being requested, we don't want to forward the next frame's register contents
   1097         // up the stack -- the register is not retrievable at this frame.
   1098         ABI *abi = process ? process->GetABI().get() : NULL;
   1099         if (abi)
   1100         {
   1101             const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
   1102             if (reg_info && abi->RegisterIsVolatile (reg_info))
   1103             {
   1104                 UnwindLogMsg ("did not supply reg location for %d because it is volatile", lldb_regnum);
   1105                 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
   1106             }
   1107         }
   1108 
   1109         if (IsFrameZero ())
   1110         {
   1111             // This is frame 0 - we should return the actual live register context value
   1112             lldb_private::UnwindLLDB::RegisterLocation new_regloc;
   1113             new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
   1114             new_regloc.location.register_number = lldb_regnum;
   1115             m_registers[lldb_regnum] = new_regloc;
   1116             regloc = new_regloc;
   1117             UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0", lldb_regnum);
   1118             return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1119         }
   1120         else
   1121         UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
   1122         return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1123     }
   1124 
   1125     // unwindplan_regloc has valid contents about where to retrieve the register
   1126     if (unwindplan_regloc.IsUnspecified())
   1127     {
   1128         lldb_private::UnwindLLDB::RegisterLocation new_regloc;
   1129         new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
   1130         m_registers[lldb_regnum] = new_regloc;
   1131         UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
   1132         return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1133     }
   1134 
   1135     if (unwindplan_regloc.IsSame())
   1136     {
   1137         if (IsFrameZero ())
   1138         {
   1139             UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
   1140             return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1141         }
   1142         else
   1143         {
   1144             return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1145         }
   1146     }
   1147 
   1148     if (unwindplan_regloc.IsCFAPlusOffset())
   1149     {
   1150         int offset = unwindplan_regloc.GetOffset();
   1151         regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
   1152         regloc.location.inferred_value = m_cfa + offset;
   1153         m_registers[lldb_regnum] = regloc;
   1154         UnwindLogMsg ("supplying caller's register %d, value is CFA plus offset", lldb_regnum);
   1155         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1156     }
   1157 
   1158     if (unwindplan_regloc.IsAtCFAPlusOffset())
   1159     {
   1160         int offset = unwindplan_regloc.GetOffset();
   1161         regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
   1162         regloc.location.target_memory_location = m_cfa + offset;
   1163         m_registers[lldb_regnum] = regloc;
   1164         UnwindLogMsg ("supplying caller's register %d from the stack, saved at CFA plus offset", lldb_regnum);
   1165         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1166     }
   1167 
   1168     if (unwindplan_regloc.IsInOtherRegister())
   1169     {
   1170         uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
   1171         uint32_t row_regnum_in_lldb;
   1172         if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, unwindplan_regnum, eRegisterKindLLDB, row_regnum_in_lldb))
   1173         {
   1174             UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
   1175             return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1176         }
   1177         regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
   1178         regloc.location.register_number = row_regnum_in_lldb;
   1179         m_registers[lldb_regnum] = regloc;
   1180         UnwindLogMsg ("supplying caller's register %d, saved in register %d", lldb_regnum, row_regnum_in_lldb);
   1181         return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1182     }
   1183 
   1184     if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression())
   1185     {
   1186         DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(),
   1187                                  unwindplan_regloc.GetDWARFExpressionLength(),
   1188                                  process->GetByteOrder(), process->GetAddressByteSize());
   1189         DWARFExpression dwarfexpr (dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength());
   1190         dwarfexpr.SetRegisterKind (unwindplan_registerkind);
   1191         Value result;
   1192         Error error;
   1193         if (dwarfexpr.Evaluate (&exe_ctx, NULL, NULL, this, 0, NULL, result, &error))
   1194         {
   1195             addr_t val;
   1196             val = result.GetScalar().ULongLong();
   1197             if (unwindplan_regloc.IsDWARFExpression())
   1198              {
   1199                 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
   1200                 regloc.location.inferred_value = val;
   1201                 m_registers[lldb_regnum] = regloc;
   1202                 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsDWARFExpression)", lldb_regnum);
   1203                 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1204             }
   1205             else
   1206             {
   1207                 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
   1208                 regloc.location.target_memory_location = val;
   1209                 m_registers[lldb_regnum] = regloc;
   1210                 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsAtDWARFExpression)", lldb_regnum);
   1211                 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
   1212             }
   1213         }
   1214         UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for reg %d but failed", lldb_regnum);
   1215         return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1216     }
   1217 
   1218     UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
   1219 
   1220     // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported.
   1221 
   1222     return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
   1223 }
   1224 
   1225 // If the Full unwindplan has been determined to be incorrect, this method will
   1226 // replace it with the architecture's default unwindplna, if one is defined.
   1227 // It will also find the FuncUnwinders object for this function and replace the
   1228 // Full unwind method for the function there so we don't use the errant Full unwindplan
   1229 // again in the future of this debug session.
   1230 // We're most likely doing this because the Full unwindplan was generated by assembly
   1231 // instruction profiling and the profiler got something wrong.
   1232 
   1233 void
   1234 RegisterContextLLDB::InvalidateFullUnwindPlan ()
   1235 {
   1236     UnwindPlan::Row::RegisterLocation unwindplan_regloc;
   1237     ExecutionContext exe_ctx (m_thread.shared_from_this());
   1238     Process *process = exe_ctx.GetProcessPtr();
   1239     ABI *abi = process ? process->GetABI().get() : NULL;
   1240     if (abi)
   1241     {
   1242         UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
   1243         UnwindPlanSP arch_default_unwind_plan_sp;
   1244         arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
   1245         abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
   1246         if (arch_default_unwind_plan_sp)
   1247         {
   1248             UnwindPlan::RowSP active_row = arch_default_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
   1249 
   1250             if (active_row && active_row->GetCFARegister() != LLDB_INVALID_REGNUM)
   1251             {
   1252                 FuncUnwindersSP func_unwinders_sp;
   1253                 if (m_sym_ctx_valid && m_current_pc.IsValid() && m_current_pc.GetModule())
   1254                 {
   1255                     func_unwinders_sp = m_current_pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
   1256                     if (func_unwinders_sp)
   1257                     {
   1258                         func_unwinders_sp->InvalidateNonCallSiteUnwindPlan (m_thread);
   1259                     }
   1260                 }
   1261                 m_registers.clear();
   1262                 m_full_unwind_plan_sp = arch_default_unwind_plan_sp;
   1263                 addr_t cfa_regval = LLDB_INVALID_ADDRESS;
   1264                 if (ReadGPRValue (arch_default_unwind_plan_sp->GetRegisterKind(), active_row->GetCFARegister(), cfa_regval))
   1265                 {
   1266                     m_cfa = cfa_regval + active_row->GetCFAOffset ();
   1267                 }
   1268 
   1269                 UnwindLogMsg ("full unwind plan '%s' has been replaced by architecture default unwind plan '%s' for this function from now on.",
   1270                               original_full_unwind_plan_sp->GetSourceName().GetCString(), arch_default_unwind_plan_sp->GetSourceName().GetCString());
   1271             }
   1272         }
   1273     }
   1274 }
   1275 
   1276 // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that
   1277 // this frame called.  e.g.
   1278 //
   1279 //  foo () { }
   1280 //  bar () { foo (); }
   1281 //  main () { bar (); }
   1282 //
   1283 //  stopped in foo() so
   1284 //     frame 0 - foo
   1285 //     frame 1 - bar
   1286 //     frame 2 - main
   1287 //  and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask
   1288 //  where frame 0 (the "next" frame) saved that and retrieve the value.
   1289 
   1290 bool
   1291 RegisterContextLLDB::ReadGPRValue (int register_kind, uint32_t regnum, addr_t &value)
   1292 {
   1293     if (!IsValid())
   1294         return false;
   1295 
   1296     uint32_t lldb_regnum;
   1297     if (register_kind == eRegisterKindLLDB)
   1298     {
   1299         lldb_regnum = regnum;
   1300     }
   1301     else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum))
   1302     {
   1303         return false;
   1304     }
   1305 
   1306     const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
   1307     RegisterValue reg_value;
   1308     // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers
   1309     if (IsFrameZero ())
   1310     {
   1311         if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value))
   1312         {
   1313             value = reg_value.GetAsUInt64();
   1314             return true;
   1315         }
   1316         return false;
   1317     }
   1318 
   1319     bool pc_register = false;
   1320     uint32_t generic_regnum;
   1321     if (register_kind == eRegisterKindGeneric && regnum == LLDB_REGNUM_GENERIC_PC)
   1322     {
   1323         pc_register = true;
   1324     }
   1325     else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum)
   1326              && generic_regnum == LLDB_REGNUM_GENERIC_PC)
   1327     {
   1328         pc_register = true;
   1329     }
   1330 
   1331     lldb_private::UnwindLLDB::RegisterLocation regloc;
   1332     if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register))
   1333     {
   1334         return false;
   1335     }
   1336     if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value))
   1337     {
   1338         value = reg_value.GetAsUInt64();
   1339         return true;
   1340     }
   1341     return false;
   1342 }
   1343 
   1344 // Find the value of a register in THIS frame
   1345 
   1346 bool
   1347 RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value)
   1348 {
   1349     if (!IsValid())
   1350         return false;
   1351 
   1352     const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
   1353     UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
   1354 
   1355     // If this is the 0th frame, hand this over to the live register context
   1356     if (IsFrameZero ())
   1357     {
   1358         UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
   1359         return m_thread.GetRegisterContext()->ReadRegister (reg_info, value);
   1360     }
   1361 
   1362     lldb_private::UnwindLLDB::RegisterLocation regloc;
   1363     // Find out where the NEXT frame saved THIS frame's register contents
   1364     if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false))
   1365         return false;
   1366 
   1367     return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value);
   1368 }
   1369 
   1370 bool
   1371 RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value)
   1372 {
   1373     if (!IsValid())
   1374         return false;
   1375 
   1376     const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
   1377     UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
   1378 
   1379     // If this is the 0th frame, hand this over to the live register context
   1380     if (IsFrameZero ())
   1381     {
   1382         UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
   1383         return m_thread.GetRegisterContext()->WriteRegister (reg_info, value);
   1384     }
   1385 
   1386     lldb_private::UnwindLLDB::RegisterLocation regloc;
   1387     // Find out where the NEXT frame saved THIS frame's register contents
   1388     if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false))
   1389         return false;
   1390 
   1391     return WriteRegisterValueToRegisterLocation (regloc, reg_info, value);
   1392 }
   1393 
   1394 // Don't need to implement this one
   1395 bool
   1396 RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp)
   1397 {
   1398     return false;
   1399 }
   1400 
   1401 // Don't need to implement this one
   1402 bool
   1403 RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp)
   1404 {
   1405     return false;
   1406 }
   1407 
   1408 // Retrieve the pc value for THIS from
   1409 
   1410 bool
   1411 RegisterContextLLDB::GetCFA (addr_t& cfa)
   1412 {
   1413     if (!IsValid())
   1414     {
   1415         return false;
   1416     }
   1417     if (m_cfa == LLDB_INVALID_ADDRESS)
   1418     {
   1419         return false;
   1420     }
   1421     cfa = m_cfa;
   1422     return true;
   1423 }
   1424 
   1425 
   1426 RegisterContextLLDB::SharedPtr
   1427 RegisterContextLLDB::GetNextFrame () const
   1428 {
   1429     RegisterContextLLDB::SharedPtr regctx;
   1430     if (m_frame_number == 0)
   1431       return regctx;
   1432     return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1);
   1433 }
   1434 
   1435 RegisterContextLLDB::SharedPtr
   1436 RegisterContextLLDB::GetPrevFrame () const
   1437 {
   1438     RegisterContextLLDB::SharedPtr regctx;
   1439     return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1);
   1440 }
   1441 
   1442 // Retrieve the address of the start of the function of THIS frame
   1443 
   1444 bool
   1445 RegisterContextLLDB::GetStartPC (addr_t& start_pc)
   1446 {
   1447     if (!IsValid())
   1448         return false;
   1449 
   1450     if (!m_start_pc.IsValid())
   1451     {
   1452         return ReadPC (start_pc);
   1453     }
   1454     start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get());
   1455     return true;
   1456 }
   1457 
   1458 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
   1459 
   1460 bool
   1461 RegisterContextLLDB::ReadPC (addr_t& pc)
   1462 {
   1463     if (!IsValid())
   1464         return false;
   1465 
   1466     if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
   1467     {
   1468         // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk.
   1469         // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may
   1470         // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help
   1471         // find the bug.
   1472 
   1473         if (m_all_registers_available == false
   1474             && (pc == 0 || pc == 1))
   1475         {
   1476             return false;
   1477         }
   1478         else
   1479         {
   1480             return true;
   1481         }
   1482     }
   1483     else
   1484     {
   1485         return false;
   1486     }
   1487 }
   1488 
   1489 
   1490 void
   1491 RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...)
   1492 {
   1493     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
   1494     if (log)
   1495     {
   1496         va_list args;
   1497         va_start (args, fmt);
   1498 
   1499         char *logmsg;
   1500         if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
   1501         {
   1502             if (logmsg)
   1503                 free (logmsg);
   1504             va_end (args);
   1505             return;
   1506         }
   1507         va_end (args);
   1508 
   1509         log->Printf ("%*sth%d/fr%u %s",
   1510                       m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,
   1511                       logmsg);
   1512         free (logmsg);
   1513     }
   1514 }
   1515 
   1516 void
   1517 RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...)
   1518 {
   1519     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
   1520     if (log && log->GetVerbose())
   1521     {
   1522         va_list args;
   1523         va_start (args, fmt);
   1524 
   1525         char *logmsg;
   1526         if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
   1527         {
   1528             if (logmsg)
   1529                 free (logmsg);
   1530             va_end (args);
   1531             return;
   1532         }
   1533         va_end (args);
   1534 
   1535         log->Printf ("%*sth%d/fr%u %s",
   1536                       m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,
   1537                       logmsg);
   1538         free (logmsg);
   1539     }
   1540 }
   1541 
   1542