blob: 3d793d0c1c20384f4d9cc4f40b5d034552a20547 [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +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>
Ed Mastea02f5532013-07-02 16:45:16 +000014#include <stdint.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000015#include <unistd.h>
16#include <signal.h>
17#include <sys/ptrace.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21
22// C++ Includes
23// Other libraries and framework includes
24#include "lldb/Core/Error.h"
25#include "lldb/Core/RegisterValue.h"
26#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
32
33#include "POSIXThread.h"
34#include "ProcessFreeBSD.h"
35#include "ProcessPOSIXLog.h"
36#include "ProcessMonitor.h"
37
38extern "C" {
39 extern char ** environ;
40 }
41
42using namespace lldb;
43using namespace lldb_private;
44
45// We disable the tracing of ptrace calls for integration builds to
46// avoid the additional indirection and checks.
47#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
48// Wrapper for ptrace to catch errors and log calls.
49
50const char *
51Get_PT_IO_OP(int op)
52{
53 switch (op) {
54 case PIOD_READ_D: return "READ_D";
55 case PIOD_WRITE_D: return "WRITE_D";
56 case PIOD_READ_I: return "READ_I";
57 case PIOD_WRITE_I: return "WRITE_I";
58 default: return "Unknown op";
59 }
60}
61
Matt Kopec7de48462013-03-06 17:20:48 +000062// Wrapper for ptrace to catch errors and log calls.
63// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000064extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000065PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000066 const char* reqName, const char* file, int line)
67{
68 long int result;
69
Ashok Thirumurthi01186352013-03-28 16:02:31 +000070 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000071
72 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000073 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000074 reqName, pid, addr, data, file, line);
75 if (req == PT_IO) {
76 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
77
Sylvestre Ledru779f9212013-10-31 23:55:19 +000078 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000079 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000080 }
81 }
82
83 //PtraceDisplayBytes(req, data);
84
85 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000086 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000087
88 //PtraceDisplayBytes(req, data);
89
Matt Kopec7de48462013-03-06 17:20:48 +000090 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000091 {
92 const char* str;
93 switch (errno)
94 {
95 case ESRCH: str = "ESRCH"; break;
96 case EINVAL: str = "EINVAL"; break;
97 case EBUSY: str = "EBUSY"; break;
98 case EPERM: str = "EPERM"; break;
99 default: str = "<unknown>";
100 }
101 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
102 }
103
104 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000105#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000106 if (req == PT_GETREGS) {
107 struct reg *r = (struct reg *) addr;
108
109 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
110 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
111 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
112 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
113 }
Ed Maste7d4c0d5b2013-07-22 20:51:08 +0000114#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000115 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
116 struct dbreg *r = (struct dbreg *) addr;
117 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
118
119 for (int i = 0; i <= 7; i++)
120 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
121 }
122 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000123
124 return result;
125}
126
Matt Kopec7de48462013-03-06 17:20:48 +0000127// Wrapper for ptrace when logging is not required.
128// Sets errno to 0 prior to calling ptrace.
129extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000130PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000131{
132 long result = 0;
133 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000134 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000135 return result;
136}
137
Johnny Chen9ed5b492012-01-05 21:48:15 +0000138#define PTRACE(req, pid, addr, data) \
139 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
140#else
Matt Kopec7de48462013-03-06 17:20:48 +0000141 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000142#endif
143
144//------------------------------------------------------------------------------
145// Static implementations of ProcessMonitor::ReadMemory and
146// ProcessMonitor::WriteMemory. This enables mutual recursion between these
147// functions without needed to go thru the thread funnel.
148
149static size_t
150DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
151 Error &error)
152{
153 struct ptrace_io_desc pi_desc;
154
155 pi_desc.piod_op = PIOD_READ_D;
156 pi_desc.piod_offs = (void *)vm_addr;
157 pi_desc.piod_addr = buf;
158 pi_desc.piod_len = size;
159
160 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
161 error.SetErrorToErrno();
162 return pi_desc.piod_len;
163}
164
165static size_t
166DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
167 size_t size, Error &error)
168{
169 struct ptrace_io_desc pi_desc;
170
171 pi_desc.piod_op = PIOD_WRITE_D;
172 pi_desc.piod_offs = (void *)vm_addr;
173 pi_desc.piod_addr = (void *)buf;
174 pi_desc.piod_len = size;
175
176 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
177 error.SetErrorToErrno();
178 return pi_desc.piod_len;
179}
180
181// Simple helper function to ensure flags are enabled on the given file
182// descriptor.
183static bool
184EnsureFDFlags(int fd, int flags, Error &error)
185{
186 int status;
187
188 if ((status = fcntl(fd, F_GETFL)) == -1)
189 {
190 error.SetErrorToErrno();
191 return false;
192 }
193
194 if (fcntl(fd, F_SETFL, status | flags) == -1)
195 {
196 error.SetErrorToErrno();
197 return false;
198 }
199
200 return true;
201}
202
203//------------------------------------------------------------------------------
204/// @class Operation
205/// @brief Represents a ProcessMonitor operation.
206///
207/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
208/// one that spawned or attached to the process from the start. Therefore, when
209/// a ProcessMonitor is asked to deliver or change the state of an inferior
210/// process the operation must be "funneled" to a specific thread to perform the
211/// task. The Operation class provides an abstract base for all services the
212/// ProcessMonitor must perform via the single virtual function Execute, thus
213/// encapsulating the code that needs to run in the privileged context.
214class Operation
215{
216public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000217 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 virtual void Execute(ProcessMonitor *monitor) = 0;
219};
220
221//------------------------------------------------------------------------------
222/// @class ReadOperation
223/// @brief Implements ProcessMonitor::ReadMemory.
224class ReadOperation : public Operation
225{
226public:
227 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
228 Error &error, size_t &result)
229 : m_addr(addr), m_buff(buff), m_size(size),
230 m_error(error), m_result(result)
231 { }
232
233 void Execute(ProcessMonitor *monitor);
234
235private:
236 lldb::addr_t m_addr;
237 void *m_buff;
238 size_t m_size;
239 Error &m_error;
240 size_t &m_result;
241};
242
243void
244ReadOperation::Execute(ProcessMonitor *monitor)
245{
246 lldb::pid_t pid = monitor->GetPID();
247
248 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
249}
250
251//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000252/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000253/// @brief Implements ProcessMonitor::WriteMemory.
254class WriteOperation : public Operation
255{
256public:
257 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
258 Error &error, size_t &result)
259 : m_addr(addr), m_buff(buff), m_size(size),
260 m_error(error), m_result(result)
261 { }
262
263 void Execute(ProcessMonitor *monitor);
264
265private:
266 lldb::addr_t m_addr;
267 const void *m_buff;
268 size_t m_size;
269 Error &m_error;
270 size_t &m_result;
271};
272
273void
274WriteOperation::Execute(ProcessMonitor *monitor)
275{
276 lldb::pid_t pid = monitor->GetPID();
277
278 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
279}
280
281//------------------------------------------------------------------------------
282/// @class ReadRegOperation
283/// @brief Implements ProcessMonitor::ReadRegisterValue.
284class ReadRegOperation : public Operation
285{
286public:
Ed Maste6f066412013-07-04 21:47:32 +0000287 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
288 RegisterValue &value, bool &result)
289 : m_tid(tid), m_offset(offset), m_size(size),
290 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000291 { }
292
293 void Execute(ProcessMonitor *monitor);
294
295private:
Ed Maste6f066412013-07-04 21:47:32 +0000296 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000297 unsigned m_offset;
298 unsigned m_size;
299 RegisterValue &m_value;
300 bool &m_result;
301};
302
303void
304ReadRegOperation::Execute(ProcessMonitor *monitor)
305{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000306 struct reg regs;
307 int rc;
308
Ed Maste6f066412013-07-04 21:47:32 +0000309 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000310 m_result = false;
311 } else {
312 if (m_size == sizeof(uintptr_t))
313 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
314 else
315 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
316 m_result = true;
317 }
318}
319
320//------------------------------------------------------------------------------
321/// @class WriteRegOperation
322/// @brief Implements ProcessMonitor::WriteRegisterValue.
323class WriteRegOperation : public Operation
324{
325public:
Ed Maste6f066412013-07-04 21:47:32 +0000326 WriteRegOperation(lldb::tid_t tid, unsigned offset,
327 const RegisterValue &value, bool &result)
328 : m_tid(tid), m_offset(offset),
329 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000330 { }
331
332 void Execute(ProcessMonitor *monitor);
333
334private:
Ed Maste6f066412013-07-04 21:47:32 +0000335 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000336 unsigned m_offset;
337 const RegisterValue &m_value;
338 bool &m_result;
339};
340
341void
342WriteRegOperation::Execute(ProcessMonitor *monitor)
343{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000344 struct reg regs;
345
Ed Maste6f066412013-07-04 21:47:32 +0000346 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000347 m_result = false;
348 return;
349 }
350 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000351 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000352 m_result = false;
353 else
354 m_result = true;
355}
356
357//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000358/// @class ReadDebugRegOperation
359/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
360class ReadDebugRegOperation : public Operation
361{
362public:
363 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
364 RegisterValue &value, bool &result)
365 : m_tid(tid), m_offset(offset), m_size(size),
366 m_value(value), m_result(result)
367 { }
368
369 void Execute(ProcessMonitor *monitor);
370
371private:
372 lldb::tid_t m_tid;
373 unsigned m_offset;
374 unsigned m_size;
375 RegisterValue &m_value;
376 bool &m_result;
377};
378
379void
380ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
381{
382 struct dbreg regs;
383 int rc;
384
385 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
386 m_result = false;
387 } else {
388 if (m_size == sizeof(uintptr_t))
389 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
390 else
391 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
392 m_result = true;
393 }
394}
395
396//------------------------------------------------------------------------------
397/// @class WriteDebugRegOperation
398/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
399class WriteDebugRegOperation : public Operation
400{
401public:
402 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
403 const RegisterValue &value, bool &result)
404 : m_tid(tid), m_offset(offset),
405 m_value(value), m_result(result)
406 { }
407
408 void Execute(ProcessMonitor *monitor);
409
410private:
411 lldb::tid_t m_tid;
412 unsigned m_offset;
413 const RegisterValue &m_value;
414 bool &m_result;
415};
416
417void
418WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
419{
420 struct dbreg regs;
421
422 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
423 m_result = false;
424 return;
425 }
426 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
427 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
428 m_result = false;
429 else
430 m_result = true;
431}
432
433//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000434/// @class ReadGPROperation
435/// @brief Implements ProcessMonitor::ReadGPR.
436class ReadGPROperation : public Operation
437{
438public:
Ed Maste6f066412013-07-04 21:47:32 +0000439 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
440 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000441 { }
442
443 void Execute(ProcessMonitor *monitor);
444
445private:
Ed Maste6f066412013-07-04 21:47:32 +0000446 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000447 void *m_buf;
448 bool &m_result;
449};
450
451void
452ReadGPROperation::Execute(ProcessMonitor *monitor)
453{
454 int rc;
455
456 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000457 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000458 if (errno != 0)
459 m_result = false;
460 else
461 m_result = true;
462}
463
464//------------------------------------------------------------------------------
465/// @class ReadFPROperation
466/// @brief Implements ProcessMonitor::ReadFPR.
467class ReadFPROperation : public Operation
468{
469public:
Ed Maste6f066412013-07-04 21:47:32 +0000470 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
471 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000472 { }
473
474 void Execute(ProcessMonitor *monitor);
475
476private:
Ed Maste6f066412013-07-04 21:47:32 +0000477 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000478 void *m_buf;
479 bool &m_result;
480};
481
482void
483ReadFPROperation::Execute(ProcessMonitor *monitor)
484{
Ed Maste6f066412013-07-04 21:47:32 +0000485 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486 m_result = false;
487 else
488 m_result = true;
489}
490
491//------------------------------------------------------------------------------
492/// @class WriteGPROperation
493/// @brief Implements ProcessMonitor::WriteGPR.
494class WriteGPROperation : public Operation
495{
496public:
Ed Maste6f066412013-07-04 21:47:32 +0000497 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
498 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000499 { }
500
501 void Execute(ProcessMonitor *monitor);
502
503private:
Ed Maste6f066412013-07-04 21:47:32 +0000504 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000505 void *m_buf;
506 bool &m_result;
507};
508
509void
510WriteGPROperation::Execute(ProcessMonitor *monitor)
511{
Ed Maste6f066412013-07-04 21:47:32 +0000512 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513 m_result = false;
514 else
515 m_result = true;
516}
517
518//------------------------------------------------------------------------------
519/// @class WriteFPROperation
520/// @brief Implements ProcessMonitor::WriteFPR.
521class WriteFPROperation : public Operation
522{
523public:
Ed Maste6f066412013-07-04 21:47:32 +0000524 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
525 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000526 { }
527
528 void Execute(ProcessMonitor *monitor);
529
530private:
Ed Maste6f066412013-07-04 21:47:32 +0000531 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000532 void *m_buf;
533 bool &m_result;
534};
535
536void
537WriteFPROperation::Execute(ProcessMonitor *monitor)
538{
Ed Maste6f066412013-07-04 21:47:32 +0000539 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540 m_result = false;
541 else
542 m_result = true;
543}
544
545//------------------------------------------------------------------------------
546/// @class ResumeOperation
547/// @brief Implements ProcessMonitor::Resume.
548class ResumeOperation : public Operation
549{
550public:
Ed Maste502f9022013-11-25 16:31:23 +0000551 ResumeOperation(uint32_t signo, bool &result) :
552 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000553
554 void Execute(ProcessMonitor *monitor);
555
556private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000557 uint32_t m_signo;
558 bool &m_result;
559};
560
561void
562ResumeOperation::Execute(ProcessMonitor *monitor)
563{
Ed Maste502f9022013-11-25 16:31:23 +0000564 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000565 int data = 0;
566
567 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
568 data = m_signo;
569
Ed Maste502f9022013-11-25 16:31:23 +0000570 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000571 {
572 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
573
574 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000575 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000576 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000577 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000578 else
579 m_result = true;
580}
581
582//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000583/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584/// @brief Implements ProcessMonitor::SingleStep.
585class SingleStepOperation : public Operation
586{
587public:
Ed Maste502f9022013-11-25 16:31:23 +0000588 SingleStepOperation(uint32_t signo, bool &result)
589 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000590
591 void Execute(ProcessMonitor *monitor);
592
593private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000594 uint32_t m_signo;
595 bool &m_result;
596};
597
598void
599SingleStepOperation::Execute(ProcessMonitor *monitor)
600{
Ed Maste502f9022013-11-25 16:31:23 +0000601 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000602 int data = 0;
603
604 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
605 data = m_signo;
606
Ed Maste502f9022013-11-25 16:31:23 +0000607 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000608 m_result = false;
609 else
610 m_result = true;
611}
612
613//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000614/// @class LwpInfoOperation
615/// @brief Implements ProcessMonitor::GetLwpInfo.
616class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000617{
618public:
Ed Maste819e3992013-07-17 14:02:20 +0000619 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000620 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000621
622 void Execute(ProcessMonitor *monitor);
623
624private:
625 lldb::tid_t m_tid;
626 void *m_info;
627 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000628 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000629};
630
631void
Ed Maste819e3992013-07-17 14:02:20 +0000632LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000633{
634 struct ptrace_lwpinfo plwp;
635
Daniel Maleaa35970a2012-11-23 18:09:58 +0000636 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000637 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000638 m_err = errno;
639 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000640 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000641 m_result = true;
642 }
643}
644
645//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000646/// @class ThreadSuspendOperation
647/// @brief Implements ProcessMonitor::ThreadSuspend.
648class ThreadSuspendOperation : public Operation
649{
650public:
651 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
652 : m_tid(tid), m_suspend(suspend), m_result(result) { }
653
654 void Execute(ProcessMonitor *monitor);
655
656private:
657 lldb::tid_t m_tid;
658 bool m_suspend;
659 bool &m_result;
660} ;
661
662void
663ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
664{
665 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
666}
667
668
669
670//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000671/// @class EventMessageOperation
672/// @brief Implements ProcessMonitor::GetEventMessage.
673class EventMessageOperation : public Operation
674{
675public:
676 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
677 : m_tid(tid), m_message(message), m_result(result) { }
678
679 void Execute(ProcessMonitor *monitor);
680
681private:
682 lldb::tid_t m_tid;
683 unsigned long *m_message;
684 bool &m_result;
685};
686
687void
688EventMessageOperation::Execute(ProcessMonitor *monitor)
689{
690 struct ptrace_lwpinfo plwp;
691
692 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
693 m_result = false;
694 else {
695 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000696 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000697 m_result = true;
698 } else
699 m_result = false;
700 }
701}
702
703//------------------------------------------------------------------------------
704/// @class KillOperation
705/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
706class KillOperation : public Operation
707{
708public:
709 KillOperation(bool &result) : m_result(result) { }
710
711 void Execute(ProcessMonitor *monitor);
712
713private:
714 bool &m_result;
715};
716
717void
718KillOperation::Execute(ProcessMonitor *monitor)
719{
720 lldb::pid_t pid = monitor->GetPID();
721
722 if (PTRACE(PT_KILL, pid, NULL, 0))
723 m_result = false;
724 else
725 m_result = true;
726}
727
728//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000729/// @class DetachOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000730/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
731class DetachOperation : public Operation
732{
733public:
734 DetachOperation(Error &result) : m_error(result) { }
735
736 void Execute(ProcessMonitor *monitor);
737
738private:
739 Error &m_error;
740};
741
742void
743DetachOperation::Execute(ProcessMonitor *monitor)
744{
745 lldb::pid_t pid = monitor->GetPID();
746
747 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
748 m_error.SetErrorToErrno();
749
750}
751
752ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
753 : m_monitor(monitor)
754{
755 sem_init(&m_semaphore, 0, 0);
756}
757
758ProcessMonitor::OperationArgs::~OperationArgs()
759{
760 sem_destroy(&m_semaphore);
761}
762
763ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
764 lldb_private::Module *module,
765 char const **argv,
766 char const **envp,
767 const char *stdin_path,
768 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000769 const char *stderr_path,
770 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000771 : OperationArgs(monitor),
772 m_module(module),
773 m_argv(argv),
774 m_envp(envp),
775 m_stdin_path(stdin_path),
776 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000777 m_stderr_path(stderr_path),
778 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000779
780ProcessMonitor::LaunchArgs::~LaunchArgs()
781{ }
782
783ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
784 lldb::pid_t pid)
785 : OperationArgs(monitor), m_pid(pid) { }
786
787ProcessMonitor::AttachArgs::~AttachArgs()
788{ }
789
790//------------------------------------------------------------------------------
791/// The basic design of the ProcessMonitor is built around two threads.
792///
793/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
794/// for changes in the debugee state. When a change is detected a
795/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
796/// "drives" state changes in the debugger.
797///
798/// The second thread (@see OperationThread) is responsible for two things 1)
799/// launching or attaching to the inferior process, and then 2) servicing
800/// operations such as register reads/writes, stepping, etc. See the comments
801/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000802ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000803 Module *module,
804 const char *argv[],
805 const char *envp[],
806 const char *stdin_path,
807 const char *stdout_path,
808 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000809 const char *working_dir,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000810 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000811 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000812 m_operation_thread(LLDB_INVALID_HOST_THREAD),
813 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
814 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000815 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000816 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000817{
Ed Maste756e1ff2013-09-18 19:34:08 +0000818 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
819 stdin_path, stdout_path, stderr_path,
820 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821
822
Ed Maste756e1ff2013-09-18 19:34:08 +0000823 sem_init(&m_operation_pending, 0, 0);
824 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000825
826 StartLaunchOpThread(args.get(), error);
827 if (!error.Success())
828 return;
829
830WAIT_AGAIN:
831 // Wait for the operation thread to initialize.
832 if (sem_wait(&args->m_semaphore))
833 {
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.
844 if (!args->m_error.Success())
845 {
Ed Mastea02f5532013-07-02 16:45:16 +0000846 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000847 error = args->m_error;
848 return;
849 }
850
851 // Finally, start monitoring the child process for change in state.
852 m_monitor_thread = Host::StartMonitoringChildProcess(
853 ProcessMonitor::MonitorCallback, this, GetPID(), true);
854 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
855 {
856 error.SetErrorToGenericError();
857 error.SetErrorString("Process launch failed.");
858 return;
859 }
860}
861
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000862ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000863 lldb::pid_t pid,
864 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000865 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000866 m_operation_thread(LLDB_INVALID_HOST_THREAD),
867 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
868 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000869 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000870 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000871{
Ed Maste756e1ff2013-09-18 19:34:08 +0000872 sem_init(&m_operation_pending, 0, 0);
873 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874
Johnny Chen9ed5b492012-01-05 21:48:15 +0000875
Ed Maste756e1ff2013-09-18 19:34:08 +0000876 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000877
878 StartAttachOpThread(args.get(), error);
879 if (!error.Success())
880 return;
881
882WAIT_AGAIN:
883 // Wait for the operation thread to initialize.
884 if (sem_wait(&args->m_semaphore))
885 {
886 if (errno == EINTR)
887 goto WAIT_AGAIN;
888 else
889 {
890 error.SetErrorToErrno();
891 return;
892 }
893 }
894
Ed Mastea02f5532013-07-02 16:45:16 +0000895 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000896 if (!args->m_error.Success())
897 {
Ed Mastea02f5532013-07-02 16:45:16 +0000898 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000899 error = args->m_error;
900 return;
901 }
902
903 // Finally, start monitoring the child process for change in state.
904 m_monitor_thread = Host::StartMonitoringChildProcess(
905 ProcessMonitor::MonitorCallback, this, GetPID(), true);
906 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
907 {
908 error.SetErrorToGenericError();
909 error.SetErrorString("Process attach failed.");
910 return;
911 }
912}
913
914ProcessMonitor::~ProcessMonitor()
915{
916 StopMonitor();
917}
918
919//------------------------------------------------------------------------------
920// Thread setup and tear down.
921void
922ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
923{
924 static const char *g_thread_name = "lldb.process.freebsd.operation";
925
926 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
927 return;
928
929 m_operation_thread =
930 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
931}
932
Johnny Chen9ed5b492012-01-05 21:48:15 +0000933void *
934ProcessMonitor::LaunchOpThread(void *arg)
935{
936 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
937
938 if (!Launch(args)) {
939 sem_post(&args->m_semaphore);
940 return NULL;
941 }
942
943 ServeOperation(args);
944 return NULL;
945}
946
947bool
948ProcessMonitor::Launch(LaunchArgs *args)
949{
950 ProcessMonitor *monitor = args->m_monitor;
951 ProcessFreeBSD &process = monitor->GetProcess();
952 const char **argv = args->m_argv;
953 const char **envp = args->m_envp;
954 const char *stdin_path = args->m_stdin_path;
955 const char *stdout_path = args->m_stdout_path;
956 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000957 const char *working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000958
959 lldb_utility::PseudoTerminal terminal;
960 const size_t err_len = 1024;
961 char err_str[err_len];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000962 lldb::pid_t pid;
963
Johnny Chen9ed5b492012-01-05 21:48:15 +0000964 // Propagate the environment if one is not supplied.
965 if (envp == NULL || envp[0] == NULL)
966 envp = const_cast<const char **>(environ);
967
Ed Mastea02f5532013-07-02 16:45:16 +0000968 if ((pid = terminal.Fork(err_str, err_len)) == -1)
969 {
970 args->m_error.SetErrorToGenericError();
971 args->m_error.SetErrorString("Process fork failed.");
972 goto FINISH;
973 }
974
Johnny Chen9ed5b492012-01-05 21:48:15 +0000975 // Recognized child exit status codes.
976 enum {
977 ePtraceFailed = 1,
978 eDupStdinFailed,
979 eDupStdoutFailed,
980 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000981 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000982 eExecFailed,
983 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000984 };
985
Johnny Chen9ed5b492012-01-05 21:48:15 +0000986 // Child process.
987 if (pid == 0)
988 {
989 // Trace this process.
990 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
991 exit(ePtraceFailed);
992
993 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +0000994 if (setgid(getgid()) != 0)
995 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000996
997 // Let us have our own process group.
998 setpgid(0, 0);
999
1000 // Dup file descriptors if needed.
1001 //
1002 // FIXME: If two or more of the paths are the same we needlessly open
1003 // the same file multiple times.
1004 if (stdin_path != NULL && stdin_path[0])
1005 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1006 exit(eDupStdinFailed);
1007
1008 if (stdout_path != NULL && stdout_path[0])
1009 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1010 exit(eDupStdoutFailed);
1011
1012 if (stderr_path != NULL && stderr_path[0])
1013 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1014 exit(eDupStderrFailed);
1015
Daniel Malea6217d2a2013-01-08 14:49:22 +00001016 // Change working directory
1017 if (working_dir != NULL && working_dir[0])
1018 if (0 != ::chdir(working_dir))
1019 exit(eChdirFailed);
1020
Johnny Chen9ed5b492012-01-05 21:48:15 +00001021 // Execute. We should never return.
1022 execve(argv[0],
1023 const_cast<char *const *>(argv),
1024 const_cast<char *const *>(envp));
1025 exit(eExecFailed);
1026 }
1027
1028 // Wait for the child process to to trap on its call to execve.
1029 ::pid_t wpid;
1030 int status;
1031 if ((wpid = waitpid(pid, &status, 0)) < 0)
1032 {
1033 args->m_error.SetErrorToErrno();
1034 goto FINISH;
1035 }
1036 else if (WIFEXITED(status))
1037 {
1038 // open, dup or execve likely failed for some reason.
1039 args->m_error.SetErrorToGenericError();
1040 switch (WEXITSTATUS(status))
1041 {
Ed Maste5d34af32013-06-24 15:09:18 +00001042 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001043 args->m_error.SetErrorString("Child ptrace failed.");
1044 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001045 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001046 args->m_error.SetErrorString("Child open stdin failed.");
1047 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001048 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001049 args->m_error.SetErrorString("Child open stdout failed.");
1050 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001051 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001052 args->m_error.SetErrorString("Child open stderr failed.");
1053 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001054 case eChdirFailed:
1055 args->m_error.SetErrorString("Child failed to set working directory.");
1056 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001057 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001058 args->m_error.SetErrorString("Child exec failed.");
1059 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001060 case eSetGidFailed:
1061 args->m_error.SetErrorString("Child setgid failed.");
1062 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001063 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001064 args->m_error.SetErrorString("Child returned unknown exit status.");
1065 break;
1066 }
1067 goto FINISH;
1068 }
Ed Maste82a00052013-11-25 21:15:53 +00001069 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001070 "Could not sync with inferior process.");
1071
1072#ifdef notyet
1073 // Have the child raise an event on exit. This is used to keep the child in
1074 // limbo until it is destroyed.
1075 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1076 {
1077 args->m_error.SetErrorToErrno();
1078 goto FINISH;
1079 }
1080#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001081 // Release the master terminal descriptor and pass it off to the
1082 // ProcessMonitor instance. Similarly stash the inferior pid.
1083 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001084 monitor->m_pid = pid;
1085
1086 // Set the terminal fd to be in non blocking mode (it simplifies the
1087 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1088 // descriptor to read from).
1089 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1090 goto FINISH;
1091
Ed Mastee5441432013-09-03 23:55:30 +00001092 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001093
1094FINISH:
1095 return args->m_error.Success();
1096}
1097
Johnny Chen9ed5b492012-01-05 21:48:15 +00001098void
1099ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1100{
1101 static const char *g_thread_name = "lldb.process.freebsd.operation";
1102
1103 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1104 return;
1105
1106 m_operation_thread =
1107 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1108}
1109
Johnny Chen9ed5b492012-01-05 21:48:15 +00001110void *
1111ProcessMonitor::AttachOpThread(void *arg)
1112{
1113 AttachArgs *args = static_cast<AttachArgs*>(arg);
1114
1115 if (!Attach(args))
1116 return NULL;
1117
1118 ServeOperation(args);
1119 return NULL;
1120}
1121
1122bool
1123ProcessMonitor::Attach(AttachArgs *args)
1124{
1125 lldb::pid_t pid = args->m_pid;
1126
1127 ProcessMonitor *monitor = args->m_monitor;
1128 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001129
1130 if (pid <= 1)
1131 {
1132 args->m_error.SetErrorToGenericError();
1133 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1134 goto FINISH;
1135 }
1136
1137 // Attach to the requested process.
1138 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1139 {
1140 args->m_error.SetErrorToErrno();
1141 goto FINISH;
1142 }
1143
1144 int status;
1145 if ((status = waitpid(pid, NULL, 0)) < 0)
1146 {
1147 args->m_error.SetErrorToErrno();
1148 goto FINISH;
1149 }
1150
Ed Mastee5441432013-09-03 23:55:30 +00001151 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001152
Ed Mastee5441432013-09-03 23:55:30 +00001153FINISH:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001154 return args->m_error.Success();
1155}
1156
Ed Maste7fd845c2013-12-09 15:51:17 +00001157size_t
1158ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1159{
1160 lwpid_t *tids;
1161 int tdcnt;
1162
1163 thread_ids.clear();
1164
1165 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1166 if (tdcnt <= 0)
1167 return 0;
1168 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1169 if (tids == NULL)
1170 return 0;
1171 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1172 free(tids);
1173 return 0;
1174 }
1175 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1176 free(tids);
1177 return thread_ids.size();
1178}
1179
Johnny Chen9ed5b492012-01-05 21:48:15 +00001180bool
1181ProcessMonitor::MonitorCallback(void *callback_baton,
1182 lldb::pid_t pid,
1183 bool exited,
1184 int signal,
1185 int status)
1186{
1187 ProcessMessage message;
1188 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001189 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001190 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001191 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001192 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001193 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001194
Ed Mastea02f5532013-07-02 16:45:16 +00001195 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1196
1197 if (exited)
1198 {
1199 if (log)
1200 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1201 message = ProcessMessage::Exit(pid, status);
1202 process->SendMessage(message);
1203 return pid == process->GetID();
1204 }
1205
Ed Maste819e3992013-07-17 14:02:20 +00001206 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001207 stop_monitoring = true; // pid is gone. Bail.
1208 else {
Ed Maste819e3992013-07-17 14:02:20 +00001209 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001210 {
1211 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001212 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001213 break;
1214
1215 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001216 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001217 break;
1218 }
1219
1220 process->SendMessage(message);
1221 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1222 }
1223
1224 return stop_monitoring;
1225}
1226
1227ProcessMessage
1228ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001229 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001230{
1231 ProcessMessage message;
1232
Ed Mastea02f5532013-07-02 16:45:16 +00001233 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1234
Matt Kopec7de48462013-03-06 17:20:48 +00001235 assert(monitor);
1236 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001237
1238 switch (info->si_code)
1239 {
1240 default:
1241 assert(false && "Unexpected SIGTRAP code!");
1242 break;
1243
1244 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1245 {
1246 // The inferior process is about to exit. Maintain the process in a
1247 // state of "limbo" until we are explicitly commanded to detach,
1248 // destroy, resume, etc.
1249 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001250 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001251 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001252 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001253 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1254 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001255 break;
1256 }
1257
1258 case 0:
1259 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001260 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001261 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001262 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001263 break;
1264
1265 case SI_KERNEL:
1266 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001267 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001268 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1269 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001270 break;
1271 }
1272
1273 return message;
1274}
1275
1276ProcessMessage
1277ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001278 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001279{
1280 ProcessMessage message;
1281 int signo = info->si_signo;
1282
Ed Mastea02f5532013-07-02 16:45:16 +00001283 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1284
Johnny Chen9ed5b492012-01-05 21:48:15 +00001285 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1286 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1287 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1288 //
1289 // IOW, user generated signals never generate what we consider to be a
1290 // "crash".
1291 //
1292 // Similarly, ACK signals generated by this monitor.
1293 if (info->si_code == SI_USER)
1294 {
Ed Mastea02f5532013-07-02 16:45:16 +00001295 if (log)
1296 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1297 __FUNCTION__,
1298 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1299 "SI_USER",
1300 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001301 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001302 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001303 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001304 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001305 }
1306
Ed Mastea02f5532013-07-02 16:45:16 +00001307 if (log)
1308 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1309
Johnny Chen9ed5b492012-01-05 21:48:15 +00001310 if (signo == SIGSEGV) {
1311 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1312 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001313 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001314 }
1315
1316 if (signo == SIGILL) {
1317 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1318 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001319 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001320 }
1321
1322 if (signo == SIGFPE) {
1323 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1324 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001325 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001326 }
1327
1328 if (signo == SIGBUS) {
1329 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1330 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001331 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001332 }
1333
1334 // Everything else is "normal" and does not require any special action on
1335 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001336 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001337}
1338
1339ProcessMessage::CrashReason
1340ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1341{
1342 ProcessMessage::CrashReason reason;
1343 assert(info->si_signo == SIGSEGV);
1344
1345 reason = ProcessMessage::eInvalidCrashReason;
1346
1347 switch (info->si_code)
1348 {
1349 default:
1350 assert(false && "unexpected si_code for SIGSEGV");
1351 break;
1352 case SEGV_MAPERR:
1353 reason = ProcessMessage::eInvalidAddress;
1354 break;
1355 case SEGV_ACCERR:
1356 reason = ProcessMessage::ePrivilegedAddress;
1357 break;
1358 }
1359
1360 return reason;
1361}
1362
1363ProcessMessage::CrashReason
1364ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1365{
1366 ProcessMessage::CrashReason reason;
1367 assert(info->si_signo == SIGILL);
1368
1369 reason = ProcessMessage::eInvalidCrashReason;
1370
1371 switch (info->si_code)
1372 {
1373 default:
1374 assert(false && "unexpected si_code for SIGILL");
1375 break;
1376 case ILL_ILLOPC:
1377 reason = ProcessMessage::eIllegalOpcode;
1378 break;
1379 case ILL_ILLOPN:
1380 reason = ProcessMessage::eIllegalOperand;
1381 break;
1382 case ILL_ILLADR:
1383 reason = ProcessMessage::eIllegalAddressingMode;
1384 break;
1385 case ILL_ILLTRP:
1386 reason = ProcessMessage::eIllegalTrap;
1387 break;
1388 case ILL_PRVOPC:
1389 reason = ProcessMessage::ePrivilegedOpcode;
1390 break;
1391 case ILL_PRVREG:
1392 reason = ProcessMessage::ePrivilegedRegister;
1393 break;
1394 case ILL_COPROC:
1395 reason = ProcessMessage::eCoprocessorError;
1396 break;
1397 case ILL_BADSTK:
1398 reason = ProcessMessage::eInternalStackError;
1399 break;
1400 }
1401
1402 return reason;
1403}
1404
1405ProcessMessage::CrashReason
1406ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1407{
1408 ProcessMessage::CrashReason reason;
1409 assert(info->si_signo == SIGFPE);
1410
1411 reason = ProcessMessage::eInvalidCrashReason;
1412
1413 switch (info->si_code)
1414 {
1415 default:
1416 assert(false && "unexpected si_code for SIGFPE");
1417 break;
1418 case FPE_INTDIV:
1419 reason = ProcessMessage::eIntegerDivideByZero;
1420 break;
1421 case FPE_INTOVF:
1422 reason = ProcessMessage::eIntegerOverflow;
1423 break;
1424 case FPE_FLTDIV:
1425 reason = ProcessMessage::eFloatDivideByZero;
1426 break;
1427 case FPE_FLTOVF:
1428 reason = ProcessMessage::eFloatOverflow;
1429 break;
1430 case FPE_FLTUND:
1431 reason = ProcessMessage::eFloatUnderflow;
1432 break;
1433 case FPE_FLTRES:
1434 reason = ProcessMessage::eFloatInexactResult;
1435 break;
1436 case FPE_FLTINV:
1437 reason = ProcessMessage::eFloatInvalidOperation;
1438 break;
1439 case FPE_FLTSUB:
1440 reason = ProcessMessage::eFloatSubscriptRange;
1441 break;
1442 }
1443
1444 return reason;
1445}
1446
1447ProcessMessage::CrashReason
1448ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1449{
1450 ProcessMessage::CrashReason reason;
1451 assert(info->si_signo == SIGBUS);
1452
1453 reason = ProcessMessage::eInvalidCrashReason;
1454
1455 switch (info->si_code)
1456 {
1457 default:
1458 assert(false && "unexpected si_code for SIGBUS");
1459 break;
1460 case BUS_ADRALN:
1461 reason = ProcessMessage::eIllegalAlignment;
1462 break;
1463 case BUS_ADRERR:
1464 reason = ProcessMessage::eIllegalAddress;
1465 break;
1466 case BUS_OBJERR:
1467 reason = ProcessMessage::eHardwareError;
1468 break;
1469 }
1470
1471 return reason;
1472}
1473
1474void
1475ProcessMonitor::ServeOperation(OperationArgs *args)
1476{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001477 ProcessMonitor *monitor = args->m_monitor;
1478
Johnny Chen9ed5b492012-01-05 21:48:15 +00001479 // We are finised with the arguments and are ready to go. Sync with the
1480 // parent thread and start serving operations on the inferior.
1481 sem_post(&args->m_semaphore);
1482
1483 for (;;)
1484 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001485 // wait for next pending operation
1486 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001487
Ed Maste756e1ff2013-09-18 19:34:08 +00001488 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001489
Ed Maste756e1ff2013-09-18 19:34:08 +00001490 // notify calling thread that operation is complete
1491 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001492 }
1493}
1494
1495void
1496ProcessMonitor::DoOperation(Operation *op)
1497{
Ed Maste756e1ff2013-09-18 19:34:08 +00001498 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001499
Ed Maste756e1ff2013-09-18 19:34:08 +00001500 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001501
Ed Maste756e1ff2013-09-18 19:34:08 +00001502 // notify operation thread that an operation is ready to be processed
1503 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001504
Ed Maste756e1ff2013-09-18 19:34:08 +00001505 // wait for operation to complete
1506 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001507}
1508
1509size_t
1510ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1511 Error &error)
1512{
1513 size_t result;
1514 ReadOperation op(vm_addr, buf, size, error, result);
1515 DoOperation(&op);
1516 return result;
1517}
1518
1519size_t
1520ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1521 lldb_private::Error &error)
1522{
1523 size_t result;
1524 WriteOperation op(vm_addr, buf, size, error, result);
1525 DoOperation(&op);
1526 return result;
1527}
1528
1529bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001530ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001531 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001532{
1533 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001534 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001535 DoOperation(&op);
1536 return result;
1537}
1538
1539bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001540ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001541 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001542{
1543 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001544 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001545 DoOperation(&op);
1546 return result;
1547}
1548
1549bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001550ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1551 const char *reg_name, unsigned size,
1552 lldb_private::RegisterValue &value)
1553{
1554 bool result;
1555 ReadDebugRegOperation op(tid, offset, size, value, result);
1556 DoOperation(&op);
1557 return result;
1558}
1559
1560bool
1561ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1562 const char *reg_name,
1563 const lldb_private::RegisterValue &value)
1564{
1565 bool result;
1566 WriteDebugRegOperation op(tid, offset, value, result);
1567 DoOperation(&op);
1568 return result;
1569}
1570
1571bool
Matt Kopec7de48462013-03-06 17:20:48 +00001572ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001573{
1574 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001575 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001576 DoOperation(&op);
1577 return result;
1578}
1579
1580bool
Matt Kopec7de48462013-03-06 17:20:48 +00001581ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001582{
1583 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001584 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001585 DoOperation(&op);
1586 return result;
1587}
1588
1589bool
Ed Maste5d34af32013-06-24 15:09:18 +00001590ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1591{
1592 return false;
1593}
1594
1595bool
Matt Kopec7de48462013-03-06 17:20:48 +00001596ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001597{
1598 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001599 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001600 DoOperation(&op);
1601 return result;
1602}
1603
1604bool
Matt Kopec7de48462013-03-06 17:20:48 +00001605ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001606{
1607 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001608 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001609 DoOperation(&op);
1610 return result;
1611}
1612
1613bool
Ed Maste5d34af32013-06-24 15:09:18 +00001614ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1615{
1616 return false;
1617}
1618
1619bool
Ed Maste68f51792013-10-18 19:16:44 +00001620ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1621{
1622 return false;
1623}
1624
1625bool
Ed Maste502f9022013-11-25 16:31:23 +00001626ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001627{
1628 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001629 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1630
1631 if (log)
Ed Maste502f9022013-11-25 16:31:23 +00001632 log->Printf ("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s", __FUNCTION__, GetPID(),
Ed Mastea02f5532013-07-02 16:45:16 +00001633 m_process->GetUnixSignals().GetSignalAsCString (signo));
Ed Maste502f9022013-11-25 16:31:23 +00001634 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001635 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001636 if (log)
1637 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001638 return result;
1639}
1640
1641bool
Ed Maste502f9022013-11-25 16:31:23 +00001642ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001643{
1644 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001645 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001646 DoOperation(&op);
1647 return result;
1648}
1649
1650bool
1651ProcessMonitor::BringProcessIntoLimbo()
1652{
1653 bool result;
1654 KillOperation op(result);
1655 DoOperation(&op);
1656 return result;
1657}
1658
1659bool
Ed Maste819e3992013-07-17 14:02:20 +00001660ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001661{
1662 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001663 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001664 DoOperation(&op);
1665 return result;
1666}
1667
1668bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001669ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1670{
1671 bool result;
1672 ThreadSuspendOperation op(tid, suspend, result);
1673 DoOperation(&op);
1674 return result;
1675}
1676
1677bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001678ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1679{
1680 bool result;
1681 EventMessageOperation op(tid, message, result);
1682 DoOperation(&op);
1683 return result;
1684}
1685
Ed Mastea02f5532013-07-02 16:45:16 +00001686lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001687ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001688{
Ed Mastea02f5532013-07-02 16:45:16 +00001689 lldb_private::Error error;
1690 if (tid != LLDB_INVALID_THREAD_ID)
1691 {
1692 DetachOperation op(error);
1693 DoOperation(&op);
1694 }
1695 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001696}
1697
1698bool
1699ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1700{
1701 int target_fd = open(path, flags, 0666);
1702
1703 if (target_fd == -1)
1704 return false;
1705
1706 return (dup2(target_fd, fd) == -1) ? false : true;
1707}
1708
1709void
1710ProcessMonitor::StopMonitoringChildProcess()
1711{
1712 lldb::thread_result_t thread_result;
1713
1714 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1715 {
1716 Host::ThreadCancel(m_monitor_thread, NULL);
1717 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1718 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1719 }
1720}
1721
1722void
1723ProcessMonitor::StopMonitor()
1724{
1725 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001726 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001727 sem_destroy(&m_operation_pending);
1728 sem_destroy(&m_operation_done);
1729
Andrew Kaylor5e268992013-09-14 00:17:31 +00001730 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
1731 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
1732 // the descriptor to a ConnectionFileDescriptor object. Consequently
1733 // even though still has the file descriptor, we shouldn't close it here.
Johnny Chen9ed5b492012-01-05 21:48:15 +00001734}
1735
Andrew Kaylord4d54992013-09-17 00:30:24 +00001736// FIXME: On Linux, when a new thread is created, we receive to notifications,
1737// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1738// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1739// the new child thread indicating that it has is stopped because we attached.
1740// We have no guarantee of the order in which these arrive, but we need both
1741// before we are ready to proceed. We currently keep a list of threads which
1742// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1743// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1744// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1745//
1746// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1747// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1748// logically needed.
1749//
1750// We really should figure out what actually happens on FreeBSD and move the
1751// Linux-specific logic out of ProcessPOSIX as needed.
1752
1753bool
1754ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1755{
1756 return true;
1757}
1758
Johnny Chen9ed5b492012-01-05 21:48:15 +00001759void
Ed Mastea02f5532013-07-02 16:45:16 +00001760ProcessMonitor::StopOpThread()
1761{
1762 lldb::thread_result_t result;
1763
1764 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1765 return;
1766
1767 Host::ThreadCancel(m_operation_thread, NULL);
1768 Host::ThreadJoin(m_operation_thread, &result, NULL);
1769 m_operation_thread = LLDB_INVALID_HOST_THREAD;
1770}