blob: 1ff0782fc2f10d14be0f36fbe47ae811a8cfbbd8 [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
18#include <sys/ptrace.h>
19#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000020#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000021#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000022#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include <sys/wait.h>
24
25// C++ Includes
26// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000027#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000029#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Core/Scalar.h"
31#include "lldb/Host/Host.h"
32#include "lldb/Target/Thread.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Utility/PseudoTerminal.h"
35
Johnny Chen30213ff2012-01-05 19:17:38 +000036#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000038#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
40
Greg Clayton386ff182011-11-05 01:09:16 +000041#define DEBUG_PTRACE_MAXBYTES 20
42
Matt Kopec58c0b962013-03-20 20:34:35 +000043// Support ptrace extensions even when compiled without required kernel support
44#ifndef PTRACE_GETREGSET
45 #define PTRACE_GETREGSET 0x4204
46#endif
47#ifndef PTRACE_SETREGSET
48 #define PTRACE_SETREGSET 0x4205
49#endif
Richard Mitton0a558352013-10-17 21:14:00 +000050#ifndef PTRACE_GET_THREAD_AREA
51 #define PTRACE_GET_THREAD_AREA 25
52#endif
53#ifndef PTRACE_ARCH_PRCTL
54 #define PTRACE_ARCH_PRCTL 30
55#endif
56#ifndef ARCH_GET_FS
57 #define ARCH_SET_GS 0x1001
58 #define ARCH_SET_FS 0x1002
59 #define ARCH_GET_FS 0x1003
60 #define ARCH_GET_GS 0x1004
61#endif
62
Matt Kopec58c0b962013-03-20 20:34:35 +000063
Matt Kopece9ea0da2013-05-07 19:29:28 +000064// Support hardware breakpoints in case it has not been defined
65#ifndef TRAP_HWBKPT
66 #define TRAP_HWBKPT 4
67#endif
68
Andrew Kaylor93132f52013-05-28 23:04:25 +000069// Try to define a macro to encapsulate the tgkill syscall
70// fall back on kill() if tgkill isn't available
71#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
72
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073using namespace lldb_private;
74
Johnny Chen0d5f2d42011-10-18 18:09:30 +000075// FIXME: this code is host-dependent with respect to types and
76// endianness and needs to be fixed. For example, lldb::addr_t is
77// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
78// 32-bit pointer arguments. This code uses casts to work around the
79// problem.
80
81// We disable the tracing of ptrace calls for integration builds to
82// avoid the additional indirection and checks.
83#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
84
Greg Clayton386ff182011-11-05 01:09:16 +000085static void
86DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
87{
88 uint8_t *ptr = (uint8_t *)bytes;
89 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
90 for(uint32_t i=0; i<loop_count; i++)
91 {
92 s.Printf ("[%x]", *ptr);
93 ptr++;
94 }
95}
96
Matt Kopec58c0b962013-03-20 20:34:35 +000097static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000098{
99 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000100 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000101 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000102
103 if (verbose_log)
104 {
105 switch(req)
106 {
107 case PTRACE_POKETEXT:
108 {
109 DisplayBytes(buf, &data, 8);
110 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
111 break;
112 }
Greg Clayton542e4072012-09-07 17:49:29 +0000113 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000114 {
115 DisplayBytes(buf, &data, 8);
116 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, &data, 8);
122 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
123 break;
124 }
Greg Clayton542e4072012-09-07 17:49:29 +0000125 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000126 {
Matt Kopec7de48462013-03-06 17:20:48 +0000127 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000128 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
129 break;
130 }
131 case PTRACE_SETFPREGS:
132 {
Matt Kopec7de48462013-03-06 17:20:48 +0000133 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000134 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
135 break;
136 }
Greg Clayton542e4072012-09-07 17:49:29 +0000137 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000138 {
139 DisplayBytes(buf, data, sizeof(siginfo_t));
140 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
141 break;
142 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000143 case PTRACE_SETREGSET:
144 {
145 // Extract iov_base from data, which is a pointer to the struct IOVEC
146 DisplayBytes(buf, *(void **)data, data_size);
147 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
148 break;
149 }
Greg Clayton386ff182011-11-05 01:09:16 +0000150 default:
151 {
152 }
153 }
154 }
155}
156
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000157// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000158// Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000159extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000160PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000161 const char* reqName, const char* file, int line)
162{
Greg Clayton386ff182011-11-05 01:09:16 +0000163 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000164
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000165 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000166
Matt Kopec7de48462013-03-06 17:20:48 +0000167 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000168
169 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000170 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000171 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000172 else
Todd Fiala4507f062014-02-27 20:46:12 +0000173 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000174
Ed Mastec099c952014-02-24 14:07:45 +0000175 if (log)
176 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
177 reqName, pid, addr, data, data_size, result, file, line);
178
Matt Kopec7de48462013-03-06 17:20:48 +0000179 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000180
Matt Kopec7de48462013-03-06 17:20:48 +0000181 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000182 {
183 const char* str;
184 switch (errno)
185 {
186 case ESRCH: str = "ESRCH"; break;
187 case EINVAL: str = "EINVAL"; break;
188 case EBUSY: str = "EBUSY"; break;
189 case EPERM: str = "EPERM"; break;
190 default: str = "<unknown>";
191 }
192 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
193 }
194
195 return result;
196}
197
Matt Kopec7de48462013-03-06 17:20:48 +0000198// Wrapper for ptrace when logging is not required.
199// Sets errno to 0 prior to calling ptrace.
200extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000201PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000202{
Matt Kopec58c0b962013-03-20 20:34:35 +0000203 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000204 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000205 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
206 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
207 else
208 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000209 return result;
210}
211
212#define PTRACE(req, pid, addr, data, data_size) \
213 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000214#else
Matt Kopec7de48462013-03-06 17:20:48 +0000215 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000216#endif
217
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218//------------------------------------------------------------------------------
219// Static implementations of ProcessMonitor::ReadMemory and
220// ProcessMonitor::WriteMemory. This enables mutual recursion between these
221// functions without needed to go thru the thread funnel.
222
223static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000224DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000225 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
226{
Greg Clayton542e4072012-09-07 17:49:29 +0000227 // ptrace word size is determined by the host, not the child
228 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 unsigned char *dst = static_cast<unsigned char*>(buf);
230 size_t bytes_read;
231 size_t remainder;
232 long data;
233
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000235 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000236 ProcessPOSIXLog::IncNestLevel();
237 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000238 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000239 pid, word_size, (void*)vm_addr, buf, size);
240
241 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000242 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
243 {
244 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000245 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
246 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000247 {
248 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000249 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000250 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 return bytes_read;
252 }
253
254 remainder = size - bytes_read;
255 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000256
257 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000258 for (unsigned i = 0; i < remainder; ++i)
259 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
262 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
263 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
264 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000265 {
266 uintptr_t print_dst = 0;
267 // Format bytes from data by moving into print_dst for log output
268 for (unsigned i = 0; i < remainder; ++i)
269 print_dst |= (((data >> i*8) & 0xFF) << i*8);
270 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
271 (void*)vm_addr, print_dst, (unsigned long)data);
272 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000273
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274 vm_addr += word_size;
275 dst += word_size;
276 }
277
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000278 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000279 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 return bytes_read;
281}
282
283static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000284DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
286{
Greg Clayton542e4072012-09-07 17:49:29 +0000287 // ptrace word size is determined by the host, not the child
288 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000289 const unsigned char *src = static_cast<const unsigned char*>(buf);
290 size_t bytes_written = 0;
291 size_t remainder;
292
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000293 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000294 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000295 ProcessPOSIXLog::IncNestLevel();
296 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000297 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 pid, word_size, (void*)vm_addr, buf, size);
299
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000300 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
301 {
302 remainder = size - bytes_written;
303 remainder = remainder > word_size ? word_size : remainder;
304
305 if (remainder == word_size)
306 {
307 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000308 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000309 for (unsigned i = 0; i < word_size; ++i)
310 data |= (unsigned long)src[i] << i*8;
311
Johnny Chen30213ff2012-01-05 19:17:38 +0000312 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
313 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
314 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
315 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000316 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
317 (void*)vm_addr, *(unsigned long*)src, data);
318
Matt Kopec7de48462013-03-06 17:20:48 +0000319 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000320 {
321 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000323 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000324 return bytes_written;
325 }
326 }
327 else
328 {
329 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000330 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000332 {
333 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000334 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000335 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000336 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000337
338 memcpy(buff, src, remainder);
339
Greg Clayton542e4072012-09-07 17:49:29 +0000340 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 {
343 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000344 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 }
347
Johnny Chen30213ff2012-01-05 19:17:38 +0000348 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
349 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
350 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
351 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
353 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000354 }
355
356 vm_addr += word_size;
357 src += word_size;
358 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000359 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000360 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000361 return bytes_written;
362}
363
Stephen Wilson26977162011-03-23 02:14:42 +0000364// Simple helper function to ensure flags are enabled on the given file
365// descriptor.
366static bool
367EnsureFDFlags(int fd, int flags, Error &error)
368{
369 int status;
370
371 if ((status = fcntl(fd, F_GETFL)) == -1)
372 {
373 error.SetErrorToErrno();
374 return false;
375 }
376
377 if (fcntl(fd, F_SETFL, status | flags) == -1)
378 {
379 error.SetErrorToErrno();
380 return false;
381 }
382
383 return true;
384}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000385
386//------------------------------------------------------------------------------
387/// @class Operation
388/// @brief Represents a ProcessMonitor operation.
389///
390/// Under Linux, it is not possible to ptrace() from any other thread but the
391/// one that spawned or attached to the process from the start. Therefore, when
392/// a ProcessMonitor is asked to deliver or change the state of an inferior
393/// process the operation must be "funneled" to a specific thread to perform the
394/// task. The Operation class provides an abstract base for all services the
395/// ProcessMonitor must perform via the single virtual function Execute, thus
396/// encapsulating the code that needs to run in the privileged context.
397class Operation
398{
399public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000400 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000401 virtual void Execute(ProcessMonitor *monitor) = 0;
402};
403
404//------------------------------------------------------------------------------
405/// @class ReadOperation
406/// @brief Implements ProcessMonitor::ReadMemory.
407class ReadOperation : public Operation
408{
409public:
410 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
411 Error &error, size_t &result)
412 : m_addr(addr), m_buff(buff), m_size(size),
413 m_error(error), m_result(result)
414 { }
415
416 void Execute(ProcessMonitor *monitor);
417
418private:
419 lldb::addr_t m_addr;
420 void *m_buff;
421 size_t m_size;
422 Error &m_error;
423 size_t &m_result;
424};
425
426void
427ReadOperation::Execute(ProcessMonitor *monitor)
428{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000429 lldb::pid_t pid = monitor->GetPID();
430
Greg Clayton542e4072012-09-07 17:49:29 +0000431 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000432}
433
434//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000435/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000436/// @brief Implements ProcessMonitor::WriteMemory.
437class WriteOperation : public Operation
438{
439public:
440 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
441 Error &error, size_t &result)
442 : m_addr(addr), m_buff(buff), m_size(size),
443 m_error(error), m_result(result)
444 { }
445
446 void Execute(ProcessMonitor *monitor);
447
448private:
449 lldb::addr_t m_addr;
450 const void *m_buff;
451 size_t m_size;
452 Error &m_error;
453 size_t &m_result;
454};
455
456void
457WriteOperation::Execute(ProcessMonitor *monitor)
458{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000459 lldb::pid_t pid = monitor->GetPID();
460
Greg Clayton542e4072012-09-07 17:49:29 +0000461 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000462}
463
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000464
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465//------------------------------------------------------------------------------
466/// @class ReadRegOperation
467/// @brief Implements ProcessMonitor::ReadRegisterValue.
468class ReadRegOperation : public Operation
469{
470public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000471 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000472 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000473 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000474 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000475 { }
476
477 void Execute(ProcessMonitor *monitor);
478
479private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000481 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000482 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000483 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000484 bool &m_result;
485};
486
487void
488ReadRegOperation::Execute(ProcessMonitor *monitor)
489{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000490 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000491
492 // Set errno to zero so that we can detect a failed peek.
493 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000494 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
495 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000496 m_result = false;
497 else
498 {
499 m_value = data;
500 m_result = true;
501 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000502 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000503 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000504 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000505}
506
507//------------------------------------------------------------------------------
508/// @class WriteRegOperation
509/// @brief Implements ProcessMonitor::WriteRegisterValue.
510class WriteRegOperation : public Operation
511{
512public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000513 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000514 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000515 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000516 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000517 { }
518
519 void Execute(ProcessMonitor *monitor);
520
521private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000523 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000524 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000525 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000526 bool &m_result;
527};
528
529void
530WriteRegOperation::Execute(ProcessMonitor *monitor)
531{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000532 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000533 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000534
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000535 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000536
537 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000538 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000539 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000540 m_result = false;
541 else
542 m_result = true;
543}
544
545//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000546/// @class ReadGPROperation
547/// @brief Implements ProcessMonitor::ReadGPR.
548class ReadGPROperation : public Operation
549{
550public:
Matt Kopec7de48462013-03-06 17:20:48 +0000551 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
552 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000553 { }
554
555 void Execute(ProcessMonitor *monitor);
556
557private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000558 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000559 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000560 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000561 bool &m_result;
562};
563
564void
565ReadGPROperation::Execute(ProcessMonitor *monitor)
566{
Matt Kopec7de48462013-03-06 17:20:48 +0000567 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000568 m_result = false;
569 else
570 m_result = true;
571}
572
573//------------------------------------------------------------------------------
574/// @class ReadFPROperation
575/// @brief Implements ProcessMonitor::ReadFPR.
576class ReadFPROperation : public Operation
577{
578public:
Matt Kopec7de48462013-03-06 17:20:48 +0000579 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
580 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000581 { }
582
583 void Execute(ProcessMonitor *monitor);
584
585private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000586 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000587 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000588 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000589 bool &m_result;
590};
591
592void
593ReadFPROperation::Execute(ProcessMonitor *monitor)
594{
Matt Kopec7de48462013-03-06 17:20:48 +0000595 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000596 m_result = false;
597 else
598 m_result = true;
599}
600
601//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000602/// @class ReadRegisterSetOperation
603/// @brief Implements ProcessMonitor::ReadRegisterSet.
604class ReadRegisterSetOperation : public Operation
605{
606public:
607 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
608 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
609 { }
610
611 void Execute(ProcessMonitor *monitor);
612
613private:
614 lldb::tid_t m_tid;
615 void *m_buf;
616 size_t m_buf_size;
617 const unsigned int m_regset;
618 bool &m_result;
619};
620
621void
622ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
623{
624 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
625 m_result = false;
626 else
627 m_result = true;
628}
629
630//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000631/// @class WriteGPROperation
632/// @brief Implements ProcessMonitor::WriteGPR.
633class WriteGPROperation : public Operation
634{
635public:
Matt Kopec7de48462013-03-06 17:20:48 +0000636 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
637 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000638 { }
639
640 void Execute(ProcessMonitor *monitor);
641
642private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000643 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000644 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000645 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000646 bool &m_result;
647};
648
649void
650WriteGPROperation::Execute(ProcessMonitor *monitor)
651{
Matt Kopec7de48462013-03-06 17:20:48 +0000652 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000653 m_result = false;
654 else
655 m_result = true;
656}
657
658//------------------------------------------------------------------------------
659/// @class WriteFPROperation
660/// @brief Implements ProcessMonitor::WriteFPR.
661class WriteFPROperation : public Operation
662{
663public:
Matt Kopec7de48462013-03-06 17:20:48 +0000664 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
665 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000666 { }
667
668 void Execute(ProcessMonitor *monitor);
669
670private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000671 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000672 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000673 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000674 bool &m_result;
675};
676
677void
678WriteFPROperation::Execute(ProcessMonitor *monitor)
679{
Matt Kopec7de48462013-03-06 17:20:48 +0000680 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000681 m_result = false;
682 else
683 m_result = true;
684}
685
686//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000687/// @class WriteRegisterSetOperation
688/// @brief Implements ProcessMonitor::WriteRegisterSet.
689class WriteRegisterSetOperation : public Operation
690{
691public:
692 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
693 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
694 { }
695
696 void Execute(ProcessMonitor *monitor);
697
698private:
699 lldb::tid_t m_tid;
700 void *m_buf;
701 size_t m_buf_size;
702 const unsigned int m_regset;
703 bool &m_result;
704};
705
706void
707WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
708{
709 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
710 m_result = false;
711 else
712 m_result = true;
713}
714
715//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000716/// @class ReadThreadPointerOperation
717/// @brief Implements ProcessMonitor::ReadThreadPointer.
718class ReadThreadPointerOperation : public Operation
719{
720public:
721 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
722 : m_tid(tid), m_addr(addr), m_result(result)
723 { }
724
725 void Execute(ProcessMonitor *monitor);
726
727private:
728 lldb::tid_t m_tid;
729 lldb::addr_t *m_addr;
730 bool &m_result;
731};
732
733void
734ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
735{
736 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
737 if (log)
738 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
739
740 // The process for getting the thread area on Linux is
741 // somewhat... obscure. There's several different ways depending on
742 // what arch you're on, and what kernel version you have.
743
744 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
745 switch(arch.GetMachine())
746 {
Todd Fiala720cd3f2014-06-16 14:49:28 +0000747#if defined(__i386__) || defined(__x86_64__)
748 // Note that struct user below has a field named i387 which is x86-specific.
749 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000750 case llvm::Triple::x86:
751 {
752 // Find the GS register location for our host architecture.
753 size_t gs_user_offset = offsetof(struct user, regs);
754#ifdef __x86_64__
755 gs_user_offset += offsetof(struct user_regs_struct, gs);
756#endif
757#ifdef __i386__
758 gs_user_offset += offsetof(struct user_regs_struct, xgs);
759#endif
760
761 // Read the GS register value to get the selector.
762 errno = 0;
763 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
764 if (errno)
765 {
766 m_result = false;
767 break;
768 }
769
770 // Read the LDT base for that selector.
771 uint32_t tmp[4];
772 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
773 *m_addr = tmp[1];
774 break;
775 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000776#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000777 case llvm::Triple::x86_64:
778 // Read the FS register base.
779 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
780 break;
781 default:
782 m_result = false;
783 break;
784 }
785}
786
787//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000788/// @class ResumeOperation
789/// @brief Implements ProcessMonitor::Resume.
790class ResumeOperation : public Operation
791{
792public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000793 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
794 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000795
796 void Execute(ProcessMonitor *monitor);
797
798private:
799 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000800 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000801 bool &m_result;
802};
803
804void
805ResumeOperation::Execute(ProcessMonitor *monitor)
806{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000807 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000808
809 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
810 data = m_signo;
811
Matt Kopec7de48462013-03-06 17:20:48 +0000812 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000813 {
814 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
815
816 if (log)
817 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000818 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000819 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000820 else
821 m_result = true;
822}
823
824//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000825/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000826/// @brief Implements ProcessMonitor::SingleStep.
827class SingleStepOperation : public Operation
828{
829public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000830 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
831 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000832
833 void Execute(ProcessMonitor *monitor);
834
835private:
836 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000837 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 bool &m_result;
839};
840
841void
842SingleStepOperation::Execute(ProcessMonitor *monitor)
843{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000844 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000845
846 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
847 data = m_signo;
848
Matt Kopec7de48462013-03-06 17:20:48 +0000849 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000850 m_result = false;
851 else
852 m_result = true;
853}
854
855//------------------------------------------------------------------------------
856/// @class SiginfoOperation
857/// @brief Implements ProcessMonitor::GetSignalInfo.
858class SiginfoOperation : public Operation
859{
860public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000861 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
862 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000863
864 void Execute(ProcessMonitor *monitor);
865
866private:
867 lldb::tid_t m_tid;
868 void *m_info;
869 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000870 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000871};
872
873void
874SiginfoOperation::Execute(ProcessMonitor *monitor)
875{
Matt Kopec7de48462013-03-06 17:20:48 +0000876 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000877 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000878 m_err = errno;
879 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000880 else
881 m_result = true;
882}
883
884//------------------------------------------------------------------------------
885/// @class EventMessageOperation
886/// @brief Implements ProcessMonitor::GetEventMessage.
887class EventMessageOperation : public Operation
888{
889public:
890 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
891 : m_tid(tid), m_message(message), m_result(result) { }
892
893 void Execute(ProcessMonitor *monitor);
894
895private:
896 lldb::tid_t m_tid;
897 unsigned long *m_message;
898 bool &m_result;
899};
900
901void
902EventMessageOperation::Execute(ProcessMonitor *monitor)
903{
Matt Kopec7de48462013-03-06 17:20:48 +0000904 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000905 m_result = false;
906 else
907 m_result = true;
908}
909
910//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000911/// @class DetachOperation
912/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000913class DetachOperation : public Operation
914{
915public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000916 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000917
918 void Execute(ProcessMonitor *monitor);
919
920private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000921 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000922 Error &m_error;
923};
924
925void
926DetachOperation::Execute(ProcessMonitor *monitor)
927{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000928 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000929 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000930}
931
Johnny Chen25e68e32011-06-14 19:19:50 +0000932ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
933 : m_monitor(monitor)
934{
935 sem_init(&m_semaphore, 0, 0);
936}
937
938ProcessMonitor::OperationArgs::~OperationArgs()
939{
940 sem_destroy(&m_semaphore);
941}
942
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000943ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
944 lldb_private::Module *module,
945 char const **argv,
946 char const **envp,
947 const char *stdin_path,
948 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000949 const char *stderr_path,
950 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000951 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000952 m_module(module),
953 m_argv(argv),
954 m_envp(envp),
955 m_stdin_path(stdin_path),
956 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000957 m_stderr_path(stderr_path),
958 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000959
960ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000961{ }
962
963ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
964 lldb::pid_t pid)
965 : OperationArgs(monitor), m_pid(pid) { }
966
967ProcessMonitor::AttachArgs::~AttachArgs()
968{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000969
970//------------------------------------------------------------------------------
971/// The basic design of the ProcessMonitor is built around two threads.
972///
973/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
974/// for changes in the debugee state. When a change is detected a
975/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
976/// "drives" state changes in the debugger.
977///
978/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000979/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000980/// operations such as register reads/writes, stepping, etc. See the comments
981/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000982ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000983 Module *module,
984 const char *argv[],
985 const char *envp[],
986 const char *stdin_path,
987 const char *stdout_path,
988 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000989 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000990 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000991 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000992 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000993 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000994 m_pid(LLDB_INVALID_PROCESS_ID),
995 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +0000996 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000997{
Daniel Malea1efb4182013-09-16 23:12:18 +0000998 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
999 stdin_path, stdout_path, stderr_path,
1000 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001001
Daniel Malea1efb4182013-09-16 23:12:18 +00001002 sem_init(&m_operation_pending, 0, 0);
1003 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001004
Johnny Chen25e68e32011-06-14 19:19:50 +00001005 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001006 if (!error.Success())
1007 return;
1008
1009WAIT_AGAIN:
1010 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001011 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012 {
1013 if (errno == EINTR)
1014 goto WAIT_AGAIN;
1015 else
1016 {
1017 error.SetErrorToErrno();
1018 return;
1019 }
1020 }
1021
1022 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001023 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001024 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001025 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001026 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001027 return;
1028 }
1029
1030 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001031 m_monitor_thread = Host::StartMonitoringChildProcess(
1032 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001033 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001034 {
1035 error.SetErrorToGenericError();
1036 error.SetErrorString("Process launch failed.");
1037 return;
1038 }
1039}
1040
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001041ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001042 lldb::pid_t pid,
1043 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001044 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001045 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001046 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001047 m_pid(LLDB_INVALID_PROCESS_ID),
1048 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001049 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001050{
Daniel Malea1efb4182013-09-16 23:12:18 +00001051 sem_init(&m_operation_pending, 0, 0);
1052 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001053
Daniel Malea1efb4182013-09-16 23:12:18 +00001054 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001055
1056 StartAttachOpThread(args.get(), error);
1057 if (!error.Success())
1058 return;
1059
1060WAIT_AGAIN:
1061 // Wait for the operation thread to initialize.
1062 if (sem_wait(&args->m_semaphore))
1063 {
1064 if (errno == EINTR)
1065 goto WAIT_AGAIN;
1066 else
1067 {
1068 error.SetErrorToErrno();
1069 return;
1070 }
1071 }
1072
Greg Clayton743ecf42012-10-16 20:20:18 +00001073 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001074 if (!args->m_error.Success())
1075 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001076 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001077 error = args->m_error;
1078 return;
1079 }
1080
1081 // Finally, start monitoring the child process for change in state.
1082 m_monitor_thread = Host::StartMonitoringChildProcess(
1083 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1084 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1085 {
1086 error.SetErrorToGenericError();
1087 error.SetErrorString("Process attach failed.");
1088 return;
1089 }
1090}
1091
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001092ProcessMonitor::~ProcessMonitor()
1093{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001094 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001095}
1096
1097//------------------------------------------------------------------------------
1098// Thread setup and tear down.
1099void
Johnny Chen25e68e32011-06-14 19:19:50 +00001100ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001101{
1102 static const char *g_thread_name = "lldb.process.linux.operation";
1103
Stephen Wilsond4182f42011-02-09 20:10:35 +00001104 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001105 return;
1106
1107 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001108 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001109}
1110
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001111void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001112ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001113{
1114 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1115
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001116 if (!Launch(args)) {
1117 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001118 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001119 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001120
Stephen Wilson570243b2011-01-19 01:37:06 +00001121 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001122 return NULL;
1123}
1124
1125bool
1126ProcessMonitor::Launch(LaunchArgs *args)
1127{
1128 ProcessMonitor *monitor = args->m_monitor;
1129 ProcessLinux &process = monitor->GetProcess();
1130 const char **argv = args->m_argv;
1131 const char **envp = args->m_envp;
1132 const char *stdin_path = args->m_stdin_path;
1133 const char *stdout_path = args->m_stdout_path;
1134 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001135 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001136
1137 lldb_utility::PseudoTerminal terminal;
1138 const size_t err_len = 1024;
1139 char err_str[err_len];
1140 lldb::pid_t pid;
1141
1142 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001143 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001144
Stephen Wilson57740ec2011-01-15 00:12:41 +00001145 // Propagate the environment if one is not supplied.
1146 if (envp == NULL || envp[0] == NULL)
1147 envp = const_cast<const char **>(environ);
1148
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001149 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001150 {
1151 args->m_error.SetErrorToGenericError();
1152 args->m_error.SetErrorString("Process fork failed.");
1153 goto FINISH;
1154 }
1155
Peter Collingbourne6a520222011-06-14 03:55:58 +00001156 // Recognized child exit status codes.
1157 enum {
1158 ePtraceFailed = 1,
1159 eDupStdinFailed,
1160 eDupStdoutFailed,
1161 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001162 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001163 eExecFailed,
1164 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001165 };
1166
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167 // Child process.
1168 if (pid == 0)
1169 {
1170 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001171 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001172 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001173
1174 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001175 if (setgid(getgid()) != 0)
1176 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001177
1178 // Let us have our own process group.
1179 setpgid(0, 0);
1180
Greg Clayton710dd5a2011-01-08 20:28:42 +00001181 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001182 //
1183 // FIXME: If two or more of the paths are the same we needlessly open
1184 // the same file multiple times.
1185 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001186 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001187 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001188
1189 if (stdout_path != NULL && stdout_path[0])
1190 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001191 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192
1193 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001194 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001195 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001196
Daniel Malea6217d2a2013-01-08 14:49:22 +00001197 // Change working directory
1198 if (working_dir != NULL && working_dir[0])
1199 if (0 != ::chdir(working_dir))
1200 exit(eChdirFailed);
1201
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001202 // Execute. We should never return.
1203 execve(argv[0],
1204 const_cast<char *const *>(argv),
1205 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001206 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 }
1208
1209 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001210 lldb::pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001211 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001212 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001213 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001214 args->m_error.SetErrorToErrno();
1215 goto FINISH;
1216 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001217 else if (WIFEXITED(status))
1218 {
1219 // open, dup or execve likely failed for some reason.
1220 args->m_error.SetErrorToGenericError();
1221 switch (WEXITSTATUS(status))
1222 {
Greg Clayton542e4072012-09-07 17:49:29 +00001223 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001224 args->m_error.SetErrorString("Child ptrace failed.");
1225 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001226 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001227 args->m_error.SetErrorString("Child open stdin failed.");
1228 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001229 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001230 args->m_error.SetErrorString("Child open stdout failed.");
1231 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001232 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001233 args->m_error.SetErrorString("Child open stderr failed.");
1234 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001235 case eChdirFailed:
1236 args->m_error.SetErrorString("Child failed to set working directory.");
1237 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001238 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001239 args->m_error.SetErrorString("Child exec failed.");
1240 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001241 case eSetGidFailed:
1242 args->m_error.SetErrorString("Child setgid failed.");
1243 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001244 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001245 args->m_error.SetErrorString("Child returned unknown exit status.");
1246 break;
1247 }
1248 goto FINISH;
1249 }
1250 assert(WIFSTOPPED(status) && wpid == pid &&
1251 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001252
Matt Kopec085d6ce2013-05-31 22:00:07 +00001253 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001254 {
1255 args->m_error.SetErrorToErrno();
1256 goto FINISH;
1257 }
1258
1259 // Release the master terminal descriptor and pass it off to the
1260 // ProcessMonitor instance. Similarly stash the inferior pid.
1261 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1262 monitor->m_pid = pid;
1263
Stephen Wilson26977162011-03-23 02:14:42 +00001264 // Set the terminal fd to be in non blocking mode (it simplifies the
1265 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1266 // descriptor to read from).
1267 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1268 goto FINISH;
1269
Johnny Chen30213ff2012-01-05 19:17:38 +00001270 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001271 // FIXME: should we be letting UpdateThreadList handle this?
1272 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001273 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001274
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001275 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001276 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001277 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001278
Matt Kopecb2910442013-07-09 15:09:45 +00001279 process.AddThreadForInitialStopIfNeeded(pid);
1280
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001281 // Let our process instance know the thread has stopped.
1282 process.SendMessage(ProcessMessage::Trace(pid));
1283
1284FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001285 return args->m_error.Success();
1286}
1287
Johnny Chen25e68e32011-06-14 19:19:50 +00001288void
1289ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1290{
1291 static const char *g_thread_name = "lldb.process.linux.operation";
1292
1293 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1294 return;
1295
1296 m_operation_thread =
1297 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1298}
1299
Johnny Chen25e68e32011-06-14 19:19:50 +00001300void *
1301ProcessMonitor::AttachOpThread(void *arg)
1302{
1303 AttachArgs *args = static_cast<AttachArgs*>(arg);
1304
Greg Clayton743ecf42012-10-16 20:20:18 +00001305 if (!Attach(args)) {
1306 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001307 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001308 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001309
1310 ServeOperation(args);
1311 return NULL;
1312}
1313
1314bool
1315ProcessMonitor::Attach(AttachArgs *args)
1316{
1317 lldb::pid_t pid = args->m_pid;
1318
1319 ProcessMonitor *monitor = args->m_monitor;
1320 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001321 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001322 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001323
Matt Kopec085d6ce2013-05-31 22:00:07 +00001324 // Use a map to keep track of the threads which we have attached/need to attach.
1325 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001326 if (pid <= 1)
1327 {
1328 args->m_error.SetErrorToGenericError();
1329 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1330 goto FINISH;
1331 }
1332
Matt Kopec085d6ce2013-05-31 22:00:07 +00001333 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001334 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001335 for (Host::TidMap::iterator it = tids_to_attach.begin();
1336 it != tids_to_attach.end(); ++it)
1337 {
1338 if (it->second == false)
1339 {
1340 lldb::tid_t tid = it->first;
1341
1342 // Attach to the requested process.
1343 // An attach will cause the thread to stop with a SIGSTOP.
1344 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1345 {
1346 // No such thread. The thread may have exited.
1347 // More error handling may be needed.
1348 if (errno == ESRCH)
1349 {
1350 tids_to_attach.erase(it);
1351 continue;
1352 }
1353 else
1354 {
1355 args->m_error.SetErrorToErrno();
1356 goto FINISH;
1357 }
1358 }
1359
1360 int status;
1361 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1362 // At this point we should have a thread stopped if waitpid succeeds.
1363 if ((status = waitpid(tid, NULL, __WALL)) < 0)
1364 {
1365 // No such thread. The thread may have exited.
1366 // More error handling may be needed.
1367 if (errno == ESRCH)
1368 {
1369 tids_to_attach.erase(it);
1370 continue;
1371 }
1372 else
1373 {
1374 args->m_error.SetErrorToErrno();
1375 goto FINISH;
1376 }
1377 }
1378
1379 if (!SetDefaultPtraceOpts(tid))
1380 {
1381 args->m_error.SetErrorToErrno();
1382 goto FINISH;
1383 }
1384
1385 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001386 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001387
Matt Kopec085d6ce2013-05-31 22:00:07 +00001388 if (log)
1389 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1390 process.GetThreadList().AddThread(inferior);
1391 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001392 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001393 }
1394 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001395 }
1396
Matt Kopec085d6ce2013-05-31 22:00:07 +00001397 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001398 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001399 monitor->m_pid = pid;
1400 // Let our process instance know the thread has stopped.
1401 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001402 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001403 else
1404 {
1405 args->m_error.SetErrorToGenericError();
1406 args->m_error.SetErrorString("No such process.");
1407 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001408
1409 FINISH:
1410 return args->m_error.Success();
1411}
1412
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001413bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001414ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1415{
1416 long ptrace_opts = 0;
1417
1418 // Have the child raise an event on exit. This is used to keep the child in
1419 // limbo until it is destroyed.
1420 ptrace_opts |= PTRACE_O_TRACEEXIT;
1421
1422 // Have the tracer trace threads which spawn in the inferior process.
1423 // TODO: if we want to support tracing the inferiors' child, add the
1424 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1425 ptrace_opts |= PTRACE_O_TRACECLONE;
1426
1427 // Have the tracer notify us before execve returns
1428 // (needed to disable legacy SIGTRAP generation)
1429 ptrace_opts |= PTRACE_O_TRACEEXEC;
1430
1431 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1432}
1433
1434bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001435ProcessMonitor::MonitorCallback(void *callback_baton,
1436 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001437 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001438 int signal,
1439 int status)
1440{
1441 ProcessMessage message;
1442 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001443 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001444 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001445 bool stop_monitoring;
1446 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001447 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001448
Andrew Kaylor93132f52013-05-28 23:04:25 +00001449 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1450
1451 if (exited)
1452 {
1453 if (log)
1454 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1455 message = ProcessMessage::Exit(pid, status);
1456 process->SendMessage(message);
1457 return pid == process->GetID();
1458 }
1459
Daniel Maleaa35970a2012-11-23 18:09:58 +00001460 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1461 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001462 if (log)
1463 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001464 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1465 if (!monitor->Resume(pid, SIGSTOP)) {
1466 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1467 }
1468 stop_monitoring = false;
1469 } else {
1470 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1471 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001472 // stop the monitor thread if this is the main pid.
1473 if (log)
1474 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1475 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1476 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001477 // If we are going to stop monitoring, we need to notify our process object
1478 if (stop_monitoring)
1479 {
1480 message = ProcessMessage::Exit(pid, status);
1481 process->SendMessage(message);
1482 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001483 }
1484 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001485 else {
1486 switch (info.si_signo)
1487 {
1488 case SIGTRAP:
1489 message = MonitorSIGTRAP(monitor, &info, pid);
1490 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001491
Stephen Wilson84ffe702011-03-30 15:55:52 +00001492 default:
1493 message = MonitorSignal(monitor, &info, pid);
1494 break;
1495 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001496
Stephen Wilson84ffe702011-03-30 15:55:52 +00001497 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001498 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001499 }
1500
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001501 return stop_monitoring;
1502}
1503
1504ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001505ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001506 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001507{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001508 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001509
Andrew Kaylor93132f52013-05-28 23:04:25 +00001510 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1511
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001512 assert(monitor);
1513 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001514
Stephen Wilson84ffe702011-03-30 15:55:52 +00001515 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001516 {
1517 default:
1518 assert(false && "Unexpected SIGTRAP code!");
1519 break;
1520
Matt Kopeca360d7e2013-05-17 19:27:47 +00001521 // TODO: these two cases are required if we want to support tracing
1522 // of the inferiors' children
1523 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1524 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1525
Matt Kopec650648f2013-01-08 16:30:18 +00001526 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1527 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001528 if (log)
1529 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1530
Matt Kopec650648f2013-01-08 16:30:18 +00001531 unsigned long tid = 0;
1532 if (!monitor->GetEventMessage(pid, &tid))
1533 tid = -1;
1534 message = ProcessMessage::NewThread(pid, tid);
1535 break;
1536 }
1537
Matt Kopeca360d7e2013-05-17 19:27:47 +00001538 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001539 if (log)
1540 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1541
1542 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001543 break;
1544
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001545 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1546 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001547 // The inferior process or one of its threads is about to exit.
1548 // Maintain the process or thread in a state of "limbo" until we are
1549 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001550 unsigned long data = 0;
1551 if (!monitor->GetEventMessage(pid, &data))
1552 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001553 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001554 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001555 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001556 break;
1557 }
1558
1559 case 0:
1560 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001561 if (log)
1562 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001563 message = ProcessMessage::Trace(pid);
1564 break;
1565
1566 case SI_KERNEL:
1567 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001568 if (log)
1569 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001570 message = ProcessMessage::Break(pid);
1571 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001572
1573 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001574 if (log)
1575 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001576 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1577 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001578
1579 case SIGTRAP:
1580 case (SIGTRAP | 0x80):
1581 if (log)
1582 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1583 // Ignore these signals until we know more about them
1584 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001585 }
1586
1587 return message;
1588}
1589
Stephen Wilson84ffe702011-03-30 15:55:52 +00001590ProcessMessage
1591ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001592 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001593{
1594 ProcessMessage message;
1595 int signo = info->si_signo;
1596
Andrew Kaylor93132f52013-05-28 23:04:25 +00001597 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1598
Stephen Wilson84ffe702011-03-30 15:55:52 +00001599 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1600 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1601 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1602 //
1603 // IOW, user generated signals never generate what we consider to be a
1604 // "crash".
1605 //
1606 // Similarly, ACK signals generated by this monitor.
1607 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1608 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001609 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001610 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001611 __FUNCTION__,
1612 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1613 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1614 info->si_pid);
1615
Stephen Wilson84ffe702011-03-30 15:55:52 +00001616 if (info->si_pid == getpid())
1617 return ProcessMessage::SignalDelivered(pid, signo);
1618 else
1619 return ProcessMessage::Signal(pid, signo);
1620 }
1621
Andrew Kaylor93132f52013-05-28 23:04:25 +00001622 if (log)
1623 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1624
Stephen Wilson84ffe702011-03-30 15:55:52 +00001625 if (signo == SIGSEGV) {
1626 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1627 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1628 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1629 }
1630
1631 if (signo == SIGILL) {
1632 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1633 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1634 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1635 }
1636
1637 if (signo == SIGFPE) {
1638 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1639 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1640 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1641 }
1642
1643 if (signo == SIGBUS) {
1644 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1645 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1646 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1647 }
1648
1649 // Everything else is "normal" and does not require any special action on
1650 // our part.
1651 return ProcessMessage::Signal(pid, signo);
1652}
1653
Andrew Kaylord4d54992013-09-17 00:30:24 +00001654// On Linux, when a new thread is created, we receive to notifications,
1655// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1656// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1657// the new child thread indicating that it has is stopped because we attached.
1658// We have no guarantee of the order in which these arrive, but we need both
1659// before we are ready to proceed. We currently keep a list of threads which
1660// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1661// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1662// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1663
1664bool
1665ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1666{
1667 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1668 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001669 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001670
1671 // Wait for the thread to stop
1672 while (true)
1673 {
1674 int status = -1;
1675 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001676 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001677 lldb::pid_t wait_pid = waitpid(tid, &status, __WALL);
1678 if (status == -1)
1679 {
1680 // If we got interrupted by a signal (in our process, not the
1681 // inferior) try again.
1682 if (errno == EINTR)
1683 continue;
1684 else
1685 {
1686 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001687 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001688 return false; // This is bad, but there's nothing we can do.
1689 }
1690 }
1691
1692 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001693 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001694
1695 assert(wait_pid == tid);
1696
1697 siginfo_t info;
1698 int ptrace_err;
1699 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1700 {
1701 if (log)
1702 {
1703 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1704 }
1705 return false;
1706 }
1707
1708 // If this is a thread exit, we won't get any more information.
1709 if (WIFEXITED(status))
1710 {
1711 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1712 if (wait_pid == tid)
1713 return true;
1714 continue;
1715 }
1716
1717 assert(info.si_code == SI_USER);
1718 assert(WSTOPSIG(status) == SIGSTOP);
1719
1720 if (log)
1721 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1722 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1723 return true;
1724 }
1725 return false;
1726}
1727
Andrew Kaylor93132f52013-05-28 23:04:25 +00001728bool
1729ProcessMonitor::StopThread(lldb::tid_t tid)
1730{
1731 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1732
1733 // FIXME: Try to use tgkill or tkill
1734 int ret = tgkill(m_pid, tid, SIGSTOP);
1735 if (log)
1736 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1737
1738 // This can happen if a thread exited while we were trying to stop it. That's OK.
1739 // We'll get the signal for that later.
1740 if (ret < 0)
1741 return false;
1742
1743 // Wait for the thread to stop
1744 while (true)
1745 {
1746 int status = -1;
1747 if (log)
1748 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Andrew MacPherson82aae0d2014-04-02 06:57:45 +00001749 lldb::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001750 if (log)
1751 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d", __FUNCTION__, wait_pid, status);
1752
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001753 if (wait_pid == static_cast<lldb::pid_t>(-1))
Andrew Kaylor93132f52013-05-28 23:04:25 +00001754 {
1755 // If we got interrupted by a signal (in our process, not the
1756 // inferior) try again.
1757 if (errno == EINTR)
1758 continue;
1759 else
1760 return false; // This is bad, but there's nothing we can do.
1761 }
1762
1763 // If this is a thread exit, we won't get any more information.
1764 if (WIFEXITED(status))
1765 {
1766 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1767 if (wait_pid == tid)
1768 return true;
1769 continue;
1770 }
1771
1772 siginfo_t info;
1773 int ptrace_err;
1774 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1775 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001776 // another signal causing a StopAllThreads may have been received
1777 // before wait_pid's group-stop was processed, handle it now
1778 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001779 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001780 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001781
Todd Fiala1b0539c2014-01-27 17:03:57 +00001782 if (log)
1783 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1784 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1785 if (!Resume(wait_pid, SIGSTOP)) {
1786 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1787 }
1788 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001789 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001790
1791 if (log)
1792 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001793 return false;
1794 }
1795
1796 // Handle events from other threads
1797 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001798 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64, __FUNCTION__, wait_pid);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001799
1800 ProcessMessage message;
1801 if (info.si_signo == SIGTRAP)
1802 message = MonitorSIGTRAP(this, &info, wait_pid);
1803 else
1804 message = MonitorSignal(this, &info, wait_pid);
1805
1806 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1807
1808 // When a new thread is created, we may get a SIGSTOP for the new thread
1809 // just before we get the SIGTRAP that we use to add the thread to our
1810 // process thread list. We don't need to worry about that signal here.
1811 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1812
1813 if (!thread)
1814 {
1815 m_process->SendMessage(message);
1816 continue;
1817 }
1818
1819 switch (message.GetKind())
1820 {
Michael Sartainc258b302013-09-18 15:32:06 +00001821 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001822 case ProcessMessage::eInvalidMessage:
1823 break;
1824
1825 // These need special handling because we don't want to send a
1826 // resume even if we already sent a SIGSTOP to this thread. In
1827 // this case the resume will cause the thread to disappear. It is
1828 // unlikely that we'll ever get eExitMessage here, but the same
1829 // reasoning applies.
1830 case ProcessMessage::eLimboMessage:
1831 case ProcessMessage::eExitMessage:
1832 if (log)
1833 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1834 // SendMessage will set the thread state as needed.
1835 m_process->SendMessage(message);
1836 // If this is the thread we're waiting for, stop waiting. Even
1837 // though this wasn't the signal we expected, it's the last
1838 // signal we'll see while this thread is alive.
1839 if (wait_pid == tid)
1840 return true;
1841 break;
1842
Matt Kopecb2910442013-07-09 15:09:45 +00001843 case ProcessMessage::eSignalMessage:
1844 if (log)
1845 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1846 if (WSTOPSIG(status) == SIGSTOP)
1847 {
1848 m_process->AddThreadForInitialStopIfNeeded(tid);
1849 thread->SetState(lldb::eStateStopped);
1850 }
1851 else
1852 {
1853 m_process->SendMessage(message);
1854 // This isn't the stop we were expecting, but the thread is
1855 // stopped. SendMessage will handle processing of this event,
1856 // but we need to resume here to get the stop we are waiting
1857 // for (otherwise the thread will stop again immediately when
1858 // we try to resume).
1859 if (wait_pid == tid)
1860 Resume(wait_pid, eResumeSignalNone);
1861 }
1862 break;
1863
Andrew Kaylor93132f52013-05-28 23:04:25 +00001864 case ProcessMessage::eSignalDeliveredMessage:
1865 // This is the stop we're expecting.
1866 if (wait_pid == tid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && info.si_code == SI_TKILL)
1867 {
1868 if (log)
1869 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1870 thread->SetState(lldb::eStateStopped);
1871 return true;
1872 }
1873 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001874 case ProcessMessage::eBreakpointMessage:
1875 case ProcessMessage::eTraceMessage:
1876 case ProcessMessage::eWatchpointMessage:
1877 case ProcessMessage::eCrashMessage:
1878 case ProcessMessage::eNewThreadMessage:
1879 if (log)
1880 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1881 // SendMessage will set the thread state as needed.
1882 m_process->SendMessage(message);
1883 // This isn't the stop we were expecting, but the thread is
1884 // stopped. SendMessage will handle processing of this event,
1885 // but we need to resume here to get the stop we are waiting
1886 // for (otherwise the thread will stop again immediately when
1887 // we try to resume).
1888 if (wait_pid == tid)
1889 Resume(wait_pid, eResumeSignalNone);
1890 break;
1891 }
1892 }
1893 return false;
1894}
1895
Stephen Wilson84ffe702011-03-30 15:55:52 +00001896ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001897ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001898{
1899 ProcessMessage::CrashReason reason;
1900 assert(info->si_signo == SIGSEGV);
1901
1902 reason = ProcessMessage::eInvalidCrashReason;
1903
Greg Clayton542e4072012-09-07 17:49:29 +00001904 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001905 {
1906 default:
1907 assert(false && "unexpected si_code for SIGSEGV");
1908 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001909 case SI_KERNEL:
1910 // Linux will occasionally send spurious SI_KERNEL codes.
1911 // (this is poorly documented in sigaction)
1912 // One way to get this is via unaligned SIMD loads.
1913 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1914 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001915 case SEGV_MAPERR:
1916 reason = ProcessMessage::eInvalidAddress;
1917 break;
1918 case SEGV_ACCERR:
1919 reason = ProcessMessage::ePrivilegedAddress;
1920 break;
1921 }
Greg Clayton542e4072012-09-07 17:49:29 +00001922
Stephen Wilson84ffe702011-03-30 15:55:52 +00001923 return reason;
1924}
1925
1926ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001927ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001928{
1929 ProcessMessage::CrashReason reason;
1930 assert(info->si_signo == SIGILL);
1931
1932 reason = ProcessMessage::eInvalidCrashReason;
1933
1934 switch (info->si_code)
1935 {
1936 default:
1937 assert(false && "unexpected si_code for SIGILL");
1938 break;
1939 case ILL_ILLOPC:
1940 reason = ProcessMessage::eIllegalOpcode;
1941 break;
1942 case ILL_ILLOPN:
1943 reason = ProcessMessage::eIllegalOperand;
1944 break;
1945 case ILL_ILLADR:
1946 reason = ProcessMessage::eIllegalAddressingMode;
1947 break;
1948 case ILL_ILLTRP:
1949 reason = ProcessMessage::eIllegalTrap;
1950 break;
1951 case ILL_PRVOPC:
1952 reason = ProcessMessage::ePrivilegedOpcode;
1953 break;
1954 case ILL_PRVREG:
1955 reason = ProcessMessage::ePrivilegedRegister;
1956 break;
1957 case ILL_COPROC:
1958 reason = ProcessMessage::eCoprocessorError;
1959 break;
1960 case ILL_BADSTK:
1961 reason = ProcessMessage::eInternalStackError;
1962 break;
1963 }
1964
1965 return reason;
1966}
1967
1968ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001969ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001970{
1971 ProcessMessage::CrashReason reason;
1972 assert(info->si_signo == SIGFPE);
1973
1974 reason = ProcessMessage::eInvalidCrashReason;
1975
1976 switch (info->si_code)
1977 {
1978 default:
1979 assert(false && "unexpected si_code for SIGFPE");
1980 break;
1981 case FPE_INTDIV:
1982 reason = ProcessMessage::eIntegerDivideByZero;
1983 break;
1984 case FPE_INTOVF:
1985 reason = ProcessMessage::eIntegerOverflow;
1986 break;
1987 case FPE_FLTDIV:
1988 reason = ProcessMessage::eFloatDivideByZero;
1989 break;
1990 case FPE_FLTOVF:
1991 reason = ProcessMessage::eFloatOverflow;
1992 break;
1993 case FPE_FLTUND:
1994 reason = ProcessMessage::eFloatUnderflow;
1995 break;
1996 case FPE_FLTRES:
1997 reason = ProcessMessage::eFloatInexactResult;
1998 break;
1999 case FPE_FLTINV:
2000 reason = ProcessMessage::eFloatInvalidOperation;
2001 break;
2002 case FPE_FLTSUB:
2003 reason = ProcessMessage::eFloatSubscriptRange;
2004 break;
2005 }
2006
2007 return reason;
2008}
2009
2010ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002011ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002012{
2013 ProcessMessage::CrashReason reason;
2014 assert(info->si_signo == SIGBUS);
2015
2016 reason = ProcessMessage::eInvalidCrashReason;
2017
2018 switch (info->si_code)
2019 {
2020 default:
2021 assert(false && "unexpected si_code for SIGBUS");
2022 break;
2023 case BUS_ADRALN:
2024 reason = ProcessMessage::eIllegalAlignment;
2025 break;
2026 case BUS_ADRERR:
2027 reason = ProcessMessage::eIllegalAddress;
2028 break;
2029 case BUS_OBJERR:
2030 reason = ProcessMessage::eHardwareError;
2031 break;
2032 }
2033
2034 return reason;
2035}
2036
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002037void
Johnny Chen25e68e32011-06-14 19:19:50 +00002038ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002039{
Stephen Wilson570243b2011-01-19 01:37:06 +00002040 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002041
Stephen Wilson570243b2011-01-19 01:37:06 +00002042 // We are finised with the arguments and are ready to go. Sync with the
2043 // parent thread and start serving operations on the inferior.
2044 sem_post(&args->m_semaphore);
2045
Michael Sartain704bf892013-10-09 01:28:57 +00002046 for(;;)
2047 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002048 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002049 if (sem_wait(&monitor->m_operation_pending))
2050 {
2051 if (errno == EINTR)
2052 continue;
2053 assert(false && "Unexpected errno from sem_wait");
2054 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002055
Daniel Malea1efb4182013-09-16 23:12:18 +00002056 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002057
Daniel Malea1efb4182013-09-16 23:12:18 +00002058 // notify calling thread that operation is complete
2059 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002060 }
2061}
2062
2063void
2064ProcessMonitor::DoOperation(Operation *op)
2065{
Daniel Malea1efb4182013-09-16 23:12:18 +00002066 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002067
Daniel Malea1efb4182013-09-16 23:12:18 +00002068 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002069
Daniel Malea1efb4182013-09-16 23:12:18 +00002070 // notify operation thread that an operation is ready to be processed
2071 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002072
Daniel Malea1efb4182013-09-16 23:12:18 +00002073 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002074 while (sem_wait(&m_operation_done))
2075 {
2076 if (errno == EINTR)
2077 continue;
2078 assert(false && "Unexpected errno from sem_wait");
2079 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002080}
2081
2082size_t
2083ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2084 Error &error)
2085{
2086 size_t result;
2087 ReadOperation op(vm_addr, buf, size, error, result);
2088 DoOperation(&op);
2089 return result;
2090}
2091
2092size_t
2093ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2094 lldb_private::Error &error)
2095{
2096 size_t result;
2097 WriteOperation op(vm_addr, buf, size, error, result);
2098 DoOperation(&op);
2099 return result;
2100}
2101
2102bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002103ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002104 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002105{
2106 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002107 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002108 DoOperation(&op);
2109 return result;
2110}
2111
2112bool
Matt Kopec7de48462013-03-06 17:20:48 +00002113ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002114 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002115{
2116 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002117 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002118 DoOperation(&op);
2119 return result;
2120}
2121
2122bool
Matt Kopec7de48462013-03-06 17:20:48 +00002123ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002124{
2125 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002126 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002127 DoOperation(&op);
2128 return result;
2129}
2130
2131bool
Matt Kopec7de48462013-03-06 17:20:48 +00002132ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002133{
2134 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002135 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002136 DoOperation(&op);
2137 return result;
2138}
2139
2140bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002141ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2142{
2143 bool result;
2144 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2145 DoOperation(&op);
2146 return result;
2147}
2148
2149bool
Matt Kopec7de48462013-03-06 17:20:48 +00002150ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002151{
2152 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002153 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002154 DoOperation(&op);
2155 return result;
2156}
2157
2158bool
Matt Kopec7de48462013-03-06 17:20:48 +00002159ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002160{
2161 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002162 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002163 DoOperation(&op);
2164 return result;
2165}
2166
2167bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002168ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2169{
2170 bool result;
2171 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2172 DoOperation(&op);
2173 return result;
2174}
2175
2176bool
Richard Mitton0a558352013-10-17 21:14:00 +00002177ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2178{
2179 bool result;
2180 ReadThreadPointerOperation op(tid, &value, result);
2181 DoOperation(&op);
2182 return result;
2183}
2184
2185bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002186ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002187{
2188 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002189 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2190
2191 if (log)
2192 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2193 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002194 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002195 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002196 if (log)
2197 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002198 return result;
2199}
2200
2201bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002202ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002203{
2204 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002205 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002206 DoOperation(&op);
2207 return result;
2208}
2209
2210bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002211ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002212{
Ed Maste4e0999b2014-04-01 18:14:06 +00002213 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002214}
2215
2216bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002217ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002218{
2219 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002220 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002221 DoOperation(&op);
2222 return result;
2223}
2224
2225bool
2226ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2227{
2228 bool result;
2229 EventMessageOperation op(tid, message, result);
2230 DoOperation(&op);
2231 return result;
2232}
2233
Greg Clayton743ecf42012-10-16 20:20:18 +00002234lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002235ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002236{
Greg Clayton28041352011-11-29 20:50:10 +00002237 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002238 if (tid != LLDB_INVALID_THREAD_ID)
2239 {
2240 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002241 DoOperation(&op);
2242 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002243 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002244}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002245
2246bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002247ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2248{
Peter Collingbourne62343202011-06-14 03:55:54 +00002249 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002250
2251 if (target_fd == -1)
2252 return false;
2253
Peter Collingbourne62343202011-06-14 03:55:54 +00002254 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002255}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002256
2257void
2258ProcessMonitor::StopMonitoringChildProcess()
2259{
2260 lldb::thread_result_t thread_result;
2261
Stephen Wilsond4182f42011-02-09 20:10:35 +00002262 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002263 {
2264 Host::ThreadCancel(m_monitor_thread, NULL);
2265 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2266 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2267 }
2268}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002269
2270void
2271ProcessMonitor::StopMonitor()
2272{
2273 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002274 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002275 sem_destroy(&m_operation_pending);
2276 sem_destroy(&m_operation_done);
2277
Andrew Kaylor5e268992013-09-14 00:17:31 +00002278 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2279 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2280 // the descriptor to a ConnectionFileDescriptor object. Consequently
2281 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002282}
2283
2284void
Greg Clayton743ecf42012-10-16 20:20:18 +00002285ProcessMonitor::StopOpThread()
2286{
2287 lldb::thread_result_t result;
2288
2289 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2290 return;
2291
2292 Host::ThreadCancel(m_operation_thread, NULL);
2293 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002294 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002295}