1 //===-- UnwindAssemblyInstEmulation.cpp -----------------------------------===//
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 LLDB_PLUGIN_DEFINE(UnwindAssemblyInstEmulation)
32 
33 //  UnwindAssemblyInstEmulation method definitions
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_up.get()) {
60 
61     // The instruction emulation subclass setup the unwind plan for the first
62     // instruction.
63     m_inst_emulator_up->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, nullptr, nullptr, range.GetBaseAddress(), opcode_data,
73         opcode_size, 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_up->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_up->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_up->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_up->SetInstruction(inst->GetOpcode(),
173                                                inst->GetAddress(), nullptr);
174 
175             if (last_condition !=
176                 m_inst_emulator_up->GetInstructionCondition()) {
177               if (m_inst_emulator_up->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, nullptr, nullptr, nullptr, &format, 0);
219               log->PutString(strm.GetString());
220             }
221 
222             last_condition = m_inst_emulator_up->GetInstructionCondition();
223 
224             m_inst_emulator_up->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_up(
300       EmulateInstruction::FindPlugin(arch, eInstructionTypePrologueEpilogue,
301                                      nullptr));
302   // Make sure that all prologue instructions are handled
303   if (inst_emulator_up)
304     return new UnwindAssemblyInstEmulation(arch, inst_emulator_up.release());
305   return nullptr;
306 }
307 
308 // PluginInterface protocol in UnwindAssemblyParser_x86
309 ConstString UnwindAssemblyInstEmulation::GetPluginName() {
310   return GetPluginNameStatic();
311 }
312 
313 uint32_t UnwindAssemblyInstEmulation::GetPluginVersion() { return 1; }
314 
315 void UnwindAssemblyInstEmulation::Initialize() {
316   PluginManager::RegisterPlugin(GetPluginNameStatic(),
317                                 GetPluginDescriptionStatic(), CreateInstance);
318 }
319 
320 void UnwindAssemblyInstEmulation::Terminate() {
321   PluginManager::UnregisterPlugin(CreateInstance);
322 }
323 
324 ConstString UnwindAssemblyInstEmulation::GetPluginNameStatic() {
325   static ConstString g_name("inst-emulation");
326   return g_name;
327 }
328 
329 const char *UnwindAssemblyInstEmulation::GetPluginDescriptionStatic() {
330   return "Instruction emulation based unwind information.";
331 }
332 
333 uint64_t UnwindAssemblyInstEmulation::MakeRegisterKindValuePair(
334     const RegisterInfo &reg_info) {
335   lldb::RegisterKind reg_kind;
336   uint32_t reg_num;
337   if (EmulateInstruction::GetBestRegisterKindAndNumber(&reg_info, reg_kind,
338                                                        reg_num))
339     return (uint64_t)reg_kind << 24 | reg_num;
340   return 0ull;
341 }
342 
343 void UnwindAssemblyInstEmulation::SetRegisterValue(
344     const RegisterInfo &reg_info, const RegisterValue &reg_value) {
345   m_register_values[MakeRegisterKindValuePair(reg_info)] = reg_value;
346 }
347 
348 bool UnwindAssemblyInstEmulation::GetRegisterValue(const RegisterInfo &reg_info,
349                                                    RegisterValue &reg_value) {
350   const uint64_t reg_id = MakeRegisterKindValuePair(reg_info);
351   RegisterValueMap::const_iterator pos = m_register_values.find(reg_id);
352   if (pos != m_register_values.end()) {
353     reg_value = pos->second;
354     return true; // We had a real value that comes from an opcode that wrote
355                  // to it...
356   }
357   // We are making up a value that is recognizable...
358   reg_value.SetUInt(reg_id, reg_info.byte_size);
359   return false;
360 }
361 
362 size_t UnwindAssemblyInstEmulation::ReadMemory(
363     EmulateInstruction *instruction, void *baton,
364     const EmulateInstruction::Context &context, lldb::addr_t addr, void *dst,
365     size_t dst_len) {
366   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
367 
368   if (log && log->GetVerbose()) {
369     StreamString strm;
370     strm.Printf(
371         "UnwindAssemblyInstEmulation::ReadMemory    (addr = 0x%16.16" PRIx64
372         ", dst = %p, dst_len = %" PRIu64 ", context = ",
373         addr, dst, (uint64_t)dst_len);
374     context.Dump(strm, instruction);
375     log->PutString(strm.GetString());
376   }
377   memset(dst, 0, dst_len);
378   return dst_len;
379 }
380 
381 size_t UnwindAssemblyInstEmulation::WriteMemory(
382     EmulateInstruction *instruction, void *baton,
383     const EmulateInstruction::Context &context, lldb::addr_t addr,
384     const void *dst, size_t dst_len) {
385   if (baton && dst && dst_len)
386     return ((UnwindAssemblyInstEmulation *)baton)
387         ->WriteMemory(instruction, context, addr, dst, dst_len);
388   return 0;
389 }
390 
391 size_t UnwindAssemblyInstEmulation::WriteMemory(
392     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
393     lldb::addr_t addr, const void *dst, size_t dst_len) {
394   DataExtractor data(dst, dst_len,
395                      instruction->GetArchitecture().GetByteOrder(),
396                      instruction->GetArchitecture().GetAddressByteSize());
397 
398   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
399 
400   if (log && log->GetVerbose()) {
401     StreamString strm;
402 
403     strm.PutCString("UnwindAssemblyInstEmulation::WriteMemory   (");
404     DumpDataExtractor(data, &strm, 0, eFormatBytes, 1, dst_len, UINT32_MAX,
405                       addr, 0, 0);
406     strm.PutCString(", context = ");
407     context.Dump(strm, instruction);
408     log->PutString(strm.GetString());
409   }
410 
411   const bool cant_replace = false;
412 
413   switch (context.type) {
414   default:
415   case EmulateInstruction::eContextInvalid:
416   case EmulateInstruction::eContextReadOpcode:
417   case EmulateInstruction::eContextImmediate:
418   case EmulateInstruction::eContextAdjustBaseRegister:
419   case EmulateInstruction::eContextRegisterPlusOffset:
420   case EmulateInstruction::eContextAdjustPC:
421   case EmulateInstruction::eContextRegisterStore:
422   case EmulateInstruction::eContextRegisterLoad:
423   case EmulateInstruction::eContextRelativeBranchImmediate:
424   case EmulateInstruction::eContextAbsoluteBranchRegister:
425   case EmulateInstruction::eContextSupervisorCall:
426   case EmulateInstruction::eContextTableBranchReadMemory:
427   case EmulateInstruction::eContextWriteRegisterRandomBits:
428   case EmulateInstruction::eContextWriteMemoryRandomBits:
429   case EmulateInstruction::eContextArithmetic:
430   case EmulateInstruction::eContextAdvancePC:
431   case EmulateInstruction::eContextReturnFromException:
432   case EmulateInstruction::eContextPopRegisterOffStack:
433   case EmulateInstruction::eContextAdjustStackPointer:
434     break;
435 
436   case EmulateInstruction::eContextPushRegisterOnStack: {
437     uint32_t reg_num = LLDB_INVALID_REGNUM;
438     uint32_t generic_regnum = LLDB_INVALID_REGNUM;
439     assert(context.info_type ==
440                EmulateInstruction::eInfoTypeRegisterToRegisterPlusOffset &&
441            "unhandled case, add code to handle this!");
442     const uint32_t unwind_reg_kind = m_unwind_plan_ptr->GetRegisterKind();
443     reg_num = context.info.RegisterToRegisterPlusOffset.data_reg
444                   .kinds[unwind_reg_kind];
445     generic_regnum = context.info.RegisterToRegisterPlusOffset.data_reg
446                          .kinds[eRegisterKindGeneric];
447 
448     if (reg_num != LLDB_INVALID_REGNUM &&
449         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
450       if (m_pushed_regs.find(reg_num) == m_pushed_regs.end()) {
451         m_pushed_regs[reg_num] = addr;
452         const int32_t offset = addr - m_initial_sp;
453         m_curr_row->SetRegisterLocationToAtCFAPlusOffset(reg_num, offset,
454                                                          cant_replace);
455         m_curr_row_modified = true;
456       }
457     }
458   } break;
459   }
460 
461   return dst_len;
462 }
463 
464 bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
465                                                void *baton,
466                                                const RegisterInfo *reg_info,
467                                                RegisterValue &reg_value) {
468 
469   if (baton && reg_info)
470     return ((UnwindAssemblyInstEmulation *)baton)
471         ->ReadRegister(instruction, reg_info, reg_value);
472   return false;
473 }
474 bool UnwindAssemblyInstEmulation::ReadRegister(EmulateInstruction *instruction,
475                                                const RegisterInfo *reg_info,
476                                                RegisterValue &reg_value) {
477   bool synthetic = GetRegisterValue(*reg_info, reg_value);
478 
479   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
480 
481   if (log && log->GetVerbose()) {
482 
483     StreamString strm;
484     strm.Printf("UnwindAssemblyInstEmulation::ReadRegister  (name = \"%s\") => "
485                 "synthetic_value = %i, value = ",
486                 reg_info->name, synthetic);
487     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
488     log->PutString(strm.GetString());
489   }
490   return true;
491 }
492 
493 bool UnwindAssemblyInstEmulation::WriteRegister(
494     EmulateInstruction *instruction, void *baton,
495     const EmulateInstruction::Context &context, const RegisterInfo *reg_info,
496     const RegisterValue &reg_value) {
497   if (baton && reg_info)
498     return ((UnwindAssemblyInstEmulation *)baton)
499         ->WriteRegister(instruction, context, reg_info, reg_value);
500   return false;
501 }
502 bool UnwindAssemblyInstEmulation::WriteRegister(
503     EmulateInstruction *instruction, const EmulateInstruction::Context &context,
504     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
505   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
506 
507   if (log && log->GetVerbose()) {
508 
509     StreamString strm;
510     strm.Printf(
511         "UnwindAssemblyInstEmulation::WriteRegister (name = \"%s\", value = ",
512         reg_info->name);
513     DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
514     strm.PutCString(", context = ");
515     context.Dump(strm, instruction);
516     log->PutString(strm.GetString());
517   }
518 
519   SetRegisterValue(*reg_info, reg_value);
520 
521   switch (context.type) {
522   case EmulateInstruction::eContextInvalid:
523   case EmulateInstruction::eContextReadOpcode:
524   case EmulateInstruction::eContextImmediate:
525   case EmulateInstruction::eContextAdjustBaseRegister:
526   case EmulateInstruction::eContextRegisterPlusOffset:
527   case EmulateInstruction::eContextAdjustPC:
528   case EmulateInstruction::eContextRegisterStore:
529   case EmulateInstruction::eContextSupervisorCall:
530   case EmulateInstruction::eContextTableBranchReadMemory:
531   case EmulateInstruction::eContextWriteRegisterRandomBits:
532   case EmulateInstruction::eContextWriteMemoryRandomBits:
533   case EmulateInstruction::eContextAdvancePC:
534   case EmulateInstruction::eContextReturnFromException:
535   case EmulateInstruction::eContextPushRegisterOnStack:
536   case EmulateInstruction::eContextRegisterLoad:
537     //            {
538     //                const uint32_t reg_num =
539     //                reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
540     //                if (reg_num != LLDB_INVALID_REGNUM)
541     //                {
542     //                    const bool can_replace_only_if_unspecified = true;
543     //
544     //                    m_curr_row.SetRegisterLocationToUndefined (reg_num,
545     //                                                               can_replace_only_if_unspecified,
546     //                                                               can_replace_only_if_unspecified);
547     //                    m_curr_row_modified = true;
548     //                }
549     //            }
550     break;
551 
552   case EmulateInstruction::eContextArithmetic: {
553     // If we adjusted the current frame pointer by a constant then adjust the
554     // CFA offset
555     // with the same amount.
556     lldb::RegisterKind kind = m_unwind_plan_ptr->GetRegisterKind();
557     if (m_fp_is_cfa && reg_info->kinds[kind] == m_cfa_reg_info.kinds[kind] &&
558         context.info_type == EmulateInstruction::eInfoTypeRegisterPlusOffset &&
559         context.info.RegisterPlusOffset.reg.kinds[kind] ==
560             m_cfa_reg_info.kinds[kind]) {
561       const int64_t offset = context.info.RegisterPlusOffset.signed_offset;
562       m_curr_row->GetCFAValue().IncOffset(-1 * offset);
563       m_curr_row_modified = true;
564     }
565   } break;
566 
567   case EmulateInstruction::eContextAbsoluteBranchRegister:
568   case EmulateInstruction::eContextRelativeBranchImmediate: {
569     if (context.info_type == EmulateInstruction::eInfoTypeISAAndImmediate &&
570         context.info.ISAAndImmediate.unsigned_data32 > 0) {
571       m_forward_branch_offset =
572           context.info.ISAAndImmediateSigned.signed_data32;
573     } else if (context.info_type ==
574                    EmulateInstruction::eInfoTypeISAAndImmediateSigned &&
575                context.info.ISAAndImmediateSigned.signed_data32 > 0) {
576       m_forward_branch_offset = context.info.ISAAndImmediate.unsigned_data32;
577     } else if (context.info_type == EmulateInstruction::eInfoTypeImmediate &&
578                context.info.unsigned_immediate > 0) {
579       m_forward_branch_offset = context.info.unsigned_immediate;
580     } else if (context.info_type ==
581                    EmulateInstruction::eInfoTypeImmediateSigned &&
582                context.info.signed_immediate > 0) {
583       m_forward_branch_offset = context.info.signed_immediate;
584     }
585   } break;
586 
587   case EmulateInstruction::eContextPopRegisterOffStack: {
588     const uint32_t reg_num =
589         reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
590     const uint32_t generic_regnum = reg_info->kinds[eRegisterKindGeneric];
591     if (reg_num != LLDB_INVALID_REGNUM &&
592         generic_regnum != LLDB_REGNUM_GENERIC_SP) {
593       switch (context.info_type) {
594       case EmulateInstruction::eInfoTypeAddress:
595         if (m_pushed_regs.find(reg_num) != m_pushed_regs.end() &&
596             context.info.address == m_pushed_regs[reg_num]) {
597           m_curr_row->SetRegisterLocationToSame(reg_num,
598                                                 false /*must_replace*/);
599           m_curr_row_modified = true;
600         }
601         break;
602       case EmulateInstruction::eInfoTypeISA:
603         assert(
604             (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
605              generic_regnum == LLDB_REGNUM_GENERIC_FLAGS) &&
606             "eInfoTypeISA used for popping a register other the PC/FLAGS");
607         if (generic_regnum != LLDB_REGNUM_GENERIC_FLAGS) {
608           m_curr_row->SetRegisterLocationToSame(reg_num,
609                                                 false /*must_replace*/);
610           m_curr_row_modified = true;
611         }
612         break;
613       default:
614         assert(false && "unhandled case, add code to handle this!");
615         break;
616       }
617     }
618   } break;
619 
620   case EmulateInstruction::eContextSetFramePointer:
621     if (!m_fp_is_cfa) {
622       m_fp_is_cfa = true;
623       m_cfa_reg_info = *reg_info;
624       const uint32_t cfa_reg_num =
625           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
626       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
627       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
628           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
629       m_curr_row_modified = true;
630     }
631     break;
632 
633   case EmulateInstruction::eContextRestoreStackPointer:
634     if (m_fp_is_cfa) {
635       m_fp_is_cfa = false;
636       m_cfa_reg_info = *reg_info;
637       const uint32_t cfa_reg_num =
638           reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
639       assert(cfa_reg_num != LLDB_INVALID_REGNUM);
640       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
641           cfa_reg_num, m_initial_sp - reg_value.GetAsUInt64());
642       m_curr_row_modified = true;
643     }
644     break;
645 
646   case EmulateInstruction::eContextAdjustStackPointer:
647     // If we have created a frame using the frame pointer, don't follow
648     // subsequent adjustments to the stack pointer.
649     if (!m_fp_is_cfa) {
650       m_curr_row->GetCFAValue().SetIsRegisterPlusOffset(
651           m_curr_row->GetCFAValue().GetRegisterNumber(),
652           m_initial_sp - reg_value.GetAsUInt64());
653       m_curr_row_modified = true;
654     }
655     break;
656   }
657   return true;
658 }
659