blob: 048a517d4f1231235904707e8cd4c9bca0b97bb4 [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 Fiala0bce1b62014-08-17 00:10:50 +000018#include <sys/personality.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019#include <sys/ptrace.h>
Todd Fiala6ac1be42014-08-21 16:34:03 +000020#include <sys/uio.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000021#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000022#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000024#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025#include <sys/wait.h>
26
Todd Fiala6ac1be42014-08-21 16:34:03 +000027#if defined (__arm64__) || defined (__aarch64__)
28// NT_PRSTATUS and NT_FPREGSET definition
29#include <elf.h>
30#endif
31
Stephen Wilsone6f9f662010-07-24 02:19:04 +000032// C++ Includes
33// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000034#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000036#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "lldb/Core/Scalar.h"
38#include "lldb/Host/Host.h"
39#include "lldb/Target/Thread.h"
40#include "lldb/Target/RegisterContext.h"
41#include "lldb/Utility/PseudoTerminal.h"
42
Johnny Chen30213ff2012-01-05 19:17:38 +000043#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000044#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000045#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000046#include "ProcessMonitor.h"
47
Greg Clayton386ff182011-11-05 01:09:16 +000048#define DEBUG_PTRACE_MAXBYTES 20
49
Matt Kopec58c0b962013-03-20 20:34:35 +000050// Support ptrace extensions even when compiled without required kernel support
51#ifndef PTRACE_GETREGSET
52 #define PTRACE_GETREGSET 0x4204
53#endif
54#ifndef PTRACE_SETREGSET
55 #define PTRACE_SETREGSET 0x4205
56#endif
Richard Mitton0a558352013-10-17 21:14:00 +000057#ifndef PTRACE_GET_THREAD_AREA
58 #define PTRACE_GET_THREAD_AREA 25
59#endif
60#ifndef PTRACE_ARCH_PRCTL
61 #define PTRACE_ARCH_PRCTL 30
62#endif
63#ifndef ARCH_GET_FS
64 #define ARCH_SET_GS 0x1001
65 #define ARCH_SET_FS 0x1002
66 #define ARCH_GET_FS 0x1003
67 #define ARCH_GET_GS 0x1004
68#endif
69
Todd Fiala0bce1b62014-08-17 00:10:50 +000070#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000071
Matt Kopece9ea0da2013-05-07 19:29:28 +000072// Support hardware breakpoints in case it has not been defined
73#ifndef TRAP_HWBKPT
74 #define TRAP_HWBKPT 4
75#endif
76
Andrew Kaylor93132f52013-05-28 23:04:25 +000077// Try to define a macro to encapsulate the tgkill syscall
78// fall back on kill() if tgkill isn't available
79#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
80
Stephen Wilsone6f9f662010-07-24 02:19:04 +000081using namespace lldb_private;
82
Johnny Chen0d5f2d42011-10-18 18:09:30 +000083// FIXME: this code is host-dependent with respect to types and
84// endianness and needs to be fixed. For example, lldb::addr_t is
85// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
86// 32-bit pointer arguments. This code uses casts to work around the
87// problem.
88
89// We disable the tracing of ptrace calls for integration builds to
90// avoid the additional indirection and checks.
91#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
92
Greg Clayton386ff182011-11-05 01:09:16 +000093static void
94DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
95{
96 uint8_t *ptr = (uint8_t *)bytes;
97 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
98 for(uint32_t i=0; i<loop_count; i++)
99 {
100 s.Printf ("[%x]", *ptr);
101 ptr++;
102 }
103}
104
Matt Kopec58c0b962013-03-20 20:34:35 +0000105static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000106{
107 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000108 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000109 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000110
111 if (verbose_log)
112 {
113 switch(req)
114 {
115 case PTRACE_POKETEXT:
116 {
117 DisplayBytes(buf, &data, 8);
118 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
119 break;
120 }
Greg Clayton542e4072012-09-07 17:49:29 +0000121 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000122 {
123 DisplayBytes(buf, &data, 8);
124 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
125 break;
126 }
Greg Clayton542e4072012-09-07 17:49:29 +0000127 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000128 {
129 DisplayBytes(buf, &data, 8);
130 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
131 break;
132 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000133#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000134 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000135 {
Matt Kopec7de48462013-03-06 17:20:48 +0000136 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000137 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
138 break;
139 }
140 case PTRACE_SETFPREGS:
141 {
Matt Kopec7de48462013-03-06 17:20:48 +0000142 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000143 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
144 break;
145 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000146#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000147 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000148 {
149 DisplayBytes(buf, data, sizeof(siginfo_t));
150 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
151 break;
152 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000153 case PTRACE_SETREGSET:
154 {
155 // Extract iov_base from data, which is a pointer to the struct IOVEC
156 DisplayBytes(buf, *(void **)data, data_size);
157 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
158 break;
159 }
Greg Clayton386ff182011-11-05 01:09:16 +0000160 default:
161 {
162 }
163 }
164 }
165}
166
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000167// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000168// 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 +0000169extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000170PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000171 const char* reqName, const char* file, int line)
172{
Greg Clayton386ff182011-11-05 01:09:16 +0000173 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000174
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000175 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000176
Matt Kopec7de48462013-03-06 17:20:48 +0000177 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000178
179 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000180 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000181 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000182 else
Todd Fiala4507f062014-02-27 20:46:12 +0000183 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000184
Ed Mastec099c952014-02-24 14:07:45 +0000185 if (log)
186 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
187 reqName, pid, addr, data, data_size, result, file, line);
188
Matt Kopec7de48462013-03-06 17:20:48 +0000189 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000190
Matt Kopec7de48462013-03-06 17:20:48 +0000191 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000192 {
193 const char* str;
194 switch (errno)
195 {
196 case ESRCH: str = "ESRCH"; break;
197 case EINVAL: str = "EINVAL"; break;
198 case EBUSY: str = "EBUSY"; break;
199 case EPERM: str = "EPERM"; break;
200 default: str = "<unknown>";
201 }
202 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
203 }
204
205 return result;
206}
207
Matt Kopec7de48462013-03-06 17:20:48 +0000208// Wrapper for ptrace when logging is not required.
209// Sets errno to 0 prior to calling ptrace.
210extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000211PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000212{
Matt Kopec58c0b962013-03-20 20:34:35 +0000213 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000214 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000215 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
216 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
217 else
218 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000219 return result;
220}
221
222#define PTRACE(req, pid, addr, data, data_size) \
223 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000224#else
Matt Kopec7de48462013-03-06 17:20:48 +0000225 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000226#endif
227
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000228//------------------------------------------------------------------------------
229// Static implementations of ProcessMonitor::ReadMemory and
230// ProcessMonitor::WriteMemory. This enables mutual recursion between these
231// functions without needed to go thru the thread funnel.
232
233static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000234DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
236{
Greg Clayton542e4072012-09-07 17:49:29 +0000237 // ptrace word size is determined by the host, not the child
238 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000239 unsigned char *dst = static_cast<unsigned char*>(buf);
240 size_t bytes_read;
241 size_t remainder;
242 long data;
243
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000244 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000245 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000246 ProcessPOSIXLog::IncNestLevel();
247 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000248 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000249 pid, word_size, (void*)vm_addr, buf, size);
250
251 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000252 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
253 {
254 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000255 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
256 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 {
258 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000259 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000260 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000261 return bytes_read;
262 }
263
264 remainder = size - bytes_read;
265 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000266
267 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000268 for (unsigned i = 0; i < remainder; ++i)
269 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000270
Johnny Chen30213ff2012-01-05 19:17:38 +0000271 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
272 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
273 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
274 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000275 {
276 uintptr_t print_dst = 0;
277 // Format bytes from data by moving into print_dst for log output
278 for (unsigned i = 0; i < remainder; ++i)
279 print_dst |= (((data >> i*8) & 0xFF) << i*8);
280 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
281 (void*)vm_addr, print_dst, (unsigned long)data);
282 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000283
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000284 vm_addr += word_size;
285 dst += word_size;
286 }
287
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000288 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000289 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290 return bytes_read;
291}
292
293static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000294DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000295 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
296{
Greg Clayton542e4072012-09-07 17:49:29 +0000297 // ptrace word size is determined by the host, not the child
298 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000299 const unsigned char *src = static_cast<const unsigned char*>(buf);
300 size_t bytes_written = 0;
301 size_t remainder;
302
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000303 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000305 ProcessPOSIXLog::IncNestLevel();
306 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000307 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000308 pid, word_size, (void*)vm_addr, buf, size);
309
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000310 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
311 {
312 remainder = size - bytes_written;
313 remainder = remainder > word_size ? word_size : remainder;
314
315 if (remainder == word_size)
316 {
317 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000318 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000319 for (unsigned i = 0; i < word_size; ++i)
320 data |= (unsigned long)src[i] << i*8;
321
Johnny Chen30213ff2012-01-05 19:17:38 +0000322 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
323 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
324 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
325 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000326 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
327 (void*)vm_addr, *(unsigned long*)src, data);
328
Matt Kopec7de48462013-03-06 17:20:48 +0000329 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000330 {
331 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000332 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000333 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000334 return bytes_written;
335 }
336 }
337 else
338 {
339 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000340 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 {
343 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000344 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000347
348 memcpy(buff, src, remainder);
349
Greg Clayton542e4072012-09-07 17:49:29 +0000350 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000351 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 {
353 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000354 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000355 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000356 }
357
Johnny Chen30213ff2012-01-05 19:17:38 +0000358 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
359 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
360 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
361 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000362 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
363 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000364 }
365
366 vm_addr += word_size;
367 src += word_size;
368 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000369 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000370 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000371 return bytes_written;
372}
373
Stephen Wilson26977162011-03-23 02:14:42 +0000374// Simple helper function to ensure flags are enabled on the given file
375// descriptor.
376static bool
377EnsureFDFlags(int fd, int flags, Error &error)
378{
379 int status;
380
381 if ((status = fcntl(fd, F_GETFL)) == -1)
382 {
383 error.SetErrorToErrno();
384 return false;
385 }
386
387 if (fcntl(fd, F_SETFL, status | flags) == -1)
388 {
389 error.SetErrorToErrno();
390 return false;
391 }
392
393 return true;
394}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000395
396//------------------------------------------------------------------------------
397/// @class Operation
398/// @brief Represents a ProcessMonitor operation.
399///
400/// Under Linux, it is not possible to ptrace() from any other thread but the
401/// one that spawned or attached to the process from the start. Therefore, when
402/// a ProcessMonitor is asked to deliver or change the state of an inferior
403/// process the operation must be "funneled" to a specific thread to perform the
404/// task. The Operation class provides an abstract base for all services the
405/// ProcessMonitor must perform via the single virtual function Execute, thus
406/// encapsulating the code that needs to run in the privileged context.
407class Operation
408{
409public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000410 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000411 virtual void Execute(ProcessMonitor *monitor) = 0;
412};
413
414//------------------------------------------------------------------------------
415/// @class ReadOperation
416/// @brief Implements ProcessMonitor::ReadMemory.
417class ReadOperation : public Operation
418{
419public:
420 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
421 Error &error, size_t &result)
422 : m_addr(addr), m_buff(buff), m_size(size),
423 m_error(error), m_result(result)
424 { }
425
426 void Execute(ProcessMonitor *monitor);
427
428private:
429 lldb::addr_t m_addr;
430 void *m_buff;
431 size_t m_size;
432 Error &m_error;
433 size_t &m_result;
434};
435
436void
437ReadOperation::Execute(ProcessMonitor *monitor)
438{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000439 lldb::pid_t pid = monitor->GetPID();
440
Greg Clayton542e4072012-09-07 17:49:29 +0000441 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000442}
443
444//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000445/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000446/// @brief Implements ProcessMonitor::WriteMemory.
447class WriteOperation : public Operation
448{
449public:
450 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
451 Error &error, size_t &result)
452 : m_addr(addr), m_buff(buff), m_size(size),
453 m_error(error), m_result(result)
454 { }
455
456 void Execute(ProcessMonitor *monitor);
457
458private:
459 lldb::addr_t m_addr;
460 const void *m_buff;
461 size_t m_size;
462 Error &m_error;
463 size_t &m_result;
464};
465
466void
467WriteOperation::Execute(ProcessMonitor *monitor)
468{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000469 lldb::pid_t pid = monitor->GetPID();
470
Greg Clayton542e4072012-09-07 17:49:29 +0000471 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000472}
473
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000474
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000475//------------------------------------------------------------------------------
476/// @class ReadRegOperation
477/// @brief Implements ProcessMonitor::ReadRegisterValue.
478class ReadRegOperation : public Operation
479{
480public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000481 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000482 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000483 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000484 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000485 { }
486
487 void Execute(ProcessMonitor *monitor);
488
489private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000490 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000491 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000492 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000493 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000494 bool &m_result;
495};
496
497void
498ReadRegOperation::Execute(ProcessMonitor *monitor)
499{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000500 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000501
502 // Set errno to zero so that we can detect a failed peek.
503 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000504 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
505 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000506 m_result = false;
507 else
508 {
509 m_value = data;
510 m_result = true;
511 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000512 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000513 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000514 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000515}
516
517//------------------------------------------------------------------------------
518/// @class WriteRegOperation
519/// @brief Implements ProcessMonitor::WriteRegisterValue.
520class WriteRegOperation : public Operation
521{
522public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000523 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000524 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000525 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000526 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000527 { }
528
529 void Execute(ProcessMonitor *monitor);
530
531private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000532 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000533 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000534 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000535 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000536 bool &m_result;
537};
538
539void
540WriteRegOperation::Execute(ProcessMonitor *monitor)
541{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000542 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000543 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000544
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000545 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000546
547 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000548 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000549 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000550 m_result = false;
551 else
552 m_result = true;
553}
554
555//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000556/// @class ReadGPROperation
557/// @brief Implements ProcessMonitor::ReadGPR.
558class ReadGPROperation : public Operation
559{
560public:
Matt Kopec7de48462013-03-06 17:20:48 +0000561 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
562 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000563 { }
564
565 void Execute(ProcessMonitor *monitor);
566
567private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000568 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000569 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000570 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000571 bool &m_result;
572};
573
574void
575ReadGPROperation::Execute(ProcessMonitor *monitor)
576{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000577#if defined (__arm64__) || defined (__aarch64__)
578 int regset = NT_PRSTATUS;
579 struct iovec ioVec;
580
581 ioVec.iov_base = m_buf;
582 ioVec.iov_len = m_buf_size;
583 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000584 m_result = false;
585 else
586 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000587#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000588 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
589 m_result = false;
590 else
591 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000592#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000593}
594
595//------------------------------------------------------------------------------
596/// @class ReadFPROperation
597/// @brief Implements ProcessMonitor::ReadFPR.
598class ReadFPROperation : public Operation
599{
600public:
Matt Kopec7de48462013-03-06 17:20:48 +0000601 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
602 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000603 { }
604
605 void Execute(ProcessMonitor *monitor);
606
607private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000608 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000609 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000610 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000611 bool &m_result;
612};
613
614void
615ReadFPROperation::Execute(ProcessMonitor *monitor)
616{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000617#if defined (__arm64__) || defined (__aarch64__)
618 int regset = NT_FPREGSET;
619 struct iovec ioVec;
620
621 ioVec.iov_base = m_buf;
622 ioVec.iov_len = m_buf_size;
623 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000624 m_result = false;
625 else
626 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000627#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000628 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
629 m_result = false;
630 else
631 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000632#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000633}
634
635//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000636/// @class ReadRegisterSetOperation
637/// @brief Implements ProcessMonitor::ReadRegisterSet.
638class ReadRegisterSetOperation : public Operation
639{
640public:
641 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
642 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
643 { }
644
645 void Execute(ProcessMonitor *monitor);
646
647private:
648 lldb::tid_t m_tid;
649 void *m_buf;
650 size_t m_buf_size;
651 const unsigned int m_regset;
652 bool &m_result;
653};
654
655void
656ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
657{
658 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
659 m_result = false;
660 else
661 m_result = true;
662}
663
664//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000665/// @class WriteGPROperation
666/// @brief Implements ProcessMonitor::WriteGPR.
667class WriteGPROperation : public Operation
668{
669public:
Matt Kopec7de48462013-03-06 17:20:48 +0000670 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
671 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000672 { }
673
674 void Execute(ProcessMonitor *monitor);
675
676private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000677 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000678 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000679 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000680 bool &m_result;
681};
682
683void
684WriteGPROperation::Execute(ProcessMonitor *monitor)
685{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000686#if defined (__arm64__) || defined (__aarch64__)
687 int regset = NT_PRSTATUS;
688 struct iovec ioVec;
689
690 ioVec.iov_base = m_buf;
691 ioVec.iov_len = m_buf_size;
692 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000693 m_result = false;
694 else
695 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000696#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000697 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
698 m_result = false;
699 else
700 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000701#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000702}
703
704//------------------------------------------------------------------------------
705/// @class WriteFPROperation
706/// @brief Implements ProcessMonitor::WriteFPR.
707class WriteFPROperation : public Operation
708{
709public:
Matt Kopec7de48462013-03-06 17:20:48 +0000710 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
711 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000712 { }
713
714 void Execute(ProcessMonitor *monitor);
715
716private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000717 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000718 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000719 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000720 bool &m_result;
721};
722
723void
724WriteFPROperation::Execute(ProcessMonitor *monitor)
725{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000726#if defined (__arm64__) || defined (__aarch64__)
727 int regset = NT_FPREGSET;
728 struct iovec ioVec;
729
730 ioVec.iov_base = m_buf;
731 ioVec.iov_len = m_buf_size;
732 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000733 m_result = false;
734 else
735 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000736#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000737 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
738 m_result = false;
739 else
740 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000741#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000742}
743
744//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000745/// @class WriteRegisterSetOperation
746/// @brief Implements ProcessMonitor::WriteRegisterSet.
747class WriteRegisterSetOperation : public Operation
748{
749public:
750 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
751 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
752 { }
753
754 void Execute(ProcessMonitor *monitor);
755
756private:
757 lldb::tid_t m_tid;
758 void *m_buf;
759 size_t m_buf_size;
760 const unsigned int m_regset;
761 bool &m_result;
762};
763
764void
765WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
766{
767 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
768 m_result = false;
769 else
770 m_result = true;
771}
772
773//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000774/// @class ReadThreadPointerOperation
775/// @brief Implements ProcessMonitor::ReadThreadPointer.
776class ReadThreadPointerOperation : public Operation
777{
778public:
779 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
780 : m_tid(tid), m_addr(addr), m_result(result)
781 { }
782
783 void Execute(ProcessMonitor *monitor);
784
785private:
786 lldb::tid_t m_tid;
787 lldb::addr_t *m_addr;
788 bool &m_result;
789};
790
791void
792ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
793{
794 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
795 if (log)
796 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
797
798 // The process for getting the thread area on Linux is
799 // somewhat... obscure. There's several different ways depending on
800 // what arch you're on, and what kernel version you have.
801
802 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
803 switch(arch.GetMachine())
804 {
Todd Fiala720cd3f2014-06-16 14:49:28 +0000805#if defined(__i386__) || defined(__x86_64__)
806 // Note that struct user below has a field named i387 which is x86-specific.
807 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000808 case llvm::Triple::x86:
809 {
810 // Find the GS register location for our host architecture.
811 size_t gs_user_offset = offsetof(struct user, regs);
812#ifdef __x86_64__
813 gs_user_offset += offsetof(struct user_regs_struct, gs);
814#endif
815#ifdef __i386__
816 gs_user_offset += offsetof(struct user_regs_struct, xgs);
817#endif
818
819 // Read the GS register value to get the selector.
820 errno = 0;
821 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
822 if (errno)
823 {
824 m_result = false;
825 break;
826 }
827
828 // Read the LDT base for that selector.
829 uint32_t tmp[4];
830 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
831 *m_addr = tmp[1];
832 break;
833 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000834#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000835 case llvm::Triple::x86_64:
836 // Read the FS register base.
837 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
838 break;
839 default:
840 m_result = false;
841 break;
842 }
843}
844
845//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000846/// @class ResumeOperation
847/// @brief Implements ProcessMonitor::Resume.
848class ResumeOperation : public Operation
849{
850public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000851 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
852 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000853
854 void Execute(ProcessMonitor *monitor);
855
856private:
857 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000858 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000859 bool &m_result;
860};
861
862void
863ResumeOperation::Execute(ProcessMonitor *monitor)
864{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000865 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000866
867 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
868 data = m_signo;
869
Matt Kopec7de48462013-03-06 17:20:48 +0000870 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000871 {
872 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
873
874 if (log)
875 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000876 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000877 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000878 else
879 m_result = true;
880}
881
882//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000883/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000884/// @brief Implements ProcessMonitor::SingleStep.
885class SingleStepOperation : public Operation
886{
887public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000888 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
889 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000890
891 void Execute(ProcessMonitor *monitor);
892
893private:
894 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000895 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000896 bool &m_result;
897};
898
899void
900SingleStepOperation::Execute(ProcessMonitor *monitor)
901{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000902 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000903
904 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
905 data = m_signo;
906
Matt Kopec7de48462013-03-06 17:20:48 +0000907 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000908 m_result = false;
909 else
910 m_result = true;
911}
912
913//------------------------------------------------------------------------------
914/// @class SiginfoOperation
915/// @brief Implements ProcessMonitor::GetSignalInfo.
916class SiginfoOperation : public Operation
917{
918public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000919 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
920 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000921
922 void Execute(ProcessMonitor *monitor);
923
924private:
925 lldb::tid_t m_tid;
926 void *m_info;
927 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000928 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000929};
930
931void
932SiginfoOperation::Execute(ProcessMonitor *monitor)
933{
Matt Kopec7de48462013-03-06 17:20:48 +0000934 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000935 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000936 m_err = errno;
937 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000938 else
939 m_result = true;
940}
941
942//------------------------------------------------------------------------------
943/// @class EventMessageOperation
944/// @brief Implements ProcessMonitor::GetEventMessage.
945class EventMessageOperation : public Operation
946{
947public:
948 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
949 : m_tid(tid), m_message(message), m_result(result) { }
950
951 void Execute(ProcessMonitor *monitor);
952
953private:
954 lldb::tid_t m_tid;
955 unsigned long *m_message;
956 bool &m_result;
957};
958
959void
960EventMessageOperation::Execute(ProcessMonitor *monitor)
961{
Matt Kopec7de48462013-03-06 17:20:48 +0000962 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000963 m_result = false;
964 else
965 m_result = true;
966}
967
968//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000969/// @class DetachOperation
970/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000971class DetachOperation : public Operation
972{
973public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000974 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000975
976 void Execute(ProcessMonitor *monitor);
977
978private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000979 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000980 Error &m_error;
981};
982
983void
984DetachOperation::Execute(ProcessMonitor *monitor)
985{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000986 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000987 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000988}
989
Johnny Chen25e68e32011-06-14 19:19:50 +0000990ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
991 : m_monitor(monitor)
992{
993 sem_init(&m_semaphore, 0, 0);
994}
995
996ProcessMonitor::OperationArgs::~OperationArgs()
997{
998 sem_destroy(&m_semaphore);
999}
1000
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001001ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1002 lldb_private::Module *module,
1003 char const **argv,
1004 char const **envp,
1005 const char *stdin_path,
1006 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001007 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001008 const char *working_dir,
1009 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001010 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001011 m_module(module),
1012 m_argv(argv),
1013 m_envp(envp),
1014 m_stdin_path(stdin_path),
1015 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001016 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001017 m_working_dir(working_dir),
1018 m_launch_info(launch_info)
1019{
1020}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001021
1022ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001023{ }
1024
1025ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1026 lldb::pid_t pid)
1027 : OperationArgs(monitor), m_pid(pid) { }
1028
1029ProcessMonitor::AttachArgs::~AttachArgs()
1030{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001031
1032//------------------------------------------------------------------------------
1033/// The basic design of the ProcessMonitor is built around two threads.
1034///
1035/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1036/// for changes in the debugee state. When a change is detected a
1037/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1038/// "drives" state changes in the debugger.
1039///
1040/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001041/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001042/// operations such as register reads/writes, stepping, etc. See the comments
1043/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001044ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001045 Module *module,
1046 const char *argv[],
1047 const char *envp[],
1048 const char *stdin_path,
1049 const char *stdout_path,
1050 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001051 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001052 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001053 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001054 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001055 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001056 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001057 m_pid(LLDB_INVALID_PROCESS_ID),
1058 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001059 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001060{
Daniel Malea1efb4182013-09-16 23:12:18 +00001061 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1062 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001063 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001064
Daniel Malea1efb4182013-09-16 23:12:18 +00001065 sem_init(&m_operation_pending, 0, 0);
1066 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001067
Johnny Chen25e68e32011-06-14 19:19:50 +00001068 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001069 if (!error.Success())
1070 return;
1071
1072WAIT_AGAIN:
1073 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001074 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001075 {
1076 if (errno == EINTR)
1077 goto WAIT_AGAIN;
1078 else
1079 {
1080 error.SetErrorToErrno();
1081 return;
1082 }
1083 }
1084
1085 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001086 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001087 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001088 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001089 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001090 return;
1091 }
1092
1093 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001094 m_monitor_thread = Host::StartMonitoringChildProcess(
1095 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001096 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001097 {
1098 error.SetErrorToGenericError();
1099 error.SetErrorString("Process launch failed.");
1100 return;
1101 }
1102}
1103
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001104ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001105 lldb::pid_t pid,
1106 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001107 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001108 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001109 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001110 m_pid(LLDB_INVALID_PROCESS_ID),
1111 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001112 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001113{
Daniel Malea1efb4182013-09-16 23:12:18 +00001114 sem_init(&m_operation_pending, 0, 0);
1115 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001116
Daniel Malea1efb4182013-09-16 23:12:18 +00001117 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001118
1119 StartAttachOpThread(args.get(), error);
1120 if (!error.Success())
1121 return;
1122
1123WAIT_AGAIN:
1124 // Wait for the operation thread to initialize.
1125 if (sem_wait(&args->m_semaphore))
1126 {
1127 if (errno == EINTR)
1128 goto WAIT_AGAIN;
1129 else
1130 {
1131 error.SetErrorToErrno();
1132 return;
1133 }
1134 }
1135
Greg Clayton743ecf42012-10-16 20:20:18 +00001136 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001137 if (!args->m_error.Success())
1138 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001139 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001140 error = args->m_error;
1141 return;
1142 }
1143
1144 // Finally, start monitoring the child process for change in state.
1145 m_monitor_thread = Host::StartMonitoringChildProcess(
1146 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1147 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1148 {
1149 error.SetErrorToGenericError();
1150 error.SetErrorString("Process attach failed.");
1151 return;
1152 }
1153}
1154
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001155ProcessMonitor::~ProcessMonitor()
1156{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001157 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001158}
1159
1160//------------------------------------------------------------------------------
1161// Thread setup and tear down.
1162void
Johnny Chen25e68e32011-06-14 19:19:50 +00001163ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001164{
1165 static const char *g_thread_name = "lldb.process.linux.operation";
1166
Stephen Wilsond4182f42011-02-09 20:10:35 +00001167 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001168 return;
1169
1170 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001171 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001172}
1173
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001174void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001175ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001176{
1177 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1178
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001179 if (!Launch(args)) {
1180 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001181 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001182 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001183
Stephen Wilson570243b2011-01-19 01:37:06 +00001184 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001185 return NULL;
1186}
1187
1188bool
1189ProcessMonitor::Launch(LaunchArgs *args)
1190{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001191 assert (args && "null args");
1192 if (!args)
1193 return false;
1194
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001195 ProcessMonitor *monitor = args->m_monitor;
1196 ProcessLinux &process = monitor->GetProcess();
1197 const char **argv = args->m_argv;
1198 const char **envp = args->m_envp;
1199 const char *stdin_path = args->m_stdin_path;
1200 const char *stdout_path = args->m_stdout_path;
1201 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001202 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001203
1204 lldb_utility::PseudoTerminal terminal;
1205 const size_t err_len = 1024;
1206 char err_str[err_len];
1207 lldb::pid_t pid;
1208
1209 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001210 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001211
Stephen Wilson57740ec2011-01-15 00:12:41 +00001212 // Propagate the environment if one is not supplied.
1213 if (envp == NULL || envp[0] == NULL)
1214 envp = const_cast<const char **>(environ);
1215
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001216 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001217 {
1218 args->m_error.SetErrorToGenericError();
1219 args->m_error.SetErrorString("Process fork failed.");
1220 goto FINISH;
1221 }
1222
Peter Collingbourne6a520222011-06-14 03:55:58 +00001223 // Recognized child exit status codes.
1224 enum {
1225 ePtraceFailed = 1,
1226 eDupStdinFailed,
1227 eDupStdoutFailed,
1228 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001229 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001230 eExecFailed,
1231 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001232 };
1233
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001234 // Child process.
1235 if (pid == 0)
1236 {
1237 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001238 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001239 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001240
1241 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001242 if (setgid(getgid()) != 0)
1243 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001244
1245 // Let us have our own process group.
1246 setpgid(0, 0);
1247
Greg Clayton710dd5a2011-01-08 20:28:42 +00001248 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001249 //
1250 // FIXME: If two or more of the paths are the same we needlessly open
1251 // the same file multiple times.
1252 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001253 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001254 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001255
1256 if (stdout_path != NULL && stdout_path[0])
1257 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001258 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001259
1260 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001261 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001262 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001263
Daniel Malea6217d2a2013-01-08 14:49:22 +00001264 // Change working directory
1265 if (working_dir != NULL && working_dir[0])
1266 if (0 != ::chdir(working_dir))
1267 exit(eChdirFailed);
1268
Todd Fiala0bce1b62014-08-17 00:10:50 +00001269 // Disable ASLR if requested.
1270 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1271 {
1272 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1273 if (old_personality == -1)
1274 {
1275 if (log)
1276 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1277 }
1278 else
1279 {
1280 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1281 if (new_personality == -1)
1282 {
1283 if (log)
1284 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1285
1286 }
1287 else
1288 {
1289 if (log)
1290 log->Printf ("ProcessMonitor::%s disbling ASLR: SUCCESS", __FUNCTION__);
1291
1292 }
1293 }
1294 }
1295
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001296 // Execute. We should never return.
1297 execve(argv[0],
1298 const_cast<char *const *>(argv),
1299 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001300 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301 }
1302
1303 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001304 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001305 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001306 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001307
1308 raw_pid = waitpid(pid, &status, 0);
1309 wpid = static_cast <lldb::pid_t> (raw_pid);
1310 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001311 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001312 args->m_error.SetErrorToErrno();
1313 goto FINISH;
1314 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001315 else if (WIFEXITED(status))
1316 {
1317 // open, dup or execve likely failed for some reason.
1318 args->m_error.SetErrorToGenericError();
1319 switch (WEXITSTATUS(status))
1320 {
Greg Clayton542e4072012-09-07 17:49:29 +00001321 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001322 args->m_error.SetErrorString("Child ptrace failed.");
1323 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001324 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001325 args->m_error.SetErrorString("Child open stdin failed.");
1326 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001327 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001328 args->m_error.SetErrorString("Child open stdout failed.");
1329 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001330 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001331 args->m_error.SetErrorString("Child open stderr failed.");
1332 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001333 case eChdirFailed:
1334 args->m_error.SetErrorString("Child failed to set working directory.");
1335 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001336 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001337 args->m_error.SetErrorString("Child exec failed.");
1338 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001339 case eSetGidFailed:
1340 args->m_error.SetErrorString("Child setgid failed.");
1341 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001342 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001343 args->m_error.SetErrorString("Child returned unknown exit status.");
1344 break;
1345 }
1346 goto FINISH;
1347 }
1348 assert(WIFSTOPPED(status) && wpid == pid &&
1349 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001350
Matt Kopec085d6ce2013-05-31 22:00:07 +00001351 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001352 {
1353 args->m_error.SetErrorToErrno();
1354 goto FINISH;
1355 }
1356
1357 // Release the master terminal descriptor and pass it off to the
1358 // ProcessMonitor instance. Similarly stash the inferior pid.
1359 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1360 monitor->m_pid = pid;
1361
Stephen Wilson26977162011-03-23 02:14:42 +00001362 // Set the terminal fd to be in non blocking mode (it simplifies the
1363 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1364 // descriptor to read from).
1365 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1366 goto FINISH;
1367
Johnny Chen30213ff2012-01-05 19:17:38 +00001368 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001369 // FIXME: should we be letting UpdateThreadList handle this?
1370 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001371 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001372
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001373 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001374 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001375 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001376
Matt Kopecb2910442013-07-09 15:09:45 +00001377 process.AddThreadForInitialStopIfNeeded(pid);
1378
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001379 // Let our process instance know the thread has stopped.
1380 process.SendMessage(ProcessMessage::Trace(pid));
1381
1382FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001383 return args->m_error.Success();
1384}
1385
Johnny Chen25e68e32011-06-14 19:19:50 +00001386void
1387ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1388{
1389 static const char *g_thread_name = "lldb.process.linux.operation";
1390
1391 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1392 return;
1393
1394 m_operation_thread =
1395 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1396}
1397
Johnny Chen25e68e32011-06-14 19:19:50 +00001398void *
1399ProcessMonitor::AttachOpThread(void *arg)
1400{
1401 AttachArgs *args = static_cast<AttachArgs*>(arg);
1402
Greg Clayton743ecf42012-10-16 20:20:18 +00001403 if (!Attach(args)) {
1404 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001405 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001406 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001407
1408 ServeOperation(args);
1409 return NULL;
1410}
1411
1412bool
1413ProcessMonitor::Attach(AttachArgs *args)
1414{
1415 lldb::pid_t pid = args->m_pid;
1416
1417 ProcessMonitor *monitor = args->m_monitor;
1418 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001419 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001420 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001421
Matt Kopec085d6ce2013-05-31 22:00:07 +00001422 // Use a map to keep track of the threads which we have attached/need to attach.
1423 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001424 if (pid <= 1)
1425 {
1426 args->m_error.SetErrorToGenericError();
1427 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1428 goto FINISH;
1429 }
1430
Matt Kopec085d6ce2013-05-31 22:00:07 +00001431 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001432 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001433 for (Host::TidMap::iterator it = tids_to_attach.begin();
1434 it != tids_to_attach.end(); ++it)
1435 {
1436 if (it->second == false)
1437 {
1438 lldb::tid_t tid = it->first;
1439
1440 // Attach to the requested process.
1441 // An attach will cause the thread to stop with a SIGSTOP.
1442 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1443 {
1444 // No such thread. The thread may have exited.
1445 // More error handling may be needed.
1446 if (errno == ESRCH)
1447 {
1448 tids_to_attach.erase(it);
1449 continue;
1450 }
1451 else
1452 {
1453 args->m_error.SetErrorToErrno();
1454 goto FINISH;
1455 }
1456 }
1457
Todd Fiala9be50492014-07-01 16:30:53 +00001458 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001459 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1460 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001461 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001462 {
1463 // No such thread. The thread may have exited.
1464 // More error handling may be needed.
1465 if (errno == ESRCH)
1466 {
1467 tids_to_attach.erase(it);
1468 continue;
1469 }
1470 else
1471 {
1472 args->m_error.SetErrorToErrno();
1473 goto FINISH;
1474 }
1475 }
1476
1477 if (!SetDefaultPtraceOpts(tid))
1478 {
1479 args->m_error.SetErrorToErrno();
1480 goto FINISH;
1481 }
1482
1483 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001484 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001485
Matt Kopec085d6ce2013-05-31 22:00:07 +00001486 if (log)
1487 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1488 process.GetThreadList().AddThread(inferior);
1489 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001490 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001491 }
1492 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001493 }
1494
Matt Kopec085d6ce2013-05-31 22:00:07 +00001495 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001496 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001497 monitor->m_pid = pid;
1498 // Let our process instance know the thread has stopped.
1499 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001500 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001501 else
1502 {
1503 args->m_error.SetErrorToGenericError();
1504 args->m_error.SetErrorString("No such process.");
1505 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001506
1507 FINISH:
1508 return args->m_error.Success();
1509}
1510
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001511bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001512ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1513{
1514 long ptrace_opts = 0;
1515
1516 // Have the child raise an event on exit. This is used to keep the child in
1517 // limbo until it is destroyed.
1518 ptrace_opts |= PTRACE_O_TRACEEXIT;
1519
1520 // Have the tracer trace threads which spawn in the inferior process.
1521 // TODO: if we want to support tracing the inferiors' child, add the
1522 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1523 ptrace_opts |= PTRACE_O_TRACECLONE;
1524
1525 // Have the tracer notify us before execve returns
1526 // (needed to disable legacy SIGTRAP generation)
1527 ptrace_opts |= PTRACE_O_TRACEEXEC;
1528
1529 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1530}
1531
1532bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001533ProcessMonitor::MonitorCallback(void *callback_baton,
1534 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001535 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001536 int signal,
1537 int status)
1538{
1539 ProcessMessage message;
1540 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001541 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001542 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001543 bool stop_monitoring;
1544 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001545 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001546
Andrew Kaylor93132f52013-05-28 23:04:25 +00001547 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1548
1549 if (exited)
1550 {
1551 if (log)
1552 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1553 message = ProcessMessage::Exit(pid, status);
1554 process->SendMessage(message);
1555 return pid == process->GetID();
1556 }
1557
Daniel Maleaa35970a2012-11-23 18:09:58 +00001558 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1559 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001560 if (log)
1561 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001562 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1563 if (!monitor->Resume(pid, SIGSTOP)) {
1564 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1565 }
1566 stop_monitoring = false;
1567 } else {
1568 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1569 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001570 // stop the monitor thread if this is the main pid.
1571 if (log)
1572 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1573 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1574 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001575 // If we are going to stop monitoring, we need to notify our process object
1576 if (stop_monitoring)
1577 {
1578 message = ProcessMessage::Exit(pid, status);
1579 process->SendMessage(message);
1580 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001581 }
1582 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001583 else {
1584 switch (info.si_signo)
1585 {
1586 case SIGTRAP:
1587 message = MonitorSIGTRAP(monitor, &info, pid);
1588 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001589
Stephen Wilson84ffe702011-03-30 15:55:52 +00001590 default:
1591 message = MonitorSignal(monitor, &info, pid);
1592 break;
1593 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001594
Stephen Wilson84ffe702011-03-30 15:55:52 +00001595 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001596 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001597 }
1598
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001599 return stop_monitoring;
1600}
1601
1602ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001603ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001604 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001605{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001606 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001607
Andrew Kaylor93132f52013-05-28 23:04:25 +00001608 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1609
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001610 assert(monitor);
1611 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001612
Stephen Wilson84ffe702011-03-30 15:55:52 +00001613 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001614 {
1615 default:
1616 assert(false && "Unexpected SIGTRAP code!");
1617 break;
1618
Matt Kopeca360d7e2013-05-17 19:27:47 +00001619 // TODO: these two cases are required if we want to support tracing
1620 // of the inferiors' children
1621 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1622 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1623
Matt Kopec650648f2013-01-08 16:30:18 +00001624 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1625 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001626 if (log)
1627 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1628
Matt Kopec650648f2013-01-08 16:30:18 +00001629 unsigned long tid = 0;
1630 if (!monitor->GetEventMessage(pid, &tid))
1631 tid = -1;
1632 message = ProcessMessage::NewThread(pid, tid);
1633 break;
1634 }
1635
Matt Kopeca360d7e2013-05-17 19:27:47 +00001636 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001637 if (log)
1638 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1639
1640 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001641 break;
1642
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001643 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1644 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001645 // The inferior process or one of its threads is about to exit.
1646 // Maintain the process or thread in a state of "limbo" until we are
1647 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001648 unsigned long data = 0;
1649 if (!monitor->GetEventMessage(pid, &data))
1650 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001651 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001652 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001653 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001654 break;
1655 }
1656
1657 case 0:
1658 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001659 if (log)
1660 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001661 message = ProcessMessage::Trace(pid);
1662 break;
1663
1664 case SI_KERNEL:
1665 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001666 if (log)
1667 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001668 message = ProcessMessage::Break(pid);
1669 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001670
1671 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001672 if (log)
1673 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001674 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1675 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001676
1677 case SIGTRAP:
1678 case (SIGTRAP | 0x80):
1679 if (log)
1680 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1681 // Ignore these signals until we know more about them
1682 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001683 }
1684
1685 return message;
1686}
1687
Stephen Wilson84ffe702011-03-30 15:55:52 +00001688ProcessMessage
1689ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001690 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001691{
1692 ProcessMessage message;
1693 int signo = info->si_signo;
1694
Andrew Kaylor93132f52013-05-28 23:04:25 +00001695 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1696
Stephen Wilson84ffe702011-03-30 15:55:52 +00001697 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1698 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1699 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1700 //
1701 // IOW, user generated signals never generate what we consider to be a
1702 // "crash".
1703 //
1704 // Similarly, ACK signals generated by this monitor.
1705 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1706 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001707 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001708 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001709 __FUNCTION__,
1710 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1711 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1712 info->si_pid);
1713
Stephen Wilson84ffe702011-03-30 15:55:52 +00001714 if (info->si_pid == getpid())
1715 return ProcessMessage::SignalDelivered(pid, signo);
1716 else
1717 return ProcessMessage::Signal(pid, signo);
1718 }
1719
Andrew Kaylor93132f52013-05-28 23:04:25 +00001720 if (log)
1721 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1722
Stephen Wilson84ffe702011-03-30 15:55:52 +00001723 if (signo == SIGSEGV) {
1724 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1725 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1726 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1727 }
1728
1729 if (signo == SIGILL) {
1730 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1731 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1732 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1733 }
1734
1735 if (signo == SIGFPE) {
1736 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1737 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1738 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1739 }
1740
1741 if (signo == SIGBUS) {
1742 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1743 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1744 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1745 }
1746
1747 // Everything else is "normal" and does not require any special action on
1748 // our part.
1749 return ProcessMessage::Signal(pid, signo);
1750}
1751
Andrew Kaylord4d54992013-09-17 00:30:24 +00001752// On Linux, when a new thread is created, we receive to notifications,
1753// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1754// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1755// the new child thread indicating that it has is stopped because we attached.
1756// We have no guarantee of the order in which these arrive, but we need both
1757// before we are ready to proceed. We currently keep a list of threads which
1758// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1759// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1760// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1761
1762bool
1763ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1764{
1765 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1766 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001767 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001768
1769 // Wait for the thread to stop
1770 while (true)
1771 {
1772 int status = -1;
1773 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001774 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001775 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001776 if (status == -1)
1777 {
1778 // If we got interrupted by a signal (in our process, not the
1779 // inferior) try again.
1780 if (errno == EINTR)
1781 continue;
1782 else
1783 {
1784 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001785 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001786 return false; // This is bad, but there's nothing we can do.
1787 }
1788 }
1789
1790 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001791 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001792
Todd Fiala9be50492014-07-01 16:30:53 +00001793 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001794
1795 siginfo_t info;
1796 int ptrace_err;
1797 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1798 {
1799 if (log)
1800 {
1801 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1802 }
1803 return false;
1804 }
1805
1806 // If this is a thread exit, we won't get any more information.
1807 if (WIFEXITED(status))
1808 {
1809 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001810 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001811 return true;
1812 continue;
1813 }
1814
1815 assert(info.si_code == SI_USER);
1816 assert(WSTOPSIG(status) == SIGSTOP);
1817
1818 if (log)
1819 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1820 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1821 return true;
1822 }
1823 return false;
1824}
1825
Andrew Kaylor93132f52013-05-28 23:04:25 +00001826bool
1827ProcessMonitor::StopThread(lldb::tid_t tid)
1828{
1829 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1830
1831 // FIXME: Try to use tgkill or tkill
1832 int ret = tgkill(m_pid, tid, SIGSTOP);
1833 if (log)
1834 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1835
1836 // This can happen if a thread exited while we were trying to stop it. That's OK.
1837 // We'll get the signal for that later.
1838 if (ret < 0)
1839 return false;
1840
1841 // Wait for the thread to stop
1842 while (true)
1843 {
1844 int status = -1;
1845 if (log)
1846 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001847 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001848 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001849 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1850 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001851
Todd Fiala9be50492014-07-01 16:30:53 +00001852 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001853 {
1854 // If we got interrupted by a signal (in our process, not the
1855 // inferior) try again.
1856 if (errno == EINTR)
1857 continue;
1858 else
1859 return false; // This is bad, but there's nothing we can do.
1860 }
1861
1862 // If this is a thread exit, we won't get any more information.
1863 if (WIFEXITED(status))
1864 {
1865 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001866 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001867 return true;
1868 continue;
1869 }
1870
1871 siginfo_t info;
1872 int ptrace_err;
1873 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1874 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001875 // another signal causing a StopAllThreads may have been received
1876 // before wait_pid's group-stop was processed, handle it now
1877 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001878 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001879 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001880
Todd Fiala1b0539c2014-01-27 17:03:57 +00001881 if (log)
1882 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1883 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1884 if (!Resume(wait_pid, SIGSTOP)) {
1885 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1886 }
1887 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001888 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001889
1890 if (log)
1891 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001892 return false;
1893 }
1894
1895 // Handle events from other threads
1896 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001897 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1898 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001899
1900 ProcessMessage message;
1901 if (info.si_signo == SIGTRAP)
1902 message = MonitorSIGTRAP(this, &info, wait_pid);
1903 else
1904 message = MonitorSignal(this, &info, wait_pid);
1905
1906 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1907
1908 // When a new thread is created, we may get a SIGSTOP for the new thread
1909 // just before we get the SIGTRAP that we use to add the thread to our
1910 // process thread list. We don't need to worry about that signal here.
1911 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1912
1913 if (!thread)
1914 {
1915 m_process->SendMessage(message);
1916 continue;
1917 }
1918
1919 switch (message.GetKind())
1920 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00001921 case ProcessMessage::eExecMessage:
1922 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00001923 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001924 case ProcessMessage::eInvalidMessage:
1925 break;
1926
1927 // These need special handling because we don't want to send a
1928 // resume even if we already sent a SIGSTOP to this thread. In
1929 // this case the resume will cause the thread to disappear. It is
1930 // unlikely that we'll ever get eExitMessage here, but the same
1931 // reasoning applies.
1932 case ProcessMessage::eLimboMessage:
1933 case ProcessMessage::eExitMessage:
1934 if (log)
1935 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1936 // SendMessage will set the thread state as needed.
1937 m_process->SendMessage(message);
1938 // If this is the thread we're waiting for, stop waiting. Even
1939 // though this wasn't the signal we expected, it's the last
1940 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00001941 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001942 return true;
1943 break;
1944
Matt Kopecb2910442013-07-09 15:09:45 +00001945 case ProcessMessage::eSignalMessage:
1946 if (log)
1947 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1948 if (WSTOPSIG(status) == SIGSTOP)
1949 {
1950 m_process->AddThreadForInitialStopIfNeeded(tid);
1951 thread->SetState(lldb::eStateStopped);
1952 }
1953 else
1954 {
1955 m_process->SendMessage(message);
1956 // This isn't the stop we were expecting, but the thread is
1957 // stopped. SendMessage will handle processing of this event,
1958 // but we need to resume here to get the stop we are waiting
1959 // for (otherwise the thread will stop again immediately when
1960 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001961 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00001962 Resume(wait_pid, eResumeSignalNone);
1963 }
1964 break;
1965
Andrew Kaylor93132f52013-05-28 23:04:25 +00001966 case ProcessMessage::eSignalDeliveredMessage:
1967 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00001968 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
1969 WIFSTOPPED(status) &&
1970 WSTOPSIG(status) == SIGSTOP &&
1971 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001972 {
1973 if (log)
1974 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1975 thread->SetState(lldb::eStateStopped);
1976 return true;
1977 }
1978 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001979 case ProcessMessage::eBreakpointMessage:
1980 case ProcessMessage::eTraceMessage:
1981 case ProcessMessage::eWatchpointMessage:
1982 case ProcessMessage::eCrashMessage:
1983 case ProcessMessage::eNewThreadMessage:
1984 if (log)
1985 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1986 // SendMessage will set the thread state as needed.
1987 m_process->SendMessage(message);
1988 // This isn't the stop we were expecting, but the thread is
1989 // stopped. SendMessage will handle processing of this event,
1990 // but we need to resume here to get the stop we are waiting
1991 // for (otherwise the thread will stop again immediately when
1992 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001993 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001994 Resume(wait_pid, eResumeSignalNone);
1995 break;
1996 }
1997 }
1998 return false;
1999}
2000
Stephen Wilson84ffe702011-03-30 15:55:52 +00002001ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002002ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002003{
2004 ProcessMessage::CrashReason reason;
2005 assert(info->si_signo == SIGSEGV);
2006
2007 reason = ProcessMessage::eInvalidCrashReason;
2008
Greg Clayton542e4072012-09-07 17:49:29 +00002009 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002010 {
2011 default:
2012 assert(false && "unexpected si_code for SIGSEGV");
2013 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00002014 case SI_KERNEL:
2015 // Linux will occasionally send spurious SI_KERNEL codes.
2016 // (this is poorly documented in sigaction)
2017 // One way to get this is via unaligned SIMD loads.
2018 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
2019 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002020 case SEGV_MAPERR:
2021 reason = ProcessMessage::eInvalidAddress;
2022 break;
2023 case SEGV_ACCERR:
2024 reason = ProcessMessage::ePrivilegedAddress;
2025 break;
2026 }
Greg Clayton542e4072012-09-07 17:49:29 +00002027
Stephen Wilson84ffe702011-03-30 15:55:52 +00002028 return reason;
2029}
2030
2031ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002032ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002033{
2034 ProcessMessage::CrashReason reason;
2035 assert(info->si_signo == SIGILL);
2036
2037 reason = ProcessMessage::eInvalidCrashReason;
2038
2039 switch (info->si_code)
2040 {
2041 default:
2042 assert(false && "unexpected si_code for SIGILL");
2043 break;
2044 case ILL_ILLOPC:
2045 reason = ProcessMessage::eIllegalOpcode;
2046 break;
2047 case ILL_ILLOPN:
2048 reason = ProcessMessage::eIllegalOperand;
2049 break;
2050 case ILL_ILLADR:
2051 reason = ProcessMessage::eIllegalAddressingMode;
2052 break;
2053 case ILL_ILLTRP:
2054 reason = ProcessMessage::eIllegalTrap;
2055 break;
2056 case ILL_PRVOPC:
2057 reason = ProcessMessage::ePrivilegedOpcode;
2058 break;
2059 case ILL_PRVREG:
2060 reason = ProcessMessage::ePrivilegedRegister;
2061 break;
2062 case ILL_COPROC:
2063 reason = ProcessMessage::eCoprocessorError;
2064 break;
2065 case ILL_BADSTK:
2066 reason = ProcessMessage::eInternalStackError;
2067 break;
2068 }
2069
2070 return reason;
2071}
2072
2073ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002074ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002075{
2076 ProcessMessage::CrashReason reason;
2077 assert(info->si_signo == SIGFPE);
2078
2079 reason = ProcessMessage::eInvalidCrashReason;
2080
2081 switch (info->si_code)
2082 {
2083 default:
2084 assert(false && "unexpected si_code for SIGFPE");
2085 break;
2086 case FPE_INTDIV:
2087 reason = ProcessMessage::eIntegerDivideByZero;
2088 break;
2089 case FPE_INTOVF:
2090 reason = ProcessMessage::eIntegerOverflow;
2091 break;
2092 case FPE_FLTDIV:
2093 reason = ProcessMessage::eFloatDivideByZero;
2094 break;
2095 case FPE_FLTOVF:
2096 reason = ProcessMessage::eFloatOverflow;
2097 break;
2098 case FPE_FLTUND:
2099 reason = ProcessMessage::eFloatUnderflow;
2100 break;
2101 case FPE_FLTRES:
2102 reason = ProcessMessage::eFloatInexactResult;
2103 break;
2104 case FPE_FLTINV:
2105 reason = ProcessMessage::eFloatInvalidOperation;
2106 break;
2107 case FPE_FLTSUB:
2108 reason = ProcessMessage::eFloatSubscriptRange;
2109 break;
2110 }
2111
2112 return reason;
2113}
2114
2115ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002116ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002117{
2118 ProcessMessage::CrashReason reason;
2119 assert(info->si_signo == SIGBUS);
2120
2121 reason = ProcessMessage::eInvalidCrashReason;
2122
2123 switch (info->si_code)
2124 {
2125 default:
2126 assert(false && "unexpected si_code for SIGBUS");
2127 break;
2128 case BUS_ADRALN:
2129 reason = ProcessMessage::eIllegalAlignment;
2130 break;
2131 case BUS_ADRERR:
2132 reason = ProcessMessage::eIllegalAddress;
2133 break;
2134 case BUS_OBJERR:
2135 reason = ProcessMessage::eHardwareError;
2136 break;
2137 }
2138
2139 return reason;
2140}
2141
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002142void
Johnny Chen25e68e32011-06-14 19:19:50 +00002143ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002144{
Stephen Wilson570243b2011-01-19 01:37:06 +00002145 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002146
Stephen Wilson570243b2011-01-19 01:37:06 +00002147 // We are finised with the arguments and are ready to go. Sync with the
2148 // parent thread and start serving operations on the inferior.
2149 sem_post(&args->m_semaphore);
2150
Michael Sartain704bf892013-10-09 01:28:57 +00002151 for(;;)
2152 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002153 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002154 if (sem_wait(&monitor->m_operation_pending))
2155 {
2156 if (errno == EINTR)
2157 continue;
2158 assert(false && "Unexpected errno from sem_wait");
2159 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002160
Daniel Malea1efb4182013-09-16 23:12:18 +00002161 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002162
Daniel Malea1efb4182013-09-16 23:12:18 +00002163 // notify calling thread that operation is complete
2164 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002165 }
2166}
2167
2168void
2169ProcessMonitor::DoOperation(Operation *op)
2170{
Daniel Malea1efb4182013-09-16 23:12:18 +00002171 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002172
Daniel Malea1efb4182013-09-16 23:12:18 +00002173 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002174
Daniel Malea1efb4182013-09-16 23:12:18 +00002175 // notify operation thread that an operation is ready to be processed
2176 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002177
Daniel Malea1efb4182013-09-16 23:12:18 +00002178 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002179 while (sem_wait(&m_operation_done))
2180 {
2181 if (errno == EINTR)
2182 continue;
2183 assert(false && "Unexpected errno from sem_wait");
2184 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002185}
2186
2187size_t
2188ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2189 Error &error)
2190{
2191 size_t result;
2192 ReadOperation op(vm_addr, buf, size, error, result);
2193 DoOperation(&op);
2194 return result;
2195}
2196
2197size_t
2198ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2199 lldb_private::Error &error)
2200{
2201 size_t result;
2202 WriteOperation op(vm_addr, buf, size, error, result);
2203 DoOperation(&op);
2204 return result;
2205}
2206
2207bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002208ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002209 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002210{
2211 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002212 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002213 DoOperation(&op);
2214 return result;
2215}
2216
2217bool
Matt Kopec7de48462013-03-06 17:20:48 +00002218ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002219 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002220{
2221 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002222 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002223 DoOperation(&op);
2224 return result;
2225}
2226
2227bool
Matt Kopec7de48462013-03-06 17:20:48 +00002228ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002229{
2230 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002231 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002232 DoOperation(&op);
2233 return result;
2234}
2235
2236bool
Matt Kopec7de48462013-03-06 17:20:48 +00002237ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002238{
2239 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002240 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002241 DoOperation(&op);
2242 return result;
2243}
2244
2245bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002246ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2247{
2248 bool result;
2249 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2250 DoOperation(&op);
2251 return result;
2252}
2253
2254bool
Matt Kopec7de48462013-03-06 17:20:48 +00002255ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002256{
2257 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002258 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002259 DoOperation(&op);
2260 return result;
2261}
2262
2263bool
Matt Kopec7de48462013-03-06 17:20:48 +00002264ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002265{
2266 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002267 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002268 DoOperation(&op);
2269 return result;
2270}
2271
2272bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002273ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2274{
2275 bool result;
2276 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2277 DoOperation(&op);
2278 return result;
2279}
2280
2281bool
Richard Mitton0a558352013-10-17 21:14:00 +00002282ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2283{
2284 bool result;
2285 ReadThreadPointerOperation op(tid, &value, result);
2286 DoOperation(&op);
2287 return result;
2288}
2289
2290bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002291ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002292{
2293 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002294 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2295
2296 if (log)
2297 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2298 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002299 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002300 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002301 if (log)
2302 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002303 return result;
2304}
2305
2306bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002307ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002308{
2309 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002310 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002311 DoOperation(&op);
2312 return result;
2313}
2314
2315bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002316ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002317{
Ed Maste4e0999b2014-04-01 18:14:06 +00002318 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002319}
2320
2321bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002322ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002323{
2324 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002325 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002326 DoOperation(&op);
2327 return result;
2328}
2329
2330bool
2331ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2332{
2333 bool result;
2334 EventMessageOperation op(tid, message, result);
2335 DoOperation(&op);
2336 return result;
2337}
2338
Greg Clayton743ecf42012-10-16 20:20:18 +00002339lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002340ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002341{
Greg Clayton28041352011-11-29 20:50:10 +00002342 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002343 if (tid != LLDB_INVALID_THREAD_ID)
2344 {
2345 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002346 DoOperation(&op);
2347 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002348 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002349}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002350
2351bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002352ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2353{
Peter Collingbourne62343202011-06-14 03:55:54 +00002354 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002355
2356 if (target_fd == -1)
2357 return false;
2358
Peter Collingbourne62343202011-06-14 03:55:54 +00002359 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002360}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002361
2362void
2363ProcessMonitor::StopMonitoringChildProcess()
2364{
2365 lldb::thread_result_t thread_result;
2366
Stephen Wilsond4182f42011-02-09 20:10:35 +00002367 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002368 {
2369 Host::ThreadCancel(m_monitor_thread, NULL);
2370 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2371 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2372 }
2373}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002374
2375void
2376ProcessMonitor::StopMonitor()
2377{
2378 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002379 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002380 sem_destroy(&m_operation_pending);
2381 sem_destroy(&m_operation_done);
2382
Andrew Kaylor5e268992013-09-14 00:17:31 +00002383 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2384 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2385 // the descriptor to a ConnectionFileDescriptor object. Consequently
2386 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002387}
2388
2389void
Greg Clayton743ecf42012-10-16 20:20:18 +00002390ProcessMonitor::StopOpThread()
2391{
2392 lldb::thread_result_t result;
2393
2394 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2395 return;
2396
2397 Host::ThreadCancel(m_operation_thread, NULL);
2398 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002399 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002400}