1 //===-- NativeProcessProtocol.cpp -----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/common/NativeProcessProtocol.h" 10 #include "lldb/Host/Host.h" 11 #include "lldb/Host/common/NativeBreakpointList.h" 12 #include "lldb/Host/common/NativeRegisterContext.h" 13 #include "lldb/Host/common/NativeThreadProtocol.h" 14 #include "lldb/Utility/LLDBAssert.h" 15 #include "lldb/Utility/Log.h" 16 #include "lldb/Utility/State.h" 17 #include "lldb/lldb-enumerations.h" 18 19 #include "llvm/Support/Process.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 // NativeProcessProtocol Members 25 26 NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 27 NativeDelegate &delegate) 28 : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) { 29 delegate.InitializeDelegate(this); 30 } 31 32 lldb_private::Status NativeProcessProtocol::Interrupt() { 33 Status error; 34 #if !defined(SIGSTOP) 35 error.SetErrorString("local host does not support signaling"); 36 return error; 37 #else 38 return Signal(SIGSTOP); 39 #endif 40 } 41 42 Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) { 43 m_signals_to_ignore.clear(); 44 m_signals_to_ignore.insert(signals.begin(), signals.end()); 45 return Status(); 46 } 47 48 lldb_private::Status 49 NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, 50 MemoryRegionInfo &range_info) { 51 // Default: not implemented. 52 return Status("not implemented"); 53 } 54 55 llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() { 56 if (m_state == lldb::eStateExited) 57 return m_exit_status; 58 59 return llvm::None; 60 } 61 62 bool NativeProcessProtocol::SetExitStatus(WaitStatus status, 63 bool bNotifyStateChange) { 64 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 65 LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange); 66 67 // Exit status already set 68 if (m_state == lldb::eStateExited) { 69 if (m_exit_status) 70 LLDB_LOG(log, "exit status already set to {0}", *m_exit_status); 71 else 72 LLDB_LOG(log, "state is exited, but status not set"); 73 return false; 74 } 75 76 m_state = lldb::eStateExited; 77 m_exit_status = status; 78 79 if (bNotifyStateChange) 80 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); 81 82 return true; 83 } 84 85 NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { 86 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 87 if (idx < m_threads.size()) 88 return m_threads[idx].get(); 89 return nullptr; 90 } 91 92 NativeThreadProtocol * 93 NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { 94 for (const auto &thread : m_threads) { 95 if (thread->GetID() == tid) 96 return thread.get(); 97 } 98 return nullptr; 99 } 100 101 NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { 102 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 103 return GetThreadByIDUnlocked(tid); 104 } 105 106 bool NativeProcessProtocol::IsAlive() const { 107 return m_state != eStateDetached && m_state != eStateExited && 108 m_state != eStateInvalid && m_state != eStateUnloaded; 109 } 110 111 const NativeWatchpointList::WatchpointMap & 112 NativeProcessProtocol::GetWatchpointMap() const { 113 return m_watchpoint_list.GetWatchpointMap(); 114 } 115 116 llvm::Optional<std::pair<uint32_t, uint32_t>> 117 NativeProcessProtocol::GetHardwareDebugSupportInfo() const { 118 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 119 120 // get any thread 121 NativeThreadProtocol *thread( 122 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); 123 if (!thread) { 124 LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!"); 125 return llvm::None; 126 } 127 128 NativeRegisterContext ®_ctx = thread->GetRegisterContext(); 129 return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(), 130 reg_ctx.NumSupportedHardwareWatchpoints()); 131 } 132 133 Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, 134 uint32_t watch_flags, 135 bool hardware) { 136 // This default implementation assumes setting the watchpoint for the process 137 // will require setting the watchpoint for each of the threads. Furthermore, 138 // it will track watchpoints set for the process and will add them to each 139 // thread that is attached to via the (FIXME implement) OnThreadAttached () 140 // method. 141 142 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 143 144 // Update the thread list 145 UpdateThreads(); 146 147 // Keep track of the threads we successfully set the watchpoint for. If one 148 // of the thread watchpoint setting operations fails, back off and remove the 149 // watchpoint for all the threads that were successfully set so we get back 150 // to a consistent state. 151 std::vector<NativeThreadProtocol *> watchpoint_established_threads; 152 153 // Tell each thread to set a watchpoint. In the event that hardware 154 // watchpoints are requested but the SetWatchpoint fails, try to set a 155 // software watchpoint as a fallback. It's conceivable that if there are 156 // more threads than hardware watchpoints available, some of the threads will 157 // fail to set hardware watchpoints while software ones may be available. 158 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 159 for (const auto &thread : m_threads) { 160 assert(thread && "thread list should not have a NULL thread!"); 161 162 Status thread_error = 163 thread->SetWatchpoint(addr, size, watch_flags, hardware); 164 if (thread_error.Fail() && hardware) { 165 // Try software watchpoints since we failed on hardware watchpoint 166 // setting and we may have just run out of hardware watchpoints. 167 thread_error = thread->SetWatchpoint(addr, size, watch_flags, false); 168 if (thread_error.Success()) 169 LLDB_LOG(log, 170 "hardware watchpoint requested but software watchpoint set"); 171 } 172 173 if (thread_error.Success()) { 174 // Remember that we set this watchpoint successfully in case we need to 175 // clear it later. 176 watchpoint_established_threads.push_back(thread.get()); 177 } else { 178 // Unset the watchpoint for each thread we successfully set so that we 179 // get back to a consistent state of "not set" for the watchpoint. 180 for (auto unwatch_thread_sp : watchpoint_established_threads) { 181 Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); 182 if (remove_error.Fail()) 183 LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}", 184 GetID(), unwatch_thread_sp->GetID(), remove_error); 185 } 186 187 return thread_error; 188 } 189 } 190 return m_watchpoint_list.Add(addr, size, watch_flags, hardware); 191 } 192 193 Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { 194 // Update the thread list 195 UpdateThreads(); 196 197 Status overall_error; 198 199 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 200 for (const auto &thread : m_threads) { 201 assert(thread && "thread list should not have a NULL thread!"); 202 203 const Status thread_error = thread->RemoveWatchpoint(addr); 204 if (thread_error.Fail()) { 205 // Keep track of the first thread error if any threads fail. We want to 206 // try to remove the watchpoint from every thread, though, even if one or 207 // more have errors. 208 if (!overall_error.Fail()) 209 overall_error = thread_error; 210 } 211 } 212 const Status error = m_watchpoint_list.Remove(addr); 213 return overall_error.Fail() ? overall_error : error; 214 } 215 216 const HardwareBreakpointMap & 217 NativeProcessProtocol::GetHardwareBreakpointMap() const { 218 return m_hw_breakpoints_map; 219 } 220 221 Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr, 222 size_t size) { 223 // This default implementation assumes setting a hardware breakpoint for this 224 // process will require setting same hardware breakpoint for each of its 225 // existing threads. New thread will do the same once created. 226 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 227 228 // Update the thread list 229 UpdateThreads(); 230 231 // Exit here if target does not have required hardware breakpoint capability. 232 auto hw_debug_cap = GetHardwareDebugSupportInfo(); 233 234 if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 || 235 hw_debug_cap->first <= m_hw_breakpoints_map.size()) 236 return Status("Target does not have required no of hardware breakpoints"); 237 238 // Vector below stores all thread pointer for which we have we successfully 239 // set this hardware breakpoint. If any of the current process threads fails 240 // to set this hardware breakpoint then roll back and remove this breakpoint 241 // for all the threads that had already set it successfully. 242 std::vector<NativeThreadProtocol *> breakpoint_established_threads; 243 244 // Request to set a hardware breakpoint for each of current process threads. 245 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 246 for (const auto &thread : m_threads) { 247 assert(thread && "thread list should not have a NULL thread!"); 248 249 Status thread_error = thread->SetHardwareBreakpoint(addr, size); 250 if (thread_error.Success()) { 251 // Remember that we set this breakpoint successfully in case we need to 252 // clear it later. 253 breakpoint_established_threads.push_back(thread.get()); 254 } else { 255 // Unset the breakpoint for each thread we successfully set so that we 256 // get back to a consistent state of "not set" for this hardware 257 // breakpoint. 258 for (auto rollback_thread_sp : breakpoint_established_threads) { 259 Status remove_error = 260 rollback_thread_sp->RemoveHardwareBreakpoint(addr); 261 if (remove_error.Fail()) 262 LLDB_LOG(log, 263 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}", 264 GetID(), rollback_thread_sp->GetID(), remove_error); 265 } 266 267 return thread_error; 268 } 269 } 270 271 // Register new hardware breakpoint into hardware breakpoints map of current 272 // process. 273 m_hw_breakpoints_map[addr] = {addr, size}; 274 275 return Status(); 276 } 277 278 Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) { 279 // Update the thread list 280 UpdateThreads(); 281 282 Status error; 283 284 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 285 for (const auto &thread : m_threads) { 286 assert(thread && "thread list should not have a NULL thread!"); 287 error = thread->RemoveHardwareBreakpoint(addr); 288 } 289 290 // Also remove from hardware breakpoint map of current process. 291 m_hw_breakpoints_map.erase(addr); 292 293 return error; 294 } 295 296 void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 297 lldb::StateType state) { 298 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 299 300 m_delegate.ProcessStateChanged(this, state); 301 302 LLDB_LOG(log, "sent state notification [{0}] from process {1}", state, 303 GetID()); 304 } 305 306 void NativeProcessProtocol::NotifyDidExec() { 307 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 308 LLDB_LOG(log, "process {0} exec()ed", GetID()); 309 310 m_delegate.DidExec(this); 311 } 312 313 Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 314 uint32_t size_hint) { 315 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 316 LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint); 317 318 auto it = m_software_breakpoints.find(addr); 319 if (it != m_software_breakpoints.end()) { 320 ++it->second.ref_count; 321 return Status(); 322 } 323 auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint); 324 if (!expected_bkpt) 325 return Status(expected_bkpt.takeError()); 326 327 m_software_breakpoints.emplace(addr, std::move(*expected_bkpt)); 328 return Status(); 329 } 330 331 Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) { 332 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 333 LLDB_LOG(log, "addr = {0:x}", addr); 334 auto it = m_software_breakpoints.find(addr); 335 if (it == m_software_breakpoints.end()) 336 return Status("Breakpoint not found."); 337 assert(it->second.ref_count > 0); 338 if (--it->second.ref_count > 0) 339 return Status(); 340 341 // This is the last reference. Let's remove the breakpoint. 342 Status error; 343 344 // Clear a software breakpoint instruction 345 llvm::SmallVector<uint8_t, 4> curr_break_op( 346 it->second.breakpoint_opcodes.size(), 0); 347 348 // Read the breakpoint opcode 349 size_t bytes_read = 0; 350 error = 351 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read); 352 if (error.Fail() || bytes_read < curr_break_op.size()) { 353 return Status("addr=0x%" PRIx64 354 ": tried to read %zu bytes but only read %zu", 355 addr, curr_break_op.size(), bytes_read); 356 } 357 const auto &saved = it->second.saved_opcodes; 358 // Make sure the breakpoint opcode exists at this address 359 if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) { 360 if (curr_break_op != it->second.saved_opcodes) 361 return Status("Original breakpoint trap is no longer in memory."); 362 LLDB_LOG(log, 363 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.", 364 llvm::make_range(saved.begin(), saved.end()), addr); 365 } else { 366 // We found a valid breakpoint opcode at this address, now restore the 367 // saved opcode. 368 size_t bytes_written = 0; 369 error = WriteMemory(addr, saved.data(), saved.size(), bytes_written); 370 if (error.Fail() || bytes_written < saved.size()) { 371 return Status("addr=0x%" PRIx64 372 ": tried to write %zu bytes but only wrote %zu", 373 addr, saved.size(), bytes_written); 374 } 375 376 // Verify that our original opcode made it back to the inferior 377 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0); 378 size_t verify_bytes_read = 0; 379 error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(), 380 verify_bytes_read); 381 if (error.Fail() || verify_bytes_read < verify_opcode.size()) { 382 return Status("addr=0x%" PRIx64 383 ": tried to read %zu verification bytes but only read %zu", 384 addr, verify_opcode.size(), verify_bytes_read); 385 } 386 if (verify_opcode != saved) 387 LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr, 388 llvm::make_range(saved.begin(), saved.end())); 389 } 390 391 m_software_breakpoints.erase(it); 392 return Status(); 393 } 394 395 llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint> 396 NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr, 397 uint32_t size_hint) { 398 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 399 400 auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint); 401 if (!expected_trap) 402 return expected_trap.takeError(); 403 404 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0); 405 // Save the original opcodes by reading them so we can restore later. 406 size_t bytes_read = 0; 407 Status error = ReadMemory(addr, saved_opcode_bytes.data(), 408 saved_opcode_bytes.size(), bytes_read); 409 if (error.Fail()) 410 return error.ToError(); 411 412 // Ensure we read as many bytes as we expected. 413 if (bytes_read != saved_opcode_bytes.size()) { 414 return llvm::createStringError( 415 llvm::inconvertibleErrorCode(), 416 "Failed to read memory while attempting to set breakpoint: attempted " 417 "to read {0} bytes but only read {1}.", 418 saved_opcode_bytes.size(), bytes_read); 419 } 420 421 LLDB_LOG( 422 log, "Overwriting bytes at {0:x}: {1:@[x]}", addr, 423 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end())); 424 425 // Write a software breakpoint in place of the original opcode. 426 size_t bytes_written = 0; 427 error = WriteMemory(addr, expected_trap->data(), expected_trap->size(), 428 bytes_written); 429 if (error.Fail()) 430 return error.ToError(); 431 432 // Ensure we wrote as many bytes as we expected. 433 if (bytes_written != expected_trap->size()) { 434 return llvm::createStringError( 435 llvm::inconvertibleErrorCode(), 436 "Failed write memory while attempting to set " 437 "breakpoint: attempted to write {0} bytes but only wrote {1}", 438 expected_trap->size(), bytes_written); 439 } 440 441 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(), 442 0); 443 size_t verify_bytes_read = 0; 444 error = ReadMemory(addr, verify_bp_opcode_bytes.data(), 445 verify_bp_opcode_bytes.size(), verify_bytes_read); 446 if (error.Fail()) 447 return error.ToError(); 448 449 // Ensure we read as many verification bytes as we expected. 450 if (verify_bytes_read != verify_bp_opcode_bytes.size()) { 451 return llvm::createStringError( 452 llvm::inconvertibleErrorCode(), 453 "Failed to read memory while " 454 "attempting to verify breakpoint: attempted to read {0} bytes " 455 "but only read {1}", 456 verify_bp_opcode_bytes.size(), verify_bytes_read); 457 } 458 459 if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) != 460 *expected_trap) { 461 return llvm::createStringError( 462 llvm::inconvertibleErrorCode(), 463 "Verification of software breakpoint " 464 "writing failed - trap opcodes not successfully read back " 465 "after writing when setting breakpoint at {0:x}", 466 addr); 467 } 468 469 LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr); 470 return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap}; 471 } 472 473 llvm::Expected<llvm::ArrayRef<uint8_t>> 474 NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 475 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 476 static const uint8_t g_i386_opcode[] = {0xCC}; 477 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 478 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 479 static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 480 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; // trap 481 static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 482 483 switch (GetArchitecture().GetMachine()) { 484 case llvm::Triple::aarch64: 485 case llvm::Triple::aarch64_32: 486 return llvm::makeArrayRef(g_aarch64_opcode); 487 488 case llvm::Triple::x86: 489 case llvm::Triple::x86_64: 490 return llvm::makeArrayRef(g_i386_opcode); 491 492 case llvm::Triple::mips: 493 case llvm::Triple::mips64: 494 return llvm::makeArrayRef(g_mips64_opcode); 495 496 case llvm::Triple::mipsel: 497 case llvm::Triple::mips64el: 498 return llvm::makeArrayRef(g_mips64el_opcode); 499 500 case llvm::Triple::systemz: 501 return llvm::makeArrayRef(g_s390x_opcode); 502 503 case llvm::Triple::ppc: 504 case llvm::Triple::ppc64: 505 return llvm::makeArrayRef(g_ppc_opcode); 506 507 case llvm::Triple::ppc64le: 508 return llvm::makeArrayRef(g_ppcle_opcode); 509 510 default: 511 return llvm::createStringError(llvm::inconvertibleErrorCode(), 512 "CPU type not supported!"); 513 } 514 } 515 516 size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() { 517 switch (GetArchitecture().GetMachine()) { 518 case llvm::Triple::x86: 519 case llvm::Triple::x86_64: 520 case llvm::Triple::systemz: 521 // These architectures report increment the PC after breakpoint is hit. 522 return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size(); 523 524 case llvm::Triple::arm: 525 case llvm::Triple::aarch64: 526 case llvm::Triple::aarch64_32: 527 case llvm::Triple::mips64: 528 case llvm::Triple::mips64el: 529 case llvm::Triple::mips: 530 case llvm::Triple::mipsel: 531 case llvm::Triple::ppc: 532 case llvm::Triple::ppc64: 533 case llvm::Triple::ppc64le: 534 // On these architectures the PC doesn't get updated for breakpoint hits. 535 return 0; 536 537 default: 538 llvm_unreachable("CPU type not supported!"); 539 } 540 } 541 542 void NativeProcessProtocol::FixupBreakpointPCAsNeeded( 543 NativeThreadProtocol &thread) { 544 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 545 546 Status error; 547 548 // Find out the size of a breakpoint (might depend on where we are in the 549 // code). 550 NativeRegisterContext &context = thread.GetRegisterContext(); 551 552 uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset(); 553 LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 554 if (breakpoint_size == 0) 555 return; 556 557 // First try probing for a breakpoint at a software breakpoint location: PC - 558 // breakpoint size. 559 const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 560 lldb::addr_t breakpoint_addr = initial_pc_addr; 561 // Do not allow breakpoint probe to wrap around. 562 if (breakpoint_addr >= breakpoint_size) 563 breakpoint_addr -= breakpoint_size; 564 565 if (m_software_breakpoints.count(breakpoint_addr) == 0) { 566 // We didn't find one at a software probe location. Nothing to do. 567 LLDB_LOG(log, 568 "pid {0} no lldb software breakpoint found at current pc with " 569 "adjustment: {1}", 570 GetID(), breakpoint_addr); 571 return; 572 } 573 574 // 575 // We have a software breakpoint and need to adjust the PC. 576 // 577 578 // Change the program counter. 579 LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 580 thread.GetID(), initial_pc_addr, breakpoint_addr); 581 582 error = context.SetPC(breakpoint_addr); 583 if (error.Fail()) { 584 // This can happen in case the process was killed between the time we read 585 // the PC and when we are updating it. There's nothing better to do than to 586 // swallow the error. 587 LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 588 thread.GetID(), error); 589 } 590 } 591 592 Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, 593 bool hardware) { 594 if (hardware) 595 return RemoveHardwareBreakpoint(addr); 596 else 597 return RemoveSoftwareBreakpoint(addr); 598 } 599 600 Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr, 601 void *buf, size_t size, 602 size_t &bytes_read) { 603 Status error = ReadMemory(addr, buf, size, bytes_read); 604 if (error.Fail()) 605 return error; 606 607 auto data = 608 llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read); 609 for (const auto &pair : m_software_breakpoints) { 610 lldb::addr_t bp_addr = pair.first; 611 auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes); 612 613 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr) 614 continue; // Breakpoint not in range, ignore 615 616 if (bp_addr < addr) { 617 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr); 618 bp_addr = addr; 619 } 620 auto bp_data = data.drop_front(bp_addr - addr); 621 std::copy_n(saved_opcodes.begin(), 622 std::min(saved_opcodes.size(), bp_data.size()), 623 bp_data.begin()); 624 } 625 return Status(); 626 } 627 628 llvm::Expected<llvm::StringRef> 629 NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer, 630 size_t max_size, 631 size_t &total_bytes_read) { 632 static const size_t cache_line_size = 633 llvm::sys::Process::getPageSizeEstimate(); 634 size_t bytes_read = 0; 635 size_t bytes_left = max_size; 636 addr_t curr_addr = addr; 637 size_t string_size; 638 char *curr_buffer = buffer; 639 total_bytes_read = 0; 640 Status status; 641 642 while (bytes_left > 0 && status.Success()) { 643 addr_t cache_line_bytes_left = 644 cache_line_size - (curr_addr % cache_line_size); 645 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); 646 status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer), 647 bytes_to_read, bytes_read); 648 649 if (bytes_read == 0) 650 break; 651 652 void *str_end = std::memchr(curr_buffer, '\0', bytes_read); 653 if (str_end != nullptr) { 654 total_bytes_read = 655 static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1)); 656 status.Clear(); 657 break; 658 } 659 660 total_bytes_read += bytes_read; 661 curr_buffer += bytes_read; 662 curr_addr += bytes_read; 663 bytes_left -= bytes_read; 664 } 665 666 string_size = total_bytes_read - 1; 667 668 // Make sure we return a null terminated string. 669 if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') { 670 buffer[max_size - 1] = '\0'; 671 total_bytes_read--; 672 } 673 674 if (!status.Success()) 675 return status.ToError(); 676 677 return llvm::StringRef(buffer, string_size); 678 } 679 680 lldb::StateType NativeProcessProtocol::GetState() const { 681 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 682 return m_state; 683 } 684 685 void NativeProcessProtocol::SetState(lldb::StateType state, 686 bool notify_delegates) { 687 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 688 689 if (state == m_state) 690 return; 691 692 m_state = state; 693 694 if (StateIsStoppedState(state, false)) { 695 ++m_stop_id; 696 697 // Give process a chance to do any stop id bump processing, such as 698 // clearing cached data that is invalidated each time the process runs. 699 // Note if/when we support some threads running, we'll end up needing to 700 // manage this per thread and per process. 701 DoStopIDBumped(m_stop_id); 702 } 703 704 // Optionally notify delegates of the state change. 705 if (notify_delegates) 706 SynchronouslyNotifyProcessStateChanged(state); 707 } 708 709 uint32_t NativeProcessProtocol::GetStopID() const { 710 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 711 return m_stop_id; 712 } 713 714 void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 715 // Default implementation does nothing. 716 } 717 718 NativeProcessProtocol::Factory::~Factory() = default; 719