| 1 | //===-- GDBRemoteCommunicationServerLLGS.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_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H |
| 10 | #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H |
| 11 | |
| 12 | #include <mutex> |
| 13 | #include <unordered_map> |
| 14 | #include <unordered_set> |
| 15 | |
| 16 | #include "lldb/Core/Communication.h" |
| 17 | #include "lldb/Host/MainLoop.h" |
| 18 | #include "lldb/Host/common/NativeProcessProtocol.h" |
| 19 | #include "lldb/Utility/RegisterValue.h" |
| 20 | #include "lldb/lldb-private-forward.h" |
| 21 | |
| 22 | #include "GDBRemoteCommunicationServerCommon.h" |
| 23 | |
| 24 | class ; |
| 25 | |
| 26 | namespace lldb_private { |
| 27 | |
| 28 | namespace process_gdb_remote { |
| 29 | |
| 30 | class ProcessGDBRemote; |
| 31 | |
| 32 | class GDBRemoteCommunicationServerLLGS |
| 33 | : public GDBRemoteCommunicationServerCommon, |
| 34 | public NativeProcessProtocol::NativeDelegate { |
| 35 | public: |
| 36 | // Constructors and Destructors |
| 37 | GDBRemoteCommunicationServerLLGS( |
| 38 | MainLoop &mainloop, |
| 39 | NativeProcessProtocol::Manager &process_manager); |
| 40 | |
| 41 | void SetLaunchInfo(const ProcessLaunchInfo &info); |
| 42 | |
| 43 | /// Launch a process with the current launch settings. |
| 44 | /// |
| 45 | /// This method supports running an lldb-gdbserver or similar |
| 46 | /// server in a situation where the startup code has been provided |
| 47 | /// with all the information for a child process to be launched. |
| 48 | /// |
| 49 | /// \return |
| 50 | /// An Status object indicating the success or failure of the |
| 51 | /// launch. |
| 52 | Status LaunchProcess() override; |
| 53 | |
| 54 | /// Attach to a process. |
| 55 | /// |
| 56 | /// This method supports attaching llgs to a process accessible via the |
| 57 | /// configured Platform. |
| 58 | /// |
| 59 | /// \return |
| 60 | /// An Status object indicating the success or failure of the |
| 61 | /// attach operation. |
| 62 | Status AttachToProcess(lldb::pid_t pid); |
| 63 | |
| 64 | /// Wait to attach to a process with a given name. |
| 65 | /// |
| 66 | /// This method supports waiting for the next instance of a process |
| 67 | /// with a given name and attaching llgs to that via the configured |
| 68 | /// Platform. |
| 69 | /// |
| 70 | /// \return |
| 71 | /// An Status object indicating the success or failure of the |
| 72 | /// attach operation. |
| 73 | Status AttachWaitProcess(llvm::StringRef process_name, bool include_existing); |
| 74 | |
| 75 | // NativeProcessProtocol::NativeDelegate overrides |
| 76 | void InitializeDelegate(NativeProcessProtocol *process) override; |
| 77 | |
| 78 | void ProcessStateChanged(NativeProcessProtocol *process, |
| 79 | lldb::StateType state) override; |
| 80 | |
| 81 | void DidExec(NativeProcessProtocol *process) override; |
| 82 | |
| 83 | void |
| 84 | NewSubprocess(NativeProcessProtocol *parent_process, |
| 85 | std::unique_ptr<NativeProcessProtocol> child_process) override; |
| 86 | |
| 87 | Status InitializeConnection(std::unique_ptr<Connection> connection); |
| 88 | |
| 89 | struct DebuggedProcess { |
| 90 | enum class Flag { |
| 91 | vkilled = (1u << 0), |
| 92 | |
| 93 | LLVM_MARK_AS_BITMASK_ENUM(vkilled) |
| 94 | }; |
| 95 | |
| 96 | std::unique_ptr<NativeProcessProtocol> process_up; |
| 97 | Flag flags; |
| 98 | }; |
| 99 | |
| 100 | protected: |
| 101 | MainLoop &m_mainloop; |
| 102 | MainLoop::ReadHandleUP m_network_handle_up; |
| 103 | NativeProcessProtocol::Manager &m_process_manager; |
| 104 | lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID; |
| 105 | lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID; |
| 106 | NativeProcessProtocol *m_current_process; |
| 107 | NativeProcessProtocol *m_continue_process; |
| 108 | std::recursive_mutex m_debugged_process_mutex; |
| 109 | std::unordered_map<lldb::pid_t, DebuggedProcess> m_debugged_processes; |
| 110 | |
| 111 | Communication m_stdio_communication; |
| 112 | MainLoop::ReadHandleUP m_stdio_handle_up; |
| 113 | |
| 114 | llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map; |
| 115 | std::mutex m_saved_registers_mutex; |
| 116 | std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map; |
| 117 | uint32_t m_next_saved_registers_id = 1; |
| 118 | bool m_thread_suffix_supported = false; |
| 119 | bool m_list_threads_in_stop_reply = false; |
| 120 | bool m_non_stop = false; |
| 121 | bool m_disabling_non_stop = false; |
| 122 | std::deque<std::string> m_stdio_notification_queue; |
| 123 | std::deque<std::string> m_stop_notification_queue; |
| 124 | |
| 125 | NativeProcessProtocol::Extension m_extensions_supported = {}; |
| 126 | |
| 127 | // Typically we would use a SmallVector for this data but in this context we |
| 128 | // don't know how much data we're recieving so we would have to heap allocate |
| 129 | // a lot, or have a very large stack frame. So it's a member instead. |
| 130 | uint8_t m_reg_bytes[RegisterValue::kMaxRegisterByteSize]; |
| 131 | |
| 132 | PacketResult SendONotification(const char *buffer, uint32_t len); |
| 133 | |
| 134 | PacketResult SendWResponse(NativeProcessProtocol *process); |
| 135 | |
| 136 | StreamString PrepareStopReplyPacketForThread(NativeThreadProtocol &thread); |
| 137 | |
| 138 | PacketResult SendStopReplyPacketForThread(NativeProcessProtocol &process, |
| 139 | lldb::tid_t tid, |
| 140 | bool force_synchronous); |
| 141 | |
| 142 | PacketResult SendStopReasonForState(NativeProcessProtocol &process, |
| 143 | lldb::StateType process_state, |
| 144 | bool force_synchronous); |
| 145 | |
| 146 | void EnqueueStopReplyPackets(lldb::tid_t thread_to_skip); |
| 147 | |
| 148 | PacketResult Handle_k(StringExtractorGDBRemote &packet); |
| 149 | |
| 150 | PacketResult Handle_vKill(StringExtractorGDBRemote &packet); |
| 151 | |
| 152 | PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet); |
| 153 | |
| 154 | PacketResult Handle_qC(StringExtractorGDBRemote &packet); |
| 155 | |
| 156 | PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet); |
| 157 | |
| 158 | PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet); |
| 159 | |
| 160 | PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet); |
| 161 | |
| 162 | PacketResult Handle_QThreadSuffixSupported(StringExtractorGDBRemote &packet); |
| 163 | |
| 164 | PacketResult Handle_QListThreadsInStopReply(StringExtractorGDBRemote &packet); |
| 165 | |
| 166 | PacketResult ResumeProcess(NativeProcessProtocol &process, |
| 167 | const ResumeActionList &actions); |
| 168 | |
| 169 | PacketResult Handle_C(StringExtractorGDBRemote &packet); |
| 170 | |
| 171 | PacketResult Handle_c(StringExtractorGDBRemote &packet); |
| 172 | |
| 173 | PacketResult Handle_vCont(StringExtractorGDBRemote &packet); |
| 174 | |
| 175 | PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet); |
| 176 | |
| 177 | PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet); |
| 178 | |
| 179 | PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet); |
| 180 | |
| 181 | void AddProcessThreads(StreamGDBRemote &response, |
| 182 | NativeProcessProtocol &process, bool &had_any); |
| 183 | |
| 184 | PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet); |
| 185 | |
| 186 | PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet); |
| 187 | |
| 188 | PacketResult Handle_p(StringExtractorGDBRemote &packet); |
| 189 | |
| 190 | PacketResult Handle_P(StringExtractorGDBRemote &packet); |
| 191 | |
| 192 | PacketResult Handle_H(StringExtractorGDBRemote &packet); |
| 193 | |
| 194 | PacketResult Handle_I(StringExtractorGDBRemote &packet); |
| 195 | |
| 196 | PacketResult Handle_interrupt(StringExtractorGDBRemote &packet); |
| 197 | |
| 198 | // Handles $m and $x packets. |
| 199 | PacketResult Handle_memory_read(StringExtractorGDBRemote &packet); |
| 200 | |
| 201 | PacketResult Handle_M(StringExtractorGDBRemote &packet); |
| 202 | PacketResult Handle__M(StringExtractorGDBRemote &packet); |
| 203 | PacketResult Handle__m(StringExtractorGDBRemote &packet); |
| 204 | |
| 205 | PacketResult |
| 206 | Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet); |
| 207 | |
| 208 | PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet); |
| 209 | |
| 210 | PacketResult Handle_Z(StringExtractorGDBRemote &packet); |
| 211 | |
| 212 | PacketResult Handle_z(StringExtractorGDBRemote &packet); |
| 213 | |
| 214 | PacketResult Handle_s(StringExtractorGDBRemote &packet); |
| 215 | |
| 216 | PacketResult Handle_qXfer(StringExtractorGDBRemote &packet); |
| 217 | |
| 218 | PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet); |
| 219 | |
| 220 | PacketResult Handle_jLLDBTraceSupported(StringExtractorGDBRemote &packet); |
| 221 | |
| 222 | PacketResult Handle_jLLDBTraceStart(StringExtractorGDBRemote &packet); |
| 223 | |
| 224 | PacketResult Handle_jLLDBTraceStop(StringExtractorGDBRemote &packet); |
| 225 | |
| 226 | PacketResult Handle_jLLDBTraceGetState(StringExtractorGDBRemote &packet); |
| 227 | |
| 228 | PacketResult Handle_jLLDBTraceGetBinaryData(StringExtractorGDBRemote &packet); |
| 229 | |
| 230 | PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet); |
| 231 | |
| 232 | PacketResult Handle_vAttach(StringExtractorGDBRemote &packet); |
| 233 | |
| 234 | PacketResult Handle_vAttachWait(StringExtractorGDBRemote &packet); |
| 235 | |
| 236 | PacketResult Handle_qVAttachOrWaitSupported(StringExtractorGDBRemote &packet); |
| 237 | |
| 238 | PacketResult Handle_vAttachOrWait(StringExtractorGDBRemote &packet); |
| 239 | |
| 240 | PacketResult Handle_vRun(StringExtractorGDBRemote &packet); |
| 241 | |
| 242 | PacketResult Handle_D(StringExtractorGDBRemote &packet); |
| 243 | |
| 244 | PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet); |
| 245 | |
| 246 | PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet); |
| 247 | |
| 248 | PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet); |
| 249 | |
| 250 | PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet); |
| 251 | |
| 252 | PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet); |
| 253 | |
| 254 | PacketResult Handle_qSaveCore(StringExtractorGDBRemote &packet); |
| 255 | |
| 256 | PacketResult Handle_QNonStop(StringExtractorGDBRemote &packet); |
| 257 | |
| 258 | PacketResult HandleNotificationAck(std::deque<std::string> &queue); |
| 259 | |
| 260 | PacketResult Handle_vStdio(StringExtractorGDBRemote &packet); |
| 261 | |
| 262 | PacketResult Handle_vStopped(StringExtractorGDBRemote &packet); |
| 263 | |
| 264 | PacketResult Handle_vCtrlC(StringExtractorGDBRemote &packet); |
| 265 | |
| 266 | PacketResult Handle_g(StringExtractorGDBRemote &packet); |
| 267 | |
| 268 | PacketResult Handle_qMemTags(StringExtractorGDBRemote &packet); |
| 269 | |
| 270 | PacketResult Handle_QMemTags(StringExtractorGDBRemote &packet); |
| 271 | |
| 272 | PacketResult Handle_T(StringExtractorGDBRemote &packet); |
| 273 | |
| 274 | void SetCurrentThreadID(lldb::tid_t tid); |
| 275 | |
| 276 | lldb::tid_t GetCurrentThreadID() const; |
| 277 | |
| 278 | void SetContinueThreadID(lldb::tid_t tid); |
| 279 | |
| 280 | lldb::tid_t GetContinueThreadID() const { return m_continue_tid; } |
| 281 | |
| 282 | Status SetSTDIOFileDescriptor(int fd); |
| 283 | |
| 284 | FileSpec FindModuleFile(const std::string &module_path, |
| 285 | const ArchSpec &arch) override; |
| 286 | |
| 287 | llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> |
| 288 | ReadXferObject(llvm::StringRef object, llvm::StringRef annex); |
| 289 | |
| 290 | static std::string XMLEncodeAttributeValue(llvm::StringRef value); |
| 291 | |
| 292 | std::vector<std::string> HandleFeatures( |
| 293 | const llvm::ArrayRef<llvm::StringRef> client_features) override; |
| 294 | |
| 295 | // Provide a response for successful continue action, i.e. send "OK" |
| 296 | // in non-stop mode, no response otherwise. |
| 297 | PacketResult SendContinueSuccessResponse(); |
| 298 | |
| 299 | void AppendThreadIDToResponse(Stream &response, lldb::pid_t pid, |
| 300 | lldb::tid_t tid); |
| 301 | |
| 302 | private: |
| 303 | llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml(); |
| 304 | |
| 305 | void HandleInferiorState_Exited(NativeProcessProtocol *process); |
| 306 | |
| 307 | void HandleInferiorState_Stopped(NativeProcessProtocol *process); |
| 308 | |
| 309 | NativeThreadProtocol *(StringExtractorGDBRemote &packet); |
| 310 | |
| 311 | uint32_t GetNextSavedRegistersID(); |
| 312 | |
| 313 | void MaybeCloseInferiorTerminalConnection(); |
| 314 | |
| 315 | void ClearProcessSpecificData(); |
| 316 | |
| 317 | void RegisterPacketHandlers(); |
| 318 | |
| 319 | void DataAvailableCallback(); |
| 320 | |
| 321 | void SendProcessOutput(); |
| 322 | |
| 323 | void StartSTDIOForwarding(); |
| 324 | |
| 325 | void StopSTDIOForwarding(); |
| 326 | |
| 327 | // Call SetEnabledExtensions() with appropriate flags on the process. |
| 328 | void SetEnabledExtensions(NativeProcessProtocol &process); |
| 329 | |
| 330 | // For GDBRemoteCommunicationServerLLGS only |
| 331 | GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) = |
| 332 | delete; |
| 333 | const GDBRemoteCommunicationServerLLGS & |
| 334 | operator=(const GDBRemoteCommunicationServerLLGS &) = delete; |
| 335 | }; |
| 336 | |
| 337 | std::string LLGSArgToURL(llvm::StringRef url_arg, bool reverse_connect); |
| 338 | |
| 339 | } // namespace process_gdb_remote |
| 340 | } // namespace lldb_private |
| 341 | |
| 342 | #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H |
| 343 | |