blob: cc412349453dfbb154b989dab13eb528c080ea8c [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>
20#include <sys/types.h>
21#include <sys/wait.h>
22
23// C++ Includes
24// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000025#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000026#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000027#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000028#include "lldb/Core/Scalar.h"
29#include "lldb/Host/Host.h"
30#include "lldb/Target/Thread.h"
31#include "lldb/Target/RegisterContext.h"
32#include "lldb/Utility/PseudoTerminal.h"
33
Johnny Chen30213ff2012-01-05 19:17:38 +000034#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000036#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessMonitor.h"
38
39
Greg Clayton386ff182011-11-05 01:09:16 +000040#define DEBUG_PTRACE_MAXBYTES 20
41
Matt Kopec58c0b962013-03-20 20:34:35 +000042// Support ptrace extensions even when compiled without required kernel support
43#ifndef PTRACE_GETREGSET
44 #define PTRACE_GETREGSET 0x4204
45#endif
46#ifndef PTRACE_SETREGSET
47 #define PTRACE_SETREGSET 0x4205
48#endif
49
Stephen Wilsone6f9f662010-07-24 02:19:04 +000050using namespace lldb_private;
51
Johnny Chen0d5f2d42011-10-18 18:09:30 +000052// FIXME: this code is host-dependent with respect to types and
53// endianness and needs to be fixed. For example, lldb::addr_t is
54// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
55// 32-bit pointer arguments. This code uses casts to work around the
56// problem.
57
58// We disable the tracing of ptrace calls for integration builds to
59// avoid the additional indirection and checks.
60#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
61
Greg Clayton386ff182011-11-05 01:09:16 +000062static void
63DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
64{
65 uint8_t *ptr = (uint8_t *)bytes;
66 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
67 for(uint32_t i=0; i<loop_count; i++)
68 {
69 s.Printf ("[%x]", *ptr);
70 ptr++;
71 }
72}
73
Matt Kopec58c0b962013-03-20 20:34:35 +000074static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000075{
76 StreamString buf;
Johnny Chen30213ff2012-01-05 19:17:38 +000077 LogSP verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
78 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +000079
80 if (verbose_log)
81 {
82 switch(req)
83 {
84 case PTRACE_POKETEXT:
85 {
86 DisplayBytes(buf, &data, 8);
87 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
88 break;
89 }
Greg Clayton542e4072012-09-07 17:49:29 +000090 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +000091 {
92 DisplayBytes(buf, &data, 8);
93 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
94 break;
95 }
Greg Clayton542e4072012-09-07 17:49:29 +000096 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +000097 {
98 DisplayBytes(buf, &data, 8);
99 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
100 break;
101 }
Greg Clayton542e4072012-09-07 17:49:29 +0000102 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000103 {
Matt Kopec7de48462013-03-06 17:20:48 +0000104 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000105 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
106 break;
107 }
108 case PTRACE_SETFPREGS:
109 {
Matt Kopec7de48462013-03-06 17:20:48 +0000110 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000111 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
112 break;
113 }
Greg Clayton542e4072012-09-07 17:49:29 +0000114 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000115 {
116 DisplayBytes(buf, data, sizeof(siginfo_t));
117 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
118 break;
119 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000120 case PTRACE_SETREGSET:
121 {
122 // Extract iov_base from data, which is a pointer to the struct IOVEC
123 DisplayBytes(buf, *(void **)data, data_size);
124 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
125 break;
126 }
Greg Clayton386ff182011-11-05 01:09:16 +0000127 default:
128 {
129 }
130 }
131 }
132}
133
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000134// Wrapper for ptrace to catch errors and log calls.
Matt Kopec7de48462013-03-06 17:20:48 +0000135// Note that ptrace sets errno on error because -1 is a valid result for PTRACE_PEEK*
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000136extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000137PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000138 const char* reqName, const char* file, int line)
139{
Greg Clayton386ff182011-11-05 01:09:16 +0000140 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000141
Johnny Chen30213ff2012-01-05 19:17:38 +0000142 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000143
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000144 if (log)
Matt Kopec58c0b962013-03-20 20:34:35 +0000145 log->Printf("ptrace(%s, %lu, %p, %p, %zu) called from file %s line %d",
Matt Kopec7de48462013-03-06 17:20:48 +0000146 reqName, pid, addr, data, data_size, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000147
Matt Kopec7de48462013-03-06 17:20:48 +0000148 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000149
150 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000151 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
152 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
153 else
154 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000155
Matt Kopec7de48462013-03-06 17:20:48 +0000156 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000157
Matt Kopec7de48462013-03-06 17:20:48 +0000158 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000159 {
160 const char* str;
161 switch (errno)
162 {
163 case ESRCH: str = "ESRCH"; break;
164 case EINVAL: str = "EINVAL"; break;
165 case EBUSY: str = "EBUSY"; break;
166 case EPERM: str = "EPERM"; break;
167 default: str = "<unknown>";
168 }
169 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
170 }
171
172 return result;
173}
174
Matt Kopec7de48462013-03-06 17:20:48 +0000175// Wrapper for ptrace when logging is not required.
176// Sets errno to 0 prior to calling ptrace.
177extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000178PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000179{
Matt Kopec58c0b962013-03-20 20:34:35 +0000180 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000181 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000182 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
183 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
184 else
185 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000186 return result;
187}
188
189#define PTRACE(req, pid, addr, data, data_size) \
190 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000191#else
Matt Kopec7de48462013-03-06 17:20:48 +0000192 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000193#endif
194
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000195//------------------------------------------------------------------------------
196// Static implementations of ProcessMonitor::ReadMemory and
197// ProcessMonitor::WriteMemory. This enables mutual recursion between these
198// functions without needed to go thru the thread funnel.
199
200static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000201DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000202 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
203{
Greg Clayton542e4072012-09-07 17:49:29 +0000204 // ptrace word size is determined by the host, not the child
205 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000206 unsigned char *dst = static_cast<unsigned char*>(buf);
207 size_t bytes_read;
208 size_t remainder;
209 long data;
210
Johnny Chen30213ff2012-01-05 19:17:38 +0000211 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000212 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000213 ProcessPOSIXLog::IncNestLevel();
214 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000215 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000216 pid, word_size, (void*)vm_addr, buf, size);
217
218 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000219 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
220 {
221 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000222 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
223 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224 {
225 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000226 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000227 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000228 return bytes_read;
229 }
230
231 remainder = size - bytes_read;
232 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000233
234 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235 for (unsigned i = 0; i < remainder; ++i)
236 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000237
Johnny Chen30213ff2012-01-05 19:17:38 +0000238 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
239 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
240 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
241 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000242 {
243 uintptr_t print_dst = 0;
244 // Format bytes from data by moving into print_dst for log output
245 for (unsigned i = 0; i < remainder; ++i)
246 print_dst |= (((data >> i*8) & 0xFF) << i*8);
247 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
248 (void*)vm_addr, print_dst, (unsigned long)data);
249 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000250
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 vm_addr += word_size;
252 dst += word_size;
253 }
254
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000255 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000256 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000257 return bytes_read;
258}
259
260static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000261DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000262 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
263{
Greg Clayton542e4072012-09-07 17:49:29 +0000264 // ptrace word size is determined by the host, not the child
265 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000266 const unsigned char *src = static_cast<const unsigned char*>(buf);
267 size_t bytes_written = 0;
268 size_t remainder;
269
Johnny Chen30213ff2012-01-05 19:17:38 +0000270 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000271 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000272 ProcessPOSIXLog::IncNestLevel();
273 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000274 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000275 pid, word_size, (void*)vm_addr, buf, size);
276
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000277 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
278 {
279 remainder = size - bytes_written;
280 remainder = remainder > word_size ? word_size : remainder;
281
282 if (remainder == word_size)
283 {
284 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000285 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286 for (unsigned i = 0; i < word_size; ++i)
287 data |= (unsigned long)src[i] << i*8;
288
Johnny Chen30213ff2012-01-05 19:17:38 +0000289 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
290 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
291 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
292 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000293 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
294 (void*)vm_addr, *(unsigned long*)src, data);
295
Matt Kopec7de48462013-03-06 17:20:48 +0000296 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000297 {
298 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000299 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000300 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000301 return bytes_written;
302 }
303 }
304 else
305 {
306 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000307 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000308 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000309 {
310 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000311 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000312 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000313 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000314
315 memcpy(buff, src, remainder);
316
Greg Clayton542e4072012-09-07 17:49:29 +0000317 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000318 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000319 {
320 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000321 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000322 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000323 }
324
Johnny Chen30213ff2012-01-05 19:17:38 +0000325 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
326 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
327 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
328 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000329 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
330 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331 }
332
333 vm_addr += word_size;
334 src += word_size;
335 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000336 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000337 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000338 return bytes_written;
339}
340
Stephen Wilson26977162011-03-23 02:14:42 +0000341// Simple helper function to ensure flags are enabled on the given file
342// descriptor.
343static bool
344EnsureFDFlags(int fd, int flags, Error &error)
345{
346 int status;
347
348 if ((status = fcntl(fd, F_GETFL)) == -1)
349 {
350 error.SetErrorToErrno();
351 return false;
352 }
353
354 if (fcntl(fd, F_SETFL, status | flags) == -1)
355 {
356 error.SetErrorToErrno();
357 return false;
358 }
359
360 return true;
361}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000362
363//------------------------------------------------------------------------------
364/// @class Operation
365/// @brief Represents a ProcessMonitor operation.
366///
367/// Under Linux, it is not possible to ptrace() from any other thread but the
368/// one that spawned or attached to the process from the start. Therefore, when
369/// a ProcessMonitor is asked to deliver or change the state of an inferior
370/// process the operation must be "funneled" to a specific thread to perform the
371/// task. The Operation class provides an abstract base for all services the
372/// ProcessMonitor must perform via the single virtual function Execute, thus
373/// encapsulating the code that needs to run in the privileged context.
374class Operation
375{
376public:
377 virtual void Execute(ProcessMonitor *monitor) = 0;
378};
379
380//------------------------------------------------------------------------------
381/// @class ReadOperation
382/// @brief Implements ProcessMonitor::ReadMemory.
383class ReadOperation : public Operation
384{
385public:
386 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
387 Error &error, size_t &result)
388 : m_addr(addr), m_buff(buff), m_size(size),
389 m_error(error), m_result(result)
390 { }
391
392 void Execute(ProcessMonitor *monitor);
393
394private:
395 lldb::addr_t m_addr;
396 void *m_buff;
397 size_t m_size;
398 Error &m_error;
399 size_t &m_result;
400};
401
402void
403ReadOperation::Execute(ProcessMonitor *monitor)
404{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000405 lldb::pid_t pid = monitor->GetPID();
406
Greg Clayton542e4072012-09-07 17:49:29 +0000407 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000408}
409
410//------------------------------------------------------------------------------
411/// @class ReadOperation
412/// @brief Implements ProcessMonitor::WriteMemory.
413class WriteOperation : public Operation
414{
415public:
416 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
417 Error &error, size_t &result)
418 : m_addr(addr), m_buff(buff), m_size(size),
419 m_error(error), m_result(result)
420 { }
421
422 void Execute(ProcessMonitor *monitor);
423
424private:
425 lldb::addr_t m_addr;
426 const void *m_buff;
427 size_t m_size;
428 Error &m_error;
429 size_t &m_result;
430};
431
432void
433WriteOperation::Execute(ProcessMonitor *monitor)
434{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000435 lldb::pid_t pid = monitor->GetPID();
436
Greg Clayton542e4072012-09-07 17:49:29 +0000437 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000438}
439
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000440
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000441//------------------------------------------------------------------------------
442/// @class ReadRegOperation
443/// @brief Implements ProcessMonitor::ReadRegisterValue.
444class ReadRegOperation : public Operation
445{
446public:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000447 ReadRegOperation(lldb::tid_t tid, unsigned offset,
448 RegisterValue &value, bool &result)
449 : m_tid(tid), m_offset(offset),
450 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000451 { }
452
453 void Execute(ProcessMonitor *monitor);
454
455private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000456 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000457 uintptr_t m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000458 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000459 bool &m_result;
460};
461
462void
463ReadRegOperation::Execute(ProcessMonitor *monitor)
464{
Johnny Chen30213ff2012-01-05 19:17:38 +0000465 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000466
467 // Set errno to zero so that we can detect a failed peek.
468 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000469 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
470 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000471 m_result = false;
472 else
473 {
474 m_value = data;
475 m_result = true;
476 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000477 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000478 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000479 POSIXThread::GetRegisterNameFromOffset(m_offset), data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000480}
481
482//------------------------------------------------------------------------------
483/// @class WriteRegOperation
484/// @brief Implements ProcessMonitor::WriteRegisterValue.
485class WriteRegOperation : public Operation
486{
487public:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000488 WriteRegOperation(lldb::tid_t tid, unsigned offset,
489 const RegisterValue &value, bool &result)
490 : m_tid(tid), m_offset(offset),
491 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000492 { }
493
494 void Execute(ProcessMonitor *monitor);
495
496private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000497 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000498 uintptr_t m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000499 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000500 bool &m_result;
501};
502
503void
504WriteRegOperation::Execute(ProcessMonitor *monitor)
505{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000506 void* buf;
Johnny Chen30213ff2012-01-05 19:17:38 +0000507 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000508
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000509#if __WORDSIZE == 32
510 buf = (void*) m_value.GetAsUInt32();
511#else
512 buf = (void*) m_value.GetAsUInt64();
513#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000514
515 if (log)
516 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000517 POSIXThread::GetRegisterNameFromOffset(m_offset), buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000518 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000519 m_result = false;
520 else
521 m_result = true;
522}
523
524//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000525/// @class ReadGPROperation
526/// @brief Implements ProcessMonitor::ReadGPR.
527class ReadGPROperation : public Operation
528{
529public:
Matt Kopec7de48462013-03-06 17:20:48 +0000530 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
531 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000532 { }
533
534 void Execute(ProcessMonitor *monitor);
535
536private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000537 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000538 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000539 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000540 bool &m_result;
541};
542
543void
544ReadGPROperation::Execute(ProcessMonitor *monitor)
545{
Matt Kopec7de48462013-03-06 17:20:48 +0000546 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000547 m_result = false;
548 else
549 m_result = true;
550}
551
552//------------------------------------------------------------------------------
553/// @class ReadFPROperation
554/// @brief Implements ProcessMonitor::ReadFPR.
555class ReadFPROperation : public Operation
556{
557public:
Matt Kopec7de48462013-03-06 17:20:48 +0000558 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
559 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000560 { }
561
562 void Execute(ProcessMonitor *monitor);
563
564private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000565 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000566 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000567 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000568 bool &m_result;
569};
570
571void
572ReadFPROperation::Execute(ProcessMonitor *monitor)
573{
Matt Kopec7de48462013-03-06 17:20:48 +0000574 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000575 m_result = false;
576 else
577 m_result = true;
578}
579
580//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000581/// @class ReadRegisterSetOperation
582/// @brief Implements ProcessMonitor::ReadRegisterSet.
583class ReadRegisterSetOperation : public Operation
584{
585public:
586 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
587 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
588 { }
589
590 void Execute(ProcessMonitor *monitor);
591
592private:
593 lldb::tid_t m_tid;
594 void *m_buf;
595 size_t m_buf_size;
596 const unsigned int m_regset;
597 bool &m_result;
598};
599
600void
601ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
602{
603 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
604 m_result = false;
605 else
606 m_result = true;
607}
608
609//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000610/// @class WriteGPROperation
611/// @brief Implements ProcessMonitor::WriteGPR.
612class WriteGPROperation : public Operation
613{
614public:
Matt Kopec7de48462013-03-06 17:20:48 +0000615 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
616 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000617 { }
618
619 void Execute(ProcessMonitor *monitor);
620
621private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000622 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000623 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000624 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000625 bool &m_result;
626};
627
628void
629WriteGPROperation::Execute(ProcessMonitor *monitor)
630{
Matt Kopec7de48462013-03-06 17:20:48 +0000631 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000632 m_result = false;
633 else
634 m_result = true;
635}
636
637//------------------------------------------------------------------------------
638/// @class WriteFPROperation
639/// @brief Implements ProcessMonitor::WriteFPR.
640class WriteFPROperation : public Operation
641{
642public:
Matt Kopec7de48462013-03-06 17:20:48 +0000643 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
644 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000645 { }
646
647 void Execute(ProcessMonitor *monitor);
648
649private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000650 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000651 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000652 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000653 bool &m_result;
654};
655
656void
657WriteFPROperation::Execute(ProcessMonitor *monitor)
658{
Matt Kopec7de48462013-03-06 17:20:48 +0000659 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000660 m_result = false;
661 else
662 m_result = true;
663}
664
665//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000666/// @class WriteRegisterSetOperation
667/// @brief Implements ProcessMonitor::WriteRegisterSet.
668class WriteRegisterSetOperation : public Operation
669{
670public:
671 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
672 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
673 { }
674
675 void Execute(ProcessMonitor *monitor);
676
677private:
678 lldb::tid_t m_tid;
679 void *m_buf;
680 size_t m_buf_size;
681 const unsigned int m_regset;
682 bool &m_result;
683};
684
685void
686WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
687{
688 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
689 m_result = false;
690 else
691 m_result = true;
692}
693
694//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000695/// @class ResumeOperation
696/// @brief Implements ProcessMonitor::Resume.
697class ResumeOperation : public Operation
698{
699public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000700 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
701 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000702
703 void Execute(ProcessMonitor *monitor);
704
705private:
706 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000707 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000708 bool &m_result;
709};
710
711void
712ResumeOperation::Execute(ProcessMonitor *monitor)
713{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000714 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000715
716 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
717 data = m_signo;
718
Matt Kopec7de48462013-03-06 17:20:48 +0000719 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000720 m_result = false;
721 else
722 m_result = true;
723}
724
725//------------------------------------------------------------------------------
726/// @class ResumeOperation
727/// @brief Implements ProcessMonitor::SingleStep.
728class SingleStepOperation : public Operation
729{
730public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000731 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
732 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000733
734 void Execute(ProcessMonitor *monitor);
735
736private:
737 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000738 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000739 bool &m_result;
740};
741
742void
743SingleStepOperation::Execute(ProcessMonitor *monitor)
744{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000745 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000746
747 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
748 data = m_signo;
749
Matt Kopec7de48462013-03-06 17:20:48 +0000750 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000751 m_result = false;
752 else
753 m_result = true;
754}
755
756//------------------------------------------------------------------------------
757/// @class SiginfoOperation
758/// @brief Implements ProcessMonitor::GetSignalInfo.
759class SiginfoOperation : public Operation
760{
761public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000762 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
763 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000764
765 void Execute(ProcessMonitor *monitor);
766
767private:
768 lldb::tid_t m_tid;
769 void *m_info;
770 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000771 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000772};
773
774void
775SiginfoOperation::Execute(ProcessMonitor *monitor)
776{
Matt Kopec7de48462013-03-06 17:20:48 +0000777 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000778 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000779 m_err = errno;
780 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000781 else
782 m_result = true;
783}
784
785//------------------------------------------------------------------------------
786/// @class EventMessageOperation
787/// @brief Implements ProcessMonitor::GetEventMessage.
788class EventMessageOperation : public Operation
789{
790public:
791 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
792 : m_tid(tid), m_message(message), m_result(result) { }
793
794 void Execute(ProcessMonitor *monitor);
795
796private:
797 lldb::tid_t m_tid;
798 unsigned long *m_message;
799 bool &m_result;
800};
801
802void
803EventMessageOperation::Execute(ProcessMonitor *monitor)
804{
Matt Kopec7de48462013-03-06 17:20:48 +0000805 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000806 m_result = false;
807 else
808 m_result = true;
809}
810
811//------------------------------------------------------------------------------
812/// @class KillOperation
813/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
814class KillOperation : public Operation
815{
816public:
817 KillOperation(bool &result) : m_result(result) { }
818
819 void Execute(ProcessMonitor *monitor);
820
821private:
822 bool &m_result;
823};
824
825void
826KillOperation::Execute(ProcessMonitor *monitor)
827{
828 lldb::pid_t pid = monitor->GetPID();
829
Matt Kopec7de48462013-03-06 17:20:48 +0000830 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000831 m_result = false;
832 else
833 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000834}
835
Greg Clayton28041352011-11-29 20:50:10 +0000836//------------------------------------------------------------------------------
837/// @class KillOperation
838/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
839class DetachOperation : public Operation
840{
841public:
842 DetachOperation(Error &result) : m_error(result) { }
843
844 void Execute(ProcessMonitor *monitor);
845
846private:
847 Error &m_error;
848};
849
850void
851DetachOperation::Execute(ProcessMonitor *monitor)
852{
853 lldb::pid_t pid = monitor->GetPID();
854
855 if (ptrace(PT_DETACH, pid, NULL, 0) < 0)
856 m_error.SetErrorToErrno();
Greg Clayton542e4072012-09-07 17:49:29 +0000857
Greg Clayton28041352011-11-29 20:50:10 +0000858}
859
Johnny Chen25e68e32011-06-14 19:19:50 +0000860ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
861 : m_monitor(monitor)
862{
863 sem_init(&m_semaphore, 0, 0);
864}
865
866ProcessMonitor::OperationArgs::~OperationArgs()
867{
868 sem_destroy(&m_semaphore);
869}
870
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000871ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
872 lldb_private::Module *module,
873 char const **argv,
874 char const **envp,
875 const char *stdin_path,
876 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000877 const char *stderr_path,
878 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000879 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000880 m_module(module),
881 m_argv(argv),
882 m_envp(envp),
883 m_stdin_path(stdin_path),
884 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000885 m_stderr_path(stderr_path),
886 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000887
888ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000889{ }
890
891ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
892 lldb::pid_t pid)
893 : OperationArgs(monitor), m_pid(pid) { }
894
895ProcessMonitor::AttachArgs::~AttachArgs()
896{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000897
898//------------------------------------------------------------------------------
899/// The basic design of the ProcessMonitor is built around two threads.
900///
901/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
902/// for changes in the debugee state. When a change is detected a
903/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
904/// "drives" state changes in the debugger.
905///
906/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000907/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000908/// operations such as register reads/writes, stepping, etc. See the comments
909/// on the Operation class for more info as to why this is needed.
Johnny Chen30213ff2012-01-05 19:17:38 +0000910ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000911 Module *module,
912 const char *argv[],
913 const char *envp[],
914 const char *stdin_path,
915 const char *stdout_path,
916 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000917 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000918 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000919 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000920 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000921 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000922 m_pid(LLDB_INVALID_PROCESS_ID),
923 m_terminal_fd(-1),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000924 m_client_fd(-1),
925 m_server_fd(-1)
926{
Stephen Wilson57740ec2011-01-15 00:12:41 +0000927 std::auto_ptr<LaunchArgs> args;
928
929 args.reset(new LaunchArgs(this, module, argv, envp,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000930 stdin_path, stdout_path, stderr_path, working_dir));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000931
932 // Server/client descriptors.
933 if (!EnableIPC())
934 {
935 error.SetErrorToGenericError();
936 error.SetErrorString("Monitor failed to initialize.");
937 }
938
Johnny Chen25e68e32011-06-14 19:19:50 +0000939 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000940 if (!error.Success())
941 return;
942
943WAIT_AGAIN:
944 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000945 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000946 {
947 if (errno == EINTR)
948 goto WAIT_AGAIN;
949 else
950 {
951 error.SetErrorToErrno();
952 return;
953 }
954 }
955
956 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000957 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000958 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000959 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +0000960 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000961 return;
962 }
963
964 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000965 m_monitor_thread = Host::StartMonitoringChildProcess(
966 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +0000967 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968 {
969 error.SetErrorToGenericError();
970 error.SetErrorString("Process launch failed.");
971 return;
972 }
973}
974
Johnny Chen30213ff2012-01-05 19:17:38 +0000975ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +0000976 lldb::pid_t pid,
977 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000978 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +0000979 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000980 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +0000981 m_pid(LLDB_INVALID_PROCESS_ID),
982 m_terminal_fd(-1),
Matt Kopec7de48462013-03-06 17:20:48 +0000983
Johnny Chen25e68e32011-06-14 19:19:50 +0000984 m_client_fd(-1),
985 m_server_fd(-1)
986{
987 std::auto_ptr<AttachArgs> args;
988
989 args.reset(new AttachArgs(this, pid));
990
991 // Server/client descriptors.
992 if (!EnableIPC())
993 {
994 error.SetErrorToGenericError();
995 error.SetErrorString("Monitor failed to initialize.");
996 }
997
998 StartAttachOpThread(args.get(), error);
999 if (!error.Success())
1000 return;
1001
1002WAIT_AGAIN:
1003 // Wait for the operation thread to initialize.
1004 if (sem_wait(&args->m_semaphore))
1005 {
1006 if (errno == EINTR)
1007 goto WAIT_AGAIN;
1008 else
1009 {
1010 error.SetErrorToErrno();
1011 return;
1012 }
1013 }
1014
Greg Clayton743ecf42012-10-16 20:20:18 +00001015 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001016 if (!args->m_error.Success())
1017 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001018 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001019 error = args->m_error;
1020 return;
1021 }
1022
1023 // Finally, start monitoring the child process for change in state.
1024 m_monitor_thread = Host::StartMonitoringChildProcess(
1025 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1026 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1027 {
1028 error.SetErrorToGenericError();
1029 error.SetErrorString("Process attach failed.");
1030 return;
1031 }
1032}
1033
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001034ProcessMonitor::~ProcessMonitor()
1035{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001036 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001037}
1038
1039//------------------------------------------------------------------------------
1040// Thread setup and tear down.
1041void
Johnny Chen25e68e32011-06-14 19:19:50 +00001042ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001043{
1044 static const char *g_thread_name = "lldb.process.linux.operation";
1045
Stephen Wilsond4182f42011-02-09 20:10:35 +00001046 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001047 return;
1048
1049 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001050 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051}
1052
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001053void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001054ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001055{
1056 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1057
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001058 if (!Launch(args)) {
1059 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001060 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001061 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001062
Stephen Wilson570243b2011-01-19 01:37:06 +00001063 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001064 return NULL;
1065}
1066
1067bool
1068ProcessMonitor::Launch(LaunchArgs *args)
1069{
1070 ProcessMonitor *monitor = args->m_monitor;
1071 ProcessLinux &process = monitor->GetProcess();
1072 const char **argv = args->m_argv;
1073 const char **envp = args->m_envp;
1074 const char *stdin_path = args->m_stdin_path;
1075 const char *stdout_path = args->m_stdout_path;
1076 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001077 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001078
1079 lldb_utility::PseudoTerminal terminal;
1080 const size_t err_len = 1024;
1081 char err_str[err_len];
1082 lldb::pid_t pid;
Matt Kopec650648f2013-01-08 16:30:18 +00001083 long ptrace_opts = 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001084
1085 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001086 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001087
Stephen Wilson57740ec2011-01-15 00:12:41 +00001088 // Propagate the environment if one is not supplied.
1089 if (envp == NULL || envp[0] == NULL)
1090 envp = const_cast<const char **>(environ);
1091
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001092 // Pseudo terminal setup.
1093 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
1094 {
1095 args->m_error.SetErrorToGenericError();
1096 args->m_error.SetErrorString("Could not open controlling TTY.");
1097 goto FINISH;
1098 }
1099
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001100 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001101 {
1102 args->m_error.SetErrorToGenericError();
1103 args->m_error.SetErrorString("Process fork failed.");
1104 goto FINISH;
1105 }
1106
Peter Collingbourne6a520222011-06-14 03:55:58 +00001107 // Recognized child exit status codes.
1108 enum {
1109 ePtraceFailed = 1,
1110 eDupStdinFailed,
1111 eDupStdoutFailed,
1112 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001113 eChdirFailed,
Peter Collingbourne6a520222011-06-14 03:55:58 +00001114 eExecFailed
1115 };
1116
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001117 // Child process.
1118 if (pid == 0)
1119 {
1120 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001121 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001122 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001123
1124 // Do not inherit setgid powers.
1125 setgid(getgid());
1126
1127 // Let us have our own process group.
1128 setpgid(0, 0);
1129
Greg Clayton710dd5a2011-01-08 20:28:42 +00001130 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001131 //
1132 // FIXME: If two or more of the paths are the same we needlessly open
1133 // the same file multiple times.
1134 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001135 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001136 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001137
1138 if (stdout_path != NULL && stdout_path[0])
1139 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001140 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001141
1142 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001143 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001144 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001145
Daniel Malea6217d2a2013-01-08 14:49:22 +00001146 // Change working directory
1147 if (working_dir != NULL && working_dir[0])
1148 if (0 != ::chdir(working_dir))
1149 exit(eChdirFailed);
1150
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001151 // Execute. We should never return.
1152 execve(argv[0],
1153 const_cast<char *const *>(argv),
1154 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001155 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001156 }
1157
1158 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001159 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001160 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001161 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001162 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163 args->m_error.SetErrorToErrno();
1164 goto FINISH;
1165 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001166 else if (WIFEXITED(status))
1167 {
1168 // open, dup or execve likely failed for some reason.
1169 args->m_error.SetErrorToGenericError();
1170 switch (WEXITSTATUS(status))
1171 {
Greg Clayton542e4072012-09-07 17:49:29 +00001172 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001173 args->m_error.SetErrorString("Child ptrace failed.");
1174 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001175 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001176 args->m_error.SetErrorString("Child open stdin failed.");
1177 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001178 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001179 args->m_error.SetErrorString("Child open stdout failed.");
1180 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001181 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001182 args->m_error.SetErrorString("Child open stderr failed.");
1183 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001184 case eChdirFailed:
1185 args->m_error.SetErrorString("Child failed to set working directory.");
1186 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001187 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001188 args->m_error.SetErrorString("Child exec failed.");
1189 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001190 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001191 args->m_error.SetErrorString("Child returned unknown exit status.");
1192 break;
1193 }
1194 goto FINISH;
1195 }
1196 assert(WIFSTOPPED(status) && wpid == pid &&
1197 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001198
1199 // Have the child raise an event on exit. This is used to keep the child in
1200 // limbo until it is destroyed.
Matt Kopec650648f2013-01-08 16:30:18 +00001201 ptrace_opts |= PTRACE_O_TRACEEXIT;
1202
1203 // Have the tracer trace threads which spawn in the inferior process.
1204 ptrace_opts |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
1205
Matt Kopec7de48462013-03-06 17:20:48 +00001206 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 {
1208 args->m_error.SetErrorToErrno();
1209 goto FINISH;
1210 }
1211
1212 // Release the master terminal descriptor and pass it off to the
1213 // ProcessMonitor instance. Similarly stash the inferior pid.
1214 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1215 monitor->m_pid = pid;
1216
Stephen Wilson26977162011-03-23 02:14:42 +00001217 // Set the terminal fd to be in non blocking mode (it simplifies the
1218 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1219 // descriptor to read from).
1220 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1221 goto FINISH;
1222
Johnny Chen30213ff2012-01-05 19:17:38 +00001223 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001224 // FIXME: should we be letting UpdateThreadList handle this?
1225 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001226 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001227 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001228 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001229 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001230
1231 // Let our process instance know the thread has stopped.
1232 process.SendMessage(ProcessMessage::Trace(pid));
1233
1234FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001235 return args->m_error.Success();
1236}
1237
1238bool
1239ProcessMonitor::EnableIPC()
1240{
1241 int fd[2];
1242
1243 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1244 return false;
1245
1246 m_client_fd = fd[0];
1247 m_server_fd = fd[1];
1248 return true;
1249}
1250
Johnny Chen25e68e32011-06-14 19:19:50 +00001251void
1252ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1253{
1254 static const char *g_thread_name = "lldb.process.linux.operation";
1255
1256 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1257 return;
1258
1259 m_operation_thread =
1260 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1261}
1262
Johnny Chen25e68e32011-06-14 19:19:50 +00001263void *
1264ProcessMonitor::AttachOpThread(void *arg)
1265{
1266 AttachArgs *args = static_cast<AttachArgs*>(arg);
1267
Greg Clayton743ecf42012-10-16 20:20:18 +00001268 if (!Attach(args)) {
1269 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001270 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001271 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001272
1273 ServeOperation(args);
1274 return NULL;
1275}
1276
1277bool
1278ProcessMonitor::Attach(AttachArgs *args)
1279{
1280 lldb::pid_t pid = args->m_pid;
1281
1282 ProcessMonitor *monitor = args->m_monitor;
1283 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001284 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001285 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001286
1287 if (pid <= 1)
1288 {
1289 args->m_error.SetErrorToGenericError();
1290 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1291 goto FINISH;
1292 }
1293
1294 // Attach to the requested process.
Matt Kopec7de48462013-03-06 17:20:48 +00001295 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL, 0) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001296 {
1297 args->m_error.SetErrorToErrno();
1298 goto FINISH;
1299 }
1300
1301 int status;
1302 if ((status = waitpid(pid, NULL, 0)) < 0)
1303 {
1304 args->m_error.SetErrorToErrno();
1305 goto FINISH;
1306 }
1307
Greg Clayton926cce72012-10-12 16:10:12 +00001308 monitor->m_pid = pid;
1309
Johnny Chen30213ff2012-01-05 19:17:38 +00001310 // Update the process thread list with the attached thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001311 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001312 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001313 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001314 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001315
1316 // Let our process instance know the thread has stopped.
1317 process.SendMessage(ProcessMessage::Trace(pid));
1318
1319 FINISH:
1320 return args->m_error.Success();
1321}
1322
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001323bool
1324ProcessMonitor::MonitorCallback(void *callback_baton,
1325 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001326 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001327 int signal,
1328 int status)
1329{
1330 ProcessMessage message;
1331 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1332 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001333 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001334 bool stop_monitoring;
1335 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001336 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001337
Daniel Maleaa35970a2012-11-23 18:09:58 +00001338 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1339 if (ptrace_err == EINVAL) {
1340 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1341 if (!monitor->Resume(pid, SIGSTOP)) {
1342 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1343 }
1344 stop_monitoring = false;
1345 } else {
1346 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1347 // this means the child pid is gone (or not being debugged) therefore
1348 // stop the monitor thread.
1349 stop_monitoring = true;
1350 }
1351 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001352 else {
1353 switch (info.si_signo)
1354 {
1355 case SIGTRAP:
1356 message = MonitorSIGTRAP(monitor, &info, pid);
1357 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001358
Stephen Wilson84ffe702011-03-30 15:55:52 +00001359 default:
1360 message = MonitorSignal(monitor, &info, pid);
1361 break;
1362 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001363
Stephen Wilson84ffe702011-03-30 15:55:52 +00001364 process->SendMessage(message);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001365 stop_monitoring = !process->IsAlive();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001366 }
1367
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001368 return stop_monitoring;
1369}
1370
1371ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001372ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001373 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001374{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001375 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001376
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001377 assert(monitor);
1378 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001379
Stephen Wilson84ffe702011-03-30 15:55:52 +00001380 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001381 {
1382 default:
1383 assert(false && "Unexpected SIGTRAP code!");
1384 break;
1385
Matt Kopec650648f2013-01-08 16:30:18 +00001386 case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1387 case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1388 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1389 {
1390 unsigned long tid = 0;
1391 if (!monitor->GetEventMessage(pid, &tid))
1392 tid = -1;
1393 message = ProcessMessage::NewThread(pid, tid);
1394 break;
1395 }
1396
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001397 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1398 {
1399 // The inferior process is about to exit. Maintain the process in a
1400 // state of "limbo" until we are explicitly commanded to detach,
1401 // destroy, resume, etc.
1402 unsigned long data = 0;
1403 if (!monitor->GetEventMessage(pid, &data))
1404 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001405 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001406 break;
1407 }
1408
1409 case 0:
1410 case TRAP_TRACE:
1411 message = ProcessMessage::Trace(pid);
1412 break;
1413
1414 case SI_KERNEL:
1415 case TRAP_BRKPT:
1416 message = ProcessMessage::Break(pid);
1417 break;
1418 }
1419
1420 return message;
1421}
1422
Stephen Wilson84ffe702011-03-30 15:55:52 +00001423ProcessMessage
1424ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001425 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001426{
1427 ProcessMessage message;
1428 int signo = info->si_signo;
1429
1430 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1431 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1432 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1433 //
1434 // IOW, user generated signals never generate what we consider to be a
1435 // "crash".
1436 //
1437 // Similarly, ACK signals generated by this monitor.
1438 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1439 {
1440 if (info->si_pid == getpid())
1441 return ProcessMessage::SignalDelivered(pid, signo);
1442 else
1443 return ProcessMessage::Signal(pid, signo);
1444 }
1445
1446 if (signo == SIGSEGV) {
1447 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1448 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1449 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1450 }
1451
1452 if (signo == SIGILL) {
1453 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1454 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1455 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1456 }
1457
1458 if (signo == SIGFPE) {
1459 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1460 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1461 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1462 }
1463
1464 if (signo == SIGBUS) {
1465 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1466 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1467 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1468 }
1469
1470 // Everything else is "normal" and does not require any special action on
1471 // our part.
1472 return ProcessMessage::Signal(pid, signo);
1473}
1474
1475ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001476ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001477{
1478 ProcessMessage::CrashReason reason;
1479 assert(info->si_signo == SIGSEGV);
1480
1481 reason = ProcessMessage::eInvalidCrashReason;
1482
Greg Clayton542e4072012-09-07 17:49:29 +00001483 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001484 {
1485 default:
1486 assert(false && "unexpected si_code for SIGSEGV");
1487 break;
1488 case SEGV_MAPERR:
1489 reason = ProcessMessage::eInvalidAddress;
1490 break;
1491 case SEGV_ACCERR:
1492 reason = ProcessMessage::ePrivilegedAddress;
1493 break;
1494 }
Greg Clayton542e4072012-09-07 17:49:29 +00001495
Stephen Wilson84ffe702011-03-30 15:55:52 +00001496 return reason;
1497}
1498
1499ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001500ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001501{
1502 ProcessMessage::CrashReason reason;
1503 assert(info->si_signo == SIGILL);
1504
1505 reason = ProcessMessage::eInvalidCrashReason;
1506
1507 switch (info->si_code)
1508 {
1509 default:
1510 assert(false && "unexpected si_code for SIGILL");
1511 break;
1512 case ILL_ILLOPC:
1513 reason = ProcessMessage::eIllegalOpcode;
1514 break;
1515 case ILL_ILLOPN:
1516 reason = ProcessMessage::eIllegalOperand;
1517 break;
1518 case ILL_ILLADR:
1519 reason = ProcessMessage::eIllegalAddressingMode;
1520 break;
1521 case ILL_ILLTRP:
1522 reason = ProcessMessage::eIllegalTrap;
1523 break;
1524 case ILL_PRVOPC:
1525 reason = ProcessMessage::ePrivilegedOpcode;
1526 break;
1527 case ILL_PRVREG:
1528 reason = ProcessMessage::ePrivilegedRegister;
1529 break;
1530 case ILL_COPROC:
1531 reason = ProcessMessage::eCoprocessorError;
1532 break;
1533 case ILL_BADSTK:
1534 reason = ProcessMessage::eInternalStackError;
1535 break;
1536 }
1537
1538 return reason;
1539}
1540
1541ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001542ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001543{
1544 ProcessMessage::CrashReason reason;
1545 assert(info->si_signo == SIGFPE);
1546
1547 reason = ProcessMessage::eInvalidCrashReason;
1548
1549 switch (info->si_code)
1550 {
1551 default:
1552 assert(false && "unexpected si_code for SIGFPE");
1553 break;
1554 case FPE_INTDIV:
1555 reason = ProcessMessage::eIntegerDivideByZero;
1556 break;
1557 case FPE_INTOVF:
1558 reason = ProcessMessage::eIntegerOverflow;
1559 break;
1560 case FPE_FLTDIV:
1561 reason = ProcessMessage::eFloatDivideByZero;
1562 break;
1563 case FPE_FLTOVF:
1564 reason = ProcessMessage::eFloatOverflow;
1565 break;
1566 case FPE_FLTUND:
1567 reason = ProcessMessage::eFloatUnderflow;
1568 break;
1569 case FPE_FLTRES:
1570 reason = ProcessMessage::eFloatInexactResult;
1571 break;
1572 case FPE_FLTINV:
1573 reason = ProcessMessage::eFloatInvalidOperation;
1574 break;
1575 case FPE_FLTSUB:
1576 reason = ProcessMessage::eFloatSubscriptRange;
1577 break;
1578 }
1579
1580 return reason;
1581}
1582
1583ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001584ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001585{
1586 ProcessMessage::CrashReason reason;
1587 assert(info->si_signo == SIGBUS);
1588
1589 reason = ProcessMessage::eInvalidCrashReason;
1590
1591 switch (info->si_code)
1592 {
1593 default:
1594 assert(false && "unexpected si_code for SIGBUS");
1595 break;
1596 case BUS_ADRALN:
1597 reason = ProcessMessage::eIllegalAlignment;
1598 break;
1599 case BUS_ADRERR:
1600 reason = ProcessMessage::eIllegalAddress;
1601 break;
1602 case BUS_OBJERR:
1603 reason = ProcessMessage::eHardwareError;
1604 break;
1605 }
1606
1607 return reason;
1608}
1609
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001610void
Johnny Chen25e68e32011-06-14 19:19:50 +00001611ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001612{
1613 int status;
1614 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001615
Stephen Wilson570243b2011-01-19 01:37:06 +00001616 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001617
1618 fdset.fd = monitor->m_server_fd;
1619 fdset.events = POLLIN | POLLPRI;
1620 fdset.revents = 0;
1621
Stephen Wilson570243b2011-01-19 01:37:06 +00001622 // We are finised with the arguments and are ready to go. Sync with the
1623 // parent thread and start serving operations on the inferior.
1624 sem_post(&args->m_semaphore);
1625
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001626 for (;;)
1627 {
1628 if ((status = poll(&fdset, 1, -1)) < 0)
1629 {
1630 switch (errno)
1631 {
1632 default:
1633 assert(false && "Unexpected poll() failure!");
1634 continue;
1635
1636 case EINTR: continue; // Just poll again.
1637 case EBADF: return; // Connection terminated.
1638 }
1639 }
1640
1641 assert(status == 1 && "Too many descriptors!");
1642
1643 if (fdset.revents & POLLIN)
1644 {
1645 Operation *op = NULL;
1646
1647 READ_AGAIN:
1648 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1649 {
1650 // There is only one acceptable failure.
1651 assert(errno == EINTR);
1652 goto READ_AGAIN;
1653 }
Matt Kopec9eb40a92013-03-14 21:35:26 +00001654 if (status == 0)
1655 continue; // Poll again. The connection probably terminated.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001656 assert(status == sizeof(op));
1657 op->Execute(monitor);
1658 write(fdset.fd, &op, sizeof(op));
1659 }
1660 }
1661}
1662
1663void
1664ProcessMonitor::DoOperation(Operation *op)
1665{
1666 int status;
1667 Operation *ack = NULL;
1668 Mutex::Locker lock(m_server_mutex);
1669
1670 // FIXME: Do proper error checking here.
1671 write(m_client_fd, &op, sizeof(op));
1672
1673READ_AGAIN:
1674 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1675 {
1676 // If interrupted by a signal handler try again. Otherwise the monitor
1677 // thread probably died and we have a stale file descriptor -- abort the
1678 // operation.
1679 if (errno == EINTR)
1680 goto READ_AGAIN;
1681 return;
1682 }
1683
1684 assert(status == sizeof(ack));
1685 assert(ack == op && "Invalid monitor thread response!");
1686}
1687
1688size_t
1689ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1690 Error &error)
1691{
1692 size_t result;
1693 ReadOperation op(vm_addr, buf, size, error, result);
1694 DoOperation(&op);
1695 return result;
1696}
1697
1698size_t
1699ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1700 lldb_private::Error &error)
1701{
1702 size_t result;
1703 WriteOperation op(vm_addr, buf, size, error, result);
1704 DoOperation(&op);
1705 return result;
1706}
1707
1708bool
Matt Kopec7de48462013-03-06 17:20:48 +00001709ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
1710 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001711{
1712 bool result;
Daniel Maleaf0da3712012-12-18 19:50:15 +00001713 ReadRegOperation op(tid, offset, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001714 DoOperation(&op);
1715 return result;
1716}
1717
1718bool
Matt Kopec7de48462013-03-06 17:20:48 +00001719ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
1720 const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001721{
1722 bool result;
Daniel Maleaf0da3712012-12-18 19:50:15 +00001723 WriteRegOperation op(tid, offset, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001724 DoOperation(&op);
1725 return result;
1726}
1727
1728bool
Matt Kopec7de48462013-03-06 17:20:48 +00001729ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001730{
1731 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001732 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001733 DoOperation(&op);
1734 return result;
1735}
1736
1737bool
Matt Kopec7de48462013-03-06 17:20:48 +00001738ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001739{
1740 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001741 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001742 DoOperation(&op);
1743 return result;
1744}
1745
1746bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001747ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1748{
1749 bool result;
1750 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
1751 DoOperation(&op);
1752 return result;
1753}
1754
1755bool
Matt Kopec7de48462013-03-06 17:20:48 +00001756ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001757{
1758 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001759 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001760 DoOperation(&op);
1761 return result;
1762}
1763
1764bool
Matt Kopec7de48462013-03-06 17:20:48 +00001765ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001766{
1767 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001768 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001769 DoOperation(&op);
1770 return result;
1771}
1772
1773bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001774ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1775{
1776 bool result;
1777 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
1778 DoOperation(&op);
1779 return result;
1780}
1781
1782bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001783ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001784{
1785 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001786 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001787 DoOperation(&op);
1788 return result;
1789}
1790
1791bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001792ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001793{
1794 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001795 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001796 DoOperation(&op);
1797 return result;
1798}
1799
1800bool
1801ProcessMonitor::BringProcessIntoLimbo()
1802{
1803 bool result;
1804 KillOperation op(result);
1805 DoOperation(&op);
1806 return result;
1807}
1808
1809bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001810ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001811{
1812 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001813 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001814 DoOperation(&op);
1815 return result;
1816}
1817
1818bool
1819ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1820{
1821 bool result;
1822 EventMessageOperation op(tid, message, result);
1823 DoOperation(&op);
1824 return result;
1825}
1826
Greg Clayton743ecf42012-10-16 20:20:18 +00001827lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +00001828ProcessMonitor::Detach()
1829{
Greg Clayton28041352011-11-29 20:50:10 +00001830 lldb_private::Error error;
Greg Clayton743ecf42012-10-16 20:20:18 +00001831 if (m_pid != LLDB_INVALID_PROCESS_ID) {
1832 DetachOperation op(error);
1833 DoOperation(&op);
1834 }
Greg Clayton743ecf42012-10-16 20:20:18 +00001835 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00001836}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001837
1838bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001839ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1840{
Peter Collingbourne62343202011-06-14 03:55:54 +00001841 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001842
1843 if (target_fd == -1)
1844 return false;
1845
Peter Collingbourne62343202011-06-14 03:55:54 +00001846 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001847}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001848
1849void
1850ProcessMonitor::StopMonitoringChildProcess()
1851{
1852 lldb::thread_result_t thread_result;
1853
Stephen Wilsond4182f42011-02-09 20:10:35 +00001854 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001855 {
1856 Host::ThreadCancel(m_monitor_thread, NULL);
1857 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1858 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1859 }
1860}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001861
1862void
1863ProcessMonitor::StopMonitor()
1864{
1865 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00001866 StopOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001867 CloseFD(m_terminal_fd);
1868 CloseFD(m_client_fd);
1869 CloseFD(m_server_fd);
1870}
1871
1872void
Greg Clayton743ecf42012-10-16 20:20:18 +00001873ProcessMonitor::StopOpThread()
1874{
1875 lldb::thread_result_t result;
1876
1877 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1878 return;
1879
1880 Host::ThreadCancel(m_operation_thread, NULL);
1881 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001882 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00001883}
1884
1885void
Stephen Wilson84ffe702011-03-30 15:55:52 +00001886ProcessMonitor::CloseFD(int &fd)
1887{
1888 if (fd != -1)
1889 {
1890 close(fd);
1891 fd = -1;
1892 }
1893}