blob: bfe8dc984a4b887d789bcaf3e7e7d307b0ea8bc7 [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
Todd Fialadbec1ff2014-09-04 16:08:20 +000068#define LLDB_PTRACE_NT_ARM_TLS 0x401 // ARM TLS register
69
Matt Kopece9ea0da2013-05-07 19:29:28 +000070// Support hardware breakpoints in case it has not been defined
71#ifndef TRAP_HWBKPT
72 #define TRAP_HWBKPT 4
73#endif
74
Andrew Kaylor93132f52013-05-28 23:04:25 +000075// Try to define a macro to encapsulate the tgkill syscall
76// fall back on kill() if tgkill isn't available
77#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
78
Stephen Wilsone6f9f662010-07-24 02:19:04 +000079using namespace lldb_private;
80
Johnny Chen0d5f2d42011-10-18 18:09:30 +000081// FIXME: this code is host-dependent with respect to types and
82// endianness and needs to be fixed. For example, lldb::addr_t is
83// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
84// 32-bit pointer arguments. This code uses casts to work around the
85// problem.
86
87// We disable the tracing of ptrace calls for integration builds to
88// avoid the additional indirection and checks.
89#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
90
Greg Clayton386ff182011-11-05 01:09:16 +000091static void
92DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
93{
94 uint8_t *ptr = (uint8_t *)bytes;
95 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
96 for(uint32_t i=0; i<loop_count; i++)
97 {
98 s.Printf ("[%x]", *ptr);
99 ptr++;
100 }
101}
102
Matt Kopec58c0b962013-03-20 20:34:35 +0000103static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000104{
105 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000106 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000107 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000108
109 if (verbose_log)
110 {
111 switch(req)
112 {
113 case PTRACE_POKETEXT:
114 {
115 DisplayBytes(buf, &data, 8);
116 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, &data, 8);
122 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
123 break;
124 }
Greg Clayton542e4072012-09-07 17:49:29 +0000125 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000126 {
127 DisplayBytes(buf, &data, 8);
128 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
129 break;
130 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000131#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000132 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000133 {
Matt Kopec7de48462013-03-06 17:20:48 +0000134 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000135 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
136 break;
137 }
138 case PTRACE_SETFPREGS:
139 {
Matt Kopec7de48462013-03-06 17:20:48 +0000140 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000141 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
142 break;
143 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000144#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000145 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000146 {
147 DisplayBytes(buf, data, sizeof(siginfo_t));
148 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
149 break;
150 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000151 case PTRACE_SETREGSET:
152 {
153 // Extract iov_base from data, which is a pointer to the struct IOVEC
154 DisplayBytes(buf, *(void **)data, data_size);
155 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
156 break;
157 }
Greg Clayton386ff182011-11-05 01:09:16 +0000158 default:
159 {
160 }
161 }
162 }
163}
164
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000165// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000166// 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 +0000167extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000168PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000169 const char* reqName, const char* file, int line)
170{
Greg Clayton386ff182011-11-05 01:09:16 +0000171 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000172
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000173 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000174
Matt Kopec7de48462013-03-06 17:20:48 +0000175 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000176
177 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000178 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000179 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000180 else
Todd Fiala4507f062014-02-27 20:46:12 +0000181 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000182
Ed Mastec099c952014-02-24 14:07:45 +0000183 if (log)
184 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
185 reqName, pid, addr, data, data_size, result, file, line);
186
Matt Kopec7de48462013-03-06 17:20:48 +0000187 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000188
Matt Kopec7de48462013-03-06 17:20:48 +0000189 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000190 {
191 const char* str;
192 switch (errno)
193 {
194 case ESRCH: str = "ESRCH"; break;
195 case EINVAL: str = "EINVAL"; break;
196 case EBUSY: str = "EBUSY"; break;
197 case EPERM: str = "EPERM"; break;
198 default: str = "<unknown>";
199 }
200 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
201 }
202
203 return result;
204}
205
Matt Kopec7de48462013-03-06 17:20:48 +0000206// Wrapper for ptrace when logging is not required.
207// Sets errno to 0 prior to calling ptrace.
208extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000209PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000210{
Matt Kopec58c0b962013-03-20 20:34:35 +0000211 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000212 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000213 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
214 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
215 else
216 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000217 return result;
218}
219
220#define PTRACE(req, pid, addr, data, data_size) \
221 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000222#else
Matt Kopec7de48462013-03-06 17:20:48 +0000223 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000224#endif
225
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226//------------------------------------------------------------------------------
227// Static implementations of ProcessMonitor::ReadMemory and
228// ProcessMonitor::WriteMemory. This enables mutual recursion between these
229// functions without needed to go thru the thread funnel.
230
231static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000232DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000233 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
234{
Greg Clayton542e4072012-09-07 17:49:29 +0000235 // ptrace word size is determined by the host, not the child
236 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000237 unsigned char *dst = static_cast<unsigned char*>(buf);
238 size_t bytes_read;
239 size_t remainder;
240 long data;
241
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000242 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000243 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000244 ProcessPOSIXLog::IncNestLevel();
245 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000246 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000247 pid, word_size, (void*)vm_addr, buf, size);
248
249 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000250 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
251 {
252 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000253 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
254 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255 {
256 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000257 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000258 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259 return bytes_read;
260 }
261
262 remainder = size - bytes_read;
263 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000264
265 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000266 for (unsigned i = 0; i < remainder; ++i)
267 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000268
Johnny Chen30213ff2012-01-05 19:17:38 +0000269 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
270 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
271 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
272 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000273 {
274 uintptr_t print_dst = 0;
275 // Format bytes from data by moving into print_dst for log output
276 for (unsigned i = 0; i < remainder; ++i)
277 print_dst |= (((data >> i*8) & 0xFF) << i*8);
278 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
279 (void*)vm_addr, print_dst, (unsigned long)data);
280 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000281
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000282 vm_addr += word_size;
283 dst += word_size;
284 }
285
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000286 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000287 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000288 return bytes_read;
289}
290
291static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000292DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000293 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
294{
Greg Clayton542e4072012-09-07 17:49:29 +0000295 // ptrace word size is determined by the host, not the child
296 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000297 const unsigned char *src = static_cast<const unsigned char*>(buf);
298 size_t bytes_written = 0;
299 size_t remainder;
300
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000301 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000302 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000303 ProcessPOSIXLog::IncNestLevel();
304 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000305 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000306 pid, word_size, (void*)vm_addr, buf, size);
307
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000308 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
309 {
310 remainder = size - bytes_written;
311 remainder = remainder > word_size ? word_size : remainder;
312
313 if (remainder == word_size)
314 {
315 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000316 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000317 for (unsigned i = 0; i < word_size; ++i)
318 data |= (unsigned long)src[i] << i*8;
319
Johnny Chen30213ff2012-01-05 19:17:38 +0000320 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
321 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
322 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
323 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000324 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
325 (void*)vm_addr, *(unsigned long*)src, data);
326
Matt Kopec7de48462013-03-06 17:20:48 +0000327 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000328 {
329 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000330 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000331 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000332 return bytes_written;
333 }
334 }
335 else
336 {
337 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000338 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000339 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000340 {
341 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000342 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000343 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000344 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345
346 memcpy(buff, src, remainder);
347
Greg Clayton542e4072012-09-07 17:49:29 +0000348 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000349 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000350 {
351 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000352 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000353 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000354 }
355
Johnny Chen30213ff2012-01-05 19:17:38 +0000356 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
357 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
358 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
359 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000360 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
361 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000362 }
363
364 vm_addr += word_size;
365 src += word_size;
366 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000367 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000368 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000369 return bytes_written;
370}
371
Stephen Wilson26977162011-03-23 02:14:42 +0000372// Simple helper function to ensure flags are enabled on the given file
373// descriptor.
374static bool
375EnsureFDFlags(int fd, int flags, Error &error)
376{
377 int status;
378
379 if ((status = fcntl(fd, F_GETFL)) == -1)
380 {
381 error.SetErrorToErrno();
382 return false;
383 }
384
385 if (fcntl(fd, F_SETFL, status | flags) == -1)
386 {
387 error.SetErrorToErrno();
388 return false;
389 }
390
391 return true;
392}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000393
394//------------------------------------------------------------------------------
395/// @class Operation
396/// @brief Represents a ProcessMonitor operation.
397///
398/// Under Linux, it is not possible to ptrace() from any other thread but the
399/// one that spawned or attached to the process from the start. Therefore, when
400/// a ProcessMonitor is asked to deliver or change the state of an inferior
401/// process the operation must be "funneled" to a specific thread to perform the
402/// task. The Operation class provides an abstract base for all services the
403/// ProcessMonitor must perform via the single virtual function Execute, thus
404/// encapsulating the code that needs to run in the privileged context.
405class Operation
406{
407public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000408 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000409 virtual void Execute(ProcessMonitor *monitor) = 0;
410};
411
412//------------------------------------------------------------------------------
413/// @class ReadOperation
414/// @brief Implements ProcessMonitor::ReadMemory.
415class ReadOperation : public Operation
416{
417public:
418 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
419 Error &error, size_t &result)
420 : m_addr(addr), m_buff(buff), m_size(size),
421 m_error(error), m_result(result)
422 { }
423
424 void Execute(ProcessMonitor *monitor);
425
426private:
427 lldb::addr_t m_addr;
428 void *m_buff;
429 size_t m_size;
430 Error &m_error;
431 size_t &m_result;
432};
433
434void
435ReadOperation::Execute(ProcessMonitor *monitor)
436{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000437 lldb::pid_t pid = monitor->GetPID();
438
Greg Clayton542e4072012-09-07 17:49:29 +0000439 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000440}
441
442//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000443/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000444/// @brief Implements ProcessMonitor::WriteMemory.
445class WriteOperation : public Operation
446{
447public:
448 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
449 Error &error, size_t &result)
450 : m_addr(addr), m_buff(buff), m_size(size),
451 m_error(error), m_result(result)
452 { }
453
454 void Execute(ProcessMonitor *monitor);
455
456private:
457 lldb::addr_t m_addr;
458 const void *m_buff;
459 size_t m_size;
460 Error &m_error;
461 size_t &m_result;
462};
463
464void
465WriteOperation::Execute(ProcessMonitor *monitor)
466{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000467 lldb::pid_t pid = monitor->GetPID();
468
Greg Clayton542e4072012-09-07 17:49:29 +0000469 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000470}
471
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000472
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000473//------------------------------------------------------------------------------
474/// @class ReadRegOperation
475/// @brief Implements ProcessMonitor::ReadRegisterValue.
476class ReadRegOperation : public Operation
477{
478public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000479 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000481 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000482 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000483 { }
484
485 void Execute(ProcessMonitor *monitor);
486
487private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000488 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000489 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000490 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000491 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000492 bool &m_result;
493};
494
495void
496ReadRegOperation::Execute(ProcessMonitor *monitor)
497{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000498 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000499
500 // Set errno to zero so that we can detect a failed peek.
501 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000502 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
503 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000504 m_result = false;
505 else
506 {
507 m_value = data;
508 m_result = true;
509 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000510 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000511 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000512 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000513}
514
515//------------------------------------------------------------------------------
516/// @class WriteRegOperation
517/// @brief Implements ProcessMonitor::WriteRegisterValue.
518class WriteRegOperation : public Operation
519{
520public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000521 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000523 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000524 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000525 { }
526
527 void Execute(ProcessMonitor *monitor);
528
529private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000530 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000531 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000532 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000533 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000534 bool &m_result;
535};
536
537void
538WriteRegOperation::Execute(ProcessMonitor *monitor)
539{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000540 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000541 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000542
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000543 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000544
545 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000546 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000547 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000548 m_result = false;
549 else
550 m_result = true;
551}
552
553//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000554/// @class ReadGPROperation
555/// @brief Implements ProcessMonitor::ReadGPR.
556class ReadGPROperation : public Operation
557{
558public:
Matt Kopec7de48462013-03-06 17:20:48 +0000559 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
560 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000561 { }
562
563 void Execute(ProcessMonitor *monitor);
564
565private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000566 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000567 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000568 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000569 bool &m_result;
570};
571
572void
573ReadGPROperation::Execute(ProcessMonitor *monitor)
574{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000575#if defined (__arm64__) || defined (__aarch64__)
576 int regset = NT_PRSTATUS;
577 struct iovec ioVec;
578
579 ioVec.iov_base = m_buf;
580 ioVec.iov_len = m_buf_size;
581 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000582 m_result = false;
583 else
584 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000585#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000586 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
587 m_result = false;
588 else
589 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000590#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000591}
592
593//------------------------------------------------------------------------------
594/// @class ReadFPROperation
595/// @brief Implements ProcessMonitor::ReadFPR.
596class ReadFPROperation : public Operation
597{
598public:
Matt Kopec7de48462013-03-06 17:20:48 +0000599 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
600 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000601 { }
602
603 void Execute(ProcessMonitor *monitor);
604
605private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000606 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000607 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000608 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000609 bool &m_result;
610};
611
612void
613ReadFPROperation::Execute(ProcessMonitor *monitor)
614{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000615#if defined (__arm64__) || defined (__aarch64__)
616 int regset = NT_FPREGSET;
617 struct iovec ioVec;
618
619 ioVec.iov_base = m_buf;
620 ioVec.iov_len = m_buf_size;
621 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000622 m_result = false;
623 else
624 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000625#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000626 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
627 m_result = false;
628 else
629 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000630#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000631}
632
633//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000634/// @class ReadRegisterSetOperation
635/// @brief Implements ProcessMonitor::ReadRegisterSet.
636class ReadRegisterSetOperation : public Operation
637{
638public:
639 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
640 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
641 { }
642
643 void Execute(ProcessMonitor *monitor);
644
645private:
646 lldb::tid_t m_tid;
647 void *m_buf;
648 size_t m_buf_size;
649 const unsigned int m_regset;
650 bool &m_result;
651};
652
653void
654ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
655{
656 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
657 m_result = false;
658 else
659 m_result = true;
660}
661
662//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000663/// @class WriteGPROperation
664/// @brief Implements ProcessMonitor::WriteGPR.
665class WriteGPROperation : public Operation
666{
667public:
Matt Kopec7de48462013-03-06 17:20:48 +0000668 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
669 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000670 { }
671
672 void Execute(ProcessMonitor *monitor);
673
674private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000675 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000676 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000677 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000678 bool &m_result;
679};
680
681void
682WriteGPROperation::Execute(ProcessMonitor *monitor)
683{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000684#if defined (__arm64__) || defined (__aarch64__)
685 int regset = NT_PRSTATUS;
686 struct iovec ioVec;
687
688 ioVec.iov_base = m_buf;
689 ioVec.iov_len = m_buf_size;
690 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000691 m_result = false;
692 else
693 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000694#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000695 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
696 m_result = false;
697 else
698 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000699#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000700}
701
702//------------------------------------------------------------------------------
703/// @class WriteFPROperation
704/// @brief Implements ProcessMonitor::WriteFPR.
705class WriteFPROperation : public Operation
706{
707public:
Matt Kopec7de48462013-03-06 17:20:48 +0000708 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
709 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000710 { }
711
712 void Execute(ProcessMonitor *monitor);
713
714private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000715 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000716 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000717 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000718 bool &m_result;
719};
720
721void
722WriteFPROperation::Execute(ProcessMonitor *monitor)
723{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000724#if defined (__arm64__) || defined (__aarch64__)
725 int regset = NT_FPREGSET;
726 struct iovec ioVec;
727
728 ioVec.iov_base = m_buf;
729 ioVec.iov_len = m_buf_size;
730 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000731 m_result = false;
732 else
733 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000734#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000735 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
736 m_result = false;
737 else
738 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000739#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000740}
741
742//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000743/// @class WriteRegisterSetOperation
744/// @brief Implements ProcessMonitor::WriteRegisterSet.
745class WriteRegisterSetOperation : public Operation
746{
747public:
748 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
749 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
750 { }
751
752 void Execute(ProcessMonitor *monitor);
753
754private:
755 lldb::tid_t m_tid;
756 void *m_buf;
757 size_t m_buf_size;
758 const unsigned int m_regset;
759 bool &m_result;
760};
761
762void
763WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
764{
765 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
766 m_result = false;
767 else
768 m_result = true;
769}
770
771//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000772/// @class ReadThreadPointerOperation
773/// @brief Implements ProcessMonitor::ReadThreadPointer.
774class ReadThreadPointerOperation : public Operation
775{
776public:
777 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
778 : m_tid(tid), m_addr(addr), m_result(result)
779 { }
780
781 void Execute(ProcessMonitor *monitor);
782
783private:
784 lldb::tid_t m_tid;
785 lldb::addr_t *m_addr;
786 bool &m_result;
787};
788
789void
790ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
791{
792 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
793 if (log)
794 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
795
796 // The process for getting the thread area on Linux is
797 // somewhat... obscure. There's several different ways depending on
798 // what arch you're on, and what kernel version you have.
799
800 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
801 switch(arch.GetMachine())
802 {
Todd Fiala42079682014-08-27 16:05:26 +0000803 case llvm::Triple::aarch64:
804 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000805 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000806 struct iovec ioVec;
807
808 ioVec.iov_base = m_addr;
809 ioVec.iov_len = sizeof(lldb::addr_t);
810 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
811 m_result = false;
812 else
813 m_result = true;
814 break;
815 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000816#if defined(__i386__) || defined(__x86_64__)
817 // Note that struct user below has a field named i387 which is x86-specific.
818 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000819 case llvm::Triple::x86:
820 {
821 // Find the GS register location for our host architecture.
822 size_t gs_user_offset = offsetof(struct user, regs);
823#ifdef __x86_64__
824 gs_user_offset += offsetof(struct user_regs_struct, gs);
825#endif
826#ifdef __i386__
827 gs_user_offset += offsetof(struct user_regs_struct, xgs);
828#endif
829
830 // Read the GS register value to get the selector.
831 errno = 0;
832 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
833 if (errno)
834 {
835 m_result = false;
836 break;
837 }
838
839 // Read the LDT base for that selector.
840 uint32_t tmp[4];
841 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
842 *m_addr = tmp[1];
843 break;
844 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000845#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000846 case llvm::Triple::x86_64:
847 // Read the FS register base.
848 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
849 break;
850 default:
851 m_result = false;
852 break;
853 }
854}
855
856//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000857/// @class ResumeOperation
858/// @brief Implements ProcessMonitor::Resume.
859class ResumeOperation : public Operation
860{
861public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000862 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
863 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000864
865 void Execute(ProcessMonitor *monitor);
866
867private:
868 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000869 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000870 bool &m_result;
871};
872
873void
874ResumeOperation::Execute(ProcessMonitor *monitor)
875{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000876 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000877
878 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
879 data = m_signo;
880
Matt Kopec7de48462013-03-06 17:20:48 +0000881 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000882 {
883 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
884
885 if (log)
886 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000887 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000888 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000889 else
890 m_result = true;
891}
892
893//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000894/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000895/// @brief Implements ProcessMonitor::SingleStep.
896class SingleStepOperation : public Operation
897{
898public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000899 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
900 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000901
902 void Execute(ProcessMonitor *monitor);
903
904private:
905 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000906 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000907 bool &m_result;
908};
909
910void
911SingleStepOperation::Execute(ProcessMonitor *monitor)
912{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000913 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000914
915 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
916 data = m_signo;
917
Matt Kopec7de48462013-03-06 17:20:48 +0000918 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000919 m_result = false;
920 else
921 m_result = true;
922}
923
924//------------------------------------------------------------------------------
925/// @class SiginfoOperation
926/// @brief Implements ProcessMonitor::GetSignalInfo.
927class SiginfoOperation : public Operation
928{
929public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000930 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
931 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000932
933 void Execute(ProcessMonitor *monitor);
934
935private:
936 lldb::tid_t m_tid;
937 void *m_info;
938 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000939 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000940};
941
942void
943SiginfoOperation::Execute(ProcessMonitor *monitor)
944{
Matt Kopec7de48462013-03-06 17:20:48 +0000945 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000946 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000947 m_err = errno;
948 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000949 else
950 m_result = true;
951}
952
953//------------------------------------------------------------------------------
954/// @class EventMessageOperation
955/// @brief Implements ProcessMonitor::GetEventMessage.
956class EventMessageOperation : public Operation
957{
958public:
959 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
960 : m_tid(tid), m_message(message), m_result(result) { }
961
962 void Execute(ProcessMonitor *monitor);
963
964private:
965 lldb::tid_t m_tid;
966 unsigned long *m_message;
967 bool &m_result;
968};
969
970void
971EventMessageOperation::Execute(ProcessMonitor *monitor)
972{
Matt Kopec7de48462013-03-06 17:20:48 +0000973 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000974 m_result = false;
975 else
976 m_result = true;
977}
978
979//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000980/// @class DetachOperation
981/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000982class DetachOperation : public Operation
983{
984public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000985 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000986
987 void Execute(ProcessMonitor *monitor);
988
989private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000990 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000991 Error &m_error;
992};
993
994void
995DetachOperation::Execute(ProcessMonitor *monitor)
996{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000997 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000998 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000999}
1000
Johnny Chen25e68e32011-06-14 19:19:50 +00001001ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1002 : m_monitor(monitor)
1003{
1004 sem_init(&m_semaphore, 0, 0);
1005}
1006
1007ProcessMonitor::OperationArgs::~OperationArgs()
1008{
1009 sem_destroy(&m_semaphore);
1010}
1011
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1013 lldb_private::Module *module,
1014 char const **argv,
1015 char const **envp,
1016 const char *stdin_path,
1017 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001018 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001019 const char *working_dir,
1020 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001021 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001022 m_module(module),
1023 m_argv(argv),
1024 m_envp(envp),
1025 m_stdin_path(stdin_path),
1026 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001027 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001028 m_working_dir(working_dir),
1029 m_launch_info(launch_info)
1030{
1031}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001032
1033ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001034{ }
1035
1036ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1037 lldb::pid_t pid)
1038 : OperationArgs(monitor), m_pid(pid) { }
1039
1040ProcessMonitor::AttachArgs::~AttachArgs()
1041{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001042
1043//------------------------------------------------------------------------------
1044/// The basic design of the ProcessMonitor is built around two threads.
1045///
1046/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1047/// for changes in the debugee state. When a change is detected a
1048/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1049/// "drives" state changes in the debugger.
1050///
1051/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001052/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001053/// operations such as register reads/writes, stepping, etc. See the comments
1054/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001055ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001056 Module *module,
1057 const char *argv[],
1058 const char *envp[],
1059 const char *stdin_path,
1060 const char *stdout_path,
1061 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001062 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001063 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001064 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001065 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001066 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001067 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001068 m_pid(LLDB_INVALID_PROCESS_ID),
1069 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001070 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001071{
Daniel Malea1efb4182013-09-16 23:12:18 +00001072 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1073 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001074 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001075
Daniel Malea1efb4182013-09-16 23:12:18 +00001076 sem_init(&m_operation_pending, 0, 0);
1077 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001078
Johnny Chen25e68e32011-06-14 19:19:50 +00001079 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001080 if (!error.Success())
1081 return;
1082
1083WAIT_AGAIN:
1084 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001085 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001086 {
1087 if (errno == EINTR)
1088 goto WAIT_AGAIN;
1089 else
1090 {
1091 error.SetErrorToErrno();
1092 return;
1093 }
1094 }
1095
1096 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001097 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001098 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001099 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001100 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001101 return;
1102 }
1103
1104 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001105 m_monitor_thread = Host::StartMonitoringChildProcess(
1106 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001107 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001108 {
1109 error.SetErrorToGenericError();
1110 error.SetErrorString("Process launch failed.");
1111 return;
1112 }
1113}
1114
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001115ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001116 lldb::pid_t pid,
1117 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001118 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001119 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001120 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001121 m_pid(LLDB_INVALID_PROCESS_ID),
1122 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001123 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001124{
Daniel Malea1efb4182013-09-16 23:12:18 +00001125 sem_init(&m_operation_pending, 0, 0);
1126 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001127
Daniel Malea1efb4182013-09-16 23:12:18 +00001128 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001129
1130 StartAttachOpThread(args.get(), error);
1131 if (!error.Success())
1132 return;
1133
1134WAIT_AGAIN:
1135 // Wait for the operation thread to initialize.
1136 if (sem_wait(&args->m_semaphore))
1137 {
1138 if (errno == EINTR)
1139 goto WAIT_AGAIN;
1140 else
1141 {
1142 error.SetErrorToErrno();
1143 return;
1144 }
1145 }
1146
Greg Clayton743ecf42012-10-16 20:20:18 +00001147 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001148 if (!args->m_error.Success())
1149 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001150 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001151 error = args->m_error;
1152 return;
1153 }
1154
1155 // Finally, start monitoring the child process for change in state.
1156 m_monitor_thread = Host::StartMonitoringChildProcess(
1157 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1158 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1159 {
1160 error.SetErrorToGenericError();
1161 error.SetErrorString("Process attach failed.");
1162 return;
1163 }
1164}
1165
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001166ProcessMonitor::~ProcessMonitor()
1167{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001168 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169}
1170
1171//------------------------------------------------------------------------------
1172// Thread setup and tear down.
1173void
Johnny Chen25e68e32011-06-14 19:19:50 +00001174ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001175{
1176 static const char *g_thread_name = "lldb.process.linux.operation";
1177
Stephen Wilsond4182f42011-02-09 20:10:35 +00001178 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001179 return;
1180
1181 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001182 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001183}
1184
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001185void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001186ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001187{
1188 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1189
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001190 if (!Launch(args)) {
1191 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001193 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001194
Stephen Wilson570243b2011-01-19 01:37:06 +00001195 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001196 return NULL;
1197}
1198
1199bool
1200ProcessMonitor::Launch(LaunchArgs *args)
1201{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001202 assert (args && "null args");
1203 if (!args)
1204 return false;
1205
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001206 ProcessMonitor *monitor = args->m_monitor;
1207 ProcessLinux &process = monitor->GetProcess();
1208 const char **argv = args->m_argv;
1209 const char **envp = args->m_envp;
1210 const char *stdin_path = args->m_stdin_path;
1211 const char *stdout_path = args->m_stdout_path;
1212 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001213 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001214
1215 lldb_utility::PseudoTerminal terminal;
1216 const size_t err_len = 1024;
1217 char err_str[err_len];
1218 lldb::pid_t pid;
1219
1220 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001221 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001222
Stephen Wilson57740ec2011-01-15 00:12:41 +00001223 // Propagate the environment if one is not supplied.
1224 if (envp == NULL || envp[0] == NULL)
1225 envp = const_cast<const char **>(environ);
1226
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001227 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001228 {
1229 args->m_error.SetErrorToGenericError();
1230 args->m_error.SetErrorString("Process fork failed.");
1231 goto FINISH;
1232 }
1233
Peter Collingbourne6a520222011-06-14 03:55:58 +00001234 // Recognized child exit status codes.
1235 enum {
1236 ePtraceFailed = 1,
1237 eDupStdinFailed,
1238 eDupStdoutFailed,
1239 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001240 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001241 eExecFailed,
1242 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001243 };
1244
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001245 // Child process.
1246 if (pid == 0)
1247 {
1248 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001249 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001250 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001251
1252 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001253 if (setgid(getgid()) != 0)
1254 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001255
1256 // Let us have our own process group.
1257 setpgid(0, 0);
1258
Greg Clayton710dd5a2011-01-08 20:28:42 +00001259 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001260 //
1261 // FIXME: If two or more of the paths are the same we needlessly open
1262 // the same file multiple times.
1263 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001264 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001265 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001266
1267 if (stdout_path != NULL && stdout_path[0])
1268 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001269 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001270
1271 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001272 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001273 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001274
Daniel Malea6217d2a2013-01-08 14:49:22 +00001275 // Change working directory
1276 if (working_dir != NULL && working_dir[0])
1277 if (0 != ::chdir(working_dir))
1278 exit(eChdirFailed);
1279
Todd Fiala0bce1b62014-08-17 00:10:50 +00001280 // Disable ASLR if requested.
1281 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1282 {
1283 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1284 if (old_personality == -1)
1285 {
1286 if (log)
1287 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1288 }
1289 else
1290 {
1291 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1292 if (new_personality == -1)
1293 {
1294 if (log)
1295 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1296
1297 }
1298 else
1299 {
1300 if (log)
1301 log->Printf ("ProcessMonitor::%s disbling ASLR: SUCCESS", __FUNCTION__);
1302
1303 }
1304 }
1305 }
1306
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001307 // Execute. We should never return.
1308 execve(argv[0],
1309 const_cast<char *const *>(argv),
1310 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001311 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001312 }
1313
1314 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001315 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001316 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001317 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001318
1319 raw_pid = waitpid(pid, &status, 0);
1320 wpid = static_cast <lldb::pid_t> (raw_pid);
1321 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001322 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001323 args->m_error.SetErrorToErrno();
1324 goto FINISH;
1325 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001326 else if (WIFEXITED(status))
1327 {
1328 // open, dup or execve likely failed for some reason.
1329 args->m_error.SetErrorToGenericError();
1330 switch (WEXITSTATUS(status))
1331 {
Greg Clayton542e4072012-09-07 17:49:29 +00001332 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001333 args->m_error.SetErrorString("Child ptrace failed.");
1334 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001335 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001336 args->m_error.SetErrorString("Child open stdin failed.");
1337 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001338 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001339 args->m_error.SetErrorString("Child open stdout failed.");
1340 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001341 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001342 args->m_error.SetErrorString("Child open stderr failed.");
1343 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001344 case eChdirFailed:
1345 args->m_error.SetErrorString("Child failed to set working directory.");
1346 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001347 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001348 args->m_error.SetErrorString("Child exec failed.");
1349 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001350 case eSetGidFailed:
1351 args->m_error.SetErrorString("Child setgid failed.");
1352 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001353 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001354 args->m_error.SetErrorString("Child returned unknown exit status.");
1355 break;
1356 }
1357 goto FINISH;
1358 }
1359 assert(WIFSTOPPED(status) && wpid == pid &&
1360 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001361
Matt Kopec085d6ce2013-05-31 22:00:07 +00001362 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001363 {
1364 args->m_error.SetErrorToErrno();
1365 goto FINISH;
1366 }
1367
1368 // Release the master terminal descriptor and pass it off to the
1369 // ProcessMonitor instance. Similarly stash the inferior pid.
1370 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1371 monitor->m_pid = pid;
1372
Stephen Wilson26977162011-03-23 02:14:42 +00001373 // Set the terminal fd to be in non blocking mode (it simplifies the
1374 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1375 // descriptor to read from).
1376 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1377 goto FINISH;
1378
Johnny Chen30213ff2012-01-05 19:17:38 +00001379 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001380 // FIXME: should we be letting UpdateThreadList handle this?
1381 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001382 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001383
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001384 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001385 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001386 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001387
Matt Kopecb2910442013-07-09 15:09:45 +00001388 process.AddThreadForInitialStopIfNeeded(pid);
1389
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001390 // Let our process instance know the thread has stopped.
1391 process.SendMessage(ProcessMessage::Trace(pid));
1392
1393FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001394 return args->m_error.Success();
1395}
1396
Johnny Chen25e68e32011-06-14 19:19:50 +00001397void
1398ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1399{
1400 static const char *g_thread_name = "lldb.process.linux.operation";
1401
1402 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1403 return;
1404
1405 m_operation_thread =
1406 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1407}
1408
Johnny Chen25e68e32011-06-14 19:19:50 +00001409void *
1410ProcessMonitor::AttachOpThread(void *arg)
1411{
1412 AttachArgs *args = static_cast<AttachArgs*>(arg);
1413
Greg Clayton743ecf42012-10-16 20:20:18 +00001414 if (!Attach(args)) {
1415 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001416 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001417 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001418
1419 ServeOperation(args);
1420 return NULL;
1421}
1422
1423bool
1424ProcessMonitor::Attach(AttachArgs *args)
1425{
1426 lldb::pid_t pid = args->m_pid;
1427
1428 ProcessMonitor *monitor = args->m_monitor;
1429 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001430 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001431 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001432
Matt Kopec085d6ce2013-05-31 22:00:07 +00001433 // Use a map to keep track of the threads which we have attached/need to attach.
1434 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001435 if (pid <= 1)
1436 {
1437 args->m_error.SetErrorToGenericError();
1438 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1439 goto FINISH;
1440 }
1441
Matt Kopec085d6ce2013-05-31 22:00:07 +00001442 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001443 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001444 for (Host::TidMap::iterator it = tids_to_attach.begin();
1445 it != tids_to_attach.end(); ++it)
1446 {
1447 if (it->second == false)
1448 {
1449 lldb::tid_t tid = it->first;
1450
1451 // Attach to the requested process.
1452 // An attach will cause the thread to stop with a SIGSTOP.
1453 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1454 {
1455 // No such thread. The thread may have exited.
1456 // More error handling may be needed.
1457 if (errno == ESRCH)
1458 {
1459 tids_to_attach.erase(it);
1460 continue;
1461 }
1462 else
1463 {
1464 args->m_error.SetErrorToErrno();
1465 goto FINISH;
1466 }
1467 }
1468
Todd Fiala9be50492014-07-01 16:30:53 +00001469 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001470 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1471 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001472 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001473 {
1474 // No such thread. The thread may have exited.
1475 // More error handling may be needed.
1476 if (errno == ESRCH)
1477 {
1478 tids_to_attach.erase(it);
1479 continue;
1480 }
1481 else
1482 {
1483 args->m_error.SetErrorToErrno();
1484 goto FINISH;
1485 }
1486 }
1487
1488 if (!SetDefaultPtraceOpts(tid))
1489 {
1490 args->m_error.SetErrorToErrno();
1491 goto FINISH;
1492 }
1493
1494 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001495 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001496
Matt Kopec085d6ce2013-05-31 22:00:07 +00001497 if (log)
1498 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1499 process.GetThreadList().AddThread(inferior);
1500 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001501 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001502 }
1503 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001504 }
1505
Matt Kopec085d6ce2013-05-31 22:00:07 +00001506 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001507 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001508 monitor->m_pid = pid;
1509 // Let our process instance know the thread has stopped.
1510 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001511 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001512 else
1513 {
1514 args->m_error.SetErrorToGenericError();
1515 args->m_error.SetErrorString("No such process.");
1516 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001517
1518 FINISH:
1519 return args->m_error.Success();
1520}
1521
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001522bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001523ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1524{
1525 long ptrace_opts = 0;
1526
1527 // Have the child raise an event on exit. This is used to keep the child in
1528 // limbo until it is destroyed.
1529 ptrace_opts |= PTRACE_O_TRACEEXIT;
1530
1531 // Have the tracer trace threads which spawn in the inferior process.
1532 // TODO: if we want to support tracing the inferiors' child, add the
1533 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1534 ptrace_opts |= PTRACE_O_TRACECLONE;
1535
1536 // Have the tracer notify us before execve returns
1537 // (needed to disable legacy SIGTRAP generation)
1538 ptrace_opts |= PTRACE_O_TRACEEXEC;
1539
1540 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1541}
1542
1543bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001544ProcessMonitor::MonitorCallback(void *callback_baton,
1545 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001546 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001547 int signal,
1548 int status)
1549{
1550 ProcessMessage message;
1551 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001552 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001553 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001554 bool stop_monitoring;
1555 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001556 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001557
Andrew Kaylor93132f52013-05-28 23:04:25 +00001558 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1559
1560 if (exited)
1561 {
1562 if (log)
1563 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1564 message = ProcessMessage::Exit(pid, status);
1565 process->SendMessage(message);
1566 return pid == process->GetID();
1567 }
1568
Daniel Maleaa35970a2012-11-23 18:09:58 +00001569 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1570 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001571 if (log)
1572 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001573 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1574 if (!monitor->Resume(pid, SIGSTOP)) {
1575 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1576 }
1577 stop_monitoring = false;
1578 } else {
1579 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1580 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001581 // stop the monitor thread if this is the main pid.
1582 if (log)
1583 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1584 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1585 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001586 // If we are going to stop monitoring, we need to notify our process object
1587 if (stop_monitoring)
1588 {
1589 message = ProcessMessage::Exit(pid, status);
1590 process->SendMessage(message);
1591 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001592 }
1593 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001594 else {
1595 switch (info.si_signo)
1596 {
1597 case SIGTRAP:
1598 message = MonitorSIGTRAP(monitor, &info, pid);
1599 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001600
Stephen Wilson84ffe702011-03-30 15:55:52 +00001601 default:
1602 message = MonitorSignal(monitor, &info, pid);
1603 break;
1604 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001605
Stephen Wilson84ffe702011-03-30 15:55:52 +00001606 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001607 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001608 }
1609
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001610 return stop_monitoring;
1611}
1612
1613ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001614ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001615 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001616{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001617 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001618
Andrew Kaylor93132f52013-05-28 23:04:25 +00001619 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1620
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001621 assert(monitor);
1622 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001623
Stephen Wilson84ffe702011-03-30 15:55:52 +00001624 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001625 {
1626 default:
1627 assert(false && "Unexpected SIGTRAP code!");
1628 break;
1629
Matt Kopeca360d7e2013-05-17 19:27:47 +00001630 // TODO: these two cases are required if we want to support tracing
1631 // of the inferiors' children
1632 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1633 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1634
Matt Kopec650648f2013-01-08 16:30:18 +00001635 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1636 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001637 if (log)
1638 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1639
Matt Kopec650648f2013-01-08 16:30:18 +00001640 unsigned long tid = 0;
1641 if (!monitor->GetEventMessage(pid, &tid))
1642 tid = -1;
1643 message = ProcessMessage::NewThread(pid, tid);
1644 break;
1645 }
1646
Matt Kopeca360d7e2013-05-17 19:27:47 +00001647 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001648 if (log)
1649 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1650
1651 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001652 break;
1653
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001654 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1655 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001656 // The inferior process or one of its threads is about to exit.
1657 // Maintain the process or thread in a state of "limbo" until we are
1658 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001659 unsigned long data = 0;
1660 if (!monitor->GetEventMessage(pid, &data))
1661 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001662 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001663 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001664 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001665 break;
1666 }
1667
1668 case 0:
1669 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001670 if (log)
1671 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001672 message = ProcessMessage::Trace(pid);
1673 break;
1674
1675 case SI_KERNEL:
1676 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001677 if (log)
1678 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001679 message = ProcessMessage::Break(pid);
1680 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001681
1682 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001683 if (log)
1684 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001685 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1686 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001687
1688 case SIGTRAP:
1689 case (SIGTRAP | 0x80):
1690 if (log)
1691 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1692 // Ignore these signals until we know more about them
1693 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001694 }
1695
1696 return message;
1697}
1698
Stephen Wilson84ffe702011-03-30 15:55:52 +00001699ProcessMessage
1700ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001701 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001702{
1703 ProcessMessage message;
1704 int signo = info->si_signo;
1705
Andrew Kaylor93132f52013-05-28 23:04:25 +00001706 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1707
Stephen Wilson84ffe702011-03-30 15:55:52 +00001708 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1709 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1710 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1711 //
1712 // IOW, user generated signals never generate what we consider to be a
1713 // "crash".
1714 //
1715 // Similarly, ACK signals generated by this monitor.
1716 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1717 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001718 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001719 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001720 __FUNCTION__,
1721 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1722 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1723 info->si_pid);
1724
Stephen Wilson84ffe702011-03-30 15:55:52 +00001725 if (info->si_pid == getpid())
1726 return ProcessMessage::SignalDelivered(pid, signo);
1727 else
1728 return ProcessMessage::Signal(pid, signo);
1729 }
1730
Andrew Kaylor93132f52013-05-28 23:04:25 +00001731 if (log)
1732 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1733
Stephen Wilson84ffe702011-03-30 15:55:52 +00001734 if (signo == SIGSEGV) {
1735 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1736 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1737 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1738 }
1739
1740 if (signo == SIGILL) {
1741 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1742 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1743 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1744 }
1745
1746 if (signo == SIGFPE) {
1747 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1748 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1749 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1750 }
1751
1752 if (signo == SIGBUS) {
1753 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1754 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1755 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1756 }
1757
1758 // Everything else is "normal" and does not require any special action on
1759 // our part.
1760 return ProcessMessage::Signal(pid, signo);
1761}
1762
Andrew Kaylord4d54992013-09-17 00:30:24 +00001763// On Linux, when a new thread is created, we receive to notifications,
1764// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1765// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1766// the new child thread indicating that it has is stopped because we attached.
1767// We have no guarantee of the order in which these arrive, but we need both
1768// before we are ready to proceed. We currently keep a list of threads which
1769// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1770// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1771// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1772
1773bool
1774ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1775{
1776 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1777 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001778 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001779
1780 // Wait for the thread to stop
1781 while (true)
1782 {
1783 int status = -1;
1784 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001785 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001786 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001787 if (status == -1)
1788 {
1789 // If we got interrupted by a signal (in our process, not the
1790 // inferior) try again.
1791 if (errno == EINTR)
1792 continue;
1793 else
1794 {
1795 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001796 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001797 return false; // This is bad, but there's nothing we can do.
1798 }
1799 }
1800
1801 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001802 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001803
Todd Fiala9be50492014-07-01 16:30:53 +00001804 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001805
1806 siginfo_t info;
1807 int ptrace_err;
1808 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1809 {
1810 if (log)
1811 {
1812 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1813 }
1814 return false;
1815 }
1816
1817 // If this is a thread exit, we won't get any more information.
1818 if (WIFEXITED(status))
1819 {
1820 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001821 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001822 return true;
1823 continue;
1824 }
1825
1826 assert(info.si_code == SI_USER);
1827 assert(WSTOPSIG(status) == SIGSTOP);
1828
1829 if (log)
1830 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1831 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1832 return true;
1833 }
1834 return false;
1835}
1836
Andrew Kaylor93132f52013-05-28 23:04:25 +00001837bool
1838ProcessMonitor::StopThread(lldb::tid_t tid)
1839{
1840 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1841
1842 // FIXME: Try to use tgkill or tkill
1843 int ret = tgkill(m_pid, tid, SIGSTOP);
1844 if (log)
1845 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1846
1847 // This can happen if a thread exited while we were trying to stop it. That's OK.
1848 // We'll get the signal for that later.
1849 if (ret < 0)
1850 return false;
1851
1852 // Wait for the thread to stop
1853 while (true)
1854 {
1855 int status = -1;
1856 if (log)
1857 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001858 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001859 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001860 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1861 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001862
Todd Fiala9be50492014-07-01 16:30:53 +00001863 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001864 {
1865 // If we got interrupted by a signal (in our process, not the
1866 // inferior) try again.
1867 if (errno == EINTR)
1868 continue;
1869 else
1870 return false; // This is bad, but there's nothing we can do.
1871 }
1872
1873 // If this is a thread exit, we won't get any more information.
1874 if (WIFEXITED(status))
1875 {
1876 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001877 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001878 return true;
1879 continue;
1880 }
1881
1882 siginfo_t info;
1883 int ptrace_err;
1884 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1885 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001886 // another signal causing a StopAllThreads may have been received
1887 // before wait_pid's group-stop was processed, handle it now
1888 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001889 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001890 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001891
Todd Fiala1b0539c2014-01-27 17:03:57 +00001892 if (log)
1893 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1894 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1895 if (!Resume(wait_pid, SIGSTOP)) {
1896 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1897 }
1898 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001899 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001900
1901 if (log)
1902 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001903 return false;
1904 }
1905
1906 // Handle events from other threads
1907 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001908 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1909 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001910
1911 ProcessMessage message;
1912 if (info.si_signo == SIGTRAP)
1913 message = MonitorSIGTRAP(this, &info, wait_pid);
1914 else
1915 message = MonitorSignal(this, &info, wait_pid);
1916
1917 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1918
1919 // When a new thread is created, we may get a SIGSTOP for the new thread
1920 // just before we get the SIGTRAP that we use to add the thread to our
1921 // process thread list. We don't need to worry about that signal here.
1922 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1923
1924 if (!thread)
1925 {
1926 m_process->SendMessage(message);
1927 continue;
1928 }
1929
1930 switch (message.GetKind())
1931 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00001932 case ProcessMessage::eExecMessage:
1933 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00001934 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001935 case ProcessMessage::eInvalidMessage:
1936 break;
1937
1938 // These need special handling because we don't want to send a
1939 // resume even if we already sent a SIGSTOP to this thread. In
1940 // this case the resume will cause the thread to disappear. It is
1941 // unlikely that we'll ever get eExitMessage here, but the same
1942 // reasoning applies.
1943 case ProcessMessage::eLimboMessage:
1944 case ProcessMessage::eExitMessage:
1945 if (log)
1946 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1947 // SendMessage will set the thread state as needed.
1948 m_process->SendMessage(message);
1949 // If this is the thread we're waiting for, stop waiting. Even
1950 // though this wasn't the signal we expected, it's the last
1951 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00001952 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001953 return true;
1954 break;
1955
Matt Kopecb2910442013-07-09 15:09:45 +00001956 case ProcessMessage::eSignalMessage:
1957 if (log)
1958 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1959 if (WSTOPSIG(status) == SIGSTOP)
1960 {
1961 m_process->AddThreadForInitialStopIfNeeded(tid);
1962 thread->SetState(lldb::eStateStopped);
1963 }
1964 else
1965 {
1966 m_process->SendMessage(message);
1967 // This isn't the stop we were expecting, but the thread is
1968 // stopped. SendMessage will handle processing of this event,
1969 // but we need to resume here to get the stop we are waiting
1970 // for (otherwise the thread will stop again immediately when
1971 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001972 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00001973 Resume(wait_pid, eResumeSignalNone);
1974 }
1975 break;
1976
Andrew Kaylor93132f52013-05-28 23:04:25 +00001977 case ProcessMessage::eSignalDeliveredMessage:
1978 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00001979 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
1980 WIFSTOPPED(status) &&
1981 WSTOPSIG(status) == SIGSTOP &&
1982 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001983 {
1984 if (log)
1985 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1986 thread->SetState(lldb::eStateStopped);
1987 return true;
1988 }
1989 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001990 case ProcessMessage::eBreakpointMessage:
1991 case ProcessMessage::eTraceMessage:
1992 case ProcessMessage::eWatchpointMessage:
1993 case ProcessMessage::eCrashMessage:
1994 case ProcessMessage::eNewThreadMessage:
1995 if (log)
1996 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1997 // SendMessage will set the thread state as needed.
1998 m_process->SendMessage(message);
1999 // This isn't the stop we were expecting, but the thread is
2000 // stopped. SendMessage will handle processing of this event,
2001 // but we need to resume here to get the stop we are waiting
2002 // for (otherwise the thread will stop again immediately when
2003 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002004 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002005 Resume(wait_pid, eResumeSignalNone);
2006 break;
2007 }
2008 }
2009 return false;
2010}
2011
Stephen Wilson84ffe702011-03-30 15:55:52 +00002012ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002013ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002014{
2015 ProcessMessage::CrashReason reason;
2016 assert(info->si_signo == SIGSEGV);
2017
2018 reason = ProcessMessage::eInvalidCrashReason;
2019
Greg Clayton542e4072012-09-07 17:49:29 +00002020 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002021 {
2022 default:
2023 assert(false && "unexpected si_code for SIGSEGV");
2024 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00002025 case SI_KERNEL:
2026 // Linux will occasionally send spurious SI_KERNEL codes.
2027 // (this is poorly documented in sigaction)
2028 // One way to get this is via unaligned SIMD loads.
2029 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
2030 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002031 case SEGV_MAPERR:
2032 reason = ProcessMessage::eInvalidAddress;
2033 break;
2034 case SEGV_ACCERR:
2035 reason = ProcessMessage::ePrivilegedAddress;
2036 break;
2037 }
Greg Clayton542e4072012-09-07 17:49:29 +00002038
Stephen Wilson84ffe702011-03-30 15:55:52 +00002039 return reason;
2040}
2041
2042ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002043ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002044{
2045 ProcessMessage::CrashReason reason;
2046 assert(info->si_signo == SIGILL);
2047
2048 reason = ProcessMessage::eInvalidCrashReason;
2049
2050 switch (info->si_code)
2051 {
2052 default:
2053 assert(false && "unexpected si_code for SIGILL");
2054 break;
2055 case ILL_ILLOPC:
2056 reason = ProcessMessage::eIllegalOpcode;
2057 break;
2058 case ILL_ILLOPN:
2059 reason = ProcessMessage::eIllegalOperand;
2060 break;
2061 case ILL_ILLADR:
2062 reason = ProcessMessage::eIllegalAddressingMode;
2063 break;
2064 case ILL_ILLTRP:
2065 reason = ProcessMessage::eIllegalTrap;
2066 break;
2067 case ILL_PRVOPC:
2068 reason = ProcessMessage::ePrivilegedOpcode;
2069 break;
2070 case ILL_PRVREG:
2071 reason = ProcessMessage::ePrivilegedRegister;
2072 break;
2073 case ILL_COPROC:
2074 reason = ProcessMessage::eCoprocessorError;
2075 break;
2076 case ILL_BADSTK:
2077 reason = ProcessMessage::eInternalStackError;
2078 break;
2079 }
2080
2081 return reason;
2082}
2083
2084ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002085ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002086{
2087 ProcessMessage::CrashReason reason;
2088 assert(info->si_signo == SIGFPE);
2089
2090 reason = ProcessMessage::eInvalidCrashReason;
2091
2092 switch (info->si_code)
2093 {
2094 default:
2095 assert(false && "unexpected si_code for SIGFPE");
2096 break;
2097 case FPE_INTDIV:
2098 reason = ProcessMessage::eIntegerDivideByZero;
2099 break;
2100 case FPE_INTOVF:
2101 reason = ProcessMessage::eIntegerOverflow;
2102 break;
2103 case FPE_FLTDIV:
2104 reason = ProcessMessage::eFloatDivideByZero;
2105 break;
2106 case FPE_FLTOVF:
2107 reason = ProcessMessage::eFloatOverflow;
2108 break;
2109 case FPE_FLTUND:
2110 reason = ProcessMessage::eFloatUnderflow;
2111 break;
2112 case FPE_FLTRES:
2113 reason = ProcessMessage::eFloatInexactResult;
2114 break;
2115 case FPE_FLTINV:
2116 reason = ProcessMessage::eFloatInvalidOperation;
2117 break;
2118 case FPE_FLTSUB:
2119 reason = ProcessMessage::eFloatSubscriptRange;
2120 break;
2121 }
2122
2123 return reason;
2124}
2125
2126ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002127ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002128{
2129 ProcessMessage::CrashReason reason;
2130 assert(info->si_signo == SIGBUS);
2131
2132 reason = ProcessMessage::eInvalidCrashReason;
2133
2134 switch (info->si_code)
2135 {
2136 default:
2137 assert(false && "unexpected si_code for SIGBUS");
2138 break;
2139 case BUS_ADRALN:
2140 reason = ProcessMessage::eIllegalAlignment;
2141 break;
2142 case BUS_ADRERR:
2143 reason = ProcessMessage::eIllegalAddress;
2144 break;
2145 case BUS_OBJERR:
2146 reason = ProcessMessage::eHardwareError;
2147 break;
2148 }
2149
2150 return reason;
2151}
2152
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002153void
Johnny Chen25e68e32011-06-14 19:19:50 +00002154ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002155{
Stephen Wilson570243b2011-01-19 01:37:06 +00002156 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002157
Stephen Wilson570243b2011-01-19 01:37:06 +00002158 // We are finised with the arguments and are ready to go. Sync with the
2159 // parent thread and start serving operations on the inferior.
2160 sem_post(&args->m_semaphore);
2161
Michael Sartain704bf892013-10-09 01:28:57 +00002162 for(;;)
2163 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002164 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002165 if (sem_wait(&monitor->m_operation_pending))
2166 {
2167 if (errno == EINTR)
2168 continue;
2169 assert(false && "Unexpected errno from sem_wait");
2170 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002171
Daniel Malea1efb4182013-09-16 23:12:18 +00002172 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002173
Daniel Malea1efb4182013-09-16 23:12:18 +00002174 // notify calling thread that operation is complete
2175 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002176 }
2177}
2178
2179void
2180ProcessMonitor::DoOperation(Operation *op)
2181{
Daniel Malea1efb4182013-09-16 23:12:18 +00002182 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002183
Daniel Malea1efb4182013-09-16 23:12:18 +00002184 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002185
Daniel Malea1efb4182013-09-16 23:12:18 +00002186 // notify operation thread that an operation is ready to be processed
2187 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002188
Daniel Malea1efb4182013-09-16 23:12:18 +00002189 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002190 while (sem_wait(&m_operation_done))
2191 {
2192 if (errno == EINTR)
2193 continue;
2194 assert(false && "Unexpected errno from sem_wait");
2195 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002196}
2197
2198size_t
2199ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2200 Error &error)
2201{
2202 size_t result;
2203 ReadOperation op(vm_addr, buf, size, error, result);
2204 DoOperation(&op);
2205 return result;
2206}
2207
2208size_t
2209ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2210 lldb_private::Error &error)
2211{
2212 size_t result;
2213 WriteOperation op(vm_addr, buf, size, error, result);
2214 DoOperation(&op);
2215 return result;
2216}
2217
2218bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002219ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002220 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002221{
2222 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002223 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002224 DoOperation(&op);
2225 return result;
2226}
2227
2228bool
Matt Kopec7de48462013-03-06 17:20:48 +00002229ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002230 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002231{
2232 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002233 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002234 DoOperation(&op);
2235 return result;
2236}
2237
2238bool
Matt Kopec7de48462013-03-06 17:20:48 +00002239ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002240{
2241 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002242 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002243 DoOperation(&op);
2244 return result;
2245}
2246
2247bool
Matt Kopec7de48462013-03-06 17:20:48 +00002248ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002249{
2250 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002251 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002252 DoOperation(&op);
2253 return result;
2254}
2255
2256bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002257ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2258{
2259 bool result;
2260 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2261 DoOperation(&op);
2262 return result;
2263}
2264
2265bool
Matt Kopec7de48462013-03-06 17:20:48 +00002266ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002267{
2268 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002269 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002270 DoOperation(&op);
2271 return result;
2272}
2273
2274bool
Matt Kopec7de48462013-03-06 17:20:48 +00002275ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002276{
2277 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002278 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002279 DoOperation(&op);
2280 return result;
2281}
2282
2283bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002284ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2285{
2286 bool result;
2287 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2288 DoOperation(&op);
2289 return result;
2290}
2291
2292bool
Richard Mitton0a558352013-10-17 21:14:00 +00002293ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2294{
2295 bool result;
2296 ReadThreadPointerOperation op(tid, &value, result);
2297 DoOperation(&op);
2298 return result;
2299}
2300
2301bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002302ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002303{
2304 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002305 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2306
2307 if (log)
2308 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2309 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002310 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002311 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002312 if (log)
2313 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002314 return result;
2315}
2316
2317bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002318ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002319{
2320 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002321 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002322 DoOperation(&op);
2323 return result;
2324}
2325
2326bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002327ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002328{
Ed Maste4e0999b2014-04-01 18:14:06 +00002329 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002330}
2331
2332bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002333ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002334{
2335 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002336 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002337 DoOperation(&op);
2338 return result;
2339}
2340
2341bool
2342ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2343{
2344 bool result;
2345 EventMessageOperation op(tid, message, result);
2346 DoOperation(&op);
2347 return result;
2348}
2349
Greg Clayton743ecf42012-10-16 20:20:18 +00002350lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002351ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002352{
Greg Clayton28041352011-11-29 20:50:10 +00002353 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002354 if (tid != LLDB_INVALID_THREAD_ID)
2355 {
2356 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002357 DoOperation(&op);
2358 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002359 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002360}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002361
2362bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002363ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2364{
Peter Collingbourne62343202011-06-14 03:55:54 +00002365 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002366
2367 if (target_fd == -1)
2368 return false;
2369
Peter Collingbourne62343202011-06-14 03:55:54 +00002370 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002371}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002372
2373void
2374ProcessMonitor::StopMonitoringChildProcess()
2375{
2376 lldb::thread_result_t thread_result;
2377
Stephen Wilsond4182f42011-02-09 20:10:35 +00002378 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002379 {
2380 Host::ThreadCancel(m_monitor_thread, NULL);
2381 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2382 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2383 }
2384}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002385
2386void
2387ProcessMonitor::StopMonitor()
2388{
2389 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002390 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002391 sem_destroy(&m_operation_pending);
2392 sem_destroy(&m_operation_done);
2393
Andrew Kaylor5e268992013-09-14 00:17:31 +00002394 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2395 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2396 // the descriptor to a ConnectionFileDescriptor object. Consequently
2397 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002398}
2399
2400void
Greg Clayton743ecf42012-10-16 20:20:18 +00002401ProcessMonitor::StopOpThread()
2402{
2403 lldb::thread_result_t result;
2404
2405 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2406 return;
2407
2408 Host::ThreadCancel(m_operation_thread, NULL);
2409 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002410 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002411}