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 <unordered_set>
15 
16 // Other libraries and framework includes
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Host/Debug.h"
19 #include "lldb/Host/HostThread.h"
20 #include "lldb/Host/linux/Support.h"
21 #include "lldb/Target/MemoryRegionInfo.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/lldb-types.h"
24 
25 #include "NativeThreadLinux.h"
26 #include "lldb/Host/common/NativeProcessProtocol.h"
27 
28 namespace lldb_private {
29 class Status;
30 class Scalar;
31 
32 namespace process_linux {
33 /// @class NativeProcessLinux
34 /// @brief Manages communication with the inferior (debugee) process.
35 ///
36 /// Upon construction, this class prepares and launches an inferior process for
37 /// debugging.
38 ///
39 /// Changes in the inferior process state are broadcasted.
40 class NativeProcessLinux : public NativeProcessProtocol {
41   friend Status NativeProcessProtocol::Launch(
42       ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
43       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
44 
45   friend Status NativeProcessProtocol::Attach(
46       lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
47       MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
48 
49 public:
50   // ---------------------------------------------------------------------
51   // NativeProcessProtocol Interface
52   // ---------------------------------------------------------------------
53   Status Resume(const ResumeActionList &resume_actions) override;
54 
55   Status Halt() override;
56 
57   Status Detach() override;
58 
59   Status Signal(int signo) override;
60 
61   Status Interrupt() override;
62 
63   Status Kill() override;
64 
65   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
66                              MemoryRegionInfo &range_info) override;
67 
68   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
69                     size_t &bytes_read) override;
70 
71   Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
72                                size_t &bytes_read) override;
73 
74   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
75                      size_t &bytes_written) override;
76 
77   Status AllocateMemory(size_t size, uint32_t permissions,
78                         lldb::addr_t &addr) override;
79 
80   Status DeallocateMemory(lldb::addr_t addr) override;
81 
82   lldb::addr_t GetSharedLibraryInfoAddress() override;
83 
84   size_t UpdateThreads() override;
85 
86   bool GetArchitecture(ArchSpec &arch) const override;
87 
88   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
89                        bool hardware) override;
90 
91   Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
92 
93   void DoStopIDBumped(uint32_t newBumpId) override;
94 
95   Status GetLoadedModuleFileSpec(const char *module_path,
96                                  FileSpec &file_spec) override;
97 
98   Status GetFileLoadAddress(const llvm::StringRef &file_name,
99                             lldb::addr_t &load_addr) override;
100 
101   NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
102 
103   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
104   GetAuxvData() const override {
105     return getProcFile(GetID(), "auxv");
106   }
107 
108   // ---------------------------------------------------------------------
109   // Interface used by NativeRegisterContext-derived classes.
110   // ---------------------------------------------------------------------
111   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
112                               void *data = nullptr, size_t data_size = 0,
113                               long *result = nullptr);
114 
115   bool SupportHardwareSingleStepping() const;
116 
117 protected:
118   // ---------------------------------------------------------------------
119   // NativeProcessProtocol protected interface
120   // ---------------------------------------------------------------------
121   Status
122   GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
123                                   size_t &actual_opcode_size,
124                                   const uint8_t *&trap_opcode_bytes) override;
125 
126 private:
127   MainLoop::SignalHandleUP m_sigchld_handle;
128   ArchSpec m_arch;
129 
130   LazyBool m_supports_mem_region;
131   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
132 
133   lldb::tid_t m_pending_notification_tid;
134 
135   // List of thread ids stepping with a breakpoint with the address of
136   // the relevan breakpoint
137   std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
138 
139   // ---------------------------------------------------------------------
140   // Private Instance Methods
141   // ---------------------------------------------------------------------
142   NativeProcessLinux();
143 
144   Status LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
145 
146   /// Attaches to an existing process.  Forms the
147   /// implementation of Process::DoAttach
148   void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error);
149 
150   ::pid_t Attach(lldb::pid_t pid, Status &error);
151 
152   static Status SetDefaultPtraceOpts(const lldb::pid_t);
153 
154   static void *MonitorThread(void *baton);
155 
156   void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
157 
158   void WaitForNewThread(::pid_t tid);
159 
160   void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
161 
162   void MonitorTrace(NativeThreadLinux &thread);
163 
164   void MonitorBreakpoint(NativeThreadLinux &thread);
165 
166   void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
167 
168   void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
169                      bool exited);
170 
171   Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
172 
173 #if 0
174         static ::ProcessMessage::CrashReason
175         GetCrashReasonForSIGSEGV(const siginfo_t *info);
176 
177         static ::ProcessMessage::CrashReason
178         GetCrashReasonForSIGILL(const siginfo_t *info);
179 
180         static ::ProcessMessage::CrashReason
181         GetCrashReasonForSIGFPE(const siginfo_t *info);
182 
183         static ::ProcessMessage::CrashReason
184         GetCrashReasonForSIGBUS(const siginfo_t *info);
185 #endif
186 
187   bool HasThreadNoLock(lldb::tid_t thread_id);
188 
189   bool StopTrackingThread(lldb::tid_t thread_id);
190 
191   NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
192 
193   Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
194 
195   Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
196 
197   /// Writes a siginfo_t structure corresponding to the given thread ID to the
198   /// memory region pointed to by @p siginfo.
199   Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
200 
201   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
202   /// corresponding to the given thread ID to the memory pointed to by @p
203   /// message.
204   Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
205 
206   void NotifyThreadDeath(lldb::tid_t tid);
207 
208   Status Detach(lldb::tid_t tid);
209 
210   // This method is requests a stop on all threads which are still running. It
211   // sets up a
212   // deferred delegate notification, which will fire once threads report as
213   // stopped. The
214   // triggerring_tid will be set as the current thread (main stop reason).
215   void StopRunningThreads(lldb::tid_t triggering_tid);
216 
217   // Notify the delegate if all threads have stopped.
218   void SignalIfAllThreadsStopped();
219 
220   // Resume the given thread, optionally passing it the given signal. The type
221   // of resume
222   // operation (continue, single-step) depends on the state parameter.
223   Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
224                       int signo);
225 
226   void ThreadWasCreated(NativeThreadLinux &thread);
227 
228   void SigchldHandler();
229 
230   Status PopulateMemoryRegionCache();
231 };
232 
233 } // namespace process_linux
234 } // namespace lldb_private
235 
236 #endif // #ifndef liblldb_NativeProcessLinux_H_
237