1 //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===// 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 using namespace lldb; 20 using namespace lldb_private; 21 22 // NativeProcessProtocol Members 23 24 NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 25 NativeDelegate &delegate) 26 : m_pid(pid), m_terminal_fd(terminal_fd) { 27 bool registered = RegisterNativeDelegate(delegate); 28 assert(registered); 29 (void)registered; 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 bool NativeProcessProtocol::RegisterNativeDelegate( 297 NativeDelegate &native_delegate) { 298 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 299 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != 300 m_delegates.end()) 301 return false; 302 303 m_delegates.push_back(&native_delegate); 304 native_delegate.InitializeDelegate(this); 305 return true; 306 } 307 308 bool NativeProcessProtocol::UnregisterNativeDelegate( 309 NativeDelegate &native_delegate) { 310 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 311 312 const auto initial_size = m_delegates.size(); 313 m_delegates.erase( 314 remove(m_delegates.begin(), m_delegates.end(), &native_delegate), 315 m_delegates.end()); 316 317 // We removed the delegate if the count of delegates shrank after removing 318 // all copies of the given native_delegate from the vector. 319 return m_delegates.size() < initial_size; 320 } 321 322 void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 323 lldb::StateType state) { 324 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 325 326 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 327 for (auto native_delegate : m_delegates) 328 native_delegate->ProcessStateChanged(this, state); 329 330 if (log) { 331 if (!m_delegates.empty()) { 332 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] " 333 "from process %" PRIu64, 334 __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 335 } else { 336 log->Printf("NativeProcessProtocol::%s: would send state notification " 337 "[%s] from process %" PRIu64 ", but no delegates", 338 __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 339 } 340 } 341 } 342 343 void NativeProcessProtocol::NotifyDidExec() { 344 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 345 if (log) 346 log->Printf("NativeProcessProtocol::%s - preparing to call delegates", 347 __FUNCTION__); 348 349 { 350 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 351 for (auto native_delegate : m_delegates) 352 native_delegate->DidExec(this); 353 } 354 } 355 356 Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 357 uint32_t size_hint) { 358 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 359 LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint); 360 361 auto it = m_software_breakpoints.find(addr); 362 if (it != m_software_breakpoints.end()) { 363 ++it->second.ref_count; 364 return Status(); 365 } 366 auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint); 367 if (!expected_bkpt) 368 return Status(expected_bkpt.takeError()); 369 370 m_software_breakpoints.emplace(addr, std::move(*expected_bkpt)); 371 return Status(); 372 } 373 374 Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) { 375 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 376 LLDB_LOG(log, "addr = {0:x}", addr); 377 auto it = m_software_breakpoints.find(addr); 378 if (it == m_software_breakpoints.end()) 379 return Status("Breakpoint not found."); 380 assert(it->second.ref_count > 0); 381 if (--it->second.ref_count > 0) 382 return Status(); 383 384 // This is the last reference. Let's remove the breakpoint. 385 Status error; 386 387 // Clear a software breakpoint instruction 388 llvm::SmallVector<uint8_t, 4> curr_break_op( 389 it->second.breakpoint_opcodes.size(), 0); 390 391 // Read the breakpoint opcode 392 size_t bytes_read = 0; 393 error = 394 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read); 395 if (error.Fail() || bytes_read < curr_break_op.size()) { 396 return Status("addr=0x%" PRIx64 397 ": tried to read %zu bytes but only read %zu", 398 addr, curr_break_op.size(), bytes_read); 399 } 400 const auto &saved = it->second.saved_opcodes; 401 // Make sure the breakpoint opcode exists at this address 402 if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) { 403 if (curr_break_op != it->second.saved_opcodes) 404 return Status("Original breakpoint trap is no longer in memory."); 405 LLDB_LOG(log, 406 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.", 407 llvm::make_range(saved.begin(), saved.end()), addr); 408 } else { 409 // We found a valid breakpoint opcode at this address, now restore the 410 // saved opcode. 411 size_t bytes_written = 0; 412 error = WriteMemory(addr, saved.data(), saved.size(), bytes_written); 413 if (error.Fail() || bytes_written < saved.size()) { 414 return Status("addr=0x%" PRIx64 415 ": tried to write %zu bytes but only wrote %zu", 416 addr, saved.size(), bytes_written); 417 } 418 419 // Verify that our original opcode made it back to the inferior 420 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0); 421 size_t verify_bytes_read = 0; 422 error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(), 423 verify_bytes_read); 424 if (error.Fail() || verify_bytes_read < verify_opcode.size()) { 425 return Status("addr=0x%" PRIx64 426 ": tried to read %zu verification bytes but only read %zu", 427 addr, verify_opcode.size(), verify_bytes_read); 428 } 429 if (verify_opcode != saved) 430 LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr, 431 llvm::make_range(saved.begin(), saved.end())); 432 } 433 434 m_software_breakpoints.erase(it); 435 return Status(); 436 } 437 438 llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint> 439 NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr, 440 uint32_t size_hint) { 441 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 442 443 auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint); 444 if (!expected_trap) 445 return expected_trap.takeError(); 446 447 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0); 448 // Save the original opcodes by reading them so we can restore later. 449 size_t bytes_read = 0; 450 Status error = ReadMemory(addr, saved_opcode_bytes.data(), 451 saved_opcode_bytes.size(), bytes_read); 452 if (error.Fail()) 453 return error.ToError(); 454 455 // Ensure we read as many bytes as we expected. 456 if (bytes_read != saved_opcode_bytes.size()) { 457 return llvm::createStringError( 458 llvm::inconvertibleErrorCode(), 459 "Failed to read memory while attempting to set breakpoint: attempted " 460 "to read {0} bytes but only read {1}.", 461 saved_opcode_bytes.size(), bytes_read); 462 } 463 464 LLDB_LOG( 465 log, "Overwriting bytes at {0:x}: {1:@[x]}", addr, 466 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end())); 467 468 // Write a software breakpoint in place of the original opcode. 469 size_t bytes_written = 0; 470 error = WriteMemory(addr, expected_trap->data(), expected_trap->size(), 471 bytes_written); 472 if (error.Fail()) 473 return error.ToError(); 474 475 // Ensure we wrote as many bytes as we expected. 476 if (bytes_written != expected_trap->size()) { 477 return llvm::createStringError( 478 llvm::inconvertibleErrorCode(), 479 "Failed write memory while attempting to set " 480 "breakpoint: attempted to write {0} bytes but only wrote {1}", 481 expected_trap->size(), bytes_written); 482 } 483 484 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(), 485 0); 486 size_t verify_bytes_read = 0; 487 error = ReadMemory(addr, verify_bp_opcode_bytes.data(), 488 verify_bp_opcode_bytes.size(), verify_bytes_read); 489 if (error.Fail()) 490 return error.ToError(); 491 492 // Ensure we read as many verification bytes as we expected. 493 if (verify_bytes_read != verify_bp_opcode_bytes.size()) { 494 return llvm::createStringError( 495 llvm::inconvertibleErrorCode(), 496 "Failed to read memory while " 497 "attempting to verify breakpoint: attempted to read {0} bytes " 498 "but only read {1}", 499 verify_bp_opcode_bytes.size(), verify_bytes_read); 500 } 501 502 if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) != 503 *expected_trap) { 504 return llvm::createStringError( 505 llvm::inconvertibleErrorCode(), 506 "Verification of software breakpoint " 507 "writing failed - trap opcodes not successfully read back " 508 "after writing when setting breakpoint at {0:x}", 509 addr); 510 } 511 512 LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr); 513 return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap}; 514 } 515 516 llvm::Expected<llvm::ArrayRef<uint8_t>> 517 NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 518 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 519 static const uint8_t g_i386_opcode[] = {0xCC}; 520 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 521 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 522 static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 523 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 524 525 switch (GetArchitecture().GetMachine()) { 526 case llvm::Triple::aarch64: 527 return llvm::makeArrayRef(g_aarch64_opcode); 528 529 case llvm::Triple::x86: 530 case llvm::Triple::x86_64: 531 return llvm::makeArrayRef(g_i386_opcode); 532 533 case llvm::Triple::mips: 534 case llvm::Triple::mips64: 535 return llvm::makeArrayRef(g_mips64_opcode); 536 537 case llvm::Triple::mipsel: 538 case llvm::Triple::mips64el: 539 return llvm::makeArrayRef(g_mips64el_opcode); 540 541 case llvm::Triple::systemz: 542 return llvm::makeArrayRef(g_s390x_opcode); 543 544 case llvm::Triple::ppc64le: 545 return llvm::makeArrayRef(g_ppc64le_opcode); 546 547 default: 548 return llvm::createStringError(llvm::inconvertibleErrorCode(), 549 "CPU type not supported!"); 550 } 551 } 552 553 size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() { 554 switch (GetArchitecture().GetMachine()) { 555 case llvm::Triple::x86: 556 case llvm::Triple::x86_64: 557 case llvm::Triple::systemz: 558 // These architectures report increment the PC after breakpoint is hit. 559 return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size(); 560 561 case llvm::Triple::arm: 562 case llvm::Triple::aarch64: 563 case llvm::Triple::mips64: 564 case llvm::Triple::mips64el: 565 case llvm::Triple::mips: 566 case llvm::Triple::mipsel: 567 case llvm::Triple::ppc64le: 568 // On these architectures the PC doesn't get updated for breakpoint hits. 569 return 0; 570 571 default: 572 llvm_unreachable("CPU type not supported!"); 573 } 574 } 575 576 void NativeProcessProtocol::FixupBreakpointPCAsNeeded( 577 NativeThreadProtocol &thread) { 578 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 579 580 Status error; 581 582 // Find out the size of a breakpoint (might depend on where we are in the 583 // code). 584 NativeRegisterContext &context = thread.GetRegisterContext(); 585 586 uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset(); 587 LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 588 if (breakpoint_size == 0) 589 return; 590 591 // First try probing for a breakpoint at a software breakpoint location: PC - 592 // breakpoint size. 593 const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 594 lldb::addr_t breakpoint_addr = initial_pc_addr; 595 // Do not allow breakpoint probe to wrap around. 596 if (breakpoint_addr >= breakpoint_size) 597 breakpoint_addr -= breakpoint_size; 598 599 if (m_software_breakpoints.count(breakpoint_addr) == 0) { 600 // We didn't find one at a software probe location. Nothing to do. 601 LLDB_LOG(log, 602 "pid {0} no lldb software breakpoint found at current pc with " 603 "adjustment: {1}", 604 GetID(), breakpoint_addr); 605 return; 606 } 607 608 // 609 // We have a software breakpoint and need to adjust the PC. 610 // 611 612 // Change the program counter. 613 LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 614 thread.GetID(), initial_pc_addr, breakpoint_addr); 615 616 error = context.SetPC(breakpoint_addr); 617 if (error.Fail()) { 618 // This can happen in case the process was killed between the time we read 619 // the PC and when we are updating it. There's nothing better to do than to 620 // swallow the error. 621 LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 622 thread.GetID(), error); 623 } 624 } 625 626 Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, 627 bool hardware) { 628 if (hardware) 629 return RemoveHardwareBreakpoint(addr); 630 else 631 return RemoveSoftwareBreakpoint(addr); 632 } 633 634 Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr, 635 void *buf, size_t size, 636 size_t &bytes_read) { 637 Status error = ReadMemory(addr, buf, size, bytes_read); 638 if (error.Fail()) 639 return error; 640 641 auto data = 642 llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read); 643 for (const auto &pair : m_software_breakpoints) { 644 lldb::addr_t bp_addr = pair.first; 645 auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes); 646 647 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr) 648 continue; // Breapoint not in range, ignore 649 650 if (bp_addr < addr) { 651 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr); 652 bp_addr = addr; 653 } 654 auto bp_data = data.drop_front(bp_addr - addr); 655 std::copy_n(saved_opcodes.begin(), 656 std::min(saved_opcodes.size(), bp_data.size()), 657 bp_data.begin()); 658 } 659 return Status(); 660 } 661 662 lldb::StateType NativeProcessProtocol::GetState() const { 663 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 664 return m_state; 665 } 666 667 void NativeProcessProtocol::SetState(lldb::StateType state, 668 bool notify_delegates) { 669 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 670 671 if (state == m_state) 672 return; 673 674 m_state = state; 675 676 if (StateIsStoppedState(state, false)) { 677 ++m_stop_id; 678 679 // Give process a chance to do any stop id bump processing, such as 680 // clearing cached data that is invalidated each time the process runs. 681 // Note if/when we support some threads running, we'll end up needing to 682 // manage this per thread and per process. 683 DoStopIDBumped(m_stop_id); 684 } 685 686 // Optionally notify delegates of the state change. 687 if (notify_delegates) 688 SynchronouslyNotifyProcessStateChanged(state); 689 } 690 691 uint32_t NativeProcessProtocol::GetStopID() const { 692 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 693 return m_stop_id; 694 } 695 696 void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 697 // Default implementation does nothing. 698 } 699 700 NativeProcessProtocol::Factory::~Factory() = default; 701