blob: 68ab41651162c30a595a2dc4cc9acedceb71f542 [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessMonitor.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// C Includes
11#include <errno.h>
12#include <poll.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000013#include <signal.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include <stdint.h>
15#include <string.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000016#include <sys/ptrace.h>
17#include <sys/socket.h>
18#include <sys/types.h>
19#include <sys/wait.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include <unistd.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000021
22// C++ Includes
23// Other libraries and framework includes
Johnny Chen9ed5b492012-01-05 21:48:15 +000024#include "lldb/Core/RegisterValue.h"
25#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
Zachary Turner24ae6292017-02-16 19:38:21 +000027#include "lldb/Host/PseudoTerminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000029#include "lldb/Target/RegisterContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000030#include "lldb/Target/Thread.h"
Ed Maste8702e922015-03-03 22:44:18 +000031#include "lldb/Target/UnixSignals.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000032#include "lldb/Utility/Error.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000033
Ed Mastefe5a6422015-07-28 15:45:57 +000034#include "FreeBSDThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000035#include "Plugins/Process/POSIX/CrashReason.h"
Pavel Labathf0a6d8a2017-04-11 12:26:25 +000036#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000037#include "ProcessFreeBSD.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000038#include "ProcessMonitor.h"
39
40extern "C" {
Kate Stoneb9c1b512016-09-06 20:57:50 +000041extern char **environ;
42}
Johnny Chen9ed5b492012-01-05 21:48:15 +000043
44using namespace lldb;
45using namespace lldb_private;
46
47// We disable the tracing of ptrace calls for integration builds to
48// avoid the additional indirection and checks.
49#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
50// Wrapper for ptrace to catch errors and log calls.
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052const char *Get_PT_IO_OP(int op) {
53 switch (op) {
54 case PIOD_READ_D:
55 return "READ_D";
56 case PIOD_WRITE_D:
57 return "WRITE_D";
58 case PIOD_READ_I:
59 return "READ_I";
60 case PIOD_WRITE_I:
61 return "WRITE_I";
62 default:
63 return "Unknown op";
64 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000065}
66
Matt Kopec7de48462013-03-06 17:20:48 +000067// Wrapper for ptrace to catch errors and log calls.
Kate Stoneb9c1b512016-09-06 20:57:50 +000068// Note that ptrace sets errno on error because -1 is reserved as a valid
69// result.
70extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
71 const char *reqName, const char *file, int line) {
72 long int result;
Johnny Chen9ed5b492012-01-05 21:48:15 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 if (log) {
77 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
78 reqName, pid, addr, data, file, line);
79 if (req == PT_IO) {
80 struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
81
82 log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
83 (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 errno = 0;
90 result = ptrace(req, pid, (caddr_t)addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 if (log && errno != 0) {
95 const char *str;
96 switch (errno) {
97 case ESRCH:
98 str = "ESRCH";
99 break;
100 case EINVAL:
101 str = "EINVAL";
102 break;
103 case EBUSY:
104 str = "EBUSY";
105 break;
106 case EPERM:
107 str = "EPERM";
108 break;
109 default:
110 str = "<unknown>";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
113 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000116#ifdef __amd64__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (req == PT_GETREGS) {
118 struct reg *r = (struct reg *)addr;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
121 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Ed Mastea4be2c52014-02-19 18:34:06 +0000122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
124 struct dbreg *r = (struct dbreg *)addr;
125 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
126
127 for (int i = 0; i <= 7; i++)
128 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
129 }
130#endif
131 }
132
133 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000134}
135
Matt Kopec7de48462013-03-06 17:20:48 +0000136// Wrapper for ptrace when logging is not required.
137// Sets errno to 0 prior to calling ptrace.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
139 long result = 0;
140 errno = 0;
141 result = ptrace(req, pid, (caddr_t)addr, data);
142 return result;
Matt Kopec7de48462013-03-06 17:20:48 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145#define PTRACE(req, pid, addr, data) \
146 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000147#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000149#endif
150
151//------------------------------------------------------------------------------
152// Static implementations of ProcessMonitor::ReadMemory and
153// ProcessMonitor::WriteMemory. This enables mutual recursion between these
154// functions without needed to go thru the thread funnel.
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
157 size_t size, Error &error) {
158 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 pi_desc.piod_op = PIOD_READ_D;
161 pi_desc.piod_offs = (void *)vm_addr;
162 pi_desc.piod_addr = buf;
163 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
166 error.SetErrorToErrno();
167 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
171 const void *buf, size_t size, Error &error) {
172 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 pi_desc.piod_op = PIOD_WRITE_D;
175 pi_desc.piod_offs = (void *)vm_addr;
176 pi_desc.piod_addr = (void *)buf;
177 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
180 error.SetErrorToErrno();
181 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000182}
183
184// Simple helper function to ensure flags are enabled on the given file
185// descriptor.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186static bool EnsureFDFlags(int fd, int flags, Error &error) {
187 int status;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 if ((status = fcntl(fd, F_GETFL)) == -1) {
190 error.SetErrorToErrno();
191 return false;
192 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 if (fcntl(fd, F_SETFL, status | flags) == -1) {
195 error.SetErrorToErrno();
196 return false;
197 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000200}
201
202//------------------------------------------------------------------------------
203/// @class Operation
204/// @brief Represents a ProcessMonitor operation.
205///
206/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
207/// one that spawned or attached to the process from the start. Therefore, when
208/// a ProcessMonitor is asked to deliver or change the state of an inferior
209/// process the operation must be "funneled" to a specific thread to perform the
210/// task. The Operation class provides an abstract base for all services the
211/// ProcessMonitor must perform via the single virtual function Execute, thus
212/// encapsulating the code that needs to run in the privileged context.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213class Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000214public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 virtual ~Operation() {}
216 virtual void Execute(ProcessMonitor *monitor) = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000217};
218
219//------------------------------------------------------------------------------
220/// @class ReadOperation
221/// @brief Implements ProcessMonitor::ReadMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222class ReadOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000223public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 ReadOperation(lldb::addr_t addr, void *buff, size_t size, Error &error,
225 size_t &result)
226 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
227 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000230
231private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 lldb::addr_t m_addr;
233 void *m_buff;
234 size_t m_size;
235 Error &m_error;
236 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000237};
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239void ReadOperation::Execute(ProcessMonitor *monitor) {
240 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000243}
244
245//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000246/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000247/// @brief Implements ProcessMonitor::WriteMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248class WriteOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000249public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 WriteOperation(lldb::addr_t addr, const void *buff, size_t size, Error &error,
251 size_t &result)
252 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
253 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256
257private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 lldb::addr_t m_addr;
259 const void *m_buff;
260 size_t m_size;
261 Error &m_error;
262 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000263};
264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265void WriteOperation::Execute(ProcessMonitor *monitor) {
266 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000269}
270
271//------------------------------------------------------------------------------
272/// @class ReadRegOperation
273/// @brief Implements ProcessMonitor::ReadRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274class ReadRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000275public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
277 RegisterValue &value, bool &result)
278 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
279 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000282
283private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 lldb::tid_t m_tid;
285 unsigned m_offset;
286 unsigned m_size;
287 RegisterValue &m_value;
288 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000289};
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291void ReadRegOperation::Execute(ProcessMonitor *monitor) {
292 struct reg regs;
293 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
296 m_result = false;
297 } else {
298 // 'struct reg' contains only 32- or 64-bit register values. Punt on
299 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
300 // processes on powerpc64 (probably the same for i386 on amd64)
301 if (m_size == sizeof(uint32_t))
302 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
303 else if (m_size == sizeof(uint64_t))
304 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
305 else
306 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
307 m_result = true;
308 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000309}
310
311//------------------------------------------------------------------------------
312/// @class WriteRegOperation
313/// @brief Implements ProcessMonitor::WriteRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314class WriteRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000315public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 WriteRegOperation(lldb::tid_t tid, unsigned offset,
317 const RegisterValue &value, bool &result)
318 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000321
322private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 lldb::tid_t m_tid;
324 unsigned m_offset;
325 const RegisterValue &m_value;
326 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000327};
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329void WriteRegOperation::Execute(ProcessMonitor *monitor) {
330 struct reg regs;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
333 m_result = false;
334 return;
335 }
336 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
337 (uintptr_t)m_value.GetAsUInt64();
338 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
339 m_result = false;
340 else
341 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000342}
343
344//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000345/// @class ReadDebugRegOperation
346/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347class ReadDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000348public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
350 RegisterValue &value, bool &result)
351 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
352 m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000355
356private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 lldb::tid_t m_tid;
358 unsigned m_offset;
359 unsigned m_size;
360 RegisterValue &m_value;
361 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000362};
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364void ReadDebugRegOperation::Execute(ProcessMonitor *monitor) {
365 struct dbreg regs;
366 int rc;
Ed Mastea4be2c52014-02-19 18:34:06 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
369 m_result = false;
370 } else {
371 if (m_size == sizeof(uintptr_t))
372 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
373 else
374 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
375 m_result = true;
376 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000377}
378
379//------------------------------------------------------------------------------
380/// @class WriteDebugRegOperation
381/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382class WriteDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000383public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
385 const RegisterValue &value, bool &result)
386 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000389
390private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 lldb::tid_t m_tid;
392 unsigned m_offset;
393 const RegisterValue &m_value;
394 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000395};
396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397void WriteDebugRegOperation::Execute(ProcessMonitor *monitor) {
398 struct dbreg regs;
Ed Mastea4be2c52014-02-19 18:34:06 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
401 m_result = false;
402 return;
403 }
404 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
405 (uintptr_t)m_value.GetAsUInt64();
406 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
407 m_result = false;
408 else
409 m_result = true;
Ed Mastea4be2c52014-02-19 18:34:06 +0000410}
411
412//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000413/// @class ReadGPROperation
414/// @brief Implements ProcessMonitor::ReadGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415class ReadGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000416public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
418 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000421
422private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 lldb::tid_t m_tid;
424 void *m_buf;
425 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000426};
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428void ReadGPROperation::Execute(ProcessMonitor *monitor) {
429 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 errno = 0;
432 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
433 if (errno != 0)
434 m_result = false;
435 else
436 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000437}
438
439//------------------------------------------------------------------------------
440/// @class ReadFPROperation
441/// @brief Implements ProcessMonitor::ReadFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442class ReadFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000443public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
445 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000448
449private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 lldb::tid_t m_tid;
451 void *m_buf;
452 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000453};
454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455void ReadFPROperation::Execute(ProcessMonitor *monitor) {
456 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
457 m_result = false;
458 else
459 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000460}
461
462//------------------------------------------------------------------------------
463/// @class WriteGPROperation
464/// @brief Implements ProcessMonitor::WriteGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465class WriteGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000466public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
468 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000471
472private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 lldb::tid_t m_tid;
474 void *m_buf;
475 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000476};
477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478void WriteGPROperation::Execute(ProcessMonitor *monitor) {
479 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
480 m_result = false;
481 else
482 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000483}
484
485//------------------------------------------------------------------------------
486/// @class WriteFPROperation
487/// @brief Implements ProcessMonitor::WriteFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488class WriteFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000489public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
491 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000494
495private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 lldb::tid_t m_tid;
497 void *m_buf;
498 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000499};
500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501void WriteFPROperation::Execute(ProcessMonitor *monitor) {
502 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
503 m_result = false;
504 else
505 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000506}
507
508//------------------------------------------------------------------------------
509/// @class ResumeOperation
510/// @brief Implements ProcessMonitor::Resume.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511class ResumeOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000512public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 ResumeOperation(uint32_t signo, bool &result)
514 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000517
518private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 uint32_t m_signo;
520 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000521};
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523void ResumeOperation::Execute(ProcessMonitor *monitor) {
524 lldb::pid_t pid = monitor->GetPID();
525 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
528 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) {
531 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +0000532
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 if (log)
534 log->Printf("ResumeOperation (%" PRIu64 ") failed: %s", pid,
535 strerror(errno));
536 m_result = false;
537 } else
538 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000539}
540
541//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000542/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000543/// @brief Implements ProcessMonitor::SingleStep.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544class SingleStepOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000545public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 SingleStepOperation(uint32_t signo, bool &result)
547 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000550
551private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 uint32_t m_signo;
553 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000554};
555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556void SingleStepOperation::Execute(ProcessMonitor *monitor) {
557 lldb::pid_t pid = monitor->GetPID();
558 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
561 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 if (PTRACE(PT_STEP, pid, NULL, data))
564 m_result = false;
565 else
566 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000567}
568
569//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000570/// @class LwpInfoOperation
571/// @brief Implements ProcessMonitor::GetLwpInfo.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572class LwpInfoOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000573public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
575 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000578
579private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 lldb::tid_t m_tid;
581 void *m_info;
582 bool &m_result;
583 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584};
585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586void LwpInfoOperation::Execute(ProcessMonitor *monitor) {
587 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
590 m_result = false;
591 m_err = errno;
592 } else {
593 memcpy(m_info, &plwp, sizeof(plwp));
594 m_result = true;
595 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000596}
597
598//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000599/// @class ThreadSuspendOperation
600/// @brief Implements ProcessMonitor::ThreadSuspend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601class ThreadSuspendOperation : public Operation {
Ed Maste7fd845c2013-12-09 15:51:17 +0000602public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
604 : m_tid(tid), m_suspend(suspend), m_result(result) {}
Ed Maste7fd845c2013-12-09 15:51:17 +0000605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 void Execute(ProcessMonitor *monitor);
Ed Maste7fd845c2013-12-09 15:51:17 +0000607
608private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 lldb::tid_t m_tid;
610 bool m_suspend;
611 bool &m_result;
612};
Ed Maste7fd845c2013-12-09 15:51:17 +0000613
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614void ThreadSuspendOperation::Execute(ProcessMonitor *monitor) {
615 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
Ed Maste7fd845c2013-12-09 15:51:17 +0000616}
617
Ed Maste7fd845c2013-12-09 15:51:17 +0000618//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000619/// @class EventMessageOperation
620/// @brief Implements ProcessMonitor::GetEventMessage.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621class EventMessageOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000622public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
624 : m_tid(tid), m_message(message), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000627
628private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 lldb::tid_t m_tid;
630 unsigned long *m_message;
631 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000632};
633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634void EventMessageOperation::Execute(ProcessMonitor *monitor) {
635 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
638 m_result = false;
639 else {
640 if (plwp.pl_flags & PL_FLAG_FORKED) {
641 *m_message = plwp.pl_child_pid;
642 m_result = true;
643 } else
644 m_result = false;
645 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000646}
647
648//------------------------------------------------------------------------------
649/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000650/// @brief Implements ProcessMonitor::Kill.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651class KillOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000652public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 KillOperation(bool &result) : m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000654
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000656
657private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000659};
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661void KillOperation::Execute(ProcessMonitor *monitor) {
662 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000663
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (PTRACE(PT_KILL, pid, NULL, 0))
665 m_result = false;
666 else
667 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000668}
669
670//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000671/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000672/// @brief Implements ProcessMonitor::Detach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673class DetachOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000674public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 DetachOperation(Error &result) : m_error(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000676
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000678
679private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 Error &m_error;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000681};
682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683void DetachOperation::Execute(ProcessMonitor *monitor) {
684 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
687 m_error.SetErrorToErrno();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000688}
689
690ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 : m_monitor(monitor) {
692 sem_init(&m_semaphore, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000693}
694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000696
697ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
698 lldb_private::Module *module,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 char const **argv, char const **envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000700 const FileSpec &stdin_file_spec,
701 const FileSpec &stdout_file_spec,
702 const FileSpec &stderr_file_spec,
703 const FileSpec &working_dir)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 : OperationArgs(monitor), m_module(module), m_argv(argv), m_envp(envp),
705 m_stdin_file_spec(stdin_file_spec), m_stdout_file_spec(stdout_file_spec),
706 m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000707
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708ProcessMonitor::LaunchArgs::~LaunchArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
711 : OperationArgs(monitor), m_pid(pid) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000712
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713ProcessMonitor::AttachArgs::~AttachArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000714
715//------------------------------------------------------------------------------
716/// The basic design of the ProcessMonitor is built around two threads.
717///
718/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
719/// for changes in the debugee state. When a change is detected a
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
721/// thread
Johnny Chen9ed5b492012-01-05 21:48:15 +0000722/// "drives" state changes in the debugger.
723///
724/// The second thread (@see OperationThread) is responsible for two things 1)
725/// launching or attaching to the inferior process, and then 2) servicing
726/// operations such as register reads/writes, stepping, etc. See the comments
727/// on the Operation class for more info as to why this is needed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728ProcessMonitor::ProcessMonitor(
729 ProcessFreeBSD *process, Module *module, const char *argv[],
730 const char *envp[], const FileSpec &stdin_file_spec,
731 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
732 const FileSpec &working_dir,
733 const lldb_private::ProcessLaunchInfo & /* launch_info */,
734 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000735 : m_process(static_cast<ProcessFreeBSD *>(process)),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
737 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 std::unique_ptr<LaunchArgs> args(
740 new LaunchArgs(this, module, argv, envp, stdin_file_spec,
741 stdout_file_spec, stderr_file_spec, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 sem_init(&m_operation_pending, 0, 0);
744 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000745
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 StartLaunchOpThread(args.get(), error);
747 if (!error.Success())
748 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000749
750WAIT_AGAIN:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 // Wait for the operation thread to initialize.
752 if (sem_wait(&args->m_semaphore)) {
753 if (errno == EINTR)
754 goto WAIT_AGAIN;
755 else {
756 error.SetErrorToErrno();
757 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 // Check that the launch was a success.
762 if (!args->m_error.Success()) {
763 StopOpThread();
764 error = args->m_error;
765 return;
766 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000767
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 // Finally, start monitoring the child process for change in state.
769 m_monitor_thread = Host::StartMonitoringChildProcess(
770 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
771 GetPID(), true);
772 if (!m_monitor_thread.IsJoinable()) {
773 error.SetErrorToGenericError();
774 error.SetErrorString("Process launch failed.");
775 return;
776 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000777}
778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000780 lldb_private::Error &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
782 m_terminal_fd(-1), m_operation(0) {
783 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000784
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 sem_init(&m_operation_pending, 0, 0);
786 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000789
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 StartAttachOpThread(args.get(), error);
791 if (!error.Success())
792 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000793
794WAIT_AGAIN:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 // Wait for the operation thread to initialize.
796 if (sem_wait(&args->m_semaphore)) {
797 if (errno == EINTR)
798 goto WAIT_AGAIN;
799 else {
800 error.SetErrorToErrno();
801 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 // Check that the attach was a success.
806 if (!args->m_error.Success()) {
807 StopOpThread();
808 error = args->m_error;
809 return;
810 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 // Finally, start monitoring the child process for change in state.
813 m_monitor_thread = Host::StartMonitoringChildProcess(
814 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
815 GetPID(), true);
816 if (!m_monitor_thread.IsJoinable()) {
817 error.SetErrorToGenericError();
818 error.SetErrorString("Process attach failed.");
819 return;
820 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821}
822
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000824
825//------------------------------------------------------------------------------
826// Thread setup and tear down.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error) {
828 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 if (m_operation_thread.IsJoinable())
831 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000832
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 m_operation_thread =
834 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000835}
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837void *ProcessMonitor::LaunchOpThread(void *arg) {
838 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000839
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 if (!Launch(args)) {
841 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000842 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 }
844
845 ServeOperation(args);
846 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000847}
848
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849bool ProcessMonitor::Launch(LaunchArgs *args) {
850 ProcessMonitor *monitor = args->m_monitor;
851 ProcessFreeBSD &process = monitor->GetProcess();
852 const char **argv = args->m_argv;
853 const char **envp = args->m_envp;
854 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
855 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
856 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
857 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000858
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 lldb_utility::PseudoTerminal terminal;
860 const size_t err_len = 1024;
861 char err_str[err_len];
862 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000863
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 // Propagate the environment if one is not supplied.
865 if (envp == NULL || envp[0] == NULL)
866 envp = const_cast<const char **>(environ);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000867
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 if ((pid = terminal.Fork(err_str, err_len)) == -1) {
869 args->m_error.SetErrorToGenericError();
870 args->m_error.SetErrorString("Process fork failed.");
871 goto FINISH;
872 }
873
874 // Recognized child exit status codes.
875 enum {
876 ePtraceFailed = 1,
877 eDupStdinFailed,
878 eDupStdoutFailed,
879 eDupStderrFailed,
880 eChdirFailed,
881 eExecFailed,
882 eSetGidFailed
883 };
884
885 // Child process.
886 if (pid == 0) {
887 // Trace this process.
888 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
889 exit(ePtraceFailed);
890
891 // terminal has already dupped the tty descriptors to stdin/out/err.
892 // This closes original fd from which they were copied (and avoids
893 // leaking descriptors to the debugged process.
894 terminal.CloseSlaveFileDescriptor();
895
896 // Do not inherit setgid powers.
897 if (setgid(getgid()) != 0)
898 exit(eSetGidFailed);
899
900 // Let us have our own process group.
901 setpgid(0, 0);
902
903 // Dup file descriptors if needed.
904 //
905 // FIXME: If two or more of the paths are the same we needlessly open
906 // the same file multiple times.
907 if (stdin_file_spec)
908 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
909 exit(eDupStdinFailed);
910
911 if (stdout_file_spec)
912 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
913 exit(eDupStdoutFailed);
914
915 if (stderr_file_spec)
916 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
917 exit(eDupStderrFailed);
918
919 // Change working directory
920 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
921 exit(eChdirFailed);
922
923 // Execute. We should never return.
924 execve(argv[0], const_cast<char *const *>(argv),
925 const_cast<char *const *>(envp));
926 exit(eExecFailed);
927 }
928
929 // Wait for the child process to to trap on its call to execve.
930 ::pid_t wpid;
931 int status;
932 if ((wpid = waitpid(pid, &status, 0)) < 0) {
933 args->m_error.SetErrorToErrno();
934 goto FINISH;
935 } else if (WIFEXITED(status)) {
936 // open, dup or execve likely failed for some reason.
937 args->m_error.SetErrorToGenericError();
938 switch (WEXITSTATUS(status)) {
939 case ePtraceFailed:
940 args->m_error.SetErrorString("Child ptrace failed.");
941 break;
942 case eDupStdinFailed:
943 args->m_error.SetErrorString("Child open stdin failed.");
944 break;
945 case eDupStdoutFailed:
946 args->m_error.SetErrorString("Child open stdout failed.");
947 break;
948 case eDupStderrFailed:
949 args->m_error.SetErrorString("Child open stderr failed.");
950 break;
951 case eChdirFailed:
952 args->m_error.SetErrorString("Child failed to set working directory.");
953 break;
954 case eExecFailed:
955 args->m_error.SetErrorString("Child exec failed.");
956 break;
957 case eSetGidFailed:
958 args->m_error.SetErrorString("Child setgid failed.");
959 break;
960 default:
961 args->m_error.SetErrorString("Child returned unknown exit status.");
962 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000963 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 goto FINISH;
965 }
966 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
967 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968
969#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 // Have the child raise an event on exit. This is used to keep the child in
971 // limbo until it is destroyed.
972 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
973 args->m_error.SetErrorToErrno();
974 goto FINISH;
975 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000976#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 // Release the master terminal descriptor and pass it off to the
978 // ProcessMonitor instance. Similarly stash the inferior pid.
979 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
980 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000981
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 // Set the terminal fd to be in non blocking mode (it simplifies the
983 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
984 // descriptor to read from).
985 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
986 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000989
990FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000992}
993
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
995 lldb_private::Error &error) {
996 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000997
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 if (m_operation_thread.IsJoinable())
999 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 m_operation_thread =
1002 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001003}
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005void *ProcessMonitor::AttachOpThread(void *arg) {
1006 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 ServeOperation(args);
1011 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001012}
1013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014void ProcessMonitor::Attach(AttachArgs *args) {
1015 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 ProcessMonitor *monitor = args->m_monitor;
1018 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 if (pid <= 1) {
1021 args->m_error.SetErrorToGenericError();
1022 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1023 return;
1024 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 // Attach to the requested process.
1027 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
1028 args->m_error.SetErrorToErrno();
1029 return;
1030 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 int status;
1033 if ((status = waitpid(pid, NULL, 0)) < 0) {
1034 args->m_error.SetErrorToErrno();
1035 return;
1036 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001037
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001039}
1040
Ed Maste7fd845c2013-12-09 15:51:17 +00001041size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1043 lwpid_t *tids;
1044 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001045
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001047
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1049 if (tdcnt <= 0)
1050 return 0;
1051 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1052 if (tids == NULL)
1053 return 0;
1054 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001055 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 return 0;
1057 }
1058 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1059 free(tids);
1060 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001061}
1062
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1064 bool exited, int signal, int status) {
1065 ProcessMessage message;
1066 ProcessFreeBSD *process = monitor->m_process;
1067 assert(process);
1068 bool stop_monitoring;
1069 struct ptrace_lwpinfo plwp;
1070 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001071
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 if (exited) {
1075 if (log)
1076 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1077 __FUNCTION__, pid);
1078 message = ProcessMessage::Exit(pid, status);
1079 process->SendMessage(message);
1080 return pid == process->GetID();
1081 }
Ed Mastea02f5532013-07-02 16:45:16 +00001082
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1084 stop_monitoring = true; // pid is gone. Bail.
1085 else {
1086 switch (plwp.pl_siginfo.si_signo) {
1087 case SIGTRAP:
1088 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1089 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001090
Johnny Chen9ed5b492012-01-05 21:48:15 +00001091 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1093 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094 }
1095
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 process->SendMessage(message);
1097 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1098 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001101}
1102
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1104 const siginfo_t *info,
1105 lldb::tid_t tid) {
1106 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001109
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 assert(monitor);
1111 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001112
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113 switch (info->si_code) {
1114 default:
1115 assert(false && "Unexpected SIGTRAP code!");
1116 break;
1117
1118 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
1119 // The inferior process is about to exit. Maintain the process in a
1120 // state of "limbo" until we are explicitly commanded to detach,
1121 // destroy, resume, etc.
1122 unsigned long data = 0;
1123 if (!monitor->GetEventMessage(tid, &data))
1124 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001125 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1127 "= %" PRIu64,
1128 __FUNCTION__, data, tid);
1129 message = ProcessMessage::Limbo(tid, (data >> 8));
1130 break;
1131 }
Ed Mastea02f5532013-07-02 16:45:16 +00001132
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 case 0:
1134 case TRAP_TRACE:
1135 if (log)
1136 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1137 " : si_code = %d",
1138 __FUNCTION__, tid, info->si_code);
1139 message = ProcessMessage::Trace(tid);
1140 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 case SI_KERNEL:
1143 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001144 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1145 if (log)
1146 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1147 "event, tid = %" PRIu64,
1148 __FUNCTION__, tid);
1149 message = ProcessMessage::Trace(tid);
1150 } else {
1151 if (log)
1152 log->Printf(
1153 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1154 __FUNCTION__, tid);
1155 message = ProcessMessage::Break(tid);
1156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 break;
1158 }
1159
1160 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001161}
1162
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1164 const siginfo_t *info,
1165 lldb::tid_t tid) {
1166 ProcessMessage message;
1167 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001168
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1172 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1173 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1174 //
1175 // IOW, user generated signals never generate what we consider to be a
1176 // "crash".
1177 //
1178 // Similarly, ACK signals generated by this monitor.
1179 if (info->si_code == SI_USER) {
1180 if (log)
1181 log->Printf(
1182 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1183 __FUNCTION__,
1184 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1185 "SI_USER", info->si_pid);
1186 if (info->si_pid == getpid())
1187 return ProcessMessage::SignalDelivered(tid, signo);
1188 else
1189 return ProcessMessage::Signal(tid, signo);
1190 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001191
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192 if (log)
1193 log->Printf(
1194 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1195 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001196
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 switch (signo) {
1198 case SIGSEGV:
1199 case SIGILL:
1200 case SIGFPE:
1201 case SIGBUS:
1202 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1203 const auto reason = GetCrashReason(*info);
1204 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1205 }
1206
1207 // Everything else is "normal" and does not require any special action on
1208 // our part.
1209 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001210}
1211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212void ProcessMonitor::ServeOperation(OperationArgs *args) {
1213 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 // We are finised with the arguments and are ready to go. Sync with the
1216 // parent thread and start serving operations on the inferior.
1217 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001218
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 for (;;) {
1220 // wait for next pending operation
1221 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001222
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 monitor->m_operation->Execute(monitor);
1224
1225 // notify calling thread that operation is complete
1226 sem_post(&monitor->m_operation_done);
1227 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001228}
1229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230void ProcessMonitor::DoOperation(Operation *op) {
1231 std::lock_guard<std::mutex> guard(m_operation_mutex);
1232
1233 m_operation = op;
1234
1235 // notify operation thread that an operation is ready to be processed
1236 sem_post(&m_operation_pending);
1237
1238 // wait for operation to complete
1239 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001240}
1241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1243 Error &error) {
1244 size_t result;
1245 ReadOperation op(vm_addr, buf, size, error, result);
1246 DoOperation(&op);
1247 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001248}
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
1251 size_t size, lldb_private::Error &error) {
1252 size_t result;
1253 WriteOperation op(vm_addr, buf, size, error, result);
1254 DoOperation(&op);
1255 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001256}
1257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001259 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 RegisterValue &value) {
1261 bool result;
1262 ReadRegOperation op(tid, offset, size, value, result);
1263 DoOperation(&op);
1264 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001265}
1266
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001268 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 const RegisterValue &value) {
1270 bool result;
1271 WriteRegOperation op(tid, offset, value, result);
1272 DoOperation(&op);
1273 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001274}
1275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276bool ProcessMonitor::ReadDebugRegisterValue(
1277 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1278 lldb_private::RegisterValue &value) {
1279 bool result;
1280 ReadDebugRegOperation op(tid, offset, size, value, result);
1281 DoOperation(&op);
1282 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001283}
1284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285bool ProcessMonitor::WriteDebugRegisterValue(
1286 lldb::tid_t tid, unsigned offset, const char *reg_name,
1287 const lldb_private::RegisterValue &value) {
1288 bool result;
1289 WriteDebugRegOperation op(tid, offset, value, result);
1290 DoOperation(&op);
1291 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001292}
1293
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1295 bool result;
1296 ReadGPROperation op(tid, buf, result);
1297 DoOperation(&op);
1298 return result;
1299}
1300
1301bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1302 bool result;
1303 ReadFPROperation op(tid, buf, result);
1304 DoOperation(&op);
1305 return result;
1306}
1307
1308bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1309 size_t buf_size, unsigned int regset) {
1310 return false;
1311}
1312
1313bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1314 bool result;
1315 WriteGPROperation op(tid, buf, result);
1316 DoOperation(&op);
1317 return result;
1318}
1319
1320bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1321 bool result;
1322 WriteFPROperation op(tid, buf, result);
1323 DoOperation(&op);
1324 return result;
1325}
1326
1327bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1328 size_t buf_size, unsigned int regset) {
1329 return false;
1330}
1331
1332bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1333 return false;
1334}
1335
1336bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1337 bool result;
1338 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1339
1340 if (log) {
1341 const char *signame =
1342 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1343 if (signame == nullptr)
1344 signame = "<none>";
1345 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1346 __FUNCTION__, GetPID(), signame);
1347 }
1348 ResumeOperation op(signo, result);
1349 DoOperation(&op);
1350 if (log)
1351 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1352 result ? "true" : "false");
1353 return result;
1354}
1355
1356bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1357 bool result;
1358 SingleStepOperation op(signo, result);
1359 DoOperation(&op);
1360 return result;
1361}
1362
1363bool ProcessMonitor::Kill() {
1364 bool result;
1365 KillOperation op(result);
1366 DoOperation(&op);
1367 return result;
1368}
1369
1370bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1371 int &ptrace_err) {
1372 bool result;
1373 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1374 DoOperation(&op);
1375 return result;
1376}
1377
1378bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1379 bool result;
1380 ThreadSuspendOperation op(tid, suspend, result);
1381 DoOperation(&op);
1382 return result;
1383}
1384
1385bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1386 bool result;
1387 EventMessageOperation op(tid, message, result);
1388 DoOperation(&op);
1389 return result;
1390}
1391
1392lldb_private::Error ProcessMonitor::Detach(lldb::tid_t tid) {
1393 lldb_private::Error error;
1394 if (tid != LLDB_INVALID_THREAD_ID) {
1395 DetachOperation op(error);
1396 DoOperation(&op);
1397 }
1398 return error;
1399}
1400
1401bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1402 int flags) {
1403 int target_fd = open(file_spec.GetCString(), flags, 0666);
1404
1405 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001406 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001407
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001409 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410
1411 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001412}
1413
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414void ProcessMonitor::StopMonitoringChildProcess() {
1415 if (m_monitor_thread.IsJoinable()) {
1416 m_monitor_thread.Cancel();
1417 m_monitor_thread.Join(nullptr);
1418 m_monitor_thread.Reset();
1419 }
Ed Maste68f51792013-10-18 19:16:44 +00001420}
1421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422void ProcessMonitor::StopMonitor() {
1423 StopMonitoringChildProcess();
1424 StopOpThread();
1425 sem_destroy(&m_operation_pending);
1426 sem_destroy(&m_operation_done);
1427 if (m_terminal_fd >= 0) {
1428 close(m_terminal_fd);
1429 m_terminal_fd = -1;
1430 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001431}
1432
Andrew Kaylord4d54992013-09-17 00:30:24 +00001433// FIXME: On Linux, when a new thread is created, we receive to notifications,
1434// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1435// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1436// the new child thread indicating that it has is stopped because we attached.
1437// We have no guarantee of the order in which these arrive, but we need both
1438// before we are ready to proceed. We currently keep a list of threads which
1439// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1440// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1441// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1442//
1443// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1444// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1445// logically needed.
1446//
1447// We really should figure out what actually happens on FreeBSD and move the
1448// Linux-specific logic out of ProcessPOSIX as needed.
1449
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001451
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452void ProcessMonitor::StopOpThread() {
1453 if (!m_operation_thread.IsJoinable())
1454 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 m_operation_thread.Cancel();
1457 m_operation_thread.Join(nullptr);
1458 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001459}