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