blob: 416d6a65da5aaed0cc426f12cf3a6569718bfe32 [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>
Todd Fiala42079682014-08-27 16:05:26 +000018#include <elf.h>
Todd Fiala0bce1b62014-08-17 00:10:50 +000019#include <sys/personality.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000020#include <sys/ptrace.h>
Todd Fiala6ac1be42014-08-21 16:34:03 +000021#include <sys/uio.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000022#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000023#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000024#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000025#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000026#include <sys/wait.h>
27
28// C++ Includes
29// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000030#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000031#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000032#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033#include "lldb/Core/Scalar.h"
34#include "lldb/Host/Host.h"
35#include "lldb/Target/Thread.h"
36#include "lldb/Target/RegisterContext.h"
37#include "lldb/Utility/PseudoTerminal.h"
38
Johnny Chen30213ff2012-01-05 19:17:38 +000039#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000040#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000041#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000042#include "ProcessMonitor.h"
43
Greg Clayton386ff182011-11-05 01:09:16 +000044#define DEBUG_PTRACE_MAXBYTES 20
45
Matt Kopec58c0b962013-03-20 20:34:35 +000046// Support ptrace extensions even when compiled without required kernel support
47#ifndef PTRACE_GETREGSET
48 #define PTRACE_GETREGSET 0x4204
49#endif
50#ifndef PTRACE_SETREGSET
51 #define PTRACE_SETREGSET 0x4205
52#endif
Richard Mitton0a558352013-10-17 21:14:00 +000053#ifndef PTRACE_GET_THREAD_AREA
54 #define PTRACE_GET_THREAD_AREA 25
55#endif
56#ifndef PTRACE_ARCH_PRCTL
57 #define PTRACE_ARCH_PRCTL 30
58#endif
59#ifndef ARCH_GET_FS
60 #define ARCH_SET_GS 0x1001
61 #define ARCH_SET_FS 0x1002
62 #define ARCH_GET_FS 0x1003
63 #define ARCH_GET_GS 0x1004
64#endif
65
Todd Fiala0bce1b62014-08-17 00:10:50 +000066#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000067
Matt Kopece9ea0da2013-05-07 19:29:28 +000068// Support hardware breakpoints in case it has not been defined
69#ifndef TRAP_HWBKPT
70 #define TRAP_HWBKPT 4
71#endif
72
Andrew Kaylor93132f52013-05-28 23:04:25 +000073// Try to define a macro to encapsulate the tgkill syscall
74// fall back on kill() if tgkill isn't available
75#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
76
Stephen Wilsone6f9f662010-07-24 02:19:04 +000077using namespace lldb_private;
78
Johnny Chen0d5f2d42011-10-18 18:09:30 +000079// FIXME: this code is host-dependent with respect to types and
80// endianness and needs to be fixed. For example, lldb::addr_t is
81// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
82// 32-bit pointer arguments. This code uses casts to work around the
83// problem.
84
85// We disable the tracing of ptrace calls for integration builds to
86// avoid the additional indirection and checks.
87#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
88
Greg Clayton386ff182011-11-05 01:09:16 +000089static void
90DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
91{
92 uint8_t *ptr = (uint8_t *)bytes;
93 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
94 for(uint32_t i=0; i<loop_count; i++)
95 {
96 s.Printf ("[%x]", *ptr);
97 ptr++;
98 }
99}
100
Matt Kopec58c0b962013-03-20 20:34:35 +0000101static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000102{
103 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000104 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000105 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000106
107 if (verbose_log)
108 {
109 switch(req)
110 {
111 case PTRACE_POKETEXT:
112 {
113 DisplayBytes(buf, &data, 8);
114 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
115 break;
116 }
Greg Clayton542e4072012-09-07 17:49:29 +0000117 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000118 {
119 DisplayBytes(buf, &data, 8);
120 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
121 break;
122 }
Greg Clayton542e4072012-09-07 17:49:29 +0000123 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000124 {
125 DisplayBytes(buf, &data, 8);
126 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
127 break;
128 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000129#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000130 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000131 {
Matt Kopec7de48462013-03-06 17:20:48 +0000132 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000133 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
134 break;
135 }
136 case PTRACE_SETFPREGS:
137 {
Matt Kopec7de48462013-03-06 17:20:48 +0000138 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000139 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
140 break;
141 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000142#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000143 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000144 {
145 DisplayBytes(buf, data, sizeof(siginfo_t));
146 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
147 break;
148 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000149 case PTRACE_SETREGSET:
150 {
151 // Extract iov_base from data, which is a pointer to the struct IOVEC
152 DisplayBytes(buf, *(void **)data, data_size);
153 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
154 break;
155 }
Greg Clayton386ff182011-11-05 01:09:16 +0000156 default:
157 {
158 }
159 }
160 }
161}
162
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000163// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000164// 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 +0000165extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000166PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000167 const char* reqName, const char* file, int line)
168{
Greg Clayton386ff182011-11-05 01:09:16 +0000169 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000170
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000171 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000172
Matt Kopec7de48462013-03-06 17:20:48 +0000173 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000174
175 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000176 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000177 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000178 else
Todd Fiala4507f062014-02-27 20:46:12 +0000179 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000180
Ed Mastec099c952014-02-24 14:07:45 +0000181 if (log)
182 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
183 reqName, pid, addr, data, data_size, result, file, line);
184
Matt Kopec7de48462013-03-06 17:20:48 +0000185 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000186
Matt Kopec7de48462013-03-06 17:20:48 +0000187 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000188 {
189 const char* str;
190 switch (errno)
191 {
192 case ESRCH: str = "ESRCH"; break;
193 case EINVAL: str = "EINVAL"; break;
194 case EBUSY: str = "EBUSY"; break;
195 case EPERM: str = "EPERM"; break;
196 default: str = "<unknown>";
197 }
198 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
199 }
200
201 return result;
202}
203
Matt Kopec7de48462013-03-06 17:20:48 +0000204// Wrapper for ptrace when logging is not required.
205// Sets errno to 0 prior to calling ptrace.
206extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000207PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000208{
Matt Kopec58c0b962013-03-20 20:34:35 +0000209 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000210 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000211 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
212 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
213 else
214 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000215 return result;
216}
217
218#define PTRACE(req, pid, addr, data, data_size) \
219 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000220#else
Matt Kopec7de48462013-03-06 17:20:48 +0000221 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000222#endif
223
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224//------------------------------------------------------------------------------
225// Static implementations of ProcessMonitor::ReadMemory and
226// ProcessMonitor::WriteMemory. This enables mutual recursion between these
227// functions without needed to go thru the thread funnel.
228
229static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000230DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
232{
Greg Clayton542e4072012-09-07 17:49:29 +0000233 // ptrace word size is determined by the host, not the child
234 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235 unsigned char *dst = static_cast<unsigned char*>(buf);
236 size_t bytes_read;
237 size_t remainder;
238 long data;
239
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000240 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000241 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000242 ProcessPOSIXLog::IncNestLevel();
243 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000244 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000245 pid, word_size, (void*)vm_addr, buf, size);
246
247 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
249 {
250 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000251 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
252 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000253 {
254 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000255 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000256 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 return bytes_read;
258 }
259
260 remainder = size - bytes_read;
261 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000262
263 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000264 for (unsigned i = 0; i < remainder; ++i)
265 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000266
Johnny Chen30213ff2012-01-05 19:17:38 +0000267 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
268 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
269 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
270 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000271 {
272 uintptr_t print_dst = 0;
273 // Format bytes from data by moving into print_dst for log output
274 for (unsigned i = 0; i < remainder; ++i)
275 print_dst |= (((data >> i*8) & 0xFF) << i*8);
276 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
277 (void*)vm_addr, print_dst, (unsigned long)data);
278 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000279
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 vm_addr += word_size;
281 dst += word_size;
282 }
283
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000284 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000285 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286 return bytes_read;
287}
288
289static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000290DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000291 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
292{
Greg Clayton542e4072012-09-07 17:49:29 +0000293 // ptrace word size is determined by the host, not the child
294 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000295 const unsigned char *src = static_cast<const unsigned char*>(buf);
296 size_t bytes_written = 0;
297 size_t remainder;
298
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000299 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000300 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000301 ProcessPOSIXLog::IncNestLevel();
302 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000303 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 pid, word_size, (void*)vm_addr, buf, size);
305
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000306 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
307 {
308 remainder = size - bytes_written;
309 remainder = remainder > word_size ? word_size : remainder;
310
311 if (remainder == word_size)
312 {
313 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000314 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000315 for (unsigned i = 0; i < word_size; ++i)
316 data |= (unsigned long)src[i] << i*8;
317
Johnny Chen30213ff2012-01-05 19:17:38 +0000318 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
319 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
320 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
321 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
323 (void*)vm_addr, *(unsigned long*)src, data);
324
Matt Kopec7de48462013-03-06 17:20:48 +0000325 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000326 {
327 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000328 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000329 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000330 return bytes_written;
331 }
332 }
333 else
334 {
335 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000336 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000337 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000338 {
339 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000340 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000343
344 memcpy(buff, src, remainder);
345
Greg Clayton542e4072012-09-07 17:49:29 +0000346 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000347 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000348 {
349 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000350 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000351 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 }
353
Johnny Chen30213ff2012-01-05 19:17:38 +0000354 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
355 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
356 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
357 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000358 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
359 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000360 }
361
362 vm_addr += word_size;
363 src += word_size;
364 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000365 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000366 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367 return bytes_written;
368}
369
Stephen Wilson26977162011-03-23 02:14:42 +0000370// Simple helper function to ensure flags are enabled on the given file
371// descriptor.
372static bool
373EnsureFDFlags(int fd, int flags, Error &error)
374{
375 int status;
376
377 if ((status = fcntl(fd, F_GETFL)) == -1)
378 {
379 error.SetErrorToErrno();
380 return false;
381 }
382
383 if (fcntl(fd, F_SETFL, status | flags) == -1)
384 {
385 error.SetErrorToErrno();
386 return false;
387 }
388
389 return true;
390}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000391
392//------------------------------------------------------------------------------
393/// @class Operation
394/// @brief Represents a ProcessMonitor operation.
395///
396/// Under Linux, it is not possible to ptrace() from any other thread but the
397/// one that spawned or attached to the process from the start. Therefore, when
398/// a ProcessMonitor is asked to deliver or change the state of an inferior
399/// process the operation must be "funneled" to a specific thread to perform the
400/// task. The Operation class provides an abstract base for all services the
401/// ProcessMonitor must perform via the single virtual function Execute, thus
402/// encapsulating the code that needs to run in the privileged context.
403class Operation
404{
405public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000406 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000407 virtual void Execute(ProcessMonitor *monitor) = 0;
408};
409
410//------------------------------------------------------------------------------
411/// @class ReadOperation
412/// @brief Implements ProcessMonitor::ReadMemory.
413class ReadOperation : public Operation
414{
415public:
416 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
417 Error &error, size_t &result)
418 : m_addr(addr), m_buff(buff), m_size(size),
419 m_error(error), m_result(result)
420 { }
421
422 void Execute(ProcessMonitor *monitor);
423
424private:
425 lldb::addr_t m_addr;
426 void *m_buff;
427 size_t m_size;
428 Error &m_error;
429 size_t &m_result;
430};
431
432void
433ReadOperation::Execute(ProcessMonitor *monitor)
434{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000435 lldb::pid_t pid = monitor->GetPID();
436
Greg Clayton542e4072012-09-07 17:49:29 +0000437 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000438}
439
440//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000441/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000442/// @brief Implements ProcessMonitor::WriteMemory.
443class WriteOperation : public Operation
444{
445public:
446 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
447 Error &error, size_t &result)
448 : m_addr(addr), m_buff(buff), m_size(size),
449 m_error(error), m_result(result)
450 { }
451
452 void Execute(ProcessMonitor *monitor);
453
454private:
455 lldb::addr_t m_addr;
456 const void *m_buff;
457 size_t m_size;
458 Error &m_error;
459 size_t &m_result;
460};
461
462void
463WriteOperation::Execute(ProcessMonitor *monitor)
464{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465 lldb::pid_t pid = monitor->GetPID();
466
Greg Clayton542e4072012-09-07 17:49:29 +0000467 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000468}
469
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000470
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000471//------------------------------------------------------------------------------
472/// @class ReadRegOperation
473/// @brief Implements ProcessMonitor::ReadRegisterValue.
474class ReadRegOperation : public Operation
475{
476public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000477 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000478 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000479 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000481 { }
482
483 void Execute(ProcessMonitor *monitor);
484
485private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000486 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000487 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000488 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000489 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000490 bool &m_result;
491};
492
493void
494ReadRegOperation::Execute(ProcessMonitor *monitor)
495{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000496 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000497
498 // Set errno to zero so that we can detect a failed peek.
499 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000500 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
501 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000502 m_result = false;
503 else
504 {
505 m_value = data;
506 m_result = true;
507 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000508 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000509 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000510 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000511}
512
513//------------------------------------------------------------------------------
514/// @class WriteRegOperation
515/// @brief Implements ProcessMonitor::WriteRegisterValue.
516class WriteRegOperation : public Operation
517{
518public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000519 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000520 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000521 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000523 { }
524
525 void Execute(ProcessMonitor *monitor);
526
527private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000528 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000529 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000530 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000531 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000532 bool &m_result;
533};
534
535void
536WriteRegOperation::Execute(ProcessMonitor *monitor)
537{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000538 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000539 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000540
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000541 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000542
543 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000544 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000545 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000546 m_result = false;
547 else
548 m_result = true;
549}
550
551//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000552/// @class ReadGPROperation
553/// @brief Implements ProcessMonitor::ReadGPR.
554class ReadGPROperation : public Operation
555{
556public:
Matt Kopec7de48462013-03-06 17:20:48 +0000557 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
558 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000559 { }
560
561 void Execute(ProcessMonitor *monitor);
562
563private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000564 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000565 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000566 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000567 bool &m_result;
568};
569
570void
571ReadGPROperation::Execute(ProcessMonitor *monitor)
572{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000573#if defined (__arm64__) || defined (__aarch64__)
574 int regset = NT_PRSTATUS;
575 struct iovec ioVec;
576
577 ioVec.iov_base = m_buf;
578 ioVec.iov_len = m_buf_size;
579 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000580 m_result = false;
581 else
582 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000583#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000584 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
585 m_result = false;
586 else
587 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000588#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000589}
590
591//------------------------------------------------------------------------------
592/// @class ReadFPROperation
593/// @brief Implements ProcessMonitor::ReadFPR.
594class ReadFPROperation : public Operation
595{
596public:
Matt Kopec7de48462013-03-06 17:20:48 +0000597 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
598 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000599 { }
600
601 void Execute(ProcessMonitor *monitor);
602
603private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000604 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000605 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000606 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000607 bool &m_result;
608};
609
610void
611ReadFPROperation::Execute(ProcessMonitor *monitor)
612{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000613#if defined (__arm64__) || defined (__aarch64__)
614 int regset = NT_FPREGSET;
615 struct iovec ioVec;
616
617 ioVec.iov_base = m_buf;
618 ioVec.iov_len = m_buf_size;
619 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000620 m_result = false;
621 else
622 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000623#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000624 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
625 m_result = false;
626 else
627 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000628#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000629}
630
631//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000632/// @class ReadRegisterSetOperation
633/// @brief Implements ProcessMonitor::ReadRegisterSet.
634class ReadRegisterSetOperation : public Operation
635{
636public:
637 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
638 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
639 { }
640
641 void Execute(ProcessMonitor *monitor);
642
643private:
644 lldb::tid_t m_tid;
645 void *m_buf;
646 size_t m_buf_size;
647 const unsigned int m_regset;
648 bool &m_result;
649};
650
651void
652ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
653{
654 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
655 m_result = false;
656 else
657 m_result = true;
658}
659
660//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000661/// @class WriteGPROperation
662/// @brief Implements ProcessMonitor::WriteGPR.
663class WriteGPROperation : public Operation
664{
665public:
Matt Kopec7de48462013-03-06 17:20:48 +0000666 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
667 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000668 { }
669
670 void Execute(ProcessMonitor *monitor);
671
672private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000673 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000674 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000675 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000676 bool &m_result;
677};
678
679void
680WriteGPROperation::Execute(ProcessMonitor *monitor)
681{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000682#if defined (__arm64__) || defined (__aarch64__)
683 int regset = NT_PRSTATUS;
684 struct iovec ioVec;
685
686 ioVec.iov_base = m_buf;
687 ioVec.iov_len = m_buf_size;
688 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000689 m_result = false;
690 else
691 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000692#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000693 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
694 m_result = false;
695 else
696 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000697#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000698}
699
700//------------------------------------------------------------------------------
701/// @class WriteFPROperation
702/// @brief Implements ProcessMonitor::WriteFPR.
703class WriteFPROperation : public Operation
704{
705public:
Matt Kopec7de48462013-03-06 17:20:48 +0000706 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
707 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000708 { }
709
710 void Execute(ProcessMonitor *monitor);
711
712private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000713 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000714 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000715 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000716 bool &m_result;
717};
718
719void
720WriteFPROperation::Execute(ProcessMonitor *monitor)
721{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000722#if defined (__arm64__) || defined (__aarch64__)
723 int regset = NT_FPREGSET;
724 struct iovec ioVec;
725
726 ioVec.iov_base = m_buf;
727 ioVec.iov_len = m_buf_size;
728 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000729 m_result = false;
730 else
731 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000732#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000733 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
734 m_result = false;
735 else
736 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000737#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000738}
739
740//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000741/// @class WriteRegisterSetOperation
742/// @brief Implements ProcessMonitor::WriteRegisterSet.
743class WriteRegisterSetOperation : public Operation
744{
745public:
746 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
747 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
748 { }
749
750 void Execute(ProcessMonitor *monitor);
751
752private:
753 lldb::tid_t m_tid;
754 void *m_buf;
755 size_t m_buf_size;
756 const unsigned int m_regset;
757 bool &m_result;
758};
759
760void
761WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
762{
763 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
764 m_result = false;
765 else
766 m_result = true;
767}
768
769//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000770/// @class ReadThreadPointerOperation
771/// @brief Implements ProcessMonitor::ReadThreadPointer.
772class ReadThreadPointerOperation : public Operation
773{
774public:
775 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
776 : m_tid(tid), m_addr(addr), m_result(result)
777 { }
778
779 void Execute(ProcessMonitor *monitor);
780
781private:
782 lldb::tid_t m_tid;
783 lldb::addr_t *m_addr;
784 bool &m_result;
785};
786
787void
788ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
789{
790 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
791 if (log)
792 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
793
794 // The process for getting the thread area on Linux is
795 // somewhat... obscure. There's several different ways depending on
796 // what arch you're on, and what kernel version you have.
797
798 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
799 switch(arch.GetMachine())
800 {
Todd Fiala42079682014-08-27 16:05:26 +0000801 case llvm::Triple::aarch64:
802 {
803 int regset = NT_ARM_TLS;
804 struct iovec ioVec;
805
806 ioVec.iov_base = m_addr;
807 ioVec.iov_len = sizeof(lldb::addr_t);
808 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
809 m_result = false;
810 else
811 m_result = true;
812 break;
813 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000814#if defined(__i386__) || defined(__x86_64__)
815 // Note that struct user below has a field named i387 which is x86-specific.
816 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000817 case llvm::Triple::x86:
818 {
819 // Find the GS register location for our host architecture.
820 size_t gs_user_offset = offsetof(struct user, regs);
821#ifdef __x86_64__
822 gs_user_offset += offsetof(struct user_regs_struct, gs);
823#endif
824#ifdef __i386__
825 gs_user_offset += offsetof(struct user_regs_struct, xgs);
826#endif
827
828 // Read the GS register value to get the selector.
829 errno = 0;
830 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
831 if (errno)
832 {
833 m_result = false;
834 break;
835 }
836
837 // Read the LDT base for that selector.
838 uint32_t tmp[4];
839 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
840 *m_addr = tmp[1];
841 break;
842 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000843#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000844 case llvm::Triple::x86_64:
845 // Read the FS register base.
846 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
847 break;
848 default:
849 m_result = false;
850 break;
851 }
852}
853
854//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000855/// @class ResumeOperation
856/// @brief Implements ProcessMonitor::Resume.
857class ResumeOperation : public Operation
858{
859public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000860 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
861 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000862
863 void Execute(ProcessMonitor *monitor);
864
865private:
866 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000867 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000868 bool &m_result;
869};
870
871void
872ResumeOperation::Execute(ProcessMonitor *monitor)
873{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000874 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000875
876 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
877 data = m_signo;
878
Matt Kopec7de48462013-03-06 17:20:48 +0000879 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000880 {
881 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
882
883 if (log)
884 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000885 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000886 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000887 else
888 m_result = true;
889}
890
891//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000892/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000893/// @brief Implements ProcessMonitor::SingleStep.
894class SingleStepOperation : public Operation
895{
896public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000897 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
898 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000899
900 void Execute(ProcessMonitor *monitor);
901
902private:
903 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000904 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000905 bool &m_result;
906};
907
908void
909SingleStepOperation::Execute(ProcessMonitor *monitor)
910{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000911 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000912
913 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
914 data = m_signo;
915
Matt Kopec7de48462013-03-06 17:20:48 +0000916 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000917 m_result = false;
918 else
919 m_result = true;
920}
921
922//------------------------------------------------------------------------------
923/// @class SiginfoOperation
924/// @brief Implements ProcessMonitor::GetSignalInfo.
925class SiginfoOperation : public Operation
926{
927public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000928 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
929 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000930
931 void Execute(ProcessMonitor *monitor);
932
933private:
934 lldb::tid_t m_tid;
935 void *m_info;
936 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000937 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000938};
939
940void
941SiginfoOperation::Execute(ProcessMonitor *monitor)
942{
Matt Kopec7de48462013-03-06 17:20:48 +0000943 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000944 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000945 m_err = errno;
946 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000947 else
948 m_result = true;
949}
950
951//------------------------------------------------------------------------------
952/// @class EventMessageOperation
953/// @brief Implements ProcessMonitor::GetEventMessage.
954class EventMessageOperation : public Operation
955{
956public:
957 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
958 : m_tid(tid), m_message(message), m_result(result) { }
959
960 void Execute(ProcessMonitor *monitor);
961
962private:
963 lldb::tid_t m_tid;
964 unsigned long *m_message;
965 bool &m_result;
966};
967
968void
969EventMessageOperation::Execute(ProcessMonitor *monitor)
970{
Matt Kopec7de48462013-03-06 17:20:48 +0000971 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000972 m_result = false;
973 else
974 m_result = true;
975}
976
977//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000978/// @class DetachOperation
979/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000980class DetachOperation : public Operation
981{
982public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000983 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000984
985 void Execute(ProcessMonitor *monitor);
986
987private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000988 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000989 Error &m_error;
990};
991
992void
993DetachOperation::Execute(ProcessMonitor *monitor)
994{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000995 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000996 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000997}
998
Johnny Chen25e68e32011-06-14 19:19:50 +0000999ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1000 : m_monitor(monitor)
1001{
1002 sem_init(&m_semaphore, 0, 0);
1003}
1004
1005ProcessMonitor::OperationArgs::~OperationArgs()
1006{
1007 sem_destroy(&m_semaphore);
1008}
1009
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001010ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1011 lldb_private::Module *module,
1012 char const **argv,
1013 char const **envp,
1014 const char *stdin_path,
1015 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001016 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001017 const char *working_dir,
1018 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001019 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001020 m_module(module),
1021 m_argv(argv),
1022 m_envp(envp),
1023 m_stdin_path(stdin_path),
1024 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001025 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001026 m_working_dir(working_dir),
1027 m_launch_info(launch_info)
1028{
1029}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001030
1031ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001032{ }
1033
1034ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1035 lldb::pid_t pid)
1036 : OperationArgs(monitor), m_pid(pid) { }
1037
1038ProcessMonitor::AttachArgs::~AttachArgs()
1039{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001040
1041//------------------------------------------------------------------------------
1042/// The basic design of the ProcessMonitor is built around two threads.
1043///
1044/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1045/// for changes in the debugee state. When a change is detected a
1046/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1047/// "drives" state changes in the debugger.
1048///
1049/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001050/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051/// operations such as register reads/writes, stepping, etc. See the comments
1052/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001053ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 Module *module,
1055 const char *argv[],
1056 const char *envp[],
1057 const char *stdin_path,
1058 const char *stdout_path,
1059 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001060 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001061 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001062 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001063 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001064 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001065 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001066 m_pid(LLDB_INVALID_PROCESS_ID),
1067 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001068 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001069{
Daniel Malea1efb4182013-09-16 23:12:18 +00001070 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1071 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001072 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001073
Daniel Malea1efb4182013-09-16 23:12:18 +00001074 sem_init(&m_operation_pending, 0, 0);
1075 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001076
Johnny Chen25e68e32011-06-14 19:19:50 +00001077 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001078 if (!error.Success())
1079 return;
1080
1081WAIT_AGAIN:
1082 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001083 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001084 {
1085 if (errno == EINTR)
1086 goto WAIT_AGAIN;
1087 else
1088 {
1089 error.SetErrorToErrno();
1090 return;
1091 }
1092 }
1093
1094 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001095 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001096 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001097 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001098 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001099 return;
1100 }
1101
1102 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001103 m_monitor_thread = Host::StartMonitoringChildProcess(
1104 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001105 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001106 {
1107 error.SetErrorToGenericError();
1108 error.SetErrorString("Process launch failed.");
1109 return;
1110 }
1111}
1112
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001113ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001114 lldb::pid_t pid,
1115 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001116 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001117 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001118 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001119 m_pid(LLDB_INVALID_PROCESS_ID),
1120 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001121 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001122{
Daniel Malea1efb4182013-09-16 23:12:18 +00001123 sem_init(&m_operation_pending, 0, 0);
1124 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001125
Daniel Malea1efb4182013-09-16 23:12:18 +00001126 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001127
1128 StartAttachOpThread(args.get(), error);
1129 if (!error.Success())
1130 return;
1131
1132WAIT_AGAIN:
1133 // Wait for the operation thread to initialize.
1134 if (sem_wait(&args->m_semaphore))
1135 {
1136 if (errno == EINTR)
1137 goto WAIT_AGAIN;
1138 else
1139 {
1140 error.SetErrorToErrno();
1141 return;
1142 }
1143 }
1144
Greg Clayton743ecf42012-10-16 20:20:18 +00001145 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001146 if (!args->m_error.Success())
1147 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001148 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001149 error = args->m_error;
1150 return;
1151 }
1152
1153 // Finally, start monitoring the child process for change in state.
1154 m_monitor_thread = Host::StartMonitoringChildProcess(
1155 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1156 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1157 {
1158 error.SetErrorToGenericError();
1159 error.SetErrorString("Process attach failed.");
1160 return;
1161 }
1162}
1163
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001164ProcessMonitor::~ProcessMonitor()
1165{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001166 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167}
1168
1169//------------------------------------------------------------------------------
1170// Thread setup and tear down.
1171void
Johnny Chen25e68e32011-06-14 19:19:50 +00001172ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001173{
1174 static const char *g_thread_name = "lldb.process.linux.operation";
1175
Stephen Wilsond4182f42011-02-09 20:10:35 +00001176 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001177 return;
1178
1179 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001180 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001181}
1182
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001183void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001184ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001185{
1186 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1187
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001188 if (!Launch(args)) {
1189 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001190 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001191 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192
Stephen Wilson570243b2011-01-19 01:37:06 +00001193 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001194 return NULL;
1195}
1196
1197bool
1198ProcessMonitor::Launch(LaunchArgs *args)
1199{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001200 assert (args && "null args");
1201 if (!args)
1202 return false;
1203
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001204 ProcessMonitor *monitor = args->m_monitor;
1205 ProcessLinux &process = monitor->GetProcess();
1206 const char **argv = args->m_argv;
1207 const char **envp = args->m_envp;
1208 const char *stdin_path = args->m_stdin_path;
1209 const char *stdout_path = args->m_stdout_path;
1210 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001211 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001212
1213 lldb_utility::PseudoTerminal terminal;
1214 const size_t err_len = 1024;
1215 char err_str[err_len];
1216 lldb::pid_t pid;
1217
1218 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001219 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001220
Stephen Wilson57740ec2011-01-15 00:12:41 +00001221 // Propagate the environment if one is not supplied.
1222 if (envp == NULL || envp[0] == NULL)
1223 envp = const_cast<const char **>(environ);
1224
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001225 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001226 {
1227 args->m_error.SetErrorToGenericError();
1228 args->m_error.SetErrorString("Process fork failed.");
1229 goto FINISH;
1230 }
1231
Peter Collingbourne6a520222011-06-14 03:55:58 +00001232 // Recognized child exit status codes.
1233 enum {
1234 ePtraceFailed = 1,
1235 eDupStdinFailed,
1236 eDupStdoutFailed,
1237 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001238 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001239 eExecFailed,
1240 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001241 };
1242
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001243 // Child process.
1244 if (pid == 0)
1245 {
1246 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001247 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001248 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001249
1250 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001251 if (setgid(getgid()) != 0)
1252 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001253
1254 // Let us have our own process group.
1255 setpgid(0, 0);
1256
Greg Clayton710dd5a2011-01-08 20:28:42 +00001257 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001258 //
1259 // FIXME: If two or more of the paths are the same we needlessly open
1260 // the same file multiple times.
1261 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001262 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001263 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001264
1265 if (stdout_path != NULL && stdout_path[0])
1266 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001267 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001268
1269 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001270 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001271 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001272
Daniel Malea6217d2a2013-01-08 14:49:22 +00001273 // Change working directory
1274 if (working_dir != NULL && working_dir[0])
1275 if (0 != ::chdir(working_dir))
1276 exit(eChdirFailed);
1277
Todd Fiala0bce1b62014-08-17 00:10:50 +00001278 // Disable ASLR if requested.
1279 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1280 {
1281 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1282 if (old_personality == -1)
1283 {
1284 if (log)
1285 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1286 }
1287 else
1288 {
1289 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1290 if (new_personality == -1)
1291 {
1292 if (log)
1293 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1294
1295 }
1296 else
1297 {
1298 if (log)
1299 log->Printf ("ProcessMonitor::%s disbling ASLR: SUCCESS", __FUNCTION__);
1300
1301 }
1302 }
1303 }
1304
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001305 // Execute. We should never return.
1306 execve(argv[0],
1307 const_cast<char *const *>(argv),
1308 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001309 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001310 }
1311
1312 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001313 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001314 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001315 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001316
1317 raw_pid = waitpid(pid, &status, 0);
1318 wpid = static_cast <lldb::pid_t> (raw_pid);
1319 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001320 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001321 args->m_error.SetErrorToErrno();
1322 goto FINISH;
1323 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001324 else if (WIFEXITED(status))
1325 {
1326 // open, dup or execve likely failed for some reason.
1327 args->m_error.SetErrorToGenericError();
1328 switch (WEXITSTATUS(status))
1329 {
Greg Clayton542e4072012-09-07 17:49:29 +00001330 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001331 args->m_error.SetErrorString("Child ptrace failed.");
1332 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001333 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001334 args->m_error.SetErrorString("Child open stdin failed.");
1335 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001336 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001337 args->m_error.SetErrorString("Child open stdout failed.");
1338 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001339 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001340 args->m_error.SetErrorString("Child open stderr failed.");
1341 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001342 case eChdirFailed:
1343 args->m_error.SetErrorString("Child failed to set working directory.");
1344 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001345 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001346 args->m_error.SetErrorString("Child exec failed.");
1347 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001348 case eSetGidFailed:
1349 args->m_error.SetErrorString("Child setgid failed.");
1350 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001351 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001352 args->m_error.SetErrorString("Child returned unknown exit status.");
1353 break;
1354 }
1355 goto FINISH;
1356 }
1357 assert(WIFSTOPPED(status) && wpid == pid &&
1358 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001359
Matt Kopec085d6ce2013-05-31 22:00:07 +00001360 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001361 {
1362 args->m_error.SetErrorToErrno();
1363 goto FINISH;
1364 }
1365
1366 // Release the master terminal descriptor and pass it off to the
1367 // ProcessMonitor instance. Similarly stash the inferior pid.
1368 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1369 monitor->m_pid = pid;
1370
Stephen Wilson26977162011-03-23 02:14:42 +00001371 // Set the terminal fd to be in non blocking mode (it simplifies the
1372 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1373 // descriptor to read from).
1374 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1375 goto FINISH;
1376
Johnny Chen30213ff2012-01-05 19:17:38 +00001377 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001378 // FIXME: should we be letting UpdateThreadList handle this?
1379 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001380 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001381
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001382 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001383 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001384 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001385
Matt Kopecb2910442013-07-09 15:09:45 +00001386 process.AddThreadForInitialStopIfNeeded(pid);
1387
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001388 // Let our process instance know the thread has stopped.
1389 process.SendMessage(ProcessMessage::Trace(pid));
1390
1391FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001392 return args->m_error.Success();
1393}
1394
Johnny Chen25e68e32011-06-14 19:19:50 +00001395void
1396ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1397{
1398 static const char *g_thread_name = "lldb.process.linux.operation";
1399
1400 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1401 return;
1402
1403 m_operation_thread =
1404 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1405}
1406
Johnny Chen25e68e32011-06-14 19:19:50 +00001407void *
1408ProcessMonitor::AttachOpThread(void *arg)
1409{
1410 AttachArgs *args = static_cast<AttachArgs*>(arg);
1411
Greg Clayton743ecf42012-10-16 20:20:18 +00001412 if (!Attach(args)) {
1413 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001414 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001415 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001416
1417 ServeOperation(args);
1418 return NULL;
1419}
1420
1421bool
1422ProcessMonitor::Attach(AttachArgs *args)
1423{
1424 lldb::pid_t pid = args->m_pid;
1425
1426 ProcessMonitor *monitor = args->m_monitor;
1427 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001428 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001429 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001430
Matt Kopec085d6ce2013-05-31 22:00:07 +00001431 // Use a map to keep track of the threads which we have attached/need to attach.
1432 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001433 if (pid <= 1)
1434 {
1435 args->m_error.SetErrorToGenericError();
1436 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1437 goto FINISH;
1438 }
1439
Matt Kopec085d6ce2013-05-31 22:00:07 +00001440 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001441 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001442 for (Host::TidMap::iterator it = tids_to_attach.begin();
1443 it != tids_to_attach.end(); ++it)
1444 {
1445 if (it->second == false)
1446 {
1447 lldb::tid_t tid = it->first;
1448
1449 // Attach to the requested process.
1450 // An attach will cause the thread to stop with a SIGSTOP.
1451 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1452 {
1453 // No such thread. The thread may have exited.
1454 // More error handling may be needed.
1455 if (errno == ESRCH)
1456 {
1457 tids_to_attach.erase(it);
1458 continue;
1459 }
1460 else
1461 {
1462 args->m_error.SetErrorToErrno();
1463 goto FINISH;
1464 }
1465 }
1466
Todd Fiala9be50492014-07-01 16:30:53 +00001467 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001468 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1469 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001470 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001471 {
1472 // No such thread. The thread may have exited.
1473 // More error handling may be needed.
1474 if (errno == ESRCH)
1475 {
1476 tids_to_attach.erase(it);
1477 continue;
1478 }
1479 else
1480 {
1481 args->m_error.SetErrorToErrno();
1482 goto FINISH;
1483 }
1484 }
1485
1486 if (!SetDefaultPtraceOpts(tid))
1487 {
1488 args->m_error.SetErrorToErrno();
1489 goto FINISH;
1490 }
1491
1492 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001493 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001494
Matt Kopec085d6ce2013-05-31 22:00:07 +00001495 if (log)
1496 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1497 process.GetThreadList().AddThread(inferior);
1498 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001499 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001500 }
1501 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001502 }
1503
Matt Kopec085d6ce2013-05-31 22:00:07 +00001504 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001505 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001506 monitor->m_pid = pid;
1507 // Let our process instance know the thread has stopped.
1508 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001509 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001510 else
1511 {
1512 args->m_error.SetErrorToGenericError();
1513 args->m_error.SetErrorString("No such process.");
1514 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001515
1516 FINISH:
1517 return args->m_error.Success();
1518}
1519
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001520bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001521ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1522{
1523 long ptrace_opts = 0;
1524
1525 // Have the child raise an event on exit. This is used to keep the child in
1526 // limbo until it is destroyed.
1527 ptrace_opts |= PTRACE_O_TRACEEXIT;
1528
1529 // Have the tracer trace threads which spawn in the inferior process.
1530 // TODO: if we want to support tracing the inferiors' child, add the
1531 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1532 ptrace_opts |= PTRACE_O_TRACECLONE;
1533
1534 // Have the tracer notify us before execve returns
1535 // (needed to disable legacy SIGTRAP generation)
1536 ptrace_opts |= PTRACE_O_TRACEEXEC;
1537
1538 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1539}
1540
1541bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001542ProcessMonitor::MonitorCallback(void *callback_baton,
1543 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001544 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001545 int signal,
1546 int status)
1547{
1548 ProcessMessage message;
1549 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001550 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001551 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001552 bool stop_monitoring;
1553 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001554 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001555
Andrew Kaylor93132f52013-05-28 23:04:25 +00001556 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1557
1558 if (exited)
1559 {
1560 if (log)
1561 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1562 message = ProcessMessage::Exit(pid, status);
1563 process->SendMessage(message);
1564 return pid == process->GetID();
1565 }
1566
Daniel Maleaa35970a2012-11-23 18:09:58 +00001567 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1568 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001569 if (log)
1570 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001571 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1572 if (!monitor->Resume(pid, SIGSTOP)) {
1573 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1574 }
1575 stop_monitoring = false;
1576 } else {
1577 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1578 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001579 // stop the monitor thread if this is the main pid.
1580 if (log)
1581 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1582 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1583 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001584 // If we are going to stop monitoring, we need to notify our process object
1585 if (stop_monitoring)
1586 {
1587 message = ProcessMessage::Exit(pid, status);
1588 process->SendMessage(message);
1589 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001590 }
1591 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001592 else {
1593 switch (info.si_signo)
1594 {
1595 case SIGTRAP:
1596 message = MonitorSIGTRAP(monitor, &info, pid);
1597 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001598
Stephen Wilson84ffe702011-03-30 15:55:52 +00001599 default:
1600 message = MonitorSignal(monitor, &info, pid);
1601 break;
1602 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001603
Stephen Wilson84ffe702011-03-30 15:55:52 +00001604 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001605 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001606 }
1607
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001608 return stop_monitoring;
1609}
1610
1611ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001612ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001613 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001614{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001615 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001616
Andrew Kaylor93132f52013-05-28 23:04:25 +00001617 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1618
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001619 assert(monitor);
1620 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001621
Stephen Wilson84ffe702011-03-30 15:55:52 +00001622 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001623 {
1624 default:
1625 assert(false && "Unexpected SIGTRAP code!");
1626 break;
1627
Matt Kopeca360d7e2013-05-17 19:27:47 +00001628 // TODO: these two cases are required if we want to support tracing
1629 // of the inferiors' children
1630 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1631 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1632
Matt Kopec650648f2013-01-08 16:30:18 +00001633 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1634 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001635 if (log)
1636 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1637
Matt Kopec650648f2013-01-08 16:30:18 +00001638 unsigned long tid = 0;
1639 if (!monitor->GetEventMessage(pid, &tid))
1640 tid = -1;
1641 message = ProcessMessage::NewThread(pid, tid);
1642 break;
1643 }
1644
Matt Kopeca360d7e2013-05-17 19:27:47 +00001645 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001646 if (log)
1647 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1648
1649 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001650 break;
1651
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001652 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1653 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001654 // The inferior process or one of its threads is about to exit.
1655 // Maintain the process or thread in a state of "limbo" until we are
1656 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001657 unsigned long data = 0;
1658 if (!monitor->GetEventMessage(pid, &data))
1659 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001660 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001661 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001662 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001663 break;
1664 }
1665
1666 case 0:
1667 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001668 if (log)
1669 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001670 message = ProcessMessage::Trace(pid);
1671 break;
1672
1673 case SI_KERNEL:
1674 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001675 if (log)
1676 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001677 message = ProcessMessage::Break(pid);
1678 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001679
1680 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001681 if (log)
1682 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001683 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1684 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001685
1686 case SIGTRAP:
1687 case (SIGTRAP | 0x80):
1688 if (log)
1689 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1690 // Ignore these signals until we know more about them
1691 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001692 }
1693
1694 return message;
1695}
1696
Stephen Wilson84ffe702011-03-30 15:55:52 +00001697ProcessMessage
1698ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001699 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001700{
1701 ProcessMessage message;
1702 int signo = info->si_signo;
1703
Andrew Kaylor93132f52013-05-28 23:04:25 +00001704 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1705
Stephen Wilson84ffe702011-03-30 15:55:52 +00001706 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1707 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1708 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1709 //
1710 // IOW, user generated signals never generate what we consider to be a
1711 // "crash".
1712 //
1713 // Similarly, ACK signals generated by this monitor.
1714 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1715 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001716 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001717 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001718 __FUNCTION__,
1719 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1720 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1721 info->si_pid);
1722
Stephen Wilson84ffe702011-03-30 15:55:52 +00001723 if (info->si_pid == getpid())
1724 return ProcessMessage::SignalDelivered(pid, signo);
1725 else
1726 return ProcessMessage::Signal(pid, signo);
1727 }
1728
Andrew Kaylor93132f52013-05-28 23:04:25 +00001729 if (log)
1730 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1731
Stephen Wilson84ffe702011-03-30 15:55:52 +00001732 if (signo == SIGSEGV) {
1733 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1734 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1735 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1736 }
1737
1738 if (signo == SIGILL) {
1739 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1740 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1741 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1742 }
1743
1744 if (signo == SIGFPE) {
1745 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1746 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1747 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1748 }
1749
1750 if (signo == SIGBUS) {
1751 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1752 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1753 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1754 }
1755
1756 // Everything else is "normal" and does not require any special action on
1757 // our part.
1758 return ProcessMessage::Signal(pid, signo);
1759}
1760
Andrew Kaylord4d54992013-09-17 00:30:24 +00001761// On Linux, when a new thread is created, we receive to notifications,
1762// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1763// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1764// the new child thread indicating that it has is stopped because we attached.
1765// We have no guarantee of the order in which these arrive, but we need both
1766// before we are ready to proceed. We currently keep a list of threads which
1767// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1768// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1769// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1770
1771bool
1772ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1773{
1774 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1775 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001776 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001777
1778 // Wait for the thread to stop
1779 while (true)
1780 {
1781 int status = -1;
1782 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001783 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001784 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001785 if (status == -1)
1786 {
1787 // If we got interrupted by a signal (in our process, not the
1788 // inferior) try again.
1789 if (errno == EINTR)
1790 continue;
1791 else
1792 {
1793 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001794 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001795 return false; // This is bad, but there's nothing we can do.
1796 }
1797 }
1798
1799 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001800 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001801
Todd Fiala9be50492014-07-01 16:30:53 +00001802 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001803
1804 siginfo_t info;
1805 int ptrace_err;
1806 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1807 {
1808 if (log)
1809 {
1810 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1811 }
1812 return false;
1813 }
1814
1815 // If this is a thread exit, we won't get any more information.
1816 if (WIFEXITED(status))
1817 {
1818 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001819 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001820 return true;
1821 continue;
1822 }
1823
1824 assert(info.si_code == SI_USER);
1825 assert(WSTOPSIG(status) == SIGSTOP);
1826
1827 if (log)
1828 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1829 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1830 return true;
1831 }
1832 return false;
1833}
1834
Andrew Kaylor93132f52013-05-28 23:04:25 +00001835bool
1836ProcessMonitor::StopThread(lldb::tid_t tid)
1837{
1838 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1839
1840 // FIXME: Try to use tgkill or tkill
1841 int ret = tgkill(m_pid, tid, SIGSTOP);
1842 if (log)
1843 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1844
1845 // This can happen if a thread exited while we were trying to stop it. That's OK.
1846 // We'll get the signal for that later.
1847 if (ret < 0)
1848 return false;
1849
1850 // Wait for the thread to stop
1851 while (true)
1852 {
1853 int status = -1;
1854 if (log)
1855 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001856 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001857 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001858 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1859 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001860
Todd Fiala9be50492014-07-01 16:30:53 +00001861 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001862 {
1863 // If we got interrupted by a signal (in our process, not the
1864 // inferior) try again.
1865 if (errno == EINTR)
1866 continue;
1867 else
1868 return false; // This is bad, but there's nothing we can do.
1869 }
1870
1871 // If this is a thread exit, we won't get any more information.
1872 if (WIFEXITED(status))
1873 {
1874 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001875 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001876 return true;
1877 continue;
1878 }
1879
1880 siginfo_t info;
1881 int ptrace_err;
1882 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1883 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001884 // another signal causing a StopAllThreads may have been received
1885 // before wait_pid's group-stop was processed, handle it now
1886 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001887 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001888 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001889
Todd Fiala1b0539c2014-01-27 17:03:57 +00001890 if (log)
1891 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1892 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1893 if (!Resume(wait_pid, SIGSTOP)) {
1894 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1895 }
1896 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001897 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001898
1899 if (log)
1900 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001901 return false;
1902 }
1903
1904 // Handle events from other threads
1905 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001906 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1907 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001908
1909 ProcessMessage message;
1910 if (info.si_signo == SIGTRAP)
1911 message = MonitorSIGTRAP(this, &info, wait_pid);
1912 else
1913 message = MonitorSignal(this, &info, wait_pid);
1914
1915 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1916
1917 // When a new thread is created, we may get a SIGSTOP for the new thread
1918 // just before we get the SIGTRAP that we use to add the thread to our
1919 // process thread list. We don't need to worry about that signal here.
1920 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1921
1922 if (!thread)
1923 {
1924 m_process->SendMessage(message);
1925 continue;
1926 }
1927
1928 switch (message.GetKind())
1929 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00001930 case ProcessMessage::eExecMessage:
1931 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00001932 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001933 case ProcessMessage::eInvalidMessage:
1934 break;
1935
1936 // These need special handling because we don't want to send a
1937 // resume even if we already sent a SIGSTOP to this thread. In
1938 // this case the resume will cause the thread to disappear. It is
1939 // unlikely that we'll ever get eExitMessage here, but the same
1940 // reasoning applies.
1941 case ProcessMessage::eLimboMessage:
1942 case ProcessMessage::eExitMessage:
1943 if (log)
1944 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1945 // SendMessage will set the thread state as needed.
1946 m_process->SendMessage(message);
1947 // If this is the thread we're waiting for, stop waiting. Even
1948 // though this wasn't the signal we expected, it's the last
1949 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00001950 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001951 return true;
1952 break;
1953
Matt Kopecb2910442013-07-09 15:09:45 +00001954 case ProcessMessage::eSignalMessage:
1955 if (log)
1956 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1957 if (WSTOPSIG(status) == SIGSTOP)
1958 {
1959 m_process->AddThreadForInitialStopIfNeeded(tid);
1960 thread->SetState(lldb::eStateStopped);
1961 }
1962 else
1963 {
1964 m_process->SendMessage(message);
1965 // This isn't the stop we were expecting, but the thread is
1966 // stopped. SendMessage will handle processing of this event,
1967 // but we need to resume here to get the stop we are waiting
1968 // for (otherwise the thread will stop again immediately when
1969 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001970 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00001971 Resume(wait_pid, eResumeSignalNone);
1972 }
1973 break;
1974
Andrew Kaylor93132f52013-05-28 23:04:25 +00001975 case ProcessMessage::eSignalDeliveredMessage:
1976 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00001977 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
1978 WIFSTOPPED(status) &&
1979 WSTOPSIG(status) == SIGSTOP &&
1980 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001981 {
1982 if (log)
1983 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1984 thread->SetState(lldb::eStateStopped);
1985 return true;
1986 }
1987 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001988 case ProcessMessage::eBreakpointMessage:
1989 case ProcessMessage::eTraceMessage:
1990 case ProcessMessage::eWatchpointMessage:
1991 case ProcessMessage::eCrashMessage:
1992 case ProcessMessage::eNewThreadMessage:
1993 if (log)
1994 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1995 // SendMessage will set the thread state as needed.
1996 m_process->SendMessage(message);
1997 // This isn't the stop we were expecting, but the thread is
1998 // stopped. SendMessage will handle processing of this event,
1999 // but we need to resume here to get the stop we are waiting
2000 // for (otherwise the thread will stop again immediately when
2001 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002002 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002003 Resume(wait_pid, eResumeSignalNone);
2004 break;
2005 }
2006 }
2007 return false;
2008}
2009
Stephen Wilson84ffe702011-03-30 15:55:52 +00002010ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002011ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002012{
2013 ProcessMessage::CrashReason reason;
2014 assert(info->si_signo == SIGSEGV);
2015
2016 reason = ProcessMessage::eInvalidCrashReason;
2017
Greg Clayton542e4072012-09-07 17:49:29 +00002018 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002019 {
2020 default:
2021 assert(false && "unexpected si_code for SIGSEGV");
2022 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00002023 case SI_KERNEL:
2024 // Linux will occasionally send spurious SI_KERNEL codes.
2025 // (this is poorly documented in sigaction)
2026 // One way to get this is via unaligned SIMD loads.
2027 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
2028 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002029 case SEGV_MAPERR:
2030 reason = ProcessMessage::eInvalidAddress;
2031 break;
2032 case SEGV_ACCERR:
2033 reason = ProcessMessage::ePrivilegedAddress;
2034 break;
2035 }
Greg Clayton542e4072012-09-07 17:49:29 +00002036
Stephen Wilson84ffe702011-03-30 15:55:52 +00002037 return reason;
2038}
2039
2040ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002041ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002042{
2043 ProcessMessage::CrashReason reason;
2044 assert(info->si_signo == SIGILL);
2045
2046 reason = ProcessMessage::eInvalidCrashReason;
2047
2048 switch (info->si_code)
2049 {
2050 default:
2051 assert(false && "unexpected si_code for SIGILL");
2052 break;
2053 case ILL_ILLOPC:
2054 reason = ProcessMessage::eIllegalOpcode;
2055 break;
2056 case ILL_ILLOPN:
2057 reason = ProcessMessage::eIllegalOperand;
2058 break;
2059 case ILL_ILLADR:
2060 reason = ProcessMessage::eIllegalAddressingMode;
2061 break;
2062 case ILL_ILLTRP:
2063 reason = ProcessMessage::eIllegalTrap;
2064 break;
2065 case ILL_PRVOPC:
2066 reason = ProcessMessage::ePrivilegedOpcode;
2067 break;
2068 case ILL_PRVREG:
2069 reason = ProcessMessage::ePrivilegedRegister;
2070 break;
2071 case ILL_COPROC:
2072 reason = ProcessMessage::eCoprocessorError;
2073 break;
2074 case ILL_BADSTK:
2075 reason = ProcessMessage::eInternalStackError;
2076 break;
2077 }
2078
2079 return reason;
2080}
2081
2082ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002083ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002084{
2085 ProcessMessage::CrashReason reason;
2086 assert(info->si_signo == SIGFPE);
2087
2088 reason = ProcessMessage::eInvalidCrashReason;
2089
2090 switch (info->si_code)
2091 {
2092 default:
2093 assert(false && "unexpected si_code for SIGFPE");
2094 break;
2095 case FPE_INTDIV:
2096 reason = ProcessMessage::eIntegerDivideByZero;
2097 break;
2098 case FPE_INTOVF:
2099 reason = ProcessMessage::eIntegerOverflow;
2100 break;
2101 case FPE_FLTDIV:
2102 reason = ProcessMessage::eFloatDivideByZero;
2103 break;
2104 case FPE_FLTOVF:
2105 reason = ProcessMessage::eFloatOverflow;
2106 break;
2107 case FPE_FLTUND:
2108 reason = ProcessMessage::eFloatUnderflow;
2109 break;
2110 case FPE_FLTRES:
2111 reason = ProcessMessage::eFloatInexactResult;
2112 break;
2113 case FPE_FLTINV:
2114 reason = ProcessMessage::eFloatInvalidOperation;
2115 break;
2116 case FPE_FLTSUB:
2117 reason = ProcessMessage::eFloatSubscriptRange;
2118 break;
2119 }
2120
2121 return reason;
2122}
2123
2124ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002125ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002126{
2127 ProcessMessage::CrashReason reason;
2128 assert(info->si_signo == SIGBUS);
2129
2130 reason = ProcessMessage::eInvalidCrashReason;
2131
2132 switch (info->si_code)
2133 {
2134 default:
2135 assert(false && "unexpected si_code for SIGBUS");
2136 break;
2137 case BUS_ADRALN:
2138 reason = ProcessMessage::eIllegalAlignment;
2139 break;
2140 case BUS_ADRERR:
2141 reason = ProcessMessage::eIllegalAddress;
2142 break;
2143 case BUS_OBJERR:
2144 reason = ProcessMessage::eHardwareError;
2145 break;
2146 }
2147
2148 return reason;
2149}
2150
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002151void
Johnny Chen25e68e32011-06-14 19:19:50 +00002152ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002153{
Stephen Wilson570243b2011-01-19 01:37:06 +00002154 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002155
Stephen Wilson570243b2011-01-19 01:37:06 +00002156 // We are finised with the arguments and are ready to go. Sync with the
2157 // parent thread and start serving operations on the inferior.
2158 sem_post(&args->m_semaphore);
2159
Michael Sartain704bf892013-10-09 01:28:57 +00002160 for(;;)
2161 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002162 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002163 if (sem_wait(&monitor->m_operation_pending))
2164 {
2165 if (errno == EINTR)
2166 continue;
2167 assert(false && "Unexpected errno from sem_wait");
2168 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002169
Daniel Malea1efb4182013-09-16 23:12:18 +00002170 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002171
Daniel Malea1efb4182013-09-16 23:12:18 +00002172 // notify calling thread that operation is complete
2173 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002174 }
2175}
2176
2177void
2178ProcessMonitor::DoOperation(Operation *op)
2179{
Daniel Malea1efb4182013-09-16 23:12:18 +00002180 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002181
Daniel Malea1efb4182013-09-16 23:12:18 +00002182 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002183
Daniel Malea1efb4182013-09-16 23:12:18 +00002184 // notify operation thread that an operation is ready to be processed
2185 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002186
Daniel Malea1efb4182013-09-16 23:12:18 +00002187 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002188 while (sem_wait(&m_operation_done))
2189 {
2190 if (errno == EINTR)
2191 continue;
2192 assert(false && "Unexpected errno from sem_wait");
2193 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002194}
2195
2196size_t
2197ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2198 Error &error)
2199{
2200 size_t result;
2201 ReadOperation op(vm_addr, buf, size, error, result);
2202 DoOperation(&op);
2203 return result;
2204}
2205
2206size_t
2207ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2208 lldb_private::Error &error)
2209{
2210 size_t result;
2211 WriteOperation op(vm_addr, buf, size, error, result);
2212 DoOperation(&op);
2213 return result;
2214}
2215
2216bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002217ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002218 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002219{
2220 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002221 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002222 DoOperation(&op);
2223 return result;
2224}
2225
2226bool
Matt Kopec7de48462013-03-06 17:20:48 +00002227ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002228 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002229{
2230 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002231 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002232 DoOperation(&op);
2233 return result;
2234}
2235
2236bool
Matt Kopec7de48462013-03-06 17:20:48 +00002237ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002238{
2239 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002240 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002241 DoOperation(&op);
2242 return result;
2243}
2244
2245bool
Matt Kopec7de48462013-03-06 17:20:48 +00002246ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002247{
2248 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002249 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002250 DoOperation(&op);
2251 return result;
2252}
2253
2254bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002255ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2256{
2257 bool result;
2258 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2259 DoOperation(&op);
2260 return result;
2261}
2262
2263bool
Matt Kopec7de48462013-03-06 17:20:48 +00002264ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002265{
2266 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002267 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002268 DoOperation(&op);
2269 return result;
2270}
2271
2272bool
Matt Kopec7de48462013-03-06 17:20:48 +00002273ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002274{
2275 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002276 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002277 DoOperation(&op);
2278 return result;
2279}
2280
2281bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002282ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2283{
2284 bool result;
2285 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2286 DoOperation(&op);
2287 return result;
2288}
2289
2290bool
Richard Mitton0a558352013-10-17 21:14:00 +00002291ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2292{
2293 bool result;
2294 ReadThreadPointerOperation op(tid, &value, result);
2295 DoOperation(&op);
2296 return result;
2297}
2298
2299bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002300ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002301{
2302 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002303 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2304
2305 if (log)
2306 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2307 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002308 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002309 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002310 if (log)
2311 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002312 return result;
2313}
2314
2315bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002316ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002317{
2318 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002319 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002320 DoOperation(&op);
2321 return result;
2322}
2323
2324bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002325ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002326{
Ed Maste4e0999b2014-04-01 18:14:06 +00002327 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002328}
2329
2330bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002331ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002332{
2333 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002334 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002335 DoOperation(&op);
2336 return result;
2337}
2338
2339bool
2340ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2341{
2342 bool result;
2343 EventMessageOperation op(tid, message, result);
2344 DoOperation(&op);
2345 return result;
2346}
2347
Greg Clayton743ecf42012-10-16 20:20:18 +00002348lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002349ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002350{
Greg Clayton28041352011-11-29 20:50:10 +00002351 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002352 if (tid != LLDB_INVALID_THREAD_ID)
2353 {
2354 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002355 DoOperation(&op);
2356 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002357 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002358}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002359
2360bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002361ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2362{
Peter Collingbourne62343202011-06-14 03:55:54 +00002363 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002364
2365 if (target_fd == -1)
2366 return false;
2367
Peter Collingbourne62343202011-06-14 03:55:54 +00002368 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002369}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002370
2371void
2372ProcessMonitor::StopMonitoringChildProcess()
2373{
2374 lldb::thread_result_t thread_result;
2375
Stephen Wilsond4182f42011-02-09 20:10:35 +00002376 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002377 {
2378 Host::ThreadCancel(m_monitor_thread, NULL);
2379 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2380 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2381 }
2382}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002383
2384void
2385ProcessMonitor::StopMonitor()
2386{
2387 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002388 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002389 sem_destroy(&m_operation_pending);
2390 sem_destroy(&m_operation_done);
2391
Andrew Kaylor5e268992013-09-14 00:17:31 +00002392 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2393 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2394 // the descriptor to a ConnectionFileDescriptor object. Consequently
2395 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002396}
2397
2398void
Greg Clayton743ecf42012-10-16 20:20:18 +00002399ProcessMonitor::StopOpThread()
2400{
2401 lldb::thread_result_t result;
2402
2403 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2404 return;
2405
2406 Host::ThreadCancel(m_operation_thread, NULL);
2407 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002408 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002409}