blob: 84e35ba22644ba0c300076c8b7933b7cfc3ac8c7 [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
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000116#ifndef __powerpc__
Ed Mastea4be2c52014-02-19 18:34:06 +0000117 if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
118 struct dbreg *r = (struct dbreg *) addr;
119 char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
120
121 for (int i = 0; i <= 7; i++)
122 log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
123 }
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000124#endif
Ed Mastea4be2c52014-02-19 18:34:06 +0000125 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000126
127 return result;
128}
129
Matt Kopec7de48462013-03-06 17:20:48 +0000130// Wrapper for ptrace when logging is not required.
131// Sets errno to 0 prior to calling ptrace.
132extern long
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000133PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000134{
135 long result = 0;
136 errno = 0;
Ashok Thirumurthi0f3b9b82013-05-01 20:38:19 +0000137 result = ptrace(req, pid, (caddr_t)addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000138 return result;
139}
140
Johnny Chen9ed5b492012-01-05 21:48:15 +0000141#define PTRACE(req, pid, addr, data) \
142 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
143#else
Matt Kopec7de48462013-03-06 17:20:48 +0000144 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000145#endif
146
147//------------------------------------------------------------------------------
148// Static implementations of ProcessMonitor::ReadMemory and
149// ProcessMonitor::WriteMemory. This enables mutual recursion between these
150// functions without needed to go thru the thread funnel.
151
152static size_t
153DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
154 Error &error)
155{
156 struct ptrace_io_desc pi_desc;
157
158 pi_desc.piod_op = PIOD_READ_D;
159 pi_desc.piod_offs = (void *)vm_addr;
160 pi_desc.piod_addr = buf;
161 pi_desc.piod_len = size;
162
163 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
164 error.SetErrorToErrno();
165 return pi_desc.piod_len;
166}
167
168static size_t
169DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
170 size_t size, Error &error)
171{
172 struct ptrace_io_desc pi_desc;
173
174 pi_desc.piod_op = PIOD_WRITE_D;
175 pi_desc.piod_offs = (void *)vm_addr;
176 pi_desc.piod_addr = (void *)buf;
177 pi_desc.piod_len = size;
178
179 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
180 error.SetErrorToErrno();
181 return pi_desc.piod_len;
182}
183
184// Simple helper function to ensure flags are enabled on the given file
185// descriptor.
186static bool
187EnsureFDFlags(int fd, int flags, Error &error)
188{
189 int status;
190
191 if ((status = fcntl(fd, F_GETFL)) == -1)
192 {
193 error.SetErrorToErrno();
194 return false;
195 }
196
197 if (fcntl(fd, F_SETFL, status | flags) == -1)
198 {
199 error.SetErrorToErrno();
200 return false;
201 }
202
203 return true;
204}
205
206//------------------------------------------------------------------------------
207/// @class Operation
208/// @brief Represents a ProcessMonitor operation.
209///
210/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
211/// one that spawned or attached to the process from the start. Therefore, when
212/// a ProcessMonitor is asked to deliver or change the state of an inferior
213/// process the operation must be "funneled" to a specific thread to perform the
214/// task. The Operation class provides an abstract base for all services the
215/// ProcessMonitor must perform via the single virtual function Execute, thus
216/// encapsulating the code that needs to run in the privileged context.
217class Operation
218{
219public:
Ed Maste5a9a6262013-06-24 14:55:03 +0000220 virtual ~Operation() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221 virtual void Execute(ProcessMonitor *monitor) = 0;
222};
223
224//------------------------------------------------------------------------------
225/// @class ReadOperation
226/// @brief Implements ProcessMonitor::ReadMemory.
227class ReadOperation : public Operation
228{
229public:
230 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
231 Error &error, size_t &result)
232 : m_addr(addr), m_buff(buff), m_size(size),
233 m_error(error), m_result(result)
234 { }
235
236 void Execute(ProcessMonitor *monitor);
237
238private:
239 lldb::addr_t m_addr;
240 void *m_buff;
241 size_t m_size;
242 Error &m_error;
243 size_t &m_result;
244};
245
246void
247ReadOperation::Execute(ProcessMonitor *monitor)
248{
249 lldb::pid_t pid = monitor->GetPID();
250
251 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
252}
253
254//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000255/// @class WriteOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256/// @brief Implements ProcessMonitor::WriteMemory.
257class WriteOperation : public Operation
258{
259public:
260 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
261 Error &error, size_t &result)
262 : m_addr(addr), m_buff(buff), m_size(size),
263 m_error(error), m_result(result)
264 { }
265
266 void Execute(ProcessMonitor *monitor);
267
268private:
269 lldb::addr_t m_addr;
270 const void *m_buff;
271 size_t m_size;
272 Error &m_error;
273 size_t &m_result;
274};
275
276void
277WriteOperation::Execute(ProcessMonitor *monitor)
278{
279 lldb::pid_t pid = monitor->GetPID();
280
281 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
282}
283
284//------------------------------------------------------------------------------
285/// @class ReadRegOperation
286/// @brief Implements ProcessMonitor::ReadRegisterValue.
287class ReadRegOperation : public Operation
288{
289public:
Ed Maste6f066412013-07-04 21:47:32 +0000290 ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
291 RegisterValue &value, bool &result)
292 : m_tid(tid), m_offset(offset), m_size(size),
293 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000294 { }
295
296 void Execute(ProcessMonitor *monitor);
297
298private:
Ed Maste6f066412013-07-04 21:47:32 +0000299 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000300 unsigned m_offset;
301 unsigned m_size;
302 RegisterValue &m_value;
303 bool &m_result;
304};
305
306void
307ReadRegOperation::Execute(ProcessMonitor *monitor)
308{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000309 struct reg regs;
310 int rc;
311
Ed Maste6f066412013-07-04 21:47:32 +0000312 if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000313 m_result = false;
314 } else {
Justin Hibbits89e6f382014-11-12 15:14:08 +0000315 // 'struct reg' contains only 32- or 64-bit register values. Punt on
316 // others. Also, not all entries may be uintptr_t sized, such as 32-bit
317 // processes on powerpc64 (probably the same for i386 on amd64)
318 if (m_size == sizeof(uint32_t))
319 m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
320 else if (m_size == sizeof(uint64_t))
321 m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
322 else
Johnny Chen9ed5b492012-01-05 21:48:15 +0000323 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
324 m_result = true;
325 }
326}
327
328//------------------------------------------------------------------------------
329/// @class WriteRegOperation
330/// @brief Implements ProcessMonitor::WriteRegisterValue.
331class WriteRegOperation : public Operation
332{
333public:
Ed Maste6f066412013-07-04 21:47:32 +0000334 WriteRegOperation(lldb::tid_t tid, unsigned offset,
335 const RegisterValue &value, bool &result)
336 : m_tid(tid), m_offset(offset),
337 m_value(value), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000338 { }
339
340 void Execute(ProcessMonitor *monitor);
341
342private:
Ed Maste6f066412013-07-04 21:47:32 +0000343 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000344 unsigned m_offset;
345 const RegisterValue &m_value;
346 bool &m_result;
347};
348
349void
350WriteRegOperation::Execute(ProcessMonitor *monitor)
351{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000352 struct reg regs;
353
Ed Maste6f066412013-07-04 21:47:32 +0000354 if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000355 m_result = false;
356 return;
357 }
358 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
Ed Maste6f066412013-07-04 21:47:32 +0000359 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000360 m_result = false;
361 else
362 m_result = true;
363}
364
365//------------------------------------------------------------------------------
Ed Mastea4be2c52014-02-19 18:34:06 +0000366/// @class ReadDebugRegOperation
367/// @brief Implements ProcessMonitor::ReadDebugRegisterValue.
368class ReadDebugRegOperation : public Operation
369{
370public:
371 ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
372 RegisterValue &value, bool &result)
373 : m_tid(tid), m_offset(offset), m_size(size),
374 m_value(value), m_result(result)
375 { }
376
377 void Execute(ProcessMonitor *monitor);
378
379private:
380 lldb::tid_t m_tid;
381 unsigned m_offset;
382 unsigned m_size;
383 RegisterValue &m_value;
384 bool &m_result;
385};
386
387void
388ReadDebugRegOperation::Execute(ProcessMonitor *monitor)
389{
390 struct dbreg regs;
391 int rc;
392
393 if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
394 m_result = false;
395 } else {
396 if (m_size == sizeof(uintptr_t))
397 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
398 else
399 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
400 m_result = true;
401 }
402}
403
404//------------------------------------------------------------------------------
405/// @class WriteDebugRegOperation
406/// @brief Implements ProcessMonitor::WriteDebugRegisterValue.
407class WriteDebugRegOperation : public Operation
408{
409public:
410 WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
411 const RegisterValue &value, bool &result)
412 : m_tid(tid), m_offset(offset),
413 m_value(value), m_result(result)
414 { }
415
416 void Execute(ProcessMonitor *monitor);
417
418private:
419 lldb::tid_t m_tid;
420 unsigned m_offset;
421 const RegisterValue &m_value;
422 bool &m_result;
423};
424
425void
426WriteDebugRegOperation::Execute(ProcessMonitor *monitor)
427{
428 struct dbreg regs;
429
430 if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
431 m_result = false;
432 return;
433 }
434 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
435 if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
436 m_result = false;
437 else
438 m_result = true;
439}
440
441//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000442/// @class ReadGPROperation
443/// @brief Implements ProcessMonitor::ReadGPR.
444class ReadGPROperation : public Operation
445{
446public:
Ed Maste6f066412013-07-04 21:47:32 +0000447 ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
448 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000449 { }
450
451 void Execute(ProcessMonitor *monitor);
452
453private:
Ed Maste6f066412013-07-04 21:47:32 +0000454 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000455 void *m_buf;
456 bool &m_result;
457};
458
459void
460ReadGPROperation::Execute(ProcessMonitor *monitor)
461{
462 int rc;
463
464 errno = 0;
Ed Maste6f066412013-07-04 21:47:32 +0000465 rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000466 if (errno != 0)
467 m_result = false;
468 else
469 m_result = true;
470}
471
472//------------------------------------------------------------------------------
473/// @class ReadFPROperation
474/// @brief Implements ProcessMonitor::ReadFPR.
475class ReadFPROperation : public Operation
476{
477public:
Ed Maste6f066412013-07-04 21:47:32 +0000478 ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
479 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000480 { }
481
482 void Execute(ProcessMonitor *monitor);
483
484private:
Ed Maste6f066412013-07-04 21:47:32 +0000485 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486 void *m_buf;
487 bool &m_result;
488};
489
490void
491ReadFPROperation::Execute(ProcessMonitor *monitor)
492{
Ed Maste6f066412013-07-04 21:47:32 +0000493 if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000494 m_result = false;
495 else
496 m_result = true;
497}
498
499//------------------------------------------------------------------------------
500/// @class WriteGPROperation
501/// @brief Implements ProcessMonitor::WriteGPR.
502class WriteGPROperation : public Operation
503{
504public:
Ed Maste6f066412013-07-04 21:47:32 +0000505 WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
506 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000507 { }
508
509 void Execute(ProcessMonitor *monitor);
510
511private:
Ed Maste6f066412013-07-04 21:47:32 +0000512 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000513 void *m_buf;
514 bool &m_result;
515};
516
517void
518WriteGPROperation::Execute(ProcessMonitor *monitor)
519{
Ed Maste6f066412013-07-04 21:47:32 +0000520 if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000521 m_result = false;
522 else
523 m_result = true;
524}
525
526//------------------------------------------------------------------------------
527/// @class WriteFPROperation
528/// @brief Implements ProcessMonitor::WriteFPR.
529class WriteFPROperation : public Operation
530{
531public:
Ed Maste6f066412013-07-04 21:47:32 +0000532 WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
533 : m_tid(tid), m_buf(buf), m_result(result)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000534 { }
535
536 void Execute(ProcessMonitor *monitor);
537
538private:
Ed Maste6f066412013-07-04 21:47:32 +0000539 lldb::tid_t m_tid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540 void *m_buf;
541 bool &m_result;
542};
543
544void
545WriteFPROperation::Execute(ProcessMonitor *monitor)
546{
Ed Maste6f066412013-07-04 21:47:32 +0000547 if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000548 m_result = false;
549 else
550 m_result = true;
551}
552
553//------------------------------------------------------------------------------
554/// @class ResumeOperation
555/// @brief Implements ProcessMonitor::Resume.
556class ResumeOperation : public Operation
557{
558public:
Ed Maste502f9022013-11-25 16:31:23 +0000559 ResumeOperation(uint32_t signo, bool &result) :
560 m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000561
562 void Execute(ProcessMonitor *monitor);
563
564private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000565 uint32_t m_signo;
566 bool &m_result;
567};
568
569void
570ResumeOperation::Execute(ProcessMonitor *monitor)
571{
Ed Maste502f9022013-11-25 16:31:23 +0000572 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000573 int data = 0;
574
575 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
576 data = m_signo;
577
Ed Maste502f9022013-11-25 16:31:23 +0000578 if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data))
Ed Mastea02f5532013-07-02 16:45:16 +0000579 {
580 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
581
582 if (log)
Ed Maste502f9022013-11-25 16:31:23 +0000583 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", pid, strerror(errno));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000584 m_result = false;
Ed Mastea02f5532013-07-02 16:45:16 +0000585 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000586 else
587 m_result = true;
588}
589
590//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000591/// @class SingleStepOperation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000592/// @brief Implements ProcessMonitor::SingleStep.
593class SingleStepOperation : public Operation
594{
595public:
Ed Maste502f9022013-11-25 16:31:23 +0000596 SingleStepOperation(uint32_t signo, bool &result)
597 : m_signo(signo), m_result(result) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000598
599 void Execute(ProcessMonitor *monitor);
600
601private:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000602 uint32_t m_signo;
603 bool &m_result;
604};
605
606void
607SingleStepOperation::Execute(ProcessMonitor *monitor)
608{
Ed Maste502f9022013-11-25 16:31:23 +0000609 lldb::pid_t pid = monitor->GetPID();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000610 int data = 0;
611
612 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
613 data = m_signo;
614
Ed Maste502f9022013-11-25 16:31:23 +0000615 if (PTRACE(PT_STEP, pid, NULL, data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000616 m_result = false;
617 else
618 m_result = true;
619}
620
621//------------------------------------------------------------------------------
Ed Maste819e3992013-07-17 14:02:20 +0000622/// @class LwpInfoOperation
623/// @brief Implements ProcessMonitor::GetLwpInfo.
624class LwpInfoOperation : public Operation
Johnny Chen9ed5b492012-01-05 21:48:15 +0000625{
626public:
Ed Maste819e3992013-07-17 14:02:20 +0000627 LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
Daniel Maleaa35970a2012-11-23 18:09:58 +0000628 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000629
630 void Execute(ProcessMonitor *monitor);
631
632private:
633 lldb::tid_t m_tid;
634 void *m_info;
635 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000636 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000637};
638
639void
Ed Maste819e3992013-07-17 14:02:20 +0000640LwpInfoOperation::Execute(ProcessMonitor *monitor)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000641{
642 struct ptrace_lwpinfo plwp;
643
Daniel Maleaa35970a2012-11-23 18:09:58 +0000644 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000645 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000646 m_err = errno;
647 } else {
Ed Maste819e3992013-07-17 14:02:20 +0000648 memcpy(m_info, &plwp, sizeof(plwp));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000649 m_result = true;
650 }
651}
652
653//------------------------------------------------------------------------------
Ed Maste7fd845c2013-12-09 15:51:17 +0000654/// @class ThreadSuspendOperation
655/// @brief Implements ProcessMonitor::ThreadSuspend.
656class ThreadSuspendOperation : public Operation
657{
658public:
659 ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
660 : m_tid(tid), m_suspend(suspend), m_result(result) { }
661
662 void Execute(ProcessMonitor *monitor);
663
664private:
665 lldb::tid_t m_tid;
666 bool m_suspend;
667 bool &m_result;
668} ;
669
670void
671ThreadSuspendOperation::Execute(ProcessMonitor *monitor)
672{
673 m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
674}
675
676
677
678//------------------------------------------------------------------------------
Johnny Chen9ed5b492012-01-05 21:48:15 +0000679/// @class EventMessageOperation
680/// @brief Implements ProcessMonitor::GetEventMessage.
681class EventMessageOperation : public Operation
682{
683public:
684 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
685 : m_tid(tid), m_message(message), m_result(result) { }
686
687 void Execute(ProcessMonitor *monitor);
688
689private:
690 lldb::tid_t m_tid;
691 unsigned long *m_message;
692 bool &m_result;
693};
694
695void
696EventMessageOperation::Execute(ProcessMonitor *monitor)
697{
698 struct ptrace_lwpinfo plwp;
699
700 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
701 m_result = false;
702 else {
703 if (plwp.pl_flags & PL_FLAG_FORKED) {
Ed Maste82a00052013-11-25 21:15:53 +0000704 *m_message = plwp.pl_child_pid;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000705 m_result = true;
706 } else
707 m_result = false;
708 }
709}
710
711//------------------------------------------------------------------------------
712/// @class KillOperation
Ed Maste70882932014-04-01 14:30:56 +0000713/// @brief Implements ProcessMonitor::Kill.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000714class KillOperation : public Operation
715{
716public:
717 KillOperation(bool &result) : m_result(result) { }
718
719 void Execute(ProcessMonitor *monitor);
720
721private:
722 bool &m_result;
723};
724
725void
726KillOperation::Execute(ProcessMonitor *monitor)
727{
728 lldb::pid_t pid = monitor->GetPID();
729
730 if (PTRACE(PT_KILL, pid, NULL, 0))
731 m_result = false;
732 else
733 m_result = true;
734}
735
736//------------------------------------------------------------------------------
Ed Mastea02f5532013-07-02 16:45:16 +0000737/// @class DetachOperation
Ed Maste263c9282014-03-17 17:45:53 +0000738/// @brief Implements ProcessMonitor::Detach.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000739class DetachOperation : public Operation
740{
741public:
742 DetachOperation(Error &result) : m_error(result) { }
743
744 void Execute(ProcessMonitor *monitor);
745
746private:
747 Error &m_error;
748};
749
750void
751DetachOperation::Execute(ProcessMonitor *monitor)
752{
753 lldb::pid_t pid = monitor->GetPID();
754
755 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
756 m_error.SetErrorToErrno();
757
758}
759
760ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
761 : m_monitor(monitor)
762{
763 sem_init(&m_semaphore, 0, 0);
764}
765
766ProcessMonitor::OperationArgs::~OperationArgs()
767{
768 sem_destroy(&m_semaphore);
769}
770
771ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
772 lldb_private::Module *module,
773 char const **argv,
774 char const **envp,
775 const char *stdin_path,
776 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000777 const char *stderr_path,
778 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000779 : OperationArgs(monitor),
780 m_module(module),
781 m_argv(argv),
782 m_envp(envp),
783 m_stdin_path(stdin_path),
784 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000785 m_stderr_path(stderr_path),
786 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000787
788ProcessMonitor::LaunchArgs::~LaunchArgs()
789{ }
790
791ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
792 lldb::pid_t pid)
793 : OperationArgs(monitor), m_pid(pid) { }
794
795ProcessMonitor::AttachArgs::~AttachArgs()
796{ }
797
798//------------------------------------------------------------------------------
799/// The basic design of the ProcessMonitor is built around two threads.
800///
801/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
802/// for changes in the debugee state. When a change is detected a
803/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
804/// "drives" state changes in the debugger.
805///
806/// The second thread (@see OperationThread) is responsible for two things 1)
807/// launching or attaching to the inferior process, and then 2) servicing
808/// operations such as register reads/writes, stepping, etc. See the comments
809/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000810ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000811 Module *module,
812 const char *argv[],
813 const char *envp[],
814 const char *stdin_path,
815 const char *stdout_path,
816 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000817 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000818 const lldb_private::ProcessLaunchInfo & /* launch_info */,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000819 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000820 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000821 m_pid(LLDB_INVALID_PROCESS_ID),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000823 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000824{
Ed Maste756e1ff2013-09-18 19:34:08 +0000825 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
826 stdin_path, stdout_path, stderr_path,
827 working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000828
829
Ed Maste756e1ff2013-09-18 19:34:08 +0000830 sem_init(&m_operation_pending, 0, 0);
831 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000832
833 StartLaunchOpThread(args.get(), error);
834 if (!error.Success())
835 return;
836
837WAIT_AGAIN:
838 // Wait for the operation thread to initialize.
839 if (sem_wait(&args->m_semaphore))
840 {
841 if (errno == EINTR)
842 goto WAIT_AGAIN;
843 else
844 {
845 error.SetErrorToErrno();
846 return;
847 }
848 }
849
850 // Check that the launch was a success.
851 if (!args->m_error.Success())
852 {
Ed Mastea02f5532013-07-02 16:45:16 +0000853 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000854 error = args->m_error;
855 return;
856 }
857
858 // Finally, start monitoring the child process for change in state.
859 m_monitor_thread = Host::StartMonitoringChildProcess(
860 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000861 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000862 {
863 error.SetErrorToGenericError();
864 error.SetErrorString("Process launch failed.");
865 return;
866 }
867}
868
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000869ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000870 lldb::pid_t pid,
871 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000872 : m_process(static_cast<ProcessFreeBSD *>(process)),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000873 m_pid(pid),
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874 m_terminal_fd(-1),
Ed Maste756e1ff2013-09-18 19:34:08 +0000875 m_operation(0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000876{
Ed Maste756e1ff2013-09-18 19:34:08 +0000877 sem_init(&m_operation_pending, 0, 0);
878 sem_init(&m_operation_done, 0, 0);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000879
Johnny Chen9ed5b492012-01-05 21:48:15 +0000880
Ed Maste756e1ff2013-09-18 19:34:08 +0000881 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000882
883 StartAttachOpThread(args.get(), error);
884 if (!error.Success())
885 return;
886
887WAIT_AGAIN:
888 // Wait for the operation thread to initialize.
889 if (sem_wait(&args->m_semaphore))
890 {
891 if (errno == EINTR)
892 goto WAIT_AGAIN;
893 else
894 {
895 error.SetErrorToErrno();
896 return;
897 }
898 }
899
Ed Mastea02f5532013-07-02 16:45:16 +0000900 // Check that the attach was a success.
Johnny Chen9ed5b492012-01-05 21:48:15 +0000901 if (!args->m_error.Success())
902 {
Ed Mastea02f5532013-07-02 16:45:16 +0000903 StopOpThread();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000904 error = args->m_error;
905 return;
906 }
907
908 // Finally, start monitoring the child process for change in state.
909 m_monitor_thread = Host::StartMonitoringChildProcess(
910 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000911 if (!m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000912 {
913 error.SetErrorToGenericError();
914 error.SetErrorString("Process attach failed.");
915 return;
916 }
917}
918
919ProcessMonitor::~ProcessMonitor()
920{
921 StopMonitor();
922}
923
924//------------------------------------------------------------------------------
925// Thread setup and tear down.
926void
927ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
928{
929 static const char *g_thread_name = "lldb.process.freebsd.operation";
930
Zachary Turner25cbf5a2014-09-30 16:56:56 +0000931 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000932 return;
933
Zachary Turner39de3112014-09-09 20:54:56 +0000934 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000935}
936
Johnny Chen9ed5b492012-01-05 21:48:15 +0000937void *
938ProcessMonitor::LaunchOpThread(void *arg)
939{
940 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
941
942 if (!Launch(args)) {
943 sem_post(&args->m_semaphore);
944 return NULL;
945 }
946
947 ServeOperation(args);
948 return NULL;
949}
950
951bool
952ProcessMonitor::Launch(LaunchArgs *args)
953{
954 ProcessMonitor *monitor = args->m_monitor;
955 ProcessFreeBSD &process = monitor->GetProcess();
956 const char **argv = args->m_argv;
957 const char **envp = args->m_envp;
958 const char *stdin_path = args->m_stdin_path;
959 const char *stdout_path = args->m_stdout_path;
960 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000961 const char *working_dir = args->m_working_dir;
Ed Mastea02f5532013-07-02 16:45:16 +0000962
963 lldb_utility::PseudoTerminal terminal;
964 const size_t err_len = 1024;
965 char err_str[err_len];
Johnny Chen9ed5b492012-01-05 21:48:15 +0000966 lldb::pid_t pid;
967
Johnny Chen9ed5b492012-01-05 21:48:15 +0000968 // Propagate the environment if one is not supplied.
969 if (envp == NULL || envp[0] == NULL)
970 envp = const_cast<const char **>(environ);
971
Ed Mastea02f5532013-07-02 16:45:16 +0000972 if ((pid = terminal.Fork(err_str, err_len)) == -1)
973 {
974 args->m_error.SetErrorToGenericError();
975 args->m_error.SetErrorString("Process fork failed.");
976 goto FINISH;
977 }
978
Johnny Chen9ed5b492012-01-05 21:48:15 +0000979 // Recognized child exit status codes.
980 enum {
981 ePtraceFailed = 1,
982 eDupStdinFailed,
983 eDupStdoutFailed,
984 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000985 eChdirFailed,
Ed Maste441a1be2014-02-04 19:37:15 +0000986 eExecFailed,
987 eSetGidFailed
Johnny Chen9ed5b492012-01-05 21:48:15 +0000988 };
989
Johnny Chen9ed5b492012-01-05 21:48:15 +0000990 // Child process.
991 if (pid == 0)
992 {
993 // Trace this process.
994 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
995 exit(ePtraceFailed);
996
997 // Do not inherit setgid powers.
Ed Maste441a1be2014-02-04 19:37:15 +0000998 if (setgid(getgid()) != 0)
999 exit(eSetGidFailed);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001000
1001 // Let us have our own process group.
1002 setpgid(0, 0);
1003
1004 // Dup file descriptors if needed.
1005 //
1006 // FIXME: If two or more of the paths are the same we needlessly open
1007 // the same file multiple times.
1008 if (stdin_path != NULL && stdin_path[0])
1009 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
1010 exit(eDupStdinFailed);
1011
1012 if (stdout_path != NULL && stdout_path[0])
1013 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
1014 exit(eDupStdoutFailed);
1015
1016 if (stderr_path != NULL && stderr_path[0])
1017 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
1018 exit(eDupStderrFailed);
1019
Daniel Malea6217d2a2013-01-08 14:49:22 +00001020 // Change working directory
1021 if (working_dir != NULL && working_dir[0])
1022 if (0 != ::chdir(working_dir))
1023 exit(eChdirFailed);
1024
Johnny Chen9ed5b492012-01-05 21:48:15 +00001025 // Execute. We should never return.
1026 execve(argv[0],
1027 const_cast<char *const *>(argv),
1028 const_cast<char *const *>(envp));
1029 exit(eExecFailed);
1030 }
1031
1032 // Wait for the child process to to trap on its call to execve.
1033 ::pid_t wpid;
1034 int status;
1035 if ((wpid = waitpid(pid, &status, 0)) < 0)
1036 {
1037 args->m_error.SetErrorToErrno();
1038 goto FINISH;
1039 }
1040 else if (WIFEXITED(status))
1041 {
1042 // open, dup or execve likely failed for some reason.
1043 args->m_error.SetErrorToGenericError();
1044 switch (WEXITSTATUS(status))
1045 {
Ed Maste5d34af32013-06-24 15:09:18 +00001046 case ePtraceFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001047 args->m_error.SetErrorString("Child ptrace failed.");
1048 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001049 case eDupStdinFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001050 args->m_error.SetErrorString("Child open stdin failed.");
1051 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001052 case eDupStdoutFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001053 args->m_error.SetErrorString("Child open stdout failed.");
1054 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001055 case eDupStderrFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001056 args->m_error.SetErrorString("Child open stderr failed.");
1057 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001058 case eChdirFailed:
1059 args->m_error.SetErrorString("Child failed to set working directory.");
1060 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001061 case eExecFailed:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001062 args->m_error.SetErrorString("Child exec failed.");
1063 break;
Ed Maste441a1be2014-02-04 19:37:15 +00001064 case eSetGidFailed:
1065 args->m_error.SetErrorString("Child setgid failed.");
1066 break;
Ed Maste5d34af32013-06-24 15:09:18 +00001067 default:
Johnny Chen9ed5b492012-01-05 21:48:15 +00001068 args->m_error.SetErrorString("Child returned unknown exit status.");
1069 break;
1070 }
1071 goto FINISH;
1072 }
Ed Maste82a00052013-11-25 21:15:53 +00001073 assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
Johnny Chen9ed5b492012-01-05 21:48:15 +00001074 "Could not sync with inferior process.");
1075
1076#ifdef notyet
1077 // Have the child raise an event on exit. This is used to keep the child in
1078 // limbo until it is destroyed.
1079 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
1080 {
1081 args->m_error.SetErrorToErrno();
1082 goto FINISH;
1083 }
1084#endif
Ed Mastea02f5532013-07-02 16:45:16 +00001085 // Release the master terminal descriptor and pass it off to the
1086 // ProcessMonitor instance. Similarly stash the inferior pid.
1087 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001088 monitor->m_pid = pid;
1089
1090 // Set the terminal fd to be in non blocking mode (it simplifies the
1091 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
1092 // descriptor to read from).
1093 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1094 goto FINISH;
1095
Ed Mastee5441432013-09-03 23:55:30 +00001096 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001097
1098FINISH:
1099 return args->m_error.Success();
1100}
1101
Johnny Chen9ed5b492012-01-05 21:48:15 +00001102void
1103ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1104{
1105 static const char *g_thread_name = "lldb.process.freebsd.operation";
1106
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001107 if (m_operation_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001108 return;
1109
Zachary Turner39de3112014-09-09 20:54:56 +00001110 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001111}
1112
Johnny Chen9ed5b492012-01-05 21:48:15 +00001113void *
1114ProcessMonitor::AttachOpThread(void *arg)
1115{
1116 AttachArgs *args = static_cast<AttachArgs*>(arg);
1117
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001118 Attach(args);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001119
1120 ServeOperation(args);
1121 return NULL;
1122}
1123
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001124void
Johnny Chen9ed5b492012-01-05 21:48:15 +00001125ProcessMonitor::Attach(AttachArgs *args)
1126{
1127 lldb::pid_t pid = args->m_pid;
1128
1129 ProcessMonitor *monitor = args->m_monitor;
1130 ProcessFreeBSD &process = monitor->GetProcess();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001131
1132 if (pid <= 1)
1133 {
1134 args->m_error.SetErrorToGenericError();
1135 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001136 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001137 }
1138
1139 // Attach to the requested process.
1140 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1141 {
1142 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001143 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001144 }
1145
1146 int status;
1147 if ((status = waitpid(pid, NULL, 0)) < 0)
1148 {
1149 args->m_error.SetErrorToErrno();
Oleksiy Vyalov5d064742014-11-19 18:27:45 +00001150 return;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001151 }
1152
Ed Mastee5441432013-09-03 23:55:30 +00001153 process.SendMessage(ProcessMessage::Attach(pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001154}
1155
Ed Maste7fd845c2013-12-09 15:51:17 +00001156size_t
1157ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t>&thread_ids)
1158{
1159 lwpid_t *tids;
1160 int tdcnt;
1161
1162 thread_ids.clear();
1163
1164 tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1165 if (tdcnt <= 0)
1166 return 0;
1167 tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1168 if (tids == NULL)
1169 return 0;
1170 if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1171 free(tids);
1172 return 0;
1173 }
1174 thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1175 free(tids);
1176 return thread_ids.size();
1177}
1178
Johnny Chen9ed5b492012-01-05 21:48:15 +00001179bool
1180ProcessMonitor::MonitorCallback(void *callback_baton,
1181 lldb::pid_t pid,
1182 bool exited,
1183 int signal,
1184 int status)
1185{
1186 ProcessMessage message;
1187 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001188 ProcessFreeBSD *process = monitor->m_process;
Ed Mastea02f5532013-07-02 16:45:16 +00001189 assert(process);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001190 bool stop_monitoring;
Ed Maste819e3992013-07-17 14:02:20 +00001191 struct ptrace_lwpinfo plwp;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001192 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001193
Ed Mastea02f5532013-07-02 16:45:16 +00001194 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1195
1196 if (exited)
1197 {
1198 if (log)
1199 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1200 message = ProcessMessage::Exit(pid, status);
1201 process->SendMessage(message);
1202 return pid == process->GetID();
1203 }
1204
Ed Maste819e3992013-07-17 14:02:20 +00001205 if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001206 stop_monitoring = true; // pid is gone. Bail.
1207 else {
Ed Maste819e3992013-07-17 14:02:20 +00001208 switch (plwp.pl_siginfo.si_signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001209 {
1210 case SIGTRAP:
Ed Maste7fd845c2013-12-09 15:51:17 +00001211 message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001212 break;
1213
1214 default:
Ed Maste7fd845c2013-12-09 15:51:17 +00001215 message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001216 break;
1217 }
1218
1219 process->SendMessage(message);
1220 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1221 }
1222
1223 return stop_monitoring;
1224}
1225
1226ProcessMessage
1227ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001228 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001229{
1230 ProcessMessage message;
1231
Ed Mastea02f5532013-07-02 16:45:16 +00001232 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1233
Matt Kopec7de48462013-03-06 17:20:48 +00001234 assert(monitor);
1235 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001236
1237 switch (info->si_code)
1238 {
1239 default:
1240 assert(false && "Unexpected SIGTRAP code!");
1241 break;
1242
1243 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1244 {
1245 // The inferior process is about to exit. Maintain the process in a
1246 // state of "limbo" until we are explicitly commanded to detach,
1247 // destroy, resume, etc.
1248 unsigned long data = 0;
Ed Maste7fd845c2013-12-09 15:51:17 +00001249 if (!monitor->GetEventMessage(tid, &data))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001250 data = -1;
Ed Mastea02f5532013-07-02 16:45:16 +00001251 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001252 log->Printf ("ProcessMonitor::%s() received exit? event, data = %lx, tid = %" PRIu64, __FUNCTION__, data, tid);
1253 message = ProcessMessage::Limbo(tid, (data >> 8));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001254 break;
1255 }
1256
1257 case 0:
1258 case TRAP_TRACE:
Ed Mastea02f5532013-07-02 16:45:16 +00001259 if (log)
Ed Mastea4be2c52014-02-19 18:34:06 +00001260 log->Printf ("ProcessMonitor::%s() received trace event, tid = %" PRIu64 " : si_code = %d", __FUNCTION__, tid, info->si_code);
Ed Maste7fd845c2013-12-09 15:51:17 +00001261 message = ProcessMessage::Trace(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001262 break;
1263
1264 case SI_KERNEL:
1265 case TRAP_BRKPT:
Ed Mastea02f5532013-07-02 16:45:16 +00001266 if (log)
Ed Maste7fd845c2013-12-09 15:51:17 +00001267 log->Printf ("ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64, __FUNCTION__, tid);
1268 message = ProcessMessage::Break(tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001269 break;
1270 }
1271
1272 return message;
1273}
1274
1275ProcessMessage
1276ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Ed Maste7fd845c2013-12-09 15:51:17 +00001277 const siginfo_t *info, lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001278{
1279 ProcessMessage message;
1280 int signo = info->si_signo;
1281
Ed Mastea02f5532013-07-02 16:45:16 +00001282 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1283
Johnny Chen9ed5b492012-01-05 21:48:15 +00001284 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1285 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1286 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1287 //
1288 // IOW, user generated signals never generate what we consider to be a
1289 // "crash".
1290 //
1291 // Similarly, ACK signals generated by this monitor.
1292 if (info->si_code == SI_USER)
1293 {
Ed Mastea02f5532013-07-02 16:45:16 +00001294 if (log)
1295 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1296 __FUNCTION__,
1297 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1298 "SI_USER",
1299 info->si_pid);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001300 if (info->si_pid == getpid())
Ed Maste7fd845c2013-12-09 15:51:17 +00001301 return ProcessMessage::SignalDelivered(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001302 else
Ed Maste7fd845c2013-12-09 15:51:17 +00001303 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001304 }
1305
Ed Mastea02f5532013-07-02 16:45:16 +00001306 if (log)
1307 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1308
Johnny Chen9ed5b492012-01-05 21:48:15 +00001309 if (signo == SIGSEGV) {
1310 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1311 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001312 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001313 }
1314
1315 if (signo == SIGILL) {
1316 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1317 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001318 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001319 }
1320
1321 if (signo == SIGFPE) {
1322 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1323 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001324 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001325 }
1326
1327 if (signo == SIGBUS) {
1328 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1329 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
Ed Maste7fd845c2013-12-09 15:51:17 +00001330 return ProcessMessage::Crash(tid, reason, signo, fault_addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001331 }
1332
1333 // Everything else is "normal" and does not require any special action on
1334 // our part.
Ed Maste7fd845c2013-12-09 15:51:17 +00001335 return ProcessMessage::Signal(tid, signo);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001336}
1337
1338ProcessMessage::CrashReason
1339ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1340{
1341 ProcessMessage::CrashReason reason;
1342 assert(info->si_signo == SIGSEGV);
1343
1344 reason = ProcessMessage::eInvalidCrashReason;
1345
1346 switch (info->si_code)
1347 {
1348 default:
1349 assert(false && "unexpected si_code for SIGSEGV");
1350 break;
1351 case SEGV_MAPERR:
1352 reason = ProcessMessage::eInvalidAddress;
1353 break;
1354 case SEGV_ACCERR:
1355 reason = ProcessMessage::ePrivilegedAddress;
1356 break;
1357 }
1358
1359 return reason;
1360}
1361
1362ProcessMessage::CrashReason
1363ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1364{
1365 ProcessMessage::CrashReason reason;
1366 assert(info->si_signo == SIGILL);
1367
1368 reason = ProcessMessage::eInvalidCrashReason;
1369
1370 switch (info->si_code)
1371 {
1372 default:
1373 assert(false && "unexpected si_code for SIGILL");
1374 break;
1375 case ILL_ILLOPC:
1376 reason = ProcessMessage::eIllegalOpcode;
1377 break;
1378 case ILL_ILLOPN:
1379 reason = ProcessMessage::eIllegalOperand;
1380 break;
1381 case ILL_ILLADR:
1382 reason = ProcessMessage::eIllegalAddressingMode;
1383 break;
1384 case ILL_ILLTRP:
1385 reason = ProcessMessage::eIllegalTrap;
1386 break;
1387 case ILL_PRVOPC:
1388 reason = ProcessMessage::ePrivilegedOpcode;
1389 break;
1390 case ILL_PRVREG:
1391 reason = ProcessMessage::ePrivilegedRegister;
1392 break;
1393 case ILL_COPROC:
1394 reason = ProcessMessage::eCoprocessorError;
1395 break;
1396 case ILL_BADSTK:
1397 reason = ProcessMessage::eInternalStackError;
1398 break;
1399 }
1400
1401 return reason;
1402}
1403
1404ProcessMessage::CrashReason
1405ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1406{
1407 ProcessMessage::CrashReason reason;
1408 assert(info->si_signo == SIGFPE);
1409
1410 reason = ProcessMessage::eInvalidCrashReason;
1411
1412 switch (info->si_code)
1413 {
1414 default:
1415 assert(false && "unexpected si_code for SIGFPE");
1416 break;
1417 case FPE_INTDIV:
1418 reason = ProcessMessage::eIntegerDivideByZero;
1419 break;
1420 case FPE_INTOVF:
1421 reason = ProcessMessage::eIntegerOverflow;
1422 break;
1423 case FPE_FLTDIV:
1424 reason = ProcessMessage::eFloatDivideByZero;
1425 break;
1426 case FPE_FLTOVF:
1427 reason = ProcessMessage::eFloatOverflow;
1428 break;
1429 case FPE_FLTUND:
1430 reason = ProcessMessage::eFloatUnderflow;
1431 break;
1432 case FPE_FLTRES:
1433 reason = ProcessMessage::eFloatInexactResult;
1434 break;
1435 case FPE_FLTINV:
1436 reason = ProcessMessage::eFloatInvalidOperation;
1437 break;
1438 case FPE_FLTSUB:
1439 reason = ProcessMessage::eFloatSubscriptRange;
1440 break;
1441 }
1442
1443 return reason;
1444}
1445
1446ProcessMessage::CrashReason
1447ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1448{
1449 ProcessMessage::CrashReason reason;
1450 assert(info->si_signo == SIGBUS);
1451
1452 reason = ProcessMessage::eInvalidCrashReason;
1453
1454 switch (info->si_code)
1455 {
1456 default:
1457 assert(false && "unexpected si_code for SIGBUS");
1458 break;
1459 case BUS_ADRALN:
1460 reason = ProcessMessage::eIllegalAlignment;
1461 break;
1462 case BUS_ADRERR:
1463 reason = ProcessMessage::eIllegalAddress;
1464 break;
1465 case BUS_OBJERR:
1466 reason = ProcessMessage::eHardwareError;
1467 break;
1468 }
1469
1470 return reason;
1471}
1472
1473void
1474ProcessMonitor::ServeOperation(OperationArgs *args)
1475{
Johnny Chen9ed5b492012-01-05 21:48:15 +00001476 ProcessMonitor *monitor = args->m_monitor;
1477
Johnny Chen9ed5b492012-01-05 21:48:15 +00001478 // We are finised with the arguments and are ready to go. Sync with the
1479 // parent thread and start serving operations on the inferior.
1480 sem_post(&args->m_semaphore);
1481
1482 for (;;)
1483 {
Ed Maste756e1ff2013-09-18 19:34:08 +00001484 // wait for next pending operation
1485 sem_wait(&monitor->m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001486
Ed Maste756e1ff2013-09-18 19:34:08 +00001487 monitor->m_operation->Execute(monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001488
Ed Maste756e1ff2013-09-18 19:34:08 +00001489 // notify calling thread that operation is complete
1490 sem_post(&monitor->m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001491 }
1492}
1493
1494void
1495ProcessMonitor::DoOperation(Operation *op)
1496{
Ed Maste756e1ff2013-09-18 19:34:08 +00001497 Mutex::Locker lock(m_operation_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001498
Ed Maste756e1ff2013-09-18 19:34:08 +00001499 m_operation = op;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001500
Ed Maste756e1ff2013-09-18 19:34:08 +00001501 // notify operation thread that an operation is ready to be processed
1502 sem_post(&m_operation_pending);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001503
Ed Maste756e1ff2013-09-18 19:34:08 +00001504 // wait for operation to complete
1505 sem_wait(&m_operation_done);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001506}
1507
1508size_t
1509ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1510 Error &error)
1511{
1512 size_t result;
1513 ReadOperation op(vm_addr, buf, size, error, result);
1514 DoOperation(&op);
1515 return result;
1516}
1517
1518size_t
1519ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1520 lldb_private::Error &error)
1521{
1522 size_t result;
1523 WriteOperation op(vm_addr, buf, size, error, result);
1524 DoOperation(&op);
1525 return result;
1526}
1527
1528bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001529ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +00001530 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001531{
1532 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001533 ReadRegOperation op(tid, offset, size, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001534 DoOperation(&op);
1535 return result;
1536}
1537
1538bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001539ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00001540 const char* reg_name, const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001541{
1542 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001543 WriteRegOperation op(tid, offset, value, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001544 DoOperation(&op);
1545 return result;
1546}
1547
1548bool
Ed Mastea4be2c52014-02-19 18:34:06 +00001549ProcessMonitor::ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1550 const char *reg_name, unsigned size,
1551 lldb_private::RegisterValue &value)
1552{
1553 bool result;
1554 ReadDebugRegOperation op(tid, offset, size, value, result);
1555 DoOperation(&op);
1556 return result;
1557}
1558
1559bool
1560ProcessMonitor::WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
1561 const char *reg_name,
1562 const lldb_private::RegisterValue &value)
1563{
1564 bool result;
1565 WriteDebugRegOperation op(tid, offset, value, result);
1566 DoOperation(&op);
1567 return result;
1568}
1569
1570bool
Matt Kopec7de48462013-03-06 17:20:48 +00001571ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001572{
1573 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001574 ReadGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001575 DoOperation(&op);
1576 return result;
1577}
1578
1579bool
Matt Kopec7de48462013-03-06 17:20:48 +00001580ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001581{
1582 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001583 ReadFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001584 DoOperation(&op);
1585 return result;
1586}
1587
1588bool
Ed Maste5d34af32013-06-24 15:09:18 +00001589ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1590{
1591 return false;
1592}
1593
1594bool
Matt Kopec7de48462013-03-06 17:20:48 +00001595ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001596{
1597 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001598 WriteGPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001599 DoOperation(&op);
1600 return result;
1601}
1602
1603bool
Matt Kopec7de48462013-03-06 17:20:48 +00001604ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001605{
1606 bool result;
Ed Maste6f066412013-07-04 21:47:32 +00001607 WriteFPROperation op(tid, buf, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001608 DoOperation(&op);
1609 return result;
1610}
1611
1612bool
Ed Maste5d34af32013-06-24 15:09:18 +00001613ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
1614{
1615 return false;
1616}
1617
1618bool
Ed Maste68f51792013-10-18 19:16:44 +00001619ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
1620{
1621 return false;
1622}
1623
1624bool
Ed Maste502f9022013-11-25 16:31:23 +00001625ProcessMonitor::Resume(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001626{
1627 bool result;
Ed Mastea02f5532013-07-02 16:45:16 +00001628 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1629
Ed Maste4aeb3c02014-05-28 14:11:20 +00001630 if (log) {
1631 const char *signame = m_process->GetUnixSignals().GetSignalAsCString (signo);
1632 if (signame == nullptr)
1633 signame = "<none>";
1634 log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1635 __FUNCTION__, GetPID(), signame);
1636 }
Ed Maste502f9022013-11-25 16:31:23 +00001637 ResumeOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001638 DoOperation(&op);
Ed Mastea02f5532013-07-02 16:45:16 +00001639 if (log)
1640 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001641 return result;
1642}
1643
1644bool
Ed Maste502f9022013-11-25 16:31:23 +00001645ProcessMonitor::SingleStep(lldb::tid_t unused, uint32_t signo)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001646{
1647 bool result;
Ed Maste502f9022013-11-25 16:31:23 +00001648 SingleStepOperation op(signo, result);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001649 DoOperation(&op);
1650 return result;
1651}
1652
1653bool
Ed Maste70882932014-04-01 14:30:56 +00001654ProcessMonitor::Kill()
Johnny Chen9ed5b492012-01-05 21:48:15 +00001655{
1656 bool result;
1657 KillOperation op(result);
1658 DoOperation(&op);
1659 return result;
1660}
1661
1662bool
Ed Maste819e3992013-07-17 14:02:20 +00001663ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001664{
1665 bool result;
Ed Maste819e3992013-07-17 14:02:20 +00001666 LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001667 DoOperation(&op);
1668 return result;
1669}
1670
1671bool
Ed Maste7fd845c2013-12-09 15:51:17 +00001672ProcessMonitor::ThreadSuspend(lldb::tid_t tid, bool suspend)
1673{
1674 bool result;
1675 ThreadSuspendOperation op(tid, suspend, result);
1676 DoOperation(&op);
1677 return result;
1678}
1679
1680bool
Johnny Chen9ed5b492012-01-05 21:48:15 +00001681ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1682{
1683 bool result;
1684 EventMessageOperation op(tid, message, result);
1685 DoOperation(&op);
1686 return result;
1687}
1688
Ed Mastea02f5532013-07-02 16:45:16 +00001689lldb_private::Error
Matt Kopecedee1822013-06-03 19:48:53 +00001690ProcessMonitor::Detach(lldb::tid_t tid)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001691{
Ed Mastea02f5532013-07-02 16:45:16 +00001692 lldb_private::Error error;
1693 if (tid != LLDB_INVALID_THREAD_ID)
1694 {
1695 DetachOperation op(error);
1696 DoOperation(&op);
1697 }
1698 return error;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001699}
1700
1701bool
1702ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1703{
1704 int target_fd = open(path, flags, 0666);
1705
1706 if (target_fd == -1)
1707 return false;
1708
1709 return (dup2(target_fd, fd) == -1) ? false : true;
1710}
1711
1712void
1713ProcessMonitor::StopMonitoringChildProcess()
1714{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001715 if (m_monitor_thread.IsJoinable())
Johnny Chen9ed5b492012-01-05 21:48:15 +00001716 {
Zachary Turner39de3112014-09-09 20:54:56 +00001717 m_monitor_thread.Cancel();
1718 m_monitor_thread.Join(nullptr);
1719 m_monitor_thread.Reset();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001720 }
1721}
1722
1723void
1724ProcessMonitor::StopMonitor()
1725{
1726 StopMonitoringChildProcess();
Ed Mastea02f5532013-07-02 16:45:16 +00001727 StopOpThread();
Ed Maste756e1ff2013-09-18 19:34:08 +00001728 sem_destroy(&m_operation_pending);
1729 sem_destroy(&m_operation_done);
1730
Andrew Kaylor5e268992013-09-14 00:17:31 +00001731 // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to
1732 // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of
1733 // the descriptor to a ConnectionFileDescriptor object. Consequently
1734 // even though still has the file descriptor, we shouldn't close it here.
Johnny Chen9ed5b492012-01-05 21:48:15 +00001735}
1736
Andrew Kaylord4d54992013-09-17 00:30:24 +00001737// FIXME: On Linux, when a new thread is created, we receive to notifications,
1738// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1739// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1740// the new child thread indicating that it has is stopped because we attached.
1741// We have no guarantee of the order in which these arrive, but we need both
1742// before we are ready to proceed. We currently keep a list of threads which
1743// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1744// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1745// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1746//
1747// Right now, the above logic is in ProcessPOSIX, so we need a definition of
1748// this function in the FreeBSD ProcessMonitor implementation even if it isn't
1749// logically needed.
1750//
1751// We really should figure out what actually happens on FreeBSD and move the
1752// Linux-specific logic out of ProcessPOSIX as needed.
1753
1754bool
1755ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1756{
1757 return true;
1758}
1759
Johnny Chen9ed5b492012-01-05 21:48:15 +00001760void
Ed Mastea02f5532013-07-02 16:45:16 +00001761ProcessMonitor::StopOpThread()
1762{
Zachary Turner25cbf5a2014-09-30 16:56:56 +00001763 if (!m_operation_thread.IsJoinable())
Ed Mastea02f5532013-07-02 16:45:16 +00001764 return;
1765
Zachary Turner39de3112014-09-09 20:54:56 +00001766 m_operation_thread.Cancel();
1767 m_operation_thread.Join(nullptr);
1768 m_operation_thread.Reset();
Ed Mastea02f5532013-07-02 16:45:16 +00001769}