blob: 8583f67ab0e73d3b9849b8ed70dcd9351df30a65 [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//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000907/// @class DetachOperation
908/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000909class DetachOperation : public Operation
910{
911public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000912 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000913
914 void Execute(ProcessMonitor *monitor);
915
916private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000917 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000918 Error &m_error;
919};
920
921void
922DetachOperation::Execute(ProcessMonitor *monitor)
923{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000924 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000925 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000926}
927
Johnny Chen25e68e32011-06-14 19:19:50 +0000928ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
929 : m_monitor(monitor)
930{
931 sem_init(&m_semaphore, 0, 0);
932}
933
934ProcessMonitor::OperationArgs::~OperationArgs()
935{
936 sem_destroy(&m_semaphore);
937}
938
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000939ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
940 lldb_private::Module *module,
941 char const **argv,
942 char const **envp,
943 const char *stdin_path,
944 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000945 const char *stderr_path,
946 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000947 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000948 m_module(module),
949 m_argv(argv),
950 m_envp(envp),
951 m_stdin_path(stdin_path),
952 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000953 m_stderr_path(stderr_path),
954 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000955
956ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000957{ }
958
959ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
960 lldb::pid_t pid)
961 : OperationArgs(monitor), m_pid(pid) { }
962
963ProcessMonitor::AttachArgs::~AttachArgs()
964{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000965
966//------------------------------------------------------------------------------
967/// The basic design of the ProcessMonitor is built around two threads.
968///
969/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
970/// for changes in the debugee state. When a change is detected a
971/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
972/// "drives" state changes in the debugger.
973///
974/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000975/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000976/// operations such as register reads/writes, stepping, etc. See the comments
977/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000978ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000979 Module *module,
980 const char *argv[],
981 const char *envp[],
982 const char *stdin_path,
983 const char *stdout_path,
984 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000985 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000986 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000987 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000988 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000989 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000990 m_pid(LLDB_INVALID_PROCESS_ID),
991 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +0000992 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000993{
Daniel Malea1efb4182013-09-16 23:12:18 +0000994 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
995 stdin_path, stdout_path, stderr_path,
996 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +0000997
Daniel Malea1efb4182013-09-16 23:12:18 +0000998 sem_init(&m_operation_pending, 0, 0);
999 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001000
Johnny Chen25e68e32011-06-14 19:19:50 +00001001 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001002 if (!error.Success())
1003 return;
1004
1005WAIT_AGAIN:
1006 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001007 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001008 {
1009 if (errno == EINTR)
1010 goto WAIT_AGAIN;
1011 else
1012 {
1013 error.SetErrorToErrno();
1014 return;
1015 }
1016 }
1017
1018 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001019 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001020 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001021 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001022 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001023 return;
1024 }
1025
1026 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001027 m_monitor_thread = Host::StartMonitoringChildProcess(
1028 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001029 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001030 {
1031 error.SetErrorToGenericError();
1032 error.SetErrorString("Process launch failed.");
1033 return;
1034 }
1035}
1036
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001037ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001038 lldb::pid_t pid,
1039 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001040 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001041 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001042 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001043 m_pid(LLDB_INVALID_PROCESS_ID),
1044 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001045 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001046{
Daniel Malea1efb4182013-09-16 23:12:18 +00001047 sem_init(&m_operation_pending, 0, 0);
1048 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001049
Daniel Malea1efb4182013-09-16 23:12:18 +00001050 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001051
1052 StartAttachOpThread(args.get(), error);
1053 if (!error.Success())
1054 return;
1055
1056WAIT_AGAIN:
1057 // Wait for the operation thread to initialize.
1058 if (sem_wait(&args->m_semaphore))
1059 {
1060 if (errno == EINTR)
1061 goto WAIT_AGAIN;
1062 else
1063 {
1064 error.SetErrorToErrno();
1065 return;
1066 }
1067 }
1068
Greg Clayton743ecf42012-10-16 20:20:18 +00001069 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001070 if (!args->m_error.Success())
1071 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001072 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001073 error = args->m_error;
1074 return;
1075 }
1076
1077 // Finally, start monitoring the child process for change in state.
1078 m_monitor_thread = Host::StartMonitoringChildProcess(
1079 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1080 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1081 {
1082 error.SetErrorToGenericError();
1083 error.SetErrorString("Process attach failed.");
1084 return;
1085 }
1086}
1087
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001088ProcessMonitor::~ProcessMonitor()
1089{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001090 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001091}
1092
1093//------------------------------------------------------------------------------
1094// Thread setup and tear down.
1095void
Johnny Chen25e68e32011-06-14 19:19:50 +00001096ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001097{
1098 static const char *g_thread_name = "lldb.process.linux.operation";
1099
Stephen Wilsond4182f42011-02-09 20:10:35 +00001100 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001101 return;
1102
1103 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001104 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001105}
1106
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001107void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001108ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001109{
1110 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1111
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001112 if (!Launch(args)) {
1113 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001114 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001115 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001116
Stephen Wilson570243b2011-01-19 01:37:06 +00001117 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001118 return NULL;
1119}
1120
1121bool
1122ProcessMonitor::Launch(LaunchArgs *args)
1123{
1124 ProcessMonitor *monitor = args->m_monitor;
1125 ProcessLinux &process = monitor->GetProcess();
1126 const char **argv = args->m_argv;
1127 const char **envp = args->m_envp;
1128 const char *stdin_path = args->m_stdin_path;
1129 const char *stdout_path = args->m_stdout_path;
1130 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001131 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001132
1133 lldb_utility::PseudoTerminal terminal;
1134 const size_t err_len = 1024;
1135 char err_str[err_len];
1136 lldb::pid_t pid;
1137
1138 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001139 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001140
Stephen Wilson57740ec2011-01-15 00:12:41 +00001141 // Propagate the environment if one is not supplied.
1142 if (envp == NULL || envp[0] == NULL)
1143 envp = const_cast<const char **>(environ);
1144
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001145 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001146 {
1147 args->m_error.SetErrorToGenericError();
1148 args->m_error.SetErrorString("Process fork failed.");
1149 goto FINISH;
1150 }
1151
Peter Collingbourne6a520222011-06-14 03:55:58 +00001152 // Recognized child exit status codes.
1153 enum {
1154 ePtraceFailed = 1,
1155 eDupStdinFailed,
1156 eDupStdoutFailed,
1157 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001158 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001159 eExecFailed,
1160 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001161 };
1162
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163 // Child process.
1164 if (pid == 0)
1165 {
1166 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001167 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001168 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169
1170 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001171 if (setgid(getgid()) != 0)
1172 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001173
1174 // Let us have our own process group.
1175 setpgid(0, 0);
1176
Greg Clayton710dd5a2011-01-08 20:28:42 +00001177 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001178 //
1179 // FIXME: If two or more of the paths are the same we needlessly open
1180 // the same file multiple times.
1181 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001182 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001183 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001184
1185 if (stdout_path != NULL && stdout_path[0])
1186 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001187 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001188
1189 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001190 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001191 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192
Daniel Malea6217d2a2013-01-08 14:49:22 +00001193 // Change working directory
1194 if (working_dir != NULL && working_dir[0])
1195 if (0 != ::chdir(working_dir))
1196 exit(eChdirFailed);
1197
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001198 // Execute. We should never return.
1199 execve(argv[0],
1200 const_cast<char *const *>(argv),
1201 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001202 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001203 }
1204
1205 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001206 lldb::pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001208 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001209 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001210 args->m_error.SetErrorToErrno();
1211 goto FINISH;
1212 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001213 else if (WIFEXITED(status))
1214 {
1215 // open, dup or execve likely failed for some reason.
1216 args->m_error.SetErrorToGenericError();
1217 switch (WEXITSTATUS(status))
1218 {
Greg Clayton542e4072012-09-07 17:49:29 +00001219 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001220 args->m_error.SetErrorString("Child ptrace failed.");
1221 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001222 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001223 args->m_error.SetErrorString("Child open stdin failed.");
1224 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001225 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001226 args->m_error.SetErrorString("Child open stdout failed.");
1227 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001228 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001229 args->m_error.SetErrorString("Child open stderr failed.");
1230 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001231 case eChdirFailed:
1232 args->m_error.SetErrorString("Child failed to set working directory.");
1233 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001234 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001235 args->m_error.SetErrorString("Child exec failed.");
1236 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001237 case eSetGidFailed:
1238 args->m_error.SetErrorString("Child setgid failed.");
1239 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001240 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001241 args->m_error.SetErrorString("Child returned unknown exit status.");
1242 break;
1243 }
1244 goto FINISH;
1245 }
1246 assert(WIFSTOPPED(status) && wpid == pid &&
1247 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001248
Matt Kopec085d6ce2013-05-31 22:00:07 +00001249 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001250 {
1251 args->m_error.SetErrorToErrno();
1252 goto FINISH;
1253 }
1254
1255 // Release the master terminal descriptor and pass it off to the
1256 // ProcessMonitor instance. Similarly stash the inferior pid.
1257 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1258 monitor->m_pid = pid;
1259
Stephen Wilson26977162011-03-23 02:14:42 +00001260 // Set the terminal fd to be in non blocking mode (it simplifies the
1261 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1262 // descriptor to read from).
1263 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1264 goto FINISH;
1265
Johnny Chen30213ff2012-01-05 19:17:38 +00001266 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001267 // FIXME: should we be letting UpdateThreadList handle this?
1268 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001269 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001270
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001271 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001272 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001273 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001274
Matt Kopecb2910442013-07-09 15:09:45 +00001275 process.AddThreadForInitialStopIfNeeded(pid);
1276
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001277 // Let our process instance know the thread has stopped.
1278 process.SendMessage(ProcessMessage::Trace(pid));
1279
1280FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001281 return args->m_error.Success();
1282}
1283
Johnny Chen25e68e32011-06-14 19:19:50 +00001284void
1285ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1286{
1287 static const char *g_thread_name = "lldb.process.linux.operation";
1288
1289 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1290 return;
1291
1292 m_operation_thread =
1293 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1294}
1295
Johnny Chen25e68e32011-06-14 19:19:50 +00001296void *
1297ProcessMonitor::AttachOpThread(void *arg)
1298{
1299 AttachArgs *args = static_cast<AttachArgs*>(arg);
1300
Greg Clayton743ecf42012-10-16 20:20:18 +00001301 if (!Attach(args)) {
1302 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001303 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001304 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001305
1306 ServeOperation(args);
1307 return NULL;
1308}
1309
1310bool
1311ProcessMonitor::Attach(AttachArgs *args)
1312{
1313 lldb::pid_t pid = args->m_pid;
1314
1315 ProcessMonitor *monitor = args->m_monitor;
1316 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001317 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001318 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001319
Matt Kopec085d6ce2013-05-31 22:00:07 +00001320 // Use a map to keep track of the threads which we have attached/need to attach.
1321 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001322 if (pid <= 1)
1323 {
1324 args->m_error.SetErrorToGenericError();
1325 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1326 goto FINISH;
1327 }
1328
Matt Kopec085d6ce2013-05-31 22:00:07 +00001329 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001330 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001331 for (Host::TidMap::iterator it = tids_to_attach.begin();
1332 it != tids_to_attach.end(); ++it)
1333 {
1334 if (it->second == false)
1335 {
1336 lldb::tid_t tid = it->first;
1337
1338 // Attach to the requested process.
1339 // An attach will cause the thread to stop with a SIGSTOP.
1340 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1341 {
1342 // No such thread. The thread may have exited.
1343 // More error handling may be needed.
1344 if (errno == ESRCH)
1345 {
1346 tids_to_attach.erase(it);
1347 continue;
1348 }
1349 else
1350 {
1351 args->m_error.SetErrorToErrno();
1352 goto FINISH;
1353 }
1354 }
1355
1356 int status;
1357 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1358 // At this point we should have a thread stopped if waitpid succeeds.
1359 if ((status = waitpid(tid, NULL, __WALL)) < 0)
1360 {
1361 // No such thread. The thread may have exited.
1362 // More error handling may be needed.
1363 if (errno == ESRCH)
1364 {
1365 tids_to_attach.erase(it);
1366 continue;
1367 }
1368 else
1369 {
1370 args->m_error.SetErrorToErrno();
1371 goto FINISH;
1372 }
1373 }
1374
1375 if (!SetDefaultPtraceOpts(tid))
1376 {
1377 args->m_error.SetErrorToErrno();
1378 goto FINISH;
1379 }
1380
1381 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001382 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001383
Matt Kopec085d6ce2013-05-31 22:00:07 +00001384 if (log)
1385 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1386 process.GetThreadList().AddThread(inferior);
1387 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001388 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001389 }
1390 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001391 }
1392
Matt Kopec085d6ce2013-05-31 22:00:07 +00001393 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001394 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001395 monitor->m_pid = pid;
1396 // Let our process instance know the thread has stopped.
1397 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001398 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001399 else
1400 {
1401 args->m_error.SetErrorToGenericError();
1402 args->m_error.SetErrorString("No such process.");
1403 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001404
1405 FINISH:
1406 return args->m_error.Success();
1407}
1408
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001409bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001410ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1411{
1412 long ptrace_opts = 0;
1413
1414 // Have the child raise an event on exit. This is used to keep the child in
1415 // limbo until it is destroyed.
1416 ptrace_opts |= PTRACE_O_TRACEEXIT;
1417
1418 // Have the tracer trace threads which spawn in the inferior process.
1419 // TODO: if we want to support tracing the inferiors' child, add the
1420 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1421 ptrace_opts |= PTRACE_O_TRACECLONE;
1422
1423 // Have the tracer notify us before execve returns
1424 // (needed to disable legacy SIGTRAP generation)
1425 ptrace_opts |= PTRACE_O_TRACEEXEC;
1426
1427 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1428}
1429
1430bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001431ProcessMonitor::MonitorCallback(void *callback_baton,
1432 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001433 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001434 int signal,
1435 int status)
1436{
1437 ProcessMessage message;
1438 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001439 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001440 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001441 bool stop_monitoring;
1442 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001443 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001444
Andrew Kaylor93132f52013-05-28 23:04:25 +00001445 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1446
1447 if (exited)
1448 {
1449 if (log)
1450 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1451 message = ProcessMessage::Exit(pid, status);
1452 process->SendMessage(message);
1453 return pid == process->GetID();
1454 }
1455
Daniel Maleaa35970a2012-11-23 18:09:58 +00001456 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1457 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001458 if (log)
1459 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001460 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1461 if (!monitor->Resume(pid, SIGSTOP)) {
1462 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1463 }
1464 stop_monitoring = false;
1465 } else {
1466 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1467 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001468 // stop the monitor thread if this is the main pid.
1469 if (log)
1470 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1471 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1472 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001473 // If we are going to stop monitoring, we need to notify our process object
1474 if (stop_monitoring)
1475 {
1476 message = ProcessMessage::Exit(pid, status);
1477 process->SendMessage(message);
1478 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001479 }
1480 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001481 else {
1482 switch (info.si_signo)
1483 {
1484 case SIGTRAP:
1485 message = MonitorSIGTRAP(monitor, &info, pid);
1486 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001487
Stephen Wilson84ffe702011-03-30 15:55:52 +00001488 default:
1489 message = MonitorSignal(monitor, &info, pid);
1490 break;
1491 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001492
Stephen Wilson84ffe702011-03-30 15:55:52 +00001493 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001494 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001495 }
1496
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001497 return stop_monitoring;
1498}
1499
1500ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001501ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001502 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001503{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001504 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001505
Andrew Kaylor93132f52013-05-28 23:04:25 +00001506 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1507
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001508 assert(monitor);
1509 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001510
Stephen Wilson84ffe702011-03-30 15:55:52 +00001511 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001512 {
1513 default:
1514 assert(false && "Unexpected SIGTRAP code!");
1515 break;
1516
Matt Kopeca360d7e2013-05-17 19:27:47 +00001517 // TODO: these two cases are required if we want to support tracing
1518 // of the inferiors' children
1519 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1520 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1521
Matt Kopec650648f2013-01-08 16:30:18 +00001522 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1523 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001524 if (log)
1525 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1526
Matt Kopec650648f2013-01-08 16:30:18 +00001527 unsigned long tid = 0;
1528 if (!monitor->GetEventMessage(pid, &tid))
1529 tid = -1;
1530 message = ProcessMessage::NewThread(pid, tid);
1531 break;
1532 }
1533
Matt Kopeca360d7e2013-05-17 19:27:47 +00001534 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001535 if (log)
1536 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1537
1538 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001539 break;
1540
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001541 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1542 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001543 // The inferior process or one of its threads is about to exit.
1544 // Maintain the process or thread in a state of "limbo" until we are
1545 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001546 unsigned long data = 0;
1547 if (!monitor->GetEventMessage(pid, &data))
1548 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001549 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001550 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001551 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001552 break;
1553 }
1554
1555 case 0:
1556 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001557 if (log)
1558 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001559 message = ProcessMessage::Trace(pid);
1560 break;
1561
1562 case SI_KERNEL:
1563 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001564 if (log)
1565 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001566 message = ProcessMessage::Break(pid);
1567 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001568
1569 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001570 if (log)
1571 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001572 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1573 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001574
1575 case SIGTRAP:
1576 case (SIGTRAP | 0x80):
1577 if (log)
1578 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1579 // Ignore these signals until we know more about them
1580 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001581 }
1582
1583 return message;
1584}
1585
Stephen Wilson84ffe702011-03-30 15:55:52 +00001586ProcessMessage
1587ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001588 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001589{
1590 ProcessMessage message;
1591 int signo = info->si_signo;
1592
Andrew Kaylor93132f52013-05-28 23:04:25 +00001593 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1594
Stephen Wilson84ffe702011-03-30 15:55:52 +00001595 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1596 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1597 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1598 //
1599 // IOW, user generated signals never generate what we consider to be a
1600 // "crash".
1601 //
1602 // Similarly, ACK signals generated by this monitor.
1603 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1604 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001605 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001606 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001607 __FUNCTION__,
1608 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1609 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1610 info->si_pid);
1611
Stephen Wilson84ffe702011-03-30 15:55:52 +00001612 if (info->si_pid == getpid())
1613 return ProcessMessage::SignalDelivered(pid, signo);
1614 else
1615 return ProcessMessage::Signal(pid, signo);
1616 }
1617
Andrew Kaylor93132f52013-05-28 23:04:25 +00001618 if (log)
1619 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1620
Stephen Wilson84ffe702011-03-30 15:55:52 +00001621 if (signo == SIGSEGV) {
1622 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1623 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1624 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1625 }
1626
1627 if (signo == SIGILL) {
1628 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1629 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1630 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1631 }
1632
1633 if (signo == SIGFPE) {
1634 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1635 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1636 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1637 }
1638
1639 if (signo == SIGBUS) {
1640 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1641 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1642 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1643 }
1644
1645 // Everything else is "normal" and does not require any special action on
1646 // our part.
1647 return ProcessMessage::Signal(pid, signo);
1648}
1649
Andrew Kaylord4d54992013-09-17 00:30:24 +00001650// On Linux, when a new thread is created, we receive to notifications,
1651// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1652// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1653// the new child thread indicating that it has is stopped because we attached.
1654// We have no guarantee of the order in which these arrive, but we need both
1655// before we are ready to proceed. We currently keep a list of threads which
1656// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1657// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1658// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1659
1660bool
1661ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1662{
1663 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1664 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001665 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001666
1667 // Wait for the thread to stop
1668 while (true)
1669 {
1670 int status = -1;
1671 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001672 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001673 lldb::pid_t wait_pid = waitpid(tid, &status, __WALL);
1674 if (status == -1)
1675 {
1676 // If we got interrupted by a signal (in our process, not the
1677 // inferior) try again.
1678 if (errno == EINTR)
1679 continue;
1680 else
1681 {
1682 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001683 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001684 return false; // This is bad, but there's nothing we can do.
1685 }
1686 }
1687
1688 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001689 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001690
1691 assert(wait_pid == tid);
1692
1693 siginfo_t info;
1694 int ptrace_err;
1695 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1696 {
1697 if (log)
1698 {
1699 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1700 }
1701 return false;
1702 }
1703
1704 // If this is a thread exit, we won't get any more information.
1705 if (WIFEXITED(status))
1706 {
1707 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1708 if (wait_pid == tid)
1709 return true;
1710 continue;
1711 }
1712
1713 assert(info.si_code == SI_USER);
1714 assert(WSTOPSIG(status) == SIGSTOP);
1715
1716 if (log)
1717 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1718 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1719 return true;
1720 }
1721 return false;
1722}
1723
Andrew Kaylor93132f52013-05-28 23:04:25 +00001724bool
1725ProcessMonitor::StopThread(lldb::tid_t tid)
1726{
1727 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1728
1729 // FIXME: Try to use tgkill or tkill
1730 int ret = tgkill(m_pid, tid, SIGSTOP);
1731 if (log)
1732 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1733
1734 // This can happen if a thread exited while we were trying to stop it. That's OK.
1735 // We'll get the signal for that later.
1736 if (ret < 0)
1737 return false;
1738
1739 // Wait for the thread to stop
1740 while (true)
1741 {
1742 int status = -1;
1743 if (log)
1744 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
1745 lldb::pid_t wait_pid = ::waitpid (-1*m_pid, &status, __WALL);
1746 if (log)
1747 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d", __FUNCTION__, wait_pid, status);
1748
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001749 if (wait_pid == static_cast<lldb::pid_t>(-1))
Andrew Kaylor93132f52013-05-28 23:04:25 +00001750 {
1751 // If we got interrupted by a signal (in our process, not the
1752 // inferior) try again.
1753 if (errno == EINTR)
1754 continue;
1755 else
1756 return false; // This is bad, but there's nothing we can do.
1757 }
1758
1759 // If this is a thread exit, we won't get any more information.
1760 if (WIFEXITED(status))
1761 {
1762 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1763 if (wait_pid == tid)
1764 return true;
1765 continue;
1766 }
1767
1768 siginfo_t info;
1769 int ptrace_err;
1770 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1771 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001772 // another signal causing a StopAllThreads may have been received
1773 // before wait_pid's group-stop was processed, handle it now
1774 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001775 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001776 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001777
Todd Fiala1b0539c2014-01-27 17:03:57 +00001778 if (log)
1779 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1780 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1781 if (!Resume(wait_pid, SIGSTOP)) {
1782 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1783 }
1784 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001785 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001786
1787 if (log)
1788 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001789 return false;
1790 }
1791
1792 // Handle events from other threads
1793 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001794 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64, __FUNCTION__, wait_pid);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001795
1796 ProcessMessage message;
1797 if (info.si_signo == SIGTRAP)
1798 message = MonitorSIGTRAP(this, &info, wait_pid);
1799 else
1800 message = MonitorSignal(this, &info, wait_pid);
1801
1802 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1803
1804 // When a new thread is created, we may get a SIGSTOP for the new thread
1805 // just before we get the SIGTRAP that we use to add the thread to our
1806 // process thread list. We don't need to worry about that signal here.
1807 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1808
1809 if (!thread)
1810 {
1811 m_process->SendMessage(message);
1812 continue;
1813 }
1814
1815 switch (message.GetKind())
1816 {
Michael Sartainc258b302013-09-18 15:32:06 +00001817 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001818 case ProcessMessage::eInvalidMessage:
1819 break;
1820
1821 // These need special handling because we don't want to send a
1822 // resume even if we already sent a SIGSTOP to this thread. In
1823 // this case the resume will cause the thread to disappear. It is
1824 // unlikely that we'll ever get eExitMessage here, but the same
1825 // reasoning applies.
1826 case ProcessMessage::eLimboMessage:
1827 case ProcessMessage::eExitMessage:
1828 if (log)
1829 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1830 // SendMessage will set the thread state as needed.
1831 m_process->SendMessage(message);
1832 // If this is the thread we're waiting for, stop waiting. Even
1833 // though this wasn't the signal we expected, it's the last
1834 // signal we'll see while this thread is alive.
1835 if (wait_pid == tid)
1836 return true;
1837 break;
1838
Matt Kopecb2910442013-07-09 15:09:45 +00001839 case ProcessMessage::eSignalMessage:
1840 if (log)
1841 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1842 if (WSTOPSIG(status) == SIGSTOP)
1843 {
1844 m_process->AddThreadForInitialStopIfNeeded(tid);
1845 thread->SetState(lldb::eStateStopped);
1846 }
1847 else
1848 {
1849 m_process->SendMessage(message);
1850 // This isn't the stop we were expecting, but the thread is
1851 // stopped. SendMessage will handle processing of this event,
1852 // but we need to resume here to get the stop we are waiting
1853 // for (otherwise the thread will stop again immediately when
1854 // we try to resume).
1855 if (wait_pid == tid)
1856 Resume(wait_pid, eResumeSignalNone);
1857 }
1858 break;
1859
Andrew Kaylor93132f52013-05-28 23:04:25 +00001860 case ProcessMessage::eSignalDeliveredMessage:
1861 // This is the stop we're expecting.
1862 if (wait_pid == tid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && info.si_code == SI_TKILL)
1863 {
1864 if (log)
1865 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1866 thread->SetState(lldb::eStateStopped);
1867 return true;
1868 }
1869 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001870 case ProcessMessage::eBreakpointMessage:
1871 case ProcessMessage::eTraceMessage:
1872 case ProcessMessage::eWatchpointMessage:
1873 case ProcessMessage::eCrashMessage:
1874 case ProcessMessage::eNewThreadMessage:
1875 if (log)
1876 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1877 // SendMessage will set the thread state as needed.
1878 m_process->SendMessage(message);
1879 // This isn't the stop we were expecting, but the thread is
1880 // stopped. SendMessage will handle processing of this event,
1881 // but we need to resume here to get the stop we are waiting
1882 // for (otherwise the thread will stop again immediately when
1883 // we try to resume).
1884 if (wait_pid == tid)
1885 Resume(wait_pid, eResumeSignalNone);
1886 break;
1887 }
1888 }
1889 return false;
1890}
1891
Stephen Wilson84ffe702011-03-30 15:55:52 +00001892ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001893ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001894{
1895 ProcessMessage::CrashReason reason;
1896 assert(info->si_signo == SIGSEGV);
1897
1898 reason = ProcessMessage::eInvalidCrashReason;
1899
Greg Clayton542e4072012-09-07 17:49:29 +00001900 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001901 {
1902 default:
1903 assert(false && "unexpected si_code for SIGSEGV");
1904 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001905 case SI_KERNEL:
1906 // Linux will occasionally send spurious SI_KERNEL codes.
1907 // (this is poorly documented in sigaction)
1908 // One way to get this is via unaligned SIMD loads.
1909 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1910 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001911 case SEGV_MAPERR:
1912 reason = ProcessMessage::eInvalidAddress;
1913 break;
1914 case SEGV_ACCERR:
1915 reason = ProcessMessage::ePrivilegedAddress;
1916 break;
1917 }
Greg Clayton542e4072012-09-07 17:49:29 +00001918
Stephen Wilson84ffe702011-03-30 15:55:52 +00001919 return reason;
1920}
1921
1922ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001923ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001924{
1925 ProcessMessage::CrashReason reason;
1926 assert(info->si_signo == SIGILL);
1927
1928 reason = ProcessMessage::eInvalidCrashReason;
1929
1930 switch (info->si_code)
1931 {
1932 default:
1933 assert(false && "unexpected si_code for SIGILL");
1934 break;
1935 case ILL_ILLOPC:
1936 reason = ProcessMessage::eIllegalOpcode;
1937 break;
1938 case ILL_ILLOPN:
1939 reason = ProcessMessage::eIllegalOperand;
1940 break;
1941 case ILL_ILLADR:
1942 reason = ProcessMessage::eIllegalAddressingMode;
1943 break;
1944 case ILL_ILLTRP:
1945 reason = ProcessMessage::eIllegalTrap;
1946 break;
1947 case ILL_PRVOPC:
1948 reason = ProcessMessage::ePrivilegedOpcode;
1949 break;
1950 case ILL_PRVREG:
1951 reason = ProcessMessage::ePrivilegedRegister;
1952 break;
1953 case ILL_COPROC:
1954 reason = ProcessMessage::eCoprocessorError;
1955 break;
1956 case ILL_BADSTK:
1957 reason = ProcessMessage::eInternalStackError;
1958 break;
1959 }
1960
1961 return reason;
1962}
1963
1964ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001965ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001966{
1967 ProcessMessage::CrashReason reason;
1968 assert(info->si_signo == SIGFPE);
1969
1970 reason = ProcessMessage::eInvalidCrashReason;
1971
1972 switch (info->si_code)
1973 {
1974 default:
1975 assert(false && "unexpected si_code for SIGFPE");
1976 break;
1977 case FPE_INTDIV:
1978 reason = ProcessMessage::eIntegerDivideByZero;
1979 break;
1980 case FPE_INTOVF:
1981 reason = ProcessMessage::eIntegerOverflow;
1982 break;
1983 case FPE_FLTDIV:
1984 reason = ProcessMessage::eFloatDivideByZero;
1985 break;
1986 case FPE_FLTOVF:
1987 reason = ProcessMessage::eFloatOverflow;
1988 break;
1989 case FPE_FLTUND:
1990 reason = ProcessMessage::eFloatUnderflow;
1991 break;
1992 case FPE_FLTRES:
1993 reason = ProcessMessage::eFloatInexactResult;
1994 break;
1995 case FPE_FLTINV:
1996 reason = ProcessMessage::eFloatInvalidOperation;
1997 break;
1998 case FPE_FLTSUB:
1999 reason = ProcessMessage::eFloatSubscriptRange;
2000 break;
2001 }
2002
2003 return reason;
2004}
2005
2006ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002007ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002008{
2009 ProcessMessage::CrashReason reason;
2010 assert(info->si_signo == SIGBUS);
2011
2012 reason = ProcessMessage::eInvalidCrashReason;
2013
2014 switch (info->si_code)
2015 {
2016 default:
2017 assert(false && "unexpected si_code for SIGBUS");
2018 break;
2019 case BUS_ADRALN:
2020 reason = ProcessMessage::eIllegalAlignment;
2021 break;
2022 case BUS_ADRERR:
2023 reason = ProcessMessage::eIllegalAddress;
2024 break;
2025 case BUS_OBJERR:
2026 reason = ProcessMessage::eHardwareError;
2027 break;
2028 }
2029
2030 return reason;
2031}
2032
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002033void
Johnny Chen25e68e32011-06-14 19:19:50 +00002034ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002035{
Stephen Wilson570243b2011-01-19 01:37:06 +00002036 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002037
Stephen Wilson570243b2011-01-19 01:37:06 +00002038 // We are finised with the arguments and are ready to go. Sync with the
2039 // parent thread and start serving operations on the inferior.
2040 sem_post(&args->m_semaphore);
2041
Michael Sartain704bf892013-10-09 01:28:57 +00002042 for(;;)
2043 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002044 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002045 if (sem_wait(&monitor->m_operation_pending))
2046 {
2047 if (errno == EINTR)
2048 continue;
2049 assert(false && "Unexpected errno from sem_wait");
2050 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002051
Daniel Malea1efb4182013-09-16 23:12:18 +00002052 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002053
Daniel Malea1efb4182013-09-16 23:12:18 +00002054 // notify calling thread that operation is complete
2055 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002056 }
2057}
2058
2059void
2060ProcessMonitor::DoOperation(Operation *op)
2061{
Daniel Malea1efb4182013-09-16 23:12:18 +00002062 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002063
Daniel Malea1efb4182013-09-16 23:12:18 +00002064 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002065
Daniel Malea1efb4182013-09-16 23:12:18 +00002066 // notify operation thread that an operation is ready to be processed
2067 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002068
Daniel Malea1efb4182013-09-16 23:12:18 +00002069 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002070 while (sem_wait(&m_operation_done))
2071 {
2072 if (errno == EINTR)
2073 continue;
2074 assert(false && "Unexpected errno from sem_wait");
2075 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002076}
2077
2078size_t
2079ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2080 Error &error)
2081{
2082 size_t result;
2083 ReadOperation op(vm_addr, buf, size, error, result);
2084 DoOperation(&op);
2085 return result;
2086}
2087
2088size_t
2089ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2090 lldb_private::Error &error)
2091{
2092 size_t result;
2093 WriteOperation op(vm_addr, buf, size, error, result);
2094 DoOperation(&op);
2095 return result;
2096}
2097
2098bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002099ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002100 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002101{
2102 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002103 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002104 DoOperation(&op);
2105 return result;
2106}
2107
2108bool
Matt Kopec7de48462013-03-06 17:20:48 +00002109ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002110 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002111{
2112 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002113 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002114 DoOperation(&op);
2115 return result;
2116}
2117
2118bool
Matt Kopec7de48462013-03-06 17:20:48 +00002119ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002120{
2121 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002122 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002123 DoOperation(&op);
2124 return result;
2125}
2126
2127bool
Matt Kopec7de48462013-03-06 17:20:48 +00002128ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002129{
2130 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002131 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002132 DoOperation(&op);
2133 return result;
2134}
2135
2136bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002137ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2138{
2139 bool result;
2140 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2141 DoOperation(&op);
2142 return result;
2143}
2144
2145bool
Matt Kopec7de48462013-03-06 17:20:48 +00002146ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002147{
2148 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002149 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002150 DoOperation(&op);
2151 return result;
2152}
2153
2154bool
Matt Kopec7de48462013-03-06 17:20:48 +00002155ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002156{
2157 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002158 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002159 DoOperation(&op);
2160 return result;
2161}
2162
2163bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002164ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2165{
2166 bool result;
2167 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2168 DoOperation(&op);
2169 return result;
2170}
2171
2172bool
Richard Mitton0a558352013-10-17 21:14:00 +00002173ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2174{
2175 bool result;
2176 ReadThreadPointerOperation op(tid, &value, result);
2177 DoOperation(&op);
2178 return result;
2179}
2180
2181bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002182ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002183{
2184 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002185 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2186
2187 if (log)
2188 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2189 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002190 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002191 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002192 if (log)
2193 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002194 return result;
2195}
2196
2197bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002198ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002199{
2200 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002201 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002202 DoOperation(&op);
2203 return result;
2204}
2205
2206bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002207ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002208{
Ed Maste4e0999b2014-04-01 18:14:06 +00002209 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002210}
2211
2212bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002213ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002214{
2215 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002216 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002217 DoOperation(&op);
2218 return result;
2219}
2220
2221bool
2222ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2223{
2224 bool result;
2225 EventMessageOperation op(tid, message, result);
2226 DoOperation(&op);
2227 return result;
2228}
2229
Greg Clayton743ecf42012-10-16 20:20:18 +00002230lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002231ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002232{
Greg Clayton28041352011-11-29 20:50:10 +00002233 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002234 if (tid != LLDB_INVALID_THREAD_ID)
2235 {
2236 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002237 DoOperation(&op);
2238 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002239 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002240}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002241
2242bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002243ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2244{
Peter Collingbourne62343202011-06-14 03:55:54 +00002245 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002246
2247 if (target_fd == -1)
2248 return false;
2249
Peter Collingbourne62343202011-06-14 03:55:54 +00002250 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002251}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002252
2253void
2254ProcessMonitor::StopMonitoringChildProcess()
2255{
2256 lldb::thread_result_t thread_result;
2257
Stephen Wilsond4182f42011-02-09 20:10:35 +00002258 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002259 {
2260 Host::ThreadCancel(m_monitor_thread, NULL);
2261 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2262 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2263 }
2264}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002265
2266void
2267ProcessMonitor::StopMonitor()
2268{
2269 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002270 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002271 sem_destroy(&m_operation_pending);
2272 sem_destroy(&m_operation_done);
2273
Andrew Kaylor5e268992013-09-14 00:17:31 +00002274 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2275 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2276 // the descriptor to a ConnectionFileDescriptor object. Consequently
2277 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002278}
2279
2280void
Greg Clayton743ecf42012-10-16 20:20:18 +00002281ProcessMonitor::StopOpThread()
2282{
2283 lldb::thread_result_t result;
2284
2285 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2286 return;
2287
2288 Host::ThreadCancel(m_operation_thread, NULL);
2289 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002290 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002291}