]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Process / FreeBSD / ProcessMonitor.h
1 //===-- ProcessMonitor.h -------------------------------------- -*- 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 #ifndef liblldb_ProcessMonitor_H_
11 #define liblldb_ProcessMonitor_H_
12
13 // C Includes
14 #include <semaphore.h>
15 #include <signal.h>
16
17 // C++ Includes
18 #include <mutex>
19
20 // Other libraries and framework includes
21 #include "lldb/Host/HostThread.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/lldb-types.h"
24
25 namespace lldb_private {
26 class Status;
27 class Module;
28 class Scalar;
29 } // End lldb_private namespace.
30
31 class ProcessFreeBSD;
32 class Operation;
33
34 /// @class ProcessMonitor
35 /// Manages communication with the inferior (debugee) process.
36 ///
37 /// Upon construction, this class prepares and launches an inferior process
38 /// for debugging.
39 ///
40 /// Changes in the inferior process state are propagated to the associated
41 /// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
42 /// appropriate ProcessMessage events.
43 ///
44 /// A purposely minimal set of operations are provided to interrogate and change
45 /// the inferior process state.
46 class ProcessMonitor {
47 public:
48   /// Launches an inferior process ready for debugging.  Forms the
49   /// implementation of Process::DoLaunch.
50   ProcessMonitor(ProcessFreeBSD *process, lldb_private::Module *module,
51                  char const *argv[], lldb_private::Environment env,
52                  const lldb_private::FileSpec &stdin_file_spec,
53                  const lldb_private::FileSpec &stdout_file_spec,
54                  const lldb_private::FileSpec &stderr_file_spec,
55                  const lldb_private::FileSpec &working_dir,
56                  const lldb_private::ProcessLaunchInfo &launch_info,
57                  lldb_private::Status &error);
58
59   ProcessMonitor(ProcessFreeBSD *process, lldb::pid_t pid,
60                  lldb_private::Status &error);
61
62   ~ProcessMonitor();
63
64   /// Provides the process number of debugee.
65   lldb::pid_t GetPID() const { return m_pid; }
66
67   /// Returns the process associated with this ProcessMonitor.
68   ProcessFreeBSD &GetProcess() { return *m_process; }
69
70   /// Returns a file descriptor to the controlling terminal of the inferior
71   /// process.
72   ///
73   /// Reads from this file descriptor yield both the standard output and
74   /// standard error of this debugee.  Even if stderr and stdout were
75   /// redirected on launch it may still happen that data is available on this
76   /// descriptor (if the inferior process opens /dev/tty, for example). This
77   /// descriptor is closed after a call to StopMonitor().
78   ///
79   /// If this monitor was attached to an existing process this method returns
80   /// -1.
81   int GetTerminalFD() const { return m_terminal_fd; }
82
83   /// Reads @p size bytes from address @vm_adder in the inferior process
84   /// address space.
85   ///
86   /// This method is provided to implement Process::DoReadMemory.
87   size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
88                     lldb_private::Status &error);
89
90   /// Writes @p size bytes from address @p vm_adder in the inferior process
91   /// address space.
92   ///
93   /// This method is provided to implement Process::DoWriteMemory.
94   size_t WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
95                      lldb_private::Status &error);
96
97   /// Reads the contents from the register identified by the given
98   /// (architecture dependent) offset.
99   ///
100   /// This method is provided for use by RegisterContextFreeBSD derivatives.
101   bool ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
102                          unsigned size, lldb_private::RegisterValue &value);
103
104   /// Writes the given value to the register identified by the given
105   /// (architecture dependent) offset.
106   ///
107   /// This method is provided for use by RegisterContextFreeBSD derivatives.
108   bool WriteRegisterValue(lldb::tid_t tid, unsigned offset,
109                           const char *reg_name,
110                           const lldb_private::RegisterValue &value);
111
112   /// Reads the contents from the debug register identified by the given
113   /// (architecture dependent) offset.
114   ///
115   /// This method is provided for use by RegisterContextFreeBSD derivatives.
116   bool ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
117                               const char *reg_name, unsigned size,
118                               lldb_private::RegisterValue &value);
119
120   /// Writes the given value to the debug register identified by the given
121   /// (architecture dependent) offset.
122   ///
123   /// This method is provided for use by RegisterContextFreeBSD derivatives.
124   bool WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
125                                const char *reg_name,
126                                const lldb_private::RegisterValue &value);
127   /// Reads all general purpose registers into the specified buffer.
128   bool ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
129
130   /// Reads all floating point registers into the specified buffer.
131   bool ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
132
133   /// Reads the specified register set into the specified buffer.
134   ///
135   /// This method is provided for use by RegisterContextFreeBSD derivatives.
136   bool ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size,
137                        unsigned int regset);
138
139   /// Writes all general purpose registers into the specified buffer.
140   bool WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
141
142   /// Writes all floating point registers into the specified buffer.
143   bool WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
144
145   /// Writes the specified register set into the specified buffer.
146   ///
147   /// This method is provided for use by RegisterContextFreeBSD derivatives.
148   bool WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size,
149                         unsigned int regset);
150
151   /// Reads the value of the thread-specific pointer for a given thread ID.
152   bool ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
153
154   /// Returns current thread IDs in process
155   size_t GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
156
157   /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
158   /// to the memory region pointed to by @p lwpinfo.
159   bool GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
160
161   /// Suspends or unsuspends a thread prior to process resume or step.
162   bool ThreadSuspend(lldb::tid_t tid, bool suspend);
163
164   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
165   /// corresponding to the given thread IDto the memory pointed to by @p
166   /// message.
167   bool GetEventMessage(lldb::tid_t tid, unsigned long *message);
168
169   /// Resumes the process.  If @p signo is anything but
170   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
171   bool Resume(lldb::tid_t unused, uint32_t signo);
172
173   /// Single steps the process.  If @p signo is anything but
174   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
175   bool SingleStep(lldb::tid_t unused, uint32_t signo);
176
177   /// Terminate the traced process.
178   bool Kill();
179
180   lldb_private::Status Detach(lldb::tid_t tid);
181
182   void StopMonitor();
183
184   // Waits for the initial stop message from a new thread.
185   bool WaitForInitialTIDStop(lldb::tid_t tid);
186
187 private:
188   ProcessFreeBSD *m_process;
189
190   lldb_private::HostThread m_operation_thread;
191   lldb_private::HostThread m_monitor_thread;
192   lldb::pid_t m_pid;
193
194   int m_terminal_fd;
195
196   // current operation which must be executed on the privileged thread
197   Operation *m_operation;
198   std::mutex m_operation_mutex;
199
200   // semaphores notified when Operation is ready to be processed and when
201   // the operation is complete.
202   sem_t m_operation_pending;
203   sem_t m_operation_done;
204
205   struct OperationArgs {
206     OperationArgs(ProcessMonitor *monitor);
207
208     ~OperationArgs();
209
210     ProcessMonitor *m_monitor;   // The monitor performing the attach.
211     sem_t m_semaphore;           // Posted to once operation complete.
212     lldb_private::Status m_error; // Set if process operation failed.
213   };
214
215   /// @class LauchArgs
216   ///
217   /// Simple structure to pass data to the thread responsible for launching a
218   /// child process.
219   struct LaunchArgs : OperationArgs {
220     LaunchArgs(ProcessMonitor *monitor, lldb_private::Module *module,
221                char const **argv, lldb_private::Environment env,
222                const lldb_private::FileSpec &stdin_file_spec,
223                const lldb_private::FileSpec &stdout_file_spec,
224                const lldb_private::FileSpec &stderr_file_spec,
225                const lldb_private::FileSpec &working_dir);
226
227     ~LaunchArgs();
228
229     lldb_private::Module *m_module; // The executable image to launch.
230     char const **m_argv;            // Process arguments.
231     lldb_private::Environment m_env;                // Process environment.
232     const lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty.
233     const lldb_private::FileSpec
234         m_stdout_file_spec; // Redirect stdout or empty.
235     const lldb_private::FileSpec
236         m_stderr_file_spec;                     // Redirect stderr or empty.
237     const lldb_private::FileSpec m_working_dir; // Working directory or empty.
238   };
239
240   void StartLaunchOpThread(LaunchArgs *args, lldb_private::Status &error);
241
242   static void *LaunchOpThread(void *arg);
243
244   static bool Launch(LaunchArgs *args);
245
246   struct AttachArgs : OperationArgs {
247     AttachArgs(ProcessMonitor *monitor, lldb::pid_t pid);
248
249     ~AttachArgs();
250
251     lldb::pid_t m_pid; // pid of the process to be attached.
252   };
253
254   void StartAttachOpThread(AttachArgs *args, lldb_private::Status &error);
255
256   static void *AttachOpThread(void *args);
257
258   static void Attach(AttachArgs *args);
259
260   static void ServeOperation(OperationArgs *args);
261
262   static bool DupDescriptor(const lldb_private::FileSpec &file_spec, int fd,
263                             int flags);
264
265   static bool MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid,
266                               bool exited, int signal, int status);
267
268   static ProcessMessage MonitorSIGTRAP(ProcessMonitor *monitor,
269                                        const siginfo_t *info, lldb::pid_t pid);
270
271   static ProcessMessage MonitorSignal(ProcessMonitor *monitor,
272                                       const siginfo_t *info, lldb::pid_t pid);
273
274   void DoOperation(Operation *op);
275
276   /// Stops the child monitor thread.
277   void StopMonitoringChildProcess();
278
279   /// Stops the operation thread used to attach/launch a process.
280   void StopOpThread();
281 };
282
283 #endif // #ifndef liblldb_ProcessMonitor_H_