blob: 63439b155111c1e1d34f3f34f852d80954629b86 [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"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Utility/PseudoTerminal.h"
31
32
33#include "POSIXThread.h"
34#include "ProcessFreeBSD.h"
35#include "ProcessPOSIXLog.h"
36#include "ProcessMonitor.h"
37
38extern "C" {
39 extern char ** environ;
40 }
41
42using namespace lldb;
43using namespace lldb_private;
44
45// We disable the tracing of ptrace calls for integration builds to
46// avoid the additional indirection and checks.
47#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
48// Wrapper for ptrace to catch errors and log calls.
49
50const char *
51Get_PT_IO_OP(int op)
52{
53 switch (op) {
54 case PIOD_READ_D: return "READ_D";
55 case PIOD_WRITE_D: return "WRITE_D";
56 case PIOD_READ_I: return "READ_I";
57 case PIOD_WRITE_I: return "WRITE_I";
58 default: return "Unknown op";
59 }
60}
61
Matt Kopec7de48462013-03-06 17:20:48 +000062// Wrapper for ptrace to catch errors and log calls.
63// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000064extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000065PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000066 const char* reqName, const char* file, int line)
67{
68 long int result;
69
Ashok Thirumurthi01186352013-03-28 16:02:31 +000070 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000071
72 if (log) {
Sylvestre Ledru779f9212013-10-31 23:55:19 +000073 log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
Johnny Chen9ed5b492012-01-05 21:48:15 +000074 reqName, pid, addr, data, file, line);
75 if (req == PT_IO) {
76 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
77
Sylvestre Ledru779f9212013-10-31 23:55:19 +000078 log->Printf("PT_IO: op=%s offs=%zx size=%zu",
Ed Mastea708a362013-06-25 14:47:45 +000079 Get_PT_IO_OP(pi->piod_op), (size_t)pi->piod_offs, pi->piod_len);
Johnny Chen9ed5b492012-01-05 21:48:15 +000080 }
81 }
82
83 //PtraceDisplayBytes(req, data);
84
85 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000086 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000087
88 //PtraceDisplayBytes(req, data);
89
Matt Kopec7de48462013-03-06 17:20:48 +000090 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000091 {
92 const char* str;
93 switch (errno)
94 {
95 case ESRCH: str = "ESRCH"; break;
96 case EINVAL: str = "EINVAL"; break;
97 case EBUSY: str = "EBUSY"; break;
98 case EPERM: str = "EPERM"; break;
99 default: str = "<unknown>";
100 }
101 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
102 }
103
104 if (log) {
Ed Mastea4be2c52014-02-19 18:34:06 +0000105#ifdef __amd64__
Johnny Chen9ed5b492012-01-05 21:48:15 +0000106 if (req == PT_GETREGS) {
107 struct reg *r = (struct reg *) addr;
108
109 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
110 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
111 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
112 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
113 }
Ed Maste7d4c0d5b2013-07-22 20:51:08 +0000114#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000115 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
116 struct dbreg *r = (struct dbreg *) addr;
117 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
118
119 for (int i = 0; i <= 7; i++)
120 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
121 }
122 }
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 {
312 if (m_size == sizeof(uintptr_t))
313 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
314 else
315 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
316 m_result = true;
317 }
318}
319
320//------------------------------------------------------------------------------
321/// @class WriteRegOperation
322/// @brief Implements ProcessMonitor::WriteRegisterValue.
323class WriteRegOperation : public Operation
324{
325public:
Ed Maste6f066412013-07-04 21:47:32 +0000326 WriteRegOperation(lldb::tid_t tid, unsigned offset,
327 const RegisterValue &value, bool &result)
328 : m_tid(tid), m_offset(offset),
329 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000330 { }
331
332 void Execute(ProcessMonitor *monitor);
333
334private:
Ed Maste6f066412013-07-04 21:47:32 +0000335 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000336 unsigned m_offset;
337 const RegisterValue &m_value;
338 bool &m_result;
339};
340
341void
342WriteRegOperation::Execute(ProcessMonitor *monitor)
343{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000344 struct reg regs;
345
Ed Maste6f066412013-07-04 21:47:32 +0000346 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000347 m_result = false;
348 return;
349 }
350 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000351 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000352 m_result = false;
353 else
354 m_result = true;
355}
356
357//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000358/// @class ReadDebugRegOperation
359/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
360class ReadDebugRegOperation : public Operation
361{
362public:
363 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
364 RegisterValue &value, bool &result)
365 : m_tid(tid), m_offset(offset), m_size(size),
366 m_value(value), m_result(result)
367 { }
368
369 void Execute(ProcessMonitor *monitor);
370
371private:
372 lldb::tid_t m_tid;
373 unsigned m_offset;
374 unsigned m_size;
375 RegisterValue &m_value;
376 bool &m_result;
377};
378
379void
380ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
381{
382 struct dbreg regs;
383 int rc;
384
385 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
386 m_result = false;
387 } else {
388 if (m_size == sizeof(uintptr_t))
389 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
390 else
391 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
392 m_result = true;
393 }
394}
395
396//------------------------------------------------------------------------------
397/// @class WriteDebugRegOperation
398/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
399class WriteDebugRegOperation : public Operation
400{
401public:
402 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
403 const RegisterValue &value, bool &result)
404 : m_tid(tid), m_offset(offset),
405 m_value(value), m_result(result)
406 { }
407
408 void Execute(ProcessMonitor *monitor);
409
410private:
411 lldb::tid_t m_tid;
412 unsigned m_offset;
413 const RegisterValue &m_value;
414 bool &m_result;
415};
416
417void
418WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
419{
420 struct dbreg regs;
421
422 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
423 m_result = false;
424 return;
425 }
426 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
427 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
428 m_result = false;
429 else
430 m_result = true;
431}
432
433//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000434/// @class ReadGPROperation
435/// @brief Implements ProcessMonitor::ReadGPR.
436class ReadGPROperation : public Operation
437{
438public:
Ed Maste6f066412013-07-04 21:47:32 +0000439 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
440 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000441 { }
442
443 void Execute(ProcessMonitor *monitor);
444
445private:
Ed Maste6f066412013-07-04 21:47:32 +0000446 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000447 void *m_buf;
448 bool &m_result;
449};
450
451void
452ReadGPROperation::Execute(ProcessMonitor *monitor)
453{
454 int rc;
455
456 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000457 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000458 if (errno != 0)
459 m_result = false;
460 else
461 m_result = true;
462}
463
464//------------------------------------------------------------------------------
465/// @class ReadFPROperation
466/// @brief Implements ProcessMonitor::ReadFPR.
467class ReadFPROperation : public Operation
468{
469public:
Ed Maste6f066412013-07-04 21:47:32 +0000470 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
471 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000472 { }
473
474 void Execute(ProcessMonitor *monitor);
475
476private:
Ed Maste6f066412013-07-04 21:47:32 +0000477 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000478 void *m_buf;
479 bool &m_result;
480};
481
482void
483ReadFPROperation::Execute(ProcessMonitor *monitor)
484{
Ed Maste6f066412013-07-04 21:47:32 +0000485 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486 m_result = false;
487 else
488 m_result = true;
489}
490
491//------------------------------------------------------------------------------
492/// @class WriteGPROperation
493/// @brief Implements ProcessMonitor::WriteGPR.
494class WriteGPROperation : public Operation
495{
496public:
Ed Maste6f066412013-07-04 21:47:32 +0000497 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
498 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000499 { }
500
501 void Execute(ProcessMonitor *monitor);
502
503private:
Ed Maste6f066412013-07-04 21:47:32 +0000504 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000505 void *m_buf;
506 bool &m_result;
507};
508
509void
510WriteGPROperation::Execute(ProcessMonitor *monitor)
511{
Ed Maste6f066412013-07-04 21:47:32 +0000512 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513 m_result = false;
514 else
515 m_result = true;
516}
517
518//------------------------------------------------------------------------------
519/// @class WriteFPROperation
520/// @brief Implements ProcessMonitor::WriteFPR.
521class WriteFPROperation : public Operation
522{
523public:
Ed Maste6f066412013-07-04 21:47:32 +0000524 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
525 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000526 { }
527
528 void Execute(ProcessMonitor *monitor);
529
530private:
Ed Maste6f066412013-07-04 21:47:32 +0000531 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000532 void *m_buf;
533 bool &m_result;
534};
535
536void
537WriteFPROperation::Execute(ProcessMonitor *monitor)
538{
Ed Maste6f066412013-07-04 21:47:32 +0000539 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540 m_result = false;
541 else
542 m_result = true;
543}
544
545//------------------------------------------------------------------------------
546/// @class ResumeOperation
547/// @brief Implements ProcessMonitor::Resume.
548class ResumeOperation : public Operation
549{
550public:
Ed Maste502f9022013-11-25 16:31:23 +0000551 ResumeOperation(uint32_t signo, bool &result) :
552 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000553
554 void Execute(ProcessMonitor *monitor);
555
556private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000557 uint32_t m_signo;
558 bool &m_result;
559};
560
561void
562ResumeOperation::Execute(ProcessMonitor *monitor)
563{
Ed Maste502f9022013-11-25 16:31:23 +0000564 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000565 int data = 0;
566
567 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
568 data = m_signo;
569
Ed Maste502f9022013-11-25 16:31:23 +0000570 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000571 {
572 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
573
574 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000575 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000576 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000577 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000578 else
579 m_result = true;
580}
581
582//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000583/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584/// @brief Implements ProcessMonitor::SingleStep.
585class SingleStepOperation : public Operation
586{
587public:
Ed Maste502f9022013-11-25 16:31:23 +0000588 SingleStepOperation(uint32_t signo, bool &result)
589 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000590
591 void Execute(ProcessMonitor *monitor);
592
593private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000594 uint32_t m_signo;
595 bool &m_result;
596};
597
598void
599SingleStepOperation::Execute(ProcessMonitor *monitor)
600{
Ed Maste502f9022013-11-25 16:31:23 +0000601 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000602 int data = 0;
603
604 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
605 data = m_signo;
606
Ed Maste502f9022013-11-25 16:31:23 +0000607 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000608 m_result = false;
609 else
610 m_result = true;
611}
612
613//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000614/// @class LwpInfoOperation
615/// @brief Implements ProcessMonitor::GetLwpInfo.
616class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000617{
618public:
Ed Maste819e3992013-07-17 14:02:20 +0000619 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000620 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000621
622 void Execute(ProcessMonitor *monitor);
623
624private:
625 lldb::tid_t m_tid;
626 void *m_info;
627 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000628 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000629};
630
631void
Ed Maste819e3992013-07-17 14:02:20 +0000632LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000633{
634 struct ptrace_lwpinfo plwp;
635
Daniel Maleaa35970a2012-11-23 18:09:58 +0000636 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000637 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000638 m_err = errno;
639 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000640 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000641 m_result = true;
642 }
643}
644
645//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000646/// @class ThreadSuspendOperation
647/// @brief Implements ProcessMonitor::ThreadSuspend.
648class ThreadSuspendOperation : public Operation
649{
650public:
651 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
652 : m_tid(tid), m_suspend(suspend), m_result(result) { }
653
654 void Execute(ProcessMonitor *monitor);
655
656private:
657 lldb::tid_t m_tid;
658 bool m_suspend;
659 bool &m_result;
660} ;
661
662void
663ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
664{
665 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
666}
667
668
669
670//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000671/// @class EventMessageOperation
672/// @brief Implements ProcessMonitor::GetEventMessage.
673class EventMessageOperation : public Operation
674{
675public:
676 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
677 : m_tid(tid), m_message(message), m_result(result) { }
678
679 void Execute(ProcessMonitor *monitor);
680
681private:
682 lldb::tid_t m_tid;
683 unsigned long *m_message;
684 bool &m_result;
685};
686
687void
688EventMessageOperation::Execute(ProcessMonitor *monitor)
689{
690 struct ptrace_lwpinfo plwp;
691
692 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
693 m_result = false;
694 else {
695 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000696 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000697 m_result = true;
698 } else
699 m_result = false;
700 }
701}
702
703//------------------------------------------------------------------------------
704/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000705/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000706class KillOperation : public Operation
707{
708public:
709 KillOperation(bool &result) : m_result(result) { }
710
711 void Execute(ProcessMonitor *monitor);
712
713private:
714 bool &m_result;
715};
716
717void
718KillOperation::Execute(ProcessMonitor *monitor)
719{
720 lldb::pid_t pid = monitor->GetPID();
721
722 if (PTRACE(PT_KILL, pid, NULL, 0))
723 m_result = false;
724 else
725 m_result = true;
726}
727
728//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000729/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000730/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000731class DetachOperation : public Operation
732{
733public:
734 DetachOperation(Error &result) : m_error(result) { }
735
736 void Execute(ProcessMonitor *monitor);
737
738private:
739 Error &m_error;
740};
741
742void
743DetachOperation::Execute(ProcessMonitor *monitor)
744{
745 lldb::pid_t pid = monitor->GetPID();
746
747 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
748 m_error.SetErrorToErrno();
749
750}
751
752ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
753 : m_monitor(monitor)
754{
755 sem_init(&m_semaphore, 0, 0);
756}
757
758ProcessMonitor::OperationArgs::~OperationArgs()
759{
760 sem_destroy(&m_semaphore);
761}
762
763ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
764 lldb_private::Module *module,
765 char const **argv,
766 char const **envp,
767 const char *stdin_path,
768 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000769 const char *stderr_path,
770 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000771 : OperationArgs(monitor),
772 m_module(module),
773 m_argv(argv),
774 m_envp(envp),
775 m_stdin_path(stdin_path),
776 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000777 m_stderr_path(stderr_path),
778 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000779
780ProcessMonitor::LaunchArgs::~LaunchArgs()
781{ }
782
783ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
784 lldb::pid_t pid)
785 : OperationArgs(monitor), m_pid(pid) { }
786
787ProcessMonitor::AttachArgs::~AttachArgs()
788{ }
789
790//------------------------------------------------------------------------------
791/// The basic design of the ProcessMonitor is built around two threads.
792///
793/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
794/// for changes in the debugee state. When a change is detected a
795/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
796/// "drives" state changes in the debugger.
797///
798/// The second thread (@see OperationThread) is responsible for two things 1)
799/// launching or attaching to the inferior process, and then 2) servicing
800/// operations such as register reads/writes, stepping, etc. See the comments
801/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000802ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000803 Module *module,
804 const char *argv[],
805 const char *envp[],
806 const char *stdin_path,
807 const char *stdout_path,
808 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000809 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000810 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000811 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000812 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000813 m_operation_thread(LLDB_INVALID_HOST_THREAD),
814 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
815 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000816 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000817 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000818{
Ed Maste756e1ff2013-09-18 19:34:08 +0000819 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
820 stdin_path, stdout_path, stderr_path,
821 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822
823
Ed Maste756e1ff2013-09-18 19:34:08 +0000824 sem_init(&m_operation_pending, 0, 0);
825 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000826
827 StartLaunchOpThread(args.get(), error);
828 if (!error.Success())
829 return;
830
831WAIT_AGAIN:
832 // Wait for the operation thread to initialize.
833 if (sem_wait(&args->m_semaphore))
834 {
835 if (errno == EINTR)
836 goto WAIT_AGAIN;
837 else
838 {
839 error.SetErrorToErrno();
840 return;
841 }
842 }
843
844 // Check that the launch was a success.
845 if (!args->m_error.Success())
846 {
Ed Mastea02f5532013-07-02 16:45:16 +0000847 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000848 error = args->m_error;
849 return;
850 }
851
852 // Finally, start monitoring the child process for change in state.
853 m_monitor_thread = Host::StartMonitoringChildProcess(
854 ProcessMonitor::MonitorCallback, this, GetPID(), true);
855 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
856 {
857 error.SetErrorToGenericError();
858 error.SetErrorString("Process launch failed.");
859 return;
860 }
861}
862
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000863ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000864 lldb::pid_t pid,
865 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000866 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000867 m_operation_thread(LLDB_INVALID_HOST_THREAD),
868 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
869 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000870 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000871 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000872{
Ed Maste756e1ff2013-09-18 19:34:08 +0000873 sem_init(&m_operation_pending, 0, 0);
874 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000875
Johnny Chen9ed5b492012-01-05 21:48:15 +0000876
Ed Maste756e1ff2013-09-18 19:34:08 +0000877 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000878
879 StartAttachOpThread(args.get(), error);
880 if (!error.Success())
881 return;
882
883WAIT_AGAIN:
884 // Wait for the operation thread to initialize.
885 if (sem_wait(&args->m_semaphore))
886 {
887 if (errno == EINTR)
888 goto WAIT_AGAIN;
889 else
890 {
891 error.SetErrorToErrno();
892 return;
893 }
894 }
895
Ed Mastea02f5532013-07-02 16:45:16 +0000896 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000897 if (!args->m_error.Success())
898 {
Ed Mastea02f5532013-07-02 16:45:16 +0000899 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000900 error = args->m_error;
901 return;
902 }
903
904 // Finally, start monitoring the child process for change in state.
905 m_monitor_thread = Host::StartMonitoringChildProcess(
906 ProcessMonitor::MonitorCallback, this, GetPID(), true);
907 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
908 {
909 error.SetErrorToGenericError();
910 error.SetErrorString("Process attach failed.");
911 return;
912 }
913}
914
915ProcessMonitor::~ProcessMonitor()
916{
917 StopMonitor();
918}
919
920//------------------------------------------------------------------------------
921// Thread setup and tear down.
922void
923ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
924{
925 static const char *g_thread_name = "lldb.process.freebsd.operation";
926
927 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
928 return;
929
930 m_operation_thread =
931 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
932}
933
Johnny Chen9ed5b492012-01-05 21:48:15 +0000934void *
935ProcessMonitor::LaunchOpThread(void *arg)
936{
937 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
938
939 if (!Launch(args)) {
940 sem_post(&args->m_semaphore);
941 return NULL;
942 }
943
944 ServeOperation(args);
945 return NULL;
946}
947
948bool
949ProcessMonitor::Launch(LaunchArgs *args)
950{
951 ProcessMonitor *monitor = args->m_monitor;
952 ProcessFreeBSD &process = monitor->GetProcess();
953 const char **argv = args->m_argv;
954 const char **envp = args->m_envp;
955 const char *stdin_path = args->m_stdin_path;
956 const char *stdout_path = args->m_stdout_path;
957 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000958 const char *working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000959
960 lldb_utility::PseudoTerminal terminal;
961 const size_t err_len = 1024;
962 char err_str[err_len];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000963 lldb::pid_t pid;
964
Johnny Chen9ed5b492012-01-05 21:48:15 +0000965 // Propagate the environment if one is not supplied.
966 if (envp == NULL || envp[0] == NULL)
967 envp = const_cast<const char **>(environ);
968
Ed Mastea02f5532013-07-02 16:45:16 +0000969 if ((pid = terminal.Fork(err_str, err_len)) == -1)
970 {
971 args->m_error.SetErrorToGenericError();
972 args->m_error.SetErrorString("Process fork failed.");
973 goto FINISH;
974 }
975
Johnny Chen9ed5b492012-01-05 21:48:15 +0000976 // Recognized child exit status codes.
977 enum {
978 ePtraceFailed = 1,
979 eDupStdinFailed,
980 eDupStdoutFailed,
981 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000982 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000983 eExecFailed,
984 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000985 };
986
Johnny Chen9ed5b492012-01-05 21:48:15 +0000987 // Child process.
988 if (pid == 0)
989 {
990 // Trace this process.
991 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
992 exit(ePtraceFailed);
993
994 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +0000995 if (setgid(getgid()) != 0)
996 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000997
998 // Let us have our own process group.
999 setpgid(0, 0);
1000
1001 // Dup file descriptors if needed.
1002 //
1003 // FIXME: If two or more of the paths are the same we needlessly open
1004 // the same file multiple times.
1005 if (stdin_path != NULL && stdin_path[0])
1006 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1007 exit(eDupStdinFailed);
1008
1009 if (stdout_path != NULL && stdout_path[0])
1010 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1011 exit(eDupStdoutFailed);
1012
1013 if (stderr_path != NULL && stderr_path[0])
1014 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1015 exit(eDupStderrFailed);
1016
Daniel Malea6217d2a2013-01-08 14:49:22 +00001017 // Change working directory
1018 if (working_dir != NULL && working_dir[0])
1019 if (0 != ::chdir(working_dir))
1020 exit(eChdirFailed);
1021
Johnny Chen9ed5b492012-01-05 21:48:15 +00001022 // Execute. We should never return.
1023 execve(argv[0],
1024 const_cast<char *const *>(argv),
1025 const_cast<char *const *>(envp));
1026 exit(eExecFailed);
1027 }
1028
1029 // Wait for the child process to to trap on its call to execve.
1030 ::pid_t wpid;
1031 int status;
1032 if ((wpid = waitpid(pid, &status, 0)) < 0)
1033 {
1034 args->m_error.SetErrorToErrno();
1035 goto FINISH;
1036 }
1037 else if (WIFEXITED(status))
1038 {
1039 // open, dup or execve likely failed for some reason.
1040 args->m_error.SetErrorToGenericError();
1041 switch (WEXITSTATUS(status))
1042 {
Ed Maste5d34af32013-06-24 15:09:18 +00001043 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001044 args->m_error.SetErrorString("Child ptrace failed.");
1045 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001046 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001047 args->m_error.SetErrorString("Child open stdin failed.");
1048 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001049 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001050 args->m_error.SetErrorString("Child open stdout failed.");
1051 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001052 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001053 args->m_error.SetErrorString("Child open stderr failed.");
1054 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001055 case eChdirFailed:
1056 args->m_error.SetErrorString("Child failed to set working directory.");
1057 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001058 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001059 args->m_error.SetErrorString("Child exec failed.");
1060 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001061 case eSetGidFailed:
1062 args->m_error.SetErrorString("Child setgid failed.");
1063 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001064 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001065 args->m_error.SetErrorString("Child returned unknown exit status.");
1066 break;
1067 }
1068 goto FINISH;
1069 }
Ed Maste82a00052013-11-25 21:15:53 +00001070 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001071 "Could not sync with inferior process.");
1072
1073#ifdef notyet
1074 // Have the child raise an event on exit. This is used to keep the child in
1075 // limbo until it is destroyed.
1076 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1077 {
1078 args->m_error.SetErrorToErrno();
1079 goto FINISH;
1080 }
1081#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001082 // Release the master terminal descriptor and pass it off to the
1083 // ProcessMonitor instance. Similarly stash the inferior pid.
1084 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001085 monitor->m_pid = pid;
1086
1087 // Set the terminal fd to be in non blocking mode (it simplifies the
1088 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1089 // descriptor to read from).
1090 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1091 goto FINISH;
1092
Ed Mastee5441432013-09-03 23:55:30 +00001093 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094
1095FINISH:
1096 return args->m_error.Success();
1097}
1098
Johnny Chen9ed5b492012-01-05 21:48:15 +00001099void
1100ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1101{
1102 static const char *g_thread_name = "lldb.process.freebsd.operation";
1103
1104 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1105 return;
1106
1107 m_operation_thread =
1108 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1109}
1110
Johnny Chen9ed5b492012-01-05 21:48:15 +00001111void *
1112ProcessMonitor::AttachOpThread(void *arg)
1113{
1114 AttachArgs *args = static_cast<AttachArgs*>(arg);
1115
1116 if (!Attach(args))
1117 return NULL;
1118
1119 ServeOperation(args);
1120 return NULL;
1121}
1122
1123bool
1124ProcessMonitor::Attach(AttachArgs *args)
1125{
1126 lldb::pid_t pid = args->m_pid;
1127
1128 ProcessMonitor *monitor = args->m_monitor;
1129 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001130
1131 if (pid <= 1)
1132 {
1133 args->m_error.SetErrorToGenericError();
1134 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1135 goto FINISH;
1136 }
1137
1138 // Attach to the requested process.
1139 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1140 {
1141 args->m_error.SetErrorToErrno();
1142 goto FINISH;
1143 }
1144
1145 int status;
1146 if ((status = waitpid(pid, NULL, 0)) < 0)
1147 {
1148 args->m_error.SetErrorToErrno();
1149 goto FINISH;
1150 }
1151
Ed Mastee5441432013-09-03 23:55:30 +00001152 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001153
Ed Mastee5441432013-09-03 23:55:30 +00001154FINISH:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001155 return args->m_error.Success();
1156}
1157
Ed Maste7fd845c2013-12-09 15:51:17 +00001158size_t
1159ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1160{
1161 lwpid_t *tids;
1162 int tdcnt;
1163
1164 thread_ids.clear();
1165
1166 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1167 if (tdcnt <= 0)
1168 return 0;
1169 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1170 if (tids == NULL)
1171 return 0;
1172 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1173 free(tids);
1174 return 0;
1175 }
1176 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1177 free(tids);
1178 return thread_ids.size();
1179}
1180
Johnny Chen9ed5b492012-01-05 21:48:15 +00001181bool
1182ProcessMonitor::MonitorCallback(void *callback_baton,
1183 lldb::pid_t pid,
1184 bool exited,
1185 int signal,
1186 int status)
1187{
1188 ProcessMessage message;
1189 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001190 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001191 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001192 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001193 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001194 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001195
Ed Mastea02f5532013-07-02 16:45:16 +00001196 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1197
1198 if (exited)
1199 {
1200 if (log)
1201 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1202 message = ProcessMessage::Exit(pid, status);
1203 process->SendMessage(message);
1204 return pid == process->GetID();
1205 }
1206
Ed Maste819e3992013-07-17 14:02:20 +00001207 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001208 stop_monitoring = true; // pid is gone. Bail.
1209 else {
Ed Maste819e3992013-07-17 14:02:20 +00001210 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001211 {
1212 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001213 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001214 break;
1215
1216 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001217 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001218 break;
1219 }
1220
1221 process->SendMessage(message);
1222 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1223 }
1224
1225 return stop_monitoring;
1226}
1227
1228ProcessMessage
1229ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001230 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001231{
1232 ProcessMessage message;
1233
Ed Mastea02f5532013-07-02 16:45:16 +00001234 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1235
Matt Kopec7de48462013-03-06 17:20:48 +00001236 assert(monitor);
1237 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001238
1239 switch (info->si_code)
1240 {
1241 default:
1242 assert(false && "Unexpected SIGTRAP code!");
1243 break;
1244
1245 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1246 {
1247 // The inferior process is about to exit. Maintain the process in a
1248 // state of "limbo" until we are explicitly commanded to detach,
1249 // destroy, resume, etc.
1250 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001251 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001252 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001253 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001254 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1255 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001256 break;
1257 }
1258
1259 case 0:
1260 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001261 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001262 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001263 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001264 break;
1265
1266 case SI_KERNEL:
1267 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001268 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001269 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1270 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001271 break;
1272 }
1273
1274 return message;
1275}
1276
1277ProcessMessage
1278ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001279 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001280{
1281 ProcessMessage message;
1282 int signo = info->si_signo;
1283
Ed Mastea02f5532013-07-02 16:45:16 +00001284 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1285
Johnny Chen9ed5b492012-01-05 21:48:15 +00001286 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1287 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1288 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1289 //
1290 // IOW, user generated signals never generate what we consider to be a
1291 // "crash".
1292 //
1293 // Similarly, ACK signals generated by this monitor.
1294 if (info->si_code == SI_USER)
1295 {
Ed Mastea02f5532013-07-02 16:45:16 +00001296 if (log)
1297 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1298 __FUNCTION__,
1299 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1300 "SI_USER",
1301 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001302 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001303 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001304 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001305 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001306 }
1307
Ed Mastea02f5532013-07-02 16:45:16 +00001308 if (log)
1309 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1310
Johnny Chen9ed5b492012-01-05 21:48:15 +00001311 if (signo == SIGSEGV) {
1312 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1313 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001314 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001315 }
1316
1317 if (signo == SIGILL) {
1318 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1319 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(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 if (signo == SIGFPE) {
1324 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1325 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001326 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001327 }
1328
1329 if (signo == SIGBUS) {
1330 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1331 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001332 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001333 }
1334
1335 // Everything else is "normal" and does not require any special action on
1336 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001337 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001338}
1339
1340ProcessMessage::CrashReason
1341ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1342{
1343 ProcessMessage::CrashReason reason;
1344 assert(info->si_signo == SIGSEGV);
1345
1346 reason = ProcessMessage::eInvalidCrashReason;
1347
1348 switch (info->si_code)
1349 {
1350 default:
1351 assert(false && "unexpected si_code for SIGSEGV");
1352 break;
1353 case SEGV_MAPERR:
1354 reason = ProcessMessage::eInvalidAddress;
1355 break;
1356 case SEGV_ACCERR:
1357 reason = ProcessMessage::ePrivilegedAddress;
1358 break;
1359 }
1360
1361 return reason;
1362}
1363
1364ProcessMessage::CrashReason
1365ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1366{
1367 ProcessMessage::CrashReason reason;
1368 assert(info->si_signo == SIGILL);
1369
1370 reason = ProcessMessage::eInvalidCrashReason;
1371
1372 switch (info->si_code)
1373 {
1374 default:
1375 assert(false && "unexpected si_code for SIGILL");
1376 break;
1377 case ILL_ILLOPC:
1378 reason = ProcessMessage::eIllegalOpcode;
1379 break;
1380 case ILL_ILLOPN:
1381 reason = ProcessMessage::eIllegalOperand;
1382 break;
1383 case ILL_ILLADR:
1384 reason = ProcessMessage::eIllegalAddressingMode;
1385 break;
1386 case ILL_ILLTRP:
1387 reason = ProcessMessage::eIllegalTrap;
1388 break;
1389 case ILL_PRVOPC:
1390 reason = ProcessMessage::ePrivilegedOpcode;
1391 break;
1392 case ILL_PRVREG:
1393 reason = ProcessMessage::ePrivilegedRegister;
1394 break;
1395 case ILL_COPROC:
1396 reason = ProcessMessage::eCoprocessorError;
1397 break;
1398 case ILL_BADSTK:
1399 reason = ProcessMessage::eInternalStackError;
1400 break;
1401 }
1402
1403 return reason;
1404}
1405
1406ProcessMessage::CrashReason
1407ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1408{
1409 ProcessMessage::CrashReason reason;
1410 assert(info->si_signo == SIGFPE);
1411
1412 reason = ProcessMessage::eInvalidCrashReason;
1413
1414 switch (info->si_code)
1415 {
1416 default:
1417 assert(false && "unexpected si_code for SIGFPE");
1418 break;
1419 case FPE_INTDIV:
1420 reason = ProcessMessage::eIntegerDivideByZero;
1421 break;
1422 case FPE_INTOVF:
1423 reason = ProcessMessage::eIntegerOverflow;
1424 break;
1425 case FPE_FLTDIV:
1426 reason = ProcessMessage::eFloatDivideByZero;
1427 break;
1428 case FPE_FLTOVF:
1429 reason = ProcessMessage::eFloatOverflow;
1430 break;
1431 case FPE_FLTUND:
1432 reason = ProcessMessage::eFloatUnderflow;
1433 break;
1434 case FPE_FLTRES:
1435 reason = ProcessMessage::eFloatInexactResult;
1436 break;
1437 case FPE_FLTINV:
1438 reason = ProcessMessage::eFloatInvalidOperation;
1439 break;
1440 case FPE_FLTSUB:
1441 reason = ProcessMessage::eFloatSubscriptRange;
1442 break;
1443 }
1444
1445 return reason;
1446}
1447
1448ProcessMessage::CrashReason
1449ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1450{
1451 ProcessMessage::CrashReason reason;
1452 assert(info->si_signo == SIGBUS);
1453
1454 reason = ProcessMessage::eInvalidCrashReason;
1455
1456 switch (info->si_code)
1457 {
1458 default:
1459 assert(false && "unexpected si_code for SIGBUS");
1460 break;
1461 case BUS_ADRALN:
1462 reason = ProcessMessage::eIllegalAlignment;
1463 break;
1464 case BUS_ADRERR:
1465 reason = ProcessMessage::eIllegalAddress;
1466 break;
1467 case BUS_OBJERR:
1468 reason = ProcessMessage::eHardwareError;
1469 break;
1470 }
1471
1472 return reason;
1473}
1474
1475void
1476ProcessMonitor::ServeOperation(OperationArgs *args)
1477{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001478 ProcessMonitor *monitor = args->m_monitor;
1479
Johnny Chen9ed5b492012-01-05 21:48:15 +00001480 // We are finised with the arguments and are ready to go. Sync with the
1481 // parent thread and start serving operations on the inferior.
1482 sem_post(&args->m_semaphore);
1483
1484 for (;;)
1485 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001486 // wait for next pending operation
1487 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001488
Ed Maste756e1ff2013-09-18 19:34:08 +00001489 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001490
Ed Maste756e1ff2013-09-18 19:34:08 +00001491 // notify calling thread that operation is complete
1492 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001493 }
1494}
1495
1496void
1497ProcessMonitor::DoOperation(Operation *op)
1498{
Ed Maste756e1ff2013-09-18 19:34:08 +00001499 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001500
Ed Maste756e1ff2013-09-18 19:34:08 +00001501 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001502
Ed Maste756e1ff2013-09-18 19:34:08 +00001503 // notify operation thread that an operation is ready to be processed
1504 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001505
Ed Maste756e1ff2013-09-18 19:34:08 +00001506 // wait for operation to complete
1507 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001508}
1509
1510size_t
1511ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1512 Error &error)
1513{
1514 size_t result;
1515 ReadOperation op(vm_addr, buf, size, error, result);
1516 DoOperation(&op);
1517 return result;
1518}
1519
1520size_t
1521ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1522 lldb_private::Error &error)
1523{
1524 size_t result;
1525 WriteOperation op(vm_addr, buf, size, error, result);
1526 DoOperation(&op);
1527 return result;
1528}
1529
1530bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001531ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001532 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001533{
1534 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001535 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001536 DoOperation(&op);
1537 return result;
1538}
1539
1540bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001541ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001542 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001543{
1544 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001545 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001546 DoOperation(&op);
1547 return result;
1548}
1549
1550bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001551ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1552 const char *reg_name, unsigned size,
1553 lldb_private::RegisterValue &value)
1554{
1555 bool result;
1556 ReadDebugRegOperation op(tid, offset, size, value, result);
1557 DoOperation(&op);
1558 return result;
1559}
1560
1561bool
1562ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1563 const char *reg_name,
1564 const lldb_private::RegisterValue &value)
1565{
1566 bool result;
1567 WriteDebugRegOperation op(tid, offset, value, result);
1568 DoOperation(&op);
1569 return result;
1570}
1571
1572bool
Matt Kopec7de48462013-03-06 17:20:48 +00001573ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001574{
1575 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001576 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001577 DoOperation(&op);
1578 return result;
1579}
1580
1581bool
Matt Kopec7de48462013-03-06 17:20:48 +00001582ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001583{
1584 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001585 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001586 DoOperation(&op);
1587 return result;
1588}
1589
1590bool
Ed Maste5d34af32013-06-24 15:09:18 +00001591ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1592{
1593 return false;
1594}
1595
1596bool
Matt Kopec7de48462013-03-06 17:20:48 +00001597ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001598{
1599 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001600 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001601 DoOperation(&op);
1602 return result;
1603}
1604
1605bool
Matt Kopec7de48462013-03-06 17:20:48 +00001606ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001607{
1608 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001609 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001610 DoOperation(&op);
1611 return result;
1612}
1613
1614bool
Ed Maste5d34af32013-06-24 15:09:18 +00001615ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1616{
1617 return false;
1618}
1619
1620bool
Ed Maste68f51792013-10-18 19:16:44 +00001621ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1622{
1623 return false;
1624}
1625
1626bool
Ed Maste502f9022013-11-25 16:31:23 +00001627ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001628{
1629 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001630 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1631
Ed Maste4aeb3c02014-05-28 14:11:20 +00001632 if (log) {
1633 const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo);
1634 if (signame == nullptr)
1635 signame = "<none>";
1636 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1637 __FUNCTION__, GetPID(), signame);
1638 }
Ed Maste502f9022013-11-25 16:31:23 +00001639 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001640 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001641 if (log)
1642 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001643 return result;
1644}
1645
1646bool
Ed Maste502f9022013-11-25 16:31:23 +00001647ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001648{
1649 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001650 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001651 DoOperation(&op);
1652 return result;
1653}
1654
1655bool
Ed Maste70882932014-04-01 14:30:56 +00001656ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001657{
1658 bool result;
1659 KillOperation op(result);
1660 DoOperation(&op);
1661 return result;
1662}
1663
1664bool
Ed Maste819e3992013-07-17 14:02:20 +00001665ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001666{
1667 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001668 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001669 DoOperation(&op);
1670 return result;
1671}
1672
1673bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001674ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1675{
1676 bool result;
1677 ThreadSuspendOperation op(tid, suspend, result);
1678 DoOperation(&op);
1679 return result;
1680}
1681
1682bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001683ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1684{
1685 bool result;
1686 EventMessageOperation op(tid, message, result);
1687 DoOperation(&op);
1688 return result;
1689}
1690
Ed Mastea02f5532013-07-02 16:45:16 +00001691lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001692ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001693{
Ed Mastea02f5532013-07-02 16:45:16 +00001694 lldb_private::Error error;
1695 if (tid != LLDB_INVALID_THREAD_ID)
1696 {
1697 DetachOperation op(error);
1698 DoOperation(&op);
1699 }
1700 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001701}
1702
1703bool
1704ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1705{
1706 int target_fd = open(path, flags, 0666);
1707
1708 if (target_fd == -1)
1709 return false;
1710
1711 return (dup2(target_fd, fd) == -1) ? false : true;
1712}
1713
1714void
1715ProcessMonitor::StopMonitoringChildProcess()
1716{
1717 lldb::thread_result_t thread_result;
1718
1719 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1720 {
1721 Host::ThreadCancel(m_monitor_thread, NULL);
1722 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1723 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1724 }
1725}
1726
1727void
1728ProcessMonitor::StopMonitor()
1729{
1730 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001731 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001732 sem_destroy(&m_operation_pending);
1733 sem_destroy(&m_operation_done);
1734
Andrew Kaylor5e268992013-09-14 00:17:31 +00001735 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
1736 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
1737 // the descriptor to a ConnectionFileDescriptor object. Consequently
1738 // even though still has the file descriptor, we shouldn't close it here.
Johnny Chen9ed5b492012-01-05 21:48:15 +00001739}
1740
Andrew Kaylord4d54992013-09-17 00:30:24 +00001741// FIXME: On Linux, when a new thread is created, we receive to notifications,
1742// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1743// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1744// the new child thread indicating that it has is stopped because we attached.
1745// We have no guarantee of the order in which these arrive, but we need both
1746// before we are ready to proceed. We currently keep a list of threads which
1747// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1748// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1749// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1750//
1751// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1752// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1753// logically needed.
1754//
1755// We really should figure out what actually happens on FreeBSD and move the
1756// Linux-specific logic out of ProcessPOSIX as needed.
1757
1758bool
1759ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1760{
1761 return true;
1762}
1763
Johnny Chen9ed5b492012-01-05 21:48:15 +00001764void
Ed Mastea02f5532013-07-02 16:45:16 +00001765ProcessMonitor::StopOpThread()
1766{
1767 lldb::thread_result_t result;
1768
1769 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1770 return;
1771
1772 Host::ThreadCancel(m_operation_thread, NULL);
1773 Host::ThreadJoin(m_operation_thread, &result, NULL);
1774 m_operation_thread = LLDB_INVALID_HOST_THREAD;
1775}