blob: 2f060df0bfe905cd292930ac4e5a761adf102f36 [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 }
80 case PTRACE_POKEDATA:
81 {
82 DisplayBytes(buf, &data, 8);
83 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
84 break;
85 }
86 case PTRACE_POKEUSER:
87 {
88 DisplayBytes(buf, &data, 8);
89 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
90 break;
91 }
92 case PTRACE_SETREGS:
93 {
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 }
104 case PTRACE_SETSIGINFO:
105 {
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 Clayton386ff182011-11-05 01:09:16 +0000129
130 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
166DoReadMemory(lldb::pid_t pid, unsigned word_size,
167 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
168{
169 unsigned char *dst = static_cast<unsigned char*>(buf);
170 size_t bytes_read;
171 size_t remainder;
172 long data;
173
Johnny Chen30213ff2012-01-05 19:17:38 +0000174 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000175 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000176 ProcessPOSIXLog::IncNestLevel();
177 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000178 log->Printf ("ProcessMonitor::%s(%d, %d, %p, %p, %d, _)", __FUNCTION__,
179 pid, word_size, (void*)vm_addr, buf, size);
180
181 assert(sizeof(data) >= word_size);
182 assert(sizeof(void*) == word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000183 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
184 {
185 errno = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000186 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000187 if (data == -1L && errno)
188 {
189 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000190 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000191 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000192 return bytes_read;
193 }
194
195 remainder = size - bytes_read;
196 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000197
198 // Copy the data into our buffer
199 if (log)
200 memset(dst, 0, sizeof(dst));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000201 for (unsigned i = 0; i < remainder; ++i)
202 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000203
Johnny Chen30213ff2012-01-05 19:17:38 +0000204 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
205 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
206 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
207 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000208 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
209 (void*)vm_addr, *(unsigned long*)dst, (unsigned long)data);
210
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000211 vm_addr += word_size;
212 dst += word_size;
213 }
214
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000215 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000216 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000217 return bytes_read;
218}
219
220static size_t
221DoWriteMemory(lldb::pid_t pid, unsigned word_size,
222 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
223{
224 const unsigned char *src = static_cast<const unsigned char*>(buf);
225 size_t bytes_written = 0;
226 size_t remainder;
227
Johnny Chen30213ff2012-01-05 19:17:38 +0000228 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000229 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000230 ProcessPOSIXLog::IncNestLevel();
231 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000232 log->Printf ("ProcessMonitor::%s(%d, %d, %p, %p, %d, _)", __FUNCTION__,
233 pid, word_size, (void*)vm_addr, buf, size);
234
235 assert(sizeof(void*) == word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000236 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
237 {
238 remainder = size - bytes_written;
239 remainder = remainder > word_size ? word_size : remainder;
240
241 if (remainder == word_size)
242 {
243 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000244 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000245 for (unsigned i = 0; i < word_size; ++i)
246 data |= (unsigned long)src[i] << i*8;
247
Johnny Chen30213ff2012-01-05 19:17:38 +0000248 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
249 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
250 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
251 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000252 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
253 (void*)vm_addr, *(unsigned long*)src, data);
254
255 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256 {
257 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000258 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000259 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000260 return bytes_written;
261 }
262 }
263 else
264 {
265 unsigned char buff[8];
266 if (DoReadMemory(pid, word_size, vm_addr,
267 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000268 {
269 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000270 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000271 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000272 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000273
274 memcpy(buff, src, remainder);
275
276 if (DoWriteMemory(pid, word_size, vm_addr,
277 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000278 {
279 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000280 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000281 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000282 }
283
Johnny Chen30213ff2012-01-05 19:17:38 +0000284 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
285 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
286 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
287 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000288 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
289 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290 }
291
292 vm_addr += word_size;
293 src += word_size;
294 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000295 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000296 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000297 return bytes_written;
298}
299
Stephen Wilson26977162011-03-23 02:14:42 +0000300// Simple helper function to ensure flags are enabled on the given file
301// descriptor.
302static bool
303EnsureFDFlags(int fd, int flags, Error &error)
304{
305 int status;
306
307 if ((status = fcntl(fd, F_GETFL)) == -1)
308 {
309 error.SetErrorToErrno();
310 return false;
311 }
312
313 if (fcntl(fd, F_SETFL, status | flags) == -1)
314 {
315 error.SetErrorToErrno();
316 return false;
317 }
318
319 return true;
320}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000321
322//------------------------------------------------------------------------------
323/// @class Operation
324/// @brief Represents a ProcessMonitor operation.
325///
326/// Under Linux, it is not possible to ptrace() from any other thread but the
327/// one that spawned or attached to the process from the start. Therefore, when
328/// a ProcessMonitor is asked to deliver or change the state of an inferior
329/// process the operation must be "funneled" to a specific thread to perform the
330/// task. The Operation class provides an abstract base for all services the
331/// ProcessMonitor must perform via the single virtual function Execute, thus
332/// encapsulating the code that needs to run in the privileged context.
333class Operation
334{
335public:
336 virtual void Execute(ProcessMonitor *monitor) = 0;
337};
338
339//------------------------------------------------------------------------------
340/// @class ReadOperation
341/// @brief Implements ProcessMonitor::ReadMemory.
342class ReadOperation : public Operation
343{
344public:
345 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
346 Error &error, size_t &result)
347 : m_addr(addr), m_buff(buff), m_size(size),
348 m_error(error), m_result(result)
349 { }
350
351 void Execute(ProcessMonitor *monitor);
352
353private:
354 lldb::addr_t m_addr;
355 void *m_buff;
356 size_t m_size;
357 Error &m_error;
358 size_t &m_result;
359};
360
361void
362ReadOperation::Execute(ProcessMonitor *monitor)
363{
364 const unsigned word_size = monitor->GetProcess().GetAddressByteSize();
365 lldb::pid_t pid = monitor->GetPID();
366
367 m_result = DoReadMemory(pid, word_size, m_addr, m_buff, m_size, m_error);
368}
369
370//------------------------------------------------------------------------------
371/// @class ReadOperation
372/// @brief Implements ProcessMonitor::WriteMemory.
373class WriteOperation : public Operation
374{
375public:
376 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
377 Error &error, size_t &result)
378 : m_addr(addr), m_buff(buff), m_size(size),
379 m_error(error), m_result(result)
380 { }
381
382 void Execute(ProcessMonitor *monitor);
383
384private:
385 lldb::addr_t m_addr;
386 const void *m_buff;
387 size_t m_size;
388 Error &m_error;
389 size_t &m_result;
390};
391
392void
393WriteOperation::Execute(ProcessMonitor *monitor)
394{
395 const unsigned word_size = monitor->GetProcess().GetAddressByteSize();
396 lldb::pid_t pid = monitor->GetPID();
397
398 m_result = DoWriteMemory(pid, word_size, m_addr, m_buff, m_size, m_error);
399}
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();
747
748}
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 {
Johnny Chen25e68e32011-06-14 19:19:50 +0000846 StopLaunchOpThread();
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
901 // Check that the launch was a success.
902 if (!args->m_error.Success())
903 {
904 StopAttachOpThread();
905 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
939void
Johnny Chen25e68e32011-06-14 19:19:50 +0000940ProcessMonitor::StopLaunchOpThread()
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000941{
942 lldb::thread_result_t result;
943
Stephen Wilsond4182f42011-02-09 20:10:35 +0000944 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000945 return;
946
947 Host::ThreadCancel(m_operation_thread, NULL);
948 Host::ThreadJoin(m_operation_thread, &result, NULL);
949}
950
951void *
Johnny Chen25e68e32011-06-14 19:19:50 +0000952ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000953{
954 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
955
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000956 if (!Launch(args)) {
957 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000958 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +0000959 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000960
Stephen Wilson570243b2011-01-19 01:37:06 +0000961 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000962 return NULL;
963}
964
965bool
966ProcessMonitor::Launch(LaunchArgs *args)
967{
968 ProcessMonitor *monitor = args->m_monitor;
969 ProcessLinux &process = monitor->GetProcess();
Greg Clayton0c90ef42012-02-21 18:40:07 +0000970 lldb::ProcessSP processSP = process.shared_from_this();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000971 const char **argv = args->m_argv;
972 const char **envp = args->m_envp;
973 const char *stdin_path = args->m_stdin_path;
974 const char *stdout_path = args->m_stdout_path;
975 const char *stderr_path = args->m_stderr_path;
976
977 lldb_utility::PseudoTerminal terminal;
978 const size_t err_len = 1024;
979 char err_str[err_len];
980 lldb::pid_t pid;
981
982 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +0000983 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000984
Stephen Wilson57740ec2011-01-15 00:12:41 +0000985 // Propagate the environment if one is not supplied.
986 if (envp == NULL || envp[0] == NULL)
987 envp = const_cast<const char **>(environ);
988
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000989 // Pseudo terminal setup.
990 if (!terminal.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY, err_str, err_len))
991 {
992 args->m_error.SetErrorToGenericError();
993 args->m_error.SetErrorString("Could not open controlling TTY.");
994 goto FINISH;
995 }
996
997 if ((pid = terminal.Fork(err_str, err_len)) < 0)
998 {
999 args->m_error.SetErrorToGenericError();
1000 args->m_error.SetErrorString("Process fork failed.");
1001 goto FINISH;
1002 }
1003
Peter Collingbourne6a520222011-06-14 03:55:58 +00001004 // Recognized child exit status codes.
1005 enum {
1006 ePtraceFailed = 1,
1007 eDupStdinFailed,
1008 eDupStdoutFailed,
1009 eDupStderrFailed,
1010 eExecFailed
1011 };
1012
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001013 // Child process.
1014 if (pid == 0)
1015 {
1016 // Trace this process.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001017 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001018 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001019
1020 // Do not inherit setgid powers.
1021 setgid(getgid());
1022
1023 // Let us have our own process group.
1024 setpgid(0, 0);
1025
Greg Clayton710dd5a2011-01-08 20:28:42 +00001026 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001027 //
1028 // FIXME: If two or more of the paths are the same we needlessly open
1029 // the same file multiple times.
1030 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001031 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001032 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001033
1034 if (stdout_path != NULL && stdout_path[0])
1035 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001036 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001037
1038 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001039 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001040 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001041
1042 // Execute. We should never return.
1043 execve(argv[0],
1044 const_cast<char *const *>(argv),
1045 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001046 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001047 }
1048
1049 // Wait for the child process to to trap on its call to execve.
Peter Collingbourne6a520222011-06-14 03:55:58 +00001050 pid_t wpid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051 int status;
Peter Collingbourne6a520222011-06-14 03:55:58 +00001052 if ((wpid = waitpid(pid, &status, 0)) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001053 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 args->m_error.SetErrorToErrno();
1055 goto FINISH;
1056 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001057 else if (WIFEXITED(status))
1058 {
1059 // open, dup or execve likely failed for some reason.
1060 args->m_error.SetErrorToGenericError();
1061 switch (WEXITSTATUS(status))
1062 {
1063 case ePtraceFailed:
1064 args->m_error.SetErrorString("Child ptrace failed.");
1065 break;
1066 case eDupStdinFailed:
1067 args->m_error.SetErrorString("Child open stdin failed.");
1068 break;
1069 case eDupStdoutFailed:
1070 args->m_error.SetErrorString("Child open stdout failed.");
1071 break;
1072 case eDupStderrFailed:
1073 args->m_error.SetErrorString("Child open stderr failed.");
1074 break;
1075 case eExecFailed:
1076 args->m_error.SetErrorString("Child exec failed.");
1077 break;
1078 default:
1079 args->m_error.SetErrorString("Child returned unknown exit status.");
1080 break;
1081 }
1082 goto FINISH;
1083 }
1084 assert(WIFSTOPPED(status) && wpid == pid &&
1085 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001086
1087 // Have the child raise an event on exit. This is used to keep the child in
1088 // limbo until it is destroyed.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001089 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)PTRACE_O_TRACEEXIT) < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001090 {
1091 args->m_error.SetErrorToErrno();
1092 goto FINISH;
1093 }
1094
1095 // Release the master terminal descriptor and pass it off to the
1096 // ProcessMonitor instance. Similarly stash the inferior pid.
1097 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1098 monitor->m_pid = pid;
1099
Stephen Wilson26977162011-03-23 02:14:42 +00001100 // Set the terminal fd to be in non blocking mode (it simplifies the
1101 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1102 // descriptor to read from).
1103 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1104 goto FINISH;
1105
Johnny Chen30213ff2012-01-05 19:17:38 +00001106 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001107 // FIXME: should we be letting UpdateThreadList handle this?
1108 // FIXME: by using pids instead of tids, we can only support one thread.
Greg Clayton0c90ef42012-02-21 18:40:07 +00001109 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001110 if (log)
1111 log->Printf ("ProcessMonitor::%s() adding pid = %i", __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001112 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001113
1114 // Let our process instance know the thread has stopped.
1115 process.SendMessage(ProcessMessage::Trace(pid));
1116
1117FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001118 return args->m_error.Success();
1119}
1120
1121bool
1122ProcessMonitor::EnableIPC()
1123{
1124 int fd[2];
1125
1126 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
1127 return false;
1128
1129 m_client_fd = fd[0];
1130 m_server_fd = fd[1];
1131 return true;
1132}
1133
Johnny Chen25e68e32011-06-14 19:19:50 +00001134void
1135ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1136{
1137 static const char *g_thread_name = "lldb.process.linux.operation";
1138
1139 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1140 return;
1141
1142 m_operation_thread =
1143 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1144}
1145
1146void
1147ProcessMonitor::StopAttachOpThread()
1148{
1149 assert(!"Not implemented yet!!!");
1150}
1151
1152void *
1153ProcessMonitor::AttachOpThread(void *arg)
1154{
1155 AttachArgs *args = static_cast<AttachArgs*>(arg);
1156
1157 if (!Attach(args))
1158 return NULL;
1159
1160 ServeOperation(args);
1161 return NULL;
1162}
1163
1164bool
1165ProcessMonitor::Attach(AttachArgs *args)
1166{
1167 lldb::pid_t pid = args->m_pid;
1168
1169 ProcessMonitor *monitor = args->m_monitor;
1170 ProcessLinux &process = monitor->GetProcess();
Greg Clayton0c90ef42012-02-21 18:40:07 +00001171 lldb::ProcessSP processSP = process.shared_from_this();
Johnny Chen25e68e32011-06-14 19:19:50 +00001172 lldb::ThreadSP inferior;
Johnny Chen30213ff2012-01-05 19:17:38 +00001173 LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001174
1175 if (pid <= 1)
1176 {
1177 args->m_error.SetErrorToGenericError();
1178 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1179 goto FINISH;
1180 }
1181
1182 // Attach to the requested process.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001183 if (PTRACE(PTRACE_ATTACH, pid, NULL, NULL) < 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001184 {
1185 args->m_error.SetErrorToErrno();
1186 goto FINISH;
1187 }
1188
1189 int status;
1190 if ((status = waitpid(pid, NULL, 0)) < 0)
1191 {
1192 args->m_error.SetErrorToErrno();
1193 goto FINISH;
1194 }
1195
Johnny Chen30213ff2012-01-05 19:17:38 +00001196 // Update the process thread list with the attached thread.
Greg Clayton0c90ef42012-02-21 18:40:07 +00001197 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001198 if (log)
1199 log->Printf ("ProcessMonitor::%s() adding tid = %i", __FUNCTION__, pid);
Johnny Chen25e68e32011-06-14 19:19:50 +00001200 process.GetThreadList().AddThread(inferior);
Johnny Chen25e68e32011-06-14 19:19:50 +00001201
1202 // Let our process instance know the thread has stopped.
1203 process.SendMessage(ProcessMessage::Trace(pid));
1204
1205 FINISH:
1206 return args->m_error.Success();
1207}
1208
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001209bool
1210ProcessMonitor::MonitorCallback(void *callback_baton,
1211 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001212 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001213 int signal,
1214 int status)
1215{
1216 ProcessMessage message;
1217 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1218 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001219 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001220 bool stop_monitoring;
1221 siginfo_t info;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001222
Stephen Wilson84ffe702011-03-30 15:55:52 +00001223 if (!monitor->GetSignalInfo(pid, &info))
1224 stop_monitoring = true; // pid is gone. Bail.
1225 else {
1226 switch (info.si_signo)
1227 {
1228 case SIGTRAP:
1229 message = MonitorSIGTRAP(monitor, &info, pid);
1230 break;
1231
1232 default:
1233 message = MonitorSignal(monitor, &info, pid);
1234 break;
1235 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001236
Stephen Wilson84ffe702011-03-30 15:55:52 +00001237 process->SendMessage(message);
1238 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001239 }
1240
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001241 return stop_monitoring;
1242}
1243
1244ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001245ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001246 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001247{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001248 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001249
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001250 assert(monitor);
1251 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001252
Stephen Wilson84ffe702011-03-30 15:55:52 +00001253 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001254 {
1255 default:
1256 assert(false && "Unexpected SIGTRAP code!");
1257 break;
1258
1259 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1260 {
1261 // The inferior process is about to exit. Maintain the process in a
1262 // state of "limbo" until we are explicitly commanded to detach,
1263 // destroy, resume, etc.
1264 unsigned long data = 0;
1265 if (!monitor->GetEventMessage(pid, &data))
1266 data = -1;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001267 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001268 break;
1269 }
1270
1271 case 0:
1272 case TRAP_TRACE:
1273 message = ProcessMessage::Trace(pid);
1274 break;
1275
1276 case SI_KERNEL:
1277 case TRAP_BRKPT:
1278 message = ProcessMessage::Break(pid);
1279 break;
1280 }
1281
1282 return message;
1283}
1284
Stephen Wilson84ffe702011-03-30 15:55:52 +00001285ProcessMessage
1286ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001287 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001288{
1289 ProcessMessage message;
1290 int signo = info->si_signo;
1291
1292 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1293 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1294 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1295 //
1296 // IOW, user generated signals never generate what we consider to be a
1297 // "crash".
1298 //
1299 // Similarly, ACK signals generated by this monitor.
1300 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1301 {
1302 if (info->si_pid == getpid())
1303 return ProcessMessage::SignalDelivered(pid, signo);
1304 else
1305 return ProcessMessage::Signal(pid, signo);
1306 }
1307
1308 if (signo == SIGSEGV) {
1309 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1310 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1311 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1312 }
1313
1314 if (signo == SIGILL) {
1315 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1316 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1317 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1318 }
1319
1320 if (signo == SIGFPE) {
1321 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1322 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1323 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1324 }
1325
1326 if (signo == SIGBUS) {
1327 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1328 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1329 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1330 }
1331
1332 // Everything else is "normal" and does not require any special action on
1333 // our part.
1334 return ProcessMessage::Signal(pid, signo);
1335}
1336
1337ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001338ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001339{
1340 ProcessMessage::CrashReason reason;
1341 assert(info->si_signo == SIGSEGV);
1342
1343 reason = ProcessMessage::eInvalidCrashReason;
1344
1345 switch (info->si_code)
1346 {
1347 default:
1348 assert(false && "unexpected si_code for SIGSEGV");
1349 break;
1350 case SEGV_MAPERR:
1351 reason = ProcessMessage::eInvalidAddress;
1352 break;
1353 case SEGV_ACCERR:
1354 reason = ProcessMessage::ePrivilegedAddress;
1355 break;
1356 }
1357
1358 return reason;
1359}
1360
1361ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001362ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001363{
1364 ProcessMessage::CrashReason reason;
1365 assert(info->si_signo == SIGILL);
1366
1367 reason = ProcessMessage::eInvalidCrashReason;
1368
1369 switch (info->si_code)
1370 {
1371 default:
1372 assert(false && "unexpected si_code for SIGILL");
1373 break;
1374 case ILL_ILLOPC:
1375 reason = ProcessMessage::eIllegalOpcode;
1376 break;
1377 case ILL_ILLOPN:
1378 reason = ProcessMessage::eIllegalOperand;
1379 break;
1380 case ILL_ILLADR:
1381 reason = ProcessMessage::eIllegalAddressingMode;
1382 break;
1383 case ILL_ILLTRP:
1384 reason = ProcessMessage::eIllegalTrap;
1385 break;
1386 case ILL_PRVOPC:
1387 reason = ProcessMessage::ePrivilegedOpcode;
1388 break;
1389 case ILL_PRVREG:
1390 reason = ProcessMessage::ePrivilegedRegister;
1391 break;
1392 case ILL_COPROC:
1393 reason = ProcessMessage::eCoprocessorError;
1394 break;
1395 case ILL_BADSTK:
1396 reason = ProcessMessage::eInternalStackError;
1397 break;
1398 }
1399
1400 return reason;
1401}
1402
1403ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001404ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001405{
1406 ProcessMessage::CrashReason reason;
1407 assert(info->si_signo == SIGFPE);
1408
1409 reason = ProcessMessage::eInvalidCrashReason;
1410
1411 switch (info->si_code)
1412 {
1413 default:
1414 assert(false && "unexpected si_code for SIGFPE");
1415 break;
1416 case FPE_INTDIV:
1417 reason = ProcessMessage::eIntegerDivideByZero;
1418 break;
1419 case FPE_INTOVF:
1420 reason = ProcessMessage::eIntegerOverflow;
1421 break;
1422 case FPE_FLTDIV:
1423 reason = ProcessMessage::eFloatDivideByZero;
1424 break;
1425 case FPE_FLTOVF:
1426 reason = ProcessMessage::eFloatOverflow;
1427 break;
1428 case FPE_FLTUND:
1429 reason = ProcessMessage::eFloatUnderflow;
1430 break;
1431 case FPE_FLTRES:
1432 reason = ProcessMessage::eFloatInexactResult;
1433 break;
1434 case FPE_FLTINV:
1435 reason = ProcessMessage::eFloatInvalidOperation;
1436 break;
1437 case FPE_FLTSUB:
1438 reason = ProcessMessage::eFloatSubscriptRange;
1439 break;
1440 }
1441
1442 return reason;
1443}
1444
1445ProcessMessage::CrashReason
Greg Clayton28041352011-11-29 20:50:10 +00001446ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001447{
1448 ProcessMessage::CrashReason reason;
1449 assert(info->si_signo == SIGBUS);
1450
1451 reason = ProcessMessage::eInvalidCrashReason;
1452
1453 switch (info->si_code)
1454 {
1455 default:
1456 assert(false && "unexpected si_code for SIGBUS");
1457 break;
1458 case BUS_ADRALN:
1459 reason = ProcessMessage::eIllegalAlignment;
1460 break;
1461 case BUS_ADRERR:
1462 reason = ProcessMessage::eIllegalAddress;
1463 break;
1464 case BUS_OBJERR:
1465 reason = ProcessMessage::eHardwareError;
1466 break;
1467 }
1468
1469 return reason;
1470}
1471
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001472void
Johnny Chen25e68e32011-06-14 19:19:50 +00001473ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001474{
1475 int status;
1476 pollfd fdset;
Johnny Chen25e68e32011-06-14 19:19:50 +00001477
Stephen Wilson570243b2011-01-19 01:37:06 +00001478 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001479
1480 fdset.fd = monitor->m_server_fd;
1481 fdset.events = POLLIN | POLLPRI;
1482 fdset.revents = 0;
1483
Stephen Wilson570243b2011-01-19 01:37:06 +00001484 // We are finised with the arguments and are ready to go. Sync with the
1485 // parent thread and start serving operations on the inferior.
1486 sem_post(&args->m_semaphore);
1487
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001488 for (;;)
1489 {
1490 if ((status = poll(&fdset, 1, -1)) < 0)
1491 {
1492 switch (errno)
1493 {
1494 default:
1495 assert(false && "Unexpected poll() failure!");
1496 continue;
1497
1498 case EINTR: continue; // Just poll again.
1499 case EBADF: return; // Connection terminated.
1500 }
1501 }
1502
1503 assert(status == 1 && "Too many descriptors!");
1504
1505 if (fdset.revents & POLLIN)
1506 {
1507 Operation *op = NULL;
1508
1509 READ_AGAIN:
1510 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1511 {
1512 // There is only one acceptable failure.
1513 assert(errno == EINTR);
1514 goto READ_AGAIN;
1515 }
1516
1517 assert(status == sizeof(op));
1518 op->Execute(monitor);
1519 write(fdset.fd, &op, sizeof(op));
1520 }
1521 }
1522}
1523
1524void
1525ProcessMonitor::DoOperation(Operation *op)
1526{
1527 int status;
1528 Operation *ack = NULL;
1529 Mutex::Locker lock(m_server_mutex);
1530
1531 // FIXME: Do proper error checking here.
1532 write(m_client_fd, &op, sizeof(op));
1533
1534READ_AGAIN:
1535 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1536 {
1537 // If interrupted by a signal handler try again. Otherwise the monitor
1538 // thread probably died and we have a stale file descriptor -- abort the
1539 // operation.
1540 if (errno == EINTR)
1541 goto READ_AGAIN;
1542 return;
1543 }
1544
1545 assert(status == sizeof(ack));
1546 assert(ack == op && "Invalid monitor thread response!");
1547}
1548
1549size_t
1550ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1551 Error &error)
1552{
1553 size_t result;
1554 ReadOperation op(vm_addr, buf, size, error, result);
1555 DoOperation(&op);
1556 return result;
1557}
1558
1559size_t
1560ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1561 lldb_private::Error &error)
1562{
1563 size_t result;
1564 WriteOperation op(vm_addr, buf, size, error, result);
1565 DoOperation(&op);
1566 return result;
1567}
1568
1569bool
Johnny Chen30213ff2012-01-05 19:17:38 +00001570ProcessMonitor::ReadRegisterValue(unsigned offset, unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001571{
1572 bool result;
1573 ReadRegOperation op(offset, value, result);
1574 DoOperation(&op);
1575 return result;
1576}
1577
1578bool
Johnny Chen13e8e1c2011-05-13 21:29:50 +00001579ProcessMonitor::WriteRegisterValue(unsigned offset, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001580{
1581 bool result;
1582 WriteRegOperation op(offset, value, result);
1583 DoOperation(&op);
1584 return result;
1585}
1586
1587bool
Stephen Wilsonade1aea2011-01-19 01:31:38 +00001588ProcessMonitor::ReadGPR(void *buf)
1589{
1590 bool result;
1591 ReadGPROperation op(buf, result);
1592 DoOperation(&op);
1593 return result;
1594}
1595
1596bool
1597ProcessMonitor::ReadFPR(void *buf)
1598{
1599 bool result;
1600 ReadFPROperation op(buf, result);
1601 DoOperation(&op);
1602 return result;
1603}
1604
1605bool
Peter Collingbourne10bc0102011-06-03 20:41:02 +00001606ProcessMonitor::WriteGPR(void *buf)
1607{
1608 bool result;
1609 WriteGPROperation op(buf, result);
1610 DoOperation(&op);
1611 return result;
1612}
1613
1614bool
1615ProcessMonitor::WriteFPR(void *buf)
1616{
1617 bool result;
1618 WriteFPROperation op(buf, result);
1619 DoOperation(&op);
1620 return result;
1621}
1622
1623bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001624ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001625{
1626 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001627 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001628 DoOperation(&op);
1629 return result;
1630}
1631
1632bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001633ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001634{
1635 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001636 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001637 DoOperation(&op);
1638 return result;
1639}
1640
1641bool
1642ProcessMonitor::BringProcessIntoLimbo()
1643{
1644 bool result;
1645 KillOperation op(result);
1646 DoOperation(&op);
1647 return result;
1648}
1649
1650bool
1651ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo)
1652{
1653 bool result;
1654 SiginfoOperation op(tid, siginfo, result);
1655 DoOperation(&op);
1656 return result;
1657}
1658
1659bool
1660ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1661{
1662 bool result;
1663 EventMessageOperation op(tid, message, result);
1664 DoOperation(&op);
1665 return result;
1666}
1667
1668bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00001669ProcessMonitor::Detach()
1670{
1671 bool result;
Greg Clayton28041352011-11-29 20:50:10 +00001672 lldb_private::Error error;
1673 DetachOperation op(error);
1674 result = error.Success();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001675 DoOperation(&op);
1676 StopMonitor();
1677 return result;
1678}
1679
1680bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001681ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1682{
Peter Collingbourne62343202011-06-14 03:55:54 +00001683 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001684
1685 if (target_fd == -1)
1686 return false;
1687
Peter Collingbourne62343202011-06-14 03:55:54 +00001688 return (dup2(target_fd, fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001689}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001690
1691void
1692ProcessMonitor::StopMonitoringChildProcess()
1693{
1694 lldb::thread_result_t thread_result;
1695
Stephen Wilsond4182f42011-02-09 20:10:35 +00001696 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
Stephen Wilson9212d7f2011-01-04 21:40:25 +00001697 {
1698 Host::ThreadCancel(m_monitor_thread, NULL);
1699 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1700 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1701 }
1702}
Stephen Wilson84ffe702011-03-30 15:55:52 +00001703
1704void
1705ProcessMonitor::StopMonitor()
1706{
1707 StopMonitoringChildProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001708 StopLaunchOpThread();
Stephen Wilson84ffe702011-03-30 15:55:52 +00001709 CloseFD(m_terminal_fd);
1710 CloseFD(m_client_fd);
1711 CloseFD(m_server_fd);
1712}
1713
1714void
1715ProcessMonitor::CloseFD(int &fd)
1716{
1717 if (fd != -1)
1718 {
1719 close(fd);
1720 fd = -1;
1721 }
1722}