11a3d19ddSKamil Rytarowski //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===//
21a3d19ddSKamil Rytarowski //
31a3d19ddSKamil Rytarowski //                     The LLVM Compiler Infrastructure
41a3d19ddSKamil Rytarowski //
51a3d19ddSKamil Rytarowski // This file is distributed under the University of Illinois Open Source
61a3d19ddSKamil Rytarowski // License. See LICENSE.TXT for details.
71a3d19ddSKamil Rytarowski //
81a3d19ddSKamil Rytarowski //===----------------------------------------------------------------------===//
91a3d19ddSKamil Rytarowski 
101a3d19ddSKamil Rytarowski #include "NativeProcessNetBSD.h"
111a3d19ddSKamil Rytarowski 
121a3d19ddSKamil Rytarowski // C Includes
131a3d19ddSKamil Rytarowski 
141a3d19ddSKamil Rytarowski // C++ Includes
151a3d19ddSKamil Rytarowski 
161a3d19ddSKamil Rytarowski // Other libraries and framework includes
171a3d19ddSKamil Rytarowski #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
18f07a9995SKamil Rytarowski #include "lldb/Host/HostProcess.h"
19f07a9995SKamil Rytarowski #include "lldb/Host/common/NativeRegisterContext.h"
20f07a9995SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
21f07a9995SKamil Rytarowski #include "lldb/Target/Process.h"
22d821c997SPavel Labath #include "lldb/Utility/State.h"
23c1a6b128SPavel Labath #include "llvm/Support/Errno.h"
241a3d19ddSKamil Rytarowski 
251a3d19ddSKamil Rytarowski // System includes - They have to be included after framework includes because
2605097246SAdrian Prantl // they define some macros which collide with variable names in other modules
27f07a9995SKamil Rytarowski // clang-format off
28f07a9995SKamil Rytarowski #include <sys/types.h>
29f07a9995SKamil Rytarowski #include <sys/ptrace.h>
30f07a9995SKamil Rytarowski #include <sys/sysctl.h>
31f07a9995SKamil Rytarowski #include <sys/wait.h>
32f07a9995SKamil Rytarowski #include <uvm/uvm_prot.h>
33f07a9995SKamil Rytarowski #include <elf.h>
34f07a9995SKamil Rytarowski #include <util.h>
35f07a9995SKamil Rytarowski // clang-format on
361a3d19ddSKamil Rytarowski 
371a3d19ddSKamil Rytarowski using namespace lldb;
381a3d19ddSKamil Rytarowski using namespace lldb_private;
391a3d19ddSKamil Rytarowski using namespace lldb_private::process_netbsd;
401a3d19ddSKamil Rytarowski using namespace llvm;
411a3d19ddSKamil Rytarowski 
42f07a9995SKamil Rytarowski // Simple helper function to ensure flags are enabled on the given file
43f07a9995SKamil Rytarowski // descriptor.
4497206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) {
4597206d57SZachary Turner   Status error;
46f07a9995SKamil Rytarowski 
47f07a9995SKamil Rytarowski   int status = fcntl(fd, F_GETFL);
48f07a9995SKamil Rytarowski   if (status == -1) {
49f07a9995SKamil Rytarowski     error.SetErrorToErrno();
50f07a9995SKamil Rytarowski     return error;
51f07a9995SKamil Rytarowski   }
52f07a9995SKamil Rytarowski 
53f07a9995SKamil Rytarowski   if (fcntl(fd, F_SETFL, status | flags) == -1) {
54f07a9995SKamil Rytarowski     error.SetErrorToErrno();
55f07a9995SKamil Rytarowski     return error;
56f07a9995SKamil Rytarowski   }
57f07a9995SKamil Rytarowski 
58f07a9995SKamil Rytarowski   return error;
59f07a9995SKamil Rytarowski }
60f07a9995SKamil Rytarowski 
611a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
621a3d19ddSKamil Rytarowski // Public Static Methods
631a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
641a3d19ddSKamil Rytarowski 
6582abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
6696e600fcSPavel Labath NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
6796e600fcSPavel Labath                                      NativeDelegate &native_delegate,
6896e600fcSPavel Labath                                      MainLoop &mainloop) const {
69f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
70f07a9995SKamil Rytarowski 
7196e600fcSPavel Labath   Status status;
7296e600fcSPavel Labath   ::pid_t pid = ProcessLauncherPosixFork()
7396e600fcSPavel Labath                     .LaunchProcess(launch_info, status)
7496e600fcSPavel Labath                     .GetProcessId();
7596e600fcSPavel Labath   LLDB_LOG(log, "pid = {0:x}", pid);
7696e600fcSPavel Labath   if (status.Fail()) {
7796e600fcSPavel Labath     LLDB_LOG(log, "failed to launch process: {0}", status);
7896e600fcSPavel Labath     return status.ToError();
79f07a9995SKamil Rytarowski   }
80f07a9995SKamil Rytarowski 
8196e600fcSPavel Labath   // Wait for the child process to trap on its call to execve.
8296e600fcSPavel Labath   int wstatus;
8396e600fcSPavel Labath   ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
8496e600fcSPavel Labath   assert(wpid == pid);
8596e600fcSPavel Labath   (void)wpid;
8696e600fcSPavel Labath   if (!WIFSTOPPED(wstatus)) {
8796e600fcSPavel Labath     LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
8896e600fcSPavel Labath              WaitStatus::Decode(wstatus));
8996e600fcSPavel Labath     return llvm::make_error<StringError>("Could not sync with inferior process",
9096e600fcSPavel Labath                                          llvm::inconvertibleErrorCode());
9196e600fcSPavel Labath   }
9296e600fcSPavel Labath   LLDB_LOG(log, "inferior started, now in stopped state");
93f07a9995SKamil Rytarowski 
9436e82208SPavel Labath   ProcessInstanceInfo Info;
9536e82208SPavel Labath   if (!Host::GetProcessInfo(pid, Info)) {
9636e82208SPavel Labath     return llvm::make_error<StringError>("Cannot get process architecture",
9736e82208SPavel Labath                                          llvm::inconvertibleErrorCode());
9836e82208SPavel Labath   }
9996e600fcSPavel Labath 
10096e600fcSPavel Labath   // Set the architecture to the exe architecture.
10196e600fcSPavel Labath   LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
10236e82208SPavel Labath            Info.GetArchitecture().GetArchitectureName());
10396e600fcSPavel Labath 
10482abefa4SPavel Labath   std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
10596e600fcSPavel Labath       pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
10636e82208SPavel Labath       Info.GetArchitecture(), mainloop));
10796e600fcSPavel Labath 
10882abefa4SPavel Labath   status = process_up->ReinitializeThreads();
10996e600fcSPavel Labath   if (status.Fail())
11096e600fcSPavel Labath     return status.ToError();
11196e600fcSPavel Labath 
112a5be48b3SPavel Labath   for (const auto &thread : process_up->m_threads)
113a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
1148a4bf06bSKamil Rytarowski   process_up->SetState(StateType::eStateStopped, false);
11596e600fcSPavel Labath 
11682abefa4SPavel Labath   return std::move(process_up);
117f07a9995SKamil Rytarowski }
118f07a9995SKamil Rytarowski 
11982abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
12082abefa4SPavel Labath NativeProcessNetBSD::Factory::Attach(
1211a3d19ddSKamil Rytarowski     lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
12296e600fcSPavel Labath     MainLoop &mainloop) const {
123f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
124f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid = {0:x}", pid);
125f07a9995SKamil Rytarowski 
126f07a9995SKamil Rytarowski   // Retrieve the architecture for the running process.
12736e82208SPavel Labath   ProcessInstanceInfo Info;
12836e82208SPavel Labath   if (!Host::GetProcessInfo(pid, Info)) {
12936e82208SPavel Labath     return llvm::make_error<StringError>("Cannot get process architecture",
13036e82208SPavel Labath                                          llvm::inconvertibleErrorCode());
13136e82208SPavel Labath   }
132f07a9995SKamil Rytarowski 
13336e82208SPavel Labath   std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
13436e82208SPavel Labath       pid, -1, native_delegate, Info.GetArchitecture(), mainloop));
135f07a9995SKamil Rytarowski 
13602d4e50eSPavel Labath   Status status = process_up->Attach();
13796e600fcSPavel Labath   if (!status.Success())
13896e600fcSPavel Labath     return status.ToError();
139f07a9995SKamil Rytarowski 
14082abefa4SPavel Labath   return std::move(process_up);
1411a3d19ddSKamil Rytarowski }
1421a3d19ddSKamil Rytarowski 
1431a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
1441a3d19ddSKamil Rytarowski // Public Instance Methods
1451a3d19ddSKamil Rytarowski // -----------------------------------------------------------------------------
1461a3d19ddSKamil Rytarowski 
14796e600fcSPavel Labath NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd,
14896e600fcSPavel Labath                                          NativeDelegate &delegate,
14996e600fcSPavel Labath                                          const ArchSpec &arch,
15096e600fcSPavel Labath                                          MainLoop &mainloop)
15196e600fcSPavel Labath     : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
15296e600fcSPavel Labath   if (m_terminal_fd != -1) {
15396e600fcSPavel Labath     Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
15496e600fcSPavel Labath     assert(status.Success());
15596e600fcSPavel Labath   }
15696e600fcSPavel Labath 
15796e600fcSPavel Labath   Status status;
15896e600fcSPavel Labath   m_sigchld_handle = mainloop.RegisterSignal(
15996e600fcSPavel Labath       SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
16096e600fcSPavel Labath   assert(m_sigchld_handle && status.Success());
16196e600fcSPavel Labath }
162f07a9995SKamil Rytarowski 
163f07a9995SKamil Rytarowski // Handles all waitpid events from the inferior process.
164f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) {
165f07a9995SKamil Rytarowski   switch (signal) {
166f07a9995SKamil Rytarowski   case SIGTRAP:
167f07a9995SKamil Rytarowski     return MonitorSIGTRAP(pid);
168f07a9995SKamil Rytarowski   case SIGSTOP:
169f07a9995SKamil Rytarowski     return MonitorSIGSTOP(pid);
170f07a9995SKamil Rytarowski   default:
171f07a9995SKamil Rytarowski     return MonitorSignal(pid, signal);
172f07a9995SKamil Rytarowski   }
173f07a9995SKamil Rytarowski }
174f07a9995SKamil Rytarowski 
1753508fc8cSPavel Labath void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
176f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
177f07a9995SKamil Rytarowski 
1783508fc8cSPavel Labath   LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
179f07a9995SKamil Rytarowski 
180f07a9995SKamil Rytarowski   /* Stop Tracking All Threads attached to Process */
181f07a9995SKamil Rytarowski   m_threads.clear();
182f07a9995SKamil Rytarowski 
1833508fc8cSPavel Labath   SetExitStatus(status, true);
184f07a9995SKamil Rytarowski 
185f07a9995SKamil Rytarowski   // Notify delegate that our process has exited.
186f07a9995SKamil Rytarowski   SetState(StateType::eStateExited, true);
187f07a9995SKamil Rytarowski }
188f07a9995SKamil Rytarowski 
189f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) {
190f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
191f07a9995SKamil Rytarowski 
192f07a9995SKamil Rytarowski   const auto siginfo_err =
193f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
194f07a9995SKamil Rytarowski 
195f07a9995SKamil Rytarowski   // Get details on the signal raised.
196f07a9995SKamil Rytarowski   if (siginfo_err.Success()) {
197f07a9995SKamil Rytarowski     // Handle SIGSTOP from LLGS (LLDB GDB Server)
198f07a9995SKamil Rytarowski     if (info.psi_siginfo.si_code == SI_USER &&
199f07a9995SKamil Rytarowski         info.psi_siginfo.si_pid == ::getpid()) {
200a5be48b3SPavel Labath       /* Stop Tracking all Threads attached to Process */
201a5be48b3SPavel Labath       for (const auto &thread : m_threads) {
202a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
203f07a9995SKamil Rytarowski             SIGSTOP, &info.psi_siginfo);
204f07a9995SKamil Rytarowski       }
205f07a9995SKamil Rytarowski     }
206f07a9995SKamil Rytarowski   }
207f07a9995SKamil Rytarowski }
208f07a9995SKamil Rytarowski 
209f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) {
210f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
211f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
212f07a9995SKamil Rytarowski 
213f07a9995SKamil Rytarowski   const auto siginfo_err =
214f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
215f07a9995SKamil Rytarowski 
216f07a9995SKamil Rytarowski   // Get details on the signal raised.
21736e23ecaSKamil Rytarowski   if (siginfo_err.Fail()) {
21836e23ecaSKamil Rytarowski     return;
21936e23ecaSKamil Rytarowski   }
22036e23ecaSKamil Rytarowski 
221f07a9995SKamil Rytarowski   switch (info.psi_siginfo.si_code) {
222f07a9995SKamil Rytarowski   case TRAP_BRKPT:
223a5be48b3SPavel Labath     for (const auto &thread : m_threads) {
224a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
225a5be48b3SPavel Labath       FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
226f07a9995SKamil Rytarowski     }
227f07a9995SKamil Rytarowski     SetState(StateType::eStateStopped, true);
228f07a9995SKamil Rytarowski     break;
2293eef2b5eSKamil Rytarowski   case TRAP_TRACE:
230a5be48b3SPavel Labath     for (const auto &thread : m_threads)
231a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
2323eef2b5eSKamil Rytarowski     SetState(StateType::eStateStopped, true);
2333eef2b5eSKamil Rytarowski     break;
2343eef2b5eSKamil Rytarowski   case TRAP_EXEC: {
23597206d57SZachary Turner     Status error = ReinitializeThreads();
2363eef2b5eSKamil Rytarowski     if (error.Fail()) {
2373eef2b5eSKamil Rytarowski       SetState(StateType::eStateInvalid);
2383eef2b5eSKamil Rytarowski       return;
2393eef2b5eSKamil Rytarowski     }
2403eef2b5eSKamil Rytarowski 
2413eef2b5eSKamil Rytarowski     // Let our delegate know we have just exec'd.
2423eef2b5eSKamil Rytarowski     NotifyDidExec();
2433eef2b5eSKamil Rytarowski 
244a5be48b3SPavel Labath     for (const auto &thread : m_threads)
245a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
2463eef2b5eSKamil Rytarowski     SetState(StateType::eStateStopped, true);
2473eef2b5eSKamil Rytarowski   } break;
24836e23ecaSKamil Rytarowski   case TRAP_DBREG: {
24936e23ecaSKamil Rytarowski     // If a watchpoint was hit, report it
25036e23ecaSKamil Rytarowski     uint32_t wp_index;
251a5be48b3SPavel Labath     Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
252a5be48b3SPavel Labath                        .GetRegisterContext()
253d37349f3SPavel Labath                        .GetWatchpointHitIndex(
254a5be48b3SPavel Labath                            wp_index, (uintptr_t)info.psi_siginfo.si_addr);
25536e23ecaSKamil Rytarowski     if (error.Fail())
25636e23ecaSKamil Rytarowski       LLDB_LOG(log,
25736e23ecaSKamil Rytarowski                "received error while checking for watchpoint hits, pid = "
25836e23ecaSKamil Rytarowski                "{0}, LWP = {1}, error = {2}",
25936e23ecaSKamil Rytarowski                GetID(), info.psi_lwpid, error);
26036e23ecaSKamil Rytarowski     if (wp_index != LLDB_INVALID_INDEX32) {
261a5be48b3SPavel Labath       for (const auto &thread : m_threads)
262a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
263a5be48b3SPavel Labath             wp_index);
26436e23ecaSKamil Rytarowski       SetState(StateType::eStateStopped, true);
26536e23ecaSKamil Rytarowski       break;
26636e23ecaSKamil Rytarowski     }
26736e23ecaSKamil Rytarowski 
26836e23ecaSKamil Rytarowski     // If a breakpoint was hit, report it
26936e23ecaSKamil Rytarowski     uint32_t bp_index;
270a5be48b3SPavel Labath     error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
271a5be48b3SPavel Labath                 .GetRegisterContext()
272d37349f3SPavel Labath                 .GetHardwareBreakHitIndex(bp_index,
27336e23ecaSKamil Rytarowski                                            (uintptr_t)info.psi_siginfo.si_addr);
27436e23ecaSKamil Rytarowski     if (error.Fail())
27536e23ecaSKamil Rytarowski       LLDB_LOG(log,
27636e23ecaSKamil Rytarowski                "received error while checking for hardware "
27736e23ecaSKamil Rytarowski                "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
27836e23ecaSKamil Rytarowski                GetID(), info.psi_lwpid, error);
27936e23ecaSKamil Rytarowski     if (bp_index != LLDB_INVALID_INDEX32) {
280a5be48b3SPavel Labath       for (const auto &thread : m_threads)
281a5be48b3SPavel Labath         static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
28236e23ecaSKamil Rytarowski       SetState(StateType::eStateStopped, true);
28336e23ecaSKamil Rytarowski       break;
28436e23ecaSKamil Rytarowski     }
28536e23ecaSKamil Rytarowski   } break;
286f07a9995SKamil Rytarowski   }
287f07a9995SKamil Rytarowski }
288f07a9995SKamil Rytarowski 
289f07a9995SKamil Rytarowski void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) {
290f07a9995SKamil Rytarowski   ptrace_siginfo_t info;
291f07a9995SKamil Rytarowski   const auto siginfo_err =
292f07a9995SKamil Rytarowski       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
293f07a9995SKamil Rytarowski 
294a5be48b3SPavel Labath   for (const auto &thread : m_threads) {
295a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
296f07a9995SKamil Rytarowski         info.psi_siginfo.si_signo, &info.psi_siginfo);
297f07a9995SKamil Rytarowski   }
298f07a9995SKamil Rytarowski   SetState(StateType::eStateStopped, true);
299f07a9995SKamil Rytarowski }
300f07a9995SKamil Rytarowski 
30197206d57SZachary Turner Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
302f07a9995SKamil Rytarowski                                           int data, int *result) {
303f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
30497206d57SZachary Turner   Status error;
305f07a9995SKamil Rytarowski   int ret;
306f07a9995SKamil Rytarowski 
307f07a9995SKamil Rytarowski   errno = 0;
308f07a9995SKamil Rytarowski   ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
309f07a9995SKamil Rytarowski 
310f07a9995SKamil Rytarowski   if (ret == -1)
311f07a9995SKamil Rytarowski     error.SetErrorToErrno();
312f07a9995SKamil Rytarowski 
313f07a9995SKamil Rytarowski   if (result)
314f07a9995SKamil Rytarowski     *result = ret;
315f07a9995SKamil Rytarowski 
316f07a9995SKamil Rytarowski   LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
317f07a9995SKamil Rytarowski 
318f07a9995SKamil Rytarowski   if (error.Fail())
319f07a9995SKamil Rytarowski     LLDB_LOG(log, "ptrace() failed: {0}", error);
320f07a9995SKamil Rytarowski 
321f07a9995SKamil Rytarowski   return error;
322f07a9995SKamil Rytarowski }
323f07a9995SKamil Rytarowski 
32497206d57SZachary Turner Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) {
325f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
326f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0}", GetID());
327f07a9995SKamil Rytarowski 
328a5be48b3SPavel Labath   const auto &thread = m_threads[0];
329f07a9995SKamil Rytarowski   const ResumeAction *const action =
330a5be48b3SPavel Labath       resume_actions.GetActionForThread(thread->GetID(), true);
331f07a9995SKamil Rytarowski 
332f07a9995SKamil Rytarowski   if (action == nullptr) {
333f07a9995SKamil Rytarowski     LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
334a5be48b3SPavel Labath              thread->GetID());
33597206d57SZachary Turner     return Status();
336f07a9995SKamil Rytarowski   }
337f07a9995SKamil Rytarowski 
33897206d57SZachary Turner   Status error;
3393eef2b5eSKamil Rytarowski 
340f07a9995SKamil Rytarowski   switch (action->state) {
341f07a9995SKamil Rytarowski   case eStateRunning: {
342f07a9995SKamil Rytarowski     // Run the thread, possibly feeding it the signal.
3433eef2b5eSKamil Rytarowski     error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1,
3443eef2b5eSKamil Rytarowski                                                action->signal);
345f07a9995SKamil Rytarowski     if (!error.Success())
346f07a9995SKamil Rytarowski       return error;
347a5be48b3SPavel Labath     for (const auto &thread : m_threads)
348a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
349f07a9995SKamil Rytarowski     SetState(eStateRunning, true);
350f07a9995SKamil Rytarowski     break;
351f07a9995SKamil Rytarowski   }
352f07a9995SKamil Rytarowski   case eStateStepping:
3533eef2b5eSKamil Rytarowski     // Run the thread, possibly feeding it the signal.
3543eef2b5eSKamil Rytarowski     error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1,
3553eef2b5eSKamil Rytarowski                                                action->signal);
3563eef2b5eSKamil Rytarowski     if (!error.Success())
3573eef2b5eSKamil Rytarowski       return error;
358a5be48b3SPavel Labath     for (const auto &thread : m_threads)
359a5be48b3SPavel Labath       static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
3603eef2b5eSKamil Rytarowski     SetState(eStateStepping, true);
361f07a9995SKamil Rytarowski     break;
362f07a9995SKamil Rytarowski 
363f07a9995SKamil Rytarowski   case eStateSuspended:
364f07a9995SKamil Rytarowski   case eStateStopped:
365f07a9995SKamil Rytarowski     llvm_unreachable("Unexpected state");
366f07a9995SKamil Rytarowski 
367f07a9995SKamil Rytarowski   default:
36897206d57SZachary Turner     return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
369f07a9995SKamil Rytarowski                   "for pid %" PRIu64 ", tid %" PRIu64,
370f07a9995SKamil Rytarowski                   __FUNCTION__, StateAsCString(action->state), GetID(),
371a5be48b3SPavel Labath                   thread->GetID());
372f07a9995SKamil Rytarowski   }
373f07a9995SKamil Rytarowski 
37497206d57SZachary Turner   return Status();
375f07a9995SKamil Rytarowski }
376f07a9995SKamil Rytarowski 
37797206d57SZachary Turner Status NativeProcessNetBSD::Halt() {
37897206d57SZachary Turner   Status error;
379f07a9995SKamil Rytarowski 
380f07a9995SKamil Rytarowski   if (kill(GetID(), SIGSTOP) != 0)
381f07a9995SKamil Rytarowski     error.SetErrorToErrno();
382f07a9995SKamil Rytarowski 
383f07a9995SKamil Rytarowski   return error;
384f07a9995SKamil Rytarowski }
385f07a9995SKamil Rytarowski 
38697206d57SZachary Turner Status NativeProcessNetBSD::Detach() {
38797206d57SZachary Turner   Status error;
388f07a9995SKamil Rytarowski 
389f07a9995SKamil Rytarowski   // Stop monitoring the inferior.
390f07a9995SKamil Rytarowski   m_sigchld_handle.reset();
391f07a9995SKamil Rytarowski 
392f07a9995SKamil Rytarowski   // Tell ptrace to detach from the process.
393f07a9995SKamil Rytarowski   if (GetID() == LLDB_INVALID_PROCESS_ID)
394f07a9995SKamil Rytarowski     return error;
395f07a9995SKamil Rytarowski 
396f07a9995SKamil Rytarowski   return PtraceWrapper(PT_DETACH, GetID());
397f07a9995SKamil Rytarowski }
398f07a9995SKamil Rytarowski 
39997206d57SZachary Turner Status NativeProcessNetBSD::Signal(int signo) {
40097206d57SZachary Turner   Status error;
401f07a9995SKamil Rytarowski 
402f07a9995SKamil Rytarowski   if (kill(GetID(), signo))
403f07a9995SKamil Rytarowski     error.SetErrorToErrno();
404f07a9995SKamil Rytarowski 
405f07a9995SKamil Rytarowski   return error;
406f07a9995SKamil Rytarowski }
407f07a9995SKamil Rytarowski 
40897206d57SZachary Turner Status NativeProcessNetBSD::Kill() {
409f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
410f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0}", GetID());
411f07a9995SKamil Rytarowski 
41297206d57SZachary Turner   Status error;
413f07a9995SKamil Rytarowski 
414f07a9995SKamil Rytarowski   switch (m_state) {
415f07a9995SKamil Rytarowski   case StateType::eStateInvalid:
416f07a9995SKamil Rytarowski   case StateType::eStateExited:
417f07a9995SKamil Rytarowski   case StateType::eStateCrashed:
418f07a9995SKamil Rytarowski   case StateType::eStateDetached:
419f07a9995SKamil Rytarowski   case StateType::eStateUnloaded:
420f07a9995SKamil Rytarowski     // Nothing to do - the process is already dead.
421f07a9995SKamil Rytarowski     LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
422f07a9995SKamil Rytarowski              StateAsCString(m_state));
423f07a9995SKamil Rytarowski     return error;
424f07a9995SKamil Rytarowski 
425f07a9995SKamil Rytarowski   case StateType::eStateConnected:
426f07a9995SKamil Rytarowski   case StateType::eStateAttaching:
427f07a9995SKamil Rytarowski   case StateType::eStateLaunching:
428f07a9995SKamil Rytarowski   case StateType::eStateStopped:
429f07a9995SKamil Rytarowski   case StateType::eStateRunning:
430f07a9995SKamil Rytarowski   case StateType::eStateStepping:
431f07a9995SKamil Rytarowski   case StateType::eStateSuspended:
432f07a9995SKamil Rytarowski     // We can try to kill a process in these states.
433f07a9995SKamil Rytarowski     break;
434f07a9995SKamil Rytarowski   }
435f07a9995SKamil Rytarowski 
436f07a9995SKamil Rytarowski   if (kill(GetID(), SIGKILL) != 0) {
437f07a9995SKamil Rytarowski     error.SetErrorToErrno();
438f07a9995SKamil Rytarowski     return error;
439f07a9995SKamil Rytarowski   }
440f07a9995SKamil Rytarowski 
441f07a9995SKamil Rytarowski   return error;
442f07a9995SKamil Rytarowski }
443f07a9995SKamil Rytarowski 
44497206d57SZachary Turner Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr,
445f07a9995SKamil Rytarowski                                                 MemoryRegionInfo &range_info) {
446f07a9995SKamil Rytarowski 
447f07a9995SKamil Rytarowski   if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
448f07a9995SKamil Rytarowski     // We're done.
44997206d57SZachary Turner     return Status("unsupported");
450f07a9995SKamil Rytarowski   }
451f07a9995SKamil Rytarowski 
45297206d57SZachary Turner   Status error = PopulateMemoryRegionCache();
453f07a9995SKamil Rytarowski   if (error.Fail()) {
454f07a9995SKamil Rytarowski     return error;
455f07a9995SKamil Rytarowski   }
456f07a9995SKamil Rytarowski 
457f07a9995SKamil Rytarowski   lldb::addr_t prev_base_address = 0;
458f07a9995SKamil Rytarowski   // FIXME start by finding the last region that is <= target address using
459f07a9995SKamil Rytarowski   // binary search.  Data is sorted.
460f07a9995SKamil Rytarowski   // There can be a ton of regions on pthreads apps with lots of threads.
461f07a9995SKamil Rytarowski   for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
462f07a9995SKamil Rytarowski        ++it) {
463f07a9995SKamil Rytarowski     MemoryRegionInfo &proc_entry_info = it->first;
464f07a9995SKamil Rytarowski     // Sanity check assumption that memory map entries are ascending.
465f07a9995SKamil Rytarowski     assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
466f07a9995SKamil Rytarowski            "descending memory map entries detected, unexpected");
467f07a9995SKamil Rytarowski     prev_base_address = proc_entry_info.GetRange().GetRangeBase();
468f07a9995SKamil Rytarowski     UNUSED_IF_ASSERT_DISABLED(prev_base_address);
46905097246SAdrian Prantl     // If the target address comes before this entry, indicate distance to next
47005097246SAdrian Prantl     // region.
471f07a9995SKamil Rytarowski     if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
472f07a9995SKamil Rytarowski       range_info.GetRange().SetRangeBase(load_addr);
473f07a9995SKamil Rytarowski       range_info.GetRange().SetByteSize(
474f07a9995SKamil Rytarowski           proc_entry_info.GetRange().GetRangeBase() - load_addr);
475f07a9995SKamil Rytarowski       range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
476f07a9995SKamil Rytarowski       range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
477f07a9995SKamil Rytarowski       range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
478f07a9995SKamil Rytarowski       range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
479f07a9995SKamil Rytarowski       return error;
480f07a9995SKamil Rytarowski     } else if (proc_entry_info.GetRange().Contains(load_addr)) {
481f07a9995SKamil Rytarowski       // The target address is within the memory region we're processing here.
482f07a9995SKamil Rytarowski       range_info = proc_entry_info;
483f07a9995SKamil Rytarowski       return error;
484f07a9995SKamil Rytarowski     }
485f07a9995SKamil Rytarowski     // The target memory address comes somewhere after the region we just
486f07a9995SKamil Rytarowski     // parsed.
487f07a9995SKamil Rytarowski   }
488f07a9995SKamil Rytarowski   // If we made it here, we didn't find an entry that contained the given
48905097246SAdrian Prantl   // address. Return the load_addr as start and the amount of bytes betwwen
49005097246SAdrian Prantl   // load address and the end of the memory as size.
491f07a9995SKamil Rytarowski   range_info.GetRange().SetRangeBase(load_addr);
492f07a9995SKamil Rytarowski   range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
493f07a9995SKamil Rytarowski   range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
494f07a9995SKamil Rytarowski   range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
495f07a9995SKamil Rytarowski   range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
496f07a9995SKamil Rytarowski   range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
497f07a9995SKamil Rytarowski   return error;
498f07a9995SKamil Rytarowski }
499f07a9995SKamil Rytarowski 
50097206d57SZachary Turner Status NativeProcessNetBSD::PopulateMemoryRegionCache() {
501f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
502f07a9995SKamil Rytarowski   // If our cache is empty, pull the latest.  There should always be at least
503f07a9995SKamil Rytarowski   // one memory region if memory region handling is supported.
504f07a9995SKamil Rytarowski   if (!m_mem_region_cache.empty()) {
505f07a9995SKamil Rytarowski     LLDB_LOG(log, "reusing {0} cached memory region entries",
506f07a9995SKamil Rytarowski              m_mem_region_cache.size());
50797206d57SZachary Turner     return Status();
508f07a9995SKamil Rytarowski   }
509f07a9995SKamil Rytarowski 
510f07a9995SKamil Rytarowski   struct kinfo_vmentry *vm;
511f07a9995SKamil Rytarowski   size_t count, i;
512f07a9995SKamil Rytarowski   vm = kinfo_getvmmap(GetID(), &count);
513f07a9995SKamil Rytarowski   if (vm == NULL) {
514f07a9995SKamil Rytarowski     m_supports_mem_region = LazyBool::eLazyBoolNo;
51597206d57SZachary Turner     Status error;
516f07a9995SKamil Rytarowski     error.SetErrorString("not supported");
517f07a9995SKamil Rytarowski     return error;
518f07a9995SKamil Rytarowski   }
519f07a9995SKamil Rytarowski   for (i = 0; i < count; i++) {
520f07a9995SKamil Rytarowski     MemoryRegionInfo info;
521f07a9995SKamil Rytarowski     info.Clear();
522f07a9995SKamil Rytarowski     info.GetRange().SetRangeBase(vm[i].kve_start);
523f07a9995SKamil Rytarowski     info.GetRange().SetRangeEnd(vm[i].kve_end);
524f07a9995SKamil Rytarowski     info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
525f07a9995SKamil Rytarowski 
526f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_READ)
527f07a9995SKamil Rytarowski       info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
528f07a9995SKamil Rytarowski     else
529f07a9995SKamil Rytarowski       info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
530f07a9995SKamil Rytarowski 
531f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_WRITE)
532f07a9995SKamil Rytarowski       info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
533f07a9995SKamil Rytarowski     else
534f07a9995SKamil Rytarowski       info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
535f07a9995SKamil Rytarowski 
536f07a9995SKamil Rytarowski     if (vm[i].kve_protection & VM_PROT_EXECUTE)
537f07a9995SKamil Rytarowski       info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
538f07a9995SKamil Rytarowski     else
539f07a9995SKamil Rytarowski       info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
540f07a9995SKamil Rytarowski 
541f07a9995SKamil Rytarowski     if (vm[i].kve_path[0])
542f07a9995SKamil Rytarowski       info.SetName(vm[i].kve_path);
543f07a9995SKamil Rytarowski 
544f07a9995SKamil Rytarowski     m_mem_region_cache.emplace_back(
545*a0a44e9cSKamil Rytarowski         info, FileSpec(info.GetName().GetCString()));
546f07a9995SKamil Rytarowski   }
547f07a9995SKamil Rytarowski   free(vm);
548f07a9995SKamil Rytarowski 
549f07a9995SKamil Rytarowski   if (m_mem_region_cache.empty()) {
55005097246SAdrian Prantl     // No entries after attempting to read them.  This shouldn't happen. Assume
55105097246SAdrian Prantl     // we don't support map entries.
552f07a9995SKamil Rytarowski     LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
553f07a9995SKamil Rytarowski                   "for memory region metadata retrieval");
554f07a9995SKamil Rytarowski     m_supports_mem_region = LazyBool::eLazyBoolNo;
55597206d57SZachary Turner     Status error;
556f07a9995SKamil Rytarowski     error.SetErrorString("not supported");
557f07a9995SKamil Rytarowski     return error;
558f07a9995SKamil Rytarowski   }
559f07a9995SKamil Rytarowski   LLDB_LOG(log, "read {0} memory region entries from process {1}",
560f07a9995SKamil Rytarowski            m_mem_region_cache.size(), GetID());
561f07a9995SKamil Rytarowski   // We support memory retrieval, remember that.
562f07a9995SKamil Rytarowski   m_supports_mem_region = LazyBool::eLazyBoolYes;
56397206d57SZachary Turner   return Status();
564f07a9995SKamil Rytarowski }
565f07a9995SKamil Rytarowski 
56697206d57SZachary Turner Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions,
567f07a9995SKamil Rytarowski                                            lldb::addr_t &addr) {
56897206d57SZachary Turner   return Status("Unimplemented");
569f07a9995SKamil Rytarowski }
570f07a9995SKamil Rytarowski 
57197206d57SZachary Turner Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) {
57297206d57SZachary Turner   return Status("Unimplemented");
573f07a9995SKamil Rytarowski }
574f07a9995SKamil Rytarowski 
575f07a9995SKamil Rytarowski lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() {
576f07a9995SKamil Rytarowski   // punt on this for now
577f07a9995SKamil Rytarowski   return LLDB_INVALID_ADDRESS;
578f07a9995SKamil Rytarowski }
579f07a9995SKamil Rytarowski 
580f07a9995SKamil Rytarowski size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); }
581f07a9995SKamil Rytarowski 
58297206d57SZachary Turner Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
583f07a9995SKamil Rytarowski                                           bool hardware) {
584f07a9995SKamil Rytarowski   if (hardware)
58597206d57SZachary Turner     return Status("NativeProcessNetBSD does not support hardware breakpoints");
586f07a9995SKamil Rytarowski   else
587f07a9995SKamil Rytarowski     return SetSoftwareBreakpoint(addr, size);
588f07a9995SKamil Rytarowski }
589f07a9995SKamil Rytarowski 
59097206d57SZachary Turner Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path,
591f07a9995SKamil Rytarowski                                                     FileSpec &file_spec) {
59297206d57SZachary Turner   return Status("Unimplemented");
593f07a9995SKamil Rytarowski }
594f07a9995SKamil Rytarowski 
59597206d57SZachary Turner Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
596f07a9995SKamil Rytarowski                                                lldb::addr_t &load_addr) {
597f07a9995SKamil Rytarowski   load_addr = LLDB_INVALID_ADDRESS;
59897206d57SZachary Turner   return Status();
599f07a9995SKamil Rytarowski }
600f07a9995SKamil Rytarowski 
601f07a9995SKamil Rytarowski void NativeProcessNetBSD::SigchldHandler() {
602f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
603f07a9995SKamil Rytarowski   // Process all pending waitpid notifications.
604f07a9995SKamil Rytarowski   int status;
605c1a6b128SPavel Labath   ::pid_t wait_pid =
606c1a6b128SPavel Labath       llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG);
607f07a9995SKamil Rytarowski 
608f07a9995SKamil Rytarowski   if (wait_pid == 0)
609f07a9995SKamil Rytarowski     return; // We are done.
610f07a9995SKamil Rytarowski 
611f07a9995SKamil Rytarowski   if (wait_pid == -1) {
61297206d57SZachary Turner     Status error(errno, eErrorTypePOSIX);
613f07a9995SKamil Rytarowski     LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
614f07a9995SKamil Rytarowski   }
615f07a9995SKamil Rytarowski 
6163508fc8cSPavel Labath   WaitStatus wait_status = WaitStatus::Decode(status);
6173508fc8cSPavel Labath   bool exited = wait_status.type == WaitStatus::Exit ||
6183508fc8cSPavel Labath                 (wait_status.type == WaitStatus::Signal &&
6193508fc8cSPavel Labath                  wait_pid == static_cast<::pid_t>(GetID()));
620f07a9995SKamil Rytarowski 
621f07a9995SKamil Rytarowski   LLDB_LOG(log,
6223508fc8cSPavel Labath            "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
6233508fc8cSPavel Labath            GetID(), wait_pid, status, exited);
624f07a9995SKamil Rytarowski 
625f07a9995SKamil Rytarowski   if (exited)
6263508fc8cSPavel Labath     MonitorExited(wait_pid, wait_status);
6273508fc8cSPavel Labath   else {
6284bb74415SKamil Rytarowski     assert(wait_status.type == WaitStatus::Stop);
6293508fc8cSPavel Labath     MonitorCallback(wait_pid, wait_status.status);
6303508fc8cSPavel Labath   }
631f07a9995SKamil Rytarowski }
632f07a9995SKamil Rytarowski 
633269eec03SKamil Rytarowski bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
634a5be48b3SPavel Labath   for (const auto &thread : m_threads) {
635a5be48b3SPavel Labath     assert(thread && "thread list should not contain NULL threads");
636a5be48b3SPavel Labath     if (thread->GetID() == thread_id) {
637269eec03SKamil Rytarowski       // We have this thread.
638269eec03SKamil Rytarowski       return true;
639269eec03SKamil Rytarowski     }
640269eec03SKamil Rytarowski   }
641269eec03SKamil Rytarowski 
642269eec03SKamil Rytarowski   // We don't have this thread.
643269eec03SKamil Rytarowski   return false;
644269eec03SKamil Rytarowski }
645269eec03SKamil Rytarowski 
646a5be48b3SPavel Labath NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
647f07a9995SKamil Rytarowski 
648f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
649f07a9995SKamil Rytarowski   LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
650f07a9995SKamil Rytarowski 
651f07a9995SKamil Rytarowski   assert(!HasThreadNoLock(thread_id) &&
652f07a9995SKamil Rytarowski          "attempted to add a thread by id that already exists");
653f07a9995SKamil Rytarowski 
654f07a9995SKamil Rytarowski   // If this is the first thread, save it as the current thread
655f07a9995SKamil Rytarowski   if (m_threads.empty())
656f07a9995SKamil Rytarowski     SetCurrentThreadID(thread_id);
657f07a9995SKamil Rytarowski 
658a5be48b3SPavel Labath   m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
659a5be48b3SPavel Labath   return static_cast<NativeThreadNetBSD &>(*m_threads.back());
660f07a9995SKamil Rytarowski }
661f07a9995SKamil Rytarowski 
66296e600fcSPavel Labath Status NativeProcessNetBSD::Attach() {
663f07a9995SKamil Rytarowski   // Attach to the requested process.
664f07a9995SKamil Rytarowski   // An attach will cause the thread to stop with a SIGSTOP.
66596e600fcSPavel Labath   Status status = PtraceWrapper(PT_ATTACH, m_pid);
66696e600fcSPavel Labath   if (status.Fail())
66796e600fcSPavel Labath     return status;
668f07a9995SKamil Rytarowski 
66996e600fcSPavel Labath   int wstatus;
67005097246SAdrian Prantl   // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this
67105097246SAdrian Prantl   // point we should have a thread stopped if waitpid succeeds.
67296e600fcSPavel Labath   if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0)
67396e600fcSPavel Labath     return Status(errno, eErrorTypePOSIX);
674f07a9995SKamil Rytarowski 
675f07a9995SKamil Rytarowski   /* Initialize threads */
67696e600fcSPavel Labath   status = ReinitializeThreads();
67796e600fcSPavel Labath   if (status.Fail())
67896e600fcSPavel Labath     return status;
679f07a9995SKamil Rytarowski 
680a5be48b3SPavel Labath   for (const auto &thread : m_threads)
681a5be48b3SPavel Labath     static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
68236e23ecaSKamil Rytarowski 
683f07a9995SKamil Rytarowski   // Let our process instance know the thread has stopped.
684f07a9995SKamil Rytarowski   SetState(StateType::eStateStopped);
68596e600fcSPavel Labath   return Status();
686f07a9995SKamil Rytarowski }
687f07a9995SKamil Rytarowski 
68897206d57SZachary Turner Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf,
68997206d57SZachary Turner                                        size_t size, size_t &bytes_read) {
690f07a9995SKamil Rytarowski   unsigned char *dst = static_cast<unsigned char *>(buf);
691f07a9995SKamil Rytarowski   struct ptrace_io_desc io;
692f07a9995SKamil Rytarowski 
693f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
694f07a9995SKamil Rytarowski   LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
695f07a9995SKamil Rytarowski 
696f07a9995SKamil Rytarowski   bytes_read = 0;
697f07a9995SKamil Rytarowski   io.piod_op = PIOD_READ_D;
698f07a9995SKamil Rytarowski   io.piod_len = size;
699f07a9995SKamil Rytarowski 
700f07a9995SKamil Rytarowski   do {
701f07a9995SKamil Rytarowski     io.piod_offs = (void *)(addr + bytes_read);
702f07a9995SKamil Rytarowski     io.piod_addr = dst + bytes_read;
703f07a9995SKamil Rytarowski 
70497206d57SZachary Turner     Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
705f07a9995SKamil Rytarowski     if (error.Fail())
706f07a9995SKamil Rytarowski       return error;
707f07a9995SKamil Rytarowski 
708f07a9995SKamil Rytarowski     bytes_read = io.piod_len;
709f07a9995SKamil Rytarowski     io.piod_len = size - bytes_read;
710f07a9995SKamil Rytarowski   } while (bytes_read < size);
711f07a9995SKamil Rytarowski 
71297206d57SZachary Turner   return Status();
713f07a9995SKamil Rytarowski }
714f07a9995SKamil Rytarowski 
71597206d57SZachary Turner Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf,
716f07a9995SKamil Rytarowski                                         size_t size, size_t &bytes_written) {
717f07a9995SKamil Rytarowski   const unsigned char *src = static_cast<const unsigned char *>(buf);
71897206d57SZachary Turner   Status error;
719f07a9995SKamil Rytarowski   struct ptrace_io_desc io;
720f07a9995SKamil Rytarowski 
721f07a9995SKamil Rytarowski   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
722f07a9995SKamil Rytarowski   LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
723f07a9995SKamil Rytarowski 
724f07a9995SKamil Rytarowski   bytes_written = 0;
725f07a9995SKamil Rytarowski   io.piod_op = PIOD_WRITE_D;
726f07a9995SKamil Rytarowski   io.piod_len = size;
727f07a9995SKamil Rytarowski 
728f07a9995SKamil Rytarowski   do {
729269eec03SKamil Rytarowski     io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written));
730f07a9995SKamil Rytarowski     io.piod_offs = (void *)(addr + bytes_written);
731f07a9995SKamil Rytarowski 
73297206d57SZachary Turner     Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
733f07a9995SKamil Rytarowski     if (error.Fail())
734f07a9995SKamil Rytarowski       return error;
735f07a9995SKamil Rytarowski 
736f07a9995SKamil Rytarowski     bytes_written = io.piod_len;
737f07a9995SKamil Rytarowski     io.piod_len = size - bytes_written;
738f07a9995SKamil Rytarowski   } while (bytes_written < size);
739f07a9995SKamil Rytarowski 
740f07a9995SKamil Rytarowski   return error;
741f07a9995SKamil Rytarowski }
742f07a9995SKamil Rytarowski 
743f07a9995SKamil Rytarowski llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
744f07a9995SKamil Rytarowski NativeProcessNetBSD::GetAuxvData() const {
745f07a9995SKamil Rytarowski   /*
746f07a9995SKamil Rytarowski    * ELF_AUX_ENTRIES is currently restricted to kernel
747f07a9995SKamil Rytarowski    * (<sys/exec_elf.h> r. 1.155 specifies 15)
748f07a9995SKamil Rytarowski    *
749f07a9995SKamil Rytarowski    * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
750f07a9995SKamil Rytarowski    * information isn't needed.
751f07a9995SKamil Rytarowski    */
752f07a9995SKamil Rytarowski   size_t auxv_size = 100 * sizeof(AuxInfo);
753f07a9995SKamil Rytarowski 
754e831bb3cSPavel Labath   ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
755dbda2851SPavel Labath       llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
756f07a9995SKamil Rytarowski 
757269eec03SKamil Rytarowski   struct ptrace_io_desc io;
758269eec03SKamil Rytarowski   io.piod_op = PIOD_READ_AUXV;
759269eec03SKamil Rytarowski   io.piod_offs = 0;
760e831bb3cSPavel Labath   io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
761269eec03SKamil Rytarowski   io.piod_len = auxv_size;
762f07a9995SKamil Rytarowski 
76397206d57SZachary Turner   Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
764f07a9995SKamil Rytarowski 
765f07a9995SKamil Rytarowski   if (error.Fail())
766f07a9995SKamil Rytarowski     return std::error_code(error.GetError(), std::generic_category());
767f07a9995SKamil Rytarowski 
768f07a9995SKamil Rytarowski   if (io.piod_len < 1)
769f07a9995SKamil Rytarowski     return std::error_code(ECANCELED, std::generic_category());
770f07a9995SKamil Rytarowski 
771e831bb3cSPavel Labath   return std::move(buf);
772f07a9995SKamil Rytarowski }
7733eef2b5eSKamil Rytarowski 
77497206d57SZachary Turner Status NativeProcessNetBSD::ReinitializeThreads() {
7753eef2b5eSKamil Rytarowski   // Clear old threads
7763eef2b5eSKamil Rytarowski   m_threads.clear();
7773eef2b5eSKamil Rytarowski 
7783eef2b5eSKamil Rytarowski   // Initialize new thread
7793eef2b5eSKamil Rytarowski   struct ptrace_lwpinfo info = {};
78097206d57SZachary Turner   Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
7813eef2b5eSKamil Rytarowski   if (error.Fail()) {
7823eef2b5eSKamil Rytarowski     return error;
7833eef2b5eSKamil Rytarowski   }
7843eef2b5eSKamil Rytarowski   // Reinitialize from scratch threads and register them in process
7853eef2b5eSKamil Rytarowski   while (info.pl_lwpid != 0) {
786a5be48b3SPavel Labath     AddThread(info.pl_lwpid);
7873eef2b5eSKamil Rytarowski     error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
7883eef2b5eSKamil Rytarowski     if (error.Fail()) {
7893eef2b5eSKamil Rytarowski       return error;
7903eef2b5eSKamil Rytarowski     }
7913eef2b5eSKamil Rytarowski   }
7923eef2b5eSKamil Rytarowski 
7933eef2b5eSKamil Rytarowski   return error;
7943eef2b5eSKamil Rytarowski }
795