LLDB  mainline
ProcessMonitor.cpp
Go to the documentation of this file.
1 //===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <errno.h>
10 #include <poll.h>
11 #include <signal.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <sys/ptrace.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 
20 #include "lldb/Host/Host.h"
24 #include "lldb/Target/Thread.h"
27 #include "lldb/Utility/Scalar.h"
28 #include "lldb/Utility/Status.h"
29 #include "llvm/Support/Errno.h"
30 
31 #include "FreeBSDThread.h"
34 #include "ProcessFreeBSD.h"
35 #include "ProcessMonitor.h"
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 
40 // We disable the tracing of ptrace calls for integration builds to avoid the
41 // additional indirection and checks.
42 #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
43 // Wrapper for ptrace to catch errors and log calls.
44 
45 const char *Get_PT_IO_OP(int op) {
46  switch (op) {
47  case PIOD_READ_D:
48  return "READ_D";
49  case PIOD_WRITE_D:
50  return "WRITE_D";
51  case PIOD_READ_I:
52  return "READ_I";
53  case PIOD_WRITE_I:
54  return "WRITE_I";
55  default:
56  return "Unknown op";
57  }
58 }
59 
60 // Wrapper for ptrace to catch errors and log calls. Note that ptrace sets
61 // errno on error because -1 is reserved as a valid result.
62 extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data,
63  const char *reqName, const char *file, int line) {
64  long int result;
65 
67 
68  if (log) {
69  log->Printf("ptrace(%s, %" PRIu64 ", %p, %x) called from file %s line %d",
70  reqName, pid, addr, data, file, line);
71  if (req == PT_IO) {
72  struct ptrace_io_desc *pi = (struct ptrace_io_desc *)addr;
73 
74  log->Printf("PT_IO: op=%s offs=%zx size=%zu", Get_PT_IO_OP(pi->piod_op),
75  (size_t)pi->piod_offs, pi->piod_len);
76  }
77  }
78 
79  // PtraceDisplayBytes(req, data);
80 
81  errno = 0;
82  result = ptrace(req, pid, (caddr_t)addr, data);
83 
84  // PtraceDisplayBytes(req, data);
85 
86  if (log && errno != 0) {
87  const char *str;
88  switch (errno) {
89  case ESRCH:
90  str = "ESRCH";
91  break;
92  case EINVAL:
93  str = "EINVAL";
94  break;
95  case EBUSY:
96  str = "EBUSY";
97  break;
98  case EPERM:
99  str = "EPERM";
100  break;
101  default:
102  str = "<unknown>";
103  }
104  log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
105  }
106 
107  if (log) {
108 #ifdef __amd64__
109  if (req == PT_GETREGS) {
110  struct reg *r = (struct reg *)addr;
111 
112  log->Printf("PT_GETREGS: rip=0x%lx rsp=0x%lx rbp=0x%lx rax=0x%lx",
113  r->r_rip, r->r_rsp, r->r_rbp, r->r_rax);
114  }
115  if (req == PT_GETDBREGS || req == PT_SETDBREGS) {
116  struct dbreg *r = (struct dbreg *)addr;
117  char setget = (req == PT_GETDBREGS) ? 'G' : 'S';
118 
119  for (int i = 0; i <= 7; i++)
120  log->Printf("PT_%cETDBREGS: dr[%d]=0x%lx", setget, i, r->dr[i]);
121  }
122 #endif
123  }
124 
125  return result;
126 }
127 
128 // Wrapper for ptrace when logging is not required. Sets errno to 0 prior to
129 // calling ptrace.
130 extern long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data) {
131  long result = 0;
132  errno = 0;
133  result = ptrace(req, pid, (caddr_t)addr, data);
134  return result;
135 }
136 
137 #define PTRACE(req, pid, addr, data) \
138  PtraceWrapper((req), (pid), (addr), (data), #req, __FILE__, __LINE__)
139 #else
140 PtraceWrapper((req), (pid), (addr), (data))
141 #endif
142 
143 // Static implementations of ProcessMonitor::ReadMemory and
144 // ProcessMonitor::WriteMemory. This enables mutual recursion between these
145 // functions without needed to go thru the thread funnel.
146 
147 static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf,
148  size_t size, Status &error) {
149  struct ptrace_io_desc pi_desc;
150 
151  pi_desc.piod_op = PIOD_READ_D;
152  pi_desc.piod_offs = (void *)vm_addr;
153  pi_desc.piod_addr = buf;
154  pi_desc.piod_len = size;
155 
156  if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
157  error.SetErrorToErrno();
158  return 0;
159  }
160  return pi_desc.piod_len;
161 }
162 
163 static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr,
164  const void *buf, size_t size, Status &error) {
165  struct ptrace_io_desc pi_desc;
166 
167  pi_desc.piod_op = PIOD_WRITE_D;
168  pi_desc.piod_offs = (void *)vm_addr;
169  pi_desc.piod_addr = const_cast<void *>(buf);
170  pi_desc.piod_len = size;
171 
172  if (PTRACE(PT_IO, pid, (caddr_t)&pi_desc, 0) < 0) {
173  error.SetErrorToErrno();
174  return 0;
175  }
176  return pi_desc.piod_len;
177 }
178 
179 // Simple helper function to ensure flags are enabled on the given file
180 // descriptor.
181 static bool EnsureFDFlags(int fd, int flags, Status &error) {
182  int status;
183 
184  if ((status = fcntl(fd, F_GETFL)) == -1) {
185  error.SetErrorToErrno();
186  return false;
187  }
188 
189  if (fcntl(fd, F_SETFL, status | flags) == -1) {
190  error.SetErrorToErrno();
191  return false;
192  }
193 
194  return true;
195 }
196 
197 /// \class Operation
198 /// Represents a ProcessMonitor operation.
199 ///
200 /// Under FreeBSD, it is not possible to ptrace() from any other thread but
201 /// the one that spawned or attached to the process from the start.
202 /// Therefore, when a ProcessMonitor is asked to deliver or change the state
203 /// of an inferior process the operation must be "funneled" to a specific
204 /// thread to perform the task. The Operation class provides an abstract base
205 /// for all services the ProcessMonitor must perform via the single virtual
206 /// function Execute, thus encapsulating the code that needs to run in the
207 /// privileged context.
208 class Operation {
209 public:
210  virtual ~Operation() {}
211  virtual void Execute(ProcessMonitor *monitor) = 0;
212 };
213 
214 /// \class ReadOperation
215 /// Implements ProcessMonitor::ReadMemory.
216 class ReadOperation : public Operation {
217 public:
218  ReadOperation(lldb::addr_t addr, void *buff, size_t size, Status &error,
219  size_t &result)
220  : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
221  m_result(result) {}
222 
223  void Execute(ProcessMonitor *monitor);
224 
225 private:
226  lldb::addr_t m_addr;
227  void *m_buff;
228  size_t m_size;
229  Status &m_error;
230  size_t &m_result;
231 };
232 
234  lldb::pid_t pid = monitor->GetPID();
235 
236  m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
237 }
238 
239 /// \class WriteOperation
240 /// Implements ProcessMonitor::WriteMemory.
241 class WriteOperation : public Operation {
242 public:
243  WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
244  Status &error, size_t &result)
245  : m_addr(addr), m_buff(buff), m_size(size), m_error(error),
246  m_result(result) {}
247 
248  void Execute(ProcessMonitor *monitor);
249 
250 private:
251  lldb::addr_t m_addr;
252  const void *m_buff;
253  size_t m_size;
254  Status &m_error;
255  size_t &m_result;
256 };
257 
259  lldb::pid_t pid = monitor->GetPID();
260 
261  m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
262 }
263 
264 /// \class ReadRegOperation
265 /// Implements ProcessMonitor::ReadRegisterValue.
266 class ReadRegOperation : public Operation {
267 public:
268  ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
269  RegisterValue &value, bool &result)
270  : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
271  m_result(result) {}
272 
273  void Execute(ProcessMonitor *monitor);
274 
275 private:
276  lldb::tid_t m_tid;
277  unsigned m_offset;
278  unsigned m_size;
279  RegisterValue &m_value;
280  bool &m_result;
281 };
282 
284  struct reg regs;
285  int rc;
286 
287  if ((rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
288  m_result = false;
289  } else {
290  // 'struct reg' contains only 32- or 64-bit register values. Punt on
291  // others. Also, not all entries may be uintptr_t sized, such as 32-bit
292  // processes on powerpc64 (probably the same for i386 on amd64)
293  if (m_size == sizeof(uint32_t))
294  m_value = *(uint32_t *)(((caddr_t)&regs) + m_offset);
295  else if (m_size == sizeof(uint64_t))
296  m_value = *(uint64_t *)(((caddr_t)&regs) + m_offset);
297  else
298  memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
299  m_result = true;
300  }
301 }
302 
303 /// \class WriteRegOperation
304 /// Implements ProcessMonitor::WriteRegisterValue.
305 class WriteRegOperation : public Operation {
306 public:
307  WriteRegOperation(lldb::tid_t tid, unsigned offset,
308  const RegisterValue &value, bool &result)
309  : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
310 
311  void Execute(ProcessMonitor *monitor);
312 
313 private:
314  lldb::tid_t m_tid;
315  unsigned m_offset;
316  const RegisterValue &m_value;
317  bool &m_result;
318 };
319 
321  struct reg regs;
322 
323  if (PTRACE(PT_GETREGS, m_tid, (caddr_t)&regs, 0) < 0) {
324  m_result = false;
325  return;
326  }
327  *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
328  (uintptr_t)m_value.GetAsUInt64();
329  if (PTRACE(PT_SETREGS, m_tid, (caddr_t)&regs, 0) < 0)
330  m_result = false;
331  else
332  m_result = true;
333 }
334 
335 /// \class ReadDebugRegOperation
336 /// Implements ProcessMonitor::ReadDebugRegisterValue.
338 public:
339  ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size,
340  RegisterValue &value, bool &result)
341  : m_tid(tid), m_offset(offset), m_size(size), m_value(value),
342  m_result(result) {}
343 
344  void Execute(ProcessMonitor *monitor);
345 
346 private:
347  lldb::tid_t m_tid;
348  unsigned m_offset;
349  unsigned m_size;
350  RegisterValue &m_value;
351  bool &m_result;
352 };
353 
355  struct dbreg regs;
356  int rc;
357 
358  if ((rc = PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0)) < 0) {
359  m_result = false;
360  } else {
361  if (m_size == sizeof(uintptr_t))
362  m_value = *(uintptr_t *)(((caddr_t)&regs) + m_offset);
363  else
364  memcpy((void *)&m_value, (((caddr_t)&regs) + m_offset), m_size);
365  m_result = true;
366  }
367 }
368 
369 /// \class WriteDebugRegOperation
370 /// Implements ProcessMonitor::WriteDebugRegisterValue.
372 public:
373  WriteDebugRegOperation(lldb::tid_t tid, unsigned offset,
374  const RegisterValue &value, bool &result)
375  : m_tid(tid), m_offset(offset), m_value(value), m_result(result) {}
376 
377  void Execute(ProcessMonitor *monitor);
378 
379 private:
380  lldb::tid_t m_tid;
381  unsigned m_offset;
382  const RegisterValue &m_value;
383  bool &m_result;
384 };
385 
387  struct dbreg regs;
388 
389  if (PTRACE(PT_GETDBREGS, m_tid, (caddr_t)&regs, 0) < 0) {
390  m_result = false;
391  return;
392  }
393  *(uintptr_t *)(((caddr_t)&regs) + m_offset) =
394  (uintptr_t)m_value.GetAsUInt64();
395  if (PTRACE(PT_SETDBREGS, m_tid, (caddr_t)&regs, 0) < 0)
396  m_result = false;
397  else
398  m_result = true;
399 }
400 
401 /// \class ReadGPROperation
402 /// Implements ProcessMonitor::ReadGPR.
403 class ReadGPROperation : public Operation {
404 public:
405  ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
406  : m_tid(tid), m_buf(buf), m_result(result) {}
407 
408  void Execute(ProcessMonitor *monitor);
409 
410 private:
411  lldb::tid_t m_tid;
412  void *m_buf;
413  bool &m_result;
414 };
415 
417  int rc;
418 
419  errno = 0;
420  rc = PTRACE(PT_GETREGS, m_tid, (caddr_t)m_buf, 0);
421  if (errno != 0)
422  m_result = false;
423  else
424  m_result = true;
425 }
426 
427 /// \class ReadFPROperation
428 /// Implements ProcessMonitor::ReadFPR.
429 class ReadFPROperation : public Operation {
430 public:
431  ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
432  : m_tid(tid), m_buf(buf), m_result(result) {}
433 
434  void Execute(ProcessMonitor *monitor);
435 
436 private:
437  lldb::tid_t m_tid;
438  void *m_buf;
439  bool &m_result;
440 };
441 
443  if (PTRACE(PT_GETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
444  m_result = false;
445  else
446  m_result = true;
447 }
448 
449 /// \class WriteGPROperation
450 /// Implements ProcessMonitor::WriteGPR.
451 class WriteGPROperation : public Operation {
452 public:
453  WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
454  : m_tid(tid), m_buf(buf), m_result(result) {}
455 
456  void Execute(ProcessMonitor *monitor);
457 
458 private:
459  lldb::tid_t m_tid;
460  void *m_buf;
461  bool &m_result;
462 };
463 
465  if (PTRACE(PT_SETREGS, m_tid, (caddr_t)m_buf, 0) < 0)
466  m_result = false;
467  else
468  m_result = true;
469 }
470 
471 /// \class WriteFPROperation
472 /// Implements ProcessMonitor::WriteFPR.
473 class WriteFPROperation : public Operation {
474 public:
475  WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
476  : m_tid(tid), m_buf(buf), m_result(result) {}
477 
478  void Execute(ProcessMonitor *monitor);
479 
480 private:
481  lldb::tid_t m_tid;
482  void *m_buf;
483  bool &m_result;
484 };
485 
487  if (PTRACE(PT_SETFPREGS, m_tid, (caddr_t)m_buf, 0) < 0)
488  m_result = false;
489  else
490  m_result = true;
491 }
492 
493 /// \class ResumeOperation
494 /// Implements ProcessMonitor::Resume.
495 class ResumeOperation : public Operation {
496 public:
497  ResumeOperation(uint32_t signo, bool &result)
498  : m_signo(signo), m_result(result) {}
499 
500  void Execute(ProcessMonitor *monitor);
501 
502 private:
503  uint32_t m_signo;
504  bool &m_result;
505 };
506 
508  lldb::pid_t pid = monitor->GetPID();
509  int data = 0;
510 
511  if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
512  data = m_signo;
513 
514  if (PTRACE(PT_CONTINUE, pid, (caddr_t)1, data)) {
516  LLDB_LOG(log, "ResumeOperation ({0}) failed: {1}", pid,
517  llvm::sys::StrError(errno));
518  m_result = false;
519  } else
520  m_result = true;
521 }
522 
523 /// \class SingleStepOperation
524 /// Implements ProcessMonitor::SingleStep.
526 public:
527  SingleStepOperation(uint32_t signo, bool &result)
528  : m_signo(signo), m_result(result) {}
529 
530  void Execute(ProcessMonitor *monitor);
531 
532 private:
533  uint32_t m_signo;
534  bool &m_result;
535 };
536 
538  lldb::pid_t pid = monitor->GetPID();
539  int data = 0;
540 
541  if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
542  data = m_signo;
543 
544  if (PTRACE(PT_STEP, pid, NULL, data))
545  m_result = false;
546  else
547  m_result = true;
548 }
549 
550 /// \class LwpInfoOperation
551 /// Implements ProcessMonitor::GetLwpInfo.
552 class LwpInfoOperation : public Operation {
553 public:
554  LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
555  : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) {}
556 
557  void Execute(ProcessMonitor *monitor);
558 
559 private:
560  lldb::tid_t m_tid;
561  void *m_info;
562  bool &m_result;
563  int &m_err;
564 };
565 
567  struct ptrace_lwpinfo plwp;
568 
569  if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp))) {
570  m_result = false;
571  m_err = errno;
572  } else {
573  memcpy(m_info, &plwp, sizeof(plwp));
574  m_result = true;
575  }
576 }
577 
578 /// \class ThreadSuspendOperation
579 /// Implements ProcessMonitor::ThreadSuspend.
581 public:
582  ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
583  : m_tid(tid), m_suspend(suspend), m_result(result) {}
584 
585  void Execute(ProcessMonitor *monitor);
586 
587 private:
588  lldb::tid_t m_tid;
589  bool m_suspend;
590  bool &m_result;
591 };
592 
594  m_result = !PTRACE(m_suspend ? PT_SUSPEND : PT_RESUME, m_tid, NULL, 0);
595 }
596 
597 /// \class EventMessageOperation
598 /// Implements ProcessMonitor::GetEventMessage.
600 public:
601  EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
602  : m_tid(tid), m_message(message), m_result(result) {}
603 
604  void Execute(ProcessMonitor *monitor);
605 
606 private:
607  lldb::tid_t m_tid;
608  unsigned long *m_message;
609  bool &m_result;
610 };
611 
613  struct ptrace_lwpinfo plwp;
614 
615  if (PTRACE(PT_LWPINFO, m_tid, (caddr_t)&plwp, sizeof(plwp)))
616  m_result = false;
617  else {
618  if (plwp.pl_flags & PL_FLAG_FORKED) {
619  *m_message = plwp.pl_child_pid;
620  m_result = true;
621  } else
622  m_result = false;
623  }
624 }
625 
626 /// \class KillOperation
627 /// Implements ProcessMonitor::Kill.
628 class KillOperation : public Operation {
629 public:
630  KillOperation(bool &result) : m_result(result) {}
631 
632  void Execute(ProcessMonitor *monitor);
633 
634 private:
635  bool &m_result;
636 };
637 
639  lldb::pid_t pid = monitor->GetPID();
640 
641  if (PTRACE(PT_KILL, pid, NULL, 0))
642  m_result = false;
643  else
644  m_result = true;
645 }
646 
647 /// \class DetachOperation
648 /// Implements ProcessMonitor::Detach.
649 class DetachOperation : public Operation {
650 public:
651  DetachOperation(Status &result) : m_error(result) {}
652 
653  void Execute(ProcessMonitor *monitor);
654 
655 private:
656  Status &m_error;
657 };
658 
660  lldb::pid_t pid = monitor->GetPID();
661 
662  if (PTRACE(PT_DETACH, pid, NULL, 0) < 0)
663  m_error.SetErrorToErrno();
664 }
665 
666 ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
667  : m_monitor(monitor) {
668  sem_init(&m_semaphore, 0, 0);
669 }
670 
671 ProcessMonitor::OperationArgs::~OperationArgs() { sem_destroy(&m_semaphore); }
672 
673 ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
674  lldb_private::Module *module,
675  char const **argv, Environment env,
676  const FileSpec &stdin_file_spec,
677  const FileSpec &stdout_file_spec,
678  const FileSpec &stderr_file_spec,
679  const FileSpec &working_dir)
680  : OperationArgs(monitor), m_module(module), m_argv(argv),
681  m_env(std::move(env)), m_stdin_file_spec(stdin_file_spec),
682  m_stdout_file_spec(stdout_file_spec),
683  m_stderr_file_spec(stderr_file_spec), m_working_dir(working_dir) {}
684 
685 ProcessMonitor::LaunchArgs::~LaunchArgs() {}
686 
687 ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid)
688  : OperationArgs(monitor), m_pid(pid) {}
689 
690 ProcessMonitor::AttachArgs::~AttachArgs() {}
691 
692 /// The basic design of the ProcessMonitor is built around two threads.
693 ///
694 /// One thread (@see SignalThread) simply blocks on a call to waitpid()
695 /// looking for changes in the debugee state. When a change is detected a
696 /// ProcessMessage is sent to the associated ProcessFreeBSD instance. This
697 /// thread "drives" state changes in the debugger.
698 ///
699 /// The second thread (@see OperationThread) is responsible for two things 1)
700 /// launching or attaching to the inferior process, and then 2) servicing
701 /// operations such as register reads/writes, stepping, etc. See the comments
702 /// on the Operation class for more info as to why this is needed.
704  ProcessFreeBSD *process, Module *module, const char *argv[],
705  Environment env, const FileSpec &stdin_file_spec,
706  const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec,
707  const FileSpec &working_dir,
708  const lldb_private::ProcessLaunchInfo & /* launch_info */,
709  lldb_private::Status &error)
710  : m_process(static_cast<ProcessFreeBSD *>(process)),
711  m_pid(LLDB_INVALID_PROCESS_ID), m_terminal_fd(-1), m_operation(0) {
712  using namespace std::placeholders;
713 
714  std::unique_ptr<LaunchArgs> args(
715  new LaunchArgs(this, module, argv, std::move(env), stdin_file_spec,
716  stdout_file_spec, stderr_file_spec, working_dir));
717 
718  sem_init(&m_operation_pending, 0, 0);
719  sem_init(&m_operation_done, 0, 0);
720 
721  StartLaunchOpThread(args.get(), error);
722  if (!error.Success())
723  return;
724 
725  if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
726  error.SetErrorToErrno();
727  return;
728  }
729 
730  // Check that the launch was a success.
731  if (!args->m_error.Success()) {
732  StopOpThread();
733  error = args->m_error;
734  return;
735  }
736 
737  // Finally, start monitoring the child process for change in state.
738  m_monitor_thread = Host::StartMonitoringChildProcess(
739  std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
740  GetPID(), true);
741  if (!m_monitor_thread.IsJoinable()) {
742  error.SetErrorToGenericError();
743  error.SetErrorString("Process launch failed.");
744  return;
745  }
746 }
747 
749  lldb_private::Status &error)
750  : m_process(static_cast<ProcessFreeBSD *>(process)), m_pid(pid),
751  m_terminal_fd(-1), m_operation(0) {
752  using namespace std::placeholders;
753 
754  sem_init(&m_operation_pending, 0, 0);
755  sem_init(&m_operation_done, 0, 0);
756 
757  std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
758 
759  StartAttachOpThread(args.get(), error);
760  if (!error.Success())
761  return;
762 
763  if (llvm::sys::RetryAfterSignal(-1, sem_wait, &args->m_semaphore) == -1) {
764  error.SetErrorToErrno();
765  return;
766  }
767 
768  // Check that the attach was a success.
769  if (!args->m_error.Success()) {
770  StopOpThread();
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  std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4),
778  GetPID(), true);
779  if (!m_monitor_thread.IsJoinable()) {
780  error.SetErrorToGenericError();
781  error.SetErrorString("Process attach failed.");
782  return;
783  }
784 }
785 
787 
788 // Thread setup and tear down.
789 void ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Status &error) {
790  static const char *g_thread_name = "lldb.process.freebsd.operation";
791 
792  if (m_operation_thread.IsJoinable())
793  return;
794 
795  m_operation_thread =
796  ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
797 }
798 
799 void *ProcessMonitor::LaunchOpThread(void *arg) {
800  LaunchArgs *args = static_cast<LaunchArgs *>(arg);
801 
802  if (!Launch(args)) {
803  sem_post(&args->m_semaphore);
804  return NULL;
805  }
806 
807  ServeOperation(args);
808  return NULL;
809 }
810 
811 bool ProcessMonitor::Launch(LaunchArgs *args) {
812  ProcessMonitor *monitor = args->m_monitor;
813  ProcessFreeBSD &process = monitor->GetProcess();
814  const char **argv = args->m_argv;
815  const FileSpec &stdin_file_spec = args->m_stdin_file_spec;
816  const FileSpec &stdout_file_spec = args->m_stdout_file_spec;
817  const FileSpec &stderr_file_spec = args->m_stderr_file_spec;
818  const FileSpec &working_dir = args->m_working_dir;
819 
820  PseudoTerminal terminal;
821  const size_t err_len = 1024;
822  char err_str[err_len];
823  ::pid_t pid;
824 
825  // Propagate the environment if one is not supplied.
826  Environment::Envp envp =
827  (args->m_env.empty() ? Host::GetEnvironment() : args->m_env).getEnvp();
828 
829  if ((pid = terminal.Fork(err_str, err_len)) == -1) {
830  args->m_error.SetErrorToGenericError();
831  args->m_error.SetErrorString("Process fork failed.");
832  goto FINISH;
833  }
834 
835  // Recognized child exit status codes.
836  enum {
837  ePtraceFailed = 1,
838  eDupStdinFailed,
839  eDupStdoutFailed,
840  eDupStderrFailed,
841  eChdirFailed,
842  eExecFailed,
843  eSetGidFailed
844  };
845 
846  // Child process.
847  if (pid == 0) {
848  // Trace this process.
849  if (PTRACE(PT_TRACE_ME, 0, NULL, 0) < 0)
850  exit(ePtraceFailed);
851 
852  // terminal has already dupped the tty descriptors to stdin/out/err. This
853  // closes original fd from which they were copied (and avoids leaking
854  // descriptors to the debugged process.
855  terminal.CloseSlaveFileDescriptor();
856 
857  // Do not inherit setgid powers.
858  if (setgid(getgid()) != 0)
859  exit(eSetGidFailed);
860 
861  // Let us have our own process group.
862  setpgid(0, 0);
863 
864  // Dup file descriptors if needed.
865  //
866  // FIXME: If two or more of the paths are the same we needlessly open
867  // the same file multiple times.
868  if (stdin_file_spec)
869  if (!DupDescriptor(stdin_file_spec, STDIN_FILENO, O_RDONLY))
870  exit(eDupStdinFailed);
871 
872  if (stdout_file_spec)
873  if (!DupDescriptor(stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT))
874  exit(eDupStdoutFailed);
875 
876  if (stderr_file_spec)
877  if (!DupDescriptor(stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT))
878  exit(eDupStderrFailed);
879 
880  // Change working directory
881  if (working_dir && 0 != ::chdir(working_dir.GetCString()))
882  exit(eChdirFailed);
883 
884  // Execute. We should never return.
885  execve(argv[0], const_cast<char *const *>(argv), envp);
886  exit(eExecFailed);
887  }
888 
889  // Wait for the child process to to trap on its call to execve.
890  ::pid_t wpid;
891  int status;
892  if ((wpid = waitpid(pid, &status, 0)) < 0) {
893  args->m_error.SetErrorToErrno();
894  goto FINISH;
895  } else if (WIFEXITED(status)) {
896  // open, dup or execve likely failed for some reason.
897  args->m_error.SetErrorToGenericError();
898  switch (WEXITSTATUS(status)) {
899  case ePtraceFailed:
900  args->m_error.SetErrorString("Child ptrace failed.");
901  break;
902  case eDupStdinFailed:
903  args->m_error.SetErrorString("Child open stdin failed.");
904  break;
905  case eDupStdoutFailed:
906  args->m_error.SetErrorString("Child open stdout failed.");
907  break;
908  case eDupStderrFailed:
909  args->m_error.SetErrorString("Child open stderr failed.");
910  break;
911  case eChdirFailed:
912  args->m_error.SetErrorString("Child failed to set working directory.");
913  break;
914  case eExecFailed:
915  args->m_error.SetErrorString("Child exec failed.");
916  break;
917  case eSetGidFailed:
918  args->m_error.SetErrorString("Child setgid failed.");
919  break;
920  default:
921  args->m_error.SetErrorString("Child returned unknown exit status.");
922  break;
923  }
924  goto FINISH;
925  }
926  assert(WIFSTOPPED(status) && wpid == (::pid_t)pid &&
927  "Could not sync with inferior process.");
928 
929 #ifdef notyet
930  // Have the child raise an event on exit. This is used to keep the child in
931  // limbo until it is destroyed.
932  if (PTRACE(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT) < 0) {
933  args->m_error.SetErrorToErrno();
934  goto FINISH;
935  }
936 #endif
937  // Release the master terminal descriptor and pass it off to the
938  // ProcessMonitor instance. Similarly stash the inferior pid.
939  monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
940  monitor->m_pid = pid;
941 
942  // Set the terminal fd to be in non blocking mode (it simplifies the
943  // implementation of ProcessFreeBSD::GetSTDOUT to have a non-blocking
944  // descriptor to read from).
945  if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
946  goto FINISH;
947 
948  process.SendMessage(ProcessMessage::Attach(pid));
949 
950 FINISH:
951  return args->m_error.Success();
952 }
953 
954 void ProcessMonitor::StartAttachOpThread(AttachArgs *args,
955  lldb_private::Status &error) {
956  static const char *g_thread_name = "lldb.process.freebsd.operation";
957 
958  if (m_operation_thread.IsJoinable())
959  return;
960 
961  m_operation_thread =
962  ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
963 }
964 
965 void *ProcessMonitor::AttachOpThread(void *arg) {
966  AttachArgs *args = static_cast<AttachArgs *>(arg);
967 
968  Attach(args);
969 
970  ServeOperation(args);
971  return NULL;
972 }
973 
974 void ProcessMonitor::Attach(AttachArgs *args) {
975  lldb::pid_t pid = args->m_pid;
976 
977  ProcessMonitor *monitor = args->m_monitor;
978  ProcessFreeBSD &process = monitor->GetProcess();
979 
980  if (pid <= 1) {
981  args->m_error.SetErrorToGenericError();
982  args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
983  return;
984  }
985 
986  // Attach to the requested process.
987  if (PTRACE(PT_ATTACH, pid, NULL, 0) < 0) {
988  args->m_error.SetErrorToErrno();
989  return;
990  }
991 
992  int status;
993  if ((status = waitpid(pid, NULL, 0)) < 0) {
994  args->m_error.SetErrorToErrno();
995  return;
996  }
997 
998  process.SendMessage(ProcessMessage::Attach(pid));
999 }
1000 
1001 size_t
1002 ProcessMonitor::GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids) {
1003  lwpid_t *tids;
1004  int tdcnt;
1005 
1006  thread_ids.clear();
1007 
1008  tdcnt = PTRACE(PT_GETNUMLWPS, m_pid, NULL, 0);
1009  if (tdcnt <= 0)
1010  return 0;
1011  tids = (lwpid_t *)malloc(tdcnt * sizeof(*tids));
1012  if (tids == NULL)
1013  return 0;
1014  if (PTRACE(PT_GETLWPLIST, m_pid, (void *)tids, tdcnt) < 0) {
1015  free(tids);
1016  return 0;
1017  }
1018  thread_ids = std::vector<lldb::tid_t>(tids, tids + tdcnt);
1019  free(tids);
1020  return thread_ids.size();
1021 }
1022 
1023 bool ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
1024  bool exited, int signal, int status) {
1025  ProcessMessage message;
1026  ProcessFreeBSD *process = monitor->m_process;
1027  assert(process);
1028  bool stop_monitoring;
1029  struct ptrace_lwpinfo plwp;
1030  int ptrace_err;
1031 
1033 
1034  if (exited) {
1035  if (log)
1036  log->Printf("ProcessMonitor::%s() got exit signal, tid = %" PRIu64,
1037  __FUNCTION__, pid);
1038  message = ProcessMessage::Exit(pid, status);
1039  process->SendMessage(message);
1040  return pid == process->GetID();
1041  }
1042 
1043  if (!monitor->GetLwpInfo(pid, &plwp, ptrace_err))
1044  stop_monitoring = true; // pid is gone. Bail.
1045  else {
1046  switch (plwp.pl_siginfo.si_signo) {
1047  case SIGTRAP:
1048  message = MonitorSIGTRAP(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1049  break;
1050 
1051  default:
1052  message = MonitorSignal(monitor, &plwp.pl_siginfo, plwp.pl_lwpid);
1053  break;
1054  }
1055 
1056  process->SendMessage(message);
1057  stop_monitoring = message.GetKind() == ProcessMessage::eExitMessage;
1058  }
1059 
1060  return stop_monitoring;
1061 }
1062 
1063 ProcessMessage ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
1064  const siginfo_t *info,
1065  lldb::tid_t tid) {
1066  ProcessMessage message;
1067 
1069 
1070  assert(monitor);
1071  assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
1072 
1073  switch (info->si_code) {
1074  default:
1075  assert(false && "Unexpected SIGTRAP code!");
1076  break;
1077 
1078  case (SIGTRAP /* | (PTRACE_EVENT_EXIT << 8) */): {
1079  // The inferior process is about to exit. Maintain the process in a state
1080  // of "limbo" until we are explicitly commanded to detach, destroy, resume,
1081  // etc.
1082  unsigned long data = 0;
1083  if (!monitor->GetEventMessage(tid, &data))
1084  data = -1;
1085  if (log)
1086  log->Printf("ProcessMonitor::%s() received exit? event, data = %lx, tid "
1087  "= %" PRIu64,
1088  __FUNCTION__, data, tid);
1089  message = ProcessMessage::Limbo(tid, (data >> 8));
1090  break;
1091  }
1092 
1093  case 0:
1094  case TRAP_TRACE:
1095 #ifdef TRAP_CAP
1096  // Map TRAP_CAP to a trace trap in the absense of a more specific handler.
1097  case TRAP_CAP:
1098 #endif
1099  if (log)
1100  log->Printf("ProcessMonitor::%s() received trace event, tid = %" PRIu64
1101  " : si_code = %d",
1102  __FUNCTION__, tid, info->si_code);
1103  message = ProcessMessage::Trace(tid);
1104  break;
1105 
1106  case SI_KERNEL:
1107  case TRAP_BRKPT:
1108  if (monitor->m_process->IsSoftwareStepBreakpoint(tid)) {
1109  if (log)
1110  log->Printf("ProcessMonitor::%s() received sw single step breakpoint "
1111  "event, tid = %" PRIu64,
1112  __FUNCTION__, tid);
1113  message = ProcessMessage::Trace(tid);
1114  } else {
1115  if (log)
1116  log->Printf(
1117  "ProcessMonitor::%s() received breakpoint event, tid = %" PRIu64,
1118  __FUNCTION__, tid);
1119  message = ProcessMessage::Break(tid);
1120  }
1121  break;
1122  }
1123 
1124  return message;
1125 }
1126 
1127 ProcessMessage ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
1128  const siginfo_t *info,
1129  lldb::tid_t tid) {
1130  ProcessMessage message;
1131  int signo = info->si_signo;
1132 
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 kill(2)
1137  // 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  if (log)
1145  log->Printf(
1146  "ProcessMonitor::%s() received signal %s with code %s, pid = %d",
1147  __FUNCTION__,
1148  monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo),
1149  "SI_USER", info->si_pid);
1150  if (info->si_pid == getpid())
1151  return ProcessMessage::SignalDelivered(tid, signo);
1152  else
1153  return ProcessMessage::Signal(tid, signo);
1154  }
1155 
1156  if (log)
1157  log->Printf(
1158  "ProcessMonitor::%s() received signal %s", __FUNCTION__,
1159  monitor->m_process->GetUnixSignals()->GetSignalAsCString(signo));
1160 
1161  switch (signo) {
1162  case SIGSEGV:
1163  case SIGILL:
1164  case SIGFPE:
1165  case SIGBUS:
1166  lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
1167  const auto reason = GetCrashReason(*info);
1168  if (reason != CrashReason::eInvalidCrashReason) {
1169  return ProcessMessage::Crash(tid, reason, signo, fault_addr);
1170  } // else; Use atleast si_signo info for other si_code
1171  }
1172 
1173  // Everything else is "normal" and does not require any special action on our
1174  // part.
1175  return ProcessMessage::Signal(tid, signo);
1176 }
1177 
1178 void ProcessMonitor::ServeOperation(OperationArgs *args) {
1179  ProcessMonitor *monitor = args->m_monitor;
1180 
1181  // We are finised with the arguments and are ready to go. Sync with the
1182  // parent thread and start serving operations on the inferior.
1183  sem_post(&args->m_semaphore);
1184 
1185  for (;;) {
1186  // wait for next pending operation
1187  sem_wait(&monitor->m_operation_pending);
1188 
1189  monitor->m_operation->Execute(monitor);
1190 
1191  // notify calling thread that operation is complete
1192  sem_post(&monitor->m_operation_done);
1193  }
1194 }
1195 
1196 void ProcessMonitor::DoOperation(Operation *op) {
1197  std::lock_guard<std::mutex> guard(m_operation_mutex);
1198 
1199  m_operation = op;
1200 
1201  // notify operation thread that an operation is ready to be processed
1202  sem_post(&m_operation_pending);
1203 
1204  // wait for operation to complete
1205  sem_wait(&m_operation_done);
1206 }
1207 
1208 size_t ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
1209  Status &error) {
1210  size_t result;
1211  ReadOperation op(vm_addr, buf, size, error, result);
1212  DoOperation(&op);
1213  return result;
1214 }
1215 
1216 size_t ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf,
1217  size_t size, lldb_private::Status &error) {
1218  size_t result;
1219  WriteOperation op(vm_addr, buf, size, error, result);
1220  DoOperation(&op);
1221  return result;
1222 }
1223 
1225  const char *reg_name, unsigned size,
1226  RegisterValue &value) {
1227  bool result;
1228  ReadRegOperation op(tid, offset, size, value, result);
1229  DoOperation(&op);
1230  return result;
1231 }
1232 
1234  const char *reg_name,
1235  const RegisterValue &value) {
1236  bool result;
1237  WriteRegOperation op(tid, offset, value, result);
1238  DoOperation(&op);
1239  return result;
1240 }
1241 
1243  lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size,
1244  lldb_private::RegisterValue &value) {
1245  bool result;
1246  ReadDebugRegOperation op(tid, offset, size, value, result);
1247  DoOperation(&op);
1248  return result;
1249 }
1250 
1252  lldb::tid_t tid, unsigned offset, const char *reg_name,
1253  const lldb_private::RegisterValue &value) {
1254  bool result;
1255  WriteDebugRegOperation op(tid, offset, value, result);
1256  DoOperation(&op);
1257  return result;
1258 }
1259 
1260 bool ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1261  bool result;
1262  ReadGPROperation op(tid, buf, result);
1263  DoOperation(&op);
1264  return result;
1265 }
1266 
1267 bool ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1268  bool result;
1269  ReadFPROperation op(tid, buf, result);
1270  DoOperation(&op);
1271  return result;
1272 }
1273 
1275  size_t buf_size, unsigned int regset) {
1276  return false;
1277 }
1278 
1279 bool ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1280  bool result;
1281  WriteGPROperation op(tid, buf, result);
1282  DoOperation(&op);
1283  return result;
1284 }
1285 
1286 bool ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) {
1287  bool result;
1288  WriteFPROperation op(tid, buf, result);
1289  DoOperation(&op);
1290  return result;
1291 }
1292 
1294  size_t buf_size, unsigned int regset) {
1295  return false;
1296 }
1297 
1299  return false;
1300 }
1301 
1303  bool result;
1305 
1306  if (log) {
1307  const char *signame =
1308  m_process->GetUnixSignals()->GetSignalAsCString(signo);
1309  if (signame == nullptr)
1310  signame = "<none>";
1311  log->Printf("ProcessMonitor::%s() resuming pid %" PRIu64 " with signal %s",
1312  __FUNCTION__, GetPID(), signame);
1313  }
1314  ResumeOperation op(signo, result);
1315  DoOperation(&op);
1316  if (log)
1317  log->Printf("ProcessMonitor::%s() resuming result = %s", __FUNCTION__,
1318  result ? "true" : "false");
1319  return result;
1320 }
1321 
1323  bool result;
1324  SingleStepOperation op(signo, result);
1325  DoOperation(&op);
1326  return result;
1327 }
1328 
1330  bool result;
1331  KillOperation op(result);
1332  DoOperation(&op);
1333  return result;
1334 }
1335 
1336 bool ProcessMonitor::GetLwpInfo(lldb::tid_t tid, void *lwpinfo,
1337  int &ptrace_err) {
1338  bool result;
1339  LwpInfoOperation op(tid, lwpinfo, result, ptrace_err);
1340  DoOperation(&op);
1341  return result;
1342 }
1343 
1345  bool result;
1346  ThreadSuspendOperation op(tid, suspend, result);
1347  DoOperation(&op);
1348  return result;
1349 }
1350 
1351 bool ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message) {
1352  bool result;
1353  EventMessageOperation op(tid, message, result);
1354  DoOperation(&op);
1355  return result;
1356 }
1357 
1359  lldb_private::Status error;
1360  if (tid != LLDB_INVALID_THREAD_ID) {
1361  DetachOperation op(error);
1362  DoOperation(&op);
1363  }
1364  return error;
1365 }
1366 
1367 bool ProcessMonitor::DupDescriptor(const FileSpec &file_spec, int fd,
1368  int flags) {
1369  int target_fd = llvm::sys::RetryAfterSignal(-1, open,
1370  file_spec.GetCString(), flags, 0666);
1371 
1372  if (target_fd == -1)
1373  return false;
1374 
1375  if (dup2(target_fd, fd) == -1)
1376  return false;
1377 
1378  return (close(target_fd) == -1) ? false : true;
1379 }
1380 
1381 void ProcessMonitor::StopMonitoringChildProcess() {
1382  if (m_monitor_thread.IsJoinable()) {
1383  m_monitor_thread.Cancel();
1384  m_monitor_thread.Join(nullptr);
1385  m_monitor_thread.Reset();
1386  }
1387 }
1388 
1390  StopMonitoringChildProcess();
1391  StopOpThread();
1392  sem_destroy(&m_operation_pending);
1393  sem_destroy(&m_operation_done);
1394  if (m_terminal_fd >= 0) {
1395  close(m_terminal_fd);
1396  m_terminal_fd = -1;
1397  }
1398 }
1399 
1400 // FIXME: On Linux, when a new thread is created, we receive to notifications,
1401 // (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the child
1402 // thread id as additional information, and (2) a SIGSTOP|SI_USER from the new
1403 // child thread indicating that it has is stopped because we attached. We have
1404 // no guarantee of the order in which these arrive, but we need both before we
1405 // are ready to proceed. We currently keep a list of threads which have sent
1406 // the initial SIGSTOP|SI_USER event. Then when we receive the
1407 // SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not
1408 // occurred we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1409 //
1410 // Right now, the above logic is in ProcessPOSIX, so we need a definition of
1411 // this function in the FreeBSD ProcessMonitor implementation even if it isn't
1412 // logically needed.
1413 //
1414 // We really should figure out what actually happens on FreeBSD and move the
1415 // Linux-specific logic out of ProcessPOSIX as needed.
1416 
1418 
1419 void ProcessMonitor::StopOpThread() {
1420  if (!m_operation_thread.IsJoinable())
1421  return;
1422 
1423  m_operation_thread.Cancel();
1424  m_operation_thread.Join(nullptr);
1425  m_operation_thread.Reset();
1426 }
void Execute(ProcessMonitor *monitor)
size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, lldb_private::Status &error)
Reads size bytes from address in the inferior process address space.
ReadFPROperation(lldb::tid_t tid, void *buf, bool &result)
ReadDebugRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, RegisterValue &value, bool &result)
ThreadSuspendOperation(lldb::tid_t tid, bool suspend, bool &result)
void Execute(ProcessMonitor *monitor)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
void CloseSlaveFileDescriptor()
Close the slave file descriptor if it is valid.
void Execute(ProcessMonitor *monitor)
static ProcessMessage Exit(lldb::tid_t tid, int status)
Indicates that the thread tid is about to exit with status status.
bool IsSoftwareStepBreakpoint(lldb::tid_t tid)
void Execute(ProcessMonitor *monitor)
long PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data, const char *reqName, const char *file, int line)
void Execute(ProcessMonitor *monitor)
#define PTRACE(req, pid, addr, data)
WriteRegOperation(lldb::tid_t tid, unsigned offset, const RegisterValue &value, bool &result)
void Execute(ProcessMonitor *monitor)
static ProcessMessage Crash(lldb::pid_t pid, CrashReason reason, int signo, lldb::addr_t fault_addr)
Indicates that the thread tid crashed.
static bool EnsureFDFlags(int fd, int flags, Status &error)
bool ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size, lldb_private::RegisterValue &value)
Reads the contents from the debug register identified by the given (architecture dependent) offset...
bool ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
Reads the specified register set into the specified buffer.
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:92
WriteDebugRegOperation(lldb::tid_t tid, unsigned offset, const RegisterValue &value, bool &result)
#define O_NONBLOCK
void Execute(ProcessMonitor *monitor)
Implements ProcessMonitor::GetEventMessage.
#define LLDB_INVALID_SIGNAL_NUMBER
Definition: lldb-defines.h:95
CrashReason GetCrashReason(const siginfo_t &info)
lldb::pid_t Fork(char *error_str, size_t error_len)
Fork a child process that uses pseudo terminals for its stdio.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
Implements ProcessMonitor::WriteMemory.
A file utility class.
Definition: FileSpec.h:55
bool SingleStep(lldb::tid_t unused, uint32_t signo)
Single steps the process.
bool WaitForInitialTIDStop(lldb::tid_t tid)
A pseudo terminal helper class.
WriteOperation(lldb::addr_t addr, const void *buff, size_t size, Status &error, size_t &result)
virtual void Execute(ProcessMonitor *monitor)=0
static size_t DoWriteMemory(lldb::pid_t pid, lldb::addr_t vm_addr, const void *buf, size_t size, Status &error)
Implements ProcessMonitor::GetLwpInfo.
Implements ProcessMonitor::WriteFPR.
ResumeOperation(uint32_t signo, bool &result)
#define LLDB_LOG(log,...)
Definition: Log.h:209
int ReleaseMasterFileDescriptor()
Release the master file descriptor.
bool GetEventMessage(lldb::tid_t tid, unsigned long *message)
Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) corresponding to the given thread ID...
void Execute(ProcessMonitor *monitor)
void SetErrorToErrno()
Set the current error to errno.
Definition: Status.cpp:223
Implements ProcessMonitor::Resume.
Implements ProcessMonitor::ReadFPR.
SingleStepOperation(uint32_t signo, bool &result)
bool ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Reads all floating point registers into the specified buffer.
Manages communication with the inferior (debugee) process.
virtual ~Operation()
void Execute(ProcessMonitor *monitor)
const lldb::UnixSignalsSP & GetUnixSignals()
Definition: Process.cpp:3361
size_t GetCurrentThreadIDs(std::vector< lldb::tid_t > &thread_ids)
Returns current thread IDs in process.
Implements ProcessMonitor::Kill.
void SetErrorToGenericError()
Set the current error to a generic error.
Definition: Status.cpp:231
void Execute(ProcessMonitor *monitor)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
static ProcessMessage Attach(lldb::pid_t pid)
Indicates that the process pid has successfully attached.
#define POSIX_LOG_PROCESS
ReadOperation(lldb::addr_t addr, void *buff, size_t size, Status &error, size_t &result)
bool ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
Reads the value of the thread-specific pointer for a given thread ID.
Kind GetKind() const
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:109
EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
lldb::pid_t GetPID() const
Provides the process number of debugee.
uint64_t tid_t
Definition: lldb-types.h:86
bool GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no)
Writes a ptrace_lwpinfo structure corresponding to the given thread ID to the memory region pointed t...
Implements ProcessMonitor::WriteGPR.
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
Implements ProcessMonitor::ThreadSuspend.
KillOperation(bool &result)
virtual void SendMessage(const ProcessMessage &message)
Registers the given message with this process.
const char * Get_PT_IO_OP(int op)
bool Success() const
Test for success condition.
Definition: Status.cpp:287
DetachOperation(Status &result)
static ProcessMessage SignalDelivered(lldb::tid_t tid, int signum)
Indicates that a signal signum generated by the debugging process was delivered to the thread tid...
Implements ProcessMonitor::ReadMemory.
static size_t DoReadMemory(lldb::pid_t pid, lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
static ProcessMessage Break(lldb::tid_t tid)
Indicates that the thread tid encountered a break point.
bool Kill()
Terminate the traced process.
Implements ProcessMonitor::Detach.
ProcessMonitor(ProcessFreeBSD *process, lldb_private::Module *module, char const *argv[], lldb_private::Environment env, const lldb_private::FileSpec &stdin_file_spec, const lldb_private::FileSpec &stdout_file_spec, const lldb_private::FileSpec &stderr_file_spec, const lldb_private::FileSpec &working_dir, const lldb_private::ProcessLaunchInfo &launch_info, lldb_private::Status &error)
Launches an inferior process ready for debugging.
const char * GetCString(bool denormalize=true) const
Definition: FileSpec.cpp:392
Implements ProcessMonitor::ReadRegisterValue.
static ProcessMessage Limbo(lldb::tid_t tid, int status)
Indicates that the thread tid is about to exit with status status.
bool WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Writes all floating point registers into the specified buffer.
void Execute(ProcessMonitor *monitor)
Implements ProcessMonitor::SingleStep.
Implements ProcessMonitor::WriteRegisterValue.
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:93
uint64_t addr_t
Definition: lldb-types.h:83
static ProcessMessage Signal(lldb::tid_t tid, int signum)
Indicates that the thread tid had the signal signum delivered.
void Execute(ProcessMonitor *monitor)
WriteFPROperation(lldb::tid_t tid, void *buf, bool &result)
ProcessFreeBSD & GetProcess()
Returns the process associated with this ProcessMonitor.
Definition: SBAddress.h:15
LwpInfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
bool ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, unsigned size, lldb_private::RegisterValue &value)
Reads the contents from the register identified by the given (architecture dependent) offset...
bool ThreadSuspend(lldb::tid_t tid, bool suspend)
Suspends or unsuspends a thread prior to process resume or step.
uint64_t pid_t
Definition: lldb-types.h:85
Represents a ProcessMonitor operation.
Implements ProcessMonitor::ReadGPR.
lldb_private::Status Detach(lldb::tid_t tid)
#define POSIX_LOG_PTRACE
static ProcessMessage Trace(lldb::tid_t tid)
Indicates that the thread tid encountered a trace point.
ReadRegOperation(lldb::tid_t tid, unsigned offset, unsigned size, RegisterValue &value, bool &result)
void Execute(ProcessMonitor *monitor)
WriteGPROperation(lldb::tid_t tid, void *buf, bool &result)
void Execute(ProcessMonitor *monitor)
#define SIGTRAP
void Execute(ProcessMonitor *monitor)
bool Resume(lldb::tid_t unused, uint32_t signo)
Resumes the process.
Implements ProcessMonitor::WriteDebugRegisterValue.
void Execute(ProcessMonitor *monitor)
Implements ProcessMonitor::ReadDebugRegisterValue.
ReadGPROperation(lldb::tid_t tid, void *buf, bool &result)
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
bool WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
Writes the specified register set into the specified buffer.
bool ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Reads all general purpose registers into the specified buffer.
bool WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, const lldb_private::RegisterValue &value)
Writes the given value to the register identified by the given (architecture dependent) offset...
size_t WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, lldb_private::Status &error)
Writes size bytes from address vm_adder in the inferior process address space.
void Execute(ProcessMonitor *monitor)
bool WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, const lldb_private::RegisterValue &value)
Writes the given value to the debug register identified by the given (architecture dependent) offset...
An error handling class.
Definition: Status.h:44
bool WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Writes all general purpose registers into the specified buffer.