blob: 36795e584cfaf8def671c80febf0f8dfaf115757 [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#if __WORDSIZE == 32
536 buf = (void*) m_value.GetAsUInt32();
537#else
538 buf = (void*) m_value.GetAsUInt64();
539#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000540
541 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000542 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000543 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000544 m_result = false;
545 else
546 m_result = true;
547}
548
549//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000550/// @class ReadGPROperation
551/// @brief Implements ProcessMonitor::ReadGPR.
552class ReadGPROperation : public Operation
553{
554public:
Matt Kopec7de48462013-03-06 17:20:48 +0000555 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
556 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000557 { }
558
559 void Execute(ProcessMonitor *monitor);
560
561private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000562 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000563 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000564 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000565 bool &m_result;
566};
567
568void
569ReadGPROperation::Execute(ProcessMonitor *monitor)
570{
Matt Kopec7de48462013-03-06 17:20:48 +0000571 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000572 m_result = false;
573 else
574 m_result = true;
575}
576
577//------------------------------------------------------------------------------
578/// @class ReadFPROperation
579/// @brief Implements ProcessMonitor::ReadFPR.
580class ReadFPROperation : public Operation
581{
582public:
Matt Kopec7de48462013-03-06 17:20:48 +0000583 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
584 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000585 { }
586
587 void Execute(ProcessMonitor *monitor);
588
589private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000590 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000591 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000592 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000593 bool &m_result;
594};
595
596void
597ReadFPROperation::Execute(ProcessMonitor *monitor)
598{
Matt Kopec7de48462013-03-06 17:20:48 +0000599 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000600 m_result = false;
601 else
602 m_result = true;
603}
604
605//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000606/// @class ReadRegisterSetOperation
607/// @brief Implements ProcessMonitor::ReadRegisterSet.
608class ReadRegisterSetOperation : public Operation
609{
610public:
611 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
612 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
613 { }
614
615 void Execute(ProcessMonitor *monitor);
616
617private:
618 lldb::tid_t m_tid;
619 void *m_buf;
620 size_t m_buf_size;
621 const unsigned int m_regset;
622 bool &m_result;
623};
624
625void
626ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
627{
628 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
629 m_result = false;
630 else
631 m_result = true;
632}
633
634//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000635/// @class WriteGPROperation
636/// @brief Implements ProcessMonitor::WriteGPR.
637class WriteGPROperation : public Operation
638{
639public:
Matt Kopec7de48462013-03-06 17:20:48 +0000640 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
641 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000642 { }
643
644 void Execute(ProcessMonitor *monitor);
645
646private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000647 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000648 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000649 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000650 bool &m_result;
651};
652
653void
654WriteGPROperation::Execute(ProcessMonitor *monitor)
655{
Matt Kopec7de48462013-03-06 17:20:48 +0000656 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000657 m_result = false;
658 else
659 m_result = true;
660}
661
662//------------------------------------------------------------------------------
663/// @class WriteFPROperation
664/// @brief Implements ProcessMonitor::WriteFPR.
665class WriteFPROperation : public Operation
666{
667public:
Matt Kopec7de48462013-03-06 17:20:48 +0000668 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
669 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000670 { }
671
672 void Execute(ProcessMonitor *monitor);
673
674private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000675 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000676 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000677 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000678 bool &m_result;
679};
680
681void
682WriteFPROperation::Execute(ProcessMonitor *monitor)
683{
Matt Kopec7de48462013-03-06 17:20:48 +0000684 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000685 m_result = false;
686 else
687 m_result = true;
688}
689
690//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000691/// @class WriteRegisterSetOperation
692/// @brief Implements ProcessMonitor::WriteRegisterSet.
693class WriteRegisterSetOperation : public Operation
694{
695public:
696 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
697 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
698 { }
699
700 void Execute(ProcessMonitor *monitor);
701
702private:
703 lldb::tid_t m_tid;
704 void *m_buf;
705 size_t m_buf_size;
706 const unsigned int m_regset;
707 bool &m_result;
708};
709
710void
711WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
712{
713 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
714 m_result = false;
715 else
716 m_result = true;
717}
718
719//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000720/// @class ReadThreadPointerOperation
721/// @brief Implements ProcessMonitor::ReadThreadPointer.
722class ReadThreadPointerOperation : public Operation
723{
724public:
725 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
726 : m_tid(tid), m_addr(addr), m_result(result)
727 { }
728
729 void Execute(ProcessMonitor *monitor);
730
731private:
732 lldb::tid_t m_tid;
733 lldb::addr_t *m_addr;
734 bool &m_result;
735};
736
737void
738ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
739{
740 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
741 if (log)
742 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
743
744 // The process for getting the thread area on Linux is
745 // somewhat... obscure. There's several different ways depending on
746 // what arch you're on, and what kernel version you have.
747
748 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
749 switch(arch.GetMachine())
750 {
751 case llvm::Triple::x86:
752 {
753 // Find the GS register location for our host architecture.
754 size_t gs_user_offset = offsetof(struct user, regs);
755#ifdef __x86_64__
756 gs_user_offset += offsetof(struct user_regs_struct, gs);
757#endif
758#ifdef __i386__
759 gs_user_offset += offsetof(struct user_regs_struct, xgs);
760#endif
761
762 // Read the GS register value to get the selector.
763 errno = 0;
764 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
765 if (errno)
766 {
767 m_result = false;
768 break;
769 }
770
771 // Read the LDT base for that selector.
772 uint32_t tmp[4];
773 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
774 *m_addr = tmp[1];
775 break;
776 }
777 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//------------------------------------------------------------------------------
911/// @class KillOperation
912/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
913class KillOperation : public Operation
914{
915public:
916 KillOperation(bool &result) : m_result(result) { }
917
918 void Execute(ProcessMonitor *monitor);
919
920private:
921 bool &m_result;
922};
923
924void
925KillOperation::Execute(ProcessMonitor *monitor)
926{
927 lldb::pid_t pid = monitor->GetPID();
928
Matt Kopec7de48462013-03-06 17:20:48 +0000929 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000930 m_result = false;
931 else
932 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000933}
934
Greg Clayton28041352011-11-29 20:50:10 +0000935//------------------------------------------------------------------------------
936/// @class KillOperation
937/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
938class DetachOperation : public Operation
939{
940public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000941 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000942
943 void Execute(ProcessMonitor *monitor);
944
945private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000946 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000947 Error &m_error;
948};
949
950void
951DetachOperation::Execute(ProcessMonitor *monitor)
952{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000953 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000954 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000955}
956
Johnny Chen25e68e32011-06-14 19:19:50 +0000957ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
958 : m_monitor(monitor)
959{
960 sem_init(&m_semaphore, 0, 0);
961}
962
963ProcessMonitor::OperationArgs::~OperationArgs()
964{
965 sem_destroy(&m_semaphore);
966}
967
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
969 lldb_private::Module *module,
970 char const **argv,
971 char const **envp,
972 const char *stdin_path,
973 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000974 const char *stderr_path,
975 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000976 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000977 m_module(module),
978 m_argv(argv),
979 m_envp(envp),
980 m_stdin_path(stdin_path),
981 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000982 m_stderr_path(stderr_path),
983 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000984
985ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000986{ }
987
988ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
989 lldb::pid_t pid)
990 : OperationArgs(monitor), m_pid(pid) { }
991
992ProcessMonitor::AttachArgs::~AttachArgs()
993{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000994
995//------------------------------------------------------------------------------
996/// The basic design of the ProcessMonitor is built around two threads.
997///
998/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
999/// for changes in the debugee state. When a change is detected a
1000/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1001/// "drives" state changes in the debugger.
1002///
1003/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001004/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001005/// operations such as register reads/writes, stepping, etc. See the comments
1006/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001007ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001008 Module *module,
1009 const char *argv[],
1010 const char *envp[],
1011 const char *stdin_path,
1012 const char *stdout_path,
1013 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001014 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001015 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001016 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001018 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001019 m_pid(LLDB_INVALID_PROCESS_ID),
1020 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001021 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001022{
Daniel Malea1efb4182013-09-16 23:12:18 +00001023 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1024 stdin_path, stdout_path, stderr_path,
1025 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001026
Daniel Malea1efb4182013-09-16 23:12:18 +00001027 sem_init(&m_operation_pending, 0, 0);
1028 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001029
Johnny Chen25e68e32011-06-14 19:19:50 +00001030 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001031 if (!error.Success())
1032 return;
1033
1034WAIT_AGAIN:
1035 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001036 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001037 {
1038 if (errno == EINTR)
1039 goto WAIT_AGAIN;
1040 else
1041 {
1042 error.SetErrorToErrno();
1043 return;
1044 }
1045 }
1046
1047 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001048 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001049 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001050 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001051 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001052 return;
1053 }
1054
1055 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001056 m_monitor_thread = Host::StartMonitoringChildProcess(
1057 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001058 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001059 {
1060 error.SetErrorToGenericError();
1061 error.SetErrorString("Process launch failed.");
1062 return;
1063 }
1064}
1065
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001066ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001067 lldb::pid_t pid,
1068 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001069 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001070 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001071 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001072 m_pid(LLDB_INVALID_PROCESS_ID),
1073 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001074 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001075{
Daniel Malea1efb4182013-09-16 23:12:18 +00001076 sem_init(&m_operation_pending, 0, 0);
1077 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001078
Daniel Malea1efb4182013-09-16 23:12:18 +00001079 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001080
1081 StartAttachOpThread(args.get(), error);
1082 if (!error.Success())
1083 return;
1084
1085WAIT_AGAIN:
1086 // Wait for the operation thread to initialize.
1087 if (sem_wait(&args->m_semaphore))
1088 {
1089 if (errno == EINTR)
1090 goto WAIT_AGAIN;
1091 else
1092 {
1093 error.SetErrorToErrno();
1094 return;
1095 }
1096 }
1097
Greg Clayton743ecf42012-10-16 20:20:18 +00001098 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001099 if (!args->m_error.Success())
1100 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001101 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001102 error = args->m_error;
1103 return;
1104 }
1105
1106 // Finally, start monitoring the child process for change in state.
1107 m_monitor_thread = Host::StartMonitoringChildProcess(
1108 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1109 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1110 {
1111 error.SetErrorToGenericError();
1112 error.SetErrorString("Process attach failed.");
1113 return;
1114 }
1115}
1116
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001117ProcessMonitor::~ProcessMonitor()
1118{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001119 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001120}
1121
1122//------------------------------------------------------------------------------
1123// Thread setup and tear down.
1124void
Johnny Chen25e68e32011-06-14 19:19:50 +00001125ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001126{
1127 static const char *g_thread_name = "lldb.process.linux.operation";
1128
Stephen Wilsond4182f42011-02-09 20:10:35 +00001129 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130 return;
1131
1132 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001133 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001134}
1135
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001136void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001137ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138{
1139 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1140
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001141 if (!Launch(args)) {
1142 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001143 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001144 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001145
Stephen Wilson570243b2011-01-19 01:37:06 +00001146 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001147 return NULL;
1148}
1149
1150bool
1151ProcessMonitor::Launch(LaunchArgs *args)
1152{
1153 ProcessMonitor *monitor = args->m_monitor;
1154 ProcessLinux &process = monitor->GetProcess();
1155 const char **argv = args->m_argv;
1156 const char **envp = args->m_envp;
1157 const char *stdin_path = args->m_stdin_path;
1158 const char *stdout_path = args->m_stdout_path;
1159 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001160 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001161
1162 lldb_utility::PseudoTerminal terminal;
1163 const size_t err_len = 1024;
1164 char err_str[err_len];
1165 lldb::pid_t pid;
1166
1167 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001168 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169
Stephen Wilson57740ec2011-01-15 00:12:41 +00001170 // Propagate the environment if one is not supplied.
1171 if (envp == NULL || envp[0] == NULL)
1172 envp = const_cast<const char **>(environ);
1173
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001174 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001175 {
1176 args->m_error.SetErrorToGenericError();
1177 args->m_error.SetErrorString("Process fork failed.");
1178 goto FINISH;
1179 }
1180
Peter Collingbourne6a520222011-06-14 03:55:58 +00001181 // Recognized child exit status codes.
1182 enum {
1183 ePtraceFailed = 1,
1184 eDupStdinFailed,
1185 eDupStdoutFailed,
1186 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001187 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001188 eExecFailed,
1189 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001190 };
1191
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192 // Child process.
1193 if (pid == 0)
1194 {
1195 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001196 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001197 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001198
1199 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001200 if (setgid(getgid()) != 0)
1201 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001202
1203 // Let us have our own process group.
1204 setpgid(0, 0);
1205
Greg Clayton710dd5a2011-01-08 20:28:42 +00001206 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 //
1208 // FIXME: If two or more of the paths are the same we needlessly open
1209 // the same file multiple times.
1210 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001211 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001212 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001213
1214 if (stdout_path != NULL && stdout_path[0])
1215 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001216 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001217
1218 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001219 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001220 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001221
Daniel Malea6217d2a2013-01-08 14:49:22 +00001222 // Change working directory
1223 if (working_dir != NULL && working_dir[0])
1224 if (0 != ::chdir(working_dir))
1225 exit(eChdirFailed);
1226
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001227 // Execute. We should never return.
1228 execve(argv[0],
1229 const_cast<char *const *>(argv),
1230 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001231 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001232 }
1233
1234 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001235 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001236 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001237 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001238 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001239 args->m_error.SetErrorToErrno();
1240 goto FINISH;
1241 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001242 else if (WIFEXITED(status))
1243 {
1244 // open, dup or execve likely failed for some reason.
1245 args->m_error.SetErrorToGenericError();
1246 switch (WEXITSTATUS(status))
1247 {
Greg Clayton542e4072012-09-07 17:49:29 +00001248 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001249 args->m_error.SetErrorString("Child ptrace failed.");
1250 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001251 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001252 args->m_error.SetErrorString("Child open stdin failed.");
1253 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001254 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001255 args->m_error.SetErrorString("Child open stdout failed.");
1256 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001257 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001258 args->m_error.SetErrorString("Child open stderr failed.");
1259 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001260 case eChdirFailed:
1261 args->m_error.SetErrorString("Child failed to set working directory.");
1262 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001263 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001264 args->m_error.SetErrorString("Child exec failed.");
1265 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001266 case eSetGidFailed:
1267 args->m_error.SetErrorString("Child setgid failed.");
1268 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001269 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001270 args->m_error.SetErrorString("Child returned unknown exit status.");
1271 break;
1272 }
1273 goto FINISH;
1274 }
1275 assert(WIFSTOPPED(status) && wpid == pid &&
1276 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001277
Matt Kopec085d6ce2013-05-31 22:00:07 +00001278 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001279 {
1280 args->m_error.SetErrorToErrno();
1281 goto FINISH;
1282 }
1283
1284 // Release the master terminal descriptor and pass it off to the
1285 // ProcessMonitor instance. Similarly stash the inferior pid.
1286 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1287 monitor->m_pid = pid;
1288
Stephen Wilson26977162011-03-23 02:14:42 +00001289 // Set the terminal fd to be in non blocking mode (it simplifies the
1290 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1291 // descriptor to read from).
1292 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1293 goto FINISH;
1294
Johnny Chen30213ff2012-01-05 19:17:38 +00001295 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001296 // FIXME: should we be letting UpdateThreadList handle this?
1297 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001298 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001299
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001300 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001301 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001302 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001303
Matt Kopecb2910442013-07-09 15:09:45 +00001304 process.AddThreadForInitialStopIfNeeded(pid);
1305
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001306 // Let our process instance know the thread has stopped.
1307 process.SendMessage(ProcessMessage::Trace(pid));
1308
1309FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001310 return args->m_error.Success();
1311}
1312
Johnny Chen25e68e32011-06-14 19:19:50 +00001313void
1314ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1315{
1316 static const char *g_thread_name = "lldb.process.linux.operation";
1317
1318 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1319 return;
1320
1321 m_operation_thread =
1322 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1323}
1324
Johnny Chen25e68e32011-06-14 19:19:50 +00001325void *
1326ProcessMonitor::AttachOpThread(void *arg)
1327{
1328 AttachArgs *args = static_cast<AttachArgs*>(arg);
1329
Greg Clayton743ecf42012-10-16 20:20:18 +00001330 if (!Attach(args)) {
1331 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001332 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001333 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001334
1335 ServeOperation(args);
1336 return NULL;
1337}
1338
1339bool
1340ProcessMonitor::Attach(AttachArgs *args)
1341{
1342 lldb::pid_t pid = args->m_pid;
1343
1344 ProcessMonitor *monitor = args->m_monitor;
1345 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001346 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001347 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001348
Matt Kopec085d6ce2013-05-31 22:00:07 +00001349 // Use a map to keep track of the threads which we have attached/need to attach.
1350 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001351 if (pid <= 1)
1352 {
1353 args->m_error.SetErrorToGenericError();
1354 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1355 goto FINISH;
1356 }
1357
Matt Kopec085d6ce2013-05-31 22:00:07 +00001358 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001359 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001360 for (Host::TidMap::iterator it = tids_to_attach.begin();
1361 it != tids_to_attach.end(); ++it)
1362 {
1363 if (it->second == false)
1364 {
1365 lldb::tid_t tid = it->first;
1366
1367 // Attach to the requested process.
1368 // An attach will cause the thread to stop with a SIGSTOP.
1369 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1370 {
1371 // No such thread. The thread may have exited.
1372 // More error handling may be needed.
1373 if (errno == ESRCH)
1374 {
1375 tids_to_attach.erase(it);
1376 continue;
1377 }
1378 else
1379 {
1380 args->m_error.SetErrorToErrno();
1381 goto FINISH;
1382 }
1383 }
1384
1385 int status;
1386 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1387 // At this point we should have a thread stopped if waitpid succeeds.
1388 if ((status = waitpid(tid, NULL, __WALL)) < 0)
1389 {
1390 // No such thread. The thread may have exited.
1391 // More error handling may be needed.
1392 if (errno == ESRCH)
1393 {
1394 tids_to_attach.erase(it);
1395 continue;
1396 }
1397 else
1398 {
1399 args->m_error.SetErrorToErrno();
1400 goto FINISH;
1401 }
1402 }
1403
1404 if (!SetDefaultPtraceOpts(tid))
1405 {
1406 args->m_error.SetErrorToErrno();
1407 goto FINISH;
1408 }
1409
1410 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001411 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001412
Matt Kopec085d6ce2013-05-31 22:00:07 +00001413 if (log)
1414 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1415 process.GetThreadList().AddThread(inferior);
1416 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001417 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001418 }
1419 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001420 }
1421
Matt Kopec085d6ce2013-05-31 22:00:07 +00001422 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001423 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001424 monitor->m_pid = pid;
1425 // Let our process instance know the thread has stopped.
1426 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001427 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001428 else
1429 {
1430 args->m_error.SetErrorToGenericError();
1431 args->m_error.SetErrorString("No such process.");
1432 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001433
1434 FINISH:
1435 return args->m_error.Success();
1436}
1437
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001438bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001439ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1440{
1441 long ptrace_opts = 0;
1442
1443 // Have the child raise an event on exit. This is used to keep the child in
1444 // limbo until it is destroyed.
1445 ptrace_opts |= PTRACE_O_TRACEEXIT;
1446
1447 // Have the tracer trace threads which spawn in the inferior process.
1448 // TODO: if we want to support tracing the inferiors' child, add the
1449 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1450 ptrace_opts |= PTRACE_O_TRACECLONE;
1451
1452 // Have the tracer notify us before execve returns
1453 // (needed to disable legacy SIGTRAP generation)
1454 ptrace_opts |= PTRACE_O_TRACEEXEC;
1455
1456 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1457}
1458
1459bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001460ProcessMonitor::MonitorCallback(void *callback_baton,
1461 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001462 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001463 int signal,
1464 int status)
1465{
1466 ProcessMessage message;
1467 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001468 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001469 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001470 bool stop_monitoring;
1471 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001472 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001473
Andrew Kaylor93132f52013-05-28 23:04:25 +00001474 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1475
1476 if (exited)
1477 {
1478 if (log)
1479 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1480 message = ProcessMessage::Exit(pid, status);
1481 process->SendMessage(message);
1482 return pid == process->GetID();
1483 }
1484
Daniel Maleaa35970a2012-11-23 18:09:58 +00001485 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1486 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001487 if (log)
1488 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001489 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1490 if (!monitor->Resume(pid, SIGSTOP)) {
1491 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1492 }
1493 stop_monitoring = false;
1494 } else {
1495 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1496 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001497 // stop the monitor thread if this is the main pid.
1498 if (log)
1499 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1500 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1501 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001502 // If we are going to stop monitoring, we need to notify our process object
1503 if (stop_monitoring)
1504 {
1505 message = ProcessMessage::Exit(pid, status);
1506 process->SendMessage(message);
1507 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001508 }
1509 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001510 else {
1511 switch (info.si_signo)
1512 {
1513 case SIGTRAP:
1514 message = MonitorSIGTRAP(monitor, &info, pid);
1515 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001516
Stephen Wilson84ffe702011-03-30 15:55:52 +00001517 default:
1518 message = MonitorSignal(monitor, &info, pid);
1519 break;
1520 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001521
Stephen Wilson84ffe702011-03-30 15:55:52 +00001522 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001523 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001524 }
1525
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001526 return stop_monitoring;
1527}
1528
1529ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001530ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001531 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001532{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001533 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001534
Andrew Kaylor93132f52013-05-28 23:04:25 +00001535 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1536
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001537 assert(monitor);
1538 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001539
Stephen Wilson84ffe702011-03-30 15:55:52 +00001540 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001541 {
1542 default:
1543 assert(false && "Unexpected SIGTRAP code!");
1544 break;
1545
Matt Kopeca360d7e2013-05-17 19:27:47 +00001546 // TODO: these two cases are required if we want to support tracing
1547 // of the inferiors' children
1548 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1549 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1550
Matt Kopec650648f2013-01-08 16:30:18 +00001551 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1552 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001553 if (log)
1554 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1555
Matt Kopec650648f2013-01-08 16:30:18 +00001556 unsigned long tid = 0;
1557 if (!monitor->GetEventMessage(pid, &tid))
1558 tid = -1;
1559 message = ProcessMessage::NewThread(pid, tid);
1560 break;
1561 }
1562
Matt Kopeca360d7e2013-05-17 19:27:47 +00001563 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001564 if (log)
1565 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1566
1567 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001568 break;
1569
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001570 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1571 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001572 // The inferior process or one of its threads is about to exit.
1573 // Maintain the process or thread in a state of "limbo" until we are
1574 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001575 unsigned long data = 0;
1576 if (!monitor->GetEventMessage(pid, &data))
1577 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001578 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001579 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001580 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001581 break;
1582 }
1583
1584 case 0:
1585 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001586 if (log)
1587 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001588 message = ProcessMessage::Trace(pid);
1589 break;
1590
1591 case SI_KERNEL:
1592 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001593 if (log)
1594 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001595 message = ProcessMessage::Break(pid);
1596 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001597
1598 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001599 if (log)
1600 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001601 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1602 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001603
1604 case SIGTRAP:
1605 case (SIGTRAP | 0x80):
1606 if (log)
1607 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1608 // Ignore these signals until we know more about them
1609 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001610 }
1611
1612 return message;
1613}
1614
Stephen Wilson84ffe702011-03-30 15:55:52 +00001615ProcessMessage
1616ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001617 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001618{
1619 ProcessMessage message;
1620 int signo = info->si_signo;
1621
Andrew Kaylor93132f52013-05-28 23:04:25 +00001622 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1623
Stephen Wilson84ffe702011-03-30 15:55:52 +00001624 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1625 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1626 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1627 //
1628 // IOW, user generated signals never generate what we consider to be a
1629 // "crash".
1630 //
1631 // Similarly, ACK signals generated by this monitor.
1632 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1633 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001634 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001635 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001636 __FUNCTION__,
1637 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1638 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1639 info->si_pid);
1640
Stephen Wilson84ffe702011-03-30 15:55:52 +00001641 if (info->si_pid == getpid())
1642 return ProcessMessage::SignalDelivered(pid, signo);
1643 else
1644 return ProcessMessage::Signal(pid, signo);
1645 }
1646
Andrew Kaylor93132f52013-05-28 23:04:25 +00001647 if (log)
1648 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1649
Stephen Wilson84ffe702011-03-30 15:55:52 +00001650 if (signo == SIGSEGV) {
1651 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1652 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1653 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1654 }
1655
1656 if (signo == SIGILL) {
1657 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1658 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1659 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1660 }
1661
1662 if (signo == SIGFPE) {
1663 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1664 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1665 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1666 }
1667
1668 if (signo == SIGBUS) {
1669 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1670 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1671 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1672 }
1673
1674 // Everything else is "normal" and does not require any special action on
1675 // our part.
1676 return ProcessMessage::Signal(pid, signo);
1677}
1678
Andrew Kaylord4d54992013-09-17 00:30:24 +00001679// On Linux, when a new thread is created, we receive to notifications,
1680// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1681// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1682// the new child thread indicating that it has is stopped because we attached.
1683// We have no guarantee of the order in which these arrive, but we need both
1684// before we are ready to proceed. We currently keep a list of threads which
1685// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1686// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1687// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1688
1689bool
1690ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1691{
1692 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1693 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001694 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001695
1696 // Wait for the thread to stop
1697 while (true)
1698 {
1699 int status = -1;
1700 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001701 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001702 lldb::pid_t wait_pid = waitpid(tid, &status, __WALL);
1703 if (status == -1)
1704 {
1705 // If we got interrupted by a signal (in our process, not the
1706 // inferior) try again.
1707 if (errno == EINTR)
1708 continue;
1709 else
1710 {
1711 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001712 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001713 return false; // This is bad, but there's nothing we can do.
1714 }
1715 }
1716
1717 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001718 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001719
1720 assert(wait_pid == tid);
1721
1722 siginfo_t info;
1723 int ptrace_err;
1724 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1725 {
1726 if (log)
1727 {
1728 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1729 }
1730 return false;
1731 }
1732
1733 // If this is a thread exit, we won't get any more information.
1734 if (WIFEXITED(status))
1735 {
1736 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1737 if (wait_pid == tid)
1738 return true;
1739 continue;
1740 }
1741
1742 assert(info.si_code == SI_USER);
1743 assert(WSTOPSIG(status) == SIGSTOP);
1744
1745 if (log)
1746 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1747 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1748 return true;
1749 }
1750 return false;
1751}
1752
Andrew Kaylor93132f52013-05-28 23:04:25 +00001753bool
1754ProcessMonitor::StopThread(lldb::tid_t tid)
1755{
1756 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1757
1758 // FIXME: Try to use tgkill or tkill
1759 int ret = tgkill(m_pid, tid, SIGSTOP);
1760 if (log)
1761 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1762
1763 // This can happen if a thread exited while we were trying to stop it. That's OK.
1764 // We'll get the signal for that later.
1765 if (ret < 0)
1766 return false;
1767
1768 // Wait for the thread to stop
1769 while (true)
1770 {
1771 int status = -1;
1772 if (log)
1773 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
1774 lldb::pid_t wait_pid = ::waitpid (-1*m_pid, &status, __WALL);
1775 if (log)
1776 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d", __FUNCTION__, wait_pid, status);
1777
1778 if (wait_pid == -1)
1779 {
1780 // If we got interrupted by a signal (in our process, not the
1781 // inferior) try again.
1782 if (errno == EINTR)
1783 continue;
1784 else
1785 return false; // This is bad, but there's nothing we can do.
1786 }
1787
1788 // If this is a thread exit, we won't get any more information.
1789 if (WIFEXITED(status))
1790 {
1791 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1792 if (wait_pid == tid)
1793 return true;
1794 continue;
1795 }
1796
1797 siginfo_t info;
1798 int ptrace_err;
1799 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1800 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001801 // another signal causing a StopAllThreads may have been received
1802 // before wait_pid's group-stop was processed, handle it now
1803 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001804 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001805 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001806
Todd Fiala1b0539c2014-01-27 17:03:57 +00001807 if (log)
1808 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1809 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1810 if (!Resume(wait_pid, SIGSTOP)) {
1811 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1812 }
1813 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001814 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001815
1816 if (log)
1817 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001818 return false;
1819 }
1820
1821 // Handle events from other threads
1822 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001823 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64, __FUNCTION__, wait_pid);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001824
1825 ProcessMessage message;
1826 if (info.si_signo == SIGTRAP)
1827 message = MonitorSIGTRAP(this, &info, wait_pid);
1828 else
1829 message = MonitorSignal(this, &info, wait_pid);
1830
1831 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1832
1833 // When a new thread is created, we may get a SIGSTOP for the new thread
1834 // just before we get the SIGTRAP that we use to add the thread to our
1835 // process thread list. We don't need to worry about that signal here.
1836 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1837
1838 if (!thread)
1839 {
1840 m_process->SendMessage(message);
1841 continue;
1842 }
1843
1844 switch (message.GetKind())
1845 {
Michael Sartainc258b302013-09-18 15:32:06 +00001846 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001847 case ProcessMessage::eInvalidMessage:
1848 break;
1849
1850 // These need special handling because we don't want to send a
1851 // resume even if we already sent a SIGSTOP to this thread. In
1852 // this case the resume will cause the thread to disappear. It is
1853 // unlikely that we'll ever get eExitMessage here, but the same
1854 // reasoning applies.
1855 case ProcessMessage::eLimboMessage:
1856 case ProcessMessage::eExitMessage:
1857 if (log)
1858 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1859 // SendMessage will set the thread state as needed.
1860 m_process->SendMessage(message);
1861 // If this is the thread we're waiting for, stop waiting. Even
1862 // though this wasn't the signal we expected, it's the last
1863 // signal we'll see while this thread is alive.
1864 if (wait_pid == tid)
1865 return true;
1866 break;
1867
Matt Kopecb2910442013-07-09 15:09:45 +00001868 case ProcessMessage::eSignalMessage:
1869 if (log)
1870 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1871 if (WSTOPSIG(status) == SIGSTOP)
1872 {
1873 m_process->AddThreadForInitialStopIfNeeded(tid);
1874 thread->SetState(lldb::eStateStopped);
1875 }
1876 else
1877 {
1878 m_process->SendMessage(message);
1879 // This isn't the stop we were expecting, but the thread is
1880 // stopped. SendMessage will handle processing of this event,
1881 // but we need to resume here to get the stop we are waiting
1882 // for (otherwise the thread will stop again immediately when
1883 // we try to resume).
1884 if (wait_pid == tid)
1885 Resume(wait_pid, eResumeSignalNone);
1886 }
1887 break;
1888
Andrew Kaylor93132f52013-05-28 23:04:25 +00001889 case ProcessMessage::eSignalDeliveredMessage:
1890 // This is the stop we're expecting.
1891 if (wait_pid == tid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && info.si_code == SI_TKILL)
1892 {
1893 if (log)
1894 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1895 thread->SetState(lldb::eStateStopped);
1896 return true;
1897 }
1898 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001899 case ProcessMessage::eBreakpointMessage:
1900 case ProcessMessage::eTraceMessage:
1901 case ProcessMessage::eWatchpointMessage:
1902 case ProcessMessage::eCrashMessage:
1903 case ProcessMessage::eNewThreadMessage:
1904 if (log)
1905 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1906 // SendMessage will set the thread state as needed.
1907 m_process->SendMessage(message);
1908 // This isn't the stop we were expecting, but the thread is
1909 // stopped. SendMessage will handle processing of this event,
1910 // but we need to resume here to get the stop we are waiting
1911 // for (otherwise the thread will stop again immediately when
1912 // we try to resume).
1913 if (wait_pid == tid)
1914 Resume(wait_pid, eResumeSignalNone);
1915 break;
1916 }
1917 }
1918 return false;
1919}
1920
Stephen Wilson84ffe702011-03-30 15:55:52 +00001921ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001922ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001923{
1924 ProcessMessage::CrashReason reason;
1925 assert(info->si_signo == SIGSEGV);
1926
1927 reason = ProcessMessage::eInvalidCrashReason;
1928
Greg Clayton542e4072012-09-07 17:49:29 +00001929 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001930 {
1931 default:
1932 assert(false && "unexpected si_code for SIGSEGV");
1933 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001934 case SI_KERNEL:
1935 // Linux will occasionally send spurious SI_KERNEL codes.
1936 // (this is poorly documented in sigaction)
1937 // One way to get this is via unaligned SIMD loads.
1938 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1939 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001940 case SEGV_MAPERR:
1941 reason = ProcessMessage::eInvalidAddress;
1942 break;
1943 case SEGV_ACCERR:
1944 reason = ProcessMessage::ePrivilegedAddress;
1945 break;
1946 }
Greg Clayton542e4072012-09-07 17:49:29 +00001947
Stephen Wilson84ffe702011-03-30 15:55:52 +00001948 return reason;
1949}
1950
1951ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001952ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001953{
1954 ProcessMessage::CrashReason reason;
1955 assert(info->si_signo == SIGILL);
1956
1957 reason = ProcessMessage::eInvalidCrashReason;
1958
1959 switch (info->si_code)
1960 {
1961 default:
1962 assert(false && "unexpected si_code for SIGILL");
1963 break;
1964 case ILL_ILLOPC:
1965 reason = ProcessMessage::eIllegalOpcode;
1966 break;
1967 case ILL_ILLOPN:
1968 reason = ProcessMessage::eIllegalOperand;
1969 break;
1970 case ILL_ILLADR:
1971 reason = ProcessMessage::eIllegalAddressingMode;
1972 break;
1973 case ILL_ILLTRP:
1974 reason = ProcessMessage::eIllegalTrap;
1975 break;
1976 case ILL_PRVOPC:
1977 reason = ProcessMessage::ePrivilegedOpcode;
1978 break;
1979 case ILL_PRVREG:
1980 reason = ProcessMessage::ePrivilegedRegister;
1981 break;
1982 case ILL_COPROC:
1983 reason = ProcessMessage::eCoprocessorError;
1984 break;
1985 case ILL_BADSTK:
1986 reason = ProcessMessage::eInternalStackError;
1987 break;
1988 }
1989
1990 return reason;
1991}
1992
1993ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001994ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001995{
1996 ProcessMessage::CrashReason reason;
1997 assert(info->si_signo == SIGFPE);
1998
1999 reason = ProcessMessage::eInvalidCrashReason;
2000
2001 switch (info->si_code)
2002 {
2003 default:
2004 assert(false && "unexpected si_code for SIGFPE");
2005 break;
2006 case FPE_INTDIV:
2007 reason = ProcessMessage::eIntegerDivideByZero;
2008 break;
2009 case FPE_INTOVF:
2010 reason = ProcessMessage::eIntegerOverflow;
2011 break;
2012 case FPE_FLTDIV:
2013 reason = ProcessMessage::eFloatDivideByZero;
2014 break;
2015 case FPE_FLTOVF:
2016 reason = ProcessMessage::eFloatOverflow;
2017 break;
2018 case FPE_FLTUND:
2019 reason = ProcessMessage::eFloatUnderflow;
2020 break;
2021 case FPE_FLTRES:
2022 reason = ProcessMessage::eFloatInexactResult;
2023 break;
2024 case FPE_FLTINV:
2025 reason = ProcessMessage::eFloatInvalidOperation;
2026 break;
2027 case FPE_FLTSUB:
2028 reason = ProcessMessage::eFloatSubscriptRange;
2029 break;
2030 }
2031
2032 return reason;
2033}
2034
2035ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002036ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002037{
2038 ProcessMessage::CrashReason reason;
2039 assert(info->si_signo == SIGBUS);
2040
2041 reason = ProcessMessage::eInvalidCrashReason;
2042
2043 switch (info->si_code)
2044 {
2045 default:
2046 assert(false && "unexpected si_code for SIGBUS");
2047 break;
2048 case BUS_ADRALN:
2049 reason = ProcessMessage::eIllegalAlignment;
2050 break;
2051 case BUS_ADRERR:
2052 reason = ProcessMessage::eIllegalAddress;
2053 break;
2054 case BUS_OBJERR:
2055 reason = ProcessMessage::eHardwareError;
2056 break;
2057 }
2058
2059 return reason;
2060}
2061
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002062void
Johnny Chen25e68e32011-06-14 19:19:50 +00002063ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002064{
Stephen Wilson570243b2011-01-19 01:37:06 +00002065 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002066
Stephen Wilson570243b2011-01-19 01:37:06 +00002067 // We are finised with the arguments and are ready to go. Sync with the
2068 // parent thread and start serving operations on the inferior.
2069 sem_post(&args->m_semaphore);
2070
Michael Sartain704bf892013-10-09 01:28:57 +00002071 for(;;)
2072 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002073 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002074 if (sem_wait(&monitor->m_operation_pending))
2075 {
2076 if (errno == EINTR)
2077 continue;
2078 assert(false && "Unexpected errno from sem_wait");
2079 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002080
Daniel Malea1efb4182013-09-16 23:12:18 +00002081 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002082
Daniel Malea1efb4182013-09-16 23:12:18 +00002083 // notify calling thread that operation is complete
2084 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002085 }
2086}
2087
2088void
2089ProcessMonitor::DoOperation(Operation *op)
2090{
Daniel Malea1efb4182013-09-16 23:12:18 +00002091 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002092
Daniel Malea1efb4182013-09-16 23:12:18 +00002093 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002094
Daniel Malea1efb4182013-09-16 23:12:18 +00002095 // notify operation thread that an operation is ready to be processed
2096 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002097
Daniel Malea1efb4182013-09-16 23:12:18 +00002098 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002099 while (sem_wait(&m_operation_done))
2100 {
2101 if (errno == EINTR)
2102 continue;
2103 assert(false && "Unexpected errno from sem_wait");
2104 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002105}
2106
2107size_t
2108ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2109 Error &error)
2110{
2111 size_t result;
2112 ReadOperation op(vm_addr, buf, size, error, result);
2113 DoOperation(&op);
2114 return result;
2115}
2116
2117size_t
2118ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2119 lldb_private::Error &error)
2120{
2121 size_t result;
2122 WriteOperation op(vm_addr, buf, size, error, result);
2123 DoOperation(&op);
2124 return result;
2125}
2126
2127bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002128ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002129 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002130{
2131 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002132 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002133 DoOperation(&op);
2134 return result;
2135}
2136
2137bool
Matt Kopec7de48462013-03-06 17:20:48 +00002138ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002139 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002140{
2141 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002142 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002143 DoOperation(&op);
2144 return result;
2145}
2146
2147bool
Matt Kopec7de48462013-03-06 17:20:48 +00002148ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002149{
2150 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002151 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002152 DoOperation(&op);
2153 return result;
2154}
2155
2156bool
Matt Kopec7de48462013-03-06 17:20:48 +00002157ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002158{
2159 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002160 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002161 DoOperation(&op);
2162 return result;
2163}
2164
2165bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002166ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2167{
2168 bool result;
2169 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2170 DoOperation(&op);
2171 return result;
2172}
2173
2174bool
Matt Kopec7de48462013-03-06 17:20:48 +00002175ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002176{
2177 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002178 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002179 DoOperation(&op);
2180 return result;
2181}
2182
2183bool
Matt Kopec7de48462013-03-06 17:20:48 +00002184ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002185{
2186 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002187 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002188 DoOperation(&op);
2189 return result;
2190}
2191
2192bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002193ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2194{
2195 bool result;
2196 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2197 DoOperation(&op);
2198 return result;
2199}
2200
2201bool
Richard Mitton0a558352013-10-17 21:14:00 +00002202ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2203{
2204 bool result;
2205 ReadThreadPointerOperation op(tid, &value, result);
2206 DoOperation(&op);
2207 return result;
2208}
2209
2210bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002211ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002212{
2213 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002214 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2215
2216 if (log)
2217 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2218 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002219 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002220 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002221 if (log)
2222 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002223 return result;
2224}
2225
2226bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002227ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002228{
2229 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002230 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002231 DoOperation(&op);
2232 return result;
2233}
2234
2235bool
2236ProcessMonitor::BringProcessIntoLimbo()
2237{
2238 bool result;
2239 KillOperation op(result);
2240 DoOperation(&op);
2241 return result;
2242}
2243
2244bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002245ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002246{
2247 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002248 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002249 DoOperation(&op);
2250 return result;
2251}
2252
2253bool
2254ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2255{
2256 bool result;
2257 EventMessageOperation op(tid, message, result);
2258 DoOperation(&op);
2259 return result;
2260}
2261
Greg Clayton743ecf42012-10-16 20:20:18 +00002262lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002263ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002264{
Greg Clayton28041352011-11-29 20:50:10 +00002265 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002266 if (tid != LLDB_INVALID_THREAD_ID)
2267 {
2268 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002269 DoOperation(&op);
2270 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002271 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002272}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002273
2274bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002275ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2276{
Peter Collingbourne62343202011-06-14 03:55:54 +00002277 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002278
2279 if (target_fd == -1)
2280 return false;
2281
Peter Collingbourne62343202011-06-14 03:55:54 +00002282 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002283}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002284
2285void
2286ProcessMonitor::StopMonitoringChildProcess()
2287{
2288 lldb::thread_result_t thread_result;
2289
Stephen Wilsond4182f42011-02-09 20:10:35 +00002290 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002291 {
2292 Host::ThreadCancel(m_monitor_thread, NULL);
2293 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2294 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2295 }
2296}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002297
2298void
2299ProcessMonitor::StopMonitor()
2300{
2301 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002302 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002303 sem_destroy(&m_operation_pending);
2304 sem_destroy(&m_operation_done);
2305
Andrew Kaylor5e268992013-09-14 00:17:31 +00002306 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2307 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2308 // the descriptor to a ConnectionFileDescriptor object. Consequently
2309 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002310}
2311
2312void
Greg Clayton743ecf42012-10-16 20:20:18 +00002313ProcessMonitor::StopOpThread()
2314{
2315 lldb::thread_result_t result;
2316
2317 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2318 return;
2319
2320 Host::ThreadCancel(m_operation_thread, NULL);
2321 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002322 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002323}