blob: f7f7d8c6b733d5c3ba3910145ee6adb5db297ffa [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
Matt Kopece9ea0da2013-05-07 19:29:28 +000050// Support hardware breakpoints in case it has not been defined
51#ifndef TRAP_HWBKPT
52 #define TRAP_HWBKPT 4
53#endif
54
Stephen Wilsone6f9f662010-07-24 02:19:04 +000055using namespace lldb_private;
56
Johnny Chen0d5f2d42011-10-18 18:09:30 +000057// FIXME: this code is host-dependent with respect to types and
58// endianness and needs to be fixed. For example, lldb::addr_t is
59// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
60// 32-bit pointer arguments. This code uses casts to work around the
61// problem.
62
63// We disable the tracing of ptrace calls for integration builds to
64// avoid the additional indirection and checks.
65#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
66
Greg Clayton386ff182011-11-05 01:09:16 +000067static void
68DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
69{
70 uint8_t *ptr = (uint8_t *)bytes;
71 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
72 for(uint32_t i=0; i<loop_count; i++)
73 {
74 s.Printf ("[%x]", *ptr);
75 ptr++;
76 }
77}
78
Matt Kopec58c0b962013-03-20 20:34:35 +000079static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000080{
81 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +000082 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +000083 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +000084
85 if (verbose_log)
86 {
87 switch(req)
88 {
89 case PTRACE_POKETEXT:
90 {
91 DisplayBytes(buf, &data, 8);
92 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
93 break;
94 }
Greg Clayton542e4072012-09-07 17:49:29 +000095 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +000096 {
97 DisplayBytes(buf, &data, 8);
98 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
99 break;
100 }
Greg Clayton542e4072012-09-07 17:49:29 +0000101 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000102 {
103 DisplayBytes(buf, &data, 8);
104 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
105 break;
106 }
Greg Clayton542e4072012-09-07 17:49:29 +0000107 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000108 {
Matt Kopec7de48462013-03-06 17:20:48 +0000109 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000110 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
111 break;
112 }
113 case PTRACE_SETFPREGS:
114 {
Matt Kopec7de48462013-03-06 17:20:48 +0000115 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000116 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
117 break;
118 }
Greg Clayton542e4072012-09-07 17:49:29 +0000119 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000120 {
121 DisplayBytes(buf, data, sizeof(siginfo_t));
122 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
123 break;
124 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000125 case PTRACE_SETREGSET:
126 {
127 // Extract iov_base from data, which is a pointer to the struct IOVEC
128 DisplayBytes(buf, *(void **)data, data_size);
129 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
130 break;
131 }
Greg Clayton386ff182011-11-05 01:09:16 +0000132 default:
133 {
134 }
135 }
136 }
137}
138
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000139// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000140// 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 +0000141extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000142PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000143 const char* reqName, const char* file, int line)
144{
Greg Clayton386ff182011-11-05 01:09:16 +0000145 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000146
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000147 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000148
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000149 if (log)
Matt Kopec58c0b962013-03-20 20:34:35 +0000150 log->Printf("ptrace(%s, %lu, %p, %p, %zu) called from file %s line %d",
Matt Kopec7de48462013-03-06 17:20:48 +0000151 reqName, pid, addr, data, data_size, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000152
Matt Kopec7de48462013-03-06 17:20:48 +0000153 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000154
155 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000156 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
157 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
158 else
159 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000160
Matt Kopec7de48462013-03-06 17:20:48 +0000161 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000162
Matt Kopec7de48462013-03-06 17:20:48 +0000163 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000164 {
165 const char* str;
166 switch (errno)
167 {
168 case ESRCH: str = "ESRCH"; break;
169 case EINVAL: str = "EINVAL"; break;
170 case EBUSY: str = "EBUSY"; break;
171 case EPERM: str = "EPERM"; break;
172 default: str = "<unknown>";
173 }
174 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
175 }
176
177 return result;
178}
179
Matt Kopec7de48462013-03-06 17:20:48 +0000180// Wrapper for ptrace when logging is not required.
181// Sets errno to 0 prior to calling ptrace.
182extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000183PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000184{
Matt Kopec58c0b962013-03-20 20:34:35 +0000185 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000186 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000187 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
188 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
189 else
190 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000191 return result;
192}
193
194#define PTRACE(req, pid, addr, data, data_size) \
195 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000196#else
Matt Kopec7de48462013-03-06 17:20:48 +0000197 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000198#endif
199
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000200//------------------------------------------------------------------------------
201// Static implementations of ProcessMonitor::ReadMemory and
202// ProcessMonitor::WriteMemory. This enables mutual recursion between these
203// functions without needed to go thru the thread funnel.
204
205static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000206DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000207 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
208{
Greg Clayton542e4072012-09-07 17:49:29 +0000209 // ptrace word size is determined by the host, not the child
210 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000211 unsigned char *dst = static_cast<unsigned char*>(buf);
212 size_t bytes_read;
213 size_t remainder;
214 long data;
215
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000216 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000217 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000218 ProcessPOSIXLog::IncNestLevel();
219 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000220 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000221 pid, word_size, (void*)vm_addr, buf, size);
222
223 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000224 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
225 {
226 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000227 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
228 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 {
230 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000231 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000232 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000233 return bytes_read;
234 }
235
236 remainder = size - bytes_read;
237 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000238
239 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000240 for (unsigned i = 0; i < remainder; ++i)
241 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000242
Johnny Chen30213ff2012-01-05 19:17:38 +0000243 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
244 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
245 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
246 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000247 {
248 uintptr_t print_dst = 0;
249 // Format bytes from data by moving into print_dst for log output
250 for (unsigned i = 0; i < remainder; ++i)
251 print_dst |= (((data >> i*8) & 0xFF) << i*8);
252 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
253 (void*)vm_addr, print_dst, (unsigned long)data);
254 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000255
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256 vm_addr += word_size;
257 dst += word_size;
258 }
259
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000262 return bytes_read;
263}
264
265static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000266DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000267 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
268{
Greg Clayton542e4072012-09-07 17:49:29 +0000269 // ptrace word size is determined by the host, not the child
270 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000271 const unsigned char *src = static_cast<const unsigned char*>(buf);
272 size_t bytes_written = 0;
273 size_t remainder;
274
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000275 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000276 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000277 ProcessPOSIXLog::IncNestLevel();
278 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000279 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000280 pid, word_size, (void*)vm_addr, buf, size);
281
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000282 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
283 {
284 remainder = size - bytes_written;
285 remainder = remainder > word_size ? word_size : remainder;
286
287 if (remainder == word_size)
288 {
289 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000290 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000291 for (unsigned i = 0; i < word_size; ++i)
292 data |= (unsigned long)src[i] << i*8;
293
Johnny Chen30213ff2012-01-05 19:17:38 +0000294 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
295 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
296 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
297 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
299 (void*)vm_addr, *(unsigned long*)src, data);
300
Matt Kopec7de48462013-03-06 17:20:48 +0000301 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000302 {
303 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000305 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000306 return bytes_written;
307 }
308 }
309 else
310 {
311 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000312 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000313 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000314 {
315 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000316 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000317 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000318 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000319
320 memcpy(buff, src, remainder);
321
Greg Clayton542e4072012-09-07 17:49:29 +0000322 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000323 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000324 {
325 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000326 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000327 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000328 }
329
Johnny Chen30213ff2012-01-05 19:17:38 +0000330 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
331 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
332 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
333 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000334 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
335 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000336 }
337
338 vm_addr += word_size;
339 src += word_size;
340 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000341 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000342 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000343 return bytes_written;
344}
345
Stephen Wilson26977162011-03-23 02:14:42 +0000346// Simple helper function to ensure flags are enabled on the given file
347// descriptor.
348static bool
349EnsureFDFlags(int fd, int flags, Error &error)
350{
351 int status;
352
353 if ((status = fcntl(fd, F_GETFL)) == -1)
354 {
355 error.SetErrorToErrno();
356 return false;
357 }
358
359 if (fcntl(fd, F_SETFL, status | flags) == -1)
360 {
361 error.SetErrorToErrno();
362 return false;
363 }
364
365 return true;
366}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367
368//------------------------------------------------------------------------------
369/// @class Operation
370/// @brief Represents a ProcessMonitor operation.
371///
372/// Under Linux, it is not possible to ptrace() from any other thread but the
373/// one that spawned or attached to the process from the start. Therefore, when
374/// a ProcessMonitor is asked to deliver or change the state of an inferior
375/// process the operation must be "funneled" to a specific thread to perform the
376/// task. The Operation class provides an abstract base for all services the
377/// ProcessMonitor must perform via the single virtual function Execute, thus
378/// encapsulating the code that needs to run in the privileged context.
379class Operation
380{
381public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000382 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000383 virtual void Execute(ProcessMonitor *monitor) = 0;
384};
385
386//------------------------------------------------------------------------------
387/// @class ReadOperation
388/// @brief Implements ProcessMonitor::ReadMemory.
389class ReadOperation : public Operation
390{
391public:
392 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
393 Error &error, size_t &result)
394 : m_addr(addr), m_buff(buff), m_size(size),
395 m_error(error), m_result(result)
396 { }
397
398 void Execute(ProcessMonitor *monitor);
399
400private:
401 lldb::addr_t m_addr;
402 void *m_buff;
403 size_t m_size;
404 Error &m_error;
405 size_t &m_result;
406};
407
408void
409ReadOperation::Execute(ProcessMonitor *monitor)
410{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000411 lldb::pid_t pid = monitor->GetPID();
412
Greg Clayton542e4072012-09-07 17:49:29 +0000413 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000414}
415
416//------------------------------------------------------------------------------
417/// @class ReadOperation
418/// @brief Implements ProcessMonitor::WriteMemory.
419class WriteOperation : public Operation
420{
421public:
422 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
423 Error &error, size_t &result)
424 : m_addr(addr), m_buff(buff), m_size(size),
425 m_error(error), m_result(result)
426 { }
427
428 void Execute(ProcessMonitor *monitor);
429
430private:
431 lldb::addr_t m_addr;
432 const void *m_buff;
433 size_t m_size;
434 Error &m_error;
435 size_t &m_result;
436};
437
438void
439WriteOperation::Execute(ProcessMonitor *monitor)
440{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000441 lldb::pid_t pid = monitor->GetPID();
442
Greg Clayton542e4072012-09-07 17:49:29 +0000443 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000444}
445
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000446
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000447//------------------------------------------------------------------------------
448/// @class ReadRegOperation
449/// @brief Implements ProcessMonitor::ReadRegisterValue.
450class ReadRegOperation : public Operation
451{
452public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000453 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000454 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000455 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000456 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000457 { }
458
459 void Execute(ProcessMonitor *monitor);
460
461private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000462 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000463 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000464 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000465 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000466 bool &m_result;
467};
468
469void
470ReadRegOperation::Execute(ProcessMonitor *monitor)
471{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000472 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000473
474 // Set errno to zero so that we can detect a failed peek.
475 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000476 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
477 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000478 m_result = false;
479 else
480 {
481 m_value = data;
482 m_result = true;
483 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000484 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000485 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000486 m_reg_name, data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000487}
488
489//------------------------------------------------------------------------------
490/// @class WriteRegOperation
491/// @brief Implements ProcessMonitor::WriteRegisterValue.
492class WriteRegOperation : public Operation
493{
494public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000495 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000496 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000497 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000498 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000499 { }
500
501 void Execute(ProcessMonitor *monitor);
502
503private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000504 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000505 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000506 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000507 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000508 bool &m_result;
509};
510
511void
512WriteRegOperation::Execute(ProcessMonitor *monitor)
513{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000514 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000515 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000516
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000517#if __WORDSIZE == 32
518 buf = (void*) m_value.GetAsUInt32();
519#else
520 buf = (void*) m_value.GetAsUInt64();
521#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000522
523 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000524 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000525 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000526 m_result = false;
527 else
528 m_result = true;
529}
530
531//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000532/// @class ReadGPROperation
533/// @brief Implements ProcessMonitor::ReadGPR.
534class ReadGPROperation : public Operation
535{
536public:
Matt Kopec7de48462013-03-06 17:20:48 +0000537 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
538 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000539 { }
540
541 void Execute(ProcessMonitor *monitor);
542
543private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000544 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000545 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000546 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000547 bool &m_result;
548};
549
550void
551ReadGPROperation::Execute(ProcessMonitor *monitor)
552{
Matt Kopec7de48462013-03-06 17:20:48 +0000553 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000554 m_result = false;
555 else
556 m_result = true;
557}
558
559//------------------------------------------------------------------------------
560/// @class ReadFPROperation
561/// @brief Implements ProcessMonitor::ReadFPR.
562class ReadFPROperation : public Operation
563{
564public:
Matt Kopec7de48462013-03-06 17:20:48 +0000565 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
566 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000567 { }
568
569 void Execute(ProcessMonitor *monitor);
570
571private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000572 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000573 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000574 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000575 bool &m_result;
576};
577
578void
579ReadFPROperation::Execute(ProcessMonitor *monitor)
580{
Matt Kopec7de48462013-03-06 17:20:48 +0000581 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000582 m_result = false;
583 else
584 m_result = true;
585}
586
587//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000588/// @class ReadRegisterSetOperation
589/// @brief Implements ProcessMonitor::ReadRegisterSet.
590class ReadRegisterSetOperation : public Operation
591{
592public:
593 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
594 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
595 { }
596
597 void Execute(ProcessMonitor *monitor);
598
599private:
600 lldb::tid_t m_tid;
601 void *m_buf;
602 size_t m_buf_size;
603 const unsigned int m_regset;
604 bool &m_result;
605};
606
607void
608ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
609{
610 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
611 m_result = false;
612 else
613 m_result = true;
614}
615
616//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000617/// @class WriteGPROperation
618/// @brief Implements ProcessMonitor::WriteGPR.
619class WriteGPROperation : public Operation
620{
621public:
Matt Kopec7de48462013-03-06 17:20:48 +0000622 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
623 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000624 { }
625
626 void Execute(ProcessMonitor *monitor);
627
628private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000629 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000630 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000631 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000632 bool &m_result;
633};
634
635void
636WriteGPROperation::Execute(ProcessMonitor *monitor)
637{
Matt Kopec7de48462013-03-06 17:20:48 +0000638 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000639 m_result = false;
640 else
641 m_result = true;
642}
643
644//------------------------------------------------------------------------------
645/// @class WriteFPROperation
646/// @brief Implements ProcessMonitor::WriteFPR.
647class WriteFPROperation : public Operation
648{
649public:
Matt Kopec7de48462013-03-06 17:20:48 +0000650 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
651 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000652 { }
653
654 void Execute(ProcessMonitor *monitor);
655
656private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000657 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000658 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000659 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000660 bool &m_result;
661};
662
663void
664WriteFPROperation::Execute(ProcessMonitor *monitor)
665{
Matt Kopec7de48462013-03-06 17:20:48 +0000666 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000667 m_result = false;
668 else
669 m_result = true;
670}
671
672//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000673/// @class WriteRegisterSetOperation
674/// @brief Implements ProcessMonitor::WriteRegisterSet.
675class WriteRegisterSetOperation : public Operation
676{
677public:
678 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
679 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
680 { }
681
682 void Execute(ProcessMonitor *monitor);
683
684private:
685 lldb::tid_t m_tid;
686 void *m_buf;
687 size_t m_buf_size;
688 const unsigned int m_regset;
689 bool &m_result;
690};
691
692void
693WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
694{
695 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
696 m_result = false;
697 else
698 m_result = true;
699}
700
701//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000702/// @class ResumeOperation
703/// @brief Implements ProcessMonitor::Resume.
704class ResumeOperation : public Operation
705{
706public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000707 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
708 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000709
710 void Execute(ProcessMonitor *monitor);
711
712private:
713 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000714 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000715 bool &m_result;
716};
717
718void
719ResumeOperation::Execute(ProcessMonitor *monitor)
720{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000721 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000722
723 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
724 data = m_signo;
725
Matt Kopec7de48462013-03-06 17:20:48 +0000726 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000727 m_result = false;
728 else
729 m_result = true;
730}
731
732//------------------------------------------------------------------------------
733/// @class ResumeOperation
734/// @brief Implements ProcessMonitor::SingleStep.
735class SingleStepOperation : public Operation
736{
737public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000738 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
739 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000740
741 void Execute(ProcessMonitor *monitor);
742
743private:
744 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000745 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000746 bool &m_result;
747};
748
749void
750SingleStepOperation::Execute(ProcessMonitor *monitor)
751{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000752 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000753
754 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
755 data = m_signo;
756
Matt Kopec7de48462013-03-06 17:20:48 +0000757 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000758 m_result = false;
759 else
760 m_result = true;
761}
762
763//------------------------------------------------------------------------------
764/// @class SiginfoOperation
765/// @brief Implements ProcessMonitor::GetSignalInfo.
766class SiginfoOperation : public Operation
767{
768public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000769 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
770 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000771
772 void Execute(ProcessMonitor *monitor);
773
774private:
775 lldb::tid_t m_tid;
776 void *m_info;
777 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000778 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000779};
780
781void
782SiginfoOperation::Execute(ProcessMonitor *monitor)
783{
Matt Kopec7de48462013-03-06 17:20:48 +0000784 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000785 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000786 m_err = errno;
787 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000788 else
789 m_result = true;
790}
791
792//------------------------------------------------------------------------------
793/// @class EventMessageOperation
794/// @brief Implements ProcessMonitor::GetEventMessage.
795class EventMessageOperation : public Operation
796{
797public:
798 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
799 : m_tid(tid), m_message(message), m_result(result) { }
800
801 void Execute(ProcessMonitor *monitor);
802
803private:
804 lldb::tid_t m_tid;
805 unsigned long *m_message;
806 bool &m_result;
807};
808
809void
810EventMessageOperation::Execute(ProcessMonitor *monitor)
811{
Matt Kopec7de48462013-03-06 17:20:48 +0000812 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000813 m_result = false;
814 else
815 m_result = true;
816}
817
818//------------------------------------------------------------------------------
819/// @class KillOperation
820/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
821class KillOperation : public Operation
822{
823public:
824 KillOperation(bool &result) : m_result(result) { }
825
826 void Execute(ProcessMonitor *monitor);
827
828private:
829 bool &m_result;
830};
831
832void
833KillOperation::Execute(ProcessMonitor *monitor)
834{
835 lldb::pid_t pid = monitor->GetPID();
836
Matt Kopec7de48462013-03-06 17:20:48 +0000837 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 m_result = false;
839 else
840 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000841}
842
Greg Clayton28041352011-11-29 20:50:10 +0000843//------------------------------------------------------------------------------
844/// @class KillOperation
845/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
846class DetachOperation : public Operation
847{
848public:
849 DetachOperation(Error &result) : m_error(result) { }
850
851 void Execute(ProcessMonitor *monitor);
852
853private:
854 Error &m_error;
855};
856
857void
858DetachOperation::Execute(ProcessMonitor *monitor)
859{
860 lldb::pid_t pid = monitor->GetPID();
861
862 if (ptrace(PT_DETACH, pid, NULL, 0) < 0)
863 m_error.SetErrorToErrno();
Greg Clayton542e4072012-09-07 17:49:29 +0000864
Greg Clayton28041352011-11-29 20:50:10 +0000865}
866
Johnny Chen25e68e32011-06-14 19:19:50 +0000867ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
868 : m_monitor(monitor)
869{
870 sem_init(&m_semaphore, 0, 0);
871}
872
873ProcessMonitor::OperationArgs::~OperationArgs()
874{
875 sem_destroy(&m_semaphore);
876}
877
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000878ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
879 lldb_private::Module *module,
880 char const **argv,
881 char const **envp,
882 const char *stdin_path,
883 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000884 const char *stderr_path,
885 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000886 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000887 m_module(module),
888 m_argv(argv),
889 m_envp(envp),
890 m_stdin_path(stdin_path),
891 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000892 m_stderr_path(stderr_path),
893 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000894
895ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000896{ }
897
898ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
899 lldb::pid_t pid)
900 : OperationArgs(monitor), m_pid(pid) { }
901
902ProcessMonitor::AttachArgs::~AttachArgs()
903{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000904
905//------------------------------------------------------------------------------
906/// The basic design of the ProcessMonitor is built around two threads.
907///
908/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
909/// for changes in the debugee state. When a change is detected a
910/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
911/// "drives" state changes in the debugger.
912///
913/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000914/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000915/// operations such as register reads/writes, stepping, etc. See the comments
916/// on the Operation class for more info as to why this is needed.
Johnny Chen30213ff2012-01-05 19:17:38 +0000917ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000918 Module *module,
919 const char *argv[],
920 const char *envp[],
921 const char *stdin_path,
922 const char *stdout_path,
923 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000924 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000925 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000926 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000927 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000928 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000929 m_pid(LLDB_INVALID_PROCESS_ID),
930 m_terminal_fd(-1),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000931 m_client_fd(-1),
932 m_server_fd(-1)
933{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000934 std::unique_ptr<LaunchArgs> args;
Stephen Wilson57740ec2011-01-15 00:12:41 +0000935
936 args.reset(new LaunchArgs(this, module, argv, envp,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000937 stdin_path, stdout_path, stderr_path, working_dir));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000938
939 // Server/client descriptors.
940 if (!EnableIPC())
941 {
942 error.SetErrorToGenericError();
943 error.SetErrorString("Monitor failed to initialize.");
944 }
945
Johnny Chen25e68e32011-06-14 19:19:50 +0000946 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000947 if (!error.Success())
948 return;
949
950WAIT_AGAIN:
951 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000952 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000953 {
954 if (errno == EINTR)
955 goto WAIT_AGAIN;
956 else
957 {
958 error.SetErrorToErrno();
959 return;
960 }
961 }
962
963 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000964 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000965 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000966 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +0000967 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968 return;
969 }
970
971 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000972 m_monitor_thread = Host::StartMonitoringChildProcess(
973 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +0000974 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975 {
976 error.SetErrorToGenericError();
977 error.SetErrorString("Process launch failed.");
978 return;
979 }
980}
981
Johnny Chen30213ff2012-01-05 19:17:38 +0000982ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +0000983 lldb::pid_t pid,
984 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000985 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +0000986 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000987 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +0000988 m_pid(LLDB_INVALID_PROCESS_ID),
989 m_terminal_fd(-1),
Matt Kopec7de48462013-03-06 17:20:48 +0000990
Johnny Chen25e68e32011-06-14 19:19:50 +0000991 m_client_fd(-1),
992 m_server_fd(-1)
993{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000994 std::unique_ptr<AttachArgs> args;
Johnny Chen25e68e32011-06-14 19:19:50 +0000995
996 args.reset(new AttachArgs(this, pid));
997
998 // Server/client descriptors.
999 if (!EnableIPC())
1000 {
1001 error.SetErrorToGenericError();
1002 error.SetErrorString("Monitor failed to initialize.");
1003 }
1004
1005 StartAttachOpThread(args.get(), error);
1006 if (!error.Success())
1007 return;
1008
1009WAIT_AGAIN:
1010 // Wait for the operation thread to initialize.
1011 if (sem_wait(&args->m_semaphore))
1012 {
1013 if (errno == EINTR)
1014 goto WAIT_AGAIN;
1015 else
1016 {
1017 error.SetErrorToErrno();
1018 return;
1019 }
1020 }
1021
Greg Clayton743ecf42012-10-16 20:20:18 +00001022 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001023 if (!args->m_error.Success())
1024 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001025 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001026 error = args->m_error;
1027 return;
1028 }
1029
1030 // Finally, start monitoring the child process for change in state.
1031 m_monitor_thread = Host::StartMonitoringChildProcess(
1032 ProcessMonitor::MonitorCallback, this, GetPID(), true);
1033 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1034 {
1035 error.SetErrorToGenericError();
1036 error.SetErrorString("Process attach failed.");
1037 return;
1038 }
1039}
1040
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001041ProcessMonitor::~ProcessMonitor()
1042{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001043 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001044}
1045
1046//------------------------------------------------------------------------------
1047// Thread setup and tear down.
1048void
Johnny Chen25e68e32011-06-14 19:19:50 +00001049ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001050{
1051 static const char *g_thread_name = "lldb.process.linux.operation";
1052
Stephen Wilsond4182f42011-02-09 20:10:35 +00001053 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 return;
1055
1056 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +00001057 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001058}
1059
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001060void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001061ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001062{
1063 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1064
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001065 if (!Launch(args)) {
1066 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001067 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001068 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001069
Stephen Wilson570243b2011-01-19 01:37:06 +00001070 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001071 return NULL;
1072}
1073
1074bool
1075ProcessMonitor::Launch(LaunchArgs *args)
1076{
1077 ProcessMonitor *monitor = args->m_monitor;
1078 ProcessLinux &process = monitor->GetProcess();
1079 const char **argv = args->m_argv;
1080 const char **envp = args->m_envp;
1081 const char *stdin_path = args->m_stdin_path;
1082 const char *stdout_path = args->m_stdout_path;
1083 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001084 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001085
1086 lldb_utility::PseudoTerminal terminal;
1087 const size_t err_len = 1024;
1088 char err_str[err_len];
1089 lldb::pid_t pid;
Matt Kopec650648f2013-01-08 16:30:18 +00001090 long ptrace_opts = 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001091
1092 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001093 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001094
Stephen Wilson57740ec2011-01-15 00:12:41 +00001095 // Propagate the environment if one is not supplied.
1096 if (envp == NULL || envp[0] == NULL)
1097 envp = const_cast<const char **>(environ);
1098
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001099 // Pseudo terminal setup.
1100 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
1101 {
1102 args->m_error.SetErrorToGenericError();
1103 args->m_error.SetErrorString("Could not open controlling TTY.");
1104 goto FINISH;
1105 }
1106
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001107 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001108 {
1109 args->m_error.SetErrorToGenericError();
1110 args->m_error.SetErrorString("Process fork failed.");
1111 goto FINISH;
1112 }
1113
Peter Collingbourne6a520222011-06-14 03:55:58 +00001114 // Recognized child exit status codes.
1115 enum {
1116 ePtraceFailed = 1,
1117 eDupStdinFailed,
1118 eDupStdoutFailed,
1119 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001120 eChdirFailed,
Peter Collingbourne6a520222011-06-14 03:55:58 +00001121 eExecFailed
1122 };
1123
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001124 // Child process.
1125 if (pid == 0)
1126 {
1127 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001128 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001129 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130
1131 // Do not inherit setgid powers.
1132 setgid(getgid());
1133
1134 // Let us have our own process group.
1135 setpgid(0, 0);
1136
Greg Clayton710dd5a2011-01-08 20:28:42 +00001137 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138 //
1139 // FIXME: If two or more of the paths are the same we needlessly open
1140 // the same file multiple times.
1141 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001142 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001143 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001144
1145 if (stdout_path != NULL && stdout_path[0])
1146 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001147 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001148
1149 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001150 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001151 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001152
Daniel Malea6217d2a2013-01-08 14:49:22 +00001153 // Change working directory
1154 if (working_dir != NULL && working_dir[0])
1155 if (0 != ::chdir(working_dir))
1156 exit(eChdirFailed);
1157
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001158 // Execute. We should never return.
1159 execve(argv[0],
1160 const_cast<char *const *>(argv),
1161 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001162 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163 }
1164
1165 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001166 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001168 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001169 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001170 args->m_error.SetErrorToErrno();
1171 goto FINISH;
1172 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001173 else if (WIFEXITED(status))
1174 {
1175 // open, dup or execve likely failed for some reason.
1176 args->m_error.SetErrorToGenericError();
1177 switch (WEXITSTATUS(status))
1178 {
Greg Clayton542e4072012-09-07 17:49:29 +00001179 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001180 args->m_error.SetErrorString("Child ptrace failed.");
1181 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001182 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001183 args->m_error.SetErrorString("Child open stdin failed.");
1184 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001185 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001186 args->m_error.SetErrorString("Child open stdout failed.");
1187 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001188 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001189 args->m_error.SetErrorString("Child open stderr failed.");
1190 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001191 case eChdirFailed:
1192 args->m_error.SetErrorString("Child failed to set working directory.");
1193 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001194 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001195 args->m_error.SetErrorString("Child exec failed.");
1196 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001197 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001198 args->m_error.SetErrorString("Child returned unknown exit status.");
1199 break;
1200 }
1201 goto FINISH;
1202 }
1203 assert(WIFSTOPPED(status) && wpid == pid &&
1204 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001205
1206 // Have the child raise an event on exit. This is used to keep the child in
1207 // limbo until it is destroyed.
Matt Kopec650648f2013-01-08 16:30:18 +00001208 ptrace_opts |= PTRACE_O_TRACEEXIT;
1209
1210 // Have the tracer trace threads which spawn in the inferior process.
1211 ptrace_opts |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
1212
Matt Kopec7de48462013-03-06 17:20:48 +00001213 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001214 {
1215 args->m_error.SetErrorToErrno();
1216 goto FINISH;
1217 }
1218
1219 // Release the master terminal descriptor and pass it off to the
1220 // ProcessMonitor instance. Similarly stash the inferior pid.
1221 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1222 monitor->m_pid = pid;
1223
Stephen Wilson26977162011-03-23 02:14:42 +00001224 // Set the terminal fd to be in non blocking mode (it simplifies the
1225 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1226 // descriptor to read from).
1227 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1228 goto FINISH;
1229
Johnny Chen30213ff2012-01-05 19:17:38 +00001230 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001231 // FIXME: should we be letting UpdateThreadList handle this?
1232 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001233 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001234 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001235 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001236 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001237
1238 // Let our process instance know the thread has stopped.
1239 process.SendMessage(ProcessMessage::Trace(pid));
1240
1241FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001242 return args->m_error.Success();
1243}
1244
1245bool
1246ProcessMonitor::EnableIPC()
1247{
1248 int fd[2];
1249
1250 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1251 return false;
1252
1253 m_client_fd = fd[0];
1254 m_server_fd = fd[1];
1255 return true;
1256}
1257
Johnny Chen25e68e32011-06-14 19:19:50 +00001258void
1259ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1260{
1261 static const char *g_thread_name = "lldb.process.linux.operation";
1262
1263 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1264 return;
1265
1266 m_operation_thread =
1267 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1268}
1269
Johnny Chen25e68e32011-06-14 19:19:50 +00001270void *
1271ProcessMonitor::AttachOpThread(void *arg)
1272{
1273 AttachArgs *args = static_cast<AttachArgs*>(arg);
1274
Greg Clayton743ecf42012-10-16 20:20:18 +00001275 if (!Attach(args)) {
1276 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001277 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001278 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001279
1280 ServeOperation(args);
1281 return NULL;
1282}
1283
1284bool
1285ProcessMonitor::Attach(AttachArgs *args)
1286{
1287 lldb::pid_t pid = args->m_pid;
1288
1289 ProcessMonitor *monitor = args->m_monitor;
1290 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001291 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001292 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001293
1294 if (pid <= 1)
1295 {
1296 args->m_error.SetErrorToGenericError();
1297 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1298 goto FINISH;
1299 }
1300
1301 // Attach to the requested process.
Matt Kopec7de48462013-03-06 17:20:48 +00001302 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL, 0) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001303 {
1304 args->m_error.SetErrorToErrno();
1305 goto FINISH;
1306 }
1307
1308 int status;
1309 if ((status = waitpid(pid, NULL, 0)) < 0)
1310 {
1311 args->m_error.SetErrorToErrno();
1312 goto FINISH;
1313 }
1314
Greg Clayton926cce72012-10-12 16:10:12 +00001315 monitor->m_pid = pid;
1316
Johnny Chen30213ff2012-01-05 19:17:38 +00001317 // Update the process thread list with the attached thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001318 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001319 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001320 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001321 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001322
1323 // Let our process instance know the thread has stopped.
1324 process.SendMessage(ProcessMessage::Trace(pid));
1325
1326 FINISH:
1327 return args->m_error.Success();
1328}
1329
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001330bool
1331ProcessMonitor::MonitorCallback(void *callback_baton,
1332 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001333 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001334 int signal,
1335 int status)
1336{
1337 ProcessMessage message;
1338 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1339 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001340 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001341 bool stop_monitoring;
1342 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001343 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001344
Daniel Maleaa35970a2012-11-23 18:09:58 +00001345 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1346 if (ptrace_err == EINVAL) {
1347 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1348 if (!monitor->Resume(pid, SIGSTOP)) {
1349 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1350 }
1351 stop_monitoring = false;
1352 } else {
1353 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1354 // this means the child pid is gone (or not being debugged) therefore
1355 // stop the monitor thread.
1356 stop_monitoring = true;
1357 }
1358 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001359 else {
1360 switch (info.si_signo)
1361 {
1362 case SIGTRAP:
1363 message = MonitorSIGTRAP(monitor, &info, pid);
1364 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001365
Stephen Wilson84ffe702011-03-30 15:55:52 +00001366 default:
1367 message = MonitorSignal(monitor, &info, pid);
1368 break;
1369 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001370
Stephen Wilson84ffe702011-03-30 15:55:52 +00001371 process->SendMessage(message);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001372 stop_monitoring = !process->IsAlive();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001373 }
1374
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001375 return stop_monitoring;
1376}
1377
1378ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001379ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001380 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001381{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001382 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001383
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001384 assert(monitor);
1385 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001386
Stephen Wilson84ffe702011-03-30 15:55:52 +00001387 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001388 {
1389 default:
1390 assert(false && "Unexpected SIGTRAP code!");
1391 break;
1392
Matt Kopec650648f2013-01-08 16:30:18 +00001393 case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1394 case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1395 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1396 {
1397 unsigned long tid = 0;
1398 if (!monitor->GetEventMessage(pid, &tid))
1399 tid = -1;
1400 message = ProcessMessage::NewThread(pid, tid);
1401 break;
1402 }
1403
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001404 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1405 {
1406 // The inferior process is about to exit. Maintain the process in a
1407 // state of "limbo" until we are explicitly commanded to detach,
1408 // destroy, resume, etc.
1409 unsigned long data = 0;
1410 if (!monitor->GetEventMessage(pid, &data))
1411 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001412 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001413 break;
1414 }
1415
1416 case 0:
1417 case TRAP_TRACE:
1418 message = ProcessMessage::Trace(pid);
1419 break;
1420
1421 case SI_KERNEL:
1422 case TRAP_BRKPT:
1423 message = ProcessMessage::Break(pid);
1424 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001425
1426 case TRAP_HWBKPT:
1427 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1428 break;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001429 }
1430
1431 return message;
1432}
1433
Stephen Wilson84ffe702011-03-30 15:55:52 +00001434ProcessMessage
1435ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001436 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001437{
1438 ProcessMessage message;
1439 int signo = info->si_signo;
1440
1441 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1442 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1443 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1444 //
1445 // IOW, user generated signals never generate what we consider to be a
1446 // "crash".
1447 //
1448 // Similarly, ACK signals generated by this monitor.
1449 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1450 {
1451 if (info->si_pid == getpid())
1452 return ProcessMessage::SignalDelivered(pid, signo);
1453 else
1454 return ProcessMessage::Signal(pid, signo);
1455 }
1456
1457 if (signo == SIGSEGV) {
1458 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1459 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1460 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1461 }
1462
1463 if (signo == SIGILL) {
1464 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1465 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1466 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1467 }
1468
1469 if (signo == SIGFPE) {
1470 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1471 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1472 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1473 }
1474
1475 if (signo == SIGBUS) {
1476 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1477 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1478 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1479 }
1480
1481 // Everything else is "normal" and does not require any special action on
1482 // our part.
1483 return ProcessMessage::Signal(pid, signo);
1484}
1485
1486ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001487ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001488{
1489 ProcessMessage::CrashReason reason;
1490 assert(info->si_signo == SIGSEGV);
1491
1492 reason = ProcessMessage::eInvalidCrashReason;
1493
Greg Clayton542e4072012-09-07 17:49:29 +00001494 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001495 {
1496 default:
1497 assert(false && "unexpected si_code for SIGSEGV");
1498 break;
1499 case SEGV_MAPERR:
1500 reason = ProcessMessage::eInvalidAddress;
1501 break;
1502 case SEGV_ACCERR:
1503 reason = ProcessMessage::ePrivilegedAddress;
1504 break;
1505 }
Greg Clayton542e4072012-09-07 17:49:29 +00001506
Stephen Wilson84ffe702011-03-30 15:55:52 +00001507 return reason;
1508}
1509
1510ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001511ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001512{
1513 ProcessMessage::CrashReason reason;
1514 assert(info->si_signo == SIGILL);
1515
1516 reason = ProcessMessage::eInvalidCrashReason;
1517
1518 switch (info->si_code)
1519 {
1520 default:
1521 assert(false && "unexpected si_code for SIGILL");
1522 break;
1523 case ILL_ILLOPC:
1524 reason = ProcessMessage::eIllegalOpcode;
1525 break;
1526 case ILL_ILLOPN:
1527 reason = ProcessMessage::eIllegalOperand;
1528 break;
1529 case ILL_ILLADR:
1530 reason = ProcessMessage::eIllegalAddressingMode;
1531 break;
1532 case ILL_ILLTRP:
1533 reason = ProcessMessage::eIllegalTrap;
1534 break;
1535 case ILL_PRVOPC:
1536 reason = ProcessMessage::ePrivilegedOpcode;
1537 break;
1538 case ILL_PRVREG:
1539 reason = ProcessMessage::ePrivilegedRegister;
1540 break;
1541 case ILL_COPROC:
1542 reason = ProcessMessage::eCoprocessorError;
1543 break;
1544 case ILL_BADSTK:
1545 reason = ProcessMessage::eInternalStackError;
1546 break;
1547 }
1548
1549 return reason;
1550}
1551
1552ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001553ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001554{
1555 ProcessMessage::CrashReason reason;
1556 assert(info->si_signo == SIGFPE);
1557
1558 reason = ProcessMessage::eInvalidCrashReason;
1559
1560 switch (info->si_code)
1561 {
1562 default:
1563 assert(false && "unexpected si_code for SIGFPE");
1564 break;
1565 case FPE_INTDIV:
1566 reason = ProcessMessage::eIntegerDivideByZero;
1567 break;
1568 case FPE_INTOVF:
1569 reason = ProcessMessage::eIntegerOverflow;
1570 break;
1571 case FPE_FLTDIV:
1572 reason = ProcessMessage::eFloatDivideByZero;
1573 break;
1574 case FPE_FLTOVF:
1575 reason = ProcessMessage::eFloatOverflow;
1576 break;
1577 case FPE_FLTUND:
1578 reason = ProcessMessage::eFloatUnderflow;
1579 break;
1580 case FPE_FLTRES:
1581 reason = ProcessMessage::eFloatInexactResult;
1582 break;
1583 case FPE_FLTINV:
1584 reason = ProcessMessage::eFloatInvalidOperation;
1585 break;
1586 case FPE_FLTSUB:
1587 reason = ProcessMessage::eFloatSubscriptRange;
1588 break;
1589 }
1590
1591 return reason;
1592}
1593
1594ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001595ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001596{
1597 ProcessMessage::CrashReason reason;
1598 assert(info->si_signo == SIGBUS);
1599
1600 reason = ProcessMessage::eInvalidCrashReason;
1601
1602 switch (info->si_code)
1603 {
1604 default:
1605 assert(false && "unexpected si_code for SIGBUS");
1606 break;
1607 case BUS_ADRALN:
1608 reason = ProcessMessage::eIllegalAlignment;
1609 break;
1610 case BUS_ADRERR:
1611 reason = ProcessMessage::eIllegalAddress;
1612 break;
1613 case BUS_OBJERR:
1614 reason = ProcessMessage::eHardwareError;
1615 break;
1616 }
1617
1618 return reason;
1619}
1620
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001621void
Johnny Chen25e68e32011-06-14 19:19:50 +00001622ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001623{
1624 int status;
1625 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001626
Stephen Wilson570243b2011-01-19 01:37:06 +00001627 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001628
1629 fdset.fd = monitor->m_server_fd;
1630 fdset.events = POLLIN | POLLPRI;
1631 fdset.revents = 0;
1632
Stephen Wilson570243b2011-01-19 01:37:06 +00001633 // We are finised with the arguments and are ready to go. Sync with the
1634 // parent thread and start serving operations on the inferior.
1635 sem_post(&args->m_semaphore);
1636
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001637 for (;;)
1638 {
1639 if ((status = poll(&fdset, 1, -1)) < 0)
1640 {
1641 switch (errno)
1642 {
1643 default:
1644 assert(false && "Unexpected poll() failure!");
1645 continue;
1646
1647 case EINTR: continue; // Just poll again.
1648 case EBADF: return; // Connection terminated.
1649 }
1650 }
1651
1652 assert(status == 1 && "Too many descriptors!");
1653
1654 if (fdset.revents & POLLIN)
1655 {
1656 Operation *op = NULL;
1657
1658 READ_AGAIN:
1659 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1660 {
1661 // There is only one acceptable failure.
1662 assert(errno == EINTR);
1663 goto READ_AGAIN;
1664 }
Matt Kopec9eb40a92013-03-14 21:35:26 +00001665 if (status == 0)
1666 continue; // Poll again. The connection probably terminated.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001667 assert(status == sizeof(op));
1668 op->Execute(monitor);
1669 write(fdset.fd, &op, sizeof(op));
1670 }
1671 }
1672}
1673
1674void
1675ProcessMonitor::DoOperation(Operation *op)
1676{
1677 int status;
1678 Operation *ack = NULL;
1679 Mutex::Locker lock(m_server_mutex);
1680
1681 // FIXME: Do proper error checking here.
1682 write(m_client_fd, &op, sizeof(op));
1683
1684READ_AGAIN:
1685 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1686 {
1687 // If interrupted by a signal handler try again. Otherwise the monitor
1688 // thread probably died and we have a stale file descriptor -- abort the
1689 // operation.
1690 if (errno == EINTR)
1691 goto READ_AGAIN;
1692 return;
1693 }
1694
1695 assert(status == sizeof(ack));
1696 assert(ack == op && "Invalid monitor thread response!");
1697}
1698
1699size_t
1700ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1701 Error &error)
1702{
1703 size_t result;
1704 ReadOperation op(vm_addr, buf, size, error, result);
1705 DoOperation(&op);
1706 return result;
1707}
1708
1709size_t
1710ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1711 lldb_private::Error &error)
1712{
1713 size_t result;
1714 WriteOperation op(vm_addr, buf, size, error, result);
1715 DoOperation(&op);
1716 return result;
1717}
1718
1719bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001720ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00001721 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001722{
1723 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001724 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001725 DoOperation(&op);
1726 return result;
1727}
1728
1729bool
Matt Kopec7de48462013-03-06 17:20:48 +00001730ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001731 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001732{
1733 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001734 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001735 DoOperation(&op);
1736 return result;
1737}
1738
1739bool
Matt Kopec7de48462013-03-06 17:20:48 +00001740ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001741{
1742 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001743 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001744 DoOperation(&op);
1745 return result;
1746}
1747
1748bool
Matt Kopec7de48462013-03-06 17:20:48 +00001749ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001750{
1751 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001752 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001753 DoOperation(&op);
1754 return result;
1755}
1756
1757bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001758ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1759{
1760 bool result;
1761 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
1762 DoOperation(&op);
1763 return result;
1764}
1765
1766bool
Matt Kopec7de48462013-03-06 17:20:48 +00001767ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001768{
1769 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001770 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001771 DoOperation(&op);
1772 return result;
1773}
1774
1775bool
Matt Kopec7de48462013-03-06 17:20:48 +00001776ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001777{
1778 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001779 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001780 DoOperation(&op);
1781 return result;
1782}
1783
1784bool
Matt Kopec58c0b962013-03-20 20:34:35 +00001785ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1786{
1787 bool result;
1788 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
1789 DoOperation(&op);
1790 return result;
1791}
1792
1793bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001794ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001795{
1796 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001797 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001798 DoOperation(&op);
1799 return result;
1800}
1801
1802bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001803ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001804{
1805 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001806 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001807 DoOperation(&op);
1808 return result;
1809}
1810
1811bool
1812ProcessMonitor::BringProcessIntoLimbo()
1813{
1814 bool result;
1815 KillOperation op(result);
1816 DoOperation(&op);
1817 return result;
1818}
1819
1820bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001821ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001822{
1823 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001824 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001825 DoOperation(&op);
1826 return result;
1827}
1828
1829bool
1830ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1831{
1832 bool result;
1833 EventMessageOperation op(tid, message, result);
1834 DoOperation(&op);
1835 return result;
1836}
1837
Greg Clayton743ecf42012-10-16 20:20:18 +00001838lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +00001839ProcessMonitor::Detach()
1840{
Greg Clayton28041352011-11-29 20:50:10 +00001841 lldb_private::Error error;
Greg Clayton743ecf42012-10-16 20:20:18 +00001842 if (m_pid != LLDB_INVALID_PROCESS_ID) {
1843 DetachOperation op(error);
1844 DoOperation(&op);
1845 }
Greg Clayton743ecf42012-10-16 20:20:18 +00001846 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00001847}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001848
1849bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001850ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1851{
Peter Collingbourne62343202011-06-14 03:55:54 +00001852 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001853
1854 if (target_fd == -1)
1855 return false;
1856
Peter Collingbourne62343202011-06-14 03:55:54 +00001857 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001858}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001859
1860void
1861ProcessMonitor::StopMonitoringChildProcess()
1862{
1863 lldb::thread_result_t thread_result;
1864
Stephen Wilsond4182f42011-02-09 20:10:35 +00001865 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001866 {
1867 Host::ThreadCancel(m_monitor_thread, NULL);
1868 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1869 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1870 }
1871}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001872
1873void
1874ProcessMonitor::StopMonitor()
1875{
1876 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00001877 StopOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001878 CloseFD(m_terminal_fd);
1879 CloseFD(m_client_fd);
1880 CloseFD(m_server_fd);
1881}
1882
1883void
Greg Clayton743ecf42012-10-16 20:20:18 +00001884ProcessMonitor::StopOpThread()
1885{
1886 lldb::thread_result_t result;
1887
1888 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1889 return;
1890
1891 Host::ThreadCancel(m_operation_thread, NULL);
1892 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001893 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00001894}
1895
1896void
Stephen Wilson84ffe702011-03-30 15:55:52 +00001897ProcessMonitor::CloseFD(int &fd)
1898{
1899 if (fd != -1)
1900 {
1901 close(fd);
1902 fd = -1;
1903 }
1904}