blob: bd06fa25f0a01abb365efdbd0e2e95fbd71bc8b3 [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 Turner97206d52017-05-12 04:51:55 +000032#include "lldb/Utility/Status.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000033#include "llvm/Support/Errno.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000034
Ed Mastefe5a6422015-07-28 15:45:57 +000035#include "FreeBSDThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "Plugins/Process/POSIX/CrashReason.h"
Pavel Labathf0a6d8a2017-04-11 12:26:25 +000037#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000038#include "ProcessFreeBSD.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000039#include "ProcessMonitor.h"
40
41extern "C" {
Kate Stoneb9c1b512016-09-06 20:57:50 +000042extern char **environ;
43}
Johnny Chen9ed5b492012-01-05 21:48:15 +000044
45using namespace lldb;
46using namespace lldb_private;
47
48// We disable the tracing of ptrace calls for integration builds to
49// avoid the additional indirection and checks.
50#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
51// Wrapper for ptrace to catch errors and log calls.
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053const char *Get_PT_IO_OP(int op) {
54 switch (op) {
55 case PIOD_READ_D:
56 return "READ_D";
57 case PIOD_WRITE_D:
58 return "WRITE_D";
59 case PIOD_READ_I:
60 return "READ_I";
61 case PIOD_WRITE_I:
62 return "WRITE_I";
63 default:
64 return "Unknown op";
65 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000066}
67
Matt Kopec7de48462013-03-06 17:20:48 +000068// Wrapper for ptrace to catch errors and log calls.
Kate Stoneb9c1b512016-09-06 20:57:50 +000069// Note that ptrace sets errno on error because -1 is reserved as a valid
70// result.
71extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
72 const char *reqName, const char *file, int line) {
73 long int result;
Johnny Chen9ed5b492012-01-05 21:48:15 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 if (log) {
78 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
79 reqName, pid, addr, data, file, line);
80 if (req == PT_IO) {
81 struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
82
83 log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
84 (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 errno = 0;
91 result = ptrace(req, pid, (caddr_t)addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 if (log && errno != 0) {
96 const char *str;
97 switch (errno) {
98 case ESRCH:
99 str = "ESRCH";
100 break;
101 case EINVAL:
102 str = "EINVAL";
103 break;
104 case EBUSY:
105 str = "EBUSY";
106 break;
107 case EPERM:
108 str = "EPERM";
109 break;
110 default:
111 str = "<unknown>";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000112 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
114 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000117#ifdef __amd64__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (req == PT_GETREGS) {
119 struct reg *r = (struct reg *)addr;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
122 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Ed Mastea4be2c52014-02-19 18:34:06 +0000123 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
125 struct dbreg *r = (struct dbreg *)addr;
126 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
127
128 for (int i = 0; i <= 7; i++)
129 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
130 }
131#endif
132 }
133
134 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000135}
136
Matt Kopec7de48462013-03-06 17:20:48 +0000137// Wrapper for ptrace when logging is not required.
138// Sets errno to 0 prior to calling ptrace.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
140 long result = 0;
141 errno = 0;
142 result = ptrace(req, pid, (caddr_t)addr, data);
143 return result;
Matt Kopec7de48462013-03-06 17:20:48 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146#define PTRACE(req, pid, addr, data) \
147 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000148#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000150#endif
151
152//------------------------------------------------------------------------------
153// Static implementations of ProcessMonitor::ReadMemory and
154// ProcessMonitor::WriteMemory. This enables mutual recursion between these
155// functions without needed to go thru the thread funnel.
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +0000158 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 pi_desc.piod_op = PIOD_READ_D;
162 pi_desc.piod_offs = (void *)vm_addr;
163 pi_desc.piod_addr = buf;
164 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
167 error.SetErrorToErrno();
168 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000169}
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
Zachary Turner97206d52017-05-12 04:51:55 +0000172 const void *buf, size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 pi_desc.piod_op = PIOD_WRITE_D;
176 pi_desc.piod_offs = (void *)vm_addr;
177 pi_desc.piod_addr = (void *)buf;
178 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
181 error.SetErrorToErrno();
182 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000183}
184
185// Simple helper function to ensure flags are enabled on the given file
186// descriptor.
Zachary Turner97206d52017-05-12 04:51:55 +0000187static bool EnsureFDFlags(int fd, int flags, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 int status;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 if ((status = fcntl(fd, F_GETFL)) == -1) {
191 error.SetErrorToErrno();
192 return false;
193 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 if (fcntl(fd, F_SETFL, status | flags) == -1) {
196 error.SetErrorToErrno();
197 return false;
198 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000201}
202
203//------------------------------------------------------------------------------
204/// @class Operation
205/// @brief Represents a ProcessMonitor operation.
206///
207/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
208/// one that spawned or attached to the process from the start. Therefore, when
209/// a ProcessMonitor is asked to deliver or change the state of an inferior
210/// process the operation must be "funneled" to a specific thread to perform the
211/// task. The Operation class provides an abstract base for all services the
212/// ProcessMonitor must perform via the single virtual function Execute, thus
213/// encapsulating the code that needs to run in the privileged context.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214class Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000215public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 virtual ~Operation() {}
217 virtual void Execute(ProcessMonitor *monitor) = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218};
219
220//------------------------------------------------------------------------------
221/// @class ReadOperation
222/// @brief Implements ProcessMonitor::ReadMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223class ReadOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000224public:
Zachary Turner97206d52017-05-12 04:51:55 +0000225 ReadOperation(lldb::addr_t addr, void *buff, size_t size, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 size_t &result)
227 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
228 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000231
232private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 lldb::addr_t m_addr;
234 void *m_buff;
235 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000236 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000238};
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240void ReadOperation::Execute(ProcessMonitor *monitor) {
241 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000244}
245
246//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000247/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000248/// @brief Implements ProcessMonitor::WriteMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249class WriteOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000250public:
Zachary Turner97206d52017-05-12 04:51:55 +0000251 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
252 Status &error, size_t &result)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
254 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000257
258private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 lldb::addr_t m_addr;
260 const void *m_buff;
261 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000262 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000264};
265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266void WriteOperation::Execute(ProcessMonitor *monitor) {
267 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000270}
271
272//------------------------------------------------------------------------------
273/// @class ReadRegOperation
274/// @brief Implements ProcessMonitor::ReadRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275class ReadRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000276public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
278 RegisterValue &value, bool &result)
279 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
280 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000283
284private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 lldb::tid_t m_tid;
286 unsigned m_offset;
287 unsigned m_size;
288 RegisterValue &m_value;
289 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000290};
291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292void ReadRegOperation::Execute(ProcessMonitor *monitor) {
293 struct reg regs;
294 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
297 m_result = false;
298 } else {
299 // 'struct reg' contains only 32- or 64-bit register values. Punt on
300 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
301 // processes on powerpc64 (probably the same for i386 on amd64)
302 if (m_size == sizeof(uint32_t))
303 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
304 else if (m_size == sizeof(uint64_t))
305 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
306 else
307 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
308 m_result = true;
309 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000310}
311
312//------------------------------------------------------------------------------
313/// @class WriteRegOperation
314/// @brief Implements ProcessMonitor::WriteRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315class WriteRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000316public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 WriteRegOperation(lldb::tid_t tid, unsigned offset,
318 const RegisterValue &value, bool &result)
319 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000322
323private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 lldb::tid_t m_tid;
325 unsigned m_offset;
326 const RegisterValue &m_value;
327 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000328};
329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330void WriteRegOperation::Execute(ProcessMonitor *monitor) {
331 struct reg regs;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
334 m_result = false;
335 return;
336 }
337 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
338 (uintptr_t)m_value.GetAsUInt64();
339 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
340 m_result = false;
341 else
342 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000343}
344
345//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000346/// @class ReadDebugRegOperation
347/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348class ReadDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000349public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
351 RegisterValue &value, bool &result)
352 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
353 m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000356
357private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 lldb::tid_t m_tid;
359 unsigned m_offset;
360 unsigned m_size;
361 RegisterValue &m_value;
362 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000363};
364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365void ReadDebugRegOperation::Execute(ProcessMonitor *monitor) {
366 struct dbreg regs;
367 int rc;
Ed Mastea4be2c52014-02-19 18:34:06 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
370 m_result = false;
371 } else {
372 if (m_size == sizeof(uintptr_t))
373 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
374 else
375 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
376 m_result = true;
377 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000378}
379
380//------------------------------------------------------------------------------
381/// @class WriteDebugRegOperation
382/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383class WriteDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000384public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
386 const RegisterValue &value, bool &result)
387 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000390
391private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 lldb::tid_t m_tid;
393 unsigned m_offset;
394 const RegisterValue &m_value;
395 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000396};
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398void WriteDebugRegOperation::Execute(ProcessMonitor *monitor) {
399 struct dbreg regs;
Ed Mastea4be2c52014-02-19 18:34:06 +0000400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
402 m_result = false;
403 return;
404 }
405 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
406 (uintptr_t)m_value.GetAsUInt64();
407 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
408 m_result = false;
409 else
410 m_result = true;
Ed Mastea4be2c52014-02-19 18:34:06 +0000411}
412
413//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000414/// @class ReadGPROperation
415/// @brief Implements ProcessMonitor::ReadGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416class ReadGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000417public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
419 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000422
423private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 lldb::tid_t m_tid;
425 void *m_buf;
426 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000427};
428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429void ReadGPROperation::Execute(ProcessMonitor *monitor) {
430 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 errno = 0;
433 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
434 if (errno != 0)
435 m_result = false;
436 else
437 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000438}
439
440//------------------------------------------------------------------------------
441/// @class ReadFPROperation
442/// @brief Implements ProcessMonitor::ReadFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443class ReadFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000444public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
446 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000449
450private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 lldb::tid_t m_tid;
452 void *m_buf;
453 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000454};
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456void ReadFPROperation::Execute(ProcessMonitor *monitor) {
457 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
458 m_result = false;
459 else
460 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000461}
462
463//------------------------------------------------------------------------------
464/// @class WriteGPROperation
465/// @brief Implements ProcessMonitor::WriteGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466class WriteGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000467public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
469 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000470
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000472
473private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 lldb::tid_t m_tid;
475 void *m_buf;
476 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000477};
478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479void WriteGPROperation::Execute(ProcessMonitor *monitor) {
480 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
481 m_result = false;
482 else
483 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000484}
485
486//------------------------------------------------------------------------------
487/// @class WriteFPROperation
488/// @brief Implements ProcessMonitor::WriteFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489class WriteFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000490public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
492 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000495
496private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 lldb::tid_t m_tid;
498 void *m_buf;
499 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000500};
501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502void WriteFPROperation::Execute(ProcessMonitor *monitor) {
503 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
504 m_result = false;
505 else
506 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000507}
508
509//------------------------------------------------------------------------------
510/// @class ResumeOperation
511/// @brief Implements ProcessMonitor::Resume.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512class ResumeOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 ResumeOperation(uint32_t signo, bool &result)
515 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000518
519private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 uint32_t m_signo;
521 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000522};
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524void ResumeOperation::Execute(ProcessMonitor *monitor) {
525 lldb::pid_t pid = monitor->GetPID();
526 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
529 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000530
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) {
532 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Pavel Labath10c41f32017-06-06 14:06:17 +0000533 LLDB_LOG(log, "ResumeOperation ({0}) failed: {1}", pid,
534 llvm::sys::StrError(errno));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 m_result = false;
536 } else
537 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000538}
539
540//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000541/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000542/// @brief Implements ProcessMonitor::SingleStep.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543class SingleStepOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000544public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 SingleStepOperation(uint32_t signo, bool &result)
546 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000549
550private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 uint32_t m_signo;
552 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000553};
554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555void SingleStepOperation::Execute(ProcessMonitor *monitor) {
556 lldb::pid_t pid = monitor->GetPID();
557 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
560 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 if (PTRACE(PT_STEP, pid, NULL, data))
563 m_result = false;
564 else
565 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000566}
567
568//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000569/// @class LwpInfoOperation
570/// @brief Implements ProcessMonitor::GetLwpInfo.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571class LwpInfoOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000572public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
574 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000577
578private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 lldb::tid_t m_tid;
580 void *m_info;
581 bool &m_result;
582 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000583};
584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585void LwpInfoOperation::Execute(ProcessMonitor *monitor) {
586 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
589 m_result = false;
590 m_err = errno;
591 } else {
592 memcpy(m_info, &plwp, sizeof(plwp));
593 m_result = true;
594 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000595}
596
597//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000598/// @class ThreadSuspendOperation
599/// @brief Implements ProcessMonitor::ThreadSuspend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600class ThreadSuspendOperation : public Operation {
Ed Maste7fd845c2013-12-09 15:51:17 +0000601public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
603 : m_tid(tid), m_suspend(suspend), m_result(result) {}
Ed Maste7fd845c2013-12-09 15:51:17 +0000604
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 void Execute(ProcessMonitor *monitor);
Ed Maste7fd845c2013-12-09 15:51:17 +0000606
607private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 lldb::tid_t m_tid;
609 bool m_suspend;
610 bool &m_result;
611};
Ed Maste7fd845c2013-12-09 15:51:17 +0000612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613void ThreadSuspendOperation::Execute(ProcessMonitor *monitor) {
614 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
Ed Maste7fd845c2013-12-09 15:51:17 +0000615}
616
Ed Maste7fd845c2013-12-09 15:51:17 +0000617//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000618/// @class EventMessageOperation
619/// @brief Implements ProcessMonitor::GetEventMessage.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620class EventMessageOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000621public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
623 : m_tid(tid), m_message(message), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000626
627private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 lldb::tid_t m_tid;
629 unsigned long *m_message;
630 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000631};
632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633void EventMessageOperation::Execute(ProcessMonitor *monitor) {
634 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
637 m_result = false;
638 else {
639 if (plwp.pl_flags & PL_FLAG_FORKED) {
640 *m_message = plwp.pl_child_pid;
641 m_result = true;
642 } else
643 m_result = false;
644 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000645}
646
647//------------------------------------------------------------------------------
648/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000649/// @brief Implements ProcessMonitor::Kill.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650class KillOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000651public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 KillOperation(bool &result) : m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000653
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000655
656private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000658};
659
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660void KillOperation::Execute(ProcessMonitor *monitor) {
661 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (PTRACE(PT_KILL, pid, NULL, 0))
664 m_result = false;
665 else
666 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000667}
668
669//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000670/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000671/// @brief Implements ProcessMonitor::Detach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672class DetachOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000673public:
Zachary Turner97206d52017-05-12 04:51:55 +0000674 DetachOperation(Status &result) : m_error(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000677
678private:
Zachary Turner97206d52017-05-12 04:51:55 +0000679 Status &m_error;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000680};
681
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682void DetachOperation::Execute(ProcessMonitor *monitor) {
683 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
686 m_error.SetErrorToErrno();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000687}
688
689ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 : m_monitor(monitor) {
691 sem_init(&m_semaphore, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000692}
693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000695
696ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
697 lldb_private::Module *module,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 char const **argv, char const **envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000699 const FileSpec &stdin_file_spec,
700 const FileSpec &stdout_file_spec,
701 const FileSpec &stderr_file_spec,
702 const FileSpec &working_dir)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 : OperationArgs(monitor), m_module(module), m_argv(argv), m_envp(envp),
704 m_stdin_file_spec(stdin_file_spec), m_stdout_file_spec(stdout_file_spec),
705 m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000706
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707ProcessMonitor::LaunchArgs::~LaunchArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000708
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
710 : OperationArgs(monitor), m_pid(pid) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712ProcessMonitor::AttachArgs::~AttachArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000713
714//------------------------------------------------------------------------------
715/// The basic design of the ProcessMonitor is built around two threads.
716///
717/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
718/// for changes in the debugee state. When a change is detected a
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
720/// thread
Johnny Chen9ed5b492012-01-05 21:48:15 +0000721/// "drives" state changes in the debugger.
722///
723/// The second thread (@see OperationThread) is responsible for two things 1)
724/// launching or attaching to the inferior process, and then 2) servicing
725/// operations such as register reads/writes, stepping, etc. See the comments
726/// on the Operation class for more info as to why this is needed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727ProcessMonitor::ProcessMonitor(
728 ProcessFreeBSD *process, Module *module, const char *argv[],
729 const char *envp[], const FileSpec &stdin_file_spec,
730 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
731 const FileSpec &working_dir,
732 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Zachary Turner97206d52017-05-12 04:51:55 +0000733 lldb_private::Status &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000734 : m_process(static_cast<ProcessFreeBSD *>(process)),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
736 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 std::unique_ptr<LaunchArgs> args(
739 new LaunchArgs(this, module, argv, envp, stdin_file_spec,
740 stdout_file_spec, stderr_file_spec, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000741
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 sem_init(&m_operation_pending, 0, 0);
743 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000744
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 StartLaunchOpThread(args.get(), error);
746 if (!error.Success())
747 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000748
Pavel Labathc1a6b122017-07-03 09:25:55 +0000749 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
750 error.SetErrorToErrno();
751 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 // Check that the launch was a success.
755 if (!args->m_error.Success()) {
756 StopOpThread();
757 error = args->m_error;
758 return;
759 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 // Finally, start monitoring the child process for change in state.
762 m_monitor_thread = Host::StartMonitoringChildProcess(
763 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
764 GetPID(), true);
765 if (!m_monitor_thread.IsJoinable()) {
766 error.SetErrorToGenericError();
767 error.SetErrorString("Process launch failed.");
768 return;
769 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000770}
771
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +0000773 lldb_private::Status &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
775 m_terminal_fd(-1), m_operation(0) {
776 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 sem_init(&m_operation_pending, 0, 0);
779 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000780
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000782
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 StartAttachOpThread(args.get(), error);
784 if (!error.Success())
785 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000786
Pavel Labathc1a6b122017-07-03 09:25:55 +0000787 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
788 error.SetErrorToErrno();
789 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000791
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 // Check that the attach was a success.
793 if (!args->m_error.Success()) {
794 StopOpThread();
795 error = args->m_error;
796 return;
797 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000798
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799 // Finally, start monitoring the child process for change in state.
800 m_monitor_thread = Host::StartMonitoringChildProcess(
801 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
802 GetPID(), true);
803 if (!m_monitor_thread.IsJoinable()) {
804 error.SetErrorToGenericError();
805 error.SetErrorString("Process attach failed.");
806 return;
807 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000808}
809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000811
812//------------------------------------------------------------------------------
813// Thread setup and tear down.
Zachary Turner97206d52017-05-12 04:51:55 +0000814void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 if (m_operation_thread.IsJoinable())
818 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000819
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 m_operation_thread =
821 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822}
823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824void *ProcessMonitor::LaunchOpThread(void *arg) {
825 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000826
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 if (!Launch(args)) {
828 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 }
831
832 ServeOperation(args);
833 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000834}
835
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836bool ProcessMonitor::Launch(LaunchArgs *args) {
837 ProcessMonitor *monitor = args->m_monitor;
838 ProcessFreeBSD &process = monitor->GetProcess();
839 const char **argv = args->m_argv;
840 const char **envp = args->m_envp;
841 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
842 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
843 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
844 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000845
Pavel Labath07d6f882017-12-11 10:09:14 +0000846 PseudoTerminal terminal;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 const size_t err_len = 1024;
848 char err_str[err_len];
849 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 // Propagate the environment if one is not supplied.
852 if (envp == NULL || envp[0] == NULL)
853 envp = const_cast<const char **>(environ);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 if ((pid = terminal.Fork(err_str, err_len)) == -1) {
856 args->m_error.SetErrorToGenericError();
857 args->m_error.SetErrorString("Process fork failed.");
858 goto FINISH;
859 }
860
861 // Recognized child exit status codes.
862 enum {
863 ePtraceFailed = 1,
864 eDupStdinFailed,
865 eDupStdoutFailed,
866 eDupStderrFailed,
867 eChdirFailed,
868 eExecFailed,
869 eSetGidFailed
870 };
871
872 // Child process.
873 if (pid == 0) {
874 // Trace this process.
875 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
876 exit(ePtraceFailed);
877
878 // terminal has already dupped the tty descriptors to stdin/out/err.
879 // This closes original fd from which they were copied (and avoids
880 // leaking descriptors to the debugged process.
881 terminal.CloseSlaveFileDescriptor();
882
883 // Do not inherit setgid powers.
884 if (setgid(getgid()) != 0)
885 exit(eSetGidFailed);
886
887 // Let us have our own process group.
888 setpgid(0, 0);
889
890 // Dup file descriptors if needed.
891 //
892 // FIXME: If two or more of the paths are the same we needlessly open
893 // the same file multiple times.
894 if (stdin_file_spec)
895 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
896 exit(eDupStdinFailed);
897
898 if (stdout_file_spec)
899 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
900 exit(eDupStdoutFailed);
901
902 if (stderr_file_spec)
903 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
904 exit(eDupStderrFailed);
905
906 // Change working directory
907 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
908 exit(eChdirFailed);
909
910 // Execute. We should never return.
911 execve(argv[0], const_cast<char *const *>(argv),
912 const_cast<char *const *>(envp));
913 exit(eExecFailed);
914 }
915
916 // Wait for the child process to to trap on its call to execve.
917 ::pid_t wpid;
918 int status;
919 if ((wpid = waitpid(pid, &status, 0)) < 0) {
920 args->m_error.SetErrorToErrno();
921 goto FINISH;
922 } else if (WIFEXITED(status)) {
923 // open, dup or execve likely failed for some reason.
924 args->m_error.SetErrorToGenericError();
925 switch (WEXITSTATUS(status)) {
926 case ePtraceFailed:
927 args->m_error.SetErrorString("Child ptrace failed.");
928 break;
929 case eDupStdinFailed:
930 args->m_error.SetErrorString("Child open stdin failed.");
931 break;
932 case eDupStdoutFailed:
933 args->m_error.SetErrorString("Child open stdout failed.");
934 break;
935 case eDupStderrFailed:
936 args->m_error.SetErrorString("Child open stderr failed.");
937 break;
938 case eChdirFailed:
939 args->m_error.SetErrorString("Child failed to set working directory.");
940 break;
941 case eExecFailed:
942 args->m_error.SetErrorString("Child exec failed.");
943 break;
944 case eSetGidFailed:
945 args->m_error.SetErrorString("Child setgid failed.");
946 break;
947 default:
948 args->m_error.SetErrorString("Child returned unknown exit status.");
949 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000950 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 goto FINISH;
952 }
953 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
954 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000955
956#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 // Have the child raise an event on exit. This is used to keep the child in
958 // limbo until it is destroyed.
959 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
960 args->m_error.SetErrorToErrno();
961 goto FINISH;
962 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000963#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 // Release the master terminal descriptor and pass it off to the
965 // ProcessMonitor instance. Similarly stash the inferior pid.
966 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
967 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 // Set the terminal fd to be in non blocking mode (it simplifies the
970 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
971 // descriptor to read from).
972 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
973 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000974
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000976
977FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000979}
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
Zachary Turner97206d52017-05-12 04:51:55 +0000982 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 if (m_operation_thread.IsJoinable())
986 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 m_operation_thread =
989 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000990}
991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992void *ProcessMonitor::AttachOpThread(void *arg) {
993 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000994
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 ServeOperation(args);
998 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000999}
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001void ProcessMonitor::Attach(AttachArgs *args) {
1002 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 ProcessMonitor *monitor = args->m_monitor;
1005 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 if (pid <= 1) {
1008 args->m_error.SetErrorToGenericError();
1009 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1010 return;
1011 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001012
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 // Attach to the requested process.
1014 if (PTRACE(PT_ATTACH, 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 int status;
1020 if ((status = waitpid(pid, NULL, 0)) < 0) {
1021 args->m_error.SetErrorToErrno();
1022 return;
1023 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001026}
1027
Ed Maste7fd845c2013-12-09 15:51:17 +00001028size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1030 lwpid_t *tids;
1031 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1036 if (tdcnt <= 0)
1037 return 0;
1038 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1039 if (tids == NULL)
1040 return 0;
1041 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001042 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 return 0;
1044 }
1045 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1046 free(tids);
1047 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001048}
1049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1051 bool exited, int signal, int status) {
1052 ProcessMessage message;
1053 ProcessFreeBSD *process = monitor->m_process;
1054 assert(process);
1055 bool stop_monitoring;
1056 struct ptrace_lwpinfo plwp;
1057 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 if (exited) {
1062 if (log)
1063 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1064 __FUNCTION__, pid);
1065 message = ProcessMessage::Exit(pid, status);
1066 process->SendMessage(message);
1067 return pid == process->GetID();
1068 }
Ed Mastea02f5532013-07-02 16:45:16 +00001069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1071 stop_monitoring = true; // pid is gone. Bail.
1072 else {
1073 switch (plwp.pl_siginfo.si_signo) {
1074 case SIGTRAP:
1075 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1076 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001077
Johnny Chen9ed5b492012-01-05 21:48:15 +00001078 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1080 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001081 }
1082
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 process->SendMessage(message);
1084 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1085 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001086
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001088}
1089
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1091 const siginfo_t *info,
1092 lldb::tid_t tid) {
1093 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001096
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 assert(monitor);
1098 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 switch (info->si_code) {
1101 default:
1102 assert(false && "Unexpected SIGTRAP code!");
1103 break;
1104
1105 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
1106 // The inferior process is about to exit. Maintain the process in a
1107 // state of "limbo" until we are explicitly commanded to detach,
1108 // destroy, resume, etc.
1109 unsigned long data = 0;
1110 if (!monitor->GetEventMessage(tid, &data))
1111 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001112 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1114 "= %" PRIu64,
1115 __FUNCTION__, data, tid);
1116 message = ProcessMessage::Limbo(tid, (data >> 8));
1117 break;
1118 }
Ed Mastea02f5532013-07-02 16:45:16 +00001119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 case 0:
1121 case TRAP_TRACE:
Ed Masted6fa2c32017-05-26 03:15:46 +00001122#ifdef TRAP_CAP
1123 // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1124 case TRAP_CAP:
1125#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 if (log)
1127 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1128 " : si_code = %d",
1129 __FUNCTION__, tid, info->si_code);
1130 message = ProcessMessage::Trace(tid);
1131 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001132
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 case SI_KERNEL:
1134 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001135 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1136 if (log)
1137 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1138 "event, tid = %" PRIu64,
1139 __FUNCTION__, tid);
1140 message = ProcessMessage::Trace(tid);
1141 } else {
1142 if (log)
1143 log->Printf(
1144 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1145 __FUNCTION__, tid);
1146 message = ProcessMessage::Break(tid);
1147 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 break;
1149 }
1150
1151 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001152}
1153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1155 const siginfo_t *info,
1156 lldb::tid_t tid) {
1157 ProcessMessage message;
1158 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001161
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1163 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1164 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1165 //
1166 // IOW, user generated signals never generate what we consider to be a
1167 // "crash".
1168 //
1169 // Similarly, ACK signals generated by this monitor.
1170 if (info->si_code == SI_USER) {
1171 if (log)
1172 log->Printf(
1173 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1174 __FUNCTION__,
1175 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1176 "SI_USER", info->si_pid);
1177 if (info->si_pid == getpid())
1178 return ProcessMessage::SignalDelivered(tid, signo);
1179 else
1180 return ProcessMessage::Signal(tid, signo);
1181 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 if (log)
1184 log->Printf(
1185 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1186 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 switch (signo) {
1189 case SIGSEGV:
1190 case SIGILL:
1191 case SIGFPE:
1192 case SIGBUS:
1193 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1194 const auto reason = GetCrashReason(*info);
Ed Maste5e82ca32017-08-10 13:47:17 +00001195 if (reason != CrashReason::eInvalidCrashReason) {
1196 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1197 } // else; Use atleast si_signo info for other si_code
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 }
1199
1200 // Everything else is "normal" and does not require any special action on
1201 // our part.
1202 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001203}
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205void ProcessMonitor::ServeOperation(OperationArgs *args) {
1206 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001207
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208 // We are finised with the arguments and are ready to go. Sync with the
1209 // parent thread and start serving operations on the inferior.
1210 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 for (;;) {
1213 // wait for next pending operation
1214 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001215
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 monitor->m_operation->Execute(monitor);
1217
1218 // notify calling thread that operation is complete
1219 sem_post(&monitor->m_operation_done);
1220 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001221}
1222
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223void ProcessMonitor::DoOperation(Operation *op) {
1224 std::lock_guard<std::mutex> guard(m_operation_mutex);
1225
1226 m_operation = op;
1227
1228 // notify operation thread that an operation is ready to be processed
1229 sem_post(&m_operation_pending);
1230
1231 // wait for operation to complete
1232 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001233}
1234
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +00001236 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237 size_t result;
1238 ReadOperation op(vm_addr, buf, size, error, result);
1239 DoOperation(&op);
1240 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001241}
1242
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +00001244 size_t size, lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245 size_t result;
1246 WriteOperation op(vm_addr, buf, size, error, result);
1247 DoOperation(&op);
1248 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001249}
1250
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001252 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253 RegisterValue &value) {
1254 bool result;
1255 ReadRegOperation op(tid, offset, size, value, result);
1256 DoOperation(&op);
1257 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001258}
1259
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001261 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 const RegisterValue &value) {
1263 bool result;
1264 WriteRegOperation op(tid, offset, value, result);
1265 DoOperation(&op);
1266 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001267}
1268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269bool ProcessMonitor::ReadDebugRegisterValue(
1270 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1271 lldb_private::RegisterValue &value) {
1272 bool result;
1273 ReadDebugRegOperation op(tid, offset, size, value, result);
1274 DoOperation(&op);
1275 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001276}
1277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278bool ProcessMonitor::WriteDebugRegisterValue(
1279 lldb::tid_t tid, unsigned offset, const char *reg_name,
1280 const lldb_private::RegisterValue &value) {
1281 bool result;
1282 WriteDebugRegOperation op(tid, offset, value, result);
1283 DoOperation(&op);
1284 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001285}
1286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1288 bool result;
1289 ReadGPROperation op(tid, buf, result);
1290 DoOperation(&op);
1291 return result;
1292}
1293
1294bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1295 bool result;
1296 ReadFPROperation op(tid, buf, result);
1297 DoOperation(&op);
1298 return result;
1299}
1300
1301bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1302 size_t buf_size, unsigned int regset) {
1303 return false;
1304}
1305
1306bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1307 bool result;
1308 WriteGPROperation op(tid, buf, result);
1309 DoOperation(&op);
1310 return result;
1311}
1312
1313bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1314 bool result;
1315 WriteFPROperation op(tid, buf, result);
1316 DoOperation(&op);
1317 return result;
1318}
1319
1320bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1321 size_t buf_size, unsigned int regset) {
1322 return false;
1323}
1324
1325bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1326 return false;
1327}
1328
1329bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1330 bool result;
1331 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1332
1333 if (log) {
1334 const char *signame =
1335 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1336 if (signame == nullptr)
1337 signame = "<none>";
1338 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1339 __FUNCTION__, GetPID(), signame);
1340 }
1341 ResumeOperation op(signo, result);
1342 DoOperation(&op);
1343 if (log)
1344 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1345 result ? "true" : "false");
1346 return result;
1347}
1348
1349bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1350 bool result;
1351 SingleStepOperation op(signo, result);
1352 DoOperation(&op);
1353 return result;
1354}
1355
1356bool ProcessMonitor::Kill() {
1357 bool result;
1358 KillOperation op(result);
1359 DoOperation(&op);
1360 return result;
1361}
1362
1363bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1364 int &ptrace_err) {
1365 bool result;
1366 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1367 DoOperation(&op);
1368 return result;
1369}
1370
1371bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1372 bool result;
1373 ThreadSuspendOperation op(tid, suspend, result);
1374 DoOperation(&op);
1375 return result;
1376}
1377
1378bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1379 bool result;
1380 EventMessageOperation op(tid, message, result);
1381 DoOperation(&op);
1382 return result;
1383}
1384
Zachary Turner97206d52017-05-12 04:51:55 +00001385lldb_private::Status ProcessMonitor::Detach(lldb::tid_t tid) {
1386 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387 if (tid != LLDB_INVALID_THREAD_ID) {
1388 DetachOperation op(error);
1389 DoOperation(&op);
1390 }
1391 return error;
1392}
1393
1394bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1395 int flags) {
1396 int target_fd = open(file_spec.GetCString(), flags, 0666);
1397
1398 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001399 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001400
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001402 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403
1404 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001405}
1406
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407void ProcessMonitor::StopMonitoringChildProcess() {
1408 if (m_monitor_thread.IsJoinable()) {
1409 m_monitor_thread.Cancel();
1410 m_monitor_thread.Join(nullptr);
1411 m_monitor_thread.Reset();
1412 }
Ed Maste68f51792013-10-18 19:16:44 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415void ProcessMonitor::StopMonitor() {
1416 StopMonitoringChildProcess();
1417 StopOpThread();
1418 sem_destroy(&m_operation_pending);
1419 sem_destroy(&m_operation_done);
1420 if (m_terminal_fd >= 0) {
1421 close(m_terminal_fd);
1422 m_terminal_fd = -1;
1423 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001424}
1425
Andrew Kaylord4d54992013-09-17 00:30:24 +00001426// FIXME: On Linux, when a new thread is created, we receive to notifications,
1427// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1428// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1429// the new child thread indicating that it has is stopped because we attached.
1430// We have no guarantee of the order in which these arrive, but we need both
1431// before we are ready to proceed. We currently keep a list of threads which
1432// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1433// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1434// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1435//
1436// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1437// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1438// logically needed.
1439//
1440// We really should figure out what actually happens on FreeBSD and move the
1441// Linux-specific logic out of ProcessPOSIX as needed.
1442
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001444
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445void ProcessMonitor::StopOpThread() {
1446 if (!m_operation_thread.IsJoinable())
1447 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001448
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449 m_operation_thread.Cancel();
1450 m_operation_thread.Join(nullptr);
1451 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001452}