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 const lldb_private::ProcessLaunchInfo &launch_info,
59 lldb_private::Error &error);
61 ProcessMonitor(ProcessPOSIX *process,
63 lldb_private::Error &error);
67 /// Provides the process number of debugee.
69 GetPID() const { return m_pid; }
71 /// Returns the process associated with this ProcessMonitor.
73 GetProcess() { return *m_process; }
75 /// Returns a file descriptor to the controlling terminal of the inferior
78 /// Reads from this file descriptor yield both the standard output and
79 /// standard error of this debugee. Even if stderr and stdout were
80 /// redirected on launch it may still happen that data is available on this
81 /// descriptor (if the inferior process opens /dev/tty, for example).
83 /// If this monitor was attached to an existing process this method returns
86 GetTerminalFD() const { return m_terminal_fd; }
88 /// Reads @p size bytes from address @vm_adder in the inferior process
91 /// This method is provided to implement Process::DoReadMemory.
93 ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
94 lldb_private::Error &error);
96 /// Writes @p size bytes from address @p vm_adder in the inferior process
99 /// This method is provided to implement Process::DoWriteMemory.
101 WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
102 lldb_private::Error &error);
104 /// Reads the contents from the register identified by the given (architecture
105 /// dependent) offset.
107 /// This method is provided for use by RegisterContextFreeBSD derivatives.
109 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
110 unsigned size, lldb_private::RegisterValue &value);
112 /// Writes the given value to the register identified by the given
113 /// (architecture dependent) offset.
115 /// This method is provided for use by RegisterContextFreeBSD derivatives.
117 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
118 const lldb_private::RegisterValue &value);
120 /// Reads the contents from the debug register identified by the given
121 /// (architecture dependent) offset.
123 /// This method is provided for use by RegisterContextFreeBSD derivatives.
125 ReadDebugRegisterValue(lldb::tid_t tid, unsigned offset,
126 const char *reg_name, unsigned size,
127 lldb_private::RegisterValue &value);
129 /// Writes the given value to the debug register identified by the given
130 /// (architecture dependent) offset.
132 /// This method is provided for use by RegisterContextFreeBSD derivatives.
134 WriteDebugRegisterValue(lldb::tid_t tid, unsigned offset,
135 const char *reg_name,
136 const lldb_private::RegisterValue &value);
137 /// Reads all general purpose registers into the specified buffer.
139 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
141 /// Reads all floating point registers into the specified buffer.
143 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
145 /// Reads the specified register set into the specified buffer.
147 /// This method is provided for use by RegisterContextFreeBSD derivatives.
149 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
151 /// Writes all general purpose registers into the specified buffer.
153 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
155 /// Writes all floating point registers into the specified buffer.
157 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
159 /// Writes the specified register set into the specified buffer.
161 /// This method is provided for use by RegisterContextFreeBSD derivatives.
163 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
165 /// Reads the value of the thread-specific pointer for a given thread ID.
167 ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
169 /// Returns current thread IDs in process
171 GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids);
173 /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
174 /// to the memory region pointed to by @p lwpinfo.
176 GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
178 /// Suspends or unsuspends a thread prior to process resume or step.
180 ThreadSuspend(lldb::tid_t tid, bool suspend);
182 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
183 /// corresponding to the given thread IDto the memory pointed to by @p
186 GetEventMessage(lldb::tid_t tid, unsigned long *message);
188 /// Resumes the process. If @p signo is anything but
189 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
191 Resume(lldb::tid_t unused, uint32_t signo);
193 /// Single steps the process. If @p signo is anything but
194 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
196 SingleStep(lldb::tid_t unused, uint32_t signo);
198 /// Terminate the traced process.
203 Detach(lldb::tid_t tid);
208 // Waits for the initial stop message from a new thread.
210 WaitForInitialTIDStop(lldb::tid_t tid);
213 ProcessFreeBSD *m_process;
215 lldb::thread_t m_operation_thread;
216 lldb::thread_t m_monitor_thread;
221 // current operation which must be executed on the privileged thread
222 Operation *m_operation;
223 lldb_private::Mutex m_operation_mutex;
225 // semaphores notified when Operation is ready to be processed and when
226 // the operation is complete.
227 sem_t m_operation_pending;
228 sem_t m_operation_done;
232 OperationArgs(ProcessMonitor *monitor);
236 ProcessMonitor *m_monitor; // The monitor performing the attach.
237 sem_t m_semaphore; // Posted to once operation complete.
238 lldb_private::Error m_error; // Set if process operation failed.
243 /// @brief Simple structure to pass data to the thread responsible for
244 /// launching a child process.
245 struct LaunchArgs : OperationArgs
247 LaunchArgs(ProcessMonitor *monitor,
248 lldb_private::Module *module,
251 const char *stdin_path,
252 const char *stdout_path,
253 const char *stderr_path,
254 const char *working_dir);
258 lldb_private::Module *m_module; // The executable image to launch.
259 char const **m_argv; // Process arguments.
260 char const **m_envp; // Process environment.
261 const char *m_stdin_path; // Redirect stdin or NULL.
262 const char *m_stdout_path; // Redirect stdout or NULL.
263 const char *m_stderr_path; // Redirect stderr or NULL.
264 const char *m_working_dir; // Working directory or NULL.
268 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
271 LaunchOpThread(void *arg);
274 Launch(LaunchArgs *args);
276 struct AttachArgs : OperationArgs
278 AttachArgs(ProcessMonitor *monitor,
283 lldb::pid_t m_pid; // pid of the process to be attached.
287 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
290 AttachOpThread(void *args);
293 Attach(AttachArgs *args);
296 ServeOperation(OperationArgs *args);
299 DupDescriptor(const char *path, int fd, int flags);
302 MonitorCallback(void *callback_baton,
303 lldb::pid_t pid, bool exited, int signal, int status);
305 static ProcessMessage
306 MonitorSIGTRAP(ProcessMonitor *monitor,
307 const siginfo_t *info, lldb::pid_t pid);
309 static ProcessMessage
310 MonitorSignal(ProcessMonitor *monitor,
311 const siginfo_t *info, lldb::pid_t pid);
313 static ProcessMessage::CrashReason
314 GetCrashReasonForSIGSEGV(const siginfo_t *info);
316 static ProcessMessage::CrashReason
317 GetCrashReasonForSIGILL(const siginfo_t *info);
319 static ProcessMessage::CrashReason
320 GetCrashReasonForSIGFPE(const siginfo_t *info);
322 static ProcessMessage::CrashReason
323 GetCrashReasonForSIGBUS(const siginfo_t *info);
326 DoOperation(Operation *op);
328 /// Stops the child monitor thread.
330 StopMonitoringChildProcess();
332 /// Stops the operation thread used to attach/launch a process.
337 #endif // #ifndef liblldb_ProcessMonitor_H_