blob: 46ea84bdf25f1b63b1490ab4fd472fb486e3e425 [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
10// C Includes
11#include <errno.h>
12#include <poll.h>
13#include <string.h>
14#include <unistd.h>
15#include <sys/ptrace.h>
16#include <sys/socket.h>
17#include <sys/types.h>
Benjamin Kramerc2b5c672012-04-07 09:13:49 +000018#include <sys/user.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019#include <sys/wait.h>
20
21// C++ Includes
22// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000023#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000024#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000025#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000026#include "lldb/Core/Scalar.h"
27#include "lldb/Host/Host.h"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Utility/PseudoTerminal.h"
31
Johnny Chen30213ff2012-01-05 19:17:38 +000032#include "POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033#include "ProcessLinux.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000034#include "ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035#include "ProcessMonitor.h"
36
37
Greg Clayton386ff182011-11-05 01:09:16 +000038#define DEBUG_PTRACE_MAXBYTES 20
39
Stephen Wilsone6f9f662010-07-24 02:19:04 +000040using namespace lldb_private;
41
Johnny Chen0d5f2d42011-10-18 18:09:30 +000042// FIXME: this code is host-dependent with respect to types and
43// endianness and needs to be fixed. For example, lldb::addr_t is
44// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
45// 32-bit pointer arguments. This code uses casts to work around the
46// problem.
47
48// We disable the tracing of ptrace calls for integration builds to
49// avoid the additional indirection and checks.
50#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
51
Greg Clayton386ff182011-11-05 01:09:16 +000052static void
53DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
54{
55 uint8_t *ptr = (uint8_t *)bytes;
56 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
57 for(uint32_t i=0; i<loop_count; i++)
58 {
59 s.Printf ("[%x]", *ptr);
60 ptr++;
61 }
62}
63
64static void PtraceDisplayBytes(__ptrace_request &req, void *data)
65{
66 StreamString buf;
Johnny Chen30213ff2012-01-05 19:17:38 +000067 LogSP verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
68 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +000069
70 if (verbose_log)
71 {
72 switch(req)
73 {
74 case PTRACE_POKETEXT:
75 {
76 DisplayBytes(buf, &data, 8);
77 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
78 break;
79 }
Greg Clayton542e4072012-09-07 17:49:29 +000080 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +000081 {
82 DisplayBytes(buf, &data, 8);
83 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
84 break;
85 }
Greg Clayton542e4072012-09-07 17:49:29 +000086 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +000087 {
88 DisplayBytes(buf, &data, 8);
89 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
90 break;
91 }
Greg Clayton542e4072012-09-07 17:49:29 +000092 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +000093 {
94 DisplayBytes(buf, data, sizeof(user_regs_struct));
95 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
96 break;
97 }
98 case PTRACE_SETFPREGS:
99 {
100 DisplayBytes(buf, data, sizeof(user_fpregs_struct));
101 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
102 break;
103 }
Greg Clayton542e4072012-09-07 17:49:29 +0000104 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000105 {
106 DisplayBytes(buf, data, sizeof(siginfo_t));
107 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
108 break;
109 }
110 default:
111 {
112 }
113 }
114 }
115}
116
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000117// Wrapper for ptrace to catch errors and log calls.
118extern long
119PtraceWrapper(__ptrace_request req, pid_t pid, void *addr, void *data,
120 const char* reqName, const char* file, int line)
121{
Greg Clayton386ff182011-11-05 01:09:16 +0000122 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000123
Johnny Chen30213ff2012-01-05 19:17:38 +0000124 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000125
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000126 if (log)
127 log->Printf("ptrace(%s, %u, %p, %p) called from file %s line %d",
128 reqName, pid, addr, data, file, line);
Greg Clayton542e4072012-09-07 17:49:29 +0000129
Greg Clayton386ff182011-11-05 01:09:16 +0000130 PtraceDisplayBytes(req, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000131
132 errno = 0;
133 result = ptrace(req, pid, addr, data);
134
Greg Clayton386ff182011-11-05 01:09:16 +0000135 PtraceDisplayBytes(req, data);
136
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000137 if (log && (result == -1 || errno != 0))
138 {
139 const char* str;
140 switch (errno)
141 {
142 case ESRCH: str = "ESRCH"; break;
143 case EINVAL: str = "EINVAL"; break;
144 case EBUSY: str = "EBUSY"; break;
145 case EPERM: str = "EPERM"; break;
146 default: str = "<unknown>";
147 }
148 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
149 }
150
151 return result;
152}
153
154#define PTRACE(req, pid, addr, data) \
155 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
156#else
157#define PTRACE ptrace
158#endif
159
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000160//------------------------------------------------------------------------------
161// Static implementations of ProcessMonitor::ReadMemory and
162// ProcessMonitor::WriteMemory. This enables mutual recursion between these
163// functions without needed to go thru the thread funnel.
164
165static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000166DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000167 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
168{
Greg Clayton542e4072012-09-07 17:49:29 +0000169 // ptrace word size is determined by the host, not the child
170 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000171 unsigned char *dst = static_cast<unsigned char*>(buf);
172 size_t bytes_read;
173 size_t remainder;
174 long data;
175
Johnny Chen30213ff2012-01-05 19:17:38 +0000176 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000177 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000178 ProcessPOSIXLog::IncNestLevel();
179 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000180 log->Printf ("ProcessMonitor::%s(%d, %d, %p, %p, %d, _)", __FUNCTION__,
181 pid, word_size, (void*)vm_addr, buf, size);
182
183 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000184 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
185 {
186 errno = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000187 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000188 if (data == -1L && errno)
189 {
190 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000191 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000192 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000193 return bytes_read;
194 }
195
196 remainder = size - bytes_read;
197 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000198
199 // Copy the data into our buffer
200 if (log)
201 memset(dst, 0, sizeof(dst));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000202 for (unsigned i = 0; i < remainder; ++i)
203 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000204
Johnny Chen30213ff2012-01-05 19:17:38 +0000205 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
206 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
207 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
208 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000209 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
210 (void*)vm_addr, *(unsigned long*)dst, (unsigned long)data);
211
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000212 vm_addr += word_size;
213 dst += word_size;
214 }
215
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000216 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000217 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000218 return bytes_read;
219}
220
221static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000222DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000223 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
224{
Greg Clayton542e4072012-09-07 17:49:29 +0000225 // ptrace word size is determined by the host, not the child
226 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000227 const unsigned char *src = static_cast<const unsigned char*>(buf);
228 size_t bytes_written = 0;
229 size_t remainder;
230
Johnny Chen30213ff2012-01-05 19:17:38 +0000231 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000232 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000233 ProcessPOSIXLog::IncNestLevel();
234 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000235 log->Printf ("ProcessMonitor::%s(%d, %d, %p, %p, %d, _)", __FUNCTION__,
236 pid, word_size, (void*)vm_addr, buf, size);
237
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000238 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
239 {
240 remainder = size - bytes_written;
241 remainder = remainder > word_size ? word_size : remainder;
242
243 if (remainder == word_size)
244 {
245 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000246 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000247 for (unsigned i = 0; i < word_size; ++i)
248 data |= (unsigned long)src[i] << i*8;
249
Johnny Chen30213ff2012-01-05 19:17:38 +0000250 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
251 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
252 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
253 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000254 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
255 (void*)vm_addr, *(unsigned long*)src, data);
256
257 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000258 {
259 error.SetErrorToErrno();
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_written;
263 }
264 }
265 else
266 {
267 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000268 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000269 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000270 {
271 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000272 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000273 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000274 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275
276 memcpy(buff, src, remainder);
277
Greg Clayton542e4072012-09-07 17:49:29 +0000278 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000279 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000280 {
281 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000282 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000283 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000284 }
285
Johnny Chen30213ff2012-01-05 19:17:38 +0000286 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
287 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
288 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
289 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000290 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
291 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000292 }
293
294 vm_addr += word_size;
295 src += word_size;
296 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000297 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000298 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000299 return bytes_written;
300}
301
Stephen Wilson26977162011-03-23 02:14:42 +0000302// Simple helper function to ensure flags are enabled on the given file
303// descriptor.
304static bool
305EnsureFDFlags(int fd, int flags, Error &error)
306{
307 int status;
308
309 if ((status = fcntl(fd, F_GETFL)) == -1)
310 {
311 error.SetErrorToErrno();
312 return false;
313 }
314
315 if (fcntl(fd, F_SETFL, status | flags) == -1)
316 {
317 error.SetErrorToErrno();
318 return false;
319 }
320
321 return true;
322}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000323
324//------------------------------------------------------------------------------
325/// @class Operation
326/// @brief Represents a ProcessMonitor operation.
327///
328/// Under Linux, it is not possible to ptrace() from any other thread but the
329/// one that spawned or attached to the process from the start. Therefore, when
330/// a ProcessMonitor is asked to deliver or change the state of an inferior
331/// process the operation must be "funneled" to a specific thread to perform the
332/// task. The Operation class provides an abstract base for all services the
333/// ProcessMonitor must perform via the single virtual function Execute, thus
334/// encapsulating the code that needs to run in the privileged context.
335class Operation
336{
337public:
338 virtual void Execute(ProcessMonitor *monitor) = 0;
339};
340
341//------------------------------------------------------------------------------
342/// @class ReadOperation
343/// @brief Implements ProcessMonitor::ReadMemory.
344class ReadOperation : public Operation
345{
346public:
347 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
348 Error &error, size_t &result)
349 : m_addr(addr), m_buff(buff), m_size(size),
350 m_error(error), m_result(result)
351 { }
352
353 void Execute(ProcessMonitor *monitor);
354
355private:
356 lldb::addr_t m_addr;
357 void *m_buff;
358 size_t m_size;
359 Error &m_error;
360 size_t &m_result;
361};
362
363void
364ReadOperation::Execute(ProcessMonitor *monitor)
365{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000366 lldb::pid_t pid = monitor->GetPID();
367
Greg Clayton542e4072012-09-07 17:49:29 +0000368 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000369}
370
371//------------------------------------------------------------------------------
372/// @class ReadOperation
373/// @brief Implements ProcessMonitor::WriteMemory.
374class WriteOperation : public Operation
375{
376public:
377 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
378 Error &error, size_t &result)
379 : m_addr(addr), m_buff(buff), m_size(size),
380 m_error(error), m_result(result)
381 { }
382
383 void Execute(ProcessMonitor *monitor);
384
385private:
386 lldb::addr_t m_addr;
387 const void *m_buff;
388 size_t m_size;
389 Error &m_error;
390 size_t &m_result;
391};
392
393void
394WriteOperation::Execute(ProcessMonitor *monitor)
395{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000396 lldb::pid_t pid = monitor->GetPID();
397
Greg Clayton542e4072012-09-07 17:49:29 +0000398 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000399}
400
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000401
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000402//------------------------------------------------------------------------------
403/// @class ReadRegOperation
404/// @brief Implements ProcessMonitor::ReadRegisterValue.
405class ReadRegOperation : public Operation
406{
407public:
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000408 ReadRegOperation(unsigned offset, RegisterValue &value, bool &result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000409 : m_offset(offset), m_value(value), m_result(result)
410 { }
411
412 void Execute(ProcessMonitor *monitor);
413
414private:
415 unsigned m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000416 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000417 bool &m_result;
418};
419
420void
421ReadRegOperation::Execute(ProcessMonitor *monitor)
422{
423 lldb::pid_t pid = monitor->GetPID();
Johnny Chen30213ff2012-01-05 19:17:38 +0000424 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000425
426 // Set errno to zero so that we can detect a failed peek.
427 errno = 0;
Greg Clayton386ff182011-11-05 01:09:16 +0000428 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, pid, (void*)m_offset, NULL);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000429 if (data == -1UL && errno)
430 m_result = false;
431 else
432 {
433 m_value = data;
434 m_result = true;
435 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000436 if (log)
437 log->Printf ("ProcessMonitor::%s() reg %s: 0x%x", __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000438 POSIXThread::GetRegisterNameFromOffset(m_offset), data);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000439}
440
441//------------------------------------------------------------------------------
442/// @class WriteRegOperation
443/// @brief Implements ProcessMonitor::WriteRegisterValue.
444class WriteRegOperation : public Operation
445{
446public:
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000447 WriteRegOperation(unsigned offset, const RegisterValue &value, bool &result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000448 : m_offset(offset), m_value(value), m_result(result)
449 { }
450
451 void Execute(ProcessMonitor *monitor);
452
453private:
454 unsigned m_offset;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000455 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000456 bool &m_result;
457};
458
459void
460WriteRegOperation::Execute(ProcessMonitor *monitor)
461{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000462 void* buf;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000463 lldb::pid_t pid = monitor->GetPID();
Johnny Chen30213ff2012-01-05 19:17:38 +0000464 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000465
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000466 if (sizeof(void*) == sizeof(uint64_t))
467 buf = (void*) m_value.GetAsUInt64();
468 else
469 {
470 assert(sizeof(void*) == sizeof(uint32_t));
471 buf = (void*) m_value.GetAsUInt32();
472 }
473
474 if (log)
475 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__,
Johnny Chen30213ff2012-01-05 19:17:38 +0000476 POSIXThread::GetRegisterNameFromOffset(m_offset), buf);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000477 if (PTRACE(PTRACE_POKEUSER, pid, (void*)m_offset, buf))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000478 m_result = false;
479 else
480 m_result = true;
481}
482
483//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000484/// @class ReadGPROperation
485/// @brief Implements ProcessMonitor::ReadGPR.
486class ReadGPROperation : public Operation
487{
488public:
489 ReadGPROperation(void *buf, bool &result)
490 : m_buf(buf), m_result(result)
491 { }
492
493 void Execute(ProcessMonitor *monitor);
494
495private:
496 void *m_buf;
497 bool &m_result;
498};
499
500void
501ReadGPROperation::Execute(ProcessMonitor *monitor)
502{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000503 if (PTRACE(PTRACE_GETREGS, monitor->GetPID(), NULL, m_buf) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000504 m_result = false;
505 else
506 m_result = true;
507}
508
509//------------------------------------------------------------------------------
510/// @class ReadFPROperation
511/// @brief Implements ProcessMonitor::ReadFPR.
512class ReadFPROperation : public Operation
513{
514public:
515 ReadFPROperation(void *buf, bool &result)
516 : m_buf(buf), m_result(result)
517 { }
518
519 void Execute(ProcessMonitor *monitor);
520
521private:
522 void *m_buf;
523 bool &m_result;
524};
525
526void
527ReadFPROperation::Execute(ProcessMonitor *monitor)
528{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000529 if (PTRACE(PTRACE_GETFPREGS, monitor->GetPID(), NULL, m_buf) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000530 m_result = false;
531 else
532 m_result = true;
533}
534
535//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000536/// @class WriteGPROperation
537/// @brief Implements ProcessMonitor::WriteGPR.
538class WriteGPROperation : public Operation
539{
540public:
541 WriteGPROperation(void *buf, bool &result)
542 : m_buf(buf), m_result(result)
543 { }
544
545 void Execute(ProcessMonitor *monitor);
546
547private:
548 void *m_buf;
549 bool &m_result;
550};
551
552void
553WriteGPROperation::Execute(ProcessMonitor *monitor)
554{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000555 if (PTRACE(PTRACE_SETREGS, monitor->GetPID(), NULL, m_buf) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000556 m_result = false;
557 else
558 m_result = true;
559}
560
561//------------------------------------------------------------------------------
562/// @class WriteFPROperation
563/// @brief Implements ProcessMonitor::WriteFPR.
564class WriteFPROperation : public Operation
565{
566public:
567 WriteFPROperation(void *buf, bool &result)
568 : m_buf(buf), m_result(result)
569 { }
570
571 void Execute(ProcessMonitor *monitor);
572
573private:
574 void *m_buf;
575 bool &m_result;
576};
577
578void
579WriteFPROperation::Execute(ProcessMonitor *monitor)
580{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000581 if (PTRACE(PTRACE_SETFPREGS, monitor->GetPID(), NULL, m_buf) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000582 m_result = false;
583 else
584 m_result = true;
585}
586
587//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000588/// @class ResumeOperation
589/// @brief Implements ProcessMonitor::Resume.
590class ResumeOperation : public Operation
591{
592public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000593 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
594 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000595
596 void Execute(ProcessMonitor *monitor);
597
598private:
599 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000600 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000601 bool &m_result;
602};
603
604void
605ResumeOperation::Execute(ProcessMonitor *monitor)
606{
Stephen Wilson84ffe702011-03-30 15:55:52 +0000607 int data = 0;
608
609 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
610 data = m_signo;
611
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000612 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000613 m_result = false;
614 else
615 m_result = true;
616}
617
618//------------------------------------------------------------------------------
619/// @class ResumeOperation
620/// @brief Implements ProcessMonitor::SingleStep.
621class SingleStepOperation : public Operation
622{
623public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000624 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
625 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000626
627 void Execute(ProcessMonitor *monitor);
628
629private:
630 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000631 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000632 bool &m_result;
633};
634
635void
636SingleStepOperation::Execute(ProcessMonitor *monitor)
637{
Stephen Wilson84ffe702011-03-30 15:55:52 +0000638 int data = 0;
639
640 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
641 data = m_signo;
642
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000643 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000644 m_result = false;
645 else
646 m_result = true;
647}
648
649//------------------------------------------------------------------------------
650/// @class SiginfoOperation
651/// @brief Implements ProcessMonitor::GetSignalInfo.
652class SiginfoOperation : public Operation
653{
654public:
655 SiginfoOperation(lldb::tid_t tid, void *info, bool &result)
656 : m_tid(tid), m_info(info), m_result(result) { }
657
658 void Execute(ProcessMonitor *monitor);
659
660private:
661 lldb::tid_t m_tid;
662 void *m_info;
663 bool &m_result;
664};
665
666void
667SiginfoOperation::Execute(ProcessMonitor *monitor)
668{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000669 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000670 m_result = false;
671 else
672 m_result = true;
673}
674
675//------------------------------------------------------------------------------
676/// @class EventMessageOperation
677/// @brief Implements ProcessMonitor::GetEventMessage.
678class EventMessageOperation : public Operation
679{
680public:
681 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
682 : m_tid(tid), m_message(message), m_result(result) { }
683
684 void Execute(ProcessMonitor *monitor);
685
686private:
687 lldb::tid_t m_tid;
688 unsigned long *m_message;
689 bool &m_result;
690};
691
692void
693EventMessageOperation::Execute(ProcessMonitor *monitor)
694{
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000695 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000696 m_result = false;
697 else
698 m_result = true;
699}
700
701//------------------------------------------------------------------------------
702/// @class KillOperation
703/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
704class KillOperation : public Operation
705{
706public:
707 KillOperation(bool &result) : m_result(result) { }
708
709 void Execute(ProcessMonitor *monitor);
710
711private:
712 bool &m_result;
713};
714
715void
716KillOperation::Execute(ProcessMonitor *monitor)
717{
718 lldb::pid_t pid = monitor->GetPID();
719
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000720 if (PTRACE(PTRACE_KILL, pid, NULL, NULL))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000721 m_result = false;
722 else
723 m_result = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000724}
725
Greg Clayton28041352011-11-29 20:50:10 +0000726//------------------------------------------------------------------------------
727/// @class KillOperation
728/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
729class DetachOperation : public Operation
730{
731public:
732 DetachOperation(Error &result) : m_error(result) { }
733
734 void Execute(ProcessMonitor *monitor);
735
736private:
737 Error &m_error;
738};
739
740void
741DetachOperation::Execute(ProcessMonitor *monitor)
742{
743 lldb::pid_t pid = monitor->GetPID();
744
745 if (ptrace(PT_DETACH, pid, NULL, 0) < 0)
746 m_error.SetErrorToErrno();
Greg Clayton542e4072012-09-07 17:49:29 +0000747
Greg Clayton28041352011-11-29 20:50:10 +0000748}
749
Johnny Chen25e68e32011-06-14 19:19:50 +0000750ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
751 : m_monitor(monitor)
752{
753 sem_init(&m_semaphore, 0, 0);
754}
755
756ProcessMonitor::OperationArgs::~OperationArgs()
757{
758 sem_destroy(&m_semaphore);
759}
760
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000761ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
762 lldb_private::Module *module,
763 char const **argv,
764 char const **envp,
765 const char *stdin_path,
766 const char *stdout_path,
767 const char *stderr_path)
Johnny Chen25e68e32011-06-14 19:19:50 +0000768 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000769 m_module(module),
770 m_argv(argv),
771 m_envp(envp),
772 m_stdin_path(stdin_path),
773 m_stdout_path(stdout_path),
Johnny Chen25e68e32011-06-14 19:19:50 +0000774 m_stderr_path(stderr_path) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000775
776ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +0000777{ }
778
779ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
780 lldb::pid_t pid)
781 : OperationArgs(monitor), m_pid(pid) { }
782
783ProcessMonitor::AttachArgs::~AttachArgs()
784{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000785
786//------------------------------------------------------------------------------
787/// The basic design of the ProcessMonitor is built around two threads.
788///
789/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
790/// for changes in the debugee state. When a change is detected a
791/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
792/// "drives" state changes in the debugger.
793///
794/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +0000795/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000796/// operations such as register reads/writes, stepping, etc. See the comments
797/// on the Operation class for more info as to why this is needed.
Johnny Chen30213ff2012-01-05 19:17:38 +0000798ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000799 Module *module,
800 const char *argv[],
801 const char *envp[],
802 const char *stdin_path,
803 const char *stdout_path,
804 const char *stderr_path,
805 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000806 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000807 m_operation_thread(LLDB_INVALID_HOST_THREAD),
808 m_pid(LLDB_INVALID_PROCESS_ID),
809 m_terminal_fd(-1),
Stephen Wilson9212d7f2011-01-04 21:40:25 +0000810 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000811 m_client_fd(-1),
812 m_server_fd(-1)
813{
Stephen Wilson57740ec2011-01-15 00:12:41 +0000814 std::auto_ptr<LaunchArgs> args;
815
816 args.reset(new LaunchArgs(this, module, argv, envp,
817 stdin_path, stdout_path, stderr_path));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000818
819 // Server/client descriptors.
820 if (!EnableIPC())
821 {
822 error.SetErrorToGenericError();
823 error.SetErrorString("Monitor failed to initialize.");
824 }
825
Johnny Chen25e68e32011-06-14 19:19:50 +0000826 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000827 if (!error.Success())
828 return;
829
830WAIT_AGAIN:
831 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000832 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000833 {
834 if (errno == EINTR)
835 goto WAIT_AGAIN;
836 else
837 {
838 error.SetErrorToErrno();
839 return;
840 }
841 }
842
843 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000844 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000845 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000846 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +0000847 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000848 return;
849 }
850
851 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +0000852 m_monitor_thread = Host::StartMonitoringChildProcess(
853 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Stephen Wilsond4182f42011-02-09 20:10:35 +0000854 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000855 {
856 error.SetErrorToGenericError();
857 error.SetErrorString("Process launch failed.");
858 return;
859 }
860}
861
Johnny Chen30213ff2012-01-05 19:17:38 +0000862ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +0000863 lldb::pid_t pid,
864 lldb_private::Error &error)
Johnny Chen30213ff2012-01-05 19:17:38 +0000865 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +0000866 m_operation_thread(LLDB_INVALID_HOST_THREAD),
867 m_pid(LLDB_INVALID_PROCESS_ID),
868 m_terminal_fd(-1),
869 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
870 m_client_fd(-1),
871 m_server_fd(-1)
872{
873 std::auto_ptr<AttachArgs> args;
874
875 args.reset(new AttachArgs(this, pid));
876
877 // Server/client descriptors.
878 if (!EnableIPC())
879 {
880 error.SetErrorToGenericError();
881 error.SetErrorString("Monitor failed to initialize.");
882 }
883
884 StartAttachOpThread(args.get(), error);
885 if (!error.Success())
886 return;
887
888WAIT_AGAIN:
889 // Wait for the operation thread to initialize.
890 if (sem_wait(&args->m_semaphore))
891 {
892 if (errno == EINTR)
893 goto WAIT_AGAIN;
894 else
895 {
896 error.SetErrorToErrno();
897 return;
898 }
899 }
900
Greg Clayton743ecf42012-10-16 20:20:18 +0000901 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +0000902 if (!args->m_error.Success())
903 {
Greg Clayton743ecf42012-10-16 20:20:18 +0000904 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +0000905 error = args->m_error;
906 return;
907 }
908
909 // Finally, start monitoring the child process for change in state.
910 m_monitor_thread = Host::StartMonitoringChildProcess(
911 ProcessMonitor::MonitorCallback, this, GetPID(), true);
912 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
913 {
914 error.SetErrorToGenericError();
915 error.SetErrorString("Process attach failed.");
916 return;
917 }
918}
919
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000920ProcessMonitor::~ProcessMonitor()
921{
Stephen Wilson84ffe702011-03-30 15:55:52 +0000922 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000923}
924
925//------------------------------------------------------------------------------
926// Thread setup and tear down.
927void
Johnny Chen25e68e32011-06-14 19:19:50 +0000928ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000929{
930 static const char *g_thread_name = "lldb.process.linux.operation";
931
Stephen Wilsond4182f42011-02-09 20:10:35 +0000932 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000933 return;
934
935 m_operation_thread =
Johnny Chen25e68e32011-06-14 19:19:50 +0000936 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000937}
938
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000939void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000940ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000941{
942 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
943
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000944 if (!Launch(args)) {
945 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000946 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000947 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000948
Stephen Wilson570243b2011-01-19 01:37:06 +0000949 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000950 return NULL;
951}
952
953bool
954ProcessMonitor::Launch(LaunchArgs *args)
955{
956 ProcessMonitor *monitor = args->m_monitor;
957 ProcessLinux &process = monitor->GetProcess();
958 const char **argv = args->m_argv;
959 const char **envp = args->m_envp;
960 const char *stdin_path = args->m_stdin_path;
961 const char *stdout_path = args->m_stdout_path;
962 const char *stderr_path = args->m_stderr_path;
963
964 lldb_utility::PseudoTerminal terminal;
965 const size_t err_len = 1024;
966 char err_str[err_len];
967 lldb::pid_t pid;
968
969 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +0000970 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000971
Stephen Wilson57740ec2011-01-15 00:12:41 +0000972 // Propagate the environment if one is not supplied.
973 if (envp == NULL || envp[0] == NULL)
974 envp = const_cast<const char **>(environ);
975
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000976 // Pseudo terminal setup.
977 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
978 {
979 args->m_error.SetErrorToGenericError();
980 args->m_error.SetErrorString("Could not open controlling TTY.");
981 goto FINISH;
982 }
983
984 if ((pid = terminal.Fork(err_str, err_len)) < 0)
985 {
986 args->m_error.SetErrorToGenericError();
987 args->m_error.SetErrorString("Process fork failed.");
988 goto FINISH;
989 }
990
Peter Collingbourne6a520222011-06-14 03:55:58 +0000991 // Recognized child exit status codes.
992 enum {
993 ePtraceFailed = 1,
994 eDupStdinFailed,
995 eDupStdoutFailed,
996 eDupStderrFailed,
997 eExecFailed
998 };
999
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001000 // Child process.
1001 if (pid == 0)
1002 {
1003 // Trace this process.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001004 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001005 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001006
1007 // Do not inherit setgid powers.
1008 setgid(getgid());
1009
1010 // Let us have our own process group.
1011 setpgid(0, 0);
1012
Greg Clayton710dd5a2011-01-08 20:28:42 +00001013 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001014 //
1015 // FIXME: If two or more of the paths are the same we needlessly open
1016 // the same file multiple times.
1017 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001018 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001019 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001020
1021 if (stdout_path != NULL && stdout_path[0])
1022 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001023 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001024
1025 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001026 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001027 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001028
1029 // Execute. We should never return.
1030 execve(argv[0],
1031 const_cast<char *const *>(argv),
1032 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001033 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001034 }
1035
1036 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001037 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001038 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001039 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001040 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001041 args->m_error.SetErrorToErrno();
1042 goto FINISH;
1043 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001044 else if (WIFEXITED(status))
1045 {
1046 // open, dup or execve likely failed for some reason.
1047 args->m_error.SetErrorToGenericError();
1048 switch (WEXITSTATUS(status))
1049 {
Greg Clayton542e4072012-09-07 17:49:29 +00001050 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001051 args->m_error.SetErrorString("Child ptrace failed.");
1052 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001053 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001054 args->m_error.SetErrorString("Child open stdin failed.");
1055 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001056 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001057 args->m_error.SetErrorString("Child open stdout failed.");
1058 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001059 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001060 args->m_error.SetErrorString("Child open stderr failed.");
1061 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001062 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001063 args->m_error.SetErrorString("Child exec failed.");
1064 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001065 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001066 args->m_error.SetErrorString("Child returned unknown exit status.");
1067 break;
1068 }
1069 goto FINISH;
1070 }
1071 assert(WIFSTOPPED(status) && wpid == pid &&
1072 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001073
1074 // Have the child raise an event on exit. This is used to keep the child in
1075 // limbo until it is destroyed.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001076 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)PTRACE_O_TRACEEXIT) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001077 {
1078 args->m_error.SetErrorToErrno();
1079 goto FINISH;
1080 }
1081
1082 // Release the master terminal descriptor and pass it off to the
1083 // ProcessMonitor instance. Similarly stash the inferior pid.
1084 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1085 monitor->m_pid = pid;
1086
Stephen Wilson26977162011-03-23 02:14:42 +00001087 // Set the terminal fd to be in non blocking mode (it simplifies the
1088 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1089 // descriptor to read from).
1090 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1091 goto FINISH;
1092
Johnny Chen30213ff2012-01-05 19:17:38 +00001093 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001094 // FIXME: should we be letting UpdateThreadList handle this?
1095 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001096 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001097 if (log)
1098 log->Printf ("ProcessMonitor::%s() adding pid = %i", __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001099 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001100
1101 // Let our process instance know the thread has stopped.
1102 process.SendMessage(ProcessMessage::Trace(pid));
1103
1104FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001105 return args->m_error.Success();
1106}
1107
1108bool
1109ProcessMonitor::EnableIPC()
1110{
1111 int fd[2];
1112
1113 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1114 return false;
1115
1116 m_client_fd = fd[0];
1117 m_server_fd = fd[1];
1118 return true;
1119}
1120
Johnny Chen25e68e32011-06-14 19:19:50 +00001121void
1122ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1123{
1124 static const char *g_thread_name = "lldb.process.linux.operation";
1125
1126 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1127 return;
1128
1129 m_operation_thread =
1130 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1131}
1132
Johnny Chen25e68e32011-06-14 19:19:50 +00001133void *
1134ProcessMonitor::AttachOpThread(void *arg)
1135{
1136 AttachArgs *args = static_cast<AttachArgs*>(arg);
1137
Greg Clayton743ecf42012-10-16 20:20:18 +00001138 if (!Attach(args)) {
1139 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001140 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001141 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001142
1143 ServeOperation(args);
1144 return NULL;
1145}
1146
1147bool
1148ProcessMonitor::Attach(AttachArgs *args)
1149{
1150 lldb::pid_t pid = args->m_pid;
1151
1152 ProcessMonitor *monitor = args->m_monitor;
1153 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001154 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001155 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001156
1157 if (pid <= 1)
1158 {
1159 args->m_error.SetErrorToGenericError();
1160 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1161 goto FINISH;
1162 }
1163
1164 // Attach to the requested process.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001165 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001166 {
1167 args->m_error.SetErrorToErrno();
1168 goto FINISH;
1169 }
1170
1171 int status;
1172 if ((status = waitpid(pid, NULL, 0)) < 0)
1173 {
1174 args->m_error.SetErrorToErrno();
1175 goto FINISH;
1176 }
1177
Greg Clayton926cce72012-10-12 16:10:12 +00001178 monitor->m_pid = pid;
1179
Johnny Chen30213ff2012-01-05 19:17:38 +00001180 // Update the process thread list with the attached thread.
Greg Claytondf3df252012-10-12 16:23:23 +00001181 inferior.reset(new POSIXThread(process, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001182 if (log)
1183 log->Printf ("ProcessMonitor::%s() adding tid = %i", __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001184 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001185
1186 // Let our process instance know the thread has stopped.
1187 process.SendMessage(ProcessMessage::Trace(pid));
1188
1189 FINISH:
1190 return args->m_error.Success();
1191}
1192
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001193bool
1194ProcessMonitor::MonitorCallback(void *callback_baton,
1195 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001196 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001197 int signal,
1198 int status)
1199{
1200 ProcessMessage message;
1201 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1202 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001203 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001204 bool stop_monitoring;
1205 siginfo_t info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001206
Stephen Wilson84ffe702011-03-30 15:55:52 +00001207 if (!monitor->GetSignalInfo(pid, &info))
1208 stop_monitoring = true; // pid is gone. Bail.
1209 else {
1210 switch (info.si_signo)
1211 {
1212 case SIGTRAP:
1213 message = MonitorSIGTRAP(monitor, &info, pid);
1214 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001215
Stephen Wilson84ffe702011-03-30 15:55:52 +00001216 default:
1217 message = MonitorSignal(monitor, &info, pid);
1218 break;
1219 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001220
Stephen Wilson84ffe702011-03-30 15:55:52 +00001221 process->SendMessage(message);
1222 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001223 }
1224
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001225 return stop_monitoring;
1226}
1227
1228ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001229ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001230 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001231{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001232 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001233
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001234 assert(monitor);
1235 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001236
Stephen Wilson84ffe702011-03-30 15:55:52 +00001237 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001238 {
1239 default:
1240 assert(false && "Unexpected SIGTRAP code!");
1241 break;
1242
1243 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1244 {
1245 // The inferior process is about to exit. Maintain the process in a
1246 // state of "limbo" until we are explicitly commanded to detach,
1247 // destroy, resume, etc.
1248 unsigned long data = 0;
1249 if (!monitor->GetEventMessage(pid, &data))
1250 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001251 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001252 break;
1253 }
1254
1255 case 0:
1256 case TRAP_TRACE:
1257 message = ProcessMessage::Trace(pid);
1258 break;
1259
1260 case SI_KERNEL:
1261 case TRAP_BRKPT:
1262 message = ProcessMessage::Break(pid);
1263 break;
1264 }
1265
1266 return message;
1267}
1268
Stephen Wilson84ffe702011-03-30 15:55:52 +00001269ProcessMessage
1270ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001271 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001272{
1273 ProcessMessage message;
1274 int signo = info->si_signo;
1275
1276 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1277 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1278 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1279 //
1280 // IOW, user generated signals never generate what we consider to be a
1281 // "crash".
1282 //
1283 // Similarly, ACK signals generated by this monitor.
1284 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1285 {
1286 if (info->si_pid == getpid())
1287 return ProcessMessage::SignalDelivered(pid, signo);
1288 else
1289 return ProcessMessage::Signal(pid, signo);
1290 }
1291
1292 if (signo == SIGSEGV) {
1293 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1294 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1295 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1296 }
1297
1298 if (signo == SIGILL) {
1299 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1300 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1301 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1302 }
1303
1304 if (signo == SIGFPE) {
1305 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1306 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1307 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1308 }
1309
1310 if (signo == SIGBUS) {
1311 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1312 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1313 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1314 }
1315
1316 // Everything else is "normal" and does not require any special action on
1317 // our part.
1318 return ProcessMessage::Signal(pid, signo);
1319}
1320
1321ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001322ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001323{
1324 ProcessMessage::CrashReason reason;
1325 assert(info->si_signo == SIGSEGV);
1326
1327 reason = ProcessMessage::eInvalidCrashReason;
1328
Greg Clayton542e4072012-09-07 17:49:29 +00001329 switch (info->si_code)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001330 {
1331 default:
1332 assert(false && "unexpected si_code for SIGSEGV");
1333 break;
1334 case SEGV_MAPERR:
1335 reason = ProcessMessage::eInvalidAddress;
1336 break;
1337 case SEGV_ACCERR:
1338 reason = ProcessMessage::ePrivilegedAddress;
1339 break;
1340 }
Greg Clayton542e4072012-09-07 17:49:29 +00001341
Stephen Wilson84ffe702011-03-30 15:55:52 +00001342 return reason;
1343}
1344
1345ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001346ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001347{
1348 ProcessMessage::CrashReason reason;
1349 assert(info->si_signo == SIGILL);
1350
1351 reason = ProcessMessage::eInvalidCrashReason;
1352
1353 switch (info->si_code)
1354 {
1355 default:
1356 assert(false && "unexpected si_code for SIGILL");
1357 break;
1358 case ILL_ILLOPC:
1359 reason = ProcessMessage::eIllegalOpcode;
1360 break;
1361 case ILL_ILLOPN:
1362 reason = ProcessMessage::eIllegalOperand;
1363 break;
1364 case ILL_ILLADR:
1365 reason = ProcessMessage::eIllegalAddressingMode;
1366 break;
1367 case ILL_ILLTRP:
1368 reason = ProcessMessage::eIllegalTrap;
1369 break;
1370 case ILL_PRVOPC:
1371 reason = ProcessMessage::ePrivilegedOpcode;
1372 break;
1373 case ILL_PRVREG:
1374 reason = ProcessMessage::ePrivilegedRegister;
1375 break;
1376 case ILL_COPROC:
1377 reason = ProcessMessage::eCoprocessorError;
1378 break;
1379 case ILL_BADSTK:
1380 reason = ProcessMessage::eInternalStackError;
1381 break;
1382 }
1383
1384 return reason;
1385}
1386
1387ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001388ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001389{
1390 ProcessMessage::CrashReason reason;
1391 assert(info->si_signo == SIGFPE);
1392
1393 reason = ProcessMessage::eInvalidCrashReason;
1394
1395 switch (info->si_code)
1396 {
1397 default:
1398 assert(false && "unexpected si_code for SIGFPE");
1399 break;
1400 case FPE_INTDIV:
1401 reason = ProcessMessage::eIntegerDivideByZero;
1402 break;
1403 case FPE_INTOVF:
1404 reason = ProcessMessage::eIntegerOverflow;
1405 break;
1406 case FPE_FLTDIV:
1407 reason = ProcessMessage::eFloatDivideByZero;
1408 break;
1409 case FPE_FLTOVF:
1410 reason = ProcessMessage::eFloatOverflow;
1411 break;
1412 case FPE_FLTUND:
1413 reason = ProcessMessage::eFloatUnderflow;
1414 break;
1415 case FPE_FLTRES:
1416 reason = ProcessMessage::eFloatInexactResult;
1417 break;
1418 case FPE_FLTINV:
1419 reason = ProcessMessage::eFloatInvalidOperation;
1420 break;
1421 case FPE_FLTSUB:
1422 reason = ProcessMessage::eFloatSubscriptRange;
1423 break;
1424 }
1425
1426 return reason;
1427}
1428
1429ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001430ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001431{
1432 ProcessMessage::CrashReason reason;
1433 assert(info->si_signo == SIGBUS);
1434
1435 reason = ProcessMessage::eInvalidCrashReason;
1436
1437 switch (info->si_code)
1438 {
1439 default:
1440 assert(false && "unexpected si_code for SIGBUS");
1441 break;
1442 case BUS_ADRALN:
1443 reason = ProcessMessage::eIllegalAlignment;
1444 break;
1445 case BUS_ADRERR:
1446 reason = ProcessMessage::eIllegalAddress;
1447 break;
1448 case BUS_OBJERR:
1449 reason = ProcessMessage::eHardwareError;
1450 break;
1451 }
1452
1453 return reason;
1454}
1455
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001456void
Johnny Chen25e68e32011-06-14 19:19:50 +00001457ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001458{
1459 int status;
1460 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001461
Stephen Wilson570243b2011-01-19 01:37:06 +00001462 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001463
1464 fdset.fd = monitor->m_server_fd;
1465 fdset.events = POLLIN | POLLPRI;
1466 fdset.revents = 0;
1467
Stephen Wilson570243b2011-01-19 01:37:06 +00001468 // We are finised with the arguments and are ready to go. Sync with the
1469 // parent thread and start serving operations on the inferior.
1470 sem_post(&args->m_semaphore);
1471
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001472 for (;;)
1473 {
1474 if ((status = poll(&fdset, 1, -1)) < 0)
1475 {
1476 switch (errno)
1477 {
1478 default:
1479 assert(false && "Unexpected poll() failure!");
1480 continue;
1481
1482 case EINTR: continue; // Just poll again.
1483 case EBADF: return; // Connection terminated.
1484 }
1485 }
1486
1487 assert(status == 1 && "Too many descriptors!");
1488
1489 if (fdset.revents & POLLIN)
1490 {
1491 Operation *op = NULL;
1492
1493 READ_AGAIN:
1494 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1495 {
1496 // There is only one acceptable failure.
1497 assert(errno == EINTR);
1498 goto READ_AGAIN;
1499 }
1500
1501 assert(status == sizeof(op));
1502 op->Execute(monitor);
1503 write(fdset.fd, &op, sizeof(op));
1504 }
1505 }
1506}
1507
1508void
1509ProcessMonitor::DoOperation(Operation *op)
1510{
1511 int status;
1512 Operation *ack = NULL;
1513 Mutex::Locker lock(m_server_mutex);
1514
1515 // FIXME: Do proper error checking here.
1516 write(m_client_fd, &op, sizeof(op));
1517
1518READ_AGAIN:
1519 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1520 {
1521 // If interrupted by a signal handler try again. Otherwise the monitor
1522 // thread probably died and we have a stale file descriptor -- abort the
1523 // operation.
1524 if (errno == EINTR)
1525 goto READ_AGAIN;
1526 return;
1527 }
1528
1529 assert(status == sizeof(ack));
1530 assert(ack == op && "Invalid monitor thread response!");
1531}
1532
1533size_t
1534ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1535 Error &error)
1536{
1537 size_t result;
1538 ReadOperation op(vm_addr, buf, size, error, result);
1539 DoOperation(&op);
1540 return result;
1541}
1542
1543size_t
1544ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1545 lldb_private::Error &error)
1546{
1547 size_t result;
1548 WriteOperation op(vm_addr, buf, size, error, result);
1549 DoOperation(&op);
1550 return result;
1551}
1552
1553bool
Johnny Chen30213ff2012-01-05 19:17:38 +00001554ProcessMonitor::ReadRegisterValue(unsigned offset, unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001555{
1556 bool result;
1557 ReadRegOperation op(offset, value, result);
1558 DoOperation(&op);
1559 return result;
1560}
1561
1562bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +00001563ProcessMonitor::WriteRegisterValue(unsigned offset, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001564{
1565 bool result;
1566 WriteRegOperation op(offset, value, result);
1567 DoOperation(&op);
1568 return result;
1569}
1570
1571bool
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001572ProcessMonitor::ReadGPR(void *buf)
1573{
1574 bool result;
1575 ReadGPROperation op(buf, result);
1576 DoOperation(&op);
1577 return result;
1578}
1579
1580bool
1581ProcessMonitor::ReadFPR(void *buf)
1582{
1583 bool result;
1584 ReadFPROperation op(buf, result);
1585 DoOperation(&op);
1586 return result;
1587}
1588
1589bool
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001590ProcessMonitor::WriteGPR(void *buf)
1591{
1592 bool result;
1593 WriteGPROperation op(buf, result);
1594 DoOperation(&op);
1595 return result;
1596}
1597
1598bool
1599ProcessMonitor::WriteFPR(void *buf)
1600{
1601 bool result;
1602 WriteFPROperation op(buf, result);
1603 DoOperation(&op);
1604 return result;
1605}
1606
1607bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001608ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001609{
1610 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001611 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001612 DoOperation(&op);
1613 return result;
1614}
1615
1616bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001617ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001618{
1619 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001620 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001621 DoOperation(&op);
1622 return result;
1623}
1624
1625bool
1626ProcessMonitor::BringProcessIntoLimbo()
1627{
1628 bool result;
1629 KillOperation op(result);
1630 DoOperation(&op);
1631 return result;
1632}
1633
1634bool
1635ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo)
1636{
1637 bool result;
1638 SiginfoOperation op(tid, siginfo, result);
1639 DoOperation(&op);
1640 return result;
1641}
1642
1643bool
1644ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1645{
1646 bool result;
1647 EventMessageOperation op(tid, message, result);
1648 DoOperation(&op);
1649 return result;
1650}
1651
Greg Clayton743ecf42012-10-16 20:20:18 +00001652lldb_private::Error
Stephen Wilson84ffe702011-03-30 15:55:52 +00001653ProcessMonitor::Detach()
1654{
Greg Clayton28041352011-11-29 20:50:10 +00001655 lldb_private::Error error;
Greg Clayton743ecf42012-10-16 20:20:18 +00001656 if (m_pid != LLDB_INVALID_PROCESS_ID) {
1657 DetachOperation op(error);
1658 DoOperation(&op);
1659 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001660 StopMonitor();
Greg Clayton743ecf42012-10-16 20:20:18 +00001661 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00001662}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001663
1664bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001665ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1666{
Peter Collingbourne62343202011-06-14 03:55:54 +00001667 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001668
1669 if (target_fd == -1)
1670 return false;
1671
Peter Collingbourne62343202011-06-14 03:55:54 +00001672 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001673}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001674
1675void
1676ProcessMonitor::StopMonitoringChildProcess()
1677{
1678 lldb::thread_result_t thread_result;
1679
Stephen Wilsond4182f42011-02-09 20:10:35 +00001680 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001681 {
1682 Host::ThreadCancel(m_monitor_thread, NULL);
1683 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1684 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1685 }
1686}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001687
1688void
1689ProcessMonitor::StopMonitor()
1690{
1691 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00001692 StopOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001693 CloseFD(m_terminal_fd);
1694 CloseFD(m_client_fd);
1695 CloseFD(m_server_fd);
1696}
1697
1698void
Greg Clayton743ecf42012-10-16 20:20:18 +00001699ProcessMonitor::StopOpThread()
1700{
1701 lldb::thread_result_t result;
1702
1703 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1704 return;
1705
1706 Host::ThreadCancel(m_operation_thread, NULL);
1707 Host::ThreadJoin(m_operation_thread, &result, NULL);
1708}
1709
1710void
Stephen Wilson84ffe702011-03-30 15:55:52 +00001711ProcessMonitor::CloseFD(int &fd)
1712{
1713 if (fd != -1)
1714 {
1715 close(fd);
1716 fd = -1;
1717 }
1718}