1 //===-- UnwindAssemblyInstEmulation.cpp --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnwindAssemblyInstEmulation.h"
11 
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/DataExtractor.h"
16 #include "lldb/Core/Disassembler.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/StreamString.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/Target.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 
30 
31 //-----------------------------------------------------------------------------------------------
32 //  UnwindAssemblyInstEmulation method definitions
33 //-----------------------------------------------------------------------------------------------
34 
35 bool
36 UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& range,
37                                                                    Thread& thread,
38                                                                    UnwindPlan& unwind_plan)
39 {
40     if (range.GetByteSize() > 0 &&
41         range.GetBaseAddress().IsValid() &&
42         m_inst_emulator_ap.get())
43     {
44 
45         // The instruction emulation subclass setup the unwind plan for the
46         // first instruction.
47         m_inst_emulator_ap->CreateFunctionEntryUnwind (unwind_plan);
48 
49         // CreateFunctionEntryUnwind should have created the first row. If it
50         // doesn't, then we are done.
51         if (unwind_plan.GetRowCount() == 0)
52             return false;
53 
54         ExecutionContext exe_ctx;
55         thread.CalculateExecutionContext(exe_ctx);
56         const bool prefer_file_cache = true;
57         DisassemblerSP disasm_sp (Disassembler::DisassembleRange (m_arch,
58                                                                   NULL,
59                                                                   NULL,
60                                                                   exe_ctx,
61                                                                   range,
62                                                                   prefer_file_cache));
63 
64         Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
65 
66         if (disasm_sp)
67         {
68 
69             m_range_ptr = ⦥
70             m_thread_ptr = &thread;
71             m_unwind_plan_ptr = &unwind_plan;
72 
73             const uint32_t addr_byte_size = m_arch.GetAddressByteSize();
74             const bool show_address = true;
75             const bool show_bytes = true;
76             m_inst_emulator_ap->GetRegisterInfo (unwind_plan.GetRegisterKind(),
77                                                  unwind_plan.GetInitialCFARegister(),
78                                                  m_cfa_reg_info);
79 
80             m_fp_is_cfa = false;
81             m_register_values.clear();
82             m_pushed_regs.clear();
83 
84             // Initialize the CFA with a known value. In the 32 bit case
85             // it will be 0x80000000, and in the 64 bit case 0x8000000000000000.
86             // We use the address byte size to be safe for any future address sizes
87             m_initial_sp = (1ull << ((addr_byte_size * 8) - 1));
88             RegisterValue cfa_reg_value;
89             cfa_reg_value.SetUInt (m_initial_sp, m_cfa_reg_info.byte_size);
90             SetRegisterValue (m_cfa_reg_info, cfa_reg_value);
91 
92             const InstructionList &inst_list = disasm_sp->GetInstructionList ();
93             const size_t num_instructions = inst_list.GetSize();
94 
95             if (num_instructions > 0)
96             {
97                 Instruction *inst = inst_list.GetInstructionAtIndex (0).get();
98                 const addr_t base_addr = inst->GetAddress().GetFileAddress();
99 
100                 // Make a copy of the current instruction Row and save it in m_curr_row
101                 // so we can add updates as we process the instructions.
102                 UnwindPlan::RowSP last_row = unwind_plan.GetLastRow();
103                 UnwindPlan::Row *newrow = new UnwindPlan::Row;
104                 if (last_row.get())
105                     *newrow = *last_row.get();
106                 m_curr_row.reset(newrow);
107 
108                 // Once we've seen the initial prologue instructions complete, save a
109                 // copy of the CFI at that point into prologue_completed_row for possible
110                 // use later.
111                 int instructions_since_last_prologue_insn = 0;     // # of insns since last CFI was update
112 
113                 bool reinstate_prologue_next_instruction = false;  // Next iteration, re-install the prologue row of CFI
114 
115                 bool last_instruction_restored_return_addr_reg = false;  // re-install the prologue row of CFI if the next instruction is a branch immediate
116 
117                 bool return_address_register_has_been_saved = false; // if we've seen the ra register get saved yet
118 
119                 UnwindPlan::RowSP prologue_completed_row;          // copy of prologue row of CFI
120 
121                 // cache the pc register number (in whatever register numbering this UnwindPlan uses) for
122                 // quick reference during instruction parsing.
123                 uint32_t pc_reg_num = LLDB_INVALID_REGNUM;
124                 RegisterInfo pc_reg_info;
125                 if (m_inst_emulator_ap->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc_reg_info))
126                     pc_reg_num = pc_reg_info.kinds[unwind_plan.GetRegisterKind()];
127                 else
128                     pc_reg_num = LLDB_INVALID_REGNUM;
129 
130                 // cache the return address register number (in whatever register numbering this UnwindPlan uses) for
131                 // quick reference during instruction parsing.
132                 uint32_t ra_reg_num = LLDB_INVALID_REGNUM;
133                 RegisterInfo ra_reg_info;
134                 if (m_inst_emulator_ap->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, ra_reg_info))
135                     ra_reg_num = ra_reg_info.kinds[unwind_plan.GetRegisterKind()];
136                 else
137                     ra_reg_num = LLDB_INVALID_REGNUM;
138 
139                 for (size_t idx=0; idx<num_instructions; ++idx)
140                 {
141                     m_curr_row_modified = false;
142                     m_curr_insn_restored_a_register = false;
143                     inst = inst_list.GetInstructionAtIndex (idx).get();
144                     if (inst)
145                     {
146                         if (log && log->GetVerbose ())
147                         {
148                             StreamString strm;
149                             inst->Dump(&strm, inst_list.GetMaxOpcocdeByteSize (), show_address, show_bytes, NULL);
150                             log->PutCString (strm.GetData());
151                         }
152 
153                         m_inst_emulator_ap->SetInstruction (inst->GetOpcode(),
154                                                             inst->GetAddress(),
155                                                             exe_ctx.GetTargetPtr());
156 
157                         m_inst_emulator_ap->EvaluateInstruction (eEmulateInstructionOptionIgnoreConditions);
158 
159                         // Were there any changes to the CFI while evaluating this instruction?
160                         if (m_curr_row_modified)
161                         {
162                             reinstate_prologue_next_instruction = false;
163                             m_curr_row->SetOffset (inst->GetAddress().GetFileAddress() + inst->GetOpcode().GetByteSize() - base_addr);
164                             // Append the new row
165                             unwind_plan.AppendRow (m_curr_row);
166 
167                             // Allocate a new Row for m_curr_row, copy the current state into it
168                             UnwindPlan::Row *newrow = new UnwindPlan::Row;
169                             *newrow = *m_curr_row.get();
170                             m_curr_row.reset(newrow);
171 
172                             // If m_curr_insn_restored_a_register == true, we're looking at an epilogue instruction.
173                             // Set instructions_since_last_prologue_insn to a very high number so we don't append
174                             // any of these epilogue instructions to our prologue_complete row.
175                             if (m_curr_insn_restored_a_register == false && instructions_since_last_prologue_insn < 8)
176                               instructions_since_last_prologue_insn = 0;
177                             else
178                               instructions_since_last_prologue_insn = 99;
179 
180                             UnwindPlan::Row::RegisterLocation pc_regloc;
181                             UnwindPlan::Row::RegisterLocation ra_regloc;
182 
183                             // While parsing the instructions of this function, if we've ever
184                             // seen the return address register (aka lr on arm) in a non-IsSame() state,
185                             // it has been saved on the stack.  If it's ever back to IsSame(), we've
186                             // executed an epilogue.
187                             if (ra_reg_num != LLDB_INVALID_REGNUM
188                                 && m_curr_row->GetRegisterInfo (ra_reg_num, ra_regloc)
189                                 && !ra_regloc.IsSame())
190                             {
191                                 return_address_register_has_been_saved = true;
192                             }
193 
194                             // If the caller's pc is "same", we've just executed an epilogue and we return to the caller
195                             // after this instruction completes executing.
196                             // If there are any instructions past this, there must have been flow control over this
197                             // epilogue so we'll reinstate the original prologue setup instructions.
198                             if (prologue_completed_row.get()
199                                 && pc_reg_num != LLDB_INVALID_REGNUM
200                                 && m_curr_row->GetRegisterInfo (pc_reg_num, pc_regloc)
201                                 && pc_regloc.IsSame())
202                             {
203                                 if (log && log->GetVerbose())
204                                     log->Printf("UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly -- pc is <same>, restore prologue instructions.");
205                                 reinstate_prologue_next_instruction = true;
206                             }
207                             else if (prologue_completed_row.get()
208                                      && return_address_register_has_been_saved
209                                      && ra_reg_num != LLDB_INVALID_REGNUM
210                                      && m_curr_row->GetRegisterInfo (ra_reg_num, ra_regloc)
211                                      && ra_regloc.IsSame())
212                             {
213                                 if (log && log->GetVerbose())
214                                     log->Printf("UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly -- lr is <same>, restore prologue instruction if the next instruction is a branch immediate.");
215                                 last_instruction_restored_return_addr_reg = true;
216                             }
217                         }
218                         else
219                         {
220                             // If the previous instruction was a return-to-caller (epilogue), and we're still executing
221                             // instructions in this function, there must be a code path that jumps over that epilogue.
222                             // Also detect the case where we epilogue & branch imm to another function (tail-call opt)
223                             // instead of a normal pop lr-into-pc exit.
224                             // Reinstate the frame setup from the prologue.
225                             if (reinstate_prologue_next_instruction
226                                 || (m_curr_insn_is_branch_immediate && last_instruction_restored_return_addr_reg))
227                             {
228                                 if (log && log->GetVerbose())
229                                     log->Printf("UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly -- Reinstating prologue instruction set");
230                                 UnwindPlan::Row *newrow = new UnwindPlan::Row;
231                                 *newrow = *prologue_completed_row.get();
232                                 m_curr_row.reset(newrow);
233                                 m_curr_row->SetOffset (inst->GetAddress().GetFileAddress() + inst->GetOpcode().GetByteSize() - base_addr);
234                                 unwind_plan.AppendRow(m_curr_row);
235 
236                                 newrow = new UnwindPlan::Row;
237                                 *newrow = *m_curr_row.get();
238                                 m_curr_row.reset(newrow);
239 
240                                 reinstate_prologue_next_instruction = false;
241                                 last_instruction_restored_return_addr_reg = false;
242                                 m_curr_insn_is_branch_immediate = false;
243                             }
244 
245                             // clear both of these if either one wasn't set
246                             if (last_instruction_restored_return_addr_reg)
247                             {
248                                 last_instruction_restored_return_addr_reg = false;
249                             }
250                             if (m_curr_insn_is_branch_immediate)
251                             {
252                                 m_curr_insn_is_branch_immediate = false;
253                             }
254 
255                             // Stop updating the prologue instructions if we've seen 8 non-prologue instructions
256                             // in a row.
257                             if (instructions_since_last_prologue_insn++ < 8)
258                             {
259                                 UnwindPlan::Row *newrow = new UnwindPlan::Row;
260                                 *newrow = *m_curr_row.get();
261                                 prologue_completed_row.reset(newrow);
262                                 if (log && log->GetVerbose())
263                                     log->Printf("UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly -- saving a copy of the current row as the prologue row.");
264                             }
265                         }
266                     }
267                 }
268             }
269             // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
270             // I'll fix that but for now, just clear the list and it will go away nicely.
271             disasm_sp->GetInstructionList().Clear();
272         }
273 
274         if (log && log->GetVerbose ())
275         {
276             StreamString strm;
277             lldb::addr_t base_addr = range.GetBaseAddress().GetLoadAddress(thread.CalculateTarget().get());
278             strm.Printf ("Resulting unwind rows for [0x%" PRIx64 " - 0x%" PRIx64 "):", base_addr, base_addr + range.GetByteSize());
279             unwind_plan.Dump(strm, &thread, base_addr);
280             log->PutCString (strm.GetData());
281         }
282         return unwind_plan.GetRowCount() > 0;
283     }
284     return false;
285 }
286 
287 bool
288 UnwindAssemblyInstEmulation::AugmentUnwindPlanFromCallSite (AddressRange& func,
289                                                             Thread& thread,
290                                                             UnwindPlan& unwind_plan)
291 {
292     return false;
293 }
294 
295 bool
296 UnwindAssemblyInstEmulation::GetFastUnwindPlan (AddressRange& func,
297                                                 Thread& thread,
298                                                 UnwindPlan &unwind_plan)
299 {
300     return false;
301 }
302 
303 bool
304 UnwindAssemblyInstEmulation::FirstNonPrologueInsn (AddressRange& func,
305                                                    const ExecutionContext &exe_ctx,
306                                                    Address& first_non_prologue_insn)
307 {
308     return false;
309 }
310 
311 UnwindAssembly *
312 UnwindAssemblyInstEmulation::CreateInstance (const ArchSpec &arch)
313 {
314     std::unique_ptr<EmulateInstruction> inst_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypePrologueEpilogue, NULL));
315     // Make sure that all prologue instructions are handled
316     if (inst_emulator_ap.get())
317         return new UnwindAssemblyInstEmulation (arch, inst_emulator_ap.release());
318     return NULL;
319 }
320 
321 
322 //------------------------------------------------------------------
323 // PluginInterface protocol in UnwindAssemblyParser_x86
324 //------------------------------------------------------------------
325 ConstString
326 UnwindAssemblyInstEmulation::GetPluginName()
327 {
328     return GetPluginNameStatic();
329 }
330 
331 uint32_t
332 UnwindAssemblyInstEmulation::GetPluginVersion()
333 {
334     return 1;
335 }
336 
337 void
338 UnwindAssemblyInstEmulation::Initialize()
339 {
340     PluginManager::RegisterPlugin (GetPluginNameStatic(),
341                                    GetPluginDescriptionStatic(),
342                                    CreateInstance);
343 }
344 
345 void
346 UnwindAssemblyInstEmulation::Terminate()
347 {
348     PluginManager::UnregisterPlugin (CreateInstance);
349 }
350 
351 
352 ConstString
353 UnwindAssemblyInstEmulation::GetPluginNameStatic()
354 {
355     static ConstString g_name("inst-emulation");
356     return g_name;
357 }
358 
359 const char *
360 UnwindAssemblyInstEmulation::GetPluginDescriptionStatic()
361 {
362     return "Instruction emulation based unwind information.";
363 }
364 
365 
366 uint64_t
367 UnwindAssemblyInstEmulation::MakeRegisterKindValuePair (const RegisterInfo &reg_info)
368 {
369     lldb::RegisterKind reg_kind;
370     uint32_t reg_num;
371     if (EmulateInstruction::GetBestRegisterKindAndNumber (&reg_info, reg_kind, reg_num))
372         return (uint64_t)reg_kind << 24 | reg_num;
373     return 0ull;
374 }
375 
376 void
377 UnwindAssemblyInstEmulation::SetRegisterValue (const RegisterInfo &reg_info, const RegisterValue &reg_value)
378 {
379     m_register_values[MakeRegisterKindValuePair (reg_info)] = reg_value;
380 }
381 
382 bool
383 UnwindAssemblyInstEmulation::GetRegisterValue (const RegisterInfo &reg_info, RegisterValue &reg_value)
384 {
385     const uint64_t reg_id = MakeRegisterKindValuePair (reg_info);
386     RegisterValueMap::const_iterator pos = m_register_values.find(reg_id);
387     if (pos != m_register_values.end())
388     {
389         reg_value = pos->second;
390         return true; // We had a real value that comes from an opcode that wrote
391                      // to it...
392     }
393     // We are making up a value that is recognizable...
394     reg_value.SetUInt(reg_id, reg_info.byte_size);
395     return false;
396 }
397 
398 
399 size_t
400 UnwindAssemblyInstEmulation::ReadMemory (EmulateInstruction *instruction,
401                                          void *baton,
402                                          const EmulateInstruction::Context &context,
403                                          lldb::addr_t addr,
404                                          void *dst,
405                                          size_t dst_len)
406 {
407     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
408 
409     if (log && log->GetVerbose ())
410     {
411         StreamString strm;
412         strm.Printf ("UnwindAssemblyInstEmulation::ReadMemory    (addr = 0x%16.16" PRIx64 ", dst = %p, dst_len = %" PRIu64 ", context = ",
413                      addr,
414                      dst,
415                      (uint64_t)dst_len);
416         context.Dump(strm, instruction);
417         log->PutCString (strm.GetData ());
418     }
419     memset (dst, 0, dst_len);
420     return dst_len;
421 }
422 
423 size_t
424 UnwindAssemblyInstEmulation::WriteMemory (EmulateInstruction *instruction,
425                                           void *baton,
426                                           const EmulateInstruction::Context &context,
427                                           lldb::addr_t addr,
428                                           const void *dst,
429                                           size_t dst_len)
430 {
431     if (baton && dst && dst_len)
432         return ((UnwindAssemblyInstEmulation *)baton)->WriteMemory (instruction, context, addr, dst, dst_len);
433     return 0;
434 }
435 
436 size_t
437 UnwindAssemblyInstEmulation::WriteMemory (EmulateInstruction *instruction,
438                                           const EmulateInstruction::Context &context,
439                                           lldb::addr_t addr,
440                                           const void *dst,
441                                           size_t dst_len)
442 {
443     DataExtractor data (dst,
444                         dst_len,
445                         instruction->GetArchitecture ().GetByteOrder(),
446                         instruction->GetArchitecture ().GetAddressByteSize());
447 
448     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
449 
450     if (log && log->GetVerbose ())
451     {
452         StreamString strm;
453 
454         strm.PutCString ("UnwindAssemblyInstEmulation::WriteMemory   (");
455         data.Dump(&strm, 0, eFormatBytes, 1, dst_len, UINT32_MAX, addr, 0, 0);
456         strm.PutCString (", context = ");
457         context.Dump(strm, instruction);
458         log->PutCString (strm.GetData());
459     }
460 
461     const bool can_replace = true;
462     const bool cant_replace = false;
463 
464     switch (context.type)
465     {
466         default:
467         case EmulateInstruction::eContextInvalid:
468         case EmulateInstruction::eContextReadOpcode:
469         case EmulateInstruction::eContextImmediate:
470         case EmulateInstruction::eContextAdjustBaseRegister:
471         case EmulateInstruction::eContextRegisterPlusOffset:
472         case EmulateInstruction::eContextAdjustPC:
473         case EmulateInstruction::eContextRegisterStore:
474         case EmulateInstruction::eContextRegisterLoad:
475         case EmulateInstruction::eContextRelativeBranchImmediate:
476         case EmulateInstruction::eContextAbsoluteBranchRegister:
477         case EmulateInstruction::eContextSupervisorCall:
478         case EmulateInstruction::eContextTableBranchReadMemory:
479         case EmulateInstruction::eContextWriteRegisterRandomBits:
480         case EmulateInstruction::eContextWriteMemoryRandomBits:
481         case EmulateInstruction::eContextArithmetic:
482         case EmulateInstruction::eContextAdvancePC:
483         case EmulateInstruction::eContextReturnFromException:
484         case EmulateInstruction::eContextPopRegisterOffStack:
485         case EmulateInstruction::eContextAdjustStackPointer:
486             break;
487 
488         case EmulateInstruction::eContextPushRegisterOnStack:
489             {
490                 uint32_t reg_num = LLDB_INVALID_REGNUM;
491                 bool is_return_address_reg = false;
492                 const uint32_t unwind_reg_kind = m_unwind_plan_ptr->GetRegisterKind();
493                 if (context.info_type == EmulateInstruction::eInfoTypeRegisterToRegisterPlusOffset)
494                 {
495                     reg_num = context.info.RegisterToRegisterPlusOffset.data_reg.kinds[unwind_reg_kind];
496                     if (context.info.RegisterToRegisterPlusOffset.data_reg.kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA)
497                         is_return_address_reg = true;
498                 }
499                 else
500                 {
501                     assert (!"unhandled case, add code to handle this!");
502                 }
503 
504                 if (reg_num != LLDB_INVALID_REGNUM)
505                 {
506                     if (m_pushed_regs.find (reg_num) == m_pushed_regs.end())
507                     {
508                         m_pushed_regs[reg_num] = addr;
509                         const int32_t offset = addr - m_initial_sp;
510                         m_curr_row->SetRegisterLocationToAtCFAPlusOffset (reg_num, offset, cant_replace);
511                         m_curr_row_modified = true;
512                         if (is_return_address_reg)
513                         {
514                             // This push was pushing the return address register,
515                             // so this is also how we will unwind the PC...
516                             RegisterInfo pc_reg_info;
517                             if (instruction->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc_reg_info))
518                             {
519                                 uint32_t pc_reg_num = pc_reg_info.kinds[unwind_reg_kind];
520                                 if (pc_reg_num != LLDB_INVALID_REGNUM)
521                                 {
522                                     m_curr_row->SetRegisterLocationToAtCFAPlusOffset (pc_reg_num, offset, can_replace);
523                                     m_curr_row_modified = true;
524                                 }
525                             }
526                         }
527                     }
528                 }
529             }
530             break;
531 
532     }
533 
534     return dst_len;
535 }
536 
537 bool
538 UnwindAssemblyInstEmulation::ReadRegister (EmulateInstruction *instruction,
539                                            void *baton,
540                                            const RegisterInfo *reg_info,
541                                            RegisterValue &reg_value)
542 {
543 
544     if (baton && reg_info)
545         return ((UnwindAssemblyInstEmulation *)baton)->ReadRegister (instruction, reg_info, reg_value);
546     return false;
547 }
548 bool
549 UnwindAssemblyInstEmulation::ReadRegister (EmulateInstruction *instruction,
550                                            const RegisterInfo *reg_info,
551                                            RegisterValue &reg_value)
552 {
553     bool synthetic = GetRegisterValue (*reg_info, reg_value);
554 
555     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
556 
557     if (log && log->GetVerbose ())
558     {
559 
560         StreamString strm;
561         strm.Printf ("UnwindAssemblyInstEmulation::ReadRegister  (name = \"%s\") => synthetic_value = %i, value = ", reg_info->name, synthetic);
562         reg_value.Dump(&strm, reg_info, false, false, eFormatDefault);
563         log->PutCString(strm.GetData());
564     }
565     return true;
566 }
567 
568 bool
569 UnwindAssemblyInstEmulation::WriteRegister (EmulateInstruction *instruction,
570                                             void *baton,
571                                             const EmulateInstruction::Context &context,
572                                             const RegisterInfo *reg_info,
573                                             const RegisterValue &reg_value)
574 {
575     if (baton && reg_info)
576         return ((UnwindAssemblyInstEmulation *)baton)->WriteRegister (instruction, context, reg_info, reg_value);
577     return false;
578 }
579 bool
580 UnwindAssemblyInstEmulation::WriteRegister (EmulateInstruction *instruction,
581                                             const EmulateInstruction::Context &context,
582                                             const RegisterInfo *reg_info,
583                                             const RegisterValue &reg_value)
584 {
585     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
586 
587     if (log && log->GetVerbose ())
588     {
589 
590         StreamString strm;
591         strm.Printf ("UnwindAssemblyInstEmulation::WriteRegister (name = \"%s\", value = ", reg_info->name);
592         reg_value.Dump(&strm, reg_info, false, false, eFormatDefault);
593         strm.PutCString (", context = ");
594         context.Dump(strm, instruction);
595         log->PutCString(strm.GetData());
596     }
597 
598     const bool must_replace = true;
599     SetRegisterValue (*reg_info, reg_value);
600 
601     switch (context.type)
602     {
603         case EmulateInstruction::eContextInvalid:
604         case EmulateInstruction::eContextReadOpcode:
605         case EmulateInstruction::eContextImmediate:
606         case EmulateInstruction::eContextAdjustBaseRegister:
607         case EmulateInstruction::eContextRegisterPlusOffset:
608         case EmulateInstruction::eContextAdjustPC:
609         case EmulateInstruction::eContextRegisterStore:
610         case EmulateInstruction::eContextRegisterLoad:
611         case EmulateInstruction::eContextAbsoluteBranchRegister:
612         case EmulateInstruction::eContextSupervisorCall:
613         case EmulateInstruction::eContextTableBranchReadMemory:
614         case EmulateInstruction::eContextWriteRegisterRandomBits:
615         case EmulateInstruction::eContextWriteMemoryRandomBits:
616         case EmulateInstruction::eContextArithmetic:
617         case EmulateInstruction::eContextAdvancePC:
618         case EmulateInstruction::eContextReturnFromException:
619         case EmulateInstruction::eContextPushRegisterOnStack:
620 //            {
621 //                const uint32_t reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
622 //                if (reg_num != LLDB_INVALID_REGNUM)
623 //                {
624 //                    const bool can_replace_only_if_unspecified = true;
625 //
626 //                    m_curr_row.SetRegisterLocationToUndefined (reg_num,
627 //                                                               can_replace_only_if_unspecified,
628 //                                                               can_replace_only_if_unspecified);
629 //                    m_curr_row_modified = true;
630 //                }
631 //            }
632             break;
633 
634         case EmulateInstruction::eContextRelativeBranchImmediate:
635             {
636 
637                 {
638                     m_curr_insn_is_branch_immediate = true;
639                 }
640             }
641             break;
642 
643         case EmulateInstruction::eContextPopRegisterOffStack:
644             {
645                 const uint32_t reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
646                 if (reg_num != LLDB_INVALID_REGNUM)
647                 {
648                     m_curr_row->SetRegisterLocationToSame (reg_num, must_replace);
649                     m_curr_row_modified = true;
650                     m_curr_insn_restored_a_register = true;
651                 }
652             }
653             break;
654 
655         case EmulateInstruction::eContextSetFramePointer:
656             if (!m_fp_is_cfa)
657             {
658                 m_fp_is_cfa = true;
659                 m_cfa_reg_info = *reg_info;
660                 const uint32_t cfa_reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()];
661                 assert (cfa_reg_num != LLDB_INVALID_REGNUM);
662                 m_curr_row->SetCFARegister(cfa_reg_num);
663                 m_curr_row->SetCFAOffset(m_initial_sp - reg_value.GetAsUInt64());
664                 m_curr_row_modified = true;
665             }
666             break;
667 
668         case EmulateInstruction::eContextAdjustStackPointer:
669             // If we have created a frame using the frame pointer, don't follow
670             // subsequent adjustments to the stack pointer.
671             if (!m_fp_is_cfa)
672             {
673                 m_curr_row->SetCFAOffset (m_initial_sp - reg_value.GetAsUInt64());
674                 m_curr_row_modified = true;
675             }
676             break;
677     }
678     return true;
679 }
680 
681 
682