blob: 10dd14753914289eda681821f163fcacfd6bcf83 [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
749WAIT_AGAIN:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 // Wait for the operation thread to initialize.
751 if (sem_wait(&args->m_semaphore)) {
752 if (errno == EINTR)
753 goto WAIT_AGAIN;
754 else {
755 error.SetErrorToErrno();
756 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000757 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 // Check that the launch was a success.
761 if (!args->m_error.Success()) {
762 StopOpThread();
763 error = args->m_error;
764 return;
765 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000766
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 // Finally, start monitoring the child process for change in state.
768 m_monitor_thread = Host::StartMonitoringChildProcess(
769 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
770 GetPID(), true);
771 if (!m_monitor_thread.IsJoinable()) {
772 error.SetErrorToGenericError();
773 error.SetErrorString("Process launch failed.");
774 return;
775 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000776}
777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +0000779 lldb_private::Status &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
781 m_terminal_fd(-1), m_operation(0) {
782 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 sem_init(&m_operation_pending, 0, 0);
785 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000786
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 StartAttachOpThread(args.get(), error);
790 if (!error.Success())
791 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000792
793WAIT_AGAIN:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 // Wait for the operation thread to initialize.
795 if (sem_wait(&args->m_semaphore)) {
796 if (errno == EINTR)
797 goto WAIT_AGAIN;
798 else {
799 error.SetErrorToErrno();
800 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000803
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 // Check that the attach was a success.
805 if (!args->m_error.Success()) {
806 StopOpThread();
807 error = args->m_error;
808 return;
809 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 // Finally, start monitoring the child process for change in state.
812 m_monitor_thread = Host::StartMonitoringChildProcess(
813 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
814 GetPID(), true);
815 if (!m_monitor_thread.IsJoinable()) {
816 error.SetErrorToGenericError();
817 error.SetErrorString("Process attach failed.");
818 return;
819 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000820}
821
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000823
824//------------------------------------------------------------------------------
825// Thread setup and tear down.
Zachary Turner97206d52017-05-12 04:51:55 +0000826void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000828
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 if (m_operation_thread.IsJoinable())
830 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000831
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 m_operation_thread =
833 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000834}
835
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836void *ProcessMonitor::LaunchOpThread(void *arg) {
837 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000838
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 if (!Launch(args)) {
840 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000841 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 }
843
844 ServeOperation(args);
845 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000846}
847
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848bool ProcessMonitor::Launch(LaunchArgs *args) {
849 ProcessMonitor *monitor = args->m_monitor;
850 ProcessFreeBSD &process = monitor->GetProcess();
851 const char **argv = args->m_argv;
852 const char **envp = args->m_envp;
853 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
854 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
855 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
856 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000857
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 lldb_utility::PseudoTerminal terminal;
859 const size_t err_len = 1024;
860 char err_str[err_len];
861 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000862
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 // Propagate the environment if one is not supplied.
864 if (envp == NULL || envp[0] == NULL)
865 envp = const_cast<const char **>(environ);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000866
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 if ((pid = terminal.Fork(err_str, err_len)) == -1) {
868 args->m_error.SetErrorToGenericError();
869 args->m_error.SetErrorString("Process fork failed.");
870 goto FINISH;
871 }
872
873 // Recognized child exit status codes.
874 enum {
875 ePtraceFailed = 1,
876 eDupStdinFailed,
877 eDupStdoutFailed,
878 eDupStderrFailed,
879 eChdirFailed,
880 eExecFailed,
881 eSetGidFailed
882 };
883
884 // Child process.
885 if (pid == 0) {
886 // Trace this process.
887 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
888 exit(ePtraceFailed);
889
890 // terminal has already dupped the tty descriptors to stdin/out/err.
891 // This closes original fd from which they were copied (and avoids
892 // leaking descriptors to the debugged process.
893 terminal.CloseSlaveFileDescriptor();
894
895 // Do not inherit setgid powers.
896 if (setgid(getgid()) != 0)
897 exit(eSetGidFailed);
898
899 // Let us have our own process group.
900 setpgid(0, 0);
901
902 // Dup file descriptors if needed.
903 //
904 // FIXME: If two or more of the paths are the same we needlessly open
905 // the same file multiple times.
906 if (stdin_file_spec)
907 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
908 exit(eDupStdinFailed);
909
910 if (stdout_file_spec)
911 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
912 exit(eDupStdoutFailed);
913
914 if (stderr_file_spec)
915 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
916 exit(eDupStderrFailed);
917
918 // Change working directory
919 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
920 exit(eChdirFailed);
921
922 // Execute. We should never return.
923 execve(argv[0], const_cast<char *const *>(argv),
924 const_cast<char *const *>(envp));
925 exit(eExecFailed);
926 }
927
928 // Wait for the child process to to trap on its call to execve.
929 ::pid_t wpid;
930 int status;
931 if ((wpid = waitpid(pid, &status, 0)) < 0) {
932 args->m_error.SetErrorToErrno();
933 goto FINISH;
934 } else if (WIFEXITED(status)) {
935 // open, dup or execve likely failed for some reason.
936 args->m_error.SetErrorToGenericError();
937 switch (WEXITSTATUS(status)) {
938 case ePtraceFailed:
939 args->m_error.SetErrorString("Child ptrace failed.");
940 break;
941 case eDupStdinFailed:
942 args->m_error.SetErrorString("Child open stdin failed.");
943 break;
944 case eDupStdoutFailed:
945 args->m_error.SetErrorString("Child open stdout failed.");
946 break;
947 case eDupStderrFailed:
948 args->m_error.SetErrorString("Child open stderr failed.");
949 break;
950 case eChdirFailed:
951 args->m_error.SetErrorString("Child failed to set working directory.");
952 break;
953 case eExecFailed:
954 args->m_error.SetErrorString("Child exec failed.");
955 break;
956 case eSetGidFailed:
957 args->m_error.SetErrorString("Child setgid failed.");
958 break;
959 default:
960 args->m_error.SetErrorString("Child returned unknown exit status.");
961 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000962 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 goto FINISH;
964 }
965 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
966 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000967
968#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 // Have the child raise an event on exit. This is used to keep the child in
970 // limbo until it is destroyed.
971 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
972 args->m_error.SetErrorToErrno();
973 goto FINISH;
974 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000975#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 // Release the master terminal descriptor and pass it off to the
977 // ProcessMonitor instance. Similarly stash the inferior pid.
978 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
979 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 // Set the terminal fd to be in non blocking mode (it simplifies the
982 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
983 // descriptor to read from).
984 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
985 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000988
989FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000991}
992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
Zachary Turner97206d52017-05-12 04:51:55 +0000994 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 if (m_operation_thread.IsJoinable())
998 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 m_operation_thread =
1001 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001002}
1003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004void *ProcessMonitor::AttachOpThread(void *arg) {
1005 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001008
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 ServeOperation(args);
1010 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001011}
1012
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013void ProcessMonitor::Attach(AttachArgs *args) {
1014 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 ProcessMonitor *monitor = args->m_monitor;
1017 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 if (pid <= 1) {
1020 args->m_error.SetErrorToGenericError();
1021 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1022 return;
1023 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 // Attach to the requested process.
1026 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
1027 args->m_error.SetErrorToErrno();
1028 return;
1029 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001030
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031 int status;
1032 if ((status = waitpid(pid, NULL, 0)) < 0) {
1033 args->m_error.SetErrorToErrno();
1034 return;
1035 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001036
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001038}
1039
Ed Maste7fd845c2013-12-09 15:51:17 +00001040size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1042 lwpid_t *tids;
1043 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001046
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1048 if (tdcnt <= 0)
1049 return 0;
1050 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1051 if (tids == NULL)
1052 return 0;
1053 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001054 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 return 0;
1056 }
1057 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1058 free(tids);
1059 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001060}
1061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1063 bool exited, int signal, int status) {
1064 ProcessMessage message;
1065 ProcessFreeBSD *process = monitor->m_process;
1066 assert(process);
1067 bool stop_monitoring;
1068 struct ptrace_lwpinfo plwp;
1069 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 if (exited) {
1074 if (log)
1075 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1076 __FUNCTION__, pid);
1077 message = ProcessMessage::Exit(pid, status);
1078 process->SendMessage(message);
1079 return pid == process->GetID();
1080 }
Ed Mastea02f5532013-07-02 16:45:16 +00001081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1083 stop_monitoring = true; // pid is gone. Bail.
1084 else {
1085 switch (plwp.pl_siginfo.si_signo) {
1086 case SIGTRAP:
1087 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1088 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001089
Johnny Chen9ed5b492012-01-05 21:48:15 +00001090 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1092 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001093 }
1094
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 process->SendMessage(message);
1096 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1097 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001098
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001100}
1101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1103 const siginfo_t *info,
1104 lldb::tid_t tid) {
1105 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001106
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 assert(monitor);
1110 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001111
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 switch (info->si_code) {
1113 default:
1114 assert(false && "Unexpected SIGTRAP code!");
1115 break;
1116
1117 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
1118 // The inferior process is about to exit. Maintain the process in a
1119 // state of "limbo" until we are explicitly commanded to detach,
1120 // destroy, resume, etc.
1121 unsigned long data = 0;
1122 if (!monitor->GetEventMessage(tid, &data))
1123 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001124 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1126 "= %" PRIu64,
1127 __FUNCTION__, data, tid);
1128 message = ProcessMessage::Limbo(tid, (data >> 8));
1129 break;
1130 }
Ed Mastea02f5532013-07-02 16:45:16 +00001131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 case 0:
1133 case TRAP_TRACE:
Ed Masted6fa2c32017-05-26 03:15:46 +00001134#ifdef TRAP_CAP
1135 // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1136 case TRAP_CAP:
1137#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 if (log)
1139 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1140 " : si_code = %d",
1141 __FUNCTION__, tid, info->si_code);
1142 message = ProcessMessage::Trace(tid);
1143 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 case SI_KERNEL:
1146 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001147 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1148 if (log)
1149 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1150 "event, tid = %" PRIu64,
1151 __FUNCTION__, tid);
1152 message = ProcessMessage::Trace(tid);
1153 } else {
1154 if (log)
1155 log->Printf(
1156 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1157 __FUNCTION__, tid);
1158 message = ProcessMessage::Break(tid);
1159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 break;
1161 }
1162
1163 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001164}
1165
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1167 const siginfo_t *info,
1168 lldb::tid_t tid) {
1169 ProcessMessage message;
1170 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001173
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1175 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1176 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1177 //
1178 // IOW, user generated signals never generate what we consider to be a
1179 // "crash".
1180 //
1181 // Similarly, ACK signals generated by this monitor.
1182 if (info->si_code == SI_USER) {
1183 if (log)
1184 log->Printf(
1185 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1186 __FUNCTION__,
1187 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1188 "SI_USER", info->si_pid);
1189 if (info->si_pid == getpid())
1190 return ProcessMessage::SignalDelivered(tid, signo);
1191 else
1192 return ProcessMessage::Signal(tid, signo);
1193 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001194
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 if (log)
1196 log->Printf(
1197 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1198 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001199
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 switch (signo) {
1201 case SIGSEGV:
1202 case SIGILL:
1203 case SIGFPE:
1204 case SIGBUS:
1205 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1206 const auto reason = GetCrashReason(*info);
1207 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1208 }
1209
1210 // Everything else is "normal" and does not require any special action on
1211 // our part.
1212 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001213}
1214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215void ProcessMonitor::ServeOperation(OperationArgs *args) {
1216 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001217
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 // We are finised with the arguments and are ready to go. Sync with the
1219 // parent thread and start serving operations on the inferior.
1220 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001221
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 for (;;) {
1223 // wait for next pending operation
1224 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001225
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226 monitor->m_operation->Execute(monitor);
1227
1228 // notify calling thread that operation is complete
1229 sem_post(&monitor->m_operation_done);
1230 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001231}
1232
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233void ProcessMonitor::DoOperation(Operation *op) {
1234 std::lock_guard<std::mutex> guard(m_operation_mutex);
1235
1236 m_operation = op;
1237
1238 // notify operation thread that an operation is ready to be processed
1239 sem_post(&m_operation_pending);
1240
1241 // wait for operation to complete
1242 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001243}
1244
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +00001246 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 size_t result;
1248 ReadOperation op(vm_addr, buf, size, error, result);
1249 DoOperation(&op);
1250 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001251}
1252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +00001254 size_t size, lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 size_t result;
1256 WriteOperation op(vm_addr, buf, size, error, result);
1257 DoOperation(&op);
1258 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001259}
1260
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001262 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 RegisterValue &value) {
1264 bool result;
1265 ReadRegOperation op(tid, offset, size, value, result);
1266 DoOperation(&op);
1267 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001268}
1269
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001271 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 const RegisterValue &value) {
1273 bool result;
1274 WriteRegOperation op(tid, offset, value, result);
1275 DoOperation(&op);
1276 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001277}
1278
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279bool ProcessMonitor::ReadDebugRegisterValue(
1280 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1281 lldb_private::RegisterValue &value) {
1282 bool result;
1283 ReadDebugRegOperation op(tid, offset, size, value, result);
1284 DoOperation(&op);
1285 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001286}
1287
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288bool ProcessMonitor::WriteDebugRegisterValue(
1289 lldb::tid_t tid, unsigned offset, const char *reg_name,
1290 const lldb_private::RegisterValue &value) {
1291 bool result;
1292 WriteDebugRegOperation op(tid, offset, value, result);
1293 DoOperation(&op);
1294 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001295}
1296
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1298 bool result;
1299 ReadGPROperation op(tid, buf, result);
1300 DoOperation(&op);
1301 return result;
1302}
1303
1304bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1305 bool result;
1306 ReadFPROperation op(tid, buf, result);
1307 DoOperation(&op);
1308 return result;
1309}
1310
1311bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1312 size_t buf_size, unsigned int regset) {
1313 return false;
1314}
1315
1316bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1317 bool result;
1318 WriteGPROperation op(tid, buf, result);
1319 DoOperation(&op);
1320 return result;
1321}
1322
1323bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1324 bool result;
1325 WriteFPROperation op(tid, buf, result);
1326 DoOperation(&op);
1327 return result;
1328}
1329
1330bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1331 size_t buf_size, unsigned int regset) {
1332 return false;
1333}
1334
1335bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1336 return false;
1337}
1338
1339bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1340 bool result;
1341 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1342
1343 if (log) {
1344 const char *signame =
1345 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1346 if (signame == nullptr)
1347 signame = "<none>";
1348 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1349 __FUNCTION__, GetPID(), signame);
1350 }
1351 ResumeOperation op(signo, result);
1352 DoOperation(&op);
1353 if (log)
1354 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1355 result ? "true" : "false");
1356 return result;
1357}
1358
1359bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1360 bool result;
1361 SingleStepOperation op(signo, result);
1362 DoOperation(&op);
1363 return result;
1364}
1365
1366bool ProcessMonitor::Kill() {
1367 bool result;
1368 KillOperation op(result);
1369 DoOperation(&op);
1370 return result;
1371}
1372
1373bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1374 int &ptrace_err) {
1375 bool result;
1376 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1377 DoOperation(&op);
1378 return result;
1379}
1380
1381bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1382 bool result;
1383 ThreadSuspendOperation op(tid, suspend, result);
1384 DoOperation(&op);
1385 return result;
1386}
1387
1388bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1389 bool result;
1390 EventMessageOperation op(tid, message, result);
1391 DoOperation(&op);
1392 return result;
1393}
1394
Zachary Turner97206d52017-05-12 04:51:55 +00001395lldb_private::Status ProcessMonitor::Detach(lldb::tid_t tid) {
1396 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 if (tid != LLDB_INVALID_THREAD_ID) {
1398 DetachOperation op(error);
1399 DoOperation(&op);
1400 }
1401 return error;
1402}
1403
1404bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1405 int flags) {
1406 int target_fd = open(file_spec.GetCString(), flags, 0666);
1407
1408 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001409 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001410
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001412 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413
1414 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001415}
1416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417void ProcessMonitor::StopMonitoringChildProcess() {
1418 if (m_monitor_thread.IsJoinable()) {
1419 m_monitor_thread.Cancel();
1420 m_monitor_thread.Join(nullptr);
1421 m_monitor_thread.Reset();
1422 }
Ed Maste68f51792013-10-18 19:16:44 +00001423}
1424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425void ProcessMonitor::StopMonitor() {
1426 StopMonitoringChildProcess();
1427 StopOpThread();
1428 sem_destroy(&m_operation_pending);
1429 sem_destroy(&m_operation_done);
1430 if (m_terminal_fd >= 0) {
1431 close(m_terminal_fd);
1432 m_terminal_fd = -1;
1433 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001434}
1435
Andrew Kaylord4d54992013-09-17 00:30:24 +00001436// FIXME: On Linux, when a new thread is created, we receive to notifications,
1437// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1438// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1439// the new child thread indicating that it has is stopped because we attached.
1440// We have no guarantee of the order in which these arrive, but we need both
1441// before we are ready to proceed. We currently keep a list of threads which
1442// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1443// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1444// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1445//
1446// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1447// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1448// logically needed.
1449//
1450// We really should figure out what actually happens on FreeBSD and move the
1451// Linux-specific logic out of ProcessPOSIX as needed.
1452
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455void ProcessMonitor::StopOpThread() {
1456 if (!m_operation_thread.IsJoinable())
1457 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001458
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 m_operation_thread.Cancel();
1460 m_operation_thread.Join(nullptr);
1461 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001462}