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>
18 // Other libraries and framework includes
19 #include "lldb/lldb-types.h"
20 #include "lldb/Host/FileSpec.h"
21 #include "lldb/Host/HostThread.h"
22 #include "lldb/Host/Mutex.h"
24 namespace lldb_private
29 } // End lldb_private namespace.
34 /// @class ProcessMonitor
35 /// @brief Manages communication with the inferior (debugee) process.
37 /// Upon construction, this class prepares and launches an inferior process for
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.
44 /// A purposely minimal set of operations are provided to interrogate and change
45 /// the inferior process state.
50 /// Launches an inferior process ready for debugging. Forms the
51 /// implementation of Process::DoLaunch.
52 ProcessMonitor(ProcessFreeBSD *process,
53 lldb_private::Module *module,
56 const lldb_private::FileSpec &stdin_file_spec,
57 const lldb_private::FileSpec &stdout_file_spec,
58 const lldb_private::FileSpec &stderr_file_spec,
59 const lldb_private::FileSpec &working_dir,
60 const lldb_private::ProcessLaunchInfo &launch_info,
61 lldb_private::Error &error);
63 ProcessMonitor(ProcessFreeBSD *process,
65 lldb_private::Error &error);
69 /// Provides the process number of debugee.
71 GetPID() const { return m_pid; }
73 /// Returns the process associated with this ProcessMonitor.
75 GetProcess() { return *m_process; }
77 /// Returns a file descriptor to the controlling terminal of the inferior
80 /// Reads from this file descriptor yield both the standard output and
81 /// standard error of this debugee. Even if stderr and stdout were
82 /// redirected on launch it may still happen that data is available on this
83 /// descriptor (if the inferior process opens /dev/tty, for example). This descriptor is
84 /// closed after a call to StopMonitor().
86 /// If this monitor was attached to an existing process this method returns
89 GetTerminalFD() const { return m_terminal_fd; }
91 /// Reads @p size bytes from address @vm_adder in the inferior process
94 /// This method is provided to implement Process::DoReadMemory.
96 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
97 lldb_private::Error &error);
99 /// Writes @p size bytes from address @p vm_adder in the inferior process
102 /// This method is provided to implement Process::DoWriteMemory.
104 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
105 lldb_private::Error &error);
107 /// Reads the contents from the register identified by the given (architecture
108 /// dependent) offset.
110 /// This method is provided for use by RegisterContextFreeBSD derivatives.
112 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
113 unsigned size, lldb_private::RegisterValue &value);
115 /// Writes the given value to the register identified by the given
116 /// (architecture dependent) offset.
118 /// This method is provided for use by RegisterContextFreeBSD derivatives.
120 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
121 const lldb_private::RegisterValue &value);
123 /// Reads the contents from the debug register identified by the given
124 /// (architecture dependent) offset.
126 /// This method is provided for use by RegisterContextFreeBSD derivatives.
128 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
129 const char *reg_name, unsigned size,
130 lldb_private::RegisterValue &value);
132 /// Writes the given value to the debug register identified by the given
133 /// (architecture dependent) offset.
135 /// This method is provided for use by RegisterContextFreeBSD derivatives.
137 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
138 const char *reg_name,
139 const lldb_private::RegisterValue &value);
140 /// Reads all general purpose registers into the specified buffer.
142 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
144 /// Reads all floating point registers into the specified buffer.
146 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
148 /// Reads the specified register set into the specified buffer.
150 /// This method is provided for use by RegisterContextFreeBSD derivatives.
152 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
154 /// Writes all general purpose registers into the specified buffer.
156 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
158 /// Writes all floating point registers into the specified buffer.
160 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
162 /// Writes the specified register set into the specified buffer.
164 /// This method is provided for use by RegisterContextFreeBSD derivatives.
166 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
168 /// Reads the value of the thread-specific pointer for a given thread ID.
170 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
172 /// Returns current thread IDs in process
174 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
176 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
177 /// to the memory region pointed to by @p lwpinfo.
179 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
181 /// Suspends or unsuspends a thread prior to process resume or step.
183 ThreadSuspend(lldb::tid_t tid, bool suspend);
185 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
186 /// corresponding to the given thread IDto the memory pointed to by @p
189 GetEventMessage(lldb::tid_t tid, unsigned long *message);
191 /// Resumes the process. If @p signo is anything but
192 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
194 Resume(lldb::tid_t unused, uint32_t signo);
196 /// Single steps the process. If @p signo is anything but
197 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
199 SingleStep(lldb::tid_t unused, uint32_t signo);
201 /// Terminate the traced process.
206 Detach(lldb::tid_t tid);
211 // Waits for the initial stop message from a new thread.
213 WaitForInitialTIDStop(lldb::tid_t tid);
216 ProcessFreeBSD *m_process;
218 lldb_private::HostThread m_operation_thread;
219 lldb_private::HostThread m_monitor_thread;
224 // current operation which must be executed on the privileged thread
225 Operation *m_operation;
226 lldb_private::Mutex m_operation_mutex;
228 // semaphores notified when Operation is ready to be processed and when
229 // the operation is complete.
230 sem_t m_operation_pending;
231 sem_t m_operation_done;
235 OperationArgs(ProcessMonitor *monitor);
239 ProcessMonitor *m_monitor; // The monitor performing the attach.
240 sem_t m_semaphore; // Posted to once operation complete.
241 lldb_private::Error m_error; // Set if process operation failed.
246 /// @brief Simple structure to pass data to the thread responsible for
247 /// launching a child process.
248 struct LaunchArgs : OperationArgs
250 LaunchArgs(ProcessMonitor *monitor,
251 lldb_private::Module *module,
254 const lldb_private::FileSpec &stdin_file_spec,
255 const lldb_private::FileSpec &stdout_file_spec,
256 const lldb_private::FileSpec &stderr_file_spec,
257 const lldb_private::FileSpec &working_dir);
261 lldb_private::Module *m_module; // The executable image to launch.
262 char const **m_argv; // Process arguments.
263 char const **m_envp; // Process environment.
264 const lldb_private::FileSpec m_stdin_file_spec; // Redirect stdin or empty.
265 const lldb_private::FileSpec m_stdout_file_spec; // Redirect stdout or empty.
266 const lldb_private::FileSpec m_stderr_file_spec; // Redirect stderr or empty.
267 const lldb_private::FileSpec m_working_dir; // Working directory or empty.
271 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
274 LaunchOpThread(void *arg);
277 Launch(LaunchArgs *args);
279 struct AttachArgs : OperationArgs
281 AttachArgs(ProcessMonitor *monitor,
286 lldb::pid_t m_pid; // pid of the process to be attached.
290 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
293 AttachOpThread(void *args);
296 Attach(AttachArgs *args);
299 ServeOperation(OperationArgs *args);
302 DupDescriptor(const lldb_private::FileSpec &file_spec, int fd, int flags);
305 MonitorCallback(void *callback_baton,
306 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_