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 "llvm-c/EnhancedDisassembly.h" 13 14 #include "lldb/Core/Address.h" 15 #include "lldb/Core/ArchSpec.h" 16 #include "lldb/Core/DataBufferHeap.h" 17 #include "lldb/Core/Disassembler.h" 18 #include "lldb/Core/Error.h" 19 #include "lldb/Core/Log.h" 20 #include "lldb/Core/PluginManager.h" 21 #include "lldb/Core/StreamString.h" 22 #include "lldb/Target/ExecutionContext.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/Thread.h" 25 #include "lldb/Target/Target.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 31 32 //----------------------------------------------------------------------------------------------- 33 // UnwindAssemblyInstEmulation method definitions 34 //----------------------------------------------------------------------------------------------- 35 36 bool 37 UnwindAssemblyInstEmulation::GetNonCallSiteUnwindPlanFromAssembly (AddressRange& range, 38 Thread& thread, 39 UnwindPlan& unwind_plan) 40 { 41 if (range.GetByteSize() > 0 && 42 range.GetBaseAddress().IsValid() && 43 m_inst_emulator_ap.get()) 44 { 45 46 // The the instruction emulation subclass setup the unwind plan for the 47 // first instruction. 48 m_inst_emulator_ap->CreateFunctionEntryUnwind (unwind_plan); 49 50 // CreateFunctionEntryUnwind should have created the first row. If it 51 // doesn't, then we are done. 52 if (unwind_plan.GetRowCount() == 0) 53 return false; 54 55 ExecutionContext exe_ctx; 56 thread.CalculateExecutionContext(exe_ctx); 57 DisassemblerSP disasm_sp (Disassembler::DisassembleRange (m_arch, 58 NULL, 59 exe_ctx, 60 range)); 61 62 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 63 64 if (disasm_sp) 65 { 66 67 m_range_ptr = ⦥ 68 m_thread_ptr = &thread; 69 m_unwind_plan_ptr = &unwind_plan; 70 71 const uint32_t addr_byte_size = m_arch.GetAddressByteSize(); 72 const bool show_address = true; 73 const bool show_bytes = true; 74 m_inst_emulator_ap->GetRegisterInfo (unwind_plan.GetRegisterKind(), 75 unwind_plan.GetInitialCFARegister(), 76 m_cfa_reg_info); 77 78 m_fp_is_cfa = false; 79 m_register_values.clear(); 80 m_pushed_regs.clear(); 81 82 // Initialize the CFA with a known value. In the 32 bit case 83 // it will be 0x80000000, and in the 64 bit case 0x8000000000000000. 84 // We use the address byte size to be safe for any future addresss sizes 85 m_initial_sp = (1ull << ((addr_byte_size * 8) - 1)); 86 RegisterValue cfa_reg_value; 87 cfa_reg_value.SetUInt (m_initial_sp, m_cfa_reg_info.byte_size); 88 SetRegisterValue (m_cfa_reg_info, cfa_reg_value); 89 90 const InstructionList &inst_list = disasm_sp->GetInstructionList (); 91 const size_t num_instructions = inst_list.GetSize(); 92 if (num_instructions > 0) 93 { 94 Instruction *inst = inst_list.GetInstructionAtIndex (0).get(); 95 const addr_t base_addr = inst->GetAddress().GetFileAddress(); 96 // Initialize the current row with the one row that was created 97 // from the CreateFunctionEntryUnwind call above... 98 m_curr_row = unwind_plan.GetLastRow(); 99 100 for (size_t idx=0; idx<num_instructions; ++idx) 101 { 102 inst = inst_list.GetInstructionAtIndex (idx).get(); 103 if (inst) 104 { 105 106 if (log && log->GetVerbose ()) 107 { 108 StreamString strm; 109 inst->Dump(&strm, inst_list.GetMaxOpcocdeByteSize (), show_address, show_bytes, NULL); 110 log->PutCString (strm.GetData()); 111 } 112 113 m_inst_emulator_ap->SetInstruction (inst->GetOpcode(), 114 inst->GetAddress(), 115 exe_ctx.GetTargetPtr()); 116 117 m_inst_emulator_ap->EvaluateInstruction (eEmulateInstructionOptionIgnoreConditions); 118 119 if (unwind_plan.GetLastRow() != m_curr_row) 120 { 121 // Be sure to not edit the offset unless our row has changed 122 // so that the "!=" call above doesn't trigger every time 123 m_curr_row.SetOffset (inst->GetAddress().GetFileAddress() + inst->GetOpcode().GetByteSize() - base_addr); 124 // Append the new row 125 unwind_plan.AppendRow (m_curr_row); 126 } 127 } 128 } 129 } 130 } 131 132 if (log && log->GetVerbose ()) 133 { 134 StreamString strm; 135 lldb::addr_t base_addr = range.GetBaseAddress().GetLoadAddress(thread.CalculateTarget().get()); 136 strm.Printf ("Resulting unwind rows for [0x%llx - 0x%llx):", base_addr, base_addr + range.GetByteSize()); 137 unwind_plan.Dump(strm, &thread, base_addr); 138 log->PutCString (strm.GetData()); 139 } 140 return unwind_plan.GetRowCount() > 0; 141 } 142 return false; 143 } 144 145 bool 146 UnwindAssemblyInstEmulation::GetFastUnwindPlan (AddressRange& func, 147 Thread& thread, 148 UnwindPlan &unwind_plan) 149 { 150 return false; 151 } 152 153 bool 154 UnwindAssemblyInstEmulation::FirstNonPrologueInsn (AddressRange& func, 155 const ExecutionContext &exe_ctx, 156 Address& first_non_prologue_insn) 157 { 158 return false; 159 } 160 161 UnwindAssembly * 162 UnwindAssemblyInstEmulation::CreateInstance (const ArchSpec &arch) 163 { 164 std::auto_ptr<EmulateInstruction> inst_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypePrologueEpilogue, NULL)); 165 // Make sure that all prologue instructions are handled 166 if (inst_emulator_ap.get()) 167 return new UnwindAssemblyInstEmulation (arch, inst_emulator_ap.release()); 168 return NULL; 169 } 170 171 172 //------------------------------------------------------------------ 173 // PluginInterface protocol in UnwindAssemblyParser_x86 174 //------------------------------------------------------------------ 175 176 const char * 177 UnwindAssemblyInstEmulation::GetPluginName() 178 { 179 return "UnwindAssemblyInstEmulation"; 180 } 181 182 const char * 183 UnwindAssemblyInstEmulation::GetShortPluginName() 184 { 185 return "unwindassembly.inst-emulation"; 186 } 187 188 189 uint32_t 190 UnwindAssemblyInstEmulation::GetPluginVersion() 191 { 192 return 1; 193 } 194 195 void 196 UnwindAssemblyInstEmulation::Initialize() 197 { 198 PluginManager::RegisterPlugin (GetPluginNameStatic(), 199 GetPluginDescriptionStatic(), 200 CreateInstance); 201 } 202 203 void 204 UnwindAssemblyInstEmulation::Terminate() 205 { 206 PluginManager::UnregisterPlugin (CreateInstance); 207 } 208 209 210 const char * 211 UnwindAssemblyInstEmulation::GetPluginNameStatic() 212 { 213 return "UnwindAssemblyInstEmulation"; 214 } 215 216 const char * 217 UnwindAssemblyInstEmulation::GetPluginDescriptionStatic() 218 { 219 return "Instruction emulation based unwind information."; 220 } 221 222 223 uint64_t 224 UnwindAssemblyInstEmulation::MakeRegisterKindValuePair (const RegisterInfo ®_info) 225 { 226 uint32_t reg_kind, reg_num; 227 if (EmulateInstruction::GetBestRegisterKindAndNumber (®_info, reg_kind, reg_num)) 228 return (uint64_t)reg_kind << 24 | reg_num; 229 return 0ull; 230 } 231 232 void 233 UnwindAssemblyInstEmulation::SetRegisterValue (const RegisterInfo ®_info, const RegisterValue ®_value) 234 { 235 m_register_values[MakeRegisterKindValuePair (reg_info)] = reg_value; 236 } 237 238 bool 239 UnwindAssemblyInstEmulation::GetRegisterValue (const RegisterInfo ®_info, RegisterValue ®_value) 240 { 241 const uint64_t reg_id = MakeRegisterKindValuePair (reg_info); 242 RegisterValueMap::const_iterator pos = m_register_values.find(reg_id); 243 if (pos != m_register_values.end()) 244 { 245 reg_value = pos->second; 246 return true; // We had a real value that comes from an opcode that wrote 247 // to it... 248 } 249 // We are making up a value that is recognizable... 250 reg_value.SetUInt(reg_id, reg_info.byte_size); 251 return false; 252 } 253 254 255 size_t 256 UnwindAssemblyInstEmulation::ReadMemory (EmulateInstruction *instruction, 257 void *baton, 258 const EmulateInstruction::Context &context, 259 lldb::addr_t addr, 260 void *dst, 261 size_t dst_len) 262 { 263 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 264 265 if (log && log->GetVerbose ()) 266 { 267 StreamString strm; 268 strm.Printf ("UnwindAssemblyInstEmulation::ReadMemory (addr = 0x%16.16llx, dst = %p, dst_len = %zu, context = ", 269 addr, 270 dst, 271 dst_len); 272 context.Dump(strm, instruction); 273 log->PutCString (strm.GetData ()); 274 } 275 return dst_len; 276 } 277 278 size_t 279 UnwindAssemblyInstEmulation::WriteMemory (EmulateInstruction *instruction, 280 void *baton, 281 const EmulateInstruction::Context &context, 282 lldb::addr_t addr, 283 const void *dst, 284 size_t dst_len) 285 { 286 if (baton && dst && dst_len) 287 return ((UnwindAssemblyInstEmulation *)baton)->WriteMemory (instruction, context, addr, dst, dst_len); 288 return 0; 289 } 290 291 size_t 292 UnwindAssemblyInstEmulation::WriteMemory (EmulateInstruction *instruction, 293 const EmulateInstruction::Context &context, 294 lldb::addr_t addr, 295 const void *dst, 296 size_t dst_len) 297 { 298 DataExtractor data (dst, 299 dst_len, 300 instruction->GetArchitecture ().GetByteOrder(), 301 instruction->GetArchitecture ().GetAddressByteSize()); 302 303 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 304 305 if (log && log->GetVerbose ()) 306 { 307 StreamString strm; 308 309 strm.PutCString ("UnwindAssemblyInstEmulation::WriteMemory ("); 310 data.Dump(&strm, 0, eFormatBytes, 1, dst_len, UINT32_MAX, addr, 0, 0); 311 strm.PutCString (", context = "); 312 context.Dump(strm, instruction); 313 log->PutCString (strm.GetData()); 314 } 315 316 const bool can_replace = true; 317 const bool cant_replace = false; 318 319 switch (context.type) 320 { 321 default: 322 case EmulateInstruction::eContextInvalid: 323 case EmulateInstruction::eContextReadOpcode: 324 case EmulateInstruction::eContextImmediate: 325 case EmulateInstruction::eContextAdjustBaseRegister: 326 case EmulateInstruction::eContextRegisterPlusOffset: 327 case EmulateInstruction::eContextAdjustPC: 328 case EmulateInstruction::eContextRegisterStore: 329 case EmulateInstruction::eContextRegisterLoad: 330 case EmulateInstruction::eContextRelativeBranchImmediate: 331 case EmulateInstruction::eContextAbsoluteBranchRegister: 332 case EmulateInstruction::eContextSupervisorCall: 333 case EmulateInstruction::eContextTableBranchReadMemory: 334 case EmulateInstruction::eContextWriteRegisterRandomBits: 335 case EmulateInstruction::eContextWriteMemoryRandomBits: 336 case EmulateInstruction::eContextArithmetic: 337 case EmulateInstruction::eContextAdvancePC: 338 case EmulateInstruction::eContextReturnFromException: 339 case EmulateInstruction::eContextPopRegisterOffStack: 340 case EmulateInstruction::eContextAdjustStackPointer: 341 break; 342 343 case EmulateInstruction::eContextPushRegisterOnStack: 344 { 345 uint32_t reg_num = LLDB_INVALID_REGNUM; 346 bool is_return_address_reg = false; 347 const uint32_t unwind_reg_kind = m_unwind_plan_ptr->GetRegisterKind(); 348 if (context.info_type == EmulateInstruction::eInfoTypeRegisterToRegisterPlusOffset) 349 { 350 reg_num = context.info.RegisterToRegisterPlusOffset.data_reg.kinds[unwind_reg_kind]; 351 if (context.info.RegisterToRegisterPlusOffset.data_reg.kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA) 352 is_return_address_reg = true; 353 } 354 else 355 { 356 assert (!"unhandled case, add code to handle this!"); 357 } 358 359 if (reg_num != LLDB_INVALID_REGNUM) 360 { 361 if (m_pushed_regs.find (reg_num) == m_pushed_regs.end()) 362 { 363 m_pushed_regs[reg_num] = addr; 364 const int32_t offset = addr - m_initial_sp; 365 m_curr_row.SetRegisterLocationToAtCFAPlusOffset (reg_num, offset, cant_replace); 366 if (is_return_address_reg) 367 { 368 // This push was pushing the return address register, 369 // so this is also how we will unwind the PC... 370 RegisterInfo pc_reg_info; 371 if (instruction->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc_reg_info)) 372 { 373 uint32_t pc_reg_num = pc_reg_info.kinds[unwind_reg_kind]; 374 if (pc_reg_num != LLDB_INVALID_REGNUM) 375 m_curr_row.SetRegisterLocationToAtCFAPlusOffset (pc_reg_num, offset, can_replace); 376 } 377 } 378 } 379 } 380 } 381 break; 382 383 } 384 385 return dst_len; 386 } 387 388 bool 389 UnwindAssemblyInstEmulation::ReadRegister (EmulateInstruction *instruction, 390 void *baton, 391 const RegisterInfo *reg_info, 392 RegisterValue ®_value) 393 { 394 395 if (baton && reg_info) 396 return ((UnwindAssemblyInstEmulation *)baton)->ReadRegister (instruction, reg_info, reg_value); 397 return false; 398 } 399 bool 400 UnwindAssemblyInstEmulation::ReadRegister (EmulateInstruction *instruction, 401 const RegisterInfo *reg_info, 402 RegisterValue ®_value) 403 { 404 bool synthetic = GetRegisterValue (*reg_info, reg_value); 405 406 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 407 408 if (log && log->GetVerbose ()) 409 { 410 411 StreamString strm; 412 strm.Printf ("UnwindAssemblyInstEmulation::ReadRegister (name = \"%s\") => synthetic_value = %i, value = ", reg_info->name, synthetic); 413 reg_value.Dump(&strm, reg_info, false, false, eFormatDefault); 414 log->PutCString(strm.GetData()); 415 } 416 return true; 417 } 418 419 bool 420 UnwindAssemblyInstEmulation::WriteRegister (EmulateInstruction *instruction, 421 void *baton, 422 const EmulateInstruction::Context &context, 423 const RegisterInfo *reg_info, 424 const RegisterValue ®_value) 425 { 426 if (baton && reg_info) 427 return ((UnwindAssemblyInstEmulation *)baton)->WriteRegister (instruction, context, reg_info, reg_value); 428 return false; 429 } 430 bool 431 UnwindAssemblyInstEmulation::WriteRegister (EmulateInstruction *instruction, 432 const EmulateInstruction::Context &context, 433 const RegisterInfo *reg_info, 434 const RegisterValue ®_value) 435 { 436 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 437 438 if (log && log->GetVerbose ()) 439 { 440 441 StreamString strm; 442 strm.Printf ("UnwindAssemblyInstEmulation::WriteRegister (name = \"%s\", value = ", reg_info->name); 443 reg_value.Dump(&strm, reg_info, false, false, eFormatDefault); 444 strm.PutCString (", context = "); 445 context.Dump(strm, instruction); 446 log->PutCString(strm.GetData()); 447 } 448 449 const bool must_replace = true; 450 SetRegisterValue (*reg_info, reg_value); 451 452 switch (context.type) 453 { 454 default: 455 case EmulateInstruction::eContextInvalid: 456 case EmulateInstruction::eContextReadOpcode: 457 case EmulateInstruction::eContextImmediate: 458 case EmulateInstruction::eContextAdjustBaseRegister: 459 case EmulateInstruction::eContextRegisterPlusOffset: 460 case EmulateInstruction::eContextAdjustPC: 461 case EmulateInstruction::eContextRegisterStore: 462 case EmulateInstruction::eContextRegisterLoad: 463 case EmulateInstruction::eContextRelativeBranchImmediate: 464 case EmulateInstruction::eContextAbsoluteBranchRegister: 465 case EmulateInstruction::eContextSupervisorCall: 466 case EmulateInstruction::eContextTableBranchReadMemory: 467 case EmulateInstruction::eContextWriteRegisterRandomBits: 468 case EmulateInstruction::eContextWriteMemoryRandomBits: 469 case EmulateInstruction::eContextArithmetic: 470 case EmulateInstruction::eContextAdvancePC: 471 case EmulateInstruction::eContextReturnFromException: 472 case EmulateInstruction::eContextPushRegisterOnStack: 473 // { 474 // const uint32_t reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()]; 475 // if (reg_num != LLDB_INVALID_REGNUM) 476 // { 477 // const bool can_replace_only_if_unspecified = true; 478 // 479 // m_curr_row.SetRegisterLocationToUndefined (reg_num, 480 // can_replace_only_if_unspecified, 481 // can_replace_only_if_unspecified); 482 // } 483 // } 484 break; 485 486 case EmulateInstruction::eContextPopRegisterOffStack: 487 { 488 const uint32_t reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()]; 489 if (reg_num != LLDB_INVALID_REGNUM) 490 { 491 m_curr_row.SetRegisterLocationToSame (reg_num, must_replace); 492 } 493 } 494 break; 495 496 case EmulateInstruction::eContextSetFramePointer: 497 if (!m_fp_is_cfa) 498 { 499 m_fp_is_cfa = true; 500 m_cfa_reg_info = *reg_info; 501 const uint32_t cfa_reg_num = reg_info->kinds[m_unwind_plan_ptr->GetRegisterKind()]; 502 assert (cfa_reg_num != LLDB_INVALID_REGNUM); 503 m_curr_row.SetCFARegister(cfa_reg_num); 504 m_curr_row.SetCFAOffset(m_initial_sp - reg_value.GetAsUInt64()); 505 } 506 break; 507 508 case EmulateInstruction::eContextAdjustStackPointer: 509 // If we have created a frame using the frame pointer, don't follow 510 // subsequent adjustments to the stack pointer. 511 if (!m_fp_is_cfa) 512 { 513 m_curr_row.SetCFAOffset (m_initial_sp - reg_value.GetAsUInt64()); 514 } 515 break; 516 } 517 return true; 518 } 519 520 521