blob: 617ae3030f1052c9e9b5813450a845ae8a94076b [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
Johnny Chen9ed5b492012-01-05 21:48:15 +000010#include <errno.h>
11#include <poll.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000012#include <signal.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include <stdint.h>
14#include <string.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000015#include <sys/ptrace.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <sys/wait.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include <unistd.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000020
Johnny Chen9ed5b492012-01-05 21:48:15 +000021#include "lldb/Host/Host.h"
Zachary Turner24ae6292017-02-16 19:38:21 +000022#include "lldb/Host/PseudoTerminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000023#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000024#include "lldb/Target/RegisterContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/Target/Thread.h"
Ed Maste8702e922015-03-03 22:44:18 +000026#include "lldb/Target/UnixSignals.h"
Pavel Labathd821c992018-08-07 11:07:21 +000027#include "lldb/Utility/RegisterValue.h"
28#include "lldb/Utility/Scalar.h"
Zachary Turner97206d52017-05-12 04:51:55 +000029#include "lldb/Utility/Status.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000030#include "llvm/Support/Errno.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000031
Ed Mastefe5a6422015-07-28 15:45:57 +000032#include "FreeBSDThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#include "Plugins/Process/POSIX/CrashReason.h"
Pavel Labathf0a6d8a2017-04-11 12:26:25 +000034#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000035#include "ProcessFreeBSD.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000036#include "ProcessMonitor.h"
37
Johnny Chen9ed5b492012-01-05 21:48:15 +000038using namespace lldb;
39using namespace lldb_private;
40
Adrian Prantl05097242018-04-30 16:49:04 +000041// We disable the tracing of ptrace calls for integration builds to avoid the
42// additional indirection and checks.
Johnny Chen9ed5b492012-01-05 21:48:15 +000043#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
44// Wrapper for ptrace to catch errors and log calls.
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046const char *Get_PT_IO_OP(int op) {
47 switch (op) {
48 case PIOD_READ_D:
49 return "READ_D";
50 case PIOD_WRITE_D:
51 return "WRITE_D";
52 case PIOD_READ_I:
53 return "READ_I";
54 case PIOD_WRITE_I:
55 return "WRITE_I";
56 default:
57 return "Unknown op";
58 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000059}
60
Adrian Prantl05097242018-04-30 16:49:04 +000061// Wrapper for ptrace to catch errors and log calls. Note that ptrace sets
62// errno on error because -1 is reserved as a valid result.
Kate Stoneb9c1b512016-09-06 20:57:50 +000063extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
64 const char *reqName, const char *file, int line) {
65 long int result;
Johnny Chen9ed5b492012-01-05 21:48:15 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 if (log) {
70 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
71 reqName, pid, addr, data, file, line);
72 if (req == PT_IO) {
73 struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
74
75 log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
76 (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 }
Johnny Chen9ed5b492012-01-05 21:48:15 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 errno = 0;
83 result = ptrace(req, pid, (caddr_t)addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 // PtraceDisplayBytes(req, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 if (log && errno != 0) {
88 const char *str;
89 switch (errno) {
90 case ESRCH:
91 str = "ESRCH";
92 break;
93 case EINVAL:
94 str = "EINVAL";
95 break;
96 case EBUSY:
97 str = "EBUSY";
98 break;
99 case EPERM:
100 str = "EPERM";
101 break;
102 default:
103 str = "<unknown>";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
106 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000109#ifdef __amd64__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (req == PT_GETREGS) {
111 struct reg *r = (struct reg *)addr;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
114 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Ed Mastea4be2c52014-02-19 18:34:06 +0000115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
117 struct dbreg *r = (struct dbreg *)addr;
118 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
119
120 for (int i = 0; i <= 7; i++)
121 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
122 }
123#endif
124 }
125
126 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000127}
128
Adrian Prantl05097242018-04-30 16:49:04 +0000129// Wrapper for ptrace when logging is not required. Sets errno to 0 prior to
130// calling ptrace.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
132 long result = 0;
133 errno = 0;
134 result = ptrace(req, pid, (caddr_t)addr, data);
135 return result;
Matt Kopec7de48462013-03-06 17:20:48 +0000136}
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138#define PTRACE(req, pid, addr, data) \
139 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000140#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000142#endif
143
144//------------------------------------------------------------------------------
145// Static implementations of ProcessMonitor::ReadMemory and
146// ProcessMonitor::WriteMemory. This enables mutual recursion between these
147// functions without needed to go thru the thread funnel.
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +0000150 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 pi_desc.piod_op = PIOD_READ_D;
154 pi_desc.piod_offs = (void *)vm_addr;
155 pi_desc.piod_addr = buf;
156 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000157
Ed Maste592d29a2018-04-21 11:23:56 +0000158 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000160 return 0;
161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
Zachary Turner97206d52017-05-12 04:51:55 +0000166 const void *buf, size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 struct ptrace_io_desc pi_desc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 pi_desc.piod_op = PIOD_WRITE_D;
170 pi_desc.piod_offs = (void *)vm_addr;
171 pi_desc.piod_addr = (void *)buf;
172 pi_desc.piod_len = size;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000173
Ed Maste592d29a2018-04-21 11:23:56 +0000174 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 error.SetErrorToErrno();
Ed Maste592d29a2018-04-21 11:23:56 +0000176 return 0;
177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 return pi_desc.piod_len;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000179}
180
181// Simple helper function to ensure flags are enabled on the given file
182// descriptor.
Zachary Turner97206d52017-05-12 04:51:55 +0000183static bool EnsureFDFlags(int fd, int flags, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 int status;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 if ((status = fcntl(fd, F_GETFL)) == -1) {
187 error.SetErrorToErrno();
188 return false;
189 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 if (fcntl(fd, F_SETFL, status | flags) == -1) {
192 error.SetErrorToErrno();
193 return false;
194 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000197}
198
199//------------------------------------------------------------------------------
200/// @class Operation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000201/// Represents a ProcessMonitor operation.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000202///
Adrian Prantld8f460e2018-05-02 16:55:16 +0000203/// Under FreeBSD, it is not possible to ptrace() from any other thread but
204/// the one that spawned or attached to the process from the start.
205/// Therefore, when a ProcessMonitor is asked to deliver or change the state
206/// of an inferior process the operation must be "funneled" to a specific
207/// thread to perform the task. The Operation class provides an abstract base
208/// for all services the ProcessMonitor must perform via the single virtual
209/// function Execute, thus encapsulating the code that needs to run in the
210/// privileged context.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211class Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000212public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 virtual ~Operation() {}
214 virtual void Execute(ProcessMonitor *monitor) = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000215};
216
217//------------------------------------------------------------------------------
218/// @class ReadOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000219/// Implements ProcessMonitor::ReadMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220class ReadOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221public:
Zachary Turner97206d52017-05-12 04:51:55 +0000222 ReadOperation(lldb::addr_t addr, void *buff, size_t size, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 size_t &result)
224 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
225 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000228
229private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 lldb::addr_t m_addr;
231 void *m_buff;
232 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000233 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000235};
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237void ReadOperation::Execute(ProcessMonitor *monitor) {
238 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000241}
242
243//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000244/// @class WriteOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000245/// Implements ProcessMonitor::WriteMemory.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246class WriteOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000247public:
Zachary Turner97206d52017-05-12 04:51:55 +0000248 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
249 Status &error, size_t &result)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
251 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000254
255private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 lldb::addr_t m_addr;
257 const void *m_buff;
258 size_t m_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000259 Status &m_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 size_t &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000261};
262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263void WriteOperation::Execute(ProcessMonitor *monitor) {
264 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000267}
268
269//------------------------------------------------------------------------------
270/// @class ReadRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000271/// Implements ProcessMonitor::ReadRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272class ReadRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000273public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
275 RegisterValue &value, bool &result)
276 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
277 m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000280
281private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 lldb::tid_t m_tid;
283 unsigned m_offset;
284 unsigned m_size;
285 RegisterValue &m_value;
286 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000287};
288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289void ReadRegOperation::Execute(ProcessMonitor *monitor) {
290 struct reg regs;
291 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
294 m_result = false;
295 } else {
296 // 'struct reg' contains only 32- or 64-bit register values. Punt on
297 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
298 // processes on powerpc64 (probably the same for i386 on amd64)
299 if (m_size == sizeof(uint32_t))
300 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
301 else if (m_size == sizeof(uint64_t))
302 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
303 else
304 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
305 m_result = true;
306 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000307}
308
309//------------------------------------------------------------------------------
310/// @class WriteRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000311/// Implements ProcessMonitor::WriteRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312class WriteRegOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000313public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 WriteRegOperation(lldb::tid_t tid, unsigned offset,
315 const RegisterValue &value, bool &result)
316 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000319
320private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 lldb::tid_t m_tid;
322 unsigned m_offset;
323 const RegisterValue &m_value;
324 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000325};
326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327void WriteRegOperation::Execute(ProcessMonitor *monitor) {
328 struct reg regs;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
331 m_result = false;
332 return;
333 }
334 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
335 (uintptr_t)m_value.GetAsUInt64();
336 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
337 m_result = false;
338 else
339 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000340}
341
342//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000343/// @class ReadDebugRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000344/// Implements ProcessMonitor::ReadDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345class ReadDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000346public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
348 RegisterValue &value, bool &result)
349 : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
350 m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000353
354private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 lldb::tid_t m_tid;
356 unsigned m_offset;
357 unsigned m_size;
358 RegisterValue &m_value;
359 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000360};
361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362void ReadDebugRegOperation::Execute(ProcessMonitor *monitor) {
363 struct dbreg regs;
364 int rc;
Ed Mastea4be2c52014-02-19 18:34:06 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
367 m_result = false;
368 } else {
369 if (m_size == sizeof(uintptr_t))
370 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
371 else
372 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
373 m_result = true;
374 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000375}
376
377//------------------------------------------------------------------------------
378/// @class WriteDebugRegOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000379/// Implements ProcessMonitor::WriteDebugRegisterValue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380class WriteDebugRegOperation : public Operation {
Ed Mastea4be2c52014-02-19 18:34:06 +0000381public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
383 const RegisterValue &value, bool &result)
384 : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
Ed Mastea4be2c52014-02-19 18:34:06 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 void Execute(ProcessMonitor *monitor);
Ed Mastea4be2c52014-02-19 18:34:06 +0000387
388private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 lldb::tid_t m_tid;
390 unsigned m_offset;
391 const RegisterValue &m_value;
392 bool &m_result;
Ed Mastea4be2c52014-02-19 18:34:06 +0000393};
394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395void WriteDebugRegOperation::Execute(ProcessMonitor *monitor) {
396 struct dbreg regs;
Ed Mastea4be2c52014-02-19 18:34:06 +0000397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
399 m_result = false;
400 return;
401 }
402 *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
403 (uintptr_t)m_value.GetAsUInt64();
404 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
405 m_result = false;
406 else
407 m_result = true;
Ed Mastea4be2c52014-02-19 18:34:06 +0000408}
409
410//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000411/// @class ReadGPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000412/// Implements ProcessMonitor::ReadGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413class ReadGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000414public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
416 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000419
420private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 lldb::tid_t m_tid;
422 void *m_buf;
423 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000424};
425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426void ReadGPROperation::Execute(ProcessMonitor *monitor) {
427 int rc;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 errno = 0;
430 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
431 if (errno != 0)
432 m_result = false;
433 else
434 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000435}
436
437//------------------------------------------------------------------------------
438/// @class ReadFPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000439/// Implements ProcessMonitor::ReadFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440class ReadFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000441public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
443 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000446
447private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 lldb::tid_t m_tid;
449 void *m_buf;
450 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000451};
452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453void ReadFPROperation::Execute(ProcessMonitor *monitor) {
454 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
455 m_result = false;
456 else
457 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000458}
459
460//------------------------------------------------------------------------------
461/// @class WriteGPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000462/// Implements ProcessMonitor::WriteGPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463class WriteGPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000464public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
466 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000469
470private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 lldb::tid_t m_tid;
472 void *m_buf;
473 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000474};
475
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476void WriteGPROperation::Execute(ProcessMonitor *monitor) {
477 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
478 m_result = false;
479 else
480 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000481}
482
483//------------------------------------------------------------------------------
484/// @class WriteFPROperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000485/// Implements ProcessMonitor::WriteFPR.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486class WriteFPROperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000487public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
489 : m_tid(tid), m_buf(buf), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000492
493private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 lldb::tid_t m_tid;
495 void *m_buf;
496 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000497};
498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499void WriteFPROperation::Execute(ProcessMonitor *monitor) {
500 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
501 m_result = false;
502 else
503 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000504}
505
506//------------------------------------------------------------------------------
507/// @class ResumeOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000508/// Implements ProcessMonitor::Resume.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509class ResumeOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000510public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 ResumeOperation(uint32_t signo, bool &result)
512 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000515
516private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 uint32_t m_signo;
518 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000519};
520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521void ResumeOperation::Execute(ProcessMonitor *monitor) {
522 lldb::pid_t pid = monitor->GetPID();
523 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
526 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) {
529 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Pavel Labath10c41f32017-06-06 14:06:17 +0000530 LLDB_LOG(log, "ResumeOperation ({0}) failed: {1}", pid,
531 llvm::sys::StrError(errno));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 m_result = false;
533 } else
534 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000535}
536
537//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000538/// @class SingleStepOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000539/// Implements ProcessMonitor::SingleStep.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540class SingleStepOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000541public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542 SingleStepOperation(uint32_t signo, bool &result)
543 : m_signo(signo), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000546
547private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 uint32_t m_signo;
549 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000550};
551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552void SingleStepOperation::Execute(ProcessMonitor *monitor) {
553 lldb::pid_t pid = monitor->GetPID();
554 int data = 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
557 data = m_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 if (PTRACE(PT_STEP, pid, NULL, data))
560 m_result = false;
561 else
562 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000563}
564
565//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000566/// @class LwpInfoOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000567/// Implements ProcessMonitor::GetLwpInfo.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568class LwpInfoOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000569public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
571 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000574
575private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 lldb::tid_t m_tid;
577 void *m_info;
578 bool &m_result;
579 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000580};
581
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582void LwpInfoOperation::Execute(ProcessMonitor *monitor) {
583 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
586 m_result = false;
587 m_err = errno;
588 } else {
589 memcpy(m_info, &plwp, sizeof(plwp));
590 m_result = true;
591 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000592}
593
594//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000595/// @class ThreadSuspendOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000596/// Implements ProcessMonitor::ThreadSuspend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597class ThreadSuspendOperation : public Operation {
Ed Maste7fd845c2013-12-09 15:51:17 +0000598public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
600 : m_tid(tid), m_suspend(suspend), m_result(result) {}
Ed Maste7fd845c2013-12-09 15:51:17 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 void Execute(ProcessMonitor *monitor);
Ed Maste7fd845c2013-12-09 15:51:17 +0000603
604private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 lldb::tid_t m_tid;
606 bool m_suspend;
607 bool &m_result;
608};
Ed Maste7fd845c2013-12-09 15:51:17 +0000609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610void ThreadSuspendOperation::Execute(ProcessMonitor *monitor) {
611 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
Ed Maste7fd845c2013-12-09 15:51:17 +0000612}
613
Ed Maste7fd845c2013-12-09 15:51:17 +0000614//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000615/// @class EventMessageOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000616/// Implements ProcessMonitor::GetEventMessage.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617class EventMessageOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000618public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
620 : m_tid(tid), m_message(message), m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000623
624private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 lldb::tid_t m_tid;
626 unsigned long *m_message;
627 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000628};
629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630void EventMessageOperation::Execute(ProcessMonitor *monitor) {
631 struct ptrace_lwpinfo plwp;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
634 m_result = false;
635 else {
636 if (plwp.pl_flags & PL_FLAG_FORKED) {
637 *m_message = plwp.pl_child_pid;
638 m_result = true;
639 } else
640 m_result = false;
641 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000642}
643
644//------------------------------------------------------------------------------
645/// @class KillOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000646/// Implements ProcessMonitor::Kill.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647class KillOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000648public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 KillOperation(bool &result) : m_result(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000652
653private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 bool &m_result;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000655};
656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657void KillOperation::Execute(ProcessMonitor *monitor) {
658 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000659
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 if (PTRACE(PT_KILL, pid, NULL, 0))
661 m_result = false;
662 else
663 m_result = true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000664}
665
666//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000667/// @class DetachOperation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000668/// Implements ProcessMonitor::Detach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669class DetachOperation : public Operation {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000670public:
Zachary Turner97206d52017-05-12 04:51:55 +0000671 DetachOperation(Status &result) : m_error(result) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 void Execute(ProcessMonitor *monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000674
675private:
Zachary Turner97206d52017-05-12 04:51:55 +0000676 Status &m_error;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000677};
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679void DetachOperation::Execute(ProcessMonitor *monitor) {
680 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000681
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
683 m_error.SetErrorToErrno();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000684}
685
686ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 : m_monitor(monitor) {
688 sem_init(&m_semaphore, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000689}
690
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000692
693ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
694 lldb_private::Module *module,
Pavel Labath75c6de02018-01-10 13:53:40 +0000695 char const **argv, Environment env,
Ed Maste41fba2b2015-06-01 15:24:37 +0000696 const FileSpec &stdin_file_spec,
697 const FileSpec &stdout_file_spec,
698 const FileSpec &stderr_file_spec,
699 const FileSpec &working_dir)
Pavel Labath75c6de02018-01-10 13:53:40 +0000700 : OperationArgs(monitor), m_module(module), m_argv(argv),
701 m_env(std::move(env)), m_stdin_file_spec(stdin_file_spec),
702 m_stdout_file_spec(stdout_file_spec),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705ProcessMonitor::LaunchArgs::~LaunchArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000706
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
708 : OperationArgs(monitor), m_pid(pid) {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710ProcessMonitor::AttachArgs::~AttachArgs() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000711
712//------------------------------------------------------------------------------
713/// The basic design of the ProcessMonitor is built around two threads.
714///
Adrian Prantld8f460e2018-05-02 16:55:16 +0000715/// One thread (@see SignalThread) simply blocks on a call to waitpid()
716/// looking for changes in the debugee state. When a change is detected a
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
Adrian Prantld8f460e2018-05-02 16:55:16 +0000718/// thread "drives" state changes in the debugger.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000719///
720/// The second thread (@see OperationThread) is responsible for two things 1)
721/// launching or attaching to the inferior process, and then 2) servicing
722/// operations such as register reads/writes, stepping, etc. See the comments
723/// on the Operation class for more info as to why this is needed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724ProcessMonitor::ProcessMonitor(
725 ProcessFreeBSD *process, Module *module, const char *argv[],
Pavel Labath75c6de02018-01-10 13:53:40 +0000726 Environment env, const FileSpec &stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
728 const FileSpec &working_dir,
729 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Zachary Turner97206d52017-05-12 04:51:55 +0000730 lldb_private::Status &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000731 : m_process(static_cast<ProcessFreeBSD *>(process)),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
733 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 std::unique_ptr<LaunchArgs> args(
Pavel Labath75c6de02018-01-10 13:53:40 +0000736 new LaunchArgs(this, module, argv, std::move(env), stdin_file_spec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 stdout_file_spec, stderr_file_spec, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 sem_init(&m_operation_pending, 0, 0);
740 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000741
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 StartLaunchOpThread(args.get(), error);
743 if (!error.Success())
744 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000745
Pavel Labathc1a6b122017-07-03 09:25:55 +0000746 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
747 error.SetErrorToErrno();
748 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000750
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 // Check that the launch was a success.
752 if (!args->m_error.Success()) {
753 StopOpThread();
754 error = args->m_error;
755 return;
756 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000757
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 // Finally, start monitoring the child process for change in state.
759 m_monitor_thread = Host::StartMonitoringChildProcess(
760 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
761 GetPID(), true);
762 if (!m_monitor_thread.IsJoinable()) {
763 error.SetErrorToGenericError();
764 error.SetErrorString("Process launch failed.");
765 return;
766 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000767}
768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
Zachary Turner97206d52017-05-12 04:51:55 +0000770 lldb_private::Status &error)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
772 m_terminal_fd(-1), m_operation(0) {
773 using namespace std::placeholders;
Pavel Labath998bdc52016-05-11 16:59:04 +0000774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 sem_init(&m_operation_pending, 0, 0);
776 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000779
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 StartAttachOpThread(args.get(), error);
781 if (!error.Success())
782 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000783
Pavel Labathc1a6b122017-07-03 09:25:55 +0000784 if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
785 error.SetErrorToErrno();
786 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 // Check that the attach was a success.
790 if (!args->m_error.Success()) {
791 StopOpThread();
792 error = args->m_error;
793 return;
794 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000795
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 // Finally, start monitoring the child process for change in state.
797 m_monitor_thread = Host::StartMonitoringChildProcess(
798 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
799 GetPID(), true);
800 if (!m_monitor_thread.IsJoinable()) {
801 error.SetErrorToGenericError();
802 error.SetErrorString("Process attach failed.");
803 return;
804 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000805}
806
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807ProcessMonitor::~ProcessMonitor() { StopMonitor(); }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000808
809//------------------------------------------------------------------------------
810// Thread setup and tear down.
Zachary Turner97206d52017-05-12 04:51:55 +0000811void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 if (m_operation_thread.IsJoinable())
815 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 m_operation_thread =
818 ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000819}
820
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821void *ProcessMonitor::LaunchOpThread(void *arg) {
822 LaunchArgs *args = static_cast<LaunchArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 if (!Launch(args)) {
825 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000826 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 }
828
829 ServeOperation(args);
830 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000831}
832
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833bool ProcessMonitor::Launch(LaunchArgs *args) {
834 ProcessMonitor *monitor = args->m_monitor;
835 ProcessFreeBSD &process = monitor->GetProcess();
836 const char **argv = args->m_argv;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
838 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
839 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
840 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000841
Pavel Labath07d6f882017-12-11 10:09:14 +0000842 PseudoTerminal terminal;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 const size_t err_len = 1024;
844 char err_str[err_len];
845 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000846
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 // Propagate the environment if one is not supplied.
Pavel Labath75c6de02018-01-10 13:53:40 +0000848 Environment::Envp envp =
849 (args->m_env.empty() ? Host::GetEnvironment() : args->m_env).getEnvp();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 if ((pid = terminal.Fork(err_str, err_len)) == -1) {
852 args->m_error.SetErrorToGenericError();
853 args->m_error.SetErrorString("Process fork failed.");
854 goto FINISH;
855 }
856
857 // Recognized child exit status codes.
858 enum {
859 ePtraceFailed = 1,
860 eDupStdinFailed,
861 eDupStdoutFailed,
862 eDupStderrFailed,
863 eChdirFailed,
864 eExecFailed,
865 eSetGidFailed
866 };
867
868 // Child process.
869 if (pid == 0) {
870 // Trace this process.
871 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
872 exit(ePtraceFailed);
873
Adrian Prantl05097242018-04-30 16:49:04 +0000874 // terminal has already dupped the tty descriptors to stdin/out/err. This
875 // closes original fd from which they were copied (and avoids leaking
876 // descriptors to the debugged process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 terminal.CloseSlaveFileDescriptor();
878
879 // Do not inherit setgid powers.
880 if (setgid(getgid()) != 0)
881 exit(eSetGidFailed);
882
883 // Let us have our own process group.
884 setpgid(0, 0);
885
886 // Dup file descriptors if needed.
887 //
888 // FIXME: If two or more of the paths are the same we needlessly open
889 // the same file multiple times.
890 if (stdin_file_spec)
891 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
892 exit(eDupStdinFailed);
893
894 if (stdout_file_spec)
895 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
896 exit(eDupStdoutFailed);
897
898 if (stderr_file_spec)
899 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
900 exit(eDupStderrFailed);
901
902 // Change working directory
903 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
904 exit(eChdirFailed);
905
906 // Execute. We should never return.
Pavel Labath75c6de02018-01-10 13:53:40 +0000907 execve(argv[0], const_cast<char *const *>(argv), envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 exit(eExecFailed);
909 }
910
911 // Wait for the child process to to trap on its call to execve.
912 ::pid_t wpid;
913 int status;
914 if ((wpid = waitpid(pid, &status, 0)) < 0) {
915 args->m_error.SetErrorToErrno();
916 goto FINISH;
917 } else if (WIFEXITED(status)) {
918 // open, dup or execve likely failed for some reason.
919 args->m_error.SetErrorToGenericError();
920 switch (WEXITSTATUS(status)) {
921 case ePtraceFailed:
922 args->m_error.SetErrorString("Child ptrace failed.");
923 break;
924 case eDupStdinFailed:
925 args->m_error.SetErrorString("Child open stdin failed.");
926 break;
927 case eDupStdoutFailed:
928 args->m_error.SetErrorString("Child open stdout failed.");
929 break;
930 case eDupStderrFailed:
931 args->m_error.SetErrorString("Child open stderr failed.");
932 break;
933 case eChdirFailed:
934 args->m_error.SetErrorString("Child failed to set working directory.");
935 break;
936 case eExecFailed:
937 args->m_error.SetErrorString("Child exec failed.");
938 break;
939 case eSetGidFailed:
940 args->m_error.SetErrorString("Child setgid failed.");
941 break;
942 default:
943 args->m_error.SetErrorString("Child returned unknown exit status.");
944 break;
Ed Mastea02f5532013-07-02 16:45:16 +0000945 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 goto FINISH;
947 }
948 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
949 "Could not sync with inferior process.");
Johnny Chen9ed5b492012-01-05 21:48:15 +0000950
951#ifdef notyet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 // Have the child raise an event on exit. This is used to keep the child in
953 // limbo until it is destroyed.
954 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
955 args->m_error.SetErrorToErrno();
956 goto FINISH;
957 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000958#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 // Release the master terminal descriptor and pass it off to the
960 // ProcessMonitor instance. Similarly stash the inferior pid.
961 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
962 monitor->m_pid = pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 // Set the terminal fd to be in non blocking mode (it simplifies the
965 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
966 // descriptor to read from).
967 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
968 goto FINISH;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000971
972FINISH:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 return args->m_error.Success();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000974}
975
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
Zachary Turner97206d52017-05-12 04:51:55 +0000977 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 static const char *g_thread_name = "lldb.process.freebsd.operation";
Johnny Chen9ed5b492012-01-05 21:48:15 +0000979
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 if (m_operation_thread.IsJoinable())
981 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000982
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983 m_operation_thread =
984 ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987void *ProcessMonitor::AttachOpThread(void *arg) {
988 AttachArgs *args = static_cast<AttachArgs *>(arg);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000989
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 ServeOperation(args);
993 return NULL;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000994}
995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996void ProcessMonitor::Attach(AttachArgs *args) {
997 lldb::pid_t pid = args->m_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000998
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 ProcessMonitor *monitor = args->m_monitor;
1000 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001001
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 if (pid <= 1) {
1003 args->m_error.SetErrorToGenericError();
1004 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1005 return;
1006 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 // Attach to the requested process.
1009 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
1010 args->m_error.SetErrorToErrno();
1011 return;
1012 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 int status;
1015 if ((status = waitpid(pid, NULL, 0)) < 0) {
1016 args->m_error.SetErrorToErrno();
1017 return;
1018 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001021}
1022
Ed Maste7fd845c2013-12-09 15:51:17 +00001023size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1025 lwpid_t *tids;
1026 int tdcnt;
Ed Maste7fd845c2013-12-09 15:51:17 +00001027
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 thread_ids.clear();
Ed Maste7fd845c2013-12-09 15:51:17 +00001029
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1031 if (tdcnt <= 0)
1032 return 0;
1033 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1034 if (tids == NULL)
1035 return 0;
1036 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
Ed Maste7fd845c2013-12-09 15:51:17 +00001037 free(tids);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 return 0;
1039 }
1040 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1041 free(tids);
1042 return thread_ids.size();
Ed Maste7fd845c2013-12-09 15:51:17 +00001043}
1044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1046 bool exited, int signal, int status) {
1047 ProcessMessage message;
1048 ProcessFreeBSD *process = monitor->m_process;
1049 assert(process);
1050 bool stop_monitoring;
1051 struct ptrace_lwpinfo plwp;
1052 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 if (exited) {
1057 if (log)
1058 log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1059 __FUNCTION__, pid);
1060 message = ProcessMessage::Exit(pid, status);
1061 process->SendMessage(message);
1062 return pid == process->GetID();
1063 }
Ed Mastea02f5532013-07-02 16:45:16 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1066 stop_monitoring = true; // pid is gone. Bail.
1067 else {
1068 switch (plwp.pl_siginfo.si_signo) {
1069 case SIGTRAP:
1070 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1071 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001072
Johnny Chen9ed5b492012-01-05 21:48:15 +00001073 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1075 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001076 }
1077
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 process->SendMessage(message);
1079 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1080 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 return stop_monitoring;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001083}
1084
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1086 const siginfo_t *info,
1087 lldb::tid_t tid) {
1088 ProcessMessage message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001089
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Mastea02f5532013-07-02 16:45:16 +00001091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 assert(monitor);
1093 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 switch (info->si_code) {
1096 default:
1097 assert(false && "Unexpected SIGTRAP code!");
1098 break;
1099
1100 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
Adrian Prantl05097242018-04-30 16:49:04 +00001101 // The inferior process is about to exit. Maintain the process in a state
1102 // of "limbo" until we are explicitly commanded to detach, destroy, resume,
1103 // etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 unsigned long data = 0;
1105 if (!monitor->GetEventMessage(tid, &data))
1106 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001107 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1109 "= %" PRIu64,
1110 __FUNCTION__, data, tid);
1111 message = ProcessMessage::Limbo(tid, (data >> 8));
1112 break;
1113 }
Ed Mastea02f5532013-07-02 16:45:16 +00001114
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 case 0:
1116 case TRAP_TRACE:
Ed Masted6fa2c32017-05-26 03:15:46 +00001117#ifdef TRAP_CAP
1118 // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1119 case TRAP_CAP:
1120#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 if (log)
1122 log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1123 " : si_code = %d",
1124 __FUNCTION__, tid, info->si_code);
1125 message = ProcessMessage::Trace(tid);
1126 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001127
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 case SI_KERNEL:
1129 case TRAP_BRKPT:
Ed Maste31f01802017-01-24 14:34:49 +00001130 if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1131 if (log)
1132 log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1133 "event, tid = %" PRIu64,
1134 __FUNCTION__, tid);
1135 message = ProcessMessage::Trace(tid);
1136 } else {
1137 if (log)
1138 log->Printf(
1139 "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1140 __FUNCTION__, tid);
1141 message = ProcessMessage::Break(tid);
1142 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 break;
1144 }
1145
1146 return message;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001147}
1148
Kate Stoneb9c1b512016-09-06 20:57:50 +00001149ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1150 const siginfo_t *info,
1151 lldb::tid_t tid) {
1152 ProcessMessage message;
1153 int signo = info->si_signo;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001154
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001156
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
Adrian Prantl05097242018-04-30 16:49:04 +00001158 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a kill(2)
1159 // or raise(3). Similarly for tgkill(2) on FreeBSD.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 //
1161 // IOW, user generated signals never generate what we consider to be a
1162 // "crash".
1163 //
1164 // Similarly, ACK signals generated by this monitor.
1165 if (info->si_code == SI_USER) {
1166 if (log)
1167 log->Printf(
1168 "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1169 __FUNCTION__,
1170 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1171 "SI_USER", info->si_pid);
1172 if (info->si_pid == getpid())
1173 return ProcessMessage::SignalDelivered(tid, signo);
1174 else
1175 return ProcessMessage::Signal(tid, signo);
1176 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001177
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 if (log)
1179 log->Printf(
1180 "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1181 monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 switch (signo) {
1184 case SIGSEGV:
1185 case SIGILL:
1186 case SIGFPE:
1187 case SIGBUS:
1188 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1189 const auto reason = GetCrashReason(*info);
Ed Maste5e82ca32017-08-10 13:47:17 +00001190 if (reason != CrashReason::eInvalidCrashReason) {
1191 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1192 } // else; Use atleast si_signo info for other si_code
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 }
1194
Adrian Prantl05097242018-04-30 16:49:04 +00001195 // Everything else is "normal" and does not require any special action on our
1196 // part.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001198}
1199
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200void ProcessMonitor::ServeOperation(OperationArgs *args) {
1201 ProcessMonitor *monitor = args->m_monitor;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001202
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203 // We are finised with the arguments and are ready to go. Sync with the
1204 // parent thread and start serving operations on the inferior.
1205 sem_post(&args->m_semaphore);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001206
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207 for (;;) {
1208 // wait for next pending operation
1209 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001210
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 monitor->m_operation->Execute(monitor);
1212
1213 // notify calling thread that operation is complete
1214 sem_post(&monitor->m_operation_done);
1215 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001216}
1217
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218void ProcessMonitor::DoOperation(Operation *op) {
1219 std::lock_guard<std::mutex> guard(m_operation_mutex);
1220
1221 m_operation = op;
1222
1223 // notify operation thread that an operation is ready to be processed
1224 sem_post(&m_operation_pending);
1225
1226 // wait for operation to complete
1227 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001228}
1229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +00001231 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232 size_t result;
1233 ReadOperation op(vm_addr, buf, size, error, result);
1234 DoOperation(&op);
1235 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001236}
1237
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
Zachary Turner97206d52017-05-12 04:51:55 +00001239 size_t size, lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 size_t result;
1241 WriteOperation op(vm_addr, buf, size, error, result);
1242 DoOperation(&op);
1243 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001244}
1245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246bool ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001247 const char *reg_name, unsigned size,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 RegisterValue &value) {
1249 bool result;
1250 ReadRegOperation op(tid, offset, size, value, result);
1251 DoOperation(&op);
1252 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001253}
1254
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255bool ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ed Mastea4be2c52014-02-19 18:34:06 +00001256 const char *reg_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 const RegisterValue &value) {
1258 bool result;
1259 WriteRegOperation op(tid, offset, value, result);
1260 DoOperation(&op);
1261 return result;
Ed Mastea4be2c52014-02-19 18:34:06 +00001262}
1263
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264bool ProcessMonitor::ReadDebugRegisterValue(
1265 lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1266 lldb_private::RegisterValue &value) {
1267 bool result;
1268 ReadDebugRegOperation op(tid, offset, size, value, result);
1269 DoOperation(&op);
1270 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001271}
1272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273bool ProcessMonitor::WriteDebugRegisterValue(
1274 lldb::tid_t tid, unsigned offset, const char *reg_name,
1275 const lldb_private::RegisterValue &value) {
1276 bool result;
1277 WriteDebugRegOperation op(tid, offset, value, result);
1278 DoOperation(&op);
1279 return result;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001280}
1281
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1283 bool result;
1284 ReadGPROperation op(tid, buf, result);
1285 DoOperation(&op);
1286 return result;
1287}
1288
1289bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1290 bool result;
1291 ReadFPROperation op(tid, buf, result);
1292 DoOperation(&op);
1293 return result;
1294}
1295
1296bool ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf,
1297 size_t buf_size, unsigned int regset) {
1298 return false;
1299}
1300
1301bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1302 bool result;
1303 WriteGPROperation op(tid, buf, result);
1304 DoOperation(&op);
1305 return result;
1306}
1307
1308bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1309 bool result;
1310 WriteFPROperation op(tid, buf, result);
1311 DoOperation(&op);
1312 return result;
1313}
1314
1315bool ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf,
1316 size_t buf_size, unsigned int regset) {
1317 return false;
1318}
1319
1320bool ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value) {
1321 return false;
1322}
1323
1324bool ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo) {
1325 bool result;
1326 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
1327
1328 if (log) {
1329 const char *signame =
1330 m_process->GetUnixSignals()->GetSignalAsCString(signo);
1331 if (signame == nullptr)
1332 signame = "<none>";
1333 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1334 __FUNCTION__, GetPID(), signame);
1335 }
1336 ResumeOperation op(signo, result);
1337 DoOperation(&op);
1338 if (log)
1339 log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1340 result ? "true" : "false");
1341 return result;
1342}
1343
1344bool ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo) {
1345 bool result;
1346 SingleStepOperation op(signo, result);
1347 DoOperation(&op);
1348 return result;
1349}
1350
1351bool ProcessMonitor::Kill() {
1352 bool result;
1353 KillOperation op(result);
1354 DoOperation(&op);
1355 return result;
1356}
1357
1358bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1359 int &ptrace_err) {
1360 bool result;
1361 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1362 DoOperation(&op);
1363 return result;
1364}
1365
1366bool ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend) {
1367 bool result;
1368 ThreadSuspendOperation op(tid, suspend, result);
1369 DoOperation(&op);
1370 return result;
1371}
1372
1373bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1374 bool result;
1375 EventMessageOperation op(tid, message, result);
1376 DoOperation(&op);
1377 return result;
1378}
1379
Zachary Turner97206d52017-05-12 04:51:55 +00001380lldb_private::Status ProcessMonitor::Detach(lldb::tid_t tid) {
1381 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 if (tid != LLDB_INVALID_THREAD_ID) {
1383 DetachOperation op(error);
1384 DoOperation(&op);
1385 }
1386 return error;
1387}
1388
1389bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1390 int flags) {
1391 int target_fd = open(file_spec.GetCString(), flags, 0666);
1392
1393 if (target_fd == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001394 return false;
Ed Maste5d34af32013-06-24 15:09:18 +00001395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 if (dup2(target_fd, fd) == -1)
Ed Maste5d34af32013-06-24 15:09:18 +00001397 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398
1399 return (close(target_fd) == -1) ? false : true;
Ed Maste5d34af32013-06-24 15:09:18 +00001400}
1401
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402void ProcessMonitor::StopMonitoringChildProcess() {
1403 if (m_monitor_thread.IsJoinable()) {
1404 m_monitor_thread.Cancel();
1405 m_monitor_thread.Join(nullptr);
1406 m_monitor_thread.Reset();
1407 }
Ed Maste68f51792013-10-18 19:16:44 +00001408}
1409
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410void ProcessMonitor::StopMonitor() {
1411 StopMonitoringChildProcess();
1412 StopOpThread();
1413 sem_destroy(&m_operation_pending);
1414 sem_destroy(&m_operation_done);
1415 if (m_terminal_fd >= 0) {
1416 close(m_terminal_fd);
1417 m_terminal_fd = -1;
1418 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001419}
1420
Andrew Kaylord4d54992013-09-17 00:30:24 +00001421// FIXME: On Linux, when a new thread is created, we receive to notifications,
Adrian Prantl05097242018-04-30 16:49:04 +00001422// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the child
1423// thread id as additional information, and (2) a SIGSTOP|SI_USER from the new
1424// child thread indicating that it has is stopped because we attached. We have
1425// no guarantee of the order in which these arrive, but we need both before we
1426// are ready to proceed. We currently keep a list of threads which have sent
1427// the initial SIGSTOP|SI_USER event. Then when we receive the
1428// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not
1429// occurred we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
Andrew Kaylord4d54992013-09-17 00:30:24 +00001430//
1431// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1432// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1433// logically needed.
1434//
1435// We really should figure out what actually happens on FreeBSD and move the
1436// Linux-specific logic out of ProcessPOSIX as needed.
1437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438bool ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid) { return true; }
Andrew Kaylord4d54992013-09-17 00:30:24 +00001439
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440void ProcessMonitor::StopOpThread() {
1441 if (!m_operation_thread.IsJoinable())
1442 return;
Ed Mastea02f5532013-07-02 16:45:16 +00001443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 m_operation_thread.Cancel();
1445 m_operation_thread.Join(nullptr);
1446 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001447}