1 //===-- ProcessMonitor.h -------------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ProcessMonitor_H_
11 #define liblldb_ProcessMonitor_H_
14 #include <semaphore.h>
20 // Other libraries and framework includes
21 #include "lldb/lldb-types.h"
22 #include "lldb/Host/FileSpec.h"
23 #include "lldb/Host/HostThread.h"
25 namespace lldb_private
30 } // End lldb_private namespace.
35 /// @class ProcessMonitor
36 /// @brief Manages communication with the inferior (debugee) process.
38 /// Upon construction, this class prepares and launches an inferior process for
41 /// Changes in the inferior process state are propagated to the associated
42 /// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
43 /// appropriate ProcessMessage events.
45 /// A purposely minimal set of operations are provided to interrogate and change
46 /// the inferior process state.
51 /// Launches an inferior process ready for debugging. Forms the
52 /// implementation of Process::DoLaunch.
53 ProcessMonitor(ProcessFreeBSD *process,
54 lldb_private::Module *module,
57 const lldb_private::FileSpec &stdin_file_spec,
58 const lldb_private::FileSpec &stdout_file_spec,
59 const lldb_private::FileSpec &stderr_file_spec,
60 const lldb_private::FileSpec &working_dir,
61 const lldb_private::ProcessLaunchInfo &launch_info,
62 lldb_private::Error &error);
64 ProcessMonitor(ProcessFreeBSD *process,
66 lldb_private::Error &error);
70 /// Provides the process number of debugee.
72 GetPID() const { return m_pid; }
74 /// Returns the process associated with this ProcessMonitor.
76 GetProcess() { return *m_process; }
78 /// Returns a file descriptor to the controlling terminal of the inferior
81 /// Reads from this file descriptor yield both the standard output and
82 /// standard error of this debugee. Even if stderr and stdout were
83 /// redirected on launch it may still happen that data is available on this
84 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is
85 /// closed after a call to StopMonitor().
87 /// If this monitor was attached to an existing process this method returns
90 GetTerminalFD() const { return m_terminal_fd; }
92 /// Reads @p size bytes from address @vm_adder in the inferior process
95 /// This method is provided to implement Process::DoReadMemory.
97 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
98 lldb_private::Error &error);
100 /// Writes @p size bytes from address @p vm_adder in the inferior process
103 /// This method is provided to implement Process::DoWriteMemory.
105 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
106 lldb_private::Error &error);
108 /// Reads the contents from the register identified by the given (architecture
109 /// dependent) offset.
111 /// This method is provided for use by RegisterContextFreeBSD derivatives.
113 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
114 unsigned size, lldb_private::RegisterValue &value);
116 /// Writes the given value to the register identified by the given
117 /// (architecture dependent) offset.
119 /// This method is provided for use by RegisterContextFreeBSD derivatives.
121 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
122 const lldb_private::RegisterValue &value);
124 /// Reads the contents from the debug register identified by the given
125 /// (architecture dependent) offset.
127 /// This method is provided for use by RegisterContextFreeBSD derivatives.
129 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
130 const char *reg_name, unsigned size,
131 lldb_private::RegisterValue &value);
133 /// Writes the given value to the debug register identified by the given
134 /// (architecture dependent) offset.
136 /// This method is provided for use by RegisterContextFreeBSD derivatives.
138 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
139 const char *reg_name,
140 const lldb_private::RegisterValue &value);
141 /// Reads all general purpose registers into the specified buffer.
143 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
145 /// Reads all floating point registers into the specified buffer.
147 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
149 /// Reads the specified register set into the specified buffer.
151 /// This method is provided for use by RegisterContextFreeBSD derivatives.
153 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
155 /// Writes all general purpose registers into the specified buffer.
157 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
159 /// Writes all floating point registers into the specified buffer.
161 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
163 /// Writes the specified register set into the specified buffer.
165 /// This method is provided for use by RegisterContextFreeBSD derivatives.
167 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
169 /// Reads the value of the thread-specific pointer for a given thread ID.
171 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
173 /// Returns current thread IDs in process
175 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
177 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
178 /// to the memory region pointed to by @p lwpinfo.
180 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
182 /// Suspends or unsuspends a thread prior to process resume or step.
184 ThreadSuspend(lldb::tid_t tid, bool suspend);
186 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
187 /// corresponding to the given thread IDto the memory pointed to by @p
190 GetEventMessage(lldb::tid_t tid, unsigned long *message);
192 /// Resumes the process. If @p signo is anything but
193 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
195 Resume(lldb::tid_t unused, uint32_t signo);
197 /// Single steps the process. If @p signo is anything but
198 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
200 SingleStep(lldb::tid_t unused, uint32_t signo);
202 /// Terminate the traced process.
207 Detach(lldb::tid_t tid);
212 // Waits for the initial stop message from a new thread.
214 WaitForInitialTIDStop(lldb::tid_t tid);
217 ProcessFreeBSD *m_process;
219 lldb_private::HostThread m_operation_thread;
220 lldb_private::HostThread m_monitor_thread;
225 // current operation which must be executed on the privileged thread
226 Operation *m_operation;
227 std::mutex m_operation_mutex;
229 // semaphores notified when Operation is ready to be processed and when
230 // the operation is complete.
231 sem_t m_operation_pending;
232 sem_t m_operation_done;
236 OperationArgs(ProcessMonitor *monitor);
240 ProcessMonitor *m_monitor; // The monitor performing the attach.
241 sem_t m_semaphore; // Posted to once operation complete.
242 lldb_private::Error m_error; // Set if process operation failed.
247 /// @brief Simple structure to pass data to the thread responsible for
248 /// launching a child process.
249 struct LaunchArgs : OperationArgs
251 LaunchArgs(ProcessMonitor *monitor,
252 lldb_private::Module *module,
255 const lldb_private::FileSpec &stdin_file_spec,
256 const lldb_private::FileSpec &stdout_file_spec,
257 const lldb_private::FileSpec &stderr_file_spec,
258 const lldb_private::FileSpec &working_dir);
262 lldb_private::Module *m_module; // The executable image to launch.
263 char const **m_argv; // Process arguments.
264 char const **m_envp; // Process environment.
265 const lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty.
266 const lldb_private::FileSpec m_stdout_file_spec; // Redirect stdout or empty.
267 const lldb_private::FileSpec m_stderr_file_spec; // Redirect stderr or empty.
268 const lldb_private::FileSpec m_working_dir; // Working directory or empty.
272 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
275 LaunchOpThread(void *arg);
278 Launch(LaunchArgs *args);
280 struct AttachArgs : OperationArgs
282 AttachArgs(ProcessMonitor *monitor,
287 lldb::pid_t m_pid; // pid of the process to be attached.
291 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
294 AttachOpThread(void *args);
297 Attach(AttachArgs *args);
300 ServeOperation(OperationArgs *args);
303 DupDescriptor(const lldb_private::FileSpec &file_spec, int fd, int flags);
306 MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status);
308 static ProcessMessage
309 MonitorSIGTRAP(ProcessMonitor *monitor,
310 const siginfo_t *info, lldb::pid_t pid);
312 static ProcessMessage
313 MonitorSignal(ProcessMonitor *monitor,
314 const siginfo_t *info, lldb::pid_t pid);
317 DoOperation(Operation *op);
319 /// Stops the child monitor thread.
321 StopMonitoringChildProcess();
323 /// Stops the operation thread used to attach/launch a process.
328 #endif // #ifndef liblldb_ProcessMonitor_H_