11a3d19ddSKamil Rytarowski //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===//
21a3d19ddSKamil Rytarowski //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61a3d19ddSKamil Rytarowski //
71a3d19ddSKamil Rytarowski //===----------------------------------------------------------------------===//
81a3d19ddSKamil Rytarowski 
91a3d19ddSKamil Rytarowski #include "NativeProcessNetBSD.h"
101a3d19ddSKamil Rytarowski 
111a3d19ddSKamil Rytarowski 
121a3d19ddSKamil Rytarowski 
131a3d19ddSKamil Rytarowski #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
14f07a9995SKamil Rytarowski #include "lldb/Host/HostProcess.h"
15f07a9995SKamil Rytarowski #include "lldb/Host/common/NativeRegisterContext.h"
16f07a9995SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
17f07a9995SKamil Rytarowski #include "lldb/Target/Process.h"
18d821c997SPavel Labath #include "lldb/Utility/State.h"
19c1a6b128SPavel Labath #include "llvm/Support/Errno.h"
201a3d19ddSKamil Rytarowski 
211a3d19ddSKamil Rytarowski // System includes - They have to be included after framework includes because
2205097246SAdrian Prantl // they define some macros which collide with variable names in other modules
23f07a9995SKamil Rytarowski // clang-format off
24f07a9995SKamil Rytarowski #include <sys/types.h>
25f07a9995SKamil Rytarowski #include <sys/ptrace.h>
26f07a9995SKamil Rytarowski #include <sys/sysctl.h>
27f07a9995SKamil Rytarowski #include <sys/wait.h>
28f07a9995SKamil Rytarowski #include <uvm/uvm_prot.h>
29f07a9995SKamil Rytarowski #include <elf.h>
30f07a9995SKamil Rytarowski #include <util.h>
31f07a9995SKamil Rytarowski // clang-format on
321a3d19ddSKamil Rytarowski 
331a3d19ddSKamil Rytarowski using namespace lldb;
341a3d19ddSKamil Rytarowski using namespace lldb_private;
351a3d19ddSKamil Rytarowski using namespace lldb_private::process_netbsd;
361a3d19ddSKamil Rytarowski using namespace llvm;
371a3d19ddSKamil Rytarowski 
38f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file
39f07a9995SKamil Rytarowski // descriptor.
4097206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) {
4197206d57SZachary Turner   Status error;
42f07a9995SKamil Rytarowski 
43f07a9995SKamil Rytarowski   int status = fcntl(fd, F_GETFL);
44f07a9995SKamil Rytarowski   if (status == -1) {
45f07a9995SKamil Rytarowski     error.SetErrorToErrno();
46f07a9995SKamil Rytarowski     return error;
47f07a9995SKamil Rytarowski   }
48f07a9995SKamil Rytarowski 
49f07a9995SKamil Rytarowski   if (fcntl(fd, F_SETFL, status | flags) == -1) {
50f07a9995SKamil Rytarowski     error.SetErrorToErrno();
51f07a9995SKamil Rytarowski     return error;
52f07a9995SKamil Rytarowski   }
53f07a9995SKamil Rytarowski 
54f07a9995SKamil Rytarowski   return error;
55f07a9995SKamil Rytarowski }
56f07a9995SKamil Rytarowski 
571a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
581a3d19ddSKamil Rytarowski // Public Static Methods
591a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
601a3d19ddSKamil Rytarowski 
6182abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
6296e600fcSPavel Labath NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
6396e600fcSPavel Labath                                      NativeDelegate &native_delegate,
6496e600fcSPavel Labath                                      MainLoop &mainloop) const {
65f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
66f07a9995SKamil Rytarowski 
6796e600fcSPavel Labath   Status status;
6896e600fcSPavel Labath   ::pid_t pid = ProcessLauncherPosixFork()
6996e600fcSPavel Labath                     .LaunchProcess(launch_info, status)
7096e600fcSPavel Labath                     .GetProcessId();
7196e600fcSPavel Labath   LLDB_LOG(log, "pid = {0:x}", pid);
7296e600fcSPavel Labath   if (status.Fail()) {
7396e600fcSPavel Labath     LLDB_LOG(log, "failed to launch process: {0}", status);
7496e600fcSPavel Labath     return status.ToError();
75f07a9995SKamil Rytarowski   }
76f07a9995SKamil Rytarowski 
7796e600fcSPavel Labath   // Wait for the child process to trap on its call to execve.
7896e600fcSPavel Labath   int wstatus;
7996e600fcSPavel Labath   ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
8096e600fcSPavel Labath   assert(wpid == pid);
8196e600fcSPavel Labath   (void)wpid;
8296e600fcSPavel Labath   if (!WIFSTOPPED(wstatus)) {
8396e600fcSPavel Labath     LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
8496e600fcSPavel Labath              WaitStatus::Decode(wstatus));
8596e600fcSPavel Labath     return llvm::make_error<StringError>("Could not sync with inferior process",
8696e600fcSPavel Labath                                          llvm::inconvertibleErrorCode());
8796e600fcSPavel Labath   }
8896e600fcSPavel Labath   LLDB_LOG(log, "inferior started, now in stopped state");
89f07a9995SKamil Rytarowski 
9036e82208SPavel Labath   ProcessInstanceInfo Info;
9136e82208SPavel Labath   if (!Host::GetProcessInfo(pid, Info)) {
9236e82208SPavel Labath     return llvm::make_error<StringError>("Cannot get process architecture",
9336e82208SPavel Labath                                          llvm::inconvertibleErrorCode());
9436e82208SPavel Labath   }
9596e600fcSPavel Labath 
9696e600fcSPavel Labath   // Set the architecture to the exe architecture.
9796e600fcSPavel Labath   LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
9836e82208SPavel Labath            Info.GetArchitecture().GetArchitectureName());
9996e600fcSPavel Labath 
10082abefa4SPavel Labath   std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
10196e600fcSPavel Labath       pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
10236e82208SPavel Labath       Info.GetArchitecture(), mainloop));
10396e600fcSPavel Labath 
10482abefa4SPavel Labath   status = process_up->ReinitializeThreads();
10596e600fcSPavel Labath   if (status.Fail())
10696e600fcSPavel Labath     return status.ToError();
10796e600fcSPavel Labath 
108a5be48b3SPavel Labath   for (const auto &thread : process_up->m_threads)
109a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
1108a4bf06bSKamil Rytarowski   process_up->SetState(StateType::eStateStopped, false);
11196e600fcSPavel Labath 
11282abefa4SPavel Labath   return std::move(process_up);
113f07a9995SKamil Rytarowski }
114f07a9995SKamil Rytarowski 
11582abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
11682abefa4SPavel Labath NativeProcessNetBSD::Factory::Attach(
1171a3d19ddSKamil Rytarowski     lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
11896e600fcSPavel Labath     MainLoop &mainloop) const {
119f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
120f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid = {0:x}", pid);
121f07a9995SKamil Rytarowski 
122f07a9995SKamil Rytarowski   // Retrieve the architecture for the running process.
12336e82208SPavel Labath   ProcessInstanceInfo Info;
12436e82208SPavel Labath   if (!Host::GetProcessInfo(pid, Info)) {
12536e82208SPavel Labath     return llvm::make_error<StringError>("Cannot get process architecture",
12636e82208SPavel Labath                                          llvm::inconvertibleErrorCode());
12736e82208SPavel Labath   }
128f07a9995SKamil Rytarowski 
12936e82208SPavel Labath   std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
13036e82208SPavel Labath       pid, -1, native_delegate, Info.GetArchitecture(), mainloop));
131f07a9995SKamil Rytarowski 
13202d4e50eSPavel Labath   Status status = process_up->Attach();
13396e600fcSPavel Labath   if (!status.Success())
13496e600fcSPavel Labath     return status.ToError();
135f07a9995SKamil Rytarowski 
13682abefa4SPavel Labath   return std::move(process_up);
1371a3d19ddSKamil Rytarowski }
1381a3d19ddSKamil Rytarowski 
1391a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
1401a3d19ddSKamil Rytarowski // Public Instance Methods
1411a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
1421a3d19ddSKamil Rytarowski 
14396e600fcSPavel Labath NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd,
14496e600fcSPavel Labath                                          NativeDelegate &delegate,
14596e600fcSPavel Labath                                          const ArchSpec &arch,
14696e600fcSPavel Labath                                          MainLoop &mainloop)
14796e600fcSPavel Labath     : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
14896e600fcSPavel Labath   if (m_terminal_fd != -1) {
14996e600fcSPavel Labath     Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
15096e600fcSPavel Labath     assert(status.Success());
15196e600fcSPavel Labath   }
15296e600fcSPavel Labath 
15396e600fcSPavel Labath   Status status;
15496e600fcSPavel Labath   m_sigchld_handle = mainloop.RegisterSignal(
15596e600fcSPavel Labath       SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
15696e600fcSPavel Labath   assert(m_sigchld_handle && status.Success());
15796e600fcSPavel Labath }
158f07a9995SKamil Rytarowski 
159f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process.
160f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) {
161f07a9995SKamil Rytarowski   switch (signal) {
162f07a9995SKamil Rytarowski   case SIGTRAP:
163f07a9995SKamil Rytarowski     return MonitorSIGTRAP(pid);
164f07a9995SKamil Rytarowski   case SIGSTOP:
165f07a9995SKamil Rytarowski     return MonitorSIGSTOP(pid);
166f07a9995SKamil Rytarowski   default:
167f07a9995SKamil Rytarowski     return MonitorSignal(pid, signal);
168f07a9995SKamil Rytarowski   }
169f07a9995SKamil Rytarowski }
170f07a9995SKamil Rytarowski 
1713508fc8cSPavel Labath void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
172f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
173f07a9995SKamil Rytarowski 
1743508fc8cSPavel Labath   LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
175f07a9995SKamil Rytarowski 
176f07a9995SKamil Rytarowski   /* Stop Tracking All Threads attached to Process */
177f07a9995SKamil Rytarowski   m_threads.clear();
178f07a9995SKamil Rytarowski 
1793508fc8cSPavel Labath   SetExitStatus(status, true);
180f07a9995SKamil Rytarowski 
181f07a9995SKamil Rytarowski   // Notify delegate that our process has exited.
182f07a9995SKamil Rytarowski   SetState(StateType::eStateExited, true);
183f07a9995SKamil Rytarowski }
184f07a9995SKamil Rytarowski 
185f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) {
186f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
187f07a9995SKamil Rytarowski 
188f07a9995SKamil Rytarowski   const auto siginfo_err =
189f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
190f07a9995SKamil Rytarowski 
191f07a9995SKamil Rytarowski   // Get details on the signal raised.
192f07a9995SKamil Rytarowski   if (siginfo_err.Success()) {
193f07a9995SKamil Rytarowski     // Handle SIGSTOP from LLGS (LLDB GDB Server)
194f07a9995SKamil Rytarowski     if (info.psi_siginfo.si_code == SI_USER &&
195f07a9995SKamil Rytarowski         info.psi_siginfo.si_pid == ::getpid()) {
196a5be48b3SPavel Labath       /* Stop Tracking all Threads attached to Process */
197a5be48b3SPavel Labath       for (const auto &thread : m_threads) {
198a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
199f07a9995SKamil Rytarowski             SIGSTOP, &info.psi_siginfo);
200f07a9995SKamil Rytarowski       }
201f07a9995SKamil Rytarowski     }
202f07a9995SKamil Rytarowski   }
203f07a9995SKamil Rytarowski }
204f07a9995SKamil Rytarowski 
205f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) {
206f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
207f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
208f07a9995SKamil Rytarowski 
209f07a9995SKamil Rytarowski   const auto siginfo_err =
210f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
211f07a9995SKamil Rytarowski 
212f07a9995SKamil Rytarowski   // Get details on the signal raised.
21336e23ecaSKamil Rytarowski   if (siginfo_err.Fail()) {
21436e23ecaSKamil Rytarowski     return;
21536e23ecaSKamil Rytarowski   }
21636e23ecaSKamil Rytarowski 
217f07a9995SKamil Rytarowski   switch (info.psi_siginfo.si_code) {
218f07a9995SKamil Rytarowski   case TRAP_BRKPT:
219a5be48b3SPavel Labath     for (const auto &thread : m_threads) {
220a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
221a5be48b3SPavel Labath       FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
222f07a9995SKamil Rytarowski     }
223f07a9995SKamil Rytarowski     SetState(StateType::eStateStopped, true);
224f07a9995SKamil Rytarowski     break;
2253eef2b5eSKamil Rytarowski   case TRAP_TRACE:
226a5be48b3SPavel Labath     for (const auto &thread : m_threads)
227a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
2283eef2b5eSKamil Rytarowski     SetState(StateType::eStateStopped, true);
2293eef2b5eSKamil Rytarowski     break;
2303eef2b5eSKamil Rytarowski   case TRAP_EXEC: {
23197206d57SZachary Turner     Status error = ReinitializeThreads();
2323eef2b5eSKamil Rytarowski     if (error.Fail()) {
2333eef2b5eSKamil Rytarowski       SetState(StateType::eStateInvalid);
2343eef2b5eSKamil Rytarowski       return;
2353eef2b5eSKamil Rytarowski     }
2363eef2b5eSKamil Rytarowski 
2373eef2b5eSKamil Rytarowski     // Let our delegate know we have just exec'd.
2383eef2b5eSKamil Rytarowski     NotifyDidExec();
2393eef2b5eSKamil Rytarowski 
240a5be48b3SPavel Labath     for (const auto &thread : m_threads)
241a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
2423eef2b5eSKamil Rytarowski     SetState(StateType::eStateStopped, true);
2433eef2b5eSKamil Rytarowski   } break;
24436e23ecaSKamil Rytarowski   case TRAP_DBREG: {
24536e23ecaSKamil Rytarowski     // If a watchpoint was hit, report it
24636e23ecaSKamil Rytarowski     uint32_t wp_index;
247a5be48b3SPavel Labath     Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
248a5be48b3SPavel Labath                        .GetRegisterContext()
249d37349f3SPavel Labath                        .GetWatchpointHitIndex(
250a5be48b3SPavel Labath                            wp_index, (uintptr_t)info.psi_siginfo.si_addr);
25136e23ecaSKamil Rytarowski     if (error.Fail())
25236e23ecaSKamil Rytarowski       LLDB_LOG(log,
25336e23ecaSKamil Rytarowski                "received error while checking for watchpoint hits, pid = "
25436e23ecaSKamil Rytarowski                "{0}, LWP = {1}, error = {2}",
25536e23ecaSKamil Rytarowski                GetID(), info.psi_lwpid, error);
25636e23ecaSKamil Rytarowski     if (wp_index != LLDB_INVALID_INDEX32) {
257a5be48b3SPavel Labath       for (const auto &thread : m_threads)
258a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
259a5be48b3SPavel Labath             wp_index);
26036e23ecaSKamil Rytarowski       SetState(StateType::eStateStopped, true);
26136e23ecaSKamil Rytarowski       break;
26236e23ecaSKamil Rytarowski     }
26336e23ecaSKamil Rytarowski 
26436e23ecaSKamil Rytarowski     // If a breakpoint was hit, report it
26536e23ecaSKamil Rytarowski     uint32_t bp_index;
266a5be48b3SPavel Labath     error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
267a5be48b3SPavel Labath                 .GetRegisterContext()
268d37349f3SPavel Labath                 .GetHardwareBreakHitIndex(bp_index,
26936e23ecaSKamil Rytarowski                                            (uintptr_t)info.psi_siginfo.si_addr);
27036e23ecaSKamil Rytarowski     if (error.Fail())
27136e23ecaSKamil Rytarowski       LLDB_LOG(log,
27236e23ecaSKamil Rytarowski                "received error while checking for hardware "
27336e23ecaSKamil Rytarowski                "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
27436e23ecaSKamil Rytarowski                GetID(), info.psi_lwpid, error);
27536e23ecaSKamil Rytarowski     if (bp_index != LLDB_INVALID_INDEX32) {
276a5be48b3SPavel Labath       for (const auto &thread : m_threads)
277a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
27836e23ecaSKamil Rytarowski       SetState(StateType::eStateStopped, true);
27936e23ecaSKamil Rytarowski       break;
28036e23ecaSKamil Rytarowski     }
28136e23ecaSKamil Rytarowski   } break;
282f07a9995SKamil Rytarowski   }
283f07a9995SKamil Rytarowski }
284f07a9995SKamil Rytarowski 
285f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) {
286f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
287f07a9995SKamil Rytarowski   const auto siginfo_err =
288f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
289f07a9995SKamil Rytarowski 
290a5be48b3SPavel Labath   for (const auto &thread : m_threads) {
291a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
292f07a9995SKamil Rytarowski         info.psi_siginfo.si_signo, &info.psi_siginfo);
293f07a9995SKamil Rytarowski   }
294f07a9995SKamil Rytarowski   SetState(StateType::eStateStopped, true);
295f07a9995SKamil Rytarowski }
296f07a9995SKamil Rytarowski 
29797206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
298f07a9995SKamil Rytarowski                                           int data, int *result) {
299f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
30097206d57SZachary Turner   Status error;
301f07a9995SKamil Rytarowski   int ret;
302f07a9995SKamil Rytarowski 
303f07a9995SKamil Rytarowski   errno = 0;
304f07a9995SKamil Rytarowski   ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
305f07a9995SKamil Rytarowski 
306f07a9995SKamil Rytarowski   if (ret == -1)
307f07a9995SKamil Rytarowski     error.SetErrorToErrno();
308f07a9995SKamil Rytarowski 
309f07a9995SKamil Rytarowski   if (result)
310f07a9995SKamil Rytarowski     *result = ret;
311f07a9995SKamil Rytarowski 
312f07a9995SKamil Rytarowski   LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
313f07a9995SKamil Rytarowski 
314f07a9995SKamil Rytarowski   if (error.Fail())
315f07a9995SKamil Rytarowski     LLDB_LOG(log, "ptrace() failed: {0}", error);
316f07a9995SKamil Rytarowski 
317f07a9995SKamil Rytarowski   return error;
318f07a9995SKamil Rytarowski }
319f07a9995SKamil Rytarowski 
32097206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) {
321f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
322f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0}", GetID());
323f07a9995SKamil Rytarowski 
324a5be48b3SPavel Labath   const auto &thread = m_threads[0];
325f07a9995SKamil Rytarowski   const ResumeAction *const action =
326a5be48b3SPavel Labath       resume_actions.GetActionForThread(thread->GetID(), true);
327f07a9995SKamil Rytarowski 
328f07a9995SKamil Rytarowski   if (action == nullptr) {
329f07a9995SKamil Rytarowski     LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
330a5be48b3SPavel Labath              thread->GetID());
33197206d57SZachary Turner     return Status();
332f07a9995SKamil Rytarowski   }
333f07a9995SKamil Rytarowski 
33497206d57SZachary Turner   Status error;
3353eef2b5eSKamil Rytarowski 
336f07a9995SKamil Rytarowski   switch (action->state) {
337f07a9995SKamil Rytarowski   case eStateRunning: {
338f07a9995SKamil Rytarowski     // Run the thread, possibly feeding it the signal.
3393eef2b5eSKamil Rytarowski     error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1,
3403eef2b5eSKamil Rytarowski                                                action->signal);
341f07a9995SKamil Rytarowski     if (!error.Success())
342f07a9995SKamil Rytarowski       return error;
343a5be48b3SPavel Labath     for (const auto &thread : m_threads)
344a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
345f07a9995SKamil Rytarowski     SetState(eStateRunning, true);
346f07a9995SKamil Rytarowski     break;
347f07a9995SKamil Rytarowski   }
348f07a9995SKamil Rytarowski   case eStateStepping:
3493eef2b5eSKamil Rytarowski     // Run the thread, possibly feeding it the signal.
3503eef2b5eSKamil Rytarowski     error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1,
3513eef2b5eSKamil Rytarowski                                                action->signal);
3523eef2b5eSKamil Rytarowski     if (!error.Success())
3533eef2b5eSKamil Rytarowski       return error;
354a5be48b3SPavel Labath     for (const auto &thread : m_threads)
355a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
3563eef2b5eSKamil Rytarowski     SetState(eStateStepping, true);
357f07a9995SKamil Rytarowski     break;
358f07a9995SKamil Rytarowski 
359f07a9995SKamil Rytarowski   case eStateSuspended:
360f07a9995SKamil Rytarowski   case eStateStopped:
361f07a9995SKamil Rytarowski     llvm_unreachable("Unexpected state");
362f07a9995SKamil Rytarowski 
363f07a9995SKamil Rytarowski   default:
36497206d57SZachary Turner     return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
365f07a9995SKamil Rytarowski                   "for pid %" PRIu64 ", tid %" PRIu64,
366f07a9995SKamil Rytarowski                   __FUNCTION__, StateAsCString(action->state), GetID(),
367a5be48b3SPavel Labath                   thread->GetID());
368f07a9995SKamil Rytarowski   }
369f07a9995SKamil Rytarowski 
37097206d57SZachary Turner   return Status();
371f07a9995SKamil Rytarowski }
372f07a9995SKamil Rytarowski 
37397206d57SZachary Turner Status NativeProcessNetBSD::Halt() {
37497206d57SZachary Turner   Status error;
375f07a9995SKamil Rytarowski 
376f07a9995SKamil Rytarowski   if (kill(GetID(), SIGSTOP) != 0)
377f07a9995SKamil Rytarowski     error.SetErrorToErrno();
378f07a9995SKamil Rytarowski 
379f07a9995SKamil Rytarowski   return error;
380f07a9995SKamil Rytarowski }
381f07a9995SKamil Rytarowski 
38297206d57SZachary Turner Status NativeProcessNetBSD::Detach() {
38397206d57SZachary Turner   Status error;
384f07a9995SKamil Rytarowski 
385f07a9995SKamil Rytarowski   // Stop monitoring the inferior.
386f07a9995SKamil Rytarowski   m_sigchld_handle.reset();
387f07a9995SKamil Rytarowski 
388f07a9995SKamil Rytarowski   // Tell ptrace to detach from the process.
389f07a9995SKamil Rytarowski   if (GetID() == LLDB_INVALID_PROCESS_ID)
390f07a9995SKamil Rytarowski     return error;
391f07a9995SKamil Rytarowski 
392f07a9995SKamil Rytarowski   return PtraceWrapper(PT_DETACH, GetID());
393f07a9995SKamil Rytarowski }
394f07a9995SKamil Rytarowski 
39597206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) {
39697206d57SZachary Turner   Status error;
397f07a9995SKamil Rytarowski 
398f07a9995SKamil Rytarowski   if (kill(GetID(), signo))
399f07a9995SKamil Rytarowski     error.SetErrorToErrno();
400f07a9995SKamil Rytarowski 
401f07a9995SKamil Rytarowski   return error;
402f07a9995SKamil Rytarowski }
403f07a9995SKamil Rytarowski 
40497206d57SZachary Turner Status NativeProcessNetBSD::Kill() {
405f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
406f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0}", GetID());
407f07a9995SKamil Rytarowski 
40897206d57SZachary Turner   Status error;
409f07a9995SKamil Rytarowski 
410f07a9995SKamil Rytarowski   switch (m_state) {
411f07a9995SKamil Rytarowski   case StateType::eStateInvalid:
412f07a9995SKamil Rytarowski   case StateType::eStateExited:
413f07a9995SKamil Rytarowski   case StateType::eStateCrashed:
414f07a9995SKamil Rytarowski   case StateType::eStateDetached:
415f07a9995SKamil Rytarowski   case StateType::eStateUnloaded:
416f07a9995SKamil Rytarowski     // Nothing to do - the process is already dead.
417f07a9995SKamil Rytarowski     LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
418f07a9995SKamil Rytarowski              StateAsCString(m_state));
419f07a9995SKamil Rytarowski     return error;
420f07a9995SKamil Rytarowski 
421f07a9995SKamil Rytarowski   case StateType::eStateConnected:
422f07a9995SKamil Rytarowski   case StateType::eStateAttaching:
423f07a9995SKamil Rytarowski   case StateType::eStateLaunching:
424f07a9995SKamil Rytarowski   case StateType::eStateStopped:
425f07a9995SKamil Rytarowski   case StateType::eStateRunning:
426f07a9995SKamil Rytarowski   case StateType::eStateStepping:
427f07a9995SKamil Rytarowski   case StateType::eStateSuspended:
428f07a9995SKamil Rytarowski     // We can try to kill a process in these states.
429f07a9995SKamil Rytarowski     break;
430f07a9995SKamil Rytarowski   }
431f07a9995SKamil Rytarowski 
432f07a9995SKamil Rytarowski   if (kill(GetID(), SIGKILL) != 0) {
433f07a9995SKamil Rytarowski     error.SetErrorToErrno();
434f07a9995SKamil Rytarowski     return error;
435f07a9995SKamil Rytarowski   }
436f07a9995SKamil Rytarowski 
437f07a9995SKamil Rytarowski   return error;
438f07a9995SKamil Rytarowski }
439f07a9995SKamil Rytarowski 
44097206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr,
441f07a9995SKamil Rytarowski                                                 MemoryRegionInfo &range_info) {
442f07a9995SKamil Rytarowski 
443f07a9995SKamil Rytarowski   if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
444f07a9995SKamil Rytarowski     // We're done.
44597206d57SZachary Turner     return Status("unsupported");
446f07a9995SKamil Rytarowski   }
447f07a9995SKamil Rytarowski 
44897206d57SZachary Turner   Status error = PopulateMemoryRegionCache();
449f07a9995SKamil Rytarowski   if (error.Fail()) {
450f07a9995SKamil Rytarowski     return error;
451f07a9995SKamil Rytarowski   }
452f07a9995SKamil Rytarowski 
453f07a9995SKamil Rytarowski   lldb::addr_t prev_base_address = 0;
454f07a9995SKamil Rytarowski   // FIXME start by finding the last region that is <= target address using
455f07a9995SKamil Rytarowski   // binary search.  Data is sorted.
456f07a9995SKamil Rytarowski   // There can be a ton of regions on pthreads apps with lots of threads.
457f07a9995SKamil Rytarowski   for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
458f07a9995SKamil Rytarowski        ++it) {
459f07a9995SKamil Rytarowski     MemoryRegionInfo &proc_entry_info = it->first;
460f07a9995SKamil Rytarowski     // Sanity check assumption that memory map entries are ascending.
461f07a9995SKamil Rytarowski     assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
462f07a9995SKamil Rytarowski            "descending memory map entries detected, unexpected");
463f07a9995SKamil Rytarowski     prev_base_address = proc_entry_info.GetRange().GetRangeBase();
464f07a9995SKamil Rytarowski     UNUSED_IF_ASSERT_DISABLED(prev_base_address);
46505097246SAdrian Prantl     // If the target address comes before this entry, indicate distance to next
46605097246SAdrian Prantl     // region.
467f07a9995SKamil Rytarowski     if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
468f07a9995SKamil Rytarowski       range_info.GetRange().SetRangeBase(load_addr);
469f07a9995SKamil Rytarowski       range_info.GetRange().SetByteSize(
470f07a9995SKamil Rytarowski           proc_entry_info.GetRange().GetRangeBase() - load_addr);
471f07a9995SKamil Rytarowski       range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
472f07a9995SKamil Rytarowski       range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
473f07a9995SKamil Rytarowski       range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
474f07a9995SKamil Rytarowski       range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
475f07a9995SKamil Rytarowski       return error;
476f07a9995SKamil Rytarowski     } else if (proc_entry_info.GetRange().Contains(load_addr)) {
477f07a9995SKamil Rytarowski       // The target address is within the memory region we're processing here.
478f07a9995SKamil Rytarowski       range_info = proc_entry_info;
479f07a9995SKamil Rytarowski       return error;
480f07a9995SKamil Rytarowski     }
481f07a9995SKamil Rytarowski     // The target memory address comes somewhere after the region we just
482f07a9995SKamil Rytarowski     // parsed.
483f07a9995SKamil Rytarowski   }
484f07a9995SKamil Rytarowski   // If we made it here, we didn't find an entry that contained the given
48505097246SAdrian Prantl   // address. Return the load_addr as start and the amount of bytes betwwen
48605097246SAdrian Prantl   // load address and the end of the memory as size.
487f07a9995SKamil Rytarowski   range_info.GetRange().SetRangeBase(load_addr);
488f07a9995SKamil Rytarowski   range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
489f07a9995SKamil Rytarowski   range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
490f07a9995SKamil Rytarowski   range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
491f07a9995SKamil Rytarowski   range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
492f07a9995SKamil Rytarowski   range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
493f07a9995SKamil Rytarowski   return error;
494f07a9995SKamil Rytarowski }
495f07a9995SKamil Rytarowski 
49697206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() {
497f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
498f07a9995SKamil Rytarowski   // If our cache is empty, pull the latest.  There should always be at least
499f07a9995SKamil Rytarowski   // one memory region if memory region handling is supported.
500f07a9995SKamil Rytarowski   if (!m_mem_region_cache.empty()) {
501f07a9995SKamil Rytarowski     LLDB_LOG(log, "reusing {0} cached memory region entries",
502f07a9995SKamil Rytarowski              m_mem_region_cache.size());
50397206d57SZachary Turner     return Status();
504f07a9995SKamil Rytarowski   }
505f07a9995SKamil Rytarowski 
506f07a9995SKamil Rytarowski   struct kinfo_vmentry *vm;
507f07a9995SKamil Rytarowski   size_t count, i;
508f07a9995SKamil Rytarowski   vm = kinfo_getvmmap(GetID(), &count);
509f07a9995SKamil Rytarowski   if (vm == NULL) {
510f07a9995SKamil Rytarowski     m_supports_mem_region = LazyBool::eLazyBoolNo;
51197206d57SZachary Turner     Status error;
512f07a9995SKamil Rytarowski     error.SetErrorString("not supported");
513f07a9995SKamil Rytarowski     return error;
514f07a9995SKamil Rytarowski   }
515f07a9995SKamil Rytarowski   for (i = 0; i < count; i++) {
516f07a9995SKamil Rytarowski     MemoryRegionInfo info;
517f07a9995SKamil Rytarowski     info.Clear();
518f07a9995SKamil Rytarowski     info.GetRange().SetRangeBase(vm[i].kve_start);
519f07a9995SKamil Rytarowski     info.GetRange().SetRangeEnd(vm[i].kve_end);
520f07a9995SKamil Rytarowski     info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
521f07a9995SKamil Rytarowski 
522f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_READ)
523f07a9995SKamil Rytarowski       info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
524f07a9995SKamil Rytarowski     else
525f07a9995SKamil Rytarowski       info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
526f07a9995SKamil Rytarowski 
527f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_WRITE)
528f07a9995SKamil Rytarowski       info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
529f07a9995SKamil Rytarowski     else
530f07a9995SKamil Rytarowski       info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
531f07a9995SKamil Rytarowski 
532f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_EXECUTE)
533f07a9995SKamil Rytarowski       info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
534f07a9995SKamil Rytarowski     else
535f07a9995SKamil Rytarowski       info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
536f07a9995SKamil Rytarowski 
537f07a9995SKamil Rytarowski     if (vm[i].kve_path[0])
538f07a9995SKamil Rytarowski       info.SetName(vm[i].kve_path);
539f07a9995SKamil Rytarowski 
540f07a9995SKamil Rytarowski     m_mem_region_cache.emplace_back(
541a0a44e9cSKamil Rytarowski         info, FileSpec(info.GetName().GetCString()));
542f07a9995SKamil Rytarowski   }
543f07a9995SKamil Rytarowski   free(vm);
544f07a9995SKamil Rytarowski 
545f07a9995SKamil Rytarowski   if (m_mem_region_cache.empty()) {
54605097246SAdrian Prantl     // No entries after attempting to read them.  This shouldn't happen. Assume
54705097246SAdrian Prantl     // we don't support map entries.
548f07a9995SKamil Rytarowski     LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
549f07a9995SKamil Rytarowski                   "for memory region metadata retrieval");
550f07a9995SKamil Rytarowski     m_supports_mem_region = LazyBool::eLazyBoolNo;
55197206d57SZachary Turner     Status error;
552f07a9995SKamil Rytarowski     error.SetErrorString("not supported");
553f07a9995SKamil Rytarowski     return error;
554f07a9995SKamil Rytarowski   }
555f07a9995SKamil Rytarowski   LLDB_LOG(log, "read {0} memory region entries from process {1}",
556f07a9995SKamil Rytarowski            m_mem_region_cache.size(), GetID());
557f07a9995SKamil Rytarowski   // We support memory retrieval, remember that.
558f07a9995SKamil Rytarowski   m_supports_mem_region = LazyBool::eLazyBoolYes;
55997206d57SZachary Turner   return Status();
560f07a9995SKamil Rytarowski }
561f07a9995SKamil Rytarowski 
56297206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions,
563f07a9995SKamil Rytarowski                                            lldb::addr_t &addr) {
56497206d57SZachary Turner   return Status("Unimplemented");
565f07a9995SKamil Rytarowski }
566f07a9995SKamil Rytarowski 
56797206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) {
56897206d57SZachary Turner   return Status("Unimplemented");
569f07a9995SKamil Rytarowski }
570f07a9995SKamil Rytarowski 
571f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() {
572f07a9995SKamil Rytarowski   // punt on this for now
573f07a9995SKamil Rytarowski   return LLDB_INVALID_ADDRESS;
574f07a9995SKamil Rytarowski }
575f07a9995SKamil Rytarowski 
576f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); }
577f07a9995SKamil Rytarowski 
57897206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
579f07a9995SKamil Rytarowski                                           bool hardware) {
580f07a9995SKamil Rytarowski   if (hardware)
58197206d57SZachary Turner     return Status("NativeProcessNetBSD does not support hardware breakpoints");
582f07a9995SKamil Rytarowski   else
583f07a9995SKamil Rytarowski     return SetSoftwareBreakpoint(addr, size);
584f07a9995SKamil Rytarowski }
585f07a9995SKamil Rytarowski 
58697206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path,
587f07a9995SKamil Rytarowski                                                     FileSpec &file_spec) {
58897206d57SZachary Turner   return Status("Unimplemented");
589f07a9995SKamil Rytarowski }
590f07a9995SKamil Rytarowski 
59197206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
592f07a9995SKamil Rytarowski                                                lldb::addr_t &load_addr) {
593f07a9995SKamil Rytarowski   load_addr = LLDB_INVALID_ADDRESS;
59497206d57SZachary Turner   return Status();
595f07a9995SKamil Rytarowski }
596f07a9995SKamil Rytarowski 
597f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() {
598f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
599f07a9995SKamil Rytarowski   // Process all pending waitpid notifications.
600f07a9995SKamil Rytarowski   int status;
601c1a6b128SPavel Labath   ::pid_t wait_pid =
602c1a6b128SPavel Labath       llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG);
603f07a9995SKamil Rytarowski 
604f07a9995SKamil Rytarowski   if (wait_pid == 0)
605f07a9995SKamil Rytarowski     return; // We are done.
606f07a9995SKamil Rytarowski 
607f07a9995SKamil Rytarowski   if (wait_pid == -1) {
60897206d57SZachary Turner     Status error(errno, eErrorTypePOSIX);
609f07a9995SKamil Rytarowski     LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
610f07a9995SKamil Rytarowski   }
611f07a9995SKamil Rytarowski 
6123508fc8cSPavel Labath   WaitStatus wait_status = WaitStatus::Decode(status);
6133508fc8cSPavel Labath   bool exited = wait_status.type == WaitStatus::Exit ||
6143508fc8cSPavel Labath                 (wait_status.type == WaitStatus::Signal &&
6153508fc8cSPavel Labath                  wait_pid == static_cast<::pid_t>(GetID()));
616f07a9995SKamil Rytarowski 
617f07a9995SKamil Rytarowski   LLDB_LOG(log,
6183508fc8cSPavel Labath            "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
6193508fc8cSPavel Labath            GetID(), wait_pid, status, exited);
620f07a9995SKamil Rytarowski 
621f07a9995SKamil Rytarowski   if (exited)
6223508fc8cSPavel Labath     MonitorExited(wait_pid, wait_status);
6233508fc8cSPavel Labath   else {
6244bb74415SKamil Rytarowski     assert(wait_status.type == WaitStatus::Stop);
6253508fc8cSPavel Labath     MonitorCallback(wait_pid, wait_status.status);
6263508fc8cSPavel Labath   }
627f07a9995SKamil Rytarowski }
628f07a9995SKamil Rytarowski 
629269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
630a5be48b3SPavel Labath   for (const auto &thread : m_threads) {
631a5be48b3SPavel Labath     assert(thread && "thread list should not contain NULL threads");
632a5be48b3SPavel Labath     if (thread->GetID() == thread_id) {
633269eec03SKamil Rytarowski       // We have this thread.
634269eec03SKamil Rytarowski       return true;
635269eec03SKamil Rytarowski     }
636269eec03SKamil Rytarowski   }
637269eec03SKamil Rytarowski 
638269eec03SKamil Rytarowski   // We don't have this thread.
639269eec03SKamil Rytarowski   return false;
640269eec03SKamil Rytarowski }
641269eec03SKamil Rytarowski 
642a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
643f07a9995SKamil Rytarowski 
644f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
645f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
646f07a9995SKamil Rytarowski 
647f07a9995SKamil Rytarowski   assert(!HasThreadNoLock(thread_id) &&
648f07a9995SKamil Rytarowski          "attempted to add a thread by id that already exists");
649f07a9995SKamil Rytarowski 
650f07a9995SKamil Rytarowski   // If this is the first thread, save it as the current thread
651f07a9995SKamil Rytarowski   if (m_threads.empty())
652f07a9995SKamil Rytarowski     SetCurrentThreadID(thread_id);
653f07a9995SKamil Rytarowski 
654a5be48b3SPavel Labath   m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
655a5be48b3SPavel Labath   return static_cast<NativeThreadNetBSD &>(*m_threads.back());
656f07a9995SKamil Rytarowski }
657f07a9995SKamil Rytarowski 
65896e600fcSPavel Labath Status NativeProcessNetBSD::Attach() {
659f07a9995SKamil Rytarowski   // Attach to the requested process.
660f07a9995SKamil Rytarowski   // An attach will cause the thread to stop with a SIGSTOP.
66196e600fcSPavel Labath   Status status = PtraceWrapper(PT_ATTACH, m_pid);
66296e600fcSPavel Labath   if (status.Fail())
66396e600fcSPavel Labath     return status;
664f07a9995SKamil Rytarowski 
66596e600fcSPavel Labath   int wstatus;
66605097246SAdrian Prantl   // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this
66705097246SAdrian Prantl   // point we should have a thread stopped if waitpid succeeds.
6682819136fSMichal Gorny   if ((wstatus = llvm::sys::RetryAfterSignal(-1, waitpid,
669*c5d7bc86SMichal Gorny           m_pid, nullptr, WALLSIG)) < 0)
67096e600fcSPavel Labath     return Status(errno, eErrorTypePOSIX);
671f07a9995SKamil Rytarowski 
672f07a9995SKamil Rytarowski   /* Initialize threads */
67396e600fcSPavel Labath   status = ReinitializeThreads();
67496e600fcSPavel Labath   if (status.Fail())
67596e600fcSPavel Labath     return status;
676f07a9995SKamil Rytarowski 
677a5be48b3SPavel Labath   for (const auto &thread : m_threads)
678a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
67936e23ecaSKamil Rytarowski 
680f07a9995SKamil Rytarowski   // Let our process instance know the thread has stopped.
681f07a9995SKamil Rytarowski   SetState(StateType::eStateStopped);
68296e600fcSPavel Labath   return Status();
683f07a9995SKamil Rytarowski }
684f07a9995SKamil Rytarowski 
68597206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf,
68697206d57SZachary Turner                                        size_t size, size_t &bytes_read) {
687f07a9995SKamil Rytarowski   unsigned char *dst = static_cast<unsigned char *>(buf);
688f07a9995SKamil Rytarowski   struct ptrace_io_desc io;
689f07a9995SKamil Rytarowski 
690f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
691f07a9995SKamil Rytarowski   LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
692f07a9995SKamil Rytarowski 
693f07a9995SKamil Rytarowski   bytes_read = 0;
694f07a9995SKamil Rytarowski   io.piod_op = PIOD_READ_D;
695f07a9995SKamil Rytarowski   io.piod_len = size;
696f07a9995SKamil Rytarowski 
697f07a9995SKamil Rytarowski   do {
698f07a9995SKamil Rytarowski     io.piod_offs = (void *)(addr + bytes_read);
699f07a9995SKamil Rytarowski     io.piod_addr = dst + bytes_read;
700f07a9995SKamil Rytarowski 
70197206d57SZachary Turner     Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
702f07a9995SKamil Rytarowski     if (error.Fail())
703f07a9995SKamil Rytarowski       return error;
704f07a9995SKamil Rytarowski 
705f07a9995SKamil Rytarowski     bytes_read = io.piod_len;
706f07a9995SKamil Rytarowski     io.piod_len = size - bytes_read;
707f07a9995SKamil Rytarowski   } while (bytes_read < size);
708f07a9995SKamil Rytarowski 
70997206d57SZachary Turner   return Status();
710f07a9995SKamil Rytarowski }
711f07a9995SKamil Rytarowski 
71297206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf,
713f07a9995SKamil Rytarowski                                         size_t size, size_t &bytes_written) {
714f07a9995SKamil Rytarowski   const unsigned char *src = static_cast<const unsigned char *>(buf);
71597206d57SZachary Turner   Status error;
716f07a9995SKamil Rytarowski   struct ptrace_io_desc io;
717f07a9995SKamil Rytarowski 
718f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
719f07a9995SKamil Rytarowski   LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
720f07a9995SKamil Rytarowski 
721f07a9995SKamil Rytarowski   bytes_written = 0;
722f07a9995SKamil Rytarowski   io.piod_op = PIOD_WRITE_D;
723f07a9995SKamil Rytarowski   io.piod_len = size;
724f07a9995SKamil Rytarowski 
725f07a9995SKamil Rytarowski   do {
726269eec03SKamil Rytarowski     io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written));
727f07a9995SKamil Rytarowski     io.piod_offs = (void *)(addr + bytes_written);
728f07a9995SKamil Rytarowski 
72997206d57SZachary Turner     Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
730f07a9995SKamil Rytarowski     if (error.Fail())
731f07a9995SKamil Rytarowski       return error;
732f07a9995SKamil Rytarowski 
733f07a9995SKamil Rytarowski     bytes_written = io.piod_len;
734f07a9995SKamil Rytarowski     io.piod_len = size - bytes_written;
735f07a9995SKamil Rytarowski   } while (bytes_written < size);
736f07a9995SKamil Rytarowski 
737f07a9995SKamil Rytarowski   return error;
738f07a9995SKamil Rytarowski }
739f07a9995SKamil Rytarowski 
740f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
741f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const {
742f07a9995SKamil Rytarowski   /*
743f07a9995SKamil Rytarowski    * ELF_AUX_ENTRIES is currently restricted to kernel
744f07a9995SKamil Rytarowski    * (<sys/exec_elf.h> r. 1.155 specifies 15)
745f07a9995SKamil Rytarowski    *
746f07a9995SKamil Rytarowski    * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
747f07a9995SKamil Rytarowski    * information isn't needed.
748f07a9995SKamil Rytarowski    */
749f07a9995SKamil Rytarowski   size_t auxv_size = 100 * sizeof(AuxInfo);
750f07a9995SKamil Rytarowski 
751e831bb3cSPavel Labath   ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
752dbda2851SPavel Labath       llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
753f07a9995SKamil Rytarowski 
754269eec03SKamil Rytarowski   struct ptrace_io_desc io;
755269eec03SKamil Rytarowski   io.piod_op = PIOD_READ_AUXV;
756269eec03SKamil Rytarowski   io.piod_offs = 0;
757e831bb3cSPavel Labath   io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
758269eec03SKamil Rytarowski   io.piod_len = auxv_size;
759f07a9995SKamil Rytarowski 
76097206d57SZachary Turner   Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
761f07a9995SKamil Rytarowski 
762f07a9995SKamil Rytarowski   if (error.Fail())
763f07a9995SKamil Rytarowski     return std::error_code(error.GetError(), std::generic_category());
764f07a9995SKamil Rytarowski 
765f07a9995SKamil Rytarowski   if (io.piod_len < 1)
766f07a9995SKamil Rytarowski     return std::error_code(ECANCELED, std::generic_category());
767f07a9995SKamil Rytarowski 
768e831bb3cSPavel Labath   return std::move(buf);
769f07a9995SKamil Rytarowski }
7703eef2b5eSKamil Rytarowski 
77197206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() {
7723eef2b5eSKamil Rytarowski   // Clear old threads
7733eef2b5eSKamil Rytarowski   m_threads.clear();
7743eef2b5eSKamil Rytarowski 
7753eef2b5eSKamil Rytarowski   // Initialize new thread
7763eef2b5eSKamil Rytarowski   struct ptrace_lwpinfo info = {};
77797206d57SZachary Turner   Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
7783eef2b5eSKamil Rytarowski   if (error.Fail()) {
7793eef2b5eSKamil Rytarowski     return error;
7803eef2b5eSKamil Rytarowski   }
7813eef2b5eSKamil Rytarowski   // Reinitialize from scratch threads and register them in process
7823eef2b5eSKamil Rytarowski   while (info.pl_lwpid != 0) {
783a5be48b3SPavel Labath     AddThread(info.pl_lwpid);
7843eef2b5eSKamil Rytarowski     error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
7853eef2b5eSKamil Rytarowski     if (error.Fail()) {
7863eef2b5eSKamil Rytarowski       return error;
7873eef2b5eSKamil Rytarowski     }
7883eef2b5eSKamil Rytarowski   }
7893eef2b5eSKamil Rytarowski 
7903eef2b5eSKamil Rytarowski   return error;
7913eef2b5eSKamil Rytarowski }
792