blob: 42a0aa51f4ca25704bceb216d9322a2e68177514 [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>
20#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000021#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000022#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000023#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000024#include <sys/wait.h>
25
26// C++ Includes
27// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000028#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000029#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000030#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000031#include "lldb/Core/Scalar.h"
32#include "lldb/Host/Host.h"
33#include "lldb/Target/Thread.h"
34#include "lldb/Target/RegisterContext.h"
35#include "lldb/Utility/PseudoTerminal.h"
36
Johnny Chen30213ff2012-01-05 19:17:38 +000037#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000038#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000039#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000040#include "ProcessMonitor.h"
41
Greg Clayton386ff182011-11-05 01:09:16 +000042#define DEBUG_PTRACE_MAXBYTES 20
43
Matt Kopec58c0b962013-03-20 20:34:35 +000044// Support ptrace extensions even when compiled without required kernel support
45#ifndef PTRACE_GETREGSET
46 #define PTRACE_GETREGSET 0x4204
47#endif
48#ifndef PTRACE_SETREGSET
49 #define PTRACE_SETREGSET 0x4205
50#endif
Richard Mitton0a558352013-10-17 21:14:00 +000051#ifndef PTRACE_GET_THREAD_AREA
52 #define PTRACE_GET_THREAD_AREA 25
53#endif
54#ifndef PTRACE_ARCH_PRCTL
55 #define PTRACE_ARCH_PRCTL 30
56#endif
57#ifndef ARCH_GET_FS
58 #define ARCH_SET_GS 0x1001
59 #define ARCH_SET_FS 0x1002
60 #define ARCH_GET_FS 0x1003
61 #define ARCH_GET_GS 0x1004
62#endif
63
Todd Fiala0bce1b62014-08-17 00:10:50 +000064#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000065
Matt Kopece9ea0da2013-05-07 19:29:28 +000066// Support hardware breakpoints in case it has not been defined
67#ifndef TRAP_HWBKPT
68 #define TRAP_HWBKPT 4
69#endif
70
Andrew Kaylor93132f52013-05-28 23:04:25 +000071// Try to define a macro to encapsulate the tgkill syscall
72// fall back on kill() if tgkill isn't available
73#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
74
Stephen Wilsone6f9f662010-07-24 02:19:04 +000075using namespace lldb_private;
76
Johnny Chen0d5f2d42011-10-18 18:09:30 +000077// FIXME: this code is host-dependent with respect to types and
78// endianness and needs to be fixed. For example, lldb::addr_t is
79// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
80// 32-bit pointer arguments. This code uses casts to work around the
81// problem.
82
83// We disable the tracing of ptrace calls for integration builds to
84// avoid the additional indirection and checks.
85#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
86
Greg Clayton386ff182011-11-05 01:09:16 +000087static void
88DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
89{
90 uint8_t *ptr = (uint8_t *)bytes;
91 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
92 for(uint32_t i=0; i<loop_count; i++)
93 {
94 s.Printf ("[%x]", *ptr);
95 ptr++;
96 }
97}
98
Matt Kopec58c0b962013-03-20 20:34:35 +000099static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000100{
101 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000102 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000103 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000104
105 if (verbose_log)
106 {
107 switch(req)
108 {
109 case PTRACE_POKETEXT:
110 {
111 DisplayBytes(buf, &data, 8);
112 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
113 break;
114 }
Greg Clayton542e4072012-09-07 17:49:29 +0000115 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000116 {
117 DisplayBytes(buf, &data, 8);
118 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
119 break;
120 }
Greg Clayton542e4072012-09-07 17:49:29 +0000121 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000122 {
123 DisplayBytes(buf, &data, 8);
124 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
125 break;
126 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000127#ifdef PT_SETREGS
Greg Clayton542e4072012-09-07 17:49:29 +0000128 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000129 {
Matt Kopec7de48462013-03-06 17:20:48 +0000130 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000131 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
132 break;
133 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000134#endif
135#ifdef PT_SETFPREGS
Greg Clayton386ff182011-11-05 01:09:16 +0000136 case PTRACE_SETFPREGS:
137 {
Matt Kopec7de48462013-03-06 17:20:48 +0000138 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000139 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
140 break;
141 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000142#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000143 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000144 {
145 DisplayBytes(buf, data, sizeof(siginfo_t));
146 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
147 break;
148 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000149 case PTRACE_SETREGSET:
150 {
151 // Extract iov_base from data, which is a pointer to the struct IOVEC
152 DisplayBytes(buf, *(void **)data, data_size);
153 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
154 break;
155 }
Greg Clayton386ff182011-11-05 01:09:16 +0000156 default:
157 {
158 }
159 }
160 }
161}
162
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000163// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000164// Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000165extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000166PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000167 const char* reqName, const char* file, int line)
168{
Greg Clayton386ff182011-11-05 01:09:16 +0000169 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000170
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000171 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000172
Matt Kopec7de48462013-03-06 17:20:48 +0000173 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000174
175 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000176 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000177 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000178 else
Todd Fiala4507f062014-02-27 20:46:12 +0000179 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000180
Ed Mastec099c952014-02-24 14:07:45 +0000181 if (log)
182 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
183 reqName, pid, addr, data, data_size, result, file, line);
184
Matt Kopec7de48462013-03-06 17:20:48 +0000185 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000186
Matt Kopec7de48462013-03-06 17:20:48 +0000187 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000188 {
189 const char* str;
190 switch (errno)
191 {
192 case ESRCH: str = "ESRCH"; break;
193 case EINVAL: str = "EINVAL"; break;
194 case EBUSY: str = "EBUSY"; break;
195 case EPERM: str = "EPERM"; break;
196 default: str = "<unknown>";
197 }
198 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
199 }
200
201 return result;
202}
203
Matt Kopec7de48462013-03-06 17:20:48 +0000204// Wrapper for ptrace when logging is not required.
205// Sets errno to 0 prior to calling ptrace.
206extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000207PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000208{
Matt Kopec58c0b962013-03-20 20:34:35 +0000209 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000210 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000211 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
212 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
213 else
214 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000215 return result;
216}
217
218#define PTRACE(req, pid, addr, data, data_size) \
219 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000220#else
Matt Kopec7de48462013-03-06 17:20:48 +0000221 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000222#endif
223
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224//------------------------------------------------------------------------------
225// Static implementations of ProcessMonitor::ReadMemory and
226// ProcessMonitor::WriteMemory. This enables mutual recursion between these
227// functions without needed to go thru the thread funnel.
228
229static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000230DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000231 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
232{
Greg Clayton542e4072012-09-07 17:49:29 +0000233 // ptrace word size is determined by the host, not the child
234 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235 unsigned char *dst = static_cast<unsigned char*>(buf);
236 size_t bytes_read;
237 size_t remainder;
238 long data;
239
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000240 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000241 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000242 ProcessPOSIXLog::IncNestLevel();
243 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000244 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000245 pid, word_size, (void*)vm_addr, buf, size);
246
247 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
249 {
250 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000251 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
252 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000253 {
254 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000255 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000256 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 return bytes_read;
258 }
259
260 remainder = size - bytes_read;
261 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000262
263 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000264 for (unsigned i = 0; i < remainder; ++i)
265 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000266
Johnny Chen30213ff2012-01-05 19:17:38 +0000267 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
268 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
269 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
270 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000271 {
272 uintptr_t print_dst = 0;
273 // Format bytes from data by moving into print_dst for log output
274 for (unsigned i = 0; i < remainder; ++i)
275 print_dst |= (((data >> i*8) & 0xFF) << i*8);
276 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
277 (void*)vm_addr, print_dst, (unsigned long)data);
278 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000279
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 vm_addr += word_size;
281 dst += word_size;
282 }
283
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000284 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000285 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286 return bytes_read;
287}
288
289static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000290DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000291 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
292{
Greg Clayton542e4072012-09-07 17:49:29 +0000293 // ptrace word size is determined by the host, not the child
294 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000295 const unsigned char *src = static_cast<const unsigned char*>(buf);
296 size_t bytes_written = 0;
297 size_t remainder;
298
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000299 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000300 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000301 ProcessPOSIXLog::IncNestLevel();
302 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000303 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 pid, word_size, (void*)vm_addr, buf, size);
305
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000306 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
307 {
308 remainder = size - bytes_written;
309 remainder = remainder > word_size ? word_size : remainder;
310
311 if (remainder == word_size)
312 {
313 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000314 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000315 for (unsigned i = 0; i < word_size; ++i)
316 data |= (unsigned long)src[i] << i*8;
317
Johnny Chen30213ff2012-01-05 19:17:38 +0000318 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
319 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
320 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
321 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
323 (void*)vm_addr, *(unsigned long*)src, data);
324
Matt Kopec7de48462013-03-06 17:20:48 +0000325 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000326 {
327 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000328 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000329 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000330 return bytes_written;
331 }
332 }
333 else
334 {
335 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000336 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000337 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000338 {
339 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000340 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000343
344 memcpy(buff, src, remainder);
345
Greg Clayton542e4072012-09-07 17:49:29 +0000346 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000347 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000348 {
349 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000350 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000351 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 }
353
Johnny Chen30213ff2012-01-05 19:17:38 +0000354 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
355 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
356 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
357 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000358 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
359 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000360 }
361
362 vm_addr += word_size;
363 src += word_size;
364 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000365 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000366 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367 return bytes_written;
368}
369
Stephen Wilson26977162011-03-23 02:14:42 +0000370// Simple helper function to ensure flags are enabled on the given file
371// descriptor.
372static bool
373EnsureFDFlags(int fd, int flags, Error &error)
374{
375 int status;
376
377 if ((status = fcntl(fd, F_GETFL)) == -1)
378 {
379 error.SetErrorToErrno();
380 return false;
381 }
382
383 if (fcntl(fd, F_SETFL, status | flags) == -1)
384 {
385 error.SetErrorToErrno();
386 return false;
387 }
388
389 return true;
390}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000391
392//------------------------------------------------------------------------------
393/// @class Operation
394/// @brief Represents a ProcessMonitor operation.
395///
396/// Under Linux, it is not possible to ptrace() from any other thread but the
397/// one that spawned or attached to the process from the start. Therefore, when
398/// a ProcessMonitor is asked to deliver or change the state of an inferior
399/// process the operation must be "funneled" to a specific thread to perform the
400/// task. The Operation class provides an abstract base for all services the
401/// ProcessMonitor must perform via the single virtual function Execute, thus
402/// encapsulating the code that needs to run in the privileged context.
403class Operation
404{
405public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000406 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000407 virtual void Execute(ProcessMonitor *monitor) = 0;
408};
409
410//------------------------------------------------------------------------------
411/// @class ReadOperation
412/// @brief Implements ProcessMonitor::ReadMemory.
413class ReadOperation : public Operation
414{
415public:
416 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
417 Error &error, size_t &result)
418 : m_addr(addr), m_buff(buff), m_size(size),
419 m_error(error), m_result(result)
420 { }
421
422 void Execute(ProcessMonitor *monitor);
423
424private:
425 lldb::addr_t m_addr;
426 void *m_buff;
427 size_t m_size;
428 Error &m_error;
429 size_t &m_result;
430};
431
432void
433ReadOperation::Execute(ProcessMonitor *monitor)
434{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000435 lldb::pid_t pid = monitor->GetPID();
436
Greg Clayton542e4072012-09-07 17:49:29 +0000437 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000438}
439
440//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000441/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000442/// @brief Implements ProcessMonitor::WriteMemory.
443class WriteOperation : public Operation
444{
445public:
446 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
447 Error &error, size_t &result)
448 : m_addr(addr), m_buff(buff), m_size(size),
449 m_error(error), m_result(result)
450 { }
451
452 void Execute(ProcessMonitor *monitor);
453
454private:
455 lldb::addr_t m_addr;
456 const void *m_buff;
457 size_t m_size;
458 Error &m_error;
459 size_t &m_result;
460};
461
462void
463WriteOperation::Execute(ProcessMonitor *monitor)
464{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465 lldb::pid_t pid = monitor->GetPID();
466
Greg Clayton542e4072012-09-07 17:49:29 +0000467 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000468}
469
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000470
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000471//------------------------------------------------------------------------------
472/// @class ReadRegOperation
473/// @brief Implements ProcessMonitor::ReadRegisterValue.
474class ReadRegOperation : public Operation
475{
476public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000477 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000478 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000479 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000481 { }
482
483 void Execute(ProcessMonitor *monitor);
484
485private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000486 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000487 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000488 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000489 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000490 bool &m_result;
491};
492
493void
494ReadRegOperation::Execute(ProcessMonitor *monitor)
495{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000496 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000497
498 // Set errno to zero so that we can detect a failed peek.
499 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000500 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
501 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000502 m_result = false;
503 else
504 {
505 m_value = data;
506 m_result = true;
507 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000508 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000509 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000510 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000511}
512
513//------------------------------------------------------------------------------
514/// @class WriteRegOperation
515/// @brief Implements ProcessMonitor::WriteRegisterValue.
516class WriteRegOperation : public Operation
517{
518public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000519 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000520 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000521 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000523 { }
524
525 void Execute(ProcessMonitor *monitor);
526
527private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000528 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000529 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000530 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000531 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000532 bool &m_result;
533};
534
535void
536WriteRegOperation::Execute(ProcessMonitor *monitor)
537{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000538 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000539 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000540
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000541 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000542
543 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000544 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000545 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000546 m_result = false;
547 else
548 m_result = true;
549}
550
551//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000552/// @class ReadGPROperation
553/// @brief Implements ProcessMonitor::ReadGPR.
554class ReadGPROperation : public Operation
555{
556public:
Matt Kopec7de48462013-03-06 17:20:48 +0000557 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
558 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000559 { }
560
561 void Execute(ProcessMonitor *monitor);
562
563private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000564 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000565 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000566 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000567 bool &m_result;
568};
569
570void
571ReadGPROperation::Execute(ProcessMonitor *monitor)
572{
Todd Fialad35f2b92014-06-23 15:59:04 +0000573#ifdef PT_GETREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000574 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000575 m_result = false;
576 else
577 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000578#else
579 m_result = false;
580#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000581}
582
583//------------------------------------------------------------------------------
584/// @class ReadFPROperation
585/// @brief Implements ProcessMonitor::ReadFPR.
586class ReadFPROperation : public Operation
587{
588public:
Matt Kopec7de48462013-03-06 17:20:48 +0000589 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
590 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000591 { }
592
593 void Execute(ProcessMonitor *monitor);
594
595private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000596 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000597 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000598 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000599 bool &m_result;
600};
601
602void
603ReadFPROperation::Execute(ProcessMonitor *monitor)
604{
Todd Fialad35f2b92014-06-23 15:59:04 +0000605#ifdef PT_GETFPREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000606 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000607 m_result = false;
608 else
609 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000610#else
611 m_result = false;
612#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000613}
614
615//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000616/// @class ReadRegisterSetOperation
617/// @brief Implements ProcessMonitor::ReadRegisterSet.
618class ReadRegisterSetOperation : public Operation
619{
620public:
621 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
622 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
623 { }
624
625 void Execute(ProcessMonitor *monitor);
626
627private:
628 lldb::tid_t m_tid;
629 void *m_buf;
630 size_t m_buf_size;
631 const unsigned int m_regset;
632 bool &m_result;
633};
634
635void
636ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
637{
638 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
639 m_result = false;
640 else
641 m_result = true;
642}
643
644//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000645/// @class WriteGPROperation
646/// @brief Implements ProcessMonitor::WriteGPR.
647class WriteGPROperation : public Operation
648{
649public:
Matt Kopec7de48462013-03-06 17:20:48 +0000650 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
651 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000652 { }
653
654 void Execute(ProcessMonitor *monitor);
655
656private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000657 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000658 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000659 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000660 bool &m_result;
661};
662
663void
664WriteGPROperation::Execute(ProcessMonitor *monitor)
665{
Todd Fialad35f2b92014-06-23 15:59:04 +0000666#ifdef PT_SETREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000667 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000668 m_result = false;
669 else
670 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000671#else
672 m_result = false;
673#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000674}
675
676//------------------------------------------------------------------------------
677/// @class WriteFPROperation
678/// @brief Implements ProcessMonitor::WriteFPR.
679class WriteFPROperation : public Operation
680{
681public:
Matt Kopec7de48462013-03-06 17:20:48 +0000682 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
683 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000684 { }
685
686 void Execute(ProcessMonitor *monitor);
687
688private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000689 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000690 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000691 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000692 bool &m_result;
693};
694
695void
696WriteFPROperation::Execute(ProcessMonitor *monitor)
697{
Todd Fialad35f2b92014-06-23 15:59:04 +0000698#ifdef PT_SETFPREGS
Matt Kopec7de48462013-03-06 17:20:48 +0000699 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000700 m_result = false;
701 else
702 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000703#else
704 m_result = false;
705#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000706}
707
708//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000709/// @class WriteRegisterSetOperation
710/// @brief Implements ProcessMonitor::WriteRegisterSet.
711class WriteRegisterSetOperation : public Operation
712{
713public:
714 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
715 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
716 { }
717
718 void Execute(ProcessMonitor *monitor);
719
720private:
721 lldb::tid_t m_tid;
722 void *m_buf;
723 size_t m_buf_size;
724 const unsigned int m_regset;
725 bool &m_result;
726};
727
728void
729WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
730{
731 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
732 m_result = false;
733 else
734 m_result = true;
735}
736
737//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000738/// @class ReadThreadPointerOperation
739/// @brief Implements ProcessMonitor::ReadThreadPointer.
740class ReadThreadPointerOperation : public Operation
741{
742public:
743 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
744 : m_tid(tid), m_addr(addr), m_result(result)
745 { }
746
747 void Execute(ProcessMonitor *monitor);
748
749private:
750 lldb::tid_t m_tid;
751 lldb::addr_t *m_addr;
752 bool &m_result;
753};
754
755void
756ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
757{
758 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
759 if (log)
760 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
761
762 // The process for getting the thread area on Linux is
763 // somewhat... obscure. There's several different ways depending on
764 // what arch you're on, and what kernel version you have.
765
766 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
767 switch(arch.GetMachine())
768 {
Todd Fiala720cd3f2014-06-16 14:49:28 +0000769#if defined(__i386__) || defined(__x86_64__)
770 // Note that struct user below has a field named i387 which is x86-specific.
771 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000772 case llvm::Triple::x86:
773 {
774 // Find the GS register location for our host architecture.
775 size_t gs_user_offset = offsetof(struct user, regs);
776#ifdef __x86_64__
777 gs_user_offset += offsetof(struct user_regs_struct, gs);
778#endif
779#ifdef __i386__
780 gs_user_offset += offsetof(struct user_regs_struct, xgs);
781#endif
782
783 // Read the GS register value to get the selector.
784 errno = 0;
785 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
786 if (errno)
787 {
788 m_result = false;
789 break;
790 }
791
792 // Read the LDT base for that selector.
793 uint32_t tmp[4];
794 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
795 *m_addr = tmp[1];
796 break;
797 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000798#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000799 case llvm::Triple::x86_64:
800 // Read the FS register base.
801 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
802 break;
803 default:
804 m_result = false;
805 break;
806 }
807}
808
809//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000810/// @class ResumeOperation
811/// @brief Implements ProcessMonitor::Resume.
812class ResumeOperation : public Operation
813{
814public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000815 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
816 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000817
818 void Execute(ProcessMonitor *monitor);
819
820private:
821 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000822 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000823 bool &m_result;
824};
825
826void
827ResumeOperation::Execute(ProcessMonitor *monitor)
828{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000829 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000830
831 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
832 data = m_signo;
833
Matt Kopec7de48462013-03-06 17:20:48 +0000834 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000835 {
836 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
837
838 if (log)
839 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000840 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000841 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000842 else
843 m_result = true;
844}
845
846//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000847/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000848/// @brief Implements ProcessMonitor::SingleStep.
849class SingleStepOperation : public Operation
850{
851public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000852 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
853 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000854
855 void Execute(ProcessMonitor *monitor);
856
857private:
858 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000859 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000860 bool &m_result;
861};
862
863void
864SingleStepOperation::Execute(ProcessMonitor *monitor)
865{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000866 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000867
868 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
869 data = m_signo;
870
Matt Kopec7de48462013-03-06 17:20:48 +0000871 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000872 m_result = false;
873 else
874 m_result = true;
875}
876
877//------------------------------------------------------------------------------
878/// @class SiginfoOperation
879/// @brief Implements ProcessMonitor::GetSignalInfo.
880class SiginfoOperation : public Operation
881{
882public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000883 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
884 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000885
886 void Execute(ProcessMonitor *monitor);
887
888private:
889 lldb::tid_t m_tid;
890 void *m_info;
891 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000892 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000893};
894
895void
896SiginfoOperation::Execute(ProcessMonitor *monitor)
897{
Matt Kopec7de48462013-03-06 17:20:48 +0000898 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000899 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000900 m_err = errno;
901 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000902 else
903 m_result = true;
904}
905
906//------------------------------------------------------------------------------
907/// @class EventMessageOperation
908/// @brief Implements ProcessMonitor::GetEventMessage.
909class EventMessageOperation : public Operation
910{
911public:
912 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
913 : m_tid(tid), m_message(message), m_result(result) { }
914
915 void Execute(ProcessMonitor *monitor);
916
917private:
918 lldb::tid_t m_tid;
919 unsigned long *m_message;
920 bool &m_result;
921};
922
923void
924EventMessageOperation::Execute(ProcessMonitor *monitor)
925{
Matt Kopec7de48462013-03-06 17:20:48 +0000926 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000927 m_result = false;
928 else
929 m_result = true;
930}
931
932//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +0000933/// @class DetachOperation
934/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +0000935class DetachOperation : public Operation
936{
937public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000938 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000939
940 void Execute(ProcessMonitor *monitor);
941
942private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000943 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000944 Error &m_error;
945};
946
947void
948DetachOperation::Execute(ProcessMonitor *monitor)
949{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000950 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000951 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000952}
953
Johnny Chen25e68e32011-06-14 19:19:50 +0000954ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
955 : m_monitor(monitor)
956{
957 sem_init(&m_semaphore, 0, 0);
958}
959
960ProcessMonitor::OperationArgs::~OperationArgs()
961{
962 sem_destroy(&m_semaphore);
963}
964
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000965ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
966 lldb_private::Module *module,
967 char const **argv,
968 char const **envp,
969 const char *stdin_path,
970 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000971 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000972 const char *working_dir,
973 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +0000974 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975 m_module(module),
976 m_argv(argv),
977 m_envp(envp),
978 m_stdin_path(stdin_path),
979 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000980 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +0000981 m_working_dir(working_dir),
982 m_launch_info(launch_info)
983{
984}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000985
986ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000987{ }
988
989ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
990 lldb::pid_t pid)
991 : OperationArgs(monitor), m_pid(pid) { }
992
993ProcessMonitor::AttachArgs::~AttachArgs()
994{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000995
996//------------------------------------------------------------------------------
997/// The basic design of the ProcessMonitor is built around two threads.
998///
999/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1000/// for changes in the debugee state. When a change is detected a
1001/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1002/// "drives" state changes in the debugger.
1003///
1004/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001005/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001006/// operations such as register reads/writes, stepping, etc. See the comments
1007/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001008ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001009 Module *module,
1010 const char *argv[],
1011 const char *envp[],
1012 const char *stdin_path,
1013 const char *stdout_path,
1014 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001015 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001016 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001018 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001019 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001020 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001021 m_pid(LLDB_INVALID_PROCESS_ID),
1022 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001023 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001024{
Daniel Malea1efb4182013-09-16 23:12:18 +00001025 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1026 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001027 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001028
Daniel Malea1efb4182013-09-16 23:12:18 +00001029 sem_init(&m_operation_pending, 0, 0);
1030 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001031
Johnny Chen25e68e32011-06-14 19:19:50 +00001032 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001033 if (!error.Success())
1034 return;
1035
1036WAIT_AGAIN:
1037 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001038 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001039 {
1040 if (errno == EINTR)
1041 goto WAIT_AGAIN;
1042 else
1043 {
1044 error.SetErrorToErrno();
1045 return;
1046 }
1047 }
1048
1049 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001050 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001052 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001053 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 return;
1055 }
1056
1057 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001058 m_monitor_thread = Host::StartMonitoringChildProcess(
1059 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001060 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001061 {
1062 error.SetErrorToGenericError();
1063 error.SetErrorString("Process launch failed.");
1064 return;
1065 }
1066}
1067
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001068ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001069 lldb::pid_t pid,
1070 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001071 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001072 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001073 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001074 m_pid(LLDB_INVALID_PROCESS_ID),
1075 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001076 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001077{
Daniel Malea1efb4182013-09-16 23:12:18 +00001078 sem_init(&m_operation_pending, 0, 0);
1079 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001080
Daniel Malea1efb4182013-09-16 23:12:18 +00001081 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001082
1083 StartAttachOpThread(args.get(), error);
1084 if (!error.Success())
1085 return;
1086
1087WAIT_AGAIN:
1088 // Wait for the operation thread to initialize.
1089 if (sem_wait(&args->m_semaphore))
1090 {
1091 if (errno == EINTR)
1092 goto WAIT_AGAIN;
1093 else
1094 {
1095 error.SetErrorToErrno();
1096 return;
1097 }
1098 }
1099
Greg Clayton743ecf42012-10-16 20:20:18 +00001100 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001101 if (!args->m_error.Success())
1102 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001103 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001104 error = args->m_error;
1105 return;
1106 }
1107
1108 // Finally, start monitoring the child process for change in state.
1109 m_monitor_thread = Host::StartMonitoringChildProcess(
1110 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1111 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1112 {
1113 error.SetErrorToGenericError();
1114 error.SetErrorString("Process attach failed.");
1115 return;
1116 }
1117}
1118
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001119ProcessMonitor::~ProcessMonitor()
1120{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001121 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001122}
1123
1124//------------------------------------------------------------------------------
1125// Thread setup and tear down.
1126void
Johnny Chen25e68e32011-06-14 19:19:50 +00001127ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001128{
1129 static const char *g_thread_name = "lldb.process.linux.operation";
1130
Stephen Wilsond4182f42011-02-09 20:10:35 +00001131 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001132 return;
1133
1134 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001135 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001136}
1137
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001139ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001140{
1141 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1142
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001143 if (!Launch(args)) {
1144 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001145 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001146 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001147
Stephen Wilson570243b2011-01-19 01:37:06 +00001148 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001149 return NULL;
1150}
1151
1152bool
1153ProcessMonitor::Launch(LaunchArgs *args)
1154{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001155 assert (args && "null args");
1156 if (!args)
1157 return false;
1158
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001159 ProcessMonitor *monitor = args->m_monitor;
1160 ProcessLinux &process = monitor->GetProcess();
1161 const char **argv = args->m_argv;
1162 const char **envp = args->m_envp;
1163 const char *stdin_path = args->m_stdin_path;
1164 const char *stdout_path = args->m_stdout_path;
1165 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001166 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167
1168 lldb_utility::PseudoTerminal terminal;
1169 const size_t err_len = 1024;
1170 char err_str[err_len];
1171 lldb::pid_t pid;
1172
1173 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001174 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001175
Stephen Wilson57740ec2011-01-15 00:12:41 +00001176 // Propagate the environment if one is not supplied.
1177 if (envp == NULL || envp[0] == NULL)
1178 envp = const_cast<const char **>(environ);
1179
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001180 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001181 {
1182 args->m_error.SetErrorToGenericError();
1183 args->m_error.SetErrorString("Process fork failed.");
1184 goto FINISH;
1185 }
1186
Peter Collingbourne6a520222011-06-14 03:55:58 +00001187 // Recognized child exit status codes.
1188 enum {
1189 ePtraceFailed = 1,
1190 eDupStdinFailed,
1191 eDupStdoutFailed,
1192 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001193 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001194 eExecFailed,
1195 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001196 };
1197
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001198 // Child process.
1199 if (pid == 0)
1200 {
1201 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001202 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001203 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001204
1205 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001206 if (setgid(getgid()) != 0)
1207 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001208
1209 // Let us have our own process group.
1210 setpgid(0, 0);
1211
Greg Clayton710dd5a2011-01-08 20:28:42 +00001212 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001213 //
1214 // FIXME: If two or more of the paths are the same we needlessly open
1215 // the same file multiple times.
1216 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001217 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001218 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001219
1220 if (stdout_path != NULL && stdout_path[0])
1221 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001222 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001223
1224 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001225 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001226 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001227
Daniel Malea6217d2a2013-01-08 14:49:22 +00001228 // Change working directory
1229 if (working_dir != NULL && working_dir[0])
1230 if (0 != ::chdir(working_dir))
1231 exit(eChdirFailed);
1232
Todd Fiala0bce1b62014-08-17 00:10:50 +00001233 // Disable ASLR if requested.
1234 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1235 {
1236 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1237 if (old_personality == -1)
1238 {
1239 if (log)
1240 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1241 }
1242 else
1243 {
1244 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1245 if (new_personality == -1)
1246 {
1247 if (log)
1248 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1249
1250 }
1251 else
1252 {
1253 if (log)
1254 log->Printf ("ProcessMonitor::%s disbling ASLR: SUCCESS", __FUNCTION__);
1255
1256 }
1257 }
1258 }
1259
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001260 // Execute. We should never return.
1261 execve(argv[0],
1262 const_cast<char *const *>(argv),
1263 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001264 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001265 }
1266
1267 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001268 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001269 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001270 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001271
1272 raw_pid = waitpid(pid, &status, 0);
1273 wpid = static_cast <lldb::pid_t> (raw_pid);
1274 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001275 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001276 args->m_error.SetErrorToErrno();
1277 goto FINISH;
1278 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001279 else if (WIFEXITED(status))
1280 {
1281 // open, dup or execve likely failed for some reason.
1282 args->m_error.SetErrorToGenericError();
1283 switch (WEXITSTATUS(status))
1284 {
Greg Clayton542e4072012-09-07 17:49:29 +00001285 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001286 args->m_error.SetErrorString("Child ptrace failed.");
1287 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001288 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001289 args->m_error.SetErrorString("Child open stdin failed.");
1290 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001291 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001292 args->m_error.SetErrorString("Child open stdout failed.");
1293 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001294 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001295 args->m_error.SetErrorString("Child open stderr failed.");
1296 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001297 case eChdirFailed:
1298 args->m_error.SetErrorString("Child failed to set working directory.");
1299 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001300 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001301 args->m_error.SetErrorString("Child exec failed.");
1302 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001303 case eSetGidFailed:
1304 args->m_error.SetErrorString("Child setgid failed.");
1305 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001306 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001307 args->m_error.SetErrorString("Child returned unknown exit status.");
1308 break;
1309 }
1310 goto FINISH;
1311 }
1312 assert(WIFSTOPPED(status) && wpid == pid &&
1313 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001314
Matt Kopec085d6ce2013-05-31 22:00:07 +00001315 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001316 {
1317 args->m_error.SetErrorToErrno();
1318 goto FINISH;
1319 }
1320
1321 // Release the master terminal descriptor and pass it off to the
1322 // ProcessMonitor instance. Similarly stash the inferior pid.
1323 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1324 monitor->m_pid = pid;
1325
Stephen Wilson26977162011-03-23 02:14:42 +00001326 // Set the terminal fd to be in non blocking mode (it simplifies the
1327 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1328 // descriptor to read from).
1329 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1330 goto FINISH;
1331
Johnny Chen30213ff2012-01-05 19:17:38 +00001332 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001333 // FIXME: should we be letting UpdateThreadList handle this?
1334 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001335 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001336
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001337 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001338 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001339 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001340
Matt Kopecb2910442013-07-09 15:09:45 +00001341 process.AddThreadForInitialStopIfNeeded(pid);
1342
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001343 // Let our process instance know the thread has stopped.
1344 process.SendMessage(ProcessMessage::Trace(pid));
1345
1346FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001347 return args->m_error.Success();
1348}
1349
Johnny Chen25e68e32011-06-14 19:19:50 +00001350void
1351ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1352{
1353 static const char *g_thread_name = "lldb.process.linux.operation";
1354
1355 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1356 return;
1357
1358 m_operation_thread =
1359 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1360}
1361
Johnny Chen25e68e32011-06-14 19:19:50 +00001362void *
1363ProcessMonitor::AttachOpThread(void *arg)
1364{
1365 AttachArgs *args = static_cast<AttachArgs*>(arg);
1366
Greg Clayton743ecf42012-10-16 20:20:18 +00001367 if (!Attach(args)) {
1368 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001369 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001370 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001371
1372 ServeOperation(args);
1373 return NULL;
1374}
1375
1376bool
1377ProcessMonitor::Attach(AttachArgs *args)
1378{
1379 lldb::pid_t pid = args->m_pid;
1380
1381 ProcessMonitor *monitor = args->m_monitor;
1382 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001383 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001384 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001385
Matt Kopec085d6ce2013-05-31 22:00:07 +00001386 // Use a map to keep track of the threads which we have attached/need to attach.
1387 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001388 if (pid <= 1)
1389 {
1390 args->m_error.SetErrorToGenericError();
1391 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1392 goto FINISH;
1393 }
1394
Matt Kopec085d6ce2013-05-31 22:00:07 +00001395 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001396 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001397 for (Host::TidMap::iterator it = tids_to_attach.begin();
1398 it != tids_to_attach.end(); ++it)
1399 {
1400 if (it->second == false)
1401 {
1402 lldb::tid_t tid = it->first;
1403
1404 // Attach to the requested process.
1405 // An attach will cause the thread to stop with a SIGSTOP.
1406 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1407 {
1408 // No such thread. The thread may have exited.
1409 // More error handling may be needed.
1410 if (errno == ESRCH)
1411 {
1412 tids_to_attach.erase(it);
1413 continue;
1414 }
1415 else
1416 {
1417 args->m_error.SetErrorToErrno();
1418 goto FINISH;
1419 }
1420 }
1421
Todd Fiala9be50492014-07-01 16:30:53 +00001422 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001423 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1424 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001425 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001426 {
1427 // No such thread. The thread may have exited.
1428 // More error handling may be needed.
1429 if (errno == ESRCH)
1430 {
1431 tids_to_attach.erase(it);
1432 continue;
1433 }
1434 else
1435 {
1436 args->m_error.SetErrorToErrno();
1437 goto FINISH;
1438 }
1439 }
1440
1441 if (!SetDefaultPtraceOpts(tid))
1442 {
1443 args->m_error.SetErrorToErrno();
1444 goto FINISH;
1445 }
1446
1447 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001448 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001449
Matt Kopec085d6ce2013-05-31 22:00:07 +00001450 if (log)
1451 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1452 process.GetThreadList().AddThread(inferior);
1453 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001454 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001455 }
1456 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001457 }
1458
Matt Kopec085d6ce2013-05-31 22:00:07 +00001459 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001460 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001461 monitor->m_pid = pid;
1462 // Let our process instance know the thread has stopped.
1463 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001464 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001465 else
1466 {
1467 args->m_error.SetErrorToGenericError();
1468 args->m_error.SetErrorString("No such process.");
1469 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001470
1471 FINISH:
1472 return args->m_error.Success();
1473}
1474
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001475bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001476ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1477{
1478 long ptrace_opts = 0;
1479
1480 // Have the child raise an event on exit. This is used to keep the child in
1481 // limbo until it is destroyed.
1482 ptrace_opts |= PTRACE_O_TRACEEXIT;
1483
1484 // Have the tracer trace threads which spawn in the inferior process.
1485 // TODO: if we want to support tracing the inferiors' child, add the
1486 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1487 ptrace_opts |= PTRACE_O_TRACECLONE;
1488
1489 // Have the tracer notify us before execve returns
1490 // (needed to disable legacy SIGTRAP generation)
1491 ptrace_opts |= PTRACE_O_TRACEEXEC;
1492
1493 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1494}
1495
1496bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001497ProcessMonitor::MonitorCallback(void *callback_baton,
1498 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001499 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001500 int signal,
1501 int status)
1502{
1503 ProcessMessage message;
1504 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001505 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001506 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001507 bool stop_monitoring;
1508 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001509 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001510
Andrew Kaylor93132f52013-05-28 23:04:25 +00001511 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1512
1513 if (exited)
1514 {
1515 if (log)
1516 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1517 message = ProcessMessage::Exit(pid, status);
1518 process->SendMessage(message);
1519 return pid == process->GetID();
1520 }
1521
Daniel Maleaa35970a2012-11-23 18:09:58 +00001522 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1523 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001524 if (log)
1525 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001526 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1527 if (!monitor->Resume(pid, SIGSTOP)) {
1528 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1529 }
1530 stop_monitoring = false;
1531 } else {
1532 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1533 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001534 // stop the monitor thread if this is the main pid.
1535 if (log)
1536 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1537 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1538 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001539 // If we are going to stop monitoring, we need to notify our process object
1540 if (stop_monitoring)
1541 {
1542 message = ProcessMessage::Exit(pid, status);
1543 process->SendMessage(message);
1544 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001545 }
1546 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001547 else {
1548 switch (info.si_signo)
1549 {
1550 case SIGTRAP:
1551 message = MonitorSIGTRAP(monitor, &info, pid);
1552 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001553
Stephen Wilson84ffe702011-03-30 15:55:52 +00001554 default:
1555 message = MonitorSignal(monitor, &info, pid);
1556 break;
1557 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001558
Stephen Wilson84ffe702011-03-30 15:55:52 +00001559 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001560 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001561 }
1562
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001563 return stop_monitoring;
1564}
1565
1566ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001567ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001568 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001569{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001570 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001571
Andrew Kaylor93132f52013-05-28 23:04:25 +00001572 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1573
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001574 assert(monitor);
1575 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001576
Stephen Wilson84ffe702011-03-30 15:55:52 +00001577 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001578 {
1579 default:
1580 assert(false && "Unexpected SIGTRAP code!");
1581 break;
1582
Matt Kopeca360d7e2013-05-17 19:27:47 +00001583 // TODO: these two cases are required if we want to support tracing
1584 // of the inferiors' children
1585 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1586 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1587
Matt Kopec650648f2013-01-08 16:30:18 +00001588 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1589 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001590 if (log)
1591 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1592
Matt Kopec650648f2013-01-08 16:30:18 +00001593 unsigned long tid = 0;
1594 if (!monitor->GetEventMessage(pid, &tid))
1595 tid = -1;
1596 message = ProcessMessage::NewThread(pid, tid);
1597 break;
1598 }
1599
Matt Kopeca360d7e2013-05-17 19:27:47 +00001600 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001601 if (log)
1602 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1603
1604 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001605 break;
1606
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001607 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1608 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001609 // The inferior process or one of its threads is about to exit.
1610 // Maintain the process or thread in a state of "limbo" until we are
1611 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001612 unsigned long data = 0;
1613 if (!monitor->GetEventMessage(pid, &data))
1614 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001615 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001616 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001617 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001618 break;
1619 }
1620
1621 case 0:
1622 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001623 if (log)
1624 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001625 message = ProcessMessage::Trace(pid);
1626 break;
1627
1628 case SI_KERNEL:
1629 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001630 if (log)
1631 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001632 message = ProcessMessage::Break(pid);
1633 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001634
1635 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001636 if (log)
1637 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001638 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1639 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001640
1641 case SIGTRAP:
1642 case (SIGTRAP | 0x80):
1643 if (log)
1644 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1645 // Ignore these signals until we know more about them
1646 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001647 }
1648
1649 return message;
1650}
1651
Stephen Wilson84ffe702011-03-30 15:55:52 +00001652ProcessMessage
1653ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001654 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001655{
1656 ProcessMessage message;
1657 int signo = info->si_signo;
1658
Andrew Kaylor93132f52013-05-28 23:04:25 +00001659 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1660
Stephen Wilson84ffe702011-03-30 15:55:52 +00001661 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1662 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1663 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1664 //
1665 // IOW, user generated signals never generate what we consider to be a
1666 // "crash".
1667 //
1668 // Similarly, ACK signals generated by this monitor.
1669 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1670 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001671 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001672 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001673 __FUNCTION__,
1674 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1675 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1676 info->si_pid);
1677
Stephen Wilson84ffe702011-03-30 15:55:52 +00001678 if (info->si_pid == getpid())
1679 return ProcessMessage::SignalDelivered(pid, signo);
1680 else
1681 return ProcessMessage::Signal(pid, signo);
1682 }
1683
Andrew Kaylor93132f52013-05-28 23:04:25 +00001684 if (log)
1685 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1686
Stephen Wilson84ffe702011-03-30 15:55:52 +00001687 if (signo == SIGSEGV) {
1688 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1689 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1690 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1691 }
1692
1693 if (signo == SIGILL) {
1694 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1695 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1696 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1697 }
1698
1699 if (signo == SIGFPE) {
1700 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1701 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1702 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1703 }
1704
1705 if (signo == SIGBUS) {
1706 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1707 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1708 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1709 }
1710
1711 // Everything else is "normal" and does not require any special action on
1712 // our part.
1713 return ProcessMessage::Signal(pid, signo);
1714}
1715
Andrew Kaylord4d54992013-09-17 00:30:24 +00001716// On Linux, when a new thread is created, we receive to notifications,
1717// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1718// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1719// the new child thread indicating that it has is stopped because we attached.
1720// We have no guarantee of the order in which these arrive, but we need both
1721// before we are ready to proceed. We currently keep a list of threads which
1722// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1723// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1724// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1725
1726bool
1727ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1728{
1729 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1730 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001731 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001732
1733 // Wait for the thread to stop
1734 while (true)
1735 {
1736 int status = -1;
1737 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001738 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001739 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001740 if (status == -1)
1741 {
1742 // If we got interrupted by a signal (in our process, not the
1743 // inferior) try again.
1744 if (errno == EINTR)
1745 continue;
1746 else
1747 {
1748 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001749 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001750 return false; // This is bad, but there's nothing we can do.
1751 }
1752 }
1753
1754 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001755 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001756
Todd Fiala9be50492014-07-01 16:30:53 +00001757 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001758
1759 siginfo_t info;
1760 int ptrace_err;
1761 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1762 {
1763 if (log)
1764 {
1765 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1766 }
1767 return false;
1768 }
1769
1770 // If this is a thread exit, we won't get any more information.
1771 if (WIFEXITED(status))
1772 {
1773 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001774 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001775 return true;
1776 continue;
1777 }
1778
1779 assert(info.si_code == SI_USER);
1780 assert(WSTOPSIG(status) == SIGSTOP);
1781
1782 if (log)
1783 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1784 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1785 return true;
1786 }
1787 return false;
1788}
1789
Andrew Kaylor93132f52013-05-28 23:04:25 +00001790bool
1791ProcessMonitor::StopThread(lldb::tid_t tid)
1792{
1793 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1794
1795 // FIXME: Try to use tgkill or tkill
1796 int ret = tgkill(m_pid, tid, SIGSTOP);
1797 if (log)
1798 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1799
1800 // This can happen if a thread exited while we were trying to stop it. That's OK.
1801 // We'll get the signal for that later.
1802 if (ret < 0)
1803 return false;
1804
1805 // Wait for the thread to stop
1806 while (true)
1807 {
1808 int status = -1;
1809 if (log)
1810 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001811 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001812 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001813 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1814 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001815
Todd Fiala9be50492014-07-01 16:30:53 +00001816 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001817 {
1818 // If we got interrupted by a signal (in our process, not the
1819 // inferior) try again.
1820 if (errno == EINTR)
1821 continue;
1822 else
1823 return false; // This is bad, but there's nothing we can do.
1824 }
1825
1826 // If this is a thread exit, we won't get any more information.
1827 if (WIFEXITED(status))
1828 {
1829 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001830 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001831 return true;
1832 continue;
1833 }
1834
1835 siginfo_t info;
1836 int ptrace_err;
1837 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1838 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001839 // another signal causing a StopAllThreads may have been received
1840 // before wait_pid's group-stop was processed, handle it now
1841 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001842 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001843 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001844
Todd Fiala1b0539c2014-01-27 17:03:57 +00001845 if (log)
1846 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1847 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1848 if (!Resume(wait_pid, SIGSTOP)) {
1849 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1850 }
1851 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001852 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001853
1854 if (log)
1855 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001856 return false;
1857 }
1858
1859 // Handle events from other threads
1860 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001861 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1862 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001863
1864 ProcessMessage message;
1865 if (info.si_signo == SIGTRAP)
1866 message = MonitorSIGTRAP(this, &info, wait_pid);
1867 else
1868 message = MonitorSignal(this, &info, wait_pid);
1869
1870 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1871
1872 // When a new thread is created, we may get a SIGSTOP for the new thread
1873 // just before we get the SIGTRAP that we use to add the thread to our
1874 // process thread list. We don't need to worry about that signal here.
1875 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1876
1877 if (!thread)
1878 {
1879 m_process->SendMessage(message);
1880 continue;
1881 }
1882
1883 switch (message.GetKind())
1884 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00001885 case ProcessMessage::eExecMessage:
1886 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00001887 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001888 case ProcessMessage::eInvalidMessage:
1889 break;
1890
1891 // These need special handling because we don't want to send a
1892 // resume even if we already sent a SIGSTOP to this thread. In
1893 // this case the resume will cause the thread to disappear. It is
1894 // unlikely that we'll ever get eExitMessage here, but the same
1895 // reasoning applies.
1896 case ProcessMessage::eLimboMessage:
1897 case ProcessMessage::eExitMessage:
1898 if (log)
1899 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1900 // SendMessage will set the thread state as needed.
1901 m_process->SendMessage(message);
1902 // If this is the thread we're waiting for, stop waiting. Even
1903 // though this wasn't the signal we expected, it's the last
1904 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00001905 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001906 return true;
1907 break;
1908
Matt Kopecb2910442013-07-09 15:09:45 +00001909 case ProcessMessage::eSignalMessage:
1910 if (log)
1911 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1912 if (WSTOPSIG(status) == SIGSTOP)
1913 {
1914 m_process->AddThreadForInitialStopIfNeeded(tid);
1915 thread->SetState(lldb::eStateStopped);
1916 }
1917 else
1918 {
1919 m_process->SendMessage(message);
1920 // This isn't the stop we were expecting, but the thread is
1921 // stopped. SendMessage will handle processing of this event,
1922 // but we need to resume here to get the stop we are waiting
1923 // for (otherwise the thread will stop again immediately when
1924 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001925 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00001926 Resume(wait_pid, eResumeSignalNone);
1927 }
1928 break;
1929
Andrew Kaylor93132f52013-05-28 23:04:25 +00001930 case ProcessMessage::eSignalDeliveredMessage:
1931 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00001932 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
1933 WIFSTOPPED(status) &&
1934 WSTOPSIG(status) == SIGSTOP &&
1935 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001936 {
1937 if (log)
1938 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1939 thread->SetState(lldb::eStateStopped);
1940 return true;
1941 }
1942 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001943 case ProcessMessage::eBreakpointMessage:
1944 case ProcessMessage::eTraceMessage:
1945 case ProcessMessage::eWatchpointMessage:
1946 case ProcessMessage::eCrashMessage:
1947 case ProcessMessage::eNewThreadMessage:
1948 if (log)
1949 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1950 // SendMessage will set the thread state as needed.
1951 m_process->SendMessage(message);
1952 // This isn't the stop we were expecting, but the thread is
1953 // stopped. SendMessage will handle processing of this event,
1954 // but we need to resume here to get the stop we are waiting
1955 // for (otherwise the thread will stop again immediately when
1956 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00001957 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001958 Resume(wait_pid, eResumeSignalNone);
1959 break;
1960 }
1961 }
1962 return false;
1963}
1964
Stephen Wilson84ffe702011-03-30 15:55:52 +00001965ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001966ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001967{
1968 ProcessMessage::CrashReason reason;
1969 assert(info->si_signo == SIGSEGV);
1970
1971 reason = ProcessMessage::eInvalidCrashReason;
1972
Greg Clayton542e4072012-09-07 17:49:29 +00001973 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001974 {
1975 default:
1976 assert(false && "unexpected si_code for SIGSEGV");
1977 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001978 case SI_KERNEL:
1979 // Linux will occasionally send spurious SI_KERNEL codes.
1980 // (this is poorly documented in sigaction)
1981 // One way to get this is via unaligned SIMD loads.
1982 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1983 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001984 case SEGV_MAPERR:
1985 reason = ProcessMessage::eInvalidAddress;
1986 break;
1987 case SEGV_ACCERR:
1988 reason = ProcessMessage::ePrivilegedAddress;
1989 break;
1990 }
Greg Clayton542e4072012-09-07 17:49:29 +00001991
Stephen Wilson84ffe702011-03-30 15:55:52 +00001992 return reason;
1993}
1994
1995ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001996ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001997{
1998 ProcessMessage::CrashReason reason;
1999 assert(info->si_signo == SIGILL);
2000
2001 reason = ProcessMessage::eInvalidCrashReason;
2002
2003 switch (info->si_code)
2004 {
2005 default:
2006 assert(false && "unexpected si_code for SIGILL");
2007 break;
2008 case ILL_ILLOPC:
2009 reason = ProcessMessage::eIllegalOpcode;
2010 break;
2011 case ILL_ILLOPN:
2012 reason = ProcessMessage::eIllegalOperand;
2013 break;
2014 case ILL_ILLADR:
2015 reason = ProcessMessage::eIllegalAddressingMode;
2016 break;
2017 case ILL_ILLTRP:
2018 reason = ProcessMessage::eIllegalTrap;
2019 break;
2020 case ILL_PRVOPC:
2021 reason = ProcessMessage::ePrivilegedOpcode;
2022 break;
2023 case ILL_PRVREG:
2024 reason = ProcessMessage::ePrivilegedRegister;
2025 break;
2026 case ILL_COPROC:
2027 reason = ProcessMessage::eCoprocessorError;
2028 break;
2029 case ILL_BADSTK:
2030 reason = ProcessMessage::eInternalStackError;
2031 break;
2032 }
2033
2034 return reason;
2035}
2036
2037ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002038ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002039{
2040 ProcessMessage::CrashReason reason;
2041 assert(info->si_signo == SIGFPE);
2042
2043 reason = ProcessMessage::eInvalidCrashReason;
2044
2045 switch (info->si_code)
2046 {
2047 default:
2048 assert(false && "unexpected si_code for SIGFPE");
2049 break;
2050 case FPE_INTDIV:
2051 reason = ProcessMessage::eIntegerDivideByZero;
2052 break;
2053 case FPE_INTOVF:
2054 reason = ProcessMessage::eIntegerOverflow;
2055 break;
2056 case FPE_FLTDIV:
2057 reason = ProcessMessage::eFloatDivideByZero;
2058 break;
2059 case FPE_FLTOVF:
2060 reason = ProcessMessage::eFloatOverflow;
2061 break;
2062 case FPE_FLTUND:
2063 reason = ProcessMessage::eFloatUnderflow;
2064 break;
2065 case FPE_FLTRES:
2066 reason = ProcessMessage::eFloatInexactResult;
2067 break;
2068 case FPE_FLTINV:
2069 reason = ProcessMessage::eFloatInvalidOperation;
2070 break;
2071 case FPE_FLTSUB:
2072 reason = ProcessMessage::eFloatSubscriptRange;
2073 break;
2074 }
2075
2076 return reason;
2077}
2078
2079ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002080ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002081{
2082 ProcessMessage::CrashReason reason;
2083 assert(info->si_signo == SIGBUS);
2084
2085 reason = ProcessMessage::eInvalidCrashReason;
2086
2087 switch (info->si_code)
2088 {
2089 default:
2090 assert(false && "unexpected si_code for SIGBUS");
2091 break;
2092 case BUS_ADRALN:
2093 reason = ProcessMessage::eIllegalAlignment;
2094 break;
2095 case BUS_ADRERR:
2096 reason = ProcessMessage::eIllegalAddress;
2097 break;
2098 case BUS_OBJERR:
2099 reason = ProcessMessage::eHardwareError;
2100 break;
2101 }
2102
2103 return reason;
2104}
2105
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002106void
Johnny Chen25e68e32011-06-14 19:19:50 +00002107ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002108{
Stephen Wilson570243b2011-01-19 01:37:06 +00002109 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002110
Stephen Wilson570243b2011-01-19 01:37:06 +00002111 // We are finised with the arguments and are ready to go. Sync with the
2112 // parent thread and start serving operations on the inferior.
2113 sem_post(&args->m_semaphore);
2114
Michael Sartain704bf892013-10-09 01:28:57 +00002115 for(;;)
2116 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002117 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002118 if (sem_wait(&monitor->m_operation_pending))
2119 {
2120 if (errno == EINTR)
2121 continue;
2122 assert(false && "Unexpected errno from sem_wait");
2123 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002124
Daniel Malea1efb4182013-09-16 23:12:18 +00002125 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002126
Daniel Malea1efb4182013-09-16 23:12:18 +00002127 // notify calling thread that operation is complete
2128 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002129 }
2130}
2131
2132void
2133ProcessMonitor::DoOperation(Operation *op)
2134{
Daniel Malea1efb4182013-09-16 23:12:18 +00002135 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002136
Daniel Malea1efb4182013-09-16 23:12:18 +00002137 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002138
Daniel Malea1efb4182013-09-16 23:12:18 +00002139 // notify operation thread that an operation is ready to be processed
2140 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002141
Daniel Malea1efb4182013-09-16 23:12:18 +00002142 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002143 while (sem_wait(&m_operation_done))
2144 {
2145 if (errno == EINTR)
2146 continue;
2147 assert(false && "Unexpected errno from sem_wait");
2148 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002149}
2150
2151size_t
2152ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2153 Error &error)
2154{
2155 size_t result;
2156 ReadOperation op(vm_addr, buf, size, error, result);
2157 DoOperation(&op);
2158 return result;
2159}
2160
2161size_t
2162ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2163 lldb_private::Error &error)
2164{
2165 size_t result;
2166 WriteOperation op(vm_addr, buf, size, error, result);
2167 DoOperation(&op);
2168 return result;
2169}
2170
2171bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002172ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002173 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002174{
2175 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002176 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002177 DoOperation(&op);
2178 return result;
2179}
2180
2181bool
Matt Kopec7de48462013-03-06 17:20:48 +00002182ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002183 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002184{
2185 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002186 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002187 DoOperation(&op);
2188 return result;
2189}
2190
2191bool
Matt Kopec7de48462013-03-06 17:20:48 +00002192ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002193{
2194 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002195 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002196 DoOperation(&op);
2197 return result;
2198}
2199
2200bool
Matt Kopec7de48462013-03-06 17:20:48 +00002201ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002202{
2203 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002204 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002205 DoOperation(&op);
2206 return result;
2207}
2208
2209bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002210ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2211{
2212 bool result;
2213 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2214 DoOperation(&op);
2215 return result;
2216}
2217
2218bool
Matt Kopec7de48462013-03-06 17:20:48 +00002219ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002220{
2221 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002222 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002223 DoOperation(&op);
2224 return result;
2225}
2226
2227bool
Matt Kopec7de48462013-03-06 17:20:48 +00002228ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002229{
2230 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002231 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002232 DoOperation(&op);
2233 return result;
2234}
2235
2236bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002237ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2238{
2239 bool result;
2240 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2241 DoOperation(&op);
2242 return result;
2243}
2244
2245bool
Richard Mitton0a558352013-10-17 21:14:00 +00002246ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2247{
2248 bool result;
2249 ReadThreadPointerOperation op(tid, &value, result);
2250 DoOperation(&op);
2251 return result;
2252}
2253
2254bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002255ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002256{
2257 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002258 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2259
2260 if (log)
2261 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2262 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002263 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002264 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002265 if (log)
2266 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002267 return result;
2268}
2269
2270bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002271ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002272{
2273 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002274 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002275 DoOperation(&op);
2276 return result;
2277}
2278
2279bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002280ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002281{
Ed Maste4e0999b2014-04-01 18:14:06 +00002282 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002283}
2284
2285bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002286ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002287{
2288 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002289 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002290 DoOperation(&op);
2291 return result;
2292}
2293
2294bool
2295ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2296{
2297 bool result;
2298 EventMessageOperation op(tid, message, result);
2299 DoOperation(&op);
2300 return result;
2301}
2302
Greg Clayton743ecf42012-10-16 20:20:18 +00002303lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002304ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002305{
Greg Clayton28041352011-11-29 20:50:10 +00002306 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002307 if (tid != LLDB_INVALID_THREAD_ID)
2308 {
2309 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002310 DoOperation(&op);
2311 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002312 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002313}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002314
2315bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002316ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2317{
Peter Collingbourne62343202011-06-14 03:55:54 +00002318 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002319
2320 if (target_fd == -1)
2321 return false;
2322
Peter Collingbourne62343202011-06-14 03:55:54 +00002323 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002324}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002325
2326void
2327ProcessMonitor::StopMonitoringChildProcess()
2328{
2329 lldb::thread_result_t thread_result;
2330
Stephen Wilsond4182f42011-02-09 20:10:35 +00002331 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002332 {
2333 Host::ThreadCancel(m_monitor_thread, NULL);
2334 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2335 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2336 }
2337}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002338
2339void
2340ProcessMonitor::StopMonitor()
2341{
2342 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002343 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002344 sem_destroy(&m_operation_pending);
2345 sem_destroy(&m_operation_done);
2346
Andrew Kaylor5e268992013-09-14 00:17:31 +00002347 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2348 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2349 // the descriptor to a ConnectionFileDescriptor object. Consequently
2350 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002351}
2352
2353void
Greg Clayton743ecf42012-10-16 20:20:18 +00002354ProcessMonitor::StopOpThread()
2355{
2356 lldb::thread_result_t result;
2357
2358 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2359 return;
2360
2361 Host::ThreadCancel(m_operation_thread, NULL);
2362 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002363 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002364}