blob: 64e394e2e190edcef17bd8e8f466234a465d0eb2 [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"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/RegisterContext.h"
Ed Maste8702e922015-03-03 22:44:18 +000031#include "lldb/Target/UnixSignals.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000032#include "lldb/Utility/PseudoTerminal.h"
33
Chaoren Lin28e57422015-02-03 01:51:25 +000034#include "Plugins/Process/POSIX/CrashReason.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000035#include "POSIXThread.h"
36#include "ProcessFreeBSD.h"
37#include "ProcessPOSIXLog.h"
38#include "ProcessMonitor.h"
39
40extern "C" {
41 extern char ** environ;
42 }
43
44using namespace lldb;
45using namespace lldb_private;
46
47// We disable the tracing of ptrace calls for integration builds to
48// avoid the additional indirection and checks.
49#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
50// Wrapper for ptrace to catch errors and log calls.
51
52const char *
53Get_PT_IO_OP(int op)
54{
55 switch (op) {
56 case PIOD_READ_D: return "READ_D";
57 case PIOD_WRITE_D: return "WRITE_D";
58 case PIOD_READ_I: return "READ_I";
59 case PIOD_WRITE_I: return "WRITE_I";
60 default: return "Unknown op";
61 }
62}
63
Matt Kopec7de48462013-03-06 17:20:48 +000064// Wrapper for ptrace to catch errors and log calls.
65// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000066extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000067PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000068 const char* reqName, const char* file, int line)
69{
70 long int result;
71
Ashok Thirumurthi01186352013-03-28 16:02:31 +000072 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000073
74 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000075 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000076 reqName, pid, addr, data, file, line);
77 if (req == PT_IO) {
78 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
79
Sylvestre Ledru779f9212013-10-31 23:55:19 +000080 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000081 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000082 }
83 }
84
85 //PtraceDisplayBytes(req, data);
86
87 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000088 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000089
90 //PtraceDisplayBytes(req, data);
91
Matt Kopec7de48462013-03-06 17:20:48 +000092 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000093 {
94 const char* str;
95 switch (errno)
96 {
97 case ESRCH: str = "ESRCH"; break;
98 case EINVAL: str = "EINVAL"; break;
99 case EBUSY: str = "EBUSY"; break;
100 case EPERM: str = "EPERM"; break;
101 default: str = "<unknown>";
102 }
103 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
104 }
105
106 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000107#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000108 if (req == PT_GETREGS) {
109 struct reg *r = (struct reg *) addr;
110
111 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
112 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
113 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
114 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
115 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000116 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
117 struct dbreg *r = (struct dbreg *) addr;
118 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
119
120 for (int i = 0; i <= 7; i++)
121 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
122 }
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000123#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000124 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000125
126 return result;
127}
128
Matt Kopec7de48462013-03-06 17:20:48 +0000129// Wrapper for ptrace when logging is not required.
130// Sets errno to 0 prior to calling ptrace.
131extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000132PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000133{
134 long result = 0;
135 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000136 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000137 return result;
138}
139
Johnny Chen9ed5b492012-01-05 21:48:15 +0000140#define PTRACE(req, pid, addr, data) \
141 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
142#else
Matt Kopec7de48462013-03-06 17:20:48 +0000143 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000144#endif
145
146//------------------------------------------------------------------------------
147// Static implementations of ProcessMonitor::ReadMemory and
148// ProcessMonitor::WriteMemory. This enables mutual recursion between these
149// functions without needed to go thru the thread funnel.
150
151static size_t
152DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
153 Error &error)
154{
155 struct ptrace_io_desc pi_desc;
156
157 pi_desc.piod_op = PIOD_READ_D;
158 pi_desc.piod_offs = (void *)vm_addr;
159 pi_desc.piod_addr = buf;
160 pi_desc.piod_len = size;
161
162 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
163 error.SetErrorToErrno();
164 return pi_desc.piod_len;
165}
166
167static size_t
168DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
169 size_t size, Error &error)
170{
171 struct ptrace_io_desc pi_desc;
172
173 pi_desc.piod_op = PIOD_WRITE_D;
174 pi_desc.piod_offs = (void *)vm_addr;
175 pi_desc.piod_addr = (void *)buf;
176 pi_desc.piod_len = size;
177
178 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
179 error.SetErrorToErrno();
180 return pi_desc.piod_len;
181}
182
183// Simple helper function to ensure flags are enabled on the given file
184// descriptor.
185static bool
186EnsureFDFlags(int fd, int flags, Error &error)
187{
188 int status;
189
190 if ((status = fcntl(fd, F_GETFL)) == -1)
191 {
192 error.SetErrorToErrno();
193 return false;
194 }
195
196 if (fcntl(fd, F_SETFL, status | flags) == -1)
197 {
198 error.SetErrorToErrno();
199 return false;
200 }
201
202 return true;
203}
204
205//------------------------------------------------------------------------------
206/// @class Operation
207/// @brief Represents a ProcessMonitor operation.
208///
209/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
210/// one that spawned or attached to the process from the start. Therefore, when
211/// a ProcessMonitor is asked to deliver or change the state of an inferior
212/// process the operation must be "funneled" to a specific thread to perform the
213/// task. The Operation class provides an abstract base for all services the
214/// ProcessMonitor must perform via the single virtual function Execute, thus
215/// encapsulating the code that needs to run in the privileged context.
216class Operation
217{
218public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000219 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000220 virtual void Execute(ProcessMonitor *monitor) = 0;
221};
222
223//------------------------------------------------------------------------------
224/// @class ReadOperation
225/// @brief Implements ProcessMonitor::ReadMemory.
226class ReadOperation : public Operation
227{
228public:
229 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
230 Error &error, size_t &result)
231 : m_addr(addr), m_buff(buff), m_size(size),
232 m_error(error), m_result(result)
233 { }
234
235 void Execute(ProcessMonitor *monitor);
236
237private:
238 lldb::addr_t m_addr;
239 void *m_buff;
240 size_t m_size;
241 Error &m_error;
242 size_t &m_result;
243};
244
245void
246ReadOperation::Execute(ProcessMonitor *monitor)
247{
248 lldb::pid_t pid = monitor->GetPID();
249
250 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
251}
252
253//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000254/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000255/// @brief Implements ProcessMonitor::WriteMemory.
256class WriteOperation : public Operation
257{
258public:
259 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
260 Error &error, size_t &result)
261 : m_addr(addr), m_buff(buff), m_size(size),
262 m_error(error), m_result(result)
263 { }
264
265 void Execute(ProcessMonitor *monitor);
266
267private:
268 lldb::addr_t m_addr;
269 const void *m_buff;
270 size_t m_size;
271 Error &m_error;
272 size_t &m_result;
273};
274
275void
276WriteOperation::Execute(ProcessMonitor *monitor)
277{
278 lldb::pid_t pid = monitor->GetPID();
279
280 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
281}
282
283//------------------------------------------------------------------------------
284/// @class ReadRegOperation
285/// @brief Implements ProcessMonitor::ReadRegisterValue.
286class ReadRegOperation : public Operation
287{
288public:
Ed Maste6f066412013-07-04 21:47:32 +0000289 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
290 RegisterValue &value, bool &result)
291 : m_tid(tid), m_offset(offset), m_size(size),
292 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000293 { }
294
295 void Execute(ProcessMonitor *monitor);
296
297private:
Ed Maste6f066412013-07-04 21:47:32 +0000298 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000299 unsigned m_offset;
300 unsigned m_size;
301 RegisterValue &m_value;
302 bool &m_result;
303};
304
305void
306ReadRegOperation::Execute(ProcessMonitor *monitor)
307{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000308 struct reg regs;
309 int rc;
310
Ed Maste6f066412013-07-04 21:47:32 +0000311 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000312 m_result = false;
313 } else {
Justin Hibbits89e6f382014-11-12 15:14:08 +0000314 // 'struct reg' contains only 32- or 64-bit register values. Punt on
315 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
316 // processes on powerpc64 (probably the same for i386 on amd64)
317 if (m_size == sizeof(uint32_t))
318 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
319 else if (m_size == sizeof(uint64_t))
320 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
321 else
Johnny Chen9ed5b492012-01-05 21:48:15 +0000322 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
323 m_result = true;
324 }
325}
326
327//------------------------------------------------------------------------------
328/// @class WriteRegOperation
329/// @brief Implements ProcessMonitor::WriteRegisterValue.
330class WriteRegOperation : public Operation
331{
332public:
Ed Maste6f066412013-07-04 21:47:32 +0000333 WriteRegOperation(lldb::tid_t tid, unsigned offset,
334 const RegisterValue &value, bool &result)
335 : m_tid(tid), m_offset(offset),
336 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000337 { }
338
339 void Execute(ProcessMonitor *monitor);
340
341private:
Ed Maste6f066412013-07-04 21:47:32 +0000342 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000343 unsigned m_offset;
344 const RegisterValue &m_value;
345 bool &m_result;
346};
347
348void
349WriteRegOperation::Execute(ProcessMonitor *monitor)
350{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000351 struct reg regs;
352
Ed Maste6f066412013-07-04 21:47:32 +0000353 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000354 m_result = false;
355 return;
356 }
357 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000358 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000359 m_result = false;
360 else
361 m_result = true;
362}
363
364//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000365/// @class ReadDebugRegOperation
366/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
367class ReadDebugRegOperation : public Operation
368{
369public:
370 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
371 RegisterValue &value, bool &result)
372 : m_tid(tid), m_offset(offset), m_size(size),
373 m_value(value), m_result(result)
374 { }
375
376 void Execute(ProcessMonitor *monitor);
377
378private:
379 lldb::tid_t m_tid;
380 unsigned m_offset;
381 unsigned m_size;
382 RegisterValue &m_value;
383 bool &m_result;
384};
385
386void
387ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
388{
389 struct dbreg regs;
390 int rc;
391
392 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
393 m_result = false;
394 } else {
395 if (m_size == sizeof(uintptr_t))
396 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
397 else
398 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
399 m_result = true;
400 }
401}
402
403//------------------------------------------------------------------------------
404/// @class WriteDebugRegOperation
405/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
406class WriteDebugRegOperation : public Operation
407{
408public:
409 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
410 const RegisterValue &value, bool &result)
411 : m_tid(tid), m_offset(offset),
412 m_value(value), m_result(result)
413 { }
414
415 void Execute(ProcessMonitor *monitor);
416
417private:
418 lldb::tid_t m_tid;
419 unsigned m_offset;
420 const RegisterValue &m_value;
421 bool &m_result;
422};
423
424void
425WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
426{
427 struct dbreg regs;
428
429 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
430 m_result = false;
431 return;
432 }
433 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
434 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
435 m_result = false;
436 else
437 m_result = true;
438}
439
440//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000441/// @class ReadGPROperation
442/// @brief Implements ProcessMonitor::ReadGPR.
443class ReadGPROperation : public Operation
444{
445public:
Ed Maste6f066412013-07-04 21:47:32 +0000446 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
447 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000448 { }
449
450 void Execute(ProcessMonitor *monitor);
451
452private:
Ed Maste6f066412013-07-04 21:47:32 +0000453 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000454 void *m_buf;
455 bool &m_result;
456};
457
458void
459ReadGPROperation::Execute(ProcessMonitor *monitor)
460{
461 int rc;
462
463 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000464 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000465 if (errno != 0)
466 m_result = false;
467 else
468 m_result = true;
469}
470
471//------------------------------------------------------------------------------
472/// @class ReadFPROperation
473/// @brief Implements ProcessMonitor::ReadFPR.
474class ReadFPROperation : public Operation
475{
476public:
Ed Maste6f066412013-07-04 21:47:32 +0000477 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
478 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000479 { }
480
481 void Execute(ProcessMonitor *monitor);
482
483private:
Ed Maste6f066412013-07-04 21:47:32 +0000484 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000485 void *m_buf;
486 bool &m_result;
487};
488
489void
490ReadFPROperation::Execute(ProcessMonitor *monitor)
491{
Ed Maste6f066412013-07-04 21:47:32 +0000492 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000493 m_result = false;
494 else
495 m_result = true;
496}
497
498//------------------------------------------------------------------------------
499/// @class WriteGPROperation
500/// @brief Implements ProcessMonitor::WriteGPR.
501class WriteGPROperation : public Operation
502{
503public:
Ed Maste6f066412013-07-04 21:47:32 +0000504 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
505 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000506 { }
507
508 void Execute(ProcessMonitor *monitor);
509
510private:
Ed Maste6f066412013-07-04 21:47:32 +0000511 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000512 void *m_buf;
513 bool &m_result;
514};
515
516void
517WriteGPROperation::Execute(ProcessMonitor *monitor)
518{
Ed Maste6f066412013-07-04 21:47:32 +0000519 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000520 m_result = false;
521 else
522 m_result = true;
523}
524
525//------------------------------------------------------------------------------
526/// @class WriteFPROperation
527/// @brief Implements ProcessMonitor::WriteFPR.
528class WriteFPROperation : public Operation
529{
530public:
Ed Maste6f066412013-07-04 21:47:32 +0000531 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
532 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000533 { }
534
535 void Execute(ProcessMonitor *monitor);
536
537private:
Ed Maste6f066412013-07-04 21:47:32 +0000538 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000539 void *m_buf;
540 bool &m_result;
541};
542
543void
544WriteFPROperation::Execute(ProcessMonitor *monitor)
545{
Ed Maste6f066412013-07-04 21:47:32 +0000546 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000547 m_result = false;
548 else
549 m_result = true;
550}
551
552//------------------------------------------------------------------------------
553/// @class ResumeOperation
554/// @brief Implements ProcessMonitor::Resume.
555class ResumeOperation : public Operation
556{
557public:
Ed Maste502f9022013-11-25 16:31:23 +0000558 ResumeOperation(uint32_t signo, bool &result) :
559 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000560
561 void Execute(ProcessMonitor *monitor);
562
563private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000564 uint32_t m_signo;
565 bool &m_result;
566};
567
568void
569ResumeOperation::Execute(ProcessMonitor *monitor)
570{
Ed Maste502f9022013-11-25 16:31:23 +0000571 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000572 int data = 0;
573
574 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
575 data = m_signo;
576
Ed Maste502f9022013-11-25 16:31:23 +0000577 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000578 {
579 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
580
581 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000582 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000583 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000584 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000585 else
586 m_result = true;
587}
588
589//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000590/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000591/// @brief Implements ProcessMonitor::SingleStep.
592class SingleStepOperation : public Operation
593{
594public:
Ed Maste502f9022013-11-25 16:31:23 +0000595 SingleStepOperation(uint32_t signo, bool &result)
596 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000597
598 void Execute(ProcessMonitor *monitor);
599
600private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000601 uint32_t m_signo;
602 bool &m_result;
603};
604
605void
606SingleStepOperation::Execute(ProcessMonitor *monitor)
607{
Ed Maste502f9022013-11-25 16:31:23 +0000608 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000609 int data = 0;
610
611 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
612 data = m_signo;
613
Ed Maste502f9022013-11-25 16:31:23 +0000614 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000615 m_result = false;
616 else
617 m_result = true;
618}
619
620//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000621/// @class LwpInfoOperation
622/// @brief Implements ProcessMonitor::GetLwpInfo.
623class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000624{
625public:
Ed Maste819e3992013-07-17 14:02:20 +0000626 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000627 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000628
629 void Execute(ProcessMonitor *monitor);
630
631private:
632 lldb::tid_t m_tid;
633 void *m_info;
634 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000635 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000636};
637
638void
Ed Maste819e3992013-07-17 14:02:20 +0000639LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000640{
641 struct ptrace_lwpinfo plwp;
642
Daniel Maleaa35970a2012-11-23 18:09:58 +0000643 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000644 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000645 m_err = errno;
646 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000647 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000648 m_result = true;
649 }
650}
651
652//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000653/// @class ThreadSuspendOperation
654/// @brief Implements ProcessMonitor::ThreadSuspend.
655class ThreadSuspendOperation : public Operation
656{
657public:
658 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
659 : m_tid(tid), m_suspend(suspend), m_result(result) { }
660
661 void Execute(ProcessMonitor *monitor);
662
663private:
664 lldb::tid_t m_tid;
665 bool m_suspend;
666 bool &m_result;
667} ;
668
669void
670ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
671{
672 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
673}
674
675
676
677//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000678/// @class EventMessageOperation
679/// @brief Implements ProcessMonitor::GetEventMessage.
680class EventMessageOperation : public Operation
681{
682public:
683 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
684 : m_tid(tid), m_message(message), m_result(result) { }
685
686 void Execute(ProcessMonitor *monitor);
687
688private:
689 lldb::tid_t m_tid;
690 unsigned long *m_message;
691 bool &m_result;
692};
693
694void
695EventMessageOperation::Execute(ProcessMonitor *monitor)
696{
697 struct ptrace_lwpinfo plwp;
698
699 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
700 m_result = false;
701 else {
702 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000703 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000704 m_result = true;
705 } else
706 m_result = false;
707 }
708}
709
710//------------------------------------------------------------------------------
711/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000712/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000713class KillOperation : public Operation
714{
715public:
716 KillOperation(bool &result) : m_result(result) { }
717
718 void Execute(ProcessMonitor *monitor);
719
720private:
721 bool &m_result;
722};
723
724void
725KillOperation::Execute(ProcessMonitor *monitor)
726{
727 lldb::pid_t pid = monitor->GetPID();
728
729 if (PTRACE(PT_KILL, pid, NULL, 0))
730 m_result = false;
731 else
732 m_result = true;
733}
734
735//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000736/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000737/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000738class DetachOperation : public Operation
739{
740public:
741 DetachOperation(Error &result) : m_error(result) { }
742
743 void Execute(ProcessMonitor *monitor);
744
745private:
746 Error &m_error;
747};
748
749void
750DetachOperation::Execute(ProcessMonitor *monitor)
751{
752 lldb::pid_t pid = monitor->GetPID();
753
754 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
755 m_error.SetErrorToErrno();
756
757}
758
759ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
760 : m_monitor(monitor)
761{
762 sem_init(&m_semaphore, 0, 0);
763}
764
765ProcessMonitor::OperationArgs::~OperationArgs()
766{
767 sem_destroy(&m_semaphore);
768}
769
770ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
771 lldb_private::Module *module,
772 char const **argv,
773 char const **envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000774 const FileSpec &stdin_file_spec,
775 const FileSpec &stdout_file_spec,
776 const FileSpec &stderr_file_spec,
777 const FileSpec &working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000778 : OperationArgs(monitor),
779 m_module(module),
780 m_argv(argv),
781 m_envp(envp),
Ed Maste41fba2b2015-06-01 15:24:37 +0000782 m_stdin_file_spec(stdin_file_spec),
783 m_stdout_file_spec(stdout_file_spec),
784 m_stderr_file_spec(stderr_file_spec),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000785 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000786
787ProcessMonitor::LaunchArgs::~LaunchArgs()
788{ }
789
790ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
791 lldb::pid_t pid)
792 : OperationArgs(monitor), m_pid(pid) { }
793
794ProcessMonitor::AttachArgs::~AttachArgs()
795{ }
796
797//------------------------------------------------------------------------------
798/// The basic design of the ProcessMonitor is built around two threads.
799///
800/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
801/// for changes in the debugee state. When a change is detected a
802/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
803/// "drives" state changes in the debugger.
804///
805/// The second thread (@see OperationThread) is responsible for two things 1)
806/// launching or attaching to the inferior process, and then 2) servicing
807/// operations such as register reads/writes, stepping, etc. See the comments
808/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000809ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000810 Module *module,
811 const char *argv[],
812 const char *envp[],
Ed Maste41fba2b2015-06-01 15:24:37 +0000813 const FileSpec &stdin_file_spec,
814 const FileSpec &stdout_file_spec,
815 const FileSpec &stderr_file_spec,
816 const FileSpec &working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000817 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000818 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000819 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000820 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000822 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000823{
Ed Maste756e1ff2013-09-18 19:34:08 +0000824 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000825 stdin_file_spec,
826 stdout_file_spec,
827 stderr_file_spec,
828 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829
830
Ed Maste756e1ff2013-09-18 19:34:08 +0000831 sem_init(&m_operation_pending, 0, 0);
832 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000833
834 StartLaunchOpThread(args.get(), error);
835 if (!error.Success())
836 return;
837
838WAIT_AGAIN:
839 // Wait for the operation thread to initialize.
840 if (sem_wait(&args->m_semaphore))
841 {
842 if (errno == EINTR)
843 goto WAIT_AGAIN;
844 else
845 {
846 error.SetErrorToErrno();
847 return;
848 }
849 }
850
851 // Check that the launch was a success.
852 if (!args->m_error.Success())
853 {
Ed Mastea02f5532013-07-02 16:45:16 +0000854 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000855 error = args->m_error;
856 return;
857 }
858
859 // Finally, start monitoring the child process for change in state.
860 m_monitor_thread = Host::StartMonitoringChildProcess(
861 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000862 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000863 {
864 error.SetErrorToGenericError();
865 error.SetErrorString("Process launch failed.");
866 return;
867 }
868}
869
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000870ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000871 lldb::pid_t pid,
872 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000873 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000875 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000876 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000877{
Ed Maste756e1ff2013-09-18 19:34:08 +0000878 sem_init(&m_operation_pending, 0, 0);
879 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000880
Johnny Chen9ed5b492012-01-05 21:48:15 +0000881
Ed Maste756e1ff2013-09-18 19:34:08 +0000882 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000883
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
Ed Mastea02f5532013-07-02 16:45:16 +0000901 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000902 if (!args->m_error.Success())
903 {
Ed Mastea02f5532013-07-02 16:45:16 +0000904 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000905 error = args->m_error;
906 return;
907 }
908
909 // Finally, start monitoring the child process for change in state.
910 m_monitor_thread = Host::StartMonitoringChildProcess(
911 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000912 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000913 {
914 error.SetErrorToGenericError();
915 error.SetErrorString("Process attach failed.");
916 return;
917 }
918}
919
920ProcessMonitor::~ProcessMonitor()
921{
922 StopMonitor();
923}
924
925//------------------------------------------------------------------------------
926// Thread setup and tear down.
927void
928ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
929{
930 static const char *g_thread_name = "lldb.process.freebsd.operation";
931
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000932 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000933 return;
934
Zachary Turner39de3112014-09-09 20:54:56 +0000935 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000936}
937
Johnny Chen9ed5b492012-01-05 21:48:15 +0000938void *
939ProcessMonitor::LaunchOpThread(void *arg)
940{
941 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
942
943 if (!Launch(args)) {
944 sem_post(&args->m_semaphore);
945 return NULL;
946 }
947
948 ServeOperation(args);
949 return NULL;
950}
951
952bool
953ProcessMonitor::Launch(LaunchArgs *args)
954{
955 ProcessMonitor *monitor = args->m_monitor;
956 ProcessFreeBSD &process = monitor->GetProcess();
957 const char **argv = args->m_argv;
958 const char **envp = args->m_envp;
Ed Maste41fba2b2015-06-01 15:24:37 +0000959 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
960 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
961 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
962 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000963
964 lldb_utility::PseudoTerminal terminal;
965 const size_t err_len = 1024;
966 char err_str[err_len];
Davide Italianocd9f7b82015-03-22 23:43:58 +0000967 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968
Johnny Chen9ed5b492012-01-05 21:48:15 +0000969 // Propagate the environment if one is not supplied.
970 if (envp == NULL || envp[0] == NULL)
971 envp = const_cast<const char **>(environ);
972
Ed Mastea02f5532013-07-02 16:45:16 +0000973 if ((pid = terminal.Fork(err_str, err_len)) == -1)
974 {
975 args->m_error.SetErrorToGenericError();
976 args->m_error.SetErrorString("Process fork failed.");
977 goto FINISH;
978 }
979
Johnny Chen9ed5b492012-01-05 21:48:15 +0000980 // Recognized child exit status codes.
981 enum {
982 ePtraceFailed = 1,
983 eDupStdinFailed,
984 eDupStdoutFailed,
985 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000986 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000987 eExecFailed,
988 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000989 };
990
Johnny Chen9ed5b492012-01-05 21:48:15 +0000991 // Child process.
992 if (pid == 0)
993 {
994 // Trace this process.
995 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
996 exit(ePtraceFailed);
997
Ed Maste7f0230f2015-02-05 16:09:03 +0000998 // terminal has already dupped the tty descriptors to stdin/out/err.
999 // This closes original fd from which they were copied (and avoids
1000 // leaking descriptors to the debugged process.
1001 terminal.CloseSlaveFileDescriptor();
1002
Johnny Chen9ed5b492012-01-05 21:48:15 +00001003 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +00001004 if (setgid(getgid()) != 0)
1005 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001006
1007 // Let us have our own process group.
1008 setpgid(0, 0);
1009
1010 // Dup file descriptors if needed.
1011 //
1012 // FIXME: If two or more of the paths are the same we needlessly open
1013 // the same file multiple times.
Ed Maste41fba2b2015-06-01 15:24:37 +00001014 if (stdin_file_spec)
1015 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001016 exit(eDupStdinFailed);
1017
Ed Maste41fba2b2015-06-01 15:24:37 +00001018 if (stdout_file_spec)
1019 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001020 exit(eDupStdoutFailed);
1021
Ed Maste41fba2b2015-06-01 15:24:37 +00001022 if (stderr_file_spec)
1023 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001024 exit(eDupStderrFailed);
1025
Daniel Malea6217d2a2013-01-08 14:49:22 +00001026 // Change working directory
Ed Maste41fba2b2015-06-01 15:24:37 +00001027 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
1028 exit(eChdirFailed);
Daniel Malea6217d2a2013-01-08 14:49:22 +00001029
Johnny Chen9ed5b492012-01-05 21:48:15 +00001030 // Execute. We should never return.
1031 execve(argv[0],
1032 const_cast<char *const *>(argv),
1033 const_cast<char *const *>(envp));
1034 exit(eExecFailed);
1035 }
1036
1037 // Wait for the child process to to trap on its call to execve.
1038 ::pid_t wpid;
1039 int status;
1040 if ((wpid = waitpid(pid, &status, 0)) < 0)
1041 {
1042 args->m_error.SetErrorToErrno();
1043 goto FINISH;
1044 }
1045 else if (WIFEXITED(status))
1046 {
1047 // open, dup or execve likely failed for some reason.
1048 args->m_error.SetErrorToGenericError();
1049 switch (WEXITSTATUS(status))
1050 {
Ed Maste5d34af32013-06-24 15:09:18 +00001051 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001052 args->m_error.SetErrorString("Child ptrace failed.");
1053 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001054 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001055 args->m_error.SetErrorString("Child open stdin failed.");
1056 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001057 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001058 args->m_error.SetErrorString("Child open stdout failed.");
1059 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001060 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001061 args->m_error.SetErrorString("Child open stderr failed.");
1062 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001063 case eChdirFailed:
1064 args->m_error.SetErrorString("Child failed to set working directory.");
1065 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001066 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001067 args->m_error.SetErrorString("Child exec failed.");
1068 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001069 case eSetGidFailed:
1070 args->m_error.SetErrorString("Child setgid failed.");
1071 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001072 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001073 args->m_error.SetErrorString("Child returned unknown exit status.");
1074 break;
1075 }
1076 goto FINISH;
1077 }
Ed Maste82a00052013-11-25 21:15:53 +00001078 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001079 "Could not sync with inferior process.");
1080
1081#ifdef notyet
1082 // Have the child raise an event on exit. This is used to keep the child in
1083 // limbo until it is destroyed.
1084 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1085 {
1086 args->m_error.SetErrorToErrno();
1087 goto FINISH;
1088 }
1089#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001090 // Release the master terminal descriptor and pass it off to the
1091 // ProcessMonitor instance. Similarly stash the inferior pid.
1092 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001093 monitor->m_pid = pid;
1094
1095 // Set the terminal fd to be in non blocking mode (it simplifies the
1096 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1097 // descriptor to read from).
1098 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1099 goto FINISH;
1100
Ed Mastee5441432013-09-03 23:55:30 +00001101 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001102
1103FINISH:
1104 return args->m_error.Success();
1105}
1106
Johnny Chen9ed5b492012-01-05 21:48:15 +00001107void
1108ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1109{
1110 static const char *g_thread_name = "lldb.process.freebsd.operation";
1111
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001112 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001113 return;
1114
Zachary Turner39de3112014-09-09 20:54:56 +00001115 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001116}
1117
Johnny Chen9ed5b492012-01-05 21:48:15 +00001118void *
1119ProcessMonitor::AttachOpThread(void *arg)
1120{
1121 AttachArgs *args = static_cast<AttachArgs*>(arg);
1122
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001123 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001124
1125 ServeOperation(args);
1126 return NULL;
1127}
1128
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001129void
Johnny Chen9ed5b492012-01-05 21:48:15 +00001130ProcessMonitor::Attach(AttachArgs *args)
1131{
1132 lldb::pid_t pid = args->m_pid;
1133
1134 ProcessMonitor *monitor = args->m_monitor;
1135 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001136
1137 if (pid <= 1)
1138 {
1139 args->m_error.SetErrorToGenericError();
1140 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001141 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001142 }
1143
1144 // Attach to the requested process.
1145 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1146 {
1147 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001148 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001149 }
1150
1151 int status;
1152 if ((status = waitpid(pid, NULL, 0)) < 0)
1153 {
1154 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001155 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001156 }
1157
Ed Mastee5441432013-09-03 23:55:30 +00001158 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001159}
1160
Ed Maste7fd845c2013-12-09 15:51:17 +00001161size_t
1162ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1163{
1164 lwpid_t *tids;
1165 int tdcnt;
1166
1167 thread_ids.clear();
1168
1169 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1170 if (tdcnt <= 0)
1171 return 0;
1172 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1173 if (tids == NULL)
1174 return 0;
1175 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1176 free(tids);
1177 return 0;
1178 }
1179 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1180 free(tids);
1181 return thread_ids.size();
1182}
1183
Johnny Chen9ed5b492012-01-05 21:48:15 +00001184bool
1185ProcessMonitor::MonitorCallback(void *callback_baton,
1186 lldb::pid_t pid,
1187 bool exited,
1188 int signal,
1189 int status)
1190{
1191 ProcessMessage message;
1192 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001193 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001194 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001195 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001196 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001197 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001198
Ed Mastea02f5532013-07-02 16:45:16 +00001199 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1200
1201 if (exited)
1202 {
1203 if (log)
1204 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1205 message = ProcessMessage::Exit(pid, status);
1206 process->SendMessage(message);
1207 return pid == process->GetID();
1208 }
1209
Ed Maste819e3992013-07-17 14:02:20 +00001210 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001211 stop_monitoring = true; // pid is gone. Bail.
1212 else {
Ed Maste819e3992013-07-17 14:02:20 +00001213 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001214 {
1215 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001216 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001217 break;
1218
1219 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001220 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001221 break;
1222 }
1223
1224 process->SendMessage(message);
1225 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1226 }
1227
1228 return stop_monitoring;
1229}
1230
1231ProcessMessage
1232ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001233 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001234{
1235 ProcessMessage message;
1236
Ed Mastea02f5532013-07-02 16:45:16 +00001237 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1238
Matt Kopec7de48462013-03-06 17:20:48 +00001239 assert(monitor);
1240 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001241
1242 switch (info->si_code)
1243 {
1244 default:
1245 assert(false && "Unexpected SIGTRAP code!");
1246 break;
1247
1248 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1249 {
1250 // The inferior process is about to exit. Maintain the process in a
1251 // state of "limbo" until we are explicitly commanded to detach,
1252 // destroy, resume, etc.
1253 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001254 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001255 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001256 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001257 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1258 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001259 break;
1260 }
1261
1262 case 0:
1263 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001264 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001265 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001266 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001267 break;
1268
1269 case SI_KERNEL:
1270 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001271 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001272 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1273 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001274 break;
1275 }
1276
1277 return message;
1278}
1279
1280ProcessMessage
1281ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001282 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001283{
1284 ProcessMessage message;
1285 int signo = info->si_signo;
1286
Ed Mastea02f5532013-07-02 16:45:16 +00001287 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1288
Johnny Chen9ed5b492012-01-05 21:48:15 +00001289 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1290 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1291 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1292 //
1293 // IOW, user generated signals never generate what we consider to be a
1294 // "crash".
1295 //
1296 // Similarly, ACK signals generated by this monitor.
1297 if (info->si_code == SI_USER)
1298 {
Ed Mastea02f5532013-07-02 16:45:16 +00001299 if (log)
1300 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1301 __FUNCTION__,
1302 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1303 "SI_USER",
1304 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001305 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001306 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001307 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001308 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001309 }
1310
Ed Mastea02f5532013-07-02 16:45:16 +00001311 if (log)
1312 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1313
Chaoren Lin28e57422015-02-03 01:51:25 +00001314 switch (signo)
1315 {
1316 case SIGSEGV:
1317 case SIGILL:
1318 case SIGFPE:
1319 case SIGBUS:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001320 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001321 const auto reason = GetCrashReason(*info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001322 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001323 }
1324
1325 // Everything else is "normal" and does not require any special action on
1326 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001327 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001328}
1329
Johnny Chen9ed5b492012-01-05 21:48:15 +00001330void
1331ProcessMonitor::ServeOperation(OperationArgs *args)
1332{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001333 ProcessMonitor *monitor = args->m_monitor;
1334
Johnny Chen9ed5b492012-01-05 21:48:15 +00001335 // We are finised with the arguments and are ready to go. Sync with the
1336 // parent thread and start serving operations on the inferior.
1337 sem_post(&args->m_semaphore);
1338
1339 for (;;)
1340 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001341 // wait for next pending operation
1342 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001343
Ed Maste756e1ff2013-09-18 19:34:08 +00001344 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001345
Ed Maste756e1ff2013-09-18 19:34:08 +00001346 // notify calling thread that operation is complete
1347 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001348 }
1349}
1350
1351void
1352ProcessMonitor::DoOperation(Operation *op)
1353{
Ed Maste756e1ff2013-09-18 19:34:08 +00001354 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001355
Ed Maste756e1ff2013-09-18 19:34:08 +00001356 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001357
Ed Maste756e1ff2013-09-18 19:34:08 +00001358 // notify operation thread that an operation is ready to be processed
1359 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001360
Ed Maste756e1ff2013-09-18 19:34:08 +00001361 // wait for operation to complete
1362 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001363}
1364
1365size_t
1366ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1367 Error &error)
1368{
1369 size_t result;
1370 ReadOperation op(vm_addr, buf, size, error, result);
1371 DoOperation(&op);
1372 return result;
1373}
1374
1375size_t
1376ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1377 lldb_private::Error &error)
1378{
1379 size_t result;
1380 WriteOperation op(vm_addr, buf, size, error, result);
1381 DoOperation(&op);
1382 return result;
1383}
1384
1385bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001386ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001387 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001388{
1389 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001390 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001391 DoOperation(&op);
1392 return result;
1393}
1394
1395bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001396ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001397 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001398{
1399 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001400 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001401 DoOperation(&op);
1402 return result;
1403}
1404
1405bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001406ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1407 const char *reg_name, unsigned size,
1408 lldb_private::RegisterValue &value)
1409{
1410 bool result;
1411 ReadDebugRegOperation op(tid, offset, size, value, result);
1412 DoOperation(&op);
1413 return result;
1414}
1415
1416bool
1417ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1418 const char *reg_name,
1419 const lldb_private::RegisterValue &value)
1420{
1421 bool result;
1422 WriteDebugRegOperation op(tid, offset, value, result);
1423 DoOperation(&op);
1424 return result;
1425}
1426
1427bool
Matt Kopec7de48462013-03-06 17:20:48 +00001428ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001429{
1430 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001431 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001432 DoOperation(&op);
1433 return result;
1434}
1435
1436bool
Matt Kopec7de48462013-03-06 17:20:48 +00001437ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001438{
1439 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001440 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001441 DoOperation(&op);
1442 return result;
1443}
1444
1445bool
Ed Maste5d34af32013-06-24 15:09:18 +00001446ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1447{
1448 return false;
1449}
1450
1451bool
Matt Kopec7de48462013-03-06 17:20:48 +00001452ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001453{
1454 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001455 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001456 DoOperation(&op);
1457 return result;
1458}
1459
1460bool
Matt Kopec7de48462013-03-06 17:20:48 +00001461ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001462{
1463 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001464 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001465 DoOperation(&op);
1466 return result;
1467}
1468
1469bool
Ed Maste5d34af32013-06-24 15:09:18 +00001470ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1471{
1472 return false;
1473}
1474
1475bool
Ed Maste68f51792013-10-18 19:16:44 +00001476ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1477{
1478 return false;
1479}
1480
1481bool
Ed Maste502f9022013-11-25 16:31:23 +00001482ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001483{
1484 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001485 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1486
Ed Maste4aeb3c02014-05-28 14:11:20 +00001487 if (log) {
1488 const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo);
1489 if (signame == nullptr)
1490 signame = "<none>";
1491 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1492 __FUNCTION__, GetPID(), signame);
1493 }
Ed Maste502f9022013-11-25 16:31:23 +00001494 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001495 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001496 if (log)
1497 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001498 return result;
1499}
1500
1501bool
Ed Maste502f9022013-11-25 16:31:23 +00001502ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001503{
1504 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001505 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001506 DoOperation(&op);
1507 return result;
1508}
1509
1510bool
Ed Maste70882932014-04-01 14:30:56 +00001511ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001512{
1513 bool result;
1514 KillOperation op(result);
1515 DoOperation(&op);
1516 return result;
1517}
1518
1519bool
Ed Maste819e3992013-07-17 14:02:20 +00001520ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001521{
1522 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001523 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001524 DoOperation(&op);
1525 return result;
1526}
1527
1528bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001529ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1530{
1531 bool result;
1532 ThreadSuspendOperation op(tid, suspend, result);
1533 DoOperation(&op);
1534 return result;
1535}
1536
1537bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001538ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1539{
1540 bool result;
1541 EventMessageOperation op(tid, message, result);
1542 DoOperation(&op);
1543 return result;
1544}
1545
Ed Mastea02f5532013-07-02 16:45:16 +00001546lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001547ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001548{
Ed Mastea02f5532013-07-02 16:45:16 +00001549 lldb_private::Error error;
1550 if (tid != LLDB_INVALID_THREAD_ID)
1551 {
1552 DetachOperation op(error);
1553 DoOperation(&op);
1554 }
1555 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001556}
1557
1558bool
Ed Maste41fba2b2015-06-01 15:24:37 +00001559ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd, int flags)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001560{
Ed Maste41fba2b2015-06-01 15:24:37 +00001561 int target_fd = open(file_spec.GetCString(), flags, 0666);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001562
1563 if (target_fd == -1)
1564 return false;
1565
Ed Maste7f0230f2015-02-05 16:09:03 +00001566 if (dup2(target_fd, fd) == -1)
1567 return false;
1568
1569 return (close(target_fd) == -1) ? false : true;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001570}
1571
1572void
1573ProcessMonitor::StopMonitoringChildProcess()
1574{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001575 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001576 {
Zachary Turner39de3112014-09-09 20:54:56 +00001577 m_monitor_thread.Cancel();
1578 m_monitor_thread.Join(nullptr);
1579 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001580 }
1581}
1582
1583void
1584ProcessMonitor::StopMonitor()
1585{
1586 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001587 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001588 sem_destroy(&m_operation_pending);
1589 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00001590 if (m_terminal_fd >= 0) {
1591 close(m_terminal_fd);
1592 m_terminal_fd = -1;
1593 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001594}
1595
Andrew Kaylord4d54992013-09-17 00:30:24 +00001596// FIXME: On Linux, when a new thread is created, we receive to notifications,
1597// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1598// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1599// the new child thread indicating that it has is stopped because we attached.
1600// We have no guarantee of the order in which these arrive, but we need both
1601// before we are ready to proceed. We currently keep a list of threads which
1602// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1603// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1604// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1605//
1606// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1607// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1608// logically needed.
1609//
1610// We really should figure out what actually happens on FreeBSD and move the
1611// Linux-specific logic out of ProcessPOSIX as needed.
1612
1613bool
1614ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1615{
1616 return true;
1617}
1618
Johnny Chen9ed5b492012-01-05 21:48:15 +00001619void
Ed Mastea02f5532013-07-02 16:45:16 +00001620ProcessMonitor::StopOpThread()
1621{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001622 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001623 return;
1624
Zachary Turner39de3112014-09-09 20:54:56 +00001625 m_operation_thread.Cancel();
1626 m_operation_thread.Join(nullptr);
1627 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001628}