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