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 Error 147 NativeRegisterContext::SetPC (lldb::addr_t pc) 148 { 149 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 150 return WriteRegisterFromUnsigned (reg, pc); 151 } 152 153 lldb::addr_t 154 NativeRegisterContext::GetSP (lldb::addr_t fail_value) 155 { 156 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); 157 return ReadRegisterAsUnsigned (reg, fail_value); 158 } 159 160 Error 161 NativeRegisterContext::SetSP (lldb::addr_t sp) 162 { 163 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); 164 return WriteRegisterFromUnsigned (reg, sp); 165 } 166 167 lldb::addr_t 168 NativeRegisterContext::GetFP (lldb::addr_t fail_value) 169 { 170 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP); 171 return ReadRegisterAsUnsigned (reg, fail_value); 172 } 173 174 Error 175 NativeRegisterContext::SetFP (lldb::addr_t fp) 176 { 177 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP); 178 return WriteRegisterFromUnsigned (reg, fp); 179 } 180 181 lldb::addr_t 182 NativeRegisterContext::GetReturnAddress (lldb::addr_t fail_value) 183 { 184 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA); 185 return ReadRegisterAsUnsigned (reg, fail_value); 186 } 187 188 lldb::addr_t 189 NativeRegisterContext::GetFlags (lldb::addr_t fail_value) 190 { 191 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 192 return ReadRegisterAsUnsigned (reg, fail_value); 193 } 194 195 196 lldb::addr_t 197 NativeRegisterContext::ReadRegisterAsUnsigned (uint32_t reg, lldb::addr_t fail_value) 198 { 199 if (reg != LLDB_INVALID_REGNUM) 200 return ReadRegisterAsUnsigned (GetRegisterInfoAtIndex (reg), fail_value); 201 return fail_value; 202 } 203 204 uint64_t 205 NativeRegisterContext::ReadRegisterAsUnsigned (const RegisterInfo *reg_info, lldb::addr_t fail_value) 206 { 207 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 208 209 if (reg_info) 210 { 211 RegisterValue value; 212 Error error = ReadRegister (reg_info, value); 213 if (error.Success ()) 214 { 215 if (log) 216 log->Printf ("NativeRegisterContext::%s ReadRegister() succeeded, value %" PRIu64, __FUNCTION__, value.GetAsUInt64()); 217 return value.GetAsUInt64(); 218 } 219 else 220 { 221 if (log) 222 log->Printf ("NativeRegisterContext::%s ReadRegister() failed, error %s", __FUNCTION__, error.AsCString ()); 223 } 224 } 225 else 226 { 227 if (log) 228 log->Printf ("NativeRegisterContext::%s ReadRegister() null reg_info", __FUNCTION__); 229 } 230 return fail_value; 231 } 232 233 Error 234 NativeRegisterContext::WriteRegisterFromUnsigned (uint32_t reg, uint64_t uval) 235 { 236 if (reg == LLDB_INVALID_REGNUM) 237 return Error ("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__); 238 return WriteRegisterFromUnsigned (GetRegisterInfoAtIndex (reg), uval); 239 } 240 241 Error 242 NativeRegisterContext::WriteRegisterFromUnsigned (const RegisterInfo *reg_info, uint64_t uval) 243 { 244 assert (reg_info); 245 if (!reg_info) 246 return Error ("reg_info is nullptr"); 247 248 RegisterValue value; 249 if (!value.SetUInt(uval, reg_info->byte_size)) 250 return Error ("RegisterValue::SetUInt () failed"); 251 252 return WriteRegister (reg_info, value); 253 } 254 255 lldb::tid_t 256 NativeRegisterContext::GetThreadID() const 257 { 258 return m_thread.GetID(); 259 } 260 261 uint32_t 262 NativeRegisterContext::NumSupportedHardwareBreakpoints () 263 { 264 return 0; 265 } 266 267 uint32_t 268 NativeRegisterContext::SetHardwareBreakpoint (lldb::addr_t addr, size_t size) 269 { 270 return LLDB_INVALID_INDEX32; 271 } 272 273 bool 274 NativeRegisterContext::ClearHardwareBreakpoint (uint32_t hw_idx) 275 { 276 return false; 277 } 278 279 280 uint32_t 281 NativeRegisterContext::NumSupportedHardwareWatchpoints () 282 { 283 return 0; 284 } 285 286 uint32_t 287 NativeRegisterContext::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags) 288 { 289 return LLDB_INVALID_INDEX32; 290 } 291 292 bool 293 NativeRegisterContext::ClearHardwareWatchpoint (uint32_t hw_index) 294 { 295 return false; 296 } 297 298 Error 299 NativeRegisterContext::ClearAllHardwareWatchpoints () 300 { 301 return Error ("not implemented"); 302 } 303 304 Error 305 NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) 306 { 307 is_hit = false; 308 return Error ("not implemented"); 309 } 310 311 Error 312 NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index) 313 { 314 wp_index = LLDB_INVALID_INDEX32; 315 return Error ("not implemented"); 316 } 317 318 Error 319 NativeRegisterContext::IsWatchpointVacant (uint32_t wp_index, bool &is_vacant) 320 { 321 is_vacant = false; 322 return Error ("not implemented"); 323 } 324 325 lldb::addr_t 326 NativeRegisterContext::GetWatchpointAddress (uint32_t wp_index) 327 { 328 return LLDB_INVALID_ADDRESS; 329 } 330 331 bool 332 NativeRegisterContext::HardwareSingleStep (bool enable) 333 { 334 return false; 335 } 336 337 Error 338 NativeRegisterContext::ReadRegisterValueFromMemory ( 339 const RegisterInfo *reg_info, 340 lldb::addr_t src_addr, 341 lldb::addr_t src_len, 342 RegisterValue ®_value) 343 { 344 Error error; 345 if (reg_info == nullptr) 346 { 347 error.SetErrorString ("invalid register info argument."); 348 return error; 349 } 350 351 352 // Moving from addr into a register 353 // 354 // Case 1: src_len == dst_len 355 // 356 // |AABBCCDD| Address contents 357 // |AABBCCDD| Register contents 358 // 359 // Case 2: src_len > dst_len 360 // 361 // Error! (The register should always be big enough to hold the data) 362 // 363 // Case 3: src_len < dst_len 364 // 365 // |AABB| Address contents 366 // |AABB0000| Register contents [on little-endian hardware] 367 // |0000AABB| Register contents [on big-endian hardware] 368 if (src_len > RegisterValue::kMaxRegisterByteSize) 369 { 370 error.SetErrorString ("register too small to receive memory data"); 371 return error; 372 } 373 374 const lldb::addr_t dst_len = reg_info->byte_size; 375 376 if (src_len > dst_len) 377 { 378 error.SetErrorStringWithFormat("%" PRIu64 " bytes is too big to store in register %s (%" PRIu64 " bytes)", src_len, reg_info->name, dst_len); 379 return error; 380 } 381 382 NativeProcessProtocolSP process_sp (m_thread.GetProcess ()); 383 if (!process_sp) 384 { 385 error.SetErrorString("invalid process"); 386 return error; 387 } 388 389 uint8_t src[RegisterValue::kMaxRegisterByteSize]; 390 391 // Read the memory 392 lldb::addr_t bytes_read; 393 error = process_sp->ReadMemory (src_addr, src, src_len, bytes_read); 394 if (error.Fail ()) 395 return error; 396 397 // Make sure the memory read succeeded... 398 if (bytes_read != src_len) 399 { 400 // This might happen if we read _some_ bytes but not all 401 error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes", bytes_read, src_len); 402 return error; 403 } 404 405 // We now have a memory buffer that contains the part or all of the register 406 // value. Set the register value using this memory data. 407 // TODO: we might need to add a parameter to this function in case the byte 408 // order of the memory data doesn't match the process. For now we are assuming 409 // they are the same. 410 lldb::ByteOrder byte_order; 411 if (!process_sp->GetByteOrder (byte_order)) 412 { 413 error.SetErrorString ( "NativeProcessProtocol::GetByteOrder () failed"); 414 return error; 415 } 416 417 reg_value.SetFromMemoryData ( 418 reg_info, 419 src, 420 src_len, 421 byte_order, 422 error); 423 424 return error; 425 } 426 427 Error 428 NativeRegisterContext::WriteRegisterValueToMemory ( 429 const RegisterInfo *reg_info, 430 lldb::addr_t dst_addr, 431 lldb::addr_t dst_len, 432 const RegisterValue ®_value) 433 { 434 435 uint8_t dst[RegisterValue::kMaxRegisterByteSize]; 436 437 Error error; 438 439 NativeProcessProtocolSP process_sp (m_thread.GetProcess ()); 440 if (process_sp) 441 { 442 443 // TODO: we might need to add a parameter to this function in case the byte 444 // order of the memory data doesn't match the process. For now we are assuming 445 // they are the same. 446 lldb::ByteOrder byte_order; 447 if (!process_sp->GetByteOrder (byte_order)) 448 return Error ("NativeProcessProtocol::GetByteOrder () failed"); 449 450 const lldb::addr_t bytes_copied = reg_value.GetAsMemoryData ( 451 reg_info, 452 dst, 453 dst_len, 454 byte_order, 455 error); 456 457 if (error.Success()) 458 { 459 if (bytes_copied == 0) 460 { 461 error.SetErrorString("byte copy failed."); 462 } 463 else 464 { 465 lldb::addr_t bytes_written; 466 error = process_sp->WriteMemory (dst_addr, dst, bytes_copied, bytes_written); 467 if (error.Fail ()) 468 return error; 469 470 if (bytes_written != bytes_copied) 471 { 472 // This might happen if we read _some_ bytes but not all 473 error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64 " bytes", bytes_written, bytes_copied); 474 } 475 } 476 } 477 } 478 else 479 error.SetErrorString("invalid process"); 480 481 return error; 482 } 483 484 uint32_t 485 NativeRegisterContext::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const 486 { 487 const uint32_t num_regs = GetRegisterCount(); 488 489 assert (kind < kNumRegisterKinds); 490 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) 491 { 492 const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg_idx); 493 494 if (reg_info->kinds[kind] == num) 495 return reg_idx; 496 } 497 498 return LLDB_INVALID_REGNUM; 499 } 500 501 502