blob: feba3af8fcf1e1f2772e61c714f2472d1a9a0e1f [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
Johnny Chen9ed5b492012-01-05 21:48:15 +000041using namespace lldb;
42using namespace lldb_private;
43
44// We disable the tracing of ptrace calls for integration builds to
45// avoid the additional indirection and checks.
46#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
47// Wrapper for ptrace to catch errors and log calls.
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049const char *Get_PT_IO_OP(int op) {
50 switch (op) {
51 case PIOD_READ_D:
52 return "READ_D";
53 case PIOD_WRITE_D:
54 return "WRITE_D";
55 case PIOD_READ_I:
56 return "READ_I";
57 case PIOD_WRITE_I:
58 return "WRITE_I";
59 default:
60 return "Unknown op";
61 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000062}
63
Matt Kopec7de48462013-03-06 17:20:48 +000064// Wrapper for ptrace to catch errors and log calls.
Kate Stoneb9c1b512016-09-06 20:57:50 +000065// Note that ptrace sets errno on error because -1 is reserved as a valid
66// result.
67extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
68 const char *reqName, const char *file, int line) {
69 long int result;
Johnny Chen9ed5b492012-01-05 21:48:15 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 if (log) {
74 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
75 reqName, pid, addr, data, file, line);
76 if (req == PT_IO) {
77 struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
78
79 log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
80 (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 errno = 0;
87 result = ptrace(req, pid, (caddr_t)addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (log && errno != 0) {
92 const char *str;
93 switch (errno) {
94 case ESRCH:
95 str = "ESRCH";
96 break;
97 case EINVAL:
98 str = "EINVAL";
99 break;
100 case EBUSY:
101 str = "EBUSY";
102 break;
103 case EPERM:
104 str = "EPERM";
105 break;
106 default:
107 str = "<unknown>";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
110 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000113#ifdef __amd64__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 if (req == PT_GETREGS) {
115 struct reg *r = (struct reg *)addr;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
118 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Ed Mastea4be2c52014-02-19 18:34:06 +0000119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
121 struct dbreg *r = (struct dbreg *)addr;
122 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
123
124 for (int i = 0; i <= 7; i++)
125 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
126 }
127#endif
128 }
129
130 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000131}
132
Matt Kopec7de48462013-03-06 17:20:48 +0000133// Wrapper for ptrace when logging is not required.
134// Sets errno to 0 prior to calling ptrace.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
136 long result = 0;
137 errno = 0;
138 result = ptrace(req, pid, (caddr_t)addr, data);
139 return result;
Matt Kopec7de48462013-03-06 17:20:48 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142#define PTRACE(req, pid, addr, data) \
143 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000144#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000146#endif
147
148//------------------------------------------------------------------------------
149// Static implementations of ProcessMonitor::ReadMemory and
150// ProcessMonitor::WriteMemory. This enables mutual recursion between these
151// functions without needed to go thru the thread funnel.
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +0000154 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 pi_desc.piod_op = PIOD_READ_D;
158 pi_desc.piod_offs = (void *)vm_addr;
159 pi_desc.piod_addr = buf;
160 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000161
Ed Maste592d29a2018-04-21 11:23:56 +0000162 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000164 return 0;
165 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
Zachary Turner97206d52017-05-12 04:51:55 +0000170 const void *buf, size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 pi_desc.piod_op = PIOD_WRITE_D;
174 pi_desc.piod_offs = (void *)vm_addr;
175 pi_desc.piod_addr = (void *)buf;
176 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000177
Ed Maste592d29a2018-04-21 11:23:56 +0000178 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000180 return 0;
181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 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,
Pavel Labath75c6de02018-01-10 13:53:40 +0000698 char const **argv, Environment env,
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)
Pavel Labath75c6de02018-01-10 13:53:40 +0000703 : OperationArgs(monitor), m_module(module), m_argv(argv),
704 m_env(std::move(env)), m_stdin_file_spec(stdin_file_spec),
705 m_stdout_file_spec(stdout_file_spec),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000707
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708ProcessMonitor::LaunchArgs::~LaunchArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
711 : OperationArgs(monitor), m_pid(pid) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000712
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713ProcessMonitor::AttachArgs::~AttachArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000714
715//------------------------------------------------------------------------------
716/// The basic design of the ProcessMonitor is built around two threads.
717///
718/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
719/// for changes in the debugee state. When a change is detected a
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
721/// thread
Johnny Chen9ed5b492012-01-05 21:48:15 +0000722/// "drives" state changes in the debugger.
723///
724/// The second thread (@see OperationThread) is responsible for two things 1)
725/// launching or attaching to the inferior process, and then 2) servicing
726/// operations such as register reads/writes, stepping, etc. See the comments
727/// on the Operation class for more info as to why this is needed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728ProcessMonitor::ProcessMonitor(
729 ProcessFreeBSD *process, Module *module, const char *argv[],
Pavel Labath75c6de02018-01-10 13:53:40 +0000730 Environment env, const FileSpec &stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
732 const FileSpec &working_dir,
733 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Zachary Turner97206d52017-05-12 04:51:55 +0000734 lldb_private::Status &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000735 : m_process(static_cast<ProcessFreeBSD *>(process)),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
737 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 std::unique_ptr<LaunchArgs> args(
Pavel Labath75c6de02018-01-10 13:53:40 +0000740 new LaunchArgs(this, module, argv, std::move(env), stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 stdout_file_spec, stderr_file_spec, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 sem_init(&m_operation_pending, 0, 0);
744 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000745
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 StartLaunchOpThread(args.get(), error);
747 if (!error.Success())
748 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000749
Pavel Labathc1a6b122017-07-03 09:25:55 +0000750 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
751 error.SetErrorToErrno();
752 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000754
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 // Check that the launch was a success.
756 if (!args->m_error.Success()) {
757 StopOpThread();
758 error = args->m_error;
759 return;
760 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000761
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 // Finally, start monitoring the child process for change in state.
763 m_monitor_thread = Host::StartMonitoringChildProcess(
764 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
765 GetPID(), true);
766 if (!m_monitor_thread.IsJoinable()) {
767 error.SetErrorToGenericError();
768 error.SetErrorString("Process launch failed.");
769 return;
770 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000771}
772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +0000774 lldb_private::Status &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
776 m_terminal_fd(-1), m_operation(0) {
777 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 sem_init(&m_operation_pending, 0, 0);
780 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000781
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 StartAttachOpThread(args.get(), error);
785 if (!error.Success())
786 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000787
Pavel Labathc1a6b122017-07-03 09:25:55 +0000788 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
789 error.SetErrorToErrno();
790 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 // Check that the attach was a success.
794 if (!args->m_error.Success()) {
795 StopOpThread();
796 error = args->m_error;
797 return;
798 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000799
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 // Finally, start monitoring the child process for change in state.
801 m_monitor_thread = Host::StartMonitoringChildProcess(
802 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
803 GetPID(), true);
804 if (!m_monitor_thread.IsJoinable()) {
805 error.SetErrorToGenericError();
806 error.SetErrorString("Process attach failed.");
807 return;
808 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000809}
810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000812
813//------------------------------------------------------------------------------
814// Thread setup and tear down.
Zachary Turner97206d52017-05-12 04:51:55 +0000815void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818 if (m_operation_thread.IsJoinable())
819 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000820
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 m_operation_thread =
822 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000823}
824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825void *ProcessMonitor::LaunchOpThread(void *arg) {
826 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 if (!Launch(args)) {
829 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000830 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 }
832
833 ServeOperation(args);
834 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000835}
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837bool ProcessMonitor::Launch(LaunchArgs *args) {
838 ProcessMonitor *monitor = args->m_monitor;
839 ProcessFreeBSD &process = monitor->GetProcess();
840 const char **argv = args->m_argv;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 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.
Pavel Labath75c6de02018-01-10 13:53:40 +0000852 Environment::Envp envp =
853 (args->m_env.empty() ? Host::GetEnvironment() : args->m_env).getEnvp();
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.
Pavel Labath75c6de02018-01-10 13:53:40 +0000911 execve(argv[0], const_cast<char *const *>(argv), envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 exit(eExecFailed);
913 }
914
915 // Wait for the child process to to trap on its call to execve.
916 ::pid_t wpid;
917 int status;
918 if ((wpid = waitpid(pid, &status, 0)) < 0) {
919 args->m_error.SetErrorToErrno();
920 goto FINISH;
921 } else if (WIFEXITED(status)) {
922 // open, dup or execve likely failed for some reason.
923 args->m_error.SetErrorToGenericError();
924 switch (WEXITSTATUS(status)) {
925 case ePtraceFailed:
926 args->m_error.SetErrorString("Child ptrace failed.");
927 break;
928 case eDupStdinFailed:
929 args->m_error.SetErrorString("Child open stdin failed.");
930 break;
931 case eDupStdoutFailed:
932 args->m_error.SetErrorString("Child open stdout failed.");
933 break;
934 case eDupStderrFailed:
935 args->m_error.SetErrorString("Child open stderr failed.");
936 break;
937 case eChdirFailed:
938 args->m_error.SetErrorString("Child failed to set working directory.");
939 break;
940 case eExecFailed:
941 args->m_error.SetErrorString("Child exec failed.");
942 break;
943 case eSetGidFailed:
944 args->m_error.SetErrorString("Child setgid failed.");
945 break;
946 default:
947 args->m_error.SetErrorString("Child returned unknown exit status.");
948 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 goto FINISH;
951 }
952 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
953 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000954
955#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 // Have the child raise an event on exit. This is used to keep the child in
957 // limbo until it is destroyed.
958 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
959 args->m_error.SetErrorToErrno();
960 goto FINISH;
961 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000962#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 // Release the master terminal descriptor and pass it off to the
964 // ProcessMonitor instance. Similarly stash the inferior pid.
965 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
966 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000967
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 // Set the terminal fd to be in non blocking mode (it simplifies the
969 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
970 // descriptor to read from).
971 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
972 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000973
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000975
976FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000978}
979
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
Zachary Turner97206d52017-05-12 04:51:55 +0000981 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000983
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 if (m_operation_thread.IsJoinable())
985 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 m_operation_thread =
988 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000989}
990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991void *ProcessMonitor::AttachOpThread(void *arg) {
992 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000993
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 ServeOperation(args);
997 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000998}
999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000void ProcessMonitor::Attach(AttachArgs *args) {
1001 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001002
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 ProcessMonitor *monitor = args->m_monitor;
1004 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 if (pid <= 1) {
1007 args->m_error.SetErrorToGenericError();
1008 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1009 return;
1010 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001011
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 // Attach to the requested process.
1013 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
1014 args->m_error.SetErrorToErrno();
1015 return;
1016 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 int status;
1019 if ((status = waitpid(pid, NULL, 0)) < 0) {
1020 args->m_error.SetErrorToErrno();
1021 return;
1022 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001023
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001025}
1026
Ed Maste7fd845c2013-12-09 15:51:17 +00001027size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1029 lwpid_t *tids;
1030 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001033
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1035 if (tdcnt <= 0)
1036 return 0;
1037 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1038 if (tids == NULL)
1039 return 0;
1040 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001041 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 return 0;
1043 }
1044 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1045 free(tids);
1046 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001047}
1048
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1050 bool exited, int signal, int status) {
1051 ProcessMessage message;
1052 ProcessFreeBSD *process = monitor->m_process;
1053 assert(process);
1054 bool stop_monitoring;
1055 struct ptrace_lwpinfo plwp;
1056 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 if (exited) {
1061 if (log)
1062 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1063 __FUNCTION__, pid);
1064 message = ProcessMessage::Exit(pid, status);
1065 process->SendMessage(message);
1066 return pid == process->GetID();
1067 }
Ed Mastea02f5532013-07-02 16:45:16 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1070 stop_monitoring = true; // pid is gone. Bail.
1071 else {
1072 switch (plwp.pl_siginfo.si_signo) {
1073 case SIGTRAP:
1074 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1075 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001076
Johnny Chen9ed5b492012-01-05 21:48:15 +00001077 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1079 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001080 }
1081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 process->SendMessage(message);
1083 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1084 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001085
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001087}
1088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1090 const siginfo_t *info,
1091 lldb::tid_t tid) {
1092 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001095
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 assert(monitor);
1097 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001098
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 switch (info->si_code) {
1100 default:
1101 assert(false && "Unexpected SIGTRAP code!");
1102 break;
1103
1104 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
1105 // The inferior process is about to exit. Maintain the process in a
1106 // state of "limbo" until we are explicitly commanded to detach,
1107 // destroy, resume, etc.
1108 unsigned long data = 0;
1109 if (!monitor->GetEventMessage(tid, &data))
1110 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001111 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1113 "= %" PRIu64,
1114 __FUNCTION__, data, tid);
1115 message = ProcessMessage::Limbo(tid, (data >> 8));
1116 break;
1117 }
Ed Mastea02f5532013-07-02 16:45:16 +00001118
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 case 0:
1120 case TRAP_TRACE:
Ed Masted6fa2c32017-05-26 03:15:46 +00001121#ifdef TRAP_CAP
1122 // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1123 case TRAP_CAP:
1124#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 if (log)
1126 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1127 " : si_code = %d",
1128 __FUNCTION__, tid, info->si_code);
1129 message = ProcessMessage::Trace(tid);
1130 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 case SI_KERNEL:
1133 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001134 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1135 if (log)
1136 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1137 "event, tid = %" PRIu64,
1138 __FUNCTION__, tid);
1139 message = ProcessMessage::Trace(tid);
1140 } else {
1141 if (log)
1142 log->Printf(
1143 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1144 __FUNCTION__, tid);
1145 message = ProcessMessage::Break(tid);
1146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 break;
1148 }
1149
1150 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001151}
1152
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1154 const siginfo_t *info,
1155 lldb::tid_t tid) {
1156 ProcessMessage message;
1157 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001158
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001160
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1162 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1163 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1164 //
1165 // IOW, user generated signals never generate what we consider to be a
1166 // "crash".
1167 //
1168 // Similarly, ACK signals generated by this monitor.
1169 if (info->si_code == SI_USER) {
1170 if (log)
1171 log->Printf(
1172 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1173 __FUNCTION__,
1174 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1175 "SI_USER", info->si_pid);
1176 if (info->si_pid == getpid())
1177 return ProcessMessage::SignalDelivered(tid, signo);
1178 else
1179 return ProcessMessage::Signal(tid, signo);
1180 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001181
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182 if (log)
1183 log->Printf(
1184 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1185 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001186
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187 switch (signo) {
1188 case SIGSEGV:
1189 case SIGILL:
1190 case SIGFPE:
1191 case SIGBUS:
1192 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1193 const auto reason = GetCrashReason(*info);
Ed Maste5e82ca32017-08-10 13:47:17 +00001194 if (reason != CrashReason::eInvalidCrashReason) {
1195 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1196 } // else; Use atleast si_signo info for other si_code
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 }
1198
1199 // Everything else is "normal" and does not require any special action on
1200 // our part.
1201 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001202}
1203
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204void ProcessMonitor::ServeOperation(OperationArgs *args) {
1205 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001206
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207 // We are finised with the arguments and are ready to go. Sync with the
1208 // parent thread and start serving operations on the inferior.
1209 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001210
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 for (;;) {
1212 // wait for next pending operation
1213 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 monitor->m_operation->Execute(monitor);
1216
1217 // notify calling thread that operation is complete
1218 sem_post(&monitor->m_operation_done);
1219 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001220}
1221
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222void ProcessMonitor::DoOperation(Operation *op) {
1223 std::lock_guard<std::mutex> guard(m_operation_mutex);
1224
1225 m_operation = op;
1226
1227 // notify operation thread that an operation is ready to be processed
1228 sem_post(&m_operation_pending);
1229
1230 // wait for operation to complete
1231 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001232}
1233
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +00001235 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 size_t result;
1237 ReadOperation op(vm_addr, buf, size, error, result);
1238 DoOperation(&op);
1239 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001240}
1241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +00001243 size_t size, lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 size_t result;
1245 WriteOperation op(vm_addr, buf, size, error, result);
1246 DoOperation(&op);
1247 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001248}
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001251 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 RegisterValue &value) {
1253 bool result;
1254 ReadRegOperation op(tid, offset, size, value, result);
1255 DoOperation(&op);
1256 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001257}
1258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001260 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 const RegisterValue &value) {
1262 bool result;
1263 WriteRegOperation op(tid, offset, value, result);
1264 DoOperation(&op);
1265 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001266}
1267
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268bool ProcessMonitor::ReadDebugRegisterValue(
1269 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1270 lldb_private::RegisterValue &value) {
1271 bool result;
1272 ReadDebugRegOperation op(tid, offset, size, value, result);
1273 DoOperation(&op);
1274 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001275}
1276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277bool ProcessMonitor::WriteDebugRegisterValue(
1278 lldb::tid_t tid, unsigned offset, const char *reg_name,
1279 const lldb_private::RegisterValue &value) {
1280 bool result;
1281 WriteDebugRegOperation op(tid, offset, value, result);
1282 DoOperation(&op);
1283 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001284}
1285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1287 bool result;
1288 ReadGPROperation op(tid, buf, result);
1289 DoOperation(&op);
1290 return result;
1291}
1292
1293bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1294 bool result;
1295 ReadFPROperation op(tid, buf, result);
1296 DoOperation(&op);
1297 return result;
1298}
1299
1300bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1301 size_t buf_size, unsigned int regset) {
1302 return false;
1303}
1304
1305bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1306 bool result;
1307 WriteGPROperation op(tid, buf, result);
1308 DoOperation(&op);
1309 return result;
1310}
1311
1312bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1313 bool result;
1314 WriteFPROperation op(tid, buf, result);
1315 DoOperation(&op);
1316 return result;
1317}
1318
1319bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1320 size_t buf_size, unsigned int regset) {
1321 return false;
1322}
1323
1324bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1325 return false;
1326}
1327
1328bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1329 bool result;
1330 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1331
1332 if (log) {
1333 const char *signame =
1334 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1335 if (signame == nullptr)
1336 signame = "<none>";
1337 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1338 __FUNCTION__, GetPID(), signame);
1339 }
1340 ResumeOperation op(signo, result);
1341 DoOperation(&op);
1342 if (log)
1343 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1344 result ? "true" : "false");
1345 return result;
1346}
1347
1348bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1349 bool result;
1350 SingleStepOperation op(signo, result);
1351 DoOperation(&op);
1352 return result;
1353}
1354
1355bool ProcessMonitor::Kill() {
1356 bool result;
1357 KillOperation op(result);
1358 DoOperation(&op);
1359 return result;
1360}
1361
1362bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1363 int &ptrace_err) {
1364 bool result;
1365 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1366 DoOperation(&op);
1367 return result;
1368}
1369
1370bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1371 bool result;
1372 ThreadSuspendOperation op(tid, suspend, result);
1373 DoOperation(&op);
1374 return result;
1375}
1376
1377bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1378 bool result;
1379 EventMessageOperation op(tid, message, result);
1380 DoOperation(&op);
1381 return result;
1382}
1383
Zachary Turner97206d52017-05-12 04:51:55 +00001384lldb_private::Status ProcessMonitor::Detach(lldb::tid_t tid) {
1385 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 if (tid != LLDB_INVALID_THREAD_ID) {
1387 DetachOperation op(error);
1388 DoOperation(&op);
1389 }
1390 return error;
1391}
1392
1393bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1394 int flags) {
1395 int target_fd = open(file_spec.GetCString(), flags, 0666);
1396
1397 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001398 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001399
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001401 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402
1403 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001404}
1405
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406void ProcessMonitor::StopMonitoringChildProcess() {
1407 if (m_monitor_thread.IsJoinable()) {
1408 m_monitor_thread.Cancel();
1409 m_monitor_thread.Join(nullptr);
1410 m_monitor_thread.Reset();
1411 }
Ed Maste68f51792013-10-18 19:16:44 +00001412}
1413
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414void ProcessMonitor::StopMonitor() {
1415 StopMonitoringChildProcess();
1416 StopOpThread();
1417 sem_destroy(&m_operation_pending);
1418 sem_destroy(&m_operation_done);
1419 if (m_terminal_fd >= 0) {
1420 close(m_terminal_fd);
1421 m_terminal_fd = -1;
1422 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001423}
1424
Andrew Kaylord4d54992013-09-17 00:30:24 +00001425// FIXME: On Linux, when a new thread is created, we receive to notifications,
1426// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1427// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1428// the new child thread indicating that it has is stopped because we attached.
1429// We have no guarantee of the order in which these arrive, but we need both
1430// before we are ready to proceed. We currently keep a list of threads which
1431// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1432// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1433// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1434//
1435// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1436// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1437// logically needed.
1438//
1439// We really should figure out what actually happens on FreeBSD and move the
1440// Linux-specific logic out of ProcessPOSIX as needed.
1441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444void ProcessMonitor::StopOpThread() {
1445 if (!m_operation_thread.IsJoinable())
1446 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001447
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 m_operation_thread.Cancel();
1449 m_operation_thread.Join(nullptr);
1450 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001451}