Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h
39644 views
//===-- GDBRemoteCommunicationServerLLGS.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_GDBREMOTECOMMUNICATIONSERVERLLGS_H9#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H1011#include <mutex>12#include <unordered_map>13#include <unordered_set>1415#include "lldb/Core/Communication.h"16#include "lldb/Host/MainLoop.h"17#include "lldb/Host/common/NativeProcessProtocol.h"18#include "lldb/Utility/RegisterValue.h"19#include "lldb/lldb-private-forward.h"2021#include "GDBRemoteCommunicationServerCommon.h"2223class StringExtractorGDBRemote;2425namespace lldb_private {2627namespace process_gdb_remote {2829class ProcessGDBRemote;3031class GDBRemoteCommunicationServerLLGS32: public GDBRemoteCommunicationServerCommon,33public NativeProcessProtocol::NativeDelegate {34public:35// Constructors and Destructors36GDBRemoteCommunicationServerLLGS(37MainLoop &mainloop,38NativeProcessProtocol::Manager &process_manager);3940void SetLaunchInfo(const ProcessLaunchInfo &info);4142/// Launch a process with the current launch settings.43///44/// This method supports running an lldb-gdbserver or similar45/// server in a situation where the startup code has been provided46/// with all the information for a child process to be launched.47///48/// \return49/// An Status object indicating the success or failure of the50/// launch.51Status LaunchProcess() override;5253/// Attach to a process.54///55/// This method supports attaching llgs to a process accessible via the56/// configured Platform.57///58/// \return59/// An Status object indicating the success or failure of the60/// attach operation.61Status AttachToProcess(lldb::pid_t pid);6263/// Wait to attach to a process with a given name.64///65/// This method supports waiting for the next instance of a process66/// with a given name and attaching llgs to that via the configured67/// Platform.68///69/// \return70/// An Status object indicating the success or failure of the71/// attach operation.72Status AttachWaitProcess(llvm::StringRef process_name, bool include_existing);7374// NativeProcessProtocol::NativeDelegate overrides75void InitializeDelegate(NativeProcessProtocol *process) override;7677void ProcessStateChanged(NativeProcessProtocol *process,78lldb::StateType state) override;7980void DidExec(NativeProcessProtocol *process) override;8182void83NewSubprocess(NativeProcessProtocol *parent_process,84std::unique_ptr<NativeProcessProtocol> child_process) override;8586Status InitializeConnection(std::unique_ptr<Connection> connection);8788struct DebuggedProcess {89enum class Flag {90vkilled = (1u << 0),9192LLVM_MARK_AS_BITMASK_ENUM(vkilled)93};9495std::unique_ptr<NativeProcessProtocol> process_up;96Flag flags;97};9899protected:100MainLoop &m_mainloop;101MainLoop::ReadHandleUP m_network_handle_up;102NativeProcessProtocol::Manager &m_process_manager;103lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;104lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;105NativeProcessProtocol *m_current_process;106NativeProcessProtocol *m_continue_process;107std::recursive_mutex m_debugged_process_mutex;108std::unordered_map<lldb::pid_t, DebuggedProcess> m_debugged_processes;109110Communication m_stdio_communication;111MainLoop::ReadHandleUP m_stdio_handle_up;112113llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map;114std::mutex m_saved_registers_mutex;115std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;116uint32_t m_next_saved_registers_id = 1;117bool m_thread_suffix_supported = false;118bool m_list_threads_in_stop_reply = false;119bool m_non_stop = false;120bool m_disabling_non_stop = false;121std::deque<std::string> m_stdio_notification_queue;122std::deque<std::string> m_stop_notification_queue;123124NativeProcessProtocol::Extension m_extensions_supported = {};125126// Typically we would use a SmallVector for this data but in this context we127// don't know how much data we're recieving so we would have to heap allocate128// a lot, or have a very large stack frame. So it's a member instead.129uint8_t m_reg_bytes[RegisterValue::kMaxRegisterByteSize];130131PacketResult SendONotification(const char *buffer, uint32_t len);132133PacketResult SendWResponse(NativeProcessProtocol *process);134135StreamString PrepareStopReplyPacketForThread(NativeThreadProtocol &thread);136137PacketResult SendStopReplyPacketForThread(NativeProcessProtocol &process,138lldb::tid_t tid,139bool force_synchronous);140141PacketResult SendStopReasonForState(NativeProcessProtocol &process,142lldb::StateType process_state,143bool force_synchronous);144145void EnqueueStopReplyPackets(lldb::tid_t thread_to_skip);146147PacketResult Handle_k(StringExtractorGDBRemote &packet);148149PacketResult Handle_vKill(StringExtractorGDBRemote &packet);150151PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);152153PacketResult Handle_qC(StringExtractorGDBRemote &packet);154155PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);156157PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);158159PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);160161PacketResult Handle_QThreadSuffixSupported(StringExtractorGDBRemote &packet);162163PacketResult Handle_QListThreadsInStopReply(StringExtractorGDBRemote &packet);164165PacketResult ResumeProcess(NativeProcessProtocol &process,166const ResumeActionList &actions);167168PacketResult Handle_C(StringExtractorGDBRemote &packet);169170PacketResult Handle_c(StringExtractorGDBRemote &packet);171172PacketResult Handle_vCont(StringExtractorGDBRemote &packet);173174PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);175176PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);177178PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);179180void AddProcessThreads(StreamGDBRemote &response,181NativeProcessProtocol &process, bool &had_any);182183PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);184185PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);186187PacketResult Handle_p(StringExtractorGDBRemote &packet);188189PacketResult Handle_P(StringExtractorGDBRemote &packet);190191PacketResult Handle_H(StringExtractorGDBRemote &packet);192193PacketResult Handle_I(StringExtractorGDBRemote &packet);194195PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);196197// Handles $m and $x packets.198PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);199200PacketResult Handle_M(StringExtractorGDBRemote &packet);201PacketResult Handle__M(StringExtractorGDBRemote &packet);202PacketResult Handle__m(StringExtractorGDBRemote &packet);203204PacketResult205Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);206207PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);208209PacketResult Handle_Z(StringExtractorGDBRemote &packet);210211PacketResult Handle_z(StringExtractorGDBRemote &packet);212213PacketResult Handle_s(StringExtractorGDBRemote &packet);214215PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);216217PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);218219PacketResult Handle_jLLDBTraceSupported(StringExtractorGDBRemote &packet);220221PacketResult Handle_jLLDBTraceStart(StringExtractorGDBRemote &packet);222223PacketResult Handle_jLLDBTraceStop(StringExtractorGDBRemote &packet);224225PacketResult Handle_jLLDBTraceGetState(StringExtractorGDBRemote &packet);226227PacketResult Handle_jLLDBTraceGetBinaryData(StringExtractorGDBRemote &packet);228229PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);230231PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);232233PacketResult Handle_vAttachWait(StringExtractorGDBRemote &packet);234235PacketResult Handle_qVAttachOrWaitSupported(StringExtractorGDBRemote &packet);236237PacketResult Handle_vAttachOrWait(StringExtractorGDBRemote &packet);238239PacketResult Handle_vRun(StringExtractorGDBRemote &packet);240241PacketResult Handle_D(StringExtractorGDBRemote &packet);242243PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);244245PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);246247PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);248249PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);250251PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);252253PacketResult Handle_qSaveCore(StringExtractorGDBRemote &packet);254255PacketResult Handle_QNonStop(StringExtractorGDBRemote &packet);256257PacketResult HandleNotificationAck(std::deque<std::string> &queue);258259PacketResult Handle_vStdio(StringExtractorGDBRemote &packet);260261PacketResult Handle_vStopped(StringExtractorGDBRemote &packet);262263PacketResult Handle_vCtrlC(StringExtractorGDBRemote &packet);264265PacketResult Handle_g(StringExtractorGDBRemote &packet);266267PacketResult Handle_qMemTags(StringExtractorGDBRemote &packet);268269PacketResult Handle_QMemTags(StringExtractorGDBRemote &packet);270271PacketResult Handle_T(StringExtractorGDBRemote &packet);272273void SetCurrentThreadID(lldb::tid_t tid);274275lldb::tid_t GetCurrentThreadID() const;276277void SetContinueThreadID(lldb::tid_t tid);278279lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }280281Status SetSTDIOFileDescriptor(int fd);282283FileSpec FindModuleFile(const std::string &module_path,284const ArchSpec &arch) override;285286llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>287ReadXferObject(llvm::StringRef object, llvm::StringRef annex);288289static std::string XMLEncodeAttributeValue(llvm::StringRef value);290291std::vector<std::string> HandleFeatures(292const llvm::ArrayRef<llvm::StringRef> client_features) override;293294// Provide a response for successful continue action, i.e. send "OK"295// in non-stop mode, no response otherwise.296PacketResult SendContinueSuccessResponse();297298void AppendThreadIDToResponse(Stream &response, lldb::pid_t pid,299lldb::tid_t tid);300301private:302llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml();303304void HandleInferiorState_Exited(NativeProcessProtocol *process);305306void HandleInferiorState_Stopped(NativeProcessProtocol *process);307308NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);309310uint32_t GetNextSavedRegistersID();311312void MaybeCloseInferiorTerminalConnection();313314void ClearProcessSpecificData();315316void RegisterPacketHandlers();317318void DataAvailableCallback();319320void SendProcessOutput();321322void StartSTDIOForwarding();323324void StopSTDIOForwarding();325326// Call SetEnabledExtensions() with appropriate flags on the process.327void SetEnabledExtensions(NativeProcessProtocol &process);328329// For GDBRemoteCommunicationServerLLGS only330GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) =331delete;332const GDBRemoteCommunicationServerLLGS &333operator=(const GDBRemoteCommunicationServerLLGS &) = delete;334};335336std::string LLGSArgToURL(llvm::StringRef url_arg, bool reverse_connect);337338} // namespace process_gdb_remote339} // namespace lldb_private340341#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H342343344