blob: bfb710dd3fb31e31ad917ddd3fcc3be274727df2 [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>
20#include <sys/types.h>
21#include <sys/wait.h>
22
23// C++ Includes
24// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000025#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000026#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000027#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028#include "lldb/Core/Scalar.h"
29#include "lldb/Host/Host.h"
30#include "lldb/Target/Thread.h"
31#include "lldb/Target/RegisterContext.h"
32#include "lldb/Utility/PseudoTerminal.h"
33
Johnny Chen30213ff2012-01-05 19:17:38 +000034#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000036#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessMonitor.h"
38
39
Greg Clayton386ff182011-11-05 01:09:16 +000040#define DEBUG_PTRACE_MAXBYTES 20
41
Matt Kopec58c0b962013-03-20 20:34:35 +000042// Support ptrace extensions even when compiled without required kernel support
43#ifndef PTRACE_GETREGSET
44 #define PTRACE_GETREGSET 0x4204
45#endif
46#ifndef PTRACE_SETREGSET
47 #define PTRACE_SETREGSET 0x4205
48#endif
49
Matt Kopece9ea0da2013-05-07 19:29:28 +000050// Support hardware breakpoints in case it has not been defined
51#ifndef TRAP_HWBKPT
52 #define TRAP_HWBKPT 4
53#endif
54
Stephen Wilsone6f9f662010-07-24 02:19:04 +000055using namespace lldb_private;
56
Johnny Chen0d5f2d42011-10-18 18:09:30 +000057// FIXME: this code is host-dependent with respect to types and
58// endianness and needs to be fixed. For example, lldb::addr_t is
59// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
60// 32-bit pointer arguments. This code uses casts to work around the
61// problem.
62
63// We disable the tracing of ptrace calls for integration builds to
64// avoid the additional indirection and checks.
65#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
66
Greg Clayton386ff182011-11-05 01:09:16 +000067static void
68DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
69{
70 uint8_t *ptr = (uint8_t *)bytes;
71 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
72 for(uint32_t i=0; i<loop_count; i++)
73 {
74 s.Printf ("[%x]", *ptr);
75 ptr++;
76 }
77}
78
Matt Kopec58c0b962013-03-20 20:34:35 +000079static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000080{
81 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +000082 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +000083 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +000084
85 if (verbose_log)
86 {
87 switch(req)
88 {
89 case PTRACE_POKETEXT:
90 {
91 DisplayBytes(buf, &data, 8);
92 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
93 break;
94 }
Greg Clayton542e4072012-09-07 17:49:29 +000095 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +000096 {
97 DisplayBytes(buf, &data, 8);
98 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
99 break;
100 }
Greg Clayton542e4072012-09-07 17:49:29 +0000101 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000102 {
103 DisplayBytes(buf, &data, 8);
104 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
105 break;
106 }
Greg Clayton542e4072012-09-07 17:49:29 +0000107 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000108 {
Matt Kopec7de48462013-03-06 17:20:48 +0000109 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000110 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
111 break;
112 }
113 case PTRACE_SETFPREGS:
114 {
Matt Kopec7de48462013-03-06 17:20:48 +0000115 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000116 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, data, sizeof(siginfo_t));
122 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
123 break;
124 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000125 case PTRACE_SETREGSET:
126 {
127 // Extract iov_base from data, which is a pointer to the struct IOVEC
128 DisplayBytes(buf, *(void **)data, data_size);
129 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
130 break;
131 }
Greg Clayton386ff182011-11-05 01:09:16 +0000132 default:
133 {
134 }
135 }
136 }
137}
138
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000139// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000140// 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 +0000141extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000142PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000143 const char* reqName, const char* file, int line)
144{
Greg Clayton386ff182011-11-05 01:09:16 +0000145 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000146
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000147 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000148
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000149 if (log)
Matt Kopec58c0b962013-03-20 20:34:35 +0000150 log->Printf("ptrace(%s, %lu, %p, %p, %zu) called from file %s line %d",
Matt Kopec7de48462013-03-06 17:20:48 +0000151 reqName, pid, addr, data, data_size, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000152
Matt Kopec7de48462013-03-06 17:20:48 +0000153 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000154
155 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000156 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
157 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
158 else
159 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000160
Matt Kopec7de48462013-03-06 17:20:48 +0000161 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000162
Matt Kopec7de48462013-03-06 17:20:48 +0000163 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000164 {
165 const char* str;
166 switch (errno)
167 {
168 case ESRCH: str = "ESRCH"; break;
169 case EINVAL: str = "EINVAL"; break;
170 case EBUSY: str = "EBUSY"; break;
171 case EPERM: str = "EPERM"; break;
172 default: str = "<unknown>";
173 }
174 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
175 }
176
177 return result;
178}
179
Matt Kopec7de48462013-03-06 17:20:48 +0000180// Wrapper for ptrace when logging is not required.
181// Sets errno to 0 prior to calling ptrace.
182extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000183PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000184{
Matt Kopec58c0b962013-03-20 20:34:35 +0000185 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000186 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000187 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
188 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
189 else
190 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000191 return result;
192}
193
194#define PTRACE(req, pid, addr, data, data_size) \
195 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000196#else
Matt Kopec7de48462013-03-06 17:20:48 +0000197 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000198#endif
199
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000200//------------------------------------------------------------------------------
201// Static implementations of ProcessMonitor::ReadMemory and
202// ProcessMonitor::WriteMemory. This enables mutual recursion between these
203// functions without needed to go thru the thread funnel.
204
205static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000206DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000207 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
208{
Greg Clayton542e4072012-09-07 17:49:29 +0000209 // ptrace word size is determined by the host, not the child
210 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000211 unsigned char *dst = static_cast<unsigned char*>(buf);
212 size_t bytes_read;
213 size_t remainder;
214 long data;
215
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000216 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000217 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000218 ProcessPOSIXLog::IncNestLevel();
219 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000220 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000221 pid, word_size, (void*)vm_addr, buf, size);
222
223 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
225 {
226 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000227 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
228 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 {
230 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000231 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000232 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000233 return bytes_read;
234 }
235
236 remainder = size - bytes_read;
237 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000238
239 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000240 for (unsigned i = 0; i < remainder; ++i)
241 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000242
Johnny Chen30213ff2012-01-05 19:17:38 +0000243 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
244 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
245 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
246 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000247 {
248 uintptr_t print_dst = 0;
249 // Format bytes from data by moving into print_dst for log output
250 for (unsigned i = 0; i < remainder; ++i)
251 print_dst |= (((data >> i*8) & 0xFF) << i*8);
252 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
253 (void*)vm_addr, print_dst, (unsigned long)data);
254 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000255
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256 vm_addr += word_size;
257 dst += word_size;
258 }
259
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000262 return bytes_read;
263}
264
265static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000266DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000267 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
268{
Greg Clayton542e4072012-09-07 17:49:29 +0000269 // ptrace word size is determined by the host, not the child
270 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000271 const unsigned char *src = static_cast<const unsigned char*>(buf);
272 size_t bytes_written = 0;
273 size_t remainder;
274
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000275 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000276 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000277 ProcessPOSIXLog::IncNestLevel();
278 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000279 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000280 pid, word_size, (void*)vm_addr, buf, size);
281
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000282 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
283 {
284 remainder = size - bytes_written;
285 remainder = remainder > word_size ? word_size : remainder;
286
287 if (remainder == word_size)
288 {
289 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000290 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000291 for (unsigned i = 0; i < word_size; ++i)
292 data |= (unsigned long)src[i] << i*8;
293
Johnny Chen30213ff2012-01-05 19:17:38 +0000294 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
295 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
296 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
297 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
299 (void*)vm_addr, *(unsigned long*)src, data);
300
Matt Kopec7de48462013-03-06 17:20:48 +0000301 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000302 {
303 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000305 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000306 return bytes_written;
307 }
308 }
309 else
310 {
311 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000312 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000313 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000314 {
315 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000316 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000317 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000318 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000319
320 memcpy(buff, src, remainder);
321
Greg Clayton542e4072012-09-07 17:49:29 +0000322 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000323 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000324 {
325 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000326 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000327 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000328 }
329
Johnny Chen30213ff2012-01-05 19:17:38 +0000330 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
331 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
332 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
333 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000334 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
335 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000336 }
337
338 vm_addr += word_size;
339 src += word_size;
340 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000341 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000342 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000343 return bytes_written;
344}
345
Stephen Wilson26977162011-03-23 02:14:42 +0000346// Simple helper function to ensure flags are enabled on the given file
347// descriptor.
348static bool
349EnsureFDFlags(int fd, int flags, Error &error)
350{
351 int status;
352
353 if ((status = fcntl(fd, F_GETFL)) == -1)
354 {
355 error.SetErrorToErrno();
356 return false;
357 }
358
359 if (fcntl(fd, F_SETFL, status | flags) == -1)
360 {
361 error.SetErrorToErrno();
362 return false;
363 }
364
365 return true;
366}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367
368//------------------------------------------------------------------------------
369/// @class Operation
370/// @brief Represents a ProcessMonitor operation.
371///
372/// Under Linux, it is not possible to ptrace() from any other thread but the
373/// one that spawned or attached to the process from the start. Therefore, when
374/// a ProcessMonitor is asked to deliver or change the state of an inferior
375/// process the operation must be "funneled" to a specific thread to perform the
376/// task. The Operation class provides an abstract base for all services the
377/// ProcessMonitor must perform via the single virtual function Execute, thus
378/// encapsulating the code that needs to run in the privileged context.
379class Operation
380{
381public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000382 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000383 virtual void Execute(ProcessMonitor *monitor) = 0;
384};
385
386//------------------------------------------------------------------------------
387/// @class ReadOperation
388/// @brief Implements ProcessMonitor::ReadMemory.
389class ReadOperation : public Operation
390{
391public:
392 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
393 Error &error, size_t &result)
394 : m_addr(addr), m_buff(buff), m_size(size),
395 m_error(error), m_result(result)
396 { }
397
398 void Execute(ProcessMonitor *monitor);
399
400private:
401 lldb::addr_t m_addr;
402 void *m_buff;
403 size_t m_size;
404 Error &m_error;
405 size_t &m_result;
406};
407
408void
409ReadOperation::Execute(ProcessMonitor *monitor)
410{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000411 lldb::pid_t pid = monitor->GetPID();
412
Greg Clayton542e4072012-09-07 17:49:29 +0000413 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000414}
415
416//------------------------------------------------------------------------------
417/// @class ReadOperation
418/// @brief Implements ProcessMonitor::WriteMemory.
419class WriteOperation : public Operation
420{
421public:
422 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
423 Error &error, size_t &result)
424 : m_addr(addr), m_buff(buff), m_size(size),
425 m_error(error), m_result(result)
426 { }
427
428 void Execute(ProcessMonitor *monitor);
429
430private:
431 lldb::addr_t m_addr;
432 const void *m_buff;
433 size_t m_size;
434 Error &m_error;
435 size_t &m_result;
436};
437
438void
439WriteOperation::Execute(ProcessMonitor *monitor)
440{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000441 lldb::pid_t pid = monitor->GetPID();
442
Greg Clayton542e4072012-09-07 17:49:29 +0000443 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000444}
445
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000446
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000447//------------------------------------------------------------------------------
448/// @class ReadRegOperation
449/// @brief Implements ProcessMonitor::ReadRegisterValue.
450class ReadRegOperation : public Operation
451{
452public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000453 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000454 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000455 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000456 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000457 { }
458
459 void Execute(ProcessMonitor *monitor);
460
461private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000462 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000463 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000464 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000465 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000466 bool &m_result;
467};
468
469void
470ReadRegOperation::Execute(ProcessMonitor *monitor)
471{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000472 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000473
474 // Set errno to zero so that we can detect a failed peek.
475 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000476 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
477 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000478 m_result = false;
479 else
480 {
481 m_value = data;
482 m_result = true;
483 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000484 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000485 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000486 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000487}
488
489//------------------------------------------------------------------------------
490/// @class WriteRegOperation
491/// @brief Implements ProcessMonitor::WriteRegisterValue.
492class WriteRegOperation : public Operation
493{
494public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000495 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000496 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000497 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000498 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000499 { }
500
501 void Execute(ProcessMonitor *monitor);
502
503private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000504 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000505 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000506 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000507 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000508 bool &m_result;
509};
510
511void
512WriteRegOperation::Execute(ProcessMonitor *monitor)
513{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000514 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000515 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000516
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000517#if __WORDSIZE == 32
518 buf = (void*) m_value.GetAsUInt32();
519#else
520 buf = (void*) m_value.GetAsUInt64();
521#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000522
523 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000524 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000525 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000526 m_result = false;
527 else
528 m_result = true;
529}
530
531//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000532/// @class ReadGPROperation
533/// @brief Implements ProcessMonitor::ReadGPR.
534class ReadGPROperation : public Operation
535{
536public:
Matt Kopec7de48462013-03-06 17:20:48 +0000537 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
538 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000539 { }
540
541 void Execute(ProcessMonitor *monitor);
542
543private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000544 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000545 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000546 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000547 bool &m_result;
548};
549
550void
551ReadGPROperation::Execute(ProcessMonitor *monitor)
552{
Matt Kopec7de48462013-03-06 17:20:48 +0000553 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000554 m_result = false;
555 else
556 m_result = true;
557}
558
559//------------------------------------------------------------------------------
560/// @class ReadFPROperation
561/// @brief Implements ProcessMonitor::ReadFPR.
562class ReadFPROperation : public Operation
563{
564public:
Matt Kopec7de48462013-03-06 17:20:48 +0000565 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
566 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000567 { }
568
569 void Execute(ProcessMonitor *monitor);
570
571private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000572 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000573 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000574 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000575 bool &m_result;
576};
577
578void
579ReadFPROperation::Execute(ProcessMonitor *monitor)
580{
Matt Kopec7de48462013-03-06 17:20:48 +0000581 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000582 m_result = false;
583 else
584 m_result = true;
585}
586
587//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000588/// @class ReadRegisterSetOperation
589/// @brief Implements ProcessMonitor::ReadRegisterSet.
590class ReadRegisterSetOperation : public Operation
591{
592public:
593 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
594 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
595 { }
596
597 void Execute(ProcessMonitor *monitor);
598
599private:
600 lldb::tid_t m_tid;
601 void *m_buf;
602 size_t m_buf_size;
603 const unsigned int m_regset;
604 bool &m_result;
605};
606
607void
608ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
609{
610 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
611 m_result = false;
612 else
613 m_result = true;
614}
615
616//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000617/// @class WriteGPROperation
618/// @brief Implements ProcessMonitor::WriteGPR.
619class WriteGPROperation : public Operation
620{
621public:
Matt Kopec7de48462013-03-06 17:20:48 +0000622 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
623 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000624 { }
625
626 void Execute(ProcessMonitor *monitor);
627
628private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000629 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000630 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000631 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000632 bool &m_result;
633};
634
635void
636WriteGPROperation::Execute(ProcessMonitor *monitor)
637{
Matt Kopec7de48462013-03-06 17:20:48 +0000638 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000639 m_result = false;
640 else
641 m_result = true;
642}
643
644//------------------------------------------------------------------------------
645/// @class WriteFPROperation
646/// @brief Implements ProcessMonitor::WriteFPR.
647class WriteFPROperation : public Operation
648{
649public:
Matt Kopec7de48462013-03-06 17:20:48 +0000650 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
651 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000652 { }
653
654 void Execute(ProcessMonitor *monitor);
655
656private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000657 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000658 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000659 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000660 bool &m_result;
661};
662
663void
664WriteFPROperation::Execute(ProcessMonitor *monitor)
665{
Matt Kopec7de48462013-03-06 17:20:48 +0000666 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000667 m_result = false;
668 else
669 m_result = true;
670}
671
672//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000673/// @class WriteRegisterSetOperation
674/// @brief Implements ProcessMonitor::WriteRegisterSet.
675class WriteRegisterSetOperation : public Operation
676{
677public:
678 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
679 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
680 { }
681
682 void Execute(ProcessMonitor *monitor);
683
684private:
685 lldb::tid_t m_tid;
686 void *m_buf;
687 size_t m_buf_size;
688 const unsigned int m_regset;
689 bool &m_result;
690};
691
692void
693WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
694{
695 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
696 m_result = false;
697 else
698 m_result = true;
699}
700
701//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000702/// @class ResumeOperation
703/// @brief Implements ProcessMonitor::Resume.
704class ResumeOperation : public Operation
705{
706public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000707 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
708 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000709
710 void Execute(ProcessMonitor *monitor);
711
712private:
713 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000714 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000715 bool &m_result;
716};
717
718void
719ResumeOperation::Execute(ProcessMonitor *monitor)
720{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000721 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000722
723 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
724 data = m_signo;
725
Matt Kopec7de48462013-03-06 17:20:48 +0000726 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000727 m_result = false;
728 else
729 m_result = true;
730}
731
732//------------------------------------------------------------------------------
733/// @class ResumeOperation
734/// @brief Implements ProcessMonitor::SingleStep.
735class SingleStepOperation : public Operation
736{
737public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000738 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
739 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000740
741 void Execute(ProcessMonitor *monitor);
742
743private:
744 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000745 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000746 bool &m_result;
747};
748
749void
750SingleStepOperation::Execute(ProcessMonitor *monitor)
751{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000752 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000753
754 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
755 data = m_signo;
756
Matt Kopec7de48462013-03-06 17:20:48 +0000757 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000758 m_result = false;
759 else
760 m_result = true;
761}
762
763//------------------------------------------------------------------------------
764/// @class SiginfoOperation
765/// @brief Implements ProcessMonitor::GetSignalInfo.
766class SiginfoOperation : public Operation
767{
768public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000769 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
770 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000771
772 void Execute(ProcessMonitor *monitor);
773
774private:
775 lldb::tid_t m_tid;
776 void *m_info;
777 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000778 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000779};
780
781void
782SiginfoOperation::Execute(ProcessMonitor *monitor)
783{
Matt Kopec7de48462013-03-06 17:20:48 +0000784 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000785 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000786 m_err = errno;
787 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000788 else
789 m_result = true;
790}
791
792//------------------------------------------------------------------------------
793/// @class EventMessageOperation
794/// @brief Implements ProcessMonitor::GetEventMessage.
795class EventMessageOperation : public Operation
796{
797public:
798 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
799 : m_tid(tid), m_message(message), m_result(result) { }
800
801 void Execute(ProcessMonitor *monitor);
802
803private:
804 lldb::tid_t m_tid;
805 unsigned long *m_message;
806 bool &m_result;
807};
808
809void
810EventMessageOperation::Execute(ProcessMonitor *monitor)
811{
Matt Kopec7de48462013-03-06 17:20:48 +0000812 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000813 m_result = false;
814 else
815 m_result = true;
816}
817
818//------------------------------------------------------------------------------
819/// @class KillOperation
820/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
821class KillOperation : public Operation
822{
823public:
824 KillOperation(bool &result) : m_result(result) { }
825
826 void Execute(ProcessMonitor *monitor);
827
828private:
829 bool &m_result;
830};
831
832void
833KillOperation::Execute(ProcessMonitor *monitor)
834{
835 lldb::pid_t pid = monitor->GetPID();
836
Matt Kopec7de48462013-03-06 17:20:48 +0000837 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 m_result = false;
839 else
840 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000841}
842
Greg Clayton28041352011-11-29 20:50:10 +0000843//------------------------------------------------------------------------------
844/// @class KillOperation
845/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
846class DetachOperation : public Operation
847{
848public:
849 DetachOperation(Error &result) : m_error(result) { }
850
851 void Execute(ProcessMonitor *monitor);
852
853private:
854 Error &m_error;
855};
856
857void
858DetachOperation::Execute(ProcessMonitor *monitor)
859{
860 lldb::pid_t pid = monitor->GetPID();
861
862 if (ptrace(PT_DETACH, pid, NULL, 0) < 0)
863 m_error.SetErrorToErrno();
Greg Clayton542e4072012-09-07 17:49:29 +0000864
Greg Clayton28041352011-11-29 20:50:10 +0000865}
866
Johnny Chen25e68e32011-06-14 19:19:50 +0000867ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
868 : m_monitor(monitor)
869{
870 sem_init(&m_semaphore, 0, 0);
871}
872
873ProcessMonitor::OperationArgs::~OperationArgs()
874{
875 sem_destroy(&m_semaphore);
876}
877
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000878ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
879 lldb_private::Module *module,
880 char const **argv,
881 char const **envp,
882 const char *stdin_path,
883 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000884 const char *stderr_path,
885 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000886 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000887 m_module(module),
888 m_argv(argv),
889 m_envp(envp),
890 m_stdin_path(stdin_path),
891 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000892 m_stderr_path(stderr_path),
893 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000894
895ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000896{ }
897
898ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
899 lldb::pid_t pid)
900 : OperationArgs(monitor), m_pid(pid) { }
901
902ProcessMonitor::AttachArgs::~AttachArgs()
903{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000904
905//------------------------------------------------------------------------------
906/// The basic design of the ProcessMonitor is built around two threads.
907///
908/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
909/// for changes in the debugee state. When a change is detected a
910/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
911/// "drives" state changes in the debugger.
912///
913/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000914/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000915/// operations such as register reads/writes, stepping, etc. See the comments
916/// on the Operation class for more info as to why this is needed.
Johnny Chen30213ff2012-01-05 19:17:38 +0000917ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000918 Module *module,
919 const char *argv[],
920 const char *envp[],
921 const char *stdin_path,
922 const char *stdout_path,
923 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000924 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000925 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000926 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000927 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000928 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000929 m_pid(LLDB_INVALID_PROCESS_ID),
930 m_terminal_fd(-1),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000931 m_client_fd(-1),
932 m_server_fd(-1)
933{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000934 std::unique_ptr<LaunchArgs> args;
Stephen Wilson57740ec2011-01-15 00:12:41 +0000935
936 args.reset(new LaunchArgs(this, module, argv, envp,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000937 stdin_path, stdout_path, stderr_path, working_dir));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000938
939 // Server/client descriptors.
940 if (!EnableIPC())
941 {
942 error.SetErrorToGenericError();
943 error.SetErrorString("Monitor failed to initialize.");
944 }
945
Johnny Chen25e68e32011-06-14 19:19:50 +0000946 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000947 if (!error.Success())
948 return;
949
950WAIT_AGAIN:
951 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000952 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000953 {
954 if (errno == EINTR)
955 goto WAIT_AGAIN;
956 else
957 {
958 error.SetErrorToErrno();
959 return;
960 }
961 }
962
963 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000964 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000965 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000966 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +0000967 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968 return;
969 }
970
971 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000972 m_monitor_thread = Host::StartMonitoringChildProcess(
973 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +0000974 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975 {
976 error.SetErrorToGenericError();
977 error.SetErrorString("Process launch failed.");
978 return;
979 }
980}
981
Johnny Chen30213ff2012-01-05 19:17:38 +0000982ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +0000983 lldb::pid_t pid,
984 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000985 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +0000986 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000987 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +0000988 m_pid(LLDB_INVALID_PROCESS_ID),
989 m_terminal_fd(-1),
Matt Kopec7de48462013-03-06 17:20:48 +0000990
Johnny Chen25e68e32011-06-14 19:19:50 +0000991 m_client_fd(-1),
992 m_server_fd(-1)
993{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000994 std::unique_ptr<AttachArgs> args;
Johnny Chen25e68e32011-06-14 19:19:50 +0000995
996 args.reset(new AttachArgs(this, pid));
997
998 // Server/client descriptors.
999 if (!EnableIPC())
1000 {
1001 error.SetErrorToGenericError();
1002 error.SetErrorString("Monitor failed to initialize.");
1003 }
1004
1005 StartAttachOpThread(args.get(), error);
1006 if (!error.Success())
1007 return;
1008
1009WAIT_AGAIN:
1010 // Wait for the operation thread to initialize.
1011 if (sem_wait(&args->m_semaphore))
1012 {
1013 if (errno == EINTR)
1014 goto WAIT_AGAIN;
1015 else
1016 {
1017 error.SetErrorToErrno();
1018 return;
1019 }
1020 }
1021
Greg Clayton743ecf42012-10-16 20:20:18 +00001022 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001023 if (!args->m_error.Success())
1024 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001025 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001026 error = args->m_error;
1027 return;
1028 }
1029
1030 // Finally, start monitoring the child process for change in state.
1031 m_monitor_thread = Host::StartMonitoringChildProcess(
1032 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1033 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1034 {
1035 error.SetErrorToGenericError();
1036 error.SetErrorString("Process attach failed.");
1037 return;
1038 }
1039}
1040
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001041ProcessMonitor::~ProcessMonitor()
1042{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001043 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001044}
1045
1046//------------------------------------------------------------------------------
1047// Thread setup and tear down.
1048void
Johnny Chen25e68e32011-06-14 19:19:50 +00001049ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001050{
1051 static const char *g_thread_name = "lldb.process.linux.operation";
1052
Stephen Wilsond4182f42011-02-09 20:10:35 +00001053 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 return;
1055
1056 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001057 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001058}
1059
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001060void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001061ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001062{
1063 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1064
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001065 if (!Launch(args)) {
1066 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001067 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001068 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001069
Stephen Wilson570243b2011-01-19 01:37:06 +00001070 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001071 return NULL;
1072}
1073
1074bool
1075ProcessMonitor::Launch(LaunchArgs *args)
1076{
1077 ProcessMonitor *monitor = args->m_monitor;
1078 ProcessLinux &process = monitor->GetProcess();
1079 const char **argv = args->m_argv;
1080 const char **envp = args->m_envp;
1081 const char *stdin_path = args->m_stdin_path;
1082 const char *stdout_path = args->m_stdout_path;
1083 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001084 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001085
1086 lldb_utility::PseudoTerminal terminal;
1087 const size_t err_len = 1024;
1088 char err_str[err_len];
1089 lldb::pid_t pid;
Matt Kopec650648f2013-01-08 16:30:18 +00001090 long ptrace_opts = 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001091
1092 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001093 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001094
Stephen Wilson57740ec2011-01-15 00:12:41 +00001095 // Propagate the environment if one is not supplied.
1096 if (envp == NULL || envp[0] == NULL)
1097 envp = const_cast<const char **>(environ);
1098
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001099 // Pseudo terminal setup.
1100 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
1101 {
1102 args->m_error.SetErrorToGenericError();
1103 args->m_error.SetErrorString("Could not open controlling TTY.");
1104 goto FINISH;
1105 }
1106
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001107 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001108 {
1109 args->m_error.SetErrorToGenericError();
1110 args->m_error.SetErrorString("Process fork failed.");
1111 goto FINISH;
1112 }
1113
Peter Collingbourne6a520222011-06-14 03:55:58 +00001114 // Recognized child exit status codes.
1115 enum {
1116 ePtraceFailed = 1,
1117 eDupStdinFailed,
1118 eDupStdoutFailed,
1119 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001120 eChdirFailed,
Peter Collingbourne6a520222011-06-14 03:55:58 +00001121 eExecFailed
1122 };
1123
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001124 // Child process.
1125 if (pid == 0)
1126 {
1127 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001128 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001129 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130
1131 // Do not inherit setgid powers.
1132 setgid(getgid());
1133
1134 // Let us have our own process group.
1135 setpgid(0, 0);
1136
Greg Clayton710dd5a2011-01-08 20:28:42 +00001137 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138 //
1139 // FIXME: If two or more of the paths are the same we needlessly open
1140 // the same file multiple times.
1141 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001142 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001143 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001144
1145 if (stdout_path != NULL && stdout_path[0])
1146 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001147 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001148
1149 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001150 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001151 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001152
Daniel Malea6217d2a2013-01-08 14:49:22 +00001153 // Change working directory
1154 if (working_dir != NULL && working_dir[0])
1155 if (0 != ::chdir(working_dir))
1156 exit(eChdirFailed);
1157
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001158 // Execute. We should never return.
1159 execve(argv[0],
1160 const_cast<char *const *>(argv),
1161 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001162 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163 }
1164
1165 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001166 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001168 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001170 args->m_error.SetErrorToErrno();
1171 goto FINISH;
1172 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001173 else if (WIFEXITED(status))
1174 {
1175 // open, dup or execve likely failed for some reason.
1176 args->m_error.SetErrorToGenericError();
1177 switch (WEXITSTATUS(status))
1178 {
Greg Clayton542e4072012-09-07 17:49:29 +00001179 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001180 args->m_error.SetErrorString("Child ptrace failed.");
1181 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001182 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001183 args->m_error.SetErrorString("Child open stdin failed.");
1184 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001185 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001186 args->m_error.SetErrorString("Child open stdout failed.");
1187 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001188 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001189 args->m_error.SetErrorString("Child open stderr failed.");
1190 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001191 case eChdirFailed:
1192 args->m_error.SetErrorString("Child failed to set working directory.");
1193 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001194 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001195 args->m_error.SetErrorString("Child exec failed.");
1196 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001197 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001198 args->m_error.SetErrorString("Child returned unknown exit status.");
1199 break;
1200 }
1201 goto FINISH;
1202 }
1203 assert(WIFSTOPPED(status) && wpid == pid &&
1204 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001205
1206 // Have the child raise an event on exit. This is used to keep the child in
1207 // limbo until it is destroyed.
Matt Kopec650648f2013-01-08 16:30:18 +00001208 ptrace_opts |= PTRACE_O_TRACEEXIT;
1209
1210 // Have the tracer trace threads which spawn in the inferior process.
Matt Kopeca360d7e2013-05-17 19:27:47 +00001211 // TODO: if we want to support tracing the inferiors' child, add the
1212 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1213 ptrace_opts |= PTRACE_O_TRACECLONE;
1214
1215 // Have the tracer notify us before execve returns
1216 // (needed to disable legacy SIGTRAP generation)
1217 ptrace_opts |= PTRACE_O_TRACEEXEC;
Matt Kopec650648f2013-01-08 16:30:18 +00001218
Matt Kopec7de48462013-03-06 17:20:48 +00001219 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001220 {
1221 args->m_error.SetErrorToErrno();
1222 goto FINISH;
1223 }
1224
1225 // Release the master terminal descriptor and pass it off to the
1226 // ProcessMonitor instance. Similarly stash the inferior pid.
1227 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1228 monitor->m_pid = pid;
1229
Stephen Wilson26977162011-03-23 02:14:42 +00001230 // Set the terminal fd to be in non blocking mode (it simplifies the
1231 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1232 // descriptor to read from).
1233 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1234 goto FINISH;
1235
Johnny Chen30213ff2012-01-05 19:17:38 +00001236 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001237 // FIXME: should we be letting UpdateThreadList handle this?
1238 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001239 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001240 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001241 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001242 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001243
1244 // Let our process instance know the thread has stopped.
1245 process.SendMessage(ProcessMessage::Trace(pid));
1246
1247FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001248 return args->m_error.Success();
1249}
1250
1251bool
1252ProcessMonitor::EnableIPC()
1253{
1254 int fd[2];
1255
1256 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1257 return false;
1258
1259 m_client_fd = fd[0];
1260 m_server_fd = fd[1];
1261 return true;
1262}
1263
Johnny Chen25e68e32011-06-14 19:19:50 +00001264void
1265ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1266{
1267 static const char *g_thread_name = "lldb.process.linux.operation";
1268
1269 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1270 return;
1271
1272 m_operation_thread =
1273 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1274}
1275
Johnny Chen25e68e32011-06-14 19:19:50 +00001276void *
1277ProcessMonitor::AttachOpThread(void *arg)
1278{
1279 AttachArgs *args = static_cast<AttachArgs*>(arg);
1280
Greg Clayton743ecf42012-10-16 20:20:18 +00001281 if (!Attach(args)) {
1282 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001283 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001284 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001285
1286 ServeOperation(args);
1287 return NULL;
1288}
1289
1290bool
1291ProcessMonitor::Attach(AttachArgs *args)
1292{
1293 lldb::pid_t pid = args->m_pid;
1294
1295 ProcessMonitor *monitor = args->m_monitor;
1296 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001297 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001298 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001299
1300 if (pid <= 1)
1301 {
1302 args->m_error.SetErrorToGenericError();
1303 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1304 goto FINISH;
1305 }
1306
1307 // Attach to the requested process.
Matt Kopec7de48462013-03-06 17:20:48 +00001308 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL, 0) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001309 {
1310 args->m_error.SetErrorToErrno();
1311 goto FINISH;
1312 }
1313
1314 int status;
1315 if ((status = waitpid(pid, NULL, 0)) < 0)
1316 {
1317 args->m_error.SetErrorToErrno();
1318 goto FINISH;
1319 }
1320
Greg Clayton926cce72012-10-12 16:10:12 +00001321 monitor->m_pid = pid;
1322
Johnny Chen30213ff2012-01-05 19:17:38 +00001323 // Update the process thread list with the attached thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001324 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001325 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001326 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001327 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001328
1329 // Let our process instance know the thread has stopped.
1330 process.SendMessage(ProcessMessage::Trace(pid));
1331
1332 FINISH:
1333 return args->m_error.Success();
1334}
1335
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001336bool
1337ProcessMonitor::MonitorCallback(void *callback_baton,
1338 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001339 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001340 int signal,
1341 int status)
1342{
1343 ProcessMessage message;
1344 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1345 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001346 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001347 bool stop_monitoring;
1348 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001349 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001350
Daniel Maleaa35970a2012-11-23 18:09:58 +00001351 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1352 if (ptrace_err == EINVAL) {
1353 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1354 if (!monitor->Resume(pid, SIGSTOP)) {
1355 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1356 }
1357 stop_monitoring = false;
1358 } else {
1359 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1360 // this means the child pid is gone (or not being debugged) therefore
1361 // stop the monitor thread.
1362 stop_monitoring = true;
1363 }
1364 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001365 else {
1366 switch (info.si_signo)
1367 {
1368 case SIGTRAP:
1369 message = MonitorSIGTRAP(monitor, &info, pid);
1370 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001371
Stephen Wilson84ffe702011-03-30 15:55:52 +00001372 default:
1373 message = MonitorSignal(monitor, &info, pid);
1374 break;
1375 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001376
Stephen Wilson84ffe702011-03-30 15:55:52 +00001377 process->SendMessage(message);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001378 stop_monitoring = !process->IsAlive();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001379 }
1380
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001381 return stop_monitoring;
1382}
1383
1384ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001385ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001386 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001387{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001388 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001389
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001390 assert(monitor);
1391 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001392
Stephen Wilson84ffe702011-03-30 15:55:52 +00001393 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001394 {
1395 default:
1396 assert(false && "Unexpected SIGTRAP code!");
1397 break;
1398
Matt Kopeca360d7e2013-05-17 19:27:47 +00001399 // TODO: these two cases are required if we want to support tracing
1400 // of the inferiors' children
1401 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1402 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1403
Matt Kopec650648f2013-01-08 16:30:18 +00001404 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1405 {
1406 unsigned long tid = 0;
1407 if (!monitor->GetEventMessage(pid, &tid))
1408 tid = -1;
1409 message = ProcessMessage::NewThread(pid, tid);
1410 break;
1411 }
1412
Matt Kopeca360d7e2013-05-17 19:27:47 +00001413 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
1414 // Don't follow the child by default and resume
1415 monitor->Resume(pid, SIGCONT);
1416 break;
1417
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001418 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1419 {
1420 // The inferior process is about to exit. Maintain the process in a
1421 // state of "limbo" until we are explicitly commanded to detach,
1422 // destroy, resume, etc.
1423 unsigned long data = 0;
1424 if (!monitor->GetEventMessage(pid, &data))
1425 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001426 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001427 break;
1428 }
1429
1430 case 0:
1431 case TRAP_TRACE:
1432 message = ProcessMessage::Trace(pid);
1433 break;
1434
1435 case SI_KERNEL:
1436 case TRAP_BRKPT:
1437 message = ProcessMessage::Break(pid);
1438 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001439
1440 case TRAP_HWBKPT:
1441 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1442 break;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001443 }
1444
1445 return message;
1446}
1447
Stephen Wilson84ffe702011-03-30 15:55:52 +00001448ProcessMessage
1449ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001450 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001451{
1452 ProcessMessage message;
1453 int signo = info->si_signo;
1454
1455 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1456 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1457 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1458 //
1459 // IOW, user generated signals never generate what we consider to be a
1460 // "crash".
1461 //
1462 // Similarly, ACK signals generated by this monitor.
1463 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1464 {
1465 if (info->si_pid == getpid())
1466 return ProcessMessage::SignalDelivered(pid, signo);
1467 else
1468 return ProcessMessage::Signal(pid, signo);
1469 }
1470
1471 if (signo == SIGSEGV) {
1472 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1473 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1474 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1475 }
1476
1477 if (signo == SIGILL) {
1478 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1479 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1480 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1481 }
1482
1483 if (signo == SIGFPE) {
1484 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1485 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1486 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1487 }
1488
1489 if (signo == SIGBUS) {
1490 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1491 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1492 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1493 }
1494
Matt Kopeca360d7e2013-05-17 19:27:47 +00001495 if (signo == SIGCHLD) {
1496 assert(monitor);
1497 // TODO: Implement tracing of inferiors' children
1498 // If we fail to deliver the signal then create a message with the signal
1499 if (!monitor->Resume(pid, signo)) {
1500 assert(0 && "SIGCHLD delivery failed");
1501 message = ProcessMessage::Signal(pid, signo);
1502 }
1503 return message;
1504 }
1505
Stephen Wilson84ffe702011-03-30 15:55:52 +00001506 // Everything else is "normal" and does not require any special action on
1507 // our part.
1508 return ProcessMessage::Signal(pid, signo);
1509}
1510
1511ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001512ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001513{
1514 ProcessMessage::CrashReason reason;
1515 assert(info->si_signo == SIGSEGV);
1516
1517 reason = ProcessMessage::eInvalidCrashReason;
1518
Greg Clayton542e4072012-09-07 17:49:29 +00001519 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001520 {
1521 default:
1522 assert(false && "unexpected si_code for SIGSEGV");
1523 break;
1524 case SEGV_MAPERR:
1525 reason = ProcessMessage::eInvalidAddress;
1526 break;
1527 case SEGV_ACCERR:
1528 reason = ProcessMessage::ePrivilegedAddress;
1529 break;
1530 }
Greg Clayton542e4072012-09-07 17:49:29 +00001531
Stephen Wilson84ffe702011-03-30 15:55:52 +00001532 return reason;
1533}
1534
1535ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001536ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001537{
1538 ProcessMessage::CrashReason reason;
1539 assert(info->si_signo == SIGILL);
1540
1541 reason = ProcessMessage::eInvalidCrashReason;
1542
1543 switch (info->si_code)
1544 {
1545 default:
1546 assert(false && "unexpected si_code for SIGILL");
1547 break;
1548 case ILL_ILLOPC:
1549 reason = ProcessMessage::eIllegalOpcode;
1550 break;
1551 case ILL_ILLOPN:
1552 reason = ProcessMessage::eIllegalOperand;
1553 break;
1554 case ILL_ILLADR:
1555 reason = ProcessMessage::eIllegalAddressingMode;
1556 break;
1557 case ILL_ILLTRP:
1558 reason = ProcessMessage::eIllegalTrap;
1559 break;
1560 case ILL_PRVOPC:
1561 reason = ProcessMessage::ePrivilegedOpcode;
1562 break;
1563 case ILL_PRVREG:
1564 reason = ProcessMessage::ePrivilegedRegister;
1565 break;
1566 case ILL_COPROC:
1567 reason = ProcessMessage::eCoprocessorError;
1568 break;
1569 case ILL_BADSTK:
1570 reason = ProcessMessage::eInternalStackError;
1571 break;
1572 }
1573
1574 return reason;
1575}
1576
1577ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001578ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001579{
1580 ProcessMessage::CrashReason reason;
1581 assert(info->si_signo == SIGFPE);
1582
1583 reason = ProcessMessage::eInvalidCrashReason;
1584
1585 switch (info->si_code)
1586 {
1587 default:
1588 assert(false && "unexpected si_code for SIGFPE");
1589 break;
1590 case FPE_INTDIV:
1591 reason = ProcessMessage::eIntegerDivideByZero;
1592 break;
1593 case FPE_INTOVF:
1594 reason = ProcessMessage::eIntegerOverflow;
1595 break;
1596 case FPE_FLTDIV:
1597 reason = ProcessMessage::eFloatDivideByZero;
1598 break;
1599 case FPE_FLTOVF:
1600 reason = ProcessMessage::eFloatOverflow;
1601 break;
1602 case FPE_FLTUND:
1603 reason = ProcessMessage::eFloatUnderflow;
1604 break;
1605 case FPE_FLTRES:
1606 reason = ProcessMessage::eFloatInexactResult;
1607 break;
1608 case FPE_FLTINV:
1609 reason = ProcessMessage::eFloatInvalidOperation;
1610 break;
1611 case FPE_FLTSUB:
1612 reason = ProcessMessage::eFloatSubscriptRange;
1613 break;
1614 }
1615
1616 return reason;
1617}
1618
1619ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001620ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001621{
1622 ProcessMessage::CrashReason reason;
1623 assert(info->si_signo == SIGBUS);
1624
1625 reason = ProcessMessage::eInvalidCrashReason;
1626
1627 switch (info->si_code)
1628 {
1629 default:
1630 assert(false && "unexpected si_code for SIGBUS");
1631 break;
1632 case BUS_ADRALN:
1633 reason = ProcessMessage::eIllegalAlignment;
1634 break;
1635 case BUS_ADRERR:
1636 reason = ProcessMessage::eIllegalAddress;
1637 break;
1638 case BUS_OBJERR:
1639 reason = ProcessMessage::eHardwareError;
1640 break;
1641 }
1642
1643 return reason;
1644}
1645
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001646void
Johnny Chen25e68e32011-06-14 19:19:50 +00001647ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001648{
1649 int status;
1650 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001651
Stephen Wilson570243b2011-01-19 01:37:06 +00001652 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001653
1654 fdset.fd = monitor->m_server_fd;
1655 fdset.events = POLLIN | POLLPRI;
1656 fdset.revents = 0;
1657
Stephen Wilson570243b2011-01-19 01:37:06 +00001658 // We are finised with the arguments and are ready to go. Sync with the
1659 // parent thread and start serving operations on the inferior.
1660 sem_post(&args->m_semaphore);
1661
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001662 for (;;)
1663 {
1664 if ((status = poll(&fdset, 1, -1)) < 0)
1665 {
1666 switch (errno)
1667 {
1668 default:
1669 assert(false && "Unexpected poll() failure!");
1670 continue;
1671
1672 case EINTR: continue; // Just poll again.
1673 case EBADF: return; // Connection terminated.
1674 }
1675 }
1676
1677 assert(status == 1 && "Too many descriptors!");
1678
1679 if (fdset.revents & POLLIN)
1680 {
1681 Operation *op = NULL;
1682
1683 READ_AGAIN:
1684 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1685 {
1686 // There is only one acceptable failure.
1687 assert(errno == EINTR);
1688 goto READ_AGAIN;
1689 }
Matt Kopec9eb40a92013-03-14 21:35:26 +00001690 if (status == 0)
1691 continue; // Poll again. The connection probably terminated.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001692 assert(status == sizeof(op));
1693 op->Execute(monitor);
1694 write(fdset.fd, &op, sizeof(op));
1695 }
1696 }
1697}
1698
1699void
1700ProcessMonitor::DoOperation(Operation *op)
1701{
1702 int status;
1703 Operation *ack = NULL;
1704 Mutex::Locker lock(m_server_mutex);
1705
1706 // FIXME: Do proper error checking here.
1707 write(m_client_fd, &op, sizeof(op));
1708
1709READ_AGAIN:
1710 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1711 {
1712 // If interrupted by a signal handler try again. Otherwise the monitor
1713 // thread probably died and we have a stale file descriptor -- abort the
1714 // operation.
1715 if (errno == EINTR)
1716 goto READ_AGAIN;
1717 return;
1718 }
1719
1720 assert(status == sizeof(ack));
1721 assert(ack == op && "Invalid monitor thread response!");
1722}
1723
1724size_t
1725ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1726 Error &error)
1727{
1728 size_t result;
1729 ReadOperation op(vm_addr, buf, size, error, result);
1730 DoOperation(&op);
1731 return result;
1732}
1733
1734size_t
1735ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1736 lldb_private::Error &error)
1737{
1738 size_t result;
1739 WriteOperation op(vm_addr, buf, size, error, result);
1740 DoOperation(&op);
1741 return result;
1742}
1743
1744bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001745ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00001746 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001747{
1748 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001749 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001750 DoOperation(&op);
1751 return result;
1752}
1753
1754bool
Matt Kopec7de48462013-03-06 17:20:48 +00001755ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001756 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001757{
1758 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001759 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001760 DoOperation(&op);
1761 return result;
1762}
1763
1764bool
Matt Kopec7de48462013-03-06 17:20:48 +00001765ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001766{
1767 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001768 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001769 DoOperation(&op);
1770 return result;
1771}
1772
1773bool
Matt Kopec7de48462013-03-06 17:20:48 +00001774ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001775{
1776 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001777 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001778 DoOperation(&op);
1779 return result;
1780}
1781
1782bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001783ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1784{
1785 bool result;
1786 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
1787 DoOperation(&op);
1788 return result;
1789}
1790
1791bool
Matt Kopec7de48462013-03-06 17:20:48 +00001792ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001793{
1794 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001795 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001796 DoOperation(&op);
1797 return result;
1798}
1799
1800bool
Matt Kopec7de48462013-03-06 17:20:48 +00001801ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001802{
1803 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001804 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001805 DoOperation(&op);
1806 return result;
1807}
1808
1809bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001810ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1811{
1812 bool result;
1813 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
1814 DoOperation(&op);
1815 return result;
1816}
1817
1818bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001819ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001820{
1821 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001822 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001823 DoOperation(&op);
1824 return result;
1825}
1826
1827bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001828ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001829{
1830 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001831 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001832 DoOperation(&op);
1833 return result;
1834}
1835
1836bool
1837ProcessMonitor::BringProcessIntoLimbo()
1838{
1839 bool result;
1840 KillOperation op(result);
1841 DoOperation(&op);
1842 return result;
1843}
1844
1845bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001846ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001847{
1848 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001849 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001850 DoOperation(&op);
1851 return result;
1852}
1853
1854bool
1855ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1856{
1857 bool result;
1858 EventMessageOperation op(tid, message, result);
1859 DoOperation(&op);
1860 return result;
1861}
1862
Greg Clayton743ecf42012-10-16 20:20:18 +00001863lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +00001864ProcessMonitor::Detach()
1865{
Greg Clayton28041352011-11-29 20:50:10 +00001866 lldb_private::Error error;
Greg Clayton743ecf42012-10-16 20:20:18 +00001867 if (m_pid != LLDB_INVALID_PROCESS_ID) {
1868 DetachOperation op(error);
1869 DoOperation(&op);
1870 }
Greg Clayton743ecf42012-10-16 20:20:18 +00001871 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00001872}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001873
1874bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001875ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1876{
Peter Collingbourne62343202011-06-14 03:55:54 +00001877 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001878
1879 if (target_fd == -1)
1880 return false;
1881
Peter Collingbourne62343202011-06-14 03:55:54 +00001882 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001883}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001884
1885void
1886ProcessMonitor::StopMonitoringChildProcess()
1887{
1888 lldb::thread_result_t thread_result;
1889
Stephen Wilsond4182f42011-02-09 20:10:35 +00001890 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001891 {
1892 Host::ThreadCancel(m_monitor_thread, NULL);
1893 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1894 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1895 }
1896}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001897
1898void
1899ProcessMonitor::StopMonitor()
1900{
1901 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00001902 StopOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001903 CloseFD(m_terminal_fd);
1904 CloseFD(m_client_fd);
1905 CloseFD(m_server_fd);
1906}
1907
1908void
Greg Clayton743ecf42012-10-16 20:20:18 +00001909ProcessMonitor::StopOpThread()
1910{
1911 lldb::thread_result_t result;
1912
1913 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1914 return;
1915
1916 Host::ThreadCancel(m_operation_thread, NULL);
1917 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001918 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00001919}
1920
1921void
Stephen Wilson84ffe702011-03-30 15:55:52 +00001922ProcessMonitor::CloseFD(int &fd)
1923{
1924 if (fd != -1)
1925 {
1926 close(fd);
1927 fd = -1;
1928 }
1929}