1 //===-- NativeRegisterContext.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 "lldb/Host/common/NativeRegisterContext.h" 11 12 #include "lldb/Core/RegisterValue.h" 13 #include "lldb/Utility/Log.h" 14 15 #include "lldb/Host/PosixApi.h" 16 #include "lldb/Host/common/NativeProcessProtocol.h" 17 #include "lldb/Host/common/NativeThreadProtocol.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread) 23 : m_thread(thread) {} 24 25 //---------------------------------------------------------------------- 26 // Destructor 27 //---------------------------------------------------------------------- 28 NativeRegisterContext::~NativeRegisterContext() {} 29 30 // FIXME revisit invalidation, process stop ids, etc. Right now we don't 31 // support caching in NativeRegisterContext. We can do this later by 32 // utilizing NativeProcessProtocol::GetStopID () and adding a stop id to 33 // NativeRegisterContext. 34 35 // void 36 // NativeRegisterContext::InvalidateIfNeeded (bool force) 37 // { 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 // { 52 // InvalidateAllRegisters (); 53 // SetStopID (process_stop_id); 54 // } 55 // } 56 57 const RegisterInfo * 58 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name, 59 uint32_t start_idx) { 60 if (reg_name.empty()) 61 return nullptr; 62 63 const uint32_t num_registers = GetRegisterCount(); 64 for (uint32_t reg = start_idx; reg < num_registers; ++reg) { 65 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 66 67 if (reg_name.equals_lower(reg_info->name) || 68 reg_name.equals_lower(reg_info->alt_name)) 69 return reg_info; 70 } 71 return nullptr; 72 } 73 74 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind, 75 uint32_t num) { 76 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num); 77 if (reg_num == LLDB_INVALID_REGNUM) 78 return nullptr; 79 return GetRegisterInfoAtIndex(reg_num); 80 } 81 82 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) { 83 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg); 84 if (reg_info) 85 return reg_info->name; 86 return nullptr; 87 } 88 89 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex( 90 uint32_t reg_index) const { 91 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index); 92 if (!reg_info) 93 return nullptr; 94 95 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) { 96 const RegisterSet *const reg_set = GetRegisterSet(set_index); 97 if (!reg_set) 98 continue; 99 100 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers; 101 ++reg_num_index) { 102 const uint32_t reg_num = reg_set->registers[reg_num_index]; 103 // FIXME double check we're checking the right register kind here. 104 if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num) { 105 // The given register is a member of this register set. Return the 106 // register set name. 107 return reg_set->name; 108 } 109 } 110 } 111 112 // Didn't find it. 113 return nullptr; 114 } 115 116 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) { 117 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 118 119 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 120 LLDB_REGNUM_GENERIC_PC); 121 if (log) 122 log->Printf("NativeRegisterContext::%s using reg index %" PRIu32 123 " (default %" PRIu64 ")", 124 __FUNCTION__, reg, fail_value); 125 126 const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value); 127 128 if (log) 129 log->Printf("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64, 130 __FUNCTION__, retval); 131 132 return retval; 133 } 134 135 lldb::addr_t 136 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) { 137 return GetPC(fail_value); 138 } 139 140 Status NativeRegisterContext::SetPC(lldb::addr_t pc) { 141 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 142 LLDB_REGNUM_GENERIC_PC); 143 return WriteRegisterFromUnsigned(reg, pc); 144 } 145 146 lldb::addr_t NativeRegisterContext::GetSP(lldb::addr_t fail_value) { 147 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 148 LLDB_REGNUM_GENERIC_SP); 149 return ReadRegisterAsUnsigned(reg, fail_value); 150 } 151 152 Status NativeRegisterContext::SetSP(lldb::addr_t sp) { 153 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 154 LLDB_REGNUM_GENERIC_SP); 155 return WriteRegisterFromUnsigned(reg, sp); 156 } 157 158 lldb::addr_t NativeRegisterContext::GetFP(lldb::addr_t fail_value) { 159 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 160 LLDB_REGNUM_GENERIC_FP); 161 return ReadRegisterAsUnsigned(reg, fail_value); 162 } 163 164 Status NativeRegisterContext::SetFP(lldb::addr_t fp) { 165 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 166 LLDB_REGNUM_GENERIC_FP); 167 return WriteRegisterFromUnsigned(reg, fp); 168 } 169 170 lldb::addr_t NativeRegisterContext::GetReturnAddress(lldb::addr_t fail_value) { 171 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 172 LLDB_REGNUM_GENERIC_RA); 173 return ReadRegisterAsUnsigned(reg, fail_value); 174 } 175 176 lldb::addr_t NativeRegisterContext::GetFlags(lldb::addr_t fail_value) { 177 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, 178 LLDB_REGNUM_GENERIC_FLAGS); 179 return ReadRegisterAsUnsigned(reg, fail_value); 180 } 181 182 lldb::addr_t 183 NativeRegisterContext::ReadRegisterAsUnsigned(uint32_t reg, 184 lldb::addr_t fail_value) { 185 if (reg != LLDB_INVALID_REGNUM) 186 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value); 187 return fail_value; 188 } 189 190 uint64_t 191 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info, 192 lldb::addr_t fail_value) { 193 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 194 195 if (reg_info) { 196 RegisterValue value; 197 Status error = ReadRegister(reg_info, value); 198 if (error.Success()) { 199 if (log) 200 log->Printf("NativeRegisterContext::%s ReadRegister() succeeded, value " 201 "%" PRIu64, 202 __FUNCTION__, value.GetAsUInt64()); 203 return value.GetAsUInt64(); 204 } else { 205 if (log) 206 log->Printf("NativeRegisterContext::%s ReadRegister() failed, error %s", 207 __FUNCTION__, error.AsCString()); 208 } 209 } else { 210 if (log) 211 log->Printf("NativeRegisterContext::%s ReadRegister() null reg_info", 212 __FUNCTION__); 213 } 214 return fail_value; 215 } 216 217 Status NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg, 218 uint64_t uval) { 219 if (reg == LLDB_INVALID_REGNUM) 220 return Status("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__); 221 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval); 222 } 223 224 Status 225 NativeRegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info, 226 uint64_t uval) { 227 assert(reg_info); 228 if (!reg_info) 229 return Status("reg_info is nullptr"); 230 231 RegisterValue value; 232 if (!value.SetUInt(uval, reg_info->byte_size)) 233 return Status("RegisterValue::SetUInt () failed"); 234 235 return WriteRegister(reg_info, value); 236 } 237 238 lldb::tid_t NativeRegisterContext::GetThreadID() const { 239 return m_thread.GetID(); 240 } 241 242 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; } 243 244 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr, 245 size_t size) { 246 return LLDB_INVALID_INDEX32; 247 } 248 249 Status NativeRegisterContext::ClearAllHardwareBreakpoints() { 250 return Status("not implemented"); 251 } 252 253 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) { 254 return false; 255 } 256 257 Status NativeRegisterContext::GetHardwareBreakHitIndex(uint32_t &bp_index, 258 lldb::addr_t trap_addr) { 259 bp_index = LLDB_INVALID_INDEX32; 260 return Status("not implemented"); 261 } 262 263 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; } 264 265 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr, 266 size_t size, 267 uint32_t watch_flags) { 268 return LLDB_INVALID_INDEX32; 269 } 270 271 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) { 272 return false; 273 } 274 275 Status NativeRegisterContext::ClearAllHardwareWatchpoints() { 276 return Status("not implemented"); 277 } 278 279 Status NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) { 280 is_hit = false; 281 return Status("not implemented"); 282 } 283 284 Status NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index, 285 lldb::addr_t trap_addr) { 286 wp_index = LLDB_INVALID_INDEX32; 287 return Status("not implemented"); 288 } 289 290 Status NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index, 291 bool &is_vacant) { 292 is_vacant = false; 293 return Status("not implemented"); 294 } 295 296 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) { 297 return LLDB_INVALID_ADDRESS; 298 } 299 300 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) { 301 return LLDB_INVALID_ADDRESS; 302 } 303 304 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; } 305 306 Status NativeRegisterContext::ReadRegisterValueFromMemory( 307 const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len, 308 RegisterValue ®_value) { 309 Status error; 310 if (reg_info == nullptr) { 311 error.SetErrorString("invalid register info argument."); 312 return error; 313 } 314 315 // Moving from addr into a register 316 // 317 // Case 1: src_len == dst_len 318 // 319 // |AABBCCDD| Address contents 320 // |AABBCCDD| Register contents 321 // 322 // Case 2: src_len > dst_len 323 // 324 // Status! (The register should always be big enough to hold the data) 325 // 326 // Case 3: src_len < dst_len 327 // 328 // |AABB| Address contents 329 // |AABB0000| Register contents [on little-endian hardware] 330 // |0000AABB| Register contents [on big-endian hardware] 331 if (src_len > RegisterValue::kMaxRegisterByteSize) { 332 error.SetErrorString("register too small to receive memory data"); 333 return error; 334 } 335 336 const size_t dst_len = reg_info->byte_size; 337 338 if (src_len > dst_len) { 339 error.SetErrorStringWithFormat( 340 "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64 341 " bytes)", 342 static_cast<uint64_t>(src_len), reg_info->name, 343 static_cast<uint64_t>(dst_len)); 344 return error; 345 } 346 347 NativeProcessProtocol &process = m_thread.GetProcess(); 348 uint8_t src[RegisterValue::kMaxRegisterByteSize]; 349 350 // Read the memory 351 size_t bytes_read; 352 error = process.ReadMemory(src_addr, src, src_len, bytes_read); 353 if (error.Fail()) 354 return error; 355 356 // Make sure the memory read succeeded... 357 if (bytes_read != src_len) { 358 // This might happen if we read _some_ bytes but not all 359 error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes", 360 static_cast<uint64_t>(bytes_read), 361 static_cast<uint64_t>(src_len)); 362 return error; 363 } 364 365 // We now have a memory buffer that contains the part or all of the register 366 // value. Set the register value using this memory data. 367 // TODO: we might need to add a parameter to this function in case the byte 368 // order of the memory data doesn't match the process. For now we are assuming 369 // they are the same. 370 reg_value.SetFromMemoryData(reg_info, src, src_len, process.GetByteOrder(), 371 error); 372 373 return error; 374 } 375 376 Status NativeRegisterContext::WriteRegisterValueToMemory( 377 const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len, 378 const RegisterValue ®_value) { 379 380 uint8_t dst[RegisterValue::kMaxRegisterByteSize]; 381 382 Status error; 383 384 NativeProcessProtocol &process = m_thread.GetProcess(); 385 386 // TODO: we might need to add a parameter to this function in case the byte 387 // order of the memory data doesn't match the process. For now we are 388 // assuming 389 // they are the same. 390 const size_t bytes_copied = reg_value.GetAsMemoryData( 391 reg_info, dst, dst_len, process.GetByteOrder(), error); 392 393 if (error.Success()) { 394 if (bytes_copied == 0) { 395 error.SetErrorString("byte copy failed."); 396 } else { 397 size_t bytes_written; 398 error = process.WriteMemory(dst_addr, dst, bytes_copied, bytes_written); 399 if (error.Fail()) 400 return error; 401 402 if (bytes_written != bytes_copied) { 403 // This might happen if we read _some_ bytes but not all 404 error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64 405 " bytes", 406 static_cast<uint64_t>(bytes_written), 407 static_cast<uint64_t>(bytes_copied)); 408 } 409 } 410 } 411 412 return error; 413 } 414 415 uint32_t 416 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind, 417 uint32_t num) const { 418 const uint32_t num_regs = GetRegisterCount(); 419 420 assert(kind < kNumRegisterKinds); 421 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) { 422 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx); 423 424 if (reg_info->kinds[kind] == num) 425 return reg_idx; 426 } 427 428 return LLDB_INVALID_REGNUM; 429 } 430