blob: e37a660925baff4badb4feb0344d16129df0b1f4 [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,
630 const char *stderr_path)
631 : OperationArgs(monitor),
632 m_module(module),
633 m_argv(argv),
634 m_envp(envp),
635 m_stdin_path(stdin_path),
636 m_stdout_path(stdout_path),
637 m_stderr_path(stderr_path) { }
638
639ProcessMonitor::LaunchArgs::~LaunchArgs()
640{ }
641
642ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
643 lldb::pid_t pid)
644 : OperationArgs(monitor), m_pid(pid) { }
645
646ProcessMonitor::AttachArgs::~AttachArgs()
647{ }
648
649//------------------------------------------------------------------------------
650/// The basic design of the ProcessMonitor is built around two threads.
651///
652/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
653/// for changes in the debugee state. When a change is detected a
654/// ProcessMessage is sent to the associated ProcessFreeBSD instance. This thread
655/// "drives" state changes in the debugger.
656///
657/// The second thread (@see OperationThread) is responsible for two things 1)
658/// launching or attaching to the inferior process, and then 2) servicing
659/// operations such as register reads/writes, stepping, etc. See the comments
660/// on the Operation class for more info as to why this is needed.
661ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
662 Module *module,
663 const char *argv[],
664 const char *envp[],
665 const char *stdin_path,
666 const char *stdout_path,
667 const char *stderr_path,
668 lldb_private::Error &error)
669 : m_process(static_cast<ProcessFreeBSD *>(process)),
670 m_operation_thread(LLDB_INVALID_HOST_THREAD),
671 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
672 m_pid(LLDB_INVALID_PROCESS_ID),
673 m_server_mutex(Mutex::eMutexTypeRecursive),
674 m_terminal_fd(-1),
675 m_client_fd(-1),
676 m_server_fd(-1)
677{
678 std::auto_ptr<LaunchArgs> args;
679
680 args.reset(new LaunchArgs(this, module, argv, envp,
681 stdin_path, stdout_path, stderr_path));
682
683
684 // Server/client descriptors.
685 if (!EnableIPC())
686 {
687 error.SetErrorToGenericError();
688 error.SetErrorString("Monitor failed to initialize.");
689 }
690
691 StartLaunchOpThread(args.get(), error);
692 if (!error.Success())
693 return;
694
695WAIT_AGAIN:
696 // Wait for the operation thread to initialize.
697 if (sem_wait(&args->m_semaphore))
698 {
699 if (errno == EINTR)
700 goto WAIT_AGAIN;
701 else
702 {
703 error.SetErrorToErrno();
704 return;
705 }
706 }
707
708 // Check that the launch was a success.
709 if (!args->m_error.Success())
710 {
711 StopLaunchOpThread();
712 error = args->m_error;
713 return;
714 }
715
716 // Finally, start monitoring the child process for change in state.
717 m_monitor_thread = Host::StartMonitoringChildProcess(
718 ProcessMonitor::MonitorCallback, this, GetPID(), true);
719 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
720 {
721 error.SetErrorToGenericError();
722 error.SetErrorString("Process launch failed.");
723 return;
724 }
725}
726
727ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
728 lldb::pid_t pid,
729 lldb_private::Error &error)
730 : m_process(static_cast<ProcessFreeBSD *>(process)),
731 m_operation_thread(LLDB_INVALID_HOST_THREAD),
732 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
733 m_pid(pid),
734 m_server_mutex(Mutex::eMutexTypeRecursive),
735 m_terminal_fd(-1),
736 m_client_fd(-1),
737 m_server_fd(-1)
738{
739 std::auto_ptr<AttachArgs> args;
740
741 args.reset(new AttachArgs(this, pid));
742
743 // Server/client descriptors.
744 if (!EnableIPC())
745 {
746 error.SetErrorToGenericError();
747 error.SetErrorString("Monitor failed to initialize.");
748 }
749
750 StartAttachOpThread(args.get(), error);
751 if (!error.Success())
752 return;
753
754WAIT_AGAIN:
755 // Wait for the operation thread to initialize.
756 if (sem_wait(&args->m_semaphore))
757 {
758 if (errno == EINTR)
759 goto WAIT_AGAIN;
760 else
761 {
762 error.SetErrorToErrno();
763 return;
764 }
765 }
766
767 // Check that the launch was a success.
768 if (!args->m_error.Success())
769 {
770 StopAttachOpThread();
771 error = args->m_error;
772 return;
773 }
774
775 // Finally, start monitoring the child process for change in state.
776 m_monitor_thread = Host::StartMonitoringChildProcess(
777 ProcessMonitor::MonitorCallback, this, GetPID(), true);
778 if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
779 {
780 error.SetErrorToGenericError();
781 error.SetErrorString("Process attach failed.");
782 return;
783 }
784}
785
786ProcessMonitor::~ProcessMonitor()
787{
788 StopMonitor();
789}
790
791//------------------------------------------------------------------------------
792// Thread setup and tear down.
793void
794ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
795{
796 static const char *g_thread_name = "lldb.process.freebsd.operation";
797
798 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
799 return;
800
801 m_operation_thread =
802 Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
803}
804
805void
806ProcessMonitor::StopLaunchOpThread()
807{
808 lldb::thread_result_t result;
809
810 if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
811 return;
812
813 Host::ThreadCancel(m_operation_thread, NULL);
814 Host::ThreadJoin(m_operation_thread, &result, NULL);
815}
816
817void *
818ProcessMonitor::LaunchOpThread(void *arg)
819{
820 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
821
822 if (!Launch(args)) {
823 sem_post(&args->m_semaphore);
824 return NULL;
825 }
826
827 ServeOperation(args);
828 return NULL;
829}
830
831bool
832ProcessMonitor::Launch(LaunchArgs *args)
833{
834 ProcessMonitor *monitor = args->m_monitor;
835 ProcessFreeBSD &process = monitor->GetProcess();
Greg Clayton29d19302012-02-27 18:40:48 +0000836 lldb::ProcessSP processSP = process.shared_from_this();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000837 const char **argv = args->m_argv;
838 const char **envp = args->m_envp;
839 const char *stdin_path = args->m_stdin_path;
840 const char *stdout_path = args->m_stdout_path;
841 const char *stderr_path = args->m_stderr_path;
842 lldb::pid_t pid;
843
844 lldb::ThreadSP inferior;
845
846 // Propagate the environment if one is not supplied.
847 if (envp == NULL || envp[0] == NULL)
848 envp = const_cast<const char **>(environ);
849
850 // Recognized child exit status codes.
851 enum {
852 ePtraceFailed = 1,
853 eDupStdinFailed,
854 eDupStdoutFailed,
855 eDupStderrFailed,
856 eExecFailed
857 };
858
859 pid = fork();
860
861 // Child process.
862 if (pid == 0)
863 {
864 // Trace this process.
865 if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
866 exit(ePtraceFailed);
867
868 // Do not inherit setgid powers.
869 setgid(getgid());
870
871 // Let us have our own process group.
872 setpgid(0, 0);
873
874 // Dup file descriptors if needed.
875 //
876 // FIXME: If two or more of the paths are the same we needlessly open
877 // the same file multiple times.
878 if (stdin_path != NULL && stdin_path[0])
879 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
880 exit(eDupStdinFailed);
881
882 if (stdout_path != NULL && stdout_path[0])
883 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
884 exit(eDupStdoutFailed);
885
886 if (stderr_path != NULL && stderr_path[0])
887 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
888 exit(eDupStderrFailed);
889
890 // Execute. We should never return.
891 execve(argv[0],
892 const_cast<char *const *>(argv),
893 const_cast<char *const *>(envp));
894 exit(eExecFailed);
895 }
896
897 // Wait for the child process to to trap on its call to execve.
898 ::pid_t wpid;
899 int status;
900 if ((wpid = waitpid(pid, &status, 0)) < 0)
901 {
902 args->m_error.SetErrorToErrno();
903 goto FINISH;
904 }
905 else if (WIFEXITED(status))
906 {
907 // open, dup or execve likely failed for some reason.
908 args->m_error.SetErrorToGenericError();
909 switch (WEXITSTATUS(status))
910 {
911 case ePtraceFailed:
912 args->m_error.SetErrorString("Child ptrace failed.");
913 break;
914 case eDupStdinFailed:
915 args->m_error.SetErrorString("Child open stdin failed.");
916 break;
917 case eDupStdoutFailed:
918 args->m_error.SetErrorString("Child open stdout failed.");
919 break;
920 case eDupStderrFailed:
921 args->m_error.SetErrorString("Child open stderr failed.");
922 break;
923 case eExecFailed:
924 args->m_error.SetErrorString("Child exec failed.");
925 break;
926 default:
927 args->m_error.SetErrorString("Child returned unknown exit status.");
928 break;
929 }
930 goto FINISH;
931 }
932 assert(WIFSTOPPED(status) && wpid == pid &&
933 "Could not sync with inferior process.");
934
935#ifdef notyet
936 // Have the child raise an event on exit. This is used to keep the child in
937 // limbo until it is destroyed.
938 if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0)
939 {
940 args->m_error.SetErrorToErrno();
941 goto FINISH;
942 }
943#endif
944 // XXX - Release the master terminal descriptor and pass it off to the
945 // XXX - ProcessMonitor instance. Similarly stash the inferior pid.
946 // For now just use stdin fd
Filipe Cabecinhasb76d5c92012-05-31 07:49:36 +0000947 monitor->m_terminal_fd = ::dup(STDIN_FILENO);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000948 monitor->m_pid = pid;
949
950 // Set the terminal fd to be in non blocking mode (it simplifies the
951 // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
952 // descriptor to read from).
953 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
954 goto FINISH;
955
956 // Update the process thread list with this new thread.
Greg Clayton29d19302012-02-27 18:40:48 +0000957 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000958 process.GetThreadList().AddThread(inferior);
959
960 // Let our process instance know the thread has stopped.
961 process.SendMessage(ProcessMessage::Trace(pid));
962
963FINISH:
964 return args->m_error.Success();
965}
966
967bool
968ProcessMonitor::EnableIPC()
969{
970 int fd[2];
971
972 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd))
973 return false;
974
975 m_client_fd = fd[0];
976 m_server_fd = fd[1];
977 return true;
978}
979
980void
981ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
982{
983 static const char *g_thread_name = "lldb.process.freebsd.operation";
984
985 if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
986 return;
987
988 m_operation_thread =
989 Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
990}
991
992void
993ProcessMonitor::StopAttachOpThread()
994{
995 assert(!"Not implemented yet!!!");
996}
997
998void *
999ProcessMonitor::AttachOpThread(void *arg)
1000{
1001 AttachArgs *args = static_cast<AttachArgs*>(arg);
1002
1003 if (!Attach(args))
1004 return NULL;
1005
1006 ServeOperation(args);
1007 return NULL;
1008}
1009
1010bool
1011ProcessMonitor::Attach(AttachArgs *args)
1012{
1013 lldb::pid_t pid = args->m_pid;
1014
1015 ProcessMonitor *monitor = args->m_monitor;
1016 ProcessFreeBSD &process = monitor->GetProcess();
Greg Clayton29d19302012-02-27 18:40:48 +00001017 lldb::ProcessSP processSP = process.shared_from_this();
Johnny Chen9ed5b492012-01-05 21:48:15 +00001018 ThreadList &tl = process.GetThreadList();
1019 lldb::ThreadSP inferior;
1020
1021 if (pid <= 1)
1022 {
1023 args->m_error.SetErrorToGenericError();
1024 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1025 goto FINISH;
1026 }
1027
1028 // Attach to the requested process.
1029 if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0)
1030 {
1031 args->m_error.SetErrorToErrno();
1032 goto FINISH;
1033 }
1034
1035 int status;
1036 if ((status = waitpid(pid, NULL, 0)) < 0)
1037 {
1038 args->m_error.SetErrorToErrno();
1039 goto FINISH;
1040 }
1041
1042 // Update the process thread list with the attached thread.
Greg Clayton29d19302012-02-27 18:40:48 +00001043 inferior.reset(new POSIXThread(processSP, pid));
Johnny Chen9ed5b492012-01-05 21:48:15 +00001044 tl.AddThread(inferior);
1045
1046 // Let our process instance know the thread has stopped.
1047 process.SendMessage(ProcessMessage::Trace(pid));
1048
1049 FINISH:
1050 return args->m_error.Success();
1051}
1052
1053bool
1054ProcessMonitor::MonitorCallback(void *callback_baton,
1055 lldb::pid_t pid,
1056 bool exited,
1057 int signal,
1058 int status)
1059{
1060 ProcessMessage message;
1061 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
1062 ProcessFreeBSD *process = monitor->m_process;
1063 bool stop_monitoring;
1064 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001065 int ptrace_err;
Johnny Chen9ed5b492012-01-05 21:48:15 +00001066
Daniel Maleaa35970a2012-11-23 18:09:58 +00001067 if (!monitor->GetSignalInfo(pid, &info, ptrace_err))
Johnny Chen9ed5b492012-01-05 21:48:15 +00001068 stop_monitoring = true; // pid is gone. Bail.
1069 else {
1070 switch (info.si_signo)
1071 {
1072 case SIGTRAP:
1073 message = MonitorSIGTRAP(monitor, &info, pid);
1074 break;
1075
1076 default:
1077 message = MonitorSignal(monitor, &info, pid);
1078 break;
1079 }
1080
1081 process->SendMessage(message);
1082 stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1083 }
1084
1085 return stop_monitoring;
1086}
1087
1088ProcessMessage
1089ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1090 const siginfo_t *info, lldb::pid_t pid)
1091{
1092 ProcessMessage message;
1093
1094 assert(info->si_signo == SIGTRAP && "Unexpected child signal!");
1095
1096 switch (info->si_code)
1097 {
1098 default:
1099 assert(false && "Unexpected SIGTRAP code!");
1100 break;
1101
1102 case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */):
1103 {
1104 // The inferior process is about to exit. Maintain the process in a
1105 // state of "limbo" until we are explicitly commanded to detach,
1106 // destroy, resume, etc.
1107 unsigned long data = 0;
1108 if (!monitor->GetEventMessage(pid, &data))
1109 data = -1;
1110 message = ProcessMessage::Limbo(pid, (data >> 8));
1111 break;
1112 }
1113
1114 case 0:
1115 case TRAP_TRACE:
1116 message = ProcessMessage::Trace(pid);
1117 break;
1118
1119 case SI_KERNEL:
1120 case TRAP_BRKPT:
1121 message = ProcessMessage::Break(pid);
1122 break;
1123 }
1124
1125 return message;
1126}
1127
1128ProcessMessage
1129ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1130 const siginfo_t *info, lldb::pid_t pid)
1131{
1132 ProcessMessage message;
1133 int signo = info->si_signo;
1134
1135 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1136 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1137 // kill(2) or raise(3). Similarly for tgkill(2) on FreeBSD.
1138 //
1139 // IOW, user generated signals never generate what we consider to be a
1140 // "crash".
1141 //
1142 // Similarly, ACK signals generated by this monitor.
1143 if (info->si_code == SI_USER)
1144 {
1145 if (info->si_pid == getpid())
1146 return ProcessMessage::SignalDelivered(pid, signo);
1147 else
1148 return ProcessMessage::Signal(pid, signo);
1149 }
1150
1151 if (signo == SIGSEGV) {
1152 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1153 ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info);
1154 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1155 }
1156
1157 if (signo == SIGILL) {
1158 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1159 ProcessMessage::CrashReason reason = GetCrashReasonForSIGILL(info);
1160 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1161 }
1162
1163 if (signo == SIGFPE) {
1164 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1165 ProcessMessage::CrashReason reason = GetCrashReasonForSIGFPE(info);
1166 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1167 }
1168
1169 if (signo == SIGBUS) {
1170 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1171 ProcessMessage::CrashReason reason = GetCrashReasonForSIGBUS(info);
1172 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1173 }
1174
1175 // Everything else is "normal" and does not require any special action on
1176 // our part.
1177 return ProcessMessage::Signal(pid, signo);
1178}
1179
1180ProcessMessage::CrashReason
1181ProcessMonitor::GetCrashReasonForSIGSEGV(const siginfo_t *info)
1182{
1183 ProcessMessage::CrashReason reason;
1184 assert(info->si_signo == SIGSEGV);
1185
1186 reason = ProcessMessage::eInvalidCrashReason;
1187
1188 switch (info->si_code)
1189 {
1190 default:
1191 assert(false && "unexpected si_code for SIGSEGV");
1192 break;
1193 case SEGV_MAPERR:
1194 reason = ProcessMessage::eInvalidAddress;
1195 break;
1196 case SEGV_ACCERR:
1197 reason = ProcessMessage::ePrivilegedAddress;
1198 break;
1199 }
1200
1201 return reason;
1202}
1203
1204ProcessMessage::CrashReason
1205ProcessMonitor::GetCrashReasonForSIGILL(const siginfo_t *info)
1206{
1207 ProcessMessage::CrashReason reason;
1208 assert(info->si_signo == SIGILL);
1209
1210 reason = ProcessMessage::eInvalidCrashReason;
1211
1212 switch (info->si_code)
1213 {
1214 default:
1215 assert(false && "unexpected si_code for SIGILL");
1216 break;
1217 case ILL_ILLOPC:
1218 reason = ProcessMessage::eIllegalOpcode;
1219 break;
1220 case ILL_ILLOPN:
1221 reason = ProcessMessage::eIllegalOperand;
1222 break;
1223 case ILL_ILLADR:
1224 reason = ProcessMessage::eIllegalAddressingMode;
1225 break;
1226 case ILL_ILLTRP:
1227 reason = ProcessMessage::eIllegalTrap;
1228 break;
1229 case ILL_PRVOPC:
1230 reason = ProcessMessage::ePrivilegedOpcode;
1231 break;
1232 case ILL_PRVREG:
1233 reason = ProcessMessage::ePrivilegedRegister;
1234 break;
1235 case ILL_COPROC:
1236 reason = ProcessMessage::eCoprocessorError;
1237 break;
1238 case ILL_BADSTK:
1239 reason = ProcessMessage::eInternalStackError;
1240 break;
1241 }
1242
1243 return reason;
1244}
1245
1246ProcessMessage::CrashReason
1247ProcessMonitor::GetCrashReasonForSIGFPE(const siginfo_t *info)
1248{
1249 ProcessMessage::CrashReason reason;
1250 assert(info->si_signo == SIGFPE);
1251
1252 reason = ProcessMessage::eInvalidCrashReason;
1253
1254 switch (info->si_code)
1255 {
1256 default:
1257 assert(false && "unexpected si_code for SIGFPE");
1258 break;
1259 case FPE_INTDIV:
1260 reason = ProcessMessage::eIntegerDivideByZero;
1261 break;
1262 case FPE_INTOVF:
1263 reason = ProcessMessage::eIntegerOverflow;
1264 break;
1265 case FPE_FLTDIV:
1266 reason = ProcessMessage::eFloatDivideByZero;
1267 break;
1268 case FPE_FLTOVF:
1269 reason = ProcessMessage::eFloatOverflow;
1270 break;
1271 case FPE_FLTUND:
1272 reason = ProcessMessage::eFloatUnderflow;
1273 break;
1274 case FPE_FLTRES:
1275 reason = ProcessMessage::eFloatInexactResult;
1276 break;
1277 case FPE_FLTINV:
1278 reason = ProcessMessage::eFloatInvalidOperation;
1279 break;
1280 case FPE_FLTSUB:
1281 reason = ProcessMessage::eFloatSubscriptRange;
1282 break;
1283 }
1284
1285 return reason;
1286}
1287
1288ProcessMessage::CrashReason
1289ProcessMonitor::GetCrashReasonForSIGBUS(const siginfo_t *info)
1290{
1291 ProcessMessage::CrashReason reason;
1292 assert(info->si_signo == SIGBUS);
1293
1294 reason = ProcessMessage::eInvalidCrashReason;
1295
1296 switch (info->si_code)
1297 {
1298 default:
1299 assert(false && "unexpected si_code for SIGBUS");
1300 break;
1301 case BUS_ADRALN:
1302 reason = ProcessMessage::eIllegalAlignment;
1303 break;
1304 case BUS_ADRERR:
1305 reason = ProcessMessage::eIllegalAddress;
1306 break;
1307 case BUS_OBJERR:
1308 reason = ProcessMessage::eHardwareError;
1309 break;
1310 }
1311
1312 return reason;
1313}
1314
1315void
1316ProcessMonitor::ServeOperation(OperationArgs *args)
1317{
1318 int status;
1319 pollfd fdset;
1320
1321 ProcessMonitor *monitor = args->m_monitor;
1322
1323 fdset.fd = monitor->m_server_fd;
1324 fdset.events = POLLIN | POLLPRI;
1325 fdset.revents = 0;
1326
1327 // We are finised with the arguments and are ready to go. Sync with the
1328 // parent thread and start serving operations on the inferior.
1329 sem_post(&args->m_semaphore);
1330
1331 for (;;)
1332 {
1333 if ((status = poll(&fdset, 1, -1)) < 0)
1334 {
1335 switch (errno)
1336 {
1337 default:
1338 assert(false && "Unexpected poll() failure!");
1339 continue;
1340
1341 case EINTR: continue; // Just poll again.
1342 case EBADF: return; // Connection terminated.
1343 }
1344 }
1345
1346 assert(status == 1 && "Too many descriptors!");
1347
1348 if (fdset.revents & POLLIN)
1349 {
1350 Operation *op = NULL;
1351
1352 READ_AGAIN:
1353 if ((status = read(fdset.fd, &op, sizeof(op))) < 0)
1354 {
1355 // There is only one acceptable failure.
1356 assert(errno == EINTR);
1357 goto READ_AGAIN;
1358 }
1359
1360 assert(status == sizeof(op));
1361 op->Execute(monitor);
1362 write(fdset.fd, &op, sizeof(op));
1363 }
1364 }
1365}
1366
1367void
1368ProcessMonitor::DoOperation(Operation *op)
1369{
1370 int status;
1371 Operation *ack = NULL;
1372 Mutex::Locker lock(m_server_mutex);
1373
1374 // FIXME: Do proper error checking here.
1375 write(m_client_fd, &op, sizeof(op));
1376
1377READ_AGAIN:
1378 if ((status = read(m_client_fd, &ack, sizeof(ack))) < 0)
1379 {
1380 // If interrupted by a signal handler try again. Otherwise the monitor
1381 // thread probably died and we have a stale file descriptor -- abort the
1382 // operation.
1383 if (errno == EINTR)
1384 goto READ_AGAIN;
1385 return;
1386 }
1387
1388 assert(status == sizeof(ack));
1389 assert(ack == op && "Invalid monitor thread response!");
1390}
1391
1392size_t
1393ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1394 Error &error)
1395{
1396 size_t result;
1397 ReadOperation op(vm_addr, buf, size, error, result);
1398 DoOperation(&op);
1399 return result;
1400}
1401
1402size_t
1403ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
1404 lldb_private::Error &error)
1405{
1406 size_t result;
1407 WriteOperation op(vm_addr, buf, size, error, result);
1408 DoOperation(&op);
1409 return result;
1410}
1411
1412bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001413ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset,
1414 unsigned size, RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001415{
1416 bool result;
1417 ReadRegOperation op(offset, size, value, result);
1418 DoOperation(&op);
1419 return result;
1420}
1421
1422bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001423ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
1424 const RegisterValue &value)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001425{
1426 bool result;
1427 WriteRegOperation op(offset, value, result);
1428 DoOperation(&op);
1429 return result;
1430}
1431
1432bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001433ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001434{
1435 bool result;
1436 ReadGPROperation op(buf, result);
1437 DoOperation(&op);
1438 return result;
1439}
1440
1441bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001442ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001443{
1444 bool result;
1445 ReadFPROperation op(buf, result);
1446 DoOperation(&op);
1447 return result;
1448}
1449
1450bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001451ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001452{
1453 bool result;
1454 WriteGPROperation op(buf, result);
1455 DoOperation(&op);
1456 return result;
1457}
1458
1459bool
Daniel Maleaf0da3712012-12-18 19:50:15 +00001460ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001461{
1462 bool result;
1463 WriteFPROperation op(buf, result);
1464 DoOperation(&op);
1465 return result;
1466}
1467
1468bool
1469ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
1470{
1471 bool result;
1472 ResumeOperation op(tid, signo, result);
1473 DoOperation(&op);
1474 return result;
1475}
1476
1477bool
1478ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
1479{
1480 bool result;
1481 SingleStepOperation op(tid, signo, result);
1482 DoOperation(&op);
1483 return result;
1484}
1485
1486bool
1487ProcessMonitor::BringProcessIntoLimbo()
1488{
1489 bool result;
1490 KillOperation op(result);
1491 DoOperation(&op);
1492 return result;
1493}
1494
1495bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00001496ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Johnny Chen9ed5b492012-01-05 21:48:15 +00001497{
1498 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001499 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Johnny Chen9ed5b492012-01-05 21:48:15 +00001500 DoOperation(&op);
1501 return result;
1502}
1503
1504bool
1505ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
1506{
1507 bool result;
1508 EventMessageOperation op(tid, message, result);
1509 DoOperation(&op);
1510 return result;
1511}
1512
1513Error
1514ProcessMonitor::Detach()
1515{
1516 Error result;
1517 DetachOperation op(result);
1518 DoOperation(&op);
1519 StopMonitor();
1520 return result;
1521}
1522
1523bool
1524ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
1525{
1526 int target_fd = open(path, flags, 0666);
1527
1528 if (target_fd == -1)
1529 return false;
1530
1531 return (dup2(target_fd, fd) == -1) ? false : true;
1532}
1533
1534void
1535ProcessMonitor::StopMonitoringChildProcess()
1536{
1537 lldb::thread_result_t thread_result;
1538
1539 if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
1540 {
1541 Host::ThreadCancel(m_monitor_thread, NULL);
1542 Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
1543 m_monitor_thread = LLDB_INVALID_HOST_THREAD;
1544 }
1545}
1546
1547void
1548ProcessMonitor::StopMonitor()
1549{
1550 StopMonitoringChildProcess();
1551 StopLaunchOpThread();
1552 CloseFD(m_terminal_fd);
1553 CloseFD(m_client_fd);
1554 CloseFD(m_server_fd);
1555}
1556
1557void
1558ProcessMonitor::CloseFD(int &fd)
1559{
1560 if (fd != -1)
1561 {
1562 close(fd);
1563 fd = -1;
1564 }
1565}