1 //===-- UnwindAssemblyInstEmulation.cpp --------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "UnwindAssemblyInstEmulation.h"
10 
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/Disassembler.h"
13 #include "lldb/Core/DumpDataExtractor.h"
14 #include "lldb/Core/DumpRegisterValue.h"
15 #include "lldb/Core/FormatEntity.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Target/ExecutionContext.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/Thread.h"
21 #include "lldb/Utility/ArchSpec.h"
22 #include "lldb/Utility/DataBufferHeap.h"
23 #include "lldb/Utility/DataExtractor.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Status.h"
26 #include "lldb/Utility/StreamString.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 //-----------------------------------------------------------------------------------------------
32 //  UnwindAssemblyInstEmulation method definitions
33 //-----------------------------------------------------------------------------------------------
34 
35 bool UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly(
36     AddressRange &range, Thread &thread, UnwindPlan &unwind_plan) {
37   std::vector<uint8_t> function_text(range.GetByteSize());
38   ProcessSP process_sp(thread.GetProcess());
39   if (process_sp) {
40     Status error;
41     const bool prefer_file_cache = true;
42     if (process_sp->GetTarget().ReadMemory(
43             range.GetBaseAddress(), prefer_file_cache, function_text.data(),
44             range.GetByteSize(), error) != range.GetByteSize()) {
45       return false;
46     }
47   }
48   return GetNonCallSiteUnwindPlanFromAssembly(
49       range, function_text.data(), function_text.size(), unwind_plan);
50 }
51 
52 bool UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly(
53     AddressRange &range, uint8_t *opcode_data, size_t opcode_size,
54     UnwindPlan &unwind_plan) {
55   if (opcode_data == nullptr || opcode_size == 0)
56     return false;
57 
58   if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid() &&
59       m_inst_emulator_ap.get()) {
60 
61     // The instruction emulation subclass setup the unwind plan for the first
62     // instruction.
63     m_inst_emulator_ap->CreateFunctionEntryUnwind(unwind_plan);
64 
65     // CreateFunctionEntryUnwind should have created the first row. If it
66     // doesn't, then we are done.
67     if (unwind_plan.GetRowCount() == 0)
68       return false;
69 
70     const bool prefer_file_cache = true;
71     DisassemblerSP disasm_sp(Disassembler::DisassembleBytes(
72         m_arch, NULL, NULL, range.GetBaseAddress(), opcode_data, opcode_size,
73         99999, prefer_file_cache));
74 
75     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
76 
77     if (disasm_sp) {
78 
79       m_range_ptr = &range;
80       m_unwind_plan_ptr = &unwind_plan;
81 
82       const uint32_t addr_byte_size = m_arch.GetAddressByteSize();
83       const bool show_address = true;
84       const bool show_bytes = true;
85       m_inst_emulator_ap->GetRegisterInfo(unwind_plan.GetRegisterKind(),
86                                           unwind_plan.GetInitialCFARegister(),
87                                           m_cfa_reg_info);
88 
89       m_fp_is_cfa = false;
90       m_register_values.clear();
91       m_pushed_regs.clear();
92 
93       // Initialize the CFA with a known value. In the 32 bit case it will be
94       // 0x80000000, and in the 64 bit case 0x8000000000000000. We use the
95       // address byte size to be safe for any future address sizes
96       m_initial_sp = (1ull << ((addr_byte_size * 8) - 1));
97       RegisterValue cfa_reg_value;
98       cfa_reg_value.SetUInt(m_initial_sp, m_cfa_reg_info.byte_size);
99       SetRegisterValue(m_cfa_reg_info, cfa_reg_value);
100 
101       const InstructionList &inst_list = disasm_sp->GetInstructionList();
102       const size_t num_instructions = inst_list.GetSize();
103 
104       if (num_instructions > 0) {
105         Instruction *inst = inst_list.GetInstructionAtIndex(0).get();
106         const lldb::addr_t base_addr = inst->GetAddress().GetFileAddress();
107 
108         // Map for storing the unwind plan row and the value of the registers
109         // at a given offset. When we see a forward branch we add a new entry
110         // to this map with the actual unwind plan row and register context for
111         // the target address of the branch as the current data have to be
112         // valid for the target address of the branch too if we are in the same
113         // function.
114         std::map<lldb::addr_t, std::pair<UnwindPlan::RowSP, RegisterValueMap>>
115             saved_unwind_states;
116 
117         // Make a copy of the current instruction Row and save it in m_curr_row
118         // so we can add updates as we process the instructions.
119         UnwindPlan::RowSP last_row = unwind_plan.GetLastRow();
120         UnwindPlan::Row *newrow = new UnwindPlan::Row;
121         if (last_row.get())
122           *newrow = *last_row.get();
123         m_curr_row.reset(newrow);
124 
125         // Add the initial state to the save list with offset 0.
126         saved_unwind_states.insert({0, {last_row, m_register_values}});
127 
128         // cache the pc register number (in whatever register numbering this
129         // UnwindPlan uses) for quick reference during instruction parsing.
130         RegisterInfo pc_reg_info;
131         m_inst_emulator_ap->GetRegisterInfo(
132             eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc_reg_info);
133 
134         // cache the return address register number (in whatever register
135         // numbering this UnwindPlan uses) for quick reference during
136         // instruction parsing.
137         RegisterInfo ra_reg_info;
138         m_inst_emulator_ap->GetRegisterInfo(
139             eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, ra_reg_info);
140 
141         // The architecture dependent condition code of the last processed
142         // instruction.
143         EmulateInstruction::InstructionCondition last_condition =
144             EmulateInstruction::UnconditionalCondition;
145         lldb::addr_t condition_block_start_offset = 0;
146 
147         for (size_t idx = 0; idx < num_instructions; ++idx) {
148           m_curr_row_modified = false;
149           m_forward_branch_offset = 0;
150 
151           inst = inst_list.GetInstructionAtIndex(idx).get();
152           if (inst) {
153             lldb::addr_t current_offset =
154                 inst->GetAddress().GetFileAddress() - base_addr;
155             auto it = saved_unwind_states.upper_bound(current_offset);
156             assert(it != saved_unwind_states.begin() &&
157                    "Unwind row for the function entry missing");
158             --it; // Move it to the row corresponding to the current offset
159 
160             // If the offset of m_curr_row don't match with the offset we see
161             // in saved_unwind_states then we have to update m_curr_row and
162             // m_register_values based on the saved values. It is happening
163             // after we processed an epilogue and a return to caller
164             // instruction.
165             if (it->second.first->GetOffset() != m_curr_row->GetOffset()) {
166               UnwindPlan::Row *newrow = new UnwindPlan::Row;
167               *newrow = *it->second.first;
168               m_curr_row.reset(newrow);
169               m_register_values = it->second.second;
170             }
171 
172             m_inst_emulator_ap->SetInstruction(inst->GetOpcode(),
173                                                inst->GetAddress(), nullptr);
174 
175             if (last_condition !=
176                 m_inst_emulator_ap->GetInstructionCondition()) {
177               if (m_inst_emulator_ap->GetInstructionCondition() !=
178                       EmulateInstruction::UnconditionalCondition &&
179                   saved_unwind_states.count(current_offset) == 0) {
180                 // If we don't have a saved row for the current offset then
181                 // save our current state because we will have to restore it
182                 // after the conditional block.
183                 auto new_row =
184                     std::make_shared<UnwindPlan::Row>(*m_curr_row.get());
185                 saved_unwind_states.insert(
186                     {current_offset, {new_row, m_register_values}});
187               }
188 
189               // If the last instruction was conditional with a different
190               // condition then the then current condition then restore the
191               // condition.
192               if (last_condition !=
193                   EmulateInstruction::UnconditionalCondition) {
194                 const auto &saved_state =
195                     saved_unwind_states.at(condition_block_start_offset);
196                 m_curr_row =
197                     std::make_shared<UnwindPlan::Row>(*saved_state.first);
198                 m_curr_row->SetOffset(current_offset);
199                 m_register_values = saved_state.second;
200                 bool replace_existing =
201                     true; // The last instruction might already
202                           // created a row for this offset and
203                           // we want to overwrite it.
204                 unwind_plan.InsertRow(
205                     std::make_shared<UnwindPlan::Row>(*m_curr_row),
206                     replace_existing);
207               }
208 
209               // We are starting a new conditional block at the actual offset
210               condition_block_start_offset = current_offset;
211             }
212 
213             if (log && log->GetVerbose()) {
214               StreamString strm;
215               lldb_private::FormatEntity::Entry format;
216               FormatEntity::Parse("${frame.pc}: ", format);
217               inst->Dump(&strm, inst_list.GetMaxOpcocdeByteSize(), show_address,
218                          show_bytes, NULL, NULL, NULL, &format, 0);
219               log->PutString(strm.GetString());
220             }
221 
222             last_condition = m_inst_emulator_ap->GetInstructionCondition();
223 
224             m_inst_emulator_ap->EvaluateInstruction(
225                 eEmulateInstructionOptionIgnoreConditions);
226 
227             // If the current instruction is a branch forward then save the
228             // current CFI information for the offset where we are branching.
229             if (m_forward_branch_offset != 0 &&
230                 range.ContainsFileAddress(inst->GetAddress().GetFileAddress() +
231                                           m_forward_branch_offset)) {
232               auto newrow =
233                   std::make_shared<UnwindPlan::Row>(*m_curr_row.get());
234               newrow->SetOffset(current_offset + m_forward_branch_offset);
235               saved_unwind_states.insert(
236                   {current_offset + m_forward_branch_offset,
237                    {newrow, m_register_values}});
238               unwind_plan.InsertRow(newrow);
239             }
240 
241             // Were there any changes to the CFI while evaluating this
242             // instruction?
243             if (m_curr_row_modified) {
244               // Save the modified row if we don't already have a CFI row in
245               // the current address
246               if (saved_unwind_states.count(
247                       current_offset + inst->GetOpcode().GetByteSize()) == 0) {
248                 m_curr_row->SetOffset(current_offset +
249                                       inst->GetOpcode().GetByteSize());
250                 unwind_plan.InsertRow(m_curr_row);
251                 saved_unwind_states.insert(
252                     {current_offset + inst->GetOpcode().GetByteSize(),
253                      {m_curr_row, m_register_values}});
254 
255                 // Allocate a new Row for m_curr_row, copy the current state
256                 // into it
257                 UnwindPlan::Row *newrow = new UnwindPlan::Row;
258                 *newrow = *m_curr_row.get();
259                 m_curr_row.reset(newrow);
260               }
261             }
262           }
263         }
264       }
265     }
266 
267     if (log && log->GetVerbose()) {
268       StreamString strm;
269       lldb::addr_t base_addr = range.GetBaseAddress().GetFileAddress();
270       strm.Printf("Resulting unwind rows for [0x%" PRIx64 " - 0x%" PRIx64 "):",
271                   base_addr, base_addr + range.GetByteSize());
272       unwind_plan.Dump(strm, nullptr, base_addr);
273       log->PutString(strm.GetString());
274     }
275     return unwind_plan.GetRowCount() > 0;
276   }
277   return false;
278 }
279 
280 bool UnwindAssemblyInstEmulation::AugmentUnwindPlanFromCallSite(
281     AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) {
282   return false;
283 }
284 
285 bool UnwindAssemblyInstEmulation::GetFastUnwindPlan(AddressRange &func,
286                                                     Thread &thread,
287                                                     UnwindPlan &unwind_plan) {
288   return false;
289 }
290 
291 bool UnwindAssemblyInstEmulation::FirstNonPrologueInsn(
292     AddressRange &func, const ExecutionContext &exe_ctx,
293     Address &first_non_prologue_insn) {
294   return false;
295 }
296 
297 UnwindAssembly *
298 UnwindAssemblyInstEmulation::CreateInstance(const ArchSpec &arch) {
299   std::unique_ptr<EmulateInstruction> inst_emulator_ap(
300       EmulateInstruction::FindPlugin(arch, eInstructionTypePrologueEpilogue,
301                                      NULL));
302   // Make sure that all prologue instructions are handled
303   if (inst_emulator_ap.get())
304     return new UnwindAssemblyInstEmulation(arch, inst_emulator_ap.release());
305   return NULL;
306 }
307 
308 //------------------------------------------------------------------
309 // PluginInterface protocol in UnwindAssemblyParser_x86
310 //------------------------------------------------------------------
311 ConstString UnwindAssemblyInstEmulation::GetPluginName() {
312   return GetPluginNameStatic();
313 }
314 
315 uint32_t UnwindAssemblyInstEmulation::GetPluginVersion() { return 1; }
316 
317 void UnwindAssemblyInstEmulation::Initialize() {
318   PluginManager::RegisterPlugin(GetPluginNameStatic(),
319                                 GetPluginDescriptionStatic(), CreateInstance);
320 }
321 
322 void UnwindAssemblyInstEmulation::Terminate() {
323   PluginManager::UnregisterPlugin(CreateInstance);
324 }
325 
326 ConstString UnwindAssemblyInstEmulation::GetPluginNameStatic() {
327   static ConstString g_name("inst-emulation");
328   return g_name;
329 }
330 
331 const char *UnwindAssemblyInstEmulation::GetPluginDescriptionStatic() {
332   return "Instruction emulation based unwind information.";
333 }
334 
335 uint64_t UnwindAssemblyInstEmulation::MakeRegisterKindValuePair(
336     const RegisterInfo &reg_info) {
337   lldb::RegisterKind reg_kind;
338   uint32_t reg_num;
339   if (EmulateInstruction::GetBestRegisterKindAndNumber(&reg_info, reg_kind,
340                                                        reg_num))
341     return (uint64_t)reg_kind << 24 | reg_num;
342   return 0ull;
343 }
344 
345 void UnwindAssemblyInstEmulation::SetRegisterValue(
346     const RegisterInfo &reg_info, const RegisterValue &reg_value) {
347   m_register_values[MakeRegisterKindValuePair(reg_info)] = reg_value;
348 }
349 
350 bool UnwindAssemblyInstEmulation::GetRegisterValue(const RegisterInfo &reg_info,
351                                                    RegisterValue &reg_value) {
352   const uint64_t reg_id = MakeRegisterKindValuePair(reg_info);
353   RegisterValueMap::const_iterator pos = m_register_values.find(reg_id);
354   if (pos != m_register_values.end()) {
355     reg_value = pos->second;
356     return true; // We had a real value that comes from an opcode that wrote
357                  // to it...
358   }
359   // We are making up a value that is recognizable...
360   reg_value.SetUInt(reg_id, reg_info.byte_size);
361   return false;
362 }
363 
364 size_t UnwindAssemblyInstEmulation::ReadMemory(
365     EmulateInstruction *instruction, void *baton,
366     const EmulateInstruction::Context &context, lldb::addr_t addr, void *dst,
367     size_t dst_len) {
368   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
369 
370   if (log && log->GetVerbose()) {
371     StreamString strm;
372     strm.Printf(
373         "UnwindAssemblyInstEmulation::ReadMemory    (addr = 0x%16.16" PRIx64
374         ", dst = %p, dst_len = %" PRIu64 ", context = ",
375         addr, dst, (uint64_t)dst_len);
376     context.Dump(strm, instruction);
377     log->PutString(strm.GetString());
378   }
379   memset(dst, 0, dst_len);
380   return dst_len;
381 }
382 
383 size_t UnwindAssemblyInstEmulation::WriteMemory(
384     EmulateInstruction *instruction, void *baton,
385     const EmulateInstruction::Context &context, lldb::addr_t addr,
386     const void *dst, size_t dst_len) {
387   if (baton && dst && dst_len)
388     return ((UnwindAssemblyInstEmulation *)baton)
389         ->WriteMemory(instruction, context, addr, dst, dst_len);
390   return 0;
391 }
392 
393 size_t UnwindAssemblyInstEmulation::WriteMemory(
394     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
395     lldb::addr_t addr, const void *dst, size_t dst_len) {
396   DataExtractor data(dst, dst_len,
397                      instruction->GetArchitecture().GetByteOrder(),
398                      instruction->GetArchitecture().GetAddressByteSize());
399 
400   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
401 
402   if (log && log->GetVerbose()) {
403     StreamString strm;
404 
405     strm.PutCString("UnwindAssemblyInstEmulation::WriteMemory   (");
406     DumpDataExtractor(data, &strm, 0, eFormatBytes, 1, dst_len, UINT32_MAX,
407                       addr, 0, 0);
408     strm.PutCString(", context = ");
409     context.Dump(strm, instruction);
410     log->PutString(strm.GetString());
411   }
412 
413   const bool cant_replace = false;
414 
415   switch (context.type) {
416   default:
417   case EmulateInstruction::eContextInvalid:
418   case EmulateInstruction::eContextReadOpcode:
419   case EmulateInstruction::eContextImmediate:
420   case EmulateInstruction::eContextAdjustBaseRegister:
421   case EmulateInstruction::eContextRegisterPlusOffset:
422   case EmulateInstruction::eContextAdjustPC:
423   case EmulateInstruction::eContextRegisterStore:
424   case EmulateInstruction::eContextRegisterLoad:
425   case EmulateInstruction::eContextRelativeBranchImmediate:
426   case EmulateInstruction::eContextAbsoluteBranchRegister:
427   case EmulateInstruction::eContextSupervisorCall:
428   case EmulateInstruction::eContextTableBranchReadMemory:
429   case EmulateInstruction::eContextWriteRegisterRandomBits:
430   case EmulateInstruction::eContextWriteMemoryRandomBits:
431   case EmulateInstruction::eContextArithmetic:
432   case EmulateInstruction::eContextAdvancePC:
433   case EmulateInstruction::eContextReturnFromException:
434   case EmulateInstruction::eContextPopRegisterOffStack:
435   case EmulateInstruction::eContextAdjustStackPointer:
436     break;
437 
438   case EmulateInstruction::eContextPushRegisterOnStack: {
439     uint32_t reg_num = LLDB_INVALID_REGNUM;
440     uint32_t generic_regnum = LLDB_INVALID_REGNUM;
441     assert(context.info_type ==
442                EmulateInstruction::eInfoTypeRegisterToRegisterPlusOffset &&
443            "unhandled case, add code to handle this!");
444     const uint32_t unwind_reg_kind = m_unwind_plan_ptr->GetRegisterKind();
445     reg_num = context.info.RegisterToRegisterPlusOffset.data_reg
446                   .kinds[unwind_reg_kind];
447     generic_regnum = context.info.RegisterToRegisterPlusOffset.data_reg
448                          .kinds[eRegisterKindGeneric];
449 
450     if (reg_num != LLDB_INVALID_REGNUM &&
451         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
452       if (m_pushed_regs.find(reg_num) == m_pushed_regs.end()) {
453         m_pushed_regs[reg_num] = addr;
454         const int32_t offset = addr - m_initial_sp;
455         m_curr_row->SetRegisterLocationToAtCFAPlusOffset(reg_num, offset,
456                                                          cant_replace);
457         m_curr_row_modified = true;
458       }
459     }
460   } break;
461   }
462 
463   return dst_len;
464 }
465 
466 bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
467                                                void *baton,
468                                                const RegisterInfo *reg_info,
469                                                RegisterValue &reg_value) {
470 
471   if (baton && reg_info)
472     return ((UnwindAssemblyInstEmulation *)baton)
473         ->ReadRegister(instruction, reg_info, reg_value);
474   return false;
475 }
476 bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
477                                                const RegisterInfo *reg_info,
478                                                RegisterValue &reg_value) {
479   bool synthetic = GetRegisterValue(*reg_info, reg_value);
480 
481   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
482 
483   if (log && log->GetVerbose()) {
484 
485     StreamString strm;
486     strm.Printf("UnwindAssemblyInstEmulation::ReadRegister  (name = \"%s\") => "
487                 "synthetic_value = %i, value = ",
488                 reg_info->name, synthetic);
489     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
490     log->PutString(strm.GetString());
491   }
492   return true;
493 }
494 
495 bool UnwindAssemblyInstEmulation::WriteRegister(
496     EmulateInstruction *instruction, void *baton,
497     const EmulateInstruction::Context &context, const RegisterInfo *reg_info,
498     const RegisterValue &reg_value) {
499   if (baton && reg_info)
500     return ((UnwindAssemblyInstEmulation *)baton)
501         ->WriteRegister(instruction, context, reg_info, reg_value);
502   return false;
503 }
504 bool UnwindAssemblyInstEmulation::WriteRegister(
505     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
506     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
507   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
508 
509   if (log && log->GetVerbose()) {
510 
511     StreamString strm;
512     strm.Printf(
513         "UnwindAssemblyInstEmulation::WriteRegister (name = \"%s\", value = ",
514         reg_info->name);
515     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
516     strm.PutCString(", context = ");
517     context.Dump(strm, instruction);
518     log->PutString(strm.GetString());
519   }
520 
521   SetRegisterValue(*reg_info, reg_value);
522 
523   switch (context.type) {
524   case EmulateInstruction::eContextInvalid:
525   case EmulateInstruction::eContextReadOpcode:
526   case EmulateInstruction::eContextImmediate:
527   case EmulateInstruction::eContextAdjustBaseRegister:
528   case EmulateInstruction::eContextRegisterPlusOffset:
529   case EmulateInstruction::eContextAdjustPC:
530   case EmulateInstruction::eContextRegisterStore:
531   case EmulateInstruction::eContextSupervisorCall:
532   case EmulateInstruction::eContextTableBranchReadMemory:
533   case EmulateInstruction::eContextWriteRegisterRandomBits:
534   case EmulateInstruction::eContextWriteMemoryRandomBits:
535   case EmulateInstruction::eContextAdvancePC:
536   case EmulateInstruction::eContextReturnFromException:
537   case EmulateInstruction::eContextPushRegisterOnStack:
538   case EmulateInstruction::eContextRegisterLoad:
539     //            {
540     //                const uint32_t reg_num =
541     //                reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
542     //                if (reg_num != LLDB_INVALID_REGNUM)
543     //                {
544     //                    const bool can_replace_only_if_unspecified = true;
545     //
546     //                    m_curr_row.SetRegisterLocationToUndefined (reg_num,
547     //                                                               can_replace_only_if_unspecified,
548     //                                                               can_replace_only_if_unspecified);
549     //                    m_curr_row_modified = true;
550     //                }
551     //            }
552     break;
553 
554   case EmulateInstruction::eContextArithmetic: {
555     // If we adjusted the current frame pointer by a constant then adjust the
556     // CFA offset
557     // with the same amount.
558     lldb::RegisterKind kind = m_unwind_plan_ptr->GetRegisterKind();
559     if (m_fp_is_cfa && reg_info->kinds[kind] == m_cfa_reg_info.kinds[kind] &&
560         context.info_type == EmulateInstruction::eInfoTypeRegisterPlusOffset &&
561         context.info.RegisterPlusOffset.reg.kinds[kind] ==
562             m_cfa_reg_info.kinds[kind]) {
563       const int64_t offset = context.info.RegisterPlusOffset.signed_offset;
564       m_curr_row->GetCFAValue().IncOffset(-1 * offset);
565       m_curr_row_modified = true;
566     }
567   } break;
568 
569   case EmulateInstruction::eContextAbsoluteBranchRegister:
570   case EmulateInstruction::eContextRelativeBranchImmediate: {
571     if (context.info_type == EmulateInstruction::eInfoTypeISAAndImmediate &&
572         context.info.ISAAndImmediate.unsigned_data32 > 0) {
573       m_forward_branch_offset =
574           context.info.ISAAndImmediateSigned.signed_data32;
575     } else if (context.info_type ==
576                    EmulateInstruction::eInfoTypeISAAndImmediateSigned &&
577                context.info.ISAAndImmediateSigned.signed_data32 > 0) {
578       m_forward_branch_offset = context.info.ISAAndImmediate.unsigned_data32;
579     } else if (context.info_type == EmulateInstruction::eInfoTypeImmediate &&
580                context.info.unsigned_immediate > 0) {
581       m_forward_branch_offset = context.info.unsigned_immediate;
582     } else if (context.info_type ==
583                    EmulateInstruction::eInfoTypeImmediateSigned &&
584                context.info.signed_immediate > 0) {
585       m_forward_branch_offset = context.info.signed_immediate;
586     }
587   } break;
588 
589   case EmulateInstruction::eContextPopRegisterOffStack: {
590     const uint32_t reg_num =
591         reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
592     const uint32_t generic_regnum = reg_info->kinds[eRegisterKindGeneric];
593     if (reg_num != LLDB_INVALID_REGNUM &&
594         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
595       switch (context.info_type) {
596       case EmulateInstruction::eInfoTypeAddress:
597         if (m_pushed_regs.find(reg_num) != m_pushed_regs.end() &&
598             context.info.address == m_pushed_regs[reg_num]) {
599           m_curr_row->SetRegisterLocationToSame(reg_num,
600                                                 false /*must_replace*/);
601           m_curr_row_modified = true;
602         }
603         break;
604       case EmulateInstruction::eInfoTypeISA:
605         assert(
606             (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
607              generic_regnum == LLDB_REGNUM_GENERIC_FLAGS) &&
608             "eInfoTypeISA used for popping a register other the PC/FLAGS");
609         if (generic_regnum != LLDB_REGNUM_GENERIC_FLAGS) {
610           m_curr_row->SetRegisterLocationToSame(reg_num,
611                                                 false /*must_replace*/);
612           m_curr_row_modified = true;
613         }
614         break;
615       default:
616         assert(false && "unhandled case, add code to handle this!");
617         break;
618       }
619     }
620   } break;
621 
622   case EmulateInstruction::eContextSetFramePointer:
623     if (!m_fp_is_cfa) {
624       m_fp_is_cfa = true;
625       m_cfa_reg_info = *reg_info;
626       const uint32_t cfa_reg_num =
627           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
628       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
629       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
630           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
631       m_curr_row_modified = true;
632     }
633     break;
634 
635   case EmulateInstruction::eContextRestoreStackPointer:
636     if (m_fp_is_cfa) {
637       m_fp_is_cfa = false;
638       m_cfa_reg_info = *reg_info;
639       const uint32_t cfa_reg_num =
640           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
641       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
642       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
643           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
644       m_curr_row_modified = true;
645     }
646     break;
647 
648   case EmulateInstruction::eContextAdjustStackPointer:
649     // If we have created a frame using the frame pointer, don't follow
650     // subsequent adjustments to the stack pointer.
651     if (!m_fp_is_cfa) {
652       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
653           m_curr_row->GetCFAValue().GetRegisterNumber(),
654           m_initial_sp - reg_value.GetAsUInt64());
655       m_curr_row_modified = true;
656     }
657     break;
658   }
659   return true;
660 }
661