blob: 2318a657578b9dfff8efb51d8edd2d910b7177f3 [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"
Ed Mastefe5a6422015-07-28 15:45:57 +000035#include "FreeBSDThread.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000036#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
Ed Mastea681ea22015-06-24 20:02:56 +0000111 log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
112 r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000113 }
Ed Mastea4be2c52014-02-19 18:34:06 +0000114 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
115 struct dbreg *r = (struct dbreg *) addr;
116 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
117
118 for (int i = 0; i <= 7; i++)
119 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
120 }
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000121#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000122 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000123
124 return result;
125}
126
Matt Kopec7de48462013-03-06 17:20:48 +0000127// Wrapper for ptrace when logging is not required.
128// Sets errno to 0 prior to calling ptrace.
129extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000130PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000131{
132 long result = 0;
133 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000134 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000135 return result;
136}
137
Johnny Chen9ed5b492012-01-05 21:48:15 +0000138#define PTRACE(req, pid, addr, data) \
139 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
140#else
Matt Kopec7de48462013-03-06 17:20:48 +0000141 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000142#endif
143
144//------------------------------------------------------------------------------
145// Static implementations of ProcessMonitor::ReadMemory and
146// ProcessMonitor::WriteMemory. This enables mutual recursion between these
147// functions without needed to go thru the thread funnel.
148
149static size_t
150DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
151 Error &error)
152{
153 struct ptrace_io_desc pi_desc;
154
155 pi_desc.piod_op = PIOD_READ_D;
156 pi_desc.piod_offs = (void *)vm_addr;
157 pi_desc.piod_addr = buf;
158 pi_desc.piod_len = size;
159
160 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
161 error.SetErrorToErrno();
162 return pi_desc.piod_len;
163}
164
165static size_t
166DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
167 size_t size, Error &error)
168{
169 struct ptrace_io_desc pi_desc;
170
171 pi_desc.piod_op = PIOD_WRITE_D;
172 pi_desc.piod_offs = (void *)vm_addr;
173 pi_desc.piod_addr = (void *)buf;
174 pi_desc.piod_len = size;
175
176 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
177 error.SetErrorToErrno();
178 return pi_desc.piod_len;
179}
180
181// Simple helper function to ensure flags are enabled on the given file
182// descriptor.
183static bool
184EnsureFDFlags(int fd, int flags, Error &error)
185{
186 int status;
187
188 if ((status = fcntl(fd, F_GETFL)) == -1)
189 {
190 error.SetErrorToErrno();
191 return false;
192 }
193
194 if (fcntl(fd, F_SETFL, status | flags) == -1)
195 {
196 error.SetErrorToErrno();
197 return false;
198 }
199
200 return true;
201}
202
203//------------------------------------------------------------------------------
204/// @class Operation
205/// @brief Represents a ProcessMonitor operation.
206///
207/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
208/// one that spawned or attached to the process from the start. Therefore, when
209/// a ProcessMonitor is asked to deliver or change the state of an inferior
210/// process the operation must be "funneled" to a specific thread to perform the
211/// task. The Operation class provides an abstract base for all services the
212/// ProcessMonitor must perform via the single virtual function Execute, thus
213/// encapsulating the code that needs to run in the privileged context.
214class Operation
215{
216public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000217 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 virtual void Execute(ProcessMonitor *monitor) = 0;
219};
220
221//------------------------------------------------------------------------------
222/// @class ReadOperation
223/// @brief Implements ProcessMonitor::ReadMemory.
224class ReadOperation : public Operation
225{
226public:
227 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
228 Error &error, size_t &result)
229 : m_addr(addr), m_buff(buff), m_size(size),
230 m_error(error), m_result(result)
231 { }
232
233 void Execute(ProcessMonitor *monitor);
234
235private:
236 lldb::addr_t m_addr;
237 void *m_buff;
238 size_t m_size;
239 Error &m_error;
240 size_t &m_result;
241};
242
243void
244ReadOperation::Execute(ProcessMonitor *monitor)
245{
246 lldb::pid_t pid = monitor->GetPID();
247
248 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
249}
250
251//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000252/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000253/// @brief Implements ProcessMonitor::WriteMemory.
254class WriteOperation : public Operation
255{
256public:
257 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
258 Error &error, size_t &result)
259 : m_addr(addr), m_buff(buff), m_size(size),
260 m_error(error), m_result(result)
261 { }
262
263 void Execute(ProcessMonitor *monitor);
264
265private:
266 lldb::addr_t m_addr;
267 const void *m_buff;
268 size_t m_size;
269 Error &m_error;
270 size_t &m_result;
271};
272
273void
274WriteOperation::Execute(ProcessMonitor *monitor)
275{
276 lldb::pid_t pid = monitor->GetPID();
277
278 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
279}
280
281//------------------------------------------------------------------------------
282/// @class ReadRegOperation
283/// @brief Implements ProcessMonitor::ReadRegisterValue.
284class ReadRegOperation : public Operation
285{
286public:
Ed Maste6f066412013-07-04 21:47:32 +0000287 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
288 RegisterValue &value, bool &result)
289 : m_tid(tid), m_offset(offset), m_size(size),
290 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000291 { }
292
293 void Execute(ProcessMonitor *monitor);
294
295private:
Ed Maste6f066412013-07-04 21:47:32 +0000296 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000297 unsigned m_offset;
298 unsigned m_size;
299 RegisterValue &m_value;
300 bool &m_result;
301};
302
303void
304ReadRegOperation::Execute(ProcessMonitor *monitor)
305{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000306 struct reg regs;
307 int rc;
308
Ed Maste6f066412013-07-04 21:47:32 +0000309 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000310 m_result = false;
311 } else {
Justin Hibbits89e6f382014-11-12 15:14:08 +0000312 // 'struct reg' contains only 32- or 64-bit register values. Punt on
313 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
314 // processes on powerpc64 (probably the same for i386 on amd64)
315 if (m_size == sizeof(uint32_t))
316 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
317 else if (m_size == sizeof(uint64_t))
318 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
319 else
Davide Italiano6bed7192016-01-04 19:22:35 +0000320 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000321 m_result = true;
322 }
323}
324
325//------------------------------------------------------------------------------
326/// @class WriteRegOperation
327/// @brief Implements ProcessMonitor::WriteRegisterValue.
328class WriteRegOperation : public Operation
329{
330public:
Ed Maste6f066412013-07-04 21:47:32 +0000331 WriteRegOperation(lldb::tid_t tid, unsigned offset,
332 const RegisterValue &value, bool &result)
333 : m_tid(tid), m_offset(offset),
334 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000335 { }
336
337 void Execute(ProcessMonitor *monitor);
338
339private:
Ed Maste6f066412013-07-04 21:47:32 +0000340 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000341 unsigned m_offset;
342 const RegisterValue &m_value;
343 bool &m_result;
344};
345
346void
347WriteRegOperation::Execute(ProcessMonitor *monitor)
348{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000349 struct reg regs;
350
Ed Maste6f066412013-07-04 21:47:32 +0000351 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000352 m_result = false;
353 return;
354 }
355 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000356 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000357 m_result = false;
358 else
359 m_result = true;
360}
361
362//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000363/// @class ReadDebugRegOperation
364/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
365class ReadDebugRegOperation : public Operation
366{
367public:
368 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
369 RegisterValue &value, bool &result)
370 : m_tid(tid), m_offset(offset), m_size(size),
371 m_value(value), m_result(result)
372 { }
373
374 void Execute(ProcessMonitor *monitor);
375
376private:
377 lldb::tid_t m_tid;
378 unsigned m_offset;
379 unsigned m_size;
380 RegisterValue &m_value;
381 bool &m_result;
382};
383
384void
385ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
386{
387 struct dbreg regs;
388 int rc;
389
390 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
391 m_result = false;
392 } else {
393 if (m_size == sizeof(uintptr_t))
394 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
395 else
Davide Italiano6bed7192016-01-04 19:22:35 +0000396 memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
Ed Mastea4be2c52014-02-19 18:34:06 +0000397 m_result = true;
398 }
399}
400
401//------------------------------------------------------------------------------
402/// @class WriteDebugRegOperation
403/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
404class WriteDebugRegOperation : public Operation
405{
406public:
407 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
408 const RegisterValue &value, bool &result)
409 : m_tid(tid), m_offset(offset),
410 m_value(value), m_result(result)
411 { }
412
413 void Execute(ProcessMonitor *monitor);
414
415private:
416 lldb::tid_t m_tid;
417 unsigned m_offset;
418 const RegisterValue &m_value;
419 bool &m_result;
420};
421
422void
423WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
424{
425 struct dbreg regs;
426
427 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
428 m_result = false;
429 return;
430 }
431 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
432 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
433 m_result = false;
434 else
435 m_result = true;
436}
437
438//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000439/// @class ReadGPROperation
440/// @brief Implements ProcessMonitor::ReadGPR.
441class ReadGPROperation : public Operation
442{
443public:
Ed Maste6f066412013-07-04 21:47:32 +0000444 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
445 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000446 { }
447
448 void Execute(ProcessMonitor *monitor);
449
450private:
Ed Maste6f066412013-07-04 21:47:32 +0000451 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000452 void *m_buf;
453 bool &m_result;
454};
455
456void
457ReadGPROperation::Execute(ProcessMonitor *monitor)
458{
459 int rc;
460
461 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000462 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000463 if (errno != 0)
464 m_result = false;
465 else
466 m_result = true;
467}
468
469//------------------------------------------------------------------------------
470/// @class ReadFPROperation
471/// @brief Implements ProcessMonitor::ReadFPR.
472class ReadFPROperation : public Operation
473{
474public:
Ed Maste6f066412013-07-04 21:47:32 +0000475 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
476 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000477 { }
478
479 void Execute(ProcessMonitor *monitor);
480
481private:
Ed Maste6f066412013-07-04 21:47:32 +0000482 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000483 void *m_buf;
484 bool &m_result;
485};
486
487void
488ReadFPROperation::Execute(ProcessMonitor *monitor)
489{
Ed Maste6f066412013-07-04 21:47:32 +0000490 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000491 m_result = false;
492 else
493 m_result = true;
494}
495
496//------------------------------------------------------------------------------
497/// @class WriteGPROperation
498/// @brief Implements ProcessMonitor::WriteGPR.
499class WriteGPROperation : public Operation
500{
501public:
Ed Maste6f066412013-07-04 21:47:32 +0000502 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
503 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000504 { }
505
506 void Execute(ProcessMonitor *monitor);
507
508private:
Ed Maste6f066412013-07-04 21:47:32 +0000509 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000510 void *m_buf;
511 bool &m_result;
512};
513
514void
515WriteGPROperation::Execute(ProcessMonitor *monitor)
516{
Ed Maste6f066412013-07-04 21:47:32 +0000517 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000518 m_result = false;
519 else
520 m_result = true;
521}
522
523//------------------------------------------------------------------------------
524/// @class WriteFPROperation
525/// @brief Implements ProcessMonitor::WriteFPR.
526class WriteFPROperation : public Operation
527{
528public:
Ed Maste6f066412013-07-04 21:47:32 +0000529 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
530 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000531 { }
532
533 void Execute(ProcessMonitor *monitor);
534
535private:
Ed Maste6f066412013-07-04 21:47:32 +0000536 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000537 void *m_buf;
538 bool &m_result;
539};
540
541void
542WriteFPROperation::Execute(ProcessMonitor *monitor)
543{
Ed Maste6f066412013-07-04 21:47:32 +0000544 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000545 m_result = false;
546 else
547 m_result = true;
548}
549
550//------------------------------------------------------------------------------
551/// @class ResumeOperation
552/// @brief Implements ProcessMonitor::Resume.
553class ResumeOperation : public Operation
554{
555public:
Ed Maste502f9022013-11-25 16:31:23 +0000556 ResumeOperation(uint32_t signo, bool &result) :
557 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000558
559 void Execute(ProcessMonitor *monitor);
560
561private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000562 uint32_t m_signo;
563 bool &m_result;
564};
565
566void
567ResumeOperation::Execute(ProcessMonitor *monitor)
568{
Ed Maste502f9022013-11-25 16:31:23 +0000569 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000570 int data = 0;
571
572 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
573 data = m_signo;
574
Ed Maste502f9022013-11-25 16:31:23 +0000575 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000576 {
577 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
578
579 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000580 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000581 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000582 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000583 else
584 m_result = true;
585}
586
587//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000588/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000589/// @brief Implements ProcessMonitor::SingleStep.
590class SingleStepOperation : public Operation
591{
592public:
Ed Maste502f9022013-11-25 16:31:23 +0000593 SingleStepOperation(uint32_t signo, bool &result)
594 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000595
596 void Execute(ProcessMonitor *monitor);
597
598private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000599 uint32_t m_signo;
600 bool &m_result;
601};
602
603void
604SingleStepOperation::Execute(ProcessMonitor *monitor)
605{
Ed Maste502f9022013-11-25 16:31:23 +0000606 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000607 int data = 0;
608
609 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
610 data = m_signo;
611
Ed Maste502f9022013-11-25 16:31:23 +0000612 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000613 m_result = false;
614 else
615 m_result = true;
616}
617
618//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000619/// @class LwpInfoOperation
620/// @brief Implements ProcessMonitor::GetLwpInfo.
621class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000622{
623public:
Ed Maste819e3992013-07-17 14:02:20 +0000624 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000625 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000626
627 void Execute(ProcessMonitor *monitor);
628
629private:
630 lldb::tid_t m_tid;
631 void *m_info;
632 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000633 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000634};
635
636void
Ed Maste819e3992013-07-17 14:02:20 +0000637LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000638{
639 struct ptrace_lwpinfo plwp;
640
Daniel Maleaa35970a2012-11-23 18:09:58 +0000641 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000642 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000643 m_err = errno;
644 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000645 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000646 m_result = true;
647 }
648}
649
650//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000651/// @class ThreadSuspendOperation
652/// @brief Implements ProcessMonitor::ThreadSuspend.
653class ThreadSuspendOperation : public Operation
654{
655public:
656 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
657 : m_tid(tid), m_suspend(suspend), m_result(result) { }
658
659 void Execute(ProcessMonitor *monitor);
660
661private:
662 lldb::tid_t m_tid;
663 bool m_suspend;
664 bool &m_result;
665} ;
666
667void
668ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
669{
670 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
671}
672
673
674
675//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000676/// @class EventMessageOperation
677/// @brief Implements ProcessMonitor::GetEventMessage.
678class EventMessageOperation : public Operation
679{
680public:
681 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
682 : m_tid(tid), m_message(message), m_result(result) { }
683
684 void Execute(ProcessMonitor *monitor);
685
686private:
687 lldb::tid_t m_tid;
688 unsigned long *m_message;
689 bool &m_result;
690};
691
692void
693EventMessageOperation::Execute(ProcessMonitor *monitor)
694{
695 struct ptrace_lwpinfo plwp;
696
697 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
698 m_result = false;
699 else {
700 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000701 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000702 m_result = true;
703 } else
704 m_result = false;
705 }
706}
707
708//------------------------------------------------------------------------------
709/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000710/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000711class KillOperation : public Operation
712{
713public:
714 KillOperation(bool &result) : m_result(result) { }
715
716 void Execute(ProcessMonitor *monitor);
717
718private:
719 bool &m_result;
720};
721
722void
723KillOperation::Execute(ProcessMonitor *monitor)
724{
725 lldb::pid_t pid = monitor->GetPID();
726
727 if (PTRACE(PT_KILL, pid, NULL, 0))
728 m_result = false;
729 else
730 m_result = true;
731}
732
733//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000734/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000735/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000736class DetachOperation : public Operation
737{
738public:
739 DetachOperation(Error &result) : m_error(result) { }
740
741 void Execute(ProcessMonitor *monitor);
742
743private:
744 Error &m_error;
745};
746
747void
748DetachOperation::Execute(ProcessMonitor *monitor)
749{
750 lldb::pid_t pid = monitor->GetPID();
751
752 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
753 m_error.SetErrorToErrno();
754
755}
756
757ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
758 : m_monitor(monitor)
759{
760 sem_init(&m_semaphore, 0, 0);
761}
762
763ProcessMonitor::OperationArgs::~OperationArgs()
764{
765 sem_destroy(&m_semaphore);
766}
767
768ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
769 lldb_private::Module *module,
770 char const **argv,
771 char const **envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000772 const FileSpec &stdin_file_spec,
773 const FileSpec &stdout_file_spec,
774 const FileSpec &stderr_file_spec,
775 const FileSpec &working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000776 : OperationArgs(monitor),
777 m_module(module),
778 m_argv(argv),
779 m_envp(envp),
Ed Maste41fba2b2015-06-01 15:24:37 +0000780 m_stdin_file_spec(stdin_file_spec),
781 m_stdout_file_spec(stdout_file_spec),
782 m_stderr_file_spec(stderr_file_spec),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000783 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000784
785ProcessMonitor::LaunchArgs::~LaunchArgs()
786{ }
787
788ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
789 lldb::pid_t pid)
790 : OperationArgs(monitor), m_pid(pid) { }
791
792ProcessMonitor::AttachArgs::~AttachArgs()
793{ }
794
795//------------------------------------------------------------------------------
796/// The basic design of the ProcessMonitor is built around two threads.
797///
798/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
799/// for changes in the debugee state. When a change is detected a
800/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
801/// "drives" state changes in the debugger.
802///
803/// The second thread (@see OperationThread) is responsible for two things 1)
804/// launching or attaching to the inferior process, and then 2) servicing
805/// operations such as register reads/writes, stepping, etc. See the comments
806/// on the Operation class for more info as to why this is needed.
Ed Mastefe5a6422015-07-28 15:45:57 +0000807ProcessMonitor::ProcessMonitor(ProcessFreeBSD *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000808 Module *module,
809 const char *argv[],
810 const char *envp[],
Ed Maste41fba2b2015-06-01 15:24:37 +0000811 const FileSpec &stdin_file_spec,
812 const FileSpec &stdout_file_spec,
813 const FileSpec &stderr_file_spec,
814 const FileSpec &working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000815 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000816 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000817 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000818 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000819 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000820 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821{
Pavel Labath998bdc52016-05-11 16:59:04 +0000822 using namespace std::placeholders;
823
Ed Maste756e1ff2013-09-18 19:34:08 +0000824 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000825 stdin_file_spec,
826 stdout_file_spec,
827 stderr_file_spec,
828 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829
830
Ed Maste756e1ff2013-09-18 19:34:08 +0000831 sem_init(&m_operation_pending, 0, 0);
832 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000833
834 StartLaunchOpThread(args.get(), error);
835 if (!error.Success())
836 return;
837
838WAIT_AGAIN:
839 // Wait for the operation thread to initialize.
840 if (sem_wait(&args->m_semaphore))
841 {
842 if (errno == EINTR)
843 goto WAIT_AGAIN;
844 else
845 {
846 error.SetErrorToErrno();
847 return;
848 }
849 }
850
851 // Check that the launch was a success.
852 if (!args->m_error.Success())
853 {
Ed Mastea02f5532013-07-02 16:45:16 +0000854 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000855 error = args->m_error;
856 return;
857 }
858
859 // Finally, start monitoring the child process for change in state.
860 m_monitor_thread = Host::StartMonitoringChildProcess(
Pavel Labath998bdc52016-05-11 16:59:04 +0000861 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), 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
Ed Mastefe5a6422015-07-28 15:45:57 +0000870ProcessMonitor::ProcessMonitor(ProcessFreeBSD *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{
Pavel Labath998bdc52016-05-11 16:59:04 +0000878 using namespace std::placeholders;
879
Ed Maste756e1ff2013-09-18 19:34:08 +0000880 sem_init(&m_operation_pending, 0, 0);
881 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000882
Johnny Chen9ed5b492012-01-05 21:48:15 +0000883
Ed Maste756e1ff2013-09-18 19:34:08 +0000884 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000885
886 StartAttachOpThread(args.get(), error);
887 if (!error.Success())
888 return;
889
890WAIT_AGAIN:
891 // Wait for the operation thread to initialize.
892 if (sem_wait(&args->m_semaphore))
893 {
894 if (errno == EINTR)
895 goto WAIT_AGAIN;
896 else
897 {
898 error.SetErrorToErrno();
899 return;
900 }
901 }
902
Ed Mastea02f5532013-07-02 16:45:16 +0000903 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000904 if (!args->m_error.Success())
905 {
Ed Mastea02f5532013-07-02 16:45:16 +0000906 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000907 error = args->m_error;
908 return;
909 }
910
911 // Finally, start monitoring the child process for change in state.
912 m_monitor_thread = Host::StartMonitoringChildProcess(
Pavel Labath998bdc52016-05-11 16:59:04 +0000913 std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000914 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000915 {
916 error.SetErrorToGenericError();
917 error.SetErrorString("Process attach failed.");
918 return;
919 }
920}
921
922ProcessMonitor::~ProcessMonitor()
923{
924 StopMonitor();
925}
926
927//------------------------------------------------------------------------------
928// Thread setup and tear down.
929void
930ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
931{
932 static const char *g_thread_name = "lldb.process.freebsd.operation";
933
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000934 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000935 return;
936
Zachary Turner39de3112014-09-09 20:54:56 +0000937 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000938}
939
Johnny Chen9ed5b492012-01-05 21:48:15 +0000940void *
941ProcessMonitor::LaunchOpThread(void *arg)
942{
943 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
944
945 if (!Launch(args)) {
946 sem_post(&args->m_semaphore);
947 return NULL;
948 }
949
950 ServeOperation(args);
951 return NULL;
952}
953
954bool
955ProcessMonitor::Launch(LaunchArgs *args)
956{
957 ProcessMonitor *monitor = args->m_monitor;
958 ProcessFreeBSD &process = monitor->GetProcess();
959 const char **argv = args->m_argv;
960 const char **envp = args->m_envp;
Ed Maste41fba2b2015-06-01 15:24:37 +0000961 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
962 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
963 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
964 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000965
966 lldb_utility::PseudoTerminal terminal;
967 const size_t err_len = 1024;
968 char err_str[err_len];
Davide Italianocd9f7b82015-03-22 23:43:58 +0000969 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000970
Johnny Chen9ed5b492012-01-05 21:48:15 +0000971 // Propagate the environment if one is not supplied.
972 if (envp == NULL || envp[0] == NULL)
973 envp = const_cast<const char **>(environ);
974
Ed Mastea02f5532013-07-02 16:45:16 +0000975 if ((pid = terminal.Fork(err_str, err_len)) == -1)
976 {
977 args->m_error.SetErrorToGenericError();
978 args->m_error.SetErrorString("Process fork failed.");
979 goto FINISH;
980 }
981
Johnny Chen9ed5b492012-01-05 21:48:15 +0000982 // Recognized child exit status codes.
983 enum {
984 ePtraceFailed = 1,
985 eDupStdinFailed,
986 eDupStdoutFailed,
987 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000988 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000989 eExecFailed,
990 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000991 };
992
Johnny Chen9ed5b492012-01-05 21:48:15 +0000993 // Child process.
994 if (pid == 0)
995 {
996 // Trace this process.
997 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
998 exit(ePtraceFailed);
999
Ed Maste7f0230f2015-02-05 16:09:03 +00001000 // terminal has already dupped the tty descriptors to stdin/out/err.
1001 // This closes original fd from which they were copied (and avoids
1002 // leaking descriptors to the debugged process.
1003 terminal.CloseSlaveFileDescriptor();
1004
Johnny Chen9ed5b492012-01-05 21:48:15 +00001005 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +00001006 if (setgid(getgid()) != 0)
1007 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001008
1009 // Let us have our own process group.
1010 setpgid(0, 0);
1011
1012 // Dup file descriptors if needed.
1013 //
1014 // FIXME: If two or more of the paths are the same we needlessly open
1015 // the same file multiple times.
Ed Maste41fba2b2015-06-01 15:24:37 +00001016 if (stdin_file_spec)
1017 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018 exit(eDupStdinFailed);
1019
Ed Maste41fba2b2015-06-01 15:24:37 +00001020 if (stdout_file_spec)
1021 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001022 exit(eDupStdoutFailed);
1023
Ed Maste41fba2b2015-06-01 15:24:37 +00001024 if (stderr_file_spec)
1025 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001026 exit(eDupStderrFailed);
1027
Daniel Malea6217d2a2013-01-08 14:49:22 +00001028 // Change working directory
Ed Maste41fba2b2015-06-01 15:24:37 +00001029 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
1030 exit(eChdirFailed);
Daniel Malea6217d2a2013-01-08 14:49:22 +00001031
Johnny Chen9ed5b492012-01-05 21:48:15 +00001032 // Execute. We should never return.
1033 execve(argv[0],
1034 const_cast<char *const *>(argv),
1035 const_cast<char *const *>(envp));
1036 exit(eExecFailed);
1037 }
1038
1039 // Wait for the child process to to trap on its call to execve.
1040 ::pid_t wpid;
1041 int status;
1042 if ((wpid = waitpid(pid, &status, 0)) < 0)
1043 {
1044 args->m_error.SetErrorToErrno();
1045 goto FINISH;
1046 }
1047 else if (WIFEXITED(status))
1048 {
1049 // open, dup or execve likely failed for some reason.
1050 args->m_error.SetErrorToGenericError();
1051 switch (WEXITSTATUS(status))
1052 {
Ed Maste5d34af32013-06-24 15:09:18 +00001053 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001054 args->m_error.SetErrorString("Child ptrace failed.");
1055 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001056 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001057 args->m_error.SetErrorString("Child open stdin failed.");
1058 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001059 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001060 args->m_error.SetErrorString("Child open stdout failed.");
1061 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001062 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001063 args->m_error.SetErrorString("Child open stderr failed.");
1064 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001065 case eChdirFailed:
1066 args->m_error.SetErrorString("Child failed to set working directory.");
1067 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001068 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001069 args->m_error.SetErrorString("Child exec failed.");
1070 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001071 case eSetGidFailed:
1072 args->m_error.SetErrorString("Child setgid failed.");
1073 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001074 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001075 args->m_error.SetErrorString("Child returned unknown exit status.");
1076 break;
1077 }
1078 goto FINISH;
1079 }
Ed Maste82a00052013-11-25 21:15:53 +00001080 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001081 "Could not sync with inferior process.");
1082
1083#ifdef notyet
1084 // Have the child raise an event on exit. This is used to keep the child in
1085 // limbo until it is destroyed.
1086 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1087 {
1088 args->m_error.SetErrorToErrno();
1089 goto FINISH;
1090 }
1091#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001092 // Release the master terminal descriptor and pass it off to the
1093 // ProcessMonitor instance. Similarly stash the inferior pid.
1094 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001095 monitor->m_pid = pid;
1096
1097 // Set the terminal fd to be in non blocking mode (it simplifies the
1098 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1099 // descriptor to read from).
1100 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1101 goto FINISH;
1102
Ed Mastee5441432013-09-03 23:55:30 +00001103 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001104
1105FINISH:
1106 return args->m_error.Success();
1107}
1108
Johnny Chen9ed5b492012-01-05 21:48:15 +00001109void
1110ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1111{
1112 static const char *g_thread_name = "lldb.process.freebsd.operation";
1113
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001114 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001115 return;
1116
Zachary Turner39de3112014-09-09 20:54:56 +00001117 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001118}
1119
Johnny Chen9ed5b492012-01-05 21:48:15 +00001120void *
1121ProcessMonitor::AttachOpThread(void *arg)
1122{
1123 AttachArgs *args = static_cast<AttachArgs*>(arg);
1124
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001125 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001126
1127 ServeOperation(args);
1128 return NULL;
1129}
1130
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001131void
Johnny Chen9ed5b492012-01-05 21:48:15 +00001132ProcessMonitor::Attach(AttachArgs *args)
1133{
1134 lldb::pid_t pid = args->m_pid;
1135
1136 ProcessMonitor *monitor = args->m_monitor;
1137 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001138
1139 if (pid <= 1)
1140 {
1141 args->m_error.SetErrorToGenericError();
1142 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001143 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001144 }
1145
1146 // Attach to the requested process.
1147 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1148 {
1149 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001150 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001151 }
1152
1153 int status;
1154 if ((status = waitpid(pid, NULL, 0)) < 0)
1155 {
1156 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001157 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001158 }
1159
Ed Mastee5441432013-09-03 23:55:30 +00001160 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001161}
1162
Ed Maste7fd845c2013-12-09 15:51:17 +00001163size_t
1164ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1165{
1166 lwpid_t *tids;
1167 int tdcnt;
1168
1169 thread_ids.clear();
1170
1171 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1172 if (tdcnt <= 0)
1173 return 0;
1174 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1175 if (tids == NULL)
1176 return 0;
1177 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1178 free(tids);
1179 return 0;
1180 }
1181 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1182 free(tids);
1183 return thread_ids.size();
1184}
1185
Johnny Chen9ed5b492012-01-05 21:48:15 +00001186bool
Pavel Labath998bdc52016-05-11 16:59:04 +00001187ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001188{
1189 ProcessMessage message;
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001190 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001191 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001192 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001193 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001194 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001195
Ed Mastea02f5532013-07-02 16:45:16 +00001196 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1197
1198 if (exited)
1199 {
1200 if (log)
1201 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1202 message = ProcessMessage::Exit(pid, status);
1203 process->SendMessage(message);
1204 return pid == process->GetID();
1205 }
1206
Ed Maste819e3992013-07-17 14:02:20 +00001207 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001208 stop_monitoring = true; // pid is gone. Bail.
1209 else {
Ed Maste819e3992013-07-17 14:02:20 +00001210 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001211 {
1212 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001213 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001214 break;
1215
1216 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001217 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001218 break;
1219 }
1220
1221 process->SendMessage(message);
1222 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1223 }
1224
1225 return stop_monitoring;
1226}
1227
1228ProcessMessage
1229ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001230 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001231{
1232 ProcessMessage message;
1233
Ed Mastea02f5532013-07-02 16:45:16 +00001234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1235
Matt Kopec7de48462013-03-06 17:20:48 +00001236 assert(monitor);
1237 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001238
1239 switch (info->si_code)
1240 {
1241 default:
1242 assert(false && "Unexpected SIGTRAP code!");
1243 break;
1244
1245 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1246 {
1247 // The inferior process is about to exit. Maintain the process in a
1248 // state of "limbo" until we are explicitly commanded to detach,
1249 // destroy, resume, etc.
1250 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001251 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001252 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001253 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001254 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1255 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001256 break;
1257 }
1258
1259 case 0:
1260 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001261 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001262 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001263 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001264 break;
1265
1266 case SI_KERNEL:
1267 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001268 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001269 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1270 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001271 break;
1272 }
1273
1274 return message;
1275}
1276
1277ProcessMessage
1278ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001279 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001280{
1281 ProcessMessage message;
1282 int signo = info->si_signo;
1283
Ed Mastea02f5532013-07-02 16:45:16 +00001284 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1285
Johnny Chen9ed5b492012-01-05 21:48:15 +00001286 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1287 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1288 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1289 //
1290 // IOW, user generated signals never generate what we consider to be a
1291 // "crash".
1292 //
1293 // Similarly, ACK signals generated by this monitor.
1294 if (info->si_code == SI_USER)
1295 {
Ed Mastea02f5532013-07-02 16:45:16 +00001296 if (log)
1297 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1298 __FUNCTION__,
Chaoren Line515a5a2015-07-14 03:18:23 +00001299 monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo),
Ed Mastea02f5532013-07-02 16:45:16 +00001300 "SI_USER",
1301 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001302 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001303 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001304 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001305 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001306 }
1307
Ed Mastea02f5532013-07-02 16:45:16 +00001308 if (log)
Chaoren Line515a5a2015-07-14 03:18:23 +00001309 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo));
Ed Mastea02f5532013-07-02 16:45:16 +00001310
Chaoren Lin28e57422015-02-03 01:51:25 +00001311 switch (signo)
1312 {
1313 case SIGSEGV:
1314 case SIGILL:
1315 case SIGFPE:
1316 case SIGBUS:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001317 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001318 const auto reason = GetCrashReason(*info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001319 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001320 }
1321
1322 // Everything else is "normal" and does not require any special action on
1323 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001324 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001325}
1326
Johnny Chen9ed5b492012-01-05 21:48:15 +00001327void
1328ProcessMonitor::ServeOperation(OperationArgs *args)
1329{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001330 ProcessMonitor *monitor = args->m_monitor;
1331
Johnny Chen9ed5b492012-01-05 21:48:15 +00001332 // We are finised with the arguments and are ready to go. Sync with the
1333 // parent thread and start serving operations on the inferior.
1334 sem_post(&args->m_semaphore);
1335
1336 for (;;)
1337 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001338 // wait for next pending operation
1339 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001340
Ed Maste756e1ff2013-09-18 19:34:08 +00001341 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001342
Ed Maste756e1ff2013-09-18 19:34:08 +00001343 // notify calling thread that operation is complete
1344 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001345 }
1346}
1347
1348void
1349ProcessMonitor::DoOperation(Operation *op)
1350{
Ed Maste756e1ff2013-09-18 19:34:08 +00001351 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001352
Ed Maste756e1ff2013-09-18 19:34:08 +00001353 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001354
Ed Maste756e1ff2013-09-18 19:34:08 +00001355 // notify operation thread that an operation is ready to be processed
1356 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001357
Ed Maste756e1ff2013-09-18 19:34:08 +00001358 // wait for operation to complete
1359 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001360}
1361
1362size_t
1363ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1364 Error &error)
1365{
1366 size_t result;
1367 ReadOperation op(vm_addr, buf, size, error, result);
1368 DoOperation(&op);
1369 return result;
1370}
1371
1372size_t
1373ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1374 lldb_private::Error &error)
1375{
1376 size_t result;
1377 WriteOperation op(vm_addr, buf, size, error, result);
1378 DoOperation(&op);
1379 return result;
1380}
1381
1382bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001383ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001384 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001385{
1386 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001387 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001388 DoOperation(&op);
1389 return result;
1390}
1391
1392bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001393ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001394 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001395{
1396 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001397 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001398 DoOperation(&op);
1399 return result;
1400}
1401
1402bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001403ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1404 const char *reg_name, unsigned size,
1405 lldb_private::RegisterValue &value)
1406{
1407 bool result;
1408 ReadDebugRegOperation op(tid, offset, size, value, result);
1409 DoOperation(&op);
1410 return result;
1411}
1412
1413bool
1414ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1415 const char *reg_name,
1416 const lldb_private::RegisterValue &value)
1417{
1418 bool result;
1419 WriteDebugRegOperation op(tid, offset, value, result);
1420 DoOperation(&op);
1421 return result;
1422}
1423
1424bool
Matt Kopec7de48462013-03-06 17:20:48 +00001425ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001426{
1427 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001428 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001429 DoOperation(&op);
1430 return result;
1431}
1432
1433bool
Matt Kopec7de48462013-03-06 17:20:48 +00001434ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001435{
1436 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001437 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001438 DoOperation(&op);
1439 return result;
1440}
1441
1442bool
Ed Maste5d34af32013-06-24 15:09:18 +00001443ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1444{
1445 return false;
1446}
1447
1448bool
Matt Kopec7de48462013-03-06 17:20:48 +00001449ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001450{
1451 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001452 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001453 DoOperation(&op);
1454 return result;
1455}
1456
1457bool
Matt Kopec7de48462013-03-06 17:20:48 +00001458ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001459{
1460 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001461 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001462 DoOperation(&op);
1463 return result;
1464}
1465
1466bool
Ed Maste5d34af32013-06-24 15:09:18 +00001467ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1468{
1469 return false;
1470}
1471
1472bool
Ed Maste68f51792013-10-18 19:16:44 +00001473ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1474{
1475 return false;
1476}
1477
1478bool
Ed Maste502f9022013-11-25 16:31:23 +00001479ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001480{
1481 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001482 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1483
Ed Maste4aeb3c02014-05-28 14:11:20 +00001484 if (log) {
Chaoren Line515a5a2015-07-14 03:18:23 +00001485 const char *signame = m_process->GetUnixSignals()->GetSignalAsCString (signo);
Ed Maste4aeb3c02014-05-28 14:11:20 +00001486 if (signame == nullptr)
1487 signame = "<none>";
1488 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1489 __FUNCTION__, GetPID(), signame);
1490 }
Ed Maste502f9022013-11-25 16:31:23 +00001491 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001492 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001493 if (log)
1494 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001495 return result;
1496}
1497
1498bool
Ed Maste502f9022013-11-25 16:31:23 +00001499ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001500{
1501 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001502 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001503 DoOperation(&op);
1504 return result;
1505}
1506
1507bool
Ed Maste70882932014-04-01 14:30:56 +00001508ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001509{
1510 bool result;
1511 KillOperation op(result);
1512 DoOperation(&op);
1513 return result;
1514}
1515
1516bool
Ed Maste819e3992013-07-17 14:02:20 +00001517ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001518{
1519 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001520 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001521 DoOperation(&op);
1522 return result;
1523}
1524
1525bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001526ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1527{
1528 bool result;
1529 ThreadSuspendOperation op(tid, suspend, result);
1530 DoOperation(&op);
1531 return result;
1532}
1533
1534bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001535ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1536{
1537 bool result;
1538 EventMessageOperation op(tid, message, result);
1539 DoOperation(&op);
1540 return result;
1541}
1542
Ed Mastea02f5532013-07-02 16:45:16 +00001543lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001544ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001545{
Ed Mastea02f5532013-07-02 16:45:16 +00001546 lldb_private::Error error;
1547 if (tid != LLDB_INVALID_THREAD_ID)
1548 {
1549 DetachOperation op(error);
1550 DoOperation(&op);
1551 }
1552 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001553}
1554
1555bool
Ed Maste41fba2b2015-06-01 15:24:37 +00001556ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd, int flags)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001557{
Ed Maste41fba2b2015-06-01 15:24:37 +00001558 int target_fd = open(file_spec.GetCString(), flags, 0666);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001559
1560 if (target_fd == -1)
1561 return false;
1562
Ed Maste7f0230f2015-02-05 16:09:03 +00001563 if (dup2(target_fd, fd) == -1)
1564 return false;
1565
1566 return (close(target_fd) == -1) ? false : true;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001567}
1568
1569void
1570ProcessMonitor::StopMonitoringChildProcess()
1571{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001572 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001573 {
Zachary Turner39de3112014-09-09 20:54:56 +00001574 m_monitor_thread.Cancel();
1575 m_monitor_thread.Join(nullptr);
1576 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001577 }
1578}
1579
1580void
1581ProcessMonitor::StopMonitor()
1582{
1583 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001584 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001585 sem_destroy(&m_operation_pending);
1586 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00001587 if (m_terminal_fd >= 0) {
1588 close(m_terminal_fd);
1589 m_terminal_fd = -1;
1590 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001591}
1592
Andrew Kaylord4d54992013-09-17 00:30:24 +00001593// FIXME: On Linux, when a new thread is created, we receive to notifications,
1594// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1595// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1596// the new child thread indicating that it has is stopped because we attached.
1597// We have no guarantee of the order in which these arrive, but we need both
1598// before we are ready to proceed. We currently keep a list of threads which
1599// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1600// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1601// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1602//
1603// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1604// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1605// logically needed.
1606//
1607// We really should figure out what actually happens on FreeBSD and move the
1608// Linux-specific logic out of ProcessPOSIX as needed.
1609
1610bool
1611ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1612{
1613 return true;
1614}
1615
Johnny Chen9ed5b492012-01-05 21:48:15 +00001616void
Ed Mastea02f5532013-07-02 16:45:16 +00001617ProcessMonitor::StopOpThread()
1618{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001619 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001620 return;
1621
Zachary Turner39de3112014-09-09 20:54:56 +00001622 m_operation_thread.Cancel();
1623 m_operation_thread.Join(nullptr);
1624 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001625}