blob: 427c66ce3e6b3c447a5f4ceded1ab7df2543f2ef [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <errno.h>
12#include <poll.h>
13#include <string.h>
Ed Mastea02f5532013-07-02 16:45:16 +000014#include <stdint.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000015#include <unistd.h>
16#include <signal.h>
17#include <sys/ptrace.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21
22// C++ Includes
23// Other libraries and framework includes
24#include "lldb/Core/Error.h"
25#include "lldb/Core/RegisterValue.h"
26#include "lldb/Core/Scalar.h"
27#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/RegisterContext.h"
Ed Maste8702e922015-03-03 22:44:18 +000031#include "lldb/Target/UnixSignals.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000032#include "lldb/Utility/PseudoTerminal.h"
33
Chaoren Lin28e57422015-02-03 01:51:25 +000034#include "Plugins/Process/POSIX/CrashReason.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000035#include "POSIXThread.h"
36#include "ProcessFreeBSD.h"
37#include "ProcessPOSIXLog.h"
38#include "ProcessMonitor.h"
39
40extern "C" {
41 extern char ** environ;
42 }
43
44using namespace lldb;
45using namespace lldb_private;
46
47// We disable the tracing of ptrace calls for integration builds to
48// avoid the additional indirection and checks.
49#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
50// Wrapper for ptrace to catch errors and log calls.
51
52const char *
53Get_PT_IO_OP(int op)
54{
55 switch (op) {
56 case PIOD_READ_D: return "READ_D";
57 case PIOD_WRITE_D: return "WRITE_D";
58 case PIOD_READ_I: return "READ_I";
59 case PIOD_WRITE_I: return "WRITE_I";
60 default: return "Unknown op";
61 }
62}
63
Matt Kopec7de48462013-03-06 17:20:48 +000064// Wrapper for ptrace to catch errors and log calls.
65// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000066extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000067PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000068 const char* reqName, const char* file, int line)
69{
70 long int result;
71
Ashok Thirumurthi01186352013-03-28 16:02:31 +000072 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000073
74 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000075 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000076 reqName, pid, addr, data, file, line);
77 if (req == PT_IO) {
78 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
79
Sylvestre Ledru779f9212013-10-31 23:55:19 +000080 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000081 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000082 }
83 }
84
85 //PtraceDisplayBytes(req, data);
86
87 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000088 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000089
90 //PtraceDisplayBytes(req, data);
91
Matt Kopec7de48462013-03-06 17:20:48 +000092 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000093 {
94 const char* str;
95 switch (errno)
96 {
97 case ESRCH: str = "ESRCH"; break;
98 case EINVAL: str = "EINVAL"; break;
99 case EBUSY: str = "EBUSY"; break;
100 case EPERM: str = "EPERM"; break;
101 default: str = "<unknown>";
102 }
103 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
104 }
105
106 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000107#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000108 if (req == PT_GETREGS) {
109 struct reg *r = (struct reg *) addr;
110
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
Johnny Chen9ed5b492012-01-05 21:48:15 +0000320 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
321 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
396 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
397 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.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000807ProcessMonitor::ProcessMonitor(ProcessPOSIX *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{
Ed Maste756e1ff2013-09-18 19:34:08 +0000822 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
Ed Maste41fba2b2015-06-01 15:24:37 +0000823 stdin_file_spec,
824 stdout_file_spec,
825 stderr_file_spec,
826 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000827
828
Ed Maste756e1ff2013-09-18 19:34:08 +0000829 sem_init(&m_operation_pending, 0, 0);
830 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000831
832 StartLaunchOpThread(args.get(), error);
833 if (!error.Success())
834 return;
835
836WAIT_AGAIN:
837 // Wait for the operation thread to initialize.
838 if (sem_wait(&args->m_semaphore))
839 {
840 if (errno == EINTR)
841 goto WAIT_AGAIN;
842 else
843 {
844 error.SetErrorToErrno();
845 return;
846 }
847 }
848
849 // Check that the launch was a success.
850 if (!args->m_error.Success())
851 {
Ed Mastea02f5532013-07-02 16:45:16 +0000852 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000853 error = args->m_error;
854 return;
855 }
856
857 // Finally, start monitoring the child process for change in state.
858 m_monitor_thread = Host::StartMonitoringChildProcess(
859 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000860 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000861 {
862 error.SetErrorToGenericError();
863 error.SetErrorString("Process launch failed.");
864 return;
865 }
866}
867
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000868ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000869 lldb::pid_t pid,
870 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000871 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000872 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000873 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000874 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000875{
Ed Maste756e1ff2013-09-18 19:34:08 +0000876 sem_init(&m_operation_pending, 0, 0);
877 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000878
Johnny Chen9ed5b492012-01-05 21:48:15 +0000879
Ed Maste756e1ff2013-09-18 19:34:08 +0000880 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000881
882 StartAttachOpThread(args.get(), error);
883 if (!error.Success())
884 return;
885
886WAIT_AGAIN:
887 // Wait for the operation thread to initialize.
888 if (sem_wait(&args->m_semaphore))
889 {
890 if (errno == EINTR)
891 goto WAIT_AGAIN;
892 else
893 {
894 error.SetErrorToErrno();
895 return;
896 }
897 }
898
Ed Mastea02f5532013-07-02 16:45:16 +0000899 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000900 if (!args->m_error.Success())
901 {
Ed Mastea02f5532013-07-02 16:45:16 +0000902 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000903 error = args->m_error;
904 return;
905 }
906
907 // Finally, start monitoring the child process for change in state.
908 m_monitor_thread = Host::StartMonitoringChildProcess(
909 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000910 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000911 {
912 error.SetErrorToGenericError();
913 error.SetErrorString("Process attach failed.");
914 return;
915 }
916}
917
918ProcessMonitor::~ProcessMonitor()
919{
920 StopMonitor();
921}
922
923//------------------------------------------------------------------------------
924// Thread setup and tear down.
925void
926ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
927{
928 static const char *g_thread_name = "lldb.process.freebsd.operation";
929
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000930 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000931 return;
932
Zachary Turner39de3112014-09-09 20:54:56 +0000933 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000934}
935
Johnny Chen9ed5b492012-01-05 21:48:15 +0000936void *
937ProcessMonitor::LaunchOpThread(void *arg)
938{
939 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
940
941 if (!Launch(args)) {
942 sem_post(&args->m_semaphore);
943 return NULL;
944 }
945
946 ServeOperation(args);
947 return NULL;
948}
949
950bool
951ProcessMonitor::Launch(LaunchArgs *args)
952{
953 ProcessMonitor *monitor = args->m_monitor;
954 ProcessFreeBSD &process = monitor->GetProcess();
955 const char **argv = args->m_argv;
956 const char **envp = args->m_envp;
Ed Maste41fba2b2015-06-01 15:24:37 +0000957 const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
958 const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
959 const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
960 const FileSpec &working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000961
962 lldb_utility::PseudoTerminal terminal;
963 const size_t err_len = 1024;
964 char err_str[err_len];
Davide Italianocd9f7b82015-03-22 23:43:58 +0000965 ::pid_t pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000966
Johnny Chen9ed5b492012-01-05 21:48:15 +0000967 // Propagate the environment if one is not supplied.
968 if (envp == NULL || envp[0] == NULL)
969 envp = const_cast<const char **>(environ);
970
Ed Mastea02f5532013-07-02 16:45:16 +0000971 if ((pid = terminal.Fork(err_str, err_len)) == -1)
972 {
973 args->m_error.SetErrorToGenericError();
974 args->m_error.SetErrorString("Process fork failed.");
975 goto FINISH;
976 }
977
Johnny Chen9ed5b492012-01-05 21:48:15 +0000978 // Recognized child exit status codes.
979 enum {
980 ePtraceFailed = 1,
981 eDupStdinFailed,
982 eDupStdoutFailed,
983 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000984 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000985 eExecFailed,
986 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000987 };
988
Johnny Chen9ed5b492012-01-05 21:48:15 +0000989 // Child process.
990 if (pid == 0)
991 {
992 // Trace this process.
993 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
994 exit(ePtraceFailed);
995
Ed Maste7f0230f2015-02-05 16:09:03 +0000996 // terminal has already dupped the tty descriptors to stdin/out/err.
997 // This closes original fd from which they were copied (and avoids
998 // leaking descriptors to the debugged process.
999 terminal.CloseSlaveFileDescriptor();
1000
Johnny Chen9ed5b492012-01-05 21:48:15 +00001001 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +00001002 if (setgid(getgid()) != 0)
1003 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001004
1005 // Let us have our own process group.
1006 setpgid(0, 0);
1007
1008 // Dup file descriptors if needed.
1009 //
1010 // FIXME: If two or more of the paths are the same we needlessly open
1011 // the same file multiple times.
Ed Maste41fba2b2015-06-01 15:24:37 +00001012 if (stdin_file_spec)
1013 if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001014 exit(eDupStdinFailed);
1015
Ed Maste41fba2b2015-06-01 15:24:37 +00001016 if (stdout_file_spec)
1017 if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018 exit(eDupStdoutFailed);
1019
Ed Maste41fba2b2015-06-01 15:24:37 +00001020 if (stderr_file_spec)
1021 if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001022 exit(eDupStderrFailed);
1023
Daniel Malea6217d2a2013-01-08 14:49:22 +00001024 // Change working directory
Ed Maste41fba2b2015-06-01 15:24:37 +00001025 if (working_dir && 0 != ::chdir(working_dir.GetCString()))
1026 exit(eChdirFailed);
Daniel Malea6217d2a2013-01-08 14:49:22 +00001027
Johnny Chen9ed5b492012-01-05 21:48:15 +00001028 // Execute. We should never return.
1029 execve(argv[0],
1030 const_cast<char *const *>(argv),
1031 const_cast<char *const *>(envp));
1032 exit(eExecFailed);
1033 }
1034
1035 // Wait for the child process to to trap on its call to execve.
1036 ::pid_t wpid;
1037 int status;
1038 if ((wpid = waitpid(pid, &status, 0)) < 0)
1039 {
1040 args->m_error.SetErrorToErrno();
1041 goto FINISH;
1042 }
1043 else if (WIFEXITED(status))
1044 {
1045 // open, dup or execve likely failed for some reason.
1046 args->m_error.SetErrorToGenericError();
1047 switch (WEXITSTATUS(status))
1048 {
Ed Maste5d34af32013-06-24 15:09:18 +00001049 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001050 args->m_error.SetErrorString("Child ptrace failed.");
1051 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001052 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001053 args->m_error.SetErrorString("Child open stdin failed.");
1054 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001055 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001056 args->m_error.SetErrorString("Child open stdout failed.");
1057 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001058 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001059 args->m_error.SetErrorString("Child open stderr failed.");
1060 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001061 case eChdirFailed:
1062 args->m_error.SetErrorString("Child failed to set working directory.");
1063 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001064 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001065 args->m_error.SetErrorString("Child exec failed.");
1066 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001067 case eSetGidFailed:
1068 args->m_error.SetErrorString("Child setgid failed.");
1069 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001070 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001071 args->m_error.SetErrorString("Child returned unknown exit status.");
1072 break;
1073 }
1074 goto FINISH;
1075 }
Ed Maste82a00052013-11-25 21:15:53 +00001076 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001077 "Could not sync with inferior process.");
1078
1079#ifdef notyet
1080 // Have the child raise an event on exit. This is used to keep the child in
1081 // limbo until it is destroyed.
1082 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1083 {
1084 args->m_error.SetErrorToErrno();
1085 goto FINISH;
1086 }
1087#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001088 // Release the master terminal descriptor and pass it off to the
1089 // ProcessMonitor instance. Similarly stash the inferior pid.
1090 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001091 monitor->m_pid = pid;
1092
1093 // Set the terminal fd to be in non blocking mode (it simplifies the
1094 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1095 // descriptor to read from).
1096 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1097 goto FINISH;
1098
Ed Mastee5441432013-09-03 23:55:30 +00001099 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001100
1101FINISH:
1102 return args->m_error.Success();
1103}
1104
Johnny Chen9ed5b492012-01-05 21:48:15 +00001105void
1106ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1107{
1108 static const char *g_thread_name = "lldb.process.freebsd.operation";
1109
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001110 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001111 return;
1112
Zachary Turner39de3112014-09-09 20:54:56 +00001113 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001114}
1115
Johnny Chen9ed5b492012-01-05 21:48:15 +00001116void *
1117ProcessMonitor::AttachOpThread(void *arg)
1118{
1119 AttachArgs *args = static_cast<AttachArgs*>(arg);
1120
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001121 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001122
1123 ServeOperation(args);
1124 return NULL;
1125}
1126
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001127void
Johnny Chen9ed5b492012-01-05 21:48:15 +00001128ProcessMonitor::Attach(AttachArgs *args)
1129{
1130 lldb::pid_t pid = args->m_pid;
1131
1132 ProcessMonitor *monitor = args->m_monitor;
1133 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001134
1135 if (pid <= 1)
1136 {
1137 args->m_error.SetErrorToGenericError();
1138 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001139 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001140 }
1141
1142 // Attach to the requested process.
1143 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1144 {
1145 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001146 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001147 }
1148
1149 int status;
1150 if ((status = waitpid(pid, NULL, 0)) < 0)
1151 {
1152 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001153 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001154 }
1155
Ed Mastee5441432013-09-03 23:55:30 +00001156 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001157}
1158
Ed Maste7fd845c2013-12-09 15:51:17 +00001159size_t
1160ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1161{
1162 lwpid_t *tids;
1163 int tdcnt;
1164
1165 thread_ids.clear();
1166
1167 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1168 if (tdcnt <= 0)
1169 return 0;
1170 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1171 if (tids == NULL)
1172 return 0;
1173 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1174 free(tids);
1175 return 0;
1176 }
1177 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1178 free(tids);
1179 return thread_ids.size();
1180}
1181
Johnny Chen9ed5b492012-01-05 21:48:15 +00001182bool
1183ProcessMonitor::MonitorCallback(void *callback_baton,
1184 lldb::pid_t pid,
1185 bool exited,
1186 int signal,
1187 int status)
1188{
1189 ProcessMessage message;
1190 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001191 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001192 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001193 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001194 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001195 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001196
Ed Mastea02f5532013-07-02 16:45:16 +00001197 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1198
1199 if (exited)
1200 {
1201 if (log)
1202 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1203 message = ProcessMessage::Exit(pid, status);
1204 process->SendMessage(message);
1205 return pid == process->GetID();
1206 }
1207
Ed Maste819e3992013-07-17 14:02:20 +00001208 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001209 stop_monitoring = true; // pid is gone. Bail.
1210 else {
Ed Maste819e3992013-07-17 14:02:20 +00001211 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001212 {
1213 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001214 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001215 break;
1216
1217 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001218 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001219 break;
1220 }
1221
1222 process->SendMessage(message);
1223 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1224 }
1225
1226 return stop_monitoring;
1227}
1228
1229ProcessMessage
1230ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001231 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001232{
1233 ProcessMessage message;
1234
Ed Mastea02f5532013-07-02 16:45:16 +00001235 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1236
Matt Kopec7de48462013-03-06 17:20:48 +00001237 assert(monitor);
1238 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001239
1240 switch (info->si_code)
1241 {
1242 default:
1243 assert(false && "Unexpected SIGTRAP code!");
1244 break;
1245
1246 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1247 {
1248 // The inferior process is about to exit. Maintain the process in a
1249 // state of "limbo" until we are explicitly commanded to detach,
1250 // destroy, resume, etc.
1251 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001252 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001253 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001254 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001255 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1256 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001257 break;
1258 }
1259
1260 case 0:
1261 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001262 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001263 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001264 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001265 break;
1266
1267 case SI_KERNEL:
1268 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001269 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001270 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1271 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001272 break;
1273 }
1274
1275 return message;
1276}
1277
1278ProcessMessage
1279ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001280 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001281{
1282 ProcessMessage message;
1283 int signo = info->si_signo;
1284
Ed Mastea02f5532013-07-02 16:45:16 +00001285 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1286
Johnny Chen9ed5b492012-01-05 21:48:15 +00001287 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1288 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1289 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1290 //
1291 // IOW, user generated signals never generate what we consider to be a
1292 // "crash".
1293 //
1294 // Similarly, ACK signals generated by this monitor.
1295 if (info->si_code == SI_USER)
1296 {
Ed Mastea02f5532013-07-02 16:45:16 +00001297 if (log)
1298 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1299 __FUNCTION__,
Chaoren Line515a5a2015-07-14 03:18:23 +00001300 monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo),
Ed Mastea02f5532013-07-02 16:45:16 +00001301 "SI_USER",
1302 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001303 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001304 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001305 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001306 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001307 }
1308
Ed Mastea02f5532013-07-02 16:45:16 +00001309 if (log)
Chaoren Line515a5a2015-07-14 03:18:23 +00001310 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals()->GetSignalAsCString (signo));
Ed Mastea02f5532013-07-02 16:45:16 +00001311
Chaoren Lin28e57422015-02-03 01:51:25 +00001312 switch (signo)
1313 {
1314 case SIGSEGV:
1315 case SIGILL:
1316 case SIGFPE:
1317 case SIGBUS:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001318 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001319 const auto reason = GetCrashReason(*info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001320 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001321 }
1322
1323 // Everything else is "normal" and does not require any special action on
1324 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001325 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001326}
1327
Johnny Chen9ed5b492012-01-05 21:48:15 +00001328void
1329ProcessMonitor::ServeOperation(OperationArgs *args)
1330{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001331 ProcessMonitor *monitor = args->m_monitor;
1332
Johnny Chen9ed5b492012-01-05 21:48:15 +00001333 // We are finised with the arguments and are ready to go. Sync with the
1334 // parent thread and start serving operations on the inferior.
1335 sem_post(&args->m_semaphore);
1336
1337 for (;;)
1338 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001339 // wait for next pending operation
1340 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001341
Ed Maste756e1ff2013-09-18 19:34:08 +00001342 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001343
Ed Maste756e1ff2013-09-18 19:34:08 +00001344 // notify calling thread that operation is complete
1345 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001346 }
1347}
1348
1349void
1350ProcessMonitor::DoOperation(Operation *op)
1351{
Ed Maste756e1ff2013-09-18 19:34:08 +00001352 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001353
Ed Maste756e1ff2013-09-18 19:34:08 +00001354 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001355
Ed Maste756e1ff2013-09-18 19:34:08 +00001356 // notify operation thread that an operation is ready to be processed
1357 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001358
Ed Maste756e1ff2013-09-18 19:34:08 +00001359 // wait for operation to complete
1360 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001361}
1362
1363size_t
1364ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1365 Error &error)
1366{
1367 size_t result;
1368 ReadOperation op(vm_addr, buf, size, error, result);
1369 DoOperation(&op);
1370 return result;
1371}
1372
1373size_t
1374ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1375 lldb_private::Error &error)
1376{
1377 size_t result;
1378 WriteOperation op(vm_addr, buf, size, error, result);
1379 DoOperation(&op);
1380 return result;
1381}
1382
1383bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001384ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001385 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001386{
1387 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001388 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001389 DoOperation(&op);
1390 return result;
1391}
1392
1393bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001394ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001395 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001396{
1397 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001398 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001399 DoOperation(&op);
1400 return result;
1401}
1402
1403bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001404ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1405 const char *reg_name, unsigned size,
1406 lldb_private::RegisterValue &value)
1407{
1408 bool result;
1409 ReadDebugRegOperation op(tid, offset, size, value, result);
1410 DoOperation(&op);
1411 return result;
1412}
1413
1414bool
1415ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1416 const char *reg_name,
1417 const lldb_private::RegisterValue &value)
1418{
1419 bool result;
1420 WriteDebugRegOperation op(tid, offset, value, result);
1421 DoOperation(&op);
1422 return result;
1423}
1424
1425bool
Matt Kopec7de48462013-03-06 17:20:48 +00001426ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001427{
1428 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001429 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001430 DoOperation(&op);
1431 return result;
1432}
1433
1434bool
Matt Kopec7de48462013-03-06 17:20:48 +00001435ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001436{
1437 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001438 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001439 DoOperation(&op);
1440 return result;
1441}
1442
1443bool
Ed Maste5d34af32013-06-24 15:09:18 +00001444ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1445{
1446 return false;
1447}
1448
1449bool
Matt Kopec7de48462013-03-06 17:20:48 +00001450ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001451{
1452 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001453 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001454 DoOperation(&op);
1455 return result;
1456}
1457
1458bool
Matt Kopec7de48462013-03-06 17:20:48 +00001459ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001460{
1461 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001462 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001463 DoOperation(&op);
1464 return result;
1465}
1466
1467bool
Ed Maste5d34af32013-06-24 15:09:18 +00001468ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1469{
1470 return false;
1471}
1472
1473bool
Ed Maste68f51792013-10-18 19:16:44 +00001474ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1475{
1476 return false;
1477}
1478
1479bool
Ed Maste502f9022013-11-25 16:31:23 +00001480ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001481{
1482 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001483 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1484
Ed Maste4aeb3c02014-05-28 14:11:20 +00001485 if (log) {
Chaoren Line515a5a2015-07-14 03:18:23 +00001486 const char *signame = m_process->GetUnixSignals()->GetSignalAsCString (signo);
Ed Maste4aeb3c02014-05-28 14:11:20 +00001487 if (signame == nullptr)
1488 signame = "<none>";
1489 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1490 __FUNCTION__, GetPID(), signame);
1491 }
Ed Maste502f9022013-11-25 16:31:23 +00001492 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001493 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001494 if (log)
1495 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001496 return result;
1497}
1498
1499bool
Ed Maste502f9022013-11-25 16:31:23 +00001500ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001501{
1502 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001503 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001504 DoOperation(&op);
1505 return result;
1506}
1507
1508bool
Ed Maste70882932014-04-01 14:30:56 +00001509ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001510{
1511 bool result;
1512 KillOperation op(result);
1513 DoOperation(&op);
1514 return result;
1515}
1516
1517bool
Ed Maste819e3992013-07-17 14:02:20 +00001518ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001519{
1520 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001521 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001522 DoOperation(&op);
1523 return result;
1524}
1525
1526bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001527ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1528{
1529 bool result;
1530 ThreadSuspendOperation op(tid, suspend, result);
1531 DoOperation(&op);
1532 return result;
1533}
1534
1535bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001536ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1537{
1538 bool result;
1539 EventMessageOperation op(tid, message, result);
1540 DoOperation(&op);
1541 return result;
1542}
1543
Ed Mastea02f5532013-07-02 16:45:16 +00001544lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001545ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001546{
Ed Mastea02f5532013-07-02 16:45:16 +00001547 lldb_private::Error error;
1548 if (tid != LLDB_INVALID_THREAD_ID)
1549 {
1550 DetachOperation op(error);
1551 DoOperation(&op);
1552 }
1553 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001554}
1555
1556bool
Ed Maste41fba2b2015-06-01 15:24:37 +00001557ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd, int flags)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001558{
Ed Maste41fba2b2015-06-01 15:24:37 +00001559 int target_fd = open(file_spec.GetCString(), flags, 0666);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001560
1561 if (target_fd == -1)
1562 return false;
1563
Ed Maste7f0230f2015-02-05 16:09:03 +00001564 if (dup2(target_fd, fd) == -1)
1565 return false;
1566
1567 return (close(target_fd) == -1) ? false : true;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001568}
1569
1570void
1571ProcessMonitor::StopMonitoringChildProcess()
1572{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001573 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001574 {
Zachary Turner39de3112014-09-09 20:54:56 +00001575 m_monitor_thread.Cancel();
1576 m_monitor_thread.Join(nullptr);
1577 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001578 }
1579}
1580
1581void
1582ProcessMonitor::StopMonitor()
1583{
1584 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001585 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001586 sem_destroy(&m_operation_pending);
1587 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00001588 if (m_terminal_fd >= 0) {
1589 close(m_terminal_fd);
1590 m_terminal_fd = -1;
1591 }
Johnny Chen9ed5b492012-01-05 21:48:15 +00001592}
1593
Andrew Kaylord4d54992013-09-17 00:30:24 +00001594// FIXME: On Linux, when a new thread is created, we receive to notifications,
1595// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1596// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1597// the new child thread indicating that it has is stopped because we attached.
1598// We have no guarantee of the order in which these arrive, but we need both
1599// before we are ready to proceed. We currently keep a list of threads which
1600// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1601// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1602// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1603//
1604// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1605// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1606// logically needed.
1607//
1608// We really should figure out what actually happens on FreeBSD and move the
1609// Linux-specific logic out of ProcessPOSIX as needed.
1610
1611bool
1612ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1613{
1614 return true;
1615}
1616
Johnny Chen9ed5b492012-01-05 21:48:15 +00001617void
Ed Mastea02f5532013-07-02 16:45:16 +00001618ProcessMonitor::StopOpThread()
1619{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001620 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001621 return;
1622
Zachary Turner39de3112014-09-09 20:54:56 +00001623 m_operation_thread.Cancel();
1624 m_operation_thread.Join(nullptr);
1625 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001626}