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