Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
39644 views
//===-- ProcessGDBRemote.h --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_PROCESSGDBREMOTE_H9#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_PROCESSGDBREMOTE_H1011#include <atomic>12#include <map>13#include <mutex>14#include <optional>15#include <string>16#include <vector>1718#include "lldb/Core/LoadedModuleInfoList.h"19#include "lldb/Core/ModuleSpec.h"20#include "lldb/Core/ThreadSafeValue.h"21#include "lldb/Host/HostThread.h"22#include "lldb/Target/DynamicRegisterInfo.h"23#include "lldb/Target/Process.h"24#include "lldb/Target/Thread.h"25#include "lldb/Utility/ArchSpec.h"26#include "lldb/Utility/Broadcaster.h"27#include "lldb/Utility/ConstString.h"28#include "lldb/Utility/GDBRemote.h"29#include "lldb/Utility/Status.h"30#include "lldb/Utility/StreamString.h"31#include "lldb/Utility/StringExtractor.h"32#include "lldb/Utility/StringList.h"33#include "lldb/Utility/StructuredData.h"34#include "lldb/lldb-private-forward.h"3536#include "GDBRemoteCommunicationClient.h"37#include "GDBRemoteRegisterContext.h"3839#include "llvm/ADT/DenseMap.h"40#include "llvm/ADT/StringMap.h"4142namespace lldb_private {43namespace repro {44class Loader;45}46namespace process_gdb_remote {4748class ThreadGDBRemote;4950class ProcessGDBRemote : public Process,51private GDBRemoteClientBase::ContinueDelegate {52public:53~ProcessGDBRemote() override;5455static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,56lldb::ListenerSP listener_sp,57const FileSpec *crash_file_path,58bool can_connect);5960static void Initialize();6162static void DebuggerInitialize(Debugger &debugger);6364static void Terminate();6566static llvm::StringRef GetPluginNameStatic() { return "gdb-remote"; }6768static llvm::StringRef GetPluginDescriptionStatic();6970static std::chrono::seconds GetPacketTimeout();7172ArchSpec GetSystemArchitecture() override;7374// Check if a given Process75bool CanDebug(lldb::TargetSP target_sp,76bool plugin_specified_by_name) override;7778CommandObject *GetPluginCommandObject() override;7980void DumpPluginHistory(Stream &s) override;8182// Creating a new process, or attaching to an existing one83Status DoWillLaunch(Module *module) override;8485Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;8687void DidLaunch() override;8889Status DoWillAttachToProcessWithID(lldb::pid_t pid) override;9091Status DoWillAttachToProcessWithName(const char *process_name,92bool wait_for_launch) override;9394Status DoConnectRemote(llvm::StringRef remote_url) override;9596Status WillLaunchOrAttach();9798Status DoAttachToProcessWithID(lldb::pid_t pid,99const ProcessAttachInfo &attach_info) override;100101Status102DoAttachToProcessWithName(const char *process_name,103const ProcessAttachInfo &attach_info) override;104105void DidAttach(ArchSpec &process_arch) override;106107// PluginInterface protocol108llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }109110// Process Control111Status WillResume() override;112113Status DoResume() override;114115Status DoHalt(bool &caused_stop) override;116117Status DoDetach(bool keep_stopped) override;118119bool DetachRequiresHalt() override { return true; }120121Status DoSignal(int signal) override;122123Status DoDestroy() override;124125void RefreshStateAfterStop() override;126127void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp);128129// Process Queries130bool IsAlive() override;131132lldb::addr_t GetImageInfoAddress() override;133134void WillPublicStop() override;135136// Process Memory137size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,138Status &error) override;139140Status141WriteObjectFile(std::vector<ObjectFile::LoadableData> entries) override;142143size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,144Status &error) override;145146lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,147Status &error) override;148149Status DoDeallocateMemory(lldb::addr_t ptr) override;150151// Process STDIO152size_t PutSTDIN(const char *buf, size_t buf_size, Status &error) override;153154// Process Breakpoints155Status EnableBreakpointSite(BreakpointSite *bp_site) override;156157Status DisableBreakpointSite(BreakpointSite *bp_site) override;158159// Process Watchpoints160Status EnableWatchpoint(lldb::WatchpointSP wp_sp,161bool notify = true) override;162163Status DisableWatchpoint(lldb::WatchpointSP wp_sp,164bool notify = true) override;165166std::optional<uint32_t> GetWatchpointSlotCount() override;167168llvm::Expected<TraceSupportedResponse> TraceSupported() override;169170llvm::Error TraceStop(const TraceStopRequest &request) override;171172llvm::Error TraceStart(const llvm::json::Value &request) override;173174llvm::Expected<std::string> TraceGetState(llvm::StringRef type) override;175176llvm::Expected<std::vector<uint8_t>>177TraceGetBinaryData(const TraceGetBinaryDataRequest &request) override;178179std::optional<bool> DoGetWatchpointReportedAfter() override;180181bool StartNoticingNewThreads() override;182183bool StopNoticingNewThreads() override;184185GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; }186187Status SendEventData(const char *data) override;188189// Override DidExit so we can disconnect from the remote GDB server190void DidExit() override;191192void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max);193194bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,195ModuleSpec &module_spec) override;196197void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs,198const llvm::Triple &triple) override;199200llvm::VersionTuple GetHostOSVersion() override;201llvm::VersionTuple GetHostMacCatalystVersion() override;202203llvm::Error LoadModules() override;204205llvm::Expected<LoadedModuleInfoList> GetLoadedModuleList() override;206207Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded,208lldb::addr_t &load_addr) override;209210void ModulesDidLoad(ModuleList &module_list) override;211212StructuredData::ObjectSP213GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address,214lldb::addr_t image_count) override;215216Status217ConfigureStructuredData(llvm::StringRef type_name,218const StructuredData::ObjectSP &config_sp) override;219220StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override;221222StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(223const std::vector<lldb::addr_t> &load_addresses) override;224225StructuredData::ObjectSP226GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args);227228StructuredData::ObjectSP GetSharedCacheInfo() override;229230StructuredData::ObjectSP GetDynamicLoaderProcessState() override;231232std::string HarmonizeThreadIdsForProfileData(233StringExtractorGDBRemote &inputStringExtractor);234235void DidFork(lldb::pid_t child_pid, lldb::tid_t child_tid) override;236void DidVFork(lldb::pid_t child_pid, lldb::tid_t child_tid) override;237void DidVForkDone() override;238void DidExec() override;239240llvm::Expected<bool> SaveCore(llvm::StringRef outfile) override;241242protected:243friend class ThreadGDBRemote;244friend class GDBRemoteCommunicationClient;245friend class GDBRemoteRegisterContext;246247ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);248249bool SupportsMemoryTagging() override;250251/// Broadcaster event bits definitions.252enum {253eBroadcastBitAsyncContinue = (1 << 0),254eBroadcastBitAsyncThreadShouldExit = (1 << 1),255eBroadcastBitAsyncThreadDidExit = (1 << 2)256};257258GDBRemoteCommunicationClient m_gdb_comm;259std::atomic<lldb::pid_t> m_debugserver_pid;260261std::optional<StringExtractorGDBRemote> m_last_stop_packet;262std::recursive_mutex m_last_stop_packet_mutex;263264GDBRemoteDynamicRegisterInfoSP m_register_info_sp;265Broadcaster m_async_broadcaster;266lldb::ListenerSP m_async_listener_sp;267HostThread m_async_thread;268std::recursive_mutex m_async_thread_state_mutex;269typedef std::vector<lldb::tid_t> tid_collection;270typedef std::vector<std::pair<lldb::tid_t, int>> tid_sig_collection;271typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;272typedef std::map<uint32_t, std::string> ExpeditedRegisterMap;273tid_collection m_thread_ids; // Thread IDs for all threads. This list gets274// updated after stopping275std::vector<lldb::addr_t> m_thread_pcs; // PC values for all the threads.276StructuredData::ObjectSP m_jstopinfo_sp; // Stop info only for any threads277// that have valid stop infos278StructuredData::ObjectSP m_jthreadsinfo_sp; // Full stop info, expedited279// registers and memory for all280// threads if "jThreadsInfo"281// packet is supported282tid_collection m_continue_c_tids; // 'c' for continue283tid_sig_collection m_continue_C_tids; // 'C' for continue with signal284tid_collection m_continue_s_tids; // 's' for step285tid_sig_collection m_continue_S_tids; // 'S' for step with signal286uint64_t m_max_memory_size; // The maximum number of bytes to read/write when287// reading and writing memory288uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote289// gdb stub can handle290MMapMap m_addr_to_mmap_size;291lldb::BreakpointSP m_thread_create_bp_sp;292bool m_waiting_for_attach;293lldb::CommandObjectSP m_command_sp;294int64_t m_breakpoint_pc_offset;295lldb::tid_t m_initial_tid; // The initial thread ID, given by stub on attach296bool m_use_g_packet_for_reading;297298bool m_allow_flash_writes;299using FlashRangeVector = lldb_private::RangeVector<lldb::addr_t, size_t>;300using FlashRange = FlashRangeVector::Entry;301FlashRangeVector m_erased_flash_ranges;302303// Number of vfork() operations being handled.304uint32_t m_vfork_in_progress_count;305306// Accessors307bool IsRunning(lldb::StateType state) {308return state == lldb::eStateRunning || IsStepping(state);309}310311bool IsStepping(lldb::StateType state) {312return state == lldb::eStateStepping;313}314315bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }316317bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }318319void Clear();320321bool DoUpdateThreadList(ThreadList &old_thread_list,322ThreadList &new_thread_list) override;323324Status EstablishConnectionIfNeeded(const ProcessInfo &process_info);325326Status LaunchAndConnectToDebugserver(const ProcessInfo &process_info);327328void KillDebugserverProcess();329330void BuildDynamicRegisterInfo(bool force);331332void SetLastStopPacket(const StringExtractorGDBRemote &response);333334bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec);335336DataExtractor GetAuxvData() override;337338StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid);339340void GetMaxMemorySize();341342bool CalculateThreadStopInfo(ThreadGDBRemote *thread);343344size_t UpdateThreadPCsFromStopReplyThreadsValue(llvm::StringRef value);345346size_t UpdateThreadIDsFromStopReplyThreadsValue(llvm::StringRef value);347348bool StartAsyncThread();349350void StopAsyncThread();351352lldb::thread_result_t AsyncThread();353354static void355MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp,356lldb::pid_t pid, int signo, int exit_status);357358lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet);359360bool361GetThreadStopInfoFromJSON(ThreadGDBRemote *thread,362const StructuredData::ObjectSP &thread_infos_sp);363364lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict);365366lldb::ThreadSP367SetThreadStopInfo(lldb::tid_t tid,368ExpeditedRegisterMap &expedited_register_map, uint8_t signo,369const std::string &thread_name, const std::string &reason,370const std::string &description, uint32_t exc_type,371const std::vector<lldb::addr_t> &exc_data,372lldb::addr_t thread_dispatch_qaddr, bool queue_vars_valid,373lldb_private::LazyBool associated_with_libdispatch_queue,374lldb::addr_t dispatch_queue_t, std::string &queue_name,375lldb::QueueKind queue_kind, uint64_t queue_serial);376377void ClearThreadIDList();378379bool UpdateThreadIDList();380381void DidLaunchOrAttach(ArchSpec &process_arch);382void LoadStubBinaries();383void MaybeLoadExecutableModule();384385Status ConnectToDebugserver(llvm::StringRef host_port);386387const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr,388std::string &dispatch_queue_name);389390DynamicLoader *GetDynamicLoader() override;391392bool GetGDBServerRegisterInfoXMLAndProcess(393ArchSpec &arch_to_use, std::string xml_filename,394std::vector<DynamicRegisterInfo::Register> ®isters);395396// Convert DynamicRegisterInfo::Registers into RegisterInfos and add397// to the dynamic register list.398void AddRemoteRegisters(std::vector<DynamicRegisterInfo::Register> ®isters,399const ArchSpec &arch_to_use);400// Query remote GDBServer for register information401bool GetGDBServerRegisterInfo(ArchSpec &arch);402403lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file,404lldb::addr_t link_map,405lldb::addr_t base_addr,406bool value_is_offset);407408Status UpdateAutomaticSignalFiltering() override;409410Status FlashErase(lldb::addr_t addr, size_t size);411412Status FlashDone();413414bool HasErased(FlashRange range);415416llvm::Expected<std::vector<uint8_t>>417DoReadMemoryTags(lldb::addr_t addr, size_t len, int32_t type) override;418419Status DoWriteMemoryTags(lldb::addr_t addr, size_t len, int32_t type,420const std::vector<uint8_t> &tags) override;421422Status DoGetMemoryRegionInfo(lldb::addr_t load_addr,423MemoryRegionInfo ®ion_info) override;424425private:426// For ProcessGDBRemote only427std::string m_partial_profile_data;428std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;429uint64_t m_last_signals_version = 0;430431static bool NewThreadNotifyBreakpointHit(void *baton,432StoppointCallbackContext *context,433lldb::user_id_t break_id,434lldb::user_id_t break_loc_id);435436// ContinueDelegate interface437void HandleAsyncStdout(llvm::StringRef out) override;438void HandleAsyncMisc(llvm::StringRef data) override;439void HandleStopReply() override;440void HandleAsyncStructuredDataPacket(llvm::StringRef data) override;441442void SetThreadPc(const lldb::ThreadSP &thread_sp, uint64_t index);443using ModuleCacheKey = std::pair<std::string, std::string>;444// KeyInfo for the cached module spec DenseMap.445// The invariant is that all real keys will have the file and architecture446// set.447// The empty key has an empty file and an empty arch.448// The tombstone key has an invalid arch and an empty file.449// The comparison and hash functions take the file name and architecture450// triple into account.451struct ModuleCacheInfo {452static ModuleCacheKey getEmptyKey() { return ModuleCacheKey(); }453454static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); }455456static unsigned getHashValue(const ModuleCacheKey &key) {457return llvm::hash_combine(key.first, key.second);458}459460static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) {461return LHS == RHS;462}463};464465llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo>466m_cached_module_specs;467468ProcessGDBRemote(const ProcessGDBRemote &) = delete;469const ProcessGDBRemote &operator=(const ProcessGDBRemote &) = delete;470471// fork helpers472void DidForkSwitchSoftwareBreakpoints(bool enable);473void DidForkSwitchHardwareTraps(bool enable);474475void ParseExpeditedRegisters(ExpeditedRegisterMap &expedited_register_map,476lldb::ThreadSP thread_sp);477478// Lists of register fields generated from the remote's target XML.479// Pointers to these RegisterFlags will be set in the register info passed480// back to the upper levels of lldb. Doing so is safe because this class will481// live at least as long as the debug session. We therefore do not store the482// data directly in the map because the map may reallocate it's storage as new483// entries are added. Which would invalidate any pointers set in the register484// info up to that point.485llvm::StringMap<std::unique_ptr<RegisterFlags>> m_registers_flags_types;486487// Enum types are referenced by register fields. This does not store the data488// directly because the map may reallocate. Pointers to these are contained489// within instances of RegisterFlags.490llvm::StringMap<std::unique_ptr<FieldEnum>> m_registers_enum_types;491};492493} // namespace process_gdb_remote494} // namespace lldb_private495496#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_PROCESSGDBREMOTE_H497498499