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/Module.h" 16 #include "lldb/Core/Value.h" 17 #include "lldb/Expression/DWARFExpression.h" 18 #include "lldb/Target/ExecutionContext.h" 19 #include "lldb/Target/Process.h" 20 #include "lldb/Target/StackFrame.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Target/Thread.h" 23 #include "lldb/Utility/DataExtractor.h" 24 #include "lldb/Utility/Endian.h" 25 #include "lldb/Utility/RegisterValue.h" 26 #include "lldb/Utility/Scalar.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 * 57 RegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name, 58 uint32_t start_idx) { 59 if (reg_name.empty()) 60 return nullptr; 61 62 const uint32_t num_registers = GetRegisterCount(); 63 for (uint32_t reg = start_idx; reg < num_registers; ++reg) { 64 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 65 66 if (reg_name.equals_lower(reg_info->name) || 67 reg_name.equals_lower(reg_info->alt_name)) 68 return reg_info; 69 } 70 return nullptr; 71 } 72 73 uint32_t 74 RegisterContext::UpdateDynamicRegisterSize(const lldb_private::ArchSpec &arch, 75 RegisterInfo *reg_info) { 76 ExecutionContext exe_ctx(CalculateThread()); 77 78 // In MIPS, the floating point registers size is depends on FR bit of SR 79 // register. if SR.FR == 1 then all floating point registers are 64 bits. 80 // else they are all 32 bits. 81 82 int expr_result; 83 uint32_t addr_size = arch.GetAddressByteSize(); 84 const uint8_t *dwarf_opcode_ptr = reg_info->dynamic_size_dwarf_expr_bytes; 85 const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len; 86 87 DataExtractor dwarf_data(dwarf_opcode_ptr, dwarf_opcode_len, 88 arch.GetByteOrder(), addr_size); 89 ModuleSP opcode_ctx; 90 DWARFExpression dwarf_expr(opcode_ctx, dwarf_data, nullptr, 0, 91 dwarf_opcode_len); 92 Value result; 93 Status error; 94 const lldb::offset_t offset = 0; 95 if (dwarf_expr.Evaluate(&exe_ctx, this, opcode_ctx, dwarf_data, nullptr, 96 offset, dwarf_opcode_len, eRegisterKindDWARF, nullptr, 97 nullptr, result, &error)) { 98 expr_result = result.GetScalar().SInt(-1); 99 switch (expr_result) { 100 case 0: 101 return 4; 102 case 1: 103 return 8; 104 default: 105 return reg_info->byte_size; 106 } 107 } else { 108 printf("Error executing DwarfExpression::Evaluate %s\n", error.AsCString()); 109 return reg_info->byte_size; 110 } 111 } 112 113 const RegisterInfo *RegisterContext::GetRegisterInfo(lldb::RegisterKind kind, 114 uint32_t num) { 115 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num); 116 if (reg_num == LLDB_INVALID_REGNUM) 117 return nullptr; 118 return GetRegisterInfoAtIndex(reg_num); 119 } 120 121 const char *RegisterContext::GetRegisterName(uint32_t reg) { 122 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 123 if (reg_info) 124 return reg_info->name; 125 return nullptr; 126 } 127 128 uint64_t RegisterContext::GetPC(uint64_t fail_value) { 129 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 130 LLDB_REGNUM_GENERIC_PC); 131 uint64_t pc = ReadRegisterAsUnsigned(reg, fail_value); 132 133 if (pc != fail_value) { 134 TargetSP target_sp = m_thread.CalculateTarget(); 135 if (target_sp) { 136 Target *target = target_sp.get(); 137 if (target) 138 pc = target->GetOpcodeLoadAddress(pc, AddressClass::eCode); 139 } 140 } 141 142 return pc; 143 } 144 145 bool RegisterContext::SetPC(uint64_t pc) { 146 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 147 LLDB_REGNUM_GENERIC_PC); 148 bool success = WriteRegisterFromUnsigned(reg, pc); 149 if (success) { 150 StackFrameSP frame_sp( 151 m_thread.GetFrameWithConcreteFrameIndex(m_concrete_frame_idx)); 152 if (frame_sp) 153 frame_sp->ChangePC(pc); 154 else 155 m_thread.ClearStackFrames(); 156 } 157 return success; 158 } 159 160 bool RegisterContext::SetPC(Address addr) { 161 TargetSP target_sp = m_thread.CalculateTarget(); 162 Target *target = target_sp.get(); 163 164 lldb::addr_t callAddr = addr.GetCallableLoadAddress(target); 165 if (callAddr == LLDB_INVALID_ADDRESS) 166 return false; 167 168 return SetPC(callAddr); 169 } 170 171 uint64_t RegisterContext::GetSP(uint64_t fail_value) { 172 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 173 LLDB_REGNUM_GENERIC_SP); 174 return ReadRegisterAsUnsigned(reg, fail_value); 175 } 176 177 bool RegisterContext::SetSP(uint64_t sp) { 178 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 179 LLDB_REGNUM_GENERIC_SP); 180 return WriteRegisterFromUnsigned(reg, sp); 181 } 182 183 uint64_t RegisterContext::GetFP(uint64_t fail_value) { 184 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 185 LLDB_REGNUM_GENERIC_FP); 186 return ReadRegisterAsUnsigned(reg, fail_value); 187 } 188 189 bool RegisterContext::SetFP(uint64_t fp) { 190 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 191 LLDB_REGNUM_GENERIC_FP); 192 return WriteRegisterFromUnsigned(reg, fp); 193 } 194 195 uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value) { 196 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 197 LLDB_REGNUM_GENERIC_RA); 198 return ReadRegisterAsUnsigned(reg, fail_value); 199 } 200 201 uint64_t RegisterContext::GetFlags(uint64_t fail_value) { 202 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 203 LLDB_REGNUM_GENERIC_FLAGS); 204 return ReadRegisterAsUnsigned(reg, fail_value); 205 } 206 207 uint64_t RegisterContext::ReadRegisterAsUnsigned(uint32_t reg, 208 uint64_t fail_value) { 209 if (reg != LLDB_INVALID_REGNUM) 210 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value); 211 return fail_value; 212 } 213 214 uint64_t RegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info, 215 uint64_t fail_value) { 216 if (reg_info) { 217 RegisterValue value; 218 if (ReadRegister(reg_info, value)) 219 return value.GetAsUInt64(); 220 } 221 return fail_value; 222 } 223 224 bool RegisterContext::WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval) { 225 if (reg == LLDB_INVALID_REGNUM) 226 return false; 227 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval); 228 } 229 230 bool RegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info, 231 uint64_t uval) { 232 if (reg_info) { 233 RegisterValue value; 234 if (value.SetUInt(uval, reg_info->byte_size)) 235 return WriteRegister(reg_info, value); 236 } 237 return false; 238 } 239 240 bool RegisterContext::CopyFromRegisterContext(lldb::RegisterContextSP context) { 241 uint32_t num_register_sets = context->GetRegisterSetCount(); 242 // We don't know that two threads have the same register context, so require 243 // the threads to be the same. 244 if (context->GetThreadID() != GetThreadID()) 245 return false; 246 247 if (num_register_sets != GetRegisterSetCount()) 248 return false; 249 250 RegisterContextSP frame_zero_context = m_thread.GetRegisterContext(); 251 252 for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx) { 253 const RegisterSet *const reg_set = GetRegisterSet(set_idx); 254 255 const uint32_t num_registers = reg_set->num_registers; 256 for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx) { 257 const uint32_t reg = reg_set->registers[reg_idx]; 258 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 259 if (!reg_info || reg_info->value_regs) 260 continue; 261 RegisterValue reg_value; 262 263 // If we can reconstruct the register from the frame we are copying from, 264 // then do so, otherwise use the value from frame 0. 265 if (context->ReadRegister(reg_info, reg_value)) { 266 WriteRegister(reg_info, reg_value); 267 } else if (frame_zero_context->ReadRegister(reg_info, reg_value)) { 268 WriteRegister(reg_info, reg_value); 269 } 270 } 271 } 272 return true; 273 } 274 275 lldb::tid_t RegisterContext::GetThreadID() const { return m_thread.GetID(); } 276 277 uint32_t RegisterContext::NumSupportedHardwareBreakpoints() { return 0; } 278 279 uint32_t RegisterContext::SetHardwareBreakpoint(lldb::addr_t addr, 280 size_t size) { 281 return LLDB_INVALID_INDEX32; 282 } 283 284 bool RegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) { return false; } 285 286 uint32_t RegisterContext::NumSupportedHardwareWatchpoints() { return 0; } 287 288 uint32_t RegisterContext::SetHardwareWatchpoint(lldb::addr_t addr, size_t size, 289 bool read, bool write) { 290 return LLDB_INVALID_INDEX32; 291 } 292 293 bool RegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) { 294 return false; 295 } 296 297 bool RegisterContext::HardwareSingleStep(bool enable) { return false; } 298 299 Status RegisterContext::ReadRegisterValueFromMemory( 300 const RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, 301 RegisterValue ®_value) { 302 Status error; 303 if (reg_info == nullptr) { 304 error.SetErrorString("invalid register info argument."); 305 return error; 306 } 307 308 // Moving from addr into a register 309 // 310 // Case 1: src_len == dst_len 311 // 312 // |AABBCCDD| Address contents 313 // |AABBCCDD| Register contents 314 // 315 // Case 2: src_len > dst_len 316 // 317 // Status! (The register should always be big enough to hold the data) 318 // 319 // Case 3: src_len < dst_len 320 // 321 // |AABB| Address contents 322 // |AABB0000| Register contents [on little-endian hardware] 323 // |0000AABB| Register contents [on big-endian hardware] 324 if (src_len > RegisterValue::kMaxRegisterByteSize) { 325 error.SetErrorString("register too small to receive memory data"); 326 return error; 327 } 328 329 const uint32_t dst_len = reg_info->byte_size; 330 331 if (src_len > dst_len) { 332 error.SetErrorStringWithFormat( 333 "%u bytes is too big to store in register %s (%u bytes)", src_len, 334 reg_info->name, dst_len); 335 return error; 336 } 337 338 ProcessSP process_sp(m_thread.GetProcess()); 339 if (process_sp) { 340 uint8_t src[RegisterValue::kMaxRegisterByteSize]; 341 342 // Read the memory 343 const uint32_t bytes_read = 344 process_sp->ReadMemory(src_addr, src, src_len, error); 345 346 // Make sure the memory read succeeded... 347 if (bytes_read != src_len) { 348 if (error.Success()) { 349 // This might happen if we read _some_ bytes but not all 350 error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read, 351 src_len); 352 } 353 return error; 354 } 355 356 // We now have a memory buffer that contains the part or all of the 357 // register value. Set the register value using this memory data. 358 // TODO: we might need to add a parameter to this function in case the byte 359 // order of the memory data doesn't match the process. For now we are 360 // assuming they are the same. 361 reg_value.SetFromMemoryData(reg_info, src, src_len, 362 process_sp->GetByteOrder(), error); 363 } else 364 error.SetErrorString("invalid process"); 365 366 return error; 367 } 368 369 Status RegisterContext::WriteRegisterValueToMemory( 370 const RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, 371 const RegisterValue ®_value) { 372 uint8_t dst[RegisterValue::kMaxRegisterByteSize]; 373 374 Status error; 375 376 ProcessSP process_sp(m_thread.GetProcess()); 377 if (process_sp) { 378 379 // TODO: we might need to add a parameter to this function in case the byte 380 // order of the memory data doesn't match the process. For now we are 381 // assuming they are the same. 382 383 const uint32_t bytes_copied = reg_value.GetAsMemoryData( 384 reg_info, dst, dst_len, process_sp->GetByteOrder(), error); 385 386 if (error.Success()) { 387 if (bytes_copied == 0) { 388 error.SetErrorString("byte copy failed."); 389 } else { 390 const uint32_t bytes_written = 391 process_sp->WriteMemory(dst_addr, dst, bytes_copied, error); 392 if (bytes_written != bytes_copied) { 393 if (error.Success()) { 394 // This might happen if we read _some_ bytes but not all 395 error.SetErrorStringWithFormat("only wrote %u of %u bytes", 396 bytes_written, bytes_copied); 397 } 398 } 399 } 400 } 401 } else 402 error.SetErrorString("invalid process"); 403 404 return error; 405 } 406 407 bool RegisterContext::ReadAllRegisterValues( 408 lldb_private::RegisterCheckpoint ®_checkpoint) { 409 return ReadAllRegisterValues(reg_checkpoint.GetData()); 410 } 411 412 bool RegisterContext::WriteAllRegisterValues( 413 const lldb_private::RegisterCheckpoint ®_checkpoint) { 414 return WriteAllRegisterValues(reg_checkpoint.GetData()); 415 } 416 417 TargetSP RegisterContext::CalculateTarget() { 418 return m_thread.CalculateTarget(); 419 } 420 421 ProcessSP RegisterContext::CalculateProcess() { 422 return m_thread.CalculateProcess(); 423 } 424 425 ThreadSP RegisterContext::CalculateThread() { 426 return m_thread.shared_from_this(); 427 } 428 429 StackFrameSP RegisterContext::CalculateStackFrame() { 430 // Register contexts might belong to many frames if we have inlined functions 431 // inside a frame since all inlined functions share the same registers, so we 432 // can't definitively say which frame we come from... 433 return StackFrameSP(); 434 } 435 436 void RegisterContext::CalculateExecutionContext(ExecutionContext &exe_ctx) { 437 m_thread.CalculateExecutionContext(exe_ctx); 438 } 439 440 bool RegisterContext::ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk, 441 uint32_t source_regnum, 442 lldb::RegisterKind target_rk, 443 uint32_t &target_regnum) { 444 const uint32_t num_registers = GetRegisterCount(); 445 for (uint32_t reg = 0; reg < num_registers; ++reg) { 446 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 447 448 if (reg_info->kinds[source_rk] == source_regnum) { 449 target_regnum = reg_info->kinds[target_rk]; 450 return (target_regnum != LLDB_INVALID_REGNUM); 451 } 452 } 453 return false; 454 } 455