1*af245d11STodd Fiala //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
2*af245d11STodd Fiala //
3*af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4*af245d11STodd Fiala //
5*af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6*af245d11STodd Fiala // License. See LICENSE.TXT for details.
7*af245d11STodd Fiala //
8*af245d11STodd Fiala //===----------------------------------------------------------------------===//
9*af245d11STodd Fiala 
10*af245d11STodd Fiala #ifndef liblldb_NativeProcessLinux_H_
11*af245d11STodd Fiala #define liblldb_NativeProcessLinux_H_
12*af245d11STodd Fiala 
13*af245d11STodd Fiala // C Includes
14*af245d11STodd Fiala #include <semaphore.h>
15*af245d11STodd Fiala #include <signal.h>
16*af245d11STodd Fiala 
17*af245d11STodd Fiala // C++ Includes
18*af245d11STodd Fiala #include <unordered_set>
19*af245d11STodd Fiala 
20*af245d11STodd Fiala // Other libraries and framework includes
21*af245d11STodd Fiala #include "lldb/Core/ArchSpec.h"
22*af245d11STodd Fiala #include "lldb/lldb-types.h"
23*af245d11STodd Fiala #include "lldb/Host/Debug.h"
24*af245d11STodd Fiala #include "lldb/Host/Mutex.h"
25*af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h"
26*af245d11STodd Fiala 
27*af245d11STodd Fiala #include "Host/common/NativeProcessProtocol.h"
28*af245d11STodd Fiala 
29*af245d11STodd Fiala namespace lldb_private
30*af245d11STodd Fiala {
31*af245d11STodd Fiala     class Error;
32*af245d11STodd Fiala     class Module;
33*af245d11STodd Fiala     class Scalar;
34*af245d11STodd Fiala 
35*af245d11STodd Fiala     /// @class NativeProcessLinux
36*af245d11STodd Fiala     /// @brief Manages communication with the inferior (debugee) process.
37*af245d11STodd Fiala     ///
38*af245d11STodd Fiala     /// Upon construction, this class prepares and launches an inferior process for
39*af245d11STodd Fiala     /// debugging.
40*af245d11STodd Fiala     ///
41*af245d11STodd Fiala     /// Changes in the inferior process state are broadcasted.
42*af245d11STodd Fiala     class NativeProcessLinux: public NativeProcessProtocol
43*af245d11STodd Fiala     {
44*af245d11STodd Fiala     public:
45*af245d11STodd Fiala 
46*af245d11STodd Fiala         // ---------------------------------------------------------------------
47*af245d11STodd Fiala         // Public Static Methods
48*af245d11STodd Fiala         // ---------------------------------------------------------------------
49*af245d11STodd Fiala         static lldb_private::Error
50*af245d11STodd Fiala         LaunchProcess (
51*af245d11STodd Fiala             Module *exe_module,
52*af245d11STodd Fiala             ProcessLaunchInfo &launch_info,
53*af245d11STodd Fiala             lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
54*af245d11STodd Fiala             NativeProcessProtocolSP &native_process_sp);
55*af245d11STodd Fiala 
56*af245d11STodd Fiala         static lldb_private::Error
57*af245d11STodd Fiala         AttachToProcess (
58*af245d11STodd Fiala             lldb::pid_t pid,
59*af245d11STodd Fiala             lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
60*af245d11STodd Fiala             NativeProcessProtocolSP &native_process_sp);
61*af245d11STodd Fiala 
62*af245d11STodd Fiala         // ---------------------------------------------------------------------
63*af245d11STodd Fiala         // Public Instance Methods
64*af245d11STodd Fiala         // ---------------------------------------------------------------------
65*af245d11STodd Fiala 
66*af245d11STodd Fiala         ~NativeProcessLinux() override;
67*af245d11STodd Fiala 
68*af245d11STodd Fiala         // ---------------------------------------------------------------------
69*af245d11STodd Fiala         // NativeProcessProtocol Interface
70*af245d11STodd Fiala         // ---------------------------------------------------------------------
71*af245d11STodd Fiala         Error
72*af245d11STodd Fiala         Resume (const ResumeActionList &resume_actions) override;
73*af245d11STodd Fiala 
74*af245d11STodd Fiala         Error
75*af245d11STodd Fiala         Halt () override;
76*af245d11STodd Fiala 
77*af245d11STodd Fiala         Error
78*af245d11STodd Fiala         Detach () override;
79*af245d11STodd Fiala 
80*af245d11STodd Fiala         Error
81*af245d11STodd Fiala         Signal (int signo) override;
82*af245d11STodd Fiala 
83*af245d11STodd Fiala         Error
84*af245d11STodd Fiala         Kill () override;
85*af245d11STodd Fiala 
86*af245d11STodd Fiala         Error
87*af245d11STodd Fiala         GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
88*af245d11STodd Fiala 
89*af245d11STodd Fiala         Error
90*af245d11STodd Fiala         ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
91*af245d11STodd Fiala 
92*af245d11STodd Fiala         Error
93*af245d11STodd Fiala         WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
94*af245d11STodd Fiala 
95*af245d11STodd Fiala         Error
96*af245d11STodd Fiala         AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
97*af245d11STodd Fiala 
98*af245d11STodd Fiala         Error
99*af245d11STodd Fiala         DeallocateMemory (lldb::addr_t addr) override;
100*af245d11STodd Fiala 
101*af245d11STodd Fiala         lldb::addr_t
102*af245d11STodd Fiala         GetSharedLibraryInfoAddress () override;
103*af245d11STodd Fiala 
104*af245d11STodd Fiala         size_t
105*af245d11STodd Fiala         UpdateThreads () override;
106*af245d11STodd Fiala 
107*af245d11STodd Fiala         bool
108*af245d11STodd Fiala         GetArchitecture (ArchSpec &arch) const override;
109*af245d11STodd Fiala 
110*af245d11STodd Fiala         Error
111*af245d11STodd Fiala         SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
112*af245d11STodd Fiala 
113*af245d11STodd Fiala         void
114*af245d11STodd Fiala         DoStopIDBumped (uint32_t newBumpId) override;
115*af245d11STodd Fiala 
116*af245d11STodd Fiala         // ---------------------------------------------------------------------
117*af245d11STodd Fiala         // Interface used by NativeRegisterContext-derived classes.
118*af245d11STodd Fiala         // ---------------------------------------------------------------------
119*af245d11STodd Fiala 
120*af245d11STodd Fiala         /// Reads the contents from the register identified by the given (architecture
121*af245d11STodd Fiala         /// dependent) offset.
122*af245d11STodd Fiala         ///
123*af245d11STodd Fiala         /// This method is provided for use by RegisterContextLinux derivatives.
124*af245d11STodd Fiala         bool
125*af245d11STodd Fiala         ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
126*af245d11STodd Fiala                           unsigned size, lldb_private::RegisterValue &value);
127*af245d11STodd Fiala 
128*af245d11STodd Fiala         /// Writes the given value to the register identified by the given
129*af245d11STodd Fiala         /// (architecture dependent) offset.
130*af245d11STodd Fiala         ///
131*af245d11STodd Fiala         /// This method is provided for use by RegisterContextLinux derivatives.
132*af245d11STodd Fiala         bool
133*af245d11STodd Fiala         WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
134*af245d11STodd Fiala                            const lldb_private::RegisterValue &value);
135*af245d11STodd Fiala 
136*af245d11STodd Fiala         /// Reads all general purpose registers into the specified buffer.
137*af245d11STodd Fiala         bool
138*af245d11STodd Fiala         ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
139*af245d11STodd Fiala 
140*af245d11STodd Fiala         /// Reads generic floating point registers into the specified buffer.
141*af245d11STodd Fiala         bool
142*af245d11STodd Fiala         ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
143*af245d11STodd Fiala 
144*af245d11STodd Fiala         /// Reads the specified register set into the specified buffer.
145*af245d11STodd Fiala         /// For instance, the extended floating-point register set.
146*af245d11STodd Fiala         bool
147*af245d11STodd Fiala         ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
148*af245d11STodd Fiala 
149*af245d11STodd Fiala         /// Writes all general purpose registers into the specified buffer.
150*af245d11STodd Fiala         bool
151*af245d11STodd Fiala         WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
152*af245d11STodd Fiala 
153*af245d11STodd Fiala         /// Writes generic floating point registers into the specified buffer.
154*af245d11STodd Fiala         bool
155*af245d11STodd Fiala         WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
156*af245d11STodd Fiala 
157*af245d11STodd Fiala         /// Writes the specified register set into the specified buffer.
158*af245d11STodd Fiala         /// For instance, the extended floating-point register set.
159*af245d11STodd Fiala         bool
160*af245d11STodd Fiala         WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
161*af245d11STodd Fiala 
162*af245d11STodd Fiala     protected:
163*af245d11STodd Fiala         // ---------------------------------------------------------------------
164*af245d11STodd Fiala         // NativeProcessProtocol protected interface
165*af245d11STodd Fiala         // ---------------------------------------------------------------------
166*af245d11STodd Fiala         Error
167*af245d11STodd Fiala         GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
168*af245d11STodd Fiala 
169*af245d11STodd Fiala     private:
170*af245d11STodd Fiala 
171*af245d11STodd Fiala         lldb_private::ArchSpec m_arch;
172*af245d11STodd Fiala 
173*af245d11STodd Fiala         lldb::thread_t m_operation_thread;
174*af245d11STodd Fiala         lldb::thread_t m_monitor_thread;
175*af245d11STodd Fiala 
176*af245d11STodd Fiala         // current operation which must be executed on the priviliged thread
177*af245d11STodd Fiala         void *m_operation;
178*af245d11STodd Fiala         lldb_private::Mutex m_operation_mutex;
179*af245d11STodd Fiala 
180*af245d11STodd Fiala         // semaphores notified when Operation is ready to be processed and when
181*af245d11STodd Fiala         // the operation is complete.
182*af245d11STodd Fiala         sem_t m_operation_pending;
183*af245d11STodd Fiala         sem_t m_operation_done;
184*af245d11STodd Fiala 
185*af245d11STodd Fiala         // Set of tids we're waiting to stop before we notify the delegate of
186*af245d11STodd Fiala         // the stopped state.  We only notify the delegate after all threads
187*af245d11STodd Fiala         // ordered to stop have signaled their stop.
188*af245d11STodd Fiala         std::unordered_set<lldb::tid_t> m_wait_for_stop_tids;
189*af245d11STodd Fiala         lldb_private::Mutex m_wait_for_stop_tids_mutex;
190*af245d11STodd Fiala 
191*af245d11STodd Fiala         lldb_private::LazyBool m_supports_mem_region;
192*af245d11STodd Fiala         std::vector<MemoryRegionInfo> m_mem_region_cache;
193*af245d11STodd Fiala         lldb_private::Mutex m_mem_region_cache_mutex;
194*af245d11STodd Fiala 
195*af245d11STodd Fiala 
196*af245d11STodd Fiala         struct OperationArgs
197*af245d11STodd Fiala         {
198*af245d11STodd Fiala             OperationArgs(NativeProcessLinux *monitor);
199*af245d11STodd Fiala 
200*af245d11STodd Fiala             ~OperationArgs();
201*af245d11STodd Fiala 
202*af245d11STodd Fiala             NativeProcessLinux *m_monitor;      // The monitor performing the attach.
203*af245d11STodd Fiala             sem_t m_semaphore;              // Posted to once operation complete.
204*af245d11STodd Fiala             lldb_private::Error m_error;    // Set if process operation failed.
205*af245d11STodd Fiala         };
206*af245d11STodd Fiala 
207*af245d11STodd Fiala         /// @class LauchArgs
208*af245d11STodd Fiala         ///
209*af245d11STodd Fiala         /// @brief Simple structure to pass data to the thread responsible for
210*af245d11STodd Fiala         /// launching a child process.
211*af245d11STodd Fiala         struct LaunchArgs : OperationArgs
212*af245d11STodd Fiala         {
213*af245d11STodd Fiala             LaunchArgs(NativeProcessLinux *monitor,
214*af245d11STodd Fiala                     lldb_private::Module *module,
215*af245d11STodd Fiala                     char const **argv,
216*af245d11STodd Fiala                     char const **envp,
217*af245d11STodd Fiala                     const char *stdin_path,
218*af245d11STodd Fiala                     const char *stdout_path,
219*af245d11STodd Fiala                     const char *stderr_path,
220*af245d11STodd Fiala                     const char *working_dir);
221*af245d11STodd Fiala 
222*af245d11STodd Fiala             ~LaunchArgs();
223*af245d11STodd Fiala 
224*af245d11STodd Fiala             lldb_private::Module *m_module; // The executable image to launch.
225*af245d11STodd Fiala             char const **m_argv;            // Process arguments.
226*af245d11STodd Fiala             char const **m_envp;            // Process environment.
227*af245d11STodd Fiala             const char *m_stdin_path;       // Redirect stdin or NULL.
228*af245d11STodd Fiala             const char *m_stdout_path;      // Redirect stdout or NULL.
229*af245d11STodd Fiala             const char *m_stderr_path;      // Redirect stderr or NULL.
230*af245d11STodd Fiala             const char *m_working_dir;      // Working directory or NULL.
231*af245d11STodd Fiala         };
232*af245d11STodd Fiala 
233*af245d11STodd Fiala         struct AttachArgs : OperationArgs
234*af245d11STodd Fiala         {
235*af245d11STodd Fiala             AttachArgs(NativeProcessLinux *monitor,
236*af245d11STodd Fiala                        lldb::pid_t pid);
237*af245d11STodd Fiala 
238*af245d11STodd Fiala             ~AttachArgs();
239*af245d11STodd Fiala 
240*af245d11STodd Fiala             lldb::pid_t m_pid;              // pid of the process to be attached.
241*af245d11STodd Fiala         };
242*af245d11STodd Fiala 
243*af245d11STodd Fiala         // ---------------------------------------------------------------------
244*af245d11STodd Fiala         // Private Instance Methods
245*af245d11STodd Fiala         // ---------------------------------------------------------------------
246*af245d11STodd Fiala         NativeProcessLinux ();
247*af245d11STodd Fiala 
248*af245d11STodd Fiala         /// Launches an inferior process ready for debugging.  Forms the
249*af245d11STodd Fiala         /// implementation of Process::DoLaunch.
250*af245d11STodd Fiala         void
251*af245d11STodd Fiala         LaunchInferior (
252*af245d11STodd Fiala             Module *module,
253*af245d11STodd Fiala             char const *argv[],
254*af245d11STodd Fiala             char const *envp[],
255*af245d11STodd Fiala             const char *stdin_path,
256*af245d11STodd Fiala             const char *stdout_path,
257*af245d11STodd Fiala             const char *stderr_path,
258*af245d11STodd Fiala             const char *working_dir,
259*af245d11STodd Fiala             Error &error);
260*af245d11STodd Fiala 
261*af245d11STodd Fiala         /// Attaches to an existing process.  Forms the
262*af245d11STodd Fiala         /// implementation of Process::DoLaunch.
263*af245d11STodd Fiala         void
264*af245d11STodd Fiala         AttachToInferior (lldb::pid_t pid, Error &error);
265*af245d11STodd Fiala 
266*af245d11STodd Fiala         void
267*af245d11STodd Fiala         StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
268*af245d11STodd Fiala 
269*af245d11STodd Fiala         static void *
270*af245d11STodd Fiala         LaunchOpThread(void *arg);
271*af245d11STodd Fiala 
272*af245d11STodd Fiala         static bool
273*af245d11STodd Fiala         Launch(LaunchArgs *args);
274*af245d11STodd Fiala 
275*af245d11STodd Fiala         void
276*af245d11STodd Fiala         StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
277*af245d11STodd Fiala 
278*af245d11STodd Fiala         static void *
279*af245d11STodd Fiala         AttachOpThread(void *args);
280*af245d11STodd Fiala 
281*af245d11STodd Fiala         static bool
282*af245d11STodd Fiala         Attach(AttachArgs *args);
283*af245d11STodd Fiala 
284*af245d11STodd Fiala         static bool
285*af245d11STodd Fiala         SetDefaultPtraceOpts(const lldb::pid_t);
286*af245d11STodd Fiala 
287*af245d11STodd Fiala         static void
288*af245d11STodd Fiala         ServeOperation(OperationArgs *args);
289*af245d11STodd Fiala 
290*af245d11STodd Fiala         static bool
291*af245d11STodd Fiala         DupDescriptor(const char *path, int fd, int flags);
292*af245d11STodd Fiala 
293*af245d11STodd Fiala         static bool
294*af245d11STodd Fiala         MonitorCallback(void *callback_baton,
295*af245d11STodd Fiala                 lldb::pid_t pid, bool exited, int signal, int status);
296*af245d11STodd Fiala 
297*af245d11STodd Fiala         void
298*af245d11STodd Fiala         MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
299*af245d11STodd Fiala 
300*af245d11STodd Fiala         void
301*af245d11STodd Fiala         MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
302*af245d11STodd Fiala 
303*af245d11STodd Fiala #if 0
304*af245d11STodd Fiala         static ::ProcessMessage::CrashReason
305*af245d11STodd Fiala         GetCrashReasonForSIGSEGV(const siginfo_t *info);
306*af245d11STodd Fiala 
307*af245d11STodd Fiala         static ::ProcessMessage::CrashReason
308*af245d11STodd Fiala         GetCrashReasonForSIGILL(const siginfo_t *info);
309*af245d11STodd Fiala 
310*af245d11STodd Fiala         static ::ProcessMessage::CrashReason
311*af245d11STodd Fiala         GetCrashReasonForSIGFPE(const siginfo_t *info);
312*af245d11STodd Fiala 
313*af245d11STodd Fiala         static ::ProcessMessage::CrashReason
314*af245d11STodd Fiala         GetCrashReasonForSIGBUS(const siginfo_t *info);
315*af245d11STodd Fiala #endif
316*af245d11STodd Fiala 
317*af245d11STodd Fiala         void
318*af245d11STodd Fiala         DoOperation(void *op);
319*af245d11STodd Fiala 
320*af245d11STodd Fiala         /// Stops the child monitor thread.
321*af245d11STodd Fiala         void
322*af245d11STodd Fiala         StopMonitoringChildProcess();
323*af245d11STodd Fiala 
324*af245d11STodd Fiala         /// Stops the operation thread used to attach/launch a process.
325*af245d11STodd Fiala         void
326*af245d11STodd Fiala         StopOpThread();
327*af245d11STodd Fiala 
328*af245d11STodd Fiala         /// Stops monitoring the child process thread.
329*af245d11STodd Fiala         void
330*af245d11STodd Fiala         StopMonitor();
331*af245d11STodd Fiala 
332*af245d11STodd Fiala         bool
333*af245d11STodd Fiala         HasThreadNoLock (lldb::tid_t thread_id);
334*af245d11STodd Fiala 
335*af245d11STodd Fiala         NativeThreadProtocolSP
336*af245d11STodd Fiala         MaybeGetThreadNoLock (lldb::tid_t thread_id);
337*af245d11STodd Fiala 
338*af245d11STodd Fiala         bool
339*af245d11STodd Fiala         StopTrackingThread (lldb::tid_t thread_id);
340*af245d11STodd Fiala 
341*af245d11STodd Fiala         NativeThreadProtocolSP
342*af245d11STodd Fiala         AddThread (lldb::tid_t thread_id);
343*af245d11STodd Fiala 
344*af245d11STodd Fiala         NativeThreadProtocolSP
345*af245d11STodd Fiala         GetOrCreateThread (lldb::tid_t thread_id, bool &created);
346*af245d11STodd Fiala 
347*af245d11STodd Fiala         Error
348*af245d11STodd Fiala         GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
349*af245d11STodd Fiala 
350*af245d11STodd Fiala         Error
351*af245d11STodd Fiala         FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
352*af245d11STodd Fiala 
353*af245d11STodd Fiala         /// Writes a siginfo_t structure corresponding to the given thread ID to the
354*af245d11STodd Fiala         /// memory region pointed to by @p siginfo.
355*af245d11STodd Fiala         bool
356*af245d11STodd Fiala         GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err);
357*af245d11STodd Fiala 
358*af245d11STodd Fiala         /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
359*af245d11STodd Fiala         /// corresponding to the given thread ID to the memory pointed to by @p
360*af245d11STodd Fiala         /// message.
361*af245d11STodd Fiala         bool
362*af245d11STodd Fiala         GetEventMessage(lldb::tid_t tid, unsigned long *message);
363*af245d11STodd Fiala 
364*af245d11STodd Fiala         /// Resumes the given thread.  If @p signo is anything but
365*af245d11STodd Fiala         /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
366*af245d11STodd Fiala         bool
367*af245d11STodd Fiala         Resume(lldb::tid_t tid, uint32_t signo);
368*af245d11STodd Fiala 
369*af245d11STodd Fiala         /// Single steps the given thread.  If @p signo is anything but
370*af245d11STodd Fiala         /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
371*af245d11STodd Fiala         bool
372*af245d11STodd Fiala         SingleStep(lldb::tid_t tid, uint32_t signo);
373*af245d11STodd Fiala 
374*af245d11STodd Fiala         lldb_private::Error
375*af245d11STodd Fiala         Detach(lldb::tid_t tid);
376*af245d11STodd Fiala     };
377*af245d11STodd Fiala } // End lldb_private namespace.
378*af245d11STodd Fiala 
379*af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
380