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