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 "ProcessorTrace.h" 27 #include "lldb/Host/common/NativeProcessProtocol.h" 28 29 namespace lldb_private { 30 class Status; 31 class Scalar; 32 33 namespace process_linux { 34 /// @class NativeProcessLinux 35 /// @brief Manages communication with the inferior (debugee) process. 36 /// 37 /// Upon construction, this class prepares and launches an inferior process for 38 /// debugging. 39 /// 40 /// Changes in the inferior process state are broadcasted. 41 class NativeProcessLinux : public NativeProcessProtocol { 42 friend Status NativeProcessProtocol::Launch( 43 ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 44 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 45 46 friend Status NativeProcessProtocol::Attach( 47 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 48 MainLoop &mainloop, NativeProcessProtocolSP &process_sp); 49 50 public: 51 // --------------------------------------------------------------------- 52 // NativeProcessProtocol Interface 53 // --------------------------------------------------------------------- 54 Status Resume(const ResumeActionList &resume_actions) override; 55 56 Status Halt() override; 57 58 Status Detach() override; 59 60 Status Signal(int signo) override; 61 62 Status Interrupt() override; 63 64 Status Kill() override; 65 66 Status GetMemoryRegionInfo(lldb::addr_t load_addr, 67 MemoryRegionInfo &range_info) override; 68 69 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 70 size_t &bytes_read) override; 71 72 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 73 size_t &bytes_read) override; 74 75 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 76 size_t &bytes_written) override; 77 78 Status AllocateMemory(size_t size, uint32_t permissions, 79 lldb::addr_t &addr) override; 80 81 Status DeallocateMemory(lldb::addr_t addr) override; 82 83 lldb::addr_t GetSharedLibraryInfoAddress() override; 84 85 size_t UpdateThreads() override; 86 87 bool GetArchitecture(ArchSpec &arch) const override; 88 89 Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 90 bool hardware) override; 91 92 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 93 94 void DoStopIDBumped(uint32_t newBumpId) override; 95 96 Status GetLoadedModuleFileSpec(const char *module_path, 97 FileSpec &file_spec) override; 98 99 Status GetFileLoadAddress(const llvm::StringRef &file_name, 100 lldb::addr_t &load_addr) override; 101 102 NativeThreadLinuxSP GetThreadByID(lldb::tid_t id); 103 104 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 105 GetAuxvData() const override { 106 return getProcFile(GetID(), "auxv"); 107 } 108 109 lldb::user_id_t StartTrace(const TraceOptions &config, 110 Status &error) override; 111 112 Status StopTrace(lldb::user_id_t traceid, 113 lldb::tid_t thread) override; 114 115 Status GetData(lldb::user_id_t traceid, lldb::tid_t thread, 116 llvm::MutableArrayRef<uint8_t> &buffer, 117 size_t offset = 0) override; 118 119 Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread, 120 llvm::MutableArrayRef<uint8_t> &buffer, 121 size_t offset = 0) override; 122 123 Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override; 124 125 // --------------------------------------------------------------------- 126 // Interface used by NativeRegisterContext-derived classes. 127 // --------------------------------------------------------------------- 128 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 129 void *data = nullptr, size_t data_size = 0, 130 long *result = nullptr); 131 132 bool SupportHardwareSingleStepping() const; 133 134 protected: 135 // --------------------------------------------------------------------- 136 // NativeProcessProtocol protected interface 137 // --------------------------------------------------------------------- 138 Status 139 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint, 140 size_t &actual_opcode_size, 141 const uint8_t *&trap_opcode_bytes) override; 142 143 private: 144 MainLoop::SignalHandleUP m_sigchld_handle; 145 ArchSpec m_arch; 146 147 LazyBool m_supports_mem_region; 148 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 149 150 lldb::tid_t m_pending_notification_tid; 151 152 // List of thread ids stepping with a breakpoint with the address of 153 // the relevan breakpoint 154 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 155 156 // --------------------------------------------------------------------- 157 // Private Instance Methods 158 // --------------------------------------------------------------------- 159 NativeProcessLinux(); 160 161 Status LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info); 162 163 /// Attaches to an existing process. Forms the 164 /// implementation of Process::DoAttach 165 void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error); 166 167 ::pid_t Attach(lldb::pid_t pid, Status &error); 168 169 static Status SetDefaultPtraceOpts(const lldb::pid_t); 170 171 static void *MonitorThread(void *baton); 172 173 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 174 175 void WaitForNewThread(::pid_t tid); 176 177 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 178 179 void MonitorTrace(NativeThreadLinux &thread); 180 181 void MonitorBreakpoint(NativeThreadLinux &thread); 182 183 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 184 185 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 186 bool exited); 187 188 Status SetupSoftwareSingleStepping(NativeThreadLinux &thread); 189 190 #if 0 191 static ::ProcessMessage::CrashReason 192 GetCrashReasonForSIGSEGV(const siginfo_t *info); 193 194 static ::ProcessMessage::CrashReason 195 GetCrashReasonForSIGILL(const siginfo_t *info); 196 197 static ::ProcessMessage::CrashReason 198 GetCrashReasonForSIGFPE(const siginfo_t *info); 199 200 static ::ProcessMessage::CrashReason 201 GetCrashReasonForSIGBUS(const siginfo_t *info); 202 #endif 203 204 bool HasThreadNoLock(lldb::tid_t thread_id); 205 206 bool StopTrackingThread(lldb::tid_t thread_id); 207 208 NativeThreadLinuxSP AddThread(lldb::tid_t thread_id); 209 210 Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); 211 212 Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); 213 214 /// Writes a siginfo_t structure corresponding to the given thread ID to the 215 /// memory region pointed to by @p siginfo. 216 Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 217 218 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 219 /// corresponding to the given thread ID to the memory pointed to by @p 220 /// message. 221 Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 222 223 void NotifyThreadDeath(lldb::tid_t tid); 224 225 Status Detach(lldb::tid_t tid); 226 227 // This method is requests a stop on all threads which are still running. It 228 // sets up a 229 // deferred delegate notification, which will fire once threads report as 230 // stopped. The 231 // triggerring_tid will be set as the current thread (main stop reason). 232 void StopRunningThreads(lldb::tid_t triggering_tid); 233 234 // Notify the delegate if all threads have stopped. 235 void SignalIfAllThreadsStopped(); 236 237 // Resume the given thread, optionally passing it the given signal. The type 238 // of resume 239 // operation (continue, single-step) depends on the state parameter. 240 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 241 int signo); 242 243 void ThreadWasCreated(NativeThreadLinux &thread); 244 245 void SigchldHandler(); 246 247 Status PopulateMemoryRegionCache(); 248 249 lldb::user_id_t StartTraceGroup(const TraceOptions &config, 250 Status &error); 251 252 // This function is intended to be used to stop tracing 253 // on a thread that exited. 254 Status StopTracingForThread(lldb::tid_t thread); 255 256 // The below function as the name suggests, looks up a ProcessorTrace 257 // instance from the m_processor_trace_monitor map. In the case of 258 // process tracing where the traceid passed would map to the complete 259 // process, it is mandatory to provide a threadid to obtain a trace 260 // instance (since ProcessorTrace is tied to a thread). In the other 261 // scenario that an individual thread is being traced, just the traceid 262 // is sufficient to obtain the actual ProcessorTrace instance. 263 llvm::Expected<ProcessorTraceMonitor &> 264 LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 265 266 // Stops tracing on individual threads being traced. Not intended 267 // to be used to stop tracing on complete process. 268 Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 269 lldb::tid_t thread); 270 271 // Intended to stop tracing on complete process. 272 // Should not be used for stopping trace on 273 // individual threads. 274 void StopProcessorTracingOnProcess(); 275 276 llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 277 m_processor_trace_monitor; 278 279 // Set for tracking threads being traced under 280 // same process user id. 281 llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 282 283 lldb::user_id_t m_pt_proces_trace_id; 284 TraceOptions m_pt_process_trace_config; 285 }; 286 287 } // namespace process_linux 288 } // namespace lldb_private 289 290 #endif // #ifndef liblldb_NativeProcessLinux_H_ 291