1 //===-- RegisterContext.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/RegisterContext.h" 15 #include "lldb/Core/DataExtractor.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/RegisterValue.h" 18 #include "lldb/Core/Scalar.h" 19 #include "lldb/Core/Value.h" 20 #include "lldb/Expression/DWARFExpression.h" 21 #include "lldb/Host/Endian.h" 22 #include "lldb/Target/ExecutionContext.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/StackFrame.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Target/Thread.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 RegisterContext::RegisterContext(Thread &thread, uint32_t concrete_frame_idx) 32 : m_thread(thread), m_concrete_frame_idx(concrete_frame_idx), 33 m_stop_id(thread.GetProcess()->GetStopID()) {} 34 35 RegisterContext::~RegisterContext() = default; 36 37 void RegisterContext::InvalidateIfNeeded(bool force) { 38 ProcessSP process_sp(m_thread.GetProcess()); 39 bool invalidate = force; 40 uint32_t process_stop_id = UINT32_MAX; 41 42 if (process_sp) 43 process_stop_id = process_sp->GetStopID(); 44 else 45 invalidate = true; 46 47 if (!invalidate) 48 invalidate = process_stop_id != GetStopID(); 49 50 if (invalidate) { 51 InvalidateAllRegisters(); 52 SetStopID(process_stop_id); 53 } 54 } 55 56 const RegisterInfo *RegisterContext::GetRegisterInfoByName(const char *reg_name, 57 uint32_t start_idx) { 58 if (reg_name && reg_name[0]) { 59 const uint32_t num_registers = GetRegisterCount(); 60 for (uint32_t reg = start_idx; reg < num_registers; ++reg) { 61 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 62 63 if ((reg_info->name != nullptr && 64 ::strcasecmp(reg_info->name, reg_name) == 0) || 65 (reg_info->alt_name != nullptr && 66 ::strcasecmp(reg_info->alt_name, reg_name) == 0)) { 67 return reg_info; 68 } 69 } 70 } 71 return nullptr; 72 } 73 74 uint32_t 75 RegisterContext::UpdateDynamicRegisterSize(const lldb_private::ArchSpec &arch, 76 RegisterInfo *reg_info) { 77 ExecutionContext exe_ctx(CalculateThread()); 78 79 // In MIPS, the floating point registers size is depends on FR bit of SR 80 // register. 81 // if SR.FR == 1 then all floating point registers are 64 bits. 82 // else they are all 32 bits. 83 84 int expr_result; 85 uint32_t addr_size = arch.GetAddressByteSize(); 86 const uint8_t *dwarf_opcode_ptr = reg_info->dynamic_size_dwarf_expr_bytes; 87 const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len; 88 89 DataExtractor dwarf_data(dwarf_opcode_ptr, dwarf_opcode_len, 90 arch.GetByteOrder(), addr_size); 91 ModuleSP opcode_ctx; 92 DWARFExpression dwarf_expr(opcode_ctx, dwarf_data, nullptr, 0, 93 dwarf_opcode_len); 94 Value result; 95 Error error; 96 const lldb::offset_t offset = 0; 97 if (dwarf_expr.Evaluate(&exe_ctx, nullptr, nullptr, this, opcode_ctx, 98 dwarf_data, nullptr, offset, dwarf_opcode_len, 99 eRegisterKindDWARF, nullptr, nullptr, result, 100 &error)) { 101 expr_result = result.GetScalar().SInt(-1); 102 switch (expr_result) { 103 case 0: 104 return 4; 105 case 1: 106 return 8; 107 default: 108 return reg_info->byte_size; 109 } 110 } else { 111 printf("Error executing DwarfExpression::Evaluate %s\n", error.AsCString()); 112 return reg_info->byte_size; 113 } 114 } 115 116 const RegisterInfo *RegisterContext::GetRegisterInfo(lldb::RegisterKind kind, 117 uint32_t num) { 118 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num); 119 if (reg_num == LLDB_INVALID_REGNUM) 120 return nullptr; 121 return GetRegisterInfoAtIndex(reg_num); 122 } 123 124 const char *RegisterContext::GetRegisterName(uint32_t reg) { 125 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 126 if (reg_info) 127 return reg_info->name; 128 return nullptr; 129 } 130 131 uint64_t RegisterContext::GetPC(uint64_t fail_value) { 132 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 133 LLDB_REGNUM_GENERIC_PC); 134 uint64_t pc = ReadRegisterAsUnsigned(reg, fail_value); 135 136 if (pc != fail_value) { 137 TargetSP target_sp = m_thread.CalculateTarget(); 138 if (target_sp) { 139 Target *target = target_sp.get(); 140 if (target) 141 pc = target->GetOpcodeLoadAddress(pc, eAddressClassCode); 142 } 143 } 144 145 return pc; 146 } 147 148 bool RegisterContext::SetPC(uint64_t pc) { 149 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 150 LLDB_REGNUM_GENERIC_PC); 151 bool success = WriteRegisterFromUnsigned(reg, pc); 152 if (success) { 153 StackFrameSP frame_sp( 154 m_thread.GetFrameWithConcreteFrameIndex(m_concrete_frame_idx)); 155 if (frame_sp) 156 frame_sp->ChangePC(pc); 157 else 158 m_thread.ClearStackFrames(); 159 } 160 return success; 161 } 162 163 bool RegisterContext::SetPC(Address addr) { 164 TargetSP target_sp = m_thread.CalculateTarget(); 165 Target *target = target_sp.get(); 166 167 lldb::addr_t callAddr = addr.GetCallableLoadAddress(target); 168 if (callAddr == LLDB_INVALID_ADDRESS) 169 return false; 170 171 return SetPC(callAddr); 172 } 173 174 uint64_t RegisterContext::GetSP(uint64_t fail_value) { 175 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 176 LLDB_REGNUM_GENERIC_SP); 177 return ReadRegisterAsUnsigned(reg, fail_value); 178 } 179 180 bool RegisterContext::SetSP(uint64_t sp) { 181 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 182 LLDB_REGNUM_GENERIC_SP); 183 return WriteRegisterFromUnsigned(reg, sp); 184 } 185 186 uint64_t RegisterContext::GetFP(uint64_t fail_value) { 187 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 188 LLDB_REGNUM_GENERIC_FP); 189 return ReadRegisterAsUnsigned(reg, fail_value); 190 } 191 192 bool RegisterContext::SetFP(uint64_t fp) { 193 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 194 LLDB_REGNUM_GENERIC_FP); 195 return WriteRegisterFromUnsigned(reg, fp); 196 } 197 198 uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value) { 199 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 200 LLDB_REGNUM_GENERIC_RA); 201 return ReadRegisterAsUnsigned(reg, fail_value); 202 } 203 204 uint64_t RegisterContext::GetFlags(uint64_t fail_value) { 205 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 206 LLDB_REGNUM_GENERIC_FLAGS); 207 return ReadRegisterAsUnsigned(reg, fail_value); 208 } 209 210 uint64_t RegisterContext::ReadRegisterAsUnsigned(uint32_t reg, 211 uint64_t fail_value) { 212 if (reg != LLDB_INVALID_REGNUM) 213 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value); 214 return fail_value; 215 } 216 217 uint64_t RegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info, 218 uint64_t fail_value) { 219 if (reg_info) { 220 RegisterValue value; 221 if (ReadRegister(reg_info, value)) 222 return value.GetAsUInt64(); 223 } 224 return fail_value; 225 } 226 227 bool RegisterContext::WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval) { 228 if (reg == LLDB_INVALID_REGNUM) 229 return false; 230 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval); 231 } 232 233 bool RegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info, 234 uint64_t uval) { 235 if (reg_info) { 236 RegisterValue value; 237 if (value.SetUInt(uval, reg_info->byte_size)) 238 return WriteRegister(reg_info, value); 239 } 240 return false; 241 } 242 243 bool RegisterContext::CopyFromRegisterContext(lldb::RegisterContextSP context) { 244 uint32_t num_register_sets = context->GetRegisterSetCount(); 245 // We don't know that two threads have the same register context, so require 246 // the threads to be the same. 247 if (context->GetThreadID() != GetThreadID()) 248 return false; 249 250 if (num_register_sets != GetRegisterSetCount()) 251 return false; 252 253 RegisterContextSP frame_zero_context = m_thread.GetRegisterContext(); 254 255 for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx) { 256 const RegisterSet *const reg_set = GetRegisterSet(set_idx); 257 258 const uint32_t num_registers = reg_set->num_registers; 259 for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx) { 260 const uint32_t reg = reg_set->registers[reg_idx]; 261 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 262 if (!reg_info || reg_info->value_regs) 263 continue; 264 RegisterValue reg_value; 265 266 // If we can reconstruct the register from the frame we are copying from, 267 // then do so, otherwise 268 // use the value from frame 0. 269 if (context->ReadRegister(reg_info, reg_value)) { 270 WriteRegister(reg_info, reg_value); 271 } else if (frame_zero_context->ReadRegister(reg_info, reg_value)) { 272 WriteRegister(reg_info, reg_value); 273 } 274 } 275 } 276 return true; 277 } 278 279 lldb::tid_t RegisterContext::GetThreadID() const { return m_thread.GetID(); } 280 281 uint32_t RegisterContext::NumSupportedHardwareBreakpoints() { return 0; } 282 283 uint32_t RegisterContext::SetHardwareBreakpoint(lldb::addr_t addr, 284 size_t size) { 285 return LLDB_INVALID_INDEX32; 286 } 287 288 bool RegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) { return false; } 289 290 uint32_t RegisterContext::NumSupportedHardwareWatchpoints() { return 0; } 291 292 uint32_t RegisterContext::SetHardwareWatchpoint(lldb::addr_t addr, size_t size, 293 bool read, bool write) { 294 return LLDB_INVALID_INDEX32; 295 } 296 297 bool RegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) { 298 return false; 299 } 300 301 bool RegisterContext::HardwareSingleStep(bool enable) { return false; } 302 303 Error RegisterContext::ReadRegisterValueFromMemory(const RegisterInfo *reg_info, 304 lldb::addr_t src_addr, 305 uint32_t src_len, 306 RegisterValue ®_value) { 307 Error error; 308 if (reg_info == nullptr) { 309 error.SetErrorString("invalid register info argument."); 310 return error; 311 } 312 313 // Moving from addr into a register 314 // 315 // Case 1: src_len == dst_len 316 // 317 // |AABBCCDD| Address contents 318 // |AABBCCDD| Register contents 319 // 320 // Case 2: src_len > dst_len 321 // 322 // Error! (The register should always be big enough to hold the data) 323 // 324 // Case 3: src_len < dst_len 325 // 326 // |AABB| Address contents 327 // |AABB0000| Register contents [on little-endian hardware] 328 // |0000AABB| Register contents [on big-endian hardware] 329 if (src_len > RegisterValue::kMaxRegisterByteSize) { 330 error.SetErrorString("register too small to receive memory data"); 331 return error; 332 } 333 334 const uint32_t dst_len = reg_info->byte_size; 335 336 if (src_len > dst_len) { 337 error.SetErrorStringWithFormat( 338 "%u bytes is too big to store in register %s (%u bytes)", src_len, 339 reg_info->name, dst_len); 340 return error; 341 } 342 343 ProcessSP process_sp(m_thread.GetProcess()); 344 if (process_sp) { 345 uint8_t src[RegisterValue::kMaxRegisterByteSize]; 346 347 // Read the memory 348 const uint32_t bytes_read = 349 process_sp->ReadMemory(src_addr, src, src_len, error); 350 351 // Make sure the memory read succeeded... 352 if (bytes_read != src_len) { 353 if (error.Success()) { 354 // This might happen if we read _some_ bytes but not all 355 error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read, 356 src_len); 357 } 358 return error; 359 } 360 361 // We now have a memory buffer that contains the part or all of the register 362 // value. Set the register value using this memory data. 363 // TODO: we might need to add a parameter to this function in case the byte 364 // order of the memory data doesn't match the process. For now we are 365 // assuming 366 // they are the same. 367 reg_value.SetFromMemoryData(reg_info, src, src_len, 368 process_sp->GetByteOrder(), error); 369 } else 370 error.SetErrorString("invalid process"); 371 372 return error; 373 } 374 375 Error RegisterContext::WriteRegisterValueToMemory( 376 const RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, 377 const RegisterValue ®_value) { 378 uint8_t dst[RegisterValue::kMaxRegisterByteSize]; 379 380 Error error; 381 382 ProcessSP process_sp(m_thread.GetProcess()); 383 if (process_sp) { 384 385 // TODO: we might need to add a parameter to this function in case the byte 386 // order of the memory data doesn't match the process. For now we are 387 // assuming 388 // they are the same. 389 390 const uint32_t bytes_copied = reg_value.GetAsMemoryData( 391 reg_info, dst, dst_len, process_sp->GetByteOrder(), error); 392 393 if (error.Success()) { 394 if (bytes_copied == 0) { 395 error.SetErrorString("byte copy failed."); 396 } else { 397 const uint32_t bytes_written = 398 process_sp->WriteMemory(dst_addr, dst, bytes_copied, error); 399 if (bytes_written != bytes_copied) { 400 if (error.Success()) { 401 // This might happen if we read _some_ bytes but not all 402 error.SetErrorStringWithFormat("only wrote %u of %u bytes", 403 bytes_written, bytes_copied); 404 } 405 } 406 } 407 } 408 } else 409 error.SetErrorString("invalid process"); 410 411 return error; 412 } 413 414 bool RegisterContext::ReadAllRegisterValues( 415 lldb_private::RegisterCheckpoint ®_checkpoint) { 416 return ReadAllRegisterValues(reg_checkpoint.GetData()); 417 } 418 419 bool RegisterContext::WriteAllRegisterValues( 420 const lldb_private::RegisterCheckpoint ®_checkpoint) { 421 return WriteAllRegisterValues(reg_checkpoint.GetData()); 422 } 423 424 TargetSP RegisterContext::CalculateTarget() { 425 return m_thread.CalculateTarget(); 426 } 427 428 ProcessSP RegisterContext::CalculateProcess() { 429 return m_thread.CalculateProcess(); 430 } 431 432 ThreadSP RegisterContext::CalculateThread() { 433 return m_thread.shared_from_this(); 434 } 435 436 StackFrameSP RegisterContext::CalculateStackFrame() { 437 // Register contexts might belong to many frames if we have inlined 438 // functions inside a frame since all inlined functions share the 439 // same registers, so we can't definitively say which frame we come from... 440 return StackFrameSP(); 441 } 442 443 void RegisterContext::CalculateExecutionContext(ExecutionContext &exe_ctx) { 444 m_thread.CalculateExecutionContext(exe_ctx); 445 } 446 447 bool RegisterContext::ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk, 448 uint32_t source_regnum, 449 lldb::RegisterKind target_rk, 450 uint32_t &target_regnum) { 451 const uint32_t num_registers = GetRegisterCount(); 452 for (uint32_t reg = 0; reg < num_registers; ++reg) { 453 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 454 455 if (reg_info->kinds[source_rk] == source_regnum) { 456 target_regnum = reg_info->kinds[target_rk]; 457 return (target_regnum != LLDB_INVALID_REGNUM); 458 } 459 } 460 return false; 461 } 462 463 // bool 464 // RegisterContext::ReadRegisterValue (uint32_t reg, Scalar &value) 465 //{ 466 // DataExtractor data; 467 // if (!ReadRegisterBytes (reg, data)) 468 // return false; 469 // 470 // const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg); 471 // uint32_t offset = 0; 472 // switch (reg_info->encoding) 473 // { 474 // case eEncodingInvalid: 475 // case eEncodingVector: 476 // break; 477 // 478 // case eEncodingUint: 479 // switch (reg_info->byte_size) 480 // { 481 // case 1: 482 // { 483 // value = data.GetU8 (&offset); 484 // return true; 485 // } 486 // case 2: 487 // { 488 // value = data.GetU16 (&offset); 489 // return true; 490 // } 491 // case 4: 492 // { 493 // value = data.GetU32 (&offset); 494 // return true; 495 // } 496 // case 8: 497 // { 498 // value = data.GetU64 (&offset); 499 // return true; 500 // } 501 // } 502 // break; 503 // case eEncodingSint: 504 // switch (reg_info->byte_size) 505 // { 506 // case 1: 507 // { 508 // int8_t v; 509 // if (data.ExtractBytes (0, sizeof (int8_t), 510 // endian::InlHostByteOrder(), &v) != sizeof (int8_t)) 511 // return false; 512 // value = v; 513 // return true; 514 // } 515 // case 2: 516 // { 517 // int16_t v; 518 // if (data.ExtractBytes (0, sizeof (int16_t), 519 // endian::InlHostByteOrder(), &v) != sizeof (int16_t)) 520 // return false; 521 // value = v; 522 // return true; 523 // } 524 // case 4: 525 // { 526 // int32_t v; 527 // if (data.ExtractBytes (0, sizeof (int32_t), 528 // endian::InlHostByteOrder(), &v) != sizeof (int32_t)) 529 // return false; 530 // value = v; 531 // return true; 532 // } 533 // case 8: 534 // { 535 // int64_t v; 536 // if (data.ExtractBytes (0, sizeof (int64_t), 537 // endian::InlHostByteOrder(), &v) != sizeof (int64_t)) 538 // return false; 539 // value = v; 540 // return true; 541 // } 542 // } 543 // break; 544 // case eEncodingIEEE754: 545 // switch (reg_info->byte_size) 546 // { 547 // case sizeof (float): 548 // { 549 // float v; 550 // if (data.ExtractBytes (0, sizeof (float), 551 // endian::InlHostByteOrder(), &v) != sizeof (float)) 552 // return false; 553 // value = v; 554 // return true; 555 // } 556 // case sizeof (double): 557 // { 558 // double v; 559 // if (data.ExtractBytes (0, sizeof (double), 560 // endian::InlHostByteOrder(), &v) != sizeof (double)) 561 // return false; 562 // value = v; 563 // return true; 564 // } 565 // case sizeof (long double): 566 // { 567 // double v; 568 // if (data.ExtractBytes (0, sizeof (long double), 569 // endian::InlHostByteOrder(), &v) != sizeof (long double)) 570 // return false; 571 // value = v; 572 // return true; 573 // } 574 // } 575 // break; 576 // } 577 // return false; 578 //} 579 // 580 // bool 581 // RegisterContext::WriteRegisterValue (uint32_t reg, const Scalar &value) 582 //{ 583 // DataExtractor data; 584 // if (!value.IsValid()) 585 // return false; 586 // if (!value.GetData (data)) 587 // return false; 588 // 589 // return WriteRegisterBytes (reg, data); 590 //} 591