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/Mutex.h"
22 namespace lldb_private
27 } // End lldb_private namespace.
32 /// @class ProcessMonitor
33 /// @brief Manages communication with the inferior (debugee) process.
35 /// Upon construction, this class prepares and launches an inferior process for
38 /// Changes in the inferior process state are propagated to the associated
39 /// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
40 /// appropriate ProcessMessage events.
42 /// A purposely minimal set of operations are provided to interrogate and change
43 /// the inferior process state.
48 /// Launches an inferior process ready for debugging. Forms the
49 /// implementation of Process::DoLaunch.
50 ProcessMonitor(ProcessPOSIX *process,
51 lldb_private::Module *module,
54 const char *stdin_path,
55 const char *stdout_path,
56 const char *stderr_path,
57 const char *working_dir,
58 lldb_private::Error &error);
60 ProcessMonitor(ProcessPOSIX *process,
62 lldb_private::Error &error);
66 /// Provides the process number of debugee.
68 GetPID() const { return m_pid; }
70 /// Returns the process associated with this ProcessMonitor.
72 GetProcess() { return *m_process; }
74 /// Returns a file descriptor to the controlling terminal of the inferior
77 /// Reads from this file descriptor yield both the standard output and
78 /// standard error of this debugee. Even if stderr and stdout were
79 /// redirected on launch it may still happen that data is available on this
80 /// descriptor (if the inferior process opens /dev/tty, for example).
82 /// If this monitor was attached to an existing process this method returns
85 GetTerminalFD() const { return m_terminal_fd; }
87 /// Reads @p size bytes from address @vm_adder in the inferior process
90 /// This method is provided to implement Process::DoReadMemory.
92 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
93 lldb_private::Error &error);
95 /// Writes @p size bytes from address @p vm_adder in the inferior process
98 /// This method is provided to implement Process::DoWriteMemory.
100 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
101 lldb_private::Error &error);
103 /// Reads the contents from the register identified by the given (architecture
104 /// dependent) offset.
106 /// This method is provided for use by RegisterContextFreeBSD derivatives.
107 /// FIXME: The FreeBSD implementation of this function should use tid in order
108 /// to enable support for debugging threaded programs.
110 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
111 unsigned size, lldb_private::RegisterValue &value);
113 /// Writes the given value to the register identified by the given
114 /// (architecture dependent) offset.
116 /// This method is provided for use by RegisterContextFreeBSD derivatives.
117 /// FIXME: The FreeBSD implementation of this function should use tid in order
118 /// to enable support for debugging threaded programs.
120 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
121 const lldb_private::RegisterValue &value);
123 /// Reads all general purpose registers into the specified buffer.
124 /// FIXME: The FreeBSD implementation of this function should use tid in order
125 /// to enable support for debugging threaded programs.
127 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
129 /// Reads all floating point registers into the specified buffer.
130 /// FIXME: The FreeBSD implementation of this function should use tid in order
131 /// to enable support for debugging threaded programs.
133 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
135 /// Reads the specified register set into the specified buffer.
137 /// This method is provided for use by RegisterContextFreeBSD derivatives.
138 /// FIXME: The FreeBSD implementation of this function should use tid in order
139 /// to enable support for debugging threaded programs.
141 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
143 /// Writes all general purpose registers into the specified buffer.
144 /// FIXME: The FreeBSD implementation of this function should use tid in order
145 /// to enable support for debugging threaded programs.
147 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
149 /// Writes all floating point registers into the specified buffer.
150 /// FIXME: The FreeBSD implementation of this function should use tid in order
151 /// to enable support for debugging threaded programs.
153 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
155 /// Writes the specified register set into the specified buffer.
157 /// This method is provided for use by RegisterContextFreeBSD derivatives.
158 /// FIXME: The FreeBSD implementation of this function should use tid in order
159 /// to enable support for debugging threaded programs.
161 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
163 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
164 /// to the memory region pointed to by @p lwpinfo.
166 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
168 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
169 /// corresponding to the given thread IDto the memory pointed to by @p
172 GetEventMessage(lldb::tid_t tid, unsigned long *message);
174 /// Resumes the given thread. If @p signo is anything but
175 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
177 Resume(lldb::tid_t tid, uint32_t signo);
179 /// Single steps the given thread. If @p signo is anything but
180 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
182 SingleStep(lldb::tid_t tid, uint32_t signo);
184 /// Sends the inferior process a PTRACE_KILL signal. The inferior will
185 /// still exists and can be interrogated. Once resumed it will exit as
186 /// though it received a SIGKILL.
188 BringProcessIntoLimbo();
191 Detach(lldb::tid_t tid);
197 ProcessFreeBSD *m_process;
199 lldb::thread_t m_operation_thread;
200 lldb::thread_t m_monitor_thread;
204 lldb_private::Mutex m_server_mutex;
211 OperationArgs(ProcessMonitor *monitor);
215 ProcessMonitor *m_monitor; // The monitor performing the attach.
216 sem_t m_semaphore; // Posted to once operation complete.
217 lldb_private::Error m_error; // Set if process operation failed.
222 /// @brief Simple structure to pass data to the thread responsible for
223 /// launching a child process.
224 struct LaunchArgs : OperationArgs
226 LaunchArgs(ProcessMonitor *monitor,
227 lldb_private::Module *module,
230 const char *stdin_path,
231 const char *stdout_path,
232 const char *stderr_path,
233 const char *working_dir);
237 lldb_private::Module *m_module; // The executable image to launch.
238 char const **m_argv; // Process arguments.
239 char const **m_envp; // Process environment.
240 const char *m_stdin_path; // Redirect stdin or NULL.
241 const char *m_stdout_path; // Redirect stdout or NULL.
242 const char *m_stderr_path; // Redirect stderr or NULL.
243 const char *m_working_dir; // Working directory or NULL.
247 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
250 LaunchOpThread(void *arg);
253 Launch(LaunchArgs *args);
258 struct AttachArgs : OperationArgs
260 AttachArgs(ProcessMonitor *monitor,
265 lldb::pid_t m_pid; // pid of the process to be attached.
269 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
272 AttachOpThread(void *args);
275 Attach(AttachArgs *args);
278 ServeOperation(OperationArgs *args);
281 DupDescriptor(const char *path, int fd, int flags);
284 MonitorCallback(void *callback_baton,
285 lldb::pid_t pid, bool exited, int signal, int status);
287 static ProcessMessage
288 MonitorSIGTRAP(ProcessMonitor *monitor,
289 const siginfo_t *info, lldb::pid_t pid);
291 static ProcessMessage
292 MonitorSignal(ProcessMonitor *monitor,
293 const siginfo_t *info, lldb::pid_t pid);
295 static ProcessMessage::CrashReason
296 GetCrashReasonForSIGSEGV(const siginfo_t *info);
298 static ProcessMessage::CrashReason
299 GetCrashReasonForSIGILL(const siginfo_t *info);
301 static ProcessMessage::CrashReason
302 GetCrashReasonForSIGFPE(const siginfo_t *info);
304 static ProcessMessage::CrashReason
305 GetCrashReasonForSIGBUS(const siginfo_t *info);
308 DoOperation(Operation *op);
310 /// Stops the child monitor thread.
312 StopMonitoringChildProcess();
314 /// Stops the operation thread used to attach/launch a process.
322 #endif // #ifndef liblldb_ProcessMonitor_H_