blob: b92e7728f417ddb7f972bf89b3faa196debc822d [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>
14#include <unistd.h>
15#include <signal.h>
16#include <sys/ptrace.h>
17#include <sys/socket.h>
18#include <sys/types.h>
19#include <sys/wait.h>
20
21// C++ Includes
22// Other libraries and framework includes
23#include "lldb/Core/Error.h"
24#include "lldb/Core/RegisterValue.h"
25#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
27#include "lldb/Target/Thread.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Utility/PseudoTerminal.h"
30
31
32#include "POSIXThread.h"
33#include "ProcessFreeBSD.h"
34#include "ProcessPOSIXLog.h"
35#include "ProcessMonitor.h"
36
37extern "C" {
38 extern char ** environ;
39 }
40
41using namespace lldb;
42using namespace lldb_private;
43
44// We disable the tracing of ptrace calls for integration builds to
45// avoid the additional indirection and checks.
46#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
47// Wrapper for ptrace to catch errors and log calls.
48
49const char *
50Get_PT_IO_OP(int op)
51{
52 switch (op) {
53 case PIOD_READ_D: return "READ_D";
54 case PIOD_WRITE_D: return "WRITE_D";
55 case PIOD_READ_I: return "READ_I";
56 case PIOD_WRITE_I: return "WRITE_I";
57 default: return "Unknown op";
58 }
59}
60
Matt Kopec7de48462013-03-06 17:20:48 +000061// Wrapper for ptrace to catch errors and log calls.
62// Note that ptrace sets errno on error because -1 is reserved as a valid result.
Johnny Chen9ed5b492012-01-05 21:48:15 +000063extern long
Matt Kopec58c0b962013-03-20 20:34:35 +000064PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
Johnny Chen9ed5b492012-01-05 21:48:15 +000065 const char* reqName, const char* file, int line)
66{
67 long int result;
68
Ashok Thirumurthi01186352013-03-28 16:02:31 +000069 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE));
Johnny Chen9ed5b492012-01-05 21:48:15 +000070
71 if (log) {
72 log->Printf("ptrace(%s, %u, %p, %x) called from file %s line %d",
73 reqName, pid, addr, data, file, line);
74 if (req == PT_IO) {
75 struct ptrace_io_desc *pi = (struct ptrace_io_desc *) addr;
76
77 log->Printf("PT_IO: op=%s offs=%zx size=%ld",
78 Get_PT_IO_OP(pi->piod_op), pi->piod_offs, pi->piod_len);
79 }
80 }
81
82 //PtraceDisplayBytes(req, data);
83
84 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +000085 result = ptrace(req, pid, (caddr_t) addr, data);
Johnny Chen9ed5b492012-01-05 21:48:15 +000086
87 //PtraceDisplayBytes(req, data);
88
Matt Kopec7de48462013-03-06 17:20:48 +000089 if (log && errno != 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +000090 {
91 const char* str;
92 switch (errno)
93 {
94 case ESRCH: str = "ESRCH"; break;
95 case EINVAL: str = "EINVAL"; break;
96 case EBUSY: str = "EBUSY"; break;
97 case EPERM: str = "EPERM"; break;
98 default: str = "<unknown>";
99 }
100 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
101 }
102
103 if (log) {
104 if (req == PT_GETREGS) {
105 struct reg *r = (struct reg *) addr;
106
107 log->Printf("PT_GETREGS: ip=0x%lx", r->r_rip);
108 log->Printf("PT_GETREGS: sp=0x%lx", r->r_rsp);
109 log->Printf("PT_GETREGS: bp=0x%lx", r->r_rbp);
110 log->Printf("PT_GETREGS: ax=0x%lx", r->r_rax);
111 }
112 }
113
114 return result;
115}
116
Matt Kopec7de48462013-03-06 17:20:48 +0000117// Wrapper for ptrace when logging is not required.
118// Sets errno to 0 prior to calling ptrace.
119extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000120PtraceWrapper(__ptrace_request req, lldb::pid_t pid, void *addr, int data)
Matt Kopec7de48462013-03-06 17:20:48 +0000121{
122 long result = 0;
123 errno = 0;
Matt Kopecc6672c82013-03-15 20:00:39 +0000124 result = ptrace(req, pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000125 return result;
126}
127
Johnny Chen9ed5b492012-01-05 21:48:15 +0000128#define PTRACE(req, pid, addr, data) \
129 PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
130#else
Matt Kopec7de48462013-03-06 17:20:48 +0000131 PtraceWrapper((req), (pid), (addr), (data))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000132#endif
133
134//------------------------------------------------------------------------------
135// Static implementations of ProcessMonitor::ReadMemory and
136// ProcessMonitor::WriteMemory. This enables mutual recursion between these
137// functions without needed to go thru the thread funnel.
138
139static size_t
140DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size,
141 Error &error)
142{
143 struct ptrace_io_desc pi_desc;
144
145 pi_desc.piod_op = PIOD_READ_D;
146 pi_desc.piod_offs = (void *)vm_addr;
147 pi_desc.piod_addr = buf;
148 pi_desc.piod_len = size;
149
150 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
151 error.SetErrorToErrno();
152 return pi_desc.piod_len;
153}
154
155static size_t
156DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf,
157 size_t size, Error &error)
158{
159 struct ptrace_io_desc pi_desc;
160
161 pi_desc.piod_op = PIOD_WRITE_D;
162 pi_desc.piod_offs = (void *)vm_addr;
163 pi_desc.piod_addr = (void *)buf;
164 pi_desc.piod_len = size;
165
166 if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0)
167 error.SetErrorToErrno();
168 return pi_desc.piod_len;
169}
170
171// Simple helper function to ensure flags are enabled on the given file
172// descriptor.
173static bool
174EnsureFDFlags(int fd, int flags, Error &error)
175{
176 int status;
177
178 if ((status = fcntl(fd, F_GETFL)) == -1)
179 {
180 error.SetErrorToErrno();
181 return false;
182 }
183
184 if (fcntl(fd, F_SETFL, status | flags) == -1)
185 {
186 error.SetErrorToErrno();
187 return false;
188 }
189
190 return true;
191}
192
193//------------------------------------------------------------------------------
194/// @class Operation
195/// @brief Represents a ProcessMonitor operation.
196///
197/// Under FreeBSD, it is not possible to ptrace() from any other thread but the
198/// one that spawned or attached to the process from the start. Therefore, when
199/// a ProcessMonitor is asked to deliver or change the state of an inferior
200/// process the operation must be "funneled" to a specific thread to perform the
201/// task. The Operation class provides an abstract base for all services the
202/// ProcessMonitor must perform via the single virtual function Execute, thus
203/// encapsulating the code that needs to run in the privileged context.
204class Operation
205{
206public:
207 virtual void Execute(ProcessMonitor *monitor) = 0;
208};
209
210//------------------------------------------------------------------------------
211/// @class ReadOperation
212/// @brief Implements ProcessMonitor::ReadMemory.
213class ReadOperation : public Operation
214{
215public:
216 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
217 Error &error, size_t &result)
218 : m_addr(addr), m_buff(buff), m_size(size),
219 m_error(error), m_result(result)
220 { }
221
222 void Execute(ProcessMonitor *monitor);
223
224private:
225 lldb::addr_t m_addr;
226 void *m_buff;
227 size_t m_size;
228 Error &m_error;
229 size_t &m_result;
230};
231
232void
233ReadOperation::Execute(ProcessMonitor *monitor)
234{
235 lldb::pid_t pid = monitor->GetPID();
236
237 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
238}
239
240//------------------------------------------------------------------------------
241/// @class ReadOperation
242/// @brief Implements ProcessMonitor::WriteMemory.
243class WriteOperation : public Operation
244{
245public:
246 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
247 Error &error, size_t &result)
248 : m_addr(addr), m_buff(buff), m_size(size),
249 m_error(error), m_result(result)
250 { }
251
252 void Execute(ProcessMonitor *monitor);
253
254private:
255 lldb::addr_t m_addr;
256 const void *m_buff;
257 size_t m_size;
258 Error &m_error;
259 size_t &m_result;
260};
261
262void
263WriteOperation::Execute(ProcessMonitor *monitor)
264{
265 lldb::pid_t pid = monitor->GetPID();
266
267 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
268}
269
270//------------------------------------------------------------------------------
271/// @class ReadRegOperation
272/// @brief Implements ProcessMonitor::ReadRegisterValue.
273class ReadRegOperation : public Operation
274{
275public:
276 ReadRegOperation(unsigned offset, unsigned size, RegisterValue &value, bool &result)
277 : m_offset(offset), m_size(size), m_value(value), m_result(result)
278 { }
279
280 void Execute(ProcessMonitor *monitor);
281
282private:
283 unsigned m_offset;
284 unsigned m_size;
285 RegisterValue &m_value;
286 bool &m_result;
287};
288
289void
290ReadRegOperation::Execute(ProcessMonitor *monitor)
291{
292 lldb::pid_t pid = monitor->GetPID();
293 struct reg regs;
294 int rc;
295
296 if ((rc = PTRACE(PT_GETREGS, pid, (caddr_t)&regs, 0)) < 0) {
297 m_result = false;
298 } else {
299 if (m_size == sizeof(uintptr_t))
300 m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
301 else
302 memcpy(&m_value, (((caddr_t)&regs) + m_offset), m_size);
303 m_result = true;
304 }
305}
306
307//------------------------------------------------------------------------------
308/// @class WriteRegOperation
309/// @brief Implements ProcessMonitor::WriteRegisterValue.
310class WriteRegOperation : public Operation
311{
312public:
313 WriteRegOperation(unsigned offset, const RegisterValue &value, bool &result)
314 : m_offset(offset), m_value(value), m_result(result)
315 { }
316
317 void Execute(ProcessMonitor *monitor);
318
319private:
320 unsigned m_offset;
321 const RegisterValue &m_value;
322 bool &m_result;
323};
324
325void
326WriteRegOperation::Execute(ProcessMonitor *monitor)
327{
328 lldb::pid_t pid = monitor->GetPID();
329 struct reg regs;
330
331 if (PTRACE(PT_GETREGS, pid, (caddr_t)&regs, 0) < 0) {
332 m_result = false;
333 return;
334 }
335 *(uintptr_t *)(((caddr_t)&regs) + m_offset) = (uintptr_t)m_value.GetAsUInt64();
336 if (PTRACE(PT_SETREGS, pid, (caddr_t)&regs, 0) < 0)
337 m_result = false;
338 else
339 m_result = true;
340}
341
342//------------------------------------------------------------------------------
343/// @class ReadGPROperation
344/// @brief Implements ProcessMonitor::ReadGPR.
345class ReadGPROperation : public Operation
346{
347public:
348 ReadGPROperation(void *buf, bool &result)
349 : m_buf(buf), m_result(result)
350 { }
351
352 void Execute(ProcessMonitor *monitor);
353
354private:
355 void *m_buf;
356 bool &m_result;
357};
358
359void
360ReadGPROperation::Execute(ProcessMonitor *monitor)
361{
362 int rc;
363
364 errno = 0;
365 rc = PTRACE(PT_GETREGS, monitor->GetPID(), (caddr_t)m_buf, 0);
366 if (errno != 0)
367 m_result = false;
368 else
369 m_result = true;
370}
371
372//------------------------------------------------------------------------------
373/// @class ReadFPROperation
374/// @brief Implements ProcessMonitor::ReadFPR.
375class ReadFPROperation : public Operation
376{
377public:
378 ReadFPROperation(void *buf, bool &result)
379 : m_buf(buf), m_result(result)
380 { }
381
382 void Execute(ProcessMonitor *monitor);
383
384private:
385 void *m_buf;
386 bool &m_result;
387};
388
389void
390ReadFPROperation::Execute(ProcessMonitor *monitor)
391{
392 if (PTRACE(PT_GETFPREGS, monitor->GetPID(), (caddr_t)m_buf, 0) < 0)
393 m_result = false;
394 else
395 m_result = true;
396}
397
398//------------------------------------------------------------------------------
399/// @class WriteGPROperation
400/// @brief Implements ProcessMonitor::WriteGPR.
401class WriteGPROperation : public Operation
402{
403public:
404 WriteGPROperation(void *buf, bool &result)
405 : m_buf(buf), m_result(result)
406 { }
407
408 void Execute(ProcessMonitor *monitor);
409
410private:
411 void *m_buf;
412 bool &m_result;
413};
414
415void
416WriteGPROperation::Execute(ProcessMonitor *monitor)
417{
418 if (PTRACE(PT_SETREGS, monitor->GetPID(), (caddr_t)m_buf, 0) < 0)
419 m_result = false;
420 else
421 m_result = true;
422}
423
424//------------------------------------------------------------------------------
425/// @class WriteFPROperation
426/// @brief Implements ProcessMonitor::WriteFPR.
427class WriteFPROperation : public Operation
428{
429public:
430 WriteFPROperation(void *buf, bool &result)
431 : m_buf(buf), m_result(result)
432 { }
433
434 void Execute(ProcessMonitor *monitor);
435
436private:
437 void *m_buf;
438 bool &m_result;
439};
440
441void
442WriteFPROperation::Execute(ProcessMonitor *monitor)
443{
444 if (PTRACE(PT_SETFPREGS, monitor->GetPID(), (caddr_t)m_buf, 0) < 0)
445 m_result = false;
446 else
447 m_result = true;
448}
449
450//------------------------------------------------------------------------------
451/// @class ResumeOperation
452/// @brief Implements ProcessMonitor::Resume.
453class ResumeOperation : public Operation
454{
455public:
456 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
457 m_tid(tid), m_signo(signo), m_result(result) { }
458
459 void Execute(ProcessMonitor *monitor);
460
461private:
462 lldb::tid_t m_tid;
463 uint32_t m_signo;
464 bool &m_result;
465};
466
467void
468ResumeOperation::Execute(ProcessMonitor *monitor)
469{
470 int data = 0;
471
472 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
473 data = m_signo;
474
475 if (PTRACE(PT_CONTINUE, m_tid, (caddr_t)1, data))
476 m_result = false;
477 else
478 m_result = true;
479}
480
481//------------------------------------------------------------------------------
482/// @class ResumeOperation
483/// @brief Implements ProcessMonitor::SingleStep.
484class SingleStepOperation : public Operation
485{
486public:
487 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
488 : m_tid(tid), m_signo(signo), m_result(result) { }
489
490 void Execute(ProcessMonitor *monitor);
491
492private:
493 lldb::tid_t m_tid;
494 uint32_t m_signo;
495 bool &m_result;
496};
497
498void
499SingleStepOperation::Execute(ProcessMonitor *monitor)
500{
501 int data = 0;
502
503 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
504 data = m_signo;
505
506 if (PTRACE(PT_STEP, m_tid, NULL, data))
507 m_result = false;
508 else
509 m_result = true;
510}
511
512//------------------------------------------------------------------------------
513/// @class SiginfoOperation
514/// @brief Implements ProcessMonitor::GetSignalInfo.
515class SiginfoOperation : public Operation
516{
517public:
Daniel Maleaa35970a2012-11-23 18:09:58 +0000518 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
519 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000520
521 void Execute(ProcessMonitor *monitor);
522
523private:
524 lldb::tid_t m_tid;
525 void *m_info;
526 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000527 int &m_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000528};
529
530void
531SiginfoOperation::Execute(ProcessMonitor *monitor)
532{
533 struct ptrace_lwpinfo plwp;
534
Daniel Maleaa35970a2012-11-23 18:09:58 +0000535 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000536 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +0000537 m_err = errno;
538 } else {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000539 memcpy(m_info, &plwp.pl_siginfo, sizeof(siginfo_t));
540 m_result = true;
541 }
542}
543
544//------------------------------------------------------------------------------
545/// @class EventMessageOperation
546/// @brief Implements ProcessMonitor::GetEventMessage.
547class EventMessageOperation : public Operation
548{
549public:
550 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
551 : m_tid(tid), m_message(message), m_result(result) { }
552
553 void Execute(ProcessMonitor *monitor);
554
555private:
556 lldb::tid_t m_tid;
557 unsigned long *m_message;
558 bool &m_result;
559};
560
561void
562EventMessageOperation::Execute(ProcessMonitor *monitor)
563{
564 struct ptrace_lwpinfo plwp;
565
566 if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
567 m_result = false;
568 else {
569 if (plwp.pl_flags & PL_FLAG_FORKED) {
570 m_message = (unsigned long *)plwp.pl_child_pid;
571 m_result = true;
572 } else
573 m_result = false;
574 }
575}
576
577//------------------------------------------------------------------------------
578/// @class KillOperation
579/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
580class KillOperation : public Operation
581{
582public:
583 KillOperation(bool &result) : m_result(result) { }
584
585 void Execute(ProcessMonitor *monitor);
586
587private:
588 bool &m_result;
589};
590
591void
592KillOperation::Execute(ProcessMonitor *monitor)
593{
594 lldb::pid_t pid = monitor->GetPID();
595
596 if (PTRACE(PT_KILL, pid, NULL, 0))
597 m_result = false;
598 else
599 m_result = true;
600}
601
602//------------------------------------------------------------------------------
603/// @class KillOperation
604/// @brief Implements ProcessMonitor::BringProcessIntoLimbo.
605class DetachOperation : public Operation
606{
607public:
608 DetachOperation(Error &result) : m_error(result) { }
609
610 void Execute(ProcessMonitor *monitor);
611
612private:
613 Error &m_error;
614};
615
616void
617DetachOperation::Execute(ProcessMonitor *monitor)
618{
619 lldb::pid_t pid = monitor->GetPID();
620
621 if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
622 m_error.SetErrorToErrno();
623
624}
625
626ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
627 : m_monitor(monitor)
628{
629 sem_init(&m_semaphore, 0, 0);
630}
631
632ProcessMonitor::OperationArgs::~OperationArgs()
633{
634 sem_destroy(&m_semaphore);
635}
636
637ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
638 lldb_private::Module *module,
639 char const **argv,
640 char const **envp,
641 const char *stdin_path,
642 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000643 const char *stderr_path,
644 const char *working_dir)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000645 : OperationArgs(monitor),
646 m_module(module),
647 m_argv(argv),
648 m_envp(envp),
649 m_stdin_path(stdin_path),
650 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +0000651 m_stderr_path(stderr_path),
652 m_working_dir(working_dir) { }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000653
654ProcessMonitor::LaunchArgs::~LaunchArgs()
655{ }
656
657ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
658 lldb::pid_t pid)
659 : OperationArgs(monitor), m_pid(pid) { }
660
661ProcessMonitor::AttachArgs::~AttachArgs()
662{ }
663
664//------------------------------------------------------------------------------
665/// The basic design of the ProcessMonitor is built around two threads.
666///
667/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
668/// for changes in the debugee state. When a change is detected a
669/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
670/// "drives" state changes in the debugger.
671///
672/// The second thread (@see OperationThread) is responsible for two things 1)
673/// launching or attaching to the inferior process, and then 2) servicing
674/// operations such as register reads/writes, stepping, etc. See the comments
675/// on the Operation class for more info as to why this is needed.
676ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
677 Module *module,
678 const char *argv[],
679 const char *envp[],
680 const char *stdin_path,
681 const char *stdout_path,
682 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000683 const char *working_dir,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000684 lldb_private::Error &error)
685 : m_process(static_cast<ProcessFreeBSD *>(process)),
686 m_operation_thread(LLDB_INVALID_HOST_THREAD),
687 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
688 m_pid(LLDB_INVALID_PROCESS_ID),
689 m_server_mutex(Mutex::eMutexTypeRecursive),
690 m_terminal_fd(-1),
691 m_client_fd(-1),
692 m_server_fd(-1)
693{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000694 std::unique_ptr<LaunchArgs> args;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000695
696 args.reset(new LaunchArgs(this, module, argv, envp,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000697 stdin_path, stdout_path, stderr_path, working_dir));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000698
699
700 // Server/client descriptors.
701 if (!EnableIPC())
702 {
703 error.SetErrorToGenericError();
704 error.SetErrorString("Monitor failed to initialize.");
705 }
706
707 StartLaunchOpThread(args.get(), error);
708 if (!error.Success())
709 return;
710
711WAIT_AGAIN:
712 // Wait for the operation thread to initialize.
713 if (sem_wait(&args->m_semaphore))
714 {
715 if (errno == EINTR)
716 goto WAIT_AGAIN;
717 else
718 {
719 error.SetErrorToErrno();
720 return;
721 }
722 }
723
724 // Check that the launch was a success.
725 if (!args->m_error.Success())
726 {
727 StopLaunchOpThread();
728 error = args->m_error;
729 return;
730 }
731
732 // Finally, start monitoring the child process for change in state.
733 m_monitor_thread = Host::StartMonitoringChildProcess(
734 ProcessMonitor::MonitorCallback, this, GetPID(), true);
735 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
736 {
737 error.SetErrorToGenericError();
738 error.SetErrorString("Process launch failed.");
739 return;
740 }
741}
742
743ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
744 lldb::pid_t pid,
745 lldb_private::Error &error)
746 : m_process(static_cast<ProcessFreeBSD *>(process)),
747 m_operation_thread(LLDB_INVALID_HOST_THREAD),
748 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
749 m_pid(pid),
750 m_server_mutex(Mutex::eMutexTypeRecursive),
751 m_terminal_fd(-1),
752 m_client_fd(-1),
753 m_server_fd(-1)
754{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000755 std::unique_ptr<AttachArgs> args;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000756
757 args.reset(new AttachArgs(this, pid));
758
759 // Server/client descriptors.
760 if (!EnableIPC())
761 {
762 error.SetErrorToGenericError();
763 error.SetErrorString("Monitor failed to initialize.");
764 }
765
766 StartAttachOpThread(args.get(), error);
767 if (!error.Success())
768 return;
769
770WAIT_AGAIN:
771 // Wait for the operation thread to initialize.
772 if (sem_wait(&args->m_semaphore))
773 {
774 if (errno == EINTR)
775 goto WAIT_AGAIN;
776 else
777 {
778 error.SetErrorToErrno();
779 return;
780 }
781 }
782
783 // Check that the launch was a success.
784 if (!args->m_error.Success())
785 {
786 StopAttachOpThread();
787 error = args->m_error;
788 return;
789 }
790
791 // Finally, start monitoring the child process for change in state.
792 m_monitor_thread = Host::StartMonitoringChildProcess(
793 ProcessMonitor::MonitorCallback, this, GetPID(), true);
794 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
795 {
796 error.SetErrorToGenericError();
797 error.SetErrorString("Process attach failed.");
798 return;
799 }
800}
801
802ProcessMonitor::~ProcessMonitor()
803{
804 StopMonitor();
805}
806
807//------------------------------------------------------------------------------
808// Thread setup and tear down.
809void
810ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
811{
812 static const char *g_thread_name = "lldb.process.freebsd.operation";
813
814 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
815 return;
816
817 m_operation_thread =
818 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
819}
820
821void
822ProcessMonitor::StopLaunchOpThread()
823{
824 lldb::thread_result_t result;
825
826 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
827 return;
828
829 Host::ThreadCancel(m_operation_thread, NULL);
830 Host::ThreadJoin(m_operation_thread, &result, NULL);
831}
832
833void *
834ProcessMonitor::LaunchOpThread(void *arg)
835{
836 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
837
838 if (!Launch(args)) {
839 sem_post(&args->m_semaphore);
840 return NULL;
841 }
842
843 ServeOperation(args);
844 return NULL;
845}
846
847bool
848ProcessMonitor::Launch(LaunchArgs *args)
849{
850 ProcessMonitor *monitor = args->m_monitor;
851 ProcessFreeBSD &process = monitor->GetProcess();
Greg Clayton29d19302012-02-27 18:40:48 +0000852 lldb::ProcessSP processSP = process.shared_from_this();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000853 const char **argv = args->m_argv;
854 const char **envp = args->m_envp;
855 const char *stdin_path = args->m_stdin_path;
856 const char *stdout_path = args->m_stdout_path;
857 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000858 const char *working_dir = args->m_working_dir;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000859 lldb::pid_t pid;
860
861 lldb::ThreadSP inferior;
862
863 // Propagate the environment if one is not supplied.
864 if (envp == NULL || envp[0] == NULL)
865 envp = const_cast<const char **>(environ);
866
867 // Recognized child exit status codes.
868 enum {
869 ePtraceFailed = 1,
870 eDupStdinFailed,
871 eDupStdoutFailed,
872 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000873 eChdirFailed,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874 eExecFailed
875 };
876
877 pid = fork();
878
879 // Child process.
880 if (pid == 0)
881 {
882 // Trace this process.
883 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
884 exit(ePtraceFailed);
885
886 // Do not inherit setgid powers.
887 setgid(getgid());
888
889 // Let us have our own process group.
890 setpgid(0, 0);
891
892 // Dup file descriptors if needed.
893 //
894 // FIXME: If two or more of the paths are the same we needlessly open
895 // the same file multiple times.
896 if (stdin_path != NULL && stdin_path[0])
897 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
898 exit(eDupStdinFailed);
899
900 if (stdout_path != NULL && stdout_path[0])
901 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
902 exit(eDupStdoutFailed);
903
904 if (stderr_path != NULL && stderr_path[0])
905 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
906 exit(eDupStderrFailed);
907
Daniel Malea6217d2a2013-01-08 14:49:22 +0000908 // Change working directory
909 if (working_dir != NULL && working_dir[0])
910 if (0 != ::chdir(working_dir))
911 exit(eChdirFailed);
912
Johnny Chen9ed5b492012-01-05 21:48:15 +0000913 // Execute. We should never return.
914 execve(argv[0],
915 const_cast<char *const *>(argv),
916 const_cast<char *const *>(envp));
917 exit(eExecFailed);
918 }
919
920 // Wait for the child process to to trap on its call to execve.
921 ::pid_t wpid;
922 int status;
923 if ((wpid = waitpid(pid, &status, 0)) < 0)
924 {
925 args->m_error.SetErrorToErrno();
926 goto FINISH;
927 }
928 else if (WIFEXITED(status))
929 {
930 // open, dup or execve likely failed for some reason.
931 args->m_error.SetErrorToGenericError();
932 switch (WEXITSTATUS(status))
933 {
934 case ePtraceFailed:
935 args->m_error.SetErrorString("Child ptrace failed.");
936 break;
937 case eDupStdinFailed:
938 args->m_error.SetErrorString("Child open stdin failed.");
939 break;
940 case eDupStdoutFailed:
941 args->m_error.SetErrorString("Child open stdout failed.");
942 break;
943 case eDupStderrFailed:
944 args->m_error.SetErrorString("Child open stderr failed.");
945 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000946 case eChdirFailed:
947 args->m_error.SetErrorString("Child failed to set working directory.");
948 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000949 case eExecFailed:
950 args->m_error.SetErrorString("Child exec failed.");
951 break;
952 default:
953 args->m_error.SetErrorString("Child returned unknown exit status.");
954 break;
955 }
956 goto FINISH;
957 }
958 assert(WIFSTOPPED(status) && wpid == pid &&
959 "Could not sync with inferior process.");
960
961#ifdef notyet
962 // Have the child raise an event on exit. This is used to keep the child in
963 // limbo until it is destroyed.
964 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
965 {
966 args->m_error.SetErrorToErrno();
967 goto FINISH;
968 }
969#endif
970 // XXX - Release the master terminal descriptor and pass it off to the
971 // XXX - ProcessMonitor instance. Similarly stash the inferior pid.
972 // For now just use stdin fd
Filipe Cabecinhasb76d5c92012-05-31 07:49:36 +0000973 monitor->m_terminal_fd = ::dup(STDIN_FILENO);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000974 monitor->m_pid = pid;
975
976 // Set the terminal fd to be in non blocking mode (it simplifies the
977 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
978 // descriptor to read from).
979 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
980 goto FINISH;
981
982 // Update the process thread list with this new thread.
Greg Clayton29d19302012-02-27 18:40:48 +0000983 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000984 process.GetThreadList().AddThread(inferior);
985
986 // Let our process instance know the thread has stopped.
987 process.SendMessage(ProcessMessage::Trace(pid));
988
989FINISH:
990 return args->m_error.Success();
991}
992
993bool
994ProcessMonitor::EnableIPC()
995{
996 int fd[2];
997
998 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
999 return false;
1000
1001 m_client_fd = fd[0];
1002 m_server_fd = fd[1];
1003 return true;
1004}
1005
1006void
1007ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1008{
1009 static const char *g_thread_name = "lldb.process.freebsd.operation";
1010
1011 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
1012 return;
1013
1014 m_operation_thread =
1015 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
1016}
1017
1018void
1019ProcessMonitor::StopAttachOpThread()
1020{
1021 assert(!"Not implemented yet!!!");
1022}
1023
1024void *
1025ProcessMonitor::AttachOpThread(void *arg)
1026{
1027 AttachArgs *args = static_cast<AttachArgs*>(arg);
1028
1029 if (!Attach(args))
1030 return NULL;
1031
1032 ServeOperation(args);
1033 return NULL;
1034}
1035
1036bool
1037ProcessMonitor::Attach(AttachArgs *args)
1038{
1039 lldb::pid_t pid = args->m_pid;
1040
1041 ProcessMonitor *monitor = args->m_monitor;
1042 ProcessFreeBSD &process = monitor->GetProcess();
Greg Clayton29d19302012-02-27 18:40:48 +00001043 lldb::ProcessSP processSP = process.shared_from_this();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001044 ThreadList &tl = process.GetThreadList();
1045 lldb::ThreadSP inferior;
1046
1047 if (pid <= 1)
1048 {
1049 args->m_error.SetErrorToGenericError();
1050 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1051 goto FINISH;
1052 }
1053
1054 // Attach to the requested process.
1055 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1056 {
1057 args->m_error.SetErrorToErrno();
1058 goto FINISH;
1059 }
1060
1061 int status;
1062 if ((status = waitpid(pid, NULL, 0)) < 0)
1063 {
1064 args->m_error.SetErrorToErrno();
1065 goto FINISH;
1066 }
1067
1068 // Update the process thread list with the attached thread.
Greg Clayton29d19302012-02-27 18:40:48 +00001069 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001070 tl.AddThread(inferior);
1071
1072 // Let our process instance know the thread has stopped.
1073 process.SendMessage(ProcessMessage::Trace(pid));
1074
1075 FINISH:
1076 return args->m_error.Success();
1077}
1078
1079bool
1080ProcessMonitor::MonitorCallback(void *callback_baton,
1081 lldb::pid_t pid,
1082 bool exited,
1083 int signal,
1084 int status)
1085{
1086 ProcessMessage message;
1087 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1088 ProcessFreeBSD *process = monitor->m_process;
1089 bool stop_monitoring;
1090 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001091 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001092
Daniel Maleaa35970a2012-11-23 18:09:58 +00001093 if (!monitor->GetSignalInfo(pid, &info, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001094 stop_monitoring = true; // pid is gone. Bail.
1095 else {
1096 switch (info.si_signo)
1097 {
1098 case SIGTRAP:
1099 message = MonitorSIGTRAP(monitor, &info, pid);
1100 break;
1101
1102 default:
1103 message = MonitorSignal(monitor, &info, pid);
1104 break;
1105 }
1106
1107 process->SendMessage(message);
1108 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1109 }
1110
1111 return stop_monitoring;
1112}
1113
1114ProcessMessage
1115ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1116 const siginfo_t *info, lldb::pid_t pid)
1117{
1118 ProcessMessage message;
1119
Matt Kopec7de48462013-03-06 17:20:48 +00001120 assert(monitor);
1121 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Johnny Chen9ed5b492012-01-05 21:48:15 +00001122
1123 switch (info->si_code)
1124 {
1125 default:
1126 assert(false && "Unexpected SIGTRAP code!");
1127 break;
1128
1129 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1130 {
1131 // The inferior process is about to exit. Maintain the process in a
1132 // state of "limbo" until we are explicitly commanded to detach,
1133 // destroy, resume, etc.
1134 unsigned long data = 0;
1135 if (!monitor->GetEventMessage(pid, &data))
1136 data = -1;
1137 message = ProcessMessage::Limbo(pid, (data >> 8));
1138 break;
1139 }
1140
1141 case 0:
1142 case TRAP_TRACE:
1143 message = ProcessMessage::Trace(pid);
1144 break;
1145
1146 case SI_KERNEL:
1147 case TRAP_BRKPT:
1148 message = ProcessMessage::Break(pid);
1149 break;
1150 }
1151
1152 return message;
1153}
1154
1155ProcessMessage
1156ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1157 const siginfo_t *info, lldb::pid_t pid)
1158{
1159 ProcessMessage message;
1160 int signo = info->si_signo;
1161
1162 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1163 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1164 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1165 //
1166 // IOW, user generated signals never generate what we consider to be a
1167 // "crash".
1168 //
1169 // Similarly, ACK signals generated by this monitor.
1170 if (info->si_code == SI_USER)
1171 {
1172 if (info->si_pid == getpid())
1173 return ProcessMessage::SignalDelivered(pid, signo);
1174 else
1175 return ProcessMessage::Signal(pid, signo);
1176 }
1177
1178 if (signo == SIGSEGV) {
1179 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1180 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1181 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1182 }
1183
1184 if (signo == SIGILL) {
1185 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1186 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1187 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1188 }
1189
1190 if (signo == SIGFPE) {
1191 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1192 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1193 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1194 }
1195
1196 if (signo == SIGBUS) {
1197 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1198 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1199 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1200 }
1201
1202 // Everything else is "normal" and does not require any special action on
1203 // our part.
1204 return ProcessMessage::Signal(pid, signo);
1205}
1206
1207ProcessMessage::CrashReason
1208ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1209{
1210 ProcessMessage::CrashReason reason;
1211 assert(info->si_signo == SIGSEGV);
1212
1213 reason = ProcessMessage::eInvalidCrashReason;
1214
1215 switch (info->si_code)
1216 {
1217 default:
1218 assert(false && "unexpected si_code for SIGSEGV");
1219 break;
1220 case SEGV_MAPERR:
1221 reason = ProcessMessage::eInvalidAddress;
1222 break;
1223 case SEGV_ACCERR:
1224 reason = ProcessMessage::ePrivilegedAddress;
1225 break;
1226 }
1227
1228 return reason;
1229}
1230
1231ProcessMessage::CrashReason
1232ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1233{
1234 ProcessMessage::CrashReason reason;
1235 assert(info->si_signo == SIGILL);
1236
1237 reason = ProcessMessage::eInvalidCrashReason;
1238
1239 switch (info->si_code)
1240 {
1241 default:
1242 assert(false && "unexpected si_code for SIGILL");
1243 break;
1244 case ILL_ILLOPC:
1245 reason = ProcessMessage::eIllegalOpcode;
1246 break;
1247 case ILL_ILLOPN:
1248 reason = ProcessMessage::eIllegalOperand;
1249 break;
1250 case ILL_ILLADR:
1251 reason = ProcessMessage::eIllegalAddressingMode;
1252 break;
1253 case ILL_ILLTRP:
1254 reason = ProcessMessage::eIllegalTrap;
1255 break;
1256 case ILL_PRVOPC:
1257 reason = ProcessMessage::ePrivilegedOpcode;
1258 break;
1259 case ILL_PRVREG:
1260 reason = ProcessMessage::ePrivilegedRegister;
1261 break;
1262 case ILL_COPROC:
1263 reason = ProcessMessage::eCoprocessorError;
1264 break;
1265 case ILL_BADSTK:
1266 reason = ProcessMessage::eInternalStackError;
1267 break;
1268 }
1269
1270 return reason;
1271}
1272
1273ProcessMessage::CrashReason
1274ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1275{
1276 ProcessMessage::CrashReason reason;
1277 assert(info->si_signo == SIGFPE);
1278
1279 reason = ProcessMessage::eInvalidCrashReason;
1280
1281 switch (info->si_code)
1282 {
1283 default:
1284 assert(false && "unexpected si_code for SIGFPE");
1285 break;
1286 case FPE_INTDIV:
1287 reason = ProcessMessage::eIntegerDivideByZero;
1288 break;
1289 case FPE_INTOVF:
1290 reason = ProcessMessage::eIntegerOverflow;
1291 break;
1292 case FPE_FLTDIV:
1293 reason = ProcessMessage::eFloatDivideByZero;
1294 break;
1295 case FPE_FLTOVF:
1296 reason = ProcessMessage::eFloatOverflow;
1297 break;
1298 case FPE_FLTUND:
1299 reason = ProcessMessage::eFloatUnderflow;
1300 break;
1301 case FPE_FLTRES:
1302 reason = ProcessMessage::eFloatInexactResult;
1303 break;
1304 case FPE_FLTINV:
1305 reason = ProcessMessage::eFloatInvalidOperation;
1306 break;
1307 case FPE_FLTSUB:
1308 reason = ProcessMessage::eFloatSubscriptRange;
1309 break;
1310 }
1311
1312 return reason;
1313}
1314
1315ProcessMessage::CrashReason
1316ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1317{
1318 ProcessMessage::CrashReason reason;
1319 assert(info->si_signo == SIGBUS);
1320
1321 reason = ProcessMessage::eInvalidCrashReason;
1322
1323 switch (info->si_code)
1324 {
1325 default:
1326 assert(false && "unexpected si_code for SIGBUS");
1327 break;
1328 case BUS_ADRALN:
1329 reason = ProcessMessage::eIllegalAlignment;
1330 break;
1331 case BUS_ADRERR:
1332 reason = ProcessMessage::eIllegalAddress;
1333 break;
1334 case BUS_OBJERR:
1335 reason = ProcessMessage::eHardwareError;
1336 break;
1337 }
1338
1339 return reason;
1340}
1341
1342void
1343ProcessMonitor::ServeOperation(OperationArgs *args)
1344{
1345 int status;
1346 pollfd fdset;
1347
1348 ProcessMonitor *monitor = args->m_monitor;
1349
1350 fdset.fd = monitor->m_server_fd;
1351 fdset.events = POLLIN | POLLPRI;
1352 fdset.revents = 0;
1353
1354 // We are finised with the arguments and are ready to go. Sync with the
1355 // parent thread and start serving operations on the inferior.
1356 sem_post(&args->m_semaphore);
1357
1358 for (;;)
1359 {
1360 if ((status = poll(&fdset, 1, -1)) < 0)
1361 {
1362 switch (errno)
1363 {
1364 default:
1365 assert(false && "Unexpected poll() failure!");
1366 continue;
1367
1368 case EINTR: continue; // Just poll again.
1369 case EBADF: return; // Connection terminated.
1370 }
1371 }
1372
1373 assert(status == 1 && "Too many descriptors!");
1374
1375 if (fdset.revents & POLLIN)
1376 {
1377 Operation *op = NULL;
1378
1379 READ_AGAIN:
1380 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1381 {
1382 // There is only one acceptable failure.
1383 assert(errno == EINTR);
1384 goto READ_AGAIN;
1385 }
1386
1387 assert(status == sizeof(op));
1388 op->Execute(monitor);
1389 write(fdset.fd, &op, sizeof(op));
1390 }
1391 }
1392}
1393
1394void
1395ProcessMonitor::DoOperation(Operation *op)
1396{
1397 int status;
1398 Operation *ack = NULL;
1399 Mutex::Locker lock(m_server_mutex);
1400
1401 // FIXME: Do proper error checking here.
1402 write(m_client_fd, &op, sizeof(op));
1403
1404READ_AGAIN:
1405 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1406 {
1407 // If interrupted by a signal handler try again. Otherwise the monitor
1408 // thread probably died and we have a stale file descriptor -- abort the
1409 // operation.
1410 if (errno == EINTR)
1411 goto READ_AGAIN;
1412 return;
1413 }
1414
1415 assert(status == sizeof(ack));
1416 assert(ack == op && "Invalid monitor thread response!");
1417}
1418
1419size_t
1420ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1421 Error &error)
1422{
1423 size_t result;
1424 ReadOperation op(vm_addr, buf, size, error, result);
1425 DoOperation(&op);
1426 return result;
1427}
1428
1429size_t
1430ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1431 lldb_private::Error &error)
1432{
1433 size_t result;
1434 WriteOperation op(vm_addr, buf, size, error, result);
1435 DoOperation(&op);
1436 return result;
1437}
1438
1439bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001440ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
1441 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001442{
1443 bool result;
1444 ReadRegOperation op(offset, size, value, result);
1445 DoOperation(&op);
1446 return result;
1447}
1448
1449bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001450ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
1451 const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001452{
1453 bool result;
1454 WriteRegOperation op(offset, value, result);
1455 DoOperation(&op);
1456 return result;
1457}
1458
1459bool
Matt Kopec7de48462013-03-06 17:20:48 +00001460ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001461{
1462 bool result;
1463 ReadGPROperation op(buf, result);
1464 DoOperation(&op);
1465 return result;
1466}
1467
1468bool
Matt Kopec7de48462013-03-06 17:20:48 +00001469ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001470{
1471 bool result;
1472 ReadFPROperation op(buf, result);
1473 DoOperation(&op);
1474 return result;
1475}
1476
1477bool
Matt Kopec7de48462013-03-06 17:20:48 +00001478ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001479{
1480 bool result;
1481 WriteGPROperation op(buf, result);
1482 DoOperation(&op);
1483 return result;
1484}
1485
1486bool
Matt Kopec7de48462013-03-06 17:20:48 +00001487ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001488{
1489 bool result;
1490 WriteFPROperation op(buf, result);
1491 DoOperation(&op);
1492 return result;
1493}
1494
1495bool
1496ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
1497{
1498 bool result;
1499 ResumeOperation op(tid, signo, result);
1500 DoOperation(&op);
1501 return result;
1502}
1503
1504bool
1505ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
1506{
1507 bool result;
1508 SingleStepOperation op(tid, signo, result);
1509 DoOperation(&op);
1510 return result;
1511}
1512
1513bool
1514ProcessMonitor::BringProcessIntoLimbo()
1515{
1516 bool result;
1517 KillOperation op(result);
1518 DoOperation(&op);
1519 return result;
1520}
1521
1522bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001523ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001524{
1525 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001526 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001527 DoOperation(&op);
1528 return result;
1529}
1530
1531bool
1532ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1533{
1534 bool result;
1535 EventMessageOperation op(tid, message, result);
1536 DoOperation(&op);
1537 return result;
1538}
1539
1540Error
1541ProcessMonitor::Detach()
1542{
1543 Error result;
1544 DetachOperation op(result);
1545 DoOperation(&op);
1546 StopMonitor();
1547 return result;
1548}
1549
1550bool
1551ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1552{
1553 int target_fd = open(path, flags, 0666);
1554
1555 if (target_fd == -1)
1556 return false;
1557
1558 return (dup2(target_fd, fd) == -1) ? false : true;
1559}
1560
1561void
1562ProcessMonitor::StopMonitoringChildProcess()
1563{
1564 lldb::thread_result_t thread_result;
1565
1566 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1567 {
1568 Host::ThreadCancel(m_monitor_thread, NULL);
1569 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1570 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1571 }
1572}
1573
1574void
1575ProcessMonitor::StopMonitor()
1576{
1577 StopMonitoringChildProcess();
1578 StopLaunchOpThread();
1579 CloseFD(m_terminal_fd);
1580 CloseFD(m_client_fd);
1581 CloseFD(m_server_fd);
1582}
1583
1584void
1585ProcessMonitor::CloseFD(int &fd)
1586{
1587 if (fd != -1)
1588 {
1589 close(fd);
1590 fd = -1;
1591 }
1592}