blob: a72edd837690cb97215fb07a4f32c5ea14fb589d [file] [log] [blame]
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +00001//===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "NativeProcessNetBSD.h"
11
12// C Includes
13
14// C++ Includes
15
16// Other libraries and framework includes
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000017#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000018#include "lldb/Core/State.h"
19#include "lldb/Host/HostProcess.h"
20#include "lldb/Host/common/NativeBreakpoint.h"
21#include "lldb/Host/common/NativeRegisterContext.h"
22#include "lldb/Host/posix/ProcessLauncherPosixFork.h"
23#include "lldb/Target/Process.h"
Pavel Labathc1a6b122017-07-03 09:25:55 +000024#include "llvm/Support/Errno.h"
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000025
26// System includes - They have to be included after framework includes because
27// they define some
28// macros which collide with variable names in other modules
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000029// clang-format off
30#include <sys/types.h>
31#include <sys/ptrace.h>
32#include <sys/sysctl.h>
33#include <sys/wait.h>
34#include <uvm/uvm_prot.h>
35#include <elf.h>
36#include <util.h>
37// clang-format on
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000038
39using namespace lldb;
40using namespace lldb_private;
41using namespace lldb_private::process_netbsd;
42using namespace llvm;
43
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000044// Simple helper function to ensure flags are enabled on the given file
45// descriptor.
Zachary Turner97206d52017-05-12 04:51:55 +000046static Status EnsureFDFlags(int fd, int flags) {
47 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000048
49 int status = fcntl(fd, F_GETFL);
50 if (status == -1) {
51 error.SetErrorToErrno();
52 return error;
53 }
54
55 if (fcntl(fd, F_SETFL, status | flags) == -1) {
56 error.SetErrorToErrno();
57 return error;
58 }
59
60 return error;
61}
62
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000063// -----------------------------------------------------------------------------
64// Public Static Methods
65// -----------------------------------------------------------------------------
66
Pavel Labath82abefa2017-07-18 09:24:48 +000067llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
Pavel Labath96e600f2017-07-07 11:02:19 +000068NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
69 NativeDelegate &native_delegate,
70 MainLoop &mainloop) const {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000071 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
72
Pavel Labath96e600f2017-07-07 11:02:19 +000073 Status status;
74 ::pid_t pid = ProcessLauncherPosixFork()
75 .LaunchProcess(launch_info, status)
76 .GetProcessId();
77 LLDB_LOG(log, "pid = {0:x}", pid);
78 if (status.Fail()) {
79 LLDB_LOG(log, "failed to launch process: {0}", status);
80 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000081 }
82
Pavel Labath96e600f2017-07-07 11:02:19 +000083 // Wait for the child process to trap on its call to execve.
84 int wstatus;
85 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
86 assert(wpid == pid);
87 (void)wpid;
88 if (!WIFSTOPPED(wstatus)) {
89 LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
90 WaitStatus::Decode(wstatus));
91 return llvm::make_error<StringError>("Could not sync with inferior process",
92 llvm::inconvertibleErrorCode());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000093 }
Pavel Labath96e600f2017-07-07 11:02:19 +000094 LLDB_LOG(log, "inferior started, now in stopped state");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000095
Pavel Labath96e600f2017-07-07 11:02:19 +000096 ArchSpec arch;
97 if ((status = ResolveProcessArchitecture(pid, arch)).Fail())
98 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000099
Pavel Labath96e600f2017-07-07 11:02:19 +0000100 // Set the architecture to the exe architecture.
101 LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
102 arch.GetArchitectureName());
103
Pavel Labath82abefa2017-07-18 09:24:48 +0000104 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
Pavel Labath96e600f2017-07-07 11:02:19 +0000105 pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
106 arch, mainloop));
107
Pavel Labath82abefa2017-07-18 09:24:48 +0000108 status = process_up->ReinitializeThreads();
Pavel Labath96e600f2017-07-07 11:02:19 +0000109 if (status.Fail())
110 return status.ToError();
111
Pavel Labatha5be48b2017-10-17 15:52:16 +0000112 for (const auto &thread : process_up->m_threads)
113 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
Pavel Labath82abefa2017-07-18 09:24:48 +0000114 process_up->SetState(StateType::eStateStopped);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000115
Pavel Labath82abefa2017-07-18 09:24:48 +0000116 return std::move(process_up);
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000117}
118
Pavel Labath82abefa2017-07-18 09:24:48 +0000119llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
120NativeProcessNetBSD::Factory::Attach(
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000121 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
Pavel Labath96e600f2017-07-07 11:02:19 +0000122 MainLoop &mainloop) const {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000123 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
124 LLDB_LOG(log, "pid = {0:x}", pid);
125
126 // Retrieve the architecture for the running process.
Pavel Labath96e600f2017-07-07 11:02:19 +0000127 ArchSpec arch;
128 Status status = ResolveProcessArchitecture(pid, arch);
129 if (!status.Success())
130 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000131
Pavel Labath82abefa2017-07-18 09:24:48 +0000132 std::unique_ptr<NativeProcessNetBSD> process_up(
Pavel Labath96e600f2017-07-07 11:02:19 +0000133 new NativeProcessNetBSD(pid, -1, native_delegate, arch, mainloop));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000134
Pavel Labath82abefa2017-07-18 09:24:48 +0000135 status = process_up->Attach();
Pavel Labath96e600f2017-07-07 11:02:19 +0000136 if (!status.Success())
137 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000138
Pavel Labath82abefa2017-07-18 09:24:48 +0000139 return std::move(process_up);
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000140}
141
142// -----------------------------------------------------------------------------
143// Public Instance Methods
144// -----------------------------------------------------------------------------
145
Pavel Labath96e600f2017-07-07 11:02:19 +0000146NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd,
147 NativeDelegate &delegate,
148 const ArchSpec &arch,
149 MainLoop &mainloop)
150 : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
151 if (m_terminal_fd != -1) {
152 Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
153 assert(status.Success());
154 }
155
156 Status status;
157 m_sigchld_handle = mainloop.RegisterSignal(
158 SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
159 assert(m_sigchld_handle && status.Success());
160}
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000161
162// Handles all waitpid events from the inferior process.
163void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000164 switch (signal) {
165 case SIGTRAP:
166 return MonitorSIGTRAP(pid);
167 case SIGSTOP:
168 return MonitorSIGSTOP(pid);
169 default:
170 return MonitorSignal(pid, signal);
171 }
172}
173
Pavel Labath3508fc82017-06-19 12:47:50 +0000174void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000175 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
176
Pavel Labath3508fc82017-06-19 12:47:50 +0000177 LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000178
179 /* Stop Tracking All Threads attached to Process */
180 m_threads.clear();
181
Pavel Labath3508fc82017-06-19 12:47:50 +0000182 SetExitStatus(status, true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000183
184 // Notify delegate that our process has exited.
185 SetState(StateType::eStateExited, true);
186}
187
188void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000189 ptrace_siginfo_t info;
190
191 const auto siginfo_err =
192 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
193
194 // Get details on the signal raised.
195 if (siginfo_err.Success()) {
196 // Handle SIGSTOP from LLGS (LLDB GDB Server)
197 if (info.psi_siginfo.si_code == SI_USER &&
198 info.psi_siginfo.si_pid == ::getpid()) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000199 /* Stop Tracking all Threads attached to Process */
200 for (const auto &thread : m_threads) {
201 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000202 SIGSTOP, &info.psi_siginfo);
203 }
204 }
205 }
206}
207
208void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) {
209 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
210 ptrace_siginfo_t info;
211
212 const auto siginfo_err =
213 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
214
215 // Get details on the signal raised.
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000216 if (siginfo_err.Fail()) {
217 return;
218 }
219
220 switch (info.psi_siginfo.si_code) {
221 case TRAP_BRKPT:
Pavel Labatha5be48b2017-10-17 15:52:16 +0000222 for (const auto &thread : m_threads) {
223 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
224 FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000225 }
226 SetState(StateType::eStateStopped, true);
227 break;
228 case TRAP_TRACE:
Pavel Labatha5be48b2017-10-17 15:52:16 +0000229 for (const auto &thread : m_threads)
230 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000231 SetState(StateType::eStateStopped, true);
232 break;
233 case TRAP_EXEC: {
Zachary Turner97206d52017-05-12 04:51:55 +0000234 Status error = ReinitializeThreads();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000235 if (error.Fail()) {
236 SetState(StateType::eStateInvalid);
237 return;
238 }
239
240 // Let our delegate know we have just exec'd.
241 NotifyDidExec();
242
Pavel Labatha5be48b2017-10-17 15:52:16 +0000243 for (const auto &thread : m_threads)
244 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000245 SetState(StateType::eStateStopped, true);
246 } break;
247 case TRAP_DBREG: {
248 // If a watchpoint was hit, report it
249 uint32_t wp_index;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000250 Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
251 .GetRegisterContext()
Pavel Labathd37349f2017-11-10 11:05:49 +0000252 .GetWatchpointHitIndex(
Pavel Labatha5be48b2017-10-17 15:52:16 +0000253 wp_index, (uintptr_t)info.psi_siginfo.si_addr);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000254 if (error.Fail())
255 LLDB_LOG(log,
256 "received error while checking for watchpoint hits, pid = "
257 "{0}, LWP = {1}, error = {2}",
258 GetID(), info.psi_lwpid, error);
259 if (wp_index != LLDB_INVALID_INDEX32) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000260 for (const auto &thread : m_threads)
261 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
262 wp_index);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000263 SetState(StateType::eStateStopped, true);
264 break;
265 }
266
267 // If a breakpoint was hit, report it
268 uint32_t bp_index;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000269 error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
270 .GetRegisterContext()
Pavel Labathd37349f2017-11-10 11:05:49 +0000271 .GetHardwareBreakHitIndex(bp_index,
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000272 (uintptr_t)info.psi_siginfo.si_addr);
273 if (error.Fail())
274 LLDB_LOG(log,
275 "received error while checking for hardware "
276 "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
277 GetID(), info.psi_lwpid, error);
278 if (bp_index != LLDB_INVALID_INDEX32) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000279 for (const auto &thread : m_threads)
280 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000281 SetState(StateType::eStateStopped, true);
282 break;
283 }
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000284 } break;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000285 }
286}
287
288void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000289 ptrace_siginfo_t info;
290 const auto siginfo_err =
291 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
292
Pavel Labatha5be48b2017-10-17 15:52:16 +0000293 for (const auto &thread : m_threads) {
294 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000295 info.psi_siginfo.si_signo, &info.psi_siginfo);
296 }
297 SetState(StateType::eStateStopped, true);
298}
299
Zachary Turner97206d52017-05-12 04:51:55 +0000300Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
301 int data, int *result) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000302 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Zachary Turner97206d52017-05-12 04:51:55 +0000303 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000304 int ret;
305
306 errno = 0;
307 ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
308
309 if (ret == -1)
310 error.SetErrorToErrno();
311
312 if (result)
313 *result = ret;
314
315 LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
316
317 if (error.Fail())
318 LLDB_LOG(log, "ptrace() failed: {0}", error);
319
320 return error;
321}
322
Zachary Turner97206d52017-05-12 04:51:55 +0000323Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000324 uint32_t &actual_opcode_size) {
325 // FIXME put this behind a breakpoint protocol class that can be
326 // set per architecture. Need ARM, MIPS support here.
327 static const uint8_t g_i386_opcode[] = {0xCC};
328 switch (m_arch.GetMachine()) {
329 case llvm::Triple::x86_64:
330 actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode));
Zachary Turner97206d52017-05-12 04:51:55 +0000331 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000332 default:
333 assert(false && "CPU type not supported!");
Zachary Turner97206d52017-05-12 04:51:55 +0000334 return Status("CPU type not supported");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000335 }
336}
337
Zachary Turner97206d52017-05-12 04:51:55 +0000338Status
339NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000340 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS));
Zachary Turner97206d52017-05-12 04:51:55 +0000341 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000342 // Find out the size of a breakpoint (might depend on where we are in the
343 // code).
Pavel Labathd37349f2017-11-10 11:05:49 +0000344 NativeRegisterContext& context = thread.GetRegisterContext();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000345 uint32_t breakpoint_size = 0;
346 error = GetSoftwareBreakpointPCOffset(breakpoint_size);
347 if (error.Fail()) {
348 LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error);
349 return error;
350 } else
351 LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000352 // First try probing for a breakpoint at a software breakpoint location: PC
353 // - breakpoint size.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000354 const lldb::addr_t initial_pc_addr =
Pavel Labathd37349f2017-11-10 11:05:49 +0000355 context.GetPCfromBreakpointLocation();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000356 lldb::addr_t breakpoint_addr = initial_pc_addr;
357 if (breakpoint_size > 0) {
358 // Do not allow breakpoint probe to wrap around.
359 if (breakpoint_addr >= breakpoint_size)
360 breakpoint_addr -= breakpoint_size;
361 }
362 // Check if we stopped because of a breakpoint.
363 NativeBreakpointSP breakpoint_sp;
364 error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp);
365 if (!error.Success() || !breakpoint_sp) {
366 // We didn't find one at a software probe location. Nothing to do.
367 LLDB_LOG(log,
368 "pid {0} no lldb breakpoint found at current pc with "
369 "adjustment: {1}",
370 GetID(), breakpoint_addr);
Zachary Turner97206d52017-05-12 04:51:55 +0000371 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000372 }
373 // If the breakpoint is not a software breakpoint, nothing to do.
374 if (!breakpoint_sp->IsSoftwareBreakpoint()) {
375 LLDB_LOG(
376 log,
377 "pid {0} breakpoint found at {1:x}, not software, nothing to adjust",
378 GetID(), breakpoint_addr);
Zachary Turner97206d52017-05-12 04:51:55 +0000379 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000380 }
381 //
382 // We have a software breakpoint and need to adjust the PC.
383 //
384 // Sanity check.
385 if (breakpoint_size == 0) {
386 // Nothing to do! How did we get here?
387 LLDB_LOG(log,
388 "pid {0} breakpoint found at {1:x}, it is software, but the "
389 "size is zero, nothing to do (unexpected)",
390 GetID(), breakpoint_addr);
Zachary Turner97206d52017-05-12 04:51:55 +0000391 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000392 }
393 //
394 // We have a software breakpoint and need to adjust the PC.
395 //
396 // Sanity check.
397 if (breakpoint_size == 0) {
398 // Nothing to do! How did we get here?
399 LLDB_LOG(log,
400 "pid {0} breakpoint found at {1:x}, it is software, but the "
401 "size is zero, nothing to do (unexpected)",
402 GetID(), breakpoint_addr);
Zachary Turner97206d52017-05-12 04:51:55 +0000403 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000404 }
405 // Change the program counter.
406 LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),
407 thread.GetID(), initial_pc_addr, breakpoint_addr);
Pavel Labathd37349f2017-11-10 11:05:49 +0000408 error = context.SetPC(breakpoint_addr);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000409 if (error.Fail()) {
410 LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),
411 thread.GetID(), error);
412 return error;
413 }
414 return error;
415}
416
Zachary Turner97206d52017-05-12 04:51:55 +0000417Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000418 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
419 LLDB_LOG(log, "pid {0}", GetID());
420
Pavel Labatha5be48b2017-10-17 15:52:16 +0000421 const auto &thread = m_threads[0];
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000422 const ResumeAction *const action =
Pavel Labatha5be48b2017-10-17 15:52:16 +0000423 resume_actions.GetActionForThread(thread->GetID(), true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000424
425 if (action == nullptr) {
426 LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
Pavel Labatha5be48b2017-10-17 15:52:16 +0000427 thread->GetID());
Zachary Turner97206d52017-05-12 04:51:55 +0000428 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000429 }
430
Zachary Turner97206d52017-05-12 04:51:55 +0000431 Status error;
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000432
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000433 switch (action->state) {
434 case eStateRunning: {
435 // Run the thread, possibly feeding it the signal.
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000436 error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1,
437 action->signal);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000438 if (!error.Success())
439 return error;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000440 for (const auto &thread : m_threads)
441 static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000442 SetState(eStateRunning, true);
443 break;
444 }
445 case eStateStepping:
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000446 // Run the thread, possibly feeding it the signal.
447 error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1,
448 action->signal);
449 if (!error.Success())
450 return error;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000451 for (const auto &thread : m_threads)
452 static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000453 SetState(eStateStepping, true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000454 break;
455
456 case eStateSuspended:
457 case eStateStopped:
458 llvm_unreachable("Unexpected state");
459
460 default:
Zachary Turner97206d52017-05-12 04:51:55 +0000461 return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
462 "for pid %" PRIu64 ", tid %" PRIu64,
463 __FUNCTION__, StateAsCString(action->state), GetID(),
Pavel Labatha5be48b2017-10-17 15:52:16 +0000464 thread->GetID());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000465 }
466
Zachary Turner97206d52017-05-12 04:51:55 +0000467 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000468}
469
Zachary Turner97206d52017-05-12 04:51:55 +0000470Status NativeProcessNetBSD::Halt() {
471 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000472
473 if (kill(GetID(), SIGSTOP) != 0)
474 error.SetErrorToErrno();
475
476 return error;
477}
478
Zachary Turner97206d52017-05-12 04:51:55 +0000479Status NativeProcessNetBSD::Detach() {
480 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000481
482 // Stop monitoring the inferior.
483 m_sigchld_handle.reset();
484
485 // Tell ptrace to detach from the process.
486 if (GetID() == LLDB_INVALID_PROCESS_ID)
487 return error;
488
489 return PtraceWrapper(PT_DETACH, GetID());
490}
491
Zachary Turner97206d52017-05-12 04:51:55 +0000492Status NativeProcessNetBSD::Signal(int signo) {
493 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000494
495 if (kill(GetID(), signo))
496 error.SetErrorToErrno();
497
498 return error;
499}
500
Zachary Turner97206d52017-05-12 04:51:55 +0000501Status NativeProcessNetBSD::Kill() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000502 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
503 LLDB_LOG(log, "pid {0}", GetID());
504
Zachary Turner97206d52017-05-12 04:51:55 +0000505 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000506
507 switch (m_state) {
508 case StateType::eStateInvalid:
509 case StateType::eStateExited:
510 case StateType::eStateCrashed:
511 case StateType::eStateDetached:
512 case StateType::eStateUnloaded:
513 // Nothing to do - the process is already dead.
514 LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
515 StateAsCString(m_state));
516 return error;
517
518 case StateType::eStateConnected:
519 case StateType::eStateAttaching:
520 case StateType::eStateLaunching:
521 case StateType::eStateStopped:
522 case StateType::eStateRunning:
523 case StateType::eStateStepping:
524 case StateType::eStateSuspended:
525 // We can try to kill a process in these states.
526 break;
527 }
528
529 if (kill(GetID(), SIGKILL) != 0) {
530 error.SetErrorToErrno();
531 return error;
532 }
533
534 return error;
535}
536
Zachary Turner97206d52017-05-12 04:51:55 +0000537Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr,
538 MemoryRegionInfo &range_info) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000539
540 if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
541 // We're done.
Zachary Turner97206d52017-05-12 04:51:55 +0000542 return Status("unsupported");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000543 }
544
Zachary Turner97206d52017-05-12 04:51:55 +0000545 Status error = PopulateMemoryRegionCache();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000546 if (error.Fail()) {
547 return error;
548 }
549
550 lldb::addr_t prev_base_address = 0;
551 // FIXME start by finding the last region that is <= target address using
552 // binary search. Data is sorted.
553 // There can be a ton of regions on pthreads apps with lots of threads.
554 for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
555 ++it) {
556 MemoryRegionInfo &proc_entry_info = it->first;
557 // Sanity check assumption that memory map entries are ascending.
558 assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
559 "descending memory map entries detected, unexpected");
560 prev_base_address = proc_entry_info.GetRange().GetRangeBase();
561 UNUSED_IF_ASSERT_DISABLED(prev_base_address);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000562 // If the target address comes before this entry, indicate distance to
563 // next region.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000564 if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
565 range_info.GetRange().SetRangeBase(load_addr);
566 range_info.GetRange().SetByteSize(
567 proc_entry_info.GetRange().GetRangeBase() - load_addr);
568 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
569 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
570 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
571 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
572 return error;
573 } else if (proc_entry_info.GetRange().Contains(load_addr)) {
574 // The target address is within the memory region we're processing here.
575 range_info = proc_entry_info;
576 return error;
577 }
578 // The target memory address comes somewhere after the region we just
579 // parsed.
580 }
581 // If we made it here, we didn't find an entry that contained the given
582 // address. Return the
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000583 // load_addr as start and the amount of bytes betwwen load address and the
584 // end of the memory as size.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000585 range_info.GetRange().SetRangeBase(load_addr);
586 range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
587 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
588 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
589 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
590 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
591 return error;
592}
593
Zachary Turner97206d52017-05-12 04:51:55 +0000594Status NativeProcessNetBSD::PopulateMemoryRegionCache() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000595 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
596 // If our cache is empty, pull the latest. There should always be at least
597 // one memory region if memory region handling is supported.
598 if (!m_mem_region_cache.empty()) {
599 LLDB_LOG(log, "reusing {0} cached memory region entries",
600 m_mem_region_cache.size());
Zachary Turner97206d52017-05-12 04:51:55 +0000601 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000602 }
603
604 struct kinfo_vmentry *vm;
605 size_t count, i;
606 vm = kinfo_getvmmap(GetID(), &count);
607 if (vm == NULL) {
608 m_supports_mem_region = LazyBool::eLazyBoolNo;
Zachary Turner97206d52017-05-12 04:51:55 +0000609 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000610 error.SetErrorString("not supported");
611 return error;
612 }
613 for (i = 0; i < count; i++) {
614 MemoryRegionInfo info;
615 info.Clear();
616 info.GetRange().SetRangeBase(vm[i].kve_start);
617 info.GetRange().SetRangeEnd(vm[i].kve_end);
618 info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
619
620 if (vm[i].kve_protection & VM_PROT_READ)
621 info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
622 else
623 info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
624
625 if (vm[i].kve_protection & VM_PROT_WRITE)
626 info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
627 else
628 info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
629
630 if (vm[i].kve_protection & VM_PROT_EXECUTE)
631 info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
632 else
633 info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
634
635 if (vm[i].kve_path[0])
636 info.SetName(vm[i].kve_path);
637
638 m_mem_region_cache.emplace_back(
639 info, FileSpec(info.GetName().GetCString(), true));
640 }
641 free(vm);
642
643 if (m_mem_region_cache.empty()) {
644 // No entries after attempting to read them. This shouldn't happen.
645 // Assume we don't support map entries.
646 LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
647 "for memory region metadata retrieval");
648 m_supports_mem_region = LazyBool::eLazyBoolNo;
Zachary Turner97206d52017-05-12 04:51:55 +0000649 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000650 error.SetErrorString("not supported");
651 return error;
652 }
653 LLDB_LOG(log, "read {0} memory region entries from process {1}",
654 m_mem_region_cache.size(), GetID());
655 // We support memory retrieval, remember that.
656 m_supports_mem_region = LazyBool::eLazyBoolYes;
Zachary Turner97206d52017-05-12 04:51:55 +0000657 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000658}
659
Zachary Turner97206d52017-05-12 04:51:55 +0000660Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions,
661 lldb::addr_t &addr) {
662 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000663}
664
Zachary Turner97206d52017-05-12 04:51:55 +0000665Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) {
666 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000667}
668
669lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() {
670 // punt on this for now
671 return LLDB_INVALID_ADDRESS;
672}
673
674size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); }
675
Zachary Turner97206d52017-05-12 04:51:55 +0000676Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
677 bool hardware) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000678 if (hardware)
Zachary Turner97206d52017-05-12 04:51:55 +0000679 return Status("NativeProcessNetBSD does not support hardware breakpoints");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000680 else
681 return SetSoftwareBreakpoint(addr, size);
682}
683
Zachary Turner97206d52017-05-12 04:51:55 +0000684Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000685 size_t trap_opcode_size_hint, size_t &actual_opcode_size,
686 const uint8_t *&trap_opcode_bytes) {
687 static const uint8_t g_i386_opcode[] = {0xCC};
688
689 switch (m_arch.GetMachine()) {
690 case llvm::Triple::x86:
691 case llvm::Triple::x86_64:
692 trap_opcode_bytes = g_i386_opcode;
693 actual_opcode_size = sizeof(g_i386_opcode);
Zachary Turner97206d52017-05-12 04:51:55 +0000694 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000695 default:
696 assert(false && "CPU type not supported!");
Zachary Turner97206d52017-05-12 04:51:55 +0000697 return Status("CPU type not supported");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000698 }
699}
700
Zachary Turner97206d52017-05-12 04:51:55 +0000701Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path,
702 FileSpec &file_spec) {
703 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000704}
705
Zachary Turner97206d52017-05-12 04:51:55 +0000706Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
707 lldb::addr_t &load_addr) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000708 load_addr = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +0000709 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000710}
711
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000712void NativeProcessNetBSD::SigchldHandler() {
713 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
714 // Process all pending waitpid notifications.
715 int status;
Pavel Labathc1a6b122017-07-03 09:25:55 +0000716 ::pid_t wait_pid =
717 llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000718
719 if (wait_pid == 0)
720 return; // We are done.
721
722 if (wait_pid == -1) {
Zachary Turner97206d52017-05-12 04:51:55 +0000723 Status error(errno, eErrorTypePOSIX);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000724 LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
725 }
726
Pavel Labath3508fc82017-06-19 12:47:50 +0000727 WaitStatus wait_status = WaitStatus::Decode(status);
728 bool exited = wait_status.type == WaitStatus::Exit ||
729 (wait_status.type == WaitStatus::Signal &&
730 wait_pid == static_cast<::pid_t>(GetID()));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000731
732 LLDB_LOG(log,
Pavel Labath3508fc82017-06-19 12:47:50 +0000733 "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
734 GetID(), wait_pid, status, exited);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000735
736 if (exited)
Pavel Labath3508fc82017-06-19 12:47:50 +0000737 MonitorExited(wait_pid, wait_status);
738 else {
Kamil Rytarowski4bb74412017-06-20 13:51:06 +0000739 assert(wait_status.type == WaitStatus::Stop);
Pavel Labath3508fc82017-06-19 12:47:50 +0000740 MonitorCallback(wait_pid, wait_status.status);
741 }
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000742}
743
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000744bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000745 for (const auto &thread : m_threads) {
746 assert(thread && "thread list should not contain NULL threads");
747 if (thread->GetID() == thread_id) {
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000748 // We have this thread.
749 return true;
750 }
751 }
752
753 // We don't have this thread.
754 return false;
755}
756
Pavel Labatha5be48b2017-10-17 15:52:16 +0000757NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000758
759 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
760 LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
761
762 assert(!HasThreadNoLock(thread_id) &&
763 "attempted to add a thread by id that already exists");
764
765 // If this is the first thread, save it as the current thread
766 if (m_threads.empty())
767 SetCurrentThreadID(thread_id);
768
Pavel Labatha5be48b2017-10-17 15:52:16 +0000769 m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
770 return static_cast<NativeThreadNetBSD &>(*m_threads.back());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000771}
772
Pavel Labath96e600f2017-07-07 11:02:19 +0000773Status NativeProcessNetBSD::Attach() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000774 // Attach to the requested process.
775 // An attach will cause the thread to stop with a SIGSTOP.
Pavel Labath96e600f2017-07-07 11:02:19 +0000776 Status status = PtraceWrapper(PT_ATTACH, m_pid);
777 if (status.Fail())
778 return status;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000779
Pavel Labath96e600f2017-07-07 11:02:19 +0000780 int wstatus;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000781 // Need to use WALLSIG otherwise we receive an error with errno=ECHLD
782 // At this point we should have a thread stopped if waitpid succeeds.
Pavel Labath96e600f2017-07-07 11:02:19 +0000783 if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0)
784 return Status(errno, eErrorTypePOSIX);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000785
786 /* Initialize threads */
Pavel Labath96e600f2017-07-07 11:02:19 +0000787 status = ReinitializeThreads();
788 if (status.Fail())
789 return status;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000790
Pavel Labatha5be48b2017-10-17 15:52:16 +0000791 for (const auto &thread : m_threads)
792 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000793
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000794 // Let our process instance know the thread has stopped.
795 SetState(StateType::eStateStopped);
Pavel Labath96e600f2017-07-07 11:02:19 +0000796 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000797}
798
Zachary Turner97206d52017-05-12 04:51:55 +0000799Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf,
800 size_t size, size_t &bytes_read) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000801 unsigned char *dst = static_cast<unsigned char *>(buf);
802 struct ptrace_io_desc io;
803
804 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
805 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
806
807 bytes_read = 0;
808 io.piod_op = PIOD_READ_D;
809 io.piod_len = size;
810
811 do {
812 io.piod_offs = (void *)(addr + bytes_read);
813 io.piod_addr = dst + bytes_read;
814
Zachary Turner97206d52017-05-12 04:51:55 +0000815 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000816 if (error.Fail())
817 return error;
818
819 bytes_read = io.piod_len;
820 io.piod_len = size - bytes_read;
821 } while (bytes_read < size);
822
Zachary Turner97206d52017-05-12 04:51:55 +0000823 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000824}
825
Zachary Turner97206d52017-05-12 04:51:55 +0000826Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf,
827 size_t size,
828 size_t &bytes_read) {
829 Status error = ReadMemory(addr, buf, size, bytes_read);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000830 if (error.Fail())
831 return error;
832 return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size);
833}
834
Zachary Turner97206d52017-05-12 04:51:55 +0000835Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf,
836 size_t size, size_t &bytes_written) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000837 const unsigned char *src = static_cast<const unsigned char *>(buf);
Zachary Turner97206d52017-05-12 04:51:55 +0000838 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000839 struct ptrace_io_desc io;
840
841 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
842 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
843
844 bytes_written = 0;
845 io.piod_op = PIOD_WRITE_D;
846 io.piod_len = size;
847
848 do {
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000849 io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000850 io.piod_offs = (void *)(addr + bytes_written);
851
Zachary Turner97206d52017-05-12 04:51:55 +0000852 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000853 if (error.Fail())
854 return error;
855
856 bytes_written = io.piod_len;
857 io.piod_len = size - bytes_written;
858 } while (bytes_written < size);
859
860 return error;
861}
862
863llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
864NativeProcessNetBSD::GetAuxvData() const {
865 /*
866 * ELF_AUX_ENTRIES is currently restricted to kernel
867 * (<sys/exec_elf.h> r. 1.155 specifies 15)
868 *
869 * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
870 * information isn't needed.
871 */
872 size_t auxv_size = 100 * sizeof(AuxInfo);
873
Pavel Labathe831bb32018-01-15 11:32:43 +0000874 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
Pavel Labathdbda2852018-01-15 11:50:05 +0000875 llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000876
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000877 struct ptrace_io_desc io;
878 io.piod_op = PIOD_READ_AUXV;
879 io.piod_offs = 0;
Pavel Labathe831bb32018-01-15 11:32:43 +0000880 io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000881 io.piod_len = auxv_size;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000882
Zachary Turner97206d52017-05-12 04:51:55 +0000883 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000884
885 if (error.Fail())
886 return std::error_code(error.GetError(), std::generic_category());
887
888 if (io.piod_len < 1)
889 return std::error_code(ECANCELED, std::generic_category());
890
Pavel Labathe831bb32018-01-15 11:32:43 +0000891 return std::move(buf);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000892}
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000893
Zachary Turner97206d52017-05-12 04:51:55 +0000894Status NativeProcessNetBSD::ReinitializeThreads() {
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000895 // Clear old threads
896 m_threads.clear();
897
898 // Initialize new thread
899 struct ptrace_lwpinfo info = {};
Zachary Turner97206d52017-05-12 04:51:55 +0000900 Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000901 if (error.Fail()) {
902 return error;
903 }
904 // Reinitialize from scratch threads and register them in process
905 while (info.pl_lwpid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000906 AddThread(info.pl_lwpid);
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000907 error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
908 if (error.Fail()) {
909 return error;
910 }
911 }
912
913 return error;
914}