blob: 872dffe098c9f1e3c1a673013ae69c669d61e06a [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
Stephen Wilsone6f9f662010-07-24 02:19:04 +000042using namespace lldb_private;
43
Johnny Chen0d5f2d42011-10-18 18:09:30 +000044// FIXME: this code is host-dependent with respect to types and
45// endianness and needs to be fixed. For example, lldb::addr_t is
46// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
47// 32-bit pointer arguments. This code uses casts to work around the
48// problem.
49
50// We disable the tracing of ptrace calls for integration builds to
51// avoid the additional indirection and checks.
52#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
53
Greg Clayton386ff182011-11-05 01:09:16 +000054static void
55DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
56{
57 uint8_t *ptr = (uint8_t *)bytes;
58 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
59 for(uint32_t i=0; i<loop_count; i++)
60 {
61 s.Printf ("[%x]", *ptr);
62 ptr++;
63 }
64}
65
Matt Kopec7de48462013-03-06 17:20:48 +000066static void PtraceDisplayBytes(__ptrace_request &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000067{
68 StreamString buf;
Johnny Chen30213ff2012-01-05 19:17:38 +000069 LogSP verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
70 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +000071
72 if (verbose_log)
73 {
74 switch(req)
75 {
76 case PTRACE_POKETEXT:
77 {
78 DisplayBytes(buf, &data, 8);
79 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
80 break;
81 }
Greg Clayton542e4072012-09-07 17:49:29 +000082 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +000083 {
84 DisplayBytes(buf, &data, 8);
85 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
86 break;
87 }
Greg Clayton542e4072012-09-07 17:49:29 +000088 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +000089 {
90 DisplayBytes(buf, &data, 8);
91 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
92 break;
93 }
Greg Clayton542e4072012-09-07 17:49:29 +000094 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +000095 {
Matt Kopec7de48462013-03-06 17:20:48 +000096 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +000097 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
98 break;
99 }
100 case PTRACE_SETFPREGS:
101 {
Matt Kopec7de48462013-03-06 17:20:48 +0000102 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000103 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
104 break;
105 }
Greg Clayton542e4072012-09-07 17:49:29 +0000106 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000107 {
108 DisplayBytes(buf, data, sizeof(siginfo_t));
109 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
110 break;
111 }
112 default:
113 {
114 }
115 }
116 }
117}
118
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000119// Wrapper for ptrace to catch errors and log calls.
Matt Kopec7de48462013-03-06 17:20:48 +0000120// Note that ptrace sets errno on error because -1 is a valid result for PTRACE_PEEK*
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000121extern long
Matt Kopec7de48462013-03-06 17:20:48 +0000122PtraceWrapper(__ptrace_request req, pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000123 const char* reqName, const char* file, int line)
124{
Greg Clayton386ff182011-11-05 01:09:16 +0000125 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000126
Johnny Chen30213ff2012-01-05 19:17:38 +0000127 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000128
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000129 if (log)
Matt Kopec7de48462013-03-06 17:20:48 +0000130 log->Printf("ptrace(%s, %u, %p, %p, %zu) called from file %s line %d",
131 reqName, pid, addr, data, data_size, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000132
Matt Kopec7de48462013-03-06 17:20:48 +0000133 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000134
135 errno = 0;
136 result = ptrace(req, pid, addr, data);
137
Matt Kopec7de48462013-03-06 17:20:48 +0000138 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000139
Matt Kopec7de48462013-03-06 17:20:48 +0000140 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000141 {
142 const char* str;
143 switch (errno)
144 {
145 case ESRCH: str = "ESRCH"; break;
146 case EINVAL: str = "EINVAL"; break;
147 case EBUSY: str = "EBUSY"; break;
148 case EPERM: str = "EPERM"; break;
149 default: str = "<unknown>";
150 }
151 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
152 }
153
154 return result;
155}
156
Matt Kopec7de48462013-03-06 17:20:48 +0000157// Wrapper for ptrace when logging is not required.
158// Sets errno to 0 prior to calling ptrace.
159extern long
160PtraceWrapper(__ptrace_request req, pid_t pid, void *addr, void *data, size_t data_size)
161{
162 errno = 0;
163 long result = ptrace(req, pid, addr, data);
164 return result;
165}
166
167#define PTRACE(req, pid, addr, data, data_size) \
168 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000169#else
Matt Kopec7de48462013-03-06 17:20:48 +0000170 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000171#endif
172
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000173//------------------------------------------------------------------------------
174// Static implementations of ProcessMonitor::ReadMemory and
175// ProcessMonitor::WriteMemory. This enables mutual recursion between these
176// functions without needed to go thru the thread funnel.
177
178static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000179DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000180 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
181{
Greg Clayton542e4072012-09-07 17:49:29 +0000182 // ptrace word size is determined by the host, not the child
183 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000184 unsigned char *dst = static_cast<unsigned char*>(buf);
185 size_t bytes_read;
186 size_t remainder;
187 long data;
188
Johnny Chen30213ff2012-01-05 19:17:38 +0000189 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000190 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000191 ProcessPOSIXLog::IncNestLevel();
192 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000193 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000194 pid, word_size, (void*)vm_addr, buf, size);
195
196 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000197 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
198 {
199 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000200 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
201 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000202 {
203 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000204 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000205 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000206 return bytes_read;
207 }
208
209 remainder = size - bytes_read;
210 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000211
212 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000213 for (unsigned i = 0; i < remainder; ++i)
214 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000215
Johnny Chen30213ff2012-01-05 19:17:38 +0000216 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
217 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
218 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
219 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000220 {
221 uintptr_t print_dst = 0;
222 // Format bytes from data by moving into print_dst for log output
223 for (unsigned i = 0; i < remainder; ++i)
224 print_dst |= (((data >> i*8) & 0xFF) << i*8);
225 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
226 (void*)vm_addr, print_dst, (unsigned long)data);
227 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000228
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000229 vm_addr += word_size;
230 dst += word_size;
231 }
232
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000233 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000234 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000235 return bytes_read;
236}
237
238static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000239DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000240 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
241{
Greg Clayton542e4072012-09-07 17:49:29 +0000242 // ptrace word size is determined by the host, not the child
243 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000244 const unsigned char *src = static_cast<const unsigned char*>(buf);
245 size_t bytes_written = 0;
246 size_t remainder;
247
Johnny Chen30213ff2012-01-05 19:17:38 +0000248 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000249 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000250 ProcessPOSIXLog::IncNestLevel();
251 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000252 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000253 pid, word_size, (void*)vm_addr, buf, size);
254
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
256 {
257 remainder = size - bytes_written;
258 remainder = remainder > word_size ? word_size : remainder;
259
260 if (remainder == word_size)
261 {
262 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000263 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000264 for (unsigned i = 0; i < word_size; ++i)
265 data |= (unsigned long)src[i] << i*8;
266
Johnny Chen30213ff2012-01-05 19:17:38 +0000267 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
268 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
269 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
270 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000271 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
272 (void*)vm_addr, *(unsigned long*)src, data);
273
Matt Kopec7de48462013-03-06 17:20:48 +0000274 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275 {
276 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000277 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000278 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000279 return bytes_written;
280 }
281 }
282 else
283 {
284 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000285 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000287 {
288 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000289 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000291 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000292
293 memcpy(buff, src, remainder);
294
Greg Clayton542e4072012-09-07 17:49:29 +0000295 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000296 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000297 {
298 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000299 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000300 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000301 }
302
Johnny Chen30213ff2012-01-05 19:17:38 +0000303 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
304 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
305 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
306 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000307 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
308 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000309 }
310
311 vm_addr += word_size;
312 src += word_size;
313 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000314 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000315 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000316 return bytes_written;
317}
318
Stephen Wilson26977162011-03-23 02:14:42 +0000319// Simple helper function to ensure flags are enabled on the given file
320// descriptor.
321static bool
322EnsureFDFlags(int fd, int flags, Error &error)
323{
324 int status;
325
326 if ((status = fcntl(fd, F_GETFL)) == -1)
327 {
328 error.SetErrorToErrno();
329 return false;
330 }
331
332 if (fcntl(fd, F_SETFL, status | flags) == -1)
333 {
334 error.SetErrorToErrno();
335 return false;
336 }
337
338 return true;
339}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000340
341//------------------------------------------------------------------------------
342/// @class Operation
343/// @brief Represents a ProcessMonitor operation.
344///
345/// Under Linux, it is not possible to ptrace() from any other thread but the
346/// one that spawned or attached to the process from the start. Therefore, when
347/// a ProcessMonitor is asked to deliver or change the state of an inferior
348/// process the operation must be "funneled" to a specific thread to perform the
349/// task. The Operation class provides an abstract base for all services the
350/// ProcessMonitor must perform via the single virtual function Execute, thus
351/// encapsulating the code that needs to run in the privileged context.
352class Operation
353{
354public:
355 virtual void Execute(ProcessMonitor *monitor) = 0;
356};
357
358//------------------------------------------------------------------------------
359/// @class ReadOperation
360/// @brief Implements ProcessMonitor::ReadMemory.
361class ReadOperation : public Operation
362{
363public:
364 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
365 Error &error, size_t &result)
366 : m_addr(addr), m_buff(buff), m_size(size),
367 m_error(error), m_result(result)
368 { }
369
370 void Execute(ProcessMonitor *monitor);
371
372private:
373 lldb::addr_t m_addr;
374 void *m_buff;
375 size_t m_size;
376 Error &m_error;
377 size_t &m_result;
378};
379
380void
381ReadOperation::Execute(ProcessMonitor *monitor)
382{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000383 lldb::pid_t pid = monitor->GetPID();
384
Greg Clayton542e4072012-09-07 17:49:29 +0000385 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000386}
387
388//------------------------------------------------------------------------------
389/// @class ReadOperation
390/// @brief Implements ProcessMonitor::WriteMemory.
391class WriteOperation : public Operation
392{
393public:
394 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
395 Error &error, size_t &result)
396 : m_addr(addr), m_buff(buff), m_size(size),
397 m_error(error), m_result(result)
398 { }
399
400 void Execute(ProcessMonitor *monitor);
401
402private:
403 lldb::addr_t m_addr;
404 const void *m_buff;
405 size_t m_size;
406 Error &m_error;
407 size_t &m_result;
408};
409
410void
411WriteOperation::Execute(ProcessMonitor *monitor)
412{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000413 lldb::pid_t pid = monitor->GetPID();
414
Greg Clayton542e4072012-09-07 17:49:29 +0000415 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000416}
417
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000418
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000419//------------------------------------------------------------------------------
420/// @class ReadRegOperation
421/// @brief Implements ProcessMonitor::ReadRegisterValue.
422class ReadRegOperation : public Operation
423{
424public:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000425 ReadRegOperation(lldb::tid_t tid, unsigned offset,
426 RegisterValue &value, bool &result)
427 : m_tid(tid), m_offset(offset),
428 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000429 { }
430
431 void Execute(ProcessMonitor *monitor);
432
433private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000434 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000435 uintptr_t m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000436 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000437 bool &m_result;
438};
439
440void
441ReadRegOperation::Execute(ProcessMonitor *monitor)
442{
Johnny Chen30213ff2012-01-05 19:17:38 +0000443 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000444
445 // Set errno to zero so that we can detect a failed peek.
446 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000447 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
448 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000449 m_result = false;
450 else
451 {
452 m_value = data;
453 m_result = true;
454 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000455 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000456 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000457 POSIXThread::GetRegisterNameFromOffset(m_offset), data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000458}
459
460//------------------------------------------------------------------------------
461/// @class WriteRegOperation
462/// @brief Implements ProcessMonitor::WriteRegisterValue.
463class WriteRegOperation : public Operation
464{
465public:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000466 WriteRegOperation(lldb::tid_t tid, unsigned offset,
467 const RegisterValue &value, bool &result)
468 : m_tid(tid), m_offset(offset),
469 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000470 { }
471
472 void Execute(ProcessMonitor *monitor);
473
474private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000475 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000476 uintptr_t m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000477 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000478 bool &m_result;
479};
480
481void
482WriteRegOperation::Execute(ProcessMonitor *monitor)
483{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000484 void* buf;
Johnny Chen30213ff2012-01-05 19:17:38 +0000485 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000486
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000487#if __WORDSIZE == 32
488 buf = (void*) m_value.GetAsUInt32();
489#else
490 buf = (void*) m_value.GetAsUInt64();
491#endif
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000492
493 if (log)
494 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000495 POSIXThread::GetRegisterNameFromOffset(m_offset), buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000496 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000497 m_result = false;
498 else
499 m_result = true;
500}
501
502//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000503/// @class ReadGPROperation
504/// @brief Implements ProcessMonitor::ReadGPR.
505class ReadGPROperation : public Operation
506{
507public:
Matt Kopec7de48462013-03-06 17:20:48 +0000508 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
509 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000510 { }
511
512 void Execute(ProcessMonitor *monitor);
513
514private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000515 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000516 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000517 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000518 bool &m_result;
519};
520
521void
522ReadGPROperation::Execute(ProcessMonitor *monitor)
523{
Matt Kopec7de48462013-03-06 17:20:48 +0000524 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000525 m_result = false;
526 else
527 m_result = true;
528}
529
530//------------------------------------------------------------------------------
531/// @class ReadFPROperation
532/// @brief Implements ProcessMonitor::ReadFPR.
533class ReadFPROperation : public Operation
534{
535public:
Matt Kopec7de48462013-03-06 17:20:48 +0000536 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
537 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000538 { }
539
540 void Execute(ProcessMonitor *monitor);
541
542private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000543 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000544 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000545 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000546 bool &m_result;
547};
548
549void
550ReadFPROperation::Execute(ProcessMonitor *monitor)
551{
Matt Kopec7de48462013-03-06 17:20:48 +0000552 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000553 m_result = false;
554 else
555 m_result = true;
556}
557
558//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000559/// @class WriteGPROperation
560/// @brief Implements ProcessMonitor::WriteGPR.
561class WriteGPROperation : public Operation
562{
563public:
Matt Kopec7de48462013-03-06 17:20:48 +0000564 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
565 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000566 { }
567
568 void Execute(ProcessMonitor *monitor);
569
570private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000571 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000572 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000573 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000574 bool &m_result;
575};
576
577void
578WriteGPROperation::Execute(ProcessMonitor *monitor)
579{
Matt Kopec7de48462013-03-06 17:20:48 +0000580 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000581 m_result = false;
582 else
583 m_result = true;
584}
585
586//------------------------------------------------------------------------------
587/// @class WriteFPROperation
588/// @brief Implements ProcessMonitor::WriteFPR.
589class WriteFPROperation : public Operation
590{
591public:
Matt Kopec7de48462013-03-06 17:20:48 +0000592 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
593 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000594 { }
595
596 void Execute(ProcessMonitor *monitor);
597
598private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000599 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000600 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000601 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000602 bool &m_result;
603};
604
605void
606WriteFPROperation::Execute(ProcessMonitor *monitor)
607{
Matt Kopec7de48462013-03-06 17:20:48 +0000608 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000609 m_result = false;
610 else
611 m_result = true;
612}
613
614//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000615/// @class ResumeOperation
616/// @brief Implements ProcessMonitor::Resume.
617class ResumeOperation : public Operation
618{
619public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000620 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
621 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000622
623 void Execute(ProcessMonitor *monitor);
624
625private:
626 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000627 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000628 bool &m_result;
629};
630
631void
632ResumeOperation::Execute(ProcessMonitor *monitor)
633{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000634 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000635
636 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
637 data = m_signo;
638
Matt Kopec7de48462013-03-06 17:20:48 +0000639 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000640 m_result = false;
641 else
642 m_result = true;
643}
644
645//------------------------------------------------------------------------------
646/// @class ResumeOperation
647/// @brief Implements ProcessMonitor::SingleStep.
648class SingleStepOperation : public Operation
649{
650public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000651 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
652 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000653
654 void Execute(ProcessMonitor *monitor);
655
656private:
657 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000658 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000659 bool &m_result;
660};
661
662void
663SingleStepOperation::Execute(ProcessMonitor *monitor)
664{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000665 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000666
667 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
668 data = m_signo;
669
Matt Kopec7de48462013-03-06 17:20:48 +0000670 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000671 m_result = false;
672 else
673 m_result = true;
674}
675
676//------------------------------------------------------------------------------
677/// @class SiginfoOperation
678/// @brief Implements ProcessMonitor::GetSignalInfo.
679class SiginfoOperation : public Operation
680{
681public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000682 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
683 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000684
685 void Execute(ProcessMonitor *monitor);
686
687private:
688 lldb::tid_t m_tid;
689 void *m_info;
690 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000691 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000692};
693
694void
695SiginfoOperation::Execute(ProcessMonitor *monitor)
696{
Matt Kopec7de48462013-03-06 17:20:48 +0000697 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000698 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000699 m_err = errno;
700 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000701 else
702 m_result = true;
703}
704
705//------------------------------------------------------------------------------
706/// @class EventMessageOperation
707/// @brief Implements ProcessMonitor::GetEventMessage.
708class EventMessageOperation : public Operation
709{
710public:
711 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
712 : m_tid(tid), m_message(message), m_result(result) { }
713
714 void Execute(ProcessMonitor *monitor);
715
716private:
717 lldb::tid_t m_tid;
718 unsigned long *m_message;
719 bool &m_result;
720};
721
722void
723EventMessageOperation::Execute(ProcessMonitor *monitor)
724{
Matt Kopec7de48462013-03-06 17:20:48 +0000725 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000726 m_result = false;
727 else
728 m_result = true;
729}
730
731//------------------------------------------------------------------------------
732/// @class KillOperation
733/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
734class KillOperation : public Operation
735{
736public:
737 KillOperation(bool &result) : m_result(result) { }
738
739 void Execute(ProcessMonitor *monitor);
740
741private:
742 bool &m_result;
743};
744
745void
746KillOperation::Execute(ProcessMonitor *monitor)
747{
748 lldb::pid_t pid = monitor->GetPID();
749
Matt Kopec7de48462013-03-06 17:20:48 +0000750 if (PTRACE(PTRACE_KILL, pid, NULL, NULL, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000751 m_result = false;
752 else
753 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000754}
755
Greg Clayton28041352011-11-29 20:50:10 +0000756//------------------------------------------------------------------------------
757/// @class KillOperation
758/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
759class DetachOperation : public Operation
760{
761public:
762 DetachOperation(Error &result) : m_error(result) { }
763
764 void Execute(ProcessMonitor *monitor);
765
766private:
767 Error &m_error;
768};
769
770void
771DetachOperation::Execute(ProcessMonitor *monitor)
772{
773 lldb::pid_t pid = monitor->GetPID();
774
775 if (ptrace(PT_DETACH, pid, NULL, 0) < 0)
776 m_error.SetErrorToErrno();
Greg Clayton542e4072012-09-07 17:49:29 +0000777
Greg Clayton28041352011-11-29 20:50:10 +0000778}
779
Johnny Chen25e68e32011-06-14 19:19:50 +0000780ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
781 : m_monitor(monitor)
782{
783 sem_init(&m_semaphore, 0, 0);
784}
785
786ProcessMonitor::OperationArgs::~OperationArgs()
787{
788 sem_destroy(&m_semaphore);
789}
790
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000791ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
792 lldb_private::Module *module,
793 char const **argv,
794 char const **envp,
795 const char *stdin_path,
796 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000797 const char *stderr_path,
798 const char *working_dir)
Johnny Chen25e68e32011-06-14 19:19:50 +0000799 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000800 m_module(module),
801 m_argv(argv),
802 m_envp(envp),
803 m_stdin_path(stdin_path),
804 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000805 m_stderr_path(stderr_path),
806 m_working_dir(working_dir) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000807
808ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000809{ }
810
811ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
812 lldb::pid_t pid)
813 : OperationArgs(monitor), m_pid(pid) { }
814
815ProcessMonitor::AttachArgs::~AttachArgs()
816{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000817
818//------------------------------------------------------------------------------
819/// The basic design of the ProcessMonitor is built around two threads.
820///
821/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
822/// for changes in the debugee state. When a change is detected a
823/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
824/// "drives" state changes in the debugger.
825///
826/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000827/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000828/// operations such as register reads/writes, stepping, etc. See the comments
829/// on the Operation class for more info as to why this is needed.
Johnny Chen30213ff2012-01-05 19:17:38 +0000830ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000831 Module *module,
832 const char *argv[],
833 const char *envp[],
834 const char *stdin_path,
835 const char *stdout_path,
836 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000837 const char *working_dir,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000838 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000839 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000840 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000841 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000842 m_pid(LLDB_INVALID_PROCESS_ID),
843 m_terminal_fd(-1),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000844 m_client_fd(-1),
845 m_server_fd(-1)
846{
Stephen Wilson57740ec2011-01-15 00:12:41 +0000847 std::auto_ptr<LaunchArgs> args;
848
849 args.reset(new LaunchArgs(this, module, argv, envp,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000850 stdin_path, stdout_path, stderr_path, working_dir));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000851
852 // Server/client descriptors.
853 if (!EnableIPC())
854 {
855 error.SetErrorToGenericError();
856 error.SetErrorString("Monitor failed to initialize.");
857 }
858
Johnny Chen25e68e32011-06-14 19:19:50 +0000859 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000860 if (!error.Success())
861 return;
862
863WAIT_AGAIN:
864 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000865 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000866 {
867 if (errno == EINTR)
868 goto WAIT_AGAIN;
869 else
870 {
871 error.SetErrorToErrno();
872 return;
873 }
874 }
875
876 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000877 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000878 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000879 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +0000880 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000881 return;
882 }
883
884 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000885 m_monitor_thread = Host::StartMonitoringChildProcess(
886 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +0000887 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000888 {
889 error.SetErrorToGenericError();
890 error.SetErrorString("Process launch failed.");
891 return;
892 }
893}
894
Johnny Chen30213ff2012-01-05 19:17:38 +0000895ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +0000896 lldb::pid_t pid,
897 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000898 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +0000899 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +0000900 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +0000901 m_pid(LLDB_INVALID_PROCESS_ID),
902 m_terminal_fd(-1),
Matt Kopec7de48462013-03-06 17:20:48 +0000903
Johnny Chen25e68e32011-06-14 19:19:50 +0000904 m_client_fd(-1),
905 m_server_fd(-1)
906{
907 std::auto_ptr<AttachArgs> args;
908
909 args.reset(new AttachArgs(this, pid));
910
911 // Server/client descriptors.
912 if (!EnableIPC())
913 {
914 error.SetErrorToGenericError();
915 error.SetErrorString("Monitor failed to initialize.");
916 }
917
918 StartAttachOpThread(args.get(), error);
919 if (!error.Success())
920 return;
921
922WAIT_AGAIN:
923 // Wait for the operation thread to initialize.
924 if (sem_wait(&args->m_semaphore))
925 {
926 if (errno == EINTR)
927 goto WAIT_AGAIN;
928 else
929 {
930 error.SetErrorToErrno();
931 return;
932 }
933 }
934
Greg Clayton743ecf42012-10-16 20:20:18 +0000935 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +0000936 if (!args->m_error.Success())
937 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000938 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +0000939 error = args->m_error;
940 return;
941 }
942
943 // Finally, start monitoring the child process for change in state.
944 m_monitor_thread = Host::StartMonitoringChildProcess(
945 ProcessMonitor::MonitorCallback, this, GetPID(), true);
946 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
947 {
948 error.SetErrorToGenericError();
949 error.SetErrorString("Process attach failed.");
950 return;
951 }
952}
953
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000954ProcessMonitor::~ProcessMonitor()
955{
Stephen Wilson84ffe702011-03-30 15:55:52 +0000956 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000957}
958
959//------------------------------------------------------------------------------
960// Thread setup and tear down.
961void
Johnny Chen25e68e32011-06-14 19:19:50 +0000962ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000963{
964 static const char *g_thread_name = "lldb.process.linux.operation";
965
Stephen Wilsond4182f42011-02-09 20:10:35 +0000966 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000967 return;
968
969 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +0000970 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000971}
972
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000973void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000974ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975{
976 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
977
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000978 if (!Launch(args)) {
979 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000980 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000981 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000982
Stephen Wilson570243b2011-01-19 01:37:06 +0000983 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000984 return NULL;
985}
986
987bool
988ProcessMonitor::Launch(LaunchArgs *args)
989{
990 ProcessMonitor *monitor = args->m_monitor;
991 ProcessLinux &process = monitor->GetProcess();
992 const char **argv = args->m_argv;
993 const char **envp = args->m_envp;
994 const char *stdin_path = args->m_stdin_path;
995 const char *stdout_path = args->m_stdout_path;
996 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000997 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000998
999 lldb_utility::PseudoTerminal terminal;
1000 const size_t err_len = 1024;
1001 char err_str[err_len];
1002 lldb::pid_t pid;
Matt Kopec650648f2013-01-08 16:30:18 +00001003 long ptrace_opts = 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001004
1005 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001006 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001007
Stephen Wilson57740ec2011-01-15 00:12:41 +00001008 // Propagate the environment if one is not supplied.
1009 if (envp == NULL || envp[0] == NULL)
1010 envp = const_cast<const char **>(environ);
1011
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012 // Pseudo terminal setup.
1013 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
1014 {
1015 args->m_error.SetErrorToGenericError();
1016 args->m_error.SetErrorString("Could not open controlling TTY.");
1017 goto FINISH;
1018 }
1019
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001020 if ((pid = terminal.Fork(err_str, err_len)) == -1)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001021 {
1022 args->m_error.SetErrorToGenericError();
1023 args->m_error.SetErrorString("Process fork failed.");
1024 goto FINISH;
1025 }
1026
Peter Collingbourne6a520222011-06-14 03:55:58 +00001027 // Recognized child exit status codes.
1028 enum {
1029 ePtraceFailed = 1,
1030 eDupStdinFailed,
1031 eDupStdoutFailed,
1032 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001033 eChdirFailed,
Peter Collingbourne6a520222011-06-14 03:55:58 +00001034 eExecFailed
1035 };
1036
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001037 // Child process.
1038 if (pid == 0)
1039 {
1040 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001041 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001042 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001043
1044 // Do not inherit setgid powers.
1045 setgid(getgid());
1046
1047 // Let us have our own process group.
1048 setpgid(0, 0);
1049
Greg Clayton710dd5a2011-01-08 20:28:42 +00001050 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051 //
1052 // FIXME: If two or more of the paths are the same we needlessly open
1053 // the same file multiple times.
1054 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001055 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001056 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001057
1058 if (stdout_path != NULL && stdout_path[0])
1059 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001060 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001061
1062 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001063 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001064 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001065
Daniel Malea6217d2a2013-01-08 14:49:22 +00001066 // Change working directory
1067 if (working_dir != NULL && working_dir[0])
1068 if (0 != ::chdir(working_dir))
1069 exit(eChdirFailed);
1070
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001071 // Execute. We should never return.
1072 execve(argv[0],
1073 const_cast<char *const *>(argv),
1074 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001075 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001076 }
1077
1078 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001079 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001080 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001081 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001082 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001083 args->m_error.SetErrorToErrno();
1084 goto FINISH;
1085 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001086 else if (WIFEXITED(status))
1087 {
1088 // open, dup or execve likely failed for some reason.
1089 args->m_error.SetErrorToGenericError();
1090 switch (WEXITSTATUS(status))
1091 {
Greg Clayton542e4072012-09-07 17:49:29 +00001092 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001093 args->m_error.SetErrorString("Child ptrace failed.");
1094 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001095 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001096 args->m_error.SetErrorString("Child open stdin failed.");
1097 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001098 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001099 args->m_error.SetErrorString("Child open stdout failed.");
1100 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001101 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001102 args->m_error.SetErrorString("Child open stderr failed.");
1103 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001104 case eChdirFailed:
1105 args->m_error.SetErrorString("Child failed to set working directory.");
1106 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001107 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001108 args->m_error.SetErrorString("Child exec failed.");
1109 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001110 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001111 args->m_error.SetErrorString("Child returned unknown exit status.");
1112 break;
1113 }
1114 goto FINISH;
1115 }
1116 assert(WIFSTOPPED(status) && wpid == pid &&
1117 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001118
1119 // Have the child raise an event on exit. This is used to keep the child in
1120 // limbo until it is destroyed.
Matt Kopec650648f2013-01-08 16:30:18 +00001121 ptrace_opts |= PTRACE_O_TRACEEXIT;
1122
1123 // Have the tracer trace threads which spawn in the inferior process.
1124 ptrace_opts |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
1125
Matt Kopec7de48462013-03-06 17:20:48 +00001126 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001127 {
1128 args->m_error.SetErrorToErrno();
1129 goto FINISH;
1130 }
1131
1132 // Release the master terminal descriptor and pass it off to the
1133 // ProcessMonitor instance. Similarly stash the inferior pid.
1134 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1135 monitor->m_pid = pid;
1136
Stephen Wilson26977162011-03-23 02:14:42 +00001137 // Set the terminal fd to be in non blocking mode (it simplifies the
1138 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1139 // descriptor to read from).
1140 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1141 goto FINISH;
1142
Johnny Chen30213ff2012-01-05 19:17:38 +00001143 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001144 // FIXME: should we be letting UpdateThreadList handle this?
1145 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001146 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001147 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001148 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001149 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001150
1151 // Let our process instance know the thread has stopped.
1152 process.SendMessage(ProcessMessage::Trace(pid));
1153
1154FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001155 return args->m_error.Success();
1156}
1157
1158bool
1159ProcessMonitor::EnableIPC()
1160{
1161 int fd[2];
1162
1163 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1164 return false;
1165
1166 m_client_fd = fd[0];
1167 m_server_fd = fd[1];
1168 return true;
1169}
1170
Johnny Chen25e68e32011-06-14 19:19:50 +00001171void
1172ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1173{
1174 static const char *g_thread_name = "lldb.process.linux.operation";
1175
1176 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1177 return;
1178
1179 m_operation_thread =
1180 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1181}
1182
Johnny Chen25e68e32011-06-14 19:19:50 +00001183void *
1184ProcessMonitor::AttachOpThread(void *arg)
1185{
1186 AttachArgs *args = static_cast<AttachArgs*>(arg);
1187
Greg Clayton743ecf42012-10-16 20:20:18 +00001188 if (!Attach(args)) {
1189 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001190 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001191 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001192
1193 ServeOperation(args);
1194 return NULL;
1195}
1196
1197bool
1198ProcessMonitor::Attach(AttachArgs *args)
1199{
1200 lldb::pid_t pid = args->m_pid;
1201
1202 ProcessMonitor *monitor = args->m_monitor;
1203 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001204 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001205 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001206
1207 if (pid <= 1)
1208 {
1209 args->m_error.SetErrorToGenericError();
1210 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1211 goto FINISH;
1212 }
1213
1214 // Attach to the requested process.
Matt Kopec7de48462013-03-06 17:20:48 +00001215 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL, 0) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001216 {
1217 args->m_error.SetErrorToErrno();
1218 goto FINISH;
1219 }
1220
1221 int status;
1222 if ((status = waitpid(pid, NULL, 0)) < 0)
1223 {
1224 args->m_error.SetErrorToErrno();
1225 goto FINISH;
1226 }
1227
Greg Clayton926cce72012-10-12 16:10:12 +00001228 monitor->m_pid = pid;
1229
Johnny Chen30213ff2012-01-05 19:17:38 +00001230 // Update the process thread list with the attached thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001231 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001232 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001233 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001234 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001235
1236 // Let our process instance know the thread has stopped.
1237 process.SendMessage(ProcessMessage::Trace(pid));
1238
1239 FINISH:
1240 return args->m_error.Success();
1241}
1242
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001243bool
1244ProcessMonitor::MonitorCallback(void *callback_baton,
1245 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001246 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001247 int signal,
1248 int status)
1249{
1250 ProcessMessage message;
1251 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1252 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001253 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001254 bool stop_monitoring;
1255 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001256 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001257
Daniel Maleaa35970a2012-11-23 18:09:58 +00001258 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1259 if (ptrace_err == EINVAL) {
1260 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1261 if (!monitor->Resume(pid, SIGSTOP)) {
1262 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1263 }
1264 stop_monitoring = false;
1265 } else {
1266 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1267 // this means the child pid is gone (or not being debugged) therefore
1268 // stop the monitor thread.
1269 stop_monitoring = true;
1270 }
1271 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001272 else {
1273 switch (info.si_signo)
1274 {
1275 case SIGTRAP:
1276 message = MonitorSIGTRAP(monitor, &info, pid);
1277 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001278
Stephen Wilson84ffe702011-03-30 15:55:52 +00001279 default:
1280 message = MonitorSignal(monitor, &info, pid);
1281 break;
1282 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001283
Stephen Wilson84ffe702011-03-30 15:55:52 +00001284 process->SendMessage(message);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001285 stop_monitoring = !process->IsAlive();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001286 }
1287
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001288 return stop_monitoring;
1289}
1290
1291ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001292ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001293 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001294{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001295 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001296
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001297 assert(monitor);
1298 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001299
Stephen Wilson84ffe702011-03-30 15:55:52 +00001300 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301 {
1302 default:
1303 assert(false && "Unexpected SIGTRAP code!");
1304 break;
1305
Matt Kopec650648f2013-01-08 16:30:18 +00001306 case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1307 case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1308 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1309 {
1310 unsigned long tid = 0;
1311 if (!monitor->GetEventMessage(pid, &tid))
1312 tid = -1;
1313 message = ProcessMessage::NewThread(pid, tid);
1314 break;
1315 }
1316
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001317 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1318 {
1319 // The inferior process is about to exit. Maintain the process in a
1320 // state of "limbo" until we are explicitly commanded to detach,
1321 // destroy, resume, etc.
1322 unsigned long data = 0;
1323 if (!monitor->GetEventMessage(pid, &data))
1324 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001325 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001326 break;
1327 }
1328
1329 case 0:
1330 case TRAP_TRACE:
1331 message = ProcessMessage::Trace(pid);
1332 break;
1333
1334 case SI_KERNEL:
1335 case TRAP_BRKPT:
1336 message = ProcessMessage::Break(pid);
1337 break;
1338 }
1339
1340 return message;
1341}
1342
Stephen Wilson84ffe702011-03-30 15:55:52 +00001343ProcessMessage
1344ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001345 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001346{
1347 ProcessMessage message;
1348 int signo = info->si_signo;
1349
1350 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1351 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1352 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1353 //
1354 // IOW, user generated signals never generate what we consider to be a
1355 // "crash".
1356 //
1357 // Similarly, ACK signals generated by this monitor.
1358 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1359 {
1360 if (info->si_pid == getpid())
1361 return ProcessMessage::SignalDelivered(pid, signo);
1362 else
1363 return ProcessMessage::Signal(pid, signo);
1364 }
1365
1366 if (signo == SIGSEGV) {
1367 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1368 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1369 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1370 }
1371
1372 if (signo == SIGILL) {
1373 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1374 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1375 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1376 }
1377
1378 if (signo == SIGFPE) {
1379 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1380 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1381 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1382 }
1383
1384 if (signo == SIGBUS) {
1385 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1386 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1387 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1388 }
1389
1390 // Everything else is "normal" and does not require any special action on
1391 // our part.
1392 return ProcessMessage::Signal(pid, signo);
1393}
1394
1395ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001396ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001397{
1398 ProcessMessage::CrashReason reason;
1399 assert(info->si_signo == SIGSEGV);
1400
1401 reason = ProcessMessage::eInvalidCrashReason;
1402
Greg Clayton542e4072012-09-07 17:49:29 +00001403 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001404 {
1405 default:
1406 assert(false && "unexpected si_code for SIGSEGV");
1407 break;
1408 case SEGV_MAPERR:
1409 reason = ProcessMessage::eInvalidAddress;
1410 break;
1411 case SEGV_ACCERR:
1412 reason = ProcessMessage::ePrivilegedAddress;
1413 break;
1414 }
Greg Clayton542e4072012-09-07 17:49:29 +00001415
Stephen Wilson84ffe702011-03-30 15:55:52 +00001416 return reason;
1417}
1418
1419ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001420ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001421{
1422 ProcessMessage::CrashReason reason;
1423 assert(info->si_signo == SIGILL);
1424
1425 reason = ProcessMessage::eInvalidCrashReason;
1426
1427 switch (info->si_code)
1428 {
1429 default:
1430 assert(false && "unexpected si_code for SIGILL");
1431 break;
1432 case ILL_ILLOPC:
1433 reason = ProcessMessage::eIllegalOpcode;
1434 break;
1435 case ILL_ILLOPN:
1436 reason = ProcessMessage::eIllegalOperand;
1437 break;
1438 case ILL_ILLADR:
1439 reason = ProcessMessage::eIllegalAddressingMode;
1440 break;
1441 case ILL_ILLTRP:
1442 reason = ProcessMessage::eIllegalTrap;
1443 break;
1444 case ILL_PRVOPC:
1445 reason = ProcessMessage::ePrivilegedOpcode;
1446 break;
1447 case ILL_PRVREG:
1448 reason = ProcessMessage::ePrivilegedRegister;
1449 break;
1450 case ILL_COPROC:
1451 reason = ProcessMessage::eCoprocessorError;
1452 break;
1453 case ILL_BADSTK:
1454 reason = ProcessMessage::eInternalStackError;
1455 break;
1456 }
1457
1458 return reason;
1459}
1460
1461ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001462ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001463{
1464 ProcessMessage::CrashReason reason;
1465 assert(info->si_signo == SIGFPE);
1466
1467 reason = ProcessMessage::eInvalidCrashReason;
1468
1469 switch (info->si_code)
1470 {
1471 default:
1472 assert(false && "unexpected si_code for SIGFPE");
1473 break;
1474 case FPE_INTDIV:
1475 reason = ProcessMessage::eIntegerDivideByZero;
1476 break;
1477 case FPE_INTOVF:
1478 reason = ProcessMessage::eIntegerOverflow;
1479 break;
1480 case FPE_FLTDIV:
1481 reason = ProcessMessage::eFloatDivideByZero;
1482 break;
1483 case FPE_FLTOVF:
1484 reason = ProcessMessage::eFloatOverflow;
1485 break;
1486 case FPE_FLTUND:
1487 reason = ProcessMessage::eFloatUnderflow;
1488 break;
1489 case FPE_FLTRES:
1490 reason = ProcessMessage::eFloatInexactResult;
1491 break;
1492 case FPE_FLTINV:
1493 reason = ProcessMessage::eFloatInvalidOperation;
1494 break;
1495 case FPE_FLTSUB:
1496 reason = ProcessMessage::eFloatSubscriptRange;
1497 break;
1498 }
1499
1500 return reason;
1501}
1502
1503ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001504ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001505{
1506 ProcessMessage::CrashReason reason;
1507 assert(info->si_signo == SIGBUS);
1508
1509 reason = ProcessMessage::eInvalidCrashReason;
1510
1511 switch (info->si_code)
1512 {
1513 default:
1514 assert(false && "unexpected si_code for SIGBUS");
1515 break;
1516 case BUS_ADRALN:
1517 reason = ProcessMessage::eIllegalAlignment;
1518 break;
1519 case BUS_ADRERR:
1520 reason = ProcessMessage::eIllegalAddress;
1521 break;
1522 case BUS_OBJERR:
1523 reason = ProcessMessage::eHardwareError;
1524 break;
1525 }
1526
1527 return reason;
1528}
1529
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001530void
Johnny Chen25e68e32011-06-14 19:19:50 +00001531ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001532{
1533 int status;
1534 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001535
Stephen Wilson570243b2011-01-19 01:37:06 +00001536 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001537
1538 fdset.fd = monitor->m_server_fd;
1539 fdset.events = POLLIN | POLLPRI;
1540 fdset.revents = 0;
1541
Stephen Wilson570243b2011-01-19 01:37:06 +00001542 // We are finised with the arguments and are ready to go. Sync with the
1543 // parent thread and start serving operations on the inferior.
1544 sem_post(&args->m_semaphore);
1545
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001546 for (;;)
1547 {
1548 if ((status = poll(&fdset, 1, -1)) < 0)
1549 {
1550 switch (errno)
1551 {
1552 default:
1553 assert(false && "Unexpected poll() failure!");
1554 continue;
1555
1556 case EINTR: continue; // Just poll again.
1557 case EBADF: return; // Connection terminated.
1558 }
1559 }
1560
1561 assert(status == 1 && "Too many descriptors!");
1562
1563 if (fdset.revents & POLLIN)
1564 {
1565 Operation *op = NULL;
1566
1567 READ_AGAIN:
1568 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1569 {
1570 // There is only one acceptable failure.
1571 assert(errno == EINTR);
1572 goto READ_AGAIN;
1573 }
1574
1575 assert(status == sizeof(op));
1576 op->Execute(monitor);
1577 write(fdset.fd, &op, sizeof(op));
1578 }
1579 }
1580}
1581
1582void
1583ProcessMonitor::DoOperation(Operation *op)
1584{
1585 int status;
1586 Operation *ack = NULL;
1587 Mutex::Locker lock(m_server_mutex);
1588
1589 // FIXME: Do proper error checking here.
1590 write(m_client_fd, &op, sizeof(op));
1591
1592READ_AGAIN:
1593 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1594 {
1595 // If interrupted by a signal handler try again. Otherwise the monitor
1596 // thread probably died and we have a stale file descriptor -- abort the
1597 // operation.
1598 if (errno == EINTR)
1599 goto READ_AGAIN;
1600 return;
1601 }
1602
1603 assert(status == sizeof(ack));
1604 assert(ack == op && "Invalid monitor thread response!");
1605}
1606
1607size_t
1608ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1609 Error &error)
1610{
1611 size_t result;
1612 ReadOperation op(vm_addr, buf, size, error, result);
1613 DoOperation(&op);
1614 return result;
1615}
1616
1617size_t
1618ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1619 lldb_private::Error &error)
1620{
1621 size_t result;
1622 WriteOperation op(vm_addr, buf, size, error, result);
1623 DoOperation(&op);
1624 return result;
1625}
1626
1627bool
Matt Kopec7de48462013-03-06 17:20:48 +00001628ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
1629 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001630{
1631 bool result;
Daniel Maleaf0da3712012-12-18 19:50:15 +00001632 ReadRegOperation op(tid, offset, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001633 DoOperation(&op);
1634 return result;
1635}
1636
1637bool
Matt Kopec7de48462013-03-06 17:20:48 +00001638ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
1639 const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001640{
1641 bool result;
Daniel Maleaf0da3712012-12-18 19:50:15 +00001642 WriteRegOperation op(tid, offset, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001643 DoOperation(&op);
1644 return result;
1645}
1646
1647bool
Matt Kopec7de48462013-03-06 17:20:48 +00001648ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001649{
1650 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001651 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001652 DoOperation(&op);
1653 return result;
1654}
1655
1656bool
Matt Kopec7de48462013-03-06 17:20:48 +00001657ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001658{
1659 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001660 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001661 DoOperation(&op);
1662 return result;
1663}
1664
1665bool
Matt Kopec7de48462013-03-06 17:20:48 +00001666ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001667{
1668 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001669 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001670 DoOperation(&op);
1671 return result;
1672}
1673
1674bool
Matt Kopec7de48462013-03-06 17:20:48 +00001675ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001676{
1677 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00001678 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001679 DoOperation(&op);
1680 return result;
1681}
1682
1683bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001684ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001685{
1686 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001687 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001688 DoOperation(&op);
1689 return result;
1690}
1691
1692bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001693ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001694{
1695 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001696 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001697 DoOperation(&op);
1698 return result;
1699}
1700
1701bool
1702ProcessMonitor::BringProcessIntoLimbo()
1703{
1704 bool result;
1705 KillOperation op(result);
1706 DoOperation(&op);
1707 return result;
1708}
1709
1710bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001711ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001712{
1713 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001714 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001715 DoOperation(&op);
1716 return result;
1717}
1718
1719bool
1720ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1721{
1722 bool result;
1723 EventMessageOperation op(tid, message, result);
1724 DoOperation(&op);
1725 return result;
1726}
1727
Greg Clayton743ecf42012-10-16 20:20:18 +00001728lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +00001729ProcessMonitor::Detach()
1730{
Greg Clayton28041352011-11-29 20:50:10 +00001731 lldb_private::Error error;
Greg Clayton743ecf42012-10-16 20:20:18 +00001732 if (m_pid != LLDB_INVALID_PROCESS_ID) {
1733 DetachOperation op(error);
1734 DoOperation(&op);
1735 }
Greg Clayton743ecf42012-10-16 20:20:18 +00001736 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00001737}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001738
1739bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001740ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1741{
Peter Collingbourne62343202011-06-14 03:55:54 +00001742 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001743
1744 if (target_fd == -1)
1745 return false;
1746
Peter Collingbourne62343202011-06-14 03:55:54 +00001747 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001748}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001749
1750void
1751ProcessMonitor::StopMonitoringChildProcess()
1752{
1753 lldb::thread_result_t thread_result;
1754
Stephen Wilsond4182f42011-02-09 20:10:35 +00001755 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001756 {
1757 Host::ThreadCancel(m_monitor_thread, NULL);
1758 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1759 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1760 }
1761}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001762
1763void
1764ProcessMonitor::StopMonitor()
1765{
1766 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00001767 StopOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001768 CloseFD(m_terminal_fd);
1769 CloseFD(m_client_fd);
1770 CloseFD(m_server_fd);
1771}
1772
1773void
Greg Clayton743ecf42012-10-16 20:20:18 +00001774ProcessMonitor::StopOpThread()
1775{
1776 lldb::thread_result_t result;
1777
1778 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1779 return;
1780
1781 Host::ThreadCancel(m_operation_thread, NULL);
1782 Host::ThreadJoin(m_operation_thread, &result, NULL);
Daniel Malea8b9e71e2012-11-22 18:21:05 +00001783 m_operation_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton743ecf42012-10-16 20:20:18 +00001784}
1785
1786void
Stephen Wilson84ffe702011-03-30 15:55:52 +00001787ProcessMonitor::CloseFD(int &fd)
1788{
1789 if (fd != -1)
1790 {
1791 close(fd);
1792 fd = -1;
1793 }
1794}