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