1 //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "NativeProcessNetBSD.h"
13 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
14 #include "lldb/Host/HostProcess.h"
15 #include "lldb/Host/common/NativeRegisterContext.h"
16 #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Utility/State.h"
19 #include "llvm/Support/Errno.h"
21 // System includes - They have to be included after framework includes because
22 // they define some macros which collide with variable names in other modules
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <sys/sysctl.h>
28 #include <uvm/uvm_prot.h>
34 using namespace lldb_private;
35 using namespace lldb_private::process_netbsd;
38 // Simple helper function to ensure flags are enabled on the given file
40 static Status EnsureFDFlags(int fd, int flags) {
43 int status = fcntl(fd, F_GETFL);
45 error.SetErrorToErrno();
49 if (fcntl(fd, F_SETFL, status | flags) == -1) {
50 error.SetErrorToErrno();
57 // Public Static Methods
59 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
60 NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
61 NativeDelegate &native_delegate,
62 MainLoop &mainloop) const {
63 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
66 ::pid_t pid = ProcessLauncherPosixFork()
67 .LaunchProcess(launch_info, status)
69 LLDB_LOG(log, "pid = {0:x}", pid);
71 LLDB_LOG(log, "failed to launch process: {0}", status);
72 return status.ToError();
75 // Wait for the child process to trap on its call to execve.
77 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
80 if (!WIFSTOPPED(wstatus)) {
81 LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
82 WaitStatus::Decode(wstatus));
83 return llvm::make_error<StringError>("Could not sync with inferior process",
84 llvm::inconvertibleErrorCode());
86 LLDB_LOG(log, "inferior started, now in stopped state");
88 ProcessInstanceInfo Info;
89 if (!Host::GetProcessInfo(pid, Info)) {
90 return llvm::make_error<StringError>("Cannot get process architecture",
91 llvm::inconvertibleErrorCode());
94 // Set the architecture to the exe architecture.
95 LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
96 Info.GetArchitecture().GetArchitectureName());
98 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
99 pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
100 Info.GetArchitecture(), mainloop));
102 status = process_up->ReinitializeThreads();
104 return status.ToError();
106 for (const auto &thread : process_up->m_threads)
107 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
108 process_up->SetState(StateType::eStateStopped, false);
110 return std::move(process_up);
113 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
114 NativeProcessNetBSD::Factory::Attach(
115 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
116 MainLoop &mainloop) const {
117 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
118 LLDB_LOG(log, "pid = {0:x}", pid);
120 // Retrieve the architecture for the running process.
121 ProcessInstanceInfo Info;
122 if (!Host::GetProcessInfo(pid, Info)) {
123 return llvm::make_error<StringError>("Cannot get process architecture",
124 llvm::inconvertibleErrorCode());
127 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
128 pid, -1, native_delegate, Info.GetArchitecture(), mainloop));
130 Status status = process_up->Attach();
131 if (!status.Success())
132 return status.ToError();
134 return std::move(process_up);
137 // Public Instance Methods
139 NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd,
140 NativeDelegate &delegate,
141 const ArchSpec &arch,
143 : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
144 if (m_terminal_fd != -1) {
145 Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
146 assert(status.Success());
150 m_sigchld_handle = mainloop.RegisterSignal(
151 SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
152 assert(m_sigchld_handle && status.Success());
155 // Handles all waitpid events from the inferior process.
156 void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) {
159 return MonitorSIGTRAP(pid);
161 return MonitorSIGSTOP(pid);
163 return MonitorSignal(pid, signal);
167 void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
168 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
170 LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
172 /* Stop Tracking All Threads attached to Process */
175 SetExitStatus(status, true);
177 // Notify delegate that our process has exited.
178 SetState(StateType::eStateExited, true);
181 void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) {
182 ptrace_siginfo_t info;
184 const auto siginfo_err =
185 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
187 // Get details on the signal raised.
188 if (siginfo_err.Success()) {
189 // Handle SIGSTOP from LLGS (LLDB GDB Server)
190 if (info.psi_siginfo.si_code == SI_USER &&
191 info.psi_siginfo.si_pid == ::getpid()) {
192 /* Stop Tracking all Threads attached to Process */
193 for (const auto &thread : m_threads) {
194 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
195 SIGSTOP, &info.psi_siginfo);
201 void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) {
202 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
203 ptrace_siginfo_t info;
205 const auto siginfo_err =
206 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
208 // Get details on the signal raised.
209 if (siginfo_err.Fail()) {
213 switch (info.psi_siginfo.si_code) {
215 for (const auto &thread : m_threads) {
216 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
217 FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
219 SetState(StateType::eStateStopped, true);
222 for (const auto &thread : m_threads)
223 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
224 SetState(StateType::eStateStopped, true);
227 Status error = ReinitializeThreads();
229 SetState(StateType::eStateInvalid);
233 // Let our delegate know we have just exec'd.
236 for (const auto &thread : m_threads)
237 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
238 SetState(StateType::eStateStopped, true);
242 NativeThreadNetBSD* thread = nullptr;
243 for (const auto &t : m_threads) {
244 if (t->GetID() == info.psi_lwpid) {
245 thread = static_cast<NativeThreadNetBSD *>(t.get());
251 "thread not found in m_threads, pid = {0}, LWP = {1}",
252 GetID(), info.psi_lwpid);
256 // If a watchpoint was hit, report it
257 uint32_t wp_index = LLDB_INVALID_INDEX32;
258 Status error = thread->GetRegisterContext().GetWatchpointHitIndex(
259 wp_index, (uintptr_t)info.psi_siginfo.si_addr);
262 "received error while checking for watchpoint hits, pid = "
263 "{0}, LWP = {1}, error = {2}",
264 GetID(), info.psi_lwpid, error);
265 if (wp_index != LLDB_INVALID_INDEX32) {
266 for (const auto &thread : m_threads)
267 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
269 SetState(StateType::eStateStopped, true);
273 // If a breakpoint was hit, report it
274 uint32_t bp_index = LLDB_INVALID_INDEX32;
275 error = thread->GetRegisterContext().GetHardwareBreakHitIndex(
276 bp_index, (uintptr_t)info.psi_siginfo.si_addr);
279 "received error while checking for hardware "
280 "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
281 GetID(), info.psi_lwpid, error);
282 if (bp_index != LLDB_INVALID_INDEX32) {
283 for (const auto &thread : m_threads)
284 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
285 SetState(StateType::eStateStopped, true);
292 void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) {
293 ptrace_siginfo_t info;
294 const auto siginfo_err =
295 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
297 for (const auto &thread : m_threads) {
298 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
299 info.psi_siginfo.si_signo, &info.psi_siginfo);
301 SetState(StateType::eStateStopped, true);
304 Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
305 int data, int *result) {
306 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
311 ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
314 error.SetErrorToErrno();
319 LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
322 LLDB_LOG(log, "ptrace() failed: {0}", error);
327 Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) {
328 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
329 LLDB_LOG(log, "pid {0}", GetID());
331 const auto &thread = m_threads[0];
332 const ResumeAction *const action =
333 resume_actions.GetActionForThread(thread->GetID(), true);
335 if (action == nullptr) {
336 LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
343 switch (action->state) {
344 case eStateRunning: {
345 // Run the thread, possibly feeding it the signal.
346 error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1,
348 if (!error.Success())
350 for (const auto &thread : m_threads)
351 static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
352 SetState(eStateRunning, true);
356 // Run the thread, possibly feeding it the signal.
357 error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1,
359 if (!error.Success())
361 for (const auto &thread : m_threads)
362 static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
363 SetState(eStateStepping, true);
366 case eStateSuspended:
368 llvm_unreachable("Unexpected state");
371 return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
372 "for pid %" PRIu64 ", tid %" PRIu64,
373 __FUNCTION__, StateAsCString(action->state), GetID(),
380 Status NativeProcessNetBSD::Halt() {
383 if (kill(GetID(), SIGSTOP) != 0)
384 error.SetErrorToErrno();
389 Status NativeProcessNetBSD::Detach() {
392 // Stop monitoring the inferior.
393 m_sigchld_handle.reset();
395 // Tell ptrace to detach from the process.
396 if (GetID() == LLDB_INVALID_PROCESS_ID)
399 return PtraceWrapper(PT_DETACH, GetID());
402 Status NativeProcessNetBSD::Signal(int signo) {
405 if (kill(GetID(), signo))
406 error.SetErrorToErrno();
411 Status NativeProcessNetBSD::Kill() {
412 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
413 LLDB_LOG(log, "pid {0}", GetID());
418 case StateType::eStateInvalid:
419 case StateType::eStateExited:
420 case StateType::eStateCrashed:
421 case StateType::eStateDetached:
422 case StateType::eStateUnloaded:
423 // Nothing to do - the process is already dead.
424 LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
425 StateAsCString(m_state));
428 case StateType::eStateConnected:
429 case StateType::eStateAttaching:
430 case StateType::eStateLaunching:
431 case StateType::eStateStopped:
432 case StateType::eStateRunning:
433 case StateType::eStateStepping:
434 case StateType::eStateSuspended:
435 // We can try to kill a process in these states.
439 if (kill(GetID(), SIGKILL) != 0) {
440 error.SetErrorToErrno();
447 Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr,
448 MemoryRegionInfo &range_info) {
450 if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
452 return Status("unsupported");
455 Status error = PopulateMemoryRegionCache();
460 lldb::addr_t prev_base_address = 0;
461 // FIXME start by finding the last region that is <= target address using
462 // binary search. Data is sorted.
463 // There can be a ton of regions on pthreads apps with lots of threads.
464 for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
466 MemoryRegionInfo &proc_entry_info = it->first;
467 // Sanity check assumption that memory map entries are ascending.
468 assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
469 "descending memory map entries detected, unexpected");
470 prev_base_address = proc_entry_info.GetRange().GetRangeBase();
471 UNUSED_IF_ASSERT_DISABLED(prev_base_address);
472 // If the target address comes before this entry, indicate distance to next
474 if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
475 range_info.GetRange().SetRangeBase(load_addr);
476 range_info.GetRange().SetByteSize(
477 proc_entry_info.GetRange().GetRangeBase() - load_addr);
478 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
479 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
480 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
481 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
483 } else if (proc_entry_info.GetRange().Contains(load_addr)) {
484 // The target address is within the memory region we're processing here.
485 range_info = proc_entry_info;
488 // The target memory address comes somewhere after the region we just
491 // If we made it here, we didn't find an entry that contained the given
492 // address. Return the load_addr as start and the amount of bytes betwwen
493 // load address and the end of the memory as size.
494 range_info.GetRange().SetRangeBase(load_addr);
495 range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
496 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
497 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
498 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
499 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
503 Status NativeProcessNetBSD::PopulateMemoryRegionCache() {
504 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
505 // If our cache is empty, pull the latest. There should always be at least
506 // one memory region if memory region handling is supported.
507 if (!m_mem_region_cache.empty()) {
508 LLDB_LOG(log, "reusing {0} cached memory region entries",
509 m_mem_region_cache.size());
513 struct kinfo_vmentry *vm;
515 vm = kinfo_getvmmap(GetID(), &count);
517 m_supports_mem_region = LazyBool::eLazyBoolNo;
519 error.SetErrorString("not supported");
522 for (i = 0; i < count; i++) {
523 MemoryRegionInfo info;
525 info.GetRange().SetRangeBase(vm[i].kve_start);
526 info.GetRange().SetRangeEnd(vm[i].kve_end);
527 info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
529 if (vm[i].kve_protection & VM_PROT_READ)
530 info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
532 info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
534 if (vm[i].kve_protection & VM_PROT_WRITE)
535 info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
537 info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
539 if (vm[i].kve_protection & VM_PROT_EXECUTE)
540 info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
542 info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
544 if (vm[i].kve_path[0])
545 info.SetName(vm[i].kve_path);
547 m_mem_region_cache.emplace_back(
548 info, FileSpec(info.GetName().GetCString()));
552 if (m_mem_region_cache.empty()) {
553 // No entries after attempting to read them. This shouldn't happen. Assume
554 // we don't support map entries.
555 LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
556 "for memory region metadata retrieval");
557 m_supports_mem_region = LazyBool::eLazyBoolNo;
559 error.SetErrorString("not supported");
562 LLDB_LOG(log, "read {0} memory region entries from process {1}",
563 m_mem_region_cache.size(), GetID());
564 // We support memory retrieval, remember that.
565 m_supports_mem_region = LazyBool::eLazyBoolYes;
569 Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions,
570 lldb::addr_t &addr) {
571 return Status("Unimplemented");
574 Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) {
575 return Status("Unimplemented");
578 lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() {
579 // punt on this for now
580 return LLDB_INVALID_ADDRESS;
583 size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); }
585 Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
588 return Status("NativeProcessNetBSD does not support hardware breakpoints");
590 return SetSoftwareBreakpoint(addr, size);
593 Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path,
594 FileSpec &file_spec) {
595 return Status("Unimplemented");
598 Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
599 lldb::addr_t &load_addr) {
600 load_addr = LLDB_INVALID_ADDRESS;
604 void NativeProcessNetBSD::SigchldHandler() {
605 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
606 // Process all pending waitpid notifications.
609 llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG);
612 return; // We are done.
614 if (wait_pid == -1) {
615 Status error(errno, eErrorTypePOSIX);
616 LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
619 WaitStatus wait_status = WaitStatus::Decode(status);
620 bool exited = wait_status.type == WaitStatus::Exit ||
621 (wait_status.type == WaitStatus::Signal &&
622 wait_pid == static_cast<::pid_t>(GetID()));
625 "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
626 GetID(), wait_pid, status, exited);
629 MonitorExited(wait_pid, wait_status);
631 assert(wait_status.type == WaitStatus::Stop);
632 MonitorCallback(wait_pid, wait_status.status);
636 bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
637 for (const auto &thread : m_threads) {
638 assert(thread && "thread list should not contain NULL threads");
639 if (thread->GetID() == thread_id) {
640 // We have this thread.
645 // We don't have this thread.
649 NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
651 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
652 LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
654 assert(!HasThreadNoLock(thread_id) &&
655 "attempted to add a thread by id that already exists");
657 // If this is the first thread, save it as the current thread
658 if (m_threads.empty())
659 SetCurrentThreadID(thread_id);
661 m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
662 return static_cast<NativeThreadNetBSD &>(*m_threads.back());
665 Status NativeProcessNetBSD::Attach() {
666 // Attach to the requested process.
667 // An attach will cause the thread to stop with a SIGSTOP.
668 Status status = PtraceWrapper(PT_ATTACH, m_pid);
673 // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this
674 // point we should have a thread stopped if waitpid succeeds.
675 if ((wstatus = llvm::sys::RetryAfterSignal(-1, waitpid,
676 m_pid, nullptr, WALLSIG)) < 0)
677 return Status(errno, eErrorTypePOSIX);
679 /* Initialize threads */
680 status = ReinitializeThreads();
684 for (const auto &thread : m_threads)
685 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
687 // Let our process instance know the thread has stopped.
688 SetState(StateType::eStateStopped);
692 Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf,
693 size_t size, size_t &bytes_read) {
694 unsigned char *dst = static_cast<unsigned char *>(buf);
695 struct ptrace_io_desc io;
697 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
698 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
701 io.piod_op = PIOD_READ_D;
705 io.piod_offs = (void *)(addr + bytes_read);
706 io.piod_addr = dst + bytes_read;
708 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
709 if (error.Fail() || io.piod_len == 0)
712 bytes_read += io.piod_len;
713 io.piod_len = size - bytes_read;
714 } while (bytes_read < size);
719 Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf,
720 size_t size, size_t &bytes_written) {
721 const unsigned char *src = static_cast<const unsigned char *>(buf);
723 struct ptrace_io_desc io;
725 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
726 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
729 io.piod_op = PIOD_WRITE_D;
733 io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written));
734 io.piod_offs = (void *)(addr + bytes_written);
736 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
737 if (error.Fail() || io.piod_len == 0)
740 bytes_written += io.piod_len;
741 io.piod_len = size - bytes_written;
742 } while (bytes_written < size);
747 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
748 NativeProcessNetBSD::GetAuxvData() const {
750 * ELF_AUX_ENTRIES is currently restricted to kernel
751 * (<sys/exec_elf.h> r. 1.155 specifies 15)
753 * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
754 * information isn't needed.
756 size_t auxv_size = 100 * sizeof(AuxInfo);
758 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
759 llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
761 struct ptrace_io_desc io;
762 io.piod_op = PIOD_READ_AUXV;
764 io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
765 io.piod_len = auxv_size;
767 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
770 return std::error_code(error.GetError(), std::generic_category());
773 return std::error_code(ECANCELED, std::generic_category());
775 return std::move(buf);
778 Status NativeProcessNetBSD::ReinitializeThreads() {
782 // Initialize new thread
783 struct ptrace_lwpinfo info = {};
784 Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
788 // Reinitialize from scratch threads and register them in process
789 while (info.pl_lwpid != 0) {
790 AddThread(info.pl_lwpid);
791 error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));