ProcessMonitor.h revision 263367
140496Sbde//===-- ProcessMonitor.h -------------------------------------- -*- C++ -*-===//
250473Speter//
31539Srgrimes//                     The LLVM Compiler Infrastructure
4114731Sbde//
51539Srgrimes// This file is distributed under the University of Illinois Open Source
6156813Sru// License. See LICENSE.TXT for details.
7156813Sru//
818420Sbde//===----------------------------------------------------------------------===//
9267666Stijl
10133567Stjr#ifndef liblldb_ProcessMonitor_H_
11133559Stjr#define liblldb_ProcessMonitor_H_
12107046Smarcel
13267666Stijl// C Includes
14220370Sobrien#include <semaphore.h>
15107046Smarcel#include <signal.h>
16157236Sjasone
17152994Sru// C++ Includes
18166243Speter// Other libraries and framework includes
19153486Sphk#include "lldb/lldb-types.h"
20205146Sed#include "lldb/Host/Mutex.h"
21201546Sdavidxu
22251230Sednamespace lldb_private
23228879Sed{
24199898Sedclass Error;
25133333Sstefanfclass Module;
26250883Sedclass Scalar;
27232498Stheraven} // End lldb_private namespace.
281539Srgrimes
29244401Sbrooksclass ProcessFreeBSD;
30244401Sbrooksclass Operation;
31244401Sbrooks
32119630Skan/// @class ProcessMonitor
3334030Sdufault/// @brief Manages communication with the inferior (debugee) process.
34201546Sdavidxu///
3534030Sdufault/// Upon construction, this class prepares and launches an inferior process for
36251230Sed/// debugging.
37251230Sed///
381539Srgrimes/// Changes in the inferior process state are propagated to the associated
39171453Srwatson/// ProcessFreeBSD instance by calling ProcessFreeBSD::SendMessage with the
40252356Sdavide/// appropriate ProcessMessage events.
41156905Sru///
42188642Snyan/// A purposely minimal set of operations are provided to interrogate and change
4317900Speter/// the inferior process state.
44195534Sscottlclass ProcessMonitor
45246367Sjhb{
46246367Sjhbpublic:
47240621Sjimharris
48246367Sjhb    /// Launches an inferior process ready for debugging.  Forms the
49280419Sngie    /// implementation of Process::DoLaunch.
50241636Sattilio    ProcessMonitor(ProcessPOSIX *process,
51252356Sdavide                   lldb_private::Module *module,
52163851Spjd                   char const *argv[],
53202437Strasz                   char const *envp[],
54219974Smav                   const char *stdin_path,
55135339Sglebius                   const char *stdout_path,
56156885Srwatson                   const char *stderr_path,
57135339Sglebius                   const char *working_dir,
58135339Sglebius                   lldb_private::Error &error);
59137556Smarkm
6077031Sru    ProcessMonitor(ProcessPOSIX *process,
61192901Sthompsa                   lldb::pid_t pid,
62178818Sjhb                   lldb_private::Error &error);
63280427Sngie
64280427Sngie    ~ProcessMonitor();
65160892Ssobomax
66160892Ssobomax    /// Provides the process number of debugee.
67156813Sru    lldb::pid_t
68141397Sphk    GetPID() const { return m_pid; }
69141397Sphk
70141397Sphk    /// Returns the process associated with this ProcessMonitor.
71267666Stijl    ProcessFreeBSD &
72267666Stijl    GetProcess() { return *m_process; }
73267666Stijl
74267666Stijl    /// Returns a file descriptor to the controlling terminal of the inferior
75267666Stijl    /// process.
76156813Sru    ///
77148796Sphk    /// Reads from this file descriptor yield both the standard output and
78148796Sphk    /// standard error of this debugee.  Even if stderr and stdout were
79148796Sphk    /// redirected on launch it may still happen that data is available on this
80252356Sdavide    /// descriptor (if the inferior process opens /dev/tty, for example).
81156905Sru    ///
82156905Sru    /// If this monitor was attached to an existing process this method returns
83156905Sru    /// -1.
84156905Sru    int
85254273Speter    GetTerminalFD() const { return m_terminal_fd; }
86254273Speter
87258230Sgjb    /// Reads @p size bytes from address @vm_adder in the inferior process
88254273Speter    /// address space.
89254273Speter    ///
90280427Sngie    /// This method is provided to implement Process::DoReadMemory.
91280427Sngie    size_t
92280427Sngie    ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
93280427Sngie               lldb_private::Error &error);
94280427Sngie
95280427Sngie    /// Writes @p size bytes from address @p vm_adder in the inferior process
96280427Sngie    /// address space.
97280427Sngie    ///
9854351Smarcel    /// This method is provided to implement Process::DoWriteMemory.
9954351Smarcel    size_t
10054351Smarcel    WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
10154351Smarcel                lldb_private::Error &error);
10254351Smarcel
10354351Smarcel    /// Reads the contents from the register identified by the given (architecture
10454351Smarcel    /// dependent) offset.
10596462Sru    ///
10625734Speter    /// This method is provided for use by RegisterContextFreeBSD derivatives.
107255930Sian    /// FIXME: The FreeBSD implementation of this function should use tid in order
108255930Sian    ///        to enable support for debugging threaded programs.
109255930Sian    bool
110255775Sian    ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
11125734Speter                      unsigned size, lldb_private::RegisterValue &value);
112255775Sian
113255775Sian    /// Writes the given value to the register identified by the given
114255775Sian    /// (architecture dependent) offset.
115255775Sian    ///
116255775Sian    /// This method is provided for use by RegisterContextFreeBSD derivatives.
117255930Sian    /// FIXME: The FreeBSD implementation of this function should use tid in order
118255807Sian    ///        to enable support for debugging threaded programs.
119255775Sian    bool
12088055Sru    WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
12196462Sru                       const lldb_private::RegisterValue &value);
12217900Speter
12388055Sru    /// Reads all general purpose registers into the specified buffer.
12496462Sru    /// FIXME: The FreeBSD implementation of this function should use tid in order
12517900Speter    ///        to enable support for debugging threaded programs.
12688055Sru    bool
127164184Strhodes    ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
12834030Sdufault
12954351Smarcel    /// Reads all floating point registers into the specified buffer.
130211725Simp    /// FIXME: The FreeBSD implementation of this function should use tid in order
131214629Sjhb    ///        to enable support for debugging threaded programs.
132144514Simp    bool
133214629Sjhb    ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
134214629Sjhb
135214629Sjhb    /// Reads the specified register set into the specified buffer.
136144514Simp    ///
137114731Sbde    /// This method is provided for use by RegisterContextFreeBSD derivatives.
138114731Sbde    /// FIXME: The FreeBSD implementation of this function should use tid in order
139114731Sbde    ///        to enable support for debugging threaded programs.
140114731Sbde    bool
141114731Sbde    ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
142114731Sbde
143114731Sbde    /// Writes all general purpose registers into the specified buffer.
144214629Sjhb    /// FIXME: The FreeBSD implementation of this function should use tid in order
145114731Sbde    ///        to enable support for debugging threaded programs.
146114731Sbde    bool
14754351Smarcel    WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
14854351Smarcel
149114731Sbde    /// Writes all floating point registers into the specified buffer.
150114731Sbde    /// FIXME: The FreeBSD implementation of this function should use tid in order
151114731Sbde    ///        to enable support for debugging threaded programs.
152114731Sbde    bool
153114731Sbde    WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
154144514Simp
155214629Sjhb    /// Writes the specified register set into the specified buffer.
156114731Sbde    ///
157114731Sbde    /// This method is provided for use by RegisterContextFreeBSD derivatives.
158114731Sbde    /// FIXME: The FreeBSD implementation of this function should use tid in order
159114731Sbde    ///        to enable support for debugging threaded programs.
160114731Sbde    bool
161114731Sbde    WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
162114731Sbde
163246367Sjhb    /// Reads the value of the thread-specific pointer for a given thread ID.
16454351Smarcel    bool
165114731Sbde    ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value);
166114731Sbde
16754351Smarcel    /// Writes a ptrace_lwpinfo structure corresponding to the given thread ID
168143013Snjl    /// to the memory region pointed to by @p lwpinfo.
169143013Snjl    bool
170274001Srpaulo    GetLwpInfo(lldb::tid_t tid, void *lwpinfo, int &error_no);
171274001Srpaulo
172143013Snjl    /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
173246367Sjhb    /// corresponding to the given thread IDto the memory pointed to by @p
174246367Sjhb    /// message.
175246367Sjhb    bool
176123288Sobrien    GetEventMessage(lldb::tid_t tid, unsigned long *message);
177123288Sobrien
178123288Sobrien    /// Resumes the process.  If @p signo is anything but
179235537Sgber    /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
180235537Sgber    bool
181235537Sgber    Resume(lldb::tid_t unused, uint32_t signo);
182235537Sgber
183235537Sgber    /// Single steps the process.  If @p signo is anything but
184235537Sgber    /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the process.
185235537Sgber    bool
186246367Sjhb    SingleStep(lldb::tid_t unused, uint32_t signo);
187246367Sjhb
188246367Sjhb    /// Sends the inferior process a PTRACE_KILL signal.  The inferior will
189130416Smlaier    /// still exists and can be interrogated.  Once resumed it will exit as
190130416Smlaier    /// though it received a SIGKILL.
191130416Smlaier    bool
192166640Srodrigc    BringProcessIntoLimbo();
193166640Srodrigc
194166640Srodrigc    lldb_private::Error
195156813Sru    Detach(lldb::tid_t tid);
196116734Sru
197116734Sru    void
198116734Sru    StopMonitor();
199145539Sscottl
200263370Sglebius    // Waits for the initial stop message from a new thread.
201263370Sglebius    bool
202263370Sglebius    WaitForInitialTIDStop(lldb::tid_t tid);
203263370Sglebius
204263370Sglebiusprivate:
205143423Sume    ProcessFreeBSD *m_process;
206143423Sume
207143423Sume    lldb::thread_t m_operation_thread;
208116734Sru    lldb::thread_t m_monitor_thread;
209116734Sru    lldb::pid_t m_pid;
210114731Sbde
211144514Simp    int m_terminal_fd;
212114731Sbde
213114731Sbde    // current operation which must be executed on the privileged thread
214144514Simp    Operation *m_operation;
215144514Simp    lldb_private::Mutex m_operation_mutex;
216114731Sbde
217114731Sbde    // semaphores notified when Operation is ready to be processed and when
21877857Sjlemon    // the operation is complete.
219214629Sjhb    sem_t m_operation_pending;
220214629Sjhb    sem_t m_operation_done;
221144561Simp
222144561Simp    struct OperationArgs
223144514Simp    {
224144514Simp        OperationArgs(ProcessMonitor *monitor);
225144514Simp
226144514Simp        ~OperationArgs();
227144561Simp
228144561Simp        ProcessMonitor *m_monitor;      // The monitor performing the attach.
229144514Simp        sem_t m_semaphore;              // Posted to once operation complete.
230144514Simp        lldb_private::Error m_error;    // Set if process operation failed.
231144514Simp    };
232144514Simp
233144514Simp    /// @class LauchArgs
234214629Sjhb    ///
235168677Spjd    /// @brief Simple structure to pass data to the thread responsible for
236168677Spjd    /// launching a child process.
237168677Spjd    struct LaunchArgs : OperationArgs
2381539Srgrimes    {
23954351Smarcel        LaunchArgs(ProcessMonitor *monitor,
24054351Smarcel                   lldb_private::Module *module,
24156645Speter                   char const **argv,
242114731Sbde                   char const **envp,
243114731Sbde                   const char *stdin_path,
244114731Sbde                   const char *stdout_path,
245114731Sbde                   const char *stderr_path,
24654351Smarcel                   const char *working_dir);
247246367Sjhb
248114731Sbde        ~LaunchArgs();
249114731Sbde
250114731Sbde        lldb_private::Module *m_module; // The executable image to launch.
251114731Sbde        char const **m_argv;            // Process arguments.
25277046Sru        char const **m_envp;            // Process environment.
253143013Snjl        const char *m_stdin_path;       // Redirect stdin or NULL.
254274001Srpaulo        const char *m_stdout_path;      // Redirect stdout or NULL.
255143013Snjl        const char *m_stderr_path;      // Redirect stderr or NULL.
256143013Snjl        const char *m_working_dir;      // Working directory or NULL.
257143013Snjl    };
258246367Sjhb
259246367Sjhb    void
260246367Sjhb    StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
261246367Sjhb
262246367Sjhb    static void *
263142992Sru    LaunchOpThread(void *arg);
264142992Sru
265142992Sru    static bool
266142992Sru    Launch(LaunchArgs *args);
267142992Sru
268235537Sgber    struct AttachArgs : OperationArgs
269235537Sgber    {
270235537Sgber        AttachArgs(ProcessMonitor *monitor,
271235537Sgber                   lldb::pid_t pid);
272235537Sgber
273235537Sgber        ~AttachArgs();
274235537Sgber
275246367Sjhb        lldb::pid_t m_pid;              // pid of the process to be attached.
276246367Sjhb    };
277246367Sjhb
278246367Sjhb    void
279246367Sjhb    StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
280114731Sbde
281114731Sbde    static void *
282114731Sbde    AttachOpThread(void *args);
283114731Sbde
284114731Sbde    static bool
285114731Sbde    Attach(AttachArgs *args);
286130416Smlaier
287130416Smlaier    static void
288130416Smlaier    ServeOperation(OperationArgs *args);
289130416Smlaier
290130416Smlaier    static bool
291156813Sru    DupDescriptor(const char *path, int fd, int flags);
292116734Sru
293116734Sru    static bool
294116734Sru    MonitorCallback(void *callback_baton,
295116734Sru                    lldb::pid_t pid, bool exited, int signal, int status);
296116734Sru
297145539Sscottl    static ProcessMessage
298263086Sglebius    MonitorSIGTRAP(ProcessMonitor *monitor,
299263086Sglebius                   const siginfo_t *info, lldb::pid_t pid);
300263086Sglebius
301263086Sglebius    static ProcessMessage
302263086Sglebius    MonitorSignal(ProcessMonitor *monitor,
303263086Sglebius                  const siginfo_t *info, lldb::pid_t pid);
304263086Sglebius
305143423Sume    static ProcessMessage::CrashReason
306143423Sume    GetCrashReasonForSIGSEGV(const siginfo_t *info);
307143423Sume
308143423Sume    static ProcessMessage::CrashReason
309143423Sume    GetCrashReasonForSIGILL(const siginfo_t *info);
310114731Sbde
311114731Sbde    static ProcessMessage::CrashReason
312114731Sbde    GetCrashReasonForSIGFPE(const siginfo_t *info);
313114731Sbde
314114731Sbde    static ProcessMessage::CrashReason
315144514Simp    GetCrashReasonForSIGBUS(const siginfo_t *info);
316114731Sbde
317144514Simp    void
318114731Sbde    DoOperation(Operation *op);
319114731Sbde
320144514Simp    /// Stops the child monitor thread.
321144514Simp    void
322114731Sbde    StopMonitoringChildProcess();
323144514Simp
324114731Sbde    /// Stops the operation thread used to attach/launch a process.
325114731Sbde    void
326114731Sbde    StopOpThread();
327214629Sjhb};
328214629Sjhb
329144561Simp#endif // #ifndef liblldb_ProcessMonitor_H_
330144561Simp