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