blob: bda0cb33097c6bf921fd38471bcd11ac25da3af6 [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
18#include <sys/ptrace.h>
19#include <sys/socket.h>
Andrew Kaylor93132f52013-05-28 23:04:25 +000020#include <sys/syscall.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000021#include <sys/types.h>
Richard Mitton0a558352013-10-17 21:14:00 +000022#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include <sys/wait.h>
24
25// C++ Includes
26// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000027#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000029#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Core/Scalar.h"
31#include "lldb/Host/Host.h"
32#include "lldb/Target/Thread.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Utility/PseudoTerminal.h"
35
Johnny Chen30213ff2012-01-05 19:17:38 +000036#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000038#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
40
Greg Clayton386ff182011-11-05 01:09:16 +000041#define DEBUG_PTRACE_MAXBYTES 20
42
Matt Kopec58c0b962013-03-20 20:34:35 +000043// Support ptrace extensions even when compiled without required kernel support
44#ifndef PTRACE_GETREGSET
45 #define PTRACE_GETREGSET 0x4204
46#endif
47#ifndef PTRACE_SETREGSET
48 #define PTRACE_SETREGSET 0x4205
49#endif
Richard Mitton0a558352013-10-17 21:14:00 +000050#ifndef PTRACE_GET_THREAD_AREA
51 #define PTRACE_GET_THREAD_AREA 25
52#endif
53#ifndef PTRACE_ARCH_PRCTL
54 #define PTRACE_ARCH_PRCTL 30
55#endif
56#ifndef ARCH_GET_FS
57 #define ARCH_SET_GS 0x1001
58 #define ARCH_SET_FS 0x1002
59 #define ARCH_GET_FS 0x1003
60 #define ARCH_GET_GS 0x1004
61#endif
62
Matt Kopec58c0b962013-03-20 20:34:35 +000063
Matt Kopece9ea0da2013-05-07 19:29:28 +000064// Support hardware breakpoints in case it has not been defined
65#ifndef TRAP_HWBKPT
66 #define TRAP_HWBKPT 4
67#endif
68
Andrew Kaylor93132f52013-05-28 23:04:25 +000069// Try to define a macro to encapsulate the tgkill syscall
70// fall back on kill() if tgkill isn't available
71#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
72
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073using namespace lldb_private;
74
Johnny Chen0d5f2d42011-10-18 18:09:30 +000075// FIXME: this code is host-dependent with respect to types and
76// endianness and needs to be fixed. For example, lldb::addr_t is
77// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
78// 32-bit pointer arguments. This code uses casts to work around the
79// problem.
80
81// We disable the tracing of ptrace calls for integration builds to
82// avoid the additional indirection and checks.
83#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
84
Greg Clayton386ff182011-11-05 01:09:16 +000085static void
86DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
87{
88 uint8_t *ptr = (uint8_t *)bytes;
89 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
90 for(uint32_t i=0; i<loop_count; i++)
91 {
92 s.Printf ("[%x]", *ptr);
93 ptr++;
94 }
95}
96
Matt Kopec58c0b962013-03-20 20:34:35 +000097static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000098{
99 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000100 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000101 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000102
103 if (verbose_log)
104 {
105 switch(req)
106 {
107 case PTRACE_POKETEXT:
108 {
109 DisplayBytes(buf, &data, 8);
110 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
111 break;
112 }
Greg Clayton542e4072012-09-07 17:49:29 +0000113 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000114 {
115 DisplayBytes(buf, &data, 8);
116 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, &data, 8);
122 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
123 break;
124 }
Greg Clayton542e4072012-09-07 17:49:29 +0000125 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000126 {
Matt Kopec7de48462013-03-06 17:20:48 +0000127 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000128 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
129 break;
130 }
131 case PTRACE_SETFPREGS:
132 {
Matt Kopec7de48462013-03-06 17:20:48 +0000133 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000134 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
135 break;
136 }
Greg Clayton542e4072012-09-07 17:49:29 +0000137 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000138 {
139 DisplayBytes(buf, data, sizeof(siginfo_t));
140 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
141 break;
142 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000143 case PTRACE_SETREGSET:
144 {
145 // Extract iov_base from data, which is a pointer to the struct IOVEC
146 DisplayBytes(buf, *(void **)data, data_size);
147 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
148 break;
149 }
Greg Clayton386ff182011-11-05 01:09:16 +0000150 default:
151 {
152 }
153 }
154 }
155}
156
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000157// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000158// 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 +0000159extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000160PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000161 const char* reqName, const char* file, int line)
162{
Greg Clayton386ff182011-11-05 01:09:16 +0000163 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000164
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000165 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000166
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000167 if (log)
Matt Kopec58c0b962013-03-20 20:34:35 +0000168 log->Printf("ptrace(%s, %lu, %p, %p, %zu) called from file %s line %d",
Matt Kopec7de48462013-03-06 17:20:48 +0000169 reqName, pid, addr, data, data_size, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000170
Matt Kopec7de48462013-03-06 17:20:48 +0000171 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000172
173 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000174 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
175 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
176 else
177 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000178
Matt Kopec7de48462013-03-06 17:20:48 +0000179 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000180
Matt Kopec7de48462013-03-06 17:20:48 +0000181 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000182 {
183 const char* str;
184 switch (errno)
185 {
186 case ESRCH: str = "ESRCH"; break;
187 case EINVAL: str = "EINVAL"; break;
188 case EBUSY: str = "EBUSY"; break;
189 case EPERM: str = "EPERM"; break;
190 default: str = "<unknown>";
191 }
192 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
193 }
194
195 return result;
196}
197
Matt Kopec7de48462013-03-06 17:20:48 +0000198// Wrapper for ptrace when logging is not required.
199// Sets errno to 0 prior to calling ptrace.
200extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000201PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000202{
Matt Kopec58c0b962013-03-20 20:34:35 +0000203 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000204 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000205 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
206 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
207 else
208 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000209 return result;
210}
211
212#define PTRACE(req, pid, addr, data, data_size) \
213 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000214#else
Matt Kopec7de48462013-03-06 17:20:48 +0000215 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000216#endif
217
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218//------------------------------------------------------------------------------
219// Static implementations of ProcessMonitor::ReadMemory and
220// ProcessMonitor::WriteMemory. This enables mutual recursion between these
221// functions without needed to go thru the thread funnel.
222
223static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000224DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000225 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
226{
Greg Clayton542e4072012-09-07 17:49:29 +0000227 // ptrace word size is determined by the host, not the child
228 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 unsigned char *dst = static_cast<unsigned char*>(buf);
230 size_t bytes_read;
231 size_t remainder;
232 long data;
233
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000235 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000236 ProcessPOSIXLog::IncNestLevel();
237 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000238 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000239 pid, word_size, (void*)vm_addr, buf, size);
240
241 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000242 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
243 {
244 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000245 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
246 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000247 {
248 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000249 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000250 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 return bytes_read;
252 }
253
254 remainder = size - bytes_read;
255 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000256
257 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000258 for (unsigned i = 0; i < remainder; ++i)
259 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
262 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
263 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
264 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000265 {
266 uintptr_t print_dst = 0;
267 // Format bytes from data by moving into print_dst for log output
268 for (unsigned i = 0; i < remainder; ++i)
269 print_dst |= (((data >> i*8) & 0xFF) << i*8);
270 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
271 (void*)vm_addr, print_dst, (unsigned long)data);
272 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000273
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274 vm_addr += word_size;
275 dst += word_size;
276 }
277
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000278 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000279 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 return bytes_read;
281}
282
283static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000284DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
286{
Greg Clayton542e4072012-09-07 17:49:29 +0000287 // ptrace word size is determined by the host, not the child
288 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000289 const unsigned char *src = static_cast<const unsigned char*>(buf);
290 size_t bytes_written = 0;
291 size_t remainder;
292
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000293 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000294 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000295 ProcessPOSIXLog::IncNestLevel();
296 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000297 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 pid, word_size, (void*)vm_addr, buf, size);
299
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000300 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
301 {
302 remainder = size - bytes_written;
303 remainder = remainder > word_size ? word_size : remainder;
304
305 if (remainder == word_size)
306 {
307 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000308 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000309 for (unsigned i = 0; i < word_size; ++i)
310 data |= (unsigned long)src[i] << i*8;
311
Johnny Chen30213ff2012-01-05 19:17:38 +0000312 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
313 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
314 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
315 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000316 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
317 (void*)vm_addr, *(unsigned long*)src, data);
318
Matt Kopec7de48462013-03-06 17:20:48 +0000319 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000320 {
321 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000323 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000324 return bytes_written;
325 }
326 }
327 else
328 {
329 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000330 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000332 {
333 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000334 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000335 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000336 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000337
338 memcpy(buff, src, remainder);
339
Greg Clayton542e4072012-09-07 17:49:29 +0000340 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000341 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 {
343 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000344 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 }
347
Johnny Chen30213ff2012-01-05 19:17:38 +0000348 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
349 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
350 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
351 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000352 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
353 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000354 }
355
356 vm_addr += word_size;
357 src += word_size;
358 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000359 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000360 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000361 return bytes_written;
362}
363
Stephen Wilson26977162011-03-23 02:14:42 +0000364// Simple helper function to ensure flags are enabled on the given file
365// descriptor.
366static bool
367EnsureFDFlags(int fd, int flags, Error &error)
368{
369 int status;
370
371 if ((status = fcntl(fd, F_GETFL)) == -1)
372 {
373 error.SetErrorToErrno();
374 return false;
375 }
376
377 if (fcntl(fd, F_SETFL, status | flags) == -1)
378 {
379 error.SetErrorToErrno();
380 return false;
381 }
382
383 return true;
384}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000385
386//------------------------------------------------------------------------------
387/// @class Operation
388/// @brief Represents a ProcessMonitor operation.
389///
390/// Under Linux, it is not possible to ptrace() from any other thread but the
391/// one that spawned or attached to the process from the start. Therefore, when
392/// a ProcessMonitor is asked to deliver or change the state of an inferior
393/// process the operation must be "funneled" to a specific thread to perform the
394/// task. The Operation class provides an abstract base for all services the
395/// ProcessMonitor must perform via the single virtual function Execute, thus
396/// encapsulating the code that needs to run in the privileged context.
397class Operation
398{
399public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000400 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000401 virtual void Execute(ProcessMonitor *monitor) = 0;
402};
403
404//------------------------------------------------------------------------------
405/// @class ReadOperation
406/// @brief Implements ProcessMonitor::ReadMemory.
407class ReadOperation : public Operation
408{
409public:
410 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
411 Error &error, size_t &result)
412 : m_addr(addr), m_buff(buff), m_size(size),
413 m_error(error), m_result(result)
414 { }
415
416 void Execute(ProcessMonitor *monitor);
417
418private:
419 lldb::addr_t m_addr;
420 void *m_buff;
421 size_t m_size;
422 Error &m_error;
423 size_t &m_result;
424};
425
426void
427ReadOperation::Execute(ProcessMonitor *monitor)
428{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000429 lldb::pid_t pid = monitor->GetPID();
430
Greg Clayton542e4072012-09-07 17:49:29 +0000431 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000432}
433
434//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000435/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000436/// @brief Implements ProcessMonitor::WriteMemory.
437class WriteOperation : public Operation
438{
439public:
440 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
441 Error &error, size_t &result)
442 : m_addr(addr), m_buff(buff), m_size(size),
443 m_error(error), m_result(result)
444 { }
445
446 void Execute(ProcessMonitor *monitor);
447
448private:
449 lldb::addr_t m_addr;
450 const void *m_buff;
451 size_t m_size;
452 Error &m_error;
453 size_t &m_result;
454};
455
456void
457WriteOperation::Execute(ProcessMonitor *monitor)
458{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000459 lldb::pid_t pid = monitor->GetPID();
460
Greg Clayton542e4072012-09-07 17:49:29 +0000461 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000462}
463
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000464
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465//------------------------------------------------------------------------------
466/// @class ReadRegOperation
467/// @brief Implements ProcessMonitor::ReadRegisterValue.
468class ReadRegOperation : public Operation
469{
470public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000471 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000472 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000473 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000474 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000475 { }
476
477 void Execute(ProcessMonitor *monitor);
478
479private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000480 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000481 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000482 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000483 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000484 bool &m_result;
485};
486
487void
488ReadRegOperation::Execute(ProcessMonitor *monitor)
489{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000490 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000491
492 // Set errno to zero so that we can detect a failed peek.
493 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000494 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
495 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000496 m_result = false;
497 else
498 {
499 m_value = data;
500 m_result = true;
501 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000502 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000503 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000504 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000505}
506
507//------------------------------------------------------------------------------
508/// @class WriteRegOperation
509/// @brief Implements ProcessMonitor::WriteRegisterValue.
510class WriteRegOperation : public Operation
511{
512public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000513 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000514 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000515 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000516 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000517 { }
518
519 void Execute(ProcessMonitor *monitor);
520
521private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000522 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000523 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000524 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000525 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000526 bool &m_result;
527};
528
529void
530WriteRegOperation::Execute(ProcessMonitor *monitor)
531{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000532 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000533 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000534
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000535#if __WORDSIZE == 32
536 buf = (void*) m_value.GetAsUInt32();
537#else
538 buf = (void*) m_value.GetAsUInt64();
539#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000540
541 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000542 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000543 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000544 m_result = false;
545 else
546 m_result = true;
547}
548
549//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000550/// @class ReadGPROperation
551/// @brief Implements ProcessMonitor::ReadGPR.
552class ReadGPROperation : public Operation
553{
554public:
Matt Kopec7de48462013-03-06 17:20:48 +0000555 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
556 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000557 { }
558
559 void Execute(ProcessMonitor *monitor);
560
561private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000562 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000563 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000564 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000565 bool &m_result;
566};
567
568void
569ReadGPROperation::Execute(ProcessMonitor *monitor)
570{
Matt Kopec7de48462013-03-06 17:20:48 +0000571 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000572 m_result = false;
573 else
574 m_result = true;
575}
576
577//------------------------------------------------------------------------------
578/// @class ReadFPROperation
579/// @brief Implements ProcessMonitor::ReadFPR.
580class ReadFPROperation : public Operation
581{
582public:
Matt Kopec7de48462013-03-06 17:20:48 +0000583 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
584 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000585 { }
586
587 void Execute(ProcessMonitor *monitor);
588
589private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000590 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000591 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000592 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000593 bool &m_result;
594};
595
596void
597ReadFPROperation::Execute(ProcessMonitor *monitor)
598{
Matt Kopec7de48462013-03-06 17:20:48 +0000599 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000600 m_result = false;
601 else
602 m_result = true;
603}
604
605//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000606/// @class ReadRegisterSetOperation
607/// @brief Implements ProcessMonitor::ReadRegisterSet.
608class ReadRegisterSetOperation : public Operation
609{
610public:
611 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
612 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
613 { }
614
615 void Execute(ProcessMonitor *monitor);
616
617private:
618 lldb::tid_t m_tid;
619 void *m_buf;
620 size_t m_buf_size;
621 const unsigned int m_regset;
622 bool &m_result;
623};
624
625void
626ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
627{
628 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
629 m_result = false;
630 else
631 m_result = true;
632}
633
634//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000635/// @class WriteGPROperation
636/// @brief Implements ProcessMonitor::WriteGPR.
637class WriteGPROperation : public Operation
638{
639public:
Matt Kopec7de48462013-03-06 17:20:48 +0000640 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
641 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000642 { }
643
644 void Execute(ProcessMonitor *monitor);
645
646private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000647 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000648 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000649 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000650 bool &m_result;
651};
652
653void
654WriteGPROperation::Execute(ProcessMonitor *monitor)
655{
Matt Kopec7de48462013-03-06 17:20:48 +0000656 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000657 m_result = false;
658 else
659 m_result = true;
660}
661
662//------------------------------------------------------------------------------
663/// @class WriteFPROperation
664/// @brief Implements ProcessMonitor::WriteFPR.
665class WriteFPROperation : public Operation
666{
667public:
Matt Kopec7de48462013-03-06 17:20:48 +0000668 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
669 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000670 { }
671
672 void Execute(ProcessMonitor *monitor);
673
674private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000675 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000676 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000677 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000678 bool &m_result;
679};
680
681void
682WriteFPROperation::Execute(ProcessMonitor *monitor)
683{
Matt Kopec7de48462013-03-06 17:20:48 +0000684 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000685 m_result = false;
686 else
687 m_result = true;
688}
689
690//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000691/// @class WriteRegisterSetOperation
692/// @brief Implements ProcessMonitor::WriteRegisterSet.
693class WriteRegisterSetOperation : public Operation
694{
695public:
696 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
697 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
698 { }
699
700 void Execute(ProcessMonitor *monitor);
701
702private:
703 lldb::tid_t m_tid;
704 void *m_buf;
705 size_t m_buf_size;
706 const unsigned int m_regset;
707 bool &m_result;
708};
709
710void
711WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
712{
713 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
714 m_result = false;
715 else
716 m_result = true;
717}
718
719//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000720/// @class ReadThreadPointerOperation
721/// @brief Implements ProcessMonitor::ReadThreadPointer.
722class ReadThreadPointerOperation : public Operation
723{
724public:
725 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
726 : m_tid(tid), m_addr(addr), m_result(result)
727 { }
728
729 void Execute(ProcessMonitor *monitor);
730
731private:
732 lldb::tid_t m_tid;
733 lldb::addr_t *m_addr;
734 bool &m_result;
735};
736
737void
738ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
739{
740 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
741 if (log)
742 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
743
744 // The process for getting the thread area on Linux is
745 // somewhat... obscure. There's several different ways depending on
746 // what arch you're on, and what kernel version you have.
747
748 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
749 switch(arch.GetMachine())
750 {
751 case llvm::Triple::x86:
752 {
753 // Find the GS register location for our host architecture.
754 size_t gs_user_offset = offsetof(struct user, regs);
755#ifdef __x86_64__
756 gs_user_offset += offsetof(struct user_regs_struct, gs);
757#endif
758#ifdef __i386__
759 gs_user_offset += offsetof(struct user_regs_struct, xgs);
760#endif
761
762 // Read the GS register value to get the selector.
763 errno = 0;
764 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
765 if (errno)
766 {
767 m_result = false;
768 break;
769 }
770
771 // Read the LDT base for that selector.
772 uint32_t tmp[4];
773 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
774 *m_addr = tmp[1];
775 break;
776 }
777 case llvm::Triple::x86_64:
778 // Read the FS register base.
779 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
780 break;
781 default:
782 m_result = false;
783 break;
784 }
785}
786
787//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000788/// @class ResumeOperation
789/// @brief Implements ProcessMonitor::Resume.
790class ResumeOperation : public Operation
791{
792public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000793 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
794 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000795
796 void Execute(ProcessMonitor *monitor);
797
798private:
799 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000800 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000801 bool &m_result;
802};
803
804void
805ResumeOperation::Execute(ProcessMonitor *monitor)
806{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000807 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000808
809 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
810 data = m_signo;
811
Matt Kopec7de48462013-03-06 17:20:48 +0000812 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000813 {
814 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
815
816 if (log)
817 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000818 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000819 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000820 else
821 m_result = true;
822}
823
824//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000825/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000826/// @brief Implements ProcessMonitor::SingleStep.
827class SingleStepOperation : public Operation
828{
829public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000830 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
831 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000832
833 void Execute(ProcessMonitor *monitor);
834
835private:
836 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000837 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 bool &m_result;
839};
840
841void
842SingleStepOperation::Execute(ProcessMonitor *monitor)
843{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000844 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000845
846 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
847 data = m_signo;
848
Matt Kopec7de48462013-03-06 17:20:48 +0000849 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000850 m_result = false;
851 else
852 m_result = true;
853}
854
855//------------------------------------------------------------------------------
856/// @class SiginfoOperation
857/// @brief Implements ProcessMonitor::GetSignalInfo.
858class SiginfoOperation : public Operation
859{
860public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000861 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
862 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000863
864 void Execute(ProcessMonitor *monitor);
865
866private:
867 lldb::tid_t m_tid;
868 void *m_info;
869 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000870 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000871};
872
873void
874SiginfoOperation::Execute(ProcessMonitor *monitor)
875{
Matt Kopec7de48462013-03-06 17:20:48 +0000876 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000877 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000878 m_err = errno;
879 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000880 else
881 m_result = true;
882}
883
884//------------------------------------------------------------------------------
885/// @class EventMessageOperation
886/// @brief Implements ProcessMonitor::GetEventMessage.
887class EventMessageOperation : public Operation
888{
889public:
890 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
891 : m_tid(tid), m_message(message), m_result(result) { }
892
893 void Execute(ProcessMonitor *monitor);
894
895private:
896 lldb::tid_t m_tid;
897 unsigned long *m_message;
898 bool &m_result;
899};
900
901void
902EventMessageOperation::Execute(ProcessMonitor *monitor)
903{
Matt Kopec7de48462013-03-06 17:20:48 +0000904 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000905 m_result = false;
906 else
907 m_result = true;
908}
909
910//------------------------------------------------------------------------------
911/// @class KillOperation
912/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
913class KillOperation : public Operation
914{
915public:
916 KillOperation(bool &result) : m_result(result) { }
917
918 void Execute(ProcessMonitor *monitor);
919
920private:
921 bool &m_result;
922};
923
924void
925KillOperation::Execute(ProcessMonitor *monitor)
926{
927 lldb::pid_t pid = monitor->GetPID();
928
Matt Kopec7de48462013-03-06 17:20:48 +0000929 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000930 m_result = false;
931 else
932 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000933}
934
Greg Clayton28041352011-11-29 20:50:10 +0000935//------------------------------------------------------------------------------
936/// @class KillOperation
937/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
938class DetachOperation : public Operation
939{
940public:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000941 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +0000942
943 void Execute(ProcessMonitor *monitor);
944
945private:
Matt Kopec085d6ce2013-05-31 22:00:07 +0000946 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +0000947 Error &m_error;
948};
949
950void
951DetachOperation::Execute(ProcessMonitor *monitor)
952{
Matt Kopec085d6ce2013-05-31 22:00:07 +0000953 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +0000954 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +0000955}
956
Johnny Chen25e68e32011-06-14 19:19:50 +0000957ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
958 : m_monitor(monitor)
959{
960 sem_init(&m_semaphore, 0, 0);
961}
962
963ProcessMonitor::OperationArgs::~OperationArgs()
964{
965 sem_destroy(&m_semaphore);
966}
967
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
969 lldb_private::Module *module,
970 char const **argv,
971 char const **envp,
972 const char *stdin_path,
973 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000974 const char *stderr_path,
975 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000976 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000977 m_module(module),
978 m_argv(argv),
979 m_envp(envp),
980 m_stdin_path(stdin_path),
981 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000982 m_stderr_path(stderr_path),
983 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000984
985ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000986{ }
987
988ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
989 lldb::pid_t pid)
990 : OperationArgs(monitor), m_pid(pid) { }
991
992ProcessMonitor::AttachArgs::~AttachArgs()
993{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000994
995//------------------------------------------------------------------------------
996/// The basic design of the ProcessMonitor is built around two threads.
997///
998/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
999/// for changes in the debugee state. When a change is detected a
1000/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1001/// "drives" state changes in the debugger.
1002///
1003/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001004/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001005/// operations such as register reads/writes, stepping, etc. See the comments
1006/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001007ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001008 Module *module,
1009 const char *argv[],
1010 const char *envp[],
1011 const char *stdin_path,
1012 const char *stdout_path,
1013 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001014 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001015 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001016 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001018 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001019 m_pid(LLDB_INVALID_PROCESS_ID),
1020 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001021 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001022{
Daniel Malea1efb4182013-09-16 23:12:18 +00001023 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1024 stdin_path, stdout_path, stderr_path,
1025 working_dir));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001026
Daniel Malea1efb4182013-09-16 23:12:18 +00001027 sem_init(&m_operation_pending, 0, 0);
1028 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001029
Johnny Chen25e68e32011-06-14 19:19:50 +00001030 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001031 if (!error.Success())
1032 return;
1033
1034WAIT_AGAIN:
1035 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001036 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001037 {
1038 if (errno == EINTR)
1039 goto WAIT_AGAIN;
1040 else
1041 {
1042 error.SetErrorToErrno();
1043 return;
1044 }
1045 }
1046
1047 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001048 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001049 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001050 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001051 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001052 return;
1053 }
1054
1055 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001056 m_monitor_thread = Host::StartMonitoringChildProcess(
1057 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +00001058 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001059 {
1060 error.SetErrorToGenericError();
1061 error.SetErrorString("Process launch failed.");
1062 return;
1063 }
1064}
1065
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001066ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001067 lldb::pid_t pid,
1068 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001069 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001070 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001071 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001072 m_pid(LLDB_INVALID_PROCESS_ID),
1073 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001074 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001075{
Daniel Malea1efb4182013-09-16 23:12:18 +00001076 sem_init(&m_operation_pending, 0, 0);
1077 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001078
Daniel Malea1efb4182013-09-16 23:12:18 +00001079 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001080
1081 StartAttachOpThread(args.get(), error);
1082 if (!error.Success())
1083 return;
1084
1085WAIT_AGAIN:
1086 // Wait for the operation thread to initialize.
1087 if (sem_wait(&args->m_semaphore))
1088 {
1089 if (errno == EINTR)
1090 goto WAIT_AGAIN;
1091 else
1092 {
1093 error.SetErrorToErrno();
1094 return;
1095 }
1096 }
1097
Greg Clayton743ecf42012-10-16 20:20:18 +00001098 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001099 if (!args->m_error.Success())
1100 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001101 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001102 error = args->m_error;
1103 return;
1104 }
1105
1106 // Finally, start monitoring the child process for change in state.
1107 m_monitor_thread = Host::StartMonitoringChildProcess(
1108 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1109 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1110 {
1111 error.SetErrorToGenericError();
1112 error.SetErrorString("Process attach failed.");
1113 return;
1114 }
1115}
1116
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001117ProcessMonitor::~ProcessMonitor()
1118{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001119 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001120}
1121
1122//------------------------------------------------------------------------------
1123// Thread setup and tear down.
1124void
Johnny Chen25e68e32011-06-14 19:19:50 +00001125ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001126{
1127 static const char *g_thread_name = "lldb.process.linux.operation";
1128
Stephen Wilsond4182f42011-02-09 20:10:35 +00001129 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130 return;
1131
1132 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001133 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001134}
1135
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001136void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001137ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138{
1139 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1140
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001141 if (!Launch(args)) {
1142 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001143 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001144 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001145
Stephen Wilson570243b2011-01-19 01:37:06 +00001146 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001147 return NULL;
1148}
1149
1150bool
1151ProcessMonitor::Launch(LaunchArgs *args)
1152{
1153 ProcessMonitor *monitor = args->m_monitor;
1154 ProcessLinux &process = monitor->GetProcess();
1155 const char **argv = args->m_argv;
1156 const char **envp = args->m_envp;
1157 const char *stdin_path = args->m_stdin_path;
1158 const char *stdout_path = args->m_stdout_path;
1159 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001160 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001161
1162 lldb_utility::PseudoTerminal terminal;
1163 const size_t err_len = 1024;
1164 char err_str[err_len];
1165 lldb::pid_t pid;
1166
1167 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001168 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169
Stephen Wilson57740ec2011-01-15 00:12:41 +00001170 // Propagate the environment if one is not supplied.
1171 if (envp == NULL || envp[0] == NULL)
1172 envp = const_cast<const char **>(environ);
1173
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001174 // Pseudo terminal setup.
1175 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
1176 {
1177 args->m_error.SetErrorToGenericError();
1178 args->m_error.SetErrorString("Could not open controlling TTY.");
1179 goto FINISH;
1180 }
1181
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001182 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001183 {
1184 args->m_error.SetErrorToGenericError();
1185 args->m_error.SetErrorString("Process fork failed.");
1186 goto FINISH;
1187 }
1188
Peter Collingbourne6a520222011-06-14 03:55:58 +00001189 // Recognized child exit status codes.
1190 enum {
1191 ePtraceFailed = 1,
1192 eDupStdinFailed,
1193 eDupStdoutFailed,
1194 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001195 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001196 eExecFailed,
1197 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001198 };
1199
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001200 // Child process.
1201 if (pid == 0)
1202 {
1203 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001204 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001205 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001206
1207 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001208 if (setgid(getgid()) != 0)
1209 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001210
1211 // Let us have our own process group.
1212 setpgid(0, 0);
1213
Greg Clayton710dd5a2011-01-08 20:28:42 +00001214 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001215 //
1216 // FIXME: If two or more of the paths are the same we needlessly open
1217 // the same file multiple times.
1218 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001219 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001220 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001221
1222 if (stdout_path != NULL && stdout_path[0])
1223 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001224 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001225
1226 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001227 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001228 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001229
Daniel Malea6217d2a2013-01-08 14:49:22 +00001230 // Change working directory
1231 if (working_dir != NULL && working_dir[0])
1232 if (0 != ::chdir(working_dir))
1233 exit(eChdirFailed);
1234
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001235 // Execute. We should never return.
1236 execve(argv[0],
1237 const_cast<char *const *>(argv),
1238 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001239 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001240 }
1241
1242 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001243 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001244 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001245 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001246 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001247 args->m_error.SetErrorToErrno();
1248 goto FINISH;
1249 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001250 else if (WIFEXITED(status))
1251 {
1252 // open, dup or execve likely failed for some reason.
1253 args->m_error.SetErrorToGenericError();
1254 switch (WEXITSTATUS(status))
1255 {
Greg Clayton542e4072012-09-07 17:49:29 +00001256 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001257 args->m_error.SetErrorString("Child ptrace failed.");
1258 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001259 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001260 args->m_error.SetErrorString("Child open stdin failed.");
1261 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001262 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001263 args->m_error.SetErrorString("Child open stdout failed.");
1264 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001265 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001266 args->m_error.SetErrorString("Child open stderr failed.");
1267 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001268 case eChdirFailed:
1269 args->m_error.SetErrorString("Child failed to set working directory.");
1270 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001271 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001272 args->m_error.SetErrorString("Child exec failed.");
1273 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001274 case eSetGidFailed:
1275 args->m_error.SetErrorString("Child setgid failed.");
1276 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001277 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001278 args->m_error.SetErrorString("Child returned unknown exit status.");
1279 break;
1280 }
1281 goto FINISH;
1282 }
1283 assert(WIFSTOPPED(status) && wpid == pid &&
1284 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001285
Matt Kopec085d6ce2013-05-31 22:00:07 +00001286 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001287 {
1288 args->m_error.SetErrorToErrno();
1289 goto FINISH;
1290 }
1291
1292 // Release the master terminal descriptor and pass it off to the
1293 // ProcessMonitor instance. Similarly stash the inferior pid.
1294 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1295 monitor->m_pid = pid;
1296
Stephen Wilson26977162011-03-23 02:14:42 +00001297 // Set the terminal fd to be in non blocking mode (it simplifies the
1298 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1299 // descriptor to read from).
1300 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1301 goto FINISH;
1302
Johnny Chen30213ff2012-01-05 19:17:38 +00001303 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001304 // FIXME: should we be letting UpdateThreadList handle this?
1305 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001306 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001307
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001308 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001309 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001310 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001311
Matt Kopecb2910442013-07-09 15:09:45 +00001312 process.AddThreadForInitialStopIfNeeded(pid);
1313
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001314 // Let our process instance know the thread has stopped.
1315 process.SendMessage(ProcessMessage::Trace(pid));
1316
1317FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001318 return args->m_error.Success();
1319}
1320
Johnny Chen25e68e32011-06-14 19:19:50 +00001321void
1322ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1323{
1324 static const char *g_thread_name = "lldb.process.linux.operation";
1325
1326 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1327 return;
1328
1329 m_operation_thread =
1330 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1331}
1332
Johnny Chen25e68e32011-06-14 19:19:50 +00001333void *
1334ProcessMonitor::AttachOpThread(void *arg)
1335{
1336 AttachArgs *args = static_cast<AttachArgs*>(arg);
1337
Greg Clayton743ecf42012-10-16 20:20:18 +00001338 if (!Attach(args)) {
1339 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001340 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001341 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001342
1343 ServeOperation(args);
1344 return NULL;
1345}
1346
1347bool
1348ProcessMonitor::Attach(AttachArgs *args)
1349{
1350 lldb::pid_t pid = args->m_pid;
1351
1352 ProcessMonitor *monitor = args->m_monitor;
1353 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001354 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001355 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001356
Matt Kopec085d6ce2013-05-31 22:00:07 +00001357 // Use a map to keep track of the threads which we have attached/need to attach.
1358 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001359 if (pid <= 1)
1360 {
1361 args->m_error.SetErrorToGenericError();
1362 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1363 goto FINISH;
1364 }
1365
Matt Kopec085d6ce2013-05-31 22:00:07 +00001366 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001367 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001368 for (Host::TidMap::iterator it = tids_to_attach.begin();
1369 it != tids_to_attach.end(); ++it)
1370 {
1371 if (it->second == false)
1372 {
1373 lldb::tid_t tid = it->first;
1374
1375 // Attach to the requested process.
1376 // An attach will cause the thread to stop with a SIGSTOP.
1377 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1378 {
1379 // No such thread. The thread may have exited.
1380 // More error handling may be needed.
1381 if (errno == ESRCH)
1382 {
1383 tids_to_attach.erase(it);
1384 continue;
1385 }
1386 else
1387 {
1388 args->m_error.SetErrorToErrno();
1389 goto FINISH;
1390 }
1391 }
1392
1393 int status;
1394 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1395 // At this point we should have a thread stopped if waitpid succeeds.
1396 if ((status = waitpid(tid, NULL, __WALL)) < 0)
1397 {
1398 // No such thread. The thread may have exited.
1399 // More error handling may be needed.
1400 if (errno == ESRCH)
1401 {
1402 tids_to_attach.erase(it);
1403 continue;
1404 }
1405 else
1406 {
1407 args->m_error.SetErrorToErrno();
1408 goto FINISH;
1409 }
1410 }
1411
1412 if (!SetDefaultPtraceOpts(tid))
1413 {
1414 args->m_error.SetErrorToErrno();
1415 goto FINISH;
1416 }
1417
1418 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001419 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001420
Matt Kopec085d6ce2013-05-31 22:00:07 +00001421 if (log)
1422 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1423 process.GetThreadList().AddThread(inferior);
1424 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001425 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001426 }
1427 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001428 }
1429
Matt Kopec085d6ce2013-05-31 22:00:07 +00001430 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001431 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001432 monitor->m_pid = pid;
1433 // Let our process instance know the thread has stopped.
1434 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001435 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001436 else
1437 {
1438 args->m_error.SetErrorToGenericError();
1439 args->m_error.SetErrorString("No such process.");
1440 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001441
1442 FINISH:
1443 return args->m_error.Success();
1444}
1445
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001446bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001447ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1448{
1449 long ptrace_opts = 0;
1450
1451 // Have the child raise an event on exit. This is used to keep the child in
1452 // limbo until it is destroyed.
1453 ptrace_opts |= PTRACE_O_TRACEEXIT;
1454
1455 // Have the tracer trace threads which spawn in the inferior process.
1456 // TODO: if we want to support tracing the inferiors' child, add the
1457 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1458 ptrace_opts |= PTRACE_O_TRACECLONE;
1459
1460 // Have the tracer notify us before execve returns
1461 // (needed to disable legacy SIGTRAP generation)
1462 ptrace_opts |= PTRACE_O_TRACEEXEC;
1463
1464 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1465}
1466
1467bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001468ProcessMonitor::MonitorCallback(void *callback_baton,
1469 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001470 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001471 int signal,
1472 int status)
1473{
1474 ProcessMessage message;
1475 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001476 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001477 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001478 bool stop_monitoring;
1479 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001480 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001481
Andrew Kaylor93132f52013-05-28 23:04:25 +00001482 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1483
1484 if (exited)
1485 {
1486 if (log)
1487 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1488 message = ProcessMessage::Exit(pid, status);
1489 process->SendMessage(message);
1490 return pid == process->GetID();
1491 }
1492
Daniel Maleaa35970a2012-11-23 18:09:58 +00001493 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1494 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001495 if (log)
1496 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001497 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1498 if (!monitor->Resume(pid, SIGSTOP)) {
1499 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1500 }
1501 stop_monitoring = false;
1502 } else {
1503 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1504 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001505 // stop the monitor thread if this is the main pid.
1506 if (log)
1507 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1508 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1509 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001510 // If we are going to stop monitoring, we need to notify our process object
1511 if (stop_monitoring)
1512 {
1513 message = ProcessMessage::Exit(pid, status);
1514 process->SendMessage(message);
1515 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001516 }
1517 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001518 else {
1519 switch (info.si_signo)
1520 {
1521 case SIGTRAP:
1522 message = MonitorSIGTRAP(monitor, &info, pid);
1523 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001524
Stephen Wilson84ffe702011-03-30 15:55:52 +00001525 default:
1526 message = MonitorSignal(monitor, &info, pid);
1527 break;
1528 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001529
Stephen Wilson84ffe702011-03-30 15:55:52 +00001530 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001531 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001532 }
1533
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001534 return stop_monitoring;
1535}
1536
1537ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001538ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001539 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001540{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001541 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001542
Andrew Kaylor93132f52013-05-28 23:04:25 +00001543 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1544
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001545 assert(monitor);
1546 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001547
Stephen Wilson84ffe702011-03-30 15:55:52 +00001548 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001549 {
1550 default:
1551 assert(false && "Unexpected SIGTRAP code!");
1552 break;
1553
Matt Kopeca360d7e2013-05-17 19:27:47 +00001554 // TODO: these two cases are required if we want to support tracing
1555 // of the inferiors' children
1556 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1557 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1558
Matt Kopec650648f2013-01-08 16:30:18 +00001559 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1560 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001561 if (log)
1562 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1563
Matt Kopec650648f2013-01-08 16:30:18 +00001564 unsigned long tid = 0;
1565 if (!monitor->GetEventMessage(pid, &tid))
1566 tid = -1;
1567 message = ProcessMessage::NewThread(pid, tid);
1568 break;
1569 }
1570
Matt Kopeca360d7e2013-05-17 19:27:47 +00001571 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001572 if (log)
1573 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1574
1575 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001576 break;
1577
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001578 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1579 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001580 // The inferior process or one of its threads is about to exit.
1581 // Maintain the process or thread in a state of "limbo" until we are
1582 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001583 unsigned long data = 0;
1584 if (!monitor->GetEventMessage(pid, &data))
1585 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001586 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001587 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001588 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001589 break;
1590 }
1591
1592 case 0:
1593 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001594 if (log)
1595 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001596 message = ProcessMessage::Trace(pid);
1597 break;
1598
1599 case SI_KERNEL:
1600 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001601 if (log)
1602 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001603 message = ProcessMessage::Break(pid);
1604 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001605
1606 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001607 if (log)
1608 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001609 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1610 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001611
1612 case SIGTRAP:
1613 case (SIGTRAP | 0x80):
1614 if (log)
1615 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1616 // Ignore these signals until we know more about them
1617 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001618 }
1619
1620 return message;
1621}
1622
Stephen Wilson84ffe702011-03-30 15:55:52 +00001623ProcessMessage
1624ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001625 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001626{
1627 ProcessMessage message;
1628 int signo = info->si_signo;
1629
Andrew Kaylor93132f52013-05-28 23:04:25 +00001630 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1631
Stephen Wilson84ffe702011-03-30 15:55:52 +00001632 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1633 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1634 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1635 //
1636 // IOW, user generated signals never generate what we consider to be a
1637 // "crash".
1638 //
1639 // Similarly, ACK signals generated by this monitor.
1640 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1641 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001642 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001643 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001644 __FUNCTION__,
1645 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1646 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1647 info->si_pid);
1648
Stephen Wilson84ffe702011-03-30 15:55:52 +00001649 if (info->si_pid == getpid())
1650 return ProcessMessage::SignalDelivered(pid, signo);
1651 else
1652 return ProcessMessage::Signal(pid, signo);
1653 }
1654
Andrew Kaylor93132f52013-05-28 23:04:25 +00001655 if (log)
1656 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1657
Stephen Wilson84ffe702011-03-30 15:55:52 +00001658 if (signo == SIGSEGV) {
1659 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1660 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1661 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1662 }
1663
1664 if (signo == SIGILL) {
1665 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1666 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1667 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1668 }
1669
1670 if (signo == SIGFPE) {
1671 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1672 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1673 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1674 }
1675
1676 if (signo == SIGBUS) {
1677 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1678 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1679 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1680 }
1681
1682 // Everything else is "normal" and does not require any special action on
1683 // our part.
1684 return ProcessMessage::Signal(pid, signo);
1685}
1686
Andrew Kaylord4d54992013-09-17 00:30:24 +00001687// On Linux, when a new thread is created, we receive to notifications,
1688// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1689// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1690// the new child thread indicating that it has is stopped because we attached.
1691// We have no guarantee of the order in which these arrive, but we need both
1692// before we are ready to proceed. We currently keep a list of threads which
1693// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1694// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1695// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1696
1697bool
1698ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1699{
1700 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1701 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001702 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001703
1704 // Wait for the thread to stop
1705 while (true)
1706 {
1707 int status = -1;
1708 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001709 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001710 lldb::pid_t wait_pid = waitpid(tid, &status, __WALL);
1711 if (status == -1)
1712 {
1713 // If we got interrupted by a signal (in our process, not the
1714 // inferior) try again.
1715 if (errno == EINTR)
1716 continue;
1717 else
1718 {
1719 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001720 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001721 return false; // This is bad, but there's nothing we can do.
1722 }
1723 }
1724
1725 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001726 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001727
1728 assert(wait_pid == tid);
1729
1730 siginfo_t info;
1731 int ptrace_err;
1732 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1733 {
1734 if (log)
1735 {
1736 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1737 }
1738 return false;
1739 }
1740
1741 // If this is a thread exit, we won't get any more information.
1742 if (WIFEXITED(status))
1743 {
1744 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1745 if (wait_pid == tid)
1746 return true;
1747 continue;
1748 }
1749
1750 assert(info.si_code == SI_USER);
1751 assert(WSTOPSIG(status) == SIGSTOP);
1752
1753 if (log)
1754 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1755 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1756 return true;
1757 }
1758 return false;
1759}
1760
Andrew Kaylor93132f52013-05-28 23:04:25 +00001761bool
1762ProcessMonitor::StopThread(lldb::tid_t tid)
1763{
1764 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1765
1766 // FIXME: Try to use tgkill or tkill
1767 int ret = tgkill(m_pid, tid, SIGSTOP);
1768 if (log)
1769 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1770
1771 // This can happen if a thread exited while we were trying to stop it. That's OK.
1772 // We'll get the signal for that later.
1773 if (ret < 0)
1774 return false;
1775
1776 // Wait for the thread to stop
1777 while (true)
1778 {
1779 int status = -1;
1780 if (log)
1781 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
1782 lldb::pid_t wait_pid = ::waitpid (-1*m_pid, &status, __WALL);
1783 if (log)
1784 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d", __FUNCTION__, wait_pid, status);
1785
1786 if (wait_pid == -1)
1787 {
1788 // If we got interrupted by a signal (in our process, not the
1789 // inferior) try again.
1790 if (errno == EINTR)
1791 continue;
1792 else
1793 return false; // This is bad, but there's nothing we can do.
1794 }
1795
1796 // If this is a thread exit, we won't get any more information.
1797 if (WIFEXITED(status))
1798 {
1799 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
1800 if (wait_pid == tid)
1801 return true;
1802 continue;
1803 }
1804
1805 siginfo_t info;
1806 int ptrace_err;
1807 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1808 {
1809 if (log)
1810 {
1811 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
1812
1813 // This would be a particularly interesting case
1814 if (ptrace_err == EINVAL)
1815 log->Printf ("ProcessMonitor::%s() in group-stop", __FUNCTION__);
1816 }
1817 return false;
1818 }
1819
1820 // Handle events from other threads
1821 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001822 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64, __FUNCTION__, wait_pid);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001823
1824 ProcessMessage message;
1825 if (info.si_signo == SIGTRAP)
1826 message = MonitorSIGTRAP(this, &info, wait_pid);
1827 else
1828 message = MonitorSignal(this, &info, wait_pid);
1829
1830 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1831
1832 // When a new thread is created, we may get a SIGSTOP for the new thread
1833 // just before we get the SIGTRAP that we use to add the thread to our
1834 // process thread list. We don't need to worry about that signal here.
1835 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1836
1837 if (!thread)
1838 {
1839 m_process->SendMessage(message);
1840 continue;
1841 }
1842
1843 switch (message.GetKind())
1844 {
Michael Sartainc258b302013-09-18 15:32:06 +00001845 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001846 case ProcessMessage::eInvalidMessage:
1847 break;
1848
1849 // These need special handling because we don't want to send a
1850 // resume even if we already sent a SIGSTOP to this thread. In
1851 // this case the resume will cause the thread to disappear. It is
1852 // unlikely that we'll ever get eExitMessage here, but the same
1853 // reasoning applies.
1854 case ProcessMessage::eLimboMessage:
1855 case ProcessMessage::eExitMessage:
1856 if (log)
1857 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1858 // SendMessage will set the thread state as needed.
1859 m_process->SendMessage(message);
1860 // If this is the thread we're waiting for, stop waiting. Even
1861 // though this wasn't the signal we expected, it's the last
1862 // signal we'll see while this thread is alive.
1863 if (wait_pid == tid)
1864 return true;
1865 break;
1866
Matt Kopecb2910442013-07-09 15:09:45 +00001867 case ProcessMessage::eSignalMessage:
1868 if (log)
1869 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1870 if (WSTOPSIG(status) == SIGSTOP)
1871 {
1872 m_process->AddThreadForInitialStopIfNeeded(tid);
1873 thread->SetState(lldb::eStateStopped);
1874 }
1875 else
1876 {
1877 m_process->SendMessage(message);
1878 // This isn't the stop we were expecting, but the thread is
1879 // stopped. SendMessage will handle processing of this event,
1880 // but we need to resume here to get the stop we are waiting
1881 // for (otherwise the thread will stop again immediately when
1882 // we try to resume).
1883 if (wait_pid == tid)
1884 Resume(wait_pid, eResumeSignalNone);
1885 }
1886 break;
1887
Andrew Kaylor93132f52013-05-28 23:04:25 +00001888 case ProcessMessage::eSignalDeliveredMessage:
1889 // This is the stop we're expecting.
1890 if (wait_pid == tid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && info.si_code == SI_TKILL)
1891 {
1892 if (log)
1893 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
1894 thread->SetState(lldb::eStateStopped);
1895 return true;
1896 }
1897 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00001898 case ProcessMessage::eBreakpointMessage:
1899 case ProcessMessage::eTraceMessage:
1900 case ProcessMessage::eWatchpointMessage:
1901 case ProcessMessage::eCrashMessage:
1902 case ProcessMessage::eNewThreadMessage:
1903 if (log)
1904 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
1905 // SendMessage will set the thread state as needed.
1906 m_process->SendMessage(message);
1907 // This isn't the stop we were expecting, but the thread is
1908 // stopped. SendMessage will handle processing of this event,
1909 // but we need to resume here to get the stop we are waiting
1910 // for (otherwise the thread will stop again immediately when
1911 // we try to resume).
1912 if (wait_pid == tid)
1913 Resume(wait_pid, eResumeSignalNone);
1914 break;
1915 }
1916 }
1917 return false;
1918}
1919
Stephen Wilson84ffe702011-03-30 15:55:52 +00001920ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001921ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001922{
1923 ProcessMessage::CrashReason reason;
1924 assert(info->si_signo == SIGSEGV);
1925
1926 reason = ProcessMessage::eInvalidCrashReason;
1927
Greg Clayton542e4072012-09-07 17:49:29 +00001928 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001929 {
1930 default:
1931 assert(false && "unexpected si_code for SIGSEGV");
1932 break;
Matt Kopecf8cfe6b2013-08-09 15:26:56 +00001933 case SI_KERNEL:
1934 // Linux will occasionally send spurious SI_KERNEL codes.
1935 // (this is poorly documented in sigaction)
1936 // One way to get this is via unaligned SIMD loads.
1937 reason = ProcessMessage::eInvalidAddress; // for lack of anything better
1938 break;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001939 case SEGV_MAPERR:
1940 reason = ProcessMessage::eInvalidAddress;
1941 break;
1942 case SEGV_ACCERR:
1943 reason = ProcessMessage::ePrivilegedAddress;
1944 break;
1945 }
Greg Clayton542e4072012-09-07 17:49:29 +00001946
Stephen Wilson84ffe702011-03-30 15:55:52 +00001947 return reason;
1948}
1949
1950ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001951ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001952{
1953 ProcessMessage::CrashReason reason;
1954 assert(info->si_signo == SIGILL);
1955
1956 reason = ProcessMessage::eInvalidCrashReason;
1957
1958 switch (info->si_code)
1959 {
1960 default:
1961 assert(false && "unexpected si_code for SIGILL");
1962 break;
1963 case ILL_ILLOPC:
1964 reason = ProcessMessage::eIllegalOpcode;
1965 break;
1966 case ILL_ILLOPN:
1967 reason = ProcessMessage::eIllegalOperand;
1968 break;
1969 case ILL_ILLADR:
1970 reason = ProcessMessage::eIllegalAddressingMode;
1971 break;
1972 case ILL_ILLTRP:
1973 reason = ProcessMessage::eIllegalTrap;
1974 break;
1975 case ILL_PRVOPC:
1976 reason = ProcessMessage::ePrivilegedOpcode;
1977 break;
1978 case ILL_PRVREG:
1979 reason = ProcessMessage::ePrivilegedRegister;
1980 break;
1981 case ILL_COPROC:
1982 reason = ProcessMessage::eCoprocessorError;
1983 break;
1984 case ILL_BADSTK:
1985 reason = ProcessMessage::eInternalStackError;
1986 break;
1987 }
1988
1989 return reason;
1990}
1991
1992ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001993ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001994{
1995 ProcessMessage::CrashReason reason;
1996 assert(info->si_signo == SIGFPE);
1997
1998 reason = ProcessMessage::eInvalidCrashReason;
1999
2000 switch (info->si_code)
2001 {
2002 default:
2003 assert(false && "unexpected si_code for SIGFPE");
2004 break;
2005 case FPE_INTDIV:
2006 reason = ProcessMessage::eIntegerDivideByZero;
2007 break;
2008 case FPE_INTOVF:
2009 reason = ProcessMessage::eIntegerOverflow;
2010 break;
2011 case FPE_FLTDIV:
2012 reason = ProcessMessage::eFloatDivideByZero;
2013 break;
2014 case FPE_FLTOVF:
2015 reason = ProcessMessage::eFloatOverflow;
2016 break;
2017 case FPE_FLTUND:
2018 reason = ProcessMessage::eFloatUnderflow;
2019 break;
2020 case FPE_FLTRES:
2021 reason = ProcessMessage::eFloatInexactResult;
2022 break;
2023 case FPE_FLTINV:
2024 reason = ProcessMessage::eFloatInvalidOperation;
2025 break;
2026 case FPE_FLTSUB:
2027 reason = ProcessMessage::eFloatSubscriptRange;
2028 break;
2029 }
2030
2031 return reason;
2032}
2033
2034ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00002035ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002036{
2037 ProcessMessage::CrashReason reason;
2038 assert(info->si_signo == SIGBUS);
2039
2040 reason = ProcessMessage::eInvalidCrashReason;
2041
2042 switch (info->si_code)
2043 {
2044 default:
2045 assert(false && "unexpected si_code for SIGBUS");
2046 break;
2047 case BUS_ADRALN:
2048 reason = ProcessMessage::eIllegalAlignment;
2049 break;
2050 case BUS_ADRERR:
2051 reason = ProcessMessage::eIllegalAddress;
2052 break;
2053 case BUS_OBJERR:
2054 reason = ProcessMessage::eHardwareError;
2055 break;
2056 }
2057
2058 return reason;
2059}
2060
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002061void
Johnny Chen25e68e32011-06-14 19:19:50 +00002062ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002063{
Stephen Wilson570243b2011-01-19 01:37:06 +00002064 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002065
Stephen Wilson570243b2011-01-19 01:37:06 +00002066 // We are finised with the arguments and are ready to go. Sync with the
2067 // parent thread and start serving operations on the inferior.
2068 sem_post(&args->m_semaphore);
2069
Michael Sartain704bf892013-10-09 01:28:57 +00002070 for(;;)
2071 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002072 // wait for next pending operation
2073 sem_wait(&monitor->m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002074
Daniel Malea1efb4182013-09-16 23:12:18 +00002075 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002076
Daniel Malea1efb4182013-09-16 23:12:18 +00002077 // notify calling thread that operation is complete
2078 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002079 }
2080}
2081
2082void
2083ProcessMonitor::DoOperation(Operation *op)
2084{
Daniel Malea1efb4182013-09-16 23:12:18 +00002085 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002086
Daniel Malea1efb4182013-09-16 23:12:18 +00002087 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002088
Daniel Malea1efb4182013-09-16 23:12:18 +00002089 // notify operation thread that an operation is ready to be processed
2090 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002091
Daniel Malea1efb4182013-09-16 23:12:18 +00002092 // wait for operation to complete
2093 sem_wait(&m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002094}
2095
2096size_t
2097ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2098 Error &error)
2099{
2100 size_t result;
2101 ReadOperation op(vm_addr, buf, size, error, result);
2102 DoOperation(&op);
2103 return result;
2104}
2105
2106size_t
2107ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2108 lldb_private::Error &error)
2109{
2110 size_t result;
2111 WriteOperation op(vm_addr, buf, size, error, result);
2112 DoOperation(&op);
2113 return result;
2114}
2115
2116bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002117ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002118 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002119{
2120 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002121 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002122 DoOperation(&op);
2123 return result;
2124}
2125
2126bool
Matt Kopec7de48462013-03-06 17:20:48 +00002127ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002128 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002129{
2130 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002131 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002132 DoOperation(&op);
2133 return result;
2134}
2135
2136bool
Matt Kopec7de48462013-03-06 17:20:48 +00002137ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002138{
2139 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002140 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002141 DoOperation(&op);
2142 return result;
2143}
2144
2145bool
Matt Kopec7de48462013-03-06 17:20:48 +00002146ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002147{
2148 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002149 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002150 DoOperation(&op);
2151 return result;
2152}
2153
2154bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002155ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2156{
2157 bool result;
2158 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2159 DoOperation(&op);
2160 return result;
2161}
2162
2163bool
Matt Kopec7de48462013-03-06 17:20:48 +00002164ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002165{
2166 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002167 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002168 DoOperation(&op);
2169 return result;
2170}
2171
2172bool
Matt Kopec7de48462013-03-06 17:20:48 +00002173ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002174{
2175 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002176 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002177 DoOperation(&op);
2178 return result;
2179}
2180
2181bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002182ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2183{
2184 bool result;
2185 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2186 DoOperation(&op);
2187 return result;
2188}
2189
2190bool
Richard Mitton0a558352013-10-17 21:14:00 +00002191ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2192{
2193 bool result;
2194 ReadThreadPointerOperation op(tid, &value, result);
2195 DoOperation(&op);
2196 return result;
2197}
2198
2199bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002200ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002201{
2202 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002203 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2204
2205 if (log)
2206 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2207 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002208 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002209 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002210 if (log)
2211 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002212 return result;
2213}
2214
2215bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002216ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002217{
2218 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002219 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002220 DoOperation(&op);
2221 return result;
2222}
2223
2224bool
2225ProcessMonitor::BringProcessIntoLimbo()
2226{
2227 bool result;
2228 KillOperation op(result);
2229 DoOperation(&op);
2230 return result;
2231}
2232
2233bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002234ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002235{
2236 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002237 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002238 DoOperation(&op);
2239 return result;
2240}
2241
2242bool
2243ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2244{
2245 bool result;
2246 EventMessageOperation op(tid, message, result);
2247 DoOperation(&op);
2248 return result;
2249}
2250
Greg Clayton743ecf42012-10-16 20:20:18 +00002251lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002252ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002253{
Greg Clayton28041352011-11-29 20:50:10 +00002254 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002255 if (tid != LLDB_INVALID_THREAD_ID)
2256 {
2257 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002258 DoOperation(&op);
2259 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002260 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002261}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002262
2263bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002264ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2265{
Peter Collingbourne62343202011-06-14 03:55:54 +00002266 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002267
2268 if (target_fd == -1)
2269 return false;
2270
Peter Collingbourne62343202011-06-14 03:55:54 +00002271 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002272}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002273
2274void
2275ProcessMonitor::StopMonitoringChildProcess()
2276{
2277 lldb::thread_result_t thread_result;
2278
Stephen Wilsond4182f42011-02-09 20:10:35 +00002279 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002280 {
2281 Host::ThreadCancel(m_monitor_thread, NULL);
2282 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
2283 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
2284 }
2285}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002286
2287void
2288ProcessMonitor::StopMonitor()
2289{
2290 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002291 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002292 sem_destroy(&m_operation_pending);
2293 sem_destroy(&m_operation_done);
2294
Andrew Kaylor5e268992013-09-14 00:17:31 +00002295 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
2296 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
2297 // the descriptor to a ConnectionFileDescriptor object. Consequently
2298 // even though still has the file descriptor, we shouldn't close it here.
Stephen Wilson84ffe702011-03-30 15:55:52 +00002299}
2300
2301void
Greg Clayton743ecf42012-10-16 20:20:18 +00002302ProcessMonitor::StopOpThread()
2303{
2304 lldb::thread_result_t result;
2305
2306 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
2307 return;
2308
2309 Host::ThreadCancel(m_operation_thread, NULL);
2310 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00002311 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00002312}