[Lldb-commits] [lldb] [lldb] Add native process support for OpenBSD (PR #235641)

via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 26 00:58:22 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Brad Smith (brad0)

<details>
<summary>Changes</summary>

Add a NativeProcessOpenBSD plugin so that lldb-server can launch,
attach to and debug processes on OpenBSD, and allow process debugging
in PlatformOpenBSD.

Supported architectures are aarch64, i386 and x86_64. The plugin
provides:

- Launch, attach and detach. Attaching to a process that is not a
  child of the debugger requires root or kern.global_ptrace=1.
- Software breakpoints, instruction stepping and step-out.
- Memory reads and writes via PT_IO, and auxv via PIOD_READ_AUXV.
- Registers: GPRs, FP/SIMD, the PAC masks and tpidr on aarch64; GPRs,
  x87/SSE (FXSAVE) and AVX (XSAVE) on x86_64; GPRs and x87/SSE on
  i386. Registers are accessed by their offset in the register info,
  and the aarch64 cpsr/fpsr/fpcr fields are detected from AT_HWCAP.
- Multi-threaded processes. OpenBSD does not report thread creation
  or exit, so the thread list is refreshed on every stop. The thread
  that stopped is taken from PT_GET_PROCESS_STATE, and register
  accesses and single-steps are addressed by thread ID.
- The shared library list, provided by deriving from
  NativeProcessELF (qXfer:libraries-svr4), which locates r_debug
  through DT_DEBUG. Module file and load address lookups use the
  same list, since OpenBSD's memory map has no file names.
- Memory region information from KERN_PROC_VMMAP, which is
  root-only.
- Following fork() (fork-events and multiprocess), using PTRACE_FORK
  when the client enables it.
- Expression evaluation.

Also teach ABISysV_arm64 to read the PAC code/data masks on OpenBSD,
so that return addresses signed with pointer authentication are
stripped when unwinding.

Add RegisterInfoOpenBSDTest, which checks the register infos against
the layout of OpenBSD's ptrace(2) register structures.

Limitations, due to what OpenBSD's ptrace(2) provides:

- There is no siginfo, so crash stops report only the signal.
- There are no exec, vfork or thread events.
- Individual threads cannot be suspended. lldb can resume either all
  threads, or only the thread that reported the last stop.
- There are no hardware breakpoints or watchpoints.

Tested on OpenBSD/aarch64, OpenBSD/amd64 and OpenBSD/i386: launch,
attach and detach, breakpoints, stepping, finish, register reads and
writes, expression evaluation, shared library loading, memory
regions, following fork in parent and child mode, and a
multi-threaded program. With this change, check-lldb-unit passes on
all three.

---

Patch is 108.14 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/235641.diff


19 Files Affected:

- (modified) lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp (+29) 
- (modified) lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp (+7-3) 
- (modified) lldb/source/Plugins/Process/CMakeLists.txt (+1) 
- (added) lldb/source/Plugins/Process/OpenBSD/CMakeLists.txt (+18) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeProcessOpenBSD.cpp (+1001) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeProcessOpenBSD.h (+131) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD.cpp (+98) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD.h (+62) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_arch.cpp (+29) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_arm64.cpp (+315) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_arm64.h (+91) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_x86.cpp (+505) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_x86.h (+96) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeThreadOpenBSD.cpp (+151) 
- (added) lldb/source/Plugins/Process/OpenBSD/NativeThreadOpenBSD.h (+72) 
- (modified) lldb/tools/lldb-server/CMakeLists.txt (+4) 
- (modified) lldb/tools/lldb-server/lldb-gdbserver.cpp (+4) 
- (modified) lldb/unittests/Process/Utility/CMakeLists.txt (+6-1) 
- (added) lldb/unittests/Process/Utility/RegisterInfoOpenBSDTest.cpp (+244) 


``````````diff
diff --git a/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp b/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp
index b298ec1564bd86..0287b599c11445 100644
--- a/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp
+++ b/lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp
@@ -834,6 +834,25 @@ static lldb::addr_t ReadLinuxProcessAddressMask(lldb::ProcessSP process_sp,
   return address_mask;
 }
 
+// Reads code or data address mask for the current OpenBSD process.
+static lldb::addr_t ReadOpenBSDProcessAddressMask(lldb::ProcessSP process_sp,
+                                                  llvm::StringRef reg_name) {
+  // LLDB_INVALID_ADDRESS_MASK means the mask has not been read yet; return it
+  // until there is a thread to read the PAC mask register from, so the read is
+  // retried rather than locking in an empty mask.
+  lldb::ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread();
+  if (!thread_sp)
+    return LLDB_INVALID_ADDRESS_MASK;
+  lldb::RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
+  if (!reg_ctx_sp)
+    return LLDB_INVALID_ADDRESS_MASK;
+  const RegisterInfo *reg_info = reg_ctx_sp->GetRegisterInfoByName(reg_name, 0);
+  if (!reg_info)
+    return LLDB_INVALID_ADDRESS_MASK;
+  return reg_ctx_sp->ReadRegisterAsUnsigned(reg_info->kinds[eRegisterKindLLDB],
+                                            LLDB_INVALID_ADDRESS_MASK);
+}
+
 lldb::addr_t ABISysV_arm64::FixCodeAddress(lldb::addr_t pc) {
   if (lldb::ProcessSP process_sp = GetProcessSP()) {
     if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSLinux() &&
@@ -841,6 +860,11 @@ lldb::addr_t ABISysV_arm64::FixCodeAddress(lldb::addr_t pc) {
       process_sp->SetCodeAddressMask(
           ReadLinuxProcessAddressMask(process_sp, "code_mask"));
 
+    if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSOpenBSD() &&
+        process_sp->GetCodeAddressMask() == LLDB_INVALID_ADDRESS_MASK)
+      process_sp->SetCodeAddressMask(
+          ReadOpenBSDProcessAddressMask(process_sp, "code_mask"));
+
     // b55 is the highest bit outside TBI (if it's enabled), use
     // it to determine if the high bits are set to 0 or 1.
     const addr_t pac_sign_extension = 0x0080000000000000ULL;
@@ -862,6 +886,11 @@ lldb::addr_t ABISysV_arm64::FixDataAddress(lldb::addr_t pc) {
       process_sp->SetDataAddressMask(
           ReadLinuxProcessAddressMask(process_sp, "data_mask"));
 
+    if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSOpenBSD() &&
+        process_sp->GetDataAddressMask() == LLDB_INVALID_ADDRESS_MASK)
+      process_sp->SetDataAddressMask(
+          ReadOpenBSDProcessAddressMask(process_sp, "data_mask"));
+
     // b55 is the highest bit outside TBI (if it's enabled), use
     // it to determine if the high bits are set to 0 or 1.
     const addr_t pac_sign_extension = 0x0080000000000000ULL;
diff --git a/lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp b/lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp
index e5c30adc056158..7f4c38c0a918a5 100644
--- a/lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp
+++ b/lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp
@@ -79,7 +79,7 @@ llvm::StringRef PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) {
 }
 
 void PlatformOpenBSD::Initialize() {
-  Platform::Initialize();
+  PlatformPOSIX::Initialize();
 
   if (g_initialize_count++ == 0) {
 #if defined(__OpenBSD__)
@@ -145,9 +145,13 @@ void PlatformOpenBSD::GetStatus(Stream &strm) {
 #endif
 }
 
-// OpenBSD processes cannot yet be launched by spawning and attaching.
 bool PlatformOpenBSD::CanDebugProcess() {
-  return false;
+  if (IsHost()) {
+    return true;
+  } else {
+    // If we're connected, we can debug.
+    return IsConnected();
+  }
 }
 
 void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() {
diff --git a/lldb/source/Plugins/Process/CMakeLists.txt b/lldb/source/Plugins/Process/CMakeLists.txt
index 84cc65e45157e4..8a8f813b4870d3 100644
--- a/lldb/source/Plugins/Process/CMakeLists.txt
+++ b/lldb/source/Plugins/Process/CMakeLists.txt
@@ -16,6 +16,7 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "AIX")
   add_subdirectory(AIX)
   add_subdirectory(POSIX)
 elseif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+  add_subdirectory(OpenBSD)
   add_subdirectory(POSIX)
 elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
   add_subdirectory(Windows/Common)
diff --git a/lldb/source/Plugins/Process/OpenBSD/CMakeLists.txt b/lldb/source/Plugins/Process/OpenBSD/CMakeLists.txt
new file mode 100644
index 00000000000000..bf9fe08a3d93c7
--- /dev/null
+++ b/lldb/source/Plugins/Process/OpenBSD/CMakeLists.txt
@@ -0,0 +1,18 @@
+add_lldb_library(lldbPluginProcessOpenBSD
+  NativeProcessOpenBSD.cpp
+  NativeRegisterContextOpenBSD.cpp
+  NativeRegisterContextOpenBSD_arch.cpp
+  NativeRegisterContextOpenBSD_arm64.cpp
+  NativeRegisterContextOpenBSD_x86.cpp
+  NativeThreadOpenBSD.cpp
+
+  LINK_COMPONENTS
+    Support
+  LINK_LIBS
+    lldbHost
+    lldbSymbol
+    lldbTarget
+    lldbUtility
+    lldbPluginProcessPOSIX
+    lldbPluginProcessUtility
+  )
diff --git a/lldb/source/Plugins/Process/OpenBSD/NativeProcessOpenBSD.cpp b/lldb/source/Plugins/Process/OpenBSD/NativeProcessOpenBSD.cpp
new file mode 100644
index 00000000000000..e594397de663ec
--- /dev/null
+++ b/lldb/source/Plugins/Process/OpenBSD/NativeProcessOpenBSD.cpp
@@ -0,0 +1,1001 @@
+//===-- NativeProcessOpenBSD.cpp ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "NativeProcessOpenBSD.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#define _DYN_LOADER
+#include <elf.h>
+#include <util.h>
+
+#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostProcess.h"
+#include "lldb/Host/common/NativeRegisterContext.h"
+#include "lldb/Host/posix/ProcessLauncherPosixFork.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Utility/State.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Errno.h"
+
+#include <cstring>
+
+// System includes - They have to be included after framework includes because
+// they define some macros which collide with variable names in other modules
+// clang-format off
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <unistd.h>
+// clang-format on
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::process_openbsd;
+using namespace llvm;
+
+// Simple helper function to ensure flags are enabled on the given file
+// descriptor.
+static Status EnsureFDFlags(int fd, int flags) {
+  int status = fcntl(fd, F_GETFL);
+  if (status == -1)
+    return Status::FromErrno();
+
+  if (fcntl(fd, F_SETFL, status | flags) == -1)
+    return Status::FromErrno();
+
+  return Status();
+}
+
+// Register contexts are only implemented for these architectures.
+static llvm::Error CheckArchitectureSupported() {
+#if defined(__aarch64__) || defined(__i386__) || defined(__x86_64__)
+  return llvm::Error::success();
+#else
+  return llvm::createStringError(
+      "debugging is not supported on this architecture");
+#endif
+}
+
+// Whether kern.global_ptrace allows attaching to processes that are not
+// children of the debugger.
+static bool IsGlobalPtraceEnabled() {
+#ifdef KERN_GLOBAL_PTRACE
+  int mib[2] = {CTL_KERN, KERN_GLOBAL_PTRACE};
+  int value = 0;
+  size_t len = sizeof(value);
+  return ::sysctl(mib, 2, &value, &len, nullptr, 0) == 0 && value != 0;
+#else
+  return false;
+#endif
+}
+
+// Public Static Methods
+
+llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
+NativeProcessOpenBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
+                                      NativeDelegate &native_delegate) {
+  Log *log = GetLog(POSIXLog::Process);
+
+  if (llvm::Error error = CheckArchitectureSupported())
+    return std::move(error);
+
+  Status status;
+  ::pid_t pid = ProcessLauncherPosixFork()
+                    .LaunchProcess(launch_info, status)
+                    .GetProcessId();
+  LLDB_LOG(log, "pid = {0:x}", pid);
+  if (status.Fail()) {
+    LLDB_LOG(log, "failed to launch process: {0}", status);
+    return status.ToError();
+  }
+
+  // Wait for the child process to trap on its call to execve.
+  int wstatus;
+  ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
+  assert(wpid == pid);
+  UNUSED_IF_ASSERT_DISABLED(wpid);
+  if (!WIFSTOPPED(wstatus)) {
+    LLDB_LOG(log, "Could not sync with inferior process: wstatus={0}",
+             WaitStatus::Decode(wstatus));
+    return llvm::createStringError("could not sync with inferior process");
+  }
+  LLDB_LOG(log, "inferior started, now in stopped state");
+
+  ProcessInstanceInfo Info;
+  if (!Host::GetProcessInfo(pid, Info)) {
+    return llvm::createStringError("cannot get process architecture");
+  }
+
+  // Set the architecture to the exe architecture.
+  LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
+           Info.GetArchitecture().GetArchitectureName());
+
+  std::unique_ptr<NativeProcessOpenBSD> process_up(new NativeProcessOpenBSD(
+      pid, launch_info.GetPTY().ReleasePrimaryFileDescriptor(), native_delegate,
+      Info.GetArchitecture(), m_mainloop));
+
+  status = process_up->RefreshThreads();
+  if (status.Fail())
+    return status.ToError();
+
+  process_up->m_trapped_tid = process_up->GetTrappedThreadID();
+  for (const auto &thread : process_up->m_threads)
+    static_cast<NativeThreadOpenBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
+  process_up->SetState(StateType::eStateStopped, false);
+
+  return std::move(process_up);
+}
+
+llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
+NativeProcessOpenBSD::Manager::Attach(
+    lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate) {
+
+  Log *log = GetLog(POSIXLog::Process);
+  LLDB_LOG(log, "pid = {0:x}", pid);
+
+  if (llvm::Error error = CheckArchitectureSupported())
+    return std::move(error);
+
+  // Retrieve the architecture for the running process.
+  ProcessInstanceInfo Info;
+  if (!Host::GetProcessInfo(pid, Info)) {
+    return llvm::createStringError("cannot get process architecture");
+  }
+
+  std::unique_ptr<NativeProcessOpenBSD> process_up(new NativeProcessOpenBSD(
+      pid, -1, native_delegate, Info.GetArchitecture(), m_mainloop));
+
+  Status status = process_up->Attach();
+  if (!status.Success())
+    return status.ToError();
+
+  return std::move(process_up);
+}
+
+NativeProcessOpenBSD::Extension
+NativeProcessOpenBSD::Manager::GetSupportedExtensions() const {
+  Extension supported =
+      Extension::pass_signals | Extension::auxv | Extension::libraries_svr4;
+#ifdef PTRACE_FORK
+  // OpenBSD reports fork() but not vfork().
+  supported |= Extension::multiprocess | Extension::fork;
+#endif
+  return supported;
+}
+
+// Public Instance Methods
+
+NativeProcessOpenBSD::NativeProcessOpenBSD(::pid_t pid, int terminal_fd,
+                                           NativeDelegate &delegate,
+                                           const ArchSpec &arch,
+                                           MainLoop &mainloop)
+    : NativeProcessELF(pid, terminal_fd, delegate), m_arch(arch),
+      m_main_loop(mainloop) {
+  if (m_terminal_fd != -1) {
+    Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
+    assert(status.Success());
+  }
+
+  Status status;
+  m_sigchld_handle = mainloop.RegisterSignal(
+      SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
+  assert(m_sigchld_handle && status.Success());
+}
+
+// Handles all waitpid events from the inferior process.
+void NativeProcessOpenBSD::MonitorCallback(lldb::pid_t pid, int signal) {
+  // OpenBSD does not report thread creation or exit, so resynchronize the
+  // thread list every time the process stops.
+  Status error = RefreshThreads();
+  if (error.Fail()) {
+    Log *log = GetLog(POSIXLog::Thread);
+    LLDB_LOG(log, "failed to refresh threads for pid {0}: {1}", pid, error);
+  }
+
+  switch (signal) {
+  case SIGTRAP:
+    return MonitorSIGTRAP(pid);
+  default:
+    return MonitorSignal(pid, signal);
+  }
+}
+
+void NativeProcessOpenBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
+  Log *log = GetLog(POSIXLog::Process);
+
+  LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
+
+  /* Stop Tracking All Threads attached to Process */
+  m_threads.clear();
+
+  SetExitStatus(status, true);
+
+  // Notify delegate that our process has exited.
+  SetState(StateType::eStateExited, true);
+}
+
+void NativeProcessOpenBSD::MonitorSIGTRAP(lldb::pid_t pid) {
+  // OpenBSD reports a plain SIGTRAP for both a completed PT_STEP and a
+  // software breakpoint, so use what the last resume requested to tell them
+  // apart: it is a step only if the thread that trapped is the one that was
+  // being stepped.
+  m_trapped_tid = GetTrappedThreadID();
+
+  if (::pid_t child_pid = GetForkedChildPID()) {
+    m_step_tid = LLDB_INVALID_THREAD_ID;
+    auto *parent_thread =
+        static_cast<NativeThreadOpenBSD *>(GetThreadByID(m_trapped_tid));
+    if (parent_thread)
+      return MonitorFork(child_pid, *parent_thread);
+    LLDB_LOG(GetLog(POSIXLog::Process),
+             "fork reported by pid {0} without a trapped thread", GetID());
+  }
+
+  const bool stepped =
+      m_step_tid != LLDB_INVALID_THREAD_ID && m_step_tid == m_trapped_tid;
+  m_step_tid = LLDB_INVALID_THREAD_ID;
+
+  for (const auto &thread : m_threads) {
+    auto &t = static_cast<NativeThreadOpenBSD &>(*thread);
+    if (t.GetID() != m_trapped_tid) {
+      t.SetStoppedWithNoReason();
+    } else if (stepped) {
+      t.SetStoppedByTrace();
+    } else {
+      t.SetStoppedByBreakpoint();
+      FixupBreakpointPCAsNeeded(t);
+    }
+  }
+  if (m_trapped_tid != LLDB_INVALID_THREAD_ID)
+    SetCurrentThreadID(m_trapped_tid);
+  SetState(StateType::eStateStopped, true);
+}
+
+void NativeProcessOpenBSD::MonitorSignal(lldb::pid_t pid, int signal) {
+  m_trapped_tid = GetTrappedThreadID();
+  m_step_tid = LLDB_INVALID_THREAD_ID;
+
+  for (const auto &thread : m_threads) {
+    auto &t = static_cast<NativeThreadOpenBSD &>(*thread);
+    if (t.GetID() == m_trapped_tid)
+      t.SetStoppedBySignal(signal);
+    else
+      t.SetStoppedWithNoReason();
+  }
+  if (m_trapped_tid != LLDB_INVALID_THREAD_ID)
+    SetCurrentThreadID(m_trapped_tid);
+  SetState(StateType::eStateStopped, true);
+}
+
+void NativeProcessOpenBSD::MonitorFork(::pid_t child_pid,
+                                       NativeThreadOpenBSD &parent_thread) {
+  Log *log = GetLog(POSIXLog::Process);
+  LLDB_LOG(log, "fork, child_pid={0}", child_pid);
+
+  auto resume_parent = [&]() {
+    Status error =
+        PtraceWrapper(PT_CONTINUE, GetID(), reinterpret_cast<void *>(1), 0);
+    if (error.Fail()) {
+      LLDB_LOG(log, "unable to resume parent process {0}: {1}", GetID(), error);
+      SetState(StateType::eStateInvalid);
+    }
+  };
+
+  // The child is traced by us from the start, and stops with a SIGTRAP as
+  // soon as it runs.
+  int status;
+  ::pid_t wait_pid =
+      llvm::sys::RetryAfterSignal(-1, ::waitpid, child_pid, &status, 0);
+  if (wait_pid != child_pid || !WIFSTOPPED(status)) {
+    LLDB_LOG(log, "fork child {0} did not stop, not tracking it", child_pid);
+    return resume_parent();
+  }
+
+  std::unique_ptr<NativeProcessOpenBSD> child_process{new NativeProcessOpenBSD(
+      child_pid, m_terminal_fd, m_delegate, m_arch, m_main_loop)};
+  // The child's ptrace state names this process until the child forks.
+  child_process->m_last_fork_pid = GetID();
+  // The child has a copy of our memory, breakpoints included.
+  child_process->m_software_breakpoints = m_software_breakpoints;
+
+  Status error = child_process->RefreshThreads();
+  if (error.Fail() || child_process->m_threads.empty() ||
+      !bool(m_enabled_extensions & Extension::fork)) {
+    LLDB_LOG(log, "not following fork child {0}: {1}", child_pid, error);
+    for (const auto &[addr, bp] : m_software_breakpoints) {
+      size_t bytes_written;
+      child_process->DoWriteMemory(addr, bp.saved_opcodes.data(),
+                                   bp.saved_opcodes.size(), bytes_written);
+    }
+    child_process->Detach();
+    return resume_parent();
+  }
+
+  child_process->SetEnabledExtensions(m_enabled_extensions);
+  child_process->m_trapped_tid = child_process->GetTrappedThreadID();
+  const lldb::tid_t child_tid = child_process->m_trapped_tid;
+  for (const auto &thread : child_process->m_threads)
+    static_cast<NativeThreadOpenBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
+  child_process->SetState(StateType::eStateStopped, false);
+
+  m_delegate.NewSubprocess(this, std::move(child_process));
+
+  for (const auto &thread : m_threads) {
+    auto &t = static_cast<NativeThreadOpenBSD &>(*thread);
+    if (&t == &parent_thread)
+      t.SetStoppedByFork(child_pid, child_tid);
+    else
+      t.SetStoppedWithNoReason();
+  }
+  SetCurrentThreadID(parent_thread.GetID());
+  SetState(StateType::eStateStopped, true);
+}
+
+Status NativeProcessOpenBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
+                                           int data, int *result) {
+  Log *log = GetLog(POSIXLog::Ptrace);
+  Status error;
+  int ret;
+
+  errno = 0;
+  ret =
+      ptrace(req, static_cast<::pid_t>(pid), static_cast<caddr_t>(addr), data);
+
+  if (ret == -1)
+    error = Status::FromErrno();
+
+  if (result)
+    *result = ret;
+
+  LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
+
+  if (error.Fail())
+    LLDB_LOG(log, "ptrace() failed: {0}", error);
+
+  return error;
+}
+
+Status NativeProcessOpenBSD::Resume(const ResumeActionList &resume_actions) {
+  Log *log = GetLog(POSIXLog::Process);
+  LLDB_LOG(log, "pid {0}", GetID());
+
+  // OpenBSD's ptrace(2) can resume either the whole process or, when given a
+  // thread ID, only the thread that reported the last stop (the others stay
+  // suspended).  It cannot suspend individual threads, so map lldb's
+  // per-thread actions onto those two modes.
+  NativeThreadOpenBSD *step_thread = nullptr;
+  std::vector<NativeThreadOpenBSD *> run_threads;
+  bool any_suspended = false;
+  int signal = 0;
+
+  for (const auto &abs_thread : m_threads) {
+    auto &thread = static_cast<NativeThreadOpenBSD &>(*abs_thread);
+    const ResumeAction *action =
+        resume_actions.GetActionForThread(thread.GetID(), true);
+    if (action == nullptr) {
+      any_suspended = true;
+      continue;
+    }
+
+    switch (action->state) {
+    case eStateRunning:
+      run_threads.push_back(&thread);
+      break;
+    case eStateStepping:
+      if (step_thread)
+        return Status::FromErrorString(
+            "OpenBSD can only single-step one thread at a time");
+      step_thread = &thread;
+      break;
+    case eStateSuspended:
+    case eStateStopped:
+      any_suspended = true;
+      break;
+    default:
+      return Status::FromErrorStringWithFormat(
+          "NativeProcessOpenBSD::%s (): unexpected state %s specified "
+          "for pid %" PRIu64 ", tid %" PRIu64,
+          __FUNCTION__, StateAsCString(action->state), GetID(), thread.GetID());
+    }
+
+    if (action->signal != LLDB_INVALID_SIGNAL_NUMBER)
+      signal = action->signal;
+  }
+
+  lldb::pid_t target = GetID();
+  int req;
+  if (step_thread) {
+#ifdef PT_STEP
+    if (step_thread->GetID() != m_trapped_tid)
+      return Status::FromErrorStringWithFormat(
+          "OpenBSD can only single-step the thread that reported the last "
+          "stop (tid %" PRIu64 ")",
+          m_trapped_tid);
+    req = PT_STEP;
+    // With the thread ID only the stepping thread runs, which is what lldb
+    // wants when the other threads are suspended (e.g. stepping over a
+    // breakpoint).  With the process ID the other threads run as well.
+    if (run_threads.empty())
+      target = step_thread->GetID();
+#else
+    return Status::FromErrorString(
+        "NativeProcessOpenBSD stepping not supported on this platform");
+#endif
+  } else if (run_threads.empty()) {
+    LLDB_LOG(log, "no threads to resume for pid {0}", GetID());
+    return Status();
+  } else {
+    req = PT_CONTIN...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/235641


More information about the lldb-commits mailing list