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 "Plugins/Process/POSIX/NativeProcessELF.h" 25 #include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h" 26 #include "ProcessorTrace.h" 27 28 namespace lldb_private { 29 class Status; 30 class Scalar; 31 32 namespace process_linux { 33 /// \class NativeProcessLinux 34 /// Manages communication with the inferior (debugee) process. 35 /// 36 /// Upon construction, this class prepares and launches an inferior process 37 /// for debugging. 38 /// 39 /// Changes in the inferior process state are broadcasted. 40 class NativeProcessLinux : public NativeProcessELF, 41 private NativeProcessSoftwareSingleStep { 42 public: 43 class Factory : public NativeProcessProtocol::Factory { 44 public: 45 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 46 Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, 47 MainLoop &mainloop) const override; 48 49 llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 50 Attach(lldb::pid_t pid, NativeDelegate &native_delegate, 51 MainLoop &mainloop) const override; 52 }; 53 54 // NativeProcessProtocol Interface 55 Status Resume(const ResumeActionList &resume_actions) override; 56 57 Status Halt() override; 58 59 Status Detach() override; 60 61 Status Signal(int signo) override; 62 63 Status Interrupt() override; 64 65 Status Kill() override; 66 67 Status GetMemoryRegionInfo(lldb::addr_t load_addr, 68 MemoryRegionInfo &range_info) override; 69 70 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 71 size_t &bytes_read) override; 72 73 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 74 size_t &bytes_written) override; 75 76 llvm::Expected<lldb::addr_t> AllocateMemory(size_t size, 77 uint32_t permissions) override; 78 79 llvm::Error DeallocateMemory(lldb::addr_t addr) 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 NativeThreadLinux *GetCurrentThread(); 100 101 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 102 GetAuxvData() const override { 103 return getProcFile(GetID(), "auxv"); 104 } 105 106 lldb::user_id_t StartTrace(const TraceOptions &config, 107 Status &error) override; 108 109 Status StopTrace(lldb::user_id_t traceid, 110 lldb::tid_t thread) override; 111 112 Status GetData(lldb::user_id_t traceid, lldb::tid_t thread, 113 llvm::MutableArrayRef<uint8_t> &buffer, 114 size_t offset = 0) override; 115 116 Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread, 117 llvm::MutableArrayRef<uint8_t> &buffer, 118 size_t offset = 0) override; 119 120 Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override; 121 122 virtual llvm::Expected<TraceTypeInfo> GetSupportedTraceType() override; 123 124 // Interface used by NativeRegisterContext-derived classes. 125 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 126 void *data = nullptr, size_t data_size = 0, 127 long *result = nullptr); 128 129 bool SupportHardwareSingleStepping() const; 130 131 protected: 132 llvm::Expected<llvm::ArrayRef<uint8_t>> 133 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 134 135 llvm::Expected<uint64_t> Syscall(llvm::ArrayRef<uint64_t> args); 136 137 private: 138 MainLoop::SignalHandleUP m_sigchld_handle; 139 ArchSpec m_arch; 140 141 LazyBool m_supports_mem_region = eLazyBoolCalculate; 142 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 143 144 lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 145 146 /// Inferior memory (allocated by us) and its size. 147 llvm::DenseMap<lldb::addr_t, lldb::addr_t> m_allocated_memory; 148 149 // Private Instance Methods 150 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 151 const ArchSpec &arch, MainLoop &mainloop, 152 llvm::ArrayRef<::pid_t> tids); 153 154 // Returns a list of process threads that we have attached to. 155 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 156 157 static Status SetDefaultPtraceOpts(const lldb::pid_t); 158 159 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 160 161 void WaitForNewThread(::pid_t tid); 162 163 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 164 165 void MonitorTrace(NativeThreadLinux &thread); 166 167 void MonitorBreakpoint(NativeThreadLinux &thread); 168 169 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 170 171 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 172 bool exited); 173 174 bool HasThreadNoLock(lldb::tid_t thread_id); 175 176 bool StopTrackingThread(lldb::tid_t thread_id); 177 178 NativeThreadLinux &AddThread(lldb::tid_t thread_id); 179 180 /// Writes a siginfo_t structure corresponding to the given thread ID to the 181 /// memory region pointed to by \p siginfo. 182 Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 183 184 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 185 /// corresponding to the given thread ID to the memory pointed to by @p 186 /// message. 187 Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 188 189 void NotifyThreadDeath(lldb::tid_t tid); 190 191 Status Detach(lldb::tid_t tid); 192 193 // This method is requests a stop on all threads which are still running. It 194 // sets up a 195 // deferred delegate notification, which will fire once threads report as 196 // stopped. The 197 // triggerring_tid will be set as the current thread (main stop reason). 198 void StopRunningThreads(lldb::tid_t triggering_tid); 199 200 // Notify the delegate if all threads have stopped. 201 void SignalIfAllThreadsStopped(); 202 203 // Resume the given thread, optionally passing it the given signal. The type 204 // of resume 205 // operation (continue, single-step) depends on the state parameter. 206 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 207 int signo); 208 209 void ThreadWasCreated(NativeThreadLinux &thread); 210 211 void SigchldHandler(); 212 213 Status PopulateMemoryRegionCache(); 214 215 lldb::user_id_t StartTraceGroup(const TraceOptions &config, 216 Status &error); 217 218 // This function is intended to be used to stop tracing 219 // on a thread that exited. 220 Status StopTracingForThread(lldb::tid_t thread); 221 222 // The below function as the name suggests, looks up a ProcessorTrace 223 // instance from the m_processor_trace_monitor map. In the case of 224 // process tracing where the traceid passed would map to the complete 225 // process, it is mandatory to provide a threadid to obtain a trace 226 // instance (since ProcessorTrace is tied to a thread). In the other 227 // scenario that an individual thread is being traced, just the traceid 228 // is sufficient to obtain the actual ProcessorTrace instance. 229 llvm::Expected<ProcessorTraceMonitor &> 230 LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread); 231 232 // Stops tracing on individual threads being traced. Not intended 233 // to be used to stop tracing on complete process. 234 Status StopProcessorTracingOnThread(lldb::user_id_t traceid, 235 lldb::tid_t thread); 236 237 // Intended to stop tracing on complete process. 238 // Should not be used for stopping trace on 239 // individual threads. 240 void StopProcessorTracingOnProcess(); 241 242 llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP> 243 m_processor_trace_monitor; 244 245 // Set for tracking threads being traced under 246 // same process user id. 247 llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group; 248 249 lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID; 250 TraceOptions m_pt_process_trace_config; 251 }; 252 253 } // namespace process_linux 254 } // namespace lldb_private 255 256 #endif // #ifndef liblldb_NativeProcessLinux_H_ 257