1 //===-- NativeProcessProtocol.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 LLDB_HOST_COMMON_NATIVEPROCESSPROTOCOL_H 10 #define LLDB_HOST_COMMON_NATIVEPROCESSPROTOCOL_H 11 12 #include "NativeBreakpointList.h" 13 #include "NativeThreadProtocol.h" 14 #include "NativeWatchpointList.h" 15 #include "lldb/Host/Host.h" 16 #include "lldb/Host/MainLoop.h" 17 #include "lldb/Utility/ArchSpec.h" 18 #include "lldb/Utility/Iterable.h" 19 #include "lldb/Utility/Status.h" 20 #include "lldb/Utility/TraceGDBRemotePackets.h" 21 #include "lldb/Utility/UnimplementedError.h" 22 #include "lldb/lldb-private-forward.h" 23 #include "lldb/lldb-types.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/DenseSet.h" 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/Support/Error.h" 28 #include "llvm/Support/MemoryBuffer.h" 29 #include <mutex> 30 #include <optional> 31 #include <unordered_map> 32 #include <vector> 33 34 namespace lldb_private { 35 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); 36 37 class MemoryRegionInfo; 38 class ResumeActionList; 39 40 struct SVR4LibraryInfo { 41 std::string name; 42 lldb::addr_t link_map; 43 lldb::addr_t base_addr; 44 lldb::addr_t ld_addr; 45 lldb::addr_t next; 46 }; 47 48 // NativeProcessProtocol 49 class NativeProcessProtocol { 50 public: 51 virtual ~NativeProcessProtocol() = default; 52 53 typedef std::vector<std::unique_ptr<NativeThreadProtocol>> thread_collection; 54 typedef LockingAdaptedIterable< 55 std::recursive_mutex, thread_collection, 56 llvm::pointee_iterator<thread_collection::const_iterator>> 57 ThreadIterable; 58 59 virtual Status Resume(const ResumeActionList &resume_actions) = 0; 60 61 virtual Status Halt() = 0; 62 63 virtual Status Detach() = 0; 64 65 /// Sends a process a UNIX signal \a signal. 66 /// 67 /// \return 68 /// Returns an error object. 69 virtual Status Signal(int signo) = 0; 70 71 /// Tells a process to interrupt all operations as if by a Ctrl-C. 72 /// 73 /// The default implementation will send a local host's equivalent of 74 /// a SIGSTOP to the process via the NativeProcessProtocol::Signal() 75 /// operation. 76 /// 77 /// \return 78 /// Returns an error object. 79 virtual Status Interrupt(); 80 81 virtual Status Kill() = 0; 82 83 // Tells a process not to stop the inferior on given signals and just 84 // reinject them back. 85 virtual Status IgnoreSignals(llvm::ArrayRef<int> signals); 86 87 // Memory and memory region functions 88 89 virtual Status GetMemoryRegionInfo(lldb::addr_t load_addr, 90 MemoryRegionInfo &range_info); 91 92 virtual Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, 93 size_t &bytes_read) = 0; 94 95 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, 96 size_t &bytes_read); 97 98 virtual Status ReadMemoryTags(int32_t type, lldb::addr_t addr, size_t len, 99 std::vector<uint8_t> &tags); 100 101 virtual Status WriteMemoryTags(int32_t type, lldb::addr_t addr, size_t len, 102 const std::vector<uint8_t> &tags); 103 104 /// Reads a null terminated string from memory. 105 /// 106 /// Reads up to \p max_size bytes of memory until it finds a '\0'. 107 /// If a '\0' is not found then it reads max_size-1 bytes as a string and a 108 /// '\0' is added as the last character of the \p buffer. 109 /// 110 /// \param[in] addr 111 /// The address in memory to read from. 112 /// 113 /// \param[in] buffer 114 /// An allocated buffer with at least \p max_size size. 115 /// 116 /// \param[in] max_size 117 /// The maximum number of bytes to read from memory until it reads the 118 /// string. 119 /// 120 /// \param[out] total_bytes_read 121 /// The number of bytes read from memory into \p buffer. 122 /// 123 /// \return 124 /// Returns a StringRef backed up by the \p buffer passed in. 125 llvm::Expected<llvm::StringRef> 126 ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, 127 size_t &total_bytes_read); 128 129 virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, 130 size_t &bytes_written) = 0; 131 AllocateMemory(size_t size,uint32_t permissions)132 virtual llvm::Expected<lldb::addr_t> AllocateMemory(size_t size, 133 uint32_t permissions) { 134 return llvm::make_error<UnimplementedError>(); 135 } 136 DeallocateMemory(lldb::addr_t addr)137 virtual llvm::Error DeallocateMemory(lldb::addr_t addr) { 138 return llvm::make_error<UnimplementedError>(); 139 } 140 141 virtual lldb::addr_t GetSharedLibraryInfoAddress() = 0; 142 143 virtual llvm::Expected<std::vector<SVR4LibraryInfo>> GetLoadedSVR4Libraries()144 GetLoadedSVR4Libraries() { 145 return llvm::createStringError(llvm::inconvertibleErrorCode(), 146 "Not implemented"); 147 } 148 149 virtual bool IsAlive() const; 150 151 virtual size_t UpdateThreads() = 0; 152 153 virtual const ArchSpec &GetArchitecture() const = 0; 154 155 // Breakpoint functions 156 virtual Status SetBreakpoint(lldb::addr_t addr, uint32_t size, 157 bool hardware) = 0; 158 159 virtual Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false); 160 161 // Hardware Breakpoint functions 162 virtual const HardwareBreakpointMap &GetHardwareBreakpointMap() const; 163 164 virtual Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size); 165 166 virtual Status RemoveHardwareBreakpoint(lldb::addr_t addr); 167 168 // Watchpoint functions 169 virtual const NativeWatchpointList::WatchpointMap &GetWatchpointMap() const; 170 171 virtual std::optional<std::pair<uint32_t, uint32_t>> 172 GetHardwareDebugSupportInfo() const; 173 174 virtual Status SetWatchpoint(lldb::addr_t addr, size_t size, 175 uint32_t watch_flags, bool hardware); 176 177 virtual Status RemoveWatchpoint(lldb::addr_t addr); 178 179 // Accessors GetID()180 lldb::pid_t GetID() const { return m_pid; } 181 182 lldb::StateType GetState() const; 183 IsRunning()184 bool IsRunning() const { 185 return m_state == lldb::eStateRunning || IsStepping(); 186 } 187 IsStepping()188 bool IsStepping() const { return m_state == lldb::eStateStepping; } 189 CanResume()190 bool CanResume() const { return m_state == lldb::eStateStopped; } 191 GetByteOrder()192 lldb::ByteOrder GetByteOrder() const { 193 return GetArchitecture().GetByteOrder(); 194 } 195 GetAddressByteSize()196 uint32_t GetAddressByteSize() const { 197 return GetArchitecture().GetAddressByteSize(); 198 } 199 200 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 201 GetAuxvData() const = 0; 202 203 // Exit Status 204 virtual std::optional<WaitStatus> GetExitStatus(); 205 206 virtual bool SetExitStatus(WaitStatus status, bool bNotifyStateChange); 207 208 // Access to threads 209 NativeThreadProtocol *GetThreadAtIndex(uint32_t idx); 210 211 NativeThreadProtocol *GetThreadByID(lldb::tid_t tid); 212 SetCurrentThreadID(lldb::tid_t tid)213 void SetCurrentThreadID(lldb::tid_t tid) { m_current_thread_id = tid; } 214 GetCurrentThreadID()215 lldb::tid_t GetCurrentThreadID() const { return m_current_thread_id; } 216 GetCurrentThread()217 NativeThreadProtocol *GetCurrentThread() { 218 return GetThreadByID(m_current_thread_id); 219 } 220 Threads()221 ThreadIterable Threads() const { 222 return ThreadIterable(m_threads, m_threads_mutex); 223 } 224 225 // Access to inferior stdio GetTerminalFileDescriptor()226 virtual int GetTerminalFileDescriptor() { return m_terminal_fd; } 227 228 // Stop id interface 229 230 uint32_t GetStopID() const; 231 232 // Callbacks for low-level process state changes 233 class NativeDelegate { 234 public: 235 virtual ~NativeDelegate() = default; 236 237 virtual void InitializeDelegate(NativeProcessProtocol *process) = 0; 238 239 virtual void ProcessStateChanged(NativeProcessProtocol *process, 240 lldb::StateType state) = 0; 241 242 virtual void DidExec(NativeProcessProtocol *process) = 0; 243 244 virtual void 245 NewSubprocess(NativeProcessProtocol *parent_process, 246 std::unique_ptr<NativeProcessProtocol> child_process) = 0; 247 }; 248 249 virtual Status GetLoadedModuleFileSpec(const char *module_path, 250 FileSpec &file_spec) = 0; 251 252 virtual Status GetFileLoadAddress(const llvm::StringRef &file_name, 253 lldb::addr_t &load_addr) = 0; 254 255 /// Extension flag constants, returned by Manager::GetSupportedExtensions() 256 /// and passed to SetEnabledExtension() 257 enum class Extension { 258 multiprocess = (1u << 0), 259 fork = (1u << 1), 260 vfork = (1u << 2), 261 pass_signals = (1u << 3), 262 auxv = (1u << 4), 263 libraries_svr4 = (1u << 5), 264 memory_tagging = (1u << 6), 265 savecore = (1u << 7), 266 siginfo_read = (1u << 8), 267 268 LLVM_MARK_AS_BITMASK_ENUM(siginfo_read) 269 }; 270 271 class Manager { 272 public: Manager(MainLoop & mainloop)273 Manager(MainLoop &mainloop) : m_mainloop(mainloop) {} 274 Manager(const Manager &) = delete; 275 Manager &operator=(const Manager &) = delete; 276 277 virtual ~Manager(); 278 279 /// Launch a process for debugging. 280 /// 281 /// \param[in] launch_info 282 /// Information required to launch the process. 283 /// 284 /// \param[in] native_delegate 285 /// The delegate that will receive messages regarding the 286 /// inferior. Must outlive the NativeProcessProtocol 287 /// instance. 288 /// 289 /// \param[in] mainloop 290 /// The mainloop instance with which the process can register 291 /// callbacks. Must outlive the NativeProcessProtocol 292 /// instance. 293 /// 294 /// \return 295 /// A NativeProcessProtocol shared pointer if the operation succeeded or 296 /// an error object if it failed. 297 virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 298 Launch(ProcessLaunchInfo &launch_info, 299 NativeDelegate &native_delegate) = 0; 300 301 /// Attach to an existing process. 302 /// 303 /// \param[in] pid 304 /// pid of the process locatable 305 /// 306 /// \param[in] native_delegate 307 /// The delegate that will receive messages regarding the 308 /// inferior. Must outlive the NativeProcessProtocol 309 /// instance. 310 /// 311 /// \param[in] mainloop 312 /// The mainloop instance with which the process can register 313 /// callbacks. Must outlive the NativeProcessProtocol 314 /// instance. 315 /// 316 /// \return 317 /// A NativeProcessProtocol shared pointer if the operation succeeded or 318 /// an error object if it failed. 319 virtual llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 320 Attach(lldb::pid_t pid, NativeDelegate &native_delegate) = 0; 321 322 /// Get the bitmask of extensions supported by this process plugin. 323 /// 324 /// \return 325 /// A NativeProcessProtocol::Extension bitmask. GetSupportedExtensions()326 virtual Extension GetSupportedExtensions() const { return {}; } 327 328 protected: 329 MainLoop &m_mainloop; 330 }; 331 332 /// Notify tracers that the target process will resume NotifyTracersProcessWillResume()333 virtual void NotifyTracersProcessWillResume() {} 334 335 /// Notify tracers that the target process just stopped NotifyTracersProcessDidStop()336 virtual void NotifyTracersProcessDidStop() {} 337 338 /// Start tracing a process or its threads. 339 /// 340 /// \param[in] json_params 341 /// JSON object with the information of what and how to trace. 342 /// In the case of gdb-remote, this object should conform to the 343 /// jLLDBTraceStart packet. 344 /// 345 /// This object should have a string entry called "type", which is the 346 /// tracing technology name. 347 /// 348 /// \param[in] type 349 /// Tracing technology type, as described in the \a json_params. 350 /// 351 /// \return 352 /// \a llvm::Error::success if the operation was successful, or an 353 /// \a llvm::Error otherwise. TraceStart(llvm::StringRef json_params,llvm::StringRef type)354 virtual llvm::Error TraceStart(llvm::StringRef json_params, 355 llvm::StringRef type) { 356 return llvm::createStringError(llvm::inconvertibleErrorCode(), 357 "Unsupported tracing type '%s'", 358 type.data()); 359 } 360 361 /// \copydoc Process::TraceStop(const TraceStopRequest &) TraceStop(const TraceStopRequest & request)362 virtual llvm::Error TraceStop(const TraceStopRequest &request) { 363 return llvm::createStringError(llvm::inconvertibleErrorCode(), 364 "Unsupported tracing type '%s'", 365 request.type.data()); 366 } 367 368 /// \copydoc Process::TraceGetState(llvm::StringRef type) 369 virtual llvm::Expected<llvm::json::Value> TraceGetState(llvm::StringRef type)370 TraceGetState(llvm::StringRef type) { 371 return llvm::createStringError(llvm::inconvertibleErrorCode(), 372 "Unsupported tracing type '%s'", 373 type.data()); 374 } 375 376 /// \copydoc Process::TraceGetBinaryData(const TraceGetBinaryDataRequest &) 377 virtual llvm::Expected<std::vector<uint8_t>> TraceGetBinaryData(const TraceGetBinaryDataRequest & request)378 TraceGetBinaryData(const TraceGetBinaryDataRequest &request) { 379 return llvm::createStringError( 380 llvm::inconvertibleErrorCode(), 381 "Unsupported data kind '%s' for the '%s' tracing technology", 382 request.kind.c_str(), request.type.c_str()); 383 } 384 385 /// \copydoc Process::TraceSupported() TraceSupported()386 virtual llvm::Expected<TraceSupportedResponse> TraceSupported() { 387 return llvm::make_error<UnimplementedError>(); 388 } 389 390 /// Method called in order to propagate the bitmap of protocol 391 /// extensions supported by the client. 392 /// 393 /// \param[in] flags 394 /// The bitmap of enabled extensions. SetEnabledExtensions(Extension flags)395 virtual void SetEnabledExtensions(Extension flags) { 396 m_enabled_extensions = flags; 397 } 398 399 /// Write a core dump (without crashing the program). 400 /// 401 /// \param[in] path_hint 402 /// Suggested core dump path (optional, can be empty). 403 /// 404 /// \return 405 /// Path to the core dump if successfully written, an error 406 /// otherwise. SaveCore(llvm::StringRef path_hint)407 virtual llvm::Expected<std::string> SaveCore(llvm::StringRef path_hint) { 408 return llvm::createStringError(llvm::inconvertibleErrorCode(), 409 "Not implemented"); 410 } 411 412 protected: 413 struct SoftwareBreakpoint { 414 uint32_t ref_count; 415 llvm::SmallVector<uint8_t, 4> saved_opcodes; 416 llvm::ArrayRef<uint8_t> breakpoint_opcodes; 417 }; 418 419 std::unordered_map<lldb::addr_t, SoftwareBreakpoint> m_software_breakpoints; 420 lldb::pid_t m_pid; 421 422 std::vector<std::unique_ptr<NativeThreadProtocol>> m_threads; 423 lldb::tid_t m_current_thread_id = LLDB_INVALID_THREAD_ID; 424 mutable std::recursive_mutex m_threads_mutex; 425 426 lldb::StateType m_state = lldb::eStateInvalid; 427 mutable std::recursive_mutex m_state_mutex; 428 429 std::optional<WaitStatus> m_exit_status; 430 431 NativeDelegate &m_delegate; 432 NativeWatchpointList m_watchpoint_list; 433 HardwareBreakpointMap m_hw_breakpoints_map; 434 int m_terminal_fd; 435 uint32_t m_stop_id = 0; 436 437 // Set of signal numbers that LLDB directly injects back to inferior without 438 // stopping it. 439 llvm::DenseSet<int> m_signals_to_ignore; 440 441 // Extensions enabled per the last SetEnabledExtensions() call. 442 Extension m_enabled_extensions; 443 444 // lldb_private::Host calls should be used to launch a process for debugging, 445 // and then the process should be attached to. When attaching to a process 446 // lldb_private::Host calls should be used to locate the process to attach 447 // to, and then this function should be called. 448 NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 449 NativeDelegate &delegate); 450 SetID(lldb::pid_t pid)451 void SetID(lldb::pid_t pid) { m_pid = pid; } 452 453 // interface for state handling 454 void SetState(lldb::StateType state, bool notify_delegates = true); 455 456 // Derived classes need not implement this. It can be used as a hook to 457 // clear internal caches that should be invalidated when stop ids change. 458 // 459 // Note this function is called with the state mutex obtained by the caller. 460 virtual void DoStopIDBumped(uint32_t newBumpId); 461 462 // interface for software breakpoints 463 464 Status SetSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint); 465 Status RemoveSoftwareBreakpoint(lldb::addr_t addr); 466 467 virtual llvm::Expected<llvm::ArrayRef<uint8_t>> 468 GetSoftwareBreakpointTrapOpcode(size_t size_hint); 469 470 /// Return the offset of the PC relative to the software breakpoint that was hit. If an 471 /// architecture (e.g. arm) reports breakpoint hits before incrementing the PC, this offset 472 /// will be 0. If an architecture (e.g. intel) reports breakpoints hits after incrementing the 473 /// PC, this offset will be the size of the breakpoint opcode. 474 virtual size_t GetSoftwareBreakpointPCOffset(); 475 476 // Adjust the thread's PC after hitting a software breakpoint. On 477 // architectures where the PC points after the breakpoint instruction, this 478 // resets it to point to the breakpoint itself. 479 void FixupBreakpointPCAsNeeded(NativeThreadProtocol &thread); 480 481 /// Notify the delegate that an exec occurred. 482 /// 483 /// Provide a mechanism for a delegate to clear out any exec- 484 /// sensitive data. 485 virtual void NotifyDidExec(); 486 487 NativeThreadProtocol *GetThreadByIDUnlocked(lldb::tid_t tid); 488 489 private: 490 void SynchronouslyNotifyProcessStateChanged(lldb::StateType state); 491 llvm::Expected<SoftwareBreakpoint> 492 EnableSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint); 493 }; 494 } // namespace lldb_private 495 496 #endif // LLDB_HOST_COMMON_NATIVEPROCESSPROTOCOL_H 497