blob: 9e5f844bd13e6bd094ce16af7c988c7d5abb3fca [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 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000125#ifdef PT_SETREGS
Greg Clayton542e4072012-09-07 17:49:29 +0000126 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000127 {
Matt Kopec7de48462013-03-06 17:20:48 +0000128 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000129 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
130 break;
131 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000132#endif
133#ifdef PT_SETFPREGS
Greg Clayton386ff182011-11-05 01:09:16 +0000134 case PTRACE_SETFPREGS:
135 {
Matt Kopec7de48462013-03-06 17:20:48 +0000136 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000137 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
138 break;
139 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000140#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000141 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000142 {
143 DisplayBytes(buf, data, sizeof(siginfo_t));
144 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
145 break;
146 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000147 case PTRACE_SETREGSET:
148 {
149 // Extract iov_base from data, which is a pointer to the struct IOVEC
150 DisplayBytes(buf, *(void **)data, data_size);
151 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
152 break;
153 }
Greg Clayton386ff182011-11-05 01:09:16 +0000154 default:
155 {
156 }
157 }
158 }
159}
160
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000161// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000162// 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 +0000163extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000164PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000165 const char* reqName, const char* file, int line)
166{
Greg Clayton386ff182011-11-05 01:09:16 +0000167 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000168
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000169 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000170
Matt Kopec7de48462013-03-06 17:20:48 +0000171 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000172
173 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000174 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000175 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000176 else
Todd Fiala4507f062014-02-27 20:46:12 +0000177 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000178
Ed Mastec099c952014-02-24 14:07:45 +0000179 if (log)
180 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
181 reqName, pid, addr, data, data_size, result, file, line);
182
Matt Kopec7de48462013-03-06 17:20:48 +0000183 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000184
Matt Kopec7de48462013-03-06 17:20:48 +0000185 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000186 {
187 const char* str;
188 switch (errno)
189 {
190 case ESRCH: str = "ESRCH"; break;
191 case EINVAL: str = "EINVAL"; break;
192 case EBUSY: str = "EBUSY"; break;
193 case EPERM: str = "EPERM"; break;
194 default: str = "<unknown>";
195 }
196 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
197 }
198
199 return result;
200}
201
Matt Kopec7de48462013-03-06 17:20:48 +0000202// Wrapper for ptrace when logging is not required.
203// Sets errno to 0 prior to calling ptrace.
204extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000205PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000206{
Matt Kopec58c0b962013-03-20 20:34:35 +0000207 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000208 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000209 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
210 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
211 else
212 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000213 return result;
214}
215
216#define PTRACE(req, pid, addr, data, data_size) \
217 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000218#else
Matt Kopec7de48462013-03-06 17:20:48 +0000219 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000220#endif
221
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000222//------------------------------------------------------------------------------
223// Static implementations of ProcessMonitor::ReadMemory and
224// ProcessMonitor::WriteMemory. This enables mutual recursion between these
225// functions without needed to go thru the thread funnel.
226
227static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000228DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
230{
Greg Clayton542e4072012-09-07 17:49:29 +0000231 // ptrace word size is determined by the host, not the child
232 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000233 unsigned char *dst = static_cast<unsigned char*>(buf);
234 size_t bytes_read;
235 size_t remainder;
236 long data;
237
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000238 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000239 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000240 ProcessPOSIXLog::IncNestLevel();
241 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000242 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000243 pid, word_size, (void*)vm_addr, buf, size);
244
245 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000246 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
247 {
248 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000249 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
250 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 {
252 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000253 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000254 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255 return bytes_read;
256 }
257
258 remainder = size - bytes_read;
259 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260
261 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000262 for (unsigned i = 0; i < remainder; ++i)
263 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000264
Johnny Chen30213ff2012-01-05 19:17:38 +0000265 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
266 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
267 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
268 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000269 {
270 uintptr_t print_dst = 0;
271 // Format bytes from data by moving into print_dst for log output
272 for (unsigned i = 0; i < remainder; ++i)
273 print_dst |= (((data >> i*8) & 0xFF) << i*8);
274 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
275 (void*)vm_addr, print_dst, (unsigned long)data);
276 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000277
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000278 vm_addr += word_size;
279 dst += word_size;
280 }
281
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000282 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000283 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000284 return bytes_read;
285}
286
287static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000288DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000289 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
290{
Greg Clayton542e4072012-09-07 17:49:29 +0000291 // ptrace word size is determined by the host, not the child
292 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000293 const unsigned char *src = static_cast<const unsigned char*>(buf);
294 size_t bytes_written = 0;
295 size_t remainder;
296
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000297 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000299 ProcessPOSIXLog::IncNestLevel();
300 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000301 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000302 pid, word_size, (void*)vm_addr, buf, size);
303
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000304 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
305 {
306 remainder = size - bytes_written;
307 remainder = remainder > word_size ? word_size : remainder;
308
309 if (remainder == word_size)
310 {
311 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000312 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000313 for (unsigned i = 0; i < word_size; ++i)
314 data |= (unsigned long)src[i] << i*8;
315
Johnny Chen30213ff2012-01-05 19:17:38 +0000316 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
317 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
318 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
319 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000320 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
321 (void*)vm_addr, *(unsigned long*)src, data);
322
Matt Kopec7de48462013-03-06 17:20:48 +0000323 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000324 {
325 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000326 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000327 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000328 return bytes_written;
329 }
330 }
331 else
332 {
333 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000334 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000335 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000336 {
337 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000338 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000339 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000340 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341
342 memcpy(buff, src, remainder);
343
Greg Clayton542e4072012-09-07 17:49:29 +0000344 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 {
347 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000348 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000349 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000350 }
351
Johnny Chen30213ff2012-01-05 19:17:38 +0000352 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
353 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
354 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
355 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000356 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
357 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000358 }
359
360 vm_addr += word_size;
361 src += word_size;
362 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000363 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000364 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000365 return bytes_written;
366}
367
Stephen Wilson26977162011-03-23 02:14:42 +0000368// Simple helper function to ensure flags are enabled on the given file
369// descriptor.
370static bool
371EnsureFDFlags(int fd, int flags, Error &error)
372{
373 int status;
374
375 if ((status = fcntl(fd, F_GETFL)) == -1)
376 {
377 error.SetErrorToErrno();
378 return false;
379 }
380
381 if (fcntl(fd, F_SETFL, status | flags) == -1)
382 {
383 error.SetErrorToErrno();
384 return false;
385 }
386
387 return true;
388}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000389
390//------------------------------------------------------------------------------
391/// @class Operation
392/// @brief Represents a ProcessMonitor operation.
393///
394/// Under Linux, it is not possible to ptrace() from any other thread but the
395/// one that spawned or attached to the process from the start. Therefore, when
396/// a ProcessMonitor is asked to deliver or change the state of an inferior
397/// process the operation must be "funneled" to a specific thread to perform the
398/// task. The Operation class provides an abstract base for all services the
399/// ProcessMonitor must perform via the single virtual function Execute, thus
400/// encapsulating the code that needs to run in the privileged context.
401class Operation
402{
403public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000404 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000405 virtual void Execute(ProcessMonitor *monitor) = 0;
406};
407
408//------------------------------------------------------------------------------
409/// @class ReadOperation
410/// @brief Implements ProcessMonitor::ReadMemory.
411class ReadOperation : public Operation
412{
413public:
414 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
415 Error &error, size_t &result)
416 : m_addr(addr), m_buff(buff), m_size(size),
417 m_error(error), m_result(result)
418 { }
419
420 void Execute(ProcessMonitor *monitor);
421
422private:
423 lldb::addr_t m_addr;
424 void *m_buff;
425 size_t m_size;
426 Error &m_error;
427 size_t &m_result;
428};
429
430void
431ReadOperation::Execute(ProcessMonitor *monitor)
432{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000433 lldb::pid_t pid = monitor->GetPID();
434
Greg Clayton542e4072012-09-07 17:49:29 +0000435 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000436}
437
438//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000439/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000440/// @brief Implements ProcessMonitor::WriteMemory.
441class WriteOperation : public Operation
442{
443public:
444 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
445 Error &error, size_t &result)
446 : m_addr(addr), m_buff(buff), m_size(size),
447 m_error(error), m_result(result)
448 { }
449
450 void Execute(ProcessMonitor *monitor);
451
452private:
453 lldb::addr_t m_addr;
454 const void *m_buff;
455 size_t m_size;
456 Error &m_error;
457 size_t &m_result;
458};
459
460void
461WriteOperation::Execute(ProcessMonitor *monitor)
462{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000463 lldb::pid_t pid = monitor->GetPID();
464
Greg Clayton542e4072012-09-07 17:49:29 +0000465 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000466}
467
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000468
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000469//------------------------------------------------------------------------------
470/// @class ReadRegOperation
471/// @brief Implements ProcessMonitor::ReadRegisterValue.
472class ReadRegOperation : public Operation
473{
474public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000475 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000476 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000477 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000478 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000479 { }
480
481 void Execute(ProcessMonitor *monitor);
482
483private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000484 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000485 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000486 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000487 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000488 bool &m_result;
489};
490
491void
492ReadRegOperation::Execute(ProcessMonitor *monitor)
493{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000494 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000495
496 // Set errno to zero so that we can detect a failed peek.
497 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000498 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
499 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000500 m_result = false;
501 else
502 {
503 m_value = data;
504 m_result = true;
505 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000506 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000507 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000508 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000509}
510
511//------------------------------------------------------------------------------
512/// @class WriteRegOperation
513/// @brief Implements ProcessMonitor::WriteRegisterValue.
514class WriteRegOperation : public Operation
515{
516public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000517 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000518 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000519 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000520 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000521 { }
522
523 void Execute(ProcessMonitor *monitor);
524
525private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000526 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000527 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000528 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000529 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000530 bool &m_result;
531};
532
533void
534WriteRegOperation::Execute(ProcessMonitor *monitor)
535{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000536 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000537 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000538
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000539 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000540
541 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000542 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000543 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000544 m_result = false;
545 else
546 m_result = true;
547}
548
549//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000550/// @class ReadGPROperation
551/// @brief Implements ProcessMonitor::ReadGPR.
552class ReadGPROperation : public Operation
553{
554public:
Matt Kopec7de48462013-03-06 17:20:48 +0000555 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
556 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000557 { }
558
559 void Execute(ProcessMonitor *monitor);
560
561private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000562 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000563 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000564 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000565 bool &m_result;
566};
567
568void
569ReadGPROperation::Execute(ProcessMonitor *monitor)
570{
Todd Fialad35f2b92014-06-23 15:59:04 +0000571#ifdef PT_GETREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000572 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000573 m_result = false;
574 else
575 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000576#else
577 m_result = false;
578#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000579}
580
581//------------------------------------------------------------------------------
582/// @class ReadFPROperation
583/// @brief Implements ProcessMonitor::ReadFPR.
584class ReadFPROperation : public Operation
585{
586public:
Matt Kopec7de48462013-03-06 17:20:48 +0000587 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
588 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000589 { }
590
591 void Execute(ProcessMonitor *monitor);
592
593private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000594 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000595 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000596 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000597 bool &m_result;
598};
599
600void
601ReadFPROperation::Execute(ProcessMonitor *monitor)
602{
Todd Fialad35f2b92014-06-23 15:59:04 +0000603#ifdef PT_GETFPREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000604 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000605 m_result = false;
606 else
607 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000608#else
609 m_result = false;
610#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000611}
612
613//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000614/// @class ReadRegisterSetOperation
615/// @brief Implements ProcessMonitor::ReadRegisterSet.
616class ReadRegisterSetOperation : public Operation
617{
618public:
619 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
620 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
621 { }
622
623 void Execute(ProcessMonitor *monitor);
624
625private:
626 lldb::tid_t m_tid;
627 void *m_buf;
628 size_t m_buf_size;
629 const unsigned int m_regset;
630 bool &m_result;
631};
632
633void
634ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
635{
636 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
637 m_result = false;
638 else
639 m_result = true;
640}
641
642//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000643/// @class WriteGPROperation
644/// @brief Implements ProcessMonitor::WriteGPR.
645class WriteGPROperation : public Operation
646{
647public:
Matt Kopec7de48462013-03-06 17:20:48 +0000648 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
649 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000650 { }
651
652 void Execute(ProcessMonitor *monitor);
653
654private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000655 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000656 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000657 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000658 bool &m_result;
659};
660
661void
662WriteGPROperation::Execute(ProcessMonitor *monitor)
663{
Todd Fialad35f2b92014-06-23 15:59:04 +0000664#ifdef PT_SETREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000665 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000666 m_result = false;
667 else
668 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000669#else
670 m_result = false;
671#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000672}
673
674//------------------------------------------------------------------------------
675/// @class WriteFPROperation
676/// @brief Implements ProcessMonitor::WriteFPR.
677class WriteFPROperation : public Operation
678{
679public:
Matt Kopec7de48462013-03-06 17:20:48 +0000680 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
681 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000682 { }
683
684 void Execute(ProcessMonitor *monitor);
685
686private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000687 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000688 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000689 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000690 bool &m_result;
691};
692
693void
694WriteFPROperation::Execute(ProcessMonitor *monitor)
695{
Todd Fialad35f2b92014-06-23 15:59:04 +0000696#ifdef PT_SETFPREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000697 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000698 m_result = false;
699 else
700 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000701#else
702 m_result = false;
703#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000704}
705
706//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000707/// @class WriteRegisterSetOperation
708/// @brief Implements ProcessMonitor::WriteRegisterSet.
709class WriteRegisterSetOperation : public Operation
710{
711public:
712 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
713 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
714 { }
715
716 void Execute(ProcessMonitor *monitor);
717
718private:
719 lldb::tid_t m_tid;
720 void *m_buf;
721 size_t m_buf_size;
722 const unsigned int m_regset;
723 bool &m_result;
724};
725
726void
727WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
728{
729 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
730 m_result = false;
731 else
732 m_result = true;
733}
734
735//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000736/// @class ReadThreadPointerOperation
737/// @brief Implements ProcessMonitor::ReadThreadPointer.
738class ReadThreadPointerOperation : public Operation
739{
740public:
741 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
742 : m_tid(tid), m_addr(addr), m_result(result)
743 { }
744
745 void Execute(ProcessMonitor *monitor);
746
747private:
748 lldb::tid_t m_tid;
749 lldb::addr_t *m_addr;
750 bool &m_result;
751};
752
753void
754ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
755{
756 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
757 if (log)
758 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
759
760 // The process for getting the thread area on Linux is
761 // somewhat... obscure. There's several different ways depending on
762 // what arch you're on, and what kernel version you have.
763
764 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
765 switch(arch.GetMachine())
766 {
Todd Fiala720cd3f2014-06-16 14:49:28 +0000767#if defined(__i386__) || defined(__x86_64__)
768 // Note that struct user below has a field named i387 which is x86-specific.
769 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000770 case llvm::Triple::x86:
771 {
772 // Find the GS register location for our host architecture.
773 size_t gs_user_offset = offsetof(struct user, regs);
774#ifdef __x86_64__
775 gs_user_offset += offsetof(struct user_regs_struct, gs);
776#endif
777#ifdef __i386__
778 gs_user_offset += offsetof(struct user_regs_struct, xgs);
779#endif
780
781 // Read the GS register value to get the selector.
782 errno = 0;
783 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
784 if (errno)
785 {
786 m_result = false;
787 break;
788 }
789
790 // Read the LDT base for that selector.
791 uint32_t tmp[4];
792 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
793 *m_addr = tmp[1];
794 break;
795 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000796#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000797 case llvm::Triple::x86_64:
798 // Read the FS register base.
799 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
800 break;
801 default:
802 m_result = false;
803 break;
804 }
805}
806
807//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000808/// @class ResumeOperation
809/// @brief Implements ProcessMonitor::Resume.
810class ResumeOperation : public Operation
811{
812public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000813 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
814 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000815
816 void Execute(ProcessMonitor *monitor);
817
818private:
819 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000820 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000821 bool &m_result;
822};
823
824void
825ResumeOperation::Execute(ProcessMonitor *monitor)
826{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000827 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000828
829 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
830 data = m_signo;
831
Matt Kopec7de48462013-03-06 17:20:48 +0000832 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000833 {
834 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
835
836 if (log)
837 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000839 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000840 else
841 m_result = true;
842}
843
844//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000845/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000846/// @brief Implements ProcessMonitor::SingleStep.
847class SingleStepOperation : public Operation
848{
849public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000850 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
851 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000852
853 void Execute(ProcessMonitor *monitor);
854
855private:
856 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000857 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000858 bool &m_result;
859};
860
861void
862SingleStepOperation::Execute(ProcessMonitor *monitor)
863{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000864 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000865
866 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
867 data = m_signo;
868
Matt Kopec7de48462013-03-06 17:20:48 +0000869 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000870 m_result = false;
871 else
872 m_result = true;
873}
874
875//------------------------------------------------------------------------------
876/// @class SiginfoOperation
877/// @brief Implements ProcessMonitor::GetSignalInfo.
878class SiginfoOperation : public Operation
879{
880public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000881 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
882 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000883
884 void Execute(ProcessMonitor *monitor);
885
886private:
887 lldb::tid_t m_tid;
888 void *m_info;
889 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000890 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000891};
892
893void
894SiginfoOperation::Execute(ProcessMonitor *monitor)
895{
Matt Kopec7de48462013-03-06 17:20:48 +0000896 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000897 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000898 m_err = errno;
899 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000900 else
901 m_result = true;
902}
903
904//------------------------------------------------------------------------------
905/// @class EventMessageOperation
906/// @brief Implements ProcessMonitor::GetEventMessage.
907class EventMessageOperation : public Operation
908{
909public:
910 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
911 : m_tid(tid), m_message(message), m_result(result) { }
912
913 void Execute(ProcessMonitor *monitor);
914
915private:
916 lldb::tid_t m_tid;
917 unsigned long *m_message;
918 bool &m_result;
919};
920
921void
922EventMessageOperation::Execute(ProcessMonitor *monitor)
923{
Matt Kopec7de48462013-03-06 17:20:48 +0000924 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000925 m_result = false;
926 else
927 m_result = true;
928}
929
930//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000931/// @class DetachOperation
932/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000933class DetachOperation : public Operation
934{
935public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000936 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000937
938 void Execute(ProcessMonitor *monitor);
939
940private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000941 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000942 Error &m_error;
943};
944
945void
946DetachOperation::Execute(ProcessMonitor *monitor)
947{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000948 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000949 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000950}
951
Johnny Chen25e68e32011-06-14 19:19:50 +0000952ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
953 : m_monitor(monitor)
954{
955 sem_init(&m_semaphore, 0, 0);
956}
957
958ProcessMonitor::OperationArgs::~OperationArgs()
959{
960 sem_destroy(&m_semaphore);
961}
962
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000963ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
964 lldb_private::Module *module,
965 char const **argv,
966 char const **envp,
967 const char *stdin_path,
968 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000969 const char *stderr_path,
970 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000971 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000972 m_module(module),
973 m_argv(argv),
974 m_envp(envp),
975 m_stdin_path(stdin_path),
976 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000977 m_stderr_path(stderr_path),
978 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000979
980ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000981{ }
982
983ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
984 lldb::pid_t pid)
985 : OperationArgs(monitor), m_pid(pid) { }
986
987ProcessMonitor::AttachArgs::~AttachArgs()
988{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000989
990//------------------------------------------------------------------------------
991/// The basic design of the ProcessMonitor is built around two threads.
992///
993/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
994/// for changes in the debugee state. When a change is detected a
995/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
996/// "drives" state changes in the debugger.
997///
998/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000999/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001000/// operations such as register reads/writes, stepping, etc. See the comments
1001/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001002ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001003 Module *module,
1004 const char *argv[],
1005 const char *envp[],
1006 const char *stdin_path,
1007 const char *stdout_path,
1008 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001009 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001010 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001011 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001013 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001014 m_pid(LLDB_INVALID_PROCESS_ID),
1015 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001016 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017{
Daniel Malea1efb4182013-09-16 23:12:18 +00001018 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1019 stdin_path, stdout_path, stderr_path,
1020 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001021
Daniel Malea1efb4182013-09-16 23:12:18 +00001022 sem_init(&m_operation_pending, 0, 0);
1023 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001024
Johnny Chen25e68e32011-06-14 19:19:50 +00001025 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001026 if (!error.Success())
1027 return;
1028
1029WAIT_AGAIN:
1030 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001031 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001032 {
1033 if (errno == EINTR)
1034 goto WAIT_AGAIN;
1035 else
1036 {
1037 error.SetErrorToErrno();
1038 return;
1039 }
1040 }
1041
1042 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001043 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001044 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001045 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001046 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001047 return;
1048 }
1049
1050 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001051 m_monitor_thread = Host::StartMonitoringChildProcess(
1052 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001053 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 {
1055 error.SetErrorToGenericError();
1056 error.SetErrorString("Process launch failed.");
1057 return;
1058 }
1059}
1060
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001061ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001062 lldb::pid_t pid,
1063 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001064 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001065 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001066 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001067 m_pid(LLDB_INVALID_PROCESS_ID),
1068 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001069 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001070{
Daniel Malea1efb4182013-09-16 23:12:18 +00001071 sem_init(&m_operation_pending, 0, 0);
1072 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001073
Daniel Malea1efb4182013-09-16 23:12:18 +00001074 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001075
1076 StartAttachOpThread(args.get(), error);
1077 if (!error.Success())
1078 return;
1079
1080WAIT_AGAIN:
1081 // Wait for the operation thread to initialize.
1082 if (sem_wait(&args->m_semaphore))
1083 {
1084 if (errno == EINTR)
1085 goto WAIT_AGAIN;
1086 else
1087 {
1088 error.SetErrorToErrno();
1089 return;
1090 }
1091 }
1092
Greg Clayton743ecf42012-10-16 20:20:18 +00001093 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001094 if (!args->m_error.Success())
1095 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001096 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001097 error = args->m_error;
1098 return;
1099 }
1100
1101 // Finally, start monitoring the child process for change in state.
1102 m_monitor_thread = Host::StartMonitoringChildProcess(
1103 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1104 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1105 {
1106 error.SetErrorToGenericError();
1107 error.SetErrorString("Process attach failed.");
1108 return;
1109 }
1110}
1111
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001112ProcessMonitor::~ProcessMonitor()
1113{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001114 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001115}
1116
1117//------------------------------------------------------------------------------
1118// Thread setup and tear down.
1119void
Johnny Chen25e68e32011-06-14 19:19:50 +00001120ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001121{
1122 static const char *g_thread_name = "lldb.process.linux.operation";
1123
Stephen Wilsond4182f42011-02-09 20:10:35 +00001124 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001125 return;
1126
1127 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001128 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001129}
1130
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001131void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001132ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001133{
1134 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1135
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001136 if (!Launch(args)) {
1137 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001139 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001140
Stephen Wilson570243b2011-01-19 01:37:06 +00001141 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001142 return NULL;
1143}
1144
1145bool
1146ProcessMonitor::Launch(LaunchArgs *args)
1147{
1148 ProcessMonitor *monitor = args->m_monitor;
1149 ProcessLinux &process = monitor->GetProcess();
1150 const char **argv = args->m_argv;
1151 const char **envp = args->m_envp;
1152 const char *stdin_path = args->m_stdin_path;
1153 const char *stdout_path = args->m_stdout_path;
1154 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001155 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001156
1157 lldb_utility::PseudoTerminal terminal;
1158 const size_t err_len = 1024;
1159 char err_str[err_len];
1160 lldb::pid_t pid;
1161
1162 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001163 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001164
Stephen Wilson57740ec2011-01-15 00:12:41 +00001165 // Propagate the environment if one is not supplied.
1166 if (envp == NULL || envp[0] == NULL)
1167 envp = const_cast<const char **>(environ);
1168
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001169 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001170 {
1171 args->m_error.SetErrorToGenericError();
1172 args->m_error.SetErrorString("Process fork failed.");
1173 goto FINISH;
1174 }
1175
Peter Collingbourne6a520222011-06-14 03:55:58 +00001176 // Recognized child exit status codes.
1177 enum {
1178 ePtraceFailed = 1,
1179 eDupStdinFailed,
1180 eDupStdoutFailed,
1181 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001182 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001183 eExecFailed,
1184 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001185 };
1186
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001187 // Child process.
1188 if (pid == 0)
1189 {
1190 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001191 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001192 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001193
1194 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001195 if (setgid(getgid()) != 0)
1196 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001197
1198 // Let us have our own process group.
1199 setpgid(0, 0);
1200
Greg Clayton710dd5a2011-01-08 20:28:42 +00001201 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001202 //
1203 // FIXME: If two or more of the paths are the same we needlessly open
1204 // the same file multiple times.
1205 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001206 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001207 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001208
1209 if (stdout_path != NULL && stdout_path[0])
1210 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001211 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001212
1213 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001214 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001215 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001216
Daniel Malea6217d2a2013-01-08 14:49:22 +00001217 // Change working directory
1218 if (working_dir != NULL && working_dir[0])
1219 if (0 != ::chdir(working_dir))
1220 exit(eChdirFailed);
1221
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001222 // Execute. We should never return.
1223 execve(argv[0],
1224 const_cast<char *const *>(argv),
1225 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001226 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001227 }
1228
1229 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001230 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001231 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001232 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001233
1234 raw_pid = waitpid(pid, &status, 0);
1235 wpid = static_cast <lldb::pid_t> (raw_pid);
1236 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001237 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001238 args->m_error.SetErrorToErrno();
1239 goto FINISH;
1240 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001241 else if (WIFEXITED(status))
1242 {
1243 // open, dup or execve likely failed for some reason.
1244 args->m_error.SetErrorToGenericError();
1245 switch (WEXITSTATUS(status))
1246 {
Greg Clayton542e4072012-09-07 17:49:29 +00001247 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001248 args->m_error.SetErrorString("Child ptrace failed.");
1249 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001250 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001251 args->m_error.SetErrorString("Child open stdin failed.");
1252 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001253 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001254 args->m_error.SetErrorString("Child open stdout failed.");
1255 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001256 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001257 args->m_error.SetErrorString("Child open stderr failed.");
1258 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001259 case eChdirFailed:
1260 args->m_error.SetErrorString("Child failed to set working directory.");
1261 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001262 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001263 args->m_error.SetErrorString("Child exec failed.");
1264 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001265 case eSetGidFailed:
1266 args->m_error.SetErrorString("Child setgid failed.");
1267 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001268 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001269 args->m_error.SetErrorString("Child returned unknown exit status.");
1270 break;
1271 }
1272 goto FINISH;
1273 }
1274 assert(WIFSTOPPED(status) && wpid == pid &&
1275 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001276
Matt Kopec085d6ce2013-05-31 22:00:07 +00001277 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001278 {
1279 args->m_error.SetErrorToErrno();
1280 goto FINISH;
1281 }
1282
1283 // Release the master terminal descriptor and pass it off to the
1284 // ProcessMonitor instance. Similarly stash the inferior pid.
1285 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1286 monitor->m_pid = pid;
1287
Stephen Wilson26977162011-03-23 02:14:42 +00001288 // Set the terminal fd to be in non blocking mode (it simplifies the
1289 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1290 // descriptor to read from).
1291 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1292 goto FINISH;
1293
Johnny Chen30213ff2012-01-05 19:17:38 +00001294 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001295 // FIXME: should we be letting UpdateThreadList handle this?
1296 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001297 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001298
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001299 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001300 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001302
Matt Kopecb2910442013-07-09 15:09:45 +00001303 process.AddThreadForInitialStopIfNeeded(pid);
1304
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001305 // Let our process instance know the thread has stopped.
1306 process.SendMessage(ProcessMessage::Trace(pid));
1307
1308FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001309 return args->m_error.Success();
1310}
1311
Johnny Chen25e68e32011-06-14 19:19:50 +00001312void
1313ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1314{
1315 static const char *g_thread_name = "lldb.process.linux.operation";
1316
1317 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1318 return;
1319
1320 m_operation_thread =
1321 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1322}
1323
Johnny Chen25e68e32011-06-14 19:19:50 +00001324void *
1325ProcessMonitor::AttachOpThread(void *arg)
1326{
1327 AttachArgs *args = static_cast<AttachArgs*>(arg);
1328
Greg Clayton743ecf42012-10-16 20:20:18 +00001329 if (!Attach(args)) {
1330 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001331 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001332 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001333
1334 ServeOperation(args);
1335 return NULL;
1336}
1337
1338bool
1339ProcessMonitor::Attach(AttachArgs *args)
1340{
1341 lldb::pid_t pid = args->m_pid;
1342
1343 ProcessMonitor *monitor = args->m_monitor;
1344 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001345 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001346 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001347
Matt Kopec085d6ce2013-05-31 22:00:07 +00001348 // Use a map to keep track of the threads which we have attached/need to attach.
1349 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001350 if (pid <= 1)
1351 {
1352 args->m_error.SetErrorToGenericError();
1353 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1354 goto FINISH;
1355 }
1356
Matt Kopec085d6ce2013-05-31 22:00:07 +00001357 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001358 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001359 for (Host::TidMap::iterator it = tids_to_attach.begin();
1360 it != tids_to_attach.end(); ++it)
1361 {
1362 if (it->second == false)
1363 {
1364 lldb::tid_t tid = it->first;
1365
1366 // Attach to the requested process.
1367 // An attach will cause the thread to stop with a SIGSTOP.
1368 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1369 {
1370 // No such thread. The thread may have exited.
1371 // More error handling may be needed.
1372 if (errno == ESRCH)
1373 {
1374 tids_to_attach.erase(it);
1375 continue;
1376 }
1377 else
1378 {
1379 args->m_error.SetErrorToErrno();
1380 goto FINISH;
1381 }
1382 }
1383
Todd Fiala9be50492014-07-01 16:30:53 +00001384 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001385 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1386 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001387 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001388 {
1389 // No such thread. The thread may have exited.
1390 // More error handling may be needed.
1391 if (errno == ESRCH)
1392 {
1393 tids_to_attach.erase(it);
1394 continue;
1395 }
1396 else
1397 {
1398 args->m_error.SetErrorToErrno();
1399 goto FINISH;
1400 }
1401 }
1402
1403 if (!SetDefaultPtraceOpts(tid))
1404 {
1405 args->m_error.SetErrorToErrno();
1406 goto FINISH;
1407 }
1408
1409 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001410 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001411
Matt Kopec085d6ce2013-05-31 22:00:07 +00001412 if (log)
1413 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1414 process.GetThreadList().AddThread(inferior);
1415 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001416 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001417 }
1418 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001419 }
1420
Matt Kopec085d6ce2013-05-31 22:00:07 +00001421 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001422 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001423 monitor->m_pid = pid;
1424 // Let our process instance know the thread has stopped.
1425 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001426 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001427 else
1428 {
1429 args->m_error.SetErrorToGenericError();
1430 args->m_error.SetErrorString("No such process.");
1431 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001432
1433 FINISH:
1434 return args->m_error.Success();
1435}
1436
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001437bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001438ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1439{
1440 long ptrace_opts = 0;
1441
1442 // Have the child raise an event on exit. This is used to keep the child in
1443 // limbo until it is destroyed.
1444 ptrace_opts |= PTRACE_O_TRACEEXIT;
1445
1446 // Have the tracer trace threads which spawn in the inferior process.
1447 // TODO: if we want to support tracing the inferiors' child, add the
1448 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1449 ptrace_opts |= PTRACE_O_TRACECLONE;
1450
1451 // Have the tracer notify us before execve returns
1452 // (needed to disable legacy SIGTRAP generation)
1453 ptrace_opts |= PTRACE_O_TRACEEXEC;
1454
1455 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1456}
1457
1458bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001459ProcessMonitor::MonitorCallback(void *callback_baton,
1460 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001461 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001462 int signal,
1463 int status)
1464{
1465 ProcessMessage message;
1466 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001467 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001468 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001469 bool stop_monitoring;
1470 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001471 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001472
Andrew Kaylor93132f52013-05-28 23:04:25 +00001473 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1474
1475 if (exited)
1476 {
1477 if (log)
1478 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1479 message = ProcessMessage::Exit(pid, status);
1480 process->SendMessage(message);
1481 return pid == process->GetID();
1482 }
1483
Daniel Maleaa35970a2012-11-23 18:09:58 +00001484 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1485 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001486 if (log)
1487 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001488 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1489 if (!monitor->Resume(pid, SIGSTOP)) {
1490 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1491 }
1492 stop_monitoring = false;
1493 } else {
1494 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1495 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001496 // stop the monitor thread if this is the main pid.
1497 if (log)
1498 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1499 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1500 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001501 // If we are going to stop monitoring, we need to notify our process object
1502 if (stop_monitoring)
1503 {
1504 message = ProcessMessage::Exit(pid, status);
1505 process->SendMessage(message);
1506 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001507 }
1508 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001509 else {
1510 switch (info.si_signo)
1511 {
1512 case SIGTRAP:
1513 message = MonitorSIGTRAP(monitor, &info, pid);
1514 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001515
Stephen Wilson84ffe702011-03-30 15:55:52 +00001516 default:
1517 message = MonitorSignal(monitor, &info, pid);
1518 break;
1519 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001520
Stephen Wilson84ffe702011-03-30 15:55:52 +00001521 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001522 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001523 }
1524
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001525 return stop_monitoring;
1526}
1527
1528ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001529ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001530 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001531{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001532 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001533
Andrew Kaylor93132f52013-05-28 23:04:25 +00001534 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1535
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001536 assert(monitor);
1537 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001538
Stephen Wilson84ffe702011-03-30 15:55:52 +00001539 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001540 {
1541 default:
1542 assert(false && "Unexpected SIGTRAP code!");
1543 break;
1544
Matt Kopeca360d7e2013-05-17 19:27:47 +00001545 // TODO: these two cases are required if we want to support tracing
1546 // of the inferiors' children
1547 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1548 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1549
Matt Kopec650648f2013-01-08 16:30:18 +00001550 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1551 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001552 if (log)
1553 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1554
Matt Kopec650648f2013-01-08 16:30:18 +00001555 unsigned long tid = 0;
1556 if (!monitor->GetEventMessage(pid, &tid))
1557 tid = -1;
1558 message = ProcessMessage::NewThread(pid, tid);
1559 break;
1560 }
1561
Matt Kopeca360d7e2013-05-17 19:27:47 +00001562 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001563 if (log)
1564 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1565
1566 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001567 break;
1568
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001569 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1570 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001571 // The inferior process or one of its threads is about to exit.
1572 // Maintain the process or thread in a state of "limbo" until we are
1573 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001574 unsigned long data = 0;
1575 if (!monitor->GetEventMessage(pid, &data))
1576 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001577 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001578 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001579 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001580 break;
1581 }
1582
1583 case 0:
1584 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001585 if (log)
1586 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001587 message = ProcessMessage::Trace(pid);
1588 break;
1589
1590 case SI_KERNEL:
1591 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001592 if (log)
1593 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001594 message = ProcessMessage::Break(pid);
1595 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001596
1597 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001598 if (log)
1599 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001600 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1601 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001602
1603 case SIGTRAP:
1604 case (SIGTRAP | 0x80):
1605 if (log)
1606 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1607 // Ignore these signals until we know more about them
1608 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001609 }
1610
1611 return message;
1612}
1613
Stephen Wilson84ffe702011-03-30 15:55:52 +00001614ProcessMessage
1615ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001616 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001617{
1618 ProcessMessage message;
1619 int signo = info->si_signo;
1620
Andrew Kaylor93132f52013-05-28 23:04:25 +00001621 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1622
Stephen Wilson84ffe702011-03-30 15:55:52 +00001623 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1624 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1625 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1626 //
1627 // IOW, user generated signals never generate what we consider to be a
1628 // "crash".
1629 //
1630 // Similarly, ACK signals generated by this monitor.
1631 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1632 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001633 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001634 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001635 __FUNCTION__,
1636 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1637 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1638 info->si_pid);
1639
Stephen Wilson84ffe702011-03-30 15:55:52 +00001640 if (info->si_pid == getpid())
1641 return ProcessMessage::SignalDelivered(pid, signo);
1642 else
1643 return ProcessMessage::Signal(pid, signo);
1644 }
1645
Andrew Kaylor93132f52013-05-28 23:04:25 +00001646 if (log)
1647 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1648
Stephen Wilson84ffe702011-03-30 15:55:52 +00001649 if (signo == SIGSEGV) {
1650 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1651 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1652 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1653 }
1654
1655 if (signo == SIGILL) {
1656 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1657 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1658 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1659 }
1660
1661 if (signo == SIGFPE) {
1662 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1663 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1664 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1665 }
1666
1667 if (signo == SIGBUS) {
1668 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1669 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1670 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1671 }
1672
1673 // Everything else is "normal" and does not require any special action on
1674 // our part.
1675 return ProcessMessage::Signal(pid, signo);
1676}
1677
Andrew Kaylord4d54992013-09-17 00:30:24 +00001678// On Linux, when a new thread is created, we receive to notifications,
1679// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1680// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1681// the new child thread indicating that it has is stopped because we attached.
1682// We have no guarantee of the order in which these arrive, but we need both
1683// before we are ready to proceed. We currently keep a list of threads which
1684// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1685// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1686// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1687
1688bool
1689ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1690{
1691 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1692 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001693 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001694
1695 // Wait for the thread to stop
1696 while (true)
1697 {
1698 int status = -1;
1699 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001700 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001701 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001702 if (status == -1)
1703 {
1704 // If we got interrupted by a signal (in our process, not the
1705 // inferior) try again.
1706 if (errno == EINTR)
1707 continue;
1708 else
1709 {
1710 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001711 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001712 return false; // This is bad, but there's nothing we can do.
1713 }
1714 }
1715
1716 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001717 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001718
Todd Fiala9be50492014-07-01 16:30:53 +00001719 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001720
1721 siginfo_t info;
1722 int ptrace_err;
1723 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1724 {
1725 if (log)
1726 {
1727 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1728 }
1729 return false;
1730 }
1731
1732 // If this is a thread exit, we won't get any more information.
1733 if (WIFEXITED(status))
1734 {
1735 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001736 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001737 return true;
1738 continue;
1739 }
1740
1741 assert(info.si_code == SI_USER);
1742 assert(WSTOPSIG(status) == SIGSTOP);
1743
1744 if (log)
1745 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1746 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1747 return true;
1748 }
1749 return false;
1750}
1751
Andrew Kaylor93132f52013-05-28 23:04:25 +00001752bool
1753ProcessMonitor::StopThread(lldb::tid_t tid)
1754{
1755 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1756
1757 // FIXME: Try to use tgkill or tkill
1758 int ret = tgkill(m_pid, tid, SIGSTOP);
1759 if (log)
1760 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1761
1762 // This can happen if a thread exited while we were trying to stop it. That's OK.
1763 // We'll get the signal for that later.
1764 if (ret < 0)
1765 return false;
1766
1767 // Wait for the thread to stop
1768 while (true)
1769 {
1770 int status = -1;
1771 if (log)
1772 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001773 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001774 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001775 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1776 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001777
Todd Fiala9be50492014-07-01 16:30:53 +00001778 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001779 {
1780 // If we got interrupted by a signal (in our process, not the
1781 // inferior) try again.
1782 if (errno == EINTR)
1783 continue;
1784 else
1785 return false; // This is bad, but there's nothing we can do.
1786 }
1787
1788 // If this is a thread exit, we won't get any more information.
1789 if (WIFEXITED(status))
1790 {
1791 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001792 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001793 return true;
1794 continue;
1795 }
1796
1797 siginfo_t info;
1798 int ptrace_err;
1799 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1800 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001801 // another signal causing a StopAllThreads may have been received
1802 // before wait_pid's group-stop was processed, handle it now
1803 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001804 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001805 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001806
Todd Fiala1b0539c2014-01-27 17:03:57 +00001807 if (log)
1808 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1809 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1810 if (!Resume(wait_pid, SIGSTOP)) {
1811 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1812 }
1813 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001814 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001815
1816 if (log)
1817 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001818 return false;
1819 }
1820
1821 // Handle events from other threads
1822 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001823 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1824 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001825
1826 ProcessMessage message;
1827 if (info.si_signo == SIGTRAP)
1828 message = MonitorSIGTRAP(this, &info, wait_pid);
1829 else
1830 message = MonitorSignal(this, &info, wait_pid);
1831
1832 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1833
1834 // When a new thread is created, we may get a SIGSTOP for the new thread
1835 // just before we get the SIGTRAP that we use to add the thread to our
1836 // process thread list. We don't need to worry about that signal here.
1837 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1838
1839 if (!thread)
1840 {
1841 m_process->SendMessage(message);
1842 continue;
1843 }
1844
1845 switch (message.GetKind())
1846 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00001847 case ProcessMessage::eExecMessage:
1848 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00001849 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001850 case ProcessMessage::eInvalidMessage:
1851 break;
1852
1853 // These need special handling because we don't want to send a
1854 // resume even if we already sent a SIGSTOP to this thread. In
1855 // this case the resume will cause the thread to disappear. It is
1856 // unlikely that we'll ever get eExitMessage here, but the same
1857 // reasoning applies.
1858 case ProcessMessage::eLimboMessage:
1859 case ProcessMessage::eExitMessage:
1860 if (log)
1861 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1862 // SendMessage will set the thread state as needed.
1863 m_process->SendMessage(message);
1864 // If this is the thread we're waiting for, stop waiting. Even
1865 // though this wasn't the signal we expected, it's the last
1866 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00001867 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001868 return true;
1869 break;
1870
Matt Kopecb2910442013-07-09 15:09:45 +00001871 case ProcessMessage::eSignalMessage:
1872 if (log)
1873 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1874 if (WSTOPSIG(status) == SIGSTOP)
1875 {
1876 m_process->AddThreadForInitialStopIfNeeded(tid);
1877 thread->SetState(lldb::eStateStopped);
1878 }
1879 else
1880 {
1881 m_process->SendMessage(message);
1882 // This isn't the stop we were expecting, but the thread is
1883 // stopped. SendMessage will handle processing of this event,
1884 // but we need to resume here to get the stop we are waiting
1885 // for (otherwise the thread will stop again immediately when
1886 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001887 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00001888 Resume(wait_pid, eResumeSignalNone);
1889 }
1890 break;
1891
Andrew Kaylor93132f52013-05-28 23:04:25 +00001892 case ProcessMessage::eSignalDeliveredMessage:
1893 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00001894 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
1895 WIFSTOPPED(status) &&
1896 WSTOPSIG(status) == SIGSTOP &&
1897 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001898 {
1899 if (log)
1900 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1901 thread->SetState(lldb::eStateStopped);
1902 return true;
1903 }
1904 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001905 case ProcessMessage::eBreakpointMessage:
1906 case ProcessMessage::eTraceMessage:
1907 case ProcessMessage::eWatchpointMessage:
1908 case ProcessMessage::eCrashMessage:
1909 case ProcessMessage::eNewThreadMessage:
1910 if (log)
1911 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1912 // SendMessage will set the thread state as needed.
1913 m_process->SendMessage(message);
1914 // This isn't the stop we were expecting, but the thread is
1915 // stopped. SendMessage will handle processing of this event,
1916 // but we need to resume here to get the stop we are waiting
1917 // for (otherwise the thread will stop again immediately when
1918 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001919 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001920 Resume(wait_pid, eResumeSignalNone);
1921 break;
1922 }
1923 }
1924 return false;
1925}
1926
Stephen Wilson84ffe702011-03-30 15:55:52 +00001927ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001928ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001929{
1930 ProcessMessage::CrashReason reason;
1931 assert(info->si_signo == SIGSEGV);
1932
1933 reason = ProcessMessage::eInvalidCrashReason;
1934
Greg Clayton542e4072012-09-07 17:49:29 +00001935 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001936 {
1937 default:
1938 assert(false && "unexpected si_code for SIGSEGV");
1939 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001940 case SI_KERNEL:
1941 // Linux will occasionally send spurious SI_KERNEL codes.
1942 // (this is poorly documented in sigaction)
1943 // One way to get this is via unaligned SIMD loads.
1944 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1945 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001946 case SEGV_MAPERR:
1947 reason = ProcessMessage::eInvalidAddress;
1948 break;
1949 case SEGV_ACCERR:
1950 reason = ProcessMessage::ePrivilegedAddress;
1951 break;
1952 }
Greg Clayton542e4072012-09-07 17:49:29 +00001953
Stephen Wilson84ffe702011-03-30 15:55:52 +00001954 return reason;
1955}
1956
1957ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001958ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001959{
1960 ProcessMessage::CrashReason reason;
1961 assert(info->si_signo == SIGILL);
1962
1963 reason = ProcessMessage::eInvalidCrashReason;
1964
1965 switch (info->si_code)
1966 {
1967 default:
1968 assert(false && "unexpected si_code for SIGILL");
1969 break;
1970 case ILL_ILLOPC:
1971 reason = ProcessMessage::eIllegalOpcode;
1972 break;
1973 case ILL_ILLOPN:
1974 reason = ProcessMessage::eIllegalOperand;
1975 break;
1976 case ILL_ILLADR:
1977 reason = ProcessMessage::eIllegalAddressingMode;
1978 break;
1979 case ILL_ILLTRP:
1980 reason = ProcessMessage::eIllegalTrap;
1981 break;
1982 case ILL_PRVOPC:
1983 reason = ProcessMessage::ePrivilegedOpcode;
1984 break;
1985 case ILL_PRVREG:
1986 reason = ProcessMessage::ePrivilegedRegister;
1987 break;
1988 case ILL_COPROC:
1989 reason = ProcessMessage::eCoprocessorError;
1990 break;
1991 case ILL_BADSTK:
1992 reason = ProcessMessage::eInternalStackError;
1993 break;
1994 }
1995
1996 return reason;
1997}
1998
1999ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002000ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002001{
2002 ProcessMessage::CrashReason reason;
2003 assert(info->si_signo == SIGFPE);
2004
2005 reason = ProcessMessage::eInvalidCrashReason;
2006
2007 switch (info->si_code)
2008 {
2009 default:
2010 assert(false && "unexpected si_code for SIGFPE");
2011 break;
2012 case FPE_INTDIV:
2013 reason = ProcessMessage::eIntegerDivideByZero;
2014 break;
2015 case FPE_INTOVF:
2016 reason = ProcessMessage::eIntegerOverflow;
2017 break;
2018 case FPE_FLTDIV:
2019 reason = ProcessMessage::eFloatDivideByZero;
2020 break;
2021 case FPE_FLTOVF:
2022 reason = ProcessMessage::eFloatOverflow;
2023 break;
2024 case FPE_FLTUND:
2025 reason = ProcessMessage::eFloatUnderflow;
2026 break;
2027 case FPE_FLTRES:
2028 reason = ProcessMessage::eFloatInexactResult;
2029 break;
2030 case FPE_FLTINV:
2031 reason = ProcessMessage::eFloatInvalidOperation;
2032 break;
2033 case FPE_FLTSUB:
2034 reason = ProcessMessage::eFloatSubscriptRange;
2035 break;
2036 }
2037
2038 return reason;
2039}
2040
2041ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002042ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002043{
2044 ProcessMessage::CrashReason reason;
2045 assert(info->si_signo == SIGBUS);
2046
2047 reason = ProcessMessage::eInvalidCrashReason;
2048
2049 switch (info->si_code)
2050 {
2051 default:
2052 assert(false && "unexpected si_code for SIGBUS");
2053 break;
2054 case BUS_ADRALN:
2055 reason = ProcessMessage::eIllegalAlignment;
2056 break;
2057 case BUS_ADRERR:
2058 reason = ProcessMessage::eIllegalAddress;
2059 break;
2060 case BUS_OBJERR:
2061 reason = ProcessMessage::eHardwareError;
2062 break;
2063 }
2064
2065 return reason;
2066}
2067
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002068void
Johnny Chen25e68e32011-06-14 19:19:50 +00002069ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002070{
Stephen Wilson570243b2011-01-19 01:37:06 +00002071 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002072
Stephen Wilson570243b2011-01-19 01:37:06 +00002073 // We are finised with the arguments and are ready to go. Sync with the
2074 // parent thread and start serving operations on the inferior.
2075 sem_post(&args->m_semaphore);
2076
Michael Sartain704bf892013-10-09 01:28:57 +00002077 for(;;)
2078 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002079 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002080 if (sem_wait(&monitor->m_operation_pending))
2081 {
2082 if (errno == EINTR)
2083 continue;
2084 assert(false && "Unexpected errno from sem_wait");
2085 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002086
Daniel Malea1efb4182013-09-16 23:12:18 +00002087 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002088
Daniel Malea1efb4182013-09-16 23:12:18 +00002089 // notify calling thread that operation is complete
2090 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002091 }
2092}
2093
2094void
2095ProcessMonitor::DoOperation(Operation *op)
2096{
Daniel Malea1efb4182013-09-16 23:12:18 +00002097 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002098
Daniel Malea1efb4182013-09-16 23:12:18 +00002099 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002100
Daniel Malea1efb4182013-09-16 23:12:18 +00002101 // notify operation thread that an operation is ready to be processed
2102 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002103
Daniel Malea1efb4182013-09-16 23:12:18 +00002104 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002105 while (sem_wait(&m_operation_done))
2106 {
2107 if (errno == EINTR)
2108 continue;
2109 assert(false && "Unexpected errno from sem_wait");
2110 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002111}
2112
2113size_t
2114ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2115 Error &error)
2116{
2117 size_t result;
2118 ReadOperation op(vm_addr, buf, size, error, result);
2119 DoOperation(&op);
2120 return result;
2121}
2122
2123size_t
2124ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2125 lldb_private::Error &error)
2126{
2127 size_t result;
2128 WriteOperation op(vm_addr, buf, size, error, result);
2129 DoOperation(&op);
2130 return result;
2131}
2132
2133bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002134ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002135 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002136{
2137 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002138 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002139 DoOperation(&op);
2140 return result;
2141}
2142
2143bool
Matt Kopec7de48462013-03-06 17:20:48 +00002144ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002145 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002146{
2147 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002148 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002149 DoOperation(&op);
2150 return result;
2151}
2152
2153bool
Matt Kopec7de48462013-03-06 17:20:48 +00002154ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002155{
2156 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002157 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002158 DoOperation(&op);
2159 return result;
2160}
2161
2162bool
Matt Kopec7de48462013-03-06 17:20:48 +00002163ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002164{
2165 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002166 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002167 DoOperation(&op);
2168 return result;
2169}
2170
2171bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002172ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2173{
2174 bool result;
2175 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2176 DoOperation(&op);
2177 return result;
2178}
2179
2180bool
Matt Kopec7de48462013-03-06 17:20:48 +00002181ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002182{
2183 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002184 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002185 DoOperation(&op);
2186 return result;
2187}
2188
2189bool
Matt Kopec7de48462013-03-06 17:20:48 +00002190ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002191{
2192 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002193 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002194 DoOperation(&op);
2195 return result;
2196}
2197
2198bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002199ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2200{
2201 bool result;
2202 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2203 DoOperation(&op);
2204 return result;
2205}
2206
2207bool
Richard Mitton0a558352013-10-17 21:14:00 +00002208ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2209{
2210 bool result;
2211 ReadThreadPointerOperation op(tid, &value, result);
2212 DoOperation(&op);
2213 return result;
2214}
2215
2216bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002217ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002218{
2219 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002220 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2221
2222 if (log)
2223 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2224 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002225 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002226 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002227 if (log)
2228 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002229 return result;
2230}
2231
2232bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002233ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002234{
2235 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002236 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002237 DoOperation(&op);
2238 return result;
2239}
2240
2241bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002242ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002243{
Ed Maste4e0999b2014-04-01 18:14:06 +00002244 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002245}
2246
2247bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002248ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002249{
2250 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002251 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002252 DoOperation(&op);
2253 return result;
2254}
2255
2256bool
2257ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2258{
2259 bool result;
2260 EventMessageOperation op(tid, message, result);
2261 DoOperation(&op);
2262 return result;
2263}
2264
Greg Clayton743ecf42012-10-16 20:20:18 +00002265lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002266ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002267{
Greg Clayton28041352011-11-29 20:50:10 +00002268 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002269 if (tid != LLDB_INVALID_THREAD_ID)
2270 {
2271 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002272 DoOperation(&op);
2273 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002274 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002275}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002276
2277bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002278ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2279{
Peter Collingbourne62343202011-06-14 03:55:54 +00002280 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002281
2282 if (target_fd == -1)
2283 return false;
2284
Peter Collingbourne62343202011-06-14 03:55:54 +00002285 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002286}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002287
2288void
2289ProcessMonitor::StopMonitoringChildProcess()
2290{
2291 lldb::thread_result_t thread_result;
2292
Stephen Wilsond4182f42011-02-09 20:10:35 +00002293 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002294 {
2295 Host::ThreadCancel(m_monitor_thread, NULL);
2296 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2297 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2298 }
2299}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002300
2301void
2302ProcessMonitor::StopMonitor()
2303{
2304 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002305 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002306 sem_destroy(&m_operation_pending);
2307 sem_destroy(&m_operation_done);
2308
Andrew Kaylor5e268992013-09-14 00:17:31 +00002309 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2310 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2311 // the descriptor to a ConnectionFileDescriptor object. Consequently
2312 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002313}
2314
2315void
Greg Clayton743ecf42012-10-16 20:20:18 +00002316ProcessMonitor::StopOpThread()
2317{
2318 lldb::thread_result_t result;
2319
2320 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2321 return;
2322
2323 Host::ThreadCancel(m_operation_thread, NULL);
2324 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002325 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002326}