blob: cb30a159fa12716834bc415cbd0565766c24f61a [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <errno.h>
12#include <poll.h>
13#include <string.h>
Ed Mastea02f5532013-07-02 16:45:16 +000014#include <stdint.h>
Johnny Chen9ed5b492012-01-05 21:48:15 +000015#include <unistd.h>
16#include <signal.h>
17#include <sys/ptrace.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21
22// C++ Includes
23// Other libraries and framework includes
24#include "lldb/Core/Error.h"
25#include "lldb/Core/RegisterValue.h"
26#include "lldb/Core/Scalar.h"
27#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/ThreadLauncher.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/RegisterContext.h"
31#include "lldb/Utility/PseudoTerminal.h"
32
33
34#include "POSIXThread.h"
35#include "ProcessFreeBSD.h"
36#include "ProcessPOSIXLog.h"
37#include "ProcessMonitor.h"
38
39extern "C" {
40 extern char ** environ;
41 }
42
43using namespace lldb;
44using namespace lldb_private;
45
46// We disable the tracing of ptrace calls for integration builds to
47// avoid the additional indirection and checks.
48#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
49// Wrapper for ptrace to catch errors and log calls.
50
51const char *
52Get_PT_IO_OP(int op)
53{
54 switch (op) {
55 case PIOD_READ_D: return "READ_D";
56 case PIOD_WRITE_D: return "WRITE_D";
57 case PIOD_READ_I: return "READ_I";
58 case PIOD_WRITE_I: return "WRITE_I";
59 default: return "Unknown op";
60 }
61}
62
Matt Kopec7de48462013-03-06 17:20:48 +000063// Wrapper for ptrace to catch errors and log calls.
64// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000065extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000066PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000067 const char* reqName, const char* file, int line)
68{
69 long int result;
70
Ashok Thirumurthi01186352013-03-28 16:02:31 +000071 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000072
73 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000074 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000075 reqName, pid, addr, data, file, line);
76 if (req == PT_IO) {
77 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
78
Sylvestre Ledru779f9212013-10-31 23:55:19 +000079 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000080 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000081 }
82 }
83
84 //PtraceDisplayBytes(req, data);
85
86 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000087 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000088
89 //PtraceDisplayBytes(req, data);
90
Matt Kopec7de48462013-03-06 17:20:48 +000091 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000092 {
93 const char* str;
94 switch (errno)
95 {
96 case ESRCH: str = "ESRCH"; break;
97 case EINVAL: str = "EINVAL"; break;
98 case EBUSY: str = "EBUSY"; break;
99 case EPERM: str = "EPERM"; break;
100 default: str = "<unknown>";
101 }
102 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
103 }
104
105 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000106#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000107 if (req == PT_GETREGS) {
108 struct reg *r = (struct reg *) addr;
109
110 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
111 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
112 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
113 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
114 }
Ed Maste7d4c0d5b2013-07-22 20:51:08 +0000115#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000116 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
117 struct dbreg *r = (struct dbreg *) addr;
118 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
119
120 for (int i = 0; i <= 7; i++)
121 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
122 }
123 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000124
125 return result;
126}
127
Matt Kopec7de48462013-03-06 17:20:48 +0000128// Wrapper for ptrace when logging is not required.
129// Sets errno to 0 prior to calling ptrace.
130extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000131PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000132{
133 long result = 0;
134 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000135 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000136 return result;
137}
138
Johnny Chen9ed5b492012-01-05 21:48:15 +0000139#define PTRACE(req, pid, addr, data) \
140 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
141#else
Matt Kopec7de48462013-03-06 17:20:48 +0000142 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000143#endif
144
145//------------------------------------------------------------------------------
146// Static implementations of ProcessMonitor::ReadMemory and
147// ProcessMonitor::WriteMemory. This enables mutual recursion between these
148// functions without needed to go thru the thread funnel.
149
150static size_t
151DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
152 Error &error)
153{
154 struct ptrace_io_desc pi_desc;
155
156 pi_desc.piod_op = PIOD_READ_D;
157 pi_desc.piod_offs = (void *)vm_addr;
158 pi_desc.piod_addr = buf;
159 pi_desc.piod_len = size;
160
161 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
162 error.SetErrorToErrno();
163 return pi_desc.piod_len;
164}
165
166static size_t
167DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
168 size_t size, Error &error)
169{
170 struct ptrace_io_desc pi_desc;
171
172 pi_desc.piod_op = PIOD_WRITE_D;
173 pi_desc.piod_offs = (void *)vm_addr;
174 pi_desc.piod_addr = (void *)buf;
175 pi_desc.piod_len = size;
176
177 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
178 error.SetErrorToErrno();
179 return pi_desc.piod_len;
180}
181
182// Simple helper function to ensure flags are enabled on the given file
183// descriptor.
184static bool
185EnsureFDFlags(int fd, int flags, Error &error)
186{
187 int status;
188
189 if ((status = fcntl(fd, F_GETFL)) == -1)
190 {
191 error.SetErrorToErrno();
192 return false;
193 }
194
195 if (fcntl(fd, F_SETFL, status | flags) == -1)
196 {
197 error.SetErrorToErrno();
198 return false;
199 }
200
201 return true;
202}
203
204//------------------------------------------------------------------------------
205/// @class Operation
206/// @brief Represents a ProcessMonitor operation.
207///
208/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
209/// one that spawned or attached to the process from the start. Therefore, when
210/// a ProcessMonitor is asked to deliver or change the state of an inferior
211/// process the operation must be "funneled" to a specific thread to perform the
212/// task. The Operation class provides an abstract base for all services the
213/// ProcessMonitor must perform via the single virtual function Execute, thus
214/// encapsulating the code that needs to run in the privileged context.
215class Operation
216{
217public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000218 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000219 virtual void Execute(ProcessMonitor *monitor) = 0;
220};
221
222//------------------------------------------------------------------------------
223/// @class ReadOperation
224/// @brief Implements ProcessMonitor::ReadMemory.
225class ReadOperation : public Operation
226{
227public:
228 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
229 Error &error, size_t &result)
230 : m_addr(addr), m_buff(buff), m_size(size),
231 m_error(error), m_result(result)
232 { }
233
234 void Execute(ProcessMonitor *monitor);
235
236private:
237 lldb::addr_t m_addr;
238 void *m_buff;
239 size_t m_size;
240 Error &m_error;
241 size_t &m_result;
242};
243
244void
245ReadOperation::Execute(ProcessMonitor *monitor)
246{
247 lldb::pid_t pid = monitor->GetPID();
248
249 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
250}
251
252//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000253/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000254/// @brief Implements ProcessMonitor::WriteMemory.
255class WriteOperation : public Operation
256{
257public:
258 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
259 Error &error, size_t &result)
260 : m_addr(addr), m_buff(buff), m_size(size),
261 m_error(error), m_result(result)
262 { }
263
264 void Execute(ProcessMonitor *monitor);
265
266private:
267 lldb::addr_t m_addr;
268 const void *m_buff;
269 size_t m_size;
270 Error &m_error;
271 size_t &m_result;
272};
273
274void
275WriteOperation::Execute(ProcessMonitor *monitor)
276{
277 lldb::pid_t pid = monitor->GetPID();
278
279 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
280}
281
282//------------------------------------------------------------------------------
283/// @class ReadRegOperation
284/// @brief Implements ProcessMonitor::ReadRegisterValue.
285class ReadRegOperation : public Operation
286{
287public:
Ed Maste6f066412013-07-04 21:47:32 +0000288 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
289 RegisterValue &value, bool &result)
290 : m_tid(tid), m_offset(offset), m_size(size),
291 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000292 { }
293
294 void Execute(ProcessMonitor *monitor);
295
296private:
Ed Maste6f066412013-07-04 21:47:32 +0000297 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000298 unsigned m_offset;
299 unsigned m_size;
300 RegisterValue &m_value;
301 bool &m_result;
302};
303
304void
305ReadRegOperation::Execute(ProcessMonitor *monitor)
306{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000307 struct reg regs;
308 int rc;
309
Ed Maste6f066412013-07-04 21:47:32 +0000310 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000311 m_result = false;
312 } else {
313 if (m_size == sizeof(uintptr_t))
314 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
315 else
316 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
317 m_result = true;
318 }
319}
320
321//------------------------------------------------------------------------------
322/// @class WriteRegOperation
323/// @brief Implements ProcessMonitor::WriteRegisterValue.
324class WriteRegOperation : public Operation
325{
326public:
Ed Maste6f066412013-07-04 21:47:32 +0000327 WriteRegOperation(lldb::tid_t tid, unsigned offset,
328 const RegisterValue &value, bool &result)
329 : m_tid(tid), m_offset(offset),
330 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000331 { }
332
333 void Execute(ProcessMonitor *monitor);
334
335private:
Ed Maste6f066412013-07-04 21:47:32 +0000336 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000337 unsigned m_offset;
338 const RegisterValue &m_value;
339 bool &m_result;
340};
341
342void
343WriteRegOperation::Execute(ProcessMonitor *monitor)
344{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000345 struct reg regs;
346
Ed Maste6f066412013-07-04 21:47:32 +0000347 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000348 m_result = false;
349 return;
350 }
351 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000352 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000353 m_result = false;
354 else
355 m_result = true;
356}
357
358//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000359/// @class ReadDebugRegOperation
360/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
361class ReadDebugRegOperation : public Operation
362{
363public:
364 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
365 RegisterValue &value, bool &result)
366 : m_tid(tid), m_offset(offset), m_size(size),
367 m_value(value), m_result(result)
368 { }
369
370 void Execute(ProcessMonitor *monitor);
371
372private:
373 lldb::tid_t m_tid;
374 unsigned m_offset;
375 unsigned m_size;
376 RegisterValue &m_value;
377 bool &m_result;
378};
379
380void
381ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
382{
383 struct dbreg regs;
384 int rc;
385
386 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
387 m_result = false;
388 } else {
389 if (m_size == sizeof(uintptr_t))
390 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
391 else
392 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
393 m_result = true;
394 }
395}
396
397//------------------------------------------------------------------------------
398/// @class WriteDebugRegOperation
399/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
400class WriteDebugRegOperation : public Operation
401{
402public:
403 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
404 const RegisterValue &value, bool &result)
405 : m_tid(tid), m_offset(offset),
406 m_value(value), m_result(result)
407 { }
408
409 void Execute(ProcessMonitor *monitor);
410
411private:
412 lldb::tid_t m_tid;
413 unsigned m_offset;
414 const RegisterValue &m_value;
415 bool &m_result;
416};
417
418void
419WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
420{
421 struct dbreg regs;
422
423 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
424 m_result = false;
425 return;
426 }
427 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
428 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
429 m_result = false;
430 else
431 m_result = true;
432}
433
434//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000435/// @class ReadGPROperation
436/// @brief Implements ProcessMonitor::ReadGPR.
437class ReadGPROperation : public Operation
438{
439public:
Ed Maste6f066412013-07-04 21:47:32 +0000440 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
441 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000442 { }
443
444 void Execute(ProcessMonitor *monitor);
445
446private:
Ed Maste6f066412013-07-04 21:47:32 +0000447 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000448 void *m_buf;
449 bool &m_result;
450};
451
452void
453ReadGPROperation::Execute(ProcessMonitor *monitor)
454{
455 int rc;
456
457 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000458 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000459 if (errno != 0)
460 m_result = false;
461 else
462 m_result = true;
463}
464
465//------------------------------------------------------------------------------
466/// @class ReadFPROperation
467/// @brief Implements ProcessMonitor::ReadFPR.
468class ReadFPROperation : public Operation
469{
470public:
Ed Maste6f066412013-07-04 21:47:32 +0000471 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
472 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000473 { }
474
475 void Execute(ProcessMonitor *monitor);
476
477private:
Ed Maste6f066412013-07-04 21:47:32 +0000478 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000479 void *m_buf;
480 bool &m_result;
481};
482
483void
484ReadFPROperation::Execute(ProcessMonitor *monitor)
485{
Ed Maste6f066412013-07-04 21:47:32 +0000486 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000487 m_result = false;
488 else
489 m_result = true;
490}
491
492//------------------------------------------------------------------------------
493/// @class WriteGPROperation
494/// @brief Implements ProcessMonitor::WriteGPR.
495class WriteGPROperation : public Operation
496{
497public:
Ed Maste6f066412013-07-04 21:47:32 +0000498 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
499 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000500 { }
501
502 void Execute(ProcessMonitor *monitor);
503
504private:
Ed Maste6f066412013-07-04 21:47:32 +0000505 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000506 void *m_buf;
507 bool &m_result;
508};
509
510void
511WriteGPROperation::Execute(ProcessMonitor *monitor)
512{
Ed Maste6f066412013-07-04 21:47:32 +0000513 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000514 m_result = false;
515 else
516 m_result = true;
517}
518
519//------------------------------------------------------------------------------
520/// @class WriteFPROperation
521/// @brief Implements ProcessMonitor::WriteFPR.
522class WriteFPROperation : public Operation
523{
524public:
Ed Maste6f066412013-07-04 21:47:32 +0000525 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
526 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000527 { }
528
529 void Execute(ProcessMonitor *monitor);
530
531private:
Ed Maste6f066412013-07-04 21:47:32 +0000532 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000533 void *m_buf;
534 bool &m_result;
535};
536
537void
538WriteFPROperation::Execute(ProcessMonitor *monitor)
539{
Ed Maste6f066412013-07-04 21:47:32 +0000540 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000541 m_result = false;
542 else
543 m_result = true;
544}
545
546//------------------------------------------------------------------------------
547/// @class ResumeOperation
548/// @brief Implements ProcessMonitor::Resume.
549class ResumeOperation : public Operation
550{
551public:
Ed Maste502f9022013-11-25 16:31:23 +0000552 ResumeOperation(uint32_t signo, bool &result) :
553 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000554
555 void Execute(ProcessMonitor *monitor);
556
557private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000558 uint32_t m_signo;
559 bool &m_result;
560};
561
562void
563ResumeOperation::Execute(ProcessMonitor *monitor)
564{
Ed Maste502f9022013-11-25 16:31:23 +0000565 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000566 int data = 0;
567
568 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
569 data = m_signo;
570
Ed Maste502f9022013-11-25 16:31:23 +0000571 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000572 {
573 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
574
575 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000576 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000577 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000578 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000579 else
580 m_result = true;
581}
582
583//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000584/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000585/// @brief Implements ProcessMonitor::SingleStep.
586class SingleStepOperation : public Operation
587{
588public:
Ed Maste502f9022013-11-25 16:31:23 +0000589 SingleStepOperation(uint32_t signo, bool &result)
590 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000591
592 void Execute(ProcessMonitor *monitor);
593
594private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000595 uint32_t m_signo;
596 bool &m_result;
597};
598
599void
600SingleStepOperation::Execute(ProcessMonitor *monitor)
601{
Ed Maste502f9022013-11-25 16:31:23 +0000602 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000603 int data = 0;
604
605 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
606 data = m_signo;
607
Ed Maste502f9022013-11-25 16:31:23 +0000608 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000609 m_result = false;
610 else
611 m_result = true;
612}
613
614//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000615/// @class LwpInfoOperation
616/// @brief Implements ProcessMonitor::GetLwpInfo.
617class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000618{
619public:
Ed Maste819e3992013-07-17 14:02:20 +0000620 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000621 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000622
623 void Execute(ProcessMonitor *monitor);
624
625private:
626 lldb::tid_t m_tid;
627 void *m_info;
628 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000629 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000630};
631
632void
Ed Maste819e3992013-07-17 14:02:20 +0000633LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000634{
635 struct ptrace_lwpinfo plwp;
636
Daniel Maleaa35970a2012-11-23 18:09:58 +0000637 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000638 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000639 m_err = errno;
640 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000641 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000642 m_result = true;
643 }
644}
645
646//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000647/// @class ThreadSuspendOperation
648/// @brief Implements ProcessMonitor::ThreadSuspend.
649class ThreadSuspendOperation : public Operation
650{
651public:
652 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
653 : m_tid(tid), m_suspend(suspend), m_result(result) { }
654
655 void Execute(ProcessMonitor *monitor);
656
657private:
658 lldb::tid_t m_tid;
659 bool m_suspend;
660 bool &m_result;
661} ;
662
663void
664ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
665{
666 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
667}
668
669
670
671//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000672/// @class EventMessageOperation
673/// @brief Implements ProcessMonitor::GetEventMessage.
674class EventMessageOperation : public Operation
675{
676public:
677 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
678 : m_tid(tid), m_message(message), m_result(result) { }
679
680 void Execute(ProcessMonitor *monitor);
681
682private:
683 lldb::tid_t m_tid;
684 unsigned long *m_message;
685 bool &m_result;
686};
687
688void
689EventMessageOperation::Execute(ProcessMonitor *monitor)
690{
691 struct ptrace_lwpinfo plwp;
692
693 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
694 m_result = false;
695 else {
696 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000697 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000698 m_result = true;
699 } else
700 m_result = false;
701 }
702}
703
704//------------------------------------------------------------------------------
705/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000706/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000707class KillOperation : public Operation
708{
709public:
710 KillOperation(bool &result) : m_result(result) { }
711
712 void Execute(ProcessMonitor *monitor);
713
714private:
715 bool &m_result;
716};
717
718void
719KillOperation::Execute(ProcessMonitor *monitor)
720{
721 lldb::pid_t pid = monitor->GetPID();
722
723 if (PTRACE(PT_KILL, pid, NULL, 0))
724 m_result = false;
725 else
726 m_result = true;
727}
728
729//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000730/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000731/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000732class DetachOperation : public Operation
733{
734public:
735 DetachOperation(Error &result) : m_error(result) { }
736
737 void Execute(ProcessMonitor *monitor);
738
739private:
740 Error &m_error;
741};
742
743void
744DetachOperation::Execute(ProcessMonitor *monitor)
745{
746 lldb::pid_t pid = monitor->GetPID();
747
748 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
749 m_error.SetErrorToErrno();
750
751}
752
753ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
754 : m_monitor(monitor)
755{
756 sem_init(&m_semaphore, 0, 0);
757}
758
759ProcessMonitor::OperationArgs::~OperationArgs()
760{
761 sem_destroy(&m_semaphore);
762}
763
764ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
765 lldb_private::Module *module,
766 char const **argv,
767 char const **envp,
768 const char *stdin_path,
769 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000770 const char *stderr_path,
771 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000772 : OperationArgs(monitor),
773 m_module(module),
774 m_argv(argv),
775 m_envp(envp),
776 m_stdin_path(stdin_path),
777 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000778 m_stderr_path(stderr_path),
779 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000780
781ProcessMonitor::LaunchArgs::~LaunchArgs()
782{ }
783
784ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
785 lldb::pid_t pid)
786 : OperationArgs(monitor), m_pid(pid) { }
787
788ProcessMonitor::AttachArgs::~AttachArgs()
789{ }
790
791//------------------------------------------------------------------------------
792/// The basic design of the ProcessMonitor is built around two threads.
793///
794/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
795/// for changes in the debugee state. When a change is detected a
796/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
797/// "drives" state changes in the debugger.
798///
799/// The second thread (@see OperationThread) is responsible for two things 1)
800/// launching or attaching to the inferior process, and then 2) servicing
801/// operations such as register reads/writes, stepping, etc. See the comments
802/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000803ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000804 Module *module,
805 const char *argv[],
806 const char *envp[],
807 const char *stdin_path,
808 const char *stdout_path,
809 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000810 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000811 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000812 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000813 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000814 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000815 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000816 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000817{
Ed Maste756e1ff2013-09-18 19:34:08 +0000818 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
819 stdin_path, stdout_path, stderr_path,
820 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821
822
Ed Maste756e1ff2013-09-18 19:34:08 +0000823 sem_init(&m_operation_pending, 0, 0);
824 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000825
826 StartLaunchOpThread(args.get(), error);
827 if (!error.Success())
828 return;
829
830WAIT_AGAIN:
831 // Wait for the operation thread to initialize.
832 if (sem_wait(&args->m_semaphore))
833 {
834 if (errno == EINTR)
835 goto WAIT_AGAIN;
836 else
837 {
838 error.SetErrorToErrno();
839 return;
840 }
841 }
842
843 // Check that the launch was a success.
844 if (!args->m_error.Success())
845 {
Ed Mastea02f5532013-07-02 16:45:16 +0000846 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000847 error = args->m_error;
848 return;
849 }
850
851 // Finally, start monitoring the child process for change in state.
852 m_monitor_thread = Host::StartMonitoringChildProcess(
853 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000854 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000855 {
856 error.SetErrorToGenericError();
857 error.SetErrorString("Process launch failed.");
858 return;
859 }
860}
861
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000862ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000863 lldb::pid_t pid,
864 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000865 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000866 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000867 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000868 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000869{
Ed Maste756e1ff2013-09-18 19:34:08 +0000870 sem_init(&m_operation_pending, 0, 0);
871 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000872
Johnny Chen9ed5b492012-01-05 21:48:15 +0000873
Ed Maste756e1ff2013-09-18 19:34:08 +0000874 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000875
876 StartAttachOpThread(args.get(), error);
877 if (!error.Success())
878 return;
879
880WAIT_AGAIN:
881 // Wait for the operation thread to initialize.
882 if (sem_wait(&args->m_semaphore))
883 {
884 if (errno == EINTR)
885 goto WAIT_AGAIN;
886 else
887 {
888 error.SetErrorToErrno();
889 return;
890 }
891 }
892
Ed Mastea02f5532013-07-02 16:45:16 +0000893 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000894 if (!args->m_error.Success())
895 {
Ed Mastea02f5532013-07-02 16:45:16 +0000896 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000897 error = args->m_error;
898 return;
899 }
900
901 // Finally, start monitoring the child process for change in state.
902 m_monitor_thread = Host::StartMonitoringChildProcess(
903 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000904 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000905 {
906 error.SetErrorToGenericError();
907 error.SetErrorString("Process attach failed.");
908 return;
909 }
910}
911
912ProcessMonitor::~ProcessMonitor()
913{
914 StopMonitor();
915}
916
917//------------------------------------------------------------------------------
918// Thread setup and tear down.
919void
920ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
921{
922 static const char *g_thread_name = "lldb.process.freebsd.operation";
923
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000924 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000925 return;
926
Zachary Turner39de3112014-09-09 20:54:56 +0000927 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000928}
929
Johnny Chen9ed5b492012-01-05 21:48:15 +0000930void *
931ProcessMonitor::LaunchOpThread(void *arg)
932{
933 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
934
935 if (!Launch(args)) {
936 sem_post(&args->m_semaphore);
937 return NULL;
938 }
939
940 ServeOperation(args);
941 return NULL;
942}
943
944bool
945ProcessMonitor::Launch(LaunchArgs *args)
946{
947 ProcessMonitor *monitor = args->m_monitor;
948 ProcessFreeBSD &process = monitor->GetProcess();
949 const char **argv = args->m_argv;
950 const char **envp = args->m_envp;
951 const char *stdin_path = args->m_stdin_path;
952 const char *stdout_path = args->m_stdout_path;
953 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000954 const char *working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000955
956 lldb_utility::PseudoTerminal terminal;
957 const size_t err_len = 1024;
958 char err_str[err_len];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000959 lldb::pid_t pid;
960
Johnny Chen9ed5b492012-01-05 21:48:15 +0000961 // Propagate the environment if one is not supplied.
962 if (envp == NULL || envp[0] == NULL)
963 envp = const_cast<const char **>(environ);
964
Ed Mastea02f5532013-07-02 16:45:16 +0000965 if ((pid = terminal.Fork(err_str, err_len)) == -1)
966 {
967 args->m_error.SetErrorToGenericError();
968 args->m_error.SetErrorString("Process fork failed.");
969 goto FINISH;
970 }
971
Johnny Chen9ed5b492012-01-05 21:48:15 +0000972 // Recognized child exit status codes.
973 enum {
974 ePtraceFailed = 1,
975 eDupStdinFailed,
976 eDupStdoutFailed,
977 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000978 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000979 eExecFailed,
980 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000981 };
982
Johnny Chen9ed5b492012-01-05 21:48:15 +0000983 // Child process.
984 if (pid == 0)
985 {
986 // Trace this process.
987 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
988 exit(ePtraceFailed);
989
990 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +0000991 if (setgid(getgid()) != 0)
992 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000993
994 // Let us have our own process group.
995 setpgid(0, 0);
996
997 // Dup file descriptors if needed.
998 //
999 // FIXME: If two or more of the paths are the same we needlessly open
1000 // the same file multiple times.
1001 if (stdin_path != NULL && stdin_path[0])
1002 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1003 exit(eDupStdinFailed);
1004
1005 if (stdout_path != NULL && stdout_path[0])
1006 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1007 exit(eDupStdoutFailed);
1008
1009 if (stderr_path != NULL && stderr_path[0])
1010 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1011 exit(eDupStderrFailed);
1012
Daniel Malea6217d2a2013-01-08 14:49:22 +00001013 // Change working directory
1014 if (working_dir != NULL && working_dir[0])
1015 if (0 != ::chdir(working_dir))
1016 exit(eChdirFailed);
1017
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018 // Execute. We should never return.
1019 execve(argv[0],
1020 const_cast<char *const *>(argv),
1021 const_cast<char *const *>(envp));
1022 exit(eExecFailed);
1023 }
1024
1025 // Wait for the child process to to trap on its call to execve.
1026 ::pid_t wpid;
1027 int status;
1028 if ((wpid = waitpid(pid, &status, 0)) < 0)
1029 {
1030 args->m_error.SetErrorToErrno();
1031 goto FINISH;
1032 }
1033 else if (WIFEXITED(status))
1034 {
1035 // open, dup or execve likely failed for some reason.
1036 args->m_error.SetErrorToGenericError();
1037 switch (WEXITSTATUS(status))
1038 {
Ed Maste5d34af32013-06-24 15:09:18 +00001039 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001040 args->m_error.SetErrorString("Child ptrace failed.");
1041 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001042 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001043 args->m_error.SetErrorString("Child open stdin failed.");
1044 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001045 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001046 args->m_error.SetErrorString("Child open stdout failed.");
1047 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001048 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001049 args->m_error.SetErrorString("Child open stderr failed.");
1050 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001051 case eChdirFailed:
1052 args->m_error.SetErrorString("Child failed to set working directory.");
1053 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001054 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001055 args->m_error.SetErrorString("Child exec failed.");
1056 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001057 case eSetGidFailed:
1058 args->m_error.SetErrorString("Child setgid failed.");
1059 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001060 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001061 args->m_error.SetErrorString("Child returned unknown exit status.");
1062 break;
1063 }
1064 goto FINISH;
1065 }
Ed Maste82a00052013-11-25 21:15:53 +00001066 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001067 "Could not sync with inferior process.");
1068
1069#ifdef notyet
1070 // Have the child raise an event on exit. This is used to keep the child in
1071 // limbo until it is destroyed.
1072 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1073 {
1074 args->m_error.SetErrorToErrno();
1075 goto FINISH;
1076 }
1077#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001078 // Release the master terminal descriptor and pass it off to the
1079 // ProcessMonitor instance. Similarly stash the inferior pid.
1080 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001081 monitor->m_pid = pid;
1082
1083 // Set the terminal fd to be in non blocking mode (it simplifies the
1084 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1085 // descriptor to read from).
1086 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1087 goto FINISH;
1088
Ed Mastee5441432013-09-03 23:55:30 +00001089 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001090
1091FINISH:
1092 return args->m_error.Success();
1093}
1094
Johnny Chen9ed5b492012-01-05 21:48:15 +00001095void
1096ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1097{
1098 static const char *g_thread_name = "lldb.process.freebsd.operation";
1099
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001100 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001101 return;
1102
Zachary Turner39de3112014-09-09 20:54:56 +00001103 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001104}
1105
Johnny Chen9ed5b492012-01-05 21:48:15 +00001106void *
1107ProcessMonitor::AttachOpThread(void *arg)
1108{
1109 AttachArgs *args = static_cast<AttachArgs*>(arg);
1110
1111 if (!Attach(args))
1112 return NULL;
1113
1114 ServeOperation(args);
1115 return NULL;
1116}
1117
1118bool
1119ProcessMonitor::Attach(AttachArgs *args)
1120{
1121 lldb::pid_t pid = args->m_pid;
1122
1123 ProcessMonitor *monitor = args->m_monitor;
1124 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001125
1126 if (pid <= 1)
1127 {
1128 args->m_error.SetErrorToGenericError();
1129 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1130 goto FINISH;
1131 }
1132
1133 // Attach to the requested process.
1134 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1135 {
1136 args->m_error.SetErrorToErrno();
1137 goto FINISH;
1138 }
1139
1140 int status;
1141 if ((status = waitpid(pid, NULL, 0)) < 0)
1142 {
1143 args->m_error.SetErrorToErrno();
1144 goto FINISH;
1145 }
1146
Ed Mastee5441432013-09-03 23:55:30 +00001147 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001148
Ed Mastee5441432013-09-03 23:55:30 +00001149FINISH:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001150 return args->m_error.Success();
1151}
1152
Ed Maste7fd845c2013-12-09 15:51:17 +00001153size_t
1154ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1155{
1156 lwpid_t *tids;
1157 int tdcnt;
1158
1159 thread_ids.clear();
1160
1161 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1162 if (tdcnt <= 0)
1163 return 0;
1164 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1165 if (tids == NULL)
1166 return 0;
1167 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1168 free(tids);
1169 return 0;
1170 }
1171 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1172 free(tids);
1173 return thread_ids.size();
1174}
1175
Johnny Chen9ed5b492012-01-05 21:48:15 +00001176bool
1177ProcessMonitor::MonitorCallback(void *callback_baton,
1178 lldb::pid_t pid,
1179 bool exited,
1180 int signal,
1181 int status)
1182{
1183 ProcessMessage message;
1184 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001185 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001186 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001187 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001188 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001189 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001190
Ed Mastea02f5532013-07-02 16:45:16 +00001191 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1192
1193 if (exited)
1194 {
1195 if (log)
1196 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1197 message = ProcessMessage::Exit(pid, status);
1198 process->SendMessage(message);
1199 return pid == process->GetID();
1200 }
1201
Ed Maste819e3992013-07-17 14:02:20 +00001202 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001203 stop_monitoring = true; // pid is gone. Bail.
1204 else {
Ed Maste819e3992013-07-17 14:02:20 +00001205 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001206 {
1207 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001208 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001209 break;
1210
1211 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001212 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001213 break;
1214 }
1215
1216 process->SendMessage(message);
1217 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1218 }
1219
1220 return stop_monitoring;
1221}
1222
1223ProcessMessage
1224ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001225 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001226{
1227 ProcessMessage message;
1228
Ed Mastea02f5532013-07-02 16:45:16 +00001229 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1230
Matt Kopec7de48462013-03-06 17:20:48 +00001231 assert(monitor);
1232 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001233
1234 switch (info->si_code)
1235 {
1236 default:
1237 assert(false && "Unexpected SIGTRAP code!");
1238 break;
1239
1240 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1241 {
1242 // The inferior process is about to exit. Maintain the process in a
1243 // state of "limbo" until we are explicitly commanded to detach,
1244 // destroy, resume, etc.
1245 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001246 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001247 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001248 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001249 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1250 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001251 break;
1252 }
1253
1254 case 0:
1255 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001256 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001257 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001258 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001259 break;
1260
1261 case SI_KERNEL:
1262 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001263 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001264 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1265 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001266 break;
1267 }
1268
1269 return message;
1270}
1271
1272ProcessMessage
1273ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001274 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001275{
1276 ProcessMessage message;
1277 int signo = info->si_signo;
1278
Ed Mastea02f5532013-07-02 16:45:16 +00001279 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1280
Johnny Chen9ed5b492012-01-05 21:48:15 +00001281 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1282 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1283 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1284 //
1285 // IOW, user generated signals never generate what we consider to be a
1286 // "crash".
1287 //
1288 // Similarly, ACK signals generated by this monitor.
1289 if (info->si_code == SI_USER)
1290 {
Ed Mastea02f5532013-07-02 16:45:16 +00001291 if (log)
1292 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1293 __FUNCTION__,
1294 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1295 "SI_USER",
1296 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001297 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001298 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001299 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001300 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001301 }
1302
Ed Mastea02f5532013-07-02 16:45:16 +00001303 if (log)
1304 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1305
Johnny Chen9ed5b492012-01-05 21:48:15 +00001306 if (signo == SIGSEGV) {
1307 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1308 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001309 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001310 }
1311
1312 if (signo == SIGILL) {
1313 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1314 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001315 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001316 }
1317
1318 if (signo == SIGFPE) {
1319 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1320 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001321 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001322 }
1323
1324 if (signo == SIGBUS) {
1325 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1326 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001327 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001328 }
1329
1330 // Everything else is "normal" and does not require any special action on
1331 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001332 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001333}
1334
1335ProcessMessage::CrashReason
1336ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1337{
1338 ProcessMessage::CrashReason reason;
1339 assert(info->si_signo == SIGSEGV);
1340
1341 reason = ProcessMessage::eInvalidCrashReason;
1342
1343 switch (info->si_code)
1344 {
1345 default:
1346 assert(false && "unexpected si_code for SIGSEGV");
1347 break;
1348 case SEGV_MAPERR:
1349 reason = ProcessMessage::eInvalidAddress;
1350 break;
1351 case SEGV_ACCERR:
1352 reason = ProcessMessage::ePrivilegedAddress;
1353 break;
1354 }
1355
1356 return reason;
1357}
1358
1359ProcessMessage::CrashReason
1360ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1361{
1362 ProcessMessage::CrashReason reason;
1363 assert(info->si_signo == SIGILL);
1364
1365 reason = ProcessMessage::eInvalidCrashReason;
1366
1367 switch (info->si_code)
1368 {
1369 default:
1370 assert(false && "unexpected si_code for SIGILL");
1371 break;
1372 case ILL_ILLOPC:
1373 reason = ProcessMessage::eIllegalOpcode;
1374 break;
1375 case ILL_ILLOPN:
1376 reason = ProcessMessage::eIllegalOperand;
1377 break;
1378 case ILL_ILLADR:
1379 reason = ProcessMessage::eIllegalAddressingMode;
1380 break;
1381 case ILL_ILLTRP:
1382 reason = ProcessMessage::eIllegalTrap;
1383 break;
1384 case ILL_PRVOPC:
1385 reason = ProcessMessage::ePrivilegedOpcode;
1386 break;
1387 case ILL_PRVREG:
1388 reason = ProcessMessage::ePrivilegedRegister;
1389 break;
1390 case ILL_COPROC:
1391 reason = ProcessMessage::eCoprocessorError;
1392 break;
1393 case ILL_BADSTK:
1394 reason = ProcessMessage::eInternalStackError;
1395 break;
1396 }
1397
1398 return reason;
1399}
1400
1401ProcessMessage::CrashReason
1402ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1403{
1404 ProcessMessage::CrashReason reason;
1405 assert(info->si_signo == SIGFPE);
1406
1407 reason = ProcessMessage::eInvalidCrashReason;
1408
1409 switch (info->si_code)
1410 {
1411 default:
1412 assert(false && "unexpected si_code for SIGFPE");
1413 break;
1414 case FPE_INTDIV:
1415 reason = ProcessMessage::eIntegerDivideByZero;
1416 break;
1417 case FPE_INTOVF:
1418 reason = ProcessMessage::eIntegerOverflow;
1419 break;
1420 case FPE_FLTDIV:
1421 reason = ProcessMessage::eFloatDivideByZero;
1422 break;
1423 case FPE_FLTOVF:
1424 reason = ProcessMessage::eFloatOverflow;
1425 break;
1426 case FPE_FLTUND:
1427 reason = ProcessMessage::eFloatUnderflow;
1428 break;
1429 case FPE_FLTRES:
1430 reason = ProcessMessage::eFloatInexactResult;
1431 break;
1432 case FPE_FLTINV:
1433 reason = ProcessMessage::eFloatInvalidOperation;
1434 break;
1435 case FPE_FLTSUB:
1436 reason = ProcessMessage::eFloatSubscriptRange;
1437 break;
1438 }
1439
1440 return reason;
1441}
1442
1443ProcessMessage::CrashReason
1444ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1445{
1446 ProcessMessage::CrashReason reason;
1447 assert(info->si_signo == SIGBUS);
1448
1449 reason = ProcessMessage::eInvalidCrashReason;
1450
1451 switch (info->si_code)
1452 {
1453 default:
1454 assert(false && "unexpected si_code for SIGBUS");
1455 break;
1456 case BUS_ADRALN:
1457 reason = ProcessMessage::eIllegalAlignment;
1458 break;
1459 case BUS_ADRERR:
1460 reason = ProcessMessage::eIllegalAddress;
1461 break;
1462 case BUS_OBJERR:
1463 reason = ProcessMessage::eHardwareError;
1464 break;
1465 }
1466
1467 return reason;
1468}
1469
1470void
1471ProcessMonitor::ServeOperation(OperationArgs *args)
1472{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001473 ProcessMonitor *monitor = args->m_monitor;
1474
Johnny Chen9ed5b492012-01-05 21:48:15 +00001475 // We are finised with the arguments and are ready to go. Sync with the
1476 // parent thread and start serving operations on the inferior.
1477 sem_post(&args->m_semaphore);
1478
1479 for (;;)
1480 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001481 // wait for next pending operation
1482 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001483
Ed Maste756e1ff2013-09-18 19:34:08 +00001484 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001485
Ed Maste756e1ff2013-09-18 19:34:08 +00001486 // notify calling thread that operation is complete
1487 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001488 }
1489}
1490
1491void
1492ProcessMonitor::DoOperation(Operation *op)
1493{
Ed Maste756e1ff2013-09-18 19:34:08 +00001494 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001495
Ed Maste756e1ff2013-09-18 19:34:08 +00001496 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001497
Ed Maste756e1ff2013-09-18 19:34:08 +00001498 // notify operation thread that an operation is ready to be processed
1499 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001500
Ed Maste756e1ff2013-09-18 19:34:08 +00001501 // wait for operation to complete
1502 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001503}
1504
1505size_t
1506ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1507 Error &error)
1508{
1509 size_t result;
1510 ReadOperation op(vm_addr, buf, size, error, result);
1511 DoOperation(&op);
1512 return result;
1513}
1514
1515size_t
1516ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1517 lldb_private::Error &error)
1518{
1519 size_t result;
1520 WriteOperation op(vm_addr, buf, size, error, result);
1521 DoOperation(&op);
1522 return result;
1523}
1524
1525bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001526ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001527 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001528{
1529 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001530 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001531 DoOperation(&op);
1532 return result;
1533}
1534
1535bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001536ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001537 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001538{
1539 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001540 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001541 DoOperation(&op);
1542 return result;
1543}
1544
1545bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001546ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1547 const char *reg_name, unsigned size,
1548 lldb_private::RegisterValue &value)
1549{
1550 bool result;
1551 ReadDebugRegOperation op(tid, offset, size, value, result);
1552 DoOperation(&op);
1553 return result;
1554}
1555
1556bool
1557ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1558 const char *reg_name,
1559 const lldb_private::RegisterValue &value)
1560{
1561 bool result;
1562 WriteDebugRegOperation op(tid, offset, value, result);
1563 DoOperation(&op);
1564 return result;
1565}
1566
1567bool
Matt Kopec7de48462013-03-06 17:20:48 +00001568ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001569{
1570 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001571 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001572 DoOperation(&op);
1573 return result;
1574}
1575
1576bool
Matt Kopec7de48462013-03-06 17:20:48 +00001577ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001578{
1579 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001580 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001581 DoOperation(&op);
1582 return result;
1583}
1584
1585bool
Ed Maste5d34af32013-06-24 15:09:18 +00001586ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1587{
1588 return false;
1589}
1590
1591bool
Matt Kopec7de48462013-03-06 17:20:48 +00001592ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001593{
1594 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001595 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001596 DoOperation(&op);
1597 return result;
1598}
1599
1600bool
Matt Kopec7de48462013-03-06 17:20:48 +00001601ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001602{
1603 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001604 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001605 DoOperation(&op);
1606 return result;
1607}
1608
1609bool
Ed Maste5d34af32013-06-24 15:09:18 +00001610ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1611{
1612 return false;
1613}
1614
1615bool
Ed Maste68f51792013-10-18 19:16:44 +00001616ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1617{
1618 return false;
1619}
1620
1621bool
Ed Maste502f9022013-11-25 16:31:23 +00001622ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001623{
1624 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001625 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1626
Ed Maste4aeb3c02014-05-28 14:11:20 +00001627 if (log) {
1628 const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo);
1629 if (signame == nullptr)
1630 signame = "<none>";
1631 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1632 __FUNCTION__, GetPID(), signame);
1633 }
Ed Maste502f9022013-11-25 16:31:23 +00001634 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001635 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001636 if (log)
1637 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001638 return result;
1639}
1640
1641bool
Ed Maste502f9022013-11-25 16:31:23 +00001642ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001643{
1644 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001645 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001646 DoOperation(&op);
1647 return result;
1648}
1649
1650bool
Ed Maste70882932014-04-01 14:30:56 +00001651ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001652{
1653 bool result;
1654 KillOperation op(result);
1655 DoOperation(&op);
1656 return result;
1657}
1658
1659bool
Ed Maste819e3992013-07-17 14:02:20 +00001660ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001661{
1662 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001663 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001664 DoOperation(&op);
1665 return result;
1666}
1667
1668bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001669ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1670{
1671 bool result;
1672 ThreadSuspendOperation op(tid, suspend, result);
1673 DoOperation(&op);
1674 return result;
1675}
1676
1677bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001678ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1679{
1680 bool result;
1681 EventMessageOperation op(tid, message, result);
1682 DoOperation(&op);
1683 return result;
1684}
1685
Ed Mastea02f5532013-07-02 16:45:16 +00001686lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001687ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001688{
Ed Mastea02f5532013-07-02 16:45:16 +00001689 lldb_private::Error error;
1690 if (tid != LLDB_INVALID_THREAD_ID)
1691 {
1692 DetachOperation op(error);
1693 DoOperation(&op);
1694 }
1695 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001696}
1697
1698bool
1699ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1700{
1701 int target_fd = open(path, flags, 0666);
1702
1703 if (target_fd == -1)
1704 return false;
1705
1706 return (dup2(target_fd, fd) == -1) ? false : true;
1707}
1708
1709void
1710ProcessMonitor::StopMonitoringChildProcess()
1711{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001712 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001713 {
Zachary Turner39de3112014-09-09 20:54:56 +00001714 m_monitor_thread.Cancel();
1715 m_monitor_thread.Join(nullptr);
1716 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001717 }
1718}
1719
1720void
1721ProcessMonitor::StopMonitor()
1722{
1723 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001724 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001725 sem_destroy(&m_operation_pending);
1726 sem_destroy(&m_operation_done);
1727
Andrew Kaylor5e268992013-09-14 00:17:31 +00001728 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
1729 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
1730 // the descriptor to a ConnectionFileDescriptor object. Consequently
1731 // even though still has the file descriptor, we shouldn't close it here.
Johnny Chen9ed5b492012-01-05 21:48:15 +00001732}
1733
Andrew Kaylord4d54992013-09-17 00:30:24 +00001734// FIXME: On Linux, when a new thread is created, we receive to notifications,
1735// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1736// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1737// the new child thread indicating that it has is stopped because we attached.
1738// We have no guarantee of the order in which these arrive, but we need both
1739// before we are ready to proceed. We currently keep a list of threads which
1740// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1741// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1742// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1743//
1744// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1745// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1746// logically needed.
1747//
1748// We really should figure out what actually happens on FreeBSD and move the
1749// Linux-specific logic out of ProcessPOSIX as needed.
1750
1751bool
1752ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1753{
1754 return true;
1755}
1756
Johnny Chen9ed5b492012-01-05 21:48:15 +00001757void
Ed Mastea02f5532013-07-02 16:45:16 +00001758ProcessMonitor::StopOpThread()
1759{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001760 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001761 return;
1762
Zachary Turner39de3112014-09-09 20:54:56 +00001763 m_operation_thread.Cancel();
1764 m_operation_thread.Join(nullptr);
1765 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001766}