blob: 4cab19adf928d2b48f5731e54b1d41ca51741dde [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
18#include <sys/ptrace.h>
19#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000020#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000021#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000022#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include <sys/wait.h>
24
25// C++ Includes
26// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000027#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000029#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Core/Scalar.h"
31#include "lldb/Host/Host.h"
32#include "lldb/Target/Thread.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Utility/PseudoTerminal.h"
35
Johnny Chen30213ff2012-01-05 19:17:38 +000036#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000038#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
40
Greg Clayton386ff182011-11-05 01:09:16 +000041#define DEBUG_PTRACE_MAXBYTES 20
42
Matt Kopec58c0b962013-03-20 20:34:35 +000043// Support ptrace extensions even when compiled without required kernel support
44#ifndef PTRACE_GETREGSET
45 #define PTRACE_GETREGSET 0x4204
46#endif
47#ifndef PTRACE_SETREGSET
48 #define PTRACE_SETREGSET 0x4205
49#endif
Richard Mitton0a558352013-10-17 21:14:00 +000050#ifndef PTRACE_GET_THREAD_AREA
51 #define PTRACE_GET_THREAD_AREA 25
52#endif
53#ifndef PTRACE_ARCH_PRCTL
54 #define PTRACE_ARCH_PRCTL 30
55#endif
56#ifndef ARCH_GET_FS
57 #define ARCH_SET_GS 0x1001
58 #define ARCH_SET_FS 0x1002
59 #define ARCH_GET_FS 0x1003
60 #define ARCH_GET_GS 0x1004
61#endif
62
Matt Kopec58c0b962013-03-20 20:34:35 +000063
Matt Kopece9ea0da2013-05-07 19:29:28 +000064// Support hardware breakpoints in case it has not been defined
65#ifndef TRAP_HWBKPT
66 #define TRAP_HWBKPT 4
67#endif
68
Andrew Kaylor93132f52013-05-28 23:04:25 +000069// Try to define a macro to encapsulate the tgkill syscall
70// fall back on kill() if tgkill isn't available
71#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
72
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073using namespace lldb_private;
74
Johnny Chen0d5f2d42011-10-18 18:09:30 +000075// FIXME: this code is host-dependent with respect to types and
76// endianness and needs to be fixed. For example, lldb::addr_t is
77// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
78// 32-bit pointer arguments. This code uses casts to work around the
79// problem.
80
81// We disable the tracing of ptrace calls for integration builds to
82// avoid the additional indirection and checks.
83#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
84
Greg Clayton386ff182011-11-05 01:09:16 +000085static void
86DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
87{
88 uint8_t *ptr = (uint8_t *)bytes;
89 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
90 for(uint32_t i=0; i<loop_count; i++)
91 {
92 s.Printf ("[%x]", *ptr);
93 ptr++;
94 }
95}
96
Matt Kopec58c0b962013-03-20 20:34:35 +000097static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000098{
99 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000100 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000101 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000102
103 if (verbose_log)
104 {
105 switch(req)
106 {
107 case PTRACE_POKETEXT:
108 {
109 DisplayBytes(buf, &data, 8);
110 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
111 break;
112 }
Greg Clayton542e4072012-09-07 17:49:29 +0000113 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000114 {
115 DisplayBytes(buf, &data, 8);
116 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, &data, 8);
122 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
123 break;
124 }
Greg Clayton542e4072012-09-07 17:49:29 +0000125 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000126 {
Matt Kopec7de48462013-03-06 17:20:48 +0000127 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000128 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
129 break;
130 }
131 case PTRACE_SETFPREGS:
132 {
Matt Kopec7de48462013-03-06 17:20:48 +0000133 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000134 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
135 break;
136 }
Greg Clayton542e4072012-09-07 17:49:29 +0000137 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000138 {
139 DisplayBytes(buf, data, sizeof(siginfo_t));
140 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
141 break;
142 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000143 case PTRACE_SETREGSET:
144 {
145 // Extract iov_base from data, which is a pointer to the struct IOVEC
146 DisplayBytes(buf, *(void **)data, data_size);
147 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
148 break;
149 }
Greg Clayton386ff182011-11-05 01:09:16 +0000150 default:
151 {
152 }
153 }
154 }
155}
156
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000157// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000158// Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000159extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000160PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000161 const char* reqName, const char* file, int line)
162{
Greg Clayton386ff182011-11-05 01:09:16 +0000163 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000164
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000165 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000166
Matt Kopec7de48462013-03-06 17:20:48 +0000167 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000168
169 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000170 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000171 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000172 else
Todd Fiala4507f062014-02-27 20:46:12 +0000173 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000174
Ed Mastec099c952014-02-24 14:07:45 +0000175 if (log)
176 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
177 reqName, pid, addr, data, data_size, result, file, line);
178
Matt Kopec7de48462013-03-06 17:20:48 +0000179 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000180
Matt Kopec7de48462013-03-06 17:20:48 +0000181 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000182 {
183 const char* str;
184 switch (errno)
185 {
186 case ESRCH: str = "ESRCH"; break;
187 case EINVAL: str = "EINVAL"; break;
188 case EBUSY: str = "EBUSY"; break;
189 case EPERM: str = "EPERM"; break;
190 default: str = "<unknown>";
191 }
192 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
193 }
194
195 return result;
196}
197
Matt Kopec7de48462013-03-06 17:20:48 +0000198// Wrapper for ptrace when logging is not required.
199// Sets errno to 0 prior to calling ptrace.
200extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000201PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000202{
Matt Kopec58c0b962013-03-20 20:34:35 +0000203 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000204 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000205 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
206 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
207 else
208 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000209 return result;
210}
211
212#define PTRACE(req, pid, addr, data, data_size) \
213 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000214#else
Matt Kopec7de48462013-03-06 17:20:48 +0000215 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000216#endif
217
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218//------------------------------------------------------------------------------
219// Static implementations of ProcessMonitor::ReadMemory and
220// ProcessMonitor::WriteMemory. This enables mutual recursion between these
221// functions without needed to go thru the thread funnel.
222
223static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000224DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000225 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
226{
Greg Clayton542e4072012-09-07 17:49:29 +0000227 // ptrace word size is determined by the host, not the child
228 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 unsigned char *dst = static_cast<unsigned char*>(buf);
230 size_t bytes_read;
231 size_t remainder;
232 long data;
233
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000235 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000236 ProcessPOSIXLog::IncNestLevel();
237 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000238 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000239 pid, word_size, (void*)vm_addr, buf, size);
240
241 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000242 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
243 {
244 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000245 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
246 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000247 {
248 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000249 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000250 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 return bytes_read;
252 }
253
254 remainder = size - bytes_read;
255 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000256
257 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000258 for (unsigned i = 0; i < remainder; ++i)
259 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
262 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
263 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
264 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000265 {
266 uintptr_t print_dst = 0;
267 // Format bytes from data by moving into print_dst for log output
268 for (unsigned i = 0; i < remainder; ++i)
269 print_dst |= (((data >> i*8) & 0xFF) << i*8);
270 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
271 (void*)vm_addr, print_dst, (unsigned long)data);
272 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000273
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274 vm_addr += word_size;
275 dst += word_size;
276 }
277
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000278 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000279 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 return bytes_read;
281}
282
283static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000284DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
286{
Greg Clayton542e4072012-09-07 17:49:29 +0000287 // ptrace word size is determined by the host, not the child
288 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000289 const unsigned char *src = static_cast<const unsigned char*>(buf);
290 size_t bytes_written = 0;
291 size_t remainder;
292
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000293 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000294 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000295 ProcessPOSIXLog::IncNestLevel();
296 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000297 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 pid, word_size, (void*)vm_addr, buf, size);
299
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000300 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
301 {
302 remainder = size - bytes_written;
303 remainder = remainder > word_size ? word_size : remainder;
304
305 if (remainder == word_size)
306 {
307 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000308 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000309 for (unsigned i = 0; i < word_size; ++i)
310 data |= (unsigned long)src[i] << i*8;
311
Johnny Chen30213ff2012-01-05 19:17:38 +0000312 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
313 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
314 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
315 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000316 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
317 (void*)vm_addr, *(unsigned long*)src, data);
318
Matt Kopec7de48462013-03-06 17:20:48 +0000319 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000320 {
321 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000323 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000324 return bytes_written;
325 }
326 }
327 else
328 {
329 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000330 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000332 {
333 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000334 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000335 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000336 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000337
338 memcpy(buff, src, remainder);
339
Greg Clayton542e4072012-09-07 17:49:29 +0000340 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 {
343 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000344 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 }
347
Johnny Chen30213ff2012-01-05 19:17:38 +0000348 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
349 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
350 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
351 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
353 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000354 }
355
356 vm_addr += word_size;
357 src += word_size;
358 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000359 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000360 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000361 return bytes_written;
362}
363
Stephen Wilson26977162011-03-23 02:14:42 +0000364// Simple helper function to ensure flags are enabled on the given file
365// descriptor.
366static bool
367EnsureFDFlags(int fd, int flags, Error &error)
368{
369 int status;
370
371 if ((status = fcntl(fd, F_GETFL)) == -1)
372 {
373 error.SetErrorToErrno();
374 return false;
375 }
376
377 if (fcntl(fd, F_SETFL, status | flags) == -1)
378 {
379 error.SetErrorToErrno();
380 return false;
381 }
382
383 return true;
384}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000385
386//------------------------------------------------------------------------------
387/// @class Operation
388/// @brief Represents a ProcessMonitor operation.
389///
390/// Under Linux, it is not possible to ptrace() from any other thread but the
391/// one that spawned or attached to the process from the start. Therefore, when
392/// a ProcessMonitor is asked to deliver or change the state of an inferior
393/// process the operation must be "funneled" to a specific thread to perform the
394/// task. The Operation class provides an abstract base for all services the
395/// ProcessMonitor must perform via the single virtual function Execute, thus
396/// encapsulating the code that needs to run in the privileged context.
397class Operation
398{
399public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000400 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000401 virtual void Execute(ProcessMonitor *monitor) = 0;
402};
403
404//------------------------------------------------------------------------------
405/// @class ReadOperation
406/// @brief Implements ProcessMonitor::ReadMemory.
407class ReadOperation : public Operation
408{
409public:
410 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
411 Error &error, size_t &result)
412 : m_addr(addr), m_buff(buff), m_size(size),
413 m_error(error), m_result(result)
414 { }
415
416 void Execute(ProcessMonitor *monitor);
417
418private:
419 lldb::addr_t m_addr;
420 void *m_buff;
421 size_t m_size;
422 Error &m_error;
423 size_t &m_result;
424};
425
426void
427ReadOperation::Execute(ProcessMonitor *monitor)
428{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000429 lldb::pid_t pid = monitor->GetPID();
430
Greg Clayton542e4072012-09-07 17:49:29 +0000431 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000432}
433
434//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000435/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000436/// @brief Implements ProcessMonitor::WriteMemory.
437class WriteOperation : public Operation
438{
439public:
440 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
441 Error &error, size_t &result)
442 : m_addr(addr), m_buff(buff), m_size(size),
443 m_error(error), m_result(result)
444 { }
445
446 void Execute(ProcessMonitor *monitor);
447
448private:
449 lldb::addr_t m_addr;
450 const void *m_buff;
451 size_t m_size;
452 Error &m_error;
453 size_t &m_result;
454};
455
456void
457WriteOperation::Execute(ProcessMonitor *monitor)
458{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000459 lldb::pid_t pid = monitor->GetPID();
460
Greg Clayton542e4072012-09-07 17:49:29 +0000461 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000462}
463
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000464
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465//------------------------------------------------------------------------------
466/// @class ReadRegOperation
467/// @brief Implements ProcessMonitor::ReadRegisterValue.
468class ReadRegOperation : public Operation
469{
470public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000471 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000472 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000473 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000474 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000475 { }
476
477 void Execute(ProcessMonitor *monitor);
478
479private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000481 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000482 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000483 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000484 bool &m_result;
485};
486
487void
488ReadRegOperation::Execute(ProcessMonitor *monitor)
489{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000490 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000491
492 // Set errno to zero so that we can detect a failed peek.
493 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000494 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
495 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000496 m_result = false;
497 else
498 {
499 m_value = data;
500 m_result = true;
501 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000502 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000503 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000504 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000505}
506
507//------------------------------------------------------------------------------
508/// @class WriteRegOperation
509/// @brief Implements ProcessMonitor::WriteRegisterValue.
510class WriteRegOperation : public Operation
511{
512public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000513 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000514 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000515 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000516 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000517 { }
518
519 void Execute(ProcessMonitor *monitor);
520
521private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000523 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000524 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000525 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000526 bool &m_result;
527};
528
529void
530WriteRegOperation::Execute(ProcessMonitor *monitor)
531{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000532 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000533 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000534
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000535 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000536
537 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000538 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000539 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000540 m_result = false;
541 else
542 m_result = true;
543}
544
545//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000546/// @class ReadGPROperation
547/// @brief Implements ProcessMonitor::ReadGPR.
548class ReadGPROperation : public Operation
549{
550public:
Matt Kopec7de48462013-03-06 17:20:48 +0000551 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
552 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000553 { }
554
555 void Execute(ProcessMonitor *monitor);
556
557private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000558 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000559 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000560 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000561 bool &m_result;
562};
563
564void
565ReadGPROperation::Execute(ProcessMonitor *monitor)
566{
Matt Kopec7de48462013-03-06 17:20:48 +0000567 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000568 m_result = false;
569 else
570 m_result = true;
571}
572
573//------------------------------------------------------------------------------
574/// @class ReadFPROperation
575/// @brief Implements ProcessMonitor::ReadFPR.
576class ReadFPROperation : public Operation
577{
578public:
Matt Kopec7de48462013-03-06 17:20:48 +0000579 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
580 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000581 { }
582
583 void Execute(ProcessMonitor *monitor);
584
585private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000586 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000587 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000588 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000589 bool &m_result;
590};
591
592void
593ReadFPROperation::Execute(ProcessMonitor *monitor)
594{
Matt Kopec7de48462013-03-06 17:20:48 +0000595 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000596 m_result = false;
597 else
598 m_result = true;
599}
600
601//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000602/// @class ReadRegisterSetOperation
603/// @brief Implements ProcessMonitor::ReadRegisterSet.
604class ReadRegisterSetOperation : public Operation
605{
606public:
607 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
608 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
609 { }
610
611 void Execute(ProcessMonitor *monitor);
612
613private:
614 lldb::tid_t m_tid;
615 void *m_buf;
616 size_t m_buf_size;
617 const unsigned int m_regset;
618 bool &m_result;
619};
620
621void
622ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
623{
624 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
625 m_result = false;
626 else
627 m_result = true;
628}
629
630//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000631/// @class WriteGPROperation
632/// @brief Implements ProcessMonitor::WriteGPR.
633class WriteGPROperation : public Operation
634{
635public:
Matt Kopec7de48462013-03-06 17:20:48 +0000636 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
637 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000638 { }
639
640 void Execute(ProcessMonitor *monitor);
641
642private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000643 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000644 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000645 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000646 bool &m_result;
647};
648
649void
650WriteGPROperation::Execute(ProcessMonitor *monitor)
651{
Matt Kopec7de48462013-03-06 17:20:48 +0000652 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000653 m_result = false;
654 else
655 m_result = true;
656}
657
658//------------------------------------------------------------------------------
659/// @class WriteFPROperation
660/// @brief Implements ProcessMonitor::WriteFPR.
661class WriteFPROperation : public Operation
662{
663public:
Matt Kopec7de48462013-03-06 17:20:48 +0000664 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
665 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000666 { }
667
668 void Execute(ProcessMonitor *monitor);
669
670private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000671 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000672 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000673 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000674 bool &m_result;
675};
676
677void
678WriteFPROperation::Execute(ProcessMonitor *monitor)
679{
Matt Kopec7de48462013-03-06 17:20:48 +0000680 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000681 m_result = false;
682 else
683 m_result = true;
684}
685
686//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000687/// @class WriteRegisterSetOperation
688/// @brief Implements ProcessMonitor::WriteRegisterSet.
689class WriteRegisterSetOperation : public Operation
690{
691public:
692 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
693 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
694 { }
695
696 void Execute(ProcessMonitor *monitor);
697
698private:
699 lldb::tid_t m_tid;
700 void *m_buf;
701 size_t m_buf_size;
702 const unsigned int m_regset;
703 bool &m_result;
704};
705
706void
707WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
708{
709 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
710 m_result = false;
711 else
712 m_result = true;
713}
714
715//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000716/// @class ReadThreadPointerOperation
717/// @brief Implements ProcessMonitor::ReadThreadPointer.
718class ReadThreadPointerOperation : public Operation
719{
720public:
721 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
722 : m_tid(tid), m_addr(addr), m_result(result)
723 { }
724
725 void Execute(ProcessMonitor *monitor);
726
727private:
728 lldb::tid_t m_tid;
729 lldb::addr_t *m_addr;
730 bool &m_result;
731};
732
733void
734ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
735{
736 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
737 if (log)
738 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
739
740 // The process for getting the thread area on Linux is
741 // somewhat... obscure. There's several different ways depending on
742 // what arch you're on, and what kernel version you have.
743
744 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
745 switch(arch.GetMachine())
746 {
747 case llvm::Triple::x86:
748 {
749 // Find the GS register location for our host architecture.
750 size_t gs_user_offset = offsetof(struct user, regs);
751#ifdef __x86_64__
752 gs_user_offset += offsetof(struct user_regs_struct, gs);
753#endif
754#ifdef __i386__
755 gs_user_offset += offsetof(struct user_regs_struct, xgs);
756#endif
757
758 // Read the GS register value to get the selector.
759 errno = 0;
760 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
761 if (errno)
762 {
763 m_result = false;
764 break;
765 }
766
767 // Read the LDT base for that selector.
768 uint32_t tmp[4];
769 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
770 *m_addr = tmp[1];
771 break;
772 }
773 case llvm::Triple::x86_64:
774 // Read the FS register base.
775 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
776 break;
777 default:
778 m_result = false;
779 break;
780 }
781}
782
783//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000784/// @class ResumeOperation
785/// @brief Implements ProcessMonitor::Resume.
786class ResumeOperation : public Operation
787{
788public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000789 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
790 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000791
792 void Execute(ProcessMonitor *monitor);
793
794private:
795 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000796 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000797 bool &m_result;
798};
799
800void
801ResumeOperation::Execute(ProcessMonitor *monitor)
802{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000803 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000804
805 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
806 data = m_signo;
807
Matt Kopec7de48462013-03-06 17:20:48 +0000808 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000809 {
810 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
811
812 if (log)
813 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000814 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000815 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000816 else
817 m_result = true;
818}
819
820//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000821/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000822/// @brief Implements ProcessMonitor::SingleStep.
823class SingleStepOperation : public Operation
824{
825public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000826 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
827 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000828
829 void Execute(ProcessMonitor *monitor);
830
831private:
832 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000833 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000834 bool &m_result;
835};
836
837void
838SingleStepOperation::Execute(ProcessMonitor *monitor)
839{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000840 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000841
842 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
843 data = m_signo;
844
Matt Kopec7de48462013-03-06 17:20:48 +0000845 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000846 m_result = false;
847 else
848 m_result = true;
849}
850
851//------------------------------------------------------------------------------
852/// @class SiginfoOperation
853/// @brief Implements ProcessMonitor::GetSignalInfo.
854class SiginfoOperation : public Operation
855{
856public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000857 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
858 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000859
860 void Execute(ProcessMonitor *monitor);
861
862private:
863 lldb::tid_t m_tid;
864 void *m_info;
865 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000866 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000867};
868
869void
870SiginfoOperation::Execute(ProcessMonitor *monitor)
871{
Matt Kopec7de48462013-03-06 17:20:48 +0000872 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000873 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000874 m_err = errno;
875 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000876 else
877 m_result = true;
878}
879
880//------------------------------------------------------------------------------
881/// @class EventMessageOperation
882/// @brief Implements ProcessMonitor::GetEventMessage.
883class EventMessageOperation : public Operation
884{
885public:
886 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
887 : m_tid(tid), m_message(message), m_result(result) { }
888
889 void Execute(ProcessMonitor *monitor);
890
891private:
892 lldb::tid_t m_tid;
893 unsigned long *m_message;
894 bool &m_result;
895};
896
897void
898EventMessageOperation::Execute(ProcessMonitor *monitor)
899{
Matt Kopec7de48462013-03-06 17:20:48 +0000900 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000901 m_result = false;
902 else
903 m_result = true;
904}
905
906//------------------------------------------------------------------------------
907/// @class KillOperation
908/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
909class KillOperation : public Operation
910{
911public:
912 KillOperation(bool &result) : m_result(result) { }
913
914 void Execute(ProcessMonitor *monitor);
915
916private:
917 bool &m_result;
918};
919
920void
921KillOperation::Execute(ProcessMonitor *monitor)
922{
923 lldb::pid_t pid = monitor->GetPID();
924
Matt Kopec7de48462013-03-06 17:20:48 +0000925 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000926 m_result = false;
927 else
928 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000929}
930
Greg Clayton28041352011-11-29 20:50:10 +0000931//------------------------------------------------------------------------------
932/// @class KillOperation
933/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
934class DetachOperation : public Operation
935{
936public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000937 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000938
939 void Execute(ProcessMonitor *monitor);
940
941private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000942 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000943 Error &m_error;
944};
945
946void
947DetachOperation::Execute(ProcessMonitor *monitor)
948{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000949 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000950 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000951}
952
Johnny Chen25e68e32011-06-14 19:19:50 +0000953ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
954 : m_monitor(monitor)
955{
956 sem_init(&m_semaphore, 0, 0);
957}
958
959ProcessMonitor::OperationArgs::~OperationArgs()
960{
961 sem_destroy(&m_semaphore);
962}
963
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000964ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
965 lldb_private::Module *module,
966 char const **argv,
967 char const **envp,
968 const char *stdin_path,
969 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000970 const char *stderr_path,
971 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000972 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000973 m_module(module),
974 m_argv(argv),
975 m_envp(envp),
976 m_stdin_path(stdin_path),
977 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000978 m_stderr_path(stderr_path),
979 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000980
981ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000982{ }
983
984ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
985 lldb::pid_t pid)
986 : OperationArgs(monitor), m_pid(pid) { }
987
988ProcessMonitor::AttachArgs::~AttachArgs()
989{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000990
991//------------------------------------------------------------------------------
992/// The basic design of the ProcessMonitor is built around two threads.
993///
994/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
995/// for changes in the debugee state. When a change is detected a
996/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
997/// "drives" state changes in the debugger.
998///
999/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001000/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001001/// operations such as register reads/writes, stepping, etc. See the comments
1002/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001003ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001004 Module *module,
1005 const char *argv[],
1006 const char *envp[],
1007 const char *stdin_path,
1008 const char *stdout_path,
1009 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001010 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001011 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001012 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001013 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001014 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001015 m_pid(LLDB_INVALID_PROCESS_ID),
1016 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001017 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001018{
Daniel Malea1efb4182013-09-16 23:12:18 +00001019 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1020 stdin_path, stdout_path, stderr_path,
1021 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001022
Daniel Malea1efb4182013-09-16 23:12:18 +00001023 sem_init(&m_operation_pending, 0, 0);
1024 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001025
Johnny Chen25e68e32011-06-14 19:19:50 +00001026 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001027 if (!error.Success())
1028 return;
1029
1030WAIT_AGAIN:
1031 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001032 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001033 {
1034 if (errno == EINTR)
1035 goto WAIT_AGAIN;
1036 else
1037 {
1038 error.SetErrorToErrno();
1039 return;
1040 }
1041 }
1042
1043 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001044 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001045 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001046 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001047 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001048 return;
1049 }
1050
1051 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001052 m_monitor_thread = Host::StartMonitoringChildProcess(
1053 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001054 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001055 {
1056 error.SetErrorToGenericError();
1057 error.SetErrorString("Process launch failed.");
1058 return;
1059 }
1060}
1061
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001062ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001063 lldb::pid_t pid,
1064 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001065 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001066 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001067 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001068 m_pid(LLDB_INVALID_PROCESS_ID),
1069 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001070 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001071{
Daniel Malea1efb4182013-09-16 23:12:18 +00001072 sem_init(&m_operation_pending, 0, 0);
1073 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001074
Daniel Malea1efb4182013-09-16 23:12:18 +00001075 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001076
1077 StartAttachOpThread(args.get(), error);
1078 if (!error.Success())
1079 return;
1080
1081WAIT_AGAIN:
1082 // Wait for the operation thread to initialize.
1083 if (sem_wait(&args->m_semaphore))
1084 {
1085 if (errno == EINTR)
1086 goto WAIT_AGAIN;
1087 else
1088 {
1089 error.SetErrorToErrno();
1090 return;
1091 }
1092 }
1093
Greg Clayton743ecf42012-10-16 20:20:18 +00001094 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001095 if (!args->m_error.Success())
1096 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001097 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001098 error = args->m_error;
1099 return;
1100 }
1101
1102 // Finally, start monitoring the child process for change in state.
1103 m_monitor_thread = Host::StartMonitoringChildProcess(
1104 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1105 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1106 {
1107 error.SetErrorToGenericError();
1108 error.SetErrorString("Process attach failed.");
1109 return;
1110 }
1111}
1112
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001113ProcessMonitor::~ProcessMonitor()
1114{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001115 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001116}
1117
1118//------------------------------------------------------------------------------
1119// Thread setup and tear down.
1120void
Johnny Chen25e68e32011-06-14 19:19:50 +00001121ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001122{
1123 static const char *g_thread_name = "lldb.process.linux.operation";
1124
Stephen Wilsond4182f42011-02-09 20:10:35 +00001125 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001126 return;
1127
1128 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001129 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130}
1131
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001132void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001133ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001134{
1135 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1136
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001137 if (!Launch(args)) {
1138 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001139 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001140 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001141
Stephen Wilson570243b2011-01-19 01:37:06 +00001142 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001143 return NULL;
1144}
1145
1146bool
1147ProcessMonitor::Launch(LaunchArgs *args)
1148{
1149 ProcessMonitor *monitor = args->m_monitor;
1150 ProcessLinux &process = monitor->GetProcess();
1151 const char **argv = args->m_argv;
1152 const char **envp = args->m_envp;
1153 const char *stdin_path = args->m_stdin_path;
1154 const char *stdout_path = args->m_stdout_path;
1155 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001156 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001157
1158 lldb_utility::PseudoTerminal terminal;
1159 const size_t err_len = 1024;
1160 char err_str[err_len];
1161 lldb::pid_t pid;
1162
1163 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001164 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001165
Stephen Wilson57740ec2011-01-15 00:12:41 +00001166 // Propagate the environment if one is not supplied.
1167 if (envp == NULL || envp[0] == NULL)
1168 envp = const_cast<const char **>(environ);
1169
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001170 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001171 {
1172 args->m_error.SetErrorToGenericError();
1173 args->m_error.SetErrorString("Process fork failed.");
1174 goto FINISH;
1175 }
1176
Peter Collingbourne6a520222011-06-14 03:55:58 +00001177 // Recognized child exit status codes.
1178 enum {
1179 ePtraceFailed = 1,
1180 eDupStdinFailed,
1181 eDupStdoutFailed,
1182 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001183 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001184 eExecFailed,
1185 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001186 };
1187
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001188 // Child process.
1189 if (pid == 0)
1190 {
1191 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001192 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001193 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001194
1195 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001196 if (setgid(getgid()) != 0)
1197 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001198
1199 // Let us have our own process group.
1200 setpgid(0, 0);
1201
Greg Clayton710dd5a2011-01-08 20:28:42 +00001202 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001203 //
1204 // FIXME: If two or more of the paths are the same we needlessly open
1205 // the same file multiple times.
1206 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001207 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001208 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001209
1210 if (stdout_path != NULL && stdout_path[0])
1211 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001212 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001213
1214 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001215 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001216 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001217
Daniel Malea6217d2a2013-01-08 14:49:22 +00001218 // Change working directory
1219 if (working_dir != NULL && working_dir[0])
1220 if (0 != ::chdir(working_dir))
1221 exit(eChdirFailed);
1222
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001223 // Execute. We should never return.
1224 execve(argv[0],
1225 const_cast<char *const *>(argv),
1226 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001227 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001228 }
1229
1230 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001231 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001232 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001233 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001234 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001235 args->m_error.SetErrorToErrno();
1236 goto FINISH;
1237 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001238 else if (WIFEXITED(status))
1239 {
1240 // open, dup or execve likely failed for some reason.
1241 args->m_error.SetErrorToGenericError();
1242 switch (WEXITSTATUS(status))
1243 {
Greg Clayton542e4072012-09-07 17:49:29 +00001244 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001245 args->m_error.SetErrorString("Child ptrace failed.");
1246 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001247 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001248 args->m_error.SetErrorString("Child open stdin failed.");
1249 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001250 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001251 args->m_error.SetErrorString("Child open stdout failed.");
1252 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001253 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001254 args->m_error.SetErrorString("Child open stderr failed.");
1255 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001256 case eChdirFailed:
1257 args->m_error.SetErrorString("Child failed to set working directory.");
1258 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001259 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001260 args->m_error.SetErrorString("Child exec failed.");
1261 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001262 case eSetGidFailed:
1263 args->m_error.SetErrorString("Child setgid failed.");
1264 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001265 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001266 args->m_error.SetErrorString("Child returned unknown exit status.");
1267 break;
1268 }
1269 goto FINISH;
1270 }
1271 assert(WIFSTOPPED(status) && wpid == pid &&
1272 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001273
Matt Kopec085d6ce2013-05-31 22:00:07 +00001274 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001275 {
1276 args->m_error.SetErrorToErrno();
1277 goto FINISH;
1278 }
1279
1280 // Release the master terminal descriptor and pass it off to the
1281 // ProcessMonitor instance. Similarly stash the inferior pid.
1282 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1283 monitor->m_pid = pid;
1284
Stephen Wilson26977162011-03-23 02:14:42 +00001285 // Set the terminal fd to be in non blocking mode (it simplifies the
1286 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1287 // descriptor to read from).
1288 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1289 goto FINISH;
1290
Johnny Chen30213ff2012-01-05 19:17:38 +00001291 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001292 // FIXME: should we be letting UpdateThreadList handle this?
1293 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001294 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001295
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001296 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001297 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001298 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001299
Matt Kopecb2910442013-07-09 15:09:45 +00001300 process.AddThreadForInitialStopIfNeeded(pid);
1301
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001302 // Let our process instance know the thread has stopped.
1303 process.SendMessage(ProcessMessage::Trace(pid));
1304
1305FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001306 return args->m_error.Success();
1307}
1308
Johnny Chen25e68e32011-06-14 19:19:50 +00001309void
1310ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1311{
1312 static const char *g_thread_name = "lldb.process.linux.operation";
1313
1314 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1315 return;
1316
1317 m_operation_thread =
1318 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1319}
1320
Johnny Chen25e68e32011-06-14 19:19:50 +00001321void *
1322ProcessMonitor::AttachOpThread(void *arg)
1323{
1324 AttachArgs *args = static_cast<AttachArgs*>(arg);
1325
Greg Clayton743ecf42012-10-16 20:20:18 +00001326 if (!Attach(args)) {
1327 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001328 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001329 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001330
1331 ServeOperation(args);
1332 return NULL;
1333}
1334
1335bool
1336ProcessMonitor::Attach(AttachArgs *args)
1337{
1338 lldb::pid_t pid = args->m_pid;
1339
1340 ProcessMonitor *monitor = args->m_monitor;
1341 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001342 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001343 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001344
Matt Kopec085d6ce2013-05-31 22:00:07 +00001345 // Use a map to keep track of the threads which we have attached/need to attach.
1346 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001347 if (pid <= 1)
1348 {
1349 args->m_error.SetErrorToGenericError();
1350 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1351 goto FINISH;
1352 }
1353
Matt Kopec085d6ce2013-05-31 22:00:07 +00001354 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001355 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001356 for (Host::TidMap::iterator it = tids_to_attach.begin();
1357 it != tids_to_attach.end(); ++it)
1358 {
1359 if (it->second == false)
1360 {
1361 lldb::tid_t tid = it->first;
1362
1363 // Attach to the requested process.
1364 // An attach will cause the thread to stop with a SIGSTOP.
1365 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1366 {
1367 // No such thread. The thread may have exited.
1368 // More error handling may be needed.
1369 if (errno == ESRCH)
1370 {
1371 tids_to_attach.erase(it);
1372 continue;
1373 }
1374 else
1375 {
1376 args->m_error.SetErrorToErrno();
1377 goto FINISH;
1378 }
1379 }
1380
1381 int status;
1382 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1383 // At this point we should have a thread stopped if waitpid succeeds.
1384 if ((status = waitpid(tid, NULL, __WALL)) < 0)
1385 {
1386 // No such thread. The thread may have exited.
1387 // More error handling may be needed.
1388 if (errno == ESRCH)
1389 {
1390 tids_to_attach.erase(it);
1391 continue;
1392 }
1393 else
1394 {
1395 args->m_error.SetErrorToErrno();
1396 goto FINISH;
1397 }
1398 }
1399
1400 if (!SetDefaultPtraceOpts(tid))
1401 {
1402 args->m_error.SetErrorToErrno();
1403 goto FINISH;
1404 }
1405
1406 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001407 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001408
Matt Kopec085d6ce2013-05-31 22:00:07 +00001409 if (log)
1410 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1411 process.GetThreadList().AddThread(inferior);
1412 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001413 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001414 }
1415 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001416 }
1417
Matt Kopec085d6ce2013-05-31 22:00:07 +00001418 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001419 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001420 monitor->m_pid = pid;
1421 // Let our process instance know the thread has stopped.
1422 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001423 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001424 else
1425 {
1426 args->m_error.SetErrorToGenericError();
1427 args->m_error.SetErrorString("No such process.");
1428 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001429
1430 FINISH:
1431 return args->m_error.Success();
1432}
1433
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001434bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001435ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1436{
1437 long ptrace_opts = 0;
1438
1439 // Have the child raise an event on exit. This is used to keep the child in
1440 // limbo until it is destroyed.
1441 ptrace_opts |= PTRACE_O_TRACEEXIT;
1442
1443 // Have the tracer trace threads which spawn in the inferior process.
1444 // TODO: if we want to support tracing the inferiors' child, add the
1445 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1446 ptrace_opts |= PTRACE_O_TRACECLONE;
1447
1448 // Have the tracer notify us before execve returns
1449 // (needed to disable legacy SIGTRAP generation)
1450 ptrace_opts |= PTRACE_O_TRACEEXEC;
1451
1452 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1453}
1454
1455bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001456ProcessMonitor::MonitorCallback(void *callback_baton,
1457 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001458 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001459 int signal,
1460 int status)
1461{
1462 ProcessMessage message;
1463 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001464 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001465 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001466 bool stop_monitoring;
1467 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001468 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001469
Andrew Kaylor93132f52013-05-28 23:04:25 +00001470 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1471
1472 if (exited)
1473 {
1474 if (log)
1475 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1476 message = ProcessMessage::Exit(pid, status);
1477 process->SendMessage(message);
1478 return pid == process->GetID();
1479 }
1480
Daniel Maleaa35970a2012-11-23 18:09:58 +00001481 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1482 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001483 if (log)
1484 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001485 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1486 if (!monitor->Resume(pid, SIGSTOP)) {
1487 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1488 }
1489 stop_monitoring = false;
1490 } else {
1491 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1492 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001493 // stop the monitor thread if this is the main pid.
1494 if (log)
1495 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1496 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1497 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001498 // If we are going to stop monitoring, we need to notify our process object
1499 if (stop_monitoring)
1500 {
1501 message = ProcessMessage::Exit(pid, status);
1502 process->SendMessage(message);
1503 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001504 }
1505 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001506 else {
1507 switch (info.si_signo)
1508 {
1509 case SIGTRAP:
1510 message = MonitorSIGTRAP(monitor, &info, pid);
1511 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001512
Stephen Wilson84ffe702011-03-30 15:55:52 +00001513 default:
1514 message = MonitorSignal(monitor, &info, pid);
1515 break;
1516 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001517
Stephen Wilson84ffe702011-03-30 15:55:52 +00001518 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001519 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001520 }
1521
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001522 return stop_monitoring;
1523}
1524
1525ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001526ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001527 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001528{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001529 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001530
Andrew Kaylor93132f52013-05-28 23:04:25 +00001531 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1532
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001533 assert(monitor);
1534 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001535
Stephen Wilson84ffe702011-03-30 15:55:52 +00001536 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001537 {
1538 default:
1539 assert(false && "Unexpected SIGTRAP code!");
1540 break;
1541
Matt Kopeca360d7e2013-05-17 19:27:47 +00001542 // TODO: these two cases are required if we want to support tracing
1543 // of the inferiors' children
1544 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1545 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1546
Matt Kopec650648f2013-01-08 16:30:18 +00001547 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1548 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001549 if (log)
1550 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1551
Matt Kopec650648f2013-01-08 16:30:18 +00001552 unsigned long tid = 0;
1553 if (!monitor->GetEventMessage(pid, &tid))
1554 tid = -1;
1555 message = ProcessMessage::NewThread(pid, tid);
1556 break;
1557 }
1558
Matt Kopeca360d7e2013-05-17 19:27:47 +00001559 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001560 if (log)
1561 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1562
1563 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001564 break;
1565
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001566 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1567 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001568 // The inferior process or one of its threads is about to exit.
1569 // Maintain the process or thread in a state of "limbo" until we are
1570 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001571 unsigned long data = 0;
1572 if (!monitor->GetEventMessage(pid, &data))
1573 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001574 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001575 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001576 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001577 break;
1578 }
1579
1580 case 0:
1581 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001582 if (log)
1583 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001584 message = ProcessMessage::Trace(pid);
1585 break;
1586
1587 case SI_KERNEL:
1588 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001589 if (log)
1590 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001591 message = ProcessMessage::Break(pid);
1592 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001593
1594 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001595 if (log)
1596 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001597 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1598 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001599
1600 case SIGTRAP:
1601 case (SIGTRAP | 0x80):
1602 if (log)
1603 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1604 // Ignore these signals until we know more about them
1605 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001606 }
1607
1608 return message;
1609}
1610
Stephen Wilson84ffe702011-03-30 15:55:52 +00001611ProcessMessage
1612ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001613 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001614{
1615 ProcessMessage message;
1616 int signo = info->si_signo;
1617
Andrew Kaylor93132f52013-05-28 23:04:25 +00001618 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1619
Stephen Wilson84ffe702011-03-30 15:55:52 +00001620 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1621 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1622 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1623 //
1624 // IOW, user generated signals never generate what we consider to be a
1625 // "crash".
1626 //
1627 // Similarly, ACK signals generated by this monitor.
1628 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1629 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001630 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001631 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001632 __FUNCTION__,
1633 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1634 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1635 info->si_pid);
1636
Stephen Wilson84ffe702011-03-30 15:55:52 +00001637 if (info->si_pid == getpid())
1638 return ProcessMessage::SignalDelivered(pid, signo);
1639 else
1640 return ProcessMessage::Signal(pid, signo);
1641 }
1642
Andrew Kaylor93132f52013-05-28 23:04:25 +00001643 if (log)
1644 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1645
Stephen Wilson84ffe702011-03-30 15:55:52 +00001646 if (signo == SIGSEGV) {
1647 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1648 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1649 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1650 }
1651
1652 if (signo == SIGILL) {
1653 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1654 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1655 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1656 }
1657
1658 if (signo == SIGFPE) {
1659 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1660 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1661 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1662 }
1663
1664 if (signo == SIGBUS) {
1665 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1666 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1667 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1668 }
1669
1670 // Everything else is "normal" and does not require any special action on
1671 // our part.
1672 return ProcessMessage::Signal(pid, signo);
1673}
1674
Andrew Kaylord4d54992013-09-17 00:30:24 +00001675// On Linux, when a new thread is created, we receive to notifications,
1676// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1677// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1678// the new child thread indicating that it has is stopped because we attached.
1679// We have no guarantee of the order in which these arrive, but we need both
1680// before we are ready to proceed. We currently keep a list of threads which
1681// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1682// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1683// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1684
1685bool
1686ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1687{
1688 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1689 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001690 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001691
1692 // Wait for the thread to stop
1693 while (true)
1694 {
1695 int status = -1;
1696 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001697 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001698 lldb::pid_t wait_pid = waitpid(tid, &status, __WALL);
1699 if (status == -1)
1700 {
1701 // If we got interrupted by a signal (in our process, not the
1702 // inferior) try again.
1703 if (errno == EINTR)
1704 continue;
1705 else
1706 {
1707 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001708 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001709 return false; // This is bad, but there's nothing we can do.
1710 }
1711 }
1712
1713 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001714 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001715
1716 assert(wait_pid == tid);
1717
1718 siginfo_t info;
1719 int ptrace_err;
1720 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1721 {
1722 if (log)
1723 {
1724 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1725 }
1726 return false;
1727 }
1728
1729 // If this is a thread exit, we won't get any more information.
1730 if (WIFEXITED(status))
1731 {
1732 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1733 if (wait_pid == tid)
1734 return true;
1735 continue;
1736 }
1737
1738 assert(info.si_code == SI_USER);
1739 assert(WSTOPSIG(status) == SIGSTOP);
1740
1741 if (log)
1742 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1743 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1744 return true;
1745 }
1746 return false;
1747}
1748
Andrew Kaylor93132f52013-05-28 23:04:25 +00001749bool
1750ProcessMonitor::StopThread(lldb::tid_t tid)
1751{
1752 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1753
1754 // FIXME: Try to use tgkill or tkill
1755 int ret = tgkill(m_pid, tid, SIGSTOP);
1756 if (log)
1757 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1758
1759 // This can happen if a thread exited while we were trying to stop it. That's OK.
1760 // We'll get the signal for that later.
1761 if (ret < 0)
1762 return false;
1763
1764 // Wait for the thread to stop
1765 while (true)
1766 {
1767 int status = -1;
1768 if (log)
1769 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
1770 lldb::pid_t wait_pid = ::waitpid (-1*m_pid, &status, __WALL);
1771 if (log)
1772 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d", __FUNCTION__, wait_pid, status);
1773
1774 if (wait_pid == -1)
1775 {
1776 // If we got interrupted by a signal (in our process, not the
1777 // inferior) try again.
1778 if (errno == EINTR)
1779 continue;
1780 else
1781 return false; // This is bad, but there's nothing we can do.
1782 }
1783
1784 // If this is a thread exit, we won't get any more information.
1785 if (WIFEXITED(status))
1786 {
1787 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1788 if (wait_pid == tid)
1789 return true;
1790 continue;
1791 }
1792
1793 siginfo_t info;
1794 int ptrace_err;
1795 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1796 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001797 // another signal causing a StopAllThreads may have been received
1798 // before wait_pid's group-stop was processed, handle it now
1799 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001800 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001801 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001802
Todd Fiala1b0539c2014-01-27 17:03:57 +00001803 if (log)
1804 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1805 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1806 if (!Resume(wait_pid, SIGSTOP)) {
1807 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1808 }
1809 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001810 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001811
1812 if (log)
1813 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001814 return false;
1815 }
1816
1817 // Handle events from other threads
1818 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001819 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64, __FUNCTION__, wait_pid);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001820
1821 ProcessMessage message;
1822 if (info.si_signo == SIGTRAP)
1823 message = MonitorSIGTRAP(this, &info, wait_pid);
1824 else
1825 message = MonitorSignal(this, &info, wait_pid);
1826
1827 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1828
1829 // When a new thread is created, we may get a SIGSTOP for the new thread
1830 // just before we get the SIGTRAP that we use to add the thread to our
1831 // process thread list. We don't need to worry about that signal here.
1832 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1833
1834 if (!thread)
1835 {
1836 m_process->SendMessage(message);
1837 continue;
1838 }
1839
1840 switch (message.GetKind())
1841 {
Michael Sartainc258b302013-09-18 15:32:06 +00001842 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001843 case ProcessMessage::eInvalidMessage:
1844 break;
1845
1846 // These need special handling because we don't want to send a
1847 // resume even if we already sent a SIGSTOP to this thread. In
1848 // this case the resume will cause the thread to disappear. It is
1849 // unlikely that we'll ever get eExitMessage here, but the same
1850 // reasoning applies.
1851 case ProcessMessage::eLimboMessage:
1852 case ProcessMessage::eExitMessage:
1853 if (log)
1854 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1855 // SendMessage will set the thread state as needed.
1856 m_process->SendMessage(message);
1857 // If this is the thread we're waiting for, stop waiting. Even
1858 // though this wasn't the signal we expected, it's the last
1859 // signal we'll see while this thread is alive.
1860 if (wait_pid == tid)
1861 return true;
1862 break;
1863
Matt Kopecb2910442013-07-09 15:09:45 +00001864 case ProcessMessage::eSignalMessage:
1865 if (log)
1866 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1867 if (WSTOPSIG(status) == SIGSTOP)
1868 {
1869 m_process->AddThreadForInitialStopIfNeeded(tid);
1870 thread->SetState(lldb::eStateStopped);
1871 }
1872 else
1873 {
1874 m_process->SendMessage(message);
1875 // This isn't the stop we were expecting, but the thread is
1876 // stopped. SendMessage will handle processing of this event,
1877 // but we need to resume here to get the stop we are waiting
1878 // for (otherwise the thread will stop again immediately when
1879 // we try to resume).
1880 if (wait_pid == tid)
1881 Resume(wait_pid, eResumeSignalNone);
1882 }
1883 break;
1884
Andrew Kaylor93132f52013-05-28 23:04:25 +00001885 case ProcessMessage::eSignalDeliveredMessage:
1886 // This is the stop we're expecting.
1887 if (wait_pid == tid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && info.si_code == SI_TKILL)
1888 {
1889 if (log)
1890 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1891 thread->SetState(lldb::eStateStopped);
1892 return true;
1893 }
1894 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001895 case ProcessMessage::eBreakpointMessage:
1896 case ProcessMessage::eTraceMessage:
1897 case ProcessMessage::eWatchpointMessage:
1898 case ProcessMessage::eCrashMessage:
1899 case ProcessMessage::eNewThreadMessage:
1900 if (log)
1901 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1902 // SendMessage will set the thread state as needed.
1903 m_process->SendMessage(message);
1904 // This isn't the stop we were expecting, but the thread is
1905 // stopped. SendMessage will handle processing of this event,
1906 // but we need to resume here to get the stop we are waiting
1907 // for (otherwise the thread will stop again immediately when
1908 // we try to resume).
1909 if (wait_pid == tid)
1910 Resume(wait_pid, eResumeSignalNone);
1911 break;
1912 }
1913 }
1914 return false;
1915}
1916
Stephen Wilson84ffe702011-03-30 15:55:52 +00001917ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001918ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001919{
1920 ProcessMessage::CrashReason reason;
1921 assert(info->si_signo == SIGSEGV);
1922
1923 reason = ProcessMessage::eInvalidCrashReason;
1924
Greg Clayton542e4072012-09-07 17:49:29 +00001925 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001926 {
1927 default:
1928 assert(false && "unexpected si_code for SIGSEGV");
1929 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001930 case SI_KERNEL:
1931 // Linux will occasionally send spurious SI_KERNEL codes.
1932 // (this is poorly documented in sigaction)
1933 // One way to get this is via unaligned SIMD loads.
1934 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1935 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001936 case SEGV_MAPERR:
1937 reason = ProcessMessage::eInvalidAddress;
1938 break;
1939 case SEGV_ACCERR:
1940 reason = ProcessMessage::ePrivilegedAddress;
1941 break;
1942 }
Greg Clayton542e4072012-09-07 17:49:29 +00001943
Stephen Wilson84ffe702011-03-30 15:55:52 +00001944 return reason;
1945}
1946
1947ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001948ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001949{
1950 ProcessMessage::CrashReason reason;
1951 assert(info->si_signo == SIGILL);
1952
1953 reason = ProcessMessage::eInvalidCrashReason;
1954
1955 switch (info->si_code)
1956 {
1957 default:
1958 assert(false && "unexpected si_code for SIGILL");
1959 break;
1960 case ILL_ILLOPC:
1961 reason = ProcessMessage::eIllegalOpcode;
1962 break;
1963 case ILL_ILLOPN:
1964 reason = ProcessMessage::eIllegalOperand;
1965 break;
1966 case ILL_ILLADR:
1967 reason = ProcessMessage::eIllegalAddressingMode;
1968 break;
1969 case ILL_ILLTRP:
1970 reason = ProcessMessage::eIllegalTrap;
1971 break;
1972 case ILL_PRVOPC:
1973 reason = ProcessMessage::ePrivilegedOpcode;
1974 break;
1975 case ILL_PRVREG:
1976 reason = ProcessMessage::ePrivilegedRegister;
1977 break;
1978 case ILL_COPROC:
1979 reason = ProcessMessage::eCoprocessorError;
1980 break;
1981 case ILL_BADSTK:
1982 reason = ProcessMessage::eInternalStackError;
1983 break;
1984 }
1985
1986 return reason;
1987}
1988
1989ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001990ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001991{
1992 ProcessMessage::CrashReason reason;
1993 assert(info->si_signo == SIGFPE);
1994
1995 reason = ProcessMessage::eInvalidCrashReason;
1996
1997 switch (info->si_code)
1998 {
1999 default:
2000 assert(false && "unexpected si_code for SIGFPE");
2001 break;
2002 case FPE_INTDIV:
2003 reason = ProcessMessage::eIntegerDivideByZero;
2004 break;
2005 case FPE_INTOVF:
2006 reason = ProcessMessage::eIntegerOverflow;
2007 break;
2008 case FPE_FLTDIV:
2009 reason = ProcessMessage::eFloatDivideByZero;
2010 break;
2011 case FPE_FLTOVF:
2012 reason = ProcessMessage::eFloatOverflow;
2013 break;
2014 case FPE_FLTUND:
2015 reason = ProcessMessage::eFloatUnderflow;
2016 break;
2017 case FPE_FLTRES:
2018 reason = ProcessMessage::eFloatInexactResult;
2019 break;
2020 case FPE_FLTINV:
2021 reason = ProcessMessage::eFloatInvalidOperation;
2022 break;
2023 case FPE_FLTSUB:
2024 reason = ProcessMessage::eFloatSubscriptRange;
2025 break;
2026 }
2027
2028 return reason;
2029}
2030
2031ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002032ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002033{
2034 ProcessMessage::CrashReason reason;
2035 assert(info->si_signo == SIGBUS);
2036
2037 reason = ProcessMessage::eInvalidCrashReason;
2038
2039 switch (info->si_code)
2040 {
2041 default:
2042 assert(false && "unexpected si_code for SIGBUS");
2043 break;
2044 case BUS_ADRALN:
2045 reason = ProcessMessage::eIllegalAlignment;
2046 break;
2047 case BUS_ADRERR:
2048 reason = ProcessMessage::eIllegalAddress;
2049 break;
2050 case BUS_OBJERR:
2051 reason = ProcessMessage::eHardwareError;
2052 break;
2053 }
2054
2055 return reason;
2056}
2057
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002058void
Johnny Chen25e68e32011-06-14 19:19:50 +00002059ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002060{
Stephen Wilson570243b2011-01-19 01:37:06 +00002061 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002062
Stephen Wilson570243b2011-01-19 01:37:06 +00002063 // We are finised with the arguments and are ready to go. Sync with the
2064 // parent thread and start serving operations on the inferior.
2065 sem_post(&args->m_semaphore);
2066
Michael Sartain704bf892013-10-09 01:28:57 +00002067 for(;;)
2068 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002069 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002070 if (sem_wait(&monitor->m_operation_pending))
2071 {
2072 if (errno == EINTR)
2073 continue;
2074 assert(false && "Unexpected errno from sem_wait");
2075 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002076
Daniel Malea1efb4182013-09-16 23:12:18 +00002077 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002078
Daniel Malea1efb4182013-09-16 23:12:18 +00002079 // notify calling thread that operation is complete
2080 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002081 }
2082}
2083
2084void
2085ProcessMonitor::DoOperation(Operation *op)
2086{
Daniel Malea1efb4182013-09-16 23:12:18 +00002087 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002088
Daniel Malea1efb4182013-09-16 23:12:18 +00002089 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002090
Daniel Malea1efb4182013-09-16 23:12:18 +00002091 // notify operation thread that an operation is ready to be processed
2092 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002093
Daniel Malea1efb4182013-09-16 23:12:18 +00002094 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002095 while (sem_wait(&m_operation_done))
2096 {
2097 if (errno == EINTR)
2098 continue;
2099 assert(false && "Unexpected errno from sem_wait");
2100 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002101}
2102
2103size_t
2104ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2105 Error &error)
2106{
2107 size_t result;
2108 ReadOperation op(vm_addr, buf, size, error, result);
2109 DoOperation(&op);
2110 return result;
2111}
2112
2113size_t
2114ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2115 lldb_private::Error &error)
2116{
2117 size_t result;
2118 WriteOperation op(vm_addr, buf, size, error, result);
2119 DoOperation(&op);
2120 return result;
2121}
2122
2123bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002124ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002125 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002126{
2127 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002128 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002129 DoOperation(&op);
2130 return result;
2131}
2132
2133bool
Matt Kopec7de48462013-03-06 17:20:48 +00002134ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002135 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002136{
2137 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002138 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002139 DoOperation(&op);
2140 return result;
2141}
2142
2143bool
Matt Kopec7de48462013-03-06 17:20:48 +00002144ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002145{
2146 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002147 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002148 DoOperation(&op);
2149 return result;
2150}
2151
2152bool
Matt Kopec7de48462013-03-06 17:20:48 +00002153ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002154{
2155 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002156 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002157 DoOperation(&op);
2158 return result;
2159}
2160
2161bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002162ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2163{
2164 bool result;
2165 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2166 DoOperation(&op);
2167 return result;
2168}
2169
2170bool
Matt Kopec7de48462013-03-06 17:20:48 +00002171ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002172{
2173 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002174 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002175 DoOperation(&op);
2176 return result;
2177}
2178
2179bool
Matt Kopec7de48462013-03-06 17:20:48 +00002180ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002181{
2182 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002183 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002184 DoOperation(&op);
2185 return result;
2186}
2187
2188bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002189ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2190{
2191 bool result;
2192 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2193 DoOperation(&op);
2194 return result;
2195}
2196
2197bool
Richard Mitton0a558352013-10-17 21:14:00 +00002198ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2199{
2200 bool result;
2201 ReadThreadPointerOperation op(tid, &value, result);
2202 DoOperation(&op);
2203 return result;
2204}
2205
2206bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002207ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002208{
2209 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002210 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2211
2212 if (log)
2213 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2214 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002215 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002216 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002217 if (log)
2218 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002219 return result;
2220}
2221
2222bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002223ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002224{
2225 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002226 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002227 DoOperation(&op);
2228 return result;
2229}
2230
2231bool
2232ProcessMonitor::BringProcessIntoLimbo()
2233{
2234 bool result;
2235 KillOperation op(result);
2236 DoOperation(&op);
2237 return result;
2238}
2239
2240bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002241ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002242{
2243 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002244 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002245 DoOperation(&op);
2246 return result;
2247}
2248
2249bool
2250ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2251{
2252 bool result;
2253 EventMessageOperation op(tid, message, result);
2254 DoOperation(&op);
2255 return result;
2256}
2257
Greg Clayton743ecf42012-10-16 20:20:18 +00002258lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002259ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002260{
Greg Clayton28041352011-11-29 20:50:10 +00002261 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002262 if (tid != LLDB_INVALID_THREAD_ID)
2263 {
2264 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002265 DoOperation(&op);
2266 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002267 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002268}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002269
2270bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002271ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2272{
Peter Collingbourne62343202011-06-14 03:55:54 +00002273 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002274
2275 if (target_fd == -1)
2276 return false;
2277
Peter Collingbourne62343202011-06-14 03:55:54 +00002278 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002279}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002280
2281void
2282ProcessMonitor::StopMonitoringChildProcess()
2283{
2284 lldb::thread_result_t thread_result;
2285
Stephen Wilsond4182f42011-02-09 20:10:35 +00002286 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002287 {
2288 Host::ThreadCancel(m_monitor_thread, NULL);
2289 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2290 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2291 }
2292}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002293
2294void
2295ProcessMonitor::StopMonitor()
2296{
2297 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002298 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002299 sem_destroy(&m_operation_pending);
2300 sem_destroy(&m_operation_done);
2301
Andrew Kaylor5e268992013-09-14 00:17:31 +00002302 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2303 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2304 // the descriptor to a ConnectionFileDescriptor object. Consequently
2305 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002306}
2307
2308void
Greg Clayton743ecf42012-10-16 20:20:18 +00002309ProcessMonitor::StopOpThread()
2310{
2311 lldb::thread_result_t result;
2312
2313 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2314 return;
2315
2316 Host::ThreadCancel(m_operation_thread, NULL);
2317 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002318 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002319}