blob: a1b7d7df4553cb76d9fe6838b47248842ea39865 [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
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000012
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000013
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000014#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000015#include "lldb/Host/HostProcess.h"
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000016#include "lldb/Host/common/NativeRegisterContext.h"
17#include "lldb/Host/posix/ProcessLauncherPosixFork.h"
18#include "lldb/Target/Process.h"
Pavel Labathd821c992018-08-07 11:07:21 +000019#include "lldb/Utility/State.h"
Pavel Labathc1a6b122017-07-03 09:25:55 +000020#include "llvm/Support/Errno.h"
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000021
22// System includes - They have to be included after framework includes because
Adrian Prantl05097242018-04-30 16:49:04 +000023// they define some macros which collide with variable names in other modules
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000024// clang-format off
25#include <sys/types.h>
26#include <sys/ptrace.h>
27#include <sys/sysctl.h>
28#include <sys/wait.h>
29#include <uvm/uvm_prot.h>
30#include <elf.h>
31#include <util.h>
32// clang-format on
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000033
34using namespace lldb;
35using namespace lldb_private;
36using namespace lldb_private::process_netbsd;
37using namespace llvm;
38
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000039// Simple helper function to ensure flags are enabled on the given file
40// descriptor.
Zachary Turner97206d52017-05-12 04:51:55 +000041static Status EnsureFDFlags(int fd, int flags) {
42 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000043
44 int status = fcntl(fd, F_GETFL);
45 if (status == -1) {
46 error.SetErrorToErrno();
47 return error;
48 }
49
50 if (fcntl(fd, F_SETFL, status | flags) == -1) {
51 error.SetErrorToErrno();
52 return error;
53 }
54
55 return error;
56}
57
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000058// -----------------------------------------------------------------------------
59// Public Static Methods
60// -----------------------------------------------------------------------------
61
Pavel Labath82abefa2017-07-18 09:24:48 +000062llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
Pavel Labath96e600f2017-07-07 11:02:19 +000063NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info,
64 NativeDelegate &native_delegate,
65 MainLoop &mainloop) const {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000066 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
67
Pavel Labath96e600f2017-07-07 11:02:19 +000068 Status status;
69 ::pid_t pid = ProcessLauncherPosixFork()
70 .LaunchProcess(launch_info, status)
71 .GetProcessId();
72 LLDB_LOG(log, "pid = {0:x}", pid);
73 if (status.Fail()) {
74 LLDB_LOG(log, "failed to launch process: {0}", status);
75 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000076 }
77
Pavel Labath96e600f2017-07-07 11:02:19 +000078 // Wait for the child process to trap on its call to execve.
79 int wstatus;
80 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
81 assert(wpid == pid);
82 (void)wpid;
83 if (!WIFSTOPPED(wstatus)) {
84 LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
85 WaitStatus::Decode(wstatus));
86 return llvm::make_error<StringError>("Could not sync with inferior process",
87 llvm::inconvertibleErrorCode());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000088 }
Pavel Labath96e600f2017-07-07 11:02:19 +000089 LLDB_LOG(log, "inferior started, now in stopped state");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000090
Pavel Labath36e82202018-01-29 10:46:00 +000091 ProcessInstanceInfo Info;
92 if (!Host::GetProcessInfo(pid, Info)) {
93 return llvm::make_error<StringError>("Cannot get process architecture",
94 llvm::inconvertibleErrorCode());
95 }
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000096
Pavel Labath96e600f2017-07-07 11:02:19 +000097 // Set the architecture to the exe architecture.
98 LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
Pavel Labath36e82202018-01-29 10:46:00 +000099 Info.GetArchitecture().GetArchitectureName());
Pavel Labath96e600f2017-07-07 11:02:19 +0000100
Pavel Labath82abefa2017-07-18 09:24:48 +0000101 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
Pavel Labath96e600f2017-07-07 11:02:19 +0000102 pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
Pavel Labath36e82202018-01-29 10:46:00 +0000103 Info.GetArchitecture(), mainloop));
Pavel Labath96e600f2017-07-07 11:02:19 +0000104
Pavel Labath82abefa2017-07-18 09:24:48 +0000105 status = process_up->ReinitializeThreads();
Pavel Labath96e600f2017-07-07 11:02:19 +0000106 if (status.Fail())
107 return status.ToError();
108
Pavel Labatha5be48b2017-10-17 15:52:16 +0000109 for (const auto &thread : process_up->m_threads)
110 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
Kamil Rytarowski8a4bf062018-02-05 13:16:22 +0000111 process_up->SetState(StateType::eStateStopped, false);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000112
Pavel Labath82abefa2017-07-18 09:24:48 +0000113 return std::move(process_up);
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000114}
115
Pavel Labath82abefa2017-07-18 09:24:48 +0000116llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
117NativeProcessNetBSD::Factory::Attach(
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000118 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
Pavel Labath96e600f2017-07-07 11:02:19 +0000119 MainLoop &mainloop) const {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000120 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
121 LLDB_LOG(log, "pid = {0:x}", pid);
122
123 // Retrieve the architecture for the running process.
Pavel Labath36e82202018-01-29 10:46:00 +0000124 ProcessInstanceInfo Info;
125 if (!Host::GetProcessInfo(pid, Info)) {
126 return llvm::make_error<StringError>("Cannot get process architecture",
127 llvm::inconvertibleErrorCode());
128 }
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000129
Pavel Labath36e82202018-01-29 10:46:00 +0000130 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
131 pid, -1, native_delegate, Info.GetArchitecture(), mainloop));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000132
Pavel Labath02d4e502018-01-29 11:10:21 +0000133 Status status = process_up->Attach();
Pavel Labath96e600f2017-07-07 11:02:19 +0000134 if (!status.Success())
135 return status.ToError();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000136
Pavel Labath82abefa2017-07-18 09:24:48 +0000137 return std::move(process_up);
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +0000138}
139
140// -----------------------------------------------------------------------------
141// Public Instance Methods
142// -----------------------------------------------------------------------------
143
Pavel Labath96e600f2017-07-07 11:02:19 +0000144NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd,
145 NativeDelegate &delegate,
146 const ArchSpec &arch,
147 MainLoop &mainloop)
148 : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
149 if (m_terminal_fd != -1) {
150 Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
151 assert(status.Success());
152 }
153
154 Status status;
155 m_sigchld_handle = mainloop.RegisterSignal(
156 SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
157 assert(m_sigchld_handle && status.Success());
158}
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000159
160// Handles all waitpid events from the inferior process.
161void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000162 switch (signal) {
163 case SIGTRAP:
164 return MonitorSIGTRAP(pid);
165 case SIGSTOP:
166 return MonitorSIGSTOP(pid);
167 default:
168 return MonitorSignal(pid, signal);
169 }
170}
171
Pavel Labath3508fc82017-06-19 12:47:50 +0000172void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000173 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
174
Pavel Labath3508fc82017-06-19 12:47:50 +0000175 LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000176
177 /* Stop Tracking All Threads attached to Process */
178 m_threads.clear();
179
Pavel Labath3508fc82017-06-19 12:47:50 +0000180 SetExitStatus(status, true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000181
182 // Notify delegate that our process has exited.
183 SetState(StateType::eStateExited, true);
184}
185
186void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000187 ptrace_siginfo_t info;
188
189 const auto siginfo_err =
190 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
191
192 // Get details on the signal raised.
193 if (siginfo_err.Success()) {
194 // Handle SIGSTOP from LLGS (LLDB GDB Server)
195 if (info.psi_siginfo.si_code == SI_USER &&
196 info.psi_siginfo.si_pid == ::getpid()) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000197 /* Stop Tracking all Threads attached to Process */
198 for (const auto &thread : m_threads) {
199 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000200 SIGSTOP, &info.psi_siginfo);
201 }
202 }
203 }
204}
205
206void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) {
207 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
208 ptrace_siginfo_t info;
209
210 const auto siginfo_err =
211 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
212
213 // Get details on the signal raised.
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000214 if (siginfo_err.Fail()) {
215 return;
216 }
217
218 switch (info.psi_siginfo.si_code) {
219 case TRAP_BRKPT:
Pavel Labatha5be48b2017-10-17 15:52:16 +0000220 for (const auto &thread : m_threads) {
221 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
222 FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000223 }
224 SetState(StateType::eStateStopped, true);
225 break;
226 case TRAP_TRACE:
Pavel Labatha5be48b2017-10-17 15:52:16 +0000227 for (const auto &thread : m_threads)
228 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000229 SetState(StateType::eStateStopped, true);
230 break;
231 case TRAP_EXEC: {
Zachary Turner97206d52017-05-12 04:51:55 +0000232 Status error = ReinitializeThreads();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000233 if (error.Fail()) {
234 SetState(StateType::eStateInvalid);
235 return;
236 }
237
238 // Let our delegate know we have just exec'd.
239 NotifyDidExec();
240
Pavel Labatha5be48b2017-10-17 15:52:16 +0000241 for (const auto &thread : m_threads)
242 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000243 SetState(StateType::eStateStopped, true);
244 } break;
245 case TRAP_DBREG: {
246 // If a watchpoint was hit, report it
247 uint32_t wp_index;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000248 Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
249 .GetRegisterContext()
Pavel Labathd37349f2017-11-10 11:05:49 +0000250 .GetWatchpointHitIndex(
Pavel Labatha5be48b2017-10-17 15:52:16 +0000251 wp_index, (uintptr_t)info.psi_siginfo.si_addr);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000252 if (error.Fail())
253 LLDB_LOG(log,
254 "received error while checking for watchpoint hits, pid = "
255 "{0}, LWP = {1}, error = {2}",
256 GetID(), info.psi_lwpid, error);
257 if (wp_index != LLDB_INVALID_INDEX32) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000258 for (const auto &thread : m_threads)
259 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
260 wp_index);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000261 SetState(StateType::eStateStopped, true);
262 break;
263 }
264
265 // If a breakpoint was hit, report it
266 uint32_t bp_index;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000267 error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
268 .GetRegisterContext()
Pavel Labathd37349f2017-11-10 11:05:49 +0000269 .GetHardwareBreakHitIndex(bp_index,
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000270 (uintptr_t)info.psi_siginfo.si_addr);
271 if (error.Fail())
272 LLDB_LOG(log,
273 "received error while checking for hardware "
274 "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
275 GetID(), info.psi_lwpid, error);
276 if (bp_index != LLDB_INVALID_INDEX32) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000277 for (const auto &thread : m_threads)
278 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000279 SetState(StateType::eStateStopped, true);
280 break;
281 }
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000282 } break;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000283 }
284}
285
286void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000287 ptrace_siginfo_t info;
288 const auto siginfo_err =
289 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
290
Pavel Labatha5be48b2017-10-17 15:52:16 +0000291 for (const auto &thread : m_threads) {
292 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000293 info.psi_siginfo.si_signo, &info.psi_siginfo);
294 }
295 SetState(StateType::eStateStopped, true);
296}
297
Zachary Turner97206d52017-05-12 04:51:55 +0000298Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
299 int data, int *result) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000300 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Zachary Turner97206d52017-05-12 04:51:55 +0000301 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000302 int ret;
303
304 errno = 0;
305 ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
306
307 if (ret == -1)
308 error.SetErrorToErrno();
309
310 if (result)
311 *result = ret;
312
313 LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
314
315 if (error.Fail())
316 LLDB_LOG(log, "ptrace() failed: {0}", error);
317
318 return error;
319}
320
Zachary Turner97206d52017-05-12 04:51:55 +0000321Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000322 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
323 LLDB_LOG(log, "pid {0}", GetID());
324
Pavel Labatha5be48b2017-10-17 15:52:16 +0000325 const auto &thread = m_threads[0];
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000326 const ResumeAction *const action =
Pavel Labatha5be48b2017-10-17 15:52:16 +0000327 resume_actions.GetActionForThread(thread->GetID(), true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000328
329 if (action == nullptr) {
330 LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
Pavel Labatha5be48b2017-10-17 15:52:16 +0000331 thread->GetID());
Zachary Turner97206d52017-05-12 04:51:55 +0000332 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000333 }
334
Zachary Turner97206d52017-05-12 04:51:55 +0000335 Status error;
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000336
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000337 switch (action->state) {
338 case eStateRunning: {
339 // Run the thread, possibly feeding it the signal.
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000340 error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1,
341 action->signal);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000342 if (!error.Success())
343 return error;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000344 for (const auto &thread : m_threads)
345 static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000346 SetState(eStateRunning, true);
347 break;
348 }
349 case eStateStepping:
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000350 // Run the thread, possibly feeding it the signal.
351 error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1,
352 action->signal);
353 if (!error.Success())
354 return error;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000355 for (const auto &thread : m_threads)
356 static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000357 SetState(eStateStepping, true);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000358 break;
359
360 case eStateSuspended:
361 case eStateStopped:
362 llvm_unreachable("Unexpected state");
363
364 default:
Zachary Turner97206d52017-05-12 04:51:55 +0000365 return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
366 "for pid %" PRIu64 ", tid %" PRIu64,
367 __FUNCTION__, StateAsCString(action->state), GetID(),
Pavel Labatha5be48b2017-10-17 15:52:16 +0000368 thread->GetID());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000369 }
370
Zachary Turner97206d52017-05-12 04:51:55 +0000371 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000372}
373
Zachary Turner97206d52017-05-12 04:51:55 +0000374Status NativeProcessNetBSD::Halt() {
375 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000376
377 if (kill(GetID(), SIGSTOP) != 0)
378 error.SetErrorToErrno();
379
380 return error;
381}
382
Zachary Turner97206d52017-05-12 04:51:55 +0000383Status NativeProcessNetBSD::Detach() {
384 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000385
386 // Stop monitoring the inferior.
387 m_sigchld_handle.reset();
388
389 // Tell ptrace to detach from the process.
390 if (GetID() == LLDB_INVALID_PROCESS_ID)
391 return error;
392
393 return PtraceWrapper(PT_DETACH, GetID());
394}
395
Zachary Turner97206d52017-05-12 04:51:55 +0000396Status NativeProcessNetBSD::Signal(int signo) {
397 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000398
399 if (kill(GetID(), signo))
400 error.SetErrorToErrno();
401
402 return error;
403}
404
Zachary Turner97206d52017-05-12 04:51:55 +0000405Status NativeProcessNetBSD::Kill() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000406 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
407 LLDB_LOG(log, "pid {0}", GetID());
408
Zachary Turner97206d52017-05-12 04:51:55 +0000409 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000410
411 switch (m_state) {
412 case StateType::eStateInvalid:
413 case StateType::eStateExited:
414 case StateType::eStateCrashed:
415 case StateType::eStateDetached:
416 case StateType::eStateUnloaded:
417 // Nothing to do - the process is already dead.
418 LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
419 StateAsCString(m_state));
420 return error;
421
422 case StateType::eStateConnected:
423 case StateType::eStateAttaching:
424 case StateType::eStateLaunching:
425 case StateType::eStateStopped:
426 case StateType::eStateRunning:
427 case StateType::eStateStepping:
428 case StateType::eStateSuspended:
429 // We can try to kill a process in these states.
430 break;
431 }
432
433 if (kill(GetID(), SIGKILL) != 0) {
434 error.SetErrorToErrno();
435 return error;
436 }
437
438 return error;
439}
440
Zachary Turner97206d52017-05-12 04:51:55 +0000441Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr,
442 MemoryRegionInfo &range_info) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000443
444 if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
445 // We're done.
Zachary Turner97206d52017-05-12 04:51:55 +0000446 return Status("unsupported");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000447 }
448
Zachary Turner97206d52017-05-12 04:51:55 +0000449 Status error = PopulateMemoryRegionCache();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000450 if (error.Fail()) {
451 return error;
452 }
453
454 lldb::addr_t prev_base_address = 0;
455 // FIXME start by finding the last region that is <= target address using
456 // binary search. Data is sorted.
457 // There can be a ton of regions on pthreads apps with lots of threads.
458 for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
459 ++it) {
460 MemoryRegionInfo &proc_entry_info = it->first;
461 // Sanity check assumption that memory map entries are ascending.
462 assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
463 "descending memory map entries detected, unexpected");
464 prev_base_address = proc_entry_info.GetRange().GetRangeBase();
465 UNUSED_IF_ASSERT_DISABLED(prev_base_address);
Adrian Prantl05097242018-04-30 16:49:04 +0000466 // If the target address comes before this entry, indicate distance to next
467 // region.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000468 if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
469 range_info.GetRange().SetRangeBase(load_addr);
470 range_info.GetRange().SetByteSize(
471 proc_entry_info.GetRange().GetRangeBase() - load_addr);
472 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
473 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
474 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
475 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
476 return error;
477 } else if (proc_entry_info.GetRange().Contains(load_addr)) {
478 // The target address is within the memory region we're processing here.
479 range_info = proc_entry_info;
480 return error;
481 }
482 // The target memory address comes somewhere after the region we just
483 // parsed.
484 }
485 // If we made it here, we didn't find an entry that contained the given
Adrian Prantl05097242018-04-30 16:49:04 +0000486 // address. Return the load_addr as start and the amount of bytes betwwen
487 // load address and the end of the memory as size.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000488 range_info.GetRange().SetRangeBase(load_addr);
489 range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
490 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
491 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
492 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
493 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
494 return error;
495}
496
Zachary Turner97206d52017-05-12 04:51:55 +0000497Status NativeProcessNetBSD::PopulateMemoryRegionCache() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000498 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
499 // If our cache is empty, pull the latest. There should always be at least
500 // one memory region if memory region handling is supported.
501 if (!m_mem_region_cache.empty()) {
502 LLDB_LOG(log, "reusing {0} cached memory region entries",
503 m_mem_region_cache.size());
Zachary Turner97206d52017-05-12 04:51:55 +0000504 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000505 }
506
507 struct kinfo_vmentry *vm;
508 size_t count, i;
509 vm = kinfo_getvmmap(GetID(), &count);
510 if (vm == NULL) {
511 m_supports_mem_region = LazyBool::eLazyBoolNo;
Zachary Turner97206d52017-05-12 04:51:55 +0000512 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000513 error.SetErrorString("not supported");
514 return error;
515 }
516 for (i = 0; i < count; i++) {
517 MemoryRegionInfo info;
518 info.Clear();
519 info.GetRange().SetRangeBase(vm[i].kve_start);
520 info.GetRange().SetRangeEnd(vm[i].kve_end);
521 info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
522
523 if (vm[i].kve_protection & VM_PROT_READ)
524 info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
525 else
526 info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
527
528 if (vm[i].kve_protection & VM_PROT_WRITE)
529 info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
530 else
531 info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
532
533 if (vm[i].kve_protection & VM_PROT_EXECUTE)
534 info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
535 else
536 info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
537
538 if (vm[i].kve_path[0])
539 info.SetName(vm[i].kve_path);
540
541 m_mem_region_cache.emplace_back(
Kamil Rytarowskia0a44e92018-11-04 16:53:16 +0000542 info, FileSpec(info.GetName().GetCString()));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000543 }
544 free(vm);
545
546 if (m_mem_region_cache.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000547 // No entries after attempting to read them. This shouldn't happen. Assume
548 // we don't support map entries.
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000549 LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
550 "for memory region metadata retrieval");
551 m_supports_mem_region = LazyBool::eLazyBoolNo;
Zachary Turner97206d52017-05-12 04:51:55 +0000552 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000553 error.SetErrorString("not supported");
554 return error;
555 }
556 LLDB_LOG(log, "read {0} memory region entries from process {1}",
557 m_mem_region_cache.size(), GetID());
558 // We support memory retrieval, remember that.
559 m_supports_mem_region = LazyBool::eLazyBoolYes;
Zachary Turner97206d52017-05-12 04:51:55 +0000560 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000561}
562
Zachary Turner97206d52017-05-12 04:51:55 +0000563Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions,
564 lldb::addr_t &addr) {
565 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000566}
567
Zachary Turner97206d52017-05-12 04:51:55 +0000568Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) {
569 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000570}
571
572lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() {
573 // punt on this for now
574 return LLDB_INVALID_ADDRESS;
575}
576
577size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); }
578
Zachary Turner97206d52017-05-12 04:51:55 +0000579Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
580 bool hardware) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000581 if (hardware)
Zachary Turner97206d52017-05-12 04:51:55 +0000582 return Status("NativeProcessNetBSD does not support hardware breakpoints");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000583 else
584 return SetSoftwareBreakpoint(addr, size);
585}
586
Zachary Turner97206d52017-05-12 04:51:55 +0000587Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path,
588 FileSpec &file_spec) {
589 return Status("Unimplemented");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000590}
591
Zachary Turner97206d52017-05-12 04:51:55 +0000592Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
593 lldb::addr_t &load_addr) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000594 load_addr = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +0000595 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000596}
597
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000598void NativeProcessNetBSD::SigchldHandler() {
599 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
600 // Process all pending waitpid notifications.
601 int status;
Pavel Labathc1a6b122017-07-03 09:25:55 +0000602 ::pid_t wait_pid =
603 llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000604
605 if (wait_pid == 0)
606 return; // We are done.
607
608 if (wait_pid == -1) {
Zachary Turner97206d52017-05-12 04:51:55 +0000609 Status error(errno, eErrorTypePOSIX);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000610 LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
611 }
612
Pavel Labath3508fc82017-06-19 12:47:50 +0000613 WaitStatus wait_status = WaitStatus::Decode(status);
614 bool exited = wait_status.type == WaitStatus::Exit ||
615 (wait_status.type == WaitStatus::Signal &&
616 wait_pid == static_cast<::pid_t>(GetID()));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000617
618 LLDB_LOG(log,
Pavel Labath3508fc82017-06-19 12:47:50 +0000619 "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
620 GetID(), wait_pid, status, exited);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000621
622 if (exited)
Pavel Labath3508fc82017-06-19 12:47:50 +0000623 MonitorExited(wait_pid, wait_status);
624 else {
Kamil Rytarowski4bb74412017-06-20 13:51:06 +0000625 assert(wait_status.type == WaitStatus::Stop);
Pavel Labath3508fc82017-06-19 12:47:50 +0000626 MonitorCallback(wait_pid, wait_status.status);
627 }
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000628}
629
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000630bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000631 for (const auto &thread : m_threads) {
632 assert(thread && "thread list should not contain NULL threads");
633 if (thread->GetID() == thread_id) {
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000634 // We have this thread.
635 return true;
636 }
637 }
638
639 // We don't have this thread.
640 return false;
641}
642
Pavel Labatha5be48b2017-10-17 15:52:16 +0000643NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000644
645 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
646 LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
647
648 assert(!HasThreadNoLock(thread_id) &&
649 "attempted to add a thread by id that already exists");
650
651 // If this is the first thread, save it as the current thread
652 if (m_threads.empty())
653 SetCurrentThreadID(thread_id);
654
Pavel Labatha5be48b2017-10-17 15:52:16 +0000655 m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
656 return static_cast<NativeThreadNetBSD &>(*m_threads.back());
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000657}
658
Pavel Labath96e600f2017-07-07 11:02:19 +0000659Status NativeProcessNetBSD::Attach() {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000660 // Attach to the requested process.
661 // An attach will cause the thread to stop with a SIGSTOP.
Pavel Labath96e600f2017-07-07 11:02:19 +0000662 Status status = PtraceWrapper(PT_ATTACH, m_pid);
663 if (status.Fail())
664 return status;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000665
Pavel Labath96e600f2017-07-07 11:02:19 +0000666 int wstatus;
Adrian Prantl05097242018-04-30 16:49:04 +0000667 // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this
668 // point we should have a thread stopped if waitpid succeeds.
Pavel Labath96e600f2017-07-07 11:02:19 +0000669 if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0)
670 return Status(errno, eErrorTypePOSIX);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000671
672 /* Initialize threads */
Pavel Labath96e600f2017-07-07 11:02:19 +0000673 status = ReinitializeThreads();
674 if (status.Fail())
675 return status;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000676
Pavel Labatha5be48b2017-10-17 15:52:16 +0000677 for (const auto &thread : m_threads)
678 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000679
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000680 // Let our process instance know the thread has stopped.
681 SetState(StateType::eStateStopped);
Pavel Labath96e600f2017-07-07 11:02:19 +0000682 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000683}
684
Zachary Turner97206d52017-05-12 04:51:55 +0000685Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf,
686 size_t size, size_t &bytes_read) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000687 unsigned char *dst = static_cast<unsigned char *>(buf);
688 struct ptrace_io_desc io;
689
690 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
691 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
692
693 bytes_read = 0;
694 io.piod_op = PIOD_READ_D;
695 io.piod_len = size;
696
697 do {
698 io.piod_offs = (void *)(addr + bytes_read);
699 io.piod_addr = dst + bytes_read;
700
Zachary Turner97206d52017-05-12 04:51:55 +0000701 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000702 if (error.Fail())
703 return error;
704
705 bytes_read = io.piod_len;
706 io.piod_len = size - bytes_read;
707 } while (bytes_read < size);
708
Zachary Turner97206d52017-05-12 04:51:55 +0000709 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000710}
711
Zachary Turner97206d52017-05-12 04:51:55 +0000712Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf,
713 size_t size, size_t &bytes_written) {
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000714 const unsigned char *src = static_cast<const unsigned char *>(buf);
Zachary Turner97206d52017-05-12 04:51:55 +0000715 Status error;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000716 struct ptrace_io_desc io;
717
718 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY));
719 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
720
721 bytes_written = 0;
722 io.piod_op = PIOD_WRITE_D;
723 io.piod_len = size;
724
725 do {
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000726 io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written));
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000727 io.piod_offs = (void *)(addr + bytes_written);
728
Zachary Turner97206d52017-05-12 04:51:55 +0000729 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000730 if (error.Fail())
731 return error;
732
733 bytes_written = io.piod_len;
734 io.piod_len = size - bytes_written;
735 } while (bytes_written < size);
736
737 return error;
738}
739
740llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
741NativeProcessNetBSD::GetAuxvData() const {
742 /*
743 * ELF_AUX_ENTRIES is currently restricted to kernel
744 * (<sys/exec_elf.h> r. 1.155 specifies 15)
745 *
746 * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
747 * information isn't needed.
748 */
749 size_t auxv_size = 100 * sizeof(AuxInfo);
750
Pavel Labathe831bb32018-01-15 11:32:43 +0000751 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
Pavel Labathdbda2852018-01-15 11:50:05 +0000752 llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000753
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000754 struct ptrace_io_desc io;
755 io.piod_op = PIOD_READ_AUXV;
756 io.piod_offs = 0;
Pavel Labathe831bb32018-01-15 11:32:43 +0000757 io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
Kamil Rytarowski269eec02017-05-24 23:59:50 +0000758 io.piod_len = auxv_size;
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000759
Zachary Turner97206d52017-05-12 04:51:55 +0000760 Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000761
762 if (error.Fail())
763 return std::error_code(error.GetError(), std::generic_category());
764
765 if (io.piod_len < 1)
766 return std::error_code(ECANCELED, std::generic_category());
767
Pavel Labathe831bb32018-01-15 11:32:43 +0000768 return std::move(buf);
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000769}
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000770
Zachary Turner97206d52017-05-12 04:51:55 +0000771Status NativeProcessNetBSD::ReinitializeThreads() {
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000772 // Clear old threads
773 m_threads.clear();
774
775 // Initialize new thread
776 struct ptrace_lwpinfo info = {};
Zachary Turner97206d52017-05-12 04:51:55 +0000777 Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000778 if (error.Fail()) {
779 return error;
780 }
781 // Reinitialize from scratch threads and register them in process
782 while (info.pl_lwpid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000783 AddThread(info.pl_lwpid);
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000784 error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
785 if (error.Fail()) {
786 return error;
787 }
788 }
789
790 return error;
791}