180814287SRaphael Isemann //===-- UnwindAssemblyInstEmulation.cpp -----------------------------------===//
2ffc922e3SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ffc922e3SGreg Clayton //
7ffc922e3SGreg Clayton //===----------------------------------------------------------------------===//
8ffc922e3SGreg Clayton 
9ffc922e3SGreg Clayton #include "UnwindAssemblyInstEmulation.h"
10ffc922e3SGreg Clayton 
11ffc922e3SGreg Clayton #include "lldb/Core/Address.h"
122ed751bdSGreg Clayton #include "lldb/Core/Disassembler.h"
1329cb868aSZachary Turner #include "lldb/Core/DumpDataExtractor.h"
14e03334cfSPavel Labath #include "lldb/Core/DumpRegisterValue.h"
15554f68d3SGreg Clayton #include "lldb/Core/FormatEntity.h"
16ffc922e3SGreg Clayton #include "lldb/Core/PluginManager.h"
17ffc922e3SGreg Clayton #include "lldb/Target/ExecutionContext.h"
18ffc922e3SGreg Clayton #include "lldb/Target/Process.h"
19ffc922e3SGreg Clayton #include "lldb/Target/Target.h"
20b9c1b51eSKate Stone #include "lldb/Target/Thread.h"
215f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
22666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
23666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
24c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2697206d57SZachary Turner #include "lldb/Utility/Status.h"
27bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
28ffc922e3SGreg Clayton 
29ffc922e3SGreg Clayton using namespace lldb;
30ffc922e3SGreg Clayton using namespace lldb_private;
31ffc922e3SGreg Clayton 
LLDB_PLUGIN_DEFINE(UnwindAssemblyInstEmulation)32bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(UnwindAssemblyInstEmulation)
33fbb4d1e4SJonas Devlieghere 
3475b9cfd1SJason Molenda //  UnwindAssemblyInstEmulation method definitions
35ffc922e3SGreg Clayton 
36b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly(
37b9c1b51eSKate Stone     AddressRange &range, Thread &thread, UnwindPlan &unwind_plan) {
386853cca1SJason Molenda   std::vector<uint8_t> function_text(range.GetByteSize());
396853cca1SJason Molenda   ProcessSP process_sp(thread.GetProcess());
406853cca1SJason Molenda   if (process_sp) {
4197206d57SZachary Turner     Status error;
42e9fe788dSJason Molenda     const bool force_live_memory = true;
436853cca1SJason Molenda     if (process_sp->GetTarget().ReadMemory(
44e9fe788dSJason Molenda             range.GetBaseAddress(), function_text.data(), range.GetByteSize(),
45e9fe788dSJason Molenda             error, force_live_memory) != range.GetByteSize()) {
466853cca1SJason Molenda       return false;
476853cca1SJason Molenda     }
486853cca1SJason Molenda   }
496853cca1SJason Molenda   return GetNonCallSiteUnwindPlanFromAssembly(
506853cca1SJason Molenda       range, function_text.data(), function_text.size(), unwind_plan);
516853cca1SJason Molenda }
526853cca1SJason Molenda 
GetNonCallSiteUnwindPlanFromAssembly(AddressRange & range,uint8_t * opcode_data,size_t opcode_size,UnwindPlan & unwind_plan)536853cca1SJason Molenda bool UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly(
546853cca1SJason Molenda     AddressRange &range, uint8_t *opcode_data, size_t opcode_size,
556853cca1SJason Molenda     UnwindPlan &unwind_plan) {
566853cca1SJason Molenda   if (opcode_data == nullptr || opcode_size == 0)
576853cca1SJason Molenda     return false;
586853cca1SJason Molenda 
59b9c1b51eSKate Stone   if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid() &&
60d5b44036SJonas Devlieghere       m_inst_emulator_up.get()) {
6131f1d2f5SGreg Clayton 
6205097246SAdrian Prantl     // The instruction emulation subclass setup the unwind plan for the first
6305097246SAdrian Prantl     // instruction.
64d5b44036SJonas Devlieghere     m_inst_emulator_up->CreateFunctionEntryUnwind(unwind_plan);
6531f1d2f5SGreg Clayton 
6631f1d2f5SGreg Clayton     // CreateFunctionEntryUnwind should have created the first row. If it
6731f1d2f5SGreg Clayton     // doesn't, then we are done.
6831f1d2f5SGreg Clayton     if (unwind_plan.GetRowCount() == 0)
6931f1d2f5SGreg Clayton       return false;
702ed751bdSGreg Clayton 
716b3e6d54SJason Molenda     const bool prefer_file_cache = true;
726853cca1SJason Molenda     DisassemblerSP disasm_sp(Disassembler::DisassembleBytes(
73248a1305SKonrad Kleine         m_arch, nullptr, nullptr, range.GetBaseAddress(), opcode_data,
74248a1305SKonrad Kleine         opcode_size, 99999, prefer_file_cache));
7531f1d2f5SGreg Clayton 
76a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Unwind);
7731f1d2f5SGreg Clayton 
78b9c1b51eSKate Stone     if (disasm_sp) {
792ed751bdSGreg Clayton 
802ed751bdSGreg Clayton       m_range_ptr = &range;
812ed751bdSGreg Clayton       m_unwind_plan_ptr = &unwind_plan;
822ed751bdSGreg Clayton 
832ed751bdSGreg Clayton       const uint32_t addr_byte_size = m_arch.GetAddressByteSize();
842ed751bdSGreg Clayton       const bool show_address = true;
852ed751bdSGreg Clayton       const bool show_bytes = true;
86*ad7bcda9SWalter Erquinigo       const bool show_control_flow_kind = true;
87d5b44036SJonas Devlieghere       m_inst_emulator_up->GetRegisterInfo(unwind_plan.GetRegisterKind(),
8831f1d2f5SGreg Clayton                                           unwind_plan.GetInitialCFARegister(),
8931f1d2f5SGreg Clayton                                           m_cfa_reg_info);
9031f1d2f5SGreg Clayton 
9131f1d2f5SGreg Clayton       m_fp_is_cfa = false;
9231f1d2f5SGreg Clayton       m_register_values.clear();
9331f1d2f5SGreg Clayton       m_pushed_regs.clear();
9431f1d2f5SGreg Clayton 
9505097246SAdrian Prantl       // Initialize the CFA with a known value. In the 32 bit case it will be
9605097246SAdrian Prantl       // 0x80000000, and in the 64 bit case 0x8000000000000000. We use the
9705097246SAdrian Prantl       // address byte size to be safe for any future address sizes
987349bd90SGreg Clayton       m_initial_sp = (1ull << ((addr_byte_size * 8) - 1));
9970b57657SGreg Clayton       RegisterValue cfa_reg_value;
10070b57657SGreg Clayton       cfa_reg_value.SetUInt(m_initial_sp, m_cfa_reg_info.byte_size);
10170b57657SGreg Clayton       SetRegisterValue(m_cfa_reg_info, cfa_reg_value);
1022ed751bdSGreg Clayton 
1032ed751bdSGreg Clayton       const InstructionList &inst_list = disasm_sp->GetInstructionList();
1042ed751bdSGreg Clayton       const size_t num_instructions = inst_list.GetSize();
1051d42c7bcSJason Molenda 
106b9c1b51eSKate Stone       if (num_instructions > 0) {
107e5b3498eSGreg Clayton         Instruction *inst = inst_list.GetInstructionAtIndex(0).get();
10844ff9cceSTamas Berghammer         const lldb::addr_t base_addr = inst->GetAddress().GetFileAddress();
10944ff9cceSTamas Berghammer 
11005097246SAdrian Prantl         // Map for storing the unwind plan row and the value of the registers
11105097246SAdrian Prantl         // at a given offset. When we see a forward branch we add a new entry
11205097246SAdrian Prantl         // to this map with the actual unwind plan row and register context for
11305097246SAdrian Prantl         // the target address of the branch as the current data have to be
11405097246SAdrian Prantl         // valid for the target address of the branch too if we are in the same
11505097246SAdrian Prantl         // function.
116b9c1b51eSKate Stone         std::map<lldb::addr_t, std::pair<UnwindPlan::RowSP, RegisterValueMap>>
117b9c1b51eSKate Stone             saved_unwind_states;
11824a8378cSJason Molenda 
11924a8378cSJason Molenda         // Make a copy of the current instruction Row and save it in m_curr_row
12024a8378cSJason Molenda         // so we can add updates as we process the instructions.
1211d42c7bcSJason Molenda         UnwindPlan::RowSP last_row = unwind_plan.GetLastRow();
1221d42c7bcSJason Molenda         UnwindPlan::Row *newrow = new UnwindPlan::Row;
1231d42c7bcSJason Molenda         if (last_row.get())
1241d42c7bcSJason Molenda           *newrow = *last_row.get();
1251d42c7bcSJason Molenda         m_curr_row.reset(newrow);
126e5b3498eSGreg Clayton 
12744ff9cceSTamas Berghammer         // Add the initial state to the save list with offset 0.
12844ff9cceSTamas Berghammer         saved_unwind_states.insert({0, {last_row, m_register_values}});
12924a8378cSJason Molenda 
1301cd92e48SJason Molenda         // cache the stack pointer register number (in whatever register
13105097246SAdrian Prantl         // numbering this UnwindPlan uses) for quick reference during
13205097246SAdrian Prantl         // instruction parsing.
1331cd92e48SJason Molenda         RegisterInfo sp_reg_info;
134d5b44036SJonas Devlieghere         m_inst_emulator_up->GetRegisterInfo(
1351cd92e48SJason Molenda             eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp_reg_info);
13624a8378cSJason Molenda 
137b9c1b51eSKate Stone         // The architecture dependent condition code of the last processed
138b9c1b51eSKate Stone         // instruction.
139b9c1b51eSKate Stone         EmulateInstruction::InstructionCondition last_condition =
140b9c1b51eSKate Stone             EmulateInstruction::UnconditionalCondition;
14110e99238STamas Berghammer         lldb::addr_t condition_block_start_offset = 0;
14210e99238STamas Berghammer 
143b9c1b51eSKate Stone         for (size_t idx = 0; idx < num_instructions; ++idx) {
14424a8378cSJason Molenda           m_curr_row_modified = false;
14544ff9cceSTamas Berghammer           m_forward_branch_offset = 0;
14644ff9cceSTamas Berghammer 
147e5b3498eSGreg Clayton           inst = inst_list.GetInstructionAtIndex(idx).get();
148b9c1b51eSKate Stone           if (inst) {
149b9c1b51eSKate Stone             lldb::addr_t current_offset =
150b9c1b51eSKate Stone                 inst->GetAddress().GetFileAddress() - base_addr;
15144ff9cceSTamas Berghammer             auto it = saved_unwind_states.upper_bound(current_offset);
152b9c1b51eSKate Stone             assert(it != saved_unwind_states.begin() &&
153b9c1b51eSKate Stone                    "Unwind row for the function entry missing");
15444ff9cceSTamas Berghammer             --it; // Move it to the row corresponding to the current offset
15544ff9cceSTamas Berghammer 
15605097246SAdrian Prantl             // If the offset of m_curr_row don't match with the offset we see
15705097246SAdrian Prantl             // in saved_unwind_states then we have to update m_curr_row and
1584ebdee0aSBruce Mitchener             // m_register_values based on the saved values. It is happening
15905097246SAdrian Prantl             // after we processed an epilogue and a return to caller
16005097246SAdrian Prantl             // instruction.
161b9c1b51eSKate Stone             if (it->second.first->GetOffset() != m_curr_row->GetOffset()) {
16244ff9cceSTamas Berghammer               UnwindPlan::Row *newrow = new UnwindPlan::Row;
16344ff9cceSTamas Berghammer               *newrow = *it->second.first;
16444ff9cceSTamas Berghammer               m_curr_row.reset(newrow);
16510e99238STamas Berghammer               m_register_values = it->second.second;
1661cd92e48SJason Molenda               // re-set the CFA register ivars to match the
1671cd92e48SJason Molenda               // new m_curr_row.
1681cd92e48SJason Molenda               if (sp_reg_info.name &&
1691cd92e48SJason Molenda                   m_curr_row->GetCFAValue().IsRegisterPlusOffset()) {
1701cd92e48SJason Molenda                 uint32_t row_cfa_regnum =
1711cd92e48SJason Molenda                     m_curr_row->GetCFAValue().GetRegisterNumber();
1721cd92e48SJason Molenda                 lldb::RegisterKind row_kind =
1731cd92e48SJason Molenda                     m_unwind_plan_ptr->GetRegisterKind();
1741cd92e48SJason Molenda                 // set m_cfa_reg_info to the row's CFA reg.
1751cd92e48SJason Molenda                 m_inst_emulator_up->GetRegisterInfo(row_kind, row_cfa_regnum,
1761cd92e48SJason Molenda                                                     m_cfa_reg_info);
1771cd92e48SJason Molenda                 // set m_fp_is_cfa.
1781cd92e48SJason Molenda                 if (sp_reg_info.kinds[row_kind] == row_cfa_regnum)
1791cd92e48SJason Molenda                   m_fp_is_cfa = false;
1801cd92e48SJason Molenda                 else
1811cd92e48SJason Molenda                   m_fp_is_cfa = true;
1821cd92e48SJason Molenda               }
18310e99238STamas Berghammer             }
18410e99238STamas Berghammer 
185d5b44036SJonas Devlieghere             m_inst_emulator_up->SetInstruction(inst->GetOpcode(),
1866853cca1SJason Molenda                                                inst->GetAddress(), nullptr);
18710e99238STamas Berghammer 
188b9c1b51eSKate Stone             if (last_condition !=
189d5b44036SJonas Devlieghere                 m_inst_emulator_up->GetInstructionCondition()) {
190d5b44036SJonas Devlieghere               if (m_inst_emulator_up->GetInstructionCondition() !=
191b9c1b51eSKate Stone                       EmulateInstruction::UnconditionalCondition &&
192b9c1b51eSKate Stone                   saved_unwind_states.count(current_offset) == 0) {
19305097246SAdrian Prantl                 // If we don't have a saved row for the current offset then
19405097246SAdrian Prantl                 // save our current state because we will have to restore it
19505097246SAdrian Prantl                 // after the conditional block.
196b9c1b51eSKate Stone                 auto new_row =
197b9c1b51eSKate Stone                     std::make_shared<UnwindPlan::Row>(*m_curr_row.get());
198b9c1b51eSKate Stone                 saved_unwind_states.insert(
199b9c1b51eSKate Stone                     {current_offset, {new_row, m_register_values}});
20010e99238STamas Berghammer               }
20110e99238STamas Berghammer 
202b9c1b51eSKate Stone               // If the last instruction was conditional with a different
20305097246SAdrian Prantl               // condition then the then current condition then restore the
20405097246SAdrian Prantl               // condition.
205b9c1b51eSKate Stone               if (last_condition !=
206b9c1b51eSKate Stone                   EmulateInstruction::UnconditionalCondition) {
207b9c1b51eSKate Stone                 const auto &saved_state =
208b9c1b51eSKate Stone                     saved_unwind_states.at(condition_block_start_offset);
209b9c1b51eSKate Stone                 m_curr_row =
210b9c1b51eSKate Stone                     std::make_shared<UnwindPlan::Row>(*saved_state.first);
21110e99238STamas Berghammer                 m_curr_row->SetOffset(current_offset);
21210e99238STamas Berghammer                 m_register_values = saved_state.second;
2131cd92e48SJason Molenda                 // re-set the CFA register ivars to match the
2141cd92e48SJason Molenda                 // new m_curr_row.
2151cd92e48SJason Molenda                 if (sp_reg_info.name &&
2161cd92e48SJason Molenda                     m_curr_row->GetCFAValue().IsRegisterPlusOffset()) {
2171cd92e48SJason Molenda                   uint32_t row_cfa_regnum =
2181cd92e48SJason Molenda                       m_curr_row->GetCFAValue().GetRegisterNumber();
2191cd92e48SJason Molenda                   lldb::RegisterKind row_kind =
2201cd92e48SJason Molenda                       m_unwind_plan_ptr->GetRegisterKind();
2211cd92e48SJason Molenda                   // set m_cfa_reg_info to the row's CFA reg.
2221cd92e48SJason Molenda                   m_inst_emulator_up->GetRegisterInfo(row_kind, row_cfa_regnum,
2231cd92e48SJason Molenda                                                       m_cfa_reg_info);
2241cd92e48SJason Molenda                   // set m_fp_is_cfa.
2251cd92e48SJason Molenda                   if (sp_reg_info.kinds[row_kind] == row_cfa_regnum)
2261cd92e48SJason Molenda                     m_fp_is_cfa = false;
2271cd92e48SJason Molenda                   else
2281cd92e48SJason Molenda                     m_fp_is_cfa = true;
2291cd92e48SJason Molenda                 }
230b9c1b51eSKate Stone                 bool replace_existing =
231b9c1b51eSKate Stone                     true; // The last instruction might already
23210e99238STamas Berghammer                           // created a row for this offset and
23310e99238STamas Berghammer                           // we want to overwrite it.
234b9c1b51eSKate Stone                 unwind_plan.InsertRow(
235b9c1b51eSKate Stone                     std::make_shared<UnwindPlan::Row>(*m_curr_row),
236b9c1b51eSKate Stone                     replace_existing);
23710e99238STamas Berghammer               }
23810e99238STamas Berghammer 
2394ebdee0aSBruce Mitchener               // We are starting a new conditional block at the actual offset
24010e99238STamas Berghammer               condition_block_start_offset = current_offset;
24144ff9cceSTamas Berghammer             }
24244ff9cceSTamas Berghammer 
243b9c1b51eSKate Stone             if (log && log->GetVerbose()) {
24431f1d2f5SGreg Clayton               StreamString strm;
245554f68d3SGreg Clayton               lldb_private::FormatEntity::Entry format;
246554f68d3SGreg Clayton               FormatEntity::Parse("${frame.pc}: ", format);
247b9c1b51eSKate Stone               inst->Dump(&strm, inst_list.GetMaxOpcocdeByteSize(), show_address,
248*ad7bcda9SWalter Erquinigo                          show_bytes, show_control_flow_kind, nullptr, nullptr,
249*ad7bcda9SWalter Erquinigo                          nullptr, &format, 0);
250c156427dSZachary Turner               log->PutString(strm.GetString());
25131f1d2f5SGreg Clayton             }
2522ed751bdSGreg Clayton 
253d5b44036SJonas Devlieghere             last_condition = m_inst_emulator_up->GetInstructionCondition();
254e5b3498eSGreg Clayton 
255d5b44036SJonas Devlieghere             m_inst_emulator_up->EvaluateInstruction(
256b9c1b51eSKate Stone                 eEmulateInstructionOptionIgnoreConditions);
25731f1d2f5SGreg Clayton 
258b9c1b51eSKate Stone             // If the current instruction is a branch forward then save the
25905097246SAdrian Prantl             // current CFI information for the offset where we are branching.
260b9c1b51eSKate Stone             if (m_forward_branch_offset != 0 &&
261b9c1b51eSKate Stone                 range.ContainsFileAddress(inst->GetAddress().GetFileAddress() +
262b9c1b51eSKate Stone                                           m_forward_branch_offset)) {
263b9c1b51eSKate Stone               auto newrow =
264b9c1b51eSKate Stone                   std::make_shared<UnwindPlan::Row>(*m_curr_row.get());
26544ff9cceSTamas Berghammer               newrow->SetOffset(current_offset + m_forward_branch_offset);
266b9c1b51eSKate Stone               saved_unwind_states.insert(
267b9c1b51eSKate Stone                   {current_offset + m_forward_branch_offset,
268b9c1b51eSKate Stone                    {newrow, m_register_values}});
26944ff9cceSTamas Berghammer               unwind_plan.InsertRow(newrow);
27044ff9cceSTamas Berghammer             }
27144ff9cceSTamas Berghammer 
272b9c1b51eSKate Stone             // Were there any changes to the CFI while evaluating this
273b9c1b51eSKate Stone             // instruction?
274b9c1b51eSKate Stone             if (m_curr_row_modified) {
27505097246SAdrian Prantl               // Save the modified row if we don't already have a CFI row in
2764ebdee0aSBruce Mitchener               // the current address
277b9c1b51eSKate Stone               if (saved_unwind_states.count(
278b9c1b51eSKate Stone                       current_offset + inst->GetOpcode().GetByteSize()) == 0) {
279b9c1b51eSKate Stone                 m_curr_row->SetOffset(current_offset +
280b9c1b51eSKate Stone                                       inst->GetOpcode().GetByteSize());
28144ff9cceSTamas Berghammer                 unwind_plan.InsertRow(m_curr_row);
282b9c1b51eSKate Stone                 saved_unwind_states.insert(
283b9c1b51eSKate Stone                     {current_offset + inst->GetOpcode().GetByteSize(),
284b9c1b51eSKate Stone                      {m_curr_row, m_register_values}});
2851d42c7bcSJason Molenda 
286b9c1b51eSKate Stone                 // Allocate a new Row for m_curr_row, copy the current state
287b9c1b51eSKate Stone                 // into it
2881d42c7bcSJason Molenda                 UnwindPlan::Row *newrow = new UnwindPlan::Row;
2891d42c7bcSJason Molenda                 *newrow = *m_curr_row.get();
2901d42c7bcSJason Molenda                 m_curr_row.reset(newrow);
29124a8378cSJason Molenda               }
2922ed751bdSGreg Clayton             }
2932ed751bdSGreg Clayton           }
2942ed751bdSGreg Clayton         }
2952ed751bdSGreg Clayton       }
296e5b3498eSGreg Clayton     }
29731f1d2f5SGreg Clayton 
298b9c1b51eSKate Stone     if (log && log->GetVerbose()) {
29931f1d2f5SGreg Clayton       StreamString strm;
3006853cca1SJason Molenda       lldb::addr_t base_addr = range.GetBaseAddress().GetFileAddress();
301b9c1b51eSKate Stone       strm.Printf("Resulting unwind rows for [0x%" PRIx64 " - 0x%" PRIx64 "):",
302b9c1b51eSKate Stone                   base_addr, base_addr + range.GetByteSize());
3036853cca1SJason Molenda       unwind_plan.Dump(strm, nullptr, base_addr);
304c156427dSZachary Turner       log->PutString(strm.GetString());
30531f1d2f5SGreg Clayton     }
30631f1d2f5SGreg Clayton     return unwind_plan.GetRowCount() > 0;
30731f1d2f5SGreg Clayton   }
3082ed751bdSGreg Clayton   return false;
3092ed751bdSGreg Clayton }
3102ed751bdSGreg Clayton 
AugmentUnwindPlanFromCallSite(AddressRange & func,Thread & thread,UnwindPlan & unwind_plan)311b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::AugmentUnwindPlanFromCallSite(
312b9c1b51eSKate Stone     AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) {
3130562524bSTodd Fiala   return false;
3140562524bSTodd Fiala }
3150562524bSTodd Fiala 
GetFastUnwindPlan(AddressRange & func,Thread & thread,UnwindPlan & unwind_plan)316b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::GetFastUnwindPlan(AddressRange &func,
3172ed751bdSGreg Clayton                                                     Thread &thread,
318b9c1b51eSKate Stone                                                     UnwindPlan &unwind_plan) {
319ffc922e3SGreg Clayton   return false;
320ffc922e3SGreg Clayton }
321ffc922e3SGreg Clayton 
FirstNonPrologueInsn(AddressRange & func,const ExecutionContext & exe_ctx,Address & first_non_prologue_insn)322b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::FirstNonPrologueInsn(
323b9c1b51eSKate Stone     AddressRange &func, const ExecutionContext &exe_ctx,
324b9c1b51eSKate Stone     Address &first_non_prologue_insn) {
325ffc922e3SGreg Clayton   return false;
326ffc922e3SGreg Clayton }
327ffc922e3SGreg Clayton 
3287be2542fSGreg Clayton UnwindAssembly *
CreateInstance(const ArchSpec & arch)329b9c1b51eSKate Stone UnwindAssemblyInstEmulation::CreateInstance(const ArchSpec &arch) {
330d5b44036SJonas Devlieghere   std::unique_ptr<EmulateInstruction> inst_emulator_up(
331b9c1b51eSKate Stone       EmulateInstruction::FindPlugin(arch, eInstructionTypePrologueEpilogue,
332248a1305SKonrad Kleine                                      nullptr));
3332ed751bdSGreg Clayton   // Make sure that all prologue instructions are handled
334d5b44036SJonas Devlieghere   if (inst_emulator_up)
335d5b44036SJonas Devlieghere     return new UnwindAssemblyInstEmulation(arch, inst_emulator_up.release());
336248a1305SKonrad Kleine   return nullptr;
337ffc922e3SGreg Clayton }
338ffc922e3SGreg Clayton 
Initialize()339b9c1b51eSKate Stone void UnwindAssemblyInstEmulation::Initialize() {
340ffc922e3SGreg Clayton   PluginManager::RegisterPlugin(GetPluginNameStatic(),
341b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance);
342ffc922e3SGreg Clayton }
343ffc922e3SGreg Clayton 
Terminate()344b9c1b51eSKate Stone void UnwindAssemblyInstEmulation::Terminate() {
345ffc922e3SGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
346ffc922e3SGreg Clayton }
347ffc922e3SGreg Clayton 
GetPluginDescriptionStatic()348a3942318SPavel Labath llvm::StringRef UnwindAssemblyInstEmulation::GetPluginDescriptionStatic() {
349ffc922e3SGreg Clayton   return "Instruction emulation based unwind information.";
350ffc922e3SGreg Clayton }
3512ed751bdSGreg Clayton 
MakeRegisterKindValuePair(const RegisterInfo & reg_info)352b9c1b51eSKate Stone uint64_t UnwindAssemblyInstEmulation::MakeRegisterKindValuePair(
353b9c1b51eSKate Stone     const RegisterInfo &reg_info) {
354e7c7c3deSJean-Daniel Dupas   lldb::RegisterKind reg_kind;
355e7c7c3deSJean-Daniel Dupas   uint32_t reg_num;
356b9c1b51eSKate Stone   if (EmulateInstruction::GetBestRegisterKindAndNumber(&reg_info, reg_kind,
357b9c1b51eSKate Stone                                                        reg_num))
35879ea878bSGreg Clayton     return (uint64_t)reg_kind << 24 | reg_num;
35979ea878bSGreg Clayton   return 0ull;
36079ea878bSGreg Clayton }
36179ea878bSGreg Clayton 
SetRegisterValue(const RegisterInfo & reg_info,const RegisterValue & reg_value)362b9c1b51eSKate Stone void UnwindAssemblyInstEmulation::SetRegisterValue(
363b9c1b51eSKate Stone     const RegisterInfo &reg_info, const RegisterValue &reg_value) {
36479ea878bSGreg Clayton   m_register_values[MakeRegisterKindValuePair(reg_info)] = reg_value;
36579ea878bSGreg Clayton }
36679ea878bSGreg Clayton 
GetRegisterValue(const RegisterInfo & reg_info,RegisterValue & reg_value)367b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::GetRegisterValue(const RegisterInfo &reg_info,
368b9c1b51eSKate Stone                                                    RegisterValue &reg_value) {
36979ea878bSGreg Clayton   const uint64_t reg_id = MakeRegisterKindValuePair(reg_info);
37079ea878bSGreg Clayton   RegisterValueMap::const_iterator pos = m_register_values.find(reg_id);
371b9c1b51eSKate Stone   if (pos != m_register_values.end()) {
3727349bd90SGreg Clayton     reg_value = pos->second;
3737349bd90SGreg Clayton     return true; // We had a real value that comes from an opcode that wrote
3747349bd90SGreg Clayton                  // to it...
3757349bd90SGreg Clayton   }
3767349bd90SGreg Clayton   // We are making up a value that is recognizable...
3777349bd90SGreg Clayton   reg_value.SetUInt(reg_id, reg_info.byte_size);
3787349bd90SGreg Clayton   return false;
37979ea878bSGreg Clayton }
38079ea878bSGreg Clayton 
ReadMemory(EmulateInstruction * instruction,void * baton,const EmulateInstruction::Context & context,lldb::addr_t addr,void * dst,size_t dst_len)381b9c1b51eSKate Stone size_t UnwindAssemblyInstEmulation::ReadMemory(
382b9c1b51eSKate Stone     EmulateInstruction *instruction, void *baton,
383b9c1b51eSKate Stone     const EmulateInstruction::Context &context, lldb::addr_t addr, void *dst,
384b9c1b51eSKate Stone     size_t dst_len) {
385a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
38631f1d2f5SGreg Clayton 
387b9c1b51eSKate Stone   if (log && log->GetVerbose()) {
38831f1d2f5SGreg Clayton     StreamString strm;
389b9c1b51eSKate Stone     strm.Printf(
390b9c1b51eSKate Stone         "UnwindAssemblyInstEmulation::ReadMemory    (addr = 0x%16.16" PRIx64
391b9c1b51eSKate Stone         ", dst = %p, dst_len = %" PRIu64 ", context = ",
392b9c1b51eSKate Stone         addr, dst, (uint64_t)dst_len);
39331f1d2f5SGreg Clayton     context.Dump(strm, instruction);
394c156427dSZachary Turner     log->PutString(strm.GetString());
39531f1d2f5SGreg Clayton   }
3964d04d477SJason Molenda   memset(dst, 0, dst_len);
3972ed751bdSGreg Clayton   return dst_len;
3982ed751bdSGreg Clayton }
3992ed751bdSGreg Clayton 
WriteMemory(EmulateInstruction * instruction,void * baton,const EmulateInstruction::Context & context,lldb::addr_t addr,const void * dst,size_t dst_len)400b9c1b51eSKate Stone size_t UnwindAssemblyInstEmulation::WriteMemory(
401b9c1b51eSKate Stone     EmulateInstruction *instruction, void *baton,
402b9c1b51eSKate Stone     const EmulateInstruction::Context &context, lldb::addr_t addr,
403b9c1b51eSKate Stone     const void *dst, size_t dst_len) {
40431f1d2f5SGreg Clayton   if (baton && dst && dst_len)
405b9c1b51eSKate Stone     return ((UnwindAssemblyInstEmulation *)baton)
406b9c1b51eSKate Stone         ->WriteMemory(instruction, context, addr, dst, dst_len);
40731f1d2f5SGreg Clayton   return 0;
40831f1d2f5SGreg Clayton }
4092ed751bdSGreg Clayton 
WriteMemory(EmulateInstruction * instruction,const EmulateInstruction::Context & context,lldb::addr_t addr,const void * dst,size_t dst_len)410b9c1b51eSKate Stone size_t UnwindAssemblyInstEmulation::WriteMemory(
411b9c1b51eSKate Stone     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
412b9c1b51eSKate Stone     lldb::addr_t addr, const void *dst, size_t dst_len) {
413b9c1b51eSKate Stone   DataExtractor data(dst, dst_len,
4142ed751bdSGreg Clayton                      instruction->GetArchitecture().GetByteOrder(),
4152ed751bdSGreg Clayton                      instruction->GetArchitecture().GetAddressByteSize());
41631f1d2f5SGreg Clayton 
417a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
41831f1d2f5SGreg Clayton 
419b9c1b51eSKate Stone   if (log && log->GetVerbose()) {
42031f1d2f5SGreg Clayton     StreamString strm;
4212ed751bdSGreg Clayton 
42279ea878bSGreg Clayton     strm.PutCString("UnwindAssemblyInstEmulation::WriteMemory   (");
42329cb868aSZachary Turner     DumpDataExtractor(data, &strm, 0, eFormatBytes, 1, dst_len, UINT32_MAX,
42429cb868aSZachary Turner                       addr, 0, 0);
42579ea878bSGreg Clayton     strm.PutCString(", context = ");
42631f1d2f5SGreg Clayton     context.Dump(strm, instruction);
427c156427dSZachary Turner     log->PutString(strm.GetString());
42831f1d2f5SGreg Clayton   }
42931f1d2f5SGreg Clayton 
43031f1d2f5SGreg Clayton   const bool cant_replace = false;
431e5b3498eSGreg Clayton 
432b9c1b51eSKate Stone   switch (context.type) {
4337349bd90SGreg Clayton   default:
434e5b3498eSGreg Clayton   case EmulateInstruction::eContextInvalid:
435e5b3498eSGreg Clayton   case EmulateInstruction::eContextReadOpcode:
436e5b3498eSGreg Clayton   case EmulateInstruction::eContextImmediate:
437e5b3498eSGreg Clayton   case EmulateInstruction::eContextAdjustBaseRegister:
438e5b3498eSGreg Clayton   case EmulateInstruction::eContextRegisterPlusOffset:
439e5b3498eSGreg Clayton   case EmulateInstruction::eContextAdjustPC:
440e5b3498eSGreg Clayton   case EmulateInstruction::eContextRegisterStore:
441e5b3498eSGreg Clayton   case EmulateInstruction::eContextRegisterLoad:
442e5b3498eSGreg Clayton   case EmulateInstruction::eContextRelativeBranchImmediate:
443e5b3498eSGreg Clayton   case EmulateInstruction::eContextAbsoluteBranchRegister:
444e5b3498eSGreg Clayton   case EmulateInstruction::eContextSupervisorCall:
445e5b3498eSGreg Clayton   case EmulateInstruction::eContextTableBranchReadMemory:
446e5b3498eSGreg Clayton   case EmulateInstruction::eContextWriteRegisterRandomBits:
447e5b3498eSGreg Clayton   case EmulateInstruction::eContextWriteMemoryRandomBits:
448e5b3498eSGreg Clayton   case EmulateInstruction::eContextArithmetic:
449e5b3498eSGreg Clayton   case EmulateInstruction::eContextAdvancePC:
450e5b3498eSGreg Clayton   case EmulateInstruction::eContextReturnFromException:
451e5b3498eSGreg Clayton   case EmulateInstruction::eContextPopRegisterOffStack:
452e5b3498eSGreg Clayton   case EmulateInstruction::eContextAdjustStackPointer:
453e5b3498eSGreg Clayton     break;
454e5b3498eSGreg Clayton 
455b9c1b51eSKate Stone   case EmulateInstruction::eContextPushRegisterOnStack: {
45631f1d2f5SGreg Clayton     uint32_t reg_num = LLDB_INVALID_REGNUM;
457f366af30STamas Berghammer     uint32_t generic_regnum = LLDB_INVALID_REGNUM;
458a322f36cSDavid Blaikie     assert(context.info_type ==
459a322f36cSDavid Blaikie                EmulateInstruction::eInfoTypeRegisterToRegisterPlusOffset &&
460a322f36cSDavid Blaikie            "unhandled case, add code to handle this!");
461f366af30STamas Berghammer     const uint32_t unwind_reg_kind = m_unwind_plan_ptr->GetRegisterKind();
462b9c1b51eSKate Stone     reg_num = context.info.RegisterToRegisterPlusOffset.data_reg
463b9c1b51eSKate Stone                   .kinds[unwind_reg_kind];
464b9c1b51eSKate Stone     generic_regnum = context.info.RegisterToRegisterPlusOffset.data_reg
465b9c1b51eSKate Stone                          .kinds[eRegisterKindGeneric];
46631f1d2f5SGreg Clayton 
467b9c1b51eSKate Stone     if (reg_num != LLDB_INVALID_REGNUM &&
468b9c1b51eSKate Stone         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
469b9c1b51eSKate Stone       if (m_pushed_regs.find(reg_num) == m_pushed_regs.end()) {
47031f1d2f5SGreg Clayton         m_pushed_regs[reg_num] = addr;
47131f1d2f5SGreg Clayton         const int32_t offset = addr - m_initial_sp;
472b9c1b51eSKate Stone         m_curr_row->SetRegisterLocationToAtCFAPlusOffset(reg_num, offset,
473b9c1b51eSKate Stone                                                          cant_replace);
47424a8378cSJason Molenda         m_curr_row_modified = true;
47531f1d2f5SGreg Clayton       }
47631f1d2f5SGreg Clayton     }
477b9c1b51eSKate Stone   } break;
478e5b3498eSGreg Clayton   }
479e5b3498eSGreg Clayton 
4802ed751bdSGreg Clayton   return dst_len;
4812ed751bdSGreg Clayton }
4822ed751bdSGreg Clayton 
ReadRegister(EmulateInstruction * instruction,void * baton,const RegisterInfo * reg_info,RegisterValue & reg_value)483b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
4842ed751bdSGreg Clayton                                                void *baton,
4857349bd90SGreg Clayton                                                const RegisterInfo *reg_info,
486b9c1b51eSKate Stone                                                RegisterValue &reg_value) {
4872ed751bdSGreg Clayton 
48831f1d2f5SGreg Clayton   if (baton && reg_info)
489b9c1b51eSKate Stone     return ((UnwindAssemblyInstEmulation *)baton)
490b9c1b51eSKate Stone         ->ReadRegister(instruction, reg_info, reg_value);
49131f1d2f5SGreg Clayton   return false;
49231f1d2f5SGreg Clayton }
ReadRegister(EmulateInstruction * instruction,const RegisterInfo * reg_info,RegisterValue & reg_value)493b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
49431f1d2f5SGreg Clayton                                                const RegisterInfo *reg_info,
495b9c1b51eSKate Stone                                                RegisterValue &reg_value) {
49631f1d2f5SGreg Clayton   bool synthetic = GetRegisterValue(*reg_info, reg_value);
49731f1d2f5SGreg Clayton 
498a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
49931f1d2f5SGreg Clayton 
500b9c1b51eSKate Stone   if (log && log->GetVerbose()) {
50131f1d2f5SGreg Clayton 
50231f1d2f5SGreg Clayton     StreamString strm;
503b9c1b51eSKate Stone     strm.Printf("UnwindAssemblyInstEmulation::ReadRegister  (name = \"%s\") => "
504b9c1b51eSKate Stone                 "synthetic_value = %i, value = ",
505b9c1b51eSKate Stone                 reg_info->name, synthetic);
506e03334cfSPavel Labath     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
507c156427dSZachary Turner     log->PutString(strm.GetString());
5087349bd90SGreg Clayton   }
5092ed751bdSGreg Clayton   return true;
5102ed751bdSGreg Clayton }
5112ed751bdSGreg Clayton 
WriteRegister(EmulateInstruction * instruction,void * baton,const EmulateInstruction::Context & context,const RegisterInfo * reg_info,const RegisterValue & reg_value)512b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::WriteRegister(
513b9c1b51eSKate Stone     EmulateInstruction *instruction, void *baton,
514b9c1b51eSKate Stone     const EmulateInstruction::Context &context, const RegisterInfo *reg_info,
515b9c1b51eSKate Stone     const RegisterValue &reg_value) {
51631f1d2f5SGreg Clayton   if (baton && reg_info)
517b9c1b51eSKate Stone     return ((UnwindAssemblyInstEmulation *)baton)
518b9c1b51eSKate Stone         ->WriteRegister(instruction, context, reg_info, reg_value);
5197349bd90SGreg Clayton   return false;
52031f1d2f5SGreg Clayton }
WriteRegister(EmulateInstruction * instruction,const EmulateInstruction::Context & context,const RegisterInfo * reg_info,const RegisterValue & reg_value)521b9c1b51eSKate Stone bool UnwindAssemblyInstEmulation::WriteRegister(
522b9c1b51eSKate Stone     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
523b9c1b51eSKate Stone     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
524a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
5252ed751bdSGreg Clayton 
526b9c1b51eSKate Stone   if (log && log->GetVerbose()) {
52731f1d2f5SGreg Clayton 
52831f1d2f5SGreg Clayton     StreamString strm;
529b9c1b51eSKate Stone     strm.Printf(
530b9c1b51eSKate Stone         "UnwindAssemblyInstEmulation::WriteRegister (name = \"%s\", value = ",
531b9c1b51eSKate Stone         reg_info->name);
532e03334cfSPavel Labath     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
5337349bd90SGreg Clayton     strm.PutCString(", context = ");
53431f1d2f5SGreg Clayton     context.Dump(strm, instruction);
535c156427dSZachary Turner     log->PutString(strm.GetString());
53631f1d2f5SGreg Clayton   }
5372ed751bdSGreg Clayton 
53831f1d2f5SGreg Clayton   SetRegisterValue(*reg_info, reg_value);
53979ea878bSGreg Clayton 
540b9c1b51eSKate Stone   switch (context.type) {
5412ed751bdSGreg Clayton   case EmulateInstruction::eContextInvalid:
5422ed751bdSGreg Clayton   case EmulateInstruction::eContextReadOpcode:
5432ed751bdSGreg Clayton   case EmulateInstruction::eContextImmediate:
5442ed751bdSGreg Clayton   case EmulateInstruction::eContextAdjustBaseRegister:
5452ed751bdSGreg Clayton   case EmulateInstruction::eContextRegisterPlusOffset:
5462ed751bdSGreg Clayton   case EmulateInstruction::eContextAdjustPC:
5472ed751bdSGreg Clayton   case EmulateInstruction::eContextRegisterStore:
5482ed751bdSGreg Clayton   case EmulateInstruction::eContextSupervisorCall:
5492ed751bdSGreg Clayton   case EmulateInstruction::eContextTableBranchReadMemory:
5502ed751bdSGreg Clayton   case EmulateInstruction::eContextWriteRegisterRandomBits:
5512ed751bdSGreg Clayton   case EmulateInstruction::eContextWriteMemoryRandomBits:
5522ed751bdSGreg Clayton   case EmulateInstruction::eContextAdvancePC:
5532ed751bdSGreg Clayton   case EmulateInstruction::eContextReturnFromException:
5542ed751bdSGreg Clayton   case EmulateInstruction::eContextPushRegisterOnStack:
55544ff9cceSTamas Berghammer   case EmulateInstruction::eContextRegisterLoad:
55631f1d2f5SGreg Clayton     //            {
557b9c1b51eSKate Stone     //                const uint32_t reg_num =
558b9c1b51eSKate Stone     //                reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
55931f1d2f5SGreg Clayton     //                if (reg_num != LLDB_INVALID_REGNUM)
56031f1d2f5SGreg Clayton     //                {
56131f1d2f5SGreg Clayton     //                    const bool can_replace_only_if_unspecified = true;
56231f1d2f5SGreg Clayton     //
56331f1d2f5SGreg Clayton     //                    m_curr_row.SetRegisterLocationToUndefined (reg_num,
56431f1d2f5SGreg Clayton     //                                                               can_replace_only_if_unspecified,
56531f1d2f5SGreg Clayton     //                                                               can_replace_only_if_unspecified);
56624a8378cSJason Molenda     //                    m_curr_row_modified = true;
56731f1d2f5SGreg Clayton     //                }
56831f1d2f5SGreg Clayton     //            }
5692ed751bdSGreg Clayton     break;
5702ed751bdSGreg Clayton 
571b9c1b51eSKate Stone   case EmulateInstruction::eContextArithmetic: {
572b9c1b51eSKate Stone     // If we adjusted the current frame pointer by a constant then adjust the
573b9c1b51eSKate Stone     // CFA offset
57473bcca5bSTamas Berghammer     // with the same amount.
57573bcca5bSTamas Berghammer     lldb::RegisterKind kind = m_unwind_plan_ptr->GetRegisterKind();
57673bcca5bSTamas Berghammer     if (m_fp_is_cfa && reg_info->kinds[kind] == m_cfa_reg_info.kinds[kind] &&
57773bcca5bSTamas Berghammer         context.info_type == EmulateInstruction::eInfoTypeRegisterPlusOffset &&
578b9c1b51eSKate Stone         context.info.RegisterPlusOffset.reg.kinds[kind] ==
579b9c1b51eSKate Stone             m_cfa_reg_info.kinds[kind]) {
58073bcca5bSTamas Berghammer       const int64_t offset = context.info.RegisterPlusOffset.signed_offset;
58173bcca5bSTamas Berghammer       m_curr_row->GetCFAValue().IncOffset(-1 * offset);
58273bcca5bSTamas Berghammer       m_curr_row_modified = true;
58373bcca5bSTamas Berghammer     }
584b9c1b51eSKate Stone   } break;
58573bcca5bSTamas Berghammer 
58644ff9cceSTamas Berghammer   case EmulateInstruction::eContextAbsoluteBranchRegister:
587b9c1b51eSKate Stone   case EmulateInstruction::eContextRelativeBranchImmediate: {
58844ff9cceSTamas Berghammer     if (context.info_type == EmulateInstruction::eInfoTypeISAAndImmediate &&
589b9c1b51eSKate Stone         context.info.ISAAndImmediate.unsigned_data32 > 0) {
590b9c1b51eSKate Stone       m_forward_branch_offset =
591b9c1b51eSKate Stone           context.info.ISAAndImmediateSigned.signed_data32;
592b9c1b51eSKate Stone     } else if (context.info_type ==
593b9c1b51eSKate Stone                    EmulateInstruction::eInfoTypeISAAndImmediateSigned &&
594b9c1b51eSKate Stone                context.info.ISAAndImmediateSigned.signed_data32 > 0) {
59544ff9cceSTamas Berghammer       m_forward_branch_offset = context.info.ISAAndImmediate.unsigned_data32;
596b9c1b51eSKate Stone     } else if (context.info_type == EmulateInstruction::eInfoTypeImmediate &&
597b9c1b51eSKate Stone                context.info.unsigned_immediate > 0) {
59867ec5458STamas Berghammer       m_forward_branch_offset = context.info.unsigned_immediate;
599b9c1b51eSKate Stone     } else if (context.info_type ==
600b9c1b51eSKate Stone                    EmulateInstruction::eInfoTypeImmediateSigned &&
601b9c1b51eSKate Stone                context.info.signed_immediate > 0) {
60267ec5458STamas Berghammer       m_forward_branch_offset = context.info.signed_immediate;
60367ec5458STamas Berghammer     }
604b9c1b51eSKate Stone   } break;
6055c1ac4eaSJason Molenda 
606b9c1b51eSKate Stone   case EmulateInstruction::eContextPopRegisterOffStack: {
607b9c1b51eSKate Stone     const uint32_t reg_num =
608b9c1b51eSKate Stone         reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
609f366af30STamas Berghammer     const uint32_t generic_regnum = reg_info->kinds[eRegisterKindGeneric];
610b9c1b51eSKate Stone     if (reg_num != LLDB_INVALID_REGNUM &&
611b9c1b51eSKate Stone         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
612b9c1b51eSKate Stone       switch (context.info_type) {
613f5d3e66bSTamas Berghammer       case EmulateInstruction::eInfoTypeAddress:
61499c40e67STamas Berghammer         if (m_pushed_regs.find(reg_num) != m_pushed_regs.end() &&
615b9c1b51eSKate Stone             context.info.address == m_pushed_regs[reg_num]) {
616f5d3e66bSTamas Berghammer           m_curr_row->SetRegisterLocationToSame(reg_num,
617f5d3e66bSTamas Berghammer                                                 false /*must_replace*/);
61824a8378cSJason Molenda           m_curr_row_modified = true;
619df552edbSJason Molenda 
620df552edbSJason Molenda           // FP has been restored to its original value, we are back
621df552edbSJason Molenda           // to using SP to calculate the CFA.
622df552edbSJason Molenda           if (m_fp_is_cfa) {
623df552edbSJason Molenda             m_fp_is_cfa = false;
624df552edbSJason Molenda             RegisterInfo sp_reg_info;
625df552edbSJason Molenda             lldb::RegisterKind sp_reg_kind = eRegisterKindGeneric;
626df552edbSJason Molenda             uint32_t sp_reg_num = LLDB_REGNUM_GENERIC_SP;
627df552edbSJason Molenda             m_inst_emulator_up->GetRegisterInfo(sp_reg_kind, sp_reg_num,
628df552edbSJason Molenda                                                 sp_reg_info);
629df552edbSJason Molenda             RegisterValue sp_reg_val;
630df552edbSJason Molenda             if (GetRegisterValue(sp_reg_info, sp_reg_val)) {
631df552edbSJason Molenda               m_cfa_reg_info = sp_reg_info;
632df552edbSJason Molenda               const uint32_t cfa_reg_num =
633df552edbSJason Molenda                   sp_reg_info.kinds[m_unwind_plan_ptr->GetRegisterKind()];
634df552edbSJason Molenda               assert(cfa_reg_num != LLDB_INVALID_REGNUM);
635df552edbSJason Molenda               m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
636df552edbSJason Molenda                   cfa_reg_num, m_initial_sp - sp_reg_val.GetAsUInt64());
637df552edbSJason Molenda             }
638df552edbSJason Molenda           }
63931f1d2f5SGreg Clayton         }
640f5d3e66bSTamas Berghammer         break;
641f5d3e66bSTamas Berghammer       case EmulateInstruction::eInfoTypeISA:
642b9c1b51eSKate Stone         assert(
643b9c1b51eSKate Stone             (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
644f5d3e66bSTamas Berghammer              generic_regnum == LLDB_REGNUM_GENERIC_FLAGS) &&
645173946dcSBruce Mitchener             "eInfoTypeISA used for popping a register other the PC/FLAGS");
646b9c1b51eSKate Stone         if (generic_regnum != LLDB_REGNUM_GENERIC_FLAGS) {
647f5d3e66bSTamas Berghammer           m_curr_row->SetRegisterLocationToSame(reg_num,
648f5d3e66bSTamas Berghammer                                                 false /*must_replace*/);
649f5d3e66bSTamas Berghammer           m_curr_row_modified = true;
650e5b3498eSGreg Clayton         }
651f5d3e66bSTamas Berghammer         break;
652f5d3e66bSTamas Berghammer       default:
653f5d3e66bSTamas Berghammer         assert(false && "unhandled case, add code to handle this!");
654f5d3e66bSTamas Berghammer         break;
655f5d3e66bSTamas Berghammer       }
65699c40e67STamas Berghammer     }
657b9c1b51eSKate Stone   } break;
658e5b3498eSGreg Clayton 
65931f1d2f5SGreg Clayton   case EmulateInstruction::eContextSetFramePointer:
660b9c1b51eSKate Stone     if (!m_fp_is_cfa) {
66131f1d2f5SGreg Clayton       m_fp_is_cfa = true;
66231f1d2f5SGreg Clayton       m_cfa_reg_info = *reg_info;
663b9c1b51eSKate Stone       const uint32_t cfa_reg_num =
664b9c1b51eSKate Stone           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
66531f1d2f5SGreg Clayton       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
666b9c1b51eSKate Stone       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
667b9c1b51eSKate Stone           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
66824a8378cSJason Molenda       m_curr_row_modified = true;
669e5b3498eSGreg Clayton     }
6702ed751bdSGreg Clayton     break;
6712ed751bdSGreg Clayton 
6726853cca1SJason Molenda   case EmulateInstruction::eContextRestoreStackPointer:
6736853cca1SJason Molenda     if (m_fp_is_cfa) {
6746853cca1SJason Molenda       m_fp_is_cfa = false;
6756853cca1SJason Molenda       m_cfa_reg_info = *reg_info;
6766853cca1SJason Molenda       const uint32_t cfa_reg_num =
6776853cca1SJason Molenda           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
6786853cca1SJason Molenda       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
6796853cca1SJason Molenda       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
6806853cca1SJason Molenda           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
6816853cca1SJason Molenda       m_curr_row_modified = true;
6826853cca1SJason Molenda     }
6836853cca1SJason Molenda     break;
6846853cca1SJason Molenda 
6852ed751bdSGreg Clayton   case EmulateInstruction::eContextAdjustStackPointer:
68631f1d2f5SGreg Clayton     // If we have created a frame using the frame pointer, don't follow
68731f1d2f5SGreg Clayton     // subsequent adjustments to the stack pointer.
688b9c1b51eSKate Stone     if (!m_fp_is_cfa) {
689ab970f5eSPavel Labath       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
690ab970f5eSPavel Labath           m_curr_row->GetCFAValue().GetRegisterNumber(),
691ab970f5eSPavel Labath           m_initial_sp - reg_value.GetAsUInt64());
69224a8378cSJason Molenda       m_curr_row_modified = true;
69331f1d2f5SGreg Clayton     }
6942ed751bdSGreg Clayton     break;
6952ed751bdSGreg Clayton   }
6962ed751bdSGreg Clayton   return true;
6972ed751bdSGreg Clayton }
698