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 "IntelPTManager.h" 24 #include "NativeThreadLinux.h" 25 #include "Plugins/Process/POSIX/NativeProcessELF.h" 26 #include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.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 /// Tracing 107 /// These methods implement the jLLDBTrace packets 108 /// \{ 109 llvm::Error TraceStart(llvm::StringRef json_request, 110 llvm::StringRef type) override; 111 112 llvm::Error TraceStop(const TraceStopRequest &request) override; 113 114 llvm::Expected<llvm::json::Value> 115 TraceGetState(llvm::StringRef type) override; 116 117 llvm::Expected<std::vector<uint8_t>> 118 TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override; 119 120 llvm::Expected<TraceSupportedResponse> TraceSupported() override; 121 /// } 122 123 // Interface used by NativeRegisterContext-derived classes. 124 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr, 125 void *data = nullptr, size_t data_size = 0, 126 long *result = nullptr); 127 128 bool SupportHardwareSingleStepping() const; 129 130 protected: 131 llvm::Expected<llvm::ArrayRef<uint8_t>> 132 GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; 133 134 llvm::Expected<uint64_t> Syscall(llvm::ArrayRef<uint64_t> args); 135 136 private: 137 MainLoop::SignalHandleUP m_sigchld_handle; 138 ArchSpec m_arch; 139 140 LazyBool m_supports_mem_region = eLazyBoolCalculate; 141 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache; 142 143 lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 144 145 /// Inferior memory (allocated by us) and its size. 146 llvm::DenseMap<lldb::addr_t, lldb::addr_t> m_allocated_memory; 147 148 // Private Instance Methods 149 NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate, 150 const ArchSpec &arch, MainLoop &mainloop, 151 llvm::ArrayRef<::pid_t> tids); 152 153 // Returns a list of process threads that we have attached to. 154 static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid); 155 156 static Status SetDefaultPtraceOpts(const lldb::pid_t); 157 158 void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status); 159 160 void WaitForCloneNotification(::pid_t pid); 161 162 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); 163 164 void MonitorTrace(NativeThreadLinux &thread); 165 166 void MonitorBreakpoint(NativeThreadLinux &thread); 167 168 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); 169 170 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, 171 bool exited); 172 173 bool HasThreadNoLock(lldb::tid_t thread_id); 174 175 bool StopTrackingThread(lldb::tid_t thread_id); 176 177 /// Create a new thread. 178 /// 179 /// If process tracing is enabled and the thread can't be traced, then the 180 /// thread is left stopped with a \a eStopReasonProcessorTrace status, and 181 /// then the process is stopped. 182 /// 183 /// \param[in] resume 184 /// If a tracing error didn't happen, then resume the thread after 185 /// creation if \b true, or leave it stopped with SIGSTOP if \b false. 186 NativeThreadLinux &AddThread(lldb::tid_t thread_id, bool resume); 187 188 /// Start tracing a new thread if process tracing is enabled. 189 /// 190 /// Trace mechanisms should modify this method to provide automatic tracing 191 /// for new threads. 192 Status NotifyTracersOfNewThread(lldb::tid_t tid); 193 194 /// Stop tracing threads upon a destroy event. 195 /// 196 /// Trace mechanisms should modify this method to provide automatic trace 197 /// stopping for threads being destroyed. 198 Status NotifyTracersOfThreadDestroyed(lldb::tid_t tid); 199 200 /// Writes a siginfo_t structure corresponding to the given thread ID to the 201 /// memory region pointed to by \p siginfo. 202 Status GetSignalInfo(lldb::tid_t tid, void *siginfo); 203 204 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 205 /// corresponding to the given thread ID to the memory pointed to by @p 206 /// message. 207 Status GetEventMessage(lldb::tid_t tid, unsigned long *message); 208 209 void NotifyThreadDeath(lldb::tid_t tid); 210 211 Status Detach(lldb::tid_t tid); 212 213 // This method is requests a stop on all threads which are still running. It 214 // sets up a 215 // deferred delegate notification, which will fire once threads report as 216 // stopped. The 217 // triggerring_tid will be set as the current thread (main stop reason). 218 void StopRunningThreads(lldb::tid_t triggering_tid); 219 220 // Notify the delegate if all threads have stopped. 221 void SignalIfAllThreadsStopped(); 222 223 // Resume the given thread, optionally passing it the given signal. The type 224 // of resume 225 // operation (continue, single-step) depends on the state parameter. 226 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state, 227 int signo); 228 229 void ThreadWasCreated(NativeThreadLinux &thread); 230 231 void SigchldHandler(); 232 233 Status PopulateMemoryRegionCache(); 234 235 /// Manages Intel PT process and thread traces. 236 IntelPTManager m_intel_pt_manager; 237 238 struct CloneInfo { 239 int event; 240 lldb::tid_t parent_tid; 241 }; 242 243 // Map of child processes that have been signaled once, and we are 244 // waiting for the second signal. 245 llvm::DenseMap<lldb::pid_t, llvm::Optional<CloneInfo>> m_pending_pid_map; 246 247 // Handle a clone()-like event. If received by parent, clone_info contains 248 // additional info. Returns true if the event is handled, or false if it 249 // is pending second notification. 250 bool MonitorClone(lldb::pid_t child_pid, 251 llvm::Optional<CloneInfo> clone_info); 252 }; 253 254 } // namespace process_linux 255 } // namespace lldb_private 256 257 #endif // #ifndef liblldb_NativeProcessLinux_H_ 258