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