blob: dfebbcca283577b636e3e59c29bd095eda7550f9 [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Johnny Chen9ed5b492012-01-05 21:48:15 +00006//
7//===----------------------------------------------------------------------===//
8
Johnny Chen9ed5b492012-01-05 21:48:15 +00009#include <errno.h>
10#include <poll.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000011#include <signal.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include <stdint.h>
13#include <string.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000014#include <sys/ptrace.h>
15#include <sys/socket.h>
16#include <sys/types.h>
17#include <sys/wait.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <unistd.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000019
Johnny Chen9ed5b492012-01-05 21:48:15 +000020#include "lldb/Host/Host.h"
Zachary Turner24ae6292017-02-16 19:38:21 +000021#include "lldb/Host/PseudoTerminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000022#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000023#include "lldb/Target/RegisterContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Target/Thread.h"
Ed Maste8702e922015-03-03 22:44:18 +000025#include "lldb/Target/UnixSignals.h"
Pavel Labathd821c992018-08-07 11:07:21 +000026#include "lldb/Utility/RegisterValue.h"
27#include "lldb/Utility/Scalar.h"
Zachary Turner97206d52017-05-12 04:51:55 +000028#include "lldb/Utility/Status.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000029#include "llvm/Support/Errno.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000030
Ed Mastefe5a6422015-07-28 15:45:57 +000031#include "FreeBSDThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "Plugins/Process/POSIX/CrashReason.h"
Pavel Labathf0a6d8a2017-04-11 12:26:25 +000033#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000034#include "ProcessFreeBSD.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000035#include "ProcessMonitor.h"
36
Johnny Chen9ed5b492012-01-05 21:48:15 +000037using namespace lldb;
38using namespace lldb_private;
39
Adrian Prantl05097242018-04-30 16:49:04 +000040// We disable the tracing of ptrace calls for integration builds to avoid the
41// additional indirection and checks.
Johnny Chen9ed5b492012-01-05 21:48:15 +000042#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
43// Wrapper for ptrace to catch errors and log calls.
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045const char *Get_PT_IO_OP(int op) {
46 switch (op) {
47 case PIOD_READ_D:
48 return "READ_D";
49 case PIOD_WRITE_D:
50 return "WRITE_D";
51 case PIOD_READ_I:
52 return "READ_I";
53 case PIOD_WRITE_I:
54 return "WRITE_I";
55 default:
56 return "Unknown op";
57 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000058}
59
Adrian Prantl05097242018-04-30 16:49:04 +000060// Wrapper for ptrace to catch errors and log calls. Note that ptrace sets
61// errno on error because -1 is reserved as a valid result.
Kate Stoneb9c1b512016-09-06 20:57:50 +000062extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
63 const char *reqName, const char *file, int line) {
64 long int result;
Johnny Chen9ed5b492012-01-05 21:48:15 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 if (log) {
69 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
70 reqName, pid, addr, data, file, line);
71 if (req == PT_IO) {
72 struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
73
74 log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
75 (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 errno = 0;
82 result = ptrace(req, pid, (caddr_t)addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 if (log && errno != 0) {
87 const char *str;
88 switch (errno) {
89 case ESRCH:
90 str = "ESRCH";
91 break;
92 case EINVAL:
93 str = "EINVAL";
94 break;
95 case EBUSY:
96 str = "EBUSY";
97 break;
98 case EPERM:
99 str = "EPERM";
100 break;
101 default:
102 str = "<unknown>";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
105 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000108#ifdef __amd64__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 if (req == PT_GETREGS) {
110 struct reg *r = (struct reg *)addr;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
113 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Ed Mastea4be2c52014-02-19 18:34:06 +0000114 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
116 struct dbreg *r = (struct dbreg *)addr;
117 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
118
119 for (int i = 0; i <= 7; i++)
120 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
121 }
122#endif
123 }
124
125 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000126}
127
Adrian Prantl05097242018-04-30 16:49:04 +0000128// Wrapper for ptrace when logging is not required. Sets errno to 0 prior to
129// calling ptrace.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
131 long result = 0;
132 errno = 0;
133 result = ptrace(req, pid, (caddr_t)addr, data);
134 return result;
Matt Kopec7de48462013-03-06 17:20:48 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137#define PTRACE(req, pid, addr, data) \
138 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000139#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141#endif
142
143//------------------------------------------------------------------------------
144// Static implementations of ProcessMonitor::ReadMemory and
145// ProcessMonitor::WriteMemory. This enables mutual recursion between these
146// functions without needed to go thru the thread funnel.
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +0000149 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 pi_desc.piod_op = PIOD_READ_D;
153 pi_desc.piod_offs = (void *)vm_addr;
154 pi_desc.piod_addr = buf;
155 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000156
Ed Maste592d29a2018-04-21 11:23:56 +0000157 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000159 return 0;
160 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
Zachary Turner97206d52017-05-12 04:51:55 +0000165 const void *buf, size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 pi_desc.piod_op = PIOD_WRITE_D;
169 pi_desc.piod_offs = (void *)vm_addr;
David Carlier4fda0722019-01-31 11:54:58 +0000170 pi_desc.piod_addr = const_cast<void *>(buf);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000172
Ed Maste592d29a2018-04-21 11:23:56 +0000173 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000175 return 0;
176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000178}
179
180// Simple helper function to ensure flags are enabled on the given file
181// descriptor.
Zachary Turner97206d52017-05-12 04:51:55 +0000182static bool EnsureFDFlags(int fd, int flags, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 int status;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 if ((status = fcntl(fd, F_GETFL)) == -1) {
186 error.SetErrorToErrno();
187 return false;
188 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 if (fcntl(fd, F_SETFL, status | flags) == -1) {
191 error.SetErrorToErrno();
192 return false;
193 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196}
197
198//------------------------------------------------------------------------------
199/// @class Operation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000200/// Represents a ProcessMonitor operation.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000201///
Adrian Prantld8f460e2018-05-02 16:55:16 +0000202/// Under FreeBSD, it is not possible to ptrace() from any other thread but
203/// the one that spawned or attached to the process from the start.
204/// Therefore, when a ProcessMonitor is asked to deliver or change the state
205/// of an inferior process the operation must be "funneled" to a specific
206/// thread to perform the task. The Operation class provides an abstract base
207/// for all services the ProcessMonitor must perform via the single virtual
208/// function Execute, thus encapsulating the code that needs to run in the
209/// privileged context.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210class Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000211public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 virtual ~Operation() {}
213 virtual void Execute(ProcessMonitor *monitor) = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000214};
215
216//------------------------------------------------------------------------------
217/// @class ReadOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000218/// Implements ProcessMonitor::ReadMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219class ReadOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000220public:
Zachary Turner97206d52017-05-12 04:51:55 +0000221 ReadOperation(lldb::addr_t addr, void *buff, size_t size, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 size_t &result)
223 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
224 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000227
228private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 lldb::addr_t m_addr;
230 void *m_buff;
231 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000232 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000234};
235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236void ReadOperation::Execute(ProcessMonitor *monitor) {
237 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000240}
241
242//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000243/// @class WriteOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000244/// Implements ProcessMonitor::WriteMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245class WriteOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000246public:
Zachary Turner97206d52017-05-12 04:51:55 +0000247 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
248 Status &error, size_t &result)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
250 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000253
254private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 lldb::addr_t m_addr;
256 const void *m_buff;
257 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000258 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000260};
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262void WriteOperation::Execute(ProcessMonitor *monitor) {
263 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000266}
267
268//------------------------------------------------------------------------------
269/// @class ReadRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000270/// Implements ProcessMonitor::ReadRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271class ReadRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000272public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
274 RegisterValue &value, bool &result)
275 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
276 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000279
280private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 lldb::tid_t m_tid;
282 unsigned m_offset;
283 unsigned m_size;
284 RegisterValue &m_value;
285 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000286};
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288void ReadRegOperation::Execute(ProcessMonitor *monitor) {
289 struct reg regs;
290 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
293 m_result = false;
294 } else {
295 // 'struct reg' contains only 32- or 64-bit register values. Punt on
296 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
297 // processes on powerpc64 (probably the same for i386 on amd64)
298 if (m_size == sizeof(uint32_t))
299 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
300 else if (m_size == sizeof(uint64_t))
301 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
302 else
303 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
304 m_result = true;
305 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000306}
307
308//------------------------------------------------------------------------------
309/// @class WriteRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000310/// Implements ProcessMonitor::WriteRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311class WriteRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000312public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 WriteRegOperation(lldb::tid_t tid, unsigned offset,
314 const RegisterValue &value, bool &result)
315 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000318
319private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 lldb::tid_t m_tid;
321 unsigned m_offset;
322 const RegisterValue &m_value;
323 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000324};
325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326void WriteRegOperation::Execute(ProcessMonitor *monitor) {
327 struct reg regs;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
330 m_result = false;
331 return;
332 }
333 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
334 (uintptr_t)m_value.GetAsUInt64();
335 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
336 m_result = false;
337 else
338 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000339}
340
341//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000342/// @class ReadDebugRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000343/// Implements ProcessMonitor::ReadDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344class ReadDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000345public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
347 RegisterValue &value, bool &result)
348 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
349 m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000352
353private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 lldb::tid_t m_tid;
355 unsigned m_offset;
356 unsigned m_size;
357 RegisterValue &m_value;
358 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000359};
360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361void ReadDebugRegOperation::Execute(ProcessMonitor *monitor) {
362 struct dbreg regs;
363 int rc;
Ed Mastea4be2c52014-02-19 18:34:06 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
366 m_result = false;
367 } else {
368 if (m_size == sizeof(uintptr_t))
369 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
370 else
371 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
372 m_result = true;
373 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000374}
375
376//------------------------------------------------------------------------------
377/// @class WriteDebugRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000378/// Implements ProcessMonitor::WriteDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379class WriteDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000380public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
382 const RegisterValue &value, bool &result)
383 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000386
387private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 lldb::tid_t m_tid;
389 unsigned m_offset;
390 const RegisterValue &m_value;
391 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000392};
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394void WriteDebugRegOperation::Execute(ProcessMonitor *monitor) {
395 struct dbreg regs;
Ed Mastea4be2c52014-02-19 18:34:06 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
398 m_result = false;
399 return;
400 }
401 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
402 (uintptr_t)m_value.GetAsUInt64();
403 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
404 m_result = false;
405 else
406 m_result = true;
Ed Mastea4be2c52014-02-19 18:34:06 +0000407}
408
409//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000410/// @class ReadGPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000411/// Implements ProcessMonitor::ReadGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412class ReadGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000413public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
415 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000418
419private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 lldb::tid_t m_tid;
421 void *m_buf;
422 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000423};
424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425void ReadGPROperation::Execute(ProcessMonitor *monitor) {
426 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 errno = 0;
429 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
430 if (errno != 0)
431 m_result = false;
432 else
433 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000434}
435
436//------------------------------------------------------------------------------
437/// @class ReadFPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000438/// Implements ProcessMonitor::ReadFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439class ReadFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000440public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
442 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000445
446private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 lldb::tid_t m_tid;
448 void *m_buf;
449 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000450};
451
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452void ReadFPROperation::Execute(ProcessMonitor *monitor) {
453 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
454 m_result = false;
455 else
456 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000457}
458
459//------------------------------------------------------------------------------
460/// @class WriteGPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000461/// Implements ProcessMonitor::WriteGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462class WriteGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000463public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
465 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000468
469private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 lldb::tid_t m_tid;
471 void *m_buf;
472 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000473};
474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475void WriteGPROperation::Execute(ProcessMonitor *monitor) {
476 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
477 m_result = false;
478 else
479 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000480}
481
482//------------------------------------------------------------------------------
483/// @class WriteFPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000484/// Implements ProcessMonitor::WriteFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485class WriteFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
488 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000491
492private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 lldb::tid_t m_tid;
494 void *m_buf;
495 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000496};
497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498void WriteFPROperation::Execute(ProcessMonitor *monitor) {
499 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
500 m_result = false;
501 else
502 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000503}
504
505//------------------------------------------------------------------------------
506/// @class ResumeOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000507/// Implements ProcessMonitor::Resume.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508class ResumeOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000509public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 ResumeOperation(uint32_t signo, bool &result)
511 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000514
515private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 uint32_t m_signo;
517 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000518};
519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520void ResumeOperation::Execute(ProcessMonitor *monitor) {
521 lldb::pid_t pid = monitor->GetPID();
522 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
525 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) {
528 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Pavel Labath10c41f32017-06-06 14:06:17 +0000529 LLDB_LOG(log, "ResumeOperation ({0}) failed: {1}", pid,
530 llvm::sys::StrError(errno));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 m_result = false;
532 } else
533 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000534}
535
536//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000537/// @class SingleStepOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000538/// Implements ProcessMonitor::SingleStep.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539class SingleStepOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 SingleStepOperation(uint32_t signo, bool &result)
542 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000543
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000545
546private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 uint32_t m_signo;
548 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000549};
550
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551void SingleStepOperation::Execute(ProcessMonitor *monitor) {
552 lldb::pid_t pid = monitor->GetPID();
553 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
556 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 if (PTRACE(PT_STEP, pid, NULL, data))
559 m_result = false;
560 else
561 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000562}
563
564//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000565/// @class LwpInfoOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000566/// Implements ProcessMonitor::GetLwpInfo.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567class LwpInfoOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000568public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
570 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000573
574private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 lldb::tid_t m_tid;
576 void *m_info;
577 bool &m_result;
578 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000579};
580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581void LwpInfoOperation::Execute(ProcessMonitor *monitor) {
582 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
585 m_result = false;
586 m_err = errno;
587 } else {
588 memcpy(m_info, &plwp, sizeof(plwp));
589 m_result = true;
590 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000591}
592
593//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000594/// @class ThreadSuspendOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000595/// Implements ProcessMonitor::ThreadSuspend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596class ThreadSuspendOperation : public Operation {
Ed Maste7fd845c2013-12-09 15:51:17 +0000597public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
599 : m_tid(tid), m_suspend(suspend), m_result(result) {}
Ed Maste7fd845c2013-12-09 15:51:17 +0000600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 void Execute(ProcessMonitor *monitor);
Ed Maste7fd845c2013-12-09 15:51:17 +0000602
603private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604 lldb::tid_t m_tid;
605 bool m_suspend;
606 bool &m_result;
607};
Ed Maste7fd845c2013-12-09 15:51:17 +0000608
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609void ThreadSuspendOperation::Execute(ProcessMonitor *monitor) {
610 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
Ed Maste7fd845c2013-12-09 15:51:17 +0000611}
612
Ed Maste7fd845c2013-12-09 15:51:17 +0000613//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000614/// @class EventMessageOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000615/// Implements ProcessMonitor::GetEventMessage.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616class EventMessageOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000617public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
619 : m_tid(tid), m_message(message), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000622
623private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 lldb::tid_t m_tid;
625 unsigned long *m_message;
626 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000627};
628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629void EventMessageOperation::Execute(ProcessMonitor *monitor) {
630 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000631
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
633 m_result = false;
634 else {
635 if (plwp.pl_flags & PL_FLAG_FORKED) {
636 *m_message = plwp.pl_child_pid;
637 m_result = true;
638 } else
639 m_result = false;
640 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000641}
642
643//------------------------------------------------------------------------------
644/// @class KillOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000645/// Implements ProcessMonitor::Kill.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646class KillOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000647public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 KillOperation(bool &result) : m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000649
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000651
652private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000654};
655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656void KillOperation::Execute(ProcessMonitor *monitor) {
657 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000658
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 if (PTRACE(PT_KILL, pid, NULL, 0))
660 m_result = false;
661 else
662 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000663}
664
665//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000666/// @class DetachOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000667/// Implements ProcessMonitor::Detach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668class DetachOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000669public:
Zachary Turner97206d52017-05-12 04:51:55 +0000670 DetachOperation(Status &result) : m_error(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000673
674private:
Zachary Turner97206d52017-05-12 04:51:55 +0000675 Status &m_error;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000676};
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678void DetachOperation::Execute(ProcessMonitor *monitor) {
679 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
682 m_error.SetErrorToErrno();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000683}
684
685ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 : m_monitor(monitor) {
687 sem_init(&m_semaphore, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000688}
689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000691
692ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
693 lldb_private::Module *module,
Pavel Labath75c6de02018-01-10 13:53:40 +0000694 char const **argv, Environment env,
Ed Maste41fba2b2015-06-01 15:24:37 +0000695 const FileSpec &stdin_file_spec,
696 const FileSpec &stdout_file_spec,
697 const FileSpec &stderr_file_spec,
698 const FileSpec &working_dir)
Pavel Labath75c6de02018-01-10 13:53:40 +0000699 : OperationArgs(monitor), m_module(module), m_argv(argv),
700 m_env(std::move(env)), m_stdin_file_spec(stdin_file_spec),
701 m_stdout_file_spec(stdout_file_spec),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000703
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704ProcessMonitor::LaunchArgs::~LaunchArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
707 : OperationArgs(monitor), m_pid(pid) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000708
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709ProcessMonitor::AttachArgs::~AttachArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000710
711//------------------------------------------------------------------------------
712/// The basic design of the ProcessMonitor is built around two threads.
713///
Adrian Prantld8f460e2018-05-02 16:55:16 +0000714/// One thread (@see SignalThread) simply blocks on a call to waitpid()
715/// looking for changes in the debugee state. When a change is detected a
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
Adrian Prantld8f460e2018-05-02 16:55:16 +0000717/// thread "drives" state changes in the debugger.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000718///
719/// The second thread (@see OperationThread) is responsible for two things 1)
720/// launching or attaching to the inferior process, and then 2) servicing
721/// operations such as register reads/writes, stepping, etc. See the comments
722/// on the Operation class for more info as to why this is needed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723ProcessMonitor::ProcessMonitor(
724 ProcessFreeBSD *process, Module *module, const char *argv[],
Pavel Labath75c6de02018-01-10 13:53:40 +0000725 Environment env, const FileSpec &stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
727 const FileSpec &working_dir,
728 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Zachary Turner97206d52017-05-12 04:51:55 +0000729 lldb_private::Status &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000730 : m_process(static_cast<ProcessFreeBSD *>(process)),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
732 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 std::unique_ptr<LaunchArgs> args(
Pavel Labath75c6de02018-01-10 13:53:40 +0000735 new LaunchArgs(this, module, argv, std::move(env), stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 stdout_file_spec, stderr_file_spec, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 sem_init(&m_operation_pending, 0, 0);
739 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000740
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 StartLaunchOpThread(args.get(), error);
742 if (!error.Success())
743 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000744
Pavel Labathc1a6b122017-07-03 09:25:55 +0000745 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
746 error.SetErrorToErrno();
747 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 // Check that the launch was a success.
751 if (!args->m_error.Success()) {
752 StopOpThread();
753 error = args->m_error;
754 return;
755 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 // Finally, start monitoring the child process for change in state.
758 m_monitor_thread = Host::StartMonitoringChildProcess(
759 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
760 GetPID(), true);
761 if (!m_monitor_thread.IsJoinable()) {
762 error.SetErrorToGenericError();
763 error.SetErrorString("Process launch failed.");
764 return;
765 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000766}
767
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +0000769 lldb_private::Status &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
771 m_terminal_fd(-1), m_operation(0) {
772 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000773
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 sem_init(&m_operation_pending, 0, 0);
775 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000776
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 StartAttachOpThread(args.get(), error);
780 if (!error.Success())
781 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000782
Pavel Labathc1a6b122017-07-03 09:25:55 +0000783 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
784 error.SetErrorToErrno();
785 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 // Check that the attach was a success.
789 if (!args->m_error.Success()) {
790 StopOpThread();
791 error = args->m_error;
792 return;
793 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000794
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 // Finally, start monitoring the child process for change in state.
796 m_monitor_thread = Host::StartMonitoringChildProcess(
797 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
798 GetPID(), true);
799 if (!m_monitor_thread.IsJoinable()) {
800 error.SetErrorToGenericError();
801 error.SetErrorString("Process attach failed.");
802 return;
803 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000804}
805
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000807
808//------------------------------------------------------------------------------
809// Thread setup and tear down.
Zachary Turner97206d52017-05-12 04:51:55 +0000810void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000812
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813 if (m_operation_thread.IsJoinable())
814 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000815
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 m_operation_thread =
817 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000818}
819
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820void *ProcessMonitor::LaunchOpThread(void *arg) {
821 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 if (!Launch(args)) {
824 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000825 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826 }
827
828 ServeOperation(args);
829 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000830}
831
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832bool ProcessMonitor::Launch(LaunchArgs *args) {
833 ProcessMonitor *monitor = args->m_monitor;
834 ProcessFreeBSD &process = monitor->GetProcess();
835 const char **argv = args->m_argv;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
837 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
838 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
839 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000840
Pavel Labath07d6f882017-12-11 10:09:14 +0000841 PseudoTerminal terminal;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 const size_t err_len = 1024;
843 char err_str[err_len];
844 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000845
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 // Propagate the environment if one is not supplied.
Pavel Labath75c6de02018-01-10 13:53:40 +0000847 Environment::Envp envp =
848 (args->m_env.empty() ? Host::GetEnvironment() : args->m_env).getEnvp();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000849
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 if ((pid = terminal.Fork(err_str, err_len)) == -1) {
851 args->m_error.SetErrorToGenericError();
852 args->m_error.SetErrorString("Process fork failed.");
853 goto FINISH;
854 }
855
856 // Recognized child exit status codes.
857 enum {
858 ePtraceFailed = 1,
859 eDupStdinFailed,
860 eDupStdoutFailed,
861 eDupStderrFailed,
862 eChdirFailed,
863 eExecFailed,
864 eSetGidFailed
865 };
866
867 // Child process.
868 if (pid == 0) {
869 // Trace this process.
870 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
871 exit(ePtraceFailed);
872
Adrian Prantl05097242018-04-30 16:49:04 +0000873 // terminal has already dupped the tty descriptors to stdin/out/err. This
874 // closes original fd from which they were copied (and avoids leaking
875 // descriptors to the debugged process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 terminal.CloseSlaveFileDescriptor();
877
878 // Do not inherit setgid powers.
879 if (setgid(getgid()) != 0)
880 exit(eSetGidFailed);
881
882 // Let us have our own process group.
883 setpgid(0, 0);
884
885 // Dup file descriptors if needed.
886 //
887 // FIXME: If two or more of the paths are the same we needlessly open
888 // the same file multiple times.
889 if (stdin_file_spec)
890 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
891 exit(eDupStdinFailed);
892
893 if (stdout_file_spec)
894 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
895 exit(eDupStdoutFailed);
896
897 if (stderr_file_spec)
898 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
899 exit(eDupStderrFailed);
900
901 // Change working directory
902 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
903 exit(eChdirFailed);
904
905 // Execute. We should never return.
Pavel Labath75c6de02018-01-10 13:53:40 +0000906 execve(argv[0], const_cast<char *const *>(argv), envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 exit(eExecFailed);
908 }
909
910 // Wait for the child process to to trap on its call to execve.
911 ::pid_t wpid;
912 int status;
913 if ((wpid = waitpid(pid, &status, 0)) < 0) {
914 args->m_error.SetErrorToErrno();
915 goto FINISH;
916 } else if (WIFEXITED(status)) {
917 // open, dup or execve likely failed for some reason.
918 args->m_error.SetErrorToGenericError();
919 switch (WEXITSTATUS(status)) {
920 case ePtraceFailed:
921 args->m_error.SetErrorString("Child ptrace failed.");
922 break;
923 case eDupStdinFailed:
924 args->m_error.SetErrorString("Child open stdin failed.");
925 break;
926 case eDupStdoutFailed:
927 args->m_error.SetErrorString("Child open stdout failed.");
928 break;
929 case eDupStderrFailed:
930 args->m_error.SetErrorString("Child open stderr failed.");
931 break;
932 case eChdirFailed:
933 args->m_error.SetErrorString("Child failed to set working directory.");
934 break;
935 case eExecFailed:
936 args->m_error.SetErrorString("Child exec failed.");
937 break;
938 case eSetGidFailed:
939 args->m_error.SetErrorString("Child setgid failed.");
940 break;
941 default:
942 args->m_error.SetErrorString("Child returned unknown exit status.");
943 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000944 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 goto FINISH;
946 }
947 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
948 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000949
950#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 // Have the child raise an event on exit. This is used to keep the child in
952 // limbo until it is destroyed.
953 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
954 args->m_error.SetErrorToErrno();
955 goto FINISH;
956 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000957#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958 // Release the master terminal descriptor and pass it off to the
959 // ProcessMonitor instance. Similarly stash the inferior pid.
960 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
961 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000962
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 // Set the terminal fd to be in non blocking mode (it simplifies the
964 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
965 // descriptor to read from).
966 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
967 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000970
971FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000973}
974
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
Zachary Turner97206d52017-05-12 04:51:55 +0000976 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 if (m_operation_thread.IsJoinable())
980 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000981
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 m_operation_thread =
983 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000984}
985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986void *ProcessMonitor::AttachOpThread(void *arg) {
987 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000988
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 ServeOperation(args);
992 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000993}
994
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995void ProcessMonitor::Attach(AttachArgs *args) {
996 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000997
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 ProcessMonitor *monitor = args->m_monitor;
999 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 if (pid <= 1) {
1002 args->m_error.SetErrorToGenericError();
1003 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1004 return;
1005 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 // Attach to the requested process.
1008 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
1009 args->m_error.SetErrorToErrno();
1010 return;
1011 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001012
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 int status;
1014 if ((status = waitpid(pid, NULL, 0)) < 0) {
1015 args->m_error.SetErrorToErrno();
1016 return;
1017 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001020}
1021
Ed Maste7fd845c2013-12-09 15:51:17 +00001022size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1024 lwpid_t *tids;
1025 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001026
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001028
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1030 if (tdcnt <= 0)
1031 return 0;
1032 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1033 if (tids == NULL)
1034 return 0;
1035 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001036 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 return 0;
1038 }
1039 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1040 free(tids);
1041 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001042}
1043
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1045 bool exited, int signal, int status) {
1046 ProcessMessage message;
1047 ProcessFreeBSD *process = monitor->m_process;
1048 assert(process);
1049 bool stop_monitoring;
1050 struct ptrace_lwpinfo plwp;
1051 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001052
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 if (exited) {
1056 if (log)
1057 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1058 __FUNCTION__, pid);
1059 message = ProcessMessage::Exit(pid, status);
1060 process->SendMessage(message);
1061 return pid == process->GetID();
1062 }
Ed Mastea02f5532013-07-02 16:45:16 +00001063
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1065 stop_monitoring = true; // pid is gone. Bail.
1066 else {
1067 switch (plwp.pl_siginfo.si_signo) {
1068 case SIGTRAP:
1069 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1070 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001071
Johnny Chen9ed5b492012-01-05 21:48:15 +00001072 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1074 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001075 }
1076
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 process->SendMessage(message);
1078 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1079 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001080
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001082}
1083
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1085 const siginfo_t *info,
1086 lldb::tid_t tid) {
1087 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001090
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 assert(monitor);
1092 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 switch (info->si_code) {
1095 default:
1096 assert(false && "Unexpected SIGTRAP code!");
1097 break;
1098
1099 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
Adrian Prantl05097242018-04-30 16:49:04 +00001100 // The inferior process is about to exit. Maintain the process in a state
1101 // of "limbo" until we are explicitly commanded to detach, destroy, resume,
1102 // etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 unsigned long data = 0;
1104 if (!monitor->GetEventMessage(tid, &data))
1105 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001106 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1108 "= %" PRIu64,
1109 __FUNCTION__, data, tid);
1110 message = ProcessMessage::Limbo(tid, (data >> 8));
1111 break;
1112 }
Ed Mastea02f5532013-07-02 16:45:16 +00001113
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 case 0:
1115 case TRAP_TRACE:
Ed Masted6fa2c32017-05-26 03:15:46 +00001116#ifdef TRAP_CAP
1117 // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1118 case TRAP_CAP:
1119#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (log)
1121 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1122 " : si_code = %d",
1123 __FUNCTION__, tid, info->si_code);
1124 message = ProcessMessage::Trace(tid);
1125 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001126
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127 case SI_KERNEL:
1128 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001129 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1130 if (log)
1131 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1132 "event, tid = %" PRIu64,
1133 __FUNCTION__, tid);
1134 message = ProcessMessage::Trace(tid);
1135 } else {
1136 if (log)
1137 log->Printf(
1138 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1139 __FUNCTION__, tid);
1140 message = ProcessMessage::Break(tid);
1141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 break;
1143 }
1144
1145 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001146}
1147
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1149 const siginfo_t *info,
1150 lldb::tid_t tid) {
1151 ProcessMessage message;
1152 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001155
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
Adrian Prantl05097242018-04-30 16:49:04 +00001157 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a kill(2)
1158 // or raise(3). Similarly for tgkill(2) on FreeBSD.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 //
1160 // IOW, user generated signals never generate what we consider to be a
1161 // "crash".
1162 //
1163 // Similarly, ACK signals generated by this monitor.
1164 if (info->si_code == SI_USER) {
1165 if (log)
1166 log->Printf(
1167 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1168 __FUNCTION__,
1169 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1170 "SI_USER", info->si_pid);
1171 if (info->si_pid == getpid())
1172 return ProcessMessage::SignalDelivered(tid, signo);
1173 else
1174 return ProcessMessage::Signal(tid, signo);
1175 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001176
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 if (log)
1178 log->Printf(
1179 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1180 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001181
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182 switch (signo) {
1183 case SIGSEGV:
1184 case SIGILL:
1185 case SIGFPE:
1186 case SIGBUS:
1187 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1188 const auto reason = GetCrashReason(*info);
Ed Maste5e82ca32017-08-10 13:47:17 +00001189 if (reason != CrashReason::eInvalidCrashReason) {
1190 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1191 } // else; Use atleast si_signo info for other si_code
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192 }
1193
Adrian Prantl05097242018-04-30 16:49:04 +00001194 // Everything else is "normal" and does not require any special action on our
1195 // part.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001197}
1198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199void ProcessMonitor::ServeOperation(OperationArgs *args) {
1200 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 // We are finised with the arguments and are ready to go. Sync with the
1203 // parent thread and start serving operations on the inferior.
1204 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001205
Kate Stoneb9c1b512016-09-06 20:57:50 +00001206 for (;;) {
1207 // wait for next pending operation
1208 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001209
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 monitor->m_operation->Execute(monitor);
1211
1212 // notify calling thread that operation is complete
1213 sem_post(&monitor->m_operation_done);
1214 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001215}
1216
Kate Stoneb9c1b512016-09-06 20:57:50 +00001217void ProcessMonitor::DoOperation(Operation *op) {
1218 std::lock_guard<std::mutex> guard(m_operation_mutex);
1219
1220 m_operation = op;
1221
1222 // notify operation thread that an operation is ready to be processed
1223 sem_post(&m_operation_pending);
1224
1225 // wait for operation to complete
1226 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001227}
1228
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +00001230 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 size_t result;
1232 ReadOperation op(vm_addr, buf, size, error, result);
1233 DoOperation(&op);
1234 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001235}
1236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +00001238 size_t size, lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 size_t result;
1240 WriteOperation op(vm_addr, buf, size, error, result);
1241 DoOperation(&op);
1242 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001243}
1244
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001246 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 RegisterValue &value) {
1248 bool result;
1249 ReadRegOperation op(tid, offset, size, value, result);
1250 DoOperation(&op);
1251 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001252}
1253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001255 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 const RegisterValue &value) {
1257 bool result;
1258 WriteRegOperation op(tid, offset, value, result);
1259 DoOperation(&op);
1260 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001261}
1262
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263bool ProcessMonitor::ReadDebugRegisterValue(
1264 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1265 lldb_private::RegisterValue &value) {
1266 bool result;
1267 ReadDebugRegOperation op(tid, offset, size, value, result);
1268 DoOperation(&op);
1269 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001270}
1271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272bool ProcessMonitor::WriteDebugRegisterValue(
1273 lldb::tid_t tid, unsigned offset, const char *reg_name,
1274 const lldb_private::RegisterValue &value) {
1275 bool result;
1276 WriteDebugRegOperation op(tid, offset, value, result);
1277 DoOperation(&op);
1278 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001279}
1280
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1282 bool result;
1283 ReadGPROperation op(tid, buf, result);
1284 DoOperation(&op);
1285 return result;
1286}
1287
1288bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1289 bool result;
1290 ReadFPROperation op(tid, buf, result);
1291 DoOperation(&op);
1292 return result;
1293}
1294
1295bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1296 size_t buf_size, unsigned int regset) {
1297 return false;
1298}
1299
1300bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1301 bool result;
1302 WriteGPROperation op(tid, buf, result);
1303 DoOperation(&op);
1304 return result;
1305}
1306
1307bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1308 bool result;
1309 WriteFPROperation op(tid, buf, result);
1310 DoOperation(&op);
1311 return result;
1312}
1313
1314bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1315 size_t buf_size, unsigned int regset) {
1316 return false;
1317}
1318
1319bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1320 return false;
1321}
1322
1323bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1324 bool result;
1325 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1326
1327 if (log) {
1328 const char *signame =
1329 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1330 if (signame == nullptr)
1331 signame = "<none>";
1332 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1333 __FUNCTION__, GetPID(), signame);
1334 }
1335 ResumeOperation op(signo, result);
1336 DoOperation(&op);
1337 if (log)
1338 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1339 result ? "true" : "false");
1340 return result;
1341}
1342
1343bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1344 bool result;
1345 SingleStepOperation op(signo, result);
1346 DoOperation(&op);
1347 return result;
1348}
1349
1350bool ProcessMonitor::Kill() {
1351 bool result;
1352 KillOperation op(result);
1353 DoOperation(&op);
1354 return result;
1355}
1356
1357bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1358 int &ptrace_err) {
1359 bool result;
1360 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1361 DoOperation(&op);
1362 return result;
1363}
1364
1365bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1366 bool result;
1367 ThreadSuspendOperation op(tid, suspend, result);
1368 DoOperation(&op);
1369 return result;
1370}
1371
1372bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1373 bool result;
1374 EventMessageOperation op(tid, message, result);
1375 DoOperation(&op);
1376 return result;
1377}
1378
Zachary Turner97206d52017-05-12 04:51:55 +00001379lldb_private::Status ProcessMonitor::Detach(lldb::tid_t tid) {
1380 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 if (tid != LLDB_INVALID_THREAD_ID) {
1382 DetachOperation op(error);
1383 DoOperation(&op);
1384 }
1385 return error;
1386}
1387
1388bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1389 int flags) {
1390 int target_fd = open(file_spec.GetCString(), flags, 0666);
1391
1392 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001393 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001396 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397
1398 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001399}
1400
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401void ProcessMonitor::StopMonitoringChildProcess() {
1402 if (m_monitor_thread.IsJoinable()) {
1403 m_monitor_thread.Cancel();
1404 m_monitor_thread.Join(nullptr);
1405 m_monitor_thread.Reset();
1406 }
Ed Maste68f51792013-10-18 19:16:44 +00001407}
1408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409void ProcessMonitor::StopMonitor() {
1410 StopMonitoringChildProcess();
1411 StopOpThread();
1412 sem_destroy(&m_operation_pending);
1413 sem_destroy(&m_operation_done);
1414 if (m_terminal_fd >= 0) {
1415 close(m_terminal_fd);
1416 m_terminal_fd = -1;
1417 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001418}
1419
Andrew Kaylord4d54992013-09-17 00:30:24 +00001420// FIXME: On Linux, when a new thread is created, we receive to notifications,
Adrian Prantl05097242018-04-30 16:49:04 +00001421// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the child
1422// thread id as additional information, and (2) a SIGSTOP|SI_USER from the new
1423// child thread indicating that it has is stopped because we attached. We have
1424// no guarantee of the order in which these arrive, but we need both before we
1425// are ready to proceed. We currently keep a list of threads which have sent
1426// the initial SIGSTOP|SI_USER event. Then when we receive the
1427// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not
1428// occurred we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
Andrew Kaylord4d54992013-09-17 00:30:24 +00001429//
1430// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1431// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1432// logically needed.
1433//
1434// We really should figure out what actually happens on FreeBSD and move the
1435// Linux-specific logic out of ProcessPOSIX as needed.
1436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001438
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439void ProcessMonitor::StopOpThread() {
1440 if (!m_operation_thread.IsJoinable())
1441 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001442
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 m_operation_thread.Cancel();
1444 m_operation_thread.Join(nullptr);
1445 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001446}