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