blob: b33f83303971ae2315ad084fec6bc8feba343dcd [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"
31#include "lldb/Utility/PseudoTerminal.h"
32
Chaoren Lin28e57422015-02-03 01:51:25 +000033#include "Plugins/Process/POSIX/CrashReason.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000034#include "POSIXThread.h"
35#include "ProcessFreeBSD.h"
36#include "ProcessPOSIXLog.h"
37#include "ProcessMonitor.h"
38
39extern "C" {
40 extern char ** environ;
41 }
42
43using namespace lldb;
44using namespace lldb_private;
45
46// We disable the tracing of ptrace calls for integration builds to
47// avoid the additional indirection and checks.
48#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
49// Wrapper for ptrace to catch errors and log calls.
50
51const char *
52Get_PT_IO_OP(int op)
53{
54 switch (op) {
55 case PIOD_READ_D: return "READ_D";
56 case PIOD_WRITE_D: return "WRITE_D";
57 case PIOD_READ_I: return "READ_I";
58 case PIOD_WRITE_I: return "WRITE_I";
59 default: return "Unknown op";
60 }
61}
62
Matt Kopec7de48462013-03-06 17:20:48 +000063// Wrapper for ptrace to catch errors and log calls.
64// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000065extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000066PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000067 const char* reqName, const char* file, int line)
68{
69 long int result;
70
Ashok Thirumurthi01186352013-03-28 16:02:31 +000071 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000072
73 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000074 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000075 reqName, pid, addr, data, file, line);
76 if (req == PT_IO) {
77 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
78
Sylvestre Ledru779f9212013-10-31 23:55:19 +000079 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000080 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000081 }
82 }
83
84 //PtraceDisplayBytes(req, data);
85
86 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000087 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000088
89 //PtraceDisplayBytes(req, data);
90
Matt Kopec7de48462013-03-06 17:20:48 +000091 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000092 {
93 const char* str;
94 switch (errno)
95 {
96 case ESRCH: str = "ESRCH"; break;
97 case EINVAL: str = "EINVAL"; break;
98 case EBUSY: str = "EBUSY"; break;
99 case EPERM: str = "EPERM"; break;
100 default: str = "<unknown>";
101 }
102 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
103 }
104
105 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000106#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000107 if (req == PT_GETREGS) {
108 struct reg *r = (struct reg *) addr;
109
110 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
111 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
112 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
113 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
114 }
Ed Maste7d4c0d5b2013-07-22 20:51:08 +0000115#endif
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000116#ifndef __powerpc__
Ed Mastea4be2c52014-02-19 18:34:06 +0000117 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
118 struct dbreg *r = (struct dbreg *) addr;
119 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
120
121 for (int i = 0; i <= 7; i++)
122 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
123 }
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000124#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000125 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000126
127 return result;
128}
129
Matt Kopec7de48462013-03-06 17:20:48 +0000130// Wrapper for ptrace when logging is not required.
131// Sets errno to 0 prior to calling ptrace.
132extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000133PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000134{
135 long result = 0;
136 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000137 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000138 return result;
139}
140
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141#define PTRACE(req, pid, addr, data) \
142 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
143#else
Matt Kopec7de48462013-03-06 17:20:48 +0000144 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000145#endif
146
147//------------------------------------------------------------------------------
148// Static implementations of ProcessMonitor::ReadMemory and
149// ProcessMonitor::WriteMemory. This enables mutual recursion between these
150// functions without needed to go thru the thread funnel.
151
152static size_t
153DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
154 Error &error)
155{
156 struct ptrace_io_desc pi_desc;
157
158 pi_desc.piod_op = PIOD_READ_D;
159 pi_desc.piod_offs = (void *)vm_addr;
160 pi_desc.piod_addr = buf;
161 pi_desc.piod_len = size;
162
163 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
164 error.SetErrorToErrno();
165 return pi_desc.piod_len;
166}
167
168static size_t
169DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
170 size_t size, Error &error)
171{
172 struct ptrace_io_desc pi_desc;
173
174 pi_desc.piod_op = PIOD_WRITE_D;
175 pi_desc.piod_offs = (void *)vm_addr;
176 pi_desc.piod_addr = (void *)buf;
177 pi_desc.piod_len = size;
178
179 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
180 error.SetErrorToErrno();
181 return pi_desc.piod_len;
182}
183
184// Simple helper function to ensure flags are enabled on the given file
185// descriptor.
186static bool
187EnsureFDFlags(int fd, int flags, Error &error)
188{
189 int status;
190
191 if ((status = fcntl(fd, F_GETFL)) == -1)
192 {
193 error.SetErrorToErrno();
194 return false;
195 }
196
197 if (fcntl(fd, F_SETFL, status | flags) == -1)
198 {
199 error.SetErrorToErrno();
200 return false;
201 }
202
203 return true;
204}
205
206//------------------------------------------------------------------------------
207/// @class Operation
208/// @brief Represents a ProcessMonitor operation.
209///
210/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
211/// one that spawned or attached to the process from the start. Therefore, when
212/// a ProcessMonitor is asked to deliver or change the state of an inferior
213/// process the operation must be "funneled" to a specific thread to perform the
214/// task. The Operation class provides an abstract base for all services the
215/// ProcessMonitor must perform via the single virtual function Execute, thus
216/// encapsulating the code that needs to run in the privileged context.
217class Operation
218{
219public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000220 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221 virtual void Execute(ProcessMonitor *monitor) = 0;
222};
223
224//------------------------------------------------------------------------------
225/// @class ReadOperation
226/// @brief Implements ProcessMonitor::ReadMemory.
227class ReadOperation : public Operation
228{
229public:
230 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
231 Error &error, size_t &result)
232 : m_addr(addr), m_buff(buff), m_size(size),
233 m_error(error), m_result(result)
234 { }
235
236 void Execute(ProcessMonitor *monitor);
237
238private:
239 lldb::addr_t m_addr;
240 void *m_buff;
241 size_t m_size;
242 Error &m_error;
243 size_t &m_result;
244};
245
246void
247ReadOperation::Execute(ProcessMonitor *monitor)
248{
249 lldb::pid_t pid = monitor->GetPID();
250
251 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
252}
253
254//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000255/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256/// @brief Implements ProcessMonitor::WriteMemory.
257class WriteOperation : public Operation
258{
259public:
260 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
261 Error &error, size_t &result)
262 : m_addr(addr), m_buff(buff), m_size(size),
263 m_error(error), m_result(result)
264 { }
265
266 void Execute(ProcessMonitor *monitor);
267
268private:
269 lldb::addr_t m_addr;
270 const void *m_buff;
271 size_t m_size;
272 Error &m_error;
273 size_t &m_result;
274};
275
276void
277WriteOperation::Execute(ProcessMonitor *monitor)
278{
279 lldb::pid_t pid = monitor->GetPID();
280
281 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
282}
283
284//------------------------------------------------------------------------------
285/// @class ReadRegOperation
286/// @brief Implements ProcessMonitor::ReadRegisterValue.
287class ReadRegOperation : public Operation
288{
289public:
Ed Maste6f066412013-07-04 21:47:32 +0000290 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
291 RegisterValue &value, bool &result)
292 : m_tid(tid), m_offset(offset), m_size(size),
293 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000294 { }
295
296 void Execute(ProcessMonitor *monitor);
297
298private:
Ed Maste6f066412013-07-04 21:47:32 +0000299 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000300 unsigned m_offset;
301 unsigned m_size;
302 RegisterValue &m_value;
303 bool &m_result;
304};
305
306void
307ReadRegOperation::Execute(ProcessMonitor *monitor)
308{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000309 struct reg regs;
310 int rc;
311
Ed Maste6f066412013-07-04 21:47:32 +0000312 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000313 m_result = false;
314 } else {
Justin Hibbits89e6f382014-11-12 15:14:08 +0000315 // 'struct reg' contains only 32- or 64-bit register values. Punt on
316 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
317 // processes on powerpc64 (probably the same for i386 on amd64)
318 if (m_size == sizeof(uint32_t))
319 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
320 else if (m_size == sizeof(uint64_t))
321 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
322 else
Johnny Chen9ed5b492012-01-05 21:48:15 +0000323 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
324 m_result = true;
325 }
326}
327
328//------------------------------------------------------------------------------
329/// @class WriteRegOperation
330/// @brief Implements ProcessMonitor::WriteRegisterValue.
331class WriteRegOperation : public Operation
332{
333public:
Ed Maste6f066412013-07-04 21:47:32 +0000334 WriteRegOperation(lldb::tid_t tid, unsigned offset,
335 const RegisterValue &value, bool &result)
336 : m_tid(tid), m_offset(offset),
337 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000338 { }
339
340 void Execute(ProcessMonitor *monitor);
341
342private:
Ed Maste6f066412013-07-04 21:47:32 +0000343 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000344 unsigned m_offset;
345 const RegisterValue &m_value;
346 bool &m_result;
347};
348
349void
350WriteRegOperation::Execute(ProcessMonitor *monitor)
351{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000352 struct reg regs;
353
Ed Maste6f066412013-07-04 21:47:32 +0000354 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000355 m_result = false;
356 return;
357 }
358 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000359 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000360 m_result = false;
361 else
362 m_result = true;
363}
364
365//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000366/// @class ReadDebugRegOperation
367/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
368class ReadDebugRegOperation : public Operation
369{
370public:
371 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
372 RegisterValue &value, bool &result)
373 : m_tid(tid), m_offset(offset), m_size(size),
374 m_value(value), m_result(result)
375 { }
376
377 void Execute(ProcessMonitor *monitor);
378
379private:
380 lldb::tid_t m_tid;
381 unsigned m_offset;
382 unsigned m_size;
383 RegisterValue &m_value;
384 bool &m_result;
385};
386
387void
388ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
389{
390 struct dbreg regs;
391 int rc;
392
393 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
394 m_result = false;
395 } else {
396 if (m_size == sizeof(uintptr_t))
397 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
398 else
399 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
400 m_result = true;
401 }
402}
403
404//------------------------------------------------------------------------------
405/// @class WriteDebugRegOperation
406/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
407class WriteDebugRegOperation : public Operation
408{
409public:
410 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
411 const RegisterValue &value, bool &result)
412 : m_tid(tid), m_offset(offset),
413 m_value(value), m_result(result)
414 { }
415
416 void Execute(ProcessMonitor *monitor);
417
418private:
419 lldb::tid_t m_tid;
420 unsigned m_offset;
421 const RegisterValue &m_value;
422 bool &m_result;
423};
424
425void
426WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
427{
428 struct dbreg regs;
429
430 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
431 m_result = false;
432 return;
433 }
434 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
435 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
436 m_result = false;
437 else
438 m_result = true;
439}
440
441//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000442/// @class ReadGPROperation
443/// @brief Implements ProcessMonitor::ReadGPR.
444class ReadGPROperation : public Operation
445{
446public:
Ed Maste6f066412013-07-04 21:47:32 +0000447 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
448 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000449 { }
450
451 void Execute(ProcessMonitor *monitor);
452
453private:
Ed Maste6f066412013-07-04 21:47:32 +0000454 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000455 void *m_buf;
456 bool &m_result;
457};
458
459void
460ReadGPROperation::Execute(ProcessMonitor *monitor)
461{
462 int rc;
463
464 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000465 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000466 if (errno != 0)
467 m_result = false;
468 else
469 m_result = true;
470}
471
472//------------------------------------------------------------------------------
473/// @class ReadFPROperation
474/// @brief Implements ProcessMonitor::ReadFPR.
475class ReadFPROperation : public Operation
476{
477public:
Ed Maste6f066412013-07-04 21:47:32 +0000478 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
479 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000480 { }
481
482 void Execute(ProcessMonitor *monitor);
483
484private:
Ed Maste6f066412013-07-04 21:47:32 +0000485 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486 void *m_buf;
487 bool &m_result;
488};
489
490void
491ReadFPROperation::Execute(ProcessMonitor *monitor)
492{
Ed Maste6f066412013-07-04 21:47:32 +0000493 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000494 m_result = false;
495 else
496 m_result = true;
497}
498
499//------------------------------------------------------------------------------
500/// @class WriteGPROperation
501/// @brief Implements ProcessMonitor::WriteGPR.
502class WriteGPROperation : public Operation
503{
504public:
Ed Maste6f066412013-07-04 21:47:32 +0000505 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
506 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000507 { }
508
509 void Execute(ProcessMonitor *monitor);
510
511private:
Ed Maste6f066412013-07-04 21:47:32 +0000512 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513 void *m_buf;
514 bool &m_result;
515};
516
517void
518WriteGPROperation::Execute(ProcessMonitor *monitor)
519{
Ed Maste6f066412013-07-04 21:47:32 +0000520 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000521 m_result = false;
522 else
523 m_result = true;
524}
525
526//------------------------------------------------------------------------------
527/// @class WriteFPROperation
528/// @brief Implements ProcessMonitor::WriteFPR.
529class WriteFPROperation : public Operation
530{
531public:
Ed Maste6f066412013-07-04 21:47:32 +0000532 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
533 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000534 { }
535
536 void Execute(ProcessMonitor *monitor);
537
538private:
Ed Maste6f066412013-07-04 21:47:32 +0000539 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540 void *m_buf;
541 bool &m_result;
542};
543
544void
545WriteFPROperation::Execute(ProcessMonitor *monitor)
546{
Ed Maste6f066412013-07-04 21:47:32 +0000547 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000548 m_result = false;
549 else
550 m_result = true;
551}
552
553//------------------------------------------------------------------------------
554/// @class ResumeOperation
555/// @brief Implements ProcessMonitor::Resume.
556class ResumeOperation : public Operation
557{
558public:
Ed Maste502f9022013-11-25 16:31:23 +0000559 ResumeOperation(uint32_t signo, bool &result) :
560 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000561
562 void Execute(ProcessMonitor *monitor);
563
564private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000565 uint32_t m_signo;
566 bool &m_result;
567};
568
569void
570ResumeOperation::Execute(ProcessMonitor *monitor)
571{
Ed Maste502f9022013-11-25 16:31:23 +0000572 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000573 int data = 0;
574
575 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
576 data = m_signo;
577
Ed Maste502f9022013-11-25 16:31:23 +0000578 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000579 {
580 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
581
582 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000583 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000585 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000586 else
587 m_result = true;
588}
589
590//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000591/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000592/// @brief Implements ProcessMonitor::SingleStep.
593class SingleStepOperation : public Operation
594{
595public:
Ed Maste502f9022013-11-25 16:31:23 +0000596 SingleStepOperation(uint32_t signo, bool &result)
597 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000598
599 void Execute(ProcessMonitor *monitor);
600
601private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000602 uint32_t m_signo;
603 bool &m_result;
604};
605
606void
607SingleStepOperation::Execute(ProcessMonitor *monitor)
608{
Ed Maste502f9022013-11-25 16:31:23 +0000609 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000610 int data = 0;
611
612 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
613 data = m_signo;
614
Ed Maste502f9022013-11-25 16:31:23 +0000615 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000616 m_result = false;
617 else
618 m_result = true;
619}
620
621//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000622/// @class LwpInfoOperation
623/// @brief Implements ProcessMonitor::GetLwpInfo.
624class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000625{
626public:
Ed Maste819e3992013-07-17 14:02:20 +0000627 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000628 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000629
630 void Execute(ProcessMonitor *monitor);
631
632private:
633 lldb::tid_t m_tid;
634 void *m_info;
635 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000636 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000637};
638
639void
Ed Maste819e3992013-07-17 14:02:20 +0000640LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000641{
642 struct ptrace_lwpinfo plwp;
643
Daniel Maleaa35970a2012-11-23 18:09:58 +0000644 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000645 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000646 m_err = errno;
647 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000648 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000649 m_result = true;
650 }
651}
652
653//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000654/// @class ThreadSuspendOperation
655/// @brief Implements ProcessMonitor::ThreadSuspend.
656class ThreadSuspendOperation : public Operation
657{
658public:
659 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
660 : m_tid(tid), m_suspend(suspend), m_result(result) { }
661
662 void Execute(ProcessMonitor *monitor);
663
664private:
665 lldb::tid_t m_tid;
666 bool m_suspend;
667 bool &m_result;
668} ;
669
670void
671ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
672{
673 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
674}
675
676
677
678//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000679/// @class EventMessageOperation
680/// @brief Implements ProcessMonitor::GetEventMessage.
681class EventMessageOperation : public Operation
682{
683public:
684 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
685 : m_tid(tid), m_message(message), m_result(result) { }
686
687 void Execute(ProcessMonitor *monitor);
688
689private:
690 lldb::tid_t m_tid;
691 unsigned long *m_message;
692 bool &m_result;
693};
694
695void
696EventMessageOperation::Execute(ProcessMonitor *monitor)
697{
698 struct ptrace_lwpinfo plwp;
699
700 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
701 m_result = false;
702 else {
703 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000704 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000705 m_result = true;
706 } else
707 m_result = false;
708 }
709}
710
711//------------------------------------------------------------------------------
712/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000713/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000714class KillOperation : public Operation
715{
716public:
717 KillOperation(bool &result) : m_result(result) { }
718
719 void Execute(ProcessMonitor *monitor);
720
721private:
722 bool &m_result;
723};
724
725void
726KillOperation::Execute(ProcessMonitor *monitor)
727{
728 lldb::pid_t pid = monitor->GetPID();
729
730 if (PTRACE(PT_KILL, pid, NULL, 0))
731 m_result = false;
732 else
733 m_result = true;
734}
735
736//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000737/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000738/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000739class DetachOperation : public Operation
740{
741public:
742 DetachOperation(Error &result) : m_error(result) { }
743
744 void Execute(ProcessMonitor *monitor);
745
746private:
747 Error &m_error;
748};
749
750void
751DetachOperation::Execute(ProcessMonitor *monitor)
752{
753 lldb::pid_t pid = monitor->GetPID();
754
755 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
756 m_error.SetErrorToErrno();
757
758}
759
760ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
761 : m_monitor(monitor)
762{
763 sem_init(&m_semaphore, 0, 0);
764}
765
766ProcessMonitor::OperationArgs::~OperationArgs()
767{
768 sem_destroy(&m_semaphore);
769}
770
771ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
772 lldb_private::Module *module,
773 char const **argv,
774 char const **envp,
775 const char *stdin_path,
776 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000777 const char *stderr_path,
778 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000779 : OperationArgs(monitor),
780 m_module(module),
781 m_argv(argv),
782 m_envp(envp),
783 m_stdin_path(stdin_path),
784 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000785 m_stderr_path(stderr_path),
786 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000787
788ProcessMonitor::LaunchArgs::~LaunchArgs()
789{ }
790
791ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
792 lldb::pid_t pid)
793 : OperationArgs(monitor), m_pid(pid) { }
794
795ProcessMonitor::AttachArgs::~AttachArgs()
796{ }
797
798//------------------------------------------------------------------------------
799/// The basic design of the ProcessMonitor is built around two threads.
800///
801/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
802/// for changes in the debugee state. When a change is detected a
803/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
804/// "drives" state changes in the debugger.
805///
806/// The second thread (@see OperationThread) is responsible for two things 1)
807/// launching or attaching to the inferior process, and then 2) servicing
808/// operations such as register reads/writes, stepping, etc. See the comments
809/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000810ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000811 Module *module,
812 const char *argv[],
813 const char *envp[],
814 const char *stdin_path,
815 const char *stdout_path,
816 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000817 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000818 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000819 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000820 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000823 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000824{
Ed Maste756e1ff2013-09-18 19:34:08 +0000825 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
826 stdin_path, stdout_path, stderr_path,
827 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000828
829
Ed Maste756e1ff2013-09-18 19:34:08 +0000830 sem_init(&m_operation_pending, 0, 0);
831 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000832
833 StartLaunchOpThread(args.get(), error);
834 if (!error.Success())
835 return;
836
837WAIT_AGAIN:
838 // Wait for the operation thread to initialize.
839 if (sem_wait(&args->m_semaphore))
840 {
841 if (errno == EINTR)
842 goto WAIT_AGAIN;
843 else
844 {
845 error.SetErrorToErrno();
846 return;
847 }
848 }
849
850 // Check that the launch was a success.
851 if (!args->m_error.Success())
852 {
Ed Mastea02f5532013-07-02 16:45:16 +0000853 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000854 error = args->m_error;
855 return;
856 }
857
858 // Finally, start monitoring the child process for change in state.
859 m_monitor_thread = Host::StartMonitoringChildProcess(
860 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000861 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000862 {
863 error.SetErrorToGenericError();
864 error.SetErrorString("Process launch failed.");
865 return;
866 }
867}
868
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000869ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000870 lldb::pid_t pid,
871 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000872 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000873 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000875 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000876{
Ed Maste756e1ff2013-09-18 19:34:08 +0000877 sem_init(&m_operation_pending, 0, 0);
878 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000879
Johnny Chen9ed5b492012-01-05 21:48:15 +0000880
Ed Maste756e1ff2013-09-18 19:34:08 +0000881 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000882
883 StartAttachOpThread(args.get(), error);
884 if (!error.Success())
885 return;
886
887WAIT_AGAIN:
888 // Wait for the operation thread to initialize.
889 if (sem_wait(&args->m_semaphore))
890 {
891 if (errno == EINTR)
892 goto WAIT_AGAIN;
893 else
894 {
895 error.SetErrorToErrno();
896 return;
897 }
898 }
899
Ed Mastea02f5532013-07-02 16:45:16 +0000900 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000901 if (!args->m_error.Success())
902 {
Ed Mastea02f5532013-07-02 16:45:16 +0000903 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000904 error = args->m_error;
905 return;
906 }
907
908 // Finally, start monitoring the child process for change in state.
909 m_monitor_thread = Host::StartMonitoringChildProcess(
910 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000911 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000912 {
913 error.SetErrorToGenericError();
914 error.SetErrorString("Process attach failed.");
915 return;
916 }
917}
918
919ProcessMonitor::~ProcessMonitor()
920{
921 StopMonitor();
922}
923
924//------------------------------------------------------------------------------
925// Thread setup and tear down.
926void
927ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
928{
929 static const char *g_thread_name = "lldb.process.freebsd.operation";
930
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000931 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000932 return;
933
Zachary Turner39de3112014-09-09 20:54:56 +0000934 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000935}
936
Johnny Chen9ed5b492012-01-05 21:48:15 +0000937void *
938ProcessMonitor::LaunchOpThread(void *arg)
939{
940 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
941
942 if (!Launch(args)) {
943 sem_post(&args->m_semaphore);
944 return NULL;
945 }
946
947 ServeOperation(args);
948 return NULL;
949}
950
951bool
952ProcessMonitor::Launch(LaunchArgs *args)
953{
954 ProcessMonitor *monitor = args->m_monitor;
955 ProcessFreeBSD &process = monitor->GetProcess();
956 const char **argv = args->m_argv;
957 const char **envp = args->m_envp;
958 const char *stdin_path = args->m_stdin_path;
959 const char *stdout_path = args->m_stdout_path;
960 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000961 const char *working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000962
963 lldb_utility::PseudoTerminal terminal;
964 const size_t err_len = 1024;
965 char err_str[err_len];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000966 lldb::pid_t pid;
967
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968 // Propagate the environment if one is not supplied.
969 if (envp == NULL || envp[0] == NULL)
970 envp = const_cast<const char **>(environ);
971
Ed Mastea02f5532013-07-02 16:45:16 +0000972 if ((pid = terminal.Fork(err_str, err_len)) == -1)
973 {
974 args->m_error.SetErrorToGenericError();
975 args->m_error.SetErrorString("Process fork failed.");
976 goto FINISH;
977 }
978
Johnny Chen9ed5b492012-01-05 21:48:15 +0000979 // Recognized child exit status codes.
980 enum {
981 ePtraceFailed = 1,
982 eDupStdinFailed,
983 eDupStdoutFailed,
984 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000985 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000986 eExecFailed,
987 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000988 };
989
Johnny Chen9ed5b492012-01-05 21:48:15 +0000990 // Child process.
991 if (pid == 0)
992 {
993 // Trace this process.
994 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
995 exit(ePtraceFailed);
996
Ed Maste7f0230f2015-02-05 16:09:03 +0000997 // terminal has already dupped the tty descriptors to stdin/out/err.
998 // This closes original fd from which they were copied (and avoids
999 // leaking descriptors to the debugged process.
1000 terminal.CloseSlaveFileDescriptor();
1001
Johnny Chen9ed5b492012-01-05 21:48:15 +00001002 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +00001003 if (setgid(getgid()) != 0)
1004 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001005
1006 // Let us have our own process group.
1007 setpgid(0, 0);
1008
1009 // Dup file descriptors if needed.
1010 //
1011 // FIXME: If two or more of the paths are the same we needlessly open
1012 // the same file multiple times.
1013 if (stdin_path != NULL && stdin_path[0])
1014 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1015 exit(eDupStdinFailed);
1016
1017 if (stdout_path != NULL && stdout_path[0])
1018 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1019 exit(eDupStdoutFailed);
1020
1021 if (stderr_path != NULL && stderr_path[0])
1022 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1023 exit(eDupStderrFailed);
1024
Daniel Malea6217d2a2013-01-08 14:49:22 +00001025 // Change working directory
1026 if (working_dir != NULL && working_dir[0])
1027 if (0 != ::chdir(working_dir))
1028 exit(eChdirFailed);
1029
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
1559ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1560{
1561 int target_fd = open(path, flags, 0666);
1562
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}