blob: ba0bbfd3c6fa6ae44afd8b4e44aed054e33230c4 [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 Maste7d4c0d5b2013-07-22 20:51:08 +0000116#endif
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000117#ifndef __powerpc__
Ed Mastea4be2c52014-02-19 18:34:06 +0000118 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
119 struct dbreg *r = (struct dbreg *) addr;
120 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
121
122 for (int i = 0; i <= 7; i++)
123 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
124 }
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000125#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000126 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000127
128 return result;
129}
130
Matt Kopec7de48462013-03-06 17:20:48 +0000131// Wrapper for ptrace when logging is not required.
132// Sets errno to 0 prior to calling ptrace.
133extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000134PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000135{
136 long result = 0;
137 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000138 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000139 return result;
140}
141
Johnny Chen9ed5b492012-01-05 21:48:15 +0000142#define PTRACE(req, pid, addr, data) \
143 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
144#else
Matt Kopec7de48462013-03-06 17:20:48 +0000145 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000146#endif
147
148//------------------------------------------------------------------------------
149// Static implementations of ProcessMonitor::ReadMemory and
150// ProcessMonitor::WriteMemory. This enables mutual recursion between these
151// functions without needed to go thru the thread funnel.
152
153static size_t
154DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
155 Error &error)
156{
157 struct ptrace_io_desc pi_desc;
158
159 pi_desc.piod_op = PIOD_READ_D;
160 pi_desc.piod_offs = (void *)vm_addr;
161 pi_desc.piod_addr = buf;
162 pi_desc.piod_len = size;
163
164 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
165 error.SetErrorToErrno();
166 return pi_desc.piod_len;
167}
168
169static size_t
170DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
171 size_t size, Error &error)
172{
173 struct ptrace_io_desc pi_desc;
174
175 pi_desc.piod_op = PIOD_WRITE_D;
176 pi_desc.piod_offs = (void *)vm_addr;
177 pi_desc.piod_addr = (void *)buf;
178 pi_desc.piod_len = size;
179
180 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
181 error.SetErrorToErrno();
182 return pi_desc.piod_len;
183}
184
185// Simple helper function to ensure flags are enabled on the given file
186// descriptor.
187static bool
188EnsureFDFlags(int fd, int flags, Error &error)
189{
190 int status;
191
192 if ((status = fcntl(fd, F_GETFL)) == -1)
193 {
194 error.SetErrorToErrno();
195 return false;
196 }
197
198 if (fcntl(fd, F_SETFL, status | flags) == -1)
199 {
200 error.SetErrorToErrno();
201 return false;
202 }
203
204 return true;
205}
206
207//------------------------------------------------------------------------------
208/// @class Operation
209/// @brief Represents a ProcessMonitor operation.
210///
211/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
212/// one that spawned or attached to the process from the start. Therefore, when
213/// a ProcessMonitor is asked to deliver or change the state of an inferior
214/// process the operation must be "funneled" to a specific thread to perform the
215/// task. The Operation class provides an abstract base for all services the
216/// ProcessMonitor must perform via the single virtual function Execute, thus
217/// encapsulating the code that needs to run in the privileged context.
218class Operation
219{
220public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000221 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000222 virtual void Execute(ProcessMonitor *monitor) = 0;
223};
224
225//------------------------------------------------------------------------------
226/// @class ReadOperation
227/// @brief Implements ProcessMonitor::ReadMemory.
228class ReadOperation : public Operation
229{
230public:
231 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
232 Error &error, size_t &result)
233 : m_addr(addr), m_buff(buff), m_size(size),
234 m_error(error), m_result(result)
235 { }
236
237 void Execute(ProcessMonitor *monitor);
238
239private:
240 lldb::addr_t m_addr;
241 void *m_buff;
242 size_t m_size;
243 Error &m_error;
244 size_t &m_result;
245};
246
247void
248ReadOperation::Execute(ProcessMonitor *monitor)
249{
250 lldb::pid_t pid = monitor->GetPID();
251
252 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
253}
254
255//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000256/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000257/// @brief Implements ProcessMonitor::WriteMemory.
258class WriteOperation : public Operation
259{
260public:
261 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
262 Error &error, size_t &result)
263 : m_addr(addr), m_buff(buff), m_size(size),
264 m_error(error), m_result(result)
265 { }
266
267 void Execute(ProcessMonitor *monitor);
268
269private:
270 lldb::addr_t m_addr;
271 const void *m_buff;
272 size_t m_size;
273 Error &m_error;
274 size_t &m_result;
275};
276
277void
278WriteOperation::Execute(ProcessMonitor *monitor)
279{
280 lldb::pid_t pid = monitor->GetPID();
281
282 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
283}
284
285//------------------------------------------------------------------------------
286/// @class ReadRegOperation
287/// @brief Implements ProcessMonitor::ReadRegisterValue.
288class ReadRegOperation : public Operation
289{
290public:
Ed Maste6f066412013-07-04 21:47:32 +0000291 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
292 RegisterValue &value, bool &result)
293 : m_tid(tid), m_offset(offset), m_size(size),
294 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000295 { }
296
297 void Execute(ProcessMonitor *monitor);
298
299private:
Ed Maste6f066412013-07-04 21:47:32 +0000300 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000301 unsigned m_offset;
302 unsigned m_size;
303 RegisterValue &m_value;
304 bool &m_result;
305};
306
307void
308ReadRegOperation::Execute(ProcessMonitor *monitor)
309{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000310 struct reg regs;
311 int rc;
312
Ed Maste6f066412013-07-04 21:47:32 +0000313 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000314 m_result = false;
315 } else {
Justin Hibbits89e6f382014-11-12 15:14:08 +0000316 // 'struct reg' contains only 32- or 64-bit register values. Punt on
317 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
318 // processes on powerpc64 (probably the same for i386 on amd64)
319 if (m_size == sizeof(uint32_t))
320 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
321 else if (m_size == sizeof(uint64_t))
322 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
323 else
Johnny Chen9ed5b492012-01-05 21:48:15 +0000324 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
325 m_result = true;
326 }
327}
328
329//------------------------------------------------------------------------------
330/// @class WriteRegOperation
331/// @brief Implements ProcessMonitor::WriteRegisterValue.
332class WriteRegOperation : public Operation
333{
334public:
Ed Maste6f066412013-07-04 21:47:32 +0000335 WriteRegOperation(lldb::tid_t tid, unsigned offset,
336 const RegisterValue &value, bool &result)
337 : m_tid(tid), m_offset(offset),
338 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000339 { }
340
341 void Execute(ProcessMonitor *monitor);
342
343private:
Ed Maste6f066412013-07-04 21:47:32 +0000344 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000345 unsigned m_offset;
346 const RegisterValue &m_value;
347 bool &m_result;
348};
349
350void
351WriteRegOperation::Execute(ProcessMonitor *monitor)
352{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000353 struct reg regs;
354
Ed Maste6f066412013-07-04 21:47:32 +0000355 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000356 m_result = false;
357 return;
358 }
359 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000360 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000361 m_result = false;
362 else
363 m_result = true;
364}
365
366//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000367/// @class ReadDebugRegOperation
368/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
369class ReadDebugRegOperation : public Operation
370{
371public:
372 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
373 RegisterValue &value, bool &result)
374 : m_tid(tid), m_offset(offset), m_size(size),
375 m_value(value), m_result(result)
376 { }
377
378 void Execute(ProcessMonitor *monitor);
379
380private:
381 lldb::tid_t m_tid;
382 unsigned m_offset;
383 unsigned m_size;
384 RegisterValue &m_value;
385 bool &m_result;
386};
387
388void
389ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
390{
391 struct dbreg regs;
392 int rc;
393
394 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
395 m_result = false;
396 } else {
397 if (m_size == sizeof(uintptr_t))
398 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
399 else
400 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
401 m_result = true;
402 }
403}
404
405//------------------------------------------------------------------------------
406/// @class WriteDebugRegOperation
407/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
408class WriteDebugRegOperation : public Operation
409{
410public:
411 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
412 const RegisterValue &value, bool &result)
413 : m_tid(tid), m_offset(offset),
414 m_value(value), m_result(result)
415 { }
416
417 void Execute(ProcessMonitor *monitor);
418
419private:
420 lldb::tid_t m_tid;
421 unsigned m_offset;
422 const RegisterValue &m_value;
423 bool &m_result;
424};
425
426void
427WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
428{
429 struct dbreg regs;
430
431 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
432 m_result = false;
433 return;
434 }
435 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
436 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
437 m_result = false;
438 else
439 m_result = true;
440}
441
442//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000443/// @class ReadGPROperation
444/// @brief Implements ProcessMonitor::ReadGPR.
445class ReadGPROperation : public Operation
446{
447public:
Ed Maste6f066412013-07-04 21:47:32 +0000448 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
449 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000450 { }
451
452 void Execute(ProcessMonitor *monitor);
453
454private:
Ed Maste6f066412013-07-04 21:47:32 +0000455 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000456 void *m_buf;
457 bool &m_result;
458};
459
460void
461ReadGPROperation::Execute(ProcessMonitor *monitor)
462{
463 int rc;
464
465 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000466 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000467 if (errno != 0)
468 m_result = false;
469 else
470 m_result = true;
471}
472
473//------------------------------------------------------------------------------
474/// @class ReadFPROperation
475/// @brief Implements ProcessMonitor::ReadFPR.
476class ReadFPROperation : public Operation
477{
478public:
Ed Maste6f066412013-07-04 21:47:32 +0000479 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
480 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000481 { }
482
483 void Execute(ProcessMonitor *monitor);
484
485private:
Ed Maste6f066412013-07-04 21:47:32 +0000486 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000487 void *m_buf;
488 bool &m_result;
489};
490
491void
492ReadFPROperation::Execute(ProcessMonitor *monitor)
493{
Ed Maste6f066412013-07-04 21:47:32 +0000494 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000495 m_result = false;
496 else
497 m_result = true;
498}
499
500//------------------------------------------------------------------------------
501/// @class WriteGPROperation
502/// @brief Implements ProcessMonitor::WriteGPR.
503class WriteGPROperation : public Operation
504{
505public:
Ed Maste6f066412013-07-04 21:47:32 +0000506 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
507 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000508 { }
509
510 void Execute(ProcessMonitor *monitor);
511
512private:
Ed Maste6f066412013-07-04 21:47:32 +0000513 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000514 void *m_buf;
515 bool &m_result;
516};
517
518void
519WriteGPROperation::Execute(ProcessMonitor *monitor)
520{
Ed Maste6f066412013-07-04 21:47:32 +0000521 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000522 m_result = false;
523 else
524 m_result = true;
525}
526
527//------------------------------------------------------------------------------
528/// @class WriteFPROperation
529/// @brief Implements ProcessMonitor::WriteFPR.
530class WriteFPROperation : public Operation
531{
532public:
Ed Maste6f066412013-07-04 21:47:32 +0000533 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
534 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000535 { }
536
537 void Execute(ProcessMonitor *monitor);
538
539private:
Ed Maste6f066412013-07-04 21:47:32 +0000540 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000541 void *m_buf;
542 bool &m_result;
543};
544
545void
546WriteFPROperation::Execute(ProcessMonitor *monitor)
547{
Ed Maste6f066412013-07-04 21:47:32 +0000548 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000549 m_result = false;
550 else
551 m_result = true;
552}
553
554//------------------------------------------------------------------------------
555/// @class ResumeOperation
556/// @brief Implements ProcessMonitor::Resume.
557class ResumeOperation : public Operation
558{
559public:
Ed Maste502f9022013-11-25 16:31:23 +0000560 ResumeOperation(uint32_t signo, bool &result) :
561 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000562
563 void Execute(ProcessMonitor *monitor);
564
565private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000566 uint32_t m_signo;
567 bool &m_result;
568};
569
570void
571ResumeOperation::Execute(ProcessMonitor *monitor)
572{
Ed Maste502f9022013-11-25 16:31:23 +0000573 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000574 int data = 0;
575
576 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
577 data = m_signo;
578
Ed Maste502f9022013-11-25 16:31:23 +0000579 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000580 {
581 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
582
583 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000584 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000585 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000586 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000587 else
588 m_result = true;
589}
590
591//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000592/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000593/// @brief Implements ProcessMonitor::SingleStep.
594class SingleStepOperation : public Operation
595{
596public:
Ed Maste502f9022013-11-25 16:31:23 +0000597 SingleStepOperation(uint32_t signo, bool &result)
598 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000599
600 void Execute(ProcessMonitor *monitor);
601
602private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000603 uint32_t m_signo;
604 bool &m_result;
605};
606
607void
608SingleStepOperation::Execute(ProcessMonitor *monitor)
609{
Ed Maste502f9022013-11-25 16:31:23 +0000610 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000611 int data = 0;
612
613 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
614 data = m_signo;
615
Ed Maste502f9022013-11-25 16:31:23 +0000616 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000617 m_result = false;
618 else
619 m_result = true;
620}
621
622//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000623/// @class LwpInfoOperation
624/// @brief Implements ProcessMonitor::GetLwpInfo.
625class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000626{
627public:
Ed Maste819e3992013-07-17 14:02:20 +0000628 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000629 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000630
631 void Execute(ProcessMonitor *monitor);
632
633private:
634 lldb::tid_t m_tid;
635 void *m_info;
636 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000637 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000638};
639
640void
Ed Maste819e3992013-07-17 14:02:20 +0000641LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000642{
643 struct ptrace_lwpinfo plwp;
644
Daniel Maleaa35970a2012-11-23 18:09:58 +0000645 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000646 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000647 m_err = errno;
648 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000649 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000650 m_result = true;
651 }
652}
653
654//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000655/// @class ThreadSuspendOperation
656/// @brief Implements ProcessMonitor::ThreadSuspend.
657class ThreadSuspendOperation : public Operation
658{
659public:
660 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
661 : m_tid(tid), m_suspend(suspend), m_result(result) { }
662
663 void Execute(ProcessMonitor *monitor);
664
665private:
666 lldb::tid_t m_tid;
667 bool m_suspend;
668 bool &m_result;
669} ;
670
671void
672ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
673{
674 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
675}
676
677
678
679//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000680/// @class EventMessageOperation
681/// @brief Implements ProcessMonitor::GetEventMessage.
682class EventMessageOperation : public Operation
683{
684public:
685 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
686 : m_tid(tid), m_message(message), m_result(result) { }
687
688 void Execute(ProcessMonitor *monitor);
689
690private:
691 lldb::tid_t m_tid;
692 unsigned long *m_message;
693 bool &m_result;
694};
695
696void
697EventMessageOperation::Execute(ProcessMonitor *monitor)
698{
699 struct ptrace_lwpinfo plwp;
700
701 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
702 m_result = false;
703 else {
704 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000705 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000706 m_result = true;
707 } else
708 m_result = false;
709 }
710}
711
712//------------------------------------------------------------------------------
713/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000714/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000715class KillOperation : public Operation
716{
717public:
718 KillOperation(bool &result) : m_result(result) { }
719
720 void Execute(ProcessMonitor *monitor);
721
722private:
723 bool &m_result;
724};
725
726void
727KillOperation::Execute(ProcessMonitor *monitor)
728{
729 lldb::pid_t pid = monitor->GetPID();
730
731 if (PTRACE(PT_KILL, pid, NULL, 0))
732 m_result = false;
733 else
734 m_result = true;
735}
736
737//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000738/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000739/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000740class DetachOperation : public Operation
741{
742public:
743 DetachOperation(Error &result) : m_error(result) { }
744
745 void Execute(ProcessMonitor *monitor);
746
747private:
748 Error &m_error;
749};
750
751void
752DetachOperation::Execute(ProcessMonitor *monitor)
753{
754 lldb::pid_t pid = monitor->GetPID();
755
756 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
757 m_error.SetErrorToErrno();
758
759}
760
761ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
762 : m_monitor(monitor)
763{
764 sem_init(&m_semaphore, 0, 0);
765}
766
767ProcessMonitor::OperationArgs::~OperationArgs()
768{
769 sem_destroy(&m_semaphore);
770}
771
772ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
773 lldb_private::Module *module,
774 char const **argv,
775 char const **envp,
776 const char *stdin_path,
777 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000778 const char *stderr_path,
779 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000780 : OperationArgs(monitor),
781 m_module(module),
782 m_argv(argv),
783 m_envp(envp),
784 m_stdin_path(stdin_path),
785 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000786 m_stderr_path(stderr_path),
787 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000788
789ProcessMonitor::LaunchArgs::~LaunchArgs()
790{ }
791
792ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
793 lldb::pid_t pid)
794 : OperationArgs(monitor), m_pid(pid) { }
795
796ProcessMonitor::AttachArgs::~AttachArgs()
797{ }
798
799//------------------------------------------------------------------------------
800/// The basic design of the ProcessMonitor is built around two threads.
801///
802/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
803/// for changes in the debugee state. When a change is detected a
804/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
805/// "drives" state changes in the debugger.
806///
807/// The second thread (@see OperationThread) is responsible for two things 1)
808/// launching or attaching to the inferior process, and then 2) servicing
809/// operations such as register reads/writes, stepping, etc. See the comments
810/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000811ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000812 Module *module,
813 const char *argv[],
814 const char *envp[],
815 const char *stdin_path,
816 const char *stdout_path,
817 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000818 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000819 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000820 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000821 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000823 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000824 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000825{
Ed Maste756e1ff2013-09-18 19:34:08 +0000826 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
827 stdin_path, stdout_path, stderr_path,
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;
959 const char *stdin_path = args->m_stdin_path;
960 const char *stdout_path = args->m_stdout_path;
961 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000962 const char *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];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000967 lldb::pid_t pid;
968
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.
1014 if (stdin_path != NULL && stdin_path[0])
1015 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1016 exit(eDupStdinFailed);
1017
1018 if (stdout_path != NULL && stdout_path[0])
1019 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1020 exit(eDupStdoutFailed);
1021
1022 if (stderr_path != NULL && stderr_path[0])
1023 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1024 exit(eDupStderrFailed);
1025
Daniel Malea6217d2a2013-01-08 14:49:22 +00001026 // Change working directory
1027 if (working_dir != NULL && working_dir[0])
1028 if (0 != ::chdir(working_dir))
1029 exit(eChdirFailed);
1030
Johnny Chen9ed5b492012-01-05 21:48:15 +00001031 // Execute. We should never return.
1032 execve(argv[0],
1033 const_cast<char *const *>(argv),
1034 const_cast<char *const *>(envp));
1035 exit(eExecFailed);
1036 }
1037
1038 // Wait for the child process to to trap on its call to execve.
1039 ::pid_t wpid;
1040 int status;
1041 if ((wpid = waitpid(pid, &status, 0)) < 0)
1042 {
1043 args->m_error.SetErrorToErrno();
1044 goto FINISH;
1045 }
1046 else if (WIFEXITED(status))
1047 {
1048 // open, dup or execve likely failed for some reason.
1049 args->m_error.SetErrorToGenericError();
1050 switch (WEXITSTATUS(status))
1051 {
Ed Maste5d34af32013-06-24 15:09:18 +00001052 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001053 args->m_error.SetErrorString("Child ptrace failed.");
1054 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001055 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001056 args->m_error.SetErrorString("Child open stdin failed.");
1057 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001058 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001059 args->m_error.SetErrorString("Child open stdout failed.");
1060 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001061 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001062 args->m_error.SetErrorString("Child open stderr failed.");
1063 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001064 case eChdirFailed:
1065 args->m_error.SetErrorString("Child failed to set working directory.");
1066 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001067 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001068 args->m_error.SetErrorString("Child exec failed.");
1069 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001070 case eSetGidFailed:
1071 args->m_error.SetErrorString("Child setgid failed.");
1072 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001073 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001074 args->m_error.SetErrorString("Child returned unknown exit status.");
1075 break;
1076 }
1077 goto FINISH;
1078 }
Ed Maste82a00052013-11-25 21:15:53 +00001079 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001080 "Could not sync with inferior process.");
1081
1082#ifdef notyet
1083 // Have the child raise an event on exit. This is used to keep the child in
1084 // limbo until it is destroyed.
1085 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1086 {
1087 args->m_error.SetErrorToErrno();
1088 goto FINISH;
1089 }
1090#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001091 // Release the master terminal descriptor and pass it off to the
1092 // ProcessMonitor instance. Similarly stash the inferior pid.
1093 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094 monitor->m_pid = pid;
1095
1096 // Set the terminal fd to be in non blocking mode (it simplifies the
1097 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1098 // descriptor to read from).
1099 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1100 goto FINISH;
1101
Ed Mastee5441432013-09-03 23:55:30 +00001102 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001103
1104FINISH:
1105 return args->m_error.Success();
1106}
1107
Johnny Chen9ed5b492012-01-05 21:48:15 +00001108void
1109ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1110{
1111 static const char *g_thread_name = "lldb.process.freebsd.operation";
1112
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001113 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001114 return;
1115
Zachary Turner39de3112014-09-09 20:54:56 +00001116 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001117}
1118
Johnny Chen9ed5b492012-01-05 21:48:15 +00001119void *
1120ProcessMonitor::AttachOpThread(void *arg)
1121{
1122 AttachArgs *args = static_cast<AttachArgs*>(arg);
1123
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001124 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001125
1126 ServeOperation(args);
1127 return NULL;
1128}
1129
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001130void
Johnny Chen9ed5b492012-01-05 21:48:15 +00001131ProcessMonitor::Attach(AttachArgs *args)
1132{
1133 lldb::pid_t pid = args->m_pid;
1134
1135 ProcessMonitor *monitor = args->m_monitor;
1136 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001137
1138 if (pid <= 1)
1139 {
1140 args->m_error.SetErrorToGenericError();
1141 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001142 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001143 }
1144
1145 // Attach to the requested process.
1146 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1147 {
1148 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001149 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001150 }
1151
1152 int status;
1153 if ((status = waitpid(pid, NULL, 0)) < 0)
1154 {
1155 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001156 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001157 }
1158
Ed Mastee5441432013-09-03 23:55:30 +00001159 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001160}
1161
Ed Maste7fd845c2013-12-09 15:51:17 +00001162size_t
1163ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1164{
1165 lwpid_t *tids;
1166 int tdcnt;
1167
1168 thread_ids.clear();
1169
1170 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1171 if (tdcnt <= 0)
1172 return 0;
1173 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1174 if (tids == NULL)
1175 return 0;
1176 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1177 free(tids);
1178 return 0;
1179 }
1180 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1181 free(tids);
1182 return thread_ids.size();
1183}
1184
Johnny Chen9ed5b492012-01-05 21:48:15 +00001185bool
1186ProcessMonitor::MonitorCallback(void *callback_baton,
1187 lldb::pid_t pid,
1188 bool exited,
1189 int signal,
1190 int status)
1191{
1192 ProcessMessage message;
1193 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001194 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001195 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001196 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001197 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001198 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001199
Ed Mastea02f5532013-07-02 16:45:16 +00001200 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1201
1202 if (exited)
1203 {
1204 if (log)
1205 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1206 message = ProcessMessage::Exit(pid, status);
1207 process->SendMessage(message);
1208 return pid == process->GetID();
1209 }
1210
Ed Maste819e3992013-07-17 14:02:20 +00001211 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001212 stop_monitoring = true; // pid is gone. Bail.
1213 else {
Ed Maste819e3992013-07-17 14:02:20 +00001214 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001215 {
1216 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001217 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001218 break;
1219
1220 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001221 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001222 break;
1223 }
1224
1225 process->SendMessage(message);
1226 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1227 }
1228
1229 return stop_monitoring;
1230}
1231
1232ProcessMessage
1233ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001234 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001235{
1236 ProcessMessage message;
1237
Ed Mastea02f5532013-07-02 16:45:16 +00001238 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1239
Matt Kopec7de48462013-03-06 17:20:48 +00001240 assert(monitor);
1241 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001242
1243 switch (info->si_code)
1244 {
1245 default:
1246 assert(false && "Unexpected SIGTRAP code!");
1247 break;
1248
1249 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1250 {
1251 // The inferior process is about to exit. Maintain the process in a
1252 // state of "limbo" until we are explicitly commanded to detach,
1253 // destroy, resume, etc.
1254 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001255 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001256 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001257 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001258 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1259 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001260 break;
1261 }
1262
1263 case 0:
1264 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001265 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001266 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001267 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001268 break;
1269
1270 case SI_KERNEL:
1271 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001272 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001273 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1274 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001275 break;
1276 }
1277
1278 return message;
1279}
1280
1281ProcessMessage
1282ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001283 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001284{
1285 ProcessMessage message;
1286 int signo = info->si_signo;
1287
Ed Mastea02f5532013-07-02 16:45:16 +00001288 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1289
Johnny Chen9ed5b492012-01-05 21:48:15 +00001290 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1291 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1292 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1293 //
1294 // IOW, user generated signals never generate what we consider to be a
1295 // "crash".
1296 //
1297 // Similarly, ACK signals generated by this monitor.
1298 if (info->si_code == SI_USER)
1299 {
Ed Mastea02f5532013-07-02 16:45:16 +00001300 if (log)
1301 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1302 __FUNCTION__,
1303 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1304 "SI_USER",
1305 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001306 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001307 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001308 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001309 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001310 }
1311
Ed Mastea02f5532013-07-02 16:45:16 +00001312 if (log)
1313 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1314
Chaoren Lin28e57422015-02-03 01:51:25 +00001315 switch (signo)
1316 {
1317 case SIGSEGV:
1318 case SIGILL:
1319 case SIGFPE:
1320 case SIGBUS:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001321 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001322 const auto reason = GetCrashReason(*info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001323 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001324 }
1325
1326 // Everything else is "normal" and does not require any special action on
1327 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001328 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001329}
1330
Johnny Chen9ed5b492012-01-05 21:48:15 +00001331void
1332ProcessMonitor::ServeOperation(OperationArgs *args)
1333{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001334 ProcessMonitor *monitor = args->m_monitor;
1335
Johnny Chen9ed5b492012-01-05 21:48:15 +00001336 // We are finised with the arguments and are ready to go. Sync with the
1337 // parent thread and start serving operations on the inferior.
1338 sem_post(&args->m_semaphore);
1339
1340 for (;;)
1341 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001342 // wait for next pending operation
1343 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001344
Ed Maste756e1ff2013-09-18 19:34:08 +00001345 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001346
Ed Maste756e1ff2013-09-18 19:34:08 +00001347 // notify calling thread that operation is complete
1348 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001349 }
1350}
1351
1352void
1353ProcessMonitor::DoOperation(Operation *op)
1354{
Ed Maste756e1ff2013-09-18 19:34:08 +00001355 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001356
Ed Maste756e1ff2013-09-18 19:34:08 +00001357 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001358
Ed Maste756e1ff2013-09-18 19:34:08 +00001359 // notify operation thread that an operation is ready to be processed
1360 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001361
Ed Maste756e1ff2013-09-18 19:34:08 +00001362 // wait for operation to complete
1363 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001364}
1365
1366size_t
1367ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1368 Error &error)
1369{
1370 size_t result;
1371 ReadOperation op(vm_addr, buf, size, error, result);
1372 DoOperation(&op);
1373 return result;
1374}
1375
1376size_t
1377ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1378 lldb_private::Error &error)
1379{
1380 size_t result;
1381 WriteOperation op(vm_addr, buf, size, error, result);
1382 DoOperation(&op);
1383 return result;
1384}
1385
1386bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001387ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001388 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001389{
1390 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001391 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001392 DoOperation(&op);
1393 return result;
1394}
1395
1396bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001397ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001398 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001399{
1400 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001401 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001402 DoOperation(&op);
1403 return result;
1404}
1405
1406bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001407ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1408 const char *reg_name, unsigned size,
1409 lldb_private::RegisterValue &value)
1410{
1411 bool result;
1412 ReadDebugRegOperation op(tid, offset, size, value, result);
1413 DoOperation(&op);
1414 return result;
1415}
1416
1417bool
1418ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1419 const char *reg_name,
1420 const lldb_private::RegisterValue &value)
1421{
1422 bool result;
1423 WriteDebugRegOperation op(tid, offset, value, result);
1424 DoOperation(&op);
1425 return result;
1426}
1427
1428bool
Matt Kopec7de48462013-03-06 17:20:48 +00001429ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001430{
1431 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001432 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001433 DoOperation(&op);
1434 return result;
1435}
1436
1437bool
Matt Kopec7de48462013-03-06 17:20:48 +00001438ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001439{
1440 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001441 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001442 DoOperation(&op);
1443 return result;
1444}
1445
1446bool
Ed Maste5d34af32013-06-24 15:09:18 +00001447ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1448{
1449 return false;
1450}
1451
1452bool
Matt Kopec7de48462013-03-06 17:20:48 +00001453ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001454{
1455 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001456 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001457 DoOperation(&op);
1458 return result;
1459}
1460
1461bool
Matt Kopec7de48462013-03-06 17:20:48 +00001462ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001463{
1464 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001465 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001466 DoOperation(&op);
1467 return result;
1468}
1469
1470bool
Ed Maste5d34af32013-06-24 15:09:18 +00001471ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1472{
1473 return false;
1474}
1475
1476bool
Ed Maste68f51792013-10-18 19:16:44 +00001477ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1478{
1479 return false;
1480}
1481
1482bool
Ed Maste502f9022013-11-25 16:31:23 +00001483ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001484{
1485 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001486 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1487
Ed Maste4aeb3c02014-05-28 14:11:20 +00001488 if (log) {
1489 const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo);
1490 if (signame == nullptr)
1491 signame = "<none>";
1492 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1493 __FUNCTION__, GetPID(), signame);
1494 }
Ed Maste502f9022013-11-25 16:31:23 +00001495 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001496 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001497 if (log)
1498 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001499 return result;
1500}
1501
1502bool
Ed Maste502f9022013-11-25 16:31:23 +00001503ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001504{
1505 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001506 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001507 DoOperation(&op);
1508 return result;
1509}
1510
1511bool
Ed Maste70882932014-04-01 14:30:56 +00001512ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001513{
1514 bool result;
1515 KillOperation op(result);
1516 DoOperation(&op);
1517 return result;
1518}
1519
1520bool
Ed Maste819e3992013-07-17 14:02:20 +00001521ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001522{
1523 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001524 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001525 DoOperation(&op);
1526 return result;
1527}
1528
1529bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001530ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1531{
1532 bool result;
1533 ThreadSuspendOperation op(tid, suspend, result);
1534 DoOperation(&op);
1535 return result;
1536}
1537
1538bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001539ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1540{
1541 bool result;
1542 EventMessageOperation op(tid, message, result);
1543 DoOperation(&op);
1544 return result;
1545}
1546
Ed Mastea02f5532013-07-02 16:45:16 +00001547lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001548ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001549{
Ed Mastea02f5532013-07-02 16:45:16 +00001550 lldb_private::Error error;
1551 if (tid != LLDB_INVALID_THREAD_ID)
1552 {
1553 DetachOperation op(error);
1554 DoOperation(&op);
1555 }
1556 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001557}
1558
1559bool
1560ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1561{
1562 int target_fd = open(path, flags, 0666);
1563
1564 if (target_fd == -1)
1565 return false;
1566
Ed Maste7f0230f2015-02-05 16:09:03 +00001567 if (dup2(target_fd, fd) == -1)
1568 return false;
1569
1570 return (close(target_fd) == -1) ? false : true;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001571}
1572
1573void
1574ProcessMonitor::StopMonitoringChildProcess()
1575{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001576 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001577 {
Zachary Turner39de3112014-09-09 20:54:56 +00001578 m_monitor_thread.Cancel();
1579 m_monitor_thread.Join(nullptr);
1580 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001581 }
1582}
1583
1584void
1585ProcessMonitor::StopMonitor()
1586{
1587 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001588 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001589 sem_destroy(&m_operation_pending);
1590 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00001591 if (m_terminal_fd >= 0) {
1592 close(m_terminal_fd);
1593 m_terminal_fd = -1;
1594 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001595}
1596
Andrew Kaylord4d54992013-09-17 00:30:24 +00001597// FIXME: On Linux, when a new thread is created, we receive to notifications,
1598// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1599// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1600// the new child thread indicating that it has is stopped because we attached.
1601// We have no guarantee of the order in which these arrive, but we need both
1602// before we are ready to proceed. We currently keep a list of threads which
1603// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1604// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1605// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1606//
1607// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1608// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1609// logically needed.
1610//
1611// We really should figure out what actually happens on FreeBSD and move the
1612// Linux-specific logic out of ProcessPOSIX as needed.
1613
1614bool
1615ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1616{
1617 return true;
1618}
1619
Johnny Chen9ed5b492012-01-05 21:48:15 +00001620void
Ed Mastea02f5532013-07-02 16:45:16 +00001621ProcessMonitor::StopOpThread()
1622{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001623 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001624 return;
1625
Zachary Turner39de3112014-09-09 20:54:56 +00001626 m_operation_thread.Cancel();
1627 m_operation_thread.Join(nullptr);
1628 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001629}