1 //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef liblldb_NativeProcessLinux_H_ 10 #define liblldb_NativeProcessLinux_H_ 11 12 #include <csignal> 13 #include <unordered_set> 14 15 #include "lldb/Host/Debug.h" 16 #include "lldb/Host/HostThread.h" 17 #include "lldb/Host/linux/Support.h" 18 #include "lldb/Target/MemoryRegionInfo.h" 19 #include "lldb/Utility/ArchSpec.h" 20 #include "lldb/Utility/FileSpec.h" 21 #include "lldb/lldb-types.h" 22 23 #include "NativeThreadLinux.h" 24 #include "ProcessorTrace.h" 25 #include "lldb/Host/common/NativeProcessProtocol.h" 26 27 namespace lldb_private { 28 class Status; 29 class Scalar; 30 31 namespace process_linux { 32 /// \class NativeProcessLinux 33 /// Manages communication with the inferior (debugee) process. 34 /// 35 /// Upon construction, this class prepares and launches an inferior process 36 /// for debugging. 37 /// 38 /// Changes in the inferior process state are broadcasted. 39 class NativeProcessLinux : public NativeProcessProtocol { 40 public: 41 class Factory : public NativeProcessProtocol::Factory { 42 public: 43 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 44 Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 45 MainLoop &mainloop) const override; 46 47 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 48 Attach(lldb::pid_t pid, NativeDelegate &native_delegate, 49 MainLoop &mainloop) const override; 50 }; 51 52 // NativeProcessProtocol Interface 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 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 72 size_t &bytes_written) override; 73 74 Status AllocateMemory(size_t size, uint32_t permissions, 75 lldb::addr_t &addr) override; 76 77 Status DeallocateMemory(lldb::addr_t addr) override; 78 79 lldb::addr_t GetSharedLibraryInfoAddress() override; 80 81 size_t UpdateThreads() override; 82 83 const ArchSpec &GetArchitecture() const override { return m_arch; } 84 85 Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 86 bool hardware) override; 87 88 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override; 89 90 void DoStopIDBumped(uint32_t newBumpId) override; 91 92 Status GetLoadedModuleFileSpec(const char *module_path, 93 FileSpec &file_spec) override; 94 95 Status GetFileLoadAddress(const llvm::StringRef &file_name, 96 lldb::addr_t &load_addr) override; 97 98 NativeThreadLinux *GetThreadByID(lldb::tid_t id); 99 100 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 101 GetAuxvData() const override { 102 return getProcFile(GetID(), "auxv"); 103 } 104 105 lldb::user_id_t StartTrace(const TraceOptions &config, 106 Status &error) override; 107 108 Status StopTrace(lldb::user_id_t traceid, 109 lldb::tid_t thread) override; 110 111 Status GetData(lldb::user_id_t traceid, lldb::tid_t thread, 112 llvm::MutableArrayRef<uint8_t> &buffer, 113 size_t offset = 0) override; 114 115 Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread, 116 llvm::MutableArrayRef<uint8_t> &buffer, 117 size_t offset = 0) override; 118 119 Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override; 120 121 // Interface used by NativeRegisterContext-derived classes. 122 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 123 void *data = nullptr, size_t data_size = 0, 124 long *result = nullptr); 125 126 bool SupportHardwareSingleStepping() const; 127 128 protected: 129 llvm::Expected<llvm::ArrayRef<uint8_t>> 130 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 131 132 private: 133 MainLoop::SignalHandleUP m_sigchld_handle; 134 ArchSpec m_arch; 135 136 LazyBool m_supports_mem_region = eLazyBoolCalculate; 137 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 138 139 lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 140 141 // List of thread ids stepping with a breakpoint with the address of 142 // the relevan breakpoint 143 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 144 145 // Private Instance Methods 146 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 147 const ArchSpec &arch, MainLoop &mainloop, 148 llvm::ArrayRef<::pid_t> tids); 149 150 // Returns a list of process threads that we have attached to. 151 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 152 153 static Status SetDefaultPtraceOpts(const lldb::pid_t); 154 155 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 156 157 void WaitForNewThread(::pid_t tid); 158 159 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 160 161 void MonitorTrace(NativeThreadLinux &thread); 162 163 void MonitorBreakpoint(NativeThreadLinux &thread); 164 165 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 166 167 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 168 bool exited); 169 170 Status SetupSoftwareSingleStepping(NativeThreadLinux &thread); 171 172 bool HasThreadNoLock(lldb::tid_t thread_id); 173 174 bool StopTrackingThread(lldb::tid_t thread_id); 175 176 NativeThreadLinux &AddThread(lldb::tid_t thread_id); 177 178 /// Writes a siginfo_t structure corresponding to the given thread ID to the 179 /// memory region pointed to by \p siginfo. 180 Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 181 182 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 183 /// corresponding to the given thread ID to the memory pointed to by @p 184 /// message. 185 Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 186 187 void NotifyThreadDeath(lldb::tid_t tid); 188 189 Status Detach(lldb::tid_t tid); 190 191 // This method is requests a stop on all threads which are still running. It 192 // sets up a 193 // deferred delegate notification, which will fire once threads report as 194 // stopped. The 195 // triggerring_tid will be set as the current thread (main stop reason). 196 void StopRunningThreads(lldb::tid_t triggering_tid); 197 198 // Notify the delegate if all threads have stopped. 199 void SignalIfAllThreadsStopped(); 200 201 // Resume the given thread, optionally passing it the given signal. The type 202 // of resume 203 // operation (continue, single-step) depends on the state parameter. 204 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 205 int signo); 206 207 void ThreadWasCreated(NativeThreadLinux &thread); 208 209 void SigchldHandler(); 210 211 Status PopulateMemoryRegionCache(); 212 213 lldb::user_id_t StartTraceGroup(const TraceOptions &config, 214 Status &error); 215 216 // This function is intended to be used to stop tracing 217 // on a thread that exited. 218 Status StopTracingForThread(lldb::tid_t thread); 219 220 // The below function as the name suggests, looks up a ProcessorTrace 221 // instance from the m_processor_trace_monitor map. In the case of 222 // process tracing where the traceid passed would map to the complete 223 // process, it is mandatory to provide a threadid to obtain a trace 224 // instance (since ProcessorTrace is tied to a thread). In the other 225 // scenario that an individual thread is being traced, just the traceid 226 // is sufficient to obtain the actual ProcessorTrace instance. 227 llvm::Expected<ProcessorTraceMonitor &> 228 LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 229 230 // Stops tracing on individual threads being traced. Not intended 231 // to be used to stop tracing on complete process. 232 Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 233 lldb::tid_t thread); 234 235 // Intended to stop tracing on complete process. 236 // Should not be used for stopping trace on 237 // individual threads. 238 void StopProcessorTracingOnProcess(); 239 240 llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 241 m_processor_trace_monitor; 242 243 // Set for tracking threads being traced under 244 // same process user id. 245 llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 246 247 lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID; 248 TraceOptions m_pt_process_trace_config; 249 }; 250 251 } // namespace process_linux 252 } // namespace lldb_private 253 254 #endif // #ifndef liblldb_NativeProcessLinux_H_ 255